ZJIT: Replace GetConstantPath with Const if the IC is not empty#13183
Merged
k0kubun merged 3 commits intoruby:masterfrom Apr 28, 2025
Merged
ZJIT: Replace GetConstantPath with Const if the IC is not empty#13183k0kubun merged 3 commits intoruby:masterfrom
k0kubun merged 3 commits intoruby:masterfrom
Conversation
5deefac to
8ec8358
Compare
Contributor
Author
|
This is based on / inspired by the YJIT impl but only handles the best/fastest-path case: fn gen_opt_getconstant_path(
jit: &mut JITState,
asm: &mut Assembler,
) -> Option<CodegenStatus> {
let const_cache_as_value = jit.get_arg(0);
let ic: *const iseq_inline_constant_cache = const_cache_as_value.as_ptr();
let idlist: *const ID = unsafe { (*ic).segments };
// Make sure there is an exit for this block as the interpreter might want
// to invalidate this block from yjit_constant_ic_update().
jit_ensure_block_entry_exit(jit, asm)?;
// See vm_ic_hit_p(). The same conditions are checked in yjit_constant_ic_update().
// If a cache is not filled, fallback to the general C call.
let ice = unsafe { (*ic).entry };
if ice.is_null() {
// Prepare for const_missing
jit_prepare_non_leaf_call(jit, asm);
// If this does not trigger const_missing, vm_ic_update will invalidate this block.
extern "C" {
fn rb_vm_opt_getconstant_path(ec: EcPtr, cfp: CfpPtr, ic: *const u8) -> VALUE;
}
let val = asm.ccall(
rb_vm_opt_getconstant_path as *const u8,
vec![EC, CFP, Opnd::const_ptr(ic as *const u8)],
);
let stack_top = asm.stack_push(Type::Unknown);
asm.store(stack_top, val);
return jump_to_next_insn(jit, asm);
}
let cref_sensitive = !unsafe { (*ice).ic_cref }.is_null();
let is_shareable = unsafe { rb_yjit_constcache_shareable(ice) };
let needs_checks = cref_sensitive || (!is_shareable && !assume_single_ractor_mode(jit, asm));
if needs_checks {
// Cache is keyed on a certain lexical scope. Use the interpreter's cache.
let inline_cache = asm.load(Opnd::const_ptr(ic as *const u8));
// Call function to verify the cache. It doesn't allocate or call methods.
// This includes a check for Ractor safety
let ret_val = asm.ccall(
rb_vm_ic_hit_p as *const u8,
vec![inline_cache, Opnd::mem(64, CFP, RUBY_OFFSET_CFP_EP)]
);
// Check the result. SysV only specifies one byte for _Bool return values,
// so it's important we only check one bit to ignore the higher bits in the register.
asm.test(ret_val, 1.into());
asm.jz(Target::side_exit(Counter::opt_getconstant_path_ic_miss));
let inline_cache = asm.load(Opnd::const_ptr(ic as *const u8));
let ic_entry = asm.load(Opnd::mem(
64,
inline_cache,
RUBY_OFFSET_IC_ENTRY
));
let ic_entry_val = asm.load(Opnd::mem(
64,
ic_entry,
RUBY_OFFSET_ICE_VALUE
));
// Push ic->entry->value
let stack_top = asm.stack_push(Type::Unknown);
asm.store(stack_top, ic_entry_val);
} else {
// Invalidate output code on any constant writes associated with
// constants referenced within the current block.
jit.assume_stable_constant_names(asm, idlist);
jit_putobject(asm, unsafe { (*ice).value });
}
jump_to_next_insn(jit, asm)
} |
✅ All Tests passed!✖️no tests failed ✔️61871 tests passed(2 flakes) |
k0kubun
approved these changes
Apr 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.