/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===// |
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 "MetadataLoader.h" |
10 | | #include "ValueList.h" |
11 | | |
12 | | #include "llvm/ADT/APFloat.h" |
13 | | #include "llvm/ADT/APInt.h" |
14 | | #include "llvm/ADT/ArrayRef.h" |
15 | | #include "llvm/ADT/DenseMap.h" |
16 | | #include "llvm/ADT/DenseSet.h" |
17 | | #include "llvm/ADT/None.h" |
18 | | #include "llvm/ADT/STLExtras.h" |
19 | | #include "llvm/ADT/SmallString.h" |
20 | | #include "llvm/ADT/SmallVector.h" |
21 | | #include "llvm/ADT/Statistic.h" |
22 | | #include "llvm/ADT/StringRef.h" |
23 | | #include "llvm/ADT/Twine.h" |
24 | | #include "llvm/Bitcode/BitcodeReader.h" |
25 | | #include "llvm/Bitstream/BitstreamReader.h" |
26 | | #include "llvm/Bitcode/LLVMBitCodes.h" |
27 | | #include "llvm/IR/Argument.h" |
28 | | #include "llvm/IR/Attributes.h" |
29 | | #include "llvm/IR/AutoUpgrade.h" |
30 | | #include "llvm/IR/BasicBlock.h" |
31 | | #include "llvm/IR/CallingConv.h" |
32 | | #include "llvm/IR/Comdat.h" |
33 | | #include "llvm/IR/Constant.h" |
34 | | #include "llvm/IR/Constants.h" |
35 | | #include "llvm/IR/DebugInfo.h" |
36 | | #include "llvm/IR/DebugInfoMetadata.h" |
37 | | #include "llvm/IR/DebugLoc.h" |
38 | | #include "llvm/IR/DerivedTypes.h" |
39 | | #include "llvm/IR/DiagnosticPrinter.h" |
40 | | #include "llvm/IR/Function.h" |
41 | | #include "llvm/IR/GVMaterializer.h" |
42 | | #include "llvm/IR/GlobalAlias.h" |
43 | | #include "llvm/IR/GlobalIFunc.h" |
44 | | #include "llvm/IR/GlobalIndirectSymbol.h" |
45 | | #include "llvm/IR/GlobalObject.h" |
46 | | #include "llvm/IR/GlobalValue.h" |
47 | | #include "llvm/IR/GlobalVariable.h" |
48 | | #include "llvm/IR/InlineAsm.h" |
49 | | #include "llvm/IR/InstrTypes.h" |
50 | | #include "llvm/IR/Instruction.h" |
51 | | #include "llvm/IR/Instructions.h" |
52 | | #include "llvm/IR/IntrinsicInst.h" |
53 | | #include "llvm/IR/Intrinsics.h" |
54 | | #include "llvm/IR/LLVMContext.h" |
55 | | #include "llvm/IR/Module.h" |
56 | | #include "llvm/IR/ModuleSummaryIndex.h" |
57 | | #include "llvm/IR/OperandTraits.h" |
58 | | #include "llvm/IR/TrackingMDRef.h" |
59 | | #include "llvm/IR/Type.h" |
60 | | #include "llvm/IR/ValueHandle.h" |
61 | | #include "llvm/Support/AtomicOrdering.h" |
62 | | #include "llvm/Support/Casting.h" |
63 | | #include "llvm/Support/CommandLine.h" |
64 | | #include "llvm/Support/Compiler.h" |
65 | | #include "llvm/Support/Debug.h" |
66 | | #include "llvm/Support/Error.h" |
67 | | #include "llvm/Support/ErrorHandling.h" |
68 | | #include "llvm/Support/ManagedStatic.h" |
69 | | #include "llvm/Support/MemoryBuffer.h" |
70 | | #include "llvm/Support/raw_ostream.h" |
71 | | #include <algorithm> |
72 | | #include <cassert> |
73 | | #include <cstddef> |
74 | | #include <cstdint> |
75 | | #include <deque> |
76 | | #include <limits> |
77 | | #include <map> |
78 | | #include <memory> |
79 | | #include <string> |
80 | | #include <system_error> |
81 | | #include <tuple> |
82 | | #include <utility> |
83 | | #include <vector> |
84 | | |
85 | | using namespace llvm; |
86 | | |
87 | | #define DEBUG_TYPE "bitcode-reader" |
88 | | |
89 | | STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded"); |
90 | | STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created"); |
91 | | STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded"); |
92 | | |
93 | | /// Flag whether we need to import full type definitions for ThinLTO. |
94 | | /// Currently needed for Darwin and LLDB. |
95 | | static cl::opt<bool> ImportFullTypeDefinitions( |
96 | | "import-full-type-definitions", cl::init(false), cl::Hidden, |
97 | | cl::desc("Import full type definitions for ThinLTO.")); |
98 | | |
99 | | static cl::opt<bool> DisableLazyLoading( |
100 | | "disable-ondemand-mds-loading", cl::init(false), cl::Hidden, |
101 | | cl::desc("Force disable the lazy-loading on-demand of metadata when " |
102 | | "loading bitcode for importing.")); |
103 | | |
104 | | namespace { |
105 | | |
106 | 85 | static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1)29 : U >> 156 ; } |
107 | | |
108 | | class BitcodeReaderMetadataList { |
109 | | /// Array of metadata references. |
110 | | /// |
111 | | /// Don't use std::vector here. Some versions of libc++ copy (instead of |
112 | | /// move) on resize, and TrackingMDRef is very expensive to copy. |
113 | | SmallVector<TrackingMDRef, 1> MetadataPtrs; |
114 | | |
115 | | /// The set of indices in MetadataPtrs above of forward references that were |
116 | | /// generated. |
117 | | SmallDenseSet<unsigned, 1> ForwardReference; |
118 | | |
119 | | /// The set of indices in MetadataPtrs above of Metadata that need to be |
120 | | /// resolved. |
121 | | SmallDenseSet<unsigned, 1> UnresolvedNodes; |
122 | | |
123 | | /// Structures for resolving old type refs. |
124 | | struct { |
125 | | SmallDenseMap<MDString *, TempMDTuple, 1> Unknown; |
126 | | SmallDenseMap<MDString *, DICompositeType *, 1> Final; |
127 | | SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls; |
128 | | SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays; |
129 | | } OldTypeRefs; |
130 | | |
131 | | LLVMContext &Context; |
132 | | |
133 | | /// Maximum number of valid references. Forward references exceeding the |
134 | | /// maximum must be invalid. |
135 | | unsigned RefsUpperBound; |
136 | | |
137 | | public: |
138 | | BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound) |
139 | | : Context(C), |
140 | | RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(), |
141 | 7.79k | RefsUpperBound)) {} |
142 | | |
143 | | // vector compatibility methods |
144 | 2.11M | unsigned size() const { return MetadataPtrs.size(); } |
145 | 324 | void resize(unsigned N) { MetadataPtrs.resize(N); } |
146 | 462k | void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); } |
147 | 0 | void clear() { MetadataPtrs.clear(); } |
148 | 0 | Metadata *back() const { return MetadataPtrs.back(); } |
149 | 0 | void pop_back() { MetadataPtrs.pop_back(); } |
150 | 32 | bool empty() const { return MetadataPtrs.empty(); } |
151 | | |
152 | 0 | Metadata *operator[](unsigned i) const { |
153 | 0 | assert(i < MetadataPtrs.size()); |
154 | 0 | return MetadataPtrs[i]; |
155 | 0 | } |
156 | | |
157 | 1.23M | Metadata *lookup(unsigned I) const { |
158 | 1.23M | if (I < MetadataPtrs.size()) |
159 | 1.19M | return MetadataPtrs[I]; |
160 | 38.6k | return nullptr; |
161 | 38.6k | } |
162 | | |
163 | 337k | void shrinkTo(unsigned N) { |
164 | 337k | assert(N <= size() && "Invalid shrinkTo request!"); |
165 | 337k | assert(ForwardReference.empty() && "Unexpected forward refs"); |
166 | 337k | assert(UnresolvedNodes.empty() && "Unexpected unresolved node"); |
167 | 337k | MetadataPtrs.resize(N); |
168 | 337k | } |
169 | | |
170 | | /// Return the given metadata, creating a replaceable forward reference if |
171 | | /// necessary. |
172 | | Metadata *getMetadataFwdRef(unsigned Idx); |
173 | | |
174 | | /// Return the given metadata only if it is fully resolved. |
175 | | /// |
176 | | /// Gives the same result as \a lookup(), unless \a MDNode::isResolved() |
177 | | /// would give \c false. |
178 | | Metadata *getMetadataIfResolved(unsigned Idx); |
179 | | |
180 | | MDNode *getMDNodeFwdRefOrNull(unsigned Idx); |
181 | | void assignValue(Metadata *MD, unsigned Idx); |
182 | | void tryToResolveCycles(); |
183 | 898k | bool hasFwdRefs() const { return !ForwardReference.empty(); } |
184 | 17 | int getNextFwdRef() { |
185 | 17 | assert(hasFwdRefs()); |
186 | 17 | return *ForwardReference.begin(); |
187 | 17 | } |
188 | | |
189 | | /// Upgrade a type that had an MDString reference. |
190 | | void addTypeRef(MDString &UUID, DICompositeType &CT); |
191 | | |
192 | | /// Upgrade a type that had an MDString reference. |
193 | | Metadata *upgradeTypeRef(Metadata *MaybeUUID); |
194 | | |
195 | | /// Upgrade a type ref array that may have MDString references. |
196 | | Metadata *upgradeTypeRefArray(Metadata *MaybeTuple); |
197 | | |
198 | | private: |
199 | | Metadata *resolveTypeRefArray(Metadata *MaybeTuple); |
200 | | }; |
201 | | |
202 | 463k | void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) { |
203 | 463k | if (auto *MDN = dyn_cast<MDNode>(MD)) |
204 | 283k | if (!MDN->isResolved()) |
205 | 1.42k | UnresolvedNodes.insert(Idx); |
206 | 463k | |
207 | 463k | if (Idx == size()) { |
208 | 462k | push_back(MD); |
209 | 462k | return; |
210 | 462k | } |
211 | 1.37k | |
212 | 1.37k | if (Idx >= size()) |
213 | 0 | resize(Idx + 1); |
214 | 1.37k | |
215 | 1.37k | TrackingMDRef &OldMD = MetadataPtrs[Idx]; |
216 | 1.37k | if (!OldMD) { |
217 | 389 | OldMD.reset(MD); |
218 | 389 | return; |
219 | 389 | } |
220 | 985 | |
221 | 985 | // If there was a forward reference to this value, replace it. |
222 | 985 | TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); |
223 | 985 | PrevMD->replaceAllUsesWith(MD); |
224 | 985 | ForwardReference.erase(Idx); |
225 | 985 | } |
226 | | |
227 | 1.27M | Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) { |
228 | 1.27M | // Bail out for a clearly invalid value. |
229 | 1.27M | if (Idx >= RefsUpperBound) |
230 | 2 | return nullptr; |
231 | 1.27M | |
232 | 1.27M | if (Idx >= size()) |
233 | 318 | resize(Idx + 1); |
234 | 1.27M | |
235 | 1.27M | if (Metadata *MD = MetadataPtrs[Idx]) |
236 | 1.27M | return MD; |
237 | 988 | |
238 | 988 | // Track forward refs to be resolved later. |
239 | 988 | ForwardReference.insert(Idx); |
240 | 988 | |
241 | 988 | // Create and return a placeholder, which will later be RAUW'd. |
242 | 988 | ++NumMDNodeTemporary; |
243 | 988 | Metadata *MD = MDNode::getTemporary(Context, None).release(); |
244 | 988 | MetadataPtrs[Idx].reset(MD); |
245 | 988 | return MD; |
246 | 988 | } |
247 | | |
248 | 51.6k | Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) { |
249 | 51.6k | Metadata *MD = lookup(Idx); |
250 | 51.6k | if (auto *N = dyn_cast_or_null<MDNode>(MD)) |
251 | 8.60k | if (!N->isResolved()) |
252 | 16 | return nullptr; |
253 | 51.6k | return MD; |
254 | 51.6k | } |
255 | | |
256 | 58.4k | MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) { |
257 | 58.4k | return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx)); |
258 | 58.4k | } |
259 | | |
260 | 192k | void BitcodeReaderMetadataList::tryToResolveCycles() { |
261 | 192k | if (!ForwardReference.empty()) |
262 | 0 | // Still forward references... can't resolve cycles. |
263 | 0 | return; |
264 | 192k | |
265 | 192k | // Give up on finding a full definition for any forward decls that remain. |
266 | 192k | for (const auto &Ref : OldTypeRefs.FwdDecls) |
267 | 0 | OldTypeRefs.Final.insert(Ref); |
268 | 192k | OldTypeRefs.FwdDecls.clear(); |
269 | 192k | |
270 | 192k | // Upgrade from old type ref arrays. In strange cases, this could add to |
271 | 192k | // OldTypeRefs.Unknown. |
272 | 192k | for (const auto &Array : OldTypeRefs.Arrays) |
273 | 0 | Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get())); |
274 | 192k | OldTypeRefs.Arrays.clear(); |
275 | 192k | |
276 | 192k | // Replace old string-based type refs with the resolved node, if possible. |
277 | 192k | // If we haven't seen the node, leave it to the verifier to complain about |
278 | 192k | // the invalid string reference. |
279 | 192k | for (const auto &Ref : OldTypeRefs.Unknown) { |
280 | 2 | if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first)) |
281 | 2 | Ref.second->replaceAllUsesWith(CT); |
282 | 0 | else |
283 | 0 | Ref.second->replaceAllUsesWith(Ref.first); |
284 | 2 | } |
285 | 192k | OldTypeRefs.Unknown.clear(); |
286 | 192k | |
287 | 192k | if (UnresolvedNodes.empty()) |
288 | 192k | // Nothing to do. |
289 | 192k | return; |
290 | 28 | |
291 | 28 | // Resolve any cycles. |
292 | 1.42k | for (unsigned I : UnresolvedNodes)28 { |
293 | 1.42k | auto &MD = MetadataPtrs[I]; |
294 | 1.42k | auto *N = dyn_cast_or_null<MDNode>(MD); |
295 | 1.42k | if (!N) |
296 | 0 | continue; |
297 | 1.42k | |
298 | 1.42k | assert(!N->isTemporary() && "Unexpected forward reference"); |
299 | 1.42k | N->resolveCycles(); |
300 | 1.42k | } |
301 | 28 | |
302 | 28 | // Make sure we return early again until there's another unresolved ref. |
303 | 28 | UnresolvedNodes.clear(); |
304 | 28 | } |
305 | | |
306 | | void BitcodeReaderMetadataList::addTypeRef(MDString &UUID, |
307 | 6 | DICompositeType &CT) { |
308 | 6 | assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID"); |
309 | 6 | if (CT.isForwardDecl()) |
310 | 0 | OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT)); |
311 | 6 | else |
312 | 6 | OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT)); |
313 | 6 | } |
314 | | |
315 | 2.67k | Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) { |
316 | 2.67k | auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID); |
317 | 2.67k | if (LLVM_LIKELY(!UUID)) |
318 | 2.67k | return MaybeUUID2.63k ; |
319 | 35 | |
320 | 35 | if (auto *CT = OldTypeRefs.Final.lookup(UUID)) |
321 | 32 | return CT; |
322 | 3 | |
323 | 3 | auto &Ref = OldTypeRefs.Unknown[UUID]; |
324 | 3 | if (!Ref) |
325 | 2 | Ref = MDNode::getTemporary(Context, None); |
326 | 3 | return Ref.get(); |
327 | 3 | } |
328 | | |
329 | 18 | Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) { |
330 | 18 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); |
331 | 18 | if (!Tuple || Tuple->isDistinct()) |
332 | 0 | return MaybeTuple; |
333 | 18 | |
334 | 18 | // Look through the array immediately if possible. |
335 | 18 | if (!Tuple->isTemporary()) |
336 | 18 | return resolveTypeRefArray(Tuple); |
337 | 0 | |
338 | 0 | // Create and return a placeholder to use for now. Eventually |
339 | 0 | // resolveTypeRefArrays() will be resolve this forward reference. |
340 | 0 | OldTypeRefs.Arrays.emplace_back( |
341 | 0 | std::piecewise_construct, std::forward_as_tuple(Tuple), |
342 | 0 | std::forward_as_tuple(MDTuple::getTemporary(Context, None))); |
343 | 0 | return OldTypeRefs.Arrays.back().second.get(); |
344 | 0 | } |
345 | | |
346 | 18 | Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) { |
347 | 18 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); |
348 | 18 | if (!Tuple || Tuple->isDistinct()) |
349 | 0 | return MaybeTuple; |
350 | 18 | |
351 | 18 | // Look through the DITypeRefArray, upgrading each DIType *. |
352 | 18 | SmallVector<Metadata *, 32> Ops; |
353 | 18 | Ops.reserve(Tuple->getNumOperands()); |
354 | 18 | for (Metadata *MD : Tuple->operands()) |
355 | 28 | Ops.push_back(upgradeTypeRef(MD)); |
356 | 18 | |
357 | 18 | return MDTuple::get(Context, Ops); |
358 | 18 | } |
359 | | |
360 | | namespace { |
361 | | |
362 | | class PlaceholderQueue { |
363 | | // Placeholders would thrash around when moved, so store in a std::deque |
364 | | // instead of some sort of vector. |
365 | | std::deque<DistinctMDOperandPlaceholder> PHs; |
366 | | |
367 | | public: |
368 | 192k | ~PlaceholderQueue() { |
369 | 192k | assert(empty() && "PlaceholderQueue hasn't been flushed before being destroyed"); |
370 | 192k | } |
371 | 0 | bool empty() { return PHs.empty(); } |
372 | | DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID); |
373 | | void flush(BitcodeReaderMetadataList &MetadataList); |
374 | | |
375 | | /// Return the list of temporaries nodes in the queue, these need to be |
376 | | /// loaded before we can flush the queue. |
377 | | void getTemporaries(BitcodeReaderMetadataList &MetadataList, |
378 | 192k | DenseSet<unsigned> &Temporaries) { |
379 | 192k | for (auto &PH : PHs) { |
380 | 38.4k | auto ID = PH.getID(); |
381 | 38.4k | auto *MD = MetadataList.lookup(ID); |
382 | 38.4k | if (!MD) { |
383 | 16 | Temporaries.insert(ID); |
384 | 16 | continue; |
385 | 16 | } |
386 | 38.4k | auto *N = dyn_cast_or_null<MDNode>(MD); |
387 | 38.4k | if (N && N->isTemporary()) |
388 | 0 | Temporaries.insert(ID); |
389 | 38.4k | } |
390 | 192k | } |
391 | | }; |
392 | | |
393 | | } // end anonymous namespace |
394 | | |
395 | 38.4k | DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) { |
396 | 38.4k | PHs.emplace_back(ID); |
397 | 38.4k | return PHs.back(); |
398 | 38.4k | } |
399 | | |
400 | 192k | void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) { |
401 | 230k | while (!PHs.empty()) { |
402 | 38.4k | auto *MD = MetadataList.lookup(PHs.front().getID()); |
403 | 38.4k | assert(MD && "Flushing placeholder on unassigned MD"); |
404 | | #ifndef NDEBUG |
405 | | if (auto *MDN = dyn_cast<MDNode>(MD)) |
406 | | assert(MDN->isResolved() && |
407 | | "Flushing Placeholder while cycles aren't resolved"); |
408 | | #endif |
409 | | PHs.front().replaceUseWith(MD); |
410 | 38.4k | PHs.pop_front(); |
411 | 38.4k | } |
412 | 192k | } |
413 | | |
414 | | } // anonynous namespace |
415 | | |
416 | 0 | static Error error(const Twine &Message) { |
417 | 0 | return make_error<StringError>( |
418 | 0 | Message, make_error_code(BitcodeError::CorruptedBitcode)); |
419 | 0 | } |
420 | | |
421 | | class MetadataLoader::MetadataLoaderImpl { |
422 | | BitcodeReaderMetadataList MetadataList; |
423 | | BitcodeReaderValueList &ValueList; |
424 | | BitstreamCursor &Stream; |
425 | | LLVMContext &Context; |
426 | | Module &TheModule; |
427 | | std::function<Type *(unsigned)> getTypeByID; |
428 | | |
429 | | /// Cursor associated with the lazy-loading of Metadata. This is the easy way |
430 | | /// to keep around the right "context" (Abbrev list) to be able to jump in |
431 | | /// the middle of the metadata block and load any record. |
432 | | BitstreamCursor IndexCursor; |
433 | | |
434 | | /// Index that keeps track of MDString values. |
435 | | std::vector<StringRef> MDStringRef; |
436 | | |
437 | | /// On-demand loading of a single MDString. Requires the index above to be |
438 | | /// populated. |
439 | | MDString *lazyLoadOneMDString(unsigned Idx); |
440 | | |
441 | | /// Index that keeps track of where to find a metadata record in the stream. |
442 | | std::vector<uint64_t> GlobalMetadataBitPosIndex; |
443 | | |
444 | | /// Populate the index above to enable lazily loading of metadata, and load |
445 | | /// the named metadata as well as the transitively referenced global |
446 | | /// Metadata. |
447 | | Expected<bool> lazyLoadModuleMetadataBlock(); |
448 | | |
449 | | /// On-demand loading of a single metadata. Requires the index above to be |
450 | | /// populated. |
451 | | void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders); |
452 | | |
453 | | // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to |
454 | | // point from SP to CU after a block is completly parsed. |
455 | | std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms; |
456 | | |
457 | | /// Functions that need to be matched with subprograms when upgrading old |
458 | | /// metadata. |
459 | | SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs; |
460 | | |
461 | | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. |
462 | | DenseMap<unsigned, unsigned> MDKindMap; |
463 | | |
464 | | bool StripTBAA = false; |
465 | | bool HasSeenOldLoopTags = false; |
466 | | bool NeedUpgradeToDIGlobalVariableExpression = false; |
467 | | bool NeedDeclareExpressionUpgrade = false; |
468 | | |
469 | | /// True if metadata is being parsed for a module being ThinLTO imported. |
470 | | bool IsImporting = false; |
471 | | |
472 | | Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code, |
473 | | PlaceholderQueue &Placeholders, StringRef Blob, |
474 | | unsigned &NextMetadataNo); |
475 | | Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob, |
476 | | function_ref<void(StringRef)> CallBack); |
477 | | Error parseGlobalObjectAttachment(GlobalObject &GO, |
478 | | ArrayRef<uint64_t> Record); |
479 | | Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record); |
480 | | |
481 | | void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders); |
482 | | |
483 | | /// Upgrade old-style CU <-> SP pointers to point from SP to CU. |
484 | 36.1k | void upgradeCUSubprograms() { |
485 | 36.1k | for (auto CU_SP : CUSubprograms) |
486 | 6 | if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second)) |
487 | 6 | for (auto &Op : SPs->operands()) |
488 | 21 | if (auto *SP = dyn_cast_or_null<DISubprogram>(Op)) |
489 | 21 | SP->replaceUnit(CU_SP.first); |
490 | 36.1k | CUSubprograms.clear(); |
491 | 36.1k | } |
492 | | |
493 | | /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions. |
494 | 36.1k | void upgradeCUVariables() { |
495 | 36.1k | if (!NeedUpgradeToDIGlobalVariableExpression) |
496 | 36.1k | return; |
497 | 22 | |
498 | 22 | // Upgrade list of variables attached to the CUs. |
499 | 22 | if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) |
500 | 40 | for (unsigned I = 0, E = CUNodes->getNumOperands(); 20 I != E; ++I20 ) { |
501 | 20 | auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I)); |
502 | 20 | if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables())) |
503 | 66 | for (unsigned I = 0; 20 I < GVs->getNumOperands(); I++46 ) |
504 | 46 | if (auto *GV = |
505 | 8 | dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) { |
506 | 8 | auto *DGVE = DIGlobalVariableExpression::getDistinct( |
507 | 8 | Context, GV, DIExpression::get(Context, {})); |
508 | 8 | GVs->replaceOperandWith(I, DGVE); |
509 | 8 | } |
510 | 20 | } |
511 | 22 | |
512 | 22 | // Upgrade variables attached to globals. |
513 | 45 | for (auto &GV : TheModule.globals()) { |
514 | 45 | SmallVector<MDNode *, 1> MDs; |
515 | 45 | GV.getMetadata(LLVMContext::MD_dbg, MDs); |
516 | 45 | GV.eraseMetadata(LLVMContext::MD_dbg); |
517 | 45 | for (auto *MD : MDs) |
518 | 45 | if (auto *DGV = dyn_cast_or_null<DIGlobalVariable>(MD)) { |
519 | 3 | auto *DGVE = DIGlobalVariableExpression::getDistinct( |
520 | 3 | Context, DGV, DIExpression::get(Context, {})); |
521 | 3 | GV.addMetadata(LLVMContext::MD_dbg, *DGVE); |
522 | 3 | } else |
523 | 42 | GV.addMetadata(LLVMContext::MD_dbg, *MD); |
524 | 45 | } |
525 | 22 | } |
526 | | |
527 | | /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that |
528 | | /// describes a function argument. |
529 | 3.41M | void upgradeDeclareExpressions(Function &F) { |
530 | 3.41M | if (!NeedDeclareExpressionUpgrade) |
531 | 3.41M | return; |
532 | 40 | |
533 | 40 | for (auto &BB : F) |
534 | 30 | for (auto &I : BB) |
535 | 131 | if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) |
536 | 10 | if (auto *DIExpr = DDI->getExpression()) |
537 | 10 | if (DIExpr->startsWithDeref() && |
538 | 10 | dyn_cast_or_null<Argument>(DDI->getAddress())1 ) { |
539 | 1 | SmallVector<uint64_t, 8> Ops; |
540 | 1 | Ops.append(std::next(DIExpr->elements_begin()), |
541 | 1 | DIExpr->elements_end()); |
542 | 1 | auto *E = DIExpression::get(Context, Ops); |
543 | 1 | DDI->setOperand(2, MetadataAsValue::get(Context, E)); |
544 | 1 | } |
545 | 40 | } |
546 | | |
547 | | /// Upgrade the expression from previous versions. |
548 | | Error upgradeDIExpression(uint64_t FromVersion, |
549 | | MutableArrayRef<uint64_t> &Expr, |
550 | 142 | SmallVectorImpl<uint64_t> &Buffer) { |
551 | 142 | auto N = Expr.size(); |
552 | 142 | switch (FromVersion) { |
553 | 142 | default: |
554 | 0 | return error("Invalid record"); |
555 | 142 | case 0: |
556 | 6 | if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece3 ) |
557 | 3 | Expr[N - 3] = dwarf::DW_OP_LLVM_fragment; |
558 | 6 | LLVM_FALLTHROUGH; |
559 | 16 | case 1: |
560 | 16 | // Move DW_OP_deref to the end. |
561 | 16 | if (N && Expr[0] == dwarf::DW_OP_deref12 ) { |
562 | 4 | auto End = Expr.end(); |
563 | 4 | if (Expr.size() >= 3 && |
564 | 4 | *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment2 ) |
565 | 1 | End = std::prev(End, 3); |
566 | 4 | std::move(std::next(Expr.begin()), End, Expr.begin()); |
567 | 4 | *std::prev(End) = dwarf::DW_OP_deref; |
568 | 4 | } |
569 | 16 | NeedDeclareExpressionUpgrade = true; |
570 | 16 | LLVM_FALLTHROUGH; |
571 | 17 | case 2: { |
572 | 17 | // Change DW_OP_plus to DW_OP_plus_uconst. |
573 | 17 | // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus |
574 | 17 | auto SubExpr = ArrayRef<uint64_t>(Expr); |
575 | 36 | while (!SubExpr.empty()) { |
576 | 19 | // Skip past other operators with their operands |
577 | 19 | // for this version of the IR, obtained from |
578 | 19 | // from historic DIExpression::ExprOperand::getSize(). |
579 | 19 | size_t HistoricSize; |
580 | 19 | switch (SubExpr.front()) { |
581 | 19 | default: |
582 | 7 | HistoricSize = 1; |
583 | 7 | break; |
584 | 19 | case dwarf::DW_OP_constu: |
585 | 8 | case dwarf::DW_OP_minus: |
586 | 8 | case dwarf::DW_OP_plus: |
587 | 8 | HistoricSize = 2; |
588 | 8 | break; |
589 | 8 | case dwarf::DW_OP_LLVM_fragment: |
590 | 4 | HistoricSize = 3; |
591 | 4 | break; |
592 | 19 | } |
593 | 19 | |
594 | 19 | // If the expression is malformed, make sure we don't |
595 | 19 | // copy more elements than we should. |
596 | 19 | HistoricSize = std::min(SubExpr.size(), HistoricSize); |
597 | 19 | ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize-1); |
598 | 19 | |
599 | 19 | switch (SubExpr.front()) { |
600 | 19 | case dwarf::DW_OP_plus: |
601 | 5 | Buffer.push_back(dwarf::DW_OP_plus_uconst); |
602 | 5 | Buffer.append(Args.begin(), Args.end()); |
603 | 5 | break; |
604 | 19 | case dwarf::DW_OP_minus: |
605 | 1 | Buffer.push_back(dwarf::DW_OP_constu); |
606 | 1 | Buffer.append(Args.begin(), Args.end()); |
607 | 1 | Buffer.push_back(dwarf::DW_OP_minus); |
608 | 1 | break; |
609 | 19 | default: |
610 | 13 | Buffer.push_back(*SubExpr.begin()); |
611 | 13 | Buffer.append(Args.begin(), Args.end()); |
612 | 13 | break; |
613 | 19 | } |
614 | 19 | |
615 | 19 | // Continue with remaining elements. |
616 | 19 | SubExpr = SubExpr.slice(HistoricSize); |
617 | 19 | } |
618 | 17 | Expr = MutableArrayRef<uint64_t>(Buffer); |
619 | 17 | LLVM_FALLTHROUGH; |
620 | 17 | } |
621 | 142 | case 3: |
622 | 142 | // Up-to-date! |
623 | 142 | break; |
624 | 142 | } |
625 | 142 | |
626 | 142 | return Error::success(); |
627 | 142 | } |
628 | | |
629 | 36.1k | void upgradeDebugInfo() { |
630 | 36.1k | upgradeCUSubprograms(); |
631 | 36.1k | upgradeCUVariables(); |
632 | 36.1k | } |
633 | | |
634 | | public: |
635 | | MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule, |
636 | | BitcodeReaderValueList &ValueList, |
637 | | std::function<Type *(unsigned)> getTypeByID, |
638 | | bool IsImporting) |
639 | | : MetadataList(TheModule.getContext(), Stream.SizeInBytes()), |
640 | | ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()), |
641 | | TheModule(TheModule), getTypeByID(std::move(getTypeByID)), |
642 | 7.78k | IsImporting(IsImporting) {} |
643 | | |
644 | | Error parseMetadata(bool ModuleLevel); |
645 | | |
646 | 675k | bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); } |
647 | | |
648 | 18.1k | Metadata *getMetadataFwdRefOrLoad(unsigned ID) { |
649 | 18.1k | if (ID < MDStringRef.size()) |
650 | 0 | return lazyLoadOneMDString(ID); |
651 | 18.1k | if (auto *MD = MetadataList.lookup(ID)) |
652 | 18.1k | return MD; |
653 | 5 | // If lazy-loading is enabled, we try recursively to load the operand |
654 | 5 | // instead of creating a temporary. |
655 | 5 | if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { |
656 | 3 | PlaceholderQueue Placeholders; |
657 | 3 | lazyLoadOneMetadata(ID, Placeholders); |
658 | 3 | resolveForwardRefsAndPlaceholders(Placeholders); |
659 | 3 | return MetadataList.lookup(ID); |
660 | 3 | } |
661 | 2 | return MetadataList.getMetadataFwdRef(ID); |
662 | 2 | } |
663 | | |
664 | 337k | DISubprogram *lookupSubprogramForFunction(Function *F) { |
665 | 337k | return FunctionsWithSPs.lookup(F); |
666 | 337k | } |
667 | | |
668 | 0 | bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; } |
669 | | |
670 | | Error parseMetadataAttachment( |
671 | | Function &F, const SmallVectorImpl<Instruction *> &InstructionList); |
672 | | |
673 | | Error parseMetadataKinds(); |
674 | | |
675 | 2 | void setStripTBAA(bool Value) { StripTBAA = Value; } |
676 | 337k | bool isStrippingTBAA() { return StripTBAA; } |
677 | | |
678 | 337k | unsigned size() const { return MetadataList.size(); } |
679 | 337k | void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); } |
680 | 3.41M | void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); } |
681 | | }; |
682 | | |
683 | | Expected<bool> |
684 | 32 | MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() { |
685 | 32 | IndexCursor = Stream; |
686 | 32 | SmallVector<uint64_t, 64> Record; |
687 | 32 | // Get the abbrevs, and preload record positions to make them lazy-loadable. |
688 | 79 | while (true) { |
689 | 79 | Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks( |
690 | 79 | BitstreamCursor::AF_DontPopBlockAtEnd); |
691 | 79 | if (!MaybeEntry) |
692 | 0 | return MaybeEntry.takeError(); |
693 | 79 | BitstreamEntry Entry = MaybeEntry.get(); |
694 | 79 | |
695 | 79 | switch (Entry.Kind) { |
696 | 79 | case BitstreamEntry::SubBlock: // Handled for us already. |
697 | 0 | case BitstreamEntry::Error: |
698 | 0 | return error("Malformed block"); |
699 | 6 | case BitstreamEntry::EndBlock: { |
700 | 6 | return true; |
701 | 0 | } |
702 | 73 | case BitstreamEntry::Record: { |
703 | 73 | // The interesting case. |
704 | 73 | ++NumMDRecordLoaded; |
705 | 73 | uint64_t CurrentPos = IndexCursor.GetCurrentBitNo(); |
706 | 73 | Expected<unsigned> MaybeCode = IndexCursor.skipRecord(Entry.ID); |
707 | 73 | if (!MaybeCode) |
708 | 0 | return MaybeCode.takeError(); |
709 | 73 | unsigned Code = MaybeCode.get(); |
710 | 73 | switch (Code) { |
711 | 73 | case bitc::METADATA_STRINGS: { |
712 | 32 | // Rewind and parse the strings. |
713 | 32 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
714 | 0 | return std::move(Err); |
715 | 32 | StringRef Blob; |
716 | 32 | Record.clear(); |
717 | 32 | if (Expected<unsigned> MaybeRecord = |
718 | 32 | IndexCursor.readRecord(Entry.ID, Record, &Blob)) |
719 | 32 | ; |
720 | 0 | else |
721 | 0 | return MaybeRecord.takeError(); |
722 | 32 | unsigned NumStrings = Record[0]; |
723 | 32 | MDStringRef.reserve(NumStrings); |
724 | 184 | auto IndexNextMDString = [&](StringRef Str) { |
725 | 184 | MDStringRef.push_back(Str); |
726 | 184 | }; |
727 | 32 | if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString)) |
728 | 0 | return std::move(Err); |
729 | 32 | break; |
730 | 32 | } |
731 | 32 | case bitc::METADATA_INDEX_OFFSET: { |
732 | 6 | // This is the offset to the index, when we see this we skip all the |
733 | 6 | // records and load only an index to these. |
734 | 6 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
735 | 0 | return std::move(Err); |
736 | 6 | Record.clear(); |
737 | 6 | if (Expected<unsigned> MaybeRecord = |
738 | 6 | IndexCursor.readRecord(Entry.ID, Record)) |
739 | 6 | ; |
740 | 0 | else |
741 | 0 | return MaybeRecord.takeError(); |
742 | 6 | if (Record.size() != 2) |
743 | 0 | return error("Invalid record"); |
744 | 6 | auto Offset = Record[0] + (Record[1] << 32); |
745 | 6 | auto BeginPos = IndexCursor.GetCurrentBitNo(); |
746 | 6 | if (Error Err = IndexCursor.JumpToBit(BeginPos + Offset)) |
747 | 0 | return std::move(Err); |
748 | 6 | Expected<BitstreamEntry> MaybeEntry = |
749 | 6 | IndexCursor.advanceSkippingSubblocks( |
750 | 6 | BitstreamCursor::AF_DontPopBlockAtEnd); |
751 | 6 | if (!MaybeEntry) |
752 | 0 | return MaybeEntry.takeError(); |
753 | 6 | Entry = MaybeEntry.get(); |
754 | 6 | assert(Entry.Kind == BitstreamEntry::Record && |
755 | 6 | "Corrupted bitcode: Expected `Record` when trying to find the " |
756 | 6 | "Metadata index"); |
757 | 6 | Record.clear(); |
758 | 6 | if (Expected<unsigned> MaybeCode = |
759 | 6 | IndexCursor.readRecord(Entry.ID, Record)) |
760 | 6 | assert(MaybeCode.get() == bitc::METADATA_INDEX && |
761 | 6 | "Corrupted bitcode: Expected `METADATA_INDEX` when trying to " |
762 | 6 | "find the Metadata index"); |
763 | 6 | else |
764 | 6 | return MaybeCode.takeError()0 ; |
765 | 6 | // Delta unpack |
766 | 6 | auto CurrentValue = BeginPos; |
767 | 6 | GlobalMetadataBitPosIndex.reserve(Record.size()); |
768 | 230 | for (auto &Elt : Record) { |
769 | 230 | CurrentValue += Elt; |
770 | 230 | GlobalMetadataBitPosIndex.push_back(CurrentValue); |
771 | 230 | } |
772 | 6 | break; |
773 | 6 | } |
774 | 6 | case bitc::METADATA_INDEX: |
775 | 0 | // We don't expect to get there, the Index is loaded when we encounter |
776 | 0 | // the offset. |
777 | 0 | return error("Corrupted Metadata block"); |
778 | 9 | case bitc::METADATA_NAME: { |
779 | 9 | // Named metadata need to be materialized now and aren't deferred. |
780 | 9 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
781 | 0 | return std::move(Err); |
782 | 9 | Record.clear(); |
783 | 9 | |
784 | 9 | unsigned Code; |
785 | 9 | if (Expected<unsigned> MaybeCode = |
786 | 9 | IndexCursor.readRecord(Entry.ID, Record)) { |
787 | 9 | Code = MaybeCode.get(); |
788 | 9 | assert(Code == bitc::METADATA_NAME); |
789 | 9 | } else |
790 | 0 | return MaybeCode.takeError(); |
791 | 9 | |
792 | 9 | // Read name of the named metadata. |
793 | 9 | SmallString<8> Name(Record.begin(), Record.end()); |
794 | 9 | if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode()) |
795 | 9 | Code = MaybeCode.get(); |
796 | 0 | else |
797 | 0 | return MaybeCode.takeError(); |
798 | 9 | |
799 | 9 | // Named Metadata comes in two parts, we expect the name to be followed |
800 | 9 | // by the node |
801 | 9 | Record.clear(); |
802 | 9 | if (Expected<unsigned> MaybeNextBitCode = |
803 | 9 | IndexCursor.readRecord(Code, Record)) |
804 | 9 | assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE); |
805 | 9 | else |
806 | 9 | return MaybeNextBitCode.takeError()0 ; |
807 | 9 | |
808 | 9 | // Read named metadata elements. |
809 | 9 | unsigned Size = Record.size(); |
810 | 9 | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); |
811 | 24 | for (unsigned i = 0; i != Size; ++i15 ) { |
812 | 15 | // FIXME: We could use a placeholder here, however NamedMDNode are |
813 | 15 | // taking MDNode as operand and not using the Metadata infrastructure. |
814 | 15 | // It is acknowledged by 'TODO: Inherit from Metadata' in the |
815 | 15 | // NamedMDNode class definition. |
816 | 15 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); |
817 | 15 | assert(MD && "Invalid metadata: expect fwd ref to MDNode"); |
818 | 15 | NMD->addOperand(MD); |
819 | 15 | } |
820 | 9 | break; |
821 | 9 | } |
822 | 9 | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { |
823 | 0 | // FIXME: we need to do this early because we don't materialize global |
824 | 0 | // value explicitly. |
825 | 0 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
826 | 0 | return std::move(Err); |
827 | 0 | Record.clear(); |
828 | 0 | if (Expected<unsigned> MaybeRecord = |
829 | 0 | IndexCursor.readRecord(Entry.ID, Record)) |
830 | 0 | ; |
831 | 0 | else |
832 | 0 | return MaybeRecord.takeError(); |
833 | 0 | if (Record.size() % 2 == 0) |
834 | 0 | return error("Invalid record"); |
835 | 0 | unsigned ValueID = Record[0]; |
836 | 0 | if (ValueID >= ValueList.size()) |
837 | 0 | return error("Invalid record"); |
838 | 0 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) |
839 | 0 | if (Error Err = parseGlobalObjectAttachment( |
840 | 0 | *GO, ArrayRef<uint64_t>(Record).slice(1))) |
841 | 0 | return std::move(Err); |
842 | 0 | break; |
843 | 0 | } |
844 | 26 | case bitc::METADATA_KIND: |
845 | 26 | case bitc::METADATA_STRING_OLD: |
846 | 26 | case bitc::METADATA_OLD_FN_NODE: |
847 | 26 | case bitc::METADATA_OLD_NODE: |
848 | 26 | case bitc::METADATA_VALUE: |
849 | 26 | case bitc::METADATA_DISTINCT_NODE: |
850 | 26 | case bitc::METADATA_NODE: |
851 | 26 | case bitc::METADATA_LOCATION: |
852 | 26 | case bitc::METADATA_GENERIC_DEBUG: |
853 | 26 | case bitc::METADATA_SUBRANGE: |
854 | 26 | case bitc::METADATA_ENUMERATOR: |
855 | 26 | case bitc::METADATA_BASIC_TYPE: |
856 | 26 | case bitc::METADATA_DERIVED_TYPE: |
857 | 26 | case bitc::METADATA_COMPOSITE_TYPE: |
858 | 26 | case bitc::METADATA_SUBROUTINE_TYPE: |
859 | 26 | case bitc::METADATA_MODULE: |
860 | 26 | case bitc::METADATA_FILE: |
861 | 26 | case bitc::METADATA_COMPILE_UNIT: |
862 | 26 | case bitc::METADATA_SUBPROGRAM: |
863 | 26 | case bitc::METADATA_LEXICAL_BLOCK: |
864 | 26 | case bitc::METADATA_LEXICAL_BLOCK_FILE: |
865 | 26 | case bitc::METADATA_NAMESPACE: |
866 | 26 | case bitc::METADATA_COMMON_BLOCK: |
867 | 26 | case bitc::METADATA_MACRO: |
868 | 26 | case bitc::METADATA_MACRO_FILE: |
869 | 26 | case bitc::METADATA_TEMPLATE_TYPE: |
870 | 26 | case bitc::METADATA_TEMPLATE_VALUE: |
871 | 26 | case bitc::METADATA_GLOBAL_VAR: |
872 | 26 | case bitc::METADATA_LOCAL_VAR: |
873 | 26 | case bitc::METADATA_LABEL: |
874 | 26 | case bitc::METADATA_EXPRESSION: |
875 | 26 | case bitc::METADATA_OBJC_PROPERTY: |
876 | 26 | case bitc::METADATA_IMPORTED_ENTITY: |
877 | 26 | case bitc::METADATA_GLOBAL_VAR_EXPR: |
878 | 26 | // We don't expect to see any of these, if we see one, give up on |
879 | 26 | // lazy-loading and fallback. |
880 | 26 | MDStringRef.clear(); |
881 | 26 | GlobalMetadataBitPosIndex.clear(); |
882 | 26 | return false; |
883 | 47 | } |
884 | 47 | break; |
885 | 47 | } |
886 | 79 | } |
887 | 79 | } |
888 | 32 | } |
889 | | |
890 | | /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing |
891 | | /// module level metadata. |
892 | 36.1k | Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) { |
893 | 36.1k | if (!ModuleLevel && MetadataList.hasFwdRefs()30.7k ) |
894 | 0 | return error("Invalid metadata: fwd refs into function blocks"); |
895 | 36.1k | |
896 | 36.1k | // Record the entry position so that we can jump back here and efficiently |
897 | 36.1k | // skip the whole block in case we lazy-load. |
898 | 36.1k | auto EntryPos = Stream.GetCurrentBitNo(); |
899 | 36.1k | |
900 | 36.1k | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) |
901 | 0 | return Err; |
902 | 36.1k | |
903 | 36.1k | SmallVector<uint64_t, 64> Record; |
904 | 36.1k | PlaceholderQueue Placeholders; |
905 | 36.1k | |
906 | 36.1k | // We lazy-load module-level metadata: we build an index for each record, and |
907 | 36.1k | // then load individual record as needed, starting with the named metadata. |
908 | 36.1k | if (ModuleLevel && IsImporting5.41k && MetadataList.empty()32 && |
909 | 36.1k | !DisableLazyLoading32 ) { |
910 | 32 | auto SuccessOrErr = lazyLoadModuleMetadataBlock(); |
911 | 32 | if (!SuccessOrErr) |
912 | 0 | return SuccessOrErr.takeError(); |
913 | 32 | if (SuccessOrErr.get()) { |
914 | 6 | // An index was successfully created and we will be able to load metadata |
915 | 6 | // on-demand. |
916 | 6 | MetadataList.resize(MDStringRef.size() + |
917 | 6 | GlobalMetadataBitPosIndex.size()); |
918 | 6 | |
919 | 6 | // Reading the named metadata created forward references and/or |
920 | 6 | // placeholders, that we flush here. |
921 | 6 | resolveForwardRefsAndPlaceholders(Placeholders); |
922 | 6 | upgradeDebugInfo(); |
923 | 6 | // Return at the beginning of the block, since it is easy to skip it |
924 | 6 | // entirely from there. |
925 | 6 | Stream.ReadBlockEnd(); // Pop the abbrev block context. |
926 | 6 | if (Error Err = IndexCursor.JumpToBit(EntryPos)) |
927 | 0 | return Err; |
928 | 6 | if (Error Err = Stream.SkipBlock()) { |
929 | 6 | // FIXME this drops the error on the floor, which |
930 | 6 | // ThinLTO/X86/debuginfo-cu-import.ll relies on. |
931 | 6 | consumeError(std::move(Err)); |
932 | 6 | return Error::success(); |
933 | 6 | } |
934 | 0 | return Error::success(); |
935 | 0 | } |
936 | 32 | // Couldn't load an index, fallback to loading all the block "old-style". |
937 | 32 | } |
938 | 36.1k | |
939 | 36.1k | unsigned NextMetadataNo = MetadataList.size(); |
940 | 36.1k | |
941 | 36.1k | // Read all the records. |
942 | 425k | while (true425k ) { |
943 | 425k | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
944 | 425k | if (!MaybeEntry) |
945 | 0 | return MaybeEntry.takeError(); |
946 | 425k | BitstreamEntry Entry = MaybeEntry.get(); |
947 | 425k | |
948 | 425k | switch (Entry.Kind) { |
949 | 425k | case BitstreamEntry::SubBlock: // Handled for us already. |
950 | 0 | case BitstreamEntry::Error: |
951 | 0 | return error("Malformed block"); |
952 | 36.1k | case BitstreamEntry::EndBlock: |
953 | 36.1k | resolveForwardRefsAndPlaceholders(Placeholders); |
954 | 36.1k | upgradeDebugInfo(); |
955 | 36.1k | return Error::success(); |
956 | 389k | case BitstreamEntry::Record: |
957 | 389k | // The interesting case. |
958 | 389k | break; |
959 | 389k | } |
960 | 389k | |
961 | 389k | // Read a record. |
962 | 389k | Record.clear(); |
963 | 389k | StringRef Blob; |
964 | 389k | ++NumMDRecordLoaded; |
965 | 389k | if (Expected<unsigned> MaybeCode = |
966 | 389k | Stream.readRecord(Entry.ID, Record, &Blob)) { |
967 | 389k | if (Error Err = parseOneMetadata(Record, MaybeCode.get(), Placeholders, |
968 | 0 | Blob, NextMetadataNo)) |
969 | 0 | return Err; |
970 | 3 | } else |
971 | 3 | return MaybeCode.takeError(); |
972 | 389k | } |
973 | 36.1k | } |
974 | | |
975 | 76 | MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) { |
976 | 76 | ++NumMDStringLoaded; |
977 | 76 | if (Metadata *MD = MetadataList.lookup(ID)) |
978 | 3 | return cast<MDString>(MD); |
979 | 73 | auto MDS = MDString::get(Context, MDStringRef[ID]); |
980 | 73 | MetadataList.assignValue(MDS, ID); |
981 | 73 | return MDS; |
982 | 73 | } |
983 | | |
984 | | void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata( |
985 | 227 | unsigned ID, PlaceholderQueue &Placeholders) { |
986 | 227 | assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size()); |
987 | 227 | assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString"); |
988 | 227 | // Lookup first if the metadata hasn't already been loaded. |
989 | 227 | if (auto *MD = MetadataList.lookup(ID)) { |
990 | 19 | auto *N = dyn_cast_or_null<MDNode>(MD); |
991 | 19 | if (!N->isTemporary()) |
992 | 2 | return; |
993 | 225 | } |
994 | 225 | SmallVector<uint64_t, 64> Record; |
995 | 225 | StringRef Blob; |
996 | 225 | if (Error Err = IndexCursor.JumpToBit( |
997 | 0 | GlobalMetadataBitPosIndex[ID - MDStringRef.size()])) |
998 | 0 | report_fatal_error("lazyLoadOneMetadata failed jumping: " + |
999 | 0 | toString(std::move(Err))); |
1000 | 225 | Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks(); |
1001 | 225 | if (!MaybeEntry) |
1002 | 0 | // FIXME this drops the error on the floor. |
1003 | 0 | report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " + |
1004 | 0 | toString(MaybeEntry.takeError())); |
1005 | 225 | BitstreamEntry Entry = MaybeEntry.get(); |
1006 | 225 | ++NumMDRecordLoaded; |
1007 | 225 | if (Expected<unsigned> MaybeCode = |
1008 | 225 | IndexCursor.readRecord(Entry.ID, Record, &Blob)) { |
1009 | 225 | if (Error Err = |
1010 | 0 | parseOneMetadata(Record, MaybeCode.get(), Placeholders, Blob, ID)) |
1011 | 0 | report_fatal_error("Can't lazyload MD, parseOneMetadata: " + |
1012 | 0 | toString(std::move(Err))); |
1013 | 0 | } else |
1014 | 0 | report_fatal_error("Can't lazyload MD: " + toString(MaybeCode.takeError())); |
1015 | 225 | } |
1016 | | |
1017 | | /// Ensure that all forward-references and placeholders are resolved. |
1018 | | /// Iteratively lazy-loading metadata on-demand if needed. |
1019 | | void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders( |
1020 | 192k | PlaceholderQueue &Placeholders) { |
1021 | 192k | DenseSet<unsigned> Temporaries; |
1022 | 192k | while (1) { |
1023 | 192k | // Populate Temporaries with the placeholders that haven't been loaded yet. |
1024 | 192k | Placeholders.getTemporaries(MetadataList, Temporaries); |
1025 | 192k | |
1026 | 192k | // If we don't have any temporary, or FwdReference, we're done! |
1027 | 192k | if (Temporaries.empty() && !MetadataList.hasFwdRefs()192k ) |
1028 | 192k | break; |
1029 | 17 | |
1030 | 17 | // First, load all the temporaries. This can add new placeholders or |
1031 | 17 | // forward references. |
1032 | 17 | for (auto ID : Temporaries) |
1033 | 16 | lazyLoadOneMetadata(ID, Placeholders); |
1034 | 17 | Temporaries.clear(); |
1035 | 17 | |
1036 | 17 | // Second, load the forward-references. This can also add new placeholders |
1037 | 17 | // or forward references. |
1038 | 34 | while (MetadataList.hasFwdRefs()) |
1039 | 17 | lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders); |
1040 | 17 | } |
1041 | 192k | // At this point we don't have any forward reference remaining, or temporary |
1042 | 192k | // that haven't been loaded. We can safely drop RAUW support and mark cycles |
1043 | 192k | // as resolved. |
1044 | 192k | MetadataList.tryToResolveCycles(); |
1045 | 192k | |
1046 | 192k | // Finally, everything is in place, we can replace the placeholders operands |
1047 | 192k | // with the final node they refer to. |
1048 | 192k | Placeholders.flush(MetadataList); |
1049 | 192k | } |
1050 | | |
1051 | | Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( |
1052 | | SmallVectorImpl<uint64_t> &Record, unsigned Code, |
1053 | 389k | PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) { |
1054 | 389k | |
1055 | 389k | bool IsDistinct = false; |
1056 | 1.13M | auto getMD = [&](unsigned ID) -> Metadata * { |
1057 | 1.13M | if (ID < MDStringRef.size()) |
1058 | 76 | return lazyLoadOneMDString(ID); |
1059 | 1.13M | if (!IsDistinct) { |
1060 | 1.08M | if (auto *MD = MetadataList.lookup(ID)) |
1061 | 1.08M | return MD; |
1062 | 1.06k | // If lazy-loading is enabled, we try recursively to load the operand |
1063 | 1.06k | // instead of creating a temporary. |
1064 | 1.06k | if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { |
1065 | 190 | // Create a temporary for the node that is referencing the operand we |
1066 | 190 | // will lazy-load. It is needed before recursing in case there are |
1067 | 190 | // uniquing cycles. |
1068 | 190 | MetadataList.getMetadataFwdRef(NextMetadataNo); |
1069 | 190 | lazyLoadOneMetadata(ID, Placeholders); |
1070 | 190 | return MetadataList.lookup(ID); |
1071 | 190 | } |
1072 | 878 | // Return a temporary. |
1073 | 878 | return MetadataList.getMetadataFwdRef(ID); |
1074 | 878 | } |
1075 | 51.6k | if (auto *MD = MetadataList.getMetadataIfResolved(ID)) |
1076 | 13.2k | return MD; |
1077 | 38.4k | return &Placeholders.getPlaceholderOp(ID); |
1078 | 38.4k | }; |
1079 | 1.14M | auto getMDOrNull = [&](unsigned ID) -> Metadata * { |
1080 | 1.14M | if (ID) |
1081 | 1.13M | return getMD(ID - 1); |
1082 | 8.49k | return nullptr; |
1083 | 8.49k | }; |
1084 | 389k | auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * { |
1085 | 441 | if (ID) |
1086 | 6 | return MetadataList.getMetadataFwdRef(ID - 1); |
1087 | 435 | return nullptr; |
1088 | 435 | }; |
1089 | 389k | auto getMDString = [&](unsigned ID) -> MDString * { |
1090 | 5.01k | // This requires that the ID is not really a forward reference. In |
1091 | 5.01k | // particular, the MDString must already have been resolved. |
1092 | 5.01k | auto MDS = getMDOrNull(ID); |
1093 | 5.01k | return cast_or_null<MDString>(MDS); |
1094 | 5.01k | }; |
1095 | 389k | |
1096 | 389k | // Support for old type refs. |
1097 | 389k | auto getDITypeRefOrNull = [&](unsigned ID) { |
1098 | 2.64k | return MetadataList.upgradeTypeRef(getMDOrNull(ID)); |
1099 | 2.64k | }; |
1100 | 389k | |
1101 | 389k | #define GET_OR_DISTINCT(CLASS, ARGS) \ |
1102 | 389k | (IsDistinct ? 2.87k CLASS::getDistinct 639 ARGS1.25k : CLASS::get 2.23k ARGS809 ) |
1103 | 389k | |
1104 | 389k | switch (Code) { |
1105 | 389k | default: // Default behavior: ignore. |
1106 | 351 | break; |
1107 | 389k | case bitc::METADATA_NAME: { |
1108 | 10.7k | // Read name of the named metadata. |
1109 | 10.7k | SmallString<8> Name(Record.begin(), Record.end()); |
1110 | 10.7k | Record.clear(); |
1111 | 10.7k | Expected<unsigned> MaybeCode = Stream.ReadCode(); |
1112 | 10.7k | if (!MaybeCode) |
1113 | 0 | return MaybeCode.takeError(); |
1114 | 10.7k | Code = MaybeCode.get(); |
1115 | 10.7k | |
1116 | 10.7k | ++NumMDRecordLoaded; |
1117 | 10.7k | if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) { |
1118 | 10.7k | if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE) |
1119 | 0 | return error("METADATA_NAME not followed by METADATA_NAMED_NODE"); |
1120 | 0 | } else |
1121 | 0 | return MaybeNextBitCode.takeError(); |
1122 | 10.7k | |
1123 | 10.7k | // Read named metadata elements. |
1124 | 10.7k | unsigned Size = Record.size(); |
1125 | 10.7k | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); |
1126 | 68.2k | for (unsigned i = 0; i != Size; ++i57.5k ) { |
1127 | 57.5k | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); |
1128 | 57.5k | if (!MD) |
1129 | 0 | return error("Invalid named metadata: expect fwd ref to MDNode"); |
1130 | 57.5k | NMD->addOperand(MD); |
1131 | 57.5k | } |
1132 | 10.7k | break; |
1133 | 10.7k | } |
1134 | 10.7k | case bitc::METADATA_OLD_FN_NODE: { |
1135 | 598 | // FIXME: Remove in 4.0. |
1136 | 598 | // This is a LocalAsMetadata record, the only type of function-local |
1137 | 598 | // metadata. |
1138 | 598 | if (Record.size() % 2 == 1) |
1139 | 0 | return error("Invalid record"); |
1140 | 598 | |
1141 | 598 | // If this isn't a LocalAsMetadata record, we're dropping it. This used |
1142 | 598 | // to be legal, but there's no upgrade path. |
1143 | 598 | auto dropRecord = [&] { |
1144 | 2 | MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo); |
1145 | 2 | NextMetadataNo++; |
1146 | 2 | }; |
1147 | 598 | if (Record.size() != 2) { |
1148 | 1 | dropRecord(); |
1149 | 1 | break; |
1150 | 1 | } |
1151 | 597 | |
1152 | 597 | Type *Ty = getTypeByID(Record[0]); |
1153 | 597 | if (Ty->isMetadataTy() || Ty->isVoidTy()596 ) { |
1154 | 1 | dropRecord(); |
1155 | 1 | break; |
1156 | 1 | } |
1157 | 596 | |
1158 | 596 | MetadataList.assignValue( |
1159 | 596 | LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
1160 | 596 | NextMetadataNo); |
1161 | 596 | NextMetadataNo++; |
1162 | 596 | break; |
1163 | 596 | } |
1164 | 1.34k | case bitc::METADATA_OLD_NODE: { |
1165 | 1.34k | // FIXME: Remove in 4.0. |
1166 | 1.34k | if (Record.size() % 2 == 1) |
1167 | 0 | return error("Invalid record"); |
1168 | 1.34k | |
1169 | 1.34k | unsigned Size = Record.size(); |
1170 | 1.34k | SmallVector<Metadata *, 8> Elts; |
1171 | 11.8k | for (unsigned i = 0; i != Size; i += 210.4k ) { |
1172 | 10.4k | Type *Ty = getTypeByID(Record[i]); |
1173 | 10.4k | if (!Ty) |
1174 | 0 | return error("Invalid record"); |
1175 | 10.4k | if (Ty->isMetadataTy()) |
1176 | 5.60k | Elts.push_back(getMD(Record[i + 1])); |
1177 | 4.88k | else if (!Ty->isVoidTy()) { |
1178 | 4.62k | auto *MD = |
1179 | 4.62k | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); |
1180 | 4.62k | assert(isa<ConstantAsMetadata>(MD) && |
1181 | 4.62k | "Expected non-function-local metadata"); |
1182 | 4.62k | Elts.push_back(MD); |
1183 | 4.62k | } else |
1184 | 257 | Elts.push_back(nullptr); |
1185 | 10.4k | } |
1186 | 1.34k | MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo); |
1187 | 1.34k | NextMetadataNo++; |
1188 | 1.34k | break; |
1189 | 1.34k | } |
1190 | 79.4k | case bitc::METADATA_VALUE: { |
1191 | 79.4k | if (Record.size() != 2) |
1192 | 0 | return error("Invalid record"); |
1193 | 79.4k | |
1194 | 79.4k | Type *Ty = getTypeByID(Record[0]); |
1195 | 79.4k | if (Ty->isMetadataTy()79.4k || Ty->isVoidTy()) |
1196 | 0 | return error("Invalid record"); |
1197 | 79.4k | |
1198 | 79.4k | MetadataList.assignValue( |
1199 | 79.4k | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
1200 | 79.4k | NextMetadataNo); |
1201 | 79.4k | NextMetadataNo++; |
1202 | 79.4k | break; |
1203 | 79.4k | } |
1204 | 79.4k | case bitc::METADATA_DISTINCT_NODE: |
1205 | 19.0k | IsDistinct = true; |
1206 | 19.0k | LLVM_FALLTHROUGH; |
1207 | 278k | case bitc::METADATA_NODE: { |
1208 | 278k | SmallVector<Metadata *, 8> Elts; |
1209 | 278k | Elts.reserve(Record.size()); |
1210 | 278k | for (unsigned ID : Record) |
1211 | 1.12M | Elts.push_back(getMDOrNull(ID)); |
1212 | 278k | MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)19.0k |
1213 | 278k | : MDNode::get(Context, Elts)259k , |
1214 | 278k | NextMetadataNo); |
1215 | 278k | NextMetadataNo++; |
1216 | 278k | break; |
1217 | 19.0k | } |
1218 | 19.0k | case bitc::METADATA_LOCATION: { |
1219 | 55 | if (Record.size() != 5 && Record.size() != 649 ) |
1220 | 0 | return error("Invalid record"); |
1221 | 55 | |
1222 | 55 | IsDistinct = Record[0]; |
1223 | 55 | unsigned Line = Record[1]; |
1224 | 55 | unsigned Column = Record[2]; |
1225 | 55 | Metadata *Scope = getMD(Record[3]); |
1226 | 55 | Metadata *InlinedAt = getMDOrNull(Record[4]); |
1227 | 55 | bool ImplicitCode = Record.size() == 6 && Record[5]49 ; |
1228 | 55 | MetadataList.assignValue( |
1229 | 55 | GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt, |
1230 | 55 | ImplicitCode)), |
1231 | 55 | NextMetadataNo); |
1232 | 55 | NextMetadataNo++; |
1233 | 55 | break; |
1234 | 55 | } |
1235 | 55 | case bitc::METADATA_GENERIC_DEBUG: { |
1236 | 20 | if (Record.size() < 4) |
1237 | 0 | return error("Invalid record"); |
1238 | 20 | |
1239 | 20 | IsDistinct = Record[0]; |
1240 | 20 | unsigned Tag = Record[1]; |
1241 | 20 | unsigned Version = Record[2]; |
1242 | 20 | |
1243 | 20 | if (Tag >= 1u << 16 || Version != 0) |
1244 | 0 | return error("Invalid record"); |
1245 | 20 | |
1246 | 20 | auto *Header = getMDString(Record[3]); |
1247 | 20 | SmallVector<Metadata *, 8> DwarfOps; |
1248 | 35 | for (unsigned I = 4, E = Record.size(); I != E; ++I15 ) |
1249 | 15 | DwarfOps.push_back(getMDOrNull(Record[I])); |
1250 | 20 | MetadataList.assignValue( |
1251 | 20 | GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)), |
1252 | 20 | NextMetadataNo); |
1253 | 20 | NextMetadataNo++; |
1254 | 20 | break; |
1255 | 20 | } |
1256 | 43 | case bitc::METADATA_SUBRANGE: { |
1257 | 43 | Metadata *Val = nullptr; |
1258 | 43 | // Operand 'count' is interpreted as: |
1259 | 43 | // - Signed integer (version 0) |
1260 | 43 | // - Metadata node (version 1) |
1261 | 43 | switch (Record[0] >> 1) { |
1262 | 43 | case 0: |
1263 | 3 | Val = GET_OR_DISTINCT(DISubrange, |
1264 | 3 | (Context, Record[1], unrotateSign(Record.back()))); |
1265 | 3 | break; |
1266 | 43 | case 1: |
1267 | 40 | Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]), |
1268 | 40 | unrotateSign(Record.back()))); |
1269 | 40 | break; |
1270 | 43 | default: |
1271 | 0 | return error("Invalid record: Unsupported version of DISubrange"); |
1272 | 43 | } |
1273 | 43 | |
1274 | 43 | MetadataList.assignValue(Val, NextMetadataNo); |
1275 | 43 | IsDistinct = Record[0] & 1; |
1276 | 43 | NextMetadataNo++; |
1277 | 43 | break; |
1278 | 43 | } |
1279 | 43 | case bitc::METADATA_ENUMERATOR: { |
1280 | 42 | if (Record.size() != 3) |
1281 | 0 | return error("Invalid record"); |
1282 | 42 | |
1283 | 42 | IsDistinct = Record[0] & 1; |
1284 | 42 | bool IsUnsigned = Record[0] & 2; |
1285 | 42 | MetadataList.assignValue( |
1286 | 42 | GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]), |
1287 | 42 | IsUnsigned, getMDString(Record[2]))), |
1288 | 42 | NextMetadataNo); |
1289 | 42 | NextMetadataNo++; |
1290 | 42 | break; |
1291 | 42 | } |
1292 | 228 | case bitc::METADATA_BASIC_TYPE: { |
1293 | 228 | if (Record.size() < 6 || Record.size() > 7) |
1294 | 0 | return error("Invalid record"); |
1295 | 228 | |
1296 | 228 | IsDistinct = Record[0]; |
1297 | 228 | DINode::DIFlags Flags = (Record.size() > 6) ? |
1298 | 206 | static_cast<DINode::DIFlags>(Record[6]) : DINode::FlagZero22 ; |
1299 | 228 | |
1300 | 228 | MetadataList.assignValue( |
1301 | 228 | GET_OR_DISTINCT(DIBasicType, |
1302 | 228 | (Context, Record[1], getMDString(Record[2]), Record[3], |
1303 | 228 | Record[4], Record[5], Flags)), |
1304 | 228 | NextMetadataNo); |
1305 | 228 | NextMetadataNo++; |
1306 | 228 | break; |
1307 | 228 | } |
1308 | 228 | case bitc::METADATA_DERIVED_TYPE: { |
1309 | 190 | if (Record.size() < 12 || Record.size() > 13) |
1310 | 0 | return error("Invalid record"); |
1311 | 190 | |
1312 | 190 | // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means |
1313 | 190 | // that there is no DWARF address space associated with DIDerivedType. |
1314 | 190 | Optional<unsigned> DWARFAddressSpace; |
1315 | 190 | if (Record.size() > 12 && Record[12]164 ) |
1316 | 6 | DWARFAddressSpace = Record[12] - 1; |
1317 | 190 | |
1318 | 190 | IsDistinct = Record[0]; |
1319 | 190 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); |
1320 | 190 | MetadataList.assignValue( |
1321 | 190 | GET_OR_DISTINCT(DIDerivedType, |
1322 | 190 | (Context, Record[1], getMDString(Record[2]), |
1323 | 190 | getMDOrNull(Record[3]), Record[4], |
1324 | 190 | getDITypeRefOrNull(Record[5]), |
1325 | 190 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
1326 | 190 | Record[9], DWARFAddressSpace, Flags, |
1327 | 190 | getDITypeRefOrNull(Record[11]))), |
1328 | 190 | NextMetadataNo); |
1329 | 190 | NextMetadataNo++; |
1330 | 190 | break; |
1331 | 190 | } |
1332 | 255 | case bitc::METADATA_COMPOSITE_TYPE: { |
1333 | 255 | if (Record.size() < 16 || Record.size() > 17) |
1334 | 0 | return error("Invalid record"); |
1335 | 255 | |
1336 | 255 | // If we have a UUID and this is not a forward declaration, lookup the |
1337 | 255 | // mapping. |
1338 | 255 | IsDistinct = Record[0] & 0x1; |
1339 | 255 | bool IsNotUsedInTypeRef = Record[0] >= 2; |
1340 | 255 | unsigned Tag = Record[1]; |
1341 | 255 | MDString *Name = getMDString(Record[2]); |
1342 | 255 | Metadata *File = getMDOrNull(Record[3]); |
1343 | 255 | unsigned Line = Record[4]; |
1344 | 255 | Metadata *Scope = getDITypeRefOrNull(Record[5]); |
1345 | 255 | Metadata *BaseType = nullptr; |
1346 | 255 | uint64_t SizeInBits = Record[7]; |
1347 | 255 | if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
1348 | 0 | return error("Alignment value is too large"); |
1349 | 255 | uint32_t AlignInBits = Record[8]; |
1350 | 255 | uint64_t OffsetInBits = 0; |
1351 | 255 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); |
1352 | 255 | Metadata *Elements = nullptr; |
1353 | 255 | unsigned RuntimeLang = Record[12]; |
1354 | 255 | Metadata *VTableHolder = nullptr; |
1355 | 255 | Metadata *TemplateParams = nullptr; |
1356 | 255 | Metadata *Discriminator = nullptr; |
1357 | 255 | auto *Identifier = getMDString(Record[15]); |
1358 | 255 | // If this module is being parsed so that it can be ThinLTO imported |
1359 | 255 | // into another module, composite types only need to be imported |
1360 | 255 | // as type declarations (unless full type definitions requested). |
1361 | 255 | // Create type declarations up front to save memory. Also, buildODRType |
1362 | 255 | // handles the case where this is type ODRed with a definition needed |
1363 | 255 | // by the importing module, in which case the existing definition is |
1364 | 255 | // used. |
1365 | 255 | if (IsImporting && !ImportFullTypeDefinitions29 && Identifier21 && |
1366 | 255 | (21 Tag == dwarf::DW_TAG_enumeration_type21 || |
1367 | 21 | Tag == dwarf::DW_TAG_class_type18 || |
1368 | 21 | Tag == dwarf::DW_TAG_structure_type13 || |
1369 | 21 | Tag == dwarf::DW_TAG_union_type2 )) { |
1370 | 21 | Flags = Flags | DINode::FlagFwdDecl; |
1371 | 234 | } else { |
1372 | 234 | BaseType = getDITypeRefOrNull(Record[6]); |
1373 | 234 | OffsetInBits = Record[9]; |
1374 | 234 | Elements = getMDOrNull(Record[11]); |
1375 | 234 | VTableHolder = getDITypeRefOrNull(Record[13]); |
1376 | 234 | TemplateParams = getMDOrNull(Record[14]); |
1377 | 234 | if (Record.size() > 16) |
1378 | 216 | Discriminator = getMDOrNull(Record[16]); |
1379 | 234 | } |
1380 | 255 | DICompositeType *CT = nullptr; |
1381 | 255 | if (Identifier) |
1382 | 172 | CT = DICompositeType::buildODRType( |
1383 | 172 | Context, *Identifier, Tag, Name, File, Line, Scope, BaseType, |
1384 | 172 | SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
1385 | 172 | VTableHolder, TemplateParams, Discriminator); |
1386 | 255 | |
1387 | 255 | // Create a node if we didn't get a lazy ODR type. |
1388 | 255 | if (!CT) |
1389 | 182 | CT = GET_OR_DISTINCT(DICompositeType, |
1390 | 255 | (Context, Tag, Name, File, Line, Scope, BaseType, |
1391 | 255 | SizeInBits, AlignInBits, OffsetInBits, Flags, |
1392 | 255 | Elements, RuntimeLang, VTableHolder, TemplateParams, |
1393 | 255 | Identifier, Discriminator)); |
1394 | 255 | if (!IsNotUsedInTypeRef && Identifier13 ) |
1395 | 6 | MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT)); |
1396 | 255 | |
1397 | 255 | MetadataList.assignValue(CT, NextMetadataNo); |
1398 | 255 | NextMetadataNo++; |
1399 | 255 | break; |
1400 | 255 | } |
1401 | 283 | case bitc::METADATA_SUBROUTINE_TYPE: { |
1402 | 283 | if (Record.size() < 3 || Record.size() > 4) |
1403 | 0 | return error("Invalid record"); |
1404 | 283 | bool IsOldTypeRefArray = Record[0] < 2; |
1405 | 283 | unsigned CC = (Record.size() > 3) ? Record[3]263 : 020 ; |
1406 | 283 | |
1407 | 283 | IsDistinct = Record[0] & 0x1; |
1408 | 283 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]); |
1409 | 283 | Metadata *Types = getMDOrNull(Record[2]); |
1410 | 283 | if (LLVM_UNLIKELY(IsOldTypeRefArray)) |
1411 | 283 | Types = MetadataList.upgradeTypeRefArray(Types)18 ; |
1412 | 283 | |
1413 | 283 | MetadataList.assignValue( |
1414 | 283 | GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)), |
1415 | 283 | NextMetadataNo); |
1416 | 283 | NextMetadataNo++; |
1417 | 283 | break; |
1418 | 283 | } |
1419 | 283 | |
1420 | 283 | case bitc::METADATA_MODULE: { |
1421 | 12 | if (Record.size() != 6) |
1422 | 0 | return error("Invalid record"); |
1423 | 12 | |
1424 | 12 | IsDistinct = Record[0]; |
1425 | 12 | MetadataList.assignValue( |
1426 | 12 | GET_OR_DISTINCT(DIModule, |
1427 | 12 | (Context, getMDOrNull(Record[1]), |
1428 | 12 | getMDString(Record[2]), getMDString(Record[3]), |
1429 | 12 | getMDString(Record[4]), getMDString(Record[5]))), |
1430 | 12 | NextMetadataNo); |
1431 | 12 | NextMetadataNo++; |
1432 | 12 | break; |
1433 | 12 | } |
1434 | 12 | |
1435 | 519 | case bitc::METADATA_FILE: { |
1436 | 519 | if (Record.size() != 3 && Record.size() != 5501 && Record.size() != 624 ) |
1437 | 0 | return error("Invalid record"); |
1438 | 519 | |
1439 | 519 | IsDistinct = Record[0]; |
1440 | 519 | Optional<DIFile::ChecksumInfo<MDString *>> Checksum; |
1441 | 519 | // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum |
1442 | 519 | // is not present. This matches up with the old internal representation, |
1443 | 519 | // and the old encoding for CSK_None in the ChecksumKind. The new |
1444 | 519 | // representation reserves the value 0 in the ChecksumKind to continue to |
1445 | 519 | // encode None in a backwards-compatible way. |
1446 | 519 | if (Record.size() > 4 && Record[3]501 && Record[4]26 ) |
1447 | 26 | Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]), |
1448 | 26 | getMDString(Record[4])); |
1449 | 519 | MetadataList.assignValue( |
1450 | 519 | GET_OR_DISTINCT( |
1451 | 519 | DIFile, |
1452 | 519 | (Context, getMDString(Record[1]), getMDString(Record[2]), Checksum, |
1453 | 519 | Record.size() > 5 ? Optional<MDString *>(getMDString(Record[5])) |
1454 | 519 | : None)), |
1455 | 519 | NextMetadataNo); |
1456 | 519 | NextMetadataNo++; |
1457 | 519 | break; |
1458 | 519 | } |
1459 | 519 | case bitc::METADATA_COMPILE_UNIT: { |
1460 | 441 | if (Record.size() < 14 || Record.size() > 19) |
1461 | 0 | return error("Invalid record"); |
1462 | 441 | |
1463 | 441 | // Ignore Record[0], which indicates whether this compile unit is |
1464 | 441 | // distinct. It's always distinct. |
1465 | 441 | IsDistinct = true; |
1466 | 441 | auto *CU = DICompileUnit::getDistinct( |
1467 | 441 | Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]), |
1468 | 441 | Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]), |
1469 | 441 | Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]), |
1470 | 441 | getMDOrNull(Record[12]), getMDOrNull(Record[13]), |
1471 | 441 | Record.size() <= 15 ? nullptr7 : getMDOrNull(Record[15])434 , |
1472 | 441 | Record.size() <= 14 ? 01 : Record[14]440 , |
1473 | 441 | Record.size() <= 16 ? true12 : Record[16]429 , |
1474 | 441 | Record.size() <= 17 ? false18 : Record[17]423 , |
1475 | 441 | Record.size() <= 18 ? 034 : Record[18]407 , |
1476 | 441 | Record.size() <= 19 ? 0440 : Record[19]1 ); |
1477 | 441 | |
1478 | 441 | MetadataList.assignValue(CU, NextMetadataNo); |
1479 | 441 | NextMetadataNo++; |
1480 | 441 | |
1481 | 441 | // Move the Upgrade the list of subprograms. |
1482 | 441 | if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11])) |
1483 | 6 | CUSubprograms.push_back({CU, SPs}); |
1484 | 441 | break; |
1485 | 441 | } |
1486 | 495 | case bitc::METADATA_SUBPROGRAM: { |
1487 | 495 | if (Record.size() < 18 || Record.size() > 21) |
1488 | 0 | return error("Invalid record"); |
1489 | 495 | |
1490 | 495 | bool HasSPFlags = Record[0] & 4; |
1491 | 495 | |
1492 | 495 | DINode::DIFlags Flags; |
1493 | 495 | DISubprogram::DISPFlags SPFlags; |
1494 | 495 | if (!HasSPFlags) |
1495 | 51 | Flags = static_cast<DINode::DIFlags>(Record[11 + 2]); |
1496 | 444 | else { |
1497 | 444 | Flags = static_cast<DINode::DIFlags>(Record[11]); |
1498 | 444 | SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]); |
1499 | 444 | } |
1500 | 495 | |
1501 | 495 | // Support for old metadata when |
1502 | 495 | // subprogram specific flags are placed in DIFlags. |
1503 | 495 | const unsigned DIFlagMainSubprogram = 1 << 21; |
1504 | 495 | bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram; |
1505 | 495 | if (HasOldMainSubprogramFlag) |
1506 | 1 | // Remove old DIFlagMainSubprogram from DIFlags. |
1507 | 1 | // Note: This assumes that any future use of bit 21 defaults to it |
1508 | 1 | // being 0. |
1509 | 1 | Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram); |
1510 | 495 | |
1511 | 495 | if (HasOldMainSubprogramFlag && HasSPFlags1 ) |
1512 | 1 | SPFlags |= DISubprogram::SPFlagMainSubprogram; |
1513 | 494 | else if (!HasSPFlags) |
1514 | 51 | SPFlags = DISubprogram::toSPFlags( |
1515 | 51 | /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8], |
1516 | 51 | /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11], |
1517 | 51 | /*DIFlagMainSubprogram*/HasOldMainSubprogramFlag); |
1518 | 495 | |
1519 | 495 | // All definitions should be distinct. |
1520 | 495 | IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition)94 ; |
1521 | 495 | // Version 1 has a Function as Record[15]. |
1522 | 495 | // Version 2 has removed Record[15]. |
1523 | 495 | // Version 3 has the Unit as Record[15]. |
1524 | 495 | // Version 4 added thisAdjustment. |
1525 | 495 | // Version 5 repacked flags into DISPFlags, changing many element numbers. |
1526 | 495 | bool HasUnit = Record[0] & 2; |
1527 | 495 | if (!HasSPFlags && HasUnit51 && Record.size() < 1924 ) |
1528 | 0 | return error("Invalid record"); |
1529 | 495 | if (HasSPFlags && !HasUnit444 ) |
1530 | 0 | return error("Invalid record"); |
1531 | 495 | // Accommodate older formats. |
1532 | 495 | bool HasFn = false; |
1533 | 495 | bool HasThisAdj = true; |
1534 | 495 | bool HasThrownTypes = true; |
1535 | 495 | unsigned OffsetA = 0; |
1536 | 495 | unsigned OffsetB = 0; |
1537 | 495 | if (!HasSPFlags) { |
1538 | 51 | OffsetA = 2; |
1539 | 51 | OffsetB = 2; |
1540 | 51 | if (Record.size() >= 19) { |
1541 | 47 | HasFn = !HasUnit; |
1542 | 47 | OffsetB++; |
1543 | 47 | } |
1544 | 51 | HasThisAdj = Record.size() >= 20; |
1545 | 51 | HasThrownTypes = Record.size() >= 21; |
1546 | 51 | } |
1547 | 495 | Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]); |
1548 | 495 | DISubprogram *SP = GET_OR_DISTINCT( |
1549 | 495 | DISubprogram, |
1550 | 495 | (Context, |
1551 | 495 | getDITypeRefOrNull(Record[1]), // scope |
1552 | 495 | getMDString(Record[2]), // name |
1553 | 495 | getMDString(Record[3]), // linkageName |
1554 | 495 | getMDOrNull(Record[4]), // file |
1555 | 495 | Record[5], // line |
1556 | 495 | getMDOrNull(Record[6]), // type |
1557 | 495 | Record[7 + OffsetA], // scopeLine |
1558 | 495 | getDITypeRefOrNull(Record[8 + OffsetA]), // containingType |
1559 | 495 | Record[10 + OffsetA], // virtualIndex |
1560 | 495 | HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment |
1561 | 495 | Flags, // flags |
1562 | 495 | SPFlags, // SPFlags |
1563 | 495 | HasUnit ? CUorFn : nullptr, // unit |
1564 | 495 | getMDOrNull(Record[13 + OffsetB]), // templateParams |
1565 | 495 | getMDOrNull(Record[14 + OffsetB]), // declaration |
1566 | 495 | getMDOrNull(Record[15 + OffsetB]), // retainedNodes |
1567 | 495 | HasThrownTypes ? getMDOrNull(Record[17 + OffsetB]) |
1568 | 495 | : nullptr // thrownTypes |
1569 | 495 | )); |
1570 | 495 | MetadataList.assignValue(SP, NextMetadataNo); |
1571 | 495 | NextMetadataNo++; |
1572 | 495 | |
1573 | 495 | // Upgrade sp->function mapping to function->sp mapping. |
1574 | 495 | if (HasFn) { |
1575 | 23 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn)) |
1576 | 19 | if (auto *F = dyn_cast<Function>(CMD->getValue())) { |
1577 | 19 | if (F->isMaterializable()) |
1578 | 19 | // Defer until materialized; unmaterialized functions may not have |
1579 | 19 | // metadata. |
1580 | 19 | FunctionsWithSPs[F] = SP; |
1581 | 0 | else if (!F->empty()) |
1582 | 0 | F->setSubprogram(SP); |
1583 | 19 | } |
1584 | 23 | } |
1585 | 495 | break; |
1586 | 495 | } |
1587 | 495 | case bitc::METADATA_LEXICAL_BLOCK: { |
1588 | 107 | if (Record.size() != 5) |
1589 | 0 | return error("Invalid record"); |
1590 | 107 | |
1591 | 107 | IsDistinct = Record[0]; |
1592 | 107 | MetadataList.assignValue( |
1593 | 107 | GET_OR_DISTINCT(DILexicalBlock, |
1594 | 107 | (Context, getMDOrNull(Record[1]), |
1595 | 107 | getMDOrNull(Record[2]), Record[3], Record[4])), |
1596 | 107 | NextMetadataNo); |
1597 | 107 | NextMetadataNo++; |
1598 | 107 | break; |
1599 | 107 | } |
1600 | 107 | case bitc::METADATA_LEXICAL_BLOCK_FILE: { |
1601 | 42 | if (Record.size() != 4) |
1602 | 0 | return error("Invalid record"); |
1603 | 42 | |
1604 | 42 | IsDistinct = Record[0]; |
1605 | 42 | MetadataList.assignValue( |
1606 | 42 | GET_OR_DISTINCT(DILexicalBlockFile, |
1607 | 42 | (Context, getMDOrNull(Record[1]), |
1608 | 42 | getMDOrNull(Record[2]), Record[3])), |
1609 | 42 | NextMetadataNo); |
1610 | 42 | NextMetadataNo++; |
1611 | 42 | break; |
1612 | 42 | } |
1613 | 42 | case bitc::METADATA_COMMON_BLOCK: { |
1614 | 2 | IsDistinct = Record[0] & 1; |
1615 | 2 | MetadataList.assignValue( |
1616 | 2 | GET_OR_DISTINCT(DICommonBlock, |
1617 | 2 | (Context, getMDOrNull(Record[1]), |
1618 | 2 | getMDOrNull(Record[2]), getMDString(Record[3]), |
1619 | 2 | getMDOrNull(Record[4]), Record[5])), |
1620 | 2 | NextMetadataNo); |
1621 | 2 | NextMetadataNo++; |
1622 | 2 | break; |
1623 | 42 | } |
1624 | 42 | case bitc::METADATA_NAMESPACE: { |
1625 | 36 | // Newer versions of DINamespace dropped file and line. |
1626 | 36 | MDString *Name; |
1627 | 36 | if (Record.size() == 3) |
1628 | 35 | Name = getMDString(Record[2]); |
1629 | 1 | else if (Record.size() == 5) |
1630 | 1 | Name = getMDString(Record[3]); |
1631 | 0 | else |
1632 | 0 | return error("Invalid record"); |
1633 | 36 | |
1634 | 36 | IsDistinct = Record[0] & 1; |
1635 | 36 | bool ExportSymbols = Record[0] & 2; |
1636 | 36 | MetadataList.assignValue( |
1637 | 36 | GET_OR_DISTINCT(DINamespace, |
1638 | 36 | (Context, getMDOrNull(Record[1]), Name, ExportSymbols)), |
1639 | 36 | NextMetadataNo); |
1640 | 36 | NextMetadataNo++; |
1641 | 36 | break; |
1642 | 36 | } |
1643 | 36 | case bitc::METADATA_MACRO: { |
1644 | 13 | if (Record.size() != 5) |
1645 | 0 | return error("Invalid record"); |
1646 | 13 | |
1647 | 13 | IsDistinct = Record[0]; |
1648 | 13 | MetadataList.assignValue( |
1649 | 13 | GET_OR_DISTINCT(DIMacro, |
1650 | 13 | (Context, Record[1], Record[2], getMDString(Record[3]), |
1651 | 13 | getMDString(Record[4]))), |
1652 | 13 | NextMetadataNo); |
1653 | 13 | NextMetadataNo++; |
1654 | 13 | break; |
1655 | 13 | } |
1656 | 19 | case bitc::METADATA_MACRO_FILE: { |
1657 | 19 | if (Record.size() != 5) |
1658 | 0 | return error("Invalid record"); |
1659 | 19 | |
1660 | 19 | IsDistinct = Record[0]; |
1661 | 19 | MetadataList.assignValue( |
1662 | 19 | GET_OR_DISTINCT(DIMacroFile, |
1663 | 19 | (Context, Record[1], Record[2], getMDOrNull(Record[3]), |
1664 | 19 | getMDOrNull(Record[4]))), |
1665 | 19 | NextMetadataNo); |
1666 | 19 | NextMetadataNo++; |
1667 | 19 | break; |
1668 | 19 | } |
1669 | 30 | case bitc::METADATA_TEMPLATE_TYPE: { |
1670 | 30 | if (Record.size() != 3) |
1671 | 0 | return error("Invalid record"); |
1672 | 30 | |
1673 | 30 | IsDistinct = Record[0]; |
1674 | 30 | MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter, |
1675 | 30 | (Context, getMDString(Record[1]), |
1676 | 30 | getDITypeRefOrNull(Record[2]))), |
1677 | 30 | NextMetadataNo); |
1678 | 30 | NextMetadataNo++; |
1679 | 30 | break; |
1680 | 30 | } |
1681 | 34 | case bitc::METADATA_TEMPLATE_VALUE: { |
1682 | 34 | if (Record.size() != 5) |
1683 | 0 | return error("Invalid record"); |
1684 | 34 | |
1685 | 34 | IsDistinct = Record[0]; |
1686 | 34 | MetadataList.assignValue( |
1687 | 34 | GET_OR_DISTINCT(DITemplateValueParameter, |
1688 | 34 | (Context, Record[1], getMDString(Record[2]), |
1689 | 34 | getDITypeRefOrNull(Record[3]), |
1690 | 34 | getMDOrNull(Record[4]))), |
1691 | 34 | NextMetadataNo); |
1692 | 34 | NextMetadataNo++; |
1693 | 34 | break; |
1694 | 34 | } |
1695 | 102 | case bitc::METADATA_GLOBAL_VAR: { |
1696 | 102 | if (Record.size() < 11 || Record.size() > 13) |
1697 | 0 | return error("Invalid record"); |
1698 | 102 | |
1699 | 102 | IsDistinct = Record[0] & 1; |
1700 | 102 | unsigned Version = Record[0] >> 1; |
1701 | 102 | |
1702 | 102 | if (Version == 2) { |
1703 | 84 | MetadataList.assignValue( |
1704 | 84 | GET_OR_DISTINCT( |
1705 | 84 | DIGlobalVariable, |
1706 | 84 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
1707 | 84 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
1708 | 84 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
1709 | 84 | getMDOrNull(Record[9]), getMDOrNull(Record[10]), Record[11])), |
1710 | 84 | NextMetadataNo); |
1711 | 84 | |
1712 | 84 | NextMetadataNo++; |
1713 | 84 | } else if (18 Version == 118 ) { |
1714 | 2 | // No upgrade necessary. A null field will be introduced to indicate |
1715 | 2 | // that no parameter information is available. |
1716 | 2 | MetadataList.assignValue( |
1717 | 2 | GET_OR_DISTINCT(DIGlobalVariable, |
1718 | 2 | (Context, getMDOrNull(Record[1]), |
1719 | 2 | getMDString(Record[2]), getMDString(Record[3]), |
1720 | 2 | getMDOrNull(Record[4]), Record[5], |
1721 | 2 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
1722 | 2 | getMDOrNull(Record[10]), nullptr, Record[11])), |
1723 | 2 | NextMetadataNo); |
1724 | 2 | |
1725 | 2 | NextMetadataNo++; |
1726 | 16 | } else if (Version == 0) { |
1727 | 16 | // Upgrade old metadata, which stored a global variable reference or a |
1728 | 16 | // ConstantInt here. |
1729 | 16 | NeedUpgradeToDIGlobalVariableExpression = true; |
1730 | 16 | Metadata *Expr = getMDOrNull(Record[9]); |
1731 | 16 | uint32_t AlignInBits = 0; |
1732 | 16 | if (Record.size() > 11) { |
1733 | 7 | if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
1734 | 0 | return error("Alignment value is too large"); |
1735 | 7 | AlignInBits = Record[11]; |
1736 | 7 | } |
1737 | 16 | GlobalVariable *Attach = nullptr; |
1738 | 16 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) { |
1739 | 8 | if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) { |
1740 | 7 | Attach = GV; |
1741 | 7 | Expr = nullptr; |
1742 | 7 | } else if (auto *1 CI1 = dyn_cast<ConstantInt>(CMD->getValue())) { |
1743 | 1 | Expr = DIExpression::get(Context, |
1744 | 1 | {dwarf::DW_OP_constu, CI->getZExtValue(), |
1745 | 1 | dwarf::DW_OP_stack_value}); |
1746 | 1 | } else { |
1747 | 0 | Expr = nullptr; |
1748 | 0 | } |
1749 | 8 | } |
1750 | 16 | DIGlobalVariable *DGV = GET_OR_DISTINCT( |
1751 | 16 | DIGlobalVariable, |
1752 | 16 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
1753 | 16 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
1754 | 16 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
1755 | 16 | getMDOrNull(Record[10]), nullptr, AlignInBits)); |
1756 | 16 | |
1757 | 16 | DIGlobalVariableExpression *DGVE = nullptr; |
1758 | 16 | if (Attach || Expr9 ) |
1759 | 13 | DGVE = DIGlobalVariableExpression::getDistinct( |
1760 | 13 | Context, DGV, Expr ? Expr6 : DIExpression::get(Context, {})7 ); |
1761 | 16 | if (Attach) |
1762 | 7 | Attach->addDebugInfo(DGVE); |
1763 | 16 | |
1764 | 16 | auto *MDNode = Expr ? cast<Metadata>(DGVE)6 : cast<Metadata>(DGV)10 ; |
1765 | 16 | MetadataList.assignValue(MDNode, NextMetadataNo); |
1766 | 16 | NextMetadataNo++; |
1767 | 16 | } else |
1768 | 0 | return error("Invalid record"); |
1769 | 102 | |
1770 | 102 | break; |
1771 | 102 | } |
1772 | 147 | case bitc::METADATA_LOCAL_VAR: { |
1773 | 147 | // 10th field is for the obseleted 'inlinedAt:' field. |
1774 | 147 | if (Record.size() < 8 || Record.size() > 10) |
1775 | 0 | return error("Invalid record"); |
1776 | 147 | |
1777 | 147 | IsDistinct = Record[0] & 1; |
1778 | 147 | bool HasAlignment = Record[0] & 2; |
1779 | 147 | // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or |
1780 | 147 | // DW_TAG_arg_variable, if we have alignment flag encoded it means, that |
1781 | 147 | // this is newer version of record which doesn't have artificial tag. |
1782 | 147 | bool HasTag = !HasAlignment && Record.size() > 822 ; |
1783 | 147 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]); |
1784 | 147 | uint32_t AlignInBits = 0; |
1785 | 147 | if (HasAlignment) { |
1786 | 125 | if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
1787 | 0 | return error("Alignment value is too large"); |
1788 | 125 | AlignInBits = Record[8 + HasTag]; |
1789 | 125 | } |
1790 | 147 | MetadataList.assignValue( |
1791 | 147 | GET_OR_DISTINCT(DILocalVariable, |
1792 | 147 | (Context, getMDOrNull(Record[1 + HasTag]), |
1793 | 147 | getMDString(Record[2 + HasTag]), |
1794 | 147 | getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag], |
1795 | 147 | getDITypeRefOrNull(Record[5 + HasTag]), |
1796 | 147 | Record[6 + HasTag], Flags, AlignInBits)), |
1797 | 147 | NextMetadataNo); |
1798 | 147 | NextMetadataNo++; |
1799 | 147 | break; |
1800 | 147 | } |
1801 | 147 | case bitc::METADATA_LABEL: { |
1802 | 12 | if (Record.size() != 5) |
1803 | 0 | return error("Invalid record"); |
1804 | 12 | |
1805 | 12 | IsDistinct = Record[0] & 1; |
1806 | 12 | MetadataList.assignValue( |
1807 | 12 | GET_OR_DISTINCT(DILabel, |
1808 | 12 | (Context, getMDOrNull(Record[1]), |
1809 | 12 | getMDString(Record[2]), |
1810 | 12 | getMDOrNull(Record[3]), Record[4])), |
1811 | 12 | NextMetadataNo); |
1812 | 12 | NextMetadataNo++; |
1813 | 12 | break; |
1814 | 12 | } |
1815 | 142 | case bitc::METADATA_EXPRESSION: { |
1816 | 142 | if (Record.size() < 1) |
1817 | 0 | return error("Invalid record"); |
1818 | 142 | |
1819 | 142 | IsDistinct = Record[0] & 1; |
1820 | 142 | uint64_t Version = Record[0] >> 1; |
1821 | 142 | auto Elts = MutableArrayRef<uint64_t>(Record).slice(1); |
1822 | 142 | |
1823 | 142 | SmallVector<uint64_t, 6> Buffer; |
1824 | 142 | if (Error Err = upgradeDIExpression(Version, Elts, Buffer)) |
1825 | 0 | return Err; |
1826 | 142 | |
1827 | 142 | MetadataList.assignValue( |
1828 | 142 | GET_OR_DISTINCT(DIExpression, (Context, Elts)), NextMetadataNo); |
1829 | 142 | NextMetadataNo++; |
1830 | 142 | break; |
1831 | 142 | } |
1832 | 142 | case bitc::METADATA_GLOBAL_VAR_EXPR: { |
1833 | 67 | if (Record.size() != 3) |
1834 | 0 | return error("Invalid record"); |
1835 | 67 | |
1836 | 67 | IsDistinct = Record[0]; |
1837 | 67 | Metadata *Expr = getMDOrNull(Record[2]); |
1838 | 67 | if (!Expr) |
1839 | 0 | Expr = DIExpression::get(Context, {}); |
1840 | 67 | MetadataList.assignValue( |
1841 | 67 | GET_OR_DISTINCT(DIGlobalVariableExpression, |
1842 | 67 | (Context, getMDOrNull(Record[1]), Expr)), |
1843 | 67 | NextMetadataNo); |
1844 | 67 | NextMetadataNo++; |
1845 | 67 | break; |
1846 | 67 | } |
1847 | 67 | case bitc::METADATA_OBJC_PROPERTY: { |
1848 | 18 | if (Record.size() != 8) |
1849 | 0 | return error("Invalid record"); |
1850 | 18 | |
1851 | 18 | IsDistinct = Record[0]; |
1852 | 18 | MetadataList.assignValue( |
1853 | 18 | GET_OR_DISTINCT(DIObjCProperty, |
1854 | 18 | (Context, getMDString(Record[1]), |
1855 | 18 | getMDOrNull(Record[2]), Record[3], |
1856 | 18 | getMDString(Record[4]), getMDString(Record[5]), |
1857 | 18 | Record[6], getDITypeRefOrNull(Record[7]))), |
1858 | 18 | NextMetadataNo); |
1859 | 18 | NextMetadataNo++; |
1860 | 18 | break; |
1861 | 18 | } |
1862 | 31 | case bitc::METADATA_IMPORTED_ENTITY: { |
1863 | 31 | if (Record.size() != 6 && Record.size() != 725 ) |
1864 | 0 | return error("Invalid record"); |
1865 | 31 | |
1866 | 31 | IsDistinct = Record[0]; |
1867 | 31 | bool HasFile = (Record.size() == 7); |
1868 | 31 | MetadataList.assignValue( |
1869 | 31 | GET_OR_DISTINCT(DIImportedEntity, |
1870 | 31 | (Context, Record[1], getMDOrNull(Record[2]), |
1871 | 31 | getDITypeRefOrNull(Record[3]), |
1872 | 31 | HasFile ? getMDOrNull(Record[6]) : nullptr, |
1873 | 31 | HasFile ? Record[4] : 0, getMDString(Record[5]))), |
1874 | 31 | NextMetadataNo); |
1875 | 31 | NextMetadataNo++; |
1876 | 31 | break; |
1877 | 31 | } |
1878 | 328 | case bitc::METADATA_STRING_OLD: { |
1879 | 328 | std::string String(Record.begin(), Record.end()); |
1880 | 328 | |
1881 | 328 | // Test for upgrading !llvm.loop. |
1882 | 328 | HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String); |
1883 | 328 | ++NumMDStringLoaded; |
1884 | 328 | Metadata *MD = MDString::get(Context, String); |
1885 | 328 | MetadataList.assignValue(MD, NextMetadataNo); |
1886 | 328 | NextMetadataNo++; |
1887 | 328 | break; |
1888 | 31 | } |
1889 | 14.1k | case bitc::METADATA_STRINGS: { |
1890 | 100k | auto CreateNextMDString = [&](StringRef Str) { |
1891 | 100k | ++NumMDStringLoaded; |
1892 | 100k | MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo); |
1893 | 100k | NextMetadataNo++; |
1894 | 100k | }; |
1895 | 14.1k | if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString)) |
1896 | 0 | return Err; |
1897 | 14.1k | break; |
1898 | 14.1k | } |
1899 | 14.1k | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { |
1900 | 470 | if (Record.size() % 2 == 0) |
1901 | 0 | return error("Invalid record"); |
1902 | 470 | unsigned ValueID = Record[0]; |
1903 | 470 | if (ValueID >= ValueList.size()) |
1904 | 0 | return error("Invalid record"); |
1905 | 470 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) |
1906 | 470 | if (Error Err = parseGlobalObjectAttachment( |
1907 | 0 | *GO, ArrayRef<uint64_t>(Record).slice(1))) |
1908 | 0 | return Err; |
1909 | 470 | break; |
1910 | 470 | } |
1911 | 803 | case bitc::METADATA_KIND: { |
1912 | 803 | // Support older bitcode files that had METADATA_KIND records in a |
1913 | 803 | // block with METADATA_BLOCK_ID. |
1914 | 803 | if (Error Err = parseMetadataKindRecord(Record)) |
1915 | 0 | return Err; |
1916 | 803 | break; |
1917 | 803 | } |
1918 | 389k | } |
1919 | 389k | return Error::success(); |
1920 | 389k | #undef GET_OR_DISTINCT |
1921 | 389k | } |
1922 | | |
1923 | | Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings( |
1924 | | ArrayRef<uint64_t> Record, StringRef Blob, |
1925 | 14.1k | function_ref<void(StringRef)> CallBack) { |
1926 | 14.1k | // All the MDStrings in the block are emitted together in a single |
1927 | 14.1k | // record. The strings are concatenated and stored in a blob along with |
1928 | 14.1k | // their sizes. |
1929 | 14.1k | if (Record.size() != 2) |
1930 | 0 | return error("Invalid record: metadata strings layout"); |
1931 | 14.1k | |
1932 | 14.1k | unsigned NumStrings = Record[0]; |
1933 | 14.1k | unsigned StringsOffset = Record[1]; |
1934 | 14.1k | if (!NumStrings) |
1935 | 0 | return error("Invalid record: metadata strings with no strings"); |
1936 | 14.1k | if (StringsOffset > Blob.size()) |
1937 | 0 | return error("Invalid record: metadata strings corrupt offset"); |
1938 | 14.1k | |
1939 | 14.1k | StringRef Lengths = Blob.slice(0, StringsOffset); |
1940 | 14.1k | SimpleBitstreamCursor R(Lengths); |
1941 | 14.1k | |
1942 | 14.1k | StringRef Strings = Blob.drop_front(StringsOffset); |
1943 | 100k | do { |
1944 | 100k | if (R.AtEndOfStream()) |
1945 | 0 | return error("Invalid record: metadata strings bad length"); |
1946 | 100k | |
1947 | 100k | Expected<uint32_t> MaybeSize = R.ReadVBR(6); |
1948 | 100k | if (!MaybeSize) |
1949 | 0 | return MaybeSize.takeError(); |
1950 | 100k | uint32_t Size = MaybeSize.get(); |
1951 | 100k | if (Strings.size() < Size) |
1952 | 0 | return error("Invalid record: metadata strings truncated chars"); |
1953 | 100k | |
1954 | 100k | CallBack(Strings.slice(0, Size)); |
1955 | 100k | Strings = Strings.drop_front(Size); |
1956 | 100k | } while (--NumStrings); |
1957 | 14.1k | |
1958 | 14.1k | return Error::success(); |
1959 | 14.1k | } |
1960 | | |
1961 | | Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment( |
1962 | 798 | GlobalObject &GO, ArrayRef<uint64_t> Record) { |
1963 | 798 | assert(Record.size() % 2 == 0); |
1964 | 1.67k | for (unsigned I = 0, E = Record.size(); I != E; I += 2880 ) { |
1965 | 880 | auto K = MDKindMap.find(Record[I]); |
1966 | 880 | if (K == MDKindMap.end()) |
1967 | 0 | return error("Invalid ID"); |
1968 | 880 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]); |
1969 | 880 | if (!MD) |
1970 | 0 | return error("Invalid metadata attachment: expect fwd ref to MDNode"); |
1971 | 880 | GO.addMetadata(K->second, *MD); |
1972 | 880 | } |
1973 | 798 | return Error::success(); |
1974 | 798 | } |
1975 | | |
1976 | | /// Parse metadata attachments. |
1977 | | Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment( |
1978 | 155k | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { |
1979 | 155k | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) |
1980 | 0 | return Err; |
1981 | 155k | |
1982 | 155k | SmallVector<uint64_t, 64> Record; |
1983 | 155k | PlaceholderQueue Placeholders; |
1984 | 155k | |
1985 | 1.34M | while (true) { |
1986 | 1.34M | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
1987 | 1.34M | if (!MaybeEntry) |
1988 | 0 | return MaybeEntry.takeError(); |
1989 | 1.34M | BitstreamEntry Entry = MaybeEntry.get(); |
1990 | 1.34M | |
1991 | 1.34M | switch (Entry.Kind) { |
1992 | 1.34M | case BitstreamEntry::SubBlock: // Handled for us already. |
1993 | 0 | case BitstreamEntry::Error: |
1994 | 0 | return error("Malformed block"); |
1995 | 155k | case BitstreamEntry::EndBlock: |
1996 | 155k | resolveForwardRefsAndPlaceholders(Placeholders); |
1997 | 155k | return Error::success(); |
1998 | 1.18M | case BitstreamEntry::Record: |
1999 | 1.18M | // The interesting case. |
2000 | 1.18M | break; |
2001 | 1.18M | } |
2002 | 1.18M | |
2003 | 1.18M | // Read a metadata attachment record. |
2004 | 1.18M | Record.clear(); |
2005 | 1.18M | ++NumMDRecordLoaded; |
2006 | 1.18M | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2007 | 1.18M | if (!MaybeRecord) |
2008 | 0 | return MaybeRecord.takeError(); |
2009 | 1.18M | switch (MaybeRecord.get()) { |
2010 | 1.18M | default: // Default behavior: ignore. |
2011 | 0 | break; |
2012 | 1.18M | case bitc::METADATA_ATTACHMENT: { |
2013 | 1.18M | unsigned RecordLength = Record.size(); |
2014 | 1.18M | if (Record.empty()) |
2015 | 0 | return error("Invalid record"); |
2016 | 1.18M | if (RecordLength % 2 == 0) { |
2017 | 328 | // A function attachment. |
2018 | 328 | if (Error Err = parseGlobalObjectAttachment(F, Record)) |
2019 | 0 | return Err; |
2020 | 328 | continue; |
2021 | 328 | } |
2022 | 1.18M | |
2023 | 1.18M | // An instruction attachment. |
2024 | 1.18M | Instruction *Inst = InstructionList[Record[0]]; |
2025 | 2.39M | for (unsigned i = 1; i != RecordLength; i = i + 21.21M ) { |
2026 | 1.21M | unsigned Kind = Record[i]; |
2027 | 1.21M | DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind); |
2028 | 1.21M | if (I == MDKindMap.end()) |
2029 | 0 | return error("Invalid ID"); |
2030 | 1.21M | if (I->second == LLVMContext::MD_tbaa && StripTBAA1.06M ) |
2031 | 11 | continue; |
2032 | 1.21M | |
2033 | 1.21M | auto Idx = Record[i + 1]; |
2034 | 1.21M | if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) && |
2035 | 1.21M | !MetadataList.lookup(Idx)2 ) { |
2036 | 1 | // Load the attachment if it is in the lazy-loadable range and hasn't |
2037 | 1 | // been loaded yet. |
2038 | 1 | lazyLoadOneMetadata(Idx, Placeholders); |
2039 | 1 | resolveForwardRefsAndPlaceholders(Placeholders); |
2040 | 1 | } |
2041 | 1.21M | |
2042 | 1.21M | Metadata *Node = MetadataList.getMetadataFwdRef(Idx); |
2043 | 1.21M | if (isa<LocalAsMetadata>(Node)) |
2044 | 1 | // Drop the attachment. This used to be legal, but there's no |
2045 | 1 | // upgrade path. |
2046 | 1 | break; |
2047 | 1.21M | MDNode *MD = dyn_cast_or_null<MDNode>(Node); |
2048 | 1.21M | if (!MD) |
2049 | 0 | return error("Invalid metadata attachment"); |
2050 | 1.21M | |
2051 | 1.21M | if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop2 ) |
2052 | 2 | MD = upgradeInstructionLoopAttachment(*MD); |
2053 | 1.21M | |
2054 | 1.21M | if (I->second == LLVMContext::MD_tbaa) { |
2055 | 1.06M | assert(!MD->isTemporary() && "should load MDs before attachments"); |
2056 | 1.06M | MD = UpgradeTBAANode(*MD); |
2057 | 1.06M | } |
2058 | 1.21M | Inst->setMetadata(I->second, MD); |
2059 | 1.21M | } |
2060 | 1.18M | break; |
2061 | 1.18M | } |
2062 | 1.18M | } |
2063 | 1.18M | } |
2064 | 155k | } |
2065 | | |
2066 | | /// Parse a single METADATA_KIND record, inserting result in MDKindMap. |
2067 | | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord( |
2068 | 181k | SmallVectorImpl<uint64_t> &Record) { |
2069 | 181k | if (Record.size() < 2) |
2070 | 0 | return error("Invalid record"); |
2071 | 181k | |
2072 | 181k | unsigned Kind = Record[0]; |
2073 | 181k | SmallString<8> Name(Record.begin() + 1, Record.end()); |
2074 | 181k | |
2075 | 181k | unsigned NewKind = TheModule.getMDKindID(Name.str()); |
2076 | 181k | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) |
2077 | 0 | return error("Conflicting METADATA_KIND records"); |
2078 | 181k | return Error::success(); |
2079 | 181k | } |
2080 | | |
2081 | | /// Parse the metadata kinds out of the METADATA_KIND_BLOCK. |
2082 | 7.65k | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() { |
2083 | 7.65k | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID)) |
2084 | 0 | return Err; |
2085 | 7.65k | |
2086 | 7.65k | SmallVector<uint64_t, 64> Record; |
2087 | 7.65k | |
2088 | 7.65k | // Read all the records. |
2089 | 188k | while (true188k ) { |
2090 | 188k | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2091 | 188k | if (!MaybeEntry) |
2092 | 0 | return MaybeEntry.takeError(); |
2093 | 188k | BitstreamEntry Entry = MaybeEntry.get(); |
2094 | 188k | |
2095 | 188k | switch (Entry.Kind) { |
2096 | 188k | case BitstreamEntry::SubBlock: // Handled for us already. |
2097 | 0 | case BitstreamEntry::Error: |
2098 | 0 | return error("Malformed block"); |
2099 | 7.65k | case BitstreamEntry::EndBlock: |
2100 | 7.65k | return Error::success(); |
2101 | 180k | case BitstreamEntry::Record: |
2102 | 180k | // The interesting case. |
2103 | 180k | break; |
2104 | 180k | } |
2105 | 180k | |
2106 | 180k | // Read a record. |
2107 | 180k | Record.clear(); |
2108 | 180k | ++NumMDRecordLoaded; |
2109 | 180k | Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record); |
2110 | 180k | if (!MaybeCode) |
2111 | 0 | return MaybeCode.takeError(); |
2112 | 180k | switch (MaybeCode.get()) { |
2113 | 180k | default: // Default behavior: ignore. |
2114 | 0 | break; |
2115 | 181k | case bitc::METADATA_KIND: { |
2116 | 181k | if (Error Err = parseMetadataKindRecord(Record)) |
2117 | 0 | return Err; |
2118 | 181k | break; |
2119 | 181k | } |
2120 | 180k | } |
2121 | 180k | } |
2122 | 7.65k | } |
2123 | | |
2124 | 0 | MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) { |
2125 | 0 | Pimpl = std::move(RHS.Pimpl); |
2126 | 0 | return *this; |
2127 | 0 | } |
2128 | | MetadataLoader::MetadataLoader(MetadataLoader &&RHS) |
2129 | 7.78k | : Pimpl(std::move(RHS.Pimpl)) {} |
2130 | | |
2131 | 15.5k | MetadataLoader::~MetadataLoader() = default; |
2132 | | MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule, |
2133 | | BitcodeReaderValueList &ValueList, |
2134 | | bool IsImporting, |
2135 | | std::function<Type *(unsigned)> getTypeByID) |
2136 | | : Pimpl(llvm::make_unique<MetadataLoaderImpl>( |
2137 | 7.79k | Stream, TheModule, ValueList, std::move(getTypeByID), IsImporting)) {} |
2138 | | |
2139 | 36.1k | Error MetadataLoader::parseMetadata(bool ModuleLevel) { |
2140 | 36.1k | return Pimpl->parseMetadata(ModuleLevel); |
2141 | 36.1k | } |
2142 | | |
2143 | 675k | bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); } |
2144 | | |
2145 | | /// Return the given metadata, creating a replaceable forward reference if |
2146 | | /// necessary. |
2147 | 18.1k | Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) { |
2148 | 18.1k | return Pimpl->getMetadataFwdRefOrLoad(Idx); |
2149 | 18.1k | } |
2150 | | |
2151 | 337k | DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) { |
2152 | 337k | return Pimpl->lookupSubprogramForFunction(F); |
2153 | 337k | } |
2154 | | |
2155 | | Error MetadataLoader::parseMetadataAttachment( |
2156 | 155k | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { |
2157 | 155k | return Pimpl->parseMetadataAttachment(F, InstructionList); |
2158 | 155k | } |
2159 | | |
2160 | 7.65k | Error MetadataLoader::parseMetadataKinds() { |
2161 | 7.65k | return Pimpl->parseMetadataKinds(); |
2162 | 7.65k | } |
2163 | | |
2164 | 2 | void MetadataLoader::setStripTBAA(bool StripTBAA) { |
2165 | 2 | return Pimpl->setStripTBAA(StripTBAA); |
2166 | 2 | } |
2167 | | |
2168 | 337k | bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); } |
2169 | | |
2170 | 337k | unsigned MetadataLoader::size() const { return Pimpl->size(); } |
2171 | 337k | void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); } |
2172 | | |
2173 | 3.41M | void MetadataLoader::upgradeDebugIntrinsics(Function &F) { |
2174 | 3.41M | return Pimpl->upgradeDebugIntrinsics(F); |
2175 | 3.41M | } |