Skip to content

Commit eca1a66

Browse files
authored
Code pushing support for br_on_exn (WebAssembly#2660)
Like `br_if`, `br_on_exn` is a conditional branch and across which code can be pushed past when conditions are satisfied. Also adds a few lines of comments and NFC changes in a couple places. Changes in Vacuum are NFC because they were being handled in `default:` in the same way anyway, but I added them to be more explicit and consistent with existing code.
1 parent 4b79514 commit eca1a66

6 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/dataflow/graph.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ struct Graph : public UnifiedExpressionVisitor<Graph, Node*> {
198198
// Visiting.
199199

200200
Node* visitExpression(Expression* curr) {
201+
// TODO Exception handling instruction support
202+
201203
// Control flow and get/set etc. are special. Aside from them, we just need
202204
// to do something very generic.
203205
if (auto* block = curr->dynCast<Block>()) {

src/passes/CodePushing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Pusher {
141141
if (auto* drop = curr->dynCast<Drop>()) {
142142
curr = drop->value;
143143
}
144-
if (curr->is<If>()) {
144+
if (curr->is<If>() || curr->is<BrOnExn>()) {
145145
return true;
146146
}
147147
if (auto* br = curr->dynCast<Break>()) {

src/passes/Vacuum.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> {
8080
return curr; // not always needed, but handled in visitLoop()
8181
case Expression::Id::DropId:
8282
return curr; // not always needed, but handled in visitDrop()
83+
case Expression::Id::TryId:
84+
return curr; // not always needed, but handled in visitTry()
8385

8486
case Expression::Id::BreakId:
8587
case Expression::Id::SwitchId:
88+
case Expression::Id::BrOnExnId:
8689
case Expression::Id::CallId:
8790
case Expression::Id::CallIndirectId:
8891
case Expression::Id::LocalSetId:

src/wasm-traversal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ struct ControlFlowWalker : public PostWalker<SubType, VisitorType> {
11591159
return curr;
11601160
}
11611161
} else {
1162-
// an if, ignorable
1162+
// an if or try, ignorable
11631163
assert(curr->template is<If>() || curr->template is<Try>());
11641164
}
11651165
if (i == 0) {

test/passes/code-pushing_all-features.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,21 @@
9494
)
9595
)
9696
)
97+
(func $push-past-br-on-exn (; 4 ;)
98+
(local $x i32)
99+
(local $y exnref)
100+
(drop
101+
(block $out (result i32)
102+
(drop
103+
(br_on_exn $out $e
104+
(local.get $y)
105+
)
106+
)
107+
(local.set $x
108+
(i32.const 1)
109+
)
110+
(local.get $x)
111+
)
112+
)
113+
)
97114
)

test/passes/code-pushing_all-features.wast

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,18 @@
6060
(drop (local.get $x))
6161
)
6262
)
63+
64+
(func $push-past-br-on-exn
65+
(local $x i32)
66+
(local $y exnref)
67+
(drop
68+
(block $out (result i32)
69+
(local.set $x (i32.const 1))
70+
(drop
71+
(br_on_exn $out $e (local.get $y))
72+
)
73+
(local.get $x)
74+
)
75+
)
76+
)
6377
)

0 commit comments

Comments
 (0)