1 //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MetadataLoader.h"
11 #include "ValueList.h"
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Bitcode/BitcodeReader.h"
27 #include "llvm/Bitcode/BitstreamReader.h"
28 #include "llvm/Bitcode/LLVMBitCodes.h"
29 #include "llvm/IR/Argument.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/AutoUpgrade.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/CallSite.h"
34 #include "llvm/IR/CallingConv.h"
35 #include "llvm/IR/Comdat.h"
36 #include "llvm/IR/Constant.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/DebugInfo.h"
39 #include "llvm/IR/DebugInfoMetadata.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/DiagnosticInfo.h"
43 #include "llvm/IR/DiagnosticPrinter.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GVMaterializer.h"
46 #include "llvm/IR/GlobalAlias.h"
47 #include "llvm/IR/GlobalIFunc.h"
48 #include "llvm/IR/GlobalIndirectSymbol.h"
49 #include "llvm/IR/GlobalObject.h"
50 #include "llvm/IR/GlobalValue.h"
51 #include "llvm/IR/GlobalVariable.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/InstrTypes.h"
54 #include "llvm/IR/Instruction.h"
55 #include "llvm/IR/Instructions.h"
56 #include "llvm/IR/Intrinsics.h"
57 #include "llvm/IR/IntrinsicInst.h"
58 #include "llvm/IR/LLVMContext.h"
59 #include "llvm/IR/Module.h"
60 #include "llvm/IR/ModuleSummaryIndex.h"
61 #include "llvm/IR/OperandTraits.h"
62 #include "llvm/IR/Operator.h"
63 #include "llvm/IR/TrackingMDRef.h"
64 #include "llvm/IR/Type.h"
65 #include "llvm/IR/ValueHandle.h"
66 #include "llvm/Support/AtomicOrdering.h"
67 #include "llvm/Support/Casting.h"
68 #include "llvm/Support/CommandLine.h"
69 #include "llvm/Support/Compiler.h"
70 #include "llvm/Support/Debug.h"
71 #include "llvm/Support/Error.h"
72 #include "llvm/Support/ErrorHandling.h"
73 #include "llvm/Support/ManagedStatic.h"
74 #include "llvm/Support/MemoryBuffer.h"
75 #include "llvm/Support/raw_ostream.h"
85 #include <system_error>
92 #define DEBUG_TYPE "bitcode-reader"
94 STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
95 STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
96 STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
98 /// Flag whether we need to import full type definitions for ThinLTO.
99 /// Currently needed for Darwin and LLDB.
100 static cl::opt<bool> ImportFullTypeDefinitions(
101 "import-full-type-definitions", cl::init(false), cl::Hidden,
102 cl::desc("Import full type definitions for ThinLTO."));
104 static cl::opt<bool> DisableLazyLoading(
105 "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
106 cl::desc("Force disable the lazy-loading on-demand of metadata when "
107 "loading bitcode for importing."));
111 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
113 class BitcodeReaderMetadataList {
114 /// Array of metadata references.
116 /// Don't use std::vector here. Some versions of libc++ copy (instead of
117 /// move) on resize, and TrackingMDRef is very expensive to copy.
118 SmallVector<TrackingMDRef, 1> MetadataPtrs;
120 /// The set of indices in MetadataPtrs above of forward references that were
122 SmallDenseSet<unsigned, 1> ForwardReference;
124 /// The set of indices in MetadataPtrs above of Metadata that need to be
126 SmallDenseSet<unsigned, 1> UnresolvedNodes;
128 /// Structures for resolving old type refs.
130 SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
131 SmallDenseMap<MDString *, DICompositeType *, 1> Final;
132 SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
133 SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
136 LLVMContext &Context;
139 BitcodeReaderMetadataList(LLVMContext &C) : Context(C) {}
141 // vector compatibility methods
142 unsigned size() const { return MetadataPtrs.size(); }
143 void resize(unsigned N) { MetadataPtrs.resize(N); }
144 void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
145 void clear() { MetadataPtrs.clear(); }
146 Metadata *back() const { return MetadataPtrs.back(); }
147 void pop_back() { MetadataPtrs.pop_back(); }
148 bool empty() const { return MetadataPtrs.empty(); }
150 Metadata *operator[](unsigned i) const {
151 assert(i < MetadataPtrs.size());
152 return MetadataPtrs[i];
155 Metadata *lookup(unsigned I) const {
156 if (I < MetadataPtrs.size())
157 return MetadataPtrs[I];
161 void shrinkTo(unsigned N) {
162 assert(N <= size() && "Invalid shrinkTo request!");
163 assert(ForwardReference.empty() && "Unexpected forward refs");
164 assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
165 MetadataPtrs.resize(N);
168 /// Return the given metadata, creating a replaceable forward reference if
170 Metadata *getMetadataFwdRef(unsigned Idx);
172 /// Return the the given metadata only if it is fully resolved.
174 /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
175 /// would give \c false.
176 Metadata *getMetadataIfResolved(unsigned Idx);
178 MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
179 void assignValue(Metadata *MD, unsigned Idx);
180 void tryToResolveCycles();
181 bool hasFwdRefs() const { return !ForwardReference.empty(); }
182 int getNextFwdRef() {
183 assert(hasFwdRefs());
184 return *ForwardReference.begin();
187 /// Upgrade a type that had an MDString reference.
188 void addTypeRef(MDString &UUID, DICompositeType &CT);
190 /// Upgrade a type that had an MDString reference.
191 Metadata *upgradeTypeRef(Metadata *MaybeUUID);
193 /// Upgrade a type ref array that may have MDString references.
194 Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
197 Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
200 void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
201 if (auto *MDN = dyn_cast<MDNode>(MD))
202 if (!MDN->isResolved())
203 UnresolvedNodes.insert(Idx);
213 TrackingMDRef &OldMD = MetadataPtrs[Idx];
219 // If there was a forward reference to this value, replace it.
220 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
221 PrevMD->replaceAllUsesWith(MD);
222 ForwardReference.erase(Idx);
225 Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
229 if (Metadata *MD = MetadataPtrs[Idx])
232 // Track forward refs to be resolved later.
233 ForwardReference.insert(Idx);
235 // Create and return a placeholder, which will later be RAUW'd.
236 ++NumMDNodeTemporary;
237 Metadata *MD = MDNode::getTemporary(Context, None).release();
238 MetadataPtrs[Idx].reset(MD);
242 Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
243 Metadata *MD = lookup(Idx);
244 if (auto *N = dyn_cast_or_null<MDNode>(MD))
245 if (!N->isResolved())
250 MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
251 return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
254 void BitcodeReaderMetadataList::tryToResolveCycles() {
255 if (!ForwardReference.empty())
256 // Still forward references... can't resolve cycles.
259 // Give up on finding a full definition for any forward decls that remain.
260 for (const auto &Ref : OldTypeRefs.FwdDecls)
261 OldTypeRefs.Final.insert(Ref);
262 OldTypeRefs.FwdDecls.clear();
264 // Upgrade from old type ref arrays. In strange cases, this could add to
265 // OldTypeRefs.Unknown.
266 for (const auto &Array : OldTypeRefs.Arrays)
267 Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
268 OldTypeRefs.Arrays.clear();
270 // Replace old string-based type refs with the resolved node, if possible.
271 // If we haven't seen the node, leave it to the verifier to complain about
272 // the invalid string reference.
273 for (const auto &Ref : OldTypeRefs.Unknown) {
274 if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
275 Ref.second->replaceAllUsesWith(CT);
277 Ref.second->replaceAllUsesWith(Ref.first);
279 OldTypeRefs.Unknown.clear();
281 if (UnresolvedNodes.empty())
285 // Resolve any cycles.
286 for (unsigned I : UnresolvedNodes) {
287 auto &MD = MetadataPtrs[I];
288 auto *N = dyn_cast_or_null<MDNode>(MD);
292 assert(!N->isTemporary() && "Unexpected forward reference");
296 // Make sure we return early again until there's another unresolved ref.
297 UnresolvedNodes.clear();
300 void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
301 DICompositeType &CT) {
302 assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
303 if (CT.isForwardDecl())
304 OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
306 OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
309 Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
310 auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
311 if (LLVM_LIKELY(!UUID))
314 if (auto *CT = OldTypeRefs.Final.lookup(UUID))
317 auto &Ref = OldTypeRefs.Unknown[UUID];
319 Ref = MDNode::getTemporary(Context, None);
323 Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
324 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
325 if (!Tuple || Tuple->isDistinct())
328 // Look through the array immediately if possible.
329 if (!Tuple->isTemporary())
330 return resolveTypeRefArray(Tuple);
332 // Create and return a placeholder to use for now. Eventually
333 // resolveTypeRefArrays() will be resolve this forward reference.
334 OldTypeRefs.Arrays.emplace_back(
335 std::piecewise_construct, std::forward_as_tuple(Tuple),
336 std::forward_as_tuple(MDTuple::getTemporary(Context, None)));
337 return OldTypeRefs.Arrays.back().second.get();
340 Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
341 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
342 if (!Tuple || Tuple->isDistinct())
345 // Look through the DITypeRefArray, upgrading each DITypeRef.
346 SmallVector<Metadata *, 32> Ops;
347 Ops.reserve(Tuple->getNumOperands());
348 for (Metadata *MD : Tuple->operands())
349 Ops.push_back(upgradeTypeRef(MD));
351 return MDTuple::get(Context, Ops);
356 class PlaceholderQueue {
357 // Placeholders would thrash around when moved, so store in a std::deque
358 // instead of some sort of vector.
359 std::deque<DistinctMDOperandPlaceholder> PHs;
362 ~PlaceholderQueue() {
363 assert(empty() && "PlaceholderQueue hasn't been flushed before being destroyed");
365 bool empty() { return PHs.empty(); }
366 DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
367 void flush(BitcodeReaderMetadataList &MetadataList);
369 /// Return the list of temporaries nodes in the queue, these need to be
370 /// loaded before we can flush the queue.
371 void getTemporaries(BitcodeReaderMetadataList &MetadataList,
372 DenseSet<unsigned> &Temporaries) {
373 for (auto &PH : PHs) {
374 auto ID = PH.getID();
375 auto *MD = MetadataList.lookup(ID);
377 Temporaries.insert(ID);
380 auto *N = dyn_cast_or_null<MDNode>(MD);
381 if (N && N->isTemporary())
382 Temporaries.insert(ID);
387 } // end anonymous namespace
389 DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
390 PHs.emplace_back(ID);
394 void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
395 while (!PHs.empty()) {
396 auto *MD = MetadataList.lookup(PHs.front().getID());
397 assert(MD && "Flushing placeholder on unassigned MD");
399 if (auto *MDN = dyn_cast<MDNode>(MD))
400 assert(MDN->isResolved() &&
401 "Flushing Placeholder while cycles aren't resolved");
403 PHs.front().replaceUseWith(MD);
408 } // anonynous namespace
410 class MetadataLoader::MetadataLoaderImpl {
411 BitcodeReaderMetadataList MetadataList;
412 BitcodeReaderValueList &ValueList;
413 BitstreamCursor &Stream;
414 LLVMContext &Context;
416 std::function<Type *(unsigned)> getTypeByID;
418 /// Cursor associated with the lazy-loading of Metadata. This is the easy way
419 /// to keep around the right "context" (Abbrev list) to be able to jump in
420 /// the middle of the metadata block and load any record.
421 BitstreamCursor IndexCursor;
423 /// Index that keeps track of MDString values.
424 std::vector<StringRef> MDStringRef;
426 /// On-demand loading of a single MDString. Requires the index above to be
428 MDString *lazyLoadOneMDString(unsigned Idx);
430 /// Index that keeps track of where to find a metadata record in the stream.
431 std::vector<uint64_t> GlobalMetadataBitPosIndex;
433 /// Populate the index above to enable lazily loading of metadata, and load
434 /// the named metadata as well as the transitively referenced global
436 Expected<bool> lazyLoadModuleMetadataBlock();
438 /// On-demand loading of a single metadata. Requires the index above to be
440 void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
442 // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
443 // point from SP to CU after a block is completly parsed.
444 std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
446 /// Functions that need to be matched with subprograms when upgrading old
448 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
450 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
451 DenseMap<unsigned, unsigned> MDKindMap;
453 bool StripTBAA = false;
454 bool HasSeenOldLoopTags = false;
455 bool NeedUpgradeToDIGlobalVariableExpression = false;
456 bool NeedDeclareExpressionUpgrade = false;
458 /// True if metadata is being parsed for a module being ThinLTO imported.
459 bool IsImporting = false;
461 Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
462 PlaceholderQueue &Placeholders, StringRef Blob,
463 unsigned &NextMetadataNo);
464 Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
465 function_ref<void(StringRef)> CallBack);
466 Error parseGlobalObjectAttachment(GlobalObject &GO,
467 ArrayRef<uint64_t> Record);
468 Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
470 void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
472 /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
473 void upgradeCUSubprograms() {
474 for (auto CU_SP : CUSubprograms)
475 if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
476 for (auto &Op : SPs->operands())
477 if (auto *SP = dyn_cast_or_null<DISubprogram>(Op))
478 SP->replaceUnit(CU_SP.first);
479 CUSubprograms.clear();
482 /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions.
483 void upgradeCUVariables() {
484 if (!NeedUpgradeToDIGlobalVariableExpression)
487 // Upgrade list of variables attached to the CUs.
488 if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu"))
489 for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
490 auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I));
491 if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables()))
492 for (unsigned I = 0; I < GVs->getNumOperands(); I++)
494 dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) {
496 DIGlobalVariableExpression::getDistinct(Context, GV, nullptr);
497 GVs->replaceOperandWith(I, DGVE);
501 // Upgrade variables attached to globals.
502 for (auto &GV : TheModule.globals()) {
503 SmallVector<MDNode *, 1> MDs;
504 GV.getMetadata(LLVMContext::MD_dbg, MDs);
505 GV.eraseMetadata(LLVMContext::MD_dbg);
507 if (auto *DGV = dyn_cast_or_null<DIGlobalVariable>(MD)) {
509 DIGlobalVariableExpression::getDistinct(Context, DGV, nullptr);
510 GV.addMetadata(LLVMContext::MD_dbg, *DGVE);
512 GV.addMetadata(LLVMContext::MD_dbg, *MD);
516 /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that
517 /// describes a function argument.
518 void upgradeDeclareExpressions(Function &F) {
519 if (!NeedDeclareExpressionUpgrade)
524 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
525 if (auto *DIExpr = DDI->getExpression())
526 if (DIExpr->startsWithDeref() &&
527 dyn_cast_or_null<Argument>(DDI->getAddress())) {
528 SmallVector<uint64_t, 8> Ops;
529 Ops.append(std::next(DIExpr->elements_begin()),
530 DIExpr->elements_end());
531 auto *E = DIExpression::get(Context, Ops);
532 DDI->setOperand(2, MetadataAsValue::get(Context, E));
536 void upgradeDebugInfo() {
537 upgradeCUSubprograms();
538 upgradeCUVariables();
542 MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
543 BitcodeReaderValueList &ValueList,
544 std::function<Type *(unsigned)> getTypeByID,
546 : MetadataList(TheModule.getContext()), ValueList(ValueList),
547 Stream(Stream), Context(TheModule.getContext()), TheModule(TheModule),
548 getTypeByID(std::move(getTypeByID)), IsImporting(IsImporting) {}
550 Error parseMetadata(bool ModuleLevel);
552 bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
554 Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
555 if (ID < MDStringRef.size())
556 return lazyLoadOneMDString(ID);
557 if (auto *MD = MetadataList.lookup(ID))
559 // If lazy-loading is enabled, we try recursively to load the operand
560 // instead of creating a temporary.
561 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
562 PlaceholderQueue Placeholders;
563 lazyLoadOneMetadata(ID, Placeholders);
564 resolveForwardRefsAndPlaceholders(Placeholders);
565 return MetadataList.lookup(ID);
567 return MetadataList.getMetadataFwdRef(ID);
570 MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
571 return MetadataList.getMDNodeFwdRefOrNull(Idx);
574 DISubprogram *lookupSubprogramForFunction(Function *F) {
575 return FunctionsWithSPs.lookup(F);
578 bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; }
580 Error parseMetadataAttachment(
581 Function &F, const SmallVectorImpl<Instruction *> &InstructionList);
583 Error parseMetadataKinds();
585 void setStripTBAA(bool Value) { StripTBAA = Value; }
586 bool isStrippingTBAA() { return StripTBAA; }
588 unsigned size() const { return MetadataList.size(); }
589 void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
590 void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); }
593 static Error error(const Twine &Message) {
594 return make_error<StringError>(
595 Message, make_error_code(BitcodeError::CorruptedBitcode));
599 MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
600 IndexCursor = Stream;
601 SmallVector<uint64_t, 64> Record;
602 // Get the abbrevs, and preload record positions to make them lazy-loadable.
604 BitstreamEntry Entry = IndexCursor.advanceSkippingSubblocks(
605 BitstreamCursor::AF_DontPopBlockAtEnd);
606 switch (Entry.Kind) {
607 case BitstreamEntry::SubBlock: // Handled for us already.
608 case BitstreamEntry::Error:
609 return error("Malformed block");
610 case BitstreamEntry::EndBlock: {
613 case BitstreamEntry::Record: {
614 // The interesting case.
616 uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
617 auto Code = IndexCursor.skipRecord(Entry.ID);
619 case bitc::METADATA_STRINGS: {
620 // Rewind and parse the strings.
621 IndexCursor.JumpToBit(CurrentPos);
624 IndexCursor.readRecord(Entry.ID, Record, &Blob);
625 unsigned NumStrings = Record[0];
626 MDStringRef.reserve(NumStrings);
627 auto IndexNextMDString = [&](StringRef Str) {
628 MDStringRef.push_back(Str);
630 if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
631 return std::move(Err);
634 case bitc::METADATA_INDEX_OFFSET: {
635 // This is the offset to the index, when we see this we skip all the
636 // records and load only an index to these.
637 IndexCursor.JumpToBit(CurrentPos);
639 IndexCursor.readRecord(Entry.ID, Record);
640 if (Record.size() != 2)
641 return error("Invalid record");
642 auto Offset = Record[0] + (Record[1] << 32);
643 auto BeginPos = IndexCursor.GetCurrentBitNo();
644 IndexCursor.JumpToBit(BeginPos + Offset);
645 Entry = IndexCursor.advanceSkippingSubblocks(
646 BitstreamCursor::AF_DontPopBlockAtEnd);
647 assert(Entry.Kind == BitstreamEntry::Record &&
648 "Corrupted bitcode: Expected `Record` when trying to find the "
651 auto Code = IndexCursor.readRecord(Entry.ID, Record);
653 assert(Code == bitc::METADATA_INDEX && "Corrupted bitcode: Expected "
654 "`METADATA_INDEX` when trying "
655 "to find the Metadata index");
658 auto CurrentValue = BeginPos;
659 GlobalMetadataBitPosIndex.reserve(Record.size());
660 for (auto &Elt : Record) {
662 GlobalMetadataBitPosIndex.push_back(CurrentValue);
666 case bitc::METADATA_INDEX:
667 // We don't expect to get there, the Index is loaded when we encounter
669 return error("Corrupted Metadata block");
670 case bitc::METADATA_NAME: {
671 // Named metadata need to be materialized now and aren't deferred.
672 IndexCursor.JumpToBit(CurrentPos);
674 unsigned Code = IndexCursor.readRecord(Entry.ID, Record);
675 assert(Code == bitc::METADATA_NAME);
677 // Read name of the named metadata.
678 SmallString<8> Name(Record.begin(), Record.end());
679 Code = IndexCursor.ReadCode();
681 // Named Metadata comes in two parts, we expect the name to be followed
684 unsigned NextBitCode = IndexCursor.readRecord(Code, Record);
685 assert(NextBitCode == bitc::METADATA_NAMED_NODE);
688 // Read named metadata elements.
689 unsigned Size = Record.size();
690 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
691 for (unsigned i = 0; i != Size; ++i) {
692 // FIXME: We could use a placeholder here, however NamedMDNode are
693 // taking MDNode as operand and not using the Metadata infrastructure.
694 // It is acknowledged by 'TODO: Inherit from Metadata' in the
695 // NamedMDNode class definition.
696 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
697 assert(MD && "Invalid record");
702 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
703 // FIXME: we need to do this early because we don't materialize global
705 IndexCursor.JumpToBit(CurrentPos);
707 IndexCursor.readRecord(Entry.ID, Record);
708 if (Record.size() % 2 == 0)
709 return error("Invalid record");
710 unsigned ValueID = Record[0];
711 if (ValueID >= ValueList.size())
712 return error("Invalid record");
713 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
714 if (Error Err = parseGlobalObjectAttachment(
715 *GO, ArrayRef<uint64_t>(Record).slice(1)))
716 return std::move(Err);
719 case bitc::METADATA_KIND:
720 case bitc::METADATA_STRING_OLD:
721 case bitc::METADATA_OLD_FN_NODE:
722 case bitc::METADATA_OLD_NODE:
723 case bitc::METADATA_VALUE:
724 case bitc::METADATA_DISTINCT_NODE:
725 case bitc::METADATA_NODE:
726 case bitc::METADATA_LOCATION:
727 case bitc::METADATA_GENERIC_DEBUG:
728 case bitc::METADATA_SUBRANGE:
729 case bitc::METADATA_ENUMERATOR:
730 case bitc::METADATA_BASIC_TYPE:
731 case bitc::METADATA_DERIVED_TYPE:
732 case bitc::METADATA_COMPOSITE_TYPE:
733 case bitc::METADATA_SUBROUTINE_TYPE:
734 case bitc::METADATA_MODULE:
735 case bitc::METADATA_FILE:
736 case bitc::METADATA_COMPILE_UNIT:
737 case bitc::METADATA_SUBPROGRAM:
738 case bitc::METADATA_LEXICAL_BLOCK:
739 case bitc::METADATA_LEXICAL_BLOCK_FILE:
740 case bitc::METADATA_NAMESPACE:
741 case bitc::METADATA_MACRO:
742 case bitc::METADATA_MACRO_FILE:
743 case bitc::METADATA_TEMPLATE_TYPE:
744 case bitc::METADATA_TEMPLATE_VALUE:
745 case bitc::METADATA_GLOBAL_VAR:
746 case bitc::METADATA_LOCAL_VAR:
747 case bitc::METADATA_EXPRESSION:
748 case bitc::METADATA_OBJC_PROPERTY:
749 case bitc::METADATA_IMPORTED_ENTITY:
750 case bitc::METADATA_GLOBAL_VAR_EXPR:
751 // We don't expect to see any of these, if we see one, give up on
752 // lazy-loading and fallback.
754 GlobalMetadataBitPosIndex.clear();
763 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
764 /// module level metadata.
765 Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
766 if (!ModuleLevel && MetadataList.hasFwdRefs())
767 return error("Invalid metadata: fwd refs into function blocks");
769 // Record the entry position so that we can jump back here and efficiently
770 // skip the whole block in case we lazy-load.
771 auto EntryPos = Stream.GetCurrentBitNo();
773 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
774 return error("Invalid record");
776 SmallVector<uint64_t, 64> Record;
777 PlaceholderQueue Placeholders;
779 // We lazy-load module-level metadata: we build an index for each record, and
780 // then load individual record as needed, starting with the named metadata.
781 if (ModuleLevel && IsImporting && MetadataList.empty() &&
782 !DisableLazyLoading) {
783 auto SuccessOrErr = lazyLoadModuleMetadataBlock();
785 return SuccessOrErr.takeError();
786 if (SuccessOrErr.get()) {
787 // An index was successfully created and we will be able to load metadata
789 MetadataList.resize(MDStringRef.size() +
790 GlobalMetadataBitPosIndex.size());
792 // Reading the named metadata created forward references and/or
793 // placeholders, that we flush here.
794 resolveForwardRefsAndPlaceholders(Placeholders);
796 // Return at the beginning of the block, since it is easy to skip it
797 // entirely from there.
798 Stream.ReadBlockEnd(); // Pop the abbrev block context.
799 Stream.JumpToBit(EntryPos);
800 if (Stream.SkipBlock())
801 return error("Invalid record");
802 return Error::success();
804 // Couldn't load an index, fallback to loading all the block "old-style".
807 unsigned NextMetadataNo = MetadataList.size();
809 // Read all the records.
811 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
813 switch (Entry.Kind) {
814 case BitstreamEntry::SubBlock: // Handled for us already.
815 case BitstreamEntry::Error:
816 return error("Malformed block");
817 case BitstreamEntry::EndBlock:
818 resolveForwardRefsAndPlaceholders(Placeholders);
820 return Error::success();
821 case BitstreamEntry::Record:
822 // The interesting case.
830 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
832 parseOneMetadata(Record, Code, Placeholders, Blob, NextMetadataNo))
837 MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
839 if (Metadata *MD = MetadataList.lookup(ID))
840 return cast<MDString>(MD);
841 auto MDS = MDString::get(Context, MDStringRef[ID]);
842 MetadataList.assignValue(MDS, ID);
846 void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
847 unsigned ID, PlaceholderQueue &Placeholders) {
848 assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
849 assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
850 // Lookup first if the metadata hasn't already been loaded.
851 if (auto *MD = MetadataList.lookup(ID)) {
852 auto *N = dyn_cast_or_null<MDNode>(MD);
853 if (!N->isTemporary())
856 SmallVector<uint64_t, 64> Record;
858 IndexCursor.JumpToBit(GlobalMetadataBitPosIndex[ID - MDStringRef.size()]);
859 auto Entry = IndexCursor.advanceSkippingSubblocks();
861 unsigned Code = IndexCursor.readRecord(Entry.ID, Record, &Blob);
862 if (Error Err = parseOneMetadata(Record, Code, Placeholders, Blob, ID))
863 report_fatal_error("Can't lazyload MD");
866 /// Ensure that all forward-references and placeholders are resolved.
867 /// Iteratively lazy-loading metadata on-demand if needed.
868 void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
869 PlaceholderQueue &Placeholders) {
870 DenseSet<unsigned> Temporaries;
872 // Populate Temporaries with the placeholders that haven't been loaded yet.
873 Placeholders.getTemporaries(MetadataList, Temporaries);
875 // If we don't have any temporary, or FwdReference, we're done!
876 if (Temporaries.empty() && !MetadataList.hasFwdRefs())
879 // First, load all the temporaries. This can add new placeholders or
880 // forward references.
881 for (auto ID : Temporaries)
882 lazyLoadOneMetadata(ID, Placeholders);
885 // Second, load the forward-references. This can also add new placeholders
886 // or forward references.
887 while (MetadataList.hasFwdRefs())
888 lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
890 // At this point we don't have any forward reference remaining, or temporary
891 // that haven't been loaded. We can safely drop RAUW support and mark cycles
893 MetadataList.tryToResolveCycles();
895 // Finally, everything is in place, we can replace the placeholders operands
896 // with the final node they refer to.
897 Placeholders.flush(MetadataList);
900 Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
901 SmallVectorImpl<uint64_t> &Record, unsigned Code,
902 PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
904 bool IsDistinct = false;
905 auto getMD = [&](unsigned ID) -> Metadata * {
906 if (ID < MDStringRef.size())
907 return lazyLoadOneMDString(ID);
909 if (auto *MD = MetadataList.lookup(ID))
911 // If lazy-loading is enabled, we try recursively to load the operand
912 // instead of creating a temporary.
913 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
914 // Create a temporary for the node that is referencing the operand we
915 // will lazy-load. It is needed before recursing in case there are
917 MetadataList.getMetadataFwdRef(NextMetadataNo);
918 lazyLoadOneMetadata(ID, Placeholders);
919 return MetadataList.lookup(ID);
921 // Return a temporary.
922 return MetadataList.getMetadataFwdRef(ID);
924 if (auto *MD = MetadataList.getMetadataIfResolved(ID))
926 return &Placeholders.getPlaceholderOp(ID);
928 auto getMDOrNull = [&](unsigned ID) -> Metadata * {
930 return getMD(ID - 1);
933 auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
935 return MetadataList.getMetadataFwdRef(ID - 1);
938 auto getMDString = [&](unsigned ID) -> MDString * {
939 // This requires that the ID is not really a forward reference. In
940 // particular, the MDString must already have been resolved.
941 auto MDS = getMDOrNull(ID);
942 return cast_or_null<MDString>(MDS);
945 // Support for old type refs.
946 auto getDITypeRefOrNull = [&](unsigned ID) {
947 return MetadataList.upgradeTypeRef(getMDOrNull(ID));
950 #define GET_OR_DISTINCT(CLASS, ARGS) \
951 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
954 default: // Default behavior: ignore.
956 case bitc::METADATA_NAME: {
957 // Read name of the named metadata.
958 SmallString<8> Name(Record.begin(), Record.end());
960 Code = Stream.ReadCode();
963 unsigned NextBitCode = Stream.readRecord(Code, Record);
964 if (NextBitCode != bitc::METADATA_NAMED_NODE)
965 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
967 // Read named metadata elements.
968 unsigned Size = Record.size();
969 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
970 for (unsigned i = 0; i != Size; ++i) {
971 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
973 return error("Invalid record");
978 case bitc::METADATA_OLD_FN_NODE: {
979 // FIXME: Remove in 4.0.
980 // This is a LocalAsMetadata record, the only type of function-local
982 if (Record.size() % 2 == 1)
983 return error("Invalid record");
985 // If this isn't a LocalAsMetadata record, we're dropping it. This used
986 // to be legal, but there's no upgrade path.
987 auto dropRecord = [&] {
988 MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo);
991 if (Record.size() != 2) {
996 Type *Ty = getTypeByID(Record[0]);
997 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
1002 MetadataList.assignValue(
1003 LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1008 case bitc::METADATA_OLD_NODE: {
1009 // FIXME: Remove in 4.0.
1010 if (Record.size() % 2 == 1)
1011 return error("Invalid record");
1013 unsigned Size = Record.size();
1014 SmallVector<Metadata *, 8> Elts;
1015 for (unsigned i = 0; i != Size; i += 2) {
1016 Type *Ty = getTypeByID(Record[i]);
1018 return error("Invalid record");
1019 if (Ty->isMetadataTy())
1020 Elts.push_back(getMD(Record[i + 1]));
1021 else if (!Ty->isVoidTy()) {
1023 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
1024 assert(isa<ConstantAsMetadata>(MD) &&
1025 "Expected non-function-local metadata");
1028 Elts.push_back(nullptr);
1030 MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
1034 case bitc::METADATA_VALUE: {
1035 if (Record.size() != 2)
1036 return error("Invalid record");
1038 Type *Ty = getTypeByID(Record[0]);
1039 if (Ty->isMetadataTy() || Ty->isVoidTy())
1040 return error("Invalid record");
1042 MetadataList.assignValue(
1043 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1048 case bitc::METADATA_DISTINCT_NODE:
1051 case bitc::METADATA_NODE: {
1052 SmallVector<Metadata *, 8> Elts;
1053 Elts.reserve(Record.size());
1054 for (unsigned ID : Record)
1055 Elts.push_back(getMDOrNull(ID));
1056 MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
1057 : MDNode::get(Context, Elts),
1062 case bitc::METADATA_LOCATION: {
1063 if (Record.size() != 5)
1064 return error("Invalid record");
1066 IsDistinct = Record[0];
1067 unsigned Line = Record[1];
1068 unsigned Column = Record[2];
1069 Metadata *Scope = getMD(Record[3]);
1070 Metadata *InlinedAt = getMDOrNull(Record[4]);
1071 MetadataList.assignValue(
1072 GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt)),
1077 case bitc::METADATA_GENERIC_DEBUG: {
1078 if (Record.size() < 4)
1079 return error("Invalid record");
1081 IsDistinct = Record[0];
1082 unsigned Tag = Record[1];
1083 unsigned Version = Record[2];
1085 if (Tag >= 1u << 16 || Version != 0)
1086 return error("Invalid record");
1088 auto *Header = getMDString(Record[3]);
1089 SmallVector<Metadata *, 8> DwarfOps;
1090 for (unsigned I = 4, E = Record.size(); I != E; ++I)
1091 DwarfOps.push_back(getMDOrNull(Record[I]));
1092 MetadataList.assignValue(
1093 GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1098 case bitc::METADATA_SUBRANGE: {
1099 if (Record.size() != 3)
1100 return error("Invalid record");
1102 IsDistinct = Record[0];
1103 MetadataList.assignValue(
1104 GET_OR_DISTINCT(DISubrange,
1105 (Context, Record[1], unrotateSign(Record[2]))),
1110 case bitc::METADATA_ENUMERATOR: {
1111 if (Record.size() != 3)
1112 return error("Invalid record");
1114 IsDistinct = Record[0];
1115 MetadataList.assignValue(
1116 GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
1117 getMDString(Record[2]))),
1122 case bitc::METADATA_BASIC_TYPE: {
1123 if (Record.size() != 6)
1124 return error("Invalid record");
1126 IsDistinct = Record[0];
1127 MetadataList.assignValue(
1128 GET_OR_DISTINCT(DIBasicType,
1129 (Context, Record[1], getMDString(Record[2]), Record[3],
1130 Record[4], Record[5])),
1135 case bitc::METADATA_DERIVED_TYPE: {
1136 if (Record.size() < 12 || Record.size() > 13)
1137 return error("Invalid record");
1139 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1140 // that there is no DWARF address space associated with DIDerivedType.
1141 Optional<unsigned> DWARFAddressSpace;
1142 if (Record.size() > 12 && Record[12])
1143 DWARFAddressSpace = Record[12] - 1;
1145 IsDistinct = Record[0];
1146 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1147 MetadataList.assignValue(
1148 GET_OR_DISTINCT(DIDerivedType,
1149 (Context, Record[1], getMDString(Record[2]),
1150 getMDOrNull(Record[3]), Record[4],
1151 getDITypeRefOrNull(Record[5]),
1152 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1153 Record[9], DWARFAddressSpace, Flags,
1154 getDITypeRefOrNull(Record[11]))),
1159 case bitc::METADATA_COMPOSITE_TYPE: {
1160 if (Record.size() != 16)
1161 return error("Invalid record");
1163 // If we have a UUID and this is not a forward declaration, lookup the
1165 IsDistinct = Record[0] & 0x1;
1166 bool IsNotUsedInTypeRef = Record[0] >= 2;
1167 unsigned Tag = Record[1];
1168 MDString *Name = getMDString(Record[2]);
1169 Metadata *File = getMDOrNull(Record[3]);
1170 unsigned Line = Record[4];
1171 Metadata *Scope = getDITypeRefOrNull(Record[5]);
1172 Metadata *BaseType = nullptr;
1173 uint64_t SizeInBits = Record[7];
1174 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1175 return error("Alignment value is too large");
1176 uint32_t AlignInBits = Record[8];
1177 uint64_t OffsetInBits = 0;
1178 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1179 Metadata *Elements = nullptr;
1180 unsigned RuntimeLang = Record[12];
1181 Metadata *VTableHolder = nullptr;
1182 Metadata *TemplateParams = nullptr;
1183 auto *Identifier = getMDString(Record[15]);
1184 // If this module is being parsed so that it can be ThinLTO imported
1185 // into another module, composite types only need to be imported
1186 // as type declarations (unless full type definitions requested).
1187 // Create type declarations up front to save memory. Also, buildODRType
1188 // handles the case where this is type ODRed with a definition needed
1189 // by the importing module, in which case the existing definition is
1191 if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1192 (Tag == dwarf::DW_TAG_enumeration_type ||
1193 Tag == dwarf::DW_TAG_class_type ||
1194 Tag == dwarf::DW_TAG_structure_type ||
1195 Tag == dwarf::DW_TAG_union_type)) {
1196 Flags = Flags | DINode::FlagFwdDecl;
1198 BaseType = getDITypeRefOrNull(Record[6]);
1199 OffsetInBits = Record[9];
1200 Elements = getMDOrNull(Record[11]);
1201 VTableHolder = getDITypeRefOrNull(Record[13]);
1202 TemplateParams = getMDOrNull(Record[14]);
1204 DICompositeType *CT = nullptr;
1206 CT = DICompositeType::buildODRType(
1207 Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1208 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1209 VTableHolder, TemplateParams);
1211 // Create a node if we didn't get a lazy ODR type.
1213 CT = GET_OR_DISTINCT(DICompositeType,
1214 (Context, Tag, Name, File, Line, Scope, BaseType,
1215 SizeInBits, AlignInBits, OffsetInBits, Flags,
1216 Elements, RuntimeLang, VTableHolder, TemplateParams,
1218 if (!IsNotUsedInTypeRef && Identifier)
1219 MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1221 MetadataList.assignValue(CT, NextMetadataNo);
1225 case bitc::METADATA_SUBROUTINE_TYPE: {
1226 if (Record.size() < 3 || Record.size() > 4)
1227 return error("Invalid record");
1228 bool IsOldTypeRefArray = Record[0] < 2;
1229 unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1231 IsDistinct = Record[0] & 0x1;
1232 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1233 Metadata *Types = getMDOrNull(Record[2]);
1234 if (LLVM_UNLIKELY(IsOldTypeRefArray))
1235 Types = MetadataList.upgradeTypeRefArray(Types);
1237 MetadataList.assignValue(
1238 GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1244 case bitc::METADATA_MODULE: {
1245 if (Record.size() != 6)
1246 return error("Invalid record");
1248 IsDistinct = Record[0];
1249 MetadataList.assignValue(
1250 GET_OR_DISTINCT(DIModule,
1251 (Context, getMDOrNull(Record[1]),
1252 getMDString(Record[2]), getMDString(Record[3]),
1253 getMDString(Record[4]), getMDString(Record[5]))),
1259 case bitc::METADATA_FILE: {
1260 if (Record.size() != 3 && Record.size() != 5)
1261 return error("Invalid record");
1263 IsDistinct = Record[0];
1264 MetadataList.assignValue(
1267 (Context, getMDString(Record[1]), getMDString(Record[2]),
1268 Record.size() == 3 ? DIFile::CSK_None
1269 : static_cast<DIFile::ChecksumKind>(Record[3]),
1270 Record.size() == 3 ? nullptr : getMDString(Record[4]))),
1275 case bitc::METADATA_COMPILE_UNIT: {
1276 if (Record.size() < 14 || Record.size() > 18)
1277 return error("Invalid record");
1279 // Ignore Record[0], which indicates whether this compile unit is
1280 // distinct. It's always distinct.
1282 auto *CU = DICompileUnit::getDistinct(
1283 Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1284 Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1285 Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1286 getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1287 Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1288 Record.size() <= 14 ? 0 : Record[14],
1289 Record.size() <= 16 ? true : Record[16],
1290 Record.size() <= 17 ? false : Record[17]);
1292 MetadataList.assignValue(CU, NextMetadataNo);
1295 // Move the Upgrade the list of subprograms.
1296 if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1297 CUSubprograms.push_back({CU, SPs});
1300 case bitc::METADATA_SUBPROGRAM: {
1301 if (Record.size() < 18 || Record.size() > 21)
1302 return error("Invalid record");
1305 (Record[0] & 1) || Record[8]; // All definitions should be distinct.
1306 // Version 1 has a Function as Record[15].
1307 // Version 2 has removed Record[15].
1308 // Version 3 has the Unit as Record[15].
1309 // Version 4 added thisAdjustment.
1310 bool HasUnit = Record[0] >= 2;
1311 if (HasUnit && Record.size() < 19)
1312 return error("Invalid record");
1313 Metadata *CUorFn = getMDOrNull(Record[15]);
1314 unsigned Offset = Record.size() >= 19 ? 1 : 0;
1315 bool HasFn = Offset && !HasUnit;
1316 bool HasThisAdj = Record.size() >= 20;
1317 bool HasThrownTypes = Record.size() >= 21;
1318 DISubprogram *SP = GET_OR_DISTINCT(
1321 getDITypeRefOrNull(Record[1]), // scope
1322 getMDString(Record[2]), // name
1323 getMDString(Record[3]), // linkageName
1324 getMDOrNull(Record[4]), // file
1326 getMDOrNull(Record[6]), // type
1327 Record[7], // isLocal
1328 Record[8], // isDefinition
1329 Record[9], // scopeLine
1330 getDITypeRefOrNull(Record[10]), // containingType
1331 Record[11], // virtuality
1332 Record[12], // virtualIndex
1333 HasThisAdj ? Record[19] : 0, // thisAdjustment
1334 static_cast<DINode::DIFlags>(Record[13]), // flags
1335 Record[14], // isOptimized
1336 HasUnit ? CUorFn : nullptr, // unit
1337 getMDOrNull(Record[15 + Offset]), // templateParams
1338 getMDOrNull(Record[16 + Offset]), // declaration
1339 getMDOrNull(Record[17 + Offset]), // variables
1340 HasThrownTypes ? getMDOrNull(Record[20]) : nullptr // thrownTypes
1342 MetadataList.assignValue(SP, NextMetadataNo);
1345 // Upgrade sp->function mapping to function->sp mapping.
1347 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1348 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1349 if (F->isMaterializable())
1350 // Defer until materialized; unmaterialized functions may not have
1352 FunctionsWithSPs[F] = SP;
1353 else if (!F->empty())
1354 F->setSubprogram(SP);
1359 case bitc::METADATA_LEXICAL_BLOCK: {
1360 if (Record.size() != 5)
1361 return error("Invalid record");
1363 IsDistinct = Record[0];
1364 MetadataList.assignValue(
1365 GET_OR_DISTINCT(DILexicalBlock,
1366 (Context, getMDOrNull(Record[1]),
1367 getMDOrNull(Record[2]), Record[3], Record[4])),
1372 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1373 if (Record.size() != 4)
1374 return error("Invalid record");
1376 IsDistinct = Record[0];
1377 MetadataList.assignValue(
1378 GET_OR_DISTINCT(DILexicalBlockFile,
1379 (Context, getMDOrNull(Record[1]),
1380 getMDOrNull(Record[2]), Record[3])),
1385 case bitc::METADATA_NAMESPACE: {
1386 // Newer versions of DINamespace dropped file and line.
1388 if (Record.size() == 3)
1389 Name = getMDString(Record[2]);
1390 else if (Record.size() == 5)
1391 Name = getMDString(Record[3]);
1393 return error("Invalid record");
1395 IsDistinct = Record[0] & 1;
1396 bool ExportSymbols = Record[0] & 2;
1397 MetadataList.assignValue(
1398 GET_OR_DISTINCT(DINamespace,
1399 (Context, getMDOrNull(Record[1]), Name, ExportSymbols)),
1404 case bitc::METADATA_MACRO: {
1405 if (Record.size() != 5)
1406 return error("Invalid record");
1408 IsDistinct = Record[0];
1409 MetadataList.assignValue(
1410 GET_OR_DISTINCT(DIMacro,
1411 (Context, Record[1], Record[2], getMDString(Record[3]),
1412 getMDString(Record[4]))),
1417 case bitc::METADATA_MACRO_FILE: {
1418 if (Record.size() != 5)
1419 return error("Invalid record");
1421 IsDistinct = Record[0];
1422 MetadataList.assignValue(
1423 GET_OR_DISTINCT(DIMacroFile,
1424 (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1425 getMDOrNull(Record[4]))),
1430 case bitc::METADATA_TEMPLATE_TYPE: {
1431 if (Record.size() != 3)
1432 return error("Invalid record");
1434 IsDistinct = Record[0];
1435 MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
1436 (Context, getMDString(Record[1]),
1437 getDITypeRefOrNull(Record[2]))),
1442 case bitc::METADATA_TEMPLATE_VALUE: {
1443 if (Record.size() != 5)
1444 return error("Invalid record");
1446 IsDistinct = Record[0];
1447 MetadataList.assignValue(
1448 GET_OR_DISTINCT(DITemplateValueParameter,
1449 (Context, Record[1], getMDString(Record[2]),
1450 getDITypeRefOrNull(Record[3]),
1451 getMDOrNull(Record[4]))),
1456 case bitc::METADATA_GLOBAL_VAR: {
1457 if (Record.size() < 11 || Record.size() > 12)
1458 return error("Invalid record");
1460 IsDistinct = Record[0] & 1;
1461 unsigned Version = Record[0] >> 1;
1464 MetadataList.assignValue(
1465 GET_OR_DISTINCT(DIGlobalVariable,
1466 (Context, getMDOrNull(Record[1]),
1467 getMDString(Record[2]), getMDString(Record[3]),
1468 getMDOrNull(Record[4]), Record[5],
1469 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1470 getMDOrNull(Record[10]), Record[11])),
1473 } else if (Version == 0) {
1474 // Upgrade old metadata, which stored a global variable reference or a
1475 // ConstantInt here.
1476 NeedUpgradeToDIGlobalVariableExpression = true;
1477 Metadata *Expr = getMDOrNull(Record[9]);
1478 uint32_t AlignInBits = 0;
1479 if (Record.size() > 11) {
1480 if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
1481 return error("Alignment value is too large");
1482 AlignInBits = Record[11];
1484 GlobalVariable *Attach = nullptr;
1485 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
1486 if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
1489 } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
1490 Expr = DIExpression::get(Context,
1491 {dwarf::DW_OP_constu, CI->getZExtValue(),
1492 dwarf::DW_OP_stack_value});
1497 DIGlobalVariable *DGV = GET_OR_DISTINCT(
1499 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1500 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1501 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1502 getMDOrNull(Record[10]), AlignInBits));
1504 DIGlobalVariableExpression *DGVE = nullptr;
1506 DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr);
1508 Attach->addDebugInfo(DGVE);
1510 auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV);
1511 MetadataList.assignValue(MDNode, NextMetadataNo);
1514 return error("Invalid record");
1518 case bitc::METADATA_LOCAL_VAR: {
1519 // 10th field is for the obseleted 'inlinedAt:' field.
1520 if (Record.size() < 8 || Record.size() > 10)
1521 return error("Invalid record");
1523 IsDistinct = Record[0] & 1;
1524 bool HasAlignment = Record[0] & 2;
1525 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
1526 // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
1527 // this is newer version of record which doesn't have artificial tag.
1528 bool HasTag = !HasAlignment && Record.size() > 8;
1529 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
1530 uint32_t AlignInBits = 0;
1532 if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max())
1533 return error("Alignment value is too large");
1534 AlignInBits = Record[8 + HasTag];
1536 MetadataList.assignValue(
1537 GET_OR_DISTINCT(DILocalVariable,
1538 (Context, getMDOrNull(Record[1 + HasTag]),
1539 getMDString(Record[2 + HasTag]),
1540 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
1541 getDITypeRefOrNull(Record[5 + HasTag]),
1542 Record[6 + HasTag], Flags, AlignInBits)),
1547 case bitc::METADATA_EXPRESSION: {
1548 if (Record.size() < 1)
1549 return error("Invalid record");
1551 IsDistinct = Record[0] & 1;
1552 uint64_t Version = Record[0] >> 1;
1553 auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
1554 unsigned N = Elts.size();
1555 // Perform various upgrades.
1558 if (N >= 3 && Elts[N - 3] == dwarf::DW_OP_bit_piece)
1559 Elts[N - 3] = dwarf::DW_OP_LLVM_fragment;
1562 // Move DW_OP_deref to the end.
1563 if (N && Elts[0] == dwarf::DW_OP_deref) {
1564 auto End = Elts.end();
1565 if (Elts.size() >= 3 && *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment)
1566 End = std::prev(End, 3);
1567 std::move(std::next(Elts.begin()), End, Elts.begin());
1568 *std::prev(End) = dwarf::DW_OP_deref;
1570 NeedDeclareExpressionUpgrade = true;
1576 return error("Invalid record");
1579 MetadataList.assignValue(
1580 GET_OR_DISTINCT(DIExpression, (Context, makeArrayRef(Record).slice(1))),
1585 case bitc::METADATA_GLOBAL_VAR_EXPR: {
1586 if (Record.size() != 3)
1587 return error("Invalid record");
1589 IsDistinct = Record[0];
1590 MetadataList.assignValue(GET_OR_DISTINCT(DIGlobalVariableExpression,
1591 (Context, getMDOrNull(Record[1]),
1592 getMDOrNull(Record[2]))),
1597 case bitc::METADATA_OBJC_PROPERTY: {
1598 if (Record.size() != 8)
1599 return error("Invalid record");
1601 IsDistinct = Record[0];
1602 MetadataList.assignValue(
1603 GET_OR_DISTINCT(DIObjCProperty,
1604 (Context, getMDString(Record[1]),
1605 getMDOrNull(Record[2]), Record[3],
1606 getMDString(Record[4]), getMDString(Record[5]),
1607 Record[6], getDITypeRefOrNull(Record[7]))),
1612 case bitc::METADATA_IMPORTED_ENTITY: {
1613 if (Record.size() != 6)
1614 return error("Invalid record");
1616 IsDistinct = Record[0];
1617 MetadataList.assignValue(
1618 GET_OR_DISTINCT(DIImportedEntity,
1619 (Context, Record[1], getMDOrNull(Record[2]),
1620 getDITypeRefOrNull(Record[3]), Record[4],
1621 getMDString(Record[5]))),
1626 case bitc::METADATA_STRING_OLD: {
1627 std::string String(Record.begin(), Record.end());
1629 // Test for upgrading !llvm.loop.
1630 HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
1631 ++NumMDStringLoaded;
1632 Metadata *MD = MDString::get(Context, String);
1633 MetadataList.assignValue(MD, NextMetadataNo);
1637 case bitc::METADATA_STRINGS: {
1638 auto CreateNextMDString = [&](StringRef Str) {
1639 ++NumMDStringLoaded;
1640 MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
1643 if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
1647 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
1648 if (Record.size() % 2 == 0)
1649 return error("Invalid record");
1650 unsigned ValueID = Record[0];
1651 if (ValueID >= ValueList.size())
1652 return error("Invalid record");
1653 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
1654 if (Error Err = parseGlobalObjectAttachment(
1655 *GO, ArrayRef<uint64_t>(Record).slice(1)))
1659 case bitc::METADATA_KIND: {
1660 // Support older bitcode files that had METADATA_KIND records in a
1661 // block with METADATA_BLOCK_ID.
1662 if (Error Err = parseMetadataKindRecord(Record))
1667 return Error::success();
1668 #undef GET_OR_DISTINCT
1671 Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
1672 ArrayRef<uint64_t> Record, StringRef Blob,
1673 function_ref<void(StringRef)> CallBack) {
1674 // All the MDStrings in the block are emitted together in a single
1675 // record. The strings are concatenated and stored in a blob along with
1677 if (Record.size() != 2)
1678 return error("Invalid record: metadata strings layout");
1680 unsigned NumStrings = Record[0];
1681 unsigned StringsOffset = Record[1];
1683 return error("Invalid record: metadata strings with no strings");
1684 if (StringsOffset > Blob.size())
1685 return error("Invalid record: metadata strings corrupt offset");
1687 StringRef Lengths = Blob.slice(0, StringsOffset);
1688 SimpleBitstreamCursor R(Lengths);
1690 StringRef Strings = Blob.drop_front(StringsOffset);
1692 if (R.AtEndOfStream())
1693 return error("Invalid record: metadata strings bad length");
1695 unsigned Size = R.ReadVBR(6);
1696 if (Strings.size() < Size)
1697 return error("Invalid record: metadata strings truncated chars");
1699 CallBack(Strings.slice(0, Size));
1700 Strings = Strings.drop_front(Size);
1701 } while (--NumStrings);
1703 return Error::success();
1706 Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
1707 GlobalObject &GO, ArrayRef<uint64_t> Record) {
1708 assert(Record.size() % 2 == 0);
1709 for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
1710 auto K = MDKindMap.find(Record[I]);
1711 if (K == MDKindMap.end())
1712 return error("Invalid ID");
1713 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
1715 return error("Invalid metadata attachment");
1716 GO.addMetadata(K->second, *MD);
1718 return Error::success();
1721 /// Parse metadata attachments.
1722 Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
1723 Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1724 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
1725 return error("Invalid record");
1727 SmallVector<uint64_t, 64> Record;
1728 PlaceholderQueue Placeholders;
1731 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1733 switch (Entry.Kind) {
1734 case BitstreamEntry::SubBlock: // Handled for us already.
1735 case BitstreamEntry::Error:
1736 return error("Malformed block");
1737 case BitstreamEntry::EndBlock:
1738 resolveForwardRefsAndPlaceholders(Placeholders);
1739 return Error::success();
1740 case BitstreamEntry::Record:
1741 // The interesting case.
1745 // Read a metadata attachment record.
1747 ++NumMDRecordLoaded;
1748 switch (Stream.readRecord(Entry.ID, Record)) {
1749 default: // Default behavior: ignore.
1751 case bitc::METADATA_ATTACHMENT: {
1752 unsigned RecordLength = Record.size();
1754 return error("Invalid record");
1755 if (RecordLength % 2 == 0) {
1756 // A function attachment.
1757 if (Error Err = parseGlobalObjectAttachment(F, Record))
1762 // An instruction attachment.
1763 Instruction *Inst = InstructionList[Record[0]];
1764 for (unsigned i = 1; i != RecordLength; i = i + 2) {
1765 unsigned Kind = Record[i];
1766 DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
1767 if (I == MDKindMap.end())
1768 return error("Invalid ID");
1769 if (I->second == LLVMContext::MD_tbaa && StripTBAA)
1772 auto Idx = Record[i + 1];
1773 if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
1774 !MetadataList.lookup(Idx)) {
1775 // Load the attachment if it is in the lazy-loadable range and hasn't
1777 lazyLoadOneMetadata(Idx, Placeholders);
1778 resolveForwardRefsAndPlaceholders(Placeholders);
1781 Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
1782 if (isa<LocalAsMetadata>(Node))
1783 // Drop the attachment. This used to be legal, but there's no
1786 MDNode *MD = dyn_cast_or_null<MDNode>(Node);
1788 return error("Invalid metadata attachment");
1790 if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
1791 MD = upgradeInstructionLoopAttachment(*MD);
1793 if (I->second == LLVMContext::MD_tbaa) {
1794 assert(!MD->isTemporary() && "should load MDs before attachments");
1795 MD = UpgradeTBAANode(*MD);
1797 Inst->setMetadata(I->second, MD);
1805 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1806 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
1807 SmallVectorImpl<uint64_t> &Record) {
1808 if (Record.size() < 2)
1809 return error("Invalid record");
1811 unsigned Kind = Record[0];
1812 SmallString<8> Name(Record.begin() + 1, Record.end());
1814 unsigned NewKind = TheModule.getMDKindID(Name.str());
1815 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1816 return error("Conflicting METADATA_KIND records");
1817 return Error::success();
1820 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
1821 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
1822 if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
1823 return error("Invalid record");
1825 SmallVector<uint64_t, 64> Record;
1827 // Read all the records.
1829 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1831 switch (Entry.Kind) {
1832 case BitstreamEntry::SubBlock: // Handled for us already.
1833 case BitstreamEntry::Error:
1834 return error("Malformed block");
1835 case BitstreamEntry::EndBlock:
1836 return Error::success();
1837 case BitstreamEntry::Record:
1838 // The interesting case.
1844 ++NumMDRecordLoaded;
1845 unsigned Code = Stream.readRecord(Entry.ID, Record);
1847 default: // Default behavior: ignore.
1849 case bitc::METADATA_KIND: {
1850 if (Error Err = parseMetadataKindRecord(Record))
1858 MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
1859 Pimpl = std::move(RHS.Pimpl);
1862 MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
1863 : Pimpl(std::move(RHS.Pimpl)) {}
1865 MetadataLoader::~MetadataLoader() = default;
1866 MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
1867 BitcodeReaderValueList &ValueList,
1869 std::function<Type *(unsigned)> getTypeByID)
1870 : Pimpl(llvm::make_unique<MetadataLoaderImpl>(
1871 Stream, TheModule, ValueList, std::move(getTypeByID), IsImporting)) {}
1873 Error MetadataLoader::parseMetadata(bool ModuleLevel) {
1874 return Pimpl->parseMetadata(ModuleLevel);
1877 bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
1879 /// Return the given metadata, creating a replaceable forward reference if
1881 Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
1882 return Pimpl->getMetadataFwdRefOrLoad(Idx);
1885 MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) {
1886 return Pimpl->getMDNodeFwdRefOrNull(Idx);
1889 DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
1890 return Pimpl->lookupSubprogramForFunction(F);
1893 Error MetadataLoader::parseMetadataAttachment(
1894 Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1895 return Pimpl->parseMetadataAttachment(F, InstructionList);
1898 Error MetadataLoader::parseMetadataKinds() {
1899 return Pimpl->parseMetadataKinds();
1902 void MetadataLoader::setStripTBAA(bool StripTBAA) {
1903 return Pimpl->setStripTBAA(StripTBAA);
1906 bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
1908 unsigned MetadataLoader::size() const { return Pimpl->size(); }
1909 void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
1911 void MetadataLoader::upgradeDebugIntrinsics(Function &F) {
1912 return Pimpl->upgradeDebugIntrinsics(F);