From: "runlevel5 (Trung Le) via ruby-core" Date: 2026-11-26T14:33:55+00:00 Subject: [ruby-core:127730] [Ruby Bug#22641] ZJIT: "Function failed validation" on FixnumRShift in a block that type inference found unreachable Issue #22641 has been updated by runlevel5 (Trung Le). ## 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), "", "") ``` 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 became unreachable after the rewrite. `infer_types` leaves the instructions of such a block with the `Empty` type, so the shift amount is no longer seen as a constant, and validation runs before the block is removed. 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`: ``` ruby 4.1.0dev (2026-11-26) +ZJIT dev +PRISM [aarch64-linux] ``` Ubuntu 24.04 in Docker on arm64, Rust 1.96.1. ## Steps to reproduce ```ruby # 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), "", "") fn m2@repro.rb:2: ... thread '' 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: 1. `a0 != a4` is inlined, and the inlined `Integer#==` guards that `a4` is a Fixnum (`v91:Fixnum`). 2. `a4 / 2` was profiled with a Fixnum and a Bignum, so it is a three-way branch on `v91`: Fixnum (`bb6`), Bignum (`bb8`), and a fallback that guards Fixnum (`bb5`). 3. `v91` is already a Fixnum, so the first `CondBranchHasType` always goes to `bb6`. `bb7`, `bb8` and `bb5` are unreachable, and their instructions have no type in the dump. 4. The division in `bb5` was still rewritten to `FixnumRShift v61, v86`. `v86 = Const Value(1)` has no type, so `self.type_of(right).fixnum_value()` in the validator is `None` and 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. ## Cause The function is valid after every pass until `canonicalize` in the second iteration of the optimisation loop: 1. Iteration 1, `fold_constants`: both `a4 / 2` sites, in `bb6` and in `bb5`, are rewritten to `FixnumRShift`. Both blocks are reachable at this point and both shift amounts are typed `Fixnum[1]`. 2. Iteration 2, `type_specialize`: the `==` inside the inlined `!=` becomes `FixnumEq`, which adds `v91:Fixnum = GuardType v13, Fixnum`. 3. Iteration 2, `canonicalize`: the guard's result is forwarded to the later uses of `v13`, so the branch becomes `CondBranchHasType v91, Fixnum, bb6(), bb7()`. `canonicalize` then calls `infer_types`. 4. `infer_types` resets every type to `Empty` and types only the blocks it can reach. `v91` is a Fixnum, so `bb7`, `bb8` and `bb5` are not reached and their instructions stay `Empty`. 5. Validation runs right after `canonicalize`, before `fold_constants` turns the branch into a `Jump` and `clean_cfg` removes the dead blocks. The `FixnumRShift` left in `bb5` by step 1 now has an `Empty` shift amount. The other checks in `validate_insn_type` are subtype checks, which `Empty` passes. The shift check is the one that needs a value, so it is the one that fails on an unreachable block. I confirmed the pass by printing the pass name before each `assert_validates()`: the last one before the abort is `canonicalize`, on its second run for `m2`. ## Possible fix Accept a shift amount that type inference did not reach, in the `FixnumLShift`/`FixnumRShift` arm of `validate_insn_type`: ```diff self.assert_subtype(insn_id, left, types::Fixnum)?; self.assert_subtype(insn_id, right, types::Fixnum)?; + if self.type_of(right).is_subtype(types::Empty) { + return Ok(()); + } let Some(obj) = self.type_of(right).fixnum_value() else { ``` With this on master, `repro.rb` and the 130-line program it was reduced from both run under `--zjit-call-threshold=1` with the same output as the interpreter. I have not run `make zjit-check` with it or written a test. ---------------------------------------- Bug #22641: ZJIT: "Function failed validation" on FixnumRShift in a block that type inference found unreachable https://bugs.ruby-lang.org/issues/22641#change-120273 * Author: runlevel5 (Trung Le) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-26) +ZJIT dev +PRISM [aarch64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- ## 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), "", "") ``` 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`: ``` ruby 4.1.0dev (2026-11-26) +ZJIT dev +PRISM [aarch64-linux] ``` Ubuntu 24.04 in Docker on arm64, Rust 1.96.1. ## Steps to reproduce ```ruby # 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), "", "") fn m2@repro.rb:2: ... thread '' 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: 1. `a0 != a4` is inlined, and the inlined `Integer#==` guards that `a4` is a Fixnum (`v91:Fixnum`). 2. `a4 / 2` was profiled with a Fixnum and a Bignum, so it is a three-way branch on `v91`: Fixnum (`bb6`), Bignum (`bb8`), and a fallback that guards Fixnum (`bb5`). 3. `v91` is already a Fixnum, so the first `CondBranchHasType` always goes to `bb6`. `bb7`, `bb8` and `bb5` are unreachable, and their instructions have no type in the dump. 4. The division in `bb5` was still rewritten to `FixnumRShift v61, v86`. `v86 = Const Value(1)` has no type, so `self.type_of(right).fixnum_value()` in the validator is `None` and 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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/