LLVM  5.0.0
AMDGPUBaseInfo.cpp
Go to the documentation of this file.
1 //===- AMDGPUBaseInfo.cpp - AMDGPU Base encoding 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 #include "AMDGPUBaseInfo.h"
11 #include "AMDGPU.h"
12 #include "SIDefines.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCInstrDesc.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/Support/Casting.h"
33 #include <algorithm>
34 #include <cassert>
35 #include <cstdint>
36 #include <cstring>
37 #include <utility>
38 
40 
41 #define GET_INSTRINFO_NAMED_OPS
42 #include "AMDGPUGenInstrInfo.inc"
43 #undef GET_INSTRINFO_NAMED_OPS
44 
45 namespace {
46 
47 /// \returns Bit mask for given bit \p Shift and bit \p Width.
48 unsigned getBitMask(unsigned Shift, unsigned Width) {
49  return ((1 << Width) - 1) << Shift;
50 }
51 
52 /// \brief Packs \p Src into \p Dst for given bit \p Shift and bit \p Width.
53 ///
54 /// \returns Packed \p Dst.
55 unsigned packBits(unsigned Src, unsigned Dst, unsigned Shift, unsigned Width) {
56  Dst &= ~(1 << Shift) & ~getBitMask(Shift, Width);
57  Dst |= (Src << Shift) & getBitMask(Shift, Width);
58  return Dst;
59 }
60 
61 /// \brief Unpacks bits from \p Src for given bit \p Shift and bit \p Width.
62 ///
63 /// \returns Unpacked bits.
64 unsigned unpackBits(unsigned Src, unsigned Shift, unsigned Width) {
65  return (Src & getBitMask(Shift, Width)) >> Shift;
66 }
67 
68 /// \returns Vmcnt bit shift (lower bits).
69 unsigned getVmcntBitShiftLo() { return 0; }
70 
71 /// \returns Vmcnt bit width (lower bits).
72 unsigned getVmcntBitWidthLo() { return 4; }
73 
74 /// \returns Expcnt bit shift.
75 unsigned getExpcntBitShift() { return 4; }
76 
77 /// \returns Expcnt bit width.
78 unsigned getExpcntBitWidth() { return 3; }
79 
80 /// \returns Lgkmcnt bit shift.
81 unsigned getLgkmcntBitShift() { return 8; }
82 
83 /// \returns Lgkmcnt bit width.
84 unsigned getLgkmcntBitWidth() { return 4; }
85 
86 /// \returns Vmcnt bit shift (higher bits).
87 unsigned getVmcntBitShiftHi() { return 14; }
88 
89 /// \returns Vmcnt bit width (higher bits).
90 unsigned getVmcntBitWidthHi() { return 2; }
91 
92 } // end namespace anonymous
93 
94 namespace llvm {
95 
96 static cl::opt<bool> EnablePackedInlinableLiterals(
97  "enable-packed-inlinable-literals",
98  cl::desc("Enable packed inlinable literals (v2f16, v2i16)"),
99  cl::init(false));
100 
101 namespace AMDGPU {
102 
103 namespace IsaInfo {
104 
106  // SI.
107  if (Features.test(FeatureISAVersion6_0_0))
108  return {6, 0, 0};
109  if (Features.test(FeatureISAVersion6_0_1))
110  return {6, 0, 1};
111  // CI.
112  if (Features.test(FeatureISAVersion7_0_0))
113  return {7, 0, 0};
114  if (Features.test(FeatureISAVersion7_0_1))
115  return {7, 0, 1};
116  if (Features.test(FeatureISAVersion7_0_2))
117  return {7, 0, 2};
118  if (Features.test(FeatureISAVersion7_0_3))
119  return {7, 0, 3};
120 
121  // VI.
122  if (Features.test(FeatureISAVersion8_0_0))
123  return {8, 0, 0};
124  if (Features.test(FeatureISAVersion8_0_1))
125  return {8, 0, 1};
126  if (Features.test(FeatureISAVersion8_0_2))
127  return {8, 0, 2};
128  if (Features.test(FeatureISAVersion8_0_3))
129  return {8, 0, 3};
130  if (Features.test(FeatureISAVersion8_0_4))
131  return {8, 0, 4};
132  if (Features.test(FeatureISAVersion8_1_0))
133  return {8, 1, 0};
134 
135  // GFX9.
136  if (Features.test(FeatureISAVersion9_0_0))
137  return {9, 0, 0};
138  if (Features.test(FeatureISAVersion9_0_1))
139  return {9, 0, 1};
140  if (Features.test(FeatureISAVersion9_0_2))
141  return {9, 0, 2};
142  if (Features.test(FeatureISAVersion9_0_3))
143  return {9, 0, 3};
144 
145  if (!Features.test(FeatureGCN) || Features.test(FeatureSouthernIslands))
146  return {0, 0, 0};
147  return {7, 0, 0};
148 }
149 
151  if (Features.test(FeatureWavefrontSize16))
152  return 16;
153  if (Features.test(FeatureWavefrontSize32))
154  return 32;
155 
156  return 64;
157 }
158 
160  if (Features.test(FeatureLocalMemorySize32768))
161  return 32768;
162  if (Features.test(FeatureLocalMemorySize65536))
163  return 65536;
164 
165  return 0;
166 }
167 
169  return 4;
170 }
171 
173  unsigned FlatWorkGroupSize) {
174  if (!Features.test(FeatureGCN))
175  return 8;
176  unsigned N = getWavesPerWorkGroup(Features, FlatWorkGroupSize);
177  if (N == 1)
178  return 40;
179  N = 40 / N;
180  return std::min(N, 16u);
181 }
182 
184  return getMaxWavesPerEU(Features) * getEUsPerCU(Features);
185 }
186 
188  unsigned FlatWorkGroupSize) {
189  return getWavesPerWorkGroup(Features, FlatWorkGroupSize);
190 }
191 
193  return 1;
194 }
195 
197  if (!Features.test(FeatureGCN))
198  return 8;
199  // FIXME: Need to take scratch memory into account.
200  return 10;
201 }
202 
204  unsigned FlatWorkGroupSize) {
205  return alignTo(getMaxWavesPerCU(Features, FlatWorkGroupSize),
206  getEUsPerCU(Features)) / getEUsPerCU(Features);
207 }
208 
210  return 1;
211 }
212 
214  return 2048;
215 }
216 
218  unsigned FlatWorkGroupSize) {
219  return alignTo(FlatWorkGroupSize, getWavefrontSize(Features)) /
220  getWavefrontSize(Features);
221 }
222 
224  IsaVersion Version = getIsaVersion(Features);
225  if (Version.Major >= 8)
226  return 16;
227  return 8;
228 }
229 
231  return 8;
232 }
233 
235  IsaVersion Version = getIsaVersion(Features);
236  if (Version.Major >= 8)
237  return 800;
238  return 512;
239 }
240 
242  if (Features.test(FeatureSGPRInitBug))
244 
245  IsaVersion Version = getIsaVersion(Features);
246  if (Version.Major >= 8)
247  return 102;
248  return 104;
249 }
250 
251 unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
252  assert(WavesPerEU != 0);
253 
254  if (WavesPerEU >= getMaxWavesPerEU(Features))
255  return 0;
256  unsigned MinNumSGPRs =
257  alignDown(getTotalNumSGPRs(Features) / (WavesPerEU + 1),
258  getSGPRAllocGranule(Features)) + 1;
259  return std::min(MinNumSGPRs, getAddressableNumSGPRs(Features));
260 }
261 
262 unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
263  bool Addressable) {
264  assert(WavesPerEU != 0);
265 
266  IsaVersion Version = getIsaVersion(Features);
267  unsigned MaxNumSGPRs = alignDown(getTotalNumSGPRs(Features) / WavesPerEU,
268  getSGPRAllocGranule(Features));
269  unsigned AddressableNumSGPRs = getAddressableNumSGPRs(Features);
270  if (Version.Major >= 8 && !Addressable)
271  AddressableNumSGPRs = 112;
272  return std::min(MaxNumSGPRs, AddressableNumSGPRs);
273 }
274 
276  return 4;
277 }
278 
280  return getVGPRAllocGranule(Features);
281 }
282 
284  return 256;
285 }
286 
288  return getTotalNumVGPRs(Features);
289 }
290 
291 unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
292  assert(WavesPerEU != 0);
293 
294  if (WavesPerEU >= getMaxWavesPerEU(Features))
295  return 0;
296  unsigned MinNumVGPRs =
297  alignDown(getTotalNumVGPRs(Features) / (WavesPerEU + 1),
298  getVGPRAllocGranule(Features)) + 1;
299  return std::min(MinNumVGPRs, getAddressableNumVGPRs(Features));
300 }
301 
302 unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU) {
303  assert(WavesPerEU != 0);
304 
305  unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(Features) / WavesPerEU,
306  getVGPRAllocGranule(Features));
307  unsigned AddressableNumVGPRs = getAddressableNumVGPRs(Features);
308  return std::min(MaxNumVGPRs, AddressableNumVGPRs);
309 }
310 
311 } // end namespace IsaInfo
312 
314  const FeatureBitset &Features) {
316 
317  memset(&Header, 0, sizeof(Header));
318 
321  Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
322  Header.amd_machine_version_major = ISA.Major;
323  Header.amd_machine_version_minor = ISA.Minor;
325  Header.kernel_code_entry_byte_offset = sizeof(Header);
326  // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
327  Header.wavefront_size = 6;
328 
329  // If the code object does not support indirect functions, then the value must
330  // be 0xffffffff.
331  Header.call_convention = -1;
332 
333  // These alignment values are specified in powers of two, so alignment =
334  // 2^n. The minimum alignment is 2^4 = 16.
335  Header.kernarg_segment_alignment = 4;
336  Header.group_segment_alignment = 4;
337  Header.private_segment_alignment = 4;
338 }
339 
340 bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS) {
341  return GV->getType()->getAddressSpace() == AS.LOCAL_ADDRESS;
342 }
343 
344 bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS) {
345  return GV->getType()->getAddressSpace() == AS.GLOBAL_ADDRESS;
346 }
347 
349  return GV->getType()->getAddressSpace() == AS.CONSTANT_ADDRESS;
350 }
351 
353  return TT.getOS() != Triple::AMDHSA;
354 }
355 
356 int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
357  Attribute A = F.getFnAttribute(Name);
358  int Result = Default;
359 
360  if (A.isStringAttribute()) {
361  StringRef Str = A.getValueAsString();
362  if (Str.getAsInteger(0, Result)) {
363  LLVMContext &Ctx = F.getContext();
364  Ctx.emitError("can't parse integer attribute " + Name);
365  }
366  }
367 
368  return Result;
369 }
370 
371 std::pair<int, int> getIntegerPairAttribute(const Function &F,
372  StringRef Name,
373  std::pair<int, int> Default,
374  bool OnlyFirstRequired) {
375  Attribute A = F.getFnAttribute(Name);
376  if (!A.isStringAttribute())
377  return Default;
378 
379  LLVMContext &Ctx = F.getContext();
380  std::pair<int, int> Ints = Default;
381  std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
382  if (Strs.first.trim().getAsInteger(0, Ints.first)) {
383  Ctx.emitError("can't parse first integer attribute " + Name);
384  return Default;
385  }
386  if (Strs.second.trim().getAsInteger(0, Ints.second)) {
387  if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
388  Ctx.emitError("can't parse second integer attribute " + Name);
389  return Default;
390  }
391  }
392 
393  return Ints;
394 }
395 
397  unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
398  if (Version.Major < 9)
399  return VmcntLo;
400 
401  unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
402  return VmcntLo | VmcntHi;
403 }
404 
406  return (1 << getExpcntBitWidth()) - 1;
407 }
408 
410  return (1 << getLgkmcntBitWidth()) - 1;
411 }
412 
414  unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
415  unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
416  unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
417  unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
418  if (Version.Major < 9)
419  return Waitcnt;
420 
421  unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
422  return Waitcnt | VmcntHi;
423 }
424 
425 unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
426  unsigned VmcntLo =
427  unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
428  if (Version.Major < 9)
429  return VmcntLo;
430 
431  unsigned VmcntHi =
432  unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
433  VmcntHi <<= getVmcntBitWidthLo();
434  return VmcntLo | VmcntHi;
435 }
436 
437 unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
438  return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
439 }
440 
441 unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt) {
442  return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
443 }
444 
445 void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
446  unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
447  Vmcnt = decodeVmcnt(Version, Waitcnt);
448  Expcnt = decodeExpcnt(Version, Waitcnt);
449  Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
450 }
451 
452 unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
453  unsigned Vmcnt) {
454  Waitcnt =
455  packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
456  if (Version.Major < 9)
457  return Waitcnt;
458 
459  Vmcnt >>= getVmcntBitWidthLo();
460  return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
461 }
462 
463 unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
464  unsigned Expcnt) {
465  return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
466 }
467 
468 unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
469  unsigned Lgkmcnt) {
470  return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
471 }
472 
474  unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
475  unsigned Waitcnt = getWaitcntBitMask(Version);
476  Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
477  Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
478  Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
479  return Waitcnt;
480 }
481 
482 unsigned getInitialPSInputAddr(const Function &F) {
483  return getIntegerAttribute(F, "InitialPSInputAddr", 0);
484 }
485 
487  switch(cc) {
493  return true;
494  default:
495  return false;
496  }
497 }
498 
500  return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
501 }
502 
504  switch (CC) {
512  return true;
513  default:
514  return false;
515  }
516 }
517 
518 bool isSI(const MCSubtargetInfo &STI) {
519  return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
520 }
521 
522 bool isCI(const MCSubtargetInfo &STI) {
523  return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
524 }
525 
526 bool isVI(const MCSubtargetInfo &STI) {
527  return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
528 }
529 
530 bool isGFX9(const MCSubtargetInfo &STI) {
531  return STI.getFeatureBits()[AMDGPU::FeatureGFX9];
532 }
533 
534 bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) {
535  const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID);
536  const unsigned FirstSubReg = TRI->getSubReg(Reg, 1);
537  return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) ||
538  Reg == AMDGPU::SCC;
539 }
540 
541 bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI) {
542  for (MCRegAliasIterator R(Reg0, TRI, true); R.isValid(); ++R) {
543  if (*R == Reg1) return true;
544  }
545  return false;
546 }
547 
548 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
549 
550  switch(Reg) {
551  default: break;
552  case AMDGPU::FLAT_SCR:
553  assert(!isSI(STI));
554  return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi;
555 
556  case AMDGPU::FLAT_SCR_LO:
557  assert(!isSI(STI));
558  return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi;
559 
560  case AMDGPU::FLAT_SCR_HI:
561  assert(!isSI(STI));
562  return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi;
563  }
564  return Reg;
565 }
566 
567 unsigned mc2PseudoReg(unsigned Reg) {
568  switch (Reg) {
569  case AMDGPU::FLAT_SCR_ci:
570  case AMDGPU::FLAT_SCR_vi:
571  return FLAT_SCR;
572 
573  case AMDGPU::FLAT_SCR_LO_ci:
574  case AMDGPU::FLAT_SCR_LO_vi:
575  return AMDGPU::FLAT_SCR_LO;
576 
577  case AMDGPU::FLAT_SCR_HI_ci:
578  case AMDGPU::FLAT_SCR_HI_vi:
579  return AMDGPU::FLAT_SCR_HI;
580 
581  default:
582  return Reg;
583  }
584 }
585 
586 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
587  assert(OpNo < Desc.NumOperands);
588  unsigned OpType = Desc.OpInfo[OpNo].OperandType;
589  return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
590  OpType <= AMDGPU::OPERAND_SRC_LAST;
591 }
592 
593 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
594  assert(OpNo < Desc.NumOperands);
595  unsigned OpType = Desc.OpInfo[OpNo].OperandType;
596  switch (OpType) {
604  return true;
605  default:
606  return false;
607  }
608 }
609 
610 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
611  assert(OpNo < Desc.NumOperands);
612  unsigned OpType = Desc.OpInfo[OpNo].OperandType;
613  return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
615 }
616 
617 // Avoid using MCRegisterClass::getSize, since that function will go away
618 // (move from MC* level to Target* level). Return size in bits.
619 unsigned getRegBitWidth(unsigned RCID) {
620  switch (RCID) {
621  case AMDGPU::SGPR_32RegClassID:
622  case AMDGPU::VGPR_32RegClassID:
623  case AMDGPU::VS_32RegClassID:
624  case AMDGPU::SReg_32RegClassID:
625  case AMDGPU::SReg_32_XM0RegClassID:
626  return 32;
627  case AMDGPU::SGPR_64RegClassID:
628  case AMDGPU::VS_64RegClassID:
629  case AMDGPU::SReg_64RegClassID:
630  case AMDGPU::VReg_64RegClassID:
631  return 64;
632  case AMDGPU::VReg_96RegClassID:
633  return 96;
634  case AMDGPU::SGPR_128RegClassID:
635  case AMDGPU::SReg_128RegClassID:
636  case AMDGPU::VReg_128RegClassID:
637  return 128;
638  case AMDGPU::SReg_256RegClassID:
639  case AMDGPU::VReg_256RegClassID:
640  return 256;
641  case AMDGPU::SReg_512RegClassID:
642  case AMDGPU::VReg_512RegClassID:
643  return 512;
644  default:
645  llvm_unreachable("Unexpected register class");
646  }
647 }
648 
649 unsigned getRegBitWidth(const MCRegisterClass &RC) {
650  return getRegBitWidth(RC.getID());
651 }
652 
654  unsigned OpNo) {
655  assert(OpNo < Desc.NumOperands);
656  unsigned RCID = Desc.OpInfo[OpNo].RegClass;
657  return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
658 }
659 
660 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
661  if (Literal >= -16 && Literal <= 64)
662  return true;
663 
664  uint64_t Val = static_cast<uint64_t>(Literal);
665  return (Val == DoubleToBits(0.0)) ||
666  (Val == DoubleToBits(1.0)) ||
667  (Val == DoubleToBits(-1.0)) ||
668  (Val == DoubleToBits(0.5)) ||
669  (Val == DoubleToBits(-0.5)) ||
670  (Val == DoubleToBits(2.0)) ||
671  (Val == DoubleToBits(-2.0)) ||
672  (Val == DoubleToBits(4.0)) ||
673  (Val == DoubleToBits(-4.0)) ||
674  (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
675 }
676 
677 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
678  if (Literal >= -16 && Literal <= 64)
679  return true;
680 
681  // The actual type of the operand does not seem to matter as long
682  // as the bits match one of the inline immediate values. For example:
683  //
684  // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
685  // so it is a legal inline immediate.
686  //
687  // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
688  // floating-point, so it is a legal inline immediate.
689 
690  uint32_t Val = static_cast<uint32_t>(Literal);
691  return (Val == FloatToBits(0.0f)) ||
692  (Val == FloatToBits(1.0f)) ||
693  (Val == FloatToBits(-1.0f)) ||
694  (Val == FloatToBits(0.5f)) ||
695  (Val == FloatToBits(-0.5f)) ||
696  (Val == FloatToBits(2.0f)) ||
697  (Val == FloatToBits(-2.0f)) ||
698  (Val == FloatToBits(4.0f)) ||
699  (Val == FloatToBits(-4.0f)) ||
700  (Val == 0x3e22f983 && HasInv2Pi);
701 }
702 
703 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
704  if (!HasInv2Pi)
705  return false;
706 
707  if (Literal >= -16 && Literal <= 64)
708  return true;
709 
710  uint16_t Val = static_cast<uint16_t>(Literal);
711  return Val == 0x3C00 || // 1.0
712  Val == 0xBC00 || // -1.0
713  Val == 0x3800 || // 0.5
714  Val == 0xB800 || // -0.5
715  Val == 0x4000 || // 2.0
716  Val == 0xC000 || // -2.0
717  Val == 0x4400 || // 4.0
718  Val == 0xC400 || // -4.0
719  Val == 0x3118; // 1/2pi
720 }
721 
722 bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
723  assert(HasInv2Pi);
724 
726  return false;
727 
728  int16_t Lo16 = static_cast<int16_t>(Literal);
729  int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
730  return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
731 }
732 
733 bool isUniformMMO(const MachineMemOperand *MMO) {
734  const Value *Ptr = MMO->getValue();
735  // UndefValue means this is a load of a kernel input. These are uniform.
736  // Sometimes LDS instructions have constant pointers.
737  // If Ptr is null, then that means this mem operand contains a
738  // PseudoSourceValue like GOT.
739  if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
740  isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
741  return true;
742 
743  const Instruction *I = dyn_cast<Instruction>(Ptr);
744  return I && I->getMetadata("amdgpu.uniform");
745 }
746 
747 int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
748  if (isSI(ST) || isCI(ST))
749  return ByteOffset >> 2;
750 
751  return ByteOffset;
752 }
753 
754 bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
755  int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
756  return isSI(ST) || isCI(ST) ? isUInt<8>(EncodedOffset) :
757  isUInt<20>(EncodedOffset);
758 }
759 } // end namespace AMDGPU
760 
761 } // end namespace llvm
762 
763 const unsigned AMDGPUAS::MAX_COMMON_ADDRESS;
764 const unsigned AMDGPUAS::GLOBAL_ADDRESS;
765 const unsigned AMDGPUAS::LOCAL_ADDRESS;
766 const unsigned AMDGPUAS::PARAM_D_ADDRESS;
767 const unsigned AMDGPUAS::PARAM_I_ADDRESS;
768 const unsigned AMDGPUAS::CONSTANT_BUFFER_0;
769 const unsigned AMDGPUAS::CONSTANT_BUFFER_1;
770 const unsigned AMDGPUAS::CONSTANT_BUFFER_2;
771 const unsigned AMDGPUAS::CONSTANT_BUFFER_3;
772 const unsigned AMDGPUAS::CONSTANT_BUFFER_4;
773 const unsigned AMDGPUAS::CONSTANT_BUFFER_5;
774 const unsigned AMDGPUAS::CONSTANT_BUFFER_6;
775 const unsigned AMDGPUAS::CONSTANT_BUFFER_7;
776 const unsigned AMDGPUAS::CONSTANT_BUFFER_8;
777 const unsigned AMDGPUAS::CONSTANT_BUFFER_9;
778 const unsigned AMDGPUAS::CONSTANT_BUFFER_10;
779 const unsigned AMDGPUAS::CONSTANT_BUFFER_11;
780 const unsigned AMDGPUAS::CONSTANT_BUFFER_12;
781 const unsigned AMDGPUAS::CONSTANT_BUFFER_13;
782 const unsigned AMDGPUAS::CONSTANT_BUFFER_14;
783 const unsigned AMDGPUAS::CONSTANT_BUFFER_15;
784 const unsigned AMDGPUAS::UNKNOWN_ADDRESS_SPACE;
785 
786 namespace llvm {
787 namespace AMDGPU {
788 
790  auto Env = T.getEnvironmentName();
791  AMDGPUAS AS;
792  if (Env == "amdgiz" || Env == "amdgizcl") {
793  AS.FLAT_ADDRESS = 0;
794  AS.PRIVATE_ADDRESS = 5;
795  AS.REGION_ADDRESS = 4;
796  }
797  else {
798  AS.FLAT_ADDRESS = 4;
799  AS.PRIVATE_ADDRESS = 0;
800  AS.REGION_ADDRESS = 5;
801  }
802  return AS;
803 }
804 
806  return getAMDGPUAS(M.getTargetTriple());
807 }
808 
810  return getAMDGPUAS(Triple(M.getTargetTriple()));
811 }
812 } // namespace AMDGPU
813 } // namespace llvm
Calling convention used for Mesa compute shaders.
Definition: CallingConv.h:196
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:290
static const unsigned UNKNOWN_ADDRESS_SPACE
Definition: AMDGPU.h:202
static const unsigned CONSTANT_BUFFER_3
Definition: AMDGPU.h:187
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU)
static const unsigned CONSTANT_BUFFER_12
Definition: AMDGPU.h:196
unsigned mc2PseudoReg(unsigned Reg)
Convert hardware register Reg to a pseudo register.
unsigned getAddressableNumVGPRs(const FeatureBitset &Features)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
static const unsigned CONSTANT_BUFFER_8
Definition: AMDGPU.h:192
static const unsigned CONSTANT_BUFFER_5
Definition: AMDGPU.h:189
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS)
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features, unsigned FlatWorkGroupSize)
AMDGPUAS getAMDGPUAS(const Module &M)
unsigned getRegBitWidth(unsigned RCID)
Get the size in bits of a register from the register class RC.
Calling convention used for Mesa geometry shaders.
Definition: CallingConv.h:190
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:262
unsigned getTotalNumVGPRs(const FeatureBitset &Features)
unsigned getMinWavesPerEU(const FeatureBitset &Features)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:495
bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi)
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:233
unsigned getWavesPerWorkGroup(const FeatureBitset &Features, unsigned FlatWorkGroupSize)
static const unsigned LOCAL_ADDRESS
Address space for local memory.
Definition: AMDGPU.h:175
std::pair< int, int > getIntegerPairAttribute(const Function &F, StringRef Name, std::pair< int, int > Default, bool OnlyFirstRequired)
unsigned getVGPREncodingGranule(const FeatureBitset &Features)
const Triple & getTargetTriple() const
bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi)
uint32_t amd_kernel_code_version_major
unsigned getEUsPerCU(const FeatureBitset &Features)
uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the largest uint64_t less than or equal to Value and is Skew mod Align.
Definition: MathExtras.h:705
A description of a memory reference used in the backend.
unsigned getWavefrontSize(const FeatureBitset &Features)
bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS)
unsigned getVGPRAllocGranule(const FeatureBitset &Features)
bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo *TRI)
Is there any intersection between registers.
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
Definition: AMDGPU.h:164
static const unsigned CONSTANT_BUFFER_15
Definition: AMDGPU.h:199
AMD Kernel Code Object (amd_kernel_code_t).
unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc, unsigned OpNo)
Get size of register operand.
uint16_t amd_machine_version_major
Reg
All possible values of the reg field in the ModR/M byte.
This file contains the simple types necessary to represent the attributes associated with functions a...
unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features)
unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt, unsigned Lgkmcnt)
unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt)
unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt, unsigned Expcnt)
unsigned getMaxWavesPerCU(const FeatureBitset &Features)
Calling convention used for Mesa hull shaders.
Definition: CallingConv.h:206
unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU, bool Addressable)
unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version)
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
uint8_t OperandType
Information about the type of the operand.
Definition: MCInstrDesc.h:82
static const unsigned CONSTANT_BUFFER_9
Definition: AMDGPU.h:193
uint8_t kernarg_segment_alignment
The maximum byte alignment of variables used by the kernel in the specified memory segment...
#define F(x, y, z)
Definition: MD5.cpp:55
uint8_t group_segment_alignment
unsigned getMaxWavesPerEU(const FeatureBitset &Features)
static cl::opt< bool > EnablePackedInlinableLiterals("enable-packed-inlinable-literals", cl::desc("Enable packed inlinable literals (v2f16, v2i16)"), cl::init(false))
uint16_t amd_machine_version_minor
static const unsigned CONSTANT_BUFFER_7
Definition: AMDGPU.h:191
unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features)
unsigned getID() const
getID() - Return the register class ID number.
uint32_t amd_kernel_code_version_minor
MCRegisterClass - Base class of TargetRegisterClass.
const char * Name
unsigned short NumOperands
Definition: MCInstrDesc.h:166
uint32_t FloatToBits(float Float)
This function takes a float and returns the bit equivalent 32-bit integer.
Definition: MathExtras.h:593
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo)
Is this floating-point operand?
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:137
StringRef getEnvironmentName() const
getEnvironmentName - Get the optional environment (fourth) component of the triple, or "" if empty.
Definition: Triple.cpp:936
static const unsigned CONSTANT_BUFFER_4
Definition: AMDGPU.h:188
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset)
bool isCompute(CallingConv::ID cc)
bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi)
Is this literal inlinable.
bool isSI(const MCSubtargetInfo &STI)
constexpr bool isUInt< 8 >(uint64_t x)
Definition: MathExtras.h:335
unsigned const MachineRegisterInfo * MRI
Container class for subtarget features.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt, unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt)
Decodes Vmcnt, Expcnt and Lgkmcnt from given Waitcnt for given isa Version, and writes decoded values...
unsigned getTotalNumSGPRs(const FeatureBitset &Features)
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned getSubReg(unsigned Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo...
#define A
Definition: LargeTest.cpp:12
bool isEntryFunctionCC(CallingConv::ID CC)
unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU)
uint8_t private_segment_alignment
MCRegAliasIterator enumerates all registers aliasing Reg.
Instruction set architecture version.
static const unsigned PARAM_D_ADDRESS
Address space for direct addressible parameter memory (CONST0)
Definition: AMDGPU.h:176
static const unsigned MAX_COMMON_ADDRESS
Definition: AMDGPU.h:171
bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset)
unsigned REGION_ADDRESS
Address space for region memory.
Definition: AMDGPU.h:168
bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo)
Does this opearnd support only inlinable literals?
Calling convention used for Mesa vertex shaders.
Definition: CallingConv.h:187
void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header, const FeatureBitset &Features)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static const unsigned CONSTANT_BUFFER_13
Definition: AMDGPU.h:197
unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt)
static const unsigned CONSTANT_BUFFER_0
Definition: AMDGPU.h:184
unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version)
bool contains(unsigned Reg) const
contains - Return true if the specified register is included in this register class.
Calling convention used for Mesa pixel shaders.
Definition: CallingConv.h:193
Module.h This file contains the declarations for the Module class.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:190
static const unsigned CONSTANT_BUFFER_10
Definition: AMDGPU.h:194
const size_t N
uint16_t amd_machine_version_stepping
static const unsigned CONSTANT_BUFFER_14
Definition: AMDGPU.h:198
unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version)
uint8_t wavefront_size
Wavefront size expressed as a power of two.
const FeatureBitset & getFeatureBits() const
getFeatureBits - Return the feature bits.
static const unsigned CONSTANT_BUFFER_6
Definition: AMDGPU.h:190
uint64_t DoubleToBits(double Double)
This function takes a double and returns the bit equivalent 64-bit integer.
Definition: MathExtras.h:583
static const unsigned CONSTANT_BUFFER_2
Definition: AMDGPU.h:186
unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt, unsigned Vmcnt)
static const unsigned PARAM_I_ADDRESS
Address space for indirect addressible parameter memory (VTX1)
Definition: AMDGPU.h:177
const Value * getValue() const
Return the base address of the memory access.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
bool isShader(CallingConv::ID cc)
static const unsigned CONSTANT_BUFFER_11
Definition: AMDGPU.h:195
bool isCI(const MCSubtargetInfo &STI)
llvm_unreachable("Unknown RegBank!")
unsigned getInitialPSInputAddr(const Function &F)
bool isGFX9(const MCSubtargetInfo &STI)
Provides AMDGPU specific target descriptions.
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:260
static const unsigned CONSTANT_BUFFER_1
Definition: AMDGPU.h:185
bool isVI(const MCSubtargetInfo &STI)
int16_t RegClass
This specifies the register class enumeration of the operand if the operand is a register.
Definition: MCInstrDesc.h:76
unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt)
unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt)
Encodes Vmcnt, Expcnt and Lgkmcnt into Waitcnt for given isa Version.
unsigned FLAT_ADDRESS
Address space for flat memory.
Definition: AMDGPU.h:167
int getIntegerAttribute(const Function &F, StringRef Name, int Default)
#define I(x, y, z)
Definition: MD5.cpp:58
MCSubtargetInfo - Generic base class for all target subtargets.
bool shouldEmitConstantsToTextSection(const Triple &TT)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version)
bool isStringAttribute() const
Return true if the attribute is a string (target-dependent) attribute.
Definition: Attributes.cpp:170
bool isUniformMMO(const MachineMemOperand *MMO)
static const unsigned CONSTANT_ADDRESS
Address space for constant memory (VTX2)
Definition: AMDGPU.h:174
unsigned getAddressableNumSGPRs(const FeatureBitset &Features)
unsigned getSGPREncodingGranule(const FeatureBitset &Features)
int64_t kernel_code_entry_byte_offset
Byte offset (possibly negative) from start of amd_kernel_code_t object to kernel's entry point instru...
static const unsigned GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:173
bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo)
Can this operand also contain immediate values?
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:195
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
LLVM Value Representation.
Definition: Value.h:73
bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS)
const FeatureBitset Features
const MCOperandInfo * OpInfo
Definition: MCInstrDesc.h:174
unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI)
If Reg is a pseudo reg, return the correct hardware register given STI otherwise return Reg...
uint16_t amd_machine_kind
unsigned getSGPRAllocGranule(const FeatureBitset &Features)
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:56
unsigned getLocalMemorySize(const FeatureBitset &Features)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool isSGPR(unsigned Reg, const MCRegisterInfo *TRI)
Is Reg - scalar register.
int * Ptr
unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU)
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:199
const uint64_t Version
Definition: InstrProf.h:864
IsaVersion getIsaVersion(const FeatureBitset &Features)
unsigned PRIVATE_ADDRESS
Address space for private memory.
Definition: AMDGPU.h:166