Skip to content

[Wasm][CoreCLR] Preserve native remainder instead of expanding constant MOD to SUB/MUL/DIV #135982

Description

@adamperlin

Description

CoreCLR Wasm expands constant signed remainder into subtract/multiply/divide even though Wasm has a native remainder instruction. Consider preserving MOD for this target.

Generated code comes from CodegenStudy.Cases.RemainderThree:

[MethodImpl(MethodImplOptions.NoInlining)]
static int RemainderThree(int value)
{
    return value % 3;
}

Configuration

Revision 29d644d27d095059a3098156166e0dc4f9d513ed; browser wasm32 CoreCLR ReadyToRun, optimized Release with symbols. Compared against checkout-matched Mono LLVM AOT; Checked CoreCLR cross-JIT used separately for phase attribution.

Regression?

Not established; target-specific optimization opportunity.

Data

Final Wasm arithmetic from that method, excluding CoreCLR frame setup:

;; CoreCLR: $1=value
(i32.sub (local.get $1)
 (i32.mul (i32.div_s (local.get $1) (i32.const 3)) (i32.const 3)))
;; Mono: $0=value
(i32.rem_s (local.get $0) (i32.const 3))

Whole encoded bodies are 27 bytes (CoreCLR) versus 7 (Mono), with differing ABI/frame overhead. Keeping CoreCLR's frame setup unchanged, direct remainder would structurally avoid six encoded bytes and two arithmetic operators; this is an estimate, not a built patch.

The Checked dump identifies the origin in global morph:

Morphing MOD/UMOD [000002] to Sub/Mul/Div

Analysis

morph.cpp applies the signed non-power-of-two constant transformation through a non-ARM64 branch whose comment describes XARCH magic division. Wasm later retains ordinary division; codegenwasm.cpp already supports i32.rem_s.

Investigate target-aware gating/costing rather than inheriting this morph. % 3 can directly preserve signed semantics, including negative inputs, without divisor-zero or division-overflow cases. Other constants, especially % -1, need separate semantic care.

No timing was collected for this example; a browser may canonicalize both forms to similar host code. Division by eight is not part of this claim: both compared runtimes retained Wasm division there.

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. self-assigned this
    on Oct 16, 2026
  6. removed
    untriaged
    on Oct 16, 2026
  7. added this to the 12.0.0 milestone on Oct 16, 2026
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