tvm
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  TVM_FFI_ICHECK(can_dispatch(n))
101  << "NodeFunctor calls un-registered function on type " << 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  TVM_FFI_ICHECK(func_[tindex] == nullptr)
117  << "Dispatch for " << TNode::_type_key << " is already set";
118  TVM_FFI_ICHECK_EQ(begin_type_index_, 0) << " Cannot call set_dispatch after calling Finalize";
119  func_[tindex] = f;
120  return *this;
121  }
128  template <typename TNode>
129  TSelf& clear_dispatch() { // NOLINT(*)
130  uint32_t tindex = TNode::RuntimeTypeIndex();
131  TVM_FFI_ICHECK_LT(tindex, func_.size()) << "clear_dispatch: index out of range";
132  TVM_FFI_ICHECK_EQ(begin_type_index_, 0) << " Cannot call clear_dispatch after calling Finalize";
133  func_[tindex] = nullptr;
134  return *this;
135  }
141  void Finalize() {
142  TVM_FFI_ICHECK_EQ(begin_type_index_, 0) << "Can only call Finalize once";
143  while (begin_type_index_ < func_.size() && func_[begin_type_index_] == nullptr) {
144  ++begin_type_index_;
145  }
146  // shift up the function value
147  size_t new_ftable_size = func_.size() - begin_type_index_;
148  if (begin_type_index_ != 0) {
149  std::memmove(func_.data(), func_.data() + begin_type_index_,
150  new_ftable_size * sizeof(FPointer));
151  }
152  func_.resize(new_ftable_size);
153  func_.shrink_to_fit();
154  }
155 };
156 
157 #define TVM_REG_FUNC_VAR_DEF(ClsName) static TVM_ATTRIBUTE_UNUSED auto& __make_functor##_##ClsName
158 
200 #define TVM_STATIC_IR_FUNCTOR(ClsName, FField) \
201  TVM_STR_CONCAT(TVM_REG_FUNC_VAR_DEF(ClsName), __COUNTER__) = ClsName::FField()
202 } // namespace tvm
203 #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:129
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:141
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
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
A managed object in the TVM runtime.