Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,11 +1389,11 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
// FIXME: Both ExecuteAction and thinBackend set up optimization remarks for
// the same context.
finalizeLLVMOptimizationRemarks(M->getContext());
if (Error E =
thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
/*ModuleMap=*/nullptr, Conf.CodeGenOnly,
/*IRAddStream=*/nullptr, CGOpts.CmdArgs)) {
if (Error E = thinBackend(
Conf, &CI.getVirtualFileSystem(), -1, AddStream, *M, *CombinedIndex,
ImportList, ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
/*ModuleMap=*/nullptr, Conf.CodeGenOnly,
/*IRAddStream=*/nullptr, CGOpts.CmdArgs)) {
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
});
Expand Down
7 changes: 5 additions & 2 deletions llvm/include/llvm/LTO/LTO.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/thread.h"
#include "llvm/Transforms/IPO/FunctionAttrs.h"
#include "llvm/Transforms/IPO/FunctionImport.h"
Expand Down Expand Up @@ -210,6 +211,7 @@ using ImportsFilesContainer = llvm::SmallVector<std::string>;
class ThinBackendProc {
protected:
const Config &Conf;
IntrusiveRefCntPtr<vfs::FileSystem> FS;
ModuleSummaryIndex &CombinedIndex;
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
IndexWriteCallback OnWrite;
Expand All @@ -220,11 +222,12 @@ class ThinBackendProc {

public:
ThinBackendProc(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles,
ThreadPoolStrategy ThinLTOParallelism)
: Conf(Conf), CombinedIndex(CombinedIndex),
: Conf(Conf), FS(std::move(FS)), CombinedIndex(CombinedIndex),
ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles),
BackendThreadPool(ThinLTOParallelism) {}
Expand Down
20 changes: 10 additions & 10 deletions llvm/include/llvm/LTO/LTOBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Target;
namespace lto {

/// Runs middle-end LTO optimizations on \p Mod.
LLVM_ABI bool opt(const Config &Conf, TargetMachine *TM, unsigned Task,
Module &Mod, bool IsThinLTO,
LLVM_ABI bool opt(const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
TargetMachine *TM, unsigned Task, Module &Mod, bool IsThinLTO,
ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary,
const std::vector<uint8_t> &CmdArgs);
Expand All @@ -56,14 +56,14 @@ LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream,
/// the backend will skip optimization and only perform code generation. If
/// \p IRAddStream is not nullptr, it will be called just before code generation
/// to serialize the optimized IR.
LLVM_ABI Error
thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M,
const ModuleSummaryIndex &CombinedIndex,
const FunctionImporter::ImportMapTy &ImportList,
const GVSummaryMapTy &DefinedGlobals,
MapVector<StringRef, BitcodeModule> *ModuleMap, bool CodeGenOnly,
AddStreamFn IRAddStream = nullptr,
const std::vector<uint8_t> &CmdArgs = std::vector<uint8_t>());
LLVM_ABI Error thinBackend(
const Config &C, IntrusiveRefCntPtr<vfs::FileSystem> FS, unsigned Task,
AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex,
const FunctionImporter::ImportMapTy &ImportList,
const GVSummaryMapTy &DefinedGlobals,
MapVector<StringRef, BitcodeModule> *ModuleMap, bool CodeGenOnly,
AddStreamFn IRAddStream = nullptr,
const std::vector<uint8_t> &CmdArgs = std::vector<uint8_t>());

LLVM_ABI Error finalizeOptimizationRemarks(LLVMRemarkFileHandle DiagOutputFile);

Expand Down
84 changes: 48 additions & 36 deletions llvm/lib/LTO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VCSRevision.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/IPO.h"
Expand Down Expand Up @@ -1494,13 +1495,15 @@ class CGThinBackend : public ThinBackendProc {

public:
CGThinBackend(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
ThreadPoolStrategy ThinLTOParallelism)
: ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
: ThinBackendProc(Conf, std::move(FS), CombinedIndex,
ModuleToDefinedGVSummaries, OnWrite,
ShouldEmitImportsFiles, ThinLTOParallelism),
AddStream(std::move(AddStream)),
ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
auto &Defs = CombinedIndex.cfiFunctionDefs();
Expand All @@ -1518,14 +1521,15 @@ class InProcessThinBackend : public CGThinBackend {

public:
InProcessThinBackend(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
ThreadPoolStrategy ThinLTOParallelism,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
AddStream, OnWrite, ShouldEmitIndexFiles,
ShouldEmitImportsFiles, ThinLTOParallelism),
: CGThinBackend(Conf, std::move(FS), CombinedIndex,
ModuleToDefinedGVSummaries, AddStream, OnWrite,
ShouldEmitIndexFiles, ShouldEmitImportsFiles,
ThinLTOParallelism),
Cache(std::move(Cache)) {}

virtual Error runThinLTOBackendThread(
Expand All @@ -1545,7 +1549,7 @@ class InProcessThinBackend : public CGThinBackend {
if (!MOrErr)
return MOrErr.takeError();

return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
return thinBackend(Conf, FS, Task, AddStream, **MOrErr, CombinedIndex,
ImportList, DefinedGlobals, &ModuleMap,
Conf.CodeGenOnly);
};
Expand Down Expand Up @@ -1629,14 +1633,15 @@ class FirstRoundThinBackend : public InProcessThinBackend {

public:
FirstRoundThinBackend(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
ThreadPoolStrategy ThinLTOParallelism,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn CGAddStream, FileCache CGCache, AddStreamFn IRAddStream,
FileCache IRCache)
: InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
ModuleToDefinedGVSummaries, std::move(CGAddStream),
std::move(CGCache), /*OnWrite=*/nullptr,
: InProcessThinBackend(Conf, std::move(FS), CombinedIndex,
ThinLTOParallelism, ModuleToDefinedGVSummaries,
std::move(CGAddStream), std::move(CGCache),
/*OnWrite=*/nullptr,
/*ShouldEmitIndexFiles=*/false,
/*ShouldEmitImportsFiles=*/false),
IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
Expand All @@ -1659,7 +1664,7 @@ class FirstRoundThinBackend : public InProcessThinBackend {
if (!MOrErr)
return MOrErr.takeError();

return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
return thinBackend(Conf, FS, Task, CGAddStream, **MOrErr, CombinedIndex,
ImportList, DefinedGlobals, &ModuleMap,
Conf.CodeGenOnly, IRAddStream);
};
Expand Down Expand Up @@ -1724,15 +1729,15 @@ class SecondRoundThinBackend : public InProcessThinBackend {

public:
SecondRoundThinBackend(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
ThreadPoolStrategy ThinLTOParallelism,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache,
std::unique_ptr<SmallVector<StringRef>> IRFiles,
stable_hash CombinedCGDataHash)
: InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
ModuleToDefinedGVSummaries, std::move(AddStream),
std::move(Cache),
: InProcessThinBackend(Conf, std::move(FS), CombinedIndex,
ThinLTOParallelism, ModuleToDefinedGVSummaries,
std::move(AddStream), std::move(Cache),
/*OnWrite=*/nullptr,
/*ShouldEmitIndexFiles=*/false,
/*ShouldEmitImportsFiles=*/false),
Expand All @@ -1754,8 +1759,8 @@ class SecondRoundThinBackend : public InProcessThinBackend {
std::unique_ptr<Module> LoadedModule =
cgdata::loadModuleForTwoRounds(BM, Task, BackendContext, *IRFiles);

return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
ImportList, DefinedGlobals, &ModuleMap,
return thinBackend(Conf, FS, Task, AddStream, *LoadedModule,
CombinedIndex, ImportList, DefinedGlobals, &ModuleMap,
/*CodeGenOnly=*/true);
};
if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
Expand Down Expand Up @@ -1796,8 +1801,9 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache) {
auto FS = vfs::getRealFileSystem();
return std::make_unique<InProcessThinBackend>(
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
Conf, FS, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
ShouldEmitImportsFiles);
};
Expand Down Expand Up @@ -1845,14 +1851,15 @@ class WriteIndexesThinBackend : public ThinBackendProc {

public:
WriteIndexesThinBackend(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
ThreadPoolStrategy ThinLTOParallelism,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
std::string OldPrefix, std::string NewPrefix,
std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
: ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
: ThinBackendProc(Conf, std::move(FS), CombinedIndex,
ModuleToDefinedGVSummaries, OnWrite,
ShouldEmitImportsFiles, ThinLTOParallelism),
OldPrefix(OldPrefix), NewPrefix(NewPrefix),
NativeObjectPrefix(NativeObjectPrefix),
LinkedObjectsFile(LinkedObjectsFile) {}
Expand Down Expand Up @@ -1917,8 +1924,9 @@ ThinBackend lto::createWriteIndexesThinBackend(
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache) {
auto FS = vfs::getRealFileSystem();
return std::make_unique<WriteIndexesThinBackend>(
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
Conf, FS, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
LinkedObjectsFile, OnWrite);
};
Expand Down Expand Up @@ -2151,12 +2159,14 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
// objects and optimized IRs, using the same cache directory as the original.
cgdata::StreamCacheData CG(MaxTasks, Cache, "CG"), IR(MaxTasks, Cache, "IR");

auto FS = vfs::getRealFileSystem();

// First round: Execute optimization and code generation, outputting to
// temporary scratch objects. Serialize the optimized IRs before initiating
// code generation.
LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
Conf, FS, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
CG.AddStream, CG.Cache, IR.AddStream, IR.Cache);
if (Error E = RunBackends(FirstRoundLTO.get()))
return E;
Expand All @@ -2172,7 +2182,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
// merged data.
LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
Conf, FS, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
AddStream, Cache, IR.getResult(), CombinedHash);
return RunBackends(SecondRoundLTO.get());
}
Expand Down Expand Up @@ -2280,17 +2290,18 @@ class OutOfProcessThinBackend : public CGThinBackend {

public:
OutOfProcessThinBackend(
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
ThreadPoolStrategy ThinLTOParallelism,
const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
StringRef LinkerOutputFile, StringRef Distributor,
ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps)
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
AddStream, OnWrite, ShouldEmitIndexFiles,
ShouldEmitImportsFiles, ThinLTOParallelism),
: CGThinBackend(Conf, std::move(FS), CombinedIndex,
ModuleToDefinedGVSummaries, AddStream, OnWrite,
ShouldEmitIndexFiles, ShouldEmitImportsFiles,
ThinLTOParallelism),
LinkerOutputFile(LinkerOutputFile), DistributorPath(Distributor),
DistributorArgs(DistributorArgs), RemoteCompiler(RemoteCompiler),
RemoteCompilerArgs(RemoteCompilerArgs), SaveTemps(SaveTemps) {}
Expand Down Expand Up @@ -2549,8 +2560,9 @@ ThinBackend lto::createOutOfProcessThinBackend(
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache /*Cache*/) {
auto FS = vfs::getRealFileSystem();
return std::make_unique<OutOfProcessThinBackend>(
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
Conf, FS, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
AddStream, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles,
LinkerOutputFile, Distributor, DistributorArgs, RemoteCompiler,
RemoteCompilerArgs, SaveTemps);
Expand Down
26 changes: 15 additions & 11 deletions llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
return TM;
}

static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
unsigned OptLevel, bool IsThinLTO,
static void runNewPMPasses(const Config &Conf,
IntrusiveRefCntPtr<vfs::FileSystem> FS, Module &Mod,
TargetMachine *TM, unsigned OptLevel, bool IsThinLTO,
ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary) {
auto FS = vfs::getRealFileSystem();
std::optional<PGOOptions> PGOOpt;
if (!Conf.SampleProfile.empty())
PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
Expand Down Expand Up @@ -362,8 +362,9 @@ static bool isEmptyModule(const Module &Mod) {
Mod.getModuleInlineAsm().empty();
}

bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
bool lto::opt(const Config &Conf, IntrusiveRefCntPtr<vfs::FileSystem> FS,
TargetMachine *TM, unsigned Task, Module &Mod, bool IsThinLTO,
ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary,
const std::vector<uint8_t> &CmdArgs) {
llvm::TimeTraceScope timeScope("opt");
Expand Down Expand Up @@ -391,8 +392,8 @@ bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
// regular LTO combined module, with a large combined index from ThinLTO.
if (!isEmptyModule(Mod)) {
// FIXME: Plumb the combined index into the new pass manager.
runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
ImportSummary);
runNewPMPasses(Conf, std::move(FS), Mod, TM, Conf.OptLevel, IsThinLTO,
ExportSummary, ImportSummary);
}
return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
}
Expand Down Expand Up @@ -563,7 +564,8 @@ Error lto::backend(const Config &C, AddStreamFn AddStream,

LLVM_DEBUG(dbgs() << "Running regular LTO\n");
if (!C.CodeGenOnly) {
if (!opt(C, TM.get(), 0, Mod, /*IsThinLTO=*/false,
auto FS = vfs::getRealFileSystem();
if (!opt(C, FS, TM.get(), 0, Mod, /*IsThinLTO=*/false,
/*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
/*CmdArgs*/ std::vector<uint8_t>()))
return Error::success();
Expand Down Expand Up @@ -600,8 +602,10 @@ static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
}
}

Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
Module &Mod, const ModuleSummaryIndex &CombinedIndex,
Error lto::thinBackend(const Config &Conf,
IntrusiveRefCntPtr<vfs::FileSystem> FS, unsigned Task,
AddStreamFn AddStream, Module &Mod,
const ModuleSummaryIndex &CombinedIndex,
const FunctionImporter::ImportMapTy &ImportList,
const GVSummaryMapTy &DefinedGlobals,
MapVector<StringRef, BitcodeModule> *ModuleMap,
Expand Down Expand Up @@ -642,7 +646,7 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
[&](Module &Mod, TargetMachine *TM,
LLVMRemarkFileHandle DiagnosticOutputFile) {
// Perform optimization and code generation for ThinLTO.
if (!opt(Conf, TM, Task, Mod, /*IsThinLTO=*/true,
if (!opt(Conf, FS, TM, Task, Mod, /*IsThinLTO=*/true,
/*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex,
CmdArgs))
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
Expand Down
Loading