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

[GC] Refinalize after selectify in RemoveUnusedBrs #7104

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions src/passes/RemoveUnusedBrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
PassOptions& passOptions;

bool needUniqify = false;
bool refinalize = false;

FinalOptimizer(PassOptions& passOptions) : passOptions(passOptions) {}

Expand Down Expand Up @@ -1419,8 +1420,14 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
if (condition.invalidates(ifTrue) || condition.invalidates(ifFalse)) {
return nullptr;
}
return Builder(*getModule())
.makeSelect(iff->condition, iff->ifTrue, iff->ifFalse);
auto* select = Builder(*getModule())
.makeSelect(iff->condition, iff->ifTrue, iff->ifFalse);
if (select->type != iff->type) {
// If the select is more refined than the if it replaces, we must
// propagate that outwards.
refinalize = true;
}
return select;
}

void visitLocalSet(LocalSet* curr) {
Expand Down Expand Up @@ -1793,6 +1800,9 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
if (finalOptimizer.needUniqify) {
wasm::UniqueNameMapper::uniquify(func->body);
}
if (finalOptimizer.refinalize) {
ReFinalize().walkFunctionInModule(func, getModule());
}
}
};

Expand Down
29 changes: 29 additions & 0 deletions test/lit/passes/remove-unused-brs-gc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -864,4 +864,33 @@
)
)
)

;; CHECK: (func $select-refinalize (type $13) (param $param (ref $struct)) (result (ref struct))
;; CHECK-NEXT: (select (result (ref $struct))
;; CHECK-NEXT: (select (result (ref $struct))
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $param)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-refinalize (param $param (ref $struct)) (result (ref struct))
;; The inner if can turn into a select. The type then changes, allowing the
;; outer select to be refined, which will error if we do not refinalize.
(select (result (ref struct))
(if (result (ref struct))
(i32.const 0)
(then
(struct.new_default $struct)
)
(else
(struct.new_default $struct)
)
)
(local.get $param)
(i32.const 0)
)
)
)
Loading