Linker: Add flag to override linkage rules

Add a flag to lib/Linker (and `llvm-link`) to override linkage rules.
When set, the functions in the source module *always* replace those in
the destination module.

The `llvm-link` option is `-override=abc.ll`.  All the "regular" modules
are loaded and linked first, followed by the `-override` modules.  This
is useful for debugging workflows where some subset of the module (e.g.,
a single function) is extracted into a separate file where it's
optimized differently, before being merged back in.

Patch by Luqman Aden!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235473 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith
2015-04-22 04:11:00 +00:00
parent d9443d717d
commit fae374b95e
11 changed files with 135 additions and 10 deletions

View File

@ -38,6 +38,11 @@ static cl::list<std::string>
InputFilenames(cl::Positional, cl::OneOrMore,
cl::desc("<input bitcode files>"));
static cl::list<std::string> OverridingInputs(
"override", cl::ZeroOrMore, cl::value_desc("filename"),
cl::desc(
"input bitcode file which can override previously defined symbol(s)"));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
cl::value_desc("filename"));
@ -108,7 +113,8 @@ static void diagnosticHandler(const DiagnosticInfo &DI) {
}
static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
const cl::list<std::string> &Files) {
const cl::list<std::string> &Files,
bool OverrideDuplicateSymbols) {
for (const auto &File : Files) {
std::unique_ptr<Module> M = loadFile(argv0, File, Context);
if (!M.get()) {
@ -124,7 +130,7 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
if (Verbose)
errs() << "Linking in '" << File << "'\n";
if (L.linkInModule(M.get()))
if (L.linkInModule(M.get(), OverrideDuplicateSymbols))
return false;
}
@ -143,7 +149,12 @@ int main(int argc, char **argv) {
auto Composite = make_unique<Module>("llvm-link", Context);
Linker L(Composite.get(), diagnosticHandler);
if (!linkFiles(argv[0], Context, L, InputFilenames))
// First add all the regular input files
if (!linkFiles(argv[0], Context, L, InputFilenames, false))
return 1;
// Next the -override ones.
if (!linkFiles(argv[0], Context, L, OverridingInputs, true))
return 1;
if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;