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
|
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --remove-unused-names --local-subtyping -all -S -o - \
;; RUN: | filecheck %s
;; --remove-unused-names is run to avoid adding names to blocks. Block names
;; can prevent non-nullable local validation (we emit named blocks in the binary
;; format, if we need them, but never emit unnamed ones), which affects some
;; testcases.
(module
(type $"{}" (sub (struct)))
(type $"{i32}" (sub (struct (field i32))))
(type $array (sub (array i8)))
;; CHECK: (type $ret-any (sub (func (result anyref))))
(type $ret-any (sub (func (result anyref))))
;; CHECK: (type $ret-i31 (sub $ret-any (func (result i31ref))))
(type $ret-i31 (sub $ret-any (func (result i31ref))))
;; CHECK: (import "out" "i32" (func $i32 (type $1) (result i32)))
(import "out" "i32" (func $i32 (result i32)))
;; CHECK: (import "out" "i64" (func $i64 (type $4) (result i64)))
(import "out" "i64" (func $i64 (result i64)))
;; CHECK: (tag $e-anyref (param anyref))
(tag $e-anyref (param anyref))
;; Refinalization can find a more specific type, where the declared type was
;; not the optimal LUB.
;; CHECK: (func $refinalize (type $2) (param $x i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (if (result (ref i31))
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $block (result (ref i31))
;; CHECK-NEXT: (br $block
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $refinalize (param $x i32)
(drop
(if (result anyref)
(local.get $x)
(then
(ref.i31 (i32.const 0))
)
(else
(ref.i31 (i32.const 1))
)
)
)
(drop
(block $block (result anyref)
(br $block
(ref.i31 (i32.const 0))
)
(ref.i31 (i32.const 1))
)
)
)
;; A simple case where a local has a single assignment that we can use as a
;; more specific type. A similar thing with a parameter, however, is not a
;; thing we can optimize. Also, ignore a local with zero assignments.
;; CHECK: (func $simple-local-but-not-param (type $8) (param $x funcref)
;; CHECK-NEXT: (local $y (ref $1))
;; CHECK-NEXT: (local $unused funcref)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $simple-local-but-not-param (param $x funcref)
(local $y funcref)
(local $unused funcref)
(local.set $x
(ref.func $i32)
)
(local.set $y
(ref.func $i32)
)
)
;; CHECK: (func $locals-with-multiple-assignments (type $9) (param $struct structref)
;; CHECK-NEXT: (local $x eqref)
;; CHECK-NEXT: (local $y (ref i31))
;; CHECK-NEXT: (local $z structref)
;; CHECK-NEXT: (local $w (ref func))
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $z
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $z
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $w
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $w
;; CHECK-NEXT: (ref.func $i64)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $locals-with-multiple-assignments (param $struct (ref null struct))
(local $x anyref)
(local $y anyref)
(local $z anyref)
(local $w funcref)
;; x is assigned two different types with a new LUB possible
(local.set $x
(ref.i31 (i32.const 0))
)
(local.set $x
(local.get $struct)
)
;; y and z are assigned the same more specific type twice
(local.set $y
(ref.i31 (i32.const 0))
)
(local.set $y
(ref.i31 (i32.const 1))
)
(local.set $z
(local.get $struct)
)
(local.set $z
(local.get $struct)
)
;; w is assigned two different types *without* a new LUB heap type possible,
;; as it already had the optimal LUB heap type (but it can become non-
;; nullable).
(local.set $w
(ref.func $i32)
)
(local.set $w
(ref.func $i64)
)
)
;; In some cases multiple iterations are necessary, as one inferred new type
;; applies to a get which then allows another inference.
;; CHECK: (func $multiple-iterations (type $0)
;; CHECK-NEXT: (local $x (ref $1))
;; CHECK-NEXT: (local $y (ref $1))
;; CHECK-NEXT: (local $z (ref $1))
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $z
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $multiple-iterations
(local $x funcref)
(local $y funcref)
(local $z funcref)
(local.set $x
(ref.func $i32)
)
(local.set $y
(local.get $x)
)
(local.set $z
(local.get $y)
)
)
;; Sometimes a refinalize is necessary in between the iterations.
;; CHECK: (func $multiple-iterations-refinalize (type $2) (param $i i32)
;; CHECK-NEXT: (local $x (ref $1))
;; CHECK-NEXT: (local $y (ref $4))
;; CHECK-NEXT: (local $z (ref func))
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (ref.func $i64)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $z
;; CHECK-NEXT: (select (result (ref func))
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (local.get $i)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $multiple-iterations-refinalize (param $i i32)
(local $x funcref)
(local $y funcref)
(local $z funcref)
(local.set $x
(ref.func $i32)
)
(local.set $y
(ref.func $i64)
)
(local.set $z
(select
(local.get $x)
(local.get $y)
(local.get $i)
)
)
)
;; CHECK: (func $multiple-iterations-refinalize-call-ref (type $0)
;; CHECK-NEXT: (local $f (ref $ret-i31))
;; CHECK-NEXT: (local $x i31ref)
;; CHECK-NEXT: (local.set $f
;; CHECK-NEXT: (ref.func $ret-i31)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (call_ref $ret-i31
;; CHECK-NEXT: (local.get $f)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $multiple-iterations-refinalize-call-ref
(local $f (ref null $ret-any))
(local $x anyref)
(local.set $f
(ref.func $ret-i31)
)
(local.set $x
;; After $f is refined to hold $ret-i31 and the call_ref is refinalized,
;; we will be able to refine $x to i31.
(call_ref $ret-any
(local.get $f)
)
)
)
;; CHECK: (func $multiple-iterations-refinalize-call-ref-bottom (type $0)
;; CHECK-NEXT: (local $f nullfuncref)
;; CHECK-NEXT: (local $x (ref none))
;; CHECK-NEXT: (local.set $f
;; CHECK-NEXT: (ref.null nofunc)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (block ;; (replaces unreachable CallRef we can't emit)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $f)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $multiple-iterations-refinalize-call-ref-bottom
(local $f (ref null $ret-any))
(local $x anyref)
;; Same as above, but now we refine $f to nullfuncref. Check that we don't crash.
(local.set $f
(ref.null nofunc)
)
(local.set $x
;; We can no longer refine $x because there is no result type we can use
;; after refining $f.
(call_ref $ret-any
(local.get $f)
)
)
)
;; CHECK: (func $ret-i31 (type $ret-i31) (result i31ref)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $ret-i31 (type $ret-i31) (result i31ref)
(unreachable)
)
;; CHECK: (func $nondefaultable (type $0)
;; CHECK-NEXT: (local $x (tuple funcref funcref))
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (tuple.make 2
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: (ref.func $i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $nondefaultable
(local $x (tuple funcref funcref))
;; This tuple is assigned non-nullable values, which means the subtype is
;; nondefaultable, and we must not apply it.
(local.set $x
(tuple.make 2
(ref.func $i32)
(ref.func $i32)
)
)
)
;; CHECK: (func $uses-default (type $2) (param $i i32)
;; CHECK-NEXT: (local $x (ref null $2))
;; CHECK-NEXT: (if
;; CHECK-NEXT: (local.get $i)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $uses-default)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $uses-default (param $i i32)
(local $x funcref)
(if
(local.get $i)
;; The only set to this local uses a more specific type than funcref.
(then
(local.set $x (ref.func $uses-default))
)
)
(drop
;; This get may use the default value, but it is ok to have a null of a
;; more refined type in the local.
(local.get $x)
)
)
;; CHECK: (func $unreachables (type $3) (result funcref)
;; CHECK-NEXT: (local $temp (ref $3))
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (ref.func $unreachables)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref $3))
;; CHECK-NEXT: (local.tee $temp
;; CHECK-NEXT: (ref.func $unreachables)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
(func $unreachables (result funcref)
(local $temp funcref)
;; Set a value that allows us to refine the local's type.
(local.set $temp
(ref.func $unreachables)
)
(unreachable)
;; A tee that is not reachable. We must still update its type, and the
;; parents.
(drop
(block (result funcref)
(local.tee $temp
(ref.func $unreachables)
)
)
)
;; A get that is not reachable. We must still update its type.
(local.get $temp)
)
;; CHECK: (func $incompatible-sets (type $1) (result i32)
;; CHECK-NEXT: (local $temp (ref null $1))
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (ref.func $incompatible-sets)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $temp
;; CHECK-NEXT: (ref.null nofunc)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (ref.null nofunc)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $incompatible-sets (result i32)
(local $temp funcref)
;; Set a value that allows us to specialize the local type.
(local.set $temp
(ref.func $incompatible-sets)
)
;; Make all code unreachable from here.
(unreachable)
;; In unreachable code, assign values that are not compatible with the more
;; specific type we will optimize to. This prevents optimization here (we
;; will optimize better after --dce is run).
(drop
(local.tee $temp
(ref.null func)
)
)
(local.set $temp
(ref.null func)
)
(unreachable)
)
;; CHECK: (func $become-non-nullable (type $0)
;; CHECK-NEXT: (local $x (ref $0))
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $become-non-nullable)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $become-non-nullable
(local $x (ref null func))
(local.set $x
(ref.func $become-non-nullable)
)
(drop
(local.get $x)
)
)
;; CHECK: (func $already-non-nullable (type $0)
;; CHECK-NEXT: (local $x (ref $0))
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $already-non-nullable)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $already-non-nullable
(local $x (ref func))
(local.set $x
(ref.func $already-non-nullable)
)
(drop
(local.get $x)
)
)
;; CHECK: (func $cannot-become-non-nullable (type $0)
;; CHECK-NEXT: (local $x (ref null $0))
;; CHECK-NEXT: (if
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $become-non-nullable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cannot-become-non-nullable
(local $x (ref null func))
;; The set is in a nested scope, so we should not make the local non-
;; nullable, as it would not validate. (We can refine the heap type,
;; though.)
(if
(i32.const 1)
(then
(local.set $x
(ref.func $become-non-nullable)
)
)
)
(drop
(local.get $x)
)
)
;; CHECK: (func $cannot-become-non-nullable-block (type $0)
;; CHECK-NEXT: (local $x (ref null $0))
;; CHECK-NEXT: (block $name
;; CHECK-NEXT: (br_if $name
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $become-non-nullable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cannot-become-non-nullable-block
(local $x (ref null func))
;; A named block prevents us from optimizing here, the same as above.
(block $name
;; Add a br_if to avoid the name being removed.
(br_if $name
(i32.const 1)
)
(local.set $x
(ref.func $become-non-nullable)
)
)
(drop
(local.get $x)
)
)
;; CHECK: (func $become-non-nullable-block-unnamed (type $0)
;; CHECK-NEXT: (local $x (ref $0))
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (ref.func $become-non-nullable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $become-non-nullable-block-unnamed
(local $x (ref null func))
;; An named block does *not* prevent us from optimizing here. Unlike above,
;; an unnamed block is never emitted in the binary format, so it does not
;; prevent validation.
(block
(local.set $x
(ref.func $become-non-nullable)
)
)
(drop
(local.get $x)
)
)
;; CHECK: (func $try_table-catch-result (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $catch (result anyref)
;; CHECK-NEXT: (try_table (catch $e-anyref $catch)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $try_table-catch-result
(drop
;; Must not be refined to (result nullref).
(block $catch (result anyref)
(try_table (catch $e-anyref $catch)
(nop)
)
(ref.null none)
)
)
)
;; CHECK: (func $try_table-ref (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $catch (result exnref)
;; CHECK-NEXT: (try_table (catch_all_ref $catch)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null noexn)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $try_table-ref
(drop
;; Must not be refined to nullexnref.
;; An exnref comes from the catch_all_ref.
(block $catch (result exnref)
(try_table (catch_all_ref $catch)
(nop)
)
(ref.null exn)
)
)
)
)
|