Граф коммитов

515 Коммитов

Автор SHA1 Сообщение Дата
Benoit Daloze 9b535f3ff7 Interpolated strings are no longer frozen with frozen-string-literal: true
* Remove freezestring instruction since this was the only usage for it.
* [Feature #17104]
2020-09-15 21:32:35 +02:00
卜部昌平 f66e0212ef precalc invokebuiltin destinations
Noticed that struct rb_builtin_function is a purely compile-time
constant.  MJIT can eliminate some runtime calculations by statically
generate dedicated C code generator for each builtin functions.
2020-07-13 08:56:18 +09:00
Koichi Sasada a0f12a0258
Use ID instead of GENTRY for gvars. (#3278)
Use ID instead of GENTRY for gvars.

Global variables are compiled into GENTRY (a pointer to struct
rb_global_entry). This patch replace this GENTRY to ID and
make the code simple.

We need to search GENTRY from ID every time (st_lookup), so
additional overhead will be introduced.
However, the performance of accessing global variables is not
important now a day and this simplicity helps Ractor development.
2020-07-03 16:56:44 +09:00
Takashi Kokubun 3e02cd518f
Trace :return of builtin methods
using opt_invokebuiltin_delegate_leave insn.

Since Ruby 2.7, :return of methods using builtin have not been traced properly.
2020-06-23 23:42:38 -07:00
Takashi Kokubun e544a3a23c
Remove obsoleted opt_call_c_function insn (#3232)
* Remove obsoleted opt_call_c_function insn

* Keep opt_call_c_function with DEFINE_INSN_IF
2020-06-17 09:16:01 -07:00
卜部昌平 40ced763b4 vm_insnhelper.c: merge opt_eq_func / opt_eql_func
These two function were almost identical, except in case of
T_STRING/T_FLOAT. Why not merge them into one, and let the difference be
handled in normal method calls (slowpath).  This does not improve
runtime performance for me, but at least reduces for instance rb_eql_opt
from 653 bytes to 86 bytes on my machine, according to nm(1).
2020-06-02 12:38:01 +09:00
Jeremy Evans 900e83b501 Turn class variable warnings into exceptions
This changes the following warnings:

* warning: class variable access from toplevel
* warning: class variable @foo of D is overtaken by C

into RuntimeErrors.  Handle defined?(@@foo) at toplevel
by returning nil instead of raising an exception (the previous
behavior warned before returning nil when defined? was used).

Refactor the specs to avoid the warnings even in older versions.
The specs were checking for the warnings, but the purpose of
the related specs as evidenced from their description is to
test for behavior, not for warnings.

Fixes [Bug #14541]
2020-04-10 00:29:05 -07:00
Koichi Sasada b9007b6c54 Introduce disposable call-cache.
This patch contains several ideas:

(1) Disposable inline method cache (IMC) for race-free inline method cache
    * Making call-cache (CC) as a RVALUE (GC target object) and allocate new
      CC on cache miss.
    * This technique allows race-free access from parallel processing
      elements like RCU.
(2) Introduce per-Class method cache (pCMC)
    * Instead of fixed-size global method cache (GMC), pCMC allows flexible
      cache size.
    * Caching CCs reduces CC allocation and allow sharing CC's fast-path
      between same call-info (CI) call-sites.
(3) Invalidate an inline method cache by invalidating corresponding method
    entries (MEs)
    * Instead of using class serials, we set "invalidated" flag for method
      entry itself to represent cache invalidation.
    * Compare with using class serials, the impact of method modification
      (add/overwrite/delete) is small.
    * Updating class serials invalidate all method caches of the class and
      sub-classes.
    * Proposed approach only invalidate the method cache of only one ME.

See [Feature #16614] for more details.
2020-02-22 09:58:59 +09:00
Koichi Sasada f2286925f0 VALUE size packed callinfo (ci).
Now, rb_call_info contains how to call the method with tuple of
(mid, orig_argc, flags, kwarg). Most of cases, kwarg == NULL and
mid+argc+flags only requires 64bits. So this patch packed
rb_call_info to VALUE (1 word) on such cases. If we can not
represent it in VALUE, then use imemo_callinfo which contains
conventional callinfo (rb_callinfo, renamed from rb_call_info).

iseq->body->ci_kw_size is removed because all of callinfo is VALUE
size (packed ci or a pointer to imemo_callinfo).

To access ci information, we need to use these functions:
vm_ci_mid(ci), _flag(ci), _argc(ci), _kwarg(ci).

struct rb_call_info_kw_arg is renamed to rb_callinfo_kwarg.

rb_funcallv_with_cc() and rb_method_basic_definition_p_with_cc()
is temporary removed because cd->ci should be marked.
2020-02-22 09:58:59 +09:00
Nobuyoshi Nakada bc8f28fbd0
Fixed a typo, missing "i" [ci skip] 2020-01-27 16:23:52 +09:00
Aaron Patterson 2c8d186c6e
Introduce an "Inline IVAR cache" struct
This commit introduces an "inline ivar cache" struct.  The reason we
need this is so compaction can differentiate from an ivar cache and a
regular inline cache.  Regular inline caches contain references to
`VALUE` and ivar caches just contain references to the ivar index.  With
this new struct we can easily update references for inline caches (but
not inline var caches as they just contain an int)
2019-12-05 13:37:02 -08:00
Koichi Sasada 36da0b3da1 check interrupts at each frame pop timing.
Asynchronous events such as signal trap, finalization timing,
thread switching and so on are managed by "interrupt_flag".
Ruby's threads check this flag periodically and if a thread
does not check this flag, above events doesn't happen.

This checking is CHECK_INTS() (related) macro and it is placed
at some places (laeve instruction and so on). However, at the end
of C methods, C blocks (IMEMO_IFUNC) etc there are no checking
and it can introduce uninterruptible thread.

To modify this situation, we decide to place CHECK_INTS() at
vm_pop_frame(). It increases interrupt checking points.
[Bug #16366]

This patch can introduce unexpected events...
2019-11-29 17:47:02 +09:00
Koichi Sasada f38b6d197f Revert "export for MJIT"
This reverts commit 2e6f1cf8b2.
2019-11-29 03:22:24 +09:00
Koichi Sasada 2e6f1cf8b2 export for MJIT 2019-11-29 03:17:52 +09:00
Koichi Sasada 5e34ab5406 add casts.
add casts to avoid compile error.
http://ci.rvm.jp/results/trunk_clang_39@silicon-docker/2402215
2019-11-18 10:36:48 +09:00
Koichi Sasada 71fee9bc72 vm_invoke_builtin_delegate with start index.
opt_invokebuiltin_delegate and opt_invokebuiltin_delegate_leave
invokes builtin functions with same parameters of the method.
This technique eliminate stack push operations. However, delegation
parameters should be completely same as given parameters.
(e.g. `def foo(a, b, c) __builtin_foo(a, b, c)` is okay, but
__builtin_foo(b, c) is not allowed)

This patch relaxes this restriction. ISeq has a local variables
table which includes parameters. For example, the method defined
as `def foo(a, b, c) x=y=nil`, then local variables table contains
[a, b, c, x, y]. If calling builtin-function with arguments which
are sub-array of the lvar table, use opt_invokebuiltin_delegate
instruction with start index. For example, `__builtin_foo(b, c)`,
`__builtin_bar(c, x, y)` is okay, and so on.
2019-11-18 10:16:11 +09:00
Nobuyoshi Nakada fb6a489af2
Revert "Method reference operator"
This reverts commit 67c5747369.
[Feature #16275]
2019-11-12 17:24:48 +09:00
Koichi Sasada 43ceedecc0 use STACK_ADDR_FROM_TOP()
vm_invoke_builtin() accesses VM stack via cfp->sp. However, MJIT
can use their own stack. To access them appropriately, we need to
use STACK_ADDR_FROM_TOP().
2019-11-09 16:18:58 +09:00
Koichi Sasada 46acd0075d support builtin features with Ruby and C.
Support loading builtin features written in Ruby, which implement
with C builtin functions.
[Feature #16254]

Several features:

(1) Load .rb file at boottime with native binary.

Now, prelude.rb is loaded at boottime. However, this file is contained
into the interpreter as a text format and we need to compile it.
This patch contains a feature to load from binary format.

(2) __builtin_func() in Ruby call func() written in C.

In Ruby file, we can write `__builtin_func()` like method call.
However this is not a method call, but special syntax to call
a function `func()` written in C. C functions should be defined
in a file (same compile unit) which load this .rb file.

Functions (`func` in above example) should be defined with
  (a) 1st parameter: rb_execution_context_t *ec
  (b) rest parameters (0 to 15).
  (c) VALUE return type.
This is very similar requirements for functions used by
rb_define_method(), however `rb_execution_context_t *ec`
is new requirement.

(3) automatic C code generation from .rb files.

tool/mk_builtin_loader.rb creates a C code to load .rb files
needed by miniruby and ruby command. This script is run by
BASERUBY, so *.rb should be written in BASERUBY compatbile
syntax. This script load a .rb file and find all of __builtin_
prefix method calls, and generate a part of C code to export
functions.

tool/mk_builtin_binary.rb creates a C code which contains
binary compiled Ruby files needed by ruby command.
2019-11-08 09:09:29 +09:00
Alan Wu 89e7997622 Combine call info and cache to speed up method invocation
To perform a regular method call, the VM needs two structs,
`rb_call_info` and `rb_call_cache`. At the moment, we allocate these two
structures in separate buffers. In the worst case, the CPU needs to read
4 cache lines to complete a method call. Putting the two structures
together reduces the maximum number of cache line reads to 2.

Combining the structures also saves 8 bytes per call site as the current
layout uses separate two pointers for the call info and the call cache.
This saves about 2 MiB on Discourse.

This change improves the Optcarrot benchmark at least 3%. For more
details, see attached bugs.ruby-lang.org ticket.

Complications:
 - A new instruction attribute `comptime_sp_inc` is introduced to
 calculate SP increase at compile time without using call caches. At
 compile time, a `TS_CALLDATA` operand points to a call info struct, but
 at runtime, the same operand points to a call data struct. Instruction
 that explicitly define `sp_inc` also need to define `comptime_sp_inc`.
 - MJIT code for copying call cache becomes slightly more complicated.
 - This changes the bytecode format, which might break existing tools.

[Misc #16258]
2019-10-24 18:03:42 +09:00
卜部昌平 eb92159d72 Revert https://github.com/ruby/ruby/pull/2486
This reverts commits: 10d6a3aca7 8ba48c1b85 fba8627dc1 dd883de5ba
6c6a25feca 167e6b48f1 7cb96d41a5 3207979278 595b3c4fdd 1521f7cf89
c11c5e69ac cf33608203 3632a812c0 f56506be0d 86427a3219 .

The reason for the revert is that we observe ABA problem around
inline method cache.  When a cache misshits, we search for a
method entry.  And if the entry is identical to what was cached
before, we reuse the cache.  But the commits we are reverting here
introduced situations where a method entry is freed, then the
identical memory region is used for another method entry.  An
inline method cache cannot detect that ABA.

Here is a code that reproduce such situation:

```ruby
require 'prime'

class << Integer
  alias org_sqrt sqrt
  def sqrt(n)
    raise
  end

  GC.stress = true
  Prime.each(7*37){} rescue nil # <- Here we populate CC
  class << Object.new; end

  # These adjacent remove-then-alias maneuver
  # frees a method entry, then immediately
  # reuses it for another.
  remove_method :sqrt
  alias sqrt org_sqrt
end

Prime.each(7*37).to_a # <- SEGV
```
2019-10-03 12:45:24 +09:00
卜部昌平 fba8627dc1 delete unnecessary branch
At last, not only myself but also your compiler are fully confident
that the method entries pointed from call caches are immutable.  We
don't have to worry about silent updates.  Just delete the branch
that is now always false.

Calculating -------------------------------------
                           ours       trunk
vm2_poly_same_method     2.142M      2.070M i/s -      6.000M times in 2.801148s 2.898994s

Comparison:
             vm2_poly_same_method
                ours:   2141979.2 i/s
               trunk:   2069683.8 i/s - 1.03x  slower
2019-09-30 10:26:38 +09:00
卜部昌平 d74fa8e55c reuse cc->call
I noticed that in case of cache misshit, re-calculated cc->me can
be the same method entry than the pevious one.  That is an okay
situation but can't we partially reuse the cache, because cc->call
should still be valid then?

One thing that has to be special-cased is when the method entry
gets amended by some refinements.  That happens behind-the-scene
of call cache mechanism.  We have to check if cc->me->def points to
the previously saved one.

Calculating -------------------------------------
                          trunk        ours
vm2_poly_same_method     1.534M      2.025M i/s -      6.000M times in 3.910203s 2.962752s

Comparison:
             vm2_poly_same_method
                ours:   2025143.9 i/s
               trunk:   1534447.2 i/s - 1.32x  slower
2019-09-19 15:18:10 +09:00
Takashi Kokubun 1a9cc3b27c Avoid defining unused instructions 2019-09-03 14:22:44 +09:00
Jeremy Evans f560609d66
Merge pull request #2418 from jeremyevans/array-empty-kwsplat
Ignore empty keyword splats in arrays
2019-09-02 08:21:30 -07:00
Urabe, Shyouhei 8ad7fafcdd opt_regexpmatch1 is actually making things slower.
----

trunk: ruby 2.6.0dev (2018-09-18 trunk 64767) [x86_64-darwin15]
ours: ruby 2.6.0dev (2018-09-18 opt_regexpmatch 64775) [x86_64-darwin15]
last_commit=opt_regexpmatch1 is actually making things slower.
Calculating -------------------------------------
                              trunk        ours
Optcarrot Lan_Master.nes     33.877      35.282 fps

Comparison:
             Optcarrot Lan_Master.nes
                    ours:        35.3 fps
                   trunk:        33.9 fps - 1.04x  slower
2019-09-02 13:56:40 +09:00
Maciej Mensfeld c45dd4d482 Make the dot-colon method reference frozen
[Feature #16103]

Close: https://github.com/ruby/ruby/pull/2267
2019-08-30 18:24:33 +09:00
Nobuyoshi Nakada abe12d8b96
Freeze method reference operator object
[Feature #16103]
2019-08-29 16:58:21 +09:00
Jeremy Evans 661927a4c5 Switch to using a VM stack argument instead of 2nd operand for getconstant
Some tooling depends on the current bytecode, and adding an operand
changes the bytecode.  While tooling can be updated for new bytecode,
this support doesn't warrant such a change.
2019-08-14 11:22:07 -07:00
Jeremy Evans 6ac6de84ac Use Qtrue/Qfalse instead of 1/0 for 2nd operand to getconstant
Fixes error when using -Werror,-Wshorten-64-to-32.
2019-08-14 09:59:27 -07:00
Jeremy Evans fbcd065294 Remove support for nil::Constant
This was an intentional bug added in 1.9.

The approach taken here is to add a second operand to the
getconstant instruction for whether nil should be allowed and
treated as current scope.

Fixes [Bug #11718]
2019-08-14 09:50:14 -07:00
卜部昌平 b5146e375a
leafify opt_plus
Inspired by 346aa557b3

Closes: https://github.com/ruby/ruby/pull/2321
2019-08-06 20:59:19 +09:00
Takashi Kokubun 548cd6e2b5
Drop default leaf definition and obsoleted comments
leaf is true by default. Other insns are not specifying it explicitly.
Also the comment describing why it was not leaf is outdated.
2019-08-05 09:16:13 +09:00
Takashi Kokubun 346aa557b3
Make opt_eq and opt_neq insns leaf
# Benchmark zero?

```
require 'benchmark/ips'

Numeric.class_eval do
  def ruby_zero?
    self == 0
  end
end

Benchmark.ips do |x|
  x.report('0.zero?') { 0.ruby_zero? }
  x.report('1.zero?') { 1.ruby_zero? }
  x.compare!
end
```

## VM
No significant impact for VM.

### before
ruby 2.7.0dev (2019-08-04T02:56:02Z master 2d8c037e97) [x86_64-linux]

  0.zero?: 21855445.5 i/s
  1.zero?: 21770817.3 i/s - same-ish: difference falls within error

### after
ruby 2.7.0dev (2019-08-04T11:17:10Z opt-eq-leaf 6404bebd6a) [x86_64-linux]

  1.zero?: 21958912.3 i/s
  0.zero?: 21881625.9 i/s - same-ish: difference falls within error

## JIT
The performance improves about 1.23x.

### before
ruby 2.7.0dev (2019-08-04T02:56:02Z master 2d8c037e97) +JIT [x86_64-linux]

  0.zero?: 36343111.6 i/s
  1.zero?: 36295153.3 i/s - same-ish: difference falls within error

### after
ruby 2.7.0dev (2019-08-04T11:17:10Z opt-eq-leaf 6404bebd6a) +JIT [x86_64-linux]

  0.zero?: 44740467.2 i/s
  1.zero?: 44363616.1 i/s - same-ish: difference falls within error

# Benchmark str == str / str != str

```
# frozen_string_literal: true
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report('a == a') { 'a' == 'a' }
  x.report('a == b') { 'a' == 'b' }
  x.report('a != a') { 'a' != 'a' }
  x.report('a != b') { 'a' != 'b' }
  x.compare!
end
```

## VM
No significant impact for VM.

### before
ruby 2.7.0dev (2019-08-04T02:56:02Z master 2d8c037e97) [x86_64-linux]

  a == a: 27286219.0 i/s
  a != a: 24892389.5 i/s - 1.10x  slower
  a == b: 23623635.8 i/s - 1.16x  slower
  a != b: 21800958.0 i/s - 1.25x  slower

### after
ruby 2.7.0dev (2019-08-04T11:17:10Z opt-eq-leaf 6404bebd6a) [x86_64-linux]

  a == a: 27224016.2 i/s
  a != a: 24490109.5 i/s - 1.11x  slower
  a == b: 23391052.4 i/s - 1.16x  slower
  a != b: 21811321.7 i/s - 1.25x  slower

## JIT
The performance improves on JIT a little.

### before
ruby 2.7.0dev (2019-08-04T02:56:02Z master 2d8c037e97) +JIT [x86_64-linux]

  a == a: 42010674.7 i/s
  a != a: 38920311.2 i/s - same-ish: difference falls within error
  a == b: 32574262.2 i/s - 1.29x  slower
  a != b: 32099790.3 i/s - 1.31x  slower

### after
ruby 2.7.0dev (2019-08-04T11:17:10Z opt-eq-leaf 6404bebd6a) +JIT [x86_64-linux]

  a == a: 46902738.8 i/s
  a != a: 43097258.6 i/s - 1.09x  slower
  a == b: 35822018.4 i/s - 1.31x  slower
  a != b: 33377257.8 i/s - 1.41x  slower

This is needed towards Bug#15589.
Closes: https://github.com/ruby/ruby/pull/2318
2019-08-04 22:20:12 +09:00
Yusuke Endoh 086ffe72c7 Revert "Revert "Add a specialized instruction for `.nil?` calls""
This reverts commit a0980f2446.

Retry for macOS Mojave.
2019-08-02 23:25:38 +09:00
Yusuke Endoh a0980f2446 Revert "Add a specialized instruction for `.nil?` calls"
This reverts commit 9faef3113f.

It seemed to cause a failure on macOS Mojave, though I'm unsure how.
https://rubyci.org/logs/rubyci.s3.amazonaws.com/osx1014/ruby-master/log/20190802T034503Z.fail.html.gz

This tentative revert is to check if the issue is actually caused by the
change or not.
2019-08-02 15:03:34 +09:00
Aaron Patterson 9faef3113f
Add a specialized instruction for `.nil?` calls
This commit adds a specialized instruction for called to `.nil?`.  It is
about 27% faster than master in the case where the object is nil or not
nil.  In the case where an object implements `nil?`, I think it may be
slightly slower.  Here is a benchmark:

```ruby
require "benchmark/ips"

class Niller
  def nil?; true; end
end

not_nil = Object.new
xnil = nil
niller = Niller.new

Benchmark.ips do |x|
  x.report("nil?")    { xnil.nil? }
  x.report("not nil") { not_nil.nil? }
  x.report("niller")   { niller.nil? }
end
```

On Ruby master:

```
[aaron@TC ~/g/ruby (master)]$ ./ruby compil.rb
Warming up --------------------------------------
                nil?   429.195k i/100ms
             not nil   437.889k i/100ms
              niller   437.935k i/100ms
Calculating -------------------------------------
                nil?     20.166M (± 8.1%) i/s -    100.002M in   5.002794s
             not nil     20.046M (± 7.6%) i/s -     99.839M in   5.020086s
              niller     22.467M (± 6.1%) i/s -    112.111M in   5.013817s
[aaron@TC ~/g/ruby (master)]$ ./ruby compil.rb
Warming up --------------------------------------
                nil?   449.660k i/100ms
             not nil   433.836k i/100ms
              niller   443.073k i/100ms
Calculating -------------------------------------
                nil?     19.997M (± 8.8%) i/s -     99.375M in   5.020458s
             not nil     20.529M (± 7.0%) i/s -    102.385M in   5.020689s
              niller     21.796M (± 8.0%) i/s -    108.110M in   5.002300s
[aaron@TC ~/g/ruby (master)]$ ./ruby compil.rb
Warming up --------------------------------------
                nil?   402.119k i/100ms
             not nil   438.968k i/100ms
              niller   398.226k i/100ms
Calculating -------------------------------------
                nil?     20.050M (±12.2%) i/s -     98.519M in   5.008817s
             not nil     20.614M (± 8.0%) i/s -    102.280M in   5.004531s
              niller     22.223M (± 8.8%) i/s -    110.309M in   5.013106s

```

On this branch:

```
[aaron@TC ~/g/ruby (specialized-nilp)]$ ./ruby compil.rb
Warming up --------------------------------------
                nil?   468.371k i/100ms
             not nil   456.517k i/100ms
              niller   454.981k i/100ms
Calculating -------------------------------------
                nil?     27.849M (± 7.8%) i/s -    138.169M in   5.001730s
             not nil     26.417M (± 8.7%) i/s -    131.020M in   5.011674s
              niller     21.561M (± 7.5%) i/s -    107.376M in   5.018113s
[aaron@TC ~/g/ruby (specialized-nilp)]$ ./ruby compil.rb
Warming up --------------------------------------
                nil?   477.259k i/100ms
             not nil   428.712k i/100ms
              niller   446.109k i/100ms
Calculating -------------------------------------
                nil?     28.071M (± 7.3%) i/s -    139.837M in   5.016590s
             not nil     25.789M (±12.9%) i/s -    126.470M in   5.011144s
              niller     20.002M (±12.2%) i/s -     98.144M in   5.001737s
[aaron@TC ~/g/ruby (specialized-nilp)]$ ./ruby compil.rb
Warming up --------------------------------------
                nil?   467.676k i/100ms
             not nil   445.791k i/100ms
              niller   415.024k i/100ms
Calculating -------------------------------------
                nil?     26.907M (± 8.0%) i/s -    133.755M in   5.013915s
             not nil     25.319M (± 7.9%) i/s -    125.713M in   5.007758s
              niller     19.569M (±11.8%) i/s -     96.286M in   5.008533s
```

Co-Authored-By: Ashe Connor <kivikakk@github.com>
2019-07-31 16:21:25 -07:00
ko1 2b5bb8a087 add definemethod/definesmethod insn.
* insns.def: add definemethod and definesmethod (singleton method)
  instructions. Old YARV contains these instructions, but it is moved
  to methods of FrozenCore class because remove number of instructions
  can improve performance for some techniques (static stack caching
  and so on). However, we don't employ these technique and it is hard
  to optimize/analysis definition sequence. So I decide to introduce
  them (and remove definition methods). `putiseq` insn is also removed.

* vm_method.c (rb_scope_visibility_get): renamed to
  `vm_scope_visibility_get()` and make it accept `ec`.
  Same for `vm_scope_module_func_check()`.
  These fixes are result of refactoring `vm_define_method`.

* vm_insnhelper.c (rb_vm_get_cref): renamed to `vm_get_cref`
  because of consistency with other functions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-05 08:15:11 +00:00
svn ce9e0ffd71 * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-03-29 06:36:48 +00:00
ko1 35e677dd0a use GET_CFP() instead of access reg_cfp directly.
GET_CFP() macro contains performance counter logic.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67371 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-03-29 06:36:46 +00:00
k0kubun 193a06caf5 insns.def: opt_regexpmatch2 is not a leaf insn
related: r66982
Sadly opt_regexpmatch2 was not a leaf insn either.
http://ci.rvm.jp/results/trunk-vm-asserts@silicon-docker/1751213

CHECK_INTERRUPT_IN_MATCH_AT is just like RUBY_VM_CHECK_INTS, and it may
call arbitrary Ruby method, for example a GC finalizer from postponed
job in this case.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67091 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-02-19 15:39:35 +00:00
k0kubun 1f34da3709 insns.def: opt_regexpmatch1 is not a leaf insn
Given `str`, if `str_coderange(str)` is `ENC_CODERANGE_BROKEN`,
it calls `rb_raise`. And it calls `rb_funcallv` from `rb_exc_new3`.

http://ci.rvm.jp/results/trunk-vm-asserts@silicon-docker/1673244

Maybe we can have a function to directly call `exc_initialize` for this
purpose, but it may not be worth having such a function for keeping the
instruction leaf. We may even want to delete the insn
https://github.com/ruby/ruby/pull/1959.

I'm not sure whether compile.c could generate opt_regexpmatch2 for
invalid coderange string. Let's monitor that for a while.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66982 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-02-01 11:52:06 +00:00
shyouhei 8a098051c5 insns.def: mark exception-raising instructions non-leaf
These instructions were missed before.  The stack canary mechanism
(see r64677) can not detect rb_raise() because exceptions jump over
the canary liveness check.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66980 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-02-01 06:29:02 +00:00
tenderlove 5577a8776e insns.def (duparray, duphash): add dtrace hooks
They are considered Array and Hash creation events, so
allow dtrace (and systemtap) to track those creations.

Co-Authored-By: Eric Wong <e@80x24.org>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66767 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-09 23:04:00 +00:00
nobu 67c5747369 Method reference operator
Introduce the new operator for method reference, `.:`.
[Feature #12125] [Feature #13581]
[EXPERIMENTAL]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66667 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-31 15:00:37 +00:00
shyouhei 24b1b433c5 vm_insnhelper.c: delete unused macros
- FIXNUM_2_P: moved to vm_insnhelper.c because that is the only
  place this macro is used.

- FLONUM_2_P: ditto.

- FLOAT_HEAP_P: not used anywhere.

- FLOAT_INSTANCE_P: ditto.

- GET_TOS: ditto.

- USE_IC_FOR_SPECIALIZED_METHOD: ditto.

- rb_obj_hidden_p: ditto.

- REG_A: ditto.

- REG_B: ditto.

- GET_CONST_INLINE_CACHE: ditto.

- vm_regan_regtype: moved inside of VM_COLLECT_USAGE_DETAILS
  because that os the only place this enum is used.

- vm_regan_acttype: ditto.

- GET_GLOBAL: used only once.  Removed with replacing that usage.

- SET_GLOBAL: ditto.

- rb_method_definition_create: declaration moved to
  vm_insnhelper.c because that is the only place this declaration
  makes sense.

- rb_method_definition_set: ditto.

- rb_method_definition_eq: ditto.

- rb_make_no_method_exception: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66597 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 01:06:04 +00:00
shyouhei bc64df876e delete emacs mode lines [ci skip]
These settings are now covered by .dir-locals.el.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66584 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-27 06:12:09 +00:00
shyouhei d46ab95376 insns.def: refactor to avoid CALL_METHOD macro
These send and its variant instructions are the most frequently called
paths in the entire process.  Reducing macro expansions to make them
dedicated function called vm_sendish() is the main goal of this
changeset.  It reduces the size of vm_exec_coref from 25,552 bytes to
23,728 bytes on my machine.

I see no significant slowdown.

Fix: [GH-2056]

vanilla: ruby 2.6.0dev (2018-12-19 trunk 66449) [x86_64-darwin15]
ours: ruby 2.6.0dev (2018-12-19 refactor-send 66449) [x86_64-darwin15]
last_commit=insns.def: refactor to avoid CALL_METHOD macro
Calculating -------------------------------------
                         vanilla        ours
   vm2_defined_method     2.645M      2.823M i/s -      6.000M times in 5.109888s 4.783254s
           vm2_method     8.553M      8.873M i/s -      6.000M times in 1.579892s 1.524026s
   vm2_method_missing     3.772M      3.858M i/s -      6.000M times in 3.579482s 3.499220s
vm2_method_with_block     8.494M      8.944M i/s -      6.000M times in 1.589774s 1.509463s
      vm2_poly_method      0.571       0.607 i/s -       1.000 times in 3.947570s 3.733528s
   vm2_poly_method_ov      5.514       5.168 i/s -       1.000 times in 0.408156s 0.436169s
 vm3_clearmethodcache      2.875       2.837 i/s -       1.000 times in 0.783018s 0.793493s

Comparison:
                vm2_defined_method
                 ours:   2822555.4 i/s
              vanilla:   2644878.1 i/s - 1.07x  slower

                        vm2_method
                 ours:   8872947.8 i/s
              vanilla:   8553433.1 i/s - 1.04x  slower

                vm2_method_missing
                 ours:   3858192.3 i/s
              vanilla:   3772296.3 i/s - 1.02x  slower

             vm2_method_with_block
                 ours:   8943825.1 i/s
              vanilla:   8493955.0 i/s - 1.05x  slower

                   vm2_poly_method
                 ours:         0.6 i/s
              vanilla:         0.6 i/s - 1.06x  slower

                vm2_poly_method_ov
              vanilla:         5.5 i/s
                 ours:         5.2 i/s - 1.07x  slower

              vm3_clearmethodcache
              vanilla:         2.9 i/s
                 ours:         2.8 i/s - 1.01x  slower



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-26 00:59:37 +00:00
shyouhei 686881d383 add _sp_inc_helpers.erb [ci skip]
Just add more room for comments.  This is a pure refactoring that does
not change anything but readability.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66564 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-26 00:58:26 +00:00
ko1 2a70f68c05 hide iseq operand object for duphash. [Bug #15440]
* compile.c (compile_array): hide source Hash object.

* hash.c (rb_hash_resurrect): introduced to dup Hash object
  using rb_cHash.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66466 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-20 07:17:55 +00:00
tenderlove 2f37847820 Speed up hash literals by duping
This commit replaces the `newhashfromarray` instruction with a `duphash`
instruction.  Instead of allocating a new hash from an array stored in
the Instruction Sequences, store a hash directly in the instruction
sequences and dup it on execution.

== Instruction sequence changes ==

```ruby
code = <<-eorby
  { "foo" => "bar", "baz" => "lol" }
eorby

insns = RubyVM::InstructionSequence.compile(code, __FILE__, nil, 0, frozen_string_literal: true)
puts insns.disasm
```

On Ruby 2.5:

```
== disasm: #<ISeq:<compiled>@test.rb:0 (0,0)-(0,36)>====================
0000 putobject        "foo"
0002 putobject        "bar"
0004 putobject        "baz"
0006 putobject        "lol"
0008 newhash          4
0010 leave
```

Ruby 2.6@r66174 3b6321083a2e3525da3b34d08a0b68bac094bd7f:

```
$ ./ruby test.rb
== disasm: #<ISeq:<compiled>@test.rb:0 (0,0)-(0,36)> (catch: FALSE)
0000 newhashfromarray             2, ["foo", "bar", "baz", "lol"]
0003 leave
```

Ruby 2.6 + This commit:

```
$ ./ruby test.rb
== disasm: #<ISeq:<compiled>@test.rb:0 (0,0)-(0,36)> (catch: FALSE)
0000 duphash                      {"foo"=>"bar", "baz"=>"lol"}
0002 leave
```

== Benchmark Results ==

Compared to 2.5.3:

```
$ make benchmark ITEM=hash_literal_small COMPARE_RUBY=/Users/aaron/.rbenv/versions/2.5.3/bin/ruby
generating known_errors.inc
known_errors.inc unchanged
./revision.h unchanged
/Users/aaron/.rbenv/shims/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver \
	            --executables="compare-ruby::/Users/aaron/.rbenv/versions/2.5.3/bin/ruby -I.ext/common --disable-gem" \
	            --executables="built-ruby::./miniruby -I./lib -I. -I.ext/common  -r./prelude --disable-gem" \
	            $(find ./benchmark -maxdepth 1 -name '*hash_literal_small*.yml' -o -name '*hash_literal_small*.rb' | sort)
Calculating -------------------------------------
                     compare-ruby  built-ruby
 hash_literal_small2        1.498       1.877 i/s -       1.000 times in 0.667581s 0.532656s
 hash_literal_small4        1.197       1.642 i/s -       1.000 times in 0.835375s 0.609160s
 hash_literal_small8        0.620       1.215 i/s -       1.000 times in 1.611638s 0.823090s

Comparison:
              hash_literal_small2
          built-ruby:         1.9 i/s
        compare-ruby:         1.5 i/s - 1.25x  slower

              hash_literal_small4
          built-ruby:         1.6 i/s
        compare-ruby:         1.2 i/s - 1.37x  slower

              hash_literal_small8
          built-ruby:         1.2 i/s
        compare-ruby:         0.6 i/s - 1.96x  slower
```

Compared to r66255

```
$ make benchmark ITEM=hash_literal_small COMPARE_RUBY=/Users/aaron/.rbenv/versions/ruby-trunk/bin/ruby
generating known_errors.inc
known_errors.inc unchanged
./revision.h unchanged
/Users/aaron/.rbenv/shims/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver \
	            --executables="compare-ruby::/Users/aaron/.rbenv/versions/ruby-trunk/bin/ruby -I.ext/common --disable-gem" \
	            --executables="built-ruby::./miniruby -I./lib -I. -I.ext/common  -r./prelude --disable-gem" \
	            $(find ./benchmark -maxdepth 1 -name '*hash_literal_small*.yml' -o -name '*hash_literal_small*.rb' | sort)
Calculating -------------------------------------
                     compare-ruby  built-ruby
 hash_literal_small2        1.567       1.831 i/s -       1.000 times in 0.638056s 0.546039s
 hash_literal_small4        1.298       1.652 i/s -       1.000 times in 0.770214s 0.605182s
 hash_literal_small8        0.873       1.216 i/s -       1.000 times in 1.145304s 0.822047s

Comparison:
              hash_literal_small2
          built-ruby:         1.8 i/s
        compare-ruby:         1.6 i/s - 1.17x  slower

              hash_literal_small4
          built-ruby:         1.7 i/s
        compare-ruby:         1.3 i/s - 1.27x  slower

              hash_literal_small8
          built-ruby:         1.2 i/s
        compare-ruby:         0.9 i/s - 1.39x  slower
```

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66258 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-06 18:28:21 +00:00
mame b06649b912 Rename get/setinlinecache to opt_get/opt_setinlinecache
The instructions are just for optimization.  To clarity the intention,
this change adds the prefix "opt_", like "opt_case_dispatch".

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65600 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-07 08:13:20 +00:00
shyouhei 0f36bc093f insns.def: forgot add cast [ci skip]
See r65595


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65597 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-07 08:03:10 +00:00
shyouhei 48649e4625 insns.def: avoid integer overflow
In these expressions `1` is of type `signed int` (cf: ISO/IEC
9899:1990 section 6.1.3.2). The variable (e.g. `num`) is of type
`rb_num_t`, which is in fact `unsigned long`.  These two expressions
then exercises the "usual arithmetic conversions" (cf: ISO/IEC
9899:1990 section 6.2.1.5) and both eventually become `unsigned long`.
The two unsigned expressions are then subtracted to generate another
unsigned integer expression (cf: ISO/IEC 9899:1990 section 6.3.6).
This is where integer overflows can occur.  OTOH the left hand side of
the assignments are `rb_snum_t` which is `signed long`.  The
assignments exercise the "implicit conversion" of "an unsigned integer
is converted to its corresponding signed integer" case (cf: ISO/IEC
9899:1990 section 6.2.1.2), which is "implementation-defined" (read:
not portable).

Casts are the proper way to avoid this problem.  Because all
expressions are converted to some integer types before any binary
operations are performed, the assignments now have fully defined
behaviour.  These values can never exceed LONG_MAX so the casts must
not lose any information.

See also: https://travis-ci.org/ruby/ruby/jobs/451726874#L4357


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-07 07:16:50 +00:00
ko1 312b105d0e introduce TransientHeap. [Bug #14858]
* transient_heap.c, transient_heap.h: implement TransientHeap (theap).
  theap is designed for Ruby's object system. theap is like Eden heap
  on generational GC terminology. theap allocation is very fast because
  it only needs to bump up pointer and deallocation is also fast because
  we don't do anything. However we need to evacuate (Copy GC terminology)
  if theap memory is long-lived. Evacuation logic is needed for each type.

  See [Bug #14858] for details.

* array.c: Now, theap for T_ARRAY is supported.

  ary_heap_alloc() tries to allocate memory area from theap. If this trial
  sccesses, this array has theap ptr and RARRAY_TRANSIENT_FLAG is turned on.
  We don't need to free theap ptr.

* ruby.h: RARRAY_CONST_PTR() returns malloc'ed memory area. It menas that
  if ary is allocated at theap, force evacuation to malloc'ed memory.
  It makes programs slow, but very compatible with current code because
  theap memory can be evacuated (theap memory will be recycled).

  If you want to get transient heap ptr, use RARRAY_CONST_PTR_TRANSIENT()
  instead of RARRAY_CONST_PTR(). If you can't understand when evacuation
  will occur, use RARRAY_CONST_PTR().

(re-commit of r65444)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 21:53:56 +00:00
ko1 7d359f9b69 revert r65444 and r65446 because of commit miss
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65447 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 21:01:55 +00:00
ko1 90ac549fa6 introduce TransientHeap. [Bug #14858]
* transient_heap.c, transient_heap.h: implement TransientHeap (theap).
  theap is designed for Ruby's object system. theap is like Eden heap
  on generational GC terminology. theap allocation is very fast because
  it only needs to bump up pointer and deallocation is also fast because
  we don't do anything. However we need to evacuate (Copy GC terminology)
  if theap memory is long-lived. Evacuation logic is needed for each type.

  See [Bug #14858] for details.

* array.c: Now, theap for T_ARRAY is supported.

  ary_heap_alloc() tries to allocate memory area from theap. If this trial
  sccesses, this array has theap ptr and RARRAY_TRANSIENT_FLAG is turned on.
  We don't need to free theap ptr.

* ruby.h: RARRAY_CONST_PTR() returns malloc'ed memory area. It menas that
  if ary is allocated at theap, force evacuation to malloc'ed memory.
  It makes programs slow, but very compatible with current code because
  theap memory can be evacuated (theap memory will be recycled).

  If you want to get transient heap ptr, use RARRAY_CONST_PTR_TRANSIENT()
  instead of RARRAY_CONST_PTR(). If you can't understand when evacuation
  will occur, use RARRAY_CONST_PTR().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65444 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 20:46:24 +00:00
shyouhei c80f3f709f less verbose code by sharing attribute definitions
The idea behind this commit is that handles_sp and leaf are two
concepts that are not mutually independent.  By making one explicitly
depend another, we can reduces the number of lines of codes written,
thus making things concise.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-29 03:21:22 +00:00
ko1 6fa2b5e8d4 newhashfromarray should be a leaf insn.
* insns.def (newhashfromarray): `rb_hash_bulk_insert()` can call
  Ruby methods like #hash so that it should not be a leaf insn.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65345 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-24 02:40:13 +00:00
ko1 434207e84d need a cast
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65344 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-24 02:12:35 +00:00
ko1 f3c5239b16 introduce new YARV insn newhashfromarray.
* insns.def (newhashfromarray): added to replace `core_hash_from_ary`
  method to eliminate method call overhead.

  On my environment, I got the following benchmark results:

  x = {x: 1}

                    modified:   7864988.6 i/s
                       trunk:   6004098.1 i/s - 1.31x  slower


  x = {x: 1, y: 2}

                       trunk:   6127338.4 i/s
                    modified:   5232380.0 i/s - 1.17x  slower


  x = {x: 1, y: 2, z: 3}

                    modified:   6089553.1 i/s
                       trunk:   5249333.5 i/s - 1.16x  slower

  This trivial improvement should be reconsider because of usage of
  this instruction.

* compile.c: ditto.

* defs/id.def, vm.c: remove unused functions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65343 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-24 01:57:27 +00:00
mame 6c9a705032 Remove tracecoverage instructions
The instructions were used only for branch coverage.
Instead, it now uses a trace framework [Feature #14104].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-20 10:45:48 +00:00
ko1 d3a9ebeb1c fix OPT_CALL_THREADED_CODE issue.
* insns.def (opt_send_without_block): reorder insn position because
  `opt_str_freeze` insn refer this insn (function) when
  OPT_CALL_THREADED_CODE is true.

* vm_opts.h (OPT_THREADED_CODE): introduce new macro to select
  threaded code implementation with a compile option (-D...).


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-26 08:11:05 +00:00
k0kubun 6e62e59eec revert r64847, r64846 and r64839
because r64849 seems to fix issues which we were confused about.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64850 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-26 02:38:45 +00:00
k0kubun e08f418230 revert r64838 and r64839
because some build failures persisted

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-26 01:11:20 +00:00
k0kubun 2b610ec285 insns.def: drop bitblt insn
as a workaround to fix the build pipeline broken by r64824,
because optimizing Ruby should be prioritized higher than supporting unused jokes.

In the current build system, exceeding 200 insns somehow crashes C
extension build on some of MinGW environments like "mingw32-make[1]:
*** No rule to make target 'note'.  Stop."
https://ci.appveyor.com/project/ruby/ruby/build/9725/job/co4nu9jugm8qwdrp
and on some of Linux environments like "cannot load such file -- stringio (LoadError)"

```
build_install        /home/ko1/ruby/src/trunk_gcc5/lib/rubygems/specification.rb:18:in `require': cannot load such file -- stringio (LoadError)
	from /home/ko1/ruby/src/trunk_gcc5/lib/rubygems/specification.rb:18:in `<top (required)>'
	from /home/ko1/ruby/src/trunk_gcc5/lib/rubygems.rb:1365:in `require'
	from /home/ko1/ruby/src/trunk_gcc5/lib/rubygems.rb:1365:in `<module:Gem>'
	from /home/ko1/ruby/src/trunk_gcc5/lib/rubygems.rb:116:in `<top (required)>'
	from /home/ko1/ruby/src/trunk_gcc5/tool/rbinstall.rb:24:in `require'
	from /home/ko1/ruby/src/trunk_gcc5/tool/rbinstall.rb:24:in `<main>'
make: *** [do-install-nodoc] Error 1
```

http://ci.rvm.jp/results/trunk_gcc5@silicon-docker/1353447

This commit removes "bitblt" and "trace_bitblt" insns, which reduces the
number of insns from 202 to 200 and fixes at least the latter build
failure. I hope this fixes the MinGW build failure as well. Let me
confirm the situation on AppVeyor CI.

Note that this is hard to fix because some MinGW environments (MSP-Greg's
MinGW CI on AppVeyor) don't reproduce this and some Linux environments
(including my local machine) don't reproduce it either. Make sure you
have the reproductive environment and confirm it's fixed when reverting
this commit.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64839 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-25 17:20:02 +00:00
k0kubun 08c9f030f6 Revert "Revert r64824 to fix build failure on AppVeyor"
This reverts commit r64829. I'll prepare another temporary fix, but I'll
separately commit that to make it easier to revert that later.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64838 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-25 17:19:51 +00:00
k0kubun f00bf24272 Revert r64824 to fix build failure on AppVeyor
AppVeyor msys2/MinGW build started to fail like:
https://ci.appveyor.com/project/ruby/ruby/build/9722/job/b94kixi004klmye3

Until I can investigate that, I revert this for now.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64829 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-25 04:03:35 +00:00
k0kubun fb80f6c7ba insns.def: optimize & and | of Integer [experimental]
not optimizing Array#& and Array#| because vm_insnhelper.c can't easily
inline it (large amount of array.c code would be needed in vm_insnhelper.c)
and the method body is a little complicated compared to Integer's ones.
So I thought only Integer#& and Integer#| have a significant impact,
and eliminating unnecessary branches would contribute to JIT's performance.

vm_insnhelper.c: ditto

tool/transform_mjit_header.rb: make sure these instructions are inlined
on JIT.

compile.c: compile vm_opt_and and vm_opt_or.
id.def: define id for them to be used in compile.c and vm*.c
vm.c: track redefinition of Integer#& and Integer#|
vm_core.h: allow detecting redefinition of & and |

test/ruby/test_jit.rb: test new insns
test/ruby/test_optimization.rb: ditto

* Optcarrot benchmark

This is a kind of experimental thing but I'm committing this since the
performance impact is significant especially on Optcarrot with JIT.

$ benchmark-driver benchmark.yml --rbenv 'before::before --disable-gems;before+JIT::before --disable-gems --jit;after::after --disable-gems;after+JIT::after --disable-gems --jit' -v --repeat-count 24
before: ruby 2.6.0dev (2018-09-24 trunk 64821) [x86_64-linux]
before+JIT: ruby 2.6.0dev (2018-09-24 trunk 64821) +JIT [x86_64-linux]
after: ruby 2.6.0dev (2018-09-24 opt_and 64821) [x86_64-linux]
last_commit=opt_or
after+JIT: ruby 2.6.0dev (2018-09-24 opt_and 64821) +JIT [x86_64-linux]
last_commit=opt_or
Calculating -------------------------------------
                             before  before+JIT       after   after+JIT
Optcarrot Lan_Master.nes     51.460      66.315      53.023      71.173 fps

Comparison:
             Optcarrot Lan_Master.nes
               after+JIT:        71.2 fps
              before+JIT:        66.3 fps - 1.07x  slower
                   after:        53.0 fps - 1.34x  slower
                  before:        51.5 fps - 1.38x  slower

[close https://github.com/ruby/ruby/pull/1963]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64824 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-24 12:40:28 +00:00
shyouhei 08af3f1b39 forgot to expand tabs [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64739 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-14 07:57:19 +00:00
shyouhei 9c6bd0d840 move ADD_PC around (take 2)
Now that we can say for sure if an instruction calls a method or
not internally, it is now possible to reroute the bugs that
forced us to revert the "move PC around" optimization.

First try: r62051
Reverted:  r63763
See also:  r63999

----

trunk: ruby 2.6.0dev (2018-09-13 trunk 64736) [x86_64-darwin15]
ours: ruby 2.6.0dev (2018-09-13 trunk 64736) [x86_64-darwin15]
last_commit=move ADD_PC around (take 2)
Calculating -------------------------------------
                           trunk        ours
         so_ackermann      1.884       2.278 i/s -       1.000 times in 0.530926s 0.438935s
             so_array      1.178       1.157 i/s -       1.000 times in 0.848786s 0.864467s
      so_binary_trees      0.176       0.177 i/s -       1.000 times in 5.683895s 5.657707s
       so_concatenate      0.220       0.221 i/s -       1.000 times in 4.546896s 4.518949s
       so_count_words      6.729       6.470 i/s -       1.000 times in 0.148602s 0.154561s
         so_exception      3.324       3.688 i/s -       1.000 times in 0.300872s 0.271147s
          so_fannkuch      0.546       0.968 i/s -       1.000 times in 1.831328s 1.033376s
             so_fasta      0.541       0.547 i/s -       1.000 times in 1.849923s 1.827091s
      so_k_nucleotide      0.800       0.777 i/s -       1.000 times in 1.250635s 1.286295s
             so_lists      2.101       1.848 i/s -       1.000 times in 0.475954s 0.541095s
        so_mandelbrot      0.435       0.408 i/s -       1.000 times in 2.299328s 2.450535s
            so_matrix      1.946       1.912 i/s -       1.000 times in 0.513872s 0.523076s
    so_meteor_contest      0.311       0.317 i/s -       1.000 times in 3.219297s 3.152052s
             so_nbody      0.746       0.703 i/s -       1.000 times in 1.339815s 1.423441s
       so_nested_loop      0.899       0.901 i/s -       1.000 times in 1.111767s 1.109555s
            so_nsieve      0.559       0.579 i/s -       1.000 times in 1.787763s 1.726552s
       so_nsieve_bits      0.435       0.428 i/s -       1.000 times in 2.296282s 2.333852s
            so_object      1.368       1.442 i/s -       1.000 times in 0.731237s 0.693684s
      so_partial_sums      0.616       0.546 i/s -       1.000 times in 1.623592s 1.833097s
          so_pidigits      0.831       0.832 i/s -       1.000 times in 1.203117s 1.202334s
            so_random      2.934       2.724 i/s -       1.000 times in 0.340791s 0.367150s
so_reverse_complement      0.583       0.866 i/s -       1.000 times in 1.714144s 1.154615s
             so_sieve      1.829       2.081 i/s -       1.000 times in 0.546607s 0.480562s
      so_spectralnorm      0.524       0.558 i/s -       1.000 times in 1.908716s 1.792382s



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64737 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-14 07:44:44 +00:00
shyouhei 0f6708eb9b resurrect the string to expect modifications
String#freeze can be redefined to be destructive.  While such
redefinition is definitely weird, it should be possible.  Resurrect
the string to prepare for that sort of things.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 04:04:31 +00:00
shyouhei 02b52b2733 make opt_str_freeze leaf
Simply use DISPATCH_ORIGINAL_INSN instead of rb_funcall.  This is,
when possible, overall performant because method dispatch results are
cached inside of CALL_CACHE.  Should also be good for JIT.

----

trunk: ruby 2.6.0dev (2018-09-12 trunk 64689) [x86_64-darwin15]
ours: ruby 2.6.0dev (2018-09-12 leaf-insn 64688) [x86_64-darwin15]
last_commit=make opt_str_freeze leaf
Calculating -------------------------------------
                          trunk        ours
    vm2_freezestring     5.440M     31.411M i/s -      6.000M times in 1.102968s 0.191017s

Comparison:
                 vm2_freezestring
                ours:  31410864.5 i/s
               trunk:   5439865.4 i/s - 5.77x  slower



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64690 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 03:39:36 +00:00
shyouhei 33c8171c65 make opt_case_dispatch leaf
This instruction can be written without rb_funcall. It not only boosts
performance of case statements, but also makes room of future JIT
improvements.  Because opt_case_dispatch is about optimization this
should not be a bad thing to have.

----

trunk: ruby 2.6.0dev (2018-09-05 trunk 64634) [x86_64-darwin15]
ours: ruby 2.6.0dev (2018-09-12 leaf-insn 64688) [x86_64-darwin15]
last_commit=make opt_case_dispatch leaf
Calculating -------------------------------------
                          trunk        ours
        vm2_case_lit      1.366       2.012 i/s -       1.000 times in 0.731839s 0.497008s

Comparison:
                     vm2_case_lit
                ours:         2.0 i/s
               trunk:         1.4 i/s - 1.47x  slower



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64689 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 01:55:00 +00:00
shyouhei c2bfb4e93c add new instruction attribute called leaf
An instruction is leaf if it has no rb_funcall inside.  In order to
check this property, we introduce stack canary which is a random
number collected at runtime.  Stack top is always filled with this
number and checked for stack smashing operations, when VM_CHECK_MODE.
[GH-1947]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-11 09:48:58 +00:00
k0kubun 0f0d7805cb vm_args.c: stop requiring `calling` in vm_caller_setup_arg_block
_mjit_compile_send.erb: simplify code using the change

insns.def: adapt to the interface change

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64281 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-11 03:50:42 +00:00
mame 2138f24c70 insns.def (invokesuper): remove a dummy receiever flag hack for ZSUPER
This is just a refactoring.

The receiver of "invokesuper" was a boolean to represent if it is ZSUPER
or not.  This was used in vm_search_super_method to prohibit ZSUPER call
in define_method. (It is currently prohibited because of the limitation
of the implementation.)

This change removes the hack by introducing an explicit flag,
VM_CALL_SUPER, to signal the information.  Now, the implementation of
"invokesuper" is consistent with "send" instruction.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64268 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-10 07:45:16 +00:00
mame ca494df3e0 insns.def: remove old wrong explanation for get/setconstant
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64074 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-27 06:28:14 +00:00
k0kubun a763bc3c6b insns.def: s/handles_frame/handles_sp/
because it's more suitable to describe the current behavior now.

tool/ruby_vm/models/bare_instructions.rb: ditto.
tool/ruby_vm/views/_insn_entry.erb: ditto.
tool/ruby_vm/views/_mjit_compile_insn_body.erb: ditto.
tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64053 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-25 14:55:43 +00:00
k0kubun c86fc2bba5 mjit_compile.c: reduce sp motion on JIT
This retries r62655, which was reverted at r63863 for r63763.

tool/ruby_vm/views/_mjit_compile_insn.erb: revert the revert.
tool/ruby_vm/views/_mjit_compile_insn_body.erb: ditto.
tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb: ditto.
tool/ruby_vm/views/_mjit_compile_send.erb: ditto.
tool/ruby_vm/views/mjit_compile.inc.erb: ditto.

tool/ruby_vm/views/_insn_entry.erb: revert half of r63763. The commit
  was originally reverted since changing pc motion was bad for tracing,
  but changing sp motion was totally fine. For JIT, I wanna resurrect
  the sp motion change in r62051.
tool/ruby_vm/models/bare_instructions.rb: ditto.
insns.def: ditto.
vm_insnhelper.c: ditto.
vm_insnhelper.h: ditto.

* benchmark

$ benchmark-driver benchmark.yml --rbenv 'before;after;before --jit;after --jit' --repeat-count 12 -v
before: ruby 2.6.0dev (2018-07-19 trunk 63998) [x86_64-linux]
after: ruby 2.6.0dev (2018-07-19 add-sp 63998) [x86_64-linux]
last_commit=mjit_compile.c: reduce sp motion on JIT
before --jit: ruby 2.6.0dev (2018-07-19 trunk 63998) +JIT [x86_64-linux]
after --jit: ruby 2.6.0dev (2018-07-19 add-sp 63998) +JIT [x86_64-linux]
last_commit=mjit_compile.c: reduce sp motion on JIT
Calculating -------------------------------------
                             before       after  before --jit  after --jit
Optcarrot Lan_Master.nes     51.354      50.238        70.010       72.139 fps

Comparison:
             Optcarrot Lan_Master.nes
             after --jit:        72.1 fps
            before --jit:        70.0 fps - 1.03x  slower
                  before:        51.4 fps - 1.40x  slower
                   after:        50.2 fps - 1.44x  slower

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63999 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-19 13:25:22 +00:00
k0kubun 80dac806cc revert r63988
Due to trunk-mjit CI failures:
http://ci.rvm.jp/results/trunk-mjit@silicon-docker/1130097
http://ci.rvm.jp/results/trunk-mjit@silicon-docker/1130196

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63991 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-17 16:20:15 +00:00
k0kubun 5aa52587e8 insns.def: remove redundant ifndef in r63988
By the way, the original patch of r63988 was provided by wanabe:
https://github.com/wanabe/ruby/tree/local-stack

but I forgot to add his credit in the previous commit message.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-17 15:15:31 +00:00
k0kubun 6a4bb345df mjit_compile.c: resurrect local variable stack
This optimization was reverted on r63863, but this commit resurrects the
optimization to skip some sp motions on JIT execution.

tool/ruby_vm/views/_mjit_compile_insn_body.erb: ditto
tool/ruby_vm/views/_mjit_compile_insn.erb: ditto

insns.def: resurrect handles_frame as handles_stack, which was deleted
on r63763.
tool/ruby_vm/models/bare_instructions.rb: ditto

vm_insnhelper.c: prevent moving sp outside insns.def to allow modifying
it by JIT.

* Optcarrot benchmark

$ benchmark-driver benchmark.yml --rbenv 'before --jit;after --jit' --repeat-count 12 -v
before --jit: ruby 2.6.0dev (2018-07-17 trunk 63987) +JIT [x86_64-linux]
after --jit: ruby 2.6.0dev (2018-07-17 local-stack 63987) +JIT [x86_64-linux]
last_commit=mjit_compile.c: resurrect local variable stack
Calculating -------------------------------------
                         before --jit  after --jit
Optcarrot Lan_Master.nes       70.518       72.144 fps

Comparison:
             Optcarrot Lan_Master.nes
             after --jit:        72.1 fps
            before --jit:        70.5 fps - 1.02x  slower

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63988 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-17 15:09:41 +00:00
ktsj 78a2a1be76 insns.def: fix typo in comment
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-15 09:48:09 +00:00
k0kubun 8bec3e1fe2 insns.def: stop pushing unnecessary keys for MJIT
[Bug #14892]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63875 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-07 14:41:03 +00:00
k0kubun 7a0a585444 revert r62655 for r63763
r63655 was tightly coupled to handle_frames and some assumptions seems
to have been broken by r63763.

To partially resolve Bug#14892, this reverts the optimization for now. I
want to make MJIT CI happy first and then I'll probably retry r63655 by
partially reverting r63763 for sp changes.

The skipped test is not fixed yet.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63863 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-05 15:56:48 +00:00
shyouhei 6b534134a7 give up insn attr handles_frame
I introduced this mechanism in r62051 to speed things up. Later it
was reported that the change causes problems.  I searched for
workarounds but nothing seemed appropriate.  I hereby officially
give it up.  The idea to move ADD_PC around was a mistake.

Fixes [Bug #14809] and [Bug #14834].

Signed-off-by: Urabe, Shyouhei <shyouhei@ruby-lang.org>


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63763 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-27 09:28:09 +00:00
shyouhei 529af9c821 refactor move logics out of insns.def
This is a pure refactoring.  I see no difference in this change.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-27 01:10:02 +00:00
shyouhei 22444ae9b1 move function declarations from insns.def to internal.h
Just avoid being loose.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63755 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-27 00:57:16 +00:00
nobu a3fe1034c4 insns.def: checktype
* insns.def (checktype): split branchiftype to checktype and
  branchif, to make branch condition negation possible.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-21 10:52:52 +00:00
tenderlove 9e26858e8c Reverting r62775, this should fix i686 builds
We need to mark default values for kwarg methods.  This also fixes
Bootsnap.  IBF iseq loading needed to mark iseqs as "having markable
objects".

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62851 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-19 18:21:54 +00:00
naruse 94c40622f5 Revert "Add direct marking on iseq operands"
This reverts commit r62706.

It causes SEGV on i686-linux (debian) and armv7l-linux-eabihf:
http://www.rubyist.net/~akr/chkbuild/debian/ruby-trunk/log/20180309T204300Z.diff.html.gz
http://rubyci.s3.amazonaws.com/scw-9d6766/ruby-trunk/log/20180309T211706Z.diff.html.gz

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62775 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-16 07:59:10 +00:00
tenderlove 8952964976 Add direct marking on iseq operands
Directly marking iseq operands allows us to eliminate the "mark array"
stored on ISEQ objects, which will reduce the amount of memory ISEQ
objects consume.  This patch changes the iseq mark function to:

* Directly marks ISEQ operands
* Iterate over and mark child ISEQs

It also introduces two flags on the ISEQ object.  In order to mark
instruction operands, we have to disassemble the instructions and find
the instruction parameters and types.  Instructions may also be
translated to jump addresses.  Instruction sequences may get marked by
the GC *while* they're mid flight (being compiled).  The
`ISEQ_TRANSLATED` flag is used to indicate whether or not the
instructions have been translated to jump addresses so that when we
decode the instructions we know whether or not we need to go from jump
location back to original instruction or not.

Not all ISEQ objects have any markable objects embedded in their
instructions.  We can detect whether or not an ISEQ has markable objects
in the instructions at compile time.  If the instructions contain
markable objects, we set a flag `ISEQ_MARKABLE_ISEQ` on the ISEQ object.
This means that during the mark phase, we can skip decompilation if the
flag is *not* set.  In other words, we can avoid decompilation of we
know in advance there is nothing to mark.

`once` instructions have an operand that contains the result of a
one-time compilation of a regex.  Before this patch, that operand was
called an "inline cache", even though the struct was actually an "inline
storage".  This patch changes the operand to be an "inline storage" so
that we can differentiate between caches that need marking (the inline
storage) and caches that don't need marking (inline cache).

[ruby-core:84909]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62706 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-09 20:11:45 +00:00
k0kubun 8a15857a7f mjit_compile.c: use local variables for stack
if catch_except_p is FALSE. If catch_except_p is TRUE, stack values
should be on VM's stack when exception is thrown and the JIT-ed frame
is re-executed by VM's exception handler. If it's FALSE, the JIT-ed
frame won't be re-executed and don't need to keep values on VM's stack.

Using local variables allows us to reduce cfp->sp motion. Moving cfp->sp
is needed only for insns whose handles_frame? is false. So it improves
performance.

_mjit_compile_insn.erb: Prepare `stack_size` variable for GET_SP,
STACK_ADDR_FROM_TOP, TOPN macros. Share pc and sp motion partial view.
Use cancel handler created in mjit_compile.c.

_mjit_compile_send.erb: ditto. Also, when iseq->body->catch_except_p is
TRUE, this stops to call mjit_exec directly. I described the reason in
vm_insnhelper.h's comment for EXEC_EC_CFP.

_mjit_compile_pc_and_sp.erb: Shared logic for moving sp and pc. As you
can see from thsi file, when status->local_stack_p is TRUE and
insn.handles_frame? is false, moving sp is skipped. But if
insn.handles_frame? is true, values should be rolled back to VM's stack.
common.mk: add dependency for the file

_mjit_compile_insn_body.erb: Set sp value before canceling JIT on
DISPATCH_ORIGINAL_INSN. Replace GET_SP, STACK_ADDR_FROM_TOP, TOPN macros
for the case ocal_stack_p is TRUE and insn.handles_frame? is false.
In that case, values are not available on VM's stack and those macros
should be replaced.

mjit_compile.inc.erb: updated comments of macros which are supported by
JIT compiler. All references to `cfp->sp` should be replaced and thus
INC_SP, SET_SV, PUSH are no longer supported for now, because they are
not used now.

vm_exec.h: moved EXEC_EC_CFP definition to vm_insnhelper.h because it's
tighly coupled to CALL_METHOD.

vm_insnhelper.h: Have revised EXEC_EC_CFP definition moved from vm_exec.h.
Now it triggers mjit_exec for VM, and has the guard for catch_except_p
on JIT-ed code. See comments for details. CALL_METHOD delegates
triggering mjit_exec to EXEC_EC_CFP.

insns.def: Stopped using EXEC_EC_CFP for the case we don't want to
trigger mjit_exec. Those insns (defineclass, opt_call_c_function) are
not supported by JIT and it's safe to use RESTORE_REGS(), NEXT_INSN().
expandarray is changed to pass GET_SP() to replace the macro in
_mjit_compile_insn_body.erb.

vm_insnhelper.c: change to take sp for the above reason.

[close https://github.com/ruby/ruby/pull/1828]

This patch resurrects the performance which was attached in
[Feature #14235].

* Benchmark

Optcarrot (with configuration for benchmark_driver.gem)
https://github.com/benchmark-driver/optcarrot

$ benchmark-driver benchmark.yml --verbose 1 --rbenv 'before;before+JIT::before,--jit;after;after+JIT::after,--jit' --repeat-count 10
before: ruby 2.6.0dev (2018-03-04 trunk 62652) [x86_64-linux]
before+JIT: ruby 2.6.0dev (2018-03-04 trunk 62652) +JIT [x86_64-linux]
after: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
after+JIT: ruby 2.6.0dev (2018-03-04 local-variable.. 62652) +JIT [x86_64-linux]
last_commit=mjit_compile.c: use local variables for stack
Calculating -------------------------------------
                         before  before+JIT       after   after+JIT
           optcarrot     53.552      59.680      53.697      63.358 fps

Comparison:
                        optcarrot
           after+JIT:        63.4 fps
          before+JIT:        59.7 fps - 1.06x  slower
               after:        53.7 fps - 1.18x  slower
              before:        53.6 fps - 1.18x  slower

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62655 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-04 07:04:40 +00:00
k0kubun fc2764e58c insns.def: unwrap vm_exec for yield
Outer vm_exec can catch longjmp. We don't need to call vm_exec first
here.

This optimizes JIT-ed yield:

* Benchmark script

```
require 'benchmark_driver'

Benchmark.driver do |x|
  x.prelude %{
    def yielder
      yield + 1
    end
  }
  x.report 'yielder', %{
    yielder { 1 }
  }
  x.loop_count 300_000_000

  x.rbenv 'before', 'before,--jit', 'after', 'after,--jit'
  x.verbose
end
```

* Result

before: ruby 2.6.0dev (2018-03-03 trunk 62642) [x86_64-linux]
before,--jit: ruby 2.6.0dev (2018-03-03 trunk 62642) +JIT [x86_64-linux]
after: ruby 2.6.0dev (2018-03-03 trunk 62642) [x86_64-linux]
last_commit=insns.def: unwrap vm_exec for yield
after,--jit: ruby 2.6.0dev (2018-03-03 trunk 62642) +JIT [x86_64-linux]
last_commit=insns.def: unwrap vm_exec for yield
Calculating -------------------------------------
                         before  before,--jit       after  after,--jit
             yielder    37.214M       29.222M     35.904M      38.035M i/s -    300.000M times in 8.061581s 10.266312s 8.355716s 7.887447s

Comparison:
                          yielder
         after,--jit:  38035121.0 i/s
              before:  37213544.0 i/s - 1.02x  slower
               after:  35903565.7 i/s - 1.06x  slower
        before,--jit:  29221787.6 i/s - 1.30x  slower

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62643 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-03 04:50:14 +00:00
k0kubun b7aae52851 vm.c: add mjit_enable_p flag
to count up total calls properly. Some places (especially CALL_METHOD)
invoke mjit_exec twice for one method call. It would be problematic when
debugging, or possibly it would result in a wrong profiling result.

This commit doesn't have impact for performance:

* Optcarrot benchmark

** before

fps: 59.37757770848619
fps: 56.49998488958699
fps: 59.07900362739362
fps: 58.924749807695996
fps: 57.667905665594894
fps: 57.540021018385254
fps: 59.5518055679647
fps: 55.93831555148311
fps: 57.82685112863262
fps: 59.22391754481736
checksum: 59662

** after

fps: 58.461881158098194
fps: 59.32685183081354
fps: 54.11334310279802
fps: 59.2281560439788
fps: 58.60495705318312
fps: 55.696478648491045
fps: 58.49003452654724
fps: 58.387771929393224
fps: 59.24156772816439
fps: 56.68804731968107
checksum: 59662

* Discourse

Your Results: (note for timings- percentile is first, duration is second in millisecs)

** before (without JIT)

categories_admin:
  50: 16
  75: 17
  90: 24
  99: 37
home_admin:
  50: 20
  75: 20
  90: 24
  99: 42
topic_admin:
  50: 16
  75: 16
  90: 18
  99: 28
categories:
  50: 36
  75: 37
  90: 45
  99: 68
home:
  50: 38
  75: 40
  90: 53
  99: 92
topic:
  50: 14
  75: 15
  90: 17
  99: 26

** after (without JIT)

categories_admin:
  50: 16
  75: 16
  90: 24
  99: 36
home_admin:
  50: 19
  75: 20
  90: 23
  99: 41
topic_admin:
  50: 16
  75: 16
  90: 19
  99: 33
categories:
  50: 35
  75: 36
  90: 44
  99: 61
home:
  50: 38
  75: 40
  90: 52
  99: 101
topic:
  50: 14
  75: 15
  90: 15
  99: 24

** before (with JIT)

categories_admin:
  50: 19
  75: 23
  90: 29
  99: 44
home_admin:
  50: 24
  75: 26
  90: 32
  99: 46
topic_admin:
  50: 20
  75: 22
  90: 27
  99: 44
categories:
  50: 41
  75: 43
  90: 51
  99: 66
home:
  50: 46
  75: 49
  90: 56
  99: 68
topic:
  50: 18
  75: 19
  90: 22
  99: 31

** after (with JIT)

categories_admin:
  50: 18
  75: 21
  90: 28
  99: 42
home_admin:
  50: 23
  75: 25
  90: 31
  99: 51
topic_admin:
  50: 19
  75: 20
  90: 24
  99: 31
categories:
  50: 41
  75: 44
  90: 52
  99: 69
home:
  50: 45
  75: 48
  90: 61
  99: 88
topic:
  50: 19
  75: 20
  90: 24
  99: 33

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62641 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-03 04:07:02 +00:00
k0kubun 1012e50ac7 insns.def: remove unnecessary sp motion
This seems obsoleted after r62087.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62387 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-12 09:05:12 +00:00
nobu aea14e68fb insns.def: cache nil const
* insns.def (getinlinecache): Qnil is a valid value as a constant.
  this can be observable when accessing a deprecated constant
  which is nil.  non-nil constant is warned just once for each
  location, but every time if it is nil.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62350 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-10 16:54:47 +00:00
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
shyouhei c788fb4808 eliminate CALL_SIMPLE_METHOD
Arrange operands of several opt_something insns so that jumps to
opt_send_without_block can be applied to them. This makes it
possible to eliminate CALL_SIMPLE_METHOD macro at all.  Results
in binary size of vm_exec_core to change from 27,008 bytes to
26,016 bytes on my machine. [close GH-1779]

Note however that PC can point somewhere non-instruction now.

-----------------------------------------------------------
benchmark results:
minimum results in each 3 measurements.
Execution time (sec)
name    before  after
so_ackermann     0.450  0.426
so_array         0.789  0.824
so_binary_trees  5.760  5.635
so_concatenate   3.594  3.508
so_count_words   0.211  0.196
so_exception     0.256  0.244
so_fannkuch      1.049  1.044
so_fasta         1.485  1.472
so_k_nucleotide  1.195  1.216
so_lists         0.517  0.513
so_mandelbrot    2.264  2.394
so_matrix        0.501  0.468
so_meteor_contest        2.987  2.912
so_nbody         1.307  1.289
so_nested_loop   0.908  0.925
so_nsieve        1.679  1.614
so_nsieve_bits   2.131  2.092
so_object        0.620  0.625
so_partial_sums  1.623  1.675
so_pidigits      1.135  1.190
so_random        0.357  0.321
so_reverse_complement    0.619  0.583
so_sieve         0.493  0.496
so_spectralnorm  1.749  1.737

Speedup ratio: compare with the result of `before' (greater is better)
name    after
so_ackermann    1.057
so_array        0.958
so_binary_trees 1.022
so_concatenate  1.024
so_count_words  1.077
so_exception    1.049
so_fannkuch     1.004
so_fasta        1.009
so_k_nucleotide 0.983
so_lists        1.007
so_mandelbrot   0.946
so_matrix       1.072
so_meteor_contest       1.026
so_nbody        1.013
so_nested_loop  0.982
so_nsieve       1.040
so_nsieve_bits  1.018
so_object       0.992
so_partial_sums 0.969
so_pidigits     0.954
so_random       1.111
so_reverse_complement   1.062
so_sieve        0.994
so_spectralnorm 1.007

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62089 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-29 07:15:08 +00:00