Skip to content

Commit

Permalink
Address comments from NuriAmari
Browse files Browse the repository at this point in the history
  • Loading branch information
kyulee-com committed Sep 18, 2024
1 parent 011d4c1 commit e402d60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions llvm/lib/CGData/CodeGenData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Module> loadModuleForTwoRounds(BitcodeModule &OrigModule,
Expand All @@ -259,7 +259,7 @@ std::unique_ptr<Module> loadModuleForTwoRounds(BitcodeModule &OrigModule,
" to load optimized bitcode: " + EC.message());

std::unique_ptr<MemoryBuffer> 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");
Expand Down
33 changes: 21 additions & 12 deletions llvm/lib/LTO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
Expand All @@ -1579,25 +1582,31 @@ class NoOutputThinBackend : public InProcessThinBackend {
return std::make_unique<CachedFileStream>(
std::make_unique<raw_svector_ostream>((*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<std::vector<llvm::SmallString<0>>> 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<StringRef, GVSummaryMapTy> &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,
Expand Down Expand Up @@ -1956,7 +1965,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
// Create a scratch output to hold intermediate results.
auto Outputs =
std::make_unique<std::vector<llvm::SmallString<0>>>(getMaxTasks());
auto FirstRoundLTO = std::make_unique<NoOutputThinBackend>(
auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
Conf, ThinLTO.CombinedIndex, llvm::heavyweight_hardware_concurrency(),
ModuleToDefinedGVSummaries, std::move(Outputs));
// First round: Run optimization and code generation with a scratch output.
Expand All @@ -1970,7 +1979,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,

// Second round: Run code generation by reading IRs.
std::unique_ptr<ThinBackendProc> SecondRoundLTO =
std::make_unique<OptimizedBitcodeThinBackend>(
std::make_unique<SecondRoundThinBackend>(
Conf, ThinLTO.CombinedIndex, llvm::heavyweight_hardware_concurrency(),
ModuleToDefinedGVSummaries, AddStream);
Error E = RunBackends(SecondRoundLTO.get());
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e402d60

Please sign in to comment.