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
Optimization: combine unlikely branches #53015
Labels
Comments
I don't know how the IR looks like in those rust examples, |
(I'll provide a compiler explorer link later, on mobile currently...) Rust: fn sum(a: &[i32]) -> i32 {
a[0] + a[1] + a[2]
} Unoptimized LLVM IR:
Optimized IR (rustc 1.57.0)
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This Rust code:
performs bounds checks on each array access in order. Effectively it translates to something like this:
Generated assembly:
All three "panic" cases unconditionally call an error handling function marked
cold
.This could be optimized by combining the tests into one test covering all three unlikely cases, so that there is only one check on the hot path. If we have to go into the cold code, only then differentiate between the different cold cases. Like this:
The text was updated successfully, but these errors were encountered: