forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#122582 - scottmcm:swap-intrinsic-v2, r=<try>
Let codegen decide when to `mem::swap` with immediates Making `libcore` decide this is silly; the backend has so much better information about when it's a good idea. Thus this PR introduces a new `typed_swap` intrinsic with a fallback body, and replaces that fallback implementation when swapping immediates or scalar pairs. r? oli-obk Replaces rust-lang#111744, and means we'll never need more libs PRs like rust-lang#111803 or rust-lang#107140
- Loading branch information
Showing
15 changed files
with
298 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
22 changes: 22 additions & 0 deletions
22
src/tools/miri/tests/fail/intrinsics/typed-swap-invalid-array.rs
This file contains 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,22 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
use std::ptr::addr_of_mut; | ||
|
||
#[rustc_intrinsic] | ||
const unsafe fn typed_swap<T, const WHATEVER: usize>(_: *mut T, _: *mut T) { | ||
unreachable!() | ||
} | ||
|
||
fn invalid_array() { | ||
let mut a = [1_u8; 100]; | ||
let mut b = [2_u8; 100]; | ||
unsafe { | ||
let a = &mut *addr_of_mut!(a).cast::<[bool; 100]>(); | ||
let b = &mut *addr_of_mut!(b).cast::<[bool; 100]>(); | ||
typed_swap::<_, 0>(a, b); //~ERROR: constructing invalid value | ||
} | ||
} | ||
|
||
fn main() { | ||
invalid_array(); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/intrinsics/typed-swap-invalid-array.stderr
This file contains 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,20 @@ | ||
error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean | ||
--> $DIR/typed-swap-invalid-array.rs:LL:CC | ||
| | ||
LL | typed_swap::<_, 0>(a, b); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `invalid_array` at $DIR/typed-swap-invalid-array.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/typed-swap-invalid-array.rs:LL:CC | ||
| | ||
LL | invalid_array(); | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
22 changes: 22 additions & 0 deletions
22
src/tools/miri/tests/fail/intrinsics/typed-swap-invalid-scalar.rs
This file contains 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,22 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
use std::ptr::addr_of_mut; | ||
|
||
#[rustc_intrinsic] | ||
const unsafe fn typed_swap<T, const WHATEVER: usize>(_: *mut T, _: *mut T) { | ||
unreachable!() | ||
} | ||
|
||
fn invalid_scalar() { | ||
let mut a = 1_u8; | ||
let mut b = 2_u8; | ||
unsafe { | ||
let a = &mut *addr_of_mut!(a).cast::<bool>(); | ||
let b = &mut *addr_of_mut!(b).cast::<bool>(); | ||
typed_swap::<_, 0>(a, b); //~ERROR: constructing invalid value | ||
} | ||
} | ||
|
||
fn main() { | ||
invalid_scalar(); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/intrinsics/typed-swap-invalid-scalar.stderr
This file contains 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,20 @@ | ||
error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean | ||
--> $DIR/typed-swap-invalid-scalar.rs:LL:CC | ||
| | ||
LL | typed_swap::<_, 0>(a, b); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `invalid_scalar` at $DIR/typed-swap-invalid-scalar.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/typed-swap-invalid-scalar.rs:LL:CC | ||
| | ||
LL | invalid_scalar(); | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains 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,50 @@ | ||
//@ only-x86_64 | ||
//@ assembly-output: emit-asm | ||
//@ compile-flags: --crate-type=lib -O | ||
|
||
use std::arch::x86_64::__m128; | ||
use std::mem::swap; | ||
|
||
// CHECK-LABEL: swap_i32: | ||
#[no_mangle] | ||
pub fn swap_i32(x: &mut i32, y: &mut i32) { | ||
// CHECK: movl (%[[ARG1:.+]]), %[[T1:.+]] | ||
// CHECK: movl (%[[ARG2:.+]]), %[[T2:.+]] | ||
// CHECK: movl %[[T2]], (%[[ARG1]]) | ||
// CHECK: movl %[[T1]], (%[[ARG2]]) | ||
// CHECK: retq | ||
swap(x, y) | ||
} | ||
|
||
// CHECK-LABEL: swap_pair: | ||
#[no_mangle] | ||
pub fn swap_pair(x: &mut (i32, u32), y: &mut (i32, u32)) { | ||
// CHECK: movq (%[[ARG1]]), %[[T1:.+]] | ||
// CHECK: movq (%[[ARG2]]), %[[T2:.+]] | ||
// CHECK: movq %[[T2]], (%[[ARG1]]) | ||
// CHECK: movq %[[T1]], (%[[ARG2]]) | ||
// CHECK: retq | ||
swap(x, y) | ||
} | ||
|
||
// CHECK-LABEL: swap_str: | ||
#[no_mangle] | ||
pub fn swap_str<'a>(x: &mut &'a str, y: &mut &'a str) { | ||
// CHECK: movups (%[[ARG1]]), %[[T1:xmm.]] | ||
// CHECK: movups (%[[ARG2]]), %[[T2:xmm.]] | ||
// CHECK: movups %[[T2]], (%[[ARG1]]) | ||
// CHECK: movups %[[T1]], (%[[ARG2]]) | ||
// CHECK: retq | ||
swap(x, y) | ||
} | ||
|
||
// CHECK-LABEL: swap_simd: | ||
#[no_mangle] | ||
pub fn swap_simd(x: &mut __m128, y: &mut __m128) { | ||
// CHECK: movaps (%[[ARG1]]), %[[T1:xmm.]] | ||
// CHECK: movaps (%[[ARG2]]), %[[T2:xmm.]] | ||
// CHECK: movaps %[[T2]], (%[[ARG1]]) | ||
// CHECK: movaps %[[T1]], (%[[ARG2]]) | ||
// CHECK: retq | ||
swap(x, y) | ||
} |
Oops, something went wrong.