Skip to content

Commit 6cebbf4

Browse files
committed
ZJIT: Split CSel memory reads on x86_64
Fix Shopify#876
1 parent f52edf1 commit 6cebbf4

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

zjit/src/backend/x86_64/mod.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,23 @@ impl Assembler {
542542
*opnds = vec![];
543543
asm.push_insn(insn);
544544
}
545-
Insn::CSelZ { out, .. } |
546-
Insn::CSelNZ { out, .. } |
547-
Insn::CSelE { out, .. } |
548-
Insn::CSelNE { out, .. } |
549-
Insn::CSelL { out, .. } |
550-
Insn::CSelLE { out, .. } |
551-
Insn::CSelG { out, .. } |
552-
Insn::CSelGE { out, .. } |
545+
Insn::CSelZ { truthy: left, falsy: right, out } |
546+
Insn::CSelNZ { truthy: left, falsy: right, out } |
547+
Insn::CSelE { truthy: left, falsy: right, out } |
548+
Insn::CSelNE { truthy: left, falsy: right, out } |
549+
Insn::CSelL { truthy: left, falsy: right, out } |
550+
Insn::CSelLE { truthy: left, falsy: right, out } |
551+
Insn::CSelG { truthy: left, falsy: right, out } |
552+
Insn::CSelGE { truthy: left, falsy: right, out } => {
553+
*left = split_stack_membase(asm, *left, SCRATCH1_OPND, &stack_state);
554+
*right = split_stack_membase(asm, *right, SCRATCH0_OPND, &stack_state);
555+
*right = split_if_both_memory(asm, *right, *left, SCRATCH0_OPND);
556+
let mem_out = split_memory_write(out, SCRATCH0_OPND);
557+
asm.push_insn(insn);
558+
if let Some(mem_out) = mem_out {
559+
asm.store(mem_out, SCRATCH0_OPND);
560+
}
561+
}
553562
Insn::Lea { out, .. } => {
554563
let mem_out = split_memory_write(out, SCRATCH0_OPND);
555564
asm.push_insn(insn);
@@ -1776,4 +1785,23 @@ mod tests {
17761785
");
17771786
assert_snapshot!(cb.hexdump(), @"49bb00100000000000004c891b");
17781787
}
1788+
1789+
#[test]
1790+
fn test_csel_split_memory_read() {
1791+
let (mut asm, mut cb) = setup_asm();
1792+
1793+
let left = Opnd::Mem(Mem { base: MemBase::Stack { stack_idx: 0, num_bits: 64 }, disp: 0, num_bits: 64 });
1794+
let right = Opnd::Mem(Mem { base: MemBase::Stack { stack_idx: 1, num_bits: 64 }, disp: 2, num_bits: 64 });
1795+
let _ = asm.csel_e(left, right);
1796+
asm.compile_with_num_regs(&mut cb, 0);
1797+
1798+
assert_disasm_snapshot!(cb.disasm(), @"
1799+
0x0: mov r10, qword ptr [rbp - 8]
1800+
0x4: mov r11, qword ptr [rbp - 0x10]
1801+
0x8: mov r11, qword ptr [r11 + 2]
1802+
0xc: cmove r11, qword ptr [r10]
1803+
0x10: mov qword ptr [rbp - 8], r11
1804+
");
1805+
assert_snapshot!(cb.hexdump(), @"4c8b55f84c8b5df04d8b5b024d0f441a4c895df8");
1806+
}
17791807
}

0 commit comments

Comments
 (0)