tvm
Classes | Namespaces | Macros
functor.h File Reference

Defines the Functor data structures. More...

#include <dmlc/logging.h>
#include <tvm/runtime/object.h>
#include <type_traits>
#include <utility>
#include <vector>
Include dependency graph for 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 ObjectRef &n, Args...)>
 

Namespaces

 tvm
 runtime implementation for LibTorch/TorchScript.
 

Macros

#define TVM_REG_FUNC_VAR_DEF(ClsName)   static TVM_ATTRIBUTE_UNUSED auto& __make_functor##_##ClsName
 
#define TVM_STATIC_IR_FUNCTOR(ClsName, FField)    TVM_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)    static TVM_ATTRIBUTE_UNUSED auto& __make_functor##_##ClsName

◆ TVM_STATIC_IR_FUNCTOR

#define TVM_STATIC_IR_FUNCTOR (   ClsName,
  FField 
)     TVM_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 ReprPrinter similar to Visitor Pattern.
// vtable allows easy patch of new Node types, without changing
// interface of ReprPrinter.
class ReprPrinter {
public:
std::ostream& stream;
// the dispatch function.
void print(Expr e) {
const static FType& f = *vtable();
f(e, this);
}
using FType = NodeFunctor<void (const ObjectRef&, ReprPrinter* )>;
// function to return global function table
static FType& vtable();
};
// in cpp/cc file
ReprPrinter::FType& ReprPrinter::vtable() { // NOLINT(*)
static FType inst; return inst;
}
TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
.set_dispatch<Add>([](const ObjectRef& ref, ReprPrinter* p) {
auto* n = static_cast<const Add*>(ref.get());
p->print(n->a);
p->stream << '+'
p->print(n->b);
});
#define TVM_STATIC_IR_FUNCTOR(ClsName, FField)
Useful macro to set NodeFunctor dispatch in a global static field.
Definition: functor.h:173
RelayExpr Expr
Definition: expr.h:37
Parameters
ClsNameThe name of the class
FFieldThe static function that returns a singleton of NodeFunctor.