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