Fearless SIMD
Safer and easier SIMD
fearless_simd takes unsafe out of SIMD.
No matter what level of abstraction you're after, be it autovectorization and multiversioning, or portable SIMD, or safe access to raw
intrinsics and nothing more, fearless_simd has you covered!
Zero dependencies, from-scratch build time under 1 second, safe public APIs, and very little unsafe under the hood.
Automatic vectorization
Put the code to vectorize in an #[inline(always)] function generic over Simd.
This will generate several implementations for different SIMD levels and select the best one at runtime:
use ;
let mut values = ;
let level = new; // Detect SIMD available on the CPU. Expensive, so do it once.
dispatch!;
assert_eq!;
Portable SIMD
Use the vector types for explicit lane-wise operations while staying generic over the SIMD level:
use ;
let mut values = ;
let level = new; // Detect SIMD available on the CPU. Expensive, so do it once.
dispatch!;
assert_eq!;
You can also use fixed-size types such as [u32x8] instead of using the hardware's native SIMD width.
Explicit intrinsics
If you need access to raw intrinsics, kernel! creates a function where they can be called safely:
use ;
kernel!;
You can also mix and match intrinsics with the other approaches, using high-level code most of the time and dropping down to hardware-specific intrinsics only when necessary.
Inlining
Fearless SIMD relies heavily on Rust's inlining support to create functions which have the given target features enabled.
As a rule of thumb:
- All SIMD functions need
#[inline(always)]. - Use
dispatchwhen calling SIMD code from non-SIMD code. - Use
vectorize()when calling SIMD from SIMD if you don't want to force inlining.
The article describing the design covers why this is the case. There's also Q&A on Zulip.
Instruction set support
- x86/x86-64: v2 (SSE4.2), v3 (AVX2), Ice Lake (AVX-512, avoiding early slow implementations)
- Aarch64: Baseline NEON
- WebAssembly: 128-bit packed SIMD, relaxed SIMD
A scalar fallback is also provided for platforms, so your code still works even if SIMD is not available.
WebAssembly
WASM SIMD doesn't have feature detection, and so you need to compile two versions of your bundle for WASM, one with SIMD and one without,
then select the appropriate one for your user's browser. This can be done via the wasm-feature-detect
library.
You can compile WebAssembly with the SIMD128 feature enabled via the RUSTFLAGS environment variable
(RUSTFLAGS="-Ctarget-feature=+simd128"), or by adding the compiler flags in your Cargo
config.toml:
[]
= ["-Ctarget-feature=+simd128"]
= ["-Ctarget-feature=+simd128"]
If you want to compile both SIMD and non-SIMD versions of your WebAssembly library, your best option right now is to create a shell script
that builds it once with the RUSTFLAGS specified, and once without. Cargo currently does not allow specifying compiler flags
per-profile.
Relaxed SIMD
Fearless SIMD can make use of the relaxed SIMD WebAssembly instructions, if the requisite target feature is enabled. These instructions can return implementation-dependent results depending on what is fastest on the underlying hardware. They are only used for operations where we already give hardware-dependent results.
At the time of writing, relaxed SIMD is only supported in Chrome. To make use of it, you'll need to build two versions of your library, one
with relaxed SIMD enabled (RUSTFLAGS="-Ctarget-feature=+simd128,+relaxed-simd") and one with it disabled, and then feature-detect at
runtime.
Multiversioning on x86
x86 CPUs are not guaranteed to have any SIMD particular instruction set, so fearless_simd compiles a version
of each function generic over Simd for each instruction set, and dispatch selects the best one at runtime.
This is necessary to take advantage of SIMD, but results in an increased binary size on x86.
If binary size is a concern, the increase can be partially mitigated by setting
codegen-units=1
or lto=true in your Cargo.toml,
at the cost of longer build times.
As a last resort, you can turn off multiversioning for specific SIMD instruction sets by passing
--cfg disable_dispatch_sse4_2, --cfg disable_dispatch_avx2, or --cfg disable_dispatch_avx512 in RUSTFLAGS.
These configuration flags only control automatic multiversioning. Disabling one does not remove its token type, its
Simd implementation, or explicit [kernel] support; for example, an Avx2 token can still be used to call an
AVX2 kernel when the CPU supports it.
Note that later extensions can be beneficial even if you are only using 128-bit vectors: AVX2 and AVX-512 provide more efficient instructions for some operations, and AVX-512 also more than doubles the number of vector registers of all sizes.
You can also disable certain instruction sets for select functions without disabling them globally.
Feature Flags
The following crate feature flags are available:
std(enabled by default): Get floating point functions from the standard library (likely using your target's libc). Also allows usingLevel::newon all platforms, to detect which target features are enabled.libm: Use floating point implementations from libm. Useful for#[no_std].force_support_fallback: Force scalar fallback, to be supported, even if your compilation target has a better baseline.
At least one of std and libm is required; std overrides libm.
Credits
This crate was inspired by pulp, std::simd, among others in the Rust ecosystem, though makes many decisions differently.
It benefited from conversations with Luca Versari, though he is not responsible for any of the mistakes or bad decisions.
Minimum supported Rust Version (MSRV)
This version of Fearless SIMD has been verified to compile with Rust 1.89 and later.
Future versions of Fearless SIMD might increase the Rust version requirement. It will not be treated as a breaking change and as such can even happen with small patch releases.
Community
Discussion of Fearless SIMD development happens in the Linebender Zulip, specifically in #simd. All public content can be read without logging in.
Contributions are welcome by pull request. The Rust code of conduct applies.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.