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
34#include <string>
35
36namespace tvm {
37// forward declare shared statement.
38class Stmt;
39
40namespace tirx {
41
50class TensorVar : public Var {
51 public:
54
56 explicit TensorVar(Var var) : Var(std::move(var)) {
57 TVM_FFI_ICHECK(get() != nullptr && get()->ty.as<TensorTypeNode>())
58 << "Expected a non-null Var with TensorType";
59 }
60
62 Var var() const { return ffi::GetRef<Var>(get()); }
63
65 TensorType type() const { return get()->ty.as_or_throw<TensorType>(); }
66
68 const ffi::String& name() const { return get()->name; }
69
71 const Location& loc() const { return get()->loc; }
72
74 Expr data() const { return Call(type()->DataPtrType(), tensor_data_ptr_op(), {var()}); }
75
77 ffi::String scope() const { return (*this)->storage_scope; }
78
88 TVM_TIRX_DLL bool IsScalar(bool alloc_or_decl = true) const;
89
91 PrimType ElementType() const { return (*this)->ElementType(); }
92
93 explicit TensorVar(ffi::ObjectPtr<VarNode> node) : Var(std::move(node)) {}
94 explicit TensorVar(ffi::UnsafeInit tag) : Var(tag) {}
96
97 const TensorTypeNode* operator->() const {
98 const auto* var_node = static_cast<const VarNode*>(data_.get());
99 TVM_FFI_ICHECK(var_node != nullptr);
100 const auto* type_node = var_node->ty.as<TensorTypeNode>();
101 TVM_FFI_ICHECK(type_node != nullptr)
102 << "Expected a Var with TensorType, but " << var_node->name << " has type " << var_node->ty;
103 return type_node;
104 }
105
106 const VarNode* get() const { return static_cast<const VarNode*>(data_.get()); }
107
108 [[maybe_unused]] static constexpr bool _type_is_nullable = false;
109 static constexpr bool _type_container_is_exact = false;
111};
112
113// Preserve ObjectRef-style identity comparison for exact TensorVar operands.
114// Comparisons widened to Var or Expr continue to build symbolic expressions.
115inline bool operator==(const TensorVar& lhs, const TensorVar& rhs) { return lhs.same_as(rhs); }
116
117inline bool operator!=(const TensorVar& lhs, const TensorVar& rhs) { return !lhs.same_as(rhs); }
118
120inline TensorVar GetTensorVar(const VarNode* var) { return TensorVar(ffi::GetRef<Var>(var)); }
121
122inline ffi::ObjectPtr<TensorTypeNode> CopyTensorType(const TensorVar& var) {
123 return ffi::make_object<TensorTypeNode>(*var.operator->());
124}
125
126inline TensorVar RebuildTensorVar(const TensorVar& var, ffi::ObjectPtr<TensorTypeNode> type,
127 ffi::Optional<ffi::String> name = std::nullopt) {
128 return TensorVar(name.value_or(var.name()), TensorType(std::move(type)), var.loc());
129}
130
141TVM_TIRX_DLL TensorVar decl_tensor(ffi::Array<PrimExpr> shape, PrimType dtype = PrimType::Float(32),
142 ffi::String name = "tensor", ffi::String storage_scope = "",
143 Location loc = UnknownLoc());
144
158 std::string name, int data_alignment,
159 int offset_factor,
160 std::string memory_scope = "");
161
169TVM_TIRX_DLL TensorLoad MakeTensorLoad(TensorVar tensor, ffi::Array<PrimExpr> indices,
170 Location loc = UnknownLoc());
171
175TVM_TIRX_DLL TensorRegion TensorRegionFromPoint(TensorVar tensor, ffi::Array<PrimExpr> indices);
176
177} // namespace tirx
178} // namespace tvm
179
180namespace tvm::ffi {
181
182template <>
183inline constexpr bool use_default_type_traits_v<tirx::TensorVar> = false;
184
185template <>
186struct TypeTraits<tirx::TensorVar> : public ObjectRefTypeTraitsBase<tirx::TensorVar> {
187 using Base = ObjectRefTypeTraitsBase<tirx::TensorVar>;
188 using Base::CopyFromAnyViewAfterCheck;
189 using Base::CopyToAnyView;
190 using Base::GetMismatchTypeInfo;
191 using Base::MoveFromAnyAfterCheck;
192 using Base::MoveToAny;
193 using Base::TypeSchema;
194 using Base::TypeStr;
195
196 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
197 if (src->type_index == TypeIndex::kTVMFFINone) {
198 return false;
199 }
200 if (src->type_index != tvm::VarNode::RuntimeTypeIndex()) {
201 return false;
202 }
203 const auto* var = static_cast<const tvm::VarNode*>(
204 details::ObjectUnsafe::ObjectPtrFromUnowned<Object>(src->v_obj).get());
205 return details::AnyUnsafe::CheckAnyStrict<tirx::TensorType>(var->ExprNode::ty);
206 }
207
208 TVM_FFI_INLINE static std::optional<tirx::TensorVar> TryCastFromAnyView(const TVMFFIAny* src) {
209 if (CheckAnyStrict(src)) {
210 return details::ObjectUnsafe::ObjectRefFromObjectPtr<tirx::TensorVar>(
211 details::ObjectUnsafe::ObjectPtrFromUnowned<tvm::VarNode>(src->v_obj));
212 }
213 return std::nullopt;
214 }
215};
216
217} // namespace tvm::ffi
218
219#endif // TVM_TIRX_EXPR_H_
Managed reference to CallNode.
Definition expr.h:972
Type ty
The deduced or annotated type of the expression.
Definition base_expr.h:338
Location loc
Location that points to the original source code. Reserved debug information.
Definition base_expr.h:330
Managed reference to ExprNode.
Definition base_expr.h:359
Non-nullable source-location metadata.
Definition location.h:91
Definition base_expr.h:161
static TVM_TIRX_DLL PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
Managed reference to TensorLoadNode.
Definition expr.h:464
Managed reference to TensorRegionNode.
Definition expr.h:1079
Reference to the shared immutable unknown location.
Definition location.h:105
A local variable in the IR.
Definition expr.h:694
ffi::String name
The variable name.
Definition expr.h:697
Managed reference to VarNode.
Definition expr.h:714
Structural type of a TIRx tensor variable.
Definition type.h:64
Managed reference to TensorTypeNode.
Definition type.h:146
Checked zero-state view over an ordinary VarNode with TensorType.
Definition expr.h:50
Expr data() const
Project the physical pointer established by the definition site.
Definition expr.h:74
TVM_TIRX_DLL bool IsScalar(bool alloc_or_decl=true) const
Return a new tensor with the allocated address.
const ffi::String & name() const
Return the tensor's diagnostic name.
Definition expr.h:68
TensorType type() const
Return the tensor type carried by the ordinary variable.
Definition expr.h:65
TensorVar(Var var)
Create a checked tensor view over an existing ordinary Var.
Definition expr.h:56
Var var() const
Return the ordinary variable view over the same identity.
Definition expr.h:62
TensorVar(ffi::UnsafeInit tag)
Definition expr.h:94
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TensorVar)
const VarNode * get() const
Definition expr.h:106
const Location & loc() const
Return the source location carried by the ordinary Var.
Definition expr.h:71
ffi::String scope() const
Return the storage scope associated with this tensor.
Definition expr.h:77
static constexpr bool _type_container_is_exact
Definition expr.h:109
static constexpr bool _type_is_nullable
Definition expr.h:108
TensorVar(ffi::ObjectPtr< VarNode > node)
Definition expr.h:93
PrimType ElementType() const
Definition expr.h:91
TVM_TIRX_DLL TensorVar(ffi::String name, TensorType type, Location loc=UnknownLoc())
Construct a fresh tensor variable from an explicit TensorType.
const TensorTypeNode * operator->() const
Definition expr.h:97
Base expr nodes in TVM.
Definition base_expr.h:487
ffi::ObjectPtr< TensorTypeNode > CopyTensorType(const TensorVar &var)
Definition expr.h:122
TVM_TIRX_DLL const Op & tensor_data_ptr_op()
Project the physical pointer associated with a TensorVar definition.
TVM_TIRX_DLL tirx::TensorVar TensorWithOffsetAlignment(ffi::Array< PrimExpr > shape, PrimType dtype, std::string name, int data_alignment, int offset_factor, std::string memory_scope="")
Creates a TIR tensor for the provided parameters.
TVM_TIRX_DLL TensorRegion TensorRegionFromPoint(TensorVar tensor, ffi::Array< PrimExpr > indices)
Construct unit or vector-lane ranges from point indices.
TVM_TIRX_DLL TensorVar decl_tensor(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::String name="tensor", ffi::String storage_scope="", Location loc=UnknownLoc())
Construct a new tensor given shape, and dtype.
bool operator!=(const TensorVar &lhs, const TensorVar &rhs)
Definition expr.h:117
TensorVar GetTensorVar(const VarNode *var)
Recover a checked tensor view from an ordinary VarNode pointer.
Definition expr.h:120
TVM_TIRX_DLL TensorLoad MakeTensorLoad(TensorVar tensor, ffi::Array< PrimExpr > indices, Location loc=UnknownLoc())
Construct a TensorLoad from a TensorVar.
bool operator==(const TensorVar &lhs, const TensorVar &rhs)
Definition expr.h:115
TVM_TIRX_DLL TensorRegion FullTensorRegion(TensorVar tensor)
Select the entire tensor.
TensorVar RebuildTensorVar(const TensorVar &var, ffi::ObjectPtr< TensorTypeNode > type, ffi::Optional< ffi::String > name=std::nullopt)
Definition expr.h:126
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition expr.h:196
static TVM_FFI_INLINE std::optional< tirx::TensorVar > TryCastFromAnyView(const TVMFFIAny *src)
Definition expr.h:208
ObjectRefTypeTraitsBase< tirx::TensorVar > Base
Definition expr.h:187
Memory operations for TIRx.
Types specific to TIRX.