Skip to content

Optimize block tails where a dropped br_if's value is redundant #7506

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 25 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2312382
opt for #7489
xuruiyang2002 Apr 16, 2025
4c8983f
tests
xuruiyang2002 Apr 16, 2025
408b606
tests
xuruiyang2002 Apr 16, 2025
dd66eac
Fix: ensure value exists
xuruiyang2002 Apr 17, 2025
18ae939
Update src/passes/RemoveUnusedBrs.cpp
xuruiyang2002 Apr 22, 2025
4ce7032
Rename brk to br
xuruiyang2002 Apr 22, 2025
541906d
Use hasUnremovableSideEffects instead of hasSideEffects
xuruiyang2002 Apr 22, 2025
71bcb13
If-check step by step to avoid wasted work
xuruiyang2002 Apr 22, 2025
18dbc09
Optimization performed only when br_if targets its parent
xuruiyang2002 Apr 22, 2025
8a1f49b
Add a test
xuruiyang2002 Apr 22, 2025
c634b9f
Update src/passes/RemoveUnusedBrs.cpp
xuruiyang2002 Apr 25, 2025
659ffa3
Update src/passes/RemoveUnusedBrs.cpp
xuruiyang2002 Apr 25, 2025
8ec64c9
Update src/passes/RemoveUnusedBrs.cpp
xuruiyang2002 Apr 25, 2025
f9b3b84
Update src/passes/RemoveUnusedBrs.cpp
xuruiyang2002 Apr 25, 2025
e64af88
Update test/lit/passes/remove-unused-brs.wast
xuruiyang2002 Apr 25, 2025
d5de05c
Update test/lit/passes/remove-unused-brs.wast
xuruiyang2002 Apr 25, 2025
af88eb0
Update test cases
xuruiyang2002 Apr 25, 2025
30d4af7
Fix typo
xuruiyang2002 Apr 30, 2025
66151b5
Update test/lit/passes/remove-unused-brs.wast
xuruiyang2002 Apr 30, 2025
c60d693
Update test
xuruiyang2002 Apr 30, 2025
c2fa4a7
Fix: br->condition is null
xuruiyang2002 Apr 30, 2025
66ac882
Update
xuruiyang2002 May 1, 2025
b47ae4f
Add a test
xuruiyang2002 May 1, 2025
d6b6f36
Update test/lit/passes/remove-unused-brs.wast
kripken May 2, 2025
1d4ec95
Update test/lit/passes/remove-unused-brs.wast
kripken May 2, 2025
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
39 changes: 39 additions & 0 deletions src/passes/RemoveUnusedBrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,45 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
tablify(curr);
// Pattern-patch ifs, recreating them when it makes sense.
restructureIf(curr);

// Optimize block tails where a dropped `br_if`'s value is redundant
// when the br_if targets the block itself:
//
// (block $block (result i32)
// ..
// (drop
// (br_if $block ;; <- MUST target parent $block
// (value)
// (condition)
// )
// )
// (value) ;; <- MUST be same as br_if's value
// )
// =>
// (block $block (result i32)
// ..
// (drop
// (condition)
// )
// (value)
// )
size_t size = curr->list.size();
auto* secondLast = curr->list[size - 2];
auto* last = curr->list[size - 1];
if (auto* drop = secondLast->dynCast<Drop>()) {
if (auto* br = drop->value->dynCast<Break>();
br && br->value && br->condition) {
if (br->name == curr->name) {
if (!EffectAnalyzer(passOptions, *getModule(), br->value)
.hasUnremovableSideEffects()) {
if (ExpressionAnalyzer::equal(br->value, last)) {
// All conditions met, perform the update.
drop->value = br->condition;
}
}
}
}
}
}
}

Expand Down
166 changes: 146 additions & 20 deletions test/lit/passes/remove-unused-brs.wast
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

(module
;; Regression test in which we need to calculate a proper LUB.
;; CHECK: (func $selectify-fresh-lub (type $3) (param $x i32) (result anyref)
;; CHECK: (func $selectify-fresh-lub (type $4) (param $x i32) (result anyref)
;; CHECK-NEXT: (select (result i31ref)
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: (ref.i31
Expand All @@ -30,7 +30,7 @@
)
)

;; CHECK: (func $selectify-simple (type $1) (param $0 i32) (result i32)
;; CHECK: (func $selectify-simple (type $0) (param $0 i32) (result i32)
;; CHECK-NEXT: (select
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (i32.lt_u
Expand Down Expand Up @@ -73,7 +73,7 @@
)
)

;; CHECK: (func $restructure-br_if (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (then
Expand Down Expand Up @@ -104,12 +104,12 @@
)
)

;; CHECK: (func $nothing (type $0)
;; CHECK: (func $nothing (type $1)
;; CHECK-NEXT: )
(func $nothing)


;; CHECK: (func $restructure-br_if-condition-reorderable (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if-condition-reorderable (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (call $nothing)
Expand Down Expand Up @@ -146,7 +146,7 @@
)
)

;; CHECK: (func $restructure-br_if-value-effectful (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if-value-effectful (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (select
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (call $nothing)
Expand Down Expand Up @@ -188,7 +188,7 @@
)
)

;; CHECK: (func $restructure-br_if-value-effectful-corner-case-1 (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if-value-effectful-corner-case-1 (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (block $x (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $x
Expand Down Expand Up @@ -226,14 +226,14 @@
)
)

;; CHECK: (func $get-i32 (type $4) (result i32)
;; CHECK: (func $get-i32 (type $2) (result i32)
;; CHECK-NEXT: (i32.const 400)
;; CHECK-NEXT: )
(func $get-i32 (result i32)
(i32.const 400)
)

;; CHECK: (func $restructure-br_if-value-effectful-corner-case-2 (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if-value-effectful-corner-case-2 (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (block $x (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $x
Expand Down Expand Up @@ -272,7 +272,7 @@
(call $get-i32)
)
)
;; CHECK: (func $restructure-br_if-value-effectful-corner-case-3 (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if-value-effectful-corner-case-3 (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (block $x (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $x
Expand Down Expand Up @@ -305,7 +305,7 @@
)
)

;; CHECK: (func $restructure-br_if-value-effectful-corner-case-4 (type $1) (param $x i32) (result i32)
;; CHECK: (func $restructure-br_if-value-effectful-corner-case-4 (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (block $x (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $x
Expand Down Expand Up @@ -340,9 +340,135 @@
)
)

;; CHECK: (func $restructure-select-no-multivalue (type $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)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call $get-i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-value-redundant-in-block-tail-1 (result i32)
;; The br_if's value is equal to the value right after it, so we can remove it.
(block $parent (result i32)
(call $nothing)
(drop
(br_if $parent
(i32.const 1)
(call $get-i32)
)
)
(i32.const 1)
)
)

;; CHECK: (func $restructure-br_if-value-redundant-in-block-tail-2 (type $2) (result i32)
;; CHECK-NEXT: (block $parent (result i32)
;; CHECK-NEXT: (call $nothing)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $parent
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: (call $get-i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-value-redundant-in-block-tail-2 (result i32)
;; As above, but now the value is different, so we do not optimize
(block $parent (result i32)
(call $nothing)
(drop
(br_if $parent
(i32.const 2)
(call $get-i32)
)
)
(i32.const 1)
)
)

;; CHECK: (func $restructure-br_if-value-redundant-in-block-tail-3 (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (block $parent (result i32)
;; CHECK-NEXT: (call $nothing)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $parent
;; CHECK-NEXT: (call $get-i32)
;; CHECK-NEXT: (call $get-i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $get-i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-value-redundant-in-block-tail-3 (param $x i32) (result i32)
;; As above, but now the value has effects, so we do not optimize
(block $parent (result i32)
(call $nothing)
(drop
(br_if $parent
(call $get-i32)
(call $get-i32)
)
)
(call $get-i32)
)
)

;; CHECK: (func $restructure-br_if-value-redundant-in-block-tail-4 (type $2) (result i32)
;; CHECK-NEXT: (block $outer (result i32)
;; CHECK-NEXT: (block $inner (result i32)
;; CHECK-NEXT: (call $nothing)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_if $outer
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (call $get-i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-value-redundant-in-block-tail-4 (result i32)
;; As above, but the br_if targets another block, so we do not optimize.
(block $outer (result i32)
(block $inner (result i32)
(call $nothing)
(drop
(br_if $outer
(i32.const 1)
(call $get-i32)
)
)
(i32.const 1)
)
)
)

;; CHECK: (func $restructure-br_if-value-redundant-in-block-tail-5 (type $2) (result i32)
;; CHECK-NEXT: (block $parent (result i32)
;; CHECK-NEXT: (call $nothing)
;; CHECK-NEXT: (br $parent
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $restructure-br_if-value-redundant-in-block-tail-5 (result i32)
;; As above, but the br lacks a condition. We do not bother to optimize
;; the dead code after it, but also should not error here.
(block $parent (result i32)
(call $nothing)
(br $parent
(i32.const 1)
)
(i32.const 1)
)
)

;; CHECK: (func $restructure-select-no-multivalue (type $1)
;; CHECK-NEXT: (tuple.drop 2
;; CHECK-NEXT: (block $block (type $2) (result i32 i32)
;; CHECK-NEXT: (block $block (type $3) (result i32 i32)
;; CHECK-NEXT: (tuple.drop 2
;; CHECK-NEXT: (br_if $block
;; CHECK-NEXT: (tuple.make 2
Expand Down Expand Up @@ -387,7 +513,7 @@
)
)

;; CHECK: (func $if-of-if (type $0)
;; CHECK: (func $if-of-if (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (select
Expand Down Expand Up @@ -421,7 +547,7 @@
)
)

;; CHECK: (func $if-of-if-but-side-effects (type $0)
;; CHECK: (func $if-of-if-but-side-effects (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (local.tee $x
Expand Down Expand Up @@ -460,7 +586,7 @@
)
)

;; CHECK: (func $if-of-if-but-too-costly (type $0)
;; CHECK: (func $if-of-if-but-too-costly (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (local.tee $x
Expand Down Expand Up @@ -515,7 +641,7 @@
)
)

;; CHECK: (func $if-of-if-but-inner-else (type $0)
;; CHECK: (func $if-of-if-but-inner-else (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (local.tee $x
Expand Down Expand Up @@ -555,7 +681,7 @@
)
)

;; CHECK: (func $if-of-if-but-outer-else (type $0)
;; CHECK: (func $if-of-if-but-outer-else (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (local.tee $x
Expand Down Expand Up @@ -595,7 +721,7 @@
)
)

;; CHECK: (func $unreachable-if (type $0)
;; CHECK: (func $unreachable-if (type $1)
;; CHECK-NEXT: (block $block
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (unreachable)
Expand Down Expand Up @@ -625,7 +751,7 @@
)
)

;; CHECK: (func $loop-with-unreachable-if (type $0)
;; CHECK: (func $loop-with-unreachable-if (type $1)
;; CHECK-NEXT: (loop $label
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (unreachable)
Expand Down
Loading