1 //===- SampleProfile.cpp - Incorporate sample profiles into the IR --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 // This pass generates branch weight annotations on the IR:
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.
23 //===----------------------------------------------------------------------===//
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallSet.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Analysis/LoopInfo.h"
30 #include "llvm/Analysis/PostDominators.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/DiagnosticInfo.h"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/InstIterator.h"
37 #include "llvm/IR/Instructions.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/IR/MDBuilder.h"
40 #include "llvm/IR/Metadata.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/Pass.h"
43 #include "llvm/ProfileData/SampleProfReader.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Debug.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Transforms/IPO.h"
51 using namespace sampleprof;
53 #define DEBUG_TYPE "sample-profile"
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);
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."));
66 typedef DenseMap<BasicBlock *, unsigned> BlockWeightMap;
67 typedef DenseMap<BasicBlock *, BasicBlock *> EquivalenceClassMap;
68 typedef std::pair<BasicBlock *, BasicBlock *> Edge;
69 typedef DenseMap<Edge, unsigned> EdgeWeightMap;
70 typedef DenseMap<BasicBlock *, SmallVector<BasicBlock *, 8>> BlockEdgeMap;
72 /// \brief Sample profile pass.
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 ModulePass {
79 // Class identification, replacement for typeinfo
82 SampleProfileLoader(StringRef Name = SampleProfileFile)
83 : ModulePass(ID), DT(nullptr), PDT(nullptr), LI(nullptr), Reader(),
84 Samples(nullptr), Filename(Name), ProfileIsValid(false) {
85 initializeSampleProfileLoaderPass(*PassRegistry::getPassRegistry());
88 bool doInitialization(Module &M) override;
90 void dump() { Reader->dump(); }
92 const char *getPassName() const override { return "Sample profile pass"; }
94 bool runOnModule(Module &M) override;
96 void getAnalysisUsage(AnalysisUsage &AU) const override {
101 bool runOnFunction(Function &F);
102 unsigned getFunctionLoc(Function &F);
103 bool emitAnnotations(Function &F);
104 unsigned getInstWeight(Instruction &I);
105 unsigned getBlockWeight(BasicBlock *BB);
106 void printEdgeWeight(raw_ostream &OS, Edge E);
107 void printBlockWeight(raw_ostream &OS, BasicBlock *BB);
108 void printBlockEquivalence(raw_ostream &OS, BasicBlock *BB);
109 bool computeBlockWeights(Function &F);
110 void findEquivalenceClasses(Function &F);
111 void findEquivalencesFor(BasicBlock *BB1,
112 SmallVector<BasicBlock *, 8> Descendants,
113 DominatorTreeBase<BasicBlock> *DomTree);
114 void propagateWeights(Function &F);
115 unsigned visitEdge(Edge E, unsigned *NumUnknownEdges, Edge *UnknownEdge);
116 void buildEdges(Function &F);
117 bool propagateThroughEdges(Function &F);
118 void computeDominanceAndLoopInfo(Function &F);
120 /// \brief Line number for the function header. Used to compute absolute
121 /// line numbers from the relative line numbers found in the profile.
122 unsigned HeaderLineno;
124 /// \brief Map basic blocks to their computed weights.
126 /// The weight of a basic block is defined to be the maximum
127 /// of all the instruction weights in that block.
128 BlockWeightMap BlockWeights;
130 /// \brief Map edges to their computed weights.
132 /// Edge weights are computed by propagating basic block weights in
133 /// SampleProfile::propagateWeights.
134 EdgeWeightMap EdgeWeights;
136 /// \brief Set of visited blocks during propagation.
137 SmallPtrSet<BasicBlock *, 128> VisitedBlocks;
139 /// \brief Set of visited edges during propagation.
140 SmallSet<Edge, 128> VisitedEdges;
142 /// \brief Equivalence classes for block weights.
144 /// Two blocks BB1 and BB2 are in the same equivalence class if they
145 /// dominate and post-dominate each other, and they are in the same loop
146 /// nest. When this happens, the two blocks are guaranteed to execute
147 /// the same number of times.
148 EquivalenceClassMap EquivalenceClass;
150 /// \brief Dominance, post-dominance and loop information.
151 std::unique_ptr<DominatorTree> DT;
152 std::unique_ptr<DominatorTreeBase<BasicBlock>> PDT;
153 std::unique_ptr<LoopInfo> LI;
155 /// \brief Predecessors for each basic block in the CFG.
156 BlockEdgeMap Predecessors;
158 /// \brief Successors for each basic block in the CFG.
159 BlockEdgeMap Successors;
161 /// \brief Profile reader object.
162 std::unique_ptr<SampleProfileReader> Reader;
164 /// \brief Samples collected for the body of this function.
165 FunctionSamples *Samples;
167 /// \brief Name of the profile file to load.
170 /// \brief Flag indicating whether the profile input loaded successfully.
175 /// \brief Print the weight of edge \p E on stream \p OS.
177 /// \param OS Stream to emit the output to.
178 /// \param E Edge to print.
179 void SampleProfileLoader::printEdgeWeight(raw_ostream &OS, Edge E) {
180 OS << "weight[" << E.first->getName() << "->" << E.second->getName()
181 << "]: " << EdgeWeights[E] << "\n";
184 /// \brief Print the equivalence class of block \p BB on stream \p OS.
186 /// \param OS Stream to emit the output to.
187 /// \param BB Block to print.
188 void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS,
190 BasicBlock *Equiv = EquivalenceClass[BB];
191 OS << "equivalence[" << BB->getName()
192 << "]: " << ((Equiv) ? EquivalenceClass[BB]->getName() : "NONE") << "\n";
195 /// \brief Print the weight of block \p BB on stream \p OS.
197 /// \param OS Stream to emit the output to.
198 /// \param BB Block to print.
199 void SampleProfileLoader::printBlockWeight(raw_ostream &OS, BasicBlock *BB) {
200 OS << "weight[" << BB->getName() << "]: " << BlockWeights[BB] << "\n";
203 /// \brief Get the weight for an instruction.
205 /// The "weight" of an instruction \p Inst is the number of samples
206 /// collected on that instruction at runtime. To retrieve it, we
207 /// need to compute the line number of \p Inst relative to the start of its
208 /// function. We use HeaderLineno to compute the offset. We then
209 /// look up the samples collected for \p Inst using BodySamples.
211 /// \param Inst Instruction to query.
213 /// \returns The profiled weight of I.
214 unsigned SampleProfileLoader::getInstWeight(Instruction &Inst) {
215 DebugLoc DLoc = Inst.getDebugLoc();
219 unsigned Lineno = DLoc.getLine();
220 if (Lineno < HeaderLineno)
223 const DILocation *DIL = DLoc;
224 int LOffset = Lineno - HeaderLineno;
225 unsigned Discriminator = DIL->getDiscriminator();
226 unsigned Weight = Samples->samplesAt(LOffset, Discriminator);
227 DEBUG(dbgs() << " " << Lineno << "." << Discriminator << ":" << Inst
228 << " (line offset: " << LOffset << "." << Discriminator
229 << " - weight: " << Weight << ")\n");
233 /// \brief Compute the weight of a basic block.
235 /// The weight of basic block \p BB is the maximum weight of all the
236 /// instructions in BB. The weight of \p BB is computed and cached in
237 /// the BlockWeights map.
239 /// \param BB The basic block to query.
241 /// \returns The computed weight of BB.
242 unsigned SampleProfileLoader::getBlockWeight(BasicBlock *BB) {
243 // If we've computed BB's weight before, return it.
244 std::pair<BlockWeightMap::iterator, bool> Entry =
245 BlockWeights.insert(std::make_pair(BB, 0));
247 return Entry.first->second;
249 // Otherwise, compute and cache BB's weight.
251 for (auto &I : BB->getInstList()) {
252 unsigned InstWeight = getInstWeight(I);
253 if (InstWeight > Weight)
257 VisitedBlocks.insert(BB);
258 Entry.first->second = Weight;
262 /// \brief Compute and store the weights of every basic block.
264 /// This populates the BlockWeights map by computing
265 /// the weights of every basic block in the CFG.
267 /// \param F The function to query.
268 bool SampleProfileLoader::computeBlockWeights(Function &F) {
269 bool Changed = false;
270 DEBUG(dbgs() << "Block weights\n");
272 unsigned Weight = getBlockWeight(&BB);
273 Changed |= (Weight > 0);
274 DEBUG(printBlockWeight(dbgs(), &BB));
280 /// \brief Find equivalence classes for the given block.
282 /// This finds all the blocks that are guaranteed to execute the same
283 /// number of times as \p BB1. To do this, it traverses all the
284 /// descendants of \p BB1 in the dominator or post-dominator tree.
286 /// A block BB2 will be in the same equivalence class as \p BB1 if
287 /// the following holds:
289 /// 1- \p BB1 is a descendant of BB2 in the opposite tree. So, if BB2
290 /// is a descendant of \p BB1 in the dominator tree, then BB2 should
291 /// dominate BB1 in the post-dominator tree.
293 /// 2- Both BB2 and \p BB1 must be in the same loop.
295 /// For every block BB2 that meets those two requirements, we set BB2's
296 /// equivalence class to \p BB1.
298 /// \param BB1 Block to check.
299 /// \param Descendants Descendants of \p BB1 in either the dom or pdom tree.
300 /// \param DomTree Opposite dominator tree. If \p Descendants is filled
301 /// with blocks from \p BB1's dominator tree, then
302 /// this is the post-dominator tree, and vice versa.
303 void SampleProfileLoader::findEquivalencesFor(
304 BasicBlock *BB1, SmallVector<BasicBlock *, 8> Descendants,
305 DominatorTreeBase<BasicBlock> *DomTree) {
306 for (auto *BB2 : Descendants) {
307 bool IsDomParent = DomTree->dominates(BB2, BB1);
308 bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2);
309 if (BB1 != BB2 && IsDomParent && IsInSameLoop) {
310 EquivalenceClass[BB2] = BB1;
312 // If BB2 is heavier than BB1, make BB2 have the same weight
315 // Note that we don't worry about the opposite situation here
316 // (when BB2 is lighter than BB1). We will deal with this
317 // during the propagation phase. Right now, we just want to
318 // make sure that BB1 has the largest weight of all the
319 // members of its equivalence set.
320 unsigned &BB1Weight = BlockWeights[BB1];
321 unsigned &BB2Weight = BlockWeights[BB2];
322 BB1Weight = std::max(BB1Weight, BB2Weight);
327 /// \brief Find equivalence classes.
329 /// Since samples may be missing from blocks, we can fill in the gaps by setting
330 /// the weights of all the blocks in the same equivalence class to the same
331 /// weight. To compute the concept of equivalence, we use dominance and loop
332 /// information. Two blocks B1 and B2 are in the same equivalence class if B1
333 /// dominates B2, B2 post-dominates B1 and both are in the same loop.
335 /// \param F The function to query.
336 void SampleProfileLoader::findEquivalenceClasses(Function &F) {
337 SmallVector<BasicBlock *, 8> DominatedBBs;
338 DEBUG(dbgs() << "\nBlock equivalence classes\n");
339 // Find equivalence sets based on dominance and post-dominance information.
341 BasicBlock *BB1 = &BB;
343 // Compute BB1's equivalence class once.
344 if (EquivalenceClass.count(BB1)) {
345 DEBUG(printBlockEquivalence(dbgs(), BB1));
349 // By default, blocks are in their own equivalence class.
350 EquivalenceClass[BB1] = BB1;
352 // Traverse all the blocks dominated by BB1. We are looking for
353 // every basic block BB2 such that:
355 // 1- BB1 dominates BB2.
356 // 2- BB2 post-dominates BB1.
357 // 3- BB1 and BB2 are in the same loop nest.
359 // If all those conditions hold, it means that BB2 is executed
360 // as many times as BB1, so they are placed in the same equivalence
361 // class by making BB2's equivalence class be BB1.
362 DominatedBBs.clear();
363 DT->getDescendants(BB1, DominatedBBs);
364 findEquivalencesFor(BB1, DominatedBBs, PDT.get());
366 // Repeat the same logic for all the blocks post-dominated by BB1.
367 // We are looking for every basic block BB2 such that:
369 // 1- BB1 post-dominates BB2.
370 // 2- BB2 dominates BB1.
371 // 3- BB1 and BB2 are in the same loop nest.
373 // If all those conditions hold, BB2's equivalence class is BB1.
374 DominatedBBs.clear();
375 PDT->getDescendants(BB1, DominatedBBs);
376 findEquivalencesFor(BB1, DominatedBBs, DT.get());
378 DEBUG(printBlockEquivalence(dbgs(), BB1));
381 // Assign weights to equivalence classes.
383 // All the basic blocks in the same equivalence class will execute
384 // the same number of times. Since we know that the head block in
385 // each equivalence class has the largest weight, assign that weight
386 // to all the blocks in that equivalence class.
387 DEBUG(dbgs() << "\nAssign the same weight to all blocks in the same class\n");
389 BasicBlock *BB = &BI;
390 BasicBlock *EquivBB = EquivalenceClass[BB];
392 BlockWeights[BB] = BlockWeights[EquivBB];
393 DEBUG(printBlockWeight(dbgs(), BB));
397 /// \brief Visit the given edge to decide if it has a valid weight.
399 /// If \p E has not been visited before, we copy to \p UnknownEdge
400 /// and increment the count of unknown edges.
402 /// \param E Edge to visit.
403 /// \param NumUnknownEdges Current number of unknown edges.
404 /// \param UnknownEdge Set if E has not been visited before.
406 /// \returns E's weight, if known. Otherwise, return 0.
407 unsigned SampleProfileLoader::visitEdge(Edge E, unsigned *NumUnknownEdges,
409 if (!VisitedEdges.count(E)) {
410 (*NumUnknownEdges)++;
415 return EdgeWeights[E];
418 /// \brief Propagate weights through incoming/outgoing edges.
420 /// If the weight of a basic block is known, and there is only one edge
421 /// with an unknown weight, we can calculate the weight of that edge.
423 /// Similarly, if all the edges have a known count, we can calculate the
424 /// count of the basic block, if needed.
426 /// \param F Function to process.
428 /// \returns True if new weights were assigned to edges or blocks.
429 bool SampleProfileLoader::propagateThroughEdges(Function &F) {
430 bool Changed = false;
431 DEBUG(dbgs() << "\nPropagation through edges\n");
433 BasicBlock *BB = &BI;
435 // Visit all the predecessor and successor edges to determine
436 // which ones have a weight assigned already. Note that it doesn't
437 // matter that we only keep track of a single unknown edge. The
438 // only case we are interested in handling is when only a single
439 // edge is unknown (see setEdgeOrBlockWeight).
440 for (unsigned i = 0; i < 2; i++) {
441 unsigned TotalWeight = 0;
442 unsigned NumUnknownEdges = 0;
443 Edge UnknownEdge, SelfReferentialEdge;
446 // First, visit all predecessor edges.
447 for (auto *Pred : Predecessors[BB]) {
448 Edge E = std::make_pair(Pred, BB);
449 TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge);
450 if (E.first == E.second)
451 SelfReferentialEdge = E;
454 // On the second round, visit all successor edges.
455 for (auto *Succ : Successors[BB]) {
456 Edge E = std::make_pair(BB, Succ);
457 TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge);
461 // After visiting all the edges, there are three cases that we
462 // can handle immediately:
464 // - All the edge weights are known (i.e., NumUnknownEdges == 0).
465 // In this case, we simply check that the sum of all the edges
466 // is the same as BB's weight. If not, we change BB's weight
467 // to match. Additionally, if BB had not been visited before,
468 // we mark it visited.
470 // - Only one edge is unknown and BB has already been visited.
471 // In this case, we can compute the weight of the edge by
472 // subtracting the total block weight from all the known
473 // edge weights. If the edges weight more than BB, then the
474 // edge of the last remaining edge is set to zero.
476 // - There exists a self-referential edge and the weight of BB is
477 // known. In this case, this edge can be based on BB's weight.
478 // We add up all the other known edges and set the weight on
479 // the self-referential edge as we did in the previous case.
481 // In any other case, we must continue iterating. Eventually,
482 // all edges will get a weight, or iteration will stop when
483 // it reaches SampleProfileMaxPropagateIterations.
484 if (NumUnknownEdges <= 1) {
485 unsigned &BBWeight = BlockWeights[BB];
486 if (NumUnknownEdges == 0) {
487 // If we already know the weight of all edges, the weight of the
488 // basic block can be computed. It should be no larger than the sum
489 // of all edge weights.
490 if (TotalWeight > BBWeight) {
491 BBWeight = TotalWeight;
493 DEBUG(dbgs() << "All edge weights for " << BB->getName()
494 << " known. Set weight for block: ";
495 printBlockWeight(dbgs(), BB););
497 if (VisitedBlocks.insert(BB).second)
499 } else if (NumUnknownEdges == 1 && VisitedBlocks.count(BB)) {
500 // If there is a single unknown edge and the block has been
501 // visited, then we can compute E's weight.
502 if (BBWeight >= TotalWeight)
503 EdgeWeights[UnknownEdge] = BBWeight - TotalWeight;
505 EdgeWeights[UnknownEdge] = 0;
506 VisitedEdges.insert(UnknownEdge);
508 DEBUG(dbgs() << "Set weight for edge: ";
509 printEdgeWeight(dbgs(), UnknownEdge));
511 } else if (SelfReferentialEdge.first && VisitedBlocks.count(BB)) {
512 unsigned &BBWeight = BlockWeights[BB];
513 // We have a self-referential edge and the weight of BB is known.
514 if (BBWeight >= TotalWeight)
515 EdgeWeights[SelfReferentialEdge] = BBWeight - TotalWeight;
517 EdgeWeights[SelfReferentialEdge] = 0;
518 VisitedEdges.insert(SelfReferentialEdge);
520 DEBUG(dbgs() << "Set self-referential edge weight to: ";
521 printEdgeWeight(dbgs(), SelfReferentialEdge));
529 /// \brief Build in/out edge lists for each basic block in the CFG.
531 /// We are interested in unique edges. If a block B1 has multiple
532 /// edges to another block B2, we only add a single B1->B2 edge.
533 void SampleProfileLoader::buildEdges(Function &F) {
535 BasicBlock *B1 = &BI;
537 // Add predecessors for B1.
538 SmallPtrSet<BasicBlock *, 16> Visited;
539 if (!Predecessors[B1].empty())
540 llvm_unreachable("Found a stale predecessors list in a basic block.");
541 for (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) {
542 BasicBlock *B2 = *PI;
543 if (Visited.insert(B2).second)
544 Predecessors[B1].push_back(B2);
547 // Add successors for B1.
549 if (!Successors[B1].empty())
550 llvm_unreachable("Found a stale successors list in a basic block.");
551 for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) {
552 BasicBlock *B2 = *SI;
553 if (Visited.insert(B2).second)
554 Successors[B1].push_back(B2);
559 /// \brief Propagate weights into edges
561 /// The following rules are applied to every block BB in the CFG:
563 /// - If BB has a single predecessor/successor, then the weight
564 /// of that edge is the weight of the block.
566 /// - If all incoming or outgoing edges are known except one, and the
567 /// weight of the block is already known, the weight of the unknown
568 /// edge will be the weight of the block minus the sum of all the known
569 /// edges. If the sum of all the known edges is larger than BB's weight,
570 /// we set the unknown edge weight to zero.
572 /// - If there is a self-referential edge, and the weight of the block is
573 /// known, the weight for that edge is set to the weight of the block
574 /// minus the weight of the other incoming edges to that block (if
576 void SampleProfileLoader::propagateWeights(Function &F) {
580 // Add an entry count to the function using the samples gathered
581 // at the function entry.
582 F.setEntryCount(Samples->getHeadSamples());
584 // Before propagation starts, build, for each block, a list of
585 // unique predecessors and successors. This is necessary to handle
586 // identical edges in multiway branches. Since we visit all blocks and all
587 // edges of the CFG, it is cleaner to build these lists once at the start
591 // Propagate until we converge or we go past the iteration limit.
592 while (Changed && i++ < SampleProfileMaxPropagateIterations) {
593 Changed = propagateThroughEdges(F);
596 // Generate MD_prof metadata for every branch instruction using the
597 // edge weights computed during propagation.
598 DEBUG(dbgs() << "\nPropagation complete. Setting branch weights\n");
599 MDBuilder MDB(F.getContext());
601 BasicBlock *BB = &BI;
602 TerminatorInst *TI = BB->getTerminator();
603 if (TI->getNumSuccessors() == 1)
605 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
608 DEBUG(dbgs() << "\nGetting weights for branch at line "
609 << TI->getDebugLoc().getLine() << ".\n");
610 SmallVector<unsigned, 4> Weights;
611 bool AllWeightsZero = true;
612 for (unsigned I = 0; I < TI->getNumSuccessors(); ++I) {
613 BasicBlock *Succ = TI->getSuccessor(I);
614 Edge E = std::make_pair(BB, Succ);
615 unsigned Weight = EdgeWeights[E];
616 DEBUG(dbgs() << "\t"; printEdgeWeight(dbgs(), E));
617 Weights.push_back(Weight);
619 AllWeightsZero = false;
622 // Only set weights if there is at least one non-zero weight.
623 // In any other case, let the analyzer set weights.
624 if (!AllWeightsZero) {
625 DEBUG(dbgs() << "SUCCESS. Found non-zero weights.\n");
626 TI->setMetadata(llvm::LLVMContext::MD_prof,
627 MDB.createBranchWeights(Weights));
629 DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n");
634 /// \brief Get the line number for the function header.
636 /// This looks up function \p F in the current compilation unit and
637 /// retrieves the line number where the function is defined. This is
638 /// line 0 for all the samples read from the profile file. Every line
639 /// number is relative to this line.
641 /// \param F Function object to query.
643 /// \returns the line number where \p F is defined. If it returns 0,
644 /// it means that there is no debug information available for \p F.
645 unsigned SampleProfileLoader::getFunctionLoc(Function &F) {
646 if (DISubprogram *S = getDISubprogram(&F))
649 // If could not find the start of \p F, emit a diagnostic to inform the user
650 // about the missed opportunity.
651 F.getContext().diagnose(DiagnosticInfoSampleProfile(
652 "No debug information found in function " + F.getName() +
653 ": Function profile not used",
658 void SampleProfileLoader::computeDominanceAndLoopInfo(Function &F) {
659 DT.reset(new DominatorTree);
662 PDT.reset(new DominatorTreeBase<BasicBlock>(true));
665 LI.reset(new LoopInfo);
669 /// \brief Generate branch weight metadata for all branches in \p F.
671 /// Branch weights are computed out of instruction samples using a
672 /// propagation heuristic. Propagation proceeds in 3 phases:
674 /// 1- Assignment of block weights. All the basic blocks in the function
675 /// are initial assigned the same weight as their most frequently
676 /// executed instruction.
678 /// 2- Creation of equivalence classes. Since samples may be missing from
679 /// blocks, we can fill in the gaps by setting the weights of all the
680 /// blocks in the same equivalence class to the same weight. To compute
681 /// the concept of equivalence, we use dominance and loop information.
682 /// Two blocks B1 and B2 are in the same equivalence class if B1
683 /// dominates B2, B2 post-dominates B1 and both are in the same loop.
685 /// 3- Propagation of block weights into edges. This uses a simple
686 /// propagation heuristic. The following rules are applied to every
687 /// block BB in the CFG:
689 /// - If BB has a single predecessor/successor, then the weight
690 /// of that edge is the weight of the block.
692 /// - If all the edges are known except one, and the weight of the
693 /// block is already known, the weight of the unknown edge will
694 /// be the weight of the block minus the sum of all the known
695 /// edges. If the sum of all the known edges is larger than BB's weight,
696 /// we set the unknown edge weight to zero.
698 /// - If there is a self-referential edge, and the weight of the block is
699 /// known, the weight for that edge is set to the weight of the block
700 /// minus the weight of the other incoming edges to that block (if
703 /// Since this propagation is not guaranteed to finalize for every CFG, we
704 /// only allow it to proceed for a limited number of iterations (controlled
705 /// by -sample-profile-max-propagate-iterations).
707 /// FIXME: Try to replace this propagation heuristic with a scheme
708 /// that is guaranteed to finalize. A work-list approach similar to
709 /// the standard value propagation algorithm used by SSA-CCP might
712 /// Once all the branch weights are computed, we emit the MD_prof
713 /// metadata on BB using the computed values for each of its branches.
715 /// \param F The function to query.
717 /// \returns true if \p F was modified. Returns false, otherwise.
718 bool SampleProfileLoader::emitAnnotations(Function &F) {
719 bool Changed = false;
721 // Initialize invariants used during computation and propagation.
722 HeaderLineno = getFunctionLoc(F);
723 if (HeaderLineno == 0)
726 DEBUG(dbgs() << "Line number for the first instruction in " << F.getName()
727 << ": " << HeaderLineno << "\n");
729 // Compute basic block weights.
730 Changed |= computeBlockWeights(F);
733 // Compute dominance and loop info needed for propagation.
734 computeDominanceAndLoopInfo(F);
736 // Find equivalence classes.
737 findEquivalenceClasses(F);
739 // Propagate weights to all edges.
746 char SampleProfileLoader::ID = 0;
747 INITIALIZE_PASS_BEGIN(SampleProfileLoader, "sample-profile",
748 "Sample Profile loader", false, false)
749 INITIALIZE_PASS_DEPENDENCY(AddDiscriminators)
750 INITIALIZE_PASS_END(SampleProfileLoader, "sample-profile",
751 "Sample Profile loader", false, false)
753 bool SampleProfileLoader::doInitialization(Module &M) {
754 auto &Ctx = M.getContext();
755 auto ReaderOrErr = SampleProfileReader::create(Filename, Ctx);
756 if (std::error_code EC = ReaderOrErr.getError()) {
757 std::string Msg = "Could not open profile: " + EC.message();
758 Ctx.diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg));
761 Reader = std::move(ReaderOrErr.get());
762 ProfileIsValid = (Reader->read() == sampleprof_error::success);
766 ModulePass *llvm::createSampleProfileLoaderPass() {
767 return new SampleProfileLoader(SampleProfileFile);
770 ModulePass *llvm::createSampleProfileLoaderPass(StringRef Name) {
771 return new SampleProfileLoader(Name);
774 bool SampleProfileLoader::runOnModule(Module &M) {
777 if (!F.isDeclaration())
778 retval |= runOnFunction(F);
782 bool SampleProfileLoader::runOnFunction(Function &F) {
786 Samples = Reader->getSamplesFor(F);
787 if (!Samples->empty())
788 return emitAnnotations(F);