tvm
Loading...
Searching...
No Matches
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/ffi/function.h>
28#include <tvm/ffi/reflection/registry.h>
29#include <tvm/runtime/base.h>
30
31#include <string>
32#include <utility>
33
34namespace tvm {
35
44class EnvFuncObj : public ffi::Object {
45 public:
47 ffi::String name;
49 ffi::Function func;
52
53 static void RegisterReflection() {
54 namespace refl = tvm::ffi::reflection;
55 // func do not participate in structural equal and hash.
56 refl::ObjectDef<EnvFuncObj>()
57 .def_ro("name", &EnvFuncObj::name)
58 .def_ro("func", &EnvFuncObj::func, refl::AttachFieldFlag::SEqHashIgnore());
59 }
60
61 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
62 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.EnvFunc", EnvFuncObj, ffi::Object);
63};
64
69class EnvFunc : public ffi::ObjectRef {
70 public:
71 static constexpr bool _type_is_nullable = false;
72 explicit EnvFunc(ffi::UnsafeInit tag, ffi::ObjectPtr<ffi::Object> n) : ffi::ObjectRef(tag) {
73 data_ = std::move(n);
74 }
78 explicit EnvFunc(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
80 const EnvFuncObj* operator->() const { return static_cast<const EnvFuncObj*>(get()); }
86 template <typename... Args>
87 ffi::Any operator()(Args&&... args) const {
88 const EnvFuncObj* n = operator->();
89 TVM_FFI_ICHECK(n != nullptr);
90 return n->func(std::forward<Args>(args)...);
91 }
98 TVM_TIRX_DLL static EnvFunc Get(const ffi::String& name);
101};
102
106template <typename FType>
108
118template <typename R, typename... Args>
119class TypedEnvFunc<R(Args...)> : public ffi::ObjectRef {
120 public:
122 using TSelf = TypedEnvFunc<R(Args...)>;
123 static constexpr bool _type_is_nullable = false;
124 TypedEnvFunc(const EnvFunc& other) : ffi::ObjectRef(other) {}
128 explicit TypedEnvFunc(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
134 TSelf& operator=(const EnvFunc& other) {
135 ffi::ObjectRef::operator=(other);
136 return *this;
137 }
139 const EnvFuncObj* operator->() const { return static_cast<const EnvFuncObj*>(get()); }
145 R operator()(Args... args) const {
146 const EnvFuncObj* n = operator->();
147 TVM_FFI_ICHECK(n != nullptr);
148 if constexpr (std::is_same_v<R, void>) {
149 n->func(std::forward<Args>(args)...);
150 } else {
151 ffi::Any res = n->func(std::forward<Args>(args)...);
152 if constexpr (std::is_same_v<R, ffi::Any>) {
153 return res;
154 } else {
155 return std::move(res).cast<R>();
156 }
157 }
158 }
161};
162
163} // namespace tvm
164#endif // TVM_IR_ENV_FUNC_H_
A serializable function backed by TVM's global environment.
Definition env_func.h:44
EnvFuncObj()
constructor
Definition env_func.h:51
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition env_func.h:61
ffi::Function func
The internal packed function.
Definition env_func.h:49
static void RegisterReflection()
Definition env_func.h:53
ffi::String name
Unique name of the global function.
Definition env_func.h:47
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.EnvFunc", EnvFuncObj, ffi::Object)
Managed reference to EnvFuncObj.
Definition env_func.h:69
EnvFunc(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition env_func.h:78
const EnvFuncObj * operator->() const
Definition env_func.h:80
EnvFunc(ffi::UnsafeInit tag, ffi::ObjectPtr< ffi::Object > n)
Definition env_func.h:72
static TVM_TIRX_DLL EnvFunc Get(const ffi::String &name)
Get a global function based on the name.
ffi::Any operator()(Args &&... args) const
Invoke the function.
Definition env_func.h:87
static constexpr bool _type_is_nullable
Definition env_func.h:71
A typed version of EnvFunc. It is backed by a GlobalFuncNode internally.
Definition env_func.h:119
TypedEnvFunc(const EnvFunc &other)
Definition env_func.h:124
const EnvFuncObj * operator->() const
Definition env_func.h:139
R operator()(Args... args) const
Invoke the function.
Definition env_func.h:145
TypedEnvFunc(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition env_func.h:128
TSelf & operator=(const EnvFunc &other)
Assign global function to a TypedEnvFunc.
Definition env_func.h:134
Please refer to TypedEnvFunc<R(Args..)>.
Definition env_func.h:107
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83