Skip to content

[Strings] Lower string.concat in StringLowering #6453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/passes/StringLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ struct StringLowering : public StringGathering {
Name fromCharCodeArrayImport;
Name intoCharCodeArrayImport;
Name fromCodePointImport;
Name concatImport;
Name equalsImport;
Name compareImport;
Name lengthImport;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 24 additions & 16 deletions test/lit/passes/string-gathering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand All @@ -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"))
Expand Down Expand Up @@ -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)))
Expand All @@ -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"))
Expand Down
53 changes: 36 additions & 17 deletions test/lit/passes/string-lowering-instructions.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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: )
Expand All @@ -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)
Expand Down