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