summaryrefslogtreecommitdiff
path: root/yjit/src/codegen.rs
AgeCommit message (Collapse)Author
2025-05-24Follow up for 380938998415c22ba3ca9bc01f4035d5a73f274d. The type of ↵nagachika
jump_to_next_insn() is Option<()> on ruby_3_3.
2025-05-24Follow up for 380938998415c22ba3ca9bc01f4035d5a73f274d. jump_to_next_insn() ↵nagachika
require 3rd argument on ruby_3_3.
2025-05-24merge revision(s) cbf9c088f8005a49b6aa3f475c70041357774c61: [Backport #21310]nagachika
YJIT: End the block after OPTIMIZE_METHOD_TYPE_CALL (#13245)
2025-01-14YJIT: Filter & calls from specialized C method codegenTakashi Kokubun
Evident with the crash reported in [Bug #20997], the C replacement codegen functions aren't authored to handle block arguments (nor should they because the extra code from the complexity defeats optimization). Filter sites with VM_CALL_ARGS_BLOCKARG. Co-Authored-By: Alan Wu <alanwu@ruby-lang.org>
2024-06-04[3.3 backport] Do not emit shape transition warnings when YJIT is compiling ↵Jean byroot Boussier
(#10911) Do not emit shape transition warnings when YJIT is compiling [Bug #20522] If `Warning.warn` is redefined in Ruby, emitting a warning would invoke Ruby code, which can't safely be done when YJIT is compiling. Co-authored-by: Jean Boussier <jean.boussier@gmail.com> Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
2024-06-04YJIT: Fix out of bounds access when splatting empty array (#10905)Alan Wu
This is a backport of 6c8ae44a388e5c03b7db90376af3652007b574e8 with a test tailored to crash the 3.3.x branch (from GH-10904). Previously, we read the last element array even when the array was empty, doing an out-of-bounds access. This sometimes caused a SEGV. [Bug #20496]
2024-05-28merge revision(s) ↵Takashi Kokubun
015b0e2e1d312e2be60551587389c8da5c585e6f,ac1e9e443a0d6a4d4c0801c26d1d8bd33d9eb431: [Backport #20195] 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. 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-05-28merge revision(s) 7f51959ff14fbe06bc1afd283d1af17b26161cf4: [Backport #20204]Takashi Kokubun
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-05-28merge revision(s) bbd249e351af7e4929b518a5de73a832b5617273: [Backport ↵NARUSE, Yui
#20192] (#10249) * merge revision(s) bbd249e351af7e4929b518a5de73a832b5617273: [Backport #20192] 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] * Skip a new test for RJIT
2024-03-14Backport 3.3: YJIT memory leak fix with additional CI fixes (#9841)Alan Wu
merge revision(s) 2cc7a56e,b0711b1,db5d9429: [Backport #20209] 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] --- yjit/bindgen/src/main.rs | 1 + yjit/src/codegen.rs | 17 +++++++++++++++++ yjit/src/cruby_bindings.inc.rs | 1 + yjit/src/stats.rs | 2 ++ 4 files changed, 21 insertions(+) YJIT: Fix tailcall and JIT entry eating up FINISH frames (#9729) Suppose YJIT runs a rb_vm_opt_send_without_block() fallback and the control frame stack looks like: ``` will_tailcall_bar [FINISH] caller_that_used_fallback ``` will_tailcall_bar() runs in the interpreter and sets up a tailcall. Right before JIT_EXEC() in the `send` instruction, the stack will look like: ``` bar [FINISH] caller_that_used_fallback ``` Previously, JIT_EXEC() ran bar() in JIT code, which caused the `FINISH` flag to return to the interpreter instead of to the JIT code running caller_that_used_fallback(), causing code to run twice and probably crash. Recent flaky failures on CI about "each stub expects a particular iseq" are probably due to leaving methods twice in `test_optimizations.rb`. Only run JIT code from the interpreter if a new frame is pushed. --- test/ruby/test_optimization.rb | 11 +++++++++++ vm_exec.h | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) YJIT: No need to RESTORE_REG now that we reject tailcalls Thanks to Kokubun for noticing. Follow-up: b0711b1cf152afad0a480ee2f9bedd142a0d24ac --- vm_exec.h | 1 - 1 file changed, 1 deletion(-)
2024-02-05YJIT: Fix exits on splatkw instruction (#9715)Takashi Kokubun
[[Bug #20214]](https://bugs.ruby-lang.org/issues/20214)
2023-12-25Typofix under bootstraptest, spec and yjit directoriesHiroshi SHIBATA
2023-12-11YJIT: Fix missing arity check for splat calls to methods with optionalsAlan Wu
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-11YJIT: Rename helper function and correct counter nameAlan Wu
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-06YJIT: Add some object validity assertionsAlan Wu
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-11-30YJIT: optimized codegen for `rb_ary_length()` (#9085)Maxime Chevalier-Boisvert
YJIT: optimized codegen for rb_ary_length()
2023-11-30YJIT: Bump ec->cfp after setting cfp->jit_return (#9072)Takashi Kokubun
2023-11-28YJIT: Assert no patch overlap on pos_marker (#9048)Takashi Kokubun
2023-11-27YJIT: add top C function call counts to `--yjit-stats` (#9047)Maxime Chevalier-Boisvert
* YJIT: gather call counts for individual cfuncs Co-authored by Takashi Kokubun
2023-11-23YJIT: record `num_send_cfunc` stat (#9022)Maxime Chevalier-Boisvert
* 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-10YJIT: Take cargo --fix for unnecessary calls to into()Alan Wu
2023-11-10YJIT: Auto fix for clippy::unnecessary_castAlan Wu
2023-11-10YJIT: Auto fix for clippy::clone_on_copyAlan Wu
2023-11-08Refactor rb_shape_transition_shape_capa outJean Boussier
Right now the `rb_shape_get_next` shape caller need to first check if there is capacity left, and if not call `rb_shape_transition_shape_capa` before it can call `rb_shape_get_next`. And on each of these it needs to checks if we got a TOO_COMPLEX back. All this logic is duplicated in the interpreter, YJIT and RJIT. Instead we can have `rb_shape_get_next` do the capacity transition when needed. The caller can compare the old and new shapes capacity to know if resizing is needed. It also can check for TOO_COMPLEX only once.
2023-11-07YJIT: Fix assert in OOM scenarioAlan Wu
We still need to do `jit.record_boundary_patch_point = false` when gen_outlined_exit() returns `None` and we return with `?`. Previously, we tripped the assert at codegen.rs:1042. Found with `--yjit-exec-mem-size=3` on the lobsters benchmark. Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com> Co-authored-by: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@shopify.com>
2023-11-07YJIT: Use u32 for CodePtr to save 4 bytes eachAlan Wu
We've long had a size restriction on the code memory region such that a u32 could refer to everything. This commit capitalizes on this restriction by shrinking the size of `CodePtr` to be 4 bytes from 8. To derive a full raw pointer from a `CodePtr`, one needs a base pointer. Both `CodeBlock` and `VirtualMemory` can be used for this purpose. The base pointer is readily available everywhere, except for in the case of the `jit_return` "branch". Generalize lea_label() to lea_jump_target() in the IR to delay deriving the `jit_return` address until `compile()`, when the base pointer is available. On railsbench, this yields roughly a 1% reduction to `yjit_alloc_size` (58,397,765 to 57,742,248).
2023-11-07YJIT: Inline basic Ruby methods (#8855)Takashi Kokubun
* YJIT: Inline basic Ruby methods * YJIT: Fix "InsnOut operand made it past register allocation" checktype should not generate a useless instruction.
2023-11-07YJIT: handle out of shape situation in gen_setinstancevariable (#8857)Jean byroot Boussier
If the VM ran out of shape, `rb_shape_transition_shape_capa` might return `OBJ_TOO_COMPLEX_SHAPE`. Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2023-11-02YJIT: Always define method codegen table at boot (#8807)Takashi Kokubun
2023-10-19YJIT: Return Option from asm.compile() for has_dropped_bytes()Alan Wu
So that we get a reminder to check CodeBlock::has_dropped_bytes(). Internally, asm.compile() already checks it, and this patch just propagates it out to the caller with a `#[must_use]`. Code GC logic moved out one level in entry_stub_hit(), so the body can freely use `?`
2023-10-18Revert "shape.h: Make attr_index_t uint8_t"Katherine Oelsner
This reverts commit e3afc212ec059525fe4e5387b2a3be920ffe0f0e.
2023-10-18YJIT: Add --yjit-perf (#8697)Takashi Kokubun
Co-authored-by: Alan Wu <alansi.xingwu@shopify.com>
2023-10-17YJIT: Remove call to compile() on empty AssemblerAlan Wu
2023-10-17YJIT: Add a few missing counters for send fallback (#8681)Takashi Kokubun
2023-10-17YJIT: Lookup IDs on boot instead of binding to themAlan Wu
Previously, the version-controlled `cruby_bindings.inc.rs` file contained the build-time artifact `id.h`, which nobu mentioned hinders the goal of having fewer magic numbers in the repository. Lookup the IDs YJIT needs on boot. It costs cycles, but it's fine since YJIT only uses a handful of IDs at the moment. No perceptible degradation to boot time found in my testing.
2023-10-13YJIT: Fallback opt_getconstant_path for const_missing (#8623)Takashi Kokubun
* YJIT: Fallback opt_getconstant_path for const_missing * Fix a comment [ci skip] * Remove a wrapper function
2023-10-13YJIT: Fix argument clobbering in some block_arg+rest_param calls (#8647)Alan Wu
Previously, for block argument callsites with some specific argument count and callee local variable count combinations, YJIT ended up writing over arguments that are supposed to be collected into a rest parameter array unmodified. Detect when clobbering would happen and avoid it. Also, place the block handler after the stack overflow check, since it writes to new stack space. Reported-by: Takashi Kokubun <takashikkbn@gmail.com>
2023-10-11shape.h: Make attr_index_t uint8_tJean Boussier
Given `SHAPE_MAX_NUM_IVS 80`, we transition to TOO_COMPLEX way before we could overflow a 8bit counter. This reduce the size of `rb_shape_t` from 32B to 24B. If we decide to raise `SHAPE_MAX_NUM_IVS` we can always increase that type again.
2023-10-10Refactor rb_shape_transition_shape_capa to not accept capacityJean Boussier
This way the groth factor is encapsulated, which allows rb_shape_transition_shape_capa to be smarter about ideal sizes.
2023-10-05YJIT: Avoid writing return value to memory in `leave`Alan Wu
Previously, at the end of `leave` we did `*caller_cfp->sp = return_value`, like the interpreter. With future changes that leaves the SP field uninitialized for C frames, this will become problematic. For cases like returning from `rb_funcall()`, the return value was written above the stack and never read anyway (callers use the copy in the return register). Leave the return value in a register at the end of `leave` and have the code at `cfp->jit_return` decide what to do with it. This avoids the unnecessary memory write mentioned above. For JIT-to-JIT returns, it goes through `asm.stack_push()` and benefits from register allocation for stack temporaries. Mostly flat on benchmarks, with maybe some marginal speed improvements. Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
2023-10-03YJIT: Stop spilling temps on jit_prepare_routine_call (#8581)Takashi Kokubun
YJIT: Remove spill_temps from jit_prepare_routine_call
2023-09-29YJIT: Chain-guard opt_mult overflow (#8554)Takashi Kokubun
* YJIT: Chain-guard opt_mult overflow * YJIT: Support regenerating Jo after Mul
2023-09-29YJIT: Use registers for passing C method arguments (#8538)Takashi Kokubun
2023-09-29YJIT: Remove obsoleted jit_rb_int_mul (#8539)Takashi Kokubun
2023-09-15YJIT: Fix object movement bug in iseq guard for invokeblockAlan Wu
Since the compile-time iseq used in the guard was not marked and updated during compaction, a runtime value reusing the address could falsely pass the guard. Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
2023-09-14YJIT: Skip Insn::Comment and format! if disasm is disabled (#8441)Takashi Kokubun
* YJIT: Skip Insn::Comment and format! if disasm is disabled Co-authored-by: Alan Wu <alansi.xingwu@shopify.com> * YJIT: Get rid of asm.comment --------- Co-authored-by: Alan Wu <alansi.xingwu@shopify.com> Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2023-09-14YJIT: Remove UTF-8 BOM [ci skip]Alan Wu
/yjit/src/backend/x86_64/mod.rs Is also UTF-8 and it doesn't have the marker. The standard recommends against it, so remove it.
2023-09-12Make Kernel#lambda raise when given non-literal blockAlan Wu
Previously, Kernel#lambda returned a non-lambda proc when given a non-literal block and issued a warning under the `:deprecated` category. With this change, Kernel#lambda will always return a lambda proc, if it returns without raising. Due to interactions with block passing optimizations, we previously had two separate code paths for detecting whether Kernel#lambda got a literal block. This change allows us to remove one path, the hack done with rb_control_frame_t::block_code introduced in 85a337f for supporting situations where Kernel#lambda returned a non-lambda proc. [Feature #19777] Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com> Notes: Merged: https://github.com/ruby/ruby/pull/8405
2023-09-07Add `String#getbyte` YJIT implementation (#8397)Ian Candy
* Add getbyte JIT implementation Adds an implementation for String#getbyte for YJIT, along with a bootstrap test. This should be helpful for pure Ruby implementations and to avoid unneeded allocations. Co-authored-by: John Hawthorn <jhawthorn@github.com> * Skip the getbyte test for RJIT for now --------- Co-authored-by: John Hawthorn <jhawthorn@github.com> Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com> Notes: Merged-By: maximecb <maximecb@ruby-lang.org>
2023-09-07YJIT: Decrease IVAR_MAX_DEPTH to 8 (#8398)Takashi Kokubun
Notes: Merged-By: maximecb <maximecb@ruby-lang.org>