Skip to content

[Strings] Erase the strings section after StringLifting #7546

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 4 commits into from
Apr 24, 2025
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
66 changes: 35 additions & 31 deletions src/passes/StringLifting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,45 @@ struct StringLifting : public Pass {
}

// Imported strings may also be found in the string section.
for (auto& section : module->customSections) {
if (section.name == "string.consts") {
// We found the string consts section. Parse it.
auto copy = section.data;
json::Value array;
array.parse(copy.data(), json::Value::WTF16);
if (!array.isArray()) {
auto stringSectionIter = std::find_if(
module->customSections.begin(),
module->customSections.end(),
[&](CustomSection& section) { return section.name == "string.consts"; });
if (stringSectionIter != module->customSections.end()) {
// We found the string consts section. Parse it.
auto& section = *stringSectionIter;
auto copy = section.data;
json::Value array;
array.parse(copy.data(), json::Value::WTF16);
if (!array.isArray()) {
Fatal() << "StringLifting: string.const section should be a JSON array";
}

// We have the array of constants from the section. Find globals that
// refer to it.
for (auto& global : module->globals) {
if (!global->imported() || global->module != "string.const") {
continue;
}
// The index in the array is the basename.
Index index = std::stoi(std::string(global->base.str));
if (index >= array.size()) {
Fatal() << "StringLifting: bad index in string.const section";
}
auto item = array[index];
if (!item->isString()) {
Fatal()
<< "StringLifting: string.const section should be a JSON array";
<< "StringLifting: string.const section entry is not a string";
}

// We have the array of constants from the section. Find globals that
// refer to it.
for (auto& global : module->globals) {
if (!global->imported() || global->module != "string.const") {
continue;
}
// The index in the array is the basename.
Index index = std::stoi(std::string(global->base.str));
if (index >= array.size()) {
Fatal() << "StringLifting: bad index in string.const section";
}
auto item = array[index];
if (!item->isString()) {
Fatal()
<< "StringLifting: string.const section entry is not a string";
}
if (importedStrings.count(global->name)) {
Fatal()
<< "StringLifting: string.const section tramples other const";
}
importedStrings[global->name] = item->getIString();
if (importedStrings.count(global->name)) {
Fatal() << "StringLifting: string.const section tramples other const";
}
break;
importedStrings[global->name] = item->getIString();
}

// Remove the custom section: After lifting it has no purpose (and could
// cause problems with repeated lifting/lowering).
module->customSections.erase(stringSectionIter);
}

auto array16 = Type(Array(Field(Field::i16, Mutable)), Nullable);
Expand Down
23 changes: 23 additions & 0 deletions test/lit/passes/string-lifting-section-erase.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;; Test that the section vanishes after lifting.

(module
(func $strings
;; These strings cannot appear as magic imports, and will definitely go in
;; the strings section.
(drop
(string.const "unpaired high surrogate \ED\A0\80 ")
)
(drop
(string.const "unpaired low surrogate \ED\BD\88 ")
)
)
)

;; Lower into the section. We should see the section.
;; RUN: wasm-opt %s -all --string-lowering -S -o - | filecheck %s --check-prefix=LOWER
;; LOWER: custom section

;; Also lift. Now no section should appear.
;; RUN: wasm-opt %s -all --string-lowering --string-lifting -S -o - | filecheck %s --check-prefix=AND_LIFT
;; AND_LIFT-NOT: custom section

Loading