LLVM  9.0.0svn
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 
35  // The first counter is not necessarily an entry count for IR
36  // instrumentation profiles.
37  // Eventually MaxFunctionCount will become obsolete and this can be
38  // removed.
39  addEntryCount(R.Counts[0]);
40  for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
41  addInternalCount(R.Counts[I]);
42 }
43 
44 // To compute the detailed summary, we consider each line containing samples as
45 // equivalent to a block with a count in the instrumented profile.
47  const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
48  if (!isCallsiteSample) {
49  NumFunctions++;
52  }
53  for (const auto &I : FS.getBodySamples())
54  addCount(I.second.getSamples());
55  for (const auto &I : FS.getCallsiteSamples())
56  for (const auto &CS : I.second)
57  addRecord(CS.second, true);
58 }
59 
60 // The argument to this method is a vector of cutoff percentages and the return
61 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
63  if (DetailedSummaryCutoffs.empty())
64  return;
65  llvm::sort(DetailedSummaryCutoffs);
66  auto Iter = CountFrequencies.begin();
67  const auto End = CountFrequencies.end();
68 
69  uint32_t CountsSeen = 0;
70  uint64_t CurrSum = 0, Count = 0;
71 
72  for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
73  assert(Cutoff <= 999999);
74  APInt Temp(128, TotalCount);
75  APInt N(128, Cutoff);
77  Temp *= N;
78  Temp = Temp.sdiv(D);
79  uint64_t DesiredCount = Temp.getZExtValue();
80  assert(DesiredCount <= TotalCount);
81  while (CurrSum < DesiredCount && Iter != End) {
82  Count = Iter->first;
83  uint32_t Freq = Iter->second;
84  CurrSum += (Count * Freq);
85  CountsSeen += Freq;
86  Iter++;
87  }
88  assert(CurrSum >= DesiredCount);
89  ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
90  DetailedSummary.push_back(PSE);
91  }
92 }
93 
94 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
96  return llvm::make_unique<ProfileSummary>(
99 }
100 
101 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
103  return llvm::make_unique<ProfileSummary>(
105  MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
106 }
107 
108 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
109  addCount(Count);
110  NumFunctions++;
111  if (Count > MaxFunctionCount)
112  MaxFunctionCount = Count;
113 }
114 
115 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
116  addCount(Count);
117  if (Count > MaxInternalBlockCount)
118  MaxInternalBlockCount = Count;
119 }
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
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:1595
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:92
void addRecord(const InstrProfRecord &)
Representation of the samples collected for a function.
Definition: SampleProf.h:216
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:327
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:350
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1122
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:690
Profiling information for a single function.
Definition: InstrProf.h:689
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
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:353