| Issue |
234046
|
| Summary |
[LoopVectorize] A possibly-zero udiv induction step is expanded above the minimum-iteration check
|
| Labels |
miscompilation,
vectorizers,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/xxnhTqcET
alive: https://alive2.llvm.org/ce/z/cujTIz
```llvm
define i32 @f(i32 %n) {
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %inc, %body ]
%acc = phi i32 [ 0, %entry ], [ %next, %body ]
%cond = icmp ult i32 %i, %n
br i1 %cond, label %body, label %exit
body:
%q = udiv i32 1, %n
%next = add i32 %acc, %q
%inc = add i32 %i, 1
br label %loop
exit:
ret i32 %acc
}
; clean: the divisor cannot be zero
define i32 @f_clean(i32 %n) {
entry:
%d = or i32 %n, 1
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %inc, %body ]
%acc = phi i32 [ 0, %entry ], [ %next, %body ]
%cond = icmp ult i32 %i, %n
br i1 %cond, label %body, label %exit
body:
%q = udiv i32 1, %d
%next = add i32 %acc, %q
%inc = add i32 %i, 1
br label %loop
exit:
ret i32 %acc
}
```
```console
$ opt -S -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 repro.ll -o tgt.ll
$ grep -A2 -m1 '^entry:' tgt.ll
entry:
%0 = udiv i32 1, %n
%1 = add i32 %n, 1
$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=4 --tgt-unroll=4 repro.ll tgt.ll | grep -E '^(Transformation|ERROR|i32 %n =)'
Transformation doesn't verify!
ERROR: Source is more defined than target
i32 %n = #x00000000 (0)
Transformation seems to be correct!
```
### suggested fix
`createWidenInductionRecipe` gets the step from `vputils::getOrCreateVPValueForSCEVExpr`, which always places the `VPExpandSCEVRecipe` in the plan entry block, before the minimum-iteration check, so `udiv i32 1, %n` now runs even when the loop body never does.
Only expand a step there when `SCEVExpander::isSafeToExpand` holds (here the divisor is not known non-zero); otherwise expand it after the bypass checks or do not vectorize.
#219893 adds this check only for the early-exit symbolic max trip count in `isVectorizableEarlyExitLoop`; it does not cover induction steps.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs