Skip to content
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
15 changes: 13 additions & 2 deletions src/ir/module-splitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,8 +1182,18 @@ void ModuleSplitter::shareImportableItems() {

auto usingSecondaries =
getUsingSecondaries(global->name, &UsedNames::globals);
bool usedInPrimary = primaryUsed.globals.count(global->name);
if (!usedInPrimary && usingSecondaries.size() == 1) {
bool inPrimary = primaryUsed.globals.count(global->name);

if (!inPrimary && usingSecondaries.empty()) {
// It's not used anywhere, so delete it. Unlike other unused module items
// (memories, tables, and tags) that can just sit in the primary module
// and later be DCE'ed by another pass, we should remove it here, because
// an unused global can contain an initialier that refers to another
// global that will be moved to a secondary module, like
// (global $unused i32 (global.get $a)) // $a is moved to a secondary
globalsToRemove.push_back(global->name);

} else if (!inPrimary && usingSecondaries.size() == 1) {
// We are moving this global to this secondary module
auto* secondary = usingSecondaries[0];
auto* secondaryGlobal = ModuleUtils::copyGlobal(global.get(), *secondary);
Expand Down Expand Up @@ -1213,6 +1223,7 @@ void ModuleSplitter::shareImportableItems() {
// function.
}
}

} else { // We are NOT moving this global to the secondary module
if (global->init) {
for (auto* ref : FindAll<RefFunc>(global->init).list) {
Expand Down
2 changes: 0 additions & 2 deletions test/example/module-splitting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Keeping: <none>
After:
(module
(type $0 (func (param i32)))
(global $glob (mut i32) (i32.const 7))
(memory $mem 3 42 shared)
(table $tab 3 42 funcref)
(tag $e (type $0) (param i32))
Expand All @@ -46,7 +45,6 @@ After:
(type $0 (func (param i32)))
(import "env" "mem" (memory $mem 3 42 shared))
(import "env" "tab" (table $tab 3 42 funcref))
(import "env" "glob" (global $glob (mut i32)))
(import "env" "e" (tag $e (type $0) (param i32)))
)
Secondary:
Expand Down
12 changes: 12 additions & 0 deletions test/lit/wasm-split/ref.func.wast
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
;; PRIMARY-NEXT: (drop
;; PRIMARY-NEXT: (ref.func $trampoline_second)
;; PRIMARY-NEXT: )
;; PRIMARY-NEXT: (drop
;; PRIMARY-NEXT: (global.get $glob1)
;; PRIMARY-NEXT: )
;; PRIMARY-NEXT: (drop
;; PRIMARY-NEXT: (global.get $glob2)
;; PRIMARY-NEXT: )
;; PRIMARY-NEXT: )
(func $prime
(drop
Expand All @@ -53,6 +59,12 @@
(drop
(ref.func $second)
)
(drop
(global.get $glob1)
)
(drop
(global.get $glob2)
)
Comment on lines +62 to +67
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR, glob1 and glob2 are dead and the test doesn't do the intended testing anymore, so we use them here in the primary function

)

;; SECONDARY: (type $0 (func))
Expand Down
5 changes: 5 additions & 0 deletions test/lit/wasm-split/transitive-globals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@

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

;; This dead global is referring to a global ($a) that's moved to the
;; secondary module. This should be deleted.
;; PRIMARY-NOT: (global (global $dead i32 (global.get $a))
(global $dead i32 (global.get $a))

;; PRIMARY: (func $keep
;; PRIMARY-NEXT: (drop
;; PRIMARY-NEXT: (global.get $e)
Expand Down
Loading