I've been doing my own research into safe SIMD loads/stores for the fearless_simd crate, and found that it is possible to implement safe SIMD loads/stores without emitting any unsafe from macros for them at all; you only need a handful of safe helper functions, with one internal unsafe block per function.
The key insight is that rustc lowers the load/store intrinsics into generic load/store ops, which are also emitted by std::mem::transmute_copy and core::ptr::write_unaligned for loads and stores respectively. Then all you need is two helper functions, one for load and one for store, to wrap those ops safely using bytemuck-like Pod trait: https://github.com/linebender/fearless_simd/blob/a01002e74486e9c7856b8a5a431ee00f37bc5fec/fearless_simd/src/transmute.rs#L224-L264
Since the emitted LLVM IR is identical between this approach and dedicated intrinsics, there is no performance difference between the two. I've verified that manually for x86, and I was part of making this change for NEON: rust-lang/stdarch#2004
I found that this approach dramatically cut down on the amount of unsafe in fearless_simd. It's up to you whether you like this direction for this crate or not, I just thought I'd share.
I've been doing my own research into safe SIMD loads/stores for the
fearless_simdcrate, and found that it is possible to implement safe SIMD loads/stores without emitting anyunsafefrom macros for them at all; you only need a handful of safe helper functions, with one internalunsafeblock per function.The key insight is that rustc lowers the load/store intrinsics into generic load/store ops, which are also emitted by
std::mem::transmute_copyandcore::ptr::write_unalignedfor loads and stores respectively. Then all you need is two helper functions, one for load and one for store, to wrap those ops safely usingbytemuck-like Pod trait: https://github.com/linebender/fearless_simd/blob/a01002e74486e9c7856b8a5a431ee00f37bc5fec/fearless_simd/src/transmute.rs#L224-L264Since the emitted LLVM IR is identical between this approach and dedicated intrinsics, there is no performance difference between the two. I've verified that manually for x86, and I was part of making this change for NEON: rust-lang/stdarch#2004
I found that this approach dramatically cut down on the amount of
unsafeinfearless_simd. It's up to you whether you like this direction for this crate or not, I just thought I'd share.