Skip to content

Commit e17202e

Browse files
committed
properly handle merging of blocks with concrete unreachable elements in the middle
1 parent 323e0b4 commit e17202e

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/passes/MergeBlocks.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,20 @@ static void optimizeBlock(Block* curr, Module* module, PassOptions& passOptions)
220220
}
221221
for (auto item : child->list) {
222222
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-
}
229223
}
230224
for (size_t j = i + 1; j < curr->list.size(); j++) {
231225
merged.push_back(curr->list[j]);
232226
}
227+
// if we merged a concrete element in the middle, drop it
228+
if (!merged.empty()) {
229+
auto* last = merged.back();
230+
for (auto*& item : merged) {
231+
if (item != last && isConcreteWasmType(item->type)) {
232+
Builder builder(*module);
233+
item = builder.makeDrop(item);
234+
}
235+
}
236+
}
233237
curr->list.swap(merged);
234238
more = true;
235239
changed = true;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@
854854
)
855855
(func $concrete_finale_in_unreachable (type $5) (result f64)
856856
(unreachable)
857+
(drop
858+
(f64.const 6.322092475576799e-96)
859+
)
857860
(f64.const -1)
858861
)
859862
(func $dont-move-unreachable (type $3)
@@ -910,5 +913,19 @@
910913
(return
911914
(i32.const 21536)
912915
)
916+
(block $label$15
917+
(br $label$15)
918+
)
919+
(i32.const 19299)
920+
)
921+
(func $remove-br-after-unreachable (type $3)
922+
(block $label$9
923+
(drop
924+
(block
925+
(return)
926+
(br $label$9)
927+
)
928+
)
929+
)
913930
)
914931
)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,5 +1081,17 @@
10811081
)
10821082
)
10831083
)
1084+
(func $remove-br-after-unreachable
1085+
(block $label$9
1086+
(drop
1087+
(block
1088+
(block
1089+
(return)
1090+
(br $label$9) ;; removing this leads to the block becoming unreachable
1091+
)
1092+
)
1093+
)
1094+
)
1095+
)
10841096
)
10851097

0 commit comments

Comments
 (0)