[MLIR] Add isa<> support for Dialects.
authorTres Popp <tpopp@google.com>
Wed, 29 Apr 2020 13:45:50 +0000 (15:45 +0200)
committerTres Popp <tpopp@google.com>
Thu, 30 Apr 2020 09:11:49 +0000 (11:11 +0200)
Summary:
The purpose of this is to aid in having code behave differently on
Operations based on their Dialect without caring about the specific
Op. Additionally this is consistent with most other types supporting
isa<> and dyn_cast<>.

A Dialect matches isa<> based only on its namespace and relies on each
namespace being unique.

Differential Revision: https://reviews.llvm.org/D79088

mlir/include/mlir/IR/Dialect.h

index 292c14d..73d55c2 100644 (file)
@@ -281,4 +281,14 @@ template <typename ConcreteDialect> struct DialectRegistration {
 
 } // namespace mlir
 
+namespace llvm {
+/// Provide isa functionality for Dialects.
+template <typename T>
+struct isa_impl<T, ::mlir::Dialect> {
+  static inline bool doit(const ::mlir::Dialect &dialect) {
+    return T::getDialectNamespace() == dialect.getNamespace();
+  }
+};
+} // namespace llvm
+
 #endif