2018-12-27 01:12:09 -05:00
|
|
|
% # -*- C -*-
|
2018-02-12 01:54:25 -05:00
|
|
|
% # 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.
|
|
|
|
%
|
|
|
|
% to_cstr = lambda do |line|
|
|
|
|
% normalized = line.gsub(/\t/, ' ' * 8)
|
|
|
|
% indented = normalized.sub(/\A(?!#)/, ' ') # avoid indenting preprocessor
|
|
|
|
% rstring2cstr(indented.rstrip).sub(/"\z/, '\\n"')
|
|
|
|
% end
|
|
|
|
%
|
2018-02-12 02:27:48 -05:00
|
|
|
% #
|
|
|
|
% # Expand simple macro, which doesn't require dynamic C code.
|
|
|
|
% #
|
|
|
|
% expand_simple_macros = lambda do |arg_expr|
|
|
|
|
% arg_expr.dup.tap do |expr|
|
|
|
|
% # For `leave`. We can't proceed next ISeq in the same JIT function.
|
|
|
|
% expr.gsub!(/^(?<indent>\s*)RESTORE_REGS\(\);\n/) do
|
|
|
|
% indent = Regexp.last_match[:indent]
|
2018-02-12 04:02:30 -05:00
|
|
|
% <<-RESTORE_REGS.gsub(/^ +/, '')
|
2018-02-12 02:27:48 -05:00
|
|
|
% #if OPT_CALL_THREADED_CODE
|
|
|
|
% #{indent}rb_ec_thread_ptr(ec)->retval = val;
|
|
|
|
% #{indent}return 0;
|
|
|
|
% #else
|
|
|
|
% #{indent}return val;
|
|
|
|
% #endif
|
|
|
|
% RESTORE_REGS
|
|
|
|
% end
|
2018-12-25 19:59:37 -05:00
|
|
|
% expr.gsub!(/^(?<indent>\s*)NEXT_INSN\(\);\n/) do
|
|
|
|
% indent = Regexp.last_match[:indent]
|
|
|
|
% <<-end.gsub(/^ +/, '')
|
|
|
|
% #{indent}UNREACHABLE_RETURN(Qundef);
|
|
|
|
% end
|
|
|
|
% end
|
2018-02-12 01:54:25 -05:00
|
|
|
% end
|
2018-02-12 02:27:48 -05:00
|
|
|
% end
|
|
|
|
%
|
|
|
|
% #
|
|
|
|
% # Print a body of insn, but with macro expansion.
|
|
|
|
% #
|
|
|
|
% expand_simple_macros.call(insn.expr.expr).each_line do |line|
|
|
|
|
% #
|
|
|
|
% # Expand dynamic macro here (only JUMP for now)
|
|
|
|
% #
|
mjit_compile.c: use local variables for stack
if catch_except_p is FALSE. If catch_except_p is TRUE, stack values
should be on VM's stack when exception is thrown and the JIT-ed frame
is re-executed by VM's exception handler. If it's FALSE, the JIT-ed
frame won't be re-executed and don't need to keep values on VM's stack.
Using local variables allows us to reduce cfp->sp motion. Moving cfp->sp
is needed only for insns whose handles_frame? is false. So it improves
performance.
_mjit_compile_insn.erb: Prepare `stack_size` variable for GET_SP,
STACK_ADDR_FROM_TOP, TOPN macros. Share pc and sp motion partial view.
Use cancel handler created in mjit_compile.c.
_mjit_compile_send.erb: ditto. Also, when iseq->body->catch_except_p is
TRUE, this stops to call mjit_exec directly. I described the reason in
vm_insnhelper.h's comment for EXEC_EC_CFP.
_mjit_compile_pc_and_sp.erb: Shared logic for moving sp and pc. As you
can see from thsi file, when status->local_stack_p is TRUE and
insn.handles_frame? is false, moving sp is skipped. But if
insn.handles_frame? is true, values should be rolled back to VM's stack.
common.mk: add dependency for the file
_mjit_compile_insn_body.erb: Set sp value before canceling JIT on
DISPATCH_ORIGINAL_INSN. Replace GET_SP, STACK_ADDR_FROM_TOP, TOPN macros
for the case ocal_stack_p is TRUE and insn.handles_frame? is false.
In that case, values are not available on VM's stack and those macros
should be replaced.
mjit_compile.inc.erb: updated comments of macros which are supported by
JIT compiler. All references to `cfp->sp` should be replaced and thus
INC_SP, SET_SV, PUSH are no longer supported for now, because they are
not used now.
vm_exec.h: moved EXEC_EC_CFP definition to vm_insnhelper.h because it's
tighly coupled to CALL_METHOD.
vm_insnhelper.h: Have revised EXEC_EC_CFP definition moved from vm_exec.h.
Now it triggers mjit_exec for VM, and has the guard for catch_except_p
on JIT-ed code. See comments for details. CALL_METHOD delegates
triggering mjit_exec to EXEC_EC_CFP.
insns.def: Stopped using EXEC_EC_CFP for the case we don't want to
trigger mjit_exec. Those insns (defineclass, opt_call_c_function) are
not supported by JIT and it's safe to use RESTORE_REGS(), NEXT_INSN().
expandarray is changed to pass GET_SP() to replace the macro in
_mjit_compile_insn_body.erb.
vm_insnhelper.c: change to take sp for the above reason.
[close https://github.com/ruby/ruby/pull/1828]
This patch resurrects the performance which was attached in
[Feature #14235].
* Benchmark
Optcarrot (with configuration for benchmark_driver.gem)
https://github.com/benchmark-driver/optcarrot
$ benchmark-driver benchmark.yml --verbose 1 --rbenv 'before;before+JIT::before,--jit;after;after+JIT::after,--jit' --repeat-count 10
before: ruby 2.6.0dev (2018-03-04 trunk 62652) [x86_64-linux]
before+JIT: ruby 2.6.0dev (2018-03-04 trunk 62652) +JIT [x86_64-linux]
after: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
after+JIT: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) +JIT [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
Calculating -------------------------------------
before before+JIT after after+JIT
optcarrot 53.552 59.680 53.697 63.358 fps
Comparison:
optcarrot
after+JIT: 63.4 fps
before+JIT: 59.7 fps - 1.06x slower
after: 53.7 fps - 1.18x slower
before: 53.6 fps - 1.18x slower
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62655 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-04 02:04:40 -05:00
|
|
|
% # TODO: support combination of following macros in the same line
|
|
|
|
% case line
|
|
|
|
% when /\A\s+JUMP\((?<dest>[^)]+)\);\s+\z/
|
2018-02-12 02:27:48 -05:00
|
|
|
% dest = Regexp.last_match[:dest]
|
2018-02-12 01:54:25 -05:00
|
|
|
%
|
2018-02-12 02:27:48 -05:00
|
|
|
% if insn.name == 'opt_case_dispatch' # special case... TODO: use another macro to avoid checking name
|
2018-02-12 01:54:25 -05:00
|
|
|
{
|
|
|
|
struct case_dispatch_var arg;
|
|
|
|
arg.f = f;
|
|
|
|
arg.base_pos = pos + insn_len(insn);
|
|
|
|
arg.last_value = Qundef;
|
|
|
|
|
|
|
|
fprintf(f, " switch (<%= dest %>) {\n");
|
|
|
|
st_foreach(RHASH_TBL_RAW(hash), compile_case_dispatch_each, (VALUE)&arg);
|
|
|
|
fprintf(f, " case %lu:\n", else_offset);
|
|
|
|
fprintf(f, " goto label_%lu;\n", arg.base_pos + else_offset);
|
|
|
|
fprintf(f, " }\n");
|
|
|
|
}
|
2018-02-12 02:27:48 -05:00
|
|
|
% else
|
|
|
|
% # Before we `goto` next insn, we need to set return values, especially for getinlinecache
|
|
|
|
% insn.rets.reverse_each.with_index do |ret, i|
|
|
|
|
% # TOPN(n) = ...
|
|
|
|
fprintf(f, " stack[%d] = <%= ret.fetch(:name) %>;\n", b->stack_size + (int)<%= insn.call_attribute('sp_inc') %> - <%= i + 1 %>);
|
|
|
|
% end
|
|
|
|
%
|
2018-02-12 01:54:25 -05:00
|
|
|
next_pos = pos + insn_len(insn) + (unsigned int)<%= dest %>;
|
|
|
|
fprintf(f, " goto label_%d;\n", next_pos);
|
2018-02-12 02:27:48 -05:00
|
|
|
% end
|
2018-09-14 03:44:44 -04:00
|
|
|
% when /\A\s+CALL_SIMPLE_METHOD\(\);\s+\z/
|
mjit_compile.c: use local variables for stack
if catch_except_p is FALSE. If catch_except_p is TRUE, stack values
should be on VM's stack when exception is thrown and the JIT-ed frame
is re-executed by VM's exception handler. If it's FALSE, the JIT-ed
frame won't be re-executed and don't need to keep values on VM's stack.
Using local variables allows us to reduce cfp->sp motion. Moving cfp->sp
is needed only for insns whose handles_frame? is false. So it improves
performance.
_mjit_compile_insn.erb: Prepare `stack_size` variable for GET_SP,
STACK_ADDR_FROM_TOP, TOPN macros. Share pc and sp motion partial view.
Use cancel handler created in mjit_compile.c.
_mjit_compile_send.erb: ditto. Also, when iseq->body->catch_except_p is
TRUE, this stops to call mjit_exec directly. I described the reason in
vm_insnhelper.h's comment for EXEC_EC_CFP.
_mjit_compile_pc_and_sp.erb: Shared logic for moving sp and pc. As you
can see from thsi file, when status->local_stack_p is TRUE and
insn.handles_frame? is false, moving sp is skipped. But if
insn.handles_frame? is true, values should be rolled back to VM's stack.
common.mk: add dependency for the file
_mjit_compile_insn_body.erb: Set sp value before canceling JIT on
DISPATCH_ORIGINAL_INSN. Replace GET_SP, STACK_ADDR_FROM_TOP, TOPN macros
for the case ocal_stack_p is TRUE and insn.handles_frame? is false.
In that case, values are not available on VM's stack and those macros
should be replaced.
mjit_compile.inc.erb: updated comments of macros which are supported by
JIT compiler. All references to `cfp->sp` should be replaced and thus
INC_SP, SET_SV, PUSH are no longer supported for now, because they are
not used now.
vm_exec.h: moved EXEC_EC_CFP definition to vm_insnhelper.h because it's
tighly coupled to CALL_METHOD.
vm_insnhelper.h: Have revised EXEC_EC_CFP definition moved from vm_exec.h.
Now it triggers mjit_exec for VM, and has the guard for catch_except_p
on JIT-ed code. See comments for details. CALL_METHOD delegates
triggering mjit_exec to EXEC_EC_CFP.
insns.def: Stopped using EXEC_EC_CFP for the case we don't want to
trigger mjit_exec. Those insns (defineclass, opt_call_c_function) are
not supported by JIT and it's safe to use RESTORE_REGS(), NEXT_INSN().
expandarray is changed to pass GET_SP() to replace the macro in
_mjit_compile_insn_body.erb.
vm_insnhelper.c: change to take sp for the above reason.
[close https://github.com/ruby/ruby/pull/1828]
This patch resurrects the performance which was attached in
[Feature #14235].
* Benchmark
Optcarrot (with configuration for benchmark_driver.gem)
https://github.com/benchmark-driver/optcarrot
$ benchmark-driver benchmark.yml --verbose 1 --rbenv 'before;before+JIT::before,--jit;after;after+JIT::after,--jit' --repeat-count 10
before: ruby 2.6.0dev (2018-03-04 trunk 62652) [x86_64-linux]
before+JIT: ruby 2.6.0dev (2018-03-04 trunk 62652) +JIT [x86_64-linux]
after: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
after+JIT: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) +JIT [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
Calculating -------------------------------------
before before+JIT after after+JIT
optcarrot 53.552 59.680 53.697 63.358 fps
Comparison:
optcarrot
after+JIT: 63.4 fps
before+JIT: 59.7 fps - 1.06x slower
after: 53.7 fps - 1.18x slower
before: 53.6 fps - 1.18x slower
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62655 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-04 02:04:40 -05:00
|
|
|
% # For `opt_xxx`'s fallbacks.
|
2018-07-19 09:25:22 -04:00
|
|
|
if (status->local_stack_p) {
|
2019-03-25 10:26:11 -04:00
|
|
|
fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size);
|
2018-07-19 09:25:22 -04:00
|
|
|
}
|
2018-07-07 10:28:34 -04:00
|
|
|
fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos);
|
2019-03-29 09:54:29 -04:00
|
|
|
fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_opt_insn);\n");
|
mjit_compile.c: use local variables for stack
if catch_except_p is FALSE. If catch_except_p is TRUE, stack values
should be on VM's stack when exception is thrown and the JIT-ed frame
is re-executed by VM's exception handler. If it's FALSE, the JIT-ed
frame won't be re-executed and don't need to keep values on VM's stack.
Using local variables allows us to reduce cfp->sp motion. Moving cfp->sp
is needed only for insns whose handles_frame? is false. So it improves
performance.
_mjit_compile_insn.erb: Prepare `stack_size` variable for GET_SP,
STACK_ADDR_FROM_TOP, TOPN macros. Share pc and sp motion partial view.
Use cancel handler created in mjit_compile.c.
_mjit_compile_send.erb: ditto. Also, when iseq->body->catch_except_p is
TRUE, this stops to call mjit_exec directly. I described the reason in
vm_insnhelper.h's comment for EXEC_EC_CFP.
_mjit_compile_pc_and_sp.erb: Shared logic for moving sp and pc. As you
can see from thsi file, when status->local_stack_p is TRUE and
insn.handles_frame? is false, moving sp is skipped. But if
insn.handles_frame? is true, values should be rolled back to VM's stack.
common.mk: add dependency for the file
_mjit_compile_insn_body.erb: Set sp value before canceling JIT on
DISPATCH_ORIGINAL_INSN. Replace GET_SP, STACK_ADDR_FROM_TOP, TOPN macros
for the case ocal_stack_p is TRUE and insn.handles_frame? is false.
In that case, values are not available on VM's stack and those macros
should be replaced.
mjit_compile.inc.erb: updated comments of macros which are supported by
JIT compiler. All references to `cfp->sp` should be replaced and thus
INC_SP, SET_SV, PUSH are no longer supported for now, because they are
not used now.
vm_exec.h: moved EXEC_EC_CFP definition to vm_insnhelper.h because it's
tighly coupled to CALL_METHOD.
vm_insnhelper.h: Have revised EXEC_EC_CFP definition moved from vm_exec.h.
Now it triggers mjit_exec for VM, and has the guard for catch_except_p
on JIT-ed code. See comments for details. CALL_METHOD delegates
triggering mjit_exec to EXEC_EC_CFP.
insns.def: Stopped using EXEC_EC_CFP for the case we don't want to
trigger mjit_exec. Those insns (defineclass, opt_call_c_function) are
not supported by JIT and it's safe to use RESTORE_REGS(), NEXT_INSN().
expandarray is changed to pass GET_SP() to replace the macro in
_mjit_compile_insn_body.erb.
vm_insnhelper.c: change to take sp for the above reason.
[close https://github.com/ruby/ruby/pull/1828]
This patch resurrects the performance which was attached in
[Feature #14235].
* Benchmark
Optcarrot (with configuration for benchmark_driver.gem)
https://github.com/benchmark-driver/optcarrot
$ benchmark-driver benchmark.yml --verbose 1 --rbenv 'before;before+JIT::before,--jit;after;after+JIT::after,--jit' --repeat-count 10
before: ruby 2.6.0dev (2018-03-04 trunk 62652) [x86_64-linux]
before+JIT: ruby 2.6.0dev (2018-03-04 trunk 62652) +JIT [x86_64-linux]
after: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
after+JIT: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) +JIT [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
Calculating -------------------------------------
before before+JIT after after+JIT
optcarrot 53.552 59.680 53.697 63.358 fps
Comparison:
optcarrot
after+JIT: 63.4 fps
before+JIT: 59.7 fps - 1.06x slower
after: 53.7 fps - 1.18x slower
before: 53.6 fps - 1.18x slower
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62655 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-04 02:04:40 -05:00
|
|
|
fprintf(f, " goto cancel;\n");
|
2018-02-12 02:27:48 -05:00
|
|
|
% else
|
2018-07-25 10:55:43 -04:00
|
|
|
% if insn.handles_sp?
|
|
|
|
% # If insn.handles_sp? is true, cfp->sp might be changed inside insns (like vm_caller_setup_arg_block)
|
|
|
|
% # and thus we need to use cfp->sp, even when local_stack_p is TRUE. When insn.handles_sp? is true,
|
2018-07-19 09:25:22 -04:00
|
|
|
% # cfp->sp should be available too because _mjit_compile_pc_and_sp.erb sets it.
|
|
|
|
fprintf(f, <%= to_cstr.call(line) %>);
|
|
|
|
% else
|
2018-07-25 10:55:43 -04:00
|
|
|
% # If local_stack_p is TRUE and insn.handles_sp? is false, stack values are only available in local variables
|
2018-07-19 09:25:22 -04:00
|
|
|
% # for stack. So we need to replace those macros if local_stack_p is TRUE here.
|
|
|
|
% case line
|
|
|
|
% when /\bGET_SP\(\)/
|
|
|
|
% # reg_cfp->sp
|
|
|
|
fprintf(f, <%= to_cstr.call(line.sub(/\bGET_SP\(\)/, '%s')) %>, (status->local_stack_p ? "(stack + stack_size)" : "GET_SP()"));
|
|
|
|
% when /\bSTACK_ADDR_FROM_TOP\((?<num>[^)]+)\)/
|
|
|
|
% # #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
|
|
|
|
% num = Regexp.last_match[:num]
|
|
|
|
fprintf(f, <%= to_cstr.call(line.sub(/\bSTACK_ADDR_FROM_TOP\(([^)]+)\)/, '%s')) %>,
|
|
|
|
(status->local_stack_p ? "stack + (stack_size - (<%= num %>))" : "STACK_ADDR_FROM_TOP(<%= num %>)"));
|
|
|
|
% when /\bTOPN\((?<num>[^)]+)\)/
|
|
|
|
% # #define TOPN(n) (*(GET_SP()-(n)-1))
|
|
|
|
% num = Regexp.last_match[:num]
|
|
|
|
fprintf(f, <%= to_cstr.call(line.sub(/\bTOPN\(([^)]+)\)/, '%s')) %>,
|
|
|
|
(status->local_stack_p ? "*(stack + (stack_size - (<%= num %>) - 1))" : "TOPN(<%= num %>)"));
|
|
|
|
% else
|
2018-02-12 01:54:25 -05:00
|
|
|
fprintf(f, <%= to_cstr.call(line) %>);
|
2018-07-19 09:25:22 -04:00
|
|
|
% end
|
|
|
|
% end
|
2018-02-12 02:27:48 -05:00
|
|
|
% end
|
2018-02-12 01:54:25 -05:00
|
|
|
% end
|