forked from OSchip/llvm-project
[MLIR][Operation] Simplify Operation casting, NFC
We can simplify the code needed to implement dyn_cast/cast/isa support for MLIR operations with documented interfaces via the CastInfo structures. This will also provide an example of how to use CastInfo. Depends on D123901 Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D124963
This commit is contained in:
parent
f156b51aec
commit
bc22b5c9a2
|
@ -172,7 +172,8 @@ public:
|
||||||
Operation *getParentOp() { return block ? block->getParentOp() : nullptr; }
|
Operation *getParentOp() { return block ? block->getParentOp() : nullptr; }
|
||||||
|
|
||||||
/// Return the closest surrounding parent operation that is of type 'OpTy'.
|
/// Return the closest surrounding parent operation that is of type 'OpTy'.
|
||||||
template <typename OpTy> OpTy getParentOfType() {
|
template <typename OpTy>
|
||||||
|
OpTy getParentOfType() {
|
||||||
auto *op = this;
|
auto *op = this;
|
||||||
while ((op = op->getParentOp()))
|
while ((op = op->getParentOp()))
|
||||||
if (auto parentOp = dyn_cast<OpTy>(op))
|
if (auto parentOp = dyn_cast<OpTy>(op))
|
||||||
|
@ -521,14 +522,16 @@ public:
|
||||||
|
|
||||||
/// Returns true if the operation was registered with a particular trait, e.g.
|
/// Returns true if the operation was registered with a particular trait, e.g.
|
||||||
/// hasTrait<OperandsAreSignlessIntegerLike>().
|
/// hasTrait<OperandsAreSignlessIntegerLike>().
|
||||||
template <template <typename T> class Trait> bool hasTrait() {
|
template <template <typename T> class Trait>
|
||||||
|
bool hasTrait() {
|
||||||
return name.hasTrait<Trait>();
|
return name.hasTrait<Trait>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the operation *might* have the provided trait. This
|
/// Returns true if the operation *might* have the provided trait. This
|
||||||
/// means that either the operation is unregistered, or it was registered with
|
/// means that either the operation is unregistered, or it was registered with
|
||||||
/// the provide trait.
|
/// the provide trait.
|
||||||
template <template <typename T> class Trait> bool mightHaveTrait() {
|
template <template <typename T> class Trait>
|
||||||
|
bool mightHaveTrait() {
|
||||||
return name.mightHaveTrait<Trait>();
|
return name.mightHaveTrait<Trait>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -804,34 +807,33 @@ inline raw_ostream &operator<<(raw_ostream &os, const Operation &op) {
|
||||||
} // namespace mlir
|
} // namespace mlir
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
/// Provide isa functionality for operation casts.
|
/// Cast from an (const) Operation * to a derived operation type.
|
||||||
template <typename T> struct isa_impl<T, ::mlir::Operation> {
|
template <typename T>
|
||||||
static inline bool doit(const ::mlir::Operation &op) {
|
struct CastInfo<T, ::mlir::Operation *>
|
||||||
return T::classof(const_cast<::mlir::Operation *>(&op));
|
: public ValueFromPointerCast<T, ::mlir::Operation,
|
||||||
}
|
CastInfo<T, ::mlir::Operation *>> {
|
||||||
|
static bool isPossible(::mlir::Operation *op) { return T::classof(op); }
|
||||||
};
|
};
|
||||||
|
template <typename T>
|
||||||
|
struct CastInfo<T, const ::mlir::Operation *>
|
||||||
|
: public ConstStrippingForwardingCast<T, const ::mlir::Operation *,
|
||||||
|
CastInfo<T, ::mlir::Operation *>> {};
|
||||||
|
|
||||||
/// Allow isa<Operation *> on operations.
|
/// Cast from an (const) Operation & to a derived operation type.
|
||||||
template <> struct isa_impl<::mlir::Operation *, ::mlir::Operation> {
|
template <typename T>
|
||||||
static inline bool doit(const ::mlir::Operation &op) { return true; }
|
struct CastInfo<T, ::mlir::Operation>
|
||||||
};
|
: public NullableValueCastFailed<T>,
|
||||||
|
public DefaultDoCastIfPossible<T, ::mlir::Operation &,
|
||||||
/// Provide specializations for operation casts as the resulting T is value
|
CastInfo<T, ::mlir::Operation>> {
|
||||||
/// typed.
|
// Provide isPossible here because here we have the const-stripping from
|
||||||
template <typename T> struct cast_retty_impl<T, ::mlir::Operation *> {
|
// ConstStrippingCast.
|
||||||
using ret_type = T;
|
static bool isPossible(::mlir::Operation &val) { return T::classof(&val); }
|
||||||
};
|
static T doCast(::mlir::Operation &val) { return T(&val); }
|
||||||
template <typename T> struct cast_retty_impl<T, ::mlir::Operation> {
|
|
||||||
using ret_type = T;
|
|
||||||
};
|
|
||||||
template <class T>
|
|
||||||
struct cast_convert_val<T, ::mlir::Operation, ::mlir::Operation> {
|
|
||||||
static T doit(::mlir::Operation &val) { return T(&val); }
|
|
||||||
};
|
|
||||||
template <class T>
|
|
||||||
struct cast_convert_val<T, ::mlir::Operation *, ::mlir::Operation *> {
|
|
||||||
static T doit(::mlir::Operation *val) { return T(val); }
|
|
||||||
};
|
};
|
||||||
|
template <typename T>
|
||||||
|
struct CastInfo<T, const ::mlir::Operation>
|
||||||
|
: public ConstStrippingForwardingCast<T, const ::mlir::Operation,
|
||||||
|
CastInfo<T, ::mlir::Operation>> {};
|
||||||
} // namespace llvm
|
} // namespace llvm
|
||||||
|
|
||||||
#endif // MLIR_IR_OPERATION_H
|
#endif // MLIR_IR_OPERATION_H
|
||||||
|
|
Loading…
Reference in New Issue