Skip to content

RISC-V: the generated crate's instructions as bytes, not one directive each - #5099

Merged
alex merged 1 commit into
mainfrom
claude/kind-mayer-ugzssx
Nov 15, 2026
Merged

alex merged 1 commit into
mainfrom
claude/kind-mayer-ugzssx

Conversation

@alex

@alex alex commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

The emitter writes each run of encoded instructions of x86 and PPC64LE as one .base64 line (Rust.packRun), and joins AArch64's .inst words eight to a line. RISC-V's .insn lines were neither packed nor joined: crates/verified-garbage-asm-riscv64/src was 919,461 directives, one per instruction, 24.8 MB of Rust for rustc to lex and expand and for the assembler to parse.

Rust.insnLine? reads the bytes of a .insn 4, 0x… or .insn 2, 0x… line (little-endian parcels), so packRun packs RISC-V's runs as it does the others'. Branches, jumps, calls and lla stay text, as before.

Trust change (TCB/Rust.lean)

This changes the printer, which no proof imports (only TCB/Emit.lean and the tests do), and regenerates the riscv64 crate with it. What it emits is the same machine code:

  • The .text and .rodata of all three test binaries (lib, tests, cavp), built for riscv64gc-unknown-linux-gnu and linked with GNU ld, are byte for byte what main's are.
  • lake env lean --run Emit.lean --check passes, and only the riscv64 crate's src/ changed (381 files); assembly/ is untouched.
  • The byte order is the manual's: The RISC-V Instruction Set Manual, Volume I, "Base Instruction-Length Encoding" (instructions are sequences of 16-bit little-endian parcels, lowest-addressed parcel holding the lowest-numbered bits).

One visible difference: the object's mapping symbols (RISC-V ELF psABI, "Mapping Symbol") now mark the packed runs as data ($d) where .insn marked them as instructions ($x). A disassembler of a riscv64 object therefore prints those runs as words rather than mnemonics; their text is in assembly/, as for every target. This is the reason AArch64 keeps .inst, so it is a judgment call here: say so if you would rather keep the marking and I will close this.

What it saves

Measured on an x86-64 host, cross-compiling for riscv64 with CI's flags:

before after
riscv64 crate's source 24.8 MB 12.1 MB
compiling that crate 0.83 s 0.40 s
of which macro expansion 0.38 s 0.14 s
of which codegen and assembling 0.34 s 0.11 s

On the RISC-V runner, before this change, the crate takes 12 s of CPU and 9 s of wall time: expansion 2.3 s, codegen 2.2 s, assembling 2.7 s (measurement run, time-passes). The rest of the test build waits only for the crate's metadata, so of this only the expansion is on the build's critical path: by the ratios above, expect about 1.5 s off the 81 s build and about 5 s less CPU.

Not measured on the runner. This PR's own Rust (Linux, RISC-V, host) job cannot show it: it ran on a slower runner than the baseline's (the tests, whose machine code is identical, took 35 s, 7.4 s and 87 s against 32 s, 5.5 s and 64 s), and a 1.5 s change is below that noise.

Checks run locally

Every step of Static checks, lake build, Emit.lean --check, check_lean_public_imports.py on the two edited modules, cargo fmt --check, Clippy for riscv64, and the riscv64 cavp and RFC tests under qemu-user.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHFazY8ZqFFZT1uMZF5

…e each

The emitter writes each run of encoded instructions of x86 and PPC64LE
as one `.base64` line (`Rust.packRun`), and joins AArch64's `.inst`
words eight to a line. RISC-V's `.insn` lines were neither: its crate
was 919,461 directives, one an instruction, 24.8 MB of Rust for rustc to
lex and expand and for the assembler to parse.

`Rust.insnLine?` reads a `.insn 4, 0x…` or `.insn 2, 0x…` line's bytes
(little-endian parcels), so `packRun` packs RISC-V's runs too. Branches,
jumps, calls and `lla` stay text, as before.

The test binaries' `.text` and `.rodata` are byte for byte what they
were (riscv64gc-unknown-linux-gnu, GNU ld). The crate's source is
12.1 MB, and compiling it takes 0.46 s rather than 0.83 s on x86-64; on
the RISC-V runner it took 12 s of CPU and 9 s, of which every other
crate of the test build waits for the first 6.

Trust: a change to the printer (`TCB/Rust.lean`), which no proof
imports. The object's mapping symbols now mark the packed runs as data
(`$d`) where `.insn` marked instructions (`$x`), so a disassembler of a
riscv64 object prints them as words; their text is in `assembly/`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFazY8ZqFFZT1uMZF5
@alex
alex added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit a0b1b00 Nov 15, 2026
22 checks passed
@alex
alex deleted the claude/kind-mayer-ugzssx branch November 15, 2026 05:11
alex pushed a commit that referenced this pull request Nov 15, 2026
The RISC-V crate's generated vg_rsa_keygen_candidate.rs conflicted after
#5099, which prints each straight-line instruction as bytes: it takes
main's side, and the crate is regenerated with the full emitter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGhkN8REQLPW8yxN5A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants