tvm
Loading...
Searching...
No Matches
expr.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
24#ifndef TVM_TIRX_EXPR_H_
25#define TVM_TIRX_EXPR_H_
26
27#include <tvm/ffi/container/array.h>
28#include <tvm/ffi/reflection/registry.h>
29#include <tvm/ffi/string.h>
30#include <tvm/ir/expr.h>
31#include <tvm/tirx/op/memory.h>
32#include <tvm/tirx/type.h>
33
34namespace tvm {
35// forward declare shared statement.
36class Stmt;
37
38namespace tirx {
39
48class TensorVar : public Var {
49 public:
51 TVM_TIRX_DLL explicit TensorVar(ffi::String name, TensorType type, Location loc = UnknownLoc());
52
54 explicit TensorVar(Var var) : Var(std::move(var)) {
55 TVM_FFI_ICHECK(get() != nullptr && get()->ty.as<TensorTypeObj>())
56 << "Expected a non-null Var with TensorType";
57 }
58
60 TensorType ty() const { return get()->ty.as_or_throw<TensorType>(); }
61
63 const ffi::Array<PrimExpr>& shape() const { return ty()->shape; }
64
66 PrimType dtype() const { return ty()->dtype; }
67
69 Expr data() const { return Call(ty()->DataPtrType(), tensor_data_ptr_op(), {*this}); }
70
72 runtime::MemoryScope scope() const { return ty()->scope; }
73
74 explicit TensorVar(ffi::ObjectPtr<VarObj> node) : TensorVar(Var(std::move(node))) {}
75 explicit TensorVar(ffi::UnsafeInit tag) : Var(tag) {}
77
78 [[maybe_unused]] static constexpr bool _type_is_nullable = false;
79 static constexpr bool _type_container_is_exact = false;
81};
82
83// Preserve ObjectRef-style identity comparison for exact TensorVar operands.
84// Comparisons widened to Var or Expr remain unavailable; use same_as for identity.
85inline bool operator==(const TensorVar& lhs, const TensorVar& rhs) { return lhs.same_as(rhs); }
86
87inline bool operator!=(const TensorVar& lhs, const TensorVar& rhs) { return !lhs.same_as(rhs); }
88
89} // namespace tirx
90} // namespace tvm
91
92namespace tvm::ffi {
93
94template <>
95inline constexpr bool use_default_type_traits_v<tirx::TensorVar> = false;
96
97template <>
98struct TypeTraits<tirx::TensorVar> : public ObjectRefTypeTraitsBase<tirx::TensorVar> {
99 using Base = ObjectRefTypeTraitsBase<tirx::TensorVar>;
100 using Base::CopyFromAnyViewAfterCheck;
101 using Base::CopyToAnyView;
102 using Base::GetMismatchTypeInfo;
103 using Base::MoveFromAnyAfterCheck;
104 using Base::MoveToAny;
105 using Base::TypeSchema;
106 using Base::TypeStr;
107
108 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
109 if (src->type_index == TypeIndex::kTVMFFINone) {
110 return false;
111 }
112 if (src->type_index != tvm::VarObj::RuntimeTypeIndex()) {
113 return false;
114 }
115 const auto* var = static_cast<const tvm::VarObj*>(
116 detail::ObjectUnsafe::ObjectPtrFromUnowned<Object>(src->v_obj).get());
117 return detail::AnyUnsafe::CheckAnyStrict<tirx::TensorType>(var->ExprObj::ty);
118 }
119
120 TVM_FFI_INLINE static std::optional<tirx::TensorVar> TryCastFromAnyView(const TVMFFIAny* src) {
121 if (CheckAnyStrict(src)) {
122 return detail::ObjectUnsafe::ObjectRefFromObjectPtr<tirx::TensorVar>(
123 detail::ObjectUnsafe::ObjectPtrFromUnowned<tvm::VarObj>(src->v_obj));
124 }
125 return std::nullopt;
126 }
127};
128
129} // namespace tvm::ffi
130
131#endif // TVM_TIRX_EXPR_H_
Base expr nodes in TVM.
#define TVM_TIRX_DLL
Definition base.h:55
Types specific to TIRX.
Managed reference to CallObj.
Definition expr.h:825
Managed reference to ExprObj.
Definition expr.h:101
Non-nullable source-location metadata.
Definition location.h:91
Definition type.h:182
Reference to the shared immutable unknown location.
Definition location.h:105
A local variable in the IR.
Definition expr.h:552
Managed reference to VarObj.
Definition expr.h:571
A registered memory space, independent of backend allocation policy.
Definition memory_scope.h:65
Structural type of a TIRx tensor variable.
Definition type.h:59
Managed reference to TensorTypeObj.
Definition type.h:129
Checked zero-state view over an ordinary VarObj with TensorType.
Definition expr.h:48
Expr data() const
Project the physical pointer established by the definition site.
Definition expr.h:69
const ffi::Array< PrimExpr > & shape() const
Return the shape of the tensor.
Definition expr.h:63
TensorVar(Var var)
Create a checked tensor view over an existing ordinary Var.
Definition expr.h:54
TensorVar(ffi::UnsafeInit tag)
Definition expr.h:75
runtime::MemoryScope scope() const
Return the storage scope associated with this tensor.
Definition expr.h:72
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TensorVar)
TensorVar(ffi::ObjectPtr< VarObj > node)
Definition expr.h:74
static constexpr bool _type_container_is_exact
Definition expr.h:79
static constexpr bool _type_is_nullable
Definition expr.h:78
TensorType ty() const
Return the tensor type carried by the ordinary variable.
Definition expr.h:60
PrimType dtype() const
Return the primitive element type.
Definition expr.h:66
TVM_TIRX_DLL TensorVar(ffi::String name, TensorType type, Location loc=UnknownLoc())
Construct a fresh tensor variable from an explicit TensorType.
Definition nested_msg.h:600
TVM_TIRX_DLL const Op & tensor_data_ptr_op()
Project the physical pointer associated with a TensorVar definition.
bool operator!=(const TensorVar &lhs, const TensorVar &rhs)
Definition expr.h:87
bool operator==(const TensorVar &lhs, const TensorVar &rhs)
Definition expr.h:85
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition expr.h:108
static TVM_FFI_INLINE std::optional< tirx::TensorVar > TryCastFromAnyView(const TVMFFIAny *src)
Definition expr.h:120
ObjectRefTypeTraitsBase< tirx::TensorVar > Base
Definition expr.h:99
Memory operations for TIRx.