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/APInt.h"
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/BitmaskEnum.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLFunctionalExtras.h"
18#include "llvm/ADT/SetVector.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/BinaryFormat/Dwarf.h"
25#include "llvm/Bitcode/BitcodeReader.h"
26#include "llvm/Bitcode/LLVMBitCodes.h"
27#include "llvm/Bitstream/BitstreamReader.h"
28#include "llvm/IR/Argument.h"
29#include "llvm/IR/AutoUpgrade.h"
30#include "llvm/IR/BasicBlock.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DebugInfoMetadata.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalObject.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/Instruction.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Metadata.h"
40#include "llvm/IR/Module.h"
41#include "llvm/IR/TrackingMDRef.h"
42#include "llvm/IR/Type.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/CommandLine.h"
45#include "llvm/Support/Compiler.h"
46#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/TimeProfiler.h"
48
49#include <algorithm>
50#include <cassert>
51#include <cstddef>
52#include <cstdint>
53#include <deque>
54#include <iterator>
55#include <limits>
56#include <map>
57#include <optional>
58#include <string>
59#include <tuple>
60#include <utility>
61#include <vector>
62
63using namespace llvm;
64
65#define DEBUG_TYPE "bitcode-reader"
66
67STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
68STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
69STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
70
71/// Flag whether we need to import full type definitions for ThinLTO.
72/// Currently needed for Darwin and LLDB.
73static cl::opt<bool> ImportFullTypeDefinitions(
74 "import-full-type-definitions", cl::init(Val: false), cl::Hidden,
75 cl::desc("Import full type definitions for ThinLTO."));
76
77static cl::opt<bool> DisableLazyLoading(
78 "disable-ondemand-mds-loading", cl::init(Val: false), cl::Hidden,
79 cl::desc("Force disable the lazy-loading on-demand of metadata when "
80 "loading bitcode for importing."));
81
82namespace {
83
84class BitcodeReaderMetadataList {
85 /// Array of metadata references.
86 ///
87 /// Don't use std::vector here. Some versions of libc++ copy (instead of
88 /// move) on resize, and TrackingMDRef is very expensive to copy.
89 SmallVector<TrackingMDRef, 1> MetadataPtrs;
90
91 /// The set of indices in MetadataPtrs above of forward references that were
92 /// generated.
93 SmallDenseSet<unsigned, 1> ForwardReference;
94
95 /// The set of indices in MetadataPtrs above of Metadata that need to be
96 /// resolved.
97 SmallDenseSet<unsigned, 1> UnresolvedNodes;
98
99 /// Structures for resolving old type refs.
100 struct {
101 SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
102 SmallDenseMap<MDString *, DICompositeType *, 1> Final;
103 SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
104 SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
105 } OldTypeRefs;
106
107 LLVMContext &Context;
108
109 /// Maximum number of valid references. Forward references exceeding the
110 /// maximum must be invalid.
111 unsigned RefsUpperBound;
112
113public:
114 BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound)
115 : Context(C),
116 RefsUpperBound(std::min(a: (size_t)std::numeric_limits<unsigned>::max(),
117 b: RefsUpperBound)) {}
118
119 using const_iterator = SmallVector<TrackingMDRef, 1>::const_iterator;
120
121 // vector compatibility methods
122 unsigned size() const { return MetadataPtrs.size(); }
123 void resize(unsigned N) { MetadataPtrs.resize(N); }
124 void push_back(Metadata *MD) { MetadataPtrs.emplace_back(Args&: MD); }
125 void clear() { MetadataPtrs.clear(); }
126 Metadata *back() const { return MetadataPtrs.back(); }
127 void pop_back() { MetadataPtrs.pop_back(); }
128 bool empty() const { return MetadataPtrs.empty(); }
129 const_iterator begin() const { return MetadataPtrs.begin(); }
130 const_iterator end() const { return MetadataPtrs.end(); }
131
132 Metadata *operator[](unsigned i) const { return MetadataPtrs[i]; }
133
134 Metadata *lookup(unsigned I) const {
135 if (I < MetadataPtrs.size())
136 return MetadataPtrs[I];
137 return nullptr;
138 }
139
140 void shrinkTo(unsigned N) {
141 assert(N <= size() && "Invalid shrinkTo request!");
142 assert(ForwardReference.empty() && "Unexpected forward refs");
143 assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
144 MetadataPtrs.resize(N);
145 }
146
147 /// Return the given metadata, creating a replaceable forward reference if
148 /// necessary.
149 Metadata *getMetadataFwdRef(unsigned Idx);
150
151 /// Return the given metadata only if it is fully resolved.
152 ///
153 /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
154 /// would give \c false.
155 Metadata *getMetadataIfResolved(unsigned Idx);
156
157 MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
158 void assignValue(Metadata *MD, unsigned Idx);
159 void tryToResolveCycles();
160 bool hasFwdRefs() const { return !ForwardReference.empty(); }
161 int getNextFwdRef() {
162 assert(hasFwdRefs());
163 return *ForwardReference.begin();
164 }
165
166 /// Upgrade a type that had an MDString reference.
167 void addTypeRef(MDString &UUID, DICompositeType &CT);
168
169 /// Upgrade a type that had an MDString reference.
170 Metadata *upgradeTypeRef(Metadata *MaybeUUID);
171
172 /// Upgrade a type array that may have MDString references.
173 Metadata *upgradeTypeArray(Metadata *MaybeTuple);
174
175private:
176 Metadata *resolveTypeArray(Metadata *MaybeTuple);
177};
178} // namespace
179
180static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; }
181
182void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
183 if (auto *MDN = dyn_cast<MDNode>(Val: MD))
184 if (!MDN->isResolved())
185 UnresolvedNodes.insert(V: Idx);
186
187 if (Idx == size()) {
188 push_back(MD);
189 return;
190 }
191
192 if (Idx >= size())
193 resize(N: Idx + 1);
194
195 TrackingMDRef &OldMD = MetadataPtrs[Idx];
196 if (!OldMD) {
197 OldMD.reset(MD);
198 return;
199 }
200
201 // If there was a forward reference to this value, replace it.
202 TempMDTuple PrevMD(cast<MDTuple>(Val: OldMD.get()));
203 PrevMD->replaceAllUsesWith(MD);
204 ForwardReference.erase(V: Idx);
205}
206
207Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
208 // Bail out for a clearly invalid value.
209 if (Idx >= RefsUpperBound)
210 return nullptr;
211
212 if (Idx >= size())
213 resize(N: Idx + 1);
214
215 if (Metadata *MD = MetadataPtrs[Idx])
216 return MD;
217
218 // Track forward refs to be resolved later.
219 ForwardReference.insert(V: Idx);
220
221 // Create and return a placeholder, which will later be RAUW'd.
222 ++NumMDNodeTemporary;
223 Metadata *MD = MDNode::getTemporary(Context, MDs: {}).release();
224 MetadataPtrs[Idx].reset(MD);
225 return MD;
226}
227
228Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
229 Metadata *MD = lookup(I: Idx);
230 if (auto *N = dyn_cast_or_null<MDNode>(Val: MD))
231 if (!N->isResolved())
232 return nullptr;
233 return MD;
234}
235
236MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
237 return dyn_cast_or_null<MDNode>(Val: getMetadataFwdRef(Idx));
238}
239
240void BitcodeReaderMetadataList::tryToResolveCycles() {
241 if (!ForwardReference.empty())
242 // Still forward references... can't resolve cycles.
243 return;
244
245 // Give up on finding a full definition for any forward decls that remain.
246 for (const auto &Ref : OldTypeRefs.FwdDecls)
247 OldTypeRefs.Final.insert(KV: Ref);
248 OldTypeRefs.FwdDecls.clear();
249
250 // Upgrade from old type ref arrays. In strange cases, this could add to
251 // OldTypeRefs.Unknown.
252 for (const auto &Array : OldTypeRefs.Arrays)
253 Array.second->replaceAllUsesWith(MD: resolveTypeArray(MaybeTuple: Array.first.get()));
254 OldTypeRefs.Arrays.clear();
255
256 // Replace old string-based type refs with the resolved node, if possible.
257 // If we haven't seen the node, leave it to the verifier to complain about
258 // the invalid string reference.
259 for (const auto &Ref : OldTypeRefs.Unknown) {
260 if (DICompositeType *CT = OldTypeRefs.Final.lookup(Val: Ref.first))
261 Ref.second->replaceAllUsesWith(MD: CT);
262 else
263 Ref.second->replaceAllUsesWith(MD: Ref.first);
264 }
265 OldTypeRefs.Unknown.clear();
266
267 if (UnresolvedNodes.empty())
268 // Nothing to do.
269 return;
270
271 // Resolve any cycles.
272 for (unsigned I : UnresolvedNodes) {
273 auto &MD = MetadataPtrs[I];
274 auto *N = dyn_cast_or_null<MDNode>(Val&: MD);
275 if (!N)
276 continue;
277
278 assert(!N->isTemporary() && "Unexpected forward reference");
279 N->resolveCycles();
280 }
281
282 // Make sure we return early again until there's another unresolved ref.
283 UnresolvedNodes.clear();
284}
285
286void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
287 DICompositeType &CT) {
288 assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
289 if (CT.isForwardDecl())
290 OldTypeRefs.FwdDecls.insert(KV: std::make_pair(x: &UUID, y: &CT));
291 else
292 OldTypeRefs.Final.insert(KV: std::make_pair(x: &UUID, y: &CT));
293}
294
295Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
296 auto *UUID = dyn_cast_or_null<MDString>(Val: MaybeUUID);
297 if (LLVM_LIKELY(!UUID))
298 return MaybeUUID;
299
300 if (auto *CT = OldTypeRefs.Final.lookup(Val: UUID))
301 return CT;
302
303 auto &Ref = OldTypeRefs.Unknown[UUID];
304 if (!Ref)
305 Ref = MDNode::getTemporary(Context, MDs: {});
306 return Ref.get();
307}
308
309Metadata *BitcodeReaderMetadataList::upgradeTypeArray(Metadata *MaybeTuple) {
310 auto *Tuple = dyn_cast_or_null<MDTuple>(Val: MaybeTuple);
311 if (!Tuple || Tuple->isDistinct())
312 return MaybeTuple;
313
314 // Look through the array immediately if possible.
315 if (!Tuple->isTemporary())
316 return resolveTypeArray(MaybeTuple: Tuple);
317
318 // Create and return a placeholder to use for now. Eventually
319 // resolveTypeArrays() will be resolve this forward reference.
320 OldTypeRefs.Arrays.emplace_back(
321 Args: std::piecewise_construct, Args: std::forward_as_tuple(args&: Tuple),
322 Args: std::forward_as_tuple(args: MDTuple::getTemporary(Context, MDs: {})));
323 return OldTypeRefs.Arrays.back().second.get();
324}
325
326Metadata *BitcodeReaderMetadataList::resolveTypeArray(Metadata *MaybeTuple) {
327 auto *Tuple = dyn_cast_or_null<MDTuple>(Val: MaybeTuple);
328 if (!Tuple || Tuple->isDistinct())
329 return MaybeTuple;
330
331 // Look through the DITypeArray, upgrading each DIType *.
332 SmallVector<Metadata *, 32> Ops;
333 Ops.reserve(N: Tuple->getNumOperands());
334 for (Metadata *MD : Tuple->operands())
335 Ops.push_back(Elt: upgradeTypeRef(MaybeUUID: MD));
336
337 return MDTuple::get(Context, MDs: Ops);
338}
339
340namespace {
341
342class PlaceholderQueue {
343 // Placeholders would thrash around when moved, so store in a std::deque
344 // instead of some sort of vector.
345 std::deque<DistinctMDOperandPlaceholder> PHs;
346
347public:
348 ~PlaceholderQueue() {
349 assert(empty() &&
350 "PlaceholderQueue hasn't been flushed before being destroyed");
351 }
352 bool empty() const { return PHs.empty(); }
353 DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
354 void flush(BitcodeReaderMetadataList &MetadataList);
355
356 /// Return the list of temporaries nodes in the queue, these need to be
357 /// loaded before we can flush the queue.
358 void getTemporaries(BitcodeReaderMetadataList &MetadataList,
359 DenseSet<unsigned> &Temporaries) {
360 for (auto &PH : PHs) {
361 auto ID = PH.getID();
362 auto *MD = MetadataList.lookup(I: ID);
363 if (!MD) {
364 Temporaries.insert(V: ID);
365 continue;
366 }
367 auto *N = dyn_cast_or_null<MDNode>(Val: MD);
368 if (N && N->isTemporary())
369 Temporaries.insert(V: ID);
370 }
371 }
372};
373
374} // end anonymous namespace
375
376DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
377 PHs.emplace_back(args&: ID);
378 return PHs.back();
379}
380
381void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
382 while (!PHs.empty()) {
383 auto *MD = MetadataList.lookup(I: PHs.front().getID());
384 assert(MD && "Flushing placeholder on unassigned MD");
385#ifndef NDEBUG
386 if (auto *MDN = dyn_cast<MDNode>(MD))
387 assert(MDN->isResolved() &&
388 "Flushing Placeholder while cycles aren't resolved");
389#endif
390 PHs.front().replaceUseWith(MD);
391 PHs.pop_front();
392 }
393}
394
395static Error error(const Twine &Message) {
396 return make_error<StringError>(
397 Args: Message, Args: make_error_code(E: BitcodeError::CorruptedBitcode));
398}
399
400class MetadataLoader::MetadataLoaderImpl {
401 BitcodeReaderMetadataList MetadataList;
402 BitcodeReaderValueList &ValueList;
403 BitstreamCursor &Stream;
404 LLVMContext &Context;
405 Module &TheModule;
406 MetadataLoaderCallbacks Callbacks;
407
408 /// Cursor associated with the lazy-loading of Metadata. This is the easy way
409 /// to keep around the right "context" (Abbrev list) to be able to jump in
410 /// the middle of the metadata block and load any record.
411 BitstreamCursor IndexCursor;
412
413 /// Index that keeps track of MDString values.
414 std::vector<StringRef> MDStringRef;
415
416 /// On-demand loading of a single MDString. Requires the index above to be
417 /// populated.
418 MDString *lazyLoadOneMDString(unsigned Idx);
419
420 /// Index that keeps track of where to find a metadata record in the stream.
421 std::vector<uint64_t> GlobalMetadataBitPosIndex;
422
423 /// Cursor position of the start of the global decl attachments, to enable
424 /// loading using the index built for lazy loading, instead of forward
425 /// references.
426 uint64_t GlobalDeclAttachmentPos = 0;
427
428#ifndef NDEBUG
429 /// Baisic correctness check that we end up parsing all of the global decl
430 /// attachments.
431 unsigned NumGlobalDeclAttachSkipped = 0;
432 unsigned NumGlobalDeclAttachParsed = 0;
433#endif
434
435 /// Load the global decl attachments, using the index built for lazy loading.
436 Expected<bool> loadGlobalDeclAttachments();
437
438 /// Populate the index above to enable lazily loading of metadata, and load
439 /// the named metadata as well as the transitively referenced global
440 /// Metadata.
441 Expected<bool> lazyLoadModuleMetadataBlock();
442
443 /// On-demand loading of a single metadata. Requires the index above to be
444 /// populated.
445 void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
446
447 // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
448 // point from SP to CU after a block is completly parsed.
449 std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
450
451 /// Functions that need to be matched with subprograms when upgrading old
452 /// metadata.
453 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
454
455 /// retainedNodes of these subprograms should be cleaned up from incorrectly
456 /// scoped local types.
457 /// See \ref DISubprogram::cleanupRetainedNodes.
458 SmallVector<DISubprogram *> NewDistinctSPs;
459
460 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
461 DenseMap<unsigned, unsigned> MDKindMap;
462
463 bool StripTBAA = false;
464 bool HasSeenOldLoopTags = false;
465 bool NeedUpgradeToDIGlobalVariableExpression = false;
466 bool NeedDeclareExpressionUpgrade = false;
467
468 /// Map DILocalScope to the enclosing DISubprogram, if any.
469 DenseMap<DILocalScope *, DISubprogram *> ParentSubprogram;
470
471 /// True if metadata is being parsed for a module being ThinLTO imported.
472 bool IsImporting = false;
473
474 Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
475 PlaceholderQueue &Placeholders, StringRef Blob,
476 unsigned &NextMetadataNo);
477 Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
478 function_ref<void(StringRef)> CallBack);
479 Error parseGlobalObjectAttachment(GlobalObject &GO,
480 ArrayRef<uint64_t> Record);
481 Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
482
483 void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
484
485 /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
486 void upgradeCUSubprograms() {
487 for (auto CU_SP : CUSubprograms)
488 if (auto *SPs = dyn_cast_or_null<MDTuple>(Val: CU_SP.second))
489 for (auto &Op : SPs->operands())
490 if (auto *SP = dyn_cast_or_null<DISubprogram>(Val: Op))
491 SP->replaceUnit(CU: CU_SP.first);
492 CUSubprograms.clear();
493 }
494
495 /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions.
496 void upgradeCUVariables() {
497 if (!NeedUpgradeToDIGlobalVariableExpression)
498 return;
499
500 // Upgrade list of variables attached to the CUs.
501 if (NamedMDNode *CUNodes = TheModule.getNamedMetadata(Name: "llvm.dbg.cu"))
502 for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
503 auto *CU = cast<DICompileUnit>(Val: CUNodes->getOperand(i: I));
504 if (auto *GVs = dyn_cast_or_null<MDTuple>(Val: CU->getRawGlobalVariables()))
505 for (unsigned I = 0; I < GVs->getNumOperands(); I++)
506 if (auto *GV =
507 dyn_cast_or_null<DIGlobalVariable>(Val: GVs->getOperand(I))) {
508 auto *DGVE = DIGlobalVariableExpression::getDistinct(
509 Context, Variable: GV, Expression: DIExpression::get(Context, Elements: {}));
510 GVs->replaceOperandWith(I, New: DGVE);
511 }
512 }
513
514 // Upgrade variables attached to globals.
515 for (auto &GV : TheModule.globals()) {
516 SmallVector<MDNode *, 1> MDs;
517 GV.getMetadata(KindID: LLVMContext::MD_dbg, MDs);
518 GV.eraseMetadata(KindID: LLVMContext::MD_dbg);
519 for (auto *MD : MDs)
520 if (auto *DGV = dyn_cast<DIGlobalVariable>(Val: MD)) {
521 auto *DGVE = DIGlobalVariableExpression::getDistinct(
522 Context, Variable: DGV, Expression: DIExpression::get(Context, Elements: {}));
523 GV.addMetadata(KindID: LLVMContext::MD_dbg, MD&: *DGVE);
524 } else
525 GV.addMetadata(KindID: LLVMContext::MD_dbg, MD&: *MD);
526 }
527 }
528
529 DISubprogram *findEnclosingSubprogram(DILocalScope *S) {
530 if (!S)
531 return nullptr;
532 if (auto *SP = ParentSubprogram[S]) {
533 return SP;
534 }
535
536 DILocalScope *InitialScope = S;
537 DenseSet<DILocalScope *> Visited;
538 while (S && !isa<DISubprogram>(Val: S)) {
539 S = dyn_cast_or_null<DILocalScope>(Val: S->getScope());
540 if (!Visited.insert(V: S).second)
541 break;
542 }
543
544 return ParentSubprogram[InitialScope] =
545 llvm::dyn_cast_or_null<DISubprogram>(Val: S);
546 }
547
548 /// Move local imports from DICompileUnit's 'imports' field to
549 /// DISubprogram's retainedNodes.
550 /// Move function-local enums from DICompileUnit's enums
551 /// to DISubprogram's retainedNodes.
552 void upgradeCULocals() {
553 NamedMDNode *CUNodes = TheModule.getNamedMetadata(Name: "llvm.dbg.cu");
554 if (!CUNodes)
555 return;
556
557 // Filter out elements of ToRemove from tuple T.
558 auto FilterTuple = [this](MDNode *T,
559 const SetVector<Metadata *> &ToRemove) {
560 SmallVector<Metadata *> Result;
561 for (Metadata *Op : T->operands())
562 if (!ToRemove.contains(key: Op))
563 Result.push_back(Elt: Op);
564 return MDTuple::get(Context, MDs: Result);
565 };
566
567 // For each CU:
568 // - Collect local metadata nodes from CU's imports: and enums: lists in
569 // MetadataToRemove set.
570 // - Remove metadata nodes of MetadataToRemove set from CU's imports: and
571 // enums: lists.
572 // - Group MetadataToRemove items by their parent subprograms (in
573 // SPToEntities map).
574 // - For each subprogram SP in SPToEntities:
575 // - Append collected local metadata nodes to SP's retainedNodes: list.
576 for (MDNode *N : CUNodes->operands()) {
577 auto *CU = dyn_cast<DICompileUnit>(Val: N);
578 if (!CU)
579 continue;
580
581 SetVector<Metadata *> MetadataToRemove;
582 // Collect imported entities to be moved.
583 if (CU->getRawImportedEntities())
584 for (Metadata *Op : CU->getImportedEntities()->operands()) {
585 auto *IE = cast<DIImportedEntity>(Val: Op);
586 if (isa_and_nonnull<DILocalScope>(Val: IE->getScope()))
587 MetadataToRemove.insert(X: IE);
588 }
589 // Collect enums to be moved.
590 if (CU->getRawEnumTypes())
591 for (Metadata *Op : CU->getEnumTypes()->operands()) {
592 auto *Enum = cast<DICompositeType>(Val: Op);
593 if (isa_and_nonnull<DILocalScope>(Val: Enum->getScope()))
594 MetadataToRemove.insert(X: Enum);
595 }
596
597 if (MetadataToRemove.empty())
598 continue;
599
600 // Remove entities with local scope from CU.
601 if (CU->getRawImportedEntities())
602 CU->replaceImportedEntities(
603 N: FilterTuple(CU->getImportedEntities().get(), MetadataToRemove));
604
605 // Remove enums with local scope from CU.
606 if (CU->getRawEnumTypes())
607 CU->replaceEnumTypes(
608 N: FilterTuple(CU->getEnumTypes().get(), MetadataToRemove));
609
610 // Find DISubprogram corresponding to each entity.
611 SmallDenseMap<DISubprogram *, SmallVector<Metadata *>> SPToEntities;
612 for (auto *I : MetadataToRemove) {
613 DILocalScope *Scope =
614 DISubprogram::getRetainedNodeScope(N: cast<DINode>(Val: I));
615 if (auto *SP = findEnclosingSubprogram(S: Scope))
616 SPToEntities[SP].push_back(Elt: I);
617 }
618
619 // Update DISubprograms' retainedNodes.
620 for (auto I = SPToEntities.begin(); I != SPToEntities.end(); ++I) {
621 auto *SP = I->first;
622 auto RetainedNodes = SP->getRetainedNodes();
623 SmallVector<Metadata *> MDs(RetainedNodes.begin(), RetainedNodes.end());
624 MDs.append(RHS: I->second);
625 SP->replaceRetainedNodes(N: MDNode::get(Context, MDs));
626 }
627 }
628
629 ParentSubprogram.clear();
630 }
631
632 /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that
633 /// describes a function argument.
634 void upgradeDeclareExpressions(Function &F) {
635 if (!NeedDeclareExpressionUpgrade)
636 return;
637
638 auto UpdateDeclareIfNeeded = [&](auto *Declare) {
639 auto *DIExpr = Declare->getExpression();
640 if (!DIExpr || !DIExpr->startsWithDeref() ||
641 !isa_and_nonnull<Argument>(Declare->getAddress()))
642 return;
643 SmallVector<uint64_t, 8> Ops;
644 Ops.append(std::next(DIExpr->elements_begin()), DIExpr->elements_end());
645 Declare->setExpression(DIExpression::get(Context, Elements: Ops));
646 };
647
648 for (auto &BB : F)
649 for (auto &I : BB) {
650 for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) {
651 if (DVR.isDbgDeclare())
652 UpdateDeclareIfNeeded(&DVR);
653 }
654 if (auto *DDI = dyn_cast<DbgDeclareInst>(Val: &I))
655 UpdateDeclareIfNeeded(DDI);
656 }
657 }
658
659 /// Upgrade the expression from previous versions.
660 Error upgradeDIExpression(uint64_t FromVersion,
661 MutableArrayRef<uint64_t> &Expr,
662 SmallVectorImpl<uint64_t> &Buffer) {
663 auto N = Expr.size();
664 switch (FromVersion) {
665 default:
666 return error(Message: "Invalid record");
667 case 0:
668 if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece)
669 Expr[N - 3] = dwarf::DW_OP_LLVM_fragment;
670 [[fallthrough]];
671 case 1:
672 // Move DW_OP_deref to the end.
673 if (N && Expr[0] == dwarf::DW_OP_deref) {
674 auto End = Expr.end();
675 if (Expr.size() >= 3 &&
676 *std::prev(x: End, n: 3) == dwarf::DW_OP_LLVM_fragment)
677 End = std::prev(x: End, n: 3);
678 std::move(first: std::next(x: Expr.begin()), last: End, result: Expr.begin());
679 *std::prev(x: End) = dwarf::DW_OP_deref;
680 }
681 NeedDeclareExpressionUpgrade = true;
682 [[fallthrough]];
683 case 2: {
684 // Change DW_OP_plus to DW_OP_plus_uconst.
685 // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus
686 auto SubExpr = ArrayRef<uint64_t>(Expr);
687 while (!SubExpr.empty()) {
688 // Skip past other operators with their operands
689 // for this version of the IR, obtained from
690 // from historic DIExpression::ExprOperand::getSize().
691 size_t HistoricSize;
692 switch (SubExpr.front()) {
693 default:
694 HistoricSize = 1;
695 break;
696 case dwarf::DW_OP_constu:
697 case dwarf::DW_OP_minus:
698 case dwarf::DW_OP_plus:
699 HistoricSize = 2;
700 break;
701 case dwarf::DW_OP_LLVM_fragment:
702 HistoricSize = 3;
703 break;
704 }
705
706 // If the expression is malformed, make sure we don't
707 // copy more elements than we should.
708 HistoricSize = std::min(a: SubExpr.size(), b: HistoricSize);
709 ArrayRef<uint64_t> Args = SubExpr.slice(N: 1, M: HistoricSize - 1);
710
711 switch (SubExpr.front()) {
712 case dwarf::DW_OP_plus:
713 Buffer.push_back(Elt: dwarf::DW_OP_plus_uconst);
714 Buffer.append(in_start: Args.begin(), in_end: Args.end());
715 break;
716 case dwarf::DW_OP_minus:
717 Buffer.push_back(Elt: dwarf::DW_OP_constu);
718 Buffer.append(in_start: Args.begin(), in_end: Args.end());
719 Buffer.push_back(Elt: dwarf::DW_OP_minus);
720 break;
721 default:
722 Buffer.push_back(Elt: *SubExpr.begin());
723 Buffer.append(in_start: Args.begin(), in_end: Args.end());
724 break;
725 }
726
727 // Continue with remaining elements.
728 SubExpr = SubExpr.slice(N: HistoricSize);
729 }
730 Expr = MutableArrayRef<uint64_t>(Buffer);
731 [[fallthrough]];
732 }
733 case 3:
734 // Up-to-date!
735 break;
736 }
737
738 return Error::success();
739 }
740
741 /// Specifies which kind of debug info upgrade should be performed.
742 ///
743 /// The upgrade of compile units' enums: and imports: fields is performed
744 /// only when module level metadata block is loaded (i.e. all elements of
745 /// "llvm.dbg.cu" named metadata node are loaded).
746 enum class DebugInfoUpgradeMode {
747 /// No debug info upgrade.
748 None,
749 /// Debug info upgrade after loading function-level metadata block.
750 Partial,
751 /// Debug info upgrade after loading module-level metadata block.
752 ModuleLevel,
753 };
754
755 void upgradeDebugInfo(DebugInfoUpgradeMode Mode) {
756 if (Mode == DebugInfoUpgradeMode::None)
757 return;
758 upgradeCUSubprograms();
759 upgradeCUVariables();
760 if (Mode == DebugInfoUpgradeMode::ModuleLevel)
761 upgradeCULocals();
762 }
763
764 /// Prepare loaded metadata nodes to be used by loader clients.
765 void resolveLoadedMetadata(PlaceholderQueue &Placeholders,
766 DebugInfoUpgradeMode DIUpgradeMode) {
767 resolveForwardRefsAndPlaceholders(Placeholders);
768 upgradeDebugInfo(Mode: DIUpgradeMode);
769 DISubprogram::cleanupRetainedNodes(NewDistinctSPs);
770 LLVM_DEBUG(llvm::dbgs() << "Resolved loaded metadata. Cleaned up "
771 << NewDistinctSPs.size() << " subprogram(s).\n");
772 NewDistinctSPs.clear();
773 }
774
775 void callMDTypeCallback(Metadata **Val, unsigned TypeID);
776
777public:
778 MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
779 BitcodeReaderValueList &ValueList,
780 MetadataLoaderCallbacks Callbacks, bool IsImporting)
781 : MetadataList(TheModule.getContext(), Stream.SizeInBytes()),
782 ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()),
783 TheModule(TheModule), Callbacks(std::move(Callbacks)),
784 IsImporting(IsImporting) {}
785
786 Error parseMetadata(bool ModuleLevel);
787
788 bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
789
790 Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
791 if (ID < MDStringRef.size())
792 return lazyLoadOneMDString(Idx: ID);
793 if (auto *MD = MetadataList.lookup(I: ID))
794 return MD;
795 // If lazy-loading is enabled, we try recursively to load the operand
796 // instead of creating a temporary.
797 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
798 PlaceholderQueue Placeholders;
799 lazyLoadOneMetadata(Idx: ID, Placeholders);
800 LLVM_DEBUG(llvm::dbgs() << "\nLazy metadata loading: ");
801 resolveLoadedMetadata(Placeholders, DIUpgradeMode: DebugInfoUpgradeMode::None);
802 return MetadataList.lookup(I: ID);
803 }
804 return MetadataList.getMetadataFwdRef(Idx: ID);
805 }
806
807 DISubprogram *lookupSubprogramForFunction(Function *F) {
808 return FunctionsWithSPs.lookup(Val: F);
809 }
810
811 bool hasSeenOldLoopTags() const { return HasSeenOldLoopTags; }
812
813 Error parseMetadataAttachment(Function &F,
814 ArrayRef<Instruction *> InstructionList);
815
816 Error parseMetadataKinds();
817
818 void setStripTBAA(bool Value) { StripTBAA = Value; }
819 bool isStrippingTBAA() const { return StripTBAA; }
820
821 unsigned size() const { return MetadataList.size(); }
822 void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
823 void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); }
824};
825
826Expected<bool>
827MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
828 IndexCursor = Stream;
829 SmallVector<uint64_t, 64> Record;
830 GlobalDeclAttachmentPos = 0;
831 // Get the abbrevs, and preload record positions to make them lazy-loadable.
832 while (true) {
833 uint64_t SavedPos = IndexCursor.GetCurrentBitNo();
834 BitstreamEntry Entry;
835 if (Error E =
836 IndexCursor
837 .advanceSkippingSubblocks(Flags: BitstreamCursor::AF_DontPopBlockAtEnd)
838 .moveInto(Value&: Entry))
839 return std::move(E);
840
841 switch (Entry.Kind) {
842 case BitstreamEntry::SubBlock: // Handled for us already.
843 case BitstreamEntry::Error:
844 return error(Message: "Malformed block");
845 case BitstreamEntry::EndBlock: {
846 return true;
847 }
848 case BitstreamEntry::Record: {
849 // The interesting case.
850 ++NumMDRecordLoaded;
851 uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
852 unsigned Code;
853 if (Error E = IndexCursor.skipRecord(AbbrevID: Entry.ID).moveInto(Value&: Code))
854 return std::move(E);
855 switch (Code) {
856 case bitc::METADATA_STRINGS: {
857 // Rewind and parse the strings.
858 if (Error Err = IndexCursor.JumpToBit(BitNo: CurrentPos))
859 return std::move(Err);
860 StringRef Blob;
861 Record.clear();
862 if (Expected<unsigned> MaybeRecord =
863 IndexCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob))
864 ;
865 else
866 return MaybeRecord.takeError();
867 unsigned NumStrings = Record[0];
868 MDStringRef.reserve(n: NumStrings);
869 auto IndexNextMDString = [&](StringRef Str) {
870 MDStringRef.push_back(x: Str);
871 };
872 if (auto Err = parseMetadataStrings(Record, Blob, CallBack: IndexNextMDString))
873 return std::move(Err);
874 break;
875 }
876 case bitc::METADATA_INDEX_OFFSET: {
877 // This is the offset to the index, when we see this we skip all the
878 // records and load only an index to these.
879 if (Error Err = IndexCursor.JumpToBit(BitNo: CurrentPos))
880 return std::move(Err);
881 Record.clear();
882 if (Expected<unsigned> MaybeRecord =
883 IndexCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
884 ;
885 else
886 return MaybeRecord.takeError();
887 if (Record.size() != 2)
888 return error(Message: "Invalid record");
889 auto Offset = Record[0] + (Record[1] << 32);
890 auto BeginPos = IndexCursor.GetCurrentBitNo();
891 if (Error Err = IndexCursor.JumpToBit(BitNo: BeginPos + Offset))
892 return std::move(Err);
893 Expected<BitstreamEntry> MaybeEntry =
894 IndexCursor.advanceSkippingSubblocks(
895 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
896 if (!MaybeEntry)
897 return MaybeEntry.takeError();
898 Entry = MaybeEntry.get();
899 assert(Entry.Kind == BitstreamEntry::Record &&
900 "Corrupted bitcode: Expected `Record` when trying to find the "
901 "Metadata index");
902 Record.clear();
903 if (Expected<unsigned> MaybeCode =
904 IndexCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
905 assert(MaybeCode.get() == bitc::METADATA_INDEX &&
906 "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "
907 "find the Metadata index");
908 else
909 return MaybeCode.takeError();
910 // Delta unpack
911 auto CurrentValue = BeginPos;
912 GlobalMetadataBitPosIndex.reserve(n: Record.size());
913 for (auto &Elt : Record) {
914 CurrentValue += Elt;
915 GlobalMetadataBitPosIndex.push_back(x: CurrentValue);
916 }
917 break;
918 }
919 case bitc::METADATA_INDEX:
920 // We don't expect to get there, the Index is loaded when we encounter
921 // the offset.
922 return error(Message: "Corrupted Metadata block");
923 case bitc::METADATA_NAME: {
924 // Named metadata need to be materialized now and aren't deferred.
925 if (Error Err = IndexCursor.JumpToBit(BitNo: CurrentPos))
926 return std::move(Err);
927 Record.clear();
928
929 unsigned Code;
930 if (Expected<unsigned> MaybeCode =
931 IndexCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record)) {
932 Code = MaybeCode.get();
933 assert(Code == bitc::METADATA_NAME);
934 } else
935 return MaybeCode.takeError();
936
937 // Read name of the named metadata.
938 SmallString<8> Name(Record.begin(), Record.end());
939 if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode())
940 Code = MaybeCode.get();
941 else
942 return MaybeCode.takeError();
943
944 // Named Metadata comes in two parts, we expect the name to be followed
945 // by the node
946 Record.clear();
947 if (Expected<unsigned> MaybeNextBitCode =
948 IndexCursor.readRecord(AbbrevID: Code, Vals&: Record))
949 assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE);
950 else
951 return MaybeNextBitCode.takeError();
952
953 // Read named metadata elements.
954 unsigned Size = Record.size();
955 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
956 for (unsigned i = 0; i != Size; ++i) {
957 // FIXME: We could use a placeholder here, however NamedMDNode are
958 // taking MDNode as operand and not using the Metadata infrastructure.
959 // It is acknowledged by 'TODO: Inherit from Metadata' in the
960 // NamedMDNode class definition.
961 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Idx: Record[i]);
962 assert(MD && "Invalid metadata: expect fwd ref to MDNode");
963 NMD->addOperand(M: MD);
964 }
965 break;
966 }
967 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
968 if (!GlobalDeclAttachmentPos)
969 GlobalDeclAttachmentPos = SavedPos;
970#ifndef NDEBUG
971 NumGlobalDeclAttachSkipped++;
972#endif
973 break;
974 }
975 case bitc::METADATA_KIND:
976 case bitc::METADATA_STRING_OLD:
977 case bitc::METADATA_OLD_FN_NODE:
978 case bitc::METADATA_OLD_NODE:
979 case bitc::METADATA_VALUE:
980 case bitc::METADATA_DISTINCT_NODE:
981 case bitc::METADATA_NODE:
982 case bitc::METADATA_LOCATION:
983 case bitc::METADATA_GENERIC_DEBUG:
984 case bitc::METADATA_SUBRANGE:
985 case bitc::METADATA_ENUMERATOR:
986 case bitc::METADATA_BASIC_TYPE:
987 case bitc::METADATA_STRING_TYPE:
988 case bitc::METADATA_DERIVED_TYPE:
989 case bitc::METADATA_COMPOSITE_TYPE:
990 case bitc::METADATA_SUBROUTINE_TYPE:
991 case bitc::METADATA_MODULE:
992 case bitc::METADATA_FILE:
993 case bitc::METADATA_COMPILE_UNIT:
994 case bitc::METADATA_SUBPROGRAM:
995 case bitc::METADATA_LEXICAL_BLOCK:
996 case bitc::METADATA_LEXICAL_BLOCK_FILE:
997 case bitc::METADATA_NAMESPACE:
998 case bitc::METADATA_COMMON_BLOCK:
999 case bitc::METADATA_MACRO:
1000 case bitc::METADATA_MACRO_FILE:
1001 case bitc::METADATA_TEMPLATE_TYPE:
1002 case bitc::METADATA_TEMPLATE_VALUE:
1003 case bitc::METADATA_GLOBAL_VAR:
1004 case bitc::METADATA_LOCAL_VAR:
1005 case bitc::METADATA_ASSIGN_ID:
1006 case bitc::METADATA_LABEL:
1007 case bitc::METADATA_EXPRESSION:
1008 case bitc::METADATA_OBJC_PROPERTY:
1009 case bitc::METADATA_IMPORTED_ENTITY:
1010 case bitc::METADATA_GLOBAL_VAR_EXPR:
1011 case bitc::METADATA_GENERIC_SUBRANGE:
1012 // We don't expect to see any of these, if we see one, give up on
1013 // lazy-loading and fallback.
1014 MDStringRef.clear();
1015 GlobalMetadataBitPosIndex.clear();
1016 return false;
1017 }
1018 break;
1019 }
1020 }
1021 }
1022}
1023
1024// Load the global decl attachments after building the lazy loading index.
1025// We don't load them "lazily" - all global decl attachments must be
1026// parsed since they aren't materialized on demand. However, by delaying
1027// their parsing until after the index is created, we can use the index
1028// instead of creating temporaries.
1029Expected<bool> MetadataLoader::MetadataLoaderImpl::loadGlobalDeclAttachments() {
1030 // Nothing to do if we didn't find any of these metadata records.
1031 if (!GlobalDeclAttachmentPos)
1032 return true;
1033 // Use a temporary cursor so that we don't mess up the main Stream cursor or
1034 // the lazy loading IndexCursor (which holds the necessary abbrev ids).
1035 BitstreamCursor TempCursor = Stream;
1036 SmallVector<uint64_t, 64> Record;
1037 // Jump to the position before the first global decl attachment, so we can
1038 // scan for the first BitstreamEntry record.
1039 if (Error Err = TempCursor.JumpToBit(BitNo: GlobalDeclAttachmentPos))
1040 return std::move(Err);
1041 while (true) {
1042 BitstreamEntry Entry;
1043 if (Error E =
1044 TempCursor
1045 .advanceSkippingSubblocks(Flags: BitstreamCursor::AF_DontPopBlockAtEnd)
1046 .moveInto(Value&: Entry))
1047 return std::move(E);
1048
1049 switch (Entry.Kind) {
1050 case BitstreamEntry::SubBlock: // Handled for us already.
1051 case BitstreamEntry::Error:
1052 return error(Message: "Malformed block");
1053 case BitstreamEntry::EndBlock:
1054 // Check that we parsed them all.
1055 assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed);
1056 return true;
1057 case BitstreamEntry::Record:
1058 break;
1059 }
1060 uint64_t CurrentPos = TempCursor.GetCurrentBitNo();
1061 Expected<unsigned> MaybeCode = TempCursor.skipRecord(AbbrevID: Entry.ID);
1062 if (!MaybeCode)
1063 return MaybeCode.takeError();
1064 if (MaybeCode.get() != bitc::METADATA_GLOBAL_DECL_ATTACHMENT) {
1065 // Anything other than a global decl attachment signals the end of
1066 // these records. Check that we parsed them all.
1067 assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed);
1068 return true;
1069 }
1070#ifndef NDEBUG
1071 NumGlobalDeclAttachParsed++;
1072#endif
1073 // FIXME: we need to do this early because we don't materialize global
1074 // value explicitly.
1075 if (Error Err = TempCursor.JumpToBit(BitNo: CurrentPos))
1076 return std::move(Err);
1077 Record.clear();
1078 if (Expected<unsigned> MaybeRecord =
1079 TempCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
1080 ;
1081 else
1082 return MaybeRecord.takeError();
1083 if (Record.size() % 2 == 0)
1084 return error(Message: "Invalid record");
1085 unsigned ValueID = Record[0];
1086 if (ValueID >= ValueList.size())
1087 return error(Message: "Invalid record");
1088 if (auto *GO = dyn_cast<GlobalObject>(Val: ValueList[ValueID])) {
1089 // Need to save and restore the current position since
1090 // parseGlobalObjectAttachment will resolve all forward references which
1091 // would require parsing from locations stored in the index.
1092 CurrentPos = TempCursor.GetCurrentBitNo();
1093 if (Error Err = parseGlobalObjectAttachment(
1094 GO&: *GO, Record: ArrayRef<uint64_t>(Record).slice(N: 1)))
1095 return std::move(Err);
1096 if (Error Err = TempCursor.JumpToBit(BitNo: CurrentPos))
1097 return std::move(Err);
1098 }
1099 }
1100}
1101
1102void MetadataLoader::MetadataLoaderImpl::callMDTypeCallback(Metadata **Val,
1103 unsigned TypeID) {
1104 if (Callbacks.MDType) {
1105 (*Callbacks.MDType)(Val, TypeID, Callbacks.GetTypeByID,
1106 Callbacks.GetContainedTypeID);
1107 }
1108}
1109
1110/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1111/// module level metadata.
1112Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
1113 llvm::TimeTraceScope timeScope("Parse metadata");
1114 if (!ModuleLevel && MetadataList.hasFwdRefs())
1115 return error(Message: "Invalid metadata: fwd refs into function blocks");
1116
1117 // Record the entry position so that we can jump back here and efficiently
1118 // skip the whole block in case we lazy-load.
1119 auto EntryPos = Stream.GetCurrentBitNo();
1120
1121 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::METADATA_BLOCK_ID))
1122 return Err;
1123
1124 SmallVector<uint64_t, 64> Record;
1125 PlaceholderQueue Placeholders;
1126 auto DIUpgradeMode = ModuleLevel ? DebugInfoUpgradeMode::ModuleLevel
1127 : DebugInfoUpgradeMode::Partial;
1128
1129 // We lazy-load module-level metadata: we build an index for each record, and
1130 // then load individual record as needed, starting with the named metadata.
1131 if (ModuleLevel && IsImporting && MetadataList.empty() &&
1132 !DisableLazyLoading) {
1133 auto SuccessOrErr = lazyLoadModuleMetadataBlock();
1134 if (!SuccessOrErr)
1135 return SuccessOrErr.takeError();
1136 if (SuccessOrErr.get()) {
1137 // An index was successfully created and we will be able to load metadata
1138 // on-demand.
1139 MetadataList.resize(N: MDStringRef.size() +
1140 GlobalMetadataBitPosIndex.size());
1141
1142 // Now that we have built the index, load the global decl attachments
1143 // that were deferred during that process. This avoids creating
1144 // temporaries.
1145 SuccessOrErr = loadGlobalDeclAttachments();
1146 if (!SuccessOrErr)
1147 return SuccessOrErr.takeError();
1148 assert(SuccessOrErr.get());
1149
1150 // Reading the named metadata created forward references and/or
1151 // placeholders, that we flush here.
1152 LLVM_DEBUG(llvm::dbgs() << "\nNamed metadata loading: ");
1153 resolveLoadedMetadata(Placeholders, DIUpgradeMode);
1154 // Return at the beginning of the block, since it is easy to skip it
1155 // entirely from there.
1156 Stream.ReadBlockEnd(); // Pop the abbrev block context.
1157 if (Error Err = IndexCursor.JumpToBit(BitNo: EntryPos))
1158 return Err;
1159 if (Error Err = Stream.SkipBlock()) {
1160 // FIXME this drops the error on the floor, which
1161 // ThinLTO/X86/debuginfo-cu-import.ll relies on.
1162 consumeError(Err: std::move(Err));
1163 return Error::success();
1164 }
1165 return Error::success();
1166 }
1167 // Couldn't load an index, fallback to loading all the block "old-style".
1168 }
1169
1170 unsigned NextMetadataNo = MetadataList.size();
1171
1172 // Read all the records.
1173 while (true) {
1174 BitstreamEntry Entry;
1175 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Value&: Entry))
1176 return E;
1177
1178 switch (Entry.Kind) {
1179 case BitstreamEntry::SubBlock: // Handled for us already.
1180 case BitstreamEntry::Error:
1181 return error(Message: "Malformed block");
1182 case BitstreamEntry::EndBlock:
1183 LLVM_DEBUG(llvm::dbgs() << "\nEager metadata loading: ");
1184 resolveLoadedMetadata(Placeholders, DIUpgradeMode);
1185 return Error::success();
1186 case BitstreamEntry::Record:
1187 // The interesting case.
1188 break;
1189 }
1190
1191 // Read a record.
1192 Record.clear();
1193 StringRef Blob;
1194 ++NumMDRecordLoaded;
1195 if (Expected<unsigned> MaybeCode =
1196 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob)) {
1197 if (Error Err = parseOneMetadata(Record, Code: MaybeCode.get(), Placeholders,
1198 Blob, NextMetadataNo))
1199 return Err;
1200 } else
1201 return MaybeCode.takeError();
1202 }
1203}
1204
1205MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
1206 ++NumMDStringLoaded;
1207 if (Metadata *MD = MetadataList.lookup(I: ID))
1208 return cast<MDString>(Val: MD);
1209 auto MDS = MDString::get(Context, Str: MDStringRef[ID]);
1210 MetadataList.assignValue(MD: MDS, Idx: ID);
1211 return MDS;
1212}
1213
1214void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
1215 unsigned ID, PlaceholderQueue &Placeholders) {
1216 assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
1217 assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
1218 // Lookup first if the metadata hasn't already been loaded.
1219 if (auto *MD = MetadataList.lookup(I: ID)) {
1220 auto *N = dyn_cast<MDNode>(Val: MD);
1221 // If the node is not an MDNode, or if it is not temporary, then
1222 // we're done.
1223 if (!N || !N->isTemporary())
1224 return;
1225 }
1226 SmallVector<uint64_t, 64> Record;
1227 StringRef Blob;
1228 if (Error Err = IndexCursor.JumpToBit(
1229 BitNo: GlobalMetadataBitPosIndex[ID - MDStringRef.size()]))
1230 report_fatal_error(reason: "lazyLoadOneMetadata failed jumping: " +
1231 Twine(toString(E: std::move(Err))));
1232 BitstreamEntry Entry;
1233 if (Error E = IndexCursor.advanceSkippingSubblocks().moveInto(Value&: Entry))
1234 // FIXME this drops the error on the floor.
1235 report_fatal_error(reason: "lazyLoadOneMetadata failed advanceSkippingSubblocks: " +
1236 Twine(toString(E: std::move(E))));
1237 ++NumMDRecordLoaded;
1238 if (Expected<unsigned> MaybeCode =
1239 IndexCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob)) {
1240 if (Error Err =
1241 parseOneMetadata(Record, Code: MaybeCode.get(), Placeholders, Blob, NextMetadataNo&: ID))
1242 report_fatal_error(reason: "Can't lazyload MD, parseOneMetadata: " +
1243 Twine(toString(E: std::move(Err))));
1244 } else
1245 report_fatal_error(reason: "Can't lazyload MD: " +
1246 Twine(toString(E: MaybeCode.takeError())));
1247}
1248
1249/// Ensure that all forward-references and placeholders are resolved.
1250/// Iteratively lazy-loading metadata on-demand if needed.
1251void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
1252 PlaceholderQueue &Placeholders) {
1253 DenseSet<unsigned> Temporaries;
1254 while (true) {
1255 // Populate Temporaries with the placeholders that haven't been loaded yet.
1256 Placeholders.getTemporaries(MetadataList, Temporaries);
1257
1258 // If we don't have any temporary, or FwdReference, we're done!
1259 if (Temporaries.empty() && !MetadataList.hasFwdRefs())
1260 break;
1261
1262 // First, load all the temporaries. This can add new placeholders or
1263 // forward references.
1264 for (auto ID : Temporaries)
1265 lazyLoadOneMetadata(ID, Placeholders);
1266 Temporaries.clear();
1267
1268 // Second, load the forward-references. This can also add new placeholders
1269 // or forward references.
1270 while (MetadataList.hasFwdRefs())
1271 lazyLoadOneMetadata(ID: MetadataList.getNextFwdRef(), Placeholders);
1272 }
1273 // At this point we don't have any forward reference remaining, or temporary
1274 // that haven't been loaded. We can safely drop RAUW support and mark cycles
1275 // as resolved.
1276 MetadataList.tryToResolveCycles();
1277
1278 // Finally, everything is in place, we can replace the placeholders operands
1279 // with the final node they refer to.
1280 Placeholders.flush(MetadataList);
1281}
1282
1283static Value *getValueFwdRef(BitcodeReaderValueList &ValueList, unsigned Idx,
1284 Type *Ty, unsigned TyID) {
1285 Value *V = ValueList.getValueFwdRef(Idx, Ty, TyID,
1286 /*ConstExprInsertBB*/ nullptr);
1287 if (V)
1288 return V;
1289
1290 // This is a reference to a no longer supported constant expression.
1291 // Pretend that the constant was deleted, which will replace metadata
1292 // references with poison.
1293 // TODO: This is a rather indirect check. It would be more elegant to use
1294 // a separate ErrorInfo for constant materialization failure and thread
1295 // the error reporting through getValueFwdRef().
1296 if (Idx < ValueList.size() && ValueList[Idx] &&
1297 ValueList[Idx]->getType() == Ty)
1298 return PoisonValue::get(T: Ty);
1299
1300 return nullptr;
1301}
1302
1303Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
1304 SmallVectorImpl<uint64_t> &Record, unsigned Code,
1305 PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
1306
1307 bool IsDistinct = false;
1308 auto getMD = [&](unsigned ID) -> Metadata * {
1309 if (ID < MDStringRef.size())
1310 return lazyLoadOneMDString(ID);
1311 if (!IsDistinct) {
1312 if (auto *MD = MetadataList.lookup(I: ID))
1313 return MD;
1314 // If lazy-loading is enabled, we try recursively to load the operand
1315 // instead of creating a temporary.
1316 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
1317 // Create a temporary for the node that is referencing the operand we
1318 // will lazy-load. It is needed before recursing in case there are
1319 // uniquing cycles.
1320 MetadataList.getMetadataFwdRef(Idx: NextMetadataNo);
1321 lazyLoadOneMetadata(ID, Placeholders);
1322 return MetadataList.lookup(I: ID);
1323 }
1324 // Return a temporary.
1325 return MetadataList.getMetadataFwdRef(Idx: ID);
1326 }
1327 if (auto *MD = MetadataList.getMetadataIfResolved(Idx: ID))
1328 return MD;
1329 return &Placeholders.getPlaceholderOp(ID);
1330 };
1331 auto getMDOrNull = [&](unsigned ID) -> Metadata * {
1332 if (ID)
1333 return getMD(ID - 1);
1334 return nullptr;
1335 };
1336 auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
1337 if (ID)
1338 return MetadataList.getMetadataFwdRef(Idx: ID - 1);
1339 return nullptr;
1340 };
1341 auto getMDString = [&](unsigned ID) -> MDString * {
1342 // This requires that the ID is not really a forward reference. In
1343 // particular, the MDString must already have been resolved.
1344 auto MDS = getMDOrNull(ID);
1345 return cast_or_null<MDString>(Val: MDS);
1346 };
1347
1348 // Support for old type refs.
1349 auto getDITypeRefOrNull = [&](unsigned ID) {
1350 return MetadataList.upgradeTypeRef(MaybeUUID: getMDOrNull(ID));
1351 };
1352
1353 auto getMetadataOrConstant = [&](bool IsMetadata,
1354 uint64_t Entry) -> Metadata * {
1355 if (IsMetadata)
1356 return getMDOrNull(Entry);
1357 return ConstantAsMetadata::get(
1358 C: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Entry));
1359 };
1360
1361#define GET_OR_DISTINCT(CLASS, ARGS) \
1362 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1363
1364 switch (Code) {
1365 default: // Default behavior: ignore.
1366 break;
1367 case bitc::METADATA_NAME: {
1368 // Read name of the named metadata.
1369 SmallString<8> Name(Record.begin(), Record.end());
1370 Record.clear();
1371 if (Error E = Stream.ReadCode().moveInto(Value&: Code))
1372 return E;
1373
1374 ++NumMDRecordLoaded;
1375 if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(AbbrevID: Code, Vals&: Record)) {
1376 if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE)
1377 return error(Message: "METADATA_NAME not followed by METADATA_NAMED_NODE");
1378 } else
1379 return MaybeNextBitCode.takeError();
1380
1381 // Read named metadata elements.
1382 unsigned Size = Record.size();
1383 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
1384 for (unsigned i = 0; i != Size; ++i) {
1385 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Idx: Record[i]);
1386 if (!MD)
1387 return error(Message: "Invalid named metadata: expect fwd ref to MDNode");
1388 NMD->addOperand(M: MD);
1389 }
1390 break;
1391 }
1392 case bitc::METADATA_OLD_FN_NODE: {
1393 // Deprecated, but still needed to read old bitcode files.
1394 // This is a LocalAsMetadata record, the only type of function-local
1395 // metadata.
1396 if (Record.size() % 2 == 1)
1397 return error(Message: "Invalid record");
1398
1399 // If this isn't a LocalAsMetadata record, we're dropping it. This used
1400 // to be legal, but there's no upgrade path.
1401 auto dropRecord = [&] {
1402 MetadataList.assignValue(MD: MDNode::get(Context, MDs: {}), Idx: NextMetadataNo);
1403 NextMetadataNo++;
1404 };
1405 if (Record.size() != 2) {
1406 dropRecord();
1407 break;
1408 }
1409
1410 unsigned TyID = Record[0];
1411 Type *Ty = Callbacks.GetTypeByID(TyID);
1412 if (!Ty || Ty->isMetadataTy() || Ty->isVoidTy()) {
1413 dropRecord();
1414 break;
1415 }
1416
1417 Value *V = ValueList.getValueFwdRef(Idx: Record[1], Ty, TyID,
1418 /*ConstExprInsertBB*/ nullptr);
1419 if (!V)
1420 return error(Message: "Invalid value reference from old fn metadata");
1421
1422 MetadataList.assignValue(MD: LocalAsMetadata::get(Local: V), Idx: NextMetadataNo);
1423 NextMetadataNo++;
1424 break;
1425 }
1426 case bitc::METADATA_OLD_NODE: {
1427 // Deprecated, but still needed to read old bitcode files.
1428 if (Record.size() % 2 == 1)
1429 return error(Message: "Invalid record");
1430
1431 unsigned Size = Record.size();
1432 SmallVector<Metadata *, 8> Elts;
1433 for (unsigned i = 0; i != Size; i += 2) {
1434 unsigned TyID = Record[i];
1435 Type *Ty = Callbacks.GetTypeByID(TyID);
1436 if (!Ty)
1437 return error(Message: "Invalid record");
1438 if (Ty->isMetadataTy())
1439 Elts.push_back(Elt: getMD(Record[i + 1]));
1440 else if (!Ty->isVoidTy()) {
1441 Value *V = getValueFwdRef(ValueList, Idx: Record[i + 1], Ty, TyID);
1442 if (!V)
1443 return error(Message: "Invalid value reference from old metadata");
1444 Metadata *MD = ValueAsMetadata::get(V);
1445 assert(isa<ConstantAsMetadata>(MD) &&
1446 "Expected non-function-local metadata");
1447 callMDTypeCallback(Val: &MD, TypeID: TyID);
1448 Elts.push_back(Elt: MD);
1449 } else
1450 Elts.push_back(Elt: nullptr);
1451 }
1452 MetadataList.assignValue(MD: MDNode::get(Context, MDs: Elts), Idx: NextMetadataNo);
1453 NextMetadataNo++;
1454 break;
1455 }
1456 case bitc::METADATA_VALUE: {
1457 if (Record.size() != 2)
1458 return error(Message: "Invalid record");
1459
1460 unsigned TyID = Record[0];
1461 Type *Ty = Callbacks.GetTypeByID(TyID);
1462 if (!Ty || Ty->isMetadataTy() || Ty->isVoidTy())
1463 return error(Message: "Invalid record");
1464
1465 Value *V = getValueFwdRef(ValueList, Idx: Record[1], Ty, TyID);
1466 if (!V)
1467 return error(Message: "Invalid value reference from metadata");
1468
1469 Metadata *MD = ValueAsMetadata::get(V);
1470 callMDTypeCallback(Val: &MD, TypeID: TyID);
1471 MetadataList.assignValue(MD, Idx: NextMetadataNo);
1472 NextMetadataNo++;
1473 break;
1474 }
1475 case bitc::METADATA_DISTINCT_NODE:
1476 IsDistinct = true;
1477 [[fallthrough]];
1478 case bitc::METADATA_NODE: {
1479 SmallVector<Metadata *, 8> Elts;
1480 Elts.reserve(N: Record.size());
1481 for (unsigned ID : Record)
1482 Elts.push_back(Elt: getMDOrNull(ID));
1483 MetadataList.assignValue(MD: IsDistinct ? MDNode::getDistinct(Context, MDs: Elts)
1484 : MDNode::get(Context, MDs: Elts),
1485 Idx: NextMetadataNo);
1486 NextMetadataNo++;
1487 break;
1488 }
1489 case bitc::METADATA_LOCATION: {
1490 // 5: inlinedAt, 6: isImplicit, 8: Key Instructions fields.
1491 if (Record.size() != 5 && Record.size() != 6 && Record.size() != 8)
1492 return error(Message: "Invalid record");
1493
1494 IsDistinct = Record[0];
1495 unsigned Line = Record[1];
1496 unsigned Column = Record[2];
1497 Metadata *Scope = getMD(Record[3]);
1498 Metadata *InlinedAt = getMDOrNull(Record[4]);
1499 bool ImplicitCode = Record.size() >= 6 && Record[5];
1500 uint64_t AtomGroup = Record.size() == 8 ? Record[6] : 0;
1501 uint8_t AtomRank = Record.size() == 8 ? Record[7] : 0;
1502 MetadataList.assignValue(
1503 GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt,
1504 ImplicitCode, AtomGroup, AtomRank)),
1505 Idx: NextMetadataNo);
1506 NextMetadataNo++;
1507 break;
1508 }
1509 case bitc::METADATA_GENERIC_DEBUG: {
1510 if (Record.size() < 4)
1511 return error(Message: "Invalid record");
1512
1513 IsDistinct = Record[0];
1514 unsigned Tag = Record[1];
1515 unsigned Version = Record[2];
1516
1517 if (Tag >= 1u << 16 || Version != 0)
1518 return error(Message: "Invalid record");
1519
1520 auto *Header = getMDString(Record[3]);
1521 SmallVector<Metadata *, 8> DwarfOps;
1522 for (unsigned I = 4, E = Record.size(); I != E; ++I)
1523 DwarfOps.push_back(Elt: getMDOrNull(Record[I]));
1524 MetadataList.assignValue(
1525 GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1526 Idx: NextMetadataNo);
1527 NextMetadataNo++;
1528 break;
1529 }
1530 case bitc::METADATA_SUBRANGE: {
1531 Metadata *Val = nullptr;
1532 // Operand 'count' is interpreted as:
1533 // - Signed integer (version 0)
1534 // - Metadata node (version 1)
1535 // Operand 'lowerBound' is interpreted as:
1536 // - Signed integer (version 0 and 1)
1537 // - Metadata node (version 2)
1538 // Operands 'upperBound' and 'stride' are interpreted as:
1539 // - Metadata node (version 2)
1540 switch (Record[0] >> 1) {
1541 case 0:
1542 Val = GET_OR_DISTINCT(DISubrange,
1543 (Context, Record[1], unrotateSign(Record[2])));
1544 break;
1545 case 1:
1546 Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]),
1547 unrotateSign(Record[2])));
1548 break;
1549 case 2:
1550 Val = GET_OR_DISTINCT(
1551 DISubrange, (Context, getMDOrNull(Record[1]), getMDOrNull(Record[2]),
1552 getMDOrNull(Record[3]), getMDOrNull(Record[4])));
1553 break;
1554 default:
1555 return error(Message: "Invalid record: Unsupported version of DISubrange");
1556 }
1557
1558 MetadataList.assignValue(MD: Val, Idx: NextMetadataNo);
1559 IsDistinct = Record[0] & 1;
1560 NextMetadataNo++;
1561 break;
1562 }
1563 case bitc::METADATA_GENERIC_SUBRANGE: {
1564 Metadata *Val = nullptr;
1565 Val = GET_OR_DISTINCT(DIGenericSubrange,
1566 (Context, getMDOrNull(Record[1]),
1567 getMDOrNull(Record[2]), getMDOrNull(Record[3]),
1568 getMDOrNull(Record[4])));
1569
1570 MetadataList.assignValue(MD: Val, Idx: NextMetadataNo);
1571 IsDistinct = Record[0] & 1;
1572 NextMetadataNo++;
1573 break;
1574 }
1575 case bitc::METADATA_ENUMERATOR: {
1576 if (Record.size() < 3)
1577 return error(Message: "Invalid record");
1578
1579 IsDistinct = Record[0] & 1;
1580 bool IsUnsigned = Record[0] & 2;
1581 bool IsBigInt = Record[0] & 4;
1582 APInt Value;
1583
1584 if (IsBigInt) {
1585 const uint64_t BitWidth = Record[1];
1586 const size_t NumWords = Record.size() - 3;
1587 Value = readWideAPInt(Vals: ArrayRef(&Record[3], NumWords), TypeBits: BitWidth);
1588 } else
1589 Value = APInt(64, unrotateSign(U: Record[1]), !IsUnsigned);
1590
1591 MetadataList.assignValue(
1592 GET_OR_DISTINCT(DIEnumerator,
1593 (Context, Value, IsUnsigned, getMDString(Record[2]))),
1594 Idx: NextMetadataNo);
1595 NextMetadataNo++;
1596 break;
1597 }
1598 case bitc::METADATA_BASIC_TYPE: {
1599 if (Record.size() < 6 || Record.size() > 9)
1600 return error(Message: "Invalid record");
1601
1602 IsDistinct = Record[0] & 1;
1603 bool SizeIsMetadata = Record[0] & 2;
1604 DINode::DIFlags Flags = (Record.size() > 6)
1605 ? static_cast<DINode::DIFlags>(Record[6])
1606 : DINode::FlagZero;
1607 uint32_t NumExtraInhabitants = (Record.size() > 7) ? Record[7] : 0;
1608 uint32_t DataSizeInBits = (Record.size() > 8) ? Record[8] : 0;
1609 Metadata *SizeInBits = getMetadataOrConstant(SizeIsMetadata, Record[3]);
1610 MetadataList.assignValue(
1611 GET_OR_DISTINCT(DIBasicType,
1612 (Context, Record[1], getMDString(Record[2]), SizeInBits,
1613 Record[4], Record[5], NumExtraInhabitants,
1614 DataSizeInBits, Flags)),
1615 Idx: NextMetadataNo);
1616 NextMetadataNo++;
1617 break;
1618 }
1619 case bitc::METADATA_FIXED_POINT_TYPE: {
1620 if (Record.size() < 11)
1621 return error(Message: "Invalid record");
1622
1623 IsDistinct = Record[0] & 1;
1624 bool SizeIsMetadata = Record[0] & 2;
1625 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[6]);
1626
1627 Metadata *SizeInBits = getMetadataOrConstant(SizeIsMetadata, Record[3]);
1628
1629 size_t Offset = 9;
1630
1631 auto ReadWideInt = [&]() {
1632 uint64_t Encoded = Record[Offset++];
1633 unsigned NumWords = Encoded >> 32;
1634 unsigned BitWidth = Encoded & 0xffffffff;
1635 auto Value = readWideAPInt(Vals: ArrayRef(&Record[Offset], NumWords), TypeBits: BitWidth);
1636 Offset += NumWords;
1637 return Value;
1638 };
1639
1640 APInt Numerator = ReadWideInt();
1641 APInt Denominator = ReadWideInt();
1642
1643 if (Offset != Record.size())
1644 return error(Message: "Invalid record");
1645
1646 MetadataList.assignValue(
1647 GET_OR_DISTINCT(DIFixedPointType,
1648 (Context, Record[1], getMDString(Record[2]), SizeInBits,
1649 Record[4], Record[5], Flags, Record[7], Record[8],
1650 Numerator, Denominator)),
1651 Idx: NextMetadataNo);
1652 NextMetadataNo++;
1653 break;
1654 }
1655 case bitc::METADATA_STRING_TYPE: {
1656 if (Record.size() > 9 || Record.size() < 8)
1657 return error(Message: "Invalid record");
1658
1659 IsDistinct = Record[0] & 1;
1660 bool SizeIsMetadata = Record[0] & 2;
1661 bool SizeIs8 = Record.size() == 8;
1662 // StringLocationExp (i.e. Record[5]) is added at a later time
1663 // than the other fields. The code here enables backward compatibility.
1664 Metadata *StringLocationExp = SizeIs8 ? nullptr : getMDOrNull(Record[5]);
1665 unsigned Offset = SizeIs8 ? 5 : 6;
1666 Metadata *SizeInBits =
1667 getMetadataOrConstant(SizeIsMetadata, Record[Offset]);
1668
1669 MetadataList.assignValue(
1670 GET_OR_DISTINCT(DIStringType,
1671 (Context, Record[1], getMDString(Record[2]),
1672 getMDOrNull(Record[3]), getMDOrNull(Record[4]),
1673 StringLocationExp, SizeInBits, Record[Offset + 1],
1674 Record[Offset + 2])),
1675 Idx: NextMetadataNo);
1676 NextMetadataNo++;
1677 break;
1678 }
1679 case bitc::METADATA_DERIVED_TYPE: {
1680 if (Record.size() < 12 || Record.size() > 15)
1681 return error(Message: "Invalid record");
1682
1683 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1684 // that there is no DWARF address space associated with DIDerivedType.
1685 std::optional<unsigned> DWARFAddressSpace;
1686 if (Record.size() > 12 && Record[12])
1687 DWARFAddressSpace = Record[12] - 1;
1688
1689 Metadata *Annotations = nullptr;
1690 std::optional<DIDerivedType::PtrAuthData> PtrAuthData;
1691
1692 // Only look for annotations/ptrauth if both are allocated.
1693 // If not, we can't tell which was intended to be embedded, as both ptrauth
1694 // and annotations have been expected at Record[13] at various times.
1695 if (Record.size() > 14) {
1696 if (Record[13])
1697 Annotations = getMDOrNull(Record[13]);
1698 if (Record[14])
1699 PtrAuthData.emplace(args&: Record[14]);
1700 }
1701
1702 IsDistinct = Record[0] & 1;
1703 bool SizeIsMetadata = Record[0] & 2;
1704 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1705
1706 Metadata *SizeInBits = getMetadataOrConstant(SizeIsMetadata, Record[7]);
1707 Metadata *OffsetInBits = getMetadataOrConstant(SizeIsMetadata, Record[9]);
1708
1709 MetadataList.assignValue(
1710 GET_OR_DISTINCT(DIDerivedType,
1711 (Context, Record[1], getMDString(Record[2]),
1712 getMDOrNull(Record[3]), Record[4],
1713 getDITypeRefOrNull(Record[5]),
1714 getDITypeRefOrNull(Record[6]), SizeInBits, Record[8],
1715 OffsetInBits, DWARFAddressSpace, PtrAuthData, Flags,
1716 getDITypeRefOrNull(Record[11]), Annotations)),
1717 Idx: NextMetadataNo);
1718 NextMetadataNo++;
1719 break;
1720 }
1721 case bitc::METADATA_SUBRANGE_TYPE: {
1722 if (Record.size() != 13)
1723 return error(Message: "Invalid record");
1724
1725 IsDistinct = Record[0] & 1;
1726 bool SizeIsMetadata = Record[0] & 2;
1727 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7]);
1728
1729 Metadata *SizeInBits = getMetadataOrConstant(SizeIsMetadata, Record[5]);
1730
1731 MetadataList.assignValue(
1732 GET_OR_DISTINCT(DISubrangeType,
1733 (Context, getMDString(Record[1]),
1734 getMDOrNull(Record[2]), Record[3],
1735 getMDOrNull(Record[4]), SizeInBits, Record[6], Flags,
1736 getDITypeRefOrNull(Record[8]), getMDOrNull(Record[9]),
1737 getMDOrNull(Record[10]), getMDOrNull(Record[11]),
1738 getMDOrNull(Record[12]))),
1739 Idx: NextMetadataNo);
1740 NextMetadataNo++;
1741 break;
1742 }
1743 case bitc::METADATA_COMPOSITE_TYPE: {
1744 if (Record.size() < 16 || Record.size() > 26)
1745 return error(Message: "Invalid record");
1746
1747 // If we have a UUID and this is not a forward declaration, lookup the
1748 // mapping.
1749 IsDistinct = Record[0] & 0x1;
1750 bool IsNotUsedInTypeRef = Record[0] & 2;
1751 bool SizeIsMetadata = Record[0] & 4;
1752 unsigned Tag = Record[1];
1753 MDString *Name = getMDString(Record[2]);
1754 Metadata *File = getMDOrNull(Record[3]);
1755 unsigned Line = Record[4];
1756 Metadata *Scope = getDITypeRefOrNull(Record[5]);
1757 Metadata *BaseType = nullptr;
1758 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1759 return error(Message: "Alignment value is too large");
1760 uint32_t AlignInBits = Record[8];
1761 Metadata *OffsetInBits = nullptr;
1762 uint32_t NumExtraInhabitants = (Record.size() > 22) ? Record[22] : 0;
1763 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1764 Metadata *Elements = nullptr;
1765 unsigned RuntimeLang = Record[12];
1766 std::optional<uint32_t> EnumKind;
1767
1768 Metadata *VTableHolder = nullptr;
1769 Metadata *TemplateParams = nullptr;
1770 Metadata *Discriminator = nullptr;
1771 Metadata *DataLocation = nullptr;
1772 Metadata *Associated = nullptr;
1773 Metadata *Allocated = nullptr;
1774 Metadata *Rank = nullptr;
1775 Metadata *Annotations = nullptr;
1776 Metadata *Specification = nullptr;
1777 Metadata *BitStride = nullptr;
1778 auto *Identifier = getMDString(Record[15]);
1779 // If this module is being parsed so that it can be ThinLTO imported
1780 // into another module, composite types only need to be imported as
1781 // type declarations (unless full type definitions are requested).
1782 // Create type declarations up front to save memory. This is only
1783 // done for types which have an Identifier, and are therefore
1784 // subject to the ODR.
1785 //
1786 // buildODRType handles the case where this is type ODRed with a
1787 // definition needed by the importing module, in which case the
1788 // existing definition is used.
1789 //
1790 // We always import full definitions for anonymous composite types,
1791 // as without a name, debuggers cannot easily resolve a declaration
1792 // to its definition.
1793 if (IsImporting && !ImportFullTypeDefinitions && Identifier && Name &&
1794 (Tag == dwarf::DW_TAG_enumeration_type ||
1795 Tag == dwarf::DW_TAG_class_type ||
1796 Tag == dwarf::DW_TAG_structure_type ||
1797 Tag == dwarf::DW_TAG_union_type)) {
1798 Flags = Flags | DINode::FlagFwdDecl;
1799 // This is a hack around preserving template parameters for simplified
1800 // template names - it should probably be replaced with a
1801 // DICompositeType flag specifying whether template parameters are
1802 // required on declarations of this type.
1803 StringRef NameStr = Name->getString();
1804 if (!NameStr.contains(C: '<') || NameStr.starts_with(Prefix: "_STN|"))
1805 TemplateParams = getMDOrNull(Record[14]);
1806 } else {
1807 BaseType = getDITypeRefOrNull(Record[6]);
1808
1809 OffsetInBits = getMetadataOrConstant(SizeIsMetadata, Record[9]);
1810
1811 Elements = getMDOrNull(Record[11]);
1812 VTableHolder = getDITypeRefOrNull(Record[13]);
1813 TemplateParams = getMDOrNull(Record[14]);
1814 if (Record.size() > 16)
1815 Discriminator = getMDOrNull(Record[16]);
1816 if (Record.size() > 17)
1817 DataLocation = getMDOrNull(Record[17]);
1818 if (Record.size() > 19) {
1819 Associated = getMDOrNull(Record[18]);
1820 Allocated = getMDOrNull(Record[19]);
1821 }
1822 if (Record.size() > 20) {
1823 Rank = getMDOrNull(Record[20]);
1824 }
1825 if (Record.size() > 21) {
1826 Annotations = getMDOrNull(Record[21]);
1827 }
1828 if (Record.size() > 23) {
1829 Specification = getMDOrNull(Record[23]);
1830 }
1831 if (Record.size() > 25)
1832 BitStride = getMDOrNull(Record[25]);
1833 }
1834
1835 if (Record.size() > 24 && Record[24] != dwarf::DW_APPLE_ENUM_KIND_invalid)
1836 EnumKind = Record[24];
1837
1838 Metadata *SizeInBits = getMetadataOrConstant(SizeIsMetadata, Record[7]);
1839
1840 DICompositeType *CT = nullptr;
1841 if (Identifier)
1842 CT = DICompositeType::buildODRType(
1843 Context, Identifier&: *Identifier, Tag, Name, File, Line, Scope, BaseType,
1844 SizeInBits, AlignInBits, OffsetInBits, Specification,
1845 NumExtraInhabitants, Flags, Elements, RuntimeLang, EnumKind,
1846 VTableHolder, TemplateParams, Discriminator, DataLocation, Associated,
1847 Allocated, Rank, Annotations, BitStride);
1848
1849 // Create a node if we didn't get a lazy ODR type.
1850 if (!CT)
1851 CT = GET_OR_DISTINCT(
1852 DICompositeType,
1853 (Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1854 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, EnumKind,
1855 VTableHolder, TemplateParams, Identifier, Discriminator,
1856 DataLocation, Associated, Allocated, Rank, Annotations,
1857 Specification, NumExtraInhabitants, BitStride));
1858 if (!IsNotUsedInTypeRef && Identifier)
1859 MetadataList.addTypeRef(UUID&: *Identifier, CT&: *cast<DICompositeType>(Val: CT));
1860
1861 MetadataList.assignValue(MD: CT, Idx: NextMetadataNo);
1862 NextMetadataNo++;
1863 break;
1864 }
1865 case bitc::METADATA_SUBROUTINE_TYPE: {
1866 if (Record.size() < 3 || Record.size() > 4)
1867 return error(Message: "Invalid record");
1868 bool IsOldTypeArray = Record[0] < 2;
1869 unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1870
1871 IsDistinct = Record[0] & 0x1;
1872 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1873 Metadata *Types = getMDOrNull(Record[2]);
1874 if (LLVM_UNLIKELY(IsOldTypeArray))
1875 Types = MetadataList.upgradeTypeArray(MaybeTuple: Types);
1876
1877 MetadataList.assignValue(
1878 GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1879 Idx: NextMetadataNo);
1880 NextMetadataNo++;
1881 break;
1882 }
1883
1884 case bitc::METADATA_MODULE: {
1885 if (Record.size() < 5 || Record.size() > 9)
1886 return error(Message: "Invalid record");
1887
1888 unsigned Offset = Record.size() >= 8 ? 2 : 1;
1889 IsDistinct = Record[0];
1890 MetadataList.assignValue(
1891 GET_OR_DISTINCT(
1892 DIModule,
1893 (Context, Record.size() >= 8 ? getMDOrNull(Record[1]) : nullptr,
1894 getMDOrNull(Record[0 + Offset]), getMDString(Record[1 + Offset]),
1895 getMDString(Record[2 + Offset]), getMDString(Record[3 + Offset]),
1896 getMDString(Record[4 + Offset]),
1897 Record.size() <= 7 ? 0 : Record[7],
1898 Record.size() <= 8 ? false : Record[8])),
1899 Idx: NextMetadataNo);
1900 NextMetadataNo++;
1901 break;
1902 }
1903
1904 case bitc::METADATA_FILE: {
1905 if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6)
1906 return error(Message: "Invalid record");
1907
1908 IsDistinct = Record[0];
1909 std::optional<DIFile::ChecksumInfo<MDString *>> Checksum;
1910 // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum
1911 // is not present. This matches up with the old internal representation,
1912 // and the old encoding for CSK_None in the ChecksumKind. The new
1913 // representation reserves the value 0 in the ChecksumKind to continue to
1914 // encode None in a backwards-compatible way.
1915 if (Record.size() > 4 && Record[3] && Record[4])
1916 Checksum.emplace(args: static_cast<DIFile::ChecksumKind>(Record[3]),
1917 args: getMDString(Record[4]));
1918 MetadataList.assignValue(
1919 GET_OR_DISTINCT(DIFile,
1920 (Context, getMDString(Record[1]),
1921 getMDString(Record[2]), Checksum,
1922 Record.size() > 5 ? getMDString(Record[5]) : nullptr)),
1923 Idx: NextMetadataNo);
1924 NextMetadataNo++;
1925 break;
1926 }
1927 case bitc::METADATA_COMPILE_UNIT: {
1928 if (Record.size() < 14 || Record.size() > 23)
1929 return error(Message: "Invalid record");
1930
1931 // Ignore Record[0], which indicates whether this compile unit is
1932 // distinct. It's always distinct.
1933 IsDistinct = true;
1934
1935 const auto LangVersionMask = (uint64_t(1) << 63);
1936 const bool HasVersionedLanguage = Record[1] & LangVersionMask;
1937 const uint32_t LanguageVersion = Record.size() > 22 ? Record[22] : 0;
1938
1939 auto *CU = DICompileUnit::getDistinct(
1940 Context,
1941 SourceLanguage: HasVersionedLanguage
1942 ? DISourceLanguageName(Record[1] & ~LangVersionMask,
1943 LanguageVersion)
1944 : DISourceLanguageName(Record[1]),
1945 File: getMDOrNull(Record[2]), Producer: getMDString(Record[3]), IsOptimized: Record[4],
1946 Flags: getMDString(Record[5]), RuntimeVersion: Record[6], SplitDebugFilename: getMDString(Record[7]), EmissionKind: Record[8],
1947 EnumTypes: getMDOrNull(Record[9]), RetainedTypes: getMDOrNull(Record[10]),
1948 GlobalVariables: getMDOrNull(Record[12]), ImportedEntities: getMDOrNull(Record[13]),
1949 Macros: Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1950 DWOId: Record.size() <= 14 ? 0 : Record[14],
1951 SplitDebugInlining: Record.size() <= 16 ? true : Record[16],
1952 DebugInfoForProfiling: Record.size() <= 17 ? false : Record[17],
1953 NameTableKind: Record.size() <= 18 ? 0 : Record[18],
1954 RangesBaseAddress: Record.size() <= 19 ? false : Record[19],
1955 SysRoot: Record.size() <= 20 ? nullptr : getMDString(Record[20]),
1956 SDK: Record.size() <= 21 ? nullptr : getMDString(Record[21]));
1957
1958 MetadataList.assignValue(MD: CU, Idx: NextMetadataNo);
1959 NextMetadataNo++;
1960
1961 // Move the Upgrade the list of subprograms.
1962 if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1963 CUSubprograms.push_back(x: {CU, SPs});
1964 break;
1965 }
1966 case bitc::METADATA_SUBPROGRAM: {
1967 if (Record.size() < 18 || Record.size() > 22)
1968 return error(Message: "Invalid record");
1969
1970 bool HasSPFlags = Record[0] & 4;
1971
1972 DINode::DIFlags Flags;
1973 DISubprogram::DISPFlags SPFlags;
1974 if (!HasSPFlags)
1975 Flags = static_cast<DINode::DIFlags>(Record[11 + 2]);
1976 else {
1977 Flags = static_cast<DINode::DIFlags>(Record[11]);
1978 SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]);
1979 }
1980
1981 // Support for old metadata when
1982 // subprogram specific flags are placed in DIFlags.
1983 const unsigned DIFlagMainSubprogram = 1 << 21;
1984 bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram;
1985 if (HasOldMainSubprogramFlag)
1986 // Remove old DIFlagMainSubprogram from DIFlags.
1987 // Note: This assumes that any future use of bit 21 defaults to it
1988 // being 0.
1989 Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram);
1990
1991 if (HasOldMainSubprogramFlag && HasSPFlags)
1992 SPFlags |= DISubprogram::SPFlagMainSubprogram;
1993 else if (!HasSPFlags)
1994 SPFlags = DISubprogram::toSPFlags(
1995 /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8],
1996 /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11],
1997 /*IsMainSubprogram=*/HasOldMainSubprogramFlag);
1998
1999 // All definitions should be distinct.
2000 IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition);
2001 // Version 1 has a Function as Record[15].
2002 // Version 2 has removed Record[15].
2003 // Version 3 has the Unit as Record[15].
2004 // Version 4 added thisAdjustment.
2005 // Version 5 repacked flags into DISPFlags, changing many element numbers.
2006 bool HasUnit = Record[0] & 2;
2007 if (!HasSPFlags && HasUnit && Record.size() < 19)
2008 return error(Message: "Invalid record");
2009 if (HasSPFlags && !HasUnit)
2010 return error(Message: "Invalid record");
2011 // Accommodate older formats.
2012 bool HasFn = false;
2013 bool HasThisAdj = true;
2014 bool HasThrownTypes = true;
2015 bool HasAnnotations = false;
2016 bool HasTargetFuncName = false;
2017 unsigned OffsetA = 0;
2018 unsigned OffsetB = 0;
2019 // Key instructions won't be enabled in old-format bitcode, so only
2020 // check it if HasSPFlags is true.
2021 bool UsesKeyInstructions = false;
2022 if (!HasSPFlags) {
2023 OffsetA = 2;
2024 OffsetB = 2;
2025 if (Record.size() >= 19) {
2026 HasFn = !HasUnit;
2027 OffsetB++;
2028 }
2029 HasThisAdj = Record.size() >= 20;
2030 HasThrownTypes = Record.size() >= 21;
2031 } else {
2032 HasAnnotations = Record.size() >= 19;
2033 HasTargetFuncName = Record.size() >= 20;
2034 UsesKeyInstructions = Record.size() >= 21 ? Record[20] : 0;
2035 }
2036
2037 Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
2038 DISubprogram *SP = GET_OR_DISTINCT(
2039 DISubprogram,
2040 (Context,
2041 getDITypeRefOrNull(Record[1]), // scope
2042 getMDString(Record[2]), // name
2043 getMDString(Record[3]), // linkageName
2044 getMDOrNull(Record[4]), // file
2045 Record[5], // line
2046 getMDOrNull(Record[6]), // type
2047 Record[7 + OffsetA], // scopeLine
2048 getDITypeRefOrNull(Record[8 + OffsetA]), // containingType
2049 Record[10 + OffsetA], // virtualIndex
2050 HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment
2051 Flags, // flags
2052 SPFlags, // SPFlags
2053 HasUnit ? CUorFn : nullptr, // unit
2054 getMDOrNull(Record[13 + OffsetB]), // templateParams
2055 getMDOrNull(Record[14 + OffsetB]), // declaration
2056 getMDOrNull(Record[15 + OffsetB]), // retainedNodes
2057 HasThrownTypes ? getMDOrNull(Record[17 + OffsetB])
2058 : nullptr, // thrownTypes
2059 HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
2060 : nullptr, // annotations
2061 HasTargetFuncName ? getMDString(Record[19 + OffsetB])
2062 : nullptr, // targetFuncName
2063 UsesKeyInstructions));
2064 MetadataList.assignValue(MD: SP, Idx: NextMetadataNo);
2065 NextMetadataNo++;
2066
2067 if (IsDistinct)
2068 NewDistinctSPs.push_back(Elt: SP);
2069
2070 // Upgrade sp->function mapping to function->sp mapping.
2071 if (HasFn) {
2072 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Val: CUorFn))
2073 if (auto *F = dyn_cast<Function>(Val: CMD->getValue())) {
2074 if (F->isMaterializable())
2075 // Defer until materialized; unmaterialized functions may not have
2076 // metadata.
2077 FunctionsWithSPs[F] = SP;
2078 else if (!F->empty())
2079 F->setSubprogram(SP);
2080 }
2081 }
2082 break;
2083 }
2084 case bitc::METADATA_LEXICAL_BLOCK: {
2085 if (Record.size() != 5)
2086 return error(Message: "Invalid record");
2087
2088 IsDistinct = Record[0];
2089 MetadataList.assignValue(
2090 GET_OR_DISTINCT(DILexicalBlock,
2091 (Context, getMDOrNull(Record[1]),
2092 getMDOrNull(Record[2]), Record[3], Record[4])),
2093 Idx: NextMetadataNo);
2094 NextMetadataNo++;
2095 break;
2096 }
2097 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
2098 if (Record.size() != 4)
2099 return error(Message: "Invalid record");
2100
2101 IsDistinct = Record[0];
2102 MetadataList.assignValue(
2103 GET_OR_DISTINCT(DILexicalBlockFile,
2104 (Context, getMDOrNull(Record[1]),
2105 getMDOrNull(Record[2]), Record[3])),
2106 Idx: NextMetadataNo);
2107 NextMetadataNo++;
2108 break;
2109 }
2110 case bitc::METADATA_COMMON_BLOCK: {
2111 IsDistinct = Record[0] & 1;
2112 MetadataList.assignValue(
2113 GET_OR_DISTINCT(DICommonBlock,
2114 (Context, getMDOrNull(Record[1]),
2115 getMDOrNull(Record[2]), getMDString(Record[3]),
2116 getMDOrNull(Record[4]), Record[5])),
2117 Idx: NextMetadataNo);
2118 NextMetadataNo++;
2119 break;
2120 }
2121 case bitc::METADATA_NAMESPACE: {
2122 // Newer versions of DINamespace dropped file and line.
2123 MDString *Name;
2124 if (Record.size() == 3)
2125 Name = getMDString(Record[2]);
2126 else if (Record.size() == 5)
2127 Name = getMDString(Record[3]);
2128 else
2129 return error(Message: "Invalid record");
2130
2131 IsDistinct = Record[0] & 1;
2132 bool ExportSymbols = Record[0] & 2;
2133 MetadataList.assignValue(
2134 GET_OR_DISTINCT(DINamespace,
2135 (Context, getMDOrNull(Record[1]), Name, ExportSymbols)),
2136 Idx: NextMetadataNo);
2137 NextMetadataNo++;
2138 break;
2139 }
2140 case bitc::METADATA_MACRO: {
2141 if (Record.size() != 5)
2142 return error(Message: "Invalid record");
2143
2144 IsDistinct = Record[0];
2145 MetadataList.assignValue(
2146 GET_OR_DISTINCT(DIMacro,
2147 (Context, Record[1], Record[2], getMDString(Record[3]),
2148 getMDString(Record[4]))),
2149 Idx: NextMetadataNo);
2150 NextMetadataNo++;
2151 break;
2152 }
2153 case bitc::METADATA_MACRO_FILE: {
2154 if (Record.size() != 5)
2155 return error(Message: "Invalid record");
2156
2157 IsDistinct = Record[0];
2158 MetadataList.assignValue(
2159 GET_OR_DISTINCT(DIMacroFile,
2160 (Context, Record[1], Record[2], getMDOrNull(Record[3]),
2161 getMDOrNull(Record[4]))),
2162 Idx: NextMetadataNo);
2163 NextMetadataNo++;
2164 break;
2165 }
2166 case bitc::METADATA_TEMPLATE_TYPE: {
2167 if (Record.size() < 3 || Record.size() > 4)
2168 return error(Message: "Invalid record");
2169
2170 IsDistinct = Record[0];
2171 MetadataList.assignValue(
2172 GET_OR_DISTINCT(DITemplateTypeParameter,
2173 (Context, getMDString(Record[1]),
2174 getDITypeRefOrNull(Record[2]),
2175 (Record.size() == 4) ? getMDOrNull(Record[3])
2176 : getMDOrNull(false))),
2177 Idx: NextMetadataNo);
2178 NextMetadataNo++;
2179 break;
2180 }
2181 case bitc::METADATA_TEMPLATE_VALUE: {
2182 if (Record.size() < 5 || Record.size() > 6)
2183 return error(Message: "Invalid record");
2184
2185 IsDistinct = Record[0];
2186
2187 MetadataList.assignValue(
2188 GET_OR_DISTINCT(
2189 DITemplateValueParameter,
2190 (Context, Record[1], getMDString(Record[2]),
2191 getDITypeRefOrNull(Record[3]),
2192 (Record.size() == 6) ? getMDOrNull(Record[4]) : getMDOrNull(false),
2193 (Record.size() == 6) ? getMDOrNull(Record[5])
2194 : getMDOrNull(Record[4]))),
2195 Idx: NextMetadataNo);
2196 NextMetadataNo++;
2197 break;
2198 }
2199 case bitc::METADATA_GLOBAL_VAR: {
2200 if (Record.size() < 11 || Record.size() > 13)
2201 return error(Message: "Invalid record");
2202
2203 IsDistinct = Record[0] & 1;
2204 unsigned Version = Record[0] >> 1;
2205
2206 if (Version == 2) {
2207 Metadata *Annotations = nullptr;
2208 if (Record.size() > 12)
2209 Annotations = getMDOrNull(Record[12]);
2210
2211 MetadataList.assignValue(
2212 GET_OR_DISTINCT(DIGlobalVariable,
2213 (Context, getMDOrNull(Record[1]),
2214 getMDString(Record[2]), getMDString(Record[3]),
2215 getMDOrNull(Record[4]), Record[5],
2216 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
2217 getMDOrNull(Record[9]), getMDOrNull(Record[10]),
2218 Record[11], Annotations)),
2219 Idx: NextMetadataNo);
2220
2221 NextMetadataNo++;
2222 } else if (Version == 1) {
2223 // No upgrade necessary. A null field will be introduced to indicate
2224 // that no parameter information is available.
2225 MetadataList.assignValue(
2226 GET_OR_DISTINCT(
2227 DIGlobalVariable,
2228 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2229 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2230 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
2231 getMDOrNull(Record[10]), nullptr, Record[11], nullptr)),
2232 Idx: NextMetadataNo);
2233
2234 NextMetadataNo++;
2235 } else if (Version == 0) {
2236 // Upgrade old metadata, which stored a global variable reference or a
2237 // ConstantInt here.
2238 NeedUpgradeToDIGlobalVariableExpression = true;
2239 Metadata *Expr = getMDOrNull(Record[9]);
2240 uint32_t AlignInBits = 0;
2241 if (Record.size() > 11) {
2242 if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
2243 return error(Message: "Alignment value is too large");
2244 AlignInBits = Record[11];
2245 }
2246 GlobalVariable *Attach = nullptr;
2247 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Val: Expr)) {
2248 if (auto *GV = dyn_cast<GlobalVariable>(Val: CMD->getValue())) {
2249 Attach = GV;
2250 Expr = nullptr;
2251 } else if (auto *CI = dyn_cast<ConstantInt>(Val: CMD->getValue())) {
2252 Expr = DIExpression::get(Context,
2253 Elements: {dwarf::DW_OP_constu, CI->getZExtValue(),
2254 dwarf::DW_OP_stack_value});
2255 } else {
2256 Expr = nullptr;
2257 }
2258 }
2259 DIGlobalVariable *DGV = GET_OR_DISTINCT(
2260 DIGlobalVariable,
2261 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2262 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2263 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
2264 getMDOrNull(Record[10]), nullptr, AlignInBits, nullptr));
2265
2266 DIGlobalVariableExpression *DGVE = nullptr;
2267 if (Attach || Expr)
2268 DGVE = DIGlobalVariableExpression::getDistinct(
2269 Context, Variable: DGV, Expression: Expr ? Expr : DIExpression::get(Context, Elements: {}));
2270 if (Attach)
2271 Attach->addDebugInfo(GV: DGVE);
2272
2273 auto *MDNode = Expr ? cast<Metadata>(Val: DGVE) : cast<Metadata>(Val: DGV);
2274 MetadataList.assignValue(MD: MDNode, Idx: NextMetadataNo);
2275 NextMetadataNo++;
2276 } else
2277 return error(Message: "Invalid record");
2278
2279 break;
2280 }
2281 case bitc::METADATA_ASSIGN_ID: {
2282 if (Record.size() != 1)
2283 return error(Message: "Invalid DIAssignID record.");
2284
2285 IsDistinct = Record[0] & 1;
2286 if (!IsDistinct)
2287 return error(Message: "Invalid DIAssignID record. Must be distinct");
2288
2289 MetadataList.assignValue(MD: DIAssignID::getDistinct(Context), Idx: NextMetadataNo);
2290 NextMetadataNo++;
2291 break;
2292 }
2293 case bitc::METADATA_LOCAL_VAR: {
2294 // 10th field is for the obseleted 'inlinedAt:' field.
2295 if (Record.size() < 8 || Record.size() > 10)
2296 return error(Message: "Invalid record");
2297
2298 IsDistinct = Record[0] & 1;
2299 bool HasAlignment = Record[0] & 2;
2300 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
2301 // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
2302 // this is newer version of record which doesn't have artificial tag.
2303 bool HasTag = !HasAlignment && Record.size() > 8;
2304 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
2305 uint32_t AlignInBits = 0;
2306 Metadata *Annotations = nullptr;
2307 if (HasAlignment) {
2308 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
2309 return error(Message: "Alignment value is too large");
2310 AlignInBits = Record[8];
2311 if (Record.size() > 9)
2312 Annotations = getMDOrNull(Record[9]);
2313 }
2314
2315 MetadataList.assignValue(
2316 GET_OR_DISTINCT(DILocalVariable,
2317 (Context, getMDOrNull(Record[1 + HasTag]),
2318 getMDString(Record[2 + HasTag]),
2319 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2320 getDITypeRefOrNull(Record[5 + HasTag]),
2321 Record[6 + HasTag], Flags, AlignInBits, Annotations)),
2322 Idx: NextMetadataNo);
2323 NextMetadataNo++;
2324 break;
2325 }
2326 case bitc::METADATA_LABEL: {
2327 if (Record.size() < 5 || Record.size() > 7)
2328 return error(Message: "Invalid record");
2329
2330 IsDistinct = Record[0] & 1;
2331 uint64_t Line = Record[4];
2332 uint64_t Column = Record.size() > 5 ? Record[5] : 0;
2333 bool IsArtificial = Record[0] & 2;
2334 std::optional<unsigned> CoroSuspendIdx;
2335 if (Record.size() > 6) {
2336 uint64_t RawSuspendIdx = Record[6];
2337 if (RawSuspendIdx != std::numeric_limits<uint64_t>::max()) {
2338 if (RawSuspendIdx > (uint64_t)std::numeric_limits<unsigned>::max())
2339 return error(Message: "CoroSuspendIdx value is too large");
2340 CoroSuspendIdx = RawSuspendIdx;
2341 }
2342 }
2343
2344 MetadataList.assignValue(
2345 GET_OR_DISTINCT(DILabel,
2346 (Context, getMDOrNull(Record[1]),
2347 getMDString(Record[2]), getMDOrNull(Record[3]), Line,
2348 Column, IsArtificial, CoroSuspendIdx)),
2349 Idx: NextMetadataNo);
2350 NextMetadataNo++;
2351 break;
2352 }
2353 case bitc::METADATA_EXPRESSION: {
2354 if (Record.size() < 1)
2355 return error(Message: "Invalid record");
2356
2357 IsDistinct = Record[0] & 1;
2358 uint64_t Version = Record[0] >> 1;
2359 auto Elts = MutableArrayRef<uint64_t>(Record).slice(N: 1);
2360
2361 SmallVector<uint64_t, 6> Buffer;
2362 if (Error Err = upgradeDIExpression(FromVersion: Version, Expr&: Elts, Buffer))
2363 return Err;
2364
2365 MetadataList.assignValue(GET_OR_DISTINCT(DIExpression, (Context, Elts)),
2366 Idx: NextMetadataNo);
2367 NextMetadataNo++;
2368 break;
2369 }
2370 case bitc::METADATA_GLOBAL_VAR_EXPR: {
2371 if (Record.size() != 3)
2372 return error(Message: "Invalid record");
2373
2374 IsDistinct = Record[0];
2375 Metadata *Expr = getMDOrNull(Record[2]);
2376 if (!Expr)
2377 Expr = DIExpression::get(Context, Elements: {});
2378 MetadataList.assignValue(
2379 GET_OR_DISTINCT(DIGlobalVariableExpression,
2380 (Context, getMDOrNull(Record[1]), Expr)),
2381 Idx: NextMetadataNo);
2382 NextMetadataNo++;
2383 break;
2384 }
2385 case bitc::METADATA_OBJC_PROPERTY: {
2386 if (Record.size() != 8)
2387 return error(Message: "Invalid record");
2388
2389 IsDistinct = Record[0];
2390 MetadataList.assignValue(
2391 GET_OR_DISTINCT(DIObjCProperty,
2392 (Context, getMDString(Record[1]),
2393 getMDOrNull(Record[2]), Record[3],
2394 /*GetterName=*/getMDString(Record[5]),
2395 /*SetterName=*/getMDString(Record[4]), Record[6],
2396 getDITypeRefOrNull(Record[7]))),
2397 Idx: NextMetadataNo);
2398 NextMetadataNo++;
2399 break;
2400 }
2401 case bitc::METADATA_IMPORTED_ENTITY: {
2402 if (Record.size() < 6 || Record.size() > 8)
2403 return error(Message: "Invalid DIImportedEntity record");
2404
2405 IsDistinct = Record[0];
2406 bool HasFile = (Record.size() >= 7);
2407 bool HasElements = (Record.size() >= 8);
2408 MetadataList.assignValue(
2409 GET_OR_DISTINCT(DIImportedEntity,
2410 (Context, Record[1], getMDOrNull(Record[2]),
2411 getDITypeRefOrNull(Record[3]),
2412 HasFile ? getMDOrNull(Record[6]) : nullptr,
2413 HasFile ? Record[4] : 0, getMDString(Record[5]),
2414 HasElements ? getMDOrNull(Record[7]) : nullptr)),
2415 Idx: NextMetadataNo);
2416 NextMetadataNo++;
2417 break;
2418 }
2419 case bitc::METADATA_STRING_OLD: {
2420 std::string String(Record.begin(), Record.end());
2421
2422 // Test for upgrading !llvm.loop.
2423 HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(Name: String);
2424 ++NumMDStringLoaded;
2425 Metadata *MD = MDString::get(Context, Str: String);
2426 MetadataList.assignValue(MD, Idx: NextMetadataNo);
2427 NextMetadataNo++;
2428 break;
2429 }
2430 case bitc::METADATA_STRINGS: {
2431 auto CreateNextMDString = [&](StringRef Str) {
2432 ++NumMDStringLoaded;
2433 MetadataList.assignValue(MD: MDString::get(Context, Str), Idx: NextMetadataNo);
2434 NextMetadataNo++;
2435 };
2436 if (Error Err = parseMetadataStrings(Record, Blob, CallBack: CreateNextMDString))
2437 return Err;
2438 break;
2439 }
2440 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
2441 if (Record.size() % 2 == 0)
2442 return error(Message: "Invalid record");
2443 unsigned ValueID = Record[0];
2444 if (ValueID >= ValueList.size())
2445 return error(Message: "Invalid record");
2446 if (auto *GO = dyn_cast<GlobalObject>(Val: ValueList[ValueID]))
2447 if (Error Err = parseGlobalObjectAttachment(
2448 GO&: *GO, Record: ArrayRef<uint64_t>(Record).slice(N: 1)))
2449 return Err;
2450 break;
2451 }
2452 case bitc::METADATA_KIND: {
2453 // Support older bitcode files that had METADATA_KIND records in a
2454 // block with METADATA_BLOCK_ID.
2455 if (Error Err = parseMetadataKindRecord(Record))
2456 return Err;
2457 break;
2458 }
2459 case bitc::METADATA_ARG_LIST: {
2460 SmallVector<ValueAsMetadata *, 4> Elts;
2461 Elts.reserve(N: Record.size());
2462 for (uint64_t Elt : Record) {
2463 Metadata *MD = getMD(Elt);
2464 if (isa<MDNode>(Val: MD) && cast<MDNode>(Val: MD)->isTemporary())
2465 return error(
2466 Message: "Invalid record: DIArgList should not contain forward refs");
2467 if (!isa<ValueAsMetadata>(Val: MD))
2468 return error(Message: "Invalid record");
2469 Elts.push_back(Elt: cast<ValueAsMetadata>(Val: MD));
2470 }
2471
2472 MetadataList.assignValue(MD: DIArgList::get(Context, Args: Elts), Idx: NextMetadataNo);
2473 NextMetadataNo++;
2474 break;
2475 }
2476 }
2477 return Error::success();
2478#undef GET_OR_DISTINCT
2479}
2480
2481Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
2482 ArrayRef<uint64_t> Record, StringRef Blob,
2483 function_ref<void(StringRef)> CallBack) {
2484 // All the MDStrings in the block are emitted together in a single
2485 // record. The strings are concatenated and stored in a blob along with
2486 // their sizes.
2487 if (Record.size() != 2)
2488 return error(Message: "Invalid record: metadata strings layout");
2489
2490 unsigned NumStrings = Record[0];
2491 unsigned StringsOffset = Record[1];
2492 if (!NumStrings)
2493 return error(Message: "Invalid record: metadata strings with no strings");
2494 if (StringsOffset > Blob.size())
2495 return error(Message: "Invalid record: metadata strings corrupt offset");
2496
2497 StringRef Lengths = Blob.slice(Start: 0, End: StringsOffset);
2498 SimpleBitstreamCursor R(Lengths);
2499
2500 StringRef Strings = Blob.drop_front(N: StringsOffset);
2501 do {
2502 if (R.AtEndOfStream())
2503 return error(Message: "Invalid record: metadata strings bad length");
2504
2505 uint32_t Size;
2506 if (Error E = R.ReadVBR(NumBits: 6).moveInto(Value&: Size))
2507 return E;
2508 if (Strings.size() < Size)
2509 return error(Message: "Invalid record: metadata strings truncated chars");
2510
2511 CallBack(Strings.slice(Start: 0, End: Size));
2512 Strings = Strings.drop_front(N: Size);
2513 } while (--NumStrings);
2514
2515 return Error::success();
2516}
2517
2518Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
2519 GlobalObject &GO, ArrayRef<uint64_t> Record) {
2520 assert(Record.size() % 2 == 0);
2521 for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
2522 auto K = MDKindMap.find(Val: Record[I]);
2523 if (K == MDKindMap.end())
2524 return error(Message: "Invalid ID");
2525 MDNode *MD =
2526 dyn_cast_or_null<MDNode>(Val: getMetadataFwdRefOrLoad(ID: Record[I + 1]));
2527 if (!MD)
2528 return error(Message: "Invalid metadata attachment: expect fwd ref to MDNode");
2529 GO.addMetadata(KindID: K->second, MD&: *MD);
2530 }
2531 return Error::success();
2532}
2533
2534/// Parse metadata attachments.
2535Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
2536 Function &F, ArrayRef<Instruction *> InstructionList) {
2537 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::METADATA_ATTACHMENT_ID))
2538 return Err;
2539
2540 SmallVector<uint64_t, 64> Record;
2541 PlaceholderQueue Placeholders;
2542
2543 while (true) {
2544 BitstreamEntry Entry;
2545 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Value&: Entry))
2546 return E;
2547
2548 switch (Entry.Kind) {
2549 case BitstreamEntry::SubBlock: // Handled for us already.
2550 case BitstreamEntry::Error:
2551 return error(Message: "Malformed block");
2552 case BitstreamEntry::EndBlock:
2553 LLVM_DEBUG(llvm::dbgs() << "\nAttachment metadata loading: ");
2554 resolveLoadedMetadata(Placeholders, DIUpgradeMode: DebugInfoUpgradeMode::None);
2555 return Error::success();
2556 case BitstreamEntry::Record:
2557 // The interesting case.
2558 break;
2559 }
2560
2561 // Read a metadata attachment record.
2562 Record.clear();
2563 ++NumMDRecordLoaded;
2564 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2565 if (!MaybeRecord)
2566 return MaybeRecord.takeError();
2567 switch (MaybeRecord.get()) {
2568 default: // Default behavior: ignore.
2569 break;
2570 case bitc::METADATA_ATTACHMENT: {
2571 unsigned RecordLength = Record.size();
2572 if (Record.empty())
2573 return error(Message: "Invalid record");
2574 if (RecordLength % 2 == 0) {
2575 // A function attachment.
2576 if (Error Err = parseGlobalObjectAttachment(GO&: F, Record))
2577 return Err;
2578 continue;
2579 }
2580
2581 // An instruction attachment.
2582 Instruction *Inst = InstructionList[Record[0]];
2583 for (unsigned i = 1; i != RecordLength; i = i + 2) {
2584 unsigned Kind = Record[i];
2585 DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Val: Kind);
2586 if (I == MDKindMap.end())
2587 return error(Message: "Invalid ID");
2588 if (I->second == LLVMContext::MD_tbaa && StripTBAA)
2589 continue;
2590
2591 auto Idx = Record[i + 1];
2592 if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
2593 !MetadataList.lookup(I: Idx)) {
2594 // Load the attachment if it is in the lazy-loadable range and hasn't
2595 // been loaded yet.
2596 lazyLoadOneMetadata(ID: Idx, Placeholders);
2597 LLVM_DEBUG(llvm::dbgs() << "\nLazy attachment metadata loading: ");
2598 resolveLoadedMetadata(Placeholders, DIUpgradeMode: DebugInfoUpgradeMode::None);
2599 }
2600
2601 Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
2602 if (isa<LocalAsMetadata>(Val: Node))
2603 // Drop the attachment. This used to be legal, but there's no
2604 // upgrade path.
2605 break;
2606 MDNode *MD = dyn_cast_or_null<MDNode>(Val: Node);
2607 if (!MD)
2608 return error(Message: "Invalid metadata attachment");
2609
2610 if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
2611 MD = upgradeInstructionLoopAttachment(N&: *MD);
2612
2613 if (I->second == LLVMContext::MD_tbaa) {
2614 assert(!MD->isTemporary() && "should load MDs before attachments");
2615 MD = UpgradeTBAANode(TBAANode&: *MD);
2616 }
2617 Inst->setMetadata(KindID: I->second, Node: MD);
2618 }
2619 break;
2620 }
2621 }
2622 }
2623}
2624
2625/// Parse a single METADATA_KIND record, inserting result in MDKindMap.
2626Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
2627 SmallVectorImpl<uint64_t> &Record) {
2628 if (Record.size() < 2)
2629 return error(Message: "Invalid record");
2630
2631 unsigned Kind = Record[0];
2632 SmallString<8> Name(Record.begin() + 1, Record.end());
2633
2634 unsigned NewKind = TheModule.getMDKindID(Name: Name.str());
2635 if (!MDKindMap.insert(KV: std::make_pair(x&: Kind, y&: NewKind)).second)
2636 return error(Message: "Conflicting METADATA_KIND records");
2637 return Error::success();
2638}
2639
2640/// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2641Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
2642 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::METADATA_KIND_BLOCK_ID))
2643 return Err;
2644
2645 SmallVector<uint64_t, 64> Record;
2646
2647 // Read all the records.
2648 while (true) {
2649 BitstreamEntry Entry;
2650 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Value&: Entry))
2651 return E;
2652
2653 switch (Entry.Kind) {
2654 case BitstreamEntry::SubBlock: // Handled for us already.
2655 case BitstreamEntry::Error:
2656 return error(Message: "Malformed block");
2657 case BitstreamEntry::EndBlock:
2658 return Error::success();
2659 case BitstreamEntry::Record:
2660 // The interesting case.
2661 break;
2662 }
2663
2664 // Read a record.
2665 Record.clear();
2666 ++NumMDRecordLoaded;
2667 Expected<unsigned> MaybeCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2668 if (!MaybeCode)
2669 return MaybeCode.takeError();
2670 switch (MaybeCode.get()) {
2671 default: // Default behavior: ignore.
2672 break;
2673 case bitc::METADATA_KIND: {
2674 if (Error Err = parseMetadataKindRecord(Record))
2675 return Err;
2676 break;
2677 }
2678 }
2679 }
2680}
2681
2682MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
2683 Pimpl = std::move(RHS.Pimpl);
2684 return *this;
2685}
2686MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
2687 : Pimpl(std::move(RHS.Pimpl)) {}
2688
2689MetadataLoader::~MetadataLoader() = default;
2690MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
2691 BitcodeReaderValueList &ValueList,
2692 bool IsImporting,
2693 MetadataLoaderCallbacks Callbacks)
2694 : Pimpl(std::make_unique<MetadataLoaderImpl>(
2695 args&: Stream, args&: TheModule, args&: ValueList, args: std::move(Callbacks), args&: IsImporting)) {}
2696
2697Error MetadataLoader::parseMetadata(bool ModuleLevel) {
2698 return Pimpl->parseMetadata(ModuleLevel);
2699}
2700
2701bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
2702
2703/// Return the given metadata, creating a replaceable forward reference if
2704/// necessary.
2705Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
2706 return Pimpl->getMetadataFwdRefOrLoad(ID: Idx);
2707}
2708
2709DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
2710 return Pimpl->lookupSubprogramForFunction(F);
2711}
2712
2713Error MetadataLoader::parseMetadataAttachment(
2714 Function &F, ArrayRef<Instruction *> InstructionList) {
2715 return Pimpl->parseMetadataAttachment(F, InstructionList);
2716}
2717
2718Error MetadataLoader::parseMetadataKinds() {
2719 return Pimpl->parseMetadataKinds();
2720}
2721
2722void MetadataLoader::setStripTBAA(bool StripTBAA) {
2723 return Pimpl->setStripTBAA(StripTBAA);
2724}
2725
2726bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
2727
2728unsigned MetadataLoader::size() const { return Pimpl->size(); }
2729void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
2730
2731void MetadataLoader::upgradeDebugIntrinsics(Function &F) {
2732 return Pimpl->upgradeDebugIntrinsics(F);
2733}
2734