diff --git a/test/ruby/test_zjit_cli.rb b/test/ruby/test_zjit_cli.rb index 8b9765e7a2252c..2cac2aa1fb079b 100644 --- a/test/ruby/test_zjit_cli.rb +++ b/test/ruby/test_zjit_cli.rb @@ -47,6 +47,28 @@ def test_stats_string_no_zjit RUBY end + def test_array_aset_shared_array + assert_compiles 'true', <<~'RUBY', stats: true + def store(array, index, value) + array[index] = value + end + + # Each slice shares storage with the original until the first write. + 100.times do + original = (0...100).to_a + shared = original[1..-2] + value = Object.new + returned = store(shared, -1, value) + raise unless returned.equal?(value) + raise unless shared[-1].equal?(value) + raise unless original[-2] == 98 + end + + RubyVM::ZJIT.stats(:compiled_iseq_count) > 0 && + RubyVM::ZJIT.stats(:exit_guard_not_shared_failure) == 0 + RUBY + end + def test_stats_quiet # Test that --zjit-stats-quiet collects stats but doesn't print them script = <<~RUBY diff --git a/zjit/bindgen/src/main.rs b/zjit/bindgen/src/main.rs index 58b1eab974b911..9bf41db41af584 100644 --- a/zjit/bindgen/src/main.rs +++ b/zjit/bindgen/src/main.rs @@ -154,6 +154,7 @@ fn main() { .allowlist_function("rb_hash_new_with_bulk_insert") .allowlist_function("rb_hash_stlike_lookup") .allowlist_function("rb_ary_new_capa") + .allowlist_function("rb_ary_modify") .allowlist_function("rb_ary_store") .allowlist_function("rb_ary_resurrect") .allowlist_function("rb_ary_cat") diff --git a/zjit/src/codegen.rs b/zjit/src/codegen.rs index ae1c6b090629dd..7c7d6b51c7190a 100644 --- a/zjit/src/codegen.rs +++ b/zjit/src/codegen.rs @@ -665,8 +665,8 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio Insn::AdjustBounds { index, length } => gen_adjust_bounds(asm, opnd!(index), opnd!(length)), Insn::ArrayAref { array, index, .. } => gen_array_aref(asm, opnd!(array), opnd!(index)), Insn::ArrayArefChecked { array, index, length } => gen_array_aref_checked(jit, asm, opnd!(array), opnd!(index), opnd!(length)), - Insn::ArrayAset { array, index, val } => { - no_output!(gen_array_aset(asm, opnd!(array), opnd!(index), opnd!(val))) + Insn::ArrayAset { array, index, val, state } => { + no_output!(gen_array_aset(jit, asm, opnd!(array), opnd!(index), opnd!(val), &function.frame_state(*state))) } Insn::ArrayPop { array, state } => gen_array_pop(asm, opnd!(array), &function.frame_state(*state)), Insn::ArrayLength { array } => gen_array_length(asm, opnd!(array)), @@ -2432,11 +2432,32 @@ fn gen_array_aref_checked( } fn gen_array_aset( + jit: &mut JITState, asm: &mut Assembler, array: Opnd, index: Opnd, val: Opnd, + state: &FrameState, ) { + let hir_block_id = asm.current_block().hir_block_id; + let rpo_idx = asm.current_block().rpo_index; + let result_block = asm.new_block(hir_block_id, false, rpo_idx); + let result_edge = Target::Block(Box::new(lir::BranchEdge { target: result_block, args: vec![] })); + + let array_value = asm.load_mem(array); + let flags = asm.load(Opnd::mem(VALUE_BITS, array_value, RUBY_OFFSET_RBASIC_FLAGS)); + asm.test(flags, Opnd::UImm(RUBY_ELTS_SHARED as u64)); + asm.jz(jit, result_edge.clone()); + + // Detach copy-on-write arrays before writing through their element pointer. + gen_prepare_leaf_call_with_gc(asm, state); + asm_ccall!(asm, rb_ary_modify, array_value); + asm.jmp(result_edge); + + asm.set_current_block(result_block); + let label = jit.get_label(asm, result_block, hir_block_id); + asm.write_label(label); + let unboxed_idx = asm.load_mem(index); let array = asm.load_mem(array); let array_ptr = gen_array_ptr(asm, array); diff --git a/zjit/src/cruby_bindings.inc.rs b/zjit/src/cruby_bindings.inc.rs index 2ce31249af2e79..7372220b7d350d 100644 --- a/zjit/src/cruby_bindings.inc.rs +++ b/zjit/src/cruby_bindings.inc.rs @@ -2252,6 +2252,7 @@ unsafe extern "C" { pub fn rb_define_class(name: *const ::std::os::raw::c_char, super_: VALUE) -> VALUE; pub fn rb_obj_class(obj: VALUE) -> VALUE; pub fn rb_ary_new_capa(capa: rb_len_t) -> VALUE; + pub fn rb_ary_modify(ary: VALUE); pub fn rb_ary_store(ary: VALUE, key: rb_len_t, val: VALUE); pub fn rb_ary_dup(ary: VALUE) -> VALUE; pub fn rb_ary_resurrect(ary: VALUE) -> VALUE; diff --git a/zjit/src/cruby_methods.rs b/zjit/src/cruby_methods.rs index 0eff229ea26129..fdf7f10ecc8c17 100644 --- a/zjit/src/cruby_methods.rs +++ b/zjit/src/cruby_methods.rs @@ -462,7 +462,6 @@ fn inline_array_aset(fun: &mut hir::Function, block: hir::BlockId, recv: hir::In let recv = fun.coerce_to(block, recv, types::ArrayExact, state); let index = fun.coerce_to(block, index, types::Fixnum, state); fun.guard_not_frozen(block, recv, state); - fun.guard_not_shared(block, recv, state); // Bounds check: unbox Fixnum index and guard 0 <= idx < length. let index = fun.push_insn(block, hir::Insn::UnboxFixnum { val: index }); @@ -473,7 +472,7 @@ fn inline_array_aset(fun: &mut hir::Function, block: hir::BlockId, recv: hir::In use crate::hir::SideExitReason; let index = fun.push_insn(block, hir::Insn::GuardGreaterEq { left: index, right: zero, reason: Box::new(SideExitReason::GuardGreaterEq), state }); - let _ = fun.push_insn(block, hir::Insn::ArrayAset { array: recv, index, val }); + let _ = fun.push_insn(block, hir::Insn::ArrayAset { array: recv, index, val, state }); fun.push_insn(block, hir::Insn::WriteBarrier { recv, val }); return Some(val); } diff --git a/zjit/src/hir.rs b/zjit/src/hir.rs index d82c938032b06b..aaa2ff7cc9c787 100644 --- a/zjit/src/hir.rs +++ b/zjit/src/hir.rs @@ -1060,7 +1060,7 @@ pub enum Insn { /// adjusted to index 2, so any negative `index` is definitely considered out-of-bounds). /// `index` but may be out-of-bounds, in which case it returns `nil`. ArrayArefChecked { array: InsnId, index: InsnId, length: InsnId }, - ArrayAset { array: InsnId, index: InsnId, val: InsnId }, + ArrayAset { array: InsnId, index: InsnId, val: InsnId, state: InsnId }, ArrayPop { array: InsnId, state: InsnId }, /// Return the length of the array as a C `long` ([`types::CInt64`]) ArrayLength { array: InsnId }, @@ -1587,10 +1587,11 @@ macro_rules! for_each_operand_impl { $visit_one!(*index); $visit_one!(*length); } - Insn::ArrayAset { array, index, val } => { + Insn::ArrayAset { array, index, val, state } => { $visit_one!(*array); $visit_one!(*index); $visit_one!(*val); + $visit_one!(*state); } Insn::ArrayPop { array, state } => { $visit_one!(*array); diff --git a/zjit/src/hir/opt_tests.rs b/zjit/src/hir/opt_tests.rs index 8c9edcec431b8e..5e4844c1f887b8 100644 --- a/zjit/src/hir/opt_tests.rs +++ b/zjit/src/hir/opt_tests.rs @@ -14663,11 +14663,10 @@ mod hir_opt_tests { v35:ArrayExact = GuardType v10, ArrayExact recompile v36:CUInt64 = LoadField v35, :RBASIC_FLAGS@0x1040 v37:CUInt64 = GuardNoBitsSet v36, RUBY_FL_FREEZE=CUInt64(2048) - v39:CUInt64 = GuardNoBitsSet v37, RUBY_ELTS_SHARED=CUInt64(4096) - v48:CInt64[1] = Const CInt64(1) - v41:CInt64 = ArrayLength v35 - v42:CInt64[1] = GuardLess v48, v41 - ArrayAset v35, v42, v19 + v46:CInt64[1] = Const CInt64(1) + v39:CInt64 = ArrayLength v35 + v40:CInt64[1] = GuardLess v46, v39 + ArrayAset v35, v40, v19 PatchPoint NoEPEscape(test) CheckInterrupts Return v19 @@ -14706,14 +14705,13 @@ mod hir_opt_tests { v40:Fixnum = GuardType v15, Fixnum v41:CUInt64 = LoadField v39, :RBASIC_FLAGS@0x1040 v42:CUInt64 = GuardNoBitsSet v41, RUBY_FL_FREEZE=CUInt64(2048) - v44:CUInt64 = GuardNoBitsSet v42, RUBY_ELTS_SHARED=CUInt64(4096) - v45:CInt64 = UnboxFixnum v40 - v46:CInt64 = ArrayLength v39 - v47:CInt64 = GuardLess v45, v46 - v48:CInt64 = AdjustBounds v47, v46 - v49:CInt64[0] = Const CInt64(0) - v50:CInt64 = GuardGreaterEq v48, v49 - ArrayAset v39, v50, v16 + v43:CInt64 = UnboxFixnum v40 + v44:CInt64 = ArrayLength v39 + v45:CInt64 = GuardLess v43, v44 + v46:CInt64 = AdjustBounds v45, v44 + v47:CInt64[0] = Const CInt64(0) + v48:CInt64 = GuardGreaterEq v46, v47 + ArrayAset v39, v48, v16 WriteBarrier v39, v16 PatchPoint NoEPEscape(test) CheckInterrupts