24#ifndef TVM_IR_BASE_EXPR_H_
25#define TVM_IR_BASE_EXPR_H_
27#include <tvm/ffi/cast.h>
28#include <tvm/ffi/dtype.h>
29#include <tvm/ffi/reflection/registry.h>
30#include <tvm/ffi/string.h>
61 namespace refl = tvm::ffi::reflection;
63 refl::ObjectDef<TypeNode>().def_ro(
"span", &
TypeNode::span, refl::DefaultValue(
Span()),
64 refl::AttachFieldFlag::SEqHashIgnore());
77class Type :
public ffi::ObjectRef {
97 namespace refl = tvm::ffi::reflection;
98 refl::ObjectDef<OpaqueTypeNode>();
127 namespace refl = tvm::ffi::reflection;
175 return static_cast<DLDataTypeCode>(
static_cast<int>(get()->dtype.code));
189 <<
"Can't fetch the lanes of a scalable vector at a compile time.";
207 template <
typename...
Codes>
222 return dtype.code ==
static_cast<uint8_t>(DLDataTypeCode::kDLOpaqueHandle) && dtype.bits == 0 &&
223 static_cast<int16_t>(dtype.lanes) == 0;
228 return static_cast<int16_t>(get()->dtype.lanes) < -1;
233 return static_cast<int16_t>(get()->dtype.lanes) > 1;
247 <<
"Cannot compute compile-time storage bytes for non-fixed vector type " << dtype;
249 return static_cast<size_t>(
250 (
static_cast<uint64_t>(dtype.bits) *
static_cast<uint64_t>(dtype.lanes) + 7) / 8);
291 return lhs->dtype == rhs->dtype;
317 namespace refl = tvm::ffi::reflection;
319 refl::ObjectDef<ExprNode>()
321 refl::AttachFieldFlag::SEqHashIgnore())
335class Expr :
public ffi::ObjectRef {
355 namespace refl = tvm::ffi::reflection;
356 refl::ObjectDef<OpaqueExprNode>();
376template <
typename ExpectedType>
381 const auto* node = get();
385 return ffi::GetRef<ExpectedType>(
ty_node);
460struct TypeTraits<
PrimType> :
public ObjectRefWithFallbackTraitsBase<PrimType, DLDataType> {
464template <
typename ExpectedType>
467template <
typename ExpectedType>
469 :
public ObjectRefTypeTraitsBase<TypedExpr<ExpectedType>> {
471 using Base::CopyFromAnyViewAfterCheck;
472 using Base::CopyToAnyView;
473 using Base::GetMismatchTypeInfo;
474 using Base::MoveFromAnyAfterCheck;
475 using Base::MoveToAny;
476 using Base::TypeSchema;
480 if (src->type_index == TypeIndex::kTVMFFINone) {
483 if (src->type_index < TypeIndex::kTVMFFIStaticObjectBegin ||
484 !details::IsObjectInstance<ExprNode>(src->type_index)) {
487 const auto* expr =
static_cast<const ExprNode*
>(
488 details::ObjectUnsafe::ObjectPtrFromUnowned<Object>(src->v_obj).get());
489 return details::AnyUnsafe::CheckAnyStrict<ExpectedType>(expr->ty);
494 if (CheckAnyStrict(src)) {
495 if (src->type_index == TypeIndex::kTVMFFINone) {
496 return details::ObjectUnsafe::ObjectRefFromObjectPtr<TypedExpr<ExpectedType>>(
nullptr);
498 return details::ObjectUnsafe::ObjectRefFromObjectPtr<TypedExpr<ExpectedType>>(
499 details::ObjectUnsafe::ObjectPtrFromUnowned<ExprNode>(src->v_obj));
510 :
public ObjectRefWithFallbackTraitsBase<ObjectRefType, FallbackTypes...> {
519 return details::ObjectUnsafe::ObjectRefFromObjectPtr<ObjectRefType>(
520 details::ObjectUnsafe::ObjectPtrFromUnowned<ExprNode>(src->v_obj));
531 ffi::String, PrimExprConvertible> {
534 using Base::CheckAnyStrict;
535 using Base::CopyFromAnyViewAfterCheck;
536 using Base::CopyToAnyView;
537 using Base::GetMismatchTypeInfo;
538 using Base::MoveFromAnyAfterCheck;
539 using Base::MoveToAny;
540 using Base::TryCastFromAnyView;
541 using Base::TypeSchema;
548 return PrimExpr::ConvertFallbackValue(value);
551 return value->ToPrimExpr();
561struct TypeTraits<
Expr> :
public ObjectRefWithFallbackTraitsBase<Expr, PrimExpr> {
Managed reference to CallNode.
Definition expr.h:474
Base type of all the expressions.
Definition base_expr.h:300
Type ty
The deduced or annotated type of the expression.
Definition base_expr.h:314
Span span
Span that points to the original source code. Reserved debug information.
Definition base_expr.h:306
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition base_expr.h:325
TVM_FFI_DECLARE_OBJECT_INFO("ir.Expr", ExprNode, ffi::Object)
static constexpr const uint32_t _type_child_slots
Definition base_expr.h:327
static void RegisterReflection()
Definition base_expr.h:316
Managed reference to ExprNode.
Definition base_expr.h:335
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Expr, ffi::ObjectRef, ExprNode)
bool operator==(const Expr &other) const =delete
bool operator!=(const Expr &other) const =delete
bool operator<(const Expr &other) const =delete
Base node for opaque construction-time expressions.
Definition base_expr.h:352
static constexpr const uint32_t _type_child_slots
Definition base_expr.h:359
TVM_FFI_DECLARE_OBJECT_INFO("ir.OpaqueExpr", OpaqueExprNode, ExprNode)
static void RegisterReflection()
Definition base_expr.h:354
Managed reference to OpaqueExprNode.
Definition base_expr.h:364
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(OpaqueExpr, Expr, OpaqueExprNode)
Type marker for opaque construction-time expressions.
Definition base_expr.h:94
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.OpaqueType", OpaqueTypeNode, TypeNode)
static void RegisterReflection()
Definition base_expr.h:96
Managed reference to OpaqueTypeNode.
Definition base_expr.h:105
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(OpaqueType, Type, OpaqueTypeNode)
Base class for other IR constructs that can be converted to PrimExpr. This is useful for the FFI to c...
Definition base_expr.h:438
virtual ~PrimExprConvertibleNode()
Definition base_expr.h:440
virtual PrimExpr ToPrimExpr() const =0
TVM_FFI_DECLARE_OBJECT_INFO("ir.PrimExprConvertible", PrimExprConvertibleNode, ffi::Object)
Managed reference to PrimExprConvertibleNode.
Definition base_expr.h:449
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PrimExprConvertible, ffi::ObjectRef, PrimExprConvertibleNode)
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
static constexpr bool _type_container_is_exact
Definition base_expr.h:424
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PrimExpr, TypedExpr< PrimType >, ExprNode)
PrimExpr(float value)
construct from float.
PrimExpr(int32_t value)
construct from integer.
static PrimExpr ConvertFallbackValue(ffi::String value)
construct from string to form a StringImm.
PrimExpr(Call call)
Construct from a call after checking that its result type is PrimType.
Primitive data types used in the low-level IR.
Definition base_expr.h:119
DLDataType dtype
The raw DLPack dtype represented by this primitive type.
Definition base_expr.h:124
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.PrimType", PrimTypeNode, TypeNode)
static void RegisterReflection()
Definition base_expr.h:126
Definition base_expr.h:137
static PrimType ScalableVector(DLDataTypeCode code, int bits, int lanes)
Construct a scalable vector type.
TVM_FFI_INLINE DLDataTypeCode code() const
Definition base_expr.h:174
static PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
static PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
TVM_FFI_INLINE bool IsVoid() const
Whether this type is the void sentinel handle(0, 0).
Definition base_expr.h:220
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
static PrimType Bool(int lanes=1)
Construct a boolean type with fixed lanes.
TVM_FFI_INLINE int32_t bits() const
Definition base_expr.h:179
TVM_FFI_INLINE bool IsFixedLengthVector() const
Whether this type is a fixed-length vector.
Definition base_expr.h:232
TVM_FFI_INLINE bool IsScalar() const
Whether this type is a scalar, excluding fixed and scalable vectors.
Definition base_expr.h:214
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
static PrimType BFloat(int bits, int lanes=1)
Construct a bfloat type with fixed lanes.
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_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrimType, Type, PrimTypeNode)
TVM_FFI_INLINE int32_t VScaleFactor() const
Definition base_expr.h:279
PrimType(DLDataTypeCode code, int bits, int lanes=1)
Construct from DLPack dtype fields.
TVM_FFI_INLINE int32_t lanes() const
Definition base_expr.h:185
TVM_FFI_INLINE PrimType WithBits(int bits) const
Return the same type with a different scalar bit width, preserving code and lanes.
Definition base_expr.h:264
TVM_FFI_INLINE size_t StorageBytes() const
Return the number of bytes needed to store one value of this type.
Definition base_expr.h:242
static PrimType UInt(int bits, int lanes=1)
Construct an unsigned integer type with fixed lanes.
PrimType(DLDataType dtype)
Construct from a raw DLPack dtype.
TVM_FFI_INLINE PrimType WithCode(DLDataTypeCode code) const
Return the same type with a different dtype code, preserving bits and lanes.
Definition base_expr.h:254
Definition source_map.h:111
Type is the base type of all types.
Definition base_expr.h:52
static constexpr const uint32_t _type_child_slots
Definition base_expr.h:69
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition base_expr.h:67
TVM_FFI_DECLARE_OBJECT_INFO("ir.Type", TypeNode, ffi::Object)
static void RegisterReflection()
Definition base_expr.h:60
Span span
Span that points to the original source code. Reserved debug information.
Definition base_expr.h:58
Managed reference to TypeNode.
Definition base_expr.h:77
static Type Missing()
Sentinel for a type that has not been populated yet.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Type, ffi::ObjectRef, TypeNode)
Typed reference/view over an expression whose result type is a specific Type subtype.
Definition base_expr.h:377
static constexpr bool _type_container_is_exact
Definition base_expr.h:389
ExpectedType ty() const
Definition base_expr.h:380
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TypedExpr, Expr, ExprNode)
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
With(Args &&... args)
constructor. Enter the scope of the context.
Definition with_context.h:66
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
bool operator!=(const PrimType &lhs, const PrimType &rhs)
Definition base_expr.h:294
bool operator==(const PrimType &lhs, const PrimType &rhs)
Definition base_expr.h:290
A map from source names to source code.
static TVM_FFI_INLINE Expr ConvertFallbackValue(PrimExpr value)
Definition base_expr.h:562
static TVM_FFI_INLINE PrimExpr ConvertFallbackValue(ffi::String value)
Definition base_expr.h:547
static PrimExpr ConvertFallbackValue(int64_t value)
static TVM_FFI_INLINE PrimExpr ConvertFallbackValue(PrimExprConvertible value)
Definition base_expr.h:550
static PrimExpr ConvertFallbackValue(StrictBool value)
static PrimExpr ConvertFallbackValue(double value)
static TVM_FFI_INLINE PrimType ConvertFallbackValue(DLDataType dtype)
Definition base_expr.h:461
ObjectRefTypeTraitsBase< TypedExpr< ExpectedType > > Base
Definition base_expr.h:470
static TVM_FFI_INLINE std::optional< TypedExpr< ExpectedType > > TryCastFromAnyView(const TVMFFIAny *src)
Definition base_expr.h:492
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition base_expr.h:479
Definition base_expr.h:510
static TVM_FFI_INLINE std::optional< ObjectRefType > TryCastFromAnyView(const TVMFFIAny *src)
Definition base_expr.h:517
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition base_expr.h:513