From 00c75b517b48d6ceaaa085198bdb6b8d4524a6d6 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Wed, 27 Dec 2023 21:11:19 -0800 Subject: [PATCH] [EH] Misc. fixes for EH - Deletes a stray whitespace after `throw_ref` - Adds missing `makeThrowRef` to `wasm-builder.h` - Adds a case for `TryTable` in `ControlFlowWalker` --- src/passes/Print.cpp | 2 +- src/wasm-builder.h | 6 ++++++ src/wasm-traversal.h | 14 +++++++++----- test/lit/basic/exception-handling.wast | 24 ++++++++++++------------ 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 5ea62c9f21d..5e290ebca9d 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -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) { diff --git a/src/wasm-builder.h b/src/wasm-builder.h index dd5b498be8f..a873a73094b 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -836,6 +836,12 @@ class Builder { ret->finalize(); return ret; } + ThrowRef* makeThrowRef(Expression* exnref) { + auto* ret = wasm.allocator.alloc(); + ret->exnref = exnref; + ret->finalize(); + return ret; + } Unreachable* makeUnreachable() { return wasm.allocator.alloc(); } Pop* makePop(Type type) { auto* ret = wasm.allocator.alloc(); diff --git a/src/wasm-traversal.h b/src/wasm-traversal.h index 1a458bdf7cb..f5f25dd1fe5 100644 --- a/src/wasm-traversal.h +++ b/src/wasm-traversal.h @@ -403,7 +403,8 @@ using ExpressionStack = SmallVector; template> struct ControlFlowWalker : public PostWalker { - 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) { @@ -420,8 +421,9 @@ struct ControlFlowWalker : public PostWalker { return curr; } } else { - // an if or try, ignorable - assert(curr->template is() || curr->template is()); + // an if, try, or try_table, ignorable + assert(curr->template is() || curr->template is() || + curr->template is()); } if (i == 0) { return nullptr; @@ -447,7 +449,8 @@ struct ControlFlowWalker : public PostWalker { 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; } @@ -461,7 +464,8 @@ struct ControlFlowWalker : public PostWalker { 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; } diff --git a/test/lit/basic/exception-handling.wast b/test/lit/basic/exception-handling.wast index cf0e8a62e70..b94f89e1aad 100644 --- a/test/lit/basic/exception-handling.wast +++ b/test/lit/basic/exception-handling.wast @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -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)