Skip to content
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

Only generate string.consts custom section if it is needed. #6893

Merged
merged 5 commits into from
Sep 5, 2024
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
17 changes: 10 additions & 7 deletions src/passes/StringLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ struct StringLowering : public StringGathering {
void makeImports(Module* module) {
Index jsonImportIndex = 0;
std::stringstream json;
json << '[';
bool first = true;
for (auto& global : module->globals) {
if (global->init) {
Expand Down Expand Up @@ -267,12 +266,16 @@ struct StringLowering : public StringGathering {
}
}

// Add a custom section with the JSON.
json << ']';
auto str = json.str();
auto vec = std::vector<char>(str.begin(), str.end());
module->customSections.emplace_back(
CustomSection{"string.consts", std::move(vec)});
auto jsonString = json.str();
if (!jsonString.empty()) {
// If we are asserting UTF8, then we shouldn't be generating any JSON.
assert(!assertUTF8);
// Add a custom section with the JSON.
auto str = '[' + jsonString + ']';
auto vec = std::vector<char>(str.begin(), str.end());
module->customSections.emplace_back(
CustomSection{"string.consts", std::move(vec)});
}
}

// Common types used in imports.
Expand Down
22 changes: 22 additions & 0 deletions test/lit/passes/string-lowering_empty.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;; This file checks no custom section added by --string-lowering-magic-imports if there
;; are is only valid string constants.

(module
(func $consts
(drop
(string.const "foo")
)
)
)

;; The custom section should not exist with magic imports.
;;
;; RUN: wasm-opt %s --string-lowering-magic-imports -all -S -o - \
;; RUN: | filecheck %s
;;
;; Same behavior when using magic imports with asserts enabled.
;;
;; RUN: wasm-opt %s --string-lowering-magic-imports-assert -all -S -o - \
;; RUN: | filecheck %s
;;
;; CHECK-NOT: custom section
Loading