@@ -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
0 commit comments