[llvm] [LoopVectorize] Teach LoopVectorizationLegality about more early exits (PR #107004)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 3 05:48:51 PDT 2024
================
@@ -1442,6 +1486,95 @@ bool LoopVectorizationLegality::canVectorizeLoopNestCFG(
return Result;
}
+bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
+ // At least one of the exiting blocks must be the latch.
+ BasicBlock *LatchBB = TheLoop->getLoopLatch();
+ if (!LatchBB) {
+ reportVectorizationFailure("Loop does not have a latch",
+ "Cannot vectorize early exit loop",
+ "NoLatchEarlyExit", ORE, TheLoop);
+ return false;
+ }
+
+ SmallVector<BasicBlock *, 8> ExitingBlocks;
+ TheLoop->getExitingBlocks(ExitingBlocks);
+
+ // Keep a record of all the exiting blocks with exact exit counts, as well as
+ // those with inexact counts.
+ SmallVector<const SCEVPredicate *, 4> Predicates;
+ for (BasicBlock *BB1 : ExitingBlocks) {
+ const SCEV *EC =
+ PSE.getSE()->getPredicatedExitCount(TheLoop, BB1, &Predicates);
+ if (isa<SCEVCouldNotCompute>(EC)) {
+ UncountableExitingBlocks.push_back(BB1);
+
+ unsigned NumExitBlocks = 0;
+ for (BasicBlock *BB2 : successors(BB1)) {
+ if (!TheLoop->contains(BB2)) {
+ UncountableExitBlocks.push_back(BB2);
+ NumExitBlocks++;
+ }
+ }
+ if (NumExitBlocks > 1) {
----------------
david-arm wrote:
You may be right at the moment that the only way this can happen is with a switch statement, but I can't 100% convince myself of that. :) That's why I wrote this in a more paranoid way by checking the number of blocks manually. What I could do is bail out early if the number of successors > 2, then add the single successor outside of the loop.
https://github.com/llvm/llvm-project/pull/107004
More information about the llvm-commits
mailing list