Skip to content

[CodeGen][NewPM] Port machine trace metrics analysis to new pass manager. #108507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions llvm/include/llvm/CodeGen/MachineTraceMetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@
#ifndef LLVM_CODEGEN_MACHINETRACEMETRICS_H
#define LLVM_CODEGEN_MACHINETRACEMETRICS_H

#include "llvm/ADT/SparseSet.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SparseSet.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/TargetSchedule.h"

namespace llvm {
Expand Down Expand Up @@ -93,7 +94,7 @@ enum class MachineTraceStrategy {
TS_NumStrategies
};

class MachineTraceMetrics : public MachineFunctionPass {
class MachineTraceMetrics {
const MachineFunction *MF = nullptr;
const TargetInstrInfo *TII = nullptr;
const TargetRegisterInfo *TRI = nullptr;
Expand All @@ -102,19 +103,25 @@ class MachineTraceMetrics : public MachineFunctionPass {
TargetSchedModel SchedModel;

public:
friend class MachineTraceMetricsWrapperPass;
friend class Ensemble;
friend class Trace;

class Ensemble;

static char ID;
// For legacy pass.
MachineTraceMetrics() = default;

explicit MachineTraceMetrics(MachineFunction &MF, const MachineLoopInfo &LI) {
init(MF, LI);
}

MachineTraceMetrics();
MachineTraceMetrics(MachineTraceMetrics &&) = default;

void getAnalysisUsage(AnalysisUsage&) const override;
bool runOnMachineFunction(MachineFunction&) override;
void releaseMemory() override;
void verifyAnalysis() const override;
~MachineTraceMetrics();

void init(MachineFunction &Func, const MachineLoopInfo &LI);
void clear();

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

/// Handle invalidation explicitly.
bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &);

void verifyAnalysis() const;

private:
// One entry per basic block, indexed by block number.
SmallVector<FixedBlockInfo, 4> BlockInfo;
Expand All @@ -412,8 +425,8 @@ class MachineTraceMetrics : public MachineFunctionPass {
SmallVector<unsigned, 0> ProcReleaseAtCycles;

// One ensemble per strategy.
Ensemble
*Ensembles[static_cast<size_t>(MachineTraceStrategy::TS_NumStrategies)];
std::unique_ptr<Ensemble>
Ensembles[static_cast<size_t>(MachineTraceStrategy::TS_NumStrategies)];

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

class MachineTraceMetricsAnalysis
: public AnalysisInfoMixin<MachineTraceMetricsAnalysis> {
friend AnalysisInfoMixin<MachineTraceMetricsAnalysis>;
static AnalysisKey Key;

public:
using Result = MachineTraceMetrics;
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
};

/// Verifier pass for \c MachineTraceMetrics.
struct MachineTraceMetricsVerifierPass
: PassInfoMixin<MachineTraceMetricsVerifierPass> {
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
static bool isRequired() { return true; }
};

class MachineTraceMetricsWrapperPass : public MachineFunctionPass {
public:
static char ID;
MachineTraceMetrics MTM;

MachineTraceMetricsWrapperPass();

void getAnalysisUsage(AnalysisUsage &) const override;
bool runOnMachineFunction(MachineFunction &) override;
void releaseMemory() override { MTM.clear(); }
void verifyAnalysis() const override { MTM.verifyAnalysis(); }
MachineTraceMetrics &getMTM() { return MTM; }
};

} // end namespace llvm

#endif // LLVM_CODEGEN_MACHINETRACEMETRICS_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void initializeMachineRegionInfoPassPass(PassRegistry &);
void initializeMachineSanitizerBinaryMetadataPass(PassRegistry &);
void initializeMachineSchedulerPass(PassRegistry &);
void initializeMachineSinkingPass(PassRegistry &);
void initializeMachineTraceMetricsPass(PassRegistry &);
void initializeMachineTraceMetricsWrapperPassPass(PassRegistry &);
void initializeMachineUniformityInfoPrinterPassPass(PassRegistry &);
void initializeMachineUniformityAnalysisPassPass(PassRegistry &);
void initializeMachineVerifierLegacyPassPass(PassRegistry &);
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ MACHINE_FUNCTION_ANALYSIS("machine-opt-remark-emitter",
MachineOptimizationRemarkEmitterAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
MachinePostDominatorTreeAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics", MachineTraceMetricsAnalysis())
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
Expand All @@ -119,8 +120,6 @@ MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
// MachinePostDominatorTreeAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-region-info",
// MachineRegionInfoPassAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics",
// MachineTraceMetricsAnalysis()) MACHINE_FUNCTION_ANALYSIS("reaching-def",
// ReachingDefAnalysisAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-reg-matrix",
// LiveRegMatrixAnalysis()) MACHINE_FUNCTION_ANALYSIS("gc-analysis",
// GCMachineCodeAnalysisPass())
Expand Down Expand Up @@ -156,6 +155,7 @@ MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
MACHINE_FUNCTION_PASS("two-address-instruction", TwoAddressInstructionPass())
MACHINE_FUNCTION_PASS("verify", MachineVerifierPass())
MACHINE_FUNCTION_PASS("verify<machine-trace-metrics>", MachineTraceMetricsVerifierPass())
#undef MACHINE_FUNCTION_PASS

#ifndef MACHINE_FUNCTION_PASS_WITH_PARAMS
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/CodeGen/EarlyIfConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE,
"Early If Converter", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE,
"Early If Converter", false, false)

Expand All @@ -802,8 +802,8 @@ void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<MachineDominatorTreeWrapperPass>();
AU.addRequired<MachineLoopInfoWrapperPass>();
AU.addPreserved<MachineLoopInfoWrapperPass>();
AU.addRequired<MachineTraceMetrics>();
AU.addPreserved<MachineTraceMetrics>();
AU.addRequired<MachineTraceMetricsWrapperPass>();
AU.addPreserved<MachineTraceMetricsWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}

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

bool Changed = false;
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/CodeGen/MachineCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ char &llvm::MachineCombinerID = MachineCombiner::ID;
INITIALIZE_PASS_BEGIN(MachineCombiner, DEBUG_TYPE,
"Machine InstCombiner", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
INITIALIZE_PASS_END(MachineCombiner, DEBUG_TYPE, "Machine InstCombiner",
false, false)

Expand All @@ -142,8 +142,8 @@ void MachineCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<MachineDominatorTreeWrapperPass>();
AU.addRequired<MachineLoopInfoWrapperPass>();
AU.addPreserved<MachineLoopInfoWrapperPass>();
AU.addRequired<MachineTraceMetrics>();
AU.addPreserved<MachineTraceMetrics>();
AU.addRequired<MachineTraceMetricsWrapperPass>();
AU.addPreserved<MachineTraceMetricsWrapperPass>();
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
AU.addRequired<ProfileSummaryInfoWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
Expand Down Expand Up @@ -727,7 +727,7 @@ bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
TSchedModel.init(STI);
MRI = &MF.getRegInfo();
MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
Traces = &getAnalysis<MachineTraceMetrics>();
Traces = &getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
MBFI = (PSI && PSI->hasProfileSummary()) ?
&getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
Expand Down
78 changes: 56 additions & 22 deletions llvm/lib/CodeGen/MachineTraceMetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,66 @@ using namespace llvm;

#define DEBUG_TYPE "machine-trace-metrics"

char MachineTraceMetrics::ID = 0;
AnalysisKey MachineTraceMetricsAnalysis::Key;

char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
MachineTraceMetricsAnalysis::Result
MachineTraceMetricsAnalysis::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
return Result(MF, MFAM.getResult<MachineLoopAnalysis>(MF));
}

PreservedAnalyses
MachineTraceMetricsVerifierPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
MFAM.getResult<MachineTraceMetricsAnalysis>(MF).verifyAnalysis();
return PreservedAnalyses::all();
}

INITIALIZE_PASS_BEGIN(MachineTraceMetrics, DEBUG_TYPE, "Machine Trace Metrics",
false, true)
char MachineTraceMetricsWrapperPass::ID = 0;

char &llvm::MachineTraceMetricsID = MachineTraceMetricsWrapperPass::ID;

INITIALIZE_PASS_BEGIN(MachineTraceMetricsWrapperPass, DEBUG_TYPE,
"Machine Trace Metrics", false, true)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
INITIALIZE_PASS_END(MachineTraceMetrics, DEBUG_TYPE,
INITIALIZE_PASS_END(MachineTraceMetricsWrapperPass, DEBUG_TYPE,
"Machine Trace Metrics", false, true)

MachineTraceMetrics::MachineTraceMetrics() : MachineFunctionPass(ID) {
std::fill(std::begin(Ensembles), std::end(Ensembles), nullptr);
}
MachineTraceMetricsWrapperPass::MachineTraceMetricsWrapperPass()
: MachineFunctionPass(ID) {}

void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
void MachineTraceMetricsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<MachineLoopInfoWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}

bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) {
void MachineTraceMetrics::init(MachineFunction &Func,
const MachineLoopInfo &LI) {
MF = &Func;
const TargetSubtargetInfo &ST = MF->getSubtarget();
TII = ST.getInstrInfo();
TRI = ST.getRegisterInfo();
MRI = &MF->getRegInfo();
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
Loops = &LI;
SchedModel.init(&ST);
BlockInfo.resize(MF->getNumBlockIDs());
ProcReleaseAtCycles.resize(MF->getNumBlockIDs() *
SchedModel.getNumProcResourceKinds());
}

bool MachineTraceMetricsWrapperPass::runOnMachineFunction(MachineFunction &MF) {
MTM.init(MF, getAnalysis<MachineLoopInfoWrapperPass>().getLI());
return false;
}

void MachineTraceMetrics::releaseMemory() {
MachineTraceMetrics::~MachineTraceMetrics() { clear(); }

void MachineTraceMetrics::clear() {
MF = nullptr;
BlockInfo.clear();
for (Ensemble *&E : Ensembles) {
delete E;
E = nullptr;
}
for (auto &E : Ensembles)
E.reset();
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -395,35 +414,50 @@ MachineTraceMetrics::Ensemble *
MachineTraceMetrics::getEnsemble(MachineTraceStrategy strategy) {
assert(strategy < MachineTraceStrategy::TS_NumStrategies &&
"Invalid trace strategy enum");
Ensemble *&E = Ensembles[static_cast<size_t>(strategy)];
std::unique_ptr<MachineTraceMetrics::Ensemble> &E =
Ensembles[static_cast<size_t>(strategy)];
if (E)
return E;
return E.get();

// Allocate new Ensemble on demand.
switch (strategy) {
case MachineTraceStrategy::TS_MinInstrCount:
return (E = new MinInstrCountEnsemble(this));
E = std::make_unique<MinInstrCountEnsemble>(MinInstrCountEnsemble(this));
break;
case MachineTraceStrategy::TS_Local:
return (E = new LocalEnsemble(this));
E = std::make_unique<LocalEnsemble>(LocalEnsemble(this));
break;
default: llvm_unreachable("Invalid trace strategy enum");
}
return E.get();
}

void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) {
LLVM_DEBUG(dbgs() << "Invalidate traces through " << printMBBReference(*MBB)
<< '\n');
BlockInfo[MBB->getNumber()].invalidate();
for (Ensemble *E : Ensembles)
for (auto &E : Ensembles)
if (E)
E->invalidate(MBB);
}

bool MachineTraceMetrics::invalidate(
MachineFunction &, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on machine functions, or the
// machine function's CFG have been preserved.
auto PAC = PA.getChecker<MachineTraceMetricsAnalysis>();
return !PAC.preserved() &&
!PAC.preservedSet<AllAnalysesOn<MachineFunction>>() &&
!PAC.preservedSet<CFGAnalyses>();
}

void MachineTraceMetrics::verifyAnalysis() const {
if (!MF)
return;
#ifndef NDEBUG
assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size");
for (Ensemble *E : Ensembles)
for (auto &E : Ensembles)
if (E)
E->verify();
#endif
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/MachinePostDominators.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/MachineTraceMetrics.h"
#include "llvm/CodeGen/MachineVerifier.h"
#include "llvm/CodeGen/PHIElimination.h"
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ INITIALIZE_PASS_BEGIN(AArch64ConditionalCompares, "aarch64-ccmp",
"AArch64 CCMP Pass", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetricsWrapperPass)
INITIALIZE_PASS_END(AArch64ConditionalCompares, "aarch64-ccmp",
"AArch64 CCMP Pass", false, false)

Expand All @@ -809,8 +809,8 @@ void AArch64ConditionalCompares::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<MachineDominatorTreeWrapperPass>();
AU.addRequired<MachineLoopInfoWrapperPass>();
AU.addPreserved<MachineLoopInfoWrapperPass>();
AU.addRequired<MachineTraceMetrics>();
AU.addPreserved<MachineTraceMetrics>();
AU.addRequired<MachineTraceMetricsWrapperPass>();
AU.addPreserved<MachineTraceMetricsWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}

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

Expand Down
Loading
Loading