Path: blob/main/contrib/llvm-project/llvm/lib/Linker/IRMover.cpp
35233 views
//===- lib/Linker/IRMover.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/Linker/IRMover.h"9#include "LinkDiagnosticInfo.h"10#include "llvm/ADT/ScopeExit.h"11#include "llvm/ADT/SetVector.h"12#include "llvm/ADT/SmallPtrSet.h"13#include "llvm/ADT/SmallString.h"14#include "llvm/IR/AutoUpgrade.h"15#include "llvm/IR/Constants.h"16#include "llvm/IR/DebugInfoMetadata.h"17#include "llvm/IR/DiagnosticPrinter.h"18#include "llvm/IR/Function.h"19#include "llvm/IR/GVMaterializer.h"20#include "llvm/IR/GlobalValue.h"21#include "llvm/IR/Instruction.h"22#include "llvm/IR/Instructions.h"23#include "llvm/IR/Intrinsics.h"24#include "llvm/IR/Module.h"25#include "llvm/IR/PseudoProbe.h"26#include "llvm/IR/TypeFinder.h"27#include "llvm/Object/ModuleSymbolTable.h"28#include "llvm/Support/Error.h"29#include "llvm/Support/Path.h"30#include "llvm/TargetParser/Triple.h"31#include "llvm/Transforms/Utils/ValueMapper.h"32#include <optional>33#include <utility>34using namespace llvm;3536//===----------------------------------------------------------------------===//37// TypeMap implementation.38//===----------------------------------------------------------------------===//3940namespace {41class TypeMapTy : public ValueMapTypeRemapper {42/// This is a mapping from a source type to a destination type to use.43DenseMap<Type *, Type *> MappedTypes;4445/// When checking to see if two subgraphs are isomorphic, we speculatively46/// add types to MappedTypes, but keep track of them here in case we need to47/// roll back.48SmallVector<Type *, 16> SpeculativeTypes;4950SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;5152/// This is a list of non-opaque structs in the source module that are mapped53/// to an opaque struct in the destination module.54SmallVector<StructType *, 16> SrcDefinitionsToResolve;5556/// This is the set of opaque types in the destination modules who are57/// getting a body from the source module.58SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;5960public:61TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)62: DstStructTypesSet(DstStructTypesSet) {}6364IRMover::IdentifiedStructTypeSet &DstStructTypesSet;65/// Indicate that the specified type in the destination module is conceptually66/// equivalent to the specified type in the source module.67void addTypeMapping(Type *DstTy, Type *SrcTy);6869/// Produce a body for an opaque type in the dest module from a type70/// definition in the source module.71void linkDefinedTypeBodies();7273/// Return the mapped type to use for the specified input type from the74/// source module.75Type *get(Type *SrcTy);76Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);7778void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);7980FunctionType *get(FunctionType *T) {81return cast<FunctionType>(get((Type *)T));82}8384private:85Type *remapType(Type *SrcTy) override { return get(SrcTy); }8687bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);88};89}9091void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {92assert(SpeculativeTypes.empty());93assert(SpeculativeDstOpaqueTypes.empty());9495// Check to see if these types are recursively isomorphic and establish a96// mapping between them if so.97if (!areTypesIsomorphic(DstTy, SrcTy)) {98// Oops, they aren't isomorphic. Just discard this request by rolling out99// any speculative mappings we've established.100for (Type *Ty : SpeculativeTypes)101MappedTypes.erase(Ty);102103SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -104SpeculativeDstOpaqueTypes.size());105for (StructType *Ty : SpeculativeDstOpaqueTypes)106DstResolvedOpaqueTypes.erase(Ty);107} else {108// SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy109// and all its descendants to lower amount of renaming in LLVM context110// Renaming occurs because we load all source modules to the same context111// and declaration with existing name gets renamed (i.e Foo -> Foo.42).112// As a result we may get several different types in the destination113// module, which are in fact the same.114for (Type *Ty : SpeculativeTypes)115if (auto *STy = dyn_cast<StructType>(Ty))116if (STy->hasName())117STy->setName("");118}119SpeculativeTypes.clear();120SpeculativeDstOpaqueTypes.clear();121}122123/// Recursively walk this pair of types, returning true if they are isomorphic,124/// false if they are not.125bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {126// Two types with differing kinds are clearly not isomorphic.127if (DstTy->getTypeID() != SrcTy->getTypeID())128return false;129130// If we have an entry in the MappedTypes table, then we have our answer.131Type *&Entry = MappedTypes[SrcTy];132if (Entry)133return Entry == DstTy;134135// Two identical types are clearly isomorphic. Remember this136// non-speculatively.137if (DstTy == SrcTy) {138Entry = DstTy;139return true;140}141142// Okay, we have two types with identical kinds that we haven't seen before.143144// If this is an opaque struct type, special case it.145if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {146// Mapping an opaque type to any struct, just keep the dest struct.147if (SSTy->isOpaque()) {148Entry = DstTy;149SpeculativeTypes.push_back(SrcTy);150return true;151}152153// Mapping a non-opaque source type to an opaque dest. If this is the first154// type that we're mapping onto this destination type then we succeed. Keep155// the dest, but fill it in later. If this is the second (different) type156// that we're trying to map onto the same opaque type then we fail.157if (cast<StructType>(DstTy)->isOpaque()) {158// We can only map one source type onto the opaque destination type.159if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)160return false;161SrcDefinitionsToResolve.push_back(SSTy);162SpeculativeTypes.push_back(SrcTy);163SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));164Entry = DstTy;165return true;166}167}168169// If the number of subtypes disagree between the two types, then we fail.170if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())171return false;172173// Fail if any of the extra properties (e.g. array size) of the type disagree.174if (isa<IntegerType>(DstTy))175return false; // bitwidth disagrees.176if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {177if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())178return false;179} else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {180if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())181return false;182} else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {183StructType *SSTy = cast<StructType>(SrcTy);184if (DSTy->isLiteral() != SSTy->isLiteral() ||185DSTy->isPacked() != SSTy->isPacked())186return false;187} else if (auto *DArrTy = dyn_cast<ArrayType>(DstTy)) {188if (DArrTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())189return false;190} else if (auto *DVecTy = dyn_cast<VectorType>(DstTy)) {191if (DVecTy->getElementCount() != cast<VectorType>(SrcTy)->getElementCount())192return false;193}194195// Otherwise, we speculate that these two types will line up and recursively196// check the subelements.197Entry = DstTy;198SpeculativeTypes.push_back(SrcTy);199200for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)201if (!areTypesIsomorphic(DstTy->getContainedType(I),202SrcTy->getContainedType(I)))203return false;204205// If everything seems to have lined up, then everything is great.206return true;207}208209void TypeMapTy::linkDefinedTypeBodies() {210SmallVector<Type *, 16> Elements;211for (StructType *SrcSTy : SrcDefinitionsToResolve) {212StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);213assert(DstSTy->isOpaque());214215// Map the body of the source type over to a new body for the dest type.216Elements.resize(SrcSTy->getNumElements());217for (unsigned I = 0, E = Elements.size(); I != E; ++I)218Elements[I] = get(SrcSTy->getElementType(I));219220DstSTy->setBody(Elements, SrcSTy->isPacked());221DstStructTypesSet.switchToNonOpaque(DstSTy);222}223SrcDefinitionsToResolve.clear();224DstResolvedOpaqueTypes.clear();225}226227void TypeMapTy::finishType(StructType *DTy, StructType *STy,228ArrayRef<Type *> ETypes) {229DTy->setBody(ETypes, STy->isPacked());230231// Steal STy's name.232if (STy->hasName()) {233SmallString<16> TmpName = STy->getName();234STy->setName("");235DTy->setName(TmpName);236}237238DstStructTypesSet.addNonOpaque(DTy);239}240241Type *TypeMapTy::get(Type *Ty) {242SmallPtrSet<StructType *, 8> Visited;243return get(Ty, Visited);244}245246Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {247// If we already have an entry for this type, return it.248Type **Entry = &MappedTypes[Ty];249if (*Entry)250return *Entry;251252// These are types that LLVM itself will unique.253bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();254255if (!IsUniqued) {256#ifndef NDEBUG257for (auto &Pair : MappedTypes) {258assert(!(Pair.first != Ty && Pair.second == Ty) &&259"mapping to a source type");260}261#endif262263if (!Visited.insert(cast<StructType>(Ty)).second) {264StructType *DTy = StructType::create(Ty->getContext());265return *Entry = DTy;266}267}268269// If this is not a recursive type, then just map all of the elements and270// then rebuild the type from inside out.271SmallVector<Type *, 4> ElementTypes;272273// If there are no element types to map, then the type is itself. This is274// true for the anonymous {} struct, things like 'float', integers, etc.275if (Ty->getNumContainedTypes() == 0 && IsUniqued)276return *Entry = Ty;277278// Remap all of the elements, keeping track of whether any of them change.279bool AnyChange = false;280ElementTypes.resize(Ty->getNumContainedTypes());281for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {282ElementTypes[I] = get(Ty->getContainedType(I), Visited);283AnyChange |= ElementTypes[I] != Ty->getContainedType(I);284}285286// If we found our type while recursively processing stuff, just use it.287Entry = &MappedTypes[Ty];288if (*Entry) {289if (auto *DTy = dyn_cast<StructType>(*Entry)) {290if (DTy->isOpaque()) {291auto *STy = cast<StructType>(Ty);292finishType(DTy, STy, ElementTypes);293}294}295return *Entry;296}297298// If all of the element types mapped directly over and the type is not299// a named struct, then the type is usable as-is.300if (!AnyChange && IsUniqued)301return *Entry = Ty;302303// Otherwise, rebuild a modified type.304switch (Ty->getTypeID()) {305default:306llvm_unreachable("unknown derived type to remap");307case Type::ArrayTyID:308return *Entry = ArrayType::get(ElementTypes[0],309cast<ArrayType>(Ty)->getNumElements());310case Type::ScalableVectorTyID:311case Type::FixedVectorTyID:312return *Entry = VectorType::get(ElementTypes[0],313cast<VectorType>(Ty)->getElementCount());314case Type::PointerTyID:315return *Entry = PointerType::get(ElementTypes[0],316cast<PointerType>(Ty)->getAddressSpace());317case Type::FunctionTyID:318return *Entry = FunctionType::get(ElementTypes[0],319ArrayRef(ElementTypes).slice(1),320cast<FunctionType>(Ty)->isVarArg());321case Type::StructTyID: {322auto *STy = cast<StructType>(Ty);323bool IsPacked = STy->isPacked();324if (IsUniqued)325return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);326327// If the type is opaque, we can just use it directly.328if (STy->isOpaque()) {329DstStructTypesSet.addOpaque(STy);330return *Entry = Ty;331}332333if (StructType *OldT =334DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {335STy->setName("");336return *Entry = OldT;337}338339if (!AnyChange) {340DstStructTypesSet.addNonOpaque(STy);341return *Entry = Ty;342}343344StructType *DTy = StructType::create(Ty->getContext());345finishType(DTy, STy, ElementTypes);346return *Entry = DTy;347}348}349}350351LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,352const Twine &Msg)353: DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}354void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }355356//===----------------------------------------------------------------------===//357// IRLinker implementation.358//===----------------------------------------------------------------------===//359360namespace {361class IRLinker;362363/// Creates prototypes for functions that are lazily linked on the fly. This364/// speeds up linking for modules with many/ lazily linked functions of which365/// few get used.366class GlobalValueMaterializer final : public ValueMaterializer {367IRLinker &TheIRLinker;368369public:370GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}371Value *materialize(Value *V) override;372};373374class LocalValueMaterializer final : public ValueMaterializer {375IRLinker &TheIRLinker;376377public:378LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}379Value *materialize(Value *V) override;380};381382/// Type of the Metadata map in \a ValueToValueMapTy.383typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;384385/// This is responsible for keeping track of the state used for moving data386/// from SrcM to DstM.387class IRLinker {388Module &DstM;389std::unique_ptr<Module> SrcM;390391/// See IRMover::move().392IRMover::LazyCallback AddLazyFor;393394TypeMapTy TypeMap;395GlobalValueMaterializer GValMaterializer;396LocalValueMaterializer LValMaterializer;397398/// A metadata map that's shared between IRLinker instances.399MDMapT &SharedMDs;400401/// Mapping of values from what they used to be in Src, to what they are now402/// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead403/// due to the use of Value handles which the Linker doesn't actually need,404/// but this allows us to reuse the ValueMapper code.405ValueToValueMapTy ValueMap;406ValueToValueMapTy IndirectSymbolValueMap;407408DenseSet<GlobalValue *> ValuesToLink;409std::vector<GlobalValue *> Worklist;410std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist;411412/// Set of globals with eagerly copied metadata that may require remapping.413/// This remapping is performed after metadata linking.414DenseSet<GlobalObject *> UnmappedMetadata;415416void maybeAdd(GlobalValue *GV) {417if (ValuesToLink.insert(GV).second)418Worklist.push_back(GV);419}420421/// Whether we are importing globals for ThinLTO, as opposed to linking the422/// source module. If this flag is set, it means that we can rely on some423/// other object file to define any non-GlobalValue entities defined by the424/// source module. This currently causes us to not link retained types in425/// debug info metadata and module inline asm.426bool IsPerformingImport;427428/// Set to true when all global value body linking is complete (including429/// lazy linking). Used to prevent metadata linking from creating new430/// references.431bool DoneLinkingBodies = false;432433/// The Error encountered during materialization. We use an Optional here to434/// avoid needing to manage an unconsumed success value.435std::optional<Error> FoundError;436void setError(Error E) {437if (E)438FoundError = std::move(E);439}440441/// Most of the errors produced by this module are inconvertible StringErrors.442/// This convenience function lets us return one of those more easily.443Error stringErr(const Twine &T) {444return make_error<StringError>(T, inconvertibleErrorCode());445}446447/// Entry point for mapping values and alternate context for mapping aliases.448ValueMapper Mapper;449unsigned IndirectSymbolMCID;450451/// Handles cloning of a global values from the source module into452/// the destination module, including setting the attributes and visibility.453GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);454455void emitWarning(const Twine &Message) {456SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));457}458459/// Given a global in the source module, return the global in the460/// destination module that is being linked to, if any.461GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {462// If the source has no name it can't link. If it has local linkage,463// there is no name match-up going on.464if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())465return nullptr;466467// Otherwise see if we have a match in the destination module's symtab.468GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());469if (!DGV)470return nullptr;471472// If we found a global with the same name in the dest module, but it has473// internal linkage, we are really not doing any linkage here.474if (DGV->hasLocalLinkage())475return nullptr;476477// If we found an intrinsic declaration with mismatching prototypes, we478// probably had a nameclash. Don't use that version.479if (auto *FDGV = dyn_cast<Function>(DGV))480if (FDGV->isIntrinsic())481if (const auto *FSrcGV = dyn_cast<Function>(SrcGV))482if (FDGV->getFunctionType() != TypeMap.get(FSrcGV->getFunctionType()))483return nullptr;484485// Otherwise, we do in fact link to the destination global.486return DGV;487}488489void computeTypeMapping();490491Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,492const GlobalVariable *SrcGV);493494/// Given the GlobaValue \p SGV in the source module, and the matching495/// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV496/// into the destination module.497///498/// Note this code may call the client-provided \p AddLazyFor.499bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);500Expected<Constant *> linkGlobalValueProto(GlobalValue *GV,501bool ForIndirectSymbol);502503Error linkModuleFlagsMetadata();504505void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src);506Error linkFunctionBody(Function &Dst, Function &Src);507void linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src);508void linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src);509Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);510511/// Replace all types in the source AttributeList with the512/// corresponding destination type.513AttributeList mapAttributeTypes(LLVMContext &C, AttributeList Attrs);514515/// Functions that take care of cloning a specific global value type516/// into the destination module.517GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);518Function *copyFunctionProto(const Function *SF);519GlobalValue *copyIndirectSymbolProto(const GlobalValue *SGV);520521/// Perform "replace all uses with" operations. These work items need to be522/// performed as part of materialization, but we postpone them to happen after523/// materialization is done. The materializer called by ValueMapper is not524/// expected to delete constants, as ValueMapper is holding pointers to some525/// of them, but constant destruction may be indirectly triggered by RAUW.526/// Hence, the need to move this out of the materialization call chain.527void flushRAUWWorklist();528529/// When importing for ThinLTO, prevent importing of types listed on530/// the DICompileUnit that we don't need a copy of in the importing531/// module.532void prepareCompileUnitsForImport();533void linkNamedMDNodes();534535/// Update attributes while linking.536void updateAttributes(GlobalValue &GV);537538public:539IRLinker(Module &DstM, MDMapT &SharedMDs,540IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,541ArrayRef<GlobalValue *> ValuesToLink,542IRMover::LazyCallback AddLazyFor, bool IsPerformingImport)543: DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),544TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),545SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),546Mapper(ValueMap, RF_ReuseAndMutateDistinctMDs | RF_IgnoreMissingLocals,547&TypeMap, &GValMaterializer),548IndirectSymbolMCID(Mapper.registerAlternateMappingContext(549IndirectSymbolValueMap, &LValMaterializer)) {550ValueMap.getMDMap() = std::move(SharedMDs);551for (GlobalValue *GV : ValuesToLink)552maybeAdd(GV);553if (IsPerformingImport)554prepareCompileUnitsForImport();555}556~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }557558Error run();559Value *materialize(Value *V, bool ForIndirectSymbol);560};561}562563/// The LLVM SymbolTable class autorenames globals that conflict in the symbol564/// table. This is good for all clients except for us. Go through the trouble565/// to force this back.566static void forceRenaming(GlobalValue *GV, StringRef Name) {567// If the global doesn't force its name or if it already has the right name,568// there is nothing for us to do.569if (GV->hasLocalLinkage() || GV->getName() == Name)570return;571572Module *M = GV->getParent();573574// If there is a conflict, rename the conflict.575if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {576GV->takeName(ConflictGV);577ConflictGV->setName(Name); // This will cause ConflictGV to get renamed578assert(ConflictGV->getName() != Name && "forceRenaming didn't work");579} else {580GV->setName(Name); // Force the name back581}582}583584Value *GlobalValueMaterializer::materialize(Value *SGV) {585return TheIRLinker.materialize(SGV, false);586}587588Value *LocalValueMaterializer::materialize(Value *SGV) {589return TheIRLinker.materialize(SGV, true);590}591592Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {593auto *SGV = dyn_cast<GlobalValue>(V);594if (!SGV)595return nullptr;596597// When linking a global from other modules than source & dest, skip598// materializing it because it would be mapped later when its containing599// module is linked. Linking it now would potentially pull in many types that600// may not be mapped properly.601if (SGV->getParent() != &DstM && SGV->getParent() != SrcM.get())602return nullptr;603604Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForIndirectSymbol);605if (!NewProto) {606setError(NewProto.takeError());607return nullptr;608}609if (!*NewProto)610return nullptr;611612GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);613if (!New)614return *NewProto;615616// If we already created the body, just return.617if (auto *F = dyn_cast<Function>(New)) {618if (!F->isDeclaration())619return New;620} else if (auto *V = dyn_cast<GlobalVariable>(New)) {621if (V->hasInitializer() || V->hasAppendingLinkage())622return New;623} else if (auto *GA = dyn_cast<GlobalAlias>(New)) {624if (GA->getAliasee())625return New;626} else if (auto *GI = dyn_cast<GlobalIFunc>(New)) {627if (GI->getResolver())628return New;629} else {630llvm_unreachable("Invalid GlobalValue type");631}632633// If the global is being linked for an indirect symbol, it may have already634// been scheduled to satisfy a regular symbol. Similarly, a global being linked635// for a regular symbol may have already been scheduled for an indirect636// symbol. Check for these cases by looking in the other value map and637// confirming the same value has been scheduled. If there is an entry in the638// ValueMap but the value is different, it means that the value already had a639// definition in the destination module (linkonce for instance), but we need a640// new definition for the indirect symbol ("New" will be different).641if ((ForIndirectSymbol && ValueMap.lookup(SGV) == New) ||642(!ForIndirectSymbol && IndirectSymbolValueMap.lookup(SGV) == New))643return New;644645if (ForIndirectSymbol || shouldLink(New, *SGV))646setError(linkGlobalValueBody(*New, *SGV));647648updateAttributes(*New);649return New;650}651652/// Loop through the global variables in the src module and merge them into the653/// dest module.654GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {655// No linking to be performed or linking from the source: simply create an656// identical version of the symbol over in the dest module... the657// initializer will be filled in later by LinkGlobalInits.658GlobalVariable *NewDGV =659new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),660SGVar->isConstant(), GlobalValue::ExternalLinkage,661/*init*/ nullptr, SGVar->getName(),662/*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),663SGVar->getAddressSpace());664NewDGV->setAlignment(SGVar->getAlign());665NewDGV->copyAttributesFrom(SGVar);666return NewDGV;667}668669AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) {670for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {671for (int AttrIdx = Attribute::FirstTypeAttr;672AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {673Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;674if (Attrs.hasAttributeAtIndex(i, TypedAttr)) {675if (Type *Ty =676Attrs.getAttributeAtIndex(i, TypedAttr).getValueAsType()) {677Attrs = Attrs.replaceAttributeTypeAtIndex(C, i, TypedAttr,678TypeMap.get(Ty));679break;680}681}682}683}684return Attrs;685}686687/// Link the function in the source module into the destination module if688/// needed, setting up mapping information.689Function *IRLinker::copyFunctionProto(const Function *SF) {690// If there is no linkage to be performed or we are linking from the source,691// bring SF over.692auto *F = Function::Create(TypeMap.get(SF->getFunctionType()),693GlobalValue::ExternalLinkage,694SF->getAddressSpace(), SF->getName(), &DstM);695F->copyAttributesFrom(SF);696F->setAttributes(mapAttributeTypes(F->getContext(), F->getAttributes()));697F->IsNewDbgInfoFormat = SF->IsNewDbgInfoFormat;698return F;699}700701/// Set up prototypes for any indirect symbols that come over from the source702/// module.703GlobalValue *IRLinker::copyIndirectSymbolProto(const GlobalValue *SGV) {704// If there is no linkage to be performed or we're linking from the source,705// bring over SGA.706auto *Ty = TypeMap.get(SGV->getValueType());707708if (auto *GA = dyn_cast<GlobalAlias>(SGV)) {709auto *DGA = GlobalAlias::create(Ty, SGV->getAddressSpace(),710GlobalValue::ExternalLinkage,711SGV->getName(), &DstM);712DGA->copyAttributesFrom(GA);713return DGA;714}715716if (auto *GI = dyn_cast<GlobalIFunc>(SGV)) {717auto *DGI = GlobalIFunc::create(Ty, SGV->getAddressSpace(),718GlobalValue::ExternalLinkage,719SGV->getName(), nullptr, &DstM);720DGI->copyAttributesFrom(GI);721return DGI;722}723724llvm_unreachable("Invalid source global value type");725}726727GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,728bool ForDefinition) {729GlobalValue *NewGV;730if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {731NewGV = copyGlobalVariableProto(SGVar);732} else if (auto *SF = dyn_cast<Function>(SGV)) {733NewGV = copyFunctionProto(SF);734} else {735if (ForDefinition)736NewGV = copyIndirectSymbolProto(SGV);737else if (SGV->getValueType()->isFunctionTy())738NewGV =739Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())),740GlobalValue::ExternalLinkage, SGV->getAddressSpace(),741SGV->getName(), &DstM);742else743NewGV =744new GlobalVariable(DstM, TypeMap.get(SGV->getValueType()),745/*isConstant*/ false, GlobalValue::ExternalLinkage,746/*init*/ nullptr, SGV->getName(),747/*insertbefore*/ nullptr,748SGV->getThreadLocalMode(), SGV->getAddressSpace());749}750751if (ForDefinition)752NewGV->setLinkage(SGV->getLinkage());753else if (SGV->hasExternalWeakLinkage())754NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);755756if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {757// Metadata for global variables and function declarations is copied eagerly.758if (isa<GlobalVariable>(SGV) || SGV->isDeclaration()) {759NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);760if (SGV->isDeclaration() && NewGO->hasMetadata())761UnmappedMetadata.insert(NewGO);762}763}764765// Remove these copied constants in case this stays a declaration, since766// they point to the source module. If the def is linked the values will767// be mapped in during linkFunctionBody.768if (auto *NewF = dyn_cast<Function>(NewGV)) {769NewF->setPersonalityFn(nullptr);770NewF->setPrefixData(nullptr);771NewF->setPrologueData(nullptr);772}773774return NewGV;775}776777static StringRef getTypeNamePrefix(StringRef Name) {778size_t DotPos = Name.rfind('.');779return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||780!isdigit(static_cast<unsigned char>(Name[DotPos + 1])))781? Name782: Name.substr(0, DotPos);783}784785/// Loop over all of the linked values to compute type mappings. For example,786/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct787/// types 'Foo' but one got renamed when the module was loaded into the same788/// LLVMContext.789void IRLinker::computeTypeMapping() {790for (GlobalValue &SGV : SrcM->globals()) {791GlobalValue *DGV = getLinkedToGlobal(&SGV);792if (!DGV)793continue;794795if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {796TypeMap.addTypeMapping(DGV->getType(), SGV.getType());797continue;798}799800// Unify the element type of appending arrays.801ArrayType *DAT = cast<ArrayType>(DGV->getValueType());802ArrayType *SAT = cast<ArrayType>(SGV.getValueType());803TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());804}805806for (GlobalValue &SGV : *SrcM)807if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) {808if (DGV->getType() == SGV.getType()) {809// If the types of DGV and SGV are the same, it means that DGV is from810// the source module and got added to DstM from a shared metadata. We811// shouldn't map this type to itself in case the type's components get812// remapped to a new type from DstM (for instance, during the loop over813// SrcM->getIdentifiedStructTypes() below).814continue;815}816817TypeMap.addTypeMapping(DGV->getType(), SGV.getType());818}819820for (GlobalValue &SGV : SrcM->aliases())821if (GlobalValue *DGV = getLinkedToGlobal(&SGV))822TypeMap.addTypeMapping(DGV->getType(), SGV.getType());823824// Incorporate types by name, scanning all the types in the source module.825// At this point, the destination module may have a type "%foo = { i32 }" for826// example. When the source module got loaded into the same LLVMContext, if827// it had the same type, it would have been renamed to "%foo.42 = { i32 }".828std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();829for (StructType *ST : Types) {830if (!ST->hasName())831continue;832833if (TypeMap.DstStructTypesSet.hasType(ST)) {834// This is actually a type from the destination module.835// getIdentifiedStructTypes() can have found it by walking debug info836// metadata nodes, some of which get linked by name when ODR Type Uniquing837// is enabled on the Context, from the source to the destination module.838continue;839}840841auto STTypePrefix = getTypeNamePrefix(ST->getName());842if (STTypePrefix.size() == ST->getName().size())843continue;844845// Check to see if the destination module has a struct with the prefix name.846StructType *DST = StructType::getTypeByName(ST->getContext(), STTypePrefix);847if (!DST)848continue;849850// Don't use it if this actually came from the source module. They're in851// the same LLVMContext after all. Also don't use it unless the type is852// actually used in the destination module. This can happen in situations853// like this:854//855// Module A Module B856// -------- --------857// %Z = type { %A } %B = type { %C.1 }858// %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }859// %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }860// %C = type { i8* } %B.3 = type { %C.1 }861//862// When we link Module B with Module A, the '%B' in Module B is863// used. However, that would then use '%C.1'. But when we process '%C.1',864// we prefer to take the '%C' version. So we are then left with both865// '%C.1' and '%C' being used for the same types. This leads to some866// variables using one type and some using the other.867if (TypeMap.DstStructTypesSet.hasType(DST))868TypeMap.addTypeMapping(DST, ST);869}870871// Now that we have discovered all of the type equivalences, get a body for872// any 'opaque' types in the dest module that are now resolved.873TypeMap.linkDefinedTypeBodies();874}875876static void getArrayElements(const Constant *C,877SmallVectorImpl<Constant *> &Dest) {878unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();879880for (unsigned i = 0; i != NumElements; ++i)881Dest.push_back(C->getAggregateElement(i));882}883884/// If there were any appending global variables, link them together now.885Expected<Constant *>886IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,887const GlobalVariable *SrcGV) {888// Check that both variables have compatible properties.889if (DstGV && !DstGV->isDeclaration() && !SrcGV->isDeclaration()) {890if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())891return stringErr(892"Linking globals named '" + SrcGV->getName() +893"': can only link appending global with another appending "894"global!");895896if (DstGV->isConstant() != SrcGV->isConstant())897return stringErr("Appending variables linked with different const'ness!");898899if (DstGV->getAlign() != SrcGV->getAlign())900return stringErr(901"Appending variables with different alignment need to be linked!");902903if (DstGV->getVisibility() != SrcGV->getVisibility())904return stringErr(905"Appending variables with different visibility need to be linked!");906907if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())908return stringErr(909"Appending variables with different unnamed_addr need to be linked!");910911if (DstGV->getSection() != SrcGV->getSection())912return stringErr(913"Appending variables with different section name need to be linked!");914915if (DstGV->getAddressSpace() != SrcGV->getAddressSpace())916return stringErr("Appending variables with different address spaces need "917"to be linked!");918}919920// Do not need to do anything if source is a declaration.921if (SrcGV->isDeclaration())922return DstGV;923924Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))925->getElementType();926927// FIXME: This upgrade is done during linking to support the C API. Once the928// old form is deprecated, we should move this upgrade to929// llvm::UpgradeGlobalVariable() and simplify the logic here and in930// Mapper::mapAppendingVariable() in ValueMapper.cpp.931StringRef Name = SrcGV->getName();932bool IsNewStructor = false;933bool IsOldStructor = false;934if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {935if (cast<StructType>(EltTy)->getNumElements() == 3)936IsNewStructor = true;937else938IsOldStructor = true;939}940941PointerType *VoidPtrTy = PointerType::get(SrcGV->getContext(), 0);942if (IsOldStructor) {943auto &ST = *cast<StructType>(EltTy);944Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};945EltTy = StructType::get(SrcGV->getContext(), Tys, false);946}947948uint64_t DstNumElements = 0;949if (DstGV && !DstGV->isDeclaration()) {950ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());951DstNumElements = DstTy->getNumElements();952953// Check to see that they two arrays agree on type.954if (EltTy != DstTy->getElementType())955return stringErr("Appending variables with different element types!");956}957958SmallVector<Constant *, 16> SrcElements;959getArrayElements(SrcGV->getInitializer(), SrcElements);960961if (IsNewStructor) {962erase_if(SrcElements, [this](Constant *E) {963auto *Key =964dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts());965if (!Key)966return false;967GlobalValue *DGV = getLinkedToGlobal(Key);968return !shouldLink(DGV, *Key);969});970}971uint64_t NewSize = DstNumElements + SrcElements.size();972ArrayType *NewType = ArrayType::get(EltTy, NewSize);973974// Create the new global variable.975GlobalVariable *NG = new GlobalVariable(976DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),977/*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),978SrcGV->getAddressSpace());979980NG->copyAttributesFrom(SrcGV);981forceRenaming(NG, SrcGV->getName());982983Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));984985Mapper.scheduleMapAppendingVariable(986*NG,987(DstGV && !DstGV->isDeclaration()) ? DstGV->getInitializer() : nullptr,988IsOldStructor, SrcElements);989990// Replace any uses of the two global variables with uses of the new991// global.992if (DstGV) {993RAUWWorklist.push_back(std::make_pair(DstGV, NG));994}995996return Ret;997}998999bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {1000if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage())1001return true;10021003if (DGV && !DGV->isDeclarationForLinker())1004return false;10051006if (SGV.isDeclaration() || DoneLinkingBodies)1007return false;10081009// Callback to the client to give a chance to lazily add the Global to the1010// list of value to link.1011bool LazilyAdded = false;1012if (AddLazyFor)1013AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {1014maybeAdd(&GV);1015LazilyAdded = true;1016});1017return LazilyAdded;1018}10191020Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,1021bool ForIndirectSymbol) {1022GlobalValue *DGV = getLinkedToGlobal(SGV);10231024bool ShouldLink = shouldLink(DGV, *SGV);10251026// just missing from map1027if (ShouldLink) {1028auto I = ValueMap.find(SGV);1029if (I != ValueMap.end())1030return cast<Constant>(I->second);10311032I = IndirectSymbolValueMap.find(SGV);1033if (I != IndirectSymbolValueMap.end())1034return cast<Constant>(I->second);1035}10361037if (!ShouldLink && ForIndirectSymbol)1038DGV = nullptr;10391040// Handle the ultra special appending linkage case first.1041if (SGV->hasAppendingLinkage() || (DGV && DGV->hasAppendingLinkage()))1042return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),1043cast<GlobalVariable>(SGV));10441045bool NeedsRenaming = false;1046GlobalValue *NewGV;1047if (DGV && !ShouldLink) {1048NewGV = DGV;1049} else {1050// If we are done linking global value bodies (i.e. we are performing1051// metadata linking), don't link in the global value due to this1052// reference, simply map it to null.1053if (DoneLinkingBodies)1054return nullptr;10551056NewGV = copyGlobalValueProto(SGV, ShouldLink || ForIndirectSymbol);1057if (ShouldLink || !ForIndirectSymbol)1058NeedsRenaming = true;1059}10601061// Overloaded intrinsics have overloaded types names as part of their1062// names. If we renamed overloaded types we should rename the intrinsic1063// as well.1064if (Function *F = dyn_cast<Function>(NewGV))1065if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {1066// Note: remangleIntrinsicFunction does not copy metadata and as such1067// F should not occur in the set of objects with unmapped metadata.1068// If this assertion fails then remangleIntrinsicFunction needs updating.1069assert(!UnmappedMetadata.count(F) && "intrinsic has unmapped metadata");1070NewGV->eraseFromParent();1071NewGV = *Remangled;1072NeedsRenaming = false;1073}10741075if (NeedsRenaming)1076forceRenaming(NewGV, SGV->getName());10771078if (ShouldLink || ForIndirectSymbol) {1079if (const Comdat *SC = SGV->getComdat()) {1080if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {1081Comdat *DC = DstM.getOrInsertComdat(SC->getName());1082DC->setSelectionKind(SC->getSelectionKind());1083GO->setComdat(DC);1084}1085}1086}10871088if (!ShouldLink && ForIndirectSymbol)1089NewGV->setLinkage(GlobalValue::InternalLinkage);10901091Constant *C = NewGV;1092// Only create a bitcast if necessary. In particular, with1093// DebugTypeODRUniquing we may reach metadata in the destination module1094// containing a GV from the source module, in which case SGV will be1095// the same as DGV and NewGV, and TypeMap.get() will assert since it1096// assumes it is being invoked on a type in the source module.1097if (DGV && NewGV != SGV) {1098C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(1099NewGV, TypeMap.get(SGV->getType()));1100}11011102if (DGV && NewGV != DGV) {1103// Schedule "replace all uses with" to happen after materializing is1104// done. It is not safe to do it now, since ValueMapper may be holding1105// pointers to constants that will get deleted if RAUW runs.1106RAUWWorklist.push_back(std::make_pair(1107DGV,1108ConstantExpr::getPointerBitCastOrAddrSpaceCast(NewGV, DGV->getType())));1109}11101111return C;1112}11131114/// Update the initializers in the Dest module now that all globals that may be1115/// referenced are in Dest.1116void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {1117// Figure out what the initializer looks like in the dest module.1118Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());1119}11201121/// Copy the source function over into the dest function and fix up references1122/// to values. At this point we know that Dest is an external function, and1123/// that Src is not.1124Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {1125assert(Dst.isDeclaration() && !Src.isDeclaration());11261127// Materialize if needed.1128if (Error Err = Src.materialize())1129return Err;11301131// Link in the operands without remapping.1132if (Src.hasPrefixData())1133Dst.setPrefixData(Src.getPrefixData());1134if (Src.hasPrologueData())1135Dst.setPrologueData(Src.getPrologueData());1136if (Src.hasPersonalityFn())1137Dst.setPersonalityFn(Src.getPersonalityFn());1138assert(Src.IsNewDbgInfoFormat == Dst.IsNewDbgInfoFormat);11391140// Copy over the metadata attachments without remapping.1141Dst.copyMetadata(&Src, 0);11421143// Steal arguments and splice the body of Src into Dst.1144Dst.stealArgumentListFrom(Src);1145Dst.splice(Dst.end(), &Src);11461147// Everything has been moved over. Remap it.1148Mapper.scheduleRemapFunction(Dst);1149return Error::success();1150}11511152void IRLinker::linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src) {1153Mapper.scheduleMapGlobalAlias(Dst, *Src.getAliasee(), IndirectSymbolMCID);1154}11551156void IRLinker::linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src) {1157Mapper.scheduleMapGlobalIFunc(Dst, *Src.getResolver(), IndirectSymbolMCID);1158}11591160Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {1161if (auto *F = dyn_cast<Function>(&Src))1162return linkFunctionBody(cast<Function>(Dst), *F);1163if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {1164linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar);1165return Error::success();1166}1167if (auto *GA = dyn_cast<GlobalAlias>(&Src)) {1168linkAliasAliasee(cast<GlobalAlias>(Dst), *GA);1169return Error::success();1170}1171linkIFuncResolver(cast<GlobalIFunc>(Dst), cast<GlobalIFunc>(Src));1172return Error::success();1173}11741175void IRLinker::flushRAUWWorklist() {1176for (const auto &Elem : RAUWWorklist) {1177GlobalValue *Old;1178Value *New;1179std::tie(Old, New) = Elem;11801181Old->replaceAllUsesWith(New);1182Old->eraseFromParent();1183}1184RAUWWorklist.clear();1185}11861187void IRLinker::prepareCompileUnitsForImport() {1188NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");1189if (!SrcCompileUnits)1190return;1191// When importing for ThinLTO, prevent importing of types listed on1192// the DICompileUnit that we don't need a copy of in the importing1193// module. They will be emitted by the originating module.1194for (MDNode *N : SrcCompileUnits->operands()) {1195auto *CU = cast<DICompileUnit>(N);1196assert(CU && "Expected valid compile unit");1197// Enums, macros, and retained types don't need to be listed on the1198// imported DICompileUnit. This means they will only be imported1199// if reached from the mapped IR.1200CU->replaceEnumTypes(nullptr);1201CU->replaceMacros(nullptr);1202CU->replaceRetainedTypes(nullptr);12031204// The original definition (or at least its debug info - if the variable is1205// internalized and optimized away) will remain in the source module, so1206// there's no need to import them.1207// If LLVM ever does more advanced optimizations on global variables1208// (removing/localizing write operations, for instance) that can track1209// through debug info, this decision may need to be revisited - but do so1210// with care when it comes to debug info size. Emitting small CUs containing1211// only a few imported entities into every destination module may be very1212// size inefficient.1213CU->replaceGlobalVariables(nullptr);12141215CU->replaceImportedEntities(nullptr);1216}1217}12181219/// Insert all of the named MDNodes in Src into the Dest module.1220void IRLinker::linkNamedMDNodes() {1221const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();1222for (const NamedMDNode &NMD : SrcM->named_metadata()) {1223// Don't link module flags here. Do them separately.1224if (&NMD == SrcModFlags)1225continue;1226// Don't import pseudo probe descriptors here for thinLTO. They will be1227// emitted by the originating module.1228if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName) {1229if (!DstM.getNamedMetadata(NMD.getName()))1230emitWarning("Pseudo-probe ignored: source module '" +1231SrcM->getModuleIdentifier() +1232"' is compiled with -fpseudo-probe-for-profiling while "1233"destination module '" +1234DstM.getModuleIdentifier() + "' is not\n");1235continue;1236}1237// The stats are computed per module and will all be merged in the binary.1238// Importing the metadata will cause duplication of the stats.1239if (IsPerformingImport && NMD.getName() == "llvm.stats")1240continue;12411242NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());1243// Add Src elements into Dest node.1244for (const MDNode *Op : NMD.operands())1245DestNMD->addOperand(Mapper.mapMDNode(*Op));1246}1247}12481249/// Merge the linker flags in Src into the Dest module.1250Error IRLinker::linkModuleFlagsMetadata() {1251// If the source module has no module flags, we are done.1252const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();1253if (!SrcModFlags)1254return Error::success();12551256// Check for module flag for updates before do anything.1257UpgradeModuleFlags(*SrcM);12581259// If the destination module doesn't have module flags yet, then just copy1260// over the source module's flags.1261NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();1262if (DstModFlags->getNumOperands() == 0) {1263for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)1264DstModFlags->addOperand(SrcModFlags->getOperand(I));12651266return Error::success();1267}12681269// First build a map of the existing module flags and requirements.1270DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;1271SmallSetVector<MDNode *, 16> Requirements;1272SmallVector<unsigned, 0> Mins;1273DenseSet<MDString *> SeenMin;1274for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {1275MDNode *Op = DstModFlags->getOperand(I);1276uint64_t Behavior =1277mdconst::extract<ConstantInt>(Op->getOperand(0))->getZExtValue();1278MDString *ID = cast<MDString>(Op->getOperand(1));12791280if (Behavior == Module::Require) {1281Requirements.insert(cast<MDNode>(Op->getOperand(2)));1282} else {1283if (Behavior == Module::Min)1284Mins.push_back(I);1285Flags[ID] = std::make_pair(Op, I);1286}1287}12881289// Merge in the flags from the source module, and also collect its set of1290// requirements.1291for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {1292MDNode *SrcOp = SrcModFlags->getOperand(I);1293ConstantInt *SrcBehavior =1294mdconst::extract<ConstantInt>(SrcOp->getOperand(0));1295MDString *ID = cast<MDString>(SrcOp->getOperand(1));1296MDNode *DstOp;1297unsigned DstIndex;1298std::tie(DstOp, DstIndex) = Flags.lookup(ID);1299unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();1300SeenMin.insert(ID);13011302// If this is a requirement, add it and continue.1303if (SrcBehaviorValue == Module::Require) {1304// If the destination module does not already have this requirement, add1305// it.1306if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {1307DstModFlags->addOperand(SrcOp);1308}1309continue;1310}13111312// If there is no existing flag with this ID, just add it.1313if (!DstOp) {1314if (SrcBehaviorValue == Module::Min) {1315Mins.push_back(DstModFlags->getNumOperands());1316SeenMin.erase(ID);1317}1318Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());1319DstModFlags->addOperand(SrcOp);1320continue;1321}13221323// Otherwise, perform a merge.1324ConstantInt *DstBehavior =1325mdconst::extract<ConstantInt>(DstOp->getOperand(0));1326unsigned DstBehaviorValue = DstBehavior->getZExtValue();13271328auto overrideDstValue = [&]() {1329DstModFlags->setOperand(DstIndex, SrcOp);1330Flags[ID].first = SrcOp;1331};13321333// If either flag has override behavior, handle it first.1334if (DstBehaviorValue == Module::Override) {1335// Diagnose inconsistent flags which both have override behavior.1336if (SrcBehaviorValue == Module::Override &&1337SrcOp->getOperand(2) != DstOp->getOperand(2))1338return stringErr("linking module flags '" + ID->getString() +1339"': IDs have conflicting override values in '" +1340SrcM->getModuleIdentifier() + "' and '" +1341DstM.getModuleIdentifier() + "'");1342continue;1343} else if (SrcBehaviorValue == Module::Override) {1344// Update the destination flag to that of the source.1345overrideDstValue();1346continue;1347}13481349// Diagnose inconsistent merge behavior types.1350if (SrcBehaviorValue != DstBehaviorValue) {1351bool MinAndWarn = (SrcBehaviorValue == Module::Min &&1352DstBehaviorValue == Module::Warning) ||1353(DstBehaviorValue == Module::Min &&1354SrcBehaviorValue == Module::Warning);1355bool MaxAndWarn = (SrcBehaviorValue == Module::Max &&1356DstBehaviorValue == Module::Warning) ||1357(DstBehaviorValue == Module::Max &&1358SrcBehaviorValue == Module::Warning);1359if (!(MaxAndWarn || MinAndWarn))1360return stringErr("linking module flags '" + ID->getString() +1361"': IDs have conflicting behaviors in '" +1362SrcM->getModuleIdentifier() + "' and '" +1363DstM.getModuleIdentifier() + "'");1364}13651366auto ensureDistinctOp = [&](MDNode *DstValue) {1367assert(isa<MDTuple>(DstValue) &&1368"Expected MDTuple when appending module flags");1369if (DstValue->isDistinct())1370return dyn_cast<MDTuple>(DstValue);1371ArrayRef<MDOperand> DstOperands = DstValue->operands();1372MDTuple *New = MDTuple::getDistinct(1373DstM.getContext(),1374SmallVector<Metadata *, 4>(DstOperands.begin(), DstOperands.end()));1375Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};1376MDNode *Flag = MDTuple::getDistinct(DstM.getContext(), FlagOps);1377DstModFlags->setOperand(DstIndex, Flag);1378Flags[ID].first = Flag;1379return New;1380};13811382// Emit a warning if the values differ and either source or destination1383// request Warning behavior.1384if ((DstBehaviorValue == Module::Warning ||1385SrcBehaviorValue == Module::Warning) &&1386SrcOp->getOperand(2) != DstOp->getOperand(2)) {1387std::string Str;1388raw_string_ostream(Str)1389<< "linking module flags '" << ID->getString()1390<< "': IDs have conflicting values ('" << *SrcOp->getOperand(2)1391<< "' from " << SrcM->getModuleIdentifier() << " with '"1392<< *DstOp->getOperand(2) << "' from " << DstM.getModuleIdentifier()1393<< ')';1394emitWarning(Str);1395}13961397// Choose the minimum if either source or destination request Min behavior.1398if (DstBehaviorValue == Module::Min || SrcBehaviorValue == Module::Min) {1399ConstantInt *DstValue =1400mdconst::extract<ConstantInt>(DstOp->getOperand(2));1401ConstantInt *SrcValue =1402mdconst::extract<ConstantInt>(SrcOp->getOperand(2));14031404// The resulting flag should have a Min behavior, and contain the minimum1405// value from between the source and destination values.1406Metadata *FlagOps[] = {1407(DstBehaviorValue != Module::Min ? SrcOp : DstOp)->getOperand(0), ID,1408(SrcValue->getZExtValue() < DstValue->getZExtValue() ? SrcOp : DstOp)1409->getOperand(2)};1410MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);1411DstModFlags->setOperand(DstIndex, Flag);1412Flags[ID].first = Flag;1413continue;1414}14151416// Choose the maximum if either source or destination request Max behavior.1417if (DstBehaviorValue == Module::Max || SrcBehaviorValue == Module::Max) {1418ConstantInt *DstValue =1419mdconst::extract<ConstantInt>(DstOp->getOperand(2));1420ConstantInt *SrcValue =1421mdconst::extract<ConstantInt>(SrcOp->getOperand(2));14221423// The resulting flag should have a Max behavior, and contain the maximum1424// value from between the source and destination values.1425Metadata *FlagOps[] = {1426(DstBehaviorValue != Module::Max ? SrcOp : DstOp)->getOperand(0), ID,1427(SrcValue->getZExtValue() > DstValue->getZExtValue() ? SrcOp : DstOp)1428->getOperand(2)};1429MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);1430DstModFlags->setOperand(DstIndex, Flag);1431Flags[ID].first = Flag;1432continue;1433}14341435// Perform the merge for standard behavior types.1436switch (SrcBehaviorValue) {1437case Module::Require:1438case Module::Override:1439llvm_unreachable("not possible");1440case Module::Error: {1441// Emit an error if the values differ.1442if (SrcOp->getOperand(2) != DstOp->getOperand(2))1443return stringErr("linking module flags '" + ID->getString() +1444"': IDs have conflicting values in '" +1445SrcM->getModuleIdentifier() + "' and '" +1446DstM.getModuleIdentifier() + "'");1447continue;1448}1449case Module::Warning: {1450break;1451}1452case Module::Max: {1453break;1454}1455case Module::Append: {1456MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));1457MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));1458for (const auto &O : SrcValue->operands())1459DstValue->push_back(O);1460break;1461}1462case Module::AppendUnique: {1463SmallSetVector<Metadata *, 16> Elts;1464MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));1465MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));1466Elts.insert(DstValue->op_begin(), DstValue->op_end());1467Elts.insert(SrcValue->op_begin(), SrcValue->op_end());1468for (auto I = DstValue->getNumOperands(); I < Elts.size(); I++)1469DstValue->push_back(Elts[I]);1470break;1471}1472}14731474}14751476// For the Min behavior, set the value to 0 if either module does not have the1477// flag.1478for (auto Idx : Mins) {1479MDNode *Op = DstModFlags->getOperand(Idx);1480MDString *ID = cast<MDString>(Op->getOperand(1));1481if (!SeenMin.count(ID)) {1482ConstantInt *V = mdconst::extract<ConstantInt>(Op->getOperand(2));1483Metadata *FlagOps[] = {1484Op->getOperand(0), ID,1485ConstantAsMetadata::get(ConstantInt::get(V->getType(), 0))};1486DstModFlags->setOperand(Idx, MDNode::get(DstM.getContext(), FlagOps));1487}1488}14891490// Check all of the requirements.1491for (MDNode *Requirement : Requirements) {1492MDString *Flag = cast<MDString>(Requirement->getOperand(0));1493Metadata *ReqValue = Requirement->getOperand(1);14941495MDNode *Op = Flags[Flag].first;1496if (!Op || Op->getOperand(2) != ReqValue)1497return stringErr("linking module flags '" + Flag->getString() +1498"': does not have the required value");1499}1500return Error::success();1501}15021503/// Return InlineAsm adjusted with target-specific directives if required.1504/// For ARM and Thumb, we have to add directives to select the appropriate ISA1505/// to support mixing module-level inline assembly from ARM and Thumb modules.1506static std::string adjustInlineAsm(const std::string &InlineAsm,1507const Triple &Triple) {1508if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb)1509return ".text\n.balign 2\n.thumb\n" + InlineAsm;1510if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb)1511return ".text\n.balign 4\n.arm\n" + InlineAsm;1512return InlineAsm;1513}15141515void IRLinker::updateAttributes(GlobalValue &GV) {1516/// Remove nocallback attribute while linking, because nocallback attribute1517/// indicates that the function is only allowed to jump back into caller's1518/// module only by a return or an exception. When modules are linked, this1519/// property cannot be guaranteed anymore. For example, the nocallback1520/// function may contain a call to another module. But if we merge its caller1521/// and callee module here, and not the module containing the nocallback1522/// function definition itself, the nocallback property will be violated1523/// (since the nocallback function will call back into the newly merged module1524/// containing both its caller and callee). This could happen if the module1525/// containing the nocallback function definition is native code, so it does1526/// not participate in the LTO link. Note if the nocallback function does1527/// participate in the LTO link, and thus ends up in the merged module1528/// containing its caller and callee, removing the attribute doesn't hurt as1529/// it has no effect on definitions in the same module.1530if (auto *F = dyn_cast<Function>(&GV)) {1531if (!F->isIntrinsic())1532F->removeFnAttr(llvm::Attribute::NoCallback);15331534// Remove nocallback attribute when it is on a call-site.1535for (BasicBlock &BB : *F)1536for (Instruction &I : BB)1537if (CallBase *CI = dyn_cast<CallBase>(&I))1538CI->removeFnAttr(Attribute::NoCallback);1539}1540}15411542Error IRLinker::run() {1543// Ensure metadata materialized before value mapping.1544if (SrcM->getMaterializer())1545if (Error Err = SrcM->getMaterializer()->materializeMetadata())1546return Err;15471548// Convert source module to match dest for the duration of the link.1549ScopedDbgInfoFormatSetter FormatSetter(*SrcM, DstM.IsNewDbgInfoFormat);15501551// Inherit the target data from the source module if the destination1552// module doesn't have one already.1553if (DstM.getDataLayout().isDefault())1554DstM.setDataLayout(SrcM->getDataLayout());15551556// Copy the target triple from the source to dest if the dest's is empty.1557if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())1558DstM.setTargetTriple(SrcM->getTargetTriple());15591560Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());15611562// During CUDA compilation we have to link with the bitcode supplied with1563// CUDA. libdevice bitcode either has no data layout set (pre-CUDA-11), or has1564// the layout that is different from the one used by LLVM/clang (it does not1565// include i128). Issuing a warning is not very helpful as there's not much1566// the user can do about it.1567bool EnableDLWarning = true;1568bool EnableTripleWarning = true;1569if (SrcTriple.isNVPTX() && DstTriple.isNVPTX()) {1570std::string ModuleId = SrcM->getModuleIdentifier();1571StringRef FileName = llvm::sys::path::filename(ModuleId);1572bool SrcIsLibDevice =1573FileName.starts_with("libdevice") && FileName.ends_with(".10.bc");1574bool SrcHasLibDeviceDL =1575(SrcM->getDataLayoutStr().empty() ||1576SrcM->getDataLayoutStr() == "e-i64:64-v16:16-v32:32-n16:32:64");1577// libdevice bitcode uses nvptx64-nvidia-gpulibs or just1578// 'nvptx-unknown-unknown' triple (before CUDA-10.x) and is compatible with1579// all NVPTX variants.1580bool SrcHasLibDeviceTriple = (SrcTriple.getVendor() == Triple::NVIDIA &&1581SrcTriple.getOSName() == "gpulibs") ||1582(SrcTriple.getVendorName() == "unknown" &&1583SrcTriple.getOSName() == "unknown");1584EnableTripleWarning = !(SrcIsLibDevice && SrcHasLibDeviceTriple);1585EnableDLWarning = !(SrcIsLibDevice && SrcHasLibDeviceDL);1586}15871588if (EnableDLWarning && (SrcM->getDataLayout() != DstM.getDataLayout())) {1589emitWarning("Linking two modules of different data layouts: '" +1590SrcM->getModuleIdentifier() + "' is '" +1591SrcM->getDataLayoutStr() + "' whereas '" +1592DstM.getModuleIdentifier() + "' is '" +1593DstM.getDataLayoutStr() + "'\n");1594}15951596if (EnableTripleWarning && !SrcM->getTargetTriple().empty() &&1597!SrcTriple.isCompatibleWith(DstTriple))1598emitWarning("Linking two modules of different target triples: '" +1599SrcM->getModuleIdentifier() + "' is '" +1600SrcM->getTargetTriple() + "' whereas '" +1601DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +1602"'\n");16031604DstM.setTargetTriple(SrcTriple.merge(DstTriple));16051606// Loop over all of the linked values to compute type mappings.1607computeTypeMapping();16081609std::reverse(Worklist.begin(), Worklist.end());1610while (!Worklist.empty()) {1611GlobalValue *GV = Worklist.back();1612Worklist.pop_back();16131614// Already mapped.1615if (ValueMap.find(GV) != ValueMap.end() ||1616IndirectSymbolValueMap.find(GV) != IndirectSymbolValueMap.end())1617continue;16181619assert(!GV->isDeclaration());1620Mapper.mapValue(*GV);1621if (FoundError)1622return std::move(*FoundError);1623flushRAUWWorklist();1624}16251626// Note that we are done linking global value bodies. This prevents1627// metadata linking from creating new references.1628DoneLinkingBodies = true;1629Mapper.addFlags(RF_NullMapMissingGlobalValues);16301631// Remap all of the named MDNodes in Src into the DstM module. We do this1632// after linking GlobalValues so that MDNodes that reference GlobalValues1633// are properly remapped.1634linkNamedMDNodes();16351636// Clean up any global objects with potentially unmapped metadata.1637// Specifically declarations which did not become definitions.1638for (GlobalObject *NGO : UnmappedMetadata) {1639if (NGO->isDeclaration())1640Mapper.remapGlobalObjectMetadata(*NGO);1641}16421643if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {1644// Append the module inline asm string.1645DstM.appendModuleInlineAsm(adjustInlineAsm(SrcM->getModuleInlineAsm(),1646SrcTriple));1647} else if (IsPerformingImport) {1648// Import any symver directives for symbols in DstM.1649ModuleSymbolTable::CollectAsmSymvers(*SrcM,1650[&](StringRef Name, StringRef Alias) {1651if (DstM.getNamedValue(Name)) {1652SmallString<256> S(".symver ");1653S += Name;1654S += ", ";1655S += Alias;1656DstM.appendModuleInlineAsm(S);1657}1658});1659}16601661// Reorder the globals just added to the destination module to match their1662// original order in the source module.1663for (GlobalVariable &GV : SrcM->globals()) {1664if (GV.hasAppendingLinkage())1665continue;1666Value *NewValue = Mapper.mapValue(GV);1667if (NewValue) {1668auto *NewGV = dyn_cast<GlobalVariable>(NewValue->stripPointerCasts());1669if (NewGV) {1670NewGV->removeFromParent();1671DstM.insertGlobalVariable(NewGV);1672}1673}1674}16751676// Merge the module flags into the DstM module.1677return linkModuleFlagsMetadata();1678}16791680IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)1681: ETypes(E), IsPacked(P) {}16821683IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)1684: ETypes(ST->elements()), IsPacked(ST->isPacked()) {}16851686bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {1687return IsPacked == That.IsPacked && ETypes == That.ETypes;1688}16891690bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {1691return !this->operator==(That);1692}16931694StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {1695return DenseMapInfo<StructType *>::getEmptyKey();1696}16971698StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {1699return DenseMapInfo<StructType *>::getTombstoneKey();1700}17011702unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {1703return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),1704Key.IsPacked);1705}17061707unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {1708return getHashValue(KeyTy(ST));1709}17101711bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,1712const StructType *RHS) {1713if (RHS == getEmptyKey() || RHS == getTombstoneKey())1714return false;1715return LHS == KeyTy(RHS);1716}17171718bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,1719const StructType *RHS) {1720if (RHS == getEmptyKey() || RHS == getTombstoneKey())1721return LHS == RHS;1722return KeyTy(LHS) == KeyTy(RHS);1723}17241725void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {1726assert(!Ty->isOpaque());1727NonOpaqueStructTypes.insert(Ty);1728}17291730void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {1731assert(!Ty->isOpaque());1732NonOpaqueStructTypes.insert(Ty);1733bool Removed = OpaqueStructTypes.erase(Ty);1734(void)Removed;1735assert(Removed);1736}17371738void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {1739assert(Ty->isOpaque());1740OpaqueStructTypes.insert(Ty);1741}17421743StructType *1744IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,1745bool IsPacked) {1746IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);1747auto I = NonOpaqueStructTypes.find_as(Key);1748return I == NonOpaqueStructTypes.end() ? nullptr : *I;1749}17501751bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {1752if (Ty->isOpaque())1753return OpaqueStructTypes.count(Ty);1754auto I = NonOpaqueStructTypes.find(Ty);1755return I == NonOpaqueStructTypes.end() ? false : *I == Ty;1756}17571758IRMover::IRMover(Module &M) : Composite(M) {1759TypeFinder StructTypes;1760StructTypes.run(M, /* OnlyNamed */ false);1761for (StructType *Ty : StructTypes) {1762if (Ty->isOpaque())1763IdentifiedStructTypes.addOpaque(Ty);1764else1765IdentifiedStructTypes.addNonOpaque(Ty);1766}1767// Self-map metadatas in the destination module. This is needed when1768// DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the1769// destination module may be reached from the source module.1770for (const auto *MD : StructTypes.getVisitedMetadata()) {1771SharedMDs[MD].reset(const_cast<MDNode *>(MD));1772}1773}17741775Error IRMover::move(std::unique_ptr<Module> Src,1776ArrayRef<GlobalValue *> ValuesToLink,1777LazyCallback AddLazyFor, bool IsPerformingImport) {1778IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,1779std::move(Src), ValuesToLink, std::move(AddLazyFor),1780IsPerformingImport);1781Error E = TheIRLinker.run();1782Composite.dropTriviallyDeadConstantArrays();1783return E;1784}178517861787