Skip to content

[wasm][JIT] RA declares wasm locals that are never used when a parameter's virtual register index differs from its ABI register #136727

Closed

Description

@lewing

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 → abiReg
  • lvIsParamRegTarget → 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

  • main at c4ad249, checked clrjit_universal_wasm_arm64 on osx-arm64.
  • crossgen2 --targetos:browser --targetarch:wasm -O, compiling System.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 fields V47/V48. They stop coinciding with their ABI registers $1/$2, and the method's i64 declarations grow from local[11..16] to local[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. Deconstruct and the case above match it.

Note

This issue was generated by GitHub Copilot (AI-generated content).

Activity

  1. added
    arch-wasm
    area-CodeGen-coreclr
    on Oct 30, 2026
  2. added
    untriaged
    on Oct 30, 2026
  3. dotnet-policy-service commented on Oct 30, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
    See info in area-owners.md if you want to be subscribed.

  4. dotnet-policy-service commented on Oct 30, 2026

    @dotnet-policy-service
    Contributor

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

  5. added this to the 12.0.0 milestone on Nov 2, 2026
  6. removed
    untriaged
    on Nov 2, 2026
  7. self-assigned this
    on Nov 3, 2026
  8. added a commit that references this issue on Nov 4, 2026
    07b1f05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions