Skip to content

Commit 29cbe78

Browse files
cdevadasyanyao-wang
authored andcommitted
[AMDGPU] Split VGPR regalloc pipeline
Allocating wwm-registers and regular VGPR operands together imposes many challenges in the way the registers are reused during allocation. There are times when regalloc reuses the registers of regular VGPRs operations for wwm-operations in a small range leading to unwantedly clobbering their inactive lanes causing correctness issues which are hard to trace. This patch splits the VGPR allocation pipeline further to allocate wwm-registers first and the regular VGPR operands in a separate pipeline. The splitting would ensure that the physical registers used for wwm allocations won't taken part in the next allocation pipeline to avoid any such clobbering. Change-Id: Ib2c5b9b53944bf78709465a9d1786d129434ce40
1 parent 410d02a commit 29cbe78

File tree

78 files changed

+8079
-8506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+8079
-8506
lines changed

llvm/include/llvm/CodeGen/MachineRegisterInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ class MachineRegisterInfo {
184184
TheDelegate->MRI_NoteCloneVirtualRegister(NewReg, SrcReg);
185185
}
186186

187+
const MachineFunction &getMF() const { return *MF; }
188+
187189
//===--------------------------------------------------------------------===//
188190
// Function State
189191
//===--------------------------------------------------------------------===//

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ ModulePass *createAMDGPURemoveIncompatibleFunctionsPass(const TargetMachine *);
5656
FunctionPass *createAMDGPUCodeGenPreparePass();
5757
FunctionPass *createAMDGPULateCodeGenPreparePass();
5858
FunctionPass *createAMDGPUMachineCFGStructurizerPass();
59+
FunctionPass *createAMDGPUReserveWWMRegsPass();
5960
FunctionPass *createAMDGPURewriteOutArgumentsPass();
6061
ModulePass *
6162
createAMDGPULowerModuleLDSLegacyPass(const AMDGPUTargetMachine *TM = nullptr);
@@ -136,6 +137,9 @@ struct AMDGPULowerModuleLDSPass : PassInfoMixin<AMDGPULowerModuleLDSPass> {
136137
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
137138
};
138139

140+
void initializeAMDGPUReserveWWMRegsPass(PassRegistry &);
141+
extern char &AMDGPUReserveWWMRegsID;
142+
139143
void initializeAMDGPURewriteOutArgumentsPass(PassRegistry &);
140144
extern char &AMDGPURewriteOutArgumentsID;
141145

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//===-- AMDGPUReserveWWMRegs.cpp - Add WWM Regs to reserved regs list -----===//
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+
/// \file
10+
/// This pass should be invoked at the end of wwm-regalloc pipeline.
11+
/// It identifies the WWM regs allocated during this pipeline and add
12+
/// them to the list of reserved registers so that they won't be available for
13+
/// regular VGPR allocation in the subsequent regalloc pipeline.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "AMDGPU.h"
18+
#include "GCNSubtarget.h"
19+
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20+
#include "SIMachineFunctionInfo.h"
21+
#include "llvm/CodeGen/LiveIntervals.h"
22+
#include "llvm/CodeGen/MachineFunctionPass.h"
23+
#include "llvm/CodeGen/VirtRegMap.h"
24+
#include "llvm/InitializePasses.h"
25+
26+
using namespace llvm;
27+
28+
#define DEBUG_TYPE "amdgpu-reserve-wwm-regs"
29+
30+
namespace {
31+
32+
class AMDGPUReserveWWMRegs : public MachineFunctionPass {
33+
public:
34+
static char ID;
35+
36+
AMDGPUReserveWWMRegs() : MachineFunctionPass(ID) {
37+
initializeAMDGPUReserveWWMRegsPass(*PassRegistry::getPassRegistry());
38+
}
39+
40+
bool runOnMachineFunction(MachineFunction &MF) override;
41+
42+
StringRef getPassName() const override {
43+
return "AMDGPU Reserve WWM Registers";
44+
}
45+
46+
void getAnalysisUsage(AnalysisUsage &AU) const override {
47+
AU.setPreservesAll();
48+
MachineFunctionPass::getAnalysisUsage(AU);
49+
}
50+
};
51+
52+
} // End anonymous namespace.
53+
54+
INITIALIZE_PASS(AMDGPUReserveWWMRegs, DEBUG_TYPE,
55+
"AMDGPU Reserve WWM Registers", false, false)
56+
57+
char AMDGPUReserveWWMRegs::ID = 0;
58+
59+
char &llvm::AMDGPUReserveWWMRegsID = AMDGPUReserveWWMRegs::ID;
60+
61+
bool AMDGPUReserveWWMRegs::runOnMachineFunction(MachineFunction &MF) {
62+
SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
63+
64+
bool Changed = false;
65+
for (MachineBasicBlock &MBB : MF) {
66+
for (MachineInstr &MI : MBB) {
67+
unsigned Opc = MI.getOpcode();
68+
if (Opc != AMDGPU::SI_SPILL_S32_TO_VGPR &&
69+
Opc != AMDGPU::SI_RESTORE_S32_FROM_VGPR)
70+
continue;
71+
72+
Register Reg = Opc == AMDGPU::SI_SPILL_S32_TO_VGPR
73+
? MI.getOperand(0).getReg()
74+
: MI.getOperand(1).getReg();
75+
76+
assert(Reg.isPhysical() &&
77+
"All WWM registers should have been allocated by now.");
78+
79+
MFI->reserveWWMRegister(Reg);
80+
Changed |= true;
81+
}
82+
}
83+
84+
// The renamable flag can't be set for reserved registers. Reset the flag for
85+
// MOs involving wwm-regs as they will be reserved during vgpr-regalloc
86+
// pipeline.
87+
const MachineRegisterInfo &MRI = MF.getRegInfo();
88+
for (Register Reg : MFI->getWWMReservedRegs()) {
89+
for (MachineOperand &MO : MRI.reg_operands(Reg))
90+
MO.setIsRenamable(false);
91+
}
92+
93+
// Now clear the NonWWMRegMask earlier set during wwm-regalloc.
94+
MFI->clearNonWWMRegAllocMask();
95+
96+
return Changed;
97+
}

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class VGPRRegisterRegAlloc : public RegisterRegAllocBase<VGPRRegisterRegAlloc> {
8181
: RegisterRegAllocBase(N, D, C) {}
8282
};
8383

84+
class WWMRegisterRegAlloc : public RegisterRegAllocBase<WWMRegisterRegAlloc> {
85+
public:
86+
WWMRegisterRegAlloc(const char *N, const char *D, FunctionPassCtor C)
87+
: RegisterRegAllocBase(N, D, C) {}
88+
};
89+
8490
static bool onlyAllocateSGPRs(const TargetRegisterInfo &TRI,
8591
const MachineRegisterInfo &MRI,
8692
const Register Reg) {
@@ -95,13 +101,24 @@ static bool onlyAllocateVGPRs(const TargetRegisterInfo &TRI,
95101
return !static_cast<const SIRegisterInfo &>(TRI).isSGPRClass(RC);
96102
}
97103

98-
/// -{sgpr|vgpr}-regalloc=... command line option.
104+
static bool onlyAllocateWWMRegs(const TargetRegisterInfo &TRI,
105+
const MachineRegisterInfo &MRI,
106+
const Register Reg) {
107+
const SIMachineFunctionInfo *MFI =
108+
MRI.getMF().getInfo<SIMachineFunctionInfo>();
109+
const TargetRegisterClass *RC = MRI.getRegClass(Reg);
110+
return !static_cast<const SIRegisterInfo &>(TRI).isSGPRClass(RC) &&
111+
MFI->checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG);
112+
}
113+
114+
/// -{sgpr|wwm|vgpr}-regalloc=... command line option.
99115
static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
100116

101117
/// A dummy default pass factory indicates whether the register allocator is
102118
/// overridden on the command line.
103119
static llvm::once_flag InitializeDefaultSGPRRegisterAllocatorFlag;
104120
static llvm::once_flag InitializeDefaultVGPRRegisterAllocatorFlag;
121+
static llvm::once_flag InitializeDefaultWWMRegisterAllocatorFlag;
105122

106123
static SGPRRegisterRegAlloc
107124
defaultSGPRRegAlloc("default",
@@ -118,6 +135,12 @@ static cl::opt<VGPRRegisterRegAlloc::FunctionPassCtor, false,
118135
VGPRRegAlloc("vgpr-regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator),
119136
cl::desc("Register allocator to use for VGPRs"));
120137

138+
static cl::opt<WWMRegisterRegAlloc::FunctionPassCtor, false,
139+
RegisterPassParser<WWMRegisterRegAlloc>>
140+
WWMRegAlloc("wwm-regalloc", cl::Hidden,
141+
cl::init(&useDefaultRegisterAllocator),
142+
cl::desc("Register allocator to use for WWM registers"));
143+
121144
static void initializeDefaultSGPRRegisterAllocatorOnce() {
122145
RegisterRegAlloc::FunctionPassCtor Ctor = SGPRRegisterRegAlloc::getDefault();
123146

@@ -136,6 +159,15 @@ static void initializeDefaultVGPRRegisterAllocatorOnce() {
136159
}
137160
}
138161

162+
static void initializeDefaultWWMRegisterAllocatorOnce() {
163+
RegisterRegAlloc::FunctionPassCtor Ctor = WWMRegisterRegAlloc::getDefault();
164+
165+
if (!Ctor) {
166+
Ctor = WWMRegAlloc;
167+
WWMRegisterRegAlloc::setDefault(WWMRegAlloc);
168+
}
169+
}
170+
139171
static FunctionPass *createBasicSGPRRegisterAllocator() {
140172
return createBasicRegisterAllocator(onlyAllocateSGPRs);
141173
}
@@ -160,6 +192,18 @@ static FunctionPass *createFastVGPRRegisterAllocator() {
160192
return createFastRegisterAllocator(onlyAllocateVGPRs, true);
161193
}
162194

195+
static FunctionPass *createBasicWWMRegisterAllocator() {
196+
return createBasicRegisterAllocator(onlyAllocateWWMRegs);
197+
}
198+
199+
static FunctionPass *createGreedyWWMRegisterAllocator() {
200+
return createGreedyRegisterAllocator(onlyAllocateWWMRegs);
201+
}
202+
203+
static FunctionPass *createFastWWMRegisterAllocator() {
204+
return createFastRegisterAllocator(onlyAllocateWWMRegs, false);
205+
}
206+
163207
static SGPRRegisterRegAlloc basicRegAllocSGPR(
164208
"basic", "basic register allocator", createBasicSGPRRegisterAllocator);
165209
static SGPRRegisterRegAlloc greedyRegAllocSGPR(
@@ -176,7 +220,16 @@ static VGPRRegisterRegAlloc greedyRegAllocVGPR(
176220

177221
static VGPRRegisterRegAlloc fastRegAllocVGPR(
178222
"fast", "fast register allocator", createFastVGPRRegisterAllocator);
179-
}
223+
224+
static WWMRegisterRegAlloc basicRegAllocWWMReg("basic",
225+
"basic register allocator",
226+
createBasicWWMRegisterAllocator);
227+
static WWMRegisterRegAlloc
228+
greedyRegAllocWWMReg("greedy", "greedy register allocator",
229+
createGreedyWWMRegisterAllocator);
230+
static WWMRegisterRegAlloc fastRegAllocWWMReg("fast", "fast register allocator",
231+
createFastWWMRegisterAllocator);
232+
} // namespace
180233

181234
static cl::opt<bool>
182235
EnableEarlyIfConversion("amdgpu-early-ifcvt", cl::Hidden,
@@ -417,6 +470,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
417470
initializeAMDGPULateCodeGenPreparePass(*PR);
418471
initializeAMDGPURemoveIncompatibleFunctionsPass(*PR);
419472
initializeAMDGPULowerModuleLDSLegacyPass(*PR);
473+
initializeAMDGPUReserveWWMRegsPass(*PR);
420474
initializeAMDGPURewriteOutArgumentsPass(*PR);
421475
initializeAMDGPURewriteUndefForPHILegacyPass(*PR);
422476
initializeAMDGPUUnifyMetadataPass(*PR);
@@ -1002,6 +1056,7 @@ class GCNPassConfig final : public AMDGPUPassConfig {
10021056

10031057
FunctionPass *createSGPRAllocPass(bool Optimized);
10041058
FunctionPass *createVGPRAllocPass(bool Optimized);
1059+
FunctionPass *createWWMRegAllocPass(bool Optimized);
10051060
FunctionPass *createRegAllocPass(bool Optimized) override;
10061061

10071062
bool addRegAssignAndRewriteFast() override;
@@ -1394,7 +1449,6 @@ void GCNPassConfig::addOptimizedRegAlloc() {
13941449
}
13951450

13961451
bool GCNPassConfig::addPreRewrite() {
1397-
addPass(&SILowerWWMCopiesID);
13981452
if (EnableRegReassign)
13991453
addPass(&GCNNSAReassignID);
14001454
return true;
@@ -1430,12 +1484,28 @@ FunctionPass *GCNPassConfig::createVGPRAllocPass(bool Optimized) {
14301484
return createFastVGPRRegisterAllocator();
14311485
}
14321486

1487+
FunctionPass *GCNPassConfig::createWWMRegAllocPass(bool Optimized) {
1488+
// Initialize the global default.
1489+
llvm::call_once(InitializeDefaultWWMRegisterAllocatorFlag,
1490+
initializeDefaultWWMRegisterAllocatorOnce);
1491+
1492+
RegisterRegAlloc::FunctionPassCtor Ctor = WWMRegisterRegAlloc::getDefault();
1493+
if (Ctor != useDefaultRegisterAllocator)
1494+
return Ctor();
1495+
1496+
if (Optimized)
1497+
return createGreedyWWMRegisterAllocator();
1498+
1499+
return createFastWWMRegisterAllocator();
1500+
}
1501+
14331502
FunctionPass *GCNPassConfig::createRegAllocPass(bool Optimized) {
14341503
llvm_unreachable("should not be used");
14351504
}
14361505

14371506
static const char RegAllocOptNotSupportedMessage[] =
1438-
"-regalloc not supported with amdgcn. Use -sgpr-regalloc and -vgpr-regalloc";
1507+
"-regalloc not supported with amdgcn. Use -sgpr-regalloc, -wwm-regalloc, "
1508+
"and -vgpr-regalloc";
14391509

14401510
bool GCNPassConfig::addRegAssignAndRewriteFast() {
14411511
if (!usingDefaultRegAlloc())
@@ -1447,11 +1517,19 @@ bool GCNPassConfig::addRegAssignAndRewriteFast() {
14471517

14481518
// Equivalent of PEI for SGPRs.
14491519
addPass(&SILowerSGPRSpillsID);
1520+
1521+
// To Allocate wwm registers used in whole quad mode operations (for shaders).
14501522
addPass(&SIPreAllocateWWMRegsID);
14511523

1452-
addPass(createVGPRAllocPass(false));
1524+
// For allocating other wwm register operands.
1525+
addPass(createWWMRegAllocPass(false));
14531526

14541527
addPass(&SILowerWWMCopiesID);
1528+
addPass(&AMDGPUReserveWWMRegsID);
1529+
1530+
// For allocating regular VGPRs.
1531+
addPass(createVGPRAllocPass(false));
1532+
14551533
return true;
14561534
}
14571535

@@ -1471,8 +1549,17 @@ bool GCNPassConfig::addRegAssignAndRewriteOptimized() {
14711549

14721550
// Equivalent of PEI for SGPRs.
14731551
addPass(&SILowerSGPRSpillsID);
1552+
1553+
// To Allocate wwm registers used in whole quad mode operations (for shaders).
14741554
addPass(&SIPreAllocateWWMRegsID);
14751555

1556+
// For allocating other whole wave mode registers.
1557+
addPass(createWWMRegAllocPass(true));
1558+
addPass(&SILowerWWMCopiesID);
1559+
addPass(createVirtRegRewriter(false));
1560+
addPass(&AMDGPUReserveWWMRegsID);
1561+
1562+
// For allocating regular VGPRs.
14761563
addPass(createVGPRAllocPass(true));
14771564

14781565
addPreRewrite();

llvm/lib/Target/AMDGPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ add_llvm_target(AMDGPUCodeGen
9393
AMDGPURegBankSelect.cpp
9494
AMDGPURegisterBankInfo.cpp
9595
AMDGPURemoveIncompatibleFunctions.cpp
96+
AMDGPUReserveWWMRegs.cpp
9697
AMDGPUResourceUsageAnalysis.cpp
9798
AMDGPURewriteOutArguments.cpp
9899
AMDGPURewriteUndefForPHI.cpp

0 commit comments

Comments
 (0)