From d7e9a6576cb77acb477607eb94139e1b91288f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Tue, 3 Nov 2026 08:20:08 +0300 Subject: [PATCH 1/2] ZJIT: Preserve spilled values in parallel-copy cycles --- test/ruby/test_zjit_cli.rb | 21 +++++++++++++++++++++ zjit/src/backend/arm64/mod.rs | 1 + zjit/src/backend/lir.rs | 21 ++++++++++++++++----- zjit/src/backend/x86_64/mod.rs | 8 ++++++-- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/test/ruby/test_zjit_cli.rb b/test/ruby/test_zjit_cli.rb index 8b9765e7a2252c..5b156d8caaeadd 100644 --- a/test/ruby/test_zjit_cli.rb +++ b/test/ruby/test_zjit_cli.rb @@ -201,6 +201,27 @@ def test_zjit_max_compile_time_when_disabled RUBY end + def test_spilled_vreg_parallel_copy_cycle + omit "the regression is specific to x86_64" unless RUBY_PLATFORM.include?("x86_64") + + # Shopify/ruby#1032: a stack-to-stack move must not clobber the value + # parked in the parallel-copy scratch register. + assert_compiles "[10, 20]", <<~RUBY, call_threshold: 2 + def test(a, b, n) + i = 0 + while i < n + a, b = b, a + i += 1 + end + [a, b] + end + + test(10, 20, 4) + test(10, 20, 4) + test(10, 20, 5) + RUBY + end + def test_zjit_prelude_kernel_prepend # Simulate what bundler/setup can do: prepend a module to Kernel during # the prelude via the BUNDLER_SETUP mechanism in rubygems.rb: diff --git a/zjit/src/backend/arm64/mod.rs b/zjit/src/backend/arm64/mod.rs index e7ec76f94a7a3b..26529c4d1e0bb5 100644 --- a/zjit/src/backend/arm64/mod.rs +++ b/zjit/src/backend/arm64/mod.rs @@ -192,6 +192,7 @@ const SCRATCH1_OPND: Opnd = Opnd::Reg(X17_REG); /// A scratch register available for use by resolve_ssa to break register copy cycles. /// Must not overlap with ALLOC_REGS or other preserved registers. pub const SCRATCH_REG: Reg = X15_REG; +pub const PARCOPY_SCRATCH_REG: Reg = SCRATCH_REG; const SCRATCH2_OPND: Opnd = Opnd::Reg(X14_REG); impl Assembler { diff --git a/zjit/src/backend/lir.rs b/zjit/src/backend/lir.rs index d7ba3d5333265f..e286fe3b0c3ec8 100644 --- a/zjit/src/backend/lir.rs +++ b/zjit/src/backend/lir.rs @@ -2449,7 +2449,7 @@ impl Assembler /// registers/stack slots, we lower block parameter passing to explicit moves. pub fn resolve_ssa(&mut self, intervals: &[Interval], regs: &RegPool) { use crate::backend::parcopy; - use crate::backend::current::SCRATCH_REG; + use crate::backend::current::PARCOPY_SCRATCH_REG; // Count predecessors for each block let mut num_predecessors: HashMap = HashMap::new(); @@ -2493,7 +2493,10 @@ impl Assembler // parcopy algorithm can detect physical register conflicts. debug_assert!(reg_copies.iter().all(|c| !c.source.is_vreg() && !c.destination.is_vreg()), "parcopy must operate on physical registers, not VRegs"); - let sequentialized = parcopy::sequentialize_register(®_copies, Opnd::Reg(SCRATCH_REG)); + let sequentialized = parcopy::sequentialize_register( + ®_copies, + Opnd::Reg(PARCOPY_SCRATCH_REG), + ); let moves: Vec = sequentialized .iter() .map(|copy| match copy.source { @@ -2573,7 +2576,10 @@ impl Assembler debug_assert!(copies.iter().all(|c| !c.source.is_vreg() && !c.destination.is_vreg()), "parcopy must operate on physical locations, not VRegs"); - let sequentialized = parcopy::sequentialize_register(&copies, Opnd::Reg(SCRATCH_REG)); + let sequentialized = parcopy::sequentialize_register( + &copies, + Opnd::Reg(PARCOPY_SCRATCH_REG), + ); let moves: Vec = sequentialized .iter() .map(|copy| match copy.source { @@ -2636,7 +2642,9 @@ impl Assembler c_arg_regs: &[Reg], ) { use crate::backend::parcopy; - use crate::backend::current::{C_RET_OPND, SCRATCH_REG, NATIVE_STACK_PTR}; + use crate::backend::current::{ + C_RET_OPND, NATIVE_STACK_PTR, PARCOPY_SCRATCH_REG, SCRATCH_REG, + }; for block_id in self.block_order() { let block = &mut self.basic_blocks[block_id.0]; @@ -2794,7 +2802,10 @@ impl Assembler debug_assert!(reg_copies.iter().all(|c| !c.source.is_vreg() && !c.destination.is_vreg()), "parcopy must operate on physical registers, not VRegs"); - let sequentialized = parcopy::sequentialize_register(®_copies, Opnd::Reg(SCRATCH_REG)); + let sequentialized = parcopy::sequentialize_register( + ®_copies, + Opnd::Reg(PARCOPY_SCRATCH_REG), + ); for copy in sequentialized { new_insns.push(match copy.source { diff --git a/zjit/src/backend/x86_64/mod.rs b/zjit/src/backend/x86_64/mod.rs index 54a315d6e3d71b..21163be249343a 100644 --- a/zjit/src/backend/x86_64/mod.rs +++ b/zjit/src/backend/x86_64/mod.rs @@ -107,10 +107,14 @@ pub const ALLOC_REGS: &[Reg] = &[ const SCRATCH0_OPND: Opnd = Opnd::Reg(R11_REG); const SCRATCH1_OPND: Opnd = Opnd::Reg(R10_REG); -/// A scratch register available for use by resolve_ssa to break register copy cycles. -/// Must not overlap with ALLOC_REGS or other preserved registers. +/// A scratch register for CCall lowering. It must not overlap with ALLOC_REGS +/// or other preserved registers. pub const SCRATCH_REG: Reg = R11_REG; +/// A scratch register for parallel-copy cycles. Keep it separate from +/// SCRATCH0_OPND, which x86_scratch_split uses to lower memory-to-memory moves. +pub const PARCOPY_SCRATCH_REG: Reg = R10_REG; + impl Assembler { // This keeps frame growth below the +/-4096-byte displacement range we rely // on for common stack-slot accesses on x86_64. From 3b849e21fc840f0d7a6409f19fc9f9d26150e8a6 Mon Sep 17 00:00:00 2001 From: Efe Date: Thu, 5 Nov 2026 09:08:24 +0300 Subject: [PATCH 2/2] Fix ZJIT parallel-copy regression expectations --- test/ruby/test_zjit_cli.rb | 2 +- zjit/src/backend/x86_64/mod.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/ruby/test_zjit_cli.rb b/test/ruby/test_zjit_cli.rb index 5b156d8caaeadd..f6a71f3b91ca87 100644 --- a/test/ruby/test_zjit_cli.rb +++ b/test/ruby/test_zjit_cli.rb @@ -206,7 +206,7 @@ def test_spilled_vreg_parallel_copy_cycle # Shopify/ruby#1032: a stack-to-stack move must not clobber the value # parked in the parallel-copy scratch register. - assert_compiles "[10, 20]", <<~RUBY, call_threshold: 2 + assert_compiles "[20, 10]", <<~RUBY, call_threshold: 2 def test(a, b, n) i = 0 while i < n diff --git a/zjit/src/backend/x86_64/mod.rs b/zjit/src/backend/x86_64/mod.rs index 21163be249343a..c8f5c386b9e0f3 100644 --- a/zjit/src/backend/x86_64/mod.rs +++ b/zjit/src/backend/x86_64/mod.rs @@ -1916,13 +1916,13 @@ mod tests { asm.compile_with_num_regs(&mut cb, ALLOC_REGS.len()); assert_disasm_snapshot!(cb.disasm(), @" - 0x0: mov r11, rsi + 0x0: mov r10, rsi 0x3: mov rsi, rdi - 0x6: mov rdi, r11 + 0x6: mov rdi, r10 0x9: mov eax, 0 0xe: call rax "); - assert_snapshot!(cb.hexdump(), @"4989f34889fe4c89dfb800000000ffd0"); + assert_snapshot!(cb.hexdump(), @"4989f24889fe4c89d7b800000000ffd0"); } #[test] @@ -1940,16 +1940,16 @@ mod tests { asm.compile_with_num_regs(&mut cb, ALLOC_REGS.len()); assert_disasm_snapshot!(cb.disasm(), @" - 0x0: mov r11, rcx + 0x0: mov r10, rcx 0x3: mov rcx, rdx - 0x6: mov rdx, r11 - 0x9: mov r11, rsi + 0x6: mov rdx, r10 + 0x9: mov r10, rsi 0xc: mov rsi, rdi - 0xf: mov rdi, r11 + 0xf: mov rdi, r10 0x12: mov eax, 0 0x17: call rax "); - assert_snapshot!(cb.hexdump(), @"4989cb4889d14c89da4989f34889fe4c89dfb800000000ffd0"); + assert_snapshot!(cb.hexdump(), @"4989ca4889d14c89d24989f24889fe4c89d7b800000000ffd0"); } #[test] @@ -1966,14 +1966,14 @@ mod tests { asm.compile_with_num_regs(&mut cb, ALLOC_REGS.len()); assert_disasm_snapshot!(cb.disasm(), @" - 0x0: mov r11, rdx + 0x0: mov r10, rdx 0x3: mov rdx, rdi 0x6: mov rdi, rsi - 0x9: mov rsi, r11 + 0x9: mov rsi, r10 0xc: mov eax, 0 0x11: call rax "); - assert_snapshot!(cb.hexdump(), @"4989d34889fa4889f74c89deb800000000ffd0"); + assert_snapshot!(cb.hexdump(), @"4989d24889fa4889f74c89d6b800000000ffd0"); } #[test]