15 #ifndef MLIR_IR_MATCHERS_H
16 #define MLIR_IR_MATCHERS_H
33 typename ValueType =
typename std::enable_if_t<
34 std::is_base_of<Attribute, AttrClass>::value, AttrClass>::ValueType,
36 typename = std::enable_if_t<!std::is_void<ValueType>::value>>
44 if (
auto intAttr = llvm::dyn_cast<AttrClass>(attr)) {
75 template <
typename AttrT>
91 LogicalResult result = op->
fold(std::nullopt, foldedOp);
93 assert(succeeded(result) &&
"expected ConstantLike op to be foldable");
95 if (
auto attr = llvm::dyn_cast<AttrT>(foldedOp.front().get<
Attribute>())) {
113 auto inferIntRangeOp = dyn_cast<InferIntRangeInterface>(op);
114 if (!inferIntRangeOp)
122 bool matched =
false;
123 auto setResultRanges = [&](
Value value,
125 if (argRanges.isUninitialized())
132 inferIntRangeOp.inferResultRangesFromOptional(argRanges, setResultRanges);
139 template <
typename AttrT>
170 if (matcher.
match(attr))
173 if (
auto splatAttr = dyn_cast<SplatElementsAttr>(attr))
185 if (isa<FloatType, VectorType, RankedTensorType>(type))
198 APFloat value(APFloat::Bogus());
203 APFloat value(APFloat::Bogus());
218 if (matcher.
match(attr))
221 if (
auto splatAttr = dyn_cast<SplatElementsAttr>(attr))
233 if (isa<IntegerType, IndexType, VectorType, RankedTensorType>(type))
282 template <
typename OpClass>
289 template <
typename T,
typename MatchTarget>
291 decltype(std::declval<T>().match(std::declval<MatchTarget>()));
294 template <
typename MatcherClass>
296 MatcherClass,
Value>::value,
303 template <
typename MatcherClass>
309 return matcher.match(defOp);
335 template <
typename TupleT,
class CallbackT, std::size_t... Is>
337 std::index_sequence<Is...>) {
339 (callback(std::integral_constant<std::size_t, Is>{}, std::get<Is>(tuple)),
343 template <
typename... Tys,
typename CallbackT>
344 constexpr
void enumerate(std::tuple<Tys...> &tuple, CallbackT &&callback) {
346 std::make_index_sequence<
sizeof...(Tys)>{});
350 template <
typename OpType,
typename... OperandMatchers>
355 if (!isa<OpType>(op) || op->
getNumOperands() !=
sizeof...(OperandMatchers))
385 template <
typename AttrT>
391 template <
typename AttrT>
400 return {[](
const APFloat &value) {
return value.isZero(); }};
405 return {[](
const APFloat &value) {
return value.isPosZero(); }};
410 return {[](
const APFloat &value) {
return value.isNegZero(); }};
415 return {[](
const APFloat &value) {
416 return APFloat(value.getSemantics(), 1) == value;
423 return {[](
const APFloat &value) {
424 return !value.isNegative() && value.isInfinity();
431 return {[](
const APFloat &value) {
432 return value.isNegative() && value.isInfinity();
438 return {[](
const APInt &value) {
return 0 == value; }};
444 return {[](
const APInt &value) {
return 0 != value; }};
459 return range.smin().sgt(0) || range.smax().slt(0);
468 return range.smin().sgt(-1) || range.smax().slt(-1);
474 return {[](
const APInt &value) {
return 1 == value; }};
478 template <
typename OpClass>
484 template <
typename Pattern>
489 return const_cast<Pattern &
>(pattern).match(op);
494 template <
typename Pattern>
497 return const_cast<Pattern &
>(pattern).match(op);
502 template <
typename Pattern>
506 "Pattern does not support matching Attributes");
509 return const_cast<Pattern &
>(pattern).match(attr);
514 inline detail::constant_float_value_binder
521 inline detail::constant_int_value_binder
526 template <
typename OpType,
typename... Matchers>
527 auto m_Op(Matchers... matchers) {
Attributes are known-constant values of operations.
A set of arbitrary-precision integers representing bounds on a given integer value.
static ConstantIntRanges constant(const APInt &value)
Create a ConstantIntRanges with a constant value - that is, with the bounds [value,...
This lattice value represents the integer range of an SSA value.
const ConstantIntRanges & getValue() const
Get the known integer value range.
static IntegerValueRange getMaxRange(Value value)
Create a maximal range ([0, uint_max(t)] / [int_min(t), int_max(t)]) range that is used to mark the v...
This class provides the API for a sub-set of ops that are known to be constant-like.
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
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.
AttrClass getAttrOfType(StringAttr name)
bool hasAttr(StringAttr name)
Return true if the operation has an attribute with the provided name, false otherwise.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
unsigned getNumOperands()
OperationName getName()
The name of an operation is the key identifier for it.
operand_range getOperands()
Returns an iterator on the underlying Value's.
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...
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.
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,...
Include the generated interface declarations.
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.
detail::constant_int_range_predicate_matcher m_IntRangeWithoutNegOneS()
Matches a constant scalar / vector splat / tensor splat integer or a signed integer range that does n...
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_int_range_predicate_matcher m_IntRangeWithoutZeroS()
Matches a constant scalar / vector splat / tensor splat integer or a signed integer range that does n...
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.
detail::constant_int_range_predicate_matcher m_IntRangeWithoutZeroU()
Matches a constant scalar / vector splat / tensor splat integer or a unsigned integer range that does...
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)
A matcher that matches a given a constant scalar / vector splat / tensor splat integer value or a con...
bool match(Attribute attr)
bool match(Operation *op)
bool(* predicate)(const ConstantIntRanges &)
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)
A matcher that matches operations that implement the InferIntRangeInterface interface,...
IntegerValueRange * bind_value
infer_int_range_op_binder(IntegerValueRange *bind_value)
bool match(Operation *op)
The matcher that matches a certain kind of op.
bool match(Operation *op)