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

Igor Kirillov via llvm-commits llvm-commits at lists.llvm.org
Fri May 30 03:47:23 PDT 2025


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

>From 808b97bd136085cf66f6b3d97b0c79170d9634d5 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/3] [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 165b57c87beb1..6e61ab5f551bc 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1498,7 +1498,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 588ae00ca12a17c1f548c474b82002afac845b81 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/3] 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 6e61ab5f551bc..1715325f334d1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1498,10 +1498,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 fc5b3328d56b44c191f52564093759cc8293396c 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/3] 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 1715325f334d1..d3b21a693a245 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1495,29 +1495,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();
@@ -1562,6 +1540,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) {



More information about the llvm-commits mailing list