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