@@ -309,6 +309,12 @@ struct GlobalStructInference : public Pass {
309309
310310 bool refinalize = false ;
311311
312+ // As we prepare to un-nest globals, we create global.gets of the global
313+ // that we will un-nest the content to. That global does not yet exist,
314+ // and we note such globals as we go so we ignore them (they are invalid
315+ // IR until the global is created, later in this pass).
316+ std::unordered_set<GlobalGet*> unnestingGlobalGets;
317+
312318 void visitStructGet (StructGet* curr) {
313319 optimize (curr, curr->ref , curr->index );
314320 }
@@ -347,14 +353,18 @@ struct GlobalStructInference : public Pass {
347353 // This is a read of an immutable field. See if it is a trivial case, of
348354 // a read from an immutable global.
349355 if (auto * get = ref->dynCast <GlobalGet>()) {
350- auto * global = wasm.getGlobal (get->name );
351- if (!global->mutable_ && !global->imported ()) {
352- if (auto * structNew = global->init ->dynCast <StructNew>()) {
353- auto value = readFromStructNew (structNew, fieldIndex, field);
354- // We know the exact global being read here.
355- value.globals .push_back (global->name );
356- replaceCurrent (getReadValue (value, fieldIndex, field, curr));
357- return ;
356+ // The global.get must be valid, and not in the process of being
357+ // rewritten to point to a new un-nested global.
358+ if (!unnestingGlobalGets.count (get)) {
359+ auto * global = wasm.getGlobal (get->name );
360+ if (!global->mutable_ && !global->imported ()) {
361+ if (auto * structNew = global->init ->dynCast <StructNew>()) {
362+ auto value = readFromStructNew (structNew, fieldIndex, field);
363+ // We know the exact global being read here.
364+ value.globals .push_back (global->name );
365+ replaceCurrent (getReadValue (value, fieldIndex, field, curr));
366+ return ;
367+ }
358368 }
359369 }
360370 }
@@ -541,6 +551,7 @@ struct GlobalStructInference : public Pass {
541551
542552 globalsToUnnest.emplace_back (
543553 GlobalToUnnest{value.globals [0 ], fieldIndex, get});
554+ unnestingGlobalGets.insert (get);
544555
545556 ret = get;
546557 }
0 commit comments