Skip to content

Commit 2ddb7cb

Browse files
authored
Address review feedback for #1014 (#1016)
* address review feedback for #1014
1 parent a5416d2 commit 2ddb7cb

File tree

9 files changed

+80
-35
lines changed

9 files changed

+80
-35
lines changed

check.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ def do_asm2wasm_test():
148148
finally:
149149
if old_pass_debug is not None:
150150
os.environ['BINARYEN_PASS_DEBUG'] = old_pass_debug
151+
else:
152+
if 'BINARYEN_PASS_DEBUG' in os.environ:
153+
del os.environ['BINARYEN_PASS_DEBUG']
151154

152155
# verify in wasm
153156
if options.interpreter:

src/ast/ExpressionManipulator.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
namespace wasm {
2121

22-
Expression* ExpressionManipulator::flexibleCopy(Expression* original, Module& wasm, CustomCopier custom) {
22+
namespace ExpressionManipulator {
23+
24+
Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom) {
2325
struct Copier : public Visitor<Copier, Expression*> {
2426
Module& wasm;
2527
CustomCopier custom;
@@ -135,7 +137,7 @@ Expression* ExpressionManipulator::flexibleCopy(Expression* original, Module& wa
135137

136138

137139
// Splice an item into the middle of a block's list
138-
void ExpressionManipulator::spliceIntoBlock(Block* block, Index index, Expression* add) {
140+
void spliceIntoBlock(Block* block, Index index, Expression* add) {
139141
auto& list = block->list;
140142
if (index == list.size()) {
141143
list.push_back(add); // simple append
@@ -150,4 +152,6 @@ void ExpressionManipulator::spliceIntoBlock(Block* block, Index index, Expressio
150152
block->finalize(block->type);
151153
}
152154

155+
} // namespace ExpressionManipulator
156+
153157
} // namespace wasm

src/ast/branch-utils.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2017 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef wasm_ast_branch_h
18+
#define wasm_ast_branch_h
19+
20+
#include "wasm.h"
21+
22+
namespace wasm {
23+
24+
namespace BranchUtils {
25+
26+
// branches not actually taken (e.g. (br $out (unreachable)))
27+
// are trivially ignored in our type system
28+
29+
inline bool isBranchTaken(Break* br) {
30+
return !(br->value && br->value->type == unreachable) &&
31+
!(br->condition && br->condition->type == unreachable);
32+
}
33+
34+
inline bool isBranchTaken(Switch* sw) {
35+
return !(sw->value && sw->value->type == unreachable) &&
36+
sw->condition->type != unreachable;
37+
}
38+
39+
} // namespace BranchUtils
40+
41+
} // namespace wasm
42+
43+
#endif // wasm_ast_branch_h
44+

src/ast/manipulation.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
namespace wasm {
2323

24-
struct ExpressionManipulator {
24+
namespace ExpressionManipulator {
2525
// Re-use a node's memory. This helps avoid allocation when optimizing.
2626
template<typename InputType, typename OutputType>
27-
static OutputType* convert(InputType *input) {
27+
inline OutputType* convert(InputType *input) {
2828
static_assert(sizeof(OutputType) <= sizeof(InputType),
2929
"Can only convert to a smaller size Expression node");
3030
input->~InputType(); // arena-allocaed, so no destructor, but avoid UB.
@@ -35,13 +35,13 @@ struct ExpressionManipulator {
3535

3636
// Convenience method for nop, which is a common conversion
3737
template<typename InputType>
38-
static Nop* nop(InputType* target) {
38+
inline Nop* nop(InputType* target) {
3939
return convert<InputType, Nop>(target);
4040
}
4141

4242
// Convert a node that allocates
4343
template<typename InputType, typename OutputType>
44-
static OutputType* convert(InputType *input, MixedArena& allocator) {
44+
inline OutputType* convert(InputType *input, MixedArena& allocator) {
4545
assert(sizeof(OutputType) <= sizeof(InputType));
4646
input->~InputType(); // arena-allocaed, so no destructor, but avoid UB.
4747
OutputType* output = (OutputType*)(input);
@@ -50,18 +50,18 @@ struct ExpressionManipulator {
5050
}
5151

5252
using CustomCopier = std::function<Expression*(Expression*)>;
53-
static Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom);
53+
Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom);
5454

55-
static Expression* copy(Expression* original, Module& wasm) {
55+
inline Expression* copy(Expression* original, Module& wasm) {
5656
auto copy = [](Expression* curr) {
5757
return nullptr;
5858
};
5959
return flexibleCopy(original, wasm, copy);
6060
}
6161

6262
// Splice an item into the middle of a block's list
63-
static void spliceIntoBlock(Block* block, Index index, Expression* add);
64-
};
63+
void spliceIntoBlock(Block* block, Index index, Expression* add);
64+
}
6565

6666
} // wasm
6767

src/ast/type-updating.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ struct TypeUpdater : public ExpressionStackWalker<TypeUpdater, UnifiedExpression
132132
// adds (or removes) breaks depending on break/switch contents
133133
void discoverBreaks(Expression* curr, int change) {
134134
if (auto* br = curr->dynCast<Break>()) {
135-
if (!(br->value && br->value->type == unreachable) &&
136-
!(br->condition && br->condition->type == unreachable)) {
135+
if (BranchUtils::isBranchTaken(br)) {
137136
noteBreakChange(br->name, change, br->value);
138137
}
139138
} else if (auto* sw = curr->dynCast<Switch>()) {
140-
if (!(sw->value && sw->value->type == unreachable) &&
141-
sw->condition->type != unreachable) {
139+
if (BranchUtils::isBranchTaken(sw)) {
142140
applySwitchChanges(sw, change);
143141
}
144142
}

src/ast_utils.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "wasm-traversal.h"
2222
#include "wasm-builder.h"
2323
#include "pass.h"
24+
#include "ast/branch-utils.h"
2425

2526
namespace wasm {
2627

@@ -371,24 +372,19 @@ struct ReFinalize : public WalkerPass<PostWalker<ReFinalize>> {
371372
void visitLoop(Loop *curr) { curr->finalize(); }
372373
void visitBreak(Break *curr) {
373374
curr->finalize();
374-
if (curr->value && curr->value->type == unreachable) {
375-
return; // not an actual break
375+
if (BranchUtils::isBranchTaken(curr)) {
376+
breakValues[curr->name] = getValueType(curr->value);
376377
}
377-
if (curr->condition && curr->condition->type == unreachable) {
378-
return; // not an actual break
379-
}
380-
breakValues[curr->name] = getValueType(curr->value);
381378
}
382379
void visitSwitch(Switch *curr) {
383380
curr->finalize();
384-
if (curr->condition->type == unreachable || (curr->value && curr->value->type == unreachable)) {
385-
return; // not an actual break
386-
}
387-
auto valueType = getValueType(curr->value);
388-
for (auto target : curr->targets) {
389-
breakValues[target] = valueType;
381+
if (BranchUtils::isBranchTaken(curr)) {
382+
auto valueType = getValueType(curr->value);
383+
for (auto target : curr->targets) {
384+
breakValues[target] = valueType;
385+
}
386+
breakValues[curr->default_] = valueType;
390387
}
391-
breakValues[curr->default_] = valueType;
392388
}
393389
void visitCall(Call *curr) { curr->finalize(); }
394390
void visitCallImport(CallImport *curr) { curr->finalize(); }

src/passes/MergeBlocks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
224224
if (auto* block = child->dynCast<Block>()) {
225225
if (!block->name.is() && block->list.size() >= 2) {
226226
child = block->list.back();
227-
// we modified child )which is *&), which modifies curr, which might change its type
227+
// we modified child (which is a reference to a pointer), which modifies curr, which might change its type
228228
// (e.g. (drop (block i32 .. (unreachable)))
229229
// the child was a block of i32, and is being replaced with an unreachable, so the
230230
// parent will likely need to be unreachable too
@@ -277,7 +277,7 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
277277
if (EffectAnalyzer(getPassOptions(), curr->ifTrue).hasSideEffects()) return;
278278
outer = optimize(curr, curr->ifFalse, outer);
279279
if (EffectAnalyzer(getPassOptions(), curr->ifFalse).hasSideEffects()) return;
280-
/* . */ optimize(curr, curr->condition, outer);
280+
optimize(curr, curr->condition, outer);
281281
}
282282

283283
void visitDrop(Drop* curr) {

src/passes/Print.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace wasm {
2727

28-
static int forceFull() {
28+
static int isFullForced() {
2929
if (getenv("BINARYEN_PRINT_FULL")) {
3030
return std::stoi(getenv("BINARYEN_PRINT_FULL"));
3131
}
@@ -48,7 +48,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
4848

4949
PrintSExpression(std::ostream& o) : o(o) {
5050
setMinify(false);
51-
if (!full) full = forceFull();
51+
if (!full) full = isFullForced();
5252
}
5353

5454
void visit(Expression* curr) {
@@ -807,7 +807,7 @@ std::ostream& WasmPrinter::printExpression(Expression* expression, std::ostream&
807807
}
808808
PrintSExpression print(o);
809809
print.setMinify(minify);
810-
if (full || forceFull()) {
810+
if (full || isFullForced()) {
811811
print.setFull(true);
812812
o << "[" << printWasmType(expression->type) << "] ";
813813
}

src/wasm-validator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "wasm.h"
4444
#include "wasm-printing.h"
4545
#include "ast_utils.h"
46+
#include "ast/branch-utils.h"
4647

4748
namespace wasm {
4849

@@ -230,8 +231,7 @@ struct WasmValidator : public PostWalker<WasmValidator> {
230231
}
231232
void visitBreak(Break *curr) {
232233
// note breaks (that are actually taken)
233-
if ((!curr->value || curr->value->type != unreachable) &&
234-
(!curr->condition || curr->condition->type != unreachable)) {
234+
if (BranchUtils::isBranchTaken(curr)) {
235235
noteBreak(curr->name, curr->value, curr);
236236
}
237237
if (curr->condition) {
@@ -240,7 +240,7 @@ struct WasmValidator : public PostWalker<WasmValidator> {
240240
}
241241
void visitSwitch(Switch *curr) {
242242
// note breaks (that are actually taken)
243-
if (curr->condition->type != unreachable && (!curr->value || curr->value->type != unreachable)) {
243+
if (BranchUtils::isBranchTaken(curr)) {
244244
for (auto& target : curr->targets) {
245245
noteBreak(target, curr->value, curr);
246246
}

0 commit comments

Comments
 (0)