Skip to content

Conversation

@chsigg
Copy link
Contributor

@chsigg chsigg commented Apr 29, 2024

Also remove the member overload in TypeSwitch.

Also remove the member overload in TypeSwitch.
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir llvm:adt labels Apr 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2024

@llvm/pr-subscribers-llvm-adt
@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Christian Sigg (chsigg)

Changes

Also remove the member overload in TypeSwitch.


Full diff: https://github.com/llvm/llvm-project/pull/90453.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/TypeSwitch.h (+2-22)
  • (modified) mlir/include/mlir/Tools/PDLL/AST/Types.h (+23)
diff --git a/llvm/include/llvm/ADT/TypeSwitch.h b/llvm/include/llvm/ADT/TypeSwitch.h index 10a2d48e918db9..5bbbdf23b257ed 100644 --- a/llvm/include/llvm/ADT/TypeSwitch.h +++ b/llvm/include/llvm/ADT/TypeSwitch.h @@ -61,29 +61,9 @@ template <typename DerivedT, typename T> class TypeSwitchBase { } protected: - /// Trait to check whether `ValueT` provides a 'dyn_cast' method with type - /// `CastT`. - template <typename ValueT, typename CastT> - using has_dyn_cast_t = - decltype(std::declval<ValueT &>().template dyn_cast<CastT>()); - - /// Attempt to dyn_cast the given `value` to `CastT`. This overload is - /// selected if `value` already has a suitable dyn_cast method. + /// Attempt to dyn_cast the given `value` to `CastT`. template <typename CastT, typename ValueT> - static decltype(auto) castValue( - ValueT &&value, - std::enable_if_t<is_detected<has_dyn_cast_t, ValueT, CastT>::value> * = - nullptr) { - return value.template dyn_cast<CastT>(); - } - - /// Attempt to dyn_cast the given `value` to `CastT`. This overload is - /// selected if llvm::dyn_cast should be used. - template <typename CastT, typename ValueT> - static decltype(auto) castValue( - ValueT &&value, - std::enable_if_t<!is_detected<has_dyn_cast_t, ValueT, CastT>::value> * = - nullptr) { + static decltype(auto) castValue(ValueT &&value) { return dyn_cast<CastT>(value); } diff --git a/mlir/include/mlir/Tools/PDLL/AST/Types.h b/mlir/include/mlir/Tools/PDLL/AST/Types.h index 03252e9f6620c8..1aff7a1d0a8406 100644 --- a/mlir/include/mlir/Tools/PDLL/AST/Types.h +++ b/mlir/include/mlir/Tools/PDLL/AST/Types.h @@ -323,6 +323,29 @@ struct DenseMapInfo<mlir::pdll::ast::Type> { return lhs == rhs; } }; + +/// Add support for llvm style casts. +/// We provide a cast between To and From if From is mlir::pdll::ast::Type or +/// derives from it +template <typename To, typename From> +struct CastInfo< + To, From, + std::enable_if_t< + std::is_same_v<mlir::pdll::ast::Type, std::remove_const_t<From>> || + std::is_base_of_v<mlir::pdll::ast::Type, From>>> + : NullableValueCastFailed<To>, + DefaultDoCastIfPossible<To, From, CastInfo<To, From>> { + static inline bool isPossible(mlir::pdll::ast::Type ty) { + /// Return a constant true instead of a dynamic true when casting to self or + /// up the hierarchy. + if constexpr (std::is_base_of_v<To, From>) { + return true; + } else { + return To::classof(ty); + }; + } + static inline To doCast(mlir::pdll::ast::Type ty) { return To(ty.getImpl()); } +}; } // namespace llvm #endif // MLIR_TOOLS_PDLL_AST_TYPES_H_ 
@chsigg chsigg requested a review from joker-eph April 29, 2024 14:27
@chsigg
Copy link
Contributor Author

chsigg commented Apr 30, 2024

This has landed in PR #90413. Closing.

@chsigg chsigg closed this Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:adt mlir:core MLIR Core Infrastructure mlir

2 participants