Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

tekknolagi
Copy link
Contributor

If the LiveRange looks like (idx, idx), we will currently not allocate a
register. This change allocates a register and then immediately
deallocates it.

Fix Shopify#614

If the LiveRange looks like (idx, idx), we will currently not allocate a
register. This change allocates a register and then immediately
deallocates it.

Fix Shopify#614
@matzbot matzbot requested a review from a team August 19, 2025 14:50
@tekknolagi tekknolagi changed the title ZJIT: Allocate register for VRegs that begin and end at the same index [RFC] ZJIT: Allocate register for VRegs that begin and end at the same index Aug 19, 2025
@XrXr
Copy link
Member

XrXr commented Aug 19, 2025

I guess this would turn what's currently an arcane crash message due to unused output to a silent transient register pressure. It's an improvement, but I'd want some feedback mechanism, maybe a debug!()?

But also, test failures.

This comment has been minimized.

@k0kubun
Copy link
Member

k0kubun commented Aug 19, 2025

We used to control every LIR instruction in YJIT's design, so it was useful to have a notification to remove unused LIR instructions because it was easy to identify why it happens and remove it, which also helps us simplify the implementation.

Now that we have an extra abstraction layer of HIR with multiple possible behaviors (HIR opt or not), I guess it's harder to remove unused instructions in ZJIT? I'm open to taking this direction, but I'd like to understand what's happening and why we need this, which I don't think are explained in Shopify#614 (comment) or Shopify#614 (comment).

@k0kubun
Copy link
Member

k0kubun commented Aug 19, 2025

Alternatively, if this happens only when you disable the HIR optimizer, we could take this path when it's disabled, and make it panic (probably with a better message than what we see today) when it's enabled. At least for the production mode, unused instructions are something to be fixed, somewhere in HIR, LIR, or the LIR backend.

@tekknolagi
Copy link
Contributor Author

In Shopify#614 it happens because the HIR ends up looking like this in the block:

Optimized HIR:
fn block in [email protected]:4:
bb0(v0:BasicObject, v1:BasicObject):
  v2:NilClass = Const Value(nil)
  v3:Any = Snapshot FrameState { pc: 0x0, stack: [], locals: [v1, v2] }
  v4:BasicObject = GetLocal l1, EP@3
  v5:Fixnum[1] = Const Value(1)
  Return v5

The GetLocal generates a load but it's never used. It's not cleaned up because we haven't run the optimizer.

@k0kubun
Copy link
Member

k0kubun commented Aug 19, 2025

Thanks for the explanation. It makes sense to allow such LIR to be compiled then. I still want the debug! log (at least when the HIR optimizer is enabled) in case the HIR optimizer misses it though.

We need a register to allocate to the result of the (unused) call in
tests
@tekknolagi tekknolagi requested a review from a team August 21, 2025 17:53
@tekknolagi tekknolagi changed the title [RFC] ZJIT: Allocate register for VRegs that begin and end at the same index ZJIT: Allocate register for VRegs that begin and end at the same index Aug 21, 2025
@k0kubun
Copy link
Member

k0kubun commented Aug 21, 2025

I think you can call rb_zjit_prepare_options() in the called Option::unwrap() on a None value-ing tests to make debug!() work.

@tekknolagi tekknolagi enabled auto-merge (squash) August 21, 2025 18:35
@tekknolagi tekknolagi merged commit f2715af into ruby:master Aug 21, 2025
85 checks passed
@tekknolagi tekknolagi deleted the mb-output-reg branch August 21, 2025 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ZJIT: GetLocal or SetLocal causes backend panic
3 participants