[llvm] [VPlan] Compute UncountableExitStyle in VPlan. NFC (PR #206020)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 27 06:06:59 PDT 2026
https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/206020
>From bd29effacc33cc2339ce06e4015bc0c68a7c5e7b Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Fri, 26 Jun 2026 17:59:59 +0800
Subject: [PATCH] [VPlan] Compute UncountableExitStyle in VPlan. NFC
Currently we choose MaskedHandleExitInScalarLoop basde on a simple Instruction::mayWriteToMemory check in LoopVectorizationLegality. We can just compute this in VPlan from a fairly simple loop over the loop body's recipes. This makes it easier to choose additional uncountable exit styles in future.
---
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp | 10 +++-------
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp | 12 ++++++++++++
2 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index d6562cd5de3b0..2e151906e1a63 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6581,15 +6581,11 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan1() {
// If we're vectorizing a loop with an uncountable exit, make sure that the
// recipes are safe to handle.
- // TODO: Remove this once we can properly check the VPlan itself for both
- // the presence of an uncountable exit and the presence of stores in
- // the loop inside handleUncountableEarlyExits itself.
+ // TODO: Remove this once we can properly check the VPlan itself for the
+ // presence of an uncountable exit.
if (Legal->hasUncountableEarlyExit()) {
// TODO: Check target preference for style.
- UncountableExitStyle EEStyle =
- Legal->hasUncountableExitWithSideEffects()
- ? UncountableExitStyle::MaskedHandleExitInScalarLoop
- : UncountableExitStyle::ReadOnly;
+ UncountableExitStyle EEStyle = UncountableExitStyle::ReadOnly;
if (!RUN_VPLAN_PASS(VPlanTransforms::handleUncountableEarlyExits, *VPlan0,
OrigLoop, PSE, *DT, Legal->getAssumptionCache(),
EEStyle))
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 0f76041a9cd7c..787f47988fbee 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -3208,6 +3208,15 @@ static bool handleUncountableExitsWithSideEffects(
return true;
}
+/// Returns true if any non-branch recipe in the loop may have side effects.
+static bool loopHasSideEffects(VPBasicBlock *HeaderVPBB) {
+ for (VPBasicBlock *VPBB : vp_rpo_plain_cfg_loop_body(HeaderVPBB))
+ for (VPRecipeBase &R : *VPBB)
+ if (R.mayHaveSideEffects() && &R != VPBB->getTerminator())
+ return true;
+ return false;
+}
+
bool VPlanTransforms::handleUncountableEarlyExits(
VPlan &Plan, Loop *TheLoop, PredicatedScalarEvolution &PSE,
DominatorTree &DT, AssumptionCache *AC, UncountableExitStyle Style) {
@@ -3218,6 +3227,9 @@ bool VPlanTransforms::handleUncountableEarlyExits(
auto *MiddleVPBB = VPBlockUtils::getPlainCFGMiddleBlock(Plan);
auto [HeaderVPBB, LatchVPBB] = VPBlockUtils::getPlainCFGHeaderAndLatch(Plan);
+ if (loopHasSideEffects(HeaderVPBB))
+ Style = UncountableExitStyle::MaskedHandleExitInScalarLoop;
+
// Dereferenceability is checked separately for uncountable exit loops with
// stores, as only the loads contributing to the exit condition need to
// be checked.
More information about the llvm-commits
mailing list