| 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 | |
| 9 | #include "llvm/MC/DXContainerPSVInfo.h" |
| 10 | #include "llvm/BinaryFormat/DXContainer.h" |
| 11 | #include "llvm/MC/StringTableBuilder.h" |
| 12 | #include "llvm/Support/Alignment.h" |
| 13 | #include "llvm/Support/EndianStream.h" |
| 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::mcdxbc; |
| 18 | using namespace llvm::dxbc::PSV; |
| 19 | |
| 20 | static constexpr size_t npos = StringRef::npos; |
| 21 | |
| 22 | static size_t FindSequence(ArrayRef<uint32_t> Buffer, |
| 23 | ArrayRef<uint32_t> Sequence) { |
| 24 | if (Buffer.size() < Sequence.size()) |
| 25 | return npos; |
| 26 | for (size_t Idx = 0; Idx <= Buffer.size() - Sequence.size(); ++Idx) { |
| 27 | if (0 == memcmp(s1: static_cast<const void *>(&Buffer[Idx]), |
| 28 | s2: static_cast<const void *>(Sequence.begin()), |
| 29 | n: Sequence.size() * sizeof(uint32_t))) |
| 30 | return Idx; |
| 31 | } |
| 32 | return npos; |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | ProcessElementList(StringTableBuilder &StrTabBuilder, |
| 37 | SmallVectorImpl<uint32_t> &IndexBuffer, |
| 38 | SmallVectorImpl<v0::SignatureElement> &FinalElements, |
| 39 | SmallVectorImpl<StringRef> &SemanticNames, |
| 40 | ArrayRef<PSVSignatureElement> Elements) { |
| 41 | for (const auto &El : Elements) { |
| 42 | // Put the name in the string table and the name list. |
| 43 | StrTabBuilder.add(S: El.Name); |
| 44 | SemanticNames.push_back(Elt: El.Name); |
| 45 | |
| 46 | v0::SignatureElement FinalElement; |
| 47 | memset(s: &FinalElement, c: 0, n: sizeof(v0::SignatureElement)); |
| 48 | FinalElement.Rows = static_cast<uint8_t>(El.Indices.size()); |
| 49 | FinalElement.StartRow = El.StartRow; |
| 50 | FinalElement.Cols = El.Cols; |
| 51 | FinalElement.StartCol = El.StartCol; |
| 52 | FinalElement.Allocated = El.Allocated; |
| 53 | FinalElement.Kind = El.Kind; |
| 54 | FinalElement.Type = El.Type; |
| 55 | FinalElement.Mode = El.Mode; |
| 56 | FinalElement.DynamicMask = El.DynamicMask; |
| 57 | FinalElement.Stream = El.Stream; |
| 58 | |
| 59 | size_t Idx = FindSequence(Buffer: IndexBuffer, Sequence: El.Indices); |
| 60 | if (Idx == npos) { |
| 61 | FinalElement.IndicesOffset = static_cast<uint32_t>(IndexBuffer.size()); |
| 62 | llvm::append_range(C&: IndexBuffer, R: El.Indices); |
| 63 | } else |
| 64 | FinalElement.IndicesOffset = static_cast<uint32_t>(Idx); |
| 65 | FinalElements.push_back(Elt: FinalElement); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void PSVRuntimeInfo::write(raw_ostream &OS, uint32_t Version) const { |
| 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 | InfoSize = sizeof(dxbc::PSV::v2::RuntimeInfo); |
| 85 | BindingSize = sizeof(dxbc::PSV::v2::ResourceBindInfo); |
| 86 | break; |
| 87 | case 3: |
| 88 | default: |
| 89 | InfoSize = sizeof(dxbc::PSV::v3::RuntimeInfo); |
| 90 | BindingSize = sizeof(dxbc::PSV::v2::ResourceBindInfo); |
| 91 | } |
| 92 | |
| 93 | // Write the size of the info. |
| 94 | support::endian::write(os&: OS, value: InfoSize, endian: llvm::endianness::little); |
| 95 | |
| 96 | // Write the info itself. |
| 97 | OS.write(Ptr: reinterpret_cast<const char *>(&BaseData), Size: InfoSize); |
| 98 | |
| 99 | uint32_t ResourceCount = static_cast<uint32_t>(Resources.size()); |
| 100 | |
| 101 | support::endian::write(os&: OS, value: ResourceCount, endian: llvm::endianness::little); |
| 102 | if (ResourceCount > 0) |
| 103 | support::endian::write(os&: OS, value: BindingSize, endian: llvm::endianness::little); |
| 104 | |
| 105 | for (const auto &Res : Resources) |
| 106 | OS.write(Ptr: reinterpret_cast<const char *>(&Res), Size: BindingSize); |
| 107 | |
| 108 | // PSV Version 0 stops after the resource list. |
| 109 | if (Version == 0) |
| 110 | return; |
| 111 | |
| 112 | support::endian::write(os&: OS, |
| 113 | value: static_cast<uint32_t>(DXConStrTabBuilder.getSize()), |
| 114 | endian: llvm::endianness::little); |
| 115 | |
| 116 | // Write the string table. |
| 117 | DXConStrTabBuilder.write(OS); |
| 118 | |
| 119 | // Write the index table size, then table. |
| 120 | support::endian::write(os&: OS, value: static_cast<uint32_t>(IndexBuffer.size()), |
| 121 | endian: llvm::endianness::little); |
| 122 | for (auto I : IndexBuffer) |
| 123 | support::endian::write(os&: OS, value: I, endian: llvm::endianness::little); |
| 124 | |
| 125 | if (SignatureElements.size() > 0) { |
| 126 | // write the size of the signature elements. |
| 127 | support::endian::write(os&: OS, |
| 128 | value: static_cast<uint32_t>(sizeof(v0::SignatureElement)), |
| 129 | endian: llvm::endianness::little); |
| 130 | |
| 131 | // write the signature elements. |
| 132 | OS.write(Ptr: reinterpret_cast<const char *>(&SignatureElements[0]), |
| 133 | Size: SignatureElements.size() * sizeof(v0::SignatureElement)); |
| 134 | } |
| 135 | |
| 136 | for (const auto &MaskVector : OutputVectorMasks) |
| 137 | support::endian::write_array(os&: OS, values: ArrayRef<uint32_t>(MaskVector), |
| 138 | endian: llvm::endianness::little); |
| 139 | support::endian::write_array(os&: OS, values: ArrayRef<uint32_t>(PatchOrPrimMasks), |
| 140 | endian: llvm::endianness::little); |
| 141 | for (const auto &MaskVector : InputOutputMap) |
| 142 | support::endian::write_array(os&: OS, values: ArrayRef<uint32_t>(MaskVector), |
| 143 | endian: llvm::endianness::little); |
| 144 | support::endian::write_array(os&: OS, values: ArrayRef<uint32_t>(InputPatchMap), |
| 145 | endian: llvm::endianness::little); |
| 146 | support::endian::write_array(os&: OS, values: ArrayRef<uint32_t>(PatchOutputMap), |
| 147 | endian: llvm::endianness::little); |
| 148 | } |
| 149 | |
| 150 | void PSVRuntimeInfo::finalize(Triple::EnvironmentType Stage, uint32_t Version) { |
| 151 | IsFinalized = true; |
| 152 | BaseData.SigInputElements = static_cast<uint32_t>(InputElements.size()); |
| 153 | BaseData.SigOutputElements = static_cast<uint32_t>(OutputElements.size()); |
| 154 | BaseData.SigPatchOrPrimElements = |
| 155 | static_cast<uint32_t>(PatchOrPrimElements.size()); |
| 156 | |
| 157 | SmallVector<StringRef, 32> SemanticNames; |
| 158 | |
| 159 | // Build a string table and set associated offsets to be written when |
| 160 | // write() is called |
| 161 | ProcessElementList(StrTabBuilder&: DXConStrTabBuilder, IndexBuffer, FinalElements&: SignatureElements, |
| 162 | SemanticNames, Elements: InputElements); |
| 163 | ProcessElementList(StrTabBuilder&: DXConStrTabBuilder, IndexBuffer, FinalElements&: SignatureElements, |
| 164 | SemanticNames, Elements: OutputElements); |
| 165 | ProcessElementList(StrTabBuilder&: DXConStrTabBuilder, IndexBuffer, FinalElements&: SignatureElements, |
| 166 | SemanticNames, Elements: PatchOrPrimElements); |
| 167 | |
| 168 | if (Version >= 3 && !EntryName.empty()) |
| 169 | DXConStrTabBuilder.add(S: EntryName); |
| 170 | |
| 171 | DXConStrTabBuilder.finalize(); |
| 172 | for (auto ElAndName : zip(t&: SignatureElements, u&: SemanticNames)) { |
| 173 | llvm::dxbc::PSV::v0::SignatureElement &El = std::get<0>(t&: ElAndName); |
| 174 | StringRef Name = std::get<1>(t&: ElAndName); |
| 175 | El.NameOffset = static_cast<uint32_t>(DXConStrTabBuilder.getOffset(S: Name)); |
| 176 | if (sys::IsBigEndianHost) |
| 177 | El.swapBytes(); |
| 178 | } |
| 179 | |
| 180 | if (Version >= 3 && !EntryName.empty()) |
| 181 | BaseData.EntryNameOffset = |
| 182 | static_cast<uint32_t>(DXConStrTabBuilder.getOffset(S: EntryName)); |
| 183 | |
| 184 | if (!sys::IsBigEndianHost) |
| 185 | return; |
| 186 | BaseData.swapBytes(); |
| 187 | BaseData.swapBytes(Stage); |
| 188 | for (auto &Res : Resources) |
| 189 | Res.swapBytes(); |
| 190 | } |
| 191 | |
| 192 | void Signature::write(raw_ostream &OS, bool PadToDword) { |
| 193 | SmallVector<dxbc::ProgramSignatureElement> SigParams; |
| 194 | SigParams.reserve(N: Params.size()); |
| 195 | StringTableBuilder StrTabBuilder((StringTableBuilder::DWARF)); |
| 196 | |
| 197 | // Name offsets are from the start of the part. Pre-calculate the offset to |
| 198 | // the start of the string table so that it can be added to the table offset. |
| 199 | uint32_t TableStart = sizeof(dxbc::ProgramSignatureHeader) + |
| 200 | (sizeof(dxbc::ProgramSignatureElement) * Params.size()); |
| 201 | |
| 202 | for (const auto &P : Params) { |
| 203 | // zero out the data |
| 204 | dxbc::ProgramSignatureElement FinalElement; |
| 205 | memset(s: &FinalElement, c: 0, n: sizeof(dxbc::ProgramSignatureElement)); |
| 206 | FinalElement.Stream = P.Stream; |
| 207 | FinalElement.NameOffset = |
| 208 | static_cast<uint32_t>(StrTabBuilder.add(S: P.Name)) + TableStart; |
| 209 | FinalElement.Index = P.Index; |
| 210 | FinalElement.SystemValue = P.SystemValue; |
| 211 | FinalElement.CompType = P.CompType; |
| 212 | FinalElement.Register = P.Register; |
| 213 | FinalElement.Mask = P.Mask; |
| 214 | FinalElement.ExclusiveMask = P.ExclusiveMask; |
| 215 | FinalElement.MinPrecision = P.MinPrecision; |
| 216 | SigParams.push_back(Elt: FinalElement); |
| 217 | } |
| 218 | |
| 219 | StrTabBuilder.finalizeInOrder(); |
| 220 | stable_sort(Range&: SigParams, C: [&](const dxbc::ProgramSignatureElement &L, |
| 221 | const dxbc::ProgramSignatureElement R) { |
| 222 | return std::tie(args: L.Stream, args: L.Register, args: L.NameOffset) < |
| 223 | std::tie(args: R.Stream, args: R.Register, args: R.NameOffset); |
| 224 | }); |
| 225 | if (sys::IsBigEndianHost) |
| 226 | for (auto &El : SigParams) |
| 227 | El.swapBytes(); |
| 228 | |
| 229 | dxbc::ProgramSignatureHeader = {.ParamCount: static_cast<uint32_t>(Params.size()), |
| 230 | .FirstParamOffset: sizeof(dxbc::ProgramSignatureHeader)}; |
| 231 | if (sys::IsBigEndianHost) |
| 232 | Header.swapBytes(); |
| 233 | OS.write(Ptr: reinterpret_cast<const char *>(&Header), |
| 234 | Size: sizeof(dxbc::ProgramSignatureHeader)); |
| 235 | OS.write(Ptr: reinterpret_cast<const char *>(SigParams.data()), |
| 236 | Size: sizeof(dxbc::ProgramSignatureElement) * SigParams.size()); |
| 237 | StrTabBuilder.write(OS); |
| 238 | if (PadToDword) |
| 239 | OS.write_zeros( |
| 240 | NumZeros: offsetToAlignment(Value: TableStart + StrTabBuilder.getSize(), Alignment: Align(4))); |
| 241 | } |
| 242 | |