@@ -512,8 +512,8 @@ static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
512
512
}
513
513
514
514
static int showInstrProfile (const std::string &Filename, bool ShowCounts,
515
- bool ShowIndirectCallTargets , bool ShowMemOPSizes ,
516
- bool ShowDetailedSummary,
515
+ uint32_t TopN , bool ShowIndirectCallTargets ,
516
+ bool ShowMemOPSizes, bool ShowDetailedSummary,
517
517
std::vector<uint32_t > DetailedSummaryCutoffs,
518
518
bool ShowAllFunctions,
519
519
const std::string &ShowFunction, bool TextFormat,
@@ -532,6 +532,17 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
532
532
size_t ShownFunctions = 0 ;
533
533
int NumVPKind = IPVK_Last - IPVK_First + 1 ;
534
534
std::vector<ValueSitesStats> VPStats (NumVPKind);
535
+
536
+ auto MinCmp = [](const std::pair<std::string, uint64_t > &v1,
537
+ const std::pair<std::string, uint64_t > &v2) {
538
+ return v1.second > v2.second ;
539
+ };
540
+
541
+ std::priority_queue<std::pair<std::string, uint64_t >,
542
+ std::vector<std::pair<std::string, uint64_t >>,
543
+ decltype (MinCmp)>
544
+ HottestFuncs (MinCmp);
545
+
535
546
for (const auto &Func : *Reader) {
536
547
bool Show =
537
548
ShowAllFunctions || (!ShowFunction.empty () &&
@@ -549,6 +560,20 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
549
560
assert (Func.Counts .size () > 0 && " function missing entry counter" );
550
561
Builder.addRecord (Func);
551
562
563
+ if (TopN) {
564
+ uint64_t FuncMax = 0 ;
565
+ for (size_t I = 0 , E = Func.Counts .size (); I < E; ++I)
566
+ FuncMax = std::max (FuncMax, Func.Counts [I]);
567
+
568
+ if (HottestFuncs.size () == TopN) {
569
+ if (HottestFuncs.top ().second < FuncMax) {
570
+ HottestFuncs.pop ();
571
+ HottestFuncs.emplace (std::make_pair (std::string (Func.Name ), FuncMax));
572
+ }
573
+ } else
574
+ HottestFuncs.emplace (std::make_pair (std::string (Func.Name ), FuncMax));
575
+ }
576
+
552
577
if (Show) {
553
578
554
579
if (!ShownFunctions)
@@ -606,6 +631,18 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
606
631
OS << " Maximum function count: " << PS->getMaxFunctionCount () << " \n " ;
607
632
OS << " Maximum internal block count: " << PS->getMaxInternalCount () << " \n " ;
608
633
634
+ if (TopN) {
635
+ std::vector<std::pair<std::string, uint64_t >> SortedHottestFuncs;
636
+ while (!HottestFuncs.empty ()) {
637
+ SortedHottestFuncs.emplace_back (HottestFuncs.top ());
638
+ HottestFuncs.pop ();
639
+ }
640
+ OS << " Top " << TopN
641
+ << " functions with the largest internal block counts: \n " ;
642
+ for (auto &hotfunc : llvm::reverse (SortedHottestFuncs))
643
+ OS << " " << hotfunc.first << " , max count = " << hotfunc.second << " \n " ;
644
+ }
645
+
609
646
if (ShownFunctions && ShowIndirectCallTargets) {
610
647
OS << " Statistics for indirect call sites profile:\n " ;
611
648
showValueSitesStats (OS, IPVK_IndirectCallTarget,
@@ -689,6 +726,9 @@ static int show_main(int argc, const char *argv[]) {
689
726
cl::desc (" Profile kind:" ), cl::init (instr),
690
727
cl::values (clEnumVal (instr, " Instrumentation profile (default)" ),
691
728
clEnumVal (sample, " Sample profile" )));
729
+ cl::opt<uint32_t > TopNFunctions (
730
+ " topn" , cl::init (0 ),
731
+ cl::desc (" Show the list of functions with the largest internal counts" ));
692
732
693
733
cl::ParseCommandLineOptions (argc, argv, " LLVM profile data summary\n " );
694
734
@@ -706,10 +746,10 @@ static int show_main(int argc, const char *argv[]) {
706
746
std::vector<uint32_t > Cutoffs (DetailedSummaryCutoffs.begin (),
707
747
DetailedSummaryCutoffs.end ());
708
748
if (ProfileKind == instr)
709
- return showInstrProfile (Filename, ShowCounts, ShowIndirectCallTargets ,
710
- ShowMemOPSizes, ShowDetailedSummary ,
711
- DetailedSummaryCutoffs, ShowAllFunctions ,
712
- ShowFunction, TextFormat, OS);
749
+ return showInstrProfile (Filename, ShowCounts, TopNFunctions ,
750
+ ShowIndirectCallTargets, ShowMemOPSizes ,
751
+ ShowDetailedSummary, DetailedSummaryCutoffs ,
752
+ ShowAllFunctions, ShowFunction, TextFormat, OS);
713
753
else
714
754
return showSampleProfile (Filename, ShowCounts, ShowAllFunctions,
715
755
ShowFunction, OS);
0 commit comments