-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
hint: Make spin_loop_hint
a side-effect for all targets
#77924
Conversation
This allows users to work around the LLVM codegen bug in stable Rust by replacing ```rust loop {} ``` with ```rust loop { core::sync::atomic::spin_loop_hint(); } ``` Right now this workaround does the right thing on some (but not all) targets. Signed-off-by: Joe Richey <[email protected]>
(rust_highfive has picked a reviewer for you, use r? to override) |
I think I would rather we change codegen to explicitly emit an LLVM side effect on I don't think we should pessimize spin loop hint with inline asm volatile; that seems like it would potentially discourage it's use. Maybe it's not really important from llvm's perspective, though. Seems like we'd be hard pressed to tell since effects are likely invisible in most programs anyway. |
I wasn't able to get any sort of different codegen with this change. I would suspect that it probably does the exact same thing as marking the loop with
This was my original idea, but after looking into it, it seemed hard to do in practice (but I don't have a lot of experience with the codegen part of rustc, so maybe it's not that hard). |
Another thing that complicates this is that we don't want: loop {
core::sync::atomic::spin_loop_hint();
} to be a regression w.r.t. the LLVM bug compared with |
Seems like a reasonable hack to me, though I cannot comment on how much this would penalize other users of
How does this still make any sense once LLVM is fixed? If you don't actually want a spinloop but just a program running forever, |
So the lint is actually two separate issues. First, a correctness issue: this LLVM bug. The second is a style/performance issue, where you usually don't want a "hot" deadloop, you want to do something in the loop to relax/sleep/pause the thread. For The linked clippy PR explains this in greater detail. |
After reading more about Closing this. As @RalfJung mentioned, it doesn't make any sense once the LLVM bug is fixed. |
In rust-lang/rust-clippy#6161 and rust-lang/rust-clippy#6162 we were discussing changes to the
empty_loop
lint. That lint prohibits code like:for two reasons:
For
std
targets we recommend either panicking or replacing the code with:For
no_std
targets, in an ideal world, we would just recommend panicking or replacing the code with:While this would fix the performance issue (on some platforms), it wouldn't fix the LLVM codegen issue on all platforms. This change makes it so
spin_loop_hint
is always a side-effect from LLVM's perspective. This makes giving recommendations to users much easier. This change has no runtime costs (except that is disables optimizations we want disabled anyway). It also makes the behavior more consistent across targets (as spin_loop_hint is already a side-effect on x86 and (most) ARM targets).Without this change, we would have to recommend very weird,
unsafe
things (likeread_volatile
). Furthermore, those recommendations would become stale once the LLVM codegen bug was fixed. With this change, we can recommend a fix that still makes sense once LLVM is fixed.CC: @jonas-schievink @RalfJung @jyn514 @flip1995
Signed-off-by: Joe Richey [email protected]