diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 106363fa83e2b..6a4e3c7952b30 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -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'; }); diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h index 3a9a7f7c25859..7a78095c69813 100644 --- a/llvm/include/llvm/LTO/LTO.h +++ b/llvm/include/llvm/LTO/LTO.h @@ -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" @@ -210,6 +211,7 @@ using ImportsFilesContainer = llvm::SmallVector; class ThinBackendProc { protected: const Config &Conf; + IntrusiveRefCntPtr FS; ModuleSummaryIndex &CombinedIndex; const DenseMap &ModuleToDefinedGVSummaries; IndexWriteCallback OnWrite; @@ -220,11 +222,12 @@ class ThinBackendProc { public: ThinBackendProc( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, const DenseMap &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) {} diff --git a/llvm/include/llvm/LTO/LTOBackend.h b/llvm/include/llvm/LTO/LTOBackend.h index 48ad5aa64f61f..80876fd65bd4a 100644 --- a/llvm/include/llvm/LTO/LTOBackend.h +++ b/llvm/include/llvm/LTO/LTOBackend.h @@ -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 FS, + TargetMachine *TM, unsigned Task, Module &Mod, bool IsThinLTO, ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary, const std::vector &CmdArgs); @@ -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 *ModuleMap, bool CodeGenOnly, - AddStreamFn IRAddStream = nullptr, - const std::vector &CmdArgs = std::vector()); +LLVM_ABI Error thinBackend( + const Config &C, IntrusiveRefCntPtr FS, unsigned Task, + AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex, + const FunctionImporter::ImportMapTy &ImportList, + const GVSummaryMapTy &DefinedGlobals, + MapVector *ModuleMap, bool CodeGenOnly, + AddStreamFn IRAddStream = nullptr, + const std::vector &CmdArgs = std::vector()); LLVM_ABI Error finalizeOptimizationRemarks(LLVMRemarkFileHandle DiagOutputFile); diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 7b252627d73f9..47639cd733dda 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -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" @@ -1494,13 +1495,15 @@ class CGThinBackend : public ThinBackendProc { public: CGThinBackend( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, const DenseMap &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(); @@ -1518,14 +1521,15 @@ class InProcessThinBackend : public CGThinBackend { public: InProcessThinBackend( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, - ThreadPoolStrategy ThinLTOParallelism, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &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( @@ -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); }; @@ -1629,14 +1633,15 @@ class FirstRoundThinBackend : public InProcessThinBackend { public: FirstRoundThinBackend( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, - ThreadPoolStrategy ThinLTOParallelism, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &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)) {} @@ -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); }; @@ -1724,15 +1729,15 @@ class SecondRoundThinBackend : public InProcessThinBackend { public: SecondRoundThinBackend( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, - ThreadPoolStrategy ThinLTOParallelism, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache, std::unique_ptr> 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), @@ -1754,8 +1759,8 @@ class SecondRoundThinBackend : public InProcessThinBackend { std::unique_ptr 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) || @@ -1796,8 +1801,9 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism, [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache) { + auto FS = vfs::getRealFileSystem(); return std::make_unique( - Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, + Conf, FS, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream, Cache, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles); }; @@ -1845,14 +1851,15 @@ class WriteIndexesThinBackend : public ThinBackendProc { public: WriteIndexesThinBackend( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, - ThreadPoolStrategy ThinLTOParallelism, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &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) {} @@ -1917,8 +1924,9 @@ ThinBackend lto::createWriteIndexesThinBackend( [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache) { + auto FS = vfs::getRealFileSystem(); return std::make_unique( - Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, + Conf, FS, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite); }; @@ -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( - 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; @@ -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( - Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, + Conf, FS, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream, Cache, IR.getResult(), CombinedHash); return RunBackends(SecondRoundLTO.get()); } @@ -2280,17 +2290,18 @@ class OutOfProcessThinBackend : public CGThinBackend { public: OutOfProcessThinBackend( - const Config &Conf, ModuleSummaryIndex &CombinedIndex, - ThreadPoolStrategy ThinLTOParallelism, + const Config &Conf, IntrusiveRefCntPtr FS, + ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream, lto::IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles, StringRef LinkerOutputFile, StringRef Distributor, ArrayRef DistributorArgs, StringRef RemoteCompiler, ArrayRef 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) {} @@ -2549,8 +2560,9 @@ ThinBackend lto::createOutOfProcessThinBackend( [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache /*Cache*/) { + auto FS = vfs::getRealFileSystem(); return std::make_unique( - Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, + Conf, FS, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles, LinkerOutputFile, Distributor, DistributorArgs, RemoteCompiler, RemoteCompilerArgs, SaveTemps); diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index c126e8efe82b3..2788ea95e1cd7 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -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 FS, Module &Mod, + TargetMachine *TM, unsigned OptLevel, bool IsThinLTO, ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary) { - auto FS = vfs::getRealFileSystem(); std::optional PGOOpt; if (!Conf.SampleProfile.empty()) PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping, @@ -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 FS, + TargetMachine *TM, unsigned Task, Module &Mod, bool IsThinLTO, + ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary, const std::vector &CmdArgs) { llvm::TimeTraceScope timeScope("opt"); @@ -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); } @@ -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())) return Error::success(); @@ -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 FS, unsigned Task, + AddStreamFn AddStream, Module &Mod, + const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, MapVector *ModuleMap, @@ -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)); diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp index 8aa404da15286..a577269cf02f8 100644 --- a/llvm/lib/LTO/LTOCodeGenerator.cpp +++ b/llvm/lib/LTO/LTOCodeGenerator.cpp @@ -46,6 +46,7 @@ #include "llvm/Support/Process.h" #include "llvm/Support/Signals.h" #include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetOptions.h" #include "llvm/TargetParser/Host.h" @@ -612,7 +613,8 @@ bool LTOCodeGenerator::optimize() { ModuleSummaryIndex CombinedIndex(false); TargetMach = createTargetMachine(); - if (!opt(Config, TargetMach.get(), 0, *MergedModule, /*IsThinLTO=*/false, + auto FS = vfs::getRealFileSystem(); + if (!opt(Config, FS, TargetMach.get(), 0, *MergedModule, /*IsThinLTO=*/false, /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr, /*CmdArgs*/ std::vector())) { emitError("LTO middle-end optimizations failed");