Repository navigation
Description
Description
WasmRegAlloc::ResolveReferences declares more wasm locals than the method uses. In System.Private.CoreLib compiled for browser-wasm R2R, about 8.6% of declared locals (22,998 of 266,295, in 11,380 of 34,696 methods) are never referenced by any local.get, local.set or local.tee.
This adds to the local-count problem in #135139. Unused locals still have to be declared, zero-initialized and handled by baseline tiers that don't optimize locals.
Reproduction Steps
On main (c4ad249), crossgen2 CoreLib with the checked wasm cross JIT and --codegenopt:JitDisasm=*, then compare each method's local[a..b] declarations with the indices it actually references. Small example, System.TupleExtensions:Deconstruct[System.__Canon,System.__Canon](System.Tuple2[...],byref,byref)`:
; V02 arg0 [V02,T00] ( 4, 4 ) ref -> $2
; V03 arg1 [V03,T02] ( 3, 3 ) byref -> $3
; V04 arg2 [V04,T03] ( 3, 3 ) byref -> $4
...
local.cnt 1
local[6..8] type=i32
All three parameters live in their ABI registers ($2–$4), but the method still declares three extra i32 locals ($6..$8) that the body never touches.
Expected behavior
A candidate homed in its ABI parameter register should not also get a declared local.
Actual behavior
In ResolveReferences (src/coreclr/jit/regallocwasm.cpp, around lines 1056–1072), the root-function declared count is reduced only when the local's virtual register equals the ABI register:
if ((argVarDsc->GetRegNum() == argReg) || (argLclNum == m_compiler->lvaWasmSpArg))
{
...
virtToPhysRegMap[static_cast<unsigned>(argType)].DeclaredCount--;
}
...
if ((mapping != nullptr) &&
(m_compiler->lvaGetDesc(mapping->LclNum)->GetRegNum() == argReg))
{
virtToPhysRegMap[static_cast<unsigned>(argType)].DeclaredCount--;
}At this point GetRegNum() is still the virtual register that InitializeCandidate assigned, so the two are equal only when the virtual and ABI indices happen to coincide. allocPhysReg, however, maps the local to its ABI register whenever the types match:
lvIsRegArg && !lvIsStructField→abiReglvIsParamRegTarget→ the mapping's register
So when the indices differ, the parameter's virtual register gets no physical local, but DeclaredCount still counts it, and the method declares unused locals.
The declared count must still match allocPhysReg exactly. If it is reduced for a local that does not end up in its ABI register, the count is too low and the emitted indices are invalid. A fix should probably derive both from one predicate.
Regression?
Unknown.
Known Workarounds
None needed for correctness; the extra locals are only wasted.
Configuration
mainat c4ad249, checkedclrjit_universal_wasm_arm64on osx-arm64.- crossgen2
--targetos:browser --targetarch:wasm -O, compilingSystem.Private.CoreLib.
Other information
- Found while working on [wasm] [JIT]: Local copy coalescing #136662. Its fix forwards a single-use copy in
System.Number:ConvertDecimalIeee754ToInteger[System.Numerics.Decimal128,System.UInt128,ulong], which shifts the virtual register indices of the promoted parameter fieldsV47/V48. They stop coinciding with their ABI registers$1/$2, and the method'si64declarations grow fromlocal[11..16]tolocal[11..17]even though one fewer local is used. - I haven't measured how many of the 22,998 unused locals come from this specific cause.
Deconstructand the case above match it.
Note
This issue was generated by GitHub Copilot (AI-generated content).
Activity
dotnet-policy-service commentedon Oct 30, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Oct 30, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: fix Wasm local declaration counts (dotnet#136956)