-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Dead atomic loads aren't optimized away #50035
Comments
Discussion on irc suggests the same underlying issue might cause LLVM's SimplifyLibCalls to not optimize stuff like
(but on stable rust it works ok). |
there's a comment about constants in rust/src/librustc_trans/mir/rvalue.rs Line 49 in 5ebf748
Maybe we need to do const bitcast instead of bitcast if it's a constant? Not really sure why llvm isn't able to grok a simple bitcast. The docs say bitcasts are fine: https://llvm.org/docs/LangRef.html#constant-expressions |
There is a difference to #48627 -- in this IR, the load is dead, you wouldn't even need to constant fold it to be able to remove it. It appears this issue is specific to atomics and is independent of how the global is represented. Making the load non-atomic allows DCE to remove it in all circumstances. |
Come to think of it, it's quite difficult to justify removing an atomic load entirely (as opposed to e.g., merging two atomic loads from the same location). The mere act of doing an atomic load can make later (in program order) non-atomic loads happen-before stores in other threads that would have been data races without the atomic load. |
@rkruppe Wait, why would LLVM ever have been able to remove the load then? |
Huh, didn't realize it was optimized out previously. Going back to older opt versions on godbolt, As for why that is valid at all, I realize now it's because the static is
|
visiting for triage. Compiler team agreed that this appears to be "at most" a performance issue, if it is an issue at all (in the sense of whether this optimization can be relied upon). Thus, P-medium. |
I'm going to categorize this into "Won't fix" under the 1.26 regressions as I think it's pretty reasonable to not worry about this and it seems unlikely we'll otherwise change codegen to fix this in time. |
The linked playground compiles to
with optimizations enabled since rustc 1.60.0. |
FWIW llvm/llvm-project@aae5f81 should make this happen more reliably. We were previously only dropping such atomic loads inside specific passes, as a side-effect of other optimizations. |
https://play.rust-lang.org/?gist=7555908f8176d50b10cd06a4a5dba401&version=nightly&mode=release
Given code like:
the generated LLVM IR is:
and unfortunately our atomic load wasn't optimized away!
I believe this is a regression from stable to beta due to the way that miri defines statics (looks like LLVM doesn't recognize the bitcast) cc @oli-obk
The text was updated successfully, but these errors were encountered: