1616#include " llvm/Debuginfod/HTTPClient.h"
1717#include " llvm/IR/LLVMContext.h"
1818#include " llvm/Object/Binary.h"
19- #include " llvm/ProfileData/DataAccessProf.h"
2019#include " llvm/ProfileData/InstrProfCorrelator.h"
2120#include " llvm/ProfileData/InstrProfReader.h"
2221#include " llvm/ProfileData/InstrProfWriter.h"
4746#include < algorithm>
4847#include < cmath>
4948#include < optional>
50- #include < queue>
5149
5250using namespace llvm ;
5351using ProfCorrelatorKind = InstrProfCorrelator::ProfCorrelatorKind;
5452
5553// https://llvm.org/docs/CommandGuide/llvm-profdata.html has documentations
5654// on each subcommand.
57- cl::SubCommand ShowSubcommand (
55+ static cl::SubCommand ShowSubcommand (
5856 " show" ,
5957 " Takes a profile data file and displays the profiles. See detailed "
6058 " documentation in "
6159 " https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-show" );
62- cl::SubCommand OrderSubcommand (
60+ static cl::SubCommand OrderSubcommand (
6361 " order" ,
6462 " Reads temporal profiling traces from a profile and outputs a function "
6563 " order that reduces the number of page faults for those traces. See "
6664 " detailed documentation in "
6765 " https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-order" );
68- cl::SubCommand OverlapSubcommand (
66+ static cl::SubCommand OverlapSubcommand (
6967 " overlap" ,
7068 " Computes and displays the overlap between two profiles. See detailed "
7169 " documentation in "
7270 " https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-overlap" );
73- cl::SubCommand MergeSubcommand (
71+ static cl::SubCommand MergeSubcommand (
7472 " merge" ,
7573 " Takes several profiles and merge them together. See detailed "
7674 " documentation in "
@@ -93,12 +91,11 @@ enum class ShowFormat { Text, Json, Yaml };
9391} // namespace
9492
9593// Common options.
96- cl::opt<std::string> OutputFilename (" output" , cl::value_desc(" output" ),
97- cl::init(" -" ), cl::desc(" Output file" ),
98- cl::sub(ShowSubcommand),
99- cl::sub(OrderSubcommand),
100- cl::sub(OverlapSubcommand),
101- cl::sub(MergeSubcommand));
94+ static cl::opt<std::string>
95+ OutputFilename (" output" , cl::value_desc(" output" ), cl::init(" -" ),
96+ cl::desc(" Output file" ), cl::sub(ShowSubcommand),
97+ cl::sub(OrderSubcommand), cl::sub(OverlapSubcommand),
98+ cl::sub(MergeSubcommand));
10299// NOTE: cl::alias must not have cl::sub(), since aliased option's cl::sub()
103100// will be used. llvm::cl::alias::done() method asserts this condition.
104101static cl::alias OutputFilenameA (" o" , cl::desc(" Alias for --output" ),
@@ -528,9 +525,9 @@ static void exitWithError(Twine Message, StringRef Whence = "",
528525static void exitWithError (Error E, StringRef Whence = " " ) {
529526 if (E.isA <InstrProfError>()) {
530527 handleAllErrors (std::move (E), [&](const InstrProfError &IPE) {
531- instrprof_error instrError = IPE.get ();
528+ instrprof_error InstrError = IPE.get ();
532529 StringRef Hint = " " ;
533- if (instrError == instrprof_error::unrecognized_format) {
530+ if (InstrError == instrprof_error::unrecognized_format) {
534531 // Hint in case user missed specifying the profile type.
535532 Hint = " Perhaps you forgot to use the --sample or --memory option?" ;
536533 }
@@ -637,7 +634,7 @@ class SymbolRemapper {
637634 return New.empty () ? Name : FunctionId (New);
638635 }
639636};
640- }
637+ } // namespace
641638
642639struct WeightedFile {
643640 std::string Filename;
@@ -827,18 +824,18 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
827824 // Only show hint the first time an error occurs.
828825 auto [ErrCode, Msg] = InstrProfError::take (std::move (E));
829826 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock };
830- bool firstTime = WC->WriterErrorCodes .insert (ErrCode).second ;
827+ bool FirstTime = WC->WriterErrorCodes .insert (ErrCode).second ;
831828 handleMergeWriterError (make_error<InstrProfError>(ErrCode, Msg),
832- Input.Filename , FuncName, firstTime );
829+ Input.Filename , FuncName, FirstTime );
833830 });
834831 }
835832
836833 if (KeepVTableSymbols) {
837- const InstrProfSymtab &symtab = Reader->getSymtab ();
838- const auto &VTableNames = symtab .getVTableNames ();
834+ const InstrProfSymtab &Symtab = Reader->getSymtab ();
835+ const auto &VTableNames = Symtab .getVTableNames ();
839836
840- for (const auto &kv : VTableNames)
841- WC->Writer .addVTableName (kv .getKey ());
837+ for (const auto &KV : VTableNames)
838+ WC->Writer .addVTableName (KV .getKey ());
842839 }
843840
844841 if (Reader->hasTemporalProfile ()) {
@@ -879,8 +876,8 @@ static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
879876 Dst->Writer .mergeRecordsFromWriter (std::move (Src->Writer ), [&](Error E) {
880877 auto [ErrorCode, Msg] = InstrProfError::take (std::move (E));
881878 std::unique_lock<std::mutex> ErrGuard{Dst->ErrLock };
882- bool firstTime = Dst->WriterErrorCodes .insert (ErrorCode).second ;
883- if (firstTime )
879+ bool FirstTime = Dst->WriterErrorCodes .insert (ErrorCode).second ;
880+ if (FirstTime )
884881 warn (toString (make_error<InstrProfError>(ErrorCode, Msg)));
885882 });
886883}
@@ -890,34 +887,32 @@ getFuncName(const StringMap<InstrProfWriter::ProfilingData>::value_type &Val) {
890887 return Val.first ();
891888}
892889
893- static std::string
894- getFuncName (const SampleProfileMap::value_type &Val) {
890+ static std::string getFuncName (const SampleProfileMap::value_type &Val) {
895891 return Val.second .getContext ().toString ();
896892}
897893
898- template <typename T>
899- static void filterFunctions (T &ProfileMap) {
900- bool hasFilter = !FuncNameFilter.empty ();
901- bool hasNegativeFilter = !FuncNameNegativeFilter.empty ();
902- if (!hasFilter && !hasNegativeFilter)
894+ template <typename T> static void filterFunctions (T &ProfileMap) {
895+ bool HasFilter = !FuncNameFilter.empty ();
896+ bool HasNegativeFilter = !FuncNameNegativeFilter.empty ();
897+ if (!HasFilter && !HasNegativeFilter)
903898 return ;
904899
905900 // If filter starts with '?' it is MSVC mangled name, not a regex.
906901 llvm::Regex ProbablyMSVCMangledName (" [?@$_0-9A-Za-z]+" );
907- if (hasFilter && FuncNameFilter[0 ] == ' ?' &&
902+ if (HasFilter && FuncNameFilter[0 ] == ' ?' &&
908903 ProbablyMSVCMangledName.match (FuncNameFilter))
909904 FuncNameFilter = llvm::Regex::escape (FuncNameFilter);
910- if (hasNegativeFilter && FuncNameNegativeFilter[0 ] == ' ?' &&
905+ if (HasNegativeFilter && FuncNameNegativeFilter[0 ] == ' ?' &&
911906 ProbablyMSVCMangledName.match (FuncNameNegativeFilter))
912907 FuncNameNegativeFilter = llvm::Regex::escape (FuncNameNegativeFilter);
913908
914909 size_t Count = ProfileMap.size ();
915910 llvm::Regex Pattern (FuncNameFilter);
916911 llvm::Regex NegativePattern (FuncNameNegativeFilter);
917912 std::string Error;
918- if (hasFilter && !Pattern.isValid (Error))
913+ if (HasFilter && !Pattern.isValid (Error))
919914 exitWithError (Error);
920- if (hasNegativeFilter && !NegativePattern.isValid (Error))
915+ if (HasNegativeFilter && !NegativePattern.isValid (Error))
921916 exitWithError (Error);
922917
923918 // Handle MD5 profile, so it is still able to match using the original name.
@@ -929,10 +924,10 @@ static void filterFunctions(T &ProfileMap) {
929924 auto Tmp = I++;
930925 const auto &FuncName = getFuncName (*Tmp);
931926 // Negative filter has higher precedence than positive filter.
932- if ((hasNegativeFilter &&
927+ if ((HasNegativeFilter &&
933928 (NegativePattern.match (FuncName) ||
934929 (FunctionSamples::UseMD5 && NegativeMD5Name == FuncName))) ||
935- (hasFilter && !(Pattern.match (FuncName) ||
930+ (HasFilter && !(Pattern.match (FuncName) ||
936931 (FunctionSamples::UseMD5 && MD5Name == FuncName))))
937932 ProfileMap.erase (Tmp);
938933 }
@@ -1193,7 +1188,7 @@ adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
11931188 StringMap<StringRef> StaticFuncMap;
11941189 InstrProfSummaryBuilder IPBuilder (ProfileSummaryBuilder::DefaultCutoffs);
11951190
1196- auto checkSampleProfileHasFUnique = [&Reader]() {
1191+ auto CheckSampleProfileHasFUnique = [&Reader]() {
11971192 for (const auto &PD : Reader->getProfiles ()) {
11981193 auto &FContext = PD.second .getContext ();
11991194 if (FContext.toString ().find (FunctionSamples::UniqSuffix) !=
@@ -1204,9 +1199,9 @@ adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
12041199 return false ;
12051200 };
12061201
1207- bool SampleProfileHasFUnique = checkSampleProfileHasFUnique ();
1202+ bool SampleProfileHasFUnique = CheckSampleProfileHasFUnique ();
12081203
1209- auto buildStaticFuncMap = [&StaticFuncMap,
1204+ auto BuildStaticFuncMap = [&StaticFuncMap,
12101205 SampleProfileHasFUnique](const StringRef Name) {
12111206 std::string FilePrefixes[] = {" .cpp" , " cc" , " .c" , " .hpp" , " .h" };
12121207 size_t PrefixPos = StringRef::npos;
@@ -1366,7 +1361,7 @@ adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
13661361 InstrProfRecord *R = &PD.getValue ().begin ()->second ;
13671362 StringRef FullName = PD.getKey ();
13681363 InstrProfileMap[FullName] = InstrProfileEntry (R);
1369- buildStaticFuncMap (FullName);
1364+ BuildStaticFuncMap (FullName);
13701365 }
13711366
13721367 for (auto &PD : Reader->getProfiles ()) {
@@ -1497,8 +1492,8 @@ remapSamples(const sampleprof::FunctionSamples &Samples,
14971492 BodySample.second .getSamples ());
14981493 for (const auto &Target : BodySample.second .getCallTargets ()) {
14991494 Result.addCalledTargetSamples (BodySample.first .LineOffset ,
1500- MaskedDiscriminator,
1501- Remapper (Target. first ), Target.second );
1495+ MaskedDiscriminator, Remapper (Target. first ),
1496+ Target.second );
15021497 }
15031498 }
15041499 for (const auto &CallsiteSamples : Samples.getCallsiteSamples ()) {
@@ -1759,7 +1754,7 @@ static void parseInputFilenamesFile(MemoryBuffer *Buffer,
17591754 if (SanitizedEntry.starts_with (" #" ))
17601755 continue ;
17611756 // If there's no comma, it's an unweighted profile.
1762- else if (!SanitizedEntry.contains (' ,' ))
1757+ if (!SanitizedEntry.contains (' ,' ))
17631758 addWeightedInput (WFV, {std::string (SanitizedEntry), 1 });
17641759 else
17651760 addWeightedInput (WFV, parseWeightedFile (SanitizedEntry));
@@ -2740,10 +2735,11 @@ std::error_code SampleOverlapAggregator::loadProfiles() {
27402735 return std::error_code ();
27412736}
27422737
2743- void overlapSampleProfile (const std::string &BaseFilename,
2744- const std::string &TestFilename,
2745- const OverlapFuncFilters &FuncFilter,
2746- uint64_t SimilarityCutoff, raw_fd_ostream &OS) {
2738+ static void overlapSampleProfile (const std::string &BaseFilename,
2739+ const std::string &TestFilename,
2740+ const OverlapFuncFilters &FuncFilter,
2741+ uint64_t SimilarityCutoff,
2742+ raw_fd_ostream &OS) {
27472743 using namespace sampleprof ;
27482744
27492745 // We use 0.000005 to initialize OverlapAggr.Epsilon because the final metrics
@@ -2883,17 +2879,15 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
28832879 OS << " :ir\n " ;
28842880
28852881 for (const auto &Func : *Reader) {
2886- if (Reader-> isIRLevelProfile () ) {
2882+ if (IsIRInstr ) {
28872883 bool FuncIsCS = NamedInstrProfRecord::hasCSFlagInHash (Func.Hash );
28882884 if (FuncIsCS != ShowCS)
28892885 continue ;
28902886 }
28912887 bool Show = ShowAllFunctions ||
28922888 (!FuncNameFilter.empty () && Func.Name .contains (FuncNameFilter));
28932889
2894- bool doTextFormatDump = (Show && TextFormat);
2895-
2896- if (doTextFormatDump) {
2890+ if (Show && TextFormat) {
28972891 InstrProfSymtab &Symtab = Reader->getSymtab ();
28982892 InstrProfWriter::writeRecordInText (Func.Name , Func.Hash , Func, Symtab,
28992893 OS);
@@ -2931,9 +2925,9 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
29312925 continue ;
29322926 }
29332927
2934- for (size_t I = 0 , E = Func.Counts . size (); I < E; ++I ) {
2935- FuncMax = std::max (FuncMax, Func. Counts [I] );
2936- FuncSum += Func. Counts [I] ;
2928+ for (const auto &Count : Func.Counts ) {
2929+ FuncMax = std::max (FuncMax, Count );
2930+ FuncSum += Count ;
29372931 }
29382932
29392933 if (FuncMax < ShowValueCutoff) {
@@ -2943,7 +2937,8 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
29432937 << " Sum = " << FuncSum << " )\n " ;
29442938 }
29452939 continue ;
2946- } else if (OnlyListBelow)
2940+ }
2941+ if (OnlyListBelow)
29472942 continue ;
29482943
29492944 if (TopNFunctions) {
@@ -3017,9 +3012,8 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
30173012 if (TextFormat || ShowCovered)
30183013 return 0 ;
30193014 std::unique_ptr<ProfileSummary> PS (Builder.getSummary ());
3020- bool IsIR = Reader->isIRLevelProfile ();
3021- OS << " Instrumentation level: " << (IsIR ? " IR" : " Front-end" );
3022- if (IsIR) {
3015+ OS << " Instrumentation level: " << (IsIRInstr ? " IR" : " Front-end" );
3016+ if (IsIRInstr) {
30233017 OS << " entry_first = " << Reader->instrEntryBBEnabled ();
30243018 OS << " instrument_loop_entries = " << Reader->instrLoopEntriesEnabled ();
30253019 }
@@ -3076,10 +3070,10 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
30763070 auto &Traces = Reader->getTemporalProfTraces ();
30773071 OS << " Temporal Profile Traces (samples=" << Traces.size ()
30783072 << " seen=" << Reader->getTemporalProfTraceStreamSize () << " ):\n " ;
3079- for (unsigned i = 0 ; i < Traces. size (); i++ ) {
3080- OS << " Temporal Profile Trace " << i << " (weight=" << Traces[i] .Weight
3081- << " count=" << Traces[i] .FunctionNameRefs .size () << " ):\n " ;
3082- for (auto &NameRef : Traces[i] .FunctionNameRefs )
3073+ for (auto [Index, Trace] : llvm::enumerate (Traces) ) {
3074+ OS << " Temporal Profile Trace " << Index << " (weight=" << Trace .Weight
3075+ << " count=" << Trace .FunctionNameRefs .size () << " ):\n " ;
3076+ for (auto &NameRef : Trace .FunctionNameRefs )
30833077 OS << " " << Reader->getSymtab ().getFuncOrVarName (NameRef) << " \n " ;
30843078 }
30853079 }
@@ -3392,7 +3386,8 @@ static int show_main(StringRef ProgName) {
33923386 exitWithErrorCode (EC, OutputFilename);
33933387
33943388 if (ShowAllFunctions && !FuncNameFilter.empty ())
3395- WithColor::warning () << " -function argument ignored: showing all functions\n " ;
3389+ WithColor::warning ()
3390+ << " -function argument ignored: showing all functions\n " ;
33963391
33973392 if (!DebugInfoFilename.empty ())
33983393 return showDebugInfoCorrelation (DebugInfoFilename, SFormat, OS);
0 commit comments