39#ifndef TVM_IR_PATTERN_MATCH_H_
40#define TVM_IR_PATTERN_MATCH_H_
42#include <tvm/ffi/cast.h>
62template <
typename Derived>
89 template <
typename NodeType>
90 inline bool Match(
const NodeType& value)
const {
91 return Match(value, []() {
return true; });
108 template <
typename NodeType,
typename Condition>
109 bool Match(
const NodeType& value, Condition cond)
const {
111 return derived().Match_(value) && cond();
114 const Derived&
derived()
const {
return *
static_cast<const Derived*
>(
this); }
124 bool operator()(
const T& lhs,
const T& rhs)
const {
return lhs == rhs; }
154 template <
typename NodeRefType,
typename =
typename std::enable_if<
155 !std::is_same<NodeRefType, T>::value &&
156 std::is_base_of<ffi::ObjectRef, NodeRefType>::value &&
157 std::is_base_of<ffi::ObjectRef, T>::value>::type>
158 bool Match_(
const NodeRefType& value)
const {
159 if (
auto typed_value = value.template as<T>()) {
160 return Match_(*typed_value);
167 TVM_FFI_ICHECK(
value_.has_value());
171 T
EvalOr(
const T& default_value)
const {
return value_.value_or(default_value); }
184template <
typename Derived,
typename T>
193 if (!
static_cast<const Derived*
>(
this)->
Match_(value))
return false;
194 return pvar_.Match_(value);
197 template <
typename NodeRefType,
198 typename =
typename std::enable_if<std::is_base_of<NodeRefType, T>::value>::type>
199 bool Match_(
const NodeRefType& value)
const {
200 if (
auto typed_value = value.template as<T>()) {
201 return Match_(*typed_value);
228 T
Eval()
const {
return value_; }
234template <
typename... TPattern>
237 explicit PMatchesOneOf(
const TPattern&... patterns) : patterns_{patterns...} {}
249 template <
typename NodeType>
250 inline bool Match(
const NodeType& value)
const {
251 return Match(value, []() {
return true; });
270 template <
typename NodeType,
typename Condition>
271 inline bool Match(
const NodeType& value, Condition cond)
const {
272 return MatchImpl(value, cond, std::make_index_sequence<
sizeof...(TPattern)>());
276 template <
typename NodeType,
typename Condition>
277 inline bool MatchImpl(
const NodeType& value, Condition cond, std::index_sequence<>)
const {
281 template <
typename NodeType,
typename Condition,
size_t FirstIndex,
size_t... RemainingIndices>
282 inline bool MatchImpl(
const NodeType& value, Condition cond,
283 std::index_sequence<FirstIndex, RemainingIndices...>)
const {
284 return std::get<FirstIndex>(patterns_).Match(value, cond) ||
285 MatchImpl(value, cond, std::index_sequence<RemainingIndices...>());
297 std::tuple<
const TPattern&...> patterns_;
307template <
typename... TPattern>
308inline std::enable_if_t<(std::is_base_of_v<Pattern<TPattern>, TPattern> && ... &&
true),
309 PMatchesOneOf<TPattern...>>
Constant Pattern variable container.
Definition pattern_match.h:219
void InitMatch_() const
Definition pattern_match.h:224
T Eval() const
Definition pattern_match.h:228
bool Match_(const T &value) const
Definition pattern_match.h:226
PConst(T value)
Definition pattern_match.h:221
Default deep equality checker.
Definition pattern_match.h:122
bool operator()(const T &lhs, const T &rhs) const
Definition pattern_match.h:124
Definition pattern_match.h:235
PMatchesOneOf(const TPattern &... patterns)
Definition pattern_match.h:237
bool Match(const NodeType &value, Condition cond) const
Check if value matches one of the patterns.
Definition pattern_match.h:271
bool Match(const NodeType &value) const
Check if value matches one of the patterns.
Definition pattern_match.h:250
Wrapper for pattern variable container with extra match logic.
Definition pattern_match.h:185
void InitMatch_() const
Definition pattern_match.h:190
bool Match_(const T &value) const
Definition pattern_match.h:192
const PVarWithCheck< Derived, T > & Nested
Definition pattern_match.h:188
PVar< T > pvar_
Definition pattern_match.h:210
bool Match_(const NodeRefType &value) const
Definition pattern_match.h:199
T Eval() const
Definition pattern_match.h:207
Pattern variable container.
Definition pattern_match.h:138
bool Match_(const T &value) const
Definition pattern_match.h:145
T EvalOr(const T &default_value) const
Definition pattern_match.h:171
void InitMatch_() const
Definition pattern_match.h:143
std::optional< T > value_
The matched value.
Definition pattern_match.h:175
const PVar< T > & Nested
Definition pattern_match.h:141
bool Match_(const NodeRefType &value) const
Definition pattern_match.h:158
T Eval() const
Definition pattern_match.h:166
Base class of all the patterns.
Definition pattern_match.h:63
bool Match(const NodeType &value, Condition cond) const
Check if value matches the current pattern.
Definition pattern_match.h:109
Derived Nested
Nested storage type in the expression.
Definition pattern_match.h:77
const Derived & derived() const
Definition pattern_match.h:114
bool Match(const NodeType &value) const
Check if value matches the current pattern.
Definition pattern_match.h:90
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
std::enable_if_t<(std::is_base_of_v< Pattern< TPattern >, TPattern > &&... &&true), PMatchesOneOf< TPattern... > > MatchesOneOf(const TPattern &... patterns)
Definition pattern_match.h:310