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