Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 1 | //===- ProfileSummaryInfo.cpp - Global profile summary information --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 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" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 16 | #include "llvm/IR/BasicBlock.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CallSite.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Metadata.h" |
| 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/IR/ProfileSummary.h" |
| 21 | using 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 | |
| 31 | static cl::opt<int> ProfileSummaryCutoffHot( |
Dehao Chen | 6379951 | 2017-08-04 16:20:54 +0000 | [diff] [blame] | 32 | "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore, |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 33 | cl::desc("A count is hot if it exceeds the minimum count to" |
| 34 | " reach this percentile of total counts.")); |
| 35 | |
| 36 | static 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 Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 41 | static 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 Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 47 | |
Hiroshi Yamauchi | 857424d | 2019-09-24 22:17:51 +0000 | [diff] [blame] | 48 | static cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold( |
| 49 | "profile-summary-large-working-set-size-threshold", cl::Hidden, |
| 50 | cl::init(12500), cl::ZeroOrMore, |
| 51 | cl::desc("The code working set size is considered large if the number of" |
| 52 | " blocks required to reach the -profile-summary-cutoff-hot" |
| 53 | " percentile exceeds this count.")); |
| 54 | |
Easwaran Raman | c5e1506 | 2018-11-02 17:39:31 +0000 | [diff] [blame] | 55 | // The next two options override the counts derived from summary computation and |
| 56 | // are useful for debugging purposes. |
| 57 | static cl::opt<int> ProfileSummaryHotCount( |
| 58 | "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore, |
| 59 | cl::desc("A fixed hot count that overrides the count derived from" |
| 60 | " profile-summary-cutoff-hot")); |
| 61 | |
| 62 | static cl::opt<int> ProfileSummaryColdCount( |
| 63 | "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore, |
| 64 | cl::desc("A fixed cold count that overrides the count derived from" |
| 65 | " profile-summary-cutoff-cold")); |
| 66 | |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 67 | // Find the summary entry for a desired percentile of counts. |
| 68 | static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS, |
| 69 | uint64_t Percentile) { |
Fangrui Song | 78ee2fb | 2019-06-30 11:19:56 +0000 | [diff] [blame] | 70 | auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) { |
| 71 | return Entry.Cutoff < Percentile; |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 72 | }); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 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 Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 77 | return *It; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 78 | } |
| 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 Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 83 | // available in the module and parses it to get the Summary object. Returns true |
| 84 | // if a valid Summary is available. |
| 85 | bool ProfileSummaryInfo::computeSummary() { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 86 | if (Summary) |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 87 | return true; |
Rong Xu | a6ff69f | 2019-02-28 19:55:07 +0000 | [diff] [blame] | 88 | // First try to get context sensitive ProfileSummary. |
| 89 | auto *SummaryMD = M.getProfileSummary(/* IsCS */ true); |
| 90 | if (SummaryMD) { |
| 91 | Summary.reset(ProfileSummary::getFromMD(SummaryMD)); |
| 92 | return true; |
| 93 | } |
| 94 | // This will actually return PSK_Instr or PSK_Sample summary. |
| 95 | SummaryMD = M.getProfileSummary(/* IsCS */ false); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 96 | if (!SummaryMD) |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 97 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 98 | Summary.reset(ProfileSummary::getFromMD(SummaryMD)); |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 99 | return true; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 102 | Optional<uint64_t> |
| 103 | ProfileSummaryInfo::getProfileCount(const Instruction *Inst, |
Xinliang David Li | 499c80b | 2019-04-24 19:51:16 +0000 | [diff] [blame] | 104 | BlockFrequencyInfo *BFI, |
| 105 | bool AllowSynthetic) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 106 | if (!Inst) |
| 107 | return None; |
| 108 | assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) && |
| 109 | "We can only get profile count for call/invoke instruction."); |
Easwaran Raman | dadc0f1 | 2017-05-16 20:14:39 +0000 | [diff] [blame] | 110 | if (hasSampleProfile()) { |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 111 | // In sample PGO mode, check if there is a profile metadata on the |
| 112 | // instruction. If it is present, determine hotness solely based on that, |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 113 | // since the sampled entry count may not be accurate. If there is no |
| 114 | // annotated on the instruction, return None. |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 115 | uint64_t TotalCount; |
| 116 | if (Inst->extractProfTotalWeight(TotalCount)) |
| 117 | return TotalCount; |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 118 | return None; |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 119 | } |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 120 | if (BFI) |
Xinliang David Li | 499c80b | 2019-04-24 19:51:16 +0000 | [diff] [blame] | 121 | return BFI->getBlockProfileCount(Inst->getParent(), AllowSynthetic); |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 122 | return None; |
| 123 | } |
| 124 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 125 | /// Returns true if the function's entry is hot. If it returns false, it |
| 126 | /// either means it is not hot or it is unknown whether it is hot or not (for |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 127 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 128 | bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) { |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 129 | if (!F || !computeSummary()) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 130 | return false; |
| 131 | auto FunctionCount = F->getEntryCount(); |
| 132 | // FIXME: The heuristic used below for determining hotness is based on |
| 133 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 134 | // convenience method that calls isHotCount. |
Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 135 | return FunctionCount && isHotCount(FunctionCount.getCount()); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 138 | /// Returns true if the function contains hot code. This can include a hot |
| 139 | /// function entry count, hot basic block, or (in the case of Sample PGO) |
| 140 | /// hot total call edge count. |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 141 | /// If it returns false, it either means it is not hot or it is unknown |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 142 | /// (for example, no profile data is available). |
| 143 | bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F, |
| 144 | BlockFrequencyInfo &BFI) { |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 145 | if (!F || !computeSummary()) |
| 146 | return false; |
| 147 | if (auto FunctionCount = F->getEntryCount()) |
Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 148 | if (isHotCount(FunctionCount.getCount())) |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 149 | return true; |
| 150 | |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 151 | if (hasSampleProfile()) { |
| 152 | uint64_t TotalCallCount = 0; |
| 153 | for (const auto &BB : *F) |
| 154 | for (const auto &I : BB) |
| 155 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 156 | if (auto CallCount = getProfileCount(&I, nullptr)) |
| 157 | TotalCallCount += CallCount.getValue(); |
| 158 | if (isHotCount(TotalCallCount)) |
| 159 | return true; |
| 160 | } |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 161 | for (const auto &BB : *F) |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 162 | if (isHotBlock(&BB, &BFI)) |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 163 | return true; |
| 164 | return false; |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 167 | /// Returns true if the function only contains cold code. This means that |
| 168 | /// the function entry and blocks are all cold, and (in the case of Sample PGO) |
| 169 | /// the total call edge count is cold. |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 170 | /// If it returns false, it either means it is not cold or it is unknown |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 171 | /// (for example, no profile data is available). |
| 172 | bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F, |
| 173 | BlockFrequencyInfo &BFI) { |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 174 | if (!F || !computeSummary()) |
| 175 | return false; |
| 176 | if (auto FunctionCount = F->getEntryCount()) |
Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 177 | if (!isColdCount(FunctionCount.getCount())) |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 178 | return false; |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 179 | |
| 180 | if (hasSampleProfile()) { |
| 181 | uint64_t TotalCallCount = 0; |
| 182 | for (const auto &BB : *F) |
| 183 | for (const auto &I : BB) |
| 184 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 185 | if (auto CallCount = getProfileCount(&I, nullptr)) |
| 186 | TotalCallCount += CallCount.getValue(); |
| 187 | if (!isColdCount(TotalCallCount)) |
| 188 | return false; |
| 189 | } |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 190 | for (const auto &BB : *F) |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 191 | if (!isColdBlock(&BB, &BFI)) |
Teresa Johnson | a4ce3bf | 2017-12-20 17:53:10 +0000 | [diff] [blame] | 192 | return false; |
| 193 | return true; |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Hiroshi Yamauchi | 857424d | 2019-09-24 22:17:51 +0000 | [diff] [blame] | 196 | // Like isFunctionHotInCallGraph but for a given cutoff. |
| 197 | bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile( |
| 198 | int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) { |
| 199 | if (!F || !computeSummary()) |
| 200 | return false; |
| 201 | if (auto FunctionCount = F->getEntryCount()) |
| 202 | if (isHotCountNthPercentile(PercentileCutoff, FunctionCount.getCount())) |
| 203 | return true; |
| 204 | |
| 205 | if (hasSampleProfile()) { |
| 206 | uint64_t TotalCallCount = 0; |
| 207 | for (const auto &BB : *F) |
| 208 | for (const auto &I : BB) |
| 209 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 210 | if (auto CallCount = getProfileCount(&I, nullptr)) |
| 211 | TotalCallCount += CallCount.getValue(); |
| 212 | if (isHotCountNthPercentile(PercentileCutoff, TotalCallCount)) |
| 213 | return true; |
| 214 | } |
| 215 | for (const auto &BB : *F) |
| 216 | if (isHotBlockNthPercentile(PercentileCutoff, &BB, &BFI)) |
| 217 | return true; |
| 218 | return false; |
| 219 | } |
| 220 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 221 | /// Returns true if the function's entry is a cold. If it returns false, it |
| 222 | /// either means it is not cold or it is unknown whether it is cold or not (for |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 223 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 224 | bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) { |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 225 | if (!F) |
| 226 | return false; |
Davide Italiano | 574e597 | 2017-03-10 20:50:51 +0000 | [diff] [blame] | 227 | if (F->hasFnAttribute(Attribute::Cold)) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 228 | return true; |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 229 | if (!computeSummary()) |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 230 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 231 | auto FunctionCount = F->getEntryCount(); |
| 232 | // FIXME: The heuristic used below for determining coldness is based on |
| 233 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 234 | // convenience method that calls isHotCount. |
Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 235 | return FunctionCount && isColdCount(FunctionCount.getCount()); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 238 | /// Compute the hot and cold thresholds. |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 239 | void ProfileSummaryInfo::computeThresholds() { |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 240 | if (!computeSummary()) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 241 | return; |
| 242 | auto &DetailedSummary = Summary->getDetailedSummary(); |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 243 | auto &HotEntry = |
| 244 | getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot); |
| 245 | HotCountThreshold = HotEntry.MinCount; |
Easwaran Raman | c5e1506 | 2018-11-02 17:39:31 +0000 | [diff] [blame] | 246 | if (ProfileSummaryHotCount.getNumOccurrences() > 0) |
| 247 | HotCountThreshold = ProfileSummaryHotCount; |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 248 | auto &ColdEntry = |
| 249 | getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold); |
| 250 | ColdCountThreshold = ColdEntry.MinCount; |
Easwaran Raman | c5e1506 | 2018-11-02 17:39:31 +0000 | [diff] [blame] | 251 | if (ProfileSummaryColdCount.getNumOccurrences() > 0) |
| 252 | ColdCountThreshold = ProfileSummaryColdCount; |
| 253 | assert(ColdCountThreshold <= HotCountThreshold && |
| 254 | "Cold count threshold cannot exceed hot count threshold!"); |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 255 | HasHugeWorkingSetSize = |
| 256 | HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold; |
Hiroshi Yamauchi | 857424d | 2019-09-24 22:17:51 +0000 | [diff] [blame] | 257 | HasLargeWorkingSetSize = |
| 258 | HotEntry.NumCounts > ProfileSummaryLargeWorkingSetSizeThreshold; |
| 259 | } |
| 260 | |
| 261 | Optional<uint64_t> ProfileSummaryInfo::computeThreshold(int PercentileCutoff) { |
| 262 | if (!computeSummary()) |
| 263 | return None; |
| 264 | auto iter = ThresholdCache.find(PercentileCutoff); |
| 265 | if (iter != ThresholdCache.end()) { |
| 266 | return iter->second; |
| 267 | } |
| 268 | auto &DetailedSummary = Summary->getDetailedSummary(); |
| 269 | auto &Entry = |
| 270 | getEntryForPercentile(DetailedSummary, PercentileCutoff); |
| 271 | uint64_t CountThreshold = Entry.MinCount; |
| 272 | ThresholdCache[PercentileCutoff] = CountThreshold; |
| 273 | return CountThreshold; |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | bool ProfileSummaryInfo::hasHugeWorkingSetSize() { |
| 277 | if (!HasHugeWorkingSetSize) |
| 278 | computeThresholds(); |
| 279 | return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue(); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Hiroshi Yamauchi | 857424d | 2019-09-24 22:17:51 +0000 | [diff] [blame] | 282 | bool ProfileSummaryInfo::hasLargeWorkingSetSize() { |
| 283 | if (!HasLargeWorkingSetSize) |
| 284 | computeThresholds(); |
| 285 | return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue(); |
| 286 | } |
| 287 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 288 | bool ProfileSummaryInfo::isHotCount(uint64_t C) { |
| 289 | if (!HotCountThreshold) |
| 290 | computeThresholds(); |
| 291 | return HotCountThreshold && C >= HotCountThreshold.getValue(); |
| 292 | } |
| 293 | |
| 294 | bool ProfileSummaryInfo::isColdCount(uint64_t C) { |
| 295 | if (!ColdCountThreshold) |
| 296 | computeThresholds(); |
| 297 | return ColdCountThreshold && C <= ColdCountThreshold.getValue(); |
| 298 | } |
| 299 | |
Hiroshi Yamauchi | 857424d | 2019-09-24 22:17:51 +0000 | [diff] [blame] | 300 | bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff, uint64_t C) { |
| 301 | auto CountThreshold = computeThreshold(PercentileCutoff); |
| 302 | return CountThreshold && C >= CountThreshold.getValue(); |
| 303 | } |
| 304 | |
Wei Mi | 0c2f6be | 2018-05-10 23:02:27 +0000 | [diff] [blame] | 305 | uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() { |
| 306 | if (!HotCountThreshold) |
| 307 | computeThresholds(); |
Wei Mi | b1ef2cc | 2018-08-07 18:13:10 +0000 | [diff] [blame] | 308 | return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX; |
Wei Mi | 0c2f6be | 2018-05-10 23:02:27 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() { |
| 312 | if (!ColdCountThreshold) |
| 313 | computeThresholds(); |
Wei Mi | b1ef2cc | 2018-08-07 18:13:10 +0000 | [diff] [blame] | 314 | return ColdCountThreshold ? ColdCountThreshold.getValue() : 0; |
Wei Mi | 0c2f6be | 2018-05-10 23:02:27 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 317 | bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) { |
| 318 | auto Count = BFI->getBlockProfileCount(BB); |
Dehao Chen | 22645ee | 2017-03-10 01:44:37 +0000 | [diff] [blame] | 319 | return Count && isHotCount(*Count); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 322 | bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB, |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 323 | BlockFrequencyInfo *BFI) { |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 324 | auto Count = BFI->getBlockProfileCount(BB); |
Wei Mi | 66c6c5a | 2018-12-13 21:51:42 +0000 | [diff] [blame] | 325 | return Count && isColdCount(*Count); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Hiroshi Yamauchi | 857424d | 2019-09-24 22:17:51 +0000 | [diff] [blame] | 328 | bool ProfileSummaryInfo::isHotBlockNthPercentile(int PercentileCutoff, |
| 329 | const BasicBlock *BB, |
| 330 | BlockFrequencyInfo *BFI) { |
| 331 | auto Count = BFI->getBlockProfileCount(BB); |
| 332 | return Count && isHotCountNthPercentile(PercentileCutoff, *Count); |
| 333 | } |
| 334 | |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 335 | bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS, |
| 336 | BlockFrequencyInfo *BFI) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 337 | auto C = getProfileCount(CS.getInstruction(), BFI); |
| 338 | return C && isHotCount(*C); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS, |
| 342 | BlockFrequencyInfo *BFI) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 343 | auto C = getProfileCount(CS.getInstruction(), BFI); |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 344 | if (C) |
| 345 | return isColdCount(*C); |
| 346 | |
| 347 | // In SamplePGO, if the caller has been sampled, and there is no profile |
George Burgess IV | ceecd45 | 2018-04-12 18:36:01 +0000 | [diff] [blame] | 348 | // annotated on the callsite, we consider the callsite as cold. |
Wei Mi | 66c6c5a | 2018-12-13 21:51:42 +0000 | [diff] [blame] | 349 | return hasSampleProfile() && CS.getCaller()->hasProfileData(); |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 352 | INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", |
| 353 | "Profile summary info", false, true) |
| 354 | |
| 355 | ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass() |
| 356 | : ImmutablePass(ID) { |
| 357 | initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 358 | } |
| 359 | |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 360 | bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) { |
| 361 | PSI.reset(new ProfileSummaryInfo(M)); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) { |
| 366 | PSI.reset(); |
| 367 | return false; |
| 368 | } |
| 369 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 370 | AnalysisKey ProfileSummaryAnalysis::Key; |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 371 | ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M, |
| 372 | ModuleAnalysisManager &) { |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 373 | return ProfileSummaryInfo(M); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 376 | PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 377 | ModuleAnalysisManager &AM) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 378 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
| 379 | |
| 380 | OS << "Functions in " << M.getName() << " with hot/cold annotations: \n"; |
| 381 | for (auto &F : M) { |
| 382 | OS << F.getName(); |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 383 | if (PSI.isFunctionEntryHot(&F)) |
| 384 | OS << " :hot entry "; |
| 385 | else if (PSI.isFunctionEntryCold(&F)) |
| 386 | OS << " :cold entry "; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 387 | OS << "\n"; |
| 388 | } |
| 389 | return PreservedAnalyses::all(); |
| 390 | } |
| 391 | |
| 392 | char ProfileSummaryInfoWrapperPass::ID = 0; |