[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / lib / ObjectYAML / DXContainerYAML.cpp
blobc7cf1ec9afc1f661766f8d81690d44d8adce5ee0
1 //===- DXContainerYAML.cpp - DXContainer YAMLIO implementation ------------===//
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 // This file defines classes for handling the YAML representation of
10 // DXContainerYAML.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ObjectYAML/DXContainerYAML.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/BinaryFormat/DXContainer.h"
17 #include "llvm/Support/ScopedPrinter.h"
19 namespace llvm {
21 // This assert is duplicated here to leave a breadcrumb of the places that need
22 // to be updated if flags grow past 64-bits.
23 static_assert((uint64_t)dxbc::FeatureFlags::NextUnusedBit <= 1ull << 63,
24 "Shader flag bits exceed enum size.");
26 DXContainerYAML::ShaderFlags::ShaderFlags(uint64_t FlagData) {
27 #define SHADER_FLAG(Num, Val, Str) \
28 Val = (FlagData & (uint64_t)dxbc::FeatureFlags::Val) > 0;
29 #include "llvm/BinaryFormat/DXContainerConstants.def"
32 uint64_t DXContainerYAML::ShaderFlags::getEncodedFlags() {
33 uint64_t Flag = 0;
34 #define SHADER_FLAG(Num, Val, Str) \
35 if (Val) \
36 Flag |= (uint64_t)dxbc::FeatureFlags::Val;
37 #include "llvm/BinaryFormat/DXContainerConstants.def"
38 return Flag;
41 DXContainerYAML::ShaderHash::ShaderHash(const dxbc::ShaderHash &Data)
42 : IncludesSource((Data.Flags & static_cast<uint32_t>(
43 dxbc::HashFlags::IncludesSource)) != 0),
44 Digest(16, 0) {
45 memcpy(Digest.data(), &Data.Digest[0], 16);
48 DXContainerYAML::PSVInfo::PSVInfo() : Version(0) {
49 memset(&Info, 0, sizeof(Info));
52 DXContainerYAML::PSVInfo::PSVInfo(const dxbc::PSV::v0::RuntimeInfo *P,
53 uint16_t Stage)
54 : Version(0) {
55 memset(&Info, 0, sizeof(Info));
56 memcpy(&Info, P, sizeof(dxbc::PSV::v0::RuntimeInfo));
58 assert(Stage < std::numeric_limits<uint8_t>::max() &&
59 "Stage should be a very small number");
60 // We need to bring the stage in separately since it isn't part of the v1 data
61 // structure.
62 Info.ShaderStage = static_cast<uint8_t>(Stage);
65 DXContainerYAML::PSVInfo::PSVInfo(const dxbc::PSV::v1::RuntimeInfo *P)
66 : Version(1) {
67 memset(&Info, 0, sizeof(Info));
68 memcpy(&Info, P, sizeof(dxbc::PSV::v1::RuntimeInfo));
71 DXContainerYAML::PSVInfo::PSVInfo(const dxbc::PSV::v2::RuntimeInfo *P)
72 : Version(2) {
73 memset(&Info, 0, sizeof(Info));
74 memcpy(&Info, P, sizeof(dxbc::PSV::v2::RuntimeInfo));
77 namespace yaml {
79 void MappingTraits<DXContainerYAML::VersionTuple>::mapping(
80 IO &IO, DXContainerYAML::VersionTuple &Version) {
81 IO.mapRequired("Major", Version.Major);
82 IO.mapRequired("Minor", Version.Minor);
85 void MappingTraits<DXContainerYAML::FileHeader>::mapping(
86 IO &IO, DXContainerYAML::FileHeader &Header) {
87 IO.mapRequired("Hash", Header.Hash);
88 IO.mapRequired("Version", Header.Version);
89 IO.mapOptional("FileSize", Header.FileSize);
90 IO.mapRequired("PartCount", Header.PartCount);
91 IO.mapOptional("PartOffsets", Header.PartOffsets);
94 void MappingTraits<DXContainerYAML::DXILProgram>::mapping(
95 IO &IO, DXContainerYAML::DXILProgram &Program) {
96 IO.mapRequired("MajorVersion", Program.MajorVersion);
97 IO.mapRequired("MinorVersion", Program.MinorVersion);
98 IO.mapRequired("ShaderKind", Program.ShaderKind);
99 IO.mapOptional("Size", Program.Size);
100 IO.mapRequired("DXILMajorVersion", Program.DXILMajorVersion);
101 IO.mapRequired("DXILMinorVersion", Program.DXILMinorVersion);
102 IO.mapOptional("DXILSize", Program.DXILSize);
103 IO.mapOptional("DXIL", Program.DXIL);
106 void MappingTraits<DXContainerYAML::ShaderFlags>::mapping(
107 IO &IO, DXContainerYAML::ShaderFlags &Flags) {
108 #define SHADER_FLAG(Num, Val, Str) IO.mapRequired(#Val, Flags.Val);
109 #include "llvm/BinaryFormat/DXContainerConstants.def"
112 void MappingTraits<DXContainerYAML::ShaderHash>::mapping(
113 IO &IO, DXContainerYAML::ShaderHash &Hash) {
114 IO.mapRequired("IncludesSource", Hash.IncludesSource);
115 IO.mapRequired("Digest", Hash.Digest);
118 void MappingTraits<DXContainerYAML::PSVInfo>::mapping(
119 IO &IO, DXContainerYAML::PSVInfo &PSV) {
120 IO.mapRequired("Version", PSV.Version);
122 // Store the PSV version in the YAML context.
123 void *OldContext = IO.getContext();
124 uint32_t Version = PSV.Version;
125 IO.setContext(&Version);
127 // Restore the YAML context on function exit.
128 auto RestoreContext = make_scope_exit([&]() { IO.setContext(OldContext); });
130 // Shader stage is only included in binaries for v1 and later, but we always
131 // include it since it simplifies parsing and file construction.
132 IO.mapRequired("ShaderStage", PSV.Info.ShaderStage);
133 PSV.mapInfoForVersion(IO);
135 IO.mapRequired("ResourceStride", PSV.ResourceStride);
136 IO.mapRequired("Resources", PSV.Resources);
137 if (PSV.Version == 0)
138 return;
139 IO.mapRequired("SigInputElements", PSV.SigInputElements);
140 IO.mapRequired("SigOutputElements", PSV.SigOutputElements);
141 IO.mapRequired("SigPatchOrPrimElements", PSV.SigPatchOrPrimElements);
143 Triple::EnvironmentType Stage = dxbc::getShaderStage(PSV.Info.ShaderStage);
144 if (PSV.Info.UsesViewID) {
145 MutableArrayRef<SmallVector<llvm::yaml::Hex32>> MutableOutMasks(
146 PSV.OutputVectorMasks);
147 IO.mapRequired("OutputVectorMasks", MutableOutMasks);
148 if (Stage == Triple::EnvironmentType::Hull)
149 IO.mapRequired("PatchOrPrimMasks", PSV.PatchOrPrimMasks);
151 MutableArrayRef<SmallVector<llvm::yaml::Hex32>> MutableIOMap(
152 PSV.InputOutputMap);
153 IO.mapRequired("InputOutputMap", MutableIOMap);
155 if (Stage == Triple::EnvironmentType::Hull)
156 IO.mapRequired("InputPatchMap", PSV.InputPatchMap);
158 if (Stage == Triple::EnvironmentType::Domain)
159 IO.mapRequired("PatchOutputMap", PSV.PatchOutputMap);
162 void MappingTraits<DXContainerYAML::Part>::mapping(IO &IO,
163 DXContainerYAML::Part &P) {
164 IO.mapRequired("Name", P.Name);
165 IO.mapRequired("Size", P.Size);
166 IO.mapOptional("Program", P.Program);
167 IO.mapOptional("Flags", P.Flags);
168 IO.mapOptional("Hash", P.Hash);
169 IO.mapOptional("PSVInfo", P.Info);
172 void MappingTraits<DXContainerYAML::Object>::mapping(
173 IO &IO, DXContainerYAML::Object &Obj) {
174 IO.mapTag("!dxcontainer", true);
175 IO.mapRequired("Header", Obj.Header);
176 IO.mapRequired("Parts", Obj.Parts);
179 void MappingTraits<DXContainerYAML::ResourceBindInfo>::mapping(
180 IO &IO, DXContainerYAML::ResourceBindInfo &Res) {
181 IO.mapRequired("Type", Res.Type);
182 IO.mapRequired("Space", Res.Space);
183 IO.mapRequired("LowerBound", Res.LowerBound);
184 IO.mapRequired("UpperBound", Res.UpperBound);
186 const uint32_t *PSVVersion = static_cast<uint32_t *>(IO.getContext());
187 if (*PSVVersion < 2)
188 return;
190 IO.mapRequired("Kind", Res.Kind);
191 IO.mapRequired("Flags", Res.Flags);
194 void MappingTraits<DXContainerYAML::SignatureElement>::mapping(
195 IO &IO, DXContainerYAML::SignatureElement &El) {
196 IO.mapRequired("Name", El.Name);
197 IO.mapRequired("Indices", El.Indices);
198 IO.mapRequired("StartRow", El.StartRow);
199 IO.mapRequired("Cols", El.Cols);
200 IO.mapRequired("StartCol", El.StartCol);
201 IO.mapRequired("Allocated", El.Allocated);
202 IO.mapRequired("Kind", El.Kind);
203 IO.mapRequired("ComponentType", El.Type);
204 IO.mapRequired("Interpolation", El.Mode);
205 IO.mapRequired("DynamicMask", El.DynamicMask);
206 IO.mapRequired("Stream", El.Stream);
209 void ScalarEnumerationTraits<dxbc::PSV::SemanticKind>::enumeration(
210 IO &IO, dxbc::PSV::SemanticKind &Value) {
211 for (const auto &E : dxbc::PSV::getSemanticKinds())
212 IO.enumCase(Value, E.Name.str().c_str(), E.Value);
215 void ScalarEnumerationTraits<dxbc::PSV::ComponentType>::enumeration(
216 IO &IO, dxbc::PSV::ComponentType &Value) {
217 for (const auto &E : dxbc::PSV::getComponentTypes())
218 IO.enumCase(Value, E.Name.str().c_str(), E.Value);
221 void ScalarEnumerationTraits<dxbc::PSV::InterpolationMode>::enumeration(
222 IO &IO, dxbc::PSV::InterpolationMode &Value) {
223 for (const auto &E : dxbc::PSV::getInterpolationModes())
224 IO.enumCase(Value, E.Name.str().c_str(), E.Value);
227 } // namespace yaml
229 void DXContainerYAML::PSVInfo::mapInfoForVersion(yaml::IO &IO) {
230 dxbc::PipelinePSVInfo &StageInfo = Info.StageInfo;
231 Triple::EnvironmentType Stage = dxbc::getShaderStage(Info.ShaderStage);
233 switch (Stage) {
234 case Triple::EnvironmentType::Pixel:
235 IO.mapRequired("DepthOutput", StageInfo.PS.DepthOutput);
236 IO.mapRequired("SampleFrequency", StageInfo.PS.SampleFrequency);
237 break;
238 case Triple::EnvironmentType::Vertex:
239 IO.mapRequired("OutputPositionPresent", StageInfo.VS.OutputPositionPresent);
240 break;
241 case Triple::EnvironmentType::Geometry:
242 IO.mapRequired("InputPrimitive", StageInfo.GS.InputPrimitive);
243 IO.mapRequired("OutputTopology", StageInfo.GS.OutputTopology);
244 IO.mapRequired("OutputStreamMask", StageInfo.GS.OutputStreamMask);
245 IO.mapRequired("OutputPositionPresent", StageInfo.GS.OutputPositionPresent);
246 break;
247 case Triple::EnvironmentType::Hull:
248 IO.mapRequired("InputControlPointCount",
249 StageInfo.HS.InputControlPointCount);
250 IO.mapRequired("OutputControlPointCount",
251 StageInfo.HS.OutputControlPointCount);
252 IO.mapRequired("TessellatorDomain", StageInfo.HS.TessellatorDomain);
253 IO.mapRequired("TessellatorOutputPrimitive",
254 StageInfo.HS.TessellatorOutputPrimitive);
255 break;
256 case Triple::EnvironmentType::Domain:
257 IO.mapRequired("InputControlPointCount",
258 StageInfo.DS.InputControlPointCount);
259 IO.mapRequired("OutputPositionPresent", StageInfo.DS.OutputPositionPresent);
260 IO.mapRequired("TessellatorDomain", StageInfo.DS.TessellatorDomain);
261 break;
262 case Triple::EnvironmentType::Mesh:
263 IO.mapRequired("GroupSharedBytesUsed", StageInfo.MS.GroupSharedBytesUsed);
264 IO.mapRequired("GroupSharedBytesDependentOnViewID",
265 StageInfo.MS.GroupSharedBytesDependentOnViewID);
266 IO.mapRequired("PayloadSizeInBytes", StageInfo.MS.PayloadSizeInBytes);
267 IO.mapRequired("MaxOutputVertices", StageInfo.MS.MaxOutputVertices);
268 IO.mapRequired("MaxOutputPrimitives", StageInfo.MS.MaxOutputPrimitives);
269 break;
270 case Triple::EnvironmentType::Amplification:
271 IO.mapRequired("PayloadSizeInBytes", StageInfo.AS.PayloadSizeInBytes);
272 break;
273 default:
274 break;
277 IO.mapRequired("MinimumWaveLaneCount", Info.MinimumWaveLaneCount);
278 IO.mapRequired("MaximumWaveLaneCount", Info.MaximumWaveLaneCount);
280 if (Version == 0)
281 return;
283 IO.mapRequired("UsesViewID", Info.UsesViewID);
285 switch (Stage) {
286 case Triple::EnvironmentType::Geometry:
287 IO.mapRequired("MaxVertexCount", Info.GeomData.MaxVertexCount);
288 break;
289 case Triple::EnvironmentType::Hull:
290 case Triple::EnvironmentType::Domain:
291 IO.mapRequired("SigPatchConstOrPrimVectors",
292 Info.GeomData.SigPatchConstOrPrimVectors);
293 break;
294 case Triple::EnvironmentType::Mesh:
295 IO.mapRequired("SigPrimVectors", Info.GeomData.MeshInfo.SigPrimVectors);
296 IO.mapRequired("MeshOutputTopology",
297 Info.GeomData.MeshInfo.MeshOutputTopology);
298 break;
299 default:
300 break;
303 IO.mapRequired("SigInputVectors", Info.SigInputVectors);
304 MutableArrayRef<uint8_t> Vec(Info.SigOutputVectors);
305 IO.mapRequired("SigOutputVectors", Vec);
307 if (Version == 1)
308 return;
310 IO.mapRequired("NumThreadsX", Info.NumThreadsX);
311 IO.mapRequired("NumThreadsY", Info.NumThreadsY);
312 IO.mapRequired("NumThreadsZ", Info.NumThreadsZ);
315 } // namespace llvm