Skip to content

Commit ad6a124

Browse files
authored
Only generate string.consts custom section if it is needed (WebAssembly#6893)
1 parent 67bd842 commit ad6a124

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/passes/StringLowering.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ struct StringLowering : public StringGathering {
235235
void makeImports(Module* module) {
236236
Index jsonImportIndex = 0;
237237
std::stringstream json;
238-
json << '[';
239238
bool first = true;
240239
for (auto& global : module->globals) {
241240
if (global->init) {
@@ -267,12 +266,16 @@ struct StringLowering : public StringGathering {
267266
}
268267
}
269268

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

278281
// Common types used in imports.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;; This file checks no custom section added by --string-lowering-magic-imports if there
2+
;; are is only valid string constants.
3+
4+
(module
5+
(func $consts
6+
(drop
7+
(string.const "foo")
8+
)
9+
)
10+
)
11+
12+
;; The custom section should not exist with magic imports.
13+
;;
14+
;; RUN: wasm-opt %s --string-lowering-magic-imports -all -S -o - \
15+
;; RUN: | filecheck %s
16+
;;
17+
;; Same behavior when using magic imports with asserts enabled.
18+
;;
19+
;; RUN: wasm-opt %s --string-lowering-magic-imports-assert -all -S -o - \
20+
;; RUN: | filecheck %s
21+
;;
22+
;; CHECK-NOT: custom section

0 commit comments

Comments
 (0)