function_ref<std::unique_ptr<Dialect>()> ctor) {
auto &impl = getImpl();
// Get the correct insertion position sorted by namespace.
- auto dialectIt = impl.loadedDialects.find(dialectNamespace);
+ auto dialectIt = impl.loadedDialects.try_emplace(dialectNamespace, nullptr);
- if (dialectIt == impl.loadedDialects.end()) {
+ if (dialectIt.second) {
LLVM_DEBUG(llvm::dbgs()
<< "Load new dialect in Context " << dialectNamespace << "\n");
#ifndef NDEBUG
"missing `dependentDialects` in a pass for example.");
#endif // NDEBUG
// loadedDialects entry is initialized to nullptr, indicating that the
- // dialect is currently being loaded.
- std::unique_ptr<Dialect> &dialect = impl.loadedDialects[dialectNamespace];
- dialect = ctor();
+ // dialect is currently being loaded. Re-lookup the address in
+ // loadedDialects because the table might have been rehashed by recursive
+ // dialect loading in ctor().
+ std::unique_ptr<Dialect> &dialect = impl.loadedDialects[dialectNamespace] =
+ ctor();
assert(dialect && "dialect ctor failed");
// Refresh all the identifiers dialect field, this catches cases where a
}
#ifndef NDEBUG
- if (dialectIt->second == nullptr)
+ if (dialectIt.first->second == nullptr)
llvm::report_fatal_error(
"Loading (and getting) a dialect (" + dialectNamespace +
") while the same dialect is still loading: use loadDialect instead "
#endif // NDEBUG
// Abort if dialect with namespace has already been registered.
- std::unique_ptr<Dialect> &dialect = dialectIt->second;
+ std::unique_ptr<Dialect> &dialect = dialectIt.first->second;
if (dialect->getTypeID() != dialectID)
llvm::report_fatal_error("a dialect with namespace '" + dialectNamespace +
"' has already been registered");