# Node System Refactor Proposal

This proposal is part of the unified object protocol RFC. The node refers to 
the base object structure for constructing AST/IR nodes as well as utilities 
manipulating them in the TVM stack.

Historically, the node folder contains a implementation of a smart 
pointer(NodePtr), a base object(Node) and a base reference type(NodeRef). On 
top of these data structures, we provide runtime checking and conversion. These 
features are now replaced by runtime::Object which allows further reuse by 
other runtime object types.

Besides the basic features provided by runtime::Object. We also provided 
additional features for AST/IR nodes. The important ones include:

- Reflection: being able to quickly access any field of the node into 
frontends(e.g. python).
- Serialization: any node objects can be serialized to json.
- Pretty printing: we can print out most of the node objects.

On one side, we could just put support for these features into runtime(e.g. 
make them part of Object). However, we need to keep in mind that the runtime 
needs to be minimum. Additionally, these features are mainly for building 
compiler infra.

Some of these features are previously scattered in the project, outside the 
node directory.
We propose to consolidate them into a centralized location -- the node folder.
These features are implemented in a dialect independent manner and serves as 
base infra for compiler.

## Remove vtable from Base Node

The current implementation of node reflection system requires child classes to 
override  a virtual function VisitAttrs. As mentioned in the unified object 
protocol proposal, one of our design goal is to remove vtable from common 
AST/IR Node classes. By doing so, these node classes can be directly exposed to 
other languages via C ABI -- which allows direct access to the fields of the 
node.

We offer an alternative solution to the c++ vtable by a columnar dispatch table 
indexed by the type index of the object. The data structure, ReflectionVTable, 
is a global singleton that keeps record of the key reflection-related functions 
for each object type. These functions are registered by TVM_REGISTER_NODE_TYPE. 
Note that we already use a similar data structure for node creation, we propose 
to unify them together. This change does requires a refactor to remove the 
final keyword from VisitAttrs functions(as they are no longer virtual).

We do not, however, forbid use of virtual functions for all node sub-classes. 
We only aim to expose the core IR/AST nodes for now. The sub-classes with 
virtual functions will still work fine as ususal as long as we do not want to 
expose them via ABI.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/dmlc/tvm/issues/4116#issuecomment-545729362

Reply via email to