2016-08-11 16:58:12 +02: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"
|
2016-09-07 19:46:16 +02:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/CGSCCPassManager.h"
|
2016-08-11 16:58:12 +02:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-11-11 06:34:58 +01:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-08-11 16:58:12 +02:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2016-09-07 19:46:16 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
|
|
|
#include "llvm/IR/Verifier.h"
|
2016-08-17 08:23:09 +02:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-08-11 16:58:12 +02:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2017-03-29 01:35:34 +02:00
|
|
|
#include "llvm/Object/ModuleSymbolTable.h"
|
2016-09-07 19:46:16 +02:00
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
2016-08-11 16:58:12 +02:00
|
|
|
#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"
|
2017-01-11 10:43:56 +01:00
|
|
|
#include "llvm/Transforms/Scalar/LoopPassManager.h"
|
2016-08-11 16:58:12 +02:00
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/SplitModule.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lto;
|
|
|
|
|
2016-10-18 21:39:31 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) {
|
2016-09-18 00:32:42 +02:00
|
|
|
errs() << "failed to open " << Path << ": " << Msg << '\n';
|
|
|
|
errs().flush();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
Error Config::addSaveTemps(std::string OutputFileName,
|
|
|
|
bool UseInputModulePath) {
|
|
|
|
ShouldDiscardValueNames = false;
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
ResolutionFile = llvm::make_unique<raw_fd_ostream>(
|
2016-08-18 02:12:33 +02:00
|
|
|
OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
|
2016-08-11 16:58:12 +02: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-22 18:17:40 +02:00
|
|
|
Hook = [=](unsigned Task, const Module &M) {
|
2016-08-11 16:58:12 +02: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 02:12:33 +02:00
|
|
|
PathPrefix = OutputFileName + utostr(Task);
|
2016-08-11 16:58:12 +02: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);
|
2016-09-18 00:32:42 +02:00
|
|
|
// Because -save-temps is a debugging feature, we report the error
|
|
|
|
// directly and exit.
|
|
|
|
if (EC)
|
|
|
|
reportOpenError(Path, EC.message());
|
2016-08-11 16:58:12 +02:00
|
|
|
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 02:12:33 +02:00
|
|
|
std::string Path = OutputFileName + "index.bc";
|
2016-08-11 16:58:12 +02:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
|
2016-09-18 00:32:42 +02:00
|
|
|
// Because -save-temps is a debugging feature, we report the error
|
|
|
|
// directly and exit.
|
|
|
|
if (EC)
|
|
|
|
reportOpenError(Path, EC.message());
|
2016-08-11 16:58:12 +02:00
|
|
|
WriteIndexToFile(Index, OS);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::unique_ptr<TargetMachine>
|
2017-05-22 23:11:35 +02:00
|
|
|
createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
|
|
|
|
StringRef TheTriple = M.getTargetTriple();
|
2016-08-11 16:58:12 +02:00
|
|
|
SubtargetFeatures Features;
|
|
|
|
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
|
2016-09-07 03:08:31 +02:00
|
|
|
for (const std::string &A : Conf.MAttrs)
|
2016-08-11 16:58:12 +02:00
|
|
|
Features.AddFeature(A);
|
|
|
|
|
2017-05-22 23:11:35 +02:00
|
|
|
Reloc::Model RelocModel;
|
|
|
|
if (Conf.RelocModel)
|
|
|
|
RelocModel = *Conf.RelocModel;
|
|
|
|
else
|
|
|
|
RelocModel =
|
|
|
|
M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
|
2017-05-22 23:11:35 +02:00
|
|
|
TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
|
2016-09-07 03:08:31 +02:00
|
|
|
Conf.CodeModel, Conf.CGOptLevel));
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 03:28:31 +02:00
|
|
|
static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
|
|
|
|
unsigned OptLevel, bool IsThinLTO) {
|
|
|
|
Optional<PGOOptions> PGOOpt;
|
|
|
|
if (!Conf.SampleProfile.empty())
|
|
|
|
PGOOpt = PGOOptions("", "", Conf.SampleProfile, false, true);
|
|
|
|
|
|
|
|
PassBuilder PB(TM, PGOOpt);
|
2017-01-24 01:58:24 +01:00
|
|
|
AAManager AA;
|
|
|
|
|
|
|
|
// Parse a custom AA pipeline if asked to.
|
2017-08-02 05:03:19 +02:00
|
|
|
if (!PB.parseAAPipeline(AA, "default"))
|
|
|
|
report_fatal_error("Error parsing default AA pipeline");
|
2017-01-24 01:58:24 +01:00
|
|
|
|
2017-08-02 05:03:19 +02:00
|
|
|
LoopAnalysisManager LAM(Conf.DebugPassManager);
|
|
|
|
FunctionAnalysisManager FAM(Conf.DebugPassManager);
|
|
|
|
CGSCCAnalysisManager CGAM(Conf.DebugPassManager);
|
|
|
|
ModuleAnalysisManager MAM(Conf.DebugPassManager);
|
2017-01-24 01:58:24 +01:00
|
|
|
|
|
|
|
// Register the AA manager first so that our version is the one used.
|
|
|
|
FAM.registerPass([&] { return std::move(AA); });
|
|
|
|
|
|
|
|
// Register all the basic analyses with the managers.
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
2017-08-02 05:03:19 +02:00
|
|
|
ModulePassManager MPM(Conf.DebugPassManager);
|
2017-01-24 01:58:24 +01:00
|
|
|
// FIXME (davide): verify the input.
|
|
|
|
|
|
|
|
PassBuilder::OptimizationLevel OL;
|
|
|
|
|
|
|
|
switch (OptLevel) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid optimization level");
|
|
|
|
case 0:
|
|
|
|
OL = PassBuilder::O0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
OL = PassBuilder::O1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
OL = PassBuilder::O2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
OL = PassBuilder::O3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-01 13:39:39 +02:00
|
|
|
if (IsThinLTO)
|
2017-08-02 05:03:19 +02:00
|
|
|
MPM = PB.buildThinLTODefaultPipeline(OL, Conf.DebugPassManager);
|
2017-06-01 13:39:39 +02:00
|
|
|
else
|
2017-08-02 05:03:19 +02:00
|
|
|
MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager);
|
2017-01-24 01:58:24 +01:00
|
|
|
MPM.run(Mod, MAM);
|
|
|
|
|
|
|
|
// FIXME (davide): verify the output.
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:46:16 +02:00
|
|
|
static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM,
|
|
|
|
std::string PipelineDesc,
|
2016-09-16 23:03:21 +02:00
|
|
|
std::string AAPipelineDesc,
|
2016-09-07 19:46:16 +02:00
|
|
|
bool DisableVerify) {
|
|
|
|
PassBuilder PB(TM);
|
|
|
|
AAManager AA;
|
2016-09-16 23:03:21 +02:00
|
|
|
|
|
|
|
// Parse a custom AA pipeline if asked to.
|
|
|
|
if (!AAPipelineDesc.empty())
|
|
|
|
if (!PB.parseAAPipeline(AA, AAPipelineDesc))
|
|
|
|
report_fatal_error("unable to parse AA pipeline description: " +
|
|
|
|
AAPipelineDesc);
|
|
|
|
|
2016-09-07 19:46:16 +02:00
|
|
|
LoopAnalysisManager LAM;
|
|
|
|
FunctionAnalysisManager FAM;
|
|
|
|
CGSCCAnalysisManager CGAM;
|
|
|
|
ModuleAnalysisManager MAM;
|
|
|
|
|
|
|
|
// Register the AA manager first so that our version is the one used.
|
|
|
|
FAM.registerPass([&] { return std::move(AA); });
|
|
|
|
|
|
|
|
// Register all the basic analyses with the managers.
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
|
|
|
ModulePassManager MPM;
|
|
|
|
|
|
|
|
// Always verify the input.
|
|
|
|
MPM.addPass(VerifierPass());
|
|
|
|
|
|
|
|
// Now, add all the passes we've been requested to.
|
|
|
|
if (!PB.parsePassPipeline(MPM, PipelineDesc))
|
|
|
|
report_fatal_error("unable to parse pass pipeline description: " +
|
|
|
|
PipelineDesc);
|
|
|
|
|
|
|
|
if (!DisableVerify)
|
|
|
|
MPM.addPass(VerifierPass());
|
|
|
|
MPM.run(Mod, MAM);
|
|
|
|
}
|
|
|
|
|
2016-09-07 03:08:31 +02:00
|
|
|
static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
|
2017-03-22 19:22:59 +01:00
|
|
|
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
|
|
|
|
const ModuleSummaryIndex *ImportSummary) {
|
2016-08-11 16:58:12 +02:00
|
|
|
legacy::PassManager passes;
|
|
|
|
passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
|
|
|
|
|
|
|
|
PassManagerBuilder PMB;
|
|
|
|
PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
|
|
|
|
PMB.Inliner = createFunctionInliningPass();
|
2017-03-22 19:22:59 +01:00
|
|
|
PMB.ExportSummary = ExportSummary;
|
|
|
|
PMB.ImportSummary = ImportSummary;
|
2016-08-11 16:58:12 +02:00
|
|
|
// Unconditionally verify input since it is not verified before this
|
|
|
|
// point and has unknown origin.
|
|
|
|
PMB.VerifyInput = true;
|
2016-09-07 03:08:31 +02:00
|
|
|
PMB.VerifyOutput = !Conf.DisableVerify;
|
2016-08-11 16:58:12 +02:00
|
|
|
PMB.LoopVectorize = true;
|
|
|
|
PMB.SLPVectorize = true;
|
2016-09-07 03:08:31 +02:00
|
|
|
PMB.OptLevel = Conf.OptLevel;
|
2016-12-16 17:48:46 +01:00
|
|
|
PMB.PGOSampleUse = Conf.SampleProfile;
|
2016-11-24 01:23:09 +01:00
|
|
|
if (IsThinLTO)
|
2016-08-11 16:58:12 +02:00
|
|
|
PMB.populateThinLTOPassManager(passes);
|
|
|
|
else
|
|
|
|
PMB.populateLTOPassManager(passes);
|
2016-09-07 03:08:31 +02:00
|
|
|
passes.run(Mod);
|
2016-08-31 19:02:44 +02:00
|
|
|
}
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-09-07 03:08:31 +02:00
|
|
|
bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
|
2017-03-22 19:22:59 +01:00
|
|
|
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
|
|
|
|
const ModuleSummaryIndex *ImportSummary) {
|
2017-01-24 01:58:24 +01:00
|
|
|
// FIXME: Plumb the combined index into the new pass manager.
|
|
|
|
if (!Conf.OptPipeline.empty())
|
2016-09-16 23:03:21 +02:00
|
|
|
runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline,
|
|
|
|
Conf.DisableVerify);
|
[ThinLTO] Move -lto-use-new-pm to llvm-lto2, and change it to -use-new-pm.
Summary:
As we teach Clang to use ThinkLTO + new PM, it's good for the users to
inject through Config, instead of setting a flag in the LTOBackend
library. Move the flag to llvm-lto2.
As it moves to llvm-lto2, a new name -use-new-pm seems simpler and as
clear.
Reviewers: davide, tejohnson
Subscribers: mehdi_amini, Prazek, inglorion, eraman, chandlerc, llvm-commits
Differential Revision: https://reviews.llvm.org/D33799
llvm-svn: 304492
2017-06-02 01:13:44 +02:00
|
|
|
else if (Conf.UseNewPM)
|
2017-08-02 03:28:31 +02:00
|
|
|
runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO);
|
2017-01-24 01:58:24 +01:00
|
|
|
else
|
2017-03-22 19:22:59 +01:00
|
|
|
runOldPMPasses(Conf, Mod, TM, IsThinLTO, ExportSummary, ImportSummary);
|
2016-09-07 03:08:31 +02:00
|
|
|
return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
|
2016-09-07 03:08:31 +02:00
|
|
|
unsigned Task, Module &Mod) {
|
|
|
|
if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
|
2016-08-11 16:58:12 +02:00
|
|
|
return;
|
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
auto Stream = AddStream(Task);
|
2016-08-11 16:58:12 +02:00
|
|
|
legacy::PassManager CodeGenPasses;
|
2017-02-15 21:36:36 +01:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, Conf.CGFileType))
|
2016-08-11 16:58:12 +02:00
|
|
|
report_fatal_error("Failed to setup codegen");
|
2016-09-07 03:08:31 +02:00
|
|
|
CodeGenPasses.run(Mod);
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
|
2016-08-11 16:58:12 +02:00
|
|
|
unsigned ParallelCodeGenParallelismLevel,
|
2016-09-07 03:08:31 +02:00
|
|
|
std::unique_ptr<Module> Mod) {
|
2016-08-11 16:58:12 +02:00
|
|
|
ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
|
|
|
|
unsigned ThreadCount = 0;
|
|
|
|
const Target *T = &TM->getTarget();
|
|
|
|
|
|
|
|
SplitModule(
|
2016-09-07 03:08:31 +02:00
|
|
|
std::move(Mod), ParallelCodeGenParallelismLevel,
|
2016-08-11 16:58:12 +02:00
|
|
|
[&](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);
|
2016-11-13 08:00:17 +01:00
|
|
|
Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
|
2016-08-11 16:58:12 +02:00
|
|
|
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 =
|
2017-05-22 23:11:35 +02:00
|
|
|
createTargetMachine(C, T, *MPartInCtx);
|
2016-08-23 23:30:12 +02:00
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx);
|
2016-08-11 16:58:12 +02: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);
|
2016-09-29 05:29:28 +02:00
|
|
|
|
|
|
|
// Because the inner lambda (which runs in a worker thread) captures our local
|
|
|
|
// variables, we need to wait for the worker threads to terminate before we
|
|
|
|
// can leave the function scope.
|
2016-09-29 03:28:36 +02:00
|
|
|
CodegenThreadPool.wait();
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 03:08:31 +02:00
|
|
|
Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!C.OverrideTriple.empty())
|
2016-09-07 03:08:31 +02:00
|
|
|
Mod.setTargetTriple(C.OverrideTriple);
|
|
|
|
else if (Mod.getTargetTriple().empty())
|
|
|
|
Mod.setTargetTriple(C.DefaultTriple);
|
2016-08-11 16:58:12 +02:00
|
|
|
|
|
|
|
std::string Msg;
|
2016-09-07 03:08:31 +02:00
|
|
|
const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!T)
|
|
|
|
return make_error<StringError>(Msg, inconvertibleErrorCode());
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:39:51 +01:00
|
|
|
static void
|
2017-09-23 03:03:17 +02:00
|
|
|
finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) {
|
2017-02-13 15:39:51 +01:00
|
|
|
// Make sure we flush the diagnostic remarks file in case the linker doesn't
|
|
|
|
// call the global destructors before exiting.
|
|
|
|
if (!DiagOutputFile)
|
|
|
|
return;
|
|
|
|
DiagOutputFile->keep();
|
|
|
|
DiagOutputFile->os().flush();
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
Error lto::backend(Config &C, AddStreamFn AddStream,
|
2016-08-11 16:58:12 +02:00
|
|
|
unsigned ParallelCodeGenParallelismLevel,
|
2017-01-20 23:18:52 +01:00
|
|
|
std::unique_ptr<Module> Mod,
|
|
|
|
ModuleSummaryIndex &CombinedIndex) {
|
2016-09-07 03:08:31 +02:00
|
|
|
Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!TOrErr)
|
|
|
|
return TOrErr.takeError();
|
|
|
|
|
2017-05-22 23:11:35 +02:00
|
|
|
std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2017-02-12 04:31:30 +01:00
|
|
|
// Setup optimization remarks.
|
|
|
|
auto DiagFileOrErr = lto::setupOptimizationRemarks(
|
|
|
|
Mod->getContext(), C.RemarksFilename, C.RemarksWithHotness);
|
|
|
|
if (!DiagFileOrErr)
|
|
|
|
return DiagFileOrErr.takeError();
|
2017-02-13 15:39:51 +01:00
|
|
|
auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
|
2017-02-12 04:31:30 +01:00
|
|
|
|
2017-02-13 15:39:51 +01:00
|
|
|
if (!C.CodeGenOnly) {
|
2017-03-22 19:22:59 +01:00
|
|
|
if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false,
|
|
|
|
/*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr)) {
|
2017-02-13 15:39:51 +01:00
|
|
|
finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2017-02-13 15:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-08-23 23:30:12 +02:00
|
|
|
if (ParallelCodeGenParallelismLevel == 1) {
|
2016-09-23 23:33:43 +02:00
|
|
|
codegen(C, TM.get(), AddStream, 0, *Mod);
|
2016-08-23 23:30:12 +02:00
|
|
|
} else {
|
2016-09-23 23:33:43 +02:00
|
|
|
splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
|
2016-09-07 03:08:31 +02:00
|
|
|
std::move(Mod));
|
2016-08-23 23:30:12 +02:00
|
|
|
}
|
2017-02-13 15:39:51 +01:00
|
|
|
finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
|
2017-03-22 19:22:59 +01:00
|
|
|
Module &Mod, const ModuleSummaryIndex &CombinedIndex,
|
2016-08-11 16:58:12 +02:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals,
|
2016-12-14 02:17:59 +01:00
|
|
|
MapVector<StringRef, BitcodeModule> &ModuleMap) {
|
2016-08-16 02:44:46 +02:00
|
|
|
Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!TOrErr)
|
|
|
|
return TOrErr.takeError();
|
|
|
|
|
2017-05-22 23:11:35 +02:00
|
|
|
std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-08-22 08:25:41 +02:00
|
|
|
if (Conf.CodeGenOnly) {
|
2016-09-23 23:33:43 +02:00
|
|
|
codegen(Conf, TM.get(), AddStream, Task, Mod);
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-22 08:25:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 02:44:46 +02:00
|
|
|
if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-08-16 02:44:46 +02:00
|
|
|
renameModuleForThinLTO(Mod, CombinedIndex);
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-08-18 02:59:24 +02:00
|
|
|
thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
|
|
|
|
|
2016-08-16 02:44:46 +02:00
|
|
|
if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
|
|
|
|
if (!DefinedGlobals.empty())
|
2016-08-16 02:44:46 +02:00
|
|
|
thinLTOInternalizeModule(Mod, DefinedGlobals);
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-08-16 02:44:46 +02:00
|
|
|
if (Conf.PostInternalizeModuleHook &&
|
|
|
|
!Conf.PostInternalizeModuleHook(Task, Mod))
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
|
|
|
|
auto ModuleLoader = [&](StringRef Identifier) {
|
2016-08-23 18:53:34 +02:00
|
|
|
assert(Mod.getContext().isODRUniquingDebugTypes() &&
|
2016-09-14 20:48:43 +02:00
|
|
|
"ODR Type uniquing should be enabled on the context");
|
2016-12-14 02:17:59 +01:00
|
|
|
auto I = ModuleMap.find(Identifier);
|
|
|
|
assert(I != ModuleMap.end());
|
|
|
|
return I->second.getLazyModule(Mod.getContext(),
|
2016-12-16 22:25:01 +01:00
|
|
|
/*ShouldLazyLoadMetadata=*/true,
|
|
|
|
/*IsImporting*/ true);
|
2016-08-11 16:58:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FunctionImporter Importer(CombinedIndex, ModuleLoader);
|
2016-11-09 18:49:19 +01:00
|
|
|
if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
|
|
|
|
return Err;
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-08-16 02:44:46 +02:00
|
|
|
if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2017-03-22 19:22:59 +01:00
|
|
|
if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true,
|
|
|
|
/*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex))
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
codegen(Conf, TM.get(), AddStream, Task, Mod);
|
2016-11-11 05:28:40 +01:00
|
|
|
return Error::success();
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|