-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement black_box
using intrinsic
#87916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,6 +335,7 @@ symbols! { | |
bitreverse, | ||
bitxor, | ||
bitxor_assign, | ||
black_box, | ||
block, | ||
bool, | ||
borrowck_graphviz_format, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,23 +152,19 @@ pub fn spin_loop() { | |
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way. | ||
/// | ||
/// [`std::convert::identity`]: crate::convert::identity | ||
#[cfg_attr(not(miri), inline)] | ||
nbdd0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[cfg_attr(miri, inline(never))] | ||
#[inline] | ||
#[unstable(feature = "bench_black_box", issue = "64102")] | ||
#[cfg_attr(miri, allow(unused_mut))] | ||
#[cfg_attr(not(bootstrap), allow(unused_mut))] | ||
pub fn black_box<T>(mut dummy: T) -> T { | ||
// We need to "use" the argument in some way LLVM can't introspect, and on | ||
// targets that support it we can typically leverage inline assembly to do | ||
// this. LLVM's interpretation of inline assembly is that it's, well, a black | ||
// box. This isn't the greatest implementation since it probably deoptimizes | ||
// more than we want, but it's so far good enough. | ||
|
||
#[cfg(not(miri))] // This is just a hint, so it is fine to skip in Miri. | ||
#[cfg(bootstrap)] | ||
// SAFETY: the inline assembly is a no-op. | ||
unsafe { | ||
// FIXME: Cannot use `asm!` because it doesn't support MIPS and other architectures. | ||
llvm_asm!("" : : "r"(&mut dummy) : "memory" : "volatile"); | ||
dummy | ||
} | ||
|
||
dummy | ||
#[cfg(not(bootstrap))] | ||
{ | ||
crate::intrinsics::black_box(dummy) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Longer-term we might even want to directly reexport the intrinsic instead of using a wrapper function... but that's blocked on the fn ptr reification PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that's also blocked by path component stability There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we already reexport So, while the lack of path component stability is a problem, it's not necessarily a blocker. Anyway, even the exported function is currently still unstable; this is a discussion to be had at stabilization time. |
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.