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/LLVMContext.h"
58 #include "llvm/IR/Module.h"
59 #include "llvm/IR/ModuleSummaryIndex.h"
60 #include "llvm/IR/OperandTraits.h"
61 #include "llvm/IR/Operator.h"
62 #include "llvm/IR/TrackingMDRef.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/ValueHandle.h"
65 #include "llvm/Support/AtomicOrdering.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/CommandLine.h"
68 #include "llvm/Support/Compiler.h"
69 #include "llvm/Support/Debug.h"
70 #include "llvm/Support/Error.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/Support/ManagedStatic.h"
73 #include "llvm/Support/MemoryBuffer.h"
74 #include "llvm/Support/raw_ostream.h"
84 #include <system_error>
91 #define DEBUG_TYPE "bitcode-reader"
93 STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
94 STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
95 STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
97 /// Flag whether we need to import full type definitions for ThinLTO.
98 /// Currently needed for Darwin and LLDB.
99 static cl::opt<bool> ImportFullTypeDefinitions(
100 "import-full-type-definitions", cl::init(false), cl::Hidden,
101 cl::desc("Import full type definitions for ThinLTO."));
103 static cl::opt<bool> DisableLazyLoading(
104 "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
105 cl::desc("Force disable the lazy-loading on-demand of metadata when "
106 "loading bitcode for importing."));
110 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
112 class BitcodeReaderMetadataList {
113 /// Array of metadata references.
115 /// Don't use std::vector here. Some versions of libc++ copy (instead of
116 /// move) on resize, and TrackingMDRef is very expensive to copy.
117 SmallVector<TrackingMDRef, 1> MetadataPtrs;
119 /// The set of indices in MetadataPtrs above of forward references that were
121 SmallDenseSet<unsigned, 1> ForwardReference;
123 /// The set of indices in MetadataPtrs above of Metadata that need to be
125 SmallDenseSet<unsigned, 1> UnresolvedNodes;
127 /// Structures for resolving old type refs.
129 SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
130 SmallDenseMap<MDString *, DICompositeType *, 1> Final;
131 SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
132 SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
135 LLVMContext &Context;
138 BitcodeReaderMetadataList(LLVMContext &C) : Context(C) {}
140 // vector compatibility methods
141 unsigned size() const { return MetadataPtrs.size(); }
142 void resize(unsigned N) { MetadataPtrs.resize(N); }
143 void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
144 void clear() { MetadataPtrs.clear(); }
145 Metadata *back() const { return MetadataPtrs.back(); }
146 void pop_back() { MetadataPtrs.pop_back(); }
147 bool empty() const { return MetadataPtrs.empty(); }
149 Metadata *operator[](unsigned i) const {
150 assert(i < MetadataPtrs.size());
151 return MetadataPtrs[i];
154 Metadata *lookup(unsigned I) const {
155 if (I < MetadataPtrs.size())
156 return MetadataPtrs[I];
160 void shrinkTo(unsigned N) {
161 assert(N <= size() && "Invalid shrinkTo request!");
162 assert(ForwardReference.empty() && "Unexpected forward refs");
163 assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
164 MetadataPtrs.resize(N);
167 /// Return the given metadata, creating a replaceable forward reference if
169 Metadata *getMetadataFwdRef(unsigned Idx);
171 /// Return the the given metadata only if it is fully resolved.
173 /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
174 /// would give \c false.
175 Metadata *getMetadataIfResolved(unsigned Idx);
177 MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
178 void assignValue(Metadata *MD, unsigned Idx);
179 void tryToResolveCycles();
180 bool hasFwdRefs() const { return !ForwardReference.empty(); }
181 int getNextFwdRef() {
182 assert(hasFwdRefs());
183 return *ForwardReference.begin();
186 /// Upgrade a type that had an MDString reference.
187 void addTypeRef(MDString &UUID, DICompositeType &CT);
189 /// Upgrade a type that had an MDString reference.
190 Metadata *upgradeTypeRef(Metadata *MaybeUUID);
192 /// Upgrade a type ref array that may have MDString references.
193 Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
196 Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
199 void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
200 if (auto *MDN = dyn_cast<MDNode>(MD))
201 if (!MDN->isResolved())
202 UnresolvedNodes.insert(Idx);
212 TrackingMDRef &OldMD = MetadataPtrs[Idx];
218 // If there was a forward reference to this value, replace it.
219 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
220 PrevMD->replaceAllUsesWith(MD);
221 ForwardReference.erase(Idx);
224 Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
228 if (Metadata *MD = MetadataPtrs[Idx])
231 // Track forward refs to be resolved later.
232 ForwardReference.insert(Idx);
234 // Create and return a placeholder, which will later be RAUW'd.
235 ++NumMDNodeTemporary;
236 Metadata *MD = MDNode::getTemporary(Context, None).release();
237 MetadataPtrs[Idx].reset(MD);
241 Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
242 Metadata *MD = lookup(Idx);
243 if (auto *N = dyn_cast_or_null<MDNode>(MD))
244 if (!N->isResolved())
249 MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
250 return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
253 void BitcodeReaderMetadataList::tryToResolveCycles() {
254 if (!ForwardReference.empty())
255 // Still forward references... can't resolve cycles.
258 // Give up on finding a full definition for any forward decls that remain.
259 for (const auto &Ref : OldTypeRefs.FwdDecls)
260 OldTypeRefs.Final.insert(Ref);
261 OldTypeRefs.FwdDecls.clear();
263 // Upgrade from old type ref arrays. In strange cases, this could add to
264 // OldTypeRefs.Unknown.
265 for (const auto &Array : OldTypeRefs.Arrays)
266 Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
267 OldTypeRefs.Arrays.clear();
269 // Replace old string-based type refs with the resolved node, if possible.
270 // If we haven't seen the node, leave it to the verifier to complain about
271 // the invalid string reference.
272 for (const auto &Ref : OldTypeRefs.Unknown) {
273 if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
274 Ref.second->replaceAllUsesWith(CT);
276 Ref.second->replaceAllUsesWith(Ref.first);
278 OldTypeRefs.Unknown.clear();
280 if (UnresolvedNodes.empty())
284 // Resolve any cycles.
285 for (unsigned I : UnresolvedNodes) {
286 auto &MD = MetadataPtrs[I];
287 auto *N = dyn_cast_or_null<MDNode>(MD);
291 assert(!N->isTemporary() && "Unexpected forward reference");
295 // Make sure we return early again until there's another unresolved ref.
296 UnresolvedNodes.clear();
299 void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
300 DICompositeType &CT) {
301 assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
302 if (CT.isForwardDecl())
303 OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
305 OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
308 Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
309 auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
310 if (LLVM_LIKELY(!UUID))
313 if (auto *CT = OldTypeRefs.Final.lookup(UUID))
316 auto &Ref = OldTypeRefs.Unknown[UUID];
318 Ref = MDNode::getTemporary(Context, None);
322 Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
323 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
324 if (!Tuple || Tuple->isDistinct())
327 // Look through the array immediately if possible.
328 if (!Tuple->isTemporary())
329 return resolveTypeRefArray(Tuple);
331 // Create and return a placeholder to use for now. Eventually
332 // resolveTypeRefArrays() will be resolve this forward reference.
333 OldTypeRefs.Arrays.emplace_back(
334 std::piecewise_construct, std::forward_as_tuple(Tuple),
335 std::forward_as_tuple(MDTuple::getTemporary(Context, None)));
336 return OldTypeRefs.Arrays.back().second.get();
339 Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
340 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
341 if (!Tuple || Tuple->isDistinct())
344 // Look through the DITypeRefArray, upgrading each DITypeRef.
345 SmallVector<Metadata *, 32> Ops;
346 Ops.reserve(Tuple->getNumOperands());
347 for (Metadata *MD : Tuple->operands())
348 Ops.push_back(upgradeTypeRef(MD));
350 return MDTuple::get(Context, Ops);
355 class PlaceholderQueue {
356 // Placeholders would thrash around when moved, so store in a std::deque
357 // instead of some sort of vector.
358 std::deque<DistinctMDOperandPlaceholder> PHs;
361 bool empty() { return PHs.empty(); }
362 DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
363 void flush(BitcodeReaderMetadataList &MetadataList);
365 /// Return the list of temporaries nodes in the queue, these need to be
366 /// loaded before we can flush the queue.
367 void getTemporaries(BitcodeReaderMetadataList &MetadataList,
368 DenseSet<unsigned> &Temporaries) {
369 for (auto &PH : PHs) {
370 auto ID = PH.getID();
371 auto *MD = MetadataList.lookup(ID);
373 Temporaries.insert(ID);
376 auto *N = dyn_cast_or_null<MDNode>(MD);
377 if (N && N->isTemporary())
378 Temporaries.insert(ID);
383 } // end anonymous namespace
385 DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
386 PHs.emplace_back(ID);
390 void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
391 while (!PHs.empty()) {
392 auto *MD = MetadataList.lookup(PHs.front().getID());
393 assert(MD && "Flushing placeholder on unassigned MD");
395 if (auto *MDN = dyn_cast<MDNode>(MD))
396 assert(MDN->isResolved() &&
397 "Flushing Placeholder while cycles aren't resolved");
399 PHs.front().replaceUseWith(MD);
404 } // anonynous namespace
406 class MetadataLoader::MetadataLoaderImpl {
407 BitcodeReaderMetadataList MetadataList;
408 BitcodeReaderValueList &ValueList;
409 BitstreamCursor &Stream;
410 LLVMContext &Context;
412 std::function<Type *(unsigned)> getTypeByID;
414 /// Cursor associated with the lazy-loading of Metadata. This is the easy way
415 /// to keep around the right "context" (Abbrev list) to be able to jump in
416 /// the middle of the metadata block and load any record.
417 BitstreamCursor IndexCursor;
419 /// Index that keeps track of MDString values.
420 std::vector<StringRef> MDStringRef;
422 /// On-demand loading of a single MDString. Requires the index above to be
424 MDString *lazyLoadOneMDString(unsigned Idx);
426 /// Index that keeps track of where to find a metadata record in the stream.
427 std::vector<uint64_t> GlobalMetadataBitPosIndex;
429 /// Populate the index above to enable lazily loading of metadata, and load
430 /// the named metadata as well as the transitively referenced global
432 Expected<bool> lazyLoadModuleMetadataBlock();
434 /// On-demand loading of a single metadata. Requires the index above to be
436 void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
438 // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
439 // point from SP to CU after a block is completly parsed.
440 std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
442 /// Functions that need to be matched with subprograms when upgrading old
444 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
446 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
447 DenseMap<unsigned, unsigned> MDKindMap;
449 bool StripTBAA = false;
450 bool HasSeenOldLoopTags = false;
452 /// True if metadata is being parsed for a module being ThinLTO imported.
453 bool IsImporting = false;
455 Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
456 PlaceholderQueue &Placeholders, StringRef Blob,
457 unsigned &NextMetadataNo);
458 Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
459 std::function<void(StringRef)> CallBack);
460 Error parseGlobalObjectAttachment(GlobalObject &GO,
461 ArrayRef<uint64_t> Record);
462 Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
464 void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
466 /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
467 void upgradeCUSubprograms() {
468 for (auto CU_SP : CUSubprograms)
469 if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
470 for (auto &Op : SPs->operands())
471 if (auto *SP = dyn_cast_or_null<MDNode>(Op))
472 SP->replaceOperandWith(7, CU_SP.first);
473 CUSubprograms.clear();
477 MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
478 BitcodeReaderValueList &ValueList,
479 std::function<Type *(unsigned)> getTypeByID,
481 : MetadataList(TheModule.getContext()), ValueList(ValueList),
482 Stream(Stream), Context(TheModule.getContext()), TheModule(TheModule),
483 getTypeByID(getTypeByID), IsImporting(IsImporting) {}
485 Error parseMetadata(bool ModuleLevel);
487 bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
489 Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
490 if (ID < MDStringRef.size())
491 return lazyLoadOneMDString(ID);
492 if (auto *MD = MetadataList.lookup(ID))
494 // If lazy-loading is enabled, we try recursively to load the operand
495 // instead of creating a temporary.
496 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
497 PlaceholderQueue Placeholders;
498 lazyLoadOneMetadata(ID, Placeholders);
499 resolveForwardRefsAndPlaceholders(Placeholders);
500 return MetadataList.lookup(ID);
502 return MetadataList.getMetadataFwdRef(ID);
505 MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
506 return MetadataList.getMDNodeFwdRefOrNull(Idx);
509 DISubprogram *lookupSubprogramForFunction(Function *F) {
510 return FunctionsWithSPs.lookup(F);
513 bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; }
515 Error parseMetadataAttachment(
516 Function &F, const SmallVectorImpl<Instruction *> &InstructionList);
518 Error parseMetadataKinds();
520 void setStripTBAA(bool Value) { StripTBAA = Value; }
521 bool isStrippingTBAA() { return StripTBAA; }
523 unsigned size() const { return MetadataList.size(); }
524 void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
527 Error error(const Twine &Message) {
528 return make_error<StringError>(
529 Message, make_error_code(BitcodeError::CorruptedBitcode));
533 MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
534 IndexCursor = Stream;
535 SmallVector<uint64_t, 64> Record;
536 // Get the abbrevs, and preload record positions to make them lazy-loadable.
538 BitstreamEntry Entry = IndexCursor.advanceSkippingSubblocks(
539 BitstreamCursor::AF_DontPopBlockAtEnd);
540 switch (Entry.Kind) {
541 case BitstreamEntry::SubBlock: // Handled for us already.
542 case BitstreamEntry::Error:
543 return error("Malformed block");
544 case BitstreamEntry::EndBlock: {
547 case BitstreamEntry::Record: {
548 // The interesting case.
550 uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
551 auto Code = IndexCursor.skipRecord(Entry.ID);
553 case bitc::METADATA_STRINGS: {
554 // Rewind and parse the strings.
555 IndexCursor.JumpToBit(CurrentPos);
558 IndexCursor.readRecord(Entry.ID, Record, &Blob);
559 unsigned NumStrings = Record[0];
560 MDStringRef.reserve(NumStrings);
561 auto IndexNextMDString = [&](StringRef Str) {
562 MDStringRef.push_back(Str);
564 if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
565 return std::move(Err);
568 case bitc::METADATA_INDEX_OFFSET: {
569 // This is the offset to the index, when we see this we skip all the
570 // records and load only an index to these.
571 IndexCursor.JumpToBit(CurrentPos);
573 IndexCursor.readRecord(Entry.ID, Record);
574 if (Record.size() != 2)
575 return error("Invalid record");
576 auto Offset = Record[0] + (Record[1] << 32);
577 auto BeginPos = IndexCursor.GetCurrentBitNo();
578 IndexCursor.JumpToBit(BeginPos + Offset);
579 Entry = IndexCursor.advanceSkippingSubblocks(
580 BitstreamCursor::AF_DontPopBlockAtEnd);
581 assert(Entry.Kind == BitstreamEntry::Record &&
582 "Corrupted bitcode: Expected `Record` when trying to find the "
585 auto Code = IndexCursor.readRecord(Entry.ID, Record);
587 assert(Code == bitc::METADATA_INDEX && "Corrupted bitcode: Expected "
588 "`METADATA_INDEX` when trying "
589 "to find the Metadata index");
592 auto CurrentValue = BeginPos;
593 GlobalMetadataBitPosIndex.reserve(Record.size());
594 for (auto &Elt : Record) {
596 GlobalMetadataBitPosIndex.push_back(CurrentValue);
600 case bitc::METADATA_INDEX:
601 // We don't expect to get there, the Index is loaded when we encounter
603 return error("Corrupted Metadata block");
604 case bitc::METADATA_NAME: {
605 // Named metadata need to be materialized now and aren't deferred.
606 IndexCursor.JumpToBit(CurrentPos);
608 unsigned Code = IndexCursor.readRecord(Entry.ID, Record);
609 assert(Code == bitc::METADATA_NAME);
611 // Read name of the named metadata.
612 SmallString<8> Name(Record.begin(), Record.end());
613 Code = IndexCursor.ReadCode();
615 // Named Metadata comes in two parts, we expect the name to be followed
618 unsigned NextBitCode = IndexCursor.readRecord(Code, Record);
619 assert(NextBitCode == bitc::METADATA_NAMED_NODE);
622 // Read named metadata elements.
623 unsigned Size = Record.size();
624 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
625 for (unsigned i = 0; i != Size; ++i) {
626 // FIXME: We could use a placeholder here, however NamedMDNode are
627 // taking MDNode as operand and not using the Metadata infrastructure.
628 // It is acknowledged by 'TODO: Inherit from Metadata' in the
629 // NamedMDNode class definition.
630 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
631 assert(MD && "Invalid record");
636 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
637 // FIXME: we need to do this early because we don't materialize global
639 IndexCursor.JumpToBit(CurrentPos);
641 IndexCursor.readRecord(Entry.ID, Record);
642 if (Record.size() % 2 == 0)
643 return error("Invalid record");
644 unsigned ValueID = Record[0];
645 if (ValueID >= ValueList.size())
646 return error("Invalid record");
647 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
648 if (Error Err = parseGlobalObjectAttachment(
649 *GO, ArrayRef<uint64_t>(Record).slice(1)))
650 return std::move(Err);
653 case bitc::METADATA_KIND:
654 case bitc::METADATA_STRING_OLD:
655 case bitc::METADATA_OLD_FN_NODE:
656 case bitc::METADATA_OLD_NODE:
657 case bitc::METADATA_VALUE:
658 case bitc::METADATA_DISTINCT_NODE:
659 case bitc::METADATA_NODE:
660 case bitc::METADATA_LOCATION:
661 case bitc::METADATA_GENERIC_DEBUG:
662 case bitc::METADATA_SUBRANGE:
663 case bitc::METADATA_ENUMERATOR:
664 case bitc::METADATA_BASIC_TYPE:
665 case bitc::METADATA_DERIVED_TYPE:
666 case bitc::METADATA_COMPOSITE_TYPE:
667 case bitc::METADATA_SUBROUTINE_TYPE:
668 case bitc::METADATA_MODULE:
669 case bitc::METADATA_FILE:
670 case bitc::METADATA_COMPILE_UNIT:
671 case bitc::METADATA_SUBPROGRAM:
672 case bitc::METADATA_LEXICAL_BLOCK:
673 case bitc::METADATA_LEXICAL_BLOCK_FILE:
674 case bitc::METADATA_NAMESPACE:
675 case bitc::METADATA_MACRO:
676 case bitc::METADATA_MACRO_FILE:
677 case bitc::METADATA_TEMPLATE_TYPE:
678 case bitc::METADATA_TEMPLATE_VALUE:
679 case bitc::METADATA_GLOBAL_VAR:
680 case bitc::METADATA_LOCAL_VAR:
681 case bitc::METADATA_EXPRESSION:
682 case bitc::METADATA_OBJC_PROPERTY:
683 case bitc::METADATA_IMPORTED_ENTITY:
684 case bitc::METADATA_GLOBAL_VAR_EXPR:
685 // We don't expect to see any of these, if we see one, give up on
686 // lazy-loading and fallback.
688 GlobalMetadataBitPosIndex.clear();
697 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
698 /// module level metadata.
699 Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
700 if (!ModuleLevel && MetadataList.hasFwdRefs())
701 return error("Invalid metadata: fwd refs into function blocks");
703 // Record the entry position so that we can jump back here and efficiently
704 // skip the whole block in case we lazy-load.
705 auto EntryPos = Stream.GetCurrentBitNo();
707 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
708 return error("Invalid record");
710 SmallVector<uint64_t, 64> Record;
711 PlaceholderQueue Placeholders;
713 // We lazy-load module-level metadata: we build an index for each record, and
714 // then load individual record as needed, starting with the named metadata.
715 if (ModuleLevel && IsImporting && MetadataList.empty() &&
716 !DisableLazyLoading) {
717 auto SuccessOrErr = lazyLoadModuleMetadataBlock();
719 return SuccessOrErr.takeError();
720 if (SuccessOrErr.get()) {
721 // An index was successfully created and we will be able to load metadata
723 MetadataList.resize(MDStringRef.size() +
724 GlobalMetadataBitPosIndex.size());
726 // Reading the named metadata created forward references and/or
727 // placeholders, that we flush here.
728 resolveForwardRefsAndPlaceholders(Placeholders);
729 upgradeCUSubprograms();
730 // Return at the beginning of the block, since it is easy to skip it
731 // entirely from there.
732 Stream.ReadBlockEnd(); // Pop the abbrev block context.
733 Stream.JumpToBit(EntryPos);
734 if (Stream.SkipBlock())
735 return error("Invalid record");
736 return Error::success();
738 // Couldn't load an index, fallback to loading all the block "old-style".
741 unsigned NextMetadataNo = MetadataList.size();
743 // Read all the records.
745 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
747 switch (Entry.Kind) {
748 case BitstreamEntry::SubBlock: // Handled for us already.
749 case BitstreamEntry::Error:
750 return error("Malformed block");
751 case BitstreamEntry::EndBlock:
752 resolveForwardRefsAndPlaceholders(Placeholders);
753 upgradeCUSubprograms();
754 return Error::success();
755 case BitstreamEntry::Record:
756 // The interesting case.
764 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
766 parseOneMetadata(Record, Code, Placeholders, Blob, NextMetadataNo))
771 MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
773 if (Metadata *MD = MetadataList.lookup(ID))
774 return cast<MDString>(MD);
775 auto MDS = MDString::get(Context, MDStringRef[ID]);
776 MetadataList.assignValue(MDS, ID);
780 void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
781 unsigned ID, PlaceholderQueue &Placeholders) {
782 assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
783 assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
784 // Lookup first if the metadata hasn't already been loaded.
785 if (auto *MD = MetadataList.lookup(ID)) {
786 auto *N = dyn_cast_or_null<MDNode>(MD);
787 if (!N->isTemporary())
790 SmallVector<uint64_t, 64> Record;
792 IndexCursor.JumpToBit(GlobalMetadataBitPosIndex[ID - MDStringRef.size()]);
793 auto Entry = IndexCursor.advanceSkippingSubblocks();
795 unsigned Code = IndexCursor.readRecord(Entry.ID, Record, &Blob);
796 if (Error Err = parseOneMetadata(Record, Code, Placeholders, Blob, ID))
797 report_fatal_error("Can't lazyload MD");
800 /// Ensure that all forward-references and placeholders are resolved.
801 /// Iteratively lazy-loading metadata on-demand if needed.
802 void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
803 PlaceholderQueue &Placeholders) {
804 DenseSet<unsigned> Temporaries;
806 // Populate Temporaries with the placeholders that haven't been loaded yet.
807 Placeholders.getTemporaries(MetadataList, Temporaries);
809 // If we don't have any temporary, or FwdReference, we're done!
810 if (Temporaries.empty() && !MetadataList.hasFwdRefs())
813 // First, load all the temporaries. This can add new placeholders or
814 // forward references.
815 for (auto ID : Temporaries)
816 lazyLoadOneMetadata(ID, Placeholders);
819 // Second, load the forward-references. This can also add new placeholders
820 // or forward references.
821 while (MetadataList.hasFwdRefs())
822 lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
824 // At this point we don't have any forward reference remaining, or temporary
825 // that haven't been loaded. We can safely drop RAUW support and mark cycles
827 MetadataList.tryToResolveCycles();
829 // Finally, everything is in place, we can replace the placeholders operands
830 // with the final node they refer to.
831 Placeholders.flush(MetadataList);
834 Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
835 SmallVectorImpl<uint64_t> &Record, unsigned Code,
836 PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
838 bool IsDistinct = false;
839 auto getMD = [&](unsigned ID) -> Metadata * {
840 if (ID < MDStringRef.size())
841 return lazyLoadOneMDString(ID);
843 if (auto *MD = MetadataList.lookup(ID))
845 // If lazy-loading is enabled, we try recursively to load the operand
846 // instead of creating a temporary.
847 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
848 // Create a temporary for the node that is referencing the operand we
849 // will lazy-load. It is needed before recursing in case there are
851 MetadataList.getMetadataFwdRef(NextMetadataNo);
852 lazyLoadOneMetadata(ID, Placeholders);
853 return MetadataList.lookup(ID);
855 // Return a temporary.
856 return MetadataList.getMetadataFwdRef(ID);
858 if (auto *MD = MetadataList.getMetadataIfResolved(ID))
860 return &Placeholders.getPlaceholderOp(ID);
862 auto getMDOrNull = [&](unsigned ID) -> Metadata * {
864 return getMD(ID - 1);
867 auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
869 return MetadataList.getMetadataFwdRef(ID - 1);
872 auto getMDString = [&](unsigned ID) -> MDString * {
873 // This requires that the ID is not really a forward reference. In
874 // particular, the MDString must already have been resolved.
875 auto MDS = getMDOrNull(ID);
876 return cast_or_null<MDString>(MDS);
879 // Support for old type refs.
880 auto getDITypeRefOrNull = [&](unsigned ID) {
881 return MetadataList.upgradeTypeRef(getMDOrNull(ID));
884 #define GET_OR_DISTINCT(CLASS, ARGS) \
885 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
888 default: // Default behavior: ignore.
890 case bitc::METADATA_NAME: {
891 // Read name of the named metadata.
892 SmallString<8> Name(Record.begin(), Record.end());
894 Code = Stream.ReadCode();
897 unsigned NextBitCode = Stream.readRecord(Code, Record);
898 if (NextBitCode != bitc::METADATA_NAMED_NODE)
899 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
901 // Read named metadata elements.
902 unsigned Size = Record.size();
903 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
904 for (unsigned i = 0; i != Size; ++i) {
905 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
907 return error("Invalid record");
912 case bitc::METADATA_OLD_FN_NODE: {
913 // FIXME: Remove in 4.0.
914 // This is a LocalAsMetadata record, the only type of function-local
916 if (Record.size() % 2 == 1)
917 return error("Invalid record");
919 // If this isn't a LocalAsMetadata record, we're dropping it. This used
920 // to be legal, but there's no upgrade path.
921 auto dropRecord = [&] {
922 MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo);
925 if (Record.size() != 2) {
930 Type *Ty = getTypeByID(Record[0]);
931 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
936 MetadataList.assignValue(
937 LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
942 case bitc::METADATA_OLD_NODE: {
943 // FIXME: Remove in 4.0.
944 if (Record.size() % 2 == 1)
945 return error("Invalid record");
947 unsigned Size = Record.size();
948 SmallVector<Metadata *, 8> Elts;
949 for (unsigned i = 0; i != Size; i += 2) {
950 Type *Ty = getTypeByID(Record[i]);
952 return error("Invalid record");
953 if (Ty->isMetadataTy())
954 Elts.push_back(getMD(Record[i + 1]));
955 else if (!Ty->isVoidTy()) {
957 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
958 assert(isa<ConstantAsMetadata>(MD) &&
959 "Expected non-function-local metadata");
962 Elts.push_back(nullptr);
964 MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
968 case bitc::METADATA_VALUE: {
969 if (Record.size() != 2)
970 return error("Invalid record");
972 Type *Ty = getTypeByID(Record[0]);
973 if (Ty->isMetadataTy() || Ty->isVoidTy())
974 return error("Invalid record");
976 MetadataList.assignValue(
977 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
982 case bitc::METADATA_DISTINCT_NODE:
985 case bitc::METADATA_NODE: {
986 SmallVector<Metadata *, 8> Elts;
987 Elts.reserve(Record.size());
988 for (unsigned ID : Record)
989 Elts.push_back(getMDOrNull(ID));
990 MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
991 : MDNode::get(Context, Elts),
996 case bitc::METADATA_LOCATION: {
997 if (Record.size() != 5)
998 return error("Invalid record");
1000 IsDistinct = Record[0];
1001 unsigned Line = Record[1];
1002 unsigned Column = Record[2];
1003 Metadata *Scope = getMD(Record[3]);
1004 Metadata *InlinedAt = getMDOrNull(Record[4]);
1005 MetadataList.assignValue(
1006 GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt)),
1011 case bitc::METADATA_GENERIC_DEBUG: {
1012 if (Record.size() < 4)
1013 return error("Invalid record");
1015 IsDistinct = Record[0];
1016 unsigned Tag = Record[1];
1017 unsigned Version = Record[2];
1019 if (Tag >= 1u << 16 || Version != 0)
1020 return error("Invalid record");
1022 auto *Header = getMDString(Record[3]);
1023 SmallVector<Metadata *, 8> DwarfOps;
1024 for (unsigned I = 4, E = Record.size(); I != E; ++I)
1025 DwarfOps.push_back(getMDOrNull(Record[I]));
1026 MetadataList.assignValue(
1027 GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1032 case bitc::METADATA_SUBRANGE: {
1033 if (Record.size() != 3)
1034 return error("Invalid record");
1036 IsDistinct = Record[0];
1037 MetadataList.assignValue(
1038 GET_OR_DISTINCT(DISubrange,
1039 (Context, Record[1], unrotateSign(Record[2]))),
1044 case bitc::METADATA_ENUMERATOR: {
1045 if (Record.size() != 3)
1046 return error("Invalid record");
1048 IsDistinct = Record[0];
1049 MetadataList.assignValue(
1050 GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
1051 getMDString(Record[2]))),
1056 case bitc::METADATA_BASIC_TYPE: {
1057 if (Record.size() != 6)
1058 return error("Invalid record");
1060 IsDistinct = Record[0];
1061 MetadataList.assignValue(
1062 GET_OR_DISTINCT(DIBasicType,
1063 (Context, Record[1], getMDString(Record[2]), Record[3],
1064 Record[4], Record[5])),
1069 case bitc::METADATA_DERIVED_TYPE: {
1070 if (Record.size() != 12)
1071 return error("Invalid record");
1073 IsDistinct = Record[0];
1074 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1075 MetadataList.assignValue(
1076 GET_OR_DISTINCT(DIDerivedType,
1077 (Context, Record[1], getMDString(Record[2]),
1078 getMDOrNull(Record[3]), Record[4],
1079 getDITypeRefOrNull(Record[5]),
1080 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1081 Record[9], Flags, getDITypeRefOrNull(Record[11]))),
1086 case bitc::METADATA_COMPOSITE_TYPE: {
1087 if (Record.size() != 16)
1088 return error("Invalid record");
1090 // If we have a UUID and this is not a forward declaration, lookup the
1092 IsDistinct = Record[0] & 0x1;
1093 bool IsNotUsedInTypeRef = Record[0] >= 2;
1094 unsigned Tag = Record[1];
1095 MDString *Name = getMDString(Record[2]);
1096 Metadata *File = getMDOrNull(Record[3]);
1097 unsigned Line = Record[4];
1098 Metadata *Scope = getDITypeRefOrNull(Record[5]);
1099 Metadata *BaseType = nullptr;
1100 uint64_t SizeInBits = Record[7];
1101 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1102 return error("Alignment value is too large");
1103 uint32_t AlignInBits = Record[8];
1104 uint64_t OffsetInBits = 0;
1105 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1106 Metadata *Elements = nullptr;
1107 unsigned RuntimeLang = Record[12];
1108 Metadata *VTableHolder = nullptr;
1109 Metadata *TemplateParams = nullptr;
1110 auto *Identifier = getMDString(Record[15]);
1111 // If this module is being parsed so that it can be ThinLTO imported
1112 // into another module, composite types only need to be imported
1113 // as type declarations (unless full type definitions requested).
1114 // Create type declarations up front to save memory. Also, buildODRType
1115 // handles the case where this is type ODRed with a definition needed
1116 // by the importing module, in which case the existing definition is
1118 if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1119 (Tag == dwarf::DW_TAG_enumeration_type ||
1120 Tag == dwarf::DW_TAG_class_type ||
1121 Tag == dwarf::DW_TAG_structure_type ||
1122 Tag == dwarf::DW_TAG_union_type)) {
1123 Flags = Flags | DINode::FlagFwdDecl;
1125 BaseType = getDITypeRefOrNull(Record[6]);
1126 OffsetInBits = Record[9];
1127 Elements = getMDOrNull(Record[11]);
1128 VTableHolder = getDITypeRefOrNull(Record[13]);
1129 TemplateParams = getMDOrNull(Record[14]);
1131 DICompositeType *CT = nullptr;
1133 CT = DICompositeType::buildODRType(
1134 Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1135 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1136 VTableHolder, TemplateParams);
1138 // Create a node if we didn't get a lazy ODR type.
1140 CT = GET_OR_DISTINCT(DICompositeType,
1141 (Context, Tag, Name, File, Line, Scope, BaseType,
1142 SizeInBits, AlignInBits, OffsetInBits, Flags,
1143 Elements, RuntimeLang, VTableHolder, TemplateParams,
1145 if (!IsNotUsedInTypeRef && Identifier)
1146 MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1148 MetadataList.assignValue(CT, NextMetadataNo);
1152 case bitc::METADATA_SUBROUTINE_TYPE: {
1153 if (Record.size() < 3 || Record.size() > 4)
1154 return error("Invalid record");
1155 bool IsOldTypeRefArray = Record[0] < 2;
1156 unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1158 IsDistinct = Record[0] & 0x1;
1159 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1160 Metadata *Types = getMDOrNull(Record[2]);
1161 if (LLVM_UNLIKELY(IsOldTypeRefArray))
1162 Types = MetadataList.upgradeTypeRefArray(Types);
1164 MetadataList.assignValue(
1165 GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1171 case bitc::METADATA_MODULE: {
1172 if (Record.size() != 6)
1173 return error("Invalid record");
1175 IsDistinct = Record[0];
1176 MetadataList.assignValue(
1177 GET_OR_DISTINCT(DIModule,
1178 (Context, getMDOrNull(Record[1]),
1179 getMDString(Record[2]), getMDString(Record[3]),
1180 getMDString(Record[4]), getMDString(Record[5]))),
1186 case bitc::METADATA_FILE: {
1187 if (Record.size() != 3 && Record.size() != 5)
1188 return error("Invalid record");
1190 IsDistinct = Record[0];
1191 MetadataList.assignValue(
1194 (Context, getMDString(Record[1]), getMDString(Record[2]),
1195 Record.size() == 3 ? DIFile::CSK_None
1196 : static_cast<DIFile::ChecksumKind>(Record[3]),
1197 Record.size() == 3 ? nullptr : getMDString(Record[4]))),
1202 case bitc::METADATA_COMPILE_UNIT: {
1203 if (Record.size() < 14 || Record.size() > 17)
1204 return error("Invalid record");
1206 // Ignore Record[0], which indicates whether this compile unit is
1207 // distinct. It's always distinct.
1209 auto *CU = DICompileUnit::getDistinct(
1210 Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1211 Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1212 Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1213 getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1214 Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1215 Record.size() <= 14 ? 0 : Record[14],
1216 Record.size() <= 16 ? true : Record[16]);
1218 MetadataList.assignValue(CU, NextMetadataNo);
1221 // Move the Upgrade the list of subprograms.
1222 if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1223 CUSubprograms.push_back({CU, SPs});
1226 case bitc::METADATA_SUBPROGRAM: {
1227 if (Record.size() < 18 || Record.size() > 20)
1228 return error("Invalid record");
1231 (Record[0] & 1) || Record[8]; // All definitions should be distinct.
1232 // Version 1 has a Function as Record[15].
1233 // Version 2 has removed Record[15].
1234 // Version 3 has the Unit as Record[15].
1235 // Version 4 added thisAdjustment.
1236 bool HasUnit = Record[0] >= 2;
1237 if (HasUnit && Record.size() < 19)
1238 return error("Invalid record");
1239 Metadata *CUorFn = getMDOrNull(Record[15]);
1240 unsigned Offset = Record.size() >= 19 ? 1 : 0;
1241 bool HasFn = Offset && !HasUnit;
1242 bool HasThisAdj = Record.size() >= 20;
1243 DISubprogram *SP = GET_OR_DISTINCT(
1244 DISubprogram, (Context,
1245 getDITypeRefOrNull(Record[1]), // scope
1246 getMDString(Record[2]), // name
1247 getMDString(Record[3]), // linkageName
1248 getMDOrNull(Record[4]), // file
1250 getMDOrNull(Record[6]), // type
1251 Record[7], // isLocal
1252 Record[8], // isDefinition
1253 Record[9], // scopeLine
1254 getDITypeRefOrNull(Record[10]), // containingType
1255 Record[11], // virtuality
1256 Record[12], // virtualIndex
1257 HasThisAdj ? Record[19] : 0, // thisAdjustment
1258 static_cast<DINode::DIFlags>(Record[13] // flags
1260 Record[14], // isOptimized
1261 HasUnit ? CUorFn : nullptr, // unit
1262 getMDOrNull(Record[15 + Offset]), // templateParams
1263 getMDOrNull(Record[16 + Offset]), // declaration
1264 getMDOrNull(Record[17 + Offset]) // variables
1266 MetadataList.assignValue(SP, NextMetadataNo);
1269 // Upgrade sp->function mapping to function->sp mapping.
1271 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1272 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1273 if (F->isMaterializable())
1274 // Defer until materialized; unmaterialized functions may not have
1276 FunctionsWithSPs[F] = SP;
1277 else if (!F->empty())
1278 F->setSubprogram(SP);
1283 case bitc::METADATA_LEXICAL_BLOCK: {
1284 if (Record.size() != 5)
1285 return error("Invalid record");
1287 IsDistinct = Record[0];
1288 MetadataList.assignValue(
1289 GET_OR_DISTINCT(DILexicalBlock,
1290 (Context, getMDOrNull(Record[1]),
1291 getMDOrNull(Record[2]), Record[3], Record[4])),
1296 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1297 if (Record.size() != 4)
1298 return error("Invalid record");
1300 IsDistinct = Record[0];
1301 MetadataList.assignValue(
1302 GET_OR_DISTINCT(DILexicalBlockFile,
1303 (Context, getMDOrNull(Record[1]),
1304 getMDOrNull(Record[2]), Record[3])),
1309 case bitc::METADATA_NAMESPACE: {
1310 if (Record.size() != 5)
1311 return error("Invalid record");
1313 IsDistinct = Record[0] & 1;
1314 bool ExportSymbols = Record[0] & 2;
1315 MetadataList.assignValue(
1316 GET_OR_DISTINCT(DINamespace,
1317 (Context, getMDOrNull(Record[1]),
1318 getMDOrNull(Record[2]), getMDString(Record[3]),
1319 Record[4], ExportSymbols)),
1324 case bitc::METADATA_MACRO: {
1325 if (Record.size() != 5)
1326 return error("Invalid record");
1328 IsDistinct = Record[0];
1329 MetadataList.assignValue(
1330 GET_OR_DISTINCT(DIMacro,
1331 (Context, Record[1], Record[2], getMDString(Record[3]),
1332 getMDString(Record[4]))),
1337 case bitc::METADATA_MACRO_FILE: {
1338 if (Record.size() != 5)
1339 return error("Invalid record");
1341 IsDistinct = Record[0];
1342 MetadataList.assignValue(
1343 GET_OR_DISTINCT(DIMacroFile,
1344 (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1345 getMDOrNull(Record[4]))),
1350 case bitc::METADATA_TEMPLATE_TYPE: {
1351 if (Record.size() != 3)
1352 return error("Invalid record");
1354 IsDistinct = Record[0];
1355 MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
1356 (Context, getMDString(Record[1]),
1357 getDITypeRefOrNull(Record[2]))),
1362 case bitc::METADATA_TEMPLATE_VALUE: {
1363 if (Record.size() != 5)
1364 return error("Invalid record");
1366 IsDistinct = Record[0];
1367 MetadataList.assignValue(
1368 GET_OR_DISTINCT(DITemplateValueParameter,
1369 (Context, Record[1], getMDString(Record[2]),
1370 getDITypeRefOrNull(Record[3]),
1371 getMDOrNull(Record[4]))),
1376 case bitc::METADATA_GLOBAL_VAR: {
1377 if (Record.size() < 11 || Record.size() > 12)
1378 return error("Invalid record");
1380 IsDistinct = Record[0] & 1;
1381 unsigned Version = Record[0] >> 1;
1384 MetadataList.assignValue(
1385 GET_OR_DISTINCT(DIGlobalVariable,
1386 (Context, getMDOrNull(Record[1]),
1387 getMDString(Record[2]), getMDString(Record[3]),
1388 getMDOrNull(Record[4]), Record[5],
1389 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1390 getMDOrNull(Record[10]), Record[11])),
1393 } else if (Version == 0) {
1394 // Upgrade old metadata, which stored a global variable reference or a
1395 // ConstantInt here.
1396 Metadata *Expr = getMDOrNull(Record[9]);
1397 uint32_t AlignInBits = 0;
1398 if (Record.size() > 11) {
1399 if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
1400 return error("Alignment value is too large");
1401 AlignInBits = Record[11];
1403 GlobalVariable *Attach = nullptr;
1404 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
1405 if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
1408 } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
1409 Expr = DIExpression::get(Context,
1410 {dwarf::DW_OP_constu, CI->getZExtValue(),
1411 dwarf::DW_OP_stack_value});
1416 DIGlobalVariable *DGV = GET_OR_DISTINCT(
1418 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1419 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1420 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1421 getMDOrNull(Record[10]), AlignInBits));
1423 auto *DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr);
1424 MetadataList.assignValue(DGVE, NextMetadataNo);
1427 Attach->addDebugInfo(DGVE);
1429 return error("Invalid record");
1433 case bitc::METADATA_LOCAL_VAR: {
1434 // 10th field is for the obseleted 'inlinedAt:' field.
1435 if (Record.size() < 8 || Record.size() > 10)
1436 return error("Invalid record");
1438 IsDistinct = Record[0] & 1;
1439 bool HasAlignment = Record[0] & 2;
1440 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
1441 // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
1442 // this is newer version of record which doesn't have artifical tag.
1443 bool HasTag = !HasAlignment && Record.size() > 8;
1444 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
1445 uint32_t AlignInBits = 0;
1447 if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max())
1448 return error("Alignment value is too large");
1449 AlignInBits = Record[8 + HasTag];
1451 MetadataList.assignValue(
1452 GET_OR_DISTINCT(DILocalVariable,
1453 (Context, getMDOrNull(Record[1 + HasTag]),
1454 getMDString(Record[2 + HasTag]),
1455 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
1456 getDITypeRefOrNull(Record[5 + HasTag]),
1457 Record[6 + HasTag], Flags, AlignInBits)),
1462 case bitc::METADATA_EXPRESSION: {
1463 if (Record.size() < 1)
1464 return error("Invalid record");
1466 IsDistinct = Record[0] & 1;
1467 bool HasOpFragment = Record[0] & 2;
1468 auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
1470 if (unsigned N = Elts.size())
1471 if (N >= 3 && Elts[N - 3] == dwarf::DW_OP_bit_piece)
1472 Elts[N - 3] = dwarf::DW_OP_LLVM_fragment;
1474 MetadataList.assignValue(
1475 GET_OR_DISTINCT(DIExpression, (Context, makeArrayRef(Record).slice(1))),
1480 case bitc::METADATA_GLOBAL_VAR_EXPR: {
1481 if (Record.size() != 3)
1482 return error("Invalid record");
1484 IsDistinct = Record[0];
1485 MetadataList.assignValue(GET_OR_DISTINCT(DIGlobalVariableExpression,
1486 (Context, getMDOrNull(Record[1]),
1487 getMDOrNull(Record[2]))),
1492 case bitc::METADATA_OBJC_PROPERTY: {
1493 if (Record.size() != 8)
1494 return error("Invalid record");
1496 IsDistinct = Record[0];
1497 MetadataList.assignValue(
1498 GET_OR_DISTINCT(DIObjCProperty,
1499 (Context, getMDString(Record[1]),
1500 getMDOrNull(Record[2]), Record[3],
1501 getMDString(Record[4]), getMDString(Record[5]),
1502 Record[6], getDITypeRefOrNull(Record[7]))),
1507 case bitc::METADATA_IMPORTED_ENTITY: {
1508 if (Record.size() != 6)
1509 return error("Invalid record");
1511 IsDistinct = Record[0];
1512 MetadataList.assignValue(
1513 GET_OR_DISTINCT(DIImportedEntity,
1514 (Context, Record[1], getMDOrNull(Record[2]),
1515 getDITypeRefOrNull(Record[3]), Record[4],
1516 getMDString(Record[5]))),
1521 case bitc::METADATA_STRING_OLD: {
1522 std::string String(Record.begin(), Record.end());
1524 // Test for upgrading !llvm.loop.
1525 HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
1526 ++NumMDStringLoaded;
1527 Metadata *MD = MDString::get(Context, String);
1528 MetadataList.assignValue(MD, NextMetadataNo);
1532 case bitc::METADATA_STRINGS: {
1533 auto CreateNextMDString = [&](StringRef Str) {
1534 ++NumMDStringLoaded;
1535 MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
1538 if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
1542 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
1543 if (Record.size() % 2 == 0)
1544 return error("Invalid record");
1545 unsigned ValueID = Record[0];
1546 if (ValueID >= ValueList.size())
1547 return error("Invalid record");
1548 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
1549 if (Error Err = parseGlobalObjectAttachment(
1550 *GO, ArrayRef<uint64_t>(Record).slice(1)))
1554 case bitc::METADATA_KIND: {
1555 // Support older bitcode files that had METADATA_KIND records in a
1556 // block with METADATA_BLOCK_ID.
1557 if (Error Err = parseMetadataKindRecord(Record))
1562 return Error::success();
1563 #undef GET_OR_DISTINCT
1566 Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
1567 ArrayRef<uint64_t> Record, StringRef Blob,
1568 std::function<void(StringRef)> CallBack) {
1569 // All the MDStrings in the block are emitted together in a single
1570 // record. The strings are concatenated and stored in a blob along with
1572 if (Record.size() != 2)
1573 return error("Invalid record: metadata strings layout");
1575 unsigned NumStrings = Record[0];
1576 unsigned StringsOffset = Record[1];
1578 return error("Invalid record: metadata strings with no strings");
1579 if (StringsOffset > Blob.size())
1580 return error("Invalid record: metadata strings corrupt offset");
1582 StringRef Lengths = Blob.slice(0, StringsOffset);
1583 SimpleBitstreamCursor R(Lengths);
1585 StringRef Strings = Blob.drop_front(StringsOffset);
1587 if (R.AtEndOfStream())
1588 return error("Invalid record: metadata strings bad length");
1590 unsigned Size = R.ReadVBR(6);
1591 if (Strings.size() < Size)
1592 return error("Invalid record: metadata strings truncated chars");
1594 CallBack(Strings.slice(0, Size));
1595 Strings = Strings.drop_front(Size);
1596 } while (--NumStrings);
1598 return Error::success();
1601 Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
1602 GlobalObject &GO, ArrayRef<uint64_t> Record) {
1603 assert(Record.size() % 2 == 0);
1604 for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
1605 auto K = MDKindMap.find(Record[I]);
1606 if (K == MDKindMap.end())
1607 return error("Invalid ID");
1608 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
1610 return error("Invalid metadata attachment");
1611 GO.addMetadata(K->second, *MD);
1613 return Error::success();
1616 /// Parse metadata attachments.
1617 Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
1618 Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1619 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
1620 return error("Invalid record");
1622 SmallVector<uint64_t, 64> Record;
1623 PlaceholderQueue Placeholders;
1626 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1628 switch (Entry.Kind) {
1629 case BitstreamEntry::SubBlock: // Handled for us already.
1630 case BitstreamEntry::Error:
1631 return error("Malformed block");
1632 case BitstreamEntry::EndBlock:
1633 resolveForwardRefsAndPlaceholders(Placeholders);
1634 return Error::success();
1635 case BitstreamEntry::Record:
1636 // The interesting case.
1640 // Read a metadata attachment record.
1642 ++NumMDRecordLoaded;
1643 switch (Stream.readRecord(Entry.ID, Record)) {
1644 default: // Default behavior: ignore.
1646 case bitc::METADATA_ATTACHMENT: {
1647 unsigned RecordLength = Record.size();
1649 return error("Invalid record");
1650 if (RecordLength % 2 == 0) {
1651 // A function attachment.
1652 if (Error Err = parseGlobalObjectAttachment(F, Record))
1657 // An instruction attachment.
1658 Instruction *Inst = InstructionList[Record[0]];
1659 for (unsigned i = 1; i != RecordLength; i = i + 2) {
1660 unsigned Kind = Record[i];
1661 DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
1662 if (I == MDKindMap.end())
1663 return error("Invalid ID");
1664 if (I->second == LLVMContext::MD_tbaa && StripTBAA)
1667 auto Idx = Record[i + 1];
1668 if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
1669 !MetadataList.lookup(Idx)) {
1670 // Load the attachment if it is in the lazy-loadable range and hasn't
1672 lazyLoadOneMetadata(Idx, Placeholders);
1673 resolveForwardRefsAndPlaceholders(Placeholders);
1676 Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
1677 if (isa<LocalAsMetadata>(Node))
1678 // Drop the attachment. This used to be legal, but there's no
1681 MDNode *MD = dyn_cast_or_null<MDNode>(Node);
1683 return error("Invalid metadata attachment");
1685 if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
1686 MD = upgradeInstructionLoopAttachment(*MD);
1688 if (I->second == LLVMContext::MD_tbaa) {
1689 assert(!MD->isTemporary() && "should load MDs before attachments");
1690 MD = UpgradeTBAANode(*MD);
1692 Inst->setMetadata(I->second, MD);
1700 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1701 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
1702 SmallVectorImpl<uint64_t> &Record) {
1703 if (Record.size() < 2)
1704 return error("Invalid record");
1706 unsigned Kind = Record[0];
1707 SmallString<8> Name(Record.begin() + 1, Record.end());
1709 unsigned NewKind = TheModule.getMDKindID(Name.str());
1710 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1711 return error("Conflicting METADATA_KIND records");
1712 return Error::success();
1715 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
1716 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
1717 if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
1718 return error("Invalid record");
1720 SmallVector<uint64_t, 64> Record;
1722 // Read all the records.
1724 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1726 switch (Entry.Kind) {
1727 case BitstreamEntry::SubBlock: // Handled for us already.
1728 case BitstreamEntry::Error:
1729 return error("Malformed block");
1730 case BitstreamEntry::EndBlock:
1731 return Error::success();
1732 case BitstreamEntry::Record:
1733 // The interesting case.
1739 ++NumMDRecordLoaded;
1740 unsigned Code = Stream.readRecord(Entry.ID, Record);
1742 default: // Default behavior: ignore.
1744 case bitc::METADATA_KIND: {
1745 if (Error Err = parseMetadataKindRecord(Record))
1753 MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
1754 Pimpl = std::move(RHS.Pimpl);
1757 MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
1758 : Pimpl(std::move(RHS.Pimpl)) {}
1760 MetadataLoader::~MetadataLoader() = default;
1761 MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
1762 BitcodeReaderValueList &ValueList,
1764 std::function<Type *(unsigned)> getTypeByID)
1765 : Pimpl(llvm::make_unique<MetadataLoaderImpl>(Stream, TheModule, ValueList,
1766 getTypeByID, IsImporting)) {}
1768 Error MetadataLoader::parseMetadata(bool ModuleLevel) {
1769 return Pimpl->parseMetadata(ModuleLevel);
1772 bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
1774 /// Return the given metadata, creating a replaceable forward reference if
1776 Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
1777 return Pimpl->getMetadataFwdRefOrLoad(Idx);
1780 MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) {
1781 return Pimpl->getMDNodeFwdRefOrNull(Idx);
1784 DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
1785 return Pimpl->lookupSubprogramForFunction(F);
1788 Error MetadataLoader::parseMetadataAttachment(
1789 Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1790 return Pimpl->parseMetadataAttachment(F, InstructionList);
1793 Error MetadataLoader::parseMetadataKinds() {
1794 return Pimpl->parseMetadataKinds();
1797 void MetadataLoader::setStripTBAA(bool StripTBAA) {
1798 return Pimpl->setStripTBAA(StripTBAA);
1801 bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
1803 unsigned MetadataLoader::size() const { return Pimpl->size(); }
1804 void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }