blob: 12557cf1442ef44924da8d280a50528a9ea87738 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- AMDGPUSubtarget.cpp - AMDGPU Subtarget Information ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Implements the AMDGPU specific subclass of TargetSubtarget.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPUSubtarget.h"
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +000016#include "AMDGPU.h"
17#include "AMDGPUTargetMachine.h"
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +000018#include "AMDGPUCallLowering.h"
19#include "AMDGPUInstructionSelector.h"
20#include "AMDGPULegalizerInfo.h"
21#include "AMDGPURegisterBankInfo.h"
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +000022#include "SIMachineFunctionInfo.h"
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000023#include "llvm/ADT/SmallString.h"
Tom Stellard83f0bce2015-01-29 16:55:25 +000024#include "llvm/CodeGen/MachineScheduler.h"
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +000025#include "llvm/IR/MDBuilder.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000026#include "llvm/Target/TargetFrameLowering.h"
27#include <algorithm>
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000028
Tom Stellard75aadc22012-12-11 21:25:42 +000029using namespace llvm;
30
Chandler Carruthe96dd892014-04-21 22:55:11 +000031#define DEBUG_TYPE "amdgpu-subtarget"
32
Tom Stellard75aadc22012-12-11 21:25:42 +000033#define GET_SUBTARGETINFO_TARGET_DESC
34#define GET_SUBTARGETINFO_CTOR
35#include "AMDGPUGenSubtargetInfo.inc"
36
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000037AMDGPUSubtarget::~AMDGPUSubtarget() = default;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000038
Eric Christopherac4b69e2014-07-25 22:22:39 +000039AMDGPUSubtarget &
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000040AMDGPUSubtarget::initializeSubtargetDependencies(const Triple &TT,
41 StringRef GPU, StringRef FS) {
Eric Christopherac4b69e2014-07-25 22:22:39 +000042 // Determine default and user-specified characteristics
Matt Arsenaultf171cf22014-07-14 23:40:49 +000043 // On SI+, we want FP64 denormals to be on by default. FP32 denormals can be
44 // enabled, but some instructions do not respect them and they run at the
45 // double precision rate, so don't enable by default.
46 //
47 // We want to be able to turn these off, but making this a subtarget feature
48 // for SI has the unhelpful behavior that it unsets everything else if you
49 // disable it.
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000050
Matt Arsenault2fdf2a12017-02-21 23:35:48 +000051 SmallString<256> FullFS("+promote-alloca,+fp64-fp16-denormals,+dx10-clamp,+load-store-opt,");
Changpeng Fangb41574a2015-12-22 20:55:23 +000052 if (isAmdHsaOS()) // Turn on FlatForGlobal for HSA.
Matt Arsenault8728c5f2017-08-07 14:58:04 +000053 FullFS += "+flat-address-space,+flat-for-global,+unaligned-buffer-access,+trap-handler,";
Matt Arsenaulta6867fd2017-01-23 22:31:03 +000054
Matt Arsenaultd9a23ab2014-07-13 02:08:26 +000055 FullFS += FS;
56
57 ParseSubtargetFeatures(GPU, FullFS);
Tom Stellard2e59a452014-06-13 01:32:00 +000058
Matt Arsenaultd8f7ea32017-01-27 17:42:26 +000059 // Unless +-flat-for-global is specified, turn on FlatForGlobal for all OS-es
60 // on VI and newer hardware to avoid assertion failures due to missing ADDR64
61 // variants of MUBUF instructions.
62 if (!hasAddr64() && !FS.contains("flat-for-global")) {
63 FlatForGlobal = true;
64 }
65
Eric Christopherac4b69e2014-07-25 22:22:39 +000066 // FIXME: I don't think think Evergreen has any useful support for
67 // denormals, but should be checked. Should we issue a warning somewhere
68 // if someone tries to enable these?
Tom Stellard2e59a452014-06-13 01:32:00 +000069 if (getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Matt Arsenaulta6867fd2017-01-23 22:31:03 +000070 FP64FP16Denormals = false;
Matt Arsenaultf171cf22014-07-14 23:40:49 +000071 FP32Denormals = false;
Eric Christopherac4b69e2014-07-25 22:22:39 +000072 }
Matt Arsenault24ee0782016-02-12 02:40:47 +000073
74 // Set defaults if needed.
75 if (MaxPrivateElementSize == 0)
Matt Arsenaulte8ed8e52016-05-11 00:28:54 +000076 MaxPrivateElementSize = 4;
Matt Arsenault24ee0782016-02-12 02:40:47 +000077
Matt Arsenault8728c5f2017-08-07 14:58:04 +000078 if (LDSBankCount == 0)
79 LDSBankCount = 32;
80
81 if (TT.getArch() == Triple::amdgcn) {
82 if (LocalMemorySize == 0)
83 LocalMemorySize = 32768;
84
85 // Do something sensible for unspecified target.
86 if (!HasMovrel && !HasVGPRIndexMode)
87 HasMovrel = true;
88 }
89
Eric Christopherac4b69e2014-07-25 22:22:39 +000090 return *this;
91}
92
Quentin Colombet8dd90fb2017-08-08 22:22:30 +000093namespace {
94
95struct SIGISelActualAccessor : public GISelAccessor {
96 std::unique_ptr<AMDGPUCallLowering> CallLoweringInfo;
97 std::unique_ptr<InstructionSelector> InstSelector;
98 std::unique_ptr<LegalizerInfo> Legalizer;
99 std::unique_ptr<RegisterBankInfo> RegBankInfo;
100 const AMDGPUCallLowering *getCallLowering() const override {
101 return CallLoweringInfo.get();
102 }
103 const InstructionSelector *getInstructionSelector() const override {
104 return InstSelector.get();
105 }
106 const LegalizerInfo *getLegalizerInfo() const override {
107 return Legalizer.get();
108 }
109 const RegisterBankInfo *getRegBankInfo() const override {
110 return RegBankInfo.get();
111 }
112};
113
114} // end anonymous namespace
115
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000116AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000117 const TargetMachine &TM)
118 : AMDGPUGenSubtargetInfo(TT, GPU, FS),
119 TargetTriple(TT),
120 Gen(TT.getArch() == Triple::amdgcn ? SOUTHERN_ISLANDS : R600),
121 IsaVersion(ISAVersion0_0_0),
122 WavefrontSize(64),
123 LocalMemorySize(0),
124 LDSBankCount(0),
125 MaxPrivateElementSize(0),
Tom Stellard40ce8af2015-01-28 16:04:26 +0000126
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000127 FastFMAF32(false),
128 HalfRate64Ops(false),
129
130 FP32Denormals(false),
Matt Arsenaulta6867fd2017-01-23 22:31:03 +0000131 FP64FP16Denormals(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000132 FPExceptions(false),
Matt Arsenault2fdf2a12017-02-21 23:35:48 +0000133 DX10Clamp(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000134 FlatForGlobal(false),
Konstantin Zhuravlyovbe6c0ca2017-06-02 17:40:26 +0000135 AutoWaitcntBeforeBarrier(false),
Tom Stellard64a9d082016-10-14 18:10:39 +0000136 UnalignedScratchAccess(false),
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000137 UnalignedBufferAccess(false),
138
Matt Arsenaulte823d922017-02-18 18:29:53 +0000139 HasApertureRegs(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000140 EnableXNACK(false),
Wei Ding205bfdb2017-02-10 02:15:29 +0000141 TrapHandler(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000142 DebuggerInsertNops(false),
143 DebuggerReserveRegs(false),
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000144 DebuggerEmitPrologue(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000145
146 EnableVGPRSpilling(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000147 EnablePromoteAlloca(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000148 EnableLoadStoreOpt(false),
149 EnableUnsafeDSOffsetFolding(false),
150 EnableSIScheduler(false),
151 DumpCode(false),
152
153 FP64(false),
154 IsGCN(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000155 GCN3Encoding(false),
156 CIInsts(false),
Matt Arsenault2021f082017-02-18 19:12:26 +0000157 GFX9Insts(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000158 SGPRInitBug(false),
159 HasSMemRealTime(false),
160 Has16BitInsts(false),
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000161 HasVOP3PInsts(false),
Matt Arsenaultcc88ce32016-10-12 18:00:51 +0000162 HasMovrel(false),
163 HasVGPRIndexMode(false),
Matt Arsenaultc88ba362016-10-29 04:05:06 +0000164 HasScalarStores(false),
Benjamin Kramer11590b82017-01-20 10:37:53 +0000165 HasInv2PiInlineImm(false),
Sam Kolton07dbde22017-01-20 10:01:25 +0000166 HasSDWA(false),
Sam Kolton3c4933f2017-06-22 06:26:41 +0000167 HasSDWAOmod(false),
168 HasSDWAScalar(false),
169 HasSDWASdst(false),
170 HasSDWAMac(false),
Sam Koltona179d252017-06-27 15:02:23 +0000171 HasSDWAOutModsVOPC(false),
Sam Kolton07dbde22017-01-20 10:01:25 +0000172 HasDPP(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000173 FlatAddressSpace(false),
Matt Arsenaultacdc7652017-05-10 21:19:05 +0000174 FlatInstOffsets(false),
175 FlatGlobalInsts(false),
176 FlatScratchInsts(false),
Matt Arsenaultc37fe662017-07-20 17:42:47 +0000177 AddNoCarryInsts(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000178
179 R600ALUInst(false),
180 CaymanISA(false),
181 CFALUBug(false),
182 HasVertexCache(false),
183 TexVTXClauseSize(0),
Alexander Timofeev18009562016-12-08 17:28:47 +0000184 ScalarizeGlobal(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000185
186 FeatureDisable(false),
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000187 InstrItins(getInstrItineraryForCPU(GPU)) {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000188 AS = AMDGPU::getAMDGPUAS(TT);
Tom Stellard40ce8af2015-01-28 16:04:26 +0000189 initializeSubtargetDependencies(TT, GPU, FS);
Tom Stellarda40f9712014-01-22 21:55:43 +0000190}
Tom Stellardb8fd6ef2014-12-02 22:00:07 +0000191
Stanislav Mekhanoshin2b913b12017-02-01 22:59:50 +0000192unsigned AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves,
193 const Function &F) const {
194 if (NWaves == 1)
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000195 return getLocalMemorySize();
Stanislav Mekhanoshin2b913b12017-02-01 22:59:50 +0000196 unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second;
197 unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize);
198 unsigned MaxWaves = getMaxWavesPerEU();
199 return getLocalMemorySize() * MaxWaves / WorkGroupsPerCu / NWaves;
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000200}
201
Stanislav Mekhanoshin2b913b12017-02-01 22:59:50 +0000202unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes,
203 const Function &F) const {
204 unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second;
205 unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize);
206 unsigned MaxWaves = getMaxWavesPerEU();
207 unsigned Limit = getLocalMemorySize() * MaxWaves / WorkGroupsPerCu;
208 unsigned NumWaves = Limit / (Bytes ? Bytes : 1u);
209 NumWaves = std::min(NumWaves, MaxWaves);
210 NumWaves = std::max(NumWaves, 1u);
211 return NumWaves;
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000212}
213
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000214std::pair<unsigned, unsigned> AMDGPUSubtarget::getFlatWorkGroupSizes(
215 const Function &F) const {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000216 // Default minimum/maximum flat work group sizes.
217 std::pair<unsigned, unsigned> Default =
218 AMDGPU::isCompute(F.getCallingConv()) ?
219 std::pair<unsigned, unsigned>(getWavefrontSize() * 2,
220 getWavefrontSize() * 4) :
221 std::pair<unsigned, unsigned>(1, getWavefrontSize());
222
223 // TODO: Do not process "amdgpu-max-work-group-size" attribute once mesa
224 // starts using "amdgpu-flat-work-group-size" attribute.
225 Default.second = AMDGPU::getIntegerAttribute(
226 F, "amdgpu-max-work-group-size", Default.second);
227 Default.first = std::min(Default.first, Default.second);
228
229 // Requested minimum/maximum flat work group sizes.
230 std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
231 F, "amdgpu-flat-work-group-size", Default);
232
233 // Make sure requested minimum is less than requested maximum.
234 if (Requested.first > Requested.second)
235 return Default;
236
237 // Make sure requested values do not violate subtarget's specifications.
238 if (Requested.first < getMinFlatWorkGroupSize())
239 return Default;
240 if (Requested.second > getMaxFlatWorkGroupSize())
241 return Default;
242
243 return Requested;
244}
245
246std::pair<unsigned, unsigned> AMDGPUSubtarget::getWavesPerEU(
247 const Function &F) const {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000248 // Default minimum/maximum number of waves per execution unit.
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000249 std::pair<unsigned, unsigned> Default(1, getMaxWavesPerEU());
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000250
251 // Default/requested minimum/maximum flat work group sizes.
252 std::pair<unsigned, unsigned> FlatWorkGroupSizes = getFlatWorkGroupSizes(F);
253
254 // If minimum/maximum flat work group sizes were explicitly requested using
255 // "amdgpu-flat-work-group-size" attribute, then set default minimum/maximum
256 // number of waves per execution unit to values implied by requested
257 // minimum/maximum flat work group sizes.
258 unsigned MinImpliedByFlatWorkGroupSize =
259 getMaxWavesPerEU(FlatWorkGroupSizes.second);
260 bool RequestedFlatWorkGroupSize = false;
261
262 // TODO: Do not process "amdgpu-max-work-group-size" attribute once mesa
263 // starts using "amdgpu-flat-work-group-size" attribute.
264 if (F.hasFnAttribute("amdgpu-max-work-group-size") ||
265 F.hasFnAttribute("amdgpu-flat-work-group-size")) {
266 Default.first = MinImpliedByFlatWorkGroupSize;
267 RequestedFlatWorkGroupSize = true;
268 }
269
270 // Requested minimum/maximum number of waves per execution unit.
271 std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
272 F, "amdgpu-waves-per-eu", Default, true);
273
274 // Make sure requested minimum is less than requested maximum.
275 if (Requested.second && Requested.first > Requested.second)
276 return Default;
277
278 // Make sure requested values do not violate subtarget's specifications.
279 if (Requested.first < getMinWavesPerEU() ||
280 Requested.first > getMaxWavesPerEU())
281 return Default;
282 if (Requested.second > getMaxWavesPerEU())
283 return Default;
284
285 // Make sure requested values are compatible with values implied by requested
286 // minimum/maximum flat work group sizes.
287 if (RequestedFlatWorkGroupSize &&
Konstantin Zhuravlyov2ec725c2017-07-16 19:38:47 +0000288 Requested.first < MinImpliedByFlatWorkGroupSize)
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000289 return Default;
290
291 return Requested;
292}
293
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000294bool AMDGPUSubtarget::makeLIDRangeMetadata(Instruction *I) const {
295 Function *Kernel = I->getParent()->getParent();
296 unsigned MinSize = 0;
297 unsigned MaxSize = getFlatWorkGroupSizes(*Kernel).second;
298 bool IdQuery = false;
299
300 // If reqd_work_group_size is present it narrows value down.
301 if (auto *CI = dyn_cast<CallInst>(I)) {
302 const Function *F = CI->getCalledFunction();
303 if (F) {
304 unsigned Dim = UINT_MAX;
305 switch (F->getIntrinsicID()) {
306 case Intrinsic::amdgcn_workitem_id_x:
307 case Intrinsic::r600_read_tidig_x:
308 IdQuery = true;
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000309 LLVM_FALLTHROUGH;
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000310 case Intrinsic::r600_read_local_size_x:
311 Dim = 0;
312 break;
313 case Intrinsic::amdgcn_workitem_id_y:
314 case Intrinsic::r600_read_tidig_y:
315 IdQuery = true;
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000316 LLVM_FALLTHROUGH;
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000317 case Intrinsic::r600_read_local_size_y:
318 Dim = 1;
319 break;
320 case Intrinsic::amdgcn_workitem_id_z:
321 case Intrinsic::r600_read_tidig_z:
322 IdQuery = true;
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000323 LLVM_FALLTHROUGH;
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000324 case Intrinsic::r600_read_local_size_z:
325 Dim = 2;
326 break;
327 default:
328 break;
329 }
330 if (Dim <= 3) {
331 if (auto Node = Kernel->getMetadata("reqd_work_group_size"))
332 if (Node->getNumOperands() == 3)
333 MinSize = MaxSize = mdconst::extract<ConstantInt>(
334 Node->getOperand(Dim))->getZExtValue();
335 }
336 }
337 }
338
339 if (!MaxSize)
340 return false;
341
342 // Range metadata is [Lo, Hi). For ID query we need to pass max size
343 // as Hi. For size query we need to pass Hi + 1.
344 if (IdQuery)
345 MinSize = 0;
346 else
347 ++MaxSize;
348
349 MDBuilder MDB(I->getContext());
350 MDNode *MaxWorkGroupSizeRange = MDB.createRange(APInt(32, MinSize),
351 APInt(32, MaxSize));
352 I->setMetadata(LLVMContext::MD_range, MaxWorkGroupSizeRange);
353 return true;
354}
355
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000356R600Subtarget::R600Subtarget(const Triple &TT, StringRef GPU, StringRef FS,
357 const TargetMachine &TM) :
358 AMDGPUSubtarget(TT, GPU, FS, TM),
359 InstrInfo(*this),
360 FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0),
361 TLInfo(TM, *this) {}
362
363SISubtarget::SISubtarget(const Triple &TT, StringRef GPU, StringRef FS,
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +0000364 const TargetMachine &TM)
365 : AMDGPUSubtarget(TT, GPU, FS, TM), InstrInfo(*this),
366 FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0),
367 TLInfo(TM, *this) {
Quentin Colombet8dd90fb2017-08-08 22:22:30 +0000368 SIGISelActualAccessor *GISel = new SIGISelActualAccessor();
369 GISel->CallLoweringInfo.reset(new AMDGPUCallLowering(*getTargetLowering()));
370 GISel->Legalizer.reset(new AMDGPULegalizerInfo());
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +0000371
Quentin Colombet8dd90fb2017-08-08 22:22:30 +0000372 GISel->RegBankInfo.reset(new AMDGPURegisterBankInfo(*getRegisterInfo()));
373 GISel->InstSelector.reset(new AMDGPUInstructionSelector(
374 *this, *static_cast<AMDGPURegisterBankInfo *>(GISel->RegBankInfo.get())));
375 setGISelAccessor(*GISel);
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +0000376}
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000377
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000378void SISubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
Matt Arsenault55dff272016-06-28 00:11:26 +0000379 unsigned NumRegionInstrs) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000380 // Track register pressure so the scheduler can try to decrease
381 // pressure once register usage is above the threshold defined by
382 // SIRegisterInfo::getRegPressureSetLimit()
383 Policy.ShouldTrackPressure = true;
Tom Stellard83f0bce2015-01-29 16:55:25 +0000384
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000385 // Enabling both top down and bottom up scheduling seems to give us less
386 // register spills than just using one of these approaches on its own.
387 Policy.OnlyTopDown = false;
388 Policy.OnlyBottomUp = false;
Tom Stellard83f0bce2015-01-29 16:55:25 +0000389
Alexander Timofeev9f61fea2017-02-14 14:29:05 +0000390 // Enabling ShouldTrackLaneMasks crashes the SI Machine Scheduler.
391 if (!enableSIScheduler())
392 Policy.ShouldTrackLaneMasks = true;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000393}
Tom Stellard0bc954e2016-03-30 16:35:09 +0000394
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000395bool SISubtarget::isVGPRSpillingEnabled(const Function& F) const {
396 return EnableVGPRSpilling || !AMDGPU::isShader(F.getCallingConv());
397}
Tom Stellard0d23ebe2016-08-29 19:42:52 +0000398
Tom Stellard2f3f9852017-01-25 01:25:13 +0000399unsigned SISubtarget::getKernArgSegmentSize(const MachineFunction &MF,
Konstantin Zhuravlyov27d64c32017-02-08 13:29:23 +0000400 unsigned ExplicitArgBytes) const {
Tom Stellard2f3f9852017-01-25 01:25:13 +0000401 unsigned ImplicitBytes = getImplicitArgNumBytes(MF);
Tom Stellarde88bbc32016-09-23 01:33:26 +0000402 if (ImplicitBytes == 0)
403 return ExplicitArgBytes;
404
405 unsigned Alignment = getAlignmentForImplicitArgPtr();
406 return alignTo(ExplicitArgBytes, Alignment) + ImplicitBytes;
407}
408
Tom Stellard0d23ebe2016-08-29 19:42:52 +0000409unsigned SISubtarget::getOccupancyWithNumSGPRs(unsigned SGPRs) const {
410 if (getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
411 if (SGPRs <= 80)
412 return 10;
413 if (SGPRs <= 88)
414 return 9;
415 if (SGPRs <= 100)
416 return 8;
417 return 7;
418 }
419 if (SGPRs <= 48)
420 return 10;
421 if (SGPRs <= 56)
422 return 9;
423 if (SGPRs <= 64)
424 return 8;
425 if (SGPRs <= 72)
426 return 7;
427 if (SGPRs <= 80)
428 return 6;
429 return 5;
430}
431
432unsigned SISubtarget::getOccupancyWithNumVGPRs(unsigned VGPRs) const {
433 if (VGPRs <= 24)
434 return 10;
435 if (VGPRs <= 28)
436 return 9;
437 if (VGPRs <= 32)
438 return 8;
439 if (VGPRs <= 36)
440 return 7;
441 if (VGPRs <= 40)
442 return 6;
443 if (VGPRs <= 48)
444 return 5;
445 if (VGPRs <= 64)
446 return 4;
447 if (VGPRs <= 84)
448 return 3;
449 if (VGPRs <= 128)
450 return 2;
451 return 1;
452}
Matt Arsenault4eae3012016-10-28 20:31:47 +0000453
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000454unsigned SISubtarget::getReservedNumSGPRs(const MachineFunction &MF) const {
455 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
456 if (MFI.hasFlatScratchInit()) {
457 if (getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
458 return 6; // FLAT_SCRATCH, XNACK, VCC (in that order).
459 if (getGeneration() == AMDGPUSubtarget::SEA_ISLANDS)
460 return 4; // FLAT_SCRATCH, VCC (in that order).
461 }
462
463 if (isXNACKEnabled())
464 return 4; // XNACK, VCC (in that order).
465 return 2; // VCC.
466}
467
468unsigned SISubtarget::getMaxNumSGPRs(const MachineFunction &MF) const {
469 const Function &F = *MF.getFunction();
470 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
471
472 // Compute maximum number of SGPRs function can use using default/requested
473 // minimum number of waves per execution unit.
474 std::pair<unsigned, unsigned> WavesPerEU = MFI.getWavesPerEU();
475 unsigned MaxNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, false);
476 unsigned MaxAddressableNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, true);
477
478 // Check if maximum number of SGPRs was explicitly requested using
479 // "amdgpu-num-sgpr" attribute.
480 if (F.hasFnAttribute("amdgpu-num-sgpr")) {
481 unsigned Requested = AMDGPU::getIntegerAttribute(
482 F, "amdgpu-num-sgpr", MaxNumSGPRs);
483
484 // Make sure requested value does not violate subtarget's specifications.
485 if (Requested && (Requested <= getReservedNumSGPRs(MF)))
486 Requested = 0;
487
488 // If more SGPRs are required to support the input user/system SGPRs,
489 // increase to accommodate them.
490 //
491 // FIXME: This really ends up using the requested number of SGPRs + number
492 // of reserved special registers in total. Theoretically you could re-use
493 // the last input registers for these special registers, but this would
494 // require a lot of complexity to deal with the weird aliasing.
495 unsigned InputNumSGPRs = MFI.getNumPreloadedSGPRs();
496 if (Requested && Requested < InputNumSGPRs)
497 Requested = InputNumSGPRs;
498
499 // Make sure requested value is compatible with values implied by
500 // default/requested minimum/maximum number of waves per execution unit.
501 if (Requested && Requested > getMaxNumSGPRs(WavesPerEU.first, false))
502 Requested = 0;
503 if (WavesPerEU.second &&
504 Requested && Requested < getMinNumSGPRs(WavesPerEU.second))
505 Requested = 0;
506
507 if (Requested)
508 MaxNumSGPRs = Requested;
509 }
510
Matt Arsenault4eae3012016-10-28 20:31:47 +0000511 if (hasSGPRInitBug())
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000512 MaxNumSGPRs = AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Matt Arsenault4eae3012016-10-28 20:31:47 +0000513
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000514 return std::min(MaxNumSGPRs - getReservedNumSGPRs(MF),
515 MaxAddressableNumSGPRs);
516}
Matt Arsenault4eae3012016-10-28 20:31:47 +0000517
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000518unsigned SISubtarget::getMaxNumVGPRs(const MachineFunction &MF) const {
519 const Function &F = *MF.getFunction();
520 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
521
522 // Compute maximum number of VGPRs function can use using default/requested
523 // minimum number of waves per execution unit.
524 std::pair<unsigned, unsigned> WavesPerEU = MFI.getWavesPerEU();
525 unsigned MaxNumVGPRs = getMaxNumVGPRs(WavesPerEU.first);
526
527 // Check if maximum number of VGPRs was explicitly requested using
528 // "amdgpu-num-vgpr" attribute.
529 if (F.hasFnAttribute("amdgpu-num-vgpr")) {
530 unsigned Requested = AMDGPU::getIntegerAttribute(
531 F, "amdgpu-num-vgpr", MaxNumVGPRs);
532
533 // Make sure requested value does not violate subtarget's specifications.
534 if (Requested && Requested <= getReservedNumVGPRs(MF))
535 Requested = 0;
536
537 // Make sure requested value is compatible with values implied by
538 // default/requested minimum/maximum number of waves per execution unit.
539 if (Requested && Requested > getMaxNumVGPRs(WavesPerEU.first))
540 Requested = 0;
541 if (WavesPerEU.second &&
542 Requested && Requested < getMinNumVGPRs(WavesPerEU.second))
543 Requested = 0;
544
545 if (Requested)
546 MaxNumVGPRs = Requested;
547 }
548
549 return MaxNumVGPRs - getReservedNumVGPRs(MF);
Matt Arsenault4eae3012016-10-28 20:31:47 +0000550}