Questions about codegen?

Hi, everyone ! I have a few questions about the following code in include/tvm/tir/stmt_functor.h

template <typename TNode>
  TSelf& set_dispatch(FPointer f) {  // NOLINT(*)
    uint32_t tindex = TNode::RuntimeTypeIndex();
    if (func_.size() <= tindex) {
      func_.resize(tindex + 1, nullptr);
    }
    ICHECK(func_[tindex] == nullptr) << "Dispatch for " << TNode::_type_key << " is already set";
    func_[tindex] = f;
    return *this;
  }

#define IR_STMT_FUNCTOR_DISPATCH(OP)                                                       \
  vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self, Args... args) {     \
    return self->VisitStmt_(static_cast<const OP*>(n.get()), std::forward<Args>(args)...); \
  });

static FType InitVTable() {
    FType vtable;
    IR_STMT_FUNCTOR_DISPATCH(LetStmtNode);
    IR_STMT_FUNCTOR_DISPATCH(AttrStmtNode);
    IR_STMT_FUNCTOR_DISPATCH(IfThenElseNode);
    IR_STMT_FUNCTOR_DISPATCH(ForNode);
    IR_STMT_FUNCTOR_DISPATCH(AllocateNode);
    IR_STMT_FUNCTOR_DISPATCH(StoreNode);
    IR_STMT_FUNCTOR_DISPATCH(AssertStmtNode);
    IR_STMT_FUNCTOR_DISPATCH(ProducerStoreNode);
    IR_STMT_FUNCTOR_DISPATCH(ProducerRealizeNode);
    IR_STMT_FUNCTOR_DISPATCH(PrefetchNode);
    IR_STMT_FUNCTOR_DISPATCH(SeqStmtNode);
    IR_STMT_FUNCTOR_DISPATCH(EvaluateNode);
    IR_STMT_FUNCTOR_DISPATCH(BufferStoreNode);
    IR_STMT_FUNCTOR_DISPATCH(BufferRealizeNode);
    IR_STMT_FUNCTOR_DISPATCH(BlockNode);
    IR_STMT_FUNCTOR_DISPATCH(BlockRealizeNode);
    return vtable;
  }

 virtual R VisitStmt(const Stmt& n, Args... args) {                                                                                                                                
           static FType vtable = InitVTable();                                                                                                                                                
           return vtable(n, this, std::forward<Args>(args)...);
}

a) I can not find the template member in vtable, so I can not understand the meaning of vtable.template in IR_STMT_FUNCTOR_DISPATCH, why not just call for set_dispatch;

b)when I step in vtable(n, this, std::forward<Args>(args)...); It will find the specific function in vtable.func_ to run according to stmt n index. It should have no more than 20 functions in vtable.func_ following InitVTable(), but when I debug in gdb, it has more than 100 functions, so why ?

c) I am reading the tvm codegen source code, and I find VisitStmt function is used for transform the function body, now I get stuck in VisitStmt, I can not find the specific function to finish function body’s transforming.

Thanks in advance.

I found the answer about b) and c), but still can not understand a), can anyone give some explanation ? thanks.

template is a keyword in c++, “vtable.template set_dispatch” means vtable.set_dispatch is a template function and the compiler can’t treat it as a member variable.