annotate lld/ELF/CallGraphSort.cpp @ 192:d7606dcf6fce

Added tag llvm10 for changeset 0572611fdcc8
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 14 Dec 2020 18:01:34 +0900
parents 0572611fdcc8
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- CallGraphSort.cpp --------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 ///
anatofuz
parents:
diff changeset
9 /// Implementation of Call-Chain Clustering from: Optimizing Function Placement
anatofuz
parents:
diff changeset
10 /// for Large-Scale Data-Center Applications
anatofuz
parents:
diff changeset
11 /// https://research.fb.com/wp-content/uploads/2017/01/cgo2017-hfsort-final1.pdf
anatofuz
parents:
diff changeset
12 ///
anatofuz
parents:
diff changeset
13 /// The goal of this algorithm is to improve runtime performance of the final
anatofuz
parents:
diff changeset
14 /// executable by arranging code sections such that page table and i-cache
anatofuz
parents:
diff changeset
15 /// misses are minimized.
anatofuz
parents:
diff changeset
16 ///
anatofuz
parents:
diff changeset
17 /// Definitions:
anatofuz
parents:
diff changeset
18 /// * Cluster
anatofuz
parents:
diff changeset
19 /// * An ordered list of input sections which are laid out as a unit. At the
anatofuz
parents:
diff changeset
20 /// beginning of the algorithm each input section has its own cluster and
anatofuz
parents:
diff changeset
21 /// the weight of the cluster is the sum of the weight of all incoming
anatofuz
parents:
diff changeset
22 /// edges.
anatofuz
parents:
diff changeset
23 /// * Call-Chain Clustering (C³) Heuristic
anatofuz
parents:
diff changeset
24 /// * Defines when and how clusters are combined. Pick the highest weighted
anatofuz
parents:
diff changeset
25 /// input section then add it to its most likely predecessor if it wouldn't
anatofuz
parents:
diff changeset
26 /// penalize it too much.
anatofuz
parents:
diff changeset
27 /// * Density
anatofuz
parents:
diff changeset
28 /// * The weight of the cluster divided by the size of the cluster. This is a
anatofuz
parents:
diff changeset
29 /// proxy for the amount of execution time spent per byte of the cluster.
anatofuz
parents:
diff changeset
30 ///
anatofuz
parents:
diff changeset
31 /// It does so given a call graph profile by the following:
anatofuz
parents:
diff changeset
32 /// * Build a weighted call graph from the call graph profile
anatofuz
parents:
diff changeset
33 /// * Sort input sections by weight
anatofuz
parents:
diff changeset
34 /// * For each input section starting with the highest weight
anatofuz
parents:
diff changeset
35 /// * Find its most likely predecessor cluster
anatofuz
parents:
diff changeset
36 /// * Check if the combined cluster would be too large, or would have too low
anatofuz
parents:
diff changeset
37 /// a density.
anatofuz
parents:
diff changeset
38 /// * If not, then combine the clusters.
anatofuz
parents:
diff changeset
39 /// * Sort non-empty clusters by density
anatofuz
parents:
diff changeset
40 ///
anatofuz
parents:
diff changeset
41 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 #include "CallGraphSort.h"
anatofuz
parents:
diff changeset
44 #include "OutputSections.h"
anatofuz
parents:
diff changeset
45 #include "SymbolTable.h"
anatofuz
parents:
diff changeset
46 #include "Symbols.h"
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 #include <numeric>
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 using namespace llvm;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
51 using namespace lld;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
52 using namespace lld::elf;
150
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 namespace {
anatofuz
parents:
diff changeset
55 struct Edge {
anatofuz
parents:
diff changeset
56 int from;
anatofuz
parents:
diff changeset
57 uint64_t weight;
anatofuz
parents:
diff changeset
58 };
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 struct Cluster {
anatofuz
parents:
diff changeset
61 Cluster(int sec, size_t s) : next(sec), prev(sec), size(s) {}
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 double getDensity() const {
anatofuz
parents:
diff changeset
64 if (size == 0)
anatofuz
parents:
diff changeset
65 return 0;
anatofuz
parents:
diff changeset
66 return double(weight) / double(size);
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 int next;
anatofuz
parents:
diff changeset
70 int prev;
anatofuz
parents:
diff changeset
71 size_t size = 0;
anatofuz
parents:
diff changeset
72 uint64_t weight = 0;
anatofuz
parents:
diff changeset
73 uint64_t initialWeight = 0;
anatofuz
parents:
diff changeset
74 Edge bestPred = {-1, 0};
anatofuz
parents:
diff changeset
75 };
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 class CallGraphSort {
anatofuz
parents:
diff changeset
78 public:
anatofuz
parents:
diff changeset
79 CallGraphSort();
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 DenseMap<const InputSectionBase *, int> run();
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 private:
anatofuz
parents:
diff changeset
84 std::vector<Cluster> clusters;
anatofuz
parents:
diff changeset
85 std::vector<const InputSectionBase *> sections;
anatofuz
parents:
diff changeset
86 };
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 // Maximum amount the combined cluster density can be worse than the original
anatofuz
parents:
diff changeset
89 // cluster to consider merging.
anatofuz
parents:
diff changeset
90 constexpr int MAX_DENSITY_DEGRADATION = 8;
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 // Maximum cluster size in bytes.
anatofuz
parents:
diff changeset
93 constexpr uint64_t MAX_CLUSTER_SIZE = 1024 * 1024;
anatofuz
parents:
diff changeset
94 } // end anonymous namespace
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 using SectionPair =
anatofuz
parents:
diff changeset
97 std::pair<const InputSectionBase *, const InputSectionBase *>;
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 // Take the edge list in Config->CallGraphProfile, resolve symbol names to
anatofuz
parents:
diff changeset
100 // Symbols, and generate a graph between InputSections with the provided
anatofuz
parents:
diff changeset
101 // weights.
anatofuz
parents:
diff changeset
102 CallGraphSort::CallGraphSort() {
anatofuz
parents:
diff changeset
103 MapVector<SectionPair, uint64_t> &profile = config->callGraphProfile;
anatofuz
parents:
diff changeset
104 DenseMap<const InputSectionBase *, int> secToCluster;
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 auto getOrCreateNode = [&](const InputSectionBase *isec) -> int {
anatofuz
parents:
diff changeset
107 auto res = secToCluster.try_emplace(isec, clusters.size());
anatofuz
parents:
diff changeset
108 if (res.second) {
anatofuz
parents:
diff changeset
109 sections.push_back(isec);
anatofuz
parents:
diff changeset
110 clusters.emplace_back(clusters.size(), isec->getSize());
anatofuz
parents:
diff changeset
111 }
anatofuz
parents:
diff changeset
112 return res.first->second;
anatofuz
parents:
diff changeset
113 };
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 // Create the graph.
anatofuz
parents:
diff changeset
116 for (std::pair<SectionPair, uint64_t> &c : profile) {
anatofuz
parents:
diff changeset
117 const auto *fromSB = cast<InputSectionBase>(c.first.first->repl);
anatofuz
parents:
diff changeset
118 const auto *toSB = cast<InputSectionBase>(c.first.second->repl);
anatofuz
parents:
diff changeset
119 uint64_t weight = c.second;
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 // Ignore edges between input sections belonging to different output
anatofuz
parents:
diff changeset
122 // sections. This is done because otherwise we would end up with clusters
anatofuz
parents:
diff changeset
123 // containing input sections that can't actually be placed adjacently in the
anatofuz
parents:
diff changeset
124 // output. This messes with the cluster size and density calculations. We
anatofuz
parents:
diff changeset
125 // would also end up moving input sections in other output sections without
anatofuz
parents:
diff changeset
126 // moving them closer to what calls them.
anatofuz
parents:
diff changeset
127 if (fromSB->getOutputSection() != toSB->getOutputSection())
anatofuz
parents:
diff changeset
128 continue;
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 int from = getOrCreateNode(fromSB);
anatofuz
parents:
diff changeset
131 int to = getOrCreateNode(toSB);
anatofuz
parents:
diff changeset
132
anatofuz
parents:
diff changeset
133 clusters[to].weight += weight;
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 if (from == to)
anatofuz
parents:
diff changeset
136 continue;
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 // Remember the best edge.
anatofuz
parents:
diff changeset
139 Cluster &toC = clusters[to];
anatofuz
parents:
diff changeset
140 if (toC.bestPred.from == -1 || toC.bestPred.weight < weight) {
anatofuz
parents:
diff changeset
141 toC.bestPred.from = from;
anatofuz
parents:
diff changeset
142 toC.bestPred.weight = weight;
anatofuz
parents:
diff changeset
143 }
anatofuz
parents:
diff changeset
144 }
anatofuz
parents:
diff changeset
145 for (Cluster &c : clusters)
anatofuz
parents:
diff changeset
146 c.initialWeight = c.weight;
anatofuz
parents:
diff changeset
147 }
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 // It's bad to merge clusters which would degrade the density too much.
anatofuz
parents:
diff changeset
150 static bool isNewDensityBad(Cluster &a, Cluster &b) {
anatofuz
parents:
diff changeset
151 double newDensity = double(a.weight + b.weight) / double(a.size + b.size);
anatofuz
parents:
diff changeset
152 return newDensity < a.getDensity() / MAX_DENSITY_DEGRADATION;
anatofuz
parents:
diff changeset
153 }
anatofuz
parents:
diff changeset
154
anatofuz
parents:
diff changeset
155 // Find the leader of V's belonged cluster (represented as an equivalence
anatofuz
parents:
diff changeset
156 // class). We apply union-find path-halving technique (simple to implement) in
anatofuz
parents:
diff changeset
157 // the meantime as it decreases depths and the time complexity.
anatofuz
parents:
diff changeset
158 static int getLeader(std::vector<int> &leaders, int v) {
anatofuz
parents:
diff changeset
159 while (leaders[v] != v) {
anatofuz
parents:
diff changeset
160 leaders[v] = leaders[leaders[v]];
anatofuz
parents:
diff changeset
161 v = leaders[v];
anatofuz
parents:
diff changeset
162 }
anatofuz
parents:
diff changeset
163 return v;
anatofuz
parents:
diff changeset
164 }
anatofuz
parents:
diff changeset
165
anatofuz
parents:
diff changeset
166 static void mergeClusters(std::vector<Cluster> &cs, Cluster &into, int intoIdx,
anatofuz
parents:
diff changeset
167 Cluster &from, int fromIdx) {
anatofuz
parents:
diff changeset
168 int tail1 = into.prev, tail2 = from.prev;
anatofuz
parents:
diff changeset
169 into.prev = tail2;
anatofuz
parents:
diff changeset
170 cs[tail2].next = intoIdx;
anatofuz
parents:
diff changeset
171 from.prev = tail1;
anatofuz
parents:
diff changeset
172 cs[tail1].next = fromIdx;
anatofuz
parents:
diff changeset
173 into.size += from.size;
anatofuz
parents:
diff changeset
174 into.weight += from.weight;
anatofuz
parents:
diff changeset
175 from.size = 0;
anatofuz
parents:
diff changeset
176 from.weight = 0;
anatofuz
parents:
diff changeset
177 }
anatofuz
parents:
diff changeset
178
anatofuz
parents:
diff changeset
179 // Group InputSections into clusters using the Call-Chain Clustering heuristic
anatofuz
parents:
diff changeset
180 // then sort the clusters by density.
anatofuz
parents:
diff changeset
181 DenseMap<const InputSectionBase *, int> CallGraphSort::run() {
anatofuz
parents:
diff changeset
182 std::vector<int> sorted(clusters.size());
anatofuz
parents:
diff changeset
183 std::vector<int> leaders(clusters.size());
anatofuz
parents:
diff changeset
184
anatofuz
parents:
diff changeset
185 std::iota(leaders.begin(), leaders.end(), 0);
anatofuz
parents:
diff changeset
186 std::iota(sorted.begin(), sorted.end(), 0);
anatofuz
parents:
diff changeset
187 llvm::stable_sort(sorted, [&](int a, int b) {
anatofuz
parents:
diff changeset
188 return clusters[a].getDensity() > clusters[b].getDensity();
anatofuz
parents:
diff changeset
189 });
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 for (int l : sorted) {
anatofuz
parents:
diff changeset
192 // The cluster index is the same as the index of its leader here because
anatofuz
parents:
diff changeset
193 // clusters[L] has not been merged into another cluster yet.
anatofuz
parents:
diff changeset
194 Cluster &c = clusters[l];
anatofuz
parents:
diff changeset
195
anatofuz
parents:
diff changeset
196 // Don't consider merging if the edge is unlikely.
anatofuz
parents:
diff changeset
197 if (c.bestPred.from == -1 || c.bestPred.weight * 10 <= c.initialWeight)
anatofuz
parents:
diff changeset
198 continue;
anatofuz
parents:
diff changeset
199
anatofuz
parents:
diff changeset
200 int predL = getLeader(leaders, c.bestPred.from);
anatofuz
parents:
diff changeset
201 if (l == predL)
anatofuz
parents:
diff changeset
202 continue;
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 Cluster *predC = &clusters[predL];
anatofuz
parents:
diff changeset
205 if (c.size + predC->size > MAX_CLUSTER_SIZE)
anatofuz
parents:
diff changeset
206 continue;
anatofuz
parents:
diff changeset
207
anatofuz
parents:
diff changeset
208 if (isNewDensityBad(*predC, c))
anatofuz
parents:
diff changeset
209 continue;
anatofuz
parents:
diff changeset
210
anatofuz
parents:
diff changeset
211 leaders[l] = predL;
anatofuz
parents:
diff changeset
212 mergeClusters(clusters, *predC, predL, c, l);
anatofuz
parents:
diff changeset
213 }
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 // Sort remaining non-empty clusters by density.
anatofuz
parents:
diff changeset
216 sorted.clear();
anatofuz
parents:
diff changeset
217 for (int i = 0, e = (int)clusters.size(); i != e; ++i)
anatofuz
parents:
diff changeset
218 if (clusters[i].size > 0)
anatofuz
parents:
diff changeset
219 sorted.push_back(i);
anatofuz
parents:
diff changeset
220 llvm::stable_sort(sorted, [&](int a, int b) {
anatofuz
parents:
diff changeset
221 return clusters[a].getDensity() > clusters[b].getDensity();
anatofuz
parents:
diff changeset
222 });
anatofuz
parents:
diff changeset
223
anatofuz
parents:
diff changeset
224 DenseMap<const InputSectionBase *, int> orderMap;
anatofuz
parents:
diff changeset
225 int curOrder = 1;
anatofuz
parents:
diff changeset
226 for (int leader : sorted)
anatofuz
parents:
diff changeset
227 for (int i = leader;;) {
anatofuz
parents:
diff changeset
228 orderMap[sections[i]] = curOrder++;
anatofuz
parents:
diff changeset
229 i = clusters[i].next;
anatofuz
parents:
diff changeset
230 if (i == leader)
anatofuz
parents:
diff changeset
231 break;
anatofuz
parents:
diff changeset
232 }
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 if (!config->printSymbolOrder.empty()) {
anatofuz
parents:
diff changeset
235 std::error_code ec;
anatofuz
parents:
diff changeset
236 raw_fd_ostream os(config->printSymbolOrder, ec, sys::fs::OF_None);
anatofuz
parents:
diff changeset
237 if (ec) {
anatofuz
parents:
diff changeset
238 error("cannot open " + config->printSymbolOrder + ": " + ec.message());
anatofuz
parents:
diff changeset
239 return orderMap;
anatofuz
parents:
diff changeset
240 }
anatofuz
parents:
diff changeset
241
anatofuz
parents:
diff changeset
242 // Print the symbols ordered by C3, in the order of increasing curOrder
anatofuz
parents:
diff changeset
243 // Instead of sorting all the orderMap, just repeat the loops above.
anatofuz
parents:
diff changeset
244 for (int leader : sorted)
anatofuz
parents:
diff changeset
245 for (int i = leader;;) {
anatofuz
parents:
diff changeset
246 // Search all the symbols in the file of the section
anatofuz
parents:
diff changeset
247 // and find out a Defined symbol with name that is within the section.
anatofuz
parents:
diff changeset
248 for (Symbol *sym : sections[i]->file->getSymbols())
anatofuz
parents:
diff changeset
249 if (!sym->isSection()) // Filter out section-type symbols here.
anatofuz
parents:
diff changeset
250 if (auto *d = dyn_cast<Defined>(sym))
anatofuz
parents:
diff changeset
251 if (sections[i] == d->section)
anatofuz
parents:
diff changeset
252 os << sym->getName() << "\n";
anatofuz
parents:
diff changeset
253 i = clusters[i].next;
anatofuz
parents:
diff changeset
254 if (i == leader)
anatofuz
parents:
diff changeset
255 break;
anatofuz
parents:
diff changeset
256 }
anatofuz
parents:
diff changeset
257 }
anatofuz
parents:
diff changeset
258
anatofuz
parents:
diff changeset
259 return orderMap;
anatofuz
parents:
diff changeset
260 }
anatofuz
parents:
diff changeset
261
anatofuz
parents:
diff changeset
262 // Sort sections by the profile data provided by -callgraph-profile-file
anatofuz
parents:
diff changeset
263 //
anatofuz
parents:
diff changeset
264 // This first builds a call graph based on the profile data then merges sections
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
265 // according to the C³ heuristic. All clusters are then sorted by a density
150
anatofuz
parents:
diff changeset
266 // metric to further improve locality.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
267 DenseMap<const InputSectionBase *, int> elf::computeCallGraphProfileOrder() {
150
anatofuz
parents:
diff changeset
268 return CallGraphSort().run();
anatofuz
parents:
diff changeset
269 }