[llvm] [VPlan][NFC] Cache the vector loop region lookup (PR #199437)

Madhur Amilkanthwar via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 31 02:45:35 PDT 2026


https://github.com/madhur13490 updated https://github.com/llvm/llvm-project/pull/199437

>From 17f710051a626ed43e542b76fbb670e9597dcaab Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Sun, 24 May 2026 11:27:24 -0700
Subject: [PATCH 1/3] [VPlan] Cache the vector loop region lookup

Resolves the TODO in VPlan::getVectorLoopRegion() with a mutable cache
on VPlan, shared by both overloads.

Invalidate it in setEntry() and at the end of
VPlanTransforms::dissolveLoopRegions() -- the only points that change
the top-level region of an existing plan. Doing the invalidation
inside VPRegionBlock::dissolveToCFGLoop() is unsafe because that
function rewires the CFG in stages and the region stays transiently
reachable from getEntry() during the rewire.

Measured on an O3 build of the LLVM test suite (~12k TUs):
3,556,267 hits / 126,196 misses (96.57% hit rate).
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp           | 14 +++++++-------
 llvm/lib/Transforms/Vectorize/VPlan.h             | 10 ++++++++++
 llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp |  2 ++
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index d2a1161984d9a..850b89f203e26 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1096,18 +1096,18 @@ InstructionCost VPlan::cost(ElementCount VF, VPCostContext &Ctx) {
 }
 
 VPRegionBlock *VPlan::getVectorLoopRegion() {
-  // TODO: Cache if possible.
+  if (VectorLoopRegion)
+    return VectorLoopRegion;
   for (VPBlockBase *B : vp_depth_first_shallow(getEntry()))
-    if (auto *R = dyn_cast<VPRegionBlock>(B))
-      return R->isReplicator() ? nullptr : R;
+    if (auto *R = dyn_cast<VPRegionBlock>(B)) {
+      VectorLoopRegion = R->isReplicator() ? nullptr : R;
+      return VectorLoopRegion;
+    }
   return nullptr;
 }
 
 const VPRegionBlock *VPlan::getVectorLoopRegion() const {
-  for (const VPBlockBase *B : vp_depth_first_shallow(getEntry()))
-    if (auto *R = dyn_cast<VPRegionBlock>(B))
-      return R->isReplicator() ? nullptr : R;
-  return nullptr;
+  return const_cast<VPlan *>(this)->getVectorLoopRegion();
 }
 
 bool VPlan::isOuterLoop() const {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 7025f39decaf5..040ac9e4e8da1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -4675,6 +4675,11 @@ class VPlan {
   /// VPlan is destroyed.
   SmallVector<VPBlockBase *> CreatedBlocks;
 
+  /// Cached pointer to the (non-replicator) vector loop region, populated
+  /// lazily by getVectorLoopRegion(). Reset by setEntry() and by
+  /// VPlanTransforms::dissolveLoopRegions() once the loop region is detached.
+  mutable VPRegionBlock *VectorLoopRegion = nullptr;
+
   /// Construct a VPlan with \p Entry to the plan and with \p ScalarHeader
   /// wrapping the original header of the scalar loop. The vector loop will have
   /// index type \p IdxTy.
@@ -4706,8 +4711,13 @@ class VPlan {
   void setEntry(VPBasicBlock *VPBB) {
     Entry = VPBB;
     VPBB->setPlan(this);
+    invalidateVectorLoopRegionCache();
   }
 
+  /// Reset the cached vector loop region pointer. Must be called by
+  /// transforms that change the top-level region structure.
+  void invalidateVectorLoopRegionCache() { VectorLoopRegion = nullptr; }
+
   /// Generate the IR code for this VPlan.
   void execute(VPTransformState *State);
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index e5b7041a002a1..3a1613b5f45d5 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -3936,6 +3936,8 @@ void VPlanTransforms::dissolveLoopRegions(VPlan &Plan) {
   }
   for (VPRegionBlock *R : LoopRegions)
     R->dissolveToCFGLoop();
+  // Drop any cached pointer to the (now-detached) vector loop region.
+  Plan.invalidateVectorLoopRegionCache();
 }
 
 void VPlanTransforms::expandBranchOnTwoConds(VPlan &Plan) {

>From a2ad09947a86a811fcbd284ba1f2c41fc7dbc485 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Wed, 22 Jul 2026 22:19:42 -0700
Subject: [PATCH 2/3] fixup! address comments

---
 llvm/lib/Transforms/Vectorize/VPlan.cpp       | 24 ++++++++++++-------
 llvm/lib/Transforms/Vectorize/VPlan.h         | 10 --------
 .../Transforms/Vectorize/VPlanTransforms.cpp  |  2 --
 3 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 850b89f203e26..c6e9d3549f210 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1095,17 +1095,25 @@ InstructionCost VPlan::cost(ElementCount VF, VPCostContext &Ctx) {
   return Cost;
 }
 
-VPRegionBlock *VPlan::getVectorLoopRegion() {
-  if (VectorLoopRegion)
-    return VectorLoopRegion;
-  for (VPBlockBase *B : vp_depth_first_shallow(getEntry()))
-    if (auto *R = dyn_cast<VPRegionBlock>(B)) {
-      VectorLoopRegion = R->isReplicator() ? nullptr : R;
-      return VectorLoopRegion;
-    }
+// Find the vector loop region by following the last successor of each block,
+// starting from the plan's entry. The vector code path is always the last
+// successor of the entry (and of the min-iters bypass block, if present), and
+// every block on the path to the region has a single predecessor. Stop at the
+// first block with multiple predecessors: in a plain CFG that is the loop
+// header (no region exists yet), and in a rolled CFG it is the middle block
+// following the region.
+static VPRegionBlock *findVectorLoopRegion(VPBlockBase *Entry) {
+  for (VPBlockBase *B = Entry; B && B->getNumPredecessors() <= 1;
+       B = B->hasSuccessors() ? B->getSuccessors().back() : nullptr)
+    if (auto *R = dyn_cast<VPRegionBlock>(B))
+      return R->isReplicator() ? nullptr : R;
   return nullptr;
 }
 
+VPRegionBlock *VPlan::getVectorLoopRegion() {
+  return findVectorLoopRegion(getEntry());
+}
+
 const VPRegionBlock *VPlan::getVectorLoopRegion() const {
   return const_cast<VPlan *>(this)->getVectorLoopRegion();
 }
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 040ac9e4e8da1..7025f39decaf5 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -4675,11 +4675,6 @@ class VPlan {
   /// VPlan is destroyed.
   SmallVector<VPBlockBase *> CreatedBlocks;
 
-  /// Cached pointer to the (non-replicator) vector loop region, populated
-  /// lazily by getVectorLoopRegion(). Reset by setEntry() and by
-  /// VPlanTransforms::dissolveLoopRegions() once the loop region is detached.
-  mutable VPRegionBlock *VectorLoopRegion = nullptr;
-
   /// Construct a VPlan with \p Entry to the plan and with \p ScalarHeader
   /// wrapping the original header of the scalar loop. The vector loop will have
   /// index type \p IdxTy.
@@ -4711,13 +4706,8 @@ class VPlan {
   void setEntry(VPBasicBlock *VPBB) {
     Entry = VPBB;
     VPBB->setPlan(this);
-    invalidateVectorLoopRegionCache();
   }
 
-  /// Reset the cached vector loop region pointer. Must be called by
-  /// transforms that change the top-level region structure.
-  void invalidateVectorLoopRegionCache() { VectorLoopRegion = nullptr; }
-
   /// Generate the IR code for this VPlan.
   void execute(VPTransformState *State);
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 3a1613b5f45d5..e5b7041a002a1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -3936,8 +3936,6 @@ void VPlanTransforms::dissolveLoopRegions(VPlan &Plan) {
   }
   for (VPRegionBlock *R : LoopRegions)
     R->dissolveToCFGLoop();
-  // Drop any cached pointer to the (now-detached) vector loop region.
-  Plan.invalidateVectorLoopRegionCache();
 }
 
 void VPlanTransforms::expandBranchOnTwoConds(VPlan &Plan) {

>From 894539c7a3fdf6f87435ea6aaff43315ac13ef41 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Fri, 31 Jul 2026 02:39:40 -0700
Subject: [PATCH 3/3] fixup! address comments

---
 llvm/lib/Transforms/Vectorize/VPlan.cpp | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index c6e9d3549f210..5ee94118d5e81 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1110,8 +1110,24 @@ static VPRegionBlock *findVectorLoopRegion(VPBlockBase *Entry) {
   return nullptr;
 }
 
+#ifdef EXPENSIVE_CHECKS
+// Reference lookup that scans every top-level block. Used only to validate
+// findVectorLoopRegion() when the invariants of the last-successor walk change.
+static VPRegionBlock *findVectorLoopRegionByScan(VPBlockBase *Entry) {
+  for (VPBlockBase *B : vp_depth_first_shallow(Entry))
+    if (auto *R = dyn_cast<VPRegionBlock>(B))
+      return R->isReplicator() ? nullptr : R;
+  return nullptr;
+}
+#endif
+
 VPRegionBlock *VPlan::getVectorLoopRegion() {
-  return findVectorLoopRegion(getEntry());
+  VPRegionBlock *LoopRegion = findVectorLoopRegion(getEntry());
+#ifdef EXPENSIVE_CHECKS
+  assert(LoopRegion == findVectorLoopRegionByScan(getEntry()) &&
+         "fast vector loop region lookup disagrees with full CFG scan");
+#endif
+  return LoopRegion;
 }
 
 const VPRegionBlock *VPlan::getVectorLoopRegion() const {



More information about the llvm-commits mailing list