1 //===- DXContainer.h - DXContainer file implementation ----------*- 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 // This file declares the DXContainerFile class, which implements the ObjectFile
10 // interface for DXContainer files.
11 //
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_OBJECT_DXCONTAINER_H
16 #define LLVM_OBJECT_DXCONTAINER_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/BinaryFormat/DXContainer.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/MemoryBufferRef.h"
23 #include "llvm/TargetParser/Triple.h"
24 #include <array>
25 #include <variant>
26 
27 namespace llvm {
28 namespace object {
29 
30 namespace detail {
31 template <typename T>
swapBytes(T & value)32 std::enable_if_t<std::is_arithmetic<T>::value, void> swapBytes(T &value) {
33   sys::swapByteOrder(value);
34 }
35 
36 template <typename T>
swapBytes(T & value)37 std::enable_if_t<std::is_class<T>::value, void> swapBytes(T &value) {
38   value.swapBytes();
39 }
40 } // namespace detail
41 
42 // This class provides a view into the underlying resource array. The Resource
43 // data is little-endian encoded and may not be properly aligned to read
44 // directly from. The dereference operator creates a copy of the data and byte
45 // swaps it as appropriate.
46 template <typename T> struct ViewArray {
47   StringRef Data;
48   uint32_t Stride = sizeof(T); // size of each element in the list.
49 
50   ViewArray() = default;
ViewArrayViewArray51   ViewArray(StringRef D, size_t S) : Data(D), Stride(S) {}
52 
53   using value_type = T;
MaxStrideViewArray54   static constexpr uint32_t MaxStride() {
55     return static_cast<uint32_t>(sizeof(value_type));
56   }
57 
58   struct iterator {
59     StringRef Data;
60     uint32_t Stride; // size of each element in the list.
61     const char *Current;
62 
iteratorViewArray::iterator63     iterator(const ViewArray &A, const char *C)
64         : Data(A.Data), Stride(A.Stride), Current(C) {}
65     iterator(const iterator &) = default;
66 
67     value_type operator*() {
68       // Explicitly zero the structure so that unused fields are zeroed. It is
69       // up to the user to know if the fields are used by verifying the PSV
70       // version.
71       value_type Val;
72       std::memset(&Val, 0, sizeof(value_type));
73       if (Current >= Data.end())
74         return Val;
75       memcpy(static_cast<void *>(&Val), Current, std::min(Stride, MaxStride()));
76       if (sys::IsBigEndianHost)
77         detail::swapBytes(Val);
78       return Val;
79     }
80 
81     iterator operator++() {
82       if (Current < Data.end())
83         Current += Stride;
84       return *this;
85     }
86 
87     iterator operator++(int) {
88       iterator Tmp = *this;
89       ++*this;
90       return Tmp;
91     }
92 
93     iterator operator--() {
94       if (Current > Data.begin())
95         Current -= Stride;
96       return *this;
97     }
98 
99     iterator operator--(int) {
100       iterator Tmp = *this;
101       --*this;
102       return Tmp;
103     }
104 
105     bool operator==(const iterator I) { return I.Current == Current; }
106     bool operator!=(const iterator I) { return !(*this == I); }
107   };
108 
beginViewArray109   iterator begin() const { return iterator(*this, Data.begin()); }
110 
endViewArray111   iterator end() const { return iterator(*this, Data.end()); }
112 
sizeViewArray113   size_t size() const { return Data.size() / Stride; }
114 
isEmptyViewArray115   bool isEmpty() const { return Data.empty(); }
116 };
117 
118 namespace DirectX {
119 class PSVRuntimeInfo {
120 
121   using ResourceArray = ViewArray<dxbc::PSV::v2::ResourceBindInfo>;
122   using SigElementArray = ViewArray<dxbc::PSV::v0::SignatureElement>;
123 
124   StringRef Data;
125   uint32_t Size;
126   using InfoStruct =
127       std::variant<std::monostate, dxbc::PSV::v0::RuntimeInfo,
128                    dxbc::PSV::v1::RuntimeInfo, dxbc::PSV::v2::RuntimeInfo>;
129   InfoStruct BasicInfo;
130   ResourceArray Resources;
131   StringRef StringTable;
132   SmallVector<uint32_t> SemanticIndexTable;
133   SigElementArray SigInputElements;
134   SigElementArray SigOutputElements;
135   SigElementArray SigPatchOrPrimElements;
136 
137   std::array<ViewArray<uint32_t>, 4> OutputVectorMasks;
138   ViewArray<uint32_t> PatchOrPrimMasks;
139   std::array<ViewArray<uint32_t>, 4> InputOutputMap;
140   ViewArray<uint32_t> InputPatchMap;
141   ViewArray<uint32_t> PatchOutputMap;
142 
143 public:
PSVRuntimeInfo(StringRef D)144   PSVRuntimeInfo(StringRef D) : Data(D), Size(0) {}
145 
146   // Parsing depends on the shader kind
147   Error parse(uint16_t ShaderKind);
148 
getSize()149   uint32_t getSize() const { return Size; }
getResourceCount()150   uint32_t getResourceCount() const { return Resources.size(); }
getResources()151   ResourceArray getResources() const { return Resources; }
152 
getVersion()153   uint32_t getVersion() const {
154     return Size >= sizeof(dxbc::PSV::v2::RuntimeInfo)
155                ? 2
156                : (Size >= sizeof(dxbc::PSV::v1::RuntimeInfo) ? 1 : 0);
157   }
158 
getResourceStride()159   uint32_t getResourceStride() const { return Resources.Stride; }
160 
getInfo()161   const InfoStruct &getInfo() const { return BasicInfo; }
162 
getInfoAs()163   template <typename T> const T *getInfoAs() const {
164     if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
165       return static_cast<const T *>(P);
166     if (std::is_same<T, dxbc::PSV::v2::RuntimeInfo>::value)
167       return nullptr;
168 
169     if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
170       return static_cast<const T *>(P);
171     if (std::is_same<T, dxbc::PSV::v1::RuntimeInfo>::value)
172       return nullptr;
173 
174     if (const auto *P = std::get_if<dxbc::PSV::v0::RuntimeInfo>(&BasicInfo))
175       return static_cast<const T *>(P);
176     return nullptr;
177   }
178 
getStringTable()179   StringRef getStringTable() const { return StringTable; }
getSemanticIndexTable()180   ArrayRef<uint32_t> getSemanticIndexTable() const {
181     return SemanticIndexTable;
182   }
183 
184   uint8_t getSigInputCount() const;
185   uint8_t getSigOutputCount() const;
186   uint8_t getSigPatchOrPrimCount() const;
187 
getSigInputElements()188   SigElementArray getSigInputElements() const { return SigInputElements; }
getSigOutputElements()189   SigElementArray getSigOutputElements() const { return SigOutputElements; }
getSigPatchOrPrimElements()190   SigElementArray getSigPatchOrPrimElements() const {
191     return SigPatchOrPrimElements;
192   }
193 
getOutputVectorMasks(size_t Idx)194   ViewArray<uint32_t> getOutputVectorMasks(size_t Idx) const {
195     assert(Idx < 4);
196     return OutputVectorMasks[Idx];
197   }
198 
getPatchOrPrimMasks()199   ViewArray<uint32_t> getPatchOrPrimMasks() const { return PatchOrPrimMasks; }
200 
getInputOutputMap(size_t Idx)201   ViewArray<uint32_t> getInputOutputMap(size_t Idx) const {
202     assert(Idx < 4);
203     return InputOutputMap[Idx];
204   }
205 
getInputPatchMap()206   ViewArray<uint32_t> getInputPatchMap() const { return InputPatchMap; }
getPatchOutputMap()207   ViewArray<uint32_t> getPatchOutputMap() const { return PatchOutputMap; }
208 
getSigElementStride()209   uint32_t getSigElementStride() const { return SigInputElements.Stride; }
210 
usesViewID()211   bool usesViewID() const {
212     if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
213       return P->UsesViewID != 0;
214     return false;
215   }
216 
getInputVectorCount()217   uint8_t getInputVectorCount() const {
218     if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
219       return P->SigInputVectors;
220     return 0;
221   }
222 
getOutputVectorCounts()223   ArrayRef<uint8_t> getOutputVectorCounts() const {
224     if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
225       return ArrayRef<uint8_t>(P->SigOutputVectors);
226     return ArrayRef<uint8_t>();
227   }
228 
getPatchConstOrPrimVectorCount()229   uint8_t getPatchConstOrPrimVectorCount() const {
230     if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
231       return P->GeomData.SigPatchConstOrPrimVectors;
232     return 0;
233   }
234 };
235 
236 class Signature {
237   ViewArray<dxbc::ProgramSignatureElement> Parameters;
238   uint32_t StringTableOffset;
239   StringRef StringTable;
240 
241 public:
begin()242   ViewArray<dxbc::ProgramSignatureElement>::iterator begin() const {
243     return Parameters.begin();
244   }
245 
end()246   ViewArray<dxbc::ProgramSignatureElement>::iterator end() const {
247     return Parameters.end();
248   }
249 
getName(uint32_t Offset)250   StringRef getName(uint32_t Offset) const {
251     assert(Offset >= StringTableOffset &&
252            Offset < StringTableOffset + StringTable.size() &&
253            "Offset out of range.");
254     // Name offsets are from the start of the signature data, not from the start
255     // of the string table. The header encodes the start offset of the sting
256     // table, so we convert the offset here.
257     uint32_t TableOffset = Offset - StringTableOffset;
258     return StringTable.slice(TableOffset, StringTable.find('\0', TableOffset));
259   }
260 
isEmpty()261   bool isEmpty() const { return Parameters.isEmpty(); }
262 
263   Error initialize(StringRef Part);
264 };
265 
266 } // namespace DirectX
267 
268 class DXContainer {
269 public:
270   using DXILData = std::pair<dxbc::ProgramHeader, const char *>;
271 
272 private:
273   DXContainer(MemoryBufferRef O);
274 
275   MemoryBufferRef Data;
276   dxbc::Header Header;
277   SmallVector<uint32_t, 4> PartOffsets;
278   std::optional<DXILData> DXIL;
279   std::optional<uint64_t> ShaderFlags;
280   std::optional<dxbc::ShaderHash> Hash;
281   std::optional<DirectX::PSVRuntimeInfo> PSVInfo;
282   DirectX::Signature InputSignature;
283   DirectX::Signature OutputSignature;
284   DirectX::Signature PatchConstantSignature;
285 
286   Error parseHeader();
287   Error parsePartOffsets();
288   Error parseDXILHeader(StringRef Part);
289   Error parseShaderFlags(StringRef Part);
290   Error parseHash(StringRef Part);
291   Error parsePSVInfo(StringRef Part);
292   Error parseSignature(StringRef Part, DirectX::Signature &Array);
293   friend class PartIterator;
294 
295 public:
296   // The PartIterator is a wrapper around the iterator for the PartOffsets
297   // member of the DXContainer. It contains a refernce to the container, and the
298   // current iterator value, as well as storage for a parsed part header.
299   class PartIterator {
300     const DXContainer &Container;
301     SmallVectorImpl<uint32_t>::const_iterator OffsetIt;
302     struct PartData {
303       dxbc::PartHeader Part;
304       uint32_t Offset;
305       StringRef Data;
306     } IteratorState;
307 
308     friend class DXContainer;
309 
PartIterator(const DXContainer & C,SmallVectorImpl<uint32_t>::const_iterator It)310     PartIterator(const DXContainer &C,
311                  SmallVectorImpl<uint32_t>::const_iterator It)
312         : Container(C), OffsetIt(It) {
313       if (OffsetIt == Container.PartOffsets.end())
314         updateIteratorImpl(Container.PartOffsets.back());
315       else
316         updateIterator();
317     }
318 
319     // Updates the iterator's state data. This results in copying the part
320     // header into the iterator and handling any required byte swapping. This is
321     // called when incrementing or decrementing the iterator.
updateIterator()322     void updateIterator() {
323       if (OffsetIt != Container.PartOffsets.end())
324         updateIteratorImpl(*OffsetIt);
325     }
326 
327     // Implementation for updating the iterator state based on a specified
328     // offest.
329     void updateIteratorImpl(const uint32_t Offset);
330 
331   public:
332     PartIterator &operator++() {
333       if (OffsetIt == Container.PartOffsets.end())
334         return *this;
335       ++OffsetIt;
336       updateIterator();
337       return *this;
338     }
339 
340     PartIterator operator++(int) {
341       PartIterator Tmp = *this;
342       ++(*this);
343       return Tmp;
344     }
345 
346     bool operator==(const PartIterator &RHS) const {
347       return OffsetIt == RHS.OffsetIt;
348     }
349 
350     bool operator!=(const PartIterator &RHS) const {
351       return OffsetIt != RHS.OffsetIt;
352     }
353 
354     const PartData &operator*() { return IteratorState; }
355     const PartData *operator->() { return &IteratorState; }
356   };
357 
begin()358   PartIterator begin() const {
359     return PartIterator(*this, PartOffsets.begin());
360   }
361 
end()362   PartIterator end() const { return PartIterator(*this, PartOffsets.end()); }
363 
getData()364   StringRef getData() const { return Data.getBuffer(); }
365   static Expected<DXContainer> create(MemoryBufferRef Object);
366 
getHeader()367   const dxbc::Header &getHeader() const { return Header; }
368 
getDXIL()369   const std::optional<DXILData> &getDXIL() const { return DXIL; }
370 
getShaderFlags()371   std::optional<uint64_t> getShaderFlags() const { return ShaderFlags; }
372 
getShaderHash()373   std::optional<dxbc::ShaderHash> getShaderHash() const { return Hash; }
374 
getPSVInfo()375   const std::optional<DirectX::PSVRuntimeInfo> &getPSVInfo() const {
376     return PSVInfo;
377   };
378 
getInputSignature()379   const DirectX::Signature &getInputSignature() const { return InputSignature; }
getOutputSignature()380   const DirectX::Signature &getOutputSignature() const {
381     return OutputSignature;
382   }
getPatchConstantSignature()383   const DirectX::Signature &getPatchConstantSignature() const {
384     return PatchConstantSignature;
385   }
386 };
387 
388 } // namespace object
389 } // namespace llvm
390 
391 #endif // LLVM_OBJECT_DXCONTAINER_H
392