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"
|
|
|
|
#include "Error.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Symbols.h"
|
2016-04-01 08:35:29 +08:00
|
|
|
#include "llvm/CodeGen/CommandFlags.h"
|
2016-09-29 08:40:08 +08:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
|
|
|
#include "llvm/LTO/LTO.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-07-15 10:17:13 +08:00
|
|
|
static void saveBuffer(StringRef Buffer, const Twine &Path) {
|
2016-03-23 04:52:10 +08:00
|
|
|
std::error_code EC;
|
2016-07-15 10:17:13 +08:00
|
|
|
raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
|
2016-07-15 10:01:03 +08:00
|
|
|
if (EC)
|
|
|
|
error(EC, "cannot create " + Path);
|
2016-03-23 04:52:10 +08:00
|
|
|
OS << Buffer;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI) {
|
|
|
|
SmallString<128> ErrStorage;
|
|
|
|
raw_svector_ostream OS(ErrStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
2016-09-30 05:00:23 +08:00
|
|
|
warn(ErrStorage);
|
2016-03-23 04:52:10 +08:00
|
|
|
}
|
|
|
|
|
2016-09-30 05:00:26 +08:00
|
|
|
static void checkError(Error E) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
error(EIB.message());
|
|
|
|
return Error::success();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
static std::unique_ptr<lto::LTO> createLTO() {
|
|
|
|
lto::Config Conf;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
// LLD supports the new relocations.
|
|
|
|
Conf.Options = InitTargetOptionsFromCodeGenFlags();
|
|
|
|
Conf.Options.RelaxELFRelocations = true;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
Conf.RelocModel = Config->Pic ? Reloc::PIC_ : Reloc::Static;
|
|
|
|
Conf.DisableVerify = Config->DisableVerify;
|
|
|
|
Conf.DiagHandler = diagnosticHandler;
|
|
|
|
Conf.OptLevel = Config->LtoO;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
// Set up a custom pipeline if we've been asked to.
|
|
|
|
Conf.OptPipeline = Config->LtoNewPmPasses;
|
|
|
|
Conf.AAPipeline = Config->LtoAAPipeline;
|
2016-03-23 04:52:10 +08:00
|
|
|
|
|
|
|
if (Config->SaveTemps)
|
2016-09-30 05:00:26 +08:00
|
|
|
checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
|
2016-09-29 08:40:08 +08:00
|
|
|
/*UseInputModulePath*/ true));
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2016-10-11 02:12:53 +08:00
|
|
|
lto::ThinBackend Backend;
|
|
|
|
if (Config->LtoJobs)
|
|
|
|
Backend = lto::createInProcessThinBackend(Config->LtoJobs);
|
2016-09-29 09:13:32 +08:00
|
|
|
return llvm::make_unique<lto::LTO>(std::move(Conf), Backend, Config->LtoJobs);
|
2016-04-22 04:35:25 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
BitcodeCompiler::BitcodeCompiler() : LtoObj(createLTO()) {}
|
|
|
|
|
|
|
|
BitcodeCompiler::~BitcodeCompiler() {}
|
2016-04-23 05:16:18 +08:00
|
|
|
|
2016-05-13 03:46:14 +08:00
|
|
|
static void undefine(Symbol *S) {
|
2016-07-17 11:11:46 +08:00
|
|
|
replaceBody<Undefined>(S, S->body()->getName(), STV_DEFAULT, S->body()->Type,
|
|
|
|
nullptr);
|
2016-05-13 03:46:14 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
void BitcodeCompiler::add(BitcodeFile &F) {
|
2016-09-29 08:40:08 +08:00
|
|
|
lto::InputFile &Obj = *F.Obj;
|
|
|
|
if (Obj.getDataLayoutStr().empty())
|
2016-04-16 09:33:33 +08:00
|
|
|
fatal("invalid bitcode file: " + F.getName() + " has no datalayout");
|
2016-03-30 07:57:22 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
unsigned SymNum = 0;
|
|
|
|
std::vector<Symbol *> Syms = F.getSymbols();
|
|
|
|
std::vector<lto::SymbolResolution> Resols(Syms.size());
|
|
|
|
|
|
|
|
// Provide a resolution to the LTO API for each symbol.
|
|
|
|
for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
|
|
|
|
Symbol *Sym = Syms[SymNum];
|
|
|
|
lto::SymbolResolution &R = Resols[SymNum];
|
|
|
|
++SymNum;
|
|
|
|
SymbolBody *B = Sym->body();
|
|
|
|
|
|
|
|
// Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
|
|
|
|
// reports two symbols for module ASM defined. Without this check, lld
|
|
|
|
// flags an undefined in IR with a definition in ASM as prevailing.
|
|
|
|
// Once IRObjectFile is fixed to report only one symbol this hack can
|
|
|
|
// be removed.
|
|
|
|
R.Prevailing =
|
|
|
|
!(ObjSym.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
|
|
|
|
B->File == &F;
|
|
|
|
|
|
|
|
R.VisibleToRegularObj =
|
|
|
|
Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym());
|
|
|
|
if (R.Prevailing)
|
|
|
|
undefine(Sym);
|
2016-07-15 10:17:13 +08:00
|
|
|
}
|
2016-09-30 05:00:26 +08:00
|
|
|
checkError(LtoObj->add(std::move(F.Obj), Resols));
|
2016-04-16 06:38:10 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
2016-09-29 08:40:08 +08:00
|
|
|
// and return the resulting ObjectFile(s).
|
2016-09-14 08:05:51 +08:00
|
|
|
std::vector<InputFile *> BitcodeCompiler::compile() {
|
2016-09-29 08:40:08 +08:00
|
|
|
std::vector<InputFile *> Ret;
|
|
|
|
unsigned MaxTasks = LtoObj->getMaxTasks();
|
|
|
|
Buff.resize(MaxTasks);
|
|
|
|
|
2016-09-30 06:50:37 +08:00
|
|
|
checkError(LtoObj->run([&](size_t Task) {
|
2016-09-29 08:40:08 +08:00
|
|
|
return llvm::make_unique<lto::NativeObjectStream>(
|
|
|
|
llvm::make_unique<llvm::raw_svector_ostream>(Buff[Task]));
|
2016-09-30 06:50:37 +08:00
|
|
|
}));
|
2016-04-18 07:20:08 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
for (unsigned I = 0; I != MaxTasks; ++I) {
|
|
|
|
if (Buff[I].empty())
|
|
|
|
continue;
|
|
|
|
if (Config->SaveTemps) {
|
|
|
|
if (MaxTasks == 1)
|
|
|
|
saveBuffer(Buff[I], Config->OutputFile + ".lto.o");
|
|
|
|
else
|
|
|
|
saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
|
|
|
|
}
|
2016-09-30 06:50:37 +08:00
|
|
|
InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
|
2016-09-29 08:40:08 +08:00
|
|
|
Ret.push_back(Obj);
|
|
|
|
}
|
|
|
|
return Ret;
|
2016-03-24 05:19:27 +08:00
|
|
|
}
|