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/ffi/container/array.h>
53 #include <tvm/ffi/reflection/registry.h>
54 #include <tvm/ir/source_map.h>
55 #include <tvm/node/node.h>
56 #include <tvm/runtime/data_type.h>
57 #include <tvm/runtime/object.h>
58 
59 #include <string>
60 
61 namespace tvm {
62 
75 class TypeNode : public Object {
76  public:
81  mutable Span span;
82 
83  static void RegisterReflection() {
84  namespace refl = tvm::ffi::reflection;
85  // span do not participate in structural equal and hash.
86  refl::ObjectDef<TypeNode>().def_ro("span", &TypeNode::span, refl::DefaultValue(Span()),
87  refl::AttachFieldFlag::SEqHashIgnore());
88  }
89 
90  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
91  static constexpr const char* _type_key = "ir.Type";
92 
93  static constexpr const uint32_t _type_child_slots = 14;
95 };
96 
101 class Type : public ObjectRef {
102  public:
104 };
105 
114 class PrimTypeNode : public TypeNode {
115  public:
120 
121  static void RegisterReflection() {
122  namespace refl = tvm::ffi::reflection;
123  refl::ObjectDef<PrimTypeNode>().def_ro("dtype", &PrimTypeNode::dtype);
124  }
125 
126  static constexpr const char* _type_key = "ir.PrimType";
128 };
129 
130 /*
131  * \brief Managed reference to PrimTypeNode.
132  * \sa PrimTypeNode
133  */
134 class PrimType : public Type {
135  public:
141  TVM_DLL explicit PrimType(runtime::DataType dtype, Span span = Span());
142 
144 };
145 
156 class PointerTypeNode : public TypeNode {
157  public:
166 
167  static void RegisterReflection() {
168  namespace refl = tvm::ffi::reflection;
169  refl::ObjectDef<PointerTypeNode>()
170  .def_ro("element_type", &PointerTypeNode::element_type)
171  .def_ro("storage_scope", &PointerTypeNode::storage_scope);
172  }
173 
174  static constexpr const char* _type_key = "ir.PointerType";
176 };
177 
178 /*
179  * \brief Managed reference to PointerTypeNode.
180  * \sa PointerTypeNode
181  */
182 class PointerType : public Type {
183  public:
189  TVM_DLL explicit PointerType(Type element_type, String storage_scope = "");
190 
192 };
193 
198 class TupleTypeNode : public TypeNode {
199  public:
201  Array<Type> fields;
202 
204 
205  static void RegisterReflection() {
206  namespace refl = tvm::ffi::reflection;
207  refl::ObjectDef<TupleTypeNode>()
208  .def_ro("fields", &TupleTypeNode::fields)
209  .def_ro("span", &TupleTypeNode::span);
210  }
211 
212  static constexpr const char* _type_key = "ir.TupleType";
214 };
215 
220 class TupleType : public Type {
221  public:
227  TVM_DLL explicit TupleType(Array<Type> fields, Span span = Span());
228 
233  TVM_DLL TupleType static Empty();
234 
236 };
237 
241 inline Type VoidType() { return TupleType::Empty(); }
242 
247 inline bool IsVoidType(const Type& type) {
248  auto* n = type.as<TupleTypeNode>();
249  return n && n->fields.size() == 0;
250 }
251 
260 class FuncTypeNode : public TypeNode {
261  public:
263  Array<Type> arg_types;
266 
267  static void RegisterReflection() {
268  namespace refl = tvm::ffi::reflection;
269  refl::ObjectDef<FuncTypeNode>()
270  .def_ro("arg_types", &FuncTypeNode::arg_types)
271  .def_ro("ret_type", &FuncTypeNode::ret_type)
272  .def_ro("span", &FuncTypeNode::span);
273  }
274 
275  static constexpr const char* _type_key = "ir.FuncType";
277 };
278 
283 class FuncType : public Type {
284  public:
292  TVM_DLL FuncType(Array<Type> arg_types, Type ret_type, Span span = Span());
293 
295 };
296 
301 class TensorMapTypeNode : public TypeNode {
302  public:
303  static void RegisterReflection() {
304  namespace refl = tvm::ffi::reflection;
305  refl::ObjectDef<TensorMapTypeNode>().def_ro("span", &TensorMapTypeNode::span);
306  }
307 
308  static constexpr const char* _type_key = "ir.TensorMapType";
310 };
311 
316 class TensorMapType : public Type {
317  public:
318  TVM_DLL TensorMapType(Span span = Span());
319 
321 };
322 
323 } // namespace tvm
324 #endif // TVM_IR_TYPE_H_
Function type.
Definition: type.h:260
static constexpr const char * _type_key
Definition: type.h:275
Type ret_type
The type of return value.
Definition: type.h:265
TVM_DECLARE_FINAL_OBJECT_INFO(FuncTypeNode, TypeNode)
Array< Type > arg_types
type type of arguments
Definition: type.h:263
static void RegisterReflection()
Definition: type.h:267
Managed reference to FuncTypeNode.
Definition: type.h:283
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:156
Type element_type
The type of the element which the pointer points to.
Definition: type.h:161
TVM_DECLARE_FINAL_OBJECT_INFO(PointerTypeNode, TypeNode)
static constexpr const char * _type_key
Definition: type.h:174
String storage_scope
The storage scope of the pointer.
Definition: type.h:165
static void RegisterReflection()
Definition: type.h:167
Definition: type.h:182
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:114
runtime::DataType dtype
The corresponding dtype field.
Definition: type.h:119
static constexpr const char * _type_key
Definition: type.h:126
TVM_DECLARE_FINAL_OBJECT_INFO(PrimTypeNode, TypeNode)
static void RegisterReflection()
Definition: type.h:121
Definition: type.h:134
PrimType(runtime::DataType dtype, Span span=Span())
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(PrimType, Type, PrimTypeNode)
Definition: source_map.h:113
The type of tensor map.
Definition: type.h:301
TVM_DECLARE_FINAL_OBJECT_INFO(TensorMapTypeNode, TypeNode)
static void RegisterReflection()
Definition: type.h:303
static constexpr const char * _type_key
Definition: type.h:308
Managed reference to TensorMapTypeNode.
Definition: type.h:316
TensorMapType(Span span=Span())
TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR(TensorMapType, Type, TensorMapTypeNode)
The type of tuple values.
Definition: type.h:198
TupleTypeNode()
Definition: type.h:203
Array< Type > fields
The type of each field in the tuple.
Definition: type.h:201
static constexpr const char * _type_key
Definition: type.h:212
static void RegisterReflection()
Definition: type.h:205
TVM_DECLARE_FINAL_OBJECT_INFO(TupleTypeNode, TypeNode)
Managed reference to TupleTypeNode.
Definition: type.h:220
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:75
static constexpr const uint32_t _type_child_slots
Definition: type.h:93
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: type.h:90
static void RegisterReflection()
Definition: type.h:83
Span span
Span that points to the original source code. Reserved debug information.
Definition: type.h:81
TVM_DECLARE_BASE_OBJECT_INFO(TypeNode, Object)
static constexpr const char * _type_key
Definition: type.h:91
Managed reference to TypeNode.
Definition: type.h:101
TVM_DEFINE_OBJECT_REF_METHODS(Type, ObjectRef, TypeNode)
Runtime primitive data type.
Definition: data_type.h:47
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
bool IsVoidType(const Type &type)
Check whether the tyep represents void.
Definition: type.h:247
Type VoidType()
Definition: type.h:241
Definitions and helper macros for IR/AST nodes.
A managed object in the TVM runtime.
A map from source names to source code.