Skip to content

Commit adc83fc

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

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/60044>.
2+
3+
// We want to check that the None case is optimized away
4+
//@ compile-flags: -O
5+
6+
// Simplify the emitted code
7+
//@ compile-flags: -Cforce-unwind-tables=no
8+
9+
// Make it easier to write FileCheck directives:
10+
//@ compile-flags: -Zmerge-functions=disabled
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+
/// ```llvm
22+
/// define noundef range(i64 1, 0) i64 @some_non_zero_from_atomic_get() unnamed_addr #0 {
23+
/// start:
24+
/// %0 = load atomic i64, ptr @_ZN38some_non_zero_from_atomic_optimization1X13E monotonic, align 8
25+
/// %1 = icmp ne i64 %0, 0
26+
/// tail call void @llvm.assume(i1 %1)
27+
/// ret i64 %0
28+
/// }
29+
/// ; ...
30+
/// attributes #0 = { mustprogress nofree norecurse nounwind nonlazybind willreturn ... }
31+
/// ```
32+
// CHECK-LABEL: define noundef range(i64 1, 0) i64 @some_non_zero_from_atomic_get() unnamed_addr
33+
// CHECK-SAME: #[[#ATTRIBUTE_GROUP:]] {
34+
// CHECK: %0 = load atomic i64, ptr @{{[_a-zA-Z0-9]+}} monotonic, align 8
35+
// CHECK-NEXT: %1 = icmp ne i64 %0, 0
36+
// CHECK-NEXT: tail call void @llvm.assume(i1 %1)
37+
// CHECK-NEXT: ret i64 %0
38+
// CHECK-NEXT: }
39+
#[no_mangle]
40+
pub unsafe fn some_non_zero_from_atomic_get() -> Option<NonZeroUsize> {
41+
let x = X.load(Relaxed);
42+
Some(NonZeroUsize::new_unchecked(x))
43+
}
44+
45+
/// This function shall be identical to the above:
46+
// CHECK-LABEL: define noundef range(i64 1, 0) i64 @some_non_zero_from_atomic_get2() unnamed_addr
47+
// CHECK-SAME: #[[#ATTRIBUTE_GROUP]] {
48+
// CHECK: %0 = load atomic i64, ptr @{{[_a-zA-Z0-9]+}} monotonic, align 8
49+
// CHECK-NEXT: %1 = icmp ne i64 %0, 0
50+
// CHECK-NEXT: tail call void @llvm.assume(i1 %1)
51+
// CHECK-NEXT: ret i64 %0
52+
// CHECK-NEXT: }
53+
#[no_mangle]
54+
pub unsafe fn some_non_zero_from_atomic_get2() -> usize {
55+
match some_non_zero_from_atomic_get() {
56+
Some(x) => x.get(),
57+
None => unreachable!(), // shall be optimized out
58+
}
59+
}
60+
61+
// Finally make sure the attribute group looks reasonable:
62+
// CHECK-LABEL: attributes
63+
// CHECK: #[[#ATTRIBUTE_GROUP]] =
64+
// CHECK-SAME: mustprogress
65+
// CHECK-SAME: nofree
66+
// CHECK-SAME: norecurse
67+
// CHECK-SAME: nounwind
68+
// CHECK-SAME: willreturn

0 commit comments

Comments
 (0)