Skip to content

Commit e276453

Browse files
authored
Make RemoveMemory pass also remove the start function (#7196)
Also rename it to RemoveMemoryInit to make it more clear what it's for. For now, keep the legacy pass name, to be deleted after rolling into Emscripten.
1 parent 6804a90 commit e276453

File tree

11 files changed

+49
-20
lines changed

11 files changed

+49
-20
lines changed

src/passes/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ set(passes_SOURCES
102102
TraceCalls.cpp
103103
RedundantSetElimination.cpp
104104
RemoveImports.cpp
105-
RemoveMemory.cpp
105+
RemoveMemoryInit.cpp
106106
RemoveNonJSOps.cpp
107107
RemoveUnusedBrs.cpp
108108
RemoveUnusedNames.cpp
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@
1515
*/
1616

1717
//
18-
// Removeds memory segments, leaving only code in the module.
19-
//
18+
// Removes memory segments, leaving only code in the module. It also removes
19+
// the start function, which (in LLVM use cases) is only used for initializing
20+
// the memory.
2021

2122
#include <pass.h>
2223
#include <wasm.h>
2324

2425
namespace wasm {
2526

26-
struct RemoveMemory : public Pass {
27+
struct RemoveMemoryInit : public Pass {
2728
void run(Module* module) override {
2829
module->removeDataSegments([&](DataSegment* curr) { return true; });
30+
if (module->start) {
31+
module->removeFunction(module->start);
32+
module->start = Name();
33+
}
2934
}
3035
};
3136

32-
Pass* createRemoveMemoryPass() { return new RemoveMemory(); }
37+
Pass* createRemoveMemoryInitPass() { return new RemoveMemoryInit(); }
3338

3439
} // namespace wasm

src/passes/pass.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,12 @@ void PassRegistry::registerPasses() {
411411
registerPass("remove-imports",
412412
"removes imports and replaces them with nops",
413413
createRemoveImportsPass);
414-
registerPass(
415-
"remove-memory", "removes memory segments", createRemoveMemoryPass);
414+
registerPass("remove-memory-init",
415+
"removes memory initialization",
416+
createRemoveMemoryInitPass);
417+
registerPass("remove-memory",
418+
"removes memory init (legacy name)",
419+
createRemoveMemoryInitPass);
416420
registerPass("remove-unused-brs",
417421
"removes breaks from locations that are not needed",
418422
createRemoveUnusedBrsPass);

src/passes/passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Pass* createPrintFunctionMapPass();
132132
Pass* createPropagateGlobalsGloballyPass();
133133
Pass* createRemoveNonJSOpsPass();
134134
Pass* createRemoveImportsPass();
135-
Pass* createRemoveMemoryPass();
135+
Pass* createRemoveMemoryInitPass();
136136
Pass* createRemoveUnusedBrsPass();
137137
Pass* createRemoveUnusedModuleElementsPass();
138138
Pass* createRemoveUnusedNonFunctionModuleElementsPass();

src/tools/wasm-reduce.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ struct Reducer
294294
"--optimize-instructions",
295295
"--precompute",
296296
"--remove-imports",
297-
"--remove-memory",
297+
"--remove-memory-init",
298298
"--remove-unused-names --remove-unused-brs",
299299
"--remove-unused-module-elements",
300300
"--remove-unused-nonfunction-module-elements",

test/lit/help/wasm-metadce.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,10 @@
388388
;; CHECK-NEXT: --remove-imports removes imports and replaces
389389
;; CHECK-NEXT: them with nops
390390
;; CHECK-NEXT:
391-
;; CHECK-NEXT: --remove-memory removes memory segments
391+
;; CHECK-NEXT: --remove-memory removes memory init (legacy
392+
;; CHECK-NEXT: name)
393+
;; CHECK-NEXT:
394+
;; CHECK-NEXT: --remove-memory-init removes memory initialization
392395
;; CHECK-NEXT:
393396
;; CHECK-NEXT: --remove-non-js-ops removes operations incompatible
394397
;; CHECK-NEXT: with js

test/lit/help/wasm-opt.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@
397397
;; CHECK-NEXT: --remove-imports removes imports and replaces
398398
;; CHECK-NEXT: them with nops
399399
;; CHECK-NEXT:
400-
;; CHECK-NEXT: --remove-memory removes memory segments
400+
;; CHECK-NEXT: --remove-memory removes memory init (legacy
401+
;; CHECK-NEXT: name)
402+
;; CHECK-NEXT:
403+
;; CHECK-NEXT: --remove-memory-init removes memory initialization
401404
;; CHECK-NEXT:
402405
;; CHECK-NEXT: --remove-non-js-ops removes operations incompatible
403406
;; CHECK-NEXT: with js

test/lit/help/wasm2js.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@
351351
;; CHECK-NEXT: --remove-imports removes imports and replaces
352352
;; CHECK-NEXT: them with nops
353353
;; CHECK-NEXT:
354-
;; CHECK-NEXT: --remove-memory removes memory segments
354+
;; CHECK-NEXT: --remove-memory removes memory init (legacy
355+
;; CHECK-NEXT: name)
356+
;; CHECK-NEXT:
357+
;; CHECK-NEXT: --remove-memory-init removes memory initialization
355358
;; CHECK-NEXT:
356359
;; CHECK-NEXT: --remove-non-js-ops removes operations incompatible
357360
;; CHECK-NEXT: with js
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
;; RUN: wasm-opt %s --remove-memory-init -all -S -o - | filecheck %s
2+
3+
(module
4+
(type $0 (func))
5+
;; CHECK: (memory $mem 2)
6+
(memory $mem 2)
7+
(data (memory $mem) (i32.const 0) "Hello, world\00")
8+
(func $__wasm_init_memory (type $0)
9+
(memory.fill 0
10+
(i32.const 0)
11+
(i32.const 0)
12+
(i32.const 0)
13+
)
14+
)
15+
(start $__wasm_init_memory)
16+
)
17+
18+
;; CHECK-NOT: data
19+
;; CHECK-NOT: __wasm_init_memory

test/passes/remove-memory.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)