15 #ifndef MLIR_IR_MATCHERS_H
16 #define MLIR_IR_MATCHERS_H
31 typename ValueType =
typename std::enable_if_t<
32 std::is_base_of<Attribute, AttrClass>::value, AttrClass>::ValueType,
34 typename = std::enable_if_t<!std::is_void<ValueType>::value>>
42 if (
auto intAttr = attr.
dyn_cast<AttrClass>()) {
57 template <
typename AttrT>
75 assert(
succeeded(result) &&
"expected ConstantLike op to be foldable");
102 if (type.isa<VectorType, RankedTensorType>()) {
118 APFloat value(APFloat::Bogus());
137 if (type.isa<IntegerType, IndexType>())
139 if (type.isa<VectorType, RankedTensorType>()) {
161 template <
typename OpClass>
168 template <
typename T,
typename OperationOrValue>
170 decltype(std::declval<T>().match(std::declval<OperationOrValue>()));
173 template <
typename MatcherClass>
175 MatcherClass,
Value>::value,
182 template <
typename MatcherClass>
188 return matcher.match(defOp);
214 template <
typename TupleT,
class CallbackT, std::size_t... Is>
216 std::index_sequence<Is...>) {
218 (callback(std::integral_constant<std::size_t, Is>{}, std::get<Is>(tuple)),
222 template <
typename... Tys,
typename CallbackT>
223 constexpr
void enumerate(std::tuple<Tys...> &tuple, CallbackT &&callback) {
225 std::make_index_sequence<
sizeof...(Tys)>{});
229 template <
typename OpType,
typename... OperandMatchers>
234 if (!isa<OpType>(op) || op->
getNumOperands() !=
sizeof...(OperandMatchers))
254 template <
typename AttrT>
262 return {[](
const APFloat &value) {
return value.isZero(); }};
267 return {[](
const APFloat &value) {
return value.isPosZero(); }};
272 return {[](
const APFloat &value) {
return value.isNegZero(); }};
277 return {[](
const APFloat &value) {
278 return APFloat(value.getSemantics(), 1) == value;
285 return {[](
const APFloat &value) {
286 return !value.isNegative() && value.isInfinity();
293 return {[](
const APFloat &value) {
294 return value.isNegative() && value.isInfinity();
300 return {[](
const APInt &value) {
return 0 == value; }};
306 return {[](
const APInt &value) {
return 0 != value; }};
311 return {[](
const APInt &value) {
return 1 == value; }};
315 template <
typename OpClass>
321 template <
typename Pattern>
325 return const_cast<Pattern &
>(pattern).match(op);
330 template <
typename Pattern>
332 return const_cast<Pattern &
>(pattern).match(op);
337 inline detail::constant_float_op_binder
344 inline detail::constant_int_op_binder
349 template <
typename OpType,
typename... Matchers>
350 auto m_Op(Matchers... matchers) {
Attributes are known-constant values of operations.
This class provides the API for a sub-set of ops that are known to be constant-like.
Operation is the basic unit of execution within MLIR.
LogicalResult fold(ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results)
Attempt to fold this operation with the specified constant operand values.
Value getOperand(unsigned idx)
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
unsigned getNumOperands()
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
An attribute that represents a reference to a splat vector or tensor constant, meaning all of the ele...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
decltype(std::declval< T >().match(std::declval< OperationOrValue >())) has_operation_or_value_matcher_t
Trait to check whether T provides a 'match' method with type OperationOrValue.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
constexpr void enumerateImpl(TupleT &&tuple, CallbackT &&callback, std::index_sequence< Is... >)
std::enable_if_t< llvm::is_detected< detail::has_operation_or_value_matcher_t, MatcherClass, Value >::value, bool > matchOperandOrValueAtIndex(Operation *op, unsigned idx, MatcherClass &matcher)
Statically switch to a Value matcher.
This header declares functions that assit transformations in the MemRef dialect.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_float_op_binder m_ConstantFloat(FloatAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor float (splat) and writes the float value to bind_va...
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
detail::constant_float_predicate_matcher m_PosZeroFloat()
Matches a constant scalar / vector splat / tensor splat float positive zero.
detail::constant_int_predicate_matcher m_Zero()
Matches a constant scalar / vector splat / tensor splat integer zero.
detail::constant_float_predicate_matcher m_AnyZeroFloat()
Matches a constant scalar / vector splat / tensor splat float (both positive and negative) zero.
detail::constant_int_predicate_matcher m_One()
Matches a constant scalar / vector splat / tensor splat integer one.
detail::constant_int_op_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
detail::constant_int_predicate_matcher m_NonZero()
Matches a constant scalar / vector splat / tensor splat integer that is any non-zero value.
detail::constant_float_predicate_matcher m_NegInfFloat()
Matches a constant scalar / vector splat / tensor splat float negative infinity.
detail::constant_float_predicate_matcher m_NegZeroFloat()
Matches a constant scalar / vector splat / tensor splat float negative zero.
detail::op_matcher< OpClass > m_Op()
Matches the given OpClass.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
detail::constant_float_predicate_matcher m_PosInfFloat()
Matches a constant scalar / vector splat / tensor splat float positive infinity.
detail::constant_float_predicate_matcher m_OneFloat()
Matches a constant scalar / vector splat / tensor splat float ones.
This class represents an efficient way to signal success or failure.
Terminal matcher, always returns true.
AnyCapturedValueMatcher(Value *what)
bool match(Value op) const
Terminal matcher, always returns true.
bool match(Value op) const
Binds to a specific value and matches it.
bool match(Value val) const
PatternMatcherValue(Value val)
RecursivePatternMatcher that composes.
RecursivePatternMatcher(OperandMatchers... matchers)
std::tuple< OperandMatchers... > operandMatchers
bool match(Operation *op)
The matcher that matches a certain kind of Attribute and binds the value inside the Attribute.
attr_value_binder(ValueType *bv)
Creates a matcher instance that binds the value to bv if match succeeds.
bool match(const Attribute &attr)
The matcher that matches a constant scalar / vector splat / tensor splat float operation and binds th...
constant_float_op_binder(FloatAttr::ValueType *bv)
Creates a matcher instance that binds the value to bv if match succeeds.
FloatAttr::ValueType * bind_value
bool match(Operation *op)
The matcher that matches a given target constant scalar / vector splat / tensor splat float value tha...
bool match(Operation *op)
bool(* predicate)(const APFloat &)
The matcher that matches a constant scalar / vector splat / tensor splat integer operation and binds ...
bool match(Operation *op)
constant_int_op_binder(IntegerAttr::ValueType *bv)
Creates a matcher instance that binds the value to bv if match succeeds.
IntegerAttr::ValueType * bind_value
The matcher that matches a given target constant scalar / vector splat / tensor splat integer value t...
bool match(Operation *op)
bool(* predicate)(const APInt &)
The matcher that matches operations that have the ConstantLike trait, and binds the folded attribute ...
constant_op_binder()
Creates a matcher instance that doesn't bind if match succeeds.
constant_op_binder(AttrT *bind_value)
Creates a matcher instance that binds the constant attribute value to bind_value if match succeeds.
bool match(Operation *op)
The matcher that matches operations that have the ConstantLike trait.
bool match(Operation *op)
The matcher that matches a certain kind of op.
bool match(Operation *op)