Skip to content

[Wasm RyuJit] codegen for some intrinsics#124575

Merged
AndyAyersMS merged 4 commits intodotnet:mainfrom
AndyAyersMS:WasmSomeIntrinsics
Feb 21, 2026
Merged

[Wasm RyuJit] codegen for some intrinsics#124575
AndyAyersMS merged 4 commits intodotnet:mainfrom
AndyAyersMS:WasmSomeIntrinsics

Conversation

@AndyAyersMS
Copy link
Member

Handle some math intrinsics that map to Wasm operations.

Handle some math intrinsics that map to Wasm operations.
Copilot AI review requested due to automatic review settings February 18, 2026 23:20
@AndyAyersMS
Copy link
Member Author

@dotnet/jit-contrib PTAL

@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 Feb 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds initial WebAssembly (Wasm) RyuJIT support for lowering selected System.Math intrinsics directly to Wasm numeric instructions, enabling more operations to be emitted without helper calls.

Changes:

  • Teach the importer which System.Math intrinsics are target-supported on TARGET_WASM.
  • Add Wasm codegen handling for GT_INTRINSIC and emit the corresponding Wasm instructions for supported intrinsics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/coreclr/jit/importercalls.cpp Adds a TARGET_WASM arm to Compiler::IsTargetIntrinsic for a set of System.Math intrinsics.
src/coreclr/jit/codegenwasm.cpp Handles GT_INTRINSIC in Wasm codegen and introduces CodeGen::genIntrinsic to emit Wasm ops for supported intrinsics.

@am11 am11 added the arch-wasm WebAssembly architecture label Feb 18, 2026
@dotnet-policy-service
Copy link
Contributor

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

Copilot AI review requested due to automatic review settings February 18, 2026 23:43
@EgorBo
Copy link
Member

EgorBo commented Feb 18, 2026

I think you'd want @tannergooding's review. These intrinsics are tricky around IEEE754.

E.g. it seems f32.max in WASM corresponds to IEEE 754-2019's maximum. Is it the same for Math.Max? Same concerns for min, round

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

@AndyAyersMS
Copy link
Member Author

The Wasm spec says:

NaN Propagation

When the result of a floating-point operator other than fneg, fabs, or fcopysign is a NaN, then its sign is non-deterministic and the payload is computed as follows:
• If the payload of all NaN inputs to the operator is canonical (including the case that there are no NaN inputs), then the payload of the output is canonical as well.
• Otherwise the payload is picked non-deterministically among all arithmetic NaNs; that is, its most significant bit is 1 and all others are unspecified.

If we must explicitly check for and propagate NaN inputs this may tip the scale in favor of always using a helper call (especially for binops).

@SingleAccretion
Copy link
Contributor

Notably, the NaN propagation issue affects all FP operations, not just intrinsics. What are our actual semantic requirements for it (required for all ops, required only for min/max, required for all Math)?

@kg
Copy link
Member

kg commented Feb 20, 2026

I will note that from glancing at the interpreter code, it uses native floating point operations in C++ and doesn't take any steps to preserve NaNs. I ran a snippet of the interpreter through godbolt and it doesn't look like clang generated any special wasm to preserve NaNs for those operations either (I looked at add and mul).

So for consistency with the interpreter as it currently exists, we'd want to use the native opcodes in most cases, I think.

@AndyAyersMS
Copy link
Member Author

@tannergooding this is the issue I pinged you about offline...

@tannergooding
Copy link
Member

tannergooding commented Feb 20, 2026

Left some comments above that talked about things both ways.

The TL;DR is that we want to propagate if possible for consistency, but there is no strict requirement to do so and it is still compliant if a runtime canonicalizes their NaN results (most just don't because its faster to not).

WASM recommends that implementations do propagate matching the IEEE 754 spec and anything that isn't will have the same issue for x + y or Truncate(x), etc. So us not guaranteeing Max(NaN, y) returns the exact NaN input is then consistent with the general WASM experience on such a target (WASM guaranteeing some NaN returns for such a case is all that's actually needed, technically speaking).

@AndyAyersMS
Copy link
Member Author

/ba-g android timeouts

@AndyAyersMS AndyAyersMS merged commit e2a94a1 into dotnet:main Feb 21, 2026
121 of 128 checks passed
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.

7 participants