Bug #22641
openZJIT: "Function failed validation" on FixnumRShift in a block that type inference found unreachable
Description
Summary¶
In a build with debug assertions (--enable-zjit=dev), ZJIT aborts while compiling a method that compares an argument with != and then divides it by a power of two, once the division has been profiled with both a Fixnum and a Bignum:
Function failed validation.
Err: MismatchedOperandType(InsnId(87), InsnId(86), "<a compile-time constant>", "<unknown>")
The instruction the validator rejects is a FixnumRShift made by the strength reduction of Integer#/ by a power of two (#18258). It sits in a block that can never run, and no instruction in that block has a type, so the shift amount is not seen as a constant.
The interpreter and --zjit-disable-hir-opt both run the program correctly. I have not tried a build without debug assertions; validation only runs under cfg(debug_assertions).
Environment¶
master as of 40abfd3322, configured with --enable-zjit=dev:
Ubuntu 24.04 in Docker on arm64, Rust 1.96.1.
Steps to reproduce¶
# repro.rb
def m2(a0, a4)
a0 != a4
a4 / 2
end
m2(1, 2)
m2(1, 2**62)
m2(1, 2)
m2(1, 2)
m2(1, 2)
m2(1, 2**62)
m2(1, 2)
m2(1, 2)
$ ./miniruby repro.rb; echo $?
0
$ ./miniruby --zjit-call-threshold=1 --zjit-disable-hir-opt repro.rb; echo $?
0
$ ./miniruby --zjit-call-threshold=1 repro.rb
Function failed validation.
Err: MismatchedOperandType(InsnId(87), InsnId(86), "<a compile-time constant>", "<unknown>")
fn m2@repro.rb:2:
...
thread '<unnamed>' panicked at zjit/src/hir.rs:7888:13:
Aborting...
repro.rb:2: [BUG] Aborted at 0x00000000000034cb
The order and number of calls matter. The seven shorter sequences I tried did not fail.
The function that fails validation¶
Trimmed to the part that matters:
bb3(v11:BasicObject, v12:BasicObject, v13:BasicObject):
...
PushInlineFrame :!=, v49 (0xffff98a41c40), num_args=1
...
v90:Fixnum = GuardType v49, Fixnum recompile
v91:Fixnum = GuardType v13, Fixnum recompile
v92:BoolExact = FixnumEq v90, v91
v74:BoolExact = BoolNegate v92
...
PopInlineFrame
v25:Fixnum[2] = Const Value(2)
v26:Any = Snapshot FrameState { ... }
CondBranchHasType v91, Fixnum, bb6(), bb7()
bb6():
v30:Any = Snapshot FrameState { ... }
v31:Fixnum = RefineType v91, Fixnum
PatchPoint NoTracePoint, v30
PatchPoint MethodRedefined(Integer@0xffff98a344c0, /@0x2f, cme:0xffff98a1c438), v30
v84:Fixnum[1] = Const Value(1)
v85:Fixnum = FixnumRShift v31, v84
Jump bb4(v85)
bb7():
CondBranchHasType v91, Bignum, bb8(), bb5()
bb8():
v35 = Snapshot FrameState { ... }
v36 = RefineType v91, Bignum
PatchPoint NoTracePoint, v35
PatchPoint MethodRedefined(Integer@0xffff98a344c0, /@0x2f, cme:0xffff98a1c438), v35
v58 = CCallWithFrame v36, :Integer#/@0xaaaadb863ae4, v25
Jump bb4(v58)
bb5():
PatchPoint NoTracePoint, v26
PatchPoint MethodRedefined(Integer@0xffff98a344c0, /@0x2f, cme:0xffff98a1c438), v26
v61 = GuardType v91, Fixnum recompile
v86 = Const Value(1)
v87 = FixnumRShift v61, v86
Jump bb4(v87)
What I read from it:
a0 != a4is inlined, and the inlinedInteger#==guards thata4is a Fixnum (v91:Fixnum).a4 / 2was profiled with a Fixnum and a Bignum, so it is a three-way branch onv91: Fixnum (bb6), Bignum (bb8), and a fallback that guards Fixnum (bb5).v91is already a Fixnum, so the firstCondBranchHasTypealways goes tobb6.bb7,bb8andbb5are unreachable, and their instructions have no type in the dump.- The division in
bb5was still rewritten toFixnumRShift v61, v86.v86 = Const Value(1)has no type, soself.type_of(right).fixnum_value()in the validator isNoneand it reports the shift amount as not being a compile-time constant.
The same shift in the reachable block bb6 has a typed operand (v84:Fixnum[1]) and passes.
This is as far as I have taken it: I have not traced which pass leaves the block untyped, or whether the fix belongs in the validator (skip blocks type inference did not reach), in the rewrite, or in removing the dead branch before validation.