I have recently been working on a WebAssembly project in which I wanted to take advantage of SIMD (single instruction multiple data) instructions in WebAssembly that are now supported in modern browsers. I was using Rust for programming and due to this project discovered some peculiarities of Rust standard library that I’d like to share.
In Rust, wasm32 SIMD intrinsics are documented here. You can see in the source code of the module that the API it provides is feature-gated with wasm_target_feature
and simd128
.
Normally, you could just add the respective feature flag(+simd128
) to RUSTFLAGS
and enable these. But to my surprise, adding relevant rustflags did not make the API available to me.
I found the reason deep in the documentation for one of the intrinsics:
This intrinsic is only available when the standard library itself is compiled with the atomics target feature. This version of the standard library is not obtainable via rustup, but rather will require the standard library to be compiled from source.
So turns out the API is not availble in the standard library binary in my system since it is the default standard library that I got with rustup
.
It used to be quite a hustle to compile the standard library as you would need to essentially use a cross-compiler. But since the feature is used often, it is now in the cargo itself and you can compile the standard library by just running the follwing:
$ RUSTFLAGS='-C target-feature=+simd128 -Clinker-flavor=em'
$ cargo build --target wasm32-unknown-unknown -Z build-std=std,panic_abort