Skip to content

[wasm] Take wasm SIMD instruction sets from the compiler; default relaxed SIMD on for WASI - #137112

Merged
lewing merged 21 commits into
dotnet:mainfrom
lewing:lewing-wasi-relaxed-simd-opt-in
Nov 15, 2026
Merged

lewing merged 21 commits into
dotnet:mainfrom
lewing:lewing-wasi-relaxed-simd-opt-in

Conversation

@lewing

@lewing lewing commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Enables wasm relaxed SIMD by default on WASI for both CoreCLR ReadyToRun and NativeAOT. Browser stays opt-in, and Mono is unchanged.

Target Relaxed SIMD Opt out / in
CoreCLR WASI (R2R) On PublishReadyToRunInstructionSet=-relaxed-simd
NativeAOT WASI On IlcInstructionSet=-relaxed-simd
CoreCLR browser (R2R) Off PublishReadyToRunInstructionSet=relaxed-simd
Mono Not implemented —

Instruction sets come from the compiler

  • WASI default. InstructionSetHelpers adds relaxed-simd to the default instruction sets for wasm32 WASI, so crossgen2 and ILC both pick it up without callers passing anything.
  • PublishReadyToRunInstructionSet. A general crossgen2 property that maps to --instruction-set: and accepts +x/-x relative to the defaults, like IlcInstructionSet for NativeAOT. It replaces the browser-only WasmEnableRelaxedSimd, which only fed the same crossgen2 switch plus native flags no runtime code needed (-mrelaxed-simd, a relink trigger and a workload requirement). wasm-opt now always enables relaxed SIMD, so the ReadyToRun wasm-opt pass accepts relaxed images. Microsoft.NET.CrossGen.targets is shared with dotnet/sdk, so a matching sdk change will follow.
  • Images are checked against CoreLib. Wasm has no runtime ISA detection. The runtime now adopts the instruction sets CoreLib's R2R image was compiled for, and every other image is checked against them; a mismatch is rejected like on other platforms. This removes the browserhost hook that set the flag from a runtime config property.
  • NativeAOT. NativeAOT's CoreLib now compiles the wasm intrinsics. ILC maps System.Runtime.Intrinsics.Wasm types to instruction sets, and the JIT treats wasm ISA support as exact under the NativeAOT ABI, so IsSupported is a compile-time constant. SupportsWasmIntrinsics is now true for every wasm CoreLib, and relaxed SIMD for every non-Mono one.
  • WasiEnableCompositeR2R removed. WASI R2R already requires PublishReadyToRunComposite=true, so the host link flags depend only on PublishReadyToRun.
  • No trimmer folding of IsSupported on CoreCLR. The JIT and interpreter decide support per method, so folding could make interpreted code take an intrinsic path and throw. Measured on a trimmed WASI R2R app it saved about 2.7 KB of R2R code (6 KB of IL). The substitutions remain for Mono only.
  • Incremental publish. CoreLib's crossgen step records PublishReadyToRunInstructionSet, so changing it recompiles CoreLib. Changing crossgen2 arguments doesn't recompile app images on any target; Recompile ReadyToRun images when the crossgen2 arguments change #137563 fixes that in the common crossgen2 targets.
  • SDK mapping pending. App publishes use the SDK's copy of the crossgen2 targets, which doesn't map PublishReadyToRunInstructionSet yet (ReadyToRun: recompile when crossgen2 arguments change; map PublishReadyToRunInstructionSet sdk#57127). Until it flows, the browser opt-in and the WASI opt-out are no-ops in app publishes, and WasmSIMDTests.PublishRelaxedSimdEnabledCoreClr is disabled against [wasm] The SDK's crossgen2 targets ignore PublishReadyToRunInstructionSet #137571. In-repo builds (CoreLib, runtime tests) use the runtime's copy and already honor it.

simd128 remains required on wasm; #137553 tracks making that explicit by folding it into base.

Trade-offs of the WASI default

  • There's no runtime feature detection, so a host without relaxed SIMD fails to load the module. Use the opt-out for such hosts.
  • Relaxed SIMD results can differ by host CPU.
  • The pinned wasmtime 49.0.1 enables relaxed SIMD by default.

Tests

  • hardware-intrinsics-wasm pipeline. The browser leg now really enables relaxed SIMD; before, the property was never forwarded. A new WASI leg uses WholeProgramCompositeBuildMode, which compiles CoreLib into each test composite.
  • Other WASI R2R runtime tests run R2R code against an interpreted CoreLib ([wasi][R2R] Runtime tests run R2R code against an interpreted CoreLib #134949), so they default to the baseline instruction sets, and two PackedSimdTests stay quarantined there.
  • Library tests. System.Runtime.Intrinsics.Tests now builds for WASI, and the PackedSimd/RelaxedSimd tests run on browser and WASI.
  • Wasm.Build.Tests. PublishRelaxedSimdCoreClr publishes R2R, runs relaxed operations when enabled, and expects PlatformNotSupportedException when disabled.

Validation (local, wasmtime 49.0.1)

  • CoreCLR WASI HardwareIntrinsics_Wasm_r. Whole-program mode with relaxed SIMD: 73 passed, 1 skipped (the test that expects relaxed SIMD to be unsupported). Default mode: 72 passed, 2 skipped (the [wasi][R2R] Runtime tests run R2R code against an interpreted CoreLib #134949 quarantines).
  • NativeAOT WASI HardwareIntrinsics_Wasm_r. Default: 73 passed, 1 skipped, and the module is rejected under -W relaxed-simd=n. With IlcInstructionSet=-relaxed-simd: all 74 pass under -W relaxed-simd=n. The NativeAOT WASI CI leg only builds, so these runs are local only.
  • System.Runtime.Intrinsics.Tests on WASI. Trimmed R2R: 13,340 passed. Interpreter: 13,296 passed. Opt-out: 53 of 53 passed.
  • WASI app. R2R default, opt-out, and non-R2R publishes all behave correctly.
  • Browser is covered by CI only.

Relates to #130223, #134949, #137553

Note

This PR description was generated with GitHub Copilot.

lewing and others added 4 commits November 6, 2026 14:54
- WasiApp.CoreCLR.targets: default WasmEnableRelaxedSimd=true; when on, pass
  --instruction-set:relaxed-simd to crossgen2 and set the
  System.Runtime.Intrinsics.Wasm.RelaxedSimd.IsSupported runtime property.
- wasihost and corerun (wasm): enable the RelaxedSimd instruction set when that
  property is true, as browserhost does.
- src/tests: apply WasmEnableRelaxedSimd to crossgen2 and corerun for browser and
  WASI (default on for WASI), and forward it from build.proj to test projects.
  Previously /p:WasmEnableRelaxedSimd=true on the hardware-intrinsics-wasm lane
  reached neither, so RelaxedSimd.IsSupported stayed false.
- Add a RelaxedSimd test asserting IsSupported matches the host property under R2R.
- Add a wasi_wasm leg to hardware-intrinsics-wasm.
- Document the WASI default and trade-offs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…34949

PackedSimdIsSupported, RelaxedSimdIsSupportedReflects and LoadStoreNullCheckTest
call into CoreLib at run time, which is interpreted in WASI ReadyToRun test
composites. Skip them only when IsWasi && IsReadyToRunCompiled; they still run in
the WASI interpreter lane and on browser. Use the shared IsWasmReadyToRun
predicate for IsSupportedMatchesHostConfiguration.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Wasm has no runtime feature detection, so the runtime's CPU flags cannot
confirm the instruction sets a ReadyToRun image requires. Previously hosts
read a System.Runtime.Intrinsics.Wasm.RelaxedSimd.IsSupported runtime property
and called an exported coreclr_wasm_set_relaxed_simd_supported() so the
image-wide instruction set fixup would pass.

The engine already validates the code when it compiles the module, so on wasm
treat instruction sets an image requires as supported. Must-be-unsupported
assertions are still checked against the runtime flags. This removes the
exported setter, the browserhost/wasihost/corerun property handling, and the
RuntimeHostConfigurationOption items; WasmEnableRelaxedSimd now only selects
--instruction-set:relaxed-simd (and the browser emcc flags).

PublishRelaxedSimdCoreClr now publishes ReadyToRun and checks
RelaxedSimd.IsSupported instead of the removed runtime property.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

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

Run ConvertToInt32Native and MultiplyAddEstimate through NoInlining methods and
check their results when relaxed SIMD is enabled, and check that the opt-out
throws PlatformNotSupportedException, instead of only printing IsSupported.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@jkotas jkotas added arch-wasm WebAssembly architecture area-ReadyToRun and removed area-Infrastructure-coreclr Only use for closed issues labels Nov 6, 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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/crossgen-contrib
See info in area-owners.md if you want to be subscribed.

IsSupported should be consistent across interpreted and compiled code; the
mismatch is tracked by dotnet#134949.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
lewing and others added 2 commits November 6, 2026 16:34
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The browser leg runs non-composite: CoreLib's R2R image is precompiled at build
time while the tests are crossgen'd with --instruction-set:relaxed-simd, so the
two images disagreed on RelaxedSimd.IsSupported. Let crossgen-corelib.proj add
the instruction set when WasmEnableRelaxedSimd=true and pass it to the leg's
runtime build.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Comment thread src/tests/Directory.Build.targets Resolved
Comment thread src/coreclr/vm/jitinterface.cpp Resolved
lewing and others added 2 commits November 8, 2026 15:28
… CoreLib

- InstructionSetHelpers adds relaxed-simd to the default WASI instruction sets,
  so crossgen2 and ILC callers no longer pass it.
- Add PublishReadyToRunInstructionSet, the ReadyToRun counterpart of
  IlcInstructionSet, and drop the WASI WasmEnableRelaxedSimd plumbing. Opt out
  with PublishReadyToRunInstructionSet=-relaxed-simd.
- On wasm the runtime takes its instruction sets from CoreLib's ReadyToRun
  image and validates other images against them, instead of skipping the check.
- WASI src/tests ReadyToRun run with an interpreted CoreLib (dotnet#134949), so they
  default to -relaxed-simd.
- Remove WasiEnableCompositeR2R; PublishReadyToRunComposite is already required,
  so the host's composite link flags follow PublishReadyToRun.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add the WASI target framework to System.Runtime.Intrinsics.Tests and run
PackedSimdTests and RelaxedSimdTests on WASI as well as browser, so the WASI
library ReadyToRun lane exercises relaxed SIMD with CoreLib in the composite.
Drop the tests.wasi.targets default; the substitutions it fed are not used on
WASI.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Resolve src/tests/Directory.Build.targets: keep main's nativeaot exclusion on the
CLRTest.WasmCorerun.targets import and this branch's instruction-set block.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing
lewing marked this pull request as ready for review November 10, 2026 21:51
WASI publishing records a hash of the crossgen2 arguments as an input of
_CreateR2RImages and deletes stale images when it changes, as browser
already does. CoreLib's crossgen step records PublishReadyToRunInstructionSet.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing

lewing commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

I'm going to combine this with #137114 since the split is reducing clarity rather than adding it.

Folds in dotnet#137114. NativeAOT's CoreLib now compiles the wasm intrinsics:
- ILC maps System.Runtime.Intrinsics.Wasm types to instruction sets in
  GetHardwareIntrinsicId, so it supplies the IsSupported bodies.
- The JIT treats wasm ISA support as exact under the NativeAOT ABI, since
  wasm has no runtime ISA detection.
- SupportsWasmIntrinsics is true for every wasm CoreLib, and relaxed SIMD
  for every non-Mono one.
NativeAOT WASI picks up relaxed SIMD from the compiler's WASI default; opt
out with IlcInstructionSet=-relaxed-simd, which src/tests/build.proj now
forwards to the inner test build.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing lewing changed the title [wasm][coreclr] Enable relaxed SIMD from the crossgen2 instruction set; default it on for CoreCLR WASI [wasm] Take wasm SIMD instruction sets from the compiler; default relaxed SIMD on for WASI Nov 14, 2026
…nstructionSet

Relaxed SIMD only affects ReadyToRun code, so browser opts in the same way as
WASI: PublishReadyToRunInstructionSet=relaxed-simd. WasmEnableRelaxedSimd
duplicated that and could disagree with it.

- Drop -mrelaxed-simd, WASM_ENABLE_RELAXED_SIMD, the relink trigger and the
  workload requirement; no native code uses relaxed SIMD.
- Always enable relaxed SIMD in wasm-opt, so the ReadyToRun wasm-opt pass
  accepts images built with it. Enabling a feature only allows it.
- Stop folding IsSupported with trimmer substitutions on CoreCLR. The JIT and
  interpreter decide support per method, and measured on a trimmed WASI R2R
  app the folding saved about 2.7 KB of R2R code. Delete the relaxed SIMD
  substitution files.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Nov 14, 2026
Comment thread src/coreclr/crossgen-corelib.proj Resolved
Changing crossgen2 arguments doesn't recompile R2R images on any target, so the
fix belongs in the common crossgen2 targets rather than WASI's. Also drop the
instruction-set comment in crossgen-corelib.proj, per review.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing
lewing enabled auto-merge (squash) November 15, 2026 01:01
The SDK's copy of the crossgen2 targets doesn't map PublishReadyToRunInstructionSet
yet, so app publishes outside this repo dropped it: browser's relaxed SIMD opt-in
and WASI's opt-out had no effect. The browser and WASI targets now add
--instruction-set themselves unless crossgen2 already gets one.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
lewing and others added 2 commits November 14, 2026 20:03
…37571

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… targets

Remove the fallback targets that mapped PublishReadyToRunInstructionSet
for app publishes and disable the enabled case of PublishRelaxedSimdCoreClr
against dotnet#137571 until an SDK with the mapping flows.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
lewing added a commit to lewing/sdk that referenced this pull request Nov 15, 2026
…t argument

Matches dotnet/runtime's copy of the crossgen2 targets (dotnet/runtime#137112),
so the property works for app publishes, as IlcInstructionSet does for NativeAOT.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing
lewing requested a review from jkotas November 15, 2026 04:00
@lewing

lewing commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

/ba-g failures are not related

@lewing
lewing merged commit b48f2ac into dotnet:main Nov 15, 2026
184 of 187 checks passed
lewing added a commit that referenced this pull request Nov 18, 2026
)

`_CreateR2RImages` only compares files, so an incremental publish that
changes only the crossgen2 arguments reuses stale ReadyToRun images.
Jan's repro: publish a console app with `/p:PublishReadyToRun=true
/p:PublishReadyToRunCrossgen2ExtraArgs=--instructionset:avx2`, then
again with `avx512`. The second publish does not recompile.

## Change

This follows the established SDK pattern (for example,
`CreateGeneratedAssemblyInfoInputsCacheFile`). A new target,
`_CreateReadyToRunCompilerArgumentsCacheFile`, runs before
`_CreateR2RImages`. It uses the `Hash` task to hash:

- `PublishReadyToRunCrossgen2ExtraArgs`
- the target-computed `_PublishReadyToRunCrossgen2ExtraArgs`
- `PublishReadyToRunInstructionSet` (via
`_ReadyToRunInstructionSetArgs`)
- `PublishReadyToRunComposite`
- `PublishReadyToRunContainerFormat`
- `PublishReadyToRunUseCrossgen2`
- `PublishReadyToRunPerfmapFormatVersion`

It writes the hash to
`$(IntermediateOutputPath)$(MSBuildProjectName).ReadyToRunCompilerArguments.cache`
only when the hash changes. `_CreateR2RImages` lists that file as an
input, so changing any of these values makes every image out of date.

**Target ordering:** `BeforeTargets` hooks run in declaration order. The
new target therefore sees arguments added by hooks declared earlier,
including project and `Directory.Build.targets` hooks. Hooks imported
after the SDK targets must also run before
`_CreateReadyToRunCompilerArgumentsCacheFile`, or list their own inputs
in `_ReadyToRunCompilerInputs`. The WASI and browser hooks that add
`--directpinvoke` already list their pinvoke manifests that way. The
browser `--partial` PGO argument is tracked by
`browser-r2r-state.stamp`, which is left unchanged; it also tracks the
PGO file list and the wasm-opt post-processing state.

**`PublishReadyToRunInstructionSet` with command-line extra args:**
#137112 appended `--instruction-set:` to
`PublishReadyToRunCrossgen2ExtraArgs` at evaluation time. When that
property is passed with `-p:`, it is a global property, so the append
was ignored and the instruction set was silently dropped. maraf found
this in dotnet/sdk#57127. The instruction set now goes through an
internal `_ReadyToRunInstructionSetArgs` property, which both
`RunReadyToRunCompiler` calls pass and the hash includes. This mirrors
dotnet/sdk@485b9063d4.

## Validation

All runs used a `dotnet new console` app with the .NET 11 SDK
(11.0.100-preview.6), `-r osx-arm64 -p:PublishReadyToRun=true`, and the
in-tree targets imported via `-p:AfterMicrosoftNETSdkTargets=<this
file>`:

| Scenario | Stock SDK | This PR |
|---|---|---|
| Change `PublishReadyToRunCrossgen2ExtraArgs`
(`--instruction-set:apple-m1` → `armv8.2-a`) | `_CreateR2RImages`
skipped (bug) | rebuilt |
| Unchanged republish | — | skipped |
| Switch back to the earlier args | — | rebuilt |
| Args added to `_PublishReadyToRunCrossgen2ExtraArgs` by a
`BeforeTargets="_CreateR2RImages"` hook, then changed | — | rebuilt; the
new ISA was on the crossgen2 command line |
| Self-contained `PublishReadyToRunComposite=true` → `false` | — |
rebuilt |
| Failing args (`--bogus-option`), republish, then revert to the earlier
args | — | the failed compile retried, then reverting recompiled |
| Source-only change with the same args, then republish | — | rebuilt
once, then skipped |
| `PublishReadyToRunInstructionSet=apple-m1` with
`-p:PublishReadyToRunCrossgen2ExtraArgs=--Ot` | `--instruction-set`
dropped (main's targets) | both arguments passed to crossgen2 |
| Change `PublishReadyToRunInstructionSet`, then republish unchanged | —
| rebuilt, then skipped |
| Invalid `PublishReadyToRunInstructionSet` | — | crossgen2 reports the
invalid instruction set |

I did not build a WASI or browser CoreCLR app. The hook scenario above
uses a hook declared in `Directory.Build.targets`.

## dotnet/sdk

The SDK has the same targets in
`src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.CrossGen.targets`.
The equivalent change is in dotnet/sdk#57127. It also hashes
`PublishReadyToRunCrossgen2CompositeExtraArgs`, which only exists there,
and adds a ReadyToRun publish test that changing the crossgen2 arguments
recompiles.

Relates to #137112 (review comments:
#137112 (comment),
#137112 (comment))

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

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-ReadyToRun linkable-framework Issues associated with delivering a linker friendly framework os-wasi Related to WASI variant of arch-wasm

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants