Skip to content

[CodeGen][NPM] Update BranchFolderLegacy make tail merge configurable via flag #135277

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/BranchFoldingPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class BranchFolderPass : public PassInfoMixin<BranchFolderPass> {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoPHIs);
}

void printPipeline(raw_ostream &OS,
function_ref<StringRef(StringRef)> MapClassName2PassName);
};

} // namespace llvm
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ namespace llvm {
/// branches.
extern char &BranchFolderPassID;

MachineFunctionPass *createBranchFolderPass(bool EnableTailMerge);

/// BranchRelaxation - This pass replaces branches that need to jump further
/// than is supported by a branch instruction.
extern char &BranchRelaxationPassID;
Expand Down
26 changes: 15 additions & 11 deletions llvm/lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "llvm/CodeGen/MachineSizeOpts.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
Expand Down Expand Up @@ -90,18 +89,20 @@ namespace {

/// BranchFolderPass - Wrap branch folder in a machine function pass.
class BranchFolderLegacy : public MachineFunctionPass {
bool EnableTailMerge;

public:
static char ID;

explicit BranchFolderLegacy() : MachineFunctionPass(ID) {}
explicit BranchFolderLegacy(bool EnableTailMerge = true)
: MachineFunctionPass(ID), EnableTailMerge(EnableTailMerge) {}
Copy link
Member Author

Choose a reason for hiding this comment

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

That added the initial port to the new pm. This is now changing the pass arguments in the old PM, without the matching new PM change

Are you talking about the default argument? I see that BranchFolderPass already takes a bool EnableTailMerge:

BranchFolderPass(bool EnableTailMerge) : EnableTailMerge(EnableTailMerge) {}


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);
}

Expand All @@ -123,9 +124,6 @@ INITIALIZE_PASS(BranchFolderLegacy, DEBUG_TYPE, "Control Flow Optimizer", false,
PreservedAnalyses BranchFolderPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
MFPropsModifier _(*this, MF);
bool EnableTailMerge =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you're removing it here, the NPM require the flag requiresStructuredCFG() to be incorporated here.

https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Passes/CodeGenPassBuilder.h#L1235

!MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge;

auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
.getCachedResult<ProfileSummaryAnalysis>(
Expand All @@ -144,15 +142,17 @@ PreservedAnalyses BranchFolderPass::run(MachineFunction &MF,
return getMachineFunctionPassPreservedAnalyses();
}

void BranchFolderPass::printPipeline(
raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
OS << MapClassName2PassName(name());
if (EnableTailMerge)
OS << "<enable-tail-merge>";
}

bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;

TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
// TailMerge can create jump into if branches that make CFG irreducible for
// HW that requires structurized CFG.
bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
PassConfig->getEnableTailMerge();
MBFIWrapper MBBFreqInfo(
getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
BranchFolder Folder(
Expand Down Expand Up @@ -2080,3 +2080,7 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
++NumHoist;
return true;
}

MachineFunctionPass *llvm::createBranchFolderPass(bool EnableTailMerge = true) {
return new BranchFolderLegacy(EnableTailMerge);
}
4 changes: 3 additions & 1 deletion llvm/lib/CodeGen/TargetPassConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,9 @@ void TargetPassConfig::addMachineLateOptimization() {
addPass(&MachineLateInstrsCleanupID);

// Branch folding must be run after regalloc and prolog/epilog insertion.
addPass(&BranchFolderPassID);
if (!isPassSubstitutedOrOverridden(&BranchFolderPassID))
addPass(createBranchFolderPass(!TM->requiresStructuredCFG() &&
getEnableTailMerge()));

// Tail duplication.
// Note that duplicating tail just increases code size and degrades
Expand Down
Loading