[llvm] [LoopVectorize] Enable vectorization of uncountable loops with invariant bound loads via speculative hoisting and runtime versioning. (PR #195027)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 00:37:11 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 7cc5f037f..21a3b11ca 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -350,7 +350,7 @@ public:
   /// reductions found in the loop.
   bool isInvariantStoreOfReduction(StoreInst *SI);
 
-  LoadInst* tryToFindDyanmicBoundLoadCandidate(Loop *L, AAResults &AA);
+  LoadInst *tryToFindDyanmicBoundLoadCandidate(Loop *L, AAResults &AA);
 
   /// Returns True if given address is invariant and is used to store recurrent
   /// expression
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 4c1417820..894279c08 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -676,7 +676,7 @@ static bool isLoadSpeculativelyHoistable(Loop *L, LoadInst *LoadCandidate,
 
 LoadInst *
 LoopVectorizationLegality::tryToFindDyanmicBoundLoadCandidate(Loop *L,
-                                                             AAResults &AA) {
+                                                              AAResults &AA) {
   if (!L->isInnermost())
     return nullptr;
 
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 0614b1306..c8c13aef5 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -136,6 +136,7 @@
 #include "llvm/Support/NativeFormatting.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Transforms/Utils/InjectTLIMappings.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/LoopSimplify.h"
@@ -144,7 +145,6 @@
 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
 #include "llvm/Transforms/Utils/SizeOpts.h"
 #include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h"
-#include "llvm/Transforms/Utils/Cloning.h"
 #include <algorithm>
 #include <cassert>
 #include <cmath>
@@ -8168,7 +8168,6 @@ static void connectEpilogueVectorLoop(VPlan &EpiPlan, Loop *L,
 
 static bool isSpeculativeBoundVersioningProfitable() { return true; }
 
-
 /*
 
               preheader
@@ -8391,16 +8390,18 @@ bool LoopVectorizePass::processLoop(Loop *L) {
                                 &Requirements, &Hints, DB, AC,
                                 /*AllowRuntimeSCEVChecks=*/!OptForSize, AA);
   if (!LVL.canVectorize(EnableVPlanNativePath)) {
-     if (AllowInvariantBoundRetry) {
-      // There has to be a better way to do this? Trying to avoid changing processLoop Signature.
+    if (AllowInvariantBoundRetry) {
+      // There has to be a better way to do this? Trying to avoid changing
+      // processLoop Signature.
       AllowInvariantBoundRetry = false;
       const DataLayout &DL = F->getDataLayout();
       // 1) Check if the loop qualifies for loop invariant bound load.
-      // 2) Generate the versioned loop with runtime checks using the dynamic loop bound.
-      // load. Return this versioned loop to attempt vectorization
+      // 2) Generate the versioned loop with runtime checks using the dynamic
+      // loop bound. load. Return this versioned loop to attempt vectorization
       // again. 3) If the versioned loop is generated successfully, re-try
       // vectorization on the versioned loop.
-      if (LoadInst *BoundLoad = LVL.tryToFindDyanmicBoundLoadCandidate(L, *AA)) {
+      if (LoadInst *BoundLoad =
+              LVL.tryToFindDyanmicBoundLoadCandidate(L, *AA)) {
         if (isSpeculativeBoundVersioningProfitable())
           if (Loop *Cand = tryToVersionLoopForInvariantBoundLoad(
                   L, BoundLoad, DL, *DT, *LI, *AA, PSE, AC)) {

``````````

</details>


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


More information about the llvm-commits mailing list