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 EnvFuncNode : 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<EnvFuncNode>()
57 .def_ro("name", &EnvFuncNode::name)
58 .def_ro("func", &EnvFuncNode::func, refl::AttachFieldFlag::SEqHashIgnore());
59 }
60
63};
64
69class EnvFunc : public ffi::ObjectRef {
70 public:
72 explicit EnvFunc(ffi::ObjectPtr<ffi::Object> n) : ffi::ObjectRef(n) {}
76 explicit EnvFunc(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
78 const EnvFuncNode* operator->() const { return static_cast<const EnvFuncNode*>(get()); }
84 template <typename... Args>
85 ffi::Any operator()(Args&&... args) const {
86 const EnvFuncNode* n = operator->();
87 TVM_FFI_ICHECK(n != nullptr);
88 return n->func(std::forward<Args>(args)...);
89 }
96 TVM_DLL static EnvFunc Get(const ffi::String& name);
99};
100
104template <typename FType>
106
116template <typename R, typename... Args>
117class TypedEnvFunc<R(Args...)> : public ffi::ObjectRef {
118 public:
120 using TSelf = TypedEnvFunc<R(Args...)>;
122 explicit TypedEnvFunc(ffi::ObjectPtr<ffi::Object> n) : ffi::ObjectRef(n) {}
126 explicit TypedEnvFunc(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
133 ffi::ObjectRef::operator=(other);
134 return *this;
135 }
137 const EnvFuncNode* operator->() const { return static_cast<const EnvFuncNode*>(get()); }
143 R operator()(Args... args) const {
144 const EnvFuncNode* n = operator->();
145 TVM_FFI_ICHECK(n != nullptr);
146 if constexpr (std::is_same_v<R, void>) {
147 n->func(std::forward<Args>(args)...);
148 } else {
149 ffi::Any res = n->func(std::forward<Args>(args)...);
150 if constexpr (std::is_same_v<R, ffi::Any>) {
151 return res;
152 } else {
153 return std::move(res).cast<R>();
154 }
155 }
156 }
159};
160
161} // namespace tvm
162#endif // TVM_IR_ENV_FUNC_H_
A serializable function backed by TVM's global environment.
Definition env_func.h:44
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
EnvFuncNode()
constructor
Definition env_func.h:51
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", EnvFuncNode, ffi::Object)
Managed reference to EnvFuncNode.
Definition env_func.h:69
EnvFunc(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition env_func.h:76
static EnvFunc Get(const ffi::String &name)
Get a global function based on the name.
EnvFunc()
Definition env_func.h:71
EnvFunc(ffi::ObjectPtr< ffi::Object > n)
Definition env_func.h:72
const EnvFuncNode * operator->() const
Definition env_func.h:78
ffi::Any operator()(Args &&... args) const
Invoke the function.
Definition env_func.h:85
A typed version of EnvFunc. It is backed by a GlobalFuncNode internally.
Definition env_func.h:117
TypedEnvFunc()
Definition env_func.h:121
const EnvFuncNode * operator->() const
Definition env_func.h:137
TypedEnvFunc(ffi::ObjectPtr< ffi::Object > n)
Definition env_func.h:122
R operator()(Args... args) const
Invoke the function.
Definition env_func.h:143
TypedEnvFunc(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition env_func.h:126
TSelf & operator=(const EnvFunc &other)
Assign global function to a TypedEnvFunc.
Definition env_func.h:132
Please refer to TypedEnvFunc<R(Args..)>.
Definition env_func.h:105
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40