diff --git a/enzyme/Enzyme/TypeAnalysis/TypeTree.h b/enzyme/Enzyme/TypeAnalysis/TypeTree.h index 0a3611e7e2a3..d0eb235dea6e 100644 --- a/enzyme/Enzyme/TypeAnalysis/TypeTree.h +++ b/enzyme/Enzyme/TypeAnalysis/TypeTree.h @@ -1240,35 +1240,36 @@ class TypeTree : public std::enable_shared_from_this { /// equivalent If this is an illegal operation, `LegalOr` will be set to false bool checkedOrIn(const TypeTree &RHS, bool PointerIntSame, bool &LegalOr) { // Add fast path where nothing could change because all potentially inserted - // value are already in the map - { - bool newval = false; - for (const auto &pair : RHS.mapping) { - auto found = mapping.find(pair.first); - if (found == mapping.end()) { - newval = true; - break; - } - bool SubLegalOr = true; - auto cur = found->second; - bool SubChanged = - cur.checkedOrIn(pair.second, PointerIntSame, SubLegalOr); - if (!SubLegalOr) { - LegalOr = false; - return false; - } - if (!SubChanged) { - newval = true; - break; - } + // value are already in the map. Save all other ones that may need slow + // insertion + std::vector, ConcreteType>> todo; + + for (const auto &pair : RHS.mapping) { + auto found = mapping.find(pair.first); + if (found == mapping.end()) { + todo.emplace_back(pair); + continue; } - if (!newval) + bool SubLegalOr = true; + auto cur = found->second; + bool SubChanged = + cur.checkedOrIn(pair.second, PointerIntSame, SubLegalOr); + if (!SubLegalOr) { + LegalOr = false; return false; + } + if (!SubChanged) { + todo.emplace_back(pair); + continue; + } } + if (todo.size() == 0) + return false; + // TODO detect recursive merge and simplify bool changed = false; - for (auto &pair : RHS.mapping) { + for (auto &pair : todo) { changed |= checkedOrIn(pair.first, pair.second, PointerIntSame, LegalOr); } return changed;