[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / llvm / lib / ObjectYAML / DXContainerYAML.cpp
blob741b0cb47586fff29867965b2d8150cd2fe37fcb
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/BinaryFormat/DXContainer.h"
17 namespace llvm {
19 // This assert is duplicated here to leave a breadcrumb of the places that need
20 // to be updated if flags grow past 64-bits.
21 static_assert((uint64_t)dxbc::FeatureFlags::NextUnusedBit <= 1ull << 63,
22 "Shader flag bits exceed enum size.");
24 DXContainerYAML::ShaderFlags::ShaderFlags(uint64_t FlagData) {
25 #define SHADER_FLAG(Num, Val, Str) \
26 Val = (FlagData & (uint64_t)dxbc::FeatureFlags::Val) > 0;
27 #include "llvm/BinaryFormat/DXContainerConstants.def"
30 uint64_t DXContainerYAML::ShaderFlags::getEncodedFlags() {
31 uint64_t Flag = 0;
32 #define SHADER_FLAG(Num, Val, Str) \
33 if (Val) \
34 Flag |= (uint64_t)dxbc::FeatureFlags::Val;
35 #include "llvm/BinaryFormat/DXContainerConstants.def"
36 return Flag;
39 DXContainerYAML::ShaderHash::ShaderHash(const dxbc::ShaderHash &Data)
40 : IncludesSource((Data.Flags & static_cast<uint32_t>(
41 dxbc::HashFlags::IncludesSource)) != 0),
42 Digest(16, 0) {
43 memcpy(Digest.data(), &Data.Digest[0], 16);
46 DXContainerYAML::PSVInfo::PSVInfo() : Version(0) {
47 memset(&Info, 0, sizeof(Info));
50 DXContainerYAML::PSVInfo::PSVInfo(const dxbc::PSV::v0::RuntimeInfo *P,
51 uint16_t Stage)
52 : Version(0) {
53 memset(&Info, 0, sizeof(Info));
54 memcpy(&Info, P, sizeof(dxbc::PSV::v0::RuntimeInfo));
56 assert(Stage < std::numeric_limits<uint8_t>::max() &&
57 "Stage should be a very small number");
58 // We need to bring the stage in separately since it isn't part of the v1 data
59 // structure.
60 Info.ShaderStage = static_cast<uint8_t>(Stage);
63 DXContainerYAML::PSVInfo::PSVInfo(const dxbc::PSV::v1::RuntimeInfo *P)
64 : Version(1) {
65 memset(&Info, 0, sizeof(Info));
66 memcpy(&Info, P, sizeof(dxbc::PSV::v1::RuntimeInfo));
69 DXContainerYAML::PSVInfo::PSVInfo(const dxbc::PSV::v2::RuntimeInfo *P)
70 : Version(2) {
71 memset(&Info, 0, sizeof(Info));
72 memcpy(&Info, P, sizeof(dxbc::PSV::v2::RuntimeInfo));
75 namespace yaml {
77 void MappingTraits<DXContainerYAML::VersionTuple>::mapping(
78 IO &IO, DXContainerYAML::VersionTuple &Version) {
79 IO.mapRequired("Major", Version.Major);
80 IO.mapRequired("Minor", Version.Minor);
83 void MappingTraits<DXContainerYAML::FileHeader>::mapping(
84 IO &IO, DXContainerYAML::FileHeader &Header) {
85 IO.mapRequired("Hash", Header.Hash);
86 IO.mapRequired("Version", Header.Version);
87 IO.mapOptional("FileSize", Header.FileSize);
88 IO.mapRequired("PartCount", Header.PartCount);
89 IO.mapOptional("PartOffsets", Header.PartOffsets);
92 void MappingTraits<DXContainerYAML::DXILProgram>::mapping(
93 IO &IO, DXContainerYAML::DXILProgram &Program) {
94 IO.mapRequired("MajorVersion", Program.MajorVersion);
95 IO.mapRequired("MinorVersion", Program.MinorVersion);
96 IO.mapRequired("ShaderKind", Program.ShaderKind);
97 IO.mapOptional("Size", Program.Size);
98 IO.mapRequired("DXILMajorVersion", Program.DXILMajorVersion);
99 IO.mapRequired("DXILMinorVersion", Program.DXILMinorVersion);
100 IO.mapOptional("DXILSize", Program.DXILSize);
101 IO.mapOptional("DXIL", Program.DXIL);
104 void MappingTraits<DXContainerYAML::ShaderFlags>::mapping(
105 IO &IO, DXContainerYAML::ShaderFlags &Flags) {
106 #define SHADER_FLAG(Num, Val, Str) IO.mapRequired(#Val, Flags.Val);
107 #include "llvm/BinaryFormat/DXContainerConstants.def"
110 void MappingTraits<DXContainerYAML::ShaderHash>::mapping(
111 IO &IO, DXContainerYAML::ShaderHash &Hash) {
112 IO.mapRequired("IncludesSource", Hash.IncludesSource);
113 IO.mapRequired("Digest", Hash.Digest);
116 void MappingTraits<DXContainerYAML::PSVInfo>::mapping(
117 IO &IO, DXContainerYAML::PSVInfo &PSV) {
118 IO.mapRequired("Version", PSV.Version);
120 // Store the PSV version in the YAML context.
121 void *OldContext = IO.getContext();
122 uint32_t Version = PSV.Version;
123 IO.setContext(&Version);
125 // Shader stage is only included in binaries for v1 and later, but we always
126 // include it since it simplifies parsing and file construction.
127 IO.mapRequired("ShaderStage", PSV.Info.ShaderStage);
128 PSV.mapInfoForVersion(IO);
130 IO.mapRequired("Resources", PSV.Resources);
132 // Restore the YAML context.
133 IO.setContext(OldContext);
136 void MappingTraits<DXContainerYAML::Part>::mapping(IO &IO,
137 DXContainerYAML::Part &P) {
138 IO.mapRequired("Name", P.Name);
139 IO.mapRequired("Size", P.Size);
140 IO.mapOptional("Program", P.Program);
141 IO.mapOptional("Flags", P.Flags);
142 IO.mapOptional("Hash", P.Hash);
143 IO.mapOptional("PSVInfo", P.Info);
146 void MappingTraits<DXContainerYAML::Object>::mapping(
147 IO &IO, DXContainerYAML::Object &Obj) {
148 IO.mapTag("!dxcontainer", true);
149 IO.mapRequired("Header", Obj.Header);
150 IO.mapRequired("Parts", Obj.Parts);
153 void MappingTraits<DXContainerYAML::ResourceBindInfo>::mapping(
154 IO &IO, DXContainerYAML::ResourceBindInfo &Res) {
155 IO.mapRequired("Type", Res.Type);
156 IO.mapRequired("Space", Res.Space);
157 IO.mapRequired("LowerBound", Res.LowerBound);
158 IO.mapRequired("UpperBound", Res.UpperBound);
160 const uint32_t *PSVVersion = static_cast<uint32_t *>(IO.getContext());
161 if (*PSVVersion < 2)
162 return;
164 IO.mapRequired("Kind", Res.Kind);
165 IO.mapRequired("Flags", Res.Flags);
168 } // namespace yaml
170 void DXContainerYAML::PSVInfo::mapInfoForVersion(yaml::IO &IO) {
171 dxbc::PipelinePSVInfo &StageInfo = Info.StageInfo;
172 Triple::EnvironmentType Stage = dxbc::getShaderStage(Info.ShaderStage);
174 switch (Stage) {
175 case Triple::EnvironmentType::Pixel:
176 IO.mapRequired("DepthOutput", StageInfo.PS.DepthOutput);
177 IO.mapRequired("SampleFrequency", StageInfo.PS.SampleFrequency);
178 break;
179 case Triple::EnvironmentType::Vertex:
180 IO.mapRequired("OutputPositionPresent", StageInfo.VS.OutputPositionPresent);
181 break;
182 case Triple::EnvironmentType::Geometry:
183 IO.mapRequired("InputPrimitive", StageInfo.GS.InputPrimitive);
184 IO.mapRequired("OutputTopology", StageInfo.GS.OutputTopology);
185 IO.mapRequired("OutputStreamMask", StageInfo.GS.OutputStreamMask);
186 IO.mapRequired("OutputPositionPresent", StageInfo.GS.OutputPositionPresent);
187 break;
188 case Triple::EnvironmentType::Hull:
189 IO.mapRequired("InputControlPointCount",
190 StageInfo.HS.InputControlPointCount);
191 IO.mapRequired("OutputControlPointCount",
192 StageInfo.HS.OutputControlPointCount);
193 IO.mapRequired("TessellatorDomain", StageInfo.HS.TessellatorDomain);
194 IO.mapRequired("TessellatorOutputPrimitive",
195 StageInfo.HS.TessellatorOutputPrimitive);
196 break;
197 case Triple::EnvironmentType::Domain:
198 IO.mapRequired("InputControlPointCount",
199 StageInfo.DS.InputControlPointCount);
200 IO.mapRequired("OutputPositionPresent", StageInfo.DS.OutputPositionPresent);
201 IO.mapRequired("TessellatorDomain", StageInfo.DS.TessellatorDomain);
202 break;
203 case Triple::EnvironmentType::Mesh:
204 IO.mapRequired("GroupSharedBytesUsed", StageInfo.MS.GroupSharedBytesUsed);
205 IO.mapRequired("GroupSharedBytesDependentOnViewID",
206 StageInfo.MS.GroupSharedBytesDependentOnViewID);
207 IO.mapRequired("PayloadSizeInBytes", StageInfo.MS.PayloadSizeInBytes);
208 IO.mapRequired("MaxOutputVertices", StageInfo.MS.MaxOutputVertices);
209 IO.mapRequired("MaxOutputPrimitives", StageInfo.MS.MaxOutputPrimitives);
210 break;
211 case Triple::EnvironmentType::Amplification:
212 IO.mapRequired("PayloadSizeInBytes", StageInfo.AS.PayloadSizeInBytes);
213 break;
214 default:
215 break;
218 IO.mapRequired("MinimumWaveLaneCount", Info.MinimumWaveLaneCount);
219 IO.mapRequired("MaximumWaveLaneCount", Info.MaximumWaveLaneCount);
221 if (Version == 0)
222 return;
224 IO.mapRequired("UsesViewID", Info.UsesViewID);
226 switch (Stage) {
227 case Triple::EnvironmentType::Geometry:
228 IO.mapRequired("MaxVertexCount", Info.GeomData.MaxVertexCount);
229 break;
230 case Triple::EnvironmentType::Hull:
231 case Triple::EnvironmentType::Domain:
232 IO.mapRequired("SigPatchConstOrPrimVectors",
233 Info.GeomData.SigPatchConstOrPrimVectors);
234 break;
235 case Triple::EnvironmentType::Mesh:
236 IO.mapRequired("SigPrimVectors", Info.GeomData.MeshInfo.SigPrimVectors);
237 IO.mapRequired("MeshOutputTopology",
238 Info.GeomData.MeshInfo.MeshOutputTopology);
239 break;
240 default:
241 break;
244 IO.mapRequired("SigInputElements", Info.SigInputElements);
245 IO.mapRequired("SigOutputElements", Info.SigOutputElements);
246 IO.mapRequired("SigPatchConstOrPrimElements",
247 Info.SigPatchConstOrPrimElements);
248 IO.mapRequired("SigInputVectors", Info.SigInputVectors);
249 MutableArrayRef<uint8_t> Vec(Info.SigOutputVectors);
250 IO.mapRequired("SigOutputVectors", Vec);
252 if (Version == 1)
253 return;
255 IO.mapRequired("NumThreadsX", Info.NumThreadsX);
256 IO.mapRequired("NumThreadsY", Info.NumThreadsY);
257 IO.mapRequired("NumThreadsZ", Info.NumThreadsZ);
260 } // namespace llvm