From e402d60c6206c585495123dd327b2a5ab85982b4 Mon Sep 17 00:00:00 2001 From: Kyungwoo Lee Date: Tue, 17 Sep 2024 23:37:51 -0700 Subject: [PATCH] Address comments from NuriAmari --- llvm/lib/CGData/CodeGenData.cpp | 4 ++-- llvm/lib/LTO/LTO.cpp | 33 +++++++++++++++++++++------------ llvm/lib/LTO/LTOBackend.cpp | 2 +- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/llvm/lib/CGData/CodeGenData.cpp b/llvm/lib/CGData/CodeGenData.cpp index 58b92b7262957a..4e21045a67cba6 100644 --- a/llvm/lib/CGData/CodeGenData.cpp +++ b/llvm/lib/CGData/CodeGenData.cpp @@ -245,7 +245,7 @@ void saveModuleForTwoRounds(const Module &TheModule, unsigned Task) { if (EC) report_fatal_error(Twine("Failed to open ") + Path + " to save optimized bitcode: " + EC.message()); - WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true); + WriteBitcodeToFile(TheModule, OS, /*ShouldPreserveUseListOrder=*/true); } std::unique_ptr loadModuleForTwoRounds(BitcodeModule &OrigModule, @@ -259,7 +259,7 @@ std::unique_ptr loadModuleForTwoRounds(BitcodeModule &OrigModule, " to load optimized bitcode: " + EC.message()); std::unique_ptr FileBuffer = std::move(*FileOrError); - auto RestoredModule = llvm::parseBitcodeFile(*FileBuffer, Context); + auto RestoredModule = parseBitcodeFile(*FileBuffer, Context); if (!RestoredModule) report_fatal_error(Twine("Failed to parse optimized bitcode loaded from ") + Path + "\n"); diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 945f8c859365ea..b51b908fb28760 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -1563,11 +1563,14 @@ class InProcessThinBackend : public ThinBackendProc { } }; -/// This Backend will run ThinBackend process but throw away all the output from -/// the codegen. This class facilitates the first codegen round. -class NoOutputThinBackend : public InProcessThinBackend { +/// This backend is utilized in the first round of a two-codegen round process. +/// It first saves optimized bitcode files to disk before the codegen process +/// begins. After codegen, it stores the resulting object files in a scratch +/// buffer. Note the codegen data stored in the scratch buffer will be extracted +/// and merged in the subsequent step. +class FirstRoundThinBackend : public InProcessThinBackend { public: - NoOutputThinBackend( + FirstRoundThinBackend( const Config &Conf, ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &ModuleToDefinedGVSummaries, @@ -1579,25 +1582,31 @@ class NoOutputThinBackend : public InProcessThinBackend { return std::make_unique( std::make_unique((*Allocation)[Task])); }, - FileCache(), nullptr, false, false), + FileCache(), /*OnWrite=*/nullptr, /*ShouldEmitIndexFiles=*/false, + /*ShouldEmitImportsFiles=*/false), Scratch(std::move(Scratch)) {} /// Scratch space for writing output during the codegen. std::unique_ptr>> Scratch; }; -/// This Backend performs codegen on bitcode that was previously saved after -/// going through optimization. This class facilitates the second codegen round. -class OptimizedBitcodeThinBackend : public InProcessThinBackend { +/// This backend operates in the second round of a two-codegen round process. +/// It starts by reading the optimized bitcode files that were saved during the +/// first round. The backend then executes the codegen only to further optimize +/// the code, utilizing the codegen data merged from the first round. Finally, +/// it writes the resulting object files as usual. +class SecondRoundThinBackend : public InProcessThinBackend { public: - OptimizedBitcodeThinBackend( + SecondRoundThinBackend( const Config &Conf, ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const DenseMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream) : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism, ModuleToDefinedGVSummaries, AddStream, FileCache(), - nullptr, false, false) {} + /*OnWrite=*/nullptr, + /*ShouldEmitIndexFiles=*/false, + /*ShouldEmitImportsFiles=*/false) {} virtual Error runThinLTOBackendThread( AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM, @@ -1956,7 +1965,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache, // Create a scratch output to hold intermediate results. auto Outputs = std::make_unique>>(getMaxTasks()); - auto FirstRoundLTO = std::make_unique( + auto FirstRoundLTO = std::make_unique( Conf, ThinLTO.CombinedIndex, llvm::heavyweight_hardware_concurrency(), ModuleToDefinedGVSummaries, std::move(Outputs)); // First round: Run optimization and code generation with a scratch output. @@ -1970,7 +1979,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache, // Second round: Run code generation by reading IRs. std::unique_ptr SecondRoundLTO = - std::make_unique( + std::make_unique( Conf, ThinLTO.CombinedIndex, llvm::heavyweight_hardware_concurrency(), ModuleToDefinedGVSummaries, AddStream); Error E = RunBackends(SecondRoundLTO.get()); diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index d198e8e5102009..cf69f4add53a79 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -611,7 +611,7 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream, // Save the current module before the first codegen round. // Note that the second codegen round runs only `codegen()` without // running `opt()`. We're not reaching here as it's bailed out earlier - // with CodeGenOnly which has been set in `OptimizedBitcodeThinBackend`. + // with `CodeGenOnly` which has been set in `SecondRoundThinBackend`. if (CodeGenDataThinLTOTwoRounds) cgdata::saveModuleForTwoRounds(Mod, Task);