Skip to content

Commit 7274b74

Browse files
committed
[DTLTO][LLVM] Allow LTO to take an AddBuffer function and use in DTLTO
Replace the use of AddStream in the DTLTO ThinLTO backend to add files to the link with AddBuffer. Unlike the InProcess ThinLTO backend, DTLTO runs the backend compilation jobs by invoking an external process (currently clang). This writes the output object file to disk. Therefore, DTLTO requires a performant way of adding an existing file to the link. Note that the AddBuffer mechanism is also used for adding a file to the link if there is a cache hit.
1 parent c333a8e commit 7274b74

File tree

4 files changed

+51
-45
lines changed

4 files changed

+51
-45
lines changed

llvm/include/llvm/LTO/LTO.h

+17-9
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class ThinBackendProc {
264264
using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>(
265265
const Config &C, ModuleSummaryIndex &CombinedIndex,
266266
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
267-
AddStreamFn AddStream, FileCache Cache)>;
267+
AddStreamFn AddStream, AddBufferFn AddBuffer, FileCache Cache)>;
268268

269269
/// This type defines the behavior following the thin-link phase during ThinLTO.
270270
/// It encapsulates a backend function and a strategy for thread pool
@@ -279,10 +279,10 @@ struct ThinBackend {
279279
std::unique_ptr<ThinBackendProc> operator()(
280280
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
281281
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
282-
AddStreamFn AddStream, FileCache Cache) {
282+
AddStreamFn AddStream, AddBufferFn AddBuffer, FileCache Cache) {
283283
assert(isValid() && "Invalid backend function");
284284
return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
285-
std::move(AddStream), std::move(Cache));
285+
std::move(AddStream), std::move(AddBuffer), std::move(Cache));
286286
}
287287
ThreadPoolStrategy getParallelism() const { return Parallelism; }
288288
bool isValid() const { return static_cast<bool>(Func); }
@@ -308,7 +308,7 @@ ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
308308
/// This ThinBackend generates the index shards and then runs the individual
309309
/// backend jobs via an external process. It takes the same parameters as the
310310
/// InProcessThinBackend, however, these parameters only control the behavior
311-
/// when generating the index files for the modules. Addtionally:
311+
/// when generating the index files for the modules. Additionally:
312312
/// LinkerOutputFile is a string that should identify this LTO invocation in
313313
/// the context of a wider build. It's used for naming to aid the user in
314314
/// identifying activity related to a specific LTO invocation.
@@ -402,15 +402,22 @@ class LTO {
402402
/// full description of tasks see LTOBackend.h.
403403
unsigned getMaxTasks() const;
404404

405-
/// Runs the LTO pipeline. This function calls the supplied AddStream
406-
/// function to add native object files to the link.
405+
/// Runs the LTO pipeline. This function calls the supplied AddStream or
406+
/// AddBuffer function to add native object files to the link depending on
407+
/// whether the files are streamed into memory or written to disk by the
408+
/// backend.
407409
///
408410
/// The Cache parameter is optional. If supplied, it will be used to cache
409411
/// native object files and add them to the link.
410412
///
411-
/// The client will receive at most one callback (via either AddStream or
413+
/// The AddBuffer parameter is only required for DTLTO, currently. It is
414+
/// optional to minimise the impact on current LTO users (DTLTO is not used
415+
/// currently).
416+
///
417+
/// The client will receive at most one callback (via AddStream, AddBuffer or
412418
/// Cache) for each task identifier.
413-
Error run(AddStreamFn AddStream, FileCache Cache = {});
419+
Error run(AddStreamFn AddStream, FileCache Cache = {},
420+
AddBufferFn AddBuffer = nullptr);
414421

415422
/// Static method that returns a list of libcall symbols that can be generated
416423
/// by LTO but might not be visible from bitcode symbol table.
@@ -555,7 +562,8 @@ class LTO {
555562
StringRef Triple);
556563

557564
Error runRegularLTO(AddStreamFn AddStream);
558-
Error runThinLTO(AddStreamFn AddStream, FileCache Cache,
565+
Error runThinLTO(AddStreamFn AddStream, AddBufferFn AddBuffer,
566+
FileCache Cache,
559567
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
560568

561569
Error checkPartiallySplit();

llvm/include/llvm/Support/Caching.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ struct FileCache {
8484
std::string CacheDirectoryPath;
8585
};
8686

87-
/// This type defines the callback to add a pre-existing file (e.g. in a cache).
87+
/// This type defines the callback to add a pre-existing file (e.g. in a cache
88+
/// or created by a backend compilation run as a separate process).
8889
///
8990
/// Buffer callbacks must be thread safe.
9091
using AddBufferFn = std::function<void(unsigned Task, const Twine &ModuleName,

llvm/lib/LTO/LTO.cpp

+31-34
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ Error LTO::checkPartiallySplit() {
11721172
return Error::success();
11731173
}
11741174

1175-
Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
1175+
Error LTO::run(AddStreamFn AddStream, FileCache Cache, AddBufferFn AddBuffer) {
11761176
// Compute "dead" symbols, we don't want to import/export these!
11771177
DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
11781178
DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
@@ -1222,7 +1222,7 @@ Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
12221222
if (!Result)
12231223
// This will reset the GlobalResolutions optional once done with it to
12241224
// reduce peak memory before importing.
1225-
Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1225+
Result = runThinLTO(AddStream, AddBuffer, Cache, GUIDPreservedSymbols);
12261226

12271227
if (StatsFile)
12281228
PrintStatisticsJSON(StatsFile->os());
@@ -1448,7 +1448,6 @@ Error ThinBackendProc::emitFiles(
14481448
namespace {
14491449
class CGThinBackend : public ThinBackendProc {
14501450
protected:
1451-
AddStreamFn AddStream;
14521451
DenseSet<GlobalValue::GUID> CfiFunctionDefs;
14531452
DenseSet<GlobalValue::GUID> CfiFunctionDecls;
14541453
bool ShouldEmitIndexFiles;
@@ -1457,12 +1456,10 @@ class CGThinBackend : public ThinBackendProc {
14571456
CGThinBackend(
14581457
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
14591458
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1460-
AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
1461-
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1462-
ThreadPoolStrategy ThinLTOParallelism)
1459+
lto::IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles,
1460+
bool ShouldEmitImportsFiles, ThreadPoolStrategy ThinLTOParallelism)
14631461
: ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
14641462
OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1465-
AddStream(std::move(AddStream)),
14661463
ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
14671464
for (auto &Name : CombinedIndex.cfiFunctionDefs())
14681465
CfiFunctionDefs.insert(
@@ -1475,6 +1472,7 @@ class CGThinBackend : public ThinBackendProc {
14751472

14761473
class InProcessThinBackend : public CGThinBackend {
14771474
protected:
1475+
AddStreamFn AddStream;
14781476
FileCache Cache;
14791477

14801478
public:
@@ -1484,10 +1482,10 @@ class InProcessThinBackend : public CGThinBackend {
14841482
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
14851483
AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
14861484
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1487-
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1488-
AddStream, OnWrite, ShouldEmitIndexFiles,
1489-
ShouldEmitImportsFiles, ThinLTOParallelism),
1490-
Cache(std::move(Cache)) {}
1485+
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries, OnWrite,
1486+
ShouldEmitIndexFiles, ShouldEmitImportsFiles,
1487+
ThinLTOParallelism),
1488+
AddStream(std::move(AddStream)), Cache(std::move(Cache)) {}
14911489

14921490
virtual Error runThinLTOBackendThread(
14931491
AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
@@ -1755,7 +1753,7 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
17551753
auto Func =
17561754
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
17571755
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1758-
AddStreamFn AddStream, FileCache Cache) {
1756+
AddStreamFn AddStream, AddBufferFn /*AddBuffer*/, FileCache Cache) {
17591757
return std::make_unique<InProcessThinBackend>(
17601758
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
17611759
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
@@ -1877,7 +1875,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
18771875
auto Func =
18781876
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
18791877
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1880-
AddStreamFn AddStream, FileCache Cache) {
1878+
AddStreamFn AddStream, AddBufferFn AddBuffer, FileCache Cache) {
18811879
return std::make_unique<WriteIndexesThinBackend>(
18821880
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
18831881
OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
@@ -1886,7 +1884,8 @@ ThinBackend lto::createWriteIndexesThinBackend(
18861884
return ThinBackend(Func, Parallelism);
18871885
}
18881886

1889-
Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1887+
Error LTO::runThinLTO(AddStreamFn AddStream, AddBufferFn AddBuffer,
1888+
FileCache Cache,
18901889
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
18911890
LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
18921891
ThinLTO.CombinedIndex.releaseTemporaryMemory();
@@ -2094,7 +2093,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
20942093
if (!CodeGenDataThinLTOTwoRounds) {
20952094
std::unique_ptr<ThinBackendProc> BackendProc =
20962095
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2097-
AddStream, Cache);
2096+
AddStream, AddBuffer, Cache);
20982097
return RunBackends(BackendProc.get());
20992098
}
21002099

@@ -2201,6 +2200,8 @@ namespace {
22012200
class OutOfProcessThinBackend : public CGThinBackend {
22022201
using SString = SmallString<128>;
22032202

2203+
AddBufferFn AddBuffer;
2204+
22042205
BumpPtrAllocator Alloc;
22052206
StringSaver Saver{Alloc};
22062207

@@ -2232,15 +2233,16 @@ class OutOfProcessThinBackend : public CGThinBackend {
22322233
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
22332234
ThreadPoolStrategy ThinLTOParallelism,
22342235
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2235-
AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
2236-
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2237-
StringRef LinkerOutputFile, StringRef RemoteOptTool,
2238-
StringRef Distributor, bool SaveTemps)
2239-
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
2240-
AddStream, OnWrite, ShouldEmitIndexFiles,
2241-
ShouldEmitImportsFiles, ThinLTOParallelism),
2242-
LinkerOutputFile(LinkerOutputFile), RemoteOptTool(RemoteOptTool),
2243-
DistributorPath(Distributor), SaveTemps(SaveTemps) {}
2236+
AddStreamFn AddStream, AddBufferFn AddBuffer,
2237+
lto::IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles,
2238+
bool ShouldEmitImportsFiles, StringRef LinkerOutputFile,
2239+
StringRef RemoteOptTool, StringRef Distributor, bool SaveTemps)
2240+
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries, OnWrite,
2241+
ShouldEmitIndexFiles, ShouldEmitImportsFiles,
2242+
ThinLTOParallelism),
2243+
AddBuffer(std::move(AddBuffer)), LinkerOutputFile(LinkerOutputFile),
2244+
RemoteOptTool(RemoteOptTool), DistributorPath(Distributor),
2245+
SaveTemps(SaveTemps) {}
22442246

22452247
virtual void setup(unsigned MaxTasks) override {
22462248
UID = itostr(sys::Process::getProcessId());
@@ -2484,13 +2486,7 @@ class OutOfProcessThinBackend : public CGThinBackend {
24842486
BCError + "cannot open native object file: " +
24852487
Job.NativeObjectPath + ": " + ec.message(),
24862488
inconvertibleErrorCode());
2487-
std::unique_ptr<llvm::MemoryBuffer> umb = std::move(objFileMbOrErr.get());
2488-
Expected<std::unique_ptr<CachedFileStream>> StreamOrErr =
2489-
AddStream(Job.Task, Job.ModuleID);
2490-
if (Error Err = StreamOrErr.takeError())
2491-
report_fatal_error(std::move(Err));
2492-
std::unique_ptr<CachedFileStream> Stream = std::move(*StreamOrErr);
2493-
*Stream->OS << umb->getMemBufferRef().getBuffer();
2489+
AddBuffer(Job.Task, Job.ModuleID, std::move(objFileMbOrErr.get()));
24942490
}
24952491

24962492
return Error::success();
@@ -2506,11 +2502,12 @@ ThinBackend lto::createOutOfProcessThinBackend(
25062502
auto Func =
25072503
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
25082504
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2509-
AddStreamFn AddStream, FileCache /*Cache*/) {
2505+
AddStreamFn AddStream, AddBufferFn AddBuffer, FileCache /*Cache*/) {
25102506
return std::make_unique<OutOfProcessThinBackend>(
25112507
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2512-
AddStream, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles,
2513-
LinkerOutputFile, RemoteOptTool, Distributor, SaveTemps);
2508+
AddStream, AddBuffer, OnWrite, ShouldEmitIndexFiles,
2509+
ShouldEmitImportsFiles, LinkerOutputFile, RemoteOptTool,
2510+
Distributor, SaveTemps);
25142511
};
25152512
return ThinBackend(Func, Parallelism);
25162513
}

llvm/tools/llvm-lto2/llvm-lto2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static int run(int argc, char **argv) {
476476
Cache = check(localCache("ThinLTO", "Thin", CacheDir, AddBuffer),
477477
"failed to create cache");
478478

479-
check(Lto.run(AddStream, Cache), "LTO::run failed");
479+
check(Lto.run(AddStream, Cache, AddBuffer), "LTO::run failed");
480480
return static_cast<int>(HasErrors);
481481
}
482482

0 commit comments

Comments
 (0)