Skip to content

Commit 75162c4

Browse files
committed
fix block removal in remove-unused-brs, even if not taken, if named, we must preserve it
1 parent b2445bf commit 75162c4

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

src/passes/RemoveUnusedBrs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
259259
// so only do it if it looks useful, which it definitely is if
260260
// (a) $somewhere is straight out (so the br out vanishes), and
261261
// (b) this br_if is the only branch to that block (so the block will vanish)
262-
if (brIf->name == block->name && BranchUtils::BranchSeeker::count(block, block->name) == 1) {
262+
if (brIf->name == block->name && BranchUtils::BranchSeeker::countNamed(block, block->name) == 1) {
263263
// note that we could drop the last element here, it is a br we know for sure is removable,
264264
// but telling stealSlice to steal all to the end is more efficient, it can just truncate.
265265
list[i] = builder.makeIf(brIf->condition, builder.makeBreak(brIf->name), builder.stealSlice(block, i + 1, list.size()));
@@ -449,7 +449,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
449449
auto* br = list[0]->dynCast<Break>();
450450
if (br && br->condition && br->name == curr->name) {
451451
assert(!br->value); // can't, it would be dropped or last in the block
452-
if (BranchUtils::BranchSeeker::count(curr, curr->name) == 1) {
452+
if (BranchUtils::BranchSeeker::countNamed(curr, curr->name) == 1) {
453453
// no other breaks to that name, so we can do this
454454
Builder builder(*getModule());
455455
replaceCurrent(builder.makeIf(

test/passes/remove-unused-brs.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(type $2 (func (result i32)))
55
(type $3 (func (param i32 i32) (result i32)))
66
(type $4 (func (param i32 i32)))
7+
(type $5 (func (param f32 i32 f32 i32 i32 f64 f32) (result i32)))
78
(memory $0 256 256)
89
(func $b0-yes (type $0) (param $i1 i32)
910
(block $topmost
@@ -975,4 +976,39 @@
975976
)
976977
)
977978
)
979+
(func $untaken-brs-might-prevent-block-removal (type $5) (param $0 f32) (param $1 i32) (param $2 f32) (param $3 i32) (param $4 i32) (param $5 f64) (param $6 f32) (result i32)
980+
(block $label$0 (result i32)
981+
(block $label$1
982+
(br_if $label$1
983+
(i32.const 607395945)
984+
)
985+
(br_if $label$1
986+
(unreachable.load16_s offset=3 align=1
987+
(select
988+
(call $untaken-brs-might-prevent-block-removal
989+
(f32.const 1.4904844647389837e-07)
990+
(br_if $label$0
991+
(i32.store16 offset=4 align=1
992+
(i32.const 1900641)
993+
(br $label$0
994+
(i32.const 1628075109)
995+
)
996+
)
997+
(i32.const 1764950569)
998+
)
999+
(f32.const 1.1910939690100655e-32)
1000+
(i32.const 1628057906)
1001+
(i32.const 859068982)
1002+
(f64.const 2.524518840347722e-258)
1003+
(f32.const -nan:0x40a63)
1004+
)
1005+
(i32.const 688529440)
1006+
(i32.const 1751478890)
1007+
)
1008+
)
1009+
)
1010+
)
1011+
(i32.const 1935947830)
1012+
)
1013+
)
9781014
)

test/passes/remove-unused-brs.wast

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,5 +866,40 @@
866866
)
867867
)
868868
)
869+
(func $untaken-brs-might-prevent-block-removal (param $0 f32) (param $1 i32) (param $2 f32) (param $3 i32) (param $4 i32) (param $5 f64) (param $6 f32) (result i32)
870+
(block $label$0 (result i32)
871+
(block $label$1 ;; this block has no taken brs, but we can't remove it without removing them first
872+
(br_if $label$1
873+
(i32.const 607395945)
874+
)
875+
(br_if $label$1
876+
(i32.load16_s offset=3 align=1
877+
(select
878+
(call $untaken-brs-might-prevent-block-removal
879+
(f32.const 1.4904844647389837e-07)
880+
(br_if $label$0
881+
(i32.store16 offset=4 align=1
882+
(i32.const 1900641)
883+
(br $label$0
884+
(i32.const 1628075109)
885+
)
886+
)
887+
(i32.const 1764950569)
888+
)
889+
(f32.const 1.1910939690100655e-32)
890+
(i32.const 1628057906)
891+
(i32.const 859068982)
892+
(f64.const 2.524518840347722e-258)
893+
(f32.const -nan:0x40a63)
894+
)
895+
(i32.const 688529440)
896+
(i32.const 1751478890)
897+
)
898+
)
899+
)
900+
)
901+
(i32.const 1935947830)
902+
)
903+
)
869904
)
870905

0 commit comments

Comments
 (0)