tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
env_func.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  */
19 
24 #ifndef TVM_IR_ENV_FUNC_H_
25 #define TVM_IR_ENV_FUNC_H_
26 
27 #include <tvm/node/reflection.h>
29 
30 #include <string>
31 #include <utility>
32 
33 namespace tvm {
42 class EnvFuncNode : public Object {
43  public:
50 
51  void VisitAttrs(AttrVisitor* v) { v->Visit("name", &name); }
52 
53  bool SEqualReduce(const EnvFuncNode* other, SEqualReducer equal) const {
54  // name uniquely identifies the env function.
55  return name == other->name;
56  }
57 
58  void SHashReduce(SHashReducer hash_reduce) const {
59  // Name uniquely identifies the env function.
60  hash_reduce(name);
61  }
62 
63  static constexpr const char* _type_key = "EnvFunc";
64  static constexpr bool _type_has_method_sequal_reduce = true;
65  static constexpr bool _type_has_method_shash_reduce = true;
67 };
68 
73 class EnvFunc : public ObjectRef {
74  public:
75  EnvFunc() {}
76  explicit EnvFunc(ObjectPtr<Object> n) : ObjectRef(n) {}
78  const EnvFuncNode* operator->() const { return static_cast<const EnvFuncNode*>(get()); }
84  template <typename... Args>
85  runtime::TVMRetValue operator()(Args&&... args) const {
86  const EnvFuncNode* n = operator->();
87  ICHECK(n != nullptr);
88  return n->func(std::forward<Args>(args)...);
89  }
96  TVM_DLL static EnvFunc Get(const String& name);
99 };
100 
104 template <typename FType>
106 
116 template <typename R, typename... Args>
117 class TypedEnvFunc<R(Args...)> : public ObjectRef {
118  public:
120  using TSelf = TypedEnvFunc<R(Args...)>;
128  TSelf& operator=(const EnvFunc& other) {
129  ObjectRef::operator=(other);
130  return *this;
131  }
133  const EnvFuncNode* operator->() const { return static_cast<const EnvFuncNode*>(get()); }
139  R operator()(Args... args) const {
140  const EnvFuncNode* n = operator->();
141  ICHECK(n != nullptr);
142  return runtime::detail::typed_packed_call_dispatcher<R>::run(n->func,
143  std::forward<Args>(args)...);
144  }
147 };
148 
149 } // namespace tvm
150 #endif // TVM_IR_ENV_FUNC_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A serializable function backed by TVM's global environment.
Definition: env_func.h:42
String name
Unique name of the global function.
Definition: env_func.h:45
static constexpr const char * _type_key
Definition: env_func.h:63
TVM_DECLARE_FINAL_OBJECT_INFO(EnvFuncNode, Object)
EnvFuncNode()
constructor
Definition: env_func.h:49
runtime::PackedFunc func
The internal packed function.
Definition: env_func.h:47
static constexpr bool _type_has_method_sequal_reduce
Definition: env_func.h:64
bool SEqualReduce(const EnvFuncNode *other, SEqualReducer equal) const
Definition: env_func.h:53
void SHashReduce(SHashReducer hash_reduce) const
Definition: env_func.h:58
void VisitAttrs(AttrVisitor *v)
Definition: env_func.h:51
static constexpr bool _type_has_method_shash_reduce
Definition: env_func.h:65
Managed reference to EnvFuncNode.
Definition: env_func.h:73
EnvFunc(ObjectPtr< Object > n)
Definition: env_func.h:76
const EnvFuncNode * operator->() const
Definition: env_func.h:78
runtime::TVMRetValue operator()(Args &&... args) const
Invoke the function.
Definition: env_func.h:85
EnvFunc()
Definition: env_func.h:75
static EnvFunc Get(const String &name)
Get a global function based on the name.
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:135
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
A typed version of EnvFunc. It is backed by a GlobalFuncNode internally.
Definition: env_func.h:117
TypedEnvFunc(ObjectPtr< Object > n)
Definition: env_func.h:122
TypedEnvFunc()
Definition: env_func.h:121
TSelf & operator=(const EnvFunc &other)
Assign global function to a TypedEnvFunc.
Definition: env_func.h:128
const EnvFuncNode * operator->() const
Definition: env_func.h:133
R operator()(Args... args) const
Invoke the function.
Definition: env_func.h:139
Please refer to TypedEnvFunc<R(Args..)>.
Definition: env_func.h:105
A custom smart pointer for Object.
Definition: object.h:363
Base class of all object reference.
Definition: object.h:520
const Object * get() const
Definition: object.h:555
base class of all object containers.
Definition: object.h:172
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:141
Reference to string objects.
Definition: string.h:97
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Type-erased function used across TVM API.
Reflection and serialization of compiler IR/AST nodes.