23#ifndef TVM_IR_NODE_FUNCTOR_H_
24#define TVM_IR_NODE_FUNCTOR_H_
26#include <tvm/ffi/error.h>
61template <
typename FType>
64template <
typename R,
typename...
Args>
68 typedef R (*FPointer)(
const ffi::ObjectRef&
n,
Args...);
72 std::vector<FPointer> func_;
86 if (
type_index < begin_type_index_)
return false;
100 if (index < func_.size() && func_[index] !=
nullptr) {
101 return (*func_[index])(
n, std::forward<Args>(args)...);
106 for (
int32_t i = type_info->type_depth - 1;
i >= 0; --
i) {
107 type_index = type_info->type_ancestors[
i]->type_index;
110 if (index < func_.size() && func_[index] !=
nullptr) {
111 return (*func_[index])(
n, std::forward<Args>(args)...);
125 template <
typename TNode>
128 if (func_.size() <=
tindex) {
129 func_.resize(
tindex + 1,
nullptr);
132 <<
"Dispatch for " << TNode::_type_key <<
" is already set";
133 TVM_FFI_ICHECK_EQ(begin_type_index_, 0) <<
" Cannot call set_dispatch after calling Finalize";
143 template <
typename TNode>
147 TVM_FFI_ICHECK_EQ(begin_type_index_, 0) <<
" Cannot call clear_dispatch after calling Finalize";
158 while (begin_type_index_ < func_.size() && func_[begin_type_index_] ==
nullptr) {
163 if (begin_type_index_ != 0) {
164 std::memmove(func_.data(), func_.data() + begin_type_index_,
168 func_.shrink_to_fit();
172#define TVM_REG_FUNC_VAR_DEF(ClsName) [[maybe_unused]] static auto& __make_functor##_##ClsName
209#define TVM_STATIC_IR_FUNCTOR(ClsName, FField) \
210 TVM_FFI_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()
Definition node_functor.h:65
TSelf & set_dispatch(FPointer f)
set the dispatcher for type TNode
Definition node_functor.h:126
bool can_dispatch(const ffi::ObjectRef &n) const
Whether the functor can dispatch the corresponding Node.
Definition node_functor.h:84
void Finalize()
Finalize the functor after calling sequence of set_dispatch This function will attempt to find the mi...
Definition node_functor.h:156
R operator()(const ffi::ObjectRef &n, Args... args) const
invoke the functor, dispatch on type of n
Definition node_functor.h:96
TSelf & clear_dispatch()
unset the dispatcher for type TNode
Definition node_functor.h:144
A dynamically dispatched functor on the type of the first argument.
Definition node_functor.h:62
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40