tvm
Classes | Namespaces | Macros
node_functor.h File Reference

Defines the Functor data structures. More...

#include <tvm/ffi/error.h>
#include <cstring>
#include <type_traits>
#include <utility>
#include <vector>
Include dependency graph for node_functor.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  tvm::NodeFunctor< R(const ffi::ObjectRef &n, Args...)>
 

Namespaces

 tvm
 An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
 

Macros

#define TVM_REG_FUNC_VAR_DEF(ClsName)   [[maybe_unused]] static auto& __make_functor##_##ClsName
 
#define TVM_STATIC_IR_FUNCTOR(ClsName, FField)    TVM_FFI_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()
 Useful macro to set NodeFunctor dispatch in a global static field. More...
 

Detailed Description

Defines the Functor data structures.

Macro Definition Documentation

◆ TVM_REG_FUNC_VAR_DEF

#define TVM_REG_FUNC_VAR_DEF (   ClsName)    [[maybe_unused]] static auto& __make_functor##_##ClsName

◆ TVM_STATIC_IR_FUNCTOR

#define TVM_STATIC_IR_FUNCTOR (   ClsName,
  FField 
)     TVM_FFI_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()

Useful macro to set NodeFunctor dispatch in a global static field.

// Use NodeFunctor to implement TVMScriptPrinter similar to Visitor Pattern.
// vtable allows easy patch of new Node types, without changing
// the interface of TVMScriptPrinter.
class TVMScriptPrinter {
public:
// the dispatch function.
static std::string Script(const ffi::ObjectRef& node, const PrinterConfig& cfg) {
return vtable()(node, cfg);
}
using FType = NodeFunctor<std::string(const ffi::ObjectRef&, const PrinterConfig&)>;
// function to return global function table
static FType& vtable();
};
// in cpp/cc file
TVMScriptPrinter::FType& TVMScriptPrinter::vtable() {
static FType inst; return inst;
}
TVM_STATIC_IR_FUNCTOR(TVMScriptPrinter, vtable)
.set_dispatch<AddNode>([](const ffi::ObjectRef& ref, const PrinterConfig& cfg) {
auto* n = static_cast<const AddNode*>(ref.get());
return Script(n->a, cfg) + " + " + Script(n->b, cfg);
});
#define TVM_STATIC_IR_FUNCTOR(ClsName, FField)
Useful macro to set NodeFunctor dispatch in a global static field.
Definition: node_functor.h:191
Parameters
ClsNameThe name of the class
FFieldThe static function that returns a singleton of NodeFunctor.