mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
2023-03-07 10:17:25 +03:00
|
|
|
rjit_c.c - C helpers for RJIT
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
|
2022-12-09 09:38:55 +03:00
|
|
|
Copyright (C) 2017 Takashi Kokubun <k0kubun@ruby-lang.org>.
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2023-03-09 10:07:30 +03:00
|
|
|
#include "rjit.h" // defines USE_RJIT
|
2023-03-06 01:15:19 +03:00
|
|
|
|
2023-03-07 10:15:30 +03:00
|
|
|
#if USE_RJIT
|
2018-10-20 09:53:00 +03:00
|
|
|
|
2023-03-07 10:17:25 +03:00
|
|
|
#include "rjit_c.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/compile.h"
|
2023-02-14 09:36:02 +03:00
|
|
|
#include "internal/fixnum.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/hash.h"
|
2023-02-11 01:41:45 +03:00
|
|
|
#include "internal/sanitizers.h"
|
|
|
|
#include "internal/gc.h"
|
2022-08-13 18:13:24 +03:00
|
|
|
#include "yjit.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "vm_insnhelper.h"
|
2023-03-08 09:43:37 +03:00
|
|
|
#include "probes.h"
|
|
|
|
#include "probes_helper.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 14:22:28 +03:00
|
|
|
#include "insns.inc"
|
|
|
|
#include "insns_info.inc"
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
|
2023-03-08 09:43:37 +03:00
|
|
|
// For mmapp(), sysconf()
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2023-03-10 22:55:48 +03:00
|
|
|
#if defined(MAP_FIXED_NOREPLACE) && defined(_SC_PAGESIZE)
|
|
|
|
// Align the current write position to a multiple of bytes
|
|
|
|
static uint8_t *
|
|
|
|
align_ptr(uint8_t *ptr, uint32_t multiple)
|
2023-03-08 09:43:37 +03:00
|
|
|
{
|
2023-03-10 22:55:48 +03:00
|
|
|
// Compute the pointer modulo the given alignment boundary
|
|
|
|
uint32_t rem = ((uint32_t)(uintptr_t)ptr) % multiple;
|
|
|
|
|
|
|
|
// If the pointer is already aligned, stop
|
|
|
|
if (rem == 0)
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
// Pad the pointer by the necessary amount to align it
|
|
|
|
uint32_t pad = multiple - rem;
|
|
|
|
|
|
|
|
return ptr + pad;
|
2023-03-08 09:43:37 +03:00
|
|
|
}
|
2023-03-10 22:55:48 +03:00
|
|
|
#endif
|
2023-03-08 09:43:37 +03:00
|
|
|
|
2023-03-10 22:55:48 +03:00
|
|
|
// Address space reservation. Memory pages are mapped on an as needed basis.
|
|
|
|
// See the Rust mm module for details.
|
|
|
|
static uint8_t *
|
|
|
|
rjit_reserve_addr_space(uint32_t mem_size)
|
2023-03-08 09:43:37 +03:00
|
|
|
{
|
2023-03-10 22:55:48 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
uint8_t *mem_block;
|
|
|
|
|
|
|
|
// On Linux
|
|
|
|
#if defined(MAP_FIXED_NOREPLACE) && defined(_SC_PAGESIZE)
|
|
|
|
uint32_t const page_size = (uint32_t)sysconf(_SC_PAGESIZE);
|
|
|
|
uint8_t *const cfunc_sample_addr = (void *)&rjit_reserve_addr_space;
|
|
|
|
uint8_t *const probe_region_end = cfunc_sample_addr + INT32_MAX;
|
|
|
|
// Align the requested address to page size
|
|
|
|
uint8_t *req_addr = align_ptr(cfunc_sample_addr, page_size);
|
|
|
|
|
|
|
|
// Probe for addresses close to this function using MAP_FIXED_NOREPLACE
|
|
|
|
// to improve odds of being in range for 32-bit relative call instructions.
|
|
|
|
do {
|
|
|
|
mem_block = mmap(
|
|
|
|
req_addr,
|
|
|
|
mem_size,
|
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
|
|
|
|
-1,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
// If we succeeded, stop
|
|
|
|
if (mem_block != MAP_FAILED) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// +4MB
|
|
|
|
req_addr += 4 * 1024 * 1024;
|
|
|
|
} while (req_addr < probe_region_end);
|
|
|
|
|
|
|
|
// On MacOS and other platforms
|
|
|
|
#else
|
|
|
|
// Try to map a chunk of memory as executable
|
|
|
|
mem_block = mmap(
|
|
|
|
(void *)rjit_reserve_addr_space,
|
|
|
|
mem_size,
|
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS,
|
|
|
|
-1,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Fallback
|
|
|
|
if (mem_block == MAP_FAILED) {
|
|
|
|
// Try again without the address hint (e.g., valgrind)
|
|
|
|
mem_block = mmap(
|
|
|
|
NULL,
|
|
|
|
mem_size,
|
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS,
|
|
|
|
-1,
|
|
|
|
0
|
|
|
|
);
|
2023-03-08 09:43:37 +03:00
|
|
|
}
|
2023-03-10 22:55:48 +03:00
|
|
|
|
|
|
|
// Check that the memory mapping was successful
|
|
|
|
if (mem_block == MAP_FAILED) {
|
|
|
|
perror("ruby: yjit: mmap:");
|
|
|
|
if(errno == ENOMEM) {
|
|
|
|
// No crash report if it's only insufficient memory
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
rb_bug("mmap failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem_block;
|
|
|
|
#else
|
|
|
|
// Windows not supported for now
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
mprotect_write(rb_execution_context_t *ec, VALUE self, VALUE rb_mem_block, VALUE rb_mem_size)
|
|
|
|
{
|
|
|
|
void *mem_block = (void *)NUM2SIZET(rb_mem_block);
|
|
|
|
uint32_t mem_size = NUM2UINT(rb_mem_size);
|
|
|
|
return RBOOL(mprotect(mem_block, mem_size, PROT_READ | PROT_WRITE) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
mprotect_exec(rb_execution_context_t *ec, VALUE self, VALUE rb_mem_block, VALUE rb_mem_size)
|
|
|
|
{
|
|
|
|
void *mem_block = (void *)NUM2SIZET(rb_mem_block);
|
|
|
|
uint32_t mem_size = NUM2UINT(rb_mem_size);
|
|
|
|
if (mem_size == 0) return Qfalse; // Some platforms return an error for mem_size 0.
|
|
|
|
|
2023-03-08 09:43:37 +03:00
|
|
|
if (mprotect(mem_block, mem_size, PROT_READ | PROT_EXEC)) {
|
|
|
|
rb_bug("Couldn't make JIT page (%p, %lu bytes) executable, errno: %s\n",
|
|
|
|
mem_block, (unsigned long)mem_size, strerror(errno));
|
|
|
|
}
|
2023-03-10 22:55:48 +03:00
|
|
|
return Qtrue;
|
2023-03-08 09:43:37 +03:00
|
|
|
}
|
|
|
|
|
2023-03-09 10:37:58 +03:00
|
|
|
static VALUE
|
|
|
|
rjit_optimized_call(VALUE *recv, rb_execution_context_t *ec, int argc, VALUE *argv, int kw_splat, VALUE block_handler)
|
2023-03-08 09:43:37 +03:00
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(recv, proc);
|
|
|
|
return rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, block_handler);
|
|
|
|
}
|
|
|
|
|
2023-03-09 10:37:58 +03:00
|
|
|
static VALUE
|
|
|
|
rjit_str_neq_internal(VALUE str1, VALUE str2)
|
2023-03-08 09:43:37 +03:00
|
|
|
{
|
|
|
|
return rb_str_eql_internal(str1, str2) == Qtrue ? Qfalse : Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The code we generate in gen_send_cfunc() doesn't fire the c_return TracePoint event
|
|
|
|
// like the interpreter. When tracing for c_return is enabled, we patch the code after
|
|
|
|
// the C method return to call into this to fire the event.
|
2023-03-09 10:37:58 +03:00
|
|
|
static void
|
|
|
|
rjit_full_cfunc_return(rb_execution_context_t *ec, VALUE return_value)
|
2023-03-08 09:43:37 +03:00
|
|
|
{
|
|
|
|
rb_control_frame_t *cfp = ec->cfp;
|
|
|
|
RUBY_ASSERT_ALWAYS(cfp == GET_EC()->cfp);
|
|
|
|
const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
|
|
|
|
|
|
|
|
RUBY_ASSERT_ALWAYS(RUBYVM_CFUNC_FRAME_P(cfp));
|
|
|
|
RUBY_ASSERT_ALWAYS(me->def->type == VM_METHOD_TYPE_CFUNC);
|
|
|
|
|
|
|
|
// CHECK_CFP_CONSISTENCY("full_cfunc_return"); TODO revive this
|
|
|
|
|
|
|
|
// Pop the C func's frame and fire the c_return TracePoint event
|
|
|
|
// Note that this is the same order as vm_call_cfunc_with_frame().
|
|
|
|
rb_vm_pop_frame(ec);
|
|
|
|
EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, return_value);
|
|
|
|
// Note, this deviates from the interpreter in that users need to enable
|
|
|
|
// a c_return TracePoint for this DTrace hook to work. A reasonable change
|
|
|
|
// since the Ruby return event works this way as well.
|
|
|
|
RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
|
|
|
|
|
|
|
|
// Push return value into the caller's stack. We know that it's a frame that
|
|
|
|
// uses cfp->sp because we are patching a call done with gen_send_cfunc().
|
|
|
|
ec->cfp->sp[0] = return_value;
|
|
|
|
ec->cfp->sp++;
|
|
|
|
}
|
|
|
|
|
2023-03-09 10:37:58 +03:00
|
|
|
static rb_proc_t *
|
|
|
|
rjit_get_proc_ptr(VALUE procv)
|
2023-03-08 09:43:37 +03:00
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(procv, proc);
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:39:54 +03:00
|
|
|
// An offsetof implementation that works for unnamed struct and union.
|
|
|
|
// Multiplying 8 for compatibility with libclang's offsetof.
|
|
|
|
#define OFFSETOF(ptr, member) RB_SIZE2NUM(((char *)&ptr.member - (char*)&ptr) * 8)
|
|
|
|
|
2022-09-20 17:23:50 +03:00
|
|
|
#define SIZEOF(type) RB_SIZE2NUM(sizeof(type))
|
2022-09-26 03:21:05 +03:00
|
|
|
#define SIGNED_TYPE_P(type) RBOOL((type)(-1) < (type)(1))
|
2022-09-20 17:23:50 +03:00
|
|
|
|
2023-03-07 10:15:30 +03:00
|
|
|
#if RJIT_STATS
|
2022-12-27 09:46:40 +03:00
|
|
|
// Insn side exit counters
|
2023-03-07 10:17:25 +03:00
|
|
|
static size_t rjit_insn_exits[VM_INSTRUCTION_SIZE] = { 0 };
|
2022-12-27 09:46:40 +03:00
|
|
|
#endif // YJIT_STATS
|
|
|
|
|
2022-12-14 11:12:55 +03:00
|
|
|
// macOS: brew install capstone
|
|
|
|
// Ubuntu/Debian: apt-get install libcapstone-dev
|
|
|
|
// Fedora: dnf -y install capstone-devel
|
2022-12-21 08:53:25 +03:00
|
|
|
#ifdef HAVE_LIBCAPSTONE
|
2022-12-14 11:12:55 +03:00
|
|
|
#include <capstone/capstone.h>
|
2022-12-21 10:48:12 +03:00
|
|
|
#endif
|
2022-12-14 11:12:55 +03:00
|
|
|
|
|
|
|
// Return an array of [address, mnemonic, op_str]
|
|
|
|
static VALUE
|
|
|
|
dump_disasm(rb_execution_context_t *ec, VALUE self, VALUE from, VALUE to)
|
|
|
|
{
|
2022-12-21 10:48:12 +03:00
|
|
|
VALUE result = rb_ary_new();
|
|
|
|
#ifdef HAVE_LIBCAPSTONE
|
2022-12-14 11:12:55 +03:00
|
|
|
// Prepare for calling cs_disasm
|
|
|
|
static csh handle;
|
|
|
|
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) {
|
|
|
|
rb_raise(rb_eRuntimeError, "failed to make Capstone handle");
|
|
|
|
}
|
|
|
|
size_t from_addr = NUM2SIZET(from);
|
|
|
|
size_t to_addr = NUM2SIZET(to);
|
|
|
|
|
|
|
|
// Call cs_disasm and convert results to a Ruby array
|
|
|
|
cs_insn *insns;
|
|
|
|
size_t count = cs_disasm(handle, (const uint8_t *)from_addr, to_addr - from_addr, from_addr, 0, &insns);
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
VALUE vals = rb_ary_new_from_args(3, LONG2NUM(insns[i].address), rb_str_new2(insns[i].mnemonic), rb_str_new2(insns[i].op_str));
|
|
|
|
rb_ary_push(result, vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free memory used by capstone
|
|
|
|
cs_free(insns, count);
|
|
|
|
cs_close(&handle);
|
2022-12-21 10:48:12 +03:00
|
|
|
#endif
|
2022-12-14 11:12:55 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:15:30 +03:00
|
|
|
// Same as `RubyVM::RJIT.enabled?`, but this is used before it's defined.
|
2022-12-27 09:46:40 +03:00
|
|
|
static VALUE
|
2023-03-07 10:17:25 +03:00
|
|
|
rjit_enabled_p(rb_execution_context_t *ec, VALUE self)
|
2022-12-27 09:46:40 +03:00
|
|
|
{
|
2023-03-09 10:14:33 +03:00
|
|
|
return RBOOL(rb_rjit_enabled);
|
2022-12-27 09:46:40 +03:00
|
|
|
}
|
|
|
|
|
2023-02-11 01:41:45 +03:00
|
|
|
static int
|
|
|
|
for_each_iseq_i(void *vstart, void *vend, size_t stride, void *data)
|
|
|
|
{
|
|
|
|
VALUE block = (VALUE)data;
|
|
|
|
VALUE v = (VALUE)vstart;
|
|
|
|
for (; v != (VALUE)vend; v += stride) {
|
|
|
|
void *ptr = asan_poisoned_object_p(v);
|
|
|
|
asan_unpoison_object(v, false);
|
|
|
|
|
|
|
|
if (rb_obj_is_iseq(v)) {
|
2023-03-07 10:17:25 +03:00
|
|
|
extern VALUE rb_rjit_iseq_new(rb_iseq_t *iseq);
|
2023-02-11 01:41:45 +03:00
|
|
|
rb_iseq_t *iseq = (rb_iseq_t *)v;
|
2023-03-07 10:17:25 +03:00
|
|
|
rb_funcall(block, rb_intern("call"), 1, rb_rjit_iseq_new(iseq));
|
2023-02-11 01:41:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
asan_poison_object_if(ptr, v);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-03-07 10:17:25 +03:00
|
|
|
rjit_for_each_iseq(rb_execution_context_t *ec, VALUE self, VALUE block)
|
2023-02-11 01:41:45 +03:00
|
|
|
{
|
|
|
|
rb_objspace_each_objects(for_each_iseq_i, (void *)block);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2023-02-04 09:42:13 +03:00
|
|
|
extern bool rb_simple_iseq_p(const rb_iseq_t *iseq);
|
2023-02-17 09:29:58 +03:00
|
|
|
extern ID rb_get_symbol_id(VALUE name);
|
2023-02-04 09:42:13 +03:00
|
|
|
|
2023-03-07 10:17:25 +03:00
|
|
|
#include "rjit_c.rbinc"
|
2022-09-24 09:06:27 +03:00
|
|
|
|
2023-03-07 10:15:30 +03:00
|
|
|
#endif // USE_RJIT
|