[Mlir-commits] [mlir] 946311b - [mlir] support isa/cast/dyn_cast<Operation *>(operation)

Alex Zinenko llvmlistbot at llvm.org
Mon May 2 01:07:18 PDT 2022


Author: Alex Zinenko
Date: 2022-05-02T10:07:09+02:00
New Revision: 946311b8938114a37db5c9d42fb9f5a1481ccae1

URL: https://github.com/llvm/llvm-project/commit/946311b8938114a37db5c9d42fb9f5a1481ccae1
DIFF: https://github.com/llvm/llvm-project/commit/946311b8938114a37db5c9d42fb9f5a1481ccae1.diff

LOG: [mlir] support isa/cast/dyn_cast<Operation *>(operation)

This enables one to write generic code that can be instantiated for both
specific operation classes and the common base class without
specialization. Examples include functions that take/return ops, such
as:

```mlir
template <typename FnTy>
void applyIf(FnTy &&lambda, ...) {
  for (Operation *op : ...) {
    auto specific = dyn_cast<function_traits<FnTy>::template arg_t<0>>(op);
    if (specific)
      lambda(specific);
  }
}
```

that would otherwise need to rely on template specialization to support
lambdas that take specific operations and those that take `Operation *`.

Reviewed By: rriddle

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/Operation.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index 4692104c119db..4594cb6b219b3 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -811,6 +811,11 @@ template <typename T> struct isa_impl<T, ::mlir::Operation> {
   }
 };
 
+/// Allow isa<Operation *> on operations.
+template <> struct isa_impl<::mlir::Operation *, ::mlir::Operation> {
+  static inline bool doit(const ::mlir::Operation &op) { return true; }
+};
+
 /// Provide specializations for operation casts as the resulting T is value
 /// typed.
 template <typename T> struct cast_retty_impl<T, ::mlir::Operation *> {


        


More information about the Mlir-commits mailing list