Skip to content

Commit 875952e

Browse files
optimisanvar-const
authored andcommitted
[CodeGen][NPM] Port MachineBlockPlacementStats to NPM (#129853)
1 parent 5cbad3a commit 875952e

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

llvm/include/llvm/CodeGen/MachineBlockPlacement.h

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class MachineBlockPlacementPass
3030
function_ref<StringRef(StringRef)> MapClassName2PassName) const;
3131
};
3232

33+
class MachineBlockPlacementStatsPass
34+
: public PassInfoMixin<MachineBlockPlacementStatsPass> {
35+
36+
public:
37+
PreservedAnalyses run(MachineFunction &MF,
38+
MachineFunctionAnalysisManager &MFAM);
39+
static bool isRequired() { return true; }
40+
};
41+
3342
} // namespace llvm
3443

3544
#endif // LLVM_CODEGEN_MACHINEBLOCKPLACEMENT_H

llvm/include/llvm/InitializePasses.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ void initializeMIRNamerPass(PassRegistry &);
185185
void initializeMIRPrintingPassPass(PassRegistry &);
186186
void initializeMachineBlockFrequencyInfoWrapperPassPass(PassRegistry &);
187187
void initializeMachineBlockPlacementLegacyPass(PassRegistry &);
188-
void initializeMachineBlockPlacementStatsPass(PassRegistry &);
188+
void initializeMachineBlockPlacementStatsLegacyPass(PassRegistry &);
189189
void initializeMachineBranchProbabilityInfoWrapperPassPass(PassRegistry &);
190190
void initializeMachineCFGPrinterPass(PassRegistry &);
191191
void initializeMachineCSELegacyPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
137137
#ifndef MACHINE_FUNCTION_PASS
138138
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
139139
#endif
140+
MACHINE_FUNCTION_PASS("block-placement-stats", MachineBlockPlacementStatsPass())
140141
MACHINE_FUNCTION_PASS("branch-relaxation", BranchRelaxationPass())
141142
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
142143
MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass())
@@ -281,7 +282,6 @@ DUMMY_MACHINE_MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass)
281282
#endif
282283
DUMMY_MACHINE_FUNCTION_PASS("bbsections-prepare", BasicBlockSectionsPass)
283284
DUMMY_MACHINE_FUNCTION_PASS("bbsections-profile-reader", BasicBlockSectionsProfileReaderPass)
284-
DUMMY_MACHINE_FUNCTION_PASS("block-placement-stats", MachineBlockPlacementStatsPass)
285285
DUMMY_MACHINE_FUNCTION_PASS("break-false-deps", BreakFalseDepsPass)
286286
DUMMY_MACHINE_FUNCTION_PASS("cfguard-longjmp", CFGuardLongjmpPass)
287287
DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)

llvm/lib/CodeGen/CodeGen.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
7373
initializeMIRProfileLoaderPassPass(Registry);
7474
initializeMachineBlockFrequencyInfoWrapperPassPass(Registry);
7575
initializeMachineBlockPlacementLegacyPass(Registry);
76-
initializeMachineBlockPlacementStatsPass(Registry);
76+
initializeMachineBlockPlacementStatsLegacyPass(Registry);
7777
initializeMachineCFGPrinterPass(Registry);
7878
initializeMachineCSELegacyPass(Registry);
7979
initializeMachineCombinerPass(Registry);

llvm/lib/CodeGen/MachineBlockPlacement.cpp

+33-12
Original file line numberDiff line numberDiff line change
@@ -3839,21 +3839,35 @@ namespace {
38393839
/// placement. This is separate from the actual placement pass so that they can
38403840
/// be computed in the absence of any placement transformations or when using
38413841
/// alternative placement strategies.
3842-
class MachineBlockPlacementStats : public MachineFunctionPass {
3842+
class MachineBlockPlacementStats {
38433843
/// A handle to the branch probability pass.
38443844
const MachineBranchProbabilityInfo *MBPI;
38453845

38463846
/// A handle to the function-wide block frequency pass.
38473847
const MachineBlockFrequencyInfo *MBFI;
38483848

3849+
public:
3850+
MachineBlockPlacementStats(const MachineBranchProbabilityInfo *MBPI,
3851+
const MachineBlockFrequencyInfo *MBFI)
3852+
: MBPI(MBPI), MBFI(MBFI) {}
3853+
bool run(MachineFunction &MF);
3854+
};
3855+
3856+
class MachineBlockPlacementStatsLegacy : public MachineFunctionPass {
38493857
public:
38503858
static char ID; // Pass identification, replacement for typeid
38513859

3852-
MachineBlockPlacementStats() : MachineFunctionPass(ID) {
3853-
initializeMachineBlockPlacementStatsPass(*PassRegistry::getPassRegistry());
3860+
MachineBlockPlacementStatsLegacy() : MachineFunctionPass(ID) {
3861+
initializeMachineBlockPlacementStatsLegacyPass(
3862+
*PassRegistry::getPassRegistry());
38543863
}
38553864

3856-
bool runOnMachineFunction(MachineFunction &F) override;
3865+
bool runOnMachineFunction(MachineFunction &F) override {
3866+
auto *MBPI =
3867+
&getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
3868+
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
3869+
return MachineBlockPlacementStats(MBPI, MBFI).run(F);
3870+
}
38573871

38583872
void getAnalysisUsage(AnalysisUsage &AU) const override {
38593873
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
@@ -3865,28 +3879,35 @@ class MachineBlockPlacementStats : public MachineFunctionPass {
38653879

38663880
} // end anonymous namespace
38673881

3868-
char MachineBlockPlacementStats::ID = 0;
3882+
char MachineBlockPlacementStatsLegacy::ID = 0;
38693883

3870-
char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStats::ID;
3884+
char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStatsLegacy::ID;
38713885

3872-
INITIALIZE_PASS_BEGIN(MachineBlockPlacementStats, "block-placement-stats",
3886+
INITIALIZE_PASS_BEGIN(MachineBlockPlacementStatsLegacy, "block-placement-stats",
38733887
"Basic Block Placement Stats", false, false)
38743888
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
38753889
INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
3876-
INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats",
3890+
INITIALIZE_PASS_END(MachineBlockPlacementStatsLegacy, "block-placement-stats",
38773891
"Basic Block Placement Stats", false, false)
38783892

3879-
bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) {
3893+
PreservedAnalyses
3894+
MachineBlockPlacementStatsPass::run(MachineFunction &MF,
3895+
MachineFunctionAnalysisManager &MFAM) {
3896+
auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
3897+
auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
3898+
3899+
MachineBlockPlacementStats(&MBPI, &MBFI).run(MF);
3900+
return PreservedAnalyses::all();
3901+
}
3902+
3903+
bool MachineBlockPlacementStats::run(MachineFunction &F) {
38803904
// Check for single-block functions and skip them.
38813905
if (std::next(F.begin()) == F.end())
38823906
return false;
38833907

38843908
if (!isFunctionInPrintList(F.getName()))
38853909
return false;
38863910

3887-
MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
3888-
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
3889-
38903911
for (MachineBasicBlock &MBB : F) {
38913912
BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB);
38923913
Statistic &NumBranches =

0 commit comments

Comments
 (0)