Skip to content

Commit daba9d6

Browse files
committed
[NFC] Refactor ThinBackend
- Change it to a type from a function. - Store the parallelism in the type for the future use.
1 parent 9fc789d commit daba9d6

File tree

2 files changed

+94
-69
lines changed

2 files changed

+94
-69
lines changed

llvm/include/llvm/LTO/LTO.h

+58-5
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,41 @@ void updateMemProfAttributes(Module &Mod, const ModuleSummaryIndex &Index);
105105

106106
class LTO;
107107
struct SymbolResolution;
108-
class ThinBackendProc;
108+
109+
using IndexWriteCallback = std::function<void(const std::string &)>;
110+
111+
/// This class defines the interface to the ThinLTO backend.
112+
class ThinBackendProc {
113+
protected:
114+
const Config &Conf;
115+
ModuleSummaryIndex &CombinedIndex;
116+
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
117+
lto::IndexWriteCallback OnWrite;
118+
bool ShouldEmitImportsFiles;
119+
120+
public:
121+
ThinBackendProc(
122+
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
123+
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
124+
lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
125+
: Conf(Conf), CombinedIndex(CombinedIndex),
126+
ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
127+
OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
128+
129+
virtual ~ThinBackendProc() = default;
130+
virtual Error start(
131+
unsigned Task, BitcodeModule BM,
132+
const FunctionImporter::ImportMapTy &ImportList,
133+
const FunctionImporter::ExportSetTy &ExportList,
134+
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
135+
MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
136+
virtual Error wait() = 0;
137+
virtual unsigned getThreadCount() = 0;
138+
139+
// Write sharded indices and (optionally) imports to disk
140+
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
141+
llvm::StringRef ModulePath, const std::string &NewModulePath);
142+
};
109143

110144
/// An input file. This is a symbol table wrapper that only exposes the
111145
/// information that an LTO client should need in order to do symbol resolution.
@@ -197,10 +231,30 @@ class InputFile {
197231
/// A ThinBackend defines what happens after the thin-link phase during ThinLTO.
198232
/// The details of this type definition aren't important; clients can only
199233
/// create a ThinBackend using one of the create*ThinBackend() functions below.
200-
using ThinBackend = std::function<std::unique_ptr<ThinBackendProc>(
234+
using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>(
201235
const Config &C, ModuleSummaryIndex &CombinedIndex,
202-
DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
236+
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
203237
AddStreamFn AddStream, FileCache Cache)>;
238+
struct ThinBackend {
239+
ThinBackend(ThinBackendFunction Func, ThreadPoolStrategy Parallelism = {})
240+
: Func(std::move(Func)), Parallelism(std::move(Parallelism)) {}
241+
ThinBackend() = default;
242+
243+
std::unique_ptr<ThinBackendProc> operator()(
244+
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
245+
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
246+
AddStreamFn AddStream, FileCache Cache) {
247+
assert(isValid() && "Invalid backend function");
248+
return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
249+
std::move(AddStream), std::move(Cache));
250+
}
251+
ThreadPoolStrategy getParallelism() const { return Parallelism; }
252+
bool isValid() const { return static_cast<bool>(Func); }
253+
254+
private:
255+
ThinBackendFunction Func = nullptr;
256+
ThreadPoolStrategy Parallelism;
257+
};
204258

205259
/// This ThinBackend runs the individual backend jobs in-process.
206260
/// 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>(
210264
/// to the same path as the input module, with suffix ".thinlto.bc"
211265
/// ShouldEmitImportsFiles is true it also writes a list of imported files to a
212266
/// similar path with ".imports" appended instead.
213-
using IndexWriteCallback = std::function<void(const std::string &)>;
214267
ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
215268
IndexWriteCallback OnWrite = nullptr,
216269
bool ShouldEmitIndexFiles = false,
@@ -275,7 +328,7 @@ class LTO {
275328
/// this constructor.
276329
/// FIXME: We do currently require the DiagHandler field to be set in Conf.
277330
/// Until that is fixed, a Config argument is required.
278-
LTO(Config Conf, ThinBackend Backend = nullptr,
331+
LTO(Config Conf, ThinBackend Backend = {},
279332
unsigned ParallelCodeGenParallelismLevel = 1,
280333
LTOKind LTOMode = LTOK_Default);
281334
~LTO();

llvm/lib/LTO/LTO.cpp

+36-64
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,10 @@ LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
578578
CombinedModule->IsNewDbgInfoFormat = UseNewDbgInfoFormat;
579579
}
580580

581-
LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
582-
: Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
583-
if (!Backend)
584-
this->Backend =
581+
LTO::ThinLTOState::ThinLTOState(ThinBackend BackendParam)
582+
: Backend(std::move(BackendParam)), CombinedIndex(/*HaveGVs*/ false) {
583+
if (!Backend.isValid())
584+
Backend =
585585
createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
586586
}
587587

@@ -1368,64 +1368,6 @@ SmallVector<const char *> LTO::getRuntimeLibcallSymbols(const Triple &TT) {
13681368
return LibcallSymbols;
13691369
}
13701370

1371-
/// This class defines the interface to the ThinLTO backend.
1372-
class lto::ThinBackendProc {
1373-
protected:
1374-
const Config &Conf;
1375-
ModuleSummaryIndex &CombinedIndex;
1376-
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
1377-
lto::IndexWriteCallback OnWrite;
1378-
bool ShouldEmitImportsFiles;
1379-
1380-
public:
1381-
ThinBackendProc(
1382-
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1383-
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1384-
lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
1385-
: Conf(Conf), CombinedIndex(CombinedIndex),
1386-
ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
1387-
OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
1388-
1389-
virtual ~ThinBackendProc() = default;
1390-
virtual Error start(
1391-
unsigned Task, BitcodeModule BM,
1392-
const FunctionImporter::ImportMapTy &ImportList,
1393-
const FunctionImporter::ExportSetTy &ExportList,
1394-
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1395-
MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
1396-
virtual Error wait() = 0;
1397-
virtual unsigned getThreadCount() = 0;
1398-
1399-
// Write sharded indices and (optionally) imports to disk
1400-
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
1401-
llvm::StringRef ModulePath,
1402-
const std::string &NewModulePath) {
1403-
ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1404-
GVSummaryPtrSet DeclarationSummaries;
1405-
1406-
std::error_code EC;
1407-
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1408-
ImportList, ModuleToSummariesForIndex,
1409-
DeclarationSummaries);
1410-
1411-
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1412-
sys::fs::OpenFlags::OF_None);
1413-
if (EC)
1414-
return errorCodeToError(EC);
1415-
1416-
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1417-
&DeclarationSummaries);
1418-
1419-
if (ShouldEmitImportsFiles) {
1420-
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1421-
ModuleToSummariesForIndex);
1422-
if (EC)
1423-
return errorCodeToError(EC);
1424-
}
1425-
return Error::success();
1426-
}
1427-
};
1428-
14291371
namespace {
14301372
class InProcessThinBackend : public ThinBackendProc {
14311373
DefaultThreadPool BackendThreadPool;
@@ -1564,7 +1506,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
15641506
lto::IndexWriteCallback OnWrite,
15651507
bool ShouldEmitIndexFiles,
15661508
bool ShouldEmitImportsFiles) {
1567-
return
1509+
auto Func =
15681510
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
15691511
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
15701512
AddStreamFn AddStream, FileCache Cache) {
@@ -1573,6 +1515,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
15731515
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
15741516
ShouldEmitImportsFiles);
15751517
};
1518+
return ThinBackend(Func, Parallelism);
15761519
}
15771520

15781521
StringLiteral lto::getThinLTODefaultCPU(const Triple &TheTriple) {
@@ -1665,7 +1608,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
16651608
std::string OldPrefix, std::string NewPrefix,
16661609
std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
16671610
raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1668-
return
1611+
auto Func =
16691612
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
16701613
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
16711614
AddStreamFn AddStream, FileCache Cache) {
@@ -1674,6 +1617,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
16741617
NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
16751618
LinkedObjectsFile, OnWrite);
16761619
};
1620+
return ThinBackend(Func);
16771621
}
16781622

16791623
Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
@@ -1934,3 +1878,31 @@ std::vector<int> lto::generateModulesOrdering(ArrayRef<BitcodeModule *> R) {
19341878
});
19351879
return ModulesOrdering;
19361880
}
1881+
1882+
Error ThinBackendProc::emitFiles(
1883+
const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1884+
const std::string &NewModulePath) {
1885+
ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1886+
GVSummaryPtrSet DeclarationSummaries;
1887+
1888+
std::error_code EC;
1889+
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1890+
ImportList, ModuleToSummariesForIndex,
1891+
DeclarationSummaries);
1892+
1893+
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1894+
sys::fs::OpenFlags::OF_None);
1895+
if (EC)
1896+
return errorCodeToError(EC);
1897+
1898+
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1899+
&DeclarationSummaries);
1900+
1901+
if (ShouldEmitImportsFiles) {
1902+
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1903+
ModuleToSummariesForIndex);
1904+
if (EC)
1905+
return errorCodeToError(EC);
1906+
}
1907+
return Error::success();
1908+
}

0 commit comments

Comments
 (0)