26enum class LinkFrom { Dst, Src, Both };
32 std::unique_ptr<Module> SrcM;
34 SetVector<GlobalValue *> ValuesToLink;
40 StringSet<> Internalize;
45 std::function<void(
Module &,
const StringSet<> &)> InternalizeCallback;
56 bool shouldLinkFromSource(
bool &LinkFromSrc,
const GlobalValue &Dest,
57 const GlobalValue &Src);
60 bool emitError(
const Twine &Message) {
61 SrcM->getContext().diagnose(LinkDiagnosticInfo(
DS_Error, Message));
65 bool getComdatLeader(
Module &M, StringRef ComdatName,
66 const GlobalVariable *&GVar);
67 bool computeResultingSelectionKind(StringRef ComdatName,
72 DenseMap<const Comdat *, std::pair<Comdat::SelectionKind, LinkFrom>>
77 DenseMap<const Comdat *, std::vector<GlobalValue *>> LazyComdatMembers;
81 GlobalValue *getLinkedToGlobal(
const GlobalValue *SrcGV) {
82 Module &DstM = Mover.getModule();
104 void dropReplacedComdat(GlobalValue &GV,
105 const DenseSet<const Comdat *> &ReplacedDstComdats);
107 bool linkIfNeeded(GlobalValue &GV, SmallVectorImpl<GlobalValue *> &GVToClone);
110 ModuleLinker(IRMover &Mover, std::unique_ptr<Module> SrcM,
unsigned Flags,
111 std::function<
void(
Module &,
const StringSet<> &)>
112 InternalizeCallback = {})
113 : Mover(Mover), SrcM(std::
move(SrcM)), Flags(Flags),
114 InternalizeCallback(std::
move(InternalizeCallback)) {}
133 const GlobalValue *GVal =
M.getNamedValue(ComdatName);
138 return emitError(
"Linking COMDATs named '" + ComdatName +
139 "': COMDAT key involves incomputable alias size.");
145 "Linking COMDATs named '" + ComdatName +
146 "': GlobalVariable required for data dependent selection!");
151bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
159 bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
160 Dst == Comdat::SelectionKind::Largest;
161 bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
162 Src == Comdat::SelectionKind::Largest;
163 if (DstAnyOrLargest && SrcAnyOrLargest) {
164 if (Dst == Comdat::SelectionKind::Largest ||
165 Src == Comdat::SelectionKind::Largest)
166 Result = Comdat::SelectionKind::Largest;
168 Result = Comdat::SelectionKind::Any;
169 }
else if (Src == Dst) {
172 return emitError(
"Linking COMDATs named '" + ComdatName +
173 "': invalid selection kinds!");
177 case Comdat::SelectionKind::Any:
179 From = LinkFrom::Dst;
181 case Comdat::SelectionKind::NoDeduplicate:
182 From = LinkFrom::Both;
184 case Comdat::SelectionKind::ExactMatch:
185 case Comdat::SelectionKind::Largest:
186 case Comdat::SelectionKind::SameSize: {
187 const GlobalVariable *DstGV;
188 const GlobalVariable *SrcGV;
189 if (getComdatLeader(DstM, ComdatName, DstGV) ||
190 getComdatLeader(*SrcM, ComdatName, SrcGV))
194 const DataLayout &SrcDL = SrcM->getDataLayout();
197 if (Result == Comdat::SelectionKind::ExactMatch) {
199 return emitError(
"Linking COMDATs named '" + ComdatName +
200 "': ExactMatch violated!");
201 From = LinkFrom::Dst;
202 }
else if (Result == Comdat::SelectionKind::Largest) {
203 From = SrcSize > DstSize ? LinkFrom::Src : LinkFrom::Dst;
204 }
else if (Result == Comdat::SelectionKind::SameSize) {
205 if (SrcSize != DstSize)
206 return emitError(
"Linking COMDATs named '" + ComdatName +
207 "': SameSize violated!");
208 From = LinkFrom::Dst;
219bool ModuleLinker::getComdatResult(
const Comdat *SrcC,
224 StringRef ComdatName = SrcC->
getName();
228 if (DstCI == ComdatSymTab.
end()) {
230 From = LinkFrom::Src;
235 const Comdat *DstC = &DstCI->second;
237 return computeResultingSelectionKind(ComdatName, SSK, DSK, Result, From);
240bool ModuleLinker::shouldLinkFromSource(
bool &LinkFromSrc,
241 const GlobalValue &Dest,
242 const GlobalValue &Src) {
245 if (shouldOverrideFromSrc()) {
256 bool SrcIsDeclaration = Src.isDeclarationForLinker();
259 if (SrcIsDeclaration) {
262 if (Src.hasDLLImportStorageClass()) {
264 LinkFromSrc = DestIsDeclaration;
277 if (DestIsDeclaration) {
283 if (Src.hasCommonLinkage()) {
296 uint64_t SrcSize =
DL.getTypeAllocSize(Src.getValueType());
297 LinkFromSrc = SrcSize > DestSize;
301 if (Src.isWeakForLinker()) {
315 assert(Src.hasExternalLinkage());
320 assert(!Src.hasExternalWeakLinkage());
323 "Unexpected linkage type!");
324 return emitError(
"Linking globals named '" + Src.getName() +
325 "': symbol multiply defined!");
328bool ModuleLinker::linkIfNeeded(GlobalValue &GV,
329 SmallVectorImpl<GlobalValue *> &GVToClone) {
330 GlobalValue *DGV = getLinkedToGlobal(&GV);
332 if (shouldLinkOnlyNeeded()) {
348 if (DGVar && SGVar) {
349 if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
350 (!DGVar->isConstant() || !SGVar->isConstant())) {
351 DGVar->setConstant(
false);
352 SGVar->setConstant(
false);
354 if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
355 MaybeAlign DAlign = DGVar->getAlign();
356 MaybeAlign SAlign = SGVar->getAlign();
357 MaybeAlign
Align = std::nullopt;
358 if (DAlign || SAlign)
361 SGVar->setAlignment(Align);
362 DGVar->setAlignment(Align);
377 if (!DGV && !shouldOverrideFromSrc() &&
385 LinkFrom ComdatFrom = LinkFrom::Dst;
387 std::tie(std::ignore, ComdatFrom) = ComdatsChosen[SC];
388 if (ComdatFrom == LinkFrom::Dst)
392 bool LinkFromSrc =
true;
393 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
395 if (DGV && ComdatFrom == LinkFrom::Both)
396 GVToClone.
push_back(LinkFromSrc ? DGV : &GV);
405 !shouldLinkOnlyNeeded())
408 if (InternalizeCallback)
415 for (GlobalValue *GV2 : LazyComdatMembers[SC]) {
416 GlobalValue *DGV = getLinkedToGlobal(GV2);
417 bool LinkFromSrc =
true;
418 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, *GV2))
422 if (InternalizeCallback)
423 Internalize.
insert(GV2->getName());
428void ModuleLinker::dropReplacedComdat(
429 GlobalValue &GV,
const DenseSet<const Comdat *> &ReplacedDstComdats) {
433 if (!ReplacedDstComdats.
count(
C))
443 Var->setInitializer(
nullptr);
446 Module &
M = *Alias.getParent();
447 GlobalValue *Declaration;
452 new GlobalVariable(M, Alias.getValueType(),
false,
457 Alias.replaceAllUsesWith(Declaration);
458 Alias.eraseFromParent();
462bool ModuleLinker::run() {
464 DenseSet<const Comdat *> ReplacedDstComdats;
465 DenseSet<const Comdat *> NonPrevailingComdats;
467 for (
const auto &SMEC : SrcM->getComdatSymbolTable()) {
468 const Comdat &
C = SMEC.getValue();
469 if (ComdatsChosen.
count(&
C))
473 if (getComdatResult(&
C, SK, From))
475 ComdatsChosen[&
C] = std::make_pair(SK, From);
477 if (From == LinkFrom::Dst)
478 NonPrevailingComdats.
insert(&
C);
480 if (From != LinkFrom::Src)
485 if (DstCI == ComdatSymTab.
end())
489 const Comdat *DstC = &DstCI->second;
490 ReplacedDstComdats.
insert(DstC);
496 dropReplacedComdat(GV, ReplacedDstComdats);
499 dropReplacedComdat(GV, ReplacedDstComdats);
502 dropReplacedComdat(GV, ReplacedDstComdats);
504 if (!NonPrevailingComdats.
empty()) {
505 DenseSet<GlobalObject *> AliasedGlobals;
506 for (
auto &GA : SrcM->aliases())
507 if (GlobalObject *GO = GA.getAliaseeObject(); GO && GO->getComdat())
508 AliasedGlobals.
insert(GO);
509 for (
const Comdat *
C : NonPrevailingComdats) {
511 for (GlobalObject *GO :
C->getUsers())
512 if (GO->hasPrivateLinkage() && !AliasedGlobals.
contains(GO))
514 for (GlobalObject *GO : ToUpdate) {
516 GO->setComdat(
nullptr);
521 for (GlobalVariable &GV : SrcM->globals())
524 LazyComdatMembers[SC].push_back(&GV);
526 for (Function &SF : *SrcM)
527 if (SF.hasLinkOnceLinkage())
528 if (
const Comdat *SC = SF.getComdat())
529 LazyComdatMembers[SC].push_back(&SF);
531 for (GlobalAlias &GA : SrcM->aliases())
532 if (GA.hasLinkOnceLinkage())
533 if (
const Comdat *SC = GA.getComdat())
534 LazyComdatMembers[SC].push_back(&GA);
539 for (GlobalVariable &GV : SrcM->globals())
540 if (linkIfNeeded(GV, GVToClone))
543 for (Function &SF : *SrcM)
544 if (linkIfNeeded(SF, GVToClone))
547 for (GlobalAlias &GA : SrcM->aliases())
548 if (linkIfNeeded(GA, GVToClone))
551 for (GlobalIFunc &GI : SrcM->ifuncs())
552 if (linkIfNeeded(GI, GVToClone))
559 for (GlobalValue *GV : GVToClone) {
561 auto *NewVar =
new GlobalVariable(*Var->getParent(), Var->getValueType(),
562 Var->isConstant(), Var->getLinkage(),
563 Var->getInitializer());
564 NewVar->copyAttributesFrom(Var);
567 NewVar->setDSOLocal(
true);
568 NewVar->setComdat(Var->getComdat());
569 if (Var->getParent() != &Mover.
getModule())
570 ValuesToLink.
insert(NewVar);
572 emitError(
"linking '" + GV->
getName() +
573 "': non-variables in comdat nodeduplicate are not handled");
577 for (
unsigned I = 0;
I < ValuesToLink.
size(); ++
I) {
578 GlobalValue *GV = ValuesToLink[
I];
582 for (GlobalValue *GV2 : LazyComdatMembers[SC]) {
583 GlobalValue *DGV = getLinkedToGlobal(GV2);
584 bool LinkFromSrc =
true;
585 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, *GV2))
592 if (InternalizeCallback) {
593 for (GlobalValue *GV : ValuesToLink)
599 bool HasErrors =
false;
601 Mover.
move(std::move(SrcM), ValuesToLink.getArrayRef(),
615 if (InternalizeCallback)
616 InternalizeCallback(DstM, Internalize);
624 std::unique_ptr<Module> Src,
unsigned Flags,
626 ModuleLinker ModLinker(Mover, std::move(Src),
Flags,
627 std::move(InternalizeCallback));
628 return ModLinker.run();
641 Module &Dest, std::unique_ptr<Module> Src,
unsigned Flags,
644 return L.linkInModule(std::move(Src),
Flags, std::move(InternalizeCallback));
653 std::unique_ptr<Module> M(
unwrap(Src));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Module.h This file contains the declarations for the Module class.
static GlobalValue::VisibilityTypes getMinVisibility(GlobalValue::VisibilityTypes A, GlobalValue::VisibilityTypes B)
Machine Check Debug Module
This file implements a set that has insertion order iteration characteristics.
LLVM_ABI StringRef getName() const
SelectionKind getSelectionKind() const
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
virtual std::string message() const
Return the error message as a string.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
bool hasLinkOnceLinkage() const
bool hasExternalLinkage() const
VisibilityTypes getVisibility() const
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LinkageTypes getLinkage() const
void setUnnamedAddr(UnnamedAddr Val)
bool hasLocalLinkage() const
LLVM_ABI const Comdat * getComdat() const
bool hasExternalWeakLinkage() const
bool isDeclarationForLinker() const
static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B)
LLVM_ABI const GlobalObject * getAliaseeObject() const
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
@ DefaultVisibility
The GV is visible.
@ HiddenVisibility
The GV is hidden.
@ ProtectedVisibility
The GV is protected.
void setVisibility(VisibilityTypes V)
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
bool hasWeakLinkage() const
bool hasCommonLinkage() const
UnnamedAddr getUnnamedAddr() const
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
bool hasAppendingLinkage() const
bool hasAvailableExternallyLinkage() const
@ PrivateLinkage
Like Internal, but omit from symbol table.
@ ExternalLinkage
Externally visible function.
@ AvailableExternallyLinkage
Available for inspection, not emission.
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI Error move(std::unique_ptr< Module > Src, ArrayRef< GlobalValue * > ValuesToLink, LazyCallback AddLazyFor, bool IsPerformingImport)
Move in the provide values in ValuesToLink from Src.
std::function< void(GlobalValue &)> ValueAdder
llvm::unique_function< void(GlobalValue &GV, ValueAdder Add)> LazyCallback
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
@ OverrideFromSrc
Have symbols from Src shadow those in the Dest.
LLVM_ABI bool linkInModule(std::unique_ptr< Module > Src, unsigned Flags=Flags::None, std::function< void(Module &, const StringSet<> &)> InternalizeCallback={})
Link Src into the composite.
static LLVM_ABI bool linkModules(Module &Dest, std::unique_ptr< Module > Src, unsigned Flags=Flags::None, std::function< void(Module &, const StringSet<> &)> InternalizeCallback={})
This function links two modules together, with the resulting Dest module modified to be the composite...
LLVM_ABI Linker(Module &M)
A Module instance is used to store all the information related to an LLVM module.
LLVMContext & getContext() const
Get the global data context.
const ComdatSymTabType & getComdatSymbolTable() const
Get the Module's symbol table for COMDATs (constant).
iterator_range< alias_iterator > aliases()
iterator_range< global_iterator > globals()
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
size_type size() const
Determine the number of elements in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
void push_back(const T &Elt)
iterator find(StringRef Key)
StringMapIterBase< Comdat, false > iterator
StringRef - Represent a constant reference to a string, i.e.
StringSet - A wrapper for StringMap that provides set-like functionality.
std::pair< typename Base::iterator, bool > insert(StringRef key)
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
std::pair< iterator, bool > insert(const ValueT &V)
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
LLVM_C_ABI LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src)
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ C
The default llvm calling convention, compatible with C.
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
auto dyn_cast_or_null(const Y &Val)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Attribute unwrap(LLVMAttributeRef Attr)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.