[前][次][番号順一覧][スレッド一覧]

ruby-changes:61941

From: Takashi <ko1@a...>
Date: Fri, 26 Jun 2020 15:33:41 +0900 (JST)
Subject: [ruby-changes:61941] 7982dc1dfd (master): Decide JIT-ed insn based on cached cfunc

https://git.ruby-lang.org/ruby.git/commit/?id=7982dc1dfd

From 7982dc1dfd5df000b7361ccb7bc820da4f3547b8 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Thu, 25 Jun 2020 23:27:46 -0700
Subject: Decide JIT-ed insn based on cached cfunc

for opt_* insns.

opt_eq handles rb_obj_equal inside opt_eq, and all other cfunc is
handled by opt_send_without_block. Therefore we can't decide which insn
should be generated by checking whether it's cfunc cc or not.

```
$ benchmark-driver -v --rbenv 'before --jit;after --jit' benchmark/mjit_opt_cc_insns.yml --repeat-count=4
before --jit: ruby 2.8.0dev (2020-06-26T05:21:43Z master 9dbc2294a6) +JIT [x86_64-linux]
after --jit: ruby 2.8.0dev (2020-06-26T06:30:18Z master 75cece1b0b) +JIT [x86_64-linux]
last_commit=Decide JIT-ed insn based on cached cfunc
Calculating -------------------------------------
                     before --jit  after --jit
        mjit_nil?(1)      73.878M      74.021M i/s -     40.000M times in 0.541432s 0.540391s
         mjit_not(1)      72.635M      74.601M i/s -     40.000M times in 0.550702s 0.536187s
     mjit_eq(1, nil)       7.331M       7.445M i/s -      8.000M times in 1.091211s 1.074596s
     mjit_eq(nil, 1)      49.450M      64.711M i/s -      8.000M times in 0.161781s 0.123627s

Comparison:
                     mjit_nil?(1)
         after --jit:  74020528.4 i/s
        before --jit:  73878185.9 i/s - 1.00x  slower

                      mjit_not(1)
         after --jit:  74600882.0 i/s
        before --jit:  72634507.6 i/s - 1.03x  slower

                  mjit_eq(1, nil)
         after --jit:   7444657.4 i/s
        before --jit:   7331304.3 i/s - 1.02x  slower

                  mjit_eq(nil, 1)
         after --jit:  64710790.6 i/s
        before --jit:  49449507.4 i/s - 1.31x  slower
```

diff --git a/benchmark/mjit_opt_cc_insns.yml b/benchmark/mjit_opt_cc_insns.yml
index d8738b5..fed6d34 100644
--- a/benchmark/mjit_opt_cc_insns.yml
+++ b/benchmark/mjit_opt_cc_insns.yml
@@ -23,3 +23,5 @@ benchmark: https://github.com/ruby/ruby/blob/trunk/benchmark/mjit_opt_cc_insns.yml#L23
     loop_count: 40000000
   - script: mjit_eq(1, nil)
     loop_count: 8000000
+  - script: mjit_eq(nil, 1)
+    loop_count: 8000000
diff --git a/mjit_compile.c b/mjit_compile.c
index b2e4082..8ceab55 100644
--- a/mjit_compile.c
+++ b/mjit_compile.c
@@ -105,9 +105,11 @@ has_valid_method_type(CALL_CACHE cc) https://github.com/ruby/ruby/blob/trunk/mjit_compile.c#L105
 
 // Returns true if MJIT thinks this cc's opt_* insn may fallback to opt_send_without_block.
 static bool
-has_cache_for_send(CALL_CACHE cc, bool cfunc_cached)
+has_cache_for_send(CALL_CACHE cc, int insn)
 {
-    return has_valid_method_type(cc) && (!cfunc_cached || vm_cc_cme(cc)->def->type != VM_METHOD_TYPE_CFUNC);
+    extern bool rb_vm_opt_cfunc_p(CALL_CACHE cc, int insn);
+    return has_valid_method_type(cc) &&
+        !(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_CFUNC && rb_vm_opt_cfunc_p(cc, insn));
 }
 
 // Returns true if iseq can use fastpath for setup, otherwise NULL. This becomes true in the same condition
diff --git a/tool/ruby_vm/views/mjit_compile.inc.erb b/tool/ruby_vm/views/mjit_compile.inc.erb
index b399b7f..c8f9aca 100644
--- a/tool/ruby_vm/views/mjit_compile.inc.erb
+++ b/tool/ruby_vm/views/mjit_compile.inc.erb
@@ -29,12 +29,6 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/mjit_compile.inc.erb#L29
 %     insn.expr.expr.lines.any? { |l| l.match(/\A\s+CALL_SIMPLE_METHOD\(\);\s+\z/) }
 % end.map(&:name)
 %
-% # These insns cache cfunc in cc under optimized circumstances. They don't generate opt_send when cfunc is cached.
-% cfunc_insns = [
-%   'opt_nil_p',
-%   'opt_not',
-% ]
-%
 % # Available variables and macros in JIT-ed function:
 % #   ec: the first argument of _mjitXXX
 % #   reg_cfp: the second argument of _mjitXXX
@@ -62,7 +56,7 @@ switch (insn) { https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/mjit_compile.inc.erb#L56
 %   when *send_compatible_opt_insns
 %     # To avoid cancel, just emit `opt_send_without_block` instead of `opt_*` insn if call cache is populated.
 %     cd_index = insn.opes.index { |o| o.fetch(:type) == 'CALL_DATA' }
-      if (has_cache_for_send(captured_cc_entries(status)[call_data_index((CALL_DATA)operands[<%= cd_index %>], body)], <%= cfunc_insns.include?(insn.name) %>)) {
+      if (has_cache_for_send(captured_cc_entries(status)[call_data_index((CALL_DATA)operands[<%= cd_index %>], body)], BIN(<%= insn.name %>))) {
 <%=       render 'mjit_compile_send', locals: { insn: opt_send_without_block } -%>
 <%=       render 'mjit_compile_insn', locals: { insn: opt_send_without_block } -%>
           break;
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index af8bf46..5c85d14 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -4903,6 +4903,22 @@ vm_trace_hook(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VAL https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L4903
     }
 }
 
+// Return true if given cc has cfunc which is NOT handled by opt_send_without_block.
+bool
+rb_vm_opt_cfunc_p(CALL_CACHE cc, int insn)
+{
+    switch (insn) {
+      case BIN(opt_eq):
+        return check_cfunc(vm_cc_cme(cc), rb_obj_equal);
+      case BIN(opt_nil_p):
+        return check_cfunc(vm_cc_cme(cc), rb_false);
+      case BIN(opt_not):
+        return check_cfunc(vm_cc_cme(cc), rb_obj_not);
+      default:
+        return false;
+    }
+}
+
 #define VM_TRACE_HOOK(target_event, val) do { \
     if ((pc_events & (target_event)) & enabled_flags) { \
         vm_trace_hook(ec, reg_cfp, pc, pc_events, (target_event), global_hooks, local_hooks, (val)); \
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]