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