Skip to content

Commit

Permalink
rename typed_swap → typed_swap_nonoverlapping
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Dec 25, 2024
1 parent 00dfa3b commit 1387672
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 24 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// If we're swapping something that's *not* an `OperandValue::Ref`,
// then we can do it directly and avoid the alloca.
// Otherwise, we'll let the fallback MIR body take care of it.
if let sym::typed_swap = name {
if let sym::typed_swap_nonoverlapping = name {
let pointee_ty = fn_args.type_at(0);
let pointee_layout = bx.layout_of(pointee_ty);
if !bx.is_backend_ref(pointee_layout)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/traits/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub trait BuilderMethods<'a, 'tcx>:
/// Avoids `alloca`s for Immediates and ScalarPairs.
///
/// FIXME: Maybe do something smarter for Ref types too?
/// For now, the `typed_swap` intrinsic just doesn't call this for those
/// For now, the `typed_swap_nonoverlapping` intrinsic just doesn't call this for those
/// cases (in non-debug), preferring the fallback body instead.
fn typed_place_swap(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
let result = self.raw_eq_intrinsic(&args[0], &args[1])?;
self.write_scalar(result, dest)?;
}
sym::typed_swap => {
sym::typed_swap_nonoverlapping => {
self.typed_swap_intrinsic(&args[0], &args[1])?;
}

Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ pub fn check_intrinsic_type(
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
}

sym::typed_swap => (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit),
sym::typed_swap_nonoverlapping => {
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
}

sym::discriminant_value => {
let assoc_items = tcx.associated_item_def_ids(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ symbols! {
type_macros,
type_name,
type_privacy_lints,
typed_swap,
typed_swap_nonoverlapping,
u128,
u128_legacy_const_max,
u128_legacy_const_min,
Expand Down
27 changes: 27 additions & 0 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3956,12 +3956,39 @@ pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
// Const-unstable because `swap_nonoverlapping` is const-unstable.
#[rustc_intrinsic_const_stable_indirect]
#[rustc_allow_const_fn_unstable(const_swap_nonoverlapping)] // this is anyway not called since CTFE implements the intrinsic
#[cfg(bootstrap)]
pub const unsafe fn typed_swap<T>(x: *mut T, y: *mut T) {
// SAFETY: The caller provided single non-overlapping items behind
// pointers, so swapping them with `count: 1` is fine.
unsafe { ptr::swap_nonoverlapping(x, y, 1) };
}

#[cfg(bootstrap)]
pub use typed_swap as typed_swap_nonoverlapping;

/// Non-overlapping *typed* swap of a single value.
///
/// The codegen backends will replace this with a better implementation when
/// `T` is a simple type that can be loaded and stored as an immediate.
///
/// The stabilized form of this intrinsic is [`crate::mem::swap`].
///
/// # Safety
///
/// `x` and `y` are readable and writable as `T`, and non-overlapping.
#[rustc_nounwind]
#[inline]
#[rustc_intrinsic]
// Const-unstable because `swap_nonoverlapping` is const-unstable.
#[rustc_intrinsic_const_stable_indirect]
#[rustc_allow_const_fn_unstable(const_swap_nonoverlapping)] // this is anyway not called since CTFE implements the intrinsic
#[cfg(not(bootstrap))]
pub const unsafe fn typed_swap_nonoverlapping<T>(x: *mut T, y: *mut T) {
// SAFETY: The caller provided single non-overlapping items behind
// pointers, so swapping them with `count: 1` is fine.
unsafe { ptr::swap_nonoverlapping(x, y, 1) };
}

/// Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
/// `cfg!(ub_checks)`, but behaves different from `cfg!` when mixing crates built with different
/// flags: if the crate has UB checks enabled or carries the `#[rustc_preserve_ub_checks]`
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ pub unsafe fn uninitialized<T>() -> T {
pub const fn swap<T>(x: &mut T, y: &mut T) {
// SAFETY: `&mut` guarantees these are typed readable and writable
// as well as non-overlapping.
unsafe { intrinsics::typed_swap(x, y) }
unsafe { intrinsics::typed_swap_nonoverlapping(x, y) }
}

/// Replaces `dest` with the default value of `T`, returning the previous `dest` value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(core_intrinsics)]
#![feature(rustc_attrs)]

use std::intrinsics::typed_swap;
use std::intrinsics::typed_swap_nonoverlapping;
use std::ptr::addr_of_mut;

fn invalid_array() {
Expand All @@ -10,7 +10,7 @@ fn invalid_array() {
unsafe {
let a = addr_of_mut!(a).cast::<[bool; 100]>();
let b = addr_of_mut!(b).cast::<[bool; 100]>();
typed_swap(a, b); //~ERROR: constructing invalid value
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean
--> tests/fail/intrinsics/typed-swap-invalid-array.rs:LL:CC
|
LL | typed_swap(a, b);
| ^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean
LL | typed_swap_nonoverlapping(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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(core_intrinsics)]
#![feature(rustc_attrs)]

use std::intrinsics::typed_swap;
use std::intrinsics::typed_swap_nonoverlapping;
use std::ptr::addr_of_mut;

fn invalid_scalar() {
Expand All @@ -10,7 +10,7 @@ fn invalid_scalar() {
unsafe {
let a = addr_of_mut!(a).cast::<bool>();
let b = addr_of_mut!(b).cast::<bool>();
typed_swap(a, b); //~ERROR: constructing invalid value
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
|
LL | typed_swap(a, b);
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean
LL | typed_swap_nonoverlapping(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
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/intrinsics/typed-swap-overlap.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![feature(core_intrinsics)]
#![feature(rustc_attrs)]

use std::intrinsics::typed_swap;
use std::intrinsics::typed_swap_nonoverlapping;
use std::ptr::addr_of_mut;

fn main() {
let mut a = [0_u8; 100];
unsafe {
let a = addr_of_mut!(a);
typed_swap(a, a); //~ERROR: called on overlapping ranges
typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges
--> tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
|
LL | typed_swap(a, a);
| ^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
LL | typed_swap_nonoverlapping(a, a);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
|
= 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
Expand Down
12 changes: 6 additions & 6 deletions tests/codegen/intrinsics/typed_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#![crate_type = "lib"]
#![feature(core_intrinsics)]

use std::intrinsics::typed_swap;
use std::intrinsics::typed_swap_nonoverlapping;

// CHECK-LABEL: @swap_unit(
#[no_mangle]
pub unsafe fn swap_unit(x: &mut (), y: &mut ()) {
// CHECK: start
// CHECK-NEXT: ret void
typed_swap(x, y)
typed_swap_nonoverlapping(x, y)
}

// CHECK-LABEL: @swap_i32(
Expand All @@ -32,7 +32,7 @@ pub unsafe fn swap_i32(x: &mut i32, y: &mut i32) {
// OPT3: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %x, ptr align 4 %y, i64 4, i1 false)
// CHECK: store i32 %[[TEMP]], ptr %y, align 4
// CHECK: ret void
typed_swap(x, y)
typed_swap_nonoverlapping(x, y)
}

// CHECK-LABEL: @swap_pair(
Expand All @@ -47,7 +47,7 @@ pub unsafe fn swap_pair(x: &mut (i32, u32), y: &mut (i32, u32)) {
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %x, ptr align 4 %y, i64 8, i1 false)
// CHECK: store i32
// CHECK: store i32
typed_swap(x, y)
typed_swap_nonoverlapping(x, y)
}

// CHECK-LABEL: @swap_str(
Expand All @@ -63,7 +63,7 @@ pub unsafe fn swap_str<'a>(x: &mut &'a str, y: &mut &'a str) {
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %x, ptr align 8 %y, i64 16, i1 false)
// CHECK: store ptr
// CHECK: store i64
typed_swap(x, y)
typed_swap_nonoverlapping(x, y)
}

// OPT0-LABEL: @swap_string(
Expand All @@ -73,5 +73,5 @@ pub unsafe fn swap_string(x: &mut String, y: &mut String) {
// OPT0: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[TEMP]], ptr align 8 %x, i64 24, i1 false)
// OPT0: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %x, ptr align 8 %y, i64 24, i1 false)
// OPT0: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %y, ptr align 8 %[[TEMP]], i64 24, i1 false)
typed_swap(x, y)
typed_swap_nonoverlapping(x, y)
}

0 comments on commit 1387672

Please sign in to comment.