Skip to content

Commit 89b1183

Browse files
committed
finalize in remove-unused-brs when we conditionalize a br - we may have put an if at the end of a block, and if the block if is typed, then so now must be the if (it must have both arms of unreachable type, so this is ok to do)
1 parent 8bdfcf0 commit 89b1183

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/passes/RemoveUnusedBrs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
219219
// we need the ifTrue to break, so it cannot reach the code we want to move
220220
if (ExpressionAnalyzer::obviouslyDoesNotFlowOut(iff->ifTrue)) {
221221
iff->ifFalse = builder.stealSlice(block, i + 1, list.size());
222+
block->finalize(block->type);
222223
return true;
223224
}
224225
} else {
@@ -227,9 +228,11 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
227228
assert(!isConcreteWasmType(iff->type)); // can't be, since in the middle of a block
228229
if (ExpressionAnalyzer::obviouslyDoesNotFlowOut(iff->ifTrue)) {
229230
iff->ifFalse = builder.blockifyMerge(iff->ifFalse, builder.stealSlice(block, i + 1, list.size()));
231+
block->finalize(block->type);
230232
return true;
231233
} else if (ExpressionAnalyzer::obviouslyDoesNotFlowOut(iff->ifFalse)) {
232234
iff->ifTrue = builder.blockifyMerge(iff->ifTrue, builder.stealSlice(block, i + 1, list.size()));
235+
block->finalize(block->type);
233236
return true;
234237
}
235238
}
@@ -256,6 +259,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
256259
// note that we could drop the last element here, it is a br we know for sure is removable,
257260
// but telling stealSlice to steal all to the end is more efficient, it can just truncate.
258261
list[i] = builder.makeIf(brIf->condition, builder.makeBreak(brIf->name), builder.stealSlice(block, i + 1, list.size()));
262+
block->finalize(block->type);
259263
return true;
260264
}
261265
}

test/passes/remove-unused-brs.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,4 +937,22 @@
937937
)
938938
)
939939
)
940+
(func $loop-if (type $2) (result i32)
941+
(block $outer i32
942+
(loop $typed i32
943+
(if i32
944+
(i32.const 2)
945+
(block $block i32
946+
(drop
947+
(call $loop-if)
948+
)
949+
(br $outer
950+
(i32.const 0)
951+
)
952+
)
953+
(br $typed)
954+
)
955+
)
956+
)
957+
)
940958
)

test/passes/remove-unused-brs.wast

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,5 +831,21 @@
831831
)
832832
)
833833
)
834+
(func $loop-if (result i32)
835+
(block $outer i32
836+
(loop $typed i32
837+
;; we can move the br after us into our if-else, but that then means we are the final
838+
;; element in the block, and so must be typed
839+
(if
840+
(i32.const 2)
841+
(block
842+
(drop (call $loop-if))
843+
(br $outer (i32.const 0))
844+
)
845+
)
846+
(br $typed)
847+
)
848+
)
849+
)
834850
)
835851

0 commit comments

Comments
 (0)