LLVM 20.0.0git
SILowerWWMCopies.cpp
Go to the documentation of this file.
1//===-- SILowerWWMCopies.cpp - Lower Copies after regalloc ---===//
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/// Lowering the WWM_COPY instructions for various register classes.
11/// AMDGPU target generates WWM_COPY instruction to differentiate WWM
12/// copy from COPY. This pass generates the necessary exec mask manipulation
13/// instructions to replicate 'Whole Wave Mode' and lowers WWM_COPY back to
14/// COPY.
15//
16//===----------------------------------------------------------------------===//
17
18#include "SILowerWWMCopies.h"
19#include "AMDGPU.h"
20#include "GCNSubtarget.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "si-lower-wwm-copies"
31
32namespace {
33
34class SILowerWWMCopies {
35public:
36 SILowerWWMCopies(LiveIntervals *LIS, SlotIndexes *SI, VirtRegMap *VRM)
37 : LIS(LIS), Indexes(SI), VRM(VRM) {}
38 bool run(MachineFunction &MF);
39
40private:
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;
50};
51
52class SILowerWWMCopiesLegacy : public MachineFunctionPass {
53public:
54 static char ID;
55
56 SILowerWWMCopiesLegacy() : MachineFunctionPass(ID) {
58 }
59
60 bool runOnMachineFunction(MachineFunction &MF) override;
61
62 StringRef getPassName() const override { return "SI Lower WWM Copies"; }
63
64 void getAnalysisUsage(AnalysisUsage &AU) const override {
68 AU.setPreservesAll();
70 }
71};
72
73} // End anonymous namespace.
74
75INITIALIZE_PASS_BEGIN(SILowerWWMCopiesLegacy, DEBUG_TYPE, "SI Lower WWM Copies",
76 false, false)
79INITIALIZE_PASS_END(SILowerWWMCopiesLegacy, DEBUG_TYPE, "SI Lower WWM Copies",
81
82char SILowerWWMCopiesLegacy::ID = 0;
83
84char &llvm::SILowerWWMCopiesLegacyID = SILowerWWMCopiesLegacy::ID;
85
86bool SILowerWWMCopies::isSCCLiveAtMI(const MachineInstr &MI) {
87 // We can't determine the liveness info if LIS isn't available. Early return
88 // in that case and always assume SCC is live.
89 if (!LIS)
90 return true;
91
92 LiveRange &LR =
93 LIS->getRegUnit(*MCRegUnitIterator(MCRegister::from(AMDGPU::SCC), TRI));
94 SlotIndex Idx = LIS->getInstructionIndex(MI);
95 return LR.liveAt(Idx);
96}
97
98// If \p Reg is assigned with a physical VGPR, add the latter into wwm-spills
99// for preserving its entire lanes at function prolog/epilog.
100void SILowerWWMCopies::addToWWMSpills(MachineFunction &MF, Register Reg) {
101 if (Reg.isPhysical())
102 return;
103
104 // FIXME: VRM may be null here.
105 MCRegister PhysReg = VRM->getPhys(Reg);
106 assert(PhysReg && "should have allocated a physical register");
107
108 MFI->allocateWWMSpill(MF, PhysReg);
109}
110
111bool 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
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
137bool SILowerWWMCopies::run(MachineFunction &MF) {
138 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
139 const SIInstrInfo *TII = ST.getInstrInfo();
140
141 MFI = MF.getInfo<SIMachineFunctionInfo>();
142 TRI = ST.getRegisterInfo();
143 MRI = &MF.getRegInfo();
144
145 if (!MFI->hasVRegFlags())
146 return false;
147
148 bool Changed = false;
149 for (MachineBasicBlock &MBB : MF) {
150 for (MachineInstr &MI : MBB) {
151 if (MI.getOpcode() != AMDGPU::WWM_COPY)
152 continue;
153
154 // TODO: Club adjacent WWM ops between same exec save/restore
155 assert(TII->isVGPRCopy(MI));
156
157 // For WWM vector copies, manipulate the exec mask around the copy
158 // instruction.
159 const DebugLoc &DL = MI.getDebugLoc();
160 MachineBasicBlock::iterator InsertPt = MI.getIterator();
161 Register RegForExecCopy = MFI->getSGPRForEXECCopy();
162 TII->insertScratchExecCopy(MF, MBB, InsertPt, DL, RegForExecCopy,
163 isSCCLiveAtMI(MI), Indexes);
164 TII->restoreExec(MF, MBB, ++InsertPt, DL, RegForExecCopy, Indexes);
165 addToWWMSpills(MF, MI.getOperand(0).getReg());
166 LLVM_DEBUG(dbgs() << "WWM copy manipulation for " << MI);
167
168 // Lower WWM_COPY back to COPY
169 MI.setDesc(TII->get(AMDGPU::COPY));
170 Changed |= true;
171 }
172 }
173
174 return Changed;
175}
unsigned const MachineRegisterInfo * MRI
aarch64 promote const
Provides AMDGPU specific target descriptions.
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(...)
Definition: Debug.h:106
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SI Lower WWM Copies
#define DEBUG_TYPE
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:429
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
A debug info location.
Definition: DebugLoc.h:33
This class represents the liveness of a register, stack slot, etc.
Definition: LiveInterval.h:157
bool liveAt(SlotIndex index) const
Definition: LiveInterval.h:401
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
static MCRegister from(unsigned Val)
Check the provided unsigned value is a valid MCRegister.
Definition: MCRegister.h:78
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:65
SlotIndexes pass.
Definition: SlotIndexes.h:297
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeSILowerWWMCopiesLegacyPass(PassRegistry &)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
char & SILowerWWMCopiesLegacyID