xref: /freebsd/contrib/llvm-project/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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 #include "llvm/Support/CommandLine.h"
22 
23 using namespace llvm;
24 
25 cl::opt<bool> UseContextLessSummary(
26     "profile-summary-contextless", cl::Hidden, cl::init(false), cl::ZeroOrMore,
27     cl::desc("Merge context profiles before calculating thresholds."));
28 
29 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
30 // (which is 1000000) is a desired percentile of total counts.
31 static const uint32_t DefaultCutoffsData[] = {
32     10000,  /*  1% */
33     100000, /* 10% */
34     200000, 300000, 400000, 500000, 600000, 700000, 800000,
35     900000, 950000, 990000, 999000, 999900, 999990, 999999};
36 const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
37     DefaultCutoffsData;
38 
39 const ProfileSummaryEntry &
40 ProfileSummaryBuilder::getEntryForPercentile(SummaryEntryVector &DS,
41                                              uint64_t Percentile) {
42   auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
43     return Entry.Cutoff < Percentile;
44   });
45   // The required percentile has to be <= one of the percentiles in the
46   // detailed summary.
47   if (It == DS.end())
48     report_fatal_error("Desired percentile exceeds the maximum cutoff");
49   return *It;
50 }
51 
52 void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
53   // The first counter is not necessarily an entry count for IR
54   // instrumentation profiles.
55   // Eventually MaxFunctionCount will become obsolete and this can be
56   // removed.
57   addEntryCount(R.Counts[0]);
58   for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
59     addInternalCount(R.Counts[I]);
60 }
61 
62 // To compute the detailed summary, we consider each line containing samples as
63 // equivalent to a block with a count in the instrumented profile.
64 void SampleProfileSummaryBuilder::addRecord(
65     const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
66   if (!isCallsiteSample) {
67     NumFunctions++;
68     if (FS.getHeadSamples() > MaxFunctionCount)
69       MaxFunctionCount = FS.getHeadSamples();
70   }
71   for (const auto &I : FS.getBodySamples())
72     addCount(I.second.getSamples());
73   for (const auto &I : FS.getCallsiteSamples())
74     for (const auto &CS : I.second)
75       addRecord(CS.second, true);
76 }
77 
78 // The argument to this method is a vector of cutoff percentages and the return
79 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
80 void ProfileSummaryBuilder::computeDetailedSummary() {
81   if (DetailedSummaryCutoffs.empty())
82     return;
83   llvm::sort(DetailedSummaryCutoffs);
84   auto Iter = CountFrequencies.begin();
85   const auto End = CountFrequencies.end();
86 
87   uint32_t CountsSeen = 0;
88   uint64_t CurrSum = 0, Count = 0;
89 
90   for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
91     assert(Cutoff <= 999999);
92     APInt Temp(128, TotalCount);
93     APInt N(128, Cutoff);
94     APInt D(128, ProfileSummary::Scale);
95     Temp *= N;
96     Temp = Temp.sdiv(D);
97     uint64_t DesiredCount = Temp.getZExtValue();
98     assert(DesiredCount <= TotalCount);
99     while (CurrSum < DesiredCount && Iter != End) {
100       Count = Iter->first;
101       uint32_t Freq = Iter->second;
102       CurrSum += (Count * Freq);
103       CountsSeen += Freq;
104       Iter++;
105     }
106     assert(CurrSum >= DesiredCount);
107     ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
108     DetailedSummary.push_back(PSE);
109   }
110 }
111 
112 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
113   computeDetailedSummary();
114   return std::make_unique<ProfileSummary>(
115       ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
116       MaxFunctionCount, NumCounts, NumFunctions);
117 }
118 
119 std::unique_ptr<ProfileSummary>
120 SampleProfileSummaryBuilder::computeSummaryForProfiles(
121     const StringMap<sampleprof::FunctionSamples> &Profiles) {
122   assert(NumFunctions == 0 &&
123          "This can only be called on an empty summary builder");
124   StringMap<sampleprof::FunctionSamples> ContextLessProfiles;
125   const StringMap<sampleprof::FunctionSamples> *ProfilesToUse = &Profiles;
126   // For CSSPGO, context-sensitive profile effectively split a function profile
127   // into many copies each representing the CFG profile of a particular calling
128   // context. That makes the count distribution looks more flat as we now have
129   // more function profiles each with lower counts, which in turn leads to lower
130   // hot thresholds. To compensate for that, by defauly we merge context
131   // profiles before coumputing profile summary.
132   if (UseContextLessSummary || (sampleprof::FunctionSamples::ProfileIsCS &&
133                                 !UseContextLessSummary.getNumOccurrences())) {
134     for (const auto &I : Profiles) {
135       ContextLessProfiles[I.second.getName()].merge(I.second);
136     }
137     ProfilesToUse = &ContextLessProfiles;
138   }
139 
140   for (const auto &I : *ProfilesToUse) {
141     const sampleprof::FunctionSamples &Profile = I.second;
142     addRecord(Profile);
143   }
144 
145   return getSummary();
146 }
147 
148 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
149   computeDetailedSummary();
150   return std::make_unique<ProfileSummary>(
151       ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
152       MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
153 }
154 
155 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
156   NumFunctions++;
157 
158   // Skip invalid count.
159   if (Count == (uint64_t)-1)
160     return;
161 
162   addCount(Count);
163   if (Count > MaxFunctionCount)
164     MaxFunctionCount = Count;
165 }
166 
167 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
168   // Skip invalid count.
169   if (Count == (uint64_t)-1)
170     return;
171 
172   addCount(Count);
173   if (Count > MaxInternalBlockCount)
174     MaxInternalBlockCount = Count;
175 }
176