Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm32 SIMD Rust implementation #341

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

monoid
Copy link

@monoid monoid commented Sep 10, 2023

This code is based on rust_sse2.rs of the same distribution, and is subject to further improvements. Some comments are left intact even if their applicability is questionable.

SIMD implementation is gated by wasm32_simd feature, portable version is used otherwise.

Performance measurements with a primitive benchmark with ~16Kb of data:

| M1 native     | 11,610 ns |
| M1 WASM SIMD  | 13,355 ns |
| M1 WASM       | 22,037 ns |
| x64 native    |  6,713 ns |
| x64 intrins.  |  7,979 ns |
| x64 WASM SIMD | 11,985 ns |
| x64 WASM      | 25,978 ns |

wasmtime v12.0.1 was used on both platforms.

Closes #187.

@monoid
Copy link
Author

monoid commented Sep 10, 2023

  • README should be updated too.

Copy link
Member

@oconnor663 oconnor663 left a comment

Choose a reason for hiding this comment

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

Thank you for putting this together! This must've been a lot of work. And the benchmarks look wonderful.

One high-level question we should think about is whether we want to add Wasm support to the C implementation at the same time we add it to the Rust implementation. On the one hand, so far we've always done them both at the same time. On the other hand, Wasm is a very different beast from other SIMD extensions, dealing with different levels of support across different versions of GCC/Clang/MSVC seems like it would be a huge pain. What are your thoughts?

src/platform.rs Outdated Show resolved Hide resolved
src/wasm32_simd.rs Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
src/platform.rs Outdated Show resolved Hide resolved
src/platform.rs Outdated Show resolved Hide resolved
src/wasm32_simd.rs Outdated Show resolved Hide resolved
src/platform.rs Outdated Show resolved Hide resolved
@oconnor663
Copy link
Member

By the way, the MIPS build failure in CI is already fixed, and it should go away if you rebase.

This code is based on rust_sse2.rs of the same distribution, and is
subject to further improvements. Some comments are left intact even if
their applicability is questioned.

SIMD implementation is gated by `wasm32-simd` feature, portable version
is used otherwise.

Performance measurements with a primitive benchmark with ~16Kb of data:

| M1 native     | 11,610 ns |
| M1 WASM SIMD  | 13,355 ns |
| M1 WASM       | 22,037 ns |
| x64 native    |  6,713 ns |
| x64 WASM SIMD | 11,985 ns |
| x64 WASM      | 25,978 ns |

wasmtime v12.0.1 was used on both platforms.

Closes BLAKE3-team#187.
Wasm SIMD API has certain function safe.
@monoid
Copy link
Author

monoid commented Sep 11, 2023

Well, it took about 4 evenings, given that I have very little experience with SIMD instructions beyond general idea. I'm still surprised it works :)

I have very limited knowledge of C support in WASM land, but I've googled nothing except Emscripten compiler. And it seems it can translate SSE4 intrinsics to WASM:
https://emscripten.org/docs/porting/simd.html

Certain functions' unsafety comes from v128 loads and store.  If
argument types guarantee that these loads and stores are safe, function
is declared safe, and internal unsafe blocks are commented.
@monoid monoid changed the title Wasm32 SIMD implementation Wasm32 SIMD Rust implementation Sep 21, 2023
@oconnor663
Copy link
Member

Apologies for the delay in reviewing this PR. I just wanted to mention that I haven't forgotten about it :)

@monoid
Copy link
Author

monoid commented Dec 12, 2023

@oconnor663 Can it be a Christmas gift, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add an implementation using Wasm SIMD
2 participants