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