[llvm] [VPlan] Speed up VPSlotTracker by using ModuleSlotTracker (PR #139881)

Igor Kirillov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 11 02:03:43 PDT 2025


https://github.com/igogo-x86 updated https://github.com/llvm/llvm-project/pull/139881

>From 7e544345848091c37baa49789bc6a246effa69b6 Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Wed, 14 May 2025 10:57:58 +0000
Subject: [PATCH 1/5] [VPlan] Speed up VPSlotTracker by using ModuleSlotTracker
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently, when VPSlotTracker is initialized with a VPlan, its
assignName method calls printAsOperand on each underlying instruction.
Each such call recomputes slot numbers for the entire function, leading
to O(N × M) complexity, where M is the number of instructions in the
loop and N is the number of instructions in the function.

This results in slow debug output for large loops. For example, printing
costs of all instructions becomes O(M² × N), which is especially painful
when enabling verbose dumps.

This patch improves debugging performance by caching slot numbers using
ModuleSlotTracker. It avoids redundant recomputation and makes debug
output significantly faster.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp      |  5 ++++-
 llvm/lib/Transforms/Vectorize/VPlanHelpers.h | 14 +++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 1838562f26b82..358ab2e0dfcad 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1506,7 +1506,10 @@ void VPSlotTracker::assignName(const VPValue *V) {
   std::string Name;
   if (UV) {
     raw_string_ostream S(Name);
-    UV->printAsOperand(S, false);
+    if (MST)
+      UV->printAsOperand(S, false, *MST);
+    else
+      UV->printAsOperand(S, false);
   } else
     Name = VPI->getName();
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
index 0446991ebfff3..e2e2e18c8e4c7 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -23,6 +23,7 @@
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/IR/DebugLoc.h"
+#include "llvm/IR/ModuleSlotTracker.h"
 #include "llvm/Support/InstructionCost.h"
 
 namespace llvm {
@@ -387,14 +388,25 @@ class VPSlotTracker {
   /// Number to assign to the next VPValue without underlying value.
   unsigned NextSlot = 0;
 
+  /// Cache slot indexes to avoid recomputing them on each printAsOperand call.
+  std::unique_ptr<ModuleSlotTracker> MST;
+
   void assignName(const VPValue *V);
   void assignNames(const VPlan &Plan);
   void assignNames(const VPBasicBlock *VPBB);
 
 public:
   VPSlotTracker(const VPlan *Plan = nullptr) {
-    if (Plan)
+    if (Plan) {
+      // This check is required to support unit tests with incomplete IR.
+      if (Function *F =
+              Plan->getScalarHeader()->getIRBasicBlock()->getParent()) {
+        Module *M = F->getParent();
+        MST = std::make_unique<ModuleSlotTracker>(M);
+        MST->incorporateFunction(*F);
+      }
       assignNames(*Plan);
+    }
   }
 
   /// Returns the name assigned to \p V, if there is one, otherwise try to

>From fd0ec689d41b2fdd045c2a0440255b820b18738e Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Fri, 16 May 2025 10:59:06 +0000
Subject: [PATCH 2/5] Initialize MST lazily

---
 llvm/lib/Transforms/Vectorize/VPlan.cpp      | 17 +++++++++++++++--
 llvm/lib/Transforms/Vectorize/VPlanHelpers.h | 13 +++----------
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 358ab2e0dfcad..8e054bf3c98c6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1506,10 +1506,23 @@ void VPSlotTracker::assignName(const VPValue *V) {
   std::string Name;
   if (UV) {
     raw_string_ostream S(Name);
-    if (MST)
+    if (MST) {
       UV->printAsOperand(S, false, *MST);
-    else
+    } else if (isa<Instruction>(UV) && !UV->hasName()) {
+      // Lazily create the ModuleSlotTracker when we first hit an unnamed
+      // instruction
+      auto *IUV = cast<Instruction>(UV);
+      // This check is required to support unit tests with incomplete IR.
+      if (IUV->getParent()) {
+        MST = std::make_unique<ModuleSlotTracker>(IUV->getModule());
+        MST->incorporateFunction(*IUV->getFunction());
+      } else {
+        MST = std::make_unique<ModuleSlotTracker>(nullptr);
+      }
+      UV->printAsOperand(S, false, *MST);
+    } else {
       UV->printAsOperand(S, false);
+    }
   } else
     Name = VPI->getName();
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
index e2e2e18c8e4c7..a8082b311753c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -388,7 +388,8 @@ class VPSlotTracker {
   /// Number to assign to the next VPValue without underlying value.
   unsigned NextSlot = 0;
 
-  /// Cache slot indexes to avoid recomputing them on each printAsOperand call.
+  /// Lazily created ModuleSlotTracker, used only when unnamed IR instructions
+  /// require slot tracking.
   std::unique_ptr<ModuleSlotTracker> MST;
 
   void assignName(const VPValue *V);
@@ -397,16 +398,8 @@ class VPSlotTracker {
 
 public:
   VPSlotTracker(const VPlan *Plan = nullptr) {
-    if (Plan) {
-      // This check is required to support unit tests with incomplete IR.
-      if (Function *F =
-              Plan->getScalarHeader()->getIRBasicBlock()->getParent()) {
-        Module *M = F->getParent();
-        MST = std::make_unique<ModuleSlotTracker>(M);
-        MST->incorporateFunction(*F);
-      }
+    if (Plan)
       assignNames(*Plan);
-    }
   }
 
   /// Returns the name assigned to \p V, if there is one, otherwise try to

>From 3d6f69223e0131666d031486793df5bdf4b14dd6 Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Fri, 30 May 2025 10:46:41 +0000
Subject: [PATCH 3/5] Move code a separate function

---
 llvm/lib/Transforms/Vectorize/VPlan.cpp      | 52 +++++++++++---------
 llvm/lib/Transforms/Vectorize/VPlanHelpers.h |  1 +
 2 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 8e054bf3c98c6..a7681c8e40fe6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1503,29 +1503,7 @@ void VPSlotTracker::assignName(const VPValue *V) {
 
   // Use the name of the underlying Value, wrapped in "ir<>", and versioned by
   // appending ".Number" to the name if there are multiple uses.
-  std::string Name;
-  if (UV) {
-    raw_string_ostream S(Name);
-    if (MST) {
-      UV->printAsOperand(S, false, *MST);
-    } else if (isa<Instruction>(UV) && !UV->hasName()) {
-      // Lazily create the ModuleSlotTracker when we first hit an unnamed
-      // instruction
-      auto *IUV = cast<Instruction>(UV);
-      // This check is required to support unit tests with incomplete IR.
-      if (IUV->getParent()) {
-        MST = std::make_unique<ModuleSlotTracker>(IUV->getModule());
-        MST->incorporateFunction(*IUV->getFunction());
-      } else {
-        MST = std::make_unique<ModuleSlotTracker>(nullptr);
-      }
-      UV->printAsOperand(S, false, *MST);
-    } else {
-      UV->printAsOperand(S, false);
-    }
-  } else
-    Name = VPI->getName();
-
+  std::string Name = getName(V);
   assert(!Name.empty() && "Name cannot be empty.");
   StringRef Prefix = UV ? "ir<" : "vp<%";
   std::string BaseName = (Twine(Prefix) + Name + Twine(">")).str();
@@ -1570,6 +1548,34 @@ void VPSlotTracker::assignNames(const VPBasicBlock *VPBB) {
       assignName(Def);
 }
 
+std::string VPSlotTracker::getName(const VPValue *V) {
+  auto *UV = V->getUnderlyingValue();
+  auto *VPI = dyn_cast_or_null<VPInstruction>(V->getDefiningRecipe());
+  if (!UV)
+    return VPI->getName().str();
+
+  std::string Name;
+  raw_string_ostream S(Name);
+  if (MST) {
+    UV->printAsOperand(S, false, *MST);
+  } else if (isa<Instruction>(UV) && !UV->hasName()) {
+    // Lazily create the ModuleSlotTracker when we first hit an unnamed
+    // instruction
+    auto *IUV = cast<Instruction>(UV);
+    // This check is required to support unit tests with incomplete IR.
+    if (IUV->getParent()) {
+      MST = std::make_unique<ModuleSlotTracker>(IUV->getModule());
+      MST->incorporateFunction(*IUV->getFunction());
+    } else {
+      MST = std::make_unique<ModuleSlotTracker>(nullptr);
+    }
+    UV->printAsOperand(S, false, *MST);
+  } else {
+    UV->printAsOperand(S, false);
+  }
+  return Name;
+}
+
 std::string VPSlotTracker::getOrCreateName(const VPValue *V) const {
   std::string Name = VPValue2Name.lookup(V);
   if (!Name.empty())
diff --git a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
index a8082b311753c..2a711c3843c4a 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -395,6 +395,7 @@ class VPSlotTracker {
   void assignName(const VPValue *V);
   void assignNames(const VPlan &Plan);
   void assignNames(const VPBasicBlock *VPBB);
+  std::string getName(const VPValue *V);
 
 public:
   VPSlotTracker(const VPlan *Plan = nullptr) {

>From c6437db9ebeddf5f835a71e11cbafa10d0bd9eb1 Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Tue, 10 Jun 2025 13:29:36 +0000
Subject: [PATCH 4/5] Address review comments

---
 llvm/lib/Transforms/Vectorize/VPlan.cpp      | 35 ++++++++++----------
 llvm/lib/Transforms/Vectorize/VPlanHelpers.h |  4 +--
 2 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a7681c8e40fe6..35152e3eee4bd 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1503,7 +1503,12 @@ void VPSlotTracker::assignName(const VPValue *V) {
 
   // Use the name of the underlying Value, wrapped in "ir<>", and versioned by
   // appending ".Number" to the name if there are multiple uses.
-  std::string Name = getName(V);
+  std::string Name;
+  if (UV) {
+    Name = getName(UV);
+  } else {
+    Name = VPI->getName();
+  }
   assert(!Name.empty() && "Name cannot be empty.");
   StringRef Prefix = UV ? "ir<" : "vp<%";
   std::string BaseName = (Twine(Prefix) + Name + Twine(">")).str();
@@ -1548,31 +1553,27 @@ void VPSlotTracker::assignNames(const VPBasicBlock *VPBB) {
       assignName(Def);
 }
 
-std::string VPSlotTracker::getName(const VPValue *V) {
-  auto *UV = V->getUnderlyingValue();
-  auto *VPI = dyn_cast_or_null<VPInstruction>(V->getDefiningRecipe());
-  if (!UV)
-    return VPI->getName().str();
-
+std::string VPSlotTracker::getName(const Value *V) {
   std::string Name;
   raw_string_ostream S(Name);
-  if (MST) {
-    UV->printAsOperand(S, false, *MST);
-  } else if (isa<Instruction>(UV) && !UV->hasName()) {
+  if (V->hasName() || !isa<Instruction>(V)) {
+    V->printAsOperand(S, false);
+    return Name;
+  }
+
+  if (!MST) {
     // Lazily create the ModuleSlotTracker when we first hit an unnamed
     // instruction
-    auto *IUV = cast<Instruction>(UV);
+    auto *I = cast<Instruction>(V);
     // This check is required to support unit tests with incomplete IR.
-    if (IUV->getParent()) {
-      MST = std::make_unique<ModuleSlotTracker>(IUV->getModule());
-      MST->incorporateFunction(*IUV->getFunction());
+    if (I->getParent()) {
+      MST = std::make_unique<ModuleSlotTracker>(I->getModule());
+      MST->incorporateFunction(*I->getFunction());
     } else {
       MST = std::make_unique<ModuleSlotTracker>(nullptr);
     }
-    UV->printAsOperand(S, false, *MST);
-  } else {
-    UV->printAsOperand(S, false);
   }
+  V->printAsOperand(S, false, *MST);
   return Name;
 }
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
index 2a711c3843c4a..5f5000e8d845a 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -23,7 +23,6 @@
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/IR/DebugLoc.h"
-#include "llvm/IR/ModuleSlotTracker.h"
 #include "llvm/Support/InstructionCost.h"
 
 namespace llvm {
@@ -34,6 +33,7 @@ class DominatorTree;
 class InnerLoopVectorizer;
 class IRBuilderBase;
 class LoopInfo;
+class ModuleSlotTracker;
 class SCEV;
 class Type;
 class VPBasicBlock;
@@ -395,7 +395,7 @@ class VPSlotTracker {
   void assignName(const VPValue *V);
   void assignNames(const VPlan &Plan);
   void assignNames(const VPBasicBlock *VPBB);
-  std::string getName(const VPValue *V);
+  std::string getName(const Value *V);
 
 public:
   VPSlotTracker(const VPlan *Plan = nullptr) {

>From f580a1c283ceae0cd3a84607e72de7ed90ea929a Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Wed, 11 Jun 2025 09:03:21 +0000
Subject: [PATCH 5/5] Restore header

---
 llvm/lib/Transforms/Vectorize/VPlanHelpers.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
index 5f5000e8d845a..900c6b751a1d4 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -23,6 +23,7 @@
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/IR/DebugLoc.h"
+#include "llvm/IR/ModuleSlotTracker.h"
 #include "llvm/Support/InstructionCost.h"
 
 namespace llvm {
@@ -33,7 +34,6 @@ class DominatorTree;
 class InnerLoopVectorizer;
 class IRBuilderBase;
 class LoopInfo;
-class ModuleSlotTracker;
 class SCEV;
 class Type;
 class VPBasicBlock;



More information about the llvm-commits mailing list