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
|
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --simplify-locals -all -S -o - \
;; RUN: | filecheck %s
;; RUN: wasm-opt %s --simplify-locals -all --nominal -S -o - \
;; RUN: | filecheck %s --check-prefix=NOMNL
(module
;; CHECK: (type $struct (struct (field (mut i32))))
;; NOMNL: (type $struct (struct_subtype (field (mut i32)) data))
(type $struct (struct (field (mut i32))))
;; CHECK: (type $struct-immutable (struct (field i32)))
;; NOMNL: (type $struct-immutable (struct_subtype (field i32) data))
(type $struct-immutable (struct (field i32)))
;; CHECK: (type $B (struct (field dataref)))
;; CHECK: (type $A (struct (field (ref null data))))
;; NOMNL: (type $A (struct_subtype (field (ref null data)) data))
(type $A (struct_subtype (field (ref null data)) data))
;; $B is a subtype of $A, and its field has a more refined type (it is non-
;; nullable).
;; NOMNL: (type $B (struct_subtype (field dataref) $A))
(type $B (struct_subtype (field (ref data)) $A))
;; Writes to heap objects cannot be reordered with reads.
;; CHECK: (func $no-reorder-past-write (param $x (ref $struct)) (result i32)
;; CHECK-NEXT: (local $temp i32)
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (struct.get $struct 0
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.set $struct 0
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; NOMNL: (func $no-reorder-past-write (type $ref|$struct|_=>_i32) (param $x (ref $struct)) (result i32)
;; NOMNL-NEXT: (local $temp i32)
;; NOMNL-NEXT: (local.set $temp
;; NOMNL-NEXT: (struct.get $struct 0
;; NOMNL-NEXT: (local.get $x)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (struct.set $struct 0
;; NOMNL-NEXT: (local.get $x)
;; NOMNL-NEXT: (i32.const 42)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (local.get $temp)
;; NOMNL-NEXT: )
(func $no-reorder-past-write (param $x (ref $struct)) (result i32)
(local $temp i32)
(local.set $temp
(struct.get $struct 0
(local.get $x)
)
)
(struct.set $struct 0
(local.get $x)
(i32.const 42)
)
(local.get $temp)
)
;; CHECK: (func $reorder-past-write-if-immutable (param $x (ref $struct)) (param $y (ref $struct-immutable)) (result i32)
;; CHECK-NEXT: (local $temp i32)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (struct.set $struct 0
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.get $struct-immutable 0
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; NOMNL: (func $reorder-past-write-if-immutable (type $ref|$struct|_ref|$struct-immutable|_=>_i32) (param $x (ref $struct)) (param $y (ref $struct-immutable)) (result i32)
;; NOMNL-NEXT: (local $temp i32)
;; NOMNL-NEXT: (nop)
;; NOMNL-NEXT: (struct.set $struct 0
;; NOMNL-NEXT: (local.get $x)
;; NOMNL-NEXT: (i32.const 42)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (struct.get $struct-immutable 0
;; NOMNL-NEXT: (local.get $y)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
(func $reorder-past-write-if-immutable (param $x (ref $struct)) (param $y (ref $struct-immutable)) (result i32)
(local $temp i32)
(local.set $temp
(struct.get $struct-immutable 0
(local.get $y)
)
)
(struct.set $struct 0
(local.get $x)
(i32.const 42)
)
(local.get $temp)
)
;; CHECK: (func $unreachable-struct.get (param $x (ref $struct)) (param $y (ref $struct-immutable)) (result i32)
;; CHECK-NEXT: (local $temp i32)
;; CHECK-NEXT: (local.tee $temp
;; CHECK-NEXT: (block ;; (replaces something unreachable we can't emit)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.set $struct 0
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; NOMNL: (func $unreachable-struct.get (type $ref|$struct|_ref|$struct-immutable|_=>_i32) (param $x (ref $struct)) (param $y (ref $struct-immutable)) (result i32)
;; NOMNL-NEXT: (local $temp i32)
;; NOMNL-NEXT: (local.tee $temp
;; NOMNL-NEXT: (block ;; (replaces something unreachable we can't emit)
;; NOMNL-NEXT: (drop
;; NOMNL-NEXT: (unreachable)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (struct.set $struct 0
;; NOMNL-NEXT: (local.get $x)
;; NOMNL-NEXT: (i32.const 42)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (local.get $temp)
;; NOMNL-NEXT: )
(func $unreachable-struct.get (param $x (ref $struct)) (param $y (ref $struct-immutable)) (result i32)
(local $temp i32)
;; As above, but the get's ref is unreachable. This tests we do not hit an
;; assertion on the get's type not having a heap type (as we depend on
;; finding the heap type there in the reachable case).
;; We simply do not handle this case, leaving it for DCE.
(local.set $temp
(struct.get $struct-immutable 0
(unreachable)
)
)
(struct.set $struct 0
(local.get $x)
(i32.const 42)
)
(local.get $temp)
)
;; CHECK: (func $no-block-values-if-br_on
;; CHECK-NEXT: (local $temp anyref)
;; CHECK-NEXT: (block $block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (br_on_null $block
;; CHECK-NEXT: (ref.null any)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (ref.null any)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $block)
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (ref.null any)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; NOMNL: (func $no-block-values-if-br_on (type $none_=>_none)
;; NOMNL-NEXT: (local $temp anyref)
;; NOMNL-NEXT: (block $block
;; NOMNL-NEXT: (drop
;; NOMNL-NEXT: (br_on_null $block
;; NOMNL-NEXT: (ref.null any)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (local.set $temp
;; NOMNL-NEXT: (ref.null any)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (br $block)
;; NOMNL-NEXT: (local.set $temp
;; NOMNL-NEXT: (ref.null any)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (drop
;; NOMNL-NEXT: (ref.as_non_null
;; NOMNL-NEXT: (local.get $temp)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
(func $no-block-values-if-br_on
(local $temp (ref null any))
(block $block
(drop
;; This br_on should inhibit trying to create a block return value for
;; this block. Aside from the br_on, it looks correct, i.e., we have a
;; break with a set before it, and a set before the end of the block. Due
;; to the br_on's presence, the pass should not do anything to this
;; function.
;;
;; TODO: support br_on in this optimization eventually, but the variable
;; possible return values and sent values make that nontrivial.
(br_on_null $block
(ref.null any)
)
)
(local.set $temp
(ref.null any)
)
(br $block)
(local.set $temp
(ref.null any)
)
)
;; Attempt to use the local that the pass will try to move to a block return
;; value, to cause the optimization to try to run.
(drop
(ref.as_non_null
(local.get $temp)
)
)
)
;; CHECK: (func $needs-refinalize (param $b (ref $B)) (result anyref)
;; CHECK-NEXT: (local $a (ref null $A))
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (struct.get $B 0
;; CHECK-NEXT: (local.get $b)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; NOMNL: (func $needs-refinalize (type $ref|$B|_=>_anyref) (param $b (ref $B)) (result anyref)
;; NOMNL-NEXT: (local $a (ref null $A))
;; NOMNL-NEXT: (nop)
;; NOMNL-NEXT: (struct.get $B 0
;; NOMNL-NEXT: (local.get $b)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
(func $needs-refinalize (param $b (ref $B)) (result anyref)
(local $a (ref null $A))
(local.set $a
(local.get $b)
)
;; This begins as a struct.get of $A, but after we move the set's value onto
;; the get, we'll be reading from $B. $B's field has a more refined type, so
;; we must update the type of the struct.get using refinalize.
(struct.get $A 0
(local.get $a)
)
)
)
|