Skip to content

[Custom Descriptors] Fix exact types in RemoveUnusedBrs handling of SuccessOnlyIfNonNull #7615

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

Merged
merged 9 commits into from
May 20, 2025
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
9 changes: 7 additions & 2 deletions src/passes/RemoveUnusedBrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,8 +1008,13 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
// (...)
// (ref.null bot<X>)
// )
curr->ref = maybeCast(
curr->ref, Type(curr->getSentType().getHeapType(), Nullable));
//
// A RefCast is added in some cases, but this is still generally
// worth doing as the BrOnNonNull and the appended null may end up
// optimized with surrounding code.
auto* casted =
maybeCast(curr->ref, curr->getSentType().with(Nullable));
curr->ref = casted;
curr->op = BrOnNonNull;
curr->castType = Type::none;
curr->type = Type::none;
Expand Down
38 changes: 38 additions & 0 deletions test/lit/passes/remove-unused-brs-exact-only.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.

;; RUN: wasm-opt %s -all --remove-unused-brs -S -o - | filecheck %s

;; Like "remove-unused-brs-exact.wast", but with exact types in the input so we
;; cannot have a NO_CD mode.

(module
;; CHECK: (type $foo (struct))
(type $foo (struct))

;; CHECK: (func $br_on_cast_to_non_null (type $1) (param $foo (ref null $foo)) (result (ref (exact $foo)))
;; CHECK-NEXT: (block $block (result (ref (exact $foo)))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (br_on_non_null $block
;; CHECK-NEXT: (ref.cast (ref null (exact $foo))
;; CHECK-NEXT: (local.get $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_to_non_null (param $foo (ref null $foo)) (result (ref (exact $foo)))
;; We can simplify the br_on_cast to a br_on_non_null plus a cast.
(block $block (result (ref (exact $foo)))
(drop
(br_on_cast $block (ref null $foo) (ref (exact $foo))
(local.get $foo)
)
)
(unreachable)
)
)
)
Loading