2018-04-12 08:47:38 -05:00
|
|
|
#![stable(feature = "core_hint", since = "1.27.0")]
|
|
|
|
|
|
|
|
//! Hints to compiler that affects how code should be emitted or optimized.
|
2020-08-29 21:35:50 -05:00
|
|
|
//! Hints may be compile time or runtime.
|
2018-04-12 08:47:38 -05:00
|
|
|
|
2019-04-14 21:23:21 -05:00
|
|
|
use crate::intrinsics;
|
2018-04-12 08:47:38 -05:00
|
|
|
|
|
|
|
/// Informs the compiler that this point in the code is not reachable, enabling
|
|
|
|
/// further optimizations.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Reaching this function is completely *undefined behavior* (UB). In
|
|
|
|
/// particular, the compiler assumes that all UB must never happen, and
|
|
|
|
/// therefore will eliminate all branches that reach to a call to
|
|
|
|
/// `unreachable_unchecked()`.
|
|
|
|
///
|
2018-11-26 20:59:49 -06:00
|
|
|
/// Like all instances of UB, if this assumption turns out to be wrong, i.e., the
|
2018-04-12 08:47:38 -05:00
|
|
|
/// `unreachable_unchecked()` call is actually reachable among all possible
|
|
|
|
/// control flow, the compiler will apply the wrong optimization strategy, and
|
|
|
|
/// may sometimes even corrupt seemingly unrelated code, causing
|
|
|
|
/// difficult-to-debug problems.
|
|
|
|
///
|
|
|
|
/// Use this function only when you can prove that the code will never call it.
|
2019-04-05 12:42:09 -05:00
|
|
|
/// Otherwise, consider using the [`unreachable!`] macro, which does not allow
|
2019-04-04 22:05:33 -05:00
|
|
|
/// optimizations but will panic when executed.
|
2018-04-12 08:47:38 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn div_1(a: u32, b: u32) -> u32 {
|
|
|
|
/// use std::hint::unreachable_unchecked;
|
|
|
|
///
|
|
|
|
/// // `b.saturating_add(1)` is always positive (not zero),
|
2019-02-09 16:16:58 -06:00
|
|
|
/// // hence `checked_div` will never return `None`.
|
2018-04-12 08:47:38 -05:00
|
|
|
/// // Therefore, the else branch is unreachable.
|
|
|
|
/// a.checked_div(b.saturating_add(1))
|
|
|
|
/// .unwrap_or_else(|| unsafe { unreachable_unchecked() })
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(div_1(7, 0), 7);
|
|
|
|
/// assert_eq!(div_1(9, 1), 4);
|
2020-03-27 16:43:28 -05:00
|
|
|
/// assert_eq!(div_1(11, u32::MAX), 0);
|
2018-04-12 08:47:38 -05:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
#[stable(feature = "unreachable", since = "1.27.0")]
|
2020-07-17 14:57:13 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
|
|
|
|
pub const unsafe fn unreachable_unchecked() -> ! {
|
2020-06-21 17:54:46 -05:00
|
|
|
// SAFETY: the safety contract for `intrinsics::unreachable` must
|
|
|
|
// be upheld by the caller.
|
|
|
|
unsafe { intrinsics::unreachable() }
|
2018-04-12 08:47:38 -05:00
|
|
|
}
|
2018-12-19 15:43:29 -06:00
|
|
|
|
2020-08-29 21:35:50 -05:00
|
|
|
/// Emits a machine instruction to signal the processor that it is running in
|
|
|
|
/// a busy-wait spin-loop ("spin lock").
|
|
|
|
///
|
|
|
|
/// Upon receiving the spin-loop signal the processor can optimize its behavior by,
|
|
|
|
/// for example, saving power or switching hyper-threads.
|
|
|
|
///
|
|
|
|
/// This function is different from [`thread::yield_now`] which directly
|
|
|
|
/// yields to the system's scheduler, whereas `spin_loop` does not interact
|
|
|
|
/// with the operating system.
|
|
|
|
///
|
|
|
|
/// A common use case for `spin_loop` is implementing bounded optimistic
|
|
|
|
/// spinning in a CAS loop in synchronization primitives. To avoid problems
|
|
|
|
/// like priority inversion, it is strongly recommended that the spin loop is
|
|
|
|
/// terminated after a finite amount of iterations and an appropriate blocking
|
|
|
|
/// syscall is made.
|
|
|
|
///
|
|
|
|
/// **Note**: On platforms that do not support receiving spin-loop hints this
|
|
|
|
/// function does not do anything at all.
|
|
|
|
///
|
|
|
|
/// # Examples
|
2018-12-19 15:43:29 -06:00
|
|
|
///
|
2020-08-29 21:35:50 -05:00
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
/// use std::sync::Arc;
|
|
|
|
/// use std::{hint, thread};
|
|
|
|
///
|
|
|
|
/// // A shared atomic value that threads will use to coordinate
|
|
|
|
/// let live = Arc::new(AtomicBool::new(false));
|
|
|
|
///
|
|
|
|
/// // In a background thread we'll eventually set the value
|
|
|
|
/// let bg_work = {
|
|
|
|
/// let live = live.clone();
|
|
|
|
/// thread::spawn(move || {
|
|
|
|
/// // Do some work, then make the value live
|
|
|
|
/// do_some_work();
|
|
|
|
/// live.store(true, Ordering::Release);
|
|
|
|
/// })
|
|
|
|
/// };
|
2019-04-03 03:54:07 -05:00
|
|
|
///
|
2020-08-29 21:35:50 -05:00
|
|
|
/// // Back on our current thread, we wait for the value to be set
|
2021-01-03 11:54:54 -06:00
|
|
|
/// while !live.load(Ordering::Acquire) {
|
2020-08-29 21:35:50 -05:00
|
|
|
/// // The spin loop is a hint to the CPU that we're waiting, but probably
|
|
|
|
/// // not for very long
|
|
|
|
/// hint::spin_loop();
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // The value is now set
|
|
|
|
/// # fn do_some_work() {}
|
|
|
|
/// do_some_work();
|
|
|
|
/// bg_work.join()?;
|
|
|
|
/// # Ok::<(), Box<dyn core::any::Any + Send + 'static>>(())
|
|
|
|
/// ```
|
2019-04-03 03:54:07 -05:00
|
|
|
///
|
2020-08-29 21:35:50 -05:00
|
|
|
/// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html
|
2018-12-19 15:43:29 -06:00
|
|
|
#[inline]
|
2020-08-29 21:35:50 -05:00
|
|
|
#[stable(feature = "renamed_spin_loop", since = "1.49.0")]
|
2018-12-19 15:43:29 -06:00
|
|
|
pub fn spin_loop() {
|
2019-12-06 22:18:12 -06:00
|
|
|
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2"))]
|
|
|
|
{
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
{
|
2020-04-23 16:21:53 -05:00
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
|
2019-03-16 12:07:58 -05:00
|
|
|
unsafe { crate::arch::x86::_mm_pause() };
|
|
|
|
}
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
{
|
2020-04-23 16:21:53 -05:00
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
|
2019-03-16 12:07:58 -05:00
|
|
|
unsafe { crate::arch::x86_64::_mm_pause() };
|
|
|
|
}
|
2018-12-19 15:43:29 -06:00
|
|
|
}
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
#[cfg(any(target_arch = "aarch64", all(target_arch = "arm", target_feature = "v6")))]
|
|
|
|
{
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
{
|
2020-04-23 16:21:53 -05:00
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
|
2021-04-29 11:15:50 -05:00
|
|
|
unsafe { crate::arch::aarch64::__isb(crate::arch::aarch64::SY) };
|
2019-03-16 12:07:58 -05:00
|
|
|
}
|
2019-12-06 22:18:12 -06:00
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
{
|
2020-04-23 16:21:53 -05:00
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on arm targets
|
|
|
|
// with support for the v6 feature.
|
2019-03-16 12:07:58 -05:00
|
|
|
unsafe { crate::arch::arm::__yield() };
|
|
|
|
}
|
2018-12-19 15:43:29 -06:00
|
|
|
}
|
|
|
|
}
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 03:15:52 -05:00
|
|
|
|
2019-07-23 04:30:13 -05:00
|
|
|
/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
|
|
|
|
/// `black_box` could do.
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 03:15:52 -05:00
|
|
|
///
|
2019-07-23 04:30:13 -05:00
|
|
|
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
|
2020-08-26 18:16:18 -05:00
|
|
|
/// use `dummy` in any possible valid way that Rust code is allowed to without introducing undefined
|
2019-07-23 04:30:13 -05:00
|
|
|
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
|
|
|
|
/// certain optimizations are not desired, such as benchmarks.
|
|
|
|
///
|
|
|
|
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
|
|
|
|
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
|
|
|
|
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
|
2020-10-05 09:47:52 -05:00
|
|
|
///
|
2020-10-05 10:29:43 -05:00
|
|
|
/// [`std::convert::identity`]: crate::convert::identity
|
2021-08-10 05:50:33 -05:00
|
|
|
#[inline]
|
2021-04-15 04:19:24 -05:00
|
|
|
#[unstable(feature = "bench_black_box", issue = "64102")]
|
2021-09-08 12:13:47 -05:00
|
|
|
pub fn black_box<T>(dummy: T) -> T {
|
|
|
|
crate::intrinsics::black_box(dummy)
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 03:15:52 -05:00
|
|
|
}
|