1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*
* Copyright 2022 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "module-utils.h"
#include "support/insert_ordered.h"
namespace wasm::ModuleUtils {
namespace {
// Helper for collecting HeapTypes and their frequencies.
struct Counts : public InsertOrderedMap<HeapType, size_t> {
void note(HeapType type) {
if (!type.isBasic()) {
(*this)[type]++;
}
}
void note(Type type) {
for (HeapType ht : type.getHeapTypeChildren()) {
note(ht);
}
}
};
Counts getHeapTypeCounts(Module& wasm) {
struct CodeScanner
: PostWalker<CodeScanner, UnifiedExpressionVisitor<CodeScanner>> {
Counts& counts;
CodeScanner(Module& wasm, Counts& counts) : counts(counts) {
setModule(&wasm);
}
void visitExpression(Expression* curr) {
if (auto* call = curr->dynCast<CallIndirect>()) {
counts.note(call->heapType);
} else if (curr->is<RefNull>()) {
counts.note(curr->type);
} else if (curr->is<RttCanon>() || curr->is<RttSub>()) {
counts.note(curr->type.getRtt().heapType);
} else if (auto* make = curr->dynCast<StructNew>()) {
// Some operations emit a HeapType in the binary format, if they are
// static and not dynamic (if dynamic, the RTT provides the heap type).
if (!make->rtt && make->type != Type::unreachable) {
counts.note(make->type.getHeapType());
}
} else if (auto* make = curr->dynCast<ArrayNew>()) {
if (!make->rtt && make->type != Type::unreachable) {
counts.note(make->type.getHeapType());
}
} else if (auto* make = curr->dynCast<ArrayInit>()) {
if (!make->rtt && make->type != Type::unreachable) {
counts.note(make->type.getHeapType());
}
} else if (auto* cast = curr->dynCast<RefCast>()) {
if (!cast->rtt && cast->type != Type::unreachable) {
counts.note(cast->getIntendedType());
}
} else if (auto* cast = curr->dynCast<RefTest>()) {
if (!cast->rtt && cast->type != Type::unreachable) {
counts.note(cast->getIntendedType());
}
} else if (auto* cast = curr->dynCast<BrOn>()) {
if (cast->op == BrOnCast || cast->op == BrOnCastFail) {
if (!cast->rtt && cast->type != Type::unreachable) {
counts.note(cast->getIntendedType());
}
}
} else if (auto* get = curr->dynCast<StructGet>()) {
counts.note(get->ref->type);
} else if (auto* set = curr->dynCast<StructSet>()) {
counts.note(set->ref->type);
} else if (Properties::isControlFlowStructure(curr)) {
if (curr->type.isTuple()) {
// TODO: Allow control flow to have input types as well
counts.note(Signature(Type::none, curr->type));
} else {
counts.note(curr->type);
}
}
}
};
// Collect module-level info.
Counts counts;
CodeScanner(wasm, counts).walkModuleCode(&wasm);
for (auto& curr : wasm.tags) {
counts.note(curr->sig);
}
for (auto& curr : wasm.tables) {
counts.note(curr->type);
}
for (auto& curr : wasm.elementSegments) {
counts.note(curr->type);
}
// Collect info from functions in parallel.
ModuleUtils::ParallelFunctionAnalysis<Counts, InsertOrderedMap> analysis(
wasm, [&](Function* func, Counts& counts) {
counts.note(func->type);
for (auto type : func->vars) {
counts.note(type);
}
if (!func->imported()) {
CodeScanner(wasm, counts).walk(func->body);
}
});
// Combine the function info with the module info.
for (auto& [_, functionCounts] : analysis.map) {
for (auto& [sig, count] : functionCounts) {
counts[sig] += count;
}
}
// Recursively traverse each reference type, which may have a child type that
// is itself a reference type. This reflects an appearance in the binary
// format that is in the type section itself.
// As we do this we may find more and more types, as nested children of
// previous ones. Each such type will appear in the type section once, so
// we just need to visit it once.
InsertOrderedSet<HeapType> newTypes;
for (auto& [type, _] : counts) {
newTypes.insert(type);
}
while (!newTypes.empty()) {
auto iter = newTypes.begin();
auto ht = *iter;
newTypes.erase(iter);
for (HeapType child : ht.getHeapTypeChildren()) {
if (!child.isBasic()) {
if (!counts.count(child)) {
newTypes.insert(child);
}
counts.note(child);
}
}
if (auto super = ht.getSuperType()) {
if (!counts.count(*super)) {
newTypes.insert(*super);
// We should unconditionally count supertypes, but while the type system
// is in flux, skip counting them to keep the type orderings in nominal
// test outputs more similar to the orderings in the equirecursive
// outputs. FIXME
counts.note(*super);
}
}
// Make sure we've noted the complete recursion group of each type as well.
auto recGroup = ht.getRecGroup();
for (auto type : recGroup) {
if (!counts.count(type)) {
newTypes.insert(type);
counts.note(type);
}
}
}
return counts;
}
void coalesceRecGroups(IndexedHeapTypes& indexedTypes) {
if (getTypeSystem() != TypeSystem::Isorecursive) {
// No rec groups to coalesce.
return;
}
// TODO: Perform a topological sort of the recursion groups to create a valid
// ordering rather than this hack that just gets all the types in a group to
// be adjacent.
assert(indexedTypes.indices.empty());
std::unordered_set<HeapType> seen;
std::vector<HeapType> grouped;
grouped.reserve(indexedTypes.types.size());
for (auto type : indexedTypes.types) {
if (seen.insert(type).second) {
for (auto member : type.getRecGroup()) {
grouped.push_back(member);
seen.insert(member);
}
}
}
assert(grouped.size() == indexedTypes.types.size());
indexedTypes.types = grouped;
}
void setIndices(IndexedHeapTypes& indexedTypes) {
for (Index i = 0; i < indexedTypes.types.size(); i++) {
indexedTypes.indices[indexedTypes.types[i]] = i;
}
}
} // anonymous namespace
std::vector<HeapType> collectHeapTypes(Module& wasm) {
Counts counts = getHeapTypeCounts(wasm);
std::vector<HeapType> types;
types.reserve(counts.size());
for (auto& [type, _] : counts) {
types.push_back(type);
}
return types;
}
IndexedHeapTypes getIndexedHeapTypes(Module& wasm) {
Counts counts = getHeapTypeCounts(wasm);
IndexedHeapTypes indexedTypes;
for (auto& [type, _] : counts) {
indexedTypes.types.push_back(type);
}
coalesceRecGroups(indexedTypes);
setIndices(indexedTypes);
return indexedTypes;
}
IndexedHeapTypes getOptimizedIndexedHeapTypes(Module& wasm) {
Counts counts = getHeapTypeCounts(wasm);
// Sort by frequency and then original insertion order.
std::vector<std::pair<HeapType, size_t>> sorted(counts.begin(), counts.end());
std::stable_sort(sorted.begin(), sorted.end(), [&](auto a, auto b) {
return a.second > b.second;
});
// Collect the results.
IndexedHeapTypes indexedTypes;
for (Index i = 0; i < sorted.size(); ++i) {
indexedTypes.types.push_back(sorted[i].first);
}
// TODO: Explicitly construct a linear extension of the partial order of
// recursion groups by adding edges between unrelated groups according to
// their use counts.
coalesceRecGroups(indexedTypes);
setIndices(indexedTypes);
return indexedTypes;
}
} // namespace wasm::ModuleUtils
|