2018-04-18 07:30:05 +08:00
|
|
|
//===- CallGraphSort.cpp --------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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-04-18 07:30:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// Implementation of Call-Chain Clustering from: Optimizing Function Placement
|
|
|
|
/// for Large-Scale Data-Center Applications
|
|
|
|
/// https://research.fb.com/wp-content/uploads/2017/01/cgo2017-hfsort-final1.pdf
|
|
|
|
///
|
|
|
|
/// The goal of this algorithm is to improve runtime performance of the final
|
|
|
|
/// executable by arranging code sections such that page table and i-cache
|
|
|
|
/// misses are minimized.
|
|
|
|
///
|
|
|
|
/// Definitions:
|
|
|
|
/// * Cluster
|
2019-10-29 09:41:38 +08:00
|
|
|
/// * An ordered list of input sections which are laid out as a unit. At the
|
2018-04-18 07:30:05 +08:00
|
|
|
/// beginning of the algorithm each input section has its own cluster and
|
2019-10-29 09:41:38 +08:00
|
|
|
/// the weight of the cluster is the sum of the weight of all incoming
|
2018-04-18 07:30:05 +08:00
|
|
|
/// edges.
|
|
|
|
/// * Call-Chain Clustering (C³) Heuristic
|
|
|
|
/// * Defines when and how clusters are combined. Pick the highest weighted
|
|
|
|
/// input section then add it to its most likely predecessor if it wouldn't
|
|
|
|
/// penalize it too much.
|
|
|
|
/// * Density
|
|
|
|
/// * The weight of the cluster divided by the size of the cluster. This is a
|
2019-10-29 09:41:38 +08:00
|
|
|
/// proxy for the amount of execution time spent per byte of the cluster.
|
2018-04-18 07:30:05 +08:00
|
|
|
///
|
|
|
|
/// It does so given a call graph profile by the following:
|
|
|
|
/// * Build a weighted call graph from the call graph profile
|
|
|
|
/// * Sort input sections by weight
|
|
|
|
/// * For each input section starting with the highest weight
|
|
|
|
/// * Find its most likely predecessor cluster
|
|
|
|
/// * Check if the combined cluster would be too large, or would have too low
|
|
|
|
/// a density.
|
|
|
|
/// * If not, then combine the clusters.
|
|
|
|
/// * Sort non-empty clusters by density
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CallGraphSort.h"
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
#include <numeric>
|
|
|
|
|
2018-04-18 07:30:05 +08:00
|
|
|
using namespace llvm;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2018-04-18 07:30:05 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct Edge {
|
|
|
|
int from;
|
|
|
|
uint64_t weight;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Cluster {
|
2019-10-04 15:56:54 +08:00
|
|
|
Cluster(int sec, size_t s) : next(sec), prev(sec), size(s) {}
|
2018-04-18 07:30:05 +08:00
|
|
|
|
|
|
|
double getDensity() const {
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
return double(weight) / double(size);
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
int next;
|
|
|
|
int prev;
|
2020-07-22 04:46:11 +08:00
|
|
|
uint64_t size;
|
2018-04-18 07:30:05 +08:00
|
|
|
uint64_t weight = 0;
|
|
|
|
uint64_t initialWeight = 0;
|
2018-08-28 16:49:40 +08:00
|
|
|
Edge bestPred = {-1, 0};
|
2018-04-18 07:30:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CallGraphSort {
|
|
|
|
public:
|
|
|
|
CallGraphSort();
|
|
|
|
|
|
|
|
DenseMap<const InputSectionBase *, int> run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Cluster> clusters;
|
|
|
|
std::vector<const InputSectionBase *> sections;
|
|
|
|
};
|
|
|
|
|
2019-10-29 09:41:38 +08:00
|
|
|
// Maximum amount the combined cluster density can be worse than the original
|
2018-04-18 07:30:05 +08:00
|
|
|
// cluster to consider merging.
|
|
|
|
constexpr int MAX_DENSITY_DEGRADATION = 8;
|
|
|
|
|
|
|
|
// Maximum cluster size in bytes.
|
|
|
|
constexpr uint64_t MAX_CLUSTER_SIZE = 1024 * 1024;
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-04-01 08:11:24 +08:00
|
|
|
using SectionPair =
|
|
|
|
std::pair<const InputSectionBase *, const InputSectionBase *>;
|
2018-10-13 06:44:06 +08:00
|
|
|
|
2018-04-18 07:30:05 +08:00
|
|
|
// Take the edge list in Config->CallGraphProfile, resolve symbol names to
|
|
|
|
// Symbols, and generate a graph between InputSections with the provided
|
|
|
|
// weights.
|
|
|
|
CallGraphSort::CallGraphSort() {
|
2018-10-13 06:44:06 +08:00
|
|
|
MapVector<SectionPair, uint64_t> &profile = config->callGraphProfile;
|
2018-04-18 07:30:05 +08:00
|
|
|
DenseMap<const InputSectionBase *, int> secToCluster;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2018-04-18 07:30:05 +08:00
|
|
|
auto getOrCreateNode = [&](const InputSectionBase *isec) -> int {
|
2019-10-04 15:56:54 +08:00
|
|
|
auto res = secToCluster.try_emplace(isec, clusters.size());
|
2018-04-18 07:30:05 +08:00
|
|
|
if (res.second) {
|
|
|
|
sections.push_back(isec);
|
|
|
|
clusters.emplace_back(clusters.size(), isec->getSize());
|
|
|
|
}
|
|
|
|
return res.first->second;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the graph.
|
2018-10-13 06:44:06 +08:00
|
|
|
for (std::pair<SectionPair, uint64_t> &c : profile) {
|
2021-12-25 04:09:48 +08:00
|
|
|
const auto *fromSB = cast<InputSectionBase>(c.first.first);
|
|
|
|
const auto *toSB = cast<InputSectionBase>(c.first.second);
|
2018-04-18 07:30:05 +08:00
|
|
|
uint64_t weight = c.second;
|
|
|
|
|
|
|
|
// Ignore edges between input sections belonging to different output
|
|
|
|
// sections. This is done because otherwise we would end up with clusters
|
|
|
|
// containing input sections that can't actually be placed adjacently in the
|
|
|
|
// output. This messes with the cluster size and density calculations. We
|
|
|
|
// would also end up moving input sections in other output sections without
|
|
|
|
// moving them closer to what calls them.
|
|
|
|
if (fromSB->getOutputSection() != toSB->getOutputSection())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int from = getOrCreateNode(fromSB);
|
|
|
|
int to = getOrCreateNode(toSB);
|
|
|
|
|
|
|
|
clusters[to].weight += weight;
|
|
|
|
|
|
|
|
if (from == to)
|
|
|
|
continue;
|
|
|
|
|
2018-08-28 16:49:40 +08:00
|
|
|
// Remember the best edge.
|
|
|
|
Cluster &toC = clusters[to];
|
|
|
|
if (toC.bestPred.from == -1 || toC.bestPred.weight < weight) {
|
|
|
|
toC.bestPred.from = from;
|
|
|
|
toC.bestPred.weight = weight;
|
|
|
|
}
|
2018-04-18 07:30:05 +08:00
|
|
|
}
|
|
|
|
for (Cluster &c : clusters)
|
|
|
|
c.initialWeight = c.weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's bad to merge clusters which would degrade the density too much.
|
|
|
|
static bool isNewDensityBad(Cluster &a, Cluster &b) {
|
|
|
|
double newDensity = double(a.weight + b.weight) / double(a.size + b.size);
|
2018-10-13 06:44:06 +08:00
|
|
|
return newDensity < a.getDensity() / MAX_DENSITY_DEGRADATION;
|
2018-04-18 07:30:05 +08:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
// Find the leader of V's belonged cluster (represented as an equivalence
|
|
|
|
// class). We apply union-find path-halving technique (simple to implement) in
|
|
|
|
// the meantime as it decreases depths and the time complexity.
|
|
|
|
static int getLeader(std::vector<int> &leaders, int v) {
|
|
|
|
while (leaders[v] != v) {
|
|
|
|
leaders[v] = leaders[leaders[v]];
|
|
|
|
v = leaders[v];
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mergeClusters(std::vector<Cluster> &cs, Cluster &into, int intoIdx,
|
|
|
|
Cluster &from, int fromIdx) {
|
|
|
|
int tail1 = into.prev, tail2 = from.prev;
|
|
|
|
into.prev = tail2;
|
|
|
|
cs[tail2].next = intoIdx;
|
|
|
|
from.prev = tail1;
|
|
|
|
cs[tail1].next = fromIdx;
|
2018-04-18 07:30:05 +08:00
|
|
|
into.size += from.size;
|
|
|
|
into.weight += from.weight;
|
|
|
|
from.size = 0;
|
|
|
|
from.weight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group InputSections into clusters using the Call-Chain Clustering heuristic
|
|
|
|
// then sort the clusters by density.
|
2019-10-04 15:56:54 +08:00
|
|
|
DenseMap<const InputSectionBase *, int> CallGraphSort::run() {
|
|
|
|
std::vector<int> sorted(clusters.size());
|
|
|
|
std::vector<int> leaders(clusters.size());
|
2018-04-18 07:30:05 +08:00
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
std::iota(leaders.begin(), leaders.end(), 0);
|
|
|
|
std::iota(sorted.begin(), sorted.end(), 0);
|
|
|
|
llvm::stable_sort(sorted, [&](int a, int b) {
|
2019-04-23 10:42:06 +08:00
|
|
|
return clusters[a].getDensity() > clusters[b].getDensity();
|
2018-04-18 07:30:05 +08:00
|
|
|
});
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
for (int l : sorted) {
|
|
|
|
// The cluster index is the same as the index of its leader here because
|
|
|
|
// clusters[L] has not been merged into another cluster yet.
|
|
|
|
Cluster &c = clusters[l];
|
2018-04-18 07:30:05 +08:00
|
|
|
|
2018-08-28 16:49:40 +08:00
|
|
|
// Don't consider merging if the edge is unlikely.
|
|
|
|
if (c.bestPred.from == -1 || c.bestPred.weight * 10 <= c.initialWeight)
|
2018-04-18 07:30:05 +08:00
|
|
|
continue;
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
int predL = getLeader(leaders, c.bestPred.from);
|
|
|
|
if (l == predL)
|
2018-04-18 07:30:05 +08:00
|
|
|
continue;
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
Cluster *predC = &clusters[predL];
|
2018-04-18 07:30:05 +08:00
|
|
|
if (c.size + predC->size > MAX_CLUSTER_SIZE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isNewDensityBad(*predC, c))
|
|
|
|
continue;
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
leaders[l] = predL;
|
|
|
|
mergeClusters(clusters, *predC, predL, c, l);
|
2018-04-18 07:30:05 +08:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:56:54 +08:00
|
|
|
// Sort remaining non-empty clusters by density.
|
|
|
|
sorted.clear();
|
|
|
|
for (int i = 0, e = (int)clusters.size(); i != e; ++i)
|
|
|
|
if (clusters[i].size > 0)
|
|
|
|
sorted.push_back(i);
|
|
|
|
llvm::stable_sort(sorted, [&](int a, int b) {
|
|
|
|
return clusters[a].getDensity() > clusters[b].getDensity();
|
2019-04-23 10:42:06 +08:00
|
|
|
});
|
2018-04-18 07:30:05 +08:00
|
|
|
|
2018-10-13 06:44:06 +08:00
|
|
|
DenseMap<const InputSectionBase *, int> orderMap;
|
2019-10-04 15:56:54 +08:00
|
|
|
int curOrder = 1;
|
2020-07-22 04:46:11 +08:00
|
|
|
for (int leader : sorted) {
|
2019-10-04 15:56:54 +08:00
|
|
|
for (int i = leader;;) {
|
|
|
|
orderMap[sections[i]] = curOrder++;
|
|
|
|
i = clusters[i].next;
|
|
|
|
if (i == leader)
|
|
|
|
break;
|
|
|
|
}
|
2020-07-22 04:46:11 +08:00
|
|
|
}
|
2019-03-28 07:52:22 +08:00
|
|
|
if (!config->printSymbolOrder.empty()) {
|
|
|
|
std::error_code ec;
|
2019-08-05 13:43:48 +08:00
|
|
|
raw_fd_ostream os(config->printSymbolOrder, ec, sys::fs::OF_None);
|
2019-03-28 07:52:22 +08:00
|
|
|
if (ec) {
|
|
|
|
error("cannot open " + config->printSymbolOrder + ": " + ec.message());
|
|
|
|
return orderMap;
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Print the symbols ordered by C3, in the order of increasing curOrder
|
|
|
|
// Instead of sorting all the orderMap, just repeat the loops above.
|
2019-10-04 15:56:54 +08:00
|
|
|
for (int leader : sorted)
|
|
|
|
for (int i = leader;;) {
|
2019-03-28 07:52:22 +08:00
|
|
|
// Search all the symbols in the file of the section
|
|
|
|
// and find out a Defined symbol with name that is within the section.
|
2019-10-04 15:56:54 +08:00
|
|
|
for (Symbol *sym : sections[i]->file->getSymbols())
|
2019-03-28 07:52:22 +08:00
|
|
|
if (!sym->isSection()) // Filter out section-type symbols here.
|
|
|
|
if (auto *d = dyn_cast<Defined>(sym))
|
2019-10-04 15:56:54 +08:00
|
|
|
if (sections[i] == d->section)
|
2019-03-28 07:52:22 +08:00
|
|
|
os << sym->getName() << "\n";
|
2019-10-04 15:56:54 +08:00
|
|
|
i = clusters[i].next;
|
|
|
|
if (i == leader)
|
|
|
|
break;
|
|
|
|
}
|
2019-03-28 07:52:22 +08:00
|
|
|
}
|
|
|
|
|
2018-04-18 07:30:05 +08:00
|
|
|
return orderMap;
|
|
|
|
}
|
|
|
|
|
2021-10-26 03:52:06 +08:00
|
|
|
// Sort sections by the profile data provided by --callgraph-profile-file.
|
2018-04-18 07:30:05 +08:00
|
|
|
//
|
|
|
|
// This first builds a call graph based on the profile data then merges sections
|
2020-04-02 00:21:08 +08:00
|
|
|
// according to the C³ heuristic. All clusters are then sorted by a density
|
2018-04-18 07:30:05 +08:00
|
|
|
// metric to further improve locality.
|
2020-05-15 13:18:58 +08:00
|
|
|
DenseMap<const InputSectionBase *, int> elf::computeCallGraphProfileOrder() {
|
2018-04-18 07:30:05 +08:00
|
|
|
return CallGraphSort().run();
|
|
|
|
}
|