Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 1 | //===- 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 Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 17 | #include "llvm/IR/BasicBlock.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 18 | #include "llvm/IR/CallSite.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Metadata.h" |
| 20 | #include "llvm/IR/Module.h" |
| 21 | #include "llvm/IR/ProfileSummary.h" |
| 22 | using 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 | |
| 32 | static cl::opt<int> ProfileSummaryCutoffHot( |
Dehao Chen | 6379951 | 2017-08-04 16:20:54 +0000 | [diff] [blame] | 33 | "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore, |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 34 | cl::desc("A count is hot if it exceeds the minimum count to" |
| 35 | " reach this percentile of total counts.")); |
| 36 | |
| 37 | static 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 Chen | f0e27e6 | 2017-08-24 21:37:04 +0000 | [diff] [blame^] | 42 | static cl::opt<bool> ProfileSampleAccurate( |
| 43 | "profile-sample-accurate", cl::Hidden, cl::init(false), |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 44 | 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 Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 47 | static 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 Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 53 | |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 54 | // Find the summary entry for a desired percentile of counts. |
| 55 | static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS, |
| 56 | uint64_t Percentile) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 57 | auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) { |
| 58 | return Entry.Cutoff < Percentile; |
| 59 | }; |
| 60 | auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare); |
| 61 | // The required percentile has to be <= one of the percentiles in the |
| 62 | // detailed summary. |
| 63 | if (It == DS.end()) |
| 64 | report_fatal_error("Desired percentile exceeds the maximum cutoff"); |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 65 | return *It; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // The profile summary metadata may be attached either by the frontend or by |
| 69 | // any backend passes (IR level instrumentation, for example). This method |
| 70 | // 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] | 71 | // available in the module and parses it to get the Summary object. Returns true |
| 72 | // if a valid Summary is available. |
| 73 | bool ProfileSummaryInfo::computeSummary() { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 74 | if (Summary) |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 75 | return true; |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 76 | auto *SummaryMD = M.getProfileSummary(); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 77 | if (!SummaryMD) |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 78 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 79 | Summary.reset(ProfileSummary::getFromMD(SummaryMD)); |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 80 | return true; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 83 | Optional<uint64_t> |
| 84 | ProfileSummaryInfo::getProfileCount(const Instruction *Inst, |
| 85 | BlockFrequencyInfo *BFI) { |
| 86 | if (!Inst) |
| 87 | return None; |
| 88 | assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) && |
| 89 | "We can only get profile count for call/invoke instruction."); |
Easwaran Raman | dadc0f1 | 2017-05-16 20:14:39 +0000 | [diff] [blame] | 90 | if (hasSampleProfile()) { |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 91 | // In sample PGO mode, check if there is a profile metadata on the |
| 92 | // instruction. If it is present, determine hotness solely based on that, |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 93 | // since the sampled entry count may not be accurate. If there is no |
| 94 | // annotated on the instruction, return None. |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 95 | uint64_t TotalCount; |
| 96 | if (Inst->extractProfTotalWeight(TotalCount)) |
| 97 | return TotalCount; |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 98 | return None; |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 99 | } |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 100 | if (BFI) |
| 101 | return BFI->getBlockProfileCount(Inst->getParent()); |
| 102 | return None; |
| 103 | } |
| 104 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 105 | /// Returns true if the function's entry is hot. If it returns false, it |
| 106 | /// 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] | 107 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 108 | bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) { |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 109 | if (!F || !computeSummary()) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 110 | return false; |
| 111 | auto FunctionCount = F->getEntryCount(); |
| 112 | // FIXME: The heuristic used below for determining hotness is based on |
| 113 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 114 | // convenience method that calls isHotCount. |
Dehao Chen | c87bc79 | 2016-10-11 05:19:00 +0000 | [diff] [blame] | 115 | return FunctionCount && isHotCount(FunctionCount.getValue()); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Dehao Chen | 775341a | 2017-03-23 23:14:11 +0000 | [diff] [blame] | 118 | /// Returns true if the function's entry or total call edge count is hot. |
| 119 | /// If it returns false, it either means it is not hot or it is unknown |
| 120 | /// whether it is hot or not (for example, no profile data is available). |
| 121 | bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F) { |
| 122 | if (!F || !computeSummary()) |
| 123 | return false; |
| 124 | if (auto FunctionCount = F->getEntryCount()) |
| 125 | if (isHotCount(FunctionCount.getValue())) |
| 126 | return true; |
| 127 | |
| 128 | uint64_t TotalCallCount = 0; |
| 129 | for (const auto &BB : *F) |
| 130 | for (const auto &I : BB) |
| 131 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 132 | if (auto CallCount = getProfileCount(&I, nullptr)) |
| 133 | TotalCallCount += CallCount.getValue(); |
| 134 | return isHotCount(TotalCallCount); |
| 135 | } |
| 136 | |
| 137 | /// Returns true if the function's entry and total call edge count is cold. |
| 138 | /// If it returns false, it either means it is not cold or it is unknown |
| 139 | /// whether it is cold or not (for example, no profile data is available). |
| 140 | bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F) { |
| 141 | if (!F || !computeSummary()) |
| 142 | return false; |
| 143 | if (auto FunctionCount = F->getEntryCount()) |
| 144 | if (!isColdCount(FunctionCount.getValue())) |
| 145 | return false; |
| 146 | |
| 147 | uint64_t TotalCallCount = 0; |
| 148 | for (const auto &BB : *F) |
| 149 | for (const auto &I : BB) |
| 150 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 151 | if (auto CallCount = getProfileCount(&I, nullptr)) |
| 152 | TotalCallCount += CallCount.getValue(); |
| 153 | return isColdCount(TotalCallCount); |
| 154 | } |
| 155 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 156 | /// Returns true if the function's entry is a cold. If it returns false, it |
| 157 | /// 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] | 158 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 159 | bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) { |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 160 | if (!F) |
| 161 | return false; |
Davide Italiano | 574e597 | 2017-03-10 20:50:51 +0000 | [diff] [blame] | 162 | if (F->hasFnAttribute(Attribute::Cold)) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 163 | return true; |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 164 | if (!computeSummary()) |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 165 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 166 | auto FunctionCount = F->getEntryCount(); |
| 167 | // FIXME: The heuristic used below for determining coldness is based on |
| 168 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 169 | // convenience method that calls isHotCount. |
Dehao Chen | c87bc79 | 2016-10-11 05:19:00 +0000 | [diff] [blame] | 170 | return FunctionCount && isColdCount(FunctionCount.getValue()); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 173 | /// Compute the hot and cold thresholds. |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 174 | void ProfileSummaryInfo::computeThresholds() { |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 175 | if (!computeSummary()) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 176 | return; |
| 177 | auto &DetailedSummary = Summary->getDetailedSummary(); |
Teresa Johnson | 8482e56 | 2017-08-03 23:42:58 +0000 | [diff] [blame] | 178 | auto &HotEntry = |
| 179 | getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot); |
| 180 | HotCountThreshold = HotEntry.MinCount; |
| 181 | auto &ColdEntry = |
| 182 | getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold); |
| 183 | ColdCountThreshold = ColdEntry.MinCount; |
| 184 | HasHugeWorkingSetSize = |
| 185 | HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold; |
| 186 | } |
| 187 | |
| 188 | bool ProfileSummaryInfo::hasHugeWorkingSetSize() { |
| 189 | if (!HasHugeWorkingSetSize) |
| 190 | computeThresholds(); |
| 191 | return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue(); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | bool ProfileSummaryInfo::isHotCount(uint64_t C) { |
| 195 | if (!HotCountThreshold) |
| 196 | computeThresholds(); |
| 197 | return HotCountThreshold && C >= HotCountThreshold.getValue(); |
| 198 | } |
| 199 | |
| 200 | bool ProfileSummaryInfo::isColdCount(uint64_t C) { |
| 201 | if (!ColdCountThreshold) |
| 202 | computeThresholds(); |
| 203 | return ColdCountThreshold && C <= ColdCountThreshold.getValue(); |
| 204 | } |
| 205 | |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 206 | bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) { |
| 207 | auto Count = BFI->getBlockProfileCount(B); |
Dehao Chen | 22645ee | 2017-03-10 01:44:37 +0000 | [diff] [blame] | 208 | return Count && isHotCount(*Count); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | bool ProfileSummaryInfo::isColdBB(const BasicBlock *B, |
| 212 | BlockFrequencyInfo *BFI) { |
| 213 | auto Count = BFI->getBlockProfileCount(B); |
| 214 | return Count && isColdCount(*Count); |
| 215 | } |
| 216 | |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 217 | bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS, |
| 218 | BlockFrequencyInfo *BFI) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 219 | auto C = getProfileCount(CS.getInstruction(), BFI); |
| 220 | return C && isHotCount(*C); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS, |
| 224 | BlockFrequencyInfo *BFI) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 225 | auto C = getProfileCount(CS.getInstruction(), BFI); |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 226 | if (C) |
| 227 | return isColdCount(*C); |
| 228 | |
| 229 | // In SamplePGO, if the caller has been sampled, and there is no profile |
| 230 | // annotatedon the callsite, we consider the callsite as cold. |
| 231 | // If there is no profile for the caller, and we know the profile is |
| 232 | // accurate, we consider the callsite as cold. |
| 233 | return (hasSampleProfile() && |
Dehao Chen | f0e27e6 | 2017-08-24 21:37:04 +0000 | [diff] [blame^] | 234 | (CS.getCaller()->getEntryCount() || ProfileSampleAccurate || |
| 235 | CS.getCaller()->hasFnAttribute("profile-sample-accurate"))); |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 238 | INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", |
| 239 | "Profile summary info", false, true) |
| 240 | |
| 241 | ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass() |
| 242 | : ImmutablePass(ID) { |
| 243 | initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 244 | } |
| 245 | |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 246 | bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) { |
| 247 | PSI.reset(new ProfileSummaryInfo(M)); |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) { |
| 252 | PSI.reset(); |
| 253 | return false; |
| 254 | } |
| 255 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 256 | AnalysisKey ProfileSummaryAnalysis::Key; |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 257 | ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M, |
| 258 | ModuleAnalysisManager &) { |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 259 | return ProfileSummaryInfo(M); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 262 | PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 263 | ModuleAnalysisManager &AM) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 264 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
| 265 | |
| 266 | OS << "Functions in " << M.getName() << " with hot/cold annotations: \n"; |
| 267 | for (auto &F : M) { |
| 268 | OS << F.getName(); |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 269 | if (PSI.isFunctionEntryHot(&F)) |
| 270 | OS << " :hot entry "; |
| 271 | else if (PSI.isFunctionEntryCold(&F)) |
| 272 | OS << " :cold entry "; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 273 | OS << "\n"; |
| 274 | } |
| 275 | return PreservedAnalyses::all(); |
| 276 | } |
| 277 | |
| 278 | char ProfileSummaryInfoWrapperPass::ID = 0; |