blob: 7472b6201c2b3e36a08f087c64f95e30d0712392 [file] [log] [blame]
Easwaran Raman019e0bf2016-06-03 22:54:26 +00001//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that provides access to the global profile summary
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/ProfileSummaryInfo.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000016#include "llvm/Analysis/BlockFrequencyInfo.h"
Dehao Chen38a666d2016-11-09 23:36:02 +000017#include "llvm/IR/BasicBlock.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000018#include "llvm/IR/CallSite.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000019#include "llvm/IR/Metadata.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/ProfileSummary.h"
22using namespace llvm;
23
24// The following two parameters determine the threshold for a count to be
25// considered hot/cold. These two parameters are percentile values (multiplied
26// by 10000). If the counts are sorted in descending order, the minimum count to
27// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
28// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
29// threshold for determining cold count (everything <= this threshold is
30// considered cold).
31
32static cl::opt<int> ProfileSummaryCutoffHot(
Dehao Chen63799512017-08-04 16:20:54 +000033 "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
Easwaran Raman019e0bf2016-06-03 22:54:26 +000034 cl::desc("A count is hot if it exceeds the minimum count to"
35 " reach this percentile of total counts."));
36
37static cl::opt<int> ProfileSummaryCutoffCold(
38 "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
39 cl::desc("A count is cold if it is below the minimum count"
40 " to reach this percentile of total counts."));
41
Dehao Chenf0e27e62017-08-24 21:37:04 +000042static cl::opt<bool> ProfileSampleAccurate(
43 "profile-sample-accurate", cl::Hidden, cl::init(false),
Dehao Chenf58df392017-08-03 17:11:41 +000044 cl::desc("If the sample profile is accurate, we will mark all un-sampled "
45 "callsite as cold. Otherwise, treat un-sampled callsites as if "
46 "we have no profile."));
Teresa Johnson8482e562017-08-03 23:42:58 +000047static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
48 "profile-summary-huge-working-set-size-threshold", cl::Hidden,
49 cl::init(15000), cl::ZeroOrMore,
50 cl::desc("The code working set size is considered huge if the number of"
51 " blocks required to reach the -profile-summary-cutoff-hot"
52 " percentile exceeds this count."));
Dehao Chenf58df392017-08-03 17:11:41 +000053
Easwaran Ramanc5e15062018-11-02 17:39:31 +000054// The next two options override the counts derived from summary computation and
55// are useful for debugging purposes.
56static cl::opt<int> ProfileSummaryHotCount(
57 "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
58 cl::desc("A fixed hot count that overrides the count derived from"
59 " profile-summary-cutoff-hot"));
60
61static cl::opt<int> ProfileSummaryColdCount(
62 "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
63 cl::desc("A fixed cold count that overrides the count derived from"
64 " profile-summary-cutoff-cold"));
65
Teresa Johnson8482e562017-08-03 23:42:58 +000066// Find the summary entry for a desired percentile of counts.
67static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
68 uint64_t Percentile) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000069 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
70 return Entry.Cutoff < Percentile;
71 };
72 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
73 // The required percentile has to be <= one of the percentiles in the
74 // detailed summary.
75 if (It == DS.end())
76 report_fatal_error("Desired percentile exceeds the maximum cutoff");
Teresa Johnson8482e562017-08-03 23:42:58 +000077 return *It;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000078}
79
80// The profile summary metadata may be attached either by the frontend or by
81// any backend passes (IR level instrumentation, for example). This method
82// checks if the Summary is null and if so checks if the summary metadata is now
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000083// available in the module and parses it to get the Summary object. Returns true
84// if a valid Summary is available.
85bool ProfileSummaryInfo::computeSummary() {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000086 if (Summary)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000087 return true;
Dehao Chen5461d8b2016-09-28 21:00:58 +000088 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman019e0bf2016-06-03 22:54:26 +000089 if (!SummaryMD)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000090 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000091 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000092 return true;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000093}
94
Dehao Chenc2048152017-03-10 19:45:16 +000095Optional<uint64_t>
96ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
97 BlockFrequencyInfo *BFI) {
98 if (!Inst)
99 return None;
100 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
101 "We can only get profile count for call/invoke instruction.");
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000102 if (hasSampleProfile()) {
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000103 // In sample PGO mode, check if there is a profile metadata on the
104 // instruction. If it is present, determine hotness solely based on that,
Dehao Chenf58df392017-08-03 17:11:41 +0000105 // since the sampled entry count may not be accurate. If there is no
106 // annotated on the instruction, return None.
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000107 uint64_t TotalCount;
108 if (Inst->extractProfTotalWeight(TotalCount))
109 return TotalCount;
Dehao Chenf58df392017-08-03 17:11:41 +0000110 return None;
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000111 }
Dehao Chenc2048152017-03-10 19:45:16 +0000112 if (BFI)
113 return BFI->getBlockProfileCount(Inst->getParent());
114 return None;
115}
116
Dehao Chen84287ab2016-10-10 21:47:28 +0000117/// Returns true if the function's entry is hot. If it returns false, it
118/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000119/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000120bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000121 if (!F || !computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000122 return false;
123 auto FunctionCount = F->getEntryCount();
124 // FIXME: The heuristic used below for determining hotness is based on
125 // preliminary SPEC tuning for inliner. This will eventually be a
126 // convenience method that calls isHotCount.
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000127 return FunctionCount && isHotCount(FunctionCount.getCount());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000128}
129
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000130/// Returns true if the function contains hot code. This can include a hot
131/// function entry count, hot basic block, or (in the case of Sample PGO)
132/// hot total call edge count.
Dehao Chen775341a2017-03-23 23:14:11 +0000133/// If it returns false, it either means it is not hot or it is unknown
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000134/// (for example, no profile data is available).
135bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
136 BlockFrequencyInfo &BFI) {
Dehao Chen775341a2017-03-23 23:14:11 +0000137 if (!F || !computeSummary())
138 return false;
139 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000140 if (isHotCount(FunctionCount.getCount()))
Dehao Chen775341a2017-03-23 23:14:11 +0000141 return true;
142
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000143 if (hasSampleProfile()) {
144 uint64_t TotalCallCount = 0;
145 for (const auto &BB : *F)
146 for (const auto &I : BB)
147 if (isa<CallInst>(I) || isa<InvokeInst>(I))
148 if (auto CallCount = getProfileCount(&I, nullptr))
149 TotalCallCount += CallCount.getValue();
150 if (isHotCount(TotalCallCount))
151 return true;
152 }
Dehao Chen775341a2017-03-23 23:14:11 +0000153 for (const auto &BB : *F)
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000154 if (isHotBB(&BB, &BFI))
155 return true;
156 return false;
Dehao Chen775341a2017-03-23 23:14:11 +0000157}
158
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000159/// Returns true if the function only contains cold code. This means that
160/// the function entry and blocks are all cold, and (in the case of Sample PGO)
161/// the total call edge count is cold.
Dehao Chen775341a2017-03-23 23:14:11 +0000162/// If it returns false, it either means it is not cold or it is unknown
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000163/// (for example, no profile data is available).
164bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
165 BlockFrequencyInfo &BFI) {
Dehao Chen775341a2017-03-23 23:14:11 +0000166 if (!F || !computeSummary())
167 return false;
168 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000169 if (!isColdCount(FunctionCount.getCount()))
Dehao Chen775341a2017-03-23 23:14:11 +0000170 return false;
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000171
172 if (hasSampleProfile()) {
173 uint64_t TotalCallCount = 0;
174 for (const auto &BB : *F)
175 for (const auto &I : BB)
176 if (isa<CallInst>(I) || isa<InvokeInst>(I))
177 if (auto CallCount = getProfileCount(&I, nullptr))
178 TotalCallCount += CallCount.getValue();
179 if (!isColdCount(TotalCallCount))
180 return false;
181 }
Dehao Chen775341a2017-03-23 23:14:11 +0000182 for (const auto &BB : *F)
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000183 if (!isColdBB(&BB, &BFI))
184 return false;
185 return true;
Dehao Chen775341a2017-03-23 23:14:11 +0000186}
187
Dehao Chen84287ab2016-10-10 21:47:28 +0000188/// Returns true if the function's entry is a cold. If it returns false, it
189/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000190/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000191bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Richard Trieufae70b62016-06-10 01:42:05 +0000192 if (!F)
193 return false;
Davide Italiano574e5972017-03-10 20:50:51 +0000194 if (F->hasFnAttribute(Attribute::Cold))
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000195 return true;
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000196 if (!computeSummary())
Richard Trieufae70b62016-06-10 01:42:05 +0000197 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000198 auto FunctionCount = F->getEntryCount();
199 // FIXME: The heuristic used below for determining coldness is based on
200 // preliminary SPEC tuning for inliner. This will eventually be a
201 // convenience method that calls isHotCount.
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000202 return FunctionCount && isColdCount(FunctionCount.getCount());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000203}
204
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000205/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000206void ProfileSummaryInfo::computeThresholds() {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000207 if (!computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000208 return;
209 auto &DetailedSummary = Summary->getDetailedSummary();
Teresa Johnson8482e562017-08-03 23:42:58 +0000210 auto &HotEntry =
211 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
212 HotCountThreshold = HotEntry.MinCount;
Easwaran Ramanc5e15062018-11-02 17:39:31 +0000213 if (ProfileSummaryHotCount.getNumOccurrences() > 0)
214 HotCountThreshold = ProfileSummaryHotCount;
Teresa Johnson8482e562017-08-03 23:42:58 +0000215 auto &ColdEntry =
216 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
217 ColdCountThreshold = ColdEntry.MinCount;
Easwaran Ramanc5e15062018-11-02 17:39:31 +0000218 if (ProfileSummaryColdCount.getNumOccurrences() > 0)
219 ColdCountThreshold = ProfileSummaryColdCount;
220 assert(ColdCountThreshold <= HotCountThreshold &&
221 "Cold count threshold cannot exceed hot count threshold!");
Teresa Johnson8482e562017-08-03 23:42:58 +0000222 HasHugeWorkingSetSize =
223 HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
224}
225
226bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
227 if (!HasHugeWorkingSetSize)
228 computeThresholds();
229 return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000230}
231
232bool ProfileSummaryInfo::isHotCount(uint64_t C) {
233 if (!HotCountThreshold)
234 computeThresholds();
235 return HotCountThreshold && C >= HotCountThreshold.getValue();
236}
237
238bool ProfileSummaryInfo::isColdCount(uint64_t C) {
239 if (!ColdCountThreshold)
240 computeThresholds();
241 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
242}
243
Wei Mi0c2f6be2018-05-10 23:02:27 +0000244uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
245 if (!HotCountThreshold)
246 computeThresholds();
Wei Mib1ef2cc2018-08-07 18:13:10 +0000247 return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
Wei Mi0c2f6be2018-05-10 23:02:27 +0000248}
249
250uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
251 if (!ColdCountThreshold)
252 computeThresholds();
Wei Mib1ef2cc2018-08-07 18:13:10 +0000253 return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
Wei Mi0c2f6be2018-05-10 23:02:27 +0000254}
255
Dehao Chen38a666d2016-11-09 23:36:02 +0000256bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
257 auto Count = BFI->getBlockProfileCount(B);
Dehao Chen22645ee2017-03-10 01:44:37 +0000258 return Count && isHotCount(*Count);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000259}
260
261bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
262 BlockFrequencyInfo *BFI) {
263 auto Count = BFI->getBlockProfileCount(B);
264 return Count && isColdCount(*Count);
265}
266
Easwaran Ramanb035f912017-01-13 01:34:00 +0000267bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
268 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000269 auto C = getProfileCount(CS.getInstruction(), BFI);
270 return C && isHotCount(*C);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000271}
272
273bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
274 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000275 auto C = getProfileCount(CS.getInstruction(), BFI);
Dehao Chenf58df392017-08-03 17:11:41 +0000276 if (C)
277 return isColdCount(*C);
278
279 // In SamplePGO, if the caller has been sampled, and there is no profile
George Burgess IVceecd452018-04-12 18:36:01 +0000280 // annotated on the callsite, we consider the callsite as cold.
Dehao Chenf58df392017-08-03 17:11:41 +0000281 // If there is no profile for the caller, and we know the profile is
282 // accurate, we consider the callsite as cold.
283 return (hasSampleProfile() &&
Easwaran Ramana17f2202017-12-22 01:33:52 +0000284 (CS.getCaller()->hasProfileData() || ProfileSampleAccurate ||
Dehao Chenf0e27e62017-08-24 21:37:04 +0000285 CS.getCaller()->hasFnAttribute("profile-sample-accurate")));
Dehao Chen38a666d2016-11-09 23:36:02 +0000286}
287
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000288INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
289 "Profile summary info", false, true)
290
291ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
292 : ImmutablePass(ID) {
293 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
294}
295
Dehao Chen5461d8b2016-09-28 21:00:58 +0000296bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
297 PSI.reset(new ProfileSummaryInfo(M));
298 return false;
299}
300
301bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
302 PSI.reset();
303 return false;
304}
305
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000306AnalysisKey ProfileSummaryAnalysis::Key;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000307ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
308 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000309 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000310}
311
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000312PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000313 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000314 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
315
316 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
317 for (auto &F : M) {
318 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000319 if (PSI.isFunctionEntryHot(&F))
320 OS << " :hot entry ";
321 else if (PSI.isFunctionEntryCold(&F))
322 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000323 OS << "\n";
324 }
325 return PreservedAnalyses::all();
326}
327
328char ProfileSummaryInfoWrapperPass::ID = 0;