Skip to content

JIT: fix three wasm R2R codegen bugs - #131841

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-r2r-all-fixes
Open

JIT: fix three wasm R2R codegen bugs#131841
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-r2r-all-fixes

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Found by ReadyToRun-compiling and running the src/tests/JIT test tree on wasm.

  • fgUpdateFlowGraph: don't reverse a wasm try/catch header's GT_WASM_JEXCEPT. Its edge polarity is load-bearing for try_table codegen (false = try entry, true = catch-resumption dispatcher).
  • valuenum PeelOffsets: read ref/byref constant offsets as host size_t. They are stored as size_t; reading them as target_ssize_t is a wrong-sized read on wasm.
  • Vector Get/WithElement import: fall back to the throwing software implementation for an out-of-range constant lane index on wasm; a non-constant index still uses the jump-table lowering.

Found by ReadyToRun-compiling and running the src/tests/JIT test tree on wasm.

- fgUpdateFlowGraph: don't reverse a wasm try/catch header's GT_WASM_JEXCEPT.
  Its edge polarity is load-bearing for try_table codegen (false = try entry,
  true = catch-resumption dispatcher).
- valuenum PeelOffsets: read ref/byref constant offsets as host size_t. They are
  stored as size_t; reading them as target_ssize_t is a wrong-sized read on wasm.
- Vector Get/WithElement import: fall back to the throwing software implementation
  for an out-of-range constant lane index on wasm; a non-constant index still uses
  the jump-table lowering.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 89caa9c8-5b0f-4fcc-a8c4-726ac8535110
Copilot AI review requested due to automatic review settings August 5, 2026 00:25
@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 Aug 5, 2026
@AndyAyersMS

AndyAyersMS commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

@adamperlin PTAL
@EgorBo for the VN change

fyi @dotnet/wasm-contrib

These are crossgen2 failures -- there are also 3 runtime failures I'm still investigating.

@AndyAyersMS
AndyAyersMS requested a review from adamperlin August 5, 2026 00:26
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@AndyAyersMS
AndyAyersMS requested a review from EgorBo August 5, 2026 00:26
@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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 adjusts CoreCLR JIT behavior to fix several WebAssembly ReadyToRun (R2R) codegen issues encountered when compiling/running the JIT test suite on wasm.

Changes:

  • Fixes ValueNumStore::PeelOffsets constant offset peeling to correctly read ref/byref VN constants using host-sized storage on wasm.
  • Adds wasm-specific importer fallback for Vector.GetElement / Vector.WithElement when a constant lane index is out of range (so software path can throw).
  • Prevents fgUpdateFlowGraph from reversing wasm try/catch header GT_WASM_JEXCEPT, preserving edge polarity needed for try_table codegen.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/valuenum.cpp Updates VN offset peeling to handle ref/byref constant storage sizing correctly on wasm.
src/coreclr/jit/hwintrinsic.cpp Adds wasm-only handling to avoid generating invalid extract/replace lane encodings for out-of-range constant indices.
src/coreclr/jit/fgopt.cpp Disables a jump-reversal optimization for wasm try/catch headers to preserve GT_WASM_JEXCEPT semantics for codegen.
Suppressed comments (1)

src/coreclr/jit/valuenum.cpp:16414

  • Same issue as the previous branch: BYREF constants still use ConstantValue<target_ssize_t> (wrong-sized read on wasm), and REF coercion should use size_t (the only supported coercion for TYP_REF).
            *offset += varTypeIsGC(TypeOfVN(app.GetArg(1)))
                           ? (target_ssize_t)CoercedConstantValue<ssize_t>(app.GetArg(1))
                           : ConstantValue<target_ssize_t>(app.GetArg(1));

Comment on lines +16403 to +16407
// Ref/byref constants are stored as host size_t, so read them as such and truncate.
//
*offset += varTypeIsGC(TypeOfVN(app.GetArg(0)))
? (target_ssize_t)CoercedConstantValue<ssize_t>(app.GetArg(0))
: ConstantValue<target_ssize_t>(app.GetArg(0));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

varTypeIsGC should return true for TYP_BYREF as well, right? I'm not sure about this comment.

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.

3 participants