ruby/test
k0kubun ed935aa5be 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 11:22:28 +00:00
..
-ext- mjit_compile.c: merge initial JIT compiler 2018-02-04 11:22:28 +00:00
base64 lib/base64.rb: Specify frozen_string_literal: true. 2017-02-10 00:58:45 +00:00
benchmark lib/benchmark.rb: Specify frozen_string_literal: true. 2017-01-03 02:00:01 +00:00
bigdecimal `$SAFE` as a process global state. [Feature #14250] 2017-12-28 20:09:24 +00:00
cgi Fix strange indentation 2017-05-18 15:13:30 +00:00
coverage test/coverage/test_coverage.rb: make the expected value the same as 2.4 2017-12-20 04:45:05 +00:00
csv test_features.rb: fix unused variable warning 2017-09-26 10:45:14 +00:00
date Add FrozenError as a subclass of RuntimeError 2017-12-12 00:46:34 +00:00
dbm skip some tests so that no failure occurs in root privilege 2018-01-10 10:39:09 +00:00
digest Fix tests which fail with extra stderr output when a Thread dies 2017-12-12 18:44:21 +00:00
drb drb: use \A and \z 2017-12-30 12:10:43 +00:00
dtrace rename ruby-runner as bin/ruby 2017-09-15 17:42:00 +00:00
erb erb.rb: preserve the behavior for invalid syntax 2017-12-27 12:40:03 +00:00
etc test_etc.rb: fix test_getgrnam for duplicated group names 2017-09-29 17:19:37 +00:00
excludes TestException.rb: suppress warning 2016-04-11 08:27:19 +00:00
fiddle `$SAFE` as a process global state. [Feature #14250] 2017-12-28 20:09:24 +00:00
fileutils Fix test-all tests to avoid creating report_on_exception warnings 2017-12-12 18:44:49 +00:00
gdbm Fix indent and use as skipped message instead of comment 2018-01-11 13:56:04 +00:00
io test_io_console.rb: get rid of sporadic failures 2017-12-23 14:31:56 +00:00
irb skip some tests so that no failure occurs in root privilege 2018-01-10 10:39:09 +00:00
json require 'bigdecimal' 2017-06-16 04:04:53 +00:00
lib mjit_compile.c: merge initial JIT compiler 2018-02-04 11:22:28 +00:00
logger revert r60999 2017-12-04 00:23:31 +00:00
matrix lib/matrix: Add hadamard_product/entrywise_product. 2017-11-20 02:18:43 +00:00
minitest test_minitest_unit.rb: assert_triggered 2017-03-29 09:47:40 +00:00
misc test_ruby_mode.rb: fix for Emacs 25.1 2016-09-24 01:19:43 +00:00
mkmf multiple arguments to write 2017-10-23 14:05:07 +00:00
monitor Remove unnecessary `require 'thread'` 2017-10-08 07:00:01 +00:00
net net/pop: make modified strings mutable 2018-01-30 00:22:22 +00:00
nkf Fix test code of kconv 2017-09-15 16:56:16 +00:00
objspace io.c: encoding of ARGF.inplace_mode 2017-10-10 12:30:42 +00:00
open-uri open-uri defines URI.open defined as an alias. 2017-12-21 14:15:04 +00:00
openssl openssl: suppress report_on_exception warning 2017-12-15 08:19:32 +00:00
optparse optparse.rb: froze string literals 2018-01-26 03:41:04 +00:00
ostruct test/ostruct: Specify frozen_string_literal: true. 2017-10-25 23:45:33 +00:00
pathname skip some tests so that no failure occurs in root privilege 2018-01-10 10:39:09 +00:00
psych Merge psych-3.0.2 from ruby/psych. 2017-12-19 09:44:33 +00:00
rdoc skip some tests so that no failure occurs in root privilege 2018-01-10 10:39:09 +00:00
readline `$SAFE` as a process global state. [Feature #14250] 2017-12-28 20:09:24 +00:00
resolv fix for IPv6 env 2018-01-11 19:41:49 +00:00
rexml REXML: Fix a bug that unexpected methods can be called as a XPath function 2017-08-13 12:14:24 +00:00
rinda Fix test-all tests to avoid creating report_on_exception warnings 2017-12-12 18:44:49 +00:00
ripper parse.y: refactor list literals 2017-11-24 04:26:27 +00:00
rss rss itunes: fix a bug that <itunes:explicit> value isn't fully supported 2017-10-22 16:03:57 +00:00
ruby mjit_compile.c: merge initial JIT compiler 2018-02-04 11:22:28 +00:00
rubygems mjit_compile.c: merge initial JIT compiler 2018-02-04 11:22:28 +00:00
scanf Fix bug about String#scanf("%a") 2017-10-21 12:34:13 +00:00
sdbm skip some tests so that no failure occurs in root privilege 2018-01-10 10:39:09 +00:00
shell test_command_processor.rb: add suffix 2017-06-23 15:08:50 +00:00
socket init.c: encode socket error message 2018-01-23 15:31:22 +00:00
stringio Add FrozenError as a subclass of RuntimeError 2017-12-12 00:46:34 +00:00
strscan strscan.c: add MatchData-like methods 2017-11-29 07:57:48 +00:00
syslog Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
testunit assertions.rb: fix return in assert_raise 2017-11-02 12:29:33 +00:00
thread mjit_compile.c: merge initial JIT compiler 2018-02-04 11:22:28 +00:00
uri Allow empty path components in a URI [Bug #8352] 2017-12-14 01:11:28 +00:00
webrick mjit_compile.c: merge initial JIT compiler 2018-02-04 11:22:28 +00:00
win32ole `$SAFE` as a process global state. [Feature #14250] 2017-12-28 20:09:24 +00:00
yaml lib/pstore.rb: Specify frozen_string_literal: true. 2017-01-17 12:47:38 +00:00
zlib zlib.c: multiple arguments to write 2017-10-23 07:06:12 +00:00
colors default colors 2015-05-17 00:50:11 +00:00
runner.rb the working directory may not be srcdir 2017-09-07 17:45:34 +00:00
test_abbrev.rb lib/abbrev.rb: Specify frozen_string_literal: true. 2017-02-09 16:12:00 +00:00
test_cmath.rb lib/cmath.rb: Specify frozen_string_literal: true. 2017-01-04 05:47:52 +00:00
test_delegate.rb Add FrozenError as a subclass of RuntimeError 2017-12-12 00:46:34 +00:00
test_extlibs.rb Revert "test/test_extilibs.rb: do not check the existence of fiddle" 2017-05-16 23:59:01 +00:00
test_find.rb Fix indent and use as skipped message instead of comment 2018-01-11 13:56:04 +00:00
test_forwardable.rb Add test for Forwardable#def_delegator with r55366. 2018-02-01 09:07:16 +00:00
test_ipaddr.rb Import ipaddr 1.2.0 2017-10-21 13:34:19 +00:00
test_mutex_m.rb Remove unnecessary `require 'thread'` 2017-10-08 07:00:01 +00:00
test_observer.rb lib/observer.rb: Specify frozen_string_literal: true. 2017-01-13 12:08:29 +00:00
test_open3.rb lib/open3.rb: accept IO-like object for :stdin_data argument. 2017-10-21 07:00:58 +00:00
test_pp.rb * lib/open-uri.rb: Remove indicator for "frozen_string_literal: true". 2015-11-10 11:48:14 +00:00
test_prettyprint.rb * lib/open-uri.rb: Remove indicator for "frozen_string_literal: true". 2015-11-10 11:48:14 +00:00
test_prime.rb prime.rb: remove alias after timeout test 2017-05-20 10:21:44 +00:00
test_pstore.rb lib/pstore.rb: Specify frozen_string_literal: true. 2017-01-17 12:47:38 +00:00
test_pty.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_rbconfig.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_securerandom.rb Use `\A` and `\z` instead of `^` and `$` 2017-10-22 09:16:54 +00:00
test_set.rb Add FrozenError as a subclass of RuntimeError 2017-12-12 00:46:34 +00:00
test_shellwords.rb Fix the handling of the backslash in double quotes 2016-11-05 04:58:48 +00:00
test_singleton.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_syslog.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_tempfile.rb `$SAFE` as a process global state. [Feature #14250] 2017-12-28 20:09:24 +00:00
test_time.rb Make Time.parse respect timezone offset seconds 2017-10-20 01:03:18 +00:00
test_timeout.rb Remove unnecessary `require 'thread'` 2017-10-08 07:00:01 +00:00
test_tmpdir.rb `$SAFE` as a process global state. [Feature #14250] 2017-12-28 20:09:24 +00:00
test_tracer.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_tsort.rb test/test_tsort.rb: Specify frozen_string_literal: true. 2015-11-14 09:43:18 +00:00
test_unicode_normalize.rb * test/ruby/enc/test_case_comprehensive.rb, test_regex_casefold.rb, 2016-07-03 09:51:46 +00:00
test_weakref.rb lib/weakref.rb: Specify frozen_string_literal: true. 2017-01-26 15:14:00 +00:00
test_win32api.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00