diff --git a/src/ir/module-splitting.cpp b/src/ir/module-splitting.cpp index 3e9d5d1db90..422b18b94ea 100644 --- a/src/ir/module-splitting.cpp +++ b/src/ir/module-splitting.cpp @@ -762,27 +762,6 @@ ModuleSplitter::PrimarySecondaryUsedNames ModuleSplitter::computeUsedNames() { } } } - - // Compute the transitive closure of globals referenced in other globals' - // initializers. Since globals can reference other globals, we must ensure - // that if a global is used in a module, all its dependencies are also - // marked as used. - UniqueNonrepeatingDeferredQueue worklist; - for (auto global : used.globals) { - worklist.push(global); - } - while (!worklist.empty()) { - Name name = worklist.pop(); - // At this point all globals are still in the primary module, so this - // exists - auto* global = primary.getGlobal(name); - if (!global->imported() && global->init) { - for (auto* get : FindAll(global->init).list) { - worklist.push(get->name); - used.globals.insert(get->name); - } - } - } return used; }; @@ -842,6 +821,34 @@ ModuleSplitter::PrimarySecondaryUsedNames ModuleSplitter::computeUsedNames() { } } + // Compute the transitive closure of globals referenced in other globals' + // initializers. Since globals can reference other globals, we must ensure + // that if a global is used in a module, all its dependencies are also marked + // as used. + auto computeTransitiveGlobals = [&](UsedNames& used) { + UniqueNonrepeatingDeferredQueue worklist; + for (auto global : used.globals) { + worklist.push(global); + } + while (!worklist.empty()) { + Name name = worklist.pop(); + // At this point all globals are still in the primary module, so this + // exists + auto* global = primary.getGlobal(name); + if (!global->imported() && global->init) { + for (auto* get : FindAll(global->init).list) { + worklist.push(get->name); + used.globals.insert(get->name); + } + } + } + }; + + computeTransitiveGlobals(primaryUsed); + for (auto& used : secondaryUsed) { + computeTransitiveGlobals(used); + } + return std::make_pair(primaryUsed, secondaryUsed); } diff --git a/test/lit/wasm-split/trapping-module-items.wast b/test/lit/wasm-split/trapping-module-items.wast index c95c36c5307..8ce1df1ad6f 100644 --- a/test/lit/wasm-split/trapping-module-items.wast +++ b/test/lit/wasm-split/trapping-module-items.wast @@ -20,6 +20,20 @@ ) ) + ;; PRIMARY: (global $null-desc nullref + ;; PRIMARY-TNH-NOT: (global $null-desc nullref + (global $null-desc (ref null none) + (ref.null none) + ) + + ;; PRIMARY: (global $trapping-global-init-global-get (ref $struct) + ;; PRIMARY-TNH-NOT: (global $trapping-global-init-global-get (ref $struct) + (global $trapping-global-init-global-get (ref $struct) + (struct.new_desc $struct + (global.get $null-desc) + ) + ) + ;; PRIMARY: (table $trapping-table 1 1 (ref $struct) ;; PRIMARY-TNH-NOT: (table $trapping-table 1 1 (ref $struct) (table $trapping-table 1 1 (ref $struct)