23 #ifndef TVM_NODE_FUNCTOR_H_
24 #define TVM_NODE_FUNCTOR_H_
26 #include <tvm/runtime/logging.h>
30 #include <type_traits>
36 using runtime::ObjectRef;
64 template <
typename FType>
67 template <
typename R,
typename... Args>
71 typedef R (*FPointer)(
const ObjectRef& n, Args...);
75 std::vector<FPointer> func_;
77 uint32_t begin_type_index_{0};
89 if (type_index < begin_type_index_)
return false;
90 type_index -= begin_type_index_;
91 return type_index < func_.size() && func_[type_index] !=
nullptr;
100 ICHECK(can_dispatch(n)) <<
"NodeFunctor calls un-registered function on type "
102 return (*func_[n->
type_index() - begin_type_index_])(n, std::forward<Args>(args)...);
110 template <
typename TNode>
112 uint32_t tindex = TNode::RuntimeTypeIndex();
113 if (func_.size() <= tindex) {
114 func_.resize(tindex + 1,
nullptr);
116 ICHECK(func_[tindex] ==
nullptr) <<
"Dispatch for " << TNode::_type_key <<
" is already set";
117 ICHECK_EQ(begin_type_index_, 0) <<
" Cannot call set_dispatch after calling Finalize";
127 template <
typename TNode>
129 uint32_t tindex = TNode::RuntimeTypeIndex();
130 ICHECK_LT(tindex, func_.size()) <<
"clear_dispatch: index out of range";
131 ICHECK_EQ(begin_type_index_, 0) <<
" Cannot call clear_dispatch after calling Finalize";
132 func_[tindex] =
nullptr;
141 ICHECK_EQ(begin_type_index_, 0) <<
"Can only call Finalize once";
142 while (begin_type_index_ < func_.size() && func_[begin_type_index_] ==
nullptr) {
146 size_t new_ftable_size = func_.size() - begin_type_index_;
147 if (begin_type_index_ != 0) {
148 std::memmove(func_.data(), func_.data() + begin_type_index_,
149 new_ftable_size *
sizeof(FPointer));
151 func_.resize(new_ftable_size);
152 func_.shrink_to_fit();
156 #define TVM_REG_FUNC_VAR_DEF(ClsName) static TVM_ATTRIBUTE_UNUSED auto& __make_functor##_##ClsName
199 #define TVM_STATIC_IR_FUNCTOR(ClsName, FField) \
200 TVM_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()
TSelf & set_dispatch(FPointer f)
set the dispatcher for type TNode
Definition: functor.h:111
TSelf & clear_dispatch()
unset the dispatcher for type TNode
Definition: functor.h:128
R operator()(const ObjectRef &n, Args... args) const
invoke the functor, dispatch on type of n
Definition: functor.h:99
void Finalize()
Finalize the functor after calling sequence of set_dispatch This function will attempt to find the mi...
Definition: functor.h:140
bool can_dispatch(const ObjectRef &n) const
Whether the functor can dispatch the corresponding Node.
Definition: functor.h:87
R result_type
the result type of this functor
Definition: functor.h:81
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:65
Base class of all object reference.
Definition: object.h:520
uint32_t type_index() const
Definition: object.h:180
std::string GetTypeKey() const
Definition: object.h:185
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
A managed object in the TVM runtime.