Skip to content

Commit f3d76ad

Browse files
committed
ZJIT: Check arg limit before pushing SendWithoutBLockDirect insn
This reduces some processing and makes the HIR more accurate.
1 parent d81a11d commit f3d76ad

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

zjit/src/codegen.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,6 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
401401
&Insn::Send { cd, blockiseq, state, reason, .. } => gen_send(jit, asm, cd, blockiseq, &function.frame_state(state), reason),
402402
&Insn::SendForward { cd, blockiseq, state, reason, .. } => gen_send_forward(jit, asm, cd, blockiseq, &function.frame_state(state), reason),
403403
&Insn::SendWithoutBlock { cd, state, reason, .. } => gen_send_without_block(jit, asm, cd, &function.frame_state(state), reason),
404-
// Give up SendWithoutBlockDirect for 6+ args since asm.ccall() doesn't support it.
405-
Insn::SendWithoutBlockDirect { cd, state, args, .. } if args.len() + 1 > C_ARG_OPNDS.len() => // +1 for self
406-
gen_send_without_block(jit, asm, *cd, &function.frame_state(*state), SendFallbackReason::TooManyArgsForLir),
407404
Insn::SendWithoutBlockDirect { cme, iseq, recv, args, state, .. } => gen_send_without_block_direct(cb, jit, asm, *cme, *iseq, opnd!(recv), opnds!(args), &function.frame_state(*state)),
408405
&Insn::InvokeSuper { cd, blockiseq, state, reason, .. } => gen_invokesuper(jit, asm, cd, blockiseq, &function.frame_state(state), reason),
409406
&Insn::InvokeBlock { cd, state, reason, .. } => gen_invokeblock(jit, asm, cd, &function.frame_state(state), reason),

zjit/src/hir.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,12 @@ fn can_direct_send(function: &mut Function, block: BlockId, iseq: *const rb_iseq
16591659
return false;
16601660
}
16611661

1662+
// asm.ccall() doesn't support 6+ args
1663+
if args.len() + 1 > C_ARG_OPNDS.len() { // +1 for self
1664+
function.set_dynamic_send_reason(send_insn, TooManyArgsForLir);
1665+
return false;
1666+
}
1667+
16621668
// Because we exclude e.g. post parameters above, they are also excluded from the sum below.
16631669
let lead_num = params.lead_num;
16641670
let opt_num = params.opt_num;
@@ -2703,16 +2709,9 @@ impl Function {
27032709
let (send_state, processed_args) = if !kwarg.is_null() {
27042710
match self.reorder_keyword_arguments(&args, kwarg, iseq) {
27052711
Ok(reordered) => {
2706-
// Only use reordered state if args fit in C registers.
2707-
// Fallback to interpreter needs original order for kwarg handling.
2708-
// NOTE: This needs to match with the condition in codegen.rs.
2709-
if reordered.len() + 1 <= C_ARG_OPNDS.len() {
2710-
let new_state = self.frame_state(state).with_reordered_args(&reordered);
2711-
let snapshot = self.push_insn(block, Insn::Snapshot { state: new_state });
2712-
(snapshot, reordered)
2713-
} else {
2714-
(state, reordered)
2715-
}
2712+
let new_state = self.frame_state(state).with_reordered_args(&reordered);
2713+
let snapshot = self.push_insn(block, Insn::Snapshot { state: new_state });
2714+
(snapshot, reordered)
27162715
}
27172716
Err(reason) => {
27182717
self.set_dynamic_send_reason(insn_id, reason);
@@ -2763,16 +2762,9 @@ impl Function {
27632762
let (send_state, processed_args) = if !kwarg.is_null() {
27642763
match self.reorder_keyword_arguments(&args, kwarg, iseq) {
27652764
Ok(reordered) => {
2766-
// Only use reordered state if args fit in C registers.
2767-
// Fallback to interpreter needs original order for kwarg handling.
2768-
// NOTE: This needs to match with the condition in codegen.rs.
2769-
if reordered.len() + 1 <= C_ARG_OPNDS.len() {
2770-
let new_state = self.frame_state(state).with_reordered_args(&reordered);
2771-
let snapshot = self.push_insn(block, Insn::Snapshot { state: new_state });
2772-
(snapshot, reordered)
2773-
} else {
2774-
(state, reordered)
2775-
}
2765+
let new_state = self.frame_state(state).with_reordered_args(&reordered);
2766+
let snapshot = self.push_insn(block, Insn::Snapshot { state: new_state });
2767+
(snapshot, reordered)
27762768
}
27772769
Err(reason) => {
27782770
self.set_dynamic_send_reason(insn_id, reason);

zjit/src/hir/tests.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,11 @@ mod snapshot_tests {
152152
v23:Fixnum[7] = Const Value(7)
153153
v25:Fixnum[8] = Const Value(8)
154154
v26:Any = Snapshot FrameState { pc: 0x1008, stack: [v6, v11, v13, v15, v17, v19, v21, v23, v25], locals: [] }
155-
PatchPoint MethodRedefined(Object@0x1010, foo@0x1018, cme:0x1020)
156-
PatchPoint NoSingletonClass(Object@0x1010)
157-
v34:HeapObject[class_exact*:Object@VALUE(0x1010)] = GuardType v6, HeapObject[class_exact*:Object@VALUE(0x1010)]
158-
v35:BasicObject = SendWithoutBlockDirect v34, :foo (0x1048), v11, v13, v19, v21, v17, v15, v23, v25
159-
v28:Any = Snapshot FrameState { pc: 0x1050, stack: [v35], locals: [] }
155+
v27:BasicObject = SendWithoutBlock v6, :foo, v11, v13, v15, v17, v19, v21, v23, v25 # SendFallbackReason: Too many arguments for LIR
156+
v28:Any = Snapshot FrameState { pc: 0x1010, stack: [v27], locals: [] }
160157
PatchPoint NoTracePoint
161158
CheckInterrupts
162-
Return v35
159+
Return v27
163160
");
164161
}
165162
}

0 commit comments

Comments
 (0)