1//===- AMDGPUResourceUsageAnalysis.h ---- analysis of resources -----------===//
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/// \brief Analyzes how many registers and other resources are used by
11/// functions.
12///
13/// The results of this analysis are used to fill the register usage, flat
14/// usage, etc. into hardware registers.
15///
16//===----------------------------------------------------------------------===//
17
18#include "AMDGPUResourceUsageAnalysis.h"
19#include "AMDGPU.h"
20#include "AMDGPUTargetMachine.h"
21#include "GCNSubtarget.h"
22#include "SIMachineFunctionInfo.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/TargetPassConfig.h"
26#include "llvm/IR/GlobalValue.h"
27#include "llvm/Target/TargetMachine.h"
28
29using namespace llvm;
30using namespace llvm::AMDGPU;
31
32#define DEBUG_TYPE "amdgpu-resource-usage"
33
34char llvm::AMDGPUResourceUsageAnalysisWrapperPass::ID = 0;
35char &llvm::AMDGPUResourceUsageAnalysisID =
36 AMDGPUResourceUsageAnalysisWrapperPass::ID;
37
38INITIALIZE_PASS(AMDGPUResourceUsageAnalysisWrapperPass, DEBUG_TYPE,
39 "Function register usage analysis", true, true)
40
41static const Function *getCalleeFunction(const MachineOperand &Op) {
42 if (Op.isImm()) {
43 assert(Op.getImm() == 0);
44 return nullptr;
45 }
46 return cast<Function>(Val: Op.getGlobal()->stripPointerCastsAndAliases());
47}
48
49static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
50 const SIInstrInfo &TII, unsigned Reg) {
51 for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
52 if (!UseOp.isImplicit() || !TII.isFLAT(MI: *UseOp.getParent()))
53 return true;
54 }
55
56 return false;
57}
58
59bool AMDGPUResourceUsageAnalysisWrapperPass::runOnMachineFunction(
60 MachineFunction &MF) {
61 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
62 if (!TPC)
63 return false;
64
65 const auto &TM = TPC->getTM<GCNTargetMachine>();
66 const MCSubtargetInfo &STI = TM.getMCSubtargetInfo();
67
68 // By default, for code object v5 and later, track only the minimum scratch
69 // size
70 const AMDGPUOptions &CLOpts = TM.getCLOpts();
71 bool MinScratch = AMDGPU::getAMDHSACodeObjectVersion(
72 M: *MF.getFunction().getParent()) >= AMDGPU::AMDHSA_COV5 ||
73 STI.getTargetTriple().getOS() == Triple::AMDPAL;
74 uint32_t AssumedStackSizeForDynamicSizeObjects =
75 CLOpts.assume_dynamic_stack_object_size.value_or(u: MinScratch ? 0 : 4096);
76 uint32_t AssumedStackSizeForExternalCall =
77 CLOpts.assume_external_call_stack_size.value_or(u: MinScratch ? 0 : 16384);
78
79 ResourceInfo = AMDGPUResourceUsageAnalysisImpl().analyzeResourceUsage(
80 MF, AssumedStackSizeForDynamicSizeObjects,
81 AssumedStackSizeForExternalCall);
82
83 return false;
84}
85
86AnalysisKey AMDGPUResourceUsageAnalysis::Key;
87AMDGPUResourceUsageAnalysis::Result
88AMDGPUResourceUsageAnalysis::run(MachineFunction &MF,
89 MachineFunctionAnalysisManager &MFAM) {
90 const MCSubtargetInfo &STI = TM.getMCSubtargetInfo();
91
92 // By default, for code object v5 and later, track only the minimum scratch
93 // size
94 const AMDGPUOptions &CLOpts = TM.getCLOpts();
95 bool MinScratch = AMDGPU::getAMDHSACodeObjectVersion(
96 M: *MF.getFunction().getParent()) >= AMDGPU::AMDHSA_COV5 ||
97 STI.getTargetTriple().getOS() == Triple::AMDPAL;
98 uint32_t AssumedStackSizeForDynamicSizeObjects =
99 CLOpts.assume_dynamic_stack_object_size.value_or(u: MinScratch ? 0 : 4096);
100 uint32_t AssumedStackSizeForExternalCall =
101 CLOpts.assume_external_call_stack_size.value_or(u: MinScratch ? 0 : 16384);
102
103 return AMDGPUResourceUsageAnalysisImpl().analyzeResourceUsage(
104 MF, AssumedStackSizeForDynamicSizeObjects,
105 AssumedStackSizeForExternalCall);
106}
107
108AMDGPUResourceUsageAnalysisImpl::SIFunctionResourceInfo
109AMDGPUResourceUsageAnalysisImpl::analyzeResourceUsage(
110 const MachineFunction &MF, uint32_t AssumedStackSizeForDynamicSizeObjects,
111 uint32_t AssumedStackSizeForExternalCall) const {
112 SIFunctionResourceInfo Info;
113
114 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
115 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
116 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
117 const MachineRegisterInfo &MRI = MF.getRegInfo();
118 const SIInstrInfo *TII = ST.getInstrInfo();
119 const SIRegisterInfo &TRI = TII->getRegisterInfo();
120
121 Info.UsesFlatScratch = MRI.isPhysRegUsed(PhysReg: AMDGPU::FLAT_SCR_LO) ||
122 MRI.isPhysRegUsed(PhysReg: AMDGPU::FLAT_SCR_HI) ||
123 MRI.isLiveIn(Reg: MFI->getPreloadedReg(
124 Value: AMDGPUFunctionArgInfo::FLAT_SCRATCH_INIT));
125
126 Info.NumNamedBarrier = MFI->getNumNamedBarriers();
127
128 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
129 // instructions aren't used to access the scratch buffer. Inline assembly may
130 // need it though.
131 //
132 // If we only have implicit uses of flat_scr on flat instructions, it is not
133 // really needed.
134 if (Info.UsesFlatScratch && !MFI->getUserSGPRInfo().hasFlatScratchInit() &&
135 (!hasAnyNonFlatUseOfReg(MRI, TII: *TII, Reg: AMDGPU::FLAT_SCR) &&
136 !hasAnyNonFlatUseOfReg(MRI, TII: *TII, Reg: AMDGPU::FLAT_SCR_LO) &&
137 !hasAnyNonFlatUseOfReg(MRI, TII: *TII, Reg: AMDGPU::FLAT_SCR_HI))) {
138 Info.UsesFlatScratch = false;
139 }
140
141 Info.PrivateSegmentSize = FrameInfo.getStackSize();
142
143 // Assume a big number if there are any unknown sized objects.
144 Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
145 if (Info.HasDynamicallySizedStack)
146 Info.PrivateSegmentSize += AssumedStackSizeForDynamicSizeObjects;
147
148 if (MFI->isStackRealigned())
149 Info.PrivateSegmentSize += FrameInfo.getMaxAlign().value();
150
151 Info.UsesVCC =
152 MRI.isPhysRegUsed(PhysReg: AMDGPU::VCC_LO) || MRI.isPhysRegUsed(PhysReg: AMDGPU::VCC_HI);
153 Info.NumExplicitSGPR = TRI.getNumUsedPhysRegs(MRI, RC: AMDGPU::SGPR_32RegClass,
154 /*IncludeCalls=*/false);
155 if (ST.hasMAIInsts())
156 Info.NumAGPR = TRI.getNumUsedPhysRegs(MRI, RC: AMDGPU::AGPR_32RegClass,
157 /*IncludeCalls=*/false);
158
159 // If there are no calls, MachineRegisterInfo can tell us the used register
160 // count easily.
161 // A tail call isn't considered a call for MachineFrameInfo's purposes.
162 if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
163 Info.NumVGPR = TRI.getNumUsedPhysRegs(MRI, RC: AMDGPU::VGPR_32RegClass,
164 /*IncludeCalls=*/false);
165 return Info;
166 }
167
168 int32_t MaxVGPR = -1;
169 Info.CalleeSegmentSize = 0;
170
171 for (const MachineBasicBlock &MBB : MF) {
172 for (const MachineInstr &MI : MBB) {
173 for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
174 const MachineOperand &MO = MI.getOperand(i: I);
175
176 if (!MO.isReg())
177 continue;
178
179 Register Reg = MO.getReg();
180 switch (Reg) {
181 case AMDGPU::NoRegister:
182 assert(MI.isDebugInstr() &&
183 "Instruction uses invalid noreg register");
184 continue;
185
186 case AMDGPU::XNACK_MASK:
187 case AMDGPU::XNACK_MASK_LO:
188 case AMDGPU::XNACK_MASK_HI:
189 llvm_unreachable("xnack_mask registers should not be used");
190
191 case AMDGPU::LDS_DIRECT:
192 llvm_unreachable("lds_direct register should not be used");
193
194 case AMDGPU::TBA:
195 case AMDGPU::TBA_LO:
196 case AMDGPU::TBA_HI:
197 case AMDGPU::TMA:
198 case AMDGPU::TMA_LO:
199 case AMDGPU::TMA_HI:
200 llvm_unreachable("trap handler registers should not be used");
201
202 case AMDGPU::SRC_VCCZ:
203 llvm_unreachable("src_vccz register should not be used");
204
205 case AMDGPU::SRC_EXECZ:
206 llvm_unreachable("src_execz register should not be used");
207
208 case AMDGPU::SRC_SCC:
209 llvm_unreachable("src_scc register should not be used");
210
211 default:
212 break;
213 }
214
215 const TargetRegisterClass *RC = TRI.getPhysRegBaseClass(Reg);
216 assert((!RC || TRI.isVGPRClass(RC) || TRI.isSGPRClass(RC) ||
217 TRI.isAGPRClass(RC) || AMDGPU::TTMP_32RegClass.contains(Reg) ||
218 AMDGPU::TTMP_64RegClass.contains(Reg) ||
219 AMDGPU::TTMP_128RegClass.contains(Reg) ||
220 AMDGPU::TTMP_256RegClass.contains(Reg) ||
221 AMDGPU::TTMP_512RegClass.contains(Reg)) &&
222 "Unknown register class");
223
224 if (!RC || !TRI.isVGPRClass(RC))
225 continue;
226
227 if (MI.isCall() || MI.isMetaInstruction())
228 continue;
229
230 unsigned Width =
231 static_cast<unsigned>(divideCeil(Numerator: TRI.getRegSizeInBits(RC: *RC), Denominator: 32));
232 unsigned HWReg = TRI.getHWRegIndex(Reg);
233 int MaxUsed = HWReg + Width - 1;
234 MaxVGPR = std::max(a: MaxUsed, b: MaxVGPR);
235 }
236
237 if (MI.isCall()) {
238 // Pseudo used just to encode the underlying global. Is there a better
239 // way to track this?
240
241 // TODO: Some of the generic call-like pseudos do not encode the callee,
242 // so we overly conservatively treat this as an indirect call.
243 const MachineOperand *CalleeOp =
244 TII->getNamedOperand(MI, OperandName: AMDGPU::OpName::callee);
245
246 const Function *Callee =
247 CalleeOp ? getCalleeFunction(Op: *CalleeOp) : nullptr;
248
249 auto isSameFunction = [](const MachineFunction &MF, const Function *F) {
250 return F == &MF.getFunction();
251 };
252
253 if (Callee && !isSameFunction(MF, Callee))
254 Info.Callees.push_back(Elt: Callee);
255
256 bool IsIndirect = !Callee || Callee->isDeclaration();
257 Info.HasIndirectCall |= IsIndirect;
258
259 bool IsChainCall = MI.getOpcode() == AMDGPU::SI_TCRETURN_CHAIN;
260 Info.HasNonChainIndirectCall |= (!IsChainCall && IsIndirect);
261
262 // In object linking mode the linker has the full cross-TU view. It
263 // propagates resource usage across both direct calls to external
264 // declarations and true indirect calls. Skip the compile-time
265 // conservative assumptions so that the locally emitted metadata
266 // describes this function's own usage only.
267 if (AMDGPUTargetMachine::EnableObjectLinking)
268 continue;
269
270 // FIXME: Call site could have norecurse on it
271 if (!Callee || !Callee->doesNotRecurse()) {
272 Info.HasRecursion = true;
273
274 // TODO: If we happen to know there is no stack usage in the
275 // callgraph, we don't need to assume an infinitely growing stack.
276 if (!MI.isReturn()) {
277 // We don't need to assume an unknown stack size for tail calls.
278
279 // FIXME: This only benefits in the case where the kernel does not
280 // directly call the tail called function. If a kernel directly
281 // calls a tail recursive function, we'll assume maximum stack size
282 // based on the regular call instruction.
283 Info.CalleeSegmentSize = std::max(
284 a: Info.CalleeSegmentSize,
285 b: static_cast<uint64_t>(AssumedStackSizeForExternalCall));
286 }
287 }
288
289 if (IsIndirect) {
290 // Chain callees use the same stack frame. Exclude them.
291 if (!IsChainCall)
292 Info.CalleeSegmentSize = std::max(
293 a: Info.CalleeSegmentSize,
294 b: static_cast<uint64_t>(AssumedStackSizeForExternalCall));
295
296 // Register usage of indirect calls gets handled later
297 Info.UsesVCC = true;
298 Info.UsesFlatScratch = ST.hasFlatAddressSpace();
299 Info.HasDynamicallySizedStack = true;
300 }
301 }
302 }
303 }
304
305 Info.NumVGPR = MaxVGPR + 1;
306
307 return Info;
308}
309