Skip to content

Commit c86af33

Browse files
committedApr 12, 2016
[ThinLTO] Only compute imports for current module in FunctionImport pass
Summary: The function import pass was computing all the imports for all the modules in the index, and only using the imports for the current module. Change this to instead compute only for the given module. This means that the exports list can't be populated, but they weren't being used anyway. Longer term, the linker can collect all the imports and export lists and serialize them out for consumption by the distributed backend processes which use this pass. Reviewers: joker.eph Subscribers: llvm-commits, joker.eph Differential Revision: http://reviews.llvm.org/D18945 llvm-svn: 266125
1 parent 1bb32ac commit c86af33

File tree

4 files changed

+82
-24
lines changed

4 files changed

+82
-24
lines changed
 

‎llvm/include/llvm/IR/ModuleSummaryIndex.h

+6
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ class ModuleSummaryIndex {
434434
/// but if there was only one module or this was the first module we might
435435
/// not invoke mergeFrom.
436436
void removeEmptySummaryEntries();
437+
438+
/// Collect for the given module the list of function it defines
439+
/// (GUID -> Summary).
440+
void collectDefinedFunctionsForModule(
441+
StringRef ModulePath,
442+
std::map<GlobalValue::GUID, FunctionSummary *> &FunctionInfoMap) const;
437443
};
438444

439445
} // End llvm namespace

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

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ void ComputeCrossModuleImport(
7070
const ModuleSummaryIndex &Index,
7171
StringMap<FunctionImporter::ImportMapTy> &ImportLists,
7272
StringMap<FunctionImporter::ExportSetTy> &ExportLists);
73+
74+
/// Compute all the imports for the given module using the Index.
75+
///
76+
/// \p ImportList will be populated with a map that can be passed to
77+
/// FunctionImporter::importFunctions() above (see description there).
78+
void ComputeCrossModuleImportForModule(
79+
StringRef ModulePath, const ModuleSummaryIndex &Index,
80+
FunctionImporter::ImportMapTy &ImportList);
7381
}
7482

7583
#endif // LLVM_FUNCTIONIMPORT_H

‎llvm/lib/IR/ModuleSummaryIndex.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ void ModuleSummaryIndex::removeEmptySummaryEntries() {
6969
}
7070
}
7171

72+
// Collect for the given module the list of function it defines
73+
// (GUID -> Summary).
74+
void ModuleSummaryIndex::collectDefinedFunctionsForModule(
75+
StringRef ModulePath,
76+
std::map<GlobalValue::GUID, FunctionSummary *> &FunctionInfoMap) const {
77+
for (auto &GlobalList : *this) {
78+
auto GUID = GlobalList.first;
79+
for (auto &GlobInfo : GlobalList.second) {
80+
auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobInfo->summary());
81+
if (!Summary)
82+
// Ignore global variable, focus on functions
83+
continue;
84+
// Ignore summaries from other modules.
85+
if (Summary->modulePath() != ModulePath)
86+
continue;
87+
FunctionInfoMap[GUID] = Summary;
88+
}
89+
}
90+
}
91+
7292
GlobalValueInfo *
7393
ModuleSummaryIndex::getGlobalValueInfo(uint64_t ValueGUID,
7494
bool PerModuleIndex) const {

‎llvm/lib/Transforms/IPO/FunctionImport.cpp

+48-24
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static void computeImportForFunction(
143143
const std::map<GlobalValue::GUID, FunctionSummary *> &DefinedFunctions,
144144
SmallVectorImpl<EdgeInfo> &Worklist,
145145
FunctionImporter::ImportMapTy &ImportsForModule,
146-
StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
146+
StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
147147
for (auto &Edge : Summary.calls()) {
148148
auto GUID = Edge.first.getGUID();
149149
DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
@@ -176,19 +176,21 @@ static void computeImportForFunction(
176176

177177
// Make exports in the source module.
178178
auto ExportModulePath = CalleeSummary->modulePath();
179-
auto ExportList = ExportLists[ExportModulePath];
180-
ExportList.insert(GUID);
181-
// Mark all functions and globals referenced by this function as exported to
182-
// the outside if they are defined in the same source module.
183-
for (auto &Edge : CalleeSummary->calls()) {
184-
auto CalleeGUID = Edge.first.getGUID();
185-
if (isGlobalExported(Index, ExportModulePath, CalleeGUID))
186-
ExportList.insert(CalleeGUID);
187-
}
188-
for (auto &Ref : CalleeSummary->refs()) {
189-
auto GUID = Ref.getGUID();
190-
if (isGlobalExported(Index, ExportModulePath, GUID))
191-
ExportList.insert(GUID);
179+
if (ExportLists) {
180+
auto ExportList = (*ExportLists)[ExportModulePath];
181+
ExportList.insert(GUID);
182+
// Mark all functions and globals referenced by this function as exported
183+
// to the outside if they are defined in the same source module.
184+
for (auto &Edge : CalleeSummary->calls()) {
185+
auto CalleeGUID = Edge.first.getGUID();
186+
if (isGlobalExported(Index, ExportModulePath, CalleeGUID))
187+
ExportList.insert(CalleeGUID);
188+
}
189+
for (auto &Ref : CalleeSummary->refs()) {
190+
auto GUID = Ref.getGUID();
191+
if (isGlobalExported(Index, ExportModulePath, GUID))
192+
ExportList.insert(GUID);
193+
}
192194
}
193195

194196
// Insert the newly imported function to the worklist.
@@ -203,7 +205,7 @@ static void ComputeImportForModule(
203205
const std::map<GlobalValue::GUID, FunctionSummary *> &DefinedFunctions,
204206
const ModuleSummaryIndex &Index,
205207
FunctionImporter::ImportMapTy &ImportsForModule,
206-
StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
208+
StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
207209
// Worklist contains the list of function imported in this module, for which
208210
// we will analyse the callees and may import further down the callgraph.
209211
SmallVector<EdgeInfo, 128> Worklist;
@@ -234,7 +236,7 @@ static void ComputeImportForModule(
234236

235237
} // anonymous namespace
236238

237-
/// Compute all the import and export for every module in the Index.
239+
/// Compute all the import and export for every module using the Index.
238240
void llvm::ComputeCrossModuleImport(
239241
const ModuleSummaryIndex &Index,
240242
StringMap<FunctionImporter::ImportMapTy> &ImportLists,
@@ -265,7 +267,7 @@ void llvm::ComputeCrossModuleImport(
265267
DEBUG(dbgs() << "Computing import for Module '" << DefinedFunctions.first()
266268
<< "'\n");
267269
ComputeImportForModule(DefinedFunctions.second, Index, ImportsForModule,
268-
ExportLists);
270+
&ExportLists);
269271
}
270272

271273
#ifndef NDEBUG
@@ -286,6 +288,31 @@ void llvm::ComputeCrossModuleImport(
286288
#endif
287289
}
288290

291+
/// Compute all the imports for the given module in the Index.
292+
void llvm::ComputeCrossModuleImportForModule(
293+
StringRef ModulePath, const ModuleSummaryIndex &Index,
294+
FunctionImporter::ImportMapTy &ImportList) {
295+
296+
// Collect the list of functions this module defines.
297+
// GUID -> Summary
298+
std::map<GlobalValue::GUID, FunctionSummary *> FunctionInfoMap;
299+
Index.collectDefinedFunctionsForModule(ModulePath, FunctionInfoMap);
300+
301+
// Compute the import list for this module.
302+
DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
303+
ComputeImportForModule(FunctionInfoMap, Index, ImportList);
304+
305+
#ifndef NDEBUG
306+
DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
307+
<< ImportList.size() << " modules.\n");
308+
for (auto &Src : ImportList) {
309+
auto SrcModName = Src.first();
310+
DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
311+
<< SrcModName << "\n");
312+
}
313+
#endif
314+
}
315+
289316
// Automatically import functions in Module \p DestModule based on the summaries
290317
// index.
291318
//
@@ -463,13 +490,10 @@ class FunctionImportPass : public ModulePass {
463490
Index = IndexPtr.get();
464491
}
465492

466-
// First step is collecting the import/export lists
467-
// The export list is not used yet, but could limit the amount of renaming
468-
// performed in renameModuleForThinLTO()
469-
StringMap<FunctionImporter::ImportMapTy> ImportLists;
470-
StringMap<FunctionImporter::ExportSetTy> ExportLists;
471-
ComputeCrossModuleImport(*Index, ImportLists, ExportLists);
472-
auto &ImportList = ImportLists[M.getModuleIdentifier()];
493+
// First step is collecting the import list.
494+
FunctionImporter::ImportMapTy ImportList;
495+
ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
496+
ImportList);
473497

474498
// Next we need to promote to global scope and rename any local values that
475499
// are potentially exported to other modules.

0 commit comments

Comments
 (0)
Please sign in to comment.