-
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
Excessive overflow checks that can be proven unnecessary #114386
Comments
It's worrying about wrap around overflow in |
So LLVM just needs this transform: https://alive2.llvm.org/ce/z/4a6RLR declare void @llvm.assume(i1)
define i1 @src(i16 %x, i16 %c) {
%c_nonzero = icmp ne i16 %c, 0
call void @llvm.assume(i1 %c_nonzero)
%x3 = mul nuw i16 %x, %c
%cmp = icmp uge i16 %x3, %x
ret i1 %cmp
}
define i1 @tgt(i16 %x, i16 %c) {
ret i1 true
} |
@rustbot claim |
@rustbot ping |
@rustbot claim |
Proof: https://alive2.llvm.org/ce/z/T_ocLy Discovered in: rust-lang/rust#114386 This PR folds `X * C >= X` to `true` when `C` is known to be non-zero and `mul` is `nuw`. Folds for other math operators exist already: https://llvm-ir.godbolt.org/z/GKcYEf5Kb
@rustbot label llvm-fixed-upstream |
Proof: https://alive2.llvm.org/ce/z/T_ocLy Discovered in: rust-lang/rust#114386 This PR folds `X * C >= X` to `true` when `C` is known to be non-zero and `mul` is `nuw`. Folds for other math operators exist already: https://llvm-ir.godbolt.org/z/GKcYEf5Kb
Proof: https://alive2.llvm.org/ce/z/T_ocLy Discovered in: rust-lang/rust#114386 This PR folds `X * C >= X` to `true` when `C` is known to be non-zero and `mul` is `nuw`. Folds for other math operators exist already: https://llvm-ir.godbolt.org/z/GKcYEf5Kb
I tried this code:
The check
lim * 3 >= lim
is trivially true, sincelim
is obtained by dividingusize
by 3 so there can be no overflow. I expected to see it optimized out in ASM, like here (obtained via std::hint::black_box-ing true):Instead, this happened:
Meta
https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=17cc61a317c18d7ea86d7d98b6a43510
triple_lim >= triple_lim / 3
in a similar test is actually optimized away.Source
Optimized down from https://rust.godbolt.org/z/jsh6zhPYa (utility function for converting BGR to RGBA arrays).
The text was updated successfully, but these errors were encountered: