Skip to content

Conversation

@radekdoulik
Copy link
Member

System.Runtime.Intrinsics.Wasm WasmBase class for WebAssembly platform SIMD intrinsics

  • proof of concept with few methods
  • decide how to represent v128.const via Constant method(s) and whether make it part of public API
  • make the API and implementation complete
  • add tests

System.Runtime.Intrinsics Vector128_1 and Vector128 types support on WebAssembly

  • enable
  • check the emitted code
  • update where needed
  • update or add tests

@ghost ghost assigned radekdoulik Apr 11, 2022
@ghost
Copy link

ghost commented Apr 11, 2022

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Apr 11, 2022

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@radekdoulik
Copy link
Member Author

note for me, C#:

    static Vector128<byte> TestSIMD1()
    {
        return WasmBase.Constant(0xff11ff22ff33ff44, 0xff55ff66ff77ff88);
    }

    static Vector128<byte> TestSIMD2()
    {
        return WasmBase.Splat((byte)12);
    }

    static uint TestSIMD4(Vector128<byte> v1, Vector128<byte> v2)
    {
        var v = v1 + v2;
        var l1 = WasmBase.ExtractLane (v, 3);
        v = v1 * v2;
        var l2 = WasmBase.ExtractLane (v, 3);
        return l1 + l2;
    }

compiles to wasm code:

(func Wasm_Console_V8_CJS_Sample_Test_TestSIMD1(param $0 i32, $1 i32))
 local.get $0
 v128.const 0xff11ff22ff33ff44ff55ff66ff77ff88    [SIMD]
 v128.store    [SIMD]

(func Wasm_Console_V8_CJS_Sample_Test_TestSIMD2(param $0 i32, $1 i32))
 local.get $0
 v128.const 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c    [SIMD]
 v128.store    [SIMD]

(func Wasm_Console_V8_CJS_Sample_Test_TestSIMD4_System_Runtime_Intrinsics_Vector128_1_byte_System_Runtime_Intrinsics_Vector128_1_byte(param $0 i32, $1 i32, $2 i32) (result i32))
 local $3 v128
 local $4 v128
 local.get $1
 v128.load align:4    [SIMD]
 local.tee $3
 local.get $0
 v128.load align:4    [SIMD]
 local.tee $4
 i8x16.add    [SIMD]
 i8x16.extract.lane.u    [SIMD]
 local.get $3
 i8x16.extract.lane.u    [SIMD]
 local.get $4
 i8x16.extract.lane.u    [SIMD]
 i32.mul
 i32.const 255
 i32.and
 i32.add

@teo-tsirpanis
Copy link
Contributor

Shouldn't the APIs get approved first before opening a PR?

This will need more work, as it crashes clang during 'WebAssembly
Instruction Selection' pass:

    WasmApp.Native.targets(353,5): error : 3.    Running pass 'WebAssembly Instruction Selection' on function '@corlib_System_Runtime_Intrinsics_Wasm_WasmBase_Shuffle_System_Runtime_Intrinsics_Vector128_1_byte_System_Runtime_Intrinsics_Vector128_1_byte_System_Runtime_Intrinsics_Vector128_1_byte'
@ghost ghost closed this May 22, 2022
@ghost
Copy link

ghost commented May 22, 2022

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

radekdoulik added a commit that referenced this pull request Jun 6, 2022
Add initial SIMD support for wasm. This is subset of the original [draft PR](#67848) without the public API additions.

Add `WasmSIMD` property to enable SIMD in AOT builds. With the property enabled, the apps built with AOT get SIMD intrinsics inlined for parts of `S.R.I.Vector128` and `S.R.I.Vector128<T>` API.

Add test to build and run a simple app with SIMD enabled.

* Initial wasm SIMD support

* Enable Vector intrinsic on wasm

The llvm code generator works nicely with them.

* Add missing files

* Make SIMD support conditional

* Remove test code

* Fix debug build

* Update after merge

* Add Splat and ExcractLane methods

* Switch i64 values for Constant method

So that C#

    WasmBase.Constant(0xff11ff22ff33ff44, 0xff55ff66ff77ff88)

is compiled into wasm code

    v128.const 0xff11ff22ff33ff44ff55ff66ff77ff88    [SIMD]

* Update PlatformNotSupported version of WasmBase

* Fix CI build

* Add ReplaceLane and Swizzle

* Change WasmBase.Constant to get Vector128 as input

* Add Shuffle methods

This will need more work, as it crashes clang during 'WebAssembly
Instruction Selection' pass:

    WasmApp.Native.targets(353,5): error : 3.    Running pass 'WebAssembly Instruction Selection' on function '@corlib_System_Runtime_Intrinsics_Wasm_WasmBase_Shuffle_System_Runtime_Intrinsics_Vector128_1_byte_System_Runtime_Intrinsics_Vector128_1_byte_System_Runtime_Intrinsics_Vector128_1_byte'

* Handle SN_Shuffle

* Fix crash in OP_STOREX_MEMBASE

* Add build test

* Set WasmSIMD to false as default value

Also add "experimental" to the property comment

* Remove public API, it will be part of another PR

* Add link to the llvm issue

* Review feedback

* Review feedback
@ghost ghost locked as resolved and limited conversation to collaborators Jul 1, 2022
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants