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