Rosetta 2 part by part, applied to ScarletNX (4)

The first SIMD milestone turns these ideas into a bounded experiment. Use the conditional performance model, then compare its assumptions with the actual scene.

How Rosetta informs the design. The sources below describe observed Rosetta behavior. The final column is the proposed adaptation for ScarletNX, which must be proven independently.

Documented Rosetta behaviorEvidenceProposed application to ScarletNX
Common SSE2 operations map to short NEON sequences.Dougall Johnson, SSE2 discussionEmit a whole-vector sequence for the selected AdvSIMD group rather than calling a scalar helper for every lane.
The analyzed AOT convention maps XMM0 through XMM15 to q0 through q15.Champollion, calling conventionsKeep operands as vector SSA values within the generated function and inspect spills. Do not require the separate custom-ABI project for the first milestone.
Code expansion and instruction-cache cost influence translation choices.Dougall Johnson, one-to-one translation discussionCompare instruction count and generated bytes as well as helper calls. Reject oversized inline fixups without a measured benefit.
Apple hardware supports alternative FP behavior used by Rosetta.Dougall Johnson, floating-point discussionUse matching hardware only where guest bits and flags are proven. Otherwise use vector masks and exact fallback. Never assume a portable Arm-compatibility FP mode.

These observations do not establish Rosetta's exact exceptional-lane algorithm. Batched helpers, lane masks and FPSR reductions in this plan are ScarletNX proposals. Borrow the compact SIMD mapping principle without assuming access to Apple's hardware or host OS integration.

The following comparison was rerun with LLVM 23.1.4. These small model functions isolate compiler behavior. They establish which transformations LLVM performs, without measuring a complete emitted guest function or a game.

Input IR shapeO2, SLP offO2, SLP onConsequence
Four float lanes calling the opaque FP helperFour calls remainFour calls remainReplace the helper boundary to expose vector work.
Two double lanes calling the opaque FP helperTwo calls remainTwo calls remainThe same obstacle exists for double precision.
Extract, scalar fadd, insert, for four floats or two doublesOne vector faddOne vector faddLLVM already handles this case. A manual rewrite needs a separate benefit.
Four scalar llvm.fma.f32 operationsFour scalar intrinsicsOne llvm.fma.v4f32Emit the vector intrinsic directly where exact FMA is justified and the host supports it.

The unknown helpers can update memory reachable through CPU*. Successive calls can therefore depend on earlier state changes, including sticky FPSR updates. The extract/insert syntax alone is not the obstacle.

Representative llc -O2 checks of four-float vector intrinsics show why each target needs inspection:

OperationBaseline x86-64Zen 4Cortex-A57Required decision
llvm.sqrt.v4f32sqrtpsvsqrtpsfsqrt v0.4sUse existing vector lowering wherever guest result and flags are proven.
llvm.fma.v4f32Four fmaf library calls and spillsvfmadd213psVector fmla plus a register moveRetain the host-FMA capability guard. Avoid introducing library calls on baseline x86.
llvm.fptosi.sat.v4i32.v4f32Four scalar conversions plus fixupsFour scalar conversions plus fixupsfcvtzs v0.4sInvestigate an x86 vector conversion with exact masks. A vector intrinsic alone is insufficient here.

These instruction forms describe lowering only. In particular, an intrinsic's numeric result does not provide Arm's NaN payload rules or FPSR behavior. A conversion sequence with the right integer result still needs correct IOC and IXC handling. Likewise, a vector select does not suppress evaluation of either operand. Sanitize lanes before unsafe shifts or float-to-int operations, avoid LLVM poison, and account for host exceptions if either path can trap.

Implement I15 in this order:

  1. Expose the cost. T2/T3 count helper calls and fast/slow path entries by instruction group. Benchmark all-normal vectors and vectors with exactly one exceptional lane. Verify which guard rejects that lane rather than assuming all scalar paths have the same cause.
  2. Reduce call overhead first. Batch remaining scalar fallback work behind one vector-instruction helper entry. Measure spills and state synchronization as well as call counts. A scalar loop inside that helper is an intermediate step, not completion of SIMD translation.
  3. Remove always-scalar cases. Begin with guarded FABD, then audit rounding and conversions against the target lowering table. Reuse existing vector arithmetic and add only the missing operations and exact fixups.
  4. Keep ordinary lanes in vectors. Replace all-lane rejection where a proven vector mask and fixup can handle the exceptional lanes. Derive NaN, zero, overflow and underflow decisions from the guest pseudocode. Where the proof is incomplete, take the one-call exact fallback. Do not simply evaluate both existing paths and select: the scalar helper path changes FPSR and would still execute its serial calls.
  5. Finish estimates and shifts. I8 uses the guest's exact estimate algorithm, not approximate host reciprocal instructions assumed to match. I9 uses whole-vector masks and safe counts. Prefer generic vector IR when LLVM already lowers it well, then feature-gated intrinsics for demonstrated gaps. Check every supported host family.
  6. Keep the evidence. Add compiler fixtures under tests/llvm_perf/ during implementation, with the opaque-call, scalar arithmetic and scalar-intrinsic controls above. Keep semantic tests separate from lowering checks. Compare helper calls, spills, code size and repeated timings after each instruction group, including mixed-lane inputs.

Rosetta's SIMD-to-SIMD mapping is documented in Dougall Johnson's SSE2 discussion. Its Apple-specific FP hardware is a separate advantage. The lane masks, batched helpers and FPSR reductions above are proposed ScarletNX adaptations, not claims about Rosetta's undocumented implementation. They do not depend on a cooperative Horizon kernel.

I8 Inline FRECPE, FRSQRTE and FRECPX
I9 Inline shifts by register

Floating point

Rosetta 2. x86 and Arm both follow IEEE 754 but differ in NaN propagation and in when tininess is detected. Dougall Johnson writes that the M1 has a non-standard mode, close to the later FEAT_AFP of Armv8.7, that gives the x86 behavior in hardware. x87 is a full but slow software emulation.


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