summaryrefslogtreecommitdiff
path: root/test/lit/passes/gto_and_cfp_in_O.wast
blob: 9d385d0f9697c2dd7d2c6aa4fa695030f42c155d (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
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; RUN: foreach %s %t wasm-opt -O -all --nominal --closed-world -S -o - | filecheck %s
;; RUN: foreach %s %t wasm-opt -O -all --nominal                -S -o - | filecheck %s --check-prefix OPEN_WORLD

;; Test that -O, with nominal typing + GC enabled, will run global type
;; optimization in conjunction with constant field propagation etc. But, in an
;; open world we do not run them.

(module
  ;; OPEN_WORLD:      (type $struct (struct (field (mut funcref)) (field (mut i32))))
  (type $struct (struct_subtype (field (mut funcref)) (field (mut i32)) data))

  ;; OPEN_WORLD:      (type $none_=>_none (func))

  ;; OPEN_WORLD:      (type $none_=>_i32 (func (result i32)))

  ;; OPEN_WORLD:      (global $glob (ref $struct) (struct.new $struct
  ;; OPEN_WORLD-NEXT:  (ref.func $by-ref)
  ;; OPEN_WORLD-NEXT:  (i32.const 100)
  ;; OPEN_WORLD-NEXT: ))
  (global $glob (ref $struct) (struct.new $struct
    (ref.func $by-ref)
    (i32.const 100)
  ))

  ;; OPEN_WORLD:      (export "main" (func $main))

  ;; OPEN_WORLD:      (func $by-ref (type $none_=>_none) (; has Stack IR ;)
  ;; OPEN_WORLD-NEXT:  (struct.set $struct 1
  ;; OPEN_WORLD-NEXT:   (global.get $glob)
  ;; OPEN_WORLD-NEXT:   (i32.const 200)
  ;; OPEN_WORLD-NEXT:  )
  ;; OPEN_WORLD-NEXT: )
  (func $by-ref
    ;; This function is kept alive by the reference in $glob. After we remove
    ;; the field that the funcref is written to, we remove the funcref, which
    ;; means this function can be removed.
    ;;
    ;; Once it is removed, this write no longer exists, and does not hamper
    ;; constant field propagation from inferring the value of the i32 field.
    (struct.set $struct 1
      (global.get $glob)
      (i32.const 200)
    )
  )

  ;; CHECK:      (type $none_=>_i32 (func (result i32)))

  ;; CHECK:      (export "main" (func $main))

  ;; CHECK:      (func $main (type $none_=>_i32) (; has Stack IR ;) (result i32)
  ;; CHECK-NEXT:  (i32.const 100)
  ;; CHECK-NEXT: )
  ;; OPEN_WORLD:      (func $main (type $none_=>_i32) (; has Stack IR ;) (result i32)
  ;; OPEN_WORLD-NEXT:  (struct.get $struct 1
  ;; OPEN_WORLD-NEXT:   (global.get $glob)
  ;; OPEN_WORLD-NEXT:  )
  ;; OPEN_WORLD-NEXT: )
  (func $main (export "main") (result i32)
    ;; After all the above optimizations, we can infer that $main should simply
    ;; return 100.
    (struct.get $struct 1
      (global.get $glob)
    )
  )
)