Auto merge of #84216 - RalfJung:black-box, r=Mark-Simulacrum

move core::hint::black_box under its own feature gate

The `black_box` function had its own RFC and is tracked separately from the `test` feature at https://github.com/rust-lang/rust/issues/64102. Let's reflect this in the feature gate.

To avoid breaking all the benchmarks, libtest's `test::black_box` is a wrapping definition, not a reexport -- this means it is still under the `test` feature gate.
This commit is contained in:
bors 2021-04-25 10:35:24 +00:00
commit 06f0adb345
12 changed files with 23 additions and 13 deletions

View File

@ -1,4 +1,5 @@
#![feature(allow_internal_unstable)] #![feature(allow_internal_unstable)]
#![feature(bench_black_box)]
#![feature(const_panic)] #![feature(const_panic)]
#![feature(extend_one)] #![feature(extend_one)]
#![feature(iter_zip)] #![feature(iter_zip)]

View File

@ -112,7 +112,7 @@ fn write_str_macro_debug(bh: &mut Bencher) {
#[bench] #[bench]
fn write_u128_max(bh: &mut Bencher) { fn write_u128_max(bh: &mut Bencher) {
bh.iter(|| { bh.iter(|| {
std::hint::black_box(format!("{}", u128::MAX)); test::black_box(format!("{}", u128::MAX));
}); });
} }
@ -120,20 +120,20 @@ fn write_u128_max(bh: &mut Bencher) {
fn write_u128_min(bh: &mut Bencher) { fn write_u128_min(bh: &mut Bencher) {
bh.iter(|| { bh.iter(|| {
let s = format!("{}", 0u128); let s = format!("{}", 0u128);
std::hint::black_box(s); test::black_box(s);
}); });
} }
#[bench] #[bench]
fn write_u64_max(bh: &mut Bencher) { fn write_u64_max(bh: &mut Bencher) {
bh.iter(|| { bh.iter(|| {
std::hint::black_box(format!("{}", u64::MAX)); test::black_box(format!("{}", u64::MAX));
}); });
} }
#[bench] #[bench]
fn write_u64_min(bh: &mut Bencher) { fn write_u64_min(bh: &mut Bencher) {
bh.iter(|| { bh.iter(|| {
std::hint::black_box(format!("{}", 0u64)); test::black_box(format!("{}", 0u64));
}); });
} }

View File

@ -154,7 +154,7 @@ pub fn spin_loop() {
/// [`std::convert::identity`]: crate::convert::identity /// [`std::convert::identity`]: crate::convert::identity
#[cfg_attr(not(miri), inline)] #[cfg_attr(not(miri), inline)]
#[cfg_attr(miri, inline(never))] #[cfg_attr(miri, inline(never))]
#[unstable(feature = "test", issue = "50297")] #[unstable(feature = "bench_black_box", issue = "64102")]
#[cfg_attr(miri, allow(unused_mut))] #[cfg_attr(miri, allow(unused_mut))]
pub fn black_box<T>(mut dummy: T) -> T { pub fn black_box<T>(mut dummy: T) -> T {
// We need to "use" the argument in some way LLVM can't introspect, and on // We need to "use" the argument in some way LLVM can't introspect, and on

View File

@ -233,6 +233,7 @@
#![feature(assert_matches)] #![feature(assert_matches)]
#![feature(associated_type_bounds)] #![feature(associated_type_bounds)]
#![feature(atomic_mut_ptr)] #![feature(atomic_mut_ptr)]
#![feature(bench_black_box)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(c_variadic)] #![feature(c_variadic)]
#![feature(cfg_accessible)] #![feature(cfg_accessible)]

View File

@ -1,6 +1,4 @@
//! Benchmarking module. //! Benchmarking module.
pub use std::hint::black_box;
use super::{ use super::{
event::CompletedTest, event::CompletedTest,
options::BenchMode, options::BenchMode,
@ -16,6 +14,15 @@ use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
/// `black_box` could do.
///
/// See [`std::hint::black_box`] for details.
#[inline(always)]
pub fn black_box<T>(dummy: T) -> T {
std::hint::black_box(dummy)
}
/// Manager of the benchmarking runs. /// Manager of the benchmarking runs.
/// ///
/// This is fed into functions marked with `#[bench]` to allow for /// This is fed into functions marked with `#[bench]` to allow for

View File

@ -24,6 +24,7 @@
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(nll)] #![feature(nll)]
#![feature(available_concurrency)] #![feature(available_concurrency)]
#![feature(bench_black_box)]
#![feature(internal_output_capture)] #![feature(internal_output_capture)]
#![feature(panic_unwind)] #![feature(panic_unwind)]
#![feature(staged_api)] #![feature(staged_api)]

View File

@ -1,6 +1,6 @@
// run-pass // run-pass
// Test a ZST enum whose dicriminant is ~0i128. This caused an ICE when casting to a i32. // Test a ZST enum whose dicriminant is ~0i128. This caused an ICE when casting to a i32.
#![feature(test)] #![feature(bench_black_box)]
use std::hint::black_box; use std::hint::black_box;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]

View File

@ -1,6 +1,6 @@
// run-pass // run-pass
#![feature(const_discriminant)] #![feature(const_discriminant)]
#![feature(test)] #![feature(bench_black_box)]
#![allow(dead_code)] #![allow(dead_code)]
use std::mem::{discriminant, Discriminant}; use std::mem::{discriminant, Discriminant};

View File

@ -7,7 +7,7 @@
// error-pattern: AddressSanitizer: stack-buffer-overflow // error-pattern: AddressSanitizer: stack-buffer-overflow
// error-pattern: 'xs' (line 15) <== Memory access at offset // error-pattern: 'xs' (line 15) <== Memory access at offset
#![feature(test)] #![feature(bench_black_box)]
use std::hint::black_box; use std::hint::black_box;

View File

@ -7,7 +7,7 @@
// run-fail // run-fail
// error-pattern: HWAddressSanitizer: tag-mismatch // error-pattern: HWAddressSanitizer: tag-mismatch
#![feature(test)] #![feature(bench_black_box)]
use std::hint::black_box; use std::hint::black_box;

View File

@ -6,7 +6,7 @@
// run-fail // run-fail
// error-pattern: LeakSanitizer: detected memory leaks // error-pattern: LeakSanitizer: detected memory leaks
#![feature(test)] #![feature(bench_black_box)]
use std::hint::black_box; use std::hint::black_box;
use std::mem; use std::mem;

View File

@ -13,7 +13,7 @@
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(start)] #![feature(start)]
#![feature(test)] #![feature(bench_black_box)]
use std::hint::black_box; use std::hint::black_box;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;