Skip to content

Commit bc7f32e

Browse files
committed
[DTLTO][LLVM] Generalize the emit files infrastructure
The DTLTO ThinLTO backend will need to customize the file paths used for the summary index shard files. This is so that the names of these files can include a unique ID so that multiple DTLTO links do not overwrite each other's summary index shard files. It also needs to be able to get the import files list for each job in memory rather than writing it to a file. This is to allow the import lists to be included in the JSON used to communicate with the external distributor process.
1 parent 19ef1d1 commit bc7f32e

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

llvm/include/llvm/LTO/LTO.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class InputFile {
199199

200200
using IndexWriteCallback = std::function<void(const std::string &)>;
201201

202+
using ImportsFilesContainer = llvm::SmallVector<std::string>;
203+
202204
/// This class defines the interface to the ThinLTO backend.
203205
class ThinBackendProc {
204206
protected:
@@ -241,8 +243,15 @@ class ThinBackendProc {
241243

242244
// Write sharded indices and (optionally) imports to disk
243245
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
244-
llvm::StringRef ModulePath,
245-
const std::string &NewModulePath) const;
246+
StringRef ModulePath, const std::string &NewModulePath) const;
247+
248+
// Write sharded indices to SummaryPath, (optionally) imports
249+
// IndexPath, and (optionally) record imports in ImportsFiles.
250+
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
251+
StringRef ModulePath, StringRef SummaryPath,
252+
const std::string &NewModulePath,
253+
std::optional<std::reference_wrapper<ImportsFilesContainer>>
254+
ImportsFiles) const;
246255
};
247256

248257
/// This callable defines the behavior of a ThinLTO backend after the thin-link

llvm/include/llvm/Transforms/IPO/FunctionImport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ Error EmitImportsFiles(
421421
StringRef ModulePath, StringRef OutputFilename,
422422
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex);
423423

424+
/// Call \p F passing each of the files module \p ModulePath will import from.
425+
void processImportsFiles(
426+
StringRef ModulePath,
427+
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
428+
function_ref<void(const std::string &)> F);
429+
424430
/// Based on the information recorded in the summaries during global
425431
/// summary-based analysis:
426432
/// 1. Resolve prevailing symbol linkages and constrain visibility (CanAutoHide

llvm/lib/LTO/LTO.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,16 @@ SmallVector<const char *> LTO::getRuntimeLibcallSymbols(const Triple &TT) {
13921392
Error ThinBackendProc::emitFiles(
13931393
const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
13941394
const std::string &NewModulePath) const {
1395+
return emitFiles(ImportList, ModulePath, NewModulePath + ".thinlto.bc",
1396+
NewModulePath,
1397+
/*ImportsFiles=*/std::nullopt);
1398+
}
1399+
1400+
Error ThinBackendProc::emitFiles(
1401+
const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1402+
StringRef SummaryPath, const std::string &NewModulePath,
1403+
std::optional<std::reference_wrapper<ImportsFilesContainer>> ImportsFiles)
1404+
const {
13951405
ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
13961406
GVSummaryPtrSet DeclarationSummaries;
13971407

@@ -1400,10 +1410,9 @@ Error ThinBackendProc::emitFiles(
14001410
ImportList, ModuleToSummariesForIndex,
14011411
DeclarationSummaries);
14021412

1403-
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1404-
sys::fs::OpenFlags::OF_None);
1413+
raw_fd_ostream OS(SummaryPath, EC, sys::fs::OpenFlags::OF_None);
14051414
if (EC)
1406-
return createFileError("cannot open " + NewModulePath + ".thinlto.bc", EC);
1415+
return createFileError("cannot open " + Twine(SummaryPath), EC);
14071416

14081417
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
14091418
&DeclarationSummaries);
@@ -1414,6 +1423,13 @@ Error ThinBackendProc::emitFiles(
14141423
if (ImportFilesError)
14151424
return ImportFilesError;
14161425
}
1426+
1427+
// Optionally, store the imports files.
1428+
if (ImportsFiles)
1429+
processImportsFiles(
1430+
ModulePath, ModuleToSummariesForIndex,
1431+
[&](StringRef M) { ImportsFiles->get().push_back(M.str()); });
1432+
14171433
return Error::success();
14181434
}
14191435

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,13 +1568,23 @@ Error llvm::EmitImportsFiles(
15681568
if (EC)
15691569
return createFileError("cannot open " + OutputFilename,
15701570
errorCodeToError(EC));
1571+
processImportsFiles(ModulePath, ModuleToSummariesForIndex,
1572+
[&](StringRef M) { ImportsOS << M << "\n"; });
1573+
return Error::success();
1574+
}
1575+
1576+
/// Invoke callback \p F on the file paths from which \p ModulePath
1577+
/// will import.
1578+
void llvm::processImportsFiles(
1579+
StringRef ModulePath,
1580+
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
1581+
function_ref<void(const std::string &)> F) {
15711582
for (const auto &ILI : ModuleToSummariesForIndex)
15721583
// The ModuleToSummariesForIndex map includes an entry for the current
15731584
// Module (needed for writing out the index files). We don't want to
15741585
// include it in the imports file, however, so filter it out.
15751586
if (ILI.first != ModulePath)
1576-
ImportsOS << ILI.first << "\n";
1577-
return Error::success();
1587+
F(ILI.first);
15781588
}
15791589

15801590
bool llvm::convertToDeclaration(GlobalValue &GV) {

0 commit comments

Comments
 (0)