xref: /openbsd-src/gnu/llvm/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //=-- ProfilesummaryBuilder.cpp - Profile summary computation ---------------=//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file contains support for computing profile summary data.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
13*d415bd75Srobert #include "llvm/IR/ProfileSummary.h"
1409467b48Spatrick #include "llvm/ProfileData/InstrProf.h"
1509467b48Spatrick #include "llvm/ProfileData/ProfileCommon.h"
1609467b48Spatrick #include "llvm/ProfileData/SampleProf.h"
1773471bf0Spatrick #include "llvm/Support/CommandLine.h"
1809467b48Spatrick 
1909467b48Spatrick using namespace llvm;
2009467b48Spatrick 
21*d415bd75Srobert namespace llvm {
2273471bf0Spatrick cl::opt<bool> UseContextLessSummary(
23*d415bd75Srobert     "profile-summary-contextless", cl::Hidden,
2473471bf0Spatrick     cl::desc("Merge context profiles before calculating thresholds."));
2573471bf0Spatrick 
2673471bf0Spatrick // The following two parameters determine the threshold for a count to be
2773471bf0Spatrick // considered hot/cold. These two parameters are percentile values (multiplied
2873471bf0Spatrick // by 10000). If the counts are sorted in descending order, the minimum count to
2973471bf0Spatrick // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
3073471bf0Spatrick // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
3173471bf0Spatrick // threshold for determining cold count (everything <= this threshold is
3273471bf0Spatrick // considered cold).
3373471bf0Spatrick cl::opt<int> ProfileSummaryCutoffHot(
34*d415bd75Srobert     "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000),
3573471bf0Spatrick     cl::desc("A count is hot if it exceeds the minimum count to"
3673471bf0Spatrick              " reach this percentile of total counts."));
3773471bf0Spatrick 
3873471bf0Spatrick cl::opt<int> ProfileSummaryCutoffCold(
39*d415bd75Srobert     "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999),
4073471bf0Spatrick     cl::desc("A count is cold if it is below the minimum count"
4173471bf0Spatrick              " to reach this percentile of total counts."));
4273471bf0Spatrick 
4373471bf0Spatrick cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
4473471bf0Spatrick     "profile-summary-huge-working-set-size-threshold", cl::Hidden,
45*d415bd75Srobert     cl::init(15000),
4673471bf0Spatrick     cl::desc("The code working set size is considered huge if the number of"
4773471bf0Spatrick              " blocks required to reach the -profile-summary-cutoff-hot"
4873471bf0Spatrick              " percentile exceeds this count."));
4973471bf0Spatrick 
5073471bf0Spatrick cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold(
5173471bf0Spatrick     "profile-summary-large-working-set-size-threshold", cl::Hidden,
52*d415bd75Srobert     cl::init(12500),
5373471bf0Spatrick     cl::desc("The code working set size is considered large if the number of"
5473471bf0Spatrick              " blocks required to reach the -profile-summary-cutoff-hot"
5573471bf0Spatrick              " percentile exceeds this count."));
5673471bf0Spatrick 
5773471bf0Spatrick // The next two options override the counts derived from summary computation and
5873471bf0Spatrick // are useful for debugging purposes.
59*d415bd75Srobert cl::opt<uint64_t> ProfileSummaryHotCount(
60*d415bd75Srobert     "profile-summary-hot-count", cl::ReallyHidden,
6173471bf0Spatrick     cl::desc("A fixed hot count that overrides the count derived from"
6273471bf0Spatrick              " profile-summary-cutoff-hot"));
6373471bf0Spatrick 
64*d415bd75Srobert cl::opt<uint64_t> ProfileSummaryColdCount(
65*d415bd75Srobert     "profile-summary-cold-count", cl::ReallyHidden,
6673471bf0Spatrick     cl::desc("A fixed cold count that overrides the count derived from"
6773471bf0Spatrick              " profile-summary-cutoff-cold"));
68*d415bd75Srobert } // namespace llvm
6973471bf0Spatrick 
7009467b48Spatrick // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
7109467b48Spatrick // (which is 1000000) is a desired percentile of total counts.
7209467b48Spatrick static const uint32_t DefaultCutoffsData[] = {
7309467b48Spatrick     10000,  /*  1% */
7409467b48Spatrick     100000, /* 10% */
7509467b48Spatrick     200000, 300000, 400000, 500000, 600000, 700000, 800000,
7609467b48Spatrick     900000, 950000, 990000, 999000, 999900, 999990, 999999};
7709467b48Spatrick const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
7809467b48Spatrick     DefaultCutoffsData;
7909467b48Spatrick 
80097a140dSpatrick const ProfileSummaryEntry &
getEntryForPercentile(const SummaryEntryVector & DS,uint64_t Percentile)81*d415bd75Srobert ProfileSummaryBuilder::getEntryForPercentile(const SummaryEntryVector &DS,
82097a140dSpatrick                                              uint64_t Percentile) {
83097a140dSpatrick   auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
84097a140dSpatrick     return Entry.Cutoff < Percentile;
85097a140dSpatrick   });
86097a140dSpatrick   // The required percentile has to be <= one of the percentiles in the
87097a140dSpatrick   // detailed summary.
88097a140dSpatrick   if (It == DS.end())
89097a140dSpatrick     report_fatal_error("Desired percentile exceeds the maximum cutoff");
90097a140dSpatrick   return *It;
91097a140dSpatrick }
92097a140dSpatrick 
addRecord(const InstrProfRecord & R)9309467b48Spatrick void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
9409467b48Spatrick   // The first counter is not necessarily an entry count for IR
9509467b48Spatrick   // instrumentation profiles.
9609467b48Spatrick   // Eventually MaxFunctionCount will become obsolete and this can be
9709467b48Spatrick   // removed.
98*d415bd75Srobert 
99*d415bd75Srobert   if (R.getCountPseudoKind() != InstrProfRecord::NotPseudo)
100*d415bd75Srobert     return;
101*d415bd75Srobert 
10209467b48Spatrick   addEntryCount(R.Counts[0]);
10309467b48Spatrick   for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
10409467b48Spatrick     addInternalCount(R.Counts[I]);
10509467b48Spatrick }
10609467b48Spatrick 
10709467b48Spatrick // To compute the detailed summary, we consider each line containing samples as
10809467b48Spatrick // equivalent to a block with a count in the instrumented profile.
addRecord(const sampleprof::FunctionSamples & FS,bool isCallsiteSample)10909467b48Spatrick void SampleProfileSummaryBuilder::addRecord(
11009467b48Spatrick     const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
11109467b48Spatrick   if (!isCallsiteSample) {
11209467b48Spatrick     NumFunctions++;
11309467b48Spatrick     if (FS.getHeadSamples() > MaxFunctionCount)
11409467b48Spatrick       MaxFunctionCount = FS.getHeadSamples();
115*d415bd75Srobert   } else if (FS.getContext().hasAttribute(
116*d415bd75Srobert                  sampleprof::ContextDuplicatedIntoBase)) {
117*d415bd75Srobert     // Do not recount callee samples if they are already merged into their base
118*d415bd75Srobert     // profiles. This can happen to CS nested profile.
119*d415bd75Srobert     return;
12009467b48Spatrick   }
121*d415bd75Srobert 
12273471bf0Spatrick   for (const auto &I : FS.getBodySamples()) {
12373471bf0Spatrick     uint64_t Count = I.second.getSamples();
12473471bf0Spatrick       addCount(Count);
12573471bf0Spatrick   }
12609467b48Spatrick   for (const auto &I : FS.getCallsiteSamples())
12709467b48Spatrick     for (const auto &CS : I.second)
12809467b48Spatrick       addRecord(CS.second, true);
12909467b48Spatrick }
13009467b48Spatrick 
13109467b48Spatrick // The argument to this method is a vector of cutoff percentages and the return
13209467b48Spatrick // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
computeDetailedSummary()13309467b48Spatrick void ProfileSummaryBuilder::computeDetailedSummary() {
13409467b48Spatrick   if (DetailedSummaryCutoffs.empty())
13509467b48Spatrick     return;
13609467b48Spatrick   llvm::sort(DetailedSummaryCutoffs);
13709467b48Spatrick   auto Iter = CountFrequencies.begin();
13809467b48Spatrick   const auto End = CountFrequencies.end();
13909467b48Spatrick 
14009467b48Spatrick   uint32_t CountsSeen = 0;
14109467b48Spatrick   uint64_t CurrSum = 0, Count = 0;
14209467b48Spatrick 
14309467b48Spatrick   for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
14409467b48Spatrick     assert(Cutoff <= 999999);
14509467b48Spatrick     APInt Temp(128, TotalCount);
14609467b48Spatrick     APInt N(128, Cutoff);
14709467b48Spatrick     APInt D(128, ProfileSummary::Scale);
14809467b48Spatrick     Temp *= N;
14909467b48Spatrick     Temp = Temp.sdiv(D);
15009467b48Spatrick     uint64_t DesiredCount = Temp.getZExtValue();
15109467b48Spatrick     assert(DesiredCount <= TotalCount);
15209467b48Spatrick     while (CurrSum < DesiredCount && Iter != End) {
15309467b48Spatrick       Count = Iter->first;
15409467b48Spatrick       uint32_t Freq = Iter->second;
15509467b48Spatrick       CurrSum += (Count * Freq);
15609467b48Spatrick       CountsSeen += Freq;
15709467b48Spatrick       Iter++;
15809467b48Spatrick     }
15909467b48Spatrick     assert(CurrSum >= DesiredCount);
16009467b48Spatrick     ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
16109467b48Spatrick     DetailedSummary.push_back(PSE);
16209467b48Spatrick   }
16309467b48Spatrick }
16409467b48Spatrick 
165*d415bd75Srobert uint64_t
getHotCountThreshold(const SummaryEntryVector & DS)166*d415bd75Srobert ProfileSummaryBuilder::getHotCountThreshold(const SummaryEntryVector &DS) {
16773471bf0Spatrick   auto &HotEntry =
16873471bf0Spatrick       ProfileSummaryBuilder::getEntryForPercentile(DS, ProfileSummaryCutoffHot);
16973471bf0Spatrick   uint64_t HotCountThreshold = HotEntry.MinCount;
17073471bf0Spatrick   if (ProfileSummaryHotCount.getNumOccurrences() > 0)
17173471bf0Spatrick     HotCountThreshold = ProfileSummaryHotCount;
17273471bf0Spatrick   return HotCountThreshold;
17373471bf0Spatrick }
17473471bf0Spatrick 
175*d415bd75Srobert uint64_t
getColdCountThreshold(const SummaryEntryVector & DS)176*d415bd75Srobert ProfileSummaryBuilder::getColdCountThreshold(const SummaryEntryVector &DS) {
17773471bf0Spatrick   auto &ColdEntry = ProfileSummaryBuilder::getEntryForPercentile(
17873471bf0Spatrick       DS, ProfileSummaryCutoffCold);
17973471bf0Spatrick   uint64_t ColdCountThreshold = ColdEntry.MinCount;
18073471bf0Spatrick   if (ProfileSummaryColdCount.getNumOccurrences() > 0)
18173471bf0Spatrick     ColdCountThreshold = ProfileSummaryColdCount;
18273471bf0Spatrick   return ColdCountThreshold;
18373471bf0Spatrick }
18473471bf0Spatrick 
getSummary()18509467b48Spatrick std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
18609467b48Spatrick   computeDetailedSummary();
18709467b48Spatrick   return std::make_unique<ProfileSummary>(
18809467b48Spatrick       ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
18909467b48Spatrick       MaxFunctionCount, NumCounts, NumFunctions);
19009467b48Spatrick }
19109467b48Spatrick 
19273471bf0Spatrick std::unique_ptr<ProfileSummary>
computeSummaryForProfiles(const SampleProfileMap & Profiles)19373471bf0Spatrick SampleProfileSummaryBuilder::computeSummaryForProfiles(
194*d415bd75Srobert     const SampleProfileMap &Profiles) {
19573471bf0Spatrick   assert(NumFunctions == 0 &&
19673471bf0Spatrick          "This can only be called on an empty summary builder");
197*d415bd75Srobert   sampleprof::SampleProfileMap ContextLessProfiles;
198*d415bd75Srobert   const sampleprof::SampleProfileMap *ProfilesToUse = &Profiles;
19973471bf0Spatrick   // For CSSPGO, context-sensitive profile effectively split a function profile
20073471bf0Spatrick   // into many copies each representing the CFG profile of a particular calling
20173471bf0Spatrick   // context. That makes the count distribution looks more flat as we now have
20273471bf0Spatrick   // more function profiles each with lower counts, which in turn leads to lower
203*d415bd75Srobert   // hot thresholds. To compensate for that, by default we merge context
204*d415bd75Srobert   // profiles before computing profile summary.
20573471bf0Spatrick   if (UseContextLessSummary || (sampleprof::FunctionSamples::ProfileIsCS &&
20673471bf0Spatrick                                 !UseContextLessSummary.getNumOccurrences())) {
20773471bf0Spatrick     for (const auto &I : Profiles) {
20873471bf0Spatrick       ContextLessProfiles[I.second.getName()].merge(I.second);
20973471bf0Spatrick     }
21073471bf0Spatrick     ProfilesToUse = &ContextLessProfiles;
21173471bf0Spatrick   }
21273471bf0Spatrick 
21373471bf0Spatrick   for (const auto &I : *ProfilesToUse) {
21473471bf0Spatrick     const sampleprof::FunctionSamples &Profile = I.second;
21573471bf0Spatrick     addRecord(Profile);
21673471bf0Spatrick   }
21773471bf0Spatrick 
21873471bf0Spatrick   return getSummary();
21973471bf0Spatrick }
22073471bf0Spatrick 
getSummary()22109467b48Spatrick std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
22209467b48Spatrick   computeDetailedSummary();
22309467b48Spatrick   return std::make_unique<ProfileSummary>(
22409467b48Spatrick       ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
22509467b48Spatrick       MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
22609467b48Spatrick }
22709467b48Spatrick 
addEntryCount(uint64_t Count)22809467b48Spatrick void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
229*d415bd75Srobert   assert(Count <= getInstrMaxCountValue() &&
230*d415bd75Srobert          "Count value should be less than the max count value.");
23109467b48Spatrick   NumFunctions++;
23273471bf0Spatrick   addCount(Count);
23309467b48Spatrick   if (Count > MaxFunctionCount)
23409467b48Spatrick     MaxFunctionCount = Count;
23509467b48Spatrick }
23609467b48Spatrick 
addInternalCount(uint64_t Count)23709467b48Spatrick void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
238*d415bd75Srobert   assert(Count <= getInstrMaxCountValue() &&
239*d415bd75Srobert          "Count value should be less than the max count value.");
24009467b48Spatrick   addCount(Count);
24109467b48Spatrick   if (Count > MaxInternalBlockCount)
24209467b48Spatrick     MaxInternalBlockCount = Count;
24309467b48Spatrick }
244