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 | |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallSet.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringRef.h" |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/PostDominators.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DebugInfo.h" |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Dominators.h" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Function.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 37 | #include "llvm/IR/InstIterator.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" |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Logan Chien | 61c6df0 | 2014-02-22 06:34:10 +0000 | [diff] [blame] | 48 | #include <cctype> |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 49 | |
| 50 | using namespace llvm; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 51 | using namespace sampleprof; |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 52 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "sample-profile" |
| 54 | |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 55 | // Command line option to specify the file to read samples from. This is |
| 56 | // mainly used for debugging. |
| 57 | static cl::opt<std::string> SampleProfileFile( |
| 58 | "sample-profile-file", cl::init(""), cl::value_desc("filename"), |
| 59 | cl::desc("Profile file loaded by -sample-profile"), cl::Hidden); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 60 | static cl::opt<unsigned> SampleProfileMaxPropagateIterations( |
| 61 | "sample-profile-max-propagate-iterations", cl::init(100), |
| 62 | cl::desc("Maximum number of iterations to go through when propagating " |
| 63 | "sample block/edge weights through the CFG.")); |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 64 | |
| 65 | namespace { |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 66 | typedef DenseMap<BasicBlock *, unsigned> BlockWeightMap; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 67 | typedef DenseMap<BasicBlock *, BasicBlock *> EquivalenceClassMap; |
| 68 | typedef std::pair<BasicBlock *, BasicBlock *> Edge; |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 69 | typedef DenseMap<Edge, unsigned> EdgeWeightMap; |
Diego Novillo | 7095908 | 2014-03-14 22:07:18 +0000 | [diff] [blame] | 70 | typedef DenseMap<BasicBlock *, SmallVector<BasicBlock *, 8>> BlockEdgeMap; |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 71 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 72 | /// \brief Sample profile pass. |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 73 | /// |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 74 | /// This pass reads profile data from the file specified by |
| 75 | /// -sample-profile-file and annotates every affected function with the |
| 76 | /// profile information found in that file. |
| 77 | class SampleProfileLoader : public FunctionPass { |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 78 | public: |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 79 | // Class identification, replacement for typeinfo |
| 80 | static char ID; |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 81 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 82 | SampleProfileLoader(StringRef Name = SampleProfileFile) |
| 83 | : FunctionPass(ID), DT(nullptr), PDT(nullptr), LI(nullptr), Ctx(nullptr), |
| 84 | Reader(), Samples(nullptr), Filename(Name), ProfileIsValid(false) { |
| 85 | initializeSampleProfileLoaderPass(*PassRegistry::getPassRegistry()); |
| 86 | } |
| 87 | |
| 88 | bool doInitialization(Module &M) override; |
| 89 | |
| 90 | void dump() { Reader->dump(); } |
| 91 | |
| 92 | const char *getPassName() const override { return "Sample profile pass"; } |
| 93 | |
| 94 | bool runOnFunction(Function &F) override; |
| 95 | |
| 96 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 97 | AU.setPreservesCFG(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 98 | AU.addRequired<LoopInfoWrapperPass>(); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 99 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 100 | AU.addRequired<PostDominatorTree>(); |
| 101 | } |
| 102 | |
| 103 | protected: |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 104 | unsigned getFunctionLoc(Function &F); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 105 | bool emitAnnotations(Function &F); |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 106 | unsigned getInstWeight(Instruction &I); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 107 | unsigned getBlockWeight(BasicBlock *BB); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 108 | void printEdgeWeight(raw_ostream &OS, Edge E); |
| 109 | void printBlockWeight(raw_ostream &OS, BasicBlock *BB); |
| 110 | void printBlockEquivalence(raw_ostream &OS, BasicBlock *BB); |
| 111 | bool computeBlockWeights(Function &F); |
| 112 | void findEquivalenceClasses(Function &F); |
| 113 | void findEquivalencesFor(BasicBlock *BB1, |
| 114 | SmallVector<BasicBlock *, 8> Descendants, |
| 115 | DominatorTreeBase<BasicBlock> *DomTree); |
| 116 | void propagateWeights(Function &F); |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 117 | unsigned visitEdge(Edge E, unsigned *NumUnknownEdges, Edge *UnknownEdge); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 118 | void buildEdges(Function &F); |
| 119 | bool propagateThroughEdges(Function &F); |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 120 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 121 | /// \brief Line number for the function header. Used to compute absolute |
| 122 | /// line numbers from the relative line numbers found in the profile. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 123 | unsigned HeaderLineno; |
| 124 | |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 125 | /// \brief Map basic blocks to their computed weights. |
| 126 | /// |
| 127 | /// The weight of a basic block is defined to be the maximum |
| 128 | /// of all the instruction weights in that block. |
| 129 | BlockWeightMap BlockWeights; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 130 | |
| 131 | /// \brief Map edges to their computed weights. |
| 132 | /// |
| 133 | /// Edge weights are computed by propagating basic block weights in |
| 134 | /// SampleProfile::propagateWeights. |
| 135 | EdgeWeightMap EdgeWeights; |
| 136 | |
| 137 | /// \brief Set of visited blocks during propagation. |
| 138 | SmallPtrSet<BasicBlock *, 128> VisitedBlocks; |
| 139 | |
| 140 | /// \brief Set of visited edges during propagation. |
| 141 | SmallSet<Edge, 128> VisitedEdges; |
| 142 | |
| 143 | /// \brief Equivalence classes for block weights. |
| 144 | /// |
| 145 | /// Two blocks BB1 and BB2 are in the same equivalence class if they |
| 146 | /// dominate and post-dominate each other, and they are in the same loop |
| 147 | /// nest. When this happens, the two blocks are guaranteed to execute |
| 148 | /// the same number of times. |
| 149 | EquivalenceClassMap EquivalenceClass; |
| 150 | |
| 151 | /// \brief Dominance, post-dominance and loop information. |
| 152 | DominatorTree *DT; |
| 153 | PostDominatorTree *PDT; |
| 154 | LoopInfo *LI; |
| 155 | |
| 156 | /// \brief Predecessors for each basic block in the CFG. |
| 157 | BlockEdgeMap Predecessors; |
| 158 | |
| 159 | /// \brief Successors for each basic block in the CFG. |
| 160 | BlockEdgeMap Successors; |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 161 | |
| 162 | /// \brief LLVM context holding the debug data we need. |
| 163 | LLVMContext *Ctx; |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +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 | }; |
| 177 | } |
| 178 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 179 | /// \brief Print the weight of edge \p E on stream \p OS. |
| 180 | /// |
| 181 | /// \param OS Stream to emit the output to. |
| 182 | /// \param E Edge to print. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 183 | void SampleProfileLoader::printEdgeWeight(raw_ostream &OS, Edge E) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 184 | OS << "weight[" << E.first->getName() << "->" << E.second->getName() |
| 185 | << "]: " << EdgeWeights[E] << "\n"; |
| 186 | } |
| 187 | |
| 188 | /// \brief Print the equivalence class of block \p BB on stream \p OS. |
| 189 | /// |
| 190 | /// \param OS Stream to emit the output to. |
| 191 | /// \param BB Block to print. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 192 | void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS, |
| 193 | BasicBlock *BB) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 194 | BasicBlock *Equiv = EquivalenceClass[BB]; |
| 195 | OS << "equivalence[" << BB->getName() |
| 196 | << "]: " << ((Equiv) ? EquivalenceClass[BB]->getName() : "NONE") << "\n"; |
| 197 | } |
| 198 | |
| 199 | /// \brief Print the weight of block \p BB on stream \p OS. |
| 200 | /// |
| 201 | /// \param OS Stream to emit the output to. |
| 202 | /// \param BB Block to print. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 203 | void SampleProfileLoader::printBlockWeight(raw_ostream &OS, BasicBlock *BB) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 204 | OS << "weight[" << BB->getName() << "]: " << BlockWeights[BB] << "\n"; |
| 205 | } |
| 206 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 207 | /// \brief Get the weight for an instruction. |
| 208 | /// |
| 209 | /// The "weight" of an instruction \p Inst is the number of samples |
| 210 | /// collected on that instruction at runtime. To retrieve it, we |
| 211 | /// need to compute the line number of \p Inst relative to the start of its |
| 212 | /// function. We use HeaderLineno to compute the offset. We then |
| 213 | /// look up the samples collected for \p Inst using BodySamples. |
| 214 | /// |
| 215 | /// \param Inst Instruction to query. |
| 216 | /// |
| 217 | /// \returns The profiled weight of I. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 218 | unsigned SampleProfileLoader::getInstWeight(Instruction &Inst) { |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 219 | DebugLoc DLoc = Inst.getDebugLoc(); |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 220 | if (!DLoc) |
Duncan P. N. Exon Smith | 41a1546 | 2015-03-20 00:56:55 +0000 | [diff] [blame] | 221 | return 0; |
| 222 | |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 223 | unsigned Lineno = DLoc.getLine(); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 224 | if (Lineno < HeaderLineno) |
| 225 | return 0; |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 226 | |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 227 | DILocation DIL = DLoc.get(); |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 228 | int LOffset = Lineno - HeaderLineno; |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 229 | unsigned Discriminator = DIL->getDiscriminator(); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 230 | unsigned Weight = Samples->samplesAt(LOffset, Discriminator); |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 231 | DEBUG(dbgs() << " " << Lineno << "." << Discriminator << ":" << Inst |
| 232 | << " (line offset: " << LOffset << "." << Discriminator |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 233 | << " - weight: " << Weight << ")\n"); |
| 234 | return Weight; |
| 235 | } |
| 236 | |
| 237 | /// \brief Compute the weight of a basic block. |
| 238 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 239 | /// The weight of basic block \p BB is the maximum weight of all the |
| 240 | /// instructions in BB. The weight of \p BB is computed and cached in |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 241 | /// the BlockWeights map. |
| 242 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 243 | /// \param BB The basic block to query. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 244 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 245 | /// \returns The computed weight of BB. |
| 246 | unsigned SampleProfileLoader::getBlockWeight(BasicBlock *BB) { |
| 247 | // If we've computed BB's weight before, return it. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 248 | std::pair<BlockWeightMap::iterator, bool> Entry = |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 249 | BlockWeights.insert(std::make_pair(BB, 0)); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 250 | if (!Entry.second) |
| 251 | return Entry.first->second; |
| 252 | |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 253 | // Otherwise, compute and cache BB's weight. |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 254 | unsigned Weight = 0; |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 255 | for (auto &I : BB->getInstList()) { |
Diego Novillo | b368b7d | 2014-10-22 16:51:50 +0000 | [diff] [blame] | 256 | unsigned InstWeight = getInstWeight(I); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 257 | if (InstWeight > Weight) |
| 258 | Weight = InstWeight; |
| 259 | } |
| 260 | Entry.first->second = Weight; |
| 261 | return Weight; |
| 262 | } |
| 263 | |
| 264 | /// \brief Compute and store the weights of every basic block. |
| 265 | /// |
| 266 | /// This populates the BlockWeights map by computing |
| 267 | /// the weights of every basic block in the CFG. |
| 268 | /// |
| 269 | /// \param F The function to query. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 270 | bool SampleProfileLoader::computeBlockWeights(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 271 | bool Changed = false; |
| 272 | DEBUG(dbgs() << "Block weights\n"); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 273 | for (auto &BB : F) { |
| 274 | unsigned Weight = getBlockWeight(&BB); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 275 | Changed |= (Weight > 0); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 276 | DEBUG(printBlockWeight(dbgs(), &BB)); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return Changed; |
| 280 | } |
| 281 | |
| 282 | /// \brief Find equivalence classes for the given block. |
| 283 | /// |
| 284 | /// This finds all the blocks that are guaranteed to execute the same |
| 285 | /// number of times as \p BB1. To do this, it traverses all the the |
| 286 | /// descendants of \p BB1 in the dominator or post-dominator tree. |
| 287 | /// |
| 288 | /// A block BB2 will be in the same equivalence class as \p BB1 if |
| 289 | /// the following holds: |
| 290 | /// |
| 291 | /// 1- \p BB1 is a descendant of BB2 in the opposite tree. So, if BB2 |
| 292 | /// is a descendant of \p BB1 in the dominator tree, then BB2 should |
| 293 | /// dominate BB1 in the post-dominator tree. |
| 294 | /// |
| 295 | /// 2- Both BB2 and \p BB1 must be in the same loop. |
| 296 | /// |
| 297 | /// For every block BB2 that meets those two requirements, we set BB2's |
| 298 | /// equivalence class to \p BB1. |
| 299 | /// |
| 300 | /// \param BB1 Block to check. |
| 301 | /// \param Descendants Descendants of \p BB1 in either the dom or pdom tree. |
| 302 | /// \param DomTree Opposite dominator tree. If \p Descendants is filled |
| 303 | /// with blocks from \p BB1's dominator tree, then |
| 304 | /// this is the post-dominator tree, and vice versa. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 305 | void SampleProfileLoader::findEquivalencesFor( |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 306 | BasicBlock *BB1, SmallVector<BasicBlock *, 8> Descendants, |
| 307 | DominatorTreeBase<BasicBlock> *DomTree) { |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 308 | for (auto *BB2 : Descendants) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 309 | bool IsDomParent = DomTree->dominates(BB2, BB1); |
| 310 | bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 311 | if (BB1 != BB2 && VisitedBlocks.insert(BB2).second && IsDomParent && |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 312 | IsInSameLoop) { |
| 313 | EquivalenceClass[BB2] = BB1; |
| 314 | |
| 315 | // If BB2 is heavier than BB1, make BB2 have the same weight |
| 316 | // as BB1. |
| 317 | // |
| 318 | // Note that we don't worry about the opposite situation here |
| 319 | // (when BB2 is lighter than BB1). We will deal with this |
| 320 | // during the propagation phase. Right now, we just want to |
| 321 | // make sure that BB1 has the largest weight of all the |
| 322 | // members of its equivalence set. |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 323 | unsigned &BB1Weight = BlockWeights[BB1]; |
| 324 | unsigned &BB2Weight = BlockWeights[BB2]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 325 | BB1Weight = std::max(BB1Weight, BB2Weight); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /// \brief Find equivalence classes. |
| 331 | /// |
| 332 | /// Since samples may be missing from blocks, we can fill in the gaps by setting |
| 333 | /// the weights of all the blocks in the same equivalence class to the same |
| 334 | /// weight. To compute the concept of equivalence, we use dominance and loop |
| 335 | /// information. Two blocks B1 and B2 are in the same equivalence class if B1 |
| 336 | /// dominates B2, B2 post-dominates B1 and both are in the same loop. |
| 337 | /// |
| 338 | /// \param F The function to query. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 339 | void SampleProfileLoader::findEquivalenceClasses(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 340 | SmallVector<BasicBlock *, 8> DominatedBBs; |
| 341 | DEBUG(dbgs() << "\nBlock equivalence classes\n"); |
| 342 | // Find equivalence sets based on dominance and post-dominance information. |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 343 | for (auto &BB : F) { |
| 344 | BasicBlock *BB1 = &BB; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 345 | |
| 346 | // Compute BB1's equivalence class once. |
| 347 | if (EquivalenceClass.count(BB1)) { |
| 348 | DEBUG(printBlockEquivalence(dbgs(), BB1)); |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | // By default, blocks are in their own equivalence class. |
| 353 | EquivalenceClass[BB1] = BB1; |
| 354 | |
| 355 | // Traverse all the blocks dominated by BB1. We are looking for |
| 356 | // every basic block BB2 such that: |
| 357 | // |
| 358 | // 1- BB1 dominates BB2. |
| 359 | // 2- BB2 post-dominates BB1. |
| 360 | // 3- BB1 and BB2 are in the same loop nest. |
| 361 | // |
| 362 | // If all those conditions hold, it means that BB2 is executed |
| 363 | // as many times as BB1, so they are placed in the same equivalence |
| 364 | // class by making BB2's equivalence class be BB1. |
| 365 | DominatedBBs.clear(); |
| 366 | DT->getDescendants(BB1, DominatedBBs); |
| 367 | findEquivalencesFor(BB1, DominatedBBs, PDT->DT); |
| 368 | |
| 369 | // Repeat the same logic for all the blocks post-dominated by BB1. |
| 370 | // We are looking for every basic block BB2 such that: |
| 371 | // |
| 372 | // 1- BB1 post-dominates BB2. |
| 373 | // 2- BB2 dominates BB1. |
| 374 | // 3- BB1 and BB2 are in the same loop nest. |
| 375 | // |
| 376 | // If all those conditions hold, BB2's equivalence class is BB1. |
| 377 | DominatedBBs.clear(); |
| 378 | PDT->getDescendants(BB1, DominatedBBs); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 379 | findEquivalencesFor(BB1, DominatedBBs, DT); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 380 | |
| 381 | DEBUG(printBlockEquivalence(dbgs(), BB1)); |
| 382 | } |
| 383 | |
| 384 | // Assign weights to equivalence classes. |
| 385 | // |
| 386 | // All the basic blocks in the same equivalence class will execute |
| 387 | // the same number of times. Since we know that the head block in |
| 388 | // each equivalence class has the largest weight, assign that weight |
| 389 | // to all the blocks in that equivalence class. |
| 390 | 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] | 391 | for (auto &BI : F) { |
| 392 | BasicBlock *BB = &BI; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 393 | BasicBlock *EquivBB = EquivalenceClass[BB]; |
| 394 | if (BB != EquivBB) |
| 395 | BlockWeights[BB] = BlockWeights[EquivBB]; |
| 396 | DEBUG(printBlockWeight(dbgs(), BB)); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /// \brief Visit the given edge to decide if it has a valid weight. |
| 401 | /// |
| 402 | /// If \p E has not been visited before, we copy to \p UnknownEdge |
| 403 | /// and increment the count of unknown edges. |
| 404 | /// |
| 405 | /// \param E Edge to visit. |
| 406 | /// \param NumUnknownEdges Current number of unknown edges. |
| 407 | /// \param UnknownEdge Set if E has not been visited before. |
| 408 | /// |
| 409 | /// \returns E's weight, if known. Otherwise, return 0. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 410 | unsigned SampleProfileLoader::visitEdge(Edge E, unsigned *NumUnknownEdges, |
| 411 | Edge *UnknownEdge) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 412 | if (!VisitedEdges.count(E)) { |
| 413 | (*NumUnknownEdges)++; |
| 414 | *UnknownEdge = E; |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | return EdgeWeights[E]; |
| 419 | } |
| 420 | |
| 421 | /// \brief Propagate weights through incoming/outgoing edges. |
| 422 | /// |
| 423 | /// If the weight of a basic block is known, and there is only one edge |
| 424 | /// with an unknown weight, we can calculate the weight of that edge. |
| 425 | /// |
| 426 | /// Similarly, if all the edges have a known count, we can calculate the |
| 427 | /// count of the basic block, if needed. |
| 428 | /// |
| 429 | /// \param F Function to process. |
| 430 | /// |
| 431 | /// \returns True if new weights were assigned to edges or blocks. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 432 | bool SampleProfileLoader::propagateThroughEdges(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 433 | bool Changed = false; |
| 434 | DEBUG(dbgs() << "\nPropagation through edges\n"); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 435 | for (auto &BI : F) { |
| 436 | BasicBlock *BB = &BI; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 437 | |
| 438 | // Visit all the predecessor and successor edges to determine |
| 439 | // which ones have a weight assigned already. Note that it doesn't |
| 440 | // matter that we only keep track of a single unknown edge. The |
| 441 | // only case we are interested in handling is when only a single |
| 442 | // edge is unknown (see setEdgeOrBlockWeight). |
| 443 | for (unsigned i = 0; i < 2; i++) { |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 444 | unsigned TotalWeight = 0; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 445 | unsigned NumUnknownEdges = 0; |
| 446 | Edge UnknownEdge, SelfReferentialEdge; |
| 447 | |
| 448 | if (i == 0) { |
| 449 | // First, visit all predecessor edges. |
Diego Novillo | b368b7d | 2014-10-22 16:51:50 +0000 | [diff] [blame] | 450 | for (auto *Pred : Predecessors[BB]) { |
| 451 | Edge E = std::make_pair(Pred, BB); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 452 | TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge); |
| 453 | if (E.first == E.second) |
| 454 | SelfReferentialEdge = E; |
| 455 | } |
| 456 | } else { |
| 457 | // On the second round, visit all successor edges. |
Diego Novillo | b368b7d | 2014-10-22 16:51:50 +0000 | [diff] [blame] | 458 | for (auto *Succ : Successors[BB]) { |
| 459 | Edge E = std::make_pair(BB, Succ); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 460 | TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // After visiting all the edges, there are three cases that we |
| 465 | // can handle immediately: |
| 466 | // |
| 467 | // - All the edge weights are known (i.e., NumUnknownEdges == 0). |
| 468 | // In this case, we simply check that the sum of all the edges |
| 469 | // is the same as BB's weight. If not, we change BB's weight |
| 470 | // to match. Additionally, if BB had not been visited before, |
| 471 | // we mark it visited. |
| 472 | // |
| 473 | // - Only one edge is unknown and BB has already been visited. |
| 474 | // In this case, we can compute the weight of the edge by |
| 475 | // subtracting the total block weight from all the known |
| 476 | // edge weights. If the edges weight more than BB, then the |
| 477 | // edge of the last remaining edge is set to zero. |
| 478 | // |
| 479 | // - There exists a self-referential edge and the weight of BB is |
| 480 | // known. In this case, this edge can be based on BB's weight. |
| 481 | // We add up all the other known edges and set the weight on |
| 482 | // the self-referential edge as we did in the previous case. |
| 483 | // |
| 484 | // In any other case, we must continue iterating. Eventually, |
| 485 | // all edges will get a weight, or iteration will stop when |
| 486 | // it reaches SampleProfileMaxPropagateIterations. |
| 487 | if (NumUnknownEdges <= 1) { |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 488 | unsigned &BBWeight = BlockWeights[BB]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 489 | if (NumUnknownEdges == 0) { |
| 490 | // If we already know the weight of all edges, the weight of the |
| 491 | // basic block can be computed. It should be no larger than the sum |
| 492 | // of all edge weights. |
| 493 | if (TotalWeight > BBWeight) { |
| 494 | BBWeight = TotalWeight; |
| 495 | Changed = true; |
| 496 | DEBUG(dbgs() << "All edge weights for " << BB->getName() |
| 497 | << " known. Set weight for block: "; |
| 498 | printBlockWeight(dbgs(), BB);); |
| 499 | } |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 500 | if (VisitedBlocks.insert(BB).second) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 501 | Changed = true; |
| 502 | } else if (NumUnknownEdges == 1 && VisitedBlocks.count(BB)) { |
| 503 | // If there is a single unknown edge and the block has been |
| 504 | // visited, then we can compute E's weight. |
| 505 | if (BBWeight >= TotalWeight) |
| 506 | EdgeWeights[UnknownEdge] = BBWeight - TotalWeight; |
| 507 | else |
| 508 | EdgeWeights[UnknownEdge] = 0; |
| 509 | VisitedEdges.insert(UnknownEdge); |
| 510 | Changed = true; |
| 511 | DEBUG(dbgs() << "Set weight for edge: "; |
| 512 | printEdgeWeight(dbgs(), UnknownEdge)); |
| 513 | } |
| 514 | } else if (SelfReferentialEdge.first && VisitedBlocks.count(BB)) { |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 515 | unsigned &BBWeight = BlockWeights[BB]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 516 | // We have a self-referential edge and the weight of BB is known. |
| 517 | if (BBWeight >= TotalWeight) |
| 518 | EdgeWeights[SelfReferentialEdge] = BBWeight - TotalWeight; |
| 519 | else |
| 520 | EdgeWeights[SelfReferentialEdge] = 0; |
| 521 | VisitedEdges.insert(SelfReferentialEdge); |
| 522 | Changed = true; |
| 523 | DEBUG(dbgs() << "Set self-referential edge weight to: "; |
| 524 | printEdgeWeight(dbgs(), SelfReferentialEdge)); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return Changed; |
| 530 | } |
| 531 | |
| 532 | /// \brief Build in/out edge lists for each basic block in the CFG. |
| 533 | /// |
| 534 | /// We are interested in unique edges. If a block B1 has multiple |
| 535 | /// 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] | 536 | void SampleProfileLoader::buildEdges(Function &F) { |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 537 | for (auto &BI : F) { |
| 538 | BasicBlock *B1 = &BI; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 539 | |
| 540 | // Add predecessors for B1. |
| 541 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 542 | if (!Predecessors[B1].empty()) |
| 543 | 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] | 544 | for (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) { |
| 545 | BasicBlock *B2 = *PI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 546 | if (Visited.insert(B2).second) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 547 | Predecessors[B1].push_back(B2); |
| 548 | } |
| 549 | |
| 550 | // Add successors for B1. |
| 551 | Visited.clear(); |
| 552 | if (!Successors[B1].empty()) |
| 553 | 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] | 554 | for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) { |
| 555 | BasicBlock *B2 = *SI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 556 | if (Visited.insert(B2).second) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 557 | Successors[B1].push_back(B2); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /// \brief Propagate weights into edges |
| 563 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 564 | /// The following rules are applied to every block BB in the CFG: |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 565 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 566 | /// - If BB has a single predecessor/successor, then the weight |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 567 | /// of that edge is the weight of the block. |
| 568 | /// |
| 569 | /// - If all incoming or outgoing edges are known except one, and the |
| 570 | /// weight of the block is already known, the weight of the unknown |
| 571 | /// 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] | 572 | /// 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] | 573 | /// we set the unknown edge weight to zero. |
| 574 | /// |
| 575 | /// - If there is a self-referential edge, and the weight of the block is |
| 576 | /// known, the weight for that edge is set to the weight of the block |
| 577 | /// minus the weight of the other incoming edges to that block (if |
| 578 | /// known). |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 579 | void SampleProfileLoader::propagateWeights(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 580 | bool Changed = true; |
| 581 | unsigned i = 0; |
| 582 | |
| 583 | // Before propagation starts, build, for each block, a list of |
| 584 | // unique predecessors and successors. This is necessary to handle |
| 585 | // identical edges in multiway branches. Since we visit all blocks and all |
| 586 | // edges of the CFG, it is cleaner to build these lists once at the start |
| 587 | // of the pass. |
| 588 | buildEdges(F); |
| 589 | |
| 590 | // Propagate until we converge or we go past the iteration limit. |
| 591 | while (Changed && i++ < SampleProfileMaxPropagateIterations) { |
| 592 | Changed = propagateThroughEdges(F); |
| 593 | } |
| 594 | |
| 595 | // Generate MD_prof metadata for every branch instruction using the |
| 596 | // edge weights computed during propagation. |
| 597 | DEBUG(dbgs() << "\nPropagation complete. Setting branch weights\n"); |
| 598 | MDBuilder MDB(F.getContext()); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 599 | for (auto &BI : F) { |
| 600 | BasicBlock *BB = &BI; |
| 601 | TerminatorInst *TI = BB->getTerminator(); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 602 | if (TI->getNumSuccessors() == 1) |
| 603 | continue; |
| 604 | if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) |
| 605 | continue; |
| 606 | |
| 607 | DEBUG(dbgs() << "\nGetting weights for branch at line " |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 608 | << TI->getDebugLoc().getLine() << ".\n"); |
| 609 | SmallVector<unsigned, 4> Weights; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 610 | bool AllWeightsZero = true; |
| 611 | for (unsigned I = 0; I < TI->getNumSuccessors(); ++I) { |
| 612 | BasicBlock *Succ = TI->getSuccessor(I); |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 613 | Edge E = std::make_pair(BB, Succ); |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 614 | unsigned Weight = EdgeWeights[E]; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 615 | DEBUG(dbgs() << "\t"; printEdgeWeight(dbgs(), E)); |
| 616 | Weights.push_back(Weight); |
| 617 | if (Weight != 0) |
| 618 | AllWeightsZero = false; |
| 619 | } |
| 620 | |
| 621 | // Only set weights if there is at least one non-zero weight. |
| 622 | // In any other case, let the analyzer set weights. |
| 623 | if (!AllWeightsZero) { |
| 624 | DEBUG(dbgs() << "SUCCESS. Found non-zero weights.\n"); |
| 625 | TI->setMetadata(llvm::LLVMContext::MD_prof, |
| 626 | MDB.createBranchWeights(Weights)); |
| 627 | } else { |
| 628 | DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n"); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /// \brief Get the line number for the function header. |
| 634 | /// |
| 635 | /// This looks up function \p F in the current compilation unit and |
| 636 | /// retrieves the line number where the function is defined. This is |
| 637 | /// line 0 for all the samples read from the profile file. Every line |
| 638 | /// number is relative to this line. |
| 639 | /// |
| 640 | /// \param F Function object to query. |
| 641 | /// |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 642 | /// \returns the line number where \p F is defined. If it returns 0, |
| 643 | /// it means that there is no debug information available for \p F. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 644 | unsigned SampleProfileLoader::getFunctionLoc(Function &F) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame^] | 645 | if (MDSubprogram *S = getDISubprogram(&F)) |
| 646 | return S->getLine(); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 647 | |
Diego Novillo | 8027b80 | 2014-10-22 12:59:00 +0000 | [diff] [blame] | 648 | // If could not find the start of \p F, emit a diagnostic to inform the user |
| 649 | // about the missed opportunity. |
David Blaikie | 6107968 | 2014-03-16 01:36:18 +0000 | [diff] [blame] | 650 | F.getContext().diagnose(DiagnosticInfoSampleProfile( |
Diego Novillo | a67c0b4 | 2014-10-22 13:36:35 +0000 | [diff] [blame] | 651 | "No debug information found in function " + F.getName() + |
| 652 | ": Function profile not used", |
| 653 | DS_Warning)); |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 654 | return 0; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /// \brief Generate branch weight metadata for all branches in \p F. |
| 658 | /// |
| 659 | /// Branch weights are computed out of instruction samples using a |
| 660 | /// propagation heuristic. Propagation proceeds in 3 phases: |
| 661 | /// |
| 662 | /// 1- Assignment of block weights. All the basic blocks in the function |
| 663 | /// are initial assigned the same weight as their most frequently |
| 664 | /// executed instruction. |
| 665 | /// |
| 666 | /// 2- Creation of equivalence classes. Since samples may be missing from |
| 667 | /// blocks, we can fill in the gaps by setting the weights of all the |
| 668 | /// blocks in the same equivalence class to the same weight. To compute |
| 669 | /// the concept of equivalence, we use dominance and loop information. |
| 670 | /// Two blocks B1 and B2 are in the same equivalence class if B1 |
| 671 | /// dominates B2, B2 post-dominates B1 and both are in the same loop. |
| 672 | /// |
| 673 | /// 3- Propagation of block weights into edges. This uses a simple |
| 674 | /// propagation heuristic. The following rules are applied to every |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 675 | /// block BB in the CFG: |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 676 | /// |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 677 | /// - If BB has a single predecessor/successor, then the weight |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 678 | /// of that edge is the weight of the block. |
| 679 | /// |
| 680 | /// - If all the edges are known except one, and the weight of the |
| 681 | /// block is already known, the weight of the unknown edge will |
| 682 | /// 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] | 683 | /// 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] | 684 | /// we set the unknown edge weight to zero. |
| 685 | /// |
| 686 | /// - If there is a self-referential edge, and the weight of the block is |
| 687 | /// known, the weight for that edge is set to the weight of the block |
| 688 | /// minus the weight of the other incoming edges to that block (if |
| 689 | /// known). |
| 690 | /// |
| 691 | /// Since this propagation is not guaranteed to finalize for every CFG, we |
| 692 | /// only allow it to proceed for a limited number of iterations (controlled |
| 693 | /// by -sample-profile-max-propagate-iterations). |
| 694 | /// |
| 695 | /// FIXME: Try to replace this propagation heuristic with a scheme |
| 696 | /// that is guaranteed to finalize. A work-list approach similar to |
| 697 | /// the standard value propagation algorithm used by SSA-CCP might |
| 698 | /// work here. |
| 699 | /// |
| 700 | /// Once all the branch weights are computed, we emit the MD_prof |
Diego Novillo | 19e7b7e | 2014-10-22 18:39:50 +0000 | [diff] [blame] | 701 | /// metadata on BB using the computed values for each of its branches. |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 702 | /// |
| 703 | /// \param F The function to query. |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 704 | /// |
| 705 | /// \returns true if \p F was modified. Returns false, otherwise. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 706 | bool SampleProfileLoader::emitAnnotations(Function &F) { |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 707 | bool Changed = false; |
| 708 | |
| 709 | // Initialize invariants used during computation and propagation. |
| 710 | HeaderLineno = getFunctionLoc(F); |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 711 | if (HeaderLineno == 0) |
| 712 | return false; |
| 713 | |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 714 | DEBUG(dbgs() << "Line number for the first instruction in " << F.getName() |
| 715 | << ": " << HeaderLineno << "\n"); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 716 | |
| 717 | // Compute basic block weights. |
| 718 | Changed |= computeBlockWeights(F); |
| 719 | |
| 720 | if (Changed) { |
| 721 | // Find equivalence classes. |
| 722 | findEquivalenceClasses(F); |
| 723 | |
| 724 | // Propagate weights to all edges. |
| 725 | propagateWeights(F); |
| 726 | } |
| 727 | |
| 728 | return Changed; |
| 729 | } |
| 730 | |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 731 | char SampleProfileLoader::ID = 0; |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 732 | INITIALIZE_PASS_BEGIN(SampleProfileLoader, "sample-profile", |
| 733 | "Sample Profile loader", false, false) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 734 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 735 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTree) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 736 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 737 | INITIALIZE_PASS_DEPENDENCY(AddDiscriminators) |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 738 | INITIALIZE_PASS_END(SampleProfileLoader, "sample-profile", |
| 739 | "Sample Profile loader", false, false) |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 740 | |
| 741 | bool SampleProfileLoader::doInitialization(Module &M) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 742 | auto ReaderOrErr = SampleProfileReader::create(Filename, M.getContext()); |
| 743 | if (std::error_code EC = ReaderOrErr.getError()) { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 744 | std::string Msg = "Could not open profile: " + EC.message(); |
Diego Novillo | 77a5a5f | 2014-10-30 18:48:41 +0000 | [diff] [blame] | 745 | M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg)); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 748 | Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 749 | ProfileIsValid = (Reader->read() == sampleprof_error::success); |
Diego Novillo | c0dd103 | 2013-11-26 20:37:33 +0000 | [diff] [blame] | 750 | return true; |
| 751 | } |
| 752 | |
| 753 | FunctionPass *llvm::createSampleProfileLoaderPass() { |
| 754 | return new SampleProfileLoader(SampleProfileFile); |
| 755 | } |
| 756 | |
| 757 | FunctionPass *llvm::createSampleProfileLoaderPass(StringRef Name) { |
| 758 | return new SampleProfileLoader(Name); |
| 759 | } |
| 760 | |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 761 | bool SampleProfileLoader::runOnFunction(Function &F) { |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 762 | if (!ProfileIsValid) |
| 763 | return false; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 764 | |
| 765 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 766 | PDT = &getAnalysis<PostDominatorTree>(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 767 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 768 | Ctx = &F.getParent()->getContext(); |
| 769 | Samples = Reader->getSamplesFor(F); |
| 770 | if (!Samples->empty()) |
| 771 | return emitAnnotations(F); |
Diego Novillo | 0accb3d | 2014-01-10 23:23:46 +0000 | [diff] [blame] | 772 | return false; |
Diego Novillo | 8d6568b | 2013-11-13 12:22:21 +0000 | [diff] [blame] | 773 | } |