Skip to content

Commit d262701

Browse files
authored
[Strings] Allow customizing the module name for string constants in StringLifting/Lowering (#7399)
Previously we hardcoded "'" (a single quote).
1 parent a5f0423 commit d262701

File tree

7 files changed

+118
-7
lines changed

7 files changed

+118
-7
lines changed

scripts/test/fuzzing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
# has (ref extern) imports, which the fuzzer cannot create values for when
9797
# it removes unknown imports
9898
'string-lifting.wast',
99+
'string-lifting-custom-module.wast',
99100
# TODO: fuzzer support for stack switching
100101
'stack_switching.wast',
101102
'stack_switching_contnew.wast',

src/passes/StringLifting.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
// fully optimized. Typically StringLowering would be run later to lower them
2020
// back down.
2121
//
22+
// A pass argument allows customizing the module name for string constants:
23+
//
24+
// --pass-arg=string-constants-module@MODULE_NAME
25+
//
2226

2327
#include "ir/utils.h"
2428
#include "pass.h"
@@ -56,11 +60,13 @@ struct StringLifting : public Pass {
5660
// actual string. Find them all so we can apply them.
5761
//
5862
// TODO: parse the strings section for non-UTF16 strings.
63+
Name stringConstsModule =
64+
getArgumentOrDefault("string-constants-module", WasmStringConstsModule);
5965
for (auto& global : module->globals) {
6066
if (!global->imported()) {
6167
continue;
6268
}
63-
if (global->module == WasmStringConstsModule) {
69+
if (global->module == stringConstsModule) {
6470
importedStrings[global->name] = global->base;
6571
found = true;
6672
}

src/passes/StringLowering.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
//
2424
// StringLowering does the same, and also replaces those new globals with
2525
// imported globals of type externref, for use with the string imports proposal.
26-
// String operations will likewise need to be lowered. TODO
26+
//
27+
// A pass argument allows customizing the module name for string constants:
28+
//
29+
// --pass-arg=string-constants-module@MODULE_NAME
2730
//
2831
// Specs:
2932
// https://github.com/WebAssembly/stringref/blob/main/proposals/stringref/Overview.md
@@ -235,6 +238,8 @@ struct StringLowering : public StringGathering {
235238
}
236239

237240
void makeImports(Module* module) {
241+
Name stringConstsModule =
242+
getArgumentOrDefault("string-constants-module", WasmStringConstsModule);
238243
Index jsonImportIndex = 0;
239244
std::stringstream json;
240245
bool first = true;
@@ -244,7 +249,7 @@ struct StringLowering : public StringGathering {
244249
std::stringstream utf8;
245250
if (useMagicImports &&
246251
String::convertUTF16ToUTF8(utf8, c->string.str)) {
247-
global->module = WasmStringConstsModule;
252+
global->module = stringConstsModule;
248253
global->base = Name(utf8.str());
249254
} else {
250255
if (assertUTF8) {

src/passes/string-utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ namespace wasm {
2121

2222
const Name WasmStringsModule = "wasm:js-string";
2323

24-
const Name WasmStringConstsModule = "'";
24+
const char* WasmStringConstsModule = "'";
2525

2626
} // namespace wasm

src/passes/string-utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ namespace wasm {
2525
// https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md
2626
extern const Name WasmStringsModule;
2727

28-
// The name of the module to import string constants from, for magical imported
29-
// JS strings.
30-
extern const Name WasmStringConstsModule;
28+
// The default module to import string constants from, for magical imported JS
29+
// strings.
30+
extern const char* WasmStringConstsModule;
3131

3232
} // namespace wasm
3333

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
3+
;; Similar to string-lifting but the strings are imported from a
4+
;; custom module name.
5+
6+
;; RUN: foreach %s %t wasm-opt -all --string-lifting --pass-arg=string-constants-module@strings -S -o - | filecheck %s
7+
8+
(module
9+
;; CHECK: (type $0 (func))
10+
11+
;; CHECK: (import "\'" "foo" (global $string_foo (ref extern)))
12+
(import "\'" "foo" (global $string_foo (ref extern)))
13+
14+
;; CHECK: (import "strings" "bar" (global $string_bar (ref extern)))
15+
(import "strings" "bar" (global $string_bar (ref extern)))
16+
17+
;; CHECK: (func $func (type $0)
18+
;; CHECK-NEXT: (drop
19+
;; CHECK-NEXT: (global.get $string_foo)
20+
;; CHECK-NEXT: )
21+
;; CHECK-NEXT: (drop
22+
;; CHECK-NEXT: (string.const "bar")
23+
;; CHECK-NEXT: )
24+
;; CHECK-NEXT: )
25+
(func $func
26+
;; The first string has the default "'" module name, but we overrode it, so
27+
;; nothing changes.
28+
(drop
29+
(global.get $string_foo)
30+
)
31+
;; Here we imported with the right one given the pass-arg, so we turn this
32+
;; into a string.const.
33+
(drop
34+
(global.get $string_bar)
35+
)
36+
)
37+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
3+
;; Similar to string-lowering-imports but the strings are imported from a
4+
;; custom module name. The string constant should be imported from "strings", as
5+
;; the pass-arg specifies.
6+
7+
;; RUN: wasm-opt %s -all --string-lowering-magic-imports --pass-arg=string-constants-module@strings -S -o - | filecheck %s
8+
9+
(module
10+
;; CHECK: (type $0 (array (mut i16)))
11+
12+
;; CHECK: (type $1 (func (param externref externref) (result i32)))
13+
14+
;; CHECK: (type $2 (func))
15+
16+
;; CHECK: (type $3 (func (param (ref null $0) i32 i32) (result (ref extern))))
17+
18+
;; CHECK: (type $4 (func (param i32) (result (ref extern))))
19+
20+
;; CHECK: (type $5 (func (param externref externref) (result (ref extern))))
21+
22+
;; CHECK: (type $6 (func (param externref (ref null $0) i32) (result i32)))
23+
24+
;; CHECK: (type $7 (func (param externref) (result i32)))
25+
26+
;; CHECK: (type $8 (func (param externref i32) (result i32)))
27+
28+
;; CHECK: (type $9 (func (param externref i32 i32) (result (ref extern))))
29+
30+
;; CHECK: (import "strings" "foo" (global $"string.const_\"foo\"" (ref extern)))
31+
32+
;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $3) (param (ref null $0) i32 i32) (result (ref extern))))
33+
34+
;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $4) (param i32) (result (ref extern))))
35+
36+
;; CHECK: (import "wasm:js-string" "concat" (func $concat (type $5) (param externref externref) (result (ref extern))))
37+
38+
;; CHECK: (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $6) (param externref (ref null $0) i32) (result i32)))
39+
40+
;; CHECK: (import "wasm:js-string" "equals" (func $equals (type $1) (param externref externref) (result i32)))
41+
42+
;; CHECK: (import "wasm:js-string" "compare" (func $compare (type $1) (param externref externref) (result i32)))
43+
44+
;; CHECK: (import "wasm:js-string" "length" (func $length (type $7) (param externref) (result i32)))
45+
46+
;; CHECK: (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $8) (param externref i32) (result i32)))
47+
48+
;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $9) (param externref i32 i32) (result (ref extern))))
49+
50+
;; CHECK: (export "const" (func $const))
51+
52+
;; CHECK: (func $const (type $2)
53+
;; CHECK-NEXT: (drop
54+
;; CHECK-NEXT: (global.get $"string.const_\"foo\"")
55+
;; CHECK-NEXT: )
56+
;; CHECK-NEXT: )
57+
(func $const (export "const")
58+
(drop
59+
(string.const "foo")
60+
)
61+
)
62+
)

0 commit comments

Comments
 (0)