Repository navigation
Closed
ruby/ruby
#14083Description
Reproduction
make test-all RUST_BACKTRACE=1 RUN_OPTS='--zjit-call-threshold=1' TESTS='../test/ruby/test_keyword.rb' TESTOPTS='-n /test_many_kwargs/'
thread '<unnamed>' panicked at zjit/src/asm/arm64/mod.rs:939:13:
Expected displacement -296 to be 9 bits or less
Details
[ 23/109] TestKeywordArguments#test_many_kwargsruby: ZJIT has panicked. More info to follow...
thread '<unnamed>' panicked at zjit/src/asm/arm64/mod.rs:939:13:
Expected displacement -296 to be 9 bits or less
stack backtrace:
0: __rustc::rust_begin_unwind
1: core::panicking::panic_fmt
2: zjit::asm::arm64::stur
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/asm/arm64/mod.rs:939:13
3: zjit::backend::arm64::<impl zjit::backend::lir::Assembler>::arm64_emit
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/backend/arm64/mod.rs:1004:36
4: zjit::backend::arm64::<impl zjit::backend::lir::Assembler>::compile_with_regs
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/backend/arm64/mod.rs:1307:26
5: zjit::backend::lir::Assembler::compile
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/backend/lir.rs:1784:19
6: zjit::codegen::gen_function
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/codegen.rs:282:18
7: zjit::codegen::gen_iseq_entry_point_body
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/codegen.rs:122:47
8: zjit::codegen::gen_iseq_entry_point
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/codegen.rs:103:20
9: zjit::codegen::rb_zjit_iseq_gen_entry_point::{{closure}}
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/codegen.rs:87:9
10: std::panicking::try::do_call
at /private/tmp/rust-20250515-8123-mdgm7y/rustc-1.87.0-src/library/std/src/panicking.rs:589:40
11: ___rust_try
12: std::panicking::try
at /private/tmp/rust-20250515-8123-mdgm7y/rustc-1.87.0-src/library/std/src/panicking.rs:552:19
13: std::panic::catch_unwind
at /private/tmp/rust-20250515-8123-mdgm7y/rustc-1.87.0-src/library/std/src/panic.rs:359:14
14: zjit::cruby::with_vm_lock
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/cruby.rs:808:21
15: rb_zjit_iseq_gen_entry_point
at /Users/hung-wulo/src/github.com/Shopify/ruby/zjit/src/codegen.rs:86:20
16: _rb_zjit_compile_iseq
at ../zjit.c:173:41
17: _vm_exec_core
at ../vm.c:448:13
18: _vm_exec_core
19: _rb_vm_exec
at ../vm.c:2621:22
20: _invoke_block_from_c_bh
at ../vm.c:1651:12
21: _invoke_block_from_c_bh
at ../vm.c:1665:20
22: _rb_yield_0
at ../vm.c:1702:12
23: _rb_yield_0
at ../vm.c:1710:12
24: _rb_yield_0
at ../vm_eval.c:1362:12
25: _rb_yield
at ../vm_eval.c:1378:16
26: _rb_ary_collect
at ../array.c:3654:30
27: _vm_call_cfunc_with_frame_
at ../vm_insnhelper.c:3784:11
28: _vm_sendish
29: _vm_exec_core
30: _rb_vm_exec
at ../vm.c:2621:22
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
note: run with `ZJIT_RB_BUG=1` environment variable to display a Ruby backtrace
ZJIT panicked while holding VM lock acquired at zjit/src/codegen.rs:86. Aborting...
make: *** [yes-test-all] Abort trap: 6
Metadata
Metadata
Assignees
Labels
No labels
Activity
st0012 commentedon Jul 18, 2025
(Incorrect AI investigation result is hidden)
Details
ZJIT Keyword Arguments Displacement Issue Investigation
Summary
ZJIT panics when compiling Ruby methods with many keyword arguments due to stack displacement exceeding ARM64 instruction limits.
The Failing Test
test/ruby/test_keyword.rb:4194many_kwargswith 33 keyword arguments (a0-a7, b0-b7, c0-c7, d0-d7, e0)Expected displacement -296 to be 9 bits or lesszjit/src/asm/arm64/mod.rs:939Technical Analysis
1. Stack Layout for Keyword Arguments
For a method with 33 keyword arguments, Ruby allocates:
2. The Displacement Problem
STURinstruction can only handle 9-bit signed displacements: -256 to +255#37requires displacement of -296 bytes3. Why So Much Stack Space?
Methods with many keyword arguments need extra space for:
4. The Code Path
test_many_kwargscallsmany_kwargs(a0: :ok)many_kwargsafter threshold is hitsturfunction in ARM64 backend checks if displacement fits in 9 bitszjit/src/asm/arm64/mod.rs:939Simple Explanation (ELI5)
Imagine you're a robot with arms that can only reach 256 steps in front or behind you. You need to organize 37 boxes on shelves. Box
#37is 296 steps away - too far for your arms to reach! Instead of trying and dropping the box, the robot (ZJIT) stops and says "I can't reach that far!"Potential Solutions
[-]ZJIT: `Expected displacement -296 to be 9 bits or less`[/-][+]ZJIT: Panic: `Expected displacement -296 to be 9 bits or less`[/+]tekknolagi commentedon Jul 21, 2025
Small repro:
tekknolagi commentedon Jul 21, 2025
The AI description is a little weird and potentially misleading. I'm pretty sure it's trying to compile a side exit with a bunch of locals and failing because of the ARM64 displacement being too big. But we should already have something to split large displacements in the ARM backend. We're just not using it for some reason when we should be.
tekknolagi commentedon Jul 21, 2025
Ah.
compile_side_exitshappens post regalloc which I think means no automatic splits.tekknolagi commentedon Jul 21, 2025
Maybe it's possible (for now, until we support more control-flow in the register allocator) to make a new Assembler for each side-exit starting its live_ranges off with the jump instruction's live_ranges and compile it separately out-of-line.
XrXr commentedon Jul 29, 2025
I narrowed this down to the
stores that don't benefit from the split (legalization) pass in compile_side_exits(). There is already some arch specific stuff in this function that's supposed to cross-arch, and to just patch this over would add more arch specific code.So to fix it properly I'm going to have
Insn::Storework for more cases using scratch registers on each arch. This will be cleaner for compile_side_exits() and for other use cases reduce unnecessary register pressure introduced by the splitting -- why go through VRegs when scratch registers will suffice?