Skip to content

Commit 1797fb6

Browse files
authored
[AMDGPU][NewPM] Port SILowerControlFlow pass into NPM. (llvm#123045)
1 parent 3e3a4d8 commit 1797fb6

8 files changed

+80
-20
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ extern char &SILoadStoreOptimizerLegacyID;
207207
void initializeSIWholeQuadModePass(PassRegistry &);
208208
extern char &SIWholeQuadModeID;
209209

210-
void initializeSILowerControlFlowPass(PassRegistry &);
211-
extern char &SILowerControlFlowID;
210+
void initializeSILowerControlFlowLegacyPass(PassRegistry &);
211+
extern char &SILowerControlFlowLegacyID;
212212

213213
void initializeSIPreEmitPeepholePass(PassRegistry &);
214214
extern char &SIPreEmitPeepholeID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ MACHINE_FUNCTION_PASS("si-i1-copies", SILowerI1CopiesPass())
102102
MACHINE_FUNCTION_PASS("si-fold-operands", SIFoldOperandsPass());
103103
MACHINE_FUNCTION_PASS("gcn-dpp-combine", GCNDPPCombinePass())
104104
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
105+
MACHINE_FUNCTION_PASS("si-lower-control-flow", SILowerControlFlowPass())
105106
MACHINE_FUNCTION_PASS("si-lower-sgpr-spills", SILowerSGPRSpillsPass())
106107
MACHINE_FUNCTION_PASS("si-opt-vgpr-liverange", SIOptimizeVGPRLiveRangePass())
107108
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "SIFixSGPRCopies.h"
3939
#include "SIFoldOperands.h"
4040
#include "SILoadStoreOptimizer.h"
41+
#include "SILowerControlFlow.h"
4142
#include "SILowerSGPRSpills.h"
4243
#include "SIMachineFunctionInfo.h"
4344
#include "SIMachineScheduler.h"
@@ -523,7 +524,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
523524
initializeSIInsertWaitcntsPass(*PR);
524525
initializeSIModeRegisterPass(*PR);
525526
initializeSIWholeQuadModePass(*PR);
526-
initializeSILowerControlFlowPass(*PR);
527+
initializeSILowerControlFlowLegacyPass(*PR);
527528
initializeSIPreEmitPeepholePass(*PR);
528529
initializeSILateBranchLoweringPass(*PR);
529530
initializeSIMemoryLegalizerPass(*PR);
@@ -1459,7 +1460,7 @@ void GCNPassConfig::addFastRegAlloc() {
14591460
// This must be run immediately after phi elimination and before
14601461
// TwoAddressInstructions, otherwise the processing of the tied operand of
14611462
// SI_ELSE will introduce a copy of the tied operand source after the else.
1462-
insertPass(&PHIEliminationID, &SILowerControlFlowID);
1463+
insertPass(&PHIEliminationID, &SILowerControlFlowLegacyID);
14631464

14641465
insertPass(&TwoAddressInstructionPassID, &SIWholeQuadModeID);
14651466

@@ -1480,7 +1481,7 @@ void GCNPassConfig::addOptimizedRegAlloc() {
14801481
// This must be run immediately after phi elimination and before
14811482
// TwoAddressInstructions, otherwise the processing of the tied operand of
14821483
// SI_ELSE will introduce a copy of the tied operand source after the else.
1483-
insertPass(&PHIEliminationID, &SILowerControlFlowID);
1484+
insertPass(&PHIEliminationID, &SILowerControlFlowLegacyID);
14841485

14851486
if (EnableRewritePartialRegUses)
14861487
insertPass(&RenameIndependentSubregsID, &GCNRewritePartialRegUsesID);

llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp

+48-15
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
/// %exec = S_OR_B64 %exec, %sgpr0 // Re-enable saved exec mask bits
4949
//===----------------------------------------------------------------------===//
5050

51+
#include "SILowerControlFlow.h"
5152
#include "AMDGPU.h"
5253
#include "GCNSubtarget.h"
5354
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
@@ -68,7 +69,7 @@ RemoveRedundantEndcf("amdgpu-remove-redundant-endcf",
6869

6970
namespace {
7071

71-
class SILowerControlFlow : public MachineFunctionPass {
72+
class SILowerControlFlow {
7273
private:
7374
const SIRegisterInfo *TRI = nullptr;
7475
const SIInstrInfo *TII = nullptr;
@@ -135,10 +136,18 @@ class SILowerControlFlow : public MachineFunctionPass {
135136
// Remove redundant SI_END_CF instructions.
136137
void optimizeEndCf();
137138

139+
public:
140+
SILowerControlFlow(LiveIntervals *LIS, LiveVariables *LV,
141+
MachineDominatorTree *MDT)
142+
: LIS(LIS), LV(LV), MDT(MDT) {}
143+
bool run(MachineFunction &MF);
144+
};
145+
146+
class SILowerControlFlowLegacy : public MachineFunctionPass {
138147
public:
139148
static char ID;
140149

141-
SILowerControlFlow() : MachineFunctionPass(ID) {}
150+
SILowerControlFlowLegacy() : MachineFunctionPass(ID) {}
142151

143152
bool runOnMachineFunction(MachineFunction &MF) override;
144153

@@ -159,10 +168,10 @@ class SILowerControlFlow : public MachineFunctionPass {
159168

160169
} // end anonymous namespace
161170

162-
char SILowerControlFlow::ID = 0;
171+
char SILowerControlFlowLegacy::ID = 0;
163172

164-
INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
165-
"SI lower control flow", false, false)
173+
INITIALIZE_PASS(SILowerControlFlowLegacy, DEBUG_TYPE, "SI lower control flow",
174+
false, false)
166175

167176
static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) {
168177
MachineOperand &ImpDefSCC = MI.getOperand(3);
@@ -171,7 +180,7 @@ static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) {
171180
ImpDefSCC.setIsDead(IsDead);
172181
}
173182

174-
char &llvm::SILowerControlFlowID = SILowerControlFlow::ID;
183+
char &llvm::SILowerControlFlowLegacyID = SILowerControlFlowLegacy::ID;
175184

176185
bool SILowerControlFlow::hasKill(const MachineBasicBlock *Begin,
177186
const MachineBasicBlock *End) {
@@ -753,21 +762,13 @@ bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
753762
return true;
754763
}
755764

756-
bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
765+
bool SILowerControlFlow::run(MachineFunction &MF) {
757766
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
758767
TII = ST.getInstrInfo();
759768
TRI = &TII->getRegisterInfo();
760769
EnableOptimizeEndCf = RemoveRedundantEndcf &&
761770
MF.getTarget().getOptLevel() > CodeGenOptLevel::None;
762771

763-
// This doesn't actually need LiveIntervals, but we can preserve them.
764-
auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
765-
LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
766-
// This doesn't actually need LiveVariables, but we can preserve them.
767-
auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
768-
LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
769-
auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
770-
MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
771772
MRI = &MF.getRegInfo();
772773
BoolRC = TRI->getBoolRC();
773774

@@ -864,3 +865,35 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
864865

865866
return Changed;
866867
}
868+
869+
bool SILowerControlFlowLegacy::runOnMachineFunction(MachineFunction &MF) {
870+
// This doesn't actually need LiveIntervals, but we can preserve them.
871+
auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
872+
LiveIntervals *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
873+
// This doesn't actually need LiveVariables, but we can preserve them.
874+
auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
875+
LiveVariables *LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
876+
auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
877+
MachineDominatorTree *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
878+
return SILowerControlFlow(LIS, LV, MDT).run(MF);
879+
}
880+
881+
PreservedAnalyses
882+
SILowerControlFlowPass::run(MachineFunction &MF,
883+
MachineFunctionAnalysisManager &MFAM) {
884+
LiveIntervals *LIS = MFAM.getCachedResult<LiveIntervalsAnalysis>(MF);
885+
LiveVariables *LV = MFAM.getCachedResult<LiveVariablesAnalysis>(MF);
886+
MachineDominatorTree *MDT =
887+
MFAM.getCachedResult<MachineDominatorTreeAnalysis>(MF);
888+
889+
bool Changed = SILowerControlFlow(LIS, LV, MDT).run(MF);
890+
if (!Changed)
891+
return PreservedAnalyses::all();
892+
893+
auto PA = getMachineFunctionPassPreservedAnalyses();
894+
PA.preserve<MachineDominatorTreeAnalysis>();
895+
PA.preserve<SlotIndexesAnalysis>();
896+
PA.preserve<LiveIntervalsAnalysis>();
897+
PA.preserve<LiveVariablesAnalysis>();
898+
return PA;
899+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- SILowerControlFlow.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+
9+
#ifndef LLVM_LIB_TARGET_AMDGPU_SILOWERCONTROLFLOW_H
10+
#define LLVM_LIB_TARGET_AMDGPU_SILOWERCONTROLFLOW_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
class SILowerControlFlowPass : public PassInfoMixin<SILowerControlFlowPass> {
16+
public:
17+
PreservedAnalyses run(MachineFunction &MF,
18+
MachineFunctionAnalysisManager &MFAM);
19+
};
20+
} // namespace llvm
21+
22+
#endif // LLVM_LIB_TARGET_AMDGPU_SILOWERCONTROLFLOW_H

llvm/test/CodeGen/AMDGPU/collapse-endcf.mir

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=amdgcn -verify-machineinstrs -run-pass=si-lower-control-flow -amdgpu-remove-redundant-endcf %s -o - | FileCheck -check-prefix=GCN %s
3+
# RUN: llc -mtriple=amdgcn -passes=si-lower-control-flow -amdgpu-remove-redundant-endcf %s -o - | FileCheck -check-prefix=GCN %s
34

45
# Make sure dbg_value doesn't change codeegn when collapsing end_cf
56
---

llvm/test/CodeGen/AMDGPU/lower-control-flow-live-intervals.mir

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
22
# RUN: llc -run-pass=liveintervals -run-pass=si-lower-control-flow -mtriple=amdgcn--amdpal -mcpu=gfx1030 -verify-machineinstrs -o - %s | FileCheck %s
3+
# RUN: llc -passes='require<live-intervals>,si-lower-control-flow' -mtriple=amdgcn--amdpal -mcpu=gfx1030 -o - %s | FileCheck %s
34

45
# Check that verifier passes for the following.
56

llvm/test/CodeGen/AMDGPU/lower-control-flow-other-terminators.mir

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=amdgcn -mcpu=fiji -verify-machineinstrs -run-pass=si-lower-control-flow -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=amdgcn -mcpu=fiji -passes=si-lower-control-flow -o - %s | FileCheck %s
34

45
# Test si-lower-control-flow insertion points when other terminator
56
# instructions are present besides the control flow pseudo and a

0 commit comments

Comments
 (0)