Towards fearless SIMD, 7 years later Raph Levien, March 29, 2025 Seven years ago I wrote a blog post Towards fearless SIMD, outlining a vision for Rust as a compelling language for writing fast SIMD programs. Where are we now? Unfortunately, the present-day experience of writing SIMD in Rust is still pretty rough, though there has been progress, and there are promising efforts underway. As in the previous post, this post will outline a possible vision. Up to now, Linebender projects have not used SIMD, but that is changing. As we work on CPU/GPU hybrid rendering techniques, it's clear that we need SIMD to get maximal performance of the CPU side. We also see opportunities in faster color conversion and accelerated 2D geometry primitives. This blog post is also a companion to a podcast I recorded recently with André Popovitch. That podcast is a good introduction to SIMD concepts, while this blog post focuses more on future directions. A simple example As a running example, we'll compute a sigmoid function for a vector of 4 values. The scalar version is as follows: fn sigmoid(x: [f32; 4]) -> [f32; 4] { x.map(|y| y / (1.0 + y * y).sqrt()) } I don't see any reason why this shouldn't autovectorize, but according to Godbolt it's poorly optimized scalar code. Safety One of the biggest problems with writing SIMD in Rust is that all exposed SIMD intrinsics are marked as unsafe, even in cases where they can be used safely. The reason is that support for SIMD features varies widely, and executing a SIMD instruction on a CPU that does not support it is undefined behavior – the chip can crash, ignore the instruction, or do something unexpected. To be used safely, there must be some other mechanism to establish that the CPU does support the feature. Here's the running example in hand-written intrinsic code, showing the need to write unsafe to access SIMD intrinsics at all: #[cfg(target_arch = "aarch64")] fn sigmoid_neon(x: [f32; 4]) -> [f32; 4] { use core::arch::aarch64::*; unsafe...
First seen: 2025-03-30 00:31
Last seen: 2025-03-30 16:33