Skip to content

Commit f84fa96

Browse files
authored
when we re-finalize a function body, we may have changed it from unreachable to none. that is bad if the function has a return value, as unreachable was ok but none is not. in that case, we must add an unreachable (#1193)
1 parent db66e64 commit f84fa96

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

src/ast_utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ struct ReFinalize : public WalkerPass<PostWalker<ReFinalize>> {
176176
void visitNop(Nop *curr) { curr->finalize(); }
177177
void visitUnreachable(Unreachable *curr) { curr->finalize(); }
178178

179+
void visitFunction(Function* curr) {
180+
// we may have changed the body from unreachable to none, which might be bad
181+
// if the function has a return value
182+
if (curr->result != none && curr->body->type == none) {
183+
Builder builder(*getModule());
184+
curr->body = builder.blockify(curr->body, builder.makeUnreachable());
185+
}
186+
}
187+
179188
WasmType getValueType(Expression* value) {
180189
return value ? value->type : none;
181190
}

test/passes/remove-unused-brs.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,4 +1121,20 @@
11211121
)
11221122
)
11231123
)
1124+
(func $unreachable-return-loop-value (type $7) (result i64)
1125+
(loop $loop
1126+
(br_if $loop
1127+
(i32.eqz
1128+
(i32.const 1)
1129+
)
1130+
)
1131+
(block $block
1132+
(br_if $block
1133+
(br $loop)
1134+
)
1135+
(br $loop)
1136+
)
1137+
)
1138+
(unreachable)
1139+
)
11241140
)

test/passes/remove-unused-brs.wast

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,5 +1006,19 @@
10061006
)
10071007
)
10081008
)
1009+
(func $unreachable-return-loop-value (result i64)
1010+
(loop $loop
1011+
(if
1012+
(i32.const 1)
1013+
(block $block
1014+
(br_if $block
1015+
(br $loop)
1016+
)
1017+
(br $loop)
1018+
)
1019+
)
1020+
(br $loop) ;; we 100% go back to the loop top, the loop is never exited. but opts move code around so that is not obvious anymore, and the loop becomes a nop, but the func has a return value
1021+
)
1022+
)
10091023
)
10101024

0 commit comments

Comments
 (0)