Skip to content

Commit ca32bd6

Browse files
authored
[NewPM][AMDGPU] Port SIPreAllocateWWMRegs to NPM (#109939)
1 parent f8cb526 commit ca32bd6

File tree

6 files changed

+93
-27
lines changed

6 files changed

+93
-27
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ FunctionPass *createSIFixSGPRCopiesLegacyPass();
4949
FunctionPass *createLowerWWMCopiesPass();
5050
FunctionPass *createSIMemoryLegalizerPass();
5151
FunctionPass *createSIInsertWaitcntsPass();
52-
FunctionPass *createSIPreAllocateWWMRegsPass();
52+
FunctionPass *createSIPreAllocateWWMRegsLegacyPass();
5353
FunctionPass *createSIFormMemoryClausesPass();
5454

5555
FunctionPass *createSIPostRABundlerPass();
@@ -212,8 +212,8 @@ extern char &SILateBranchLoweringPassID;
212212
void initializeSIOptimizeExecMaskingPass(PassRegistry &);
213213
extern char &SIOptimizeExecMaskingID;
214214

215-
void initializeSIPreAllocateWWMRegsPass(PassRegistry &);
216-
extern char &SIPreAllocateWWMRegsID;
215+
void initializeSIPreAllocateWWMRegsLegacyPass(PassRegistry &);
216+
extern char &SIPreAllocateWWMRegsLegacyID;
217217

218218
void initializeAMDGPUImageIntrinsicOptimizerPass(PassRegistry &);
219219
extern char &AMDGPUImageIntrinsicOptimizerID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@ MACHINE_FUNCTION_PASS("gcn-dpp-combine", GCNDPPCombinePass())
102102
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
103103
MACHINE_FUNCTION_PASS("si-lower-sgpr-spills", SILowerSGPRSpillsPass())
104104
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())
105+
MACHINE_FUNCTION_PASS("si-pre-allocate-wwm-regs", SIPreAllocateWWMRegsPass())
105106
MACHINE_FUNCTION_PASS("si-shrink-instructions", SIShrinkInstructionsPass())
106107
#undef MACHINE_FUNCTION_PASS

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "SIMachineFunctionInfo.h"
4242
#include "SIMachineScheduler.h"
4343
#include "SIPeepholeSDWA.h"
44+
#include "SIPreAllocateWWMRegs.h"
4445
#include "SIShrinkInstructions.h"
4546
#include "TargetInfo/AMDGPUTargetInfo.h"
4647
#include "Utils/AMDGPUBaseInfo.h"
@@ -508,7 +509,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
508509
initializeSILateBranchLoweringPass(*PR);
509510
initializeSIMemoryLegalizerPass(*PR);
510511
initializeSIOptimizeExecMaskingPass(*PR);
511-
initializeSIPreAllocateWWMRegsPass(*PR);
512+
initializeSIPreAllocateWWMRegsLegacyPass(*PR);
512513
initializeSIFormMemoryClausesPass(*PR);
513514
initializeSIPostRABundlerPass(*PR);
514515
initializeGCNCreateVOPDPass(*PR);
@@ -1506,7 +1507,7 @@ bool GCNPassConfig::addRegAssignAndRewriteFast() {
15061507
addPass(&SILowerSGPRSpillsLegacyID);
15071508

15081509
// To Allocate wwm registers used in whole quad mode operations (for shaders).
1509-
addPass(&SIPreAllocateWWMRegsID);
1510+
addPass(&SIPreAllocateWWMRegsLegacyID);
15101511

15111512
// For allocating other wwm register operands.
15121513
addPass(createWWMRegAllocPass(false));
@@ -1543,7 +1544,7 @@ bool GCNPassConfig::addRegAssignAndRewriteOptimized() {
15431544
addPass(&SILowerSGPRSpillsLegacyID);
15441545

15451546
// To Allocate wwm registers used in whole quad mode operations (for shaders).
1546-
addPass(&SIPreAllocateWWMRegsID);
1547+
addPass(&SIPreAllocateWWMRegsLegacyID);
15471548

15481549
// For allocating other whole wave mode registers.
15491550
addPass(createWWMRegAllocPass(true));

llvm/lib/Target/AMDGPU/SIPreAllocateWWMRegs.cpp

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "SIPreAllocateWWMRegs.h"
1415
#include "AMDGPU.h"
1516
#include "GCNSubtarget.h"
1617
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
@@ -34,7 +35,7 @@ static cl::opt<bool>
3435

3536
namespace {
3637

37-
class SIPreAllocateWWMRegs : public MachineFunctionPass {
38+
class SIPreAllocateWWMRegs {
3839
private:
3940
const SIInstrInfo *TII;
4041
const SIRegisterInfo *TRI;
@@ -48,13 +49,21 @@ class SIPreAllocateWWMRegs : public MachineFunctionPass {
4849
#ifndef NDEBUG
4950
void printWWMInfo(const MachineInstr &MI);
5051
#endif
52+
bool processDef(MachineOperand &MO);
53+
void rewriteRegs(MachineFunction &MF);
54+
55+
public:
56+
SIPreAllocateWWMRegs(LiveIntervals *LIS, LiveRegMatrix *Matrix,
57+
VirtRegMap *VRM)
58+
: LIS(LIS), Matrix(Matrix), VRM(VRM) {}
59+
bool run(MachineFunction &MF);
60+
};
5161

62+
class SIPreAllocateWWMRegsLegacy : public MachineFunctionPass {
5263
public:
5364
static char ID;
5465

55-
SIPreAllocateWWMRegs() : MachineFunctionPass(ID) {
56-
initializeSIPreAllocateWWMRegsPass(*PassRegistry::getPassRegistry());
57-
}
66+
SIPreAllocateWWMRegsLegacy() : MachineFunctionPass(ID) {}
5867

5968
bool runOnMachineFunction(MachineFunction &MF) override;
6069

@@ -65,28 +74,24 @@ class SIPreAllocateWWMRegs : public MachineFunctionPass {
6574
AU.setPreservesAll();
6675
MachineFunctionPass::getAnalysisUsage(AU);
6776
}
68-
69-
private:
70-
bool processDef(MachineOperand &MO);
71-
void rewriteRegs(MachineFunction &MF);
7277
};
7378

7479
} // End anonymous namespace.
7580

76-
INITIALIZE_PASS_BEGIN(SIPreAllocateWWMRegs, DEBUG_TYPE,
77-
"SI Pre-allocate WWM Registers", false, false)
81+
INITIALIZE_PASS_BEGIN(SIPreAllocateWWMRegsLegacy, DEBUG_TYPE,
82+
"SI Pre-allocate WWM Registers", false, false)
7883
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
7984
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
8085
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
81-
INITIALIZE_PASS_END(SIPreAllocateWWMRegs, DEBUG_TYPE,
82-
"SI Pre-allocate WWM Registers", false, false)
86+
INITIALIZE_PASS_END(SIPreAllocateWWMRegsLegacy, DEBUG_TYPE,
87+
"SI Pre-allocate WWM Registers", false, false)
8388

84-
char SIPreAllocateWWMRegs::ID = 0;
89+
char SIPreAllocateWWMRegsLegacy::ID = 0;
8590

86-
char &llvm::SIPreAllocateWWMRegsID = SIPreAllocateWWMRegs::ID;
91+
char &llvm::SIPreAllocateWWMRegsLegacyID = SIPreAllocateWWMRegsLegacy::ID;
8792

88-
FunctionPass *llvm::createSIPreAllocateWWMRegsPass() {
89-
return new SIPreAllocateWWMRegs();
93+
FunctionPass *llvm::createSIPreAllocateWWMRegsLegacyPass() {
94+
return new SIPreAllocateWWMRegsLegacy();
9095
}
9196

9297
bool SIPreAllocateWWMRegs::processDef(MachineOperand &MO) {
@@ -184,7 +189,14 @@ SIPreAllocateWWMRegs::printWWMInfo(const MachineInstr &MI) {
184189

185190
#endif
186191

187-
bool SIPreAllocateWWMRegs::runOnMachineFunction(MachineFunction &MF) {
192+
bool SIPreAllocateWWMRegsLegacy::runOnMachineFunction(MachineFunction &MF) {
193+
auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
194+
auto *Matrix = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
195+
auto *VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
196+
return SIPreAllocateWWMRegs(LIS, Matrix, VRM).run(MF);
197+
}
198+
199+
bool SIPreAllocateWWMRegs::run(MachineFunction &MF) {
188200
LLVM_DEBUG(dbgs() << "SIPreAllocateWWMRegs: function " << MF.getName() << "\n");
189201

190202
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
@@ -193,10 +205,6 @@ bool SIPreAllocateWWMRegs::runOnMachineFunction(MachineFunction &MF) {
193205
TRI = &TII->getRegisterInfo();
194206
MRI = &MF.getRegInfo();
195207

196-
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
197-
Matrix = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
198-
VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
199-
200208
RegClassInfo.runOnMachineFunction(MF);
201209

202210
bool PreallocateSGPRSpillVGPRs =
@@ -251,3 +259,13 @@ bool SIPreAllocateWWMRegs::runOnMachineFunction(MachineFunction &MF) {
251259
rewriteRegs(MF);
252260
return true;
253261
}
262+
263+
PreservedAnalyses
264+
SIPreAllocateWWMRegsPass::run(MachineFunction &MF,
265+
MachineFunctionAnalysisManager &MFAM) {
266+
auto *LIS = &MFAM.getResult<LiveIntervalsAnalysis>(MF);
267+
auto *Matrix = &MFAM.getResult<LiveRegMatrixAnalysis>(MF);
268+
auto *VRM = &MFAM.getResult<VirtRegMapAnalysis>(MF);
269+
SIPreAllocateWWMRegs(LIS, Matrix, VRM).run(MF);
270+
return PreservedAnalyses::all();
271+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===--- SIPreAllocateWWMRegs.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_SIPREALLOCATEWWMREGS_H
10+
#define LLVM_LIB_TARGET_AMDGPU_SIPREALLOCATEWWMREGS_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class SIPreAllocateWWMRegsPass
17+
: public PassInfoMixin<SIPreAllocateWWMRegsPass> {
18+
public:
19+
PreservedAnalyses run(MachineFunction &MF,
20+
MachineFunctionAnalysisManager &MFAM);
21+
};
22+
23+
} // namespace llvm
24+
25+
#endif // LLVM_LIB_TARGET_AMDGPU_SIPREALLOCATEWWMREGS_H

llvm/test/CodeGen/AMDGPU/si-pre-allocate-wwm-regs.mir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
# RUN: llc -mtriple=amdgcn -mcpu=tahiti -verify-machineinstrs -run-pass=si-pre-allocate-wwm-regs -o - %s | FileCheck %s
33
# RUN: llc -mtriple=amdgcn -mcpu=tahiti -verify-machineinstrs -amdgpu-prealloc-sgpr-spill-vgprs -run-pass=si-pre-allocate-wwm-regs -o - %s | FileCheck %s --check-prefix=CHECK2
44

5+
# RUN: llc -mtriple=amdgcn -passes=si-pre-allocate-wwm-regs -o - -mcpu=tahiti %s | FileCheck %s
6+
# RUN: llc -mtriple=amdgcn -verify-machineinstrs -amdgpu-prealloc-sgpr-spill-vgprs -passes=si-pre-allocate-wwm-regs -o - -mcpu=tahiti %s | FileCheck %s --check-prefix=CHECK2
7+
58
# COM: auto-generated updates might remove checks for MachineFunctionInfo reserved registers.
9+
610
---
711

812
name: pre_allocate_wwm_regs_strict
@@ -21,6 +25,16 @@ body: |
2125
; CHECK-NEXT: dead $vgpr0 = V_MOV_B32_dpp $vgpr0, [[DEF]], 323, 12, 15, 0, implicit $exec
2226
; CHECK-NEXT: $exec = EXIT_STRICT_WWM killed renamable $sgpr4_sgpr5
2327
; CHECK-NEXT: dead [[COPY:%[0-9]+]]:vgpr_32 = COPY [[DEF]]
28+
;
29+
; CHECK2-LABEL: name: pre_allocate_wwm_regs_strict
30+
; CHECK2: liveins: $sgpr1
31+
; CHECK2-NEXT: {{ $}}
32+
; CHECK2-NEXT: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
33+
; CHECK2-NEXT: renamable $sgpr4_sgpr5 = ENTER_STRICT_WWM -1, implicit-def $exec, implicit-def $scc, implicit $exec
34+
; CHECK2-NEXT: $vgpr0 = V_MOV_B32_e32 0, implicit $exec
35+
; CHECK2-NEXT: dead $vgpr0 = V_MOV_B32_dpp $vgpr0, [[DEF]], 323, 12, 15, 0, implicit $exec
36+
; CHECK2-NEXT: $exec = EXIT_STRICT_WWM killed renamable $sgpr4_sgpr5
37+
; CHECK2-NEXT: dead [[COPY:%[0-9]+]]:vgpr_32 = COPY [[DEF]]
2438
%0:vgpr_32 = IMPLICIT_DEF
2539
renamable $sgpr4_sgpr5 = ENTER_STRICT_WWM -1, implicit-def $exec, implicit-def $scc, implicit $exec
2640
%1:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
@@ -35,6 +49,13 @@ tracksRegLiveness: true
3549
body: |
3650
bb.0:
3751
liveins: $sgpr1
52+
; CHECK-LABEL: name: pre_allocate_wwm_spill_to_vgpr
53+
; CHECK: liveins: $sgpr1
54+
; CHECK-NEXT: {{ $}}
55+
; CHECK-NEXT: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
56+
; CHECK-NEXT: dead [[SI_SPILL_S32_TO_VGPR:%[0-9]+]]:vgpr_32 = SI_SPILL_S32_TO_VGPR $sgpr1, 0, [[DEF]]
57+
; CHECK-NEXT: dead [[COPY:%[0-9]+]]:vgpr_32 = COPY [[DEF]]
58+
;
3859
; CHECK2-LABEL: name: pre_allocate_wwm_spill_to_vgpr
3960
; CHECK2: wwmReservedRegs:
4061
; CHECK2-NEXT: - '$vgpr0'

0 commit comments

Comments
 (0)