Skip to content

Commit d8a6d43

Browse files
tekknolagik0kubun
authored andcommitted
Fix opt_neq HIR codegen
1 parent 508a049 commit d8a6d43

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

test/ruby/test_zjit.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,17 @@ def test(a, b) = a == b
121121
}, call_threshold: 2
122122
end
123123

124-
def test_opt_neq
124+
def test_opt_neq_dynamic
125+
# TODO(max): Don't split this test; instead, run all tests with and without
126+
# profiling.
127+
assert_compiles '[false, true]', %q{
128+
def test(a, b) = a != b
129+
test(0, 2) # profile opt_neq
130+
[test(1, 1), test(0, 1)]
131+
}, call_threshold: 1
132+
end
133+
134+
def test_opt_neq_fixnum
125135
assert_compiles '[false, true]', %q{
126136
def test(a, b) = a != b
127137
test(0, 2) # profile opt_neq

zjit/src/hir.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,19 @@ pub fn iseq_to_hir(iseq: *const rb_iseq_t) -> Result<Function, ParseError> {
14571457
let exit_id = fun.push_insn(block, Insn::Snapshot { state: exit_state.clone() });
14581458
if payload.have_two_fixnums(current_insn_idx as usize) {
14591459
fun.push_insn(block, Insn::PatchPoint(Invariant::BOPRedefined { klass: INTEGER_REDEFINED_OP_FLAG, bop: $bop }));
1460+
if $bop == BOP_NEQ {
1461+
// For opt_neq, the interpreter checks that both neq and eq are unchanged.
1462+
fun.push_insn(block, Insn::PatchPoint(Invariant::BOPRedefined { klass: INTEGER_REDEFINED_OP_FLAG, bop: BOP_EQ }));
1463+
}
14601464
let (left, right) = guard_two_fixnums(&mut state, exit_id, &mut fun, block)?;
14611465
state.stack_push(fun.push_insn(block, Insn::$insn { left, right$(, $key: exit_id)? }));
14621466
} else {
1463-
let cd: *const rb_call_data = get_arg(pc, 0).as_ptr();
1467+
let cd: *const rb_call_data = if $bop == BOP_NEQ {
1468+
// opt_neq is a special case where it has two cd and the first one is opt_eq.
1469+
get_arg(pc, 1).as_ptr()
1470+
} else {
1471+
get_arg(pc, 0).as_ptr()
1472+
};
14641473
let right = state.stack_pop()?;
14651474
let left = state.stack_pop()?;
14661475
state.stack_push(fun.push_insn(block, Insn::SendWithoutBlock { self_val: left, call_info: CallInfo { method_name: $method_name.into() }, cd, args: vec![right], state: exit_id }));
@@ -2300,10 +2309,11 @@ mod tests {
23002309
fn test:
23012310
bb0(v0:BasicObject, v1:BasicObject):
23022311
PatchPoint BOPRedefined(INTEGER_REDEFINED_OP_FLAG, BOP_NEQ)
2303-
v5:Fixnum = GuardType v0, Fixnum
2304-
v6:Fixnum = GuardType v1, Fixnum
2305-
v7:BoolExact = FixnumNeq v5, v6
2306-
Return v7
2312+
PatchPoint BOPRedefined(INTEGER_REDEFINED_OP_FLAG, BOP_EQ)
2313+
v6:Fixnum = GuardType v0, Fixnum
2314+
v7:Fixnum = GuardType v1, Fixnum
2315+
v8:BoolExact = FixnumNeq v6, v7
2316+
Return v8
23072317
"#]]);
23082318
}
23092319

@@ -3105,10 +3115,11 @@ mod opt_tests {
31053115
fn test:
31063116
bb0(v0:BasicObject, v1:BasicObject):
31073117
PatchPoint BOPRedefined(INTEGER_REDEFINED_OP_FLAG, BOP_NEQ)
3108-
v5:Fixnum = GuardType v0, Fixnum
3109-
v6:Fixnum = GuardType v1, Fixnum
3110-
v8:Fixnum[5] = Const Value(5)
3111-
Return v8
3118+
PatchPoint BOPRedefined(INTEGER_REDEFINED_OP_FLAG, BOP_EQ)
3119+
v6:Fixnum = GuardType v0, Fixnum
3120+
v7:Fixnum = GuardType v1, Fixnum
3121+
v9:Fixnum[5] = Const Value(5)
3122+
Return v9
31123123
"#]]);
31133124
}
31143125

0 commit comments

Comments
 (0)