Merge MJIT infrastructure with conservative JIT compiler
In Feature#12589, Vladimir Makarov proposed to improve VM performance by replacing VM instructions
to RTL and introduce method JIT compiler based on those instructions.
While his approach for JIT (write C code to local file system, let C compiler executable
to compile it to shared object file and load it dynamically) was great and proven to work,
replacing all VM instructions may change the behavior of all Ruby programs and we can't turn off
such changes once it's released, even if we find a problem. So it's a little risky unlike optional JIT enablement.
Then I developed a JIT compiler called YARV-MJIT, which does not require any VM instruction changes.
After it, I heard Vladimir started to work on another approach to compile from current YARV instructions and
use RTL as IR for JIT compilation, but it's not published yet as far as I know.
- We're developing the same JIT infrastructure independently, which can be shared for both implementations
- it's definitely a waste of time, unlike seeking different optimization approaches
- If we continue to develop JIT in a big feature branch,
- affected places will be big too, and thus it'll be a dangerous release
- all of us will continue to waste our time by day-to-day conflict resolution against trunk
- many valuable commit logs will be lost when we maintain the branch for rebase or squash commits on merge
- Proposal: Merge MJIT infrastructure from Vladimir's patch with a conservative JIT compiler in early 2.6 development.
- MJIT infrastructure means: JIT worker thread, profiler, gcc/clang compiler support, loading function from shared object file, some hooks to ensure JIT does not cause SEGV, etc...
What's the "conservative JIT compiler"?¶
- Based on my YARV-MJIT, but this drops some problematic optimizations and is slower
make test-specwith and without JIT https://travis-ci.org/ruby/ruby/builds/321589821
- Unlike MJIT on RTL, we can play optcarrot (not just for benchmark, but on GUI) and run Rails application stably (not tested on production yet though)
- As YARV-MJIT implementation improved MJIT infrastructure too, pthread was already ported to Windows native threads and it can be compiled with Visual Studio.
- That's exactly why we should develop this in cooperation
- Visual Sudio is not supported as C compiler for JIT compilation. I did some experiments and had some ideas to support cl.exe, but I didn't want to add extra complexity to initial merge.
- But it's perfectly working on MinGW and this will be available on Windows if a user uses RubyInstaller2.
Benchmarked with: Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores, gcc 5.4.0
|2.0.0||2.5.0||JIT off||JIT on|
- 2.0.0: Ruby 2.0.0-p0
- 2.5.0: Ruby 2.5.0 (r61468)
- JIT off: Patched Ruby (based on r61475), JIT disabled
- JIT on: Patched Ruby (based on r61475), JIT enabled
Disclaimer: This JIT compiler performs better with gcc compared to clang for now, so it may be slow on macOS (clang).
I used Vladimir's benchmark set which I modified for my convenience https://github.com/benchmark-driver/mjit-benchmarks.
|2.0.0-p0||2.5.0||JIT off||JIT on|
If the proposal is accepted, I'm going to add following CLI options:
-j, --jit use MJIT with default options -j:option, --jit:option use MJIT with an option MJIT options: c, cc C compiler to generate native code (gcc, clang) s, save-temps Save MJIT temporary files in $TMP or /tmp w, warnings Enable printing MJIT warnings d, debug Enable MJIT debugging (very slow) v=num, verbose=num Print MJIT logs of level num or less to stderr n=num, num-cache=num Maximum number of JIT codes in a cache
--jit:llvm are changed to
--jit:cc so that we can support cl.exe (Visual Studio) in the future.
Also, for testing, I would like to have following module and method.
MJIT.enabled? #=> true / false
See the commit log for details.