-
Notifications
You must be signed in to change notification settings - Fork 870
Expand file tree
/
Copy pathDirectize.cpp
More file actions
265 lines (229 loc) · 8.48 KB
/
Copy pathDirectize.cpp
File metadata and controls
265 lines (229 loc) · 8.48 KB
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
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 2019 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.
*/
//
// Turn indirect calls into direct calls. This is possible if we know
// the table cannot change, and if we see a constant argument for the
// indirect call's index.
//
// If called with
//
// --pass-arg=directize-initial-contents-immutable
//
// then the initial tables' contents are assumed to be immutable (see
// TableUtils::TableInfo).
//
#include <unordered_map>
#include "call-utils.h"
#include "ir/drop.h"
#include "ir/eh-utils.h"
#include "ir/find_all.h"
#include "ir/table-utils.h"
#include "ir/utils.h"
#include "pass.h"
#include "wasm-builder.h"
#include "wasm-traversal.h"
#include "wasm.h"
namespace wasm {
namespace {
struct FunctionDirectizer : public WalkerPass<PostWalker<FunctionDirectizer>> {
bool isFunctionParallel() override { return true; }
std::unique_ptr<Pass> create() override {
return std::make_unique<FunctionDirectizer>(tables);
}
FunctionDirectizer(const TableUtils::TableInfoMap& tables) : tables(tables) {}
bool optimized = false;
void visitCallIndirect(CallIndirect* curr) {
auto& table = tables.at(curr->table);
if (!table.canOptimizeByEntry()) {
return;
}
// If the target is constant, we can emit a direct call.
if (curr->target->is<Const>()) {
std::vector<Expression*> operands(curr->operands.begin(),
curr->operands.end());
makeDirectCall(operands, curr->target, table, curr);
optimized = true;
return;
}
// Emit direct calls for things like a select over constants.
if (auto* calls = CallUtils::convertToDirectCalls(
curr,
[&](Expression* target) {
return getTargetInfo(target, table, curr);
},
*getFunction(),
*getModule())) {
replaceCurrent(calls);
optimized = true;
// Note that types may have changed, as the utility here can add locals
// which require fixups if they are non-nullable, for example.
changedTypes = true;
return;
}
}
bool hasTry = false;
void visitTry(Try* curr) { hasTry = true; }
void doWalkFunction(Function* func) {
WalkerPass<PostWalker<FunctionDirectizer>>::doWalkFunction(func);
if (optimized && hasTry) {
EHUtils::handleBlockNestedPops(func, *getModule());
}
if (changedTypes) {
ReFinalize().walkFunctionInModule(func, getModule());
}
}
private:
const TableUtils::TableInfoMap& tables;
bool changedTypes = false;
// Given an expression that we will use as the target of an indirect call,
// analyze it and return one of the results of CallUtils::IndirectCallInfo,
// that is, whether we know a direct call target, or we know it will trap, or
// if we know nothing.
CallUtils::IndirectCallInfo getTargetInfo(Expression* target,
const TableUtils::TableInfo& info,
CallIndirect* original) {
auto* c = target->dynCast<Const>();
if (!c) {
return CallUtils::Unknown{};
}
Address index = c->value.getUnsigned();
// We'll check if we know what is called from the table's initial content,
// but we can only do this if the initial contents are immutable, or if
// there is no writing to the table at all.
if (!info.initialContentsImmutable && info.hasSet) {
return CallUtils::Unknown{};
}
auto* table = getModule()->getTable(original->table);
Name calledName;
auto& flatTable = *info.flatTable;
if (index < flatTable.names.size()) {
calledName = flatTable.names[index];
}
if (!calledName) {
// We did not see a value there, but the table might have a default
// value.
if (table->imported()) {
// An imported table might have a default value, and we can't tell.
return CallUtils::Unknown{};
}
if (table->init) {
if (index < table->initial) {
// This is in bounds, so the default value of the table is called.
if (auto* refFunc = table->init->dynCast<RefFunc>()) {
calledName = refFunc->func;
} else {
// There is an initial value, but it is unknown, like a
// global.get. We can infer nothing, not even a trap.
return CallUtils::Unknown{};
}
} else {
// We are beyond the initial table size, and can't infer anything,
// unless we are in the simple case of no growth, in which case we
// trap.
if (!info.hasGrow) {
return CallUtils::Trap{};
} else {
return CallUtils::Unknown{};
}
}
}
}
// If we found no data, and the table init did not change anything, then
// we trap.
if (!calledName) {
// But we must not read a place where grow can write to - we must either
// have no grow, or have an index below where grow writes to.
if (!info.hasGrow || index < table->initial) {
return CallUtils::Trap{};
}
// Otherwise, give up.
return CallUtils::Unknown{};
}
// We know what is called, but it must have the right type.
auto* func = getModule()->getFunction(calledName);
if (!HeapType::isSubType(func->type.getHeapType(), original->heapType)) {
return CallUtils::Trap{};
}
// We know exactly what is called, and it does not trap.
return CallUtils::Known{calledName};
}
// Create a direct call for a given list of operands, an expression which is
// known to contain a constant indicating the table offset, and the relevant
// table, if we can. If we can see that the call will trap, instead replace
// with an unreachable.
void makeDirectCall(const std::vector<Expression*>& operands,
Expression* c,
const TableUtils::TableInfo& table,
CallIndirect* original) {
auto info = getTargetInfo(c, table, original);
if (std::get_if<CallUtils::Unknown>(&info)) {
// We don't know anything here.
return;
}
// If the index is invalid, or the type is wrong, we can skip the call and
// emit an unreachable here (with dropped children as needed), since in
// Binaryen it is ok to reorder/replace traps when optimizing (but never to
// remove them, at least not by default).
if (std::get_if<CallUtils::Trap>(&info)) {
replaceCurrent(
getDroppedChildrenAndAppend(original,
*getModule(),
getPassOptions(),
Builder(*getModule()).makeUnreachable(),
DropMode::IgnoreParentEffects));
changedTypes = true;
return;
}
// Everything looks good!
auto name = std::get<CallUtils::Known>(info).target;
auto results = getModule()->getFunction(name)->getResults();
replaceCurrent(Builder(*getModule())
.makeCall(name, operands, results, original->isReturn));
// When we call a function of a subtype of the call_indirect's call type, we
// may be refining results.
if (results != original->type) {
changedTypes = true;
}
}
};
struct Directize : public Pass {
void run(Module* module) override {
if (module->tables.empty()) {
return;
}
// TODO: consider a per-table option here
auto initialContentsImmutable =
hasArgument("directize-initial-contents-immutable");
auto tables =
TableUtils::computeTableInfo(*module, initialContentsImmutable);
// Stop if we cannot optimize anything.
auto hasOptimizableTable = false;
for (auto& [_, info] : tables) {
if (info.canOptimizeByEntry()) {
hasOptimizableTable = true;
break;
}
}
if (!hasOptimizableTable) {
return;
}
// We can optimize!
FunctionDirectizer(tables).run(getPassRunner(), module);
}
};
} // anonymous namespace
Pass* createDirectizePass() { return new Directize(); }
} // namespace wasm