Skip to content

[Wasm RyuJit] Fix: Move implReadyToRunUnsupported() call for funclets before wasm codegen#127185

Open
adamperlin wants to merge 1 commit intodotnet:mainfrom
adamperlin:adamperlin/wasm-fix-r2r-unsupported-funclets
Open

[Wasm RyuJit] Fix: Move implReadyToRunUnsupported() call for funclets before wasm codegen#127185
adamperlin wants to merge 1 commit intodotnet:mainfrom
adamperlin:adamperlin/wasm-fix-r2r-unsupported-funclets

Conversation

@adamperlin
Copy link
Copy Markdown
Contributor

This fixes a bug where we:

  1. Compile a method under opts that inlines a method with EH, including full generation of code + host relocations
  2. Bail out immediately after because of funclets
  3. Recompile the method within the JIT (using the goto START handling in compiler.cpp) without minopts
  4. Successfully recompile, because we don't inline the method that needs funclets this time
  5. Leave stale relocations on the host side from the original optimized compilation with the inlinee.

This avoids a bug where we compile a method under opts that inlines a
method with exceptions, bail out because of funclets, then recompile
without the inlinee under minopts, leaving stale relocs on the host
side.
Copilot AI review requested due to automatic review settings April 20, 2026 22:22
@adamperlin adamperlin changed the title Move implReadyToRunUnsupported() call for funclets before wasm codegen [Wasm RyuJit] Fix: Move implReadyToRunUnsupported() call for funclets before wasm codegen Apr 20, 2026
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 20, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a Wasm ReadyToRun compilation bug where a first (optimized) compilation attempt could record relocations and then bail out due to funclets, followed by a successful minopts recompile that left stale host-side relocations from the first attempt.

Changes:

  • Move the Wasm funclet-based implReadyToRunUnsupported() bailout to occur before any codegen/emission work begins.
  • Expand the comment to document the optimized-then-minopts recompilation edge case and why the bailout must be early.

@adamperlin adamperlin requested a review from AndyAyersMS April 20, 2026 22:26
Copy link
Copy Markdown
Member

@kg kg left a comment

Choose a reason for hiding this comment

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

This seems like an adequate fix and will address something else I ran into most likely. I'm guessing cleaning up the stale relocs from the retry is too hard? I'm thinking 'stale relocs from retries' is something we need to be able to handle more generally since under normal circumstances (AFAIK) retries can happen and should work.

@adamperlin
Copy link
Copy Markdown
Contributor Author

This seems like an adequate fix and will address something else I ran into most likely. I'm guessing cleaning up the stale relocs from the retry is too hard?

Yeah, I'm not sure there's an easy way to signal back to the host that it needs to clear out any relocs it recorded for the current method before we retry. One thing we could maybe do is add a new JIT/EE interface that signals this? That might be the best thing to do long term.

@kg
Copy link
Copy Markdown
Member

kg commented Apr 20, 2026

This seems like an adequate fix and will address something else I ran into most likely. I'm guessing cleaning up the stale relocs from the retry is too hard?

Yeah, I'm not sure there's an easy way to signal back to the host that it needs to clear out any relocs it recorded for the current method before we retry. One thing we could maybe do is add a new JIT/EE interface that signals this? That might be the best thing to do long term.

This would address the issues I hit where a late retry causes us to try and overwrite write-once boundaries and vars in the host, see https://github.com/dotnet/runtime/pull/126932/changes#diff-01e22eff46505ee20d75dd64c72a9eb57d67f28f22a76258300bbe44dca59a14R1582

@jkotas @AndyAyersMS do you think we should add a new host 'retrying' signal that clears out state like this? I'm leaning towards yes but it is something that will rarely get called so I expect we will accumulate technical debt around it (i.e. 'oh, we added this new piece of state but forgot to add it to the ResetForJitRetry EE API')

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 20, 2026

ResetForJitRetry

The robust way to implement this would be to return from the JIT with an error code and allow the host side to create a fresh compilation context for the retry so that we do not need to worry about cleaning up every bit of state.

(I know there is an existing ResetForJitRetry internal VM method for the runtime JIT. I do not think it is a good idea to promote this method to be on JIT/EE interface.)

@adamperlin
Copy link
Copy Markdown
Contributor Author

@AndyAyersMS also had the idea that we could disable the "retry compilation with minopts" logic for R2R entirely, though I'm not sure what the full implications of that would be.

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 20, 2026

"retry compilation with minopts" logic for R2R entirely,

It may be ok for R2R where we have a fallback to JIT or interpreter at runtime. I would not be ok for JIT or NAOT - there is no fallback for those.

The AOT compilers already make a distinction between these two cases when the fallback compilation is not enough:

#if READYTORUN
throw new RequiresRuntimeJitException("JIT implementation limitation");
#else
ThrowHelper.ThrowInvalidProgramException();
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants