Skip to content

[Wasm] Optimize equality comparisons against zero #135379

Closed

Description

@davidwrighton

The WebAssembly JIT currently emits general equality instructions for integer comparisons against zero:

i32.const 0
i32.eq

or:

i32.const 0
i32.ne

These can use i32.eqz/i64.eqz. When an i32 != 0 comparison feeds a conditional branch, the value can be consumed directly by br_if.

For a browser/Wasm Release build of System.Private.CoreLib.wasm, applying these transformations reduced:

  • Raw size from 26,040,664 to 25,935,080 bytes: 105,584 bytes (0.4055%).
  • Brotli q11 size by approximately 3.5-3.9 KB.

In two representative CoreLib methods, all 97 const 0 followed by eq/ne sequences were removed:

  • System.TimeZoneInfo.CacheTransitionsForYear: 105 instruction bytes saved.
  • System.SpanHelpers.IndexOfAny_0<System.__Canon>: 112 instruction bytes saved.

Ordered comparisons against zero must remain unchanged, and i64 values must be normalized with i64.eqz before use by br_if, which requires an i32 condition.

Note

This issue was drafted with GitHub Copilot.

Activity

  1. added
    area-CodeGen-coreclr
    on Oct 7, 2026
  2. added
    untriaged
    on Oct 8, 2026
  3. dotnet-policy-service commented on Oct 8, 2026

    @dotnet-policy-service
    Contributor

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

  4. added this to the 12.0.0 milestone on Oct 8, 2026
  5. removed
    untriaged
    on Oct 8, 2026
  6. added a commit that references this issue on Oct 9, 2026
    33ae118
  7. locked and limited conversation to collaborators on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions