Skip to content

Commit 6303389

Browse files
committed
merge blocks in remove-unused-names only when their type is identical, so branches to them are interchangeable
1 parent 5b9fff4 commit 6303389

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/passes/RemoveUnusedNames.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct RemoveUnusedNames : public WalkerPass<PostWalker<RemoveUnusedNames>> {
5757
void visitBlock(Block *curr) {
5858
if (curr->name.is() && curr->list.size() == 1) {
5959
auto* child = curr->list[0]->dynCast<Block>();
60-
if (child && child->name.is()) {
60+
if (child && child->name.is() && child->type == curr->type) {
6161
// we have just one child, this block, so breaking out of it goes to the same place as breaking out of us, we just need one name (and block)
6262
auto& branches = branchesSeen[curr->name];
6363
for (auto* branch : branches) {

test/passes/remove-unused-names.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(module
22
(type $0 (func (param i32) (result i32)))
33
(type $1 (func))
4+
(type $2 (func (result i32)))
45
(memory $0 256 256)
56
(func $b0 (type $0) (param $i1 i32) (result i32)
67
(i32.const 0)
@@ -63,4 +64,20 @@
6364
)
6465
)
6566
)
67+
(func $merge-typed-with-unreachable-child (type $2) (result i32)
68+
(local $0 f32)
69+
(block $label$0 (result i32)
70+
(block $label$1
71+
(br_if $label$1
72+
(i32.const 0)
73+
(br_if $label$0
74+
(i32.const 0)
75+
(br $label$0
76+
(i32.const 0)
77+
)
78+
)
79+
)
80+
)
81+
)
82+
)
6683
)

test/passes/remove-unused-names.wast

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@
7777
)
7878
)
7979
)
80+
(func $merge-typed-with-unreachable-child (result i32)
81+
(local $0 f32)
82+
(block $label$0 (result i32)
83+
(block $label$1
84+
(br_if $label$1
85+
(i32.const 0)
86+
(br_if $label$0
87+
(i32.const 0)
88+
(br $label$0
89+
(i32.const 0)
90+
)
91+
)
92+
)
93+
)
94+
)
95+
)
8096
)

0 commit comments

Comments
 (0)