Skip to content

JIT: use scalar stores for small wasm zero fills - #137234

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-small-zero-fills
Nov 10, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-small-zero-fills

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Replace constant zero memory.fill operations of 1, 2, 4, or 8 bytes with scalar stores. Share the selection across frame initialization, constant localloc, native initblk, and GC zeroing codegen.

Contain zero-valued initblk operands during lowering so codegen emits the correctly typed zero directly, including i64.const 0 for eight-byte stores. Nonzero fills, dynamic fills, copy operations, and existing explicit null checks retain their current paths.

Size

Havit composite R2R, compared with main at e8b295c1ba0:

Metric Baseline With change Reduction
Wasm owner image 31,554,208 B 31,507,008 B 47,200 B
Brotli-compressed owner image 4,130,939 B 4,129,107 B 1,832 B
gzip-compressed owner image 7,345,541 B 7,345,092 B 449 B
SuperPMI code bytes 14,566,159 B 14,545,919 B 20,240 B

SuperPMI: 38,469 successful contexts per JIT, none larger. No GC-info size or EH-clause-count changes. Code-size figures use successful compilation CSV metrics; the Wasm native differ has known decoder failures and false reports.

The comparison uses identical frozen compiler dependencies and existing local runtime packs, changing only the JIT. Compressed assets were verified to decompress byte-for-byte to their respective images. No runtime-speed improvement is claimed.

Validation

  • build.cmd clr.crossarchtools -os browser -arch wasm -c Release /p:RuntimeFlavor=CoreCLR passed for the clean baseline and changed JIT.
  • build.cmd clr.crossarchtools -os browser -arch wasm -c Checked /p:RuntimeFlavor=CoreCLR passed for the changed JIT.
  • Ad hoc actual-source codegen harness passed 114 baseline and 134 changed cases, including 24 optimization-positive controls; the baseline retains the tiny fills.
  • Generated-Wasm execution passed 496 sentinel/boundary checks and 248 out-of-bounds trap checks.
  • Managed controls passed with matching freshly built Release and Checked JIT/compiler layouts, including forced STRESS_EMITTER for Checked: 48 constant zero/nonzero fill sentinel cases, 30 frame/EH calls, two heap GC-ref zeroing cases, and existing throw-helper/EH, boolean, GC/liveness, and local-use controls.
  • Havit and control owner modules passed wasm-tools validate; jit-format --untidy and git diff --check passed.
  • The ad hoc harnesses are not included in this PR. Full runtime suite, clang-tidy, browser smoke, and runtime timing comparisons were not run for this revision; VM/libraries/runtime packs were not rebuilt.

Contributes to #135985.

Note

This PR description was generated with GitHub Copilot.

Replace constant zero fills of 1, 2, 4, or 8 bytes with scalar stores.
Contain zero-valued initblk operands so codegen emits i64.const 0
directly for 8-byte stores.

Contributes to dotnet#135985.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1da7d7aa-79b4-4ae1-ae4a-9861fdf9681c
@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 Nov 10, 2026
@azure-pipelines

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@AndyAyersMS

Copy link
Copy Markdown
Member Author

I recall there was some discussion about rewriting zero fills as a perf win; here it's just done as a small size win.

@adamperlin PTAL
fyi @dotnet/wasm-contrib

@jkotas jkotas added the arch-wasm WebAssembly architecture label Nov 10, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@AndyAyersMS
AndyAyersMS merged commit 04d981b into dotnet:main Nov 10, 2026
138 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 11, 2026
lewing added a commit that referenced this pull request Nov 14, 2026
<!-- -->

Follow-up to #137234. `genWasmZeroInit` now emits `i32.const 0;
i8x16.splat; v128.store` for constant 16-byte zero fills instead of
`memory.fill`. This covers frame zero-init, constant localloc, native
initblk and GC-ref zeroing. CoreCLR browser-wasm already requires SIMD.

`i8x16.splat` is used rather than a zero `v128.const` because it is 14
bytes smaller per site (8 vs 22 bytes) and was never slower in the
measurements below. It is 1 byte larger than the `memory.fill` sequence
(7 bytes).

### Size

In a composite R2R browser-wasm publish of a small test app (CoreLib +
System.Console + app), 300 of 321 16-byte `memory.fill` sites (of 1,539
total `memory.fill`s) became `v128.store`. The composite image grew from
2,449,568 to 2,449,856 bytes (+288 B).

### Speed

Pure-wasm microbenchmark, 16-byte zero fill, net ns per fill, Apple M5
Max:

| Engine / tier | `memory.fill` | `i8x16.splat` + `v128.store` |
`v128.const 0` + `v128.store` |
|---|---:|---:|---:|
| V8 Liftoff (`node --liftoff-only`) | 2.74 | 0.149 | 0.359 |
| V8 TurboFan (`node --no-liftoff`) | 0.170 | 0.082 | 0.082 |
| JSC BBQ (`jsc --useOMGJIT=false`) | 1.86 | 0.130 | 0.130 |
| JSC default | 0.190 | 0.082 | 0.081 |

Both baseline tiers compile `memory.fill` as an out-of-line call, so
code that has not tiered up gains the most. V8's `--print-wasm-code`
shows both v128 forms as a single `str q0` on arm64.

Composite R2R browser-wasm app on node (best of 7 per run, 5 interleaved
runs, ns/op). These loops run long enough to tier up:

| Kernel | Base | This PR | Change |
|---|---:|---:|---:|
| `arr[i] = default` for 16-byte structs (two `long`s, four refs,
`Guid`, `decimal`, mixed) | 0.386–0.392 | 0.308–0.310 | about -20% |
| Prolog zeroing of a 16-byte struct local with refs | 4.075 | 3.922 |
-3.8% (ranges overlap) |
| `List<(string,string,string,string)>` Add/RemoveAt,
`Dictionary<Guid,object>` set/remove, decimal math | | | within ±0.4% |

The A/B used JITs built from the same commit, swapped into the same
crossgen2 and runtime pack. Another build was running on the machine
during part of the measurements.

### Validation

- `./build.sh clr.crossarchtools -os browser -arch wasm -c Release
/p:RuntimeFlavor=CoreCLR` passed at this commit.
- `./build.sh clr.crossarchtools -os browser -arch wasm -c Checked
/p:RuntimeFlavor=CoreCLR` passed at this commit.
- `python3 src/coreclr/scripts/jitformat.py -a arm64 -o osx -r <repo>`
passed with no changes.
- With the same patch applied at `62278fed5a1`, the composite R2R
publish (`PublishReadyToRun=true`, `PublishReadyToRunComposite=true`)
compiled with no asserts under a Checked JIT. The resulting app ran with
checksums matching the baseline.
- SuperPMI, the runtime and library test suites, x64, and browsers other
than node were not run.

Contributes to #135985.

> [!NOTE]
> This PR description was generated with GitHub Copilot.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture 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.

3 participants