Skip to content

Commit b85cc52

Browse files
committed
[AMDGPU][NewPM] Port SILowerWWMCopies to NPM
1 parent 7b4cefb commit b85cc52

File tree

6 files changed

+127
-31
lines changed

6 files changed

+127
-31
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ extern char &SIFixSGPRCopiesLegacyID;
180180
void initializeSIFixVGPRCopiesPass(PassRegistry &);
181181
extern char &SIFixVGPRCopiesID;
182182

183-
void initializeSILowerWWMCopiesPass(PassRegistry &);
184-
extern char &SILowerWWMCopiesID;
183+
void initializeSILowerWWMCopiesLegacyPass(PassRegistry &);
184+
extern char &SILowerWWMCopiesLegacyID;
185185

186186
void initializeSILowerI1CopiesLegacyPass(PassRegistry &);
187187
extern char &SILowerI1CopiesLegacyID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ MACHINE_FUNCTION_PASS("gcn-dpp-combine", GCNDPPCombinePass())
104104
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
105105
MACHINE_FUNCTION_PASS("si-lower-control-flow", SILowerControlFlowPass())
106106
MACHINE_FUNCTION_PASS("si-lower-sgpr-spills", SILowerSGPRSpillsPass())
107+
MACHINE_FUNCTION_PASS("si-lower-wwm-copies", SILowerWWMCopiesPass())
107108
MACHINE_FUNCTION_PASS("si-opt-vgpr-liverange", SIOptimizeVGPRLiveRangePass())
108109
MACHINE_FUNCTION_PASS("si-optimize-exec-masking", SIOptimizeExecMaskingPass())
109110
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "SILoadStoreOptimizer.h"
4141
#include "SILowerControlFlow.h"
4242
#include "SILowerSGPRSpills.h"
43+
#include "SILowerWWMCopies.h"
4344
#include "SIMachineFunctionInfo.h"
4445
#include "SIMachineScheduler.h"
4546
#include "SIOptimizeExecMasking.h"
@@ -482,7 +483,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
482483
initializeAMDGPUGlobalISelDivergenceLoweringPass(*PR);
483484
initializeAMDGPURegBankSelectPass(*PR);
484485
initializeAMDGPURegBankLegalizePass(*PR);
485-
initializeSILowerWWMCopiesPass(*PR);
486+
initializeSILowerWWMCopiesLegacyPass(*PR);
486487
initializeAMDGPUMarkLastScratchLoadPass(*PR);
487488
initializeSILowerSGPRSpillsLegacyPass(*PR);
488489
initializeSIFixSGPRCopiesLegacyPass(*PR);
@@ -1581,7 +1582,7 @@ bool GCNPassConfig::addRegAssignAndRewriteFast() {
15811582
// For allocating other wwm register operands.
15821583
addPass(createWWMRegAllocPass(false));
15831584

1584-
addPass(&SILowerWWMCopiesID);
1585+
addPass(&SILowerWWMCopiesLegacyID);
15851586
addPass(&AMDGPUReserveWWMRegsID);
15861587

15871588
// For allocating per-thread VGPRs.
@@ -1617,7 +1618,7 @@ bool GCNPassConfig::addRegAssignAndRewriteOptimized() {
16171618

16181619
// For allocating other whole wave mode registers.
16191620
addPass(createWWMRegAllocPass(true));
1620-
addPass(&SILowerWWMCopiesID);
1621+
addPass(&SILowerWWMCopiesLegacyID);
16211622
addPass(createVirtRegRewriter(false));
16221623
addPass(&AMDGPUReserveWWMRegsID);
16231624

llvm/lib/Target/AMDGPU/SILowerWWMCopies.cpp

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18+
#include "SILowerWWMCopies.h"
1819
#include "AMDGPU.h"
1920
#include "GCNSubtarget.h"
2021
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
@@ -30,12 +31,30 @@ using namespace llvm;
3031

3132
namespace {
3233

33-
class SILowerWWMCopies : public MachineFunctionPass {
34+
class SILowerWWMCopies {
35+
public:
36+
SILowerWWMCopies(LiveIntervals *LIS, SlotIndexes *SI, VirtRegMap *VRM)
37+
: LIS(LIS), Indexes(SI), VRM(VRM) {}
38+
bool run(MachineFunction &MF);
39+
40+
private:
41+
bool isSCCLiveAtMI(const MachineInstr &MI);
42+
void addToWWMSpills(MachineFunction &MF, Register Reg);
43+
44+
LiveIntervals *LIS;
45+
SlotIndexes *Indexes;
46+
VirtRegMap *VRM;
47+
const SIRegisterInfo *TRI;
48+
const MachineRegisterInfo *MRI;
49+
SIMachineFunctionInfo *MFI;
50+
};
51+
52+
class SILowerWWMCopiesLegacy : public MachineFunctionPass {
3453
public:
3554
static char ID;
3655

37-
SILowerWWMCopies() : MachineFunctionPass(ID) {
38-
initializeSILowerWWMCopiesPass(*PassRegistry::getPassRegistry());
56+
SILowerWWMCopiesLegacy() : MachineFunctionPass(ID) {
57+
initializeSILowerWWMCopiesLegacyPass(*PassRegistry::getPassRegistry());
3958
}
4059

4160
bool runOnMachineFunction(MachineFunction &MF) override;
@@ -49,31 +68,20 @@ class SILowerWWMCopies : public MachineFunctionPass {
4968
AU.setPreservesAll();
5069
MachineFunctionPass::getAnalysisUsage(AU);
5170
}
52-
53-
private:
54-
bool isSCCLiveAtMI(const MachineInstr &MI);
55-
void addToWWMSpills(MachineFunction &MF, Register Reg);
56-
57-
LiveIntervals *LIS;
58-
SlotIndexes *Indexes;
59-
VirtRegMap *VRM;
60-
const SIRegisterInfo *TRI;
61-
const MachineRegisterInfo *MRI;
62-
SIMachineFunctionInfo *MFI;
6371
};
6472

6573
} // End anonymous namespace.
6674

67-
INITIALIZE_PASS_BEGIN(SILowerWWMCopies, DEBUG_TYPE, "SI Lower WWM Copies",
75+
INITIALIZE_PASS_BEGIN(SILowerWWMCopiesLegacy, DEBUG_TYPE, "SI Lower WWM Copies",
6876
false, false)
6977
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
7078
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
71-
INITIALIZE_PASS_END(SILowerWWMCopies, DEBUG_TYPE, "SI Lower WWM Copies", false,
72-
false)
79+
INITIALIZE_PASS_END(SILowerWWMCopiesLegacy, DEBUG_TYPE, "SI Lower WWM Copies",
80+
false, false)
7381

74-
char SILowerWWMCopies::ID = 0;
82+
char SILowerWWMCopiesLegacy::ID = 0;
7583

76-
char &llvm::SILowerWWMCopiesID = SILowerWWMCopies::ID;
84+
char &llvm::SILowerWWMCopiesLegacyID = SILowerWWMCopiesLegacy::ID;
7785

7886
bool SILowerWWMCopies::isSCCLiveAtMI(const MachineInstr &MI) {
7987
// We can't determine the liveness info if LIS isn't available. Early return
@@ -93,23 +101,44 @@ void SILowerWWMCopies::addToWWMSpills(MachineFunction &MF, Register Reg) {
93101
if (Reg.isPhysical())
94102
return;
95103

104+
// FIXME: VRM may be null here.
96105
MCRegister PhysReg = VRM->getPhys(Reg);
97106
assert(PhysReg && "should have allocated a physical register");
98107

99108
MFI->allocateWWMSpill(MF, PhysReg);
100109
}
101110

102-
bool SILowerWWMCopies::runOnMachineFunction(MachineFunction &MF) {
111+
bool SILowerWWMCopiesLegacy::runOnMachineFunction(MachineFunction &MF) {
112+
auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
113+
auto *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
114+
115+
auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
116+
auto *Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
117+
118+
auto *VRMWrapper = getAnalysisIfAvailable<VirtRegMapWrapperLegacy>();
119+
auto *VRM = VRMWrapper ? &VRMWrapper->getVRM() : nullptr;
120+
121+
SILowerWWMCopies Impl(LIS, Indexes, VRM);
122+
return Impl.run(MF);
123+
}
124+
125+
PreservedAnalyses
126+
SILowerWWMCopiesPass::run(MachineFunction &MF,
127+
MachineFunctionAnalysisManager &MFAM) {
128+
auto *LIS = MFAM.getCachedResult<LiveIntervalsAnalysis>(MF);
129+
auto *Indexes = MFAM.getCachedResult<SlotIndexesAnalysis>(MF);
130+
auto *VRM = MFAM.getCachedResult<VirtRegMapAnalysis>(MF);
131+
132+
SILowerWWMCopies Impl(LIS, Indexes, VRM);
133+
Impl.run(MF);
134+
return PreservedAnalyses::all();
135+
}
136+
137+
bool SILowerWWMCopies::run(MachineFunction &MF) {
103138
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
104139
const SIInstrInfo *TII = ST.getInstrInfo();
105140

106141
MFI = MF.getInfo<SIMachineFunctionInfo>();
107-
auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
108-
LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
109-
auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
110-
Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
111-
auto *VRMWrapper = getAnalysisIfAvailable<VirtRegMapWrapperLegacy>();
112-
VRM = VRMWrapper ? &VRMWrapper->getVRM() : nullptr;
113142
TRI = ST.getRegisterInfo();
114143
MRI = &MF.getRegInfo();
115144

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- SILowerWWMCopies.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_SILOWERWWMCOPIES_H
10+
#define LLVM_LIB_TARGET_AMDGPU_SILOWERWWMCOPIES_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
class SILowerWWMCopiesPass : public PassInfoMixin<SILowerWWMCopiesPass> {
16+
public:
17+
PreservedAnalyses run(MachineFunction &MF,
18+
MachineFunctionAnalysisManager &MFAM);
19+
};
20+
} // namespace llvm
21+
22+
#endif // LLVM_LIB_TARGET_AMDGPU_SILOWERWWMCOPIES_H
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
3+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=liveintervals,virtregmap,si-lower-wwm-copies -o - %s | FileCheck %s
4+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -passes="require<live-intervals>,require<virtregmap>,si-lower-wwm-copies" -o - %s | FileCheck %s
5+
6+
# Check for two cases of $scc being live and dead.
7+
---
8+
name: lower-wwm-copies
9+
registers:
10+
- { id: 1, class: vgpr_32, flags: [ WWM_REG ]}
11+
machineFunctionInfo:
12+
sgprForEXECCopy: '$sgpr2_sgpr3'
13+
tracksRegLiveness: true
14+
body: |
15+
; CHECK-LABEL: name: lower-wwm-copies
16+
; CHECK: bb.0:
17+
; CHECK-NEXT: successors: %bb.1(0x80000000)
18+
; CHECK-NEXT: liveins: $vgpr0, $scc
19+
; CHECK-NEXT: {{ $}}
20+
; CHECK-NEXT: [[DEF:%[0-9]+]]:sgpr_32 = IMPLICIT_DEF
21+
; CHECK-NEXT: S_CMP_EQ_U32 [[DEF]], 0, implicit-def $scc
22+
; CHECK-NEXT: $sgpr2_sgpr3 = S_MOV_B64 killed $exec
23+
; CHECK-NEXT: $exec = S_MOV_B64 -1
24+
; CHECK-NEXT: $vgpr1 = COPY $vgpr0
25+
; CHECK-NEXT: $exec = S_MOV_B64 killed $sgpr2_sgpr3
26+
; CHECK-NEXT: S_CBRANCH_SCC1 %bb.1, implicit $scc
27+
; CHECK-NEXT: {{ $}}
28+
; CHECK-NEXT: bb.1:
29+
; CHECK-NEXT: liveins: $vgpr1
30+
; CHECK-NEXT: {{ $}}
31+
; CHECK-NEXT: $sgpr2_sgpr3 = S_OR_SAVEEXEC_B64 -1, implicit-def $exec, implicit-def dead $scc, implicit $exec
32+
; CHECK-NEXT: $vgpr2 = COPY $vgpr1
33+
; CHECK-NEXT: $exec = S_MOV_B64 killed $sgpr2_sgpr3
34+
bb.0:
35+
liveins: $vgpr0, $scc
36+
%0:sgpr_32 = IMPLICIT_DEF
37+
S_CMP_EQ_U32 %0, 0, implicit-def $scc
38+
$vgpr1 = WWM_COPY $vgpr0
39+
S_CBRANCH_SCC1 %bb.1, implicit killed $scc
40+
41+
bb.1:
42+
liveins: $vgpr1
43+
$vgpr2 = WWM_COPY $vgpr1

0 commit comments

Comments
 (0)