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
Prev Previous commit
Next Next commit
Remove unused outer block
  • Loading branch information
aheejin committed Apr 23, 2024
commit 8c8fcc54de0053ba8c1d99d5d93a695a26ac6c3b
20 changes: 12 additions & 8 deletions src/passes/TranslateEH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {
// current 'try' into 'try_table' yet; it only adds block, br, and throw_ref
// instructions to complete the conversions of inner try~delegates that target
// the current try.
void processDelegateTarget(Try* curr, Block* outerBlock) {
void
processDelegateTarget(Try* curr, Block* outerBlock, bool& outerBlockUsedSoFar) {
Builder builder(*getModule());

// Convert
Expand Down Expand Up @@ -291,10 +292,12 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {
Name delegateBrTarget = delegateTargetToBrTarget[curr->name];
Expression* innerBody = nullptr;
if (curr->type.isConcrete()) {
outerBlockUsedSoFar = true;
auto* brToOuter = builder.makeBreak(outerBlock->name, curr->body);
innerBody = builder.blockifyWithName(
brToOuter, delegateBrTarget, nullptr, Type(HeapType::exn, Nullable));
} else {
outerBlockUsedSoFar = curr->body->type != Type::unreachable;
auto* brToOuter = curr->body->type == Type::unreachable
? nullptr
: builder.makeBreak(outerBlock->name);
Expand All @@ -304,7 +307,7 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {
curr->body = builder.makeThrowRef(innerBody);
}

void processDelegate(Try* curr, Block* outerBlock) {
void processDelegate(Try* curr, Block* outerBlock, bool outerBlockUsedSoFar) {
Builder builder(*getModule());
// Convert
// (try
Expand Down Expand Up @@ -332,15 +335,15 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {
// 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) {
if (outerBlock && outerBlockUsedSoFar) {
outerBlock->list.push_back(tryTable);
replaceCurrent(outerBlock);
} else {
replaceCurrent(tryTable);
}
}

void processCatches(Try* curr, Block* outerBlock) {
void processCatches(Try* curr, Block* outerBlock, bool outerBlockUsedSoFar) {
Module* wasm = getModule();
Builder builder(*wasm);

Expand Down Expand Up @@ -388,7 +391,7 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {
// 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) {
if (outerBlock && outerBlockUsedSoFar) {
outerBlock->list.push_back(tryTable);
replaceCurrent(outerBlock);
} else {
Expand Down Expand Up @@ -687,13 +690,14 @@ struct TranslateToNewEH : public WalkerPass<PostWalker<TranslateToNewEH>> {
builder.makeBlock(labels->getUnique("outer"), {}, curr->type);
}

bool outerBlockUsedSoFar = false;
if (it != delegateTargetToBrTarget.end()) {
processDelegateTarget(curr, outerBlock);
processDelegateTarget(curr, outerBlock, outerBlockUsedSoFar);
}
if (curr->isDelegate()) {
processDelegate(curr, outerBlock);
processDelegate(curr, outerBlock, outerBlockUsedSoFar);
} else { // try-catch or catch-less try
processCatches(curr, outerBlock);
processCatches(curr, outerBlock, outerBlockUsedSoFar);
}
}

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

;; CHECK: (func $deletate-target-outer-try-unreachable (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: (return)
;; 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: )
;; CHECK-NEXT: )
Expand Down