314 Commits
Author SHA1 Message Date
Max Bernstein fb39511f19 ZJIT: Profile opt_case_dispatch (#19080)
In 7eab8b5d48 I wanted to avoid extra
work. Since we don't actually use any profile information in ZJIT for
compiling `opt_case_dispatch`, I decided not to call the profiling
instruction.

However, this caused a regression in `mail` and `rubocop`: this call to
the profiler is also our hook to the function lifecycle code, which
triggers a rewrite from the ZJIT version of the instruction back to the
normal bare opcode. My commit stopped rewriting back to the original
opcode when the ZJIT machinery should have.

Because in the interpreter the bare opcode path is much faster for
integer-only case/when dispatch, this regressed `mail` and `rubocop`.

We'll have to figure out why we are spending a lot of time in
interpreter case/when dispatch for those benchmarks (code size?), but
for now this fixes the regression.
2026-09-28 14:05:14 +00:00
Max Bernstein 7eab8b5d48 ZJIT: Add null support for opt_case_dispatch (#19040)
We are not going to optimized switch-based dispatch with the hash table, at
least not yet, but we should let the interpreter profile the execution paths
before ZJIT runs them.

This should inject a lot more type information for the compiler. For example,
on rubyboy, we go from ~1080ms/iter to ~820ms/iter.

Later, we can make the dispatch faster too.
2026-09-26 15:38:37 +00:00
Nikita Vasilevsky 2f79c3b054 Remove unused CEscape gensym helper (#18002)
gensym was copied into the rewritten VM generator helpers in f6ea3763, but no generated-code path calls it. Removing it also removes the now-unneeded securerandom dependency.
2026-07-21 23:28:38 -04:00
Takashi Kokubun bd4de1245f Explicitly declare VM instruction dependencies (#14509)
instead of mutating RubyVM::Instructions and letting the order of
`require` impact its behavior.

Now that we have both RubyVM::TraceInstruction and
RubyVM::ZJITInstruction, it feels too much of a tight coupling to rely
on `require` to be ordered properly.
2025-09-10 14:18:44 -07:00
Takashi Kokubun 0cec87399d Rename VM instruction classes to singular names (#14507) 2025-09-10 14:04:33 -07:00
Takashi Kokubun 4f030951f2 ZJIT: Invalidate local variables on EP escape (#14448) 2025-09-05 11:26:01 -07:00
Alan Wu ce20d68dae Mark leaf defined instructions as leaf
For example, `defined?(yield)` never calls a method, so it's leaf.
2025-09-05 13:29:17 -04:00
Takashi Kokubun 30f32a048c bare_instructions.rb: Remove unused always_leaf?
83bc6ced92 was the last use of it.
2025-09-05 09:24:56 -07:00
Takashi Kokubun 799d57bd01 insns.def: Drop unused leafness_of_check_ints
It was used to let MJIT override the leafness of the instruction when it
decides to remove check_ints for it. Now that MJIT is gone, nobody needs
to "override" the leafness using this.
2025-09-05 09:15:00 -07:00
Takashi Kokubun 83bc6ced92 Drop unused tool/ruby_vm/views/_insn_sp_pc_dependency.erb
MJIT was probably the last user of it. It's not even part of
build dependencies now.
2025-09-04 16:42:47 -07:00
Alan Wu aafd10616d Add an include guard for insns_info.inc
The JIT bindgens need this.
2025-05-02 23:47:57 +09:00
Takashi Kokubun 9cdc46587d Explain a missing USE_ZJIT check on rb_vm_max_insn_name_size 2025-04-29 11:10:49 -07:00
Takashi Kokubun 0f3d6ee578 ZJIT: Disable ZJIT instructions when USE_ZJIT is 0 (#13199)
* ZJIT: Disable ZJIT instructions when USE_ZJIT is 0

* Test the order of ZJIT instructions

* Add more jobs that disable JITs

* Show instruction names in the message
2025-04-29 11:03:13 -07:00
Takashi Kokubun 58e3aa0224 ZJIT: Drop trace_zjit_* instructions (#13189) 2025-04-28 09:25:56 -07:00
Takashi Kokubun 8b72e07359 Disable ZJIT profiling at call-threshold (https://github.com/Shopify/zjit/pull/99)
* Disable ZJIT profiling at call-threshold

* Stop referencing ZJIT instructions in codegen
2025-04-18 21:53:01 +09:00
Takashi Kokubun 0a543daf15 Add zjit_* instructions to profile the interpreter (https://github.com/Shopify/zjit/pull/16)
* Add zjit_* instructions to profile the interpreter

* Rename FixnumPlus to FixnumAdd

* Update a comment about Invalidate

* Rename Guard to GuardType

* Rename Invalidate to PatchPoint

* Drop unneeded debug!()

* Plan on profiling the types

* Use the output of GuardType as type refined outputs
2025-04-18 21:52:59 +09:00
Takashi Kokubun e8f8565dc2 Remove obsoleted insn_may_depend_on_sp_or_pc()
which was for MJIT
2025-03-05 16:23:31 -08:00
Nobuyoshi Nakada 0f8a6e1f59 Remove an unused empty directory [ci skip] 2025-02-21 11:01:24 +09:00
Nobuyoshi Nakada a64bf7e5d0 Remove useless shebang in non-executable files [ci skip] 2025-02-21 11:00:33 +09:00
Nobuyoshi Nakada f4481b9515 Remove useless shebang in non-executable file [ci skip] 2025-02-21 10:32:16 +09:00
Takashi Kokubun b2cf48f406 chmod -x tool/ruby_vm/models/bare_instructions.rb
We run only tool/insns2vm.rb as the executable. It seems confusing to
have an executable flag on this file.
2025-02-20 10:14:37 -08:00
Nobuyoshi Nakada 4a67ef09cc [Feature #21116] Extract RJIT as a third-party gem 2025-02-13 18:01:03 +09:00
cdf33ed5f3 Optimized forwarding callers and callees
This patch optimizes forwarding callers and callees. It only optimizes methods that only take `...` as their parameter, and then pass `...` to other calls.

Calls it optimizes look like this:

```ruby
def bar(a) = a
def foo(...) = bar(...) # optimized
foo(123)
```

```ruby
def bar(a) = a
def foo(...) = bar(1, 2, ...) # optimized
foo(123)
```

```ruby
def bar(*a) = a

def foo(...)
  list = [1, 2]
  bar(*list, ...) # optimized
end
foo(123)
```

All variants of the above but using `super` are also optimized, including a bare super like this:

```ruby
def foo(...)
  super
end
```

This patch eliminates intermediate allocations made when calling methods that accept `...`.
We can observe allocation elimination like this:

```ruby
def m
  x = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - x
end

def bar(a) = a
def foo(...) = bar(...)

def test
  m { foo(123) }
end

test
p test # allocates 1 object on master, but 0 objects with this patch
```

```ruby
def bar(a, b:) = a + b
def foo(...) = bar(...)

def test
  m { foo(1, b: 2) }
end

test
p test # allocates 2 objects on master, but 0 objects with this patch
```

How does it work?
-----------------

This patch works by using a dynamic stack size when passing forwarded parameters to callees.
The caller's info object (known as the "CI") contains the stack size of the
parameters, so we pass the CI object itself as a parameter to the callee.
When forwarding parameters, the forwarding ISeq uses the caller's CI to determine how much stack to copy, then copies the caller's stack before calling the callee.
The CI at the forwarded call site is adjusted using information from the caller's CI.

I think this description is kind of confusing, so let's walk through an example with code.

```ruby
def delegatee(a, b) = a + b

def delegator(...)
  delegatee(...)  # CI2 (FORWARDING)
end

def caller
  delegator(1, 2) # CI1 (argc: 2)
end
```

Before we call the delegator method, the stack looks like this:

```
Executing Line | Code                                  | Stack
---------------+---------------------------------------+--------
              1| def delegatee(a, b) = a + b           | self
              2|                                       | 1
              3| def delegator(...)                    | 2
              4|   #                                   |
              5|   delegatee(...)  # CI2 (FORWARDING)  |
              6| end                                   |
              7|                                       |
              8| def caller                            |
          ->  9|   delegator(1, 2) # CI1 (argc: 2)     |
             10| end                                   |
```

The ISeq for `delegator` is tagged as "forwardable", so when `caller` calls in
to `delegator`, it writes `CI1` on to the stack as a local variable for the
`delegator` method.  The `delegator` method has a special local called `...`
that holds the caller's CI object.

Here is the ISeq disasm fo `delegator`:

```
== disasm: #<ISeq:delegator@-e:1 (1,0)-(1,39)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] "..."@0
0000 putself                                                          (   1)[LiCa]
0001 getlocal_WC_0                          "..."@0
0003 send                                   <calldata!mid:delegatee, argc:0, FCALL|FORWARDING>, nil
0006 leave                                  [Re]
```

The local called `...` will contain the caller's CI: CI1.

Here is the stack when we enter `delegator`:

```
Executing Line | Code                                  | Stack
---------------+---------------------------------------+--------
              1| def delegatee(a, b) = a + b           | self
              2|                                       | 1
              3| def delegator(...)                    | 2
           -> 4|   #                                   | CI1 (argc: 2)
              5|   delegatee(...)  # CI2 (FORWARDING)  | cref_or_me
              6| end                                   | specval
              7|                                       | type
              8| def caller                            |
              9|   delegator(1, 2) # CI1 (argc: 2)     |
             10| end                                   |
```

The CI at `delegatee` on line 5 is tagged as "FORWARDING", so it knows to
memcopy the caller's stack before calling `delegatee`.  In this case, it will
memcopy self, 1, and 2 to the stack before calling `delegatee`.  It knows how much
memory to copy from the caller because `CI1` contains stack size information
(argc: 2).

Before executing the `send` instruction, we push `...` on the stack.  The
`send` instruction pops `...`, and because it is tagged with `FORWARDING`, it
knows to memcopy (using the information in the CI it just popped):

```
== disasm: #<ISeq:delegator@-e:1 (1,0)-(1,39)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] "..."@0
0000 putself                                                          (   1)[LiCa]
0001 getlocal_WC_0                          "..."@0
0003 send                                   <calldata!mid:delegatee, argc:0, FCALL|FORWARDING>, nil
0006 leave                                  [Re]
```

Instruction 001 puts the caller's CI on the stack.  `send` is tagged with
FORWARDING, so it reads the CI and _copies_ the callers stack to this stack:

```
Executing Line | Code                                  | Stack
---------------+---------------------------------------+--------
              1| def delegatee(a, b) = a + b           | self
              2|                                       | 1
              3| def delegator(...)                    | 2
              4|   #                                   | CI1 (argc: 2)
           -> 5|   delegatee(...)  # CI2 (FORWARDING)  | cref_or_me
              6| end                                   | specval
              7|                                       | type
              8| def caller                            | self
              9|   delegator(1, 2) # CI1 (argc: 2)     | 1
             10| end                                   | 2
```

The "FORWARDING" call site combines information from CI1 with CI2 in order
to support passing other values in addition to the `...` value, as well as
perfectly forward splat args, kwargs, etc.

Since we're able to copy the stack from `caller` in to `delegator`'s stack, we
can avoid allocating objects.

I want to do this to eliminate object allocations for delegate methods.
My long term goal is to implement `Class#new` in Ruby and it uses `...`.

I was able to implement `Class#new` in Ruby
[here](https://github.com/ruby/ruby/pull/9289).
If we adopt the technique in this patch, then we can optimize allocating
objects that take keyword parameters for `initialize`.

For example, this code will allocate 2 objects: one for `SomeObject`, and one
for the kwargs:

```ruby
SomeObject.new(foo: 1)
```

If we combine this technique, plus implement `Class#new` in Ruby, then we can
reduce allocations for this common operation.

Co-Authored-By: John Hawthorn <john@hawthorn.email>
Co-Authored-By: Alan Wu <XrXr@users.noreply.github.com>
2024-06-18 09:28:25 -07:00
Nobuyoshi Nakada e9a7801a93 Drop support for old ERB 2024-03-03 00:55:45 +09:00
Nobuyoshi Nakada 5ec1fc52c1 Escape non-ascii characters in prelude C comments
Non-ASCII code are often warned by localized compilers.
2023-08-24 21:12:51 +09:00
Takashi KokubunandAlan Wu e210b899dc Move the PC regardless of the leaf flag (#8232)
Co-authored-by: Alan Wu <alansi.xingwu@shopify.com>
2023-08-16 20:28:33 -07:00
Takashi Kokubun 38be9a9b72 Clean up OPT_STACK_CACHING (#8132) 2023-07-27 17:27:05 -07:00
John Hawthorn 617c9b4656 Rename opes to operands on RubyVM::BaseInstruction 2023-03-16 14:16:56 -07:00
John HawthornandAaron Patterson 8dbddd5bf8 Rename opes to operands
Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
2023-03-16 14:16:56 -07:00
John Hawthorn d454a590cc Re-add RJIT::Instruction#opes 2023-03-16 14:16:56 -07:00
Takashi Kokubun 6440d159b3 RJIT: Simplify RubyVM::RJIT::Instruction 2023-03-10 13:15:48 -08:00
Takashi Kokubun 2e875549a9 s/MJIT/RJIT/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun eaccdc1941 Rename MJIT filenames to RJIT 2023-03-06 23:44:01 -08:00
Takashi Kokubun 011c08b643 Remove obsoleted mjit_sp_inc.inc.erb 2023-03-06 23:05:50 -08:00
Takashi Kokubun 14acf9b0a3 Decode trace insns properly 2023-03-05 22:41:35 -08:00
Takashi Kokubun d9c2eb6f42 Move modules around 2023-03-05 22:11:20 -08:00
Peter Zhu 3e09822407 Fix incorrect line numbers in GC hook
If the previous instruction is not a leaf instruction, then the PC was
incremented before the instruction was ran (meaning the currently
executing instruction is actually the previous instruction), so we
should not increment the PC otherwise we will calculate the source
line for the next instruction.

This bug can be reproduced in the following script:

```
require "objspace"

ObjectSpace.trace_object_allocations_start
a =

  1.0 / 0.0
p [ObjectSpace.allocation_sourceline(a), ObjectSpace.allocation_sourcefile(a)]
```

Which outputs: [4, "test.rb"]

This is incorrect because the object was allocated on line 10 and not
line 4. The behaviour is correct when we use a leaf instruction (e.g.
if we replaced `1.0 / 0.0` with `"hello"`), then the output is:
[10, "test.rb"].

[Bug #19456]
2023-02-24 14:10:09 -05:00
Peter Zhu d2631c427e Fix RubyVM::CExpr#inspect
@__LINE__ can be nil which causes the inspect method to fail.
2023-02-24 14:10:09 -05:00
Takashi Kokubun 0b2aea861c Polish the public docs for MJIT [ci skip]
Now every private interface is cleaned up, and the public interface is
documented.
2022-12-22 14:30:09 -08:00
Takashi Kokubun bb4cbd0803 Put RubyVM::MJIT::Compiler under ruby_vm directory (#6989)
[Misc #19250]
2022-12-21 22:46:15 -08:00
Takashi Kokubun a1d70f5b12 MJIT: Rename mjit_compile_attr to mjit_sp_inc
There's no mjit_compile.inc, so no need to use this prefix anymore.
2022-11-29 21:45:34 -08:00
Nobuyoshi Nakada c50623f093 Revert "FreeBSD make uses the target under srcdir [ci skip]"
This reverts commit 751ffb276f, which
caused build failures on other platforms.
2022-10-13 12:24:59 +09:00
Nobuyoshi Nakada 751ffb276f FreeBSD make uses the target under srcdir [ci skip] 2022-10-13 12:10:10 +09:00
Takashi Kokubun 4e0db2f753 mjit_c.rb doesn't need to be an erb 2022-09-23 06:44:28 +09:00
Takashi Kokubun 334b8bd459 Mix manual and auto-generated C APIs 2022-09-23 06:44:28 +09:00
Takashi Kokubun 00c441ce7a Bindgen macro with builtin 2022-09-23 06:44:28 +09:00
Takashi Kokubun f2bea691cd Builtin RubyVM::MJIT::C 2022-09-23 06:44:28 +09:00
Takashi Kokubun 69130e1614 Expand paths used for dumper.rb
This seems to be needed on Samuel's environment
2022-09-22 21:07:22 +09:00
Takashi Kokubun a988fe0b3e Introduce --basedir to insns2vm.rb
and leverage that to preserve the directory structure under tool/ruby_vm/views
2022-09-18 14:39:53 +09:00
Takashi Kokubun 12023c833f Revert "Preserve the directory structure under tool/ruby_vm/views"
This reverts commit 62ec621f8c.

will revisit this once fixing non-MJIT targets
2022-09-18 14:21:40 +09:00