Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test/ruby/test_zjit_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions zjit/bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 23 additions & 2 deletions zjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions zjit/src/cruby_bindings.inc.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions zjit/src/cruby_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions zjit/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 11 additions & 13 deletions zjit/src/hir/opt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading