Skip to content

Commit ad4f95d

Browse files
shared string builtins
1 parent 4ffd9fe commit ad4f95d

12 files changed

+890
-289
lines changed

src/passes/StringLifting.cpp

Lines changed: 148 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ struct StringLifting : public Pass {
5050
Name charCodeAtImport;
5151
Name substringImport;
5252

53+
// Shared imported string functions.
54+
Name fromCharCodeArraySharedImport;
55+
Name intoCharCodeArraySharedImport;
56+
Name fromCodePointSharedImport;
57+
Name concatSharedImport;
58+
Name equalsSharedImport;
59+
Name testSharedImport;
60+
Name compareSharedImport;
61+
Name lengthSharedImport;
62+
Name charCodeAtSharedImport;
63+
Name substringSharedImport;
64+
5365
void run(Module* module) override {
5466
// Whether we found any work to do.
5567
bool found = false;
@@ -121,79 +133,137 @@ struct StringLifting : public Pass {
121133
module->customSections.erase(stringSectionIter);
122134
}
123135

124-
auto array16 = Type(Array(Field(Field::i16, Mutable)), Nullable);
136+
auto array16 = Type(HeapTypes::getMutI16Array(), Nullable);
125137
auto refExtern = Type(HeapType::ext, NonNullable);
126138
auto externref = Type(HeapType::ext, Nullable);
127139
auto i32 = Type::i32;
128140

141+
auto sharedArray16 = Type(HeapTypes::getSharedMutI16Array(), Nullable);
142+
auto refSharedExtern =
143+
Type(HeapType(HeapType::ext).getBasic(Shared), NonNullable);
144+
auto sharedExternref =
145+
Type(HeapType(HeapType::ext).getBasic(Shared), Nullable);
146+
129147
// Find imported string functions.
130148
for (auto& func : module->functions) {
131149
if (!func->imported() || func->module != WasmStringsModule) {
132150
continue;
133151
}
134-
// TODO: Check exactness here too.
135152
auto type = func->type;
136153
if (func->base == "fromCharCodeArray") {
137-
if (type.getHeapType() != Signature({array16, i32, i32}, refExtern)) {
154+
if (type.getHeapType() == Signature(Type({array16, i32, i32}), refExtern)) {
155+
fromCharCodeArrayImport = func->name;
156+
found = true;
157+
} else if (type.getHeapType() ==
158+
Signature(Type({sharedArray16, i32, i32}), refSharedExtern)) {
159+
fromCharCodeArraySharedImport = func->name;
160+
found = true;
161+
} else {
138162
Fatal() << "StringLifting: bad type for fromCharCodeArray: " << type;
139163
}
140-
fromCharCodeArrayImport = func->name;
141-
found = true;
142164
} else if (func->base == "fromCodePoint") {
143-
if (type.getHeapType() != Signature(i32, refExtern)) {
165+
if (type.getHeapType() == Signature(i32, refExtern)) {
166+
fromCodePointImport = func->name;
167+
found = true;
168+
} else if (type.getHeapType() == Signature(i32, refSharedExtern)) {
169+
fromCodePointSharedImport = func->name;
170+
found = true;
171+
} else {
144172
Fatal() << "StringLifting: bad type for fromCodePoint: " << type;
145173
}
146-
fromCodePointImport = func->name;
147-
found = true;
148174
} else if (func->base == "concat") {
149-
if (type.getHeapType() !=
150-
Signature({externref, externref}, refExtern)) {
175+
if (type.getHeapType() ==
176+
Signature(Type({externref, externref}), refExtern)) {
177+
concatImport = func->name;
178+
found = true;
179+
} else if (type.getHeapType() == Signature(Type({sharedExternref,
180+
sharedExternref}),
181+
refSharedExtern)) {
182+
concatSharedImport = func->name;
183+
found = true;
184+
} else {
151185
Fatal() << "StringLifting: bad type for concat: " << type;
152186
}
153-
concatImport = func->name;
154-
found = true;
155187
} else if (func->base == "intoCharCodeArray") {
156-
if (type.getHeapType() != Signature({externref, array16, i32}, i32)) {
188+
if (type.getHeapType() == Signature(Type({externref, array16, i32}), i32)) {
189+
intoCharCodeArrayImport = func->name;
190+
found = true;
191+
} else if (type.getHeapType() == Signature(Type({sharedExternref,
192+
sharedArray16,
193+
i32}),
194+
i32)) {
195+
intoCharCodeArraySharedImport = func->name;
196+
found = true;
197+
} else {
157198
Fatal() << "StringLifting: bad type for intoCharCodeArray: " << type;
158199
}
159-
intoCharCodeArrayImport = func->name;
160-
found = true;
161200
} else if (func->base == "equals") {
162-
if (type.getHeapType() != Signature({externref, externref}, i32)) {
201+
if (type.getHeapType() == Signature(Type({externref, externref}), i32)) {
202+
equalsImport = func->name;
203+
found = true;
204+
} else if (type.getHeapType() == Signature(Type({sharedExternref,
205+
sharedExternref}),
206+
i32)) {
207+
equalsSharedImport = func->name;
208+
found = true;
209+
} else {
163210
Fatal() << "StringLifting: bad type for equals: " << type;
164211
}
165-
equalsImport = func->name;
166-
found = true;
167212
} else if (func->base == "test") {
168-
if (type.getHeapType() != Signature({externref}, i32)) {
213+
if (type.getHeapType() == Signature(Type({externref}), i32)) {
214+
testImport = func->name;
215+
found = true;
216+
} else if (type.getHeapType() == Signature(Type({sharedExternref}), i32)) {
217+
testSharedImport = func->name;
218+
found = true;
219+
} else {
169220
Fatal() << "StringLifting: bad type for test: " << type;
170221
}
171-
testImport = func->name;
172-
found = true;
173222
} else if (func->base == "compare") {
174-
if (type.getHeapType() != Signature({externref, externref}, i32)) {
223+
if (type.getHeapType() == Signature(Type({externref, externref}), i32)) {
224+
compareImport = func->name;
225+
found = true;
226+
} else if (type.getHeapType() == Signature(Type({sharedExternref,
227+
sharedExternref}),
228+
i32)) {
229+
compareSharedImport = func->name;
230+
found = true;
231+
} else {
175232
Fatal() << "StringLifting: bad type for compare: " << type;
176233
}
177-
compareImport = func->name;
178-
found = true;
179234
} else if (func->base == "length") {
180-
if (type.getHeapType() != Signature({externref}, i32)) {
235+
if (type.getHeapType() == Signature(Type({externref}), i32)) {
236+
lengthImport = func->name;
237+
found = true;
238+
} else if (type.getHeapType() == Signature(Type({sharedExternref}), i32)) {
239+
lengthSharedImport = func->name;
240+
found = true;
241+
} else {
181242
Fatal() << "StringLifting: bad type for length: " << type;
182243
}
183-
lengthImport = func->name;
184-
found = true;
185244
} else if (func->base == "charCodeAt") {
186-
if (type.getHeapType() != Signature({externref, i32}, i32)) {
245+
if (type.getHeapType() == Signature(Type({externref, i32}), i32)) {
246+
charCodeAtImport = func->name;
247+
found = true;
248+
} else if (type.getHeapType() == Signature(Type({sharedExternref, i32}), i32)) {
249+
charCodeAtSharedImport = func->name;
250+
found = true;
251+
} else {
187252
Fatal() << "StringLifting: bad type for charCodeAt: " << type;
188253
}
189-
charCodeAtImport = func->name;
190-
found = true;
191254
} else if (func->base == "substring") {
192-
if (type.getHeapType() != Signature({externref, i32, i32}, refExtern)) {
255+
if (type.getHeapType() == Signature(Type({externref, i32, i32}), refExtern)) {
256+
substringImport = func->name;
257+
found = true;
258+
} else if (type.getHeapType() == Signature(Type({sharedExternref,
259+
i32,
260+
i32}),
261+
refSharedExtern)) {
262+
substringSharedImport = func->name;
263+
found = true;
264+
} else {
193265
Fatal() << "StringLifting: bad type for substring: " << type;
194266
}
195-
substringImport = func->name;
196-
found = true;
197267
} else {
198268
std::cerr << "warning: unknown strings import: " << func->base << '\n';
199269
}
@@ -228,53 +298,54 @@ struct StringLifting : public Pass {
228298
}
229299

230300
void visitCall(Call* curr) {
301+
Builder builder(*getModule());
231302
// Replace calls of imported string methods with stringref operations.
232-
if (curr->target == parent.fromCharCodeArrayImport) {
233-
replaceCurrent(Builder(*getModule())
234-
.makeStringNew(StringNewWTF16Array,
235-
curr->operands[0],
236-
curr->operands[1],
237-
curr->operands[2]));
238-
} else if (curr->target == parent.fromCodePointImport) {
239-
replaceCurrent(
240-
Builder(*getModule())
241-
.makeStringNew(StringNewFromCodePoint, curr->operands[0]));
242-
} else if (curr->target == parent.concatImport) {
243-
replaceCurrent(
244-
Builder(*getModule())
245-
.makeStringConcat(curr->operands[0], curr->operands[1]));
246-
} else if (curr->target == parent.intoCharCodeArrayImport) {
247-
replaceCurrent(Builder(*getModule())
248-
.makeStringEncode(StringEncodeWTF16Array,
249-
curr->operands[0],
250-
curr->operands[1],
251-
curr->operands[2]));
252-
} else if (curr->target == parent.equalsImport) {
253-
replaceCurrent(Builder(*getModule())
254-
.makeStringEq(StringEqEqual,
255-
curr->operands[0],
256-
curr->operands[1]));
257-
} else if (curr->target == parent.testImport) {
258-
replaceCurrent(
259-
Builder(*getModule()).makeStringTest(curr->operands[0]));
260-
} else if (curr->target == parent.compareImport) {
261-
replaceCurrent(Builder(*getModule())
262-
.makeStringEq(StringEqCompare,
263-
curr->operands[0],
264-
curr->operands[1]));
265-
} else if (curr->target == parent.lengthImport) {
266-
replaceCurrent(
267-
Builder(*getModule())
268-
.makeStringMeasure(StringMeasureWTF16, curr->operands[0]));
269-
} else if (curr->target == parent.charCodeAtImport) {
270-
replaceCurrent(
271-
Builder(*getModule())
272-
.makeStringWTF16Get(curr->operands[0], curr->operands[1]));
273-
} else if (curr->target == parent.substringImport) {
274-
replaceCurrent(Builder(*getModule())
275-
.makeStringSliceWTF(curr->operands[0],
303+
if (curr->target == parent.fromCharCodeArrayImport ||
304+
curr->target == parent.fromCharCodeArraySharedImport) {
305+
replaceCurrent(builder.makeStringNew(StringNewWTF16Array,
306+
curr->operands[0],
276307
curr->operands[1],
277308
curr->operands[2]));
309+
} else if (curr->target == parent.fromCodePointImport ||
310+
curr->target == parent.fromCodePointSharedImport) {
311+
replaceCurrent(builder.makeStringNew(StringNewFromCodePoint,
312+
curr->operands[0]));
313+
} else if (curr->target == parent.concatImport ||
314+
curr->target == parent.concatSharedImport) {
315+
replaceCurrent(builder.makeStringConcat(curr->operands[0],
316+
curr->operands[1]));
317+
} else if (curr->target == parent.intoCharCodeArrayImport ||
318+
curr->target == parent.intoCharCodeArraySharedImport) {
319+
replaceCurrent(builder.makeStringEncode(StringEncodeWTF16Array,
320+
curr->operands[0],
321+
curr->operands[1],
322+
curr->operands[2]));
323+
} else if (curr->target == parent.equalsImport ||
324+
curr->target == parent.equalsSharedImport) {
325+
replaceCurrent(builder.makeStringEq(StringEqEqual,
326+
curr->operands[0],
327+
curr->operands[1]));
328+
} else if (curr->target == parent.testImport ||
329+
curr->target == parent.testSharedImport) {
330+
replaceCurrent(builder.makeStringTest(curr->operands[0]));
331+
} else if (curr->target == parent.compareImport ||
332+
curr->target == parent.compareSharedImport) {
333+
replaceCurrent(builder.makeStringEq(StringEqCompare,
334+
curr->operands[0],
335+
curr->operands[1]));
336+
} else if (curr->target == parent.lengthImport ||
337+
curr->target == parent.lengthSharedImport) {
338+
replaceCurrent(builder.makeStringMeasure(StringMeasureWTF16,
339+
curr->operands[0]));
340+
} else if (curr->target == parent.charCodeAtImport ||
341+
curr->target == parent.charCodeAtSharedImport) {
342+
replaceCurrent(builder.makeStringWTF16Get(curr->operands[0],
343+
curr->operands[1]));
344+
} else if (curr->target == parent.substringImport ||
345+
curr->target == parent.substringSharedImport) {
346+
replaceCurrent(builder.makeStringSliceWTF(curr->operands[0],
347+
curr->operands[1],
348+
curr->operands[2]));
278349
}
279350
}
280351

0 commit comments

Comments
 (0)