From fc6c5e126dbbea8b9ed2730e61f70356fbdc3a12 Mon Sep 17 00:00:00 2001 From: k0kubun Date: Sun, 14 Oct 2018 09:24:43 +0000 Subject: _mjit_compile_getivar.erb: optimize IC-hit getivar by inlining index (and serial to invalidate that) and simplifying the branch by using JIT cancellation. mjit_compile.inc.erb: use the above file mjit_compile.c: copy USE_IC_FOR_IVAR definition. will move this to another shared file later. common.mk: add new dependency test/ruby/test_jit.rb: cover this case === Optcarrot benchmark === ``` $ benchmark-driver benchmark.yml --rbenv '2.0.0::2.0.0-p648;before::before --disable-gems;before+JIT::before --disable-gems --jit;after::after --disable-gems;after+JIT::after --disable-gems --jit' -v --repeat-count 24 2.0.0: ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux] before: ruby 2.6.0dev (2018-10-14 trunk 65072) [x86_64-linux] before+JIT: ruby 2.6.0dev (2018-10-14 trunk 65072) +JIT [x86_64-linux] after: ruby 2.6.0dev (2018-10-14 trunk 65072) [x86_64-linux] last_commit=_mjit_compile_getivar.erb: optimize IC-hit getivar after+JIT: ruby 2.6.0dev (2018-10-14 trunk 65072) +JIT [x86_64-linux] last_commit=_mjit_compile_getivar.erb: optimize IC-hit getivar Calculating ------------------------------------- 2.0.0 before before+JIT after after+JIT Optcarrot Lan_Master.nes 36.065 53.896 71.565 53.856 84.747 fps Comparison: Optcarrot Lan_Master.nes after+JIT: 84.7 fps before+JIT: 71.6 fps - 1.18x slower before: 53.9 fps - 1.57x slower after: 53.9 fps - 1.57x slower 2.0.0: 36.1 fps - 2.35x slower ``` git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65073 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- common.mk | 2 +- mjit_compile.c | 5 ++++ test/ruby/test_jit.rb | 19 ++++++++++++ tool/ruby_vm/views/_mjit_compile_getivar.erb | 44 ++++++++++++++++++++++++++++ tool/ruby_vm/views/mjit_compile.inc.erb | 2 ++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tool/ruby_vm/views/_mjit_compile_getivar.erb diff --git a/common.mk b/common.mk index 41bb45cacb..e6f8c4a843 100644 --- a/common.mk +++ b/common.mk @@ -938,7 +938,7 @@ $(srcs_vpath)vm.inc: $(srcdir)/tool/ruby_vm/views/vm.inc.erb $(inc_common_header $(srcdir)/tool/ruby_vm/views/_insn_entry.erb $(srcdir)/tool/ruby_vm/views/_trace_instruction.erb $(srcs_vpath)mjit_compile.inc: $(srcdir)/tool/ruby_vm/views/mjit_compile.inc.erb $(inc_common_headers) \ $(srcdir)/tool/ruby_vm/views/_mjit_compile_insn.erb $(srcdir)/tool/ruby_vm/views/_mjit_compile_send.erb \ - $(srcdir)/tool/ruby_vm/views/_mjit_compile_send_guard.erb \ + $(srcdir)/tool/ruby_vm/views/_mjit_compile_send_guard.erb $(srcdir)/tool/ruby_vm/views/_mjit_compile_getivar.erb \ $(srcdir)/tool/ruby_vm/views/_mjit_compile_insn_body.erb $(srcdir)/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb common-srcs: $(srcs_vpath)parse.c $(srcs_vpath)lex.c $(srcs_vpath)enc/trans/newline.c $(srcs_vpath)id.c \ diff --git a/mjit_compile.c b/mjit_compile.c index b2ece0eb77..89d1a96a10 100644 --- a/mjit_compile.c +++ b/mjit_compile.c @@ -108,6 +108,11 @@ comment_id(FILE *f, ID id) #endif } +/* TODO: share this with vm_insnhelper.c */ +#ifndef USE_IC_FOR_IVAR +#define USE_IC_FOR_IVAR 1 +#endif + static void compile_insns(FILE *f, const struct rb_iseq_constant_body *body, unsigned int stack_size, unsigned int pos, struct compile_status *status); diff --git a/test/ruby/test_jit.rb b/test/ruby/test_jit.rb index 43e6d80573..3b18373212 100644 --- a/test/ruby/test_jit.rb +++ b/test/ruby/test_jit.rb @@ -113,6 +113,25 @@ class TestJIT < Test::Unit::TestCase @foo = 1 @foo end; + + # optimized getinstancevariable call + assert_eval_with_jit("#{<<~"begin;"}\n#{<<~"end;"}", stdout: '33', success_count: 1, min_calls: 2) + begin; + class A + def initialize + @a = 1 + @b = 2 + end + + def three + @a + @b + end + end + + a = A.new + print(a.three) # set ic + print(a.three) # inlined ic + end; end def test_compile_insn_classvariable diff --git a/tool/ruby_vm/views/_mjit_compile_getivar.erb b/tool/ruby_vm/views/_mjit_compile_getivar.erb new file mode 100644 index 0000000000..630fd72959 --- /dev/null +++ b/tool/ruby_vm/views/_mjit_compile_getivar.erb @@ -0,0 +1,44 @@ +% # -*- mode:c; style:ruby; coding: utf-8; indent-tabs-mode: nil -*- +% # Copyright (c) 2018 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. +% +% # Optimized case of get_instancevariable instruction. +#if USE_IC_FOR_IVAR +{ +% # compiler: Prepare operands which may be used by `insn.call_attribute` +% insn.opes.each_with_index do |ope, i| + MAYBE_UNUSED(<%= ope.fetch(:decl) %>) = (<%= ope.fetch(:type) %>)operands[<%= i %>]; +% end +% +% # compiler: Consider receiver as T_OBJECT if ic->ic_serial is set + if (ic->ic_serial) { +% # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`. +% # <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%> +% +% # JIT: prepare vm_getivar's arguments and variables + fprintf(f, "{\n"); + fprintf(f, " VALUE obj = GET_SELF();\n"); + fprintf(f, " const rb_serial_t ic_serial = (rb_serial_t)%"PRI_SERIALT_PREFIX"u;\n", ic->ic_serial); + fprintf(f, " const st_index_t index = %lu;\n", ic->ic_value.index); +% +% # JIT: cache hit path of vm_getivar, or cancel JIT. + fprintf(f, " if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj))) {\n"); + fprintf(f, " stack[%d] = ROBJECT_IVPTR(obj)[index];\n", b->stack_size); + fprintf(f, " }\n"); + fprintf(f, " else {\n"); + fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); + fprintf(f, " reg_cfp->sp = (VALUE *)reg_cfp->bp + %d;\n", b->stack_size + 1); + fprintf(f, " goto cancel;\n"); + fprintf(f, " }\n"); + +% # compiler: Move JIT compiler's internal stack pointer + b->stack_size += <%= insn.call_attribute('sp_inc') %>; + fprintf(f, "}\n"); + break; + } +} +#endif /* USE_IC_FOR_IVAR */ diff --git a/tool/ruby_vm/views/mjit_compile.inc.erb b/tool/ruby_vm/views/mjit_compile.inc.erb index ee29693894..d211c57fdb 100644 --- a/tool/ruby_vm/views/mjit_compile.inc.erb +++ b/tool/ruby_vm/views/mjit_compile.inc.erb @@ -52,6 +52,8 @@ switch (insn) { <%= render 'mjit_compile_send', locals: { insn: insn } -%> % when 'opt_aref' # experimental. TODO: increase insns and make the list automatically by finding DISPATCH_ORIGINAL_INSN <%= render 'mjit_compile_send', locals: { insn: opt_send_without_block } -%> +% when 'getinstancevariable' +<%= render 'mjit_compile_getivar', locals: { insn: insn } -%> % when 'leave' if (b->stack_size != 1) { if (mjit_opts.warnings || mjit_opts.verbose) -- cgit v1.2.3