Skip to content

Commit 2b512ce

Browse files
authored
StringLowering: Lower nulls in call params (#6317)
1 parent feb8f24 commit 2b512ce

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

src/passes/StringLowering.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ struct StringLowering : public StringGathering {
456456
}
457457
}
458458

459+
void visitCall(Call* curr) {
460+
auto targetSig =
461+
getModule()->getFunction(curr->target)->type.getSignature();
462+
for (Index i = 0; i < curr->operands.size(); i++) {
463+
if (isExt(targetSig.params[i])) {
464+
ensureNullIsExt(curr->operands[i]);
465+
}
466+
}
467+
}
468+
459469
void visitStructNew(StructNew* curr) {
460470
if (curr->type == Type::unreachable || curr->operands.empty()) {
461471
return;

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

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
;; CHECK: (type $4 (func (result externref)))
1717

18+
;; CHECK: (type $5 (func (param externref)))
19+
1820
;; CHECK: (type $struct-of-string (struct (field externref) (field i32) (field anyref)))
1921
(type $struct-of-string (struct (field stringref) (field i32) (field anyref)))
2022

@@ -37,31 +39,31 @@
3739
(type $array16 (array (mut i16)))
3840
)
3941

40-
;; CHECK: (type $12 (func (param externref) (result externref)))
41-
4242
;; CHECK: (type $13 (func (param externref) (result externref)))
4343

44-
;; CHECK: (type $14 (func (param externref) (result i32)))
44+
;; CHECK: (type $14 (func (param externref) (result externref)))
4545

46-
;; CHECK: (type $15 (func (param externref externref) (result i32)))
46+
;; CHECK: (type $15 (func (param externref) (result i32)))
4747

48-
;; CHECK: (type $16 (func (param externref (ref $0)) (result i32)))
48+
;; CHECK: (type $16 (func (param externref externref) (result i32)))
4949

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

52-
;; CHECK: (type $18 (func (param externref (ref extern) externref externref externref (ref extern))))
52+
;; CHECK: (type $18 (func (param (ref $0))))
5353

54-
;; CHECK: (type $19 (func (param (ref null $0) i32 i32) (result (ref extern))))
54+
;; CHECK: (type $19 (func (param externref (ref extern) externref externref externref (ref extern))))
5555

56-
;; CHECK: (type $20 (func (param i32) (result (ref extern))))
56+
;; CHECK: (type $20 (func (param (ref null $0) i32 i32) (result (ref extern))))
5757

58-
;; CHECK: (type $21 (func (param externref (ref null $0) i32) (result i32)))
58+
;; CHECK: (type $21 (func (param i32) (result (ref extern))))
5959

60-
;; CHECK: (type $22 (func (param externref) (result i32)))
60+
;; CHECK: (type $22 (func (param externref (ref null $0) i32) (result i32)))
6161

62-
;; CHECK: (type $23 (func (param externref i32) (result i32)))
62+
;; CHECK: (type $23 (func (param externref) (result i32)))
6363

64-
;; CHECK: (type $24 (func (param externref i32 i32) (result (ref extern))))
64+
;; CHECK: (type $24 (func (param externref i32) (result i32)))
65+
66+
;; CHECK: (type $25 (func (param externref i32 i32) (result (ref extern))))
6567

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

@@ -70,27 +72,27 @@
7072
;; CHECK: (import "colliding" "name" (func $fromCodePoint (type $1)))
7173
(import "colliding" "name" (func $fromCodePoint))
7274

73-
;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $19) (param (ref null $0) i32 i32) (result (ref extern))))
75+
;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $20) (param (ref null $0) i32 i32) (result (ref extern))))
7476

75-
;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint_17 (type $20) (param i32) (result (ref extern))))
77+
;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint_18 (type $21) (param i32) (result (ref extern))))
7678

77-
;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $21) (param externref (ref null $0) i32) (result i32)))
79+
;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $22) (param externref (ref null $0) i32) (result i32)))
7880

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

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

83-
;; CHECK: (import "wasm:js-string" "length" (func $length (type $22) (param externref) (result i32)))
85+
;; CHECK: (import "wasm:js-string" "length" (func $length (type $23) (param externref) (result i32)))
8486

85-
;; CHECK: (import "wasm:js-string" "codePointAt" (func $codePointAt (type $23) (param externref i32) (result i32)))
87+
;; CHECK: (import "wasm:js-string" "codePointAt" (func $codePointAt (type $24) (param externref i32) (result i32)))
8688

87-
;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $24) (param externref i32 i32) (result (ref extern))))
89+
;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $25) (param externref i32 i32) (result (ref extern))))
8890

8991
;; CHECK: (export "export.1" (func $exported-string-returner))
9092

9193
;; CHECK: (export "export.2" (func $exported-string-receiver))
9294

93-
;; CHECK: (func $string.as (type $18) (param $a externref) (param $a_nn (ref extern)) (param $b externref) (param $c externref) (param $d externref) (param $nn_view (ref extern))
95+
;; 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))
9496
;; CHECK-NEXT: (local.set $b
9597
;; CHECK-NEXT: (ref.as_non_null
9698
;; CHECK-NEXT: (local.get $a)
@@ -156,7 +158,7 @@
156158
)
157159
)
158160

159-
;; CHECK: (func $string.new.gc (type $17) (param $array16 (ref $0))
161+
;; CHECK: (func $string.new.gc (type $18) (param $array16 (ref $0))
160162
;; CHECK-NEXT: (drop
161163
;; CHECK-NEXT: (call $fromCharCodeArray
162164
;; CHECK-NEXT: (local.get $array16)
@@ -176,7 +178,7 @@
176178
)
177179

178180
;; CHECK: (func $string.from_code_point (type $4) (result externref)
179-
;; CHECK-NEXT: (call $fromCodePoint_17
181+
;; CHECK-NEXT: (call $fromCodePoint_18
180182
;; CHECK-NEXT: (i32.const 1)
181183
;; CHECK-NEXT: )
182184
;; CHECK-NEXT: )
@@ -186,7 +188,7 @@
186188
)
187189
)
188190

189-
;; CHECK: (func $string.encode (type $16) (param $ref externref) (param $array16 (ref $0)) (result i32)
191+
;; CHECK: (func $string.encode (type $17) (param $ref externref) (param $array16 (ref $0)) (result i32)
190192
;; CHECK-NEXT: (call $intoCharCodeArray
191193
;; CHECK-NEXT: (local.get $ref)
192194
;; CHECK-NEXT: (local.get $array16)
@@ -201,7 +203,7 @@
201203
)
202204
)
203205

204-
;; CHECK: (func $string.eq (type $15) (param $a externref) (param $b externref) (result i32)
206+
;; CHECK: (func $string.eq (type $16) (param $a externref) (param $b externref) (result i32)
205207
;; CHECK-NEXT: (call $equals
206208
;; CHECK-NEXT: (local.get $a)
207209
;; CHECK-NEXT: (local.get $b)
@@ -214,7 +216,7 @@
214216
)
215217
)
216218

217-
;; CHECK: (func $string.compare (type $15) (param $a externref) (param $b externref) (result i32)
219+
;; CHECK: (func $string.compare (type $16) (param $a externref) (param $b externref) (result i32)
218220
;; CHECK-NEXT: (call $compare
219221
;; CHECK-NEXT: (local.get $a)
220222
;; CHECK-NEXT: (local.get $b)
@@ -227,7 +229,7 @@
227229
)
228230
)
229231

230-
;; CHECK: (func $string.length (type $14) (param $ref externref) (result i32)
232+
;; CHECK: (func $string.length (type $15) (param $ref externref) (result i32)
231233
;; CHECK-NEXT: (call $length
232234
;; CHECK-NEXT: (local.get $ref)
233235
;; CHECK-NEXT: )
@@ -238,7 +240,7 @@
238240
)
239241
)
240242

241-
;; CHECK: (func $string.get_codeunit (type $14) (param $ref externref) (result i32)
243+
;; CHECK: (func $string.get_codeunit (type $15) (param $ref externref) (result i32)
242244
;; CHECK-NEXT: (call $codePointAt
243245
;; CHECK-NEXT: (local.get $ref)
244246
;; CHECK-NEXT: (i32.const 2)
@@ -251,7 +253,7 @@
251253
)
252254
)
253255

254-
;; CHECK: (func $string.slice (type $13) (param $ref externref) (result externref)
256+
;; CHECK: (func $string.slice (type $14) (param $ref externref) (result externref)
255257
;; CHECK-NEXT: (call $substring
256258
;; CHECK-NEXT: (local.get $ref)
257259
;; CHECK-NEXT: (i32.const 2)
@@ -266,7 +268,7 @@
266268
)
267269
)
268270

269-
;; CHECK: (func $if.string (type $12) (param $ref externref) (result externref)
271+
;; CHECK: (func $if.string (type $13) (param $ref externref) (result externref)
270272
;; CHECK-NEXT: (if (result externref)
271273
;; CHECK-NEXT: (i32.const 0)
272274
;; CHECK-NEXT: (then
@@ -289,7 +291,7 @@
289291
)
290292
)
291293

292-
;; CHECK: (func $if.string.flip (type $12) (param $ref externref) (result externref)
294+
;; CHECK: (func $if.string.flip (type $13) (param $ref externref) (result externref)
293295
;; CHECK-NEXT: (if (result externref)
294296
;; CHECK-NEXT: (i32.const 0)
295297
;; CHECK-NEXT: (then
@@ -442,4 +444,16 @@
442444
(struct.new_default $struct-of-string) ;; Nothing to do here.
443445
)
444446
)
447+
448+
;; CHECK: (func $call-param-null (type $5) (param $str externref)
449+
;; CHECK-NEXT: (call $call-param-null
450+
;; CHECK-NEXT: (ref.null noextern)
451+
;; CHECK-NEXT: )
452+
;; CHECK-NEXT: )
453+
(func $call-param-null (param $str stringref)
454+
;; After the lowering this null must be an ext.
455+
(call $call-param-null
456+
(ref.null string)
457+
)
458+
)
445459
)

0 commit comments

Comments
 (0)