Skip to content

ZJIT: Support invalidating constant patch points #13998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Jul 23, 2025

No description provided.

@matzbot matzbot requested a review from a team July 23, 2025 23:21
@st0012 st0012 linked an issue Jul 24, 2025 that may be closed by this pull request
Copy link
Contributor

@tekknolagi tekknolagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great -- let's wait for Alan or Kokubun

@@ -148,6 +148,7 @@ rb_clear_constant_cache_for_id(ID id)
}

rb_yjit_constant_state_changed(id);
rb_zjit_constant_state_changed(id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only keyed on one ID, does that mean we will invalidate A::B::C if D::E::C changes (because they both share C)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes when C is redefined the current implementation invalidates all jumps associated with it (confirmed with debugger and print messages). But since the callback only receives one ID, I'm not sure if there's a way to reconstruct the full constant path based on it.

It looks like YJIT behaves the same way?

https://github.com/Shopify/ruby/blob/8f634e19599c97f9ecae31afd651a65ca06693a1/yjit/src/invariants.rs#L337-L343

@k0kubun is this correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does seem like it. rb_clear_constant_cache_for_id(ID id) is called when the id is used in the scope of a module. So if you invalidate constants with an id, that would invalidate constants with the same name under different modules.

I do think we need to do that anyway though. If the ISEQ is on a module, any random class could include it and use the JIT code. We can't really scope the invalidation into specific classes/modules unless you scan every class/module that could use it, which seems unrealistic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification!

@k0kubun
Copy link
Member

k0kubun commented Jul 24, 2025

YJIT also invalidates constants on rb_yjit_constant_ic_update. Does ZJIT need the same thing?

@tekknolagi
Copy link
Contributor

lgtm after addressing ^

@k0kubun
Copy link
Member

k0kubun commented Jul 28, 2025

So, for correctness, rb_zjit_constant_state_changed seems to catch everything. The reason why YJIT still needs the other hook rb_yjit_constant_ic_update today is that YJIT (and ZJIT) relies on the interpreter's inline cache to know the content, so it wants the interpreter to notify YJIT when the interpreter updates the inline cache.

Since ZJIT currently only discards invalid code and never recompiles them, we probably don't need the hook yet. Let's merge this as is for now. Thanks!

@k0kubun k0kubun merged commit a0d0b84 into ruby:master Jul 28, 2025
85 checks passed
@k0kubun k0kubun deleted the zjit-invalidate-constant-changes branch July 28, 2025 21:48
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: Invalidate code when constant is set
3 participants