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