Skip to content

Commit f5d6b3a

Browse files
committed
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.
1 parent 60eca54 commit f5d6b3a

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/libcore/hint.rs

+19
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,22 @@ pub fn spin_loop() {
9191
}
9292
}
9393
}
94+
95+
/// A function that is opaque to the optimizer, to allow benchmarks to
96+
/// pretend to use outputs to assist in avoiding dead-code
97+
/// elimination.
98+
///
99+
/// This function is a no-op, and does not even read from `dummy`.
100+
#[cfg_attr(any(target_arch = "asmjs", target_arch = "wasm32"), inline(never))]
101+
#[unstable(feature = "test", issue = "27812")]
102+
pub fn black_box<T>(dummy: T) -> T {
103+
#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))] {
104+
// we need to "use" the argument in some way LLVM can't
105+
// introspect.
106+
unsafe { asm!("" : : "r"(&dummy)) }
107+
dummy
108+
}
109+
#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))] {
110+
dummy
111+
}
112+
}

src/libtest/lib.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,7 @@ pub use libtest::{
2727
TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk, stats::Summary
2828
};
2929

30-
/// A function that is opaque to the optimizer, to allow benchmarks to
31-
/// pretend to use outputs to assist in avoiding dead-code
32-
/// elimination.
33-
///
34-
/// This function is a no-op, and does not even read from `dummy`.
35-
#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))]
36-
pub fn black_box<T>(dummy: T) -> T {
37-
// we need to "use" the argument in some way LLVM can't
38-
// introspect.
39-
unsafe { asm!("" : : "r"(&dummy)) }
40-
dummy
41-
}
42-
#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))]
43-
#[inline(never)]
44-
pub fn black_box<T>(dummy: T) -> T {
45-
dummy
46-
}
30+
pub use std::hint::black_box;
4731

4832
#[cfg(test)]
4933
mod tests {

0 commit comments

Comments
 (0)