From 45f1cfdaaa9005bbee2b526b965372ef10bc2a85 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:12:00 +0000 Subject: [PATCH] perf(compiler): stage pinned pairs' second elements and selects' false arms in RDX, leaving R8 to a run's locals --- .../stela-compiler/src/x86_64/block/tests.rs | 4 ++-- .../src/x86_64/conditional_select.rs | 24 +++++++++++-------- .../src/x86_64/memory_recipe.rs | 22 ++++++++++++----- .../stela-compiler/src/x86_64/memory_store.rs | 7 +++--- .../stela-compiler/src/x86_64/pinned/span.rs | 12 +++++----- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/crates/stela-compiler/src/x86_64/block/tests.rs b/crates/stela-compiler/src/x86_64/block/tests.rs index fa6e01c208..29907dddb5 100644 --- a/crates/stela-compiler/src/x86_64/block/tests.rs +++ b/crates/stela-compiler/src/x86_64/block/tests.rs @@ -3403,8 +3403,8 @@ fn pinned_conditional_increment_consumes_the_preceding_compare_flags() { // CMP X0, #1; CSINC W8, W2, W8, HI; RET. let csinc = 0x1a88_8448; for (select, prefix) in [ - (cset, &[0x0f, 0x94, 0xc0][..]), // SETE AL - (csinc, &[0x44, 0x8b, 0xc6, 0x45, 0x8d, 0x40, 0x01, 0xf5]), // MOV, LEA, CMC + (cset, &[0x0f, 0x94, 0xc0][..]), // SETE AL + (csinc, &[0x8b, 0xd6, 0x8d, 0x52, 0x01, 0xf5]), // MOV EDX, LEA, CMC ] { let source: Vec<_> = [0xf100_041fu32, select, 0xd65f_03c0] .into_iter() diff --git a/crates/stela-compiler/src/x86_64/conditional_select.rs b/crates/stela-compiler/src/x86_64/conditional_select.rs index b1ac48c5e9..5ec2753f94 100644 --- a/crates/stela-compiler/src/x86_64/conditional_select.rs +++ b/crates/stela-compiler/src/x86_64/conditional_select.rs @@ -63,6 +63,9 @@ pub(super) fn emit_compare(code: &mut Code, operand: &CompareOperand, live_nzcv: pub(super) fn emit(code: &mut Code, operand: &Operand, live_nzcv: bool) { let size = Size::of(operand.width == Width::X64); + // The false arm's scratch: RDX in pinned code, whose predicates leave it + // alone, which leaves R8 to a run's locals (`pinned::Span`). + let spare = if code.is_pinned() { Rdx } else { R8 }; let destination = code.host(operand.destination); let output = destination.unwrap_or(Rax); if operand.left == Register::Zero @@ -90,18 +93,19 @@ pub(super) fn emit(code: &mut Code, operand: &Operand, live_nzcv: bool) { { Some(source) } else { - // Prepare the false arm before the predicate. R8 survives predicate - // emission, and the true arm is a MOV that leaves host flags intact. + // Prepare the false arm before the predicate. The spare survives + // predicate emission, and the true arm is a MOV that leaves host + // flags intact. let mut asm = code.asm(); - select_input(&mut asm, R8, size, operand.right); + select_input(&mut asm, spare, size, operand.right); match operand.otherwise { - Otherwise::Increment if live_nzcv => asm.lea(size, R8, Mem::base(R8, 1)), - Otherwise::Increment => asm.inc(size, R8), - Otherwise::Invert => asm.unary(Unary::Not, size, R8), - Otherwise::Negate => asm.unary(Unary::Neg, size, R8), + Otherwise::Increment if live_nzcv => asm.lea(size, spare, Mem::base(spare, 1)), + Otherwise::Increment => asm.inc(size, spare), + Otherwise::Invert => asm.unary(Unary::Not, size, spare), + Otherwise::Negate => asm.unary(Unary::Neg, size, spare), Otherwise::Unchanged => {} } - Some(Rm::Reg(R8)) + Some(Rm::Reg(spare)) }; // If the unchanged false arm already lives in the result register, // keep it there and conditionally move the true arm instead. @@ -110,8 +114,8 @@ pub(super) fn emit(code: &mut Code, operand: &Operand, live_nzcv: bool) { let (initial, alternate) = if keep_false { let mut asm = code.asm(); let source = asm.guest(operand.left).unwrap_or_else(|| { - asm.mov_imm32(R8, 0); - Rm::Reg(R8) + asm.mov_imm32(spare, 0); + Rm::Reg(spare) }); (operand.destination, Some(source)) } else { diff --git a/crates/stela-compiler/src/x86_64/memory_recipe.rs b/crates/stela-compiler/src/x86_64/memory_recipe.rs index f35c734f6b..6bf7904fb5 100644 --- a/crates/stela-compiler/src/x86_64/memory_recipe.rs +++ b/crates/stela-compiler/src/x86_64/memory_recipe.rs @@ -725,6 +725,14 @@ pub(super) fn pair( address(code, checks, location, bytes, access, recipe.as_deref_mut()); } let (at, displacement) = place.unwrap_or((Gpr::Rax, 0)); + // The second element stages in RDX in pinned code where no pending + // writeback holds it, which leaves R8 to a run's locals + // (`pinned::Span`). + let second_stage = if code.is_pinned() && (place.is_some() || mode == AddressMode::Offset) { + Gpr::Rdx + } else { + FRAME_PAIR_RESULTS[1] + }; match transfer { MemoryTransfer::Store => { let first = match code.host(data[0]) { @@ -750,8 +758,10 @@ pub(super) fn pair( None => { let start = code.checkpoint(); match code.asm().guest(data[1]) { - Some(storage) => code.asm().mov_load(Size::Qword, Gpr::R8, storage), - None => code.asm().alu(Alu::Xor, Size::Dword, Gpr::R8, Gpr::R8), + Some(storage) => code.asm().mov_load(Size::Qword, second_stage, storage), + None => code + .asm() + .alu(Alu::Xor, Size::Dword, second_stage, second_stage), } mark( &mut recipe, @@ -759,11 +769,11 @@ pub(super) fn pair( start, Kind::Stage { element: 1, - host: Gpr::R8, + host: second_stage, }, - mask(Gpr::R8), + mask(second_stage), ); - memory_store::Source::R8 + memory_store::Source::Host(second_stage) } }; for (element, source) in [(0, first), (1, second)] { @@ -786,7 +796,7 @@ pub(super) fn pair( // The second element's own load is the last to need the base. place .and_then(|_| code.host(data[1])) - .unwrap_or(FRAME_PAIR_RESULTS[1]) + .unwrap_or(second_stage) }]; for (element, host, source) in [ (0, hosts[0], Mem::base(at, displacement)), diff --git a/crates/stela-compiler/src/x86_64/memory_store.rs b/crates/stela-compiler/src/x86_64/memory_store.rs index 3e99f23415..c0097470bc 100644 --- a/crates/stela-compiler/src/x86_64/memory_store.rs +++ b/crates/stela-compiler/src/x86_64/memory_store.rs @@ -16,8 +16,8 @@ const VECTOR_BYTES: u8 = 16; #[derive(Clone, Copy)] pub(super) enum Source { Rcx, - R8, - /// A register that holds a guest register ([`super::pinned`]). + /// A register that holds a guest register ([`super::pinned`]), or + /// that a pair's second element stages in. Host(Gpr), Xmm(u8), } @@ -63,7 +63,7 @@ pub(super) fn emit_to(code: &mut Code, bytes: u8, target: Mem, source: Source) { VECTOR_BYTES => asm.movdqu_store(target, xmm), _ => unreachable!("narrow vectors are staged through RCX"), }, - Source::Rcx | Source::R8 | Source::Host(_) => { + Source::Rcx | Source::Host(_) => { debug_assert!(bytes <= QWORD_BYTES); let size = match bytes { 1 => Size::Byte, @@ -72,7 +72,6 @@ pub(super) fn emit_to(code: &mut Code, bytes: u8, target: Mem, source: Source) { _ => Size::Qword, }; let register = match source { - Source::R8 => Gpr::R8, Source::Host(host) => host, _ => Gpr::Rcx, }; diff --git a/crates/stela-compiler/src/x86_64/pinned/span.rs b/crates/stela-compiler/src/x86_64/pinned/span.rs index 2090f73799..2d0d623cdd 100644 --- a/crates/stela-compiler/src/x86_64/pinned/span.rs +++ b/crates/stela-compiler/src/x86_64/pinned/span.rs @@ -125,10 +125,9 @@ fn effects(operation: &Operation) -> Option { operand.destination, ), }, - Operation::ConditionalSelect(operand) => Effects { - takes_r8: true, - ..alu(&[operand.left, operand.right], operand.destination) - }, + Operation::ConditionalSelect(operand) => { + alu(&[operand.left, operand.right], operand.destination) + } Operation::ConditionalCompare(operand) => Effects { takes_r8: true, ..alu( @@ -179,8 +178,9 @@ fn effects(operation: &Operation) -> Option { MemoryTransfer::Store => 0, MemoryTransfer::Load { .. } => bits(&data), }, - // A pair stages its frame-resident elements in RCX and R8. - takes_r8: true, + // A pair stages its frame-resident elements in RCX and RDX, + // or R8 where RDX holds the address's pending writeback. + takes_r8: mode != AddressMode::Offset, memory: true, }, Operation::MemoryRegisterOffset(operand) => Effects {