tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
functor.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
23 #ifndef TVM_NODE_FUNCTOR_H_
24 #define TVM_NODE_FUNCTOR_H_
25 
26 #include <tvm/runtime/logging.h>
27 #include <tvm/runtime/object.h>
28 
29 #include <cstring>
30 #include <type_traits>
31 #include <utility>
32 #include <vector>
33 
34 namespace tvm {
35 
36 using runtime::ObjectRef;
37 
64 template <typename FType>
66 
67 template <typename R, typename... Args>
68 class NodeFunctor<R(const ObjectRef& n, Args...)> {
69  private:
71  typedef R (*FPointer)(const ObjectRef& n, Args...);
73  using TSelf = NodeFunctor<R(const ObjectRef& n, Args...)>;
75  std::vector<FPointer> func_;
77  uint32_t begin_type_index_{0};
78 
79  public:
81  using result_type = R;
87  bool can_dispatch(const ObjectRef& n) const {
88  uint32_t type_index = n->type_index();
89  if (type_index < begin_type_index_) return false;
90  type_index -= begin_type_index_;
91  return type_index < func_.size() && func_[type_index] != nullptr;
92  }
99  R operator()(const ObjectRef& n, Args... args) const {
100  ICHECK(can_dispatch(n)) << "NodeFunctor calls un-registered function on type "
101  << n->GetTypeKey();
102  return (*func_[n->type_index() - begin_type_index_])(n, std::forward<Args>(args)...);
103  }
110  template <typename TNode>
111  TSelf& set_dispatch(FPointer f) { // NOLINT(*)
112  uint32_t tindex = TNode::RuntimeTypeIndex();
113  if (func_.size() <= tindex) {
114  func_.resize(tindex + 1, nullptr);
115  }
116  ICHECK(func_[tindex] == nullptr) << "Dispatch for " << TNode::_type_key << " is already set";
117  ICHECK_EQ(begin_type_index_, 0) << " Cannot call set_dispatch after calling Finalize";
118  func_[tindex] = f;
119  return *this;
120  }
127  template <typename TNode>
128  TSelf& clear_dispatch() { // NOLINT(*)
129  uint32_t tindex = TNode::RuntimeTypeIndex();
130  ICHECK_LT(tindex, func_.size()) << "clear_dispatch: index out of range";
131  ICHECK_EQ(begin_type_index_, 0) << " Cannot call clear_dispatch after calling Finalize";
132  func_[tindex] = nullptr;
133  return *this;
134  }
140  void Finalize() {
141  ICHECK_EQ(begin_type_index_, 0) << "Can only call Finalize once";
142  while (begin_type_index_ < func_.size() && func_[begin_type_index_] == nullptr) {
143  ++begin_type_index_;
144  }
145  // shift up the function value
146  size_t new_ftable_size = func_.size() - begin_type_index_;
147  if (begin_type_index_ != 0) {
148  std::memmove(func_.data(), func_.data() + begin_type_index_,
149  new_ftable_size * sizeof(FPointer));
150  }
151  func_.resize(new_ftable_size);
152  func_.shrink_to_fit();
153  }
154 };
155 
156 #define TVM_REG_FUNC_VAR_DEF(ClsName) static TVM_ATTRIBUTE_UNUSED auto& __make_functor##_##ClsName
157 
199 #define TVM_STATIC_IR_FUNCTOR(ClsName, FField) \
200  TVM_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()
201 } // namespace tvm
202 #endif // TVM_NODE_FUNCTOR_H_
TSelf & set_dispatch(FPointer f)
set the dispatcher for type TNode
Definition: functor.h:111
TSelf & clear_dispatch()
unset the dispatcher for type TNode
Definition: functor.h:128
R operator()(const ObjectRef &n, Args... args) const
invoke the functor, dispatch on type of n
Definition: functor.h:99
void Finalize()
Finalize the functor after calling sequence of set_dispatch This function will attempt to find the mi...
Definition: functor.h:140
bool can_dispatch(const ObjectRef &n) const
Whether the functor can dispatch the corresponding Node.
Definition: functor.h:87
R result_type
the result type of this functor
Definition: functor.h:81
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:65
Base class of all object reference.
Definition: object.h:520
uint32_t type_index() const
Definition: object.h:180
std::string GetTypeKey() const
Definition: object.h:185
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
A managed object in the TVM runtime.