Skip to content

Commit e0825e4

Browse files
committed
[VPlan] Speed up VPSlotTracker by using ModuleSlotTracker
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.
1 parent e5f8998 commit e0825e4

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,10 @@ void VPSlotTracker::assignName(const VPValue *V) {
14411441
std::string Name;
14421442
if (UV) {
14431443
raw_string_ostream S(Name);
1444-
UV->printAsOperand(S, false);
1444+
if (MST)
1445+
UV->printAsOperand(S, false, *MST);
1446+
else
1447+
UV->printAsOperand(S, false);
14451448
} else
14461449
Name = VPI->getName();
14471450

llvm/lib/Transforms/Vectorize/VPlanHelpers.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Analysis/DomTreeUpdater.h"
2424
#include "llvm/Analysis/TargetTransformInfo.h"
2525
#include "llvm/IR/DebugLoc.h"
26+
#include "llvm/IR/ModuleSlotTracker.h"
2627
#include "llvm/Support/InstructionCost.h"
2728

2829
namespace llvm {
@@ -382,14 +383,25 @@ class VPSlotTracker {
382383
/// Number to assign to the next VPValue without underlying value.
383384
unsigned NextSlot = 0;
384385

386+
/// Cache slot indexes to avoid recomputing them on each printAsOperand call.
387+
std::unique_ptr<ModuleSlotTracker> MST;
388+
385389
void assignName(const VPValue *V);
386390
void assignNames(const VPlan &Plan);
387391
void assignNames(const VPBasicBlock *VPBB);
388392

389393
public:
390394
VPSlotTracker(const VPlan *Plan = nullptr) {
391-
if (Plan)
395+
if (Plan) {
396+
// This check is required to support unit tests with incomplete IR.
397+
if (Function *F =
398+
Plan->getScalarHeader()->getIRBasicBlock()->getParent()) {
399+
Module *M = F->getParent();
400+
MST = std::make_unique<ModuleSlotTracker>(M);
401+
MST->incorporateFunction(*F);
402+
}
392403
assignNames(*Plan);
404+
}
393405
}
394406

395407
/// Returns the name assigned to \p V, if there is one, otherwise try to

0 commit comments

Comments
 (0)