SyntheticCountsUtils.cpp 3.7 KB

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