21#ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERSINTERNAL_H
22#define MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERSINTERNAL_H
25#include "llvm/ADT/IntrusiveRefCntPtr.h"
32 ArrayRef<DynMatcher> innerMatchers);
34 ArrayRef<DynMatcher> innerMatchers);
40template <
typename T,
typename =
void>
47 std::declval<Operation *>()))>>
52template <
typename T,
typename =
void>
59 std::declval<Operation *>(),
60 std::declval<SetVector<Operation *> &>()))>>
65 :
public llvm::ThreadSafeRefCountedBase<MatcherInterface> {
75template <
typename MatcherFn>
82 return matcherFn.match(op);
88 return matcherFn.match(op, matchedOps);
102template <VariadicOperatorFunction Func>
112 std::vector<DynMatcher> matchers;
121 : implementation(implementation) {}
132 static std::unique_ptr<DynMatcher>
134 std::vector<DynMatcher> innerMatchers) {
137 return std::make_unique<DynMatcher>(
139 std::move(innerMatchers)));
141 return std::make_unique<DynMatcher>(
143 std::move(innerMatchers)));
145 llvm_unreachable(
"Invalid Op value.");
148 template <
typename MatcherFn>
149 static std::unique_ptr<DynMatcher>
151 auto impl = std::make_unique<MatcherFnImpl<MatcherFn>>(matcherFn);
152 return std::make_unique<DynMatcher>(
impl.release());
157 return implementation->match(op, matchedOps);
165 llvm::IntrusiveRefCntPtr<MatcherInterface> implementation;
166 std::string functionName;
170template <VariadicOperatorFunction Func>
172 : matchers(std::move(matchers)) {}
174template <VariadicOperatorFunction Func>
177template <VariadicOperatorFunction Func>
179 return Func(op,
nullptr, matchers);
182template <VariadicOperatorFunction Func>
185 return Func(op, &matchedOps, matchers);
189template <
typename... Ps>
193 : varOp(varOp), params(std::forward<Ps>(params)...) {}
195 operator std::unique_ptr<DynMatcher>() const & {
197 varOp, getMatchers(std::index_sequence_for<Ps...>()));
200 operator std::unique_ptr<DynMatcher>() && {
202 varOp, std::move(*this).getMatchers(std::index_sequence_for<Ps...>()));
207 template <std::size_t... Is>
208 std::vector<DynMatcher> getMatchers(std::index_sequence<Is...>)
const & {
212 template <std::size_t... Is>
213 std::vector<DynMatcher> getMatchers(std::index_sequence<Is...>) && {
214 return {DynMatcher(std::get<Is>(std::move(params)))...};
218 std::tuple<Ps...> params;
223template <
unsigned MinCount,
unsigned MaxCount>
227 template <
typename... Ms>
229 static_assert(MinCount <=
sizeof...(Ms) &&
sizeof...(Ms) <= MaxCount,
230 "invalid number of parameters for variadic matcher");
236const VariadicOperatorMatcherFunc<1, std::numeric_limits<unsigned>::max()>
This provides public APIs that all operations should have.
Operation is the basic unit of execution within MLIR.
DynMatcher(MatcherInterface *implementation)
bool match(Operation *op) const
static std::unique_ptr< DynMatcher > constructVariadic(VariadicOperator Op, std::vector< DynMatcher > innerMatchers)
StringRef getFunctionName() const
static std::unique_ptr< DynMatcher > constructDynMatcherFromMatcherFn(MatcherFn &matcherFn)
bool hasFunctionName() const
bool match(Operation *op, SetVector< Operation * > &matchedOps) const
void setFunctionName(StringRef name)
MatcherFnImpl(MatcherFn &matcherFn)
bool match(Operation *op, SetVector< Operation * > &matchedOps) override
bool match(Operation *op) override
virtual ~MatcherInterface()=default
virtual bool match(Operation *op, SetVector< Operation * > &matchedOps)=0
virtual bool match(Operation *op)=0
bool match(Operation *op) override
~VariadicMatcher() override
VariadicMatcher(std::vector< DynMatcher > matchers)
VariadicOperatorMatcher(DynMatcher::VariadicOperator varOp, Ps &&...params)
bool anyOfVariadicOperator(Operation *op, SetVector< Operation * > *matchedOps, ArrayRef< DynMatcher > innerMatchers)
const VariadicOperatorMatcherFunc< 1, std::numeric_limits< unsigned >::max()> anyOf
bool allOfVariadicOperator(Operation *op, SetVector< Operation * > *matchedOps, ArrayRef< DynMatcher > innerMatchers)
const VariadicOperatorMatcherFunc< 1, std::numeric_limits< unsigned >::max()> allOf
Computes the backward-slice of all transitive defs reachable from rootOp, if innerMatcher matches.
bool(*)(Operation *op, SetVector< Operation * > *matchedOps, ArrayRef< DynMatcher > innerMatchers) VariadicOperatorFunction
llvm::SetVector< T, Vector, Set, N > SetVector
DynMatcher::VariadicOperator varOp
VariadicOperatorMatcher< Ms... > operator()(Ms &&...Ps) const