Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1 | //===- lib/Linker/IRMover.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Linker/IRMover.h" |
| 11 | #include "LinkDiagnosticInfo.h" |
| 12 | #include "llvm/ADT/SetVector.h" |
| 13 | #include "llvm/ADT/SmallString.h" |
| 14 | #include "llvm/ADT/Triple.h" |
| 15 | #include "llvm/IR/Constants.h" |
Teresa Johnson | 0e7c82c | 2015-12-18 17:51:37 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DebugInfo.h" |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 18 | #include "llvm/IR/GVMaterializer.h" |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Intrinsics.h" |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 20 | #include "llvm/IR/TypeFinder.h" |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Error.h" |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/Cloning.h" |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 23 | #include <utility> |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // TypeMap implementation. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | namespace { |
| 31 | class TypeMapTy : public ValueMapTypeRemapper { |
| 32 | /// This is a mapping from a source type to a destination type to use. |
| 33 | DenseMap<Type *, Type *> MappedTypes; |
| 34 | |
| 35 | /// When checking to see if two subgraphs are isomorphic, we speculatively |
| 36 | /// add types to MappedTypes, but keep track of them here in case we need to |
| 37 | /// roll back. |
| 38 | SmallVector<Type *, 16> SpeculativeTypes; |
| 39 | |
| 40 | SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes; |
| 41 | |
| 42 | /// This is a list of non-opaque structs in the source module that are mapped |
| 43 | /// to an opaque struct in the destination module. |
| 44 | SmallVector<StructType *, 16> SrcDefinitionsToResolve; |
| 45 | |
| 46 | /// This is the set of opaque types in the destination modules who are |
| 47 | /// getting a body from the source module. |
| 48 | SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes; |
| 49 | |
| 50 | public: |
| 51 | TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet) |
| 52 | : DstStructTypesSet(DstStructTypesSet) {} |
| 53 | |
| 54 | IRMover::IdentifiedStructTypeSet &DstStructTypesSet; |
| 55 | /// Indicate that the specified type in the destination module is conceptually |
| 56 | /// equivalent to the specified type in the source module. |
| 57 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
| 58 | |
| 59 | /// Produce a body for an opaque type in the dest module from a type |
| 60 | /// definition in the source module. |
| 61 | void linkDefinedTypeBodies(); |
| 62 | |
| 63 | /// Return the mapped type to use for the specified input type from the |
| 64 | /// source module. |
| 65 | Type *get(Type *SrcTy); |
| 66 | Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited); |
| 67 | |
| 68 | void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes); |
| 69 | |
| 70 | FunctionType *get(FunctionType *T) { |
| 71 | return cast<FunctionType>(get((Type *)T)); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | Type *remapType(Type *SrcTy) override { return get(SrcTy); } |
| 76 | |
| 77 | bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
| 82 | assert(SpeculativeTypes.empty()); |
| 83 | assert(SpeculativeDstOpaqueTypes.empty()); |
| 84 | |
| 85 | // Check to see if these types are recursively isomorphic and establish a |
| 86 | // mapping between them if so. |
| 87 | if (!areTypesIsomorphic(DstTy, SrcTy)) { |
| 88 | // Oops, they aren't isomorphic. Just discard this request by rolling out |
| 89 | // any speculative mappings we've established. |
| 90 | for (Type *Ty : SpeculativeTypes) |
| 91 | MappedTypes.erase(Ty); |
| 92 | |
| 93 | SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() - |
| 94 | SpeculativeDstOpaqueTypes.size()); |
| 95 | for (StructType *Ty : SpeculativeDstOpaqueTypes) |
| 96 | DstResolvedOpaqueTypes.erase(Ty); |
| 97 | } else { |
Eugene Leviant | 41e4595 | 2018-01-25 08:35:52 +0000 | [diff] [blame^] | 98 | // SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy |
| 99 | // and all its descendants to lower amount of renaming in LLVM context |
| 100 | // Renaming occurs because we load all source modules to the same context |
| 101 | // and declaration with existing name gets renamed (i.e Foo -> Foo.42). |
| 102 | // As a result we may get several different types in the destination |
| 103 | // module, which are in fact the same. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 104 | for (Type *Ty : SpeculativeTypes) |
| 105 | if (auto *STy = dyn_cast<StructType>(Ty)) |
| 106 | if (STy->hasName()) |
| 107 | STy->setName(""); |
| 108 | } |
| 109 | SpeculativeTypes.clear(); |
| 110 | SpeculativeDstOpaqueTypes.clear(); |
| 111 | } |
| 112 | |
| 113 | /// Recursively walk this pair of types, returning true if they are isomorphic, |
| 114 | /// false if they are not. |
| 115 | bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { |
| 116 | // Two types with differing kinds are clearly not isomorphic. |
| 117 | if (DstTy->getTypeID() != SrcTy->getTypeID()) |
| 118 | return false; |
| 119 | |
| 120 | // If we have an entry in the MappedTypes table, then we have our answer. |
| 121 | Type *&Entry = MappedTypes[SrcTy]; |
| 122 | if (Entry) |
| 123 | return Entry == DstTy; |
| 124 | |
| 125 | // Two identical types are clearly isomorphic. Remember this |
| 126 | // non-speculatively. |
| 127 | if (DstTy == SrcTy) { |
| 128 | Entry = DstTy; |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | // Okay, we have two types with identical kinds that we haven't seen before. |
| 133 | |
| 134 | // If this is an opaque struct type, special case it. |
| 135 | if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) { |
| 136 | // Mapping an opaque type to any struct, just keep the dest struct. |
| 137 | if (SSTy->isOpaque()) { |
| 138 | Entry = DstTy; |
| 139 | SpeculativeTypes.push_back(SrcTy); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // Mapping a non-opaque source type to an opaque dest. If this is the first |
| 144 | // type that we're mapping onto this destination type then we succeed. Keep |
| 145 | // the dest, but fill it in later. If this is the second (different) type |
| 146 | // that we're trying to map onto the same opaque type then we fail. |
| 147 | if (cast<StructType>(DstTy)->isOpaque()) { |
| 148 | // We can only map one source type onto the opaque destination type. |
| 149 | if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second) |
| 150 | return false; |
| 151 | SrcDefinitionsToResolve.push_back(SSTy); |
| 152 | SpeculativeTypes.push_back(SrcTy); |
| 153 | SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy)); |
| 154 | Entry = DstTy; |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // If the number of subtypes disagree between the two types, then we fail. |
| 160 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
| 161 | return false; |
| 162 | |
| 163 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
| 164 | if (isa<IntegerType>(DstTy)) |
| 165 | return false; // bitwidth disagrees. |
| 166 | if (PointerType *PT = dyn_cast<PointerType>(DstTy)) { |
| 167 | if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace()) |
| 168 | return false; |
| 169 | |
| 170 | } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) { |
| 171 | if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg()) |
| 172 | return false; |
| 173 | } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) { |
| 174 | StructType *SSTy = cast<StructType>(SrcTy); |
| 175 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
| 176 | DSTy->isPacked() != SSTy->isPacked()) |
| 177 | return false; |
Peter Collingbourne | bc07052 | 2016-12-02 03:20:58 +0000 | [diff] [blame] | 178 | } else if (auto *DSeqTy = dyn_cast<SequentialType>(DstTy)) { |
| 179 | if (DSeqTy->getNumElements() != |
| 180 | cast<SequentialType>(SrcTy)->getNumElements()) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Otherwise, we speculate that these two types will line up and recursively |
| 185 | // check the subelements. |
| 186 | Entry = DstTy; |
| 187 | SpeculativeTypes.push_back(SrcTy); |
| 188 | |
| 189 | for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I) |
| 190 | if (!areTypesIsomorphic(DstTy->getContainedType(I), |
| 191 | SrcTy->getContainedType(I))) |
| 192 | return false; |
| 193 | |
| 194 | // If everything seems to have lined up, then everything is great. |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | void TypeMapTy::linkDefinedTypeBodies() { |
| 199 | SmallVector<Type *, 16> Elements; |
| 200 | for (StructType *SrcSTy : SrcDefinitionsToResolve) { |
| 201 | StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]); |
| 202 | assert(DstSTy->isOpaque()); |
| 203 | |
| 204 | // Map the body of the source type over to a new body for the dest type. |
| 205 | Elements.resize(SrcSTy->getNumElements()); |
| 206 | for (unsigned I = 0, E = Elements.size(); I != E; ++I) |
| 207 | Elements[I] = get(SrcSTy->getElementType(I)); |
| 208 | |
| 209 | DstSTy->setBody(Elements, SrcSTy->isPacked()); |
| 210 | DstStructTypesSet.switchToNonOpaque(DstSTy); |
| 211 | } |
| 212 | SrcDefinitionsToResolve.clear(); |
| 213 | DstResolvedOpaqueTypes.clear(); |
| 214 | } |
| 215 | |
| 216 | void TypeMapTy::finishType(StructType *DTy, StructType *STy, |
| 217 | ArrayRef<Type *> ETypes) { |
| 218 | DTy->setBody(ETypes, STy->isPacked()); |
| 219 | |
| 220 | // Steal STy's name. |
| 221 | if (STy->hasName()) { |
| 222 | SmallString<16> TmpName = STy->getName(); |
| 223 | STy->setName(""); |
| 224 | DTy->setName(TmpName); |
| 225 | } |
| 226 | |
| 227 | DstStructTypesSet.addNonOpaque(DTy); |
| 228 | } |
| 229 | |
| 230 | Type *TypeMapTy::get(Type *Ty) { |
| 231 | SmallPtrSet<StructType *, 8> Visited; |
| 232 | return get(Ty, Visited); |
| 233 | } |
| 234 | |
| 235 | Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) { |
| 236 | // If we already have an entry for this type, return it. |
| 237 | Type **Entry = &MappedTypes[Ty]; |
| 238 | if (*Entry) |
| 239 | return *Entry; |
| 240 | |
| 241 | // These are types that LLVM itself will unique. |
| 242 | bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral(); |
| 243 | |
| 244 | #ifndef NDEBUG |
| 245 | if (!IsUniqued) { |
| 246 | for (auto &Pair : MappedTypes) { |
| 247 | assert(!(Pair.first != Ty && Pair.second == Ty) && |
| 248 | "mapping to a source type"); |
| 249 | } |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) { |
| 254 | StructType *DTy = StructType::create(Ty->getContext()); |
| 255 | return *Entry = DTy; |
| 256 | } |
| 257 | |
| 258 | // If this is not a recursive type, then just map all of the elements and |
| 259 | // then rebuild the type from inside out. |
| 260 | SmallVector<Type *, 4> ElementTypes; |
| 261 | |
| 262 | // If there are no element types to map, then the type is itself. This is |
| 263 | // true for the anonymous {} struct, things like 'float', integers, etc. |
| 264 | if (Ty->getNumContainedTypes() == 0 && IsUniqued) |
| 265 | return *Entry = Ty; |
| 266 | |
| 267 | // Remap all of the elements, keeping track of whether any of them change. |
| 268 | bool AnyChange = false; |
| 269 | ElementTypes.resize(Ty->getNumContainedTypes()); |
| 270 | for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) { |
| 271 | ElementTypes[I] = get(Ty->getContainedType(I), Visited); |
| 272 | AnyChange |= ElementTypes[I] != Ty->getContainedType(I); |
| 273 | } |
| 274 | |
| 275 | // If we found our type while recursively processing stuff, just use it. |
| 276 | Entry = &MappedTypes[Ty]; |
| 277 | if (*Entry) { |
| 278 | if (auto *DTy = dyn_cast<StructType>(*Entry)) { |
| 279 | if (DTy->isOpaque()) { |
| 280 | auto *STy = cast<StructType>(Ty); |
| 281 | finishType(DTy, STy, ElementTypes); |
| 282 | } |
| 283 | } |
| 284 | return *Entry; |
| 285 | } |
| 286 | |
| 287 | // If all of the element types mapped directly over and the type is not |
Hans Wennborg | 2d55d67 | 2016-10-19 20:10:03 +0000 | [diff] [blame] | 288 | // a named struct, then the type is usable as-is. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 289 | if (!AnyChange && IsUniqued) |
| 290 | return *Entry = Ty; |
| 291 | |
| 292 | // Otherwise, rebuild a modified type. |
| 293 | switch (Ty->getTypeID()) { |
| 294 | default: |
| 295 | llvm_unreachable("unknown derived type to remap"); |
| 296 | case Type::ArrayTyID: |
| 297 | return *Entry = ArrayType::get(ElementTypes[0], |
| 298 | cast<ArrayType>(Ty)->getNumElements()); |
| 299 | case Type::VectorTyID: |
| 300 | return *Entry = VectorType::get(ElementTypes[0], |
| 301 | cast<VectorType>(Ty)->getNumElements()); |
| 302 | case Type::PointerTyID: |
| 303 | return *Entry = PointerType::get(ElementTypes[0], |
| 304 | cast<PointerType>(Ty)->getAddressSpace()); |
| 305 | case Type::FunctionTyID: |
| 306 | return *Entry = FunctionType::get(ElementTypes[0], |
| 307 | makeArrayRef(ElementTypes).slice(1), |
| 308 | cast<FunctionType>(Ty)->isVarArg()); |
| 309 | case Type::StructTyID: { |
| 310 | auto *STy = cast<StructType>(Ty); |
| 311 | bool IsPacked = STy->isPacked(); |
| 312 | if (IsUniqued) |
| 313 | return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked); |
| 314 | |
| 315 | // If the type is opaque, we can just use it directly. |
| 316 | if (STy->isOpaque()) { |
| 317 | DstStructTypesSet.addOpaque(STy); |
| 318 | return *Entry = Ty; |
| 319 | } |
| 320 | |
| 321 | if (StructType *OldT = |
| 322 | DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) { |
| 323 | STy->setName(""); |
| 324 | return *Entry = OldT; |
| 325 | } |
| 326 | |
| 327 | if (!AnyChange) { |
| 328 | DstStructTypesSet.addNonOpaque(STy); |
| 329 | return *Entry = Ty; |
| 330 | } |
| 331 | |
| 332 | StructType *DTy = StructType::create(Ty->getContext()); |
| 333 | finishType(DTy, STy, ElementTypes); |
| 334 | return *Entry = DTy; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity, |
| 340 | const Twine &Msg) |
| 341 | : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {} |
| 342 | void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; } |
| 343 | |
| 344 | //===----------------------------------------------------------------------===// |
Teresa Johnson | bef5436 | 2015-12-18 19:28:59 +0000 | [diff] [blame] | 345 | // IRLinker implementation. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 346 | //===----------------------------------------------------------------------===// |
| 347 | |
| 348 | namespace { |
| 349 | class IRLinker; |
| 350 | |
| 351 | /// Creates prototypes for functions that are lazily linked on the fly. This |
| 352 | /// speeds up linking for modules with many/ lazily linked functions of which |
| 353 | /// few get used. |
| 354 | class GlobalValueMaterializer final : public ValueMaterializer { |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 355 | IRLinker &TheIRLinker; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 356 | |
| 357 | public: |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 358 | GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {} |
Mehdi Amini | cc8c107 | 2016-05-25 21:03:21 +0000 | [diff] [blame] | 359 | Value *materialize(Value *V) override; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | class LocalValueMaterializer final : public ValueMaterializer { |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 363 | IRLinker &TheIRLinker; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 364 | |
| 365 | public: |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 366 | LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {} |
Mehdi Amini | cc8c107 | 2016-05-25 21:03:21 +0000 | [diff] [blame] | 367 | Value *materialize(Value *V) override; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
Duncan P. N. Exon Smith | 565a0aa | 2016-04-17 23:30:31 +0000 | [diff] [blame] | 370 | /// Type of the Metadata map in \a ValueToValueMapTy. |
| 371 | typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT; |
| 372 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 373 | /// This is responsible for keeping track of the state used for moving data |
| 374 | /// from SrcM to DstM. |
| 375 | class IRLinker { |
| 376 | Module &DstM; |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 377 | std::unique_ptr<Module> SrcM; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 378 | |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 379 | /// See IRMover::move(). |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 380 | std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor; |
| 381 | |
| 382 | TypeMapTy TypeMap; |
| 383 | GlobalValueMaterializer GValMaterializer; |
| 384 | LocalValueMaterializer LValMaterializer; |
| 385 | |
Duncan P. N. Exon Smith | 565a0aa | 2016-04-17 23:30:31 +0000 | [diff] [blame] | 386 | /// A metadata map that's shared between IRLinker instances. |
| 387 | MDMapT &SharedMDs; |
| 388 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 389 | /// Mapping of values from what they used to be in Src, to what they are now |
| 390 | /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead |
| 391 | /// due to the use of Value handles which the Linker doesn't actually need, |
| 392 | /// but this allows us to reuse the ValueMapper code. |
| 393 | ValueToValueMapTy ValueMap; |
| 394 | ValueToValueMapTy AliasValueMap; |
| 395 | |
| 396 | DenseSet<GlobalValue *> ValuesToLink; |
| 397 | std::vector<GlobalValue *> Worklist; |
| 398 | |
| 399 | void maybeAdd(GlobalValue *GV) { |
| 400 | if (ValuesToLink.insert(GV).second) |
| 401 | Worklist.push_back(GV); |
| 402 | } |
| 403 | |
Peter Collingbourne | e6fd9ff | 2017-02-03 17:01:14 +0000 | [diff] [blame] | 404 | /// Whether we are importing globals for ThinLTO, as opposed to linking the |
| 405 | /// source module. If this flag is set, it means that we can rely on some |
| 406 | /// other object file to define any non-GlobalValue entities defined by the |
| 407 | /// source module. This currently causes us to not link retained types in |
| 408 | /// debug info metadata and module inline asm. |
| 409 | bool IsPerformingImport; |
Teresa Johnson | 4b9b379 | 2016-10-12 18:39:29 +0000 | [diff] [blame] | 410 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 411 | /// Set to true when all global value body linking is complete (including |
| 412 | /// lazy linking). Used to prevent metadata linking from creating new |
| 413 | /// references. |
| 414 | bool DoneLinkingBodies = false; |
| 415 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 416 | /// The Error encountered during materialization. We use an Optional here to |
| 417 | /// avoid needing to manage an unconsumed success value. |
| 418 | Optional<Error> FoundError; |
| 419 | void setError(Error E) { |
| 420 | if (E) |
| 421 | FoundError = std::move(E); |
| 422 | } |
| 423 | |
| 424 | /// Most of the errors produced by this module are inconvertible StringErrors. |
| 425 | /// This convenience function lets us return one of those more easily. |
| 426 | Error stringErr(const Twine &T) { |
| 427 | return make_error<StringError>(T, inconvertibleErrorCode()); |
| 428 | } |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 429 | |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 430 | /// Entry point for mapping values and alternate context for mapping aliases. |
| 431 | ValueMapper Mapper; |
| 432 | unsigned AliasMCID; |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 433 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 434 | /// Handles cloning of a global values from the source module into |
| 435 | /// the destination module, including setting the attributes and visibility. |
| 436 | GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition); |
| 437 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 438 | void emitWarning(const Twine &Message) { |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 439 | SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message)); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /// Given a global in the source module, return the global in the |
| 443 | /// destination module that is being linked to, if any. |
| 444 | GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) { |
| 445 | // If the source has no name it can't link. If it has local linkage, |
| 446 | // there is no name match-up going on. |
| 447 | if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) |
| 448 | return nullptr; |
| 449 | |
| 450 | // Otherwise see if we have a match in the destination module's symtab. |
| 451 | GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName()); |
| 452 | if (!DGV) |
| 453 | return nullptr; |
| 454 | |
| 455 | // If we found a global with the same name in the dest module, but it has |
| 456 | // internal linkage, we are really not doing any linkage here. |
| 457 | if (DGV->hasLocalLinkage()) |
| 458 | return nullptr; |
| 459 | |
| 460 | // Otherwise, we do in fact link to the destination global. |
| 461 | return DGV; |
| 462 | } |
| 463 | |
| 464 | void computeTypeMapping(); |
| 465 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 466 | Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV, |
| 467 | const GlobalVariable *SrcGV); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 468 | |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 469 | /// Given the GlobaValue \p SGV in the source module, and the matching |
| 470 | /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV |
| 471 | /// into the destination module. |
| 472 | /// |
| 473 | /// Note this code may call the client-provided \p AddLazyFor. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 474 | bool shouldLink(GlobalValue *DGV, GlobalValue &SGV); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 475 | Expected<Constant *> linkGlobalValueProto(GlobalValue *GV, bool ForAlias); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 476 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 477 | Error linkModuleFlagsMetadata(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 478 | |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 479 | void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 480 | Error linkFunctionBody(Function &Dst, Function &Src); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 481 | void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 482 | Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 483 | |
| 484 | /// Functions that take care of cloning a specific global value type |
| 485 | /// into the destination module. |
| 486 | GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar); |
| 487 | Function *copyFunctionProto(const Function *SF); |
| 488 | GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA); |
| 489 | |
Teresa Johnson | 040cc16 | 2016-12-12 16:09:30 +0000 | [diff] [blame] | 490 | /// When importing for ThinLTO, prevent importing of types listed on |
| 491 | /// the DICompileUnit that we don't need a copy of in the importing |
| 492 | /// module. |
| 493 | void prepareCompileUnitsForImport(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 494 | void linkNamedMDNodes(); |
| 495 | |
| 496 | public: |
Duncan P. N. Exon Smith | 565a0aa | 2016-04-17 23:30:31 +0000 | [diff] [blame] | 497 | IRLinker(Module &DstM, MDMapT &SharedMDs, |
| 498 | IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM, |
| 499 | ArrayRef<GlobalValue *> ValuesToLink, |
Teresa Johnson | 4b9b379 | 2016-10-12 18:39:29 +0000 | [diff] [blame] | 500 | std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor, |
Peter Collingbourne | e6fd9ff | 2017-02-03 17:01:14 +0000 | [diff] [blame] | 501 | bool IsPerformingImport) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 502 | : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)), |
| 503 | TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this), |
Peter Collingbourne | e6fd9ff | 2017-02-03 17:01:14 +0000 | [diff] [blame] | 504 | SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport), |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 505 | Mapper(ValueMap, RF_MoveDistinctMDs | RF_IgnoreMissingLocals, &TypeMap, |
| 506 | &GValMaterializer), |
| 507 | AliasMCID(Mapper.registerAlternateMappingContext(AliasValueMap, |
| 508 | &LValMaterializer)) { |
Duncan P. N. Exon Smith | a4810fa | 2016-04-19 16:57:24 +0000 | [diff] [blame] | 509 | ValueMap.getMDMap() = std::move(SharedMDs); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 510 | for (GlobalValue *GV : ValuesToLink) |
| 511 | maybeAdd(GV); |
Teresa Johnson | 040cc16 | 2016-12-12 16:09:30 +0000 | [diff] [blame] | 512 | if (IsPerformingImport) |
| 513 | prepareCompileUnitsForImport(); |
Teresa Johnson | cc42857 | 2015-12-30 19:32:24 +0000 | [diff] [blame] | 514 | } |
Duncan P. N. Exon Smith | a4810fa | 2016-04-19 16:57:24 +0000 | [diff] [blame] | 515 | ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); } |
Teresa Johnson | cc42857 | 2015-12-30 19:32:24 +0000 | [diff] [blame] | 516 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 517 | Error run(); |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 518 | Value *materialize(Value *V, bool ForAlias); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 519 | }; |
| 520 | } |
| 521 | |
| 522 | /// The LLVM SymbolTable class autorenames globals that conflict in the symbol |
| 523 | /// table. This is good for all clients except for us. Go through the trouble |
| 524 | /// to force this back. |
| 525 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
| 526 | // If the global doesn't force its name or if it already has the right name, |
| 527 | // there is nothing for us to do. |
| 528 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
| 529 | return; |
| 530 | |
| 531 | Module *M = GV->getParent(); |
| 532 | |
| 533 | // If there is a conflict, rename the conflict. |
| 534 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
| 535 | GV->takeName(ConflictGV); |
| 536 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
| 537 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work"); |
| 538 | } else { |
| 539 | GV->setName(Name); // Force the name back |
| 540 | } |
| 541 | } |
| 542 | |
Mehdi Amini | cc8c107 | 2016-05-25 21:03:21 +0000 | [diff] [blame] | 543 | Value *GlobalValueMaterializer::materialize(Value *SGV) { |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 544 | return TheIRLinker.materialize(SGV, false); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Mehdi Amini | cc8c107 | 2016-05-25 21:03:21 +0000 | [diff] [blame] | 547 | Value *LocalValueMaterializer::materialize(Value *SGV) { |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 548 | return TheIRLinker.materialize(SGV, true); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 551 | Value *IRLinker::materialize(Value *V, bool ForAlias) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 552 | auto *SGV = dyn_cast<GlobalValue>(V); |
| 553 | if (!SGV) |
| 554 | return nullptr; |
| 555 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 556 | Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForAlias); |
| 557 | if (!NewProto) { |
| 558 | setError(NewProto.takeError()); |
| 559 | return nullptr; |
| 560 | } |
| 561 | if (!*NewProto) |
| 562 | return nullptr; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 563 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 564 | GlobalValue *New = dyn_cast<GlobalValue>(*NewProto); |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 565 | if (!New) |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 566 | return *NewProto; |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 567 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 568 | // If we already created the body, just return. |
| 569 | if (auto *F = dyn_cast<Function>(New)) { |
| 570 | if (!F->isDeclaration()) |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 571 | return New; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 572 | } else if (auto *V = dyn_cast<GlobalVariable>(New)) { |
Duncan P. N. Exon Smith | 0fdaf8c | 2016-04-17 19:40:20 +0000 | [diff] [blame] | 573 | if (V->hasInitializer() || V->hasAppendingLinkage()) |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 574 | return New; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 575 | } else { |
| 576 | auto *A = cast<GlobalAlias>(New); |
| 577 | if (A->getAliasee()) |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 578 | return New; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Mehdi Amini | 3d4f3a0 | 2016-05-25 21:00:44 +0000 | [diff] [blame] | 581 | // When linking a global for an alias, it will always be linked. However we |
Adrian Prantl | 1f9ac96 | 2016-11-14 17:26:32 +0000 | [diff] [blame] | 582 | // need to check if it was not already scheduled to satisfy a reference from a |
Mehdi Amini | 3d4f3a0 | 2016-05-25 21:00:44 +0000 | [diff] [blame] | 583 | // regular global value initializer. We know if it has been schedule if the |
| 584 | // "New" GlobalValue that is mapped here for the alias is the same as the one |
| 585 | // already mapped. If there is an entry in the ValueMap but the value is |
| 586 | // different, it means that the value already had a definition in the |
| 587 | // destination module (linkonce for instance), but we need a new definition |
| 588 | // for the alias ("New" will be different. |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 589 | if (ForAlias && ValueMap.lookup(SGV) == New) |
| 590 | return New; |
Mehdi Amini | 3d4f3a0 | 2016-05-25 21:00:44 +0000 | [diff] [blame] | 591 | |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 592 | if (ForAlias || shouldLink(New, *SGV)) |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 593 | setError(linkGlobalValueBody(*New, *SGV)); |
Mehdi Amini | 53a6672 | 2016-05-25 21:01:51 +0000 | [diff] [blame] | 594 | |
| 595 | return New; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /// Loop through the global variables in the src module and merge them into the |
| 599 | /// dest module. |
| 600 | GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) { |
| 601 | // No linking to be performed or linking from the source: simply create an |
| 602 | // identical version of the symbol over in the dest module... the |
| 603 | // initializer will be filled in later by LinkGlobalInits. |
| 604 | GlobalVariable *NewDGV = |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 605 | new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()), |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 606 | SGVar->isConstant(), GlobalValue::ExternalLinkage, |
| 607 | /*init*/ nullptr, SGVar->getName(), |
| 608 | /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(), |
| 609 | SGVar->getType()->getAddressSpace()); |
| 610 | NewDGV->setAlignment(SGVar->getAlignment()); |
Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 611 | NewDGV->copyAttributesFrom(SGVar); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 612 | return NewDGV; |
| 613 | } |
| 614 | |
| 615 | /// Link the function in the source module into the destination module if |
| 616 | /// needed, setting up mapping information. |
| 617 | Function *IRLinker::copyFunctionProto(const Function *SF) { |
| 618 | // If there is no linkage to be performed or we are linking from the source, |
| 619 | // bring SF over. |
Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 620 | auto *F = |
| 621 | Function::Create(TypeMap.get(SF->getFunctionType()), |
| 622 | GlobalValue::ExternalLinkage, SF->getName(), &DstM); |
| 623 | F->copyAttributesFrom(SF); |
| 624 | return F; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /// Set up prototypes for any aliases that come over from the source module. |
| 628 | GlobalValue *IRLinker::copyGlobalAliasProto(const GlobalAlias *SGA) { |
| 629 | // If there is no linkage to be performed or we're linking from the source, |
| 630 | // bring over SGA. |
| 631 | auto *Ty = TypeMap.get(SGA->getValueType()); |
Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 632 | auto *GA = |
| 633 | GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(), |
| 634 | GlobalValue::ExternalLinkage, SGA->getName(), &DstM); |
| 635 | GA->copyAttributesFrom(SGA); |
| 636 | return GA; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV, |
| 640 | bool ForDefinition) { |
| 641 | GlobalValue *NewGV; |
| 642 | if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) { |
| 643 | NewGV = copyGlobalVariableProto(SGVar); |
| 644 | } else if (auto *SF = dyn_cast<Function>(SGV)) { |
| 645 | NewGV = copyFunctionProto(SF); |
| 646 | } else { |
| 647 | if (ForDefinition) |
| 648 | NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV)); |
Peter Collingbourne | 00f808f | 2017-08-10 01:07:44 +0000 | [diff] [blame] | 649 | else if (SGV->getValueType()->isFunctionTy()) |
| 650 | NewGV = |
| 651 | Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())), |
| 652 | GlobalValue::ExternalLinkage, SGV->getName(), &DstM); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 653 | else |
| 654 | NewGV = new GlobalVariable( |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 655 | DstM, TypeMap.get(SGV->getValueType()), |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 656 | /*isConstant*/ false, GlobalValue::ExternalLinkage, |
| 657 | /*init*/ nullptr, SGV->getName(), |
| 658 | /*insertbefore*/ nullptr, SGV->getThreadLocalMode(), |
| 659 | SGV->getType()->getAddressSpace()); |
| 660 | } |
| 661 | |
| 662 | if (ForDefinition) |
| 663 | NewGV->setLinkage(SGV->getLinkage()); |
Mehdi Amini | 113adde | 2016-04-19 16:11:05 +0000 | [diff] [blame] | 664 | else if (SGV->hasExternalWeakLinkage()) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 665 | NewGV->setLinkage(GlobalValue::ExternalWeakLinkage); |
| 666 | |
Peter Collingbourne | 4f7c16d | 2016-06-24 17:42:21 +0000 | [diff] [blame] | 667 | if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) { |
| 668 | // Metadata for global variables and function declarations is copied eagerly. |
| 669 | if (isa<GlobalVariable>(SGV) || SGV->isDeclaration()) |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 670 | NewGO->copyMetadata(cast<GlobalObject>(SGV), 0); |
Peter Collingbourne | 4f7c16d | 2016-06-24 17:42:21 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Teresa Johnson | 5fe4005 | 2016-01-12 00:24:24 +0000 | [diff] [blame] | 673 | // Remove these copied constants in case this stays a declaration, since |
| 674 | // they point to the source module. If the def is linked the values will |
| 675 | // be mapped in during linkFunctionBody. |
| 676 | if (auto *NewF = dyn_cast<Function>(NewGV)) { |
| 677 | NewF->setPersonalityFn(nullptr); |
| 678 | NewF->setPrefixData(nullptr); |
| 679 | NewF->setPrologueData(nullptr); |
| 680 | } |
| 681 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 682 | return NewGV; |
| 683 | } |
| 684 | |
| 685 | /// Loop over all of the linked values to compute type mappings. For example, |
| 686 | /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct |
| 687 | /// types 'Foo' but one got renamed when the module was loaded into the same |
| 688 | /// LLVMContext. |
| 689 | void IRLinker::computeTypeMapping() { |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 690 | for (GlobalValue &SGV : SrcM->globals()) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 691 | GlobalValue *DGV = getLinkedToGlobal(&SGV); |
| 692 | if (!DGV) |
| 693 | continue; |
| 694 | |
| 695 | if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) { |
| 696 | TypeMap.addTypeMapping(DGV->getType(), SGV.getType()); |
| 697 | continue; |
| 698 | } |
| 699 | |
| 700 | // Unify the element type of appending arrays. |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 701 | ArrayType *DAT = cast<ArrayType>(DGV->getValueType()); |
| 702 | ArrayType *SAT = cast<ArrayType>(SGV.getValueType()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 703 | TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); |
| 704 | } |
| 705 | |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 706 | for (GlobalValue &SGV : *SrcM) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 707 | if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) |
| 708 | TypeMap.addTypeMapping(DGV->getType(), SGV.getType()); |
| 709 | |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 710 | for (GlobalValue &SGV : SrcM->aliases()) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 711 | if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) |
| 712 | TypeMap.addTypeMapping(DGV->getType(), SGV.getType()); |
| 713 | |
| 714 | // Incorporate types by name, scanning all the types in the source module. |
| 715 | // At this point, the destination module may have a type "%foo = { i32 }" for |
| 716 | // example. When the source module got loaded into the same LLVMContext, if |
| 717 | // it had the same type, it would have been renamed to "%foo.42 = { i32 }". |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 718 | std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 719 | for (StructType *ST : Types) { |
| 720 | if (!ST->hasName()) |
| 721 | continue; |
| 722 | |
Hans Wennborg | aeacdc2 | 2016-11-18 17:33:05 +0000 | [diff] [blame] | 723 | if (TypeMap.DstStructTypesSet.hasType(ST)) { |
| 724 | // This is actually a type from the destination module. |
| 725 | // getIdentifiedStructTypes() can have found it by walking debug info |
| 726 | // metadata nodes, some of which get linked by name when ODR Type Uniquing |
| 727 | // is enabled on the Context, from the source to the destination module. |
| 728 | continue; |
| 729 | } |
| 730 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 731 | // Check to see if there is a dot in the name followed by a digit. |
| 732 | size_t DotPos = ST->getName().rfind('.'); |
| 733 | if (DotPos == 0 || DotPos == StringRef::npos || |
| 734 | ST->getName().back() == '.' || |
| 735 | !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1]))) |
| 736 | continue; |
| 737 | |
| 738 | // Check to see if the destination module has a struct with the prefix name. |
| 739 | StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos)); |
| 740 | if (!DST) |
| 741 | continue; |
| 742 | |
| 743 | // Don't use it if this actually came from the source module. They're in |
| 744 | // the same LLVMContext after all. Also don't use it unless the type is |
| 745 | // actually used in the destination module. This can happen in situations |
| 746 | // like this: |
| 747 | // |
| 748 | // Module A Module B |
| 749 | // -------- -------- |
| 750 | // %Z = type { %A } %B = type { %C.1 } |
| 751 | // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* } |
| 752 | // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] } |
| 753 | // %C = type { i8* } %B.3 = type { %C.1 } |
| 754 | // |
| 755 | // When we link Module B with Module A, the '%B' in Module B is |
| 756 | // used. However, that would then use '%C.1'. But when we process '%C.1', |
| 757 | // we prefer to take the '%C' version. So we are then left with both |
| 758 | // '%C.1' and '%C' being used for the same types. This leads to some |
| 759 | // variables using one type and some using the other. |
| 760 | if (TypeMap.DstStructTypesSet.hasType(DST)) |
| 761 | TypeMap.addTypeMapping(DST, ST); |
| 762 | } |
| 763 | |
| 764 | // Now that we have discovered all of the type equivalences, get a body for |
| 765 | // any 'opaque' types in the dest module that are now resolved. |
| 766 | TypeMap.linkDefinedTypeBodies(); |
| 767 | } |
| 768 | |
| 769 | static void getArrayElements(const Constant *C, |
| 770 | SmallVectorImpl<Constant *> &Dest) { |
| 771 | unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements(); |
| 772 | |
| 773 | for (unsigned i = 0; i != NumElements; ++i) |
| 774 | Dest.push_back(C->getAggregateElement(i)); |
| 775 | } |
| 776 | |
| 777 | /// If there were any appending global variables, link them together now. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 778 | Expected<Constant *> |
| 779 | IRLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
| 780 | const GlobalVariable *SrcGV) { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 781 | Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType())) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 782 | ->getElementType(); |
| 783 | |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 784 | // FIXME: This upgrade is done during linking to support the C API. Once the |
| 785 | // old form is deprecated, we should move this upgrade to |
| 786 | // llvm::UpgradeGlobalVariable() and simplify the logic here and in |
| 787 | // Mapper::mapAppendingVariable() in ValueMapper.cpp. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 788 | StringRef Name = SrcGV->getName(); |
| 789 | bool IsNewStructor = false; |
| 790 | bool IsOldStructor = false; |
| 791 | if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") { |
| 792 | if (cast<StructType>(EltTy)->getNumElements() == 3) |
| 793 | IsNewStructor = true; |
| 794 | else |
| 795 | IsOldStructor = true; |
| 796 | } |
| 797 | |
| 798 | PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo(); |
| 799 | if (IsOldStructor) { |
| 800 | auto &ST = *cast<StructType>(EltTy); |
| 801 | Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy}; |
| 802 | EltTy = StructType::get(SrcGV->getContext(), Tys, false); |
| 803 | } |
| 804 | |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 805 | uint64_t DstNumElements = 0; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 806 | if (DstGV) { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 807 | ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType()); |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 808 | DstNumElements = DstTy->getNumElements(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 809 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 810 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) |
| 811 | return stringErr( |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 812 | "Linking globals named '" + SrcGV->getName() + |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 813 | "': can only link appending global with another appending " |
| 814 | "global!"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 815 | |
| 816 | // Check to see that they two arrays agree on type. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 817 | if (EltTy != DstTy->getElementType()) |
| 818 | return stringErr("Appending variables with different element types!"); |
| 819 | if (DstGV->isConstant() != SrcGV->isConstant()) |
| 820 | return stringErr("Appending variables linked with different const'ness!"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 821 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 822 | if (DstGV->getAlignment() != SrcGV->getAlignment()) |
| 823 | return stringErr( |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 824 | "Appending variables with different alignment need to be linked!"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 825 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 826 | if (DstGV->getVisibility() != SrcGV->getVisibility()) |
| 827 | return stringErr( |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 828 | "Appending variables with different visibility need to be linked!"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 829 | |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 830 | if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr()) |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 831 | return stringErr( |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 832 | "Appending variables with different unnamed_addr need to be linked!"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 833 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 834 | if (DstGV->getSection() != SrcGV->getSection()) |
| 835 | return stringErr( |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 836 | "Appending variables with different section name need to be linked!"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 839 | SmallVector<Constant *, 16> SrcElements; |
| 840 | getArrayElements(SrcGV->getInitializer(), SrcElements); |
| 841 | |
Justin Bogner | 375f71e | 2016-08-15 22:41:42 +0000 | [diff] [blame] | 842 | if (IsNewStructor) { |
| 843 | auto It = remove_if(SrcElements, [this](Constant *E) { |
| 844 | auto *Key = |
| 845 | dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts()); |
| 846 | if (!Key) |
| 847 | return false; |
| 848 | GlobalValue *DGV = getLinkedToGlobal(Key); |
| 849 | return !shouldLink(DGV, *Key); |
| 850 | }); |
| 851 | SrcElements.erase(It, SrcElements.end()); |
| 852 | } |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 853 | uint64_t NewSize = DstNumElements + SrcElements.size(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 854 | ArrayType *NewType = ArrayType::get(EltTy, NewSize); |
| 855 | |
| 856 | // Create the new global variable. |
| 857 | GlobalVariable *NG = new GlobalVariable( |
| 858 | DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(), |
| 859 | /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(), |
| 860 | SrcGV->getType()->getAddressSpace()); |
| 861 | |
| 862 | NG->copyAttributesFrom(SrcGV); |
| 863 | forceRenaming(NG, SrcGV->getName()); |
| 864 | |
| 865 | Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); |
| 866 | |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 867 | Mapper.scheduleMapAppendingVariable(*NG, |
| 868 | DstGV ? DstGV->getInitializer() : nullptr, |
| 869 | IsOldStructor, SrcElements); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 870 | |
| 871 | // Replace any uses of the two global variables with uses of the new |
| 872 | // global. |
| 873 | if (DstGV) { |
| 874 | DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType())); |
| 875 | DstGV->eraseFromParent(); |
| 876 | } |
| 877 | |
| 878 | return Ret; |
| 879 | } |
| 880 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 881 | bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) { |
Davide Italiano | 9533965 | 2016-06-07 14:55:04 +0000 | [diff] [blame] | 882 | if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage()) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 883 | return true; |
| 884 | |
Rafael Espindola | 55a7ae5 | 2016-01-20 22:38:23 +0000 | [diff] [blame] | 885 | if (DGV && !DGV->isDeclarationForLinker()) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 886 | return false; |
| 887 | |
Davide Italiano | 9533965 | 2016-06-07 14:55:04 +0000 | [diff] [blame] | 888 | if (SGV.isDeclaration() || DoneLinkingBodies) |
Rafael Espindola | 15ca14c | 2016-04-21 14:56:33 +0000 | [diff] [blame] | 889 | return false; |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 890 | |
| 891 | // Callback to the client to give a chance to lazily add the Global to the |
| 892 | // list of value to link. |
| 893 | bool LazilyAdded = false; |
| 894 | AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) { |
| 895 | maybeAdd(&GV); |
| 896 | LazilyAdded = true; |
| 897 | }); |
| 898 | return LazilyAdded; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 901 | Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV, |
| 902 | bool ForAlias) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 903 | GlobalValue *DGV = getLinkedToGlobal(SGV); |
| 904 | |
| 905 | bool ShouldLink = shouldLink(DGV, *SGV); |
| 906 | |
| 907 | // just missing from map |
| 908 | if (ShouldLink) { |
| 909 | auto I = ValueMap.find(SGV); |
| 910 | if (I != ValueMap.end()) |
| 911 | return cast<Constant>(I->second); |
| 912 | |
| 913 | I = AliasValueMap.find(SGV); |
| 914 | if (I != AliasValueMap.end()) |
| 915 | return cast<Constant>(I->second); |
| 916 | } |
| 917 | |
Mehdi Amini | 3366107 | 2016-03-11 22:19:06 +0000 | [diff] [blame] | 918 | if (!ShouldLink && ForAlias) |
| 919 | DGV = nullptr; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 920 | |
| 921 | // Handle the ultra special appending linkage case first. |
| 922 | assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage()); |
| 923 | if (SGV->hasAppendingLinkage()) |
| 924 | return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV), |
| 925 | cast<GlobalVariable>(SGV)); |
| 926 | |
| 927 | GlobalValue *NewGV; |
Rafael Espindola | 55a7ae5 | 2016-01-20 22:38:23 +0000 | [diff] [blame] | 928 | if (DGV && !ShouldLink) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 929 | NewGV = DGV; |
| 930 | } else { |
| 931 | // If we are done linking global value bodies (i.e. we are performing |
| 932 | // metadata linking), don't link in the global value due to this |
| 933 | // reference, simply map it to null. |
| 934 | if (DoneLinkingBodies) |
| 935 | return nullptr; |
| 936 | |
| 937 | NewGV = copyGlobalValueProto(SGV, ShouldLink); |
Evgeniy Stepanov | 9fb70f5 | 2016-01-20 22:05:50 +0000 | [diff] [blame] | 938 | if (ShouldLink || !ForAlias) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 939 | forceRenaming(NewGV, SGV->getName()); |
| 940 | } |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 941 | |
| 942 | // Overloaded intrinsics have overloaded types names as part of their |
| 943 | // names. If we renamed overloaded types we should rename the intrinsic |
| 944 | // as well. |
| 945 | if (Function *F = dyn_cast<Function>(NewGV)) |
| 946 | if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) |
| 947 | NewGV = Remangled.getValue(); |
| 948 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 949 | if (ShouldLink || ForAlias) { |
| 950 | if (const Comdat *SC = SGV->getComdat()) { |
| 951 | if (auto *GO = dyn_cast<GlobalObject>(NewGV)) { |
| 952 | Comdat *DC = DstM.getOrInsertComdat(SC->getName()); |
| 953 | DC->setSelectionKind(SC->getSelectionKind()); |
| 954 | GO->setComdat(DC); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (!ShouldLink && ForAlias) |
| 960 | NewGV->setLinkage(GlobalValue::InternalLinkage); |
| 961 | |
| 962 | Constant *C = NewGV; |
Teresa Johnson | ba22da0d | 2018-01-09 18:32:53 +0000 | [diff] [blame] | 963 | // Only create a bitcast if necessary. In particular, with |
| 964 | // DebugTypeODRUniquing we may reach metadata in the destination module |
| 965 | // containing a GV from the source module, in which case SGV will be |
| 966 | // the same as DGV and NewGV, and TypeMap.get() will assert since it |
| 967 | // assumes it is being invoked on a type in the source module. |
| 968 | if (DGV && NewGV != SGV) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 969 | C = ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType())); |
| 970 | |
| 971 | if (DGV && NewGV != DGV) { |
| 972 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType())); |
| 973 | DGV->eraseFromParent(); |
| 974 | } |
| 975 | |
| 976 | return C; |
| 977 | } |
| 978 | |
| 979 | /// Update the initializers in the Dest module now that all globals that may be |
| 980 | /// referenced are in Dest. |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 981 | void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 982 | // Figure out what the initializer looks like in the dest module. |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 983 | Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /// Copy the source function over into the dest function and fix up references |
| 987 | /// to values. At this point we know that Dest is an external function, and |
| 988 | /// that Src is not. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 989 | Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 990 | assert(Dst.isDeclaration() && !Src.isDeclaration()); |
| 991 | |
| 992 | // Materialize if needed. |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 993 | if (Error Err = Src.materialize()) |
| 994 | return Err; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 995 | |
Duncan P. N. Exon Smith | bb2c3e1 | 2016-04-08 19:26:32 +0000 | [diff] [blame] | 996 | // Link in the operands without remapping. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 997 | if (Src.hasPrefixData()) |
Duncan P. N. Exon Smith | bb2c3e1 | 2016-04-08 19:26:32 +0000 | [diff] [blame] | 998 | Dst.setPrefixData(Src.getPrefixData()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 999 | if (Src.hasPrologueData()) |
Duncan P. N. Exon Smith | bb2c3e1 | 2016-04-08 19:26:32 +0000 | [diff] [blame] | 1000 | Dst.setPrologueData(Src.getPrologueData()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1001 | if (Src.hasPersonalityFn()) |
Duncan P. N. Exon Smith | bb2c3e1 | 2016-04-08 19:26:32 +0000 | [diff] [blame] | 1002 | Dst.setPersonalityFn(Src.getPersonalityFn()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1003 | |
Duncan P. N. Exon Smith | bb2c3e1 | 2016-04-08 19:26:32 +0000 | [diff] [blame] | 1004 | // Copy over the metadata attachments without remapping. |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1005 | Dst.copyMetadata(&Src, 0); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1006 | |
Duncan P. N. Exon Smith | bdfc984 | 2016-04-06 06:38:15 +0000 | [diff] [blame] | 1007 | // Steal arguments and splice the body of Src into Dst. |
| 1008 | Dst.stealArgumentListFrom(Src); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1009 | Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList()); |
| 1010 | |
Duncan P. N. Exon Smith | bb2c3e1 | 2016-04-08 19:26:32 +0000 | [diff] [blame] | 1011 | // Everything has been moved over. Remap it. |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 1012 | Mapper.scheduleRemapFunction(Dst); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1013 | return Error::success(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | void IRLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) { |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 1017 | Mapper.scheduleMapGlobalAliasee(Dst, *Src.getAliasee(), AliasMCID); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1020 | Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1021 | if (auto *F = dyn_cast<Function>(&Src)) |
| 1022 | return linkFunctionBody(cast<Function>(Dst), *F); |
| 1023 | if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) { |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 1024 | linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1025 | return Error::success(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1026 | } |
| 1027 | linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src)); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1028 | return Error::success(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Teresa Johnson | 040cc16 | 2016-12-12 16:09:30 +0000 | [diff] [blame] | 1031 | void IRLinker::prepareCompileUnitsForImport() { |
| 1032 | NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu"); |
| 1033 | if (!SrcCompileUnits) |
| 1034 | return; |
| 1035 | // When importing for ThinLTO, prevent importing of types listed on |
| 1036 | // the DICompileUnit that we don't need a copy of in the importing |
| 1037 | // module. They will be emitted by the originating module. |
| 1038 | for (unsigned I = 0, E = SrcCompileUnits->getNumOperands(); I != E; ++I) { |
| 1039 | auto *CU = cast<DICompileUnit>(SrcCompileUnits->getOperand(I)); |
| 1040 | assert(CU && "Expected valid compile unit"); |
| 1041 | // Enums, macros, and retained types don't need to be listed on the |
| 1042 | // imported DICompileUnit. This means they will only be imported |
| 1043 | // if reached from the mapped IR. Do this by setting their value map |
| 1044 | // entries to nullptr, which will automatically prevent their importing |
| 1045 | // when reached from the DICompileUnit during metadata mapping. |
| 1046 | ValueMap.MD()[CU->getRawEnumTypes()].reset(nullptr); |
| 1047 | ValueMap.MD()[CU->getRawMacros()].reset(nullptr); |
| 1048 | ValueMap.MD()[CU->getRawRetainedTypes()].reset(nullptr); |
| 1049 | // If we ever start importing global variable defs, we'll need to |
| 1050 | // add their DIGlobalVariable to the globals list on the imported |
| 1051 | // DICompileUnit. Confirm none are imported, and then we can |
| 1052 | // map the list of global variables to nullptr. |
| 1053 | assert(none_of( |
| 1054 | ValuesToLink, |
| 1055 | [](const GlobalValue *GV) { return isa<GlobalVariable>(GV); }) && |
| 1056 | "Unexpected importing of a GlobalVariable definition"); |
| 1057 | ValueMap.MD()[CU->getRawGlobalVariables()].reset(nullptr); |
| 1058 | |
| 1059 | // Imported entities only need to be mapped in if they have local |
| 1060 | // scope, as those might correspond to an imported entity inside a |
| 1061 | // function being imported (any locally scoped imported entities that |
| 1062 | // don't end up referenced by an imported function will not be emitted |
| 1063 | // into the object). Imported entities not in a local scope |
| 1064 | // (e.g. on the namespace) only need to be emitted by the originating |
| 1065 | // module. Create a list of the locally scoped imported entities, and |
| 1066 | // replace the source CUs imported entity list with the new list, so |
| 1067 | // only those are mapped in. |
| 1068 | // FIXME: Locally-scoped imported entities could be moved to the |
| 1069 | // functions they are local to instead of listing them on the CU, and |
| 1070 | // we would naturally only link in those needed by function importing. |
| 1071 | SmallVector<TrackingMDNodeRef, 4> AllImportedModules; |
| 1072 | bool ReplaceImportedEntities = false; |
| 1073 | for (auto *IE : CU->getImportedEntities()) { |
| 1074 | DIScope *Scope = IE->getScope(); |
| 1075 | assert(Scope && "Invalid Scope encoding!"); |
| 1076 | if (isa<DILocalScope>(Scope)) |
| 1077 | AllImportedModules.emplace_back(IE); |
| 1078 | else |
| 1079 | ReplaceImportedEntities = true; |
| 1080 | } |
| 1081 | if (ReplaceImportedEntities) { |
| 1082 | if (!AllImportedModules.empty()) |
| 1083 | CU->replaceImportedEntities(MDTuple::get( |
| 1084 | CU->getContext(), |
| 1085 | SmallVector<Metadata *, 16>(AllImportedModules.begin(), |
| 1086 | AllImportedModules.end()))); |
| 1087 | else |
| 1088 | // If there were no local scope imported entities, we can map |
| 1089 | // the whole list to nullptr. |
| 1090 | ValueMap.MD()[CU->getRawImportedEntities()].reset(nullptr); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1095 | /// Insert all of the named MDNodes in Src into the Dest module. |
| 1096 | void IRLinker::linkNamedMDNodes() { |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1097 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
| 1098 | for (const NamedMDNode &NMD : SrcM->named_metadata()) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1099 | // Don't link module flags here. Do them separately. |
| 1100 | if (&NMD == SrcModFlags) |
| 1101 | continue; |
| 1102 | NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName()); |
| 1103 | // Add Src elements into Dest node. |
Duncan P. N. Exon Smith | 8a15dab | 2016-04-15 23:32:44 +0000 | [diff] [blame] | 1104 | for (const MDNode *Op : NMD.operands()) |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 1105 | DestNMD->addOperand(Mapper.mapMDNode(*Op)); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /// Merge the linker flags in Src into the Dest module. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1110 | Error IRLinker::linkModuleFlagsMetadata() { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1111 | // If the source module has no module flags, we are done. |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1112 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1113 | if (!SrcModFlags) |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1114 | return Error::success(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1115 | |
| 1116 | // If the destination module doesn't have module flags yet, then just copy |
| 1117 | // over the source module's flags. |
| 1118 | NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata(); |
| 1119 | if (DstModFlags->getNumOperands() == 0) { |
| 1120 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) |
| 1121 | DstModFlags->addOperand(SrcModFlags->getOperand(I)); |
| 1122 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1123 | return Error::success(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | // First build a map of the existing module flags and requirements. |
| 1127 | DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags; |
| 1128 | SmallSetVector<MDNode *, 16> Requirements; |
| 1129 | for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) { |
| 1130 | MDNode *Op = DstModFlags->getOperand(I); |
| 1131 | ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0)); |
| 1132 | MDString *ID = cast<MDString>(Op->getOperand(1)); |
| 1133 | |
| 1134 | if (Behavior->getZExtValue() == Module::Require) { |
| 1135 | Requirements.insert(cast<MDNode>(Op->getOperand(2))); |
| 1136 | } else { |
| 1137 | Flags[ID] = std::make_pair(Op, I); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | // Merge in the flags from the source module, and also collect its set of |
| 1142 | // requirements. |
| 1143 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) { |
| 1144 | MDNode *SrcOp = SrcModFlags->getOperand(I); |
| 1145 | ConstantInt *SrcBehavior = |
| 1146 | mdconst::extract<ConstantInt>(SrcOp->getOperand(0)); |
| 1147 | MDString *ID = cast<MDString>(SrcOp->getOperand(1)); |
| 1148 | MDNode *DstOp; |
| 1149 | unsigned DstIndex; |
| 1150 | std::tie(DstOp, DstIndex) = Flags.lookup(ID); |
| 1151 | unsigned SrcBehaviorValue = SrcBehavior->getZExtValue(); |
| 1152 | |
| 1153 | // If this is a requirement, add it and continue. |
| 1154 | if (SrcBehaviorValue == Module::Require) { |
| 1155 | // If the destination module does not already have this requirement, add |
| 1156 | // it. |
| 1157 | if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) { |
| 1158 | DstModFlags->addOperand(SrcOp); |
| 1159 | } |
| 1160 | continue; |
| 1161 | } |
| 1162 | |
| 1163 | // If there is no existing flag with this ID, just add it. |
| 1164 | if (!DstOp) { |
| 1165 | Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands()); |
| 1166 | DstModFlags->addOperand(SrcOp); |
| 1167 | continue; |
| 1168 | } |
| 1169 | |
| 1170 | // Otherwise, perform a merge. |
| 1171 | ConstantInt *DstBehavior = |
| 1172 | mdconst::extract<ConstantInt>(DstOp->getOperand(0)); |
| 1173 | unsigned DstBehaviorValue = DstBehavior->getZExtValue(); |
| 1174 | |
Teresa Johnson | 2db1369 | 2017-05-23 00:08:00 +0000 | [diff] [blame] | 1175 | auto overrideDstValue = [&]() { |
| 1176 | DstModFlags->setOperand(DstIndex, SrcOp); |
| 1177 | Flags[ID].first = SrcOp; |
| 1178 | }; |
| 1179 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1180 | // If either flag has override behavior, handle it first. |
| 1181 | if (DstBehaviorValue == Module::Override) { |
| 1182 | // Diagnose inconsistent flags which both have override behavior. |
| 1183 | if (SrcBehaviorValue == Module::Override && |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1184 | SrcOp->getOperand(2) != DstOp->getOperand(2)) |
| 1185 | return stringErr("linking module flags '" + ID->getString() + |
| 1186 | "': IDs have conflicting override values"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1187 | continue; |
| 1188 | } else if (SrcBehaviorValue == Module::Override) { |
| 1189 | // Update the destination flag to that of the source. |
Teresa Johnson | 2db1369 | 2017-05-23 00:08:00 +0000 | [diff] [blame] | 1190 | overrideDstValue(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1191 | continue; |
| 1192 | } |
| 1193 | |
| 1194 | // Diagnose inconsistent merge behavior types. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1195 | if (SrcBehaviorValue != DstBehaviorValue) |
| 1196 | return stringErr("linking module flags '" + ID->getString() + |
| 1197 | "': IDs have conflicting behaviors"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1198 | |
| 1199 | auto replaceDstValue = [&](MDNode *New) { |
| 1200 | Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New}; |
| 1201 | MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps); |
| 1202 | DstModFlags->setOperand(DstIndex, Flag); |
| 1203 | Flags[ID].first = Flag; |
| 1204 | }; |
| 1205 | |
| 1206 | // Perform the merge for standard behavior types. |
| 1207 | switch (SrcBehaviorValue) { |
| 1208 | case Module::Require: |
| 1209 | case Module::Override: |
| 1210 | llvm_unreachable("not possible"); |
| 1211 | case Module::Error: { |
| 1212 | // Emit an error if the values differ. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1213 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) |
| 1214 | return stringErr("linking module flags '" + ID->getString() + |
| 1215 | "': IDs have conflicting values"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1216 | continue; |
| 1217 | } |
| 1218 | case Module::Warning: { |
| 1219 | // Emit a warning if the values differ. |
| 1220 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1221 | emitWarning("linking module flags '" + ID->getString() + |
| 1222 | "': IDs have conflicting values"); |
| 1223 | } |
| 1224 | continue; |
| 1225 | } |
Teresa Johnson | 2db1369 | 2017-05-23 00:08:00 +0000 | [diff] [blame] | 1226 | case Module::Max: { |
| 1227 | ConstantInt *DstValue = |
| 1228 | mdconst::extract<ConstantInt>(DstOp->getOperand(2)); |
| 1229 | ConstantInt *SrcValue = |
| 1230 | mdconst::extract<ConstantInt>(SrcOp->getOperand(2)); |
| 1231 | if (SrcValue->getZExtValue() > DstValue->getZExtValue()) |
| 1232 | overrideDstValue(); |
| 1233 | break; |
| 1234 | } |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1235 | case Module::Append: { |
| 1236 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1237 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
| 1238 | SmallVector<Metadata *, 8> MDs; |
| 1239 | MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands()); |
| 1240 | MDs.append(DstValue->op_begin(), DstValue->op_end()); |
| 1241 | MDs.append(SrcValue->op_begin(), SrcValue->op_end()); |
| 1242 | |
| 1243 | replaceDstValue(MDNode::get(DstM.getContext(), MDs)); |
| 1244 | break; |
| 1245 | } |
| 1246 | case Module::AppendUnique: { |
| 1247 | SmallSetVector<Metadata *, 16> Elts; |
| 1248 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1249 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
| 1250 | Elts.insert(DstValue->op_begin(), DstValue->op_end()); |
| 1251 | Elts.insert(SrcValue->op_begin(), SrcValue->op_end()); |
| 1252 | |
| 1253 | replaceDstValue(MDNode::get(DstM.getContext(), |
| 1254 | makeArrayRef(Elts.begin(), Elts.end()))); |
| 1255 | break; |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | // Check all of the requirements. |
| 1261 | for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { |
| 1262 | MDNode *Requirement = Requirements[I]; |
| 1263 | MDString *Flag = cast<MDString>(Requirement->getOperand(0)); |
| 1264 | Metadata *ReqValue = Requirement->getOperand(1); |
| 1265 | |
| 1266 | MDNode *Op = Flags[Flag].first; |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1267 | if (!Op || Op->getOperand(2) != ReqValue) |
| 1268 | return stringErr("linking module flags '" + Flag->getString() + |
| 1269 | "': does not have the required value"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1270 | } |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1271 | return Error::success(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Florian Hahn | 745266b | 2017-07-12 11:52:28 +0000 | [diff] [blame] | 1274 | /// Return InlineAsm adjusted with target-specific directives if required. |
| 1275 | /// For ARM and Thumb, we have to add directives to select the appropriate ISA |
| 1276 | /// to support mixing module-level inline assembly from ARM and Thumb modules. |
| 1277 | static std::string adjustInlineAsm(const std::string &InlineAsm, |
| 1278 | const Triple &Triple) { |
| 1279 | if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb) |
| 1280 | return ".text\n.balign 2\n.thumb\n" + InlineAsm; |
| 1281 | if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb) |
| 1282 | return ".text\n.balign 4\n.arm\n" + InlineAsm; |
| 1283 | return InlineAsm; |
| 1284 | } |
| 1285 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1286 | Error IRLinker::run() { |
Teresa Johnson | 0556e22 | 2016-03-10 18:47:03 +0000 | [diff] [blame] | 1287 | // Ensure metadata materialized before value mapping. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1288 | if (SrcM->getMaterializer()) |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 1289 | if (Error Err = SrcM->getMaterializer()->materializeMetadata()) |
| 1290 | return Err; |
Teresa Johnson | 0556e22 | 2016-03-10 18:47:03 +0000 | [diff] [blame] | 1291 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1292 | // Inherit the target data from the source module if the destination module |
| 1293 | // doesn't have one already. |
| 1294 | if (DstM.getDataLayout().isDefault()) |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1295 | DstM.setDataLayout(SrcM->getDataLayout()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1296 | |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1297 | if (SrcM->getDataLayout() != DstM.getDataLayout()) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1298 | emitWarning("Linking two modules of different data layouts: '" + |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1299 | SrcM->getModuleIdentifier() + "' is '" + |
| 1300 | SrcM->getDataLayoutStr() + "' whereas '" + |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1301 | DstM.getModuleIdentifier() + "' is '" + |
| 1302 | DstM.getDataLayoutStr() + "'\n"); |
| 1303 | } |
| 1304 | |
| 1305 | // Copy the target triple from the source to dest if the dest's is empty. |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1306 | if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) |
| 1307 | DstM.setTargetTriple(SrcM->getTargetTriple()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1308 | |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1309 | Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple()); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1310 | |
Akira Hatanaka | b10bff1 | 2017-05-18 03:52:29 +0000 | [diff] [blame] | 1311 | if (!SrcM->getTargetTriple().empty()&& |
| 1312 | !SrcTriple.isCompatibleWith(DstTriple)) |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1313 | emitWarning("Linking two modules of different target triples: " + |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1314 | SrcM->getModuleIdentifier() + "' is '" + |
| 1315 | SrcM->getTargetTriple() + "' whereas '" + |
| 1316 | DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() + |
| 1317 | "'\n"); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1318 | |
Akira Hatanaka | b10bff1 | 2017-05-18 03:52:29 +0000 | [diff] [blame] | 1319 | DstM.setTargetTriple(SrcTriple.merge(DstTriple)); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1320 | |
| 1321 | // Append the module inline asm string. |
Peter Collingbourne | e6fd9ff | 2017-02-03 17:01:14 +0000 | [diff] [blame] | 1322 | if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) { |
Florian Hahn | 745266b | 2017-07-12 11:52:28 +0000 | [diff] [blame] | 1323 | std::string SrcModuleInlineAsm = adjustInlineAsm(SrcM->getModuleInlineAsm(), |
| 1324 | SrcTriple); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1325 | if (DstM.getModuleInlineAsm().empty()) |
Florian Hahn | 745266b | 2017-07-12 11:52:28 +0000 | [diff] [blame] | 1326 | DstM.setModuleInlineAsm(SrcModuleInlineAsm); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1327 | else |
| 1328 | DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" + |
Florian Hahn | 745266b | 2017-07-12 11:52:28 +0000 | [diff] [blame] | 1329 | SrcModuleInlineAsm); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | // Loop over all of the linked values to compute type mappings. |
| 1333 | computeTypeMapping(); |
| 1334 | |
| 1335 | std::reverse(Worklist.begin(), Worklist.end()); |
| 1336 | while (!Worklist.empty()) { |
| 1337 | GlobalValue *GV = Worklist.back(); |
| 1338 | Worklist.pop_back(); |
| 1339 | |
| 1340 | // Already mapped. |
| 1341 | if (ValueMap.find(GV) != ValueMap.end() || |
| 1342 | AliasValueMap.find(GV) != AliasValueMap.end()) |
| 1343 | continue; |
| 1344 | |
| 1345 | assert(!GV->isDeclaration()); |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 1346 | Mapper.mapValue(*GV); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1347 | if (FoundError) |
| 1348 | return std::move(*FoundError); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | // Note that we are done linking global value bodies. This prevents |
| 1352 | // metadata linking from creating new references. |
| 1353 | DoneLinkingBodies = true; |
Duncan P. N. Exon Smith | 39423b0 | 2016-04-16 02:29:55 +0000 | [diff] [blame] | 1354 | Mapper.addFlags(RF_NullMapMissingGlobalValues); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Remap all of the named MDNodes in Src into the DstM module. We do this |
| 1357 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
| 1358 | // are properly remapped. |
Teresa Johnson | b703c77 | 2016-03-29 18:24:19 +0000 | [diff] [blame] | 1359 | linkNamedMDNodes(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1360 | |
Teresa Johnson | b703c77 | 2016-03-29 18:24:19 +0000 | [diff] [blame] | 1361 | // Merge the module flags into the DstM module. |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1362 | return linkModuleFlagsMetadata(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P) |
| 1366 | : ETypes(E), IsPacked(P) {} |
| 1367 | |
| 1368 | IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST) |
| 1369 | : ETypes(ST->elements()), IsPacked(ST->isPacked()) {} |
| 1370 | |
| 1371 | bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const { |
Davide Italiano | 9533965 | 2016-06-07 14:55:04 +0000 | [diff] [blame] | 1372 | return IsPacked == That.IsPacked && ETypes == That.ETypes; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const { |
| 1376 | return !this->operator==(That); |
| 1377 | } |
| 1378 | |
| 1379 | StructType *IRMover::StructTypeKeyInfo::getEmptyKey() { |
| 1380 | return DenseMapInfo<StructType *>::getEmptyKey(); |
| 1381 | } |
| 1382 | |
| 1383 | StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() { |
| 1384 | return DenseMapInfo<StructType *>::getTombstoneKey(); |
| 1385 | } |
| 1386 | |
| 1387 | unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) { |
| 1388 | return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()), |
| 1389 | Key.IsPacked); |
| 1390 | } |
| 1391 | |
| 1392 | unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) { |
| 1393 | return getHashValue(KeyTy(ST)); |
| 1394 | } |
| 1395 | |
| 1396 | bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS, |
| 1397 | const StructType *RHS) { |
| 1398 | if (RHS == getEmptyKey() || RHS == getTombstoneKey()) |
| 1399 | return false; |
| 1400 | return LHS == KeyTy(RHS); |
| 1401 | } |
| 1402 | |
| 1403 | bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS, |
| 1404 | const StructType *RHS) { |
Davide Italiano | 9533965 | 2016-06-07 14:55:04 +0000 | [diff] [blame] | 1405 | if (RHS == getEmptyKey() || RHS == getTombstoneKey()) |
| 1406 | return LHS == RHS; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1407 | return KeyTy(LHS) == KeyTy(RHS); |
| 1408 | } |
| 1409 | |
| 1410 | void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) { |
| 1411 | assert(!Ty->isOpaque()); |
| 1412 | NonOpaqueStructTypes.insert(Ty); |
| 1413 | } |
| 1414 | |
| 1415 | void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) { |
| 1416 | assert(!Ty->isOpaque()); |
| 1417 | NonOpaqueStructTypes.insert(Ty); |
| 1418 | bool Removed = OpaqueStructTypes.erase(Ty); |
| 1419 | (void)Removed; |
| 1420 | assert(Removed); |
| 1421 | } |
| 1422 | |
| 1423 | void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) { |
| 1424 | assert(Ty->isOpaque()); |
| 1425 | OpaqueStructTypes.insert(Ty); |
| 1426 | } |
| 1427 | |
| 1428 | StructType * |
| 1429 | IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes, |
| 1430 | bool IsPacked) { |
| 1431 | IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked); |
| 1432 | auto I = NonOpaqueStructTypes.find_as(Key); |
Davide Italiano | 9533965 | 2016-06-07 14:55:04 +0000 | [diff] [blame] | 1433 | return I == NonOpaqueStructTypes.end() ? nullptr : *I; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) { |
| 1437 | if (Ty->isOpaque()) |
| 1438 | return OpaqueStructTypes.count(Ty); |
| 1439 | auto I = NonOpaqueStructTypes.find(Ty); |
Davide Italiano | 9533965 | 2016-06-07 14:55:04 +0000 | [diff] [blame] | 1440 | return I == NonOpaqueStructTypes.end() ? false : *I == Ty; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 1443 | IRMover::IRMover(Module &M) : Composite(M) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1444 | TypeFinder StructTypes; |
Mehdi Amini | fec2158 | 2016-11-19 18:44:16 +0000 | [diff] [blame] | 1445 | StructTypes.run(M, /* OnlyNamed */ false); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1446 | for (StructType *Ty : StructTypes) { |
| 1447 | if (Ty->isOpaque()) |
| 1448 | IdentifiedStructTypes.addOpaque(Ty); |
| 1449 | else |
| 1450 | IdentifiedStructTypes.addNonOpaque(Ty); |
| 1451 | } |
Mehdi Amini | ebb3434 | 2016-09-03 21:12:33 +0000 | [diff] [blame] | 1452 | // Self-map metadatas in the destination module. This is needed when |
| 1453 | // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the |
| 1454 | // destination module may be reached from the source module. |
| 1455 | for (auto *MD : StructTypes.getVisitedMetadata()) { |
| 1456 | SharedMDs[MD].reset(const_cast<MDNode *>(MD)); |
| 1457 | } |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1460 | Error IRMover::move( |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 1461 | std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink, |
Teresa Johnson | 4b9b379 | 2016-10-12 18:39:29 +0000 | [diff] [blame] | 1462 | std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor, |
Peter Collingbourne | e6fd9ff | 2017-02-03 17:01:14 +0000 | [diff] [blame] | 1463 | bool IsPerformingImport) { |
Duncan P. N. Exon Smith | 565a0aa | 2016-04-17 23:30:31 +0000 | [diff] [blame] | 1464 | IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes, |
Teresa Johnson | 4b9b379 | 2016-10-12 18:39:29 +0000 | [diff] [blame] | 1465 | std::move(Src), ValuesToLink, std::move(AddLazyFor), |
Peter Collingbourne | e6fd9ff | 2017-02-03 17:01:14 +0000 | [diff] [blame] | 1466 | IsPerformingImport); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1467 | Error E = TheIRLinker.run(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1468 | Composite.dropTriviallyDeadConstantArrays(); |
Peter Collingbourne | 1eaa97f | 2016-05-27 05:21:35 +0000 | [diff] [blame] | 1469 | return E; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 1470 | } |