67#ifndef TVM_IR_PRIM_PATTERN_MATCH_H_
68#define TVM_IR_PRIM_PATTERN_MATCH_H_
83 if (lhs.same_as(rhs))
return true;
98 return std::fabs(lhs->value - rhs->value) < 1e-20;
114template <
typename T,
typename DType,
115 typename = std::enable_if<std::is_base_of<T, PrimExpr>::value>>
120 bool Match_(
const T& value)
const {
return dtype_.Match_(value.ty()->dtype); }
147template <
typename OpType,
typename TA,
typename TB,
bool kFoldConstants = true>
157 bool Match_(
const ffi::ObjectRef& node)
const {
158 using NodeType =
typename OpType::ContainerType;
159 if (
const NodeType* ptr = node.as<NodeType>()) {
160 if (!a_.Match_(ptr->a))
return false;
161 if (!b_.Match_(ptr->b))
return false;
171 if constexpr (kFoldConstants) {
172 if (
auto ret = prim::TryConstFold<OpType>(lhs, rhs))
return ret.value();
174 return OpType(lhs, rhs);
178 typename TA::Nested a_;
179 typename TB::Nested b_;
182template <
typename TA>
189 bool Match_(
const ffi::ObjectRef& node)
const {
191 return ptr->value == value_;
200 typename TA::Nested ref_;
204#define TVM_PATTERN_BINARY_OP_EX(FuncName, NodeName, CheckStep) \
205 template <typename TA, typename TB> \
206 inline PBinaryExpr<NodeName, TA, TB> FuncName(const Pattern<TA>& a, const Pattern<TB>& b) { \
208 return PBinaryExpr<NodeName, TA, TB>(a.derived(), b.derived()); \
210 template <typename TA> \
211 inline PBinaryExpr<NodeName, TA, PConstWithTypeLike<TA>> FuncName(const Pattern<TA>& a, \
214 return FuncName(a, PConstWithTypeLike<TA>(a.derived(), b)); \
216 template <typename TA> \
217 inline PBinaryExpr<NodeName, PConstWithTypeLike<TA>, TA> FuncName(int64_t b, \
218 const Pattern<TA>& a) { \
220 return FuncName(PConstWithTypeLike<TA>(a.derived(), b), a); \
223#define TVM_PATTERN_BINARY_OP(FuncName, NodeName) TVM_PATTERN_BINARY_OP_EX(FuncName, NodeName, )
242#define TVM_PATTERN_BITWISE_OP(FuncName, NodeName) \
243 template <typename TA, typename TB> \
244 inline PBinaryExpr<NodeName, TA, TB, false> FuncName(const Pattern<TA>& a, \
245 const Pattern<TB>& b) { \
246 return PBinaryExpr<NodeName, TA, TB, false>(a.derived(), b.derived()); \
255#undef TVM_PATTERN_BITWISE_OP
271template <
typename TA>
274 explicit PNotExpr(
const TA& value) : value_(value) {}
278 bool Match_(
const ffi::ObjectRef& node)
const {
280 if (!value_.Match_(ptr->a))
return false;
290 typename TA::Nested value_;
293template <
typename TA>
302template <
typename TA>
309 bool Match_(
const ffi::ObjectRef& node)
const {
311 if (!value_.Match_(ptr->a))
return false;
321 typename TA::Nested value_;
324template <
typename TA>
336template <
typename TCond,
typename TA,
typename TB>
339 PSelectExpr(
const TCond& condition,
const TA& true_value,
const TB& false_value)
340 : condition_(condition), true_value_(true_value), false_value_(false_value) {}
343 condition_.InitMatch_();
344 true_value_.InitMatch_();
345 false_value_.InitMatch_();
348 bool Match_(
const ffi::ObjectRef& node)
const {
350 if (!condition_.Match_(ptr->condition))
return false;
351 if (!true_value_.Match_(ptr->true_value))
return false;
352 if (!false_value_.Match_(ptr->false_value))
return false;
360 return prim::Select(condition_.Eval(), true_value_.Eval(), false_value_.Eval());
364 typename TCond::Nested condition_;
365 typename TA::Nested true_value_;
366 typename TB::Nested false_value_;
382template <
typename TCond,
typename TA,
typename TB>
395template <
typename DType,
typename TA>
398 PCastExpr(
const TA& value,
const DType& dtype) : dtype_(dtype), value_(value) {}
405 bool Match_(
const ffi::ObjectRef& node)
const {
407 if (!dtype_.Match_(ptr->ty.as_or_throw<
PrimType>()->dtype))
return false;
408 if (!value_.Match_(ptr->value))
return false;
418 typename DType::Nested dtype_;
419 typename TA::Nested value_;
433template <
typename DType,
typename TA>
444template <
typename TBase,
typename TStr
ide,
typename TLanes>
447 PRampExpr(
const TBase& base,
const TStride& stride,
const TLanes& lanes)
448 : base_(base), stride_(stride), lanes_(lanes) {}
452 stride_.InitMatch_();
456 bool Match_(
const ffi::ObjectRef& node)
const {
458 if (!base_.Match_(ptr->base))
return false;
459 if (!stride_.Match_(ptr->stride))
return false;
460 if (!lanes_.Match_(ptr->lanes))
return false;
470 typename TBase::Nested base_;
471 typename TStride::Nested stride_;
472 typename TLanes::Nested lanes_;
488template <
typename TBase,
typename TStr
ide,
typename TLanes>
495template <
typename TBase>
508template <
typename TA,
typename TLanes>
511 PBroadcastExpr(
const TA& value,
const TLanes& lanes) : value_(value), lanes_(lanes) {}
518 bool Match_(
const ffi::ObjectRef& node)
const {
520 if (!value_.Match_(ptr->value))
return false;
521 if (!lanes_.Match_(ptr->lanes))
return false;
531 typename TA::Nested value_;
532 typename TLanes::Nested lanes_;
546template <
typename TA,
typename TLanes>
555template <
bool stop, std::
size_t I,
typename F>
556struct tuple_for_each_dispatcher {
557 template <
typename TTuple>
558 static void run(F& f,
const TTuple& tuple) {
559 f(I, std::get<I>(tuple));
560 tuple_for_each_dispatcher<(I + 1) == std::tuple_size<TTuple>::value, (I + 1), F>::run(f, tuple);
564template <std::
size_t I,
typename F>
565struct tuple_for_each_dispatcher<true, I, F> {
566 template <
typename TTuple>
567 static void run(F& f,
const TTuple& tuple) {}
570template <
typename F,
typename TTuple>
571inline void tuple_for_each(F& f,
const TTuple& tuple) {
572 tuple_for_each_dispatcher<std::tuple_size<TTuple>::value == 0, 0, F>::run(f, tuple);
575struct PCallExprInitMatchFunctor {
576 template <
typename T>
577 void operator()(
size_t i,
const T& pattern)
const {
578 pattern.InitMatch_();
582struct PCallExprMatchFunctor {
586 explicit PCallExprMatchFunctor(
const CallObj* call) : call_(call) {}
588 template <
typename T>
589 void operator()(
size_t i,
const T& pattern) {
590 matched_ = matched_ && pattern.Match_(call_->
args[i]);
594struct PCallExprEvalArgsFunctor {
595 ffi::Array<PrimExpr> args_;
597 template <
typename T>
598 void operator()(
size_t i,
const T& pattern) {
599 args_.push_back(pattern.Eval());
611template <
typename Op,
typename... TArgs>
614 explicit PCallExpr(
const TArgs&... args) : args_(args...) {}
617 detail::PCallExprInitMatchFunctor finit;
618 detail::tuple_for_each(finit, args_);
621 bool Match_(
const ffi::ObjectRef& node)
const {
623 if (ptr->args.size() !=
sizeof...(TArgs))
return false;
624 if (!ptr->op.same_as(Op::GetOp()))
return false;
625 detail::PCallExprMatchFunctor fmatch(ptr);
626 detail::tuple_for_each(fmatch, args_);
627 return fmatch.matched_;
634 detail::PCallExprEvalArgsFunctor feval_args;
635 detail::tuple_for_each(feval_args, args_);
636 return Op::Eval(feval_args.args_);
640 std::tuple<
typename TArgs::Nested...> args_;
664template <
typename TCond,
typename TA,
typename TB>
Shared primitive construction and constant helpers.
Call corresponds to callable invocation.
Definition expr.h:788
ffi::Array< Expr > args
The arguments of the call.
Definition expr.h:802
Managed reference to CallObj.
Definition expr.h:825
Managed reference class to FloatImmObj.
Definition expr.h:411
Constant integer literals in the program.
Definition expr.h:314
Managed reference class to IntImmObj.
Definition expr.h:331
Reference-counted handle to a canonical named operator.
Definition op.h:140
Pattern binary expression.
Definition pattern_match.h:148
void InitMatch_() const
Definition pattern_match.h:152
PrimExpr Eval() const
Definition pattern_match.h:168
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:157
PBinaryExpr(const TA &a, const TB &b)
Definition pattern_match.h:150
Pattern bitwise not expression.
Definition pattern_match.h:303
PrimExpr Eval() const
Definition pattern_match.h:318
PBitwiseNotExpr(const TA &value)
Definition pattern_match.h:305
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:309
void InitMatch_() const
Definition pattern_match.h:307
Pattern broadcast expression.
Definition pattern_match.h:509
PBroadcastExpr(const TA &value, const TLanes &lanes)
Definition pattern_match.h:511
void InitMatch_() const
Definition pattern_match.h:513
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:518
PrimExpr Eval() const
Definition pattern_match.h:528
Pattern CallExpr expression.
Definition pattern_match.h:612
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:621
void InitMatch_() const
Definition pattern_match.h:616
PrimExpr Eval() const
Definition pattern_match.h:633
PCallExpr(const TArgs &... args)
Definition pattern_match.h:614
Pattern cast expression.
Definition pattern_match.h:396
PCastExpr(const TA &value, const DType &dtype)
Definition pattern_match.h:398
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:405
PrimExpr Eval() const
Definition pattern_match.h:415
void InitMatch_() const
Definition pattern_match.h:400
Definition pattern_match.h:183
void InitMatch_() const
Definition pattern_match.h:187
PConstWithTypeLike(const TA &ref, int64_t value)
Definition pattern_match.h:185
PrimExpr Eval() const
Definition pattern_match.h:197
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:189
bool operator()(const FloatImm &lhs, const FloatImm &rhs) const
Definition pattern_match.h:97
bool operator()(const IntImm &lhs, const IntImm &rhs) const
Definition pattern_match.h:91
bool operator()(const PrimExpr &lhs, const PrimExpr &rhs) const
Definition pattern_match.h:82
bool operator()(const Var &lhs, const Var &rhs) const
Definition pattern_match.h:105
Default deep equality checker.
Definition pattern_match.h:122
Pattern not expression.
Definition pattern_match.h:272
void InitMatch_() const
Definition pattern_match.h:276
PNotExpr(const TA &value)
Definition pattern_match.h:274
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:278
PrimExpr Eval() const
Definition pattern_match.h:287
Pattern ramp expression.
Definition pattern_match.h:445
PrimExpr Eval() const
Definition pattern_match.h:467
PRampExpr(const TBase &base, const TStride &stride, const TLanes &lanes)
Definition pattern_match.h:447
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:456
void InitMatch_() const
Definition pattern_match.h:450
Pattern select expression.
Definition pattern_match.h:337
PrimExpr Eval() const
Definition pattern_match.h:359
void InitMatch_() const
Definition pattern_match.h:342
bool Match_(const ffi::ObjectRef &node) const
Definition pattern_match.h:348
PSelectExpr(const TCond &condition, const TA &true_value, const TB &false_value)
Definition pattern_match.h:339
Wrapper for pattern variable container with extra match logic.
Definition pattern_match.h:185
Pattern variable container with expr type check.
Definition pattern_match.h:116
DType::Nested dtype_
Definition pattern_match.h:123
bool Match_(const T &value) const
Definition pattern_match.h:120
PVarWithDataType(const DType &dtype)
Definition pattern_match.h:118
Pattern variable container for data type with lanes.
Definition pattern_match.h:129
PVecDataType(DLDataType elem_dtype)
construct vector dtype placeholder with element type check
Definition pattern_match.h:132
DLDataType elem_dtype_
Definition pattern_match.h:137
bool Match_(DLDataType dtype) const
Definition pattern_match.h:134
Base class of all the patterns.
Definition pattern_match.h:63
const Derived & derived() const
Definition pattern_match.h:114
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Managed reference to VarObj.
Definition expr.h:571
Managed reference to AddObj.
Definition expr.h:118
Managed reference to AndObj.
Definition expr.h:578
Managed reference to BitwiseAndObj.
Definition expr.h:181
Managed reference to BitwiseNotObj.
Definition expr.h:667
Managed reference to BitwiseOrObj.
Definition expr.h:202
Managed reference to BitwiseXorObj.
Definition expr.h:223
Create a vector where all the elements are value.
Definition vector_expr.h:82
Managed reference to BroadcastObj.
Definition vector_expr.h:104
Cast value from one data type to another.
Definition expr.h:56
Managed reference to CastObj.
Definition expr.h:74
Managed reference to DivObj.
Definition expr.h:290
Managed reference to EQObj.
Definition expr.h:442
Managed reference to FloorDivObj.
Definition expr.h:335
Managed reference to FloorModObj.
Definition expr.h:356
Managed reference to GEObj.
Definition expr.h:547
Managed reference to GTObj.
Definition expr.h:526
Managed reference to LEObj.
Definition expr.h:505
Managed reference to LTObj.
Definition expr.h:484
Managed reference to MaxObj.
Definition expr.h:398
Managed reference to MinObj.
Definition expr.h:377
Managed reference to ModObj.
Definition expr.h:314
Managed reference to MulObj.
Definition expr.h:266
Managed reference to NEObj.
Definition expr.h:463
Managed reference to NotObj.
Definition expr.h:638
Managed reference to OrObj.
Definition expr.h:609
Managed reference to RShiftObj.
Definition expr.h:160
Construct a vector with lanes elements where its i-th element equals base + i * stride....
Definition vector_expr.h:45
Managed reference to RampObj.
Definition vector_expr.h:71
return true_value if condition is true, otherwise return false_value.
Definition expr.h:684
Managed reference to SelectObj.
Definition expr.h:712
Managed reference to SubObj.
Definition expr.h:244
Centralized location for constant folding.
PrimExpr MakeConst(PrimType dtype, ValueType value, Location loc=UnknownLoc())
Make a const value with certain data type.
Definition op.h:814
TVM_TIRX_DLL const Op & if_then_else_op()
Same as select, used for unsafe memory access.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
PBroadcastExpr< TA, TLanes > broadcast(const Pattern< TA > &value, const Pattern< TLanes > &lanes)
Construct a broadcast pattern.
Definition pattern_match.h:547
void DivAmbiguityError(const TA &a)
Helper function to raise a compiler error about division ambiguity.
Definition op.h:926
TVM_TIRX_DLL PrimExpr operator!(PrimExpr a)
not
TVM_TIRX_DLL PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Location loc=UnknownLoc())
Conditional expression.
PSelectExpr< TCond, TA, TB > select(const Pattern< TCond > &condition, const Pattern< TA > &true_value, const Pattern< TB > &false_value)
Construct a select pattern.
Definition pattern_match.h:383
TVM_TIRX_DLL PrimExpr truncdiv(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute trunc(a / b)
TVM_TIRX_DLL PrimExpr operator~(PrimExpr a)
take bitwise negation of two values
TVM_TIRX_DLL PrimExpr div(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute division in C semantics.
PCastExpr< DType, TA > cast(const Pattern< TA > &value, const Pattern< DType > &dtype)
Construct a cast pattern.
Definition pattern_match.h:434
TVM_TIRX_DLL PrimExpr floormod(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute the remainder of floordiv
TVM_TIRX_DLL PrimExpr truncmod(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute the remainder of truncdiv
TVM_TIRX_DLL PrimExpr floordiv(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute floor(a / b)
PRampExpr< TBase, TStride, TLanes > ramp(const Pattern< TBase > &base, const Pattern< TStride > &stride, const Pattern< TLanes > &lanes)
Construct a ramp pattern.
Definition pattern_match.h:489
PrimExpr max(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
max of source expression over axis
PrimExpr min(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
max of source expression over axis
Shared expression-template pattern and capture protocol.
#define TVM_PATTERN_BINARY_OP(FuncName, NodeName)
Definition pattern_match.h:223
#define TVM_PATTERN_BINARY_OP_EX(FuncName, NodeName, CheckStep)
Definition pattern_match.h:204
#define TVM_PATTERN_BITWISE_OP(FuncName, NodeName)
Definition pattern_match.h:242
Definition pattern_match.h:644
static PrimExpr Eval(ffi::Array< PrimExpr > args)
Definition pattern_match.h:645
static const Op & GetOp()
Definition pattern_match.h:648
Compare two expressions recursively and check if they are equal to each other without var remapping.
Definition expr.h:795