LLVM 20.0.0git
DXContainer.h
Go to the documentation of this file.
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
19#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Error.h"
24#include <array>
25#include <variant>
26
27namespace llvm {
28namespace object {
29
30namespace detail {
31template <typename T>
32std::enable_if_t<std::is_arithmetic<T>::value, void> swapBytes(T &value) {
34}
35
36template <typename T>
37std::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.
46template <typename T> struct ViewArray {
48 uint32_t Stride = sizeof(T); // size of each element in the list.
49
50 ViewArray() = default;
51 ViewArray(StringRef D, size_t S) : Data(D), Stride(S) {}
52
53 using value_type = T;
54 static constexpr uint32_t MaxStride() {
55 return static_cast<uint32_t>(sizeof(value_type));
56 }
57
58 struct iterator {
60 uint32_t Stride; // size of each element in the list.
61 const char *Current;
62
63 iterator(const ViewArray &A, const char *C)
64 : Data(A.Data), Stride(A.Stride), Current(C) {}
65 iterator(const iterator &) = default;
66
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()));
78 return Val;
79 }
80
82 if (Current < Data.end())
83 Current += Stride;
84 return *this;
85 }
86
88 iterator Tmp = *this;
89 ++*this;
90 return Tmp;
91 }
92
94 if (Current > Data.begin())
95 Current -= Stride;
96 return *this;
97 }
98
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
109 iterator begin() const { return iterator(*this, Data.begin()); }
110
111 iterator end() const { return iterator(*this, Data.end()); }
112
113 size_t size() const { return Data.size() / Stride; }
114
115 bool isEmpty() const { return Data.empty(); }
116};
117
118namespace DirectX {
120
123
124 StringRef Data;
125 uint32_t Size;
126 using InfoStruct =
127 std::variant<std::monostate, dxbc::PSV::v0::RuntimeInfo,
130 InfoStruct BasicInfo;
131 ResourceArray Resources;
132 StringRef StringTable;
133 SmallVector<uint32_t> SemanticIndexTable;
134 SigElementArray SigInputElements;
135 SigElementArray SigOutputElements;
136 SigElementArray SigPatchOrPrimElements;
137
138 std::array<ViewArray<uint32_t>, 4> OutputVectorMasks;
139 ViewArray<uint32_t> PatchOrPrimMasks;
140 std::array<ViewArray<uint32_t>, 4> InputOutputMap;
141 ViewArray<uint32_t> InputPatchMap;
142 ViewArray<uint32_t> PatchOutputMap;
143
144public:
145 PSVRuntimeInfo(StringRef D) : Data(D), Size(0) {}
146
147 // Parsing depends on the shader kind
148 Error parse(uint16_t ShaderKind);
149
150 uint32_t getSize() const { return Size; }
151 uint32_t getResourceCount() const { return Resources.size(); }
152 ResourceArray getResources() const { return Resources; }
153
155 return Size >= sizeof(dxbc::PSV::v3::RuntimeInfo)
156 ? 3
157 : (Size >= sizeof(dxbc::PSV::v2::RuntimeInfo) ? 2
158 : (Size >= sizeof(dxbc::PSV::v1::RuntimeInfo)) ? 1
159 : 0);
160 }
161
162 uint32_t getResourceStride() const { return Resources.Stride; }
163
164 const InfoStruct &getInfo() const { return BasicInfo; }
165
166 template <typename T> const T *getInfoAs() const {
167 if (const auto *P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
168 return static_cast<const T *>(P);
169 if (std::is_same<T, dxbc::PSV::v3::RuntimeInfo>::value)
170 return nullptr;
171
172 if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
173 return static_cast<const T *>(P);
174 if (std::is_same<T, dxbc::PSV::v2::RuntimeInfo>::value)
175 return nullptr;
176
177 if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
178 return static_cast<const T *>(P);
179 if (std::is_same<T, dxbc::PSV::v1::RuntimeInfo>::value)
180 return nullptr;
181
182 if (const auto *P = std::get_if<dxbc::PSV::v0::RuntimeInfo>(&BasicInfo))
183 return static_cast<const T *>(P);
184 return nullptr;
185 }
186
187 StringRef getStringTable() const { return StringTable; }
189 return SemanticIndexTable;
190 }
191
192 uint8_t getSigInputCount() const;
193 uint8_t getSigOutputCount() const;
194 uint8_t getSigPatchOrPrimCount() const;
195
196 SigElementArray getSigInputElements() const { return SigInputElements; }
197 SigElementArray getSigOutputElements() const { return SigOutputElements; }
199 return SigPatchOrPrimElements;
200 }
201
203 assert(Idx < 4);
204 return OutputVectorMasks[Idx];
205 }
206
207 ViewArray<uint32_t> getPatchOrPrimMasks() const { return PatchOrPrimMasks; }
208
210 assert(Idx < 4);
211 return InputOutputMap[Idx];
212 }
213
214 ViewArray<uint32_t> getInputPatchMap() const { return InputPatchMap; }
215 ViewArray<uint32_t> getPatchOutputMap() const { return PatchOutputMap; }
216
217 uint32_t getSigElementStride() const { return SigInputElements.Stride; }
218
219 bool usesViewID() const {
220 if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
221 return P->UsesViewID != 0;
222 return false;
223 }
224
225 uint8_t getInputVectorCount() const {
226 if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
227 return P->SigInputVectors;
228 return 0;
229 }
230
232 if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
233 return ArrayRef<uint8_t>(P->SigOutputVectors);
234 return ArrayRef<uint8_t>();
235 }
236
238 if (const auto *P = getInfoAs<dxbc::PSV::v1::RuntimeInfo>())
239 return P->GeomData.SigPatchConstOrPrimVectors;
240 return 0;
241 }
242};
243
247 StringRef StringTable;
248
249public:
251 return Parameters.begin();
252 }
253
255 return Parameters.end();
256 }
257
260 Offset < StringTableOffset + StringTable.size() &&
261 "Offset out of range.");
262 // Name offsets are from the start of the signature data, not from the start
263 // of the string table. The header encodes the start offset of the sting
264 // table, so we convert the offset here.
265 uint32_t TableOffset = Offset - StringTableOffset;
266 return StringTable.slice(TableOffset, StringTable.find('\0', TableOffset));
267 }
268
269 bool isEmpty() const { return Parameters.isEmpty(); }
270
272};
273
274} // namespace DirectX
275
277public:
278 using DXILData = std::pair<dxbc::ProgramHeader, const char *>;
279
280private:
282
283 MemoryBufferRef Data;
284 dxbc::Header Header;
285 SmallVector<uint32_t, 4> PartOffsets;
286 std::optional<DXILData> DXIL;
287 std::optional<uint64_t> ShaderFeatureFlags;
288 std::optional<dxbc::ShaderHash> Hash;
289 std::optional<DirectX::PSVRuntimeInfo> PSVInfo;
290 DirectX::Signature InputSignature;
291 DirectX::Signature OutputSignature;
292 DirectX::Signature PatchConstantSignature;
293
294 Error parseHeader();
295 Error parsePartOffsets();
296 Error parseDXILHeader(StringRef Part);
297 Error parseShaderFeatureFlags(StringRef Part);
298 Error parseHash(StringRef Part);
299 Error parsePSVInfo(StringRef Part);
300 Error parseSignature(StringRef Part, DirectX::Signature &Array);
301 friend class PartIterator;
302
303public:
304 // The PartIterator is a wrapper around the iterator for the PartOffsets
305 // member of the DXContainer. It contains a refernce to the container, and the
306 // current iterator value, as well as storage for a parsed part header.
308 const DXContainer &Container;
310 struct PartData {
311 dxbc::PartHeader Part;
312 uint32_t Offset;
313 StringRef Data;
314 } IteratorState;
315
316 friend class DXContainer;
317
320 : Container(C), OffsetIt(It) {
321 if (OffsetIt == Container.PartOffsets.end())
322 updateIteratorImpl(Container.PartOffsets.back());
323 else
324 updateIterator();
325 }
326
327 // Updates the iterator's state data. This results in copying the part
328 // header into the iterator and handling any required byte swapping. This is
329 // called when incrementing or decrementing the iterator.
330 void updateIterator() {
331 if (OffsetIt != Container.PartOffsets.end())
332 updateIteratorImpl(*OffsetIt);
333 }
334
335 // Implementation for updating the iterator state based on a specified
336 // offest.
337 void updateIteratorImpl(const uint32_t Offset);
338
339 public:
341 if (OffsetIt == Container.PartOffsets.end())
342 return *this;
343 ++OffsetIt;
344 updateIterator();
345 return *this;
346 }
347
349 PartIterator Tmp = *this;
350 ++(*this);
351 return Tmp;
352 }
353
354 bool operator==(const PartIterator &RHS) const {
355 return OffsetIt == RHS.OffsetIt;
356 }
357
358 bool operator!=(const PartIterator &RHS) const {
359 return OffsetIt != RHS.OffsetIt;
360 }
361
362 const PartData &operator*() { return IteratorState; }
363 const PartData *operator->() { return &IteratorState; }
364 };
365
367 return PartIterator(*this, PartOffsets.begin());
368 }
369
370 PartIterator end() const { return PartIterator(*this, PartOffsets.end()); }
371
372 StringRef getData() const { return Data.getBuffer(); }
374
375 const dxbc::Header &getHeader() const { return Header; }
376
377 const std::optional<DXILData> &getDXIL() const { return DXIL; }
378
379 std::optional<uint64_t> getShaderFeatureFlags() const {
380 return ShaderFeatureFlags;
381 }
382
383 std::optional<dxbc::ShaderHash> getShaderHash() const { return Hash; }
384
385 const std::optional<DirectX::PSVRuntimeInfo> &getPSVInfo() const {
386 return PSVInfo;
387 };
388
389 const DirectX::Signature &getInputSignature() const { return InputSignature; }
391 return OutputSignature;
392 }
394 return PatchConstantSignature;
395 }
396};
397
398} // namespace object
399} // namespace llvm
400
401#endif // LLVM_OBJECT_DXCONTAINER_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live value
#define I(x, y, z)
Definition: MD5.cpp:58
#define T
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
Value * RHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:591
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
iterator begin() const
Definition: StringRef.h:111
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
iterator end() const
Definition: StringRef.h:113
bool operator!=(const PartIterator &RHS) const
Definition: DXContainer.h:358
bool operator==(const PartIterator &RHS) const
Definition: DXContainer.h:354
PartIterator end() const
Definition: DXContainer.h:370
std::optional< uint64_t > getShaderFeatureFlags() const
Definition: DXContainer.h:379
const std::optional< DXILData > & getDXIL() const
Definition: DXContainer.h:377
const std::optional< DirectX::PSVRuntimeInfo > & getPSVInfo() const
Definition: DXContainer.h:385
const dxbc::Header & getHeader() const
Definition: DXContainer.h:375
const DirectX::Signature & getInputSignature() const
Definition: DXContainer.h:389
std::pair< dxbc::ProgramHeader, const char * > DXILData
Definition: DXContainer.h:278
StringRef getData() const
Definition: DXContainer.h:372
PartIterator begin() const
Definition: DXContainer.h:366
static Expected< DXContainer > create(MemoryBufferRef Object)
std::optional< dxbc::ShaderHash > getShaderHash() const
Definition: DXContainer.h:383
const DirectX::Signature & getOutputSignature() const
Definition: DXContainer.h:390
const DirectX::Signature & getPatchConstantSignature() const
Definition: DXContainer.h:393
ArrayRef< uint8_t > getOutputVectorCounts() const
Definition: DXContainer.h:231
ArrayRef< uint32_t > getSemanticIndexTable() const
Definition: DXContainer.h:188
ResourceArray getResources() const
Definition: DXContainer.h:152
ViewArray< uint32_t > getInputOutputMap(size_t Idx) const
Definition: DXContainer.h:209
uint8_t getPatchConstOrPrimVectorCount() const
Definition: DXContainer.h:237
ViewArray< uint32_t > getPatchOrPrimMasks() const
Definition: DXContainer.h:207
ViewArray< uint32_t > getInputPatchMap() const
Definition: DXContainer.h:214
ViewArray< uint32_t > getOutputVectorMasks(size_t Idx) const
Definition: DXContainer.h:202
SigElementArray getSigPatchOrPrimElements() const
Definition: DXContainer.h:198
SigElementArray getSigInputElements() const
Definition: DXContainer.h:196
SigElementArray getSigOutputElements() const
Definition: DXContainer.h:197
const InfoStruct & getInfo() const
Definition: DXContainer.h:164
ViewArray< uint32_t > getPatchOutputMap() const
Definition: DXContainer.h:215
ViewArray< dxbc::ProgramSignatureElement >::iterator begin() const
Definition: DXContainer.h:250
Error initialize(StringRef Part)
ViewArray< dxbc::ProgramSignatureElement >::iterator end() const
Definition: DXContainer.h:254
StringRef getName(uint32_t Offset) const
Definition: DXContainer.h:258
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
std::enable_if_t< std::is_arithmetic< T >::value, void > swapBytes(T &value)
Definition: DXContainer.h:32
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:26
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:61
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
Use this type to describe the size and type of a DXIL container part.
Definition: DXContainer.h:93
bool operator!=(const iterator I)
Definition: DXContainer.h:106
iterator(const ViewArray &A, const char *C)
Definition: DXContainer.h:63
bool operator==(const iterator I)
Definition: DXContainer.h:105
iterator(const iterator &)=default
static constexpr uint32_t MaxStride()
Definition: DXContainer.h:54
iterator end() const
Definition: DXContainer.h:111
ViewArray(StringRef D, size_t S)
Definition: DXContainer.h:51
size_t size() const
Definition: DXContainer.h:113
iterator begin() const
Definition: DXContainer.h:109
Definition: regcomp.c:192