19#ifndef TVM_SCRIPT_PRINTER_IR_DOCSIFIER_FUNCTOR_H_
20#define TVM_SCRIPT_PRINTER_IR_DOCSIFIER_FUNCTOR_H_
22#include <tvm/ffi/function.h>
23#include <tvm/runtime/logging.h>
28#include <unordered_map>
41template <
typename R,
typename... Args>
46 template <
class TObjectRef,
class TCallable>
47 using IsDispatchFunction =
62 template <
class TObjectRef>
65 const ffi::Function*
pf =
nullptr;
69 if ((
pf = LookupDispatchTable(
"",
type_index)) !=
nullptr) {
72 if ((
pf = LookupFallback()) !=
nullptr) {
76 LOG(
WARNING) <<
"ObjectFunctor calls un-registered function on type: "
77 << ffi::Object::TypeIndex2Key(
type_index) <<
" (token: " <<
token <<
")"
78 <<
". ObjectType: " <<
obj->GetTypeKey() <<
". Object: " <<
obj;
79 TVM_FFI_ICHECK(
false) <<
"ObjectFunctor calls un-registered function on type: "
80 << ffi::Object::TypeIndex2Key(
type_index) <<
" (token: " <<
token <<
")"
81 <<
". ObjectType: " <<
obj->GetTypeKey() <<
". Object: " <<
obj;
95 std::vector<ffi::Function>*
table = &dispatch_table_[
token];
100 if (
slot !=
nullptr) {
101 TVM_FFI_ICHECK(
false) <<
"Dispatch for type is already registered: "
109 TVM_FFI_ICHECK(!dispatch_fallback_.has_value()) <<
"Fallback is already defined";
110 dispatch_fallback_ =
f;
122 typename = std::enable_if_t<IsDispatchFunction<TObjectRef, TCallable>::value>>
129 typename = std::enable_if_t<IsDispatchFunction<ffi::ObjectRef, TCallable>::value>>
131 ffi::Function func = ffi::TypedFunction<
R(ffi::ObjectRef,
Args...)>(
f);
144 std::vector<ffi::Function>*
table = &dispatch_table_[
token];
159 auto it = dispatch_table_.find(
token);
160 if (
it == dispatch_table_.end()) {
163 const std::vector<ffi::Function>&
tab =
it->second;
178 const ffi::Function* LookupFallback()
const {
179 if (dispatch_fallback_.has_value()) {
180 return &*dispatch_fallback_;
190 using DispatchTable = std::unordered_map<std::string, std::vector<ffi::Function>>;
192 DispatchTable dispatch_table_;
193 std::optional<ffi::Function> dispatch_fallback_;
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Dynamic dispatch functor based on AccessPath.
Definition ir_docsifier_functor.h:42
void remove_dispatch(ffi::String token, uint32_t type_index)
Remove dispatch function.
Definition ir_docsifier_functor.h:143
R operator()(const ffi::String &token, TObjectRef obj, Args... args) const
Call the dispatch function.
Definition ir_docsifier_functor.h:63
TSelf & set_dispatch(ffi::String token, uint32_t type_index, ffi::Function f)
Set the dispatch function.
Definition ir_docsifier_functor.h:94
TSelf & set_fallback(TCallable f)
Definition ir_docsifier_functor.h:130
void remove_fallback()
Definition ir_docsifier_functor.h:114
TSelf & set_dispatch(ffi::String token, TCallable f)
Set the dispatch function.
Definition ir_docsifier_functor.h:123
TSelf & set_fallback(ffi::Function f)
Definition ir_docsifier_functor.h:108
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40