From 679ef34586e7a43151865cb7f33a3253d815f7cf Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 10 Aug 2022 10:35:48 -0700 Subject: 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: #@-e:1 (1,0)-(1,13)> (catch: FALSE) 0000 opt_getconstant_path ( 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. --- .../views/_mjit_compile_getconstant_path.erb | 30 +++++++++++++++++++++ .../ruby_vm/views/_mjit_compile_getinlinecache.erb | 31 ---------------------- tool/ruby_vm/views/mjit_compile.inc.erb | 4 +-- 3 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 tool/ruby_vm/views/_mjit_compile_getconstant_path.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_getinlinecache.erb (limited to 'tool') diff --git a/tool/ruby_vm/views/_mjit_compile_getconstant_path.erb b/tool/ruby_vm/views/_mjit_compile_getconstant_path.erb new file mode 100644 index 0000000000..c321da9a52 --- /dev/null +++ b/tool/ruby_vm/views/_mjit_compile_getconstant_path.erb @@ -0,0 +1,30 @@ +% # -*- C -*- +% # Copyright (c) 2020 Takashi Kokubun. All rights reserved. +% # +% # This file is a part of the programming language Ruby. Permission is hereby +% # granted, to either redistribute and/or modify this file, provided that the +% # conditions mentioned in the file COPYING are met. Consult the file for +% # details. +% +% # compiler: Declare dst and ic +% insn.opes.each_with_index do |ope, i| + <%= ope.fetch(:decl) %> = (<%= ope.fetch(:type) %>)operands[<%= i %>]; +% end + +% # compiler: Capture IC values, locking getinlinecache + struct iseq_inline_constant_cache_entry *ice = ic->entry; + if (ice != NULL && !status->compile_info->disable_const_cache) { +% # JIT: Inline everything in IC, and cancel the slow path + fprintf(f, " if (vm_inlined_ic_hit_p(0x%"PRIxVALUE", 0x%"PRIxVALUE", (const rb_cref_t *)0x%"PRIxVALUE", reg_cfp->ep)) {", ice->flags, ice->value, (VALUE)ice->ic_cref); + fprintf(f, " stack[%d] = 0x%"PRIxVALUE";\n", b->stack_size, ice->value); + fprintf(f, " }"); + fprintf(f, " else {"); + fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); + fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); + fprintf(f, " goto const_cancel;\n"); + fprintf(f, " }"); + +% # compiler: Move JIT compiler's internal stack pointer + b->stack_size += <%= insn.call_attribute('sp_inc') %>; + break; + } diff --git a/tool/ruby_vm/views/_mjit_compile_getinlinecache.erb b/tool/ruby_vm/views/_mjit_compile_getinlinecache.erb deleted file mode 100644 index fa38af4045..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_getinlinecache.erb +++ /dev/null @@ -1,31 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2020 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% # compiler: Declare dst and ic -% insn.opes.each_with_index do |ope, i| - <%= ope.fetch(:decl) %> = (<%= ope.fetch(:type) %>)operands[<%= i %>]; -% end - -% # compiler: Capture IC values, locking getinlinecache - struct iseq_inline_constant_cache_entry *ice = ic->entry; - if (ice != NULL && !status->compile_info->disable_const_cache) { -% # JIT: Inline everything in IC, and cancel the slow path - fprintf(f, " if (vm_inlined_ic_hit_p(0x%"PRIxVALUE", 0x%"PRIxVALUE", (const rb_cref_t *)0x%"PRIxVALUE", reg_cfp->ep)) {", ice->flags, ice->value, (VALUE)ice->ic_cref); - fprintf(f, " stack[%d] = 0x%"PRIxVALUE";\n", b->stack_size, ice->value); - fprintf(f, " goto label_%d;\n", pos + insn_len(insn) + (int)dst); - fprintf(f, " }"); - fprintf(f, " else {"); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " goto const_cancel;\n"); - fprintf(f, " }"); - -% # compiler: Move JIT compiler's internal stack pointer - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - break; - } diff --git a/tool/ruby_vm/views/mjit_compile.inc.erb b/tool/ruby_vm/views/mjit_compile.inc.erb index 0e66f78007..00808b21ff 100644 --- a/tool/ruby_vm/views/mjit_compile.inc.erb +++ b/tool/ruby_vm/views/mjit_compile.inc.erb @@ -65,8 +65,8 @@ switch (insn) { <%= render 'mjit_compile_ivar', locals: { insn: insn } -%> % when 'invokebuiltin', 'opt_invokebuiltin_delegate' <%= render 'mjit_compile_invokebuiltin', locals: { insn: insn } -%> -% when 'opt_getinlinecache' -<%= render 'mjit_compile_getinlinecache', locals: { insn: insn } -%> +% when 'opt_getconstant_path' +<%= render 'mjit_compile_getconstant_path', locals: { insn: insn } -%> % when 'leave', 'opt_invokebuiltin_delegate_leave' % # opt_invokebuiltin_delegate_leave also implements leave insn. We need to handle it here for inlining. % if insn.name == 'opt_invokebuiltin_delegate_leave' -- cgit v1.2.3