Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Analysis/ProfileSummaryInfo.h - profile summary ---*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains a pass that provides access to profile summary |
| 10 | // information. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H |
| 15 | #define LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H |
| 16 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | #include "llvm/IR/PassManager.h" |
| 19 | #include "llvm/IR/ProfileSummary.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
| 21 | #include <memory> |
| 22 | |
| 23 | namespace llvm { |
| 24 | class BasicBlock; |
| 25 | class BlockFrequencyInfo; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 26 | class CallBase; |
| 27 | class Function; |
| 28 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 29 | /// Analysis providing profile information. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 30 | /// |
| 31 | /// This is an immutable analysis pass that provides ability to query global |
| 32 | /// (program-level) profile information. The main APIs are isHotCount and |
| 33 | /// isColdCount that tells whether a given profile count is considered hot/cold |
| 34 | /// based on the profile summary. This also provides convenience methods to |
| 35 | /// check whether a function is hot or cold. |
| 36 | |
| 37 | // FIXME: Provide convenience methods to determine hotness/coldness of other IR |
| 38 | // units. This would require making this depend on BFI. |
| 39 | class ProfileSummaryInfo { |
| 40 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | const Module &M; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | std::unique_ptr<ProfileSummary> Summary; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 43 | void computeThresholds(); |
| 44 | // Count thresholds to answer isHotCount and isColdCount queries. |
| 45 | Optional<uint64_t> HotCountThreshold, ColdCountThreshold; |
| 46 | // True if the working set size of the code is considered huge, |
| 47 | // because the number of profile counts required to reach the hot |
| 48 | // percentile is above a huge threshold. |
| 49 | Optional<bool> HasHugeWorkingSetSize; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 50 | // True if the working set size of the code is considered large, |
| 51 | // because the number of profile counts required to reach the hot |
| 52 | // percentile is above a large threshold. |
| 53 | Optional<bool> HasLargeWorkingSetSize; |
| 54 | // Compute the threshold for a given cutoff. |
| 55 | Optional<uint64_t> computeThreshold(int PercentileCutoff) const; |
| 56 | // The map that caches the threshold values. The keys are the percentile |
| 57 | // cutoff values and the values are the corresponding threshold values. |
| 58 | mutable DenseMap<int, uint64_t> ThresholdCache; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | |
| 60 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 61 | ProfileSummaryInfo(const Module &M) : M(M) { refresh(); } |
| 62 | |
| 63 | ProfileSummaryInfo(ProfileSummaryInfo &&Arg) = default; |
| 64 | |
| 65 | /// If no summary is present, attempt to refresh. |
| 66 | void refresh(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 68 | /// Returns true if profile summary is available. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 69 | bool hasProfileSummary() const { return Summary != nullptr; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | /// Returns true if module \c M has sample profile. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 72 | bool hasSampleProfile() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | return hasProfileSummary() && |
| 74 | Summary->getKind() == ProfileSummary::PSK_Sample; |
| 75 | } |
| 76 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 77 | /// Returns true if module \c M has instrumentation profile. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 78 | bool hasInstrumentationProfile() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | return hasProfileSummary() && |
| 80 | Summary->getKind() == ProfileSummary::PSK_Instr; |
| 81 | } |
| 82 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 83 | /// Returns true if module \c M has context sensitive instrumentation profile. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 84 | bool hasCSInstrumentationProfile() const { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 85 | return hasProfileSummary() && |
| 86 | Summary->getKind() == ProfileSummary::PSK_CSInstr; |
| 87 | } |
| 88 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | /// Handle the invalidation of this information. |
| 90 | /// |
| 91 | /// When used as a result of \c ProfileSummaryAnalysis this method will be |
| 92 | /// called when the module this was computed for changes. Since profile |
| 93 | /// summary is immutable after it is annotated on the module, we return false |
| 94 | /// here. |
| 95 | bool invalidate(Module &, const PreservedAnalyses &, |
| 96 | ModuleAnalysisManager::Invalidator &) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | /// Returns the profile count for \p CallInst. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 101 | Optional<uint64_t> getProfileCount(const CallBase &CallInst, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 102 | BlockFrequencyInfo *BFI, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 103 | bool AllowSynthetic = false) const; |
| 104 | /// Returns true if module \c M has partial-profile sample profile. |
| 105 | bool hasPartialSampleProfile() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 106 | /// Returns true if the working set size of the code is considered huge. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 107 | bool hasHugeWorkingSetSize() const; |
| 108 | /// Returns true if the working set size of the code is considered large. |
| 109 | bool hasLargeWorkingSetSize() const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 110 | /// Returns true if \p F has hot function entry. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 111 | bool isFunctionEntryHot(const Function *F) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 112 | /// Returns true if \p F contains hot code. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 113 | bool isFunctionHotInCallGraph(const Function *F, |
| 114 | BlockFrequencyInfo &BFI) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 115 | /// Returns true if \p F has cold function entry. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 116 | bool isFunctionEntryCold(const Function *F) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | /// Returns true if \p F contains only cold code. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 118 | bool isFunctionColdInCallGraph(const Function *F, |
| 119 | BlockFrequencyInfo &BFI) const; |
| 120 | /// Returns true if the hotness of \p F is unknown. |
| 121 | bool isFunctionHotnessUnknown(const Function &F) const; |
| 122 | /// Returns true if \p F contains hot code with regard to a given hot |
| 123 | /// percentile cutoff value. |
| 124 | bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff, |
| 125 | const Function *F, |
| 126 | BlockFrequencyInfo &BFI) const; |
| 127 | /// Returns true if \p F contains cold code with regard to a given cold |
| 128 | /// percentile cutoff value. |
| 129 | bool isFunctionColdInCallGraphNthPercentile(int PercentileCutoff, |
| 130 | const Function *F, |
| 131 | BlockFrequencyInfo &BFI) const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 132 | /// Returns true if count \p C is considered hot. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 133 | bool isHotCount(uint64_t C) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 134 | /// Returns true if count \p C is considered cold. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 135 | bool isColdCount(uint64_t C) const; |
| 136 | /// Returns true if count \p C is considered hot with regard to a given |
| 137 | /// hot percentile cutoff value. |
| 138 | bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const; |
| 139 | /// Returns true if count \p C is considered cold with regard to a given |
| 140 | /// cold percentile cutoff value. |
| 141 | bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 142 | /// Returns true if BasicBlock \p BB is considered hot. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 143 | bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 144 | /// Returns true if BasicBlock \p BB is considered cold. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 145 | bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const; |
| 146 | /// Returns true if BasicBlock \p BB is considered hot with regard to a given |
| 147 | /// hot percentile cutoff value. |
| 148 | bool isHotBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, |
| 149 | BlockFrequencyInfo *BFI) const; |
| 150 | /// Returns true if BasicBlock \p BB is considered cold with regard to a given |
| 151 | /// cold percentile cutoff value. |
| 152 | bool isColdBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, |
| 153 | BlockFrequencyInfo *BFI) const; |
| 154 | /// Returns true if the call site \p CB is considered hot. |
| 155 | bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const; |
| 156 | /// Returns true if call site \p CB is considered cold. |
| 157 | bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 158 | /// Returns HotCountThreshold if set. Recompute HotCountThreshold |
| 159 | /// if not set. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 160 | uint64_t getOrCompHotCountThreshold() const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 161 | /// Returns ColdCountThreshold if set. Recompute HotCountThreshold |
| 162 | /// if not set. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 163 | uint64_t getOrCompColdCountThreshold() const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 164 | /// Returns HotCountThreshold if set. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 165 | uint64_t getHotCountThreshold() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 166 | return HotCountThreshold ? HotCountThreshold.getValue() : 0; |
| 167 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 168 | /// Returns ColdCountThreshold if set. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 169 | uint64_t getColdCountThreshold() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 170 | return ColdCountThreshold ? ColdCountThreshold.getValue() : 0; |
| 171 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 172 | |
| 173 | private: |
| 174 | template <bool isHot> |
| 175 | bool isFunctionHotOrColdInCallGraphNthPercentile( |
| 176 | int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const; |
| 177 | template <bool isHot> |
| 178 | bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C) const; |
| 179 | template <bool isHot> |
| 180 | bool isHotOrColdBlockNthPercentile(int PercentileCutoff, |
| 181 | const BasicBlock *BB, |
| 182 | BlockFrequencyInfo *BFI) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo. |
| 186 | class ProfileSummaryInfoWrapperPass : public ImmutablePass { |
| 187 | std::unique_ptr<ProfileSummaryInfo> PSI; |
| 188 | |
| 189 | public: |
| 190 | static char ID; |
| 191 | ProfileSummaryInfoWrapperPass(); |
| 192 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 193 | ProfileSummaryInfo &getPSI() { return *PSI; } |
| 194 | const ProfileSummaryInfo &getPSI() const { return *PSI; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 195 | |
| 196 | bool doInitialization(Module &M) override; |
| 197 | bool doFinalization(Module &M) override; |
| 198 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 199 | AU.setPreservesAll(); |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | /// An analysis pass based on the new PM to deliver ProfileSummaryInfo. |
| 204 | class ProfileSummaryAnalysis |
| 205 | : public AnalysisInfoMixin<ProfileSummaryAnalysis> { |
| 206 | public: |
| 207 | typedef ProfileSummaryInfo Result; |
| 208 | |
| 209 | Result run(Module &M, ModuleAnalysisManager &); |
| 210 | |
| 211 | private: |
| 212 | friend AnalysisInfoMixin<ProfileSummaryAnalysis>; |
| 213 | static AnalysisKey Key; |
| 214 | }; |
| 215 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 216 | /// Printer pass that uses \c ProfileSummaryAnalysis. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 217 | class ProfileSummaryPrinterPass |
| 218 | : public PassInfoMixin<ProfileSummaryPrinterPass> { |
| 219 | raw_ostream &OS; |
| 220 | |
| 221 | public: |
| 222 | explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 223 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); |
| 224 | }; |
| 225 | |
| 226 | } // end namespace llvm |
| 227 | |
| 228 | #endif |