summaryrefslogtreecommitdiff
path: root/test/lit/cast-and-recast-tuple.wast
blob: 6ceefe7d6052791762d976e210c526236c7eae83 (plain)
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
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.

;; Part of cast-and-recast.wast, but containing tuples. This is split out
;; because we do not roundtrip tuple-containing code properly. We also use only
;; one roundtrip because of the accumulation of tuple logic, which would
;; otherwise make the output here very hard to read.

;; RUN: wasm-opt %s -all --roundtrip -S -o - | filecheck %s

(module
  (rec
    ;; CHECK:      (rec
    ;; CHECK-NEXT:  (type $A (sub (struct)))
    (type $A (sub (struct)))
    ;; CHECK:       (type $B (sub $A (struct)))
    (type $B (sub $A (struct)))
  )

  ;; CHECK:      (func $test-local-tuple-1 (type $3) (param $B (ref $B)) (param $x i32) (result anyref i32)
  ;; CHECK-NEXT:  (local $scratch (tuple (ref $B) i32))
  ;; CHECK-NEXT:  (local $scratch_3 (ref $B))
  ;; CHECK-NEXT:  (block $block (type $2) (result (ref $A) i32)
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (block (result (ref $B))
  ;; CHECK-NEXT:     (local.set $scratch_3
  ;; CHECK-NEXT:      (tuple.extract 2 0
  ;; CHECK-NEXT:       (local.tee $scratch
  ;; CHECK-NEXT:        (br_if $block
  ;; CHECK-NEXT:         (tuple.make 2
  ;; CHECK-NEXT:          (local.get $B)
  ;; CHECK-NEXT:          (i32.const 3)
  ;; CHECK-NEXT:         )
  ;; CHECK-NEXT:         (local.get $x)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (drop
  ;; CHECK-NEXT:      (tuple.extract 2 1
  ;; CHECK-NEXT:       (local.get $scratch)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_3)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (unreachable)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $test-local-tuple-1 (param $B (ref $B)) (param $x i32) (result anyref i32)
    ;; A dropped tuple that contains a ref. As it is dropped, we do not need to
    ;; do anything for this br_if. However, due to our general handling of
    ;; tuples the code here will grow quite a bit due the roundtrip, but we can
    ;; at least verify that there is no ref.cast added anywhere here.
    (block $out (result (ref $A) i32)
      (tuple.drop 2
        (br_if $out
          (tuple.make 2
            (local.get $B)
            (i32.const 3)
          )
          (local.get $x)
        )
      )
      (unreachable)
    )
  )

  ;; CHECK:      (func $test-local-tuple-2 (type $7) (param $B (ref $B)) (param $x i32) (result i32 i32)
  ;; CHECK-NEXT:  (local $temp i32)
  ;; CHECK-NEXT:  (local $3 i32)
  ;; CHECK-NEXT:  (local $scratch (tuple i32 i32))
  ;; CHECK-NEXT:  (local $scratch_5 i32)
  ;; CHECK-NEXT:  (block $block (type $4) (result i32 i32)
  ;; CHECK-NEXT:   (local.set $temp
  ;; CHECK-NEXT:    (block (result i32)
  ;; CHECK-NEXT:     (local.set $scratch_5
  ;; CHECK-NEXT:      (tuple.extract 2 0
  ;; CHECK-NEXT:       (local.tee $scratch
  ;; CHECK-NEXT:        (br_if $block
  ;; CHECK-NEXT:         (tuple.make 2
  ;; CHECK-NEXT:          (i32.const -1)
  ;; CHECK-NEXT:          (i32.const 3)
  ;; CHECK-NEXT:         )
  ;; CHECK-NEXT:         (local.get $x)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.set $3
  ;; CHECK-NEXT:      (tuple.extract 2 1
  ;; CHECK-NEXT:       (local.get $scratch)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_5)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (unreachable)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $test-local-tuple-2 (param $B (ref $B)) (param $x i32) (result i32 i32)
    (local $temp (tuple i32 i32))
    ;; This tuple is not dropped, but it contains no references, so we do not
    ;; need to do anything for the br_if, and we add no casts.
    (block $out (result i32 i32)
      (local.set $temp
        (br_if $out
          (tuple.make 2
            (i32.const -1)
            (i32.const 3)
          )
          (local.get $x)
        )
      )
      (unreachable)
    )
  )

  ;; CHECK:      (func $test-local-tuple-3 (type $3) (param $B (ref $B)) (param $x i32) (result anyref i32)
  ;; CHECK-NEXT:  (local $temp (ref $B))
  ;; CHECK-NEXT:  (local $3 i32)
  ;; CHECK-NEXT:  (local $scratch (tuple (ref $B) i32))
  ;; CHECK-NEXT:  (local $scratch_5 (ref $B))
  ;; CHECK-NEXT:  (block $block (type $5) (result (ref $B) i32)
  ;; CHECK-NEXT:   (local.set $temp
  ;; CHECK-NEXT:    (block (result (ref $B))
  ;; CHECK-NEXT:     (local.set $scratch_5
  ;; CHECK-NEXT:      (tuple.extract 2 0
  ;; CHECK-NEXT:       (local.tee $scratch
  ;; CHECK-NEXT:        (br_if $block
  ;; CHECK-NEXT:         (tuple.make 2
  ;; CHECK-NEXT:          (local.get $B)
  ;; CHECK-NEXT:          (i32.const 3)
  ;; CHECK-NEXT:         )
  ;; CHECK-NEXT:         (local.get $x)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.set $3
  ;; CHECK-NEXT:      (tuple.extract 2 1
  ;; CHECK-NEXT:       (local.get $scratch)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_5)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (unreachable)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $test-local-tuple-3 (param $B (ref $B)) (param $x i32) (result anyref i32)
    (local $temp (tuple (ref $B) i32))
    ;; This is not dropped and has a reference, but it has the right type, so no
    ;; cast is needed.
    (block $out (result (ref $B) i32)
      (local.set $temp
        (br_if $out
          (tuple.make 2
            (local.get $B)
            (i32.const 3)
          )
          (local.get $x)
        )
      )
      (unreachable)
    )
  )

  ;; CHECK:      (func $test-local-tuple-4-bad (type $3) (param $B (ref $B)) (param $x i32) (result anyref i32)
  ;; CHECK-NEXT:  (local $temp (ref $B))
  ;; CHECK-NEXT:  (local $3 (ref $A))
  ;; CHECK-NEXT:  (local $4 i32)
  ;; CHECK-NEXT:  (local $5 i32)
  ;; CHECK-NEXT:  (local $scratch (tuple (ref $B) i32))
  ;; CHECK-NEXT:  (local $scratch_7 (ref $B))
  ;; CHECK-NEXT:  (local $scratch_8 (ref $B))
  ;; CHECK-NEXT:  (block $block (type $2) (result (ref $A) i32)
  ;; CHECK-NEXT:   (local.set $3
  ;; CHECK-NEXT:    (block (result (ref $B))
  ;; CHECK-NEXT:     (local.set $scratch_7
  ;; CHECK-NEXT:      (tuple.extract 2 0
  ;; CHECK-NEXT:       (local.tee $scratch
  ;; CHECK-NEXT:        (br_if $block
  ;; CHECK-NEXT:         (tuple.make 2
  ;; CHECK-NEXT:          (local.get $B)
  ;; CHECK-NEXT:          (i32.const 3)
  ;; CHECK-NEXT:         )
  ;; CHECK-NEXT:         (local.get $x)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.set $5
  ;; CHECK-NEXT:      (tuple.extract 2 1
  ;; CHECK-NEXT:       (local.get $scratch)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_7)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (local.set $temp
  ;; CHECK-NEXT:    (block (result (ref $B))
  ;; CHECK-NEXT:     (local.set $scratch_8
  ;; CHECK-NEXT:      (ref.cast (ref $B)
  ;; CHECK-NEXT:       (local.get $3)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.set $4
  ;; CHECK-NEXT:      (local.get $5)
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_8)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (unreachable)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $test-local-tuple-4-bad (param $B (ref $B)) (param $x i32) (result anyref i32)
    (local $temp (tuple (ref $B) i32))
    ;; As above, but none of the mitigating circumstances happens: we have a
    ;; tuple with a reference that is refined compared to the break target. As a
    ;; result we must fix this up, which we do by adding locals, saving the
    ;; br_if's output to them, and then loading from those locals and casting.
    ;;
    ;; Comparing to $test-local-tuple-4, we end up with 3 more locals, and also
    ;; there is now a ref.cast.
    (block $out (result (ref $A) i32)
      (local.set $temp
        (br_if $out
          (tuple.make 2
            (local.get $B)
            (i32.const 3)
          )
          (local.get $x)
        )
      )
      (unreachable)
    )
  )

  ;; CHECK:      (func $test-local-tuple-4-bad-dupes (type $8) (param $B (ref $B)) (param $x i32) (result i32 anyref i32)
  ;; CHECK-NEXT:  (local $temp (ref $B))
  ;; CHECK-NEXT:  (local $3 (ref $B))
  ;; CHECK-NEXT:  (local $4 (ref $A))
  ;; CHECK-NEXT:  (local $5 i32)
  ;; CHECK-NEXT:  (local $scratch i32)
  ;; CHECK-NEXT:  (local $7 i32)
  ;; CHECK-NEXT:  (local $8 i32)
  ;; CHECK-NEXT:  (local $9 i32)
  ;; CHECK-NEXT:  (local $scratch_10 (tuple i32 (ref $B) i32))
  ;; CHECK-NEXT:  (local $scratch_11 (ref $B))
  ;; CHECK-NEXT:  (local $scratch_12 i32)
  ;; CHECK-NEXT:  (local $scratch_13 (ref $B))
  ;; CHECK-NEXT:  (local $scratch_14 i32)
  ;; CHECK-NEXT:  (local $scratch_15 (ref $B))
  ;; CHECK-NEXT:  (block $block (type $6) (result i32 (ref $A) i32)
  ;; CHECK-NEXT:   (local.set $9
  ;; CHECK-NEXT:    (block (result i32)
  ;; CHECK-NEXT:     (local.set $scratch_12
  ;; CHECK-NEXT:      (tuple.extract 3 0
  ;; CHECK-NEXT:       (local.tee $scratch_10
  ;; CHECK-NEXT:        (br_if $block
  ;; CHECK-NEXT:         (tuple.make 3
  ;; CHECK-NEXT:          (i32.const -3)
  ;; CHECK-NEXT:          (local.get $B)
  ;; CHECK-NEXT:          (i32.const 3)
  ;; CHECK-NEXT:         )
  ;; CHECK-NEXT:         (local.get $x)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.set $4
  ;; CHECK-NEXT:      (block (result (ref $B))
  ;; CHECK-NEXT:       (local.set $scratch_11
  ;; CHECK-NEXT:        (tuple.extract 3 1
  ;; CHECK-NEXT:         (local.get $scratch_10)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:       (local.set $8
  ;; CHECK-NEXT:        (tuple.extract 3 2
  ;; CHECK-NEXT:         (local.get $scratch_10)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:       (local.get $scratch_11)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_12)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (local.tee $scratch
  ;; CHECK-NEXT:     (block (result i32)
  ;; CHECK-NEXT:      (local.set $scratch_14
  ;; CHECK-NEXT:       (local.get $9)
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:      (local.set $3
  ;; CHECK-NEXT:       (block (result (ref $B))
  ;; CHECK-NEXT:        (local.set $scratch_13
  ;; CHECK-NEXT:         (ref.cast (ref $B)
  ;; CHECK-NEXT:          (local.get $4)
  ;; CHECK-NEXT:         )
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:        (local.set $7
  ;; CHECK-NEXT:         (local.get $8)
  ;; CHECK-NEXT:        )
  ;; CHECK-NEXT:        (local.get $scratch_13)
  ;; CHECK-NEXT:       )
  ;; CHECK-NEXT:      )
  ;; CHECK-NEXT:      (local.get $scratch_14)
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (local.set $temp
  ;; CHECK-NEXT:    (block (result (ref $B))
  ;; CHECK-NEXT:     (local.set $scratch_15
  ;; CHECK-NEXT:      (local.get $3)
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.set $5
  ;; CHECK-NEXT:      (local.get $7)
  ;; CHECK-NEXT:     )
  ;; CHECK-NEXT:     (local.get $scratch_15)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (unreachable)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $test-local-tuple-4-bad-dupes (param $B (ref $B)) (param $x i32) (result i32 anyref i32)
    (local $temp (tuple (ref $B) i32))
    ;; As above, but now the tuple has multiple appearances of the same type in
    ;; it, each of which needs its own scratch local. We can see in the output
    ;; that the tuple.extracts use different locals for the first and last i32.
    ;; For easier reading, here is the wami output of the binary:
    ;;
    ;;   (func $func4 (param $var0 (ref $type1)) (param $var1 i32) (result i32) (result anyref) (result i32)
    ;;     (local $var2 (ref $type1))
    ;;     (local $var3 (ref $type1))
    ;;     (local $var4 (ref $type0))
    ;;     (local $var5 i32)
    ;;     (local $var6 i32)
    ;;     (local $var7 i32)
    ;;     (local $var8 i32)
    ;;     (local $var9 i32)
    ;;     block $label0 (result i32) (result (ref $type0)) (result i32)
    ;;       i32.const -3
    ;;       local.get $var0
    ;;       i32.const 3
    ;;       local.get $var1
    ;;       br_if $label0
    ;;       local.set $var8 ;; saves 3
    ;;       local.set $var4 ;; saves the ref
    ;;       local.set $var9 ;; saves -3
    ;;       local.get $var9 ;; gets -3
    ;;       local.get $var4 ;; gets the ref
    ;;       ref.cast $type1 ;; casts the ref
    ;;       local.get $var8 ;; gets 3
    ;;       local.set $var7
    ;;       local.set $var3
    ;;       local.tee $var6
    ;;       drop
    ;;       local.get $var3
    ;;       local.get $var7
    ;;       local.set $var5
    ;;       local.set $var2
    ;;       unreachable
    ;;     end $label0
    ;;   )
    ;;
    (block $out (result i32 (ref $A) i32)
      (local.set $temp
        (br_if $out
          (tuple.make 3
            (i32.const -3) ;; this was added
            (local.get $B)
            (i32.const 3)
          )
          (local.get $x)
        )
      )
      (unreachable)
    )
  )
)