Project

General

Profile

Actions

Bug #22648

open

ZJIT: [BUG] panic in codegen on FixnumRShift in a branch arm that clean_cfg made unreachable

Bug #22648: ZJIT: [BUG] panic in codegen on FixnumRShift in a branch arm that clean_cfg made unreachable

Added by runlevel5 (Trung Le) about 23 hours ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-11-27) +ZJIT +PRISM [aarch64-linux]
[ruby-core:127741]

Description

Summary

ZJIT panics while compiling a method whose || condition folds to a constant, when the arm that is no longer taken contains a >> by a constant:

dead1.rb:3: [BUG] ZJIT: panicked at zjit/src/codegen.rs:774:71:
called `Option::unwrap()` on a `None` value

This is a release build, so it is not limited to debug assertions. A build with --enable-zjit=dev stops earlier on the same function with Function failed validation and MismatchedOperandType, the message in [Bug #22641], and I found this while checking the fix I proposed there: that fix lets validation pass and the function then fails in codegen.

Environment

master as of 40abfd3322 on arm64 Linux (Ubuntu 24.04 in Docker, Rust 1.96.1), configured with --enable-zjit:

ruby 4.1.0dev (2026-11-27) +ZJIT +PRISM [aarch64-linux]

Steps to reproduce

# dead1.rb
K = -1
def test(a)
  return a if (1 != 1 || K != -1)
  a >> 1
end
test(3)
test(3)
Object.send(:remove_const, :K)
K = 32
test(3)
test(3)
test(3)
test(3)
$ ./miniruby dead1.rb; echo $?
0
$ ./miniruby --zjit-call-threshold=1 dead1.rb
thread '<unnamed>' panicked at zjit/src/codegen.rs:774:71:
called `Option::unwrap()` on a `None` value
dead1.rb:3: [BUG] ZJIT: panicked at zjit/src/codegen.rs:774:71:

--zjit-call-threshold=2 fails in the same way.

The first two calls run a >> 1 so that it is profiled. K is then replaced, which makes the condition constant when test is compiled.

The function that reaches codegen

This is the optimised HIR for test, taken from a unit test that evaluates the same program (addresses are the normalised ones the test helpers print):

bb3(v9:BasicObject, v10:BasicObject):
  v14:Fixnum[1] = Const Value(1)
  v16:Fixnum[1] = Const Value(1)
  PatchPoint MethodRedefined(Integer@0x1008, !=@0x1010, cme:0x1018)
  PushInlineFrame :!=, v14 (0x1040), num_args=1
  PatchPoint MethodRedefined(Integer@0x1008, ==@0x1060, cme:0x1068)
  v129:FalseClass = Const Value(false)
  PatchPoint NoEPEscape(!=)
  PopInlineFrame
  PatchPoint StableConstantNames(0x1090, K)
  v29:Fixnum[32] = Const Value(32)
  PatchPoint NoEPEscape(test)
  v133:TrueClass = Const Value(true)
  v43:CBool[true] = Test v133
  CondBranch v43, bb7(), bb5()
bb7():
  CheckInterrupts
  Return v10
bb5():
  v58 = Const Value(1)
  PatchPoint MethodRedefined(Integer@0x1008, >>@0x1098, cme:0x10a0)
  v76 = GuardType v10, Fixnum recompile
  v77 = FixnumRShift v76, v58
  PatchPoint NoEPEscape(test)
  CheckInterrupts
  Return v77

CondBranch v43 is on a value known to be true but has not been folded, and bb5 has no types. gen_insn takes the shift amount of FixnumRShift from function.type_of(right).fixnum_value().unwrap(), and v58 has the Empty type.

Cause

  1. In the last iteration of the loop in Function::optimize, fold_constants folds both operands of || in one run. The edges into the block that tests the result become a single Jump bbN(v133). The block still takes the value as a parameter, with the type from the last infer_types, so its Test and CondBranch are not folded.
  2. clean_cfg absorbs that block into its predecessor and, because it changed the graph, calls infer_types. The Test is now CBool[true], bb5 is not reached and its instructions are left Empty.
  3. Nothing runs fold_constants after that. No method was inlined in this iteration, so the loop ends and the function goes to codegen with the unreachable arm still in it.

Every other call to infer_types in the loop (type_specialize, inline_methods, canonicalize) is followed by fold_constants in the same iteration. clean_cfg is the one that is not.

Possible fix

Fold again whenever clean_cfg has absorbed a block:

-    fn clean_cfg(&mut self) {
+    fn clean_cfg(&mut self) -> bool {
 ...
         if changed {
             crate::stats::trace_compile_phase("infer_types", || self.infer_types());
         }
+        changed
     }
 ...
             run_pass!(fold_constants);
-            run_pass!(clean_cfg);
+            while run_pass!(clean_cfg) {
+                run_pass!(fold_constants);
+            }

With this on master, a release build runs dead1.rb with the same output as the interpreter, and bb5 is gone from the optimised function. On a ppc64le build with the change, make zjit-test passes with no existing snapshot changed, and make zjit-check and make btest RUN_OPTS=--zjit-call-threshold=1 pass.

I have not measured what the extra fold_constants runs cost in compile time. Another way to close this would be to keep the shift amount in FixnumLShift/FixnumRShift themselves rather than in the type of an operand, which would also make the check in [Bug #22641] unnecessary.

Relation to [Bug #22641]

Both are a shift whose amount is Empty because infer_types no longer reaches its block. In #22641 the block is removed by later passes and only the validator, which runs between them, objects. Here the block is never removed. The validator change proposed in #22641 is still needed for that case in debug builds, but it does not fix this one.

No data to display

Actions

Also available in: PDF Atom