Skip to content

Commit 7f96646

Browse files
committed
ZJIT: Update IsBlockGiven to use GetLEP
1 parent 92193fc commit 7f96646

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

zjit/src/codegen.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
503503
&Insn::LoadField { recv, id, offset, return_type } => gen_load_field(asm, opnd!(recv), id, offset, return_type),
504504
&Insn::StoreField { recv, id, offset, val } => no_output!(gen_store_field(asm, opnd!(recv), id, offset, opnd!(val), function.type_of(val))),
505505
&Insn::WriteBarrier { recv, val } => no_output!(gen_write_barrier(asm, opnd!(recv), opnd!(val), function.type_of(val))),
506-
&Insn::IsBlockGiven => gen_is_block_given(jit, asm),
506+
&Insn::IsBlockGiven { lep } => gen_is_block_given(asm, opnd!(lep)),
507507
Insn::ArrayInclude { elements, target, state } => gen_array_include(jit, asm, opnds!(elements), opnd!(target), &function.frame_state(*state)),
508508
Insn::ArrayPackBuffer { elements, fmt, buffer, state } => gen_array_pack_buffer(jit, asm, opnds!(elements), opnd!(fmt), opnd!(buffer), &function.frame_state(*state)),
509509
&Insn::DupArrayInclude { ary, target, state } => gen_dup_array_include(jit, asm, ary, opnd!(target), &function.frame_state(state)),
@@ -609,16 +609,10 @@ fn gen_defined(jit: &JITState, asm: &mut Assembler, op_type: usize, obj: VALUE,
609609
}
610610

611611
/// Similar to gen_defined for DEFINED_YIELD
612-
fn gen_is_block_given(jit: &JITState, asm: &mut Assembler) -> Opnd {
613-
let local_iseq = unsafe { rb_get_iseq_body_local_iseq(jit.iseq) };
614-
if unsafe { rb_get_iseq_body_type(local_iseq) } == ISEQ_TYPE_METHOD {
615-
let lep = gen_get_lep(jit, asm);
616-
let block_handler = asm.load(Opnd::mem(64, lep, SIZEOF_VALUE_I32 * VM_ENV_DATA_INDEX_SPECVAL));
617-
asm.cmp(block_handler, VM_BLOCK_HANDLER_NONE.into());
618-
asm.csel_e(Qfalse.into(), Qtrue.into())
619-
} else {
620-
Qfalse.into()
621-
}
612+
fn gen_is_block_given(asm: &mut Assembler, lep: Opnd) -> Opnd {
613+
let block_handler = asm.load(Opnd::mem(64, lep, SIZEOF_VALUE_I32 * VM_ENV_DATA_INDEX_SPECVAL));
614+
asm.cmp(block_handler, VM_BLOCK_HANDLER_NONE.into());
615+
asm.csel_e(Qfalse.into(), Qtrue.into())
622616
}
623617

624618
fn gen_unbox_fixnum(asm: &mut Assembler, val: Opnd) -> Opnd {

zjit/src/cruby_methods.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,14 @@ fn inline_kernel_itself(_fun: &mut hir::Function, _block: hir::BlockId, recv: hi
318318

319319
fn inline_kernel_block_given_p(fun: &mut hir::Function, block: hir::BlockId, _recv: hir::InsnId, args: &[hir::InsnId], _state: hir::InsnId) -> Option<hir::InsnId> {
320320
let &[] = args else { return None; };
321-
// TODO(max): In local iseq types that are not ISEQ_TYPE_METHOD, rewrite to Constant false.
322-
Some(fun.push_insn(block, hir::Insn::IsBlockGiven))
321+
322+
let local_iseq = unsafe { rb_get_iseq_body_local_iseq(fun.iseq()) };
323+
if unsafe { rb_get_iseq_body_type(local_iseq) } == ISEQ_TYPE_METHOD {
324+
let lep = fun.push_insn(block, hir::Insn::GetLEP);
325+
Some(fun.push_insn(block, hir::Insn::IsBlockGiven { lep }))
326+
} else {
327+
Some(fun.push_insn(block, hir::Insn::Const { val: hir::Const::Value(Qfalse) }))
328+
}
323329
}
324330

325331
fn inline_array_aref(fun: &mut hir::Function, block: hir::BlockId, recv: hir::InsnId, args: &[hir::InsnId], state: hir::InsnId) -> Option<hir::InsnId> {

zjit/src/hir.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ pub enum Insn {
802802
GetConstantPath { ic: *const iseq_inline_constant_cache, state: InsnId },
803803
/// Kernel#block_given? but without pushing a frame. Similar to [`Insn::Defined`] with
804804
/// `DEFINED_YIELD`
805-
IsBlockGiven,
805+
IsBlockGiven { lep: InsnId },
806806
/// Test the bit at index of val, a Fixnum.
807807
/// Return Qtrue if the bit is set, else Qfalse.
808808
FixnumBitCheck { val: InsnId, index: u8 },
@@ -1519,7 +1519,7 @@ impl<'a> std::fmt::Display for InsnPrinter<'a> {
15191519
Insn::GetBlockHandler { lep } => write!(f, "GetBlockHandler {lep}"),
15201520
Insn::PatchPoint { invariant, .. } => { write!(f, "PatchPoint {}", invariant.print(self.ptr_map)) },
15211521
Insn::GetConstantPath { ic, .. } => { write!(f, "GetConstantPath {:p}", self.ptr_map.map_ptr(ic)) },
1522-
Insn::IsBlockGiven => { write!(f, "IsBlockGiven") },
1522+
Insn::IsBlockGiven { lep } => { write!(f, "IsBlockGiven {lep}") },
15231523
Insn::FixnumBitCheck {val, index} => { write!(f, "FixnumBitCheck {val}, {index}") },
15241524
Insn::CCall { cfunc, recv, args, name, return_type: _, elidable: _ } => {
15251525
write!(f, "CCall {recv}, :{}@{:p}", name.contents_lossy(), self.ptr_map.map_ptr(cfunc))?;
@@ -1975,6 +1975,10 @@ impl Function {
19751975
}
19761976
}
19771977

1978+
pub fn iseq(&self) -> *const rb_iseq_t {
1979+
self.iseq
1980+
}
1981+
19781982
// Add an instruction to the function without adding it to any block
19791983
fn new_insn(&mut self, insn: Insn) -> InsnId {
19801984
let id = InsnId(self.insns.len());
@@ -2125,7 +2129,6 @@ impl Function {
21252129
result@(Const {..}
21262130
| Param
21272131
| GetConstantPath {..}
2128-
| IsBlockGiven
21292132
| PatchPoint {..}
21302133
| PutSpecialObject {..}
21312134
| GetGlobal {..}
@@ -2182,6 +2185,7 @@ impl Function {
21822185
&GuardLess { left, right, state } => GuardLess { left: find!(left), right: find!(right), state },
21832186
&GuardSuperMethodEntry { lep, cme, state } => GuardSuperMethodEntry { lep: find!(lep), cme, state },
21842187
&GetBlockHandler { lep } => GetBlockHandler { lep: find!(lep) },
2188+
&IsBlockGiven { lep } => IsBlockGiven { lep: find!(lep) },
21852189
&FixnumAdd { left, right, state } => FixnumAdd { left: find!(left), right: find!(right), state },
21862190
&FixnumSub { left, right, state } => FixnumSub { left: find!(left), right: find!(right), state },
21872191
&FixnumMult { left, right, state } => FixnumMult { left: find!(left), right: find!(right), state },
@@ -2453,7 +2457,7 @@ impl Function {
24532457
Insn::Defined { pushval, .. } => Type::from_value(*pushval).union(types::NilClass),
24542458
Insn::DefinedIvar { pushval, .. } => Type::from_value(*pushval).union(types::NilClass),
24552459
Insn::GetConstantPath { .. } => types::BasicObject,
2456-
Insn::IsBlockGiven => types::BoolExact,
2460+
Insn::IsBlockGiven { .. } => types::BoolExact,
24572461
Insn::FixnumBitCheck { .. } => types::BoolExact,
24582462
Insn::ArrayMax { .. } => types::BasicObject,
24592463
Insn::ArrayInclude { .. } => types::BoolExact,
@@ -4374,11 +4378,11 @@ impl Function {
43744378
| &Insn::LoadSelf
43754379
| &Insn::GetLocal { .. }
43764380
| &Insn::PutSpecialObject { .. }
4377-
| &Insn::IsBlockGiven
43784381
| &Insn::IncrCounter(_)
43794382
| &Insn::IncrCounterPtr { .. } =>
43804383
{}
4381-
&Insn::GetBlockHandler { lep } => {
4384+
&Insn::GetBlockHandler { lep }
4385+
| &Insn::IsBlockGiven { lep } => {
43824386
worklist.push_back(lep);
43834387
}
43844388
&Insn::PatchPoint { state, .. }
@@ -5114,7 +5118,7 @@ impl Function {
51145118
| Insn::PutSpecialObject { .. }
51155119
| Insn::LoadField { .. }
51165120
| Insn::GetConstantPath { .. }
5117-
| Insn::IsBlockGiven
5121+
| Insn::IsBlockGiven { .. }
51185122
| Insn::GetGlobal { .. }
51195123
| Insn::LoadPC
51205124
| Insn::LoadEC

zjit/src/hir/opt_tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,10 +2653,11 @@ mod hir_opt_tests {
26532653
PatchPoint NoSingletonClass(Object@0x1000)
26542654
PatchPoint MethodRedefined(Object@0x1000, block_given?@0x1008, cme:0x1010)
26552655
v19:HeapObject[class_exact*:Object@VALUE(0x1000)] = GuardType v6, HeapObject[class_exact*:Object@VALUE(0x1000)]
2656-
v20:BoolExact = IsBlockGiven
2656+
v20:CPtr = GetLEP
2657+
v21:BoolExact = IsBlockGiven v20
26572658
IncrCounter inline_cfunc_optimized_send_count
26582659
CheckInterrupts
2659-
Return v20
2660+
Return v21
26602661
");
26612662
}
26622663

@@ -2679,7 +2680,7 @@ mod hir_opt_tests {
26792680
PatchPoint NoSingletonClass(Object@0x1000)
26802681
PatchPoint MethodRedefined(Object@0x1000, block_given?@0x1008, cme:0x1010)
26812682
v19:HeapObject[class_exact*:Object@VALUE(0x1000)] = GuardType v6, HeapObject[class_exact*:Object@VALUE(0x1000)]
2682-
v20:BoolExact = IsBlockGiven
2683+
v20:FalseClass = Const Value(false)
26832684
IncrCounter inline_cfunc_optimized_send_count
26842685
CheckInterrupts
26852686
Return v20

0 commit comments

Comments
 (0)