Skip to content

Commit

Permalink
Don't use direct field access in Simd functions (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Apr 23, 2023
2 parents ceb2611 + f1b86ba commit f916add
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![feature(
const_ptr_read,
const_refs_to_cell,
convert_float_to_int,
decl_macro,
intra_doc_pointers,
Expand Down
53 changes: 45 additions & 8 deletions crates/core_simd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ use crate::simd::{
/// [`read`]: pointer::read
/// [`write`]: pointer::write
/// [as_simd]: slice::as_simd
//
// NOTE: Accessing the inner array directly in any way (e.g. by using the `.0` field syntax) or
// directly constructing an instance of the type (i.e. `let vector = Simd(array)`) should be
// avoided, as it will likely become illegal on `#[repr(simd)]` structs in the future. It also
// causes rustc to emit illegal LLVM IR in some cases.
#[repr(simd)]
pub struct Simd<T, const LANES: usize>([T; LANES])
where
Expand Down Expand Up @@ -135,22 +140,54 @@ where
/// assert_eq!(v.as_array(), &[0, 1, 2, 3]);
/// ```
pub const fn as_array(&self) -> &[T; LANES] {
&self.0
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
// is always valid and `Simd<T, LANES>` never has a lower alignment
// than `[T; LANES]`.
//
// NOTE: This deliberately doesn't just use `&self.0`, see the comment
// on the struct definition for details.
unsafe { &*(self as *const Self as *const [T; LANES]) }
}

/// Returns a mutable array reference containing the entire SIMD vector.
pub fn as_mut_array(&mut self) -> &mut [T; LANES] {
&mut self.0
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
// is always valid and `Simd<T, LANES>` never has a lower alignment
// than `[T; LANES]`.
//
// NOTE: This deliberately doesn't just use `&mut self.0`, see the comment
// on the struct definition for details.
unsafe { &mut *(self as *mut Self as *mut [T; LANES]) }
}

/// Converts an array to a SIMD vector.
pub const fn from_array(array: [T; LANES]) -> Self {
Self(array)
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
// is always valid. We need to use `read_unaligned` here, since
// the array may have a lower alignment than the vector.
//
// FIXME: We currently use a pointer read instead of `transmute_copy` because
// it results in better codegen with optimizations disabled, but we should
// probably just use `transmute` once that works on const generic types.
//
// NOTE: This deliberately doesn't just use `Self(array)`, see the comment
// on the struct definition for details.
unsafe { (&array as *const [T; LANES] as *const Self).read_unaligned() }
}

/// Converts a SIMD vector to an array.
pub const fn to_array(self) -> [T; LANES] {
self.0
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
// is always valid. No need to use `read_unaligned` here, since
// the vector never has a lower alignment than the array.
//
// FIXME: We currently use a pointer read instead of `transmute_copy` because
// it results in better codegen with optimizations disabled, but we should
// probably just use `transmute` once that works on const generic types.
//
// NOTE: This deliberately doesn't just use `self.0`, see the comment
// on the struct definition for details.
unsafe { (&self as *const Self as *const [T; LANES]).read() }
}

/// Converts a slice to a SIMD vector containing `slice[..LANES]`.
Expand Down Expand Up @@ -735,7 +772,7 @@ where
{
#[inline]
fn as_ref(&self) -> &[T; LANES] {
&self.0
self.as_array()
}
}

Expand All @@ -746,7 +783,7 @@ where
{
#[inline]
fn as_mut(&mut self) -> &mut [T; LANES] {
&mut self.0
self.as_mut_array()
}
}

Expand All @@ -758,7 +795,7 @@ where
{
#[inline]
fn as_ref(&self) -> &[T] {
&self.0
self.as_array()
}
}

Expand All @@ -769,7 +806,7 @@ where
{
#[inline]
fn as_mut(&mut self) -> &mut [T] {
&mut self.0
self.as_mut_array()
}
}

Expand Down

0 comments on commit f916add

Please sign in to comment.