tvm
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/ir/expr.h>
28 #include <tvm/runtime/data_type.h>
29 
30 #include <functional>
31 #include <string>
32 
33 namespace tvm {
34 namespace tirx {
35 
47 class VarNode : public PrimExprNode {
48  public:
53  ffi::String name_hint;
62 
63  static void RegisterReflection() {
64  namespace refl = tvm::ffi::reflection;
65  refl::ObjectDef<VarNode>()
66  .def_ro("name", &VarNode::name_hint, refl::AttachFieldFlag::SEqHashIgnore())
67  .def_ro("type_annotation", &VarNode::type_annotation);
68  }
69 
70  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
71  static constexpr const uint32_t _type_child_slots = 1;
73 };
74 
76 class Var : public PrimExpr {
77  public:
78  explicit Var(ffi::UnsafeInit tag) : PrimExpr(tag) {}
79  explicit Var(ObjectPtr<VarNode> n) : PrimExpr(n) {}
86  TVM_DLL explicit Var(ffi::String name_hint = "v", DataType dtype = DataType::Int(32),
87  Span span = Span());
94  TVM_DLL explicit Var(ffi::String name_hint, Type type_annotation, Span span = Span());
100  TVM_DLL Var copy_with_name(const ffi::String& name) const;
106  TVM_DLL Var copy_with_suffix(const ffi::String& suffix) const;
113 
118  const VarNode* operator->() const { return get(); }
123  const VarNode* get() const { return static_cast<const VarNode*>(data_.get()); }
126 };
127 
132 class SizeVarNode : public VarNode {
133  public:
134  static void RegisterReflection() {
135  namespace refl = tvm::ffi::reflection;
136  refl::ObjectDef<SizeVarNode>();
137  }
139 };
140 
142 class SizeVar : public Var {
143  public:
144  explicit SizeVar(ObjectPtr<SizeVarNode> n) : Var(n) {}
145  explicit SizeVar(ffi::UnsafeInit tag) : Var(tag) {}
152  TVM_DLL explicit SizeVar(ffi::String name_hint = "s", DataType t = DataType::Int(32),
153  Span span = Span());
160  TVM_DLL explicit SizeVar(ffi::String name_hint, Type type_annotation, Span span = Span());
165  const SizeVarNode* operator->() const { return get(); }
170  const SizeVarNode* get() const { return static_cast<const SizeVarNode*>(data_.get()); }
173 };
174 
175 using Region = ffi::Array<Range>;
176 
184 enum IterVarType : int {
193  kDataPar = 0,
216  kOrdered = 3,
226  kOpaque = 4,
227  // The following are possible additional
228  // types that are provided during schedule
244  kTensorized = 8
245 };
246 
254  public:
268  ffi::String thread_tag;
273  mutable Span span;
274 
275  PrimExpr ToPrimExpr() const final { return var; }
276 
277  static void RegisterReflection() {
278  namespace refl = tvm::ffi::reflection;
279  refl::ObjectDef<IterVarNode>()
280  .def_ro("dom", &IterVarNode::dom)
281  .def_ro("var", &IterVarNode::var, refl::AttachFieldFlag::SEqHashDef())
282  .def_ro("iter_type", &IterVarNode::iter_type)
283  .def_ro("thread_tag", &IterVarNode::thread_tag);
284  }
285 
286  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
288 };
289 
296 class IterVar : public PrimExprConvertible {
297  public:
298  TVM_DLL IterVar(Range dom, Var var, IterVarType iter_type, ffi::String thread_tag = "",
299  Span span = Span());
303  inline operator PrimExpr() const;
304 
307 };
308 
309 // inline implementations
310 inline IterVar::operator PrimExpr() const { return (*this)->var; }
311 
312 inline const char* IterVarType2String(IterVarType t) {
313  switch (t) {
314  case kDataPar:
315  return "DataPar";
316  case kThreadIndex:
317  return "ThreadIndex";
318  case kCommReduce:
319  return "CommReduce";
320  case kOrdered:
321  return "Ordered";
322  case kOpaque:
323  return "Opaque";
324  case kUnrolled:
325  return "Unrolled";
326  case kVectorized:
327  return "Vectorized";
328  case kParallelized:
329  return "Parallelized";
330  case kTensorized:
331  return "Tensorized";
332  }
333  return "Unknown";
334 }
335 } // namespace tirx
336 } // namespace tvm
337 
338 /* \brief Allow tirx.Var as key in STL tables
339  *
340  * For most TIR expressions, it would be ambiguous whether the
341  * expression should follow reference equality or structural equality.
342  * This is not the case for variables, which do not contain nested
343  * internal structure, and are frequently used as keys in lookup
344  * tables.
345  *
346  * Providing `std::hash` and `std::equal_to` specializations for
347  * `tirx::Var` allows it to be used as a key in STL tables. For
348  * `PrimExpr`, the user must specify the type of equality used
349  * (e.g. `std::unordered_set<T, StructuralHash, StructuralEqual>` or
350  * `std::unordered_set<T, ObjectPtrHash, ObjectPtrEqual>`).
351  */
352 template <>
353 struct std::hash<tvm::tirx::Var> {
354  std::size_t operator()(const tvm::tirx::Var& var) const {
355  return tvm::runtime::ObjectPtrHash()(var);
356  }
357 };
358 
359 template <>
360 struct std::equal_to<tvm::tirx::Var> {
361  bool operator()(const tvm::tirx::Var& var_a, const tvm::tirx::Var& var_b) const {
362  return tvm::runtime::ObjectPtrEqual()(var_a, var_b);
363  }
364 };
365 
366 #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: expr.h:156
Managed reference to PrimExprConvertibleNode.
Definition: expr.h:167
Base node of all primitive expressions.
Definition: expr.h:93
Reference to PrimExprNode.
Definition: expr.h:126
DataType dtype() const
Definition: expr.h:140
Range container
Definition: expr.h:690
Definition: source_map.h:111
Managed reference to TypeNode.
Definition: type.h:99
Runtime primitive data type.
Definition: data_type.h:47
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:278
An iteration variable representing an iteration over a one dimensional interval.
Definition: var.h:253
Range dom
the domain of iteration, if known, can be None For the intermediate schedule node,...
Definition: var.h:259
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:268
IterVarType iter_type
The type of the IterVar.
Definition: var.h:263
static void RegisterReflection()
Definition: var.h:277
PrimExpr ToPrimExpr() const final
Definition: var.h:275
Span span
Span that points to the original source code. Reserved debug information.
Definition: var.h:273
Var var
The looping variable.
Definition: var.h:261
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: var.h:286
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IterVar", IterVarNode, PrimExprConvertibleNode)
Iteration Variable, represents an iteration over an integer interval.
Definition: var.h:296
IterVar(Range dom, Var 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)
A variable node represent a tensor index size, whose value must be non-negative.
Definition: var.h:132
static void RegisterReflection()
Definition: var.h:134
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.SizeVar", SizeVarNode, VarNode)
a named variable represents a tensor index size
Definition: var.h:142
SizeVar(ffi::String name_hint, Type type_annotation, Span span=Span())
Constructor which provides a more detailed type annotation.
const SizeVarNode * get() const
Get pointer to the internal value.
Definition: var.h:170
SizeVar(ObjectPtr< SizeVarNode > n)
Definition: var.h:144
SizeVar(ffi::UnsafeInit tag)
Definition: var.h:145
SizeVar(ffi::String name_hint="s", DataType t=DataType::Int(32), Span span=Span())
constructor
const SizeVarNode * operator->() const
Get pointer to the internal value.
Definition: var.h:165
A variable node in the IR.
Definition: var.h:47
ffi::String name_hint
The hint to the variable name.
Definition: var.h:53
Type type_annotation
type annotation of the variable.
Definition: var.h:61
TVM_FFI_DECLARE_OBJECT_INFO("tirx.Var", VarNode, PrimExprNode)
static void RegisterReflection()
Definition: var.h:63
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: var.h:70
static constexpr const uint32_t _type_child_slots
Definition: var.h:71
a named variable in TIR
Definition: var.h:76
Var(ObjectPtr< VarNode > n)
Definition: var.h:79
Var copy_with_suffix(const ffi::String &suffix) const
Make a new copy of var with same type, append suffix.
const VarNode * operator->() const
Get pointer to the internal value.
Definition: var.h:118
Var(ffi::String name_hint, Type type_annotation, Span span=Span())
Constructor which provides a more detailed type annotation.
Var(ffi::String name_hint="v", DataType dtype=DataType::Int(32), Span span=Span())
Constructor.
Var copy_with_dtype(DataType dtype) const
Make a new copy of the variable with specified dtype.
Var(ffi::UnsafeInit tag)
Definition: var.h:78
Var copy_with_name(const ffi::String &name) const
Make a new copy of var with same type, but a different nam.
const VarNode * get() const
Get pointer to the internal value.
Definition: var.h:123
Base expr nodes in TVM.
Definition: repr_printer.h:91
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
IterVarType
Type of iteration variable. Each IterVar have a specific type.
Definition: var.h:184
@ kThreadIndex
The IterVar itself is a thread-index of a fixed thread launching group. Note that this is already ass...
Definition: var.h:201
@ kUnrolled
The execution is unrolled.
Definition: var.h:232
@ kCommReduce
Communicative reduction. Cannot be directly parallelized.
Definition: var.h:208
@ kTensorized
Marks boundary of tensorization intrinsic.
Definition: var.h:244
@ kVectorized
The loop is vectorized.
Definition: var.h:236
@ kDataPar
Data parallel iteration. This normally corresponds to axis of Tensor. Allow all IterVar manipulations...
Definition: var.h:193
@ kParallelized
The loop is parallelized.
Definition: var.h:240
@ kOpaque
IterVar is opaque,.
Definition: var.h:226
@ kOrdered
Serial loops with loop carry dependency, the iteration must execute in order. Cannot be re-ordered.
Definition: var.h:216
ffi::Array< Range > Region
Definition: var.h:175
const char * IterVarType2String(IterVarType t)
Definition: var.h:312
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37