tvm
Loading...
Searching...
No Matches
var.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_TIR_VAR_H_
25#define TVM_TIR_VAR_H_
26
27#include <tvm/ffi/dtype.h>
28#include <tvm/ir/cow.h>
29#include <tvm/ir/expr.h>
30#include <tvm/ir/type.h>
31
32#include <utility>
33
34namespace tvm {
35namespace tirx {
36
38using Var = tvm::Var;
39
46class PrimVar : public PrimExpr {
47 public:
49 explicit PrimVar(ffi::String name, PrimType dtype = PrimType::Int(32), Span span = Span())
50 : PrimExpr(Var(std::move(name), std::move(dtype), std::move(span)).as_or_throw<PrimExpr>()) {}
51
53 explicit PrimVar(ffi::String name, Type type_annotation, Span span = Span())
54 : PrimExpr(Var(std::move(name), std::move(type_annotation), std::move(span))
55 .as_or_throw<PrimExpr>()) {}
56
58 operator Var() const { return this->as_or_throw<Var>(); }
59
60 PrimVar CopyWithSuffix(const ffi::String& suffix) const {
61 return this->as_or_throw<Var>().CopyWithSuffix(suffix).as_or_throw<PrimVar>();
62 }
64 return this->as_or_throw<Var>().CopyWithDType(dtype).as_or_throw<PrimVar>();
65 }
66
68 static constexpr bool _type_container_is_exact = false;
69};
70
71using Region = ffi::Array<Range>;
72
80enum IterVarType : int {
123 // The following are possible additional
124 // types that are provided during schedule
140 kTensorized = 8
142
150 public:
164 ffi::String thread_tag;
169 mutable Span span;
170
172
173 static void RegisterReflection() {
174 namespace refl = tvm::ffi::reflection;
175 refl::ObjectDef<IterVarNode>()
176 .def_ro("dom", &IterVarNode::dom)
177 .def_ro("var", &IterVarNode::var, refl::AttachFieldFlag::SEqHashDefRecursive())
178 .def_ro("iter_type", &IterVarNode::iter_type)
179 .def_ro("thread_tag", &IterVarNode::thread_tag)
180 .def_ro("span", &IterVarNode::span, refl::DefaultValue(Span()),
181 refl::AttachFieldFlag::SEqHashIgnore());
182 }
183
186};
187
195 public:
196 TVM_DLL IterVar(Range dom, PrimVar var, IterVarType iter_type, ffi::String thread_tag = "",
197 Span span = Span());
201 inline operator PrimExpr() const;
202
205};
206
207// inline implementations
208inline IterVar::operator PrimExpr() const { return (*this)->var; }
209
210inline const char* IterVarType2String(IterVarType t) {
211 switch (t) {
212 case kDataPar:
213 return "DataPar";
214 case kThreadIndex:
215 return "ThreadIndex";
216 case kCommReduce:
217 return "CommReduce";
218 case kOrdered:
219 return "Ordered";
220 case kOpaque:
221 return "Opaque";
222 case kUnrolled:
223 return "Unrolled";
224 case kVectorized:
225 return "Vectorized";
226 case kParallelized:
227 return "Parallelized";
228 case kTensorized:
229 return "Tensorized";
230 }
231 return "Unknown";
232}
233} // namespace tirx
234
235} // namespace tvm
236
237namespace tvm::ffi {
238
239template <>
240inline constexpr bool use_default_type_traits_v<tirx::PrimVar> = false;
241
242template <>
243struct TypeTraits<tirx::PrimVar> : public ObjectRefTypeTraitsBase<tirx::PrimVar> {
245 using Base::CopyFromAnyViewAfterCheck;
246 using Base::CopyToAnyView;
247 using Base::GetMismatchTypeInfo;
248 using Base::MoveFromAnyAfterCheck;
249 using Base::MoveToAny;
250 using Base::TypeSchema;
251 using Base::TypeStr;
252
253 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
254 if (src->type_index == TypeIndex::kTVMFFINone) {
255 return tirx::PrimVar::_type_is_nullable;
256 }
257 if (src->type_index != tirx::VarNode::RuntimeTypeIndex()) {
258 return false;
259 }
260 const auto* var = static_cast<const tirx::VarNode*>(
261 details::ObjectUnsafe::ObjectPtrFromUnowned<Object>(src->v_obj).get());
262 return details::AnyUnsafe::CheckAnyStrict<PrimType>(var->ExprNode::ty);
263 }
264
265 TVM_FFI_INLINE static std::optional<tirx::PrimVar> TryCastFromAnyView(const TVMFFIAny* src) {
266 if (CheckAnyStrict(src)) {
267 if (src->type_index == TypeIndex::kTVMFFINone) {
268 return details::ObjectUnsafe::ObjectRefFromObjectPtr<tirx::PrimVar>(nullptr);
269 }
270 return details::ObjectUnsafe::ObjectRefFromObjectPtr<tirx::PrimVar>(
271 details::ObjectUnsafe::ObjectPtrFromUnowned<tirx::VarNode>(src->v_obj));
272 }
273 return std::nullopt;
274 }
275};
276
277} // namespace tvm::ffi
278
279namespace std {
280template <>
281struct hash<::tvm::tirx::IterVar> : public ::tvm::ffi::ObjectPtrHash {};
282} // namespace std
283
284#endif // TVM_TIR_VAR_H_
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
Managed reference to PrimExprConvertibleNode.
Definition base_expr.h:449
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Range container
Definition expr.h:610
Definition source_map.h:111
Managed reference to TypeNode.
Definition base_expr.h:77
A local variable in the IR.
Definition expr.h:355
Managed reference to VarNode.
Definition expr.h:372
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
An iteration variable representing an iteration over a one dimensional interval.
Definition var.h:149
Range dom
the domain of iteration, if known, can be None For the intermediate schedule node,...
Definition var.h:155
ffi::String thread_tag
additional tag on the iteration variable, set this if this is bound already to a known thread tag.
Definition var.h:164
IterVarType iter_type
The type of the IterVar.
Definition var.h:159
static void RegisterReflection()
Definition var.h:173
PrimVar var
The looping variable.
Definition var.h:157
PrimExpr ToPrimExpr() const final
Definition var.h:171
Span span
Span that points to the original source code. Reserved debug information.
Definition var.h:169
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition var.h:184
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IterVar", IterVarNode, PrimExprConvertibleNode)
Iteration Variable, represents an iteration over an integer interval.
Definition var.h:194
IterVar(Range dom, PrimVar var, IterVarType iter_type, ffi::String thread_tag="", Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IterVar, PrimExprConvertible, IterVarNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterVarNode)
Checked scalar view over a VarNode.
Definition var.h:46
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PrimVar, PrimExpr, VarNode)
PrimVar(ffi::String name, Type type_annotation, Span span=Span())
Construct a scalar variable directly from a checked type annotation.
Definition var.h:53
static constexpr bool _type_container_is_exact
Definition var.h:68
PrimVar CopyWithSuffix(const ffi::String &suffix) const
Definition var.h:60
PrimVar(ffi::String name, PrimType dtype=PrimType::Int(32), Span span=Span())
Construct a scalar variable directly from a primitive type.
Definition var.h:49
PrimVar CopyWithDType(PrimType dtype) const
Definition var.h:63
Copy-on-write helper macro for IR ffi::ObjectRef types.
Base expr nodes in TVM.
IR/AST nodes for TVM types shared across IR variants.
Definition iter_affine_map.h:231
const char * IterVarType2String(IterVarType t)
Definition var.h:210
IterVarType
Type of iteration variable. Each IterVar have a specific type.
Definition var.h:80
@ kThreadIndex
The IterVar itself is a thread-index of a fixed thread launching group. Note that this is already ass...
Definition var.h:97
@ kUnrolled
The execution is unrolled.
Definition var.h:128
@ kCommReduce
Communicative reduction. Cannot be directly parallelized.
Definition var.h:104
@ kTensorized
Marks boundary of tensorization intrinsic.
Definition var.h:140
@ kVectorized
The loop is vectorized.
Definition var.h:132
@ kDataPar
Data parallel iteration. This normally corresponds to axis of Tensor. Allow all IterVar manipulations...
Definition var.h:89
@ kParallelized
The loop is parallelized.
Definition var.h:136
@ kOpaque
IterVar is opaque,.
Definition var.h:122
@ kOrdered
Serial loops with loop carry dependency, the iteration must execute in order. Cannot be re-ordered.
Definition var.h:112
tvm::Var Var
Definition var.h:38
ffi::Array< Range > Region
Definition var.h:71
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
ObjectRefTypeTraitsBase< tirx::PrimVar > Base
Definition var.h:244
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition var.h:253
static TVM_FFI_INLINE std::optional< tirx::PrimVar > TryCastFromAnyView(const TVMFFIAny *src)
Definition var.h:265