Skip to content

Commit

Permalink
[EH] Misc. fixes for EH (WebAssembly#6195)
Browse files Browse the repository at this point in the history
- Deletes a stray whitespace after `throw_ref`
- Adds missing `makeThrowRef` to `wasm-builder.h`
- Adds a case for `TryTable` in `ControlFlowWalker`
  • Loading branch information
aheejin authored and radekdoulik committed Jul 12, 2024
1 parent 985e66f commit cf4f381
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ struct PrintExpressionContents
printMedium(o, "rethrow ");
printName(curr->target, o);
}
void visitThrowRef(ThrowRef* curr) { printMedium(o, "throw_ref "); }
void visitThrowRef(ThrowRef* curr) { printMedium(o, "throw_ref"); }
void visitNop(Nop* curr) { printMinor(o, "nop"); }
void visitUnreachable(Unreachable* curr) { printMinor(o, "unreachable"); }
void visitPop(Pop* curr) {
Expand Down
6 changes: 6 additions & 0 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,12 @@ class Builder {
ret->finalize();
return ret;
}
ThrowRef* makeThrowRef(Expression* exnref) {
auto* ret = wasm.allocator.alloc<ThrowRef>();
ret->exnref = exnref;
ret->finalize();
return ret;
}
Unreachable* makeUnreachable() { return wasm.allocator.alloc<Unreachable>(); }
Pop* makePop(Type type) {
auto* ret = wasm.allocator.alloc<Pop>();
Expand Down
14 changes: 9 additions & 5 deletions src/wasm-traversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ using ExpressionStack = SmallVector<Expression*, 10>;

template<typename SubType, typename VisitorType = Visitor<SubType>>
struct ControlFlowWalker : public PostWalker<SubType, VisitorType> {
ExpressionStack controlFlowStack; // contains blocks, loops, and ifs
// contains blocks, loops, ifs, trys, and try_tables
ExpressionStack controlFlowStack;

// Uses the control flow stack to find the target of a break to a name
Expression* findBreakTarget(Name name) {
Expand All @@ -420,8 +421,9 @@ struct ControlFlowWalker : public PostWalker<SubType, VisitorType> {
return curr;
}
} else {
// an if or try, ignorable
assert(curr->template is<If>() || curr->template is<Try>());
// an if, try, or try_table, ignorable
assert(curr->template is<If>() || curr->template is<Try>() ||
curr->template is<TryTable>());
}
if (i == 0) {
return nullptr;
Expand All @@ -447,7 +449,8 @@ struct ControlFlowWalker : public PostWalker<SubType, VisitorType> {
case Expression::Id::BlockId:
case Expression::Id::IfId:
case Expression::Id::LoopId:
case Expression::Id::TryId: {
case Expression::Id::TryId:
case Expression::Id::TryTableId: {
self->pushTask(SubType::doPostVisitControlFlow, currp);
break;
}
Expand All @@ -461,7 +464,8 @@ struct ControlFlowWalker : public PostWalker<SubType, VisitorType> {
case Expression::Id::BlockId:
case Expression::Id::IfId:
case Expression::Id::LoopId:
case Expression::Id::TryId: {
case Expression::Id::TryId:
case Expression::Id::TryTableId: {
self->pushTask(SubType::doPreVisitControlFlow, currp);
break;
}
Expand Down
24 changes: 12 additions & 12 deletions test/lit/basic/exception-handling.wast
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
)

;; CHECK-TEXT: (func $try-table-and-throw-ref (type $0)
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (block $l-catch-all-ref (result exnref)
;; CHECK-TEXT-NEXT: (try_table (catch_all_ref $l-catch-all-ref)
;; CHECK-TEXT-NEXT: (throw $e-i64
Expand All @@ -173,7 +173,7 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $try-table-and-throw-ref (type $0)
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (block $label$1 (result exnref)
;; CHECK-BIN-NEXT: (try_table (catch_all_ref $label$1)
;; CHECK-BIN-NEXT: (throw $e-i64
Expand Down Expand Up @@ -300,7 +300,7 @@
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $l-catch-ref (result exnref)
;; CHECK-TEXT-NEXT: (block $l-catch-all
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (block $l-catch-all-ref (result exnref)
;; CHECK-TEXT-NEXT: (try_table (catch $e-empty $l-catch) (catch_ref $e-empty $l-catch-ref) (catch_all $l-catch-all) (catch_all_ref $l-catch-all-ref)
;; CHECK-TEXT-NEXT: (call $foo)
Expand All @@ -323,7 +323,7 @@
;; CHECK-BIN-NEXT: (drop
;; CHECK-BIN-NEXT: (block $label$3 (result exnref)
;; CHECK-BIN-NEXT: (block $label$4
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (block $label$5 (result exnref)
;; CHECK-BIN-NEXT: (try_table (catch $e-empty $label$2) (catch_ref $e-empty $label$3) (catch_all $label$4) (catch_all_ref $label$5)
;; CHECK-BIN-NEXT: (call $foo)
Expand Down Expand Up @@ -376,7 +376,7 @@
;; CHECK-TEXT-NEXT: (tuple.drop 2
;; CHECK-TEXT-NEXT: (block $l-catch-ref (type $5) (result i32 exnref)
;; CHECK-TEXT-NEXT: (block $l-catch-all
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (block $l-catch-all-ref (result exnref)
;; CHECK-TEXT-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-TEXT-NEXT: (call $foo)
Expand All @@ -403,7 +403,7 @@
;; CHECK-BIN-NEXT: (local.set $0
;; CHECK-BIN-NEXT: (block $label$3 (type $5) (result i32 exnref)
;; CHECK-BIN-NEXT: (block $label$4
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (block $label$5 (result exnref)
;; CHECK-BIN-NEXT: (try_table (catch $e-i32 $label$2) (catch_ref $e-i32 $label$3) (catch_all $label$4) (catch_all_ref $label$5)
;; CHECK-BIN-NEXT: (call $foo)
Expand Down Expand Up @@ -474,7 +474,7 @@
;; CHECK-TEXT-NEXT: (tuple.drop 3
;; CHECK-TEXT-NEXT: (block $l-catch-ref (type $2) (result i32 i64 exnref)
;; CHECK-TEXT-NEXT: (block $l-catch-all
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (throw_ref
;; CHECK-TEXT-NEXT: (block $l-catch-all-ref (result exnref)
;; CHECK-TEXT-NEXT: (try_table (catch $e-i32-i64 $l-catch) (catch_ref $e-i32-i64 $l-catch-ref) (catch_all $l-catch-all) (catch_all_ref $l-catch-all-ref)
;; CHECK-TEXT-NEXT: (call $foo)
Expand Down Expand Up @@ -504,7 +504,7 @@
;; CHECK-BIN-NEXT: (local.set $0
;; CHECK-BIN-NEXT: (block $label$3 (type $2) (result i32 i64 exnref)
;; CHECK-BIN-NEXT: (block $label$4
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (throw_ref
;; CHECK-BIN-NEXT: (block $label$5 (result exnref)
;; CHECK-BIN-NEXT: (try_table (catch $e-i32-i64 $label$2) (catch_ref $e-i32-i64 $label$3) (catch_all $label$4) (catch_all_ref $label$5)
;; CHECK-BIN-NEXT: (call $foo)
Expand Down Expand Up @@ -754,7 +754,7 @@
;; CHECK-BIN-NODEBUG-NEXT: )

;; CHECK-BIN-NODEBUG: (func $4 (type $0)
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (block $label$1 (result exnref)
;; CHECK-BIN-NODEBUG-NEXT: (try_table (catch_all_ref $label$1)
;; CHECK-BIN-NODEBUG-NEXT: (throw $tag$1
Expand Down Expand Up @@ -836,7 +836,7 @@
;; CHECK-BIN-NODEBUG-NEXT: (drop
;; CHECK-BIN-NODEBUG-NEXT: (block $label$3 (result exnref)
;; CHECK-BIN-NODEBUG-NEXT: (block $label$4
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (block $label$5 (result exnref)
;; CHECK-BIN-NODEBUG-NEXT: (try_table (catch $tag$4 $label$2) (catch_ref $tag$4 $label$3) (catch_all $label$4) (catch_all_ref $label$5)
;; CHECK-BIN-NODEBUG-NEXT: (call $0)
Expand All @@ -863,7 +863,7 @@
;; CHECK-BIN-NODEBUG-NEXT: (local.set $0
;; CHECK-BIN-NODEBUG-NEXT: (block $label$3 (type $5) (result i32 exnref)
;; CHECK-BIN-NODEBUG-NEXT: (block $label$4
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (block $label$5 (result exnref)
;; CHECK-BIN-NODEBUG-NEXT: (try_table (catch $tag$0 $label$2) (catch_ref $tag$0 $label$3) (catch_all $label$4) (catch_all_ref $label$5)
;; CHECK-BIN-NODEBUG-NEXT: (call $0)
Expand Down Expand Up @@ -909,7 +909,7 @@
;; CHECK-BIN-NODEBUG-NEXT: (local.set $0
;; CHECK-BIN-NODEBUG-NEXT: (block $label$3 (type $2) (result i32 i64 exnref)
;; CHECK-BIN-NODEBUG-NEXT: (block $label$4
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (throw_ref
;; CHECK-BIN-NODEBUG-NEXT: (block $label$5 (result exnref)
;; CHECK-BIN-NODEBUG-NEXT: (try_table (catch $tag$2 $label$2) (catch_ref $tag$2 $label$3) (catch_all $label$4) (catch_all_ref $label$5)
;; CHECK-BIN-NODEBUG-NEXT: (call $0)
Expand Down

0 comments on commit cf4f381

Please sign in to comment.