LLVM  16.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 
21 namespace llvm {
23  "profile-summary-contextless", cl::Hidden,
24  cl::desc("Merge context profiles before calculating thresholds."));
25 
26 // The following two parameters determine the threshold for a count to be
27 // considered hot/cold. These two parameters are percentile values (multiplied
28 // by 10000). If the counts are sorted in descending order, the minimum count to
29 // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
30 // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
31 // threshold for determining cold count (everything <= this threshold is
32 // considered cold).
34  "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000),
35  cl::desc("A count is hot if it exceeds the minimum count to"
36  " reach this percentile of total counts."));
37 
39  "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999),
40  cl::desc("A count is cold if it is below the minimum count"
41  " to reach this percentile of total counts."));
42 
44  "profile-summary-huge-working-set-size-threshold", cl::Hidden,
45  cl::init(15000),
46  cl::desc("The code working set size is considered huge if the number of"
47  " blocks required to reach the -profile-summary-cutoff-hot"
48  " percentile exceeds this count."));
49 
51  "profile-summary-large-working-set-size-threshold", cl::Hidden,
52  cl::init(12500),
53  cl::desc("The code working set size is considered large if the number of"
54  " blocks required to reach the -profile-summary-cutoff-hot"
55  " percentile exceeds this count."));
56 
57 // The next two options override the counts derived from summary computation and
58 // are useful for debugging purposes.
60  "profile-summary-hot-count", cl::ReallyHidden,
61  cl::desc("A fixed hot count that overrides the count derived from"
62  " profile-summary-cutoff-hot"));
63 
65  "profile-summary-cold-count", cl::ReallyHidden,
66  cl::desc("A fixed cold count that overrides the count derived from"
67  " profile-summary-cutoff-cold"));
68 } // namespace llvm
69 
70 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
71 // (which is 1000000) is a desired percentile of total counts.
72 static const uint32_t DefaultCutoffsData[] = {
73  10000, /* 1% */
74  100000, /* 10% */
75  200000, 300000, 400000, 500000, 600000, 700000, 800000,
76  900000, 950000, 990000, 999000, 999900, 999990, 999999};
79 
80 const ProfileSummaryEntry &
82  uint64_t Percentile) {
83  auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
84  return Entry.Cutoff < Percentile;
85  });
86  // The required percentile has to be <= one of the percentiles in the
87  // detailed summary.
88  if (It == DS.end())
89  report_fatal_error("Desired percentile exceeds the maximum cutoff");
90  return *It;
91 }
92 
94  // The first counter is not necessarily an entry count for IR
95  // instrumentation profiles.
96  // Eventually MaxFunctionCount will become obsolete and this can be
97  // removed.
98 
99  if (R.getCountPseudoKind() != InstrProfRecord::NotPseudo)
100  return;
101 
102  addEntryCount(R.Counts[0]);
103  for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
104  addInternalCount(R.Counts[I]);
105 }
106 
107 // To compute the detailed summary, we consider each line containing samples as
108 // equivalent to a block with a count in the instrumented profile.
110  const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
111  if (!isCallsiteSample) {
112  NumFunctions++;
113  if (FS.getHeadSamples() > MaxFunctionCount)
114  MaxFunctionCount = FS.getHeadSamples();
115  } else if (FS.getContext().hasAttribute(
117  // Do not recount callee samples if they are already merged into their base
118  // profiles. This can happen to CS nested profile.
119  return;
120  }
121 
122  for (const auto &I : FS.getBodySamples()) {
123  uint64_t Count = I.second.getSamples();
124  addCount(Count);
125  }
126  for (const auto &I : FS.getCallsiteSamples())
127  for (const auto &CS : I.second)
128  addRecord(CS.second, true);
129 }
130 
131 // The argument to this method is a vector of cutoff percentages and the return
132 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
134  if (DetailedSummaryCutoffs.empty())
135  return;
136  llvm::sort(DetailedSummaryCutoffs);
137  auto Iter = CountFrequencies.begin();
138  const auto End = CountFrequencies.end();
139 
140  uint32_t CountsSeen = 0;
141  uint64_t CurrSum = 0, Count = 0;
142 
143  for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
144  assert(Cutoff <= 999999);
145  APInt Temp(128, TotalCount);
146  APInt N(128, Cutoff);
148  Temp *= N;
149  Temp = Temp.sdiv(D);
150  uint64_t DesiredCount = Temp.getZExtValue();
151  assert(DesiredCount <= TotalCount);
152  while (CurrSum < DesiredCount && Iter != End) {
153  Count = Iter->first;
154  uint32_t Freq = Iter->second;
155  CurrSum += (Count * Freq);
156  CountsSeen += Freq;
157  Iter++;
158  }
159  assert(CurrSum >= DesiredCount);
160  ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
161  DetailedSummary.push_back(PSE);
162  }
163 }
164 
165 uint64_t
167  auto &HotEntry =
169  uint64_t HotCountThreshold = HotEntry.MinCount;
170  if (ProfileSummaryHotCount.getNumOccurrences() > 0)
171  HotCountThreshold = ProfileSummaryHotCount;
172  return HotCountThreshold;
173 }
174 
175 uint64_t
179  uint64_t ColdCountThreshold = ColdEntry.MinCount;
180  if (ProfileSummaryColdCount.getNumOccurrences() > 0)
182  return ColdCountThreshold;
183 }
184 
185 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
187  return std::make_unique<ProfileSummary>(
190 }
191 
192 std::unique_ptr<ProfileSummary>
194  const SampleProfileMap &Profiles) {
195  assert(NumFunctions == 0 &&
196  "This can only be called on an empty summary builder");
197  sampleprof::SampleProfileMap ContextLessProfiles;
198  const sampleprof::SampleProfileMap *ProfilesToUse = &Profiles;
199  // For CSSPGO, context-sensitive profile effectively split a function profile
200  // into many copies each representing the CFG profile of a particular calling
201  // context. That makes the count distribution looks more flat as we now have
202  // more function profiles each with lower counts, which in turn leads to lower
203  // hot thresholds. To compensate for that, by default we merge context
204  // profiles before computing profile summary.
207  for (const auto &I : Profiles) {
208  ContextLessProfiles[I.second.getName()].merge(I.second);
209  }
210  ProfilesToUse = &ContextLessProfiles;
211  }
212 
213  for (const auto &I : *ProfilesToUse) {
214  const sampleprof::FunctionSamples &Profile = I.second;
216  }
217 
218  return getSummary();
219 }
220 
221 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
223  return std::make_unique<ProfileSummary>(
225  MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
226 }
227 
228 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
229  assert(Count <= getInstrMaxCountValue() &&
230  "Count value should be less than the max count value.");
231  NumFunctions++;
232  addCount(Count);
233  if (Count > MaxFunctionCount)
234  MaxFunctionCount = Count;
235 }
236 
237 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
238  assert(Count <= getInstrMaxCountValue() &&
239  "Count value should be less than the max count value.");
240  addCount(Count);
241  if (Count > MaxInternalBlockCount)
242  MaxInternalBlockCount = Count;
243 }
llvm::ProfileSummaryLargeWorkingSetSizeThreshold
cl::opt< unsigned > ProfileSummaryLargeWorkingSetSizeThreshold
llvm::ProfileSummaryBuilder::getHotCountThreshold
static uint64_t getHotCountThreshold(const SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:166
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::sampleprof::ContextDuplicatedIntoBase
@ ContextDuplicatedIntoBase
Definition: SampleProf.h:451
llvm::sampleprof::FunctionSamples::ProfileIsCS
static bool ProfileIsCS
Definition: SampleProf.h:1116
llvm::ProfileSummaryHugeWorkingSetSizeThreshold
cl::opt< unsigned > ProfileSummaryHugeWorkingSetSizeThreshold
ProfileSummary.h
llvm::sampleprof::SampleProfileMap
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
Definition: SampleProf.h:1195
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::getInstrMaxCountValue
uint64_t getInstrMaxCountValue()
Return the max count value. We reserver a few large values for special use.
Definition: InstrProf.h:64
llvm::X86AS::FS
@ FS
Definition: X86.h:200
llvm::ProfileSummaryHotCount
cl::opt< int > ProfileSummaryHotCount
ProfileCommon.h
llvm::cl::ReallyHidden
@ ReallyHidden
Definition: CommandLine.h:141
llvm::InstrProfRecord
Profiling information for a single function.
Definition: InstrProf.h:730
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:221
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:145
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1486
llvm::cl::Option::getNumOccurrences
int getNumOccurrences() const
Definition: CommandLine.h:403
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::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1657
llvm::SampleProfileSummaryBuilder::computeSummaryForProfiles
std::unique_ptr< ProfileSummary > computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles)
Definition: ProfileSummaryBuilder.cpp:193
llvm::cl::opt< bool >
llvm::ProfileSummaryBuilder::computeDetailedSummary
void computeDetailedSummary()
Definition: ProfileSummaryBuilder.cpp:133
llvm::ProfileSummaryCutoffCold
cl::opt< int > ProfileSummaryCutoffCold
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:718
llvm::InstrProfRecord::NotPseudo
@ NotPseudo
Definition: InstrProf.h:828
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::ProfileSummaryBuilder::getColdCountThreshold
static uint64_t getColdCountThreshold(const SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:176
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:447
llvm::ProfileSummaryColdCount
cl::opt< int > ProfileSummaryColdCount
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ProfileSummaryCutoffHot
cl::opt< int > ProfileSummaryCutoffHot
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::ArrayRef< uint32_t >
uint32_t
llvm::UseContextLessSummary
cl::opt< bool > UseContextLessSummary("profile-summary-contextless", cl::Hidden, cl::desc("Merge context profiles before calculating thresholds."))
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
llvm::SampleProfileSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:185
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:1935
llvm::SampleProfileSummaryBuilder::addRecord
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
Definition: ProfileSummaryBuilder.cpp:109
DefaultCutoffsData
static const uint32_t DefaultCutoffsData[]
Definition: ProfileSummaryBuilder.cpp:72
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:93
llvm::ProfileSummaryBuilder::DetailedSummary
SummaryEntryVector DetailedSummary
Definition: ProfileCommon.h:49
llvm::cl::desc
Definition: CommandLine.h:413
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
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:81