LLVM  15.0.0git
ProfileSummaryBuilder.cpp
Go to the documentation of this file.
1 //=-- ProfilesummaryBuilder.cpp - Profile summary computation ---------------=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for computing profile summary data.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/ProfileSummary.h"
18 
19 using namespace llvm;
20 
22  "profile-summary-contextless", cl::Hidden,
23  cl::desc("Merge context profiles before calculating thresholds."));
24 
25 // The following two parameters determine the threshold for a count to be
26 // considered hot/cold. These two parameters are percentile values (multiplied
27 // by 10000). If the counts are sorted in descending order, the minimum count to
28 // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
29 // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
30 // threshold for determining cold count (everything <= this threshold is
31 // considered cold).
33  "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000),
34  cl::desc("A count is hot if it exceeds the minimum count to"
35  " reach this percentile of total counts."));
36 
38  "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999),
39  cl::desc("A count is cold if it is below the minimum count"
40  " to reach this percentile of total counts."));
41 
43  "profile-summary-huge-working-set-size-threshold", cl::Hidden,
44  cl::init(15000),
45  cl::desc("The code working set size is considered huge if the number of"
46  " blocks required to reach the -profile-summary-cutoff-hot"
47  " percentile exceeds this count."));
48 
50  "profile-summary-large-working-set-size-threshold", cl::Hidden,
51  cl::init(12500),
52  cl::desc("The code working set size is considered large if the number of"
53  " blocks required to reach the -profile-summary-cutoff-hot"
54  " percentile exceeds this count."));
55 
56 // The next two options override the counts derived from summary computation and
57 // are useful for debugging purposes.
59  "profile-summary-hot-count", cl::ReallyHidden,
60  cl::desc("A fixed hot count that overrides the count derived from"
61  " profile-summary-cutoff-hot"));
62 
64  "profile-summary-cold-count", cl::ReallyHidden,
65  cl::desc("A fixed cold count that overrides the count derived from"
66  " profile-summary-cutoff-cold"));
67 
68 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
69 // (which is 1000000) is a desired percentile of total counts.
70 static const uint32_t DefaultCutoffsData[] = {
71  10000, /* 1% */
72  100000, /* 10% */
73  200000, 300000, 400000, 500000, 600000, 700000, 800000,
74  900000, 950000, 990000, 999000, 999900, 999990, 999999};
77 
78 const ProfileSummaryEntry &
80  uint64_t Percentile) {
81  auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
82  return Entry.Cutoff < Percentile;
83  });
84  // The required percentile has to be <= one of the percentiles in the
85  // detailed summary.
86  if (It == DS.end())
87  report_fatal_error("Desired percentile exceeds the maximum cutoff");
88  return *It;
89 }
90 
92  // The first counter is not necessarily an entry count for IR
93  // instrumentation profiles.
94  // Eventually MaxFunctionCount will become obsolete and this can be
95  // removed.
96  addEntryCount(R.Counts[0]);
97  for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
98  addInternalCount(R.Counts[I]);
99 }
100 
101 // To compute the detailed summary, we consider each line containing samples as
102 // equivalent to a block with a count in the instrumented profile.
104  const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
105  if (!isCallsiteSample) {
106  NumFunctions++;
107  if (FS.getHeadSamples() > MaxFunctionCount)
108  MaxFunctionCount = FS.getHeadSamples();
109  } else if (FS.getContext().hasAttribute(
111  // Do not recount callee samples if they are already merged into their base
112  // profiles. This can happen to CS nested profile.
113  return;
114  }
115 
116  for (const auto &I : FS.getBodySamples()) {
117  uint64_t Count = I.second.getSamples();
118  addCount(Count);
119  }
120  for (const auto &I : FS.getCallsiteSamples())
121  for (const auto &CS : I.second)
122  addRecord(CS.second, true);
123 }
124 
125 // The argument to this method is a vector of cutoff percentages and the return
126 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
128  if (DetailedSummaryCutoffs.empty())
129  return;
130  llvm::sort(DetailedSummaryCutoffs);
131  auto Iter = CountFrequencies.begin();
132  const auto End = CountFrequencies.end();
133 
134  uint32_t CountsSeen = 0;
135  uint64_t CurrSum = 0, Count = 0;
136 
137  for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
138  assert(Cutoff <= 999999);
139  APInt Temp(128, TotalCount);
140  APInt N(128, Cutoff);
142  Temp *= N;
143  Temp = Temp.sdiv(D);
144  uint64_t DesiredCount = Temp.getZExtValue();
145  assert(DesiredCount <= TotalCount);
146  while (CurrSum < DesiredCount && Iter != End) {
147  Count = Iter->first;
148  uint32_t Freq = Iter->second;
149  CurrSum += (Count * Freq);
150  CountsSeen += Freq;
151  Iter++;
152  }
153  assert(CurrSum >= DesiredCount);
154  ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
155  DetailedSummary.push_back(PSE);
156  }
157 }
158 
159 uint64_t
161  auto &HotEntry =
163  uint64_t HotCountThreshold = HotEntry.MinCount;
164  if (ProfileSummaryHotCount.getNumOccurrences() > 0)
165  HotCountThreshold = ProfileSummaryHotCount;
166  return HotCountThreshold;
167 }
168 
169 uint64_t
173  uint64_t ColdCountThreshold = ColdEntry.MinCount;
174  if (ProfileSummaryColdCount.getNumOccurrences() > 0)
176  return ColdCountThreshold;
177 }
178 
179 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
181  return std::make_unique<ProfileSummary>(
184 }
185 
186 std::unique_ptr<ProfileSummary>
188  const SampleProfileMap &Profiles) {
189  assert(NumFunctions == 0 &&
190  "This can only be called on an empty summary builder");
191  sampleprof::SampleProfileMap ContextLessProfiles;
192  const sampleprof::SampleProfileMap *ProfilesToUse = &Profiles;
193  // For CSSPGO, context-sensitive profile effectively split a function profile
194  // into many copies each representing the CFG profile of a particular calling
195  // context. That makes the count distribution looks more flat as we now have
196  // more function profiles each with lower counts, which in turn leads to lower
197  // hot thresholds. To compensate for that, by default we merge context
198  // profiles before computing profile summary.
201  for (const auto &I : Profiles) {
202  ContextLessProfiles[I.second.getName()].merge(I.second);
203  }
204  ProfilesToUse = &ContextLessProfiles;
205  }
206 
207  for (const auto &I : *ProfilesToUse) {
208  const sampleprof::FunctionSamples &Profile = I.second;
210  }
211 
212  return getSummary();
213 }
214 
215 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
217  return std::make_unique<ProfileSummary>(
219  MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
220 }
221 
222 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
223  NumFunctions++;
224 
225  // Skip invalid count.
226  if (Count == (uint64_t)-1)
227  return;
228 
229  addCount(Count);
230  if (Count > MaxFunctionCount)
231  MaxFunctionCount = Count;
232 }
233 
234 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
235  // Skip invalid count.
236  if (Count == (uint64_t)-1)
237  return;
238 
239  addCount(Count);
240  if (Count > MaxInternalBlockCount)
241  MaxInternalBlockCount = Count;
242 }
llvm::ProfileSummaryBuilder::getHotCountThreshold
static uint64_t getHotCountThreshold(const SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:160
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::sampleprof::ContextDuplicatedIntoBase
@ ContextDuplicatedIntoBase
Definition: SampleProf.h:443
llvm::sampleprof::FunctionSamples::ProfileIsCS
static bool ProfileIsCS
Definition: SampleProf.h:1104
ProfileSummary.h
llvm::sampleprof::SampleProfileMap
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
Definition: SampleProf.h:1183
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
UseContextLessSummary
cl::opt< bool > UseContextLessSummary("profile-summary-contextless", cl::Hidden, cl::desc("Merge context profiles before calculating thresholds."))
ProfileCommon.h
llvm::cl::ReallyHidden
@ ReallyHidden
Definition: CommandLine.h:140
llvm::InstrProfRecord
Profiling information for a single function.
Definition: InstrProf.h:725
ProfileSummaryColdCount
cl::opt< uint64_t > ProfileSummaryColdCount("profile-summary-cold-count", cl::ReallyHidden, cl::desc("A fixed cold count that overrides the count derived from" " profile-summary-cutoff-cold"))
llvm::ProfileSummaryBuilder::TotalCount
uint64_t TotalCount
Definition: ProfileCommon.h:50
CommandLine.h
llvm::ProfileSummaryBuilder::NumFunctions
uint32_t NumFunctions
Definition: ProfileCommon.h:54
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SampleProf.h
llvm::InstrProfSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:215
ColdCountThreshold
static cl::opt< unsigned > ColdCountThreshold("mfs-count-threshold", cl::desc("Minimum number of times a block must be executed to be retained."), cl::init(1), cl::Hidden)
InstrProf.h
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1466
llvm::cl::Option::getNumOccurrences
int getNumOccurrences() const
Definition: CommandLine.h:395
ProfileSummaryHotCount
cl::opt< uint64_t > ProfileSummaryHotCount("profile-summary-hot-count", cl::ReallyHidden, cl::desc("A fixed hot count that overrides the count derived from" " profile-summary-cutoff-hot"))
llvm::ProfileSummary::PSK_Sample
@ PSK_Sample
Definition: ProfileSummary.h:47
llvm::ProfileSummaryBuilder::MaxFunctionCount
uint64_t MaxFunctionCount
Definition: ProfileCommon.h:52
llvm::ProfileSummaryBuilder::MaxCount
uint64_t MaxCount
Definition: ProfileCommon.h:51
llvm::SampleProfileSummaryBuilder::computeSummaryForProfiles
std::unique_ptr< ProfileSummary > computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles)
Definition: ProfileSummaryBuilder.cpp:187
llvm::cl::opt< bool >
llvm::ProfileSummaryBuilder::computeDetailedSummary
void computeDetailedSummary()
Definition: ProfileSummaryBuilder.cpp:127
llvm::APInt::sdiv
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1642
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:710
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::ProfileSummaryBuilder::getColdCountThreshold
static uint64_t getColdCountThreshold(const SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:170
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::X86AS::FS
@ FS
Definition: X86.h:192
llvm::SIInstrFlags::DS
@ DS
Definition: SIDefines.h:60
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::ArrayRef< uint32_t >
ProfileSummaryCutoffHot
cl::opt< int > ProfileSummaryCutoffHot("profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::desc("A count is hot if it exceeds the minimum count to" " reach this percentile of total counts."))
uint32_t
ProfileSummaryCutoffCold
cl::opt< int > ProfileSummaryCutoffCold("profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::desc("A count is cold if it is below the minimum count" " to reach this percentile of total counts."))
llvm::ProfileSummaryEntry
Definition: ProfileSummary.h:33
Profile
Load MIR Sample Profile
Definition: MIRSampleProfile.cpp:70
llvm::SummaryEntryVector
std::vector< ProfileSummaryEntry > SummaryEntryVector
Definition: ProfileSummary.h:43
ProfileSummaryLargeWorkingSetSizeThreshold
cl::opt< unsigned > ProfileSummaryLargeWorkingSetSizeThreshold("profile-summary-large-working-set-size-threshold", cl::Hidden, cl::init(12500), cl::desc("The code working set size is considered large if the number of" " blocks required to reach the -profile-summary-cutoff-hot" " percentile exceeds this count."))
llvm::SampleProfileSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:179
llvm::ProfileSummaryBuilder::NumCounts
uint32_t NumCounts
Definition: ProfileCommon.h:53
llvm::partition_point
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:1774
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1562
llvm::SampleProfileSummaryBuilder::addRecord
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
Definition: ProfileSummaryBuilder.cpp:103
DefaultCutoffsData
static const uint32_t DefaultCutoffsData[]
Definition: ProfileSummaryBuilder.cpp:70
llvm::ProfileSummaryBuilder::addCount
void addCount(uint64_t Count)
This is called when a count is seen in the profile.
Definition: ProfileCommon.h:101
llvm::ProfileSummary::PSK_Instr
@ PSK_Instr
Definition: ProfileSummary.h:47
N
#define N
llvm::InstrProfSummaryBuilder::addRecord
void addRecord(const InstrProfRecord &)
Definition: ProfileSummaryBuilder.cpp:91
llvm::ProfileSummaryBuilder::DetailedSummary
SummaryEntryVector DetailedSummary
Definition: ProfileCommon.h:49
llvm::cl::desc
Definition: CommandLine.h:405
llvm::ProfileSummary::Scale
static const int Scale
Definition: ProfileSummary.h:69
llvm::ProfileSummaryBuilder::DefaultCutoffs
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
ProfileSummaryHugeWorkingSetSizeThreshold
cl::opt< unsigned > ProfileSummaryHugeWorkingSetSizeThreshold("profile-summary-huge-working-set-size-threshold", cl::Hidden, cl::init(15000), cl::desc("The code working set size is considered huge if the number of" " blocks required to reach the -profile-summary-cutoff-hot" " percentile exceeds this count."))
llvm::ProfileSummaryBuilder::getEntryForPercentile
static const ProfileSummaryEntry & getEntryForPercentile(const SummaryEntryVector &DS, uint64_t Percentile)
Find the summary entry for a desired percentile of counts.
Definition: ProfileSummaryBuilder.cpp:79