Skip to content

Commit adc6c58

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 3cda5ce commit adc6c58

File tree

2 files changed

+94
-69
lines changed

2 files changed

+94
-69
lines changed

llvm/include/llvm/LTO/LTO.h

Lines changed: 58 additions & 5 deletions
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

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
581581
CombinedModule->IsNewDbgInfoFormat = UseNewDbgInfoFormat;
582582
}
583583

584-
LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
585-
: Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
586-
if (!Backend)
587-
this->Backend =
584+
LTO::ThinLTOState::ThinLTOState(ThinBackend BackendParam)
585+
: Backend(std::move(BackendParam)), CombinedIndex(/*HaveGVs*/ false) {
586+
if (!Backend.isValid())
587+
Backend =
588588
createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
589589
}
590590

@@ -1371,64 +1371,6 @@ SmallVector<const char *> LTO::getRuntimeLibcallSymbols(const Triple &TT) {
13711371
return LibcallSymbols;
13721372
}
13731373

1374-
/// This class defines the interface to the ThinLTO backend.
1375-
class lto::ThinBackendProc {
1376-
protected:
1377-
const Config &Conf;
1378-
ModuleSummaryIndex &CombinedIndex;
1379-
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
1380-
lto::IndexWriteCallback OnWrite;
1381-
bool ShouldEmitImportsFiles;
1382-
1383-
public:
1384-
ThinBackendProc(
1385-
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1386-
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1387-
lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
1388-
: Conf(Conf), CombinedIndex(CombinedIndex),
1389-
ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
1390-
OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
1391-
1392-
virtual ~ThinBackendProc() = default;
1393-
virtual Error start(
1394-
unsigned Task, BitcodeModule BM,
1395-
const FunctionImporter::ImportMapTy &ImportList,
1396-
const FunctionImporter::ExportSetTy &ExportList,
1397-
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1398-
MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
1399-
virtual Error wait() = 0;
1400-
virtual unsigned getThreadCount() = 0;
1401-
1402-
// Write sharded indices and (optionally) imports to disk
1403-
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
1404-
llvm::StringRef ModulePath,
1405-
const std::string &NewModulePath) {
1406-
ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1407-
GVSummaryPtrSet DeclarationSummaries;
1408-
1409-
std::error_code EC;
1410-
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1411-
ImportList, ModuleToSummariesForIndex,
1412-
DeclarationSummaries);
1413-
1414-
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1415-
sys::fs::OpenFlags::OF_None);
1416-
if (EC)
1417-
return errorCodeToError(EC);
1418-
1419-
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1420-
&DeclarationSummaries);
1421-
1422-
if (ShouldEmitImportsFiles) {
1423-
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1424-
ModuleToSummariesForIndex);
1425-
if (EC)
1426-
return errorCodeToError(EC);
1427-
}
1428-
return Error::success();
1429-
}
1430-
};
1431-
14321374
namespace {
14331375
class InProcessThinBackend : public ThinBackendProc {
14341376
DefaultThreadPool BackendThreadPool;
@@ -1631,7 +1573,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
16311573
lto::IndexWriteCallback OnWrite,
16321574
bool ShouldEmitIndexFiles,
16331575
bool ShouldEmitImportsFiles) {
1634-
return
1576+
auto Func =
16351577
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
16361578
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
16371579
AddStreamFn AddStream, FileCache Cache) {
@@ -1640,6 +1582,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
16401582
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
16411583
ShouldEmitImportsFiles);
16421584
};
1585+
return ThinBackend(Func, Parallelism);
16431586
}
16441587

16451588
StringLiteral lto::getThinLTODefaultCPU(const Triple &TheTriple) {
@@ -1732,7 +1675,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
17321675
std::string OldPrefix, std::string NewPrefix,
17331676
std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
17341677
raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1735-
return
1678+
auto Func =
17361679
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
17371680
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
17381681
AddStreamFn AddStream, FileCache Cache) {
@@ -1741,6 +1684,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
17411684
NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
17421685
LinkedObjectsFile, OnWrite);
17431686
};
1687+
return ThinBackend(Func);
17441688
}
17451689

17461690
Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
@@ -2041,3 +1985,31 @@ std::vector<int> lto::generateModulesOrdering(ArrayRef<BitcodeModule *> R) {
20411985
});
20421986
return ModulesOrdering;
20431987
}
1988+
1989+
Error ThinBackendProc::emitFiles(
1990+
const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1991+
const std::string &NewModulePath) {
1992+
ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1993+
GVSummaryPtrSet DeclarationSummaries;
1994+
1995+
std::error_code EC;
1996+
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1997+
ImportList, ModuleToSummariesForIndex,
1998+
DeclarationSummaries);
1999+
2000+
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
2001+
sys::fs::OpenFlags::OF_None);
2002+
if (EC)
2003+
return errorCodeToError(EC);
2004+
2005+
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
2006+
&DeclarationSummaries);
2007+
2008+
if (ShouldEmitImportsFiles) {
2009+
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
2010+
ModuleToSummariesForIndex);
2011+
if (EC)
2012+
return errorCodeToError(EC);
2013+
}
2014+
return Error::success();
2015+
}

0 commit comments

Comments
 (0)