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