Skip to content

Commit 2aa02ee

Browse files
committed
tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test
1 parent 83e49b7 commit 2aa02ee

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/60044>.
2+
3+
//@ assembly-output: emit-asm
4+
//@ only-x86_64
5+
6+
// We want to check that the None case is optimized away
7+
//@ compile-flags: -O
8+
9+
// Simplify the generated assembly
10+
//@ compile-flags: -Cforce-unwind-tables=no
11+
12+
#![crate_type = "lib"]
13+
14+
use std::num::NonZeroUsize;
15+
use std::sync::atomic::AtomicUsize;
16+
use std::sync::atomic::Ordering::Relaxed;
17+
18+
pub static X: AtomicUsize = AtomicUsize::new(1);
19+
20+
/// This function function shall look like this:
21+
/// ```
22+
/// some_non_zero_from_atomic_get:
23+
/// movq _RNvCs7C4TuIcXqwO_25some_non_zero_from_atomic1X@GOTPCREL(%rip), %rax
24+
/// movq (%rax), %rax
25+
/// retq
26+
/// ```
27+
// CHECK-LABEL: some_non_zero_from_atomic_get:
28+
// CHECK-NEXT: movq {{[_a-zA-Z0-9]+}}@GOTPCREL(%rip), %rax
29+
// CHECK-NEXT: movq (%rax), %rax
30+
// CHECK-NEXT: retq
31+
#[no_mangle]
32+
pub unsafe fn some_non_zero_from_atomic_get() -> Option<NonZeroUsize> {
33+
let x = X.load(Relaxed);
34+
Some(NonZeroUsize::new_unchecked(x))
35+
}
36+
37+
/// This function shall be identical to the above, which mean that for LLVM 21
38+
/// we should see
39+
/// ```
40+
/// some_non_zero_from_atomic_get2 = some_non_zero_from_atomic_get
41+
/// ```
42+
/// and for LLVM 20 we should see
43+
/// ```
44+
/// .set some_non_zero_from_atomic_get2, some_non_zero_from_atomic_get
45+
/// ```
46+
/// so use a regex to match either one.
47+
// CHECK-DAG: {{\.set some_non_zero_from_atomic_get2, some_non_zero_from_atomic_get|some_non_zero_from_atomic_get2 = some_non_zero_from_atomic_get}}
48+
#[no_mangle]
49+
pub unsafe fn some_non_zero_from_atomic_get2() -> usize {
50+
match some_non_zero_from_atomic_get() {
51+
Some(x) => x.get(),
52+
None => unreachable!(), // shall be optimized out
53+
}
54+
}

0 commit comments

Comments
 (0)