[llvm] [LV] Invalidate SCEV values in the scalar loop after loop vectorization (PR #69886)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 02:10:59 PDT 2023


dtcxzyw wrote:

> Thanks for investigating this!
> 
> Could you provide more context about the failure in the description? form the issue, it looks like the crash is `bool llvm::ScalarEvolution::isLoopEntryGuardedByCond(const Loop *, ICmpInst::Predicate, const SCEV *, const SCEV *): Assertion `isAvailableAtLoopEntry(LHS, L) && "LHS is not available at Loop Entry"`, correct?
>

IR after vectorizing the loop `%for.body<header><latch><exiting>`:
```
define void @pr66616() {
entry:
  br i1 false, label %scalar.ph, label %vector.ph

vector.ph:                                        ; preds = %entry
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %0 = load i8, ptr null, align 1
  %broadcast.splatinsert = insertelement <16 x i8> poison, i8 %0, i64 0
  %broadcast.splat = shufflevector <16 x i8> %broadcast.splatinsert, <16 x i8> poison, <16 x i32> zeroinitializer
  %1 = sext <16 x i8> %broadcast.splat to <16 x i32>
  %2 = add <16 x i32> %1, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
  %index.next = add nuw i32 %index, 16
  %3 = icmp eq i32 %index.next, 256
  br i1 %3, label %middle.block, label %vector.body, !llvm.loop !0

middle.block:                                     ; preds = %vector.body
  %4 = extractelement <16 x i32> %2, i32 15
  br i1 true, label %for.cond5.preheader, label %scalar.ph

scalar.ph:                                        ; preds = %entry, %middle.block
  %bc.resume.val = phi i8 [ 0, %middle.block ], [ 0, %entry ]
  br label %for.body

for.cond5.preheader:                              ; preds = %middle.block, %for.body
  %add3.lcssa = phi i32 [ %add3, %for.body ], [ %4, %middle.block ]
  br label %while.body.i

for.body:                                         ; preds = %for.body, %scalar.ph
  %storemerge12 = phi i8 [ %bc.resume.val, %scalar.ph ], [ %inc, %for.body ]
  %5 = load i8, ptr null, align 1
  %conv2 = sext i8 %5 to i32
  %add3 = add i32 %conv2, 1
  %inc = add i8 %storemerge12, 1
  %conv1 = zext i8 %inc to i32
  %tobool.not = icmp eq i32 %conv1, 0
  br i1 %tobool.not, label %for.cond5.preheader, label %for.body, !llvm.loop !3

while.body.i:                                     ; preds = %while.body.i, %for.cond5.preheader
  %i.addr.09.i = phi i32 [ %dec.i, %while.body.i ], [ %add3.lcssa, %for.cond5.preheader ]
  %incdec.ptr48.i = phi ptr [ %incdec.ptr.i, %while.body.i ], [ null, %for.cond5.preheader ]
  %dec.i = add i32 %i.addr.09.i, 1
  %incdec.ptr.i = getelementptr i8, ptr %incdec.ptr48.i, i64 1
  %tobool.not.i = icmp eq i32 %i.addr.09.i, 0
  br i1 %tobool.not.i, label %for.inc9.loopexit, label %while.body.i

for.inc9.loopexit:                                ; preds = %while.body.i
  ret void
}
```
Without this patch, the trip count for loop `%while.body.i<header><latch><exiting>` is:
`(-1 + (-1 * (sext i8 %5 to i32))<nsw>)<nsw>`
However, we cannot access `%5` in BB `%while.body.i` since `%for.body` doesn't dominate `%while.body.i`.
The correct trip count should be `(-1 * %add3.lcssa)<nsw>`.

> In that case, is it possible that we kept around some invalid block & loop dispositions?
`forgetValue` also cleanups block & loop dispositions.

I will update the test later.



https://github.com/llvm/llvm-project/pull/69886


More information about the llvm-commits mailing list