Skip to content

[Wasm][CoreCLR] Loop-hoisting profitability uses a negative native-register budget #135979

Open

Description

@adamperlin

Description

CoreCLR Wasm loop hoisting recognizes an invariant but rejects it using a native-register budget of -2. Investigate a Wasm-aware profitability model.

Generated code comes from CodegenStudy.Cases.CountedLoop (optimized C#, default unchecked arithmetic):

[MethodImpl(MethodImplOptions.NoInlining)]
static int CountedLoop(int count, int scale)
{
    int result = 0;
    for (int index = 0; index < count; index++)
        result += index * (scale + 3);
    return result;
}

Configuration

Revision 29d644d27d095059a3098156166e0dc4f9d513ed; browser wasm32 CoreCLR ReadyToRun, Release with --optimize, symbols retained. Checked cross-JIT used separately for diagnostics, with the same compilation inputs and --parallelism:1 --codegenopt:JitDump=*.

Regression?

Not established; observed optimization gap.

Data

The delivered 66-byte Wasm body retains scale + 3 inside the loop:

;; CountedLoop: $4=index, $2=scale, $5=result
(local.set $5
 (i32.add
  (i32.mul (local.get $4) (i32.add (local.get $2) (i32.const 3)))
  (local.get $5)))

The Checked dump reports:

[000012] hoistable
tree cost too low: 3 < 6 (loopVarCount 4 >= availRegCount 4294967294)
... not profitable to hoist

Analysis

optimizer.cpp computes CNT_CALLEE_SAVED - 1, plus CNT_CALLEE_TRASH - 1 for a call-free loop. targetwasm.h defines both counts as zero, while providing separate nonzero *_FOR_CSE estimates. The signed budget is -2, displayed unsigned in the diagnostic; the profitability check consequently treats the loop as register-constrained.

Consider adapting the heuristic to Wasm virtual locals and auditing related native-register-budget consumers. Preserve zero-trip/overflow semantics and validate with a loop that cannot be eliminated entirely. No isolated speedup has been measured: Mono eliminates this synthetic loop, so its comparative timing cannot quantify the missed hoist.

Note

This issue was prepared by GitHub Copilot from a locally executed codegen comparison study.

Activity

  1. added
    arch-wasm
    tenet-performance
    area-CodeGen-coreclr
    on Oct 16, 2026
  2. dotnet-policy-service commented on Oct 16, 2026

    @dotnet-policy-service
    Contributor

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

  3. added
    untriaged
    on Oct 16, 2026
  4. dotnet-policy-service commented on Oct 16, 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 Oct 19, 2026
  6. kotlarmilos commented on Oct 19, 2026

    @kotlarmilos
    Member

    @adamperlin Can I assign you to this issue?

  7. removed
    untriaged
    on Oct 19, 2026
  8. self-assigned this
    on Oct 19, 2026
  9. adamperlin commented on Oct 19, 2026

    @adamperlin
    ContributorAuthor

    7. @adamperlin Can I assign you to this issue?

    Thanks for asking, I just assigned it to myself!

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-coreclrtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions