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

Make RemoveMemory pass also remove the start function #7196

Merged
merged 2 commits into from
Jan 8, 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
2 changes: 1 addition & 1 deletion src/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ set(passes_SOURCES
TraceCalls.cpp
RedundantSetElimination.cpp
RemoveImports.cpp
RemoveMemory.cpp
RemoveMemoryInit.cpp
RemoveNonJSOps.cpp
RemoveUnusedBrs.cpp
RemoveUnusedNames.cpp
Expand Down
13 changes: 9 additions & 4 deletions src/passes/RemoveMemory.cpp → src/passes/RemoveMemoryInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@
*/

//
// 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 (in LLVM use cases) is only used for initializing
// the memory.

#include <pass.h>
#include <wasm.h>

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
8 changes: 6 additions & 2 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,12 @@ void PassRegistry::registerPasses() {
registerPass("remove-imports",
"removes imports and replaces them with nops",
createRemoveImportsPass);
registerPass(
"remove-memory", "removes memory segments", createRemoveMemoryPass);
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);
Expand Down
2 changes: 1 addition & 1 deletion src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Pass* createPrintFunctionMapPass();
Pass* createPropagateGlobalsGloballyPass();
Pass* createRemoveNonJSOpsPass();
Pass* createRemoveImportsPass();
Pass* createRemoveMemoryPass();
Pass* createRemoveMemoryInitPass();
Pass* createRemoveUnusedBrsPass();
Pass* createRemoveUnusedModuleElementsPass();
Pass* createRemoveUnusedNonFunctionModuleElementsPass();
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion test/lit/help/wasm-metadce.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/lit/help/wasm-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/lit/help/wasm2js.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/lit/passes/remove-memory-init.wast
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions test/passes/remove-memory.txt

This file was deleted.

5 changes: 0 additions & 5 deletions test/passes/remove-memory.wast

This file was deleted.

Loading