Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ReorderGlobals ordering #6625

Merged
merged 84 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
b308805
start
kripken May 16, 2024
8c55b6f
fix
kripken May 16, 2024
835622f
fix
kripken May 16, 2024
d09f4a0
show problem
kripken May 16, 2024
1c52de6
work
kripken May 16, 2024
927897c
fix
kripken May 16, 2024
de1d20d
test
kripken May 16, 2024
6cf9fb3
work
kripken May 16, 2024
322bc71
exllore
kripken May 17, 2024
0b2f8a9
fail
kripken May 17, 2024
407bad3
try
kripken May 17, 2024
a88c8c4
work
kripken May 17, 2024
7de4b4d
Merge remote-tracking branch 'origin/main' into globses
kripken May 17, 2024
08e07cb
undo
kripken May 17, 2024
38ac562
heapify
kripken May 17, 2024
ad78406
heapify
kripken May 17, 2024
c4da577
fix
kripken May 17, 2024
f90da11
work
kripken May 17, 2024
8d54784
todo
kripken May 17, 2024
6000256
work
kripken May 17, 2024
0baee6b
fix
kripken May 17, 2024
ddc8f47
undo
kripken May 17, 2024
52ebfae
undo
kripken May 17, 2024
fac33e4
format
kripken May 17, 2024
2fd4ec0
fancy
kripken May 17, 2024
dbf7673
notes
kripken May 17, 2024
c1bd7c6
test
kripken May 17, 2024
d325aba
test
kripken May 17, 2024
c8f6c2d
test
kripken May 17, 2024
84b0bc8
test
kripken May 17, 2024
47c7449
test
kripken May 17, 2024
eb69de7
test
kripken May 17, 2024
de3b7aa
fix
kripken May 17, 2024
22f40a9
note
kripken May 17, 2024
fa2f756
typo
kripken May 17, 2024
0d345a8
Merge remote-tracking branch 'origin/main' into globses
kripken May 17, 2024
207634c
import
kripken May 17, 2024
9629291
import
kripken May 17, 2024
e3b3b09
work
kripken May 20, 2024
c1827c4
bigpicture
kripken May 20, 2024
ab04d1e
builds
kripken May 20, 2024
4b0ba5b
works
kripken May 20, 2024
9f50ace
test.bad"
kripken May 20, 2024
3da13d7
bad
kripken May 20, 2024
fc5bf12
wrong
kripken May 20, 2024
4b74dd5
wrong
kripken May 20, 2024
64462c7
fix
kripken May 21, 2024
6d00b1e
work
kripken May 21, 2024
c23a4f2
fix
kripken May 21, 2024
985294c
test
kripken May 21, 2024
fe754da
test
kripken May 21, 2024
29c5852
comment
kripken May 21, 2024
f6cd1eb
moar
kripken May 21, 2024
f54d8a3
work
kripken May 21, 2024
96c1f1a
test
kripken May 21, 2024
ffd1fe7
builds
kripken May 21, 2024
5b4a499
test
kripken May 21, 2024
592da79
work
kripken May 21, 2024
65f5f8d
test
kripken May 21, 2024
35a7441
fix
kripken May 21, 2024
a81f708
clean
kripken May 22, 2024
9e3d467
work
kripken May 22, 2024
c17d1b3
indices
kripken May 22, 2024
4bf17c6
actually indices
kripken May 22, 2024
8834d84
actually indices
kripken May 22, 2024
e189d66
clean
kripken May 22, 2024
838b3b3
format
kripken May 22, 2024
567e25d
comments
kripken May 22, 2024
57eb5df
fix
kripken May 22, 2024
280cfd7
fix comment
kripken May 22, 2024
dda7b3d
Update src/passes/ReorderGlobals.cpp
kripken May 30, 2024
ff792bc
comment
kripken May 30, 2024
055f296
typo
kripken May 30, 2024
bd77630
use topological sort utility
kripken May 30, 2024
b68359e
Merge remote-tracking branch 'myself/globses' into globses
kripken May 30, 2024
8ffc896
typo
kripken May 30, 2024
507dff8
optimize
kripken May 30, 2024
3353220
Update src/passes/ReorderGlobals.cpp
kripken May 30, 2024
7da3b03
Update src/passes/ReorderGlobals.cpp
kripken May 30, 2024
4ee047b
missing work
kripken May 30, 2024
35405ca
Merge remote-tracking branch 'myself/globses' into globses
kripken May 30, 2024
9cc3be5
format
kripken May 30, 2024
044a7cf
clarify sort
kripken May 31, 2024
d191ce1
Update src/passes/ReorderGlobals.cpp
kripken May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 39 additions & 45 deletions src/passes/ReorderGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "ir/find_all.h"
#include "pass.h"
#include "support/topological_sort.h"
#include "wasm.h"

namespace wasm {
Expand Down Expand Up @@ -78,7 +79,9 @@ struct ReorderGlobals : public Pass {
// each one moves, which is logically a mapping between indices.
using IndexIndexMap = std::vector<Index>;

// We will also track counts of uses for each global.
// We will also track counts of uses for each global. We use floating-point
// values here since while the initial counts are integers, we will be
// considering fractional sums of them later.
using IndexCountMap = std::vector<double>;

// We must take into account dependencies, so that globals appear before
Expand Down Expand Up @@ -148,7 +151,8 @@ struct ReorderGlobals : public Pass {
struct SortAndSize {
IndexIndexMap sort;
double size;
SortAndSize(IndexIndexMap&& sort, double size) : sort(sort), size(size) {}
SortAndSize(IndexIndexMap&& sort, double size)
: sort(std::move(sort)), size(size) {}
};
std::vector<SortAndSize> options;
auto addOption = [&](const IndexCountMap& customCounts) {
Expand Down Expand Up @@ -185,44 +189,39 @@ struct ReorderGlobals : public Pass {
// simple sum may be too naive).
double const EXPONENTIAL_FACTOR = 0.095;
IndexCountMap sumCounts(globals.size()), exponentialCounts(globals.size());
// We add items to |sumCounts, exponentialCounts| after they are computed,
// so anything not there must be pushed to a stack before we can handle it.
std::vector<Index> stack;
// Do a simple recursion to compute children before parents, while tracking
// which have been computed.
std::vector<bool> computed(globals.size());
for (Index i = 0; i < globals.size(); i++) {
stack.push_back(i);
}
while (!stack.empty()) {
auto global = stack.back();
if (computed[global]) {
// We've already computed this.
stack.pop_back();
continue;
}
// Leave ourselves on the stack and push any unresolved deps.
auto pushed = false;
for (auto dep : deps.dependedUpon[global]) {
if (!computed[dep]) {
stack.push_back(dep);
pushed = true;

struct Sort : public TopologicalSort<Index, Sort> {
const Dependencies& deps;

Sort(Index numGlobals, const Dependencies& deps) : deps(deps) {
for (Index i = 0; i < numGlobals; i++) {
push(i);
}
}
if (pushed) {
continue;

void pushPredecessors(Index global) {
auto iter = deps.dependedUpon.find(global);
if (iter == deps.dependedUpon.end()) {
return;
}
for (auto dep : iter->second) {
push(dep);
}
}
// We can handle this now, as all deps are resolved.
stack.pop_back();
// Start with the self-count, then add the deps.
} sort(globals.size(), deps);

for (auto global : sort) {
// We can compute this global's count as in the sorted order all the
// values it cares about are resolved. Start with the self-count, then
// add the deps.
sumCounts[global] = exponentialCounts[global] = counts[global];
for (auto dep : deps.dependedUpon[global]) {
sumCounts[global] += sumCounts[dep];
exponentialCounts[global] +=
EXPONENTIAL_FACTOR * exponentialCounts[dep];
}
computed[global] = true;
}

addOption(sumCounts);
addOption(exponentialCounts);

Expand All @@ -237,14 +236,12 @@ struct ReorderGlobals : public Pass {
}

// Apply the indices we computed.
std::sort(
globals.begin(),
globals.end(),
[&](const std::unique_ptr<Global>& a, const std::unique_ptr<Global>& b) {
return (*best)[originalIndices[a->name]] <
(*best)[originalIndices[b->name]];
});

std::vector<std::unique_ptr<Global>> old;
old.swap(globals);
kripken marked this conversation as resolved.
Show resolved Hide resolved
globals.resize(old.size());
for (Index i = 0; i < old.size(); i++) {
globals[(*best)[i]] = std::move(old[i]);
}
module->updateMaps();
}

Expand Down Expand Up @@ -308,11 +305,8 @@ struct ReorderGlobals : public Pass {
// Sort by the counts.
auto aCount = counts[a];
auto bCount = counts[b];
if (aCount < bCount) {
return true;
}
if (aCount > bCount) {
return false;
if (aCount != bCount) {
return aCount < bCount;
}

// Break ties using the original order, which means just using the
Expand All @@ -333,7 +327,7 @@ struct ReorderGlobals : public Pass {
}
}

// Pop off the heap: Emit the global and it its final, sorted index. Keep
// Pop off the heap: Emit the global and its final, sorted index. Keep
// doing that until we finish processing all the globals.
IndexIndexMap sortedindices(globals.size());
Index numSortedindices = 0;
Expand Down Expand Up @@ -380,7 +374,7 @@ struct ReorderGlobals : public Pass {

if (always) {
// In this mode we gradually increase the cost of later globals, in an
// unrealistic but manner.
// unrealistic but smooth manner.
double total = 0;
for (Index i = 0; i < actualOrder.size(); i++) {
// Multiply the count for this global by a smoothed LEB factor, which
Expand Down
2 changes: 1 addition & 1 deletion test/lit/passes/reorder-globals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@
;; was moved up to between $b and $c. Sum sort works well here because the
;; first comparison is $a and $other, and sum takes into account $b and $c in
;; $a's favor, so it wins. Likewise $b and $c win against $other as well, so
;; the other is $a, $b, $c, $other which is optimal here.
;; the order is $a, $b, $c, $other which is optimal here.
(module
;; CHECK: (global $a i32 (i32.const 0))
(global $a i32 (i32.const 0))
Expand Down
Loading