Skip to content

Optimize Wasm comparisons against zero - #135380

Merged
davidwrighton merged 2 commits into
dotnet:mainfrom
davidwrighton:wasm-eqz-codegen
Oct 9, 2026
Merged

davidwrighton merged 2 commits into
dotnet:mainfrom
davidwrighton:wasm-eqz-codegen

Conversation

@davidwrighton

Copy link
Copy Markdown
Member

Summary

  • Contain integral zero operands for Wasm integer equality and inequality comparisons.
  • Emit i32.eqz or i64.eqz instead of a zero constant followed by a general equality instruction.
  • Remove redundant i32 != 0 normalization when the result feeds br_if directly.

General comparison results remain normalized to 0 or 1. Inequality values therefore use a second i32.eqz, while eligible i32 != 0 branches can consume the original value directly. The direct br_if optimization is restricted to i32; i64 values still use i64.eqz because Wasm branch conditions require i32.

Size impact

For a browser/Wasm Release build of System.Private.CoreLib.wasm:

Metric Baseline Updated Difference
Raw size 26,040,664 bytes 25,935,080 bytes -105,584 bytes (-0.4055%)
Brotli q11 — — approximately -3.5 to -3.9 KB

The raw reduction was entirely in the CODE section.

Validation

The following builds completed successfully:

./build.sh -subset clr.wasmjit -c Checked
./build.sh -subset clr.tools+clr.wasmjit -c Checked
./build.sh -subset clr.crossarchtools -os browser -c Release
./build.sh -subset clr.corelib+clr.nativecorelib -os browser -c Release

Focused Crossgen2 probes verified:

  • int == 0 emits i32.eqz.
  • int != 0 values emit i32.eqz; i32.eqz.
  • long == 0 emits i64.eqz.
  • long != 0 values emit i64.eqz; i32.eqz.
  • Eligible i32 != 0 branches feed br_if directly.
  • i64 branches retain the required i64.eqz.
  • Null comparisons emit i32.eqz.
  • Ordered comparisons remain unchanged.

Matched baseline and updated CoreLib disassembly showed:

  • System.TimeZoneInfo.CacheTransitionsForYear: all 48 zero-constant equality sequences removed; 105 instruction bytes saved.
  • System.SpanHelpers.IndexOfAny_0<System.__Canon>: all 49 zero-constant equality sequences removed; 112 instruction bytes saved.

JIT formatting and git diff --check completed successfully.

Resolves #135379

Note

This pull request was drafted with GitHub Copilot.

Use eqz for integer equality comparisons against zero and avoid redundant boolean normalization before eligible br_if instructions.

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 7, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

JulieLeeMSFT
JulieLeeMSFT previously approved these changes Oct 8, 2026

@JulieLeeMSFT JulieLeeMSFT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
The eqz substitutions preserve equality results, and the branch optimization is limited to i32 != 0, so br_if receives the required i32 condition; i64 comparisons retain their conversion.

Keep zero comparisons available for Wasm control-flow reversal and use GTF_RELOP_JMP_USED to emit direct i32 branch conditions during code generation.

Co-authored-by: Copilot <[email protected]>

@adamperlin adamperlin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me as well!

@davidwrighton
davidwrighton merged commit 33ae118 into dotnet:main Oct 9, 2026
137 of 140 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Wasm] Optimize equality comparisons against zero

3 participants