Skip to content

Commit e5ebae7

Browse files
committed
fix coalesce-locals handling of set/tee local of an unreachable; we still need the value, as it may do things
1 parent 75162c4 commit e5ebae7

4 files changed

Lines changed: 61 additions & 14 deletions

File tree

src/passes/CoalesceLocals.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,13 @@ struct CoalesceLocals : public WalkerPass<CFGWalker<CoalesceLocals, Visitor<Coal
168168

169169
static void doVisitSetLocal(CoalesceLocals* self, Expression** currp) {
170170
auto* curr = (*currp)->cast<SetLocal>();
171-
// if in unreachable code, ignore
171+
// if in unreachable code, we don't need the tee (but might need the value, if it has side effects)
172172
if (!self->currBasicBlock) {
173-
*currp = Builder(*self->getModule()).replaceWithIdenticalType(curr);
173+
if (curr->isTee()) {
174+
*currp = curr->value;
175+
} else {
176+
*currp = Builder(*self->getModule()).makeDrop(curr->value);
177+
}
174178
return;
175179
}
176180
self->currBasicBlock->contents.actions.emplace_back(Action::Set, curr->index, currp);

test/passes/coalesce-locals-learning.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@
108108
)
109109
(block $block
110110
(br $block)
111-
(nop)
112111
(drop
113112
(i32.const 0)
114113
)
115-
(nop)
114+
(drop
115+
(i32.const 0)
116+
)
117+
(drop
118+
(i32.const -1)
119+
)
116120
)
117121
(drop
118122
(get_local $0)

test/passes/coalesce-locals.txt

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
(type $4 (func (param i32)))
77
(type $FUNCSIG$i (func (result i32)))
88
(type $FUNCSIG$vi (func (param i32)))
9+
(type $7 (func (param i32) (result i32)))
910
(import "env" "_emscripten_autodebug_i32" (func $_emscripten_autodebug_i32 (param i32 i32) (result i32)))
1011
(import "env" "get" (func $get (result i32)))
1112
(import "env" "set" (func $set (param i32)))
@@ -112,11 +113,15 @@
112113
)
113114
(block $block
114115
(br $block)
115-
(nop)
116116
(drop
117117
(i32.const 0)
118118
)
119-
(nop)
119+
(drop
120+
(i32.const 0)
121+
)
122+
(drop
123+
(i32.const -1)
124+
)
120125
)
121126
(drop
122127
(get_local $0)
@@ -902,37 +907,53 @@
902907
(local $0 i32)
903908
(block $x
904909
(return)
905-
(nop)
910+
(drop
911+
(i32.const 1)
912+
)
913+
(drop
914+
(i32.const 0)
915+
)
906916
(drop
907917
(i32.const 0)
908918
)
909-
(nop)
910919
)
911920
(block $y
912921
(unreachable)
913-
(nop)
922+
(drop
923+
(i32.const 1)
924+
)
925+
(drop
926+
(i32.const 0)
927+
)
914928
(drop
915929
(i32.const 0)
916930
)
917-
(nop)
918931
)
919932
(block $z
920933
(br $z)
921-
(nop)
934+
(drop
935+
(i32.const 1)
936+
)
937+
(drop
938+
(i32.const 0)
939+
)
922940
(drop
923941
(i32.const 0)
924942
)
925-
(nop)
926943
)
927944
(block $z14
928945
(br_table $z14 $z14
929946
(i32.const 100)
930947
)
931-
(nop)
948+
(drop
949+
(i32.const 1)
950+
)
951+
(drop
952+
(i32.const 0)
953+
)
932954
(drop
933955
(i32.const 0)
934956
)
935-
(nop)
936957
)
937958
)
938959
(func $nop-in-unreachable (type $2)
@@ -1083,4 +1104,12 @@
10831104
(br $top)
10841105
)
10851106
)
1107+
(func $tee_br (type $7) (param $0 i32) (result i32)
1108+
(block $b
1109+
(return
1110+
(br $b)
1111+
)
1112+
)
1113+
(i32.const 1)
1114+
)
10861115
)

test/passes/coalesce-locals.wast

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,4 +1075,14 @@
10751075
(br $top)
10761076
)
10771077
)
1078+
(func $tee_br (param $x i32) (result i32)
1079+
(block $b
1080+
(return
1081+
(tee_local $x
1082+
(br $b)
1083+
)
1084+
)
1085+
)
1086+
(i32.const 1)
1087+
)
10781088
)

0 commit comments

Comments
 (0)