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 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: foreach %s %t wasm-opt --string-gathering -all -S -o - | filecheck %s
;; RUN: foreach %s %t wasm-opt --string-lowering -all -S -o - | filecheck %s --check-prefix=LOWER
;; All the strings should be collected into globals and used from there. They
;; should also be sorted deterministically (alphabetically).
;;
;; LOWER also lowers away strings entirely, leaving only imports and a custom
;; section (that part is tested in string-lowering.wast). It also removes all
;; uses of the string heap type, leaving extern instead for the imported
;; strings.
(module
;; Note that $global will be reused: no new global will be added for "foo".
;; $global2 almost can, but it has the wrong type, so it won't.
;; CHECK: (type $0 (func))
;; CHECK: (global $global (ref string) (string.const "foo"))
(global $global (ref string) (string.const "foo"))
;; CHECK: (global $"string.const_\"bar\"" (ref string) (string.const "bar"))
;; CHECK: (global $"string.const_\"other\"" (ref string) (string.const "other"))
;; CHECK: (global $global2 stringref (global.get $"string.const_\"bar\""))
;; LOWER: (type $0 (array (mut i16)))
;; LOWER: (type $1 (func))
;; LOWER: (type $2 (func (param externref externref) (result i32)))
;; LOWER: (type $3 (func (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (type $4 (func (param i32) (result (ref extern))))
;; LOWER: (type $5 (func (param externref externref) (result (ref extern))))
;; LOWER: (type $6 (func (param externref (ref null $0) i32) (result i32)))
;; LOWER: (type $7 (func (param externref) (result i32)))
;; LOWER: (type $8 (func (param externref i32) (result i32)))
;; LOWER: (type $9 (func (param externref i32 i32) (result (ref extern))))
;; LOWER: (import "string.const" "0" (global $global (ref extern)))
;; LOWER: (import "string.const" "1" (global $"string.const_\"bar\"" (ref extern)))
;; LOWER: (import "string.const" "2" (global $"string.const_\"other\"" (ref extern)))
;; LOWER: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $3) (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $4) (param i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "concat" (func $concat (type $5) (param externref externref) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $6) (param externref (ref null $0) i32) (result i32)))
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $2) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $2) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "length" (func $length (type $7) (param externref) (result i32)))
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $8) (param externref i32) (result i32)))
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $9) (param externref i32 i32) (result (ref extern))))
;; LOWER: (global $global2 externref (global.get $"string.const_\"bar\""))
(global $global2 (ref null string) (string.const "bar"))
;; CHECK: (func $a (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $"string.const_\"bar\"")
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $global)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; LOWER: (func $a (type $1)
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $"string.const_\"bar\"")
;; LOWER-NEXT: )
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $global)
;; LOWER-NEXT: )
;; LOWER-NEXT: )
(func $a
(drop
(string.const "bar")
)
(drop
(string.const "foo")
)
)
;; CHECK: (func $b (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $"string.const_\"bar\"")
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $"string.const_\"other\"")
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $global)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $global2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; LOWER: (func $b (type $1)
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $"string.const_\"bar\"")
;; LOWER-NEXT: )
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $"string.const_\"other\"")
;; LOWER-NEXT: )
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $global)
;; LOWER-NEXT: )
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $global2)
;; LOWER-NEXT: )
;; LOWER-NEXT: )
(func $b
(drop
(string.const "bar")
)
(drop
(string.const "other")
)
;; Existing global.gets are not modified (but after this pass,
;; SimplifyGlobals could help; though in practice the globals would have
;; been propagated here anyhow).
(drop
(global.get $global)
)
(drop
(global.get $global2)
)
)
)
;; Multiple possible reusable globals. Also test ignoring of imports.
(module
;; CHECK: (import "a" "b" (global $import (ref string)))
;; LOWER: (type $0 (array (mut i16)))
;; LOWER: (type $1 (func (param externref externref) (result i32)))
;; LOWER: (type $2 (func (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (type $3 (func (param i32) (result (ref extern))))
;; LOWER: (type $4 (func (param externref externref) (result (ref extern))))
;; LOWER: (type $5 (func (param externref (ref null $0) i32) (result i32)))
;; LOWER: (type $6 (func (param externref) (result i32)))
;; LOWER: (type $7 (func (param externref i32) (result i32)))
;; LOWER: (type $8 (func (param externref i32 i32) (result (ref extern))))
;; LOWER: (import "string.const" "0" (global $global1 (ref extern)))
;; LOWER: (import "string.const" "1" (global $global4 (ref extern)))
;; LOWER: (import "a" "b" (global $import (ref extern)))
(import "a" "b" (global $import (ref string)))
;; CHECK: (global $global1 (ref string) (string.const "foo"))
(global $global1 (ref string) (string.const "foo"))
;; CHECK: (global $global4 (ref string) (string.const "bar"))
;; CHECK: (global $global2 (ref string) (global.get $global1))
;; LOWER: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $2) (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $3) (param i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "concat" (func $concat (type $4) (param externref externref) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $5) (param externref (ref null $0) i32) (result i32)))
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $1) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $1) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "length" (func $length (type $6) (param externref) (result i32)))
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $7) (param externref i32) (result i32)))
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $8) (param externref i32 i32) (result (ref extern))))
;; LOWER: (global $global2 (ref extern) (global.get $global1))
(global $global2 (ref string) (string.const "foo"))
;; CHECK: (global $global3 (ref string) (global.get $global1))
;; LOWER: (global $global3 (ref extern) (global.get $global1))
(global $global3 (ref string) (string.const "foo"))
(global $global4 (ref string) (string.const "bar"))
;; CHECK: (global $global5 (ref string) (global.get $global4))
;; LOWER: (global $global5 (ref extern) (global.get $global4))
(global $global5 (ref string) (string.const "bar"))
;; CHECK: (global $global6 (ref string) (global.get $global4))
;; LOWER: (global $global6 (ref extern) (global.get $global4))
(global $global6 (ref string) (string.const "bar"))
)
;; Mutable globals cannot be reused: we will create a new global for "foo".
(module
;; CHECK: (type $0 (func))
;; CHECK: (global $"string.const_\"foo\"" (ref string) (string.const "foo"))
;; CHECK: (global $global (mut (ref string)) (global.get $"string.const_\"foo\""))
;; LOWER: (type $0 (array (mut i16)))
;; LOWER: (type $1 (func (param externref externref) (result i32)))
;; LOWER: (type $2 (func))
;; LOWER: (type $3 (func (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (type $4 (func (param i32) (result (ref extern))))
;; LOWER: (type $5 (func (param externref externref) (result (ref extern))))
;; LOWER: (type $6 (func (param externref (ref null $0) i32) (result i32)))
;; LOWER: (type $7 (func (param externref) (result i32)))
;; LOWER: (type $8 (func (param externref i32) (result i32)))
;; LOWER: (type $9 (func (param externref i32 i32) (result (ref extern))))
;; LOWER: (import "string.const" "0" (global $"string.const_\"foo\"" (ref extern)))
;; LOWER: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $3) (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $4) (param i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "concat" (func $concat (type $5) (param externref externref) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $6) (param externref (ref null $0) i32) (result i32)))
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $1) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $1) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "length" (func $length (type $7) (param externref) (result i32)))
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $8) (param externref i32) (result i32)))
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $9) (param externref i32 i32) (result (ref extern))))
;; LOWER: (global $global (mut (ref extern)) (global.get $"string.const_\"foo\""))
(global $global (mut (ref string)) (string.const "foo"))
;; CHECK: (func $a (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $"string.const_\"foo\"")
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; LOWER: (func $a (type $2)
;; LOWER-NEXT: (drop
;; LOWER-NEXT: (global.get $"string.const_\"foo\"")
;; LOWER-NEXT: )
;; LOWER-NEXT: )
(func $a
(drop
(string.const "foo")
)
)
)
;; A module where a string (in this case the empty string) appears twice, so we
;; will use a single global for both. The first use of the string appears in a
;; nested position, inside a struct constructor, so we cannot use that one as
;; our defining global, but there is an appropriate global after it. We must be
;; careful to then sort the globals, as $string must then appear before $struct.
(module
;; CHECK: (type $struct (struct (field stringref)))
;; LOWER: (type $0 (array (mut i16)))
;; LOWER: (type $struct (struct (field externref)))
(type $struct (struct (field stringref)))
;; CHECK: (global $string (ref string) (string.const ""))
;; CHECK: (global $struct (ref $struct) (struct.new $struct
;; CHECK-NEXT: (global.get $string)
;; CHECK-NEXT: ))
;; LOWER: (type $2 (func (param externref externref) (result i32)))
;; LOWER: (type $3 (func (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (type $4 (func (param i32) (result (ref extern))))
;; LOWER: (type $5 (func (param externref externref) (result (ref extern))))
;; LOWER: (type $6 (func (param externref (ref null $0) i32) (result i32)))
;; LOWER: (type $7 (func (param externref) (result i32)))
;; LOWER: (type $8 (func (param externref i32) (result i32)))
;; LOWER: (type $9 (func (param externref i32 i32) (result (ref extern))))
;; LOWER: (import "string.const" "0" (global $string (ref extern)))
;; LOWER: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $3) (param (ref null $0) i32 i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $4) (param i32) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "concat" (func $concat (type $5) (param externref externref) (result (ref extern))))
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $6) (param externref (ref null $0) i32) (result i32)))
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $2) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $2) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "length" (func $length (type $7) (param externref) (result i32)))
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $8) (param externref i32) (result i32)))
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $9) (param externref i32 i32) (result (ref extern))))
;; LOWER: (global $struct (ref $struct) (struct.new $struct
;; LOWER-NEXT: (global.get $string)
;; LOWER-NEXT: ))
(global $struct (ref $struct) (struct.new $struct
(string.const "")
))
(global $string (ref string) (string.const ""))
)
|