xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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/IR/InstrTypes.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/Alignment.h"
17 #include <array>
18 #include <functional>
19 #include <utility>
20 
21 struct amd_kernel_code_t;
22 
23 namespace llvm {
24 
25 struct Align;
26 class Argument;
27 class Function;
28 class GlobalValue;
29 class MCInstrInfo;
30 class MCRegisterClass;
31 class MCRegisterInfo;
32 class MCSubtargetInfo;
33 class StringRef;
34 class Triple;
35 class raw_ostream;
36 
37 namespace amdhsa {
38 struct kernel_descriptor_t;
39 }
40 
41 namespace AMDGPU {
42 
43 struct IsaVersion;
44 
45 enum { AMDHSA_COV4 = 4, AMDHSA_COV5 = 5 };
46 
47 /// \returns True if \p STI is AMDHSA.
48 bool isHsaAbi(const MCSubtargetInfo &STI);
49 /// \returns HSA OS ABI Version identification.
50 std::optional<uint8_t> getHsaAbiVersion(const MCSubtargetInfo *STI);
51 /// \returns True if HSA OS ABI Version identification is 4,
52 /// false otherwise.
53 bool isHsaAbiVersion4(const MCSubtargetInfo *STI);
54 /// \returns True if HSA OS ABI Version identification is 5,
55 /// false otherwise.
56 bool isHsaAbiVersion5(const MCSubtargetInfo *STI);
57 
58 /// \returns The offset of the multigrid_sync_arg argument from implicitarg_ptr
59 unsigned getMultigridSyncArgImplicitArgPosition(unsigned COV);
60 
61 /// \returns The offset of the hostcall pointer argument from implicitarg_ptr
62 unsigned getHostcallImplicitArgPosition(unsigned COV);
63 
64 unsigned getDefaultQueueImplicitArgPosition(unsigned COV);
65 unsigned getCompletionActionImplicitArgPosition(unsigned COV);
66 
67 /// \returns Code object version.
68 unsigned getAmdhsaCodeObjectVersion();
69 
70 /// \returns Code object version.
71 unsigned getCodeObjectVersion(const Module &M);
72 
73 struct GcnBufferFormatInfo {
74   unsigned Format;
75   unsigned BitsPerComp;
76   unsigned NumComponents;
77   unsigned NumFormat;
78   unsigned DataFormat;
79 };
80 
81 struct MAIInstInfo {
82   uint16_t Opcode;
83   bool is_dgemm;
84   bool is_gfx940_xdl;
85 };
86 
87 #define GET_MIMGBaseOpcode_DECL
88 #define GET_MIMGDim_DECL
89 #define GET_MIMGEncoding_DECL
90 #define GET_MIMGLZMapping_DECL
91 #define GET_MIMGMIPMapping_DECL
92 #define GET_MIMGBiASMapping_DECL
93 #define GET_MAIInstInfoTable_DECL
94 #include "AMDGPUGenSearchableTables.inc"
95 
96 namespace IsaInfo {
97 
98 enum {
99   // The closed Vulkan driver sets 96, which limits the wave count to 8 but
100   // doesn't spill SGPRs as much as when 80 is set.
101   FIXED_NUM_SGPRS_FOR_INIT_BUG = 96,
102   TRAP_NUM_SGPRS = 16
103 };
104 
105 enum class TargetIDSetting {
106   Unsupported,
107   Any,
108   Off,
109   On
110 };
111 
112 class AMDGPUTargetID {
113 private:
114   const MCSubtargetInfo &STI;
115   TargetIDSetting XnackSetting;
116   TargetIDSetting SramEccSetting;
117   unsigned CodeObjectVersion;
118 
119 public:
120   explicit AMDGPUTargetID(const MCSubtargetInfo &STI);
121   ~AMDGPUTargetID() = default;
122 
123   /// \return True if the current xnack setting is not "Unsupported".
124   bool isXnackSupported() const {
125     return XnackSetting != TargetIDSetting::Unsupported;
126   }
127 
128   /// \returns True if the current xnack setting is "On" or "Any".
129   bool isXnackOnOrAny() const {
130     return XnackSetting == TargetIDSetting::On ||
131         XnackSetting == TargetIDSetting::Any;
132   }
133 
134   /// \returns True if current xnack setting is "On" or "Off",
135   /// false otherwise.
136   bool isXnackOnOrOff() const {
137     return getXnackSetting() == TargetIDSetting::On ||
138         getXnackSetting() == TargetIDSetting::Off;
139   }
140 
141   /// \returns The current xnack TargetIDSetting, possible options are
142   /// "Unsupported", "Any", "Off", and "On".
143   TargetIDSetting getXnackSetting() const {
144     return XnackSetting;
145   }
146 
147   void setCodeObjectVersion(unsigned COV) {
148     CodeObjectVersion = COV;
149   }
150 
151   /// Sets xnack setting to \p NewXnackSetting.
152   void setXnackSetting(TargetIDSetting NewXnackSetting) {
153     XnackSetting = NewXnackSetting;
154   }
155 
156   /// \return True if the current sramecc setting is not "Unsupported".
157   bool isSramEccSupported() const {
158     return SramEccSetting != TargetIDSetting::Unsupported;
159   }
160 
161   /// \returns True if the current sramecc setting is "On" or "Any".
162   bool isSramEccOnOrAny() const {
163   return SramEccSetting == TargetIDSetting::On ||
164       SramEccSetting == TargetIDSetting::Any;
165   }
166 
167   /// \returns True if current sramecc setting is "On" or "Off",
168   /// false otherwise.
169   bool isSramEccOnOrOff() const {
170     return getSramEccSetting() == TargetIDSetting::On ||
171         getSramEccSetting() == TargetIDSetting::Off;
172   }
173 
174   /// \returns The current sramecc TargetIDSetting, possible options are
175   /// "Unsupported", "Any", "Off", and "On".
176   TargetIDSetting getSramEccSetting() const {
177     return SramEccSetting;
178   }
179 
180   /// Sets sramecc setting to \p NewSramEccSetting.
181   void setSramEccSetting(TargetIDSetting NewSramEccSetting) {
182     SramEccSetting = NewSramEccSetting;
183   }
184 
185   void setTargetIDFromFeaturesString(StringRef FS);
186   void setTargetIDFromTargetIDStream(StringRef TargetID);
187 
188   /// \returns String representation of an object.
189   std::string toString() const;
190 };
191 
192 /// \returns Wavefront size for given subtarget \p STI.
193 unsigned getWavefrontSize(const MCSubtargetInfo *STI);
194 
195 /// \returns Local memory size in bytes for given subtarget \p STI.
196 unsigned getLocalMemorySize(const MCSubtargetInfo *STI);
197 
198 /// \returns Maximum addressable local memory size in bytes for given subtarget
199 /// \p STI.
200 unsigned getAddressableLocalMemorySize(const MCSubtargetInfo *STI);
201 
202 /// \returns Number of execution units per compute unit for given subtarget \p
203 /// STI.
204 unsigned getEUsPerCU(const MCSubtargetInfo *STI);
205 
206 /// \returns Maximum number of work groups per compute unit for given subtarget
207 /// \p STI and limited by given \p FlatWorkGroupSize.
208 unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI,
209                                unsigned FlatWorkGroupSize);
210 
211 /// \returns Minimum number of waves per execution unit for given subtarget \p
212 /// STI.
213 unsigned getMinWavesPerEU(const MCSubtargetInfo *STI);
214 
215 /// \returns Maximum number of waves per execution unit for given subtarget \p
216 /// STI without any kind of limitation.
217 unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI);
218 
219 /// \returns Number of waves per execution unit required to support the given \p
220 /// FlatWorkGroupSize.
221 unsigned getWavesPerEUForWorkGroup(const MCSubtargetInfo *STI,
222                                    unsigned FlatWorkGroupSize);
223 
224 /// \returns Minimum flat work group size for given subtarget \p STI.
225 unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI);
226 
227 /// \returns Maximum flat work group size for given subtarget \p STI.
228 unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI);
229 
230 /// \returns Number of waves per work group for given subtarget \p STI and
231 /// \p FlatWorkGroupSize.
232 unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
233                               unsigned FlatWorkGroupSize);
234 
235 /// \returns SGPR allocation granularity for given subtarget \p STI.
236 unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI);
237 
238 /// \returns SGPR encoding granularity for given subtarget \p STI.
239 unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI);
240 
241 /// \returns Total number of SGPRs for given subtarget \p STI.
242 unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI);
243 
244 /// \returns Addressable number of SGPRs for given subtarget \p STI.
245 unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI);
246 
247 /// \returns Minimum number of SGPRs that meets the given number of waves per
248 /// execution unit requirement for given subtarget \p STI.
249 unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
250 
251 /// \returns Maximum number of SGPRs that meets the given number of waves per
252 /// execution unit requirement for given subtarget \p STI.
253 unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
254                         bool Addressable);
255 
256 /// \returns Number of extra SGPRs implicitly required by given subtarget \p
257 /// STI when the given special registers are used.
258 unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
259                           bool FlatScrUsed, bool XNACKUsed);
260 
261 /// \returns Number of extra SGPRs implicitly required by given subtarget \p
262 /// STI when the given special registers are used. XNACK is inferred from
263 /// \p STI.
264 unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
265                           bool FlatScrUsed);
266 
267 /// \returns Number of SGPR blocks needed for given subtarget \p STI when
268 /// \p NumSGPRs are used. \p NumSGPRs should already include any special
269 /// register counts.
270 unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs);
271 
272 /// \returns VGPR allocation granularity for given subtarget \p STI.
273 ///
274 /// For subtargets which support it, \p EnableWavefrontSize32 should match
275 /// the ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
276 unsigned
277 getVGPRAllocGranule(const MCSubtargetInfo *STI,
278                     std::optional<bool> EnableWavefrontSize32 = std::nullopt);
279 
280 /// \returns VGPR encoding granularity for given subtarget \p STI.
281 ///
282 /// For subtargets which support it, \p EnableWavefrontSize32 should match
283 /// the ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
284 unsigned getVGPREncodingGranule(
285     const MCSubtargetInfo *STI,
286     std::optional<bool> EnableWavefrontSize32 = std::nullopt);
287 
288 /// \returns Total number of VGPRs for given subtarget \p STI.
289 unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI);
290 
291 /// \returns Addressable number of VGPRs for given subtarget \p STI.
292 unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI);
293 
294 /// \returns Minimum number of VGPRs that meets given number of waves per
295 /// execution unit requirement for given subtarget \p STI.
296 unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
297 
298 /// \returns Maximum number of VGPRs that meets given number of waves per
299 /// execution unit requirement for given subtarget \p STI.
300 unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU);
301 
302 /// \returns Number of waves reachable for a given \p NumVGPRs usage for given
303 /// subtarget \p STI.
304 unsigned getNumWavesPerEUWithNumVGPRs(const MCSubtargetInfo *STI,
305                                       unsigned NumVGPRs);
306 
307 /// \returns Number of VGPR blocks needed for given subtarget \p STI when
308 /// \p NumVGPRs are used.
309 ///
310 /// For subtargets which support it, \p EnableWavefrontSize32 should match the
311 /// ENABLE_WAVEFRONT_SIZE32 kernel descriptor field.
312 unsigned
313 getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs,
314                  std::optional<bool> EnableWavefrontSize32 = std::nullopt);
315 
316 } // end namespace IsaInfo
317 
318 LLVM_READONLY
319 int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
320 
321 LLVM_READONLY
322 inline bool hasNamedOperand(uint64_t Opcode, uint64_t NamedIdx) {
323   return getNamedOperandIdx(Opcode, NamedIdx) != -1;
324 }
325 
326 LLVM_READONLY
327 int getSOPPWithRelaxation(uint16_t Opcode);
328 
329 struct MIMGBaseOpcodeInfo {
330   MIMGBaseOpcode BaseOpcode;
331   bool Store;
332   bool Atomic;
333   bool AtomicX2;
334   bool Sampler;
335   bool Gather4;
336 
337   uint8_t NumExtraArgs;
338   bool Gradients;
339   bool G16;
340   bool Coordinates;
341   bool LodOrClampOrMip;
342   bool HasD16;
343   bool MSAA;
344   bool BVH;
345   bool A16;
346 };
347 
348 LLVM_READONLY
349 const MIMGBaseOpcodeInfo *getMIMGBaseOpcode(unsigned Opc);
350 
351 LLVM_READONLY
352 const MIMGBaseOpcodeInfo *getMIMGBaseOpcodeInfo(unsigned BaseOpcode);
353 
354 struct MIMGDimInfo {
355   MIMGDim Dim;
356   uint8_t NumCoords;
357   uint8_t NumGradients;
358   bool MSAA;
359   bool DA;
360   uint8_t Encoding;
361   const char *AsmSuffix;
362 };
363 
364 LLVM_READONLY
365 const MIMGDimInfo *getMIMGDimInfo(unsigned DimEnum);
366 
367 LLVM_READONLY
368 const MIMGDimInfo *getMIMGDimInfoByEncoding(uint8_t DimEnc);
369 
370 LLVM_READONLY
371 const MIMGDimInfo *getMIMGDimInfoByAsmSuffix(StringRef AsmSuffix);
372 
373 struct MIMGLZMappingInfo {
374   MIMGBaseOpcode L;
375   MIMGBaseOpcode LZ;
376 };
377 
378 struct MIMGMIPMappingInfo {
379   MIMGBaseOpcode MIP;
380   MIMGBaseOpcode NONMIP;
381 };
382 
383 struct MIMGBiasMappingInfo {
384   MIMGBaseOpcode Bias;
385   MIMGBaseOpcode NoBias;
386 };
387 
388 struct MIMGOffsetMappingInfo {
389   MIMGBaseOpcode Offset;
390   MIMGBaseOpcode NoOffset;
391 };
392 
393 struct MIMGG16MappingInfo {
394   MIMGBaseOpcode G;
395   MIMGBaseOpcode G16;
396 };
397 
398 LLVM_READONLY
399 const MIMGLZMappingInfo *getMIMGLZMappingInfo(unsigned L);
400 
401 struct WMMAOpcodeMappingInfo {
402   unsigned Opcode2Addr;
403   unsigned Opcode3Addr;
404 };
405 
406 LLVM_READONLY
407 const MIMGMIPMappingInfo *getMIMGMIPMappingInfo(unsigned MIP);
408 
409 LLVM_READONLY
410 const MIMGBiasMappingInfo *getMIMGBiasMappingInfo(unsigned Bias);
411 
412 LLVM_READONLY
413 const MIMGOffsetMappingInfo *getMIMGOffsetMappingInfo(unsigned Offset);
414 
415 LLVM_READONLY
416 const MIMGG16MappingInfo *getMIMGG16MappingInfo(unsigned G);
417 
418 LLVM_READONLY
419 int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding,
420                   unsigned VDataDwords, unsigned VAddrDwords);
421 
422 LLVM_READONLY
423 int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels);
424 
425 LLVM_READONLY
426 unsigned getAddrSizeMIMGOp(const MIMGBaseOpcodeInfo *BaseOpcode,
427                            const MIMGDimInfo *Dim, bool IsA16,
428                            bool IsG16Supported);
429 
430 struct MIMGInfo {
431   uint16_t Opcode;
432   uint16_t BaseOpcode;
433   uint8_t MIMGEncoding;
434   uint8_t VDataDwords;
435   uint8_t VAddrDwords;
436   uint8_t VAddrOperands;
437 };
438 
439 LLVM_READONLY
440 const MIMGInfo *getMIMGInfo(unsigned Opc);
441 
442 LLVM_READONLY
443 int getMTBUFBaseOpcode(unsigned Opc);
444 
445 LLVM_READONLY
446 int getMTBUFOpcode(unsigned BaseOpc, unsigned Elements);
447 
448 LLVM_READONLY
449 int getMTBUFElements(unsigned Opc);
450 
451 LLVM_READONLY
452 bool getMTBUFHasVAddr(unsigned Opc);
453 
454 LLVM_READONLY
455 bool getMTBUFHasSrsrc(unsigned Opc);
456 
457 LLVM_READONLY
458 bool getMTBUFHasSoffset(unsigned Opc);
459 
460 LLVM_READONLY
461 int getMUBUFBaseOpcode(unsigned Opc);
462 
463 LLVM_READONLY
464 int getMUBUFOpcode(unsigned BaseOpc, unsigned Elements);
465 
466 LLVM_READONLY
467 int getMUBUFElements(unsigned Opc);
468 
469 LLVM_READONLY
470 bool getMUBUFHasVAddr(unsigned Opc);
471 
472 LLVM_READONLY
473 bool getMUBUFHasSrsrc(unsigned Opc);
474 
475 LLVM_READONLY
476 bool getMUBUFHasSoffset(unsigned Opc);
477 
478 LLVM_READONLY
479 bool getMUBUFIsBufferInv(unsigned Opc);
480 
481 LLVM_READONLY
482 bool getSMEMIsBuffer(unsigned Opc);
483 
484 LLVM_READONLY
485 bool getVOP1IsSingle(unsigned Opc);
486 
487 LLVM_READONLY
488 bool getVOP2IsSingle(unsigned Opc);
489 
490 LLVM_READONLY
491 bool getVOP3IsSingle(unsigned Opc);
492 
493 LLVM_READONLY
494 bool isVOPC64DPP(unsigned Opc);
495 
496 /// Returns true if MAI operation is a double precision GEMM.
497 LLVM_READONLY
498 bool getMAIIsDGEMM(unsigned Opc);
499 
500 LLVM_READONLY
501 bool getMAIIsGFX940XDL(unsigned Opc);
502 
503 struct CanBeVOPD {
504   bool X;
505   bool Y;
506 };
507 
508 /// \returns SIEncodingFamily used for VOPD encoding on a \p ST.
509 LLVM_READONLY
510 unsigned getVOPDEncodingFamily(const MCSubtargetInfo &ST);
511 
512 LLVM_READONLY
513 CanBeVOPD getCanBeVOPD(unsigned Opc);
514 
515 LLVM_READONLY
516 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t BitsPerComp,
517                                                   uint8_t NumComponents,
518                                                   uint8_t NumFormat,
519                                                   const MCSubtargetInfo &STI);
520 LLVM_READONLY
521 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t Format,
522                                                   const MCSubtargetInfo &STI);
523 
524 LLVM_READONLY
525 int getMCOpcode(uint16_t Opcode, unsigned Gen);
526 
527 LLVM_READONLY
528 unsigned getVOPDOpcode(unsigned Opc);
529 
530 LLVM_READONLY
531 int getVOPDFull(unsigned OpX, unsigned OpY, unsigned EncodingFamily);
532 
533 LLVM_READONLY
534 bool isVOPD(unsigned Opc);
535 
536 LLVM_READNONE
537 bool isMAC(unsigned Opc);
538 
539 LLVM_READNONE
540 bool isPermlane16(unsigned Opc);
541 
542 LLVM_READNONE
543 bool isGenericAtomic(unsigned Opc);
544 
545 namespace VOPD {
546 
547 enum Component : unsigned {
548   DST = 0,
549   SRC0,
550   SRC1,
551   SRC2,
552 
553   DST_NUM = 1,
554   MAX_SRC_NUM = 3,
555   MAX_OPR_NUM = DST_NUM + MAX_SRC_NUM
556 };
557 
558 // LSB mask for VGPR banks per VOPD component operand.
559 // 4 banks result in a mask 3, setting 2 lower bits.
560 constexpr unsigned VOPD_VGPR_BANK_MASKS[] = {1, 3, 3, 1};
561 
562 enum ComponentIndex : unsigned { X = 0, Y = 1 };
563 constexpr unsigned COMPONENTS[] = {ComponentIndex::X, ComponentIndex::Y};
564 constexpr unsigned COMPONENTS_NUM = 2;
565 
566 // Properties of VOPD components.
567 class ComponentProps {
568 private:
569   unsigned SrcOperandsNum = 0;
570   unsigned MandatoryLiteralIdx = ~0u;
571   bool HasSrc2Acc = false;
572 
573 public:
574   ComponentProps() = default;
575   ComponentProps(const MCInstrDesc &OpDesc);
576 
577   // Return the total number of src operands this component has.
578   unsigned getCompSrcOperandsNum() const { return SrcOperandsNum; }
579 
580   // Return the number of src operands of this component visible to the parser.
581   unsigned getCompParsedSrcOperandsNum() const {
582     return SrcOperandsNum - HasSrc2Acc;
583   }
584 
585   // Return true iif this component has a mandatory literal.
586   bool hasMandatoryLiteral() const { return MandatoryLiteralIdx != ~0u; }
587 
588   // If this component has a mandatory literal, return component operand
589   // index of this literal (i.e. either Component::SRC1 or Component::SRC2).
590   unsigned getMandatoryLiteralCompOperandIndex() const {
591     assert(hasMandatoryLiteral());
592     return MandatoryLiteralIdx;
593   }
594 
595   // Return true iif this component has operand
596   // with component index CompSrcIdx and this operand may be a register.
597   bool hasRegSrcOperand(unsigned CompSrcIdx) const {
598     assert(CompSrcIdx < Component::MAX_SRC_NUM);
599     return SrcOperandsNum > CompSrcIdx && !hasMandatoryLiteralAt(CompSrcIdx);
600   }
601 
602   // Return true iif this component has tied src2.
603   bool hasSrc2Acc() const { return HasSrc2Acc; }
604 
605 private:
606   bool hasMandatoryLiteralAt(unsigned CompSrcIdx) const {
607     assert(CompSrcIdx < Component::MAX_SRC_NUM);
608     return MandatoryLiteralIdx == Component::DST_NUM + CompSrcIdx;
609   }
610 };
611 
612 enum ComponentKind : unsigned {
613   SINGLE = 0,  // A single VOP1 or VOP2 instruction which may be used in VOPD.
614   COMPONENT_X, // A VOPD instruction, X component.
615   COMPONENT_Y, // A VOPD instruction, Y component.
616   MAX = COMPONENT_Y
617 };
618 
619 // Interface functions of this class map VOPD component operand indices
620 // to indices of operands in MachineInstr/MCInst or parsed operands array.
621 //
622 // Note that this class operates with 3 kinds of indices:
623 // - VOPD component operand indices (Component::DST, Component::SRC0, etc.);
624 // - MC operand indices (they refer operands in a MachineInstr/MCInst);
625 // - parsed operand indices (they refer operands in parsed operands array).
626 //
627 // For SINGLE components mapping between these indices is trivial.
628 // But things get more complicated for COMPONENT_X and
629 // COMPONENT_Y because these components share the same
630 // MachineInstr/MCInst and the same parsed operands array.
631 // Below is an example of component operand to parsed operand
632 // mapping for the following instruction:
633 //
634 //   v_dual_add_f32 v255, v4, v5 :: v_dual_mov_b32 v6, v1
635 //
636 //                          PARSED        COMPONENT         PARSED
637 // COMPONENT               OPERANDS     OPERAND INDEX    OPERAND INDEX
638 // -------------------------------------------------------------------
639 //                     "v_dual_add_f32"                        0
640 // v_dual_add_f32            v255          0 (DST)    -->      1
641 //                           v4            1 (SRC0)   -->      2
642 //                           v5            2 (SRC1)   -->      3
643 //                          "::"                               4
644 //                     "v_dual_mov_b32"                        5
645 // v_dual_mov_b32            v6            0 (DST)    -->      6
646 //                           v1            1 (SRC0)   -->      7
647 // -------------------------------------------------------------------
648 //
649 class ComponentLayout {
650 private:
651   // Regular MachineInstr/MCInst operands are ordered as follows:
652   //   dst, src0 [, other src operands]
653   // VOPD MachineInstr/MCInst operands are ordered as follows:
654   //   dstX, dstY, src0X [, other OpX operands], src0Y [, other OpY operands]
655   // Each ComponentKind has operand indices defined below.
656   static constexpr unsigned MC_DST_IDX[] = {0, 0, 1};
657   static constexpr unsigned FIRST_MC_SRC_IDX[] = {1, 2, 2 /* + OpX.MCSrcNum */};
658 
659   // Parsed operands of regular instructions are ordered as follows:
660   //   Mnemo dst src0 [vsrc1 ...]
661   // Parsed VOPD operands are ordered as follows:
662   //   OpXMnemo dstX src0X [vsrc1X|imm vsrc1X|vsrc1X imm] '::'
663   //   OpYMnemo dstY src0Y [vsrc1Y|imm vsrc1Y|vsrc1Y imm]
664   // Each ComponentKind has operand indices defined below.
665   static constexpr unsigned PARSED_DST_IDX[] = {1, 1,
666                                                 4 /* + OpX.ParsedSrcNum */};
667   static constexpr unsigned FIRST_PARSED_SRC_IDX[] = {
668       2, 2, 5 /* + OpX.ParsedSrcNum */};
669 
670 private:
671   const ComponentKind Kind;
672   const ComponentProps PrevComp;
673 
674 public:
675   // Create layout for COMPONENT_X or SINGLE component.
676   ComponentLayout(ComponentKind Kind) : Kind(Kind) {
677     assert(Kind == ComponentKind::SINGLE || Kind == ComponentKind::COMPONENT_X);
678   }
679 
680   // Create layout for COMPONENT_Y which depends on COMPONENT_X layout.
681   ComponentLayout(const ComponentProps &OpXProps)
682       : Kind(ComponentKind::COMPONENT_Y), PrevComp(OpXProps) {}
683 
684 public:
685   // Return the index of dst operand in MCInst operands.
686   unsigned getIndexOfDstInMCOperands() const { return MC_DST_IDX[Kind]; }
687 
688   // Return the index of the specified src operand in MCInst operands.
689   unsigned getIndexOfSrcInMCOperands(unsigned CompSrcIdx) const {
690     assert(CompSrcIdx < Component::MAX_SRC_NUM);
691     return FIRST_MC_SRC_IDX[Kind] + getPrevCompSrcNum() + CompSrcIdx;
692   }
693 
694   // Return the index of dst operand in the parsed operands array.
695   unsigned getIndexOfDstInParsedOperands() const {
696     return PARSED_DST_IDX[Kind] + getPrevCompParsedSrcNum();
697   }
698 
699   // Return the index of the specified src operand in the parsed operands array.
700   unsigned getIndexOfSrcInParsedOperands(unsigned CompSrcIdx) const {
701     assert(CompSrcIdx < Component::MAX_SRC_NUM);
702     return FIRST_PARSED_SRC_IDX[Kind] + getPrevCompParsedSrcNum() + CompSrcIdx;
703   }
704 
705 private:
706   unsigned getPrevCompSrcNum() const {
707     return PrevComp.getCompSrcOperandsNum();
708   }
709   unsigned getPrevCompParsedSrcNum() const {
710     return PrevComp.getCompParsedSrcOperandsNum();
711   }
712 };
713 
714 // Layout and properties of VOPD components.
715 class ComponentInfo : public ComponentLayout, public ComponentProps {
716 public:
717   // Create ComponentInfo for COMPONENT_X or SINGLE component.
718   ComponentInfo(const MCInstrDesc &OpDesc,
719                 ComponentKind Kind = ComponentKind::SINGLE)
720       : ComponentLayout(Kind), ComponentProps(OpDesc) {}
721 
722   // Create ComponentInfo for COMPONENT_Y which depends on COMPONENT_X layout.
723   ComponentInfo(const MCInstrDesc &OpDesc, const ComponentProps &OpXProps)
724       : ComponentLayout(OpXProps), ComponentProps(OpDesc) {}
725 
726   // Map component operand index to parsed operand index.
727   // Return 0 if the specified operand does not exist.
728   unsigned getIndexInParsedOperands(unsigned CompOprIdx) const;
729 };
730 
731 // Properties of VOPD instructions.
732 class InstInfo {
733 private:
734   const ComponentInfo CompInfo[COMPONENTS_NUM];
735 
736 public:
737   using RegIndices = std::array<unsigned, Component::MAX_OPR_NUM>;
738 
739   InstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY)
740       : CompInfo{OpX, OpY} {}
741 
742   InstInfo(const ComponentInfo &OprInfoX, const ComponentInfo &OprInfoY)
743       : CompInfo{OprInfoX, OprInfoY} {}
744 
745   const ComponentInfo &operator[](size_t ComponentIdx) const {
746     assert(ComponentIdx < COMPONENTS_NUM);
747     return CompInfo[ComponentIdx];
748   }
749 
750   // Check VOPD operands constraints.
751   // GetRegIdx(Component, MCOperandIdx) must return a VGPR register index
752   // for the specified component and MC operand. The callback must return 0
753   // if the operand is not a register or not a VGPR.
754   // If \p SkipSrc is set to true then constraints for source operands are not
755   // checked.
756   bool hasInvalidOperand(std::function<unsigned(unsigned, unsigned)> GetRegIdx,
757                          bool SkipSrc = false) const {
758     return getInvalidCompOperandIndex(GetRegIdx, SkipSrc).has_value();
759   }
760 
761   // Check VOPD operands constraints.
762   // Return the index of an invalid component operand, if any.
763   // If \p SkipSrc is set to true then constraints for source operands are not
764   // checked.
765   std::optional<unsigned> getInvalidCompOperandIndex(
766       std::function<unsigned(unsigned, unsigned)> GetRegIdx,
767       bool SkipSrc = false) const;
768 
769 private:
770   RegIndices
771   getRegIndices(unsigned ComponentIdx,
772                 std::function<unsigned(unsigned, unsigned)> GetRegIdx) const;
773 };
774 
775 } // namespace VOPD
776 
777 LLVM_READONLY
778 std::pair<unsigned, unsigned> getVOPDComponents(unsigned VOPDOpcode);
779 
780 LLVM_READONLY
781 // Get properties of 2 single VOP1/VOP2 instructions
782 // used as components to create a VOPD instruction.
783 VOPD::InstInfo getVOPDInstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY);
784 
785 LLVM_READONLY
786 // Get properties of VOPD X and Y components.
787 VOPD::InstInfo
788 getVOPDInstInfo(unsigned VOPDOpcode, const MCInstrInfo *InstrInfo);
789 
790 LLVM_READONLY
791 bool isTrue16Inst(unsigned Opc);
792 
793 LLVM_READONLY
794 unsigned mapWMMA2AddrTo3AddrOpcode(unsigned Opc);
795 
796 LLVM_READONLY
797 unsigned mapWMMA3AddrTo2AddrOpcode(unsigned Opc);
798 
799 void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
800                                const MCSubtargetInfo *STI);
801 
802 amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor(
803     const MCSubtargetInfo *STI);
804 
805 bool isGroupSegment(const GlobalValue *GV);
806 bool isGlobalSegment(const GlobalValue *GV);
807 bool isReadOnlySegment(const GlobalValue *GV);
808 
809 /// \returns True if constants should be emitted to .text section for given
810 /// target triple \p TT, false otherwise.
811 bool shouldEmitConstantsToTextSection(const Triple &TT);
812 
813 /// \returns Integer value requested using \p F's \p Name attribute.
814 ///
815 /// \returns \p Default if attribute is not present.
816 ///
817 /// \returns \p Default and emits error if requested value cannot be converted
818 /// to integer.
819 int getIntegerAttribute(const Function &F, StringRef Name, int Default);
820 
821 /// \returns A pair of integer values requested using \p F's \p Name attribute
822 /// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
823 /// is false).
824 ///
825 /// \returns \p Default if attribute is not present.
826 ///
827 /// \returns \p Default and emits error if one of the requested values cannot be
828 /// converted to integer, or \p OnlyFirstRequired is false and "second" value is
829 /// not present.
830 std::pair<unsigned, unsigned>
831 getIntegerPairAttribute(const Function &F, StringRef Name,
832                         std::pair<unsigned, unsigned> Default,
833                         bool OnlyFirstRequired = false);
834 
835 /// Represents the counter values to wait for in an s_waitcnt instruction.
836 ///
837 /// Large values (including the maximum possible integer) can be used to
838 /// represent "don't care" waits.
839 struct Waitcnt {
840   unsigned VmCnt = ~0u;
841   unsigned ExpCnt = ~0u;
842   unsigned LgkmCnt = ~0u;
843   unsigned VsCnt = ~0u;
844 
845   Waitcnt() = default;
846   Waitcnt(unsigned VmCnt, unsigned ExpCnt, unsigned LgkmCnt, unsigned VsCnt)
847       : VmCnt(VmCnt), ExpCnt(ExpCnt), LgkmCnt(LgkmCnt), VsCnt(VsCnt) {}
848 
849   static Waitcnt allZero(bool HasVscnt) {
850     return Waitcnt(0, 0, 0, HasVscnt ? 0 : ~0u);
851   }
852   static Waitcnt allZeroExceptVsCnt() { return Waitcnt(0, 0, 0, ~0u); }
853 
854   bool hasWait() const {
855     return VmCnt != ~0u || ExpCnt != ~0u || LgkmCnt != ~0u || VsCnt != ~0u;
856   }
857 
858   bool hasWaitExceptVsCnt() const {
859     return VmCnt != ~0u || ExpCnt != ~0u || LgkmCnt != ~0u;
860   }
861 
862   bool hasWaitVsCnt() const {
863     return VsCnt != ~0u;
864   }
865 
866   Waitcnt combined(const Waitcnt &Other) const {
867     return Waitcnt(std::min(VmCnt, Other.VmCnt), std::min(ExpCnt, Other.ExpCnt),
868                    std::min(LgkmCnt, Other.LgkmCnt),
869                    std::min(VsCnt, Other.VsCnt));
870   }
871 };
872 
873 /// \returns Vmcnt bit mask for given isa \p Version.
874 unsigned getVmcntBitMask(const IsaVersion &Version);
875 
876 /// \returns Expcnt bit mask for given isa \p Version.
877 unsigned getExpcntBitMask(const IsaVersion &Version);
878 
879 /// \returns Lgkmcnt bit mask for given isa \p Version.
880 unsigned getLgkmcntBitMask(const IsaVersion &Version);
881 
882 /// \returns Waitcnt bit mask for given isa \p Version.
883 unsigned getWaitcntBitMask(const IsaVersion &Version);
884 
885 /// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
886 unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt);
887 
888 /// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
889 unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt);
890 
891 /// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
892 unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt);
893 
894 /// Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
895 /// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
896 /// \p Lgkmcnt respectively.
897 ///
898 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
899 ///     \p Vmcnt = \p Waitcnt[3:0]        (pre-gfx9)
900 ///     \p Vmcnt = \p Waitcnt[15:14,3:0]  (gfx9,10)
901 ///     \p Vmcnt = \p Waitcnt[15:10]      (gfx11+)
902 ///     \p Expcnt = \p Waitcnt[6:4]       (pre-gfx11)
903 ///     \p Expcnt = \p Waitcnt[2:0]       (gfx11+)
904 ///     \p Lgkmcnt = \p Waitcnt[11:8]     (pre-gfx10)
905 ///     \p Lgkmcnt = \p Waitcnt[13:8]     (gfx10)
906 ///     \p Lgkmcnt = \p Waitcnt[9:4]      (gfx11+)
907 void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
908                    unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
909 
910 Waitcnt decodeWaitcnt(const IsaVersion &Version, unsigned Encoded);
911 
912 /// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
913 unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
914                      unsigned Vmcnt);
915 
916 /// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
917 unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
918                       unsigned Expcnt);
919 
920 /// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
921 unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
922                        unsigned Lgkmcnt);
923 
924 /// Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
925 /// \p Version.
926 ///
927 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
928 ///     Waitcnt[2:0]   = \p Expcnt      (gfx11+)
929 ///     Waitcnt[3:0]   = \p Vmcnt       (pre-gfx9)
930 ///     Waitcnt[3:0]   = \p Vmcnt[3:0]  (gfx9,10)
931 ///     Waitcnt[6:4]   = \p Expcnt      (pre-gfx11)
932 ///     Waitcnt[9:4]   = \p Lgkmcnt     (gfx11+)
933 ///     Waitcnt[11:8]  = \p Lgkmcnt     (pre-gfx10)
934 ///     Waitcnt[13:8]  = \p Lgkmcnt     (gfx10)
935 ///     Waitcnt[15:10] = \p Vmcnt       (gfx11+)
936 ///     Waitcnt[15:14] = \p Vmcnt[5:4]  (gfx9,10)
937 ///
938 /// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
939 /// isa \p Version.
940 unsigned encodeWaitcnt(const IsaVersion &Version,
941                        unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
942 
943 unsigned encodeWaitcnt(const IsaVersion &Version, const Waitcnt &Decoded);
944 
945 namespace Hwreg {
946 
947 LLVM_READONLY
948 int64_t getHwregId(const StringRef Name, const MCSubtargetInfo &STI);
949 
950 LLVM_READNONE
951 bool isValidHwreg(int64_t Id);
952 
953 LLVM_READNONE
954 bool isValidHwregOffset(int64_t Offset);
955 
956 LLVM_READNONE
957 bool isValidHwregWidth(int64_t Width);
958 
959 LLVM_READNONE
960 uint64_t encodeHwreg(uint64_t Id, uint64_t Offset, uint64_t Width);
961 
962 LLVM_READNONE
963 StringRef getHwreg(unsigned Id, const MCSubtargetInfo &STI);
964 
965 void decodeHwreg(unsigned Val, unsigned &Id, unsigned &Offset, unsigned &Width);
966 
967 } // namespace Hwreg
968 
969 namespace DepCtr {
970 
971 int getDefaultDepCtrEncoding(const MCSubtargetInfo &STI);
972 int encodeDepCtr(const StringRef Name, int64_t Val, unsigned &UsedOprMask,
973                  const MCSubtargetInfo &STI);
974 bool isSymbolicDepCtrEncoding(unsigned Code, bool &HasNonDefaultVal,
975                               const MCSubtargetInfo &STI);
976 bool decodeDepCtr(unsigned Code, int &Id, StringRef &Name, unsigned &Val,
977                   bool &IsDefault, const MCSubtargetInfo &STI);
978 
979 /// \returns Decoded VaVdst from given immediate \p Encoded.
980 unsigned decodeFieldVaVdst(unsigned Encoded);
981 
982 /// \returns Decoded VmVsrc from given immediate \p Encoded.
983 unsigned decodeFieldVmVsrc(unsigned Encoded);
984 
985 /// \returns Decoded SaSdst from given immediate \p Encoded.
986 unsigned decodeFieldSaSdst(unsigned Encoded);
987 
988 /// \returns \p VmVsrc as an encoded Depctr immediate.
989 unsigned encodeFieldVmVsrc(unsigned VmVsrc);
990 
991 /// \returns \p Encoded combined with encoded \p VmVsrc.
992 unsigned encodeFieldVmVsrc(unsigned Encoded, unsigned VmVsrc);
993 
994 /// \returns \p VaVdst as an encoded Depctr immediate.
995 unsigned encodeFieldVaVdst(unsigned VaVdst);
996 
997 /// \returns \p Encoded combined with encoded \p VaVdst.
998 unsigned encodeFieldVaVdst(unsigned Encoded, unsigned VaVdst);
999 
1000 /// \returns \p SaSdst as an encoded Depctr immediate.
1001 unsigned encodeFieldSaSdst(unsigned SaSdst);
1002 
1003 /// \returns \p Encoded combined with encoded \p SaSdst.
1004 unsigned encodeFieldSaSdst(unsigned Encoded, unsigned SaSdst);
1005 
1006 } // namespace DepCtr
1007 
1008 namespace Exp {
1009 
1010 bool getTgtName(unsigned Id, StringRef &Name, int &Index);
1011 
1012 LLVM_READONLY
1013 unsigned getTgtId(const StringRef Name);
1014 
1015 LLVM_READNONE
1016 bool isSupportedTgtId(unsigned Id, const MCSubtargetInfo &STI);
1017 
1018 } // namespace Exp
1019 
1020 namespace MTBUFFormat {
1021 
1022 LLVM_READNONE
1023 int64_t encodeDfmtNfmt(unsigned Dfmt, unsigned Nfmt);
1024 
1025 void decodeDfmtNfmt(unsigned Format, unsigned &Dfmt, unsigned &Nfmt);
1026 
1027 int64_t getDfmt(const StringRef Name);
1028 
1029 StringRef getDfmtName(unsigned Id);
1030 
1031 int64_t getNfmt(const StringRef Name, const MCSubtargetInfo &STI);
1032 
1033 StringRef getNfmtName(unsigned Id, const MCSubtargetInfo &STI);
1034 
1035 bool isValidDfmtNfmt(unsigned Val, const MCSubtargetInfo &STI);
1036 
1037 bool isValidNfmt(unsigned Val, const MCSubtargetInfo &STI);
1038 
1039 int64_t getUnifiedFormat(const StringRef Name, const MCSubtargetInfo &STI);
1040 
1041 StringRef getUnifiedFormatName(unsigned Id, const MCSubtargetInfo &STI);
1042 
1043 bool isValidUnifiedFormat(unsigned Val, const MCSubtargetInfo &STI);
1044 
1045 int64_t convertDfmtNfmt2Ufmt(unsigned Dfmt, unsigned Nfmt,
1046                              const MCSubtargetInfo &STI);
1047 
1048 bool isValidFormatEncoding(unsigned Val, const MCSubtargetInfo &STI);
1049 
1050 unsigned getDefaultFormatEncoding(const MCSubtargetInfo &STI);
1051 
1052 } // namespace MTBUFFormat
1053 
1054 namespace SendMsg {
1055 
1056 LLVM_READONLY
1057 int64_t getMsgId(const StringRef Name, const MCSubtargetInfo &STI);
1058 
1059 LLVM_READONLY
1060 int64_t getMsgOpId(int64_t MsgId, const StringRef Name);
1061 
1062 LLVM_READNONE
1063 StringRef getMsgName(int64_t MsgId, const MCSubtargetInfo &STI);
1064 
1065 LLVM_READNONE
1066 StringRef getMsgOpName(int64_t MsgId, int64_t OpId, const MCSubtargetInfo &STI);
1067 
1068 LLVM_READNONE
1069 bool isValidMsgId(int64_t MsgId, const MCSubtargetInfo &STI);
1070 
1071 LLVM_READNONE
1072 bool isValidMsgOp(int64_t MsgId, int64_t OpId, const MCSubtargetInfo &STI,
1073                   bool Strict = true);
1074 
1075 LLVM_READNONE
1076 bool isValidMsgStream(int64_t MsgId, int64_t OpId, int64_t StreamId,
1077                       const MCSubtargetInfo &STI, bool Strict = true);
1078 
1079 LLVM_READNONE
1080 bool msgRequiresOp(int64_t MsgId, const MCSubtargetInfo &STI);
1081 
1082 LLVM_READNONE
1083 bool msgSupportsStream(int64_t MsgId, int64_t OpId, const MCSubtargetInfo &STI);
1084 
1085 void decodeMsg(unsigned Val, uint16_t &MsgId, uint16_t &OpId,
1086                uint16_t &StreamId, const MCSubtargetInfo &STI);
1087 
1088 LLVM_READNONE
1089 uint64_t encodeMsg(uint64_t MsgId,
1090                    uint64_t OpId,
1091                    uint64_t StreamId);
1092 
1093 } // namespace SendMsg
1094 
1095 
1096 unsigned getInitialPSInputAddr(const Function &F);
1097 
1098 bool getHasColorExport(const Function &F);
1099 
1100 bool getHasDepthExport(const Function &F);
1101 
1102 LLVM_READNONE
1103 bool isShader(CallingConv::ID CC);
1104 
1105 LLVM_READNONE
1106 bool isGraphics(CallingConv::ID CC);
1107 
1108 LLVM_READNONE
1109 bool isCompute(CallingConv::ID CC);
1110 
1111 LLVM_READNONE
1112 bool isEntryFunctionCC(CallingConv::ID CC);
1113 
1114 // These functions are considered entrypoints into the current module, i.e. they
1115 // are allowed to be called from outside the current module. This is different
1116 // from isEntryFunctionCC, which is only true for functions that are entered by
1117 // the hardware. Module entry points include all entry functions but also
1118 // include functions that can be called from other functions inside or outside
1119 // the current module. Module entry functions are allowed to allocate LDS.
1120 LLVM_READNONE
1121 bool isModuleEntryFunctionCC(CallingConv::ID CC);
1122 
1123 LLVM_READNONE
1124 bool isChainCC(CallingConv::ID CC);
1125 
1126 bool isKernelCC(const Function *Func);
1127 
1128 // FIXME: Remove this when calling conventions cleaned up
1129 LLVM_READNONE
1130 inline bool isKernel(CallingConv::ID CC) {
1131   switch (CC) {
1132   case CallingConv::AMDGPU_KERNEL:
1133   case CallingConv::SPIR_KERNEL:
1134     return true;
1135   default:
1136     return false;
1137   }
1138 }
1139 
1140 bool hasXNACK(const MCSubtargetInfo &STI);
1141 bool hasSRAMECC(const MCSubtargetInfo &STI);
1142 bool hasMIMG_R128(const MCSubtargetInfo &STI);
1143 bool hasA16(const MCSubtargetInfo &STI);
1144 bool hasG16(const MCSubtargetInfo &STI);
1145 bool hasPackedD16(const MCSubtargetInfo &STI);
1146 bool hasGDS(const MCSubtargetInfo &STI);
1147 unsigned getNSAMaxSize(const MCSubtargetInfo &STI, bool HasSampler = false);
1148 unsigned getMaxNumUserSGPRs(const MCSubtargetInfo &STI);
1149 
1150 bool isSI(const MCSubtargetInfo &STI);
1151 bool isCI(const MCSubtargetInfo &STI);
1152 bool isVI(const MCSubtargetInfo &STI);
1153 bool isGFX9(const MCSubtargetInfo &STI);
1154 bool isGFX9_GFX10(const MCSubtargetInfo &STI);
1155 bool isGFX9_GFX10_GFX11(const MCSubtargetInfo &STI);
1156 bool isGFX8_GFX9_GFX10(const MCSubtargetInfo &STI);
1157 bool isGFX8Plus(const MCSubtargetInfo &STI);
1158 bool isGFX9Plus(const MCSubtargetInfo &STI);
1159 bool isGFX10(const MCSubtargetInfo &STI);
1160 bool isGFX10_GFX11(const MCSubtargetInfo &STI);
1161 bool isGFX10Plus(const MCSubtargetInfo &STI);
1162 bool isNotGFX10Plus(const MCSubtargetInfo &STI);
1163 bool isGFX10Before1030(const MCSubtargetInfo &STI);
1164 bool isGFX11(const MCSubtargetInfo &STI);
1165 bool isGFX11Plus(const MCSubtargetInfo &STI);
1166 bool isGFX12(const MCSubtargetInfo &STI);
1167 bool isGFX12Plus(const MCSubtargetInfo &STI);
1168 bool isNotGFX12Plus(const MCSubtargetInfo &STI);
1169 bool isNotGFX11Plus(const MCSubtargetInfo &STI);
1170 bool isGCN3Encoding(const MCSubtargetInfo &STI);
1171 bool isGFX10_AEncoding(const MCSubtargetInfo &STI);
1172 bool isGFX10_BEncoding(const MCSubtargetInfo &STI);
1173 bool hasGFX10_3Insts(const MCSubtargetInfo &STI);
1174 bool isGFX10_3_GFX11(const MCSubtargetInfo &STI);
1175 bool isGFX90A(const MCSubtargetInfo &STI);
1176 bool isGFX940(const MCSubtargetInfo &STI);
1177 bool hasArchitectedFlatScratch(const MCSubtargetInfo &STI);
1178 bool hasMAIInsts(const MCSubtargetInfo &STI);
1179 bool hasVOPD(const MCSubtargetInfo &STI);
1180 bool hasDPPSrc1SGPR(const MCSubtargetInfo &STI);
1181 int getTotalNumVGPRs(bool has90AInsts, int32_t ArgNumAGPR, int32_t ArgNumVGPR);
1182 unsigned hasKernargPreload(const MCSubtargetInfo &STI);
1183 
1184 /// Is Reg - scalar register
1185 bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI);
1186 
1187 /// \returns if \p Reg occupies the high 16-bits of a 32-bit register.
1188 /// The bit indicating isHi is the LSB of the encoding.
1189 bool isHi(unsigned Reg, const MCRegisterInfo &MRI);
1190 
1191 /// If \p Reg is a pseudo reg, return the correct hardware register given
1192 /// \p STI otherwise return \p Reg.
1193 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
1194 
1195 /// Convert hardware register \p Reg to a pseudo register
1196 LLVM_READNONE
1197 unsigned mc2PseudoReg(unsigned Reg);
1198 
1199 LLVM_READNONE
1200 bool isInlineValue(unsigned Reg);
1201 
1202 /// Is this an AMDGPU specific source operand? These include registers,
1203 /// inline constants, literals and mandatory literals (KImm).
1204 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
1205 
1206 /// Is this a KImm operand?
1207 bool isKImmOperand(const MCInstrDesc &Desc, unsigned OpNo);
1208 
1209 /// Is this floating-point operand?
1210 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
1211 
1212 /// Does this operand support only inlinable literals?
1213 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
1214 
1215 /// Get the size in bits of a register from the register class \p RC.
1216 unsigned getRegBitWidth(unsigned RCID);
1217 
1218 /// Get the size in bits of a register from the register class \p RC.
1219 unsigned getRegBitWidth(const MCRegisterClass &RC);
1220 
1221 /// Get size of register operand
1222 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
1223                            unsigned OpNo);
1224 
1225 LLVM_READNONE
1226 inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
1227   switch (OpInfo.OperandType) {
1228   case AMDGPU::OPERAND_REG_IMM_INT32:
1229   case AMDGPU::OPERAND_REG_IMM_FP32:
1230   case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED:
1231   case AMDGPU::OPERAND_REG_INLINE_C_INT32:
1232   case AMDGPU::OPERAND_REG_INLINE_C_FP32:
1233   case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
1234   case AMDGPU::OPERAND_REG_INLINE_AC_FP32:
1235   case AMDGPU::OPERAND_REG_IMM_V2INT32:
1236   case AMDGPU::OPERAND_REG_IMM_V2FP32:
1237   case AMDGPU::OPERAND_REG_INLINE_C_V2INT32:
1238   case AMDGPU::OPERAND_REG_INLINE_C_V2FP32:
1239   case AMDGPU::OPERAND_KIMM32:
1240   case AMDGPU::OPERAND_KIMM16: // mandatory literal is always size 4
1241   case AMDGPU::OPERAND_INLINE_SPLIT_BARRIER_INT32:
1242     return 4;
1243 
1244   case AMDGPU::OPERAND_REG_IMM_INT64:
1245   case AMDGPU::OPERAND_REG_IMM_FP64:
1246   case AMDGPU::OPERAND_REG_INLINE_C_INT64:
1247   case AMDGPU::OPERAND_REG_INLINE_C_FP64:
1248   case AMDGPU::OPERAND_REG_INLINE_AC_FP64:
1249     return 8;
1250 
1251   case AMDGPU::OPERAND_REG_IMM_INT16:
1252   case AMDGPU::OPERAND_REG_IMM_FP16:
1253   case AMDGPU::OPERAND_REG_IMM_FP16_DEFERRED:
1254   case AMDGPU::OPERAND_REG_INLINE_C_INT16:
1255   case AMDGPU::OPERAND_REG_INLINE_C_FP16:
1256   case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
1257   case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
1258   case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
1259   case AMDGPU::OPERAND_REG_INLINE_AC_FP16:
1260   case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16:
1261   case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16:
1262   case AMDGPU::OPERAND_REG_IMM_V2INT16:
1263   case AMDGPU::OPERAND_REG_IMM_V2FP16:
1264     return 2;
1265 
1266   default:
1267     llvm_unreachable("unhandled operand type");
1268   }
1269 }
1270 
1271 LLVM_READNONE
1272 inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
1273   return getOperandSize(Desc.operands()[OpNo]);
1274 }
1275 
1276 /// Is this literal inlinable, and not one of the values intended for floating
1277 /// point values.
1278 LLVM_READNONE
1279 inline bool isInlinableIntLiteral(int64_t Literal) {
1280   return Literal >= -16 && Literal <= 64;
1281 }
1282 
1283 /// Is this literal inlinable
1284 LLVM_READNONE
1285 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
1286 
1287 LLVM_READNONE
1288 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
1289 
1290 LLVM_READNONE
1291 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
1292 
1293 LLVM_READNONE
1294 std::optional<unsigned> getInlineEncodingV2I16(uint32_t Literal);
1295 
1296 LLVM_READNONE
1297 std::optional<unsigned> getInlineEncodingV2F16(uint32_t Literal);
1298 
1299 LLVM_READNONE
1300 bool isInlinableLiteralV216(uint32_t Literal, uint8_t OpType);
1301 
1302 LLVM_READNONE
1303 bool isInlinableLiteralV2I16(uint32_t Literal);
1304 
1305 LLVM_READNONE
1306 bool isInlinableLiteralV2F16(uint32_t Literal);
1307 
1308 LLVM_READNONE
1309 bool isValid32BitLiteral(uint64_t Val, bool IsFP64);
1310 
1311 bool isArgPassedInSGPR(const Argument *Arg);
1312 
1313 bool isArgPassedInSGPR(const CallBase *CB, unsigned ArgNo);
1314 
1315 LLVM_READONLY
1316 bool isLegalSMRDEncodedUnsignedOffset(const MCSubtargetInfo &ST,
1317                                       int64_t EncodedOffset);
1318 
1319 LLVM_READONLY
1320 bool isLegalSMRDEncodedSignedOffset(const MCSubtargetInfo &ST,
1321                                     int64_t EncodedOffset,
1322                                     bool IsBuffer);
1323 
1324 /// Convert \p ByteOffset to dwords if the subtarget uses dword SMRD immediate
1325 /// offsets.
1326 uint64_t convertSMRDOffsetUnits(const MCSubtargetInfo &ST, uint64_t ByteOffset);
1327 
1328 /// \returns The encoding that will be used for \p ByteOffset in the
1329 /// SMRD offset field, or std::nullopt if it won't fit. On GFX9 and GFX10
1330 /// S_LOAD instructions have a signed offset, on other subtargets it is
1331 /// unsigned. S_BUFFER has an unsigned offset for all subtargets.
1332 std::optional<int64_t> getSMRDEncodedOffset(const MCSubtargetInfo &ST,
1333                                             int64_t ByteOffset, bool IsBuffer);
1334 
1335 /// \return The encoding that can be used for a 32-bit literal offset in an SMRD
1336 /// instruction. This is only useful on CI.s
1337 std::optional<int64_t> getSMRDEncodedLiteralOffset32(const MCSubtargetInfo &ST,
1338                                                      int64_t ByteOffset);
1339 
1340 /// For pre-GFX12 FLAT instructions the offset must be positive;
1341 /// MSB is ignored and forced to zero.
1342 ///
1343 /// \return The number of bits available for the signed offset field in flat
1344 /// instructions. Note that some forms of the instruction disallow negative
1345 /// offsets.
1346 unsigned getNumFlatOffsetBits(const MCSubtargetInfo &ST);
1347 
1348 /// \returns true if this offset is small enough to fit in the SMRD
1349 /// offset field.  \p ByteOffset should be the offset in bytes and
1350 /// not the encoded offset.
1351 bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
1352 
1353 LLVM_READNONE
1354 inline bool isLegalDPALU_DPPControl(unsigned DC) {
1355   return DC >= DPP::ROW_NEWBCAST_FIRST && DC <= DPP::ROW_NEWBCAST_LAST;
1356 }
1357 
1358 /// \returns true if an instruction may have a 64-bit VGPR operand.
1359 bool hasAny64BitVGPROperands(const MCInstrDesc &OpDesc);
1360 
1361 /// \returns true if an instruction is a DP ALU DPP.
1362 bool isDPALU_DPP(const MCInstrDesc &OpDesc);
1363 
1364 /// \returns true if the intrinsic is divergent
1365 bool isIntrinsicSourceOfDivergence(unsigned IntrID);
1366 
1367 /// \returns true if the intrinsic is uniform
1368 bool isIntrinsicAlwaysUniform(unsigned IntrID);
1369 
1370 } // end namespace AMDGPU
1371 
1372 raw_ostream &operator<<(raw_ostream &OS,
1373                         const AMDGPU::IsaInfo::TargetIDSetting S);
1374 
1375 } // end namespace llvm
1376 
1377 #endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
1378