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
|
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.
;; RUN: foreach %s %t wasm-opt -all --simplify-globals -S -o - | filecheck %s
;; $single-use has a single use, and we can fold a copy of that code into its
;; use. That global then becomes unused.
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (type $1 (func))
;; CHECK: (global $single-use anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use anyref (struct.new $A
(ref.i31
(i32.const 42)
)
))
;; CHECK: (global $other (mut anyref) (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other (mut anyref) (global.get $single-use))
;; CHECK: (func $user (type $1)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $other)
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $other
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $user
;; Uses of $other do not affect optimization
(drop
(global.get $other)
)
(global.set $other
(ref.null any)
)
)
)
;; As above, but now there is a second use, so we do nothing.
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (global $single-use anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use anyref (struct.new $A
(ref.i31
(i32.const 42)
)
))
;; CHECK: (global $other anyref (global.get $single-use))
(global $other anyref (global.get $single-use))
;; CHECK: (global $other2 anyref (global.get $single-use))
(global $other2 anyref (global.get $single-use))
)
;; As the first testcase, but now there is a second use in function code, so
;; again we do nothing.
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (type $1 (func))
;; CHECK: (global $single-use anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use anyref (struct.new $A
(ref.i31
(i32.const 42)
)
))
;; CHECK: (global $other anyref (global.get $single-use))
(global $other anyref (global.get $single-use))
;; CHECK: (func $user (type $1)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $single-use)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $user
(drop
(global.get $single-use)
)
)
)
;; As the first testcase, but now $single-use is imported, so there is no code
;; to fold.
(module
(type $A (struct (field anyref)))
;; CHECK: (import "a" "b" (global $single-use anyref))
(import "a" "b" (global $single-use anyref))
;; CHECK: (global $other anyref (global.get $single-use))
(global $other anyref (global.get $single-use))
)
;; As the first testcase, but here the single use is in function code, so
;; we do nothing (as it could be executed more than once).
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (type $1 (func))
;; CHECK: (global $single-use anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use anyref (struct.new $A
(ref.i31
(i32.const 42)
)
))
;; CHECK: (func $user (type $1)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $single-use)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $user
(drop
(global.get $single-use)
)
)
)
;; As the first testcase, but now the single use is nested in other code. We
;; can still optimize.
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (global $single-use anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use anyref (struct.new $A
(ref.i31
(i32.const 42)
)
))
;; CHECK: (global $other anyref (struct.new $A
;; CHECK-NEXT: (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other anyref (struct.new $A
(global.get $single-use)
))
)
;; Test multiple separate optimizations in one module.
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (global $single-use1 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use1 anyref (struct.new $A
(ref.i31
(i32.const 42)
)
))
;; CHECK: (global $single-use2 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1337)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use2 anyref (struct.new $A
(ref.i31
(i32.const 1337)
)
))
;; CHECK: (global $other1 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other1 anyref (global.get $single-use1))
;; CHECK: (global $single-use3 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 99999)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use3 anyref (struct.new $A
(ref.i31
(i32.const 99999)
)
))
;; CHECK: (global $other2 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1337)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other2 anyref (global.get $single-use2))
;; CHECK: (global $other3 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 99999)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other3 anyref (global.get $single-use3))
)
;; Test multiple related optimizations in one module: more than one nesting into
;; one global
(module
;; CHECK: (type $A (struct (field anyref) (field anyref)))
(type $A (struct (field anyref) (field anyref)))
;; CHECK: (global $single-use1 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: ))
(global $single-use1 anyref (struct.new $A
(ref.i31
(i32.const 42)
)
(ref.null any)
))
;; CHECK: (global $single-use2 anyref (struct.new $A
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1337)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $single-use2 anyref (struct.new $A
(ref.null any)
(ref.i31
(i32.const 1337)
)
))
;; CHECK: (global $other anyref (struct.new $A
;; CHECK-NEXT: (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.new $A
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 1337)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other anyref (struct.new $A
(global.get $single-use1)
(global.get $single-use2)
))
)
;; Test multiple related optimizations in one module: a chain.
(module
;; CHECK: (type $A (struct (field anyref)))
(type $A (struct (field anyref)))
;; CHECK: (global $single-use1 anyref (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: ))
(global $single-use1 anyref (ref.i31
(i32.const 42)
))
;; CHECK: (global $other1 anyref (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other1 anyref (struct.new $A
(global.get $single-use1)
))
;; CHECK: (global $other2 anyref (struct.new $A
;; CHECK-NEXT: (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other2 anyref (struct.new $A
(global.get $other1)
))
;; CHECK: (global $other3 anyref (struct.new $A
;; CHECK-NEXT: (struct.new $A
;; CHECK-NEXT: (ref.i31
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $other3 anyref (global.get $other2))
)
|