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
|
/*
* Copyright 2015 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.
*/
//
// Merges blocks to their parents.
//
// We also restructure blocks in order to enable such merging. For
// example,
//
// (i32.store
// (block
// (call $foo)
// (i32.load (i32.const 100))
// )
// (i32.const 0)
// )
//
// can be transformed into
//
// (block
// (call $foo)
// (i32.store
// (block
// (i32.load (i32.const 100))
// )
// (i32.const 0)
// )
// )
//
// after which the internal block can go away, and
// the new external block might be mergeable. This is always
// worth it if the internal block ends up with 1 item.
// For the second operand,
//
// (i32.store
// (i32.const 100)
// (block
// (call $foo)
// (i32.load (i32.const 200))
// )
// )
//
// The order of operations requires that the first execute
// before. We can do the same operation, but only if the
// first has no side effects, or the code we are moving out
// has no side effects.
// If we can do this to both operands, we can generate a
// single outside block.
//
#include <wasm.h>
#include <pass.h>
#include <wasm-builder.h>
#include <ast_utils.h>
#include <ast/effects.h>
namespace wasm {
// Looks for reasons we can't remove the values from breaks to an origin
// For example, if there is a switch targeting us, we can't do it - we can't remove the value from other targets
struct ProblemFinder : public ControlFlowWalker<ProblemFinder> {
Name origin;
bool foundProblem = false;
// count br_ifs, and dropped br_ifs. if they don't match, then a br_if flow value is used, and we can't drop it
Index brIfs = 0;
Index droppedBrIfs = 0;
PassOptions& passOptions;
ProblemFinder(PassOptions& passOptions) : passOptions(passOptions) {}
void visitBreak(Break* curr) {
if (curr->name == origin) {
if (curr->condition) {
brIfs++;
}
// if the value has side effects, we can't remove it
if (EffectAnalyzer(passOptions, curr->value).hasSideEffects()) {
foundProblem = true;
}
}
}
void visitDrop(Drop* curr) {
if (auto* br = curr->value->dynCast<Break>()) {
if (br->name == origin && br->condition) {
droppedBrIfs++;
}
}
}
void visitSwitch(Switch* curr) {
if (curr->default_ == origin) {
foundProblem = true;
return;
}
for (auto& target : curr->targets) {
if (target == origin) {
foundProblem = true;
return;
}
}
}
bool found() {
assert(brIfs >= droppedBrIfs);
return foundProblem || brIfs > droppedBrIfs;
}
};
// Drops values from breaks to an origin.
// While doing so it can create new blocks, so optimize blocks as well.
struct BreakValueDropper : public ControlFlowWalker<BreakValueDropper> {
Name origin;
PassOptions& passOptions;
BreakValueDropper(PassOptions& passOptions) : passOptions(passOptions) {}
void visitBlock(Block* curr);
void visitBreak(Break* curr) {
if (curr->value && curr->name == origin) {
Builder builder(*getModule());
auto* value = curr->value;
if (value->type == unreachable) {
// the break isn't even reached
replaceCurrent(value);
return;
}
curr->value = nullptr;
curr->finalize();
replaceCurrent(builder.makeSequence(builder.makeDrop(value), curr));
}
}
void visitDrop(Drop* curr) {
// if we dropped a br_if whose value we removed, then we are now dropping a (block (drop value) (br_if)) with type none, which does not need a drop
// likewise, unreachable does not need to be dropped, so we just leave drops of concrete values
if (!isConcreteWasmType(curr->value->type)) {
replaceCurrent(curr->value);
}
}
};
// core block optimizer routine
static void optimizeBlock(Block* curr, Module* module, PassOptions& passOptions) {
bool more = true;
bool changed = false;
while (more) {
more = false;
for (size_t i = 0; i < curr->list.size(); i++) {
Block* child = curr->list[i]->dynCast<Block>();
if (!child) {
// if we have a child that is (drop (block ..)) then we can move the drop into the block, and remove br values. this allows more merging,
auto* drop = curr->list[i]->dynCast<Drop>();
if (drop) {
child = drop->value->dynCast<Block>();
if (child) {
if (child->name.is()) {
Expression* expression = child;
// check if it's ok to remove the value from all breaks to us
ProblemFinder finder(passOptions);
finder.origin = child->name;
finder.walk(expression);
if (finder.found()) {
child = nullptr;
} else {
// fix up breaks
BreakValueDropper fixer(passOptions);
fixer.origin = child->name;
fixer.setModule(module);
fixer.walk(expression);
}
}
if (child) {
// we can do it!
// reuse the drop
drop->value = child->list.back();
drop->finalize();
child->list.back() = drop;
child->finalize();
curr->list[i] = child;
more = true;
changed = true;
}
}
}
}
if (!child) continue;
if (child->name.is()) continue; // named blocks can have breaks to them (and certainly do, if we ran RemoveUnusedNames and RemoveUnusedBrs)
if (child->type == unreachable) {
// an unreachable block can have a concrete final element (which is never reached)
if (!child->list.empty()) {
if (isConcreteWasmType(child->list.back()->type)) {
// just remove it
child->list.pop_back();
}
}
}
ExpressionList merged(module->allocator);
for (size_t j = 0; j < i; j++) {
merged.push_back(curr->list[j]);
}
for (auto item : child->list) {
merged.push_back(item);
}
for (size_t j = i + 1; j < curr->list.size(); j++) {
merged.push_back(curr->list[j]);
}
curr->list.swap(merged);
more = true;
changed = true;
break;
}
}
if (changed) curr->finalize(curr->type);
}
void BreakValueDropper::visitBlock(Block* curr) {
optimizeBlock(curr, getModule(), passOptions);
}
struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
bool isFunctionParallel() override { return true; }
Pass* create() override { return new MergeBlocks; }
void visitBlock(Block *curr) {
optimizeBlock(curr, getModule(), getPassOptions());
}
Block* optimize(Expression* curr, Expression*& child, Block* outer = nullptr, Expression** dependency1 = nullptr, Expression** dependency2 = nullptr) {
if (!child) return outer;
if ((dependency1 && *dependency1) || (dependency2 && *dependency2)) {
// there are dependencies, things we must be reordered through. make sure no problems there
EffectAnalyzer childEffects(getPassOptions(), child);
if (dependency1 && *dependency1 && EffectAnalyzer(getPassOptions(), *dependency1).invalidates(childEffects)) return outer;
if (dependency2 && *dependency2 && EffectAnalyzer(getPassOptions(), *dependency2).invalidates(childEffects)) return outer;
}
if (auto* block = child->dynCast<Block>()) {
if (!block->name.is() && block->list.size() >= 2) {
child = block->list.back();
// we modified child (which is a reference to a pointer), which modifies curr, which might change its type
// (e.g. (drop (block (result i32) .. (unreachable)))
// the child was a block of i32, and is being replaced with an unreachable, so the
// parent will likely need to be unreachable too
auto oldType = curr->type;
ReFinalize().walk(curr);
if (outer == nullptr) {
// reuse the block, move it out
block->list.back() = curr;
// we want the block outside to have the same type as curr had
block->finalize(oldType);
replaceCurrent(block);
return block;
} else {
// append to an existing outer block
assert(outer->list.back() == curr);
outer->list.pop_back();
for (Index i = 0; i < block->list.size() - 1; i++) {
outer->list.push_back(block->list[i]);
}
outer->list.push_back(curr);
}
}
}
return outer;
}
void visitUnary(Unary* curr) {
optimize(curr, curr->value);
}
void visitSetLocal(SetLocal* curr) {
optimize(curr, curr->value);
}
void visitLoad(Load* curr) {
optimize(curr, curr->ptr);
}
void visitReturn(Return* curr) {
optimize(curr, curr->value);
}
void visitBinary(Binary* curr) {
optimize(curr, curr->right, optimize(curr, curr->left), &curr->left);
}
void visitStore(Store* curr) {
optimize(curr, curr->value, optimize(curr, curr->ptr), &curr->ptr);
}
void visitAtomicRMW(AtomicRMW* curr) {
optimize(curr, curr->value, optimize(curr, curr->ptr), &curr->ptr);
}
void optimizeTernary(Expression* curr,
Expression*& first, Expression*& second, Expression*& third) {
// TODO: for now, just stop when we see any side effect. instead, we could
// check effects carefully for reordering
Block* outer = nullptr;
if (EffectAnalyzer(getPassOptions(), first).hasSideEffects()) return;
outer = optimize(curr, first, outer);
if (EffectAnalyzer(getPassOptions(), second).hasSideEffects()) return;
outer = optimize(curr, second, outer);
if (EffectAnalyzer(getPassOptions(), third).hasSideEffects()) return;
optimize(curr, third, outer);
}
void visitAtomicCmpxchg(AtomicCmpxchg* curr) {
optimizeTernary(curr, curr->ptr, curr->expected, curr->replacement);
}
void visitSelect(Select* curr) {
optimizeTernary(curr, curr->ifTrue, curr->ifFalse, curr->condition);
}
void visitDrop(Drop* curr) {
optimize(curr, curr->value);
}
void visitBreak(Break* curr) {
optimize(curr, curr->condition, optimize(curr, curr->value), &curr->value);
}
void visitSwitch(Switch* curr) {
optimize(curr, curr->condition, optimize(curr, curr->value), &curr->value);
}
template<typename T>
void handleCall(T* curr) {
Block* outer = nullptr;
for (Index i = 0; i < curr->operands.size(); i++) {
if (EffectAnalyzer(getPassOptions(), curr->operands[i]).hasSideEffects()) return;
outer = optimize(curr, curr->operands[i], outer);
}
return;
}
void visitCall(Call* curr) {
handleCall(curr);
}
void visitCallImport(CallImport* curr) {
handleCall(curr);
}
void visitCallIndirect(CallIndirect* curr) {
Block* outer = nullptr;
for (Index i = 0; i < curr->operands.size(); i++) {
if (EffectAnalyzer(getPassOptions(), curr->operands[i]).hasSideEffects()) return;
outer = optimize(curr, curr->operands[i], outer);
}
if (EffectAnalyzer(getPassOptions(), curr->target).hasSideEffects()) return;
optimize(curr, curr->target, outer);
}
};
Pass *createMergeBlocksPass() {
return new MergeBlocks();
}
} // namespace wasm
|