1 //===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
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 a pass that provides access to the global profile summary
10 // information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/ProfileSummaryInfo.h"
15 #include "llvm/Analysis/BlockFrequencyInfo.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/ProfileSummary.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/Support/CommandLine.h"
23 using namespace llvm;
24 
25 // The following two parameters determine the threshold for a count to be
26 // considered hot/cold. These two parameters are percentile values (multiplied
27 // by 10000). If the counts are sorted in descending order, the minimum count to
28 // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
29 // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
30 // threshold for determining cold count (everything <= this threshold is
31 // considered cold).
32 
33 static cl::opt<int> ProfileSummaryCutoffHot(
34     "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
35     cl::desc("A count is hot if it exceeds the minimum count to"
36              " reach this percentile of total counts."));
37 
38 static cl::opt<int> ProfileSummaryCutoffCold(
39     "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
40     cl::desc("A count is cold if it is below the minimum count"
41              " to reach this percentile of total counts."));
42 
43 static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
44     "profile-summary-huge-working-set-size-threshold", cl::Hidden,
45     cl::init(15000), cl::ZeroOrMore,
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 
50 static cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold(
51     "profile-summary-large-working-set-size-threshold", cl::Hidden,
52     cl::init(12500), cl::ZeroOrMore,
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.
59 static cl::opt<int> ProfileSummaryHotCount(
60     "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
61     cl::desc("A fixed hot count that overrides the count derived from"
62              " profile-summary-cutoff-hot"));
63 
64 static cl::opt<int> ProfileSummaryColdCount(
65     "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
66     cl::desc("A fixed cold count that overrides the count derived from"
67              " profile-summary-cutoff-cold"));
68 
69 static cl::opt<bool> PartialProfile(
70     "partial-profile", cl::Hidden, cl::init(false),
71     cl::desc("Specify the current profile is used as a partial profile."));
72 
73 // Find the summary entry for a desired percentile of counts.
74 static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
75                                                         uint64_t Percentile) {
76   auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
77     return Entry.Cutoff < Percentile;
78   });
79   // The required percentile has to be <= one of the percentiles in the
80   // detailed summary.
81   if (It == DS.end())
82     report_fatal_error("Desired percentile exceeds the maximum cutoff");
83   return *It;
84 }
85 
86 // The profile summary metadata may be attached either by the frontend or by
87 // any backend passes (IR level instrumentation, for example). This method
88 // checks if the Summary is null and if so checks if the summary metadata is now
89 // available in the module and parses it to get the Summary object. Returns true
90 // if a valid Summary is available.
91 bool ProfileSummaryInfo::computeSummary() {
92   if (Summary)
93     return true;
94   // First try to get context sensitive ProfileSummary.
95   auto *SummaryMD = M.getProfileSummary(/* IsCS */ true);
96   if (SummaryMD) {
97     Summary.reset(ProfileSummary::getFromMD(SummaryMD));
98     return true;
99   }
100   // This will actually return PSK_Instr or PSK_Sample summary.
101   SummaryMD = M.getProfileSummary(/* IsCS */ false);
102   if (!SummaryMD)
103     return false;
104   Summary.reset(ProfileSummary::getFromMD(SummaryMD));
105   return true;
106 }
107 
108 Optional<uint64_t> ProfileSummaryInfo::getProfileCount(const CallBase &Call,
109                                                        BlockFrequencyInfo *BFI,
110                                                        bool AllowSynthetic) {
111   assert((isa<CallInst>(Call) || isa<InvokeInst>(Call)) &&
112          "We can only get profile count for call/invoke instruction.");
113   if (hasSampleProfile()) {
114     // In sample PGO mode, check if there is a profile metadata on the
115     // instruction. If it is present, determine hotness solely based on that,
116     // since the sampled entry count may not be accurate. If there is no
117     // annotated on the instruction, return None.
118     uint64_t TotalCount;
119     if (Call.extractProfTotalWeight(TotalCount))
120       return TotalCount;
121     return None;
122   }
123   if (BFI)
124     return BFI->getBlockProfileCount(Call.getParent(), AllowSynthetic);
125   return None;
126 }
127 
128 /// Returns true if the function's entry is hot. If it returns false, it
129 /// either means it is not hot or it is unknown whether it is hot or not (for
130 /// example, no profile data is available).
131 bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
132   if (!F || !computeSummary())
133     return false;
134   auto FunctionCount = F->getEntryCount();
135   // FIXME: The heuristic used below for determining hotness is based on
136   // preliminary SPEC tuning for inliner. This will eventually be a
137   // convenience method that calls isHotCount.
138   return FunctionCount && isHotCount(FunctionCount.getCount());
139 }
140 
141 /// Returns true if the function contains hot code. This can include a hot
142 /// function entry count, hot basic block, or (in the case of Sample PGO)
143 /// hot total call edge count.
144 /// If it returns false, it either means it is not hot or it is unknown
145 /// (for example, no profile data is available).
146 bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
147                                                   BlockFrequencyInfo &BFI) {
148   if (!F || !computeSummary())
149     return false;
150   if (auto FunctionCount = F->getEntryCount())
151     if (isHotCount(FunctionCount.getCount()))
152       return true;
153 
154   if (hasSampleProfile()) {
155     uint64_t TotalCallCount = 0;
156     for (const auto &BB : *F)
157       for (const auto &I : BB)
158         if (isa<CallInst>(I) || isa<InvokeInst>(I))
159           if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
160             TotalCallCount += CallCount.getValue();
161     if (isHotCount(TotalCallCount))
162       return true;
163   }
164   for (const auto &BB : *F)
165     if (isHotBlock(&BB, &BFI))
166       return true;
167   return false;
168 }
169 
170 /// Returns true if the function only contains cold code. This means that
171 /// the function entry and blocks are all cold, and (in the case of Sample PGO)
172 /// the total call edge count is cold.
173 /// If it returns false, it either means it is not cold or it is unknown
174 /// (for example, no profile data is available).
175 bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
176                                                    BlockFrequencyInfo &BFI) {
177   if (!F || !computeSummary())
178     return false;
179   if (auto FunctionCount = F->getEntryCount())
180     if (!isColdCount(FunctionCount.getCount()))
181       return false;
182 
183   if (hasSampleProfile()) {
184     uint64_t TotalCallCount = 0;
185     for (const auto &BB : *F)
186       for (const auto &I : BB)
187         if (isa<CallInst>(I) || isa<InvokeInst>(I))
188           if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
189             TotalCallCount += CallCount.getValue();
190     if (!isColdCount(TotalCallCount))
191       return false;
192   }
193   for (const auto &BB : *F)
194     if (!isColdBlock(&BB, &BFI))
195       return false;
196   return true;
197 }
198 
199 bool ProfileSummaryInfo::isFunctionHotnessUnknown(const Function &F) {
200   assert(hasPartialSampleProfile() && "Expect partial sample profile");
201   return !F.getEntryCount().hasValue();
202 }
203 
204 template<bool isHot>
205 bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile(
206     int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) {
207   if (!F || !computeSummary())
208     return false;
209   if (auto FunctionCount = F->getEntryCount()) {
210     if (isHot &&
211         isHotCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
212       return true;
213     if (!isHot &&
214         !isColdCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
215       return false;
216   }
217   if (hasSampleProfile()) {
218     uint64_t TotalCallCount = 0;
219     for (const auto &BB : *F)
220       for (const auto &I : BB)
221         if (isa<CallInst>(I) || isa<InvokeInst>(I))
222           if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
223             TotalCallCount += CallCount.getValue();
224     if (isHot && isHotCountNthPercentile(PercentileCutoff, TotalCallCount))
225       return true;
226     if (!isHot && !isColdCountNthPercentile(PercentileCutoff, TotalCallCount))
227       return false;
228   }
229   for (const auto &BB : *F) {
230     if (isHot && isHotBlockNthPercentile(PercentileCutoff, &BB, &BFI))
231       return true;
232     if (!isHot && !isColdBlockNthPercentile(PercentileCutoff, &BB, &BFI))
233       return false;
234   }
235   return !isHot;
236 }
237 
238 // Like isFunctionHotInCallGraph but for a given cutoff.
239 bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile(
240     int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) {
241   return isFunctionHotOrColdInCallGraphNthPercentile<true>(
242       PercentileCutoff, F, BFI);
243 }
244 
245 bool ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile(
246     int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) {
247   return isFunctionHotOrColdInCallGraphNthPercentile<false>(
248       PercentileCutoff, F, BFI);
249 }
250 
251 /// Returns true if the function's entry is a cold. If it returns false, it
252 /// either means it is not cold or it is unknown whether it is cold or not (for
253 /// example, no profile data is available).
254 bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
255   if (!F)
256     return false;
257   if (F->hasFnAttribute(Attribute::Cold))
258     return true;
259   if (!computeSummary())
260     return false;
261   auto FunctionCount = F->getEntryCount();
262   // FIXME: The heuristic used below for determining coldness is based on
263   // preliminary SPEC tuning for inliner. This will eventually be a
264   // convenience method that calls isHotCount.
265   return FunctionCount && isColdCount(FunctionCount.getCount());
266 }
267 
268 /// Compute the hot and cold thresholds.
269 void ProfileSummaryInfo::computeThresholds() {
270   if (!computeSummary())
271     return;
272   auto &DetailedSummary = Summary->getDetailedSummary();
273   auto &HotEntry =
274       getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
275   HotCountThreshold = HotEntry.MinCount;
276   if (ProfileSummaryHotCount.getNumOccurrences() > 0)
277     HotCountThreshold = ProfileSummaryHotCount;
278   auto &ColdEntry =
279       getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
280   ColdCountThreshold = ColdEntry.MinCount;
281   if (ProfileSummaryColdCount.getNumOccurrences() > 0)
282     ColdCountThreshold = ProfileSummaryColdCount;
283   assert(ColdCountThreshold <= HotCountThreshold &&
284          "Cold count threshold cannot exceed hot count threshold!");
285   HasHugeWorkingSetSize =
286       HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
287   HasLargeWorkingSetSize =
288       HotEntry.NumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
289 }
290 
291 Optional<uint64_t> ProfileSummaryInfo::computeThreshold(int PercentileCutoff) {
292   if (!computeSummary())
293     return None;
294   auto iter = ThresholdCache.find(PercentileCutoff);
295   if (iter != ThresholdCache.end()) {
296     return iter->second;
297   }
298   auto &DetailedSummary = Summary->getDetailedSummary();
299   auto &Entry =
300       getEntryForPercentile(DetailedSummary, PercentileCutoff);
301   uint64_t CountThreshold = Entry.MinCount;
302   ThresholdCache[PercentileCutoff] = CountThreshold;
303   return CountThreshold;
304 }
305 
306 bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
307   if (!HasHugeWorkingSetSize)
308     computeThresholds();
309   return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
310 }
311 
312 bool ProfileSummaryInfo::hasLargeWorkingSetSize() {
313   if (!HasLargeWorkingSetSize)
314     computeThresholds();
315   return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue();
316 }
317 
318 bool ProfileSummaryInfo::isHotCount(uint64_t C) {
319   if (!HotCountThreshold)
320     computeThresholds();
321   return HotCountThreshold && C >= HotCountThreshold.getValue();
322 }
323 
324 bool ProfileSummaryInfo::isColdCount(uint64_t C) {
325   if (!ColdCountThreshold)
326     computeThresholds();
327   return ColdCountThreshold && C <= ColdCountThreshold.getValue();
328 }
329 
330 template<bool isHot>
331 bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff,
332                                                        uint64_t C) {
333   auto CountThreshold = computeThreshold(PercentileCutoff);
334   if (isHot)
335     return CountThreshold && C >= CountThreshold.getValue();
336   else
337     return CountThreshold && C <= CountThreshold.getValue();
338 }
339 
340 bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff, uint64_t C) {
341   return isHotOrColdCountNthPercentile<true>(PercentileCutoff, C);
342 }
343 
344 bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff, uint64_t C) {
345   return isHotOrColdCountNthPercentile<false>(PercentileCutoff, C);
346 }
347 
348 uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
349   if (!HotCountThreshold)
350     computeThresholds();
351   return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
352 }
353 
354 uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
355   if (!ColdCountThreshold)
356     computeThresholds();
357   return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
358 }
359 
360 bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) {
361   auto Count = BFI->getBlockProfileCount(BB);
362   return Count && isHotCount(*Count);
363 }
364 
365 bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB,
366                                   BlockFrequencyInfo *BFI) {
367   auto Count = BFI->getBlockProfileCount(BB);
368   return Count && isColdCount(*Count);
369 }
370 
371 template<bool isHot>
372 bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile(int PercentileCutoff,
373                                                        const BasicBlock *BB,
374                                                        BlockFrequencyInfo *BFI) {
375   auto Count = BFI->getBlockProfileCount(BB);
376   if (isHot)
377     return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
378   else
379     return Count && isColdCountNthPercentile(PercentileCutoff, *Count);
380 }
381 
382 bool ProfileSummaryInfo::isHotBlockNthPercentile(int PercentileCutoff,
383                                                  const BasicBlock *BB,
384                                                  BlockFrequencyInfo *BFI) {
385   return isHotOrColdBlockNthPercentile<true>(PercentileCutoff, BB, BFI);
386 }
387 
388 bool ProfileSummaryInfo::isColdBlockNthPercentile(int PercentileCutoff,
389                                                   const BasicBlock *BB,
390                                                   BlockFrequencyInfo *BFI) {
391   return isHotOrColdBlockNthPercentile<false>(PercentileCutoff, BB, BFI);
392 }
393 
394 bool ProfileSummaryInfo::isHotCallSite(const CallBase &CB,
395                                        BlockFrequencyInfo *BFI) {
396   auto C = getProfileCount(CB, BFI);
397   return C && isHotCount(*C);
398 }
399 
400 bool ProfileSummaryInfo::isColdCallSite(const CallBase &CB,
401                                         BlockFrequencyInfo *BFI) {
402   auto C = getProfileCount(CB, BFI);
403   if (C)
404     return isColdCount(*C);
405 
406   // In SamplePGO, if the caller has been sampled, and there is no profile
407   // annotated on the callsite, we consider the callsite as cold.
408   return hasSampleProfile() && CB.getCaller()->hasProfileData();
409 }
410 
411 bool ProfileSummaryInfo::hasPartialSampleProfile() {
412   return hasProfileSummary() &&
413          Summary->getKind() == ProfileSummary::PSK_Sample &&
414          (PartialProfile || Summary->isPartialProfile());
415 }
416 
417 INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
418                 "Profile summary info", false, true)
419 
420 ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
421     : ImmutablePass(ID) {
422   initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
423 }
424 
425 bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
426   PSI.reset(new ProfileSummaryInfo(M));
427   return false;
428 }
429 
430 bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
431   PSI.reset();
432   return false;
433 }
434 
435 AnalysisKey ProfileSummaryAnalysis::Key;
436 ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
437                                                ModuleAnalysisManager &) {
438   return ProfileSummaryInfo(M);
439 }
440 
441 PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
442                                                  ModuleAnalysisManager &AM) {
443   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
444 
445   OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
446   for (auto &F : M) {
447     OS << F.getName();
448     if (PSI.isFunctionEntryHot(&F))
449       OS << " :hot entry ";
450     else if (PSI.isFunctionEntryCold(&F))
451       OS << " :cold entry ";
452     OS << "\n";
453   }
454   return PreservedAnalyses::all();
455 }
456 
457 char ProfileSummaryInfoWrapperPass::ID = 0;
458