LLVM 23.0.0git
LazyRandomTypeCollection.cpp
Go to the documentation of this file.
1//===- LazyRandomTypeCollection.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"
12#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/Error.h"
17#include <algorithm>
18#include <cassert>
19#include <cstdint>
20#include <iterator>
21
22using namespace llvm;
23using namespace llvm::codeview;
24
25static void error(Error &&EC) {
26 assert(!static_cast<bool>(EC));
27 if (EC)
28 consumeError(std::move(EC));
29}
30
32 : LazyRandomTypeCollection(CVTypeArray(), RecordCountHint,
33 PartialOffsetArray()) {}
34
36 const CVTypeArray &Types, uint32_t RecordCountHint,
37 PartialOffsetArray PartialOffsets)
38 : NameStorage(Allocator), Types(Types), PartialOffsets(PartialOffsets) {
39 Records.resize(RecordCountHint);
40}
41
46
48 uint32_t RecordCountHint)
49 : LazyRandomTypeCollection(ArrayRef(Data.bytes_begin(), Data.bytes_end()),
50 RecordCountHint) {}
51
53 uint32_t NumRecords)
54 : LazyRandomTypeCollection(Types, NumRecords, PartialOffsetArray()) {}
55
57 uint32_t RecordCountHint) {
58 Count = 0;
59 PartialOffsets = PartialOffsetArray();
60
61 error(Reader.readArray(Types, Reader.bytesRemaining()));
62
63 // Clear and then resize, to make sure existing data gets destroyed.
64 Records.clear();
65 Records.resize(RecordCountHint);
66}
67
70 reset(Reader, RecordCountHint);
71}
72
74 uint32_t RecordCountHint) {
76 reset(Reader, RecordCountHint);
77}
78
80 error(ensureTypeExists(Index));
81 assert(contains(Index));
82
83 return Records[Index.toArrayIndex()].Offset;
84}
85
87 assert(!Index.isSimple());
88
89 auto EC = ensureTypeExists(Index);
90 error(std::move(EC));
91 assert(contains(Index));
92
93 return Records[Index.toArrayIndex()].Type;
94}
95
98 if (Index.isSimple())
99 return llvm::createStringError("Type index too low (%d)", Index.getIndex());
100
101 if (auto EC = ensureTypeExists(Index)) {
102 return EC;
103 }
104
105 if (!contains(Index))
106 return llvm::createStringError("Type index too high (%d)",
107 Index.getIndex());
108 return Records[Index.toArrayIndex()].Type;
109}
110
113}
114
116 if (Index.isNoneType() || Index.isSimple())
117 return TypeIndex::simpleTypeName(Index);
118
119 // Try to make sure the type exists. Even if it doesn't though, it may be
120 // because we're dumping a symbol stream with no corresponding type stream
121 // present, in which case we still want to be able to print <unknown UDT>
122 // for the type names.
123 if (auto EC = ensureTypeExists(Index)) {
124 consumeError(std::move(EC));
125 return "<unknown UDT>";
126 }
127
128 uint32_t I = Index.toArrayIndex();
129 ensureCapacityFor(Index);
130 if (Records[I].Name.data() == nullptr) {
131 StringRef Result = NameStorage.save(computeTypeName(*this, Index));
132 Records[I].Name = Result;
133 }
134 return Records[I].Name;
135}
136
138 if (Index.isSimple() || Index.isNoneType())
139 return false;
140
141 if (Records.size() <= Index.toArrayIndex())
142 return false;
143 if (!Records[Index.toArrayIndex()].Type.valid())
144 return false;
145 return true;
146}
147
149
151
152Error LazyRandomTypeCollection::ensureTypeExists(TypeIndex TI) {
153 if (contains(TI))
154 return Error::success();
155
156 return visitRangeForType(TI);
157}
158
159void LazyRandomTypeCollection::ensureCapacityFor(TypeIndex Index) {
160 assert(!Index.isSimple());
161 uint32_t MinSize = Index.toArrayIndex() + 1;
162
163 if (MinSize <= capacity())
164 return;
165
166 uint32_t NewCapacity = MinSize * 3 / 2;
167
168 assert(NewCapacity > capacity());
169 Records.resize(NewCapacity);
170}
171
172Error LazyRandomTypeCollection::visitRangeForType(TypeIndex TI) {
173 assert(!TI.isSimple());
174 if (PartialOffsets.empty())
175 return fullScanForType(TI);
176
177 auto Next = llvm::upper_bound(PartialOffsets, TI,
178 [](TypeIndex Value, const TypeIndexOffset &IO) {
179 return Value < IO.Type;
180 });
181
182 assert(Next != PartialOffsets.begin());
183 auto Prev = std::prev(Next);
184
185 TypeIndex TIB = Prev->Type;
186 if (contains(TIB)) {
187 // They've asked us to fetch a type index, but the entry we found in the
188 // partial offsets array has already been visited. Since we visit an entire
189 // block every time, that means this record should have been previously
190 // discovered. Ultimately, this means this is a request for a non-existent
191 // type index.
192 return make_error<CodeViewError>("Invalid type index");
193 }
194
195 TypeIndex TIE;
196 if (Next == PartialOffsets.end()) {
198 } else {
199 TIE = Next->Type;
200 }
201
202 visitRange(TIB, Prev->Offset, TIE);
203 return Error::success();
204}
205
206std::optional<TypeIndex> LazyRandomTypeCollection::getFirst() {
208 if (auto EC = ensureTypeExists(TI)) {
209 consumeError(std::move(EC));
210 return std::nullopt;
211 }
212 return TI;
213}
214
215std::optional<TypeIndex> LazyRandomTypeCollection::getNext(TypeIndex Prev) {
216 // We can't be sure how long this type stream is, given that the initial count
217 // given to the constructor is just a hint. So just try to make sure the next
218 // record exists, and if anything goes wrong, we must be at the end.
219 if (auto EC = ensureTypeExists(Prev + 1)) {
220 consumeError(std::move(EC));
221 return std::nullopt;
222 }
223
224 return Prev + 1;
225}
226
227Error LazyRandomTypeCollection::fullScanForType(TypeIndex TI) {
228 assert(!TI.isSimple());
229 assert(PartialOffsets.empty());
230
232 auto Begin = Types.begin();
233
234 if (Count > 0) {
235 // In the case of type streams which we don't know the number of records of,
236 // it's possible to search for a type index triggering a full scan, but then
237 // later additional records are added since we didn't know how many there
238 // would be until we did a full visitation, then you try to access the new
239 // type triggering another full scan. To avoid this, we assume that if the
240 // database has some records, this must be what's going on. We can also
241 // assume that this index must be larger than the largest type index we've
242 // visited, so we start from there and scan forward.
243 uint32_t Offset = Records[LargestTypeIndex.toArrayIndex()].Offset;
244 CurrentTI = LargestTypeIndex + 1;
245 Begin = Types.at(Offset);
246 ++Begin;
247 }
248
249 auto End = Types.end();
250 while (Begin != End) {
251 ensureCapacityFor(CurrentTI);
252 LargestTypeIndex = std::max(LargestTypeIndex, CurrentTI);
253 auto Idx = CurrentTI.toArrayIndex();
254 Records[Idx].Type = *Begin;
255 Records[Idx].Offset = Begin.offset();
256 ++Count;
257 ++Begin;
258 ++CurrentTI;
259 }
260 if (CurrentTI <= TI) {
261 return make_error<CodeViewError>("Type Index does not exist!");
262 }
263 return Error::success();
264}
265
266void LazyRandomTypeCollection::visitRange(TypeIndex Begin, uint32_t BeginOffset,
267 TypeIndex End) {
268 auto RI = Types.at(BeginOffset);
269 assert(RI != Types.end());
270
271 ensureCapacityFor(End);
272 while (Begin != End) {
273 LargestTypeIndex = std::max(LargestTypeIndex, Begin);
274 auto Idx = Begin.toArrayIndex();
275 Records[Idx].Type = *RI;
276 Records[Idx].Offset = RI.offset();
277 ++Count;
278 ++Begin;
279 ++RI;
280 }
281}
282
284 bool Stabilize) {
285 llvm_unreachable("Method cannot be called");
286}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
This file contains some templates that are useful if you are working with the STL at all.
#define error(X)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Provides read only access to a subclass of BinaryStream.
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
llvm::Expected< CVType > getTypeOrError(TypeIndex Index)
std::optional< TypeIndex > getNext(TypeIndex Prev) override
std::optional< CVType > tryGetType(TypeIndex Index)
bool replaceType(TypeIndex &Index, CVType Data, bool Stabilize) override
StringRef getTypeName(TypeIndex Index) override
std::optional< TypeIndex > getFirst() override
void reset(ArrayRef< uint8_t > Data, uint32_t RecordCountHint)
A 32-bit type reference.
Definition TypeIndex.h:97
static TypeIndex fromArrayIndex(uint32_t Index)
Definition TypeIndex.h:124
uint32_t toArrayIndex() const
Definition TypeIndex.h:119
static LLVM_ABI StringRef simpleTypeName(TypeIndex TI)
Definition TypeIndex.cpp:71
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CVRecord< TypeLeafKind > CVType
Definition CVRecord.h:64
VarStreamArray< CVType > CVTypeArray
Definition CVRecord.h:127
LLVM_ABI std::string computeTypeName(TypeCollection &Types, TypeIndex Index)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2055
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
std::optional< T > expectedToOptional(Expected< T > &&E)
Convert an Expected to an Optional without doing anything.
Definition Error.h:1094
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083