Skip to content

Commit 512dad6

Browse files
PR updates
1 parent 3192366 commit 512dad6

File tree

2 files changed

+152
-54
lines changed

2 files changed

+152
-54
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
;; RUN: foreach %s %t wasm-opt -all --closed-world --traps-never-happen --generate-global-effects --vacuum -S -o - | filecheck %s
3+
4+
(module
5+
;; CHECK: (type $nopType (func (param i32)))
6+
(type $nopType (func (param i32)))
7+
8+
;; CHECK: (func $nop (type $nopType) (param $0 i32)
9+
;; CHECK-NEXT: (nop)
10+
;; CHECK-NEXT: )
11+
(func $nop (export "nop") (type $nopType)
12+
(nop)
13+
)
14+
15+
;; CHECK: (func $calls-nop-via-nullable-ref (type $1) (param $ref (ref null $nopType))
16+
;; CHECK-NEXT: (call_ref $nopType
17+
;; CHECK-NEXT: (i32.const 1)
18+
;; CHECK-NEXT: (local.get $ref)
19+
;; CHECK-NEXT: )
20+
;; CHECK-NEXT: )
21+
(func $calls-nop-via-nullable-ref (param $ref (ref null $nopType))
22+
(call_ref $nopType (i32.const 1) (local.get $ref))
23+
)
24+
25+
;; CHECK: (func $f (type $1) (param $ref (ref null $nopType))
26+
;; CHECK-NEXT: (nop)
27+
;; CHECK-NEXT: )
28+
(func $f (param $ref (ref null $nopType))
29+
;; The only possible implementation of $nopType has no effects.
30+
;; $calls-nop-via-nullable-ref may trap from a null reference, but
31+
;; --traps-never-happen is enabled, so we're free to optimize this out.
32+
(call $calls-nop-via-nullable-ref (local.get $ref))
33+
)
34+
)

test/lit/passes/global-effects-closed-world.wast

Lines changed: 118 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,46 @@
6060
)
6161
)
6262

63+
;; Same as the above but with call_indirect
64+
(module
65+
;; CHECK: (type $nopType (func (param i32)))
66+
(type $nopType (func (param i32)))
67+
68+
(table 1 1 funcref)
69+
70+
;; CHECK: (func $nop (type $nopType) (param $0 i32)
71+
;; CHECK-NEXT: (nop)
72+
;; CHECK-NEXT: )
73+
(func $nop (export "nop") (type $nopType)
74+
(nop)
75+
)
76+
77+
;; CHECK: (func $calls-nop-via-ref (type $1)
78+
;; CHECK-NEXT: (call_indirect $0 (type $nopType)
79+
;; CHECK-NEXT: (i32.const 1)
80+
;; CHECK-NEXT: (i32.const 0)
81+
;; CHECK-NEXT: )
82+
;; CHECK-NEXT: )
83+
(func $calls-nop-via-ref
84+
;; This can only possibly be a nop in closed-world.
85+
;; Ideally vacuum could optimize this out but we don't have a way to share
86+
;; this information with other passes today.
87+
;; For now, we can at least annotate that the call to this function in $f
88+
;; has no effects.
89+
;; TODO: This call_ref could be marked as having no effects, like the call below.
90+
(call_indirect (type $nopType) (i32.const 1) (i32.const 0))
91+
)
92+
93+
;; CHECK: (func $f (type $1)
94+
;; CHECK-NEXT: (nop)
95+
;; CHECK-NEXT: )
96+
(func $f
97+
;; $calls-nop-via-ref has no effects because we determined that it can only
98+
;; call $nop. We can optimize this call out.
99+
(call $calls-nop-via-ref)
100+
)
101+
)
102+
63103
(module
64104
;; CHECK: (type $maybe-has-effects (func (param i32)))
65105
(type $maybe-has-effects (func (param i32)))
@@ -100,6 +140,47 @@
100140
)
101141
)
102142

143+
;; Same as above but with call_indirect
144+
(module
145+
(table 1 1 funcref)
146+
147+
;; CHECK: (type $maybe-has-effects (func (param i32)))
148+
(type $maybe-has-effects (func (param i32)))
149+
150+
;; CHECK: (func $unreachable (type $maybe-has-effects) (param $0 i32)
151+
;; CHECK-NEXT: (unreachable)
152+
;; CHECK-NEXT: )
153+
(func $unreachable (export "unreachable") (type $maybe-has-effects) (param i32)
154+
(unreachable)
155+
)
156+
157+
;; CHECK: (func $nop2 (type $maybe-has-effects) (param $0 i32)
158+
;; CHECK-NEXT: (nop)
159+
;; CHECK-NEXT: )
160+
(func $nop2 (export "nop2") (type $maybe-has-effects) (param i32)
161+
(nop)
162+
)
163+
164+
;; CHECK: (func $calls-effectful-function-via-ref (type $1)
165+
;; CHECK-NEXT: (call_indirect $0 (type $maybe-has-effects)
166+
;; CHECK-NEXT: (i32.const 1)
167+
;; CHECK-NEXT: (i32.const 1)
168+
;; CHECK-NEXT: )
169+
;; CHECK-NEXT: )
170+
(func $calls-effectful-function-via-ref
171+
(call_indirect (type $maybe-has-effects) (i32.const 1) (i32.const 1))
172+
)
173+
174+
;; CHECK: (func $f (type $1)
175+
;; CHECK-NEXT: (call $calls-effectful-function-via-ref)
176+
;; CHECK-NEXT: )
177+
(func $f
178+
;; This may be a nop or it may trap depending on the ref.
179+
;; We don't know so don't optimize it out.
180+
(call $calls-effectful-function-via-ref)
181+
)
182+
)
183+
103184
(module
104185
;; CHECK: (type $uninhabited (func (param i32)))
105186
(type $uninhabited (func (param i32)))
@@ -150,105 +231,88 @@
150231
)
151232

152233
(module
153-
;; CHECK: (type $super (sub (struct)))
154-
(type $super (sub (struct)))
155-
;; CHECK: (type $sub (sub $super (struct)))
156-
(type $sub (sub $super (struct)))
157-
158-
;; Supertype
159-
;; CHECK: (type $func-with-sub-param (sub (func (param (ref $sub)))))
160-
(type $func-with-sub-param (sub (func (param (ref $sub)))))
234+
;; CHECK: (type $super (sub (func)))
235+
(type $super (sub (func)))
161236
;; Subtype
162-
;; CHECK: (type $func-with-super-param (sub $func-with-sub-param (func (param (ref $super)))))
163-
(type $func-with-super-param (sub $func-with-sub-param (func (param (ref $super)))))
237+
;; CHECK: (type $sub (sub $super (func)))
238+
(type $sub (sub $super (func)))
164239

165-
;; CHECK: (func $nop-with-supertype (type $func-with-sub-param) (param $0 (ref $sub))
240+
;; CHECK: (func $nop-with-supertype (type $super)
166241
;; CHECK-NEXT: (nop)
167242
;; CHECK-NEXT: )
168-
(func $nop-with-supertype (export "nop-with-supertype") (type $func-with-sub-param) (param (ref $sub))
243+
(func $nop-with-supertype (export "nop-with-supertype") (type $super)
169244
)
170245

171-
;; CHECK: (func $effectful-with-subtype (type $func-with-super-param) (param $0 (ref $super))
246+
;; CHECK: (func $effectful-with-subtype (type $sub)
172247
;; CHECK-NEXT: (unreachable)
173248
;; CHECK-NEXT: )
174-
(func $effectful-with-subtype (export "effectful-with-subtype") (type $func-with-super-param) (param (ref $super))
249+
(func $effectful-with-subtype (export "effectful-with-subtype") (type $sub)
175250
(unreachable)
176251
)
177252

178-
;; CHECK: (func $calls-ref-with-subtype (type $3) (param $func (ref $func-with-sub-param)) (param $sub (ref $sub))
179-
;; CHECK-NEXT: (call_ref $func-with-sub-param
180-
;; CHECK-NEXT: (local.get $sub)
253+
;; CHECK: (func $calls-ref-with-supertype (type $1) (param $func (ref $super))
254+
;; CHECK-NEXT: (call_ref $super
181255
;; CHECK-NEXT: (local.get $func)
182256
;; CHECK-NEXT: )
183257
;; CHECK-NEXT: )
184-
(func $calls-ref-with-subtype (param $func (ref $func-with-sub-param)) (param $sub (ref $sub))
185-
(call_ref $func-with-sub-param (local.get $sub) (local.get $func))
258+
(func $calls-ref-with-supertype (param $func (ref $super))
259+
(call_ref $super (local.get $func))
186260
)
187261

188-
;; CHECK: (func $f (type $3) (param $func (ref $func-with-sub-param)) (param $sub (ref $sub))
189-
;; CHECK-NEXT: (call $calls-ref-with-subtype
262+
;; CHECK: (func $f (type $1) (param $func (ref $super))
263+
;; CHECK-NEXT: (call $calls-ref-with-supertype
190264
;; CHECK-NEXT: (local.get $func)
191-
;; CHECK-NEXT: (local.get $sub)
192265
;; CHECK-NEXT: )
193266
;; CHECK-NEXT: )
194-
(func $f (param $func (ref $func-with-sub-param)) (param $sub (ref $sub))
267+
(func $f (param $func (ref $super))
195268
;; Check that we account for subtyping correctly.
196-
;; The type $func-with-sub-param (the supertype) has no effects (i.e. the
197-
;; union of all effects of functions with this type is empty).
198-
;; However, a subtype of $func-with-sub-param ($func-with-super-param) does
199-
;; have effects, and we can call_ref with that subtype, so we need to
200-
;; include the unreachable effect and we can't optimize out this call.
201-
(call $calls-ref-with-subtype (local.get $func) (local.get $sub))
269+
;; $super has no effects (i.e. the union of all effects of functions with
270+
;; this type is empty). However, $sub does have effects, and we can call_ref
271+
;; with that subtype, so we need to include the unreachable effect and we
272+
;; can't optimize out this call.
273+
(call $calls-ref-with-supertype (local.get $func))
202274
)
203275
)
204276

205277
;; Same as above but this time our reference is the exact supertype
206278
;; so we know not to aggregate effects from the subtype.
207279
;; TODO: this case doesn't optimize today. Add exact ref support in the pass.
208280
(module
209-
;; CHECK: (type $super (sub (struct)))
210-
(type $super (sub (struct)))
211-
;; CHECK: (type $sub (sub $super (struct)))
212-
(type $sub (sub $super (struct)))
213-
214-
;; Supertype
215-
;; CHECK: (type $func-with-sub-param (sub (func (param (ref $sub)))))
216-
(type $func-with-sub-param (sub (func (param (ref $sub)))))
217-
;; Subtype
218-
;; CHECK: (type $func-with-super-param (sub $func-with-sub-param (func (param (ref $super)))))
219-
(type $func-with-super-param (sub $func-with-sub-param (func (param (ref $super)))))
281+
;; CHECK: (type $super (sub (func)))
282+
(type $super (sub (func)))
283+
284+
;; CHECK: (type $sub (sub $super (func)))
285+
(type $sub (sub $super (func)))
220286

221-
;; CHECK: (func $nop-with-supertype (type $func-with-sub-param) (param $0 (ref $sub))
287+
;; CHECK: (func $nop-with-supertype (type $super)
222288
;; CHECK-NEXT: (nop)
223289
;; CHECK-NEXT: )
224-
(func $nop-with-supertype (export "nop-with-supertype") (type $func-with-sub-param) (param (ref $sub))
290+
(func $nop-with-supertype (export "nop-with-supertype") (type $super)
225291
)
226292

227-
;; CHECK: (func $effectful-with-subtype (type $func-with-super-param) (param $0 (ref $super))
293+
;; CHECK: (func $effectful-with-subtype (type $sub)
228294
;; CHECK-NEXT: (unreachable)
229295
;; CHECK-NEXT: )
230-
(func $effectful-with-subtype (export "effectful-with-subtype") (type $func-with-super-param) (param (ref $super))
296+
(func $effectful-with-subtype (export "effectful-with-subtype") (type $sub)
231297
(unreachable)
232298
)
233299

234-
;; CHECK: (func $calls-ref-with-subtype (type $3) (param $func (ref (exact $func-with-sub-param))) (param $sub (ref $sub))
235-
;; CHECK-NEXT: (call_ref $func-with-sub-param
236-
;; CHECK-NEXT: (local.get $sub)
300+
;; CHECK: (func $calls-ref-with-supertype (type $1) (param $func (ref (exact $super)))
301+
;; CHECK-NEXT: (call_ref $super
237302
;; CHECK-NEXT: (local.get $func)
238303
;; CHECK-NEXT: )
239304
;; CHECK-NEXT: )
240-
(func $calls-ref-with-subtype (param $func (ref (exact $func-with-sub-param))) (param $sub (ref $sub))
241-
(call_ref $func-with-sub-param (local.get $sub) (local.get $func))
305+
(func $calls-ref-with-supertype (param $func (ref (exact $super)))
306+
(call_ref $super (local.get $func))
242307
)
243308

244-
;; CHECK: (func $f (type $3) (param $func (ref (exact $func-with-sub-param))) (param $sub (ref $sub))
245-
;; CHECK-NEXT: (call $calls-ref-with-subtype
309+
;; CHECK: (func $f (type $1) (param $func (ref (exact $super)))
310+
;; CHECK-NEXT: (call $calls-ref-with-supertype
246311
;; CHECK-NEXT: (local.get $func)
247-
;; CHECK-NEXT: (local.get $sub)
248312
;; CHECK-NEXT: )
249313
;; CHECK-NEXT: )
250-
(func $f (param $func (ref (exact $func-with-sub-param))) (param $sub (ref $sub))
251-
(call $calls-ref-with-subtype (local.get $func) (local.get $sub))
314+
(func $f (param $func (ref (exact $super)))
315+
(call $calls-ref-with-supertype (local.get $func))
252316
)
253317
)
254318

0 commit comments

Comments
 (0)