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

ruby-changes:61894

From: Takashi <ko1@a...>
Date: Mon, 22 Jun 2020 18:08:44 +0900 (JST)
Subject: [ruby-changes:61894] 78352fb52e (master): Compile opt_send for opt_* only when cc has ISeq

https://git.ruby-lang.org/ruby.git/commit/?id=78352fb52e

From 78352fb52ed2c15fe0d74c02ea29b7e5a28b18c0 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Mon, 22 Jun 2020 01:44:11 -0700
Subject: Compile opt_send for opt_* only when cc has ISeq

because opt_nil/opt_not/opt_eq populates cc even when it doesn't
fallback to opt_send_without_block because of vm_method_cfunc_is.

```
$ 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-22T08:11:24Z master d231b8f95b) +JIT [x86_64-linux]
after --jit: ruby 2.8.0dev (2020-06-22T08:53:27Z master e1125879ed) +JIT [x86_64-linux]
last_commit=Compile opt_send for opt_* only when cc has ISeq
Calculating -------------------------------------
                     before --jit  after --jit
        mjit_nil?(1)      54.106M      73.693M i/s -     40.000M times in 0.739288s 0.542795s
         mjit_not(1)      53.398M      74.477M i/s -     40.000M times in 0.749090s 0.537075s
     mjit_eq(1, nil)       7.427M       6.497M i/s -      8.000M times in 1.077136s 1.231326s

Comparison:
                     mjit_nil?(1)
         after --jit:  73692594.3 i/s
        before --jit:  54106108.4 i/s - 1.36x  slower

                      mjit_not(1)
         after --jit:  74477487.9 i/s
        before --jit:  53398125.0 i/s - 1.39x  slower

                  mjit_eq(1, nil)
        before --jit:   7427105.9 i/s
         after --jit:   6497063.0 i/s - 1.14x  slower
```

Actually opt_eq becomes slower by this. Maybe it's indeed using
opt_send_without_block, but I'll approach that one in another commit.

diff --git a/benchmark/mjit_opt_cc_insns.yml b/benchmark/mjit_opt_cc_insns.yml
new file mode 100644
index 0000000..d8738b5
--- /dev/null
+++ b/benchmark/mjit_opt_cc_insns.yml
@@ -0,0 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/mjit_opt_cc_insns.yml#L1
+# opt_* insns using vm_method_cfunc_is with send-compatible operands:
+#   * opt_nil_p
+#   * opt_not
+#   * opt_eq
+type: lib/benchmark_driver/runner/mjit
+prelude: |
+  def mjit_nil?(obj)
+    obj.nil?
+  end
+
+  def mjit_not(obj)
+    !obj
+  end
+
+  def mjit_eq(a, b)
+    a == b
+  end
+
+benchmark:
+  - script: mjit_nil?(1)
+    loop_count: 40000000
+  - script: mjit_not(1)
+    loop_count: 40000000
+  - script: mjit_eq(1, nil)
+    loop_count: 8000000
diff --git a/mjit_compile.c b/mjit_compile.c
index c4c31aa..ed1cd1f 100644
--- a/mjit_compile.c
+++ b/mjit_compile.c
@@ -97,9 +97,9 @@ captured_cc_entries(const struct compile_status *status) https://github.com/ruby/ruby/blob/trunk/mjit_compile.c#L97
 
 // Returns true if call cache is still not obsoleted and vm_cc_cme(cc)->def->type is available.
 static bool
-has_valid_method_type(CALL_CACHE cc)
+has_valid_method_type(CALL_CACHE cc, rb_method_type_t type)
 {
-    return vm_cc_cme(cc) != NULL;
+    return vm_cc_cme(cc) != NULL && vm_cc_cme(cc)->def->type == type;
 }
 
 // Returns true if iseq can use fastpath for setup, otherwise NULL. This becomes true in the same condition
@@ -439,9 +439,8 @@ precompile_inlinable_iseqs(FILE *f, const rb_iseq_t *iseq, struct compile_status https://github.com/ruby/ruby/blob/trunk/mjit_compile.c#L439
             const struct rb_callcache *cc = captured_cc_entries(status)[call_data_index(cd, body)]; // use copy to avoid race condition
 
             const rb_iseq_t *child_iseq;
-            if (has_valid_method_type(cc) &&
+            if (has_valid_method_type(cc, VM_METHOD_TYPE_ISEQ) &&
                 !(vm_ci_flag(ci) & VM_CALL_TAILCALL) && // inlining only non-tailcall path
-                vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_ISEQ &&
                 fastpath_applied_iseq_p(ci, cc, child_iseq = def_iseq_ptr(vm_cc_cme(cc)->def)) &&
                 // CC_SET_FASTPATH in vm_callee_setup_arg
                 inlinable_iseq_p(child_iseq->body)) {
diff --git a/tool/ruby_vm/views/_mjit_compile_send.erb b/tool/ruby_vm/views/_mjit_compile_send.erb
index 8c1c1c0..71efae9 100644
--- a/tool/ruby_vm/views/_mjit_compile_send.erb
+++ b/tool/ruby_vm/views/_mjit_compile_send.erb
@@ -20,13 +20,13 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_mjit_compile_send.erb#L20
     const CALL_INFO ci = cd->ci;
     int kw_splat = IS_ARGS_KW_SPLAT(ci) > 0;
     extern bool rb_splat_or_kwargs_p(const struct rb_callinfo *restrict ci);
-    if (!status->compile_info->disable_send_cache && has_valid_method_type(captured_cc) && (
+    if (!status->compile_info->disable_send_cache && (
 %       # `CC_SET_FASTPATH(cd->cc, vm_call_cfunc_with_frame, ...)` in `vm_call_cfunc`
-        (vm_cc_cme(captured_cc)->def->type == VM_METHOD_TYPE_CFUNC
+        (has_valid_method_type(captured_cc, VM_METHOD_TYPE_CFUNC)
          && !rb_splat_or_kwargs_p(ci) && !kw_splat)
 %       # `CC_SET_FASTPATH(cc, vm_call_iseq_setup_func(...), vm_call_iseq_optimizable_p(...))` in `vm_callee_setup_arg`,
 %       # and support only non-VM_CALL_TAILCALL path inside it
-        || (vm_cc_cme(captured_cc)->def->type == VM_METHOD_TYPE_ISEQ
+        || (has_valid_method_type(captured_cc, VM_METHOD_TYPE_ISEQ)
             && fastpath_applied_iseq_p(ci, captured_cc, iseq = def_iseq_ptr(vm_cc_cme(captured_cc)->def))
             && !(vm_ci_flag(ci) & VM_CALL_TAILCALL))
     )) {
diff --git a/tool/ruby_vm/views/mjit_compile.inc.erb b/tool/ruby_vm/views/mjit_compile.inc.erb
index fa2e52e..ea145e5 100644
--- a/tool/ruby_vm/views/mjit_compile.inc.erb
+++ b/tool/ruby_vm/views/mjit_compile.inc.erb
@@ -56,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_valid_method_type(captured_cc_entries(status)[call_data_index((CALL_DATA)operands[<%= cd_index %>], body)])) {
+      if (has_valid_method_type(captured_cc_entries(status)[call_data_index((CALL_DATA)operands[<%= cd_index %>], body)], VM_METHOD_TYPE_ISEQ)) {
 <%=       render 'mjit_compile_send', locals: { insn: opt_send_without_block } -%>
 <%=       render 'mjit_compile_insn', locals: { insn: opt_send_without_block } -%>
           break;
-- 
cgit v0.10.2


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

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