Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YJIT: specialized codegen for integer right shift #9564

Merged
merged 2 commits into from Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions bootstraptest/test_yjit.rb
Expand Up @@ -4352,3 +4352,8 @@ def entry
assert_equal '[2, 4611686018427387904]', %q{
[1.succ, 4611686018427387903.succ]
}

# Integer right shift
assert_equal '[0, 1, -4]', %q{
[0 >> 1, 2 >> 1, -7 >> 1]
}
1 change: 1 addition & 0 deletions yjit.rb
Expand Up @@ -290,6 +290,7 @@ def _print_stats_reasons(stats, out) # :nodoc:
print_counters(stats, out: out, prefix: "#{insn}_", prompt: "#{insn} exit reasons:", optional: true)
end
print_counters(stats, out: out, prefix: 'lshift_', prompt: 'left shift (opt_ltlt) exit reasons: ')
print_counters(stats, out: out, prefix: 'rshift_', prompt: 'right shift (>>) exit reasons: ')
print_counters(stats, out: out, prefix: 'invalidate_', prompt: 'invalidation reasons: ')
end

Expand Down
52 changes: 52 additions & 0 deletions yjit/src/codegen.rs
Expand Up @@ -4737,6 +4737,57 @@ fn jit_rb_int_lshift(
true
}

fn jit_rb_int_rshift(
jit: &mut JITState,
asm: &mut Assembler,
ocb: &mut OutlinedCb,
_ci: *const rb_callinfo,
_cme: *const rb_callable_method_entry_t,
_block: Option<BlockHandler>,
_argc: i32,
_known_recv_class: *const VALUE,
) -> bool {
if asm.ctx.two_fixnums_on_stack(jit) != Some(true) {
return false;
}
guard_two_fixnums(jit, asm, ocb);

let comptime_shift = jit.peek_at_stack(&asm.ctx, 0);

// Untag the fixnum shift amount
let shift_amt = comptime_shift.as_isize() >> 1;
if shift_amt > 63 || shift_amt < 0 {
return false;
}

// Fallback to a C call if the shift amount varies
if asm.ctx.get_chain_depth() > 2 {
return false;
}

let rhs = asm.stack_pop(1);
let lhs = asm.stack_pop(1);

// Guard on the shift amount we speculated on
asm.cmp(rhs, comptime_shift.into());
jit_chain_guard(
JCC_JNE,
jit,
asm,
ocb,
2, // defer_compilation increments chain_depth
Counter::rshift_amount_changed,
);

let shift_opnd = Opnd::UImm(shift_amt as u64);
let out_val = asm.rshift(lhs, shift_opnd);
let out_val = asm.or(out_val, 1.into());

let ret_opnd = asm.stack_push(Type::Fixnum);
asm.mov(ret_opnd, out_val);
true
}

fn jit_rb_int_aref(
jit: &mut JITState,
asm: &mut Assembler,
Expand Down Expand Up @@ -8912,6 +8963,7 @@ pub fn yjit_reg_method_codegen_fns() {
yjit_reg_method(rb_cInteger, "succ", jit_rb_int_succ);
yjit_reg_method(rb_cInteger, "/", jit_rb_int_div);
yjit_reg_method(rb_cInteger, "<<", jit_rb_int_lshift);
yjit_reg_method(rb_cInteger, ">>", jit_rb_int_rshift);
yjit_reg_method(rb_cInteger, "[]", jit_rb_int_aref);

yjit_reg_method(rb_cString, "empty?", jit_rb_str_empty_p);
Expand Down
2 changes: 2 additions & 0 deletions yjit/src/stats.rs
Expand Up @@ -453,6 +453,8 @@ make_counters! {
lshift_amount_changed,
lshift_overflow,

rshift_amount_changed,

opt_aref_argc_not_one,
opt_aref_arg_not_fixnum,
opt_aref_not_array,
Expand Down