[llvm] [VPlan] Move tail folding logic out of addMiddleCheck. NFC (PR #203475)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 12 00:41:34 PDT 2026
https://github.com/lukel97 created https://github.com/llvm/llvm-project/pull/203475
We simplify the TripCount == VectorTrip count condition with tail
folding, but we can just do that in foldTailByMasking and keep the
logic in one place instead.
>From e9177e11f7aa404399aab11a80425d1830858430 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Fri, 12 Jun 2026 15:36:51 +0800
Subject: [PATCH] [VPlan] Move tail folding logic out of addMiddleCheck. NFC
We simplify the TripCount == VectorTrip count condition with tail
folding, but we can just do that in foldTailByMasking and keep the
logic in one place instead.
---
.../Transforms/Vectorize/LoopVectorize.cpp | 3 +--
.../Vectorize/VPlanConstruction.cpp | 21 ++++++++++++-------
.../Transforms/Vectorize/VPlanTransforms.h | 2 +-
3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index f9b325a5233e6..c3dbf0a1ee3fa 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6563,8 +6563,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan1() {
// loop with an in-loop mask, then the middle check has already been
// created to compare against the actual number of lanes executed.
if (EEStyle != UncountableExitStyle::MaskedHandleExitInScalarLoop)
- RUN_VPLAN_PASS(VPlanTransforms::addMiddleCheck, *VPlan0,
- CM.foldTailByMasking());
+ RUN_VPLAN_PASS(VPlanTransforms::addMiddleCheck, *VPlan0);
RUN_VPLAN_PASS(VPlanTransforms::createLoopRegions, *VPlan0,
getDebugLocFromInstOrOperands(Legal->getPrimaryInduction()));
if (CM.foldTailByMasking())
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 3e41133ff7463..dd3c8bebd1cd1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -1301,7 +1301,7 @@ bool VPlanTransforms::handleEarlyExits(VPlan &Plan, UncountableExitStyle Style,
return true;
}
-void VPlanTransforms::addMiddleCheck(VPlan &Plan, bool TailFolded) {
+void VPlanTransforms::addMiddleCheck(VPlan &Plan) {
auto *MiddleVPBB = VPBlockUtils::getPlainCFGMiddleBlock(Plan);
// If MiddleVPBB has a single successor then the original loop does not exit
// via the latch and the single successor must be the scalar preheader.
@@ -1332,12 +1332,9 @@ void VPlanTransforms::addMiddleCheck(VPlan &Plan, bool TailFolded) {
auto *LatchVPBB = cast<VPBasicBlock>(MiddleVPBB->getSinglePredecessor());
DebugLoc LatchDL = LatchVPBB->getTerminator()->getDebugLoc();
VPBuilder Builder(MiddleVPBB);
- VPValue *Cmp;
- if (TailFolded)
- Cmp = Plan.getTrue();
- else
- Cmp = Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
- &Plan.getVectorTripCount(), LatchDL, "cmp.n");
+ VPValue *Cmp =
+ Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
+ &Plan.getVectorTripCount(), LatchDL, "cmp.n");
Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp}, LatchDL);
}
@@ -1449,6 +1446,16 @@ void VPlanTransforms::foldTailByMasking(VPlan &Plan) {
Builder.createNaryOp(VPInstruction::ExtractLane, {LastActiveLane, Op});
R.getVPSingleValue()->replaceAllUsesWith(Ext);
}
+
+ // VectorTripCount now equals TripCount so simplify the MiddleVPBB branch.
+ assert(match(Plan.getMiddleBlock()->getTerminator(),
+ m_BranchOnCond(m_OneUse(m_SpecificICmp(
+ CmpInst::ICMP_EQ, m_Specific(Plan.getTripCount()),
+ m_Specific(&Plan.getVectorTripCount()))))) &&
+ "Unexpected MiddleVPBB branch");
+ VPValue *OldCond = Plan.getMiddleBlock()->getTerminator()->getOperand(0);
+ Plan.getMiddleBlock()->getTerminator()->setOperand(0, Plan.getTrue());
+ OldCond->getDefiningRecipe()->eraseFromParent();
}
/// Insert \p CheckBlockVPBB on the edge leading to the vector preheader,
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index b186594cd5b1b..51984b803ab43 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -172,7 +172,7 @@ struct VPlanTransforms {
/// If a check is needed to guard executing the scalar epilogue loop, it will
/// be added to the middle block.
- LLVM_ABI_FOR_TEST static void addMiddleCheck(VPlan &Plan, bool TailFolded);
+ LLVM_ABI_FOR_TEST static void addMiddleCheck(VPlan &Plan);
// Create a check in \p CheckBlock to see if the vector loop should be
// executed. May create VPExpandSCEV recipes in the plan's entry block.
More information about the llvm-commits
mailing list