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