Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 1 | //===- SampleProfile.cpp - Incorporate sample profiles into the IR --------===// |
| 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 implements the SampleProfileLoader transformation. This pass |
| 11 | // reads a profile file generated by a sampling profiler (e.g. Linux Perf - |
| 12 | // http://perf.wiki.kernel.org/) and generates IR metadata to reflect the |
| 13 | // profile information in the given profile. |
| 14 | // |
| 15 | // This pass generates branch weight annotations on the IR: |
| 16 | // |
| 17 | // - prof: Represents branch weights. This annotation is added to branches |
| 18 | // to indicate the weights of each edge coming out of the branch. |
| 19 | // The weight of each edge is the weight of the target block for |
| 20 | // that edge. The weight of a block B is computed as the maximum |
| 21 | // number of samples found in B. |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseMap.h" |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringRef.h" |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/PostDominators.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DebugInfo.h" |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Function.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 36 | #include "llvm/IR/InstIterator.h" |
Dehao Chen | a8bae82 | 2016-04-20 23:36:23 +0000 | [diff] [blame] | 37 | #include "llvm/IR/IntrinsicInst.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Instructions.h" |
| 39 | #include "llvm/IR/LLVMContext.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 40 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Metadata.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Module.h" |
| 43 | #include "llvm/Pass.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 44 | #include "llvm/ProfileData/SampleProfReader.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
| 46 | #include "llvm/Support/Debug.h" |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ErrorOr.h" |
Diego Novillo | 7ff0a17 | 2015-11-29 18:23:26 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Format.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/IPO.h" |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/Utils/Cloning.h" |
Logan Chien | 61c6df0 | 2014-02-22 06:34:10 +0000 | [diff] [blame] | 52 | #include <cctype> |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace llvm; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 55 | using namespace sampleprof; |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 56 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 57 | #define DEBUG_TYPE "sample-profile" |
| 58 | |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 59 | // Command line option to specify the file to read samples from. This is |
| 60 | // mainly used for debugging. |
| 61 | static cl::opt<std::string> SampleProfileFile( |
| 62 | "sample-profile-file", cl::init(""), cl::value_desc("filename"), |
| 63 | cl::desc("Profile file loaded by -sample-profile"), cl::Hidden); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 64 | static cl::opt<unsigned> SampleProfileMaxPropagateIterations( |
| 65 | "sample-profile-max-propagate-iterations", cl::init(100), |
| 66 | cl::desc("Maximum number of iterations to go through when propagating " |
| 67 | "sample block/edge weights through the CFG.")); |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 68 | static cl::opt<unsigned> SampleProfileRecordCoverage( |
| 69 | "sample-profile-check-record-coverage", cl::init(0), cl::value_desc("N"), |
| 70 | cl::desc("Emit a warning if less than N% of records in the input profile " |
| 71 | "are matched to the IR.")); |
| 72 | static cl::opt<unsigned> SampleProfileSampleCoverage( |
| 73 | "sample-profile-check-sample-coverage", cl::init(0), cl::value_desc("N"), |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 74 | cl::desc("Emit a warning if less than N% of samples in the input profile " |
| 75 | "are matched to the IR.")); |
Diego Novillo | b579240 | 2015-11-27 23:14:49 +0000 | [diff] [blame] | 76 | static cl::opt<double> SampleProfileHotThreshold( |
| 77 | "sample-profile-inline-hot-threshold", cl::init(0.1), cl::value_desc("N"), |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 78 | cl::desc("Inlined functions that account for more than N% of all samples " |
| 79 | "collected in the parent function, will be inlined again.")); |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 80 | static cl::opt<double> SampleProfileGlobalHotThreshold( |
| 81 | "sample-profile-global-hot-threshold", cl::init(30), cl::value_desc("N"), |
| 82 | cl::desc("Top-level functions that account for more than N% of all samples " |
| 83 | "collected in the profile, will be marked as hot for the inliner " |
| 84 | "to consider.")); |
| 85 | static cl::opt<double> SampleProfileGlobalColdThreshold( |
| 86 | "sample-profile-global-cold-threshold", cl::init(0.5), cl::value_desc("N"), |
| 87 | cl::desc("Top-level functions that account for less than N% of all samples " |
| 88 | "collected in the profile, will be marked as cold for the inliner " |
| 89 | "to consider.")); |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 90 | |
| 91 | namespace { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 92 | typedef DenseMap<const BasicBlock *, uint64_t> BlockWeightMap; |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 93 | typedef DenseMap<const BasicBlock *, const BasicBlock *> EquivalenceClassMap; |
| 94 | typedef std::pair<const BasicBlock *, const BasicBlock *> Edge; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 95 | typedef DenseMap<Edge, uint64_t> EdgeWeightMap; |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 96 | typedef DenseMap<const BasicBlock *, SmallVector<const BasicBlock *, 8>> |
| 97 | BlockEdgeMap; |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 98 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 99 | /// \brief Sample profile pass. |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 100 | /// |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 101 | /// This pass reads profile data from the file specified by |
| 102 | /// -sample-profile-file and annotates every affected function with the |
| 103 | /// profile information found in that file. |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 104 | class SampleProfileLoader { |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 105 | public: |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 106 | SampleProfileLoader(StringRef Name = SampleProfileFile) |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 107 | : DT(nullptr), PDT(nullptr), LI(nullptr), Reader(), Samples(nullptr), |
| 108 | Filename(Name), ProfileIsValid(false), TotalCollectedSamples(0) {} |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 109 | |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 110 | bool doInitialization(Module &M); |
| 111 | bool runOnModule(Module &M); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 112 | |
| 113 | void dump() { Reader->dump(); } |
| 114 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 115 | protected: |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 116 | bool runOnFunction(Function &F); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 117 | unsigned getFunctionLoc(Function &F); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 118 | bool emitAnnotations(Function &F); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 119 | ErrorOr<uint64_t> getInstWeight(const Instruction &I) const; |
| 120 | ErrorOr<uint64_t> getBlockWeight(const BasicBlock *BB) const; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 121 | const FunctionSamples *findCalleeFunctionSamples(const CallInst &I) const; |
| 122 | const FunctionSamples *findFunctionSamples(const Instruction &I) const; |
| 123 | bool inlineHotFunctions(Function &F); |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 124 | bool emitInlineHints(Function &F); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 125 | void printEdgeWeight(raw_ostream &OS, Edge E); |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 126 | void printBlockWeight(raw_ostream &OS, const BasicBlock *BB) const; |
| 127 | void printBlockEquivalence(raw_ostream &OS, const BasicBlock *BB); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 128 | bool computeBlockWeights(Function &F); |
| 129 | void findEquivalenceClasses(Function &F); |
Benjamin Kramer | 8a752e3 | 2016-02-13 16:01:12 +0000 | [diff] [blame] | 130 | void findEquivalencesFor(BasicBlock *BB1, ArrayRef<BasicBlock *> Descendants, |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 131 | DominatorTreeBase<BasicBlock> *DomTree); |
| 132 | void propagateWeights(Function &F); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 133 | uint64_t visitEdge(Edge E, unsigned *NumUnknownEdges, Edge *UnknownEdge); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 134 | void buildEdges(Function &F); |
| 135 | bool propagateThroughEdges(Function &F); |
Diego Novillo | 7732ae4 | 2015-08-26 20:00:27 +0000 | [diff] [blame] | 136 | void computeDominanceAndLoopInfo(Function &F); |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 137 | unsigned getOffset(unsigned L, unsigned H) const; |
Diego Novillo | a8a3bd2 | 2015-10-28 17:40:22 +0000 | [diff] [blame] | 138 | void clearFunctionData(); |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 139 | |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 140 | /// \brief Map basic blocks to their computed weights. |
| 141 | /// |
| 142 | /// The weight of a basic block is defined to be the maximum |
| 143 | /// of all the instruction weights in that block. |
| 144 | BlockWeightMap BlockWeights; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 145 | |
| 146 | /// \brief Map edges to their computed weights. |
| 147 | /// |
| 148 | /// Edge weights are computed by propagating basic block weights in |
| 149 | /// SampleProfile::propagateWeights. |
| 150 | EdgeWeightMap EdgeWeights; |
| 151 | |
| 152 | /// \brief Set of visited blocks during propagation. |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 153 | SmallPtrSet<const BasicBlock *, 32> VisitedBlocks; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 154 | |
| 155 | /// \brief Set of visited edges during propagation. |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 156 | SmallSet<Edge, 32> VisitedEdges; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 157 | |
| 158 | /// \brief Equivalence classes for block weights. |
| 159 | /// |
| 160 | /// Two blocks BB1 and BB2 are in the same equivalence class if they |
| 161 | /// dominate and post-dominate each other, and they are in the same loop |
| 162 | /// nest. When this happens, the two blocks are guaranteed to execute |
| 163 | /// the same number of times. |
| 164 | EquivalenceClassMap EquivalenceClass; |
| 165 | |
| 166 | /// \brief Dominance, post-dominance and loop information. |
Diego Novillo | 7732ae4 | 2015-08-26 20:00:27 +0000 | [diff] [blame] | 167 | std::unique_ptr<DominatorTree> DT; |
| 168 | std::unique_ptr<DominatorTreeBase<BasicBlock>> PDT; |
| 169 | std::unique_ptr<LoopInfo> LI; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 170 | |
| 171 | /// \brief Predecessors for each basic block in the CFG. |
| 172 | BlockEdgeMap Predecessors; |
| 173 | |
| 174 | /// \brief Successors for each basic block in the CFG. |
| 175 | BlockEdgeMap Successors; |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 176 | |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 177 | /// \brief Profile reader object. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 178 | std::unique_ptr<SampleProfileReader> Reader; |
| 179 | |
| 180 | /// \brief Samples collected for the body of this function. |
| 181 | FunctionSamples *Samples; |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 182 | |
| 183 | /// \brief Name of the profile file to load. |
| 184 | StringRef Filename; |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 185 | |
Alp Toker | 16f98b2 | 2014-04-09 14:47:27 +0000 | [diff] [blame] | 186 | /// \brief Flag indicating whether the profile input loaded successfully. |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 187 | bool ProfileIsValid; |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 188 | |
| 189 | /// \brief Total number of samples collected in this profile. |
| 190 | /// |
| 191 | /// This is the sum of all the samples collected in all the functions executed |
| 192 | /// at runtime. |
| 193 | uint64_t TotalCollectedSamples; |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 194 | }; |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 195 | |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 196 | class SampleProfileLoaderLegacyPass : public ModulePass { |
| 197 | public: |
| 198 | // Class identification, replacement for typeinfo |
| 199 | static char ID; |
| 200 | |
| 201 | SampleProfileLoaderLegacyPass(StringRef Name = SampleProfileFile) |
| 202 | : ModulePass(ID), SampleLoader(Name) { |
| 203 | initializeSampleProfileLoaderLegacyPassPass( |
| 204 | *PassRegistry::getPassRegistry()); |
| 205 | } |
| 206 | |
| 207 | void dump() { SampleLoader.dump(); } |
| 208 | |
| 209 | bool doInitialization(Module &M) override { |
| 210 | return SampleLoader.doInitialization(M); |
| 211 | } |
| 212 | const char *getPassName() const override { return "Sample profile pass"; } |
| 213 | bool runOnModule(Module &M) override; |
| 214 | |
| 215 | private: |
| 216 | SampleProfileLoader SampleLoader; |
| 217 | }; |
| 218 | |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 219 | class SampleCoverageTracker { |
| 220 | public: |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 221 | SampleCoverageTracker() : SampleCoverage(), TotalUsedSamples(0) {} |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 222 | |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 223 | bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset, |
| 224 | uint32_t Discriminator, uint64_t Samples); |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 225 | unsigned computeCoverage(unsigned Used, unsigned Total) const; |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 226 | unsigned countUsedRecords(const FunctionSamples *FS) const; |
| 227 | unsigned countBodyRecords(const FunctionSamples *FS) const; |
| 228 | uint64_t getTotalUsedSamples() const { return TotalUsedSamples; } |
| 229 | uint64_t countBodySamples(const FunctionSamples *FS) const; |
| 230 | void clear() { |
| 231 | SampleCoverage.clear(); |
| 232 | TotalUsedSamples = 0; |
| 233 | } |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 234 | |
| 235 | private: |
Diego Novillo | 10cf124 | 2015-12-11 23:21:38 +0000 | [diff] [blame] | 236 | typedef std::map<LineLocation, unsigned> BodySampleCoverageMap; |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 237 | typedef DenseMap<const FunctionSamples *, BodySampleCoverageMap> |
| 238 | FunctionSamplesCoverageMap; |
| 239 | |
| 240 | /// Coverage map for sampling records. |
| 241 | /// |
| 242 | /// This map keeps a record of sampling records that have been matched to |
| 243 | /// an IR instruction. This is used to detect some form of staleness in |
| 244 | /// profiles (see flag -sample-profile-check-coverage). |
| 245 | /// |
| 246 | /// Each entry in the map corresponds to a FunctionSamples instance. This is |
| 247 | /// another map that counts how many times the sample record at the |
| 248 | /// given location has been used. |
| 249 | FunctionSamplesCoverageMap SampleCoverage; |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 250 | |
| 251 | /// Number of samples used from the profile. |
| 252 | /// |
| 253 | /// When a sampling record is used for the first time, the samples from |
| 254 | /// that record are added to this accumulator. Coverage is later computed |
| 255 | /// based on the total number of samples available in this function and |
| 256 | /// its callsites. |
| 257 | /// |
| 258 | /// Note that this accumulator tracks samples used from a single function |
| 259 | /// and all the inlined callsites. Strictly, we should have a map of counters |
| 260 | /// keyed by FunctionSamples pointers, but these stats are cleared after |
| 261 | /// every function, so we just need to keep a single counter. |
| 262 | uint64_t TotalUsedSamples; |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | SampleCoverageTracker CoverageTracker; |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 266 | |
| 267 | /// Return true if the given callsite is hot wrt to its caller. |
| 268 | /// |
| 269 | /// Functions that were inlined in the original binary will be represented |
| 270 | /// in the inline stack in the sample profile. If the profile shows that |
| 271 | /// the original inline decision was "good" (i.e., the callsite is executed |
| 272 | /// frequently), then we will recreate the inline decision and apply the |
| 273 | /// profile from the inlined callsite. |
| 274 | /// |
| 275 | /// To decide whether an inlined callsite is hot, we compute the fraction |
| 276 | /// of samples used by the callsite with respect to the total number of samples |
| 277 | /// collected in the caller. |
| 278 | /// |
| 279 | /// If that fraction is larger than the default given by |
| 280 | /// SampleProfileHotThreshold, the callsite will be inlined again. |
| 281 | bool callsiteIsHot(const FunctionSamples *CallerFS, |
| 282 | const FunctionSamples *CallsiteFS) { |
| 283 | if (!CallsiteFS) |
| 284 | return false; // The callsite was not inlined in the original binary. |
| 285 | |
| 286 | uint64_t ParentTotalSamples = CallerFS->getTotalSamples(); |
| 287 | if (ParentTotalSamples == 0) |
| 288 | return false; // Avoid division by zero. |
| 289 | |
| 290 | uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples(); |
| 291 | if (CallsiteTotalSamples == 0) |
| 292 | return false; // Callsite is trivially cold. |
| 293 | |
Diego Novillo | b579240 | 2015-11-27 23:14:49 +0000 | [diff] [blame] | 294 | double PercentSamples = |
| 295 | (double)CallsiteTotalSamples / (double)ParentTotalSamples * 100.0; |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 296 | return PercentSamples >= SampleProfileHotThreshold; |
| 297 | } |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /// Mark as used the sample record for the given function samples at |
| 301 | /// (LineOffset, Discriminator). |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 302 | /// |
| 303 | /// \returns true if this is the first time we mark the given record. |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 304 | bool SampleCoverageTracker::markSamplesUsed(const FunctionSamples *FS, |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 305 | uint32_t LineOffset, |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 306 | uint32_t Discriminator, |
| 307 | uint64_t Samples) { |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 308 | LineLocation Loc(LineOffset, Discriminator); |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 309 | unsigned &Count = SampleCoverage[FS][Loc]; |
| 310 | bool FirstTime = (++Count == 1); |
| 311 | if (FirstTime) |
| 312 | TotalUsedSamples += Samples; |
| 313 | return FirstTime; |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /// Return the number of sample records that were applied from this profile. |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 317 | /// |
| 318 | /// This count does not include records from cold inlined callsites. |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 319 | unsigned |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 320 | SampleCoverageTracker::countUsedRecords(const FunctionSamples *FS) const { |
| 321 | auto I = SampleCoverage.find(FS); |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 322 | |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 323 | // The size of the coverage map for FS represents the number of records |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 324 | // that were marked used at least once. |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 325 | unsigned Count = (I != SampleCoverage.end()) ? I->second.size() : 0; |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 326 | |
| 327 | // If there are inlined callsites in this function, count the samples found |
| 328 | // in the respective bodies. However, do not bother counting callees with 0 |
| 329 | // total samples, these are callees that were never invoked at runtime. |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 330 | for (const auto &I : FS->getCallsiteSamples()) { |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 331 | const FunctionSamples *CalleeSamples = &I.second; |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 332 | if (callsiteIsHot(FS, CalleeSamples)) |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 333 | Count += countUsedRecords(CalleeSamples); |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 336 | return Count; |
| 337 | } |
| 338 | |
| 339 | /// Return the number of sample records in the body of this profile. |
| 340 | /// |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 341 | /// This count does not include records from cold inlined callsites. |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 342 | unsigned |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 343 | SampleCoverageTracker::countBodyRecords(const FunctionSamples *FS) const { |
| 344 | unsigned Count = FS->getBodySamples().size(); |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 345 | |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 346 | // Only count records in hot callsites. |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 347 | for (const auto &I : FS->getCallsiteSamples()) { |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 348 | const FunctionSamples *CalleeSamples = &I.second; |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 349 | if (callsiteIsHot(FS, CalleeSamples)) |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 350 | Count += countBodyRecords(CalleeSamples); |
Diego Novillo | 5fb49e5 | 2015-11-20 21:46:38 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 353 | return Count; |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 356 | /// Return the number of samples collected in the body of this profile. |
| 357 | /// |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 358 | /// This count does not include samples from cold inlined callsites. |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 359 | uint64_t |
| 360 | SampleCoverageTracker::countBodySamples(const FunctionSamples *FS) const { |
| 361 | uint64_t Total = 0; |
| 362 | for (const auto &I : FS->getBodySamples()) |
| 363 | Total += I.second.getSamples(); |
| 364 | |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 365 | // Only count samples in hot callsites. |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 366 | for (const auto &I : FS->getCallsiteSamples()) { |
| 367 | const FunctionSamples *CalleeSamples = &I.second; |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 368 | if (callsiteIsHot(FS, CalleeSamples)) |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 369 | Total += countBodySamples(CalleeSamples); |
| 370 | } |
| 371 | |
| 372 | return Total; |
| 373 | } |
| 374 | |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 375 | /// Return the fraction of sample records used in this profile. |
| 376 | /// |
| 377 | /// The returned value is an unsigned integer in the range 0-100 indicating |
| 378 | /// the percentage of sample records that were used while applying this |
| 379 | /// profile to the associated function. |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 380 | unsigned SampleCoverageTracker::computeCoverage(unsigned Used, |
| 381 | unsigned Total) const { |
| 382 | assert(Used <= Total && |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 383 | "number of used records cannot exceed the total number of records"); |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 384 | return Total > 0 ? Used * 100 / Total : 100; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 385 | } |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 386 | |
Diego Novillo | a8a3bd2 | 2015-10-28 17:40:22 +0000 | [diff] [blame] | 387 | /// Clear all the per-function data used to load samples and propagate weights. |
| 388 | void SampleProfileLoader::clearFunctionData() { |
| 389 | BlockWeights.clear(); |
| 390 | EdgeWeights.clear(); |
| 391 | VisitedBlocks.clear(); |
| 392 | VisitedEdges.clear(); |
| 393 | EquivalenceClass.clear(); |
| 394 | DT = nullptr; |
| 395 | PDT = nullptr; |
| 396 | LI = nullptr; |
| 397 | Predecessors.clear(); |
| 398 | Successors.clear(); |
Diego Novillo | 1ca881c | 2015-11-23 16:30:17 +0000 | [diff] [blame] | 399 | CoverageTracker.clear(); |
Diego Novillo | a8a3bd2 | 2015-10-28 17:40:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 402 | /// \brief Returns the offset of lineno \p L to head_lineno \p H |
| 403 | /// |
| 404 | /// \param L Lineno |
| 405 | /// \param H Header lineno of the function |
| 406 | /// |
| 407 | /// \returns offset to the header lineno. 16 bits are used to represent offset. |
| 408 | /// We assume that a single function will not exceed 65535 LOC. |
| 409 | unsigned SampleProfileLoader::getOffset(unsigned L, unsigned H) const { |
| 410 | return (L - H) & 0xffff; |
| 411 | } |
| 412 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 413 | /// \brief Print the weight of edge \p E on stream \p OS. |
| 414 | /// |
| 415 | /// \param OS Stream to emit the output to. |
| 416 | /// \param E Edge to print. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 417 | void SampleProfileLoader::printEdgeWeight(raw_ostream &OS, Edge E) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 418 | OS << "weight[" << E.first->getName() << "->" << E.second->getName() |
| 419 | << "]: " << EdgeWeights[E] << "\n"; |
| 420 | } |
| 421 | |
| 422 | /// \brief Print the equivalence class of block \p BB on stream \p OS. |
| 423 | /// |
| 424 | /// \param OS Stream to emit the output to. |
| 425 | /// \param BB Block to print. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 426 | void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS, |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 427 | const BasicBlock *BB) { |
| 428 | const BasicBlock *Equiv = EquivalenceClass[BB]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 429 | OS << "equivalence[" << BB->getName() |
| 430 | << "]: " << ((Equiv) ? EquivalenceClass[BB]->getName() : "NONE") << "\n"; |
| 431 | } |
| 432 | |
| 433 | /// \brief Print the weight of block \p BB on stream \p OS. |
| 434 | /// |
| 435 | /// \param OS Stream to emit the output to. |
| 436 | /// \param BB Block to print. |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 437 | void SampleProfileLoader::printBlockWeight(raw_ostream &OS, |
| 438 | const BasicBlock *BB) const { |
| 439 | const auto &I = BlockWeights.find(BB); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 440 | uint64_t W = (I == BlockWeights.end() ? 0 : I->second); |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 441 | OS << "weight[" << BB->getName() << "]: " << W << "\n"; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 444 | /// \brief Get the weight for an instruction. |
| 445 | /// |
| 446 | /// The "weight" of an instruction \p Inst is the number of samples |
| 447 | /// collected on that instruction at runtime. To retrieve it, we |
| 448 | /// need to compute the line number of \p Inst relative to the start of its |
| 449 | /// function. We use HeaderLineno to compute the offset. We then |
| 450 | /// look up the samples collected for \p Inst using BodySamples. |
| 451 | /// |
| 452 | /// \param Inst Instruction to query. |
| 453 | /// |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 454 | /// \returns the weight of \p Inst. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 455 | ErrorOr<uint64_t> |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 456 | SampleProfileLoader::getInstWeight(const Instruction &Inst) const { |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 457 | const DebugLoc &DLoc = Inst.getDebugLoc(); |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 458 | if (!DLoc) |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 459 | return std::error_code(); |
Duncan P. N. Exon Smith | 41a1546 | 2015-03-20 00:56:55 +0000 | [diff] [blame] | 460 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 461 | const FunctionSamples *FS = findFunctionSamples(Inst); |
| 462 | if (!FS) |
| 463 | return std::error_code(); |
Dehao Chen | 41dc5a6 | 2015-10-09 16:50:16 +0000 | [diff] [blame] | 464 | |
Dehao Chen | a8bae82 | 2016-04-20 23:36:23 +0000 | [diff] [blame] | 465 | // Ignore all dbg_value intrinsics. |
| 466 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&Inst); |
| 467 | if (II && II->getIntrinsicID() == Intrinsic::dbg_value) |
| 468 | return std::error_code(); |
| 469 | |
Dehao Chen | 41dc5a6 | 2015-10-09 16:50:16 +0000 | [diff] [blame] | 470 | const DILocation *DIL = DLoc; |
| 471 | unsigned Lineno = DLoc.getLine(); |
| 472 | unsigned HeaderLineno = DIL->getScope()->getSubprogram()->getLine(); |
Dehao Chen | 41dc5a6 | 2015-10-09 16:50:16 +0000 | [diff] [blame] | 473 | |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 474 | uint32_t LineOffset = getOffset(Lineno, HeaderLineno); |
| 475 | uint32_t Discriminator = DIL->getDiscriminator(); |
| 476 | ErrorOr<uint64_t> R = FS->findSamplesAt(LineOffset, Discriminator); |
| 477 | if (R) { |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 478 | bool FirstMark = |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 479 | CoverageTracker.markSamplesUsed(FS, LineOffset, Discriminator, R.get()); |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 480 | if (FirstMark) { |
| 481 | const Function *F = Inst.getParent()->getParent(); |
| 482 | LLVMContext &Ctx = F->getContext(); |
Diego Novillo | df544a0 | 2015-11-20 15:39:42 +0000 | [diff] [blame] | 483 | emitOptimizationRemark( |
| 484 | Ctx, DEBUG_TYPE, *F, DLoc, |
| 485 | Twine("Applied ") + Twine(*R) + " samples from profile (offset: " + |
| 486 | Twine(LineOffset) + |
| 487 | ((Discriminator) ? Twine(".") + Twine(Discriminator) : "") + ")"); |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 488 | } |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 489 | DEBUG(dbgs() << " " << Lineno << "." << DIL->getDiscriminator() << ":" |
| 490 | << Inst << " (line offset: " << Lineno - HeaderLineno << "." |
| 491 | << DIL->getDiscriminator() << " - weight: " << R.get() |
| 492 | << ")\n"); |
Dehao Chen | a8bae82 | 2016-04-20 23:36:23 +0000 | [diff] [blame] | 493 | } else { |
| 494 | // If a call instruction is inlined in profile, but not inlined here, |
| 495 | // it means that the inlined callsite has no sample, thus the call |
| 496 | // instruction should have 0 count. |
| 497 | const CallInst *CI = dyn_cast<CallInst>(&Inst); |
| 498 | if (CI && findCalleeFunctionSamples(*CI)) |
| 499 | R = 0; |
Diego Novillo | 748b3ffe | 2015-10-28 22:30:25 +0000 | [diff] [blame] | 500 | } |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 501 | return R; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /// \brief Compute the weight of a basic block. |
| 505 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 506 | /// The weight of basic block \p BB is the maximum weight of all the |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 507 | /// instructions in BB. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 508 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 509 | /// \param BB The basic block to query. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 510 | /// |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 511 | /// \returns the weight for \p BB. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 512 | ErrorOr<uint64_t> |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 513 | SampleProfileLoader::getBlockWeight(const BasicBlock *BB) const { |
Dehao Chen | 5d6d484 | 2016-04-26 04:59:11 +0000 | [diff] [blame] | 514 | DenseMap<uint64_t, uint64_t> CM; |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 515 | for (auto &I : BB->getInstList()) { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 516 | const ErrorOr<uint64_t> &R = getInstWeight(I); |
Dehao Chen | 5d6d484 | 2016-04-26 04:59:11 +0000 | [diff] [blame] | 517 | if (R) CM[R.get()]++; |
| 518 | } |
| 519 | if (CM.size() == 0) return std::error_code(); |
| 520 | uint64_t W = 0, C = 0; |
| 521 | for (const auto &C_W : CM) { |
| 522 | if (C_W.second == W) { |
| 523 | C = std::max(C, C_W.first); |
| 524 | } else if (C_W.second > W) { |
| 525 | C = C_W.first; |
| 526 | W = C_W.second; |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 527 | } |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 528 | } |
Dehao Chen | 5d6d484 | 2016-04-26 04:59:11 +0000 | [diff] [blame] | 529 | return C; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /// \brief Compute and store the weights of every basic block. |
| 533 | /// |
| 534 | /// This populates the BlockWeights map by computing |
| 535 | /// the weights of every basic block in the CFG. |
| 536 | /// |
| 537 | /// \param F The function to query. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 538 | bool SampleProfileLoader::computeBlockWeights(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 539 | bool Changed = false; |
| 540 | DEBUG(dbgs() << "Block weights\n"); |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 541 | for (const auto &BB : F) { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 542 | ErrorOr<uint64_t> Weight = getBlockWeight(&BB); |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 543 | if (Weight) { |
| 544 | BlockWeights[&BB] = Weight.get(); |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 545 | VisitedBlocks.insert(&BB); |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 546 | Changed = true; |
| 547 | } |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 548 | DEBUG(printBlockWeight(dbgs(), &BB)); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | return Changed; |
| 552 | } |
| 553 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 554 | /// \brief Get the FunctionSamples for a call instruction. |
| 555 | /// |
| 556 | /// The FunctionSamples of a call instruction \p Inst is the inlined |
| 557 | /// instance in which that call instruction is calling to. It contains |
| 558 | /// all samples that resides in the inlined instance. We first find the |
| 559 | /// inlined instance in which the call instruction is from, then we |
| 560 | /// traverse its children to find the callsite with the matching |
| 561 | /// location and callee function name. |
| 562 | /// |
| 563 | /// \param Inst Call instruction to query. |
| 564 | /// |
| 565 | /// \returns The FunctionSamples pointer to the inlined instance. |
| 566 | const FunctionSamples * |
| 567 | SampleProfileLoader::findCalleeFunctionSamples(const CallInst &Inst) const { |
| 568 | const DILocation *DIL = Inst.getDebugLoc(); |
| 569 | if (!DIL) { |
| 570 | return nullptr; |
| 571 | } |
| 572 | DISubprogram *SP = DIL->getScope()->getSubprogram(); |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 573 | if (!SP) |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 574 | return nullptr; |
| 575 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 576 | const FunctionSamples *FS = findFunctionSamples(Inst); |
| 577 | if (FS == nullptr) |
| 578 | return nullptr; |
| 579 | |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 580 | return FS->findFunctionSamplesAt(LineLocation( |
| 581 | getOffset(DIL->getLine(), SP->getLine()), DIL->getDiscriminator())); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /// \brief Get the FunctionSamples for an instruction. |
| 585 | /// |
| 586 | /// The FunctionSamples of an instruction \p Inst is the inlined instance |
| 587 | /// in which that instruction is coming from. We traverse the inline stack |
| 588 | /// of that instruction, and match it with the tree nodes in the profile. |
| 589 | /// |
| 590 | /// \param Inst Instruction to query. |
| 591 | /// |
| 592 | /// \returns the FunctionSamples pointer to the inlined instance. |
| 593 | const FunctionSamples * |
| 594 | SampleProfileLoader::findFunctionSamples(const Instruction &Inst) const { |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 595 | SmallVector<LineLocation, 10> S; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 596 | const DILocation *DIL = Inst.getDebugLoc(); |
| 597 | if (!DIL) { |
| 598 | return Samples; |
| 599 | } |
Dehao Chen | 21aefae | 2016-04-29 17:19:10 +0000 | [diff] [blame] | 600 | for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) { |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 601 | DISubprogram *SP = DIL->getScope()->getSubprogram(); |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 602 | if (!SP) |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 603 | return nullptr; |
Dehao Chen | 21aefae | 2016-04-29 17:19:10 +0000 | [diff] [blame] | 604 | S.push_back(LineLocation(getOffset(DIL->getLine(), SP->getLine()), |
| 605 | DIL->getDiscriminator())); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 606 | } |
| 607 | if (S.size() == 0) |
| 608 | return Samples; |
| 609 | const FunctionSamples *FS = Samples; |
| 610 | for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) { |
| 611 | FS = FS->findFunctionSamplesAt(S[i]); |
| 612 | } |
| 613 | return FS; |
| 614 | } |
| 615 | |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 616 | /// \brief Emit an inline hint if \p F is globally hot or cold. |
| 617 | /// |
| 618 | /// If \p F consumes a significant fraction of samples (indicated by |
| 619 | /// SampleProfileGlobalHotThreshold), apply the InlineHint attribute for the |
| 620 | /// inliner to consider the function hot. |
| 621 | /// |
| 622 | /// If \p F consumes a small fraction of samples (indicated by |
| 623 | /// SampleProfileGlobalColdThreshold), apply the Cold attribute for the inliner |
| 624 | /// to consider the function cold. |
| 625 | /// |
| 626 | /// FIXME - This setting of inline hints is sub-optimal. Instead of marking a |
| 627 | /// function globally hot or cold, we should be annotating individual callsites. |
| 628 | /// This is not currently possible, but work on the inliner will eventually |
| 629 | /// provide this ability. See http://reviews.llvm.org/D15003 for details and |
| 630 | /// discussion. |
| 631 | /// |
| 632 | /// \returns True if either attribute was applied to \p F. |
| 633 | bool SampleProfileLoader::emitInlineHints(Function &F) { |
| 634 | if (TotalCollectedSamples == 0) |
| 635 | return false; |
| 636 | |
| 637 | uint64_t FunctionSamples = Samples->getTotalSamples(); |
| 638 | double SamplesPercent = |
| 639 | (double)FunctionSamples / (double)TotalCollectedSamples * 100.0; |
| 640 | |
| 641 | // If the function collected more samples than the hot threshold, mark |
| 642 | // it globally hot. |
| 643 | if (SamplesPercent >= SampleProfileGlobalHotThreshold) { |
| 644 | F.addFnAttr(llvm::Attribute::InlineHint); |
Diego Novillo | 7ff0a17 | 2015-11-29 18:23:26 +0000 | [diff] [blame] | 645 | std::string Msg; |
| 646 | raw_string_ostream S(Msg); |
| 647 | S << "Applied inline hint to globally hot function '" << F.getName() |
| 648 | << "' with " << format("%.2f", SamplesPercent) |
| 649 | << "% of samples (threshold: " |
| 650 | << format("%.2f", SampleProfileGlobalHotThreshold.getValue()) << "%)"; |
| 651 | S.flush(); |
| 652 | emitOptimizationRemark(F.getContext(), DEBUG_TYPE, F, DebugLoc(), Msg); |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 653 | return true; |
| 654 | } |
| 655 | |
| 656 | // If the function collected fewer samples than the cold threshold, mark |
| 657 | // it globally cold. |
| 658 | if (SamplesPercent <= SampleProfileGlobalColdThreshold) { |
| 659 | F.addFnAttr(llvm::Attribute::Cold); |
Diego Novillo | 7ff0a17 | 2015-11-29 18:23:26 +0000 | [diff] [blame] | 660 | std::string Msg; |
| 661 | raw_string_ostream S(Msg); |
| 662 | S << "Applied cold hint to globally cold function '" << F.getName() |
| 663 | << "' with " << format("%.2f", SamplesPercent) |
| 664 | << "% of samples (threshold: " |
| 665 | << format("%.2f", SampleProfileGlobalColdThreshold.getValue()) << "%)"; |
| 666 | S.flush(); |
| 667 | emitOptimizationRemark(F.getContext(), DEBUG_TYPE, F, DebugLoc(), Msg); |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 668 | return true; |
| 669 | } |
| 670 | |
| 671 | return false; |
| 672 | } |
| 673 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 674 | /// \brief Iteratively inline hot callsites of a function. |
| 675 | /// |
| 676 | /// Iteratively traverse all callsites of the function \p F, and find if |
| 677 | /// the corresponding inlined instance exists and is hot in profile. If |
| 678 | /// it is hot enough, inline the callsites and adds new callsites of the |
| 679 | /// callee into the caller. |
| 680 | /// |
| 681 | /// TODO: investigate the possibility of not invoking InlineFunction directly. |
| 682 | /// |
| 683 | /// \param F function to perform iterative inlining. |
| 684 | /// |
| 685 | /// \returns True if there is any inline happened. |
| 686 | bool SampleProfileLoader::inlineHotFunctions(Function &F) { |
| 687 | bool Changed = false; |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 688 | LLVMContext &Ctx = F.getContext(); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 689 | while (true) { |
| 690 | bool LocalChanged = false; |
| 691 | SmallVector<CallInst *, 10> CIS; |
| 692 | for (auto &BB : F) { |
| 693 | for (auto &I : BB.getInstList()) { |
| 694 | CallInst *CI = dyn_cast<CallInst>(&I); |
Diego Novillo | 0b6985a | 2015-11-24 22:38:37 +0000 | [diff] [blame] | 695 | if (CI && callsiteIsHot(Samples, findCalleeFunctionSamples(*CI))) |
| 696 | CIS.push_back(CI); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | for (auto CI : CIS) { |
| 700 | InlineFunctionInfo IFI; |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 701 | Function *CalledFunction = CI->getCalledFunction(); |
| 702 | DebugLoc DLoc = CI->getDebugLoc(); |
| 703 | uint64_t NumSamples = findCalleeFunctionSamples(*CI)->getTotalSamples(); |
| 704 | if (InlineFunction(CI, IFI)) { |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 705 | LocalChanged = true; |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 706 | emitOptimizationRemark(Ctx, DEBUG_TYPE, F, DLoc, |
| 707 | Twine("inlined hot callee '") + |
| 708 | CalledFunction->getName() + "' with " + |
| 709 | Twine(NumSamples) + " samples into '" + |
| 710 | F.getName() + "'"); |
| 711 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 712 | } |
| 713 | if (LocalChanged) { |
| 714 | Changed = true; |
| 715 | } else { |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | return Changed; |
| 720 | } |
| 721 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 722 | /// \brief Find equivalence classes for the given block. |
| 723 | /// |
| 724 | /// This finds all the blocks that are guaranteed to execute the same |
Eric Christopher | 572e03a | 2015-06-19 01:53:21 +0000 | [diff] [blame] | 725 | /// number of times as \p BB1. To do this, it traverses all the |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 726 | /// descendants of \p BB1 in the dominator or post-dominator tree. |
| 727 | /// |
| 728 | /// A block BB2 will be in the same equivalence class as \p BB1 if |
| 729 | /// the following holds: |
| 730 | /// |
| 731 | /// 1- \p BB1 is a descendant of BB2 in the opposite tree. So, if BB2 |
| 732 | /// is a descendant of \p BB1 in the dominator tree, then BB2 should |
| 733 | /// dominate BB1 in the post-dominator tree. |
| 734 | /// |
| 735 | /// 2- Both BB2 and \p BB1 must be in the same loop. |
| 736 | /// |
| 737 | /// For every block BB2 that meets those two requirements, we set BB2's |
| 738 | /// equivalence class to \p BB1. |
| 739 | /// |
| 740 | /// \param BB1 Block to check. |
| 741 | /// \param Descendants Descendants of \p BB1 in either the dom or pdom tree. |
| 742 | /// \param DomTree Opposite dominator tree. If \p Descendants is filled |
| 743 | /// with blocks from \p BB1's dominator tree, then |
| 744 | /// this is the post-dominator tree, and vice versa. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 745 | void SampleProfileLoader::findEquivalencesFor( |
Benjamin Kramer | 8a752e3 | 2016-02-13 16:01:12 +0000 | [diff] [blame] | 746 | BasicBlock *BB1, ArrayRef<BasicBlock *> Descendants, |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 747 | DominatorTreeBase<BasicBlock> *DomTree) { |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 748 | const BasicBlock *EC = EquivalenceClass[BB1]; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 749 | uint64_t Weight = BlockWeights[EC]; |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 750 | for (const auto *BB2 : Descendants) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 751 | bool IsDomParent = DomTree->dominates(BB2, BB1); |
| 752 | bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2); |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 753 | if (BB1 != BB2 && IsDomParent && IsInSameLoop) { |
| 754 | EquivalenceClass[BB2] = EC; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 755 | |
| 756 | // If BB2 is heavier than BB1, make BB2 have the same weight |
| 757 | // as BB1. |
| 758 | // |
| 759 | // Note that we don't worry about the opposite situation here |
| 760 | // (when BB2 is lighter than BB1). We will deal with this |
| 761 | // during the propagation phase. Right now, we just want to |
| 762 | // make sure that BB1 has the largest weight of all the |
| 763 | // members of its equivalence set. |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 764 | Weight = std::max(Weight, BlockWeights[BB2]); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 767 | BlockWeights[EC] = Weight; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /// \brief Find equivalence classes. |
| 771 | /// |
| 772 | /// Since samples may be missing from blocks, we can fill in the gaps by setting |
| 773 | /// the weights of all the blocks in the same equivalence class to the same |
| 774 | /// weight. To compute the concept of equivalence, we use dominance and loop |
| 775 | /// information. Two blocks B1 and B2 are in the same equivalence class if B1 |
| 776 | /// dominates B2, B2 post-dominates B1 and both are in the same loop. |
| 777 | /// |
| 778 | /// \param F The function to query. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 779 | void SampleProfileLoader::findEquivalenceClasses(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 780 | SmallVector<BasicBlock *, 8> DominatedBBs; |
| 781 | DEBUG(dbgs() << "\nBlock equivalence classes\n"); |
| 782 | // Find equivalence sets based on dominance and post-dominance information. |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 783 | for (auto &BB : F) { |
| 784 | BasicBlock *BB1 = &BB; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 785 | |
| 786 | // Compute BB1's equivalence class once. |
| 787 | if (EquivalenceClass.count(BB1)) { |
| 788 | DEBUG(printBlockEquivalence(dbgs(), BB1)); |
| 789 | continue; |
| 790 | } |
| 791 | |
| 792 | // By default, blocks are in their own equivalence class. |
| 793 | EquivalenceClass[BB1] = BB1; |
| 794 | |
| 795 | // Traverse all the blocks dominated by BB1. We are looking for |
| 796 | // every basic block BB2 such that: |
| 797 | // |
| 798 | // 1- BB1 dominates BB2. |
| 799 | // 2- BB2 post-dominates BB1. |
| 800 | // 3- BB1 and BB2 are in the same loop nest. |
| 801 | // |
| 802 | // If all those conditions hold, it means that BB2 is executed |
| 803 | // as many times as BB1, so they are placed in the same equivalence |
| 804 | // class by making BB2's equivalence class be BB1. |
| 805 | DominatedBBs.clear(); |
| 806 | DT->getDescendants(BB1, DominatedBBs); |
Diego Novillo | 7732ae4 | 2015-08-26 20:00:27 +0000 | [diff] [blame] | 807 | findEquivalencesFor(BB1, DominatedBBs, PDT.get()); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 808 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 809 | DEBUG(printBlockEquivalence(dbgs(), BB1)); |
| 810 | } |
| 811 | |
| 812 | // Assign weights to equivalence classes. |
| 813 | // |
| 814 | // All the basic blocks in the same equivalence class will execute |
| 815 | // the same number of times. Since we know that the head block in |
| 816 | // each equivalence class has the largest weight, assign that weight |
| 817 | // to all the blocks in that equivalence class. |
| 818 | DEBUG(dbgs() << "\nAssign the same weight to all blocks in the same class\n"); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 819 | for (auto &BI : F) { |
Dehao Chen | 8e7df83 | 2015-09-29 18:28:15 +0000 | [diff] [blame] | 820 | const BasicBlock *BB = &BI; |
| 821 | const BasicBlock *EquivBB = EquivalenceClass[BB]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 822 | if (BB != EquivBB) |
| 823 | BlockWeights[BB] = BlockWeights[EquivBB]; |
| 824 | DEBUG(printBlockWeight(dbgs(), BB)); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /// \brief Visit the given edge to decide if it has a valid weight. |
| 829 | /// |
| 830 | /// If \p E has not been visited before, we copy to \p UnknownEdge |
| 831 | /// and increment the count of unknown edges. |
| 832 | /// |
| 833 | /// \param E Edge to visit. |
| 834 | /// \param NumUnknownEdges Current number of unknown edges. |
| 835 | /// \param UnknownEdge Set if E has not been visited before. |
| 836 | /// |
| 837 | /// \returns E's weight, if known. Otherwise, return 0. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 838 | uint64_t SampleProfileLoader::visitEdge(Edge E, unsigned *NumUnknownEdges, |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 839 | Edge *UnknownEdge) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 840 | if (!VisitedEdges.count(E)) { |
| 841 | (*NumUnknownEdges)++; |
| 842 | *UnknownEdge = E; |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | return EdgeWeights[E]; |
| 847 | } |
| 848 | |
| 849 | /// \brief Propagate weights through incoming/outgoing edges. |
| 850 | /// |
| 851 | /// If the weight of a basic block is known, and there is only one edge |
| 852 | /// with an unknown weight, we can calculate the weight of that edge. |
| 853 | /// |
| 854 | /// Similarly, if all the edges have a known count, we can calculate the |
| 855 | /// count of the basic block, if needed. |
| 856 | /// |
| 857 | /// \param F Function to process. |
| 858 | /// |
| 859 | /// \returns True if new weights were assigned to edges or blocks. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 860 | bool SampleProfileLoader::propagateThroughEdges(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 861 | bool Changed = false; |
| 862 | DEBUG(dbgs() << "\nPropagation through edges\n"); |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 863 | for (const auto &BI : F) { |
| 864 | const BasicBlock *BB = &BI; |
| 865 | const BasicBlock *EC = EquivalenceClass[BB]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 866 | |
| 867 | // Visit all the predecessor and successor edges to determine |
| 868 | // which ones have a weight assigned already. Note that it doesn't |
| 869 | // matter that we only keep track of a single unknown edge. The |
| 870 | // only case we are interested in handling is when only a single |
| 871 | // edge is unknown (see setEdgeOrBlockWeight). |
| 872 | for (unsigned i = 0; i < 2; i++) { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 873 | uint64_t TotalWeight = 0; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 874 | unsigned NumUnknownEdges = 0; |
| 875 | Edge UnknownEdge, SelfReferentialEdge; |
| 876 | |
| 877 | if (i == 0) { |
| 878 | // First, visit all predecessor edges. |
Diego Novillo | b368b7d | 2014-10-22 16:51:50 +0000 | [diff] [blame] | 879 | for (auto *Pred : Predecessors[BB]) { |
| 880 | Edge E = std::make_pair(Pred, BB); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 881 | TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge); |
| 882 | if (E.first == E.second) |
| 883 | SelfReferentialEdge = E; |
| 884 | } |
| 885 | } else { |
| 886 | // On the second round, visit all successor edges. |
Diego Novillo | b368b7d | 2014-10-22 16:51:50 +0000 | [diff] [blame] | 887 | for (auto *Succ : Successors[BB]) { |
| 888 | Edge E = std::make_pair(BB, Succ); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 889 | TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | // After visiting all the edges, there are three cases that we |
| 894 | // can handle immediately: |
| 895 | // |
| 896 | // - All the edge weights are known (i.e., NumUnknownEdges == 0). |
| 897 | // In this case, we simply check that the sum of all the edges |
| 898 | // is the same as BB's weight. If not, we change BB's weight |
| 899 | // to match. Additionally, if BB had not been visited before, |
| 900 | // we mark it visited. |
| 901 | // |
| 902 | // - Only one edge is unknown and BB has already been visited. |
| 903 | // In this case, we can compute the weight of the edge by |
| 904 | // subtracting the total block weight from all the known |
| 905 | // edge weights. If the edges weight more than BB, then the |
| 906 | // edge of the last remaining edge is set to zero. |
| 907 | // |
| 908 | // - There exists a self-referential edge and the weight of BB is |
| 909 | // known. In this case, this edge can be based on BB's weight. |
| 910 | // We add up all the other known edges and set the weight on |
| 911 | // the self-referential edge as we did in the previous case. |
| 912 | // |
| 913 | // In any other case, we must continue iterating. Eventually, |
| 914 | // all edges will get a weight, or iteration will stop when |
| 915 | // it reaches SampleProfileMaxPropagateIterations. |
| 916 | if (NumUnknownEdges <= 1) { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 917 | uint64_t &BBWeight = BlockWeights[EC]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 918 | if (NumUnknownEdges == 0) { |
| 919 | // If we already know the weight of all edges, the weight of the |
| 920 | // basic block can be computed. It should be no larger than the sum |
| 921 | // of all edge weights. |
| 922 | if (TotalWeight > BBWeight) { |
| 923 | BBWeight = TotalWeight; |
| 924 | Changed = true; |
| 925 | DEBUG(dbgs() << "All edge weights for " << BB->getName() |
| 926 | << " known. Set weight for block: "; |
| 927 | printBlockWeight(dbgs(), BB);); |
| 928 | } |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 929 | if (VisitedBlocks.insert(EC).second) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 930 | Changed = true; |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 931 | } else if (NumUnknownEdges == 1 && VisitedBlocks.count(EC)) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 932 | // If there is a single unknown edge and the block has been |
| 933 | // visited, then we can compute E's weight. |
| 934 | if (BBWeight >= TotalWeight) |
| 935 | EdgeWeights[UnknownEdge] = BBWeight - TotalWeight; |
| 936 | else |
| 937 | EdgeWeights[UnknownEdge] = 0; |
| 938 | VisitedEdges.insert(UnknownEdge); |
| 939 | Changed = true; |
| 940 | DEBUG(dbgs() << "Set weight for edge: "; |
| 941 | printEdgeWeight(dbgs(), UnknownEdge)); |
| 942 | } |
Dehao Chen | 7c41dd6 | 2015-10-01 00:26:56 +0000 | [diff] [blame] | 943 | } else if (SelfReferentialEdge.first && VisitedBlocks.count(EC)) { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 944 | uint64_t &BBWeight = BlockWeights[BB]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 945 | // We have a self-referential edge and the weight of BB is known. |
| 946 | if (BBWeight >= TotalWeight) |
| 947 | EdgeWeights[SelfReferentialEdge] = BBWeight - TotalWeight; |
| 948 | else |
| 949 | EdgeWeights[SelfReferentialEdge] = 0; |
| 950 | VisitedEdges.insert(SelfReferentialEdge); |
| 951 | Changed = true; |
| 952 | DEBUG(dbgs() << "Set self-referential edge weight to: "; |
| 953 | printEdgeWeight(dbgs(), SelfReferentialEdge)); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | return Changed; |
| 959 | } |
| 960 | |
| 961 | /// \brief Build in/out edge lists for each basic block in the CFG. |
| 962 | /// |
| 963 | /// We are interested in unique edges. If a block B1 has multiple |
| 964 | /// edges to another block B2, we only add a single B1->B2 edge. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 965 | void SampleProfileLoader::buildEdges(Function &F) { |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 966 | for (auto &BI : F) { |
| 967 | BasicBlock *B1 = &BI; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 968 | |
| 969 | // Add predecessors for B1. |
| 970 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 971 | if (!Predecessors[B1].empty()) |
| 972 | llvm_unreachable("Found a stale predecessors list in a basic block."); |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 973 | for (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) { |
| 974 | BasicBlock *B2 = *PI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 975 | if (Visited.insert(B2).second) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 976 | Predecessors[B1].push_back(B2); |
| 977 | } |
| 978 | |
| 979 | // Add successors for B1. |
| 980 | Visited.clear(); |
| 981 | if (!Successors[B1].empty()) |
| 982 | llvm_unreachable("Found a stale successors list in a basic block."); |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 983 | for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) { |
| 984 | BasicBlock *B2 = *SI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 985 | if (Visited.insert(B2).second) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 986 | Successors[B1].push_back(B2); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /// \brief Propagate weights into edges |
| 992 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 993 | /// The following rules are applied to every block BB in the CFG: |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 994 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 995 | /// - If BB has a single predecessor/successor, then the weight |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 996 | /// of that edge is the weight of the block. |
| 997 | /// |
| 998 | /// - If all incoming or outgoing edges are known except one, and the |
| 999 | /// weight of the block is already known, the weight of the unknown |
| 1000 | /// edge will be the weight of the block minus the sum of all the known |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1001 | /// edges. If the sum of all the known edges is larger than BB's weight, |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1002 | /// we set the unknown edge weight to zero. |
| 1003 | /// |
| 1004 | /// - If there is a self-referential edge, and the weight of the block is |
| 1005 | /// known, the weight for that edge is set to the weight of the block |
| 1006 | /// minus the weight of the other incoming edges to that block (if |
| 1007 | /// known). |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 1008 | void SampleProfileLoader::propagateWeights(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1009 | bool Changed = true; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 1010 | unsigned I = 0; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1011 | |
Diego Novillo | ffc84e3 | 2015-05-13 17:04:29 +0000 | [diff] [blame] | 1012 | // Add an entry count to the function using the samples gathered |
| 1013 | // at the function entry. |
| 1014 | F.setEntryCount(Samples->getHeadSamples()); |
| 1015 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1016 | // Before propagation starts, build, for each block, a list of |
| 1017 | // unique predecessors and successors. This is necessary to handle |
| 1018 | // identical edges in multiway branches. Since we visit all blocks and all |
| 1019 | // edges of the CFG, it is cleaner to build these lists once at the start |
| 1020 | // of the pass. |
| 1021 | buildEdges(F); |
| 1022 | |
| 1023 | // Propagate until we converge or we go past the iteration limit. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 1024 | while (Changed && I++ < SampleProfileMaxPropagateIterations) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1025 | Changed = propagateThroughEdges(F); |
| 1026 | } |
| 1027 | |
| 1028 | // Generate MD_prof metadata for every branch instruction using the |
| 1029 | // edge weights computed during propagation. |
| 1030 | DEBUG(dbgs() << "\nPropagation complete. Setting branch weights\n"); |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1031 | LLVMContext &Ctx = F.getContext(); |
| 1032 | MDBuilder MDB(Ctx); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1033 | for (auto &BI : F) { |
| 1034 | BasicBlock *BB = &BI; |
| 1035 | TerminatorInst *TI = BB->getTerminator(); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1036 | if (TI->getNumSuccessors() == 1) |
| 1037 | continue; |
| 1038 | if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) |
| 1039 | continue; |
| 1040 | |
| 1041 | DEBUG(dbgs() << "\nGetting weights for branch at line " |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 1042 | << TI->getDebugLoc().getLine() << ".\n"); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 1043 | SmallVector<uint32_t, 4> Weights; |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1044 | uint32_t MaxWeight = 0; |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1045 | DebugLoc MaxDestLoc; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1046 | for (unsigned I = 0; I < TI->getNumSuccessors(); ++I) { |
| 1047 | BasicBlock *Succ = TI->getSuccessor(I); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1048 | Edge E = std::make_pair(BB, Succ); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 1049 | uint64_t Weight = EdgeWeights[E]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1050 | DEBUG(dbgs() << "\t"; printEdgeWeight(dbgs(), E)); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 1051 | // Use uint32_t saturated arithmetic to adjust the incoming weights, |
| 1052 | // if needed. Sample counts in profiles are 64-bit unsigned values, |
| 1053 | // but internally branch weights are expressed as 32-bit values. |
| 1054 | if (Weight > std::numeric_limits<uint32_t>::max()) { |
| 1055 | DEBUG(dbgs() << " (saturated due to uint32_t overflow)"); |
| 1056 | Weight = std::numeric_limits<uint32_t>::max(); |
| 1057 | } |
| 1058 | Weights.push_back(static_cast<uint32_t>(Weight)); |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1059 | if (Weight != 0) { |
| 1060 | if (Weight > MaxWeight) { |
| 1061 | MaxWeight = Weight; |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1062 | MaxDestLoc = Succ->getFirstNonPHIOrDbgOrLifetime()->getDebugLoc(); |
| 1063 | } |
| 1064 | } |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | // Only set weights if there is at least one non-zero weight. |
| 1068 | // In any other case, let the analyzer set weights. |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1069 | if (MaxWeight > 0) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1070 | DEBUG(dbgs() << "SUCCESS. Found non-zero weights.\n"); |
| 1071 | TI->setMetadata(llvm::LLVMContext::MD_prof, |
| 1072 | MDB.createBranchWeights(Weights)); |
Diego Novillo | 7963ea1 | 2015-10-26 18:52:53 +0000 | [diff] [blame] | 1073 | DebugLoc BranchLoc = TI->getDebugLoc(); |
| 1074 | emitOptimizationRemark( |
| 1075 | Ctx, DEBUG_TYPE, F, MaxDestLoc, |
| 1076 | Twine("most popular destination for conditional branches at ") + |
Diego Novillo | c04270d | 2015-10-27 17:37:00 +0000 | [diff] [blame] | 1077 | ((BranchLoc) ? Twine(BranchLoc->getFilename() + ":" + |
| 1078 | Twine(BranchLoc.getLine()) + ":" + |
| 1079 | Twine(BranchLoc.getCol())) |
| 1080 | : Twine("<UNKNOWN LOCATION>"))); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1081 | } else { |
| 1082 | DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n"); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /// \brief Get the line number for the function header. |
| 1088 | /// |
| 1089 | /// This looks up function \p F in the current compilation unit and |
| 1090 | /// retrieves the line number where the function is defined. This is |
| 1091 | /// line 0 for all the samples read from the profile file. Every line |
| 1092 | /// number is relative to this line. |
| 1093 | /// |
| 1094 | /// \param F Function object to query. |
| 1095 | /// |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 1096 | /// \returns the line number where \p F is defined. If it returns 0, |
| 1097 | /// it means that there is no debug information available for \p F. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 1098 | unsigned SampleProfileLoader::getFunctionLoc(Function &F) { |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 1099 | if (DISubprogram *S = F.getSubprogram()) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 1100 | return S->getLine(); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1101 | |
Diego Novillo | aa55507 | 2015-10-27 18:41:46 +0000 | [diff] [blame] | 1102 | // If the start of \p F is missing, emit a diagnostic to inform the user |
Diego Novillo | 8027b80 | 2014-10-22 12:59:00 +0000 | [diff] [blame] | 1103 | // about the missed opportunity. |
David Blaikie | 6107968 | 2014-03-16 01:36:18 +0000 | [diff] [blame] | 1104 | F.getContext().diagnose(DiagnosticInfoSampleProfile( |
Diego Novillo | a67c0b4 | 2014-10-22 13:36:35 +0000 | [diff] [blame] | 1105 | "No debug information found in function " + F.getName() + |
| 1106 | ": Function profile not used", |
| 1107 | DS_Warning)); |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 1108 | return 0; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Diego Novillo | 7732ae4 | 2015-08-26 20:00:27 +0000 | [diff] [blame] | 1111 | void SampleProfileLoader::computeDominanceAndLoopInfo(Function &F) { |
| 1112 | DT.reset(new DominatorTree); |
| 1113 | DT->recalculate(F); |
| 1114 | |
| 1115 | PDT.reset(new DominatorTreeBase<BasicBlock>(true)); |
| 1116 | PDT->recalculate(F); |
| 1117 | |
| 1118 | LI.reset(new LoopInfo); |
| 1119 | LI->analyze(*DT); |
| 1120 | } |
| 1121 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1122 | /// \brief Generate branch weight metadata for all branches in \p F. |
| 1123 | /// |
| 1124 | /// Branch weights are computed out of instruction samples using a |
| 1125 | /// propagation heuristic. Propagation proceeds in 3 phases: |
| 1126 | /// |
| 1127 | /// 1- Assignment of block weights. All the basic blocks in the function |
| 1128 | /// are initial assigned the same weight as their most frequently |
| 1129 | /// executed instruction. |
| 1130 | /// |
| 1131 | /// 2- Creation of equivalence classes. Since samples may be missing from |
| 1132 | /// blocks, we can fill in the gaps by setting the weights of all the |
| 1133 | /// blocks in the same equivalence class to the same weight. To compute |
| 1134 | /// the concept of equivalence, we use dominance and loop information. |
| 1135 | /// Two blocks B1 and B2 are in the same equivalence class if B1 |
| 1136 | /// dominates B2, B2 post-dominates B1 and both are in the same loop. |
| 1137 | /// |
| 1138 | /// 3- Propagation of block weights into edges. This uses a simple |
| 1139 | /// propagation heuristic. The following rules are applied to every |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1140 | /// block BB in the CFG: |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1141 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1142 | /// - If BB has a single predecessor/successor, then the weight |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1143 | /// of that edge is the weight of the block. |
| 1144 | /// |
| 1145 | /// - If all the edges are known except one, and the weight of the |
| 1146 | /// block is already known, the weight of the unknown edge will |
| 1147 | /// be the weight of the block minus the sum of all the known |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1148 | /// edges. If the sum of all the known edges is larger than BB's weight, |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1149 | /// we set the unknown edge weight to zero. |
| 1150 | /// |
| 1151 | /// - If there is a self-referential edge, and the weight of the block is |
| 1152 | /// known, the weight for that edge is set to the weight of the block |
| 1153 | /// minus the weight of the other incoming edges to that block (if |
| 1154 | /// known). |
| 1155 | /// |
| 1156 | /// Since this propagation is not guaranteed to finalize for every CFG, we |
| 1157 | /// only allow it to proceed for a limited number of iterations (controlled |
| 1158 | /// by -sample-profile-max-propagate-iterations). |
| 1159 | /// |
| 1160 | /// FIXME: Try to replace this propagation heuristic with a scheme |
| 1161 | /// that is guaranteed to finalize. A work-list approach similar to |
| 1162 | /// the standard value propagation algorithm used by SSA-CCP might |
| 1163 | /// work here. |
| 1164 | /// |
| 1165 | /// Once all the branch weights are computed, we emit the MD_prof |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 1166 | /// metadata on BB using the computed values for each of its branches. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1167 | /// |
| 1168 | /// \param F The function to query. |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 1169 | /// |
| 1170 | /// \returns true if \p F was modified. Returns false, otherwise. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 1171 | bool SampleProfileLoader::emitAnnotations(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1172 | bool Changed = false; |
| 1173 | |
Dehao Chen | 41dc5a6 | 2015-10-09 16:50:16 +0000 | [diff] [blame] | 1174 | if (getFunctionLoc(F) == 0) |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 1175 | return false; |
| 1176 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1177 | DEBUG(dbgs() << "Line number for the first instruction in " << F.getName() |
Dehao Chen | 41dc5a6 | 2015-10-09 16:50:16 +0000 | [diff] [blame] | 1178 | << ": " << getFunctionLoc(F) << "\n"); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1179 | |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 1180 | Changed |= emitInlineHints(F); |
| 1181 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 1182 | Changed |= inlineHotFunctions(F); |
| 1183 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1184 | // Compute basic block weights. |
| 1185 | Changed |= computeBlockWeights(F); |
| 1186 | |
| 1187 | if (Changed) { |
Diego Novillo | 7732ae4 | 2015-08-26 20:00:27 +0000 | [diff] [blame] | 1188 | // Compute dominance and loop info needed for propagation. |
| 1189 | computeDominanceAndLoopInfo(F); |
| 1190 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1191 | // Find equivalence classes. |
| 1192 | findEquivalenceClasses(F); |
| 1193 | |
| 1194 | // Propagate weights to all edges. |
| 1195 | propagateWeights(F); |
| 1196 | } |
| 1197 | |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 1198 | // If coverage checking was requested, compute it now. |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 1199 | if (SampleProfileRecordCoverage) { |
| 1200 | unsigned Used = CoverageTracker.countUsedRecords(Samples); |
| 1201 | unsigned Total = CoverageTracker.countBodyRecords(Samples); |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 1202 | unsigned Coverage = CoverageTracker.computeCoverage(Used, Total); |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 1203 | if (Coverage < SampleProfileRecordCoverage) { |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 1204 | F.getContext().diagnose(DiagnosticInfoSampleProfile( |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 1205 | F.getSubprogram()->getFilename(), getFunctionLoc(F), |
Diego Novillo | f9ed08e | 2015-10-31 21:53:58 +0000 | [diff] [blame] | 1206 | Twine(Used) + " of " + Twine(Total) + " available profile records (" + |
| 1207 | Twine(Coverage) + "%) were applied", |
| 1208 | DS_Warning)); |
| 1209 | } |
| 1210 | } |
| 1211 | |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 1212 | if (SampleProfileSampleCoverage) { |
| 1213 | uint64_t Used = CoverageTracker.getTotalUsedSamples(); |
| 1214 | uint64_t Total = CoverageTracker.countBodySamples(Samples); |
| 1215 | unsigned Coverage = CoverageTracker.computeCoverage(Used, Total); |
| 1216 | if (Coverage < SampleProfileSampleCoverage) { |
| 1217 | F.getContext().diagnose(DiagnosticInfoSampleProfile( |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 1218 | F.getSubprogram()->getFilename(), getFunctionLoc(F), |
Diego Novillo | 243ea6a | 2015-11-23 20:12:21 +0000 | [diff] [blame] | 1219 | Twine(Used) + " of " + Twine(Total) + " available profile samples (" + |
| 1220 | Twine(Coverage) + "%) were applied", |
| 1221 | DS_Warning)); |
| 1222 | } |
| 1223 | } |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1224 | return Changed; |
| 1225 | } |
| 1226 | |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 1227 | char SampleProfileLoaderLegacyPass::ID = 0; |
| 1228 | INITIALIZE_PASS_BEGIN(SampleProfileLoaderLegacyPass, "sample-profile", |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1229 | "Sample Profile loader", false, false) |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 1230 | INITIALIZE_PASS_END(SampleProfileLoaderLegacyPass, "sample-profile", |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1231 | "Sample Profile loader", false, false) |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 1232 | |
| 1233 | bool SampleProfileLoader::doInitialization(Module &M) { |
Diego Novillo | 7732ae4 | 2015-08-26 20:00:27 +0000 | [diff] [blame] | 1234 | auto &Ctx = M.getContext(); |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1235 | auto ReaderOrErr = SampleProfileReader::create(Filename, Ctx); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 1236 | if (std::error_code EC = ReaderOrErr.getError()) { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1237 | std::string Msg = "Could not open profile: " + EC.message(); |
David Blaikie | 2297a91 | 2015-11-02 20:01:13 +0000 | [diff] [blame] | 1238 | Ctx.diagnose(DiagnosticInfoSampleProfile(Filename, Msg)); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1239 | return false; |
| 1240 | } |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 1241 | Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1242 | ProfileIsValid = (Reader->read() == sampleprof_error::success); |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 1243 | return true; |
| 1244 | } |
| 1245 | |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1246 | ModulePass *llvm::createSampleProfileLoaderPass() { |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 1247 | return new SampleProfileLoaderLegacyPass(SampleProfileFile); |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1250 | ModulePass *llvm::createSampleProfileLoaderPass(StringRef Name) { |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 1251 | return new SampleProfileLoaderLegacyPass(Name); |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1254 | bool SampleProfileLoader::runOnModule(Module &M) { |
Diego Novillo | a8a3bd2 | 2015-10-28 17:40:22 +0000 | [diff] [blame] | 1255 | if (!ProfileIsValid) |
| 1256 | return false; |
| 1257 | |
Diego Novillo | 84f06cc | 2015-11-27 23:14:51 +0000 | [diff] [blame] | 1258 | // Compute the total number of samples collected in this profile. |
| 1259 | for (const auto &I : Reader->getProfiles()) |
| 1260 | TotalCollectedSamples += I.second.getTotalSamples(); |
| 1261 | |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1262 | bool retval = false; |
| 1263 | for (auto &F : M) |
Diego Novillo | a8a3bd2 | 2015-10-28 17:40:22 +0000 | [diff] [blame] | 1264 | if (!F.isDeclaration()) { |
| 1265 | clearFunctionData(); |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1266 | retval |= runOnFunction(F); |
Diego Novillo | a8a3bd2 | 2015-10-28 17:40:22 +0000 | [diff] [blame] | 1267 | } |
Diego Novillo | 4d71113 | 2015-08-25 15:25:11 +0000 | [diff] [blame] | 1268 | return retval; |
| 1269 | } |
| 1270 | |
Xinliang David Li | e897edb | 2016-05-27 22:30:44 +0000 | [diff] [blame^] | 1271 | bool SampleProfileLoaderLegacyPass::runOnModule(Module &M) { |
| 1272 | return SampleLoader.runOnModule(M); |
| 1273 | } |
| 1274 | |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 1275 | bool SampleProfileLoader::runOnFunction(Function &F) { |
Dehao Chen | 6c73b49 | 2016-02-22 22:46:21 +0000 | [diff] [blame] | 1276 | F.setEntryCount(0); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 1277 | Samples = Reader->getSamplesFor(F); |
| 1278 | if (!Samples->empty()) |
| 1279 | return emitAnnotations(F); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 1280 | return false; |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 1281 | } |