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"
@@ -54,23 +53,23 @@ using ProfCorrelatorKind = InstrProfCorrelator::ProfCorrelatorKind;
5453
5554// https://llvm.org/docs/CommandGuide/llvm-profdata.html has documentations
5655// on each subcommand.
57- cl::SubCommand ShowSubcommand (
56+ static cl::SubCommand ShowSubcommand (
5857 " show" ,
5958 " Takes a profile data file and displays the profiles. See detailed "
6059 " documentation in "
6160 " https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-show" );
62- cl::SubCommand OrderSubcommand (
61+ static cl::SubCommand OrderSubcommand (
6362 " order" ,
6463 " Reads temporal profiling traces from a profile and outputs a function "
6564 " order that reduces the number of page faults for those traces. See "
6665 " detailed documentation in "
6766 " https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-order" );
68- cl::SubCommand OverlapSubcommand (
67+ static cl::SubCommand OverlapSubcommand (
6968 " overlap" ,
7069 " Computes and displays the overlap between two profiles. See detailed "
7170 " documentation in "
7271 " https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-overlap" );
73- cl::SubCommand MergeSubcommand (
72+ static cl::SubCommand MergeSubcommand (
7473 " merge" ,
7574 " Takes several profiles and merge them together. See detailed "
7675 " documentation in "
@@ -93,12 +92,11 @@ enum class ShowFormat { Text, Json, Yaml };
9392} // namespace
9493
9594// 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));
95+ static cl::opt<std::string>
96+ OutputFilename (" output" , cl::value_desc(" output" ), cl::init(" -" ),
97+ cl::desc(" Output file" ), cl::sub(ShowSubcommand),
98+ cl::sub(OrderSubcommand), cl::sub(OverlapSubcommand),
99+ cl::sub(MergeSubcommand));
102100// NOTE: cl::alias must not have cl::sub(), since aliased option's cl::sub()
103101// will be used. llvm::cl::alias::done() method asserts this condition.
104102static cl::alias OutputFilenameA (" o" , cl::desc(" Alias for --output" ),
@@ -528,9 +526,9 @@ static void exitWithError(Twine Message, StringRef Whence = "",
528526static void exitWithError (Error E, StringRef Whence = " " ) {
529527 if (E.isA <InstrProfError>()) {
530528 handleAllErrors (std::move (E), [&](const InstrProfError &IPE) {
531- instrprof_error instrError = IPE.get ();
529+ instrprof_error InstrError = IPE.get ();
532530 StringRef Hint = " " ;
533- if (instrError == instrprof_error::unrecognized_format) {
531+ if (InstrError == instrprof_error::unrecognized_format) {
534532 // Hint in case user missed specifying the profile type.
535533 Hint = " Perhaps you forgot to use the --sample or --memory option?" ;
536534 }
@@ -637,7 +635,7 @@ class SymbolRemapper {
637635 return New.empty () ? Name : FunctionId (New);
638636 }
639637};
640- }
638+ } // namespace
641639
642640struct WeightedFile {
643641 std::string Filename;
@@ -827,18 +825,18 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
827825 // Only show hint the first time an error occurs.
828826 auto [ErrCode, Msg] = InstrProfError::take (std::move (E));
829827 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock };
830- bool firstTime = WC->WriterErrorCodes .insert (ErrCode).second ;
828+ bool FirstTime = WC->WriterErrorCodes .insert (ErrCode).second ;
831829 handleMergeWriterError (make_error<InstrProfError>(ErrCode, Msg),
832- Input.Filename , FuncName, firstTime );
830+ Input.Filename , FuncName, FirstTime );
833831 });
834832 }
835833
836834 if (KeepVTableSymbols) {
837- const InstrProfSymtab &symtab = Reader->getSymtab ();
838- const auto &VTableNames = symtab .getVTableNames ();
835+ const InstrProfSymtab &Symtab = Reader->getSymtab ();
836+ const auto &VTableNames = Symtab .getVTableNames ();
839837
840- for (const auto &kv : VTableNames)
841- WC->Writer .addVTableName (kv .getKey ());
838+ for (const auto &KV : VTableNames)
839+ WC->Writer .addVTableName (KV .getKey ());
842840 }
843841
844842 if (Reader->hasTemporalProfile ()) {
@@ -879,8 +877,8 @@ static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
879877 Dst->Writer .mergeRecordsFromWriter (std::move (Src->Writer ), [&](Error E) {
880878 auto [ErrorCode, Msg] = InstrProfError::take (std::move (E));
881879 std::unique_lock<std::mutex> ErrGuard{Dst->ErrLock };
882- bool firstTime = Dst->WriterErrorCodes .insert (ErrorCode).second ;
883- if (firstTime )
880+ bool FirstTime = Dst->WriterErrorCodes .insert (ErrorCode).second ;
881+ if (FirstTime )
884882 warn (toString (make_error<InstrProfError>(ErrorCode, Msg)));
885883 });
886884}
@@ -890,34 +888,32 @@ getFuncName(const StringMap<InstrProfWriter::ProfilingData>::value_type &Val) {
890888 return Val.first ();
891889}
892890
893- static std::string
894- getFuncName (const SampleProfileMap::value_type &Val) {
891+ static std::string getFuncName (const SampleProfileMap::value_type &Val) {
895892 return Val.second .getContext ().toString ();
896893}
897894
898- template <typename T>
899- static void filterFunctions (T &ProfileMap) {
900- bool hasFilter = !FuncNameFilter.empty ();
901- bool hasNegativeFilter = !FuncNameNegativeFilter.empty ();
902- if (!hasFilter && !hasNegativeFilter)
895+ template <typename T> static void filterFunctions (T &ProfileMap) {
896+ bool HasFilter = !FuncNameFilter.empty ();
897+ bool HasNegativeFilter = !FuncNameNegativeFilter.empty ();
898+ if (!HasFilter && !HasNegativeFilter)
903899 return ;
904900
905901 // If filter starts with '?' it is MSVC mangled name, not a regex.
906902 llvm::Regex ProbablyMSVCMangledName (" [?@$_0-9A-Za-z]+" );
907- if (hasFilter && FuncNameFilter[0 ] == ' ?' &&
903+ if (HasFilter && FuncNameFilter[0 ] == ' ?' &&
908904 ProbablyMSVCMangledName.match (FuncNameFilter))
909905 FuncNameFilter = llvm::Regex::escape (FuncNameFilter);
910- if (hasNegativeFilter && FuncNameNegativeFilter[0 ] == ' ?' &&
906+ if (HasNegativeFilter && FuncNameNegativeFilter[0 ] == ' ?' &&
911907 ProbablyMSVCMangledName.match (FuncNameNegativeFilter))
912908 FuncNameNegativeFilter = llvm::Regex::escape (FuncNameNegativeFilter);
913909
914910 size_t Count = ProfileMap.size ();
915911 llvm::Regex Pattern (FuncNameFilter);
916912 llvm::Regex NegativePattern (FuncNameNegativeFilter);
917913 std::string Error;
918- if (hasFilter && !Pattern.isValid (Error))
914+ if (HasFilter && !Pattern.isValid (Error))
919915 exitWithError (Error);
920- if (hasNegativeFilter && !NegativePattern.isValid (Error))
916+ if (HasNegativeFilter && !NegativePattern.isValid (Error))
921917 exitWithError (Error);
922918
923919 // Handle MD5 profile, so it is still able to match using the original name.
@@ -929,10 +925,10 @@ static void filterFunctions(T &ProfileMap) {
929925 auto Tmp = I++;
930926 const auto &FuncName = getFuncName (*Tmp);
931927 // Negative filter has higher precedence than positive filter.
932- if ((hasNegativeFilter &&
928+ if ((HasNegativeFilter &&
933929 (NegativePattern.match (FuncName) ||
934930 (FunctionSamples::UseMD5 && NegativeMD5Name == FuncName))) ||
935- (hasFilter && !(Pattern.match (FuncName) ||
931+ (HasFilter && !(Pattern.match (FuncName) ||
936932 (FunctionSamples::UseMD5 && MD5Name == FuncName))))
937933 ProfileMap.erase (Tmp);
938934 }
@@ -1193,7 +1189,7 @@ adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
11931189 StringMap<StringRef> StaticFuncMap;
11941190 InstrProfSummaryBuilder IPBuilder (ProfileSummaryBuilder::DefaultCutoffs);
11951191
1196- auto checkSampleProfileHasFUnique = [&Reader]() {
1192+ auto CheckSampleProfileHasFUnique = [&Reader]() {
11971193 for (const auto &PD : Reader->getProfiles ()) {
11981194 auto &FContext = PD.second .getContext ();
11991195 if (FContext.toString ().find (FunctionSamples::UniqSuffix) !=
@@ -1204,9 +1200,9 @@ adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
12041200 return false ;
12051201 };
12061202
1207- bool SampleProfileHasFUnique = checkSampleProfileHasFUnique ();
1203+ bool SampleProfileHasFUnique = CheckSampleProfileHasFUnique ();
12081204
1209- auto buildStaticFuncMap = [&StaticFuncMap,
1205+ auto BuildStaticFuncMap = [&StaticFuncMap,
12101206 SampleProfileHasFUnique](const StringRef Name) {
12111207 std::string FilePrefixes[] = {" .cpp" , " cc" , " .c" , " .hpp" , " .h" };
12121208 size_t PrefixPos = StringRef::npos;
@@ -1366,7 +1362,7 @@ adjustInstrProfile(std::unique_ptr<WriterContext> &WC,
13661362 InstrProfRecord *R = &PD.getValue ().begin ()->second ;
13671363 StringRef FullName = PD.getKey ();
13681364 InstrProfileMap[FullName] = InstrProfileEntry (R);
1369- buildStaticFuncMap (FullName);
1365+ BuildStaticFuncMap (FullName);
13701366 }
13711367
13721368 for (auto &PD : Reader->getProfiles ()) {
@@ -1497,8 +1493,8 @@ remapSamples(const sampleprof::FunctionSamples &Samples,
14971493 BodySample.second .getSamples ());
14981494 for (const auto &Target : BodySample.second .getCallTargets ()) {
14991495 Result.addCalledTargetSamples (BodySample.first .LineOffset ,
1500- MaskedDiscriminator,
1501- Remapper (Target. first ), Target.second );
1496+ MaskedDiscriminator, Remapper (Target. first ),
1497+ Target.second );
15021498 }
15031499 }
15041500 for (const auto &CallsiteSamples : Samples.getCallsiteSamples ()) {
@@ -1759,7 +1755,7 @@ static void parseInputFilenamesFile(MemoryBuffer *Buffer,
17591755 if (SanitizedEntry.starts_with (" #" ))
17601756 continue ;
17611757 // If there's no comma, it's an unweighted profile.
1762- else if (!SanitizedEntry.contains (' ,' ))
1758+ if (!SanitizedEntry.contains (' ,' ))
17631759 addWeightedInput (WFV, {std::string (SanitizedEntry), 1 });
17641760 else
17651761 addWeightedInput (WFV, parseWeightedFile (SanitizedEntry));
@@ -2740,10 +2736,11 @@ std::error_code SampleOverlapAggregator::loadProfiles() {
27402736 return std::error_code ();
27412737}
27422738
2743- void overlapSampleProfile (const std::string &BaseFilename,
2744- const std::string &TestFilename,
2745- const OverlapFuncFilters &FuncFilter,
2746- uint64_t SimilarityCutoff, raw_fd_ostream &OS) {
2739+ static void overlapSampleProfile (const std::string &BaseFilename,
2740+ const std::string &TestFilename,
2741+ const OverlapFuncFilters &FuncFilter,
2742+ uint64_t SimilarityCutoff,
2743+ raw_fd_ostream &OS) {
27472744 using namespace sampleprof ;
27482745
27492746 // We use 0.000005 to initialize OverlapAggr.Epsilon because the final metrics
@@ -2883,17 +2880,15 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
28832880 OS << " :ir\n " ;
28842881
28852882 for (const auto &Func : *Reader) {
2886- if (Reader-> isIRLevelProfile () ) {
2883+ if (IsIRInstr ) {
28872884 bool FuncIsCS = NamedInstrProfRecord::hasCSFlagInHash (Func.Hash );
28882885 if (FuncIsCS != ShowCS)
28892886 continue ;
28902887 }
28912888 bool Show = ShowAllFunctions ||
28922889 (!FuncNameFilter.empty () && Func.Name .contains (FuncNameFilter));
28932890
2894- bool doTextFormatDump = (Show && TextFormat);
2895-
2896- if (doTextFormatDump) {
2891+ if (Show && TextFormat) {
28972892 InstrProfSymtab &Symtab = Reader->getSymtab ();
28982893 InstrProfWriter::writeRecordInText (Func.Name , Func.Hash , Func, Symtab,
28992894 OS);
@@ -2931,9 +2926,9 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
29312926 continue ;
29322927 }
29332928
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] ;
2929+ for (const auto &Count : Func.Counts ) {
2930+ FuncMax = std::max (FuncMax, Count );
2931+ FuncSum += Count ;
29372932 }
29382933
29392934 if (FuncMax < ShowValueCutoff) {
@@ -2943,7 +2938,8 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
29432938 << " Sum = " << FuncSum << " )\n " ;
29442939 }
29452940 continue ;
2946- } else if (OnlyListBelow)
2941+ }
2942+ if (OnlyListBelow)
29472943 continue ;
29482944
29492945 if (TopNFunctions) {
@@ -3017,9 +3013,8 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
30173013 if (TextFormat || ShowCovered)
30183014 return 0 ;
30193015 std::unique_ptr<ProfileSummary> PS (Builder.getSummary ());
3020- bool IsIR = Reader->isIRLevelProfile ();
3021- OS << " Instrumentation level: " << (IsIR ? " IR" : " Front-end" );
3022- if (IsIR) {
3016+ OS << " Instrumentation level: " << (IsIRInstr ? " IR" : " Front-end" );
3017+ if (IsIRInstr) {
30233018 OS << " entry_first = " << Reader->instrEntryBBEnabled ();
30243019 OS << " instrument_loop_entries = " << Reader->instrLoopEntriesEnabled ();
30253020 }
@@ -3076,10 +3071,10 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
30763071 auto &Traces = Reader->getTemporalProfTraces ();
30773072 OS << " Temporal Profile Traces (samples=" << Traces.size ()
30783073 << " 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 )
3074+ for (auto [Index, Trace] : llvm::enumerate (Traces) ) {
3075+ OS << " Temporal Profile Trace " << Index << " (weight=" << Trace .Weight
3076+ << " count=" << Trace .FunctionNameRefs .size () << " ):\n " ;
3077+ for (auto &NameRef : Trace .FunctionNameRefs )
30833078 OS << " " << Reader->getSymtab ().getFuncOrVarName (NameRef) << " \n " ;
30843079 }
30853080 }
@@ -3392,7 +3387,8 @@ static int show_main(StringRef ProgName) {
33923387 exitWithErrorCode (EC, OutputFilename);
33933388
33943389 if (ShowAllFunctions && !FuncNameFilter.empty ())
3395- WithColor::warning () << " -function argument ignored: showing all functions\n " ;
3390+ WithColor::warning ()
3391+ << " -function argument ignored: showing all functions\n " ;
33963392
33973393 if (!DebugInfoFilename.empty ())
33983394 return showDebugInfoCorrelation (DebugInfoFilename, SFormat, OS);
0 commit comments