Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/ruby/test_zjit_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[20, 10]", <<~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:
Expand Down
1 change: 1 addition & 0 deletions zjit/src/backend/arm64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions zjit/src/backend/lir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockId, usize> = HashMap::new();
Expand Down Expand Up @@ -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(&reg_copies, Opnd::Reg(SCRATCH_REG));
let sequentialized = parcopy::sequentialize_register(
&reg_copies,
Opnd::Reg(PARCOPY_SCRATCH_REG),
);
let moves: Vec<Insn> = sequentialized
.iter()
.map(|copy| match copy.source {
Expand Down Expand Up @@ -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<Insn> = sequentialized
.iter()
.map(|copy| match copy.source {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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(&reg_copies, Opnd::Reg(SCRATCH_REG));
let sequentialized = parcopy::sequentialize_register(
&reg_copies,
Opnd::Reg(PARCOPY_SCRATCH_REG),
);

for copy in sequentialized {
new_insns.push(match copy.source {
Expand Down
30 changes: 17 additions & 13 deletions zjit/src/backend/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1912,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]
Expand All @@ -1936,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]
Expand All @@ -1962,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]
Expand Down
Loading