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