AMDGPUMetadata.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //===--- AMDGPUMetadata.cpp -------------------------------------*- 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. #include "llvm/Support/AMDGPUMetadata.h"
  15. #include "llvm/Support/YAMLTraits.h"
  16. #include <optional>
  17. using namespace llvm::AMDGPU;
  18. using namespace llvm::AMDGPU::HSAMD;
  19. LLVM_YAML_IS_SEQUENCE_VECTOR(Kernel::Arg::Metadata)
  20. LLVM_YAML_IS_SEQUENCE_VECTOR(Kernel::Metadata)
  21. namespace llvm {
  22. namespace yaml {
  23. template <>
  24. struct ScalarEnumerationTraits<AccessQualifier> {
  25. static void enumeration(IO &YIO, AccessQualifier &EN) {
  26. YIO.enumCase(EN, "Default", AccessQualifier::Default);
  27. YIO.enumCase(EN, "ReadOnly", AccessQualifier::ReadOnly);
  28. YIO.enumCase(EN, "WriteOnly", AccessQualifier::WriteOnly);
  29. YIO.enumCase(EN, "ReadWrite", AccessQualifier::ReadWrite);
  30. }
  31. };
  32. template <>
  33. struct ScalarEnumerationTraits<AddressSpaceQualifier> {
  34. static void enumeration(IO &YIO, AddressSpaceQualifier &EN) {
  35. YIO.enumCase(EN, "Private", AddressSpaceQualifier::Private);
  36. YIO.enumCase(EN, "Global", AddressSpaceQualifier::Global);
  37. YIO.enumCase(EN, "Constant", AddressSpaceQualifier::Constant);
  38. YIO.enumCase(EN, "Local", AddressSpaceQualifier::Local);
  39. YIO.enumCase(EN, "Generic", AddressSpaceQualifier::Generic);
  40. YIO.enumCase(EN, "Region", AddressSpaceQualifier::Region);
  41. }
  42. };
  43. template <>
  44. struct ScalarEnumerationTraits<ValueKind> {
  45. static void enumeration(IO &YIO, ValueKind &EN) {
  46. YIO.enumCase(EN, "ByValue", ValueKind::ByValue);
  47. YIO.enumCase(EN, "GlobalBuffer", ValueKind::GlobalBuffer);
  48. YIO.enumCase(EN, "DynamicSharedPointer", ValueKind::DynamicSharedPointer);
  49. YIO.enumCase(EN, "Sampler", ValueKind::Sampler);
  50. YIO.enumCase(EN, "Image", ValueKind::Image);
  51. YIO.enumCase(EN, "Pipe", ValueKind::Pipe);
  52. YIO.enumCase(EN, "Queue", ValueKind::Queue);
  53. YIO.enumCase(EN, "HiddenGlobalOffsetX", ValueKind::HiddenGlobalOffsetX);
  54. YIO.enumCase(EN, "HiddenGlobalOffsetY", ValueKind::HiddenGlobalOffsetY);
  55. YIO.enumCase(EN, "HiddenGlobalOffsetZ", ValueKind::HiddenGlobalOffsetZ);
  56. YIO.enumCase(EN, "HiddenNone", ValueKind::HiddenNone);
  57. YIO.enumCase(EN, "HiddenPrintfBuffer", ValueKind::HiddenPrintfBuffer);
  58. YIO.enumCase(EN, "HiddenHostcallBuffer", ValueKind::HiddenHostcallBuffer);
  59. YIO.enumCase(EN, "HiddenDefaultQueue", ValueKind::HiddenDefaultQueue);
  60. YIO.enumCase(EN, "HiddenCompletionAction",
  61. ValueKind::HiddenCompletionAction);
  62. YIO.enumCase(EN, "HiddenMultiGridSyncArg",
  63. ValueKind::HiddenMultiGridSyncArg);
  64. }
  65. };
  66. template <>
  67. struct ScalarEnumerationTraits<ValueType> {
  68. static void enumeration(IO &YIO, ValueType &EN) {
  69. YIO.enumCase(EN, "Struct", ValueType::Struct);
  70. YIO.enumCase(EN, "I8", ValueType::I8);
  71. YIO.enumCase(EN, "U8", ValueType::U8);
  72. YIO.enumCase(EN, "I16", ValueType::I16);
  73. YIO.enumCase(EN, "U16", ValueType::U16);
  74. YIO.enumCase(EN, "F16", ValueType::F16);
  75. YIO.enumCase(EN, "I32", ValueType::I32);
  76. YIO.enumCase(EN, "U32", ValueType::U32);
  77. YIO.enumCase(EN, "F32", ValueType::F32);
  78. YIO.enumCase(EN, "I64", ValueType::I64);
  79. YIO.enumCase(EN, "U64", ValueType::U64);
  80. YIO.enumCase(EN, "F64", ValueType::F64);
  81. }
  82. };
  83. template <>
  84. struct MappingTraits<Kernel::Attrs::Metadata> {
  85. static void mapping(IO &YIO, Kernel::Attrs::Metadata &MD) {
  86. YIO.mapOptional(Kernel::Attrs::Key::ReqdWorkGroupSize,
  87. MD.mReqdWorkGroupSize, std::vector<uint32_t>());
  88. YIO.mapOptional(Kernel::Attrs::Key::WorkGroupSizeHint,
  89. MD.mWorkGroupSizeHint, std::vector<uint32_t>());
  90. YIO.mapOptional(Kernel::Attrs::Key::VecTypeHint,
  91. MD.mVecTypeHint, std::string());
  92. YIO.mapOptional(Kernel::Attrs::Key::RuntimeHandle, MD.mRuntimeHandle,
  93. std::string());
  94. }
  95. };
  96. template <>
  97. struct MappingTraits<Kernel::Arg::Metadata> {
  98. static void mapping(IO &YIO, Kernel::Arg::Metadata &MD) {
  99. YIO.mapOptional(Kernel::Arg::Key::Name, MD.mName, std::string());
  100. YIO.mapOptional(Kernel::Arg::Key::TypeName, MD.mTypeName, std::string());
  101. YIO.mapRequired(Kernel::Arg::Key::Size, MD.mSize);
  102. YIO.mapRequired(Kernel::Arg::Key::Align, MD.mAlign);
  103. YIO.mapRequired(Kernel::Arg::Key::ValueKind, MD.mValueKind);
  104. // Removed. Accepted for parsing compatibility, but not emitted.
  105. std::optional<ValueType> Unused;
  106. YIO.mapOptional(Kernel::Arg::Key::ValueType, Unused);
  107. YIO.mapOptional(Kernel::Arg::Key::PointeeAlign, MD.mPointeeAlign,
  108. uint32_t(0));
  109. YIO.mapOptional(Kernel::Arg::Key::AddrSpaceQual, MD.mAddrSpaceQual,
  110. AddressSpaceQualifier::Unknown);
  111. YIO.mapOptional(Kernel::Arg::Key::AccQual, MD.mAccQual,
  112. AccessQualifier::Unknown);
  113. YIO.mapOptional(Kernel::Arg::Key::ActualAccQual, MD.mActualAccQual,
  114. AccessQualifier::Unknown);
  115. YIO.mapOptional(Kernel::Arg::Key::IsConst, MD.mIsConst, false);
  116. YIO.mapOptional(Kernel::Arg::Key::IsRestrict, MD.mIsRestrict, false);
  117. YIO.mapOptional(Kernel::Arg::Key::IsVolatile, MD.mIsVolatile, false);
  118. YIO.mapOptional(Kernel::Arg::Key::IsPipe, MD.mIsPipe, false);
  119. }
  120. };
  121. template <>
  122. struct MappingTraits<Kernel::CodeProps::Metadata> {
  123. static void mapping(IO &YIO, Kernel::CodeProps::Metadata &MD) {
  124. YIO.mapRequired(Kernel::CodeProps::Key::KernargSegmentSize,
  125. MD.mKernargSegmentSize);
  126. YIO.mapRequired(Kernel::CodeProps::Key::GroupSegmentFixedSize,
  127. MD.mGroupSegmentFixedSize);
  128. YIO.mapRequired(Kernel::CodeProps::Key::PrivateSegmentFixedSize,
  129. MD.mPrivateSegmentFixedSize);
  130. YIO.mapRequired(Kernel::CodeProps::Key::KernargSegmentAlign,
  131. MD.mKernargSegmentAlign);
  132. YIO.mapRequired(Kernel::CodeProps::Key::WavefrontSize,
  133. MD.mWavefrontSize);
  134. YIO.mapOptional(Kernel::CodeProps::Key::NumSGPRs,
  135. MD.mNumSGPRs, uint16_t(0));
  136. YIO.mapOptional(Kernel::CodeProps::Key::NumVGPRs,
  137. MD.mNumVGPRs, uint16_t(0));
  138. YIO.mapOptional(Kernel::CodeProps::Key::MaxFlatWorkGroupSize,
  139. MD.mMaxFlatWorkGroupSize, uint32_t(0));
  140. YIO.mapOptional(Kernel::CodeProps::Key::IsDynamicCallStack,
  141. MD.mIsDynamicCallStack, false);
  142. YIO.mapOptional(Kernel::CodeProps::Key::IsXNACKEnabled,
  143. MD.mIsXNACKEnabled, false);
  144. YIO.mapOptional(Kernel::CodeProps::Key::NumSpilledSGPRs,
  145. MD.mNumSpilledSGPRs, uint16_t(0));
  146. YIO.mapOptional(Kernel::CodeProps::Key::NumSpilledVGPRs,
  147. MD.mNumSpilledVGPRs, uint16_t(0));
  148. }
  149. };
  150. template <>
  151. struct MappingTraits<Kernel::DebugProps::Metadata> {
  152. static void mapping(IO &YIO, Kernel::DebugProps::Metadata &MD) {
  153. YIO.mapOptional(Kernel::DebugProps::Key::DebuggerABIVersion,
  154. MD.mDebuggerABIVersion, std::vector<uint32_t>());
  155. YIO.mapOptional(Kernel::DebugProps::Key::ReservedNumVGPRs,
  156. MD.mReservedNumVGPRs, uint16_t(0));
  157. YIO.mapOptional(Kernel::DebugProps::Key::ReservedFirstVGPR,
  158. MD.mReservedFirstVGPR, uint16_t(-1));
  159. YIO.mapOptional(Kernel::DebugProps::Key::PrivateSegmentBufferSGPR,
  160. MD.mPrivateSegmentBufferSGPR, uint16_t(-1));
  161. YIO.mapOptional(Kernel::DebugProps::Key::WavefrontPrivateSegmentOffsetSGPR,
  162. MD.mWavefrontPrivateSegmentOffsetSGPR, uint16_t(-1));
  163. }
  164. };
  165. template <>
  166. struct MappingTraits<Kernel::Metadata> {
  167. static void mapping(IO &YIO, Kernel::Metadata &MD) {
  168. YIO.mapRequired(Kernel::Key::Name, MD.mName);
  169. YIO.mapRequired(Kernel::Key::SymbolName, MD.mSymbolName);
  170. YIO.mapOptional(Kernel::Key::Language, MD.mLanguage, std::string());
  171. YIO.mapOptional(Kernel::Key::LanguageVersion, MD.mLanguageVersion,
  172. std::vector<uint32_t>());
  173. if (!MD.mAttrs.empty() || !YIO.outputting())
  174. YIO.mapOptional(Kernel::Key::Attrs, MD.mAttrs);
  175. if (!MD.mArgs.empty() || !YIO.outputting())
  176. YIO.mapOptional(Kernel::Key::Args, MD.mArgs);
  177. if (!MD.mCodeProps.empty() || !YIO.outputting())
  178. YIO.mapOptional(Kernel::Key::CodeProps, MD.mCodeProps);
  179. if (!MD.mDebugProps.empty() || !YIO.outputting())
  180. YIO.mapOptional(Kernel::Key::DebugProps, MD.mDebugProps);
  181. }
  182. };
  183. template <>
  184. struct MappingTraits<HSAMD::Metadata> {
  185. static void mapping(IO &YIO, HSAMD::Metadata &MD) {
  186. YIO.mapRequired(Key::Version, MD.mVersion);
  187. YIO.mapOptional(Key::Printf, MD.mPrintf, std::vector<std::string>());
  188. if (!MD.mKernels.empty() || !YIO.outputting())
  189. YIO.mapOptional(Key::Kernels, MD.mKernels);
  190. }
  191. };
  192. } // end namespace yaml
  193. namespace AMDGPU {
  194. namespace HSAMD {
  195. std::error_code fromString(StringRef String, Metadata &HSAMetadata) {
  196. yaml::Input YamlInput(String);
  197. YamlInput >> HSAMetadata;
  198. return YamlInput.error();
  199. }
  200. std::error_code toString(Metadata HSAMetadata, std::string &String) {
  201. raw_string_ostream YamlStream(String);
  202. yaml::Output YamlOutput(YamlStream, nullptr, std::numeric_limits<int>::max());
  203. YamlOutput << HSAMetadata;
  204. return std::error_code();
  205. }
  206. } // end namespace HSAMD
  207. } // end namespace AMDGPU
  208. } // end namespace llvm