Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/60044>.
//@ assembly-output: emit-asm
//@ only-x86_64

// We want to check that the None case is optimized away
//@ compile-flags: -O

// Simplify the generated assembly
//@ compile-flags: -Cforce-unwind-tables=no

#![crate_type = "lib"]

use std::num::NonZeroUsize;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::Relaxed;

pub static X: AtomicUsize = AtomicUsize::new(1);

/// This function function shall look like this:
/// ```
/// some_non_zero_from_atomic_get:
/// movq _RNvCs7C4TuIcXqwO_25some_non_zero_from_atomic1X@GOTPCREL(%rip), %rax
/// movq (%rax), %rax
/// retq
/// ```
// CHECK-LABEL: some_non_zero_from_atomic_get:
// CHECK-NEXT: movq {{[_a-zA-Z0-9]+}}@GOTPCREL(%rip), %rax
// CHECK-NEXT: movq (%rax), %rax
// CHECK-NEXT: retq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incompatible with platforms that need LVI mitigations. At the moment I think that's only the x86_64-fortanix-unknown-sgx target. Could you add //@ ignore-sgx on top to ignore this platform?

#[no_mangle]
pub unsafe fn some_non_zero_from_atomic_get() -> Option<NonZeroUsize> {
let x = X.load(Relaxed);
Some(NonZeroUsize::new_unchecked(x))
}

/// This function shall be identical to the above, which mean that for LLVM 21
/// we should see
/// ```
/// some_non_zero_from_atomic_get2 = some_non_zero_from_atomic_get
/// ```
/// and for LLVM 20 we should see
/// ```
/// .set some_non_zero_from_atomic_get2, some_non_zero_from_atomic_get
/// ```
/// so use a regex to match either one.
// 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}}
#[no_mangle]
pub unsafe fn some_non_zero_from_atomic_get2() -> usize {
match some_non_zero_from_atomic_get() {
Some(x) => x.get(),
None => unreachable!(), // shall be optimized out
}
}
Loading