[llvm] [VPlan] Speed up VPSlotTracker by using ModuleSlotTracker (PR #139881)
Igor Kirillov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 26 09:25:20 PDT 2025
https://github.com/igogo-x86 updated https://github.com/llvm/llvm-project/pull/139881
>From 6210d2514b14ea72bda889dcd163da423e31cb19 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/6] [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 63ac80698643d..cb181e848ae6d 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1510,7 +1510,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 f33f94b7162c6..b321ec3e31e91 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 {
@@ -388,14 +389,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 af0ab2145ece1a02198d6b5fecbf4220ae965cc8 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/6] 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 cb181e848ae6d..4eca9a1068cce 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1510,10 +1510,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 b321ec3e31e91..043a5d18f2772 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -389,7 +389,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);
@@ -398,16 +399,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 9faa102eadffb7cc235c7282e0ab5f6cb1ed3c5f 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/6] 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 4eca9a1068cce..df44d1ad91d2f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1507,29 +1507,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();
@@ -1574,6 +1552,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 043a5d18f2772..68bf68536f1ce 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -396,6 +396,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 1a449ba1496f867981831b3e049f3c9aafb59f56 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/6] 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 df44d1ad91d2f..b4923ed926cb3 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1507,7 +1507,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();
@@ -1552,31 +1557,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 68bf68536f1ce..c440173f93830 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;
@@ -396,7 +396,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 f9fe9dcaefe10a521a198d0f9b723c6c67ce978f 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/6] 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 c440173f93830..4154720cd2608 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;
>From a7c08265b212b1ac18e8981305cc9425a690f079 Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Thu, 26 Jun 2025 16:25:00 +0000
Subject: [PATCH 6/6] Address comments
---
llvm/lib/Transforms/Vectorize/VPlan.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index b4923ed926cb3..ffa247afa2360 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1508,11 +1508,11 @@ 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) {
+ if (UV)
Name = getName(UV);
- } else {
+ else
Name = VPI->getName();
- }
+
assert(!Name.empty() && "Name cannot be empty.");
StringRef Prefix = UV ? "ir<" : "vp<%";
std::string BaseName = (Twine(Prefix) + Name + Twine(">")).str();
@@ -1567,7 +1567,7 @@ std::string VPSlotTracker::getName(const Value *V) {
if (!MST) {
// Lazily create the ModuleSlotTracker when we first hit an unnamed
- // instruction
+ // instruction.
auto *I = cast<Instruction>(V);
// This check is required to support unit tests with incomplete IR.
if (I->getParent()) {
More information about the llvm-commits
mailing list