Skip to content

[CodeGen][NPM] Port MachineBlockPlacementStats to NPM #129853

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 2 commits into from
Apr 17, 2025
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
9 changes: 9 additions & 0 deletions llvm/include/llvm/CodeGen/MachineBlockPlacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class MachineBlockPlacementPass
function_ref<StringRef(StringRef)> MapClassName2PassName) const;
};

class MachineBlockPlacementStatsPass
: public PassInfoMixin<MachineBlockPlacementStatsPass> {

public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
static bool isRequired() { return true; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this should not be required for analyse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This keeps it consistent with the legacy pass not skipping optnone functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pass just updates statistics? It certainly shouldn't be considered required. It pre-checks isFunctionInPrintList. Should the PM be directly managing this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pass instrumentation checks for isFunctionInPrintList but it is independent of the Statistic object.
optnone is also separate from isFunctionInPrintList, so I think this should be not skipped

};

} // namespace llvm

#endif // LLVM_CODEGEN_MACHINEBLOCKPLACEMENT_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void initializeMIRNamerPass(PassRegistry &);
void initializeMIRPrintingPassPass(PassRegistry &);
void initializeMachineBlockFrequencyInfoWrapperPassPass(PassRegistry &);
void initializeMachineBlockPlacementLegacyPass(PassRegistry &);
void initializeMachineBlockPlacementStatsPass(PassRegistry &);
void initializeMachineBlockPlacementStatsLegacyPass(PassRegistry &);
void initializeMachineBranchProbabilityInfoWrapperPassPass(PassRegistry &);
void initializeMachineCFGPrinterPass(PassRegistry &);
void initializeMachineCSELegacyPass(PassRegistry &);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
#ifndef MACHINE_FUNCTION_PASS
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
#endif
MACHINE_FUNCTION_PASS("block-placement-stats", MachineBlockPlacementStatsPass())
MACHINE_FUNCTION_PASS("branch-relaxation", BranchRelaxationPass())
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass())
Expand Down Expand Up @@ -281,7 +282,6 @@ DUMMY_MACHINE_MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass)
#endif
DUMMY_MACHINE_FUNCTION_PASS("bbsections-prepare", BasicBlockSectionsPass)
DUMMY_MACHINE_FUNCTION_PASS("bbsections-profile-reader", BasicBlockSectionsProfileReaderPass)
DUMMY_MACHINE_FUNCTION_PASS("block-placement-stats", MachineBlockPlacementStatsPass)
DUMMY_MACHINE_FUNCTION_PASS("break-false-deps", BreakFalseDepsPass)
DUMMY_MACHINE_FUNCTION_PASS("cfguard-longjmp", CFGuardLongjmpPass)
DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeMIRProfileLoaderPassPass(Registry);
initializeMachineBlockFrequencyInfoWrapperPassPass(Registry);
initializeMachineBlockPlacementLegacyPass(Registry);
initializeMachineBlockPlacementStatsPass(Registry);
initializeMachineBlockPlacementStatsLegacyPass(Registry);
initializeMachineCFGPrinterPass(Registry);
initializeMachineCSELegacyPass(Registry);
initializeMachineCombinerPass(Registry);
Expand Down
45 changes: 33 additions & 12 deletions llvm/lib/CodeGen/MachineBlockPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3839,21 +3839,35 @@ namespace {
/// placement. This is separate from the actual placement pass so that they can
/// be computed in the absence of any placement transformations or when using
/// alternative placement strategies.
class MachineBlockPlacementStats : public MachineFunctionPass {
class MachineBlockPlacementStats {
/// A handle to the branch probability pass.
const MachineBranchProbabilityInfo *MBPI;

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

public:
MachineBlockPlacementStats(const MachineBranchProbabilityInfo *MBPI,
const MachineBlockFrequencyInfo *MBFI)
: MBPI(MBPI), MBFI(MBFI) {}
bool run(MachineFunction &MF);
};

class MachineBlockPlacementStatsLegacy : public MachineFunctionPass {
public:
static char ID; // Pass identification, replacement for typeid

MachineBlockPlacementStats() : MachineFunctionPass(ID) {
initializeMachineBlockPlacementStatsPass(*PassRegistry::getPassRegistry());
MachineBlockPlacementStatsLegacy() : MachineFunctionPass(ID) {
initializeMachineBlockPlacementStatsLegacyPass(
*PassRegistry::getPassRegistry());
}

bool runOnMachineFunction(MachineFunction &F) override;
bool runOnMachineFunction(MachineFunction &F) override {
auto *MBPI =
&getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
return MachineBlockPlacementStats(MBPI, MBFI).run(F);
}

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

} // end anonymous namespace

char MachineBlockPlacementStats::ID = 0;
char MachineBlockPlacementStatsLegacy::ID = 0;

char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStats::ID;
char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStatsLegacy::ID;

INITIALIZE_PASS_BEGIN(MachineBlockPlacementStats, "block-placement-stats",
INITIALIZE_PASS_BEGIN(MachineBlockPlacementStatsLegacy, "block-placement-stats",
"Basic Block Placement Stats", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats",
INITIALIZE_PASS_END(MachineBlockPlacementStatsLegacy, "block-placement-stats",
"Basic Block Placement Stats", false, false)

bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) {
PreservedAnalyses
MachineBlockPlacementStatsPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);

MachineBlockPlacementStats(&MBPI, &MBFI).run(MF);
return PreservedAnalyses::all();
}

bool MachineBlockPlacementStats::run(MachineFunction &F) {
// Check for single-block functions and skip them.
if (std::next(F.begin()) == F.end())
return false;

if (!isFunctionInPrintList(F.getName()))
return false;

MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();

for (MachineBasicBlock &MBB : F) {
BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB);
Statistic &NumBranches =
Expand Down
Loading