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);
40 template <
typename T,
typename =
void>
47 std::declval<Operation *>()))>>
52 template <
typename T,
typename =
void>
59 std::declval<Operation *>(),
60 std::declval<SetVector<Operation *> &>()))>>
65 :
public llvm::ThreadSafeRefCountedBase<MatcherInterface> {
75 template <
typename MatcherFn>
82 return matcherFn.match(op);
88 return matcherFn.match(op, matchedOps);
102 template <VariadicOperatorFunction Func>
106 : matchers(std::move(matchers)) {}
110 return Func(op, &matchedOps, matchers);
114 std::vector<DynMatcher> matchers;
123 : implementation(implementation) {}
134 static std::unique_ptr<DynMatcher>
136 std::vector<DynMatcher> innerMatchers) {
139 return std::make_unique<DynMatcher>(
141 std::move(innerMatchers)));
143 return std::make_unique<DynMatcher>(
145 std::move(innerMatchers)));
147 llvm_unreachable(
"Invalid Op value.");
150 template <
typename MatcherFn>
151 static std::unique_ptr<DynMatcher>
153 auto impl = std::make_unique<MatcherFnImpl<MatcherFn>>(matcherFn);
154 return std::make_unique<DynMatcher>(
impl.release());
159 return implementation->match(op, matchedOps);
167 llvm::IntrusiveRefCntPtr<MatcherInterface> implementation;
168 std::string functionName;
172 template <
typename... Ps>
176 : varOp(varOp), params(std::forward<Ps>(params)...) {}
178 operator std::unique_ptr<DynMatcher>() const & {
180 varOp, getMatchers(std::index_sequence_for<Ps...>()));
183 operator std::unique_ptr<DynMatcher>() && {
185 varOp, std::move(*this).getMatchers(std::index_sequence_for<Ps...>()));
190 template <std::size_t... Is>
191 std::vector<DynMatcher> getMatchers(std::index_sequence<Is...>)
const & {
195 template <std::size_t... Is>
196 std::vector<DynMatcher> getMatchers(std::index_sequence<Is...>) && {
197 return {DynMatcher(std::get<Is>(std::move(params)))...};
201 std::tuple<Ps...> params;
206 template <
unsigned MinCount,
unsigned MaxCount>
210 template <
typename... Ms>
212 static_assert(MinCount <=
sizeof...(Ms) &&
sizeof...(Ms) <= MaxCount,
213 "invalid number of parameters for variadic matcher");
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
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 > constructDynMatcherFromMatcherFn(MatcherFn &matcherFn)
StringRef getFunctionName() const
static std::unique_ptr< DynMatcher > constructVariadic(VariadicOperator Op, std::vector< DynMatcher > innerMatchers)
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, SetVector< Operation * > &matchedOps) override
bool match(Operation *op) 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
DynMatcher::VariadicOperator varOp
VariadicOperatorMatcher< Ms... > operator()(Ms &&...Ps) const