tvm
op.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
27 // Acknowledgement: Most operator APIs originate from Halide.
28 #ifndef TVM_TIR_OP_H_
29 #define TVM_TIR_OP_H_
30 
31 #include <tvm/ir/expr.h>
32 #include <tvm/ir/op.h>
33 #include <tvm/ir/type.h>
34 #include <tvm/tirx/builtin.h>
35 #include <tvm/tirx/expr.h>
36 #include <tvm/tirx/stmt.h>
37 
38 #include <algorithm>
39 #include <limits>
40 #include <type_traits>
41 
42 namespace tvm {
43 
44 #define TVM_TIR_REGISTER_OP(OpName) \
45  TVM_REGISTER_OP("tirx." OpName).set_attr<TScriptPrinterName>("TScriptPrinterName", OpName)
46 
47 // Most common operators can be overloaded by argument type(PrimExpr).
48 // So we put them under the root namespace.
49 //
50 // We put more developer oriented APIs -- make_const and is_const under tirx
51 // as they are more specific to the tirx namespace.
52 
64 TVM_DLL Type GetType(const PrimExpr& expr);
65 
74 
84 
92 TVM_DLL PrimExpr ret(PrimExpr value, Span span = Span());
93 
100 TVM_DLL PrimExpr thread_return(Span span = Span());
101 
107 TVM_DLL PrimExpr continue_loop(Span span = Span());
108 
114 TVM_DLL PrimExpr break_loop(Span span = Span());
115 
122 TVM_DLL PrimExpr max_value(const DataType& dtype, Span span = Span());
123 
130 TVM_DLL PrimExpr min_value(const DataType& dtype, Span span = Span());
131 
138 TVM_DLL PrimExpr infinity(const DataType& dtype, Span span = Span());
139 
149 TVM_DLL PrimExpr cast(const DataType& t, PrimExpr value, Span span = Span());
159 TVM_DLL PrimExpr reinterpret(const DataType& t, PrimExpr value, Span span = Span());
170 TVM_DLL PrimExpr add(PrimExpr a, PrimExpr b, Span span = Span());
181 TVM_DLL PrimExpr sub(PrimExpr a, PrimExpr b, Span span = Span());
191 TVM_DLL PrimExpr neg(PrimExpr a, Span span = Span());
202 TVM_DLL PrimExpr mul(PrimExpr a, PrimExpr b, Span span = Span());
213 TVM_DLL PrimExpr left_shift(PrimExpr a, PrimExpr b, Span span = Span());
224 TVM_DLL PrimExpr right_shift(PrimExpr a, PrimExpr b, Span span = Span());
235 TVM_DLL PrimExpr greater(PrimExpr a, PrimExpr b, Span span = Span());
257 TVM_DLL PrimExpr less(PrimExpr a, PrimExpr b, Span span = Span());
268 TVM_DLL PrimExpr less_equal(PrimExpr a, PrimExpr b, Span span = Span());
279 TVM_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Span span = Span());
290 TVM_DLL PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span = Span());
300 TVM_DLL PrimExpr logical_and(PrimExpr a, PrimExpr b, Span span = Span());
310 TVM_DLL PrimExpr logical_or(PrimExpr a, PrimExpr b, Span span = Span());
319 TVM_DLL PrimExpr logical_not(PrimExpr a, Span span = Span());
334 TVM_DLL PrimExpr div(PrimExpr a, PrimExpr b, Span span = Span());
347 TVM_DLL PrimExpr truncdiv(PrimExpr a, PrimExpr b, Span span = Span());
360 TVM_DLL PrimExpr truncmod(PrimExpr a, PrimExpr b, Span span = Span());
376 TVM_DLL PrimExpr indexdiv(PrimExpr a, PrimExpr b, Span span = Span());
392 TVM_DLL PrimExpr shapediv(PrimExpr a, PrimExpr b, Span span = Span());
407 TVM_DLL PrimExpr indexmod(PrimExpr a, PrimExpr b, Span span = Span());
418 TVM_DLL PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span = Span());
427 TVM_DLL PrimExpr logaddexp(PrimExpr a, PrimExpr b, Span span = Span());
439 TVM_DLL PrimExpr ceildiv(PrimExpr a, PrimExpr b, Span span = Span());
450 TVM_DLL PrimExpr floormod(PrimExpr a, PrimExpr b, Span span = Span());
461 TVM_DLL PrimExpr max(PrimExpr a, PrimExpr b, Span span = Span());
472 TVM_DLL PrimExpr min(PrimExpr a, PrimExpr b, Span span = Span());
483 TVM_DLL PrimExpr bitwise_and(PrimExpr a, PrimExpr b, Span span = Span());
494 TVM_DLL PrimExpr bitwise_or(PrimExpr a, PrimExpr b, Span span = Span());
505 TVM_DLL PrimExpr bitwise_xor(PrimExpr a, PrimExpr b, Span span = Span());
515 TVM_DLL PrimExpr bitwise_neg(PrimExpr a, Span span = Span());
527 TVM_DLL PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value,
528  Span span = Span());
535 TVM_DLL PrimExpr likely(PrimExpr cond, Span span = Span());
542 TVM_DLL PrimExpr pow(PrimExpr x, PrimExpr y, Span span = Span());
550 TVM_DLL PrimExpr abs(PrimExpr x, Span span = Span());
557 TVM_DLL PrimExpr isnan(PrimExpr x, Span span = Span());
558 
565 TVM_DLL PrimExpr isfinite(PrimExpr x, Span span = Span());
566 
573 TVM_DLL PrimExpr isinf(PrimExpr x, Span span = Span());
574 
583 TVM_DLL PrimExpr sum(PrimExpr source, ffi::Array<tirx::IterVar> axis,
584  ffi::Array<PrimExpr> init = {}, Span span = Span());
585 
593 TVM_DLL PrimExpr all(PrimExpr source, ffi::Array<tirx::IterVar> axis,
594  ffi::Array<PrimExpr> init = {}, Span span = Span());
595 
604 TVM_DLL PrimExpr any(PrimExpr source, ffi::Array<tirx::IterVar> axis,
605  ffi::Array<PrimExpr> init = {}, Span span = Span());
606 
615 TVM_DLL PrimExpr max(PrimExpr source, ffi::Array<tirx::IterVar> axis,
616  ffi::Array<PrimExpr> init = {}, Span span = Span());
617 
626 TVM_DLL PrimExpr min(PrimExpr source, ffi::Array<tirx::IterVar> axis,
627  ffi::Array<PrimExpr> init = {}, Span span = Span());
628 
637 TVM_DLL PrimExpr prod(PrimExpr source, ffi::Array<tirx::IterVar> axis,
638  ffi::Array<PrimExpr> init = {}, Span span = Span());
639 
646 TVM_DLL PrimExpr floor(PrimExpr x, Span span = Span());
647 
654 TVM_DLL PrimExpr ceil(PrimExpr x, Span span = Span());
655 
662 TVM_DLL PrimExpr round(PrimExpr x, Span span = Span());
663 
671 TVM_DLL PrimExpr nearbyint(PrimExpr x, Span span = Span());
672 
679 TVM_DLL PrimExpr trunc(PrimExpr x, Span span = Span());
680 
689 TVM_DLL PrimExpr LargeUIntImm(DataType dtype, int64_t low, int64_t high, Span span = Span());
690 
712  Span span = Span());
713 
721 TVM_DLL PrimExpr fast_erf_float_expr(PrimExpr arg, int bits);
722 
723 inline void CheckMathUnaryOpInputDType(const char* op_name, DataType dtype) {
724  TVM_FFI_CHECK(dtype.is_float() || dtype.is_bfloat16(), TypeError)
725  << "tirx." << op_name << " only supports floating-point inputs, but got " << dtype;
726 }
727 
728 // Intrinsic operators
729 #define TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckInputDType) \
730  inline PrimExpr OpName(PrimExpr x, Span span = Span()) { \
731  static const Op& op = Op::Get("tirx." #OpName); \
732  CheckInputDType(#OpName, x.dtype()); \
733  if (x.dtype().is_bfloat16()) { \
734  DataType bf16_dtype = x.dtype(); \
735  DataType fp32_dtype(kDLFloat, 32, bf16_dtype.lanes()); \
736  PrimExpr x_fp32 = tirx::Cast(fp32_dtype, {x}, span); \
737  PrimExpr result_fp32 = tirx::Call(fp32_dtype, op, {x_fp32}, span); \
738  return tirx::Cast(bf16_dtype, {result_fp32}, span); \
739  } else { \
740  return tirx::Call(x.dtype(), op, {x}, span); \
741  } \
742  }
743 
744 #define TVM_DECLARE_INTRIN_UNARY(OpName) \
745  TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, [](const char*, DataType) {})
746 
747 #define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName) \
748  TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckMathUnaryOpInputDType)
749 
775 
776 #define TVM_DECLARE_INTRIN_BINARY(OpName) \
777  inline PrimExpr OpName(PrimExpr x, PrimExpr y, Span span = Span()) { \
778  static const Op& op = Op::Get("tirx." #OpName); \
779  return tirx::Call(x.dtype(), op, {x, y}, span); \
780  }
781 
787 
788 namespace tirx {
789 
796 inline bool IsPointerType(const Type& type, const DataType& element_type) {
797  if (!type.defined()) return false;
798  if (const auto* ptr_type = type.as<PointerTypeNode>()) {
799  if (const auto* prim_type = ptr_type->element_type.as<PrimTypeNode>()) {
800  return prim_type->dtype == element_type;
801  }
802  }
803  return false;
804 }
805 
814 template <typename ValueType,
815  typename = typename std::enable_if<std::is_pod<ValueType>::value>::type>
816 inline PrimExpr make_const(DataType t, ValueType value, Span span = Span());
823 inline PrimExpr make_zero(DataType t, Span span = Span());
830 inline PrimExpr const_true(int lanes = 1, Span span = Span()) {
831  return make_const(DataType::Bool(lanes), 1);
832 }
839 inline PrimExpr const_false(int lanes = 1, Span span = Span()) {
840  return make_const(DataType::Bool(lanes), 0);
841 }
848 inline const int64_t* as_const_int(const PrimExpr& x) {
849  if (!x.defined()) return nullptr;
850  if (const tirx::IntImmNode* op = x.as<tirx::IntImmNode>()) {
851  return &(op->value);
852  }
853 
854  return nullptr;
855 }
856 
863 inline bool is_const_int(const PrimExpr& x, int64_t value);
864 
870 inline bool is_no_op(const tirx::Stmt& stmt);
871 
878 inline bool is_one(const PrimExpr& x) { return is_const_int(x, 1); }
879 
886 inline bool is_zero(const PrimExpr& x) { return is_const_int(x, 0); }
887 
893 inline bool is_const_int(const PrimExpr& x);
894 
900 inline bool is_const_number(const PrimExpr& x);
901 
911 template <typename FReduce>
912 inline PrimExpr foldl(FReduce freduce, PrimExpr init_value, const ffi::Array<PrimExpr>& values,
913  Span span = Span()) {
914  for (PrimExpr val : values) {
915  init_value = freduce(init_value, val, span);
916  }
917  return init_value;
918 }
919 
928 TVM_DLL bool is_const_power_of_two_integer(const PrimExpr& x, int* shift);
929 
930 // Implementation details after this
931 inline bool is_const_int(const PrimExpr& x) { return as_const_int(x); }
932 
933 inline bool is_const_number(const PrimExpr& x) {
934  if (x.as<tirx::IntImmNode>()) {
935  return true;
936  } else if (x.as<tirx::FloatImmNode>()) {
937  return true;
938  } else if (const auto* op = x.as<tirx::BroadcastNode>()) {
939  return (op->value->IsInstance<tirx::IntImmNode>() ||
940  op->value->IsInstance<tirx::FloatImmNode>());
941  }
942  return false;
943 }
944 
945 inline bool is_positive_const(const PrimExpr& a) {
946  const int64_t* as_int = as_const_int(a);
947  return as_int && (*as_int > 0);
948 }
949 
950 inline bool is_negative_const(const PrimExpr& a) {
951  const int64_t* as_int = as_const_int(a);
952  return as_int && (*as_int < 0);
953 }
954 
955 inline bool is_const_int(const PrimExpr& x, int64_t value) {
956  const int64_t* as_int = as_const_int(x);
957  return as_int && (*as_int == value);
958 }
959 
960 inline bool is_no_op(const tirx::Stmt& stmt) {
961  if (!stmt.defined()) return true;
962  if (const auto* op = stmt.as<tirx::EvaluateNode>()) {
963  return is_const_int(op->value);
964  }
965  if (const auto* op = stmt.as<tirx::SeqStmtNode>()) {
966  return op->seq.size() == 0;
967  }
968  return false;
969 }
970 
971 template <typename ValueType>
972 inline PrimExpr MakeConstScalar(DataType t, ValueType value, Span span = Span()) {
973  if (t.is_int() || t.is_bool()) return IntImm(t, static_cast<int64_t>(value), span);
974  if (t.is_uint()) {
975  // Use IntImm if it is a small integer
976  uint64_t uval = static_cast<uint64_t>(value);
977  if (value < static_cast<ValueType>(0)) {
978  TVM_FFI_THROW(InternalError) << "cannot make uint from negative value " << value;
979  } else if (uval <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
980  return IntImm(t, static_cast<int64_t>(value), span);
981  } else {
982  uint64_t mask = (static_cast<uint64_t>(1) << 32U) - 1U;
983  uint64_t low = uval & mask;
984  uint64_t high = uval >> 32U;
985  return LargeUIntImm(t, static_cast<int64_t>(low), static_cast<int64_t>(high), span);
986  }
987  }
988  if (t.is_float() || t.is_bfloat16() || t.is_float8() || t.is_float6() || t.is_float4())
989  return FloatImm(t, static_cast<double>(value), span);
990  // For now, we store const scalar values of custom datatypes within doubles; later, during the
991  // datatypes lowering pass, we will lower the value to its true representation in the format
992  // specified by the datatype.
993  // TODO(gus) when do we need to start worrying about doubles not being precise enough?
994  if (static_cast<uint8_t>(t.code()) >= static_cast<uint8_t>(DataType::kCustomBegin)) {
995  return FloatImm(t, static_cast<double>(value), span);
996  }
997  TVM_FFI_THROW(InternalError) << "cannot make const for type " << t;
998  throw;
999 }
1000 
1001 template <>
1002 inline PrimExpr MakeConstScalar(DataType t, bool value, Span span) {
1003  return MakeConstScalar(t, static_cast<int>(value), span);
1004 }
1005 
1006 template <typename ValueType, typename>
1007 inline PrimExpr make_const(DataType t, ValueType value, Span span) {
1008  if (t.is_scalar()) {
1009  return MakeConstScalar(t, value, span);
1010  } else {
1011  if (t.is_fixed_length_vector()) {
1012  return tirx::Broadcast(MakeConstScalar(t.element_of(), value, span), t.lanes(), span);
1013  } else {
1014  PrimExpr lanes =
1016  return tirx::Broadcast(MakeConstScalar(t.element_of(), value, span), lanes, span);
1017  }
1018  }
1019 }
1020 
1021 inline PrimExpr make_zero(DataType t, Span span) {
1022  if (t.is_handle()) {
1023  return reinterpret(t, make_const(DataType::UInt(64), 0, span));
1024  }
1025  return make_const(t, 0, span);
1026 }
1027 
1028 } // namespace tirx
1029 
1030 // additional const expression overloading
1031 #define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc) \
1032  inline PrimExpr Name(PrimExpr& a, PrimExpr b) { \
1033  a = OpFunc(a, b); \
1034  return a; \
1035  }
1036 
1037 #define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name) \
1038  inline PrimExpr Name(const PrimExpr& a, float b) { return Name(a, PrimExpr(b)); } \
1039  inline PrimExpr Name(float a, const PrimExpr& b) { return Name(PrimExpr(a), b); } \
1040  inline PrimExpr Name(int a, const PrimExpr& b) { \
1041  return Name(tirx::make_const(b.dtype(), a), b); \
1042  } \
1043  inline PrimExpr Name(const PrimExpr& a, int b) { \
1044  return Name(a, tirx::make_const(a.dtype(), b)); \
1045  } \
1046  inline PrimExpr Name(const PrimExpr& a, double b) { \
1047  return Name(a, tirx::make_const(DataType::Float(64), b)); \
1048  }
1049 
1050 #define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1051  inline PrimExpr Name(const PrimExpr& a, float b, Span span = Span()) { \
1052  return Name(a, PrimExpr(b), span); \
1053  } \
1054  inline PrimExpr Name(float a, const PrimExpr& b, Span span = Span()) { \
1055  return Name(PrimExpr(a), b, span); \
1056  } \
1057  inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1058  return Name(tirx::make_const(b.dtype(), a), b, span); \
1059  } \
1060  inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1061  return Name(a, tirx::make_const(a.dtype(), b), span); \
1062  } \
1063  inline PrimExpr Name(const PrimExpr& a, double b, Span span = Span()) { \
1064  return Name(a, tirx::make_const(DataType::Float(64), b), span); \
1065  }
1066 
1067 #define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name) \
1068  inline PrimExpr Name(const PrimExpr& a, bool b) { return Name(a, PrimExpr(b)); } \
1069  inline PrimExpr Name(bool a, const PrimExpr& b) { return Name(PrimExpr(a), b); }
1070 
1071 #define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1072  inline PrimExpr Name(const PrimExpr& a, bool b, Span span = Span()) { \
1073  return Name(a, PrimExpr(b), span); \
1074  } \
1075  inline PrimExpr Name(bool a, const PrimExpr& b, Span span = Span()) { \
1076  return Name(PrimExpr(a), b, span); \
1077  }
1078 
1079 #define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name) \
1080  inline PrimExpr Name(const PrimExpr& a, int b) { \
1081  return Name(a, tirx::make_const(a.dtype(), b)); \
1082  } \
1083  inline PrimExpr Name(int a, const PrimExpr& b) { return Name(tirx::make_const(b.dtype(), a), b); }
1084 
1085 #define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1086  inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1087  return Name(a, tirx::make_const(a.dtype(), b), span); \
1088  } \
1089  inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1090  return Name(tirx::make_const(b.dtype(), a), b, span); \
1091  }
1092 
1093 TVM_DEFINE_ASSIGN_OP_OVERLOAD(operator+=, operator+);
1094 TVM_DEFINE_ASSIGN_OP_OVERLOAD(operator-=, operator-);
1095 TVM_DEFINE_ASSIGN_OP_OVERLOAD(operator*=, operator*);
1099 TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(operator>); // NOLINT(*)
1101 TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(operator<); // NOLINT(*)
1113 // integer related ops
1126 TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(operator>>); // NOLINT(*)
1127 TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(operator<<); // NOLINT(*)
1131 // logical ops
1136 
1142 template <typename TA>
1143 inline void DivAmbiguityError(const TA& a) {
1144  constexpr bool div_ambiguity = !std::is_class<TA>::value;
1145  static_assert(div_ambiguity,
1146  "TVM supports multiple types of integer divisions, "
1147  "please call div, indexdiv/indexmod, "
1148  "floordiv/floormod or truncdiv/truncmod directly "
1149  "to avoid ambiguity in the code. "
1150  "Checkout these functions in tirx/op.h.");
1151 }
1152 
1153 // The following code are not intended to be used in the codebase.
1154 // Instead, they generate clear compiler errors that ask developers
1155 // to use the specific division function.
1156 // The second template argument is necessary to make sure the
1157 // code compiles lazily by the compiler during invocation.
1158 template <typename TB>
1159 inline PrimExpr operator/(const PrimExpr& a, const TB& b) {
1160  DivAmbiguityError(a);
1161  return a;
1162 }
1163 
1164 template <typename TB>
1165 inline PrimExpr operator/=(const PrimExpr& a, const TB& b) {
1166  DivAmbiguityError(a);
1167  return a;
1168 }
1169 
1170 template <typename TB>
1171 inline PrimExpr operator%(const PrimExpr& a, const TB& b) {
1172  DivAmbiguityError(a);
1173  return a;
1174 }
1175 } // namespace tvm
1176 #endif // TVM_TIR_OP_H_
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:47
bool is_handle() const
Definition: data_type.h:198
bool is_uint() const
Definition: data_type.h:196
bool is_float6() const
Definition: data_type.h:159
DataType element_of() const
Get the scalar version of the type.
Definition: data_type.h:240
@ kCustomBegin
Definition: data_type.h:75
bool is_bool() const
Definition: data_type.h:143
bool is_int() const
Definition: data_type.h:194
int code() const
Definition: data_type.h:114
int lanes() const
Definition: data_type.h:120
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:387
int vscale_factor() const
Definition: data_type.h:129
bool is_fixed_length_vector() const
Definition: data_type.h:205
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:278
bool is_scalar() const
Definition: data_type.h:141
bool is_float8() const
Definition: data_type.h:151
bool is_bfloat16() const
Definition: data_type.h:192
bool is_float4() const
Definition: data_type.h:164
static DataType UInt(int bits, int lanes=1, bool is_scalable=false)
Construct an uint type.
Definition: data_type.h:286
bool is_float() const
Definition: data_type.h:147
Create a vector where all the elements are value.
Definition: expr.h:657
Managed reference to BroadcastNode.
Definition: expr.h:677
Managed reference to CallNode.
Definition: expr.h:744
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition: stmt.h:336
Managed reference to MulNode.
Definition: expr.h:169
The container of seq statement. Represent a sequence of statements.
Definition: stmt.h:311
Container of all statements.
Definition: stmt.h:65
Base expr nodes in TVM.
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:933
bool is_zero(const PrimExpr &x)
Check whether x is a constant integer 0.
Definition: op.h:886
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:796
bool is_positive_const(const PrimExpr &a)
Definition: op.h:945
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:1007
PrimExpr MakeConstScalar(DataType t, ValueType value, Span span=Span())
Definition: op.h:972
PrimExpr const_false(int lanes=1, Span span=Span())
Make a constant false expression.
Definition: op.h:839
PrimExpr make_zero(DataType t, Span span=Span())
Make a const zero expr.
Definition: op.h:1021
PrimExpr const_true(int lanes=1, Span span=Span())
Make a constant true expression.
Definition: op.h:830
bool is_negative_const(const PrimExpr &a)
Definition: op.h:950
bool is_const_int(const PrimExpr &x, int64_t value)
Check whether x is a constant integer expression.
Definition: op.h:955
bool is_one(const PrimExpr &x)
Check whether x is a constant integer 1.
Definition: op.h:878
bool is_no_op(const tirx::Stmt &stmt)
Check whether stmt is nop.
Definition: op.h:960
PrimExpr foldl(FReduce freduce, PrimExpr init_value, const ffi::Array< PrimExpr > &values, Span span=Span())
Left fold.
Definition: op.h:912
const int64_t * as_const_int(const PrimExpr &x)
Get x as constant int expression.
Definition: op.h:848
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:754
PrimExpr erf(PrimExpr x, Span span=Span())
Definition: op.h:753
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:760
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:785
PrimExpr log1p(PrimExpr x, Span span=Span())
Definition: op.h:761
void DivAmbiguityError(const TA &a)
Helper function to raise a compiler error about division ambiguity.
Definition: op.h:1143
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:782
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:765
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:770
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:751
PrimExpr rsqrt(PrimExpr x, Span span=Span())
Definition: op.h:757
PrimExpr operator/=(const PrimExpr &a, const TB &b)
Definition: op.h:1165
PrimExpr asinh(PrimExpr x, Span span=Span())
Definition: op.h:772
PrimExpr less(PrimExpr a, PrimExpr b, Span span=Span())
less
PrimExpr sin(PrimExpr x, Span span=Span())
Definition: op.h:766
PrimExpr trunc(PrimExpr x, Span span=Span())
Calculate trunc(x)
PrimExpr round(PrimExpr x, Span span=Span())
Calculate round(x)
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:752
PrimExpr copysign(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:784
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:750
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:783
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:768
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:755
PrimExpr max(const PrimExpr &a, double b, Span span=Span())
Definition: op.h:1103
PrimExpr acos(PrimExpr x, Span span=Span())
Definition: op.h:769
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:723
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:1171
PrimExpr abs(PrimExpr x, Span span=Span())
Calculate absolute value of x.
PrimExpr atanh(PrimExpr x, Span span=Span())
Definition: op.h:773
PrimExpr sqrt(PrimExpr x, Span span=Span())
Definition: op.h:756
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:759
PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span=Span())
not_equal
PrimExpr ldexp(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:786
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:762
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:767
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:758
PrimExpr nearbyint(PrimExpr x, Span span=Span())
Calculates std::nearbyint(x)
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:774
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b)
PrimExpr acosh(PrimExpr x, Span span=Span())
Definition: op.h:771
PrimExpr tan(PrimExpr x, Span span=Span())
Definition: op.h:763
PrimExpr cos(PrimExpr x, Span span=Span())
Definition: op.h:764
PrimExpr fast_erf_float_expr(PrimExpr arg, int bits)
Fast_erf_float expression from Eigen.
TIR builtin intrinsics.
TIR expressions.
#define TVM_DECLARE_INTRIN_UNARY(OpName)
Definition: op.h:744
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1079
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1071
#define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc)
Definition: op.h:1031
#define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName)
Definition: op.h:747
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1050
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1037
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1067
#define TVM_DECLARE_INTRIN_BINARY(OpName)
Definition: op.h:776
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1085
TIR statements.