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
|
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --remove-unused-brs -all -S -o - \
;; RUN: | filecheck %s
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $struct (sub (struct)))
(type $struct (sub (struct)))
;; CHECK: (type $struct2 (struct))
(type $struct2 (struct))
;; CHECK: (type $substruct (sub $struct (struct)))
(type $substruct (sub $struct (struct)))
)
;; CHECK: (type $struct-nn (struct (field (ref any))))
(type $struct-nn (struct (field (ref any))))
;; CHECK: (func $br_on-if (type $8) (param $0 (ref struct))
;; CHECK-NEXT: (block $label
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (select (result (ref struct))
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on-if (param $0 (ref struct))
(block $label
(drop
;; This br is never taken, as the input is non-nullable, so we can remove
;; it. When we do so, we replace it with the if. We should not rescan that
;; if, which has already been walked, as that would hit an assertion.
;;
(br_on_null $label
;; This if can also be turned into a select, separately from the above
;; (that is not specifically intended to be tested here).
(if (result (ref struct))
(i32.const 0)
(then
(local.get $0)
)
(else
(local.get $0)
)
)
)
)
)
)
;; CHECK: (func $br_on_cast (type $5) (result (ref $struct))
;; CHECK-NEXT: (local $struct (ref null $struct))
;; CHECK-NEXT: (block $block (result (ref $struct))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (br_on_non_null $block
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast $block (ref $struct) (ref $substruct)
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast (result (ref $struct))
(local $struct (ref null $struct))
(block $block (result (ref $struct))
(drop
;; This cast can be computed at compile time: it will definitely be taken,
;; so we can turn it into a normal br.
(br_on_cast $block anyref (ref $struct)
(struct.new $struct)
)
)
(drop
;; This cast can be partially computed at compile time, but we still need to
;; do a null check.
(br_on_cast $block anyref (ref $struct)
(local.get $struct)
)
)
(drop
;; This cast cannot be optimized at all.
(br_on_cast $block anyref (ref $substruct)
(struct.new $struct)
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast-fallthrough (type $5) (result (ref $struct))
;; CHECK-NEXT: (local $struct (ref null $struct))
;; CHECK-NEXT: (local $any anyref)
;; CHECK-NEXT: (block $block (result (ref $struct))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (ref.cast (ref $struct)
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (br_on_non_null $block
;; CHECK-NEXT: (ref.cast (ref null $struct)
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast $block anyref (ref $substruct)
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast-fallthrough (result (ref $struct))
;; Same as above, but now the type information comes from fallthrough values.
(local $struct (ref null $struct))
(local $any anyref)
(block $block (result (ref $struct))
(drop
;; Definitely taken, but will need a cast for validity.
(br_on_cast $block anyref (ref $struct)
(local.tee $any (struct.new $struct))
)
)
(drop
;; Needs a null check and cast for validity.
(br_on_cast $block anyref (ref $struct)
(local.tee $any (local.get $struct))
)
)
(drop
;; This cannot be optimized, but at least it still doesn't need an
;; additional cast.
(br_on_cast $block anyref (ref $substruct)
(local.tee $any (struct.new $struct))
)
)
(unreachable)
)
)
;; CHECK: (func $nested_br_on_cast (type $9) (result i31ref)
;; CHECK-NEXT: (block $label$1 (result (ref i31))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $label$1
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $nested_br_on_cast (result i31ref)
(block $label$1 (result i31ref)
(drop
;; The inner br_on_cast will become a direct br since the type proves it
;; is in fact i31. That then becomes unreachable, and the parent must
;; handle that properly (do nothing without hitting an assertion).
(br_on_cast $label$1 (ref any) (ref i31)
(br_on_cast $label$1 (ref any) (ref i31)
(ref.i31 (i32.const 0))
)
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast_unrelated (type $6) (result (ref null $struct))
;; CHECK-NEXT: (local $nullable-struct2 (ref null $struct2))
;; CHECK-NEXT: (block $block (result nullref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast $block (ref null $struct2) nullref
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_unrelated (result (ref null $struct))
(local $nullable-struct2 (ref null $struct2))
(block $block (result (ref null $struct))
(drop
;; This cast can be computed at compile time: it will definitely fail, so we
;; can remove it.
(br_on_cast $block anyref (ref $struct)
(struct.new $struct2)
)
)
(drop
;; We can still remove it even if the cast allows nulls.
(br_on_cast $block anyref (ref null $struct)
(struct.new $struct2)
)
)
(drop
;; Or if the cast does not allow nulls and the value is nullable.
(br_on_cast $block anyref (ref $struct)
(local.get $nullable-struct2)
)
)
(drop
;; But if both are nullable, then the cast will succeed only if the value is
;; null, so we can partially optimize.
;; TODO: Optimize this.
(br_on_cast $block anyref (ref null $struct)
(local.get $nullable-struct2)
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast_unrelated-fallthrough (type $6) (result (ref null $struct))
;; CHECK-NEXT: (local $any anyref)
;; CHECK-NEXT: (local $nullable-struct2 (ref null $struct2))
;; CHECK-NEXT: (block $block (result nullref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast $block anyref nullref
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_unrelated-fallthrough (result (ref null $struct))
;; Same as above, but now all the type information comes from fallthrough values.
(local $any anyref)
(local $nullable-struct2 (ref null $struct2))
(block $block (result (ref null $struct))
(drop
;; Definitely not taken.
(br_on_cast $block anyref (ref $struct)
(local.tee $any (struct.new $struct2))
)
)
(drop
;; Still not taken. Note that we start by flowing out a non-nullable value,
;; and will add a cast to ensure we still do after optimization.
(br_on_cast $block anyref (ref null $struct)
(local.tee $any (struct.new $struct2))
)
)
(drop
;; Also not taken.
(br_on_cast $block anyref (ref $struct)
(local.tee $any (local.get $nullable-struct2))
)
)
(drop
;; Taken only if null.
;; TODO: Optimize this.
(br_on_cast $block anyref (ref null $struct)
(local.tee $any (local.get $nullable-struct2))
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast_fail (type $3) (result anyref)
;; CHECK-NEXT: (local $struct (ref null $struct))
;; CHECK-NEXT: (block $block (result (ref null $struct))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast_fail $block (ref null $struct) (ref $struct)
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast_fail $block (ref $struct) (ref $substruct)
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_fail (result anyref)
(local $struct (ref null $struct))
(block $block (result anyref)
(drop
;; This cast can be computed at compile time: it will definitely succeed so
;; the branch will not be taken.
(br_on_cast_fail $block anyref (ref $struct)
(struct.new $struct)
)
)
(drop
;; This cast can be partially computed at compile time, but we still need to
;; do a null check.
;; TODO: optimize this.
(br_on_cast_fail $block anyref (ref $struct)
(local.get $struct)
)
)
(drop
;; This cast cannot be optimized at all.
(br_on_cast_fail $block anyref (ref $substruct)
(struct.new $struct)
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast_fail-fallthrough (type $3) (result anyref)
;; CHECK-NEXT: (local $any anyref)
;; CHECK-NEXT: (local $struct (ref null $struct))
;; CHECK-NEXT: (block $block (result anyref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref $struct)
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast_fail $block anyref (ref $struct)
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_cast_fail $block anyref (ref $substruct)
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_fail-fallthrough (result anyref)
;; Same as above, but now the type information comes from fallthrough values.
(local $any anyref)
(local $struct (ref null $struct))
(block $block (result anyref)
(drop
;; This cast will succeed. We will need a cast for validity.
(br_on_cast_fail $block anyref (ref $struct)
(local.tee $any (struct.new $struct))
)
)
(drop
;; We will still need a null check.
;; TODO: optimize this.
(br_on_cast_fail $block anyref (ref $struct)
(local.tee $any (local.get $struct))
)
)
(drop
;; This cast cannot be optimized at all.
(br_on_cast_fail $block anyref (ref $substruct)
(local.tee $any (struct.new $struct))
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast_fail_unrelated (type $3) (result anyref)
;; CHECK-NEXT: (local $nullable-struct2 (ref null $struct2))
;; CHECK-NEXT: (block $block (result (ref null $struct2))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (br_on_non_null $block
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_fail_unrelated (result anyref)
(local $nullable-struct2 (ref null $struct2))
(block $block (result anyref)
(drop
;; This cast can be computed at compile time: it will definitely fail, so we
;; can replace it with an unconditional br.
(br_on_cast_fail $block anyref (ref $struct)
(struct.new $struct2)
)
)
(drop
;; We can still replace it even if the cast allows nulls.
(br_on_cast_fail $block anyref (ref null $struct)
(struct.new $struct2)
)
)
(drop
;; Or if the cast does not allow nulls and the value is nullable.
(br_on_cast_fail $block anyref (ref $struct)
(local.get $nullable-struct2)
)
)
(drop
;; But if both are nullable, then we can only partially optimize because we
;; still have to do a null check.
(br_on_cast_fail $block anyref (ref null $struct)
(local.get $nullable-struct2)
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast_fail_unrelated-fallthrough (type $3) (result anyref)
;; CHECK-NEXT: (local $any anyref)
;; CHECK-NEXT: (local $nullable-struct2 (ref null $struct2))
;; CHECK-NEXT: (block $block (result anyref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (struct.new_default $struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (br_on_non_null $block
;; CHECK-NEXT: (local.tee $any
;; CHECK-NEXT: (local.get $nullable-struct2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast_fail_unrelated-fallthrough (result anyref)
;; Same as above, but now type information comes from fallthrough values.
(local $any anyref)
(local $nullable-struct2 (ref null $struct2))
(block $block (result anyref)
(drop
;; Will definitely take the branch.
(br_on_cast_fail $block anyref (ref $struct)
(local.tee $any (struct.new $struct2))
)
)
(drop
;; Ditto.
(br_on_cast_fail $block anyref (ref null $struct)
(local.tee $any (struct.new $struct2))
)
)
(drop
;; Ditto.
(br_on_cast_fail $block anyref (ref $struct)
(local.tee $any (local.get $nullable-struct2))
)
)
(drop
;; Still has to do a null check.
(br_on_cast_fail $block anyref (ref null $struct)
(local.tee $any (local.get $nullable-struct2))
)
)
(unreachable)
)
)
;; CHECK: (func $br_on_cast-unreachable (type $7) (param $i31ref i31ref) (result anyref)
;; CHECK-NEXT: (block $block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref none))
;; CHECK-NEXT: (ref.cast (ref none)
;; CHECK-NEXT: (block (result i31ref)
;; CHECK-NEXT: (local.get $i31ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref none))
;; CHECK-NEXT: (ref.cast (ref none)
;; CHECK-NEXT: (block (result i31ref)
;; CHECK-NEXT: (local.get $i31ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $br_on_cast-unreachable (param $i31ref i31ref) (result anyref)
;; Optimize out br_on_cast* where the input is uninhabitable.
(block $block (result anyref)
(drop
(br_on_cast $block anyref (ref i31)
(block (result anyref)
(ref.cast (ref struct)
(block (result anyref)
(local.get $i31ref)
)
)
)
)
)
(br_on_cast_fail $block anyref (ref i31)
(block (result anyref)
(ref.cast (ref struct)
(block (result anyref)
(local.get $i31ref)
)
)
)
)
)
)
;; CHECK: (func $fallthrough-unreachable (type $7) (param $0 i31ref) (result anyref)
;; CHECK-NEXT: (block $outer
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block ;; (replaces unreachable RefCast we can't emit)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref none)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $fallthrough-unreachable (param $0 i31ref) (result anyref)
(block $outer (result (ref none))
(drop
;; This should not crash due to the new unreachable below.
(br_on_cast $outer (ref none) (ref none)
(ref.cast (ref none)
;; This will be optimized to a drop + unreachable.
(br_on_cast $outer (ref none) (ref none)
(ref.cast (ref none)
(local.get $0)
)
)
)
)
)
(unreachable)
)
)
;; CHECK: (func $casts-are-costly (type $10) (param $x i32)
;; CHECK-NEXT: (local $struct (ref null $struct))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (ref.test (ref none)
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (if (result nullref)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (ref.cast nullref
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (if (result (ref null $struct))
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (block $something (result (ref null $struct))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (br_on_non_null $something
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (select (result nullref)
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (block $nothing
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $nothing)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $casts-are-costly (param $x i32)
;; We never turn an if into a select if an arm has a cast of any kind, as
;; those things involve branches internally, so we'd be adding more than we
;; save.
(local $struct (ref null $struct))
(drop
(if (result i32)
(local.get $x)
(then
(ref.test (ref $struct)
(ref.null any)
)
)
(else
(i32.const 0)
)
)
)
(drop
(if (result anyref)
(local.get $x)
(then
(ref.null any)
)
(else
(ref.cast (ref null $struct)
(ref.null any)
)
)
)
)
;; We do not selectify here because the amount of work in the if is
;; significant (there is a cast and a branch).
(drop
(if (result anyref)
(local.get $x)
(then
(block (result anyref)
(block $something (result anyref)
(drop
(br_on_cast $something anyref (ref $struct)
(local.get $struct)
)
)
(ref.null any)
)
)
)
(else
(ref.null any)
)
)
)
;; However, null checks are fairly fast, and we will emit a select here.
(drop
(if (result anyref)
(local.get $x)
(then
(block (result anyref)
(block $nothing
(drop
(br_on_null $nothing
(ref.null $struct)
)
)
)
(ref.null any)
)
)
(else
(ref.null any)
)
)
)
)
;; CHECK: (func $threading (type $11) (param $x anyref)
;; CHECK-NEXT: (block $outer
;; CHECK-NEXT: (block $inner
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_null $outer
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $threading (param $x anyref)
(block $outer
(block $inner
;; This jump can go to $outer.
(drop
(br_on_null $inner
(local.get $x)
)
)
)
)
)
;; CHECK: (func $test (type $12) (param $x (ref any))
;; CHECK-NEXT: (local $temp anyref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $block (result (ref $struct-nn))
;; CHECK-NEXT: (struct.new $struct-nn
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (br_on_cast $block anyref (ref $struct-nn)
;; CHECK-NEXT: (local.tee $temp
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test (param $x (ref any))
(local $temp anyref)
;; Read the inline comments blow from the bottom to the top (order of
;; execution). Basically, the story is that the br_on_cast begins as
;; flowing out a non-nullable type, since the cast allows nulls (so only a
;; non-null can flow out). We can see that the br_on_cast receives a non-
;; nullable value, even though it flows through a local.tee that un-refines
;; it. Using the non-nullability, we can refine the cast type (type sent on
;; the branch) to be non-nullable. But then the type of the br_on_cast itself
;; becomes nullable, since nulls no longer get sent on the branch, which
;; breaks the parent that must receive a non-nullable value.
;;
;; To fix this, we add a cast on the br's output, forcing it to the exact
;; same type it had before.
(drop
(block $block (result anyref)
(struct.new $struct-nn ;; must provide a NON-
;; nullable value for the
;; struct field
(br_on_cast $block anyref (ref null $struct-nn) ;; GLB on the castType
;; makes it non-nullable,
;; which makes the type
;; of the br_on_cast
;; nullable
(local.tee $temp ;; nullable
(local.get $x) ;; non-nullable
)
)
)
)
)
)
)
|