blob: c414b8314a75eb68f22a4b44fe667f7dfe11f055 [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
Dehao Chen38a666d2016-11-09 23:36:02 +000015#include "llvm/Analysis/BlockFrequencyInfo.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000016#include "llvm/Analysis/ProfileSummaryInfo.h"
Dehao Chen38a666d2016-11-09 23:36:02 +000017#include "llvm/IR/BasicBlock.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(
32 "profile-summary-cutoff-hot", cl::Hidden, cl::init(999000), cl::ZeroOrMore,
33 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
41// Find the minimum count to reach a desired percentile of counts.
42static uint64_t getMinCountForPercentile(SummaryEntryVector &DS,
43 uint64_t Percentile) {
44 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
45 return Entry.Cutoff < Percentile;
46 };
47 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
48 // The required percentile has to be <= one of the percentiles in the
49 // detailed summary.
50 if (It == DS.end())
51 report_fatal_error("Desired percentile exceeds the maximum cutoff");
52 return It->MinCount;
53}
54
55// The profile summary metadata may be attached either by the frontend or by
56// any backend passes (IR level instrumentation, for example). This method
57// checks if the Summary is null and if so checks if the summary metadata is now
58// available in the module and parses it to get the Summary object.
59void ProfileSummaryInfo::computeSummary() {
60 if (Summary)
61 return;
Dehao Chen5461d8b2016-09-28 21:00:58 +000062 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman019e0bf2016-06-03 22:54:26 +000063 if (!SummaryMD)
64 return;
65 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
66}
67
Dehao Chen84287ab2016-10-10 21:47:28 +000068/// Returns true if the function's entry is hot. If it returns false, it
69/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000070/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +000071bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000072 computeSummary();
73 if (!F || !Summary)
74 return false;
75 auto FunctionCount = F->getEntryCount();
76 // FIXME: The heuristic used below for determining hotness is based on
77 // preliminary SPEC tuning for inliner. This will eventually be a
78 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +000079 return FunctionCount && isHotCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +000080}
81
Dehao Chen84287ab2016-10-10 21:47:28 +000082/// Returns true if the function's entry is a cold. If it returns false, it
83/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000084/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +000085bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000086 computeSummary();
Richard Trieufae70b62016-06-10 01:42:05 +000087 if (!F)
88 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000089 if (F->hasFnAttribute(Attribute::Cold)) {
90 return true;
91 }
Richard Trieufae70b62016-06-10 01:42:05 +000092 if (!Summary)
93 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000094 auto FunctionCount = F->getEntryCount();
95 // FIXME: The heuristic used below for determining coldness is based on
96 // preliminary SPEC tuning for inliner. This will eventually be a
97 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +000098 return FunctionCount && isColdCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +000099}
100
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000101/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000102void ProfileSummaryInfo::computeThresholds() {
103 if (!Summary)
104 computeSummary();
105 if (!Summary)
106 return;
107 auto &DetailedSummary = Summary->getDetailedSummary();
108 HotCountThreshold =
109 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
110 ColdCountThreshold =
111 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
112}
113
114bool ProfileSummaryInfo::isHotCount(uint64_t C) {
115 if (!HotCountThreshold)
116 computeThresholds();
117 return HotCountThreshold && C >= HotCountThreshold.getValue();
118}
119
120bool ProfileSummaryInfo::isColdCount(uint64_t C) {
121 if (!ColdCountThreshold)
122 computeThresholds();
123 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
124}
125
Dehao Chen38a666d2016-11-09 23:36:02 +0000126bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
127 auto Count = BFI->getBlockProfileCount(B);
128 if (Count && isHotCount(*Count))
129 return true;
130 // Use extractProfTotalWeight to get BB count.
131 // For Sample PGO, BFI may not provide accurate BB count due to errors
132 // magnified during sample count propagation. This serves as a backup plan
133 // to ensure all hot BB will not be missed.
134 // The query currently has false positives as branch instruction cloning does
135 // not update/scale branch weights. Unlike false negatives, this will not cause
136 // performance problem.
137 uint64_t TotalCount;
138 if (B->getTerminator()->extractProfTotalWeight(TotalCount) &&
139 isHotCount(TotalCount))
140 return true;
141 return false;
142}
143
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000144INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
145 "Profile summary info", false, true)
146
147ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
148 : ImmutablePass(ID) {
149 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
150}
151
Dehao Chen5461d8b2016-09-28 21:00:58 +0000152bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
153 PSI.reset(new ProfileSummaryInfo(M));
154 return false;
155}
156
157bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
158 PSI.reset();
159 return false;
160}
161
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000162char ProfileSummaryAnalysis::PassID;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000163ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
164 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000165 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000166}
167
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000168PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000169 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000170 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
171
172 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
173 for (auto &F : M) {
174 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000175 if (PSI.isFunctionEntryHot(&F))
176 OS << " :hot entry ";
177 else if (PSI.isFunctionEntryCold(&F))
178 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000179 OS << "\n";
180 }
181 return PreservedAnalyses::all();
182}
183
184char ProfileSummaryInfoWrapperPass::ID = 0;