diff options
author | Alon Zakai <azakai@google.com> | 2024-01-31 14:29:53 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-31 14:29:53 -0800 |
commit | dfcae55bd02747cb1eaf8410c02ac4d53ee1fd01 (patch) | |
tree | 2e771bfe2827e48a5542b6d65f6f003b0db5f007 /test/lit/passes/string-gathering.wast | |
parent | 396a826d791e63322cd4f47f116412d3e30ea5fc (diff) | |
download | binaryen-dfcae55bd02747cb1eaf8410c02ac4d53ee1fd01.tar.gz binaryen-dfcae55bd02747cb1eaf8410c02ac4d53ee1fd01.tar.bz2 binaryen-dfcae55bd02747cb1eaf8410c02ac4d53ee1fd01.zip |
StringGathering pass (#6257)
This pass finds all string.const and creates globals for them. After this transform, no
string.const appears anywhere but in a global, and each string appears in one global
which is then global.get-ed everywhere.
This avoids overhead in VMs where executing a string.const is an allocation, and is
also a good step towards imported strings. For that, this pass will be extended from
gathering to a full lowering pass, which will first gather into globals as this pass does,
and then turn each of those globals with a string.const into an imported externref.
(For that reason this pass is in a file called StringLowering, as the two passes will
share much of their code, and the larger pass should decide the name I think.)
This pass runs in -O2 and above. Repeated executions have no downside (see
details in code).
Diffstat (limited to 'test/lit/passes/string-gathering.wast')
-rw-r--r-- | test/lit/passes/string-gathering.wast | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/test/lit/passes/string-gathering.wast b/test/lit/passes/string-gathering.wast new file mode 100644 index 000000000..21fe358a3 --- /dev/null +++ b/test/lit/passes/string-gathering.wast @@ -0,0 +1,96 @@ +;; 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 + +;; All the strings should be collected into globals and used from there. They +;; should also be sorted deterministically (alphabetically). + +(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 $string.const_bar (ref string) (string.const "bar")) + + ;; CHECK: (global $string.const_other (ref string) (string.const "other")) + + ;; CHECK: (global $global (ref string) (string.const "foo")) + (global $global (ref string) (string.const "foo")) + + ;; CHECK: (global $global2 stringref (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: ) + (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: ) + (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))) + (import "a" "b" (global $import (ref string))) + + ;; CHECK: (global $global1 (ref string) (string.const "foo")) + (global $global1 (ref string) (string.const "foo")) + + ;; CHECK: (global $global2 (ref string) (global.get $global1)) + (global $global2 (ref string) (string.const "foo")) + + ;; CHECK: (global $global3 (ref string) (global.get $global1)) + (global $global3 (ref string) (string.const "foo")) + + ;; CHECK: (global $global4 (ref string) (string.const "bar")) + (global $global4 (ref string) (string.const "bar")) + + ;; CHECK: (global $global5 (ref string) (global.get $global4)) + (global $global5 (ref string) (string.const "bar")) + + ;; CHECK: (global $global6 (ref string) (global.get $global4)) + (global $global6 (ref string) (string.const "bar")) +) |