Skip to content

Commit a633783

Browse files
committed
work
1 parent 73ea915 commit a633783

5 files changed

Lines changed: 75 additions & 42 deletions

File tree

src/passes/DeadArgumentElimination.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ struct DAE : public Pass {
580580
const std::vector<Call*>& calls,
581581
Module* module,
582582
bool isExported) {
583+
if (isExported && !func->type.isOpen()) {
584+
// We must subtype the current type, so that imports of it work, but it is
585+
// closed.
586+
return false;
587+
}
588+
583589
auto lub = LUB::getResultsLUB(func, *module);
584590
if (!lub.noted()) {
585591
return false;
@@ -603,13 +609,20 @@ struct DAE : public Pass {
603609
}
604610
}
605611

606-
// We must explicitly subtype the old type.
607-
TypeBuilder builder(1);
608-
builder.setHeapType(0, Signature(func->getParams(), newType));
609-
builder.setSubType(0, func->type);
610-
auto result = builder.build();
611-
assert(!result.getError());
612-
func->type = (*result)[0];
612+
if (!isExported) {
613+
func->setResults(newType);
614+
} else {
615+
// We must explicitly subtype the old type.
616+
TypeBuilder builder(1);
617+
builder.setHeapType(0, Signature(func->getParams(), newType));
618+
builder.setSubType(0, func->type);
619+
// Make this subtype open like the super. This is not necessary, but might
620+
// allow more work later after other changes, in theory.
621+
builder.setOpen(0, true);
622+
auto result = builder.build();
623+
assert(!result.getError());
624+
func->type = (*result)[0];
625+
}
613626

614627
for (auto* call : calls) {
615628
if (call->type != Type::unreachable) {

test/lit/basic/extra-branch-values.wast

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
(import "env" "eqref" (global $eqref (mut eqref)))
2525

2626
;; CHECK: (import "env" "use-i32-any" (func $use-i32-any (type $15) (param i32 (ref any))))
27-
;; OPT_O: (import "env" "use-i32-any" (func $use-i32-any (type $14) (param i32 (ref any))))
27+
;; OPT_O: (import "env" "use-i32-any" (func $use-i32-any (type $15) (param i32 (ref any))))
2828
(import "env" "use-i32-any" (func $use-i32-any (param i32 (ref any))))
2929

3030
;; CHECK: (tag $e (type $7) (param i32))
31-
;; OPT_O: (tag $e (type $6) (param i32))
31+
;; OPT_O: (tag $e (type $5) (param i32))
3232
(tag $e (param i32))
3333
;; CHECK: (tag $e2 (type $7) (param i32))
34-
;; OPT_O: (tag $e2 (type $6) (param i32))
34+
;; OPT_O: (tag $e2 (type $5) (param i32))
3535
(tag $e2 (param i32))
3636

3737
;; CHECK: (func $br_on_null-one (type $8) (param $0 i32) (param $1 anyref) (result i32)
@@ -76,7 +76,7 @@
7676
;; CHECK-NEXT: (local.get $scratch_3)
7777
;; CHECK-NEXT: )
7878
;; CHECK-NEXT: )
79-
;; OPT_O: (func $br_on_null-one (type $7) (param $0 i32) (param $1 anyref) (result i32)
79+
;; OPT_O: (func $br_on_null-one (type $6) (param $0 i32) (param $1 anyref) (result i32)
8080
;; OPT_O-NEXT: (block $block (result i32)
8181
;; OPT_O-NEXT: (block $block0
8282
;; OPT_O-NEXT: (global.set $any
@@ -166,9 +166,9 @@
166166
;; CHECK-NEXT: (local.get $scratch_4)
167167
;; CHECK-NEXT: )
168168
;; CHECK-NEXT: )
169-
;; OPT_O: (func $br_on_null-two (type $15) (param $0 i32) (param $1 i64) (param $2 anyref) (result i32 i64)
169+
;; OPT_O: (func $br_on_null-two (type $16) (param $0 i32) (param $1 i64) (param $2 anyref) (result i32 i64)
170170
;; OPT_O-NEXT: (local $3 (tuple i32 i64))
171-
;; OPT_O-NEXT: (block $block (type $10) (result i32 i64)
171+
;; OPT_O-NEXT: (block $block (type $11) (result i32 i64)
172172
;; OPT_O-NEXT: (local.set $3
173173
;; OPT_O-NEXT: (tuple.make 2
174174
;; OPT_O-NEXT: (local.get $0)
@@ -254,7 +254,7 @@
254254
;; CHECK-NEXT: )
255255
;; CHECK-NEXT: )
256256
;; CHECK-NEXT: )
257-
;; OPT_O: (func $br_on_non_null-one (type $5) (param $0 i32) (param $1 anyref) (result i32 (ref any))
257+
;; OPT_O: (func $br_on_non_null-one (type $7) (param $0 i32) (param $1 anyref) (result i32 (ref any))
258258
;; OPT_O-NEXT: (block $block (type $1) (result i32 (ref any))
259259
;; OPT_O-NEXT: (tuple.make 2
260260
;; OPT_O-NEXT: (local.get $0)
@@ -549,8 +549,8 @@
549549
;; CHECK-NEXT: )
550550
;; CHECK-NEXT: )
551551
;; CHECK-NEXT: )
552-
;; OPT_O: (func $br_on_cast-two (type $16) (param $0 i32) (param $1 i64) (param $2 anyref) (result i32 i64 eqref)
553-
;; OPT_O-NEXT: (block $block (type $11) (result i32 i64 eqref)
552+
;; OPT_O: (func $br_on_cast-two (type $17) (param $0 i32) (param $1 i64) (param $2 anyref) (result i32 i64 eqref)
553+
;; OPT_O-NEXT: (block $block (type $12) (result i32 i64 eqref)
554554
;; OPT_O-NEXT: (tuple.make 3
555555
;; OPT_O-NEXT: (local.get $0)
556556
;; OPT_O-NEXT: (local.get $1)
@@ -643,7 +643,7 @@
643643
;; CHECK-NEXT: )
644644
;; CHECK-NEXT: )
645645
;; CHECK-NEXT: )
646-
;; OPT_O: (func $br_on_cast-nn (type $17) (param $0 i32) (param $1 (ref any)) (result i32 (ref eq))
646+
;; OPT_O: (func $br_on_cast-nn (type $18) (param $0 i32) (param $1 (ref any)) (result i32 (ref eq))
647647
;; OPT_O-NEXT: (block $block (type $2) (result i32 (ref eq))
648648
;; OPT_O-NEXT: (tuple.make 2
649649
;; OPT_O-NEXT: (local.get $0)
@@ -729,7 +729,7 @@
729729
;; CHECK-NEXT: )
730730
;; CHECK-NEXT: )
731731
;; CHECK-NEXT: )
732-
;; OPT_O: (func $br_on_cast-to-nn (type $18) (param $0 i32) (param $1 anyref) (result i32 (ref eq))
732+
;; OPT_O: (func $br_on_cast-to-nn (type $19) (param $0 i32) (param $1 anyref) (result i32 (ref eq))
733733
;; OPT_O-NEXT: (block $block (type $2) (result i32 (ref eq))
734734
;; OPT_O-NEXT: (tuple.make 2
735735
;; OPT_O-NEXT: (local.get $0)
@@ -815,7 +815,7 @@
815815
;; CHECK-NEXT: )
816816
;; CHECK-NEXT: )
817817
;; CHECK-NEXT: )
818-
;; OPT_O: (func $br_on_cast_fail-one (type $5) (param $0 i32) (param $1 anyref) (result i32 (ref any))
818+
;; OPT_O: (func $br_on_cast_fail-one (type $7) (param $0 i32) (param $1 anyref) (result i32 (ref any))
819819
;; OPT_O-NEXT: (block $block (type $1) (result i32 (ref any))
820820
;; OPT_O-NEXT: (tuple.make 2
821821
;; OPT_O-NEXT: (local.get $0)
@@ -1017,7 +1017,7 @@
10171017
;; CHECK-NEXT: )
10181018
;; CHECK-NEXT: )
10191019
;; CHECK-NEXT: )
1020-
;; OPT_O: (func $br_on_cast_fail-nn (type $19) (param $0 i32) (param $1 (ref any)) (result i32 (ref any))
1020+
;; OPT_O: (func $br_on_cast_fail-nn (type $20) (param $0 i32) (param $1 (ref any)) (result i32 (ref any))
10211021
;; OPT_O-NEXT: (block $block (type $1) (result i32 (ref any))
10221022
;; OPT_O-NEXT: (tuple.make 2
10231023
;; OPT_O-NEXT: (local.get $0)
@@ -1103,8 +1103,8 @@
11031103
;; CHECK-NEXT: )
11041104
;; CHECK-NEXT: )
11051105
;; CHECK-NEXT: )
1106-
;; OPT_O: (func $br_on_cast_fail-to-nn (type $20) (param $0 i32) (param $1 anyref) (result i32 anyref)
1107-
;; OPT_O-NEXT: (block $block (type $12) (result i32 anyref)
1106+
;; OPT_O: (func $br_on_cast_fail-to-nn (type $9) (param $0 i32) (param $1 anyref) (result i32 anyref)
1107+
;; OPT_O-NEXT: (block $block (type $13) (result i32 anyref)
11081108
;; OPT_O-NEXT: (tuple.make 2
11091109
;; OPT_O-NEXT: (local.get $0)
11101110
;; OPT_O-NEXT: (block $block0 (result anyref)
@@ -1187,7 +1187,7 @@
11871187
;; CHECK-NEXT: )
11881188
;; CHECK-NEXT: (local.get $scratch_7)
11891189
;; CHECK-NEXT: )
1190-
;; OPT_O: (func $unreachable-fallthrough (type $7) (param $0 i32) (param $1 anyref) (result i32)
1190+
;; OPT_O: (func $unreachable-fallthrough (type $6) (param $0 i32) (param $1 anyref) (result i32)
11911191
;; OPT_O-NEXT: (drop
11921192
;; OPT_O-NEXT: (block $l0 (result (ref any))
11931193
;; OPT_O-NEXT: (br_on_non_null $l0
@@ -1896,7 +1896,7 @@
18961896
;; CHECK-NEXT: )
18971897
;; CHECK-NEXT: )
18981898
;; OPT_O: (func $with-block-param (type $25) (param $0 i64) (param $1 anyref) (result i64 eqref)
1899-
;; OPT_O-NEXT: (block $block (type $13) (result i64 eqref)
1899+
;; OPT_O-NEXT: (block $block (type $14) (result i64 eqref)
19001900
;; OPT_O-NEXT: (tuple.make 2
19011901
;; OPT_O-NEXT: (local.get $0)
19021902
;; OPT_O-NEXT: (block $block0 (result eqref)
@@ -2098,7 +2098,7 @@
20982098
;; CHECK-NEXT: )
20992099
;; CHECK-NEXT: )
21002100
;; CHECK-NEXT: )
2101-
;; OPT_O: (func $loop-results (type $5) (param $0 i32) (param $1 anyref) (result i32 (ref any))
2101+
;; OPT_O: (func $loop-results (type $9) (param $0 i32) (param $1 anyref) (result i32 anyref)
21022102
;; OPT_O-NEXT: (local $2 (tuple i32 anyref))
21032103
;; OPT_O-NEXT: (local $3 eqref)
21042104
;; OPT_O-NEXT: (local.set $2
@@ -2215,7 +2215,7 @@
22152215
;; CHECK-NEXT: )
22162216
;; CHECK-NEXT: )
22172217
;; CHECK-NEXT: )
2218-
;; OPT_O: (func $if (type $9) (param $0 i32) (param $1 i32) (param $2 anyref) (result i32 eqref)
2218+
;; OPT_O: (func $if (type $10) (param $0 i32) (param $1 i32) (param $2 anyref) (result i32 eqref)
22192219
;; OPT_O-NEXT: (block $label (type $0) (result i32 eqref)
22202220
;; OPT_O-NEXT: (tuple.make 2
22212221
;; OPT_O-NEXT: (local.get $1)
@@ -2327,7 +2327,7 @@
23272327
;; CHECK-NEXT: )
23282328
;; CHECK-NEXT: )
23292329
;; CHECK-NEXT: )
2330-
;; OPT_O: (func $else (type $9) (param $0 i32) (param $1 i32) (param $2 anyref) (result i32 eqref)
2330+
;; OPT_O: (func $else (type $10) (param $0 i32) (param $1 i32) (param $2 anyref) (result i32 eqref)
23312331
;; OPT_O-NEXT: (block $label (type $0) (result i32 eqref)
23322332
;; OPT_O-NEXT: (tuple.make 2
23332333
;; OPT_O-NEXT: (local.get $1)

test/lit/passes/dae-gc-no-cd.wast

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@
44
;; We cannot refine to an exact type in an export, as CD is disabled.
55
(module
66
;; CHECK: (type $A (struct))
7+
8+
;; CHECK: (type $func (sub (func (result anyref))))
9+
(type $func (sub (func (result anyref))))
10+
11+
;; CHECK: (type $func2 (sub (func (result anyref i32))))
12+
(type $func2 (sub (func (result anyref i32))))
13+
714
(type $A (struct))
815

9-
;; CHECK: (func $export (type $1) (result (ref $A))
16+
;; CHECK: (func $export (type $3) (result (ref $A))
1017
;; CHECK-NEXT: (struct.new_default $A)
1118
;; CHECK-NEXT: )
12-
(func $export (export "export") (result anyref)
19+
(func $export (export "export") (type $func) (result anyref)
1320
;; We can refine to (ref $A), but not an exact one.
1421
(struct.new $A)
1522
)
1623

17-
;; CHECK: (func $export-tuple (type $2) (result (ref $A) i32)
24+
;; CHECK: (func $export-tuple (type $4) (result (ref $A) i32)
1825
;; CHECK-NEXT: (tuple.make 2
1926
;; CHECK-NEXT: (struct.new_default $A)
2027
;; CHECK-NEXT: (i32.const 42)
2128
;; CHECK-NEXT: )
2229
;; CHECK-NEXT: )
23-
(func $export-tuple (export "export-tuple") (result anyref i32)
30+
(func $export-tuple (export "export-tuple") (type $func2) (result anyref i32)
2431
;; Ditto, but the ref is in a tuple.
2532
(tuple.make 2
2633
(struct.new $A)

test/lit/passes/dae-gc.wast

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,21 @@
198198

199199
;; Function results can be refined, even if exported.
200200
(module
201-
;; CHECK: (func $export (type $0) (param $x anyref) (result (ref any))
201+
;; CHECK: (type $func (sub (func (param anyref) (result anyref))))
202+
(type $func (sub (func (param anyref) (result anyref))))
203+
204+
;; CHECK: (func $export (type $1) (param $x anyref) (result (ref any))
202205
;; CHECK-NEXT: (ref.as_non_null
203206
;; CHECK-NEXT: (local.get $x)
204207
;; CHECK-NEXT: )
205208
;; CHECK-NEXT: )
206-
(func $export (export "export") (param $x anyref) (result anyref)
209+
(func $export (export "export") (type $func) (param $x anyref) (result anyref)
207210
(ref.as_non_null
208211
(local.get $x)
209212
)
210213
)
211214

212-
;; CHECK: (func $caller (type $1)
215+
;; CHECK: (func $caller (type $2)
213216
;; CHECK-NEXT: (drop
214217
;; CHECK-NEXT: (call $export
215218
;; CHECK-NEXT: (ref.as_non_null
@@ -234,12 +237,15 @@
234237

235238
;; An export's results can be refined even without any calls to it.
236239
(module
237-
;; CHECK: (func $export (type $0) (param $x anyref) (result (ref any))
240+
;; CHECK: (type $func (sub (func (param anyref) (result anyref))))
241+
(type $func (sub (func (param anyref) (result anyref))))
242+
243+
;; CHECK: (func $export (type $1) (param $x anyref) (result (ref any))
238244
;; CHECK-NEXT: (ref.as_non_null
239245
;; CHECK-NEXT: (local.get $x)
240246
;; CHECK-NEXT: )
241247
;; CHECK-NEXT: )
242-
(func $export (export "export") (param $x anyref) (result anyref)
248+
(func $export (export "export") (type $func) (param $x anyref) (result anyref)
243249
(ref.as_non_null
244250
(local.get $x)
245251
)
@@ -248,15 +254,18 @@
248254

249255
;; A ref.func stops an export's results from being be refined.
250256
(module
251-
;; CHECK: (func $export (type $0) (param $x anyref) (result anyref)
257+
;; CHECK: (type $func (sub (func (param anyref) (result anyref))))
258+
(type $func (sub (func (param anyref) (result anyref))))
259+
260+
;; CHECK: (func $export (type $func) (param $x anyref) (result anyref)
252261
;; CHECK-NEXT: (drop
253262
;; CHECK-NEXT: (ref.func $export)
254263
;; CHECK-NEXT: )
255264
;; CHECK-NEXT: (ref.as_non_null
256265
;; CHECK-NEXT: (local.get $x)
257266
;; CHECK-NEXT: )
258267
;; CHECK-NEXT: )
259-
(func $export (export "export") (param $x anyref) (result anyref)
268+
(func $export (export "export") (type $func) (param $x anyref) (result anyref)
260269
(drop
261270
(ref.func $export)
262271
)
@@ -269,12 +278,16 @@
269278
;; We can refine to an exact type in an export, as CD is enabled.
270279
(module
271280
;; CHECK: (type $A (struct))
281+
282+
;; CHECK: (type $func (sub (func (result anyref))))
283+
(type $func (sub (func (result anyref))))
284+
272285
(type $A (struct))
273286

274-
;; CHECK: (func $export (type $1) (result (ref (exact $A)))
287+
;; CHECK: (func $export (type $2) (result (ref (exact $A)))
275288
;; CHECK-NEXT: (struct.new_default $A)
276289
;; CHECK-NEXT: )
277-
(func $export (export "export") (result anyref)
290+
(func $export (export "export") (type $func) (result anyref)
278291
(struct.new $A)
279292
)
280293
)

test/lit/passes/dae_all-features.wast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@
468468
(module
469469
;; CHECK: (type $0 (func (param funcref i32 f64) (result i64)))
470470

471-
;; CHECK: (type $1 (func (param f32) (result (ref (exact $0)))))
471+
;; CHECK: (type $1 (func (param f32) (result funcref)))
472472

473473
;; CHECK: (elem declare func $0)
474474

@@ -482,7 +482,7 @@
482482
(nop)
483483
(unreachable)
484484
)
485-
;; CHECK: (func $export (type $1) (param $0 f32) (result (ref (exact $0)))
485+
;; CHECK: (func $export (type $1) (param $0 f32) (result funcref)
486486
;; CHECK-NEXT: (ref.func $0)
487487
;; CHECK-NEXT: )
488488
(func $export (export "export") (param $0 f32) (result funcref)

0 commit comments

Comments
 (0)