Skip to content

RemoveUnusedBrs: optimize unreachable control flow mixed with side-effecting branches #7639

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

Open
wants to merge 11 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
38 changes: 38 additions & 0 deletions src/passes/RemoveUnusedBrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "ir/effects.h"
#include "ir/gc-type-utils.h"
#include "ir/literal-utils.h"
#include "ir/localize.h"
#include "ir/properties.h"
#include "ir/utils.h"
#include "parsing.h"
#include "pass.h"
Expand Down Expand Up @@ -1881,6 +1883,42 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
start = end;
}
}

void visitBreak(Break* curr) {
if (!curr->condition) {
return;
}
auto* value = Properties::getFallthrough(
curr->condition, passOptions, *getModule());
// optimize if condition's fallthrough is a constant
if (auto* c = value->dynCast<Const>()) {
if (curr->value) {
curr->condition = getDroppedChildrenAndAppend(
curr->condition,
*getModule(),
passOptions,
Builder(*getModule()).makeConst(c->value),
DropMode::NoticeParentEffects);
} else {
ChildLocalizer localizer(
curr, getFunction(), *getModule(), passOptions);
auto* block = localizer.getChildrenReplacement();
if (c->value.geti32()) {
// the branch is always taken, make it unconditional
curr->condition = nullptr;
curr->type = Type::unreachable;
Copy link
Contributor Author

@xuruiyang2002 xuruiyang2002 Jun 5, 2025

Choose a reason for hiding this comment

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

Because of this line curr->type = Type::unreachable; (which fixes type mismatch after transforming br_if to br)

As for the case found by the fuzzer, it transforms code from

    block  ;; label = @1
      global.get 0
      if  ;; label = @2
        i32.const 1
        br_if 1 (;@1;)
      else
        unreachable
      end
      local.get 0
      call 0
    end

to

    block  ;; label = @1
      global.get 0
      if  ;; label = @2
        br 1 (;@1;)
      else
        unreachable
      end
      local.get 0
      call 0
    end

which in SimplifyLocals triggers the assertion:

if (iff->ifTrue->type == Type::unreachable) {
// since the if type is none
assert(iff->ifFalse->type != Type::unreachable);
if (!ifFalse.empty()) {
goodIndex = ifFalse.begin()->first;
found = true;
}
} else if (iff->ifFalse->type == Type::unreachable) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this assertion always hold true?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. If an if has two branches of unreachable then the if should be unreachable: logically, if neither if arm returns, neither does the if itself.

So we need to figure out how things got to that situation, and prevent it. Likely a refinalize() call is missing somewhere (which updates the types of expression after changes - that would update an if to unreachable if it needs to be, etc.)

Copy link
Member

Choose a reason for hiding this comment

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

Probably the issue is that turning a br_if into a br makes it unreachable, when it wasn't before. Setting refinalize = true; in that case should fix things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, and I've updated.

And I've also fuzzing for about one hour; all is well.

block->list.push_back(curr);
block->finalize();
// the type is changed , so refinalize
refinalize = true;
replaceCurrent(block);
} else {
// the branch is never taken, allow control flow to fall through
replaceCurrent(block);
}
}
}
}
};
FinalOptimizer finalOptimizer(getPassOptions());
finalOptimizer.setModule(getModule());
Expand Down
34 changes: 34 additions & 0 deletions test/lit/passes/O1.wast
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
;; RUN: foreach %s %t wasm-opt -O1 -S -o - | filecheck %s

(module

(import "env" "fimport$0" (func $fimport$0 (param i32)))
;; CHECK: (type $0 (func (result i32)))

;; CHECK: (memory $0 1 1)
Expand Down Expand Up @@ -38,5 +40,37 @@
(i32.const -2147483648)
)
)
(func $two-branches-unreachable
;; remove-unused-brs makes the break unconditional,
;; thus in this case the two branches are unreachable,
;; refinalization is required.
(local $0 i32)
(block $label
(block $block
(if
(local.get $0)
(then
(if
(local.tee $0
(i32.const 1)
)
(then
(br $label)
)
(else
(br $block)
)
)
)
(else
(unreachable)
)
)
)
(call $fimport$0
(local.get $0)
)
)
)
)

78 changes: 77 additions & 1 deletion test/lit/passes/remove-unused-brs.wast
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,77 @@
)
)

;; CHECK: (func $restructure-br_if-constant-branch-1 (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (block $x (result i32)
;; CHECK-NEXT: (br_if $x
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $x
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-constant-branch-1 (param $x i32) (result i32)
(block $x (result i32)
(br_if $x
(unreachable)
(local.tee $x
(i32.const 42)
)
)
(unreachable)
)
)
;; CHECK: (func $restructure-br_if-constant-branch-2 (type $5) (param $x i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (block $x
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (local.tee $x
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block $x0
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (local.tee $x
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-constant-branch-2 (param $x i32)
;; there is no value, thus it can be simpler as below
(block $x
(br_if $x
;; the branch is always taken, make it unconditional
(local.tee $x
(i32.const 1)
)
)
)
(block $x
(br_if $x
;; the branch is never taken, allow control flow to fall through
(local.tee $x
(i32.const 0)
)
)
)
)


;; CHECK: (func $restructure-br_if-value-redundant-in-block-tail-1 (type $2) (result i32)
;; CHECK-NEXT: (block $parent (result i32)
;; CHECK-NEXT: (call $nothing)
Expand Down Expand Up @@ -472,7 +543,12 @@
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $block
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: (local.tee $temp
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $temp
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down
Loading
Loading