1 //===- lib/Linker/IRMover.cpp ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Linker/IRMover.h"
10 #include "LinkDiagnosticInfo.h"
11 #include "llvm/ADT/SetVector.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DebugInfo.h"
16 #include "llvm/IR/DiagnosticPrinter.h"
17 #include "llvm/IR/GVMaterializer.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/TypeFinder.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Transforms/Utils/Cloning.h"
25 //===----------------------------------------------------------------------===//
26 // TypeMap implementation.
27 //===----------------------------------------------------------------------===//
30 class TypeMapTy : public ValueMapTypeRemapper {
31 /// This is a mapping from a source type to a destination type to use.
32 DenseMap<Type *, Type *> MappedTypes;
34 /// When checking to see if two subgraphs are isomorphic, we speculatively
35 /// add types to MappedTypes, but keep track of them here in case we need to
37 SmallVector<Type *, 16> SpeculativeTypes;
39 SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
41 /// This is a list of non-opaque structs in the source module that are mapped
42 /// to an opaque struct in the destination module.
43 SmallVector<StructType *, 16> SrcDefinitionsToResolve;
45 /// This is the set of opaque types in the destination modules who are
46 /// getting a body from the source module.
47 SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
50 TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
51 : DstStructTypesSet(DstStructTypesSet) {}
53 IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
54 /// Indicate that the specified type in the destination module is conceptually
55 /// equivalent to the specified type in the source module.
56 void addTypeMapping(Type *DstTy, Type *SrcTy);
58 /// Produce a body for an opaque type in the dest module from a type
59 /// definition in the source module.
60 void linkDefinedTypeBodies();
62 /// Return the mapped type to use for the specified input type from the
64 Type *get(Type *SrcTy);
65 Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
67 void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
69 FunctionType *get(FunctionType *T) {
70 return cast<FunctionType>(get((Type *)T));
74 Type *remapType(Type *SrcTy) override { return get(SrcTy); }
76 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
80 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
81 assert(SpeculativeTypes.empty());
82 assert(SpeculativeDstOpaqueTypes.empty());
84 // Check to see if these types are recursively isomorphic and establish a
85 // mapping between them if so.
86 if (!areTypesIsomorphic(DstTy, SrcTy)) {
87 // Oops, they aren't isomorphic. Just discard this request by rolling out
88 // any speculative mappings we've established.
89 for (Type *Ty : SpeculativeTypes)
90 MappedTypes.erase(Ty);
92 SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
93 SpeculativeDstOpaqueTypes.size());
94 for (StructType *Ty : SpeculativeDstOpaqueTypes)
95 DstResolvedOpaqueTypes.erase(Ty);
97 // SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy
98 // and all its descendants to lower amount of renaming in LLVM context
99 // Renaming occurs because we load all source modules to the same context
100 // and declaration with existing name gets renamed (i.e Foo -> Foo.42).
101 // As a result we may get several different types in the destination
102 // module, which are in fact the same.
103 for (Type *Ty : SpeculativeTypes)
104 if (auto *STy = dyn_cast<StructType>(Ty))
108 SpeculativeTypes.clear();
109 SpeculativeDstOpaqueTypes.clear();
112 /// Recursively walk this pair of types, returning true if they are isomorphic,
113 /// false if they are not.
114 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
115 // Two types with differing kinds are clearly not isomorphic.
116 if (DstTy->getTypeID() != SrcTy->getTypeID())
119 // If we have an entry in the MappedTypes table, then we have our answer.
120 Type *&Entry = MappedTypes[SrcTy];
122 return Entry == DstTy;
124 // Two identical types are clearly isomorphic. Remember this
125 // non-speculatively.
126 if (DstTy == SrcTy) {
131 // Okay, we have two types with identical kinds that we haven't seen before.
133 // If this is an opaque struct type, special case it.
134 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
135 // Mapping an opaque type to any struct, just keep the dest struct.
136 if (SSTy->isOpaque()) {
138 SpeculativeTypes.push_back(SrcTy);
142 // Mapping a non-opaque source type to an opaque dest. If this is the first
143 // type that we're mapping onto this destination type then we succeed. Keep
144 // the dest, but fill it in later. If this is the second (different) type
145 // that we're trying to map onto the same opaque type then we fail.
146 if (cast<StructType>(DstTy)->isOpaque()) {
147 // We can only map one source type onto the opaque destination type.
148 if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
150 SrcDefinitionsToResolve.push_back(SSTy);
151 SpeculativeTypes.push_back(SrcTy);
152 SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
158 // If the number of subtypes disagree between the two types, then we fail.
159 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
162 // Fail if any of the extra properties (e.g. array size) of the type disagree.
163 if (isa<IntegerType>(DstTy))
164 return false; // bitwidth disagrees.
165 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
166 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
168 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
169 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
171 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
172 StructType *SSTy = cast<StructType>(SrcTy);
173 if (DSTy->isLiteral() != SSTy->isLiteral() ||
174 DSTy->isPacked() != SSTy->isPacked())
176 } else if (auto *DArrTy = dyn_cast<ArrayType>(DstTy)) {
177 if (DArrTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
179 } else if (auto *DVecTy = dyn_cast<VectorType>(DstTy)) {
180 if (DVecTy->getElementCount() != cast<VectorType>(SrcTy)->getElementCount())
184 // Otherwise, we speculate that these two types will line up and recursively
185 // check the subelements.
187 SpeculativeTypes.push_back(SrcTy);
189 for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
190 if (!areTypesIsomorphic(DstTy->getContainedType(I),
191 SrcTy->getContainedType(I)))
194 // If everything seems to have lined up, then everything is great.
198 void TypeMapTy::linkDefinedTypeBodies() {
199 SmallVector<Type *, 16> Elements;
200 for (StructType *SrcSTy : SrcDefinitionsToResolve) {
201 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
202 assert(DstSTy->isOpaque());
204 // Map the body of the source type over to a new body for the dest type.
205 Elements.resize(SrcSTy->getNumElements());
206 for (unsigned I = 0, E = Elements.size(); I != E; ++I)
207 Elements[I] = get(SrcSTy->getElementType(I));
209 DstSTy->setBody(Elements, SrcSTy->isPacked());
210 DstStructTypesSet.switchToNonOpaque(DstSTy);
212 SrcDefinitionsToResolve.clear();
213 DstResolvedOpaqueTypes.clear();
216 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
217 ArrayRef<Type *> ETypes) {
218 DTy->setBody(ETypes, STy->isPacked());
221 if (STy->hasName()) {
222 SmallString<16> TmpName = STy->getName();
224 DTy->setName(TmpName);
227 DstStructTypesSet.addNonOpaque(DTy);
230 Type *TypeMapTy::get(Type *Ty) {
231 SmallPtrSet<StructType *, 8> Visited;
232 return get(Ty, Visited);
235 Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
236 // If we already have an entry for this type, return it.
237 Type **Entry = &MappedTypes[Ty];
241 // These are types that LLVM itself will unique.
242 bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
245 StructType *STy = cast<StructType>(Ty);
246 // This is actually a type from the destination module, this can be reached
247 // when this type is loaded in another module, added to DstStructTypesSet,
248 // and then we reach the same type in another module where it has not been
249 // added to MappedTypes. (PR37684)
250 if (STy->getContext().isODRUniquingDebugTypes() && !STy->isOpaque() &&
251 DstStructTypesSet.hasType(STy))
255 for (auto &Pair : MappedTypes) {
256 assert(!(Pair.first != Ty && Pair.second == Ty) &&
257 "mapping to a source type");
261 if (!Visited.insert(STy).second) {
262 StructType *DTy = StructType::create(Ty->getContext());
267 // If this is not a recursive type, then just map all of the elements and
268 // then rebuild the type from inside out.
269 SmallVector<Type *, 4> ElementTypes;
271 // If there are no element types to map, then the type is itself. This is
272 // true for the anonymous {} struct, things like 'float', integers, etc.
273 if (Ty->getNumContainedTypes() == 0 && IsUniqued)
276 // Remap all of the elements, keeping track of whether any of them change.
277 bool AnyChange = false;
278 ElementTypes.resize(Ty->getNumContainedTypes());
279 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
280 ElementTypes[I] = get(Ty->getContainedType(I), Visited);
281 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
284 // If we found our type while recursively processing stuff, just use it.
285 Entry = &MappedTypes[Ty];
287 if (auto *DTy = dyn_cast<StructType>(*Entry)) {
288 if (DTy->isOpaque()) {
289 auto *STy = cast<StructType>(Ty);
290 finishType(DTy, STy, ElementTypes);
296 // If all of the element types mapped directly over and the type is not
297 // a named struct, then the type is usable as-is.
298 if (!AnyChange && IsUniqued)
301 // Otherwise, rebuild a modified type.
302 switch (Ty->getTypeID()) {
304 llvm_unreachable("unknown derived type to remap");
305 case Type::ArrayTyID:
306 return *Entry = ArrayType::get(ElementTypes[0],
307 cast<ArrayType>(Ty)->getNumElements());
308 case Type::ScalableVectorTyID:
309 // FIXME: handle scalable vectors
310 case Type::FixedVectorTyID:
311 return *Entry = FixedVectorType::get(
312 ElementTypes[0], cast<FixedVectorType>(Ty)->getNumElements());
313 case Type::PointerTyID:
314 return *Entry = PointerType::get(ElementTypes[0],
315 cast<PointerType>(Ty)->getAddressSpace());
316 case Type::FunctionTyID:
317 return *Entry = FunctionType::get(ElementTypes[0],
318 makeArrayRef(ElementTypes).slice(1),
319 cast<FunctionType>(Ty)->isVarArg());
320 case Type::StructTyID: {
321 auto *STy = cast<StructType>(Ty);
322 bool IsPacked = STy->isPacked();
324 return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
326 // If the type is opaque, we can just use it directly.
327 if (STy->isOpaque()) {
328 DstStructTypesSet.addOpaque(STy);
332 if (StructType *OldT =
333 DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
335 return *Entry = OldT;
339 DstStructTypesSet.addNonOpaque(STy);
343 StructType *DTy = StructType::create(Ty->getContext());
344 finishType(DTy, STy, ElementTypes);
350 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
352 : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
353 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
355 //===----------------------------------------------------------------------===//
356 // IRLinker implementation.
357 //===----------------------------------------------------------------------===//
362 /// Creates prototypes for functions that are lazily linked on the fly. This
363 /// speeds up linking for modules with many/ lazily linked functions of which
365 class GlobalValueMaterializer final : public ValueMaterializer {
366 IRLinker &TheIRLinker;
369 GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
370 Value *materialize(Value *V) override;
373 class LocalValueMaterializer final : public ValueMaterializer {
374 IRLinker &TheIRLinker;
377 LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
378 Value *materialize(Value *V) override;
381 /// Type of the Metadata map in \a ValueToValueMapTy.
382 typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
384 /// This is responsible for keeping track of the state used for moving data
385 /// from SrcM to DstM.
388 std::unique_ptr<Module> SrcM;
390 /// See IRMover::move().
391 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
394 GlobalValueMaterializer GValMaterializer;
395 LocalValueMaterializer LValMaterializer;
397 /// A metadata map that's shared between IRLinker instances.
400 /// Mapping of values from what they used to be in Src, to what they are now
401 /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
402 /// due to the use of Value handles which the Linker doesn't actually need,
403 /// but this allows us to reuse the ValueMapper code.
404 ValueToValueMapTy ValueMap;
405 ValueToValueMapTy IndirectSymbolValueMap;
407 DenseSet<GlobalValue *> ValuesToLink;
408 std::vector<GlobalValue *> Worklist;
409 std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist;
411 void maybeAdd(GlobalValue *GV) {
412 if (ValuesToLink.insert(GV).second)
413 Worklist.push_back(GV);
416 /// Whether we are importing globals for ThinLTO, as opposed to linking the
417 /// source module. If this flag is set, it means that we can rely on some
418 /// other object file to define any non-GlobalValue entities defined by the
419 /// source module. This currently causes us to not link retained types in
420 /// debug info metadata and module inline asm.
421 bool IsPerformingImport;
423 /// Set to true when all global value body linking is complete (including
424 /// lazy linking). Used to prevent metadata linking from creating new
426 bool DoneLinkingBodies = false;
428 /// The Error encountered during materialization. We use an Optional here to
429 /// avoid needing to manage an unconsumed success value.
430 Optional<Error> FoundError;
431 void setError(Error E) {
433 FoundError = std::move(E);
436 /// Most of the errors produced by this module are inconvertible StringErrors.
437 /// This convenience function lets us return one of those more easily.
438 Error stringErr(const Twine &T) {
439 return make_error<StringError>(T, inconvertibleErrorCode());
442 /// Entry point for mapping values and alternate context for mapping aliases.
444 unsigned IndirectSymbolMCID;
446 /// Handles cloning of a global values from the source module into
447 /// the destination module, including setting the attributes and visibility.
448 GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
450 void emitWarning(const Twine &Message) {
451 SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
454 /// Given a global in the source module, return the global in the
455 /// destination module that is being linked to, if any.
456 GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
457 // If the source has no name it can't link. If it has local linkage,
458 // there is no name match-up going on.
459 if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
462 // Otherwise see if we have a match in the destination module's symtab.
463 GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
467 // If we found a global with the same name in the dest module, but it has
468 // internal linkage, we are really not doing any linkage here.
469 if (DGV->hasLocalLinkage())
472 // Otherwise, we do in fact link to the destination global.
476 void computeTypeMapping();
478 Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,
479 const GlobalVariable *SrcGV);
481 /// Given the GlobaValue \p SGV in the source module, and the matching
482 /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV
483 /// into the destination module.
485 /// Note this code may call the client-provided \p AddLazyFor.
486 bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
487 Expected<Constant *> linkGlobalValueProto(GlobalValue *GV,
488 bool ForIndirectSymbol);
490 Error linkModuleFlagsMetadata();
492 void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src);
493 Error linkFunctionBody(Function &Dst, Function &Src);
494 void linkIndirectSymbolBody(GlobalIndirectSymbol &Dst,
495 GlobalIndirectSymbol &Src);
496 Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
498 /// Replace all types in the source AttributeList with the
499 /// corresponding destination type.
500 AttributeList mapAttributeTypes(LLVMContext &C, AttributeList Attrs);
502 /// Functions that take care of cloning a specific global value type
503 /// into the destination module.
504 GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
505 Function *copyFunctionProto(const Function *SF);
506 GlobalValue *copyGlobalIndirectSymbolProto(const GlobalIndirectSymbol *SGIS);
508 /// Perform "replace all uses with" operations. These work items need to be
509 /// performed as part of materialization, but we postpone them to happen after
510 /// materialization is done. The materializer called by ValueMapper is not
511 /// expected to delete constants, as ValueMapper is holding pointers to some
512 /// of them, but constant destruction may be indirectly triggered by RAUW.
513 /// Hence, the need to move this out of the materialization call chain.
514 void flushRAUWWorklist();
516 /// When importing for ThinLTO, prevent importing of types listed on
517 /// the DICompileUnit that we don't need a copy of in the importing
519 void prepareCompileUnitsForImport();
520 void linkNamedMDNodes();
523 IRLinker(Module &DstM, MDMapT &SharedMDs,
524 IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
525 ArrayRef<GlobalValue *> ValuesToLink,
526 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor,
527 bool IsPerformingImport)
528 : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
529 TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
530 SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
531 Mapper(ValueMap, RF_MoveDistinctMDs | RF_IgnoreMissingLocals, &TypeMap,
533 IndirectSymbolMCID(Mapper.registerAlternateMappingContext(
534 IndirectSymbolValueMap, &LValMaterializer)) {
535 ValueMap.getMDMap() = std::move(SharedMDs);
536 for (GlobalValue *GV : ValuesToLink)
538 if (IsPerformingImport)
539 prepareCompileUnitsForImport();
541 ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
544 Value *materialize(Value *V, bool ForIndirectSymbol);
548 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
549 /// table. This is good for all clients except for us. Go through the trouble
550 /// to force this back.
551 static void forceRenaming(GlobalValue *GV, StringRef Name) {
552 // If the global doesn't force its name or if it already has the right name,
553 // there is nothing for us to do.
554 if (GV->hasLocalLinkage() || GV->getName() == Name)
557 Module *M = GV->getParent();
559 // If there is a conflict, rename the conflict.
560 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
561 GV->takeName(ConflictGV);
562 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
563 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
565 GV->setName(Name); // Force the name back
569 Value *GlobalValueMaterializer::materialize(Value *SGV) {
570 return TheIRLinker.materialize(SGV, false);
573 Value *LocalValueMaterializer::materialize(Value *SGV) {
574 return TheIRLinker.materialize(SGV, true);
577 Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
578 auto *SGV = dyn_cast<GlobalValue>(V);
582 Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForIndirectSymbol);
584 setError(NewProto.takeError());
590 GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);
594 // If we already created the body, just return.
595 if (auto *F = dyn_cast<Function>(New)) {
596 if (!F->isDeclaration())
598 } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
599 if (V->hasInitializer() || V->hasAppendingLinkage())
602 auto *IS = cast<GlobalIndirectSymbol>(New);
603 if (IS->getIndirectSymbol())
607 // When linking a global for an indirect symbol, it will always be linked.
608 // However we need to check if it was not already scheduled to satisfy a
609 // reference from a regular global value initializer. We know if it has been
610 // schedule if the "New" GlobalValue that is mapped here for the indirect
611 // symbol is the same as the one already mapped. If there is an entry in the
612 // ValueMap but the value is different, it means that the value already had a
613 // definition in the destination module (linkonce for instance), but we need a
614 // new definition for the indirect symbol ("New" will be different.
615 if (ForIndirectSymbol && ValueMap.lookup(SGV) == New)
618 if (ForIndirectSymbol || shouldLink(New, *SGV))
619 setError(linkGlobalValueBody(*New, *SGV));
624 /// Loop through the global variables in the src module and merge them into the
626 GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
627 // No linking to be performed or linking from the source: simply create an
628 // identical version of the symbol over in the dest module... the
629 // initializer will be filled in later by LinkGlobalInits.
630 GlobalVariable *NewDGV =
631 new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),
632 SGVar->isConstant(), GlobalValue::ExternalLinkage,
633 /*init*/ nullptr, SGVar->getName(),
634 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
635 SGVar->getAddressSpace());
636 NewDGV->setAlignment(MaybeAlign(SGVar->getAlignment()));
637 NewDGV->copyAttributesFrom(SGVar);
641 AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) {
642 for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
643 if (Attrs.hasAttribute(i, Attribute::ByVal)) {
644 Type *Ty = Attrs.getAttribute(i, Attribute::ByVal).getValueAsType();
648 Attrs = Attrs.removeAttribute(C, i, Attribute::ByVal);
649 Attrs = Attrs.addAttribute(
650 C, i, Attribute::getWithByValType(C, TypeMap.get(Ty)));
656 /// Link the function in the source module into the destination module if
657 /// needed, setting up mapping information.
658 Function *IRLinker::copyFunctionProto(const Function *SF) {
659 // If there is no linkage to be performed or we are linking from the source,
661 auto *F = Function::Create(TypeMap.get(SF->getFunctionType()),
662 GlobalValue::ExternalLinkage,
663 SF->getAddressSpace(), SF->getName(), &DstM);
664 F->copyAttributesFrom(SF);
665 F->setAttributes(mapAttributeTypes(F->getContext(), F->getAttributes()));
669 /// Set up prototypes for any indirect symbols that come over from the source
672 IRLinker::copyGlobalIndirectSymbolProto(const GlobalIndirectSymbol *SGIS) {
673 // If there is no linkage to be performed or we're linking from the source,
675 auto *Ty = TypeMap.get(SGIS->getValueType());
676 GlobalIndirectSymbol *GIS;
677 if (isa<GlobalAlias>(SGIS))
678 GIS = GlobalAlias::create(Ty, SGIS->getAddressSpace(),
679 GlobalValue::ExternalLinkage, SGIS->getName(),
682 GIS = GlobalIFunc::create(Ty, SGIS->getAddressSpace(),
683 GlobalValue::ExternalLinkage, SGIS->getName(),
685 GIS->copyAttributesFrom(SGIS);
689 GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
690 bool ForDefinition) {
692 if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
693 NewGV = copyGlobalVariableProto(SGVar);
694 } else if (auto *SF = dyn_cast<Function>(SGV)) {
695 NewGV = copyFunctionProto(SF);
698 NewGV = copyGlobalIndirectSymbolProto(cast<GlobalIndirectSymbol>(SGV));
699 else if (SGV->getValueType()->isFunctionTy())
701 Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())),
702 GlobalValue::ExternalLinkage, SGV->getAddressSpace(),
703 SGV->getName(), &DstM);
706 new GlobalVariable(DstM, TypeMap.get(SGV->getValueType()),
707 /*isConstant*/ false, GlobalValue::ExternalLinkage,
708 /*init*/ nullptr, SGV->getName(),
709 /*insertbefore*/ nullptr,
710 SGV->getThreadLocalMode(), SGV->getAddressSpace());
714 NewGV->setLinkage(SGV->getLinkage());
715 else if (SGV->hasExternalWeakLinkage())
716 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
718 if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
719 // Metadata for global variables and function declarations is copied eagerly.
720 if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
721 NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
724 // Remove these copied constants in case this stays a declaration, since
725 // they point to the source module. If the def is linked the values will
726 // be mapped in during linkFunctionBody.
727 if (auto *NewF = dyn_cast<Function>(NewGV)) {
728 NewF->setPersonalityFn(nullptr);
729 NewF->setPrefixData(nullptr);
730 NewF->setPrologueData(nullptr);
736 static StringRef getTypeNamePrefix(StringRef Name) {
737 size_t DotPos = Name.rfind('.');
738 return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||
739 !isdigit(static_cast<unsigned char>(Name[DotPos + 1])))
741 : Name.substr(0, DotPos);
744 /// Loop over all of the linked values to compute type mappings. For example,
745 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
746 /// types 'Foo' but one got renamed when the module was loaded into the same
748 void IRLinker::computeTypeMapping() {
749 for (GlobalValue &SGV : SrcM->globals()) {
750 GlobalValue *DGV = getLinkedToGlobal(&SGV);
754 if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
755 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
759 // Unify the element type of appending arrays.
760 ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
761 ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
762 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
765 for (GlobalValue &SGV : *SrcM)
766 if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) {
767 if (DGV->getType() == SGV.getType()) {
768 // If the types of DGV and SGV are the same, it means that DGV is from
769 // the source module and got added to DstM from a shared metadata. We
770 // shouldn't map this type to itself in case the type's components get
771 // remapped to a new type from DstM (for instance, during the loop over
772 // SrcM->getIdentifiedStructTypes() below).
776 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
779 for (GlobalValue &SGV : SrcM->aliases())
780 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
781 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
783 // Incorporate types by name, scanning all the types in the source module.
784 // At this point, the destination module may have a type "%foo = { i32 }" for
785 // example. When the source module got loaded into the same LLVMContext, if
786 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
787 std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
788 for (StructType *ST : Types) {
792 if (TypeMap.DstStructTypesSet.hasType(ST)) {
793 // This is actually a type from the destination module.
794 // getIdentifiedStructTypes() can have found it by walking debug info
795 // metadata nodes, some of which get linked by name when ODR Type Uniquing
796 // is enabled on the Context, from the source to the destination module.
800 auto STTypePrefix = getTypeNamePrefix(ST->getName());
801 if (STTypePrefix.size()== ST->getName().size())
804 // Check to see if the destination module has a struct with the prefix name.
805 StructType *DST = DstM.getTypeByName(STTypePrefix);
809 // Don't use it if this actually came from the source module. They're in
810 // the same LLVMContext after all. Also don't use it unless the type is
811 // actually used in the destination module. This can happen in situations
816 // %Z = type { %A } %B = type { %C.1 }
817 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
818 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
819 // %C = type { i8* } %B.3 = type { %C.1 }
821 // When we link Module B with Module A, the '%B' in Module B is
822 // used. However, that would then use '%C.1'. But when we process '%C.1',
823 // we prefer to take the '%C' version. So we are then left with both
824 // '%C.1' and '%C' being used for the same types. This leads to some
825 // variables using one type and some using the other.
826 if (TypeMap.DstStructTypesSet.hasType(DST))
827 TypeMap.addTypeMapping(DST, ST);
830 // Now that we have discovered all of the type equivalences, get a body for
831 // any 'opaque' types in the dest module that are now resolved.
832 TypeMap.linkDefinedTypeBodies();
835 static void getArrayElements(const Constant *C,
836 SmallVectorImpl<Constant *> &Dest) {
837 unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
839 for (unsigned i = 0; i != NumElements; ++i)
840 Dest.push_back(C->getAggregateElement(i));
843 /// If there were any appending global variables, link them together now.
845 IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
846 const GlobalVariable *SrcGV) {
847 Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))
850 // FIXME: This upgrade is done during linking to support the C API. Once the
851 // old form is deprecated, we should move this upgrade to
852 // llvm::UpgradeGlobalVariable() and simplify the logic here and in
853 // Mapper::mapAppendingVariable() in ValueMapper.cpp.
854 StringRef Name = SrcGV->getName();
855 bool IsNewStructor = false;
856 bool IsOldStructor = false;
857 if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
858 if (cast<StructType>(EltTy)->getNumElements() == 3)
859 IsNewStructor = true;
861 IsOldStructor = true;
864 PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
866 auto &ST = *cast<StructType>(EltTy);
867 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
868 EltTy = StructType::get(SrcGV->getContext(), Tys, false);
871 uint64_t DstNumElements = 0;
873 ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
874 DstNumElements = DstTy->getNumElements();
876 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
878 "Linking globals named '" + SrcGV->getName() +
879 "': can only link appending global with another appending "
882 // Check to see that they two arrays agree on type.
883 if (EltTy != DstTy->getElementType())
884 return stringErr("Appending variables with different element types!");
885 if (DstGV->isConstant() != SrcGV->isConstant())
886 return stringErr("Appending variables linked with different const'ness!");
888 if (DstGV->getAlignment() != SrcGV->getAlignment())
890 "Appending variables with different alignment need to be linked!");
892 if (DstGV->getVisibility() != SrcGV->getVisibility())
894 "Appending variables with different visibility need to be linked!");
896 if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
898 "Appending variables with different unnamed_addr need to be linked!");
900 if (DstGV->getSection() != SrcGV->getSection())
902 "Appending variables with different section name need to be linked!");
905 SmallVector<Constant *, 16> SrcElements;
906 getArrayElements(SrcGV->getInitializer(), SrcElements);
909 auto It = remove_if(SrcElements, [this](Constant *E) {
911 dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts());
914 GlobalValue *DGV = getLinkedToGlobal(Key);
915 return !shouldLink(DGV, *Key);
917 SrcElements.erase(It, SrcElements.end());
919 uint64_t NewSize = DstNumElements + SrcElements.size();
920 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
922 // Create the new global variable.
923 GlobalVariable *NG = new GlobalVariable(
924 DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
925 /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
926 SrcGV->getAddressSpace());
928 NG->copyAttributesFrom(SrcGV);
929 forceRenaming(NG, SrcGV->getName());
931 Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
933 Mapper.scheduleMapAppendingVariable(*NG,
934 DstGV ? DstGV->getInitializer() : nullptr,
935 IsOldStructor, SrcElements);
937 // Replace any uses of the two global variables with uses of the new
940 RAUWWorklist.push_back(
941 std::make_pair(DstGV, ConstantExpr::getBitCast(NG, DstGV->getType())));
947 bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
948 if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage())
951 if (DGV && !DGV->isDeclarationForLinker())
954 if (SGV.isDeclaration() || DoneLinkingBodies)
957 // Callback to the client to give a chance to lazily add the Global to the
958 // list of value to link.
959 bool LazilyAdded = false;
960 AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
967 Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
968 bool ForIndirectSymbol) {
969 GlobalValue *DGV = getLinkedToGlobal(SGV);
971 bool ShouldLink = shouldLink(DGV, *SGV);
973 // just missing from map
975 auto I = ValueMap.find(SGV);
976 if (I != ValueMap.end())
977 return cast<Constant>(I->second);
979 I = IndirectSymbolValueMap.find(SGV);
980 if (I != IndirectSymbolValueMap.end())
981 return cast<Constant>(I->second);
984 if (!ShouldLink && ForIndirectSymbol)
987 // Handle the ultra special appending linkage case first.
988 assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
989 if (SGV->hasAppendingLinkage())
990 return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
991 cast<GlobalVariable>(SGV));
994 if (DGV && !ShouldLink) {
997 // If we are done linking global value bodies (i.e. we are performing
998 // metadata linking), don't link in the global value due to this
999 // reference, simply map it to null.
1000 if (DoneLinkingBodies)
1003 NewGV = copyGlobalValueProto(SGV, ShouldLink || ForIndirectSymbol);
1004 if (ShouldLink || !ForIndirectSymbol)
1005 forceRenaming(NewGV, SGV->getName());
1008 // Overloaded intrinsics have overloaded types names as part of their
1009 // names. If we renamed overloaded types we should rename the intrinsic
1011 if (Function *F = dyn_cast<Function>(NewGV))
1012 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F))
1013 NewGV = Remangled.getValue();
1015 if (ShouldLink || ForIndirectSymbol) {
1016 if (const Comdat *SC = SGV->getComdat()) {
1017 if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
1018 Comdat *DC = DstM.getOrInsertComdat(SC->getName());
1019 DC->setSelectionKind(SC->getSelectionKind());
1025 if (!ShouldLink && ForIndirectSymbol)
1026 NewGV->setLinkage(GlobalValue::InternalLinkage);
1028 Constant *C = NewGV;
1029 // Only create a bitcast if necessary. In particular, with
1030 // DebugTypeODRUniquing we may reach metadata in the destination module
1031 // containing a GV from the source module, in which case SGV will be
1032 // the same as DGV and NewGV, and TypeMap.get() will assert since it
1033 // assumes it is being invoked on a type in the source module.
1034 if (DGV && NewGV != SGV) {
1035 C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1036 NewGV, TypeMap.get(SGV->getType()));
1039 if (DGV && NewGV != DGV) {
1040 // Schedule "replace all uses with" to happen after materializing is
1041 // done. It is not safe to do it now, since ValueMapper may be holding
1042 // pointers to constants that will get deleted if RAUW runs.
1043 RAUWWorklist.push_back(std::make_pair(
1045 ConstantExpr::getPointerBitCastOrAddrSpaceCast(NewGV, DGV->getType())));
1051 /// Update the initializers in the Dest module now that all globals that may be
1052 /// referenced are in Dest.
1053 void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {
1054 // Figure out what the initializer looks like in the dest module.
1055 Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());
1058 /// Copy the source function over into the dest function and fix up references
1059 /// to values. At this point we know that Dest is an external function, and
1060 /// that Src is not.
1061 Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
1062 assert(Dst.isDeclaration() && !Src.isDeclaration());
1064 // Materialize if needed.
1065 if (Error Err = Src.materialize())
1068 // Link in the operands without remapping.
1069 if (Src.hasPrefixData())
1070 Dst.setPrefixData(Src.getPrefixData());
1071 if (Src.hasPrologueData())
1072 Dst.setPrologueData(Src.getPrologueData());
1073 if (Src.hasPersonalityFn())
1074 Dst.setPersonalityFn(Src.getPersonalityFn());
1076 // Copy over the metadata attachments without remapping.
1077 Dst.copyMetadata(&Src, 0);
1079 // Steal arguments and splice the body of Src into Dst.
1080 Dst.stealArgumentListFrom(Src);
1081 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1083 // Everything has been moved over. Remap it.
1084 Mapper.scheduleRemapFunction(Dst);
1085 return Error::success();
1088 void IRLinker::linkIndirectSymbolBody(GlobalIndirectSymbol &Dst,
1089 GlobalIndirectSymbol &Src) {
1090 Mapper.scheduleMapGlobalIndirectSymbol(Dst, *Src.getIndirectSymbol(),
1091 IndirectSymbolMCID);
1094 Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1095 if (auto *F = dyn_cast<Function>(&Src))
1096 return linkFunctionBody(cast<Function>(Dst), *F);
1097 if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1098 linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar);
1099 return Error::success();
1101 linkIndirectSymbolBody(cast<GlobalIndirectSymbol>(Dst), cast<GlobalIndirectSymbol>(Src));
1102 return Error::success();
1105 void IRLinker::flushRAUWWorklist() {
1106 for (const auto &Elem : RAUWWorklist) {
1109 std::tie(Old, New) = Elem;
1111 Old->replaceAllUsesWith(New);
1112 Old->eraseFromParent();
1114 RAUWWorklist.clear();
1117 void IRLinker::prepareCompileUnitsForImport() {
1118 NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
1119 if (!SrcCompileUnits)
1121 // When importing for ThinLTO, prevent importing of types listed on
1122 // the DICompileUnit that we don't need a copy of in the importing
1123 // module. They will be emitted by the originating module.
1124 for (unsigned I = 0, E = SrcCompileUnits->getNumOperands(); I != E; ++I) {
1125 auto *CU = cast<DICompileUnit>(SrcCompileUnits->getOperand(I));
1126 assert(CU && "Expected valid compile unit");
1127 // Enums, macros, and retained types don't need to be listed on the
1128 // imported DICompileUnit. This means they will only be imported
1129 // if reached from the mapped IR. Do this by setting their value map
1130 // entries to nullptr, which will automatically prevent their importing
1131 // when reached from the DICompileUnit during metadata mapping.
1132 ValueMap.MD()[CU->getRawEnumTypes()].reset(nullptr);
1133 ValueMap.MD()[CU->getRawMacros()].reset(nullptr);
1134 ValueMap.MD()[CU->getRawRetainedTypes()].reset(nullptr);
1135 // The original definition (or at least its debug info - if the variable is
1136 // internalized an optimized away) will remain in the source module, so
1137 // there's no need to import them.
1138 // If LLVM ever does more advanced optimizations on global variables
1139 // (removing/localizing write operations, for instance) that can track
1140 // through debug info, this decision may need to be revisited - but do so
1141 // with care when it comes to debug info size. Emitting small CUs containing
1142 // only a few imported entities into every destination module may be very
1143 // size inefficient.
1144 ValueMap.MD()[CU->getRawGlobalVariables()].reset(nullptr);
1146 // Imported entities only need to be mapped in if they have local
1147 // scope, as those might correspond to an imported entity inside a
1148 // function being imported (any locally scoped imported entities that
1149 // don't end up referenced by an imported function will not be emitted
1150 // into the object). Imported entities not in a local scope
1151 // (e.g. on the namespace) only need to be emitted by the originating
1152 // module. Create a list of the locally scoped imported entities, and
1153 // replace the source CUs imported entity list with the new list, so
1154 // only those are mapped in.
1155 // FIXME: Locally-scoped imported entities could be moved to the
1156 // functions they are local to instead of listing them on the CU, and
1157 // we would naturally only link in those needed by function importing.
1158 SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
1159 bool ReplaceImportedEntities = false;
1160 for (auto *IE : CU->getImportedEntities()) {
1161 DIScope *Scope = IE->getScope();
1162 assert(Scope && "Invalid Scope encoding!");
1163 if (isa<DILocalScope>(Scope))
1164 AllImportedModules.emplace_back(IE);
1166 ReplaceImportedEntities = true;
1168 if (ReplaceImportedEntities) {
1169 if (!AllImportedModules.empty())
1170 CU->replaceImportedEntities(MDTuple::get(
1172 SmallVector<Metadata *, 16>(AllImportedModules.begin(),
1173 AllImportedModules.end())));
1175 // If there were no local scope imported entities, we can map
1176 // the whole list to nullptr.
1177 ValueMap.MD()[CU->getRawImportedEntities()].reset(nullptr);
1182 /// Insert all of the named MDNodes in Src into the Dest module.
1183 void IRLinker::linkNamedMDNodes() {
1184 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1185 for (const NamedMDNode &NMD : SrcM->named_metadata()) {
1186 // Don't link module flags here. Do them separately.
1187 if (&NMD == SrcModFlags)
1189 NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1190 // Add Src elements into Dest node.
1191 for (const MDNode *Op : NMD.operands())
1192 DestNMD->addOperand(Mapper.mapMDNode(*Op));
1196 /// Merge the linker flags in Src into the Dest module.
1197 Error IRLinker::linkModuleFlagsMetadata() {
1198 // If the source module has no module flags, we are done.
1199 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1201 return Error::success();
1203 // If the destination module doesn't have module flags yet, then just copy
1204 // over the source module's flags.
1205 NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1206 if (DstModFlags->getNumOperands() == 0) {
1207 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1208 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1210 return Error::success();
1213 // First build a map of the existing module flags and requirements.
1214 DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1215 SmallSetVector<MDNode *, 16> Requirements;
1216 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1217 MDNode *Op = DstModFlags->getOperand(I);
1218 ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1219 MDString *ID = cast<MDString>(Op->getOperand(1));
1221 if (Behavior->getZExtValue() == Module::Require) {
1222 Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1224 Flags[ID] = std::make_pair(Op, I);
1228 // Merge in the flags from the source module, and also collect its set of
1230 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1231 MDNode *SrcOp = SrcModFlags->getOperand(I);
1232 ConstantInt *SrcBehavior =
1233 mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1234 MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1237 std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1238 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1240 // If this is a requirement, add it and continue.
1241 if (SrcBehaviorValue == Module::Require) {
1242 // If the destination module does not already have this requirement, add
1244 if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1245 DstModFlags->addOperand(SrcOp);
1250 // If there is no existing flag with this ID, just add it.
1252 Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1253 DstModFlags->addOperand(SrcOp);
1257 // Otherwise, perform a merge.
1258 ConstantInt *DstBehavior =
1259 mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1260 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1262 auto overrideDstValue = [&]() {
1263 DstModFlags->setOperand(DstIndex, SrcOp);
1264 Flags[ID].first = SrcOp;
1267 // If either flag has override behavior, handle it first.
1268 if (DstBehaviorValue == Module::Override) {
1269 // Diagnose inconsistent flags which both have override behavior.
1270 if (SrcBehaviorValue == Module::Override &&
1271 SrcOp->getOperand(2) != DstOp->getOperand(2))
1272 return stringErr("linking module flags '" + ID->getString() +
1273 "': IDs have conflicting override values in '" +
1274 SrcM->getModuleIdentifier() + "' and '" +
1275 DstM.getModuleIdentifier() + "'");
1277 } else if (SrcBehaviorValue == Module::Override) {
1278 // Update the destination flag to that of the source.
1283 // Diagnose inconsistent merge behavior types.
1284 if (SrcBehaviorValue != DstBehaviorValue) {
1285 bool MaxAndWarn = (SrcBehaviorValue == Module::Max &&
1286 DstBehaviorValue == Module::Warning) ||
1287 (DstBehaviorValue == Module::Max &&
1288 SrcBehaviorValue == Module::Warning);
1290 return stringErr("linking module flags '" + ID->getString() +
1291 "': IDs have conflicting behaviors in '" +
1292 SrcM->getModuleIdentifier() + "' and '" +
1293 DstM.getModuleIdentifier() + "'");
1296 auto replaceDstValue = [&](MDNode *New) {
1297 Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1298 MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1299 DstModFlags->setOperand(DstIndex, Flag);
1300 Flags[ID].first = Flag;
1303 // Emit a warning if the values differ and either source or destination
1304 // request Warning behavior.
1305 if ((DstBehaviorValue == Module::Warning ||
1306 SrcBehaviorValue == Module::Warning) &&
1307 SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1309 raw_string_ostream(Str)
1310 << "linking module flags '" << ID->getString()
1311 << "': IDs have conflicting values ('" << *SrcOp->getOperand(2)
1312 << "' from " << SrcM->getModuleIdentifier() << " with '"
1313 << *DstOp->getOperand(2) << "' from " << DstM.getModuleIdentifier()
1318 // Choose the maximum if either source or destination request Max behavior.
1319 if (DstBehaviorValue == Module::Max || SrcBehaviorValue == Module::Max) {
1320 ConstantInt *DstValue =
1321 mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1322 ConstantInt *SrcValue =
1323 mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1325 // The resulting flag should have a Max behavior, and contain the maximum
1326 // value from between the source and destination values.
1327 Metadata *FlagOps[] = {
1328 (DstBehaviorValue != Module::Max ? SrcOp : DstOp)->getOperand(0), ID,
1329 (SrcValue->getZExtValue() > DstValue->getZExtValue() ? SrcOp : DstOp)
1331 MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1332 DstModFlags->setOperand(DstIndex, Flag);
1333 Flags[ID].first = Flag;
1337 // Perform the merge for standard behavior types.
1338 switch (SrcBehaviorValue) {
1339 case Module::Require:
1340 case Module::Override:
1341 llvm_unreachable("not possible");
1342 case Module::Error: {
1343 // Emit an error if the values differ.
1344 if (SrcOp->getOperand(2) != DstOp->getOperand(2))
1345 return stringErr("linking module flags '" + ID->getString() +
1346 "': IDs have conflicting values in '" +
1347 SrcM->getModuleIdentifier() + "' and '" +
1348 DstM.getModuleIdentifier() + "'");
1351 case Module::Warning: {
1357 case Module::Append: {
1358 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1359 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1360 SmallVector<Metadata *, 8> MDs;
1361 MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1362 MDs.append(DstValue->op_begin(), DstValue->op_end());
1363 MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1365 replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1368 case Module::AppendUnique: {
1369 SmallSetVector<Metadata *, 16> Elts;
1370 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1371 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1372 Elts.insert(DstValue->op_begin(), DstValue->op_end());
1373 Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1375 replaceDstValue(MDNode::get(DstM.getContext(),
1376 makeArrayRef(Elts.begin(), Elts.end())));
1383 // Check all of the requirements.
1384 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1385 MDNode *Requirement = Requirements[I];
1386 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1387 Metadata *ReqValue = Requirement->getOperand(1);
1389 MDNode *Op = Flags[Flag].first;
1390 if (!Op || Op->getOperand(2) != ReqValue)
1391 return stringErr("linking module flags '" + Flag->getString() +
1392 "': does not have the required value");
1394 return Error::success();
1397 /// Return InlineAsm adjusted with target-specific directives if required.
1398 /// For ARM and Thumb, we have to add directives to select the appropriate ISA
1399 /// to support mixing module-level inline assembly from ARM and Thumb modules.
1400 static std::string adjustInlineAsm(const std::string &InlineAsm,
1401 const Triple &Triple) {
1402 if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb)
1403 return ".text\n.balign 2\n.thumb\n" + InlineAsm;
1404 if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb)
1405 return ".text\n.balign 4\n.arm\n" + InlineAsm;
1409 Error IRLinker::run() {
1410 // Ensure metadata materialized before value mapping.
1411 if (SrcM->getMaterializer())
1412 if (Error Err = SrcM->getMaterializer()->materializeMetadata())
1415 // Inherit the target data from the source module if the destination module
1416 // doesn't have one already.
1417 if (DstM.getDataLayout().isDefault())
1418 DstM.setDataLayout(SrcM->getDataLayout());
1420 if (SrcM->getDataLayout() != DstM.getDataLayout()) {
1421 emitWarning("Linking two modules of different data layouts: '" +
1422 SrcM->getModuleIdentifier() + "' is '" +
1423 SrcM->getDataLayoutStr() + "' whereas '" +
1424 DstM.getModuleIdentifier() + "' is '" +
1425 DstM.getDataLayoutStr() + "'\n");
1428 // Copy the target triple from the source to dest if the dest's is empty.
1429 if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1430 DstM.setTargetTriple(SrcM->getTargetTriple());
1432 Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
1434 if (!SrcM->getTargetTriple().empty()&&
1435 !SrcTriple.isCompatibleWith(DstTriple))
1436 emitWarning("Linking two modules of different target triples: " +
1437 SrcM->getModuleIdentifier() + "' is '" +
1438 SrcM->getTargetTriple() + "' whereas '" +
1439 DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
1442 DstM.setTargetTriple(SrcTriple.merge(DstTriple));
1444 // Append the module inline asm string.
1445 if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {
1446 std::string SrcModuleInlineAsm = adjustInlineAsm(SrcM->getModuleInlineAsm(),
1448 if (DstM.getModuleInlineAsm().empty())
1449 DstM.setModuleInlineAsm(SrcModuleInlineAsm);
1451 DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1452 SrcModuleInlineAsm);
1455 // Loop over all of the linked values to compute type mappings.
1456 computeTypeMapping();
1458 std::reverse(Worklist.begin(), Worklist.end());
1459 while (!Worklist.empty()) {
1460 GlobalValue *GV = Worklist.back();
1461 Worklist.pop_back();
1464 if (ValueMap.find(GV) != ValueMap.end() ||
1465 IndirectSymbolValueMap.find(GV) != IndirectSymbolValueMap.end())
1468 assert(!GV->isDeclaration());
1469 Mapper.mapValue(*GV);
1471 return std::move(*FoundError);
1472 flushRAUWWorklist();
1475 // Note that we are done linking global value bodies. This prevents
1476 // metadata linking from creating new references.
1477 DoneLinkingBodies = true;
1478 Mapper.addFlags(RF_NullMapMissingGlobalValues);
1480 // Remap all of the named MDNodes in Src into the DstM module. We do this
1481 // after linking GlobalValues so that MDNodes that reference GlobalValues
1482 // are properly remapped.
1485 // Merge the module flags into the DstM module.
1486 return linkModuleFlagsMetadata();
1489 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1490 : ETypes(E), IsPacked(P) {}
1492 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1493 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1495 bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1496 return IsPacked == That.IsPacked && ETypes == That.ETypes;
1499 bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1500 return !this->operator==(That);
1503 StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1504 return DenseMapInfo<StructType *>::getEmptyKey();
1507 StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1508 return DenseMapInfo<StructType *>::getTombstoneKey();
1511 unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1512 return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1516 unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1517 return getHashValue(KeyTy(ST));
1520 bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1521 const StructType *RHS) {
1522 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1524 return LHS == KeyTy(RHS);
1527 bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1528 const StructType *RHS) {
1529 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1531 return KeyTy(LHS) == KeyTy(RHS);
1534 void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1535 assert(!Ty->isOpaque());
1536 NonOpaqueStructTypes.insert(Ty);
1539 void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1540 assert(!Ty->isOpaque());
1541 NonOpaqueStructTypes.insert(Ty);
1542 bool Removed = OpaqueStructTypes.erase(Ty);
1547 void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1548 assert(Ty->isOpaque());
1549 OpaqueStructTypes.insert(Ty);
1553 IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1555 IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1556 auto I = NonOpaqueStructTypes.find_as(Key);
1557 return I == NonOpaqueStructTypes.end() ? nullptr : *I;
1560 bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1562 return OpaqueStructTypes.count(Ty);
1563 auto I = NonOpaqueStructTypes.find(Ty);
1564 return I == NonOpaqueStructTypes.end() ? false : *I == Ty;
1567 IRMover::IRMover(Module &M) : Composite(M) {
1568 TypeFinder StructTypes;
1569 StructTypes.run(M, /* OnlyNamed */ false);
1570 for (StructType *Ty : StructTypes) {
1572 IdentifiedStructTypes.addOpaque(Ty);
1574 IdentifiedStructTypes.addNonOpaque(Ty);
1576 // Self-map metadatas in the destination module. This is needed when
1577 // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the
1578 // destination module may be reached from the source module.
1579 for (auto *MD : StructTypes.getVisitedMetadata()) {
1580 SharedMDs[MD].reset(const_cast<MDNode *>(MD));
1584 Error IRMover::move(
1585 std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
1586 std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
1587 bool IsPerformingImport) {
1588 IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
1589 std::move(Src), ValuesToLink, std::move(AddLazyFor),
1590 IsPerformingImport);
1591 Error E = TheIRLinker.run();
1592 Composite.dropTriviallyDeadConstantArrays();