Skip to content

Commit c5ab28a

Browse files
authored
[AMDGPU][NewPM] Port SIOptimizeVGPRLiveRange pass to NPM. (#117686)
1 parent eb42e94 commit c5ab28a

8 files changed

+92
-21
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ FunctionPass *createSILoadStoreOptimizerLegacyPass();
4242
FunctionPass *createSIWholeQuadModePass();
4343
FunctionPass *createSIFixControlFlowLiveIntervalsPass();
4444
FunctionPass *createSIOptimizeExecMaskingPreRAPass();
45-
FunctionPass *createSIOptimizeVGPRLiveRangePass();
45+
FunctionPass *createSIOptimizeVGPRLiveRangeLegacyPass();
4646
FunctionPass *createSIFixSGPRCopiesLegacyPass();
4747
FunctionPass *createLowerWWMCopiesPass();
4848
FunctionPass *createSIMemoryLegalizerPass();
@@ -359,8 +359,8 @@ struct AMDGPUUnifyMetadataPass : PassInfoMixin<AMDGPUUnifyMetadataPass> {
359359
void initializeSIOptimizeExecMaskingPreRAPass(PassRegistry&);
360360
extern char &SIOptimizeExecMaskingPreRAID;
361361

362-
void initializeSIOptimizeVGPRLiveRangePass(PassRegistry &);
363-
extern char &SIOptimizeVGPRLiveRangeID;
362+
void initializeSIOptimizeVGPRLiveRangeLegacyPass(PassRegistry &);
363+
extern char &SIOptimizeVGPRLiveRangeLegacyID;
364364

365365
void initializeAMDGPUAnnotateUniformValuesLegacyPass(PassRegistry &);
366366
extern char &AMDGPUAnnotateUniformValuesLegacyPassID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ MACHINE_FUNCTION_PASS("si-fold-operands", SIFoldOperandsPass());
101101
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())
104+
MACHINE_FUNCTION_PASS("si-opt-vgpr-liverange", SIOptimizeVGPRLiveRangePass())
104105
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())
105106
MACHINE_FUNCTION_PASS("si-pre-allocate-wwm-regs", SIPreAllocateWWMRegsPass())
106107
MACHINE_FUNCTION_PASS("si-shrink-instructions", SIShrinkInstructionsPass())

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "SILowerSGPRSpills.h"
4040
#include "SIMachineFunctionInfo.h"
4141
#include "SIMachineScheduler.h"
42+
#include "SIOptimizeVGPRLiveRange.h"
4243
#include "SIPeepholeSDWA.h"
4344
#include "SIPreAllocateWWMRegs.h"
4445
#include "SIShrinkInstructions.h"
@@ -472,7 +473,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
472473
initializeSIPeepholeSDWALegacyPass(*PR);
473474
initializeSIShrinkInstructionsLegacyPass(*PR);
474475
initializeSIOptimizeExecMaskingPreRAPass(*PR);
475-
initializeSIOptimizeVGPRLiveRangePass(*PR);
476+
initializeSIOptimizeVGPRLiveRangeLegacyPass(*PR);
476477
initializeSILoadStoreOptimizerLegacyPass(*PR);
477478
initializeAMDGPUCtorDtorLoweringLegacyPass(*PR);
478479
initializeAMDGPUAlwaysInlinePass(*PR);
@@ -1421,7 +1422,7 @@ void GCNPassConfig::addOptimizedRegAlloc() {
14211422
// the register in LiveVariables, this would trigger a failure in verifier,
14221423
// we should fix it and enable the verifier.
14231424
if (OptVGPRLiveRange)
1424-
insertPass(&LiveVariablesID, &SIOptimizeVGPRLiveRangeID);
1425+
insertPass(&LiveVariablesID, &SIOptimizeVGPRLiveRangeLegacyID);
14251426

14261427
// This must be run immediately after phi elimination and before
14271428
// TwoAddressInstructions, otherwise the processing of the tied operand of

llvm/lib/Target/AMDGPU/SIOptimizeVGPRLiveRange.cpp

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
//
7272
//===----------------------------------------------------------------------===//
7373

74+
#include "SIOptimizeVGPRLiveRange.h"
7475
#include "AMDGPU.h"
7576
#include "GCNSubtarget.h"
7677
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
@@ -86,7 +87,7 @@ using namespace llvm;
8687

8788
namespace {
8889

89-
class SIOptimizeVGPRLiveRange : public MachineFunctionPass {
90+
class SIOptimizeVGPRLiveRange {
9091
private:
9192
const SIRegisterInfo *TRI = nullptr;
9293
const SIInstrInfo *TII = nullptr;
@@ -96,7 +97,10 @@ class SIOptimizeVGPRLiveRange : public MachineFunctionPass {
9697
MachineRegisterInfo *MRI = nullptr;
9798

9899
public:
99-
static char ID;
100+
SIOptimizeVGPRLiveRange(LiveVariables *LV, MachineDominatorTree *MDT,
101+
MachineLoopInfo *Loops)
102+
: LV(LV), MDT(MDT), Loops(Loops) {}
103+
bool run(MachineFunction &MF);
100104

101105
MachineBasicBlock *getElseTarget(MachineBasicBlock *MBB) const;
102106

@@ -136,8 +140,13 @@ class SIOptimizeVGPRLiveRange : public MachineFunctionPass {
136140
Register Reg, MachineBasicBlock *LoopHeader,
137141
SmallSetVector<MachineBasicBlock *, 2> &LoopBlocks,
138142
SmallVectorImpl<MachineInstr *> &Instructions) const;
143+
};
139144

140-
SIOptimizeVGPRLiveRange() : MachineFunctionPass(ID) {}
145+
class SIOptimizeVGPRLiveRangeLegacy : public MachineFunctionPass {
146+
public:
147+
static char ID;
148+
149+
SIOptimizeVGPRLiveRangeLegacy() : MachineFunctionPass(ID) {}
141150

142151
bool runOnMachineFunction(MachineFunction &MF) override;
143152

@@ -611,35 +620,59 @@ void SIOptimizeVGPRLiveRange::optimizeWaterfallLiveRange(
611620
}
612621
}
613622

614-
char SIOptimizeVGPRLiveRange::ID = 0;
623+
char SIOptimizeVGPRLiveRangeLegacy::ID = 0;
615624

616-
INITIALIZE_PASS_BEGIN(SIOptimizeVGPRLiveRange, DEBUG_TYPE,
625+
INITIALIZE_PASS_BEGIN(SIOptimizeVGPRLiveRangeLegacy, DEBUG_TYPE,
617626
"SI Optimize VGPR LiveRange", false, false)
618627
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
619628
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
620629
INITIALIZE_PASS_DEPENDENCY(LiveVariablesWrapperPass)
621-
INITIALIZE_PASS_END(SIOptimizeVGPRLiveRange, DEBUG_TYPE,
630+
INITIALIZE_PASS_END(SIOptimizeVGPRLiveRangeLegacy, DEBUG_TYPE,
622631
"SI Optimize VGPR LiveRange", false, false)
623632

624-
char &llvm::SIOptimizeVGPRLiveRangeID = SIOptimizeVGPRLiveRange::ID;
633+
char &llvm::SIOptimizeVGPRLiveRangeLegacyID = SIOptimizeVGPRLiveRangeLegacy::ID;
634+
635+
FunctionPass *llvm::createSIOptimizeVGPRLiveRangeLegacyPass() {
636+
return new SIOptimizeVGPRLiveRangeLegacy();
637+
}
638+
639+
bool SIOptimizeVGPRLiveRangeLegacy::runOnMachineFunction(MachineFunction &MF) {
640+
if (skipFunction(MF.getFunction()))
641+
return false;
625642

626-
FunctionPass *llvm::createSIOptimizeVGPRLiveRangePass() {
627-
return new SIOptimizeVGPRLiveRange();
643+
LiveVariables *LV = &getAnalysis<LiveVariablesWrapperPass>().getLV();
644+
MachineDominatorTree *MDT =
645+
&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
646+
MachineLoopInfo *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
647+
return SIOptimizeVGPRLiveRange(LV, MDT, Loops).run(MF);
628648
}
629649

630-
bool SIOptimizeVGPRLiveRange::runOnMachineFunction(MachineFunction &MF) {
650+
PreservedAnalyses
651+
SIOptimizeVGPRLiveRangePass::run(MachineFunction &MF,
652+
MachineFunctionAnalysisManager &MFAM) {
653+
MFPropsModifier _(*this, MF);
654+
LiveVariables *LV = &MFAM.getResult<LiveVariablesAnalysis>(MF);
655+
MachineDominatorTree *MDT = &MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
656+
MachineLoopInfo *Loops = &MFAM.getResult<MachineLoopAnalysis>(MF);
657+
658+
bool Changed = SIOptimizeVGPRLiveRange(LV, MDT, Loops).run(MF);
659+
if (!Changed)
660+
return PreservedAnalyses::all();
661+
662+
auto PA = getMachineFunctionPassPreservedAnalyses();
663+
PA.preserve<LiveVariablesAnalysis>();
664+
PA.preserve<DominatorTreeAnalysis>();
665+
PA.preserve<MachineLoopAnalysis>();
666+
PA.preserveSet<CFGAnalyses>();
667+
return PA;
668+
}
631669

670+
bool SIOptimizeVGPRLiveRange::run(MachineFunction &MF) {
632671
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
633672
TII = ST.getInstrInfo();
634673
TRI = &TII->getRegisterInfo();
635-
MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
636-
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
637-
LV = &getAnalysis<LiveVariablesWrapperPass>().getLV();
638674
MRI = &MF.getRegInfo();
639675

640-
if (skipFunction(MF.getFunction()))
641-
return false;
642-
643676
bool MadeChange = false;
644677

645678
// TODO: we need to think about the order of visiting the blocks to get
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===- SIOptimizeVGPRLiveRange.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_SIOPTIMIZEVGPRLIVERANGE_H
10+
#define LLVM_LIB_TARGET_AMDGPU_SIOPTIMIZEVGPRLIVERANGE_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
class SIOptimizeVGPRLiveRangePass
16+
: public PassInfoMixin<SIOptimizeVGPRLiveRangePass> {
17+
public:
18+
PreservedAnalyses run(MachineFunction &MF,
19+
MachineFunctionAnalysisManager &MFAM);
20+
21+
MachineFunctionProperties getRequiredProperties() const {
22+
return MachineFunctionProperties().set(
23+
MachineFunctionProperties::Property::IsSSA);
24+
}
25+
26+
MachineFunctionProperties getClearedProperties() const {
27+
return MachineFunctionProperties().set(
28+
MachineFunctionProperties::Property::NoPHIs);
29+
}
30+
};
31+
} // namespace llvm
32+
33+
#endif // LLVM_LIB_TARGET_AMDGPU_SIOPTIMIZEVGPRLIVERANGE_H

llvm/test/CodeGen/AMDGPU/opt-vgpr-live-range-verifier-error.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=gfx90a -verify-machineinstrs -run-pass=si-opt-vgpr-liverange -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -passes=si-opt-vgpr-liverange -o - %s | FileCheck %s
23
#
34
# This is a very rare case which comes from llvm-reduce. The SI_IF/SI_ELSE usage is quite different from normal.
45
#

llvm/test/CodeGen/AMDGPU/si-opt-vgpr-liverange-bug-deadlanes.mir

Lines changed: 1 addition & 0 deletions
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 2
22
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -verify-machineinstrs -run-pass=si-opt-vgpr-liverange -o - %s | FileCheck -check-prefixes=CHECK %s
3+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -passes=si-opt-vgpr-liverange -o - %s | FileCheck -check-prefixes=CHECK %s
34

45
# Tests a case that used to assert in SIOptimizeVGPRLiveRange when trying to optimize %3 which still appears
56
# (though in an undef operand) in the REG_SEQUENCE of the "endif block". This undef pattern was caused by

llvm/test/CodeGen/AMDGPU/si-optimize-vgpr-live-range-dbg-instr.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 -run-pass=si-opt-vgpr-liverange %s -o - | FileCheck -check-prefix=GCN %s
2+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -passes=si-opt-vgpr-liverange %s -o - | FileCheck -check-prefix=GCN %s
23

34
# SIOptimizeVGPRLiveRange shouldn't try to modify use of %5 in DBG_VALUE_LIST
45

0 commit comments

Comments
 (0)