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