tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
204 class TupleTypeNode : public TypeNode {
205  public:
208 
210 
212  v->Visit("fields", &fields);
213  v->Visit("span", &span);
214  }
215 
216  bool SEqualReduce(const TupleTypeNode* other, SEqualReducer equal) const {
217  return equal(fields, other->fields);
218  }
219 
220  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(fields); }
221 
222  static constexpr const char* _type_key = "TupleType";
224 };
225 
230 class TupleType : public Type {
231  public:
237  TVM_DLL explicit TupleType(Array<Type> fields, Span span = Span());
238 
243  TVM_DLL TupleType static Empty();
244 
246 };
247 
251 inline Type VoidType() { return TupleType::Empty(); }
252 
257 inline bool IsVoidType(const Type& type) {
258  auto* n = type.as<TupleTypeNode>();
259  return n && n->fields.size() == 0;
260 }
261 
270 class FuncTypeNode : public TypeNode {
271  public:
276 
278  v->Visit("arg_types", &arg_types);
279  v->Visit("ret_type", &ret_type);
280  v->Visit("span", &span);
281  }
282 
283  bool SEqualReduce(const FuncTypeNode* other, SEqualReducer equal) const {
284  // type params first as they defines type vars.
285  return equal(arg_types, other->arg_types) && equal(ret_type, other->ret_type);
286  }
287 
288  void SHashReduce(SHashReducer hash_reduce) const {
289  hash_reduce(arg_types);
290  hash_reduce(ret_type);
291  }
292 
293  static constexpr const char* _type_key = "FuncType";
295 };
296 
301 class FuncType : public Type {
302  public:
310  TVM_DLL FuncType(Array<Type> arg_types, Type ret_type, Span span = Span());
311 
313 };
314 
315 } // namespace tvm
316 #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:270
bool SEqualReduce(const FuncTypeNode *other, SEqualReducer equal) const
Definition: type.h:283
static constexpr const char * _type_key
Definition: type.h:293
Type ret_type
The type of return value.
Definition: type.h:275
TVM_DECLARE_FINAL_OBJECT_INFO(FuncTypeNode, TypeNode)
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:288
Array< Type > arg_types
type type of arguments
Definition: type.h:273
void VisitAttrs(AttrVisitor *v)
Definition: type.h:277
Managed reference to FuncTypeNode.
Definition: type.h:301
FuncType(Array< Type > arg_types, Type ret_type, Span span=Span())
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(FuncType, Type, FuncTypeNode)
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)
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:135
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
Definition: source_map.h:120
The type of tuple values.
Definition: type.h:204
TupleTypeNode()
Definition: type.h:209
bool SEqualReduce(const TupleTypeNode *other, SEqualReducer equal) const
Definition: type.h:216
void SHashReduce(SHashReducer hash_reduce) const
Definition: type.h:220
Array< Type > fields
The type of each field in the tuple.
Definition: type.h:207
static constexpr const char * _type_key
Definition: type.h:222
TVM_DECLARE_FINAL_OBJECT_INFO(TupleTypeNode, TypeNode)
void VisitAttrs(AttrVisitor *v)
Definition: type.h:211
Managed reference to TupleTypeNode.
Definition: type.h:230
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.
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
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:520
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:911
base class of all object containers.
Definition: object.h:172
Reference to string objects.
Definition: string.h:97
bool empty() const
Retun if the string is empty.
Definition: string.h:207
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
bool IsVoidType(const Type &type)
Check whether the tyep represents void.
Definition: type.h:257
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Type VoidType()
Definition: type.h:251
Definitions and helper macros for IR/AST nodes.
A managed object in the TVM runtime.
A map from source names to source code.