From df9500c295313f5830fa2f68735cf125a7be4ca3 Mon Sep 17 00:00:00 2001 From: Rahul Kayaith Date: Sun, 29 Jan 2023 23:34:10 -0500 Subject: [PATCH] [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 --- mlir/include/mlir/IR/Attributes.h | 10 +++++----- mlir/include/mlir/IR/Types.h | 10 +++++----- mlir/include/mlir/IR/Value.h | 7 +++++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h index a39ddf5..43a2abc 100644 --- a/mlir/include/mlir/IR/Attributes.h +++ b/mlir/include/mlir/IR/Attributes.h @@ -60,9 +60,6 @@ public: template 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> || - std::is_base_of_v || To::classof(ty); + if constexpr (std::is_base_of_v) { + 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 cc7ecfb..2a0586c 100644 --- a/mlir/include/mlir/IR/Types.h +++ b/mlir/include/mlir/IR/Types.h @@ -107,9 +107,6 @@ public: template 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> || - std::is_base_of_v || To::classof(ty); + if constexpr (std::is_base_of_v) { + 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 bc51d59..c84ae97 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> || - std::is_base_of_v || To::classof(ty); + if constexpr (std::is_base_of_v) { + return true; + } else { + return To::classof(ty); + } } static inline To doCast(mlir::Value value) { return To(value.getImpl()); } }; -- 2.7.4