aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm/lib/Linker/LinkModules.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm/lib/Linker/LinkModules.cpp')
-rw-r--r--contrib/llvm/lib/Linker/LinkModules.cpp105
1 files changed, 33 insertions, 72 deletions
diff --git a/contrib/llvm/lib/Linker/LinkModules.cpp b/contrib/llvm/lib/Linker/LinkModules.cpp
index cf2c4ccf523e..c0ce4bf76b9f 100644
--- a/contrib/llvm/lib/Linker/LinkModules.cpp
+++ b/contrib/llvm/lib/Linker/LinkModules.cpp
@@ -14,12 +14,13 @@
#include "LinkDiagnosticInfo.h"
#include "llvm-c/Linker.h"
#include "llvm/ADT/SetVector.h"
-#include "llvm/ADT/StringSet.h"
+#include "llvm/IR/Comdat.h"
#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Support/Error.h"
-#include "llvm/Transforms/Utils/FunctionImportUtils.h"
using namespace llvm;
namespace {
@@ -31,14 +32,17 @@ class ModuleLinker {
std::unique_ptr<Module> SrcM;
SetVector<GlobalValue *> ValuesToLink;
- StringSet<> Internalize;
/// For symbol clashes, prefer those from Src.
unsigned Flags;
- /// Functions to import from source module, all other functions are
- /// imported as declarations instead of definitions.
- DenseSet<const GlobalValue *> *GlobalsToImport;
+ /// List of global value names that should be internalized.
+ StringSet<> Internalize;
+
+ /// Function that will perform the actual internalization. The reason for a
+ /// callback is that the linker cannot call internalizeModule without
+ /// creating a circular dependency between IPO and the linker.
+ std::function<void(Module &, const StringSet<> &)> InternalizeCallback;
/// Used as the callback for lazy linking.
/// The mover has just hit GV and we have to decide if it, and other members
@@ -46,14 +50,8 @@ class ModuleLinker {
/// to Add.
void addLazyFor(GlobalValue &GV, const IRMover::ValueAdder &Add);
- bool shouldLinkReferencedLinkOnce() {
- return !(Flags & Linker::DontForceLinkLinkonceODR);
- }
bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
- bool shouldInternalizeLinkedSymbols() {
- return Flags & Linker::InternalizeLinkedSymbols;
- }
bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
const GlobalValue &Src);
@@ -108,31 +106,17 @@ class ModuleLinker {
bool linkIfNeeded(GlobalValue &GV);
- /// Helper method to check if we are importing from the current source
- /// module.
- bool isPerformingImport() const { return GlobalsToImport != nullptr; }
-
- /// If we are importing from the source module, checks if we should
- /// import SGV as a definition, otherwise import as a declaration.
- bool doImportAsDefinition(const GlobalValue *SGV);
-
public:
ModuleLinker(IRMover &Mover, std::unique_ptr<Module> SrcM, unsigned Flags,
- DenseSet<const GlobalValue *> *GlobalsToImport = nullptr)
+ std::function<void(Module &, const StringSet<> &)>
+ InternalizeCallback = {})
: Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags),
- GlobalsToImport(GlobalsToImport) {}
+ InternalizeCallback(std::move(InternalizeCallback)) {}
bool run();
};
}
-bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
- if (!isPerformingImport())
- return false;
- return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
- GlobalsToImport);
-}
-
static GlobalValue::VisibilityTypes
getMinVisibility(GlobalValue::VisibilityTypes A,
GlobalValue::VisibilityTypes B) {
@@ -266,18 +250,10 @@ bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
// We always have to add Src if it has appending linkage.
if (Src.hasAppendingLinkage()) {
- // Should have prevented importing for appending linkage in linkIfNeeded.
- assert(!isPerformingImport());
LinkFromSrc = true;
return false;
}
- if (isPerformingImport()) {
- // LinkFromSrc iff this is a global requested for importing.
- LinkFromSrc = GlobalsToImport->count(&Src);
- return false;
- }
-
bool SrcIsDeclaration = Src.isDeclarationForLinker();
bool DestIsDeclaration = Dest.isDeclarationForLinker();
@@ -383,19 +359,9 @@ bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
GV.setUnnamedAddr(UnnamedAddr);
}
- // Don't want to append to global_ctors list, for example, when we
- // are importing for ThinLTO, otherwise the global ctors and dtors
- // get executed multiple times for local variables (the latter causing
- // double frees).
- if (GV.hasAppendingLinkage() && isPerformingImport())
- return false;
-
- if (isPerformingImport()) {
- if (!doImportAsDefinition(&GV))
- return false;
- } else if (!DGV && !shouldOverrideFromSrc() &&
- (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
- GV.hasAvailableExternallyLinkage()))
+ if (!DGV && !shouldOverrideFromSrc() &&
+ (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
+ GV.hasAvailableExternallyLinkage()))
return false;
if (GV.isDeclaration())
@@ -418,17 +384,12 @@ bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
}
void ModuleLinker::addLazyFor(GlobalValue &GV, const IRMover::ValueAdder &Add) {
- if (!shouldLinkReferencedLinkOnce())
- // For ThinLTO we don't import more than what was required.
- // The client has to guarantee that the linkonce will be availabe at link
- // time (by promoting it to weak for instance).
- return;
-
// Add these to the internalize list
- if (!GV.hasLinkOnceLinkage() && !shouldLinkOnlyNeeded())
+ if (!GV.hasLinkOnceLinkage() && !GV.hasAvailableExternallyLinkage() &&
+ !shouldLinkOnlyNeeded())
return;
- if (shouldInternalizeLinkedSymbols())
+ if (InternalizeCallback)
Internalize.insert(GV.getName());
Add(GV);
@@ -442,7 +403,7 @@ void ModuleLinker::addLazyFor(GlobalValue &GV, const IRMover::ValueAdder &Add) {
return;
if (!LinkFromSrc)
continue;
- if (shouldInternalizeLinkedSymbols())
+ if (InternalizeCallback)
Internalize.insert(GV2->getName());
Add(*GV2);
}
@@ -571,7 +532,7 @@ bool ModuleLinker::run() {
}
}
- if (shouldInternalizeLinkedSymbols()) {
+ if (InternalizeCallback) {
for (GlobalValue *GV : ValuesToLink)
Internalize.insert(GV->getName());
}
@@ -583,8 +544,7 @@ bool ModuleLinker::run() {
[this](GlobalValue &GV, IRMover::ValueAdder Add) {
addLazyFor(GV, Add);
},
- /* LinkModuleInlineAsm */ !isPerformingImport(),
- /* IsPerformingImport */ isPerformingImport())) {
+ /* IsPerformingImport */ false)) {
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
DstM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, EIB.message()));
HasErrors = true;
@@ -593,19 +553,19 @@ bool ModuleLinker::run() {
if (HasErrors)
return true;
- for (auto &P : Internalize) {
- GlobalValue *GV = DstM.getNamedValue(P.first());
- GV->setLinkage(GlobalValue::InternalLinkage);
- }
+ if (InternalizeCallback)
+ InternalizeCallback(DstM, Internalize);
return false;
}
Linker::Linker(Module &M) : Mover(M) {}
-bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
- DenseSet<const GlobalValue *> *GlobalsToImport) {
- ModuleLinker ModLinker(Mover, std::move(Src), Flags, GlobalsToImport);
+bool Linker::linkInModule(
+ std::unique_ptr<Module> Src, unsigned Flags,
+ std::function<void(Module &, const StringSet<> &)> InternalizeCallback) {
+ ModuleLinker ModLinker(Mover, std::move(Src), Flags,
+ std::move(InternalizeCallback));
return ModLinker.run();
}
@@ -618,10 +578,11 @@ bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
/// true is returned and ErrorMsg (if not null) is set to indicate the problem.
/// Upon failure, the Dest module could be in a modified state, and shouldn't be
/// relied on to be consistent.
-bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src,
- unsigned Flags) {
+bool Linker::linkModules(
+ Module &Dest, std::unique_ptr<Module> Src, unsigned Flags,
+ std::function<void(Module &, const StringSet<> &)> InternalizeCallback) {
Linker L(Dest);
- return L.linkInModule(std::move(Src), Flags);
+ return L.linkInModule(std::move(Src), Flags, std::move(InternalizeCallback));
}
//===----------------------------------------------------------------------===//