Skip to content

Commit 6b794f6

Browse files
committed
Add the missing inttoptr when we ptrtoint in ptr atomics
1 parent b3df0d7 commit 6b794f6

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1136,12 +1136,12 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11361136
order: rustc_codegen_ssa::common::AtomicOrdering,
11371137
) -> &'ll Value {
11381138
// The only RMW operation that LLVM supports on pointers is compare-exchange.
1139-
if self.val_ty(src) == self.type_ptr()
1140-
&& op != rustc_codegen_ssa::common::AtomicRmwBinOp::AtomicXchg
1141-
{
1139+
let requires_cast_to_int = self.val_ty(src) == self.type_ptr()
1140+
&& op != rustc_codegen_ssa::common::AtomicRmwBinOp::AtomicXchg;
1141+
if requires_cast_to_int {
11421142
src = self.ptrtoint(src, self.type_isize());
11431143
}
1144-
unsafe {
1144+
let mut res = unsafe {
11451145
llvm::LLVMBuildAtomicRMW(
11461146
self.llbuilder,
11471147
AtomicRmwBinOp::from_generic(op),
@@ -1150,7 +1150,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11501150
AtomicOrdering::from_generic(order),
11511151
llvm::False, // SingleThreaded
11521152
)
1153+
};
1154+
if requires_cast_to_int {
1155+
res = self.inttoptr(res, self.type_ptr());
11531156
}
1157+
res
11541158
}
11551159

11561160
fn atomic_fence(

tests/codegen/atomicptr.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// LLVM does not support some atomic RMW operations on pointers, so inside codegen we lower those
2+
// to integer atomics, surrounded by casts to and from integer type.
3+
// This test ensures that we do the round-trip correctly for AtomicPtr::fetch_byte_add, and also
4+
// ensures that we do not have such a round-trip for AtomicPtr::swap, because LLVM supports pointer
5+
// arguments to `atomicrmw xchg`.
6+
7+
//@ compile-flags: -O -Cno-prepopulate-passes
8+
#![crate_type = "lib"]
9+
10+
#![feature(strict_provenance)]
11+
#![feature(strict_provenance_atomic_ptr)]
12+
13+
use std::sync::atomic::AtomicPtr;
14+
use std::sync::atomic::Ordering::Relaxed;
15+
use std::ptr::without_provenance_mut;
16+
17+
// Portability hack so that we can say [[USIZE]] instead of i64/i32/i16 for usize.
18+
// CHECK: @helper([[USIZE:i[0-9]+]] noundef %_1)
19+
#[no_mangle]
20+
pub fn helper(_: usize) {}
21+
22+
// CHECK-LABEL: @atomicptr_fetch_byte_add
23+
#[no_mangle]
24+
pub fn atomicptr_fetch_byte_add(a: &AtomicPtr<u8>, v: usize) -> *mut u8 {
25+
// CHECK: %[[INTPTR:.*]] = ptrtoint ptr %{{.*}} to [[USIZE]]
26+
// CHECK-NEXT: %[[RET:.*]] = atomicrmw add ptr %{{.*}}, [[USIZE]] %[[INTPTR]]
27+
// CHECK-NEXT: inttoptr [[USIZE]] %[[RET]] to ptr
28+
a.fetch_byte_add(v, Relaxed)
29+
}
30+
31+
// CHECK-LABEL: @atomicptr_swap
32+
#[no_mangle]
33+
pub fn atomicptr_swap(a: &AtomicPtr<u8>, ptr: *mut u8) -> *mut u8 {
34+
// CHECK-NOT: ptrtoint
35+
// CHECK: atomicrmw xchg ptr %{{.*}}, ptr %{{.*}} monotonic
36+
// CHECK-NOT: inttoptr
37+
a.swap(ptr, Relaxed)
38+
}

0 commit comments

Comments
 (0)