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"
|
2023-03-12 23:55:39 +03:00
|
|
|
#include "include/ruby/assert.h"
|
|
|
|
#include "include/ruby/debug.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;
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:55:39 +03:00
|
|
|
// Use the same buffer size as Stackprof.
|
|
|
|
#define BUFF_LEN 2048
|
|
|
|
|
|
|
|
extern VALUE rb_rjit_raw_samples;
|
|
|
|
extern VALUE rb_rjit_line_samples;
|
|
|
|
|
|
|
|
static void
|
|
|
|
rjit_record_exit_stack(const VALUE *exit_pc)
|
|
|
|
{
|
|
|
|
// Let Primitive.rjit_stop_stats stop this
|
|
|
|
if (!rb_rjit_call_p) return;
|
|
|
|
|
|
|
|
// Get the opcode from the encoded insn handler at this PC
|
|
|
|
int insn = rb_vm_insn_addr2opcode((void *)*exit_pc);
|
|
|
|
|
|
|
|
// Create 2 array buffers to be used to collect frames and lines.
|
|
|
|
VALUE frames_buffer[BUFF_LEN] = { 0 };
|
|
|
|
int lines_buffer[BUFF_LEN] = { 0 };
|
|
|
|
|
|
|
|
// Records call frame and line information for each method entry into two
|
|
|
|
// temporary buffers. Returns the number of times we added to the buffer (ie
|
|
|
|
// the length of the stack).
|
|
|
|
//
|
|
|
|
// Call frame info is stored in the frames_buffer, line number information
|
|
|
|
// in the lines_buffer. The first argument is the start point and the second
|
|
|
|
// argument is the buffer limit, set at 2048.
|
|
|
|
int stack_length = rb_profile_frames(0, BUFF_LEN, frames_buffer, lines_buffer);
|
|
|
|
int samples_length = stack_length + 3; // 3: length, insn, count
|
|
|
|
|
|
|
|
// If yjit_raw_samples is less than or equal to the current length of the samples
|
|
|
|
// we might have seen this stack trace previously.
|
2023-03-13 06:41:07 +03:00
|
|
|
int prev_stack_len_index = (int)RARRAY_LEN(rb_rjit_raw_samples) - samples_length;
|
2023-03-12 23:55:39 +03:00
|
|
|
VALUE prev_stack_len_obj;
|
|
|
|
if (RARRAY_LEN(rb_rjit_raw_samples) >= samples_length && FIXNUM_P(prev_stack_len_obj = RARRAY_AREF(rb_rjit_raw_samples, prev_stack_len_index))) {
|
|
|
|
int prev_stack_len = NUM2INT(prev_stack_len_obj);
|
|
|
|
int idx = stack_length - 1;
|
|
|
|
int prev_frame_idx = 0;
|
|
|
|
bool seen_already = true;
|
|
|
|
|
|
|
|
// If the previous stack length and current stack length are equal,
|
|
|
|
// loop and compare the current frame to the previous frame. If they are
|
|
|
|
// not equal, set seen_already to false and break out of the loop.
|
|
|
|
if (prev_stack_len == stack_length) {
|
|
|
|
while (idx >= 0) {
|
|
|
|
VALUE current_frame = frames_buffer[idx];
|
|
|
|
VALUE prev_frame = RARRAY_AREF(rb_rjit_raw_samples, prev_stack_len_index + prev_frame_idx + 1);
|
|
|
|
|
|
|
|
// If the current frame and previous frame are not equal, set
|
|
|
|
// seen_already to false and break out of the loop.
|
|
|
|
if (current_frame != prev_frame) {
|
|
|
|
seen_already = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx--;
|
|
|
|
prev_frame_idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we know we've seen this stack before, increment the counter by 1.
|
|
|
|
if (seen_already) {
|
2023-03-13 06:41:07 +03:00
|
|
|
int prev_idx = (int)RARRAY_LEN(rb_rjit_raw_samples) - 1;
|
2023-03-12 23:55:39 +03:00
|
|
|
int prev_count = NUM2INT(RARRAY_AREF(rb_rjit_raw_samples, prev_idx));
|
|
|
|
int new_count = prev_count + 1;
|
|
|
|
|
|
|
|
rb_ary_store(rb_rjit_raw_samples, prev_idx, INT2NUM(new_count));
|
|
|
|
rb_ary_store(rb_rjit_line_samples, prev_idx, INT2NUM(new_count));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_ary_push(rb_rjit_raw_samples, INT2NUM(stack_length));
|
|
|
|
rb_ary_push(rb_rjit_line_samples, INT2NUM(stack_length));
|
|
|
|
|
|
|
|
int idx = stack_length - 1;
|
|
|
|
|
|
|
|
while (idx >= 0) {
|
|
|
|
VALUE frame = frames_buffer[idx];
|
|
|
|
int line = lines_buffer[idx];
|
|
|
|
|
|
|
|
rb_ary_push(rb_rjit_raw_samples, frame);
|
|
|
|
rb_ary_push(rb_rjit_line_samples, INT2NUM(line));
|
|
|
|
|
|
|
|
idx--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the insn value into the yjit_raw_samples Vec.
|
|
|
|
rb_ary_push(rb_rjit_raw_samples, INT2NUM(insn));
|
|
|
|
|
|
|
|
// Push the current line onto the yjit_line_samples Vec. This
|
|
|
|
// points to the line in insns.def.
|
2023-03-13 06:41:07 +03:00
|
|
|
int line = (int)RARRAY_LEN(rb_rjit_line_samples) - 1;
|
2023-03-12 23:55:39 +03:00
|
|
|
rb_ary_push(rb_rjit_line_samples, INT2NUM(line));
|
|
|
|
|
|
|
|
// Push number of times seen onto the stack, which is 1
|
|
|
|
// because it's the first time we've seen it.
|
|
|
|
rb_ary_push(rb_rjit_raw_samples, INT2NUM(1));
|
|
|
|
rb_ary_push(rb_rjit_line_samples, INT2NUM(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a given raw_sample (frame), set the hash with the caller's
|
|
|
|
// name, file, and line number. Return the hash with collected frame_info.
|
|
|
|
static void
|
|
|
|
rjit_add_frame(VALUE hash, VALUE frame)
|
|
|
|
{
|
|
|
|
VALUE frame_id = SIZET2NUM(frame);
|
|
|
|
|
|
|
|
if (RTEST(rb_hash_aref(hash, frame_id))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE frame_info = rb_hash_new();
|
|
|
|
// Full label for the frame
|
|
|
|
VALUE name = rb_profile_frame_full_label(frame);
|
|
|
|
// Absolute path of the frame from rb_iseq_realpath
|
|
|
|
VALUE file = rb_profile_frame_absolute_path(frame);
|
|
|
|
// Line number of the frame
|
|
|
|
VALUE line = rb_profile_frame_first_lineno(frame);
|
|
|
|
|
|
|
|
// If absolute path isn't available use the rb_iseq_path
|
|
|
|
if (NIL_P(file)) {
|
|
|
|
file = rb_profile_frame_path(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("name")), name);
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("file")), file);
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("samples")), INT2NUM(0));
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("total_samples")), INT2NUM(0));
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("edges")), rb_hash_new());
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("lines")), rb_hash_new());
|
|
|
|
|
|
|
|
if (line != INT2FIX(0)) {
|
|
|
|
rb_hash_aset(frame_info, ID2SYM(rb_intern("line")), line);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_hash_aset(hash, frame_id, frame_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rjit_exit_traces(void)
|
|
|
|
{
|
2023-03-13 06:41:07 +03:00
|
|
|
int samples_len = (int)RARRAY_LEN(rb_rjit_raw_samples);
|
2023-03-12 23:55:39 +03:00
|
|
|
RUBY_ASSERT(samples_len == RARRAY_LEN(rb_rjit_line_samples));
|
|
|
|
|
|
|
|
VALUE result = rb_hash_new();
|
|
|
|
VALUE raw_samples = rb_ary_new_capa(samples_len);
|
|
|
|
VALUE line_samples = rb_ary_new_capa(samples_len);
|
|
|
|
VALUE frames = rb_hash_new();
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
// While the index is less than samples_len, parse yjit_raw_samples and
|
|
|
|
// yjit_line_samples, then add casted values to raw_samples and line_samples array.
|
|
|
|
while (idx < samples_len) {
|
|
|
|
int num = NUM2INT(RARRAY_AREF(rb_rjit_raw_samples, idx));
|
|
|
|
int line_num = NUM2INT(RARRAY_AREF(rb_rjit_line_samples, idx));
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
rb_ary_push(raw_samples, SIZET2NUM(num));
|
|
|
|
rb_ary_push(line_samples, INT2NUM(line_num));
|
|
|
|
|
|
|
|
// Loop through the length of samples_len and add data to the
|
|
|
|
// frames hash. Also push the current value onto the raw_samples
|
|
|
|
// and line_samples array respectively.
|
|
|
|
for (int o = 0; o < num; o++) {
|
|
|
|
rjit_add_frame(frames, RARRAY_AREF(rb_rjit_raw_samples, idx));
|
|
|
|
rb_ary_push(raw_samples, SIZET2NUM(RARRAY_AREF(rb_rjit_raw_samples, idx)));
|
|
|
|
rb_ary_push(line_samples, RARRAY_AREF(rb_rjit_line_samples, idx));
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// insn BIN and lineno
|
|
|
|
rb_ary_push(raw_samples, RARRAY_AREF(rb_rjit_raw_samples, idx));
|
|
|
|
rb_ary_push(line_samples, RARRAY_AREF(rb_rjit_line_samples, idx));
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
// Number of times seen
|
|
|
|
rb_ary_push(raw_samples, RARRAY_AREF(rb_rjit_raw_samples, idx));
|
|
|
|
rb_ary_push(line_samples, RARRAY_AREF(rb_rjit_line_samples, idx));
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set add the raw_samples, line_samples, and frames to the results
|
|
|
|
// hash.
|
|
|
|
rb_hash_aset(result, ID2SYM(rb_intern("raw")), raw_samples);
|
|
|
|
rb_hash_aset(result, ID2SYM(rb_intern("lines")), line_samples);
|
|
|
|
rb_hash_aset(result, ID2SYM(rb_intern("frames")), frames);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
2023-03-14 06:40:24 +03:00
|
|
|
dump_disasm(rb_execution_context_t *ec, VALUE self, VALUE from, VALUE to, VALUE test)
|
2022-12-14 11:12:55 +03:00
|
|
|
{
|
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;
|
2023-03-14 06:40:24 +03:00
|
|
|
size_t base_addr = RTEST(test) ? 0 : from_addr; // On tests, start from 0 for output stability.
|
|
|
|
size_t count = cs_disasm(handle, (const uint8_t *)from_addr, to_addr - from_addr, base_addr, 0, &insns);
|
2022-12-14 11:12:55 +03:00
|
|
|
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-03-12 08:10:44 +03:00
|
|
|
// bindgen funcs
|
2023-02-17 09:29:58 +03:00
|
|
|
extern ID rb_get_symbol_id(VALUE name);
|
2023-03-12 08:10:44 +03:00
|
|
|
extern VALUE rb_fix_aref(VALUE fix, VALUE idx);
|
|
|
|
extern VALUE rb_str_getbyte(VALUE str, VALUE index);
|
|
|
|
extern VALUE rb_vm_concat_array(VALUE ary1, VALUE ary2st);
|
|
|
|
extern VALUE rb_vm_get_ev_const(rb_execution_context_t *ec, VALUE orig_klass, ID id, VALUE allow_nil);
|
|
|
|
extern VALUE rb_vm_getclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *cfp, ID id, ICVARC ic);
|
|
|
|
extern VALUE rb_vm_opt_newarray_min(rb_execution_context_t *ec, rb_num_t num, const VALUE *ptr);
|
|
|
|
extern VALUE rb_vm_splat_array(VALUE flag, VALUE array);
|
|
|
|
extern bool rb_simple_iseq_p(const rb_iseq_t *iseq);
|
|
|
|
extern bool rb_vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v);
|
|
|
|
extern bool rb_vm_ic_hit_p(IC ic, const VALUE *reg_ep);
|
|
|
|
extern rb_event_flag_t rb_rjit_global_events;
|
|
|
|
extern void rb_vm_setinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, VALUE val, IVC ic);
|
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
|