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

835 Коммитов

Автор SHA1 Сообщение Дата
Jeremy Evans b8516d6d01 Add pushtoarray VM instruction
This instruction is similar to concattoarray, but it takes the
number of arguments to push to the array, removes that number
of arguments from the stack, and adds them to the array now at
the top of the stack.

This allows `f(*a, 1)` to allocate only a single array on the
caller side (which can be reused on the callee side in the case of
`def f(*a)`). Prior to this commit, `f(*a, 1)` would generate
3 arrays:

* a dupped by splatarray true
* 1 wrapped in array by newarray
* a dupped again by concatarray

Instructions Before for `a = []; f(*a, 1)`:

```
0000 newarray                               0                         (   1)[Li]
0002 setlocal_WC_0                          a@0
0004 putself
0005 getlocal_WC_0                          a@0
0007 splatarray                             true
0009 putobject_INT2FIX_1_
0010 newarray                               1
0012 concatarray
0013 opt_send_without_block                 <calldata!mid:f, argc:1, ARGS_SPLAT|FCALL>
0015 leave
```

Instructions After for `a = []; f(*a, 1)`:

```
0000 newarray                               0                         (   1)[Li]
0002 setlocal_WC_0                          a@0
0004 putself
0005 getlocal_WC_0                          a@0
0007 splatarray                             true
0009 putobject_INT2FIX_1_
0010 pushtoarray                            1
0012 opt_send_without_block                 <calldata!mid:f, argc:1, ARGS_SPLAT|ARGS_SPLAT_MUT|FCALL>
0014 leave
```

With these changes, method calls to Ruby methods should
implicitly allocate at most one array.

Ignore typeprof bundled gem failure due to unrecognized instruction.
2024-01-24 18:25:55 -08:00
Jeremy Evans 22e488464a Add VM_CALL_ARGS_SPLAT_MUT callinfo flag
This flag is set when the caller has already created a new array to
handle a splat, such as for `f(*a, b)` and `f(*a, *b)`.  Previously,
if `f` was defined as `def f(*a)`, these calls would create an extra
array on the callee side, instead of using the new array created
by the caller.

This modifies `setup_args_core` to set the flag whenver it would add
a `splatarray true` instruction.  However, when `splatarray true` is
changed to `splatarray false` in the peephole optimizer, to avoid
unnecessary allocations on the caller side, the flag must be removed.
Add `optimize_args_splat_no_copy` and have the peephole optimizer call
that.  This significantly simplifies the related peephole optimizer
code.

On the callee side, in `setup_parameters_complex`, set
`args->rest_dupped` to true if the flag is set.

This takes a similar approach for optimizing regular splats that was
previiously used for keyword splats in
d2c41b1bff (via VM_CALL_KW_SPLAT_MUT).
2024-01-24 18:25:55 -08:00
Alan Wu 2cc7a56ec7
YJIT: Avoid leaks by skipping objects with a singleton class
For receiver with a singleton class, there are multiple vectors YJIT can
end up retaining the object. There is a path in jit_guard_known_klass()
that bakes the receiver into the code, and the object could also be kept
alive indirectly through a path starting at the CME object baked into
the code.

To avoid these leaks, avoid compiling calls on objects with a singleton
class.

See: https://github.com/Shopify/ruby/issues/552

[Bug #20209]
2024-01-24 18:06:58 -05:00
Maxime Chevalier-Boisvert 1702528258
YJIT: reduce default exec mem size to 48MiB (#9685)
YJIT: reduce default exec mem size to 48MiB based

Based on user feedback from @jhawthorn and others.
Better for small and memory-constrained deployments.

NOTE: This commit should be included in the next Ruby 3.3.x point
release. @xrxr should we tag someone specific?
2024-01-24 18:14:36 +00:00
Maxime Chevalier-Boisvert 23d4682926
YJIT: fix small bug causing jit_rb_int_rshift to side-exit (#9684)
YJIT: fix bug causing jit_rb_int_rshift to side-exit

The nqueens benchmark was showing zero performance improvement
because we immediately side-exited as soon as the shift amount
changed. If the amount changes, we want to fall back to the
C function call, not side-exit.
2024-01-24 13:09:08 -05:00
Alan Wu 974b141ffe YJIT: Reuse get_array_{ptr,len} 2024-01-23 19:47:26 -05:00
Alan Wu ac1e9e443a YJIT: Fix ruby2_keywords splat+rest and drop bogus checks
YJIT didn't guard for ruby2_keywords hash in case of splat calls that
land in methods with a rest parameter, creating incorrect results.

The compile-time checks didn't correspond to any actual effects of
ruby2_keywords, so it was masking this bug and YJIT was needlessly
refusing to compile some code. About 16% of fallback reasons in
`lobsters` was due to the ISeq check.

We already handle the tagging part with
exit_if_supplying_kw_and_has_no_kw() and should now have a dynamic guard
for all splat cases.

Note for backporting: You also need 7f51959ff1.

[Bug #20195]
2024-01-23 19:22:57 -05:00
Takashi Kokubun 27c1dd8634
YJIT: Allow inlining ISEQ calls with a block (#9622)
* YJIT: Allow inlining ISEQ calls with a block

* Leave a TODO comment about u16 inline_block
2024-01-23 19:36:23 +00:00
Alan Wu 7f51959ff1 YJIT: Move guard up for a case of splat+rest
Previously, YJIT put the guard for having enough items to extract from
splat array at a place where the side exit is invalid, so if the guard
fails, YJIT could raise something other than ArgumentError. Move the
guard up to a place before any stack manipulation.

[Bug #20204]
2024-01-23 11:04:16 -05:00
dependabot[bot] a64e26c5d5
Bump capstone from 0.10.0 to 0.11.0 in /yjit (#9653)
Bumps [capstone](https://github.com/capstone-rust/capstone-rs) from 0.10.0 to 0.11.0.
- [Release notes](https://github.com/capstone-rust/capstone-rs/releases)
- [Changelog](https://github.com/capstone-rust/capstone-rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/capstone-rust/capstone-rs/compare/capstone-v0.10.0...capstone-v0.11.0)

---
updated-dependencies:
- dependency-name: capstone
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-22 14:44:00 -08:00
dependabot[bot] bd1895990c
Bump shlex from 1.1.0 to 1.3.0 in /yjit/bindgen (#9652)
Bumps [shlex](https://github.com/comex/rust-shlex) from 1.1.0 to 1.3.0.
- [Changelog](https://github.com/comex/rust-shlex/blob/master/CHANGELOG.md)
- [Commits](https://github.com/comex/rust-shlex/commits)

---
updated-dependencies:
- dependency-name: shlex
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-22 14:11:07 -08:00
Alan Wu 703eee7745
YJIT: Drop extra arguments passed by yield (#9596)
Support dropping extra arguments passed by `yield` in blocks. For
example `10.times { work }` drops the count argument. This is common
enough that it's about 3% of fallback reasons in `lobsters`.

Only support simple cases where the surplus arguments are at the top of
the stack, that way they just need to be popped, which takes no work.
2024-01-22 11:55:44 -05:00
Takashi Kokubun e0f7cee8c5
YJIT: Avoid doubly splitting Opnd::Value on CSel (#9617)
YJIT: Avoid doubly splitting Opnd::Value
2024-01-19 11:51:35 -08:00
Takashi Kokubun 3c9290173a
YJIT: Optimize defined?(yield) (#9599)
* YJIT: Optimize defined?(yield)

* Remove an irrelevant comment

* s/get/gen/
2024-01-19 11:00:46 -05:00
Alan Wu bbd249e351 YJIT: Properly reject keyword splat with `yield`
We don't have support for keyword splat anywhere, but we tried to
compile these anyways in case of `invokeblock`. This led to bad things
happening such as passing the wrong value and passing a hash into
rb_yjit_array_len(), which raised in the middle of compilation.

[Bug #20192]
2024-01-18 20:22:40 -05:00
Takashi Kokubun 33306a08d1
YJIT: Stop incrementing chain_depth on defer_compilation (#9597) 2024-01-18 11:40:11 -08:00
Alan Wu 03645d1eef
YJIT: Support empty splat and some block_arg calls to ivar getters (#9567)
These seem odd at first glance, but they're used with `...` calls with
`Module#delegate` from Active Support. These account for ~3% of fallback
reasons in the `lobsters` benchmark.
2024-01-17 10:52:15 -05:00
Maxime Chevalier-Boisvert afba09d30f
YJIT: specialized codegen for integer right shift (#9564)
* YJIT: specialized codegen for integer right shift

Used in optcarrot. May also be used to write pure-Ruby gems.
No overflow check or fixnum untagging required.

* Update yjit/src/codegen.rs

Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>

---------

Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
2024-01-17 10:35:48 -05:00
Takashi Kokubun 8642a573e6 Rename BUILTIN_ATTR_SINGLE_NOARG_INLINE
to BUILTIN_ATTR_SINGLE_NOARG_LEAF

The attribute was created when the other attribute was called BUILTIN_ATTR_INLINE.
Now that the original attribute is renamed to BUILTIN_ATTR_LEAF, it's
only confusing that we call it "_INLINE".
2024-01-16 17:31:27 -08:00
Takashi Kokubun e37a37e696 Drop obsoleted BUILTIN_ATTR_NO_GC attribute
The thing that has used this in the past was very buggy, and we've never
revisied it. Let's remove it until we need it again.
2024-01-16 17:27:53 -08:00
Alan Wu 2bf9970101 YJIT: Finer breakdown for block_arg counters
For example, on `lobsters` the block_arg reason was at ~10%. With this
change it shows that 6% of that is `send_cfunc_block_arg`.
2024-01-16 16:54:34 -05:00
Maxime Chevalier-Boisvert 6a1bf4cf1b
YJIT: optimized codegen for String#length (#9543)
Minor optimization for programs doing string processing.
2024-01-15 17:35:29 -05:00
Takashi Kokubun 5e61cc26c9
YJIT: Optimize Integer#succ (#9519) 2024-01-15 10:32:48 -05:00
ywenc 16624ef463
YJIT: expandarray for non-arrays (#9495)
* YJIT: expandarray for non-arrays

Co-authored-by: John Hawthorn <john@hawthorn.email>

* Skip the new test on RJIT

* Increment counter for to_ary exit

---------

Co-authored-by: John Hawthorn <john@hawthorn.email>
Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
2024-01-12 15:41:58 +00:00
Alan Wu 015b0e2e1d YJIT: Fix unused warnings
```
warning: unused import: `condition::Condition`
  --> src/asm/arm64/arg/mod.rs:13:9
   |
13 | pub use condition::Condition;
   |         ^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused import: `rb_yjit_fix_mul_fix as rb_fix_mul_fix`
   --> src/cruby.rs:188:9
    |
188 | pub use rb_yjit_fix_mul_fix as rb_fix_mul_fix;
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused import: `rb_insn_len as raw_insn_len`
   --> src/cruby.rs:142:9
    |
142 | pub use rb_insn_len as raw_insn_len;
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default
```

Make asm public so it stops warning about unused public stuff in there.
2024-01-10 13:19:15 -05:00
Takashi Kokubun a0eecfb5ba
YJIT: Fallback Integer#<< if a shift amount varies (#9426)
* YJIT: Fallback Integer#<< if a shift amount varies

* YJIT: Do not fallback lshift in the first chain
2024-01-08 17:34:57 +00:00
Takashi Kokubun 7f9c174102
YJIT: Let RubyVM::YJIT.enable respect --yjit-stats (#9415) 2024-01-05 14:08:57 -05:00
Hiroshi SHIBATA 863ded45a1
Typofix under bootstraptest, spec and yjit directories 2023-12-25 13:50:23 +09:00
Takashi Kokubun bd91c5127f
YJIT: Add stats option to RubyVM::YJIT.enable (#9297) 2023-12-19 11:47:27 -08:00
Takashi Kokubun b266890dab
YJIT: Add --yjit-disable to help and reorder it (#9230) 2023-12-13 13:29:37 -08:00
Alan Wu d4bbee7475
YJIT: Fix off-by-one in Kernel#send type handling (#9212)
Previously, if the method ID argument happens to be on one below the top
of the stack, we didn't overwrite the type of the stack slot, which
leaves an incorrect type for the stack slot. The included script tripped
asserts both with and without --yjit-verify-ctx.
2023-12-12 22:10:14 +00:00
Alan Wu 9cb0ad863c YJIT: Fix missing arity check for splat calls to methods with optionals
Previously, for splat callsites that land in methods with optional
parameters, we didn't reject the case where the caller supplies too many
arguments. Accepting those calls previously caused YJIT to construct
corrupted control frames, which leads to crashes if the callee uses
certain stack walking methods such as Kernel#raise and String#gsub (for
setting up the frame-local `$~`).

Example crash in a debug build:

    Assertion Failed: ../vm_core.h:1375:VM_ENV_FLAGS:FIXNUM_P(flags)
2023-12-11 19:21:08 -05:00
Alan Wu 4755309474 YJIT: Rename helper function and correct counter name
Counter::guard_send_iseq_has_rest_and_splat_not_equal was using
jump-if-lesser-than, so wasn't checking for equality. Rename function
because moving is destructive in Rust, which is confusing for this function
which doesn't modify the array.
2023-12-11 19:21:08 -05:00
Jeremy Evans a950f23078 Ensure f(**kw, &block) calls kw.to_hash before block.to_proc
Previously, block.to_proc was called first, by vm_caller_setup_arg_block.
kw.to_hash was called later inside CALLER_SETUP_ARG or setup_parameters_complex.

This adds a splatkw instruction that is inserted before sends with
ARGS_BLOCKARG and KW_SPLAT and without KW_SPLAT_MUT. This is not needed in the
KW_SPLAT_MUT case, because then you know the value is a hash, and you don't
need to call to_hash on it.

The splatkw instruction checks whether the second to top block is a hash,
and if not, replaces it with the value of calling to_hash on it (using
rb_to_hash_type).  As it is always before a send with ARGS_BLOCKARG and
KW_SPLAT, second to top is the keyword splat, and top is the passed block.
2023-12-09 13:15:47 -08:00
Takashi Kokubun ac5fd58700
YJIT: Fix on-stack ISEQ comparison for auto_compact (#9164) 2023-12-07 22:53:05 +00:00
Alan Wu 9d9865d9bc YJIT: Add some object validity assertions
We've seen quite a few compaction bugs lately, and these assertions
should give clearer symptoms. We only call class_of() on
objects that the Ruby code can see.
2023-12-06 16:42:53 -05:00
Takashi Kokubun a439fc2f17
YJIT: Avoid register allocation conflict with a higher stack_idx (#9143)
YJIT: Avoid register allocation conflict

with a higher stack_idx
2023-12-06 15:19:43 -05:00
Alan Wu a063969ec1 YJIT: Assert code pages are not partially in-bounds
Helps understand page switching
2023-12-05 13:20:06 -05:00
Alan Wu 695e5c179e YJIT: Simplify code page switching logic, remove an assert
We have received a report of `assert!( !cb.has_dropped_bytes())` in
set_page() failing. The only explanation for this seems to be memory
allocation failing in write_byte(). The if condition implies that
`current_write_pos < dst_pos < mem_size`, which rules out failing to
encode the relative jump. The has_capacity() assert above not tripping
implies that we were in a place in the page where write_byte() did
attempt to write the byte and potentially made a syscall in the process.

Remove the assert, since memory allocation could fail. Also, return
failure if the destination is outside of the code region to detect that
out-of-memory situation quicker.
2023-12-05 13:20:06 -05:00
Alan Wu b5a62eb9ab
YJIT: Mark and update stubs in invalidated blocks (#9104)
Like in the example given in delayed_deallocation(), stubs can be hit
even if the block housing it is invalidated. Mark them so we don't
work with invalidate ISeqs when hitting these stubs.
2023-12-04 10:13:40 -05:00
Takashi Kokubun ba1cdadfc8
YJIT: Cancel on-stack jit_return on invalidation (#9086)
* YJIT: Cancel on-stack jit_return on invalidation

Co-authored-by: Alan Wu <alansi.xingwu@shopify.com>

* Use RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P

---------

Co-authored-by: Alan Wu <alansi.xingwu@shopify.com>
2023-11-30 21:35:55 -05:00
Maxime Chevalier-Boisvert 73e6d8a0d0
YJIT: optimized codegen for `rb_ary_length()` (#9085)
YJIT: optimized codegen for rb_ary_length()
2023-11-30 17:14:53 -05:00
Takashi Kokubun d048bae96b
YJIT: Bump ec->cfp after setting cfp->jit_return (#9072) 2023-11-30 11:23:25 -05:00
Maxime Chevalier-Boisvert 8d1138c1cf
YJIT: edit `yjit.md` and bring it up to date (#9068)
Also make various minor edits to improve readability.
2023-11-29 18:09:55 -05:00
Maxime Chevalier-Boisvert ea3e17e430
YJIT: fix bug in top cfunc logging in `--yjit-stats` (#9056)
YJIT: correctly handle case where there are no cfunc calls

Fix bug in top cfunc logging in `--yjit-stats`
2023-11-28 22:27:11 +00:00
Maxime Chevalier-Boisvert 6310522a9a
YJIT: reduce default exec-mem-size to 64MiB (#9054) 2023-11-28 20:04:33 +00:00
Takashi Kokubun 476a231e7e
YJIT: Assert no patch overlap on pos_marker (#9048) 2023-11-28 10:41:14 -05:00
Maxime Chevalier-Boisvert 7f50c70574
YJIT: add top C function call counts to `--yjit-stats` (#9047)
* YJIT: gather call counts for individual cfuncs

Co-authored by Takashi Kokubun
2023-11-27 22:49:53 +00:00
Maxime Chevalier-Boisvert f05d586cc9
YJIT: record `num_send_cfunc` stat (#9022)
* YJIT: record num_send_cfunc stat

Also report num_send_known_cfunc as percentage of num_send_cfunc

* Rename num_send_known_cfunc => num_send_cfunc_inline

Name seems more descriptive of what we do with out custom codegen
2023-11-23 15:33:43 -05:00
Takashi Kokubun 440b59db10
YJIT: Apply patches ignoring page_end_reserve (#9015) 2023-11-23 10:53:12 -05:00