1 //===- AMDGPUBaseInfo.h - Top level definitions for AMDGPU ------*- C++ -*-===//
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 #ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
10 #define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
11 
12 #include "SIDefines.h"
13 #include "llvm/IR/CallingConv.h"
14 #include "llvm/Support/Alignment.h"
15 
16 struct amd_kernel_code_t;
17 
18 namespace llvm {
19 
20 struct Align;
21 class Argument;
22 class Function;
23 class GCNSubtarget;
24 class GlobalValue;
25 class MCRegisterClass;
26 class MCRegisterInfo;
27 class MCSubtargetInfo;
28 class StringRef;
29 class Triple;
30 
31 namespace amdhsa {
32 struct kernel_descriptor_t;
33 }
34 
35 namespace AMDGPU {
36 
37 struct IsaVersion;
38 
39 /// \returns HSA OS ABI Version identification.
40 Optional<uint8_t> getHsaAbiVersion(const MCSubtargetInfo *STI);
41 /// \returns True if HSA OS ABI Version identification is 2,
42 /// false otherwise.
43 bool isHsaAbiVersion2(const MCSubtargetInfo *STI);
44 /// \returns True if HSA OS ABI Version identification is 3,
45 /// false otherwise.
46 bool isHsaAbiVersion3(const MCSubtargetInfo *STI);
47 /// \returns True if HSA OS ABI Version identification is 4,
48 /// false otherwise.
49 bool isHsaAbiVersion4(const MCSubtargetInfo *STI);
50 /// \returns True if HSA OS ABI Version identification is 5,
51 /// false otherwise.
52 bool isHsaAbiVersion5(const MCSubtargetInfo *STI);
53 /// \returns True if HSA OS ABI Version identification is 3 and above,
54 /// false otherwise.
55 bool isHsaAbiVersion3AndAbove(const MCSubtargetInfo *STI);
56 
57 /// \returns The offset of the hostcall pointer argument from implicitarg_ptr
58 unsigned getHostcallImplicitArgPosition();
59 
60 /// \returns Code object version.
61 unsigned getAmdhsaCodeObjectVersion();
62 
63 struct GcnBufferFormatInfo {
64   unsigned Format;
65   unsigned BitsPerComp;
66   unsigned NumComponents;
67   unsigned NumFormat;
68   unsigned DataFormat;
69 };
70 
71 struct MAIInstInfo {
72   uint16_t Opcode;
73   bool is_dgemm;
74   bool is_gfx940_xdl;
75 };
76 
77 #define GET_MIMGBaseOpcode_DECL
78 #define GET_MIMGDim_DECL
79 #define GET_MIMGEncoding_DECL
80 #define GET_MIMGLZMapping_DECL
81 #define GET_MIMGMIPMapping_DECL
82 #define GET_MIMGBiASMapping_DECL
83 #define GET_MAIInstInfoTable_DECL
84 #include "AMDGPUGenSearchableTables.inc"
85 
86 namespace IsaInfo {
87 
88 enum {
89   // The closed Vulkan driver sets 96, which limits the wave count to 8 but
90   // doesn't spill SGPRs as much as when 80 is set.
91   FIXED_NUM_SGPRS_FOR_INIT_BUG = 96,
92   TRAP_NUM_SGPRS = 16
93 };
94 
95 enum class TargetIDSetting {
96   Unsupported,
97   Any,
98   Off,
99   On
100 };
101 
102 class AMDGPUTargetID {
103 private:
104   const MCSubtargetInfo &STI;
105   TargetIDSetting XnackSetting;
106   TargetIDSetting SramEccSetting;
107 
108 public:
109   explicit AMDGPUTargetID(const MCSubtargetInfo &STI);
110   ~AMDGPUTargetID() = default;
111 
112   /// \return True if the current xnack setting is not "Unsupported".
113   bool isXnackSupported() const {
114     return XnackSetting != TargetIDSetting::Unsupported;
115   }
116 
117   /// \returns True if the current xnack setting is "On" or "Any".
118   bool isXnackOnOrAny() const {
119     return XnackSetting == TargetIDSetting::On ||
120         XnackSetting == TargetIDSetting::Any;
121   }
122 
123   /// \returns True if current xnack setting is "On" or "Off",
124   /// false otherwise.
125   bool isXnackOnOrOff() const {
126     return getXnackSetting() == TargetIDSetting::On ||
127         getXnackSetting() == TargetIDSetting::Off;
128   }
129 
130   /// \returns The current xnack TargetIDSetting, possible options are
131   /// "Unsupported", "Any", "Off", and "On".
132   TargetIDSetting getXnackSetting() const {
133     return XnackSetting;
134   }
135 
136   /// Sets xnack setting to \p NewXnackSetting.
137   void setXnackSetting(TargetIDSetting NewXnackSetting) {
138     XnackSetting = NewXnackSetting;
139   }
140 
141   /// \return True if the current sramecc setting is not "Unsupported".
142   bool isSramEccSupported() const {
143     return SramEccSetting != TargetIDSetting::Unsupported;
144   }
145 
146   /// \returns True if the current sramecc setting is "On" or "Any".
147   bool isSramEccOnOrAny() const {
148   return SramEccSetting == TargetIDSetting::On ||
149       SramEccSetting == TargetIDSetting::Any;
150   }
151 
152   /// \returns True if current sramecc setting is "On" or "Off",
153   /// false otherwise.
154   bool isSramEccOnOrOff() const {
155     return getSramEccSetting() == TargetIDSetting::On ||
156         getSramEccSetting() == TargetIDSetting::Off;
157   }
158 
159   /// \returns The current sramecc TargetIDSetting, possible options are
160   /// "Unsupported", "Any", "Off", and "On".
161   TargetIDSetting getSramEccSetting() const {
162     return SramEccSetting;
163   }
164 
165   /// Sets sramecc setting to \p NewSramEccSetting.
166   void setSramEccSetting(TargetIDSetting NewSramEccSetting) {
167     SramEccSetting = NewSramEccSetting;
168   }
169 
170   void setTargetIDFromFeaturesString(StringRef FS);
171   void setTargetIDFromTargetIDStream(StringRef TargetID);
172 
173   /// \returns String representation of an object.
174   std::string toString() const;
175 };
176 
177 /// \returns Wavefront size for given subtarget \p STI.
178 unsigned getWavefrontSize(const MCSubtargetInfo *STI);
179 
180 /// \returns Local memory size in bytes for given subtarget \p STI.
181 unsigned getLocalMemorySize(const MCSubtargetInfo *STI);
182 
183 /// \returns Number of execution units per compute unit for given subtarget \p
184 /// STI.
185 unsigned getEUsPerCU(const MCSubtargetInfo *STI);
186 
187 /// \returns Maximum number of work groups per compute unit for given subtarget
188 /// \p STI and limited by given \p FlatWorkGroupSize.
189 unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI,
190                                unsigned FlatWorkGroupSize);
191 
192 /// \returns Minimum number of waves per execution unit for given subtarget \p
193 /// STI.
194 unsigned getMinWavesPerEU(const MCSubtargetInfo *STI);
195 
196 /// \returns Maximum number of waves per execution unit for given subtarget \p
197 /// STI without any kind of limitation.
198 unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI);
199 
200 /// \returns Number of waves per execution unit required to support the given \p
201 /// FlatWorkGroupSize.
202 unsigned getWavesPerEUForWorkGroup(const MCSubtargetInfo *STI,
203                                    unsigned FlatWorkGroupSize);
204 
205 /// \returns Minimum flat work group size for given subtarget \p STI.
206 unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI);
207 
208 /// \returns Maximum flat work group size for given subtarget \p STI.
209 unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI);
210 
211 /// \returns Number of waves per work group for given subtarget \p STI and
212 /// \p FlatWorkGroupSize.
213 unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
214                               unsigned FlatWorkGroupSize);
215 
216 /// \returns SGPR allocation granularity for given subtarget \p STI.
217 unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI);
218 
219 /// \returns SGPR encoding granularity for given subtarget \p STI.
220 unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI);
221 
222 /// \returns Total number of SGPRs for given subtarget \p STI.
223 unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI);
224 
225 /// \returns Addressable number of SGPRs for given subtarget \p STI.
226 unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI);
227 
228 /// \returns Minimum number of SGPRs that meets the given number of waves per
229 /// execution unit requirement for given subtarget \p STI.
230 unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
231 
232 /// \returns Maximum number of SGPRs that meets the given number of waves per
233 /// execution unit requirement for given subtarget \p STI.
234 unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
235                         bool Addressable);
236 
237 /// \returns Number of extra SGPRs implicitly required by given subtarget \p
238 /// STI when the given special registers are used.
239 unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
240                           bool FlatScrUsed, bool XNACKUsed);
241 
242 /// \returns Number of extra SGPRs implicitly required by given subtarget \p
243 /// STI when the given special registers are used. XNACK is inferred from
244 /// \p STI.
245 unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
246                           bool FlatScrUsed);
247 
248 /// \returns Number of SGPR blocks needed for given subtarget \p STI when
249 /// \p NumSGPRs are used. \p NumSGPRs should already include any special
250 /// register counts.
251 unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs);
252 
253 /// \returns VGPR allocation granularity for given subtarget \p STI.
254 ///
255 /// For subtargets which support it, \p EnableWavefrontSize32 should match
256 /// the ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
257 unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI,
258                              Optional<bool> EnableWavefrontSize32 = None);
259 
260 /// \returns VGPR encoding granularity for given subtarget \p STI.
261 ///
262 /// For subtargets which support it, \p EnableWavefrontSize32 should match
263 /// the ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
264 unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI,
265                                 Optional<bool> EnableWavefrontSize32 = None);
266 
267 /// \returns Total number of VGPRs for given subtarget \p STI.
268 unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI);
269 
270 /// \returns Addressable number of VGPRs for given subtarget \p STI.
271 unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI);
272 
273 /// \returns Minimum number of VGPRs that meets given number of waves per
274 /// execution unit requirement for given subtarget \p STI.
275 unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
276 
277 /// \returns Maximum number of VGPRs that meets given number of waves per
278 /// execution unit requirement for given subtarget \p STI.
279 unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
280 
281 /// \returns Number of VGPR blocks needed for given subtarget \p STI when
282 /// \p NumVGPRs are used.
283 ///
284 /// For subtargets which support it, \p EnableWavefrontSize32 should match the
285 /// ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
286 unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs,
287                           Optional<bool> EnableWavefrontSize32 = None);
288 
289 } // end namespace IsaInfo
290 
291 LLVM_READONLY
292 int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
293 
294 LLVM_READONLY
295 int getSOPPWithRelaxation(uint16_t Opcode);
296 
297 struct MIMGBaseOpcodeInfo {
298   MIMGBaseOpcode BaseOpcode;
299   bool Store;
300   bool Atomic;
301   bool AtomicX2;
302   bool Sampler;
303   bool Gather4;
304 
305   uint8_t NumExtraArgs;
306   bool Gradients;
307   bool G16;
308   bool Coordinates;
309   bool LodOrClampOrMip;
310   bool HasD16;
311   bool MSAA;
312   bool BVH;
313 };
314 
315 LLVM_READONLY
316 const MIMGBaseOpcodeInfo *getMIMGBaseOpcode(unsigned Opc);
317 
318 LLVM_READONLY
319 const MIMGBaseOpcodeInfo *getMIMGBaseOpcodeInfo(unsigned BaseOpcode);
320 
321 struct MIMGDimInfo {
322   MIMGDim Dim;
323   uint8_t NumCoords;
324   uint8_t NumGradients;
325   bool MSAA;
326   bool DA;
327   uint8_t Encoding;
328   const char *AsmSuffix;
329 };
330 
331 LLVM_READONLY
332 const MIMGDimInfo *getMIMGDimInfo(unsigned DimEnum);
333 
334 LLVM_READONLY
335 const MIMGDimInfo *getMIMGDimInfoByEncoding(uint8_t DimEnc);
336 
337 LLVM_READONLY
338 const MIMGDimInfo *getMIMGDimInfoByAsmSuffix(StringRef AsmSuffix);
339 
340 struct MIMGLZMappingInfo {
341   MIMGBaseOpcode L;
342   MIMGBaseOpcode LZ;
343 };
344 
345 struct MIMGMIPMappingInfo {
346   MIMGBaseOpcode MIP;
347   MIMGBaseOpcode NONMIP;
348 };
349 
350 struct MIMGBiasMappingInfo {
351   MIMGBaseOpcode Bias;
352   MIMGBaseOpcode NoBias;
353 };
354 
355 struct MIMGOffsetMappingInfo {
356   MIMGBaseOpcode Offset;
357   MIMGBaseOpcode NoOffset;
358 };
359 
360 struct MIMGG16MappingInfo {
361   MIMGBaseOpcode G;
362   MIMGBaseOpcode G16;
363 };
364 
365 LLVM_READONLY
366 const MIMGLZMappingInfo *getMIMGLZMappingInfo(unsigned L);
367 
368 LLVM_READONLY
369 const MIMGMIPMappingInfo *getMIMGMIPMappingInfo(unsigned MIP);
370 
371 LLVM_READONLY
372 const MIMGBiasMappingInfo *getMIMGBiasMappingInfo(unsigned Bias);
373 
374 LLVM_READONLY
375 const MIMGOffsetMappingInfo *getMIMGOffsetMappingInfo(unsigned Offset);
376 
377 LLVM_READONLY
378 const MIMGG16MappingInfo *getMIMGG16MappingInfo(unsigned G);
379 
380 LLVM_READONLY
381 int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding,
382                   unsigned VDataDwords, unsigned VAddrDwords);
383 
384 LLVM_READONLY
385 int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels);
386 
387 LLVM_READONLY
388 unsigned getAddrSizeMIMGOp(const MIMGBaseOpcodeInfo *BaseOpcode,
389                            const MIMGDimInfo *Dim, bool IsA16,
390                            bool IsG16Supported);
391 
392 struct MIMGInfo {
393   uint16_t Opcode;
394   uint16_t BaseOpcode;
395   uint8_t MIMGEncoding;
396   uint8_t VDataDwords;
397   uint8_t VAddrDwords;
398 };
399 
400 LLVM_READONLY
401 const MIMGInfo *getMIMGInfo(unsigned Opc);
402 
403 LLVM_READONLY
404 int getMTBUFBaseOpcode(unsigned Opc);
405 
406 LLVM_READONLY
407 int getMTBUFOpcode(unsigned BaseOpc, unsigned Elements);
408 
409 LLVM_READONLY
410 int getMTBUFElements(unsigned Opc);
411 
412 LLVM_READONLY
413 bool getMTBUFHasVAddr(unsigned Opc);
414 
415 LLVM_READONLY
416 bool getMTBUFHasSrsrc(unsigned Opc);
417 
418 LLVM_READONLY
419 bool getMTBUFHasSoffset(unsigned Opc);
420 
421 LLVM_READONLY
422 int getMUBUFBaseOpcode(unsigned Opc);
423 
424 LLVM_READONLY
425 int getMUBUFOpcode(unsigned BaseOpc, unsigned Elements);
426 
427 LLVM_READONLY
428 int getMUBUFElements(unsigned Opc);
429 
430 LLVM_READONLY
431 bool getMUBUFHasVAddr(unsigned Opc);
432 
433 LLVM_READONLY
434 bool getMUBUFHasSrsrc(unsigned Opc);
435 
436 LLVM_READONLY
437 bool getMUBUFHasSoffset(unsigned Opc);
438 
439 LLVM_READONLY
440 bool getMUBUFIsBufferInv(unsigned Opc);
441 
442 LLVM_READONLY
443 bool getSMEMIsBuffer(unsigned Opc);
444 
445 LLVM_READONLY
446 bool getVOP1IsSingle(unsigned Opc);
447 
448 LLVM_READONLY
449 bool getVOP2IsSingle(unsigned Opc);
450 
451 LLVM_READONLY
452 bool getVOP3IsSingle(unsigned Opc);
453 
454 /// Returns true if MAI operation is a double precision GEMM.
455 LLVM_READONLY
456 bool getMAIIsDGEMM(unsigned Opc);
457 
458 LLVM_READONLY
459 bool getMAIIsGFX940XDL(unsigned Opc);
460 
461 LLVM_READONLY
462 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t BitsPerComp,
463                                                   uint8_t NumComponents,
464                                                   uint8_t NumFormat,
465                                                   const MCSubtargetInfo &STI);
466 LLVM_READONLY
467 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t Format,
468                                                   const MCSubtargetInfo &STI);
469 
470 LLVM_READONLY
471 int getMCOpcode(uint16_t Opcode, unsigned Gen);
472 
473 void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
474                                const MCSubtargetInfo *STI);
475 
476 amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor(
477     const MCSubtargetInfo *STI);
478 
479 bool isGroupSegment(const GlobalValue *GV);
480 bool isGlobalSegment(const GlobalValue *GV);
481 bool isReadOnlySegment(const GlobalValue *GV);
482 
483 /// \returns True if constants should be emitted to .text section for given
484 /// target triple \p TT, false otherwise.
485 bool shouldEmitConstantsToTextSection(const Triple &TT);
486 
487 /// \returns Integer value requested using \p F's \p Name attribute.
488 ///
489 /// \returns \p Default if attribute is not present.
490 ///
491 /// \returns \p Default and emits error if requested value cannot be converted
492 /// to integer.
493 int getIntegerAttribute(const Function &F, StringRef Name, int Default);
494 
495 /// \returns A pair of integer values requested using \p F's \p Name attribute
496 /// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
497 /// is false).
498 ///
499 /// \returns \p Default if attribute is not present.
500 ///
501 /// \returns \p Default and emits error if one of the requested values cannot be
502 /// converted to integer, or \p OnlyFirstRequired is false and "second" value is
503 /// not present.
504 std::pair<int, int> getIntegerPairAttribute(const Function &F,
505                                             StringRef Name,
506                                             std::pair<int, int> Default,
507                                             bool OnlyFirstRequired = false);
508 
509 /// Represents the counter values to wait for in an s_waitcnt instruction.
510 ///
511 /// Large values (including the maximum possible integer) can be used to
512 /// represent "don't care" waits.
513 struct Waitcnt {
514   unsigned VmCnt = ~0u;
515   unsigned ExpCnt = ~0u;
516   unsigned LgkmCnt = ~0u;
517   unsigned VsCnt = ~0u;
518 
519   Waitcnt() = default;
520   Waitcnt(unsigned VmCnt, unsigned ExpCnt, unsigned LgkmCnt, unsigned VsCnt)
521       : VmCnt(VmCnt), ExpCnt(ExpCnt), LgkmCnt(LgkmCnt), VsCnt(VsCnt) {}
522 
523   static Waitcnt allZero(bool HasVscnt) {
524     return Waitcnt(0, 0, 0, HasVscnt ? 0 : ~0u);
525   }
526   static Waitcnt allZeroExceptVsCnt() { return Waitcnt(0, 0, 0, ~0u); }
527 
528   bool hasWait() const {
529     return VmCnt != ~0u || ExpCnt != ~0u || LgkmCnt != ~0u || VsCnt != ~0u;
530   }
531 
532   bool hasWaitExceptVsCnt() const {
533     return VmCnt != ~0u || ExpCnt != ~0u || LgkmCnt != ~0u;
534   }
535 
536   bool hasWaitVsCnt() const {
537     return VsCnt != ~0u;
538   }
539 
540   bool dominates(const Waitcnt &Other) const {
541     return VmCnt <= Other.VmCnt && ExpCnt <= Other.ExpCnt &&
542            LgkmCnt <= Other.LgkmCnt && VsCnt <= Other.VsCnt;
543   }
544 
545   Waitcnt combined(const Waitcnt &Other) const {
546     return Waitcnt(std::min(VmCnt, Other.VmCnt), std::min(ExpCnt, Other.ExpCnt),
547                    std::min(LgkmCnt, Other.LgkmCnt),
548                    std::min(VsCnt, Other.VsCnt));
549   }
550 };
551 
552 /// \returns Vmcnt bit mask for given isa \p Version.
553 unsigned getVmcntBitMask(const IsaVersion &Version);
554 
555 /// \returns Expcnt bit mask for given isa \p Version.
556 unsigned getExpcntBitMask(const IsaVersion &Version);
557 
558 /// \returns Lgkmcnt bit mask for given isa \p Version.
559 unsigned getLgkmcntBitMask(const IsaVersion &Version);
560 
561 /// \returns Waitcnt bit mask for given isa \p Version.
562 unsigned getWaitcntBitMask(const IsaVersion &Version);
563 
564 /// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
565 unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt);
566 
567 /// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
568 unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt);
569 
570 /// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
571 unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt);
572 
573 /// Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
574 /// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
575 /// \p Lgkmcnt respectively.
576 ///
577 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
578 ///     \p Vmcnt = \p Waitcnt[3:0]                      (pre-gfx9 only)
579 ///     \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14]  (gfx9+ only)
580 ///     \p Expcnt = \p Waitcnt[6:4]
581 ///     \p Lgkmcnt = \p Waitcnt[11:8]                   (pre-gfx10 only)
582 ///     \p Lgkmcnt = \p Waitcnt[13:8]                   (gfx10+ only)
583 void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
584                    unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
585 
586 Waitcnt decodeWaitcnt(const IsaVersion &Version, unsigned Encoded);
587 
588 /// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
589 unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
590                      unsigned Vmcnt);
591 
592 /// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
593 unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
594                       unsigned Expcnt);
595 
596 /// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
597 unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
598                        unsigned Lgkmcnt);
599 
600 /// Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
601 /// \p Version.
602 ///
603 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
604 ///     Waitcnt[3:0]   = \p Vmcnt       (pre-gfx9 only)
605 ///     Waitcnt[3:0]   = \p Vmcnt[3:0]  (gfx9+ only)
606 ///     Waitcnt[6:4]   = \p Expcnt
607 ///     Waitcnt[11:8]  = \p Lgkmcnt     (pre-gfx10 only)
608 ///     Waitcnt[13:8]  = \p Lgkmcnt     (gfx10+ only)
609 ///     Waitcnt[15:14] = \p Vmcnt[5:4]  (gfx9+ only)
610 ///
611 /// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
612 /// isa \p Version.
613 unsigned encodeWaitcnt(const IsaVersion &Version,
614                        unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
615 
616 unsigned encodeWaitcnt(const IsaVersion &Version, const Waitcnt &Decoded);
617 
618 namespace Hwreg {
619 
620 LLVM_READONLY
621 int64_t getHwregId(const StringRef Name, const MCSubtargetInfo &STI);
622 
623 LLVM_READNONE
624 bool isValidHwreg(int64_t Id);
625 
626 LLVM_READNONE
627 bool isValidHwregOffset(int64_t Offset);
628 
629 LLVM_READNONE
630 bool isValidHwregWidth(int64_t Width);
631 
632 LLVM_READNONE
633 uint64_t encodeHwreg(uint64_t Id, uint64_t Offset, uint64_t Width);
634 
635 LLVM_READNONE
636 StringRef getHwreg(unsigned Id, const MCSubtargetInfo &STI);
637 
638 void decodeHwreg(unsigned Val, unsigned &Id, unsigned &Offset, unsigned &Width);
639 
640 } // namespace Hwreg
641 
642 namespace Exp {
643 
644 bool getTgtName(unsigned Id, StringRef &Name, int &Index);
645 
646 LLVM_READONLY
647 unsigned getTgtId(const StringRef Name);
648 
649 LLVM_READNONE
650 bool isSupportedTgtId(unsigned Id, const MCSubtargetInfo &STI);
651 
652 } // namespace Exp
653 
654 namespace MTBUFFormat {
655 
656 LLVM_READNONE
657 int64_t encodeDfmtNfmt(unsigned Dfmt, unsigned Nfmt);
658 
659 void decodeDfmtNfmt(unsigned Format, unsigned &Dfmt, unsigned &Nfmt);
660 
661 int64_t getDfmt(const StringRef Name);
662 
663 StringRef getDfmtName(unsigned Id);
664 
665 int64_t getNfmt(const StringRef Name, const MCSubtargetInfo &STI);
666 
667 StringRef getNfmtName(unsigned Id, const MCSubtargetInfo &STI);
668 
669 bool isValidDfmtNfmt(unsigned Val, const MCSubtargetInfo &STI);
670 
671 bool isValidNfmt(unsigned Val, const MCSubtargetInfo &STI);
672 
673 int64_t getUnifiedFormat(const StringRef Name);
674 
675 StringRef getUnifiedFormatName(unsigned Id);
676 
677 bool isValidUnifiedFormat(unsigned Val);
678 
679 int64_t convertDfmtNfmt2Ufmt(unsigned Dfmt, unsigned Nfmt);
680 
681 bool isValidFormatEncoding(unsigned Val, const MCSubtargetInfo &STI);
682 
683 unsigned getDefaultFormatEncoding(const MCSubtargetInfo &STI);
684 
685 } // namespace MTBUFFormat
686 
687 namespace SendMsg {
688 
689 LLVM_READONLY
690 int64_t getMsgId(const StringRef Name, const MCSubtargetInfo &STI);
691 
692 LLVM_READONLY
693 int64_t getMsgOpId(int64_t MsgId, const StringRef Name);
694 
695 LLVM_READNONE
696 StringRef getMsgName(int64_t MsgId, const MCSubtargetInfo &STI);
697 
698 LLVM_READNONE
699 StringRef getMsgOpName(int64_t MsgId, int64_t OpId);
700 
701 LLVM_READNONE
702 bool isValidMsgId(int64_t MsgId);
703 
704 LLVM_READNONE
705 bool isValidMsgOp(int64_t MsgId, int64_t OpId, const MCSubtargetInfo &STI,
706                   bool Strict = true);
707 
708 LLVM_READNONE
709 bool isValidMsgStream(int64_t MsgId, int64_t OpId, int64_t StreamId,
710                       const MCSubtargetInfo &STI, bool Strict = true);
711 
712 LLVM_READNONE
713 bool msgRequiresOp(int64_t MsgId);
714 
715 LLVM_READNONE
716 bool msgSupportsStream(int64_t MsgId, int64_t OpId);
717 
718 void decodeMsg(unsigned Val,
719                uint16_t &MsgId,
720                uint16_t &OpId,
721                uint16_t &StreamId);
722 
723 LLVM_READNONE
724 uint64_t encodeMsg(uint64_t MsgId,
725                    uint64_t OpId,
726                    uint64_t StreamId);
727 
728 } // namespace SendMsg
729 
730 
731 unsigned getInitialPSInputAddr(const Function &F);
732 
733 bool getHasColorExport(const Function &F);
734 
735 bool getHasDepthExport(const Function &F);
736 
737 LLVM_READNONE
738 bool isShader(CallingConv::ID CC);
739 
740 LLVM_READNONE
741 bool isGraphics(CallingConv::ID CC);
742 
743 LLVM_READNONE
744 bool isCompute(CallingConv::ID CC);
745 
746 LLVM_READNONE
747 bool isEntryFunctionCC(CallingConv::ID CC);
748 
749 // These functions are considered entrypoints into the current module, i.e. they
750 // are allowed to be called from outside the current module. This is different
751 // from isEntryFunctionCC, which is only true for functions that are entered by
752 // the hardware. Module entry points include all entry functions but also
753 // include functions that can be called from other functions inside or outside
754 // the current module. Module entry functions are allowed to allocate LDS.
755 LLVM_READNONE
756 bool isModuleEntryFunctionCC(CallingConv::ID CC);
757 
758 bool isKernelCC(const Function *Func);
759 
760 // FIXME: Remove this when calling conventions cleaned up
761 LLVM_READNONE
762 inline bool isKernel(CallingConv::ID CC) {
763   switch (CC) {
764   case CallingConv::AMDGPU_KERNEL:
765   case CallingConv::SPIR_KERNEL:
766     return true;
767   default:
768     return false;
769   }
770 }
771 
772 bool hasXNACK(const MCSubtargetInfo &STI);
773 bool hasSRAMECC(const MCSubtargetInfo &STI);
774 bool hasMIMG_R128(const MCSubtargetInfo &STI);
775 bool hasGFX10A16(const MCSubtargetInfo &STI);
776 bool hasG16(const MCSubtargetInfo &STI);
777 bool hasPackedD16(const MCSubtargetInfo &STI);
778 
779 bool isSI(const MCSubtargetInfo &STI);
780 bool isCI(const MCSubtargetInfo &STI);
781 bool isVI(const MCSubtargetInfo &STI);
782 bool isGFX9(const MCSubtargetInfo &STI);
783 bool isGFX9_GFX10(const MCSubtargetInfo &STI);
784 bool isGFX8Plus(const MCSubtargetInfo &STI);
785 bool isGFX9Plus(const MCSubtargetInfo &STI);
786 bool isGFX10(const MCSubtargetInfo &STI);
787 bool isGFX10Plus(const MCSubtargetInfo &STI);
788 bool isNotGFX10Plus(const MCSubtargetInfo &STI);
789 bool isGFX10Before1030(const MCSubtargetInfo &STI);
790 bool isGCN3Encoding(const MCSubtargetInfo &STI);
791 bool isGFX10_AEncoding(const MCSubtargetInfo &STI);
792 bool isGFX10_BEncoding(const MCSubtargetInfo &STI);
793 bool hasGFX10_3Insts(const MCSubtargetInfo &STI);
794 bool isGFX90A(const MCSubtargetInfo &STI);
795 bool isGFX940(const MCSubtargetInfo &STI);
796 bool hasArchitectedFlatScratch(const MCSubtargetInfo &STI);
797 bool hasMAIInsts(const MCSubtargetInfo &STI);
798 int getTotalNumVGPRs(bool has90AInsts, int32_t ArgNumAGPR, int32_t ArgNumVGPR);
799 
800 /// Is Reg - scalar register
801 bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI);
802 
803 /// If \p Reg is a pseudo reg, return the correct hardware register given
804 /// \p STI otherwise return \p Reg.
805 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
806 
807 /// Convert hardware register \p Reg to a pseudo register
808 LLVM_READNONE
809 unsigned mc2PseudoReg(unsigned Reg);
810 
811 /// Can this operand also contain immediate values?
812 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
813 
814 /// Is this floating-point operand?
815 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
816 
817 /// Does this operand support only inlinable literals?
818 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
819 
820 /// Get the size in bits of a register from the register class \p RC.
821 unsigned getRegBitWidth(unsigned RCID);
822 
823 /// Get the size in bits of a register from the register class \p RC.
824 unsigned getRegBitWidth(const MCRegisterClass &RC);
825 
826 /// Get size of register operand
827 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
828                            unsigned OpNo);
829 
830 LLVM_READNONE
831 inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
832   switch (OpInfo.OperandType) {
833   case AMDGPU::OPERAND_REG_IMM_INT32:
834   case AMDGPU::OPERAND_REG_IMM_FP32:
835   case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED:
836   case AMDGPU::OPERAND_REG_INLINE_C_INT32:
837   case AMDGPU::OPERAND_REG_INLINE_C_FP32:
838   case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
839   case AMDGPU::OPERAND_REG_INLINE_AC_FP32:
840   case AMDGPU::OPERAND_REG_IMM_V2INT32:
841   case AMDGPU::OPERAND_REG_IMM_V2FP32:
842   case AMDGPU::OPERAND_REG_INLINE_C_V2INT32:
843   case AMDGPU::OPERAND_REG_INLINE_C_V2FP32:
844   case AMDGPU::OPERAND_KIMM32:
845   case AMDGPU::OPERAND_KIMM16: // mandatory literal is always size 4
846     return 4;
847 
848   case AMDGPU::OPERAND_REG_IMM_INT64:
849   case AMDGPU::OPERAND_REG_IMM_FP64:
850   case AMDGPU::OPERAND_REG_INLINE_C_INT64:
851   case AMDGPU::OPERAND_REG_INLINE_C_FP64:
852   case AMDGPU::OPERAND_REG_INLINE_AC_FP64:
853     return 8;
854 
855   case AMDGPU::OPERAND_REG_IMM_INT16:
856   case AMDGPU::OPERAND_REG_IMM_FP16:
857   case AMDGPU::OPERAND_REG_IMM_FP16_DEFERRED:
858   case AMDGPU::OPERAND_REG_INLINE_C_INT16:
859   case AMDGPU::OPERAND_REG_INLINE_C_FP16:
860   case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
861   case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
862   case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
863   case AMDGPU::OPERAND_REG_INLINE_AC_FP16:
864   case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16:
865   case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16:
866   case AMDGPU::OPERAND_REG_IMM_V2INT16:
867   case AMDGPU::OPERAND_REG_IMM_V2FP16:
868     return 2;
869 
870   default:
871     llvm_unreachable("unhandled operand type");
872   }
873 }
874 
875 LLVM_READNONE
876 inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
877   return getOperandSize(Desc.OpInfo[OpNo]);
878 }
879 
880 /// Is this literal inlinable, and not one of the values intended for floating
881 /// point values.
882 LLVM_READNONE
883 inline bool isInlinableIntLiteral(int64_t Literal) {
884   return Literal >= -16 && Literal <= 64;
885 }
886 
887 /// Is this literal inlinable
888 LLVM_READNONE
889 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
890 
891 LLVM_READNONE
892 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
893 
894 LLVM_READNONE
895 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
896 
897 LLVM_READNONE
898 bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
899 
900 LLVM_READNONE
901 bool isInlinableIntLiteralV216(int32_t Literal);
902 
903 LLVM_READNONE
904 bool isFoldableLiteralV216(int32_t Literal, bool HasInv2Pi);
905 
906 bool isArgPassedInSGPR(const Argument *Arg);
907 
908 LLVM_READONLY
909 bool isLegalSMRDEncodedUnsignedOffset(const MCSubtargetInfo &ST,
910                                       int64_t EncodedOffset);
911 
912 LLVM_READONLY
913 bool isLegalSMRDEncodedSignedOffset(const MCSubtargetInfo &ST,
914                                     int64_t EncodedOffset,
915                                     bool IsBuffer);
916 
917 /// Convert \p ByteOffset to dwords if the subtarget uses dword SMRD immediate
918 /// offsets.
919 uint64_t convertSMRDOffsetUnits(const MCSubtargetInfo &ST, uint64_t ByteOffset);
920 
921 /// \returns The encoding that will be used for \p ByteOffset in the
922 /// SMRD offset field, or None if it won't fit. On GFX9 and GFX10
923 /// S_LOAD instructions have a signed offset, on other subtargets it is
924 /// unsigned. S_BUFFER has an unsigned offset for all subtargets.
925 Optional<int64_t> getSMRDEncodedOffset(const MCSubtargetInfo &ST,
926                                        int64_t ByteOffset, bool IsBuffer);
927 
928 /// \return The encoding that can be used for a 32-bit literal offset in an SMRD
929 /// instruction. This is only useful on CI.s
930 Optional<int64_t> getSMRDEncodedLiteralOffset32(const MCSubtargetInfo &ST,
931                                                 int64_t ByteOffset);
932 
933 /// For FLAT segment the offset must be positive;
934 /// MSB is ignored and forced to zero.
935 ///
936 /// \return The number of bits available for the offset field in flat
937 /// instructions.
938 unsigned getNumFlatOffsetBits(const MCSubtargetInfo &ST, bool Signed);
939 
940 /// \returns true if this offset is small enough to fit in the SMRD
941 /// offset field.  \p ByteOffset should be the offset in bytes and
942 /// not the encoded offset.
943 bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
944 
945 bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset,
946                       const GCNSubtarget *Subtarget,
947                       Align Alignment = Align(4));
948 
949 LLVM_READNONE
950 inline bool isLegal64BitDPPControl(unsigned DC) {
951   return DC >= DPP::ROW_NEWBCAST_FIRST && DC <= DPP::ROW_NEWBCAST_LAST;
952 }
953 
954 /// \returns true if the intrinsic is divergent
955 bool isIntrinsicSourceOfDivergence(unsigned IntrID);
956 
957 // Track defaults for fields in the MODE register.
958 struct SIModeRegisterDefaults {
959   /// Floating point opcodes that support exception flag gathering quiet and
960   /// propagate signaling NaN inputs per IEEE 754-2008. Min_dx10 and max_dx10
961   /// become IEEE 754- 2008 compliant due to signaling NaN propagation and
962   /// quieting.
963   bool IEEE : 1;
964 
965   /// Used by the vector ALU to force DX10-style treatment of NaNs: when set,
966   /// clamp NaN to zero; otherwise, pass NaN through.
967   bool DX10Clamp : 1;
968 
969   /// If this is set, neither input or output denormals are flushed for most f32
970   /// instructions.
971   bool FP32InputDenormals : 1;
972   bool FP32OutputDenormals : 1;
973 
974   /// If this is set, neither input or output denormals are flushed for both f64
975   /// and f16/v2f16 instructions.
976   bool FP64FP16InputDenormals : 1;
977   bool FP64FP16OutputDenormals : 1;
978 
979   SIModeRegisterDefaults() :
980     IEEE(true),
981     DX10Clamp(true),
982     FP32InputDenormals(true),
983     FP32OutputDenormals(true),
984     FP64FP16InputDenormals(true),
985     FP64FP16OutputDenormals(true) {}
986 
987   SIModeRegisterDefaults(const Function &F);
988 
989   static SIModeRegisterDefaults getDefaultForCallingConv(CallingConv::ID CC) {
990     SIModeRegisterDefaults Mode;
991     Mode.IEEE = !AMDGPU::isShader(CC);
992     return Mode;
993   }
994 
995   bool operator ==(const SIModeRegisterDefaults Other) const {
996     return IEEE == Other.IEEE && DX10Clamp == Other.DX10Clamp &&
997            FP32InputDenormals == Other.FP32InputDenormals &&
998            FP32OutputDenormals == Other.FP32OutputDenormals &&
999            FP64FP16InputDenormals == Other.FP64FP16InputDenormals &&
1000            FP64FP16OutputDenormals == Other.FP64FP16OutputDenormals;
1001   }
1002 
1003   bool allFP32Denormals() const {
1004     return FP32InputDenormals && FP32OutputDenormals;
1005   }
1006 
1007   bool allFP64FP16Denormals() const {
1008     return FP64FP16InputDenormals && FP64FP16OutputDenormals;
1009   }
1010 
1011   /// Get the encoding value for the FP_DENORM bits of the mode register for the
1012   /// FP32 denormal mode.
1013   uint32_t fpDenormModeSPValue() const {
1014     if (FP32InputDenormals && FP32OutputDenormals)
1015       return FP_DENORM_FLUSH_NONE;
1016     if (FP32InputDenormals)
1017       return FP_DENORM_FLUSH_OUT;
1018     if (FP32OutputDenormals)
1019       return FP_DENORM_FLUSH_IN;
1020     return FP_DENORM_FLUSH_IN_FLUSH_OUT;
1021   }
1022 
1023   /// Get the encoding value for the FP_DENORM bits of the mode register for the
1024   /// FP64/FP16 denormal mode.
1025   uint32_t fpDenormModeDPValue() const {
1026     if (FP64FP16InputDenormals && FP64FP16OutputDenormals)
1027       return FP_DENORM_FLUSH_NONE;
1028     if (FP64FP16InputDenormals)
1029       return FP_DENORM_FLUSH_OUT;
1030     if (FP64FP16OutputDenormals)
1031       return FP_DENORM_FLUSH_IN;
1032     return FP_DENORM_FLUSH_IN_FLUSH_OUT;
1033   }
1034 
1035   /// Returns true if a flag is compatible if it's enabled in the callee, but
1036   /// disabled in the caller.
1037   static bool oneWayCompatible(bool CallerMode, bool CalleeMode) {
1038     return CallerMode == CalleeMode || (!CallerMode && CalleeMode);
1039   }
1040 
1041   // FIXME: Inlining should be OK for dx10-clamp, since the caller's mode should
1042   // be able to override.
1043   bool isInlineCompatible(SIModeRegisterDefaults CalleeMode) const {
1044     if (DX10Clamp != CalleeMode.DX10Clamp)
1045       return false;
1046     if (IEEE != CalleeMode.IEEE)
1047       return false;
1048 
1049     // Allow inlining denormals enabled into denormals flushed functions.
1050     return oneWayCompatible(FP64FP16InputDenormals, CalleeMode.FP64FP16InputDenormals) &&
1051            oneWayCompatible(FP64FP16OutputDenormals, CalleeMode.FP64FP16OutputDenormals) &&
1052            oneWayCompatible(FP32InputDenormals, CalleeMode.FP32InputDenormals) &&
1053            oneWayCompatible(FP32OutputDenormals, CalleeMode.FP32OutputDenormals);
1054   }
1055 };
1056 
1057 } // end namespace AMDGPU
1058 
1059 raw_ostream &operator<<(raw_ostream &OS,
1060                         const AMDGPU::IsaInfo::TargetIDSetting S);
1061 
1062 } // end namespace llvm
1063 
1064 #endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
1065