From f3ca96ebd759e380b86c3e0a7ea4ac5919345ad3 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Tue, 13 Oct 2020 15:42:23 -0700 Subject: [PATCH] core::hint: Make spin_loop_hint a side effect for all targets This allows users to work around the LLVM codegen bug in stable Rust by replacing ```rust loop {} ``` with ```rust loop { core::sync::atomic::spin_loop_hint(); } ``` Right now this workaround does the right thing on some (but not all) targets. Signed-off-by: Joe Richey --- library/core/src/hint.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index 454fb34e77e49..2ee94a22419a5 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -93,6 +93,15 @@ pub fn spin_loop() { unsafe { crate::arch::arm::__yield() }; } } + // This makes sure that spin_loop is always a side-effect on all platfomrs, + // allowing users to work around https://github.com/rust-lang/rust/issues/28728 + // Remove when that bug is fixed. + #[cfg(not(miri))] + // SAFETY: the inline assembly is a no-op. + unsafe { + // FIXME: Cannot use `asm!` because it doesn't support MIPS and other architectures. + llvm_asm!("" : : : : "volatile"); + } } /// An identity function that *__hints__* to the compiler to be maximally pessimistic about what