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_TIRX_OP_H_
29 #define TVM_TIRX_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/op_attr_types.h>
37 #include <tvm/tirx/stmt.h>
38 
39 #include <algorithm>
40 #include <limits>
41 #include <type_traits>
42 #include <utility>
43 
44 namespace tvm {
45 
46 #define TVM_TIR_REGISTER_OP(OpName) \
47  TVM_REGISTER_OP("tirx." OpName) \
48  .set_attr<TScriptPrinterName>("TScriptPrinterName", OpName) \
49  .set_attr<TIRxOpCategory>("TIRxOpCategory", ffi::String("builtin"), /*plevel=*/1)
50 
51 #define TVM_TIRX_REGISTER_OP(OpName) TVM_TIR_REGISTER_OP(OpName)
52 
53 // Most common operators can be overloaded by argument type(PrimExpr).
54 // So we put them under the root namespace.
55 //
56 // We put more developer oriented APIs -- MakeConst and is_const under tirx
57 // as they are more specific to the tirx namespace.
58 
70 TVM_DLL Type GetType(const PrimExpr& expr);
71 
79 TVM_DLL Type GetTypeFromRuntimeDataType(DLDataType dtype);
80 
88 TVM_DLL PrimExpr ret(PrimExpr value, Span span = Span());
89 TVM_DLL Expr ret(Expr value, Span span = Span());
90 
97 TVM_DLL PrimExpr thread_return(Span span = Span());
98 
104 TVM_DLL PrimExpr continue_loop(Span span = Span());
105 
111 TVM_DLL PrimExpr break_loop(Span span = Span());
112 
119 TVM_DLL PrimExpr max_value(PrimType dtype, Span span = Span());
120 
127 TVM_DLL PrimExpr min_value(PrimType dtype, Span span = Span());
128 
135 TVM_DLL PrimExpr infinity(PrimType dtype, Span span = Span());
136 
146 TVM_DLL PrimExpr cast(PrimType t, PrimExpr value, Span span = Span());
156 TVM_DLL PrimExpr reinterpret(PrimType t, PrimExpr value, Span span = Span());
158 TVM_DLL Expr reinterpret(Type target_ty, Expr value, Span span = Span());
169 TVM_DLL PrimExpr add(PrimExpr a, PrimExpr b, Span span = Span());
180 TVM_DLL PrimExpr sub(PrimExpr a, PrimExpr b, Span span = Span());
190 TVM_DLL PrimExpr neg(PrimExpr a, Span span = Span());
201 TVM_DLL PrimExpr mul(PrimExpr a, PrimExpr b, Span span = Span());
212 TVM_DLL PrimExpr left_shift(PrimExpr a, PrimExpr b, Span span = Span());
223 TVM_DLL PrimExpr right_shift(PrimExpr a, PrimExpr b, Span span = Span());
234 TVM_DLL PrimExpr greater(PrimExpr a, PrimExpr b, Span span = Span());
256 TVM_DLL PrimExpr less(PrimExpr a, PrimExpr b, Span span = Span());
267 TVM_DLL PrimExpr less_equal(PrimExpr a, PrimExpr b, Span span = Span());
278 TVM_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Span span = Span());
289 TVM_DLL PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span = Span());
299 TVM_DLL PrimExpr logical_and(PrimExpr a, PrimExpr b, Span span = Span());
309 TVM_DLL PrimExpr logical_or(PrimExpr a, PrimExpr b, Span span = Span());
318 TVM_DLL PrimExpr logical_not(PrimExpr a, Span span = Span());
333 TVM_DLL PrimExpr div(PrimExpr a, PrimExpr b, Span span = Span());
346 TVM_DLL PrimExpr truncdiv(PrimExpr a, PrimExpr b, Span span = Span());
359 TVM_DLL PrimExpr truncmod(PrimExpr a, PrimExpr b, Span span = Span());
375 TVM_DLL PrimExpr indexdiv(PrimExpr a, PrimExpr b, Span span = Span());
391 TVM_DLL PrimExpr shapediv(PrimExpr a, PrimExpr b, Span span = Span());
406 TVM_DLL PrimExpr indexmod(PrimExpr a, PrimExpr b, Span span = Span());
417 TVM_DLL PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span = Span());
426 TVM_DLL PrimExpr logaddexp(PrimExpr a, PrimExpr b, Span span = Span());
438 TVM_DLL PrimExpr ceildiv(PrimExpr a, PrimExpr b, Span span = Span());
449 TVM_DLL PrimExpr floormod(PrimExpr a, PrimExpr b, Span span = Span());
460 TVM_DLL PrimExpr max(PrimExpr a, PrimExpr b, Span span = Span());
471 TVM_DLL PrimExpr min(PrimExpr a, PrimExpr b, Span span = Span());
482 TVM_DLL PrimExpr bitwise_and(PrimExpr a, PrimExpr b, Span span = Span());
493 TVM_DLL PrimExpr bitwise_or(PrimExpr a, PrimExpr b, Span span = Span());
504 TVM_DLL PrimExpr bitwise_xor(PrimExpr a, PrimExpr b, Span span = Span());
514 TVM_DLL PrimExpr bitwise_neg(PrimExpr a, Span span = Span());
526 TVM_DLL PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value,
527  Span span = Span());
534 TVM_DLL PrimExpr likely(PrimExpr cond, Span span = Span());
541 TVM_DLL PrimExpr pow(PrimExpr x, PrimExpr y, Span span = Span());
549 TVM_DLL PrimExpr abs(PrimExpr x, Span span = Span());
556 TVM_DLL PrimExpr isnan(PrimExpr x, Span span = Span());
557 
564 TVM_DLL PrimExpr isfinite(PrimExpr x, Span span = Span());
565 
572 TVM_DLL PrimExpr isinf(PrimExpr x, Span span = Span());
573 
582 TVM_DLL PrimExpr sum(PrimExpr source, ffi::Array<tirx::IterVar> axis,
583  ffi::Array<PrimExpr> init = {}, Span span = Span());
584 
592 TVM_DLL PrimExpr all(PrimExpr source, ffi::Array<tirx::IterVar> axis,
593  ffi::Array<PrimExpr> init = {}, Span span = Span());
594 
603 TVM_DLL PrimExpr any(PrimExpr source, ffi::Array<tirx::IterVar> axis,
604  ffi::Array<PrimExpr> init = {}, Span span = Span());
605 
614 TVM_DLL PrimExpr max(PrimExpr source, ffi::Array<tirx::IterVar> axis,
615  ffi::Array<PrimExpr> init = {}, Span span = Span());
616 
625 TVM_DLL PrimExpr min(PrimExpr source, ffi::Array<tirx::IterVar> axis,
626  ffi::Array<PrimExpr> init = {}, Span span = Span());
627 
636 TVM_DLL PrimExpr prod(PrimExpr source, ffi::Array<tirx::IterVar> axis,
637  ffi::Array<PrimExpr> init = {}, Span span = Span());
638 
645 TVM_DLL PrimExpr floor(PrimExpr x, Span span = Span());
646 
653 TVM_DLL PrimExpr ceil(PrimExpr x, Span span = Span());
654 
665 TVM_DLL PrimExpr round(PrimExpr x, Span span = Span());
666 
676 TVM_DLL PrimExpr nearbyint(PrimExpr x, Span span = Span());
677 
684 TVM_DLL PrimExpr trunc(PrimExpr x, Span span = Span());
685 
694 TVM_DLL PrimExpr LargeUIntImm(PrimType value_ty, int64_t low, int64_t high, Span span = Span());
695 
717  Span span = Span());
718 
726 TVM_DLL PrimExpr fast_erf_float_expr(PrimExpr arg, int bits);
727 
728 inline void CheckMathUnaryOpInputDType(const char* op_name, const PrimType& dtype) {
729  TVM_FFI_CHECK(dtype.code() == DLDataTypeCode::kDLFloat ||
730  dtype.MatchesElementType(DLDataTypeCode::kDLBfloat, 16),
731  TypeError)
732  << "tirx." << op_name << " only supports floating-point inputs, but got " << dtype;
733 }
734 
735 // Intrinsic operators
736 #define TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckInputDType) \
737  inline PrimExpr OpName(PrimExpr x, Span span = Span()) { \
738  static const Op op = Op::Get("tirx." #OpName); \
739  PrimType x_ty = x.ty(); \
740  CheckInputDType(#OpName, x_ty); \
741  if (x_ty.MatchesElementType(DLDataTypeCode::kDLBfloat, 16)) { \
742  PrimType bf16_ty = x_ty; \
743  PrimType f32_ty = \
744  x_ty.IsScalableVector() \
745  ? PrimType::ScalableVector(DLDataTypeCode::kDLFloat, 32, x_ty.VScaleFactor()) \
746  : PrimType::Float(32, x_ty.lanes()); \
747  PrimExpr x_fp32 = tirx::Cast(f32_ty, x, span); \
748  PrimExpr result_fp32 = Call(f32_ty, op, {x_fp32}, {}, {}, span).as_or_throw<PrimExpr>(); \
749  return tirx::Cast(bf16_ty, result_fp32, span); \
750  } else { \
751  return Call(x_ty, op, {x}, {}, {}, span).as_or_throw<PrimExpr>(); \
752  } \
753  }
754 
755 #define TVM_DECLARE_INTRIN_UNARY(OpName) \
756  TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, [](const char*, const PrimType&) {})
757 
758 #define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName) \
759  TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckMathUnaryOpInputDType)
760 
786 
787 #define TVM_DECLARE_INTRIN_BINARY(OpName) \
788  inline PrimExpr OpName(PrimExpr x, PrimExpr y, Span span = Span()) { \
789  static const Op op = Op::Get("tirx." #OpName); \
790  return Call(x.ty(), op, {x, y}, {}, {}, span).as_or_throw<PrimExpr>(); \
791  }
792 
798 
799 namespace tirx {
800 
807 inline bool IsPointerType(const Type& type, DLDataType element_type) {
808  if (type.IsMissing()) return false;
809  if (const auto* ptr_type = type.as<PointerTypeNode>()) {
810  if (const auto* prim_type = ptr_type->element_type.as<PrimTypeNode>()) {
811  return prim_type->dtype == element_type;
812  }
813  }
814  return false;
815 }
816 
832 template <typename ValueType,
833  typename = typename std::enable_if<std::is_standard_layout<ValueType>::value &&
834  std::is_trivial<ValueType>::value>::type>
835 inline PrimExpr MakeConst(PrimType dtype, ValueType value, Span span = Span());
842 inline Expr ConstHandle(int64_t value, Span span = Span());
849 inline const int64_t* as_const_int(const PrimExpr& x) {
850  if (!x.defined()) return nullptr;
851  if (const tirx::IntImmNode* op = x.as<tirx::IntImmNode>()) {
852  return &(op->value);
853  }
854 
855  return nullptr;
856 }
857 
864 inline bool is_const_int(const PrimExpr& x, int64_t value);
865 
871 inline bool is_no_op(const tirx::Stmt& stmt);
872 
879 inline bool is_one(const PrimExpr& x) { return is_const_int(x, 1); }
880 
887 inline bool is_zero(const PrimExpr& x) { return is_const_int(x, 0); }
888 
894 inline bool is_const_int(const PrimExpr& x);
895 
901 inline bool is_const_number(const PrimExpr& x);
902 
912 template <typename FReduce>
913 inline PrimExpr foldl(FReduce freduce, PrimExpr init_value, const ffi::Array<PrimExpr>& values,
914  Span span = Span()) {
915  for (PrimExpr val : values) {
916  init_value = freduce(init_value, val, span);
917  }
918  return init_value;
919 }
920 
929 TVM_DLL bool is_const_power_of_two_integer(const PrimExpr& x, int* shift);
930 
931 // Implementation details after this
932 inline bool is_const_int(const PrimExpr& x) { return as_const_int(x); }
933 
934 inline bool is_const_number(const PrimExpr& x) {
935  if (x.as<tirx::IntImmNode>()) {
936  return true;
937  } else if (x.as<tirx::FloatImmNode>()) {
938  return true;
939  } else if (const auto* op = x.as<tirx::BroadcastNode>()) {
940  return (op->value->IsInstance<tirx::IntImmNode>() ||
941  op->value->IsInstance<tirx::FloatImmNode>());
942  }
943  return false;
944 }
945 
946 inline bool is_positive_const(const PrimExpr& a) {
947  const int64_t* as_int = as_const_int(a);
948  return as_int && (*as_int > 0);
949 }
950 
951 inline bool is_negative_const(const PrimExpr& a) {
952  const int64_t* as_int = as_const_int(a);
953  return as_int && (*as_int < 0);
954 }
955 
956 inline bool is_const_int(const PrimExpr& x, int64_t value) {
957  const int64_t* as_int = as_const_int(x);
958  return as_int && (*as_int == value);
959 }
960 
961 inline bool is_no_op(const tirx::Stmt& stmt) {
962  if (!stmt.defined()) return true;
963  if (const auto* op = stmt.as<tirx::EvaluateNode>()) {
964  auto value = op->value.as<PrimExpr>();
965  return value && is_const_int(value.value());
966  }
967  if (const auto* op = stmt.as<tirx::SeqStmtNode>()) {
968  return op->seq.size() == 0;
969  }
970  return false;
971 }
972 
973 template <typename ValueType>
974 inline PrimExpr MakeConstScalar(PrimType dtype, ValueType value, Span span = Span()) {
975  DLDataTypeCode code = dtype.code();
976  if (code == DLDataTypeCode::kDLInt || code == DLDataTypeCode::kDLBool) {
977  return IntImm(dtype, static_cast<int64_t>(value), span);
978  }
979  if (code == DLDataTypeCode::kDLUInt) {
980  // Use IntImm if it is a small integer
981  uint64_t uval = static_cast<uint64_t>(value);
982  if (value < static_cast<ValueType>(0)) {
983  TVM_FFI_THROW(InternalError) << "cannot make uint from negative value " << value;
984  } else if (uval <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
985  return IntImm(dtype, static_cast<int64_t>(value), span);
986  } else {
987  uint64_t mask = (static_cast<uint64_t>(1) << 32U) - 1U;
988  uint64_t low = uval & mask;
989  uint64_t high = uval >> 32U;
990  return LargeUIntImm(dtype, static_cast<int64_t>(low), static_cast<int64_t>(high), span);
991  }
992  }
993  if (dtype.MatchesCode(DLDataTypeCode::kDLFloat, DLDataTypeCode::kDLFloat8_e3m4,
994  DLDataTypeCode::kDLFloat8_e4m3, DLDataTypeCode::kDLFloat8_e4m3b11fnuz,
995  DLDataTypeCode::kDLFloat8_e4m3fn, DLDataTypeCode::kDLFloat8_e4m3fnuz,
996  DLDataTypeCode::kDLFloat8_e5m2, DLDataTypeCode::kDLFloat8_e5m2fnuz,
997  DLDataTypeCode::kDLFloat8_e8m0fnu, DLDataTypeCode::kDLFloat6_e2m3fn,
998  DLDataTypeCode::kDLFloat6_e3m2fn, DLDataTypeCode::kDLFloat4_e2m1fn) ||
999  dtype.MatchesElementType(DLDataTypeCode::kDLBfloat, 16)) {
1000  return FloatImm(dtype, static_cast<double>(value), span);
1001  }
1002  TVM_FFI_THROW(InternalError) << "cannot make const for type " << dtype;
1003  throw;
1004 }
1005 
1006 template <>
1007 inline PrimExpr MakeConstScalar(PrimType dtype, bool value, Span span) {
1008  return MakeConstScalar(dtype, static_cast<int>(value), span);
1009 }
1010 
1011 template <typename ValueType, typename>
1012 inline PrimExpr MakeConst(PrimType dtype, ValueType value, Span span) {
1013  if (!dtype.IsScalableVector() && !dtype.IsFixedLengthVector()) {
1014  return MakeConstScalar(dtype, value, span);
1015  }
1016  PrimType elem_ty = dtype.WithLanes(1);
1017  if (dtype.IsFixedLengthVector()) {
1018  return tirx::Broadcast(MakeConstScalar(elem_ty, value, span), dtype.lanes(), span);
1019  }
1020  PrimExpr lanes =
1021  tirx::Mul(Call(PrimType::Int(32), tirx::builtin::vscale(), {}).as_or_throw<PrimExpr>(),
1022  dtype.VScaleFactor());
1023  return tirx::Broadcast(MakeConstScalar(elem_ty, value, span), lanes, span);
1024 }
1025 
1026 inline Expr ConstHandle(int64_t value, Span span) {
1027  return reinterpret(PointerType::VoidPointerTy(), IntImm(PrimType::UInt(64), value, span), span);
1028 }
1029 
1030 } // namespace tirx
1031 
1032 // additional const expression overloading
1033 #define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc) \
1034  inline PrimExpr Name(PrimExpr& a, PrimExpr b) { \
1035  a = OpFunc(a, b); \
1036  return a; \
1037  }
1038 
1039 #define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name) \
1040  inline PrimExpr Name(const PrimExpr& a, float b) { return Name(a, PrimExpr(b)); } \
1041  inline PrimExpr Name(float a, const PrimExpr& b) { return Name(PrimExpr(a), b); } \
1042  inline PrimExpr Name(int a, const PrimExpr& b) { return Name(tirx::MakeConst(b.ty(), a), b); } \
1043  inline PrimExpr Name(const PrimExpr& a, int b) { return Name(a, tirx::MakeConst(a.ty(), b)); } \
1044  inline PrimExpr Name(const PrimExpr& a, double b) { \
1045  return Name(a, FloatImm(PrimType::Float(64), b)); \
1046  }
1047 
1048 #define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1049  inline PrimExpr Name(const PrimExpr& a, float b, Span span = Span()) { \
1050  return Name(a, PrimExpr(b), span); \
1051  } \
1052  inline PrimExpr Name(float a, const PrimExpr& b, Span span = Span()) { \
1053  return Name(PrimExpr(a), b, span); \
1054  } \
1055  inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1056  return Name(tirx::MakeConst(b.ty(), a), b, span); \
1057  } \
1058  inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1059  return Name(a, tirx::MakeConst(a.ty(), b), span); \
1060  } \
1061  inline PrimExpr Name(const PrimExpr& a, double b, Span span = Span()) { \
1062  return Name(a, FloatImm(PrimType::Float(64), b), span); \
1063  }
1064 
1065 #define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name) \
1066  inline PrimExpr Name(const PrimExpr& a, bool b) { return Name(a, PrimExpr(b)); } \
1067  inline PrimExpr Name(bool a, const PrimExpr& b) { return Name(PrimExpr(a), b); }
1068 
1069 #define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1070  inline PrimExpr Name(const PrimExpr& a, bool b, Span span = Span()) { \
1071  return Name(a, PrimExpr(b), span); \
1072  } \
1073  inline PrimExpr Name(bool a, const PrimExpr& b, Span span = Span()) { \
1074  return Name(PrimExpr(a), b, span); \
1075  }
1076 
1077 #define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name) \
1078  inline PrimExpr Name(const PrimExpr& a, int b) { return Name(a, tirx::MakeConst(a.ty(), b)); } \
1079  inline PrimExpr Name(int a, const PrimExpr& b) { return Name(tirx::MakeConst(b.ty(), a), b); }
1080 
1081 #define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name) \
1082  inline PrimExpr Name(const PrimExpr& a, int b, Span span = Span()) { \
1083  return Name(a, tirx::MakeConst(a.ty(), b), span); \
1084  } \
1085  inline PrimExpr Name(int a, const PrimExpr& b, Span span = Span()) { \
1086  return Name(tirx::MakeConst(b.ty(), a), b, span); \
1087  }
1088 
1089 TVM_DEFINE_ASSIGN_OP_OVERLOAD(operator+=, operator+);
1090 TVM_DEFINE_ASSIGN_OP_OVERLOAD(operator-=, operator-);
1091 TVM_DEFINE_ASSIGN_OP_OVERLOAD(operator*=, operator*);
1095 TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(operator>); // NOLINT(*)
1097 TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(operator<); // NOLINT(*)
1109 // integer related ops
1122 TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(operator>>); // NOLINT(*)
1123 TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(operator<<); // NOLINT(*)
1127 // logical ops
1132 
1138 template <typename TA>
1139 inline void DivAmbiguityError(const TA& a) {
1140  constexpr bool div_ambiguity = !std::is_class<TA>::value;
1141  static_assert(div_ambiguity,
1142  "TVM supports multiple types of integer divisions, "
1143  "please call div, indexdiv/indexmod, "
1144  "floordiv/floormod or truncdiv/truncmod directly "
1145  "to avoid ambiguity in the code. "
1146  "Checkout these functions in tirx/op.h.");
1147 }
1148 
1149 // The following code are not intended to be used in the codebase.
1150 // Instead, they generate clear compiler errors that ask developers
1151 // to use the specific division function.
1152 // The second template argument is necessary to make sure the
1153 // code compiles lazily by the compiler during invocation.
1154 template <typename TB>
1155 inline PrimExpr operator/(const PrimExpr& a, const TB& b) {
1156  DivAmbiguityError(a);
1157  return a;
1158 }
1159 
1160 template <typename TB>
1161 inline PrimExpr operator/=(const PrimExpr& a, const TB& b) {
1162  DivAmbiguityError(a);
1163  return a;
1164 }
1165 
1166 template <typename TB>
1167 inline PrimExpr operator%(const PrimExpr& a, const TB& b) {
1168  DivAmbiguityError(a);
1169  return a;
1170 }
1171 } // namespace tvm
1172 #endif // TVM_TIR_OP_H_
Managed reference to CallNode.
Definition: expr.h:348
Managed reference to ExprNode.
Definition: base_expr.h:311
Constant floating point literals in the program.
Definition: expr.h:424
Managed reference class to FloatImmNode.
Definition: expr.h:441
Constant integer literals in the program.
Definition: expr.h:361
int64_t value
the Internal value.
Definition: expr.h:364
Managed reference class to IntImmNode.
Definition: expr.h:378
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:354
Primitive data types used in the low-level IR.
Definition: base_expr.h:95
Definition: base_expr.h:113
TVM_FFI_INLINE DLDataTypeCode code() const
Definition: base_expr.h:150
TVM_FFI_INLINE PrimType WithLanes(int lanes) const
Return the same scalar element type with a fixed lane count.
Definition: base_expr.h:250
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:203
TVM_FFI_INLINE bool IsFixedLengthVector() const
Whether this type is a fixed-length vector.
Definition: base_expr.h:208
TVM_FFI_INLINE bool MatchesCode(Codes... codes) const
Check whether the dtype code matches any of the provided DLPack codes.
Definition: base_expr.h:184
TVM_FFI_INLINE bool MatchesElementType(DLDataTypeCode code, int bits) const
Check the scalar element code and bit width.
Definition: base_expr.h:174
TVM_FFI_INLINE int32_t VScaleFactor() const
Definition: base_expr.h:255
TVM_FFI_INLINE int32_t lanes() const
Definition: base_expr.h:161
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
bool IsMissing() const
Create a vector where all the elements are value.
Definition: expr.h:670
Managed reference to BroadcastNode.
Definition: expr.h:689
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition: stmt.h:337
Managed reference to MulNode.
Definition: expr.h:171
The container of seq statement. Represent a sequence of statements.
Definition: stmt.h:312
Container of all statements.
Definition: stmt.h:64
Base expr nodes in TVM.
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:934
bool is_zero(const PrimExpr &x)
Check whether x is a constant integer 0.
Definition: op.h:887
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:946
Expr ConstHandle(int64_t value, Span span=Span())
Make a constant opaque-pointer value.
Definition: op.h:1026
bool is_negative_const(const PrimExpr &a)
Definition: op.h:951
bool is_const_int(const PrimExpr &x, int64_t value)
Check whether x is a constant integer expression.
Definition: op.h:956
bool is_one(const PrimExpr &x)
Check whether x is a constant integer 1.
Definition: op.h:879
bool is_no_op(const tirx::Stmt &stmt)
Check whether stmt is nop.
Definition: op.h:961
PrimExpr foldl(FReduce freduce, PrimExpr init_value, const ffi::Array< PrimExpr > &values, Span span=Span())
Left fold.
Definition: op.h:913
PrimExpr MakeConstScalar(PrimType dtype, ValueType value, Span span=Span())
Definition: op.h:974
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:1012
bool IsPointerType(const Type &type, DLDataType element_type)
Check if type is a pointer to a runtime element type.
Definition: op.h:807
const int64_t * as_const_int(const PrimExpr &x)
Get x as constant int expression.
Definition: op.h:849
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: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 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:765
PrimExpr erf(PrimExpr x, Span span=Span())
Definition: op.h:764
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:771
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:796
PrimExpr log1p(PrimExpr x, Span span=Span())
Definition: op.h:772
void DivAmbiguityError(const TA &a)
Helper function to raise a compiler error about division ambiguity.
Definition: op.h:1139
PrimExpr likely(PrimExpr cond, Span span=Span())
Mark condition as likely.
PrimExpr atan2(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:793
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:776
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:781
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:762
PrimExpr rsqrt(PrimExpr x, Span span=Span())
Definition: op.h:768
PrimExpr operator/=(const PrimExpr &a, const TB &b)
Definition: op.h:1161
PrimExpr asinh(PrimExpr x, Span span=Span())
Definition: op.h:783
PrimExpr less(PrimExpr a, PrimExpr b, Span span=Span())
less
PrimExpr sin(PrimExpr x, Span span=Span())
Definition: op.h:777
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:763
PrimExpr copysign(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:795
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:761
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:794
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:779
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:766
PrimExpr max(const PrimExpr &a, double b, Span span=Span())
Definition: op.h:1099
PrimExpr infinity(PrimType dtype, Span span=Span())
void CheckMathUnaryOpInputDType(const char *op_name, const PrimType &dtype)
Definition: op.h:728
PrimExpr acos(PrimExpr x, Span span=Span())
Definition: op.h:780
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:1167
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:784
PrimExpr sqrt(PrimExpr x, Span span=Span())
Definition: op.h:767
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:770
PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span=Span())
not_equal
PrimExpr ldexp(PrimExpr x, PrimExpr y, Span span=Span())
Definition: op.h:797
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:773
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:778
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:769
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:785
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b)
PrimExpr acosh(PrimExpr x, Span span=Span())
Definition: op.h:782
PrimExpr tan(PrimExpr x, Span span=Span())
Definition: op.h:774
PrimExpr cos(PrimExpr x, Span span=Span())
Definition: op.h:775
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:755
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1077
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1069
#define TVM_DEFINE_ASSIGN_OP_OVERLOAD(Name, OpFunc)
Definition: op.h:1033
#define TVM_DECLARE_FLOAT_INTRIN_UNARY(OpName)
Definition: op.h:758
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1048
#define TVM_DEFINE_BINOP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1039
#define TVM_DEFINE_LOGICAL_OP_CONST_VAL_OVERLOAD(Name)
Definition: op.h:1065
#define TVM_DECLARE_INTRIN_BINARY(OpName)
Definition: op.h:787
#define TVM_DEFINE_INT_OP_CONST_VAL_OVERLOAD_SPANNED(Name)
Definition: op.h:1081
Attribute types in the Op registry for TIR ops.
TIR statements.