Skip to content

Commit 88eabaa

Browse files
authored
[Strings] Lower string.concat in StringLowering (#6453)
1 parent eae2638 commit 88eabaa

File tree

3 files changed

+69
-33
lines changed

3 files changed

+69
-33
lines changed

src/passes/StringLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ struct StringLowering : public StringGathering {
298298
Name fromCharCodeArrayImport;
299299
Name intoCharCodeArrayImport;
300300
Name fromCodePointImport;
301+
Name concatImport;
301302
Name equalsImport;
302303
Name compareImport;
303304
Name lengthImport;
@@ -328,6 +329,8 @@ struct StringLowering : public StringGathering {
328329
module, "fromCharCodeArray", {nullArray16, Type::i32, Type::i32}, nnExt);
329330
// string.fromCodePoint: codepoint -> ext
330331
fromCodePointImport = addImport(module, "fromCodePoint", Type::i32, nnExt);
332+
// string.concat: string, string -> string
333+
concatImport = addImport(module, "concat", {nullExt, nullExt}, nnExt);
331334
// string.intoCharCodeArray: string, array, start -> num written
332335
intoCharCodeArrayImport = addImport(module,
333336
"intoCharCodeArray",
@@ -375,6 +378,12 @@ struct StringLowering : public StringGathering {
375378
}
376379
}
377380

381+
void visitStringConcat(StringConcat* curr) {
382+
Builder builder(*getModule());
383+
replaceCurrent(builder.makeCall(
384+
lowering.concatImport, {curr->left, curr->right}, lowering.nnExt));
385+
}
386+
378387
void visitStringAs(StringAs* curr) {
379388
// There is no difference between strings and views with imported
380389
// strings: they are all just JS strings, so no conversion is needed.

test/lit/passes/string-gathering.wast

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535

3636
;; LOWER: (type $4 (func (param i32) (result (ref extern))))
3737

38-
;; LOWER: (type $5 (func (param externref (ref null $2) i32) (result i32)))
38+
;; LOWER: (type $5 (func (param externref externref) (result (ref extern))))
3939

40-
;; LOWER: (type $6 (func (param externref) (result i32)))
40+
;; LOWER: (type $6 (func (param externref (ref null $2) i32) (result i32)))
4141

42-
;; LOWER: (type $7 (func (param externref i32) (result i32)))
42+
;; LOWER: (type $7 (func (param externref) (result i32)))
4343

44-
;; LOWER: (type $8 (func (param externref i32 i32) (result (ref extern))))
44+
;; LOWER: (type $8 (func (param externref i32) (result i32)))
45+
46+
;; LOWER: (type $9 (func (param externref i32 i32) (result (ref extern))))
4547

4648
;; LOWER: (import "string.const" "0" (global $string.const_bar (ref extern)))
4749

@@ -53,17 +55,19 @@
5355

5456
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $4) (param i32) (result (ref extern))))
5557

56-
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $5) (param externref (ref null $2) i32) (result i32)))
58+
;; LOWER: (import "wasm:js-string" "concat" (func $concat (type $5) (param externref externref) (result (ref extern))))
59+
60+
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $6) (param externref (ref null $2) i32) (result i32)))
5761

5862
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $1) (param externref externref) (result i32)))
5963

6064
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $1) (param externref externref) (result i32)))
6165

62-
;; LOWER: (import "wasm:js-string" "length" (func $length (type $6) (param externref) (result i32)))
66+
;; LOWER: (import "wasm:js-string" "length" (func $length (type $7) (param externref) (result i32)))
6367

64-
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $7) (param externref i32) (result i32)))
68+
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $8) (param externref i32) (result i32)))
6569

66-
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $8) (param externref i32 i32) (result (ref extern))))
70+
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $9) (param externref i32 i32) (result (ref extern))))
6771

6872
;; LOWER: (global $global2 externref (global.get $string.const_bar))
6973
(global $global2 (ref null string) (string.const "bar"))
@@ -151,13 +155,15 @@
151155

152156
;; LOWER: (type $3 (func (param i32) (result (ref extern))))
153157

154-
;; LOWER: (type $4 (func (param externref (ref null $1) i32) (result i32)))
158+
;; LOWER: (type $4 (func (param externref externref) (result (ref extern))))
155159

156-
;; LOWER: (type $5 (func (param externref) (result i32)))
160+
;; LOWER: (type $5 (func (param externref (ref null $1) i32) (result i32)))
157161

158-
;; LOWER: (type $6 (func (param externref i32) (result i32)))
162+
;; LOWER: (type $6 (func (param externref) (result i32)))
163+
164+
;; LOWER: (type $7 (func (param externref i32) (result i32)))
159165

160-
;; LOWER: (type $7 (func (param externref i32 i32) (result (ref extern))))
166+
;; LOWER: (type $8 (func (param externref i32 i32) (result (ref extern))))
161167

162168
;; LOWER: (import "a" "b" (global $import (ref extern)))
163169
(import "a" "b" (global $import (ref string)))
@@ -174,17 +180,19 @@
174180

175181
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $3) (param i32) (result (ref extern))))
176182

177-
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $4) (param externref (ref null $1) i32) (result i32)))
183+
;; LOWER: (import "wasm:js-string" "concat" (func $concat (type $4) (param externref externref) (result (ref extern))))
184+
185+
;; LOWER: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $5) (param externref (ref null $1) i32) (result i32)))
178186

179187
;; LOWER: (import "wasm:js-string" "equals" (func $equals (type $0) (param externref externref) (result i32)))
180188

181189
;; LOWER: (import "wasm:js-string" "compare" (func $compare (type $0) (param externref externref) (result i32)))
182190

183-
;; LOWER: (import "wasm:js-string" "length" (func $length (type $5) (param externref) (result i32)))
191+
;; LOWER: (import "wasm:js-string" "length" (func $length (type $6) (param externref) (result i32)))
184192

185-
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $6) (param externref i32) (result i32)))
193+
;; LOWER: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $7) (param externref i32) (result i32)))
186194

187-
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $7) (param externref i32 i32) (result (ref extern))))
195+
;; LOWER: (import "wasm:js-string" "substring" (func $substring (type $8) (param externref i32 i32) (result (ref extern))))
188196

189197
;; LOWER: (global $global2 (ref extern) (global.get $global1))
190198
(global $global2 (ref string) (string.const "foo"))

test/lit/passes/string-lowering-instructions.wast

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,25 @@
5050

5151
;; CHECK: (type $17 (func (param externref (ref $0)) (result i32)))
5252

53-
;; CHECK: (type $18 (func (param (ref $0))))
53+
;; CHECK: (type $18 (func (param externref externref) (result (ref extern))))
5454

55-
;; CHECK: (type $19 (func (param externref (ref extern) externref externref externref (ref extern))))
55+
;; CHECK: (type $19 (func (param (ref $0))))
5656

57-
;; CHECK: (type $20 (func (param (ref null $0) i32 i32) (result (ref extern))))
57+
;; CHECK: (type $20 (func (param externref (ref extern) externref externref externref (ref extern))))
5858

59-
;; CHECK: (type $21 (func (param i32) (result (ref extern))))
59+
;; CHECK: (type $21 (func (param (ref null $0) i32 i32) (result (ref extern))))
6060

61-
;; CHECK: (type $22 (func (param externref (ref null $0) i32) (result i32)))
61+
;; CHECK: (type $22 (func (param i32) (result (ref extern))))
6262

63-
;; CHECK: (type $23 (func (param externref) (result i32)))
63+
;; CHECK: (type $23 (func (param externref externref) (result (ref extern))))
6464

65-
;; CHECK: (type $24 (func (param externref i32) (result i32)))
65+
;; CHECK: (type $24 (func (param externref (ref null $0) i32) (result i32)))
6666

67-
;; CHECK: (type $25 (func (param externref i32 i32) (result (ref extern))))
67+
;; CHECK: (type $25 (func (param externref) (result i32)))
68+
69+
;; CHECK: (type $26 (func (param externref i32) (result i32)))
70+
71+
;; CHECK: (type $27 (func (param externref i32 i32) (result (ref extern))))
6872

6973
;; CHECK: (import "string.const" "0" (global $string.const_exported (ref extern)))
7074

@@ -74,21 +78,23 @@
7478
(import "colliding" "name" (func $fromCodePoint))
7579

7680

77-
;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $20) (param (ref null $0) i32 i32) (result (ref extern))))
81+
;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $21) (param (ref null $0) i32 i32) (result (ref extern))))
7882

79-
;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint_18 (type $21) (param i32) (result (ref extern))))
83+
;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint_19 (type $22) (param i32) (result (ref extern))))
8084

81-
;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $22) (param externref (ref null $0) i32) (result i32)))
85+
;; CHECK: (import "wasm:js-string" "concat" (func $concat (type $23) (param externref externref) (result (ref extern))))
86+
87+
;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $24) (param externref (ref null $0) i32) (result i32)))
8288

8389
;; CHECK: (import "wasm:js-string" "equals" (func $equals (type $2) (param externref externref) (result i32)))
8490

8591
;; CHECK: (import "wasm:js-string" "compare" (func $compare (type $2) (param externref externref) (result i32)))
8692

87-
;; CHECK: (import "wasm:js-string" "length" (func $length (type $23) (param externref) (result i32)))
93+
;; CHECK: (import "wasm:js-string" "length" (func $length (type $25) (param externref) (result i32)))
8894

89-
;; CHECK: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $24) (param externref i32) (result i32)))
95+
;; CHECK: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $26) (param externref i32) (result i32)))
9096

91-
;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $25) (param externref i32 i32) (result (ref extern))))
97+
;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $27) (param externref i32 i32) (result (ref extern))))
9298

9399
;; CHECK: (global $string externref (ref.null noextern))
94100
(global $string stringref (ref.null string)) ;; Test we update global nulls.
@@ -97,7 +103,7 @@
97103

98104
;; CHECK: (export "export.2" (func $exported-string-receiver))
99105

100-
;; 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))
106+
;; 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))
101107
;; CHECK-NEXT: (local.set $b
102108
;; CHECK-NEXT: (ref.as_non_null
103109
;; CHECK-NEXT: (local.get $a)
@@ -163,7 +169,7 @@
163169
)
164170
)
165171

166-
;; CHECK: (func $string.new.gc (type $18) (param $array16 (ref $0))
172+
;; CHECK: (func $string.new.gc (type $19) (param $array16 (ref $0))
167173
;; CHECK-NEXT: (drop
168174
;; CHECK-NEXT: (call $fromCharCodeArray
169175
;; CHECK-NEXT: (local.get $array16)
@@ -183,7 +189,7 @@
183189
)
184190

185191
;; CHECK: (func $string.from_code_point (type $4) (result externref)
186-
;; CHECK-NEXT: (call $fromCodePoint_18
192+
;; CHECK-NEXT: (call $fromCodePoint_19
187193
;; CHECK-NEXT: (i32.const 1)
188194
;; CHECK-NEXT: )
189195
;; CHECK-NEXT: )
@@ -193,6 +199,19 @@
193199
)
194200
)
195201

202+
;; CHECK: (func $string.concat (type $18) (param $0 externref) (param $1 externref) (result (ref extern))
203+
;; CHECK-NEXT: (call $concat
204+
;; CHECK-NEXT: (local.get $0)
205+
;; CHECK-NEXT: (local.get $1)
206+
;; CHECK-NEXT: )
207+
;; CHECK-NEXT: )
208+
(func $string.concat (param stringref stringref) (result (ref string))
209+
(string.concat
210+
(local.get 0)
211+
(local.get 1)
212+
)
213+
)
214+
196215
;; CHECK: (func $string.encode (type $17) (param $ref externref) (param $array16 (ref $0)) (result i32)
197216
;; CHECK-NEXT: (call $intoCharCodeArray
198217
;; CHECK-NEXT: (local.get $ref)

0 commit comments

Comments
 (0)