Skip to content

Commit 132f7e8

Browse files
authored
[wasm-split] Remove dead globals (WebAssembly#8505)
Currently dead module items are not split and just end up remaining in the primary module. Usually a user runs DCE before or after the splitting, and the goal of wasm-split is not DCE, so from the optimization perspective it shouldn't be a problem. But after WebAssembly#8441, this can be a problem because a dead global's initializer can refer to another global that is moved to a secondary module: ```wast ;; Primary (global.get $dead i32 (global.get $a)) ;; Secondary (global $a i32 (...)) ``` This PR just removes those dead globals. We leave it and do some post processing to make it work but that's more complicated, or we can move it to the same secondary module but this requires scanning of the reverse mapping. Removing it seems the simplest. We can remove dead items for other module items (memories, tables, and tags) but it is not necessary for wasm-split to run, and they can be handled later in DCE. Fixes WebAssembly#8442 (comment).
1 parent 82c0c99 commit 132f7e8

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/ir/module-splitting.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,18 @@ void ModuleSplitter::shareImportableItems() {
11821182

11831183
auto usingSecondaries =
11841184
getUsingSecondaries(global->name, &UsedNames::globals);
1185-
bool usedInPrimary = primaryUsed.globals.count(global->name);
1186-
if (!usedInPrimary && usingSecondaries.size() == 1) {
1185+
bool inPrimary = primaryUsed.globals.count(global->name);
1186+
1187+
if (!inPrimary && usingSecondaries.empty()) {
1188+
// It's not used anywhere, so delete it. Unlike other unused module items
1189+
// (memories, tables, and tags) that can just sit in the primary module
1190+
// and later be DCE'ed by another pass, we should remove it here, because
1191+
// an unused global can contain an initialier that refers to another
1192+
// global that will be moved to a secondary module, like
1193+
// (global $unused i32 (global.get $a)) // $a is moved to a secondary
1194+
globalsToRemove.push_back(global->name);
1195+
1196+
} else if (!inPrimary && usingSecondaries.size() == 1) {
11871197
// We are moving this global to this secondary module
11881198
auto* secondary = usingSecondaries[0];
11891199
auto* secondaryGlobal = ModuleUtils::copyGlobal(global.get(), *secondary);
@@ -1213,6 +1223,7 @@ void ModuleSplitter::shareImportableItems() {
12131223
// function.
12141224
}
12151225
}
1226+
12161227
} else { // We are NOT moving this global to the secondary module
12171228
if (global->init) {
12181229
for (auto* ref : FindAll<RefFunc>(global->init).list) {

test/example/module-splitting.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Keeping: <none>
2222
After:
2323
(module
2424
(type $0 (func (param i32)))
25-
(global $glob (mut i32) (i32.const 7))
2625
(memory $mem 3 42 shared)
2726
(table $tab 3 42 funcref)
2827
(tag $e (type $0) (param i32))
@@ -46,7 +45,6 @@ After:
4645
(type $0 (func (param i32)))
4746
(import "env" "mem" (memory $mem 3 42 shared))
4847
(import "env" "tab" (table $tab 3 42 funcref))
49-
(import "env" "glob" (global $glob (mut i32)))
5048
(import "env" "e" (tag $e (type $0) (param i32)))
5149
)
5250
Secondary:

test/lit/wasm-split/ref.func.wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
;; PRIMARY-NEXT: (drop
4646
;; PRIMARY-NEXT: (ref.func $trampoline_second)
4747
;; PRIMARY-NEXT: )
48+
;; PRIMARY-NEXT: (drop
49+
;; PRIMARY-NEXT: (global.get $glob1)
50+
;; PRIMARY-NEXT: )
51+
;; PRIMARY-NEXT: (drop
52+
;; PRIMARY-NEXT: (global.get $glob2)
53+
;; PRIMARY-NEXT: )
4854
;; PRIMARY-NEXT: )
4955
(func $prime
5056
(drop
@@ -53,6 +59,12 @@
5359
(drop
5460
(ref.func $second)
5561
)
62+
(drop
63+
(global.get $glob1)
64+
)
65+
(drop
66+
(global.get $glob2)
67+
)
5668
)
5769

5870
;; SECONDARY: (type $0 (func))

test/lit/wasm-split/transitive-globals.wast

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434

3535
;; SECONDARY: (global $d i32 (global.get $e))
3636

37+
;; This dead global is referring to a global ($a) that's moved to the
38+
;; secondary module. This should be deleted.
39+
;; PRIMARY-NOT: (global (global $dead i32 (global.get $a))
40+
(global $dead i32 (global.get $a))
41+
3742
;; PRIMARY: (func $keep
3843
;; PRIMARY-NEXT: (drop
3944
;; PRIMARY-NEXT: (global.get $e)

0 commit comments

Comments
 (0)