Skip to content

Commit 1b7f1a0

Browse files
committed
plumb through reason
1 parent 61fdaeb commit 1b7f1a0

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

zjit/src/codegen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
449449
Insn::Test { val } => gen_test(asm, opnd!(val)),
450450
Insn::GuardType { val, guard_type, state } => gen_guard_type(jit, asm, opnd!(val), *guard_type, &function.frame_state(*state)),
451451
Insn::GuardTypeNot { val, guard_type, state } => gen_guard_type_not(jit, asm, opnd!(val), *guard_type, &function.frame_state(*state)),
452-
Insn::GuardBitEquals { val, expected, state } => gen_guard_bit_equals(jit, asm, opnd!(val), *expected, &function.frame_state(*state)),
452+
&Insn::GuardBitEquals { val, expected, reason, state } => gen_guard_bit_equals(jit, asm, opnd!(val), expected, reason, &function.frame_state(state)),
453453
&Insn::GuardBlockParamProxy { level, state } => no_output!(gen_guard_block_param_proxy(jit, asm, level, &function.frame_state(state))),
454454
Insn::GuardNotFrozen { recv, state } => gen_guard_not_frozen(jit, asm, opnd!(recv), &function.frame_state(*state)),
455455
Insn::GuardNotShared { recv, state } => gen_guard_not_shared(jit, asm, opnd!(recv), &function.frame_state(*state)),
@@ -2083,15 +2083,15 @@ fn gen_guard_type_not(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd, g
20832083
}
20842084

20852085
/// Compile an identity check with a side exit
2086-
fn gen_guard_bit_equals(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd, expected: crate::hir::Const, state: &FrameState) -> lir::Opnd {
2086+
fn gen_guard_bit_equals(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd, expected: crate::hir::Const, reason: SideExitReason, state: &FrameState) -> lir::Opnd {
20872087
let expected_opnd: Opnd = match expected {
20882088
crate::hir::Const::Value(v) => { Opnd::Value(v) }
20892089
crate::hir::Const::CInt64(v) => { v.into() }
20902090
crate::hir::Const::CShape(v) => { Opnd::UImm(v.0 as u64) }
20912091
_ => panic!("gen_guard_bit_equals: unexpected hir::Const {expected:?}"),
20922092
};
20932093
asm.cmp(val, expected_opnd);
2094-
asm.jnz(side_exit(jit, state, GuardBitEquals(expected)));
2094+
asm.jnz(side_exit(jit, state, reason));
20952095
val
20962096
}
20972097

zjit/src/hir.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ pub enum SideExitReason {
491491
GuardType(Type),
492492
GuardTypeNot(Type),
493493
GuardShape(ShapeId),
494-
GuardBitEquals(Const),
494+
ExpandArray,
495495
GuardNotFrozen,
496496
GuardNotShared,
497497
GuardLess,
@@ -580,7 +580,6 @@ impl std::fmt::Display for SideExitReason {
580580
SideExitReason::UnhandledDuparraySend(method_id) => write!(f, "UnhandledDuparraySend({method_id})"),
581581
SideExitReason::GuardType(guard_type) => write!(f, "GuardType({guard_type})"),
582582
SideExitReason::GuardTypeNot(guard_type) => write!(f, "GuardTypeNot({guard_type})"),
583-
SideExitReason::GuardBitEquals(value) => write!(f, "GuardBitEquals({})", value.print(&PtrPrintMap::identity())),
584583
SideExitReason::GuardNotShared => write!(f, "GuardNotShared"),
585584
SideExitReason::PatchPoint(invariant) => write!(f, "PatchPoint({invariant})"),
586585
_ => write!(f, "{self:?}"),
@@ -954,7 +953,7 @@ pub enum Insn {
954953
GuardType { val: InsnId, guard_type: Type, state: InsnId },
955954
GuardTypeNot { val: InsnId, guard_type: Type, state: InsnId },
956955
/// Side-exit if val is not the expected Const.
957-
GuardBitEquals { val: InsnId, expected: Const, state: InsnId },
956+
GuardBitEquals { val: InsnId, expected: Const, reason: SideExitReason, state: InsnId },
958957
/// Side-exit if val doesn't have the expected shape.
959958
GuardShape { val: InsnId, shape: ShapeId, state: InsnId },
960959
/// Side-exit if the block param has been modified or the block handler for the frame
@@ -1975,7 +1974,7 @@ impl Function {
19751974
&IfFalse { val, ref target } => IfFalse { val: find!(val), target: find_branch_edge!(target) },
19761975
&GuardType { val, guard_type, state } => GuardType { val: find!(val), guard_type, state },
19771976
&GuardTypeNot { val, guard_type, state } => GuardTypeNot { val: find!(val), guard_type, state },
1978-
&GuardBitEquals { val, expected, state } => GuardBitEquals { val: find!(val), expected, state },
1977+
&GuardBitEquals { val, expected, reason, state } => GuardBitEquals { val: find!(val), expected, reason, state },
19791978
&GuardShape { val, shape, state } => GuardShape { val: find!(val), shape, state },
19801979
&GuardBlockParamProxy { level, state } => GuardBlockParamProxy { level, state: find!(state) },
19811980
&GuardNotFrozen { recv, state } => GuardNotFrozen { recv: find!(recv), state },
@@ -3078,6 +3077,15 @@ impl Function {
30783077
})
30793078
}
30803079

3080+
fn guard_shape(&mut self, block: BlockId, val: InsnId, expected: ShapeId, state: InsnId) -> InsnId {
3081+
self.push_insn(block, Insn::GuardBitEquals {
3082+
val,
3083+
expected: Const::CShape(expected),
3084+
reason: SideExitReason::GuardShape(expected),
3085+
state
3086+
})
3087+
}
3088+
30813089
fn optimize_getivar(&mut self) {
30823090
for block in self.rpo() {
30833091
let old_insns = std::mem::take(&mut self.blocks[block.0].insns);
@@ -3104,7 +3112,7 @@ impl Function {
31043112
}
31053113
let self_val = self.push_insn(block, Insn::GuardType { val: self_val, guard_type: types::HeapBasicObject, state });
31063114
let shape = self.load_shape(block, self_val);
3107-
self.push_insn(block, Insn::GuardBitEquals { val: shape, expected: Const::CShape(recv_type.shape()), state });
3115+
self.guard_shape(block, shape, recv_type.shape(), state);
31083116
let mut ivar_index: u16 = 0;
31093117
let replacement = if ! unsafe { rb_shape_get_iv_index(recv_type.shape().0, id, &mut ivar_index) } {
31103118
// If there is no IVAR index, then the ivar was undefined when we
@@ -3159,7 +3167,7 @@ impl Function {
31593167
}
31603168
let self_val = self.push_insn(block, Insn::GuardType { val: self_val, guard_type: types::HeapBasicObject, state });
31613169
let shape = self.load_shape(block, self_val);
3162-
self.push_insn(block, Insn::GuardBitEquals { val: shape, expected: Const::CShape(recv_type.shape()), state });
3170+
self.guard_shape(block, shape, recv_type.shape(), state);
31633171
let mut ivar_index: u16 = 0;
31643172
let replacement = if unsafe { rb_shape_get_iv_index(recv_type.shape().0, id, &mut ivar_index) } {
31653173
self.push_insn(block, Insn::Const { val: Const::Value(pushval) })
@@ -3231,7 +3239,7 @@ impl Function {
32313239
}
32323240
let self_val = self.push_insn(block, Insn::GuardType { val: self_val, guard_type: types::HeapBasicObject, state });
32333241
let shape = self.load_shape(block, self_val);
3234-
self.push_insn(block, Insn::GuardBitEquals { val: shape, expected: Const::CShape(recv_type.shape()), state });
3242+
self.guard_shape(block, shape, recv_type.shape(), state);
32353243
// Current shape contains this ivar
32363244
let (ivar_storage, offset) = if recv_type.flags().is_embedded() {
32373245
// See ROBJECT_FIELDS() from include/ruby/internal/core/robject.h
@@ -6273,7 +6281,7 @@ pub fn iseq_to_hir(iseq: *const rb_iseq_t) -> Result<Function, ParseError> {
62736281
let val = state.stack_pop()?;
62746282
let array = fun.push_insn(block, Insn::GuardType { val, guard_type: types::ArrayExact, state: exit_id, });
62756283
let length = fun.push_insn(block, Insn::ArrayLength { array });
6276-
fun.push_insn(block, Insn::GuardBitEquals { val: length, expected: Const::CInt64(num as i64), state: exit_id });
6284+
fun.push_insn(block, Insn::GuardBitEquals { val: length, expected: Const::CInt64(num as i64), reason: SideExitReason::ExpandArray, state: exit_id });
62776285
for i in (0..num).rev() {
62786286
// TODO(max): Add a short-cut path for long indices into an array where the
62796287
// index is known to be in-bounds

zjit/src/stats.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ make_counters! {
190190
exit_guard_bit_equals_failure,
191191
exit_guard_int_equals_failure,
192192
exit_guard_shape_failure,
193+
exit_expandarray_failure,
193194
exit_guard_not_frozen_failure,
194195
exit_guard_not_shared_failure,
195196
exit_guard_less_failure,
@@ -509,8 +510,8 @@ pub fn side_exit_counter(reason: crate::hir::SideExitReason) -> Counter {
509510
BoxFixnumOverflow => exit_box_fixnum_overflow,
510511
GuardType(_) => exit_guard_type_failure,
511512
GuardTypeNot(_) => exit_guard_type_not_failure,
512-
GuardBitEquals(_) => exit_guard_bit_equals_failure,
513513
GuardShape(_) => exit_guard_shape_failure,
514+
ExpandArray => exit_expandarray_failure,
514515
GuardNotFrozen => exit_guard_not_frozen_failure,
515516
GuardNotShared => exit_guard_not_shared_failure,
516517
GuardLess => exit_guard_less_failure,

0 commit comments

Comments
 (0)