[llvm] [LV] Add support for speculative loads in loops that may fault (PR #151300)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 30 06:05:40 PDT 2025


================
@@ -1769,15 +1770,61 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
   assert(LatchBB->getUniquePredecessor() == SingleUncountableEdge->first &&
          "Expected latch predecessor to be the early exiting block");
 
-  // TODO: Handle loops that may fault.
   Predicates.clear();
-  if (!isDereferenceableReadOnlyLoop(TheLoop, PSE.getSE(), DT, AC,
-                                     &Predicates)) {
-    reportVectorizationFailure(
-        "Loop may fault",
-        "Cannot vectorize potentially faulting early exit loop",
-        "PotentiallyFaultingEarlyExitLoop", ORE, TheLoop);
-    return false;
+  SmallVector<LoadInst *, 4> NonDerefLoads;
+  unsigned NumLoad = 0;
+  for (BasicBlock *BB : TheLoop->blocks()) {
+    for (Instruction &I : *BB) {
+      if (auto *LI = dyn_cast<LoadInst>(&I)) {
+        NumLoad++;
+        if (!isDereferenceableAndAlignedInLoop(LI, TheLoop, *PSE.getSE(), *DT,
+                                               AC, &Predicates))
+          NonDerefLoads.push_back(LI);
+      } else if (I.mayReadFromMemory() || I.mayWriteToMemory() ||
+                 I.mayThrow()) {
+        reportVectorizationFailure(
+            "Loop may fault on instructions other than load",
+            "Cannot vectorize potentially faulting early exit loop",
+            "PotentiallyFaultingEarlyExitLoop", ORE, TheLoop);
+        return false;
+      }
+    }
+  }
+  // Checks if non-dereferencible loads are supported.
+  if (!NonDerefLoads.empty()) {
+    if (!TTI->supportsSpeculativeLoads()) {
----------------
alexey-bataev wrote:

Check this at first before other analysis to improve compile time.

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


More information about the llvm-commits mailing list