[Mlir-commits] [mlir] df9500c - [mlir] Simplify a few cast implementations

Rahul Kayaith llvmlistbot at llvm.org
Wed Feb 1 18:53:41 PST 2023


Author: Rahul Kayaith
Date: 2023-02-01T21:53:36-05:00
New Revision: df9500c295313f5830fa2f68735cf125a7be4ca3

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

LOG: [mlir] Simplify a few cast implementations

`{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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h
index a39ddf59df0a9..43a2abc9a73ca 100644
--- a/mlir/include/mlir/IR/Attributes.h
+++ b/mlir/include/mlir/IR/Attributes.h
@@ -60,9 +60,6 @@ class Attribute {
   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()); }
 };

diff  --git a/mlir/include/mlir/IR/Types.h b/mlir/include/mlir/IR/Types.h
index cc7ecfb9d4682..2a0586c455ebb 100644
--- a/mlir/include/mlir/IR/Types.h
+++ b/mlir/include/mlir/IR/Types.h
@@ -107,9 +107,6 @@ class Type {
   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()); }
 };

diff  --git a/mlir/include/mlir/IR/Value.h b/mlir/include/mlir/IR/Value.h
index bc51d59c745a2..c84ae97c99b9b 100644
--- a/mlir/include/mlir/IR/Value.h
+++ b/mlir/include/mlir/IR/Value.h
@@ -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()); }
 };


        


More information about the Mlir-commits mailing list