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