[llvm] [LoopVectorize] Teach LoopVectorizationLegality about more early exits (PR #107004)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 04:05:04 PDT 2024


================
@@ -1070,6 +1070,50 @@ bool LoopVectorizationLegality::canVectorizeMemory() {
     return false;
   }
 
+  // For loops with uncountable early exiting blocks that are not the latch
----------------
david-arm wrote:

I don't think this is true, because you can have loops like this:

```
while.cond:                                       ; preds = %while.body, %entry
  %i.0 = phi i32 [ %start, %entry ], [ %inc, %while.body ]
  %inc = add i32 %i.0, 1
  %cmp.not = icmp eq i32 %inc, %end
  br i1 %cmp.not, label %cleanup, label %while.body

while.body:                                       ; preds = %while.cond
  %idxprom = zext i32 %inc to i64
  %arrayidx = getelementptr inbounds i32, ptr %p, i64 %idxprom
  %0 = load i32, ptr %arrayidx, align 4, !tbaa !6
  %cmp1 = icmp eq i32 %0, 7
  br i1 %cmp1, label %cleanup, label %while.cond, !llvm.loop !10

cleanup:                                          ; preds = %while.cond, %while.body
  %retval.0 = phi i32 [ 1, %while.body ], [ 0, %while.cond ]
  ret i32 %retval.0
```

We do see code like this being generated from C:

```
unsigned foo(int *p, unsigned start, unsigned end) {
  unsigned i = start;
  while (++i != end)
    if (p[i] == 7)
      return 1;
  return 0;
}
```

In this case the latch block either jumps to the beginning of the loop or takes an early exit.

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


More information about the llvm-commits mailing list