tvm
type.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 
49 #ifndef TVM_IR_TYPE_H_
50 #define TVM_IR_TYPE_H_
51 
52 #include <tvm/ir/source_map.h>
53 #include <tvm/node/node.h>
55 #include <tvm/runtime/data_type.h>
56 #include <tvm/runtime/object.h>
57 
58 #include <string>
59 
60 namespace tvm {
61 
74 class TypeNode : public Object {
75  public:
80  mutable Span span;
81 
82  static constexpr const char* _type_key = "Type";
83  static constexpr const bool _type_has_method_sequal_reduce = true;
84  static constexpr const bool _type_has_method_shash_reduce = true;
85  static constexpr const uint32_t _type_child_slots = 14;
87 };
88 
93 class Type : public ObjectRef {
94  public:
96 };
97 
106 class PrimTypeNode : public TypeNode {
107  public:
112 
113  void VisitAttrs(AttrVisitor* v) { v->Visit("dtype", &dtype); }
114 
115  bool SEqualReduce(const PrimTypeNode* other, SEqualReducer equal) const {
116  return equal(dtype, other->dtype);
117  }
118 
119  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(dtype); }
120 
121  static constexpr const char* _type_key = "PrimType";
123 };
124 
125 /*
126  * \brief Managed reference to PrimTypeNode.
127  * \sa PrimTypeNode
128  */
129 class PrimType : public Type {
130  public:
136  TVM_DLL explicit PrimType(runtime::DataType dtype, Span span = Span());
137 
139 };
140 
151 class PointerTypeNode : public TypeNode {
152  public:
161 
163  v->Visit("element_type", &element_type);
164  v->Visit("storage_scope", &storage_scope);
165  }
166 
167  bool SEqualReduce(const PointerTypeNode* other, SEqualReducer equal) const {
168  // Make "global" equal to ""
169  String lhs_scope = storage_scope.empty() ? "global" : storage_scope;
170  String rhs_scope = other->storage_scope.empty() ? "global" : other->storage_scope;
171  return equal(element_type, other->element_type) && equal(lhs_scope, rhs_scope);
172  }
173 
174  void SHashReduce(SHashReducer hash_reduce) const {
175  hash_reduce(element_type);
176  // Make "global" equal to ""
177  hash_reduce(storage_scope.empty() ? "global" : storage_scope);
178  }
179 
180  static constexpr const char* _type_key = "PointerType";
182 };
183 
184 /*
185  * \brief Managed reference to PointerTypeNode.
186  * \sa PointerTypeNode
187  */
188 class PointerType : public Type {
189  public:
195  TVM_DLL explicit PointerType(Type element_type, String storage_scope = "");
196 
198 };
199 
201 enum TypeKind : int {
202  kType = 0,
208  kTypeData = 6
209 };
210 
213  switch (kind) {
214  case TypeKind::kType:
215  return "Type";
216  case TypeKind::kShapeVar:
217  return "ShapeVar";
218  case TypeKind::kBaseType:
219  return "BaseType";
221  return "Constraint";
223  return "AdtHandle";
224  case TypeKind::kTypeData:
225  return "TypeData";
226  }
227  LOG(FATAL) << "ValueError: Unknown TypeKind: " << static_cast<int>(kind);
228 }
229 
248 class TypeVarNode : public TypeNode {
249  public:
258 
260  v->Visit("name_hint", &name_hint);
261  v->Visit("kind", &kind);
262  v->Visit("span", &span);
263  }
264 
265  bool SEqualReduce(const TypeVarNode* other, SEqualReducer equal) const {
266  return equal(kind, other->kind) && equal.FreeVarEqualImpl(this, other);
267  }
268 
269  void SHashReduce(SHashReducer hash_reduce) const {
270  hash_reduce(kind);
271  hash_reduce.FreeVarHashImpl(this);
272  }
273 
274  static constexpr const char* _type_key = "TypeVar";
276 };
277 
282 class TypeVar : public Type {
283  public:
290  TVM_DLL TypeVar(String name_hint, TypeKind kind, Span span = Span());
291 
293 };
294 
299 class GlobalTypeVarNode : public TypeNode {
300  public:
309 
311  v->Visit("name_hint", &name_hint);
312  v->Visit("kind", &kind);
313  }
314 
315  bool SEqualReduce(const GlobalTypeVarNode* other, SEqualReducer equal) const {
316  // name matters for now in global type var.
317  return equal(name_hint, other->name_hint) && equal.FreeVarEqualImpl(this, other);
318  }
319 
320  void SHashReduce(SHashReducer hash_reduce) const {
321  hash_reduce(name_hint);
322  hash_reduce.FreeVarHashImpl(this);
323  }
324 
325  static constexpr const char* _type_key = "GlobalTypeVar";
327 };
328 
333 class GlobalTypeVar : public Type {
334  public:
341  TVM_DLL GlobalTypeVar(String name_hint, TypeKind kind, Span span = Span());
342 
344 };
345 
350 class TupleTypeNode : public TypeNode {
351  public:
354 
356 
358  v->Visit("fields", &fields);
359  v->Visit("span", &span);
360  }
361 
362  bool SEqualReduce(const TupleTypeNode* other, SEqualReducer equal) const {
363  return equal(fields, other->fields);
364  }
365 
366  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(fields); }
367 
368  static constexpr const char* _type_key = "TupleType";
370 };
371 
376 class TupleType : public Type {
377  public:
383  TVM_DLL explicit TupleType(Array<Type> fields, Span span = Span());
384 
389  TVM_DLL TupleType static Empty();
390 
392 };
393 
397 inline Type VoidType() { return TupleType::Empty(); }
398 
403 inline bool IsVoidType(const Type& type) {
404  auto* n = type.as<TupleTypeNode>();
405  return n && n->fields.size() == 0;
406 }
407 
412 class TypeConstraintNode : public TypeNode {
413  public:
414  static constexpr const char* _type_key = "TypeConstraint";
415  static constexpr const uint32_t _type_child_slots = 1;
417 };
418 
423 class TypeConstraint : public Type {
424  public:
426 };
427 
436 class FuncTypeNode : public TypeNode {
437  public:
442  // The following fields are used in polymorphic(template) functions
443  // For normal functions, the following two fields will be empty.
451 
453  v->Visit("arg_types", &arg_types);
454  v->Visit("ret_type", &ret_type);
455  v->Visit("type_params", &type_params);
456  v->Visit("type_constraints", &type_constraints);
457  v->Visit("span", &span);
458  }
459 
460  bool SEqualReduce(const FuncTypeNode* other, SEqualReducer equal) const {
461  // type params first as they defines type vars.
462  return equal.DefEqual(type_params, other->type_params) && equal(arg_types, other->arg_types) &&
464  }
465 
466  void SHashReduce(SHashReducer hash_reduce) const {
467  hash_reduce.DefHash(type_params);
468  hash_reduce(arg_types);
469  hash_reduce(ret_type);
470  hash_reduce(type_constraints);
471  }
472 
473  static constexpr const char* _type_key = "FuncType";
475 };
476 
481 class FuncType : public Type {
482  public:
492  TVM_DLL FuncType(Array<Type> arg_types, Type ret_type, Array<TypeVar> type_params,
493  Array<TypeConstraint> type_constraints, Span span = Span());
494 
496 };
497 
508 class IncompleteTypeNode : public TypeNode {
509  public:
512 
514  v->Visit("kind", &kind);
515  v->Visit("span", &span);
516  }
517 
519  return equal(kind, other->kind) && equal.FreeVarEqualImpl(this, other);
520  }
521 
522  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(kind); }
523 
524  static constexpr const char* _type_key = "IncompleteType";
526 };
527 
532 class IncompleteType : public Type {
533  public:
539  TVM_DLL explicit IncompleteType(TypeKind kind, Span span = Span());
540 
542 };
543 
549 class RelayRefTypeNode : public TypeNode {
550  public:
553 
555 
557  v->Visit("value", &value);
558  v->Visit("span", &span);
559  }
560 
561  bool SEqualReduce(const RelayRefTypeNode* other, SEqualReducer equal) const {
562  return equal(value, other->value);
563  }
564 
565  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(value); }
566 
567  // Keep the relay prefix in the type as this type is specific
568  // to the relay itself.
569  static constexpr const char* _type_key = "relay.RefType";
571 };
572 
577 class RelayRefType : public Type {
578  public:
579  TVM_DLL explicit RelayRefType(Type value, Span span = Span());
581 };
582 } // namespace tvm
583 #endif // TVM_IR_TYPE_H_
Runtime Array container types.
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Function type.
Definition: type.h:436
bool SEqualReduce(const FuncTypeNode *other, SEqualReducer equal) const
Definition: type.h:460
static constexpr const char * _type_key
Definition: type.h:473
Type ret_type
The type of return value.
Definition: type.h:441
Array< TypeConstraint > type_constraints
potential constraint the type need to obey
Definition: type.h:450
TVM_DECLARE_FINAL_OBJECT_INFO(FuncTypeNode, TypeNode)
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:466
Array< Type > arg_types
type type of arguments
Definition: type.h:439
Array< TypeVar > type_params
The type parameters of the function.
Definition: type.h:445
void VisitAttrs(AttrVisitor *v)
Definition: type.h:452
Managed reference to FuncTypeNode.
Definition: type.h:481
FuncType(Array< Type > arg_types, Type ret_type, Array< TypeVar > type_params, Array< TypeConstraint > type_constraints, Span span=Span())
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(FuncType, Type, FuncTypeNode)
A global type variable that is used for defining new types or type aliases.
Definition: type.h:299
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:320
TypeKind kind
The kind of type parameter.
Definition: type.h:308
bool SEqualReduce(const GlobalTypeVarNode *other, SEqualReducer equal) const
Definition: type.h:315
TVM_DECLARE_FINAL_OBJECT_INFO(GlobalTypeVarNode, TypeNode)
void VisitAttrs(AttrVisitor *v)
Definition: type.h:310
static constexpr const char * _type_key
Definition: type.h:325
String name_hint
The name of the variable, this only acts as a hint to the user, and is not used for equality.
Definition: type.h:306
Managed reference to GlobalTypeVarNode.
Definition: type.h:333
TVM_DEFINE_OBJECT_REF_METHODS(GlobalTypeVar, Type, GlobalTypeVarNode)
GlobalTypeVar(String name_hint, TypeKind kind, Span span=Span())
Constructor.
Intermediate values that is used to indicate incomplete type during type inference.
Definition: type.h:508
static constexpr const char * _type_key
Definition: type.h:524
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:522
TypeKind kind
kind of the type.
Definition: type.h:511
void VisitAttrs(tvm::AttrVisitor *v)
Definition: type.h:513
bool SEqualReduce(const IncompleteTypeNode *other, SEqualReducer equal) const
Definition: type.h:518
TVM_DECLARE_FINAL_OBJECT_INFO(IncompleteTypeNode, TypeNode)
Managed reference to IncompleteTypeNode.
Definition: type.h:532
TVM_DEFINE_OBJECT_REF_METHODS(IncompleteType, Type, IncompleteTypeNode)
IncompleteType(TypeKind kind, Span span=Span())
Constructor.
Low-level raw pointer type.
Definition: type.h:151
Type element_type
The type of the element which the pointer points to.
Definition: type.h:156
TVM_DECLARE_FINAL_OBJECT_INFO(PointerTypeNode, TypeNode)
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:174
void VisitAttrs(AttrVisitor *v)
Definition: type.h:162
static constexpr const char * _type_key
Definition: type.h:180
bool SEqualReduce(const PointerTypeNode *other, SEqualReducer equal) const
Definition: type.h:167
String storage_scope
The storage scope of the pointer.
Definition: type.h:160
Definition: type.h:188
TVM_DEFINE_OBJECT_REF_METHODS(PointerType, Type, PointerTypeNode)
PointerType(Type element_type, String storage_scope="")
Constructor.
Primitive data types used in the low-level IR.
Definition: type.h:106
runtime::DataType dtype
The corresponding dtype field.
Definition: type.h:111
void VisitAttrs(AttrVisitor *v)
Definition: type.h:113
static constexpr const char * _type_key
Definition: type.h:121
TVM_DECLARE_FINAL_OBJECT_INFO(PrimTypeNode, TypeNode)
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:119
bool SEqualReduce(const PrimTypeNode *other, SEqualReducer equal) const
Definition: type.h:115
Definition: type.h:129
PrimType(runtime::DataType dtype, Span span=Span())
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(PrimType, Type, PrimTypeNode)
Reference Type High-level Relay IR.
Definition: type.h:549
RelayRefTypeNode()
Definition: type.h:554
static constexpr const char * _type_key
Definition: type.h:569
Type value
The type of value in the Reference.
Definition: type.h:552
void VisitAttrs(tvm::AttrVisitor *v)
Definition: type.h:556
bool SEqualReduce(const RelayRefTypeNode *other, SEqualReducer equal) const
Definition: type.h:561
TVM_DECLARE_FINAL_OBJECT_INFO(RelayRefTypeNode, TypeNode)
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:565
Managed reference to RelayRefTypeNode.
Definition: type.h:577
RelayRefType(Type value, Span span=Span())
TVM_DEFINE_OBJECT_REF_METHODS(RelayRefType, Type, RelayRefTypeNode)
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:126
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:110
void FreeVarHashImpl(const runtime::Object *var) const
Implementation for hash for a free var.
Definition: structural_hash.h:192
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:187
Definition: source_map.h:120
The type of tuple values.
Definition: type.h:350
TupleTypeNode()
Definition: type.h:355
bool SEqualReduce(const TupleTypeNode *other, SEqualReducer equal) const
Definition: type.h:362
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:366
Array< Type > fields
The type of each field in the tuple.
Definition: type.h:353
static constexpr const char * _type_key
Definition: type.h:368
TVM_DECLARE_FINAL_OBJECT_INFO(TupleTypeNode, TypeNode)
void VisitAttrs(AttrVisitor *v)
Definition: type.h:357
Managed reference to TupleTypeNode.
Definition: type.h:376
TVM_DEFINE_OBJECT_REF_METHODS(TupleType, Type, TupleTypeNode)
TupleType(Array< Type > fields, Span span=Span())
Constructor.
static TupleType Empty()
Create an empty tuple type that constains nothing.
Potential Constraints in a function.
Definition: type.h:412
TVM_DECLARE_BASE_OBJECT_INFO(TypeConstraintNode, TypeNode)
static constexpr const char * _type_key
Definition: type.h:414
static constexpr const uint32_t _type_child_slots
Definition: type.h:415
Managed reference to TypeConstraintNode.
Definition: type.h:423
TVM_DEFINE_OBJECT_REF_METHODS(TypeConstraint, Type, TypeConstraintNode)
Type is the base type of all types.
Definition: type.h:74
static constexpr const uint32_t _type_child_slots
Definition: type.h:85
static constexpr const bool _type_has_method_shash_reduce
Definition: type.h:84
Span span
Span that points to the original source code. Reserved debug information.
Definition: type.h:80
static constexpr const bool _type_has_method_sequal_reduce
Definition: type.h:83
TVM_DECLARE_BASE_OBJECT_INFO(TypeNode, Object)
static constexpr const char * _type_key
Definition: type.h:82
Type parameter in functions.
Definition: type.h:248
bool SEqualReduce(const TypeVarNode *other, SEqualReducer equal) const
Definition: type.h:265
void VisitAttrs(AttrVisitor *v)
Definition: type.h:259
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:269
TVM_DECLARE_FINAL_OBJECT_INFO(TypeVarNode, TypeNode)
String name_hint
The name of the variable, this only acts as a hint to the user, and is not used for equality.
Definition: type.h:255
static constexpr const char * _type_key
Definition: type.h:274
TypeKind kind
The kind of type parameter.
Definition: type.h:257
Managed reference to TypeVarNode.
Definition: type.h:282
TVM_DEFINE_OBJECT_REF_METHODS(TypeVar, Type, TypeVarNode)
TypeVar(String name_hint, TypeKind kind, Span span=Span())
Constructor.
Managed reference to TypeNode.
Definition: type.h:93
TVM_DEFINE_OBJECT_REF_METHODS(Type, ObjectRef, TypeNode)
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Runtime primitive data type.
Definition: data_type.h:43
Base class of all object reference.
Definition: object.h:519
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:906
base class of all object containers.
Definition: object.h:171
Reference to string objects.
Definition: string.h:98
bool empty() const
Retun if the string is empty.
Definition: string.h:208
tvm::Span Span
Definition: base.h:65
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
bool IsVoidType(const Type &type)
Check whether the tyep represents void.
Definition: type.h:403
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Type VoidType()
Definition: type.h:397
String TypeKind2String(TypeKind kind)
Converts a TypeKind to a string.
Definition: type.h:212
TypeKind
Possible kinds of TypeVars.
Definition: type.h:201
@ kType
Definition: type.h:202
@ kBaseType
Definition: type.h:205
@ kTypeData
Definition: type.h:208
@ kConstraint
Definition: type.h:206
@ kAdtHandle
Definition: type.h:207
@ kShapeVar
Template variable in shape expression.
Definition: type.h:204
Definitions and helper macros for IR/AST nodes.
A managed object in the TVM runtime.
A map from source names to source code.