blob: 224e53d4050911a3b71ebb70cf23702e3e892783 (
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
|
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: foreach %s %t wasm-opt --optimize-instructions --ignore-implicit-traps --enable-reference-types --enable-gc -S -o - \
;; RUN: | filecheck %s
;; Also test trapsNeverHappen
;; RUN: foreach %s %t wasm-opt --optimize-instructions --traps-never-happen --enable-reference-types --enable-gc -S -o - \
;; RUN: | filecheck %s --check-prefix TNH
(module
;; CHECK: (type $parent (sub (struct (field i32))))
;; TNH: (type $parent (sub (struct (field i32))))
(type $parent (sub (struct (field i32))))
;; CHECK: (type $child (sub $parent (struct (field i32) (field f64))))
;; TNH: (type $child (sub $parent (struct (field i32) (field f64))))
(type $child (sub $parent (struct (field i32) (field f64))))
;; CHECK: (type $other (struct (field i64) (field f32)))
;; TNH: (type $other (struct (field i64) (field f32)))
(type $other (struct (field i64) (field f32)))
;; CHECK: (func $foo (type $3)
;; CHECK-NEXT: )
;; TNH: (func $foo (type $3)
;; TNH-NEXT: )
(func $foo)
;; CHECK: (func $ref-cast-iit (type $4) (param $parent (ref $parent)) (param $child (ref $child)) (param $other (ref $other))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $parent)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $child)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref $child)
;; CHECK-NEXT: (local.get $parent)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref none))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $child)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; TNH: (func $ref-cast-iit (type $4) (param $parent (ref $parent)) (param $child (ref $child)) (param $other (ref $other))
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $parent)
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $child)
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.cast (ref $child)
;; TNH-NEXT: (local.get $parent)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (block (result (ref none))
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $child)
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: )
(func $ref-cast-iit
(param $parent (ref $parent))
(param $child (ref $child))
(param $other (ref $other))
;; a cast of parent to parent. We can optimize this as the new type will be
;; valid.
(drop
(ref.cast (ref $parent)
(local.get $parent)
)
)
;; a cast of child to a supertype: again, we replace with a valid type.
(drop
(ref.cast (ref $parent)
(local.get $child)
)
)
;; a cast of parent to a subtype: we cannot replace the original heap type
;; $child with one that is not equal or more specific, like $parent, so we
;; cannot optimize here.
(drop
(ref.cast (ref $child)
(local.get $parent)
)
)
;; a cast of child to an unrelated type: it will trap anyhow
(drop
(ref.cast (ref $other)
(local.get $child)
)
)
)
;; CHECK: (func $ref-cast-iit-bad (type $5) (param $parent (ref $parent))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref $parent))
;; CHECK-NEXT: (call $foo)
;; CHECK-NEXT: (local.get $parent)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; 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: )
;; CHECK-NEXT: )
;; TNH: (func $ref-cast-iit-bad (type $5) (param $parent (ref $parent))
;; TNH-NEXT: (drop
;; TNH-NEXT: (block (result (ref $parent))
;; TNH-NEXT: (call $foo)
;; TNH-NEXT: (local.get $parent)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (block ;; (replaces unreachable RefCast we can't emit)
;; TNH-NEXT: (drop
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: )
(func $ref-cast-iit-bad
(param $parent (ref $parent))
;; optimizing this cast away requires reordering.
(drop
(ref.cast (ref $parent)
(block (result (ref $parent))
(call $foo)
(local.get $parent)
)
)
)
;; ignore unreachability
(drop
(ref.cast (ref null $parent)
(unreachable)
)
)
)
;; CHECK: (func $ref-eq-ref-cast (type $6) (param $x eqref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; TNH: (func $ref-eq-ref-cast (type $6) (param $x eqref)
;; TNH-NEXT: (drop
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: )
(func $ref-eq-ref-cast (param $x eqref)
;; we can look through a ref.cast null if we ignore traps
(drop
(ref.eq
(local.get $x)
(ref.cast (ref null $parent)
(local.get $x)
)
)
)
)
;; CHECK: (func $set-of-as-non-null (type $7) (param $x anyref)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; TNH: (func $set-of-as-non-null (type $7) (param $x anyref)
;; TNH-NEXT: (local.set $x
;; TNH-NEXT: (local.get $x)
;; TNH-NEXT: )
;; TNH-NEXT: )
(func $set-of-as-non-null (param $x anyref)
;; As we ignore such traps, we can remove the ref.as here.
(local.set $x
(ref.as_non_null
(local.get $x)
)
)
)
)
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $A (sub (struct)))
;; TNH: (rec
;; TNH-NEXT: (type $A (sub (struct)))
(type $A (sub (struct )))
;; CHECK: (type $B (sub $A (struct (field (ref null $A)))))
;; TNH: (type $B (sub $A (struct (field (ref null $A)))))
(type $B (sub $A (struct (field (ref null $A)))))
;; CHECK: (type $C (sub $B (struct (field (ref null $D)))))
;; TNH: (type $C (sub $B (struct (field (ref null $D)))))
(type $C (sub $B (struct (field (ref null $D)))))
;; CHECK: (type $D (sub $A (struct)))
;; TNH: (type $D (sub $A (struct)))
(type $D (sub $A (struct )))
)
;; CHECK: (func $test (type $4) (param $C (ref $C)) (result anyref)
;; CHECK-NEXT: (struct.get $C 0
;; CHECK-NEXT: (local.get $C)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; TNH: (func $test (type $4) (param $C (ref $C)) (result anyref)
;; TNH-NEXT: (struct.get $C 0
;; TNH-NEXT: (local.get $C)
;; TNH-NEXT: )
;; TNH-NEXT: )
(func $test (param $C (ref $C)) (result anyref)
(struct.get $B 0
(ref.cast (ref $B) ;; Try to cast a $C to its parent, $B. That always
;; works, so the cast can be removed.
;; Then once the cast is removed, the outer struct.get
;; will have a reference with a different type,
;; making it a (struct.get $C ..) instead of $B.
;; But $B and $C have different types on field 0, and
;; so the struct.get must be refinalized so the node
;; has the expected type.
(local.get $C)
)
)
)
)
|