Skip to content

Commit 9792a40

Browse files
committed
when removing an if-copy in coalesce-locals, if it's a tee, we do still need the get in that arm. only when it is not a tee can we remove that arm and make the if-else into an if
1 parent 00dd3b9 commit 9792a40

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/passes/CoalesceLocals.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,13 @@ void CoalesceLocals::pickIndices(std::vector<Index>& indices) {
597597
// Remove a copy from a set of an if, where one if arm is a get of the same set
598598
static void removeIfCopy(Expression** origin, SetLocal* set, If* iff, Expression*& copy, Expression*& other, Module* module) {
599599
// replace the origin with the if, and sink the set into the other non-copying arm
600+
bool tee = set->isTee();
600601
*origin = iff;
601602
set->value = other;
602603
set->finalize();
603604
other = set;
604-
if (!isConcreteWasmType(set->type)) {
605+
// if this is not a tee, then we can get rid of the copy in that arm
606+
if (!tee) {
605607
// we don't need the copy at all
606608
copy = nullptr;
607609
if (!iff->ifTrue) {

test/passes/coalesce-locals.txt

+31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
(type $FUNCSIG$i (func (result i32)))
88
(type $FUNCSIG$vi (func (param i32)))
99
(type $7 (func (param i32) (result i32)))
10+
(type $8 (func (param f64 i32) (result i64)))
1011
(import "env" "_emscripten_autodebug_i32" (func $_emscripten_autodebug_i32 (param i32 i32) (result i32)))
1112
(import "env" "get" (func $get (result i32)))
1213
(import "env" "set" (func $set (param i32)))
@@ -1122,4 +1123,34 @@
11221123
)
11231124
)
11241125
)
1126+
(func $tee_if_with_unreachable_else (type $8) (param $0 f64) (param $1 i32) (result i64)
1127+
(call $tee_if_with_unreachable_else
1128+
(if (result f64)
1129+
(get_local $1)
1130+
(get_local $0)
1131+
(tee_local $0
1132+
(unreachable)
1133+
)
1134+
)
1135+
(f64.lt
1136+
(f64.const -128)
1137+
(get_local $0)
1138+
)
1139+
)
1140+
)
1141+
(func $tee_if_with_unreachable_true (type $8) (param $0 f64) (param $1 i32) (result i64)
1142+
(call $tee_if_with_unreachable_else
1143+
(if (result f64)
1144+
(get_local $1)
1145+
(tee_local $0
1146+
(unreachable)
1147+
)
1148+
(get_local $0)
1149+
)
1150+
(f64.lt
1151+
(f64.const -128)
1152+
(get_local $0)
1153+
)
1154+
)
1155+
)
11251156
)

test/passes/coalesce-locals.wast

+30
Original file line numberDiff line numberDiff line change
@@ -1097,4 +1097,34 @@
10971097
)
10981098
)
10991099
)
1100+
(func $tee_if_with_unreachable_else (param $0 f64) (param $1 i32) (result i64)
1101+
(call $tee_if_with_unreachable_else
1102+
(tee_local $0
1103+
(if (result f64)
1104+
(get_local $1)
1105+
(get_local $0)
1106+
(unreachable)
1107+
)
1108+
)
1109+
(f64.lt
1110+
(f64.const -128)
1111+
(get_local $0)
1112+
)
1113+
)
1114+
)
1115+
(func $tee_if_with_unreachable_true (param $0 f64) (param $1 i32) (result i64)
1116+
(call $tee_if_with_unreachable_else
1117+
(tee_local $0
1118+
(if (result f64)
1119+
(get_local $1)
1120+
(unreachable)
1121+
(get_local $0)
1122+
)
1123+
)
1124+
(f64.lt
1125+
(f64.const -128)
1126+
(get_local $0)
1127+
)
1128+
)
1129+
)
11001130
)

0 commit comments

Comments
 (0)