Skip to content

Commit aabe70f

Browse files
olioli-obk
oli
authored andcommitted
Directly use raw pointers in AtomicPtr store/load
1 parent 4ae328b commit aabe70f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
437437
match split[1] {
438438
"cxchg" | "cxchgweak" => {
439439
let ty = substs.type_at(0);
440-
if int_type_width_signed(ty, bx.tcx()).is_some() {
440+
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
441441
let weak = split[1] == "cxchgweak";
442442
let pair = bx.atomic_cmpxchg(
443443
args[0].immediate(),
@@ -464,7 +464,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
464464

465465
"load" => {
466466
let ty = substs.type_at(0);
467-
if int_type_width_signed(ty, bx.tcx()).is_some() {
467+
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
468468
let size = bx.layout_of(ty).size;
469469
bx.atomic_load(args[0].immediate(), order, size)
470470
} else {
@@ -474,7 +474,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
474474

475475
"store" => {
476476
let ty = substs.type_at(0);
477-
if int_type_width_signed(ty, bx.tcx()).is_some() {
477+
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
478478
let size = bx.layout_of(ty).size;
479479
bx.atomic_store(args[1].immediate(), args[0].immediate(), order, size);
480480
return;

library/core/src/sync/atomic.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,16 @@ impl<T> AtomicPtr<T> {
966966
#[inline]
967967
#[stable(feature = "rust1", since = "1.0.0")]
968968
pub fn load(&self, order: Ordering) -> *mut T {
969+
#[cfg(not(bootstrap))]
969970
// SAFETY: data races are prevented by atomic intrinsics.
970-
unsafe { atomic_load(self.p.get() as *mut usize, order) as *mut T }
971+
unsafe {
972+
atomic_load(self.p.get(), order)
973+
}
974+
#[cfg(bootstrap)]
975+
// SAFETY: data races are prevented by atomic intrinsics.
976+
unsafe {
977+
atomic_load(self.p.get() as *mut usize, order) as *mut T
978+
}
971979
}
972980

973981
/// Stores a value into the pointer.
@@ -994,6 +1002,12 @@ impl<T> AtomicPtr<T> {
9941002
#[inline]
9951003
#[stable(feature = "rust1", since = "1.0.0")]
9961004
pub fn store(&self, ptr: *mut T, order: Ordering) {
1005+
#[cfg(not(bootstrap))]
1006+
// SAFETY: data races are prevented by atomic intrinsics.
1007+
unsafe {
1008+
atomic_store(self.p.get(), ptr, order);
1009+
}
1010+
#[cfg(bootstrap)]
9971011
// SAFETY: data races are prevented by atomic intrinsics.
9981012
unsafe {
9991013
atomic_store(self.p.get() as *mut usize, ptr as usize, order);
@@ -1105,6 +1119,7 @@ impl<T> AtomicPtr<T> {
11051119
success: Ordering,
11061120
failure: Ordering,
11071121
) -> Result<*mut T, *mut T> {
1122+
#[cfg(bootstrap)]
11081123
// SAFETY: data races are prevented by atomic intrinsics.
11091124
unsafe {
11101125
let res = atomic_compare_exchange(
@@ -1119,6 +1134,11 @@ impl<T> AtomicPtr<T> {
11191134
Err(x) => Err(x as *mut T),
11201135
}
11211136
}
1137+
#[cfg(not(bootstrap))]
1138+
// SAFETY: data races are prevented by atomic intrinsics.
1139+
unsafe {
1140+
atomic_compare_exchange(self.p.get(), current, new, success, failure)
1141+
}
11221142
}
11231143

11241144
/// Stores a value into the pointer if the current value is the same as the `current` value.
@@ -1165,6 +1185,7 @@ impl<T> AtomicPtr<T> {
11651185
success: Ordering,
11661186
failure: Ordering,
11671187
) -> Result<*mut T, *mut T> {
1188+
#[cfg(bootstrap)]
11681189
// SAFETY: data races are prevented by atomic intrinsics.
11691190
unsafe {
11701191
let res = atomic_compare_exchange_weak(
@@ -1179,6 +1200,11 @@ impl<T> AtomicPtr<T> {
11791200
Err(x) => Err(x as *mut T),
11801201
}
11811202
}
1203+
#[cfg(not(bootstrap))]
1204+
// SAFETY: data races are prevented by atomic intrinsics.
1205+
unsafe {
1206+
atomic_compare_exchange_weak(self.p.get(), current, new, success, failure)
1207+
}
11821208
}
11831209

11841210
/// Fetches the value, and applies a function to it that returns an optional

0 commit comments

Comments
 (0)