Skip to content

Commit 4f2661f

Browse files
committed
[RUNTIME][OBJECT] Introduce static slots for common objects.
The _type_child_slots can be used to enable quick type checking optimization by checking the whether the type index is within the bound. This PR enables these static slots: - Introduce a static assert to avoid the scenario when a developer forget to _type_child_slots when the field is set for the type's parent. - Revamp and assign static type index to common runtime objects - Add a DumpTypeTable call to allow developer monitor the current situation of type table and offers suggestions for the slots(ideally the slots equals the number of children so there is no overflow.
1 parent 3ab3751 commit 4f2661f

File tree

21 files changed

+93
-32
lines changed

21 files changed

+93
-32
lines changed

include/tvm/ir/expr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ namespace tvm {
4242
*/
4343
class BaseExprNode : public Object {
4444
public:
45-
static constexpr const char* _type_key = "Expr";
45+
static constexpr const char* _type_key = "BaseExpr";
4646
static constexpr const bool _type_has_method_sequal_reduce = true;
4747
static constexpr const bool _type_has_method_shash_reduce = true;
48+
static constexpr const uint32_t _type_child_slots = 58;
4849
TVM_DECLARE_BASE_OBJECT_INFO(BaseExprNode, Object);
4950
};
5051

@@ -88,6 +89,7 @@ class PrimExprNode : public BaseExprNode {
8889
DataType dtype;
8990

9091
static constexpr const char* _type_key = "PrimExpr";
92+
static constexpr const uint32_t _type_child_slots = 34;
9193
TVM_DECLARE_BASE_OBJECT_INFO(PrimExprNode, BaseExprNode);
9294
};
9395

@@ -161,7 +163,8 @@ class RelayExprNode : public BaseExprNode {
161163
template<typename TTypeNode>
162164
inline const TTypeNode* type_as() const;
163165

164-
static constexpr const char* _type_key = "relay.Expr";
166+
static constexpr const char* _type_key = "RelayExpr";
167+
static constexpr const uint32_t _type_child_slots = 22;
165168
TVM_DECLARE_BASE_OBJECT_INFO(RelayExprNode, BaseExprNode);
166169
};
167170

include/tvm/ir/function.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class BaseFuncNode : public RelayExprNode {
140140
}
141141

142142
static constexpr const char* _type_key = "BaseFunc";
143+
static constexpr const uint32_t _type_child_slots = 2;
143144
TVM_DECLARE_BASE_OBJECT_INFO(BaseFuncNode, RelayExprNode);
144145
};
145146

include/tvm/ir/tensor_type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace tvm {
3636
class BaseTensorTypeNode : public TypeNode {
3737
public:
3838
static constexpr const char* _type_key = "relay.BaseTensorType";
39+
static constexpr const uint32_t _type_child_slots = 1;
3940
TVM_DECLARE_BASE_OBJECT_INFO(BaseTensorTypeNode, TypeNode);
4041
};
4142

include/tvm/ir/type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class TypeNode : public Object {
8181
static constexpr const char* _type_key = "Type";
8282
static constexpr const bool _type_has_method_sequal_reduce = true;
8383
static constexpr const bool _type_has_method_shash_reduce = true;
84+
static constexpr const uint32_t _type_child_slots = 14;
8485
TVM_DECLARE_BASE_OBJECT_INFO(TypeNode, Object);
8586
};
8687

@@ -391,6 +392,7 @@ inline bool IsVoidType(const Type& type) {
391392
class TypeConstraintNode : public TypeNode {
392393
public:
393394
static constexpr const char* _type_key = "TypeConstraint";
395+
static constexpr const uint32_t _type_child_slots = 1;
394396
TVM_DECLARE_BASE_OBJECT_INFO(TypeConstraintNode, TypeNode);
395397
};
396398

include/tvm/relay/expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ class TempExprNode : public ExprNode {
630630
static constexpr const char* _type_key = "relay.TempExpr";
631631
static constexpr const bool _type_has_method_sequal_reduce = false;
632632
static constexpr const bool _type_has_method_shash_reduce = false;
633+
static constexpr const uint32_t _type_child_slots = 0;
633634
TVM_DECLARE_BASE_OBJECT_INFO(TempExprNode, ExprNode);
634635
};
635636

include/tvm/runtime/container.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ class ADTObj : public Object, public InplaceArrayBase<ADTObj, ObjectRef> {
200200
uint32_t size;
201201
// The fields of the structure follows directly in memory.
202202

203-
static constexpr const uint32_t _type_index = TypeIndex::kVMADT;
204-
static constexpr const char* _type_key = "vm.ADT";
203+
static constexpr const uint32_t _type_index = TypeIndex::kRuntimeADT;
204+
static constexpr const char* _type_key = "runtime.ADT";
205205
TVM_DECLARE_FINAL_OBJECT_INFO(ADTObj, Object);
206206

207207
private:
@@ -314,7 +314,7 @@ class StringObj : public Object {
314314
/*! \brief The length of the string object. */
315315
uint64_t size;
316316

317-
static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
317+
static constexpr const uint32_t _type_index = TypeIndex::kRuntimeString;
318318
static constexpr const char* _type_key = "runtime.String";
319319
TVM_DECLARE_FINAL_OBJECT_INFO(StringObj, Object);
320320

include/tvm/runtime/ndarray.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ class NDArray::Container :
288288
using Object::IncRef;
289289

290290
// Information for object protocol.
291-
static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
291+
static constexpr const uint32_t _type_index = TypeIndex::kRuntimeNDArray;
292292
static constexpr const uint32_t _type_child_slots = 0;
293293
static constexpr const uint32_t _type_child_slots_can_overflow = true;
294-
static constexpr const char* _type_key = "NDArray";
294+
static constexpr const char* _type_key = "runtime.NDArray";
295295
TVM_DECLARE_BASE_OBJECT_INFO(NDArray::Container, Object);
296296

297297
protected:

include/tvm/runtime/object.h

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,31 @@
4646
namespace tvm {
4747
namespace runtime {
4848

49-
/*! \brief list of the type index. */
50-
enum TypeIndex {
51-
/*! \brief Root object type. */
52-
kRoot = 0,
53-
kClosure = 1,
54-
kVMADT = 2,
55-
kRuntimeModule = 3,
56-
kStaticIndexEnd,
57-
/*! \brief Type index is allocated during runtime. */
58-
kDynamic = kStaticIndexEnd
59-
};
49+
/*!
50+
* \brief Namespace for the list of type index.
51+
* \note Use struct so that we have to use TypeIndex::ENumName to refer to
52+
* the constant, but still able to use enum.
53+
*/
54+
struct TypeIndex {
55+
enum {
56+
/*! \brief Root object type. */
57+
kRoot = 0,
58+
// Standard static index assignments,
59+
// Frontends can take benefit of these constants.
60+
/*! \brief runtime::Module. */
61+
kRuntimeModule = 1,
62+
/*! \brief runtime::NDArray. */
63+
kRuntimeNDArray = 2,
64+
/*! \brief runtime::String. */
65+
kRuntimeString = 3,
66+
// static assignments that may subject to change.
67+
kRuntimeClosure,
68+
kRuntimeADT,
69+
kStaticIndexEnd,
70+
/*! \brief Type index is allocated during runtime. */
71+
kDynamic = kStaticIndexEnd
72+
};
73+
}; // namespace TypeIndex
6074

6175
/*!
6276
* \brief base class of all object containers.
@@ -198,7 +212,7 @@ class Object {
198212
using RefCounterType = int32_t;
199213
#endif
200214

201-
static constexpr const char* _type_key = "Object";
215+
static constexpr const char* _type_key = "runtime.Object";
202216

203217
static uint32_t _GetOrAllocRuntimeTypeIndex() {
204218
return TypeIndex::kRoot;
@@ -675,6 +689,10 @@ struct ObjectEqual {
675689
#define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \
676690
static_assert(!ParentType::_type_final, "ParentObj maked as final"); \
677691
static uint32_t RuntimeTypeIndex() { \
692+
static_assert(TypeName::_type_child_slots == 0 || \
693+
ParentType::_type_child_slots == 0 || \
694+
TypeName::_type_child_slots < ParentType::_type_child_slots, \
695+
"Need to set _type_child_slots when parent specifies it."); \
678696
if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \
679697
return TypeName::_type_index; \
680698
} \
@@ -690,6 +708,7 @@ struct ObjectEqual {
690708
return tidx; \
691709
} \
692710

711+
693712
/*!
694713
* \brief helper macro to declare type information in a final class.
695714
* \param TypeName The name of the current type.

include/tvm/runtime/packed_func.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,8 @@ struct unpack_call_dispatcher<void, 0, index, F> {
12681268

12691269
template<typename R, int nargs, typename F>
12701270
inline void unpack_call(const F& f, const TVMArgs& args, TVMRetValue* rv) {
1271+
CHECK_EQ(nargs, args.size())
1272+
<< "Expect " << nargs << " arguments but get " << args.size();
12711273
unpack_call_dispatcher<R, nargs, 0, F>::run(f, args, rv);
12721274
}
12731275

include/tvm/runtime/vm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ namespace vm {
4444
*/
4545
class ClosureObj : public Object {
4646
public:
47-
static constexpr const uint32_t _type_index = TypeIndex::kClosure;
48-
static constexpr const char* _type_key = "Closure";
47+
static constexpr const uint32_t _type_index = TypeIndex::kRuntimeClosure;
48+
static constexpr const char* _type_key = "runtime.Closure";
4949
TVM_DECLARE_BASE_OBJECT_INFO(ClosureObj, Object);
5050
};
5151

0 commit comments

Comments
 (0)