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