4. Instruction translation

This is attesor's "instruction set translation" with three of its four translation challenges: registers, vector instructions and calling conventions. Memory order has part 5.

One instruction at a time, or whole functions

Rosetta 2. Each x86 instruction becomes one or more ARM instructions, and the x86 state stays canonical at every instruction boundary. That makes exceptions, sampling profilers and debuggers precise. Dougall Johnson notes that it almost entirely prevents optimization between instructions, apart from removing dead flags and combining prologues and epilogues. Rosetta also keeps maps from translated addresses back to x86 addresses (Dougall Johnson, and the per-image metadata in Champollion part 2).

ScarletNX now. The opposite trade. LLVM optimizes whole functions at O2 by default (ir.c), and the CPU structure holds the architectural state only as of the last call site (src/common/cpu/recompiler/llvm/ir_promote.c). The code is faster for it, but a fault does not know its guest pc, and crash reports cannot name the faulting instruction. The plan keeps LLVM's optimization on every host and adds the map.

I1 A guest pc for every host instruction

Registers

Rosetta 2. Champollion part 1 ("Analyzing the code in AOT files: calling conventions") shows translated code keeping the 16 x86-64 general registers in x0 to x15, starting with RAX in x0, and AOT code calling with those registers in place. Guest state never goes to memory between instructions or across calls. Dougall Johnson points out that this works because x86-64 has half as many registers as AArch64, and that the same approach would be a real drawback for an A64 guest on x86-64.

ScarletNX now. Guest registers are fields of CPU. After emission, ir_promote.c turns them into SSA values for the whole function. At each call site it stores the registers written since the last site and reloads the live ones after the call. A call to a helper that touches only FPCR, FPSR and the exclusive monitor (snx-noregs) syncs only those, one that touches no register (snx-nocpu: DC ZVA's) or only memory it is given syncs none, and every other call syncs all (call_site in ir_promote.c).

On each host. A64 has 31 general registers, SP and 32 vector registers. x86-64 has 16 and 16 (32 with AVX-512), so a full map is impossible there, and fewer stores and reloads at calls are the gain. AArch64 has the guest's own registers. PowerPC64, RISC-V and LoongArch each have 32 general registers less three or four reserved ones, so a map close to Rosetta's is possible on every host but x86-64.

I2 An ABI contract mode, with a checker
I3 Pass guest registers in host registers

Calls and returns

Rosetta 2. CALL and RET become BL and RET, so the host's return predictor works. Dougall Johnson describes a separate stack of pairs, the expected x86 return address and its translated target, that each return checks before it trusts the host return. Champollion's memory map shows a region named "Rosetta Return Stack" (part 1).

ScarletNX now. The equivalent, on every host. A guest BL is a host call and RET is a host return. The caller then checks that c->pc equals its own pc+4 and leaves for the dispatch loop when it does not (check_return in ir_codegen.c). Every backward branch inside a function also reads c->poll, so a loop without SVCs yields to a waiting thread of higher priority.

I4 Return the next guest pc in a register
I5 Cheaper polls, if they cost

Flags and condition codes

Rosetta 2. Rosetta does not compute flags that every path overwrites before a read (Dougall Johnson). It uses CFINV, RMIF and SETF8/SETF16 from FEAT_FlagM, AXFLAG/XAFLAG from FEAT_FlagM2 for FP compares, and an undocumented Apple extension that computes x86 PF and AF into bits 26 and 27 of NZCV. Without that extension, in Linux VMs, PF costs five instructions per operation.

ScarletNX now. Each flag-setting instruction computes N, Z, C and V as four 32-bit values, and records which compare produced them (flags_origin in src/common/cpu/recompiler/llvm/ir_state.c). A B.cond in the same block then folds into one host compare and branch. After promotion LLVM drops the flags nothing reads. A flag origin does not reach past its block.

On each host. AArch64 has NZCV itself. x86-64 has flags, with the carry of a subtraction inverted against Arm's C. PowerPC64 has condition register fields and a carry bit in XER. RISC-V and LoongArch have no integer flags, so each NZCV value costs instructions there, and folding compares into branches matters most on those two hosts.

I6 Flags across blocks

Vector instructions

Rosetta 2. SSE maps to reasonably optimized sequences of NEON operations (Dougall Johnson).

ScarletNX now. The AdvSIMD and crypto encodings are emitted as LLVM vector IR, and the module uses the host CPU's own extensions (snx_ir_host in ir.c). On x86-64 the emitters use AES-NI, PCLMULQDQ, SSE4.2 CRC32C, VPTERNLOG, PAVGB and GF2P8AFFINEQB when the host has them (docs/dev/architecture.md). Some groups still call helpers. The per-lane helpers of vector FP go through ir_vec_call without the snx-noregs marking. A call is a full promotion site that stores dirty registers and reloads live registers unless an earlier scalar FP declaration marked the helper (ir_vec.c, ir_emit.c, ir_promote.c). The scalar forms of FRECPE, FRSQRTE and FRECPX, and the vector forms of the first two, call one helper per lane (unary_form in ir_vec_fp.c). SRSHL, URSHL, SQSHL, UQSHL, SQRSHL and UQRSHL by register call snx_vec_shl, and the helpers of rtlib.c are a separate object that LLVM cannot inline (ir.c).

On each host. LLVM lowers the same vector IR to SSE through AVX-512, NEON, VSX, the RISC-V V extension or LSX and LASX. A RISC-V host without the V extension gets scalar code. Big-endian PowerPC64 needs care with lane order wherever the emitters move a vector through memory.

I15 Whole-vector operations instead of serial lane helpers

Made from docs/layers/cpu/plan.md at 69237645142c. GPL-3.0-or-later.