@@ -48,18 +48,18 @@ struct EffectAnalyzer : public PostWalker<EffectAnalyzer> {
4848 bool readsMemory = false ;
4949 bool writesMemory = false ;
5050 bool implicitTrap = false ; // a load or div/rem, which may trap. we ignore trap
51- // differences, so it is ok to reorder these, and we
52- // also allow reordering them with other effects
53- // (so a trap may occur later or earlier, if it is
54- // going to occur anyhow), but we can't remove them,
55- // they count as side effects
51+ // differences, so it is ok to reorder these, but we can't
52+ // remove them, as they count as side effects, and we
53+ // can't move them in a way that would cause other noticeable
54+ // (global) side effects
5655 bool isAtomic = false ; // An atomic load/store/RMW/Cmpxchg or an operator that
5756 // has a defined ordering wrt atomics (e.g. grow_memory)
5857
5958 bool accessesLocal () { return localsRead.size () + localsWritten.size () > 0 ; }
6059 bool accessesGlobal () { return globalsRead.size () + globalsWritten.size () > 0 ; }
6160 bool accessesMemory () { return calls || readsMemory || writesMemory; }
62- bool hasSideEffects () { return calls || localsWritten.size () > 0 || writesMemory || branches || globalsWritten.size () > 0 || implicitTrap || isAtomic; }
61+ bool hasGlobalSideEffects () { return calls || globalsWritten.size () > 0 || writesMemory || isAtomic; }
62+ bool hasSideEffects () { return hasGlobalSideEffects () || localsWritten.size () > 0 || branches || implicitTrap; }
6363 bool hasAnything () { return branches || calls || accessesLocal () || readsMemory || writesMemory || accessesGlobal () || implicitTrap || isAtomic; }
6464
6565 // checks if these effects would invalidate another set (e.g., if we write, we invalidate someone that reads, they can't be moved past us)
@@ -98,6 +98,10 @@ struct EffectAnalyzer : public PostWalker<EffectAnalyzer> {
9898 if ((implicitTrap && other.branches ) || (other.implicitTrap && branches)) {
9999 return true ;
100100 }
101+ // we can't reorder an implicit trap in a way that alters global state
102+ if ((implicitTrap && other.hasGlobalSideEffects ()) || (other.implicitTrap && hasGlobalSideEffects ())) {
103+ return true ;
104+ }
101105 return false ;
102106 }
103107
0 commit comments