/// emitting diagnostics.
void printStackTraceOnDiagnostic(bool enable);
- /// Return information about all registered operations. This isn't very
- /// efficient: typically you should ask the operations about their properties
- /// directly.
- std::vector<RegisteredOperationName> getRegisteredOperations();
+ /// Return a sorted array containing the information about all registered
+ /// operations.
+ ArrayRef<RegisteredOperationName> getRegisteredOperations();
/// Return true if this operation name is registered in this context.
bool isOperationRegistered(StringRef name);
/// A vector of operation info specifically for registered operations.
llvm::StringMap<RegisteredOperationName> registeredOperations;
+ /// This is a sorted container of registered operations for a deterministic
+ /// and efficient `getRegisteredOperations` implementation.
+ SmallVector<RegisteredOperationName, 0> sortedRegisteredOperations;
+
/// A mutex used when accessing operation information.
llvm::sys::SmartRWMutex<true> operationInfoMutex;
impl->printStackTraceOnDiagnostic = enable;
}
-/// Return information about all registered operations. This isn't very
-/// efficient, typically you should ask the operations about their properties
-/// directly.
-std::vector<RegisteredOperationName> MLIRContext::getRegisteredOperations() {
- // We just have the operations in a non-deterministic hash table order. Dump
- // into a temporary array, then sort it by operation name to get a stable
- // ordering.
- auto unwrappedNames = llvm::make_second_range(impl->registeredOperations);
- std::vector<RegisteredOperationName> result(unwrappedNames.begin(),
- unwrappedNames.end());
- llvm::array_pod_sort(result.begin(), result.end(),
- [](const RegisteredOperationName *lhs,
- const RegisteredOperationName *rhs) {
- return lhs->getIdentifier().compare(
- rhs->getIdentifier());
- });
-
- return result;
+/// Return information about all registered operations.
+ArrayRef<RegisteredOperationName> MLIRContext::getRegisteredOperations() {
+ return impl->sortedRegisteredOperations;
}
bool MLIRContext::isOperationRegistered(StringRef name) {
<< "' is already registered.\n";
abort();
}
- ctxImpl.registeredOperations.try_emplace(name,
- RegisteredOperationName(&impl));
+ auto emplaced = ctxImpl.registeredOperations.try_emplace(
+ name, RegisteredOperationName(&impl));
+ assert(emplaced.second && "operation name registration must be successful");
+
+ // Add emplaced operation name to the sorted operations container.
+ RegisteredOperationName &value = emplaced.first->getValue();
+ ctxImpl.sortedRegisteredOperations.insert(
+ llvm::upper_bound(ctxImpl.sortedRegisteredOperations, value,
+ [](auto &lhs, auto &rhs) {
+ return lhs.getIdentifier().compare(
+ rhs.getIdentifier());
+ }),
+ value);
// Update the registered info for this operation.
impl.dialect = &dialect;