-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Stop manually SIMDing in swap_nonoverlapping
#94212
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// compile-flags: -O | ||
// only-x86_64 | ||
// ignore-debug: the debug assertions get in the way | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::mem::swap; | ||
use std::ptr::{read, copy_nonoverlapping, write}; | ||
|
||
type KeccakBuffer = [[u64; 5]; 5]; | ||
|
||
// A basic read+copy+write swap implementation ends up copying one of the values | ||
// to stack for large types, which is completely unnecessary as the lack of | ||
// overlap means we can just do whatever fits in registers at a time. | ||
|
||
// CHECK-LABEL: @swap_basic | ||
#[no_mangle] | ||
pub fn swap_basic(x: &mut KeccakBuffer, y: &mut KeccakBuffer) { | ||
// CHECK: alloca [5 x [5 x i64]] | ||
|
||
// SAFETY: exclusive references are always valid to read/write, | ||
// are non-overlapping, and nothing here panics so it's drop-safe. | ||
unsafe { | ||
let z = read(x); | ||
copy_nonoverlapping(y, x, 1); | ||
write(y, z); | ||
} | ||
} | ||
|
||
// This test verifies that the library does something smarter, and thus | ||
// doesn't need any scratch space on the stack. | ||
|
||
// CHECK-LABEL: @swap_std | ||
#[no_mangle] | ||
pub fn swap_std(x: &mut KeccakBuffer, y: &mut KeccakBuffer) { | ||
// CHECK-NOT: alloca | ||
// CHECK: load <{{[0-9]+}} x i64> | ||
// CHECK: store <{{[0-9]+}} x i64> | ||
swap(x, y) | ||
} | ||
|
||
// CHECK-LABEL: @swap_slice | ||
#[no_mangle] | ||
pub fn swap_slice(x: &mut [KeccakBuffer], y: &mut [KeccakBuffer]) { | ||
// CHECK-NOT: alloca | ||
// CHECK: load <{{[0-9]+}} x i64> | ||
// CHECK: store <{{[0-9]+}} x i64> | ||
if x.len() == y.len() { | ||
x.swap_with_slice(y); | ||
} | ||
} | ||
|
||
type OneKilobyteBuffer = [u8; 1024]; | ||
|
||
// CHECK-LABEL: @swap_1kb_slices | ||
#[no_mangle] | ||
pub fn swap_1kb_slices(x: &mut [OneKilobyteBuffer], y: &mut [OneKilobyteBuffer]) { | ||
// CHECK-NOT: alloca | ||
// CHECK: load <{{[0-9]+}} x i8> | ||
// CHECK: store <{{[0-9]+}} x i8> | ||
if x.len() == y.len() { | ||
x.swap_with_slice(y); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// compile-flags: -O -C target-feature=+avx | ||
// only-x86_64 | ||
// ignore-debug: the debug assertions get in the way | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::mem::swap; | ||
|
||
// SIMD types are highly-aligned already, so make sure the swap code leaves their | ||
// types alone and doesn't pessimize them (such as by swapping them as `usize`s). | ||
extern crate core; | ||
use core::arch::x86_64::__m256; | ||
|
||
// CHECK-LABEL: @swap_single_m256 | ||
#[no_mangle] | ||
pub fn swap_single_m256(x: &mut __m256, y: &mut __m256) { | ||
// CHECK-NOT: alloca | ||
// CHECK: load <8 x float>{{.+}}align 32 | ||
// CHECK: store <8 x float>{{.+}}align 32 | ||
swap(x, y) | ||
} | ||
|
||
// CHECK-LABEL: @swap_m256_slice | ||
#[no_mangle] | ||
pub fn swap_m256_slice(x: &mut [__m256], y: &mut [__m256]) { | ||
// CHECK-NOT: alloca | ||
// CHECK: load <8 x float>{{.+}}align 32 | ||
// CHECK: store <8 x float>{{.+}}align 32 | ||
if x.len() == y.len() { | ||
x.swap_with_slice(y); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff for this file is pretty useless; you might want to read it in side-by-side instead: https://github.com/rust-lang/rust/pull/94212/files?diff=split&w=0