[llvm] [VPlan] Tighten m_CanonicalIV() (PR #194312)

Madhur Amilkanthwar via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 27 05:48:37 PDT 2026


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

>From 81b48a6836f769d04994da54b9fb5a515a45c112 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Mon, 27 Apr 2026 01:18:26 -0700
Subject: [PATCH 1/2] [Vplan] Tighten m_CanonicalIV()

Previously m_CanonicalIV() matched any VPRegionValue, which was only
correct because the canonical IV is the sole VPRegionValue today.
Going ahead this is not scalable and there may be
more VPRegionValue.
---
 llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h | 13 +++++++++----
 .../Vectorize/VPlanPatternMatchTest.cpp           | 15 +++++++++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
index 242046480f6e9..78368be40aef5 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
@@ -785,10 +785,15 @@ inline auto m_c_LogicalOr(const Op0_t &Op0, const Op1_t &Op1) {
   return m_c_Select(Op0, m_True(), Op1);
 }
 
-inline auto m_CanonicalIV() {
-  // TODO: Don't assume all region values are canonical IVs.
-  return m_Isa<VPRegionValue>();
-}
+/// Match the canonical induction variable (IV) of any loop region.
+struct canonical_iv_match {
+  template <typename ArgTy> bool match(const ArgTy *V) const {
+    const auto *RV = dyn_cast<VPRegionValue>(V);
+    return RV && RV->getDefiningRegion()->getCanonicalIV() == RV;
+  }
+};
+
+inline canonical_iv_match m_CanonicalIV() { return {}; }
 
 template <typename Op0_t, typename Op1_t, typename Op2_t>
 inline auto m_ScalarIVSteps(const Op0_t &Op0, const Op1_t &Op1,
diff --git a/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp
index 167ba13ede4d0..d5ed68d1f3dc2 100644
--- a/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp
@@ -50,6 +50,21 @@ TEST_F(VPPatternMatchTest, ScalarIVSteps) {
                                             m_Specific(VF))));
 }
 
+TEST_F(VPPatternMatchTest, CanonicalIV) {
+  VPlan &Plan = getPlan();
+  IntegerType *I64Ty = IntegerType::get(C, 64);
+  VPRegionBlock *VPR =
+      Plan.createLoopRegion(I64Ty, DebugLoc::getCompilerGenerated());
+  VPValue *CanIV = VPR->getCanonicalIV();
+
+  using namespace VPlanPatternMatch;
+
+  ASSERT_TRUE(match(CanIV, m_CanonicalIV()));
+
+  VPValue *LiveIn = Plan.getOrAddLiveIn(ConstantInt::get(I64Ty, 1));
+  ASSERT_FALSE(match(LiveIn, m_CanonicalIV()));
+}
+
 TEST_F(VPPatternMatchTest, GetElementPtr) {
   VPlan &Plan = getPlan();
   VPBasicBlock *VPBB = Plan.createVPBasicBlock("entry");

>From e94a30d8774fe2013e9b5089bf1293134586bfc3 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Mon, 27 Apr 2026 05:48:11 -0700
Subject: [PATCH 2/2] fixup! Address review comments

---
 .../Transforms/Vectorize/VPlanPatternMatchTest.cpp     | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp
index d5ed68d1f3dc2..c8b5ba078a8a9 100644
--- a/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/VPlanPatternMatchTest.cpp
@@ -20,6 +20,7 @@ namespace llvm {
 
 namespace {
 using VPPatternMatchTest = VPlanTestBase;
+using namespace VPlanPatternMatch;
 
 TEST_F(VPPatternMatchTest, ScalarIVSteps) {
   VPlan &Plan = getPlan();
@@ -39,8 +40,6 @@ TEST_F(VPPatternMatchTest, ScalarIVSteps) {
   VPValue *Steps2 = Builder.createScalarIVSteps(Instruction::Add, nullptr,
                                                 CanIV, Inc2, VF, DebugLoc());
 
-  using namespace VPlanPatternMatch;
-
   ASSERT_TRUE(match(Steps, m_ScalarIVSteps(m_Specific(CanIV), m_SpecificInt(1),
                                            m_Specific(VF))));
   ASSERT_FALSE(
@@ -57,12 +56,10 @@ TEST_F(VPPatternMatchTest, CanonicalIV) {
       Plan.createLoopRegion(I64Ty, DebugLoc::getCompilerGenerated());
   VPValue *CanIV = VPR->getCanonicalIV();
 
-  using namespace VPlanPatternMatch;
-
   ASSERT_TRUE(match(CanIV, m_CanonicalIV()));
 
-  VPValue *LiveIn = Plan.getOrAddLiveIn(ConstantInt::get(I64Ty, 1));
-  ASSERT_FALSE(match(LiveIn, m_CanonicalIV()));
+  VPRegionValue OtherRegionValue(I64Ty, DebugLoc::getCompilerGenerated(), VPR);
+  ASSERT_FALSE(match(&OtherRegionValue, m_CanonicalIV()));
 }
 
 TEST_F(VPPatternMatchTest, GetElementPtr) {
@@ -79,7 +76,6 @@ TEST_F(VPPatternMatchTest, GetElementPtr) {
   VPInstruction *PtrAdd = Builder.createPtrAdd(Ptr, One);
   VPInstruction *WidePtrAdd = Builder.createWidePtrAdd(Ptr, Two);
 
-  using namespace VPlanPatternMatch;
   ASSERT_TRUE(
       match(PtrAdd, m_GetElementPtr(m_Specific(Ptr), m_SpecificInt(1))));
   ASSERT_FALSE(



More information about the llvm-commits mailing list