2018-01-09 20:39:35 +01:00
|
|
|
//===--- SyntheticCountsUtils.cpp - synthetic counts propagation utils ---===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-01-09 20:39:35 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines utilities for propagating synthetic counts.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/SyntheticCountsUtils.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2018-12-13 20:54:27 +01:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2018-01-09 20:39:35 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// Given an SCC, propagate entry counts along the edge of the SCC nodes.
|
|
|
|
template <typename CallGraphType>
|
|
|
|
void SyntheticCountsUtils<CallGraphType>::propagateFromSCC(
|
2019-01-09 21:10:27 +01:00
|
|
|
const SccTy &SCC, GetProfCountTy GetProfCount, AddCountTy AddCount) {
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-12-13 20:54:27 +01:00
|
|
|
DenseSet<NodeRef> SCCNodes;
|
2018-01-25 23:02:29 +01:00
|
|
|
SmallVector<std::pair<NodeRef, EdgeRef>, 8> SCCEdges, NonSCCEdges;
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
for (auto &Node : SCC)
|
|
|
|
SCCNodes.insert(Node);
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// Partition the edges coming out of the SCC into those whose destination is
|
|
|
|
// in the SCC and the rest.
|
|
|
|
for (const auto &Node : SCCNodes) {
|
2018-02-01 20:40:35 +01:00
|
|
|
for (auto &E : children_edges<CallGraphType>(Node)) {
|
2018-01-25 23:02:29 +01:00
|
|
|
if (SCCNodes.count(CGT::edge_dest(E)))
|
|
|
|
SCCEdges.emplace_back(Node, E);
|
|
|
|
else
|
|
|
|
NonSCCEdges.emplace_back(Node, E);
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// For nodes in the same SCC, update the counts in two steps:
|
|
|
|
// 1. Compute the additional count for each node by propagating the counts
|
|
|
|
// along all incoming edges to the node that originate from within the same
|
|
|
|
// SCC and summing them up.
|
|
|
|
// 2. Add the additional counts to the nodes in the SCC.
|
2018-01-09 20:39:35 +01:00
|
|
|
// This ensures that the order of
|
2018-01-25 23:02:29 +01:00
|
|
|
// traversal of nodes within the SCC doesn't affect the final result.
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2019-01-09 21:10:27 +01:00
|
|
|
DenseMap<NodeRef, Scaled64> AdditionalCounts;
|
2018-01-25 23:02:29 +01:00
|
|
|
for (auto &E : SCCEdges) {
|
2019-01-09 21:10:27 +01:00
|
|
|
auto OptProfCount = GetProfCount(E.first, E.second);
|
|
|
|
if (!OptProfCount)
|
2018-01-25 23:02:29 +01:00
|
|
|
continue;
|
|
|
|
auto Callee = CGT::edge_dest(E.second);
|
2019-01-09 21:10:27 +01:00
|
|
|
AdditionalCounts[Callee] += OptProfCount.getValue();
|
2018-01-09 20:39:35 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// Update the counts for the nodes in the SCC.
|
2018-01-09 20:39:35 +01:00
|
|
|
for (auto &Entry : AdditionalCounts)
|
2018-01-25 23:02:29 +01:00
|
|
|
AddCount(Entry.first, Entry.second);
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// Now update the counts for nodes outside the SCC.
|
|
|
|
for (auto &E : NonSCCEdges) {
|
2019-01-09 21:10:27 +01:00
|
|
|
auto OptProfCount = GetProfCount(E.first, E.second);
|
|
|
|
if (!OptProfCount)
|
2018-01-25 23:02:29 +01:00
|
|
|
continue;
|
|
|
|
auto Callee = CGT::edge_dest(E.second);
|
2019-01-09 21:10:27 +01:00
|
|
|
AddCount(Callee, OptProfCount.getValue());
|
2018-01-09 20:39:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
/// Propgate synthetic entry counts on a callgraph \p CG.
|
2018-01-09 20:39:35 +01:00
|
|
|
///
|
|
|
|
/// This performs a reverse post-order traversal of the callgraph SCC. For each
|
2018-01-25 23:02:29 +01:00
|
|
|
/// SCC, it first propagates the entry counts to the nodes within the SCC
|
2018-01-09 20:39:35 +01:00
|
|
|
/// through call edges and updates them in one shot. Then the entry counts are
|
2018-02-01 20:40:35 +01:00
|
|
|
/// propagated to nodes outside the SCC. This requires \p GraphTraits
|
2018-01-25 23:02:29 +01:00
|
|
|
/// to have a specialization for \p CallGraphType.
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
template <typename CallGraphType>
|
|
|
|
void SyntheticCountsUtils<CallGraphType>::propagate(const CallGraphType &CG,
|
2019-01-09 21:10:27 +01:00
|
|
|
GetProfCountTy GetProfCount,
|
2018-01-25 23:02:29 +01:00
|
|
|
AddCountTy AddCount) {
|
|
|
|
std::vector<SccTy> SCCs;
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// Collect all the SCCs.
|
|
|
|
for (auto I = scc_begin(CG); !I.isAtEnd(); ++I)
|
|
|
|
SCCs.push_back(*I);
|
2018-01-09 20:39:35 +01:00
|
|
|
|
2018-01-25 23:02:29 +01:00
|
|
|
// The callgraph-scc needs to be visited in top-down order for propagation.
|
|
|
|
// The scc iterator returns the scc in bottom-up order, so reverse the SCCs
|
|
|
|
// and call propagateFromSCC.
|
|
|
|
for (auto &SCC : reverse(SCCs))
|
2019-01-09 21:10:27 +01:00
|
|
|
propagateFromSCC(SCC, GetProfCount, AddCount);
|
2018-01-09 20:39:35 +01:00
|
|
|
}
|
2018-01-25 23:02:29 +01:00
|
|
|
|
|
|
|
template class llvm::SyntheticCountsUtils<const CallGraph *>;
|
2018-12-13 20:54:27 +01:00
|
|
|
template class llvm::SyntheticCountsUtils<ModuleSummaryIndex *>;
|