From: River Riddle Date: Wed, 21 Aug 2019 02:58:35 +0000 (-0700) Subject: NFC: Keep the dialect list in the context sorted by namespace. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ad8b410f161d9bdf830771683a010cd97553676d;p=platform%2Fupstream%2Fllvm.git NFC: Keep the dialect list in the context sorted by namespace. Most dialects are initialized statically, which does not have a guaranteed initialization order. By keeping the dialect list sorted, we can guarantee a deterministic iteration order of dialects. PiperOrigin-RevId: 264522875 --- diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp index 0551be5..ab27ab1 100644 --- a/mlir/lib/IR/MLIRContext.cpp +++ b/mlir/lib/IR/MLIRContext.cpp @@ -333,18 +333,26 @@ Dialect *MLIRContext::getRegisteredDialect(StringRef name) { /// takes ownership of the heap allocated dialect. void Dialect::registerDialect(MLIRContext *context) { auto &impl = context->getImpl(); + std::unique_ptr dialect(this); // Lock access to the context registry. llvm::sys::SmartScopedWriter registryLock(impl.contextMutex); + + // Get the correct insertion position sorted by namespace. + auto insertPt = + llvm::lower_bound(impl.dialects, dialect, + [](const std::unique_ptr &lhs, + const std::unique_ptr &rhs) { + return lhs->getNamespace() < rhs->getNamespace(); + }); + // Abort if dialect with namespace has already been registered. - if (llvm::any_of(impl.dialects, [this](std::unique_ptr &dialect) { - return dialect->getNamespace() == getNamespace(); - })) { - llvm::report_fatal_error("a dialect with namespace '" + - Twine(getNamespace()) + + if (insertPt != impl.dialects.end() && + (*insertPt)->getNamespace() == getNamespace()) { + llvm::report_fatal_error("a dialect with namespace '" + getNamespace() + "' has already been registered"); } - impl.dialects.push_back(std::unique_ptr(this)); + impl.dialects.insert(insertPt, std::move(dialect)); } /// Return information about all registered operations. This isn't very