File: atomicptr.rs

package info (click to toggle)
rustc 1.91.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 943,992 kB
  • sloc: xml: 158,148; javascript: 23,353; sh: 19,314; python: 15,711; ansic: 13,112; cpp: 7,371; asm: 4,376; makefile: 723; lisp: 176; sql: 15
file content (35 lines) | stat: -rw-r--r-- 1,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// LLVM does not support some atomic RMW operations on pointers, so inside codegen we lower those
// to integer atomics, followed by an inttoptr cast.
// 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: -Copt-level=3 -Cno-prepopulate-passes
#![crate_type = "lib"]

use std::ptr::without_provenance_mut;
use std::sync::atomic::AtomicPtr;
use std::sync::atomic::Ordering::Relaxed;

// 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: llvm.lifetime.start
    // CHECK-NEXT: %[[RET:.*]] = atomicrmw add ptr %{{.*}}, [[USIZE]] %v
    // 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)
}