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