Skip to content

Commit c2a1cc8

Browse files
authored
[wasm-split] Scan trapping globals' initailizer (#8830)
This basically reverts #8790, which had unforseen consequences. We didn't scan trapping globals' initializer because computing transitive globals happened before adding those trapping globals to the primary module.
1 parent 2fe6f04 commit c2a1cc8

2 files changed

Lines changed: 42 additions & 21 deletions

File tree

src/ir/module-splitting.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -762,27 +762,6 @@ ModuleSplitter::PrimarySecondaryUsedNames ModuleSplitter::computeUsedNames() {
762762
}
763763
}
764764
}
765-
766-
// Compute the transitive closure of globals referenced in other globals'
767-
// initializers. Since globals can reference other globals, we must ensure
768-
// that if a global is used in a module, all its dependencies are also
769-
// marked as used.
770-
UniqueNonrepeatingDeferredQueue<Name> worklist;
771-
for (auto global : used.globals) {
772-
worklist.push(global);
773-
}
774-
while (!worklist.empty()) {
775-
Name name = worklist.pop();
776-
// At this point all globals are still in the primary module, so this
777-
// exists
778-
auto* global = primary.getGlobal(name);
779-
if (!global->imported() && global->init) {
780-
for (auto* get : FindAll<GlobalGet>(global->init).list) {
781-
worklist.push(get->name);
782-
used.globals.insert(get->name);
783-
}
784-
}
785-
}
786765
return used;
787766
};
788767

@@ -842,6 +821,34 @@ ModuleSplitter::PrimarySecondaryUsedNames ModuleSplitter::computeUsedNames() {
842821
}
843822
}
844823

824+
// Compute the transitive closure of globals referenced in other globals'
825+
// initializers. Since globals can reference other globals, we must ensure
826+
// that if a global is used in a module, all its dependencies are also marked
827+
// as used.
828+
auto computeTransitiveGlobals = [&](UsedNames& used) {
829+
UniqueNonrepeatingDeferredQueue<Name> worklist;
830+
for (auto global : used.globals) {
831+
worklist.push(global);
832+
}
833+
while (!worklist.empty()) {
834+
Name name = worklist.pop();
835+
// At this point all globals are still in the primary module, so this
836+
// exists
837+
auto* global = primary.getGlobal(name);
838+
if (!global->imported() && global->init) {
839+
for (auto* get : FindAll<GlobalGet>(global->init).list) {
840+
worklist.push(get->name);
841+
used.globals.insert(get->name);
842+
}
843+
}
844+
}
845+
};
846+
847+
computeTransitiveGlobals(primaryUsed);
848+
for (auto& used : secondaryUsed) {
849+
computeTransitiveGlobals(used);
850+
}
851+
845852
return std::make_pair(primaryUsed, secondaryUsed);
846853
}
847854

test/lit/wasm-split/trapping-module-items.wast

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
)
2121
)
2222

23+
;; PRIMARY: (global $null-desc nullref
24+
;; PRIMARY-TNH-NOT: (global $null-desc nullref
25+
(global $null-desc (ref null none)
26+
(ref.null none)
27+
)
28+
29+
;; PRIMARY: (global $trapping-global-init-global-get (ref $struct)
30+
;; PRIMARY-TNH-NOT: (global $trapping-global-init-global-get (ref $struct)
31+
(global $trapping-global-init-global-get (ref $struct)
32+
(struct.new_desc $struct
33+
(global.get $null-desc)
34+
)
35+
)
36+
2337
;; PRIMARY: (table $trapping-table 1 1 (ref $struct)
2438
;; PRIMARY-TNH-NOT: (table $trapping-table 1 1 (ref $struct)
2539
(table $trapping-table 1 1 (ref $struct)

0 commit comments

Comments
 (0)