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()) {
299 LinkFromSrc = SrcSize > DestSize;
303 if (Src.isWeakForLinker()) {
317 assert(Src.hasExternalLinkage());
322 assert(!Src.hasExternalWeakLinkage());
325 "Unexpected linkage type!");
326 return emitError(
"Linking globals named '" + Src.getName() +
327 "': symbol multiply defined!");
330bool ModuleLinker::linkIfNeeded(GlobalValue &GV,
331 SmallVectorImpl<GlobalValue *> &GVToClone) {
332 GlobalValue *DGV = getLinkedToGlobal(&GV);
334 if (shouldLinkOnlyNeeded()) {
350 if (DGVar && SGVar) {
351 if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
352 (!DGVar->isConstant() || !SGVar->isConstant())) {
353 DGVar->setConstant(
false);
354 SGVar->setConstant(
false);
356 if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
357 MaybeAlign DAlign = DGVar->getAlign();
358 MaybeAlign SAlign = SGVar->getAlign();
359 MaybeAlign
Align = std::nullopt;
360 if (DAlign || SAlign)
363 SGVar->setAlignment(Align);
364 DGVar->setAlignment(Align);
379 if (!DGV && !shouldOverrideFromSrc() &&
387 LinkFrom ComdatFrom = LinkFrom::Dst;
389 std::tie(std::ignore, ComdatFrom) = ComdatsChosen[SC];
390 if (ComdatFrom == LinkFrom::Dst)
394 bool LinkFromSrc =
true;
395 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
397 if (DGV && ComdatFrom == LinkFrom::Both)
398 GVToClone.
push_back(LinkFromSrc ? DGV : &GV);
407 !shouldLinkOnlyNeeded())
410 if (InternalizeCallback)
417 for (GlobalValue *GV2 : LazyComdatMembers[SC]) {
418 GlobalValue *DGV = getLinkedToGlobal(GV2);
419 bool LinkFromSrc =
true;
420 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, *GV2))
424 if (InternalizeCallback)
425 Internalize.
insert(GV2->getName());
430void ModuleLinker::dropReplacedComdat(
431 GlobalValue &GV,
const DenseSet<const Comdat *> &ReplacedDstComdats) {
435 if (!ReplacedDstComdats.
count(
C))
445 Var->setInitializer(
nullptr);
448 Module &
M = *Alias.getParent();
449 GlobalValue *Declaration;
454 new GlobalVariable(M, Alias.getValueType(),
false,
459 Alias.replaceAllUsesWith(Declaration);
460 Alias.eraseFromParent();
464bool ModuleLinker::run() {
466 DenseSet<const Comdat *> ReplacedDstComdats;
467 DenseSet<const Comdat *> NonPrevailingComdats;
469 for (
const auto &SMEC : SrcM->getComdatSymbolTable()) {
470 const Comdat &
C = SMEC.getValue();
471 if (ComdatsChosen.
count(&
C))
475 if (getComdatResult(&
C, SK, From))
477 ComdatsChosen[&
C] = std::make_pair(SK, From);
479 if (From == LinkFrom::Dst)
480 NonPrevailingComdats.
insert(&
C);
482 if (From != LinkFrom::Src)
487 if (DstCI == ComdatSymTab.
end())
491 const Comdat *DstC = &DstCI->second;
492 ReplacedDstComdats.
insert(DstC);
498 dropReplacedComdat(GV, ReplacedDstComdats);
501 dropReplacedComdat(GV, ReplacedDstComdats);
504 dropReplacedComdat(GV, ReplacedDstComdats);
506 if (!NonPrevailingComdats.
empty()) {
507 DenseSet<GlobalObject *> AliasedGlobals;
508 for (
auto &GA : SrcM->aliases())
509 if (GlobalObject *GO = GA.getAliaseeObject(); GO && GO->getComdat())
510 AliasedGlobals.
insert(GO);
511 for (
const Comdat *
C : NonPrevailingComdats) {
513 for (GlobalObject *GO :
C->getUsers())
514 if (GO->hasPrivateLinkage() && !AliasedGlobals.
contains(GO))
516 for (GlobalObject *GO : ToUpdate) {
518 GO->setComdat(
nullptr);
523 for (GlobalVariable &GV : SrcM->globals())
526 LazyComdatMembers[SC].push_back(&GV);
528 for (Function &SF : *SrcM)
529 if (SF.hasLinkOnceLinkage())
530 if (
const Comdat *SC = SF.getComdat())
531 LazyComdatMembers[SC].push_back(&SF);
533 for (GlobalAlias &GA : SrcM->aliases())
534 if (GA.hasLinkOnceLinkage())
535 if (
const Comdat *SC = GA.getComdat())
536 LazyComdatMembers[SC].push_back(&GA);
541 for (GlobalVariable &GV : SrcM->globals())
542 if (linkIfNeeded(GV, GVToClone))
545 for (Function &SF : *SrcM)
546 if (linkIfNeeded(SF, GVToClone))
549 for (GlobalAlias &GA : SrcM->aliases())
550 if (linkIfNeeded(GA, GVToClone))
553 for (GlobalIFunc &GI : SrcM->ifuncs())
554 if (linkIfNeeded(GI, GVToClone))
561 for (GlobalValue *GV : GVToClone) {
563 auto *NewVar =
new GlobalVariable(*Var->getParent(), Var->getValueType(),
564 Var->isConstant(), Var->getLinkage(),
565 Var->getInitializer());
566 NewVar->copyAttributesFrom(Var);
569 NewVar->setDSOLocal(
true);
570 NewVar->setComdat(Var->getComdat());
571 if (Var->getParent() != &Mover.
getModule())
572 ValuesToLink.
insert(NewVar);
574 emitError(
"linking '" + GV->
getName() +
575 "': non-variables in comdat nodeduplicate are not handled");
579 for (
unsigned I = 0;
I < ValuesToLink.
size(); ++
I) {
580 GlobalValue *GV = ValuesToLink[
I];
584 for (GlobalValue *GV2 : LazyComdatMembers[SC]) {
585 GlobalValue *DGV = getLinkedToGlobal(GV2);
586 bool LinkFromSrc =
true;
587 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, *GV2))
594 if (InternalizeCallback) {
595 for (GlobalValue *GV : ValuesToLink)
601 bool HasErrors =
false;
603 Mover.
move(std::move(SrcM), ValuesToLink.getArrayRef(),
617 if (InternalizeCallback)
618 InternalizeCallback(DstM, Internalize);
626 std::unique_ptr<Module> Src,
unsigned Flags,
628 ModuleLinker ModLinker(Mover, std::move(Src),
Flags,
629 std::move(InternalizeCallback));
630 return ModLinker.run();
643 Module &Dest, std::unique_ptr<Module> Src,
unsigned Flags,
646 return L.linkInModule(std::move(Src),
Flags, std::move(InternalizeCallback));
655 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
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.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI uint64_t getGlobalSize(const DataLayout &DL) const
Get the size of this global variable in bytes.
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.