Skip to content

Implementation option without unsafe emitted by macros #51

Description

@Shnatsel

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions