2016-03-23 04:52:10 +08:00
|
|
|
//===- LTO.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LTO.h"
|
|
|
|
#include "Config.h"
|
2016-04-29 03:30:41 +08:00
|
|
|
#include "Driver.h"
|
2016-03-23 04:52:10 +08:00
|
|
|
#include "Error.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2016-04-01 08:35:29 +08:00
|
|
|
#include "llvm/CodeGen/CommandFlags.h"
|
2016-04-16 06:38:10 +08:00
|
|
|
#include "llvm/CodeGen/ParallelCG.h"
|
2016-03-23 04:52:10 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#include "llvm/Linker/IRMover.h"
|
|
|
|
#include "llvm/Support/StringSaver.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2016-04-28 07:54:04 +08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2016-03-23 04:52:10 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
// This is for use when debugging LTO.
|
2016-04-16 06:38:10 +08:00
|
|
|
static void saveLtoObjectFile(StringRef Buffer, unsigned I, bool Many) {
|
|
|
|
SmallString<128> Filename = Config->OutputFile;
|
|
|
|
if (Many)
|
|
|
|
Filename += utostr(I);
|
|
|
|
Filename += ".lto.o";
|
2016-03-23 04:52:10 +08:00
|
|
|
std::error_code EC;
|
2016-04-16 06:38:10 +08:00
|
|
|
raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None);
|
2016-03-23 04:52:10 +08:00
|
|
|
check(EC);
|
|
|
|
OS << Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for use when debugging LTO.
|
|
|
|
static void saveBCFile(Module &M, StringRef Suffix) {
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC,
|
|
|
|
sys::fs::OpenFlags::F_None);
|
|
|
|
check(EC);
|
|
|
|
WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run LTO passes.
|
2016-03-30 03:19:03 +08:00
|
|
|
// Note that the gold plugin has a similar piece of code, so
|
|
|
|
// it is probably better to move this code to a common place.
|
2016-03-23 04:52:10 +08:00
|
|
|
static void runLTOPasses(Module &M, TargetMachine &TM) {
|
|
|
|
legacy::PassManager LtoPasses;
|
|
|
|
LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
|
|
|
|
PassManagerBuilder PMB;
|
|
|
|
PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
|
|
|
|
PMB.Inliner = createFunctionInliningPass();
|
2016-04-03 11:39:09 +08:00
|
|
|
PMB.VerifyInput = PMB.VerifyOutput = !Config->DisableVerify;
|
2016-03-23 04:52:10 +08:00
|
|
|
PMB.LoopVectorize = true;
|
|
|
|
PMB.SLPVectorize = true;
|
2016-04-01 05:00:27 +08:00
|
|
|
PMB.OptLevel = Config->LtoO;
|
2016-03-23 04:52:10 +08:00
|
|
|
PMB.populateLTOPassManager(LtoPasses);
|
|
|
|
LtoPasses.run(M);
|
|
|
|
|
|
|
|
if (Config->SaveTemps)
|
|
|
|
saveBCFile(M, ".lto.opt.bc");
|
|
|
|
}
|
|
|
|
|
2016-04-22 04:35:25 +08:00
|
|
|
static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used,
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
Symbol *S, GlobalValue *GV) {
|
|
|
|
if (S->IsUsedInRegularObj)
|
2016-04-22 04:35:25 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Used.count(GV))
|
|
|
|
return false;
|
|
|
|
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
return !S->includeInDynsym();
|
2016-04-22 04:35:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 05:16:18 +08:00
|
|
|
BitcodeCompiler::BitcodeCompiler()
|
2016-04-29 03:30:41 +08:00
|
|
|
: Combined(new llvm::Module("ld-temp.o", Driver->Context)),
|
|
|
|
Mover(*Combined) {}
|
2016-04-23 05:16:18 +08:00
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
void BitcodeCompiler::add(BitcodeFile &F) {
|
2016-04-29 03:30:41 +08:00
|
|
|
std::unique_ptr<IRObjectFile> Obj = std::move(F.Obj);
|
2016-03-23 04:52:10 +08:00
|
|
|
std::vector<GlobalValue *> Keep;
|
|
|
|
unsigned BodyIndex = 0;
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
ArrayRef<Symbol *> Syms = F.getSymbols();
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2016-03-30 05:46:35 +08:00
|
|
|
Module &M = Obj->getModule();
|
2016-04-16 09:33:33 +08:00
|
|
|
if (M.getDataLayoutStr().empty())
|
|
|
|
fatal("invalid bitcode file: " + F.getName() + " has no datalayout");
|
2016-03-30 07:57:22 +08:00
|
|
|
|
|
|
|
// If a symbol appears in @llvm.used, the linker is required
|
|
|
|
// to treat the symbol as there is a reference to the symbol
|
|
|
|
// that it cannot see. Therefore, we can't internalize.
|
2016-03-30 05:46:35 +08:00
|
|
|
SmallPtrSet<GlobalValue *, 8> Used;
|
|
|
|
collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false);
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
for (const BasicSymbolRef &Sym : Obj->symbols()) {
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
uint32_t Flags = Sym.getFlags();
|
2016-03-23 04:52:10 +08:00
|
|
|
GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
if (GV && GV->hasAppendingLinkage())
|
2016-03-23 04:52:10 +08:00
|
|
|
Keep.push_back(GV);
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
if (BitcodeFile::shouldSkip(Flags))
|
2016-03-23 04:52:10 +08:00
|
|
|
continue;
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
Symbol *S = Syms[BodyIndex++];
|
|
|
|
if (Flags & BasicSymbolRef::SF_Undefined)
|
2016-03-27 02:33:09 +08:00
|
|
|
continue;
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
auto *B = dyn_cast<DefinedBitcode>(S->body());
|
|
|
|
if (!B || B->File != &F)
|
2016-03-27 02:33:09 +08:00
|
|
|
continue;
|
2016-05-06 01:13:49 +08:00
|
|
|
|
|
|
|
// We collect the set of symbols we want to internalize here
|
|
|
|
// and change the linkage after the IRMover executed, i.e. after
|
|
|
|
// we imported the symbols and satisfied undefined references
|
|
|
|
// to it. We can't just change linkage here because otherwise
|
|
|
|
// the IRMover will just rename the symbol.
|
|
|
|
if (GV && shouldInternalize(Used, S, GV))
|
|
|
|
InternalizedSyms.insert(GV->getName());
|
|
|
|
|
|
|
|
// At this point we know that either the combined LTO object will provide a
|
|
|
|
// definition of a symbol, or we will internalize it. In either case, we
|
|
|
|
// need to undefine the symbol. In the former case, the real definition
|
|
|
|
// needs to be able to replace the original definition without conflicting.
|
|
|
|
// In the latter case, we need to allow the combined LTO object to provide a
|
|
|
|
// definition with the same name, for example when doing parallel codegen.
|
|
|
|
replaceBody<Undefined>(S, S->body()->getName(), STV_DEFAULT, 0);
|
|
|
|
|
|
|
|
if (!GV)
|
|
|
|
// Module asm symbol.
|
|
|
|
continue;
|
|
|
|
|
2016-03-27 02:33:09 +08:00
|
|
|
switch (GV->getLinkage()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case llvm::GlobalValue::LinkOnceAnyLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::WeakAnyLinkage);
|
|
|
|
break;
|
|
|
|
case llvm::GlobalValue::LinkOnceODRLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::WeakODRLinkage);
|
|
|
|
break;
|
2016-03-23 06:31:34 +08:00
|
|
|
}
|
2016-03-28 23:44:21 +08:00
|
|
|
|
2016-03-27 02:33:09 +08:00
|
|
|
Keep.push_back(GV);
|
2016-03-23 04:52:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Mover.move(Obj->takeModule(), Keep,
|
|
|
|
[](GlobalValue &, IRMover::ValueAdder) {});
|
|
|
|
}
|
|
|
|
|
2016-03-28 23:44:21 +08:00
|
|
|
static void internalize(GlobalValue &GV) {
|
|
|
|
assert(!GV.hasLocalLinkage() &&
|
2016-03-30 05:48:25 +08:00
|
|
|
"Trying to internalize a symbol with local linkage!");
|
2016-03-28 23:44:21 +08:00
|
|
|
GV.setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
}
|
|
|
|
|
2016-04-18 07:20:08 +08:00
|
|
|
std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen(
|
|
|
|
const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) {
|
2016-04-16 06:38:10 +08:00
|
|
|
unsigned NumThreads = Config->LtoJobs;
|
|
|
|
OwningData.resize(NumThreads);
|
|
|
|
|
|
|
|
std::list<raw_svector_ostream> OSs;
|
|
|
|
std::vector<raw_pwrite_stream *> OSPtrs;
|
|
|
|
for (SmallString<0> &Obj : OwningData) {
|
|
|
|
OSs.emplace_back(Obj);
|
|
|
|
OSPtrs.push_back(&OSs.back());
|
|
|
|
}
|
|
|
|
|
2016-04-18 07:20:08 +08:00
|
|
|
splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory);
|
2016-04-16 06:38:10 +08:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<InputFile>> ObjFiles;
|
|
|
|
for (SmallString<0> &Obj : OwningData)
|
|
|
|
ObjFiles.push_back(createObjectFile(
|
|
|
|
MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object")));
|
|
|
|
|
|
|
|
if (Config->SaveTemps)
|
|
|
|
for (unsigned I = 0; I < NumThreads; ++I)
|
|
|
|
saveLtoObjectFile(OwningData[I], I, NumThreads > 1);
|
|
|
|
|
|
|
|
return ObjFiles;
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
|
|
|
// and return the resulting ObjectFile.
|
2016-04-16 06:38:10 +08:00
|
|
|
std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::compile() {
|
|
|
|
TheTriple = Combined->getTargetTriple();
|
2016-03-28 23:44:21 +08:00
|
|
|
for (const auto &Name : InternalizedSyms) {
|
2016-04-12 06:39:51 +08:00
|
|
|
GlobalValue *GV = Combined->getNamedValue(Name.first());
|
2016-03-28 23:44:21 +08:00
|
|
|
assert(GV);
|
|
|
|
internalize(*GV);
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
if (Config->SaveTemps)
|
2016-04-12 06:39:51 +08:00
|
|
|
saveBCFile(*Combined, ".lto.bc");
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2016-03-24 05:19:27 +08:00
|
|
|
std::string Msg;
|
2016-04-16 06:38:10 +08:00
|
|
|
const Target *T = TargetRegistry::lookupTarget(TheTriple, Msg);
|
2016-03-24 05:19:27 +08:00
|
|
|
if (!T)
|
|
|
|
fatal("target not found: " + Msg);
|
2016-04-01 08:35:29 +08:00
|
|
|
TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
|
2016-03-24 05:19:27 +08:00
|
|
|
Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
|
2016-04-18 07:20:08 +08:00
|
|
|
|
|
|
|
auto CreateTargetMachine = [&]() {
|
|
|
|
return std::unique_ptr<TargetMachine>(
|
|
|
|
T->createTargetMachine(TheTriple, "", "", Options, R));
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<TargetMachine> TM = CreateTargetMachine();
|
|
|
|
runLTOPasses(*Combined, *TM);
|
|
|
|
|
|
|
|
return runSplitCodegen(CreateTargetMachine);
|
2016-03-24 05:19:27 +08:00
|
|
|
}
|