From da600313f606a4eff590543106d12f15d07d1dda Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Tue, 7 Jan 2025 16:59:53 -0800 Subject: [PATCH 1/2] Make RemoveMemory pass also remove the start function Also rename it to RemoveMemoryInit to make it more clear what it's for. --- src/passes/CMakeLists.txt | 2 +- ...{RemoveMemory.cpp => RemoveMemoryInit.cpp} | 11 ++++++++--- src/passes/pass.cpp | 4 +++- src/passes/passes.h | 2 +- src/tools/wasm-reduce.cpp | 2 +- test/lit/help/wasm-metadce.test | 5 ++++- test/lit/help/wasm-opt.test | 5 ++++- test/lit/help/wasm2js.test | 5 ++++- test/lit/passes/remove-memory-init.wast | 19 +++++++++++++++++++ test/passes/remove-memory.txt | 3 --- test/passes/remove-memory.wast | 5 ----- 11 files changed, 45 insertions(+), 18 deletions(-) rename src/passes/{RemoveMemory.cpp => RemoveMemoryInit.cpp} (69%) create mode 100644 test/lit/passes/remove-memory-init.wast delete mode 100644 test/passes/remove-memory.txt delete mode 100644 test/passes/remove-memory.wast diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt index 61c1b2f4610..ed816ba09d4 100644 --- a/src/passes/CMakeLists.txt +++ b/src/passes/CMakeLists.txt @@ -102,7 +102,7 @@ set(passes_SOURCES TraceCalls.cpp RedundantSetElimination.cpp RemoveImports.cpp - RemoveMemory.cpp + RemoveMemoryInit.cpp RemoveNonJSOps.cpp RemoveUnusedBrs.cpp RemoveUnusedNames.cpp diff --git a/src/passes/RemoveMemory.cpp b/src/passes/RemoveMemoryInit.cpp similarity index 69% rename from src/passes/RemoveMemory.cpp rename to src/passes/RemoveMemoryInit.cpp index 1520be1e8db..80197f62cd1 100644 --- a/src/passes/RemoveMemory.cpp +++ b/src/passes/RemoveMemoryInit.cpp @@ -15,7 +15,8 @@ */ // -// Removeds memory segments, leaving only code in the module. +// Removes memory segments, leaving only code in the module. It also removes +// the start function, which is only used for initializing the memory. // #include @@ -23,12 +24,16 @@ namespace wasm { -struct RemoveMemory : public Pass { +struct RemoveMemoryInit : public Pass { void run(Module* module) override { module->removeDataSegments([&](DataSegment* curr) { return true; }); + if (module->start) { + module->removeFunction(module->start); + module->start = Name(); + } } }; -Pass* createRemoveMemoryPass() { return new RemoveMemory(); } +Pass* createRemoveMemoryInitPass() { return new RemoveMemoryInit(); } } // namespace wasm diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index ab95300d0cb..bc1c314aeb8 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -412,7 +412,9 @@ void PassRegistry::registerPasses() { "removes imports and replaces them with nops", createRemoveImportsPass); registerPass( - "remove-memory", "removes memory segments", createRemoveMemoryPass); + "remove-memory-init", "removes memory initialization", createRemoveMemoryInitPass); + registerPass( + "remove-memory", "removes memory init (legacy name)", createRemoveMemoryInitPass); registerPass("remove-unused-brs", "removes breaks from locations that are not needed", createRemoveUnusedBrsPass); diff --git a/src/passes/passes.h b/src/passes/passes.h index b313b343165..81d7cdcc7ff 100644 --- a/src/passes/passes.h +++ b/src/passes/passes.h @@ -132,7 +132,7 @@ Pass* createPrintFunctionMapPass(); Pass* createPropagateGlobalsGloballyPass(); Pass* createRemoveNonJSOpsPass(); Pass* createRemoveImportsPass(); -Pass* createRemoveMemoryPass(); +Pass* createRemoveMemoryInitPass(); Pass* createRemoveUnusedBrsPass(); Pass* createRemoveUnusedModuleElementsPass(); Pass* createRemoveUnusedNonFunctionModuleElementsPass(); diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 6613c8899bf..6b89103a3ac 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -294,7 +294,7 @@ struct Reducer "--optimize-instructions", "--precompute", "--remove-imports", - "--remove-memory", + "--remove-memory-init", "--remove-unused-names --remove-unused-brs", "--remove-unused-module-elements", "--remove-unused-nonfunction-module-elements", diff --git a/test/lit/help/wasm-metadce.test b/test/lit/help/wasm-metadce.test index 0be234ac791..fa68453a76d 100644 --- a/test/lit/help/wasm-metadce.test +++ b/test/lit/help/wasm-metadce.test @@ -388,7 +388,10 @@ ;; CHECK-NEXT: --remove-imports removes imports and replaces ;; CHECK-NEXT: them with nops ;; CHECK-NEXT: -;; CHECK-NEXT: --remove-memory removes memory segments +;; CHECK-NEXT: --remove-memory removes memory init (legacy +;; CHECK-NEXT: name) +;; CHECK-NEXT: +;; CHECK-NEXT: --remove-memory-init removes memory initialization ;; CHECK-NEXT: ;; CHECK-NEXT: --remove-non-js-ops removes operations incompatible ;; CHECK-NEXT: with js diff --git a/test/lit/help/wasm-opt.test b/test/lit/help/wasm-opt.test index 630a64588a1..49130d2a9eb 100644 --- a/test/lit/help/wasm-opt.test +++ b/test/lit/help/wasm-opt.test @@ -397,7 +397,10 @@ ;; CHECK-NEXT: --remove-imports removes imports and replaces ;; CHECK-NEXT: them with nops ;; CHECK-NEXT: -;; CHECK-NEXT: --remove-memory removes memory segments +;; CHECK-NEXT: --remove-memory removes memory init (legacy +;; CHECK-NEXT: name) +;; CHECK-NEXT: +;; CHECK-NEXT: --remove-memory-init removes memory initialization ;; CHECK-NEXT: ;; CHECK-NEXT: --remove-non-js-ops removes operations incompatible ;; CHECK-NEXT: with js diff --git a/test/lit/help/wasm2js.test b/test/lit/help/wasm2js.test index a326c75dbe9..34a42d2f4a6 100644 --- a/test/lit/help/wasm2js.test +++ b/test/lit/help/wasm2js.test @@ -351,7 +351,10 @@ ;; CHECK-NEXT: --remove-imports removes imports and replaces ;; CHECK-NEXT: them with nops ;; CHECK-NEXT: -;; CHECK-NEXT: --remove-memory removes memory segments +;; CHECK-NEXT: --remove-memory removes memory init (legacy +;; CHECK-NEXT: name) +;; CHECK-NEXT: +;; CHECK-NEXT: --remove-memory-init removes memory initialization ;; CHECK-NEXT: ;; CHECK-NEXT: --remove-non-js-ops removes operations incompatible ;; CHECK-NEXT: with js diff --git a/test/lit/passes/remove-memory-init.wast b/test/lit/passes/remove-memory-init.wast new file mode 100644 index 00000000000..d4f5e4cfdfd --- /dev/null +++ b/test/lit/passes/remove-memory-init.wast @@ -0,0 +1,19 @@ +;; RUN: wasm-opt %s --remove-memory-init -all -S -o - | filecheck %s + +(module + (type $0 (func)) + ;; CHECK: (memory $mem 2) + (memory $mem 2) + (data (memory $mem) (i32.const 0) "Hello, world\00") + (func $__wasm_init_memory (type $0) + (memory.fill 0 + (i32.const 0) + (i32.const 0) + (i32.const 0) + ) + ) + (start $__wasm_init_memory) +) + +;; CHECK-NOT: data +;; CHECK-NOT: __wasm_init_memory diff --git a/test/passes/remove-memory.txt b/test/passes/remove-memory.txt deleted file mode 100644 index ddfdde49329..00000000000 --- a/test/passes/remove-memory.txt +++ /dev/null @@ -1,3 +0,0 @@ -(module - (memory $mem 1024 1024) -) diff --git a/test/passes/remove-memory.wast b/test/passes/remove-memory.wast deleted file mode 100644 index 26b4949a81f..00000000000 --- a/test/passes/remove-memory.wast +++ /dev/null @@ -1,5 +0,0 @@ -(module - (memory $mem 1024 1024) - (data (memory $mem) (i32.const 10) "123") - (data (memory $mem) (i32.const 20) "149") -) From 395a69ffece04c23b027de2a8e1a087fdb5c71cd Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 8 Jan 2025 09:02:59 -0800 Subject: [PATCH 2/2] apply suggestions, clang-format --- src/passes/RemoveMemoryInit.cpp | 4 ++-- src/passes/pass.cpp | 10 ++++++---- test/lit/passes/remove-memory-init.wast | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/passes/RemoveMemoryInit.cpp b/src/passes/RemoveMemoryInit.cpp index 80197f62cd1..6c4cc78c67a 100644 --- a/src/passes/RemoveMemoryInit.cpp +++ b/src/passes/RemoveMemoryInit.cpp @@ -16,8 +16,8 @@ // // Removes memory segments, leaving only code in the module. It also removes -// the start function, which is only used for initializing the memory. -// +// the start function, which (in LLVM use cases) is only used for initializing +// the memory. #include #include diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index bc1c314aeb8..5e690790bb4 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -411,10 +411,12 @@ void PassRegistry::registerPasses() { registerPass("remove-imports", "removes imports and replaces them with nops", createRemoveImportsPass); - registerPass( - "remove-memory-init", "removes memory initialization", createRemoveMemoryInitPass); - registerPass( - "remove-memory", "removes memory init (legacy name)", createRemoveMemoryInitPass); + registerPass("remove-memory-init", + "removes memory initialization", + createRemoveMemoryInitPass); + registerPass("remove-memory", + "removes memory init (legacy name)", + createRemoveMemoryInitPass); registerPass("remove-unused-brs", "removes breaks from locations that are not needed", createRemoveUnusedBrsPass); diff --git a/test/lit/passes/remove-memory-init.wast b/test/lit/passes/remove-memory-init.wast index d4f5e4cfdfd..dd746ff8b58 100644 --- a/test/lit/passes/remove-memory-init.wast +++ b/test/lit/passes/remove-memory-init.wast @@ -5,7 +5,7 @@ ;; CHECK: (memory $mem 2) (memory $mem 2) (data (memory $mem) (i32.const 0) "Hello, world\00") - (func $__wasm_init_memory (type $0) + (func $__wasm_init_memory (type $0) (memory.fill 0 (i32.const 0) (i32.const 0)