Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCE: Fix old EH on a pop that gets moved in a catch body #6400

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 25 additions & 7 deletions src/passes/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
// have no side effects.
//

#include <ir/iteration.h>
#include <ir/properties.h>
#include <ir/type-updating.h>
#include <pass.h>
#include <vector>
#include <wasm-builder.h>
#include <wasm.h>
#include "ir/eh-utils.h"
#include "ir/iteration.h"
#include "ir/properties.h"
#include "ir/type-updating.h"
#include "pass.h"
#include "vector"
#include "wasm-builder.h"
#include "wasm.h"

namespace wasm {

Expand Down Expand Up @@ -89,7 +90,11 @@ struct DeadCodeElimination
Builder builder(*getModule());
std::vector<Expression*> remainingChildren;
bool afterUnreachable = false;
bool hasPop = false;
for (auto* child : ChildIterator(curr)) {
if (child->is<Pop>()) {
hasPop = true;
}
if (afterUnreachable) {
typeUpdater.noteRecursiveRemoval(child);
continue;
Expand All @@ -105,6 +110,11 @@ struct DeadCodeElimination
replaceCurrent(remainingChildren[0]);
} else {
replaceCurrent(builder.makeBlock(remainingChildren));
if (hasPop) {
// We are moving a pop into a new block we just created, which
// means we may need to fix things up here.
needEHFixups = true;
}
}
}
}
Expand Down Expand Up @@ -179,6 +189,14 @@ struct DeadCodeElimination
WASM_UNREACHABLE("unimplemented DCE control flow structure");
}
}

bool needEHFixups = false;

void visitFunction(Function* curr) {
if (needEHFixups) {
EHUtils::handleBlockNestedPops(curr, *getModule());
}
}
};

Pass* createDeadCodeEliminationPass() { return new DeadCodeElimination(); }
Expand Down
56 changes: 54 additions & 2 deletions test/lit/passes/dce-eh-old.wast
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
;; reachable
(module
;; CHECK: (tag $e)
(tag $e)

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

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


;; CHECK: (func $try_unreachable (type $0)
;; CHECK-NEXT: (try $try
Expand Down Expand Up @@ -130,4 +132,54 @@
)
)
)

;; CHECK: (func $call-pop-catch (type $0)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (block $label
;; CHECK-NEXT: (try $try
;; CHECK-NEXT: (do
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $e-i32
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (pop i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $label)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $call-pop-catch
(block $label
(try
(do
(nop)
)
(catch $e-i32
;; This call is unreachable and can be removed. The pop, however, must
;; be carefully handled while we do so, to not break validation.
(call $helper-i32-i32
(pop i32)
(br $label)
)
(nop)
)
)
)
)

;; CHECK: (func $helper-i32-i32 (type $2) (param $0 i32) (param $1 i32)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $helper-i32-i32 (param $0 i32) (param $1 i32)
(nop)
)
)

Loading