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