Module arch
SIMD and vendor intrinsics module.
This module is intended to be the gateway to architecture-specific intrinsic functions, typically related to SIMD (but not always!). Each architecture that Rust compiles to may contain a submodule here, which means that this is not a portable module! If you're writing a portable library take care when using these APIs!
Under this module you'll find an architecture-named module, such as
x86_64. Each #[cfg(target_arch)] that Rust can compile to may have a
module entry here, only present on that particular target. For example the
i686-pc-windows-msvc target will have an x86 module here, whereas
x86_64-pc-windows-msvc has x86_64.
Overview
This module exposes vendor-specific intrinsics that typically correspond to a single machine instruction. These intrinsics are not portable: their availability is architecture-dependent, and not all machines of that architecture might provide the intrinsic.
The arch module is intended to be a low-level implementation detail for
higher-level APIs. Using it correctly can be quite tricky as you need to
ensure at least a few guarantees are upheld:
- The correct architecture's module is used. For example the
armmodule isn't available on thex86_64-unknown-linux-gnutarget. This is typically done by ensuring that#[cfg]is used appropriately when using this module. - The CPU the program is currently running on supports the function being called. For example, it is unsafe to call an AVX2 function on a CPU that doesn't actually support AVX2.
As a result of the latter of these guarantees all intrinsics in this module
are unsafe to call unless the caller enables the required target features,
and extra care needs to be taken when calling them!
CPU Feature Detection
In order to call these APIs in a safe fashion there's a number of
mechanisms available to ensure that the correct CPU feature is available
to call an intrinsic. Let's consider, for example, the _mm256_add_epi64
intrinsics on the x86 and x86_64 architectures. This function requires
the AVX2 feature as documented by Intel so to correctly call
this function we need to (a) guarantee we only call it on x86/x86_64
and (b) ensure that the CPU feature is available
Static CPU Feature Detection
The first option available to us is to conditionally compile code via the
#[cfg] attribute. CPU features correspond to the target_feature cfg
available, and can be used like so:
#[cfg(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "avx2"
)
)]
fn foo() {
#[cfg(target_arch = "x86")]
use std::arch::x86::_mm256_add_epi64;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::_mm256_add_epi64;
unsafe {
_mm256_add_epi64(...);
}
}
Here we're using #[cfg(target_feature = "avx2")] to conditionally compile
this function into our module. This means that if the avx2 feature is
enabled statically then we'll use the _mm256_add_epi64 function at
runtime. The unsafe block here can be justified through the usage of
#[cfg] to only compile the code in situations where the safety guarantees
are upheld.
Statically enabling a feature is typically done with the -C target-feature or -C target-cpu flags to the compiler. For example if
your local CPU supports AVX2 then you can compile the above function with:
Or otherwise you can specifically enable just the AVX2 feature:
Note that when you compile a binary with a particular feature enabled it's important to ensure that you only run the binary on systems which satisfy the required feature set.
Dynamic CPU Feature Detection
Sometimes statically dispatching isn't quite what you want. Instead you might want to build a portable binary that runs across a variety of CPUs, but at runtime it selects the most optimized implementation available. This allows you to build a "least common denominator" binary which has certain sections more optimized for different CPUs.
Taking our previous example from before, we're going to compile our binary without AVX2 support, but we'd like to enable it for just one function. We can do that in a manner like:
fn foo() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if is_x86_feature_detected!("avx2") {
return unsafe { foo_avx2() };
}
}
// fallback implementation without using AVX2
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn foo_avx2() {
#[cfg(target_arch = "x86")]
use std::arch::x86::_mm256_add_epi64;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::_mm256_add_epi64;
unsafe { _mm256_add_epi64(...); }
}
There's a couple of components in play here, so let's go through them in detail!
-
First up we notice the
is_x86_feature_detected!macro. Provided by the standard library, this macro will perform necessary runtime detection to determine whether the CPU the program is running on supports the specified feature. In this case the macro will expand to a boolean expression evaluating to whether the local CPU has the AVX2 feature or not.Note that this macro, like the
archmodule, is platform-specific. For example callingis_x86_feature_detected!("avx2")on ARM will be a compile time error. To ensure we don't hit this error a statement level#[cfg]is used to only compile usage of the macro onx86/x86_64. -
Next up we see our AVX2-enabled function,
foo_avx2. This function is decorated with the#[target_feature]attribute which enables a CPU feature for just this one function. Using a compiler flag like-C target-feature=+avx2will enable AVX2 for the entire program, but using an attribute will only enable it for the one function. Usage of the#[target_feature]attribute currently requires the function to also beunsafe, as we see here. This is because the function can only be correctly called on systems which have the AVX2 (like the intrinsics themselves).
And with all that we should have a working program! This program will run across all machines and it'll use the optimized AVX2 implementation on machines where support is detected.
Differences from pure hardware behavior
While the intrinsics in this module exist to expose particular instructions of the underlying hardware, not all of them behave exactly like their corresponding instructions.
- Floating-point operations are subject to the usual Rust semantics for NaN values.
- Operations that read or write the floating-point status register generally cannot be meaningfully used because Rust makes no guarantee about when and where floating-point operations can be executed. In particular, it is generally undefined behavior to change the default rounding or exception behavior.
- Some operations have special memory-model effects that are incompatible with the Rust Abstract Machine, which means they are subject to more strict requirements than they would be in an assembly program. Most notably, this applies to non-temporal ("streaming") stores on x86.
Ergonomics
It's important to note that using the arch module is not the easiest
thing in the world, so if you're curious to try it out you may want to
brace yourself for some wordiness!
The primary purpose of this module is to enable stable crates on crates.io to build up much more ergonomic abstractions which end up using SIMD under the hood. Over time these abstractions may also move into the standard library itself, but for now this module is tasked with providing the bare minimum necessary to use vendor intrinsics on stable Rust.
Other architectures
This documentation is only for one particular architecture, you can find others at:
x86x86_64armaarch64amdgpuhexagonriscv32riscv64mipsmips64powerpcpowerpc64nvptxwasm32loongarch32loongarch64s390x
Examples
First let's take a look at not actually using any intrinsics but instead using LLVM's auto-vectorization to produce optimized vectorized code for AVX2 and also for the default platform.
unsafe
Next up let's take a look at an example of manually using intrinsics. Here we'll be using SSE4.1 features to implement hex encoding.
// translated from
// <https://github.com/Matherunner/bin2hex-sse/blob/master/base16_sse4.cpp>
unsafe
Modules
-
aarch64
Platform-specific intrinsics for the
aarch64platform. -
amdgpu
Platform-specific intrinsics for the
amdgpuplatform. -
arm
Platform-specific intrinsics for the
armplatform. -
hexagon
Platform-specific intrinsics for the
hexagonplatform. -
loongarch32
Platform-specific intrinsics for the
loongarch32platform. -
loongarch64
Platform-specific intrinsics for the
loongarch64platform. -
mips
Platform-specific intrinsics for the
mipsplatform. -
mips64
Platform-specific intrinsics for the
mips64platform. -
nvptx
Platform-specific intrinsics for the
NVPTXplatform. -
powerpc
Platform-specific intrinsics for the
PowerPCplatform. -
powerpc64
Platform-specific intrinsics for the
PowerPC64platform. -
riscv32
Platform-specific intrinsics for the
riscv32platform. -
riscv64
Platform-specific intrinsics for the
riscv64platform. -
s390x
Platform-specific intrinsics for the
s390xplatform. -
wasm
Platform-specific intrinsics for the
wasmtarget family. -
wasm32
Platform-specific intrinsics for the
wasm32platform. -
wasm64
Platform-specific intrinsics for the
wasm64platform. -
x86
Platform-specific intrinsics for the
x86platform. -
x86_64
Platform-specific intrinsics for the
x86_64platform.