diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp --- a/mlir/lib/IR/MLIRContext.cpp +++ b/mlir/lib/IR/MLIRContext.cpp @@ -409,9 +409,9 @@ function_ref()> ctor) { auto &impl = getImpl(); // Get the correct insertion position sorted by namespace. - std::unique_ptr &dialect = impl.loadedDialects[dialectNamespace]; + auto dialectIt = impl.loadedDialects.find(dialectNamespace); - if (!dialect) { + if (dialectIt == impl.loadedDialects.end()) { LLVM_DEBUG(llvm::dbgs() << "Load new dialect in Context " << dialectNamespace << "\n"); #ifndef NDEBUG @@ -422,7 +422,8 @@ "the PassManager): this can indicate a " "missing `dependentDialects` in a pass for example."); #endif - dialect = ctor(); + std::unique_ptr &dialect = + impl.loadedDialects.insert({dialectNamespace, ctor()}).first->second; assert(dialect && "dialect ctor failed"); // Refresh all the identifiers dialect field, this catches cases where a @@ -441,6 +442,7 @@ } // Abort if dialect with namespace has already been registered. + std::unique_ptr &dialect = dialectIt->second; if (dialect->getTypeID() != dialectID) llvm::report_fatal_error("a dialect with namespace '" + dialectNamespace + "' has already been registered");