Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/passes/TranslateEH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,15 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {

// If we don't have any catches, we don't need to do more.
if (curr->catchBodies.empty()) { // catch-less try
replaceCurrent(tryTable);
// If we need an outer block for other reasons (if this is a target of a
// delegate), we insert the new try_table into it. If not we just replace
// the current try with the new try_table.
if (outerBlock) {
outerBlock->list.push_back(tryTable);
replaceCurrent(outerBlock);
} else {
replaceCurrent(tryTable);
}
return;
}

Expand Down
55 changes: 49 additions & 6 deletions test/lit/passes/translate-to-new-eh.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1788,13 +1788,15 @@
)

;; CHECK: (func $deletate-target-outer-try-unreachable (type $1)
;; CHECK-NEXT: (try_table
;; CHECK-NEXT: (throw_ref
;; CHECK-NEXT: (block $l00 (result exnref)
;; CHECK-NEXT: (try_table (catch_all_ref $l00)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (block $outer1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this outer block needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we go into actually processing catches and delegates, we create an outer block in this condition:

if (it != delegateTargetToBrTarget.end() || curr->isCatch()) {

Because

  1. In case of a delegate, it will be translated into a throw_ref to the delegate target. But when an exception does not occur, we have to bail out of that throw_ref. And this outer block is that bail-out destination. It is explained here:

    // (br $outer)

    // (br $outer ;; Now has the try_table as a child.

  2. In case of catches, we proceed into catch handler parts unless we bail out in case an exception does not occur. It is explained here:

    // (br $outer)

    // (br $outer

But this test does not need a br because the body's type is unreachable due to a return. We omit that br here:

auto* brToOuter = curr->body->type == Type::unreachable
? nullptr
: builder.makeBreak(outerBlock->name);

It is hard to check this condition before creating an outer block. We can run BranchSeeker to find out if the block is actually used as a destination for a branch afterwards to remove this unnecessary block. Do you want me to do that? It is not hard, but I didn't end up doing that because we might be adding too much optimization in this pass which should ideally be done by other passes later. Currently we run this pass at the end and don't run any other optimization passes after this, I already baked some amount of optimization into it though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried removing it without using BranchSeeker: 8c8fcc5

;; CHECK-NEXT: (try_table
;; CHECK-NEXT: (throw_ref
;; CHECK-NEXT: (block $l00 (result exnref)
;; CHECK-NEXT: (try_table (catch_all_ref $l00)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down Expand Up @@ -2125,4 +2127,45 @@
(delegate 0)
)
)

;; CHECK: (func $try-delegate-within-catchless-try (type $1)
;; CHECK-NEXT: (block $outer1
;; CHECK-NEXT: (try_table
;; CHECK-NEXT: (throw_ref
;; CHECK-NEXT: (block $l00 (result exnref)
;; CHECK-NEXT: (try_table (catch_all_ref $l00)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $outer1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; STACKIR-OPT: (func $try-delegate-within-catchless-try (type $1)
;; STACKIR-OPT-NEXT: block $outer1
;; STACKIR-OPT-NEXT: try_table
;; STACKIR-OPT-NEXT: block $l00 (result exnref)
;; STACKIR-OPT-NEXT: try_table (catch_all_ref $l00)
;; STACKIR-OPT-NEXT: call $foo
;; STACKIR-OPT-NEXT: end
;; STACKIR-OPT-NEXT: br $outer1
;; STACKIR-OPT-NEXT: end
;; STACKIR-OPT-NEXT: throw_ref
;; STACKIR-OPT-NEXT: end
;; STACKIR-OPT-NEXT: unreachable
;; STACKIR-OPT-NEXT: end
;; STACKIR-OPT-NEXT: )
(func $try-delegate-within-catchless-try
(try $l0
(do
(try
(do
(call $foo)
)
(delegate $l0)
)
)
)
)
)