xref: /freebsd/contrib/llvm-project/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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"
17 #include "llvm/ProfileData/InstrProf.h"
18 #include "llvm/ProfileData/ProfileCommon.h"
19 #include "llvm/ProfileData/SampleProf.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};
31 const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
32     DefaultCutoffsData;
33 
34 const ProfileSummaryEntry &
35 ProfileSummaryBuilder::getEntryForPercentile(SummaryEntryVector &DS,
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 
47 void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
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.
59 void SampleProfileSummaryBuilder::addRecord(
60     const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
61   if (!isCallsiteSample) {
62     NumFunctions++;
63     if (FS.getHeadSamples() > MaxFunctionCount)
64       MaxFunctionCount = FS.getHeadSamples();
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.
75 void ProfileSummaryBuilder::computeDetailedSummary() {
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);
89     APInt D(128, ProfileSummary::Scale);
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() {
108   computeDetailedSummary();
109   return std::make_unique<ProfileSummary>(
110       ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
111       MaxFunctionCount, NumCounts, NumFunctions);
112 }
113 
114 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
115   computeDetailedSummary();
116   return std::make_unique<ProfileSummary>(
117       ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
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 }
133