Live data from Hacker News

Exploring SIMD performance improvements in WebAssembly (2021)

awelm.com

11–20 of 25 posts

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#11

I wonder if the 4x performance boost of 128-bit SIMD over 32-bit would drop to 2x if WebAssembly added 64-bit registers/instructions.

I actually tried comparing 128-bit SIMD to the 64-bit performance and the difference was 2x. I only published the results for the 4x comparison, but it should be pretty easy to reproduce if you change the types in the non-SIMD code[1] from i32 -> i64.

[1] https://github.com/awelm/simd-wasm-profiling/blob/master/fil...

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#12
post #6

Earlier quoted context omitted.

Why not? Fixed-size SIMD architectures use mostly the same operations, so if you target SSE2 initially, the code should run just fine on NEON. A runtime that ships a JIT compiler also has the unique opportunity to further optimize SIMD code by using more lanes or limiting the working set to the host platform's L1 cache size. Even the AOT compilers like GCC or clang emulate platform-specific intrinsics using generic v…

They are similar but not the same, for instance SSE has movemask, but NEON does not, so it gets emulated(slowly) when targeting that platform. The cross lane ops are different enough that you might need to rewrite for other platforms. And then you run into situations where an instruction is very fast on one architecture but horribly slow on another because its basically emulated.

This isn't really relevant to wasm, though. You can't expect it to support platform-specific hacks just for SIMD, so you'll have to make do with the lowest common denominator anyway.

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#13
post #8

I compared against native: #define ITERATIONS 1000 int main() { const size_t BUFFER_SIZE = 64ul \* 1024 \* 1024; __m128i\* data_buffer = (__m128i *)memalign(64, BUFFER_SIZE); const __m128i all_ones = _mm_set1_epi8(0xFF); for (size_t i = 0; i I had to fixup the WAT because set_local and get_local don't exist anymore. They are called local.get and local.set now. At higher number of iterations the C version converges on…

very depressing when for so many use cases even native performance is very very very much not fast enough

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#14
post #8

I compared against native: #define ITERATIONS 1000 int main() { const size_t BUFFER_SIZE = 64ul \* 1024 \* 1024; __m128i\* data_buffer = (__m128i *)memalign(64, BUFFER_SIZE); const __m128i all_ones = _mm_set1_epi8(0xFF); for (size_t i = 0; i I had to fixup the WAT because set_local and get_local don't exist anymore. They are called local.get and local.set now. At higher number of iterations the C version converges on…

Thanks for mentioning, I just updated my website and Github to use the new WAT functions.

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#15
post #8

I compared against native: #define ITERATIONS 1000 int main() { const size_t BUFFER_SIZE = 64ul \* 1024 \* 1024; __m128i\* data_buffer = (__m128i *)memalign(64, BUFFER_SIZE); const __m128i all_ones = _mm_set1_epi8(0xFF); for (size_t i = 0; i I had to fixup the WAT because set_local and get_local don't exist anymore. They are called local.get and local.set now. At higher number of iterations the C version converges on…

very depressing when for so many use cases even native performance is very very very much not fast enough

> when

If native performance is "very very very" not fast enough then that's supercomputer work and it doesn't really matter if WASM is 3x native or 0.3x native. So that context should be where you're the least depressed.

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#16
post #8

I compared against native: #define ITERATIONS 1000 int main() { const size_t BUFFER_SIZE = 64ul \* 1024 \* 1024; __m128i\* data_buffer = (__m128i *)memalign(64, BUFFER_SIZE); const __m128i all_ones = _mm_set1_epi8(0xFF); for (size_t i = 0; i I had to fixup the WAT because set_local and get_local don't exist anymore. They are called local.get and local.set now. At higher number of iterations the C version converges on…

very depressing when for so many use cases even native performance is very very very much not fast enough

In many of these use cases native is only "not fast enough" because the code you're running makes very poor use of the cache, pipelining, simd instruction sets, and memory bandwidth

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#17
To compile code to WebAssembly with SIMD instructions, just use the generic+simd128 CPU target:

    zig cc  -Ofast --target=wasm32-wasi -mcpu=generic+simd128 example.c
    zig c++ -Ofast --target=wasm32-wasi -mcpu=generic+simd128 example.cpp
Or with a build file:

    zig build -Drelease-fast -Dtarget=wasm32-wasi -Dcpu=generic+simd128

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#18
post #8

I compared against native: #define ITERATIONS 1000 int main() { const size_t BUFFER_SIZE = 64ul \* 1024 \* 1024; __m128i\* data_buffer = (__m128i *)memalign(64, BUFFER_SIZE); const __m128i all_ones = _mm_set1_epi8(0xFF); for (size_t i = 0; i I had to fixup the WAT because set_local and get_local don't exist anymore. They are called local.get and local.set now. At higher number of iterations the C version converges on…

Hi, I'm Syrus from Wasmer.

Have you tried with the llvm backend? I believe the results might be even better there!

  $ time ./wasmer run --llvm fill_buffer.wasm -i fillBufferWithSIMD 1000

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#19
post #2

It looks promising! But fixed-width lanes don't seem too cross-platform? I don't just mean the v256 and v512 types that may become ubiquitous in a few years, but also things like optimizing for different L1 cache sizes, doing some operation macro-fusion on the SIMD unit, or directly supporting leading/trailing elements to reduce code size?

In practice it's not possible to optimize "generally" for all possible target architectures your wasm will run on. You're going to optimize for x86-64 or ARM, and probably going to specifically optimize for modern intel, modern amd, or apple's m1. If you try to optimize for everything you're going to run into really painful tradeoffs and probably have mediocre performance on a bunch of architectures after a lot of ha…

Wouldn't it be possible to have a binary containing multiple versions of your program compiled optimized for various CPU configuration and have a switch at runtime which would select depending on your CPUid. I think intel have a compiler for that.

Re: Exploring SIMD performance improvements in WebAssembly (2021)

#20

Earlier quoted context omitted.

very depressing when for so many use cases even native performance is very very very much not fast enough

> when If native performance is "very very very" not fast enough then that's supercomputer work and it doesn't really matter if WASM is 3x native or 0.3x native. So that context should be where you're the least depressed.

> then that's supercomputer work

today's laptop work is late 90's supercomputer's work (and it was even more depressing back then).

Post reply on HN