forked from WebAssembly/binaryen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeadArgumentElimination.cpp
468 lines (438 loc) · 15.1 KB
/
DeadArgumentElimination.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright 2018 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.
*/
//
// Optimizes call arguments in a whole-program manner, removing ones
// that are not used (dead).
//
// Specifically, this does these things:
//
// * Find functions for whom an argument is always passed the same
// constant. If so, we can just set that local to that constant
// in the function.
// * Find functions that don't use the value passed to an argument.
// If so, we can avoid even sending and receiving it. (Note how if
// the previous point was true for an argument, then the second
// must as well.)
// * Find return values ("return arguments" ;) that are never used.
//
// This pass does not depend on flattening, but it may be more effective,
// as then call arguments never have side effects (which we need to
// watch for here).
//
#include <unordered_map>
#include <unordered_set>
#include "wasm.h"
#include "pass.h"
#include "wasm-builder.h"
#include "cfg/cfg-traversal.h"
#include "ir/effects.h"
#include "ir/module-utils.h"
#include "passes/opt-utils.h"
#include "support/sorted_vector.h"
namespace wasm {
// Information for a function
struct DAEFunctionInfo {
// The unused parameters, if any.
SortedVector unusedParams;
// Maps a function name to the calls going to it.
std::unordered_map<Name, std::vector<Call*>> calls;
// Map of all calls that are dropped, to their drops' locations (so that
// if we can optimize out the drop, we can replace the drop there).
std::unordered_map<Call*, Expression**> droppedCalls;
// Whether the function can be called from places that
// affect what we can do. For now, any call we don't
// see inhibits our optimizations, but TODO: an export
// could be worked around by exporting a thunk that
// adds the parameter.
bool hasUnseenCalls = false;
};
typedef std::unordered_map<Name, DAEFunctionInfo> DAEFunctionInfoMap;
// Information in a basic block
struct DAEBlockInfo {
// A local may be read, written, or not accessed in this block.
// If it is both read and written, we just care about the first
// action (if it is read first, that's all the info we are
// looking for; if it is written first, it can't be read later).
enum LocalUse {
Read,
Written
};
std::unordered_map<Index, LocalUse> localUses;
};
struct DAEScanner : public WalkerPass<CFGWalker<DAEScanner, Visitor<DAEScanner>, DAEBlockInfo>> {
bool isFunctionParallel() override { return true; }
Pass* create() override { return new DAEScanner(infoMap); }
DAEScanner(DAEFunctionInfoMap* infoMap) : infoMap(infoMap) {}
DAEFunctionInfoMap* infoMap;
DAEFunctionInfo* info;
Index numParams;
// cfg traversal work
void visitGetLocal(GetLocal* curr) {
if (currBasicBlock) {
auto& localUses = currBasicBlock->contents.localUses;
auto index = curr->index;
if (localUses.count(index) == 0) {
localUses[index] = DAEBlockInfo::Read;
}
}
}
void visitSetLocal(SetLocal* curr) {
if (currBasicBlock) {
auto& localUses = currBasicBlock->contents.localUses;
auto index = curr->index;
if (localUses.count(index) == 0) {
localUses[index] = DAEBlockInfo::Written;
}
}
}
void visitCall(Call* curr) {
if (!getModule()->getFunction(curr->target)->imported()) {
info->calls[curr->target].push_back(curr);
}
}
void visitDrop(Drop* curr) {
if (auto* call = curr->value->dynCast<Call>()) {
info->droppedCalls[call] = getCurrentPointer();
}
}
// main entry point
void doWalkFunction(Function* func) {
numParams = func->getNumParams();
info = &((*infoMap)[func->name]);
CFGWalker<DAEScanner, Visitor<DAEScanner>, DAEBlockInfo>::doWalkFunction(func);
// If there are relevant params, check if they are used. (If
// we can't optimize the function anyhow, there's no point.)
if (numParams > 0 && !info->hasUnseenCalls) {
findUnusedParams(func);
}
}
void findUnusedParams(Function* func) {
// Flow the incoming parameter values, see if they reach a read.
// Once we've seen a parameter at a block, we need never consider it there
// again.
std::unordered_map<BasicBlock*, SortedVector> seenBlockIndexes;
// Start with all the incoming parameters.
SortedVector initial;
for (Index i = 0; i < numParams; i++) {
initial.push_back(i);
}
// The used params, which we now compute.
std::unordered_set<Index> usedParams;
// An item of work is a block plus the values arriving there.
typedef std::pair<BasicBlock*, SortedVector> Item;
std::vector<Item> work;
work.emplace_back(entry, initial);
while (!work.empty()) {
auto item = std::move(work.back());
work.pop_back();
auto* block = item.first;
auto& indexes = item.second;
// Ignore things we've already seen, or we've already seen to be used.
auto& seenIndexes = seenBlockIndexes[block];
indexes.filter([&](const Index i) {
if (seenIndexes.has(i) || usedParams.count(i)) {
return false;
} else {
seenIndexes.insert(i);
return true;
}
});
if (indexes.empty()) {
continue; // nothing more to flow
}
auto& localUses = block->contents.localUses;
SortedVector remainingIndexes;
for (auto i : indexes) {
auto iter = localUses.find(i);
if (iter != localUses.end()) {
auto use = iter->second;
if (use == DAEBlockInfo::Read) {
usedParams.insert(i);
}
// Whether it was a read or a write, we can stop looking at that local here.
} else {
remainingIndexes.insert(i);
}
}
// If there are remaining indexes, flow them forward.
if (!remainingIndexes.empty()) {
for (auto* next : block->out) {
work.emplace_back(next, remainingIndexes);
}
}
}
// We can now compute the unused params.
for (Index i = 0; i < numParams; i++) {
if (usedParams.count(i) == 0) {
info->unusedParams.insert(i);
}
}
}
};
struct DAE : public Pass {
bool optimize = false;
void run(PassRunner* runner, Module* module) override {
// Iterate to convergence.
while (1) {
if (!iteration(runner, module)) {
break;
}
}
}
bool iteration(PassRunner* runner, Module* module) {
DAEFunctionInfoMap infoMap;
// Ensure they all exist so the parallel threads don't modify the data structure.
ModuleUtils::iterDefinedFunctions(*module, [&](Function* func) {
infoMap[func->name];
});
// Check the influence of the table and exports.
for (auto& curr : module->exports) {
if (curr->kind == ExternalKind::Function) {
infoMap[curr->value].hasUnseenCalls = true;
}
}
for (auto& segment : module->table.segments) {
for (auto name : segment.data) {
infoMap[name].hasUnseenCalls = true;
}
}
// Scan all the functions.
{
PassRunner runner(module);
runner.setIsNested(true);
runner.add<DAEScanner>(&infoMap);
runner.run();
}
// Combine all the info.
std::unordered_map<Name, std::vector<Call*>> allCalls;
for (auto& pair : infoMap) {
auto& info = pair.second;
for (auto& pair : info.calls) {
auto name = pair.first;
auto& calls = pair.second;
auto& allCallsToName = allCalls[name];
allCallsToName.insert(allCallsToName.end(), calls.begin(), calls.end());
}
for (auto& pair : info.droppedCalls) {
allDroppedCalls[pair.first] = pair.second;
}
}
// We now have a mapping of all call sites for each function. Check which
// are always passed the same constant for a particular argument.
for (auto& pair : allCalls) {
auto name = pair.first;
// We can only optimize if we see all the calls and can modify
// them.
if (infoMap[name].hasUnseenCalls) {
continue;
}
auto& calls = pair.second;
auto* func = module->getFunction(name);
auto numParams = func->getNumParams();
for (Index i = 0; i < numParams; i++) {
Literal value;
for (auto* call : calls) {
assert(call->target == name);
assert(call->operands.size() == numParams);
auto* operand = call->operands[i];
if (auto* c = operand->dynCast<Const>()) {
if (value.type == none) {
// This is the first value seen.
value = c->value;
} else if (value != c->value) {
// Not identical, give up
value.type = none;
break;
}
} else {
// Not a constant, give up
value.type = none;
break;
}
}
if (value.type != none) {
// Success! We can just apply the constant in the function, which makes
// the parameter value unused, which lets us remove it later.
Builder builder(*module);
func->body = builder.makeSequence(
builder.makeSetLocal(i, builder.makeConst(value)),
func->body
);
// Mark it as unused, which we know it now is (no point to
// re-scan just for that).
infoMap[name].unusedParams.insert(i);
}
}
}
// Track which functions we changed, and optimize them later if necessary.
std::unordered_set<Function*> changed;
// We now know which parameters are unused, and can potentially remove them.
for (auto& pair : allCalls) {
auto name = pair.first;
auto& calls = pair.second;
auto* func = module->getFunction(name);
auto numParams = func->getNumParams();
if (numParams == 0) continue;
// Iterate downwards, as we may remove more than one.
Index i = numParams - 1;
while (1) {
if (infoMap[name].unusedParams.has(i)) {
// Great, it's not used. Check if none of the calls has a param with side
// effects, as that would prevent us removing them (flattening should
// have been done earlier).
bool canRemove = true;
for (auto* call : calls) {
auto* operand = call->operands[i];
if (EffectAnalyzer(runner->options, operand).hasSideEffects()) {
canRemove = false;
break;
}
}
if (canRemove) {
// Wonderful, nothing stands in our way! Do it.
// TODO: parallelize this?
removeParameter(func, i, calls);
changed.insert(func);
}
}
if (i == 0) break;
i--;
}
}
// We can also tell which calls have all their return values dropped. Note that we can't do this
// if we changed anything so far, as we may have modified allCalls (we can't modify a call site
// twice in one iteration, once to remove a param, once to drop the return value).
if (changed.empty()) {
for (auto& func : module->functions) {
if (func->result == none) {
continue;
}
auto name = func->name;
if (infoMap[name].hasUnseenCalls) {
continue;
}
auto iter = allCalls.find(name);
if (iter == allCalls.end()) {
continue;
}
auto& calls = iter->second;
bool allDropped = true;
for (auto* call : calls) {
if (!allDroppedCalls.count(call)) {
allDropped = false;
break;
}
}
if (!allDropped) {
continue;
}
removeReturnValue(func.get(), calls, module);
// TODO Removing a drop may also open optimization opportunities in the callers.
changed.insert(func.get());
}
}
if (optimize && !changed.empty()) {
OptUtils::optimizeAfterInlining(changed, module, runner);
}
return !changed.empty();
}
private:
std::unordered_map<Call*, Expression**> allDroppedCalls;
void removeParameter(Function* func, Index i, std::vector<Call*>& calls) {
// Clear the type, which is no longer accurate.
func->type = Name();
// It's cumbersome to adjust local names - TODO don't clear them?
Builder::clearLocalNames(func);
// Remove the parameter from the function. We must add a new local
// for uses of the parameter, but cannot make it use the same index
// (in general).
auto type = func->getLocalType(i);
func->params.erase(func->params.begin() + i);
Index newIndex = Builder::addVar(func, type);
// Update local operations.
struct LocalUpdater : public PostWalker<LocalUpdater> {
Index removedIndex;
Index newIndex;
LocalUpdater(Function* func, Index removedIndex, Index newIndex) : removedIndex(removedIndex), newIndex(newIndex) {
walk(func->body);
}
void visitGetLocal(GetLocal* curr) {
updateIndex(curr->index);
}
void visitSetLocal(SetLocal* curr) {
updateIndex(curr->index);
}
void updateIndex(Index& index) {
if (index == removedIndex) {
index = newIndex;
} else if (index > removedIndex) {
index--;
}
}
} localUpdater(func, i, newIndex);
// Remove the arguments from the calls.
for (auto* call : calls) {
call->operands.erase(call->operands.begin() + i);
}
}
void removeReturnValue(Function* func, std::vector<Call*>& calls, Module* module) {
// Clear the type, which is no longer accurate.
func->type = Name();
func->result = none;
Builder builder(*module);
// Remove any return values.
struct ReturnUpdater : public PostWalker<ReturnUpdater> {
Module* module;
ReturnUpdater(Function* func, Module* module) : module(module) {
walk(func->body);
}
void visitReturn(Return* curr) {
auto* value = curr->value;
assert(value);
curr->value = nullptr;
Builder builder(*module);
replaceCurrent(builder.makeSequence(
builder.makeDrop(value),
curr
));
}
} returnUpdater(func, module);
// Remove any value flowing out.
if (isConcreteType(func->body->type)) {
func->body = builder.makeDrop(func->body);
}
// Remove the drops on the calls.
for (auto* call : calls) {
auto iter = allDroppedCalls.find(call);
assert(iter != allDroppedCalls.end());
Expression** location = iter->second;
*location = call;
// Update the call's type.
if (call->type != unreachable) {
call->type = none;
}
}
}
};
Pass *createDAEPass() {
return new DAE();
}
Pass *createDAEOptimizingPass() {
auto* ret = new DAE();
ret->optimize = true;
return ret;
}
} // namespace wasm