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