-
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
Wrong optimization #98568
Comments
Simplified the function a bit: pub fn buggy(arr: Vec<i32>) -> i32 {
let mut prev = 0;
let mut cnt = 0;
// The entire loop is optimized away in release: https://godbolt.org/z/j538GxzT1
for d in arr {
if d > 0 {
if prev < 0 {
cnt += 1;
} else {
cnt = 1;
}
} else {
if prev > 0 {
cnt += 1;
} else {
cnt = 1;
}
}
prev = d;
}
cnt
}
fn main() {
let v = vec![-1,1];
let ans = buggy(v);
// The right answer is 2. But when build with release, the output is 1
println!("{}", ans);
} It seems that something assumes that d = prev in the loop. However doing it explicitely by changing the condition to if d == prev doesn't trigger the bug. |
Works with |
This C version has the same issue: https://godbolt.org/z/E86n349nr, so likely a LLVM bug. @rustbot label: +A-LLVM |
Miscompile still present on current main, and appears to be introduced during this |
Upstream issue: llvm/llvm-project#56242 |
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-critical |
LLVM miscompile. The optimiser mistakenly analyses the loop as invariant. |
Upstream fix: llvm/llvm-project@e4d1d0c |
I tried this code:
I expected to see this happen: 5 is printed.
Instead, this happened: when build with release, the output is 2.
Meta
rustc --version --verbose
:I also test all the following rust version at playground, and got wrong answer when build with release.
stable: 1.61.0
beta: 1.62.0-beta.6
nightly: 1.64.0-nightly (2022-06-25 20a6f3a)
The text was updated successfully, but these errors were encountered: