28 #ifndef TVM_TIRX_OP_H_
29 #define TVM_TIRX_OP_H_
42 #include <type_traits>
46 #define TVM_TIR_REGISTER_OP(OpName) \
47 TVM_REGISTER_OP("tirx." OpName).set_attr<TScriptPrinterName>("TScriptPrinterName", OpName)
49 #define TVM_TIRX_REGISTER_OP(OpName) TVM_TIR_REGISTER_OP(OpName)
588 ffi::Array<PrimExpr> init = {}, Span span = Span());
598 ffi::Array<PrimExpr> init = {}, Span span = Span());
609 ffi::Array<PrimExpr> init = {}, Span span = Span());
620 ffi::Array<PrimExpr> init = {}, Span span = Span());
631 ffi::Array<PrimExpr> init = {}, Span span = Span());
642 ffi::Array<PrimExpr> init = {}, Span span = Span());
735 <<
"tirx." << op_name <<
" only supports floating-point inputs, but got " << dtype;
739 #define TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckInputDType) \
740 inline PrimExpr OpName(PrimExpr x, Span span = Span()) { \
741 static const Op& op = Op::Get("tirx." #OpName); \
742 CheckInputDType(#OpName, x.dtype()); \
743 if (x.dtype().is_bfloat16()) { \
744 DataType bf16_dtype = x.dtype(); \
745 DataType fp32_dtype(kDLFloat, 32, bf16_dtype.lanes()); \
746 PrimExpr x_fp32 = tirx::Cast(fp32_dtype, {x}, span); \
747 PrimExpr result_fp32 = tirx::Call(fp32_dtype, op, {x_fp32}, span); \
748 return tirx::Cast(bf16_dtype, {result_fp32}, span); \
750 return tirx::Call(x.dtype(), op, {x}, span); \
754 #define TVM_DECLARE_INTRIN_UNARY(OpName) \
755 TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, [](const char*, DataType) {})
757 #define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName) \
758 TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckMathUnaryOpInputDType)
786 #define TVM_DECLARE_INTRIN_BINARY(OpName) \
787 inline PrimExpr OpName(PrimExpr x, PrimExpr y, Span span = Span()) { \
788 static const Op& op = Op::Get("tirx." #OpName); \
789 return tirx::Call(x.dtype(), op, {x, y}, span); \
807 if (!type.defined())
return false;
809 if (
const auto* prim_type = ptr_type->element_type.as<
PrimTypeNode>()) {
810 return prim_type->dtype == element_type;
824 template <
typename ValueType,
825 typename =
typename std::enable_if<std::is_pod<ValueType>::value>::type>
859 if (!x.defined())
return nullptr;
921 template <
typename FReduce>
925 init_value = freduce(init_value, val, span);
957 return as_int && (*as_int > 0);
962 return as_int && (*as_int < 0);
967 return as_int && (*as_int == value);
971 if (!stmt.defined())
return true;
976 return op->seq.size() == 0;
981 template <
typename ValueType>
986 uint64_t uval =
static_cast<uint64_t
>(value);
987 if (value <
static_cast<ValueType
>(0)) {
988 TVM_FFI_THROW(InternalError) <<
"cannot make uint from negative value " << value;
990 return IntImm(t,
static_cast<int64_t
>(value), span);
992 uint64_t mask = (
static_cast<uint64_t
>(1) << 32U) - 1U;
993 uint64_t low = uval & mask;
994 uint64_t high = uval >> 32U;
995 return LargeUIntImm(t,
static_cast<int64_t
>(low),
static_cast<int64_t
>(high), span);
999 return FloatImm(t,
static_cast<double>(value), span);
1005 return FloatImm(t,
static_cast<double>(value), span);
1007 TVM_FFI_THROW(InternalError) <<
"cannot make const for type " << t;
1016 template <
typename ValueType,
typename>
1041 #define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc) \
1042 inline PrimExpr Name(PrimExpr& a, PrimExpr b) { \
1047 #define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name) \
1048 inline PrimExpr Name(const PrimExpr& a, float b) { return Name(a, PrimExpr(b)); } \
1049 inline PrimExpr Name(float a, const PrimExpr& b) { return Name(PrimExpr(a), b); } \
1050 inline PrimExpr Name(int a, const PrimExpr& b) { \
1051 return Name(tirx::make_const(b.dtype(), a), b); \
1053 inline PrimExpr Name(const PrimExpr& a, int b) { \
1054 return Name(a, tirx::make_const(a.dtype(), b)); \
1056 inline PrimExpr Name(const PrimExpr& a, double b) { \
1057 return Name(a, tirx::make_const(DataType::Float(64), b)); \
1060 #define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1061 inline PrimExpr Name(const PrimExpr& a, float b, Span span = Span()) { \
1062 return Name(a, PrimExpr(b), span); \
1064 inline PrimExpr Name(float a, const PrimExpr& b, Span span = Span()) { \
1065 return Name(PrimExpr(a), b, span); \
1067 inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1068 return Name(tirx::make_const(b.dtype(), a), b, span); \
1070 inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1071 return Name(a, tirx::make_const(a.dtype(), b), span); \
1073 inline PrimExpr Name(const PrimExpr& a, double b, Span span = Span()) { \
1074 return Name(a, tirx::make_const(DataType::Float(64), b), span); \
1077 #define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name) \
1078 inline PrimExpr Name(const PrimExpr& a, bool b) { return Name(a, PrimExpr(b)); } \
1079 inline PrimExpr Name(bool a, const PrimExpr& b) { return Name(PrimExpr(a), b); }
1081 #define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1082 inline PrimExpr Name(const PrimExpr& a, bool b, Span span = Span()) { \
1083 return Name(a, PrimExpr(b), span); \
1085 inline PrimExpr Name(bool a, const PrimExpr& b, Span span = Span()) { \
1086 return Name(PrimExpr(a), b, span); \
1089 #define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name) \
1090 inline PrimExpr Name(const PrimExpr& a, int b) { \
1091 return Name(a, tirx::make_const(a.dtype(), b)); \
1093 inline PrimExpr Name(int a, const PrimExpr& b) { return Name(tirx::make_const(b.dtype(), a), b); }
1095 #define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1096 inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1097 return Name(a, tirx::make_const(a.dtype(), b), span); \
1099 inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1100 return Name(tirx::make_const(b.dtype(), a), b, span); \
1152 template <
typename TA>
1154 constexpr
bool div_ambiguity = !std::is_class<TA>::value;
1155 static_assert(div_ambiguity,
1156 "TVM supports multiple types of integer divisions, "
1157 "please call div, indexdiv/indexmod, "
1158 "floordiv/floormod or truncdiv/truncmod directly "
1159 "to avoid ambiguity in the code. "
1160 "Checkout these functions in tirx/op.h.");
1168 template <
typename TB>
1174 template <
typename TB>
1180 template <
typename TB>
Constant floating point literals in the program.
Definition: expr.h:529
Managed reference class to FloatImmNode.
Definition: expr.h:546
Constant integer literals in the program.
Definition: expr.h:494
int64_t value
the Internal value.
Definition: expr.h:497
Managed reference class to IntImmNode.
Definition: expr.h:511
Low-level raw pointer type.
Definition: type.h:152
Reference to PrimExprNode.
Definition: expr.h:126
Primitive data types used in the low-level IR.
Definition: type.h:112
Definition: source_map.h:111
Managed reference to TypeNode.
Definition: type.h:99
Runtime primitive data type.
Definition: data_type.h:45
bool is_handle() const
Definition: data_type.h:196
bool is_uint() const
Definition: data_type.h:194
bool is_float6() const
Definition: data_type.h:157
DataType element_of() const
Get the scalar version of the type.
Definition: data_type.h:238
@ kCustomBegin
Definition: data_type.h:73
bool is_bool() const
Definition: data_type.h:141
bool is_int() const
Definition: data_type.h:192
int code() const
Definition: data_type.h:112
int lanes() const
Definition: data_type.h:118
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:385
int vscale_factor() const
Definition: data_type.h:127
bool is_fixed_length_vector() const
Definition: data_type.h:203
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:276
bool is_scalar() const
Definition: data_type.h:139
bool is_float8() const
Definition: data_type.h:149
bool is_bfloat16() const
Definition: data_type.h:190
bool is_float4() const
Definition: data_type.h:162
static DataType UInt(int bits, int lanes=1, bool is_scalable=false)
Construct an uint type.
Definition: data_type.h:284
bool is_float() const
Definition: data_type.h:145
Create a vector where all the elements are value.
Definition: expr.h:658
Managed reference to BroadcastNode.
Definition: expr.h:678
Managed reference to CallNode.
Definition: expr.h:745
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition: stmt.h:338
Managed reference to MulNode.
Definition: expr.h:170
The container of seq statement. Represent a sequence of statements.
Definition: stmt.h:313
Container of all statements.
Definition: stmt.h:67
Primitive operators(builtin intrinsics) and registry for them.
IR/AST nodes for the unified type system in TVM.
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:943
bool is_zero(const PrimExpr &x)
Check whether x is a constant integer 0.
Definition: op.h:896
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 IsPointerType(const Type &type, const DataType &element_type)
Check if type is a pointer to a runtime element type.
Definition: op.h:806
bool is_positive_const(const PrimExpr &a)
Definition: op.h:955
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:1017
PrimExpr MakeConstScalar(DataType t, ValueType value, Span span=Span())
Definition: op.h:982
PrimExpr const_false(int lanes=1, Span span=Span())
Make a constant false expression.
Definition: op.h:849
PrimExpr make_zero(DataType t, Span span=Span())
Make a const zero expr.
Definition: op.h:1031
PrimExpr const_true(int lanes=1, Span span=Span())
Make a constant true expression.
Definition: op.h:840
bool is_negative_const(const PrimExpr &a)
Definition: op.h:960
bool is_const_int(const PrimExpr &x, int64_t value)
Check whether x is a constant integer expression.
Definition: op.h:965
bool is_one(const PrimExpr &x)
Check whether x is a constant integer 1.
Definition: op.h:888
bool is_no_op(const tirx::Stmt &stmt)
Check whether stmt is nop.
Definition: op.h:970
PrimExpr foldl(FReduce freduce, PrimExpr init_value, const ffi::Array< PrimExpr > &values, Span span=Span())
Left fold.
Definition: op.h:922
const int64_t * as_const_int(const PrimExpr &x)
Get x as constant int expression.
Definition: op.h:858
std::function< PrimExpr(PrimExpr source, const ffi::Array< IterVar > &axis, ffi::Array< PrimExpr > init, Span span)> FReduce
The operation to use for CommReduce.
Definition: reduction.h:47
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
runtime::DataType GetRuntimeDataType(const Type &type)
Get the implied DataType for storing values with type during runtime.
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 ret(PrimExpr value, Span span=Span())
Return the value.
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
PrimExpr tanh(PrimExpr x, Span span=Span())
Definition: op.h:764
PrimExpr erf(PrimExpr x, Span span=Span())
Definition: op.h:763
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:770
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 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:795
PrimExpr log1p(PrimExpr x, Span span=Span())
Definition: op.h:771
void DivAmbiguityError(const TA &a)
Helper function to raise a compiler error about division ambiguity.
Definition: op.h:1153
PrimExpr likely(PrimExpr cond, Span span=Span())
Mark condition as likely.
PrimExpr reinterpret(const DataType &t, PrimExpr value, Span span=Span())
perform reinterpret cast value to type.
PrimExpr atan2(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:792
PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Span span=Span())
Conditional expression.
PrimExpr min_value(const DataType &dtype, Span span=Span())
PrimExpr bitwise_neg(PrimExpr a, Span span=Span())
take bitwise negation of two values
PrimExpr cosh(PrimExpr x, Span span=Span())
Definition: op.h:775
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:780
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 cast(const DataType &t, PrimExpr value, Span span=Span())
cast value to type.
PrimExpr max_value(const DataType &dtype, Span span=Span())
PrimExpr exp2(PrimExpr x, Span span=Span())
Definition: op.h:761
PrimExpr rsqrt(PrimExpr x, Span span=Span())
Definition: op.h:767
PrimExpr operator/=(const PrimExpr &a, const TB &b)
Definition: op.h:1175
PrimExpr asinh(PrimExpr x, Span span=Span())
Definition: op.h:782
PrimExpr less(PrimExpr a, PrimExpr b, Span span=Span())
less
PrimExpr sin(PrimExpr x, Span span=Span())
Definition: op.h:776
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.
Type GetTypeFromRuntimeDataType(const DataType &dtype)
Get the type corresponding to DataType.
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:762
PrimExpr copysign(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:794
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 exp(PrimExpr x, Span span=Span())
Definition: op.h:760
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 infinity(const DataType &dtype, Span span=Span())
PrimExpr sub(PrimExpr a, PrimExpr b, Span span=Span())
subtraction operator
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:793
PrimExpr LargeUIntImm(DataType dtype, int64_t low, int64_t high, Span span=Span())
Construct a large uint constant by its low 32 bits and high 32bits.
PrimExpr asin(PrimExpr x, Span span=Span())
Definition: op.h:778
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:765
PrimExpr max(const PrimExpr &a, double b, Span span=Span())
Definition: op.h:1113
PrimExpr acos(PrimExpr x, Span span=Span())
Definition: op.h:779
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
void CheckMathUnaryOpInputDType(const char *op_name, DataType dtype)
Definition: op.h:733
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:1181
PrimExpr abs(PrimExpr x, Span span=Span())
Calculate absolute value of x.
PrimExpr atanh(PrimExpr x, Span span=Span())
Definition: op.h:783
PrimExpr sqrt(PrimExpr x, Span span=Span())
Definition: op.h:766
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:769
PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span=Span())
not_equal
PrimExpr ldexp(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:796
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:772
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:777
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:768
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
PrimExpr clz(PrimExpr x, Span span=Span())
Definition: op.h:784
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b)
PrimExpr acosh(PrimExpr x, Span span=Span())
Definition: op.h:781
PrimExpr tan(PrimExpr x, Span span=Span())
Definition: op.h:773
PrimExpr cos(PrimExpr x, Span span=Span())
Definition: op.h:774
PrimExpr fast_erf_float_expr(PrimExpr arg, int bits)
Fast_erf_float expression from Eigen.
#define TVM_DECLARE_INTRIN_UNARY(OpName)
Definition: op.h:754
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1089
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1081
#define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc)
Definition: op.h:1041
#define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName)
Definition: op.h:757
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1060
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1047
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1077
#define TVM_DECLARE_INTRIN_BINARY(OpName)
Definition: op.h:786
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1095