| Issue |
237872
|
| Summary |
[LSR] OptimizeMax exposes poison from an unselected max operand in the loop exit condition
|
| Labels |
miscompilation,
loopoptim,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/hKvaqjja6
alive: https://alive2.llvm.org/ce/z/ZLQMY2
```llvm
target datalayout = "n32"
define i32 @f(i32 %n) {
entry:
%bound = add nsw i32 %n, 1
%wrapped = add i32 %n, 1
%positive = icmp sgt i32 %wrapped, 1
%trip = select i1 %positive, i32 %bound, i32 1
br label %loop
loop:
%iv = phi i32 [ 0, %entry ], [ %next, %loop ]
%next = add i32 %iv, 1
%continue = icmp ne i32 %next, %trip
br i1 %continue, label %loop, label %exit
exit:
ret i32 %next
}
define i32 @main() {
%result = call i32 @f(i32 2147483647)
ret i32 %result
}
```
```console
$ opt -S -passes=loop-reduce issue-repro.ll
...
define i32 @f(i32 %n) {
entry:
%bound = add nsw i32 %n, 1
%wrapped = add i32 %n, 1
br label %loop
loop: ; preds = %loop, %entry
%iv = phi i32 [ 0, %entry ], [ %next, %loop ]
%next = add i32 %iv, 1
%scmp = icmp slt i32 %next, %bound
br i1 %scmp, label %loop, label %exit
exit: ; preds = %loop
%next.lcssa = phi i32 [ %next, %loop ]
ret i32 %next.lcssa
}
...
$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=2 --tgt-unroll=2 -passes=loop-reduce issue-repro.ll
...
Transformation doesn't verify!
ERROR: Source is more defined than target
NOTE: The counterexample is unique.
Example:
i32 %n = #x7fffffff (2147483647)
Source:
i32 %bound = poison
i32 %wrapped = #x80000000 (2147483648, -2147483648)
i1 %positive = #x0 (0)
i32 %trip = #x00000001 (1)
>> Jump to %loop
i32 %iv = #x00000000 (0)
i32 %next = #x00000001 (1)
i1 %continue = #x0 (0)
>> Jump to %exit
i32 %next#phi#0 = #x00000001 (1)
Target:
i32 %bound = poison
>> Jump to %loop
i32 %iv = #x00000000 (0)
i32 %next = #x00000001 (1)
i1 %scmp = poison
UB triggered on br
...
Summary:
1 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
$ cat issue-repro-control.ll
target datalayout = "n32"
define i32 @f(i32 %n) {
entry:
%bound = add i32 %n, 1
%wrapped = add i32 %n, 1
%positive = icmp sgt i32 %wrapped, 1
%trip = select i1 %positive, i32 %bound, i32 1
br label %loop
loop:
%iv = phi i32 [ 0, %entry ], [ %next, %loop ]
%next = add i32 %iv, 1
%continue = icmp ne i32 %next, %trip
br i1 %continue, label %loop, label %exit
exit:
ret i32 %next
}
define i32 @main() {
%result = call i32 @f(i32 2147483647)
ret i32 %result
}
$ opt -S -passes=loop-reduce issue-repro-control.ll
...
define i32 @f(i32 %n) {
entry:
%bound = add i32 %n, 1
%wrapped = add i32 %n, 1
br label %loop
loop: ; preds = %loop, %entry
%iv = phi i32 [ 0, %entry ], [ %next, %loop ]
%next = add i32 %iv, 1
%scmp = icmp slt i32 %next, %bound
br i1 %scmp, label %loop, label %exit
exit: ; preds = %loop
%next.lcssa = phi i32 [ %next, %loop ]
ret i32 %next.lcssa
}
...
$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=2 --tgt-unroll=2 -passes=loop-reduce issue-repro-control.ll
...
Summary:
2 correct transformations
0 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
```
### suggested fix
In `LSRInstance::OptimizeMax` in `llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp`, require `NewRHS` to be non-poison at `Cond` (e.g. using `isGuaranteedNotToBePoison`); otherwise keep the original comparison.
SCEV equality alone is insufficient: `%bound` and `%wrapped` both have SCEV `(1 + %n)`, but only `%bound` has `nsw`.
For `%n = 2147483647`, the source selects `1` and returns `1`; the replacement `icmp slt %next, %bound` instead feeds poison to the branch.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs