blob: 1f9e9813c761a8cbf8110bd826ea9226fdb249f9 [file] [log] [blame]
Easwaran Raman019e0bf2016-06-03 22:54:26 +00001//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Easwaran Raman019e0bf2016-06-03 22:54:26 +00006//
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"
Easwaran Ramanb035f912017-01-13 01:34:00 +000015#include "llvm/Analysis/BlockFrequencyInfo.h"
Dehao Chen38a666d2016-11-09 23:36:02 +000016#include "llvm/IR/BasicBlock.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000017#include "llvm/IR/CallSite.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000018#include "llvm/IR/Metadata.h"
19#include "llvm/IR/Module.h"
20#include "llvm/IR/ProfileSummary.h"
21using namespace llvm;
22
23// The following two parameters determine the threshold for a count to be
24// considered hot/cold. These two parameters are percentile values (multiplied
25// by 10000). If the counts are sorted in descending order, the minimum count to
26// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
27// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
28// threshold for determining cold count (everything <= this threshold is
29// considered cold).
30
31static cl::opt<int> ProfileSummaryCutoffHot(
Dehao Chen63799512017-08-04 16:20:54 +000032 "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
Easwaran Raman019e0bf2016-06-03 22:54:26 +000033 cl::desc("A count is hot if it exceeds the minimum count to"
34 " reach this percentile of total counts."));
35
36static cl::opt<int> ProfileSummaryCutoffCold(
37 "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
38 cl::desc("A count is cold if it is below the minimum count"
39 " to reach this percentile of total counts."));
40
Teresa Johnson8482e562017-08-03 23:42:58 +000041static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
42 "profile-summary-huge-working-set-size-threshold", cl::Hidden,
43 cl::init(15000), cl::ZeroOrMore,
44 cl::desc("The code working set size is considered huge if the number of"
45 " blocks required to reach the -profile-summary-cutoff-hot"
46 " percentile exceeds this count."));
Dehao Chenf58df392017-08-03 17:11:41 +000047
Easwaran Ramanc5e15062018-11-02 17:39:31 +000048// The next two options override the counts derived from summary computation and
49// are useful for debugging purposes.
50static cl::opt<int> ProfileSummaryHotCount(
51 "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
52 cl::desc("A fixed hot count that overrides the count derived from"
53 " profile-summary-cutoff-hot"));
54
55static cl::opt<int> ProfileSummaryColdCount(
56 "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
57 cl::desc("A fixed cold count that overrides the count derived from"
58 " profile-summary-cutoff-cold"));
59
Teresa Johnson8482e562017-08-03 23:42:58 +000060// Find the summary entry for a desired percentile of counts.
61static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
62 uint64_t Percentile) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000063 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
64 return Entry.Cutoff < Percentile;
65 };
66 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
67 // The required percentile has to be <= one of the percentiles in the
68 // detailed summary.
69 if (It == DS.end())
70 report_fatal_error("Desired percentile exceeds the maximum cutoff");
Teresa Johnson8482e562017-08-03 23:42:58 +000071 return *It;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000072}
73
74// The profile summary metadata may be attached either by the frontend or by
75// any backend passes (IR level instrumentation, for example). This method
76// checks if the Summary is null and if so checks if the summary metadata is now
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000077// available in the module and parses it to get the Summary object. Returns true
78// if a valid Summary is available.
79bool ProfileSummaryInfo::computeSummary() {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000080 if (Summary)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000081 return true;
Rong Xua6ff69f2019-02-28 19:55:07 +000082 // First try to get context sensitive ProfileSummary.
83 auto *SummaryMD = M.getProfileSummary(/* IsCS */ true);
84 if (SummaryMD) {
85 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
86 return true;
87 }
88 // This will actually return PSK_Instr or PSK_Sample summary.
89 SummaryMD = M.getProfileSummary(/* IsCS */ false);
Easwaran Raman019e0bf2016-06-03 22:54:26 +000090 if (!SummaryMD)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000091 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000092 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000093 return true;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000094}
95
Dehao Chenc2048152017-03-10 19:45:16 +000096Optional<uint64_t>
97ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
98 BlockFrequencyInfo *BFI) {
99 if (!Inst)
100 return None;
101 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
102 "We can only get profile count for call/invoke instruction.");
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000103 if (hasSampleProfile()) {
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000104 // In sample PGO mode, check if there is a profile metadata on the
105 // instruction. If it is present, determine hotness solely based on that,
Dehao Chenf58df392017-08-03 17:11:41 +0000106 // since the sampled entry count may not be accurate. If there is no
107 // annotated on the instruction, return None.
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000108 uint64_t TotalCount;
109 if (Inst->extractProfTotalWeight(TotalCount))
110 return TotalCount;
Dehao Chenf58df392017-08-03 17:11:41 +0000111 return None;
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000112 }
Dehao Chenc2048152017-03-10 19:45:16 +0000113 if (BFI)
114 return BFI->getBlockProfileCount(Inst->getParent());
115 return None;
116}
117
Dehao Chen84287ab2016-10-10 21:47:28 +0000118/// Returns true if the function's entry is hot. If it returns false, it
119/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000120/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000121bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000122 if (!F || !computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000123 return false;
124 auto FunctionCount = F->getEntryCount();
125 // FIXME: The heuristic used below for determining hotness is based on
126 // preliminary SPEC tuning for inliner. This will eventually be a
127 // convenience method that calls isHotCount.
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000128 return FunctionCount && isHotCount(FunctionCount.getCount());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000129}
130
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000131/// Returns true if the function contains hot code. This can include a hot
132/// function entry count, hot basic block, or (in the case of Sample PGO)
133/// hot total call edge count.
Dehao Chen775341a2017-03-23 23:14:11 +0000134/// If it returns false, it either means it is not hot or it is unknown
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000135/// (for example, no profile data is available).
136bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
137 BlockFrequencyInfo &BFI) {
Dehao Chen775341a2017-03-23 23:14:11 +0000138 if (!F || !computeSummary())
139 return false;
140 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000141 if (isHotCount(FunctionCount.getCount()))
Dehao Chen775341a2017-03-23 23:14:11 +0000142 return true;
143
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000144 if (hasSampleProfile()) {
145 uint64_t TotalCallCount = 0;
146 for (const auto &BB : *F)
147 for (const auto &I : BB)
148 if (isa<CallInst>(I) || isa<InvokeInst>(I))
149 if (auto CallCount = getProfileCount(&I, nullptr))
150 TotalCallCount += CallCount.getValue();
151 if (isHotCount(TotalCallCount))
152 return true;
153 }
Dehao Chen775341a2017-03-23 23:14:11 +0000154 for (const auto &BB : *F)
Vedant Kumare7b789b2018-11-19 05:23:16 +0000155 if (isHotBlock(&BB, &BFI))
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000156 return true;
157 return false;
Dehao Chen775341a2017-03-23 23:14:11 +0000158}
159
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000160/// Returns true if the function only contains cold code. This means that
161/// the function entry and blocks are all cold, and (in the case of Sample PGO)
162/// the total call edge count is cold.
Dehao Chen775341a2017-03-23 23:14:11 +0000163/// If it returns false, it either means it is not cold or it is unknown
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000164/// (for example, no profile data is available).
165bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
166 BlockFrequencyInfo &BFI) {
Dehao Chen775341a2017-03-23 23:14:11 +0000167 if (!F || !computeSummary())
168 return false;
169 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000170 if (!isColdCount(FunctionCount.getCount()))
Dehao Chen775341a2017-03-23 23:14:11 +0000171 return false;
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000172
173 if (hasSampleProfile()) {
174 uint64_t TotalCallCount = 0;
175 for (const auto &BB : *F)
176 for (const auto &I : BB)
177 if (isa<CallInst>(I) || isa<InvokeInst>(I))
178 if (auto CallCount = getProfileCount(&I, nullptr))
179 TotalCallCount += CallCount.getValue();
180 if (!isColdCount(TotalCallCount))
181 return false;
182 }
Dehao Chen775341a2017-03-23 23:14:11 +0000183 for (const auto &BB : *F)
Vedant Kumare7b789b2018-11-19 05:23:16 +0000184 if (!isColdBlock(&BB, &BFI))
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000185 return false;
186 return true;
Dehao Chen775341a2017-03-23 23:14:11 +0000187}
188
Dehao Chen84287ab2016-10-10 21:47:28 +0000189/// Returns true if the function's entry is a cold. If it returns false, it
190/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000191/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000192bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Richard Trieufae70b62016-06-10 01:42:05 +0000193 if (!F)
194 return false;
Davide Italiano574e5972017-03-10 20:50:51 +0000195 if (F->hasFnAttribute(Attribute::Cold))
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000196 return true;
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000197 if (!computeSummary())
Richard Trieufae70b62016-06-10 01:42:05 +0000198 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000199 auto FunctionCount = F->getEntryCount();
200 // FIXME: The heuristic used below for determining coldness is based on
201 // preliminary SPEC tuning for inliner. This will eventually be a
202 // convenience method that calls isHotCount.
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000203 return FunctionCount && isColdCount(FunctionCount.getCount());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000204}
205
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000206/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000207void ProfileSummaryInfo::computeThresholds() {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000208 if (!computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000209 return;
210 auto &DetailedSummary = Summary->getDetailedSummary();
Teresa Johnson8482e562017-08-03 23:42:58 +0000211 auto &HotEntry =
212 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
213 HotCountThreshold = HotEntry.MinCount;
Easwaran Ramanc5e15062018-11-02 17:39:31 +0000214 if (ProfileSummaryHotCount.getNumOccurrences() > 0)
215 HotCountThreshold = ProfileSummaryHotCount;
Teresa Johnson8482e562017-08-03 23:42:58 +0000216 auto &ColdEntry =
217 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
218 ColdCountThreshold = ColdEntry.MinCount;
Easwaran Ramanc5e15062018-11-02 17:39:31 +0000219 if (ProfileSummaryColdCount.getNumOccurrences() > 0)
220 ColdCountThreshold = ProfileSummaryColdCount;
221 assert(ColdCountThreshold <= HotCountThreshold &&
222 "Cold count threshold cannot exceed hot count threshold!");
Teresa Johnson8482e562017-08-03 23:42:58 +0000223 HasHugeWorkingSetSize =
224 HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
225}
226
227bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
228 if (!HasHugeWorkingSetSize)
229 computeThresholds();
230 return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000231}
232
233bool ProfileSummaryInfo::isHotCount(uint64_t C) {
234 if (!HotCountThreshold)
235 computeThresholds();
236 return HotCountThreshold && C >= HotCountThreshold.getValue();
237}
238
239bool ProfileSummaryInfo::isColdCount(uint64_t C) {
240 if (!ColdCountThreshold)
241 computeThresholds();
242 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
243}
244
Wei Mi0c2f6be2018-05-10 23:02:27 +0000245uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
246 if (!HotCountThreshold)
247 computeThresholds();
Wei Mib1ef2cc2018-08-07 18:13:10 +0000248 return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
Wei Mi0c2f6be2018-05-10 23:02:27 +0000249}
250
251uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
252 if (!ColdCountThreshold)
253 computeThresholds();
Wei Mib1ef2cc2018-08-07 18:13:10 +0000254 return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
Wei Mi0c2f6be2018-05-10 23:02:27 +0000255}
256
Vedant Kumare7b789b2018-11-19 05:23:16 +0000257bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) {
258 auto Count = BFI->getBlockProfileCount(BB);
Dehao Chen22645ee2017-03-10 01:44:37 +0000259 return Count && isHotCount(*Count);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000260}
261
Vedant Kumare7b789b2018-11-19 05:23:16 +0000262bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB,
Easwaran Ramanb035f912017-01-13 01:34:00 +0000263 BlockFrequencyInfo *BFI) {
Vedant Kumare7b789b2018-11-19 05:23:16 +0000264 auto Count = BFI->getBlockProfileCount(BB);
Wei Mi66c6c5a2018-12-13 21:51:42 +0000265 return Count && isColdCount(*Count);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000266}
267
Easwaran Ramanb035f912017-01-13 01:34:00 +0000268bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
269 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000270 auto C = getProfileCount(CS.getInstruction(), BFI);
271 return C && isHotCount(*C);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000272}
273
274bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
275 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000276 auto C = getProfileCount(CS.getInstruction(), BFI);
Dehao Chenf58df392017-08-03 17:11:41 +0000277 if (C)
278 return isColdCount(*C);
279
280 // In SamplePGO, if the caller has been sampled, and there is no profile
George Burgess IVceecd452018-04-12 18:36:01 +0000281 // annotated on the callsite, we consider the callsite as cold.
Wei Mi66c6c5a2018-12-13 21:51:42 +0000282 return hasSampleProfile() && CS.getCaller()->hasProfileData();
Dehao Chen38a666d2016-11-09 23:36:02 +0000283}
284
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000285INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
286 "Profile summary info", false, true)
287
288ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
289 : ImmutablePass(ID) {
290 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
291}
292
Dehao Chen5461d8b2016-09-28 21:00:58 +0000293bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
294 PSI.reset(new ProfileSummaryInfo(M));
295 return false;
296}
297
298bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
299 PSI.reset();
300 return false;
301}
302
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000303AnalysisKey ProfileSummaryAnalysis::Key;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000304ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
305 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000306 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000307}
308
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000309PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000310 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000311 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
312
313 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
314 for (auto &F : M) {
315 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000316 if (PSI.isFunctionEntryHot(&F))
317 OS << " :hot entry ";
318 else if (PSI.isFunctionEntryCold(&F))
319 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000320 OS << "\n";
321 }
322 return PreservedAnalyses::all();
323}
324
325char ProfileSummaryInfoWrapperPass::ID = 0;