AMDGPUMetadata.cpp 9.3 KB

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