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