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