2009-03-03 18:04:23 +08:00
|
|
|
//===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2004-11-13 04:37:43 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2004-11-13 04:37:43 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVM module linker.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-06 11:42:23 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2015-12-10 22:19:35 +08:00
|
|
|
#include "LinkDiagnosticInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm-c/Linker.h"
|
2012-02-11 19:38:06 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2015-12-10 22:19:35 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2014-10-25 12:06:10 +08:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2015-12-15 07:17:03 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2004-11-13 04:37:43 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
namespace {
|
2014-11-25 14:11:24 +08:00
|
|
|
|
|
|
|
/// This is an implementation class for the LinkModules function, which is the
|
|
|
|
/// entrypoint for this file.
|
|
|
|
class ModuleLinker {
|
2015-12-10 22:19:35 +08:00
|
|
|
IRMover &Mover;
|
2015-12-02 03:50:54 +08:00
|
|
|
Module &SrcM;
|
2014-11-25 14:11:24 +08:00
|
|
|
|
2015-12-03 06:59:04 +08:00
|
|
|
SetVector<GlobalValue *> ValuesToLink;
|
2015-12-10 22:19:35 +08:00
|
|
|
StringSet<> Internalize;
|
2014-10-28 07:02:10 +08:00
|
|
|
|
2015-04-22 12:11:00 +08:00
|
|
|
/// For symbol clashes, prefer those from Src.
|
2015-09-02 01:55:55 +08:00
|
|
|
unsigned Flags;
|
2015-04-22 12:11:00 +08:00
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
/// Function index passed into ModuleLinker for using in function
|
|
|
|
/// importing/exporting handling.
|
2015-11-23 09:59:16 +08:00
|
|
|
const FunctionInfoIndex *ImportIndex;
|
2015-11-03 08:14:15 +08:00
|
|
|
|
2015-12-22 01:33:24 +08:00
|
|
|
/// Functions to import from source module, all other functions are
|
2015-11-03 08:14:15 +08:00
|
|
|
/// imported as declarations instead of definitions.
|
2015-12-22 01:33:24 +08:00
|
|
|
DenseSet<const GlobalValue *> *FunctionsToImport;
|
2015-11-03 08:14:15 +08:00
|
|
|
|
|
|
|
/// Set to true if the given FunctionInfoIndex contains any functions
|
|
|
|
/// from this source module, in which case we must conservatively assume
|
|
|
|
/// that any of its functions may be imported into another module
|
|
|
|
/// as part of a different backend compilation process.
|
2015-12-02 07:06:26 +08:00
|
|
|
bool HasExportedFunctions = false;
|
2015-11-03 08:14:15 +08:00
|
|
|
|
2015-12-18 01:14:09 +08:00
|
|
|
/// Association between metadata value id and temporary metadata that
|
|
|
|
/// remains unmapped after function importing. Saved during function
|
|
|
|
/// importing and consumed during the metadata linking postpass.
|
|
|
|
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap;
|
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
/// Used as the callback for lazy linking.
|
|
|
|
/// The mover has just hit GV and we have to decide if it, and other members
|
|
|
|
/// of the same comdat, should be linked. Every member to be linked is passed
|
|
|
|
/// to Add.
|
|
|
|
void addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add);
|
2015-12-01 23:19:48 +08:00
|
|
|
|
2015-09-02 01:55:55 +08:00
|
|
|
bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
|
|
|
|
bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
|
|
|
|
bool shouldInternalizeLinkedSymbols() {
|
|
|
|
return Flags & Linker::InternalizeLinkedSymbols;
|
|
|
|
}
|
|
|
|
|
2014-11-25 14:11:24 +08:00
|
|
|
bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
|
|
|
|
const GlobalValue &Src);
|
2014-09-09 23:21:00 +08:00
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
/// Should we have mover and linker error diag info?
|
2014-11-25 14:11:24 +08:00
|
|
|
bool emitError(const Twine &Message) {
|
2015-12-15 07:17:03 +08:00
|
|
|
SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
|
2014-11-25 14:11:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-09 22:39:25 +08:00
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
bool getComdatLeader(Module &M, StringRef ComdatName,
|
2014-11-25 14:11:24 +08:00
|
|
|
const GlobalVariable *&GVar);
|
|
|
|
bool computeResultingSelectionKind(StringRef ComdatName,
|
|
|
|
Comdat::SelectionKind Src,
|
|
|
|
Comdat::SelectionKind Dst,
|
|
|
|
Comdat::SelectionKind &Result,
|
|
|
|
bool &LinkFromSrc);
|
|
|
|
std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
|
|
|
|
ComdatsChosen;
|
|
|
|
bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
|
|
|
|
bool &LinkFromSrc);
|
2015-11-11 05:09:06 +08:00
|
|
|
// Keep track of the global value members of each comdat in source.
|
|
|
|
DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers;
|
2014-11-25 14:11:24 +08:00
|
|
|
|
|
|
|
/// Given a global in the source module, return the global in the
|
|
|
|
/// destination module that is being linked to, if any.
|
|
|
|
GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
|
2015-12-10 22:19:35 +08:00
|
|
|
Module &DstM = Mover.getModule();
|
2014-11-25 14:11:24 +08:00
|
|
|
// If the source has no name it can't link. If it has local linkage,
|
|
|
|
// there is no name match-up going on.
|
2016-01-08 23:00:00 +08:00
|
|
|
if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(SrcGV->getLinkage()))
|
2014-11-25 14:11:24 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Otherwise see if we have a match in the destination module's symtab.
|
2016-01-08 23:00:00 +08:00
|
|
|
GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
|
2014-11-25 14:11:24 +08:00
|
|
|
if (!DGV)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If we found a global with the same name in the dest module, but it has
|
|
|
|
// internal linkage, we are really not doing any linkage here.
|
|
|
|
if (DGV->hasLocalLinkage())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Otherwise, we do in fact link to the destination global.
|
|
|
|
return DGV;
|
|
|
|
}
|
2014-05-09 22:39:25 +08:00
|
|
|
|
2015-12-01 23:19:48 +08:00
|
|
|
bool linkIfNeeded(GlobalValue &GV);
|
2015-11-03 08:14:15 +08:00
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
/// Helper method to check if we are importing from the current source
|
|
|
|
/// module.
|
|
|
|
bool isPerformingImport() const { return FunctionsToImport != 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, Module &SrcM, unsigned Flags,
|
|
|
|
const FunctionInfoIndex *Index = nullptr,
|
|
|
|
DenseSet<const GlobalValue *> *FunctionsToImport = nullptr,
|
|
|
|
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr)
|
|
|
|
: Mover(Mover), SrcM(SrcM), Flags(Flags), ImportIndex(Index),
|
|
|
|
FunctionsToImport(FunctionsToImport),
|
|
|
|
ValIDToTempMDMap(ValIDToTempMDMap) {
|
|
|
|
assert((ImportIndex || !FunctionsToImport) &&
|
|
|
|
"Expect a FunctionInfoIndex when importing");
|
|
|
|
// If we have a FunctionInfoIndex but no function to import,
|
|
|
|
// then this is the primary module being compiled in a ThinLTO
|
|
|
|
// backend compilation, and we need to see if it has functions that
|
|
|
|
// may be exported to another backend compilation.
|
|
|
|
if (ImportIndex && !FunctionsToImport)
|
|
|
|
HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool run();
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Class to handle necessary GlobalValue changes required by ThinLTO including
|
|
|
|
/// linkage changes and any necessary renaming.
|
|
|
|
class ThinLTOGlobalProcessing {
|
|
|
|
/// The Module which we are exporting or importing functions from.
|
|
|
|
Module &M;
|
|
|
|
|
|
|
|
/// Function index passed in for function importing/exporting handling.
|
|
|
|
const FunctionInfoIndex *ImportIndex;
|
|
|
|
|
|
|
|
/// Functions to import from this module, all other functions will be
|
|
|
|
/// imported as declarations instead of definitions.
|
|
|
|
DenseSet<const GlobalValue *> *FunctionsToImport;
|
|
|
|
|
|
|
|
/// Set to true if the given FunctionInfoIndex contains any functions
|
|
|
|
/// from this source module, in which case we must conservatively assume
|
|
|
|
/// that any of its functions may be imported into another module
|
|
|
|
/// as part of a different backend compilation process.
|
|
|
|
bool HasExportedFunctions = false;
|
|
|
|
|
|
|
|
/// Populated during ThinLTO global processing with locals promoted
|
|
|
|
/// to global scope in an exporting module, which now need to be linked
|
|
|
|
/// in if calling from the ModuleLinker.
|
|
|
|
SetVector<GlobalValue *> NewExportedValues;
|
|
|
|
|
|
|
|
/// Check if we should promote the given local value to global scope.
|
|
|
|
bool doPromoteLocalToGlobal(const GlobalValue *SGV);
|
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
/// Helper methods to check if we are importing from or potentially
|
|
|
|
/// exporting from the current source module.
|
2015-12-22 01:33:24 +08:00
|
|
|
bool isPerformingImport() const { return FunctionsToImport != nullptr; }
|
2015-12-18 00:34:53 +08:00
|
|
|
bool isModuleExporting() const { return HasExportedFunctions; }
|
2015-11-03 08:14:15 +08:00
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
/// Get the name for SGV that should be used in the linked destination
|
|
|
|
/// module. Specifically, this handles the case where we need to rename
|
|
|
|
/// a local that is being promoted to global scope.
|
|
|
|
std::string getName(const GlobalValue *SGV);
|
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
/// Process globals so that they can be used in ThinLTO. This includes
|
|
|
|
/// promoting local variables so that they can be reference externally by
|
|
|
|
/// thin lto imported globals and converting strong external globals to
|
|
|
|
/// available_externally.
|
|
|
|
void processGlobalsForThinLTO();
|
|
|
|
void processGlobalForThinLTO(GlobalValue &GV);
|
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
/// Get the new linkage for SGV that should be used in the linked destination
|
|
|
|
/// module. Specifically, for ThinLTO importing or exporting it may need
|
|
|
|
/// to be adjusted.
|
|
|
|
GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
|
|
|
|
|
2015-12-08 22:54:49 +08:00
|
|
|
public:
|
2016-01-08 23:00:00 +08:00
|
|
|
ThinLTOGlobalProcessing(
|
|
|
|
Module &M, const FunctionInfoIndex *Index,
|
|
|
|
DenseSet<const GlobalValue *> *FunctionsToImport = nullptr)
|
|
|
|
: M(M), ImportIndex(Index), FunctionsToImport(FunctionsToImport) {
|
2015-12-08 22:54:49 +08:00
|
|
|
// If we have a FunctionInfoIndex but no function to import,
|
|
|
|
// then this is the primary module being compiled in a ThinLTO
|
|
|
|
// backend compilation, and we need to see if it has functions that
|
|
|
|
// may be exported to another backend compilation.
|
2016-01-08 23:00:00 +08:00
|
|
|
if (!FunctionsToImport)
|
|
|
|
HasExportedFunctions = ImportIndex->hasExportedFunctions(M);
|
2015-12-08 22:54:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool run();
|
2016-01-08 23:00:00 +08:00
|
|
|
|
|
|
|
/// Access the promoted globals that are now exported and need to be linked.
|
|
|
|
SetVector<GlobalValue *> &getNewExportedValues() { return NewExportedValues; }
|
2014-11-25 14:11:24 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2012-02-28 12:01:21 +08:00
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
/// Checks if we should import SGV as a definition, otherwise import as a
|
|
|
|
/// declaration.
|
|
|
|
static bool
|
|
|
|
doImportAsDefinitionImpl(const GlobalValue *SGV,
|
|
|
|
DenseSet<const GlobalValue *> *FunctionsToImport) {
|
2015-11-11 02:20:11 +08:00
|
|
|
auto *GA = dyn_cast<GlobalAlias>(SGV);
|
|
|
|
if (GA) {
|
|
|
|
if (GA->hasWeakAnyLinkage())
|
|
|
|
return false;
|
2015-11-27 03:22:59 +08:00
|
|
|
const GlobalObject *GO = GA->getBaseObject();
|
|
|
|
if (!GO->hasLinkOnceODRLinkage())
|
|
|
|
return false;
|
2016-01-08 23:00:00 +08:00
|
|
|
return doImportAsDefinitionImpl(GO, FunctionsToImport);
|
2015-11-11 02:20:11 +08:00
|
|
|
}
|
2015-11-11 02:26:31 +08:00
|
|
|
// Always import GlobalVariable definitions, except for the special
|
|
|
|
// case of WeakAny which are imported as ExternalWeak declarations
|
|
|
|
// (see comments in ModuleLinker::getLinkage). The linkage changes
|
2015-11-03 08:14:15 +08:00
|
|
|
// described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
|
|
|
|
// global variables with external linkage are transformed to
|
2015-11-11 02:20:11 +08:00
|
|
|
// available_externally definitions, which are ultimately turned into
|
|
|
|
// declarations after the EliminateAvailableExternally pass).
|
2015-11-18 15:07:59 +08:00
|
|
|
if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() &&
|
2015-11-11 02:20:11 +08:00
|
|
|
!SGV->hasWeakAnyLinkage())
|
2015-11-03 08:14:15 +08:00
|
|
|
return true;
|
|
|
|
// Only import the function requested for importing.
|
|
|
|
auto *SF = dyn_cast<Function>(SGV);
|
2015-12-22 01:33:24 +08:00
|
|
|
if (SF && FunctionsToImport->count(SF))
|
2015-11-03 08:14:15 +08:00
|
|
|
return true;
|
|
|
|
// Otherwise no.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
bool ThinLTOGlobalProcessing::doImportAsDefinition(const GlobalValue *SGV) {
|
|
|
|
if (!isPerformingImport())
|
|
|
|
return false;
|
|
|
|
return doImportAsDefinitionImpl(SGV, FunctionsToImport);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
|
|
|
|
if (!isPerformingImport())
|
|
|
|
return false;
|
|
|
|
return doImportAsDefinitionImpl(SGV, FunctionsToImport);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThinLTOGlobalProcessing::doPromoteLocalToGlobal(const GlobalValue *SGV) {
|
2015-11-03 08:14:15 +08:00
|
|
|
assert(SGV->hasLocalLinkage());
|
|
|
|
// Both the imported references and the original local variable must
|
|
|
|
// be promoted.
|
|
|
|
if (!isPerformingImport() && !isModuleExporting())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Local const variables never need to be promoted unless they are address
|
|
|
|
// taken. The imported uses can simply use the clone created in this module.
|
|
|
|
// For now we are conservative in determining which variables are not
|
|
|
|
// address taken by checking the unnamed addr flag. To be more aggressive,
|
|
|
|
// the address taken information must be checked earlier during parsing
|
|
|
|
// of the module and recorded in the function index for use when importing
|
|
|
|
// from that module.
|
|
|
|
auto *GVar = dyn_cast<GlobalVariable>(SGV);
|
|
|
|
if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Eventually we only need to promote functions in the exporting module that
|
|
|
|
// are referenced by a potentially exported function (i.e. one that is in the
|
|
|
|
// function index).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
std::string ThinLTOGlobalProcessing::getName(const GlobalValue *SGV) {
|
2015-11-03 08:14:15 +08:00
|
|
|
// For locals that must be promoted to global scope, ensure that
|
|
|
|
// the promoted name uniquely identifies the copy in the original module,
|
|
|
|
// using the ID assigned during combined index creation. When importing,
|
|
|
|
// we rename all locals (not just those that are promoted) in order to
|
|
|
|
// avoid naming conflicts between locals imported from different modules.
|
|
|
|
if (SGV->hasLocalLinkage() &&
|
|
|
|
(doPromoteLocalToGlobal(SGV) || isPerformingImport()))
|
|
|
|
return FunctionInfoIndex::getGlobalNameForLocal(
|
|
|
|
SGV->getName(),
|
|
|
|
ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
|
|
|
|
return SGV->getName();
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
GlobalValue::LinkageTypes
|
|
|
|
ThinLTOGlobalProcessing::getLinkage(const GlobalValue *SGV) {
|
2015-11-03 08:14:15 +08:00
|
|
|
// Any local variable that is referenced by an exported function needs
|
|
|
|
// to be promoted to global scope. Since we don't currently know which
|
|
|
|
// functions reference which local variables/functions, we must treat
|
|
|
|
// all as potentially exported if this module is exporting anything.
|
|
|
|
if (isModuleExporting()) {
|
|
|
|
if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
return SGV->getLinkage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if we aren't importing, no linkage change is needed.
|
|
|
|
if (!isPerformingImport())
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
switch (SGV->getLinkage()) {
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
// External defnitions are converted to available_externally
|
|
|
|
// definitions upon import, so that they are available for inlining
|
|
|
|
// and/or optimization, but are turned into declarations later
|
|
|
|
// during the EliminateAvailableExternally pass.
|
2015-11-11 02:20:11 +08:00
|
|
|
if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
|
2015-11-03 08:14:15 +08:00
|
|
|
return GlobalValue::AvailableExternallyLinkage;
|
|
|
|
// An imported external declaration stays external.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::AvailableExternallyLinkage:
|
|
|
|
// An imported available_externally definition converts
|
|
|
|
// to external if imported as a declaration.
|
|
|
|
if (!doImportAsDefinition(SGV))
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
// An imported available_externally declaration stays that way.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
|
|
|
case GlobalValue::LinkOnceODRLinkage:
|
|
|
|
// These both stay the same when importing the definition.
|
|
|
|
// The ThinLTO pass will eventually force-import their definitions.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::WeakAnyLinkage:
|
|
|
|
// Can't import weak_any definitions correctly, or we might change the
|
|
|
|
// program semantics, since the linker will pick the first weak_any
|
|
|
|
// definition and importing would change the order they are seen by the
|
|
|
|
// linker. The module linking caller needs to enforce this.
|
|
|
|
assert(!doImportAsDefinition(SGV));
|
|
|
|
// If imported as a declaration, it becomes external_weak.
|
|
|
|
return GlobalValue::ExternalWeakLinkage;
|
|
|
|
|
|
|
|
case GlobalValue::WeakODRLinkage:
|
|
|
|
// For weak_odr linkage, there is a guarantee that all copies will be
|
|
|
|
// equivalent, so the issue described above for weak_any does not exist,
|
|
|
|
// and the definition can be imported. It can be treated similarly
|
|
|
|
// to an imported externally visible global value.
|
2015-11-11 02:20:11 +08:00
|
|
|
if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
|
2015-11-03 08:14:15 +08:00
|
|
|
return GlobalValue::AvailableExternallyLinkage;
|
|
|
|
else
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// It would be incorrect to import an appending linkage variable,
|
|
|
|
// since it would cause global constructors/destructors to be
|
|
|
|
// executed multiple times. This should have already been handled
|
2015-12-04 02:20:05 +08:00
|
|
|
// by linkIfNeeded, and we will assert in shouldLinkFromSource
|
2016-01-08 23:00:00 +08:00
|
|
|
// if we try to import, so we simply return AppendingLinkage.
|
2015-12-04 02:20:05 +08:00
|
|
|
return GlobalValue::AppendingLinkage;
|
2015-11-03 08:14:15 +08:00
|
|
|
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
case GlobalValue::PrivateLinkage:
|
|
|
|
// If we are promoting the local to global scope, it is handled
|
|
|
|
// similarly to a normal externally visible global.
|
|
|
|
if (doPromoteLocalToGlobal(SGV)) {
|
2015-11-11 02:20:11 +08:00
|
|
|
if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
|
2015-11-03 08:14:15 +08:00
|
|
|
return GlobalValue::AvailableExternallyLinkage;
|
|
|
|
else
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
}
|
|
|
|
// A non-promoted imported local definition stays local.
|
|
|
|
// The ThinLTO pass will eventually force-import their definitions.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::ExternalWeakLinkage:
|
|
|
|
// External weak doesn't apply to definitions, must be a declaration.
|
|
|
|
assert(!doImportAsDefinition(SGV));
|
|
|
|
// Linkage stays external_weak.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::CommonLinkage:
|
|
|
|
// Linkage stays common on definitions.
|
|
|
|
// The ThinLTO pass will eventually force-import their definitions.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("unknown linkage type");
|
|
|
|
}
|
|
|
|
|
2015-11-29 22:33:06 +08:00
|
|
|
static GlobalValue::VisibilityTypes
|
|
|
|
getMinVisibility(GlobalValue::VisibilityTypes A,
|
|
|
|
GlobalValue::VisibilityTypes B) {
|
|
|
|
if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility)
|
|
|
|
return GlobalValue::HiddenVisibility;
|
|
|
|
if (A == GlobalValue::ProtectedVisibility ||
|
|
|
|
B == GlobalValue::ProtectedVisibility)
|
|
|
|
return GlobalValue::ProtectedVisibility;
|
|
|
|
return GlobalValue::DefaultVisibility;
|
|
|
|
}
|
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
|
2014-06-28 02:19:56 +08:00
|
|
|
const GlobalVariable *&GVar) {
|
2015-12-02 03:50:54 +08:00
|
|
|
const GlobalValue *GVal = M.getNamedValue(ComdatName);
|
2014-06-28 02:19:56 +08:00
|
|
|
if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
|
|
|
|
GVal = GA->getBaseObject();
|
|
|
|
if (!GVal)
|
|
|
|
// We cannot resolve the size of the aliasee yet.
|
|
|
|
return emitError("Linking COMDATs named '" + ComdatName +
|
|
|
|
"': COMDAT key involves incomputable alias size.");
|
|
|
|
}
|
|
|
|
|
|
|
|
GVar = dyn_cast_or_null<GlobalVariable>(GVal);
|
|
|
|
if (!GVar)
|
|
|
|
return emitError(
|
|
|
|
"Linking COMDATs named '" + ComdatName +
|
|
|
|
"': GlobalVariable required for data dependent selection!");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
|
|
|
|
Comdat::SelectionKind Src,
|
|
|
|
Comdat::SelectionKind Dst,
|
|
|
|
Comdat::SelectionKind &Result,
|
|
|
|
bool &LinkFromSrc) {
|
2015-12-10 22:19:35 +08:00
|
|
|
Module &DstM = Mover.getModule();
|
2014-06-28 02:19:56 +08:00
|
|
|
// The ability to mix Comdat::SelectionKind::Any with
|
|
|
|
// Comdat::SelectionKind::Largest is a behavior that comes from COFF.
|
|
|
|
bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
|
|
|
|
Dst == Comdat::SelectionKind::Largest;
|
|
|
|
bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
|
|
|
|
Src == Comdat::SelectionKind::Largest;
|
|
|
|
if (DstAnyOrLargest && SrcAnyOrLargest) {
|
|
|
|
if (Dst == Comdat::SelectionKind::Largest ||
|
|
|
|
Src == Comdat::SelectionKind::Largest)
|
|
|
|
Result = Comdat::SelectionKind::Largest;
|
|
|
|
else
|
|
|
|
Result = Comdat::SelectionKind::Any;
|
|
|
|
} else if (Src == Dst) {
|
|
|
|
Result = Dst;
|
|
|
|
} else {
|
|
|
|
return emitError("Linking COMDATs named '" + ComdatName +
|
|
|
|
"': invalid selection kinds!");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Result) {
|
|
|
|
case Comdat::SelectionKind::Any:
|
|
|
|
// Go with Dst.
|
|
|
|
LinkFromSrc = false;
|
|
|
|
break;
|
|
|
|
case Comdat::SelectionKind::NoDuplicates:
|
|
|
|
return emitError("Linking COMDATs named '" + ComdatName +
|
|
|
|
"': noduplicates has been violated!");
|
|
|
|
case Comdat::SelectionKind::ExactMatch:
|
|
|
|
case Comdat::SelectionKind::Largest:
|
|
|
|
case Comdat::SelectionKind::SameSize: {
|
|
|
|
const GlobalVariable *DstGV;
|
|
|
|
const GlobalVariable *SrcGV;
|
|
|
|
if (getComdatLeader(DstM, ComdatName, DstGV) ||
|
|
|
|
getComdatLeader(SrcM, ComdatName, SrcGV))
|
|
|
|
return true;
|
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
const DataLayout &DstDL = DstM.getDataLayout();
|
|
|
|
const DataLayout &SrcDL = SrcM.getDataLayout();
|
2016-01-17 04:30:46 +08:00
|
|
|
uint64_t DstSize = DstDL.getTypeAllocSize(DstGV->getValueType());
|
|
|
|
uint64_t SrcSize = SrcDL.getTypeAllocSize(SrcGV->getValueType());
|
2014-06-28 02:19:56 +08:00
|
|
|
if (Result == Comdat::SelectionKind::ExactMatch) {
|
|
|
|
if (SrcGV->getInitializer() != DstGV->getInitializer())
|
|
|
|
return emitError("Linking COMDATs named '" + ComdatName +
|
|
|
|
"': ExactMatch violated!");
|
|
|
|
LinkFromSrc = false;
|
|
|
|
} else if (Result == Comdat::SelectionKind::Largest) {
|
|
|
|
LinkFromSrc = SrcSize > DstSize;
|
|
|
|
} else if (Result == Comdat::SelectionKind::SameSize) {
|
|
|
|
if (SrcSize != DstSize)
|
|
|
|
return emitError("Linking COMDATs named '" + ComdatName +
|
|
|
|
"': SameSize violated!");
|
|
|
|
LinkFromSrc = false;
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("unknown selection kind");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleLinker::getComdatResult(const Comdat *SrcC,
|
|
|
|
Comdat::SelectionKind &Result,
|
|
|
|
bool &LinkFromSrc) {
|
2015-12-10 22:19:35 +08:00
|
|
|
Module &DstM = Mover.getModule();
|
2014-08-12 01:07:34 +08:00
|
|
|
Comdat::SelectionKind SSK = SrcC->getSelectionKind();
|
2014-06-28 02:19:56 +08:00
|
|
|
StringRef ComdatName = SrcC->getName();
|
2015-12-02 03:50:54 +08:00
|
|
|
Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
|
2014-06-28 02:19:56 +08:00
|
|
|
Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
|
2014-08-12 00:55:42 +08:00
|
|
|
|
2014-08-12 01:07:34 +08:00
|
|
|
if (DstCI == ComdatSymTab.end()) {
|
|
|
|
// Use the comdat if it is only available in one of the modules.
|
|
|
|
LinkFromSrc = true;
|
|
|
|
Result = SSK;
|
2014-08-12 00:55:42 +08:00
|
|
|
return false;
|
2014-08-12 01:07:34 +08:00
|
|
|
}
|
2014-08-12 00:55:42 +08:00
|
|
|
|
|
|
|
const Comdat *DstC = &DstCI->second;
|
|
|
|
Comdat::SelectionKind DSK = DstC->getSelectionKind();
|
|
|
|
return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
|
|
|
|
LinkFromSrc);
|
2014-06-28 02:19:56 +08:00
|
|
|
}
|
2013-05-28 23:17:05 +08:00
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
|
|
|
|
const GlobalValue &Dest,
|
2014-09-09 23:21:00 +08:00
|
|
|
const GlobalValue &Src) {
|
2015-12-18 01:14:09 +08:00
|
|
|
|
2015-04-22 12:11:00 +08:00
|
|
|
// Should we unconditionally use the Src?
|
2015-09-02 01:55:55 +08:00
|
|
|
if (shouldOverrideFromSrc()) {
|
2015-04-22 12:11:00 +08:00
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-02 21:28:57 +08:00
|
|
|
// We always have to add Src if it has appending linkage.
|
|
|
|
if (Src.hasAppendingLinkage()) {
|
2015-12-04 02:20:05 +08:00
|
|
|
// Should have prevented importing for appending linkage in linkIfNeeded.
|
2015-11-03 08:14:15 +08:00
|
|
|
assert(!isPerformingImport());
|
2014-11-02 21:28:57 +08:00
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-25 02:13:04 +08:00
|
|
|
bool SrcIsDeclaration = Src.isDeclarationForLinker();
|
|
|
|
bool DestIsDeclaration = Dest.isDeclarationForLinker();
|
2014-09-09 23:21:00 +08:00
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
if (isPerformingImport()) {
|
|
|
|
if (isa<Function>(&Src)) {
|
2015-12-22 01:33:24 +08:00
|
|
|
// For functions, LinkFromSrc iff this is a function requested
|
2015-11-03 08:14:15 +08:00
|
|
|
// for importing. For variables, decide below normally.
|
2015-12-22 01:33:24 +08:00
|
|
|
LinkFromSrc = FunctionsToImport->count(&Src);
|
2015-11-03 08:14:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this is an alias with an already existing definition
|
|
|
|
// in Dest, which must have come from a prior importing pass from
|
|
|
|
// the same Src module. Unlike imported function and variable
|
|
|
|
// definitions, which are imported as available_externally and are
|
|
|
|
// not definitions for the linker, that is not a valid linkage for
|
|
|
|
// imported aliases which must be definitions. Simply use the existing
|
|
|
|
// Dest copy.
|
|
|
|
if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
|
|
|
|
assert(isa<GlobalAlias>(&Dest));
|
|
|
|
LinkFromSrc = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 23:21:00 +08:00
|
|
|
if (SrcIsDeclaration) {
|
|
|
|
// If Src is external or if both Src & Dest are external.. Just link the
|
|
|
|
// external globals, we aren't adding anything.
|
2014-10-25 12:06:10 +08:00
|
|
|
if (Src.hasDLLImportStorageClass()) {
|
2014-09-09 23:21:00 +08:00
|
|
|
// If one of GVs is marked as DLLImport, result should be dllimport'ed.
|
2014-10-25 12:06:10 +08:00
|
|
|
LinkFromSrc = DestIsDeclaration;
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-09 23:21:00 +08:00
|
|
|
// If the Dest is weak, use the source linkage.
|
2015-12-10 06:44:00 +08:00
|
|
|
if (Dest.hasExternalWeakLinkage()) {
|
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Link an available_externally over a declaration.
|
|
|
|
LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration();
|
2014-10-25 12:06:10 +08:00
|
|
|
return false;
|
2014-09-09 23:21:00 +08:00
|
|
|
}
|
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
if (DestIsDeclaration) {
|
2014-09-09 23:21:00 +08:00
|
|
|
// If Dest is external but Src is not:
|
2014-10-25 12:06:10 +08:00
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-09 23:21:00 +08:00
|
|
|
|
2014-09-09 23:59:12 +08:00
|
|
|
if (Src.hasCommonLinkage()) {
|
2014-10-25 12:06:10 +08:00
|
|
|
if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
|
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-09 23:59:12 +08:00
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
if (!Dest.hasCommonLinkage()) {
|
|
|
|
LinkFromSrc = false;
|
2014-09-09 23:59:12 +08:00
|
|
|
return false;
|
2014-10-25 12:06:10 +08:00
|
|
|
}
|
2014-09-09 23:59:12 +08:00
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = Dest.getParent()->getDataLayout();
|
2016-01-17 04:30:46 +08:00
|
|
|
uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType());
|
|
|
|
uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType());
|
2014-10-25 12:06:10 +08:00
|
|
|
LinkFromSrc = SrcSize > DestSize;
|
|
|
|
return false;
|
2014-09-09 23:59:12 +08:00
|
|
|
}
|
|
|
|
|
2014-09-09 23:21:00 +08:00
|
|
|
if (Src.isWeakForLinker()) {
|
|
|
|
assert(!Dest.hasExternalWeakLinkage());
|
|
|
|
assert(!Dest.hasAvailableExternallyLinkage());
|
2014-09-09 23:59:12 +08:00
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
|
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-09 23:21:00 +08:00
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
LinkFromSrc = false;
|
2014-09-09 23:59:12 +08:00
|
|
|
return false;
|
2014-09-09 23:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Dest.isWeakForLinker()) {
|
|
|
|
assert(Src.hasExternalLinkage());
|
2014-10-25 12:06:10 +08:00
|
|
|
LinkFromSrc = true;
|
|
|
|
return false;
|
2014-09-09 23:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(!Src.hasExternalWeakLinkage());
|
|
|
|
assert(!Dest.hasExternalWeakLinkage());
|
|
|
|
assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
|
|
|
|
"Unexpected linkage type!");
|
|
|
|
return emitError("Linking globals named '" + Src.getName() +
|
|
|
|
"': symbol multiply defined!");
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:19:48 +08:00
|
|
|
bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
|
|
|
|
GlobalValue *DGV = getLinkedToGlobal(&GV);
|
|
|
|
|
|
|
|
if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
|
|
|
|
return false;
|
|
|
|
|
2015-12-03 06:59:04 +08:00
|
|
|
if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
|
|
|
|
auto *DGVar = dyn_cast<GlobalVariable>(DGV);
|
|
|
|
auto *SGVar = dyn_cast<GlobalVariable>(&GV);
|
|
|
|
if (DGVar && SGVar) {
|
|
|
|
if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
|
|
|
|
(!DGVar->isConstant() || !SGVar->isConstant())) {
|
|
|
|
DGVar->setConstant(false);
|
|
|
|
SGVar->setConstant(false);
|
|
|
|
}
|
|
|
|
if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
|
|
|
|
unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment());
|
|
|
|
SGVar->setAlignment(Align);
|
|
|
|
DGVar->setAlignment(Align);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:19:48 +08:00
|
|
|
GlobalValue::VisibilityTypes Visibility =
|
|
|
|
getMinVisibility(DGV->getVisibility(), GV.getVisibility());
|
|
|
|
DGV->setVisibility(Visibility);
|
|
|
|
GV.setVisibility(Visibility);
|
2015-12-03 06:59:04 +08:00
|
|
|
|
|
|
|
bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr();
|
|
|
|
DGV->setUnnamedAddr(HasUnnamedAddr);
|
|
|
|
GV.setUnnamedAddr(HasUnnamedAddr);
|
2015-12-01 23:19:48 +08:00
|
|
|
}
|
|
|
|
|
2015-12-03 06:59:04 +08:00
|
|
|
// 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() && !doImportAsDefinition(&GV))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!DGV && !shouldOverrideFromSrc() &&
|
|
|
|
(GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
|
|
|
|
GV.hasAvailableExternallyLinkage()))
|
|
|
|
return false;
|
2015-12-08 00:31:41 +08:00
|
|
|
|
|
|
|
if (GV.isDeclaration())
|
|
|
|
return false;
|
2015-12-03 06:59:04 +08:00
|
|
|
|
2015-12-01 23:19:48 +08:00
|
|
|
if (const Comdat *SC = GV.getComdat()) {
|
|
|
|
bool LinkFromSrc;
|
|
|
|
Comdat::SelectionKind SK;
|
|
|
|
std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
|
2015-12-03 06:59:04 +08:00
|
|
|
if (LinkFromSrc)
|
|
|
|
ValuesToLink.insert(&GV);
|
2015-12-01 23:19:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-03 06:59:04 +08:00
|
|
|
|
|
|
|
bool LinkFromSrc = true;
|
|
|
|
if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
|
|
|
|
return true;
|
|
|
|
if (LinkFromSrc)
|
|
|
|
ValuesToLink.insert(&GV);
|
|
|
|
return false;
|
2015-12-01 23:19:48 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) {
|
|
|
|
// Add these to the internalize list
|
|
|
|
if (!GV.hasLinkOnceLinkage())
|
|
|
|
return;
|
2015-02-13 08:40:41 +08:00
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
if (shouldInternalizeLinkedSymbols())
|
|
|
|
Internalize.insert(GV.getName());
|
|
|
|
Add(GV);
|
2015-02-13 08:40:41 +08:00
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
const Comdat *SC = GV.getComdat();
|
|
|
|
if (!SC)
|
|
|
|
return;
|
|
|
|
for (GlobalValue *GV2 : ComdatMembers[SC]) {
|
|
|
|
if (!GV2->hasLocalLinkage() && shouldInternalizeLinkedSymbols())
|
|
|
|
Internalize.insert(GV2->getName());
|
|
|
|
Add(*GV2);
|
|
|
|
}
|
|
|
|
}
|
2005-04-22 06:55:34 +08:00
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
void ThinLTOGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
|
2015-12-10 22:19:35 +08:00
|
|
|
if (GV.hasLocalLinkage() &&
|
|
|
|
(doPromoteLocalToGlobal(&GV) || isPerformingImport())) {
|
|
|
|
GV.setName(getName(&GV));
|
|
|
|
GV.setLinkage(getLinkage(&GV));
|
|
|
|
if (!GV.hasLocalLinkage())
|
|
|
|
GV.setVisibility(GlobalValue::HiddenVisibility);
|
|
|
|
if (isModuleExporting())
|
2016-01-08 23:00:00 +08:00
|
|
|
NewExportedValues.insert(&GV);
|
2016-02-09 02:47:20 +08:00
|
|
|
} else
|
|
|
|
GV.setLinkage(getLinkage(&GV));
|
|
|
|
|
|
|
|
// Remove functions imported as available externally defs from comdats,
|
|
|
|
// as this is a declaration for the linker, and will be dropped eventually.
|
|
|
|
// It is illegal for comdats to contain declarations.
|
|
|
|
auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
|
|
|
|
if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
|
|
|
|
// The IRMover should not have placed any imported declarations in
|
|
|
|
// a comdat, so the only declaration that should be in a comdat
|
|
|
|
// at this point would be a definition imported as available_externally.
|
|
|
|
assert(GO->hasAvailableExternallyLinkage() &&
|
|
|
|
"Expected comdat on definition (possibly available external)");
|
|
|
|
GO->setComdat(nullptr);
|
2006-01-24 07:08:37 +08:00
|
|
|
}
|
2015-12-10 22:19:35 +08:00
|
|
|
}
|
2009-03-03 15:22:23 +08:00
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
void ThinLTOGlobalProcessing::processGlobalsForThinLTO() {
|
|
|
|
for (GlobalVariable &GV : M.globals())
|
2015-12-10 22:19:35 +08:00
|
|
|
processGlobalForThinLTO(GV);
|
2016-01-08 23:00:00 +08:00
|
|
|
for (Function &SF : M)
|
2015-12-10 22:19:35 +08:00
|
|
|
processGlobalForThinLTO(SF);
|
2016-01-08 23:00:00 +08:00
|
|
|
for (GlobalAlias &GA : M.aliases())
|
2015-12-10 22:19:35 +08:00
|
|
|
processGlobalForThinLTO(GA);
|
|
|
|
}
|
2004-11-13 04:37:43 +08:00
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
bool ThinLTOGlobalProcessing::run() {
|
|
|
|
processGlobalsForThinLTO();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
bool ModuleLinker::run() {
|
2015-12-02 03:50:54 +08:00
|
|
|
for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
|
2014-06-28 02:19:56 +08:00
|
|
|
const Comdat &C = SMEC.getValue();
|
|
|
|
if (ComdatsChosen.count(&C))
|
|
|
|
continue;
|
|
|
|
Comdat::SelectionKind SK;
|
|
|
|
bool LinkFromSrc;
|
|
|
|
if (getComdatResult(&C, SK, LinkFromSrc))
|
|
|
|
return true;
|
|
|
|
ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
|
|
|
|
}
|
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
for (GlobalVariable &GV : SrcM.globals())
|
2015-12-01 23:19:48 +08:00
|
|
|
if (const Comdat *SC = GV.getComdat())
|
|
|
|
ComdatMembers[SC].push_back(&GV);
|
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
for (Function &SF : SrcM)
|
2015-12-01 23:19:48 +08:00
|
|
|
if (const Comdat *SC = SF.getComdat())
|
|
|
|
ComdatMembers[SC].push_back(&SF);
|
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
for (GlobalAlias &GA : SrcM.aliases())
|
2015-12-01 23:19:48 +08:00
|
|
|
if (const Comdat *SC = GA.getComdat())
|
|
|
|
ComdatMembers[SC].push_back(&GA);
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
// Insert all of the globals in src into the DstM module... without linking
|
2004-11-13 04:37:43 +08:00
|
|
|
// initializers (which could refer to functions not yet mapped over).
|
2015-12-02 03:50:54 +08:00
|
|
|
for (GlobalVariable &GV : SrcM.globals())
|
2015-12-01 23:19:48 +08:00
|
|
|
if (linkIfNeeded(GV))
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
return true;
|
2004-11-13 04:37:43 +08:00
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
for (Function &SF : SrcM)
|
2015-12-01 23:19:48 +08:00
|
|
|
if (linkIfNeeded(SF))
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
return true;
|
2004-11-13 04:37:43 +08:00
|
|
|
|
2015-12-02 03:50:54 +08:00
|
|
|
for (GlobalAlias &GA : SrcM.aliases())
|
2015-12-01 23:19:48 +08:00
|
|
|
if (linkIfNeeded(GA))
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
return true;
|
2004-11-13 04:37:43 +08:00
|
|
|
|
2016-01-08 23:00:00 +08:00
|
|
|
if (ImportIndex) {
|
|
|
|
ThinLTOGlobalProcessing ThinLTOProcessing(SrcM, ImportIndex,
|
|
|
|
FunctionsToImport);
|
|
|
|
if (ThinLTOProcessing.run())
|
|
|
|
return true;
|
|
|
|
for (auto *GV : ThinLTOProcessing.getNewExportedValues())
|
|
|
|
ValuesToLink.insert(GV);
|
|
|
|
}
|
2014-12-09 02:05:48 +08:00
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
for (unsigned I = 0; I < ValuesToLink.size(); ++I) {
|
|
|
|
GlobalValue *GV = ValuesToLink[I];
|
|
|
|
const Comdat *SC = GV->getComdat();
|
|
|
|
if (!SC)
|
|
|
|
continue;
|
|
|
|
for (GlobalValue *GV2 : ComdatMembers[SC])
|
|
|
|
ValuesToLink.insert(GV2);
|
|
|
|
}
|
Restore "Move metadata linking after lazy global materialization/linking."
Summary:
This reverts commit r251965.
Restore "Move metadata linking after lazy global materialization/linking."
This restores commit r251926, with fixes for the LTO bootstrapping bot
failure.
The bot failure was caused by references from debug metadata to
otherwise unreferenced globals. Previously, this caused the lazy linking
to link in their defs, which is unnecessary. With this patch, because
lazy linking is complete when we encounter the metadata reference, the
materializer created a declaration. For definitions such as aliases and
comdats, it is illegal to have a declaration. Furthermore, metadata
linking should not change code generation. Therefore, when linking of
global value bodies is complete, the materializer will simply return
nullptr as the new reference for the linked metadata.
This change required fixing a different test to ensure there was a
real reference to a linkonce global that was only being reference from
metadata.
Note that the new changes to the only-needed-named-metadata.ll test
illustrate an issue with llvm-link -only-needed handling of comdat
groups, whereby it may result in an incomplete comdat group. I note this
in the test comments, but the issue is orthogonal to this patch (it can
be reproduced without any metadata at head).
Reviewers: dexonsmith, rafael, tra
Subscribers: tobiasvk, joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D14447
llvm-svn: 252320
2015-11-07 01:50:53 +08:00
|
|
|
|
2015-12-10 22:19:35 +08:00
|
|
|
if (shouldInternalizeLinkedSymbols()) {
|
|
|
|
for (GlobalValue *GV : ValuesToLink)
|
|
|
|
Internalize.insert(GV->getName());
|
|
|
|
}
|
2015-11-07 01:50:48 +08:00
|
|
|
|
2015-12-11 00:35:06 +08:00
|
|
|
if (Mover.move(SrcM, ValuesToLink.getArrayRef(),
|
2015-12-10 22:19:35 +08:00
|
|
|
[this](GlobalValue &GV, IRMover::ValueAdder Add) {
|
|
|
|
addLazyFor(GV, Add);
|
2015-12-18 01:14:09 +08:00
|
|
|
},
|
|
|
|
ValIDToTempMDMap, false))
|
2015-11-07 01:50:48 +08:00
|
|
|
return true;
|
2015-12-10 22:19:35 +08:00
|
|
|
Module &DstM = Mover.getModule();
|
|
|
|
for (auto &P : Internalize) {
|
|
|
|
GlobalValue *GV = DstM.getNamedValue(P.first());
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
}
|
2015-11-07 01:50:48 +08:00
|
|
|
|
2004-11-13 04:37:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-15 07:17:03 +08:00
|
|
|
Linker::Linker(Module &M) : Mover(M) {}
|
2013-05-04 11:48:37 +08:00
|
|
|
|
2015-12-17 07:16:33 +08:00
|
|
|
bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
|
2015-11-23 09:59:16 +08:00
|
|
|
const FunctionInfoIndex *Index,
|
2015-12-18 01:14:09 +08:00
|
|
|
DenseSet<const GlobalValue *> *FunctionsToImport,
|
|
|
|
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
|
2015-12-19 03:28:59 +08:00
|
|
|
ModuleLinker ModLinker(Mover, *Src, Flags, Index, FunctionsToImport,
|
2015-12-18 01:14:09 +08:00
|
|
|
ValIDToTempMDMap);
|
2015-12-19 03:28:59 +08:00
|
|
|
return ModLinker.run();
|
2015-12-17 07:16:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Linker::linkInModuleForCAPI(Module &Src) {
|
2015-12-19 03:28:59 +08:00
|
|
|
ModuleLinker ModLinker(Mover, Src, 0, nullptr, nullptr);
|
|
|
|
return ModLinker.run();
|
2013-05-04 11:48:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-18 01:14:09 +08:00
|
|
|
bool Linker::linkInMetadata(Module &Src,
|
|
|
|
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
|
|
|
|
SetVector<GlobalValue *> ValuesToLink;
|
|
|
|
if (Mover.move(
|
|
|
|
Src, ValuesToLink.getArrayRef(),
|
|
|
|
[this](GlobalValue &GV, IRMover::ValueAdder Add) { assert(false); },
|
|
|
|
ValIDToTempMDMap, true))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LinkModules entrypoint.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-10-27 10:35:46 +08:00
|
|
|
/// This function links two modules together, with the resulting Dest module
|
|
|
|
/// modified to be the composite of the two input modules. If an error occurs,
|
|
|
|
/// 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.
|
2015-12-17 07:16:33 +08:00
|
|
|
bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src,
|
|
|
|
unsigned Flags) {
|
2015-12-15 07:17:03 +08:00
|
|
|
Linker L(Dest);
|
2015-12-17 07:16:33 +08:00
|
|
|
return L.linkInModule(std::move(Src), Flags);
|
2014-10-28 07:02:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 01:06:29 +08:00
|
|
|
bool llvm::renameModuleForThinLTO(Module &M, const FunctionInfoIndex *Index) {
|
|
|
|
ThinLTOGlobalProcessing ThinLTOProcessing(M, Index);
|
|
|
|
return ThinLTOProcessing.run();
|
2015-12-05 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2012-05-09 16:55:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-12-15 07:17:03 +08:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
|
|
|
|
auto *Message = reinterpret_cast<std::string *>(C);
|
|
|
|
raw_string_ostream Stream(*Message);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
DI.print(DP);
|
|
|
|
}
|
|
|
|
|
2012-05-09 16:55:40 +08:00
|
|
|
LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
|
2015-03-03 02:59:38 +08:00
|
|
|
LLVMLinkerMode Unused, char **OutMessages) {
|
2014-10-25 12:31:08 +08:00
|
|
|
Module *D = unwrap(Dest);
|
2015-12-15 07:17:03 +08:00
|
|
|
LLVMContext &Ctx = D->getContext();
|
|
|
|
|
|
|
|
LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
|
|
|
|
Ctx.getDiagnosticHandler();
|
|
|
|
void *OldDiagnosticContext = Ctx.getDiagnosticContext();
|
2014-10-25 12:31:08 +08:00
|
|
|
std::string Message;
|
2015-12-15 07:17:03 +08:00
|
|
|
Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
|
|
|
|
|
2015-12-17 07:16:33 +08:00
|
|
|
Linker L(*D);
|
|
|
|
Module *M = unwrap(Src);
|
|
|
|
LLVMBool Result = L.linkInModuleForCAPI(*M);
|
2014-10-28 07:02:10 +08:00
|
|
|
|
2015-12-15 07:17:03 +08:00
|
|
|
Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
|
2014-10-25 12:31:08 +08:00
|
|
|
|
2015-12-15 07:17:03 +08:00
|
|
|
if (OutMessages && Result)
|
2014-10-25 12:31:08 +08:00
|
|
|
*OutMessages = strdup(Message.c_str());
|
2012-05-09 16:55:40 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2015-12-17 07:16:33 +08:00
|
|
|
|
|
|
|
LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src) {
|
|
|
|
Module *D = unwrap(Dest);
|
|
|
|
std::unique_ptr<Module> M(unwrap(Src));
|
|
|
|
return Linker::linkModules(*D, std::move(M));
|
|
|
|
}
|