LLVM  12.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"
21 
22 using namespace llvm;
23 
24 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
25 // (which is 1000000) is a desired percentile of total counts.
26 static const uint32_t DefaultCutoffsData[] = {
27  10000, /* 1% */
28  100000, /* 10% */
29  200000, 300000, 400000, 500000, 600000, 700000, 800000,
30  900000, 950000, 990000, 999000, 999900, 999990, 999999};
33 
34 const ProfileSummaryEntry &
36  uint64_t Percentile) {
37  auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
38  return Entry.Cutoff < Percentile;
39  });
40  // The required percentile has to be <= one of the percentiles in the
41  // detailed summary.
42  if (It == DS.end())
43  report_fatal_error("Desired percentile exceeds the maximum cutoff");
44  return *It;
45 }
46 
48  // The first counter is not necessarily an entry count for IR
49  // instrumentation profiles.
50  // Eventually MaxFunctionCount will become obsolete and this can be
51  // removed.
52  addEntryCount(R.Counts[0]);
53  for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
54  addInternalCount(R.Counts[I]);
55 }
56 
57 // To compute the detailed summary, we consider each line containing samples as
58 // equivalent to a block with a count in the instrumented profile.
60  const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
61  if (!isCallsiteSample) {
62  NumFunctions++;
65  }
66  for (const auto &I : FS.getBodySamples())
67  addCount(I.second.getSamples());
68  for (const auto &I : FS.getCallsiteSamples())
69  for (const auto &CS : I.second)
70  addRecord(CS.second, true);
71 }
72 
73 // The argument to this method is a vector of cutoff percentages and the return
74 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
76  if (DetailedSummaryCutoffs.empty())
77  return;
78  llvm::sort(DetailedSummaryCutoffs);
79  auto Iter = CountFrequencies.begin();
80  const auto End = CountFrequencies.end();
81 
82  uint32_t CountsSeen = 0;
83  uint64_t CurrSum = 0, Count = 0;
84 
85  for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
86  assert(Cutoff <= 999999);
87  APInt Temp(128, TotalCount);
88  APInt N(128, Cutoff);
90  Temp *= N;
91  Temp = Temp.sdiv(D);
92  uint64_t DesiredCount = Temp.getZExtValue();
93  assert(DesiredCount <= TotalCount);
94  while (CurrSum < DesiredCount && Iter != End) {
95  Count = Iter->first;
96  uint32_t Freq = Iter->second;
97  CurrSum += (Count * Freq);
98  CountsSeen += Freq;
99  Iter++;
100  }
101  assert(CurrSum >= DesiredCount);
102  ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
103  DetailedSummary.push_back(PSE);
104  }
105 }
106 
107 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
109  return std::make_unique<ProfileSummary>(
112 }
113 
114 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
116  return std::make_unique<ProfileSummary>(
118  MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
119 }
120 
121 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
122  addCount(Count);
123  NumFunctions++;
124  if (Count > MaxFunctionCount)
125  MaxFunctionCount = Count;
126 }
127 
128 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
129  addCount(Count);
130  if (Count > MaxInternalBlockCount)
131  MaxInternalBlockCount = Count;
132 }
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1623
uint32_t Cutoff
The required percentile of counts.
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
This class represents lattice values for constants.
Definition: AllocatorList.h:23
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1670
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
This file contains the declarations for metadata subclasses.
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:64
void addCount(uint64_t Count)
This is called when a count is seen in the profile.
Definition: ProfileCommon.h:96
void addRecord(const InstrProfRecord &)
Representation of the samples collected for a function.
Definition: SampleProf.h:357
static const int Scale
This file contains the simple types necessary to represent the attributes associated with functions a...
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
Definition: SampleProf.h:471
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:494
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:1632
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1433
std::unique_ptr< ProfileSummary > getSummary()
SummaryEntryVector DetailedSummary
Definition: ProfileCommon.h:48
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Class for arbitrary precision integers.
Definition: APInt.h:69
std::unique_ptr< ProfileSummary > getSummary()
std::vector< uint64_t > Counts
Definition: InstrProf.h:699
Profiling information for a single function.
Definition: InstrProf.h:698
#define I(x, y, z)
Definition: MD5.cpp:59
#define N
static const ProfileSummaryEntry & getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile)
Find the summary entry for a desired percentile of counts.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const uint32_t DefaultCutoffsData[]
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
Definition: SampleProf.h:497
std::vector< ProfileSummaryEntry > SummaryEntryVector