LLVM  13.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/Attributes.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Metadata.h"
16 #include "llvm/IR/Type.h"
20 #include "llvm/Support/Casting.h"
22 
23 using namespace llvm;
24 
26  "profile-summary-contextless", cl::Hidden, cl::init(false), cl::ZeroOrMore,
27  cl::desc("Merge context profiles before calculating thresholds."));
28 
29 // The following two parameters determine the threshold for a count to be
30 // considered hot/cold. These two parameters are percentile values (multiplied
31 // by 10000). If the counts are sorted in descending order, the minimum count to
32 // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
33 // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
34 // threshold for determining cold count (everything <= this threshold is
35 // considered cold).
37  "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
38  cl::desc("A count is hot if it exceeds the minimum count to"
39  " reach this percentile of total counts."));
40 
42  "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
43  cl::desc("A count is cold if it is below the minimum count"
44  " to reach this percentile of total counts."));
45 
47  "profile-summary-huge-working-set-size-threshold", cl::Hidden,
48  cl::init(15000), cl::ZeroOrMore,
49  cl::desc("The code working set size is considered huge if the number of"
50  " blocks required to reach the -profile-summary-cutoff-hot"
51  " percentile exceeds this count."));
52 
54  "profile-summary-large-working-set-size-threshold", cl::Hidden,
55  cl::init(12500), cl::ZeroOrMore,
56  cl::desc("The code working set size is considered large if the number of"
57  " blocks required to reach the -profile-summary-cutoff-hot"
58  " percentile exceeds this count."));
59 
60 // The next two options override the counts derived from summary computation and
61 // are useful for debugging purposes.
63  "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
64  cl::desc("A fixed hot count that overrides the count derived from"
65  " profile-summary-cutoff-hot"));
66 
68  "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
69  cl::desc("A fixed cold count that overrides the count derived from"
70  " profile-summary-cutoff-cold"));
71 
72 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
73 // (which is 1000000) is a desired percentile of total counts.
74 static const uint32_t DefaultCutoffsData[] = {
75  10000, /* 1% */
76  100000, /* 10% */
77  200000, 300000, 400000, 500000, 600000, 700000, 800000,
78  900000, 950000, 990000, 999000, 999900, 999990, 999999};
81 
82 const ProfileSummaryEntry &
84  uint64_t Percentile) {
85  auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
86  return Entry.Cutoff < Percentile;
87  });
88  // The required percentile has to be <= one of the percentiles in the
89  // detailed summary.
90  if (It == DS.end())
91  report_fatal_error("Desired percentile exceeds the maximum cutoff");
92  return *It;
93 }
94 
96  // The first counter is not necessarily an entry count for IR
97  // instrumentation profiles.
98  // Eventually MaxFunctionCount will become obsolete and this can be
99  // removed.
100  addEntryCount(R.Counts[0]);
101  for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
102  addInternalCount(R.Counts[I]);
103 }
104 
105 // To compute the detailed summary, we consider each line containing samples as
106 // equivalent to a block with a count in the instrumented profile.
108  const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
109  if (!isCallsiteSample) {
110  NumFunctions++;
111  if (FS.getHeadSamples() > MaxFunctionCount)
112  MaxFunctionCount = FS.getHeadSamples();
113  }
114  for (const auto &I : FS.getBodySamples()) {
115  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 
160  auto &HotEntry =
162  uint64_t HotCountThreshold = HotEntry.MinCount;
163  if (ProfileSummaryHotCount.getNumOccurrences() > 0)
164  HotCountThreshold = ProfileSummaryHotCount;
165  return HotCountThreshold;
166 }
167 
171  uint64_t ColdCountThreshold = ColdEntry.MinCount;
172  if (ProfileSummaryColdCount.getNumOccurrences() > 0)
174  return ColdCountThreshold;
175 }
176 
177 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
179  return std::make_unique<ProfileSummary>(
182 }
183 
184 std::unique_ptr<ProfileSummary>
186  const StringMap<sampleprof::FunctionSamples> &Profiles) {
187  assert(NumFunctions == 0 &&
188  "This can only be called on an empty summary builder");
189  StringMap<sampleprof::FunctionSamples> ContextLessProfiles;
190  const StringMap<sampleprof::FunctionSamples> *ProfilesToUse = &Profiles;
191  // For CSSPGO, context-sensitive profile effectively split a function profile
192  // into many copies each representing the CFG profile of a particular calling
193  // context. That makes the count distribution looks more flat as we now have
194  // more function profiles each with lower counts, which in turn leads to lower
195  // hot thresholds. To compensate for that, by defauly we merge context
196  // profiles before coumputing profile summary.
199  for (const auto &I : Profiles) {
200  ContextLessProfiles[I.second.getName()].merge(I.second);
201  }
202  ProfilesToUse = &ContextLessProfiles;
203  }
204 
205  for (const auto &I : *ProfilesToUse) {
206  const sampleprof::FunctionSamples &Profile = I.second;
207  addRecord(Profile);
208  }
209 
210  return getSummary();
211 }
212 
213 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
215  return std::make_unique<ProfileSummary>(
217  MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
218 }
219 
220 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
221  NumFunctions++;
222 
223  // Skip invalid count.
224  if (Count == (uint64_t)-1)
225  return;
226 
227  addCount(Count);
228  if (Count > MaxFunctionCount)
229  MaxFunctionCount = Count;
230 }
231 
232 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
233  // Skip invalid count.
234  if (Count == (uint64_t)-1)
235  return;
236 
237  addCount(Count);
238  if (Count > MaxInternalBlockCount)
239  MaxInternalBlockCount = Count;
240 }
UseContextLessSummary
cl::opt< bool > UseContextLessSummary("profile-summary-contextless", cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::desc("Merge context profiles before calculating thresholds."))
llvm::ProfileSummaryBuilder::getEntryForPercentile
static const ProfileSummaryEntry & getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile)
Find the summary entry for a desired percentile of counts.
Definition: ProfileSummaryBuilder.cpp:83
llvm
Definition: AllocatorList.h:23
llvm::sampleprof::FunctionSamples::ProfileIsProbeBased
static bool ProfileIsProbeBased
Definition: SampleProf.h:881
Metadata.h
llvm::sampleprof::FunctionSamples::ProfileIsCS
static bool ProfileIsCS
Definition: SampleProf.h:883
llvm::ProfileSummaryBuilder::getColdCountThreshold
static uint64_t getColdCountThreshold(SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:168
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::SIInstrFlags::DS
@ DS
Definition: SIDefines.h:52
ProfileCommon.h
llvm::cl::ReallyHidden
@ ReallyHidden
Definition: CommandLine.h:144
llvm::InstrProfRecord
Profiling information for a single function.
Definition: InstrProf.h:691
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")
ProfileSummaryColdCount
cl::opt< int > ProfileSummaryColdCount("profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore, cl::desc("A fixed cold count that overrides the count derived from" " profile-summary-cutoff-cold"))
ProfileSummaryCutoffCold
cl::opt< int > ProfileSummaryCutoffCold("profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore, cl::desc("A count is cold if it is below the minimum count" " to reach this percentile of total counts."))
ProfileSummaryLargeWorkingSetSizeThreshold
cl::opt< unsigned > ProfileSummaryLargeWorkingSetSizeThreshold("profile-summary-large-working-set-size-threshold", cl::Hidden, cl::init(12500), cl::ZeroOrMore, 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."))
SampleProf.h
llvm::InstrProfSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:213
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
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1631
llvm::cl::Option::getNumOccurrences
int getNumOccurrences() const
Definition: CommandLine.h:404
llvm::ProfileSummary::PSK_Sample
@ PSK_Sample
Definition: ProfileSummary.h:47
llvm::ProfileSummaryBuilder::MaxFunctionCount
uint64_t MaxFunctionCount
Definition: ProfileCommon.h:52
Type.h
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::ProfileSummaryBuilder::MaxCount
uint64_t MaxCount
Definition: ProfileCommon.h:51
llvm::cl::ZeroOrMore
@ ZeroOrMore
Definition: CommandLine.h:120
llvm::ProfileSummaryBuilder::getHotCountThreshold
static uint64_t getHotCountThreshold(SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:159
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:1675
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::SampleProfileSummaryBuilder::computeSummaryForProfiles
std::unique_ptr< ProfileSummary > computeSummaryForProfiles(const StringMap< sampleprof::FunctionSamples > &Profiles)
Definition: ProfileSummaryBuilder.cpp:185
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:533
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ProfileSummaryHugeWorkingSetSizeThreshold
cl::opt< unsigned > ProfileSummaryHugeWorkingSetSizeThreshold("profile-summary-huge-working-set-size-threshold", cl::Hidden, cl::init(15000), cl::ZeroOrMore, 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."))
ProfileSummaryCutoffHot
cl::opt< int > ProfileSummaryCutoffHot("profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore, cl::desc("A count is hot if it exceeds the minimum count to" " reach this percentile of total counts."))
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::ArrayRef< uint32_t >
uint32_t
llvm::ProfileSummaryEntry
Definition: ProfileSummary.h:33
llvm::SummaryEntryVector
std::vector< ProfileSummaryEntry > SummaryEntryVector
Definition: ProfileSummary.h:43
ProfileSummaryHotCount
cl::opt< int > ProfileSummaryHotCount("profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore, cl::desc("A fixed hot count that overrides the count derived from" " profile-summary-cutoff-hot"))
llvm::SampleProfileSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:177
Attributes.h
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:1630
Casting.h
Function.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1423
llvm::SampleProfileSummaryBuilder::addRecord
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
Definition: ProfileSummaryBuilder.cpp:107
DefaultCutoffsData
static const uint32_t DefaultCutoffsData[]
Definition: ProfileSummaryBuilder.cpp:74
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
llvm::sampleprof::FunctionSamples::InvalidProbeCount
static constexpr uint64_t InvalidProbeCount
Definition: SampleProf.h:879
N
#define N
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::InstrProfSummaryBuilder::addRecord
void addRecord(const InstrProfRecord &)
Definition: ProfileSummaryBuilder.cpp:95
llvm::ProfileSummaryBuilder::DetailedSummary
SummaryEntryVector DetailedSummary
Definition: ProfileCommon.h:49
llvm::cl::desc
Definition: CommandLine.h:414
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