1 //===-- AMDGPUSubtarget.cpp - AMDGPU Subtarget Information ----------------===//
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 /// Implements the AMDGPU specific subclass of TargetSubtarget.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AMDGPUSubtarget.h"
15 #include "AMDGPUCallLowering.h"
16 #include "AMDGPUInstructionSelector.h"
17 #include "AMDGPULegalizerInfo.h"
18 #include "AMDGPURegisterBankInfo.h"
19 #include "AMDGPUTargetMachine.h"
20 #include "R600Subtarget.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "Utils/AMDGPUBaseInfo.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/CodeGen/GlobalISel/InlineAsmLowering.h"
25 #include "llvm/CodeGen/MachineScheduler.h"
26 #include "llvm/CodeGen/TargetFrameLowering.h"
27 #include "llvm/IR/IntrinsicsAMDGPU.h"
28 #include "llvm/IR/IntrinsicsR600.h"
29 #include "llvm/IR/MDBuilder.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include <algorithm>
32 
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "amdgpu-subtarget"
36 
37 #define GET_SUBTARGETINFO_TARGET_DESC
38 #define GET_SUBTARGETINFO_CTOR
39 #define AMDGPUSubtarget GCNSubtarget
40 #include "AMDGPUGenSubtargetInfo.inc"
41 #undef AMDGPUSubtarget
42 
43 static cl::opt<bool> EnablePowerSched(
44   "amdgpu-enable-power-sched",
45   cl::desc("Enable scheduling to minimize mAI power bursts"),
46   cl::init(false));
47 
48 static cl::opt<bool> EnableVGPRIndexMode(
49   "amdgpu-vgpr-index-mode",
50   cl::desc("Use GPR indexing mode instead of movrel for vector indexing"),
51   cl::init(false));
52 
53 static cl::opt<bool> UseAA("amdgpu-use-aa-in-codegen",
54                            cl::desc("Enable the use of AA during codegen."),
55                            cl::init(true));
56 
57 GCNSubtarget::~GCNSubtarget() = default;
58 
59 GCNSubtarget &
60 GCNSubtarget::initializeSubtargetDependencies(const Triple &TT,
61                                               StringRef GPU, StringRef FS) {
62   // Determine default and user-specified characteristics
63   //
64   // We want to be able to turn these off, but making this a subtarget feature
65   // for SI has the unhelpful behavior that it unsets everything else if you
66   // disable it.
67   //
68   // Similarly we want enable-prt-strict-null to be on by default and not to
69   // unset everything else if it is disabled
70 
71   SmallString<256> FullFS("+promote-alloca,+load-store-opt,+enable-ds128,");
72 
73   // Turn on features that HSA ABI requires. Also turn on FlatForGlobal by default
74   if (isAmdHsaOS())
75     FullFS += "+flat-for-global,+unaligned-access-mode,+trap-handler,";
76 
77   FullFS += "+enable-prt-strict-null,"; // This is overridden by a disable in FS
78 
79   // Disable mutually exclusive bits.
80   if (FS.contains_insensitive("+wavefrontsize")) {
81     if (!FS.contains_insensitive("wavefrontsize16"))
82       FullFS += "-wavefrontsize16,";
83     if (!FS.contains_insensitive("wavefrontsize32"))
84       FullFS += "-wavefrontsize32,";
85     if (!FS.contains_insensitive("wavefrontsize64"))
86       FullFS += "-wavefrontsize64,";
87   }
88 
89   FullFS += FS;
90 
91   ParseSubtargetFeatures(GPU, /*TuneCPU*/ GPU, FullFS);
92 
93   // Implement the "generic" processors, which acts as the default when no
94   // generation features are enabled (e.g for -mcpu=''). HSA OS defaults to
95   // the first amdgcn target that supports flat addressing. Other OSes defaults
96   // to the first amdgcn target.
97   if (Gen == AMDGPUSubtarget::INVALID) {
98      Gen = TT.getOS() == Triple::AMDHSA ? AMDGPUSubtarget::SEA_ISLANDS
99                                         : AMDGPUSubtarget::SOUTHERN_ISLANDS;
100   }
101 
102   // We don't support FP64 for EG/NI atm.
103   assert(!hasFP64() || (getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS));
104 
105   // Targets must either support 64-bit offsets for MUBUF instructions, and/or
106   // support flat operations, otherwise they cannot access a 64-bit global
107   // address space
108   assert(hasAddr64() || hasFlat());
109   // Unless +-flat-for-global is specified, turn on FlatForGlobal for targets
110   // that do not support ADDR64 variants of MUBUF instructions. Such targets
111   // cannot use a 64 bit offset with a MUBUF instruction to access the global
112   // address space
113   if (!hasAddr64() && !FS.contains("flat-for-global") && !FlatForGlobal) {
114     ToggleFeature(AMDGPU::FeatureFlatForGlobal);
115     FlatForGlobal = true;
116   }
117   // Unless +-flat-for-global is specified, use MUBUF instructions for global
118   // address space access if flat operations are not available.
119   if (!hasFlat() && !FS.contains("flat-for-global") && FlatForGlobal) {
120     ToggleFeature(AMDGPU::FeatureFlatForGlobal);
121     FlatForGlobal = false;
122   }
123 
124   // Set defaults if needed.
125   if (MaxPrivateElementSize == 0)
126     MaxPrivateElementSize = 4;
127 
128   if (LDSBankCount == 0)
129     LDSBankCount = 32;
130 
131   if (TT.getArch() == Triple::amdgcn) {
132     if (LocalMemorySize == 0)
133       LocalMemorySize = 32768;
134 
135     // Do something sensible for unspecified target.
136     if (!HasMovrel && !HasVGPRIndexMode)
137       HasMovrel = true;
138   }
139 
140   // Don't crash on invalid devices.
141   if (WavefrontSizeLog2 == 0)
142     WavefrontSizeLog2 = 5;
143 
144   HasFminFmaxLegacy = getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS;
145   HasSMulHi = getGeneration() >= AMDGPUSubtarget::GFX9;
146 
147   TargetID.setTargetIDFromFeaturesString(FS);
148 
149   LLVM_DEBUG(dbgs() << "xnack setting for subtarget: "
150                     << TargetID.getXnackSetting() << '\n');
151   LLVM_DEBUG(dbgs() << "sramecc setting for subtarget: "
152                     << TargetID.getSramEccSetting() << '\n');
153 
154   return *this;
155 }
156 
157 AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT) : TargetTriple(TT) {}
158 
159 GCNSubtarget::GCNSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
160                            const GCNTargetMachine &TM)
161     : // clang-format off
162     AMDGPUGenSubtargetInfo(TT, GPU, /*TuneCPU*/ GPU, FS),
163     AMDGPUSubtarget(TT),
164     TargetTriple(TT),
165     TargetID(*this),
166     InstrItins(getInstrItineraryForCPU(GPU)),
167     InstrInfo(initializeSubtargetDependencies(TT, GPU, FS)),
168     TLInfo(TM, *this),
169     FrameLowering(TargetFrameLowering::StackGrowsUp, getStackAlignment(), 0) {
170   // clang-format on
171   MaxWavesPerEU = AMDGPU::IsaInfo::getMaxWavesPerEU(this);
172   CallLoweringInfo.reset(new AMDGPUCallLowering(*getTargetLowering()));
173   InlineAsmLoweringInfo.reset(new InlineAsmLowering(getTargetLowering()));
174   Legalizer.reset(new AMDGPULegalizerInfo(*this, TM));
175   RegBankInfo.reset(new AMDGPURegisterBankInfo(*this));
176   InstSelector.reset(new AMDGPUInstructionSelector(
177   *this, *static_cast<AMDGPURegisterBankInfo *>(RegBankInfo.get()), TM));
178 }
179 
180 unsigned GCNSubtarget::getConstantBusLimit(unsigned Opcode) const {
181   if (getGeneration() < GFX10)
182     return 1;
183 
184   switch (Opcode) {
185   case AMDGPU::V_LSHLREV_B64_e64:
186   case AMDGPU::V_LSHLREV_B64_gfx10:
187   case AMDGPU::V_LSHLREV_B64_e64_gfx11:
188   case AMDGPU::V_LSHL_B64_e64:
189   case AMDGPU::V_LSHRREV_B64_e64:
190   case AMDGPU::V_LSHRREV_B64_gfx10:
191   case AMDGPU::V_LSHRREV_B64_e64_gfx11:
192   case AMDGPU::V_LSHR_B64_e64:
193   case AMDGPU::V_ASHRREV_I64_e64:
194   case AMDGPU::V_ASHRREV_I64_gfx10:
195   case AMDGPU::V_ASHRREV_I64_e64_gfx11:
196   case AMDGPU::V_ASHR_I64_e64:
197     return 1;
198   }
199 
200   return 2;
201 }
202 
203 /// This list was mostly derived from experimentation.
204 bool GCNSubtarget::zeroesHigh16BitsOfDest(unsigned Opcode) const {
205   switch (Opcode) {
206   case AMDGPU::V_CVT_F16_F32_e32:
207   case AMDGPU::V_CVT_F16_F32_e64:
208   case AMDGPU::V_CVT_F16_U16_e32:
209   case AMDGPU::V_CVT_F16_U16_e64:
210   case AMDGPU::V_CVT_F16_I16_e32:
211   case AMDGPU::V_CVT_F16_I16_e64:
212   case AMDGPU::V_RCP_F16_e64:
213   case AMDGPU::V_RCP_F16_e32:
214   case AMDGPU::V_RSQ_F16_e64:
215   case AMDGPU::V_RSQ_F16_e32:
216   case AMDGPU::V_SQRT_F16_e64:
217   case AMDGPU::V_SQRT_F16_e32:
218   case AMDGPU::V_LOG_F16_e64:
219   case AMDGPU::V_LOG_F16_e32:
220   case AMDGPU::V_EXP_F16_e64:
221   case AMDGPU::V_EXP_F16_e32:
222   case AMDGPU::V_SIN_F16_e64:
223   case AMDGPU::V_SIN_F16_e32:
224   case AMDGPU::V_COS_F16_e64:
225   case AMDGPU::V_COS_F16_e32:
226   case AMDGPU::V_FLOOR_F16_e64:
227   case AMDGPU::V_FLOOR_F16_e32:
228   case AMDGPU::V_CEIL_F16_e64:
229   case AMDGPU::V_CEIL_F16_e32:
230   case AMDGPU::V_TRUNC_F16_e64:
231   case AMDGPU::V_TRUNC_F16_e32:
232   case AMDGPU::V_RNDNE_F16_e64:
233   case AMDGPU::V_RNDNE_F16_e32:
234   case AMDGPU::V_FRACT_F16_e64:
235   case AMDGPU::V_FRACT_F16_e32:
236   case AMDGPU::V_FREXP_MANT_F16_e64:
237   case AMDGPU::V_FREXP_MANT_F16_e32:
238   case AMDGPU::V_FREXP_EXP_I16_F16_e64:
239   case AMDGPU::V_FREXP_EXP_I16_F16_e32:
240   case AMDGPU::V_LDEXP_F16_e64:
241   case AMDGPU::V_LDEXP_F16_e32:
242   case AMDGPU::V_LSHLREV_B16_e64:
243   case AMDGPU::V_LSHLREV_B16_e32:
244   case AMDGPU::V_LSHRREV_B16_e64:
245   case AMDGPU::V_LSHRREV_B16_e32:
246   case AMDGPU::V_ASHRREV_I16_e64:
247   case AMDGPU::V_ASHRREV_I16_e32:
248   case AMDGPU::V_ADD_U16_e64:
249   case AMDGPU::V_ADD_U16_e32:
250   case AMDGPU::V_SUB_U16_e64:
251   case AMDGPU::V_SUB_U16_e32:
252   case AMDGPU::V_SUBREV_U16_e64:
253   case AMDGPU::V_SUBREV_U16_e32:
254   case AMDGPU::V_MUL_LO_U16_e64:
255   case AMDGPU::V_MUL_LO_U16_e32:
256   case AMDGPU::V_ADD_F16_e64:
257   case AMDGPU::V_ADD_F16_e32:
258   case AMDGPU::V_SUB_F16_e64:
259   case AMDGPU::V_SUB_F16_e32:
260   case AMDGPU::V_SUBREV_F16_e64:
261   case AMDGPU::V_SUBREV_F16_e32:
262   case AMDGPU::V_MUL_F16_e64:
263   case AMDGPU::V_MUL_F16_e32:
264   case AMDGPU::V_MAX_F16_e64:
265   case AMDGPU::V_MAX_F16_e32:
266   case AMDGPU::V_MIN_F16_e64:
267   case AMDGPU::V_MIN_F16_e32:
268   case AMDGPU::V_MAX_U16_e64:
269   case AMDGPU::V_MAX_U16_e32:
270   case AMDGPU::V_MIN_U16_e64:
271   case AMDGPU::V_MIN_U16_e32:
272   case AMDGPU::V_MAX_I16_e64:
273   case AMDGPU::V_MAX_I16_e32:
274   case AMDGPU::V_MIN_I16_e64:
275   case AMDGPU::V_MIN_I16_e32:
276   case AMDGPU::V_MAD_F16_e64:
277   case AMDGPU::V_MAD_U16_e64:
278   case AMDGPU::V_MAD_I16_e64:
279   case AMDGPU::V_FMA_F16_e64:
280   case AMDGPU::V_DIV_FIXUP_F16_e64:
281     // On gfx10, all 16-bit instructions preserve the high bits.
282     return getGeneration() <= AMDGPUSubtarget::GFX9;
283   case AMDGPU::V_MADAK_F16:
284   case AMDGPU::V_MADMK_F16:
285   case AMDGPU::V_MAC_F16_e64:
286   case AMDGPU::V_MAC_F16_e32:
287   case AMDGPU::V_FMAMK_F16:
288   case AMDGPU::V_FMAAK_F16:
289   case AMDGPU::V_FMAC_F16_e64:
290   case AMDGPU::V_FMAC_F16_e32:
291     // In gfx9, the preferred handling of the unused high 16-bits changed. Most
292     // instructions maintain the legacy behavior of 0ing. Some instructions
293     // changed to preserving the high bits.
294     return getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS;
295   case AMDGPU::V_MAD_MIXLO_F16:
296   case AMDGPU::V_MAD_MIXHI_F16:
297   default:
298     return false;
299   }
300 }
301 
302 unsigned AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves,
303   const Function &F) const {
304   if (NWaves == 1)
305     return getLocalMemorySize();
306   unsigned WorkGroupSize = getFlatWorkGroupSizes(F).second;
307   unsigned WorkGroupsPerCu = getMaxWorkGroupsPerCU(WorkGroupSize);
308   if (!WorkGroupsPerCu)
309     return 0;
310   unsigned MaxWaves = getMaxWavesPerEU();
311   return getLocalMemorySize() * MaxWaves / WorkGroupsPerCu / NWaves;
312 }
313 
314 // FIXME: Should return min,max range.
315 unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes,
316   const Function &F) const {
317   const unsigned MaxWorkGroupSize = getFlatWorkGroupSizes(F).second;
318   const unsigned MaxWorkGroupsPerCu = getMaxWorkGroupsPerCU(MaxWorkGroupSize);
319   if (!MaxWorkGroupsPerCu)
320     return 0;
321 
322   const unsigned WaveSize = getWavefrontSize();
323 
324   // FIXME: Do we need to account for alignment requirement of LDS rounding the
325   // size up?
326   // Compute restriction based on LDS usage
327   unsigned NumGroups = getLocalMemorySize() / (Bytes ? Bytes : 1u);
328 
329   // This can be queried with more LDS than is possible, so just assume the
330   // worst.
331   if (NumGroups == 0)
332     return 1;
333 
334   NumGroups = std::min(MaxWorkGroupsPerCu, NumGroups);
335 
336   // Round to the number of waves.
337   const unsigned MaxGroupNumWaves = (MaxWorkGroupSize + WaveSize - 1) / WaveSize;
338   unsigned MaxWaves = NumGroups * MaxGroupNumWaves;
339 
340   // Clamp to the maximum possible number of waves.
341   MaxWaves = std::min(MaxWaves, getMaxWavesPerEU());
342 
343   // FIXME: Needs to be a multiple of the group size?
344   //MaxWaves = MaxGroupNumWaves * (MaxWaves / MaxGroupNumWaves);
345 
346   assert(MaxWaves > 0 && MaxWaves <= getMaxWavesPerEU() &&
347          "computed invalid occupancy");
348   return MaxWaves;
349 }
350 
351 unsigned
352 AMDGPUSubtarget::getOccupancyWithLocalMemSize(const MachineFunction &MF) const {
353   const auto *MFI = MF.getInfo<SIMachineFunctionInfo>();
354   return getOccupancyWithLocalMemSize(MFI->getLDSSize(), MF.getFunction());
355 }
356 
357 std::pair<unsigned, unsigned>
358 AMDGPUSubtarget::getDefaultFlatWorkGroupSize(CallingConv::ID CC) const {
359   switch (CC) {
360   case CallingConv::AMDGPU_VS:
361   case CallingConv::AMDGPU_LS:
362   case CallingConv::AMDGPU_HS:
363   case CallingConv::AMDGPU_ES:
364   case CallingConv::AMDGPU_GS:
365   case CallingConv::AMDGPU_PS:
366     return std::make_pair(1, getWavefrontSize());
367   default:
368     return std::make_pair(1u, getMaxFlatWorkGroupSize());
369   }
370 }
371 
372 std::pair<unsigned, unsigned> AMDGPUSubtarget::getFlatWorkGroupSizes(
373   const Function &F) const {
374   // Default minimum/maximum flat work group sizes.
375   std::pair<unsigned, unsigned> Default =
376     getDefaultFlatWorkGroupSize(F.getCallingConv());
377 
378   // Requested minimum/maximum flat work group sizes.
379   std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
380     F, "amdgpu-flat-work-group-size", Default);
381 
382   // Make sure requested minimum is less than requested maximum.
383   if (Requested.first > Requested.second)
384     return Default;
385 
386   // Make sure requested values do not violate subtarget's specifications.
387   if (Requested.first < getMinFlatWorkGroupSize())
388     return Default;
389   if (Requested.second > getMaxFlatWorkGroupSize())
390     return Default;
391 
392   return Requested;
393 }
394 
395 std::pair<unsigned, unsigned> AMDGPUSubtarget::getWavesPerEU(
396     const Function &F, std::pair<unsigned, unsigned> FlatWorkGroupSizes) const {
397   // Default minimum/maximum number of waves per execution unit.
398   std::pair<unsigned, unsigned> Default(1, getMaxWavesPerEU());
399 
400   // If minimum/maximum flat work group sizes were explicitly requested using
401   // "amdgpu-flat-work-group-size" attribute, then set default minimum/maximum
402   // number of waves per execution unit to values implied by requested
403   // minimum/maximum flat work group sizes.
404   unsigned MinImpliedByFlatWorkGroupSize =
405     getWavesPerEUForWorkGroup(FlatWorkGroupSizes.second);
406   Default.first = MinImpliedByFlatWorkGroupSize;
407 
408   // Requested minimum/maximum number of waves per execution unit.
409   std::pair<unsigned, unsigned> Requested = AMDGPU::getIntegerPairAttribute(
410     F, "amdgpu-waves-per-eu", Default, true);
411 
412   // Make sure requested minimum is less than requested maximum.
413   if (Requested.second && Requested.first > Requested.second)
414     return Default;
415 
416   // Make sure requested values do not violate subtarget's specifications.
417   if (Requested.first < getMinWavesPerEU() ||
418       Requested.second > getMaxWavesPerEU())
419     return Default;
420 
421   // Make sure requested values are compatible with values implied by requested
422   // minimum/maximum flat work group sizes.
423   if (Requested.first < MinImpliedByFlatWorkGroupSize)
424     return Default;
425 
426   return Requested;
427 }
428 
429 static unsigned getReqdWorkGroupSize(const Function &Kernel, unsigned Dim) {
430   auto Node = Kernel.getMetadata("reqd_work_group_size");
431   if (Node && Node->getNumOperands() == 3)
432     return mdconst::extract<ConstantInt>(Node->getOperand(Dim))->getZExtValue();
433   return std::numeric_limits<unsigned>::max();
434 }
435 
436 bool AMDGPUSubtarget::isMesaKernel(const Function &F) const {
437   return isMesa3DOS() && !AMDGPU::isShader(F.getCallingConv());
438 }
439 
440 unsigned AMDGPUSubtarget::getMaxWorkitemID(const Function &Kernel,
441                                            unsigned Dimension) const {
442   unsigned ReqdSize = getReqdWorkGroupSize(Kernel, Dimension);
443   if (ReqdSize != std::numeric_limits<unsigned>::max())
444     return ReqdSize - 1;
445   return getFlatWorkGroupSizes(Kernel).second - 1;
446 }
447 
448 bool AMDGPUSubtarget::makeLIDRangeMetadata(Instruction *I) const {
449   Function *Kernel = I->getParent()->getParent();
450   unsigned MinSize = 0;
451   unsigned MaxSize = getFlatWorkGroupSizes(*Kernel).second;
452   bool IdQuery = false;
453 
454   // If reqd_work_group_size is present it narrows value down.
455   if (auto *CI = dyn_cast<CallInst>(I)) {
456     const Function *F = CI->getCalledFunction();
457     if (F) {
458       unsigned Dim = UINT_MAX;
459       switch (F->getIntrinsicID()) {
460       case Intrinsic::amdgcn_workitem_id_x:
461       case Intrinsic::r600_read_tidig_x:
462         IdQuery = true;
463         LLVM_FALLTHROUGH;
464       case Intrinsic::r600_read_local_size_x:
465         Dim = 0;
466         break;
467       case Intrinsic::amdgcn_workitem_id_y:
468       case Intrinsic::r600_read_tidig_y:
469         IdQuery = true;
470         LLVM_FALLTHROUGH;
471       case Intrinsic::r600_read_local_size_y:
472         Dim = 1;
473         break;
474       case Intrinsic::amdgcn_workitem_id_z:
475       case Intrinsic::r600_read_tidig_z:
476         IdQuery = true;
477         LLVM_FALLTHROUGH;
478       case Intrinsic::r600_read_local_size_z:
479         Dim = 2;
480         break;
481       default:
482         break;
483       }
484 
485       if (Dim <= 3) {
486         unsigned ReqdSize = getReqdWorkGroupSize(*Kernel, Dim);
487         if (ReqdSize != std::numeric_limits<unsigned>::max())
488           MinSize = MaxSize = ReqdSize;
489       }
490     }
491   }
492 
493   if (!MaxSize)
494     return false;
495 
496   // Range metadata is [Lo, Hi). For ID query we need to pass max size
497   // as Hi. For size query we need to pass Hi + 1.
498   if (IdQuery)
499     MinSize = 0;
500   else
501     ++MaxSize;
502 
503   MDBuilder MDB(I->getContext());
504   MDNode *MaxWorkGroupSizeRange = MDB.createRange(APInt(32, MinSize),
505                                                   APInt(32, MaxSize));
506   I->setMetadata(LLVMContext::MD_range, MaxWorkGroupSizeRange);
507   return true;
508 }
509 
510 unsigned AMDGPUSubtarget::getImplicitArgNumBytes(const Function &F) const {
511   assert(AMDGPU::isKernel(F.getCallingConv()));
512 
513   // We don't allocate the segment if we know the implicit arguments weren't
514   // used, even if the ABI implies we need them.
515   if (F.hasFnAttribute("amdgpu-no-implicitarg-ptr"))
516     return 0;
517 
518   if (isMesaKernel(F))
519     return 16;
520 
521   // Assume all implicit inputs are used by default
522   unsigned NBytes = (AMDGPU::getAmdhsaCodeObjectVersion() >= 5) ? 256 : 56;
523   return AMDGPU::getIntegerAttribute(F, "amdgpu-implicitarg-num-bytes", NBytes);
524 }
525 
526 uint64_t AMDGPUSubtarget::getExplicitKernArgSize(const Function &F,
527                                                  Align &MaxAlign) const {
528   assert(F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
529          F.getCallingConv() == CallingConv::SPIR_KERNEL);
530 
531   const DataLayout &DL = F.getParent()->getDataLayout();
532   uint64_t ExplicitArgBytes = 0;
533   MaxAlign = Align(1);
534 
535   for (const Argument &Arg : F.args()) {
536     const bool IsByRef = Arg.hasByRefAttr();
537     Type *ArgTy = IsByRef ? Arg.getParamByRefType() : Arg.getType();
538     Align Alignment = DL.getValueOrABITypeAlignment(
539         IsByRef ? Arg.getParamAlign() : None, ArgTy);
540     uint64_t AllocSize = DL.getTypeAllocSize(ArgTy);
541     ExplicitArgBytes = alignTo(ExplicitArgBytes, Alignment) + AllocSize;
542     MaxAlign = std::max(MaxAlign, Alignment);
543   }
544 
545   return ExplicitArgBytes;
546 }
547 
548 unsigned AMDGPUSubtarget::getKernArgSegmentSize(const Function &F,
549                                                 Align &MaxAlign) const {
550   uint64_t ExplicitArgBytes = getExplicitKernArgSize(F, MaxAlign);
551 
552   unsigned ExplicitOffset = getExplicitKernelArgOffset(F);
553 
554   uint64_t TotalSize = ExplicitOffset + ExplicitArgBytes;
555   unsigned ImplicitBytes = getImplicitArgNumBytes(F);
556   if (ImplicitBytes != 0) {
557     const Align Alignment = getAlignmentForImplicitArgPtr();
558     TotalSize = alignTo(ExplicitArgBytes, Alignment) + ImplicitBytes;
559     MaxAlign = std::max(MaxAlign, Alignment);
560   }
561 
562   // Being able to dereference past the end is useful for emitting scalar loads.
563   return alignTo(TotalSize, 4);
564 }
565 
566 AMDGPUDwarfFlavour AMDGPUSubtarget::getAMDGPUDwarfFlavour() const {
567   return getWavefrontSize() == 32 ? AMDGPUDwarfFlavour::Wave32
568                                   : AMDGPUDwarfFlavour::Wave64;
569 }
570 
571 void GCNSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
572                                       unsigned NumRegionInstrs) const {
573   // Track register pressure so the scheduler can try to decrease
574   // pressure once register usage is above the threshold defined by
575   // SIRegisterInfo::getRegPressureSetLimit()
576   Policy.ShouldTrackPressure = true;
577 
578   // Enabling both top down and bottom up scheduling seems to give us less
579   // register spills than just using one of these approaches on its own.
580   Policy.OnlyTopDown = false;
581   Policy.OnlyBottomUp = false;
582 
583   // Enabling ShouldTrackLaneMasks crashes the SI Machine Scheduler.
584   if (!enableSIScheduler())
585     Policy.ShouldTrackLaneMasks = true;
586 }
587 
588 bool GCNSubtarget::hasMadF16() const {
589   return InstrInfo.pseudoToMCOpcode(AMDGPU::V_MAD_F16_e64) != -1;
590 }
591 
592 bool GCNSubtarget::useVGPRIndexMode() const {
593   return !hasMovrel() || (EnableVGPRIndexMode && hasVGPRIndexMode());
594 }
595 
596 bool GCNSubtarget::useAA() const { return UseAA; }
597 
598 unsigned GCNSubtarget::getOccupancyWithNumSGPRs(unsigned SGPRs) const {
599   if (getGeneration() >= AMDGPUSubtarget::GFX10)
600     return getMaxWavesPerEU();
601 
602   if (getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
603     if (SGPRs <= 80)
604       return 10;
605     if (SGPRs <= 88)
606       return 9;
607     if (SGPRs <= 100)
608       return 8;
609     return 7;
610   }
611   if (SGPRs <= 48)
612     return 10;
613   if (SGPRs <= 56)
614     return 9;
615   if (SGPRs <= 64)
616     return 8;
617   if (SGPRs <= 72)
618     return 7;
619   if (SGPRs <= 80)
620     return 6;
621   return 5;
622 }
623 
624 unsigned GCNSubtarget::getOccupancyWithNumVGPRs(unsigned VGPRs) const {
625   unsigned MaxWaves = getMaxWavesPerEU();
626   unsigned Granule = getVGPRAllocGranule();
627   if (VGPRs < Granule)
628     return MaxWaves;
629   unsigned RoundedRegs = ((VGPRs + Granule - 1) / Granule) * Granule;
630   return std::min(std::max(getTotalNumVGPRs() / RoundedRegs, 1u), MaxWaves);
631 }
632 
633 unsigned
634 GCNSubtarget::getBaseReservedNumSGPRs(const bool HasFlatScratch) const {
635   if (getGeneration() >= AMDGPUSubtarget::GFX10)
636     return 2; // VCC. FLAT_SCRATCH and XNACK are no longer in SGPRs.
637 
638   if (HasFlatScratch || HasArchitectedFlatScratch) {
639     if (getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
640       return 6; // FLAT_SCRATCH, XNACK, VCC (in that order).
641     if (getGeneration() == AMDGPUSubtarget::SEA_ISLANDS)
642       return 4; // FLAT_SCRATCH, VCC (in that order).
643   }
644 
645   if (isXNACKEnabled())
646     return 4; // XNACK, VCC (in that order).
647   return 2; // VCC.
648 }
649 
650 unsigned GCNSubtarget::getReservedNumSGPRs(const MachineFunction &MF) const {
651   const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
652   return getBaseReservedNumSGPRs(MFI.hasFlatScratchInit());
653 }
654 
655 unsigned GCNSubtarget::getReservedNumSGPRs(const Function &F) const {
656   // In principle we do not need to reserve SGPR pair used for flat_scratch if
657   // we know flat instructions do not access the stack anywhere in the
658   // program. For now assume it's needed if we have flat instructions.
659   const bool KernelUsesFlatScratch = hasFlatAddressSpace();
660   return getBaseReservedNumSGPRs(KernelUsesFlatScratch);
661 }
662 
663 unsigned GCNSubtarget::computeOccupancy(const Function &F, unsigned LDSSize,
664                                         unsigned NumSGPRs,
665                                         unsigned NumVGPRs) const {
666   unsigned Occupancy =
667     std::min(getMaxWavesPerEU(),
668              getOccupancyWithLocalMemSize(LDSSize, F));
669   if (NumSGPRs)
670     Occupancy = std::min(Occupancy, getOccupancyWithNumSGPRs(NumSGPRs));
671   if (NumVGPRs)
672     Occupancy = std::min(Occupancy, getOccupancyWithNumVGPRs(NumVGPRs));
673   return Occupancy;
674 }
675 
676 unsigned GCNSubtarget::getBaseMaxNumSGPRs(
677     const Function &F, std::pair<unsigned, unsigned> WavesPerEU,
678     unsigned PreloadedSGPRs, unsigned ReservedNumSGPRs) const {
679   // Compute maximum number of SGPRs function can use using default/requested
680   // minimum number of waves per execution unit.
681   unsigned MaxNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, false);
682   unsigned MaxAddressableNumSGPRs = getMaxNumSGPRs(WavesPerEU.first, true);
683 
684   // Check if maximum number of SGPRs was explicitly requested using
685   // "amdgpu-num-sgpr" attribute.
686   if (F.hasFnAttribute("amdgpu-num-sgpr")) {
687     unsigned Requested = AMDGPU::getIntegerAttribute(
688       F, "amdgpu-num-sgpr", MaxNumSGPRs);
689 
690     // Make sure requested value does not violate subtarget's specifications.
691     if (Requested && (Requested <= ReservedNumSGPRs))
692       Requested = 0;
693 
694     // If more SGPRs are required to support the input user/system SGPRs,
695     // increase to accommodate them.
696     //
697     // FIXME: This really ends up using the requested number of SGPRs + number
698     // of reserved special registers in total. Theoretically you could re-use
699     // the last input registers for these special registers, but this would
700     // require a lot of complexity to deal with the weird aliasing.
701     unsigned InputNumSGPRs = PreloadedSGPRs;
702     if (Requested && Requested < InputNumSGPRs)
703       Requested = InputNumSGPRs;
704 
705     // Make sure requested value is compatible with values implied by
706     // default/requested minimum/maximum number of waves per execution unit.
707     if (Requested && Requested > getMaxNumSGPRs(WavesPerEU.first, false))
708       Requested = 0;
709     if (WavesPerEU.second &&
710         Requested && Requested < getMinNumSGPRs(WavesPerEU.second))
711       Requested = 0;
712 
713     if (Requested)
714       MaxNumSGPRs = Requested;
715   }
716 
717   if (hasSGPRInitBug())
718     MaxNumSGPRs = AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
719 
720   return std::min(MaxNumSGPRs - ReservedNumSGPRs, MaxAddressableNumSGPRs);
721 }
722 
723 unsigned GCNSubtarget::getMaxNumSGPRs(const MachineFunction &MF) const {
724   const Function &F = MF.getFunction();
725   const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
726   return getBaseMaxNumSGPRs(F, MFI.getWavesPerEU(), MFI.getNumPreloadedSGPRs(),
727                             getReservedNumSGPRs(MF));
728 }
729 
730 static unsigned getMaxNumPreloadedSGPRs() {
731   // Max number of user SGPRs
732   unsigned MaxUserSGPRs = 4 + // private segment buffer
733                           2 + // Dispatch ptr
734                           2 + // queue ptr
735                           2 + // kernel segment ptr
736                           2 + // dispatch ID
737                           2 + // flat scratch init
738                           2;  // Implicit buffer ptr
739   // Max number of system SGPRs
740   unsigned MaxSystemSGPRs = 1 + // WorkGroupIDX
741                             1 + // WorkGroupIDY
742                             1 + // WorkGroupIDZ
743                             1 + // WorkGroupInfo
744                             1;  // private segment wave byte offset
745   return MaxUserSGPRs + MaxSystemSGPRs;
746 }
747 
748 unsigned GCNSubtarget::getMaxNumSGPRs(const Function &F) const {
749   return getBaseMaxNumSGPRs(F, getWavesPerEU(F), getMaxNumPreloadedSGPRs(),
750                             getReservedNumSGPRs(F));
751 }
752 
753 unsigned GCNSubtarget::getBaseMaxNumVGPRs(
754     const Function &F, std::pair<unsigned, unsigned> WavesPerEU) const {
755   // Compute maximum number of VGPRs function can use using default/requested
756   // minimum number of waves per execution unit.
757   unsigned MaxNumVGPRs = getMaxNumVGPRs(WavesPerEU.first);
758 
759   // Check if maximum number of VGPRs was explicitly requested using
760   // "amdgpu-num-vgpr" attribute.
761   if (F.hasFnAttribute("amdgpu-num-vgpr")) {
762     unsigned Requested = AMDGPU::getIntegerAttribute(
763       F, "amdgpu-num-vgpr", MaxNumVGPRs);
764 
765     if (hasGFX90AInsts())
766       Requested *= 2;
767 
768     // Make sure requested value is compatible with values implied by
769     // default/requested minimum/maximum number of waves per execution unit.
770     if (Requested && Requested > getMaxNumVGPRs(WavesPerEU.first))
771       Requested = 0;
772     if (WavesPerEU.second &&
773         Requested && Requested < getMinNumVGPRs(WavesPerEU.second))
774       Requested = 0;
775 
776     if (Requested)
777       MaxNumVGPRs = Requested;
778   }
779 
780   return MaxNumVGPRs;
781 }
782 
783 unsigned GCNSubtarget::getMaxNumVGPRs(const Function &F) const {
784   return getBaseMaxNumVGPRs(F, getWavesPerEU(F));
785 }
786 
787 unsigned GCNSubtarget::getMaxNumVGPRs(const MachineFunction &MF) const {
788   const Function &F = MF.getFunction();
789   const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
790   return getBaseMaxNumVGPRs(F, MFI.getWavesPerEU());
791 }
792 
793 void GCNSubtarget::adjustSchedDependency(SUnit *Def, int DefOpIdx, SUnit *Use,
794                                          int UseOpIdx, SDep &Dep) const {
795   if (Dep.getKind() != SDep::Kind::Data || !Dep.getReg() ||
796       !Def->isInstr() || !Use->isInstr())
797     return;
798 
799   MachineInstr *DefI = Def->getInstr();
800   MachineInstr *UseI = Use->getInstr();
801 
802   if (DefI->isBundle()) {
803     const SIRegisterInfo *TRI = getRegisterInfo();
804     auto Reg = Dep.getReg();
805     MachineBasicBlock::const_instr_iterator I(DefI->getIterator());
806     MachineBasicBlock::const_instr_iterator E(DefI->getParent()->instr_end());
807     unsigned Lat = 0;
808     for (++I; I != E && I->isBundledWithPred(); ++I) {
809       if (I->modifiesRegister(Reg, TRI))
810         Lat = InstrInfo.getInstrLatency(getInstrItineraryData(), *I);
811       else if (Lat)
812         --Lat;
813     }
814     Dep.setLatency(Lat);
815   } else if (UseI->isBundle()) {
816     const SIRegisterInfo *TRI = getRegisterInfo();
817     auto Reg = Dep.getReg();
818     MachineBasicBlock::const_instr_iterator I(UseI->getIterator());
819     MachineBasicBlock::const_instr_iterator E(UseI->getParent()->instr_end());
820     unsigned Lat = InstrInfo.getInstrLatency(getInstrItineraryData(), *DefI);
821     for (++I; I != E && I->isBundledWithPred() && Lat; ++I) {
822       if (I->readsRegister(Reg, TRI))
823         break;
824       --Lat;
825     }
826     Dep.setLatency(Lat);
827   } else if (Dep.getLatency() == 0 && Dep.getReg() == AMDGPU::VCC_LO) {
828     // Work around the fact that SIInstrInfo::fixImplicitOperands modifies
829     // implicit operands which come from the MCInstrDesc, which can fool
830     // ScheduleDAGInstrs::addPhysRegDataDeps into treating them as implicit
831     // pseudo operands.
832     Dep.setLatency(InstrInfo.getSchedModel().computeOperandLatency(
833         DefI, DefOpIdx, UseI, UseOpIdx));
834   }
835 }
836 
837 namespace {
838 struct FillMFMAShadowMutation : ScheduleDAGMutation {
839   const SIInstrInfo *TII;
840 
841   ScheduleDAGMI *DAG;
842 
843   FillMFMAShadowMutation(const SIInstrInfo *tii) : TII(tii) {}
844 
845   bool isSALU(const SUnit *SU) const {
846     const MachineInstr *MI = SU->getInstr();
847     return MI && TII->isSALU(*MI) && !MI->isTerminator();
848   }
849 
850   bool isVALU(const SUnit *SU) const {
851     const MachineInstr *MI = SU->getInstr();
852     return MI && TII->isVALU(*MI);
853   }
854 
855   bool canAddEdge(const SUnit *Succ, const SUnit *Pred) const {
856     if (Pred->NodeNum < Succ->NodeNum)
857       return true;
858 
859     SmallVector<const SUnit*, 64> Succs({Succ}), Preds({Pred});
860 
861     for (unsigned I = 0; I < Succs.size(); ++I) {
862       for (const SDep &SI : Succs[I]->Succs) {
863         const SUnit *SU = SI.getSUnit();
864         if (SU != Succs[I] && !llvm::is_contained(Succs, SU))
865           Succs.push_back(SU);
866       }
867     }
868 
869     SmallPtrSet<const SUnit*, 32> Visited;
870     while (!Preds.empty()) {
871       const SUnit *SU = Preds.pop_back_val();
872       if (llvm::is_contained(Succs, SU))
873         return false;
874       Visited.insert(SU);
875       for (const SDep &SI : SU->Preds)
876         if (SI.getSUnit() != SU && !Visited.count(SI.getSUnit()))
877           Preds.push_back(SI.getSUnit());
878     }
879 
880     return true;
881   }
882 
883   // Link as many SALU instructions in chain as possible. Return the size
884   // of the chain. Links up to MaxChain instructions.
885   unsigned linkSALUChain(SUnit *From, SUnit *To, unsigned MaxChain,
886                          SmallPtrSetImpl<SUnit *> &Visited) const {
887     SmallVector<SUnit *, 8> Worklist({To});
888     unsigned Linked = 0;
889 
890     while (!Worklist.empty() && MaxChain-- > 0) {
891       SUnit *SU = Worklist.pop_back_val();
892       if (!Visited.insert(SU).second)
893         continue;
894 
895       LLVM_DEBUG(dbgs() << "Inserting edge from\n" ; DAG->dumpNode(*From);
896                  dbgs() << "to\n"; DAG->dumpNode(*SU); dbgs() << '\n');
897 
898       if (SU->addPred(SDep(From, SDep::Artificial), false))
899         ++Linked;
900 
901       for (SDep &SI : From->Succs) {
902         SUnit *SUv = SI.getSUnit();
903         if (SUv != From && isVALU(SUv) && canAddEdge(SUv, SU))
904           SUv->addPred(SDep(SU, SDep::Artificial), false);
905       }
906 
907       for (SDep &SI : SU->Succs) {
908         SUnit *Succ = SI.getSUnit();
909         if (Succ != SU && isSALU(Succ) && canAddEdge(From, Succ))
910           Worklist.push_back(Succ);
911       }
912     }
913 
914     return Linked;
915   }
916 
917   void apply(ScheduleDAGInstrs *DAGInstrs) override {
918     const GCNSubtarget &ST = DAGInstrs->MF.getSubtarget<GCNSubtarget>();
919     if (!ST.hasMAIInsts())
920       return;
921     DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
922     const TargetSchedModel *TSchedModel = DAGInstrs->getSchedModel();
923     if (!TSchedModel || DAG->SUnits.empty())
924       return;
925 
926     // Scan for MFMA long latency instructions and try to add a dependency
927     // of available SALU instructions to give them a chance to fill MFMA
928     // shadow. That is desirable to fill MFMA shadow with SALU instructions
929     // rather than VALU to prevent power consumption bursts and throttle.
930     auto LastSALU = DAG->SUnits.begin();
931     auto E = DAG->SUnits.end();
932     SmallPtrSet<SUnit*, 32> Visited;
933     for (SUnit &SU : DAG->SUnits) {
934       MachineInstr &MAI = *SU.getInstr();
935       if (!TII->isMAI(MAI) ||
936            MAI.getOpcode() == AMDGPU::V_ACCVGPR_WRITE_B32_e64 ||
937            MAI.getOpcode() == AMDGPU::V_ACCVGPR_READ_B32_e64)
938         continue;
939 
940       unsigned Lat = TSchedModel->computeInstrLatency(&MAI) - 1;
941 
942       LLVM_DEBUG(dbgs() << "Found MFMA: "; DAG->dumpNode(SU);
943                  dbgs() << "Need " << Lat
944                         << " instructions to cover latency.\n");
945 
946       // Find up to Lat independent scalar instructions as early as
947       // possible such that they can be scheduled after this MFMA.
948       for ( ; Lat && LastSALU != E; ++LastSALU) {
949         if (Visited.count(&*LastSALU))
950           continue;
951 
952         if (!isSALU(&*LastSALU) || !canAddEdge(&*LastSALU, &SU))
953           continue;
954 
955         Lat -= linkSALUChain(&SU, &*LastSALU, Lat, Visited);
956       }
957     }
958   }
959 };
960 } // namespace
961 
962 void GCNSubtarget::getPostRAMutations(
963     std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
964   Mutations.push_back(std::make_unique<FillMFMAShadowMutation>(&InstrInfo));
965 }
966 
967 std::unique_ptr<ScheduleDAGMutation>
968 GCNSubtarget::createFillMFMAShadowMutation(const TargetInstrInfo *TII) const {
969   return EnablePowerSched ? std::make_unique<FillMFMAShadowMutation>(&InstrInfo)
970                           : nullptr;
971 }
972 
973 const AMDGPUSubtarget &AMDGPUSubtarget::get(const MachineFunction &MF) {
974   if (MF.getTarget().getTargetTriple().getArch() == Triple::amdgcn)
975     return static_cast<const AMDGPUSubtarget&>(MF.getSubtarget<GCNSubtarget>());
976   else
977     return static_cast<const AMDGPUSubtarget&>(MF.getSubtarget<R600Subtarget>());
978 }
979 
980 const AMDGPUSubtarget &AMDGPUSubtarget::get(const TargetMachine &TM, const Function &F) {
981   if (TM.getTargetTriple().getArch() == Triple::amdgcn)
982     return static_cast<const AMDGPUSubtarget&>(TM.getSubtarget<GCNSubtarget>(F));
983   else
984     return static_cast<const AMDGPUSubtarget&>(TM.getSubtarget<R600Subtarget>(F));
985 }
986