-
Notifications
You must be signed in to change notification settings - Fork 88
Explain unsafe contracts of core::simd #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,47 @@ use crate::simd::{LaneCount, Mask, MaskElement, SupportedLaneCount}; | |
/// | ||
/// [`Wrapping<T>`]: core::num::Wrapping | ||
/// | ||
/// # Layout | ||
/// `Simd<T, N>` has a layout similar to `[T; N]` (identical "shapes"), but with a greater alignment. | ||
/// `[T; N]` is aligned to `T`, but `Simd<T, N>` will have an alignment based on both `T` and `N`. | ||
/// It is thus sound to [`transmute`] `Simd<T, N>` to `[T; N]`, and will typically optimize to zero cost, | ||
/// but the reverse transmutation is more likely to require a copy the compiler cannot simply elide. | ||
/// | ||
/// # ABI "Features" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might change this to "ABI Limitations" and "pass by reference" instead of "pass by memory", though both of these are correct too |
||
/// Due to Rust's safety guarantees, `Simd<T, N>` is currently passed to and from functions via memory, not SIMD registers, | ||
/// except as an optimization. `#[inline]` hints are recommended on functions that accept `Simd<T, N>` or return it. | ||
/// The need for this may be corrected in the future. | ||
/// | ||
/// # Safe SIMD with Unsafe Rust | ||
/// | ||
/// Operations with `Simd` are typically safe, but there are many reasons to want to combine SIMD with `unsafe` code. | ||
/// Care must be taken to respect differences between `Simd` and other types it may be transformed into or derived from. | ||
/// In particular, the layout of `Simd<T, N>` may be similar to `[T; N]`, and may allow some transmutations, | ||
/// but references to `[T; N]` are not interchangeable with those to `Simd<T, N>`. | ||
/// Thus, when using `unsafe` Rust to read and write `Simd<T, N>` through [raw pointers], it is a good idea to first try with | ||
/// [`read_unaligned`] and [`write_unaligned`]. This is because: | ||
/// - [`read`] and [`write`] require full alignment (in this case, `Simd<T, N>`'s alignment) | ||
/// - the likely source for reading or destination for writing `Simd<T, N>` is [`[T]`](slice) and similar types, aligned to `T` | ||
/// - combining these actions would violate the `unsafe` contract and explode the program into a puff of **undefined behavior** | ||
/// - the compiler can implicitly adjust layouts to make unaligned reads or writes fully aligned if it sees the optimization | ||
/// - most contemporary processors suffer no performance penalty for "unaligned" reads and writes that are aligned at runtime | ||
/// | ||
/// By imposing less obligations, unaligned functions are less likely to make the program unsound, | ||
/// and may be just as fast as stricter alternatives. | ||
/// When trying to guarantee alignment, [`[T]::as_simd`][as_simd] is an option for converting `[T]` to `[Simd<T, N>]`, | ||
/// and allows soundly operating on an aligned SIMD body, but it may cost more time when handling the scalar head and tail. | ||
/// If these are not sufficient, then it is most ideal to design data structures to be already aligned | ||
/// to the `Simd<T, N>` you wish to use before using `unsafe` Rust to read or write. | ||
/// More conventional ways to compensate for these facts, like materializing `Simd` to or from an array first, | ||
/// are handled by safe methods like [`Simd::from_array`] and [`Simd::from_slice`]. | ||
/// | ||
/// [`transmute`]: core::mem::transmute | ||
/// [raw pointers]: pointer | ||
/// [`read_unaligned`]: pointer::read_unaligned | ||
/// [`write_unaligned`]: pointer::write_unaligned | ||
/// [`read`]: pointer::read | ||
/// [`write`]: pointer::write | ||
/// [as_simd]: slice::as_simd | ||
#[repr(simd)] | ||
pub struct Simd<T, const LANES: usize>([T; LANES]) | ||
where | ||
|
@@ -133,6 +174,7 @@ where | |
#[inline] | ||
#[cfg(not(bootstrap))] | ||
pub fn cast<U: SimdElement>(self) -> Simd<U, LANES> { | ||
// Safety: The input argument is a vector of a known SIMD type. | ||
unsafe { intrinsics::simd_as(self) } | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.