Skip to content

Commit 63a58c7

Browse files
authored
ZJIT: Don't const-fold Array#[] on non-frozen array (ruby#14841)
Accidentally added in ruby#14679
1 parent bb4526b commit 63a58c7

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

zjit/src/hir.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,9 +2630,13 @@ impl Function {
26302630
Insn::ArrayArefFixnum { array, index } if self.type_of(array).ruby_object_known()
26312631
&& self.type_of(index).ruby_object_known() => {
26322632
let array_obj = self.type_of(array).ruby_object().unwrap();
2633-
let index = self.type_of(index).fixnum_value().unwrap();
2634-
let val = unsafe { rb_yarv_ary_entry_internal(array_obj, index) };
2635-
self.new_insn(Insn::Const { val: Const::Value(val) })
2633+
if array_obj.is_frozen() {
2634+
let index = self.type_of(index).fixnum_value().unwrap();
2635+
let val = unsafe { rb_yarv_ary_entry_internal(array_obj, index) };
2636+
self.new_insn(Insn::Const { val: Const::Value(val) })
2637+
} else {
2638+
insn_id
2639+
}
26362640
}
26372641
Insn::Test { val } if self.type_of(val).is_known_falsy() => {
26382642
self.new_insn(Insn::Const { val: Const::CBool(false) })
@@ -11556,6 +11560,37 @@ mod opt_tests {
1155611560
");
1155711561
}
1155811562

11563+
#[test]
11564+
fn test_dont_eliminate_load_from_non_frozen_array() {
11565+
eval(r##"
11566+
S = [4,5,6]
11567+
def test = S[0]
11568+
test
11569+
"##);
11570+
assert_snapshot!(hir_string("test"), @r"
11571+
fn test@<compiled>:3:
11572+
bb0():
11573+
EntryPoint interpreter
11574+
v1:BasicObject = LoadSelf
11575+
Jump bb2(v1)
11576+
bb1(v4:BasicObject):
11577+
EntryPoint JIT(0)
11578+
Jump bb2(v4)
11579+
bb2(v6:BasicObject):
11580+
PatchPoint SingleRactorMode
11581+
PatchPoint StableConstantNames(0x1000, S)
11582+
v24:ArrayExact[VALUE(0x1008)] = Const Value(VALUE(0x1008))
11583+
v12:Fixnum[0] = Const Value(0)
11584+
PatchPoint MethodRedefined(Array@0x1010, []@0x1018, cme:0x1020)
11585+
PatchPoint NoSingletonClass(Array@0x1010)
11586+
v28:BasicObject = ArrayArefFixnum v24, v12
11587+
CheckInterrupts
11588+
Return v28
11589+
");
11590+
// TODO(max): Check the result of `S[0] = 5; test` using `inspect` to make sure that we
11591+
// actually do the load at run-time.
11592+
}
11593+
1155911594
#[test]
1156011595
fn test_eliminate_load_from_frozen_array_in_bounds() {
1156111596
eval(r##"

0 commit comments

Comments
 (0)