From 10bf7294ed5eb3f88fd3e96401d45947642982e8 Mon Sep 17 00:00:00 2001 From: Goktug Gokdogan Date: Fri, 30 Aug 2024 20:36:37 -0700 Subject: [PATCH 1/5] Only generate string.consts custom section if it is needed. --- src/passes/StringLowering.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp index 92e3268dc22..594c05550b4 100644 --- a/src/passes/StringLowering.cpp +++ b/src/passes/StringLowering.cpp @@ -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) { @@ -267,12 +266,15 @@ struct StringLowering : public StringGathering { } } - // Add a custom section with the JSON. - json << ']'; - auto str = json.str(); - auto vec = std::vector(str.begin(), str.end()); - module->customSections.emplace_back( - CustomSection{"string.consts", std::move(vec)}); + if (json) { + // If we are asserting UTF8, then we shouldn't be generating any JSON. + assert(!assertUTF8); + // Add a custom section with the JSON. + auto str = '[' + json.str() + ']'; + auto vec = std::vector(str.begin(), str.end()); + module->customSections.emplace_back( + CustomSection{"string.consts", std::move(vec)}); + } } // Common types used in imports. From b886fee93fed4d3fa8fb6cd3db270fc5ebf7fa25 Mon Sep 17 00:00:00 2001 From: Goktug Gokdogan Date: Wed, 4 Sep 2024 14:26:28 -0700 Subject: [PATCH 2/5] Fix empty json check --- src/passes/StringLowering.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp index 594c05550b4..bb89f7c1bdb 100644 --- a/src/passes/StringLowering.cpp +++ b/src/passes/StringLowering.cpp @@ -191,7 +191,7 @@ struct StringGathering : public Pass { struct StringLowering : public StringGathering { // If true, then encode well-formed strings as (import "'" "string...") - // instead of emitting them into the JSON custom section. + // instead of emitting them into the x custom section. bool useMagicImports; // Whether to throw a fatal error on non-UTF8 strings that would not be able @@ -266,11 +266,12 @@ struct StringLowering : public StringGathering { } } - if (json) { + 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 = '[' + json.str() + ']'; + auto str = '[' + jsonString + ']'; auto vec = std::vector(str.begin(), str.end()); module->customSections.emplace_back( CustomSection{"string.consts", std::move(vec)}); From 3b9d261b58cbc460f0ecaf505bc016d251b80305 Mon Sep 17 00:00:00 2001 From: Goktug Gokdogan Date: Wed, 4 Sep 2024 16:42:29 -0700 Subject: [PATCH 3/5] add tests --- test/lit/passes/string-lowering_empty.wast | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test/lit/passes/string-lowering_empty.wast diff --git a/test/lit/passes/string-lowering_empty.wast b/test/lit/passes/string-lowering_empty.wast new file mode 100644 index 00000000000..757b6f8e881 --- /dev/null +++ b/test/lit/passes/string-lowering_empty.wast @@ -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 \ No newline at end of file From f64ceef86e3954477b02366ac5299bec73edfba5 Mon Sep 17 00:00:00 2001 From: Goktug Gokdogan Date: Wed, 4 Sep 2024 17:16:02 -0700 Subject: [PATCH 4/5] fix typo --- src/passes/StringLowering.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp index bb89f7c1bdb..349ba8cd00f 100644 --- a/src/passes/StringLowering.cpp +++ b/src/passes/StringLowering.cpp @@ -191,7 +191,7 @@ struct StringGathering : public Pass { struct StringLowering : public StringGathering { // If true, then encode well-formed strings as (import "'" "string...") - // instead of emitting them into the x custom section. + // instead of emitting them into the JSON custom section. bool useMagicImports; // Whether to throw a fatal error on non-UTF8 strings that would not be able From 35abe72a82128ebe16c099eba77c8606c6e46df8 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 4 Sep 2024 17:40:03 -0700 Subject: [PATCH 5/5] Update test/lit/passes/string-lowering_empty.wast --- test/lit/passes/string-lowering_empty.wast | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lit/passes/string-lowering_empty.wast b/test/lit/passes/string-lowering_empty.wast index 757b6f8e881..b00bca9be93 100644 --- a/test/lit/passes/string-lowering_empty.wast +++ b/test/lit/passes/string-lowering_empty.wast @@ -19,4 +19,4 @@ ;; RUN: wasm-opt %s --string-lowering-magic-imports-assert -all -S -o - \ ;; RUN: | filecheck %s ;; -;; CHECK-NOT: custom section \ No newline at end of file +;; CHECK-NOT: custom section