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/cast.h>
53 #include <tvm/ffi/container/array.h>
54 #include <tvm/ffi/reflection/registry.h>
55 #include <tvm/ir/source_map.h>
56 #include <tvm/runtime/data_type.h>
57 
58 #include <string>
59 
60 namespace tvm {
61 
74 class TypeNode : public ffi::Object {
75  public:
80  mutable Span span;
81 
82  static void RegisterReflection() {
83  namespace refl = tvm::ffi::reflection;
84  // span do not participate in structural equal and hash.
85  refl::ObjectDef<TypeNode>().def_ro("span", &TypeNode::span, refl::DefaultValue(Span()),
86  refl::AttachFieldFlag::SEqHashIgnore());
87  }
88 
89  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
90 
91  static constexpr const uint32_t _type_child_slots = 14;
92  TVM_FFI_DECLARE_OBJECT_INFO("ir.Type", TypeNode, ffi::Object);
93 };
94 
99 class Type : public ffi::ObjectRef {
100  public:
102 };
103 
112 class PrimTypeNode : public TypeNode {
113  public:
118 
119  static void RegisterReflection() {
120  namespace refl = tvm::ffi::reflection;
121  refl::ObjectDef<PrimTypeNode>().def_ro("dtype", &PrimTypeNode::dtype);
122  }
124 };
125 
126 /*
127  * \brief Managed reference to PrimTypeNode.
128  * \sa PrimTypeNode
129  */
130 class PrimType : public Type {
131  public:
137  TVM_DLL explicit PrimType(runtime::DataType dtype, Span span = Span());
138 
140 };
141 
152 class PointerTypeNode : public TypeNode {
153  public:
161  ffi::String storage_scope;
162 
163  static void RegisterReflection() {
164  namespace refl = tvm::ffi::reflection;
165  refl::ObjectDef<PointerTypeNode>()
166  .def_ro("element_type", &PointerTypeNode::element_type)
167  .def_ro("storage_scope", &PointerTypeNode::storage_scope);
168  }
170 };
171 
172 /*
173  * \brief Managed reference to PointerTypeNode.
174  * \sa PointerTypeNode
175  */
176 class PointerType : public Type {
177  public:
183  TVM_DLL explicit PointerType(Type element_type, ffi::String storage_scope = "");
184 
186 };
187 
192 class TupleTypeNode : public TypeNode {
193  public:
195  ffi::Array<Type> fields;
196 
198 
199  static void RegisterReflection() {
200  namespace refl = tvm::ffi::reflection;
201  refl::ObjectDef<TupleTypeNode>().def_ro("fields", &TupleTypeNode::fields);
202  }
204 };
205 
210 class TupleType : public Type {
211  public:
217  TVM_DLL explicit TupleType(ffi::Array<Type> fields, Span span = Span());
218 
223  TVM_DLL TupleType static Empty();
224 
226 };
227 
231 inline Type VoidType() { return TupleType::Empty(); }
232 
237 inline bool IsVoidType(const Type& type) {
238  auto* n = type.as<TupleTypeNode>();
239  return n && n->fields.size() == 0;
240 }
241 
250 class FuncTypeNode : public TypeNode {
251  public:
253  ffi::Array<Type> arg_types;
256 
257  static void RegisterReflection() {
258  namespace refl = tvm::ffi::reflection;
259  refl::ObjectDef<FuncTypeNode>()
260  .def_ro("arg_types", &FuncTypeNode::arg_types)
261  .def_ro("ret_type", &FuncTypeNode::ret_type);
262  }
264 };
265 
270 class FuncType : public Type {
271  public:
279  TVM_DLL FuncType(ffi::Array<Type> arg_types, Type ret_type, Span span = Span());
280 
282 };
283 
288 class TensorMapTypeNode : public TypeNode {
289  public:
290  static void RegisterReflection() {
291  namespace refl = tvm::ffi::reflection;
292  refl::ObjectDef<TensorMapTypeNode>();
293  }
295 };
296 
301 class TensorMapType : public Type {
302  public:
303  TVM_DLL TensorMapType(Span span = Span());
304 
305  explicit TensorMapType(::tvm::ffi::ObjectPtr<TensorMapTypeNode> n) : Type(n) {}
306  TensorMapType(const TensorMapType&) = default;
310  const TensorMapTypeNode* operator->() const {
311  return static_cast<const TensorMapTypeNode*>(data_.get());
312  }
313  const TensorMapTypeNode* get() const { return operator->(); }
315 };
316 
317 } // namespace tvm
318 #endif // TVM_IR_TYPE_H_
Function type.
Definition: type.h:250
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.FuncType", FuncTypeNode, TypeNode)
Type ret_type
The type of return value.
Definition: type.h:255
ffi::Array< Type > arg_types
type type of arguments
Definition: type.h:253
static void RegisterReflection()
Definition: type.h:257
Managed reference to FuncTypeNode.
Definition: type.h:270
FuncType(ffi::Array< Type > arg_types, Type ret_type, Span span=Span())
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(FuncType, Type, FuncTypeNode)
Low-level raw pointer type.
Definition: type.h:152
Type element_type
The type of the element which the pointer points to.
Definition: type.h:157
ffi::String storage_scope
The storage scope of the pointer.
Definition: type.h:161
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.PointerType", PointerTypeNode, TypeNode)
static void RegisterReflection()
Definition: type.h:163
Definition: type.h:176
PointerType(Type element_type, ffi::String storage_scope="")
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PointerType, Type, PointerTypeNode)
Primitive data types used in the low-level IR.
Definition: type.h:112
runtime::DataType dtype
The corresponding dtype field.
Definition: type.h:117
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.PrimType", PrimTypeNode, TypeNode)
static void RegisterReflection()
Definition: type.h:119
Definition: type.h:130
PrimType(runtime::DataType dtype, Span span=Span())
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PrimType, Type, PrimTypeNode)
Definition: source_map.h:111
The type of tensor map.
Definition: type.h:288
static void RegisterReflection()
Definition: type.h:290
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TensorMapType", TensorMapTypeNode, TypeNode)
Managed reference to TensorMapTypeNode.
Definition: type.h:301
TensorMapType(Span span=Span())
const TensorMapTypeNode * get() const
Definition: type.h:313
TensorMapType & operator=(TensorMapType &&)=default
TensorMapType & operator=(const TensorMapType &)=default
const TensorMapTypeNode * operator->() const
Definition: type.h:310
TensorMapType(const TensorMapType &)=default
TensorMapType(::tvm::ffi::ObjectPtr< TensorMapTypeNode > n)
Definition: type.h:305
TensorMapType(TensorMapType &&)=default
The type of tuple values.
Definition: type.h:192
TupleTypeNode()
Definition: type.h:197
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TupleType", TupleTypeNode, TypeNode)
static void RegisterReflection()
Definition: type.h:199
ffi::Array< Type > fields
The type of each field in the tuple.
Definition: type.h:195
Managed reference to TupleTypeNode.
Definition: type.h:210
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TupleType, Type, TupleTypeNode)
TupleType(ffi::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:91
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: type.h:89
TVM_FFI_DECLARE_OBJECT_INFO("ir.Type", TypeNode, ffi::Object)
static void RegisterReflection()
Definition: type.h:82
Span span
Span that points to the original source code. Reserved debug information.
Definition: type.h:80
Managed reference to TypeNode.
Definition: type.h:99
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Type, ffi::ObjectRef, TypeNode)
Runtime primitive data type.
Definition: data_type.h:45
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
bool IsVoidType(const Type &type)
Check whether the tyep represents void.
Definition: type.h:237
Type VoidType()
Definition: type.h:231
A map from source names to source code.