Skip to content

[LV] Replace UncountableEdge with UncountableEarlyExitingBlock (NFC). #151311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,11 @@ class LoopVectorizationLegality {

/// Returns true if the loop has exactly one uncountable early exit, i.e. an
/// uncountable exit that isn't the latch block.
bool hasUncountableEarlyExit() const {
return getUncountableEdge().has_value();
}
bool hasUncountableEarlyExit() const { return UncountableExitingBB; }

/// Returns the uncountable early exiting block, if there is exactly one.
BasicBlock *getUncountableEarlyExitingBlock() const {
return hasUncountableEarlyExit() ? getUncountableEdge()->first : nullptr;
}

/// Returns the destination of the uncountable early exiting block, if there
/// is exactly one.
BasicBlock *getUncountableEarlyExitBlock() const {
return hasUncountableEarlyExit() ? getUncountableEdge()->second : nullptr;
return UncountableExitingBB;
}

/// Return true if there is store-load forwarding dependencies.
Expand Down Expand Up @@ -473,13 +465,6 @@ class LoopVectorizationLegality {
return CountableExitingBlocks;
}

/// Returns the loop edge to an uncountable exit, or std::nullopt if there
/// isn't a single such edge.
std::optional<std::pair<BasicBlock *, BasicBlock *>>
getUncountableEdge() const {
return UncountableEdge;
}

private:
/// Return true if the pre-header, exiting and latch blocks of \p Lp and all
/// its nested loops are considered legal for vectorization. These legal
Expand Down Expand Up @@ -659,9 +644,9 @@ class LoopVectorizationLegality {
/// the exact backedge taken count is not computable.
SmallVector<BasicBlock *, 4> CountableExitingBlocks;

/// Keep track of the loop edge to an uncountable exit, comprising a pair
/// of (Exiting, Exit) blocks, if there is exactly one early exit.
std::optional<std::pair<BasicBlock *, BasicBlock *>> UncountableEdge;
/// Keep track of an uncountable exiting block, if there is exactly one early
/// exit.
BasicBlock *UncountableExitingBB = nullptr;
};

} // namespace llvm
Expand Down
17 changes: 9 additions & 8 deletions llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {

// Keep a record of all the exiting blocks.
SmallVector<const SCEVPredicate *, 4> Predicates;
std::optional<std::pair<BasicBlock *, BasicBlock *>> SingleUncountableEdge;
BasicBlock *SingleUncountableExitingBlock = nullptr;
for (BasicBlock *BB : ExitingBlocks) {
const SCEV *EC =
PSE.getSE()->getPredicatedExitCount(TheLoop, BB, &Predicates);
Expand All @@ -1687,15 +1687,15 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
ExitBlock = Succs[1];
}

if (SingleUncountableEdge) {
if (SingleUncountableExitingBlock) {
reportVectorizationFailure(
"Loop has too many uncountable exits",
"Cannot vectorize early exit loop with more than one early exit",
"TooManyUncountableEarlyExits", ORE, TheLoop);
return false;
}

SingleUncountableEdge = {BB, ExitBlock};
SingleUncountableExitingBlock = BB;
} else
CountableExitingBlocks.push_back(BB);
}
Expand All @@ -1705,15 +1705,15 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
// PSE.getSymbolicMaxBackedgeTakenCount() below.
Predicates.clear();

if (!SingleUncountableEdge) {
if (!SingleUncountableExitingBlock) {
LLVM_DEBUG(dbgs() << "LV: Cound not find any uncountable exits");
return false;
}

// The only supported early exit loops so far are ones where the early
// exiting block is a unique predecessor of the latch block.
BasicBlock *LatchPredBB = LatchBB->getUniquePredecessor();
if (LatchPredBB != SingleUncountableEdge->first) {
if (LatchPredBB != SingleUncountableExitingBlock) {
reportVectorizationFailure("Early exit is not the latch predecessor",
"Cannot vectorize early exit loop",
"EarlyExitNotLatchPredecessor", ORE, TheLoop);
Expand Down Expand Up @@ -1766,7 +1766,7 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
}

// The vectoriser cannot handle loads that occur after the early exit block.
assert(LatchBB->getUniquePredecessor() == SingleUncountableEdge->first &&
assert(LatchBB->getUniquePredecessor() == SingleUncountableExitingBlock &&
"Expected latch predecessor to be the early exiting block");

// TODO: Handle loops that may fault.
Expand All @@ -1789,7 +1789,7 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
LLVM_DEBUG(dbgs() << "LV: Found an early exit loop with symbolic max "
"backedge taken count: "
<< *SymbolicMaxBTC << '\n');
UncountableEdge = SingleUncountableEdge;
UncountableExitingBB = SingleUncountableExitingBlock;
return true;
}

Expand Down Expand Up @@ -1861,7 +1861,8 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
return false;
} else {
if (!isVectorizableEarlyExitLoop()) {
UncountableEdge = std::nullopt;
assert(!hasUncountableEarlyExit() &&
"Must be false without vectorizable early-exit loop");
if (DoExtraAnalysis)
Result = false;
else
Expand Down