Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 1 | //===--- SyntheticCountsUtils.cpp - synthetic counts propagation utils ---===// |
| 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 defines utilities for propagating synthetic counts. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/SyntheticCountsUtils.h" |
| 15 | #include "llvm/ADT/DenseSet.h" |
| 16 | #include "llvm/ADT/SCCIterator.h" |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CallGraph.h" |
| 18 | #include "llvm/IR/CallSite.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/InstIterator.h" |
| 21 | #include "llvm/IR/Instructions.h" |
Easwaran Raman | f1f1adc | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ModuleSummaryIndex.h" |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 26 | // Given an SCC, propagate entry counts along the edge of the SCC nodes. |
| 27 | template <typename CallGraphType> |
| 28 | void SyntheticCountsUtils<CallGraphType>::propagateFromSCC( |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 29 | const SccTy &SCC, GetProfCountTy GetProfCount, AddCountTy AddCount) { |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 30 | |
Easwaran Raman | f1f1adc | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 31 | DenseSet<NodeRef> SCCNodes; |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 32 | SmallVector<std::pair<NodeRef, EdgeRef>, 8> SCCEdges, NonSCCEdges; |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 33 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 34 | for (auto &Node : SCC) |
| 35 | SCCNodes.insert(Node); |
| 36 | |
| 37 | // Partition the edges coming out of the SCC into those whose destination is |
| 38 | // in the SCC and the rest. |
| 39 | for (const auto &Node : SCCNodes) { |
Easwaran Raman | 51c9341 | 2018-02-01 19:40:35 +0000 | [diff] [blame] | 40 | for (auto &E : children_edges<CallGraphType>(Node)) { |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 41 | if (SCCNodes.count(CGT::edge_dest(E))) |
| 42 | SCCEdges.emplace_back(Node, E); |
| 43 | else |
| 44 | NonSCCEdges.emplace_back(Node, E); |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 45 | } |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 46 | } |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 47 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 48 | // For nodes in the same SCC, update the counts in two steps: |
| 49 | // 1. Compute the additional count for each node by propagating the counts |
| 50 | // along all incoming edges to the node that originate from within the same |
| 51 | // SCC and summing them up. |
| 52 | // 2. Add the additional counts to the nodes in the SCC. |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 53 | // This ensures that the order of |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 54 | // traversal of nodes within the SCC doesn't affect the final result. |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 55 | |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 56 | DenseMap<NodeRef, Scaled64> AdditionalCounts; |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 57 | for (auto &E : SCCEdges) { |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 58 | auto OptProfCount = GetProfCount(E.first, E.second); |
| 59 | if (!OptProfCount) |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 60 | continue; |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 61 | auto Callee = CGT::edge_dest(E.second); |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 62 | AdditionalCounts[Callee] += OptProfCount.getValue(); |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 65 | // Update the counts for the nodes in the SCC. |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 66 | for (auto &Entry : AdditionalCounts) |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 67 | AddCount(Entry.first, Entry.second); |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 68 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 69 | // Now update the counts for nodes outside the SCC. |
| 70 | for (auto &E : NonSCCEdges) { |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 71 | auto OptProfCount = GetProfCount(E.first, E.second); |
| 72 | if (!OptProfCount) |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 73 | continue; |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 74 | auto Callee = CGT::edge_dest(E.second); |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 75 | AddCount(Callee, OptProfCount.getValue()); |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 79 | /// Propgate synthetic entry counts on a callgraph \p CG. |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 80 | /// |
| 81 | /// This performs a reverse post-order traversal of the callgraph SCC. For each |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 82 | /// SCC, it first propagates the entry counts to the nodes within the SCC |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 83 | /// through call edges and updates them in one shot. Then the entry counts are |
Easwaran Raman | 51c9341 | 2018-02-01 19:40:35 +0000 | [diff] [blame] | 84 | /// propagated to nodes outside the SCC. This requires \p GraphTraits |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 85 | /// to have a specialization for \p CallGraphType. |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 86 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 87 | template <typename CallGraphType> |
| 88 | void SyntheticCountsUtils<CallGraphType>::propagate(const CallGraphType &CG, |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 89 | GetProfCountTy GetProfCount, |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 90 | AddCountTy AddCount) { |
| 91 | std::vector<SccTy> SCCs; |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 92 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 93 | // Collect all the SCCs. |
| 94 | for (auto I = scc_begin(CG); !I.isAtEnd(); ++I) |
| 95 | SCCs.push_back(*I); |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 96 | |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 97 | // The callgraph-scc needs to be visited in top-down order for propagation. |
| 98 | // The scc iterator returns the scc in bottom-up order, so reverse the SCCs |
| 99 | // and call propagateFromSCC. |
| 100 | for (auto &SCC : reverse(SCCs)) |
Easwaran Raman | 116e080 | 2019-01-09 20:10:27 +0000 | [diff] [blame] | 101 | propagateFromSCC(SCC, GetProfCount, AddCount); |
Easwaran Raman | 283d978 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 102 | } |
Easwaran Raman | efb8717 | 2018-01-25 22:02:29 +0000 | [diff] [blame] | 103 | |
| 104 | template class llvm::SyntheticCountsUtils<const CallGraph *>; |
Easwaran Raman | f1f1adc | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 105 | template class llvm::SyntheticCountsUtils<ModuleSummaryIndex *>; |