[Mlir-commits] [mlir] 1075c8c - [mlir] support isa/cast/dyn_cast<Operation *>(operation) again

Alex Zinenko llvmlistbot at llvm.org
Tue May 17 02:15:34 PDT 2022


Author: Alex Zinenko
Date: 2022-05-17T11:15:17+02:00
New Revision: 1075c8ca49b467ec5e654a4322fbb36f63e8687f

URL: https://github.com/llvm/llvm-project/commit/1075c8ca49b467ec5e654a4322fbb36f63e8687f
DIFF: https://github.com/llvm/llvm-project/commit/1075c8ca49b467ec5e654a4322fbb36f63e8687f.diff

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

The support for this has been added by 946311b8938114a37db5c9d42fb9f5a1481ccae1
but then ignored by bc22b5c9a2f729460ffdf7627b3534a8d9f3f767.

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 *`.

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

Reviewed by: rriddle

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 86b5a94cb2113..d6a231b1941e7 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -834,6 +834,24 @@ template <typename T>
 struct CastInfo<T, const ::mlir::Operation>
     : public ConstStrippingForwardingCast<T, const ::mlir::Operation,
                                           CastInfo<T, ::mlir::Operation>> {};
+
+/// Cast (const) Operation * to itself. This is helpful to avoid SFINAE in
+/// templated implementations that should work on both base and derived
+/// operation types.
+template <>
+struct CastInfo<::mlir::Operation *, ::mlir::Operation *>
+    : public NullableValueCastFailed<::mlir::Operation *>,
+      public DefaultDoCastIfPossible<
+          ::mlir::Operation *, ::mlir::Operation *,
+          CastInfo<::mlir::Operation *, ::mlir::Operation *>> {
+  static bool isPossible(::mlir::Operation *op) { return true; }
+  static ::mlir::Operation *doCast(::mlir::Operation *op) { return op; }
+};
+template <>
+struct CastInfo<const ::mlir::Operation *, const ::mlir::Operation *>
+    : public ConstStrippingForwardingCast<
+          const ::mlir::Operation *, const ::mlir::Operation *,
+          CastInfo<::mlir::Operation *, ::mlir::Operation *>> {};
 } // namespace llvm
 
 #endif // MLIR_IR_OPERATION_H


        


More information about the Mlir-commits mailing list