[mlir] Simplify a few cast implementations
authorRahul Kayaith <rkayaith@gmail.com>
Mon, 30 Jan 2023 04:34:10 +0000 (23:34 -0500)
committerRahul Kayaith <rkayaith@gmail.com>
Thu, 2 Feb 2023 02:53:36 +0000 (21:53 -0500)
`{Attribute,Type}::classof` are never actually called at runtime due to
the early exit in their `CastInfo` implementations. By using `if
constexpr` we can avoid needing to define them.

We also don't need to check `is_same_v` here, since this is already
covered by `is_base_of_v`.

Reviewed By: rriddle

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

mlir/include/mlir/IR/Attributes.h
mlir/include/mlir/IR/Types.h
mlir/include/mlir/IR/Value.h

index a39ddf5..43a2abc 100644 (file)
@@ -60,9 +60,6 @@ public:
   template <typename U>
   U cast() const;
 
-  // Support dyn_cast'ing Attribute to itself.
-  static bool classof(Attribute) { return true; }
-
   /// Return a unique identifier for the concrete attribute type. This is used
   /// to support dynamic type casting.
   TypeID getTypeID() { return impl->getAbstractAttribute().getTypeID(); }
@@ -394,8 +391,11 @@ struct CastInfo<To, From,
   static inline bool isPossible(mlir::Attribute ty) {
     /// Return a constant true instead of a dynamic true when casting to self or
     /// up the hierarchy.
-    return std::is_same_v<To, std::remove_const_t<From>> ||
-           std::is_base_of_v<To, From> || To::classof(ty);
+    if constexpr (std::is_base_of_v<To, From>) {
+      return true;
+    } else {
+      return To::classof(ty);
+    }
   }
   static inline To doCast(mlir::Attribute attr) { return To(attr.getImpl()); }
 };
index cc7ecfb..2a0586c 100644 (file)
@@ -107,9 +107,6 @@ public:
   template <typename U>
   U cast() const;
 
-  // Support type casting Type to itself.
-  static bool classof(Type) { return true; }
-
   /// Return a unique identifier for the concrete type. This is used to support
   /// dynamic type casting.
   TypeID getTypeID() { return impl->getAbstractType().getTypeID(); }
@@ -387,8 +384,11 @@ struct CastInfo<
   static inline bool isPossible(mlir::Type ty) {
     /// Return a constant true instead of a dynamic true when casting to self or
     /// up the hierarchy.
-    return std::is_same_v<To, std::remove_const_t<From>> ||
-           std::is_base_of_v<To, From> || To::classof(ty);
+    if constexpr (std::is_base_of_v<To, From>) {
+      return true;
+    } else {
+      return To::classof(ty);
+    };
   }
   static inline To doCast(mlir::Type ty) { return To(ty.getImpl()); }
 };
index bc51d59..c84ae97 100644 (file)
@@ -583,8 +583,11 @@ struct CastInfo<
   static inline bool isPossible(mlir::Value ty) {
     /// Return a constant true instead of a dynamic true when casting to self or
     /// up the hierarchy.
-    return std::is_same_v<To, std::remove_const_t<From>> ||
-           std::is_base_of_v<To, From> || To::classof(ty);
+    if constexpr (std::is_base_of_v<To, From>) {
+      return true;
+    } else {
+      return To::classof(ty);
+    }
   }
   static inline To doCast(mlir::Value value) { return To(value.getImpl()); }
 };