Skip to content

Commit 6bfe4b7

Browse files
committed
Auto merge of #59336 - gnzlbg:hint_black_box, r=alexcrichton
Moves test::black_box to core::hint and fix black_box on wasm32 and asm.js 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! If that RFC ever gets merged, the API, docs, etc. of this API will need to change. This PR just move the implementation of the already existing API. 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. Backwards compatibility is also why we continue to re-export "black_box" from the "test" crate. This PR also fixes black_box on the wasm32 target, which now supports inline assembly, and uses volatile loads on the asm.js target. r? @Amanieu (cc @rust-lang/libs)
2 parents d20e000 + 0c127e8 commit 6bfe4b7

File tree

4 files changed

+122
-17
lines changed

4 files changed

+122
-17
lines changed

src/libcore/hint.rs

+36
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,39 @@ 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+
#[inline]
101+
#[unstable(feature = "test", issue = "27812")]
102+
pub fn black_box<T>(dummy: T) -> T {
103+
cfg_if! {
104+
if #[cfg(any(
105+
target_arch = "asmjs",
106+
all(
107+
target_arch = "wasm32",
108+
target_os = "emscripten"
109+
)
110+
))] {
111+
#[inline]
112+
unsafe fn black_box_impl<T>(d: T) -> T {
113+
// these targets do not support inline assembly
114+
let ret = crate::ptr::read_volatile(&d);
115+
crate::mem::forget(d);
116+
ret
117+
}
118+
} else {
119+
#[inline]
120+
unsafe fn black_box_impl<T>(d: T) -> T {
121+
// we need to "use" the argument in some way LLVM can't
122+
// introspect.
123+
asm!("" : : "r"(&d));
124+
d
125+
}
126+
}
127+
}
128+
unsafe { black_box_impl(dummy) }
129+
}

src/libcore/internal_macros.rs

+81
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,84 @@ macro_rules! impl_fn_for_zst {
119119
)+
120120
}
121121
}
122+
123+
/// A macro for defining `#[cfg]` if-else statements.
124+
///
125+
/// The macro provided by this crate, `cfg_if`, is similar to the `if/elif` C
126+
/// preprocessor macro by allowing definition of a cascade of `#[cfg]` cases,
127+
/// emitting the implementation which matches first.
128+
///
129+
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
130+
/// without having to rewrite each clause multiple times.
131+
///
132+
/// # Example
133+
///
134+
/// ```
135+
/// #[macro_use]
136+
/// extern crate cfg_if;
137+
///
138+
/// cfg_if! {
139+
/// if #[cfg(unix)] {
140+
/// fn foo() { /* unix specific functionality */ }
141+
/// } else if #[cfg(target_pointer_width = "32")] {
142+
/// fn foo() { /* non-unix, 32-bit functionality */ }
143+
/// } else {
144+
/// fn foo() { /* fallback implementation */ }
145+
/// }
146+
/// }
147+
///
148+
/// # fn main() {}
149+
/// ```
150+
macro_rules! cfg_if {
151+
// match if/else chains with a final `else`
152+
($(
153+
if #[cfg($($meta:meta),*)] { $($it:item)* }
154+
) else * else {
155+
$($it2:item)*
156+
}) => {
157+
cfg_if! {
158+
@__items
159+
() ;
160+
$( ( ($($meta),*) ($($it)*) ), )*
161+
( () ($($it2)*) ),
162+
}
163+
};
164+
165+
// match if/else chains lacking a final `else`
166+
(
167+
if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
168+
$(
169+
else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
170+
)*
171+
) => {
172+
cfg_if! {
173+
@__items
174+
() ;
175+
( ($($i_met),*) ($($i_it)*) ),
176+
$( ( ($($e_met),*) ($($e_it)*) ), )*
177+
( () () ),
178+
}
179+
};
180+
181+
// Internal and recursive macro to emit all the items
182+
//
183+
// Collects all the negated cfgs in a list at the beginning and after the
184+
// semicolon is all the remaining items
185+
(@__items ($($not:meta,)*) ; ) => {};
186+
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
187+
// Emit all items within one block, applying an approprate #[cfg]. The
188+
// #[cfg] will require all `$m` matchers specified and must also negate
189+
// all previous matchers.
190+
cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
191+
192+
// Recurse to emit all other items in `$rest`, and when we do so add all
193+
// our `$m` matchers to the list of `$not` matchers as future emissions
194+
// will have to negate everything we just matched as well.
195+
cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
196+
};
197+
198+
// Internal macro to Apply a cfg attribute to a list of items
199+
(@__apply $m:meta, $($it:item)*) => {
200+
$(#[$m] $it)*
201+
};
202+
}

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 {

src/tools/tidy/src/pal.rs

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const EXCEPTION_PATHS: &[&str] = &[
4242
"src/libpanic_abort",
4343
"src/libpanic_unwind",
4444
"src/libunwind",
45+
// black_box implementation is LLVM-version specific and it uses
46+
// target_os to tell targets with different LLVM-versions appart
47+
// (e.g. `wasm32-unknown-emscripten` vs `wasm32-unknown-unknown`):
48+
"src/libcore/hint.rs",
4549
"src/libstd/sys/", // Platform-specific code for std lives here.
4650
// This has the trailing slash so that sys_common is not excepted.
4751
"src/libstd/os", // Platform-specific public interfaces

0 commit comments

Comments
 (0)