Skip to content

[wasm] Correctly materialize integer constants in genCodeForConstant - #130878

Merged
tannergooding merged 5 commits into
dotnet:mainfrom
tannergooding:wasm-const-materialization
Jul 17, 2026
Merged

tannergooding merged 5 commits into
dotnet:mainfrom
tannergooding:wasm-const-materialization

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Two independent correctness fixes to genCodeForConstant for the wasm backend, both uncovered while triaging the FitsIn<INT32> assert wall in SuperPMI replay of the wasm altjit.


Always relocate wasm handle constants regardless of compReloc

ImmedValNeedsReloc is opts.compReloc && IsIconHandle(). Under a runtime-JIT configuration (compReloc off) a handle icon skips emitAddressConstant and falls into the literal i32.const path, then asserts FitsIn<INT32> on its 64-bit host address. Wasm has no absolute-address literal — every handle must be materialized as a module-base-relative relocated constant — so this is gated on IsIconHandle() instead. This is a strict superset of the old condition: identical when compReloc is on, and additionally routes the compReloc-off handle case through the reloc path that the real target always takes.

Accept any 32-bit pattern when emitting a wasm i32.const

Wasm integers are sign-agnostic and i32.const has no unsigned form (it is a sign-agnostic 32-bit bit pattern encoded as SLEB128), so an unsigned constant such as 0xFFFFFFFF typed TYP_INT is a valid literal even though it does not fit a signed INT32. The assert is relaxed to also accept UINT32, and the value is reduced to its signed 32-bit form for a canonical SLEB128 encoding. Int→long widening stays explicit (i64.extend_s_i32 / i64.extend_u_i32, chosen by the cast), so the truncation cannot introduce a wrong sign extension.


Measured impact — SuperPMI replay of the wasm altjit over coreclr_tests and libraries_tests_no_tiered_compilation, FitsIn<INT32>(bits) assertions and total replay ISSUEs:

coreclr FitsIn libs FitsIn coreclr total ISSUE libs total ISSUE
before 151,242 118,936 179,560 141,917
after 1,440 5,274 47,610 30,288

The handle-routing fix accounts for essentially all of the reduction; the i32.const relaxation additionally unblocks the unsigned-pattern case (e.g. UIntPtr generic math). Methods unblocked by these fixes progress further and re-expose known, pre-existing downstream NYI/assert signatures — no new signatures are introduced.

Out of scope: the residual FitsIn count is dominated by non-handle 64-bit host-address constants (frozen-object / static-base addresses baked as plain CNS_INT) that only appear under x64-host SuperPMI replay of a runtime-JIT collection; on a real wasm32 target these are 32-bit values or reloc handles. The relaxed assert correctly keeps firing on them rather than silently truncating a genuine 64-bit value.

Note

This PR description was drafted by GitHub Copilot.

tannergooding and others added 2 commits July 16, 2026 07:57
`ImmedValNeedsReloc` also requires `opts.compReloc`, so under a runtime-JIT
configuration (compReloc off) a handle icon falls through to the literal
`i32.const` path and asserts `FitsIn<INT32>` on its 64-bit host address. Wasm
has no absolute-address literal; every handle must be materialized as a
module-base-relative relocated constant. Gate on `IsIconHandle()` so handles
always take the `emitAddressConstant` path.

Co-authored-by: Copilot App <[email protected]>
Wasm integers are sign-agnostic and `i32.const` has no unsigned form, so an
unsigned 32-bit constant such as `0xFFFFFFFF` typed `TYP_INT` is a valid
literal even though it does not fit a signed `INT32`. Relax the assert to
accept `UINT32` as well and reduce the value to its signed 32-bit form so it
encodes as a canonical SLEB128. Widening to `i64` remains explicit
(`i64.extend_s_i32` / `i64.extend_u_i32`), so the truncation cannot introduce a
wrong sign extension.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 14:59
@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 Jul 16, 2026
@azure-pipelines

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

@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 fixes constant materialization in the WASM JIT backend (CodeGen::genCodeForConstant) to avoid emitting incorrect i32.const immediates for handle constants and to correctly accept all 32-bit bit-patterns for i32.const.

Changes:

  • Route all GT_CNS_INT/GT_CNS_LNG handle constants (IsIconHandle()) through emitAddressConstant(...), independent of opts.compReloc.
  • Relax the TYP_INT constant assertion to accept either signed- or unsigned-32-bit ranges, and canonicalize the value to a signed 32-bit form before emission.
Show a summary per file
File Description
src/coreclr/jit/codegenwasm.cpp Fixes handle constant emission to always use reloc-capable address materialization; allows any 32-bit pattern for i32.const and canonicalizes encoding.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

Comment thread src/coreclr/jit/codegenwasm.cpp Resolved
…play

On a real wasm AOT compile compReloc is always on, so a handle always needs
a reloc; the compReloc-off path only occurs under a cross-VM SuperPMI replay.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 15:38

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.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread src/coreclr/jit/codegenwasm.cpp Resolved
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 15:49

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.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread src/coreclr/jit/codegenwasm.cpp Resolved
The prior static_cast<int32_t>(bits) narrows a 64-bit value out of range,
which is implementation-defined. Casting through uint32_t makes the low-32
reduction well-defined; only the final unsigned-to-signed reinterpret stays
implementation-defined (two's complement on all supported compilers).

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 16:04

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.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread src/coreclr/jit/codegenwasm.cpp Resolved

@adamperlin adamperlin 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.

This looks good to me. Thank you for fixing this, this will make testing wasm as an altjit much more feasible.

@tannergooding

Copy link
Copy Markdown
Member Author

/ba-g unrelated timeout, wasm only change

@tannergooding
tannergooding merged commit 6c68540 into dotnet:main Jul 17, 2026
138 of 140 checks passed
@tannergooding
tannergooding deleted the wasm-const-materialization branch July 17, 2026 02:25
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 18, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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