[llvm] [LV][NFC] Refactor computeMaxVF() to move early validation out (PR #195292)

via llvm-commits llvm-commits at lists.llvm.org
Fri May 1 09:51:06 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: Hassnaa Hamdi (hassnaaHamdi)

<details>
<summary>Changes</summary>

Refactor precondition checks out of `computeMaxVF` into `VFSelectionContext::checkVectorizationPreconditions()`.
This keeps `computeMaxVF` focused on computing max-VF according to which epilogue-lowering we will end up with.

---

Patch is 79.39 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/195292.diff


9 Files Affected:

- (modified) llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h (+3-3) 
- (modified) llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h (-17) 
- (modified) llvm/lib/Transforms/Vectorize/CMakeLists.txt (+1) 
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+133-84) 
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.cpp (+74-13) 
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h (+7-9) 
- (added) llvm/lib/Transforms/Vectorize/LoopVectorizationUtils.cpp (+125) 
- (added) llvm/lib/Transforms/Vectorize/LoopVectorizationUtils.h (+96) 
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+100-246) 


``````````diff
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 49e8bb1e85526..25e68b775c960 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -239,7 +239,7 @@ class LoopVectorizationRequirements {
       ExactFPMathInst = I;
   }
 
-  Instruction *getExactFPInst() { return ExactFPMathInst; }
+  Instruction *getExactFPInst() const { return ExactFPMathInst; }
 
 private:
   Instruction *ExactFPMathInst = nullptr;
@@ -324,7 +324,7 @@ class LoopVectorizationLegality {
   void prepareToFoldTailByMasking();
 
   /// Returns the primary induction variable.
-  PHINode *getPrimaryInduction() { return PrimaryInduction; }
+  PHINode *getPrimaryInduction() const { return PrimaryInduction; }
 
   /// Returns the reduction variables found in the loop.
   const ReductionList &getReductionVars() const { return Reductions; }
@@ -344,7 +344,7 @@ class LoopVectorizationLegality {
   RecurrenceSet &getFixedOrderRecurrences() { return FixedOrderRecurrences; }
 
   /// Returns the widest induction type.
-  IntegerType *getWidestInductionType() { return WidestIndTy; }
+  IntegerType *getWidestInductionType() const { return WidestIndTy; }
 
   /// Returns True if given store is a final invariant store of one of the
   /// reductions found in the loop.
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
index 6eab92e66745e..2fe7d7692d64d 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
@@ -165,23 +165,6 @@ struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> {
   LLVM_ABI bool processLoop(Loop *L);
 };
 
-/// Reports a vectorization failure: print \p DebugMsg for debugging
-/// purposes along with the corresponding optimization remark \p RemarkName.
-/// If \p I is passed, it is an instruction that prevents vectorization.
-/// Otherwise, the loop \p TheLoop is used for the location of the remark.
-LLVM_ABI void reportVectorizationFailure(
-    const StringRef DebugMsg, const StringRef OREMsg, const StringRef ORETag,
-    OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr);
-
-/// Same as above, but the debug message and optimization remark are identical
-inline void reportVectorizationFailure(const StringRef DebugMsg,
-                                       const StringRef ORETag,
-                                       OptimizationRemarkEmitter *ORE,
-                                       Loop *TheLoop,
-                                       Instruction *I = nullptr) {
-  reportVectorizationFailure(DebugMsg, DebugMsg, ORETag, ORE, TheLoop, I);
-}
-
 /// A marker analysis to determine if extra passes should be run after loop
 /// vectorization.
 struct ShouldRunExtraVectorPasses
diff --git a/llvm/lib/Transforms/Vectorize/CMakeLists.txt b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
index 39d4711e11a56..1ebcd4034482b 100644
--- a/llvm/lib/Transforms/Vectorize/CMakeLists.txt
+++ b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
@@ -33,6 +33,7 @@ add_llvm_component_library(LLVMVectorize
   VPlanUnroll.cpp
   VPlanVerifier.cpp
   VPlanUtils.cpp
+  LoopVectorizationUtils.cpp
 
   ADDITIONAL_HEADER_DIRS
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index bee08eeba9927..600a10964b7e4 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -15,6 +15,7 @@
 //
 
 #include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h"
+#include "LoopVectorizationUtils.h"
 #include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Analysis/Loads.h"
 #include "llvm/Analysis/LoopInfo.h"
@@ -636,7 +637,9 @@ bool LoopVectorizationLegality::canVectorizeOuterLoop() {
     // not supported yet.
     Instruction *Term = BB->getTerminator();
     if (!isa<UncondBrInst, CondBrInst>(Term)) {
-      reportVectorizationFailure("Unsupported basic block terminator",
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
+          "Unsupported basic block terminator",
           "loop control flow is not understood by vectorizer",
           "CFGNotUnderstood", ORE, TheLoop);
       if (DoExtraAnalysis)
@@ -655,7 +658,8 @@ bool LoopVectorizationLegality::canVectorizeOuterLoop() {
     if (Br && !TheLoop->isLoopInvariant(Br->getCondition()) &&
         !LI->isLoopHeader(Br->getSuccessor(0)) &&
         !LI->isLoopHeader(Br->getSuccessor(1))) {
-      reportVectorizationFailure("Unsupported conditional branch",
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(), "Unsupported conditional branch",
           "loop control flow is not understood by vectorizer",
           "CFGNotUnderstood", ORE, TheLoop);
       if (DoExtraAnalysis)
@@ -669,9 +673,11 @@ bool LoopVectorizationLegality::canVectorizeOuterLoop() {
   // simple outer loops scenarios with uniform nested loops.
   if (!isUniformLoopNest(TheLoop /*loop nest*/,
                          TheLoop /*context outer loop*/)) {
-    reportVectorizationFailure("Outer loop contains divergent loops",
-        "loop control flow is not understood by vectorizer",
-        "CFGNotUnderstood", ORE, TheLoop);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(),
+        "Outer loop contains divergent loops",
+        "loop control flow is not understood by vectorizer", "CFGNotUnderstood",
+        ORE, TheLoop);
     if (DoExtraAnalysis)
       Result = false;
     else
@@ -680,8 +686,9 @@ bool LoopVectorizationLegality::canVectorizeOuterLoop() {
 
   // Check whether we are able to set up outer loop induction.
   if (!setupOuterLoopInductions()) {
-    reportVectorizationFailure("Unsupported outer loop Phi(s)",
-                               "UnsupportedPhi", ORE, TheLoop);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(), "Unsupported outer loop Phi(s)",
+        "UnsupportedPhi", ORE, TheLoop);
     if (DoExtraAnalysis)
       Result = false;
     else
@@ -812,14 +819,16 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
 
   if (!PrimaryInduction) {
     if (Inductions.empty()) {
-      reportVectorizationFailure(
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
           "Did not find one integer induction var",
           "loop induction variable could not be identified",
           "NoInductionVariable", ORE, TheLoop);
       return false;
     }
     if (!WidestIndTy) {
-      reportVectorizationFailure(
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
           "Did not find one integer induction var",
           "integer loop induction variable could not be identified",
           "NoIntegerInductionVariable", ORE, TheLoop);
@@ -846,8 +855,8 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
     // Check that this PHI type is allowed.
     if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
         !PhiTy->isPointerTy()) {
-      reportVectorizationFailure(
-          "Found a non-int non-pointer PHI",
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(), "Found a non-int non-pointer PHI",
           "loop control flow is not understood by vectorizer",
           "CFGNotUnderstood", ORE, TheLoop);
       return false;
@@ -868,8 +877,8 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
 
     // We only allow if-converted PHIs with exactly two incoming values.
     if (Phi->getNumIncomingValues() != 2) {
-      reportVectorizationFailure(
-          "Found an invalid PHI",
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(), "Found an invalid PHI",
           "loop control flow is not understood by vectorizer",
           "CFGNotUnderstood", ORE, TheLoop, Phi);
       return false;
@@ -937,11 +946,11 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
       return true;
     }
 
-    reportVectorizationFailure("Found an unidentified PHI",
-                               "value that could not be identified as "
-                               "reduction is used outside the loop",
-                               "NonReductionValueUsedOutsideLoop", ORE, TheLoop,
-                               Phi);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(), "Found an unidentified PHI",
+        "value that could not be identified as "
+        "reduction is used outside the loop",
+        "NonReductionValueUsedOutsideLoop", ORE, TheLoop, Phi);
     return false;
   } // end of PHI handling
 
@@ -966,16 +975,17 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
       // but it's hard to provide meaningful yet generic advice.
       // Also, should this be guarded by allowExtraAnalysis() and/or be part
       // of the returned info from isFunctionVectorizable()?
-      reportVectorizationFailure(
-          "Found a non-intrinsic callsite",
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(), "Found a non-intrinsic callsite",
           "library call cannot be vectorized. "
           "Try compiling with -fno-math-errno, -ffast-math, "
           "or similar flags",
           "CantVectorizeLibcall", ORE, TheLoop, CI);
     } else {
-      reportVectorizationFailure("Found a non-intrinsic callsite",
-                                 "call instruction cannot be vectorized",
-                                 "CantVectorizeLibcall", ORE, TheLoop, CI);
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(), "Found a non-intrinsic callsite",
+          "call instruction cannot be vectorized", "CantVectorizeLibcall", ORE,
+          TheLoop, CI);
     }
     return false;
   }
@@ -988,7 +998,8 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
     for (unsigned Idx = 0; Idx < CI->arg_size(); ++Idx)
       if (isVectorIntrinsicWithScalarOpAtArg(IntrinID, Idx, TTI)) {
         if (!SE->isLoopInvariant(PSE.getSCEV(CI->getOperand(Idx)), TheLoop)) {
-          reportVectorizationFailure(
+          LoopVectorizationUtils::reportVectorizationFailure(
+              Hints->vectorizeAnalysisPassName(),
               "Found unvectorizable intrinsic",
               "intrinsic instruction cannot be vectorized",
               "CantVectorizeIntrinsic", ORE, TheLoop, CI);
@@ -1021,10 +1032,10 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
       (isa<CastInst>(I) &&
        !VectorType::isValidElementType(I.getOperand(0)->getType())) ||
       isa<ExtractElementInst>(I)) {
-    reportVectorizationFailure("Found unvectorizable type",
-                               "instruction return type cannot be vectorized",
-                               "CantVectorizeInstructionReturnType", ORE,
-                               TheLoop, &I);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(), "Found unvectorizable type",
+        "instruction return type cannot be vectorized",
+        "CantVectorizeInstructionReturnType", ORE, TheLoop, &I);
     return false;
   }
 
@@ -1032,8 +1043,10 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
   if (auto *ST = dyn_cast<StoreInst>(&I)) {
     Type *T = ST->getValueOperand()->getType();
     if (!VectorType::isValidElementType(T)) {
-      reportVectorizationFailure("Store instruction cannot be vectorized",
-                                 "CantVectorizeStore", ORE, TheLoop, ST);
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
+          "Store instruction cannot be vectorized", "CantVectorizeStore", ORE,
+          TheLoop, ST);
       return false;
     }
 
@@ -1044,7 +1057,8 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
       auto *VecTy = FixedVectorType::get(T, /*NumElts=*/2);
       assert(VecTy && "did not find vectorized version of stored type");
       if (!TTI->isLegalNTStore(VecTy, ST->getAlign())) {
-        reportVectorizationFailure(
+        LoopVectorizationUtils::reportVectorizationFailure(
+            Hints->vectorizeAnalysisPassName(),
             "nontemporal store instruction cannot be vectorized",
             "CantVectorizeNontemporalStore", ORE, TheLoop, ST);
         return false;
@@ -1058,7 +1072,8 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
       auto *VecTy = FixedVectorType::get(I.getType(), /*NumElts=*/2);
       assert(VecTy && "did not find vectorized version of load type");
       if (!TTI->isLegalNTLoad(VecTy, LD->getAlign())) {
-        reportVectorizationFailure(
+        LoopVectorizationUtils::reportVectorizationFailure(
+            Hints->vectorizeAnalysisPassName(),
             "nontemporal load instruction cannot be vectorized",
             "CantVectorizeNontemporalLoad", ORE, TheLoop, LD);
         return false;
@@ -1087,8 +1102,10 @@ bool LoopVectorizationLegality::canVectorizeInstr(Instruction &I) {
       AllowedExit.insert(&I);
       return true;
     }
-    reportVectorizationFailure("Value cannot be used outside the loop",
-                               "ValueUsedOutsideLoop", ORE, TheLoop, &I);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(),
+        "Value cannot be used outside the loop", "ValueUsedOutsideLoop", ORE,
+        TheLoop, &I);
     return false;
   }
 
@@ -1234,7 +1251,8 @@ bool LoopVectorizationLegality::canVectorizeMemory() {
 
   if (!LAI->canVectorizeMemory()) {
     if (hasUncountableExitWithSideEffects()) {
-      reportVectorizationFailure(
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
           "Cannot vectorize unsafe dependencies in uncountable exit loop with "
           "side effects",
           "CantVectorizeUnsafeDependencyForEELoopWithSideEffects", ORE,
@@ -1246,11 +1264,12 @@ bool LoopVectorizationLegality::canVectorizeMemory() {
   }
 
   if (LAI->hasLoadStoreDependenceInvolvingLoopInvariantAddress()) {
-    reportVectorizationFailure("We don't allow storing to uniform addresses",
-                               "write to a loop invariant address could not "
-                               "be vectorized",
-                               "CantVectorizeStoreToLoopInvariantAddress", ORE,
-                               TheLoop);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(),
+        "We don't allow storing to uniform addresses",
+        "write to a loop invariant address could not "
+        "be vectorized",
+        "CantVectorizeStoreToLoopInvariantAddress", ORE, TheLoop);
     return false;
   }
 
@@ -1266,7 +1285,8 @@ bool LoopVectorizationLegality::canVectorizeMemory() {
         continue;
 
       if (blockNeedsPredication(SI->getParent())) {
-        reportVectorizationFailure(
+        LoopVectorizationUtils::reportVectorizationFailure(
+            Hints->vectorizeAnalysisPassName(),
             "We don't allow storing to uniform addresses",
             "write of conditional recurring variant value to a loop "
             "invariant address could not be vectorized",
@@ -1279,7 +1299,8 @@ bool LoopVectorizationLegality::canVectorizeMemory() {
       // to overcomplicate vectorization to support this case.
       if (Instruction *Ptr = dyn_cast<Instruction>(SI->getPointerOperand())) {
         if (TheLoop->contains(Ptr)) {
-          reportVectorizationFailure(
+          LoopVectorizationUtils::reportVectorizationFailure(
+              Hints->vectorizeAnalysisPassName(),
               "Invariant address is calculated inside the loop",
               "write to a loop invariant address could not "
               "be vectorized",
@@ -1322,7 +1343,8 @@ bool LoopVectorizationLegality::canVectorizeMemory() {
       bool IsOK = UnhandledStores.empty();
       // TODO: we should also validate against InvariantMemSets.
       if (!IsOK) {
-        reportVectorizationFailure(
+        LoopVectorizationUtils::reportVectorizationFailure(
+            Hints->vectorizeAnalysisPassName(),
             "We don't allow storing to uniform addresses",
             "write to a loop invariant address could not "
             "be vectorized",
@@ -1492,8 +1514,9 @@ bool LoopVectorizationLegality::blockCanBePredicated(
 
 bool LoopVectorizationLegality::canVectorizeWithIfConvert() {
   if (!EnableIfConversion) {
-    reportVectorizationFailure("If-conversion is disabled",
-                               "IfConversionDisabled", ORE, TheLoop);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(), "If-conversion is disabled",
+        "IfConversionDisabled", ORE, TheLoop);
     return false;
   }
 
@@ -1583,22 +1606,26 @@ bool LoopVectorizationLegality::canVectorizeWithIfConvert() {
     // loop.
     if (isa<SwitchInst>(BB->getTerminator())) {
       if (TheLoop->isLoopExiting(BB)) {
-        reportVectorizationFailure("Loop contains an unsupported switch",
-                                   "LoopContainsUnsupportedSwitch", ORE,
-                                   TheLoop, BB->getTerminator());
+        LoopVectorizationUtils::reportVectorizationFailure(
+            Hints->vectorizeAnalysisPassName(),
+            "Loop contains an unsupported switch",
+            "LoopContainsUnsupportedSwitch", ORE, TheLoop, BB->getTerminator());
         return false;
       }
     } else if (!isa<UncondBrInst, CondBrInst>(BB->getTerminator())) {
-      reportVectorizationFailure("Loop contains an unsupported terminator",
-                                 "LoopContainsUnsupportedTerminator", ORE,
-                                 TheLoop, BB->getTerminator());
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
+          "Loop contains an unsupported terminator",
+          "LoopContainsUnsupportedTerminator", ORE, TheLoop,
+          BB->getTerminator());
       return false;
     }
 
     // We must be able to predicate all blocks that need to be predicated.
     if (blockNeedsPredication(BB) &&
         !blockCanBePredicated(BB, SafePointers, ConditionallyExecutedOps)) {
-      reportVectorizationFailure(
+      LoopVectorizationUtils::reportVectorizationFailure(
+          Hints->vectorizeAnalysisPassName(),
           "Control flow cannot be substituted for a select", "NoCFGForSelect",
           ORE, TheLoop, BB->getTerminator());
       return false;
@@ -1628,9 +1655,11 @@ bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop *Lp,
   // We must have a loop in canonical form. Loops with indirectbr in them cannot
   // be canonicalized.
   if (!Lp->getLoopPreheader()) {
-    reportVectorizationFailure("Loop doesn't have a legal pre-header",
-        "loop control flow is not understood by vectorizer",
-        "CFGNotUnderstood", ORE, TheLoop);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(),
+        "Loop doesn't have a legal pre-header",
+        "loop control flow is not understood by vectorizer", "CFGNotUnderstood",
+        ORE, TheLoop);
     if (DoExtraAnalysis)
       Result = false;
     else
@@ -1639,9 +1668,11 @@ bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop *Lp,
 
   // We must have a single backedge.
   if (Lp->getNumBackEdges() != 1) {
-    reportVectorizationFailure("The loop must have a single backedge",
-        "loop control flow is not understood by vectorizer",
-        "CFGNotUnderstood", ORE, TheLoop);
+    LoopVectorizationUtils::reportVectorizationFailure(
+        Hints->vectorizeAnalysisPassName(),
+        "The loop must have a single backedge",
+        "loop control flow is not ...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list