Skip to content

Commit f9948a0

Browse files
aheejinradekdoulik
authored andcommitted
[EH] Support Stack IR for try_table (WebAssembly#6231)
1 parent 9dda6f9 commit f9948a0

File tree

5 files changed

+102
-27
lines changed

5 files changed

+102
-27
lines changed

src/passes/Print.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,7 +3550,8 @@ static std::ostream& printStackIR(StackIR* ir, PrintSExpression& printer) {
35503550
[[fallthrough]];
35513551
case StackInst::BlockBegin:
35523552
case StackInst::IfBegin:
3553-
case StackInst::LoopBegin: {
3553+
case StackInst::LoopBegin:
3554+
case StackInst::TryTableBegin: {
35543555
controlFlowDepth++;
35553556
doIndent();
35563557
PrintExpressionContents(printer).visit(inst->origin);
@@ -3562,7 +3563,8 @@ static std::ostream& printStackIR(StackIR* ir, PrintSExpression& printer) {
35623563
[[fallthrough]];
35633564
case StackInst::BlockEnd:
35643565
case StackInst::IfEnd:
3565-
case StackInst::LoopEnd: {
3566+
case StackInst::LoopEnd:
3567+
case StackInst::TryTableEnd: {
35663568
controlFlowDepth--;
35673569
indent--;
35683570
doIndent();

src/passes/StackIR.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ class StackIROptimizer {
292292
case StackInst::Catch:
293293
case StackInst::CatchAll:
294294
case StackInst::Delegate:
295-
case StackInst::TryEnd: {
295+
case StackInst::TryEnd:
296+
case StackInst::TryTableEnd: {
296297
return true;
297298
}
298299
default: { return false; }
@@ -305,7 +306,8 @@ class StackIROptimizer {
305306
case StackInst::BlockBegin:
306307
case StackInst::IfBegin:
307308
case StackInst::LoopBegin:
308-
case StackInst::TryBegin: {
309+
case StackInst::TryBegin:
310+
case StackInst::TryTableBegin: {
309311
return true;
310312
}
311313
default: { return false; }
@@ -319,7 +321,8 @@ class StackIROptimizer {
319321
case StackInst::IfEnd:
320322
case StackInst::LoopEnd:
321323
case StackInst::TryEnd:
322-
case StackInst::Delegate: {
324+
case StackInst::Delegate:
325+
case StackInst::TryTableEnd: {
323326
return true;
324327
}
325328
default: { return false; }

src/wasm-stack.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ class StackInst {
6060
StackInst(MixedArena&) {}
6161

6262
enum Op {
63-
Basic, // an instruction directly corresponding to a non-control-flow
64-
// Binaryen IR node
65-
BlockBegin, // the beginning of a block
66-
BlockEnd, // the ending of a block
67-
IfBegin, // the beginning of a if
68-
IfElse, // the else of a if
69-
IfEnd, // the ending of a if
70-
LoopBegin, // the beginning of a loop
71-
LoopEnd, // the ending of a loop
72-
TryBegin, // the beginning of a try
73-
Catch, // the catch within a try
74-
CatchAll, // the catch_all within a try
75-
Delegate, // the delegate within a try
76-
TryEnd // the ending of a try
63+
Basic, // an instruction directly corresponding to a
64+
// non-control-flow Binaryen IR node
65+
BlockBegin, // the beginning of a block
66+
BlockEnd, // the ending of a block
67+
IfBegin, // the beginning of a if
68+
IfElse, // the else of a if
69+
IfEnd, // the ending of a if
70+
LoopBegin, // the beginning of a loop
71+
LoopEnd, // the ending of a loop
72+
TryBegin, // the beginning of a try
73+
Catch, // the catch within a try
74+
CatchAll, // the catch_all within a try
75+
Delegate, // the delegate within a try
76+
TryEnd, // the ending of a try
77+
TryTableBegin, // the beginning of a try_table
78+
TryTableEnd // the ending of a try_table
7779
} op;
7880

7981
Expression* origin; // the expression this originates from

src/wasm/wasm-stack.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "wasm-stack.h"
1818
#include "ir/find_all.h"
19+
#include "ir/properties.h"
1920
#include "wasm-binary.h"
2021
#include "wasm-debug.h"
2122

@@ -2680,6 +2681,8 @@ void StackIRGenerator::emit(Expression* curr) {
26802681
stackInst = makeStackInst(StackInst::LoopBegin, curr);
26812682
} else if (curr->is<Try>()) {
26822683
stackInst = makeStackInst(StackInst::TryBegin, curr);
2684+
} else if (curr->is<TryTable>()) {
2685+
stackInst = makeStackInst(StackInst::TryTableBegin, curr);
26832686
} else {
26842687
stackInst = makeStackInst(curr);
26852688
}
@@ -2696,6 +2699,8 @@ void StackIRGenerator::emitScopeEnd(Expression* curr) {
26962699
stackInst = makeStackInst(StackInst::LoopEnd, curr);
26972700
} else if (curr->is<Try>()) {
26982701
stackInst = makeStackInst(StackInst::TryEnd, curr);
2702+
} else if (curr->is<TryTable>()) {
2703+
stackInst = makeStackInst(StackInst::TryTableEnd, curr);
26992704
} else {
27002705
WASM_UNREACHABLE("unexpected expr type");
27012706
}
@@ -2708,15 +2713,15 @@ StackInst* StackIRGenerator::makeStackInst(StackInst::Op op,
27082713
ret->op = op;
27092714
ret->origin = origin;
27102715
auto stackType = origin->type;
2711-
if (origin->is<Block>() || origin->is<Loop>() || origin->is<If>() ||
2712-
origin->is<Try>()) {
2716+
if (Properties::isControlFlowStructure(origin)) {
27132717
if (stackType == Type::unreachable) {
2714-
// There are no unreachable blocks, loops, or ifs. we emit extra
2715-
// unreachables to fix that up, so that they are valid as having none
2716-
// type.
2718+
// There are no unreachable blocks, loops, ifs, trys, or try_tables. we
2719+
// emit extra unreachables to fix that up, so that they are valid as
2720+
// having none type.
27172721
stackType = Type::none;
27182722
} else if (op != StackInst::BlockEnd && op != StackInst::IfEnd &&
2719-
op != StackInst::LoopEnd && op != StackInst::TryEnd) {
2723+
op != StackInst::LoopEnd && op != StackInst::TryEnd &&
2724+
op != StackInst::TryTableEnd) {
27202725
// If a concrete type is returned, we mark the end of the construct has
27212726
// having that type (as it is pushed to the value stack at that point),
27222727
// other parts are marked as none).
@@ -2742,7 +2747,8 @@ void StackIRToBinaryWriter::write() {
27422747
case StackInst::Basic:
27432748
case StackInst::BlockBegin:
27442749
case StackInst::IfBegin:
2745-
case StackInst::LoopBegin: {
2750+
case StackInst::LoopBegin:
2751+
case StackInst::TryTableBegin: {
27462752
writer.visit(inst->origin);
27472753
break;
27482754
}
@@ -2751,7 +2757,8 @@ void StackIRToBinaryWriter::write() {
27512757
[[fallthrough]];
27522758
case StackInst::BlockEnd:
27532759
case StackInst::IfEnd:
2754-
case StackInst::LoopEnd: {
2760+
case StackInst::LoopEnd:
2761+
case StackInst::TryTableEnd: {
27552762
writer.emitScopeEnd(inst->origin);
27562763
break;
27572764
}

test/lit/passes/stack-ir-eh.wast

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
;; RUN: wasm-opt %s --generate-stack-ir --optimize-stack-ir \
3+
;; RUN: -all --print-stack-ir | filecheck %s
4+
5+
(module
6+
;; CHECK: (tag $e-i32 (param i32))
7+
(tag $e-i32 (param i32))
8+
9+
;; CHECK: (func $foo (type $0)
10+
;; CHECK-NEXT: )
11+
(func $foo)
12+
13+
;; CHECK: (func $test (type $0)
14+
;; CHECK-NEXT: block $outer
15+
;; CHECK-NEXT: block $l-catch (result i32)
16+
;; CHECK-NEXT: block $l-catch-ref (type $1) (result i32 exnref)
17+
;; CHECK-NEXT: block $l-catch-all
18+
;; CHECK-NEXT: block $l-catch-all-ref (result exnref)
19+
;; CHECK-NEXT: try_table (catch $e-i32 $l-catch) (catch_ref $e-i32 $l-catch-ref) (catch_all $l-catch-all) (catch_all_ref $l-catch-all-ref)
20+
;; CHECK-NEXT: call $foo
21+
;; CHECK-NEXT: end
22+
;; CHECK-NEXT: br $outer
23+
;; CHECK-NEXT: end
24+
;; CHECK-NEXT: throw_ref
25+
;; CHECK-NEXT: end
26+
;; CHECK-NEXT: br $outer
27+
;; CHECK-NEXT: end
28+
;; CHECK-NEXT: tuple.drop 2
29+
;; CHECK-NEXT: br $outer
30+
;; CHECK-NEXT: end
31+
;; CHECK-NEXT: drop
32+
;; CHECK-NEXT: end
33+
;; CHECK-NEXT: )
34+
(func $test
35+
(block $outer
36+
(drop
37+
(block $l-catch (result i32)
38+
(tuple.drop 2
39+
(block $l-catch-ref (result i32 exnref)
40+
(block $l-catch-all
41+
(throw_ref
42+
(block $l-catch-all-ref (result exnref)
43+
(try_table (catch $e-i32 $l-catch)
44+
(catch_ref $e-i32 $l-catch-ref)
45+
(catch_all $l-catch-all)
46+
(catch_all_ref $l-catch-all-ref)
47+
(call $foo)
48+
)
49+
(br $outer)
50+
)
51+
)
52+
)
53+
(br $outer)
54+
)
55+
)
56+
(br $outer)
57+
)
58+
)
59+
)
60+
)
61+
)

0 commit comments

Comments
 (0)