Skip to content

Commit 5952972

Browse files
authored
[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.
1 parent 43ab422 commit 5952972

13 files changed

+103
-30
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- llvm/CodeGen/BranchFoldingPass.h -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_CODEGEN_BRANCHFOLDINGPASS_H
9+
#define LLVM_CODEGEN_BRANCHFOLDINGPASS_H
10+
11+
#include "llvm/CodeGen/MachinePassManager.h"
12+
13+
namespace llvm {
14+
15+
class BranchFolderPass : public PassInfoMixin<BranchFolderPass> {
16+
bool EnableTailMerge;
17+
18+
public:
19+
BranchFolderPass(bool EnableTailMerge) : EnableTailMerge(EnableTailMerge) {}
20+
PreservedAnalyses run(MachineFunction &MF,
21+
MachineFunctionAnalysisManager &MFAM);
22+
23+
MachineFunctionProperties getRequiredProperties() const {
24+
return MachineFunctionProperties().set(
25+
MachineFunctionProperties::Property::NoPHIs);
26+
}
27+
};
28+
29+
} // namespace llvm
30+
31+
#endif // LLVM_CODEGEN_BRANCHFOLDINGPASS_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void initializeBasicBlockSectionsPass(PassRegistry &);
5959
void initializeBarrierNoopPass(PassRegistry &);
6060
void initializeBasicAAWrapperPassPass(PassRegistry &);
6161
void initializeBlockFrequencyInfoWrapperPassPass(PassRegistry &);
62-
void initializeBranchFolderPassPass(PassRegistry &);
62+
void initializeBranchFolderLegacyPass(PassRegistry &);
6363
void initializeBranchProbabilityInfoWrapperPassPass(PassRegistry &);
6464
void initializeBranchRelaxationPass(PassRegistry &);
6565
void initializeBreakCriticalEdgesPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Analysis/TargetTransformInfo.h"
2424
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
2525
#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
26+
#include "llvm/CodeGen/BranchFoldingPass.h"
2627
#include "llvm/CodeGen/CallBrPrepare.h"
2728
#include "llvm/CodeGen/CodeGenPrepare.h"
2829
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
@@ -1205,7 +1206,7 @@ template <typename Derived, typename TargetMachineT>
12051206
void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineLateOptimization(
12061207
AddMachinePass &addPass) const {
12071208
// Branch folding must be run after regalloc and prolog/epilog insertion.
1208-
addPass(BranchFolderPass());
1209+
addPass(BranchFolderPass(Opt.EnableTailMerge));
12091210

12101211
// Tail duplication.
12111212
// Note that duplicating tail just increases code size and degrades

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ MACHINE_FUNCTION_PASS("verify<machine-trace-metrics>", MachineTraceMetricsVerifi
195195
#define MACHINE_FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, \
196196
PARAMS)
197197
#endif
198+
MACHINE_FUNCTION_PASS_WITH_PARAMS(
199+
"branch-folder", "BranchFolderPass",
200+
[](bool EnableTailMerge) { return BranchFolderPass(EnableTailMerge); },
201+
[](StringRef Params) {
202+
return parseSinglePassOption(Params, "enable-tail-merge",
203+
"BranchFolderPass");
204+
},
205+
"enable-tail-merge")
206+
198207
MACHINE_FUNCTION_PASS_WITH_PARAMS(
199208
"machine-sink", "MachineSinkingPass",
200209
[](bool EnableSinkAndFold) {
@@ -246,7 +255,6 @@ DUMMY_MACHINE_FUNCTION_PASS("bbsections-prepare", BasicBlockSectionsPass)
246255
DUMMY_MACHINE_FUNCTION_PASS("bbsections-profile-reader", BasicBlockSectionsProfileReaderPass)
247256
DUMMY_MACHINE_FUNCTION_PASS("block-placement", MachineBlockPlacementPass)
248257
DUMMY_MACHINE_FUNCTION_PASS("block-placement-stats", MachineBlockPlacementStatsPass)
249-
DUMMY_MACHINE_FUNCTION_PASS("branch-folder", BranchFolderPass)
250258
DUMMY_MACHINE_FUNCTION_PASS("break-false-deps", BreakFalseDepsPass)
251259
DUMMY_MACHINE_FUNCTION_PASS("cfguard-longjmp", CFGuardLongjmpPass)
252260
DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)

llvm/include/llvm/Target/CGPassBuilderOption.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct CGPassBuilderOption {
5050
bool EnableGlobalMergeFunc = false;
5151
bool EnableMachineFunctionSplitter = false;
5252
bool EnableSinkAndFold = false;
53+
bool EnableTailMerge = true;
5354
bool MISchedPostRA = false;
5455
bool EarlyLiveIntervals = false;
5556
bool GCEmptyBlocks = false;

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/Statistic.h"
2525
#include "llvm/Analysis/ProfileSummaryInfo.h"
2626
#include "llvm/CodeGen/Analysis.h"
27+
#include "llvm/CodeGen/BranchFoldingPass.h"
2728
#include "llvm/CodeGen/MBFIWrapper.h"
2829
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
2930
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
@@ -88,38 +89,62 @@ TailMergeSize("tail-merge-size",
8889
namespace {
8990

9091
/// BranchFolderPass - Wrap branch folder in a machine function pass.
91-
class BranchFolderPass : public MachineFunctionPass {
92-
public:
93-
static char ID;
92+
class BranchFolderLegacy : public MachineFunctionPass {
93+
public:
94+
static char ID;
9495

95-
explicit BranchFolderPass(): MachineFunctionPass(ID) {}
96+
explicit BranchFolderLegacy() : MachineFunctionPass(ID) {}
9697

97-
bool runOnMachineFunction(MachineFunction &MF) override;
98+
bool runOnMachineFunction(MachineFunction &MF) override;
9899

99-
void getAnalysisUsage(AnalysisUsage &AU) const override {
100-
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
101-
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
102-
AU.addRequired<ProfileSummaryInfoWrapperPass>();
103-
AU.addRequired<TargetPassConfig>();
104-
MachineFunctionPass::getAnalysisUsage(AU);
105-
}
100+
void getAnalysisUsage(AnalysisUsage &AU) const override {
101+
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
102+
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
103+
AU.addRequired<ProfileSummaryInfoWrapperPass>();
104+
AU.addRequired<TargetPassConfig>();
105+
MachineFunctionPass::getAnalysisUsage(AU);
106+
}
106107

107-
MachineFunctionProperties getRequiredProperties() const override {
108-
return MachineFunctionProperties().set(
109-
MachineFunctionProperties::Property::NoPHIs);
110-
}
111-
};
108+
MachineFunctionProperties getRequiredProperties() const override {
109+
return MachineFunctionProperties().set(
110+
MachineFunctionProperties::Property::NoPHIs);
111+
}
112+
};
112113

113114
} // end anonymous namespace
114115

115-
char BranchFolderPass::ID = 0;
116-
117-
char &llvm::BranchFolderPassID = BranchFolderPass::ID;
118-
119-
INITIALIZE_PASS(BranchFolderPass, DEBUG_TYPE,
120-
"Control Flow Optimizer", false, false)
116+
char BranchFolderLegacy::ID = 0;
117+
118+
char &llvm::BranchFolderPassID = BranchFolderLegacy::ID;
119+
120+
INITIALIZE_PASS(BranchFolderLegacy, DEBUG_TYPE, "Control Flow Optimizer", false,
121+
false)
122+
123+
PreservedAnalyses BranchFolderPass::run(MachineFunction &MF,
124+
MachineFunctionAnalysisManager &MFAM) {
125+
MFPropsModifier _(*this, MF);
126+
bool EnableTailMerge =
127+
!MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge;
128+
129+
auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
130+
auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
131+
.getCachedResult<ProfileSummaryAnalysis>(
132+
*MF.getFunction().getParent());
133+
if (!PSI)
134+
report_fatal_error(
135+
"ProfileSummaryAnalysis is required for BranchFoldingPass", false);
136+
137+
auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
138+
MBFIWrapper MBBFreqInfo(MBFI);
139+
BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, MBPI,
140+
PSI);
141+
if (!Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(),
142+
MF.getSubtarget().getRegisterInfo()))
143+
return PreservedAnalyses::all();
144+
return getMachineFunctionPassPreservedAnalyses();
145+
}
121146

122-
bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) {
147+
bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
123148
if (skipFunction(MF.getFunction()))
124149
return false;
125150

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
2222
initializeAtomicExpandLegacyPass(Registry);
2323
initializeBasicBlockPathCloningPass(Registry);
2424
initializeBasicBlockSectionsPass(Registry);
25-
initializeBranchFolderPassPass(Registry);
25+
initializeBranchFolderLegacyPass(Registry);
2626
initializeBranchRelaxationPass(Registry);
2727
initializeBreakFalseDepsPass(Registry);
2828
initializeCallBrPreparePass(Registry);

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
8181
#include "llvm/CodeGen/AtomicExpand.h"
8282
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
83+
#include "llvm/CodeGen/BranchFoldingPass.h"
8384
#include "llvm/CodeGen/CallBrPrepare.h"
8485
#include "llvm/CodeGen/CodeGenPrepare.h"
8586
#include "llvm/CodeGen/ComplexDeinterleavingPass.h"

llvm/test/CodeGen/AArch64/branch-folder-oneinst.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -o - %s -mtriple=aarch64 -run-pass branch-folder -verify-machineinstrs | FileCheck %s
2+
# RUN: llc -o - %s -mtriple=aarch64 -passes="require<profile-summary>,function(machine-function(branch-folder<enable-tail-merge>))" -verify-machineinstrs | FileCheck %s
23
# Check that BranchFolding pass is able to hoist a common instruction into a block with a single branch instruction.
34
name: func
45
tracksRegLiveness: true

llvm/test/CodeGen/AMDGPU/branch-folder-requires-no-phis.mir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# REQUIRES: asserts
2-
# RUN: not --crash llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=branch-folder -o /dev/null %s 2>&1 | FileCheck %s
2+
# RUN: not --crash llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=branch-folder -o /dev/null %s 2>&1 | FileCheck %s --check-prefixes=LEGACY-CHECK,CHECK
3+
# RUN: not --crash llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -passes="require<profile-summary>,function(machine-function(branch-folder<enable-tail-merge>))" -o /dev/null %s 2>&1 | FileCheck %s --check-prefixes=NPM-CHECK,CHECK
34

45
# BranchFolding breaks this function due to phis
56

6-
# CHECK: MachineFunctionProperties required by Control Flow Optimizer pass are not met by function func.
7+
# LEGACY-CHECK: MachineFunctionProperties required by Control Flow Optimizer pass are not met by function func.
8+
# NPM-CHECK: MachineFunctionProperties required by BranchFolderPass pass are not met by function func.
79
# CHECK-NEXT: Required properties: NoPHIs
810
# CHECK-NEXT: Current properties: IsSSA, TracksLiveness{{$}}
911
---

llvm/test/CodeGen/Hexagon/branchfolder-insert-impdef.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=hexagon -run-pass branch-folder %s -o - -verify-machineinstrs | FileCheck %s
2+
# RUN: llc -mtriple=hexagon -passes="require<profile-summary>,function(machine-function(branch-folder<enable-tail-merge>))" %s -o - -verify-machineinstrs | FileCheck %s
23

34
# Branch folding will perform tail merging of bb.1 and bb.2, and bb.2 will
45
# become the common tail. The use of R0 in bb.2 is <undef> while the

llvm/test/CodeGen/MIR/X86/branch-folder-with-label.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
# }
4040
#
4141
# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=branch-folder | FileCheck %s
42+
# RUN: llc -o - %s -mtriple=x86_64-- -passes="require<profile-summary>,function(machine-function(branch-folder<enable-tail-merge>))" | FileCheck %s
4243
--- |
4344
; ModuleID = 'test.ll'
4445
source_filename = "test.ll"

llvm/test/CodeGen/X86/branchfolding-ehpad.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=x86_64-windows-msvc -verify-machineinstrs -run-pass branch-folder -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=x86_64-windows-msvc -verify-machineinstrs -passes="require<profile-summary>,function(machine-function(branch-folder<enable-tail-merge>))" -o - %s | FileCheck %s
23

34
# Check that branch-folder does not create a fallthrough to a landing pad.
45
# Also make sure that the landing pad still can be tail merged.

0 commit comments

Comments
 (0)