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
9 changes: 6 additions & 3 deletions test/ruby/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1127,9 +1127,12 @@ def test_copy_stream_socket6
end
t1 = Thread.new { s2.read }
t2 = Thread.new {
ret = IO.copy_stream("megasrc", s1)
assert_equal(megacontent.bytesize, ret)
s1.close
begin
ret = IO.copy_stream("megasrc", s1)
assert_equal(megacontent.bytesize, ret)
ensure
s1.close unless s1.closed?
end
}
result, _ = assert_join_threads([t1, t2])
assert_equal(megacontent, result)
Expand Down
44 changes: 44 additions & 0 deletions test/ruby/test_zjit_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,50 @@ def test_enabled
RUBY
end

def test_rest_parameters_reuse_mutable_caller_splat
assert_runs 'true', <<~RUBY, assert_compiles: true, stats: true
def rest(s, *a) = a
def rest_kw(s, *a, default: 1) = a
def exact(*a) = a
def call_rest(x) = rest(*x, 4)
def call_rest_kw(x) = rest_kw(*x, 4)
def call_exact(x) = exact(*x, 4)

def measure_allocations_and_splat_optimizations(iterations = 10)
before_optimizations = RubyVM::ZJIT.stats[:caller_splat_optimized]
before_allocations = GC.stat(:total_allocated_objects)
iteration = 0
while iteration < iterations
yield
iteration += 1
end
[
GC.stat(:total_allocated_objects) - before_allocations,
RubyVM::ZJIT.stats[:caller_splat_optimized] - before_optimizations,
]
end

x = [1, 2, 3]
40.times do
measure_allocations_and_splat_optimizations(1) { call_rest(x) }
measure_allocations_and_splat_optimizations(1) { call_rest_kw(x) }
measure_allocations_and_splat_optimizations(1) { call_exact(x) }
end

raise unless call_rest(x) == [2, 3, 4]
raise unless call_rest_kw(x) == [2, 3, 4]
raise unless call_exact(x) == [1, 2, 3, 4]
raise unless x == [1, 2, 3]

measurements = [
measure_allocations_and_splat_optimizations { call_rest(x) },
measure_allocations_and_splat_optimizations { call_rest_kw(x) },
measure_allocations_and_splat_optimizations { call_exact(x) },
]
measurements.all? { |allocations, optimizations| allocations == 10 && optimizations > 0 }
RUBY
end

def test_stats_enabled
assert_runs 'false', <<~RUBY, stats: false
RubyVM::ZJIT.stats_enabled?
Expand Down
37 changes: 34 additions & 3 deletions zjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
&Insn::Send { block: Some(BlockHandler::BlockArgProc(_)), .. } => unreachable!("BlockArgProc only appears in SendDirect"),
&Insn::SendForward { cd, blockiseq, state, reason, .. } => gen_send_forward(jit, asm, function, cd, blockiseq, &function.frame_state(state), reason),
Insn::SendDirect(insn) => {
let SendDirectData { cd, cme, iseq, recv, args, kw_bits, jit_entry_idx, block, state, .. } = &**insn;
let SendDirectData { cd, cme, iseq, recv, args, kw_bits, jit_entry_idx, rest_array_behead, block, state, .. } = &**insn;
let block = block.map(|bh| match bh {
BlockHandler::BlockIseq(blockiseq) => lir::BlockHandler::Iseq(blockiseq),
BlockHandler::BlockArgProc(proc_id) => {
Expand All @@ -708,10 +708,15 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
cb, jit, asm,
function, *cd, *cme, *iseq, opnd!(recv), opnds!(args),
*kw_bits, *jit_entry_idx, &function.frame_state(*state), block,
rest_array_behead.map(|(array, count)| (opnd!(array), count)),
)
}
Insn::PushInlineFrame { cme, iseq, recv, num_args, blockiseq, state, .. } => {
no_output!(gen_push_inline_frame(jit, asm, function, *cme, *iseq, opnd!(recv), *num_args, &function.frame_state(*state), *blockiseq))
Insn::PushInlineFrame { cme, iseq, recv, num_args, blockiseq, rest_array_behead, state } => {
no_output!(gen_push_inline_frame(
jit, asm, function, *cme, *iseq, opnd!(recv), *num_args,
&function.frame_state(*state), *blockiseq,
rest_array_behead.map(|(array, count)| (opnd!(array), count)),
))
},
Insn::PopInlineFrame { iseq, argc, state } => {
no_output!(gen_pop_inline_frame(asm, *iseq, *argc, &function.frame_state(*state)))
Expand Down Expand Up @@ -1699,11 +1704,16 @@ fn gen_push_inline_frame(
num_args: u16,
state: &FrameState,
blockiseq: Option<IseqPtr>,
rest_array_behead: Option<(Opnd, usize)>,
) {
let local_size = unsafe { get_iseq_body_local_table_size(iseq) }.to_usize();
let stack_growth = state.stack_size() + local_size + unsafe { get_iseq_body_stack_max(iseq) }.to_usize();
gen_stack_overflow_check(jit, asm, function, state, stack_growth);

if let Some((array, count)) = rest_array_behead {
gen_array_behead(jit, asm, function, array, count, state);
}

// Save cfp->pc and cfp->sp for the caller frame.
// Cannot use gen_prepare_non_leaf_call because we need special SP math.
let stack_size = state.stack().len() - num_args.to_usize() - 1; // -1 for receiver
Expand Down Expand Up @@ -1802,6 +1812,22 @@ fn gen_push_inline_frame(
asm.store(Opnd::mem(64, EC, RUBY_OFFSET_EC_CFP as i32), CFP);
}

/// Remove required/optional arguments from a private caller splat array after the stack-overflow
/// check. A side exit from that check must still be able to replay the original send unchanged.
fn gen_array_behead(
jit: &mut JITState,
asm: &mut Assembler,
function: &Function,
array: Opnd,
count: usize,
state: &FrameState,
) {
debug_assert!(count > 0);
let count: c_long = count.try_into().expect("caller splat prefix exceeds rb_len_t");
gen_prepare_non_leaf_call(jit, asm, function, state);
asm_ccall!(asm, rb_ary_behead, array, count.into());
}

/// Pop the interpreter frame for an inlined callee, restoring the caller's SP and CFP.
fn gen_pop_inline_frame(
asm: &mut Assembler,
Expand Down Expand Up @@ -1837,6 +1863,7 @@ fn gen_send_iseq_direct(
jit_entry_idx: u16,
state: &FrameState,
block: Option<lir::BlockHandler>,
rest_array_behead: Option<(Opnd, usize)>,
) -> lir::Opnd {
gen_incr_counter(asm, Counter::iseq_optimized_send_count);

Expand All @@ -1851,6 +1878,10 @@ fn gen_send_iseq_direct(
let stack_growth = state.stack_size() + local_size + unsafe { get_iseq_body_stack_max(iseq) }.to_usize();
gen_stack_overflow_check(jit, asm, function, state, stack_growth);

if let Some((array, count)) = rest_array_behead {
gen_array_behead(jit, asm, function, array, count, state);
}

// Save cfp->pc and cfp->sp for the caller frame
// Can't use gen_prepare_non_leaf_call because we need special SP math.
let stack_size = state.stack().len() - args.len() - 1; // -1 for receiver
Expand Down
1 change: 1 addition & 0 deletions zjit/src/cruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ unsafe extern "C" {

pub fn rb_hash_empty_p(hash: VALUE) -> VALUE;
pub fn rb_ary_new_from_args(n: c_long, ...) -> VALUE;
pub fn rb_ary_behead(ary: VALUE, n: c_long) -> VALUE;
pub fn rb_str_setbyte(str: VALUE, index: VALUE, value: VALUE) -> VALUE;
pub fn rb_str_getbyte(str: VALUE, index: VALUE) -> VALUE;
pub fn rb_vm_splat_array(flag: VALUE, ary: VALUE) -> VALUE;
Expand Down
Loading
Loading