[llvm] [LV] Initial support for stores in early exit loops (PR #137774)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri May 9 07:28:01 PDT 2025


================
@@ -1732,13 +1794,53 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
 
   // TODO: Handle loops that may fault.
   Predicates.clear();
-  if (!isDereferenceableReadOnlyLoop(TheLoop, PSE.getSE(), DT, AC,
-                                     &Predicates)) {
+
+  if (HasStore && EELoad.has_value()) {
+    LoadInst *LI = *EELoad;
+    if (isDereferenceableAndAlignedInLoop(LI, TheLoop, *PSE.getSE(), *DT, AC,
+                                          &Predicates)) {
+      ICFLoopSafetyInfo SafetyInfo;
+      SafetyInfo.computeLoopSafetyInfo(TheLoop);
+      // FIXME: We may have multiple levels of conditional loads, so will
+      //        need to improve on outright rejection at some point.
+      if (!SafetyInfo.isGuaranteedToExecute(*LI, DT, TheLoop)) {
----------------
david-arm wrote:

Is this even possible at the moment? Given the branch condition comes from a icmp, which comes from a load, then the only way this might not execute is if the early exiting block is itself only executed conditionally. This isn't a loop structure we currently support. For example, a loop like this:

```
for.body:
   %cmp1 = icmp ...
   br i1 %cmp1, label %test.exit, %for.inc

test.exit:
   %ld1 = load i8, ...
   %cmp2 = icmp eq i8 %ld1, ...
   br i1 %cmp2, label %early.exit, label %for.inc

for.inc:
   ... branch back to for.loop or leave loop ...
```

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


More information about the llvm-commits mailing list