diff options
author | John Hawthorn <john@hawthorn.email> | 2022-08-10 10:35:48 -0700 |
---|---|---|
committer | John Hawthorn <john@hawthorn.email> | 2022-09-01 15:20:49 -0700 |
commit | 679ef34586e7a43151865cb7f33a3253d815f7cf (patch) | |
tree | 1f46e901c2c77438e050585e9e9708492cc985a6 /yjit/src/codegen.rs | |
parent | 7064d259bc20050d467874e5622082c29529a2d3 (diff) | |
download | ruby-679ef34586e7a43151865cb7f33a3253d815f7cf.tar.gz |
New constant caching insn: opt_getconstant_path
Previously YARV bytecode implemented constant caching by having a pair
of instructions, opt_getinlinecache and opt_setinlinecache, wrapping a
series of getconstant calls (with putobject providing supporting
arguments).
This commit replaces that pattern with a new instruction,
opt_getconstant_path, handling both getting/setting the inline cache and
fetching the constant on a cache miss.
This is implemented by storing the full constant path as a
null-terminated array of IDs inside of the IC structure. idNULL is used
to signal an absolute constant reference.
$ ./miniruby --dump=insns -e '::Foo::Bar::Baz'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,13)> (catch: FALSE)
0000 opt_getconstant_path <ic:0 ::Foo::Bar::Baz> ( 1)[Li]
0002 leave
The motivation for this is that we had increasingly found the need to
disassemble the instructions between the opt_getinlinecache and
opt_setinlinecache in order to determine the constant we are fetching,
or otherwise store metadata.
This disassembly was done:
* In opt_setinlinecache, to register the IC against the constant names
it is using for granular invalidation.
* In rb_iseq_free, to unregister the IC from the invalidation table.
* In YJIT to find the position of a opt_getinlinecache instruction to
invalidate it when the cache is populated
* In YJIT to register the constant names being used for invalidation.
With this change we no longe need disassemly for these (in fact
rb_iseq_each is now unused), as the list of constant names being
referenced is held in the IC. This should also make it possible to make
more optimizations in the future.
This may also reduce the size of iseqs, as previously each segment
required 32 bytes (on 64-bit platforms) for each constant segment. This
implementation only stores one ID per-segment.
There should be no significant performance change between this and the
previous implementation. Previously opt_getinlinecache was a "leaf"
instruction, but it included a jump (almost always to a separate cache
line). Now opt_getconstant_path is a non-leaf (it may
raise/autoload/call const_missing) but it does not jump. These seem to
even out.
Diffstat (limited to 'yjit/src/codegen.rs')
-rw-r--r-- | yjit/src/codegen.rs | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/yjit/src/codegen.rs b/yjit/src/codegen.rs index ac42b4a1e2..58b30d5c85 100644 --- a/yjit/src/codegen.rs +++ b/yjit/src/codegen.rs @@ -769,9 +769,10 @@ pub fn gen_single_block( .try_into() .unwrap(); - // opt_getinlinecache wants to be in a block all on its own. Cut the block short - // if we run into it. See gen_opt_getinlinecache() for details. - if opcode == YARVINSN_opt_getinlinecache.as_usize() && insn_idx > starting_insn_idx { + // We need opt_getconstant_path to be in a block all on its own. Cut the block short + // if we run into it. This is necessary because we want to invalidate based on the + // instruction's index. + if opcode == YARVINSN_opt_getconstant_path.as_usize() && insn_idx > starting_insn_idx { jump_to_next_insn(&mut jit, &ctx, &mut asm, ocb); break; } @@ -5508,15 +5509,15 @@ fn gen_setclassvariable( KeepCompiling } -fn gen_opt_getinlinecache( +fn gen_opt_getconstant_path( jit: &mut JITState, ctx: &mut Context, asm: &mut Assembler, ocb: &mut OutlinedCb, ) -> CodegenStatus { - let jump_offset = jit_get_arg(jit, 0); - let const_cache_as_value = jit_get_arg(jit, 1); + let const_cache_as_value = jit_get_arg(jit, 0); let ic: *const iseq_inline_constant_cache = const_cache_as_value.as_ptr(); + let idlist: *const ID = unsafe { (*ic).segments }; // See vm_ic_hit_p(). The same conditions are checked in yjit_constant_ic_update(). let ice = unsafe { (*ic).entry }; @@ -5573,22 +5574,12 @@ fn gen_opt_getinlinecache( // Invalidate output code on any constant writes associated with // constants referenced within the current block. - assume_stable_constant_names(jit, ocb); + assume_stable_constant_names(jit, ocb, idlist); jit_putobject(jit, ctx, asm, unsafe { (*ice).value }); } - // Jump over the code for filling the cache - let jump_idx = jit_next_insn_idx(jit) + jump_offset.as_u32(); - gen_direct_jump( - jit, - ctx, - BlockId { - iseq: jit.iseq, - idx: jump_idx, - }, - asm, - ); + jump_to_next_insn(jit, ctx, asm, ocb); EndBlock } @@ -5920,7 +5911,7 @@ fn get_gen_fn(opcode: VALUE) -> Option<InsnGenFn> { YARVINSN_opt_size => Some(gen_opt_size), YARVINSN_opt_length => Some(gen_opt_length), YARVINSN_opt_regexpmatch2 => Some(gen_opt_regexpmatch2), - YARVINSN_opt_getinlinecache => Some(gen_opt_getinlinecache), + YARVINSN_opt_getconstant_path => Some(gen_opt_getconstant_path), YARVINSN_invokebuiltin => Some(gen_invokebuiltin), YARVINSN_opt_invokebuiltin_delegate => Some(gen_opt_invokebuiltin_delegate), YARVINSN_opt_invokebuiltin_delegate_leave => Some(gen_opt_invokebuiltin_delegate), |