-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ThinLTO][NFC] Refactor ThinBackend #110461
Conversation
@llvm/pr-subscribers-lto Author: Kyungwoo Lee (kyulee-com) ChangesThis is a prep for #90933.
Full diff: https://github.com/llvm/llvm-project/pull/110461.diff 2 Files Affected:
diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h
index 214aa4e1c562dc..fde062ddbf7bc8 100644
--- a/llvm/include/llvm/LTO/LTO.h
+++ b/llvm/include/llvm/LTO/LTO.h
@@ -105,7 +105,41 @@ void updateMemProfAttributes(Module &Mod, const ModuleSummaryIndex &Index);
class LTO;
struct SymbolResolution;
-class ThinBackendProc;
+
+using IndexWriteCallback = std::function<void(const std::string &)>;
+
+/// This class defines the interface to the ThinLTO backend.
+class ThinBackendProc {
+protected:
+ const Config &Conf;
+ ModuleSummaryIndex &CombinedIndex;
+ const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
+ lto::IndexWriteCallback OnWrite;
+ bool ShouldEmitImportsFiles;
+
+public:
+ ThinBackendProc(
+ const Config &Conf, ModuleSummaryIndex &CombinedIndex,
+ const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
+ lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
+ : Conf(Conf), CombinedIndex(CombinedIndex),
+ ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
+ OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
+
+ virtual ~ThinBackendProc() = default;
+ virtual Error start(
+ unsigned Task, BitcodeModule BM,
+ const FunctionImporter::ImportMapTy &ImportList,
+ const FunctionImporter::ExportSetTy &ExportList,
+ const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
+ MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
+ virtual Error wait() = 0;
+ virtual unsigned getThreadCount() = 0;
+
+ // Write sharded indices and (optionally) imports to disk
+ Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
+ llvm::StringRef ModulePath, const std::string &NewModulePath);
+};
/// An input file. This is a symbol table wrapper that only exposes the
/// information that an LTO client should need in order to do symbol resolution.
@@ -197,10 +231,30 @@ class InputFile {
/// A ThinBackend defines what happens after the thin-link phase during ThinLTO.
/// The details of this type definition aren't important; clients can only
/// create a ThinBackend using one of the create*ThinBackend() functions below.
-using ThinBackend = std::function<std::unique_ptr<ThinBackendProc>(
+using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>(
const Config &C, ModuleSummaryIndex &CombinedIndex,
- DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
+ const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache)>;
+struct ThinBackend {
+ ThinBackend(ThinBackendFunction Func, ThreadPoolStrategy Parallelism = {})
+ : Func(std::move(Func)), Parallelism(std::move(Parallelism)) {}
+ ThinBackend() = default;
+
+ std::unique_ptr<ThinBackendProc> operator()(
+ const Config &Conf, ModuleSummaryIndex &CombinedIndex,
+ const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
+ AddStreamFn AddStream, FileCache Cache) {
+ assert(isValid() && "Invalid backend function");
+ return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
+ std::move(AddStream), std::move(Cache));
+ }
+ ThreadPoolStrategy getParallelism() const { return Parallelism; }
+ bool isValid() const { return static_cast<bool>(Func); }
+
+private:
+ ThinBackendFunction Func = nullptr;
+ ThreadPoolStrategy Parallelism;
+};
/// This ThinBackend runs the individual backend jobs in-process.
/// The default value means to use one job per hardware core (not hyper-thread).
@@ -210,7 +264,6 @@ using ThinBackend = std::function<std::unique_ptr<ThinBackendProc>(
/// to the same path as the input module, with suffix ".thinlto.bc"
/// ShouldEmitImportsFiles is true it also writes a list of imported files to a
/// similar path with ".imports" appended instead.
-using IndexWriteCallback = std::function<void(const std::string &)>;
ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
IndexWriteCallback OnWrite = nullptr,
bool ShouldEmitIndexFiles = false,
@@ -275,7 +328,7 @@ class LTO {
/// this constructor.
/// FIXME: We do currently require the DiagHandler field to be set in Conf.
/// Until that is fixed, a Config argument is required.
- LTO(Config Conf, ThinBackend Backend = nullptr,
+ LTO(Config Conf, ThinBackend Backend = {},
unsigned ParallelCodeGenParallelismLevel = 1,
LTOKind LTOMode = LTOK_Default);
~LTO();
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index a88124dacfaefd..404d0dc5365d35 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -578,10 +578,10 @@ LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
CombinedModule->IsNewDbgInfoFormat = UseNewDbgInfoFormat;
}
-LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
- : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
- if (!Backend)
- this->Backend =
+LTO::ThinLTOState::ThinLTOState(ThinBackend BackendParam)
+ : Backend(std::move(BackendParam)), CombinedIndex(/*HaveGVs*/ false) {
+ if (!Backend.isValid())
+ Backend =
createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
}
@@ -1368,64 +1368,6 @@ SmallVector<const char *> LTO::getRuntimeLibcallSymbols(const Triple &TT) {
return LibcallSymbols;
}
-/// This class defines the interface to the ThinLTO backend.
-class lto::ThinBackendProc {
-protected:
- const Config &Conf;
- ModuleSummaryIndex &CombinedIndex;
- const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
- lto::IndexWriteCallback OnWrite;
- bool ShouldEmitImportsFiles;
-
-public:
- ThinBackendProc(
- const Config &Conf, ModuleSummaryIndex &CombinedIndex,
- const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
- lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
- : Conf(Conf), CombinedIndex(CombinedIndex),
- ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
- OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
-
- virtual ~ThinBackendProc() = default;
- virtual Error start(
- unsigned Task, BitcodeModule BM,
- const FunctionImporter::ImportMapTy &ImportList,
- const FunctionImporter::ExportSetTy &ExportList,
- const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
- MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
- virtual Error wait() = 0;
- virtual unsigned getThreadCount() = 0;
-
- // Write sharded indices and (optionally) imports to disk
- Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
- llvm::StringRef ModulePath,
- const std::string &NewModulePath) {
- ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
- GVSummaryPtrSet DeclarationSummaries;
-
- std::error_code EC;
- gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
- ImportList, ModuleToSummariesForIndex,
- DeclarationSummaries);
-
- raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
- sys::fs::OpenFlags::OF_None);
- if (EC)
- return errorCodeToError(EC);
-
- writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
- &DeclarationSummaries);
-
- if (ShouldEmitImportsFiles) {
- EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
- ModuleToSummariesForIndex);
- if (EC)
- return errorCodeToError(EC);
- }
- return Error::success();
- }
-};
-
namespace {
class InProcessThinBackend : public ThinBackendProc {
DefaultThreadPool BackendThreadPool;
@@ -1564,7 +1506,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
lto::IndexWriteCallback OnWrite,
bool ShouldEmitIndexFiles,
bool ShouldEmitImportsFiles) {
- return
+ auto Func =
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache) {
@@ -1573,6 +1515,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
ShouldEmitImportsFiles);
};
+ return ThinBackend(Func, Parallelism);
}
StringLiteral lto::getThinLTODefaultCPU(const Triple &TheTriple) {
@@ -1665,7 +1608,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
std::string OldPrefix, std::string NewPrefix,
std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
- return
+ auto Func =
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
AddStreamFn AddStream, FileCache Cache) {
@@ -1674,6 +1617,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
LinkedObjectsFile, OnWrite);
};
+ return ThinBackend(Func);
}
Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
@@ -1934,3 +1878,31 @@ std::vector<int> lto::generateModulesOrdering(ArrayRef<BitcodeModule *> R) {
});
return ModulesOrdering;
}
+
+Error ThinBackendProc::emitFiles(
+ const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
+ const std::string &NewModulePath) {
+ ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
+ GVSummaryPtrSet DeclarationSummaries;
+
+ std::error_code EC;
+ gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
+ ImportList, ModuleToSummariesForIndex,
+ DeclarationSummaries);
+
+ raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
+ sys::fs::OpenFlags::OF_None);
+ if (EC)
+ return errorCodeToError(EC);
+
+ writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
+ &DeclarationSummaries);
+
+ if (ShouldEmitImportsFiles) {
+ EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
+ ModuleToSummariesForIndex);
+ if (EC)
+ return errorCodeToError(EC);
+ }
+ return Error::success();
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with a comment update
llvm/include/llvm/LTO/LTO.h
Outdated
@@ -197,10 +231,30 @@ class InputFile { | |||
/// A ThinBackend defines what happens after the thin-link phase during ThinLTO. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update this and have a separate comment for the renamed function type alias and the new struct type?
- Change it to a type from a function. - Store the parallelism in the type for the future use.
This feature is enabled by `-codegen-data-thinlto-two-rounds`, which effectively runs the `-codegen-data-generate` and `-codegen-data-use` in two rounds to enable global outlining with ThinLTO. 1. The first round: Run both optimization + codegen with a scratch output. Before running codegen, we serialize the optimized bitcode modules to a temporary path. 2. From the scratch object files, we merge them into the codegen data. 3. The second round: Read the optimized bitcode modules and start the codegen only this time. Using the codegen data, the machine outliner effectively performs the global outlining. Depends on #90934, #110461 and #110463. This is a patch for https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
This feature is enabled by `-codegen-data-thinlto-two-rounds`, which effectively runs the `-codegen-data-generate` and `-codegen-data-use` in two rounds to enable global outlining with ThinLTO. 1. The first round: Run both optimization + codegen with a scratch output. Before running codegen, we serialize the optimized bitcode modules to a temporary path. 2. From the scratch object files, we merge them into the codegen data. 3. The second round: Read the optimized bitcode modules and start the codegen only this time. Using the codegen data, the machine outliner effectively performs the global outlining. Depends on llvm#90934, llvm#110461 and llvm#110463. This is a patch for https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
This is a prep for #90933.
ThinBackend
from a function to a type.ThinBackendProc
is hoisted toLTO.h
fromLTO.cpp
to provide its body forThinBackend
. However,emitFiles()
is still implemented separately inLTO.cpp
, distinct from its parent class.