diff --git a/src/libcore/hint.rs b/src/libcore/hint.rs index b2f82ef0d175c..e73a1a2879347 100644 --- a/src/libcore/hint.rs +++ b/src/libcore/hint.rs @@ -91,3 +91,25 @@ pub fn spin_loop() { } } } + +/// A function that is opaque to the optimizer, to allow benchmarks to +/// pretend to use outputs to assist in avoiding dead-code +/// elimination. +/// +/// This function is a no-op, and does not even read from `dummy`. +#[unstable(feature = "test", issue = "27812")] +pub fn black_box(dummy: T) -> T { + #[cfg(not(target_arch = "asmjs"))] { + // we need to "use" the argument in some way LLVM can't + // introspect. + unsafe { asm!("" : : "r"(&dummy)) } + dummy + } + #[cfg(target_arch = "asmjs")] { + unsafe { + let ret = crate::ptr::read_volatile(&dummy); + crate::mem::forget(dummy); + ret + } + } +} diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index cb0ce480e4273..5c91c0ec43b19 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -27,23 +27,7 @@ pub use libtest::{ TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk, stats::Summary }; -/// A function that is opaque to the optimizer, to allow benchmarks to -/// pretend to use outputs to assist in avoiding dead-code -/// elimination. -/// -/// This function is a no-op, and does not even read from `dummy`. -#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))] -pub fn black_box(dummy: T) -> T { - // we need to "use" the argument in some way LLVM can't - // introspect. - unsafe { asm!("" : : "r"(&dummy)) } - dummy -} -#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))] -#[inline(never)] -pub fn black_box(dummy: T) -> T { - dummy -} +pub use std::hint::black_box; #[cfg(test)] mod tests {