Skip to content

Commit 732b804

Browse files
authored
[CodeGen][NewPM] Port machine trace metrics analysis to new pass manager. (#108507)
1 parent eccf4d4 commit 732b804

File tree

9 files changed

+130
-50
lines changed

9 files changed

+130
-50
lines changed

llvm/include/llvm/CodeGen/MachineTraceMetrics.h

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@
4646
#ifndef LLVM_CODEGEN_MACHINETRACEMETRICS_H
4747
#define LLVM_CODEGEN_MACHINETRACEMETRICS_H
4848

49-
#include "llvm/ADT/SparseSet.h"
5049
#include "llvm/ADT/ArrayRef.h"
5150
#include "llvm/ADT/DenseMap.h"
5251
#include "llvm/ADT/SmallVector.h"
52+
#include "llvm/ADT/SparseSet.h"
5353
#include "llvm/CodeGen/MachineBasicBlock.h"
5454
#include "llvm/CodeGen/MachineFunctionPass.h"
55+
#include "llvm/CodeGen/MachinePassManager.h"
5556
#include "llvm/CodeGen/TargetSchedule.h"
5657

5758
namespace llvm {
@@ -93,7 +94,7 @@ enum class MachineTraceStrategy {
9394
TS_NumStrategies
9495
};
9596

96-
class MachineTraceMetrics : public MachineFunctionPass {
97+
class MachineTraceMetrics {
9798
const MachineFunction *MF = nullptr;
9899
const TargetInstrInfo *TII = nullptr;
99100
const TargetRegisterInfo *TRI = nullptr;
@@ -102,19 +103,25 @@ class MachineTraceMetrics : public MachineFunctionPass {
102103
TargetSchedModel SchedModel;
103104

104105
public:
106+
friend class MachineTraceMetricsWrapperPass;
105107
friend class Ensemble;
106108
friend class Trace;
107109

108110
class Ensemble;
109111

110-
static char ID;
112+
// For legacy pass.
113+
MachineTraceMetrics() = default;
114+
115+
explicit MachineTraceMetrics(MachineFunction &MF, const MachineLoopInfo &LI) {
116+
init(MF, LI);
117+
}
111118

112-
MachineTraceMetrics();
119+
MachineTraceMetrics(MachineTraceMetrics &&) = default;
113120

114-
void getAnalysisUsage(AnalysisUsage&) const override;
115-
bool runOnMachineFunction(MachineFunction&) override;
116-
void releaseMemory() override;
117-
void verifyAnalysis() const override;
121+
~MachineTraceMetrics();
122+
123+
void init(MachineFunction &Func, const MachineLoopInfo &LI);
124+
void clear();
118125

119126
/// Per-basic block information that doesn't depend on the trace through the
120127
/// block.
@@ -400,6 +407,12 @@ class MachineTraceMetrics : public MachineFunctionPass {
400407
/// Call Ensemble::getTrace() again to update any trace handles.
401408
void invalidate(const MachineBasicBlock *MBB);
402409

410+
/// Handle invalidation explicitly.
411+
bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
412+
MachineFunctionAnalysisManager::Invalidator &);
413+
414+
void verifyAnalysis() const;
415+
403416
private:
404417
// One entry per basic block, indexed by block number.
405418
SmallVector<FixedBlockInfo, 4> BlockInfo;
@@ -412,8 +425,8 @@ class MachineTraceMetrics : public MachineFunctionPass {
412425
SmallVector<unsigned, 0> ProcReleaseAtCycles;
413426

414427
// One ensemble per strategy.
415-
Ensemble
416-
*Ensembles[static_cast<size_t>(MachineTraceStrategy::TS_NumStrategies)];
428+
std::unique_ptr<Ensemble>
429+
Ensembles[static_cast<size_t>(MachineTraceStrategy::TS_NumStrategies)];
417430

418431
// Convert scaled resource usage to a cycle count that can be compared with
419432
// latencies.
@@ -435,6 +448,38 @@ inline raw_ostream &operator<<(raw_ostream &OS,
435448
return OS;
436449
}
437450

451+
class MachineTraceMetricsAnalysis
452+
: public AnalysisInfoMixin<MachineTraceMetricsAnalysis> {
453+
friend AnalysisInfoMixin<MachineTraceMetricsAnalysis>;
454+
static AnalysisKey Key;
455+
456+
public:
457+
using Result = MachineTraceMetrics;
458+
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
459+
};
460+
461+
/// Verifier pass for \c MachineTraceMetrics.
462+
struct MachineTraceMetricsVerifierPass
463+
: PassInfoMixin<MachineTraceMetricsVerifierPass> {
464+
PreservedAnalyses run(MachineFunction &MF,
465+
MachineFunctionAnalysisManager &MFAM);
466+
static bool isRequired() { return true; }
467+
};
468+
469+
class MachineTraceMetricsWrapperPass : public MachineFunctionPass {
470+
public:
471+
static char ID;
472+
MachineTraceMetrics MTM;
473+
474+
MachineTraceMetricsWrapperPass();
475+
476+
void getAnalysisUsage(AnalysisUsage &) const override;
477+
bool runOnMachineFunction(MachineFunction &) override;
478+
void releaseMemory() override { MTM.clear(); }
479+
void verifyAnalysis() const override { MTM.verifyAnalysis(); }
480+
MachineTraceMetrics &getMTM() { return MTM; }
481+
};
482+
438483
} // end namespace llvm
439484

440485
#endif // LLVM_CODEGEN_MACHINETRACEMETRICS_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void initializeMachineRegionInfoPassPass(PassRegistry &);
209209
void initializeMachineSanitizerBinaryMetadataPass(PassRegistry &);
210210
void initializeMachineSchedulerPass(PassRegistry &);
211211
void initializeMachineSinkingPass(PassRegistry &);
212-
void initializeMachineTraceMetricsPass(PassRegistry &);
212+
void initializeMachineTraceMetricsWrapperPassPass(PassRegistry &);
213213
void initializeMachineUniformityInfoPrinterPassPass(PassRegistry &);
214214
void initializeMachineUniformityAnalysisPassPass(PassRegistry &);
215215
void initializeMachineVerifierLegacyPassPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ MACHINE_FUNCTION_ANALYSIS("machine-opt-remark-emitter",
106106
MachineOptimizationRemarkEmitterAnalysis())
107107
MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
108108
MachinePostDominatorTreeAnalysis())
109+
MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics", MachineTraceMetricsAnalysis())
109110
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
110111
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
111112
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
@@ -119,8 +120,6 @@ MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
119120
// MachinePostDominatorTreeAnalysis())
120121
// MACHINE_FUNCTION_ANALYSIS("machine-region-info",
121122
// MachineRegionInfoPassAnalysis())
122-
// MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics",
123-
// MachineTraceMetricsAnalysis()) MACHINE_FUNCTION_ANALYSIS("reaching-def",
124123
// ReachingDefAnalysisAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-reg-matrix",
125124
// LiveRegMatrixAnalysis()) MACHINE_FUNCTION_ANALYSIS("gc-analysis",
126125
// GCMachineCodeAnalysisPass())
@@ -156,6 +155,7 @@ MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
156155
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
157156
MACHINE_FUNCTION_PASS("two-address-instruction", TwoAddressInstructionPass())
158157
MACHINE_FUNCTION_PASS("verify", MachineVerifierPass())
158+
MACHINE_FUNCTION_PASS("verify<machine-trace-metrics>", MachineTraceMetricsVerifierPass())
159159
#undef MACHINE_FUNCTION_PASS
160160

161161
#ifndef MACHINE_FUNCTION_PASS_WITH_PARAMS

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE,
792792
"Early If Converter", false, false)
793793
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
794794
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
795-
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
795+
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
796796
INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE,
797797
"Early If Converter", false, false)
798798

@@ -802,8 +802,8 @@ void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const {
802802
AU.addPreserved<MachineDominatorTreeWrapperPass>();
803803
AU.addRequired<MachineLoopInfoWrapperPass>();
804804
AU.addPreserved<MachineLoopInfoWrapperPass>();
805-
AU.addRequired<MachineTraceMetrics>();
806-
AU.addPreserved<MachineTraceMetrics>();
805+
AU.addRequired<MachineTraceMetricsWrapperPass>();
806+
AU.addPreserved<MachineTraceMetricsWrapperPass>();
807807
MachineFunctionPass::getAnalysisUsage(AU);
808808
}
809809

@@ -1093,7 +1093,7 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
10931093
MRI = &MF.getRegInfo();
10941094
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
10951095
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1096-
Traces = &getAnalysis<MachineTraceMetrics>();
1096+
Traces = &getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
10971097
MinInstr = nullptr;
10981098

10991099
bool Changed = false;

llvm/lib/CodeGen/MachineCombiner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ char &llvm::MachineCombinerID = MachineCombiner::ID;
133133
INITIALIZE_PASS_BEGIN(MachineCombiner, DEBUG_TYPE,
134134
"Machine InstCombiner", false, false)
135135
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
136-
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
136+
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
137137
INITIALIZE_PASS_END(MachineCombiner, DEBUG_TYPE, "Machine InstCombiner",
138138
false, false)
139139

@@ -142,8 +142,8 @@ void MachineCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
142142
AU.addPreserved<MachineDominatorTreeWrapperPass>();
143143
AU.addRequired<MachineLoopInfoWrapperPass>();
144144
AU.addPreserved<MachineLoopInfoWrapperPass>();
145-
AU.addRequired<MachineTraceMetrics>();
146-
AU.addPreserved<MachineTraceMetrics>();
145+
AU.addRequired<MachineTraceMetricsWrapperPass>();
146+
AU.addPreserved<MachineTraceMetricsWrapperPass>();
147147
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
148148
AU.addRequired<ProfileSummaryInfoWrapperPass>();
149149
MachineFunctionPass::getAnalysisUsage(AU);
@@ -727,7 +727,7 @@ bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
727727
TSchedModel.init(STI);
728728
MRI = &MF.getRegInfo();
729729
MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
730-
Traces = &getAnalysis<MachineTraceMetrics>();
730+
Traces = &getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
731731
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
732732
MBFI = (PSI && PSI->hasProfileSummary()) ?
733733
&getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :

llvm/lib/CodeGen/MachineTraceMetrics.cpp

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,66 @@ using namespace llvm;
3939

4040
#define DEBUG_TYPE "machine-trace-metrics"
4141

42-
char MachineTraceMetrics::ID = 0;
42+
AnalysisKey MachineTraceMetricsAnalysis::Key;
4343

44-
char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
44+
MachineTraceMetricsAnalysis::Result
45+
MachineTraceMetricsAnalysis::run(MachineFunction &MF,
46+
MachineFunctionAnalysisManager &MFAM) {
47+
return Result(MF, MFAM.getResult<MachineLoopAnalysis>(MF));
48+
}
49+
50+
PreservedAnalyses
51+
MachineTraceMetricsVerifierPass::run(MachineFunction &MF,
52+
MachineFunctionAnalysisManager &MFAM) {
53+
MFAM.getResult<MachineTraceMetricsAnalysis>(MF).verifyAnalysis();
54+
return PreservedAnalyses::all();
55+
}
4556

46-
INITIALIZE_PASS_BEGIN(MachineTraceMetrics, DEBUG_TYPE, "Machine Trace Metrics",
47-
false, true)
57+
char MachineTraceMetricsWrapperPass::ID = 0;
58+
59+
char &llvm::MachineTraceMetricsID = MachineTraceMetricsWrapperPass::ID;
60+
61+
INITIALIZE_PASS_BEGIN(MachineTraceMetricsWrapperPass, DEBUG_TYPE,
62+
"Machine Trace Metrics", false, true)
4863
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
49-
INITIALIZE_PASS_END(MachineTraceMetrics, DEBUG_TYPE,
64+
INITIALIZE_PASS_END(MachineTraceMetricsWrapperPass, DEBUG_TYPE,
5065
"Machine Trace Metrics", false, true)
5166

52-
MachineTraceMetrics::MachineTraceMetrics() : MachineFunctionPass(ID) {
53-
std::fill(std::begin(Ensembles), std::end(Ensembles), nullptr);
54-
}
67+
MachineTraceMetricsWrapperPass::MachineTraceMetricsWrapperPass()
68+
: MachineFunctionPass(ID) {}
5569

56-
void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
70+
void MachineTraceMetricsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
5771
AU.setPreservesAll();
5872
AU.addRequired<MachineLoopInfoWrapperPass>();
5973
MachineFunctionPass::getAnalysisUsage(AU);
6074
}
6175

62-
bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) {
76+
void MachineTraceMetrics::init(MachineFunction &Func,
77+
const MachineLoopInfo &LI) {
6378
MF = &Func;
6479
const TargetSubtargetInfo &ST = MF->getSubtarget();
6580
TII = ST.getInstrInfo();
6681
TRI = ST.getRegisterInfo();
6782
MRI = &MF->getRegInfo();
68-
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
83+
Loops = &LI;
6984
SchedModel.init(&ST);
7085
BlockInfo.resize(MF->getNumBlockIDs());
7186
ProcReleaseAtCycles.resize(MF->getNumBlockIDs() *
7287
SchedModel.getNumProcResourceKinds());
88+
}
89+
90+
bool MachineTraceMetricsWrapperPass::runOnMachineFunction(MachineFunction &MF) {
91+
MTM.init(MF, getAnalysis<MachineLoopInfoWrapperPass>().getLI());
7392
return false;
7493
}
7594

76-
void MachineTraceMetrics::releaseMemory() {
95+
MachineTraceMetrics::~MachineTraceMetrics() { clear(); }
96+
97+
void MachineTraceMetrics::clear() {
7798
MF = nullptr;
7899
BlockInfo.clear();
79-
for (Ensemble *&E : Ensembles) {
80-
delete E;
81-
E = nullptr;
82-
}
100+
for (auto &E : Ensembles)
101+
E.reset();
83102
}
84103

85104
//===----------------------------------------------------------------------===//
@@ -395,35 +414,50 @@ MachineTraceMetrics::Ensemble *
395414
MachineTraceMetrics::getEnsemble(MachineTraceStrategy strategy) {
396415
assert(strategy < MachineTraceStrategy::TS_NumStrategies &&
397416
"Invalid trace strategy enum");
398-
Ensemble *&E = Ensembles[static_cast<size_t>(strategy)];
417+
std::unique_ptr<MachineTraceMetrics::Ensemble> &E =
418+
Ensembles[static_cast<size_t>(strategy)];
399419
if (E)
400-
return E;
420+
return E.get();
401421

402422
// Allocate new Ensemble on demand.
403423
switch (strategy) {
404424
case MachineTraceStrategy::TS_MinInstrCount:
405-
return (E = new MinInstrCountEnsemble(this));
425+
E = std::make_unique<MinInstrCountEnsemble>(MinInstrCountEnsemble(this));
426+
break;
406427
case MachineTraceStrategy::TS_Local:
407-
return (E = new LocalEnsemble(this));
428+
E = std::make_unique<LocalEnsemble>(LocalEnsemble(this));
429+
break;
408430
default: llvm_unreachable("Invalid trace strategy enum");
409431
}
432+
return E.get();
410433
}
411434

412435
void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) {
413436
LLVM_DEBUG(dbgs() << "Invalidate traces through " << printMBBReference(*MBB)
414437
<< '\n');
415438
BlockInfo[MBB->getNumber()].invalidate();
416-
for (Ensemble *E : Ensembles)
439+
for (auto &E : Ensembles)
417440
if (E)
418441
E->invalidate(MBB);
419442
}
420443

444+
bool MachineTraceMetrics::invalidate(
445+
MachineFunction &, const PreservedAnalyses &PA,
446+
MachineFunctionAnalysisManager::Invalidator &) {
447+
// Check whether the analysis, all analyses on machine functions, or the
448+
// machine function's CFG have been preserved.
449+
auto PAC = PA.getChecker<MachineTraceMetricsAnalysis>();
450+
return !PAC.preserved() &&
451+
!PAC.preservedSet<AllAnalysesOn<MachineFunction>>() &&
452+
!PAC.preservedSet<CFGAnalyses>();
453+
}
454+
421455
void MachineTraceMetrics::verifyAnalysis() const {
422456
if (!MF)
423457
return;
424458
#ifndef NDEBUG
425459
assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size");
426-
for (Ensemble *E : Ensembles)
460+
for (auto &E : Ensembles)
427461
if (E)
428462
E->verify();
429463
#endif

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
#include "llvm/CodeGen/MachinePassManager.h"
110110
#include "llvm/CodeGen/MachinePostDominators.h"
111111
#include "llvm/CodeGen/MachineRegisterInfo.h"
112+
#include "llvm/CodeGen/MachineTraceMetrics.h"
112113
#include "llvm/CodeGen/MachineVerifier.h"
113114
#include "llvm/CodeGen/PHIElimination.h"
114115
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"

llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ INITIALIZE_PASS_BEGIN(AArch64ConditionalCompares, "aarch64-ccmp",
795795
"AArch64 CCMP Pass", false, false)
796796
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
797797
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
798-
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
798+
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
799799
INITIALIZE_PASS_END(AArch64ConditionalCompares, "aarch64-ccmp",
800800
"AArch64 CCMP Pass", false, false)
801801

@@ -809,8 +809,8 @@ void AArch64ConditionalCompares::getAnalysisUsage(AnalysisUsage &AU) const {
809809
AU.addPreserved<MachineDominatorTreeWrapperPass>();
810810
AU.addRequired<MachineLoopInfoWrapperPass>();
811811
AU.addPreserved<MachineLoopInfoWrapperPass>();
812-
AU.addRequired<MachineTraceMetrics>();
813-
AU.addPreserved<MachineTraceMetrics>();
812+
AU.addRequired<MachineTraceMetricsWrapperPass>();
813+
AU.addPreserved<MachineTraceMetricsWrapperPass>();
814814
MachineFunctionPass::getAnalysisUsage(AU);
815815
}
816816

@@ -937,7 +937,7 @@ bool AArch64ConditionalCompares::runOnMachineFunction(MachineFunction &MF) {
937937
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
938938
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
939939
MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
940-
Traces = &getAnalysis<MachineTraceMetrics>();
940+
Traces = &getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
941941
MinInstr = nullptr;
942942
MinSize = MF.getFunction().hasMinSize();
943943

0 commit comments

Comments
 (0)