Skip to content

Commit 06a4780

Browse files
committed
[ThinLTO] Add an option to llvm-lto to print some basic statistics for the index
Differential Revision: https://reviews.llvm.org/D24290 llvm-svn: 281537
1 parent aa4b44c commit 06a4780

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

llvm/test/ThinLTO/X86/funcimport.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
; RUN: opt -module-summary %p/Inputs/funcimport.ll -o %t2.bc
44
; RUN: llvm-lto -thinlto-action=thinlink -o %t3.bc %t.bc %t2.bc
55

6+
; RUN: llvm-lto -thinlto-index-stats %t3.bc | FileCheck %s -check-prefix=STATS
7+
; STATS: Index {{.*}} contains 24 nodes (13 functions, 3 alias, 8 globals) and 19 edges (8 refs and 11 calls)
8+
69
; Ensure statics are promoted/renamed correctly from this file (all but
710
; constant variable need promotion).
811
; RUN: llvm-lto -thinlto-action=promote %t.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTSTATIC

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static cl::opt<char>
4242
"(default = '-O2')"),
4343
cl::Prefix, cl::ZeroOrMore, cl::init('2'));
4444

45+
static cl::opt<bool>
46+
IndexStats("thinlto-index-stats",
47+
cl::desc("Print statistic for the index in every input files"),
48+
cl::init(false));
49+
4550
static cl::opt<bool> DisableVerify(
4651
"disable-verify", cl::init(false),
4752
cl::desc("Do not run the verifier during the optimization pipeline"));
@@ -264,6 +269,40 @@ getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
264269
return std::move(*Ret);
265270
}
266271

272+
/// Print some statistics on the index for each input files.
273+
void printIndexStats() {
274+
for (auto &Filename : InputFilenames) {
275+
CurrentActivity = "loading file '" + Filename + "'";
276+
ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
277+
llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler);
278+
error(IndexOrErr, "error " + CurrentActivity);
279+
std::unique_ptr<ModuleSummaryIndex> Index = std::move(IndexOrErr.get());
280+
CurrentActivity = "";
281+
// Skip files without a module summary.
282+
if (!Index)
283+
report_fatal_error(Filename + " does not contain an index");
284+
285+
unsigned Calls = 0, Refs = 0, Functions = 0, Alias = 0, Globals = 0;
286+
for (auto &Summaries : *Index) {
287+
for (auto &Summary : Summaries.second) {
288+
Refs += Summary->refs().size();
289+
if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
290+
Functions++;
291+
Calls += FuncSummary->calls().size();
292+
} else if (isa<AliasSummary>(Summary.get()))
293+
Alias++;
294+
else
295+
Globals++;
296+
}
297+
}
298+
outs() << "Index " << Filename << " contains "
299+
<< (Alias + Globals + Functions) << " nodes (" << Functions
300+
<< " functions, " << Alias << " alias, " << Globals
301+
<< " globals) and " << (Calls + Refs) << " edges (" << Refs
302+
<< " refs and " << Calls << " calls)\n";
303+
}
304+
}
305+
267306
/// \brief List symbols in each IR file.
268307
///
269308
/// The main point here is to provide lit-testable coverage for the LTOModule
@@ -725,6 +764,11 @@ int main(int argc, char **argv) {
725764
return 0;
726765
}
727766

767+
if (IndexStats) {
768+
printIndexStats();
769+
return 0;
770+
}
771+
728772
if (CheckHasObjC) {
729773
for (auto &Filename : InputFilenames) {
730774
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =

0 commit comments

Comments
 (0)