tvm
object.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  */
23 #ifndef TVM_RUNTIME_OBJECT_H_
24 #define TVM_RUNTIME_OBJECT_H_
25 
26 #include <tvm/ffi/cast.h>
27 #include <tvm/ffi/object.h>
28 #include <tvm/ffi/optional.h>
29 #include <tvm/runtime/base.h>
30 
31 #include <utility>
32 
33 namespace tvm {
34 namespace runtime {
35 
36 using tvm::ffi::Object;
37 using tvm::ffi::ObjectPtr;
38 using tvm::ffi::ObjectPtrEqual;
39 using tvm::ffi::ObjectPtrHash;
40 using tvm::ffi::ObjectRef;
41 
42 using tvm::ffi::GetObjectPtr;
43 using tvm::ffi::GetRef;
44 
50 enum TypeIndex : int32_t {
51  // Standard static index assignments,
52  // Frontends can take benefit of these constants.
54  kRuntimeModule = TVMFFITypeIndex::kTVMFFIModule,
56  kRuntimeNDArray = TVMFFITypeIndex::kTVMFFINDArray,
58  kRuntimeShape = TVMFFITypeIndex::kTVMFFIShape,
59  // Extra builtin static index here
60  // We reserve 16 extra static indices for custom types
61  kCustomStaticIndex = TVMFFITypeIndex::kTVMFFIDynObjectBegin - 16,
68  // custom builtin
72  // static assignments that may subject to change.
74 };
75 
76 static_assert(static_cast<int>(TypeIndex::kCustomStaticIndex) >=
77  static_cast<int>(TVMFFITypeIndex::kTVMFFIStaticObjectEnd),
78  "Static slot overflows to custom indices");
79 
80 /*
81  * \brief Define the default copy/move constructor and assign operator
82  * \param TypeName The class typename.
83  */
84 #define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
85  TypeName(const TypeName& other) = default; \
86  TypeName(TypeName&& other) = default; \
87  TypeName& operator=(const TypeName& other) = default; \
88  TypeName& operator=(TypeName&& other) = default;
89 
109 #define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \
110  static_assert(ObjectName::_type_final, \
111  "TVM's CopyOnWrite may only be used for " \
112  "Object types that are declared as final, " \
113  "using the TVM_DECLARE_FINAL_OBJECT_INFO macro."); \
114  ObjectName* CopyOnWrite() { \
115  ICHECK(data_ != nullptr); \
116  if (!data_.unique()) { \
117  auto n = make_object<ObjectName>(*(operator->())); \
118  ObjectPtr<Object>(std::move(n)).swap(data_); \
119  } \
120  return static_cast<ObjectName*>(data_.get()); \
121  }
122 
123 /*
124  * \brief Define object reference methods.
125  * \param TypeName The object type name
126  * \param ParentType The parent type of the objectref
127  * \param ObjectName The type name of the object.
128  */
129 #define TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR(TypeName, ParentType, \
130  ObjectName) \
131  explicit TypeName(::tvm::ffi::ObjectPtr<::tvm::ffi::Object> n) : ParentType(n) {} \
132  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
133  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
134  const ObjectName* get() const { return operator->(); } \
135  using ContainerType = ObjectName;
136 
137 #define TVM_DECLARE_BASE_OBJECT_INFO TVM_FFI_DECLARE_BASE_OBJECT_INFO
138 #define TVM_DECLARE_FINAL_OBJECT_INFO TVM_FFI_DECLARE_FINAL_OBJECT_INFO
139 #define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS
140 
141 #define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS TVM_FFI_DEFINE_MUTABLE_OBJECT_REF_METHODS
142 #define TVM_DEFINE_OBJECT_REF_METHODS TVM_FFI_DEFINE_OBJECT_REF_METHODS
143 #define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS \
144  TVM_FFI_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS
145 
146 #define TVM_STR_CONCAT_(__x, __y) __x##__y
147 #define TVM_STR_CONCAT(__x, __y) TVM_STR_CONCAT_(__x, __y)
148 
149 } // namespace runtime
150 
151 using tvm::ffi::ObjectPtr;
152 using tvm::ffi::ObjectRef;
153 } // namespace tvm
154 #endif // TVM_RUNTIME_OBJECT_H_
TypeIndex
Namespace for the list of type index.
Definition: object.h:50
@ kRuntimeDiscoDRef
runtime::DRef for disco distributed runtime
Definition: object.h:65
@ kRuntimeMap
Definition: object.h:70
@ kRuntimeNDArray
runtime::NDArray.
Definition: object.h:56
@ kStaticIndexEnd
Definition: object.h:73
@ kRuntimePackedFunc
ffi::Function.
Definition: object.h:63
@ kRuntimeShape
runtime::Shape.
Definition: object.h:58
@ kRuntimeArray
Definition: object.h:71
@ kRuntimeRPCObjectRef
runtime::RPCObjectRef
Definition: object.h:67
@ kRuntimeString
Definition: object.h:69
@ kCustomStaticIndex
Definition: object.h:61
@ kRuntimeModule
runtime::Module.
Definition: object.h:54
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37