1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
|
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: foreach %s %t wasm-opt --signature-pruning --closed-world -all -S -o - | filecheck %s
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func (param i32 f64))))
(type $sig (sub (func (param i32) (param i64) (param f32) (param f64))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $foo)
;; CHECK: (func $foo (type $sig) (param $0 i32) (param $1 f64)
;; CHECK-NEXT: (local $2 f32)
;; CHECK-NEXT: (local $3 i64)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (f64.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32) (param $i64 i64) (param $f32 f32) (param $f64 f64)
;; Use the first and last parameter. The middle parameters will be removed
;; both from the function and from $sig, and also in the calls below.
(i32.store
(i32.const 0)
(local.get $i32)
)
(f64.store
(i32.const 0)
(local.get $f64)
)
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (f64.const 3)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (i32.const 4)
;; CHECK-NEXT: (f64.const 7)
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $foo
(i32.const 0)
(i64.const 1)
(f32.const 2)
(f64.const 3)
)
(call_ref $sig
(i32.const 4)
(i64.const 5)
(f32.const 6)
(f64.const 7)
(ref.func $foo)
)
)
)
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func (param i64 f32))))
(type $sig (sub (func (param i32) (param i64) (param f32) (param f64))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $foo)
;; CHECK: (func $foo (type $sig) (param $0 i64) (param $1 f32)
;; CHECK-NEXT: (local $2 f64)
;; CHECK-NEXT: (local $3 i32)
;; CHECK-NEXT: (i64.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (f32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32) (param $i64 i64) (param $f32 f32) (param $f64 f64)
;; Use the middle two parameters.
(i64.store
(i32.const 0)
(local.get $i64)
)
(f32.store
(i32.const 0)
(local.get $f32)
)
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: (f32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (i64.const 5)
;; CHECK-NEXT: (f32.const 6)
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $foo
(i32.const 0)
(i64.const 1)
(f32.const 2)
(f64.const 3)
)
(call_ref $sig
(i32.const 4)
(i64.const 5)
(f32.const 6)
(f64.const 7)
(ref.func $foo)
)
)
)
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func (param i64 f32))))
(type $sig (sub (func (param i32) (param i64) (param f32) (param f64))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $foo)
;; CHECK: (func $foo (type $sig) (param $0 i64) (param $1 f32)
;; CHECK-NEXT: (local $2 f64)
;; CHECK-NEXT: (local $3 i32)
;; CHECK-NEXT: (i64.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (f32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32) (param $i64 i64) (param $f32 f32) (param $f64 f64)
;; Use the middle two parameters. The other two vanish.
(i64.store
(i32.const 0)
(local.get $i64)
)
(f32.store
(i32.const 0)
(local.get $f32)
)
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (call $caller)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: (f32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (i64.const 5)
;; CHECK-NEXT: (f32.const 6)
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
;; As above, but now one of the unused parameters has a side effect. We
;; move it to a local, which allows us to remove it (and also the last,
;; which is trivial).
(call $foo
(block (result i32)
(call $caller)
(i32.const 0)
)
(i64.const 1)
(f32.const 2)
(f64.const 3)
)
(call_ref $sig
(i32.const 4)
(i64.const 5)
(f32.const 6)
(f64.const 7)
(ref.func $foo)
)
)
)
;; As above, but with the effects on a call_ref. Once more, we can optimize
;; even with effects on a param, using locals.
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func (param i64 f32))))
(type $sig (sub (func (param i32) (param i64) (param f32) (param f64))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $foo)
;; CHECK: (func $foo (type $sig) (param $0 i64) (param $1 f32)
;; CHECK-NEXT: (local $2 f64)
;; CHECK-NEXT: (local $3 i32)
;; CHECK-NEXT: (i64.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (f32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32) (param $i64 i64) (param $f32 f32) (param $f64 f64)
(i64.store
(i32.const 0)
(local.get $i64)
)
(f32.store
(i32.const 0)
(local.get $f32)
)
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: (f32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (call $caller)
;; CHECK-NEXT: (i32.const 4)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (i64.const 5)
;; CHECK-NEXT: (f32.const 6)
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $foo
(i32.const 0)
(i64.const 1)
(f32.const 2)
(f64.const 3)
)
(call_ref $sig
(block (result i32)
(call $caller)
(i32.const 4)
)
(i64.const 5)
(f32.const 6)
(f64.const 7)
(ref.func $foo)
)
)
)
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func)))
(type $sig (sub (func (param i32) (param i64) (param f32) (param f64))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $foo)
;; CHECK: (func $foo (type $sig)
;; CHECK-NEXT: (local $0 f64)
;; CHECK-NEXT: (local $1 f32)
;; CHECK-NEXT: (local $2 i64)
;; CHECK-NEXT: (local $3 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32) (param $i64 i64) (param $f32 f32) (param $f64 f64)
;; Use nothing at all: all params can be removed.
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $foo
(i32.const 0)
(i64.const 1)
(f32.const 2)
(f64.const 3)
)
(call_ref $sig
(i32.const 4)
(i64.const 5)
(f32.const 6)
(f64.const 7)
(ref.func $foo)
)
)
)
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func)))
(type $sig (sub (func (param i32))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
;; Use the parameters' index, but not its value. We can still remove it,
;; and the value set in the function is then set to a local and not a param,
;; which works just as well.
(local.set $i32
(i32.const 1)
)
(i32.store
(i32.const 0)
(local.get $i32)
)
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: )
(func $caller
(local $x i32)
(call $foo
;; (avoid passing in a constant value to avoid other opts kicking in)
(local.get $x)
)
)
)
(module
;; CHECK: (type $sig (sub (func)))
(type $sig (sub (func (param i32))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
;; This function does not use the parameter. It also has no calls, but that
;; is not a problem - we can still remove the parameter.
)
)
(module
;; CHECK: (type $sig (sub (func (param i32))))
(type $sig (sub (func (param i32))))
;; As above, but now an import also uses this signature, which prevents us
;; from changing anything.
;; CHECK: (import "out" "func" (func $import (type $sig) (param i32)))
(import "out" "func" (func $import (type $sig) (param i32)))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig) (param $i32 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
)
)
(module
;; CHECK: (type $sig (sub (func (param i32))))
(type $sig (sub (func (param i32))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig) (param $i32 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
)
;; CHECK: (func $bar (type $sig) (param $i32 i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (type $sig) (param $i32 i32)
;; As above, but now there is a second (non-imported) function using this
;; signature, and it does use the param, so we cannot optimize.
(i32.store
(i32.const 0)
(local.get $i32)
)
)
)
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $sig2 (sub (func (param i32))))
;; CHECK: (type $sig (sub (func)))
(type $sig (sub (func (param i32))))
(type $sig2 (sub (func (param i32))))
)
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
)
;; CHECK: (func $bar (type $sig2) (param $i32 i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (type $sig2) (param $i32 i32)
;; As above, but now the second function has a different signature, so we
;; can optimize one while not modifying the other.
(i32.store
(i32.const 0)
(local.get $i32)
)
)
)
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $0 (func))
;; CHECK: (type $sig (sub (func)))
(type $sig (sub (func (param i32))))
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $bar $foo)
;; CHECK: (func $foo (type $sig)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
)
;; CHECK: (func $bar (type $sig)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: )
(func $bar (type $sig) (param $i32 i32)
;; As above, but the second function also does not use the parameter, and
;; has the same type. We can optimize both at once.
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call $bar)
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (ref.func $bar)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $foo
(i32.const 0)
)
(call $bar
(i32.const 1)
)
(call_ref $sig
(i32.const 2)
(ref.func $foo)
)
(call_ref $sig
(i32.const 2)
(ref.func $bar)
)
)
;; CHECK: (func $caller-2 (type $0)
;; CHECK-NEXT: (call $bar)
;; CHECK-NEXT: (call_ref $sig
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller-2
;; Also add some more calls to see they are updated too.
(call $bar
(i32.const 1)
)
(call_ref $sig
(i32.const 2)
(ref.func $foo)
)
)
)
(module
;; The presence of a table prevents us from doing any optimizations.
(table 1 1 anyref)
;; CHECK: (type $sig (sub (func (param i32))))
(type $sig (sub (func (param i32))))
;; CHECK: (table $0 1 1 anyref)
;; CHECK: (func $foo (type $sig) (param $i32 i32)
;; CHECK-NEXT: )
(func $foo (type $sig) (param $i32 i32)
)
)
;; Exports cannot be optimized in any way: we cannot remove parameters from
;; them, and also we cannot apply constant parameter values either.
(module
;; CHECK: (type $sig (sub (func (param i32))))
(type $sig (sub (func (param i32))))
;; CHECK: (type $1 (func))
;; CHECK: (export "foo" (func $foo))
;; CHECK: (export "bar" (func $bar))
;; CHECK: (func $foo (type $sig) (param $i32 i32)
;; CHECK-NEXT: )
(func $foo (export "foo") (type $sig) (param $i32 i32)
)
;; CHECK: (func $bar (type $sig) (param $i32 i32)
;; CHECK-NEXT: )
(func $bar (export "bar") (type $sig) (param $i32 i32)
)
;; CHECK: (func $call-bar (type $1)
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $call-bar
(call $bar
(i32.const 42)
)
)
)
;; Two functions with two different types have an unused parameter. After
;; removing the parameter from each, they both have no parameters. They should
;; *not* have the same type, however: the type should be different nominally
;; even though after the pruning they are identical structurally.
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $sig2 (sub (func)))
;; CHECK: (type $sig1 (sub (func)))
(type $sig1 (sub (func (param i32))))
(type $sig2 (sub (func (param f64))))
)
;; CHECK: (func $foo1 (type $sig1)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: )
(func $foo1 (type $sig1) (param $i32 i32)
)
;; CHECK: (func $foo2 (type $sig2)
;; CHECK-NEXT: (local $0 f64)
;; CHECK-NEXT: )
(func $foo2 (type $sig2) (param $f64 f64)
)
)
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $sig-bar (sub (func (param i32))))
;; CHECK: (type $sig-foo (sub (func)))
(type $sig-foo (sub (func (param i32))))
(type $sig-bar (sub (func (param i32))))
)
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig-foo)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig-foo) (param $i32 i32)
;; This function is always called with the same constant, and we can
;; apply that constant here and prune the param.
(i32.store
(i32.const 0)
(local.get $i32)
)
(call $foo (i32.const 42))
(call $foo (i32.const 42))
(call $foo (i32.const 42))
)
;; CHECK: (func $bar (type $sig-bar) (param $i32 i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (local.get $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (i32.const 43)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (type $sig-bar) (param $i32 i32)
;; This function is called with various values, and cannot be optimized like
;; the previous one.
(i32.store
(i32.const 0)
(local.get $i32)
)
(call $bar (i32.const 42))
(call $bar (i32.const 43))
)
)
;; As above, but $foo's parameter is a ref.func, which is also a constant
;; value that we can optimize in the case of $foo (but not $bar, again, as $bar
;; is not always sent a constant value).
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $sig-bar (sub (func (param funcref))))
;; CHECK: (type $sig-foo (sub (func)))
(type $sig-foo (sub (func (param funcref))))
(type $sig-bar (sub (func (param funcref))))
)
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (elem declare func $bar $foo)
;; CHECK: (func $foo (type $sig-foo)
;; CHECK-NEXT: (local $0 funcref)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig-foo) (param $funcref funcref)
(drop (local.get $funcref))
(call $foo (ref.func $foo))
(call $foo (ref.func $foo))
(call $foo (ref.func $foo))
)
;; CHECK: (func $bar (type $sig-bar) (param $funcref funcref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $funcref)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (ref.func $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (ref.func $bar)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (type $sig-bar) (param $funcref funcref)
(drop (local.get $funcref))
(call $bar (ref.func $foo))
(call $bar (ref.func $bar))
)
)
;; As above, but the values are now ref.nulls.
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $sig-bar (sub (func (param anyref))))
;; CHECK: (type $sig-foo (sub (func)))
(type $sig-foo (sub (func (param anyref))))
(type $sig-bar (sub (func (param anyref))))
)
(memory 1 1)
;; CHECK: (memory $0 1 1)
;; CHECK: (func $foo (type $sig-foo)
;; CHECK-NEXT: (local $0 anyref)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (type $sig-foo) (param $anyref anyref)
(drop (local.get $anyref))
(call $foo (ref.null none))
(call $foo (ref.null none))
)
;; CHECK: (func $bar (type $sig-bar) (param $anyref anyref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $anyref)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $bar
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (type $sig-bar) (param $anyref anyref)
(drop (local.get $anyref))
;; Mixing a null with something else prevents optimization, of course.
(call $bar (ref.i31 (i32.const 0)))
(call $bar (ref.null none))
)
)
(module
(type $A (struct))
;; CHECK: (type $0 (func))
;; CHECK: (func $0 (type $0)
;; CHECK-NEXT: (local $0 f32)
;; CHECK-NEXT: (block ;; (replaces unreachable RefCast we can't emit)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $0 (param $0 f32)
;; $A is only used in an unreachable cast. We should not error when
;; removing the param from this function, during which we collect heap
;; types, and must find this one even though the cast is unreachable, as
;; we do need to handle it in the optimization as well as print it (note how
;; type $A is declared in the output here - it would be a bug if it were
;; not, which this is a regression test for).
(ref.cast (ref null $A)
(unreachable)
)
)
)
;; Do not prune signatures used in the call.without.effects intrinsic.
(module
;; CHECK: (type $0 (func (param i32 funcref) (result i32)))
;; CHECK: (type $1 (func (param i32) (result i32)))
;; CHECK: (type $2 (func (result i32)))
;; CHECK: (import "binaryen-intrinsics" "call.without.effects" (func $cwe (type $0) (param i32 funcref) (result i32)))
(import "binaryen-intrinsics" "call.without.effects" (func $cwe (param i32 funcref) (result i32)))
;; CHECK: (elem declare func $func)
;; CHECK: (func $func (type $1) (param $0 i32) (result i32)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
(func $func (param i32) (result i32)
;; The parameter is unused, so we want to prune it. We won't because of the
;; situation in the calling function, below.
(i32.const 1)
)
;; CHECK: (func $caller (type $2) (result i32)
;; CHECK-NEXT: (call $cwe
;; CHECK-NEXT: (i32.const 41)
;; CHECK-NEXT: (ref.func $func)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller (result i32)
;; We call $func using call.without.effects. This causes us to not optimize
;; in this case.
(call $cwe
(i32.const 41)
(ref.func $func)
)
)
)
;; Due to function subtyping, we cannot prune fields from $func.B without also
;; pruning them in $func.A, and vice versa, if they have a subtyping
;; relationship. Atm we do not prune such "cycles" so we do not optimize here.
;; TODO
(module
;; CHECK: (type $struct.A (sub (struct (field i32))))
(type $struct.A (sub (struct (field i32))))
;; CHECK: (type $array.A (sub (array (ref $struct.A))))
;; CHECK: (type $struct.B (sub $struct.A (struct (field i32) (field i64))))
(type $struct.B (sub $struct.A (struct (field i32) (field i64))))
(type $array.A (sub (array (ref $struct.A))))
;; CHECK: (type $array.B (sub $array.A (array (ref $struct.B))))
(type $array.B (sub $array.A (array (ref $struct.B))))
;; CHECK: (type $func.A (sub (func (param (ref $array.B)) (result (ref $array.A)))))
(type $func.A (sub (func (param (ref $array.B)) (result (ref $array.A)))))
;; CHECK: (type $func.B (sub $func.A (func (param (ref $array.A)) (result (ref $array.B)))))
(type $func.B (sub $func.A (func (param (ref $array.A)) (result (ref $array.B)))))
;; CHECK: (func $func.A (type $func.A) (param $0 (ref $array.B)) (result (ref $array.A))
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $func.A (type $func.A) (param $0 (ref $array.B)) (result (ref $array.A))
(unreachable)
)
;; CHECK: (func $func.B (type $func.B) (param $0 (ref $array.A)) (result (ref $array.B))
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $func.B (type $func.B) (param $0 (ref $array.A)) (result (ref $array.B))
(unreachable)
)
)
;; Test corner cases with var updating. To remove the parameter of $func we
;; must move the parameter to a local first. We must then adjust local types
;; properly while adjusting the signature (when the signature loses a parameter,
;; local indexes change, which is a delicate dance handled by
;; GlobalTypeRewriter::updateSignatures and ParamUtils::removeParameters;
;; moving the parameter to a local first should not get in the way there).
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $struct (sub (struct (field v128))))
(type $struct (sub (struct (field v128))))
;; CHECK: (type $1 (func))
;; CHECK: (type $func (func))
(type $func (func (param v128)))
;; CHECK: (elem declare func $func)
;; CHECK: (func $func (type $func)
;; CHECK-NEXT: (local $0 v128)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $func (type $func) (param $0 v128)
;; The parameter will be removed.
(nop)
)
;; CHECK: (func $caller (type $1)
;; CHECK-NEXT: (local $0 (ref $struct))
;; CHECK-NEXT: (local $1 externref)
;; CHECK-NEXT: (local $2 v128)
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (struct.get $struct 0
;; CHECK-NEXT: (local.tee $0
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref $func
;; CHECK-NEXT: (ref.func $func)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller (param $param externref)
(local $var (ref $struct))
;; The parameter of this call_ref will be removed.
(call_ref $func
;; Use a struct.get, which would error if the type the nested tee were
;; incorrect (it asserts on it being a struct type).
(struct.get $struct 0
;; Use a tee to test the updating of tee'd vars, as mentioned above.
(local.tee $var
(struct.new_default $struct)
)
)
(ref.func $func)
)
)
)
(module
;; CHECK: (type $0 (func (param i32)))
;; CHECK: (rec
;; CHECK-NEXT: (type $1 (func (param i32)))
;; CHECK: (type $2 (func (result i32)))
;; CHECK: (type $3 (func (param i32)))
;; CHECK: (tag $tag (param i32))
(tag $tag (param i32))
;; CHECK: (func $catch-pop (type $2) (result i32)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (block $block (result i32)
;; CHECK-NEXT: (try $try
;; CHECK-NEXT: (do
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $tag
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (pop i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (br_if $block
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $target
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 3)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $catch-pop (result i32)
(block $block (result i32)
(try $try
(do
(nop)
)
(catch $tag
(call $target
(pop i32)
;; We can remove this parameter by moving it to a local first, which
;; also moves the pop, which then needs to be fixed up.
(br_if $block
(i32.const 1)
(i32.const 2)
)
)
;; This nop causes the call to be in a block. When we add another
;; block to hold the code that we move, we'd get an error if we don't
;; apply fixups.
(nop)
)
)
(i32.const 3)
)
)
;; CHECK: (func $target (type $1) (param $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $target (param $x i32) (param $y i32)
;; Use only the first param. The second will be removed.
(drop
(local.get $x)
)
)
)
;; As above, but remove the other parameter (the pop).
(module
;; CHECK: (type $0 (func (param i32)))
;; CHECK: (rec
;; CHECK-NEXT: (type $1 (func (param i32)))
;; CHECK: (type $2 (func (result i32)))
;; CHECK: (type $3 (func (param i32)))
;; CHECK: (tag $tag (param i32))
(tag $tag (param i32))
;; CHECK: (func $catch-pop (type $2) (result i32)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (block $block (result i32)
;; CHECK-NEXT: (try $try
;; CHECK-NEXT: (do
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $tag
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (pop i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (br_if $block
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $target
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 3)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $catch-pop (result i32)
(block $block (result i32)
(try $try
(do
(nop)
)
(catch $tag
(call $target
(pop i32)
(br_if $block
(i32.const 1)
(i32.const 2)
)
)
(nop)
)
)
(i32.const 3)
)
)
;; CHECK: (func $target (type $1) (param $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $target (param $x i32) (param $y i32)
(drop
(local.get $y) ;; this changed from $x to $y
)
)
)
;; $exported is exported. The entire rec group becomes exported as well, which
;; causes $unused-param's type to be public, which means we cannot normally
;; modify it. However, in closed world we could allow such changes, by keeping
;; the original public rec group as-is, and add a new rec group for private
;; types, put the pruned type there, and use that pruned type on $unused-param.
;; That can work here, but not in the testcase after us. For now, we also do not
;; optimize here, as figuring out when it is safe is very difficult, and may
;; need a new design TODO
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $none (func))
(type $none (func))
;; CHECK: (type $much (func (param i32)))
(type $much (func (param i32)))
)
;; CHECK: (type $2 (func))
;; CHECK: (export "exported" (func $exported))
;; CHECK: (func $exported (type $none)
;; CHECK-NEXT: )
(func $exported (export "exported") (type $none)
)
;; CHECK: (func $unused-param (type $much) (param $param i32)
;; CHECK-NEXT: )
(func $unused-param (type $much) (param $param i32)
)
;; CHECK: (func $caller (type $2)
;; CHECK-NEXT: (call $unused-param
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $unused-param
(i32.const 0)
)
)
)
;; As the previous testcase, but add another use of the type we want to prune,
;; in a struct.new. The struct type is public, so we cannot modify it and
;; replace the reference to the function type with the pruned version.
(module
(rec
;; CHECK: (type $0 (func))
;; CHECK: (rec
;; CHECK-NEXT: (type $none (func))
(type $none (func))
;; CHECK: (type $much (func (param i32)))
(type $much (func (param i32)))
;; CHECK: (type $struct (struct (field (ref $much))))
(type $struct (struct (field (ref $much))))
)
;; CHECK: (elem declare func $unused-param)
;; CHECK: (export "exported" (func $exported))
;; CHECK: (func $exported (type $none)
;; CHECK-NEXT: )
(func $exported (export "exported") (type $none)
;; This makes the rec group public.
)
;; CHECK: (func $unused-param (type $much) (param $param i32)
;; CHECK-NEXT: )
(func $unused-param (type $much) (param $param i32)
)
;; CHECK: (func $caller (type $0)
;; CHECK-NEXT: (call $unused-param
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $caller
(call $unused-param
(i32.const 0)
)
)
;; CHECK: (func $struct.new (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (struct.new $struct
;; CHECK-NEXT: (ref.func $unused-param)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $struct.new
;; This struct.new causes the problem mentioned above.
(drop
(struct.new $struct
(ref.func $unused-param)
)
)
)
)
|