Skip to content

Commit 0b08747

Browse files
authored
[AMDGPU][NewPM] Port SILowerSGPRSpills to NPM (#108934)
1 parent b84d773 commit 0b08747

File tree

8 files changed

+75
-20
lines changed

8 files changed

+75
-20
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ extern char &AMDGPUGlobalISelDivergenceLoweringID;
187187
void initializeAMDGPUMarkLastScratchLoadPass(PassRegistry &);
188188
extern char &AMDGPUMarkLastScratchLoadID;
189189

190-
void initializeSILowerSGPRSpillsPass(PassRegistry &);
191-
extern char &SILowerSGPRSpillsID;
190+
void initializeSILowerSGPRSpillsLegacyPass(PassRegistry &);
191+
extern char &SILowerSGPRSpillsLegacyID;
192192

193193
void initializeSILoadStoreOptimizerLegacyPass(PassRegistry &);
194194
extern char &SILoadStoreOptimizerLegacyID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ MACHINE_FUNCTION_PASS("si-i1-copies", SILowerI1CopiesPass())
100100
MACHINE_FUNCTION_PASS("si-fold-operands", SIFoldOperandsPass());
101101
MACHINE_FUNCTION_PASS("gcn-dpp-combine", GCNDPPCombinePass())
102102
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
103+
MACHINE_FUNCTION_PASS("si-lower-sgpr-spills", SILowerSGPRSpillsPass())
103104
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())
104105
MACHINE_FUNCTION_PASS("si-shrink-instructions", SIShrinkInstructionsPass())
105106
#undef MACHINE_FUNCTION_PASS

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "SIFixSGPRCopies.h"
3838
#include "SIFoldOperands.h"
3939
#include "SILoadStoreOptimizer.h"
40+
#include "SILowerSGPRSpills.h"
4041
#include "SIMachineFunctionInfo.h"
4142
#include "SIMachineScheduler.h"
4243
#include "SIPeepholeSDWA.h"
@@ -413,7 +414,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
413414
initializeAMDGPUGlobalISelDivergenceLoweringPass(*PR);
414415
initializeSILowerWWMCopiesPass(*PR);
415416
initializeAMDGPUMarkLastScratchLoadPass(*PR);
416-
initializeSILowerSGPRSpillsPass(*PR);
417+
initializeSILowerSGPRSpillsLegacyPass(*PR);
417418
initializeSIFixSGPRCopiesLegacyPass(*PR);
418419
initializeSIFixVGPRCopiesPass(*PR);
419420
initializeSIFoldOperandsLegacyPass(*PR);
@@ -1441,7 +1442,7 @@ bool GCNPassConfig::addRegAssignAndRewriteFast() {
14411442
addPass(createSGPRAllocPass(false));
14421443

14431444
// Equivalent of PEI for SGPRs.
1444-
addPass(&SILowerSGPRSpillsID);
1445+
addPass(&SILowerSGPRSpillsLegacyID);
14451446
addPass(&SIPreAllocateWWMRegsID);
14461447

14471448
addPass(createVGPRAllocPass(false));
@@ -1465,7 +1466,7 @@ bool GCNPassConfig::addRegAssignAndRewriteOptimized() {
14651466
addPass(createVirtRegRewriter(false));
14661467

14671468
// Equivalent of PEI for SGPRs.
1468-
addPass(&SILowerSGPRSpillsID);
1469+
addPass(&SILowerSGPRSpillsLegacyID);
14691470
addPass(&SIPreAllocateWWMRegsID);
14701471

14711472
addPass(createVGPRAllocPass(true));

llvm/lib/Target/AMDGPU/SILowerSGPRSpills.cpp

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

18+
#include "SILowerSGPRSpills.h"
1819
#include "AMDGPU.h"
1920
#include "GCNSubtarget.h"
2021
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
@@ -32,7 +33,7 @@ using MBBVector = SmallVector<MachineBasicBlock *, 4>;
3233

3334
namespace {
3435

35-
class SILowerSGPRSpills : public MachineFunctionPass {
36+
class SILowerSGPRSpills {
3637
private:
3738
const SIRegisterInfo *TRI = nullptr;
3839
const SIInstrInfo *TII = nullptr;
@@ -45,14 +46,20 @@ class SILowerSGPRSpills : public MachineFunctionPass {
4546
MBBVector RestoreBlocks;
4647

4748
public:
48-
static char ID;
49-
50-
SILowerSGPRSpills() : MachineFunctionPass(ID) {}
51-
49+
SILowerSGPRSpills(LiveIntervals *LIS, SlotIndexes *Indexes)
50+
: LIS(LIS), Indexes(Indexes) {}
51+
bool run(MachineFunction &MF);
5252
void calculateSaveRestoreBlocks(MachineFunction &MF);
5353
bool spillCalleeSavedRegs(MachineFunction &MF,
5454
SmallVectorImpl<int> &CalleeSavedFIs);
5555
void extendWWMVirtRegLiveness(MachineFunction &MF, LiveIntervals *LIS);
56+
};
57+
58+
class SILowerSGPRSpillsLegacy : public MachineFunctionPass {
59+
public:
60+
static char ID;
61+
62+
SILowerSGPRSpillsLegacy() : MachineFunctionPass(ID) {}
5663

5764
bool runOnMachineFunction(MachineFunction &MF) override;
5865

@@ -71,16 +78,16 @@ class SILowerSGPRSpills : public MachineFunctionPass {
7178

7279
} // end anonymous namespace
7380

74-
char SILowerSGPRSpills::ID = 0;
81+
char SILowerSGPRSpillsLegacy::ID = 0;
7582

76-
INITIALIZE_PASS_BEGIN(SILowerSGPRSpills, DEBUG_TYPE,
83+
INITIALIZE_PASS_BEGIN(SILowerSGPRSpillsLegacy, DEBUG_TYPE,
7784
"SI lower SGPR spill instructions", false, false)
7885
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
7986
INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
80-
INITIALIZE_PASS_END(SILowerSGPRSpills, DEBUG_TYPE,
87+
INITIALIZE_PASS_END(SILowerSGPRSpillsLegacy, DEBUG_TYPE,
8188
"SI lower SGPR spill instructions", false, false)
8289

83-
char &llvm::SILowerSGPRSpillsID = SILowerSGPRSpills::ID;
90+
char &llvm::SILowerSGPRSpillsLegacyID = SILowerSGPRSpillsLegacy::ID;
8491

8592
/// Insert spill code for the callee-saved registers used in the function.
8693
static void insertCSRSaves(MachineBasicBlock &SaveBlock,
@@ -306,16 +313,19 @@ void SILowerSGPRSpills::extendWWMVirtRegLiveness(MachineFunction &MF,
306313
}
307314
}
308315

309-
bool SILowerSGPRSpills::runOnMachineFunction(MachineFunction &MF) {
316+
bool SILowerSGPRSpillsLegacy::runOnMachineFunction(MachineFunction &MF) {
317+
auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
318+
LiveIntervals *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
319+
auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
320+
SlotIndexes *Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
321+
return SILowerSGPRSpills(LIS, Indexes).run(MF);
322+
}
323+
324+
bool SILowerSGPRSpills::run(MachineFunction &MF) {
310325
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
311326
TII = ST.getInstrInfo();
312327
TRI = &TII->getRegisterInfo();
313328

314-
auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
315-
LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
316-
auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
317-
Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
318-
319329
assert(SaveBlocks.empty() && RestoreBlocks.empty());
320330

321331
// First, expose any CSR SGPR spills. This is mostly the same as what PEI
@@ -446,3 +456,13 @@ bool SILowerSGPRSpills::runOnMachineFunction(MachineFunction &MF) {
446456

447457
return MadeChange;
448458
}
459+
460+
PreservedAnalyses
461+
SILowerSGPRSpillsPass::run(MachineFunction &MF,
462+
MachineFunctionAnalysisManager &MFAM) {
463+
MFPropsModifier _(*this, MF);
464+
auto *LIS = MFAM.getCachedResult<LiveIntervalsAnalysis>(MF);
465+
auto *Indexes = MFAM.getCachedResult<SlotIndexesAnalysis>(MF);
466+
SILowerSGPRSpills(LIS, Indexes).run(MF);
467+
return PreservedAnalyses::all();
468+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- SILowerSGPRSpills.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_SILOWERSGPRSPILLS_H
10+
#define LLVM_LIB_TARGET_AMDGPU_SILOWERSGPRSPILLS_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
class SILowerSGPRSpillsPass : public PassInfoMixin<SILowerSGPRSpillsPass> {
16+
public:
17+
PreservedAnalyses run(MachineFunction &MF,
18+
MachineFunctionAnalysisManager &MFAM);
19+
20+
MachineFunctionProperties getClearedProperties() {
21+
// SILowerSGPRSpills introduces new Virtual VGPRs for spilling SGPRs.
22+
return MachineFunctionProperties()
23+
.set(MachineFunctionProperties::Property::IsSSA)
24+
.set(MachineFunctionProperties::Property::NoVRegs);
25+
}
26+
};
27+
} // namespace llvm
28+
29+
#endif // LLVM_LIB_TARGET_AMDGPU_SILOWERSGPRSPILLS_H

llvm/test/CodeGen/AMDGPU/sgpr-spill-dead-frame-in-dbg-value.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -amdgpu-spill-sgpr-to-vgpr=true -verify-machineinstrs -run-pass=si-lower-sgpr-spills -o - %s | FileCheck -check-prefix=SGPR_SPILL %s
22
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -amdgpu-spill-sgpr-to-vgpr=true -verify-machineinstrs --start-before=si-lower-sgpr-spills --stop-after=prologepilog -o - %s | FileCheck -check-prefix=PEI %s
3+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -amdgpu-spill-sgpr-to-vgpr=true -passes=si-lower-sgpr-spills -o - %s | FileCheck -check-prefix=SGPR_SPILL %s
34

45
# After handling the SGPR spill to VGPR in SILowerSGPRSpills pass, replace the dead frame index in the DBG_VALUE instruction with reg 0.
56
# Otherwise, the test would crash during PEI while trying to replace the dead frame index.

llvm/test/CodeGen/AMDGPU/sgpr-spill-fi-skip-processing-stack-arg-dbg-value.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -amdgpu-spill-sgpr-to-vgpr=true -verify-machineinstrs -run-pass=si-lower-sgpr-spills -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -amdgpu-spill-sgpr-to-vgpr=true -passes=si-lower-sgpr-spills -o - %s | FileCheck %s
23

34
# After handling the SGPR spill to VGPR in SILowerSGPRSpills pass, we replace the dead frame index in the DBG_VALUE instruction with reg 0.
45
# Skip looking for frame indices in the debug value instruction for incoming arguments passed via stack. The test would crash otherwise.

llvm/test/CodeGen/AMDGPU/spill192.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# RUN: llc -mtriple=amdgcn -mcpu=tahiti -passes=regallocfast -o - %s | FileCheck -check-prefix=SPILLED %s
44
# RUN: llc -mtriple=amdgcn -mcpu=tahiti -run-pass=regallocfast,si-lower-sgpr-spills -o - %s | FileCheck -check-prefix=EXPANDED %s
55

6+
# RUN: llc -mtriple=amdgcn -mcpu=tahiti -passes=regallocfast,si-lower-sgpr-spills -o - %s | FileCheck -check-prefix=EXPANDED %s
7+
68
# Make sure spill/restore of 192 bit registers works. We have to
79
# settle for a MIR test for now since inlineasm fails without 192-bit
810
# MVT.

0 commit comments

Comments
 (0)