blob: 59f9baf9af04f83520fa956601877a6b1d547b8b [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
Daniel Sandersa73f1fd2015-06-10 12:11:26 +000093AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +000094 const TargetMachine &TM)
95 : AMDGPUGenSubtargetInfo(TT, GPU, FS),
96 TargetTriple(TT),
97 Gen(TT.getArch() == Triple::amdgcn ? SOUTHERN_ISLANDS : R600),
98 IsaVersion(ISAVersion0_0_0),
99 WavefrontSize(64),
100 LocalMemorySize(0),
101 LDSBankCount(0),
102 MaxPrivateElementSize(0),
Tom Stellard40ce8af2015-01-28 16:04:26 +0000103
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000104 FastFMAF32(false),
105 HalfRate64Ops(false),
106
107 FP32Denormals(false),
Matt Arsenaulta6867fd2017-01-23 22:31:03 +0000108 FP64FP16Denormals(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000109 FPExceptions(false),
Matt Arsenault2fdf2a12017-02-21 23:35:48 +0000110 DX10Clamp(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000111 FlatForGlobal(false),
Konstantin Zhuravlyovbe6c0ca2017-06-02 17:40:26 +0000112 AutoWaitcntBeforeBarrier(false),
Tom Stellard64a9d082016-10-14 18:10:39 +0000113 UnalignedScratchAccess(false),
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000114 UnalignedBufferAccess(false),
115
Matt Arsenaulte823d922017-02-18 18:29:53 +0000116 HasApertureRegs(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000117 EnableXNACK(false),
Wei Ding205bfdb2017-02-10 02:15:29 +0000118 TrapHandler(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000119 DebuggerInsertNops(false),
120 DebuggerReserveRegs(false),
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000121 DebuggerEmitPrologue(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000122
123 EnableVGPRSpilling(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000124 EnablePromoteAlloca(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000125 EnableLoadStoreOpt(false),
126 EnableUnsafeDSOffsetFolding(false),
127 EnableSIScheduler(false),
128 DumpCode(false),
129
130 FP64(false),
131 IsGCN(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000132 GCN3Encoding(false),
133 CIInsts(false),
Matt Arsenault2021f082017-02-18 19:12:26 +0000134 GFX9Insts(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000135 SGPRInitBug(false),
136 HasSMemRealTime(false),
137 Has16BitInsts(false),
Dmitry Preobrazhenskyff64aa52017-08-16 13:51:56 +0000138 HasIntClamp(false),
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000139 HasVOP3PInsts(false),
Matt Arsenaultcc88ce32016-10-12 18:00:51 +0000140 HasMovrel(false),
141 HasVGPRIndexMode(false),
Matt Arsenaultc88ba362016-10-29 04:05:06 +0000142 HasScalarStores(false),
Benjamin Kramer11590b82017-01-20 10:37:53 +0000143 HasInv2PiInlineImm(false),
Sam Kolton07dbde22017-01-20 10:01:25 +0000144 HasSDWA(false),
Sam Kolton3c4933f2017-06-22 06:26:41 +0000145 HasSDWAOmod(false),
146 HasSDWAScalar(false),
147 HasSDWASdst(false),
148 HasSDWAMac(false),
Sam Koltona179d252017-06-27 15:02:23 +0000149 HasSDWAOutModsVOPC(false),
Sam Kolton07dbde22017-01-20 10:01:25 +0000150 HasDPP(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000151 FlatAddressSpace(false),
Matt Arsenaultacdc7652017-05-10 21:19:05 +0000152 FlatInstOffsets(false),
153 FlatGlobalInsts(false),
154 FlatScratchInsts(false),
Matt Arsenaultc37fe662017-07-20 17:42:47 +0000155 AddNoCarryInsts(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000156
157 R600ALUInst(false),
158 CaymanISA(false),
159 CFALUBug(false),
160 HasVertexCache(false),
161 TexVTXClauseSize(0),
Alexander Timofeev18009562016-12-08 17:28:47 +0000162 ScalarizeGlobal(false),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000163
164 FeatureDisable(false),
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000165 InstrItins(getInstrItineraryForCPU(GPU)) {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000166 AS = AMDGPU::getAMDGPUAS(TT);
Tom Stellard40ce8af2015-01-28 16:04:26 +0000167 initializeSubtargetDependencies(TT, GPU, FS);
Tom Stellarda40f9712014-01-22 21:55:43 +0000168}
Tom Stellardb8fd6ef2014-12-02 22:00:07 +0000169
Stanislav Mekhanoshin2b913b12017-02-01 22:59:50 +0000170unsigned AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves,
171 const Function &F) const {
172 if (NWaves == 1)
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000173 return getLocalMemorySize();
Stanislav Mekhanoshin2b913b12017-02-01 22:59:50 +0000174 unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second;
175 unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize);
176 unsigned MaxWaves = getMaxWavesPerEU();
177 return getLocalMemorySize() * MaxWaves / WorkGroupsPerCu / NWaves;
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000178}
179
Stanislav Mekhanoshin2b913b12017-02-01 22:59:50 +0000180unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes,
181 const Function &F) const {
182 unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second;
183 unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize);
184 unsigned MaxWaves = getMaxWavesPerEU();
185 unsigned Limit = getLocalMemorySize() * MaxWaves / WorkGroupsPerCu;
186 unsigned NumWaves = Limit / (Bytes ? Bytes : 1u);
187 NumWaves = std::min(NumWaves, MaxWaves);
188 NumWaves = std::max(NumWaves, 1u);
189 return NumWaves;
Matt Arsenault8a028bf2016-05-16 21:19:59 +0000190}
191
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000192std::pair<unsigned, unsigned> AMDGPUSubtarget::getFlatWorkGroupSizes(
193 const Function &F) const {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000194 // Default minimum/maximum flat work group sizes.
195 std::pair<unsigned, unsigned> Default =
196 AMDGPU::isCompute(F.getCallingConv()) ?
197 std::pair<unsigned, unsigned>(getWavefrontSize() * 2,
198 getWavefrontSize() * 4) :
199 std::pair<unsigned, unsigned>(1, getWavefrontSize());
200
201 // TODO: Do not process "amdgpu-max-work-group-size" attribute once mesa
202 // starts using "amdgpu-flat-work-group-size" attribute.
203 Default.second = AMDGPU::getIntegerAttribute(
204 F, "amdgpu-max-work-group-size", Default.second);
205 Default.first = std::min(Default.first, Default.second);
206
207 // Requested minimum/maximum flat work group sizes.
208 std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
209 F, "amdgpu-flat-work-group-size", Default);
210
211 // Make sure requested minimum is less than requested maximum.
212 if (Requested.first > Requested.second)
213 return Default;
214
215 // Make sure requested values do not violate subtarget's specifications.
216 if (Requested.first < getMinFlatWorkGroupSize())
217 return Default;
218 if (Requested.second > getMaxFlatWorkGroupSize())
219 return Default;
220
221 return Requested;
222}
223
224std::pair<unsigned, unsigned> AMDGPUSubtarget::getWavesPerEU(
225 const Function &F) const {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000226 // Default minimum/maximum number of waves per execution unit.
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000227 std::pair<unsigned, unsigned> Default(1, getMaxWavesPerEU());
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000228
229 // Default/requested minimum/maximum flat work group sizes.
230 std::pair<unsigned, unsigned> FlatWorkGroupSizes = getFlatWorkGroupSizes(F);
231
232 // If minimum/maximum flat work group sizes were explicitly requested using
233 // "amdgpu-flat-work-group-size" attribute, then set default minimum/maximum
234 // number of waves per execution unit to values implied by requested
235 // minimum/maximum flat work group sizes.
236 unsigned MinImpliedByFlatWorkGroupSize =
237 getMaxWavesPerEU(FlatWorkGroupSizes.second);
238 bool RequestedFlatWorkGroupSize = false;
239
240 // TODO: Do not process "amdgpu-max-work-group-size" attribute once mesa
241 // starts using "amdgpu-flat-work-group-size" attribute.
242 if (F.hasFnAttribute("amdgpu-max-work-group-size") ||
243 F.hasFnAttribute("amdgpu-flat-work-group-size")) {
244 Default.first = MinImpliedByFlatWorkGroupSize;
245 RequestedFlatWorkGroupSize = true;
246 }
247
248 // Requested minimum/maximum number of waves per execution unit.
249 std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
250 F, "amdgpu-waves-per-eu", Default, true);
251
252 // Make sure requested minimum is less than requested maximum.
253 if (Requested.second && Requested.first > Requested.second)
254 return Default;
255
256 // Make sure requested values do not violate subtarget's specifications.
257 if (Requested.first < getMinWavesPerEU() ||
258 Requested.first > getMaxWavesPerEU())
259 return Default;
260 if (Requested.second > getMaxWavesPerEU())
261 return Default;
262
263 // Make sure requested values are compatible with values implied by requested
264 // minimum/maximum flat work group sizes.
265 if (RequestedFlatWorkGroupSize &&
Konstantin Zhuravlyov2ec725c2017-07-16 19:38:47 +0000266 Requested.first < MinImpliedByFlatWorkGroupSize)
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000267 return Default;
268
269 return Requested;
270}
271
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000272bool AMDGPUSubtarget::makeLIDRangeMetadata(Instruction *I) const {
273 Function *Kernel = I->getParent()->getParent();
274 unsigned MinSize = 0;
275 unsigned MaxSize = getFlatWorkGroupSizes(*Kernel).second;
276 bool IdQuery = false;
277
278 // If reqd_work_group_size is present it narrows value down.
279 if (auto *CI = dyn_cast<CallInst>(I)) {
280 const Function *F = CI->getCalledFunction();
281 if (F) {
282 unsigned Dim = UINT_MAX;
283 switch (F->getIntrinsicID()) {
284 case Intrinsic::amdgcn_workitem_id_x:
285 case Intrinsic::r600_read_tidig_x:
286 IdQuery = true;
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000287 LLVM_FALLTHROUGH;
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000288 case Intrinsic::r600_read_local_size_x:
289 Dim = 0;
290 break;
291 case Intrinsic::amdgcn_workitem_id_y:
292 case Intrinsic::r600_read_tidig_y:
293 IdQuery = true;
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000294 LLVM_FALLTHROUGH;
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000295 case Intrinsic::r600_read_local_size_y:
296 Dim = 1;
297 break;
298 case Intrinsic::amdgcn_workitem_id_z:
299 case Intrinsic::r600_read_tidig_z:
300 IdQuery = true;
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000301 LLVM_FALLTHROUGH;
Stanislav Mekhanoshinc90347d2017-04-12 20:48:56 +0000302 case Intrinsic::r600_read_local_size_z:
303 Dim = 2;
304 break;
305 default:
306 break;
307 }
308 if (Dim <= 3) {
309 if (auto Node = Kernel->getMetadata("reqd_work_group_size"))
310 if (Node->getNumOperands() == 3)
311 MinSize = MaxSize = mdconst::extract<ConstantInt>(
312 Node->getOperand(Dim))->getZExtValue();
313 }
314 }
315 }
316
317 if (!MaxSize)
318 return false;
319
320 // Range metadata is [Lo, Hi). For ID query we need to pass max size
321 // as Hi. For size query we need to pass Hi + 1.
322 if (IdQuery)
323 MinSize = 0;
324 else
325 ++MaxSize;
326
327 MDBuilder MDB(I->getContext());
328 MDNode *MaxWorkGroupSizeRange = MDB.createRange(APInt(32, MinSize),
329 APInt(32, MaxSize));
330 I->setMetadata(LLVMContext::MD_range, MaxWorkGroupSizeRange);
331 return true;
332}
333
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000334R600Subtarget::R600Subtarget(const Triple &TT, StringRef GPU, StringRef FS,
335 const TargetMachine &TM) :
336 AMDGPUSubtarget(TT, GPU, FS, TM),
337 InstrInfo(*this),
338 FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0),
339 TLInfo(TM, *this) {}
340
341SISubtarget::SISubtarget(const Triple &TT, StringRef GPU, StringRef FS,
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +0000342 const TargetMachine &TM)
343 : AMDGPUSubtarget(TT, GPU, FS, TM), InstrInfo(*this),
344 FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0),
345 TLInfo(TM, *this) {
Quentin Colombet61d71a12017-08-15 22:31:51 +0000346 CallLoweringInfo.reset(new AMDGPUCallLowering(*getTargetLowering()));
347 Legalizer.reset(new AMDGPULegalizerInfo());
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +0000348
Quentin Colombet61d71a12017-08-15 22:31:51 +0000349 RegBankInfo.reset(new AMDGPURegisterBankInfo(*getRegisterInfo()));
350 InstSelector.reset(new AMDGPUInstructionSelector(
351 *this, *static_cast<AMDGPURegisterBankInfo *>(RegBankInfo.get())));
Quentin Colombetf3f7d4d2017-07-05 18:40:56 +0000352}
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000353
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000354void SISubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
Matt Arsenault55dff272016-06-28 00:11:26 +0000355 unsigned NumRegionInstrs) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000356 // Track register pressure so the scheduler can try to decrease
357 // pressure once register usage is above the threshold defined by
358 // SIRegisterInfo::getRegPressureSetLimit()
359 Policy.ShouldTrackPressure = true;
Tom Stellard83f0bce2015-01-29 16:55:25 +0000360
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000361 // Enabling both top down and bottom up scheduling seems to give us less
362 // register spills than just using one of these approaches on its own.
363 Policy.OnlyTopDown = false;
364 Policy.OnlyBottomUp = false;
Tom Stellard83f0bce2015-01-29 16:55:25 +0000365
Alexander Timofeev9f61fea2017-02-14 14:29:05 +0000366 // Enabling ShouldTrackLaneMasks crashes the SI Machine Scheduler.
367 if (!enableSIScheduler())
368 Policy.ShouldTrackLaneMasks = true;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000369}
Tom Stellard0bc954e2016-03-30 16:35:09 +0000370
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000371bool SISubtarget::isVGPRSpillingEnabled(const Function& F) const {
372 return EnableVGPRSpilling || !AMDGPU::isShader(F.getCallingConv());
373}
Tom Stellard0d23ebe2016-08-29 19:42:52 +0000374
Tom Stellard2f3f9852017-01-25 01:25:13 +0000375unsigned SISubtarget::getKernArgSegmentSize(const MachineFunction &MF,
Konstantin Zhuravlyov27d64c32017-02-08 13:29:23 +0000376 unsigned ExplicitArgBytes) const {
Tom Stellard2f3f9852017-01-25 01:25:13 +0000377 unsigned ImplicitBytes = getImplicitArgNumBytes(MF);
Tom Stellarde88bbc32016-09-23 01:33:26 +0000378 if (ImplicitBytes == 0)
379 return ExplicitArgBytes;
380
381 unsigned Alignment = getAlignmentForImplicitArgPtr();
382 return alignTo(ExplicitArgBytes, Alignment) + ImplicitBytes;
383}
384
Tom Stellard0d23ebe2016-08-29 19:42:52 +0000385unsigned SISubtarget::getOccupancyWithNumSGPRs(unsigned SGPRs) const {
386 if (getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
387 if (SGPRs <= 80)
388 return 10;
389 if (SGPRs <= 88)
390 return 9;
391 if (SGPRs <= 100)
392 return 8;
393 return 7;
394 }
395 if (SGPRs <= 48)
396 return 10;
397 if (SGPRs <= 56)
398 return 9;
399 if (SGPRs <= 64)
400 return 8;
401 if (SGPRs <= 72)
402 return 7;
403 if (SGPRs <= 80)
404 return 6;
405 return 5;
406}
407
408unsigned SISubtarget::getOccupancyWithNumVGPRs(unsigned VGPRs) const {
409 if (VGPRs <= 24)
410 return 10;
411 if (VGPRs <= 28)
412 return 9;
413 if (VGPRs <= 32)
414 return 8;
415 if (VGPRs <= 36)
416 return 7;
417 if (VGPRs <= 40)
418 return 6;
419 if (VGPRs <= 48)
420 return 5;
421 if (VGPRs <= 64)
422 return 4;
423 if (VGPRs <= 84)
424 return 3;
425 if (VGPRs <= 128)
426 return 2;
427 return 1;
428}
Matt Arsenault4eae3012016-10-28 20:31:47 +0000429
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000430unsigned SISubtarget::getReservedNumSGPRs(const MachineFunction &MF) const {
431 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
432 if (MFI.hasFlatScratchInit()) {
433 if (getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
434 return 6; // FLAT_SCRATCH, XNACK, VCC (in that order).
435 if (getGeneration() == AMDGPUSubtarget::SEA_ISLANDS)
436 return 4; // FLAT_SCRATCH, VCC (in that order).
437 }
438
439 if (isXNACKEnabled())
440 return 4; // XNACK, VCC (in that order).
441 return 2; // VCC.
442}
443
444unsigned SISubtarget::getMaxNumSGPRs(const MachineFunction &MF) const {
445 const Function &F = *MF.getFunction();
446 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
447
448 // Compute maximum number of SGPRs function can use using default/requested
449 // minimum number of waves per execution unit.
450 std::pair<unsigned, unsigned> WavesPerEU = MFI.getWavesPerEU();
451 unsigned MaxNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, false);
452 unsigned MaxAddressableNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, true);
453
454 // Check if maximum number of SGPRs was explicitly requested using
455 // "amdgpu-num-sgpr" attribute.
456 if (F.hasFnAttribute("amdgpu-num-sgpr")) {
457 unsigned Requested = AMDGPU::getIntegerAttribute(
458 F, "amdgpu-num-sgpr", MaxNumSGPRs);
459
460 // Make sure requested value does not violate subtarget's specifications.
461 if (Requested && (Requested <= getReservedNumSGPRs(MF)))
462 Requested = 0;
463
464 // If more SGPRs are required to support the input user/system SGPRs,
465 // increase to accommodate them.
466 //
467 // FIXME: This really ends up using the requested number of SGPRs + number
468 // of reserved special registers in total. Theoretically you could re-use
469 // the last input registers for these special registers, but this would
470 // require a lot of complexity to deal with the weird aliasing.
471 unsigned InputNumSGPRs = MFI.getNumPreloadedSGPRs();
472 if (Requested && Requested < InputNumSGPRs)
473 Requested = InputNumSGPRs;
474
475 // Make sure requested value is compatible with values implied by
476 // default/requested minimum/maximum number of waves per execution unit.
477 if (Requested && Requested > getMaxNumSGPRs(WavesPerEU.first, false))
478 Requested = 0;
479 if (WavesPerEU.second &&
480 Requested && Requested < getMinNumSGPRs(WavesPerEU.second))
481 Requested = 0;
482
483 if (Requested)
484 MaxNumSGPRs = Requested;
485 }
486
Matt Arsenault4eae3012016-10-28 20:31:47 +0000487 if (hasSGPRInitBug())
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000488 MaxNumSGPRs = AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Matt Arsenault4eae3012016-10-28 20:31:47 +0000489
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000490 return std::min(MaxNumSGPRs - getReservedNumSGPRs(MF),
491 MaxAddressableNumSGPRs);
492}
Matt Arsenault4eae3012016-10-28 20:31:47 +0000493
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000494unsigned SISubtarget::getMaxNumVGPRs(const MachineFunction &MF) const {
495 const Function &F = *MF.getFunction();
496 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
497
498 // Compute maximum number of VGPRs function can use using default/requested
499 // minimum number of waves per execution unit.
500 std::pair<unsigned, unsigned> WavesPerEU = MFI.getWavesPerEU();
501 unsigned MaxNumVGPRs = getMaxNumVGPRs(WavesPerEU.first);
502
503 // Check if maximum number of VGPRs was explicitly requested using
504 // "amdgpu-num-vgpr" attribute.
505 if (F.hasFnAttribute("amdgpu-num-vgpr")) {
506 unsigned Requested = AMDGPU::getIntegerAttribute(
507 F, "amdgpu-num-vgpr", MaxNumVGPRs);
508
509 // Make sure requested value does not violate subtarget's specifications.
510 if (Requested && Requested <= getReservedNumVGPRs(MF))
511 Requested = 0;
512
513 // Make sure requested value is compatible with values implied by
514 // default/requested minimum/maximum number of waves per execution unit.
515 if (Requested && Requested > getMaxNumVGPRs(WavesPerEU.first))
516 Requested = 0;
517 if (WavesPerEU.second &&
518 Requested && Requested < getMinNumVGPRs(WavesPerEU.second))
519 Requested = 0;
520
521 if (Requested)
522 MaxNumVGPRs = Requested;
523 }
524
525 return MaxNumVGPRs - getReservedNumVGPRs(MF);
Matt Arsenault4eae3012016-10-28 20:31:47 +0000526}
Stanislav Mekhanoshind4ae4702017-09-19 20:54:38 +0000527
528struct MemOpClusterMutation : ScheduleDAGMutation {
529 const SIInstrInfo *TII;
530
531 MemOpClusterMutation(const SIInstrInfo *tii) : TII(tii) {}
532
533 void apply(ScheduleDAGInstrs *DAGInstrs) override {
534 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
535
536 SUnit *SUa = nullptr;
537 // Search for two consequent memory operations and link them
538 // to prevent scheduler from moving them apart.
539 // In DAG pre-process SUnits are in the original order of
540 // the instructions before scheduling.
541 for (SUnit &SU : DAG->SUnits) {
542 MachineInstr &MI2 = *SU.getInstr();
543 if (!MI2.mayLoad() && !MI2.mayStore()) {
544 SUa = nullptr;
545 continue;
546 }
547 if (!SUa) {
548 SUa = &SU;
549 continue;
550 }
551
552 MachineInstr &MI1 = *SUa->getInstr();
553 if ((TII->isVMEM(MI1) && TII->isVMEM(MI2)) ||
554 (TII->isFLAT(MI1) && TII->isFLAT(MI2)) ||
555 (TII->isSMRD(MI1) && TII->isSMRD(MI2)) ||
556 (TII->isDS(MI1) && TII->isDS(MI2))) {
557 SU.addPredBarrier(SUa);
558
559 for (const SDep &SI : SU.Preds) {
560 if (SI.getSUnit() != SUa)
561 SUa->addPred(SDep(SI.getSUnit(), SDep::Artificial));
562 }
563
564 if (&SU != &DAG->ExitSU) {
565 for (const SDep &SI : SUa->Succs) {
566 if (SI.getSUnit() != &SU)
567 SI.getSUnit()->addPred(SDep(&SU, SDep::Artificial));
568 }
569 }
570 }
571
572 SUa = &SU;
573 }
574 }
575};
576
577void SISubtarget::getPostRAMutations(
578 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
579 Mutations.push_back(llvm::make_unique<MemOpClusterMutation>(&InstrInfo));
580}