-
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
Adding an unreachable branch helps optimize the code when matching on x % N
#93514
Comments
@rustbot label +A-codegen +C-enhancement +I-slow +T-compiler |
I think there's an LLVM bug here, despite your clang example. Here's a demonstration using just |
In LLVM, when the optimization fires, the unreachable bb is replaced with That code determines whether or not the default condition is reachable based on the number of significant bits in the condition, which is why it doesn't work until you have |
I believe https://reviews.llvm.org/D106056 has already fixed this. Unfortunately, this exposed some compile-time issues in the backend, which haven't been resolved yet, so the patch is reverted for now. |
Looks like this will be handled better in LLVM 18 (after #120055): https://godbolt.org/z/reaf5oKcb |
@erikdesjardins I believe the relevant change has been reverted. |
There is a PR that adds unreachable like that: #120268 |
This comment was marked as duplicate.
This comment was marked as duplicate.
Looks like the same issue I'm having. |
It only solves enum scenarios. The enum scenario has been solved, this PR is an extension.
|
That change got unreverted and was included in the recent LLVM 19 upgrade, so I believe this is now fixed: https://godbolt.org/z/1MvffeWff @rustbot label E-needs-test |
Hmm, looks like 1.78 has fixed this: https://rust.godbolt.org/z/n4fbG4xj4. |
…r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
…r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
…r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
…, r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
…, r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
…, r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
…r=Mark-Simulacrum Add Four Codegen Tests Closes rust-lang#74615 Closes rust-lang#123216 Closes rust-lang#49572 Closes rust-lang#93514 This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
Consider this example:
It currently generates similar LLVM-IR:
Even though the default branch is unreachable (
x % 5
can't be greater than 4) it is still generated.But, when un-commenting
5 | 6 | 7 => loop{}
line (still unreachable branch, that does the same as default) the default branch becomesunreachable
:So, adding an unreachable branch that does the same as the default helps optimize the code.
Some notes:
5 | 6 | 7
is commented-out5..=7
) do not help5 | 6 | 7 | _
doesn't help eitherN
inx % N
that is not a power of two -- adding unreachable branches until the last branch has a power-of-two-minus-one-value helps optimizing the codeN = 3
(3 =>
)N = 9
(9 | 10 | 11 | 12 | 13 | 14 | 15 =>
)N = 14
(14 | 15 =>
)loop{}
s can be replaced with anything else, for exampleunreachable!()
orpanic!()
, effect is still the sameloop{}
s to make diffs less noisyif-else-if
chains are identical tomatch
herellvm-ir
, instead of generating anunreachable
oneThe text was updated successfully, but these errors were encountered: