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