LLVM 18.0.0git
DXContainerPSVInfo.cpp
Go to the documentation of this file.
1//===- llvm/MC/DXContainerPSVInfo.cpp - DXContainer PSVInfo -----*- 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
14
15using namespace llvm;
16using namespace llvm::mcdxbc;
17using namespace llvm::dxbc::PSV;
18
19static constexpr size_t npos = StringRef::npos;
20
21static size_t FindSequence(ArrayRef<uint32_t> Buffer,
22 ArrayRef<uint32_t> Sequence) {
23 if (Buffer.size() < Sequence.size())
24 return npos;
25 for (size_t Idx = 0; Idx <= Buffer.size() - Sequence.size(); ++Idx) {
26 if (0 == memcmp(static_cast<const void *>(&Buffer[Idx]),
27 static_cast<const void *>(Sequence.begin()),
28 Sequence.size() * sizeof(uint32_t)))
29 return Idx;
30 }
31 return npos;
32}
33
34static void
36 SmallVectorImpl<uint32_t> &IndexBuffer,
38 SmallVectorImpl<StringRef> &SemanticNames,
40 for (const auto &El : Elements) {
41 // Put the name in the string table and the name list.
42 StrTabBuilder.add(El.Name);
43 SemanticNames.push_back(El.Name);
44
45 v0::SignatureElement FinalElement;
46 memset(&FinalElement, 0, sizeof(v0::SignatureElement));
47 FinalElement.Rows = static_cast<uint8_t>(El.Indices.size());
48 FinalElement.StartRow = El.StartRow;
49 FinalElement.Cols = El.Cols;
50 FinalElement.StartCol = El.StartCol;
51 FinalElement.Allocated = El.Allocated;
52 FinalElement.Kind = El.Kind;
53 FinalElement.Type = El.Type;
54 FinalElement.Mode = El.Mode;
55 FinalElement.DynamicMask = El.DynamicMask;
56 FinalElement.Stream = El.Stream;
57
58 size_t Idx = FindSequence(IndexBuffer, El.Indices);
59 if (Idx == npos) {
60 FinalElement.IndicesOffset = static_cast<uint32_t>(IndexBuffer.size());
61 IndexBuffer.insert(IndexBuffer.end(), El.Indices.begin(),
62 El.Indices.end());
63 } else
64 FinalElement.IndicesOffset = static_cast<uint32_t>(Idx);
65 FinalElements.push_back(FinalElement);
66 }
67}
68
70 assert(IsFinalized && "finalize must be called before write");
71
72 uint32_t InfoSize;
73 uint32_t BindingSize;
74 switch (Version) {
75 case 0:
76 InfoSize = sizeof(dxbc::PSV::v0::RuntimeInfo);
77 BindingSize = sizeof(dxbc::PSV::v0::ResourceBindInfo);
78 break;
79 case 1:
80 InfoSize = sizeof(dxbc::PSV::v1::RuntimeInfo);
81 BindingSize = sizeof(dxbc::PSV::v0::ResourceBindInfo);
82 break;
83 case 2:
84 default:
85 InfoSize = sizeof(dxbc::PSV::v2::RuntimeInfo);
86 BindingSize = sizeof(dxbc::PSV::v2::ResourceBindInfo);
87 }
88 // Write the size of the info.
89
91 // Write the info itself.
92 OS.write(reinterpret_cast<const char *>(&BaseData), InfoSize);
93
94 uint32_t ResourceCount = static_cast<uint32_t>(Resources.size());
95
97 if (ResourceCount > 0)
99
100 for (const auto &Res : Resources)
101 OS.write(reinterpret_cast<const char *>(&Res), BindingSize);
102
103 // PSV Version 0 stops after the resource list.
104 if (Version == 0)
105 return;
106
108 SmallVector<uint32_t, 64> IndexBuffer;
110 SmallVector<StringRef, 32> SemanticNames;
111
112 ProcessElementList(StrTabBuilder, IndexBuffer, SignatureElements,
113 SemanticNames, InputElements);
114 ProcessElementList(StrTabBuilder, IndexBuffer, SignatureElements,
115 SemanticNames, OutputElements);
116 ProcessElementList(StrTabBuilder, IndexBuffer, SignatureElements,
117 SemanticNames, PatchOrPrimElements);
118
119 StrTabBuilder.finalize();
120 for (auto ElAndName : zip(SignatureElements, SemanticNames)) {
121 v0::SignatureElement &El = std::get<0>(ElAndName);
122 StringRef Name = std::get<1>(ElAndName);
123 El.NameOffset = static_cast<uint32_t>(StrTabBuilder.getOffset(Name));
125 El.swapBytes();
126 }
127
128 support::endian::write(OS, static_cast<uint32_t>(StrTabBuilder.getSize()),
130
131 // Write the string table.
132 StrTabBuilder.write(OS);
133
134 // Write the index table size, then table.
135 support::endian::write(OS, static_cast<uint32_t>(IndexBuffer.size()),
137 for (auto I : IndexBuffer)
139
140 if (SignatureElements.size() > 0) {
141 // write the size of the signature elements.
143 static_cast<uint32_t>(sizeof(v0::SignatureElement)),
145
146 // write the signature elements.
147 OS.write(reinterpret_cast<const char *>(&SignatureElements[0]),
148 SignatureElements.size() * sizeof(v0::SignatureElement));
149 }
150
151 for (const auto &MaskVector : OutputVectorMasks)
156 for (const auto &MaskVector : InputOutputMap)
163}
static constexpr size_t npos
static void ProcessElementList(StringTableBuilder &StrTabBuilder, SmallVectorImpl< uint32_t > &IndexBuffer, SmallVectorImpl< v0::SignatureElement > &FinalElements, SmallVectorImpl< StringRef > &SemanticNames, ArrayRef< PSVSignatureElement > Elements)
static size_t FindSequence(ArrayRef< uint32_t > Buffer, ArrayRef< uint32_t > Sequence)
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
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:911
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:809
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static constexpr size_t npos
Definition: StringRef.h:52
Utility for building string tables with deduplicated suffixes.
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
void write(raw_ostream &OS) const
size_t add(CachedHashStringRef S)
Add a string to the builder.
void finalize()
Analyze the strings and build the final table.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write(unsigned char C)
void write_array(raw_ostream &os, ArrayRef< value_type > values, endianness endian)
Definition: EndianStream.h:29
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:96
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:54
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition: STLExtras.h:863
SmallVector< uint32_t > PatchOrPrimMasks
SmallVector< uint32_t > PatchOutputMap
SmallVector< dxbc::PSV::v2::ResourceBindInfo > Resources
SmallVector< PSVSignatureElement > InputElements
SmallVector< uint32_t > InputPatchMap
SmallVector< PSVSignatureElement > OutputElements
SmallVector< PSVSignatureElement > PatchOrPrimElements
void write(raw_ostream &OS, uint32_t Version=std::numeric_limits< uint32_t >::max()) const
dxbc::PSV::v2::RuntimeInfo BaseData
std::array< SmallVector< uint32_t >, 4 > OutputVectorMasks
std::array< SmallVector< uint32_t >, 4 > InputOutputMap