Skip to content

Commit 719b029

Browse files
authored
[AMDGPU][NPM] Port SILateBranchLowering to NPM (#130063)
1 parent 75f810e commit 719b029

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ extern char &SILowerControlFlowLegacyID;
213213
void initializeSIPreEmitPeepholePass(PassRegistry &);
214214
extern char &SIPreEmitPeepholeID;
215215

216-
void initializeSILateBranchLoweringPass(PassRegistry &);
216+
void initializeSILateBranchLoweringLegacyPass(PassRegistry &);
217217
extern char &SILateBranchLoweringPassID;
218218

219219
void initializeSIOptimizeExecMaskingLegacyPass(PassRegistry &);
@@ -391,6 +391,14 @@ class SIInsertHardClausesPass : public PassInfoMixin<SIInsertHardClausesPass> {
391391
MachineFunctionAnalysisManager &MFAM);
392392
};
393393

394+
class SILateBranchLoweringPass
395+
: public PassInfoMixin<SILateBranchLoweringPass> {
396+
public:
397+
PreservedAnalyses run(MachineFunction &MF,
398+
MachineFunctionAnalysisManager &MFAM);
399+
static bool isRequired() { return true; }
400+
};
401+
394402
FunctionPass *createAMDGPUAnnotateUniformValuesLegacy();
395403

396404
ModulePass *createAMDGPUPrintfRuntimeBinding();

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ MACHINE_FUNCTION_PASS("si-form-memory-clauses", SIFormMemoryClausesPass())
113113
MACHINE_FUNCTION_PASS("si-i1-copies", SILowerI1CopiesPass())
114114
MACHINE_FUNCTION_PASS("si-insert-hard-clauses", SIInsertHardClausesPass())
115115
MACHINE_FUNCTION_PASS("si-insert-waitcnts", SIInsertWaitcntsPass())
116+
MACHINE_FUNCTION_PASS("si-late-branch-lowering", SILateBranchLoweringPass())
116117
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
117118
MACHINE_FUNCTION_PASS("si-lower-control-flow", SILowerControlFlowPass())
118119
MACHINE_FUNCTION_PASS("si-lower-sgpr-spills", SILowerSGPRSpillsPass())
@@ -134,7 +135,6 @@ DUMMY_MACHINE_FUNCTION_PASS("amdgpu-pre-ra-optimizations", GCNPreRAOptimizations
134135
DUMMY_MACHINE_FUNCTION_PASS("amdgpu-rewrite-partial-reg-uses", GCNRewritePartialRegUsesPass())
135136
DUMMY_MACHINE_FUNCTION_PASS("amdgpu-set-wave-priority", AMDGPUSetWavePriorityPass())
136137

137-
DUMMY_MACHINE_FUNCTION_PASS("si-late-branch-lowering", SILateBranchLoweringPass())
138138
DUMMY_MACHINE_FUNCTION_PASS("si-pre-emit-peephole", SIPreEmitPeepholePass())
139139
// TODO: Move amdgpu-preload-kern-arg-prolog to MACHINE_FUNCTION_PASS since it
140140
// already exists.

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
541541
initializeSIWholeQuadModeLegacyPass(*PR);
542542
initializeSILowerControlFlowLegacyPass(*PR);
543543
initializeSIPreEmitPeepholePass(*PR);
544-
initializeSILateBranchLoweringPass(*PR);
544+
initializeSILateBranchLoweringLegacyPass(*PR);
545545
initializeSIMemoryLegalizerLegacyPass(*PR);
546546
initializeSIOptimizeExecMaskingLegacyPass(*PR);
547547
initializeSIPreAllocateWWMRegsLegacyPass(*PR);
@@ -2166,7 +2166,8 @@ void AMDGPUCodeGenPassBuilder::addPreEmitPass(AddMachinePass &addPass) const {
21662166
// TODO: addPass(SIInsertHardClausesPass());
21672167
}
21682168

2169-
// addPass(SILateBranchLoweringPass());
2169+
addPass(SILateBranchLoweringPass());
2170+
21702171
if (isPassEnabled(EnableSetWavePriority, CodeGenOptLevel::Less)) {
21712172
// TODO: addPass(AMDGPUSetWavePriorityPass());
21722173
}

llvm/lib/Target/AMDGPU/SILateBranchLowering.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
1717
#include "SIMachineFunctionInfo.h"
1818
#include "llvm/CodeGen/MachineDominators.h"
19+
#include "llvm/CodeGen/MachinePassManager.h"
1920

2021
using namespace llvm;
2122

2223
#define DEBUG_TYPE "si-late-branch-lowering"
2324

2425
namespace {
2526

26-
class SILateBranchLowering : public MachineFunctionPass {
27+
class SILateBranchLowering {
2728
private:
2829
const SIRegisterInfo *TRI = nullptr;
2930
const SIInstrInfo *TII = nullptr;
@@ -34,14 +35,23 @@ class SILateBranchLowering : public MachineFunctionPass {
3435
void earlyTerm(MachineInstr &MI, MachineBasicBlock *EarlyExitBlock);
3536

3637
public:
37-
static char ID;
38+
SILateBranchLowering(MachineDominatorTree *MDT) : MDT(MDT) {}
39+
40+
bool run(MachineFunction &MF);
3841

3942
unsigned MovOpc;
4043
Register ExecReg;
44+
};
4145

42-
SILateBranchLowering() : MachineFunctionPass(ID) {}
46+
class SILateBranchLoweringLegacy : public MachineFunctionPass {
47+
public:
48+
static char ID;
49+
SILateBranchLoweringLegacy() : MachineFunctionPass(ID) {}
4350

44-
bool runOnMachineFunction(MachineFunction &MF) override;
51+
bool runOnMachineFunction(MachineFunction &MF) override {
52+
auto *MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
53+
return SILateBranchLowering(MDT).run(MF);
54+
}
4555

4656
StringRef getPassName() const override {
4757
return "SI Final Branch Preparation";
@@ -56,15 +66,15 @@ class SILateBranchLowering : public MachineFunctionPass {
5666

5767
} // end anonymous namespace
5868

59-
char SILateBranchLowering::ID = 0;
69+
char SILateBranchLoweringLegacy::ID = 0;
6070

61-
INITIALIZE_PASS_BEGIN(SILateBranchLowering, DEBUG_TYPE,
71+
INITIALIZE_PASS_BEGIN(SILateBranchLoweringLegacy, DEBUG_TYPE,
6272
"SI insert s_cbranch_execz instructions", false, false)
6373
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
64-
INITIALIZE_PASS_END(SILateBranchLowering, DEBUG_TYPE,
74+
INITIALIZE_PASS_END(SILateBranchLoweringLegacy, DEBUG_TYPE,
6575
"SI insert s_cbranch_execz instructions", false, false)
6676

67-
char &llvm::SILateBranchLoweringPassID = SILateBranchLowering::ID;
77+
char &llvm::SILateBranchLoweringPassID = SILateBranchLoweringLegacy::ID;
6878

6979
static void generateEndPgm(MachineBasicBlock &MBB,
7080
MachineBasicBlock::iterator I, DebugLoc DL,
@@ -192,11 +202,21 @@ void SILateBranchLowering::earlyTerm(MachineInstr &MI,
192202
MDT->insertEdge(&MBB, EarlyExitBlock);
193203
}
194204

195-
bool SILateBranchLowering::runOnMachineFunction(MachineFunction &MF) {
205+
PreservedAnalyses
206+
llvm::SILateBranchLoweringPass::run(MachineFunction &MF,
207+
MachineFunctionAnalysisManager &MFAM) {
208+
auto *MDT = &MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
209+
if (!SILateBranchLowering(MDT).run(MF))
210+
return PreservedAnalyses::all();
211+
212+
return getMachineFunctionPassPreservedAnalyses()
213+
.preserve<MachineDominatorTreeAnalysis>();
214+
}
215+
216+
bool SILateBranchLowering::run(MachineFunction &MF) {
196217
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
197218
TII = ST.getInstrInfo();
198219
TRI = &TII->getRegisterInfo();
199-
MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
200220

201221
MovOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
202222
ExecReg = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;

llvm/test/CodeGen/AMDGPU/early-term.mir

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# RUN: llc -mtriple=amdgcn -mcpu=gfx1010 -mattr=+wavefrontsize64 -run-pass=si-late-branch-lowering -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX10 %s
33
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=+wavefrontsize64 -run-pass=si-late-branch-lowering -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX11 %s
44

5+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=+wavefrontsize64 -passes=si-late-branch-lowering %s -o - | FileCheck -check-prefixes=GCN,GFX11 %s
6+
57
--- |
68
define amdgpu_ps void @early_term_scc0_end_block() {
79
ret void

llvm/test/CodeGen/AMDGPU/readlane_exec0.mir

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -o - %s -mtriple=amdgcn -mcpu=fiji -run-pass=si-late-branch-lowering -verify-machineinstrs | FileCheck -check-prefix=GCN %s
2+
# RUN: llc -o - %s -mtriple=amdgcn -mcpu=fiji -passes=si-late-branch-lowering | FileCheck -check-prefix=GCN %s
23

34
# GCN-LABEL: readlane_exec0
45
# GCN: bb.0

0 commit comments

Comments
 (0)