Skip to content

Commit

Permalink
[EH] Support Stack IR for try_table (WebAssembly#6231)
Browse files Browse the repository at this point in the history
  • Loading branch information
aheejin authored and radekdoulik committed Jul 12, 2024
1 parent 9dda6f9 commit f9948a0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3550,7 +3550,8 @@ static std::ostream& printStackIR(StackIR* ir, PrintSExpression& printer) {
[[fallthrough]];
case StackInst::BlockBegin:
case StackInst::IfBegin:
case StackInst::LoopBegin: {
case StackInst::LoopBegin:
case StackInst::TryTableBegin: {
controlFlowDepth++;
doIndent();
PrintExpressionContents(printer).visit(inst->origin);
Expand All @@ -3562,7 +3563,8 @@ static std::ostream& printStackIR(StackIR* ir, PrintSExpression& printer) {
[[fallthrough]];
case StackInst::BlockEnd:
case StackInst::IfEnd:
case StackInst::LoopEnd: {
case StackInst::LoopEnd:
case StackInst::TryTableEnd: {
controlFlowDepth--;
indent--;
doIndent();
Expand Down
9 changes: 6 additions & 3 deletions src/passes/StackIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ class StackIROptimizer {
case StackInst::Catch:
case StackInst::CatchAll:
case StackInst::Delegate:
case StackInst::TryEnd: {
case StackInst::TryEnd:
case StackInst::TryTableEnd: {
return true;
}
default: { return false; }
Expand All @@ -305,7 +306,8 @@ class StackIROptimizer {
case StackInst::BlockBegin:
case StackInst::IfBegin:
case StackInst::LoopBegin:
case StackInst::TryBegin: {
case StackInst::TryBegin:
case StackInst::TryTableBegin: {
return true;
}
default: { return false; }
Expand All @@ -319,7 +321,8 @@ class StackIROptimizer {
case StackInst::IfEnd:
case StackInst::LoopEnd:
case StackInst::TryEnd:
case StackInst::Delegate: {
case StackInst::Delegate:
case StackInst::TryTableEnd: {
return true;
}
default: { return false; }
Expand Down
30 changes: 16 additions & 14 deletions src/wasm-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ class StackInst {
StackInst(MixedArena&) {}

enum Op {
Basic, // an instruction directly corresponding to a non-control-flow
// Binaryen IR node
BlockBegin, // the beginning of a block
BlockEnd, // the ending of a block
IfBegin, // the beginning of a if
IfElse, // the else of a if
IfEnd, // the ending of a if
LoopBegin, // the beginning of a loop
LoopEnd, // the ending of a loop
TryBegin, // the beginning of a try
Catch, // the catch within a try
CatchAll, // the catch_all within a try
Delegate, // the delegate within a try
TryEnd // the ending of a try
Basic, // an instruction directly corresponding to a
// non-control-flow Binaryen IR node
BlockBegin, // the beginning of a block
BlockEnd, // the ending of a block
IfBegin, // the beginning of a if
IfElse, // the else of a if
IfEnd, // the ending of a if
LoopBegin, // the beginning of a loop
LoopEnd, // the ending of a loop
TryBegin, // the beginning of a try
Catch, // the catch within a try
CatchAll, // the catch_all within a try
Delegate, // the delegate within a try
TryEnd, // the ending of a try
TryTableBegin, // the beginning of a try_table
TryTableEnd // the ending of a try_table
} op;

Expression* origin; // the expression this originates from
Expand Down
23 changes: 15 additions & 8 deletions src/wasm/wasm-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "wasm-stack.h"
#include "ir/find_all.h"
#include "ir/properties.h"
#include "wasm-binary.h"
#include "wasm-debug.h"

Expand Down Expand Up @@ -2680,6 +2681,8 @@ void StackIRGenerator::emit(Expression* curr) {
stackInst = makeStackInst(StackInst::LoopBegin, curr);
} else if (curr->is<Try>()) {
stackInst = makeStackInst(StackInst::TryBegin, curr);
} else if (curr->is<TryTable>()) {
stackInst = makeStackInst(StackInst::TryTableBegin, curr);
} else {
stackInst = makeStackInst(curr);
}
Expand All @@ -2696,6 +2699,8 @@ void StackIRGenerator::emitScopeEnd(Expression* curr) {
stackInst = makeStackInst(StackInst::LoopEnd, curr);
} else if (curr->is<Try>()) {
stackInst = makeStackInst(StackInst::TryEnd, curr);
} else if (curr->is<TryTable>()) {
stackInst = makeStackInst(StackInst::TryTableEnd, curr);
} else {
WASM_UNREACHABLE("unexpected expr type");
}
Expand All @@ -2708,15 +2713,15 @@ StackInst* StackIRGenerator::makeStackInst(StackInst::Op op,
ret->op = op;
ret->origin = origin;
auto stackType = origin->type;
if (origin->is<Block>() || origin->is<Loop>() || origin->is<If>() ||
origin->is<Try>()) {
if (Properties::isControlFlowStructure(origin)) {
if (stackType == Type::unreachable) {
// There are no unreachable blocks, loops, or ifs. we emit extra
// unreachables to fix that up, so that they are valid as having none
// type.
// There are no unreachable blocks, loops, ifs, trys, or try_tables. we
// emit extra unreachables to fix that up, so that they are valid as
// having none type.
stackType = Type::none;
} else if (op != StackInst::BlockEnd && op != StackInst::IfEnd &&
op != StackInst::LoopEnd && op != StackInst::TryEnd) {
op != StackInst::LoopEnd && op != StackInst::TryEnd &&
op != StackInst::TryTableEnd) {
// If a concrete type is returned, we mark the end of the construct has
// having that type (as it is pushed to the value stack at that point),
// other parts are marked as none).
Expand All @@ -2742,7 +2747,8 @@ void StackIRToBinaryWriter::write() {
case StackInst::Basic:
case StackInst::BlockBegin:
case StackInst::IfBegin:
case StackInst::LoopBegin: {
case StackInst::LoopBegin:
case StackInst::TryTableBegin: {
writer.visit(inst->origin);
break;
}
Expand All @@ -2751,7 +2757,8 @@ void StackIRToBinaryWriter::write() {
[[fallthrough]];
case StackInst::BlockEnd:
case StackInst::IfEnd:
case StackInst::LoopEnd: {
case StackInst::LoopEnd:
case StackInst::TryTableEnd: {
writer.emitScopeEnd(inst->origin);
break;
}
Expand Down
61 changes: 61 additions & 0 deletions test/lit/passes/stack-ir-eh.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --generate-stack-ir --optimize-stack-ir \
;; RUN: -all --print-stack-ir | filecheck %s

(module
;; CHECK: (tag $e-i32 (param i32))
(tag $e-i32 (param i32))

;; CHECK: (func $foo (type $0)
;; CHECK-NEXT: )
(func $foo)

;; CHECK: (func $test (type $0)
;; CHECK-NEXT: block $outer
;; CHECK-NEXT: block $l-catch (result i32)
;; CHECK-NEXT: block $l-catch-ref (type $1) (result i32 exnref)
;; CHECK-NEXT: block $l-catch-all
;; CHECK-NEXT: block $l-catch-all-ref (result exnref)
;; 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)
;; CHECK-NEXT: call $foo
;; CHECK-NEXT: end
;; CHECK-NEXT: br $outer
;; CHECK-NEXT: end
;; CHECK-NEXT: throw_ref
;; CHECK-NEXT: end
;; CHECK-NEXT: br $outer
;; CHECK-NEXT: end
;; CHECK-NEXT: tuple.drop 2
;; CHECK-NEXT: br $outer
;; CHECK-NEXT: end
;; CHECK-NEXT: drop
;; CHECK-NEXT: end
;; CHECK-NEXT: )
(func $test
(block $outer
(drop
(block $l-catch (result i32)
(tuple.drop 2
(block $l-catch-ref (result i32 exnref)
(block $l-catch-all
(throw_ref
(block $l-catch-all-ref (result exnref)
(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)
(call $foo)
)
(br $outer)
)
)
)
(br $outer)
)
)
(br $outer)
)
)
)
)
)

0 comments on commit f9948a0

Please sign in to comment.