LLVM 23.0.0git
TpiStreamBuilder.cpp
Go to the documentation of this file.
1//===- TpiStreamBuilder.cpp - -------------------------------------------===//
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
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/STLExtras.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/Error.h"
23#include <cstdint>
24#include <numeric>
25
26using namespace llvm;
27using namespace llvm::msf;
28using namespace llvm::pdb;
29using namespace llvm::support;
30
32 : Msf(Msf), Allocator(Msf.getAllocator()), Header(nullptr), Idx(StreamIdx) {
33}
34
36
40
41void TpiStreamBuilder::updateTypeIndexOffsets(ArrayRef<uint16_t> Sizes) {
42 // If we just crossed an 8KB threshold, add a type index offset.
43 for (uint16_t Size : Sizes) {
44 size_t NewSize = TypeRecordBytes + Size;
45 constexpr size_t EightKB = 8 * 1024;
46 if (NewSize / EightKB > TypeRecordBytes / EightKB || TypeRecordCount == 0) {
47 TypeIndexOffsets.push_back(
49 TypeRecordCount),
50 ulittle32_t(TypeRecordBytes)});
51 }
52 ++TypeRecordCount;
53 TypeRecordBytes = NewSize;
54 }
55}
56
58 assert(((Record.size() & 3) == 0) &&
59 "The type record's size is not a multiple of 4 bytes which will "
60 "cause misalignment in the output TPI stream!");
62 uint16_t OneSize = (uint16_t)Record.size();
63 updateTypeIndexOffsets(ArrayRef(&OneSize, 1));
64
65 TypeRecBuffers.push_back(Record);
66 TypeHashes.push_back(Hash);
67}
68
71 ArrayRef<uint32_t> Hashes) {
72 // Ignore empty type buffers. There should be no hashes or sizes in this case.
73 if (Types.empty()) {
74 assert(Sizes.empty() && Hashes.empty());
75 return;
76 }
77
78 assert(((Types.size() & 3) == 0) &&
79 "The type record's size is not a multiple of 4 bytes which will "
80 "cause misalignment in the output TPI stream!");
81 assert(Sizes.size() == Hashes.size() && "sizes and hashes should be in sync");
82 assert(std::accumulate(Sizes.begin(), Sizes.end(), 0U) == Types.size() &&
83 "sizes of type records should sum to the size of the types");
84 updateTypeIndexOffsets(Sizes);
85
86 TypeRecBuffers.push_back(Types);
87 llvm::append_range(TypeHashes, Hashes);
88}
89
90Error TpiStreamBuilder::finalize() {
91 if (Header)
92 return Error::success();
93
94 TpiStreamHeader *H = Allocator.Allocate<TpiStreamHeader>();
95
96 H->Version = VerHeader;
97 H->HeaderSize = sizeof(TpiStreamHeader);
99 H->TypeIndexEnd = H->TypeIndexBegin + TypeRecordCount;
100 H->TypeRecordBytes = TypeRecordBytes;
101
102 H->HashStreamIndex = HashStreamIndex;
103 H->HashAuxStreamIndex = kInvalidStreamIndex;
104 H->HashKeySize = sizeof(ulittle32_t);
105 H->NumHashBuckets = MaxTpiHashBuckets - 1;
106
107 // Recall that hash values go into a completely different stream identified by
108 // the `HashStreamIndex` field of the `TpiStreamHeader`. Therefore, the data
109 // begins at offset 0 of this independent stream.
110 H->HashValueBuffer.Off = 0;
111 H->HashValueBuffer.Length = calculateHashBufferSize();
112
113 // We never write any adjustments into our PDBs, so this is usually some
114 // offset with zero length.
115 H->HashAdjBuffer.Off = H->HashValueBuffer.Off + H->HashValueBuffer.Length;
116 H->HashAdjBuffer.Length = 0;
117
118 H->IndexOffsetBuffer.Off = H->HashAdjBuffer.Off + H->HashAdjBuffer.Length;
119 H->IndexOffsetBuffer.Length = calculateIndexOffsetSize();
120
121 Header = H;
122 return Error::success();
123}
124
126 return sizeof(TpiStreamHeader) + TypeRecordBytes;
127}
128
129uint32_t TpiStreamBuilder::calculateHashBufferSize() const {
130 assert((TypeRecordCount == TypeHashes.size() || TypeHashes.empty()) &&
131 "either all or no type records should have hashes");
132 return TypeHashes.size() * sizeof(ulittle32_t);
133}
134
135uint32_t TpiStreamBuilder::calculateIndexOffsetSize() const {
136 return TypeIndexOffsets.size() * sizeof(codeview::TypeIndexOffset);
137}
138
141 if (auto EC = Msf.setStreamSize(Idx, Length))
142 return EC;
143
144 uint32_t HashStreamSize =
145 calculateHashBufferSize() + calculateIndexOffsetSize();
146
147 if (HashStreamSize == 0)
148 return Error::success();
149
150 auto ExpectedIndex = Msf.addStream(HashStreamSize);
151 if (!ExpectedIndex)
152 return ExpectedIndex.takeError();
153 HashStreamIndex = *ExpectedIndex;
154 if (!TypeHashes.empty()) {
155 ulittle32_t *H = Allocator.Allocate<ulittle32_t>(TypeHashes.size());
156 MutableArrayRef<ulittle32_t> HashBuffer(H, TypeHashes.size());
157 for (uint32_t I = 0; I < TypeHashes.size(); ++I) {
158 HashBuffer[I] = TypeHashes[I] % (MaxTpiHashBuckets - 1);
159 }
160 ArrayRef<uint8_t> Bytes(
161 reinterpret_cast<const uint8_t *>(HashBuffer.data()),
162 calculateHashBufferSize());
163 HashValueStream =
164 std::make_unique<BinaryByteStream>(Bytes, llvm::endianness::little);
165 }
166 return Error::success();
167}
168
171 llvm::TimeTraceScope timeScope("Commit TPI stream");
172 if (auto EC = finalize())
173 return EC;
174
175 auto InfoS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer,
176 Idx, Allocator);
177
178 BinaryStreamWriter Writer(*InfoS);
179 if (auto EC = Writer.writeObject(*Header))
180 return EC;
181
182 for (auto Rec : TypeRecBuffers) {
183 assert(!Rec.empty() && "Attempting to write an empty type record shifts "
184 "all offsets in the TPI stream!");
185 assert(((Rec.size() & 3) == 0) &&
186 "The type record's size is not a multiple of 4 bytes which will "
187 "cause misalignment in the output TPI stream!");
188 if (auto EC = Writer.writeBytes(Rec))
189 return EC;
190 }
191
192 if (HashStreamIndex != kInvalidStreamIndex) {
194 Layout, Buffer, HashStreamIndex, Allocator);
195 BinaryStreamWriter HW(*HVS);
196 if (HashValueStream) {
197 if (auto EC = HW.writeStreamRef(*HashValueStream))
198 return EC;
199 }
200
201 for (auto &IndexOffset : TypeIndexOffsets) {
202 if (auto EC = HW.writeObject(IndexOffset))
203 return EC;
204 }
205 }
206
207 return Error::success();
208}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
arc branch finalize
This file defines the BumpPtrAllocator interface.
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
This file contains some templates that are useful if you are working with the STL at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
Provides write only access to a subclass of WritableBinaryStream.
LLVM_ABI Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
LLVM_ABI Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
A 32-bit type reference.
Definition TypeIndex.h:97
static const uint32_t FirstNonSimpleIndex
Definition TypeIndex.h:99
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
LLVM_ABI void addTypeRecord(ArrayRef< uint8_t > Type, uint32_t Hash)
LLVM_ABI Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef Buffer)
LLVM_ABI void addTypeRecords(ArrayRef< uint8_t > Types, ArrayRef< uint16_t > Sizes, ArrayRef< uint32_t > Hashes)
LLVM_ABI TpiStreamBuilder(msf::MSFBuilder &Msf, uint32_t StreamIdx)
LLVM_ABI uint32_t calculateSerializedLength()
LLVM_ABI void setVersionHeader(PdbRaw_TpiVer Version)
const uint16_t kInvalidStreamIndex
const uint32_t MaxTpiHashBuckets
Definition RawTypes.h:301
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition Endian.h:290
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Length
Definition DWP.cpp:532
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
ArrayRef(const T &OneElt) -> ArrayRef< T >