[llvm] [VPlan] Move tail folding out of VPlanPredicator. NFC (PR #176143)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 4 23:39:50 PST 2026
================
@@ -991,6 +991,96 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan) {
TopRegion->getEntryBasicBlock()->setName("vector.body");
}
+void VPlanTransforms::foldTailByMasking(VPlan &Plan) {
+ assert(Plan.getExitBlocks().size() == 1 &&
+ "only a single-exit block is supported currently");
+ assert(Plan.getExitBlocks().front()->getSinglePredecessor() ==
+ Plan.getMiddleBlock() &&
+ "the exit block must have middle block as single predecessor");
+
+ VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+ assert(LoopRegion->getSingleSuccessor() == Plan.getMiddleBlock() &&
+ "The vector loop region must have the middle block as its single "
+ "successor for now");
+ VPBasicBlock *Header = LoopRegion->getEntryBasicBlock();
+
+ Header->splitAt(Header->getFirstNonPhi());
+
+ // Create the header mask, insert it in the header and branch on it.
+ auto *IV =
+ new VPWidenCanonicalIVRecipe(Header->getParent()->getCanonicalIV());
+ VPBuilder Builder(Header, Header->getFirstNonPhi());
+ Builder.insert(IV);
+ VPValue *BTC = Plan.getOrCreateBackedgeTakenCount();
+ VPValue *HeaderMask = Builder.createICmp(CmpInst::ICMP_ULE, IV, BTC);
+ Builder.createNaryOp(VPInstruction::BranchOnCond, HeaderMask);
+
+ VPBasicBlock *OrigLatch = LoopRegion->getExitingBasicBlock();
+ VPValue *IVInc;
+ [[maybe_unused]] bool TermBranchOnCount =
+ match(OrigLatch->getTerminator(),
+ m_BranchOnCount(m_VPValue(IVInc),
+ m_Specific(&Plan.getVectorTripCount())));
+ assert(TermBranchOnCount &&
+ match(IVInc, m_Add(m_Specific(LoopRegion->getCanonicalIV()),
+ m_Specific(&Plan.getVFxUF()))) &&
+ std::next(IVInc->getDefiningRecipe()->getIterator()) ==
+ OrigLatch->getTerminator()->getIterator() &&
+ "Unexpected canonical iv increment");
+
+ // Split the latch at the IV update, and branch to it from the header mask.
+ VPBasicBlock *Latch =
+ OrigLatch->splitAt(IVInc->getDefiningRecipe()->getIterator());
+ Latch->setName("vector.latch");
+ VPBlockUtils::connectBlocks(Header, Latch);
+
+ // Collect any values defined in the loop that need a phi. Currently this
+ // includes header phi backedges and live-outs extracted in the middle block.
+ // TODO: Handle early exits via Plan.getExitBlocks()
+ MapVector<VPValue *, SmallVector<VPUser *>> NeedsPhi;
----------------
lukel97 wrote:
Agreed but I'm also drawing a blank. I guess it's something like "live-out of the predicated section". I'll try and think of something in a follow up PR.
https://github.com/llvm/llvm-project/pull/176143
More information about the llvm-commits
mailing list