summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-03-29 12:46:52 -0700
committerGitHub <noreply@github.com>2024-03-29 19:46:52 +0000
commit88eabaaddeeff6fb8295a2e35c8e29927df04724 (patch)
tree64bf4d085244fcc2adad324ba265a8bc8519b010
parenteae2638a53e778e24e1ed042f2e16aa0b3b127a5 (diff)
downloadbinaryen-88eabaaddeeff6fb8295a2e35c8e29927df04724.tar.gz
binaryen-88eabaaddeeff6fb8295a2e35c8e29927df04724.tar.bz2
binaryen-88eabaaddeeff6fb8295a2e35c8e29927df04724.zip
[Strings] Lower string.concat in StringLowering (#6453)
-rw-r--r--src/passes/StringLowering.cpp9
-rw-r--r--test/lit/passes/string-gathering.wast40
-rw-r--r--test/lit/passes/string-lowering-instructions.wast53
3 files changed, 69 insertions, 33 deletions
diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp
index 322f0deb2..df2d66860 100644
--- a/src/passes/StringLowering.cpp
+++ b/src/passes/StringLowering.cpp
@@ -298,6 +298,7 @@ struct StringLowering : public StringGathering {
Name fromCharCodeArrayImport;
Name intoCharCodeArrayImport;
Name fromCodePointImport;
+ Name concatImport;
Name equalsImport;
Name compareImport;
Name lengthImport;
@@ -328,6 +329,8 @@ struct StringLowering : public StringGathering {
module, "fromCharCodeArray", {nullArray16, Type::i32, Type::i32}, nnExt);
// string.fromCodePoint: codepoint -> ext
fromCodePointImport = addImport(module, "fromCodePoint", Type::i32, nnExt);
+ // string.concat: string, string -> string
+ concatImport = addImport(module, "concat", {nullExt, nullExt}, nnExt);
// string.intoCharCodeArray: string, array, start -> num written
intoCharCodeArrayImport = addImport(module,
"intoCharCodeArray",
@@ -375,6 +378,12 @@ struct StringLowering : public StringGathering {
}
}
+ void visitStringConcat(StringConcat* curr) {
+ Builder builder(*getModule());
+ replaceCurrent(builder.makeCall(
+ lowering.concatImport, {curr->left, curr->right}, lowering.nnExt));
+ }
+
void visitStringAs(StringAs* curr) {
// There is no difference between strings and views with imported
// strings: they are all just JS strings, so no conversion is needed.
diff --git a/test/lit/passes/string-gathering.wast b/test/lit/passes/string-gathering.wast
index efa6ba130..bef342ee0 100644
--- a/test/lit/passes/string-gathering.wast
+++ b/test/lit/passes/string-gathering.wast
@@ -35,13 +35,15 @@
;; LOWER: (type $4 (func (param i32) (result (ref extern))))
- ;; LOWER: (type $5 (func (param externref (ref null $2) i32) (result i32)))
+ ;; LOWER: (type $5 (func (param externref externref) (result (ref extern))))
- ;; LOWER: (type $6 (func (param externref) (result i32)))
+ ;; LOWER: (type $6 (func (param externref (ref null $2) i32) (result i32)))
- ;; LOWER: (type $7 (func (param externref i32) (result i32)))
+ ;; LOWER: (type $7 (func (param externref) (result i32)))
- ;; LOWER: (type $8 (func (param externref i32 i32) (result (ref extern))))
+ ;; 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_bar (ref extern)))
@@ -53,17 +55,19 @@
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $4) (param i32) (result (ref extern))))
- ;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $5) (param externref (ref null $2) i32) (result i32)))
+ ;; 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 $2) 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" "length" (func $length (type $7) (param externref) (result i32)))
- ;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $7) (param externref i32) (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 $8) (param externref i32 i32) (result (ref extern))))
+ ;; 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"))
@@ -151,13 +155,15 @@
;; LOWER: (type $3 (func (param i32) (result (ref extern))))
- ;; LOWER: (type $4 (func (param externref (ref null $1) i32) (result i32)))
+ ;; LOWER: (type $4 (func (param externref externref) (result (ref extern))))
- ;; LOWER: (type $5 (func (param externref) (result i32)))
+ ;; LOWER: (type $5 (func (param externref (ref null $1) i32) (result i32)))
- ;; LOWER: (type $6 (func (param externref i32) (result i32)))
+ ;; LOWER: (type $6 (func (param externref) (result i32)))
+
+ ;; LOWER: (type $7 (func (param externref i32) (result i32)))
- ;; LOWER: (type $7 (func (param externref i32 i32) (result (ref extern))))
+ ;; LOWER: (type $8 (func (param externref i32 i32) (result (ref extern))))
;; LOWER: (import "a" "b" (global $import (ref extern)))
(import "a" "b" (global $import (ref string)))
@@ -174,17 +180,19 @@
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $3) (param i32) (result (ref extern))))
- ;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $4) (param externref (ref null $1) i32) (result i32)))
+ ;; 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 $1) i32) (result i32)))
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $0) (param externref externref) (result i32)))
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $0) (param externref externref) (result i32)))
- ;; LOWER: (import "wasm:js-string" "length" (func $length (type $5) (param 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 $6) (param externref i32) (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 $7) (param externref i32 i32) (result (ref extern))))
+ ;; 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"))
diff --git a/test/lit/passes/string-lowering-instructions.wast b/test/lit/passes/string-lowering-instructions.wast
index 97186fba7..548586c4b 100644
--- a/test/lit/passes/string-lowering-instructions.wast
+++ b/test/lit/passes/string-lowering-instructions.wast
@@ -50,21 +50,25 @@
;; CHECK: (type $17 (func (param externref (ref $0)) (result i32)))
- ;; CHECK: (type $18 (func (param (ref $0))))
+ ;; CHECK: (type $18 (func (param externref externref) (result (ref extern))))
- ;; CHECK: (type $19 (func (param externref (ref extern) externref externref externref (ref extern))))
+ ;; CHECK: (type $19 (func (param (ref $0))))
- ;; CHECK: (type $20 (func (param (ref null $0) i32 i32) (result (ref extern))))
+ ;; CHECK: (type $20 (func (param externref (ref extern) externref externref externref (ref extern))))
- ;; CHECK: (type $21 (func (param i32) (result (ref extern))))
+ ;; CHECK: (type $21 (func (param (ref null $0) i32 i32) (result (ref extern))))
- ;; CHECK: (type $22 (func (param externref (ref null $0) i32) (result i32)))
+ ;; CHECK: (type $22 (func (param i32) (result (ref extern))))
- ;; CHECK: (type $23 (func (param externref) (result i32)))
+ ;; CHECK: (type $23 (func (param externref externref) (result (ref extern))))
- ;; CHECK: (type $24 (func (param externref i32) (result i32)))
+ ;; CHECK: (type $24 (func (param externref (ref null $0) i32) (result i32)))
- ;; CHECK: (type $25 (func (param externref i32 i32) (result (ref extern))))
+ ;; CHECK: (type $25 (func (param externref) (result i32)))
+
+ ;; CHECK: (type $26 (func (param externref i32) (result i32)))
+
+ ;; CHECK: (type $27 (func (param externref i32 i32) (result (ref extern))))
;; CHECK: (import "string.const" "0" (global $string.const_exported (ref extern)))
@@ -74,21 +78,23 @@
(import "colliding" "name" (func $fromCodePoint))
- ;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $20) (param (ref null $0) i32 i32) (result (ref extern))))
+ ;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $21) (param (ref null $0) i32 i32) (result (ref extern))))
- ;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint_18 (type $21) (param i32) (result (ref extern))))
+ ;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint_19 (type $22) (param i32) (result (ref extern))))
- ;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $22) (param externref (ref null $0) i32) (result i32)))
+ ;; CHECK: (import "wasm:js-string" "concat" (func $concat (type $23) (param externref externref) (result (ref extern))))
+
+ ;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $24) (param externref (ref null $0) i32) (result i32)))
;; CHECK: (import "wasm:js-string" "equals" (func $equals (type $2) (param externref externref) (result i32)))
;; CHECK: (import "wasm:js-string" "compare" (func $compare (type $2) (param externref externref) (result i32)))
- ;; CHECK: (import "wasm:js-string" "length" (func $length (type $23) (param externref) (result i32)))
+ ;; CHECK: (import "wasm:js-string" "length" (func $length (type $25) (param externref) (result i32)))
- ;; CHECK: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $24) (param externref i32) (result i32)))
+ ;; CHECK: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $26) (param externref i32) (result i32)))
- ;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $25) (param externref i32 i32) (result (ref extern))))
+ ;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $27) (param externref i32 i32) (result (ref extern))))
;; CHECK: (global $string externref (ref.null noextern))
(global $string stringref (ref.null string)) ;; Test we update global nulls.
@@ -97,7 +103,7 @@
;; CHECK: (export "export.2" (func $exported-string-receiver))
- ;; CHECK: (func $string.as (type $19) (param $a externref) (param $a_nn (ref extern)) (param $b externref) (param $c externref) (param $d externref) (param $nn_view (ref extern))
+ ;; CHECK: (func $string.as (type $20) (param $a externref) (param $a_nn (ref extern)) (param $b externref) (param $c externref) (param $d externref) (param $nn_view (ref extern))
;; CHECK-NEXT: (local.set $b
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $a)
@@ -163,7 +169,7 @@
)
)
- ;; CHECK: (func $string.new.gc (type $18) (param $array16 (ref $0))
+ ;; CHECK: (func $string.new.gc (type $19) (param $array16 (ref $0))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call $fromCharCodeArray
;; CHECK-NEXT: (local.get $array16)
@@ -183,7 +189,7 @@
)
;; CHECK: (func $string.from_code_point (type $4) (result externref)
- ;; CHECK-NEXT: (call $fromCodePoint_18
+ ;; CHECK-NEXT: (call $fromCodePoint_19
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
@@ -193,6 +199,19 @@
)
)
+ ;; CHECK: (func $string.concat (type $18) (param $0 externref) (param $1 externref) (result (ref extern))
+ ;; CHECK-NEXT: (call $concat
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $string.concat (param stringref stringref) (result (ref string))
+ (string.concat
+ (local.get 0)
+ (local.get 1)
+ )
+ )
+
;; CHECK: (func $string.encode (type $17) (param $ref externref) (param $array16 (ref $0)) (result i32)
;; CHECK-NEXT: (call $intoCharCodeArray
;; CHECK-NEXT: (local.get $ref)