diff options
author | Akshat Oke <Akshat.Oke@amd.com> | 2025-03-13 13:41:28 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-13 13:41:28 +0530 |
commit | 5952972c9164216be7c22292c52c131f1d0a8030 (patch) | |
tree | bfc9c7bf76484dbc63a91ae869521fbb04af2f25 /llvm/lib/CodeGen | |
parent | 43ab4228d00abcb08b5d75d154deeca9c42ca795 (diff) | |
download | llvm-5952972c9164216be7c22292c52c131f1d0a8030.zip llvm-5952972c9164216be7c22292c52c131f1d0a8030.tar.gz llvm-5952972c9164216be7c22292c52c131f1d0a8030.tar.bz2 |
[CodeGen][NPM] Port BranchFolder to NPM (#128858)
EnableTailMerge is false by default and is handled by the pass builder.
Passes are independent of target pipeline options.
This completes the generic `MachineLateOptimization` passes for the NPM
pipeline.
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/BranchFolding.cpp | 73 | ||||
-rw-r--r-- | llvm/lib/CodeGen/CodeGen.cpp | 2 |
2 files changed, 50 insertions, 25 deletions
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp index 5218e39..6f5afbd 100644 --- a/llvm/lib/CodeGen/BranchFolding.cpp +++ b/llvm/lib/CodeGen/BranchFolding.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/CodeGen/Analysis.h" +#include "llvm/CodeGen/BranchFoldingPass.h" #include "llvm/CodeGen/MBFIWrapper.h" #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" @@ -88,38 +89,62 @@ TailMergeSize("tail-merge-size", namespace { /// BranchFolderPass - Wrap branch folder in a machine function pass. - class BranchFolderPass : public MachineFunctionPass { - public: - static char ID; +class BranchFolderLegacy : public MachineFunctionPass { +public: + static char ID; - explicit BranchFolderPass(): MachineFunctionPass(ID) {} + explicit BranchFolderLegacy() : MachineFunctionPass(ID) {} - bool runOnMachineFunction(MachineFunction &MF) override; + bool runOnMachineFunction(MachineFunction &MF) override; - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); - AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); - AU.addRequired<ProfileSummaryInfoWrapperPass>(); - AU.addRequired<TargetPassConfig>(); - MachineFunctionPass::getAnalysisUsage(AU); - } + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); + AU.addRequired<ProfileSummaryInfoWrapperPass>(); + AU.addRequired<TargetPassConfig>(); + MachineFunctionPass::getAnalysisUsage(AU); + } - MachineFunctionProperties getRequiredProperties() const override { - return MachineFunctionProperties().set( - MachineFunctionProperties::Property::NoPHIs); - } - }; + MachineFunctionProperties getRequiredProperties() const override { + return MachineFunctionProperties().set( + MachineFunctionProperties::Property::NoPHIs); + } +}; } // end anonymous namespace -char BranchFolderPass::ID = 0; - -char &llvm::BranchFolderPassID = BranchFolderPass::ID; - -INITIALIZE_PASS(BranchFolderPass, DEBUG_TYPE, - "Control Flow Optimizer", false, false) +char BranchFolderLegacy::ID = 0; + +char &llvm::BranchFolderPassID = BranchFolderLegacy::ID; + +INITIALIZE_PASS(BranchFolderLegacy, DEBUG_TYPE, "Control Flow Optimizer", false, + false) + +PreservedAnalyses BranchFolderPass::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + MFPropsModifier _(*this, MF); + bool EnableTailMerge = + !MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge; + + auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF); + auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF) + .getCachedResult<ProfileSummaryAnalysis>( + *MF.getFunction().getParent()); + if (!PSI) + report_fatal_error( + "ProfileSummaryAnalysis is required for BranchFoldingPass", false); + + auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(MF); + MBFIWrapper MBBFreqInfo(MBFI); + BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, MBPI, + PSI); + if (!Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(), + MF.getSubtarget().getRegisterInfo())) + return PreservedAnalyses::all(); + return getMachineFunctionPassPreservedAnalyses(); +} -bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) { +bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) { if (skipFunction(MF.getFunction())) return false; diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp index b36d2f7..8d108c2 100644 --- a/llvm/lib/CodeGen/CodeGen.cpp +++ b/llvm/lib/CodeGen/CodeGen.cpp @@ -22,7 +22,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) { initializeAtomicExpandLegacyPass(Registry); initializeBasicBlockPathCloningPass(Registry); initializeBasicBlockSectionsPass(Registry); - initializeBranchFolderPassPass(Registry); + initializeBranchFolderLegacyPass(Registry); initializeBranchRelaxationPass(Registry); initializeBreakFalseDepsPass(Registry); initializeCallBrPreparePass(Registry); |