[llvm] [VPlan] Split out EVL exit cond transform from canonicalizeEVLLoops. NFC (PR #178181)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 29 09:22:48 PST 2026
https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/178181
>From 753bdde9baf44512fb2040d7d40b7dd7ae4a653e Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Tue, 27 Jan 2026 19:50:40 +0800
Subject: [PATCH 1/2] [VPlan] Split out EVL exit cond transform from
canonicalizeEVLLoops. NFC
This is split out from #177114.
In order to make canonicalizeEVLLoops a generic "convert to variable stepping" transform, move the code that changes the exit condition to a separate transform. Run it before canonicalizeEVLLoops before VPEVLBasedIVPHIRecipe is expanded.
Also relax the assertion for VPInstruction::ExplicitVectorLength to just bail instead, since eventually VPEVLBasedIVPHIRecipe will be used by other loops that aren't EVL tail folded.
---
.../Transforms/Vectorize/LoopVectorize.cpp | 2 +
.../Transforms/Vectorize/VPlanTransforms.cpp | 67 +++++++++++--------
.../Transforms/Vectorize/VPlanTransforms.h | 10 +--
3 files changed, 48 insertions(+), 31 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 3cf5e5fb3f818..6f0c59bc8f0ea 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7430,6 +7430,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
// Expand BranchOnTwoConds after dissolution, when latch has direct access to
// its successors.
VPlanTransforms::expandBranchOnTwoConds(BestVPlan);
+ // Switch the exit condition to AVLNext == 0 for EVL tail folded loops.
+ VPlanTransforms::convertEVLExitCond(BestVPlan);
// Canonicalize EVL loops after regions are dissolved.
VPlanTransforms::canonicalizeEVLLoops(BestVPlan);
VPlanTransforms::materializeBackedgeTakenCount(BestVPlan, VectorPH);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index d1d5870d78a03..52dd74baef6ec 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -3247,9 +3247,9 @@ void VPlanTransforms::addExplicitVectorLength(
Plan.setUF(1);
}
-void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
- // Find EVL loop entries by locating VPEVLBasedIVPHIRecipe.
- // There should be only one EVL PHI in the entire plan.
+/// Find EVL loop entries by locating VPEVLBasedIVPHIRecipe.
+/// There should be only one EVL PHI in the entire plan.
+static VPEVLBasedIVPHIRecipe *findEVLPhi(VPlan &Plan) {
VPEVLBasedIVPHIRecipe *EVLPhi = nullptr;
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
@@ -3260,28 +3260,17 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
EVLPhi = PhiR;
}
+ return EVLPhi;
+}
+
+void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
+ VPEVLBasedIVPHIRecipe *EVLPhi = findEVLPhi(Plan);
// Early return if no EVL PHI is found.
if (!EVLPhi)
return;
VPBasicBlock *HeaderVPBB = EVLPhi->getParent();
VPValue *EVLIncrement = EVLPhi->getBackedgeValue();
- VPValue *AVL;
- [[maybe_unused]] bool FoundAVL =
- match(EVLIncrement,
- m_c_Add(m_ZExtOrSelf(m_EVL(m_VPValue(AVL))), m_Specific(EVLPhi)));
- assert(FoundAVL && "Didn't find AVL?");
-
- // The AVL may be capped to a safe distance.
- VPValue *SafeAVL;
- if (match(AVL, m_Select(m_VPValue(), m_VPValue(SafeAVL), m_VPValue())))
- AVL = SafeAVL;
-
- VPValue *AVLNext;
- [[maybe_unused]] bool FoundAVLNext =
- match(AVL, m_VPInstruction<Instruction::PHI>(
- m_Specific(Plan.getTripCount()), m_VPValue(AVLNext)));
- assert(FoundAVLNext && "Didn't find AVL backedge?");
// Convert EVLPhi to concrete recipe.
auto *ScalarR =
@@ -3302,21 +3291,45 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
VPRecipeBase *CanonicalIVIncrement = Backedge->getDefiningRecipe();
CanonicalIVIncrement->eraseFromParent();
CanonicalIV->eraseFromParent();
+}
- // Replace the use of VectorTripCount in the latch-exiting block.
- // Before: (branch-on-cond (icmp eq EVLIVInc, VectorTripCount))
- // After: (branch-on-cond icmp eq AVLNext, 0)
+void VPlanTransforms::convertEVLExitCond(VPlan &Plan) {
+ VPEVLBasedIVPHIRecipe *EVLPhi = findEVLPhi(Plan);
+ if (!EVLPhi)
+ return;
+
+ // Bail if not an EVL tail folded loop.
+ VPValue *AVL;
+ if (!match(EVLPhi->getBackedgeValue(),
+ m_c_Add(m_ZExtOrSelf(m_EVL(m_VPValue(AVL))), m_Specific(EVLPhi))))
+ return;
+
+ // The AVL may be capped to a safe distance.
+ VPValue *SafeAVL;
+ if (match(AVL, m_Select(m_VPValue(), m_VPValue(SafeAVL), m_VPValue())))
+ AVL = SafeAVL;
+
+ VPValue *AVLNext;
+ [[maybe_unused]] bool FoundAVLNext =
+ match(AVL, m_VPInstruction<Instruction::PHI>(
+ m_Specific(Plan.getTripCount()), m_VPValue(AVLNext)));
+ assert(FoundAVLNext && "Didn't find AVL backedge?");
+
+ VPBasicBlock *HeaderVPBB = EVLPhi->getParent();
VPBasicBlock *LatchExiting =
HeaderVPBB->getPredecessors()[1]->getEntryBasicBlock();
auto *LatchExitingBr = cast<VPInstruction>(LatchExiting->getTerminator());
if (match(LatchExitingBr, m_BranchOnCond(m_True())))
return;
- assert(match(LatchExitingBr, m_BranchOnCond(m_SpecificCmp(
- CmpInst::ICMP_EQ, m_VPValue(EVLIncrement),
- m_Specific(&Plan.getVectorTripCount())))) &&
- "Expected BranchOnCond with ICmp comparing EVL increment with vector "
- "trip count");
+ [[maybe_unused]] auto *CanIV = cast<VPPhi>(&*EVLPhi->getParent()->begin());
+ assert(
+ match(LatchExitingBr,
+ m_BranchOnCond(m_SpecificCmp(
+ CmpInst::ICMP_EQ, m_Specific(CanIV->getIncomingValue(1)),
+ m_Specific(&Plan.getVectorTripCount())))) &&
+ "Expected BranchOnCond with ICmp comparing CanIV increment with vector "
+ "trip count");
Type *AVLTy = VPTypeAnalysis(Plan).inferScalarType(AVLNext);
VPBuilder Builder(LatchExitingBr);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index e0d09a099647a..16db95df15da6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -315,12 +315,14 @@ struct VPlanTransforms {
/// variable vector lengths instead of fixed lengths. This transformation:
/// * Makes EVL-Phi concrete.
// * Removes CanonicalIV and increment.
- /// * Replaces the exit condition from
- /// (branch-on-count CanonicalIVInc, VectorTripCount)
- /// to
- /// (branch-on-cond eq AVLNext, 0)
static void canonicalizeEVLLoops(VPlan &Plan);
+ /// Replaces the exit condition from
+ /// (branch-on-count CanonicalIVInc, VectorTripCount)
+ /// to
+ /// (branch-on-cond eq AVLNext, 0)
+ static void convertEVLExitCond(VPlan &Plan);
+
/// Lower abstract recipes to concrete ones, that can be codegen'd.
static void convertToConcreteRecipes(VPlan &Plan);
>From 0a49ca068e5cfb306defc4008c92e4cae0c449f9 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Fri, 30 Jan 2026 01:21:41 +0800
Subject: [PATCH 2/2] Address review comments
* Move to before loop regions are dissolved so we can access the canonical IV easier.
* Fix comment
* Reorder definition
---
.../Transforms/Vectorize/LoopVectorize.cpp | 4 +-
.../Transforms/Vectorize/VPlanTransforms.cpp | 41 ++++++++++---------
.../Transforms/Vectorize/VPlanTransforms.h | 12 +++---
3 files changed, 29 insertions(+), 28 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 6f0c59bc8f0ea..3259200011616 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7424,14 +7424,14 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
VPlanTransforms::removeDeadRecipes(BestVPlan);
VPlanTransforms::convertToConcreteRecipes(BestVPlan);
+ // Convert the exit condition to AVLNext == 0 for EVL tail folded loops.
+ VPlanTransforms::convertEVLExitCond(BestVPlan);
// Regions are dissolved after optimizing for VF and UF, which completely
// removes unneeded loop regions first.
VPlanTransforms::dissolveLoopRegions(BestVPlan);
// Expand BranchOnTwoConds after dissolution, when latch has direct access to
// its successors.
VPlanTransforms::expandBranchOnTwoConds(BestVPlan);
- // Switch the exit condition to AVLNext == 0 for EVL tail folded loops.
- VPlanTransforms::convertEVLExitCond(BestVPlan);
// Canonicalize EVL loops after regions are dissolved.
VPlanTransforms::canonicalizeEVLLoops(BestVPlan);
VPlanTransforms::materializeBackedgeTakenCount(BestVPlan, VectorPH);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 52dd74baef6ec..23050beeae0fb 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -3247,9 +3247,9 @@ void VPlanTransforms::addExplicitVectorLength(
Plan.setUF(1);
}
-/// Find EVL loop entries by locating VPEVLBasedIVPHIRecipe.
-/// There should be only one EVL PHI in the entire plan.
-static VPEVLBasedIVPHIRecipe *findEVLPhi(VPlan &Plan) {
+void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
+ // Find EVL loop entries by locating VPEVLBasedIVPHIRecipe.
+ // There should be only one EVL PHI in the entire plan.
VPEVLBasedIVPHIRecipe *EVLPhi = nullptr;
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
@@ -3260,11 +3260,6 @@ static VPEVLBasedIVPHIRecipe *findEVLPhi(VPlan &Plan) {
EVLPhi = PhiR;
}
- return EVLPhi;
-}
-
-void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
- VPEVLBasedIVPHIRecipe *EVLPhi = findEVLPhi(Plan);
// Early return if no EVL PHI is found.
if (!EVLPhi)
return;
@@ -3294,7 +3289,17 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
}
void VPlanTransforms::convertEVLExitCond(VPlan &Plan) {
- VPEVLBasedIVPHIRecipe *EVLPhi = findEVLPhi(Plan);
+ VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+ // The canonical IV may not exist at this stage.
+ if (!LoopRegion ||
+ !isa<VPCanonicalIVPHIRecipe>(LoopRegion->getEntryBasicBlock()->front()))
+ return;
+ VPCanonicalIVPHIRecipe *CanIV = LoopRegion->getCanonicalIV();
+ if (!CanIV || std::next(CanIV->getIterator()) == CanIV->getParent()->end())
+ return;
+ // The EVL IV is always immediately after the canonical IV.
+ auto *EVLPhi =
+ dyn_cast_or_null<VPEVLBasedIVPHIRecipe>(std::next(CanIV->getIterator()));
if (!EVLPhi)
return;
@@ -3315,16 +3320,13 @@ void VPlanTransforms::convertEVLExitCond(VPlan &Plan) {
m_Specific(Plan.getTripCount()), m_VPValue(AVLNext)));
assert(FoundAVLNext && "Didn't find AVL backedge?");
- VPBasicBlock *HeaderVPBB = EVLPhi->getParent();
- VPBasicBlock *LatchExiting =
- HeaderVPBB->getPredecessors()[1]->getEntryBasicBlock();
- auto *LatchExitingBr = cast<VPInstruction>(LatchExiting->getTerminator());
- if (match(LatchExitingBr, m_BranchOnCond(m_True())))
+ VPBasicBlock *Latch = LoopRegion->getExitingBasicBlock();
+ auto *LatchBr = cast<VPInstruction>(Latch->getTerminator());
+ if (match(LatchBr, m_BranchOnCond(m_True())))
return;
- [[maybe_unused]] auto *CanIV = cast<VPPhi>(&*EVLPhi->getParent()->begin());
assert(
- match(LatchExitingBr,
+ match(LatchBr,
m_BranchOnCond(m_SpecificCmp(
CmpInst::ICMP_EQ, m_Specific(CanIV->getIncomingValue(1)),
m_Specific(&Plan.getVectorTripCount())))) &&
@@ -3332,10 +3334,9 @@ void VPlanTransforms::convertEVLExitCond(VPlan &Plan) {
"trip count");
Type *AVLTy = VPTypeAnalysis(Plan).inferScalarType(AVLNext);
- VPBuilder Builder(LatchExitingBr);
- LatchExitingBr->setOperand(0,
- Builder.createICmp(CmpInst::ICMP_EQ, AVLNext,
- Plan.getConstantInt(AVLTy, 0)));
+ VPBuilder Builder(LatchBr);
+ LatchBr->setOperand(0, Builder.createICmp(CmpInst::ICMP_EQ, AVLNext,
+ Plan.getConstantInt(AVLTy, 0)));
}
void VPlanTransforms::replaceSymbolicStrides(
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index 16db95df15da6..30b95b08398b0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -301,6 +301,12 @@ struct VPlanTransforms {
VPlan &Plan, VPBasicBlock *HeaderVPBB,
VPBasicBlock *LatchVPBB);
+ /// Replaces the exit condition from
+ /// (branch-on-count CanonicalIVInc, VectorTripCount)
+ /// to
+ /// (branch-on-cond eq AVLNext, 0)
+ static void convertEVLExitCond(VPlan &Plan);
+
/// Replace loop regions with explicit CFG.
static void dissolveLoopRegions(VPlan &Plan);
@@ -317,12 +323,6 @@ struct VPlanTransforms {
// * Removes CanonicalIV and increment.
static void canonicalizeEVLLoops(VPlan &Plan);
- /// Replaces the exit condition from
- /// (branch-on-count CanonicalIVInc, VectorTripCount)
- /// to
- /// (branch-on-cond eq AVLNext, 0)
- static void convertEVLExitCond(VPlan &Plan);
-
/// Lower abstract recipes to concrete ones, that can be codegen'd.
static void convertToConcreteRecipes(VPlan &Plan);
More information about the llvm-commits
mailing list