Auto merge of #122917 - saethlin:atomicptr-to-int, r=nikic
Add the missing inttoptr when we ptrtoint in ptr atomics Ralf noticed this here: https://github.com/rust-lang/rust/pull/122220#discussion_r1535172094 Our previous codegen forgot to add the cast back to integer type. The code compiles anyway, because of course all locals are in-memory to start with, so previous codegen would do the integer atomic, store the integer to a local, then load a pointer from that local. Which is definitely _not_ what we wanted: That's an integer-to-pointer transmute, so all pointers returned by these `AtomicPtr` methods didn't have provenance. Yikes. Here's the IR for `AtomicPtr::fetch_byte_add` on 1.76: https://godbolt.org/z/8qTEjeraY ```llvm define noundef ptr `@atomicptr_fetch_byte_add(ptr` noundef nonnull align 8 %a, i64 noundef %v) unnamed_addr #0 !dbg !7 { start: %0 = alloca ptr, align 8, !dbg !12 %val = inttoptr i64 %v to ptr, !dbg !12 call void `@llvm.lifetime.start.p0(i64` 8, ptr %0), !dbg !28 %1 = ptrtoint ptr %val to i64, !dbg !28 %2 = atomicrmw add ptr %a, i64 %1 monotonic, align 8, !dbg !28 store i64 %2, ptr %0, align 8, !dbg !28 %self = load ptr, ptr %0, align 8, !dbg !28 call void `@llvm.lifetime.end.p0(i64` 8, ptr %0), !dbg !28 ret ptr %self, !dbg !33 } ``` r? `@RalfJung` cc `@nikic`
This commit is contained in:
commit
5dcb678ad8
@ -1149,12 +1149,12 @@ fn atomic_rmw(
|
||||
order: rustc_codegen_ssa::common::AtomicOrdering,
|
||||
) -> &'ll Value {
|
||||
// The only RMW operation that LLVM supports on pointers is compare-exchange.
|
||||
if self.val_ty(src) == self.type_ptr()
|
||||
&& op != rustc_codegen_ssa::common::AtomicRmwBinOp::AtomicXchg
|
||||
{
|
||||
let requires_cast_to_int = self.val_ty(src) == self.type_ptr()
|
||||
&& op != rustc_codegen_ssa::common::AtomicRmwBinOp::AtomicXchg;
|
||||
if requires_cast_to_int {
|
||||
src = self.ptrtoint(src, self.type_isize());
|
||||
}
|
||||
unsafe {
|
||||
let mut res = unsafe {
|
||||
llvm::LLVMBuildAtomicRMW(
|
||||
self.llbuilder,
|
||||
AtomicRmwBinOp::from_generic(op),
|
||||
@ -1163,7 +1163,11 @@ fn atomic_rmw(
|
||||
AtomicOrdering::from_generic(order),
|
||||
llvm::False, // SingleThreaded
|
||||
)
|
||||
};
|
||||
if requires_cast_to_int {
|
||||
res = self.inttoptr(res, self.type_ptr());
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn atomic_fence(
|
||||
|
38
tests/codegen/atomicptr.rs
Normal file
38
tests/codegen/atomicptr.rs
Normal file
@ -0,0 +1,38 @@
|
||||
// LLVM does not support some atomic RMW operations on pointers, so inside codegen we lower those
|
||||
// to integer atomics, surrounded by casts to and from integer type.
|
||||
// This test ensures that we do the round-trip correctly for AtomicPtr::fetch_byte_add, and also
|
||||
// ensures that we do not have such a round-trip for AtomicPtr::swap, because LLVM supports pointer
|
||||
// arguments to `atomicrmw xchg`.
|
||||
|
||||
//@ compile-flags: -O -Cno-prepopulate-passes
|
||||
#![crate_type = "lib"]
|
||||
|
||||
#![feature(strict_provenance)]
|
||||
#![feature(strict_provenance_atomic_ptr)]
|
||||
|
||||
use std::sync::atomic::AtomicPtr;
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
use std::ptr::without_provenance_mut;
|
||||
|
||||
// Portability hack so that we can say [[USIZE]] instead of i64/i32/i16 for usize.
|
||||
// CHECK: @helper([[USIZE:i[0-9]+]] noundef %_1)
|
||||
#[no_mangle]
|
||||
pub fn helper(_: usize) {}
|
||||
|
||||
// CHECK-LABEL: @atomicptr_fetch_byte_add
|
||||
#[no_mangle]
|
||||
pub fn atomicptr_fetch_byte_add(a: &AtomicPtr<u8>, v: usize) -> *mut u8 {
|
||||
// CHECK: %[[INTPTR:.*]] = ptrtoint ptr %{{.*}} to [[USIZE]]
|
||||
// CHECK-NEXT: %[[RET:.*]] = atomicrmw add ptr %{{.*}}, [[USIZE]] %[[INTPTR]]
|
||||
// CHECK-NEXT: inttoptr [[USIZE]] %[[RET]] to ptr
|
||||
a.fetch_byte_add(v, Relaxed)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @atomicptr_swap
|
||||
#[no_mangle]
|
||||
pub fn atomicptr_swap(a: &AtomicPtr<u8>, ptr: *mut u8) -> *mut u8 {
|
||||
// CHECK-NOT: ptrtoint
|
||||
// CHECK: atomicrmw xchg ptr %{{.*}}, ptr %{{.*}} monotonic
|
||||
// CHECK-NOT: inttoptr
|
||||
a.swap(ptr, Relaxed)
|
||||
}
|
Loading…
Reference in New Issue
Block a user