diff --git a/common.mk b/common.mk index 84ebfbd303..acb3d7ff4d 100644 --- a/common.mk +++ b/common.mk @@ -930,6 +930,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_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/test/ruby/test_jit.rb b/test/ruby/test_jit.rb index 4699de7d2a..891b7f496f 100644 --- a/test/ruby/test_jit.rb +++ b/test/ruby/test_jit.rb @@ -612,6 +612,37 @@ class TestJIT < Test::Unit::TestCase print(2 * a.test) end; + + assert_eval_with_jit("#{<<~"begin;"}\n#{<<~"end;"}", stdout: "true", success_count: 1, min_calls: 2) + begin; + class Hoge + attr_reader :foo + + def initialize + @foo = [] + @bar = nil + end + end + + class Fuga < Hoge + def initialize + @bar = nil + @foo = [] + end + end + + def test(recv) + recv.foo.empty? + end + + hoge = Hoge.new + fuga = Fuga.new + + test(hoge) # VM: cc set index=1 + test(hoge) # JIT: compile with index=1 + test(fuga) # JIT -> VM: cc set index=2 + print test(hoge) # JIT: should use index=1, not index=2 in cc + end; end def test_clean_so diff --git a/tool/ruby_vm/views/_mjit_compile_send.erb b/tool/ruby_vm/views/_mjit_compile_send.erb index 9b8225c8c5..cb7c1f40f8 100644 --- a/tool/ruby_vm/views/_mjit_compile_send.erb +++ b/tool/ruby_vm/views/_mjit_compile_send.erb @@ -20,7 +20,7 @@ argc += ((ci->flag & VM_CALL_ARGS_BLOCKARG) ? 1 : 0); % end - if (cc->me->def->type == VM_METHOD_TYPE_ISEQ && inlinable_iseq_p(ci, cc, iseq = rb_iseq_check(cc->me->def->body.iseq.iseqptr))) { + if (cc->me->def->type == VM_METHOD_TYPE_ISEQ && inlinable_iseq_p(ci, cc, iseq = rb_iseq_check(cc->me->def->body.iseq.iseqptr))) { /* CI_SET_FASTPATH in vm_callee_setup_arg */ int param_size = iseq->body->param.size; /* TODO: check calling->argc for argument_arity_error */ fprintf(f, "{\n"); @@ -29,16 +29,12 @@ fprintf(f, " MAYBE_UNUSED(unsigned int) stack_size = %u;\n", b->stack_size); } +% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things. +<%= render 'mjit_compile_send_guard' -%> + % # JIT: move sp and pc if necessary <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%> -% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things. - fprintf(f, " if (UNLIKELY(GET_GLOBAL_METHOD_STATE() != %"PRI_SERIALT_PREFIX"u ||\n", cc->method_state); - fprintf(f, " RCLASS_SERIAL(CLASS_OF(stack[%d])) != %"PRI_SERIALT_PREFIX"u)) {\n", b->stack_size - 1 - argc, cc->class_serial); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " goto cancel;\n"); - fprintf(f, " }\n"); - % # JIT: Print insn body in insns.def fprintf(f, " {\n"); fprintf(f, " struct rb_calling_info calling;\n"); @@ -88,5 +84,19 @@ fprintf(f, "}\n"); break; } +% if insn.name == 'opt_send_without_block' + else if (cc->me->def->type == VM_METHOD_TYPE_IVAR && !(ci->flag & VM_CALL_ARGS_SPLAT)) { /* CI_SET_FASTPATH with vm_call_ivar */ +% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things. +<%= render 'mjit_compile_send_guard' -%> + +% # JIT: vm_call_ivar without sp motion + fprintf(f, " stack[%d] = vm_getivar(stack[%d], (ID)0x%"PRIxVALUE", NULL, (CALL_CACHE)0x%"PRIxVALUE", (st_index_t)%ld, 1);\n", + b->stack_size - argc - 1, b->stack_size - argc - 1, cc->me->def->body.attr.id, (VALUE)cc, (long)cc->aux.index); + +% # compiler: Move JIT compiler's internal stack pointer + b->stack_size += <%= insn.call_attribute('sp_inc') %>; + break; + } +% end } } diff --git a/tool/ruby_vm/views/_mjit_compile_send_guard.erb b/tool/ruby_vm/views/_mjit_compile_send_guard.erb new file mode 100644 index 0000000000..7bdb2a3ebf --- /dev/null +++ b/tool/ruby_vm/views/_mjit_compile_send_guard.erb @@ -0,0 +1,14 @@ +% # 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. +% +% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things. + fprintf(f, " if (UNLIKELY(GET_GLOBAL_METHOD_STATE() != %"PRI_SERIALT_PREFIX"u ||\n", cc->method_state); + fprintf(f, " RCLASS_SERIAL(CLASS_OF(stack[%d])) != %"PRI_SERIALT_PREFIX"u)) {\n", b->stack_size - 1 - argc, cc->class_serial); + 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"); diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 0f365db3f5..57e5499e4e 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -927,24 +927,29 @@ vm_search_const_defined_class(const VALUE cbase, ID id) #define USE_IC_FOR_IVAR 1 #endif -ALWAYS_INLINE(static VALUE vm_getivar(VALUE, ID, IC, struct rb_call_cache *, int)); +/* `index` argument is used in MJIT to inline index value of call cache */ +ALWAYS_INLINE(static VALUE vm_getivar(VALUE, ID, IC, struct rb_call_cache *, st_index_t, int)); static inline VALUE -vm_getivar(VALUE obj, ID id, IC ic, struct rb_call_cache *cc, int is_attr) +vm_getivar(VALUE obj, ID id, IC ic, struct rb_call_cache *cc, st_index_t index, int is_attr) { #if USE_IC_FOR_IVAR if (LIKELY(RB_TYPE_P(obj, T_OBJECT))) { VALUE val = Qundef; - if (LIKELY(is_attr ? + if (LIKELY(index > 0 || is_attr ? RB_DEBUG_COUNTER_INC_UNLESS(ivar_get_ic_miss_unset, cc->aux.index > 0) : RB_DEBUG_COUNTER_INC_UNLESS(ivar_get_ic_miss_serial, ic->ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass)))) { - st_index_t index = !is_attr ? ic->ic_value.index : (cc->aux.index - 1); + if (index == 0) { + index = !is_attr ? ic->ic_value.index : (cc->aux.index - 1); + } + else { + index--; /* MJIT passes `cc->aux.index` as `index`. This will be `cc->aux.index - 1` */ + } if (LIKELY(index < ROBJECT_NUMIV(obj))) { val = ROBJECT_IVPTR(obj)[index]; } } else { - st_data_t index; struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj); if (iv_index_tbl) { @@ -1032,7 +1037,7 @@ vm_setivar(VALUE obj, ID id, VALUE val, IC ic, struct rb_call_cache *cc, int is_ static inline VALUE vm_getinstancevariable(VALUE obj, ID id, IC ic) { - return vm_getivar(obj, id, ic, 0, 0); + return vm_getivar(obj, id, ic, NULL, 0, FALSE); } static inline void @@ -1944,7 +1949,7 @@ static VALUE vm_call_ivar(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc) { cfp->sp -= 1; - return vm_getivar(calling->recv, cc->me->def->body.attr.id, NULL, cc, 1); + return vm_getivar(calling->recv, cc->me->def->body.attr.id, NULL, cc, 0, TRUE); } static VALUE