2016-08-11 14:58:12 +00:00
|
|
|
//===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the "backend" phase of LTO, i.e. it performs
|
|
|
|
// optimization and code generation on a loaded module. It is generally used
|
|
|
|
// internally by the LTO class but can also be used independently, for example
|
|
|
|
// to implement a standalone ThinLTO backend.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2016-08-17 06:23:09 +00:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-08-11 14:58:12 +00:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/ThreadPool.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/SplitModule.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lto;
|
|
|
|
|
|
|
|
Error Config::addSaveTemps(std::string OutputFileName,
|
|
|
|
bool UseInputModulePath) {
|
|
|
|
ShouldDiscardValueNames = false;
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
ResolutionFile = llvm::make_unique<raw_fd_ostream>(
|
2016-08-18 00:12:33 +00:00
|
|
|
OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
|
2016-08-11 14:58:12 +00:00
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
|
|
|
auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
|
|
|
|
// Keep track of the hook provided by the linker, which also needs to run.
|
|
|
|
ModuleHookFn LinkerHook = Hook;
|
2016-08-11 20:38:39 +00:00
|
|
|
Hook = [=](unsigned Task, Module &M) {
|
2016-08-11 14:58:12 +00:00
|
|
|
// If the linker's hook returned false, we need to pass that result
|
|
|
|
// through.
|
|
|
|
if (LinkerHook && !LinkerHook(Task, M))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string PathPrefix;
|
|
|
|
// If this is the combined module (not a ThinLTO backend compile) or the
|
|
|
|
// user hasn't requested using the input module's path, emit to a file
|
|
|
|
// named from the provided OutputFileName with the Task ID appended.
|
|
|
|
if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
|
2016-08-18 00:12:33 +00:00
|
|
|
PathPrefix = OutputFileName + utostr(Task);
|
2016-08-11 14:58:12 +00:00
|
|
|
} else
|
|
|
|
PathPrefix = M.getModuleIdentifier();
|
|
|
|
std::string Path = PathPrefix + "." + PathSuffix + ".bc";
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
|
|
|
|
if (EC) {
|
|
|
|
// Because -save-temps is a debugging feature, we report the error
|
|
|
|
// directly and exit.
|
|
|
|
llvm::errs() << "failed to open " << Path << ": " << EC.message()
|
|
|
|
<< '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
setHook("0.preopt", PreOptModuleHook);
|
|
|
|
setHook("1.promote", PostPromoteModuleHook);
|
|
|
|
setHook("2.internalize", PostInternalizeModuleHook);
|
|
|
|
setHook("3.import", PostImportModuleHook);
|
|
|
|
setHook("4.opt", PostOptModuleHook);
|
|
|
|
setHook("5.precodegen", PreCodeGenModuleHook);
|
|
|
|
|
|
|
|
CombinedIndexHook = [=](const ModuleSummaryIndex &Index) {
|
2016-08-18 00:12:33 +00:00
|
|
|
std::string Path = OutputFileName + "index.bc";
|
2016-08-11 14:58:12 +00:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
|
|
|
|
if (EC) {
|
|
|
|
// Because -save-temps is a debugging feature, we report the error
|
|
|
|
// directly and exit.
|
|
|
|
llvm::errs() << "failed to open " << Path << ": " << EC.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
WriteIndexToFile(Index, OS);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Error();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::unique_ptr<TargetMachine>
|
|
|
|
createTargetMachine(Config &C, StringRef TheTriple, const Target *TheTarget) {
|
|
|
|
SubtargetFeatures Features;
|
|
|
|
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
|
|
|
|
for (const std::string &A : C.MAttrs)
|
|
|
|
Features.AddFeature(A);
|
|
|
|
|
|
|
|
return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
|
|
|
|
TheTriple, C.CPU, Features.getString(), C.Options, C.RelocModel,
|
|
|
|
C.CodeModel, C.CGOptLevel));
|
|
|
|
}
|
|
|
|
|
2016-08-11 20:38:39 +00:00
|
|
|
bool opt(Config &C, TargetMachine *TM, unsigned Task, Module &M,
|
|
|
|
bool IsThinLto) {
|
2016-08-11 14:58:12 +00:00
|
|
|
M.setDataLayout(TM->createDataLayout());
|
|
|
|
|
|
|
|
legacy::PassManager passes;
|
|
|
|
passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
|
|
|
|
|
|
|
|
PassManagerBuilder PMB;
|
|
|
|
PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
|
|
|
|
PMB.Inliner = createFunctionInliningPass();
|
|
|
|
// Unconditionally verify input since it is not verified before this
|
|
|
|
// point and has unknown origin.
|
|
|
|
PMB.VerifyInput = true;
|
|
|
|
PMB.VerifyOutput = !C.DisableVerify;
|
|
|
|
PMB.LoopVectorize = true;
|
|
|
|
PMB.SLPVectorize = true;
|
|
|
|
PMB.OptLevel = C.OptLevel;
|
|
|
|
if (IsThinLto)
|
|
|
|
PMB.populateThinLTOPassManager(passes);
|
|
|
|
else
|
|
|
|
PMB.populateLTOPassManager(passes);
|
|
|
|
passes.run(M);
|
|
|
|
|
|
|
|
if (C.PostOptModuleHook && !C.PostOptModuleHook(Task, M))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:23:09 +00:00
|
|
|
void codegen(Config &C, TargetMachine *TM, AddOutputFn AddOutput, unsigned Task,
|
2016-08-11 14:58:12 +00:00
|
|
|
Module &M) {
|
|
|
|
if (C.PreCodeGenModuleHook && !C.PreCodeGenModuleHook(Task, M))
|
|
|
|
return;
|
|
|
|
|
2016-08-17 06:23:09 +00:00
|
|
|
auto Output = AddOutput(Task);
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS = Output->getStream();
|
2016-08-11 14:58:12 +00:00
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
|
|
|
|
TargetMachine::CGFT_ObjectFile))
|
|
|
|
report_fatal_error("Failed to setup codegen");
|
|
|
|
CodeGenPasses.run(M);
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:23:09 +00:00
|
|
|
void splitCodeGen(Config &C, TargetMachine *TM, AddOutputFn AddOutput,
|
2016-08-11 14:58:12 +00:00
|
|
|
unsigned ParallelCodeGenParallelismLevel,
|
|
|
|
std::unique_ptr<Module> M) {
|
|
|
|
ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
|
|
|
|
unsigned ThreadCount = 0;
|
|
|
|
const Target *T = &TM->getTarget();
|
|
|
|
|
|
|
|
SplitModule(
|
|
|
|
std::move(M), ParallelCodeGenParallelismLevel,
|
|
|
|
[&](std::unique_ptr<Module> MPart) {
|
|
|
|
// We want to clone the module in a new context to multi-thread the
|
|
|
|
// codegen. We do it by serializing partition modules to bitcode
|
|
|
|
// (while still on the main thread, in order to avoid data races) and
|
|
|
|
// spinning up new threads which deserialize the partitions into
|
|
|
|
// separate contexts.
|
|
|
|
// FIXME: Provide a more direct way to do this in LLVM.
|
|
|
|
SmallString<0> BC;
|
|
|
|
raw_svector_ostream BCOS(BC);
|
|
|
|
WriteBitcodeToFile(MPart.get(), BCOS);
|
|
|
|
|
|
|
|
// Enqueue the task
|
|
|
|
CodegenThreadPool.async(
|
|
|
|
[&](const SmallString<0> &BC, unsigned ThreadId) {
|
|
|
|
LTOLLVMContext Ctx(C);
|
|
|
|
ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
|
|
|
|
MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
|
|
|
|
Ctx);
|
|
|
|
if (!MOrErr)
|
|
|
|
report_fatal_error("Failed to read bitcode");
|
|
|
|
std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
|
|
|
|
|
|
|
|
std::unique_ptr<TargetMachine> TM =
|
|
|
|
createTargetMachine(C, MPartInCtx->getTargetTriple(), T);
|
2016-08-17 06:23:09 +00:00
|
|
|
codegen(C, TM.get(), AddOutput, ThreadId, *MPartInCtx);
|
2016-08-11 14:58:12 +00:00
|
|
|
},
|
|
|
|
// Pass BC using std::move to ensure that it get moved rather than
|
|
|
|
// copied into the thread's context.
|
|
|
|
std::move(BC), ThreadCount++);
|
|
|
|
},
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<const Target *> initAndLookupTarget(Config &C, Module &M) {
|
|
|
|
if (!C.OverrideTriple.empty())
|
|
|
|
M.setTargetTriple(C.OverrideTriple);
|
|
|
|
else if (M.getTargetTriple().empty())
|
|
|
|
M.setTargetTriple(C.DefaultTriple);
|
|
|
|
|
|
|
|
std::string Msg;
|
|
|
|
const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
|
|
|
|
if (!T)
|
|
|
|
return make_error<StringError>(Msg, inconvertibleErrorCode());
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:23:09 +00:00
|
|
|
Error lto::backend(Config &C, AddOutputFn AddOutput,
|
2016-08-11 14:58:12 +00:00
|
|
|
unsigned ParallelCodeGenParallelismLevel,
|
|
|
|
std::unique_ptr<Module> M) {
|
|
|
|
Expected<const Target *> TOrErr = initAndLookupTarget(C, *M);
|
|
|
|
if (!TOrErr)
|
|
|
|
return TOrErr.takeError();
|
|
|
|
|
|
|
|
std::unique_ptr<TargetMachine> TM =
|
|
|
|
createTargetMachine(C, M->getTargetTriple(), *TOrErr);
|
|
|
|
|
2016-08-22 06:25:41 +00:00
|
|
|
if (!C.CodeGenOnly)
|
|
|
|
if (!opt(C, TM.get(), 0, *M, /*IsThinLto=*/false))
|
|
|
|
return Error();
|
2016-08-11 14:58:12 +00:00
|
|
|
|
|
|
|
if (ParallelCodeGenParallelismLevel == 1)
|
2016-08-17 06:23:09 +00:00
|
|
|
codegen(C, TM.get(), AddOutput, 0, *M);
|
2016-08-11 14:58:12 +00:00
|
|
|
else
|
2016-08-17 06:23:09 +00:00
|
|
|
splitCodeGen(C, TM.get(), AddOutput, ParallelCodeGenParallelismLevel,
|
2016-08-11 14:58:12 +00:00
|
|
|
std::move(M));
|
|
|
|
return Error();
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:23:09 +00:00
|
|
|
Error lto::thinBackend(Config &Conf, unsigned Task, AddOutputFn AddOutput,
|
2016-08-16 00:44:46 +00:00
|
|
|
Module &Mod, ModuleSummaryIndex &CombinedIndex,
|
2016-08-11 14:58:12 +00:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals,
|
|
|
|
MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
|
2016-08-16 00:44:46 +00:00
|
|
|
Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
|
2016-08-11 14:58:12 +00:00
|
|
|
if (!TOrErr)
|
|
|
|
return TOrErr.takeError();
|
|
|
|
|
|
|
|
std::unique_ptr<TargetMachine> TM =
|
2016-08-16 00:44:46 +00:00
|
|
|
createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
|
2016-08-11 14:58:12 +00:00
|
|
|
|
2016-08-22 06:25:41 +00:00
|
|
|
if (Conf.CodeGenOnly) {
|
|
|
|
codegen(Conf, TM.get(), AddOutput, Task, Mod);
|
|
|
|
return Error();
|
|
|
|
}
|
|
|
|
|
2016-08-16 00:44:46 +00:00
|
|
|
if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
|
2016-08-11 14:58:12 +00:00
|
|
|
return Error();
|
|
|
|
|
2016-08-16 00:44:46 +00:00
|
|
|
renameModuleForThinLTO(Mod, CombinedIndex);
|
2016-08-11 14:58:12 +00:00
|
|
|
|
2016-08-18 00:59:24 +00:00
|
|
|
thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
|
|
|
|
|
2016-08-16 00:44:46 +00:00
|
|
|
if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
|
2016-08-11 14:58:12 +00:00
|
|
|
return Error();
|
|
|
|
|
|
|
|
if (!DefinedGlobals.empty())
|
2016-08-16 00:44:46 +00:00
|
|
|
thinLTOInternalizeModule(Mod, DefinedGlobals);
|
2016-08-11 14:58:12 +00:00
|
|
|
|
2016-08-16 00:44:46 +00:00
|
|
|
if (Conf.PostInternalizeModuleHook &&
|
|
|
|
!Conf.PostInternalizeModuleHook(Task, Mod))
|
2016-08-11 14:58:12 +00:00
|
|
|
return Error();
|
|
|
|
|
|
|
|
auto ModuleLoader = [&](StringRef Identifier) {
|
|
|
|
return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer(
|
|
|
|
ModuleMap[Identifier], false),
|
2016-08-16 00:44:46 +00:00
|
|
|
Mod.getContext(),
|
2016-08-11 14:58:12 +00:00
|
|
|
/*ShouldLazyLoadMetadata=*/true)
|
|
|
|
.get());
|
|
|
|
};
|
|
|
|
|
|
|
|
FunctionImporter Importer(CombinedIndex, ModuleLoader);
|
2016-08-16 00:44:46 +00:00
|
|
|
Importer.importFunctions(Mod, ImportList);
|
2016-08-11 14:58:12 +00:00
|
|
|
|
2016-08-16 00:44:46 +00:00
|
|
|
if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
|
2016-08-11 14:58:12 +00:00
|
|
|
return Error();
|
|
|
|
|
2016-08-16 00:44:46 +00:00
|
|
|
if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLto=*/true))
|
2016-08-11 14:58:12 +00:00
|
|
|
return Error();
|
|
|
|
|
2016-08-17 06:23:09 +00:00
|
|
|
codegen(Conf, TM.get(), AddOutput, Task, Mod);
|
2016-08-11 14:58:12 +00:00
|
|
|
return Error();
|
|
|
|
}
|