-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Missed optimization with short circuit AND #103327
Comments
@rustbot claim |
@rustbot label +T-compiler +A-codegen +I-slow |
Seems like I misunderstood what the "Build LLVM IR" button on the Rust playground does (I thought it shows the LLVM IR that rustc emits and not the resulting, final IR after optimization passes). It looks like LLVM started recognizing this pattern in version 13 (I'm assuming opt 13.0.0 on godbolt means it uses LLVM version 13): Though that still doesn't explain why this doesn't happen when compiled with rust. I'm on 1.66-nightly and that uses LLVM version 15.0.2 as far as I can tell ( |
This pattern is currently only handled by IPSCCP, which only runs very early. CVP doesn't handle it, because it currently only optimizes conditions with a constant operand. In principle, it can be extended to handle this with some extensions to LVI. The alternative is to get the IR into the necessary form prior to IPSCCP. I believe in this case the problem is that early SimplifyCFG runs before SROA, and we need SROA to simplify the conditions stored into variables first. Swapping the passes would probably fix this, but I vaguely remember there being some complication with doing that. |
The reason this doesn't work is that we currently rely on SimplifyCFG not optimizing branches into selects this early, so IPSCCP still sees proper branches. Would have to add an extra option to disables this in the early SimplifyCFG run. |
Upstream patch: https://reviews.llvm.org/D137253 |
Fixed by LLVM 16 upgrade. |
Add codegen tests for issues fixed by LLVM 16 Fixes rust-lang#75978. Fixes rust-lang#99960. Fixes rust-lang#101048. Fixes rust-lang#101082. Fixes rust-lang#101814. Fixes rust-lang#103132. Fixes rust-lang#103327.
Add codegen tests for issues fixed by LLVM 16 Fixes rust-lang#75978. Fixes rust-lang#99960. Fixes rust-lang#101048. Fixes rust-lang#101082. Fixes rust-lang#101814. Fixes rust-lang#103132. Fixes rust-lang#103327.
Consider this code:
This compiles to just:
(Godbolt)
However, changing any of the
&
to&&
breaks this optimization and a bunch of cmps are generated as well as the addition, which seems unnecessary in this case, because&
/&&
shouldn't make any difference as short circuit can't be observed.(Godbolt)
This works fine with Clang
Doesn't seem like an LLVM issue (to me?).
The LLVM IR that Rust emitsThe resulting LLVM IR:correctly gets optimized to this with
opt -O3 in -S -o out
(Godbolt)
The text was updated successfully, but these errors were encountered: