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.
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.
crash.c, gdb_jit.c and perf can then map a host pc to a guest pc. Rebuilding the registers at a fault would need
LLVM stack maps at memory operations, which can come later if a use appears (R1 is one).ir_codegen.c, gdb_jit.c, src/nx/runtime/crash.c.crash.c.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.
X19 to X29 and SP, and it cannot read X9 to X17 or NZCV as inputs. Add an abi_contract recompile setting
that lets promotion use these rules at guest BL and BLR sites. Callee-saved registers then stay in host registers
or host stack slots across the call with no store or reload. Caller-saved temporaries that are dead after the call
are not stored. SVC sites, helper calls and the paths into the runtime keep today's rules. A checking mode records
X19 to X29 and SP at each function entry and compares them at each normal return, and it logs the guest pc of any
function that breaks the contract. Exceptions and longjmp return to another pc, and the mismatch path already
reloads from the CPU structure.ir_promote.c, ir_codegen.c, settings.txt, the JIT cache key in jit_cache.c.native_bench shows the time.preserve_none convention where the target has it, with guest registers as
arguments and results. On x86-64 that covers the hot ones: X0 to X7, SP, X30 and the next pc. On AArch64,
PowerPC64, RISC-V and LoongArch it can cover nearly all of them, which comes close to Rosetta's fixed map. The CPU
structure stays the home of architectural state for the runtime and the cold paths. Tail calls keep working because
caller and callee share the convention. The thread loop and the miss path enter guest code through a small
trampoline with Clang's preserve_none attribute. A host whose LLVM lacks the convention keeps the C convention. On
macOS and Android, X18 stays free, because the platform reserves it.cpu.h, ir_codegen.c, ir_promote.c, src/nx/hle/kernel/thread.c, rtapi.c, jit_service.c.ctest -R module_contract).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.
void(CPU*, u64 entry) (cpu.h). RET stores c->pc and returns, and the
caller loads c->pc to compare it.pc+4, and only the mismatch path stores c->pc. The thread loop in thread.c takes the returned pc in place of
reading c->pc.cpu.h, ir_codegen.c, thread.c, jit_service.c, the AOT pack and variant paths.tests/native_module.c, the differential test in AOT and JIT forms, the calls kernel of native_bench,
and game runs that exercise exceptions and longjmp, which return to a different pc.ir_codegen.c.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.
B.cond in a
later block can still fold into one host compare. Store the flags at sites as one packed word in the promoted form.ir_state.c, ir_promote.c.CPU structure keeps four fields. Packing them in CPU too would be a bump.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.
Made from docs/layers/cpu/plan.md at 69237645142c. GPL-3.0-or-later.