Skip to content

Commit 323e0b4

Browse files
committed
handle merging blocks with items after an unreachable, that if merged would be invalid. stop on the unreachable, it is easier and better
1 parent 7231e61 commit 323e0b4

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

src/passes/MergeBlocks.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,18 @@ static void optimizeBlock(Block* curr, Module* module, PassOptions& passOptions)
214214
}
215215
if (!child) continue;
216216
if (child->name.is()) continue; // named blocks can have breaks to them (and certainly do, if we ran RemoveUnusedNames and RemoveUnusedBrs)
217-
if (child->type == unreachable) {
218-
// an unreachable block can have a concrete final element (which is never reached)
219-
if (!child->list.empty()) {
220-
if (isConcreteWasmType(child->list.back()->type)) {
221-
// just remove it
222-
child->list.pop_back();
223-
}
224-
}
225-
}
226217
ExpressionList merged(module->allocator);
227218
for (size_t j = 0; j < i; j++) {
228219
merged.push_back(curr->list[j]);
229220
}
230221
for (auto item : child->list) {
231222
merged.push_back(item);
223+
if (item->type == unreachable) {
224+
// we don't need anything else from the child,
225+
// and it is always valid to end on an unreachable,
226+
// so stop there (otherwise, we need to be careful)
227+
break;
228+
}
232229
}
233230
for (size_t j = i + 1; j < curr->list.size(); j++) {
234231
merged.push_back(curr->list[j]);

test/passes/remove-unused-names_merge-blocks.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,4 +906,9 @@
906906
)
907907
)
908908
)
909+
(func $merging-with-unreachable-in-middle (type $4) (result i32)
910+
(return
911+
(i32.const 21536)
912+
)
913+
)
909914
)

test/passes/remove-unused-names_merge-blocks.wast

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,4 +1068,18 @@
10681068
)
10691069
)
10701070
)
1071+
(func $merging-with-unreachable-in-middle (result i32)
1072+
(block $label$1 (result i32)
1073+
(block
1074+
(return
1075+
(i32.const 21536)
1076+
)
1077+
(block $label$15
1078+
(br $label$15)
1079+
)
1080+
(i32.const 19299)
1081+
)
1082+
)
1083+
)
10711084
)
1085+

0 commit comments

Comments
 (0)