xref: /freebsd/contrib/llvm-project/llvm/lib/ObjectYAML/DXContainerYAML.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ObjectYAML/DXContainerYAML.h"
15 #include "llvm/BinaryFormat/DXContainer.h"
16 
17 namespace llvm {
18 
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.");
23 
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"
28 }
29 
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;
37 }
38 
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);
44 }
45 
46 namespace yaml {
47 
48 void MappingTraits<DXContainerYAML::VersionTuple>::mapping(
49     IO &IO, DXContainerYAML::VersionTuple &Version) {
50   IO.mapRequired("Major", Version.Major);
51   IO.mapRequired("Minor", Version.Minor);
52 }
53 
54 void MappingTraits<DXContainerYAML::FileHeader>::mapping(
55     IO &IO, DXContainerYAML::FileHeader &Header) {
56   IO.mapRequired("Hash", Header.Hash);
57   IO.mapRequired("Version", Header.Version);
58   IO.mapOptional("FileSize", Header.FileSize);
59   IO.mapRequired("PartCount", Header.PartCount);
60   IO.mapOptional("PartOffsets", Header.PartOffsets);
61 }
62 
63 void MappingTraits<DXContainerYAML::DXILProgram>::mapping(
64     IO &IO, DXContainerYAML::DXILProgram &Program) {
65   IO.mapRequired("MajorVersion", Program.MajorVersion);
66   IO.mapRequired("MinorVersion", Program.MinorVersion);
67   IO.mapRequired("ShaderKind", Program.ShaderKind);
68   IO.mapOptional("Size", Program.Size);
69   IO.mapRequired("DXILMajorVersion", Program.DXILMajorVersion);
70   IO.mapRequired("DXILMinorVersion", Program.DXILMinorVersion);
71   IO.mapOptional("DXILSize", Program.DXILSize);
72   IO.mapOptional("DXIL", Program.DXIL);
73 }
74 
75 void MappingTraits<DXContainerYAML::ShaderFlags>::mapping(
76     IO &IO, DXContainerYAML::ShaderFlags &Flags) {
77 #define SHADER_FLAG(Num, Val, Str) IO.mapRequired(#Val, Flags.Val);
78 #include "llvm/BinaryFormat/DXContainerConstants.def"
79 }
80 
81 void MappingTraits<DXContainerYAML::ShaderHash>::mapping(
82     IO &IO, DXContainerYAML::ShaderHash &Hash) {
83   IO.mapRequired("IncludesSource", Hash.IncludesSource);
84   IO.mapRequired("Digest", Hash.Digest);
85 }
86 
87 void MappingTraits<DXContainerYAML::Part>::mapping(IO &IO,
88                                                    DXContainerYAML::Part &P) {
89   IO.mapRequired("Name", P.Name);
90   IO.mapRequired("Size", P.Size);
91   IO.mapOptional("Program", P.Program);
92   IO.mapOptional("Flags", P.Flags);
93   IO.mapOptional("Hash", P.Hash);
94 }
95 
96 void MappingTraits<DXContainerYAML::Object>::mapping(
97     IO &IO, DXContainerYAML::Object &Obj) {
98   IO.mapTag("!dxcontainer", true);
99   IO.mapRequired("Header", Obj.Header);
100   IO.mapRequired("Parts", Obj.Parts);
101 }
102 
103 } // namespace yaml
104 } // namespace llvm
105