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