AMDGPUMetadata.h revision 360784
1//===--- AMDGPUMetadata.h ---------------------------------------*- 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/// \file
10/// AMDGPU metadata definitions and in-memory representations.
11///
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_AMDGPUMETADATA_H
16#define LLVM_SUPPORT_AMDGPUMETADATA_H
17
18#include <cstdint>
19#include <string>
20#include <system_error>
21#include <vector>
22
23namespace llvm {
24namespace AMDGPU {
25
26//===----------------------------------------------------------------------===//
27// HSA metadata.
28//===----------------------------------------------------------------------===//
29namespace HSAMD {
30
31/// HSA metadata major version.
32constexpr uint32_t VersionMajor = 1;
33/// HSA metadata minor version.
34constexpr uint32_t VersionMinor = 0;
35
36/// HSA metadata beginning assembler directive.
37constexpr char AssemblerDirectiveBegin[] = ".amd_amdgpu_hsa_metadata";
38/// HSA metadata ending assembler directive.
39constexpr char AssemblerDirectiveEnd[] = ".end_amd_amdgpu_hsa_metadata";
40
41/// Access qualifiers.
42enum class AccessQualifier : uint8_t {
43  Default   = 0,
44  ReadOnly  = 1,
45  WriteOnly = 2,
46  ReadWrite = 3,
47  Unknown   = 0xff
48};
49
50/// Address space qualifiers.
51enum class AddressSpaceQualifier : uint8_t {
52  Private  = 0,
53  Global   = 1,
54  Constant = 2,
55  Local    = 3,
56  Generic  = 4,
57  Region   = 5,
58  Unknown  = 0xff
59};
60
61/// Value kinds.
62enum class ValueKind : uint8_t {
63  ByValue                = 0,
64  GlobalBuffer           = 1,
65  DynamicSharedPointer   = 2,
66  Sampler                = 3,
67  Image                  = 4,
68  Pipe                   = 5,
69  Queue                  = 6,
70  HiddenGlobalOffsetX    = 7,
71  HiddenGlobalOffsetY    = 8,
72  HiddenGlobalOffsetZ    = 9,
73  HiddenNone             = 10,
74  HiddenPrintfBuffer     = 11,
75  HiddenDefaultQueue     = 12,
76  HiddenCompletionAction = 13,
77  HiddenMultiGridSyncArg = 14,
78  HiddenHostcallBuffer   = 15,
79  Unknown                = 0xff
80};
81
82/// Value types.
83enum class ValueType : uint8_t {
84  Struct  = 0,
85  I8      = 1,
86  U8      = 2,
87  I16     = 3,
88  U16     = 4,
89  F16     = 5,
90  I32     = 6,
91  U32     = 7,
92  F32     = 8,
93  I64     = 9,
94  U64     = 10,
95  F64     = 11,
96  Unknown = 0xff
97};
98
99//===----------------------------------------------------------------------===//
100// Kernel Metadata.
101//===----------------------------------------------------------------------===//
102namespace Kernel {
103
104//===----------------------------------------------------------------------===//
105// Kernel Attributes Metadata.
106//===----------------------------------------------------------------------===//
107namespace Attrs {
108
109namespace Key {
110/// Key for Kernel::Attr::Metadata::mReqdWorkGroupSize.
111constexpr char ReqdWorkGroupSize[] = "ReqdWorkGroupSize";
112/// Key for Kernel::Attr::Metadata::mWorkGroupSizeHint.
113constexpr char WorkGroupSizeHint[] = "WorkGroupSizeHint";
114/// Key for Kernel::Attr::Metadata::mVecTypeHint.
115constexpr char VecTypeHint[] = "VecTypeHint";
116/// Key for Kernel::Attr::Metadata::mRuntimeHandle.
117constexpr char RuntimeHandle[] = "RuntimeHandle";
118} // end namespace Key
119
120/// In-memory representation of kernel attributes metadata.
121struct Metadata final {
122  /// 'reqd_work_group_size' attribute. Optional.
123  std::vector<uint32_t> mReqdWorkGroupSize = std::vector<uint32_t>();
124  /// 'work_group_size_hint' attribute. Optional.
125  std::vector<uint32_t> mWorkGroupSizeHint = std::vector<uint32_t>();
126  /// 'vec_type_hint' attribute. Optional.
127  std::string mVecTypeHint = std::string();
128  /// External symbol created by runtime to store the kernel address
129  /// for enqueued blocks.
130  std::string mRuntimeHandle = std::string();
131
132  /// Default constructor.
133  Metadata() = default;
134
135  /// \returns True if kernel attributes metadata is empty, false otherwise.
136  bool empty() const {
137    return !notEmpty();
138  }
139
140  /// \returns True if kernel attributes metadata is not empty, false otherwise.
141  bool notEmpty() const {
142    return !mReqdWorkGroupSize.empty() || !mWorkGroupSizeHint.empty() ||
143           !mVecTypeHint.empty() || !mRuntimeHandle.empty();
144  }
145};
146
147} // end namespace Attrs
148
149//===----------------------------------------------------------------------===//
150// Kernel Argument Metadata.
151//===----------------------------------------------------------------------===//
152namespace Arg {
153
154namespace Key {
155/// Key for Kernel::Arg::Metadata::mName.
156constexpr char Name[] = "Name";
157/// Key for Kernel::Arg::Metadata::mTypeName.
158constexpr char TypeName[] = "TypeName";
159/// Key for Kernel::Arg::Metadata::mSize.
160constexpr char Size[] = "Size";
161/// Key for Kernel::Arg::Metadata::mOffset.
162constexpr char Offset[] = "Offset";
163/// Key for Kernel::Arg::Metadata::mAlign.
164constexpr char Align[] = "Align";
165/// Key for Kernel::Arg::Metadata::mValueKind.
166constexpr char ValueKind[] = "ValueKind";
167/// Key for Kernel::Arg::Metadata::mValueType.
168constexpr char ValueType[] = "ValueType";
169/// Key for Kernel::Arg::Metadata::mPointeeAlign.
170constexpr char PointeeAlign[] = "PointeeAlign";
171/// Key for Kernel::Arg::Metadata::mAddrSpaceQual.
172constexpr char AddrSpaceQual[] = "AddrSpaceQual";
173/// Key for Kernel::Arg::Metadata::mAccQual.
174constexpr char AccQual[] = "AccQual";
175/// Key for Kernel::Arg::Metadata::mActualAccQual.
176constexpr char ActualAccQual[] = "ActualAccQual";
177/// Key for Kernel::Arg::Metadata::mIsConst.
178constexpr char IsConst[] = "IsConst";
179/// Key for Kernel::Arg::Metadata::mIsRestrict.
180constexpr char IsRestrict[] = "IsRestrict";
181/// Key for Kernel::Arg::Metadata::mIsVolatile.
182constexpr char IsVolatile[] = "IsVolatile";
183/// Key for Kernel::Arg::Metadata::mIsPipe.
184constexpr char IsPipe[] = "IsPipe";
185} // end namespace Key
186
187/// In-memory representation of kernel argument metadata.
188struct Metadata final {
189  /// Name. Optional.
190  std::string mName = std::string();
191  /// Type name. Optional.
192  std::string mTypeName = std::string();
193  /// Size in bytes. Required.
194  uint32_t mSize = 0;
195  /// Offset in bytes. Required for code object v3, unused for code object v2.
196  uint32_t mOffset = 0;
197  /// Alignment in bytes. Required.
198  uint32_t mAlign = 0;
199  /// Value kind. Required.
200  ValueKind mValueKind = ValueKind::Unknown;
201  /// Value type. Required.
202  ValueType mValueType = ValueType::Unknown;
203  /// Pointee alignment in bytes. Optional.
204  uint32_t mPointeeAlign = 0;
205  /// Address space qualifier. Optional.
206  AddressSpaceQualifier mAddrSpaceQual = AddressSpaceQualifier::Unknown;
207  /// Access qualifier. Optional.
208  AccessQualifier mAccQual = AccessQualifier::Unknown;
209  /// Actual access qualifier. Optional.
210  AccessQualifier mActualAccQual = AccessQualifier::Unknown;
211  /// True if 'const' qualifier is specified. Optional.
212  bool mIsConst = false;
213  /// True if 'restrict' qualifier is specified. Optional.
214  bool mIsRestrict = false;
215  /// True if 'volatile' qualifier is specified. Optional.
216  bool mIsVolatile = false;
217  /// True if 'pipe' qualifier is specified. Optional.
218  bool mIsPipe = false;
219
220  /// Default constructor.
221  Metadata() = default;
222};
223
224} // end namespace Arg
225
226//===----------------------------------------------------------------------===//
227// Kernel Code Properties Metadata.
228//===----------------------------------------------------------------------===//
229namespace CodeProps {
230
231namespace Key {
232/// Key for Kernel::CodeProps::Metadata::mKernargSegmentSize.
233constexpr char KernargSegmentSize[] = "KernargSegmentSize";
234/// Key for Kernel::CodeProps::Metadata::mGroupSegmentFixedSize.
235constexpr char GroupSegmentFixedSize[] = "GroupSegmentFixedSize";
236/// Key for Kernel::CodeProps::Metadata::mPrivateSegmentFixedSize.
237constexpr char PrivateSegmentFixedSize[] = "PrivateSegmentFixedSize";
238/// Key for Kernel::CodeProps::Metadata::mKernargSegmentAlign.
239constexpr char KernargSegmentAlign[] = "KernargSegmentAlign";
240/// Key for Kernel::CodeProps::Metadata::mWavefrontSize.
241constexpr char WavefrontSize[] = "WavefrontSize";
242/// Key for Kernel::CodeProps::Metadata::mNumSGPRs.
243constexpr char NumSGPRs[] = "NumSGPRs";
244/// Key for Kernel::CodeProps::Metadata::mNumVGPRs.
245constexpr char NumVGPRs[] = "NumVGPRs";
246/// Key for Kernel::CodeProps::Metadata::mMaxFlatWorkGroupSize.
247constexpr char MaxFlatWorkGroupSize[] = "MaxFlatWorkGroupSize";
248/// Key for Kernel::CodeProps::Metadata::mIsDynamicCallStack.
249constexpr char IsDynamicCallStack[] = "IsDynamicCallStack";
250/// Key for Kernel::CodeProps::Metadata::mIsXNACKEnabled.
251constexpr char IsXNACKEnabled[] = "IsXNACKEnabled";
252/// Key for Kernel::CodeProps::Metadata::mNumSpilledSGPRs.
253constexpr char NumSpilledSGPRs[] = "NumSpilledSGPRs";
254/// Key for Kernel::CodeProps::Metadata::mNumSpilledVGPRs.
255constexpr char NumSpilledVGPRs[] = "NumSpilledVGPRs";
256} // end namespace Key
257
258/// In-memory representation of kernel code properties metadata.
259struct Metadata final {
260  /// Size in bytes of the kernarg segment memory. Kernarg segment memory
261  /// holds the values of the arguments to the kernel. Required.
262  uint64_t mKernargSegmentSize = 0;
263  /// Size in bytes of the group segment memory required by a workgroup.
264  /// This value does not include any dynamically allocated group segment memory
265  /// that may be added when the kernel is dispatched. Required.
266  uint32_t mGroupSegmentFixedSize = 0;
267  /// Size in bytes of the private segment memory required by a workitem.
268  /// Private segment memory includes arg, spill and private segments. Required.
269  uint32_t mPrivateSegmentFixedSize = 0;
270  /// Maximum byte alignment of variables used by the kernel in the
271  /// kernarg memory segment. Required.
272  uint32_t mKernargSegmentAlign = 0;
273  /// Wavefront size. Required.
274  uint32_t mWavefrontSize = 0;
275  /// Total number of SGPRs used by a wavefront. Optional.
276  uint16_t mNumSGPRs = 0;
277  /// Total number of VGPRs used by a workitem. Optional.
278  uint16_t mNumVGPRs = 0;
279  /// Maximum flat work-group size supported by the kernel. Optional.
280  uint32_t mMaxFlatWorkGroupSize = 0;
281  /// True if the generated machine code is using a dynamically sized
282  /// call stack. Optional.
283  bool mIsDynamicCallStack = false;
284  /// True if the generated machine code is capable of supporting XNACK.
285  /// Optional.
286  bool mIsXNACKEnabled = false;
287  /// Number of SGPRs spilled by a wavefront. Optional.
288  uint16_t mNumSpilledSGPRs = 0;
289  /// Number of VGPRs spilled by a workitem. Optional.
290  uint16_t mNumSpilledVGPRs = 0;
291
292  /// Default constructor.
293  Metadata() = default;
294
295  /// \returns True if kernel code properties metadata is empty, false
296  /// otherwise.
297  bool empty() const {
298    return !notEmpty();
299  }
300
301  /// \returns True if kernel code properties metadata is not empty, false
302  /// otherwise.
303  bool notEmpty() const {
304    return true;
305  }
306};
307
308} // end namespace CodeProps
309
310//===----------------------------------------------------------------------===//
311// Kernel Debug Properties Metadata.
312//===----------------------------------------------------------------------===//
313namespace DebugProps {
314
315namespace Key {
316/// Key for Kernel::DebugProps::Metadata::mDebuggerABIVersion.
317constexpr char DebuggerABIVersion[] = "DebuggerABIVersion";
318/// Key for Kernel::DebugProps::Metadata::mReservedNumVGPRs.
319constexpr char ReservedNumVGPRs[] = "ReservedNumVGPRs";
320/// Key for Kernel::DebugProps::Metadata::mReservedFirstVGPR.
321constexpr char ReservedFirstVGPR[] = "ReservedFirstVGPR";
322/// Key for Kernel::DebugProps::Metadata::mPrivateSegmentBufferSGPR.
323constexpr char PrivateSegmentBufferSGPR[] = "PrivateSegmentBufferSGPR";
324/// Key for
325///     Kernel::DebugProps::Metadata::mWavefrontPrivateSegmentOffsetSGPR.
326constexpr char WavefrontPrivateSegmentOffsetSGPR[] =
327    "WavefrontPrivateSegmentOffsetSGPR";
328} // end namespace Key
329
330/// In-memory representation of kernel debug properties metadata.
331struct Metadata final {
332  /// Debugger ABI version. Optional.
333  std::vector<uint32_t> mDebuggerABIVersion = std::vector<uint32_t>();
334  /// Consecutive number of VGPRs reserved for debugger use. Must be 0 if
335  /// mDebuggerABIVersion is not set. Optional.
336  uint16_t mReservedNumVGPRs = 0;
337  /// First fixed VGPR reserved. Must be uint16_t(-1) if
338  /// mDebuggerABIVersion is not set or mReservedFirstVGPR is 0. Optional.
339  uint16_t mReservedFirstVGPR = uint16_t(-1);
340  /// Fixed SGPR of the first of 4 SGPRs used to hold the scratch V# used
341  /// for the entire kernel execution. Must be uint16_t(-1) if
342  /// mDebuggerABIVersion is not set or SGPR not used or not known. Optional.
343  uint16_t mPrivateSegmentBufferSGPR = uint16_t(-1);
344  /// Fixed SGPR used to hold the wave scratch offset for the entire
345  /// kernel execution. Must be uint16_t(-1) if mDebuggerABIVersion is not set
346  /// or SGPR is not used or not known. Optional.
347  uint16_t mWavefrontPrivateSegmentOffsetSGPR = uint16_t(-1);
348
349  /// Default constructor.
350  Metadata() = default;
351
352  /// \returns True if kernel debug properties metadata is empty, false
353  /// otherwise.
354  bool empty() const {
355    return !notEmpty();
356  }
357
358  /// \returns True if kernel debug properties metadata is not empty, false
359  /// otherwise.
360  bool notEmpty() const {
361    return !mDebuggerABIVersion.empty();
362  }
363};
364
365} // end namespace DebugProps
366
367namespace Key {
368/// Key for Kernel::Metadata::mName.
369constexpr char Name[] = "Name";
370/// Key for Kernel::Metadata::mSymbolName.
371constexpr char SymbolName[] = "SymbolName";
372/// Key for Kernel::Metadata::mLanguage.
373constexpr char Language[] = "Language";
374/// Key for Kernel::Metadata::mLanguageVersion.
375constexpr char LanguageVersion[] = "LanguageVersion";
376/// Key for Kernel::Metadata::mAttrs.
377constexpr char Attrs[] = "Attrs";
378/// Key for Kernel::Metadata::mArgs.
379constexpr char Args[] = "Args";
380/// Key for Kernel::Metadata::mCodeProps.
381constexpr char CodeProps[] = "CodeProps";
382/// Key for Kernel::Metadata::mDebugProps.
383constexpr char DebugProps[] = "DebugProps";
384} // end namespace Key
385
386/// In-memory representation of kernel metadata.
387struct Metadata final {
388  /// Kernel source name. Required.
389  std::string mName = std::string();
390  /// Kernel descriptor name. Required.
391  std::string mSymbolName = std::string();
392  /// Language. Optional.
393  std::string mLanguage = std::string();
394  /// Language version. Optional.
395  std::vector<uint32_t> mLanguageVersion = std::vector<uint32_t>();
396  /// Attributes metadata. Optional.
397  Attrs::Metadata mAttrs = Attrs::Metadata();
398  /// Arguments metadata. Optional.
399  std::vector<Arg::Metadata> mArgs = std::vector<Arg::Metadata>();
400  /// Code properties metadata. Optional.
401  CodeProps::Metadata mCodeProps = CodeProps::Metadata();
402  /// Debug properties metadata. Optional.
403  DebugProps::Metadata mDebugProps = DebugProps::Metadata();
404
405  /// Default constructor.
406  Metadata() = default;
407};
408
409} // end namespace Kernel
410
411namespace Key {
412/// Key for HSA::Metadata::mVersion.
413constexpr char Version[] = "Version";
414/// Key for HSA::Metadata::mPrintf.
415constexpr char Printf[] = "Printf";
416/// Key for HSA::Metadata::mKernels.
417constexpr char Kernels[] = "Kernels";
418} // end namespace Key
419
420/// In-memory representation of HSA metadata.
421struct Metadata final {
422  /// HSA metadata version. Required.
423  std::vector<uint32_t> mVersion = std::vector<uint32_t>();
424  /// Printf metadata. Optional.
425  std::vector<std::string> mPrintf = std::vector<std::string>();
426  /// Kernels metadata. Required.
427  std::vector<Kernel::Metadata> mKernels = std::vector<Kernel::Metadata>();
428
429  /// Default constructor.
430  Metadata() = default;
431};
432
433/// Converts \p String to \p HSAMetadata.
434std::error_code fromString(std::string String, Metadata &HSAMetadata);
435
436/// Converts \p HSAMetadata to \p String.
437std::error_code toString(Metadata HSAMetadata, std::string &String);
438
439//===----------------------------------------------------------------------===//
440// HSA metadata for v3 code object.
441//===----------------------------------------------------------------------===//
442namespace V3 {
443/// HSA metadata major version.
444constexpr uint32_t VersionMajor = 1;
445/// HSA metadata minor version.
446constexpr uint32_t VersionMinor = 0;
447
448/// HSA metadata beginning assembler directive.
449constexpr char AssemblerDirectiveBegin[] = ".amdgpu_metadata";
450/// HSA metadata ending assembler directive.
451constexpr char AssemblerDirectiveEnd[] = ".end_amdgpu_metadata";
452} // end namespace V3
453
454} // end namespace HSAMD
455
456//===----------------------------------------------------------------------===//
457// PAL metadata.
458//===----------------------------------------------------------------------===//
459namespace PALMD {
460
461/// PAL metadata (old linear format) assembler directive.
462constexpr char AssemblerDirective[] = ".amd_amdgpu_pal_metadata";
463
464/// PAL metadata (new MsgPack format) beginning assembler directive.
465constexpr char AssemblerDirectiveBegin[] = ".amdgpu_pal_metadata";
466
467/// PAL metadata (new MsgPack format) ending assembler directive.
468constexpr char AssemblerDirectiveEnd[] = ".end_amdgpu_pal_metadata";
469
470/// PAL metadata keys.
471enum Key : uint32_t {
472  R_2E12_COMPUTE_PGM_RSRC1 = 0x2e12,
473  R_2D4A_SPI_SHADER_PGM_RSRC1_LS = 0x2d4a,
474  R_2D0A_SPI_SHADER_PGM_RSRC1_HS = 0x2d0a,
475  R_2CCA_SPI_SHADER_PGM_RSRC1_ES = 0x2cca,
476  R_2C8A_SPI_SHADER_PGM_RSRC1_GS = 0x2c8a,
477  R_2C4A_SPI_SHADER_PGM_RSRC1_VS = 0x2c4a,
478  R_2C0A_SPI_SHADER_PGM_RSRC1_PS = 0x2c0a,
479  R_2E00_COMPUTE_DISPATCH_INITIATOR = 0x2e00,
480  R_A1B3_SPI_PS_INPUT_ENA = 0xa1b3,
481  R_A1B4_SPI_PS_INPUT_ADDR = 0xa1b4,
482  R_A1B6_SPI_PS_IN_CONTROL = 0xa1b6,
483  R_A2D5_VGT_SHADER_STAGES_EN = 0xa2d5,
484
485  LS_NUM_USED_VGPRS = 0x10000021,
486  HS_NUM_USED_VGPRS = 0x10000022,
487  ES_NUM_USED_VGPRS = 0x10000023,
488  GS_NUM_USED_VGPRS = 0x10000024,
489  VS_NUM_USED_VGPRS = 0x10000025,
490  PS_NUM_USED_VGPRS = 0x10000026,
491  CS_NUM_USED_VGPRS = 0x10000027,
492
493  LS_NUM_USED_SGPRS = 0x10000028,
494  HS_NUM_USED_SGPRS = 0x10000029,
495  ES_NUM_USED_SGPRS = 0x1000002a,
496  GS_NUM_USED_SGPRS = 0x1000002b,
497  VS_NUM_USED_SGPRS = 0x1000002c,
498  PS_NUM_USED_SGPRS = 0x1000002d,
499  CS_NUM_USED_SGPRS = 0x1000002e,
500
501  LS_SCRATCH_SIZE = 0x10000044,
502  HS_SCRATCH_SIZE = 0x10000045,
503  ES_SCRATCH_SIZE = 0x10000046,
504  GS_SCRATCH_SIZE = 0x10000047,
505  VS_SCRATCH_SIZE = 0x10000048,
506  PS_SCRATCH_SIZE = 0x10000049,
507  CS_SCRATCH_SIZE = 0x1000004a
508};
509
510} // end namespace PALMD
511} // end namespace AMDGPU
512} // end namespace llvm
513
514#endif // LLVM_SUPPORT_AMDGPUMETADATA_H
515