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