47#define TVM_TIR_REGISTER_OP(OpName) \
48 TVM_REGISTER_OP("tirx." OpName) \
49 .set_attr<TScriptPrinterName>("TScriptPrinterName", OpName) \
50 .set_attr<TIRxOpCategory>("TIRxOpCategory", ffi::String("builtin"), 1)
52#define TVM_TIRX_REGISTER_OP(OpName) TVM_TIR_REGISTER_OP(OpName)
574 ffi::Array<PrimExpr> init = {}, Span span = Span());
584 ffi::Array<PrimExpr> init = {}, Span span = Span());
595 ffi::Array<PrimExpr> init = {}, Span span = Span());
606 ffi::Array<PrimExpr> init = {}, Span span = Span());
617 ffi::Array<PrimExpr> init = {}, Span span = Span());
628 ffi::Array<PrimExpr> init = {}, Span span = Span());
723 <<
"tirx." <<
op_name <<
" only supports floating-point inputs, but got " << dtype;
727#define TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckInputDType) \
728 inline PrimExpr OpName(PrimExpr x, Span span = Span()) { \
729 static const Op op = Op::Get("tirx." #OpName); \
730 PrimType x_ty = x.ty(); \
731 CheckInputDType(#OpName, x_ty); \
732 if (x_ty.MatchesElementType(DLDataTypeCode::kDLBfloat, 16)) { \
733 PrimType bf16_ty = x_ty; \
735 x_ty.IsScalableVector() \
736 ? PrimType::ScalableVector(DLDataTypeCode::kDLFloat, 32, x_ty.VScaleFactor()) \
737 : PrimType::Float(32, x_ty.lanes()); \
738 PrimExpr x_fp32 = prim::Cast(f32_ty, x, span); \
739 PrimExpr result_fp32 = Call(f32_ty, op, {x_fp32}, {}, {}, span).as_or_throw<PrimExpr>(); \
740 return prim::Cast(bf16_ty, result_fp32, span); \
742 return Call(x_ty, op, {x}, {}, {}, span).as_or_throw<PrimExpr>(); \
746#define TVM_DECLARE_INTRIN_UNARY(OpName) \
747 TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, [](const char*, const PrimType&) {})
749#define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName) \
750 TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckMathUnaryOpInputDType)
778#define TVM_DECLARE_INTRIN_BINARY(OpName) \
779 inline PrimExpr OpName(PrimExpr x, PrimExpr y, Span span = Span()) { \
780 static const Op op = Op::Get("tirx." #OpName); \
781 return Call(x.ty(), op, {x, y}, {}, {}, span).as_or_throw<PrimExpr>(); \
824 typename =
typename std::enable_if<std::is_standard_layout<ValueType>::value &&
825 std::is_trivial<ValueType>::value>::type>
841 if (!
x.defined())
return nullptr;
903template <
typename FReduce>
952 if (!stmt.defined())
return true;
954 auto value = op->value.as<
PrimExpr>();
958 return op->seq.size() == 0;
963template <
typename ValueType>
966 if (code == DLDataTypeCode::kDLInt || code == DLDataTypeCode::kDLBool) {
969 if (code == DLDataTypeCode::kDLUInt) {
974 }
else if (
uval <=
static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
983 if (dtype.
MatchesCode(DLDataTypeCode::kDLFloat, DLDataTypeCode::kDLFloat8_e3m4,
984 DLDataTypeCode::kDLFloat8_e4m3, DLDataTypeCode::kDLFloat8_e4m3b11fnuz,
985 DLDataTypeCode::kDLFloat8_e4m3fn, DLDataTypeCode::kDLFloat8_e4m3fnuz,
986 DLDataTypeCode::kDLFloat8_e5m2, DLDataTypeCode::kDLFloat8_e5m2fnuz,
987 DLDataTypeCode::kDLFloat8_e8m0fnu, DLDataTypeCode::kDLFloat6_e2m3fn,
988 DLDataTypeCode::kDLFloat6_e3m2fn, DLDataTypeCode::kDLFloat4_e2m1fn) ||
990 return FloatImm(dtype,
static_cast<double>(value), span);
1001template <
typename ValueType,
typename>
1023#define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc) \
1024 inline PrimExpr Name(PrimExpr& a, PrimExpr b) { \
1029#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name) \
1030 inline PrimExpr Name(const PrimExpr& a, float b) { return Name(a, PrimExpr(b)); } \
1031 inline PrimExpr Name(float a, const PrimExpr& b) { return Name(PrimExpr(a), b); } \
1032 inline PrimExpr Name(int a, const PrimExpr& b) { return Name(tirx::MakeConst(b.ty(), a), b); } \
1033 inline PrimExpr Name(const PrimExpr& a, int b) { return Name(a, tirx::MakeConst(a.ty(), b)); } \
1034 inline PrimExpr Name(const PrimExpr& a, double b) { \
1035 return Name(a, FloatImm(PrimType::Float(64), b)); \
1038#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1039 inline PrimExpr Name(const PrimExpr& a, float b, Span span = Span()) { \
1040 return Name(a, PrimExpr(b), span); \
1042 inline PrimExpr Name(float a, const PrimExpr& b, Span span = Span()) { \
1043 return Name(PrimExpr(a), b, span); \
1045 inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1046 return Name(tirx::MakeConst(b.ty(), a), b, span); \
1048 inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1049 return Name(a, tirx::MakeConst(a.ty(), b), span); \
1051 inline PrimExpr Name(const PrimExpr& a, double b, Span span = Span()) { \
1052 return Name(a, FloatImm(PrimType::Float(64), b), span); \
1055#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name) \
1056 inline PrimExpr Name(const PrimExpr& a, bool b) { return Name(a, PrimExpr(b)); } \
1057 inline PrimExpr Name(bool a, const PrimExpr& b) { return Name(PrimExpr(a), b); }
1059#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1060 inline PrimExpr Name(const PrimExpr& a, bool b, Span span = Span()) { \
1061 return Name(a, PrimExpr(b), span); \
1063 inline PrimExpr Name(bool a, const PrimExpr& b, Span span = Span()) { \
1064 return Name(PrimExpr(a), b, span); \
1067#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name) \
1068 inline PrimExpr Name(const PrimExpr& a, int b) { return Name(a, tirx::MakeConst(a.ty(), b)); } \
1069 inline PrimExpr Name(int a, const PrimExpr& b) { return Name(tirx::MakeConst(b.ty(), a), b); }
1071#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1072 inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1073 return Name(a, tirx::MakeConst(a.ty(), b), span); \
1075 inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1076 return Name(tirx::MakeConst(b.ty(), a), b, span); \
1128template <
typename TA>
1132 "TVM supports multiple types of integer divisions, "
1133 "please call div, indexdiv/indexmod, "
1134 "floordiv/floormod or truncdiv/truncmod directly "
1135 "to avoid ambiguity in the code. "
1136 "Checkout these functions in tirx/op.h.");
1144template <
typename TB>
1150template <
typename TB>
1156template <
typename TB>
Managed reference to CallNode.
Definition expr.h:474
Managed reference to ExprNode.
Definition base_expr.h:335
Constant floating point literals in the program.
Definition expr.h:550
Managed reference class to FloatImmNode.
Definition expr.h:567
Constant integer literals in the program.
Definition expr.h:487
int64_t value
the Internal value.
Definition expr.h:490
Managed reference class to IntImmNode.
Definition expr.h:504
Low-level raw pointer type.
Definition type.h:47
static PointerType VoidPointerTy(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Primitive data types used in the low-level IR.
Definition base_expr.h:119
Definition base_expr.h:137
TVM_FFI_INLINE DLDataTypeCode code() const
Definition base_expr.h:174
TVM_FFI_INLINE PrimType WithLanes(int lanes) const
Return the same scalar element type with a fixed lane count.
Definition base_expr.h:274
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
TVM_FFI_INLINE bool IsScalableVector() const
Whether this type is a scalable vector.
Definition base_expr.h:227
TVM_FFI_INLINE bool IsFixedLengthVector() const
Whether this type is a fixed-length vector.
Definition base_expr.h:232
TVM_FFI_INLINE bool MatchesCode(Codes... codes) const
Check whether the dtype code matches any of the provided DLPack codes.
Definition base_expr.h:208
TVM_FFI_INLINE bool MatchesElementType(DLDataTypeCode code, int bits) const
Check the scalar element code and bit width.
Definition base_expr.h:198
TVM_FFI_INLINE int32_t VScaleFactor() const
Definition base_expr.h:279
TVM_FFI_INLINE int32_t lanes() const
Definition base_expr.h:185
static PrimType UInt(int bits, int lanes=1)
Construct an unsigned integer type with fixed lanes.
Definition source_map.h:111
Managed reference to TypeNode.
Definition base_expr.h:77
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Create a vector where all the elements are value.
Definition vector_expr.h:73
Managed reference to BroadcastNode.
Definition vector_expr.h:92
Managed reference to MulNode.
Definition expr.h:170
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition stmt.h:340
The container of seq statement. Represent a sequence of statements.
Definition stmt.h:315
Container of all statements.
Definition stmt.h:67
Primitive operators(builtin intrinsics) and registry for them.
IR/AST nodes for TVM types shared across IR variants.
const Op & vscale()
Get the target's vscale value. It will be lowered to llvm.vscale intrinsic (https://llvm....
bool is_const_number(const PrimExpr &x)
Check whether x is an integer/float constant.
Definition op.h:925
bool is_zero(const PrimExpr &x)
Check whether x is a constant integer 0.
Definition op.h:878
bool is_const_power_of_two_integer(const PrimExpr &x, int *shift)
Check whether x is a constant power of two If x is power of two, write the power to the shift.
bool is_positive_const(const PrimExpr &a)
Definition op.h:936
Expr ConstHandle(int64_t value, Span span=Span())
Make a constant opaque-pointer value.
Definition op.h:1016
const int64_t * as_const_int(const PrimExpr &x)
Get x as constant int expression.
Definition op.h:840
bool is_negative_const(const PrimExpr &a)
Definition op.h:941
bool is_const_int(const PrimExpr &x, int64_t value)
Check whether x is a constant integer expression.
Definition op.h:946
bool is_one(const PrimExpr &x)
Check whether x is a constant integer 1.
Definition op.h:870
bool is_no_op(const tirx::Stmt &stmt)
Check whether stmt is nop.
Definition op.h:951
PrimExpr foldl(FReduce freduce, PrimExpr init_value, const ffi::Array< PrimExpr > &values, Span span=Span())
Left fold.
Definition op.h:904
PrimExpr MakeConstScalar(PrimType dtype, ValueType value, Span span=Span())
Definition op.h:964
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition op.h:1002
bool IsPointerType(const Type &type, DLDataType element_type)
Check if type is a pointer to a runtime element type.
Definition op.h:798
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr isfinite(PrimExpr x, Span span=Span())
Check if x is finite.
PrimExpr ceildiv(PrimExpr a, PrimExpr b, Span span=Span())
compute ceil(a / b)
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
PrimExpr tanh(PrimExpr x, Span span=Span())
Definition op.h:756
PrimExpr erf(PrimExpr x, Span span=Span())
Definition op.h:755
PrimExpr shapediv(PrimExpr a, PrimExpr b, Span span=Span())
compute ceil(a / b) where a and b are non-negative.
PrimExpr log10(PrimExpr x, Span span=Span())
Definition op.h:762
PrimExpr div(PrimExpr a, PrimExpr b, Span span=Span())
compute division in C semantics.
PrimExpr operator/(PrimExpr a, PrimExpr b)
division operator
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
PrimExpr max_value(PrimType dtype, Span span=Span())
PrimExpr truncmod(PrimExpr a, PrimExpr b, Span span=Span())
compute the remainder of truncdiv
PrimExpr logical_and(PrimExpr a, PrimExpr b, Span span=Span())
and
PrimExpr hypot(PrimExpr x, PrimExpr y, Span span=Span())
Definition op.h:787
PrimExpr log1p(PrimExpr x, Span span=Span())
Definition op.h:763
void DivAmbiguityError(const TA &a)
Helper function to raise a compiler error about division ambiguity.
Definition op.h:1129
PrimExpr likely(PrimExpr cond, Span span=Span())
Mark condition as likely.
PrimExpr atan2(PrimExpr x, PrimExpr y, Span span=Span())
Definition op.h:784
PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Span span=Span())
Conditional expression.
PrimExpr bitwise_neg(PrimExpr a, Span span=Span())
take bitwise negation of two values
PrimExpr cosh(PrimExpr x, Span span=Span())
Definition op.h:767
PrimExpr logical_or(PrimExpr a, PrimExpr b, Span span=Span())
or
PrimExpr thread_return(Span span=Span())
Return from a thread.
PrimExpr atan(PrimExpr x, Span span=Span())
Definition op.h:772
Type GetType(const PrimExpr &expr)
Get the type of the expression under the unified type system.
PrimExpr isnan(PrimExpr x, Span span=Span())
Check if x is NaN.
PrimExpr exp2(PrimExpr x, Span span=Span())
Definition op.h:753
PrimExpr rsqrt(PrimExpr x, Span span=Span())
Definition op.h:759
PrimExpr operator/=(const PrimExpr &a, const TB &b)
Definition op.h:1151
PrimExpr asinh(PrimExpr x, Span span=Span())
Definition op.h:774
PrimExpr less(PrimExpr a, PrimExpr b, Span span=Span())
less
PrimExpr sin(PrimExpr x, Span span=Span())
Definition op.h:768
PrimExpr trunc(PrimExpr x, Span span=Span())
Calculate trunc(x)
PrimExpr round(PrimExpr x, Span span=Span())
Round x to the nearest integer, ties to even.
PrimExpr neg(PrimExpr a, Span span=Span())
negation.
PrimExpr ceil(PrimExpr x, Span span=Span())
Calculate ceil(x)
PrimExpr pow(PrimExpr x, PrimExpr y, Span span=Span())
Calculate power(x, y)
PrimExpr logical_not(PrimExpr a, Span span=Span())
not
PrimExpr exp10(PrimExpr x, Span span=Span())
Definition op.h:754
PrimExpr copysign(PrimExpr x, PrimExpr y, Span span=Span())
Definition op.h:786
PrimExpr bitwise_xor(PrimExpr a, PrimExpr b, Span span=Span())
take bitwise xor of two values
PrimExpr less_equal(PrimExpr a, PrimExpr b, Span span=Span())
less_equal
PrimExpr any(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
logical Or of source expression over axis
PrimExpr greater(PrimExpr a, PrimExpr b, Span span=Span())
greater
PrimExpr cast(PrimType t, PrimExpr value, Span span=Span())
cast value to type.
PrimExpr exp(PrimExpr x, Span span=Span())
Definition op.h:752
PrimExpr logaddexp(PrimExpr a, PrimExpr b, Span span=Span())
Compute log(exp(a) + exp(b)).
PrimExpr floormod(PrimExpr a, PrimExpr b, Span span=Span())
compute the remainder of floordiv
PrimExpr sub(PrimExpr a, PrimExpr b, Span span=Span())
subtraction operator
PrimExpr min_value(PrimType dtype, Span span=Span())
PrimExpr all(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
logical And of source expression over axis
PrimExpr indexdiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b) where a and b are non-negative.
PrimExpr nextafter(PrimExpr x, PrimExpr y, Span span=Span())
Definition op.h:785
PrimExpr reinterpret(PrimType t, PrimExpr value, Span span=Span())
perform reinterpret cast value to type.
PrimExpr asin(PrimExpr x, Span span=Span())
Definition op.h:770
PrimExpr prod(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
product of source expression over axis
PrimExpr sigmoid(PrimExpr x, Span span=Span())
Definition op.h:757
PrimExpr infinity(PrimType dtype, Span span=Span())
void CheckMathUnaryOpInputDType(const char *op_name, const PrimType &dtype)
Definition op.h:719
PrimExpr acos(PrimExpr x, Span span=Span())
Definition op.h:771
PrimExpr mul(PrimExpr a, PrimExpr b, Span span=Span())
multiplication operator
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
PrimExpr sum(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
sum of source expression over axis
PrimExpr floor(PrimExpr x, Span span=Span())
Calculate floor(x)
PrimExpr greater_equal(PrimExpr a, PrimExpr b, Span span=Span())
greater_equal
PrimExpr operator%(const PrimExpr &a, const TB &b)
Definition op.h:1157
PrimExpr abs(PrimExpr x, Span span=Span())
Calculate absolute value of x.
PrimExpr LargeUIntImm(PrimType value_ty, int64_t low, int64_t high, Span span=Span())
Construct a large uint constant by its low 32 bits and high 32bits.
PrimExpr atanh(PrimExpr x, Span span=Span())
Definition op.h:775
PrimExpr sqrt(PrimExpr x, Span span=Span())
Definition op.h:758
PrimExpr isinf(PrimExpr x, Span span=Span())
Check if x is infinite.
PrimExpr continue_loop(Span span=Span())
Continue current loop.
PrimExpr log2(PrimExpr x, Span span=Span())
Definition op.h:761
PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span=Span())
not_equal
PrimExpr ldexp(PrimExpr x, PrimExpr y, Span span=Span())
Definition op.h:788
PrimExpr truncdiv(PrimExpr a, PrimExpr b, Span span=Span())
compute trunc(a / b)
PrimExpr q_multiply_shift(PrimExpr x, PrimExpr y, PrimExpr q, PrimExpr s, Span span=Span())
Execute a multiplication between two Q-numbers x and y followed by a right shift s....
PrimExpr popcount(PrimExpr x, Span span=Span())
Definition op.h:764
PrimExpr bitwise_and(PrimExpr a, PrimExpr b, Span span=Span())
take bitwise and of two values
PrimExpr left_shift(PrimExpr a, PrimExpr b, Span span=Span())
left shift operator
PrimExpr sinh(PrimExpr x, Span span=Span())
Definition op.h:769
PrimExpr indexmod(PrimExpr a, PrimExpr b, Span span=Span())
compute the remainder floor(a / b) where a and b are non-negative.
PrimExpr break_loop(Span span=Span())
Break current loop.
PrimExpr add(PrimExpr a, PrimExpr b, Span span=Span())
add operator
PrimExpr log(PrimExpr x, Span span=Span())
Definition op.h:760
PrimExpr nearbyint(PrimExpr x, Span span=Span())
Round x to the nearest integer, ties to even.
PrimExpr right_shift(PrimExpr a, PrimExpr b, Span span=Span())
right shift operator
PrimExpr bitwise_or(PrimExpr a, PrimExpr b, Span span=Span())
take bitwise or of two values
Type GetTypeFromRuntimeDataType(DLDataType dtype)
Get the type corresponding to a runtime DLPack dtype.
PrimExpr clz(PrimExpr x, Span span=Span())
Definition op.h:776
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b)
PrimExpr acosh(PrimExpr x, Span span=Span())
Definition op.h:773
PrimExpr tan(PrimExpr x, Span span=Span())
Definition op.h:765
PrimExpr cos(PrimExpr x, Span span=Span())
Definition op.h:766
PrimExpr fast_erf_float_expr(PrimExpr arg, int bits)
Fast_erf_float expression from Eigen.
#define TVM_DECLARE_INTRIN_UNARY(OpName)
Definition op.h:746
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name)
Definition op.h:1067
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition op.h:1059
#define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc)
Definition op.h:1023
#define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName)
Definition op.h:749
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition op.h:1038
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name)
Definition op.h:1029
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name)
Definition op.h:1055
#define TVM_DECLARE_INTRIN_BINARY(OpName)
Definition op.h:778
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition op.h:1071
Attribute types in the Op registry for TIR ops.