Skip to content

Commit da0c455

Browse files
authored
Flatten tee (#1296)
* flatten tee_local in flatten, as it leads to more-optimizable code (tee_local, when nested, can introduce side effects in bad places). * also fix some test stuff from recent merges
1 parent dcdaa5d commit da0c455

File tree

7 files changed

+110
-42
lines changed

7 files changed

+110
-42
lines changed

src/passes/Flatten.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
// anything else is written to a local earlier.
4747
// 2. Disallow block, loop, and if return values, i.e., do not use
4848
// control flow to pass around values.
49+
// 3. Disallow tee_local, setting a local is always done in a set_local
50+
// on a non-nested-expression location.
4951
//
5052

5153
#include <wasm.h>
@@ -185,7 +187,19 @@ struct Flatten : public WalkerPass<ExpressionStackWalker<Flatten, UnifiedExpress
185187
ourPreludes.swap(iter->second);
186188
}
187189
// special handling
188-
if (auto* br = curr->dynCast<Break>()) {
190+
if (auto* set = curr->dynCast<SetLocal>()) {
191+
if (set->isTee()) {
192+
// we disallow tee_local
193+
if (set->value->type == unreachable) {
194+
replaceCurrent(set->value); // trivial, no set happens
195+
} else {
196+
// use a set in a prelude + a get
197+
set->setTee(false);
198+
ourPreludes.push_back(set);
199+
replaceCurrent(builder.makeGetLocal(set->index, set->value->type));
200+
}
201+
}
202+
} else if (auto* br = curr->dynCast<Break>()) {
189203
if (br->value) {
190204
auto type = br->value->type;
191205
if (isConcreteWasmType(type)) {

test/binaryen.js/kitchen-sink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function test_core() {
214214
console.log("getExpressionType=" + Binaryen.getExpressionType(valueList[3]));
215215
console.log(Binaryen.emitText(valueList[3])); // test printing a standalone expression
216216
console.log(Binaryen.getConstValueI32(module.i32.const(5)));
217-
console.log(Binaryen.getConstValueI64(module.i64.const(6, 7)));
217+
console.log(JSON.stringify(Binaryen.getConstValueI64(module.i64.const(6, 7))));
218218
console.log(Binaryen.getConstValueF32(module.f32.const(8.5)));
219219
console.log(Binaryen.getConstValueF64(module.f64.const(9.5)));
220220

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ getExpressionType=3
3838
)
3939

4040
5
41-
{ low: 6, high: 7 }
41+
{"low":6,"high":7}
4242
8.5
4343
9.5
4444
(module
@@ -1403,7 +1403,7 @@ getExpressionType=3
14031403
expressions[248] = BinaryenConst(the_module, BinaryenLiteralInt64(30064771078));
14041404
BinaryenConstGetValueI64Low(expressions[248]);
14051405
BinaryenConstGetValueI64High(expressions[248]);
1406-
{ low: 6, high: 7 }
1406+
{"low":6,"high":7}
14071407
expressions[249] = BinaryenConst(the_module, BinaryenLiteralFloat32(8.5));
14081408
BinaryenConstGetValueF32(expressions[249]);
14091409
8.5

test/passes/flatten.txt

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@
112112
(local $6 i32)
113113
(local $7 i32)
114114
(block $block
115+
(set_local $x
116+
(i32.const 0)
117+
)
115118
(set_local $1
116-
(tee_local $x
117-
(i32.const 0)
118-
)
119+
(get_local $x)
119120
)
120121
(set_local $2
121122
(get_local $1)
@@ -125,10 +126,11 @@
125126
(get_local $2)
126127
)
127128
(block $block1
129+
(set_local $x
130+
(i32.const 1)
131+
)
128132
(set_local $4
129-
(tee_local $x
130-
(i32.const 1)
131-
)
133+
(get_local $x)
132134
)
133135
(set_local $5
134136
(get_local $4)
@@ -160,10 +162,11 @@
160162
(local $9 i32)
161163
(block $block
162164
(block $block2
165+
(set_local $x
166+
(i32.const 0)
167+
)
163168
(set_local $1
164-
(tee_local $x
165-
(i32.const 0)
166-
)
169+
(get_local $x)
167170
)
168171
(set_local $2
169172
(get_local $1)
@@ -173,10 +176,11 @@
173176
(get_local $2)
174177
)
175178
(block $block3
179+
(set_local $x
180+
(i32.const 1)
181+
)
176182
(set_local $4
177-
(tee_local $x
178-
(i32.const 1)
179-
)
183+
(get_local $x)
180184
)
181185
(set_local $5
182186
(get_local $4)
@@ -240,10 +244,11 @@
240244
)
241245
(br $outer)
242246
(unreachable)
247+
(set_local $x
248+
(i32.const 3)
249+
)
243250
(set_local $5
244-
(tee_local $x
245-
(i32.const 3)
246-
)
251+
(get_local $x)
247252
)
248253
(set_local $6
249254
(get_local $5)
@@ -344,10 +349,11 @@
344349
(get_local $2)
345350
)
346351
(nop)
352+
(set_local $x
353+
(i32.const 2)
354+
)
347355
(set_local $3
348-
(tee_local $x
349-
(i32.const 2)
350-
)
356+
(get_local $x)
351357
)
352358
(set_local $1
353359
(get_local $3)
@@ -362,10 +368,11 @@
362368
(get_local $4)
363369
)
364370
(nop)
371+
(set_local $x
372+
(i32.const 5)
373+
)
365374
(set_local $5
366-
(tee_local $x
367-
(i32.const 5)
368-
)
375+
(get_local $x)
369376
)
370377
(set_local $1
371378
(i32.const 4)
@@ -380,15 +387,17 @@
380387
(get_local $6)
381388
)
382389
(nop)
390+
(set_local $x
391+
(i32.const 6)
392+
)
383393
(set_local $7
384-
(tee_local $x
385-
(i32.const 6)
386-
)
394+
(get_local $x)
395+
)
396+
(set_local $x
397+
(i32.const 7)
387398
)
388399
(set_local $8
389-
(tee_local $x
390-
(i32.const 7)
391-
)
400+
(get_local $x)
392401
)
393402
(set_local $1
394403
(get_local $7)
@@ -556,10 +565,11 @@
556565
(local $6 i32)
557566
(local $7 i32)
558567
(block $label$1
568+
(set_local $x
569+
(i32.const 1)
570+
)
559571
(set_local $1
560-
(tee_local $x
561-
(i32.const 1)
562-
)
572+
(get_local $x)
563573
)
564574
(block $label$2
565575
(set_local $x
@@ -612,10 +622,11 @@
612622
(set_local $1
613623
(get_local $var$0)
614624
)
625+
(set_local $var$0
626+
(f32.const -137438953472)
627+
)
615628
(set_local $2
616-
(tee_local $var$0
617-
(f32.const -137438953472)
618-
)
629+
(get_local $var$0)
619630
)
620631
(set_local $3
621632
(get_local $var$0)
@@ -1128,9 +1139,7 @@
11281139
(i32.const 11)
11291140
(block
11301141
(unreachable)
1131-
(tee_local $x
1132-
(unreachable)
1133-
)
1142+
(unreachable)
11341143
(unreachable)
11351144
)
11361145
)
@@ -2399,4 +2408,44 @@
23992408
(get_local $9)
24002409
)
24012410
)
2411+
(func $tees (; 41 ;) (type $ii) (param $x i32) (param $y i32)
2412+
(local $2 i32)
2413+
(local $3 i32)
2414+
(local $4 i32)
2415+
(block
2416+
(set_local $x
2417+
(i32.const 1)
2418+
)
2419+
(set_local $2
2420+
(get_local $x)
2421+
)
2422+
(drop
2423+
(get_local $2)
2424+
)
2425+
(nop)
2426+
(unreachable)
2427+
(unreachable)
2428+
(drop
2429+
(unreachable)
2430+
)
2431+
(unreachable)
2432+
(set_local $x
2433+
(i32.const 2)
2434+
)
2435+
(set_local $3
2436+
(get_local $x)
2437+
)
2438+
(set_local $y
2439+
(get_local $3)
2440+
)
2441+
(set_local $4
2442+
(get_local $y)
2443+
)
2444+
(drop
2445+
(get_local $4)
2446+
)
2447+
(nop)
2448+
)
2449+
(unreachable)
2450+
)
24022451
)

test/passes/flatten.wast

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,9 @@
10111011
)
10121012
)
10131013
)
1014+
(func $tees (param $x i32) (param $y i32)
1015+
(drop (tee_local $x (i32.const 1)))
1016+
(drop (tee_local $x (unreachable)))
1017+
(drop (tee_local $y (tee_local $x (i32.const 2))))
1018+
)
10141019
)

test/passes/inlining.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
(memory $0 0)
226226
(func $0 (; 0 ;) (type $1)
227227
(block $__inlined_func$1
228-
(call_indirect $T
228+
(call_indirect (type $T)
229229
(if (result i32)
230230
(i32.const 0)
231231
(unreachable)

test/passes/inlining.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
(call $1)
153153
)
154154
(func $1
155-
(call_indirect $T
155+
(call_indirect (type $T)
156156
(if (result i32) ;; if copy must preserve the forced type
157157
(i32.const 0)
158158
(unreachable)

0 commit comments

Comments
 (0)