1 #![stable(feature = "core_hint", since = "1.27.0")]
3 //! Hints to compiler that affects how code should be emitted or optimized.
7 /// Informs the compiler that this point in the code is not reachable, enabling
8 /// further optimizations.
12 /// Reaching this function is completely *undefined behavior* (UB). In
13 /// particular, the compiler assumes that all UB must never happen, and
14 /// therefore will eliminate all branches that reach to a call to
15 /// `unreachable_unchecked()`.
17 /// Like all instances of UB, if this assumption turns out to be wrong, i.e., the
18 /// `unreachable_unchecked()` call is actually reachable among all possible
19 /// control flow, the compiler will apply the wrong optimization strategy, and
20 /// may sometimes even corrupt seemingly unrelated code, causing
21 /// difficult-to-debug problems.
23 /// Use this function only when you can prove that the code will never call it.
24 /// Otherwise, consider using the [`unreachable!`] macro, which does not allow
25 /// optimizations but will panic when executed.
31 /// fn div_1(a: u32, b: u32) -> u32 {
32 /// use std::hint::unreachable_unchecked;
34 /// // `b.saturating_add(1)` is always positive (not zero),
35 /// // hence `checked_div` will never return `None`.
36 /// // Therefore, the else branch is unreachable.
37 /// a.checked_div(b.saturating_add(1))
38 /// .unwrap_or_else(|| unsafe { unreachable_unchecked() })
41 /// assert_eq!(div_1(7, 0), 7);
42 /// assert_eq!(div_1(9, 1), 4);
43 /// assert_eq!(div_1(11, u32::MAX), 0);
46 #[stable(feature = "unreachable", since = "1.27.0")]
47 #[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
48 pub const unsafe fn unreachable_unchecked() -> ! {
49 // SAFETY: the safety contract for `intrinsics::unreachable` must
50 // be upheld by the caller.
51 unsafe { intrinsics::unreachable() }
54 /// Emits a machine instruction hinting to the processor that it is running in busy-wait
55 /// spin-loop ("spin lock").
57 /// For a discussion of different locking strategies and their trade-offs, see
58 /// [`core::sync::atomic::spin_loop_hint`].
60 /// **Note**: On platforms that do not support receiving spin-loop hints this function does not
61 /// do anything at all.
63 /// [`core::sync::atomic::spin_loop_hint`]: crate::sync::atomic::spin_loop_hint
65 #[unstable(feature = "renamed_spin_loop", issue = "55002")]
67 #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2"))]
69 #[cfg(target_arch = "x86")]
71 // SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
72 unsafe { crate::arch::x86::_mm_pause() };
75 #[cfg(target_arch = "x86_64")]
77 // SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
78 unsafe { crate::arch::x86_64::_mm_pause() };
82 #[cfg(any(target_arch = "aarch64", all(target_arch = "arm", target_feature = "v6")))]
84 #[cfg(target_arch = "aarch64")]
86 // SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
87 unsafe { crate::arch::aarch64::__yield() };
89 #[cfg(target_arch = "arm")]
91 // SAFETY: the `cfg` attr ensures that we only execute this on arm targets
92 // with support for the v6 feature.
93 unsafe { crate::arch::arm::__yield() };
98 /// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
99 /// `black_box` could do.
101 /// [`std::convert::identity`]: https://doc.rust-lang.org/core/convert/fn.identity.html
103 /// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
104 /// use `dummy` in any possible valid way that Rust code is allowed to without introducing undefined
105 /// behavior in the calling code. This property makes `black_box` useful for writing code in which
106 /// certain optimizations are not desired, such as benchmarks.
108 /// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
109 /// extent to which it can block optimisations may vary depending upon the platform and code-gen
110 /// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
112 #[unstable(feature = "test", issue = "50297")]
113 #[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
114 pub fn black_box<T>(dummy: T) -> T {
115 // We need to "use" the argument in some way LLVM can't introspect, and on
116 // targets that support it we can typically leverage inline assembly to do
117 // this. LLVM's interpretation of inline assembly is that it's, well, a black
118 // box. This isn't the greatest implementation since it probably deoptimizes
119 // more than we want, but it's so far good enough.
121 #[cfg(not(miri))] // This is just a hint, so it is fine to skip in Miri.
122 // SAFETY: the inline assembly is a no-op.
124 llvm_asm!("" : : "r"(&dummy));