Loop vectorizer miscompile resulting in additional loop iteration
| Bugzilla Link | 32317 |
| Resolution | DUPLICATE |
| Resolved on | Apr 11, 2017 08:35 |
| Version | trunk |
| OS | All |
| Attachments | LLVM IR, C CODE |
| Reporter | LLVM Bugzilla Contributor |
| CC | @annamthomas,@hfinkel,@sanjoy |
Extended Description
Attached test case produces incorrect result when run -force-vector-width=2:
$ lli test.ll 89.000000
$ opt -S -O3 -force-vector-width=2 test.ll > test.opt.ll $ lli test.opt.ll 90.000000
$ opt -S -O3 -force-vector-width=4 test.ll > test.opt.ll $ lli test.opt.ll 89.000000
Looking at the output I believe that vectorizer incorrectly calculates resulting loop value for the fFld:
vector.body:
%index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
%vec.ind = phi <2 x i64> [ <i64 4, i64 5>, %vector.ph ], [ %vec.ind.next, %vector.body ]
%vector.recur = phi <2 x i64> [ %vector.recur.init, %vector.ph ], [ %20, %vector.body ]
%20 = add nsw <2 x i64> %vec.ind, %broadcast.splat
%21 = shufflevector <2 x i64> %vector.recur, <2 x i64> %20, <2 x i32> <i32 1, i32 2>
%index.next = add i64 %index, 2
%vec.ind.next = add <2 x i64> %vec.ind, <i64 2, i64 2>
%22 = icmp eq i64 %index.next, 86
br i1 %22, label %middle.block, label %vector.body, !llvm.loop !52
middle.block: ; preds = %vector.body %cmp.n = icmp eq i64 86, 86 %vector.recur.extract = extractelement <2 x i64> %20, i32 1 br i1 %cmp.n, label %bci_64, label %scalar.ph
If in the "%vector.recur.extract" instead of the "%20" we would have used "%vec.ind" - result would have been correct.
Attached .ll test case and roughly equivalent c code.
mentioned in issue llvm/llvm-bugzilla-archive#32396
Btw, the loop is no longer vectorized at O3. At O2, we see the same problem as what occurs in 32396. The vector.recur.extract generated by fixFirstOrderRecurrence assumes the last index (1) when trip-count=2 contains the correct element. This is incorrect. The fix in 32396 fixes this as well.
*** This bug has been marked as a duplicate of bug llvm/llvm-bugzilla-archive#32396 ***
Sounds similar to llvm/llvm-project#31483 , possibly a dup.