Repository navigation
[wasm] Correctly materialize integer constants in genCodeForConstant - #130878
Merged
tannergooding merged 5 commits intoJul 17, 2026
Merged
Conversation
`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]>
|
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. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Contributor
There was a problem hiding this comment.
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_LNGhandle constants (IsIconHandle()) throughemitAddressConstant(...), independent ofopts.compReloc. - Relax the
TYP_INTconstant 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
tannergooding
commented
Jul 16, 2026
…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]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
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]>
adamperlin
approved these changes
Jul 16, 2026
tannergooding
enabled auto-merge (squash)
July 17, 2026 01:23
Member
Author
|
/ba-g unrelated timeout, wasm only change |
Open
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two independent correctness fixes to
genCodeForConstantfor the wasm backend, both uncovered while triaging theFitsIn<INT32>assert wall in SuperPMI replay of the wasm altjit.Always relocate wasm handle constants regardless of
compRelocImmedValNeedsRelocisopts.compReloc && IsIconHandle(). Under a runtime-JIT configuration (compRelocoff) a handle icon skipsemitAddressConstantand falls into the literali32.constpath, then assertsFitsIn<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 onIsIconHandle()instead. This is a strict superset of the old condition: identical whencompRelocis on, and additionally routes thecompReloc-off handle case through the reloc path that the real target always takes.Accept any 32-bit pattern when emitting a wasm
i32.constWasm integers are sign-agnostic and
i32.consthas no unsigned form (it is a sign-agnostic 32-bit bit pattern encoded as SLEB128), so an unsigned constant such as0xFFFFFFFFtypedTYP_INTis a valid literal even though it does not fit a signedINT32. The assert is relaxed to also acceptUINT32, 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_testsandlibraries_tests_no_tiered_compilation,FitsIn<INT32>(bits)assertions and total replayISSUEs:FitsInFitsInThe handle-routing fix accounts for essentially all of the reduction; the
i32.constrelaxation additionally unblocks the unsigned-pattern case (e.g.UIntPtrgeneric 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
FitsIncount is dominated by non-handle 64-bit host-address constants (frozen-object / static-base addresses baked as plainCNS_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.