@@ -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+
4550static 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