diff options
author | Akshat Oke <Akshat.Oke@amd.com> | 2025-03-14 10:31:53 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-14 10:31:53 +0530 |
commit | 87916f8c32ebd8e284091db9b70339df57fd1e90 (patch) | |
tree | dbc7defc6dfa315691b3ad3767e3be7cf9806c69 /llvm/lib/CodeGen/MachineBlockPlacement.cpp | |
parent | 42748a454fc9d9a42504e8290234aaf9407437ce (diff) | |
download | llvm-87916f8c32ebd8e284091db9b70339df57fd1e90.zip llvm-87916f8c32ebd8e284091db9b70339df57fd1e90.tar.gz llvm-87916f8c32ebd8e284091db9b70339df57fd1e90.tar.bz2 |
[CodeGen][NPM] Port MachineBlockPlacement to NPM (#129828)
Diffstat (limited to 'llvm/lib/CodeGen/MachineBlockPlacement.cpp')
-rw-r--r-- | llvm/lib/CodeGen/MachineBlockPlacement.cpp | 127 |
1 files changed, 94 insertions, 33 deletions
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp index 9ccfadc..40edc47 100644 --- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -24,6 +24,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/CodeGen/MachineBlockPlacement.h" #include "BranchFolding.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" @@ -357,7 +358,7 @@ public: unsigned UnscheduledPredecessors = 0; }; -class MachineBlockPlacement : public MachineFunctionPass { +class MachineBlockPlacement { /// A type for a block filter set. using BlockFilterSet = SmallSetVector<const MachineBasicBlock *, 16>; @@ -409,7 +410,11 @@ class MachineBlockPlacement : public MachineFunctionPass { ProfileSummaryInfo *PSI = nullptr; - TargetPassConfig *PassConfig = nullptr; + // Tail merging is also determined based on + // whether structured CFG is required. + bool AllowTailMerge; + + CodeGenOptLevel OptLevel; /// Duplicator used to duplicate tails during placement. /// @@ -609,17 +614,47 @@ class MachineBlockPlacement : public MachineFunctionPass { void createCFGChainExtTsp(); public: + MachineBlockPlacement(const MachineBranchProbabilityInfo *MBPI, + MachineLoopInfo *MLI, ProfileSummaryInfo *PSI, + std::unique_ptr<MBFIWrapper> MBFI, + MachinePostDominatorTree *MPDT, bool AllowTailMerge) + : MBPI(MBPI), MBFI(std::move(MBFI)), MLI(MLI), MPDT(MPDT), PSI(PSI), + AllowTailMerge(AllowTailMerge) {}; + + bool run(MachineFunction &F); + + static bool allowTailDupPlacement(MachineFunction &MF) { + return TailDupPlacement && !MF.getTarget().requiresStructuredCFG(); + } +}; + +class MachineBlockPlacementLegacy : public MachineFunctionPass { +public: static char ID; // Pass identification, replacement for typeid - MachineBlockPlacement() : MachineFunctionPass(ID) { - initializeMachineBlockPlacementPass(*PassRegistry::getPassRegistry()); + MachineBlockPlacementLegacy() : MachineFunctionPass(ID) { + initializeMachineBlockPlacementLegacyPass(*PassRegistry::getPassRegistry()); } - bool runOnMachineFunction(MachineFunction &F) override; + bool runOnMachineFunction(MachineFunction &MF) override { + if (skipFunction(MF.getFunction())) + return false; - bool allowTailDupPlacement() const { - assert(F); - return TailDupPlacement && !F->getTarget().requiresStructuredCFG(); + auto *MBPI = + &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); + auto MBFI = std::make_unique<MBFIWrapper>( + getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI()); + auto *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI(); + auto *MPDT = MachineBlockPlacement::allowTailDupPlacement(MF) + ? &getAnalysis<MachinePostDominatorTreeWrapperPass>() + .getPostDomTree() + : nullptr; + auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); + auto *PassConfig = &getAnalysis<TargetPassConfig>(); + bool AllowTailMerge = PassConfig->getEnableTailMerge(); + return MachineBlockPlacement(MBPI, MLI, PSI, std::move(MBFI), MPDT, + AllowTailMerge) + .run(MF); } void getAnalysisUsage(AnalysisUsage &AU) const override { @@ -636,18 +671,18 @@ public: } // end anonymous namespace -char MachineBlockPlacement::ID = 0; +char MachineBlockPlacementLegacy::ID = 0; -char &llvm::MachineBlockPlacementID = MachineBlockPlacement::ID; +char &llvm::MachineBlockPlacementID = MachineBlockPlacementLegacy::ID; -INITIALIZE_PASS_BEGIN(MachineBlockPlacement, DEBUG_TYPE, +INITIALIZE_PASS_BEGIN(MachineBlockPlacementLegacy, DEBUG_TYPE, "Branch Probability Basic Block Placement", false, false) INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) -INITIALIZE_PASS_END(MachineBlockPlacement, DEBUG_TYPE, +INITIALIZE_PASS_END(MachineBlockPlacementLegacy, DEBUG_TYPE, "Branch Probability Basic Block Placement", false, false) #ifndef NDEBUG @@ -1130,7 +1165,7 @@ MachineBlockPlacement::getBestTrellisSuccessor( MachineBasicBlock *Succ1 = BestA.Dest; MachineBasicBlock *Succ2 = BestB.Dest; // Check to see if tail-duplication would be profitable. - if (allowTailDupPlacement() && shouldTailDuplicate(Succ2) && + if (allowTailDupPlacement(*F) && shouldTailDuplicate(Succ2) && canTailDuplicateUnplacedPreds(BB, Succ2, Chain, BlockFilter) && isProfitableToTailDup(BB, Succ2, MBPI->getEdgeProbability(BB, Succ1), Chain, BlockFilter)) { @@ -1655,7 +1690,7 @@ MachineBlockPlacement::selectBestSuccessor(const MachineBasicBlock *BB, if (hasBetterLayoutPredecessor(BB, Succ, SuccChain, SuccProb, RealSuccProb, Chain, BlockFilter)) { // If tail duplication would make Succ profitable, place it. - if (allowTailDupPlacement() && shouldTailDuplicate(Succ)) + if (allowTailDupPlacement(*F) && shouldTailDuplicate(Succ)) DupCandidates.emplace_back(SuccProb, Succ); continue; } @@ -1883,7 +1918,7 @@ void MachineBlockPlacement::buildChain(const MachineBasicBlock *HeadBB, auto Result = selectBestSuccessor(BB, Chain, BlockFilter); MachineBasicBlock *BestSucc = Result.BB; bool ShouldTailDup = Result.ShouldTailDup; - if (allowTailDupPlacement()) + if (allowTailDupPlacement(*F)) ShouldTailDup |= (BestSucc && canTailDuplicateUnplacedPreds( BB, BestSucc, Chain, BlockFilter)); @@ -1910,7 +1945,7 @@ void MachineBlockPlacement::buildChain(const MachineBasicBlock *HeadBB, // Placement may have changed tail duplication opportunities. // Check for that now. - if (allowTailDupPlacement() && BestSucc && ShouldTailDup) { + if (allowTailDupPlacement(*F) && BestSucc && ShouldTailDup) { repeatedlyTailDuplicateBlock(BestSucc, BB, LoopHeaderBB, Chain, BlockFilter, PrevUnplacedBlockIt, PrevUnplacedBlockInFilterIt); @@ -3466,7 +3501,7 @@ void MachineBlockPlacement::initTailDupThreshold() { // For aggressive optimization, we can adjust some thresholds to be less // conservative. - if (PassConfig->getOptLevel() >= CodeGenOptLevel::Aggressive) { + if (OptLevel >= CodeGenOptLevel::Aggressive) { // At O3 we should be more willing to copy blocks for tail duplication. This // increases size pressure, so we only do it at O3 // Do this unless only the regular threshold is explicitly set. @@ -3478,29 +3513,56 @@ void MachineBlockPlacement::initTailDupThreshold() { // If there's no threshold provided through options, query the target // information for a threshold instead. if (TailDupPlacementThreshold.getNumOccurrences() == 0 && - (PassConfig->getOptLevel() < CodeGenOptLevel::Aggressive || + (OptLevel < CodeGenOptLevel::Aggressive || TailDupPlacementAggressiveThreshold.getNumOccurrences() == 0)) - TailDupSize = TII->getTailDuplicateSize(PassConfig->getOptLevel()); + TailDupSize = TII->getTailDuplicateSize(OptLevel); } -bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) { - if (skipFunction(MF.getFunction())) - return false; +PreservedAnalyses +MachineBlockPlacementPass::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF); + auto MBFI = std::make_unique<MBFIWrapper>( + MFAM.getResult<MachineBlockFrequencyAnalysis>(MF)); + auto *MLI = &MFAM.getResult<MachineLoopAnalysis>(MF); + auto *MPDT = MachineBlockPlacement::allowTailDupPlacement(MF) + ? &MFAM.getResult<MachinePostDominatorTreeAnalysis>(MF) + : nullptr; + auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF) + .getCachedResult<ProfileSummaryAnalysis>( + *MF.getFunction().getParent()); + if (!PSI) + report_fatal_error("MachineBlockPlacement requires ProfileSummaryAnalysis", + false); + + MachineBlockPlacement MBP(MBPI, MLI, PSI, std::move(MBFI), MPDT, + AllowTailMerge); + + if (!MBP.run(MF)) + return PreservedAnalyses::all(); + + return getMachineFunctionPassPreservedAnalyses(); +} + +void MachineBlockPlacementPass::printPipeline( + raw_ostream &OS, + function_ref<StringRef(StringRef)> MapClassName2PassName) const { + OS << MapClassName2PassName(name()); + if (!AllowTailMerge) + OS << "<no-tail-merge>"; +} + +bool MachineBlockPlacement::run(MachineFunction &MF) { // Check for single-block functions and skip them. if (std::next(MF.begin()) == MF.end()) return false; F = &MF; - MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); - MBFI = std::make_unique<MBFIWrapper>( - getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI()); - MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI(); + OptLevel = F->getTarget().getOptLevel(); + TII = MF.getSubtarget().getInstrInfo(); TLI = MF.getSubtarget().getTargetLowering(); - MPDT = nullptr; - PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); - PassConfig = &getAnalysis<TargetPassConfig>(); // Initialize PreferredLoopExit to nullptr here since it may never be set if // there are no MachineLoops. @@ -3529,8 +3591,7 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) { } // Apply tail duplication. - if (allowTailDupPlacement()) { - MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree(); + if (allowTailDupPlacement(*F)) { if (OptForSize) TailDupSize = 1; const bool PreRegAlloc = false; @@ -3548,8 +3609,8 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) { // TailMerge can create jump into if branches that make CFG irreducible for // HW that requires structured CFG. const bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() && - PassConfig->getEnableTailMerge() && - BranchFoldPlacement && MF.size() > 3; + AllowTailMerge && BranchFoldPlacement && + MF.size() > 3; // No tail merging opportunities if the block number is less than four. if (EnableTailMerge) { const unsigned TailMergeSize = TailDupSize + 1; |