Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1 | //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MetadataLoader.h" |
| 10 | #include "ValueList.h" |
| 11 | |
| 12 | #include "llvm/ADT/APFloat.h" |
| 13 | #include "llvm/ADT/APInt.h" |
| 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/None.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/Bitcode/BitcodeReader.h" |
Francis Visoiu Mistrih | e030827 | 2019-07-03 22:40:07 +0000 | [diff] [blame] | 25 | #include "llvm/Bitstream/BitstreamReader.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 26 | #include "llvm/Bitcode/LLVMBitCodes.h" |
| 27 | #include "llvm/IR/Argument.h" |
| 28 | #include "llvm/IR/Attributes.h" |
| 29 | #include "llvm/IR/AutoUpgrade.h" |
| 30 | #include "llvm/IR/BasicBlock.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CallingConv.h" |
| 32 | #include "llvm/IR/Comdat.h" |
| 33 | #include "llvm/IR/Constant.h" |
| 34 | #include "llvm/IR/Constants.h" |
| 35 | #include "llvm/IR/DebugInfo.h" |
| 36 | #include "llvm/IR/DebugInfoMetadata.h" |
| 37 | #include "llvm/IR/DebugLoc.h" |
| 38 | #include "llvm/IR/DerivedTypes.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 39 | #include "llvm/IR/DiagnosticPrinter.h" |
| 40 | #include "llvm/IR/Function.h" |
| 41 | #include "llvm/IR/GVMaterializer.h" |
| 42 | #include "llvm/IR/GlobalAlias.h" |
| 43 | #include "llvm/IR/GlobalIFunc.h" |
| 44 | #include "llvm/IR/GlobalIndirectSymbol.h" |
| 45 | #include "llvm/IR/GlobalObject.h" |
| 46 | #include "llvm/IR/GlobalValue.h" |
| 47 | #include "llvm/IR/GlobalVariable.h" |
| 48 | #include "llvm/IR/InlineAsm.h" |
| 49 | #include "llvm/IR/InstrTypes.h" |
| 50 | #include "llvm/IR/Instruction.h" |
| 51 | #include "llvm/IR/Instructions.h" |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 52 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Intrinsics.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 54 | #include "llvm/IR/LLVMContext.h" |
| 55 | #include "llvm/IR/Module.h" |
| 56 | #include "llvm/IR/ModuleSummaryIndex.h" |
| 57 | #include "llvm/IR/OperandTraits.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 58 | #include "llvm/IR/TrackingMDRef.h" |
| 59 | #include "llvm/IR/Type.h" |
| 60 | #include "llvm/IR/ValueHandle.h" |
| 61 | #include "llvm/Support/AtomicOrdering.h" |
| 62 | #include "llvm/Support/Casting.h" |
| 63 | #include "llvm/Support/CommandLine.h" |
| 64 | #include "llvm/Support/Compiler.h" |
| 65 | #include "llvm/Support/Debug.h" |
| 66 | #include "llvm/Support/Error.h" |
| 67 | #include "llvm/Support/ErrorHandling.h" |
| 68 | #include "llvm/Support/ManagedStatic.h" |
| 69 | #include "llvm/Support/MemoryBuffer.h" |
| 70 | #include "llvm/Support/raw_ostream.h" |
| 71 | #include <algorithm> |
| 72 | #include <cassert> |
| 73 | #include <cstddef> |
| 74 | #include <cstdint> |
| 75 | #include <deque> |
| 76 | #include <limits> |
| 77 | #include <map> |
| 78 | #include <memory> |
| 79 | #include <string> |
| 80 | #include <system_error> |
| 81 | #include <tuple> |
| 82 | #include <utility> |
| 83 | #include <vector> |
| 84 | |
| 85 | using namespace llvm; |
| 86 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 87 | #define DEBUG_TYPE "bitcode-reader" |
| 88 | |
| 89 | STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded"); |
| 90 | STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created"); |
| 91 | STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded"); |
| 92 | |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 93 | /// Flag whether we need to import full type definitions for ThinLTO. |
| 94 | /// Currently needed for Darwin and LLDB. |
| 95 | static cl::opt<bool> ImportFullTypeDefinitions( |
| 96 | "import-full-type-definitions", cl::init(false), cl::Hidden, |
| 97 | cl::desc("Import full type definitions for ThinLTO.")); |
| 98 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 99 | static cl::opt<bool> DisableLazyLoading( |
| 100 | "disable-ondemand-mds-loading", cl::init(false), cl::Hidden, |
| 101 | cl::desc("Force disable the lazy-loading on-demand of metadata when " |
| 102 | "loading bitcode for importing.")); |
| 103 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 104 | namespace { |
| 105 | |
Simon Pilgrim | aa49be4 | 2019-05-03 13:50:38 +0000 | [diff] [blame] | 106 | static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 107 | |
| 108 | class BitcodeReaderMetadataList { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 109 | /// Array of metadata references. |
| 110 | /// |
| 111 | /// Don't use std::vector here. Some versions of libc++ copy (instead of |
| 112 | /// move) on resize, and TrackingMDRef is very expensive to copy. |
| 113 | SmallVector<TrackingMDRef, 1> MetadataPtrs; |
| 114 | |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 115 | /// The set of indices in MetadataPtrs above of forward references that were |
| 116 | /// generated. |
| 117 | SmallDenseSet<unsigned, 1> ForwardReference; |
| 118 | |
| 119 | /// The set of indices in MetadataPtrs above of Metadata that need to be |
| 120 | /// resolved. |
| 121 | SmallDenseSet<unsigned, 1> UnresolvedNodes; |
| 122 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 123 | /// Structures for resolving old type refs. |
| 124 | struct { |
| 125 | SmallDenseMap<MDString *, TempMDTuple, 1> Unknown; |
| 126 | SmallDenseMap<MDString *, DICompositeType *, 1> Final; |
| 127 | SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls; |
| 128 | SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays; |
| 129 | } OldTypeRefs; |
| 130 | |
| 131 | LLVMContext &Context; |
| 132 | |
Florian Hahn | 864474c | 2019-07-14 12:35:50 +0000 | [diff] [blame] | 133 | /// Maximum number of valid references. Forward references exceeding the |
| 134 | /// maximum must be invalid. |
| 135 | unsigned RefsUpperBound; |
| 136 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 137 | public: |
Florian Hahn | 864474c | 2019-07-14 12:35:50 +0000 | [diff] [blame] | 138 | BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound) |
| 139 | : Context(C), |
| 140 | RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(), |
| 141 | RefsUpperBound)) {} |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 142 | |
| 143 | // vector compatibility methods |
| 144 | unsigned size() const { return MetadataPtrs.size(); } |
| 145 | void resize(unsigned N) { MetadataPtrs.resize(N); } |
| 146 | void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); } |
| 147 | void clear() { MetadataPtrs.clear(); } |
| 148 | Metadata *back() const { return MetadataPtrs.back(); } |
| 149 | void pop_back() { MetadataPtrs.pop_back(); } |
| 150 | bool empty() const { return MetadataPtrs.empty(); } |
| 151 | |
| 152 | Metadata *operator[](unsigned i) const { |
| 153 | assert(i < MetadataPtrs.size()); |
| 154 | return MetadataPtrs[i]; |
| 155 | } |
| 156 | |
| 157 | Metadata *lookup(unsigned I) const { |
| 158 | if (I < MetadataPtrs.size()) |
| 159 | return MetadataPtrs[I]; |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
| 163 | void shrinkTo(unsigned N) { |
| 164 | assert(N <= size() && "Invalid shrinkTo request!"); |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 165 | assert(ForwardReference.empty() && "Unexpected forward refs"); |
| 166 | assert(UnresolvedNodes.empty() && "Unexpected unresolved node"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 167 | MetadataPtrs.resize(N); |
| 168 | } |
| 169 | |
| 170 | /// Return the given metadata, creating a replaceable forward reference if |
| 171 | /// necessary. |
| 172 | Metadata *getMetadataFwdRef(unsigned Idx); |
| 173 | |
Hiroshi Inoue | 8f976ba | 2018-01-17 12:29:38 +0000 | [diff] [blame] | 174 | /// Return the given metadata only if it is fully resolved. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 175 | /// |
| 176 | /// Gives the same result as \a lookup(), unless \a MDNode::isResolved() |
| 177 | /// would give \c false. |
| 178 | Metadata *getMetadataIfResolved(unsigned Idx); |
| 179 | |
| 180 | MDNode *getMDNodeFwdRefOrNull(unsigned Idx); |
| 181 | void assignValue(Metadata *MD, unsigned Idx); |
| 182 | void tryToResolveCycles(); |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 183 | bool hasFwdRefs() const { return !ForwardReference.empty(); } |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 184 | int getNextFwdRef() { |
| 185 | assert(hasFwdRefs()); |
| 186 | return *ForwardReference.begin(); |
| 187 | } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 188 | |
| 189 | /// Upgrade a type that had an MDString reference. |
| 190 | void addTypeRef(MDString &UUID, DICompositeType &CT); |
| 191 | |
| 192 | /// Upgrade a type that had an MDString reference. |
| 193 | Metadata *upgradeTypeRef(Metadata *MaybeUUID); |
| 194 | |
| 195 | /// Upgrade a type ref array that may have MDString references. |
| 196 | Metadata *upgradeTypeRefArray(Metadata *MaybeTuple); |
| 197 | |
| 198 | private: |
| 199 | Metadata *resolveTypeRefArray(Metadata *MaybeTuple); |
| 200 | }; |
| 201 | |
| 202 | void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) { |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 203 | if (auto *MDN = dyn_cast<MDNode>(MD)) |
| 204 | if (!MDN->isResolved()) |
| 205 | UnresolvedNodes.insert(Idx); |
| 206 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 207 | if (Idx == size()) { |
| 208 | push_back(MD); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (Idx >= size()) |
| 213 | resize(Idx + 1); |
| 214 | |
| 215 | TrackingMDRef &OldMD = MetadataPtrs[Idx]; |
| 216 | if (!OldMD) { |
| 217 | OldMD.reset(MD); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | // If there was a forward reference to this value, replace it. |
| 222 | TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); |
| 223 | PrevMD->replaceAllUsesWith(MD); |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 224 | ForwardReference.erase(Idx); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) { |
Florian Hahn | 864474c | 2019-07-14 12:35:50 +0000 | [diff] [blame] | 228 | // Bail out for a clearly invalid value. |
| 229 | if (Idx >= RefsUpperBound) |
| 230 | return nullptr; |
| 231 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 232 | if (Idx >= size()) |
| 233 | resize(Idx + 1); |
| 234 | |
| 235 | if (Metadata *MD = MetadataPtrs[Idx]) |
| 236 | return MD; |
| 237 | |
| 238 | // Track forward refs to be resolved later. |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 239 | ForwardReference.insert(Idx); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 240 | |
| 241 | // Create and return a placeholder, which will later be RAUW'd. |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 242 | ++NumMDNodeTemporary; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 243 | Metadata *MD = MDNode::getTemporary(Context, None).release(); |
| 244 | MetadataPtrs[Idx].reset(MD); |
| 245 | return MD; |
| 246 | } |
| 247 | |
| 248 | Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) { |
| 249 | Metadata *MD = lookup(Idx); |
| 250 | if (auto *N = dyn_cast_or_null<MDNode>(MD)) |
| 251 | if (!N->isResolved()) |
| 252 | return nullptr; |
| 253 | return MD; |
| 254 | } |
| 255 | |
| 256 | MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) { |
| 257 | return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx)); |
| 258 | } |
| 259 | |
| 260 | void BitcodeReaderMetadataList::tryToResolveCycles() { |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 261 | if (!ForwardReference.empty()) |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 262 | // Still forward references... can't resolve cycles. |
| 263 | return; |
| 264 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 265 | // Give up on finding a full definition for any forward decls that remain. |
| 266 | for (const auto &Ref : OldTypeRefs.FwdDecls) |
| 267 | OldTypeRefs.Final.insert(Ref); |
| 268 | OldTypeRefs.FwdDecls.clear(); |
| 269 | |
| 270 | // Upgrade from old type ref arrays. In strange cases, this could add to |
| 271 | // OldTypeRefs.Unknown. |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 272 | for (const auto &Array : OldTypeRefs.Arrays) |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 273 | Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get())); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 274 | OldTypeRefs.Arrays.clear(); |
| 275 | |
| 276 | // Replace old string-based type refs with the resolved node, if possible. |
| 277 | // If we haven't seen the node, leave it to the verifier to complain about |
| 278 | // the invalid string reference. |
| 279 | for (const auto &Ref : OldTypeRefs.Unknown) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 280 | if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first)) |
| 281 | Ref.second->replaceAllUsesWith(CT); |
| 282 | else |
| 283 | Ref.second->replaceAllUsesWith(Ref.first); |
| 284 | } |
| 285 | OldTypeRefs.Unknown.clear(); |
| 286 | |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 287 | if (UnresolvedNodes.empty()) |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 288 | // Nothing to do. |
| 289 | return; |
| 290 | |
| 291 | // Resolve any cycles. |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 292 | for (unsigned I : UnresolvedNodes) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 293 | auto &MD = MetadataPtrs[I]; |
| 294 | auto *N = dyn_cast_or_null<MDNode>(MD); |
| 295 | if (!N) |
| 296 | continue; |
| 297 | |
| 298 | assert(!N->isTemporary() && "Unexpected forward reference"); |
| 299 | N->resolveCycles(); |
| 300 | } |
| 301 | |
Mehdi Amini | 690952d | 2016-12-25 04:22:54 +0000 | [diff] [blame] | 302 | // Make sure we return early again until there's another unresolved ref. |
| 303 | UnresolvedNodes.clear(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void BitcodeReaderMetadataList::addTypeRef(MDString &UUID, |
| 307 | DICompositeType &CT) { |
| 308 | assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID"); |
| 309 | if (CT.isForwardDecl()) |
| 310 | OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT)); |
| 311 | else |
| 312 | OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT)); |
| 313 | } |
| 314 | |
| 315 | Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) { |
| 316 | auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID); |
| 317 | if (LLVM_LIKELY(!UUID)) |
| 318 | return MaybeUUID; |
| 319 | |
| 320 | if (auto *CT = OldTypeRefs.Final.lookup(UUID)) |
| 321 | return CT; |
| 322 | |
| 323 | auto &Ref = OldTypeRefs.Unknown[UUID]; |
| 324 | if (!Ref) |
| 325 | Ref = MDNode::getTemporary(Context, None); |
| 326 | return Ref.get(); |
| 327 | } |
| 328 | |
| 329 | Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) { |
| 330 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); |
| 331 | if (!Tuple || Tuple->isDistinct()) |
| 332 | return MaybeTuple; |
| 333 | |
| 334 | // Look through the array immediately if possible. |
| 335 | if (!Tuple->isTemporary()) |
| 336 | return resolveTypeRefArray(Tuple); |
| 337 | |
| 338 | // Create and return a placeholder to use for now. Eventually |
| 339 | // resolveTypeRefArrays() will be resolve this forward reference. |
| 340 | OldTypeRefs.Arrays.emplace_back( |
| 341 | std::piecewise_construct, std::forward_as_tuple(Tuple), |
| 342 | std::forward_as_tuple(MDTuple::getTemporary(Context, None))); |
| 343 | return OldTypeRefs.Arrays.back().second.get(); |
| 344 | } |
| 345 | |
| 346 | Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) { |
| 347 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); |
| 348 | if (!Tuple || Tuple->isDistinct()) |
| 349 | return MaybeTuple; |
| 350 | |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 351 | // Look through the DITypeRefArray, upgrading each DIType *. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 352 | SmallVector<Metadata *, 32> Ops; |
| 353 | Ops.reserve(Tuple->getNumOperands()); |
| 354 | for (Metadata *MD : Tuple->operands()) |
| 355 | Ops.push_back(upgradeTypeRef(MD)); |
| 356 | |
| 357 | return MDTuple::get(Context, Ops); |
| 358 | } |
| 359 | |
| 360 | namespace { |
| 361 | |
| 362 | class PlaceholderQueue { |
| 363 | // Placeholders would thrash around when moved, so store in a std::deque |
| 364 | // instead of some sort of vector. |
| 365 | std::deque<DistinctMDOperandPlaceholder> PHs; |
| 366 | |
| 367 | public: |
Mehdi Amini | 2737989 | 2017-01-20 10:18:32 +0000 | [diff] [blame] | 368 | ~PlaceholderQueue() { |
| 369 | assert(empty() && "PlaceholderQueue hasn't been flushed before being destroyed"); |
| 370 | } |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 371 | bool empty() { return PHs.empty(); } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 372 | DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID); |
| 373 | void flush(BitcodeReaderMetadataList &MetadataList); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 374 | |
| 375 | /// Return the list of temporaries nodes in the queue, these need to be |
| 376 | /// loaded before we can flush the queue. |
| 377 | void getTemporaries(BitcodeReaderMetadataList &MetadataList, |
| 378 | DenseSet<unsigned> &Temporaries) { |
| 379 | for (auto &PH : PHs) { |
| 380 | auto ID = PH.getID(); |
| 381 | auto *MD = MetadataList.lookup(ID); |
| 382 | if (!MD) { |
| 383 | Temporaries.insert(ID); |
| 384 | continue; |
| 385 | } |
| 386 | auto *N = dyn_cast_or_null<MDNode>(MD); |
| 387 | if (N && N->isTemporary()) |
| 388 | Temporaries.insert(ID); |
| 389 | } |
| 390 | } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | } // end anonymous namespace |
| 394 | |
| 395 | DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) { |
| 396 | PHs.emplace_back(ID); |
| 397 | return PHs.back(); |
| 398 | } |
| 399 | |
| 400 | void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) { |
| 401 | while (!PHs.empty()) { |
Mehdi Amini | 4f90ee0 | 2016-12-25 03:55:53 +0000 | [diff] [blame] | 402 | auto *MD = MetadataList.lookup(PHs.front().getID()); |
| 403 | assert(MD && "Flushing placeholder on unassigned MD"); |
Mehdi Amini | 5ae6170 | 2016-12-23 02:20:09 +0000 | [diff] [blame] | 404 | #ifndef NDEBUG |
Mehdi Amini | 4f90ee0 | 2016-12-25 03:55:53 +0000 | [diff] [blame] | 405 | if (auto *MDN = dyn_cast<MDNode>(MD)) |
Mehdi Amini | 5ae6170 | 2016-12-23 02:20:09 +0000 | [diff] [blame] | 406 | assert(MDN->isResolved() && |
| 407 | "Flushing Placeholder while cycles aren't resolved"); |
Mehdi Amini | 5ae6170 | 2016-12-23 02:20:09 +0000 | [diff] [blame] | 408 | #endif |
| 409 | PHs.front().replaceUseWith(MD); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 410 | PHs.pop_front(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | } // anonynous namespace |
| 415 | |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 416 | static Error error(const Twine &Message) { |
| 417 | return make_error<StringError>( |
| 418 | Message, make_error_code(BitcodeError::CorruptedBitcode)); |
| 419 | } |
| 420 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 421 | class MetadataLoader::MetadataLoaderImpl { |
| 422 | BitcodeReaderMetadataList MetadataList; |
| 423 | BitcodeReaderValueList &ValueList; |
| 424 | BitstreamCursor &Stream; |
| 425 | LLVMContext &Context; |
| 426 | Module &TheModule; |
| 427 | std::function<Type *(unsigned)> getTypeByID; |
| 428 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 429 | /// Cursor associated with the lazy-loading of Metadata. This is the easy way |
| 430 | /// to keep around the right "context" (Abbrev list) to be able to jump in |
| 431 | /// the middle of the metadata block and load any record. |
| 432 | BitstreamCursor IndexCursor; |
| 433 | |
| 434 | /// Index that keeps track of MDString values. |
| 435 | std::vector<StringRef> MDStringRef; |
| 436 | |
| 437 | /// On-demand loading of a single MDString. Requires the index above to be |
| 438 | /// populated. |
| 439 | MDString *lazyLoadOneMDString(unsigned Idx); |
| 440 | |
| 441 | /// Index that keeps track of where to find a metadata record in the stream. |
| 442 | std::vector<uint64_t> GlobalMetadataBitPosIndex; |
| 443 | |
| 444 | /// Populate the index above to enable lazily loading of metadata, and load |
| 445 | /// the named metadata as well as the transitively referenced global |
| 446 | /// Metadata. |
Mehdi Amini | 42ef199 | 2017-01-07 18:31:38 +0000 | [diff] [blame] | 447 | Expected<bool> lazyLoadModuleMetadataBlock(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 448 | |
| 449 | /// On-demand loading of a single metadata. Requires the index above to be |
| 450 | /// populated. |
| 451 | void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders); |
| 452 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 453 | // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to |
| 454 | // point from SP to CU after a block is completly parsed. |
| 455 | std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms; |
| 456 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 457 | /// Functions that need to be matched with subprograms when upgrading old |
| 458 | /// metadata. |
| 459 | SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs; |
| 460 | |
| 461 | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. |
| 462 | DenseMap<unsigned, unsigned> MDKindMap; |
| 463 | |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 464 | bool StripTBAA = false; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 465 | bool HasSeenOldLoopTags = false; |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 466 | bool NeedUpgradeToDIGlobalVariableExpression = false; |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 467 | bool NeedDeclareExpressionUpgrade = false; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 468 | |
Mehdi Amini | ec68dd4 | 2016-12-23 02:20:02 +0000 | [diff] [blame] | 469 | /// True if metadata is being parsed for a module being ThinLTO imported. |
| 470 | bool IsImporting = false; |
| 471 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 472 | Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code, |
| 473 | PlaceholderQueue &Placeholders, StringRef Blob, |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 474 | unsigned &NextMetadataNo); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 475 | Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob, |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 476 | function_ref<void(StringRef)> CallBack); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 477 | Error parseGlobalObjectAttachment(GlobalObject &GO, |
| 478 | ArrayRef<uint64_t> Record); |
| 479 | Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record); |
| 480 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 481 | void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders); |
| 482 | |
| 483 | /// Upgrade old-style CU <-> SP pointers to point from SP to CU. |
| 484 | void upgradeCUSubprograms() { |
| 485 | for (auto CU_SP : CUSubprograms) |
| 486 | if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second)) |
| 487 | for (auto &Op : SPs->operands()) |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 488 | if (auto *SP = dyn_cast_or_null<DISubprogram>(Op)) |
| 489 | SP->replaceUnit(CU_SP.first); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 490 | CUSubprograms.clear(); |
| 491 | } |
| 492 | |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 493 | /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions. |
| 494 | void upgradeCUVariables() { |
| 495 | if (!NeedUpgradeToDIGlobalVariableExpression) |
| 496 | return; |
| 497 | |
| 498 | // Upgrade list of variables attached to the CUs. |
| 499 | if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) |
| 500 | for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) { |
| 501 | auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I)); |
| 502 | if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables())) |
| 503 | for (unsigned I = 0; I < GVs->getNumOperands(); I++) |
| 504 | if (auto *GV = |
| 505 | dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) { |
Adrian Prantl | 0578221 | 2017-08-30 18:06:51 +0000 | [diff] [blame] | 506 | auto *DGVE = DIGlobalVariableExpression::getDistinct( |
| 507 | Context, GV, DIExpression::get(Context, {})); |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 508 | GVs->replaceOperandWith(I, DGVE); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Upgrade variables attached to globals. |
| 513 | for (auto &GV : TheModule.globals()) { |
Davide Italiano | 56a08b4 | 2017-05-16 18:41:46 +0000 | [diff] [blame] | 514 | SmallVector<MDNode *, 1> MDs; |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 515 | GV.getMetadata(LLVMContext::MD_dbg, MDs); |
| 516 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 517 | for (auto *MD : MDs) |
Simon Pilgrim | 2bb217b | 2019-10-02 11:48:17 +0000 | [diff] [blame] | 518 | if (auto *DGV = dyn_cast<DIGlobalVariable>(MD)) { |
Adrian Prantl | 0578221 | 2017-08-30 18:06:51 +0000 | [diff] [blame] | 519 | auto *DGVE = DIGlobalVariableExpression::getDistinct( |
| 520 | Context, DGV, DIExpression::get(Context, {})); |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 521 | GV.addMetadata(LLVMContext::MD_dbg, *DGVE); |
| 522 | } else |
| 523 | GV.addMetadata(LLVMContext::MD_dbg, *MD); |
| 524 | } |
| 525 | } |
| 526 | |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 527 | /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that |
| 528 | /// describes a function argument. |
| 529 | void upgradeDeclareExpressions(Function &F) { |
| 530 | if (!NeedDeclareExpressionUpgrade) |
| 531 | return; |
| 532 | |
| 533 | for (auto &BB : F) |
| 534 | for (auto &I : BB) |
| 535 | if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) |
| 536 | if (auto *DIExpr = DDI->getExpression()) |
| 537 | if (DIExpr->startsWithDeref() && |
| 538 | dyn_cast_or_null<Argument>(DDI->getAddress())) { |
| 539 | SmallVector<uint64_t, 8> Ops; |
| 540 | Ops.append(std::next(DIExpr->elements_begin()), |
| 541 | DIExpr->elements_end()); |
| 542 | auto *E = DIExpression::get(Context, Ops); |
| 543 | DDI->setOperand(2, MetadataAsValue::get(Context, E)); |
| 544 | } |
| 545 | } |
| 546 | |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 547 | /// Upgrade the expression from previous versions. |
| 548 | Error upgradeDIExpression(uint64_t FromVersion, |
| 549 | MutableArrayRef<uint64_t> &Expr, |
| 550 | SmallVectorImpl<uint64_t> &Buffer) { |
| 551 | auto N = Expr.size(); |
| 552 | switch (FromVersion) { |
| 553 | default: |
| 554 | return error("Invalid record"); |
| 555 | case 0: |
| 556 | if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece) |
| 557 | Expr[N - 3] = dwarf::DW_OP_LLVM_fragment; |
| 558 | LLVM_FALLTHROUGH; |
| 559 | case 1: |
| 560 | // Move DW_OP_deref to the end. |
| 561 | if (N && Expr[0] == dwarf::DW_OP_deref) { |
| 562 | auto End = Expr.end(); |
| 563 | if (Expr.size() >= 3 && |
| 564 | *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment) |
| 565 | End = std::prev(End, 3); |
| 566 | std::move(std::next(Expr.begin()), End, Expr.begin()); |
| 567 | *std::prev(End) = dwarf::DW_OP_deref; |
| 568 | } |
| 569 | NeedDeclareExpressionUpgrade = true; |
| 570 | LLVM_FALLTHROUGH; |
| 571 | case 2: { |
| 572 | // Change DW_OP_plus to DW_OP_plus_uconst. |
| 573 | // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus |
| 574 | auto SubExpr = ArrayRef<uint64_t>(Expr); |
| 575 | while (!SubExpr.empty()) { |
| 576 | // Skip past other operators with their operands |
| 577 | // for this version of the IR, obtained from |
| 578 | // from historic DIExpression::ExprOperand::getSize(). |
| 579 | size_t HistoricSize; |
| 580 | switch (SubExpr.front()) { |
| 581 | default: |
| 582 | HistoricSize = 1; |
| 583 | break; |
| 584 | case dwarf::DW_OP_constu: |
| 585 | case dwarf::DW_OP_minus: |
| 586 | case dwarf::DW_OP_plus: |
| 587 | HistoricSize = 2; |
| 588 | break; |
| 589 | case dwarf::DW_OP_LLVM_fragment: |
| 590 | HistoricSize = 3; |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | // If the expression is malformed, make sure we don't |
| 595 | // copy more elements than we should. |
| 596 | HistoricSize = std::min(SubExpr.size(), HistoricSize); |
| 597 | ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize-1); |
| 598 | |
| 599 | switch (SubExpr.front()) { |
| 600 | case dwarf::DW_OP_plus: |
| 601 | Buffer.push_back(dwarf::DW_OP_plus_uconst); |
| 602 | Buffer.append(Args.begin(), Args.end()); |
| 603 | break; |
| 604 | case dwarf::DW_OP_minus: |
| 605 | Buffer.push_back(dwarf::DW_OP_constu); |
| 606 | Buffer.append(Args.begin(), Args.end()); |
| 607 | Buffer.push_back(dwarf::DW_OP_minus); |
| 608 | break; |
| 609 | default: |
| 610 | Buffer.push_back(*SubExpr.begin()); |
| 611 | Buffer.append(Args.begin(), Args.end()); |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | // Continue with remaining elements. |
| 616 | SubExpr = SubExpr.slice(HistoricSize); |
| 617 | } |
| 618 | Expr = MutableArrayRef<uint64_t>(Buffer); |
| 619 | LLVM_FALLTHROUGH; |
| 620 | } |
| 621 | case 3: |
| 622 | // Up-to-date! |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | return Error::success(); |
| 627 | } |
| 628 | |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 629 | void upgradeDebugInfo() { |
| 630 | upgradeCUSubprograms(); |
| 631 | upgradeCUVariables(); |
| 632 | } |
| 633 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 634 | public: |
| 635 | MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule, |
| 636 | BitcodeReaderValueList &ValueList, |
Mehdi Amini | ec68dd4 | 2016-12-23 02:20:02 +0000 | [diff] [blame] | 637 | std::function<Type *(unsigned)> getTypeByID, |
| 638 | bool IsImporting) |
Florian Hahn | 864474c | 2019-07-14 12:35:50 +0000 | [diff] [blame] | 639 | : MetadataList(TheModule.getContext(), Stream.SizeInBytes()), |
| 640 | ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()), |
| 641 | TheModule(TheModule), getTypeByID(std::move(getTypeByID)), |
| 642 | IsImporting(IsImporting) {} |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 643 | |
Mehdi Amini | ec68dd4 | 2016-12-23 02:20:02 +0000 | [diff] [blame] | 644 | Error parseMetadata(bool ModuleLevel); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 645 | |
| 646 | bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); } |
Mehdi Amini | 3bb4d01 | 2017-01-20 20:29:16 +0000 | [diff] [blame] | 647 | |
| 648 | Metadata *getMetadataFwdRefOrLoad(unsigned ID) { |
| 649 | if (ID < MDStringRef.size()) |
| 650 | return lazyLoadOneMDString(ID); |
| 651 | if (auto *MD = MetadataList.lookup(ID)) |
| 652 | return MD; |
| 653 | // If lazy-loading is enabled, we try recursively to load the operand |
| 654 | // instead of creating a temporary. |
| 655 | if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { |
| 656 | PlaceholderQueue Placeholders; |
| 657 | lazyLoadOneMetadata(ID, Placeholders); |
| 658 | resolveForwardRefsAndPlaceholders(Placeholders); |
| 659 | return MetadataList.lookup(ID); |
| 660 | } |
| 661 | return MetadataList.getMetadataFwdRef(ID); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 664 | DISubprogram *lookupSubprogramForFunction(Function *F) { |
| 665 | return FunctionsWithSPs.lookup(F); |
| 666 | } |
| 667 | |
| 668 | bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; } |
| 669 | |
| 670 | Error parseMetadataAttachment( |
| 671 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList); |
| 672 | |
| 673 | Error parseMetadataKinds(); |
| 674 | |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 675 | void setStripTBAA(bool Value) { StripTBAA = Value; } |
| 676 | bool isStrippingTBAA() { return StripTBAA; } |
| 677 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 678 | unsigned size() const { return MetadataList.size(); } |
| 679 | void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); } |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 680 | void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
Mehdi Amini | 42ef199 | 2017-01-07 18:31:38 +0000 | [diff] [blame] | 683 | Expected<bool> |
| 684 | MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() { |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 685 | IndexCursor = Stream; |
| 686 | SmallVector<uint64_t, 64> Record; |
| 687 | // Get the abbrevs, and preload record positions to make them lazy-loadable. |
| 688 | while (true) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 689 | Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks( |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 690 | BitstreamCursor::AF_DontPopBlockAtEnd); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 691 | if (!MaybeEntry) |
| 692 | return MaybeEntry.takeError(); |
| 693 | BitstreamEntry Entry = MaybeEntry.get(); |
| 694 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 695 | switch (Entry.Kind) { |
| 696 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 697 | case BitstreamEntry::Error: |
| 698 | return error("Malformed block"); |
| 699 | case BitstreamEntry::EndBlock: { |
| 700 | return true; |
| 701 | } |
| 702 | case BitstreamEntry::Record: { |
| 703 | // The interesting case. |
| 704 | ++NumMDRecordLoaded; |
| 705 | uint64_t CurrentPos = IndexCursor.GetCurrentBitNo(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 706 | Expected<unsigned> MaybeCode = IndexCursor.skipRecord(Entry.ID); |
| 707 | if (!MaybeCode) |
| 708 | return MaybeCode.takeError(); |
| 709 | unsigned Code = MaybeCode.get(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 710 | switch (Code) { |
| 711 | case bitc::METADATA_STRINGS: { |
| 712 | // Rewind and parse the strings. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 713 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
| 714 | return std::move(Err); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 715 | StringRef Blob; |
| 716 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 717 | if (Expected<unsigned> MaybeRecord = |
| 718 | IndexCursor.readRecord(Entry.ID, Record, &Blob)) |
| 719 | ; |
| 720 | else |
| 721 | return MaybeRecord.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 722 | unsigned NumStrings = Record[0]; |
| 723 | MDStringRef.reserve(NumStrings); |
| 724 | auto IndexNextMDString = [&](StringRef Str) { |
| 725 | MDStringRef.push_back(Str); |
| 726 | }; |
| 727 | if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString)) |
| 728 | return std::move(Err); |
| 729 | break; |
| 730 | } |
| 731 | case bitc::METADATA_INDEX_OFFSET: { |
| 732 | // This is the offset to the index, when we see this we skip all the |
| 733 | // records and load only an index to these. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 734 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
| 735 | return std::move(Err); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 736 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 737 | if (Expected<unsigned> MaybeRecord = |
| 738 | IndexCursor.readRecord(Entry.ID, Record)) |
| 739 | ; |
| 740 | else |
| 741 | return MaybeRecord.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 742 | if (Record.size() != 2) |
| 743 | return error("Invalid record"); |
| 744 | auto Offset = Record[0] + (Record[1] << 32); |
| 745 | auto BeginPos = IndexCursor.GetCurrentBitNo(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 746 | if (Error Err = IndexCursor.JumpToBit(BeginPos + Offset)) |
| 747 | return std::move(Err); |
| 748 | Expected<BitstreamEntry> MaybeEntry = |
| 749 | IndexCursor.advanceSkippingSubblocks( |
| 750 | BitstreamCursor::AF_DontPopBlockAtEnd); |
| 751 | if (!MaybeEntry) |
| 752 | return MaybeEntry.takeError(); |
| 753 | Entry = MaybeEntry.get(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 754 | assert(Entry.Kind == BitstreamEntry::Record && |
| 755 | "Corrupted bitcode: Expected `Record` when trying to find the " |
| 756 | "Metadata index"); |
| 757 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 758 | if (Expected<unsigned> MaybeCode = |
| 759 | IndexCursor.readRecord(Entry.ID, Record)) |
| 760 | assert(MaybeCode.get() == bitc::METADATA_INDEX && |
| 761 | "Corrupted bitcode: Expected `METADATA_INDEX` when trying to " |
| 762 | "find the Metadata index"); |
| 763 | else |
| 764 | return MaybeCode.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 765 | // Delta unpack |
| 766 | auto CurrentValue = BeginPos; |
| 767 | GlobalMetadataBitPosIndex.reserve(Record.size()); |
| 768 | for (auto &Elt : Record) { |
| 769 | CurrentValue += Elt; |
| 770 | GlobalMetadataBitPosIndex.push_back(CurrentValue); |
| 771 | } |
| 772 | break; |
| 773 | } |
| 774 | case bitc::METADATA_INDEX: |
| 775 | // We don't expect to get there, the Index is loaded when we encounter |
| 776 | // the offset. |
| 777 | return error("Corrupted Metadata block"); |
| 778 | case bitc::METADATA_NAME: { |
| 779 | // Named metadata need to be materialized now and aren't deferred. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 780 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
| 781 | return std::move(Err); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 782 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 783 | |
| 784 | unsigned Code; |
| 785 | if (Expected<unsigned> MaybeCode = |
| 786 | IndexCursor.readRecord(Entry.ID, Record)) { |
| 787 | Code = MaybeCode.get(); |
| 788 | assert(Code == bitc::METADATA_NAME); |
| 789 | } else |
| 790 | return MaybeCode.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 791 | |
| 792 | // Read name of the named metadata. |
| 793 | SmallString<8> Name(Record.begin(), Record.end()); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 794 | if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode()) |
| 795 | Code = MaybeCode.get(); |
| 796 | else |
| 797 | return MaybeCode.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 798 | |
| 799 | // Named Metadata comes in two parts, we expect the name to be followed |
| 800 | // by the node |
| 801 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 802 | if (Expected<unsigned> MaybeNextBitCode = |
| 803 | IndexCursor.readRecord(Code, Record)) |
| 804 | assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE); |
| 805 | else |
| 806 | return MaybeNextBitCode.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 807 | |
| 808 | // Read named metadata elements. |
| 809 | unsigned Size = Record.size(); |
| 810 | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); |
| 811 | for (unsigned i = 0; i != Size; ++i) { |
| 812 | // FIXME: We could use a placeholder here, however NamedMDNode are |
| 813 | // taking MDNode as operand and not using the Metadata infrastructure. |
| 814 | // It is acknowledged by 'TODO: Inherit from Metadata' in the |
| 815 | // NamedMDNode class definition. |
| 816 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); |
Teresa Johnson | f3d2453d | 2018-11-14 21:57:51 +0000 | [diff] [blame] | 817 | assert(MD && "Invalid metadata: expect fwd ref to MDNode"); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 818 | NMD->addOperand(MD); |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { |
| 823 | // FIXME: we need to do this early because we don't materialize global |
| 824 | // value explicitly. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 825 | if (Error Err = IndexCursor.JumpToBit(CurrentPos)) |
| 826 | return std::move(Err); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 827 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 828 | if (Expected<unsigned> MaybeRecord = |
| 829 | IndexCursor.readRecord(Entry.ID, Record)) |
| 830 | ; |
| 831 | else |
| 832 | return MaybeRecord.takeError(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 833 | if (Record.size() % 2 == 0) |
| 834 | return error("Invalid record"); |
| 835 | unsigned ValueID = Record[0]; |
| 836 | if (ValueID >= ValueList.size()) |
| 837 | return error("Invalid record"); |
| 838 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) |
| 839 | if (Error Err = parseGlobalObjectAttachment( |
| 840 | *GO, ArrayRef<uint64_t>(Record).slice(1))) |
| 841 | return std::move(Err); |
| 842 | break; |
| 843 | } |
| 844 | case bitc::METADATA_KIND: |
| 845 | case bitc::METADATA_STRING_OLD: |
| 846 | case bitc::METADATA_OLD_FN_NODE: |
| 847 | case bitc::METADATA_OLD_NODE: |
| 848 | case bitc::METADATA_VALUE: |
| 849 | case bitc::METADATA_DISTINCT_NODE: |
| 850 | case bitc::METADATA_NODE: |
| 851 | case bitc::METADATA_LOCATION: |
| 852 | case bitc::METADATA_GENERIC_DEBUG: |
| 853 | case bitc::METADATA_SUBRANGE: |
| 854 | case bitc::METADATA_ENUMERATOR: |
| 855 | case bitc::METADATA_BASIC_TYPE: |
| 856 | case bitc::METADATA_DERIVED_TYPE: |
| 857 | case bitc::METADATA_COMPOSITE_TYPE: |
| 858 | case bitc::METADATA_SUBROUTINE_TYPE: |
| 859 | case bitc::METADATA_MODULE: |
| 860 | case bitc::METADATA_FILE: |
| 861 | case bitc::METADATA_COMPILE_UNIT: |
| 862 | case bitc::METADATA_SUBPROGRAM: |
| 863 | case bitc::METADATA_LEXICAL_BLOCK: |
| 864 | case bitc::METADATA_LEXICAL_BLOCK_FILE: |
| 865 | case bitc::METADATA_NAMESPACE: |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 866 | case bitc::METADATA_COMMON_BLOCK: |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 867 | case bitc::METADATA_MACRO: |
| 868 | case bitc::METADATA_MACRO_FILE: |
| 869 | case bitc::METADATA_TEMPLATE_TYPE: |
| 870 | case bitc::METADATA_TEMPLATE_VALUE: |
| 871 | case bitc::METADATA_GLOBAL_VAR: |
| 872 | case bitc::METADATA_LOCAL_VAR: |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 873 | case bitc::METADATA_LABEL: |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 874 | case bitc::METADATA_EXPRESSION: |
| 875 | case bitc::METADATA_OBJC_PROPERTY: |
| 876 | case bitc::METADATA_IMPORTED_ENTITY: |
| 877 | case bitc::METADATA_GLOBAL_VAR_EXPR: |
| 878 | // We don't expect to see any of these, if we see one, give up on |
| 879 | // lazy-loading and fallback. |
| 880 | MDStringRef.clear(); |
| 881 | GlobalMetadataBitPosIndex.clear(); |
| 882 | return false; |
| 883 | } |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 890 | /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing |
| 891 | /// module level metadata. |
Mehdi Amini | ec68dd4 | 2016-12-23 02:20:02 +0000 | [diff] [blame] | 892 | Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 893 | if (!ModuleLevel && MetadataList.hasFwdRefs()) |
| 894 | return error("Invalid metadata: fwd refs into function blocks"); |
| 895 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 896 | // Record the entry position so that we can jump back here and efficiently |
| 897 | // skip the whole block in case we lazy-load. |
| 898 | auto EntryPos = Stream.GetCurrentBitNo(); |
| 899 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 900 | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) |
| 901 | return Err; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 902 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 903 | SmallVector<uint64_t, 64> Record; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 904 | PlaceholderQueue Placeholders; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 905 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 906 | // We lazy-load module-level metadata: we build an index for each record, and |
| 907 | // then load individual record as needed, starting with the named metadata. |
| 908 | if (ModuleLevel && IsImporting && MetadataList.empty() && |
| 909 | !DisableLazyLoading) { |
Mehdi Amini | 42ef199 | 2017-01-07 18:31:38 +0000 | [diff] [blame] | 910 | auto SuccessOrErr = lazyLoadModuleMetadataBlock(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 911 | if (!SuccessOrErr) |
| 912 | return SuccessOrErr.takeError(); |
| 913 | if (SuccessOrErr.get()) { |
| 914 | // An index was successfully created and we will be able to load metadata |
| 915 | // on-demand. |
| 916 | MetadataList.resize(MDStringRef.size() + |
| 917 | GlobalMetadataBitPosIndex.size()); |
| 918 | |
| 919 | // Reading the named metadata created forward references and/or |
| 920 | // placeholders, that we flush here. |
| 921 | resolveForwardRefsAndPlaceholders(Placeholders); |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 922 | upgradeDebugInfo(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 923 | // Return at the beginning of the block, since it is easy to skip it |
| 924 | // entirely from there. |
| 925 | Stream.ReadBlockEnd(); // Pop the abbrev block context. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 926 | if (Error Err = IndexCursor.JumpToBit(EntryPos)) |
| 927 | return Err; |
| 928 | if (Error Err = Stream.SkipBlock()) { |
| 929 | // FIXME this drops the error on the floor, which |
| 930 | // ThinLTO/X86/debuginfo-cu-import.ll relies on. |
| 931 | consumeError(std::move(Err)); |
| 932 | return Error::success(); |
| 933 | } |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 934 | return Error::success(); |
| 935 | } |
| 936 | // Couldn't load an index, fallback to loading all the block "old-style". |
| 937 | } |
| 938 | |
| 939 | unsigned NextMetadataNo = MetadataList.size(); |
| 940 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 941 | // Read all the records. |
| 942 | while (true) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 943 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
| 944 | if (!MaybeEntry) |
| 945 | return MaybeEntry.takeError(); |
| 946 | BitstreamEntry Entry = MaybeEntry.get(); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 947 | |
| 948 | switch (Entry.Kind) { |
| 949 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 950 | case BitstreamEntry::Error: |
| 951 | return error("Malformed block"); |
| 952 | case BitstreamEntry::EndBlock: |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 953 | resolveForwardRefsAndPlaceholders(Placeholders); |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 954 | upgradeDebugInfo(); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 955 | return Error::success(); |
| 956 | case BitstreamEntry::Record: |
| 957 | // The interesting case. |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | // Read a record. |
| 962 | Record.clear(); |
| 963 | StringRef Blob; |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 964 | ++NumMDRecordLoaded; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 965 | if (Expected<unsigned> MaybeCode = |
| 966 | Stream.readRecord(Entry.ID, Record, &Blob)) { |
| 967 | if (Error Err = parseOneMetadata(Record, MaybeCode.get(), Placeholders, |
| 968 | Blob, NextMetadataNo)) |
| 969 | return Err; |
| 970 | } else |
| 971 | return MaybeCode.takeError(); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 975 | MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) { |
| 976 | ++NumMDStringLoaded; |
| 977 | if (Metadata *MD = MetadataList.lookup(ID)) |
| 978 | return cast<MDString>(MD); |
| 979 | auto MDS = MDString::get(Context, MDStringRef[ID]); |
| 980 | MetadataList.assignValue(MDS, ID); |
| 981 | return MDS; |
| 982 | } |
| 983 | |
| 984 | void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata( |
| 985 | unsigned ID, PlaceholderQueue &Placeholders) { |
| 986 | assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size()); |
| 987 | assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString"); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 988 | // Lookup first if the metadata hasn't already been loaded. |
| 989 | if (auto *MD = MetadataList.lookup(ID)) { |
Simon Pilgrim | 8739999 | 2019-09-26 11:30:47 +0000 | [diff] [blame] | 990 | auto *N = cast<MDNode>(MD); |
Mehdi Amini | 67d2cc1 | 2017-01-18 18:36:21 +0000 | [diff] [blame] | 991 | if (!N->isTemporary()) |
| 992 | return; |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 993 | } |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 994 | SmallVector<uint64_t, 64> Record; |
| 995 | StringRef Blob; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 996 | if (Error Err = IndexCursor.JumpToBit( |
| 997 | GlobalMetadataBitPosIndex[ID - MDStringRef.size()])) |
| 998 | report_fatal_error("lazyLoadOneMetadata failed jumping: " + |
| 999 | toString(std::move(Err))); |
| 1000 | Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks(); |
| 1001 | if (!MaybeEntry) |
| 1002 | // FIXME this drops the error on the floor. |
| 1003 | report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " + |
| 1004 | toString(MaybeEntry.takeError())); |
| 1005 | BitstreamEntry Entry = MaybeEntry.get(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1006 | ++NumMDRecordLoaded; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 1007 | if (Expected<unsigned> MaybeCode = |
| 1008 | IndexCursor.readRecord(Entry.ID, Record, &Blob)) { |
| 1009 | if (Error Err = |
| 1010 | parseOneMetadata(Record, MaybeCode.get(), Placeholders, Blob, ID)) |
| 1011 | report_fatal_error("Can't lazyload MD, parseOneMetadata: " + |
| 1012 | toString(std::move(Err))); |
| 1013 | } else |
| 1014 | report_fatal_error("Can't lazyload MD: " + toString(MaybeCode.takeError())); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /// Ensure that all forward-references and placeholders are resolved. |
| 1018 | /// Iteratively lazy-loading metadata on-demand if needed. |
| 1019 | void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders( |
| 1020 | PlaceholderQueue &Placeholders) { |
| 1021 | DenseSet<unsigned> Temporaries; |
| 1022 | while (1) { |
| 1023 | // Populate Temporaries with the placeholders that haven't been loaded yet. |
| 1024 | Placeholders.getTemporaries(MetadataList, Temporaries); |
| 1025 | |
| 1026 | // If we don't have any temporary, or FwdReference, we're done! |
| 1027 | if (Temporaries.empty() && !MetadataList.hasFwdRefs()) |
| 1028 | break; |
| 1029 | |
| 1030 | // First, load all the temporaries. This can add new placeholders or |
| 1031 | // forward references. |
| 1032 | for (auto ID : Temporaries) |
| 1033 | lazyLoadOneMetadata(ID, Placeholders); |
| 1034 | Temporaries.clear(); |
| 1035 | |
| 1036 | // Second, load the forward-references. This can also add new placeholders |
| 1037 | // or forward references. |
| 1038 | while (MetadataList.hasFwdRefs()) |
| 1039 | lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders); |
| 1040 | } |
| 1041 | // At this point we don't have any forward reference remaining, or temporary |
| 1042 | // that haven't been loaded. We can safely drop RAUW support and mark cycles |
| 1043 | // as resolved. |
| 1044 | MetadataList.tryToResolveCycles(); |
| 1045 | |
| 1046 | // Finally, everything is in place, we can replace the placeholders operands |
| 1047 | // with the final node they refer to. |
| 1048 | Placeholders.flush(MetadataList); |
| 1049 | } |
| 1050 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1051 | Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( |
| 1052 | SmallVectorImpl<uint64_t> &Record, unsigned Code, |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1053 | PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) { |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1054 | |
| 1055 | bool IsDistinct = false; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1056 | auto getMD = [&](unsigned ID) -> Metadata * { |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1057 | if (ID < MDStringRef.size()) |
| 1058 | return lazyLoadOneMDString(ID); |
Mehdi Amini | 67d2cc1 | 2017-01-18 18:36:21 +0000 | [diff] [blame] | 1059 | if (!IsDistinct) { |
| 1060 | if (auto *MD = MetadataList.lookup(ID)) |
| 1061 | return MD; |
| 1062 | // If lazy-loading is enabled, we try recursively to load the operand |
| 1063 | // instead of creating a temporary. |
| 1064 | if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { |
| 1065 | // Create a temporary for the node that is referencing the operand we |
| 1066 | // will lazy-load. It is needed before recursing in case there are |
| 1067 | // uniquing cycles. |
| 1068 | MetadataList.getMetadataFwdRef(NextMetadataNo); |
| 1069 | lazyLoadOneMetadata(ID, Placeholders); |
| 1070 | return MetadataList.lookup(ID); |
| 1071 | } |
| 1072 | // Return a temporary. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1073 | return MetadataList.getMetadataFwdRef(ID); |
Mehdi Amini | 67d2cc1 | 2017-01-18 18:36:21 +0000 | [diff] [blame] | 1074 | } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1075 | if (auto *MD = MetadataList.getMetadataIfResolved(ID)) |
| 1076 | return MD; |
| 1077 | return &Placeholders.getPlaceholderOp(ID); |
| 1078 | }; |
| 1079 | auto getMDOrNull = [&](unsigned ID) -> Metadata * { |
| 1080 | if (ID) |
| 1081 | return getMD(ID - 1); |
| 1082 | return nullptr; |
| 1083 | }; |
| 1084 | auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * { |
| 1085 | if (ID) |
| 1086 | return MetadataList.getMetadataFwdRef(ID - 1); |
| 1087 | return nullptr; |
| 1088 | }; |
| 1089 | auto getMDString = [&](unsigned ID) -> MDString * { |
| 1090 | // This requires that the ID is not really a forward reference. In |
| 1091 | // particular, the MDString must already have been resolved. |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1092 | auto MDS = getMDOrNull(ID); |
| 1093 | return cast_or_null<MDString>(MDS); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | // Support for old type refs. |
| 1097 | auto getDITypeRefOrNull = [&](unsigned ID) { |
| 1098 | return MetadataList.upgradeTypeRef(getMDOrNull(ID)); |
| 1099 | }; |
| 1100 | |
| 1101 | #define GET_OR_DISTINCT(CLASS, ARGS) \ |
| 1102 | (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
| 1103 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1104 | switch (Code) { |
| 1105 | default: // Default behavior: ignore. |
| 1106 | break; |
| 1107 | case bitc::METADATA_NAME: { |
| 1108 | // Read name of the named metadata. |
| 1109 | SmallString<8> Name(Record.begin(), Record.end()); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1110 | Record.clear(); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 1111 | Expected<unsigned> MaybeCode = Stream.ReadCode(); |
| 1112 | if (!MaybeCode) |
| 1113 | return MaybeCode.takeError(); |
| 1114 | Code = MaybeCode.get(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1115 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1116 | ++NumMDRecordLoaded; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 1117 | if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) { |
| 1118 | if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE) |
| 1119 | return error("METADATA_NAME not followed by METADATA_NAMED_NODE"); |
| 1120 | } else |
| 1121 | return MaybeNextBitCode.takeError(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1122 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1123 | // Read named metadata elements. |
| 1124 | unsigned Size = Record.size(); |
| 1125 | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); |
| 1126 | for (unsigned i = 0; i != Size; ++i) { |
| 1127 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); |
| 1128 | if (!MD) |
Teresa Johnson | f3d2453d | 2018-11-14 21:57:51 +0000 | [diff] [blame] | 1129 | return error("Invalid named metadata: expect fwd ref to MDNode"); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1130 | NMD->addOperand(MD); |
| 1131 | } |
| 1132 | break; |
| 1133 | } |
| 1134 | case bitc::METADATA_OLD_FN_NODE: { |
| 1135 | // FIXME: Remove in 4.0. |
| 1136 | // This is a LocalAsMetadata record, the only type of function-local |
| 1137 | // metadata. |
| 1138 | if (Record.size() % 2 == 1) |
| 1139 | return error("Invalid record"); |
| 1140 | |
| 1141 | // If this isn't a LocalAsMetadata record, we're dropping it. This used |
| 1142 | // to be legal, but there's no upgrade path. |
| 1143 | auto dropRecord = [&] { |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1144 | MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo); |
| 1145 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1146 | }; |
| 1147 | if (Record.size() != 2) { |
| 1148 | dropRecord(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1151 | |
| 1152 | Type *Ty = getTypeByID(Record[0]); |
| 1153 | if (Ty->isMetadataTy() || Ty->isVoidTy()) { |
| 1154 | dropRecord(); |
| 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | MetadataList.assignValue( |
| 1159 | LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1160 | NextMetadataNo); |
| 1161 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | } |
| 1164 | case bitc::METADATA_OLD_NODE: { |
| 1165 | // FIXME: Remove in 4.0. |
| 1166 | if (Record.size() % 2 == 1) |
| 1167 | return error("Invalid record"); |
| 1168 | |
| 1169 | unsigned Size = Record.size(); |
| 1170 | SmallVector<Metadata *, 8> Elts; |
| 1171 | for (unsigned i = 0; i != Size; i += 2) { |
| 1172 | Type *Ty = getTypeByID(Record[i]); |
| 1173 | if (!Ty) |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1174 | return error("Invalid record"); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1175 | if (Ty->isMetadataTy()) |
| 1176 | Elts.push_back(getMD(Record[i + 1])); |
| 1177 | else if (!Ty->isVoidTy()) { |
| 1178 | auto *MD = |
| 1179 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); |
| 1180 | assert(isa<ConstantAsMetadata>(MD) && |
| 1181 | "Expected non-function-local metadata"); |
| 1182 | Elts.push_back(MD); |
| 1183 | } else |
| 1184 | Elts.push_back(nullptr); |
| 1185 | } |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1186 | MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo); |
| 1187 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | case bitc::METADATA_VALUE: { |
| 1191 | if (Record.size() != 2) |
| 1192 | return error("Invalid record"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1193 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1194 | Type *Ty = getTypeByID(Record[0]); |
| 1195 | if (Ty->isMetadataTy() || Ty->isVoidTy()) |
| 1196 | return error("Invalid record"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1197 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1198 | MetadataList.assignValue( |
| 1199 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1200 | NextMetadataNo); |
| 1201 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | case bitc::METADATA_DISTINCT_NODE: |
| 1205 | IsDistinct = true; |
| 1206 | LLVM_FALLTHROUGH; |
| 1207 | case bitc::METADATA_NODE: { |
| 1208 | SmallVector<Metadata *, 8> Elts; |
| 1209 | Elts.reserve(Record.size()); |
| 1210 | for (unsigned ID : Record) |
| 1211 | Elts.push_back(getMDOrNull(ID)); |
| 1212 | MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) |
| 1213 | : MDNode::get(Context, Elts), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1214 | NextMetadataNo); |
| 1215 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | } |
| 1218 | case bitc::METADATA_LOCATION: { |
Vedant Kumar | 386ad01 | 2018-09-20 18:59:33 +0000 | [diff] [blame] | 1219 | if (Record.size() != 5 && Record.size() != 6) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1220 | return error("Invalid record"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1221 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1222 | IsDistinct = Record[0]; |
| 1223 | unsigned Line = Record[1]; |
| 1224 | unsigned Column = Record[2]; |
| 1225 | Metadata *Scope = getMD(Record[3]); |
| 1226 | Metadata *InlinedAt = getMDOrNull(Record[4]); |
Vedant Kumar | 386ad01 | 2018-09-20 18:59:33 +0000 | [diff] [blame] | 1227 | bool ImplicitCode = Record.size() == 6 && Record[5]; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1228 | MetadataList.assignValue( |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 1229 | GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt, |
| 1230 | ImplicitCode)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1231 | NextMetadataNo); |
| 1232 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1233 | break; |
| 1234 | } |
| 1235 | case bitc::METADATA_GENERIC_DEBUG: { |
| 1236 | if (Record.size() < 4) |
| 1237 | return error("Invalid record"); |
| 1238 | |
| 1239 | IsDistinct = Record[0]; |
| 1240 | unsigned Tag = Record[1]; |
| 1241 | unsigned Version = Record[2]; |
| 1242 | |
| 1243 | if (Tag >= 1u << 16 || Version != 0) |
| 1244 | return error("Invalid record"); |
| 1245 | |
| 1246 | auto *Header = getMDString(Record[3]); |
| 1247 | SmallVector<Metadata *, 8> DwarfOps; |
| 1248 | for (unsigned I = 4, E = Record.size(); I != E; ++I) |
| 1249 | DwarfOps.push_back(getMDOrNull(Record[I])); |
| 1250 | MetadataList.assignValue( |
| 1251 | GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1252 | NextMetadataNo); |
| 1253 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | } |
| 1256 | case bitc::METADATA_SUBRANGE: { |
Sander de Smalen | fdf4091 | 2018-01-24 09:56:07 +0000 | [diff] [blame] | 1257 | Metadata *Val = nullptr; |
| 1258 | // Operand 'count' is interpreted as: |
| 1259 | // - Signed integer (version 0) |
| 1260 | // - Metadata node (version 1) |
| 1261 | switch (Record[0] >> 1) { |
| 1262 | case 0: |
| 1263 | Val = GET_OR_DISTINCT(DISubrange, |
| 1264 | (Context, Record[1], unrotateSign(Record.back()))); |
| 1265 | break; |
| 1266 | case 1: |
| 1267 | Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]), |
| 1268 | unrotateSign(Record.back()))); |
| 1269 | break; |
| 1270 | default: |
| 1271 | return error("Invalid record: Unsupported version of DISubrange"); |
| 1272 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1273 | |
Sander de Smalen | fdf4091 | 2018-01-24 09:56:07 +0000 | [diff] [blame] | 1274 | MetadataList.assignValue(Val, NextMetadataNo); |
| 1275 | IsDistinct = Record[0] & 1; |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1276 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1277 | break; |
| 1278 | } |
| 1279 | case bitc::METADATA_ENUMERATOR: { |
| 1280 | if (Record.size() != 3) |
| 1281 | return error("Invalid record"); |
| 1282 | |
Momchil Velikov | 08dc66e | 2018-02-12 16:10:09 +0000 | [diff] [blame] | 1283 | IsDistinct = Record[0] & 1; |
| 1284 | bool IsUnsigned = Record[0] & 2; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1285 | MetadataList.assignValue( |
| 1286 | GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]), |
Momchil Velikov | 08dc66e | 2018-02-12 16:10:09 +0000 | [diff] [blame] | 1287 | IsUnsigned, getMDString(Record[2]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1288 | NextMetadataNo); |
| 1289 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1290 | break; |
| 1291 | } |
| 1292 | case bitc::METADATA_BASIC_TYPE: { |
Adrian Prantl | 55f4262 | 2018-08-14 19:35:34 +0000 | [diff] [blame] | 1293 | if (Record.size() < 6 || Record.size() > 7) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1294 | return error("Invalid record"); |
| 1295 | |
| 1296 | IsDistinct = Record[0]; |
Adrian Prantl | 55f4262 | 2018-08-14 19:35:34 +0000 | [diff] [blame] | 1297 | DINode::DIFlags Flags = (Record.size() > 6) ? |
| 1298 | static_cast<DINode::DIFlags>(Record[6]) : DINode::FlagZero; |
| 1299 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1300 | MetadataList.assignValue( |
| 1301 | GET_OR_DISTINCT(DIBasicType, |
| 1302 | (Context, Record[1], getMDString(Record[2]), Record[3], |
Adrian Prantl | 55f4262 | 2018-08-14 19:35:34 +0000 | [diff] [blame] | 1303 | Record[4], Record[5], Flags)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1304 | NextMetadataNo); |
| 1305 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1306 | break; |
| 1307 | } |
| 1308 | case bitc::METADATA_DERIVED_TYPE: { |
Konstantin Zhuravlyov | d5561e0 | 2017-03-08 23:55:44 +0000 | [diff] [blame] | 1309 | if (Record.size() < 12 || Record.size() > 13) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1310 | return error("Invalid record"); |
| 1311 | |
Konstantin Zhuravlyov | d5561e0 | 2017-03-08 23:55:44 +0000 | [diff] [blame] | 1312 | // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means |
| 1313 | // that there is no DWARF address space associated with DIDerivedType. |
| 1314 | Optional<unsigned> DWARFAddressSpace; |
| 1315 | if (Record.size() > 12 && Record[12]) |
| 1316 | DWARFAddressSpace = Record[12] - 1; |
| 1317 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1318 | IsDistinct = Record[0]; |
| 1319 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); |
| 1320 | MetadataList.assignValue( |
| 1321 | GET_OR_DISTINCT(DIDerivedType, |
| 1322 | (Context, Record[1], getMDString(Record[2]), |
| 1323 | getMDOrNull(Record[3]), Record[4], |
| 1324 | getDITypeRefOrNull(Record[5]), |
| 1325 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
Konstantin Zhuravlyov | d5561e0 | 2017-03-08 23:55:44 +0000 | [diff] [blame] | 1326 | Record[9], DWARFAddressSpace, Flags, |
| 1327 | getDITypeRefOrNull(Record[11]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1328 | NextMetadataNo); |
| 1329 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | case bitc::METADATA_COMPOSITE_TYPE: { |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 1333 | if (Record.size() < 16 || Record.size() > 17) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1334 | return error("Invalid record"); |
| 1335 | |
| 1336 | // If we have a UUID and this is not a forward declaration, lookup the |
| 1337 | // mapping. |
| 1338 | IsDistinct = Record[0] & 0x1; |
| 1339 | bool IsNotUsedInTypeRef = Record[0] >= 2; |
| 1340 | unsigned Tag = Record[1]; |
| 1341 | MDString *Name = getMDString(Record[2]); |
| 1342 | Metadata *File = getMDOrNull(Record[3]); |
| 1343 | unsigned Line = Record[4]; |
| 1344 | Metadata *Scope = getDITypeRefOrNull(Record[5]); |
| 1345 | Metadata *BaseType = nullptr; |
| 1346 | uint64_t SizeInBits = Record[7]; |
| 1347 | if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
| 1348 | return error("Alignment value is too large"); |
| 1349 | uint32_t AlignInBits = Record[8]; |
| 1350 | uint64_t OffsetInBits = 0; |
| 1351 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); |
| 1352 | Metadata *Elements = nullptr; |
| 1353 | unsigned RuntimeLang = Record[12]; |
| 1354 | Metadata *VTableHolder = nullptr; |
| 1355 | Metadata *TemplateParams = nullptr; |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 1356 | Metadata *Discriminator = nullptr; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1357 | auto *Identifier = getMDString(Record[15]); |
| 1358 | // If this module is being parsed so that it can be ThinLTO imported |
| 1359 | // into another module, composite types only need to be imported |
| 1360 | // as type declarations (unless full type definitions requested). |
| 1361 | // Create type declarations up front to save memory. Also, buildODRType |
| 1362 | // handles the case where this is type ODRed with a definition needed |
| 1363 | // by the importing module, in which case the existing definition is |
| 1364 | // used. |
Teresa Johnson | 5a8dba5 | 2017-01-03 23:19:29 +0000 | [diff] [blame] | 1365 | if (IsImporting && !ImportFullTypeDefinitions && Identifier && |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1366 | (Tag == dwarf::DW_TAG_enumeration_type || |
| 1367 | Tag == dwarf::DW_TAG_class_type || |
| 1368 | Tag == dwarf::DW_TAG_structure_type || |
| 1369 | Tag == dwarf::DW_TAG_union_type)) { |
| 1370 | Flags = Flags | DINode::FlagFwdDecl; |
| 1371 | } else { |
| 1372 | BaseType = getDITypeRefOrNull(Record[6]); |
| 1373 | OffsetInBits = Record[9]; |
| 1374 | Elements = getMDOrNull(Record[11]); |
| 1375 | VTableHolder = getDITypeRefOrNull(Record[13]); |
| 1376 | TemplateParams = getMDOrNull(Record[14]); |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 1377 | if (Record.size() > 16) |
| 1378 | Discriminator = getMDOrNull(Record[16]); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1379 | } |
| 1380 | DICompositeType *CT = nullptr; |
| 1381 | if (Identifier) |
| 1382 | CT = DICompositeType::buildODRType( |
| 1383 | Context, *Identifier, Tag, Name, File, Line, Scope, BaseType, |
| 1384 | SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 1385 | VTableHolder, TemplateParams, Discriminator); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Create a node if we didn't get a lazy ODR type. |
| 1388 | if (!CT) |
| 1389 | CT = GET_OR_DISTINCT(DICompositeType, |
| 1390 | (Context, Tag, Name, File, Line, Scope, BaseType, |
| 1391 | SizeInBits, AlignInBits, OffsetInBits, Flags, |
| 1392 | Elements, RuntimeLang, VTableHolder, TemplateParams, |
Jonas Devlieghere | a0c9cb1 | 2018-09-21 12:03:14 +0000 | [diff] [blame] | 1393 | Identifier, Discriminator)); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1394 | if (!IsNotUsedInTypeRef && Identifier) |
| 1395 | MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT)); |
| 1396 | |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1397 | MetadataList.assignValue(CT, NextMetadataNo); |
| 1398 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1399 | break; |
| 1400 | } |
| 1401 | case bitc::METADATA_SUBROUTINE_TYPE: { |
| 1402 | if (Record.size() < 3 || Record.size() > 4) |
| 1403 | return error("Invalid record"); |
| 1404 | bool IsOldTypeRefArray = Record[0] < 2; |
| 1405 | unsigned CC = (Record.size() > 3) ? Record[3] : 0; |
| 1406 | |
| 1407 | IsDistinct = Record[0] & 0x1; |
| 1408 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]); |
| 1409 | Metadata *Types = getMDOrNull(Record[2]); |
| 1410 | if (LLVM_UNLIKELY(IsOldTypeRefArray)) |
| 1411 | Types = MetadataList.upgradeTypeRefArray(Types); |
| 1412 | |
| 1413 | MetadataList.assignValue( |
| 1414 | GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1415 | NextMetadataNo); |
| 1416 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1417 | break; |
| 1418 | } |
| 1419 | |
| 1420 | case bitc::METADATA_MODULE: { |
| 1421 | if (Record.size() != 6) |
| 1422 | return error("Invalid record"); |
| 1423 | |
| 1424 | IsDistinct = Record[0]; |
| 1425 | MetadataList.assignValue( |
| 1426 | GET_OR_DISTINCT(DIModule, |
| 1427 | (Context, getMDOrNull(Record[1]), |
| 1428 | getMDString(Record[2]), getMDString(Record[3]), |
| 1429 | getMDString(Record[4]), getMDString(Record[5]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1430 | NextMetadataNo); |
| 1431 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | case bitc::METADATA_FILE: { |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1436 | if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1437 | return error("Invalid record"); |
| 1438 | |
| 1439 | IsDistinct = Record[0]; |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 1440 | Optional<DIFile::ChecksumInfo<MDString *>> Checksum; |
| 1441 | // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum |
| 1442 | // is not present. This matches up with the old internal representation, |
| 1443 | // and the old encoding for CSK_None in the ChecksumKind. The new |
| 1444 | // representation reserves the value 0 in the ChecksumKind to continue to |
| 1445 | // encode None in a backwards-compatible way. |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1446 | if (Record.size() > 4 && Record[3] && Record[4]) |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 1447 | Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]), |
| 1448 | getMDString(Record[4])); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1449 | MetadataList.assignValue( |
| 1450 | GET_OR_DISTINCT( |
Amjad Aboud | 7faeecc | 2016-12-25 10:12:09 +0000 | [diff] [blame] | 1451 | DIFile, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1452 | (Context, getMDString(Record[1]), getMDString(Record[2]), Checksum, |
| 1453 | Record.size() > 5 ? Optional<MDString *>(getMDString(Record[5])) |
| 1454 | : None)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1455 | NextMetadataNo); |
| 1456 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | case bitc::METADATA_COMPILE_UNIT: { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1460 | if (Record.size() < 14 || Record.size() > 19) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1461 | return error("Invalid record"); |
| 1462 | |
| 1463 | // Ignore Record[0], which indicates whether this compile unit is |
| 1464 | // distinct. It's always distinct. |
| 1465 | IsDistinct = true; |
| 1466 | auto *CU = DICompileUnit::getDistinct( |
| 1467 | Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]), |
| 1468 | Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]), |
| 1469 | Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]), |
| 1470 | getMDOrNull(Record[12]), getMDOrNull(Record[13]), |
| 1471 | Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]), |
| 1472 | Record.size() <= 14 ? 0 : Record[14], |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 1473 | Record.size() <= 16 ? true : Record[16], |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1474 | Record.size() <= 17 ? false : Record[17], |
David Blaikie | bb27911 | 2018-11-13 20:08:10 +0000 | [diff] [blame] | 1475 | Record.size() <= 18 ? 0 : Record[18], |
| 1476 | Record.size() <= 19 ? 0 : Record[19]); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1477 | |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1478 | MetadataList.assignValue(CU, NextMetadataNo); |
| 1479 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1480 | |
| 1481 | // Move the Upgrade the list of subprograms. |
| 1482 | if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11])) |
| 1483 | CUSubprograms.push_back({CU, SPs}); |
| 1484 | break; |
| 1485 | } |
| 1486 | case bitc::METADATA_SUBPROGRAM: { |
Adrian Prantl | 1d12b88 | 2017-04-26 22:56:44 +0000 | [diff] [blame] | 1487 | if (Record.size() < 18 || Record.size() > 21) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1488 | return error("Invalid record"); |
| 1489 | |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 1490 | bool HasSPFlags = Record[0] & 4; |
Petar Jovanovic | 38a6187 | 2019-03-19 13:49:03 +0000 | [diff] [blame] | 1491 | |
| 1492 | DINode::DIFlags Flags; |
| 1493 | DISubprogram::DISPFlags SPFlags; |
| 1494 | if (!HasSPFlags) |
| 1495 | Flags = static_cast<DINode::DIFlags>(Record[11 + 2]); |
| 1496 | else { |
| 1497 | Flags = static_cast<DINode::DIFlags>(Record[11]); |
| 1498 | SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]); |
| 1499 | } |
| 1500 | |
| 1501 | // Support for old metadata when |
| 1502 | // subprogram specific flags are placed in DIFlags. |
| 1503 | const unsigned DIFlagMainSubprogram = 1 << 21; |
| 1504 | bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram; |
| 1505 | if (HasOldMainSubprogramFlag) |
| 1506 | // Remove old DIFlagMainSubprogram from DIFlags. |
| 1507 | // Note: This assumes that any future use of bit 21 defaults to it |
| 1508 | // being 0. |
| 1509 | Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram); |
| 1510 | |
| 1511 | if (HasOldMainSubprogramFlag && HasSPFlags) |
| 1512 | SPFlags |= DISubprogram::SPFlagMainSubprogram; |
| 1513 | else if (!HasSPFlags) |
| 1514 | SPFlags = DISubprogram::toSPFlags( |
| 1515 | /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8], |
| 1516 | /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11], |
| 1517 | /*DIFlagMainSubprogram*/HasOldMainSubprogramFlag); |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 1518 | |
| 1519 | // All definitions should be distinct. |
| 1520 | IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1521 | // Version 1 has a Function as Record[15]. |
| 1522 | // Version 2 has removed Record[15]. |
| 1523 | // Version 3 has the Unit as Record[15]. |
| 1524 | // Version 4 added thisAdjustment. |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 1525 | // Version 5 repacked flags into DISPFlags, changing many element numbers. |
| 1526 | bool HasUnit = Record[0] & 2; |
| 1527 | if (!HasSPFlags && HasUnit && Record.size() < 19) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1528 | return error("Invalid record"); |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 1529 | if (HasSPFlags && !HasUnit) |
| 1530 | return error("Invalid record"); |
| 1531 | // Accommodate older formats. |
| 1532 | bool HasFn = false; |
| 1533 | bool HasThisAdj = true; |
| 1534 | bool HasThrownTypes = true; |
| 1535 | unsigned OffsetA = 0; |
| 1536 | unsigned OffsetB = 0; |
| 1537 | if (!HasSPFlags) { |
| 1538 | OffsetA = 2; |
| 1539 | OffsetB = 2; |
| 1540 | if (Record.size() >= 19) { |
| 1541 | HasFn = !HasUnit; |
| 1542 | OffsetB++; |
| 1543 | } |
| 1544 | HasThisAdj = Record.size() >= 20; |
| 1545 | HasThrownTypes = Record.size() >= 21; |
| 1546 | } |
| 1547 | Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1548 | DISubprogram *SP = GET_OR_DISTINCT( |
Adrian Prantl | 1d12b88 | 2017-04-26 22:56:44 +0000 | [diff] [blame] | 1549 | DISubprogram, |
| 1550 | (Context, |
| 1551 | getDITypeRefOrNull(Record[1]), // scope |
| 1552 | getMDString(Record[2]), // name |
| 1553 | getMDString(Record[3]), // linkageName |
| 1554 | getMDOrNull(Record[4]), // file |
| 1555 | Record[5], // line |
| 1556 | getMDOrNull(Record[6]), // type |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 1557 | Record[7 + OffsetA], // scopeLine |
| 1558 | getDITypeRefOrNull(Record[8 + OffsetA]), // containingType |
| 1559 | Record[10 + OffsetA], // virtualIndex |
| 1560 | HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment |
Petar Jovanovic | 38a6187 | 2019-03-19 13:49:03 +0000 | [diff] [blame] | 1561 | Flags, // flags |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 1562 | SPFlags, // SPFlags |
Adrian Prantl | 1d12b88 | 2017-04-26 22:56:44 +0000 | [diff] [blame] | 1563 | HasUnit ? CUorFn : nullptr, // unit |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 1564 | getMDOrNull(Record[13 + OffsetB]), // templateParams |
| 1565 | getMDOrNull(Record[14 + OffsetB]), // declaration |
| 1566 | getMDOrNull(Record[15 + OffsetB]), // retainedNodes |
| 1567 | HasThrownTypes ? getMDOrNull(Record[17 + OffsetB]) |
| 1568 | : nullptr // thrownTypes |
Adrian Prantl | 1d12b88 | 2017-04-26 22:56:44 +0000 | [diff] [blame] | 1569 | )); |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1570 | MetadataList.assignValue(SP, NextMetadataNo); |
| 1571 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1572 | |
| 1573 | // Upgrade sp->function mapping to function->sp mapping. |
| 1574 | if (HasFn) { |
| 1575 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn)) |
| 1576 | if (auto *F = dyn_cast<Function>(CMD->getValue())) { |
| 1577 | if (F->isMaterializable()) |
| 1578 | // Defer until materialized; unmaterialized functions may not have |
| 1579 | // metadata. |
| 1580 | FunctionsWithSPs[F] = SP; |
| 1581 | else if (!F->empty()) |
| 1582 | F->setSubprogram(SP); |
| 1583 | } |
| 1584 | } |
| 1585 | break; |
| 1586 | } |
| 1587 | case bitc::METADATA_LEXICAL_BLOCK: { |
| 1588 | if (Record.size() != 5) |
| 1589 | return error("Invalid record"); |
| 1590 | |
| 1591 | IsDistinct = Record[0]; |
| 1592 | MetadataList.assignValue( |
| 1593 | GET_OR_DISTINCT(DILexicalBlock, |
| 1594 | (Context, getMDOrNull(Record[1]), |
| 1595 | getMDOrNull(Record[2]), Record[3], Record[4])), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1596 | NextMetadataNo); |
| 1597 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1598 | break; |
| 1599 | } |
| 1600 | case bitc::METADATA_LEXICAL_BLOCK_FILE: { |
| 1601 | if (Record.size() != 4) |
| 1602 | return error("Invalid record"); |
| 1603 | |
| 1604 | IsDistinct = Record[0]; |
| 1605 | MetadataList.assignValue( |
| 1606 | GET_OR_DISTINCT(DILexicalBlockFile, |
| 1607 | (Context, getMDOrNull(Record[1]), |
| 1608 | getMDOrNull(Record[2]), Record[3])), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1609 | NextMetadataNo); |
| 1610 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | } |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 1613 | case bitc::METADATA_COMMON_BLOCK: { |
| 1614 | IsDistinct = Record[0] & 1; |
| 1615 | MetadataList.assignValue( |
| 1616 | GET_OR_DISTINCT(DICommonBlock, |
| 1617 | (Context, getMDOrNull(Record[1]), |
| 1618 | getMDOrNull(Record[2]), getMDString(Record[3]), |
| 1619 | getMDOrNull(Record[4]), Record[5])), |
| 1620 | NextMetadataNo); |
| 1621 | NextMetadataNo++; |
| 1622 | break; |
| 1623 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1624 | case bitc::METADATA_NAMESPACE: { |
Adrian Prantl | fed4f39 | 2017-04-28 22:25:46 +0000 | [diff] [blame] | 1625 | // Newer versions of DINamespace dropped file and line. |
| 1626 | MDString *Name; |
| 1627 | if (Record.size() == 3) |
| 1628 | Name = getMDString(Record[2]); |
| 1629 | else if (Record.size() == 5) |
| 1630 | Name = getMDString(Record[3]); |
| 1631 | else |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1632 | return error("Invalid record"); |
| 1633 | |
| 1634 | IsDistinct = Record[0] & 1; |
| 1635 | bool ExportSymbols = Record[0] & 2; |
| 1636 | MetadataList.assignValue( |
| 1637 | GET_OR_DISTINCT(DINamespace, |
Adrian Prantl | fed4f39 | 2017-04-28 22:25:46 +0000 | [diff] [blame] | 1638 | (Context, getMDOrNull(Record[1]), Name, ExportSymbols)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1639 | NextMetadataNo); |
| 1640 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1641 | break; |
| 1642 | } |
| 1643 | case bitc::METADATA_MACRO: { |
| 1644 | if (Record.size() != 5) |
| 1645 | return error("Invalid record"); |
| 1646 | |
| 1647 | IsDistinct = Record[0]; |
| 1648 | MetadataList.assignValue( |
| 1649 | GET_OR_DISTINCT(DIMacro, |
| 1650 | (Context, Record[1], Record[2], getMDString(Record[3]), |
| 1651 | getMDString(Record[4]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1652 | NextMetadataNo); |
| 1653 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | } |
| 1656 | case bitc::METADATA_MACRO_FILE: { |
| 1657 | if (Record.size() != 5) |
| 1658 | return error("Invalid record"); |
| 1659 | |
| 1660 | IsDistinct = Record[0]; |
| 1661 | MetadataList.assignValue( |
| 1662 | GET_OR_DISTINCT(DIMacroFile, |
| 1663 | (Context, Record[1], Record[2], getMDOrNull(Record[3]), |
| 1664 | getMDOrNull(Record[4]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1665 | NextMetadataNo); |
| 1666 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1667 | break; |
| 1668 | } |
| 1669 | case bitc::METADATA_TEMPLATE_TYPE: { |
| 1670 | if (Record.size() != 3) |
| 1671 | return error("Invalid record"); |
| 1672 | |
| 1673 | IsDistinct = Record[0]; |
| 1674 | MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter, |
| 1675 | (Context, getMDString(Record[1]), |
| 1676 | getDITypeRefOrNull(Record[2]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1677 | NextMetadataNo); |
| 1678 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | } |
| 1681 | case bitc::METADATA_TEMPLATE_VALUE: { |
| 1682 | if (Record.size() != 5) |
| 1683 | return error("Invalid record"); |
| 1684 | |
| 1685 | IsDistinct = Record[0]; |
| 1686 | MetadataList.assignValue( |
| 1687 | GET_OR_DISTINCT(DITemplateValueParameter, |
| 1688 | (Context, Record[1], getMDString(Record[2]), |
| 1689 | getDITypeRefOrNull(Record[3]), |
| 1690 | getMDOrNull(Record[4]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1691 | NextMetadataNo); |
| 1692 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
| 1695 | case bitc::METADATA_GLOBAL_VAR: { |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 1696 | if (Record.size() < 11 || Record.size() > 13) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1697 | return error("Invalid record"); |
| 1698 | |
| 1699 | IsDistinct = Record[0] & 1; |
| 1700 | unsigned Version = Record[0] >> 1; |
| 1701 | |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 1702 | if (Version == 2) { |
| 1703 | MetadataList.assignValue( |
| 1704 | GET_OR_DISTINCT( |
| 1705 | DIGlobalVariable, |
| 1706 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
| 1707 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
| 1708 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
| 1709 | getMDOrNull(Record[9]), getMDOrNull(Record[10]), Record[11])), |
| 1710 | NextMetadataNo); |
| 1711 | |
| 1712 | NextMetadataNo++; |
| 1713 | } else if (Version == 1) { |
| 1714 | // No upgrade necessary. A null field will be introduced to indicate |
| 1715 | // that no parameter information is available. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1716 | MetadataList.assignValue( |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1717 | GET_OR_DISTINCT(DIGlobalVariable, |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1718 | (Context, getMDOrNull(Record[1]), |
| 1719 | getMDString(Record[2]), getMDString(Record[3]), |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1720 | getMDOrNull(Record[4]), Record[5], |
| 1721 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 1722 | getMDOrNull(Record[10]), nullptr, Record[11])), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1723 | NextMetadataNo); |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 1724 | |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1725 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1726 | } else if (Version == 0) { |
| 1727 | // Upgrade old metadata, which stored a global variable reference or a |
| 1728 | // ConstantInt here. |
Adrian Prantl | a5bf2d7 | 2017-02-08 17:44:43 +0000 | [diff] [blame] | 1729 | NeedUpgradeToDIGlobalVariableExpression = true; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1730 | Metadata *Expr = getMDOrNull(Record[9]); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1731 | uint32_t AlignInBits = 0; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1732 | if (Record.size() > 11) { |
| 1733 | if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1734 | return error("Alignment value is too large"); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1735 | AlignInBits = Record[11]; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1736 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1737 | GlobalVariable *Attach = nullptr; |
| 1738 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) { |
| 1739 | if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) { |
| 1740 | Attach = GV; |
| 1741 | Expr = nullptr; |
| 1742 | } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) { |
| 1743 | Expr = DIExpression::get(Context, |
| 1744 | {dwarf::DW_OP_constu, CI->getZExtValue(), |
| 1745 | dwarf::DW_OP_stack_value}); |
| 1746 | } else { |
| 1747 | Expr = nullptr; |
| 1748 | } |
| 1749 | } |
| 1750 | DIGlobalVariable *DGV = GET_OR_DISTINCT( |
| 1751 | DIGlobalVariable, |
| 1752 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
| 1753 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
| 1754 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 1755 | getMDOrNull(Record[10]), nullptr, AlignInBits)); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1756 | |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 1757 | DIGlobalVariableExpression *DGVE = nullptr; |
| 1758 | if (Attach || Expr) |
Adrian Prantl | 0578221 | 2017-08-30 18:06:51 +0000 | [diff] [blame] | 1759 | DGVE = DIGlobalVariableExpression::getDistinct( |
| 1760 | Context, DGV, Expr ? Expr : DIExpression::get(Context, {})); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1761 | if (Attach) |
| 1762 | Attach->addDebugInfo(DGVE); |
Adrian Prantl | e37d314 | 2017-02-07 17:35:41 +0000 | [diff] [blame] | 1763 | |
| 1764 | auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV); |
| 1765 | MetadataList.assignValue(MDNode, NextMetadataNo); |
| 1766 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1767 | } else |
| 1768 | return error("Invalid record"); |
| 1769 | |
| 1770 | break; |
| 1771 | } |
| 1772 | case bitc::METADATA_LOCAL_VAR: { |
| 1773 | // 10th field is for the obseleted 'inlinedAt:' field. |
| 1774 | if (Record.size() < 8 || Record.size() > 10) |
| 1775 | return error("Invalid record"); |
| 1776 | |
| 1777 | IsDistinct = Record[0] & 1; |
| 1778 | bool HasAlignment = Record[0] & 2; |
| 1779 | // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or |
| 1780 | // DW_TAG_arg_variable, if we have alignment flag encoded it means, that |
Simon Pilgrim | 68168d1 | 2017-03-30 12:59:53 +0000 | [diff] [blame] | 1781 | // this is newer version of record which doesn't have artificial tag. |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1782 | bool HasTag = !HasAlignment && Record.size() > 8; |
| 1783 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]); |
| 1784 | uint32_t AlignInBits = 0; |
| 1785 | if (HasAlignment) { |
| 1786 | if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
| 1787 | return error("Alignment value is too large"); |
| 1788 | AlignInBits = Record[8 + HasTag]; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1789 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1790 | MetadataList.assignValue( |
| 1791 | GET_OR_DISTINCT(DILocalVariable, |
| 1792 | (Context, getMDOrNull(Record[1 + HasTag]), |
| 1793 | getMDString(Record[2 + HasTag]), |
| 1794 | getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag], |
| 1795 | getDITypeRefOrNull(Record[5 + HasTag]), |
| 1796 | Record[6 + HasTag], Flags, AlignInBits)), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1797 | NextMetadataNo); |
| 1798 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1799 | break; |
| 1800 | } |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 1801 | case bitc::METADATA_LABEL: { |
| 1802 | if (Record.size() != 5) |
| 1803 | return error("Invalid record"); |
| 1804 | |
| 1805 | IsDistinct = Record[0] & 1; |
| 1806 | MetadataList.assignValue( |
| 1807 | GET_OR_DISTINCT(DILabel, |
| 1808 | (Context, getMDOrNull(Record[1]), |
| 1809 | getMDString(Record[2]), |
| 1810 | getMDOrNull(Record[3]), Record[4])), |
| 1811 | NextMetadataNo); |
| 1812 | NextMetadataNo++; |
| 1813 | break; |
| 1814 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1815 | case bitc::METADATA_EXPRESSION: { |
| 1816 | if (Record.size() < 1) |
| 1817 | return error("Invalid record"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1818 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1819 | IsDistinct = Record[0] & 1; |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 1820 | uint64_t Version = Record[0] >> 1; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1821 | auto Elts = MutableArrayRef<uint64_t>(Record).slice(1); |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 1822 | |
| 1823 | SmallVector<uint64_t, 6> Buffer; |
| 1824 | if (Error Err = upgradeDIExpression(Version, Elts, Buffer)) |
| 1825 | return Err; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1826 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1827 | MetadataList.assignValue( |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 1828 | GET_OR_DISTINCT(DIExpression, (Context, Elts)), NextMetadataNo); |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1829 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1830 | break; |
| 1831 | } |
| 1832 | case bitc::METADATA_GLOBAL_VAR_EXPR: { |
| 1833 | if (Record.size() != 3) |
| 1834 | return error("Invalid record"); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 1835 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1836 | IsDistinct = Record[0]; |
Adrian Prantl | 0578221 | 2017-08-30 18:06:51 +0000 | [diff] [blame] | 1837 | Metadata *Expr = getMDOrNull(Record[2]); |
| 1838 | if (!Expr) |
| 1839 | Expr = DIExpression::get(Context, {}); |
| 1840 | MetadataList.assignValue( |
| 1841 | GET_OR_DISTINCT(DIGlobalVariableExpression, |
| 1842 | (Context, getMDOrNull(Record[1]), Expr)), |
| 1843 | NextMetadataNo); |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1844 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1845 | break; |
| 1846 | } |
| 1847 | case bitc::METADATA_OBJC_PROPERTY: { |
| 1848 | if (Record.size() != 8) |
| 1849 | return error("Invalid record"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1850 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1851 | IsDistinct = Record[0]; |
| 1852 | MetadataList.assignValue( |
| 1853 | GET_OR_DISTINCT(DIObjCProperty, |
| 1854 | (Context, getMDString(Record[1]), |
| 1855 | getMDOrNull(Record[2]), Record[3], |
| 1856 | getMDString(Record[4]), getMDString(Record[5]), |
| 1857 | Record[6], getDITypeRefOrNull(Record[7]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1858 | NextMetadataNo); |
| 1859 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1860 | break; |
| 1861 | } |
| 1862 | case bitc::METADATA_IMPORTED_ENTITY: { |
Adrian Prantl | d63bfd2 | 2017-07-19 00:09:54 +0000 | [diff] [blame] | 1863 | if (Record.size() != 6 && Record.size() != 7) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1864 | return error("Invalid record"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1865 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1866 | IsDistinct = Record[0]; |
Adrian Prantl | d63bfd2 | 2017-07-19 00:09:54 +0000 | [diff] [blame] | 1867 | bool HasFile = (Record.size() == 7); |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1868 | MetadataList.assignValue( |
| 1869 | GET_OR_DISTINCT(DIImportedEntity, |
| 1870 | (Context, Record[1], getMDOrNull(Record[2]), |
Adrian Prantl | d63bfd2 | 2017-07-19 00:09:54 +0000 | [diff] [blame] | 1871 | getDITypeRefOrNull(Record[3]), |
| 1872 | HasFile ? getMDOrNull(Record[6]) : nullptr, |
| 1873 | HasFile ? Record[4] : 0, getMDString(Record[5]))), |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1874 | NextMetadataNo); |
| 1875 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | } |
| 1878 | case bitc::METADATA_STRING_OLD: { |
| 1879 | std::string String(Record.begin(), Record.end()); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1880 | |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1881 | // Test for upgrading !llvm.loop. |
| 1882 | HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1883 | ++NumMDStringLoaded; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1884 | Metadata *MD = MDString::get(Context, String); |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1885 | MetadataList.assignValue(MD, NextMetadataNo); |
| 1886 | NextMetadataNo++; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1887 | break; |
| 1888 | } |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1889 | case bitc::METADATA_STRINGS: { |
| 1890 | auto CreateNextMDString = [&](StringRef Str) { |
| 1891 | ++NumMDStringLoaded; |
Ivan Krasin | c05c9db | 2017-01-27 15:54:49 +0000 | [diff] [blame] | 1892 | MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo); |
| 1893 | NextMetadataNo++; |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1894 | }; |
| 1895 | if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString)) |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1896 | return Err; |
| 1897 | break; |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1898 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1899 | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { |
| 1900 | if (Record.size() % 2 == 0) |
| 1901 | return error("Invalid record"); |
| 1902 | unsigned ValueID = Record[0]; |
| 1903 | if (ValueID >= ValueList.size()) |
| 1904 | return error("Invalid record"); |
| 1905 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) |
| 1906 | if (Error Err = parseGlobalObjectAttachment( |
| 1907 | *GO, ArrayRef<uint64_t>(Record).slice(1))) |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1908 | return Err; |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1909 | break; |
| 1910 | } |
| 1911 | case bitc::METADATA_KIND: { |
| 1912 | // Support older bitcode files that had METADATA_KIND records in a |
| 1913 | // block with METADATA_BLOCK_ID. |
| 1914 | if (Error Err = parseMetadataKindRecord(Record)) |
| 1915 | return Err; |
| 1916 | break; |
| 1917 | } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1918 | } |
Mehdi Amini | 9f926f7 | 2016-12-23 03:59:18 +0000 | [diff] [blame] | 1919 | return Error::success(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1920 | #undef GET_OR_DISTINCT |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings( |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1924 | ArrayRef<uint64_t> Record, StringRef Blob, |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 1925 | function_ref<void(StringRef)> CallBack) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1926 | // All the MDStrings in the block are emitted together in a single |
| 1927 | // record. The strings are concatenated and stored in a blob along with |
| 1928 | // their sizes. |
| 1929 | if (Record.size() != 2) |
| 1930 | return error("Invalid record: metadata strings layout"); |
| 1931 | |
| 1932 | unsigned NumStrings = Record[0]; |
| 1933 | unsigned StringsOffset = Record[1]; |
| 1934 | if (!NumStrings) |
| 1935 | return error("Invalid record: metadata strings with no strings"); |
| 1936 | if (StringsOffset > Blob.size()) |
| 1937 | return error("Invalid record: metadata strings corrupt offset"); |
| 1938 | |
| 1939 | StringRef Lengths = Blob.slice(0, StringsOffset); |
| 1940 | SimpleBitstreamCursor R(Lengths); |
| 1941 | |
| 1942 | StringRef Strings = Blob.drop_front(StringsOffset); |
| 1943 | do { |
| 1944 | if (R.AtEndOfStream()) |
| 1945 | return error("Invalid record: metadata strings bad length"); |
| 1946 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 1947 | Expected<uint32_t> MaybeSize = R.ReadVBR(6); |
| 1948 | if (!MaybeSize) |
| 1949 | return MaybeSize.takeError(); |
| 1950 | uint32_t Size = MaybeSize.get(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1951 | if (Strings.size() < Size) |
| 1952 | return error("Invalid record: metadata strings truncated chars"); |
| 1953 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 1954 | CallBack(Strings.slice(0, Size)); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1955 | Strings = Strings.drop_front(Size); |
| 1956 | } while (--NumStrings); |
| 1957 | |
| 1958 | return Error::success(); |
| 1959 | } |
| 1960 | |
| 1961 | Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment( |
| 1962 | GlobalObject &GO, ArrayRef<uint64_t> Record) { |
| 1963 | assert(Record.size() % 2 == 0); |
| 1964 | for (unsigned I = 0, E = Record.size(); I != E; I += 2) { |
| 1965 | auto K = MDKindMap.find(Record[I]); |
| 1966 | if (K == MDKindMap.end()) |
| 1967 | return error("Invalid ID"); |
| 1968 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]); |
| 1969 | if (!MD) |
Teresa Johnson | f3d2453d | 2018-11-14 21:57:51 +0000 | [diff] [blame] | 1970 | return error("Invalid metadata attachment: expect fwd ref to MDNode"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1971 | GO.addMetadata(K->second, *MD); |
| 1972 | } |
| 1973 | return Error::success(); |
| 1974 | } |
| 1975 | |
| 1976 | /// Parse metadata attachments. |
| 1977 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment( |
| 1978 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 1979 | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) |
| 1980 | return Err; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1981 | |
| 1982 | SmallVector<uint64_t, 64> Record; |
Mehdi Amini | 7b0d145 | 2017-01-08 00:44:45 +0000 | [diff] [blame] | 1983 | PlaceholderQueue Placeholders; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1984 | |
| 1985 | while (true) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 1986 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
| 1987 | if (!MaybeEntry) |
| 1988 | return MaybeEntry.takeError(); |
| 1989 | BitstreamEntry Entry = MaybeEntry.get(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1990 | |
| 1991 | switch (Entry.Kind) { |
| 1992 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1993 | case BitstreamEntry::Error: |
| 1994 | return error("Malformed block"); |
| 1995 | case BitstreamEntry::EndBlock: |
Mehdi Amini | 7b0d145 | 2017-01-08 00:44:45 +0000 | [diff] [blame] | 1996 | resolveForwardRefsAndPlaceholders(Placeholders); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1997 | return Error::success(); |
| 1998 | case BitstreamEntry::Record: |
| 1999 | // The interesting case. |
| 2000 | break; |
| 2001 | } |
| 2002 | |
| 2003 | // Read a metadata attachment record. |
| 2004 | Record.clear(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 2005 | ++NumMDRecordLoaded; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 2006 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
| 2007 | if (!MaybeRecord) |
| 2008 | return MaybeRecord.takeError(); |
| 2009 | switch (MaybeRecord.get()) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2010 | default: // Default behavior: ignore. |
| 2011 | break; |
| 2012 | case bitc::METADATA_ATTACHMENT: { |
| 2013 | unsigned RecordLength = Record.size(); |
| 2014 | if (Record.empty()) |
| 2015 | return error("Invalid record"); |
| 2016 | if (RecordLength % 2 == 0) { |
| 2017 | // A function attachment. |
| 2018 | if (Error Err = parseGlobalObjectAttachment(F, Record)) |
| 2019 | return Err; |
| 2020 | continue; |
| 2021 | } |
| 2022 | |
| 2023 | // An instruction attachment. |
| 2024 | Instruction *Inst = InstructionList[Record[0]]; |
| 2025 | for (unsigned i = 1; i != RecordLength; i = i + 2) { |
| 2026 | unsigned Kind = Record[i]; |
| 2027 | DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind); |
| 2028 | if (I == MDKindMap.end()) |
| 2029 | return error("Invalid ID"); |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 2030 | if (I->second == LLVMContext::MD_tbaa && StripTBAA) |
| 2031 | continue; |
| 2032 | |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 2033 | auto Idx = Record[i + 1]; |
| 2034 | if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) && |
Mehdi Amini | d5549f3 | 2017-01-07 20:24:23 +0000 | [diff] [blame] | 2035 | !MetadataList.lookup(Idx)) { |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 2036 | // Load the attachment if it is in the lazy-loadable range and hasn't |
| 2037 | // been loaded yet. |
| 2038 | lazyLoadOneMetadata(Idx, Placeholders); |
Mehdi Amini | d5549f3 | 2017-01-07 20:24:23 +0000 | [diff] [blame] | 2039 | resolveForwardRefsAndPlaceholders(Placeholders); |
| 2040 | } |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 2041 | |
| 2042 | Metadata *Node = MetadataList.getMetadataFwdRef(Idx); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2043 | if (isa<LocalAsMetadata>(Node)) |
| 2044 | // Drop the attachment. This used to be legal, but there's no |
| 2045 | // upgrade path. |
| 2046 | break; |
| 2047 | MDNode *MD = dyn_cast_or_null<MDNode>(Node); |
| 2048 | if (!MD) |
| 2049 | return error("Invalid metadata attachment"); |
| 2050 | |
| 2051 | if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop) |
| 2052 | MD = upgradeInstructionLoopAttachment(*MD); |
| 2053 | |
| 2054 | if (I->second == LLVMContext::MD_tbaa) { |
| 2055 | assert(!MD->isTemporary() && "should load MDs before attachments"); |
| 2056 | MD = UpgradeTBAANode(*MD); |
| 2057 | } |
| 2058 | Inst->setMetadata(I->second, MD); |
| 2059 | } |
| 2060 | break; |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | /// Parse a single METADATA_KIND record, inserting result in MDKindMap. |
| 2067 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord( |
| 2068 | SmallVectorImpl<uint64_t> &Record) { |
| 2069 | if (Record.size() < 2) |
| 2070 | return error("Invalid record"); |
| 2071 | |
| 2072 | unsigned Kind = Record[0]; |
| 2073 | SmallString<8> Name(Record.begin() + 1, Record.end()); |
| 2074 | |
| 2075 | unsigned NewKind = TheModule.getMDKindID(Name.str()); |
| 2076 | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) |
| 2077 | return error("Conflicting METADATA_KIND records"); |
| 2078 | return Error::success(); |
| 2079 | } |
| 2080 | |
| 2081 | /// Parse the metadata kinds out of the METADATA_KIND_BLOCK. |
| 2082 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 2083 | if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID)) |
| 2084 | return Err; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2085 | |
| 2086 | SmallVector<uint64_t, 64> Record; |
| 2087 | |
| 2088 | // Read all the records. |
| 2089 | while (true) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 2090 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
| 2091 | if (!MaybeEntry) |
| 2092 | return MaybeEntry.takeError(); |
| 2093 | BitstreamEntry Entry = MaybeEntry.get(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2094 | |
| 2095 | switch (Entry.Kind) { |
| 2096 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2097 | case BitstreamEntry::Error: |
| 2098 | return error("Malformed block"); |
| 2099 | case BitstreamEntry::EndBlock: |
| 2100 | return Error::success(); |
| 2101 | case BitstreamEntry::Record: |
| 2102 | // The interesting case. |
| 2103 | break; |
| 2104 | } |
| 2105 | |
| 2106 | // Read a record. |
| 2107 | Record.clear(); |
Mehdi Amini | 19ef4fa | 2017-01-04 22:54:33 +0000 | [diff] [blame] | 2108 | ++NumMDRecordLoaded; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 2109 | Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record); |
| 2110 | if (!MaybeCode) |
| 2111 | return MaybeCode.takeError(); |
| 2112 | switch (MaybeCode.get()) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2113 | default: // Default behavior: ignore. |
| 2114 | break; |
| 2115 | case bitc::METADATA_KIND: { |
| 2116 | if (Error Err = parseMetadataKindRecord(Record)) |
| 2117 | return Err; |
| 2118 | break; |
| 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) { |
| 2125 | Pimpl = std::move(RHS.Pimpl); |
| 2126 | return *this; |
| 2127 | } |
| 2128 | MetadataLoader::MetadataLoader(MetadataLoader &&RHS) |
Mehdi Amini | ec68dd4 | 2016-12-23 02:20:02 +0000 | [diff] [blame] | 2129 | : Pimpl(std::move(RHS.Pimpl)) {} |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2130 | |
| 2131 | MetadataLoader::~MetadataLoader() = default; |
| 2132 | MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule, |
| 2133 | BitcodeReaderValueList &ValueList, |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 2134 | bool IsImporting, |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2135 | std::function<Type *(unsigned)> getTypeByID) |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2136 | : Pimpl(std::make_unique<MetadataLoaderImpl>( |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 2137 | Stream, TheModule, ValueList, std::move(getTypeByID), IsImporting)) {} |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2138 | |
| 2139 | Error MetadataLoader::parseMetadata(bool ModuleLevel) { |
Mehdi Amini | ec68dd4 | 2016-12-23 02:20:02 +0000 | [diff] [blame] | 2140 | return Pimpl->parseMetadata(ModuleLevel); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); } |
| 2144 | |
| 2145 | /// Return the given metadata, creating a replaceable forward reference if |
| 2146 | /// necessary. |
Mehdi Amini | 3bb4d01 | 2017-01-20 20:29:16 +0000 | [diff] [blame] | 2147 | Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) { |
| 2148 | return Pimpl->getMetadataFwdRefOrLoad(Idx); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2151 | DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) { |
| 2152 | return Pimpl->lookupSubprogramForFunction(F); |
| 2153 | } |
| 2154 | |
| 2155 | Error MetadataLoader::parseMetadataAttachment( |
| 2156 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { |
| 2157 | return Pimpl->parseMetadataAttachment(F, InstructionList); |
| 2158 | } |
| 2159 | |
| 2160 | Error MetadataLoader::parseMetadataKinds() { |
| 2161 | return Pimpl->parseMetadataKinds(); |
| 2162 | } |
| 2163 | |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 2164 | void MetadataLoader::setStripTBAA(bool StripTBAA) { |
| 2165 | return Pimpl->setStripTBAA(StripTBAA); |
| 2166 | } |
| 2167 | |
| 2168 | bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); } |
| 2169 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2170 | unsigned MetadataLoader::size() const { return Pimpl->size(); } |
| 2171 | void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); } |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 2172 | |
| 2173 | void MetadataLoader::upgradeDebugIntrinsics(Function &F) { |
| 2174 | return Pimpl->upgradeDebugIntrinsics(F); |
| 2175 | } |