15 #ifndef MLIR_IR_MATCHERS_H
16 #define MLIR_IR_MATCHERS_H
32 typename ValueType =
typename std::enable_if_t<
33 std::is_base_of<Attribute, AttrClass>::value, AttrClass>::ValueType,
35 typename = std::enable_if_t<!std::is_void<ValueType>::value>>
43 if (
auto intAttr = llvm::dyn_cast<AttrClass>(attr)) {
74 template <
typename AttrT>
92 assert(
succeeded(result) &&
"expected ConstantLike op to be foldable");
94 if (
auto attr = llvm::dyn_cast<AttrT>(foldedOp.front().get<
Attribute>())) {
105 template <
typename AttrT>
115 if (
auto attr = op->getAttrOfType<AttrT>(
attrName)) {
136 if (matcher.
match(attr))
139 if (
auto splatAttr = dyn_cast<SplatElementsAttr>(attr))
150 Type type = op->getResult(0).getType();
151 if (isa<FloatType, VectorType, RankedTensorType>(type))
164 APFloat value(APFloat::Bogus());
169 APFloat value(APFloat::Bogus());
184 if (matcher.
match(attr))
187 if (
auto splatAttr = dyn_cast<SplatElementsAttr>(attr))
198 Type type = op->getResult(0).getType();
199 if (isa<IntegerType, IndexType, VectorType, RankedTensorType>(type))
223 template <
typename OpClass>
230 template <
typename T,
typename MatchTarget>
232 decltype(std::declval<T>().match(std::declval<MatchTarget>()));
235 template <
typename MatcherClass>
237 MatcherClass,
Value>::value,
240 return matcher.match(op->getOperand(idx));
244 template <
typename MatcherClass>
249 if (
auto *defOp = op->getOperand(idx).getDefiningOp())
250 return matcher.match(defOp);
276 template <
typename TupleT,
class CallbackT, std::size_t... Is>
278 std::index_sequence<Is...>) {
280 (callback(std::integral_constant<std::size_t, Is>{}, std::get<Is>(tuple)),
284 template <
typename... Tys,
typename CallbackT>
285 constexpr
void enumerate(std::tuple<Tys...> &tuple, CallbackT &&callback) {
287 std::make_index_sequence<
sizeof...(Tys)>{});
291 template <
typename OpType,
typename... OperandMatchers>
296 if (!isa<OpType>(op) || op->getNumOperands() !=
sizeof...(OperandMatchers))
326 template <
typename AttrT>
332 template <
typename AttrT>
341 return {[](
const APFloat &value) {
return value.isZero(); }};
346 return {[](
const APFloat &value) {
return value.isPosZero(); }};
351 return {[](
const APFloat &value) {
return value.isNegZero(); }};
356 return {[](
const APFloat &value) {
357 return APFloat(value.getSemantics(), 1) == value;
364 return {[](
const APFloat &value) {
365 return !value.isNegative() && value.isInfinity();
372 return {[](
const APFloat &value) {
373 return value.isNegative() && value.isInfinity();
379 return {[](
const APInt &value) {
return 0 == value; }};
385 return {[](
const APInt &value) {
return 0 != value; }};
390 return {[](
const APInt &value) {
return 1 == value; }};
394 template <
typename OpClass>
400 template <
typename Pattern>
405 return const_cast<Pattern &
>(pattern).match(op);
410 template <
typename Pattern>
413 return const_cast<Pattern &
>(pattern).match(op);
418 template <
typename Pattern>
422 "Pattern does not support matching Attributes");
425 return const_cast<Pattern &
>(pattern).match(attr);
430 inline detail::constant_float_value_binder
437 inline detail::constant_int_value_binder
442 template <
typename OpType,
typename... Matchers>
443 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.
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
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_compatible_matcher_t, MatcherClass, Value >::value, bool > matchOperandOrValueAtIndex(Operation *op, unsigned idx, MatcherClass &matcher)
Statically switch to a Value matcher.
decltype(std::declval< T >().match(std::declval< MatchTarget >())) has_compatible_matcher_t
Trait to check whether T provides a 'match' method with type MatchTarget (Value, Operation,...
This header declares functions that assist transformations in the MemRef dialect.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_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::AttrOpMatcher m_Attr(StringRef attrName)
Matches a named attribute operation.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
detail::NameOpMatcher m_Op(StringRef opName)
Matches a named operation.
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_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::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_value_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...
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
The matcher that matches operations that have the specified attribute name, and binds the attribute v...
AttrOpBinder(StringRef attrName, AttrT *bindValue)
Creates a matcher instance that binds the attribute value to bind_value if match succeeds.
bool match(Operation *op)
AttrOpBinder(StringRef attrName)
Creates a matcher instance that doesn't bind if match succeeds.
The matcher that matches operations that have the specified attribute name.
bool match(Operation *op)
AttrOpMatcher(StringRef attrName)
The matcher that matches operations that have the specified op name.
NameOpMatcher(StringRef name)
bool match(Operation *op)
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(Attribute attr)
The matcher that matches a given target constant scalar / vector splat / tensor splat float value tha...
bool match(Operation *op)
bool(* predicate)(const APFloat &)
bool match(Attribute attr)
The matcher that matches a constant scalar / vector splat / tensor splat float Attribute or Operation...
bool match(Attribute attr)
constant_float_value_binder(FloatAttr::ValueType *bv)
Creates a matcher instance that binds the value to bv if match succeeds.
bool match(Operation *op)
FloatAttr::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 &)
bool match(Attribute attr)
The matcher that matches a constant scalar / vector splat / tensor splat integer Attribute or Operati...
constant_int_value_binder(IntegerAttr::ValueType *bv)
Creates a matcher instance that binds the value to bv if match succeeds.
bool match(Attribute attr)
IntegerAttr::ValueType * bind_value
bool match(Operation *op)
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)