tvm
Loading...
Searching...
No Matches
function.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_FUNCTION_H_
25#define TVM_IR_FUNCTION_H_
26
27#include <tvm/ffi/container/array.h>
28#include <tvm/ffi/container/map.h>
29#include <tvm/ffi/string.h>
30#include <tvm/ir/attrs.h>
31#include <tvm/ir/expr.h>
32
33#include <string>
34#include <type_traits>
35
36namespace tvm {
37
38class Target;
39
46enum class CallingConv : int {
53 kDefault = 0,
60 kCPackedFunc = 1,
68};
69
73enum class LinkageType : int {
77 kInternal = 0,
82 kExternal = 1
83};
84
94class BaseFuncObj : public ExprObj {
95 public:
98
118 template <typename TObjectRef>
119 ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key,
120 ffi::Optional<TObjectRef> default_value = std::nullopt) const {
121 return attrs.GetAttr(attr_key, default_value);
122 }
129 template <typename T>
130 ffi::Optional<T> GetAttr(ffi::reflection::AttrKey<T> key,
131 ffi::Optional<typename ffi::reflection::AttrKey<T>::ValueType>
132 default_value = std::nullopt) const {
133 return attrs.GetAttr(key, std::move(default_value));
134 }
136 template <typename T>
137 ffi::Optional<T> GetAttr(ffi::reflection::AttrKey<T> key,
138 typename ffi::reflection::AttrKey<T>::ValueType default_value) const {
139 return GetAttr(key, ffi::Optional<T>(std::move(default_value)));
140 }
141
142 // variant that uses TObjectRef to enable implicit conversion to default value.
143 template <typename TObjectRef>
144 ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
145 return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
146 }
147
167 bool HasNonzeroAttr(const std::string& attr_key) const { return attrs.HasNonzeroAttr(attr_key); }
188
189 static void RegisterReflection() {
190 namespace refl = tvm::ffi::reflection;
191 refl::ObjectDef<BaseFuncObj>().def_ro("attrs", &BaseFuncObj::attrs);
192 }
193
194 static constexpr const uint32_t _type_child_slots = 2;
196};
197
202class BaseFunc : public Expr {
203 public:
204 explicit BaseFunc(ffi::ObjectPtr<BaseFuncObj> node) : Expr(std::move(node)) {}
205
207};
208
214namespace attr {
220inline constexpr ffi::reflection::AttrKey<bool> kDeclaration{"declaration"};
221
229inline constexpr ffi::reflection::AttrKey<CallingConv> kCallingConv{"calling_conv"};
230
238inline constexpr ffi::reflection::AttrKey<Target> kTarget{"target"};
239
259inline constexpr ffi::reflection::AttrKey<ffi::String> kGlobalSymbol{"global_symbol"};
260
267inline constexpr ffi::reflection::AttrKey<ffi::String> kScriptNamespace{"script.namespace"};
268
269} // namespace attr
270
277
278} // namespace tvm
279#endif // TVM_IR_FUNCTION_H_
Base expr nodes in TVM.
Helpers for attribute objects.
Base node of all functions.
Definition function.h:94
LinkageType GetLinkageType() const
Get the type of the linkage.
Definition function.h:271
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, ffi::Optional< typename ffi::reflection::AttrKey< T >::ValueType > default_value=std::nullopt) const
Get an attribute with its value type deduced only from the key.
Definition function.h:130
DictAttrs attrs
Additional attributes storing the meta-data.
Definition function.h:97
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition function.h:144
static constexpr const uint32_t _type_child_slots
Definition function.h:194
TVM_FFI_DECLARE_OBJECT_INFO("ir.BaseFunc", BaseFuncObj, ExprObj)
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, typename ffi::reflection::AttrKey< T >::ValueType default_value) const
Accept a default convertible to the key's value type.
Definition function.h:137
static void RegisterReflection()
Definition function.h:189
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=std::nullopt) const
Get a function attribute.
Definition function.h:119
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition function.h:167
Managed reference to BaseFuncObj.
Definition function.h:202
BaseFunc(ffi::ObjectPtr< BaseFuncObj > node)
Definition function.h:204
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BaseFunc, Expr, BaseFuncObj)
Managed reference to DictAttrsObj.
Definition attrs.h:108
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=ffi::Optional< TObjectRef >(std::nullopt)) const
Get a function attribute.
Definition attrs.h:171
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition attrs.h:225
Base type of all the expressions.
Definition expr.h:66
Managed reference to ExprObj.
Definition expr.h:101
constexpr ffi::reflection::AttrKey< bool > kDeclaration
Whether this function is a declaration.
Definition function.h:220
constexpr ffi::reflection::AttrKey< CallingConv > kCallingConv
Indicates the special calling convention.
Definition function.h:229
constexpr ffi::reflection::AttrKey< ffi::String > kScriptNamespace
Script namespace used to select construction, printing, and dialect lowering. Absent metadata selects...
Definition function.h:267
constexpr ffi::reflection::AttrKey< ffi::String > kGlobalSymbol
Global linker symbol of the function in generated code.
Definition function.h:259
constexpr ffi::reflection::AttrKey< Target > kTarget
Compilation target of the function.
Definition function.h:238
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
CallingConv
Possible Calling conventions.
Definition function.h:46
@ kDefault
Default calling convention.
@ kCPackedFunc
ffi::Function that exposes a Cffi::Function signature.
@ kDeviceKernelLaunch
Device kernel launch.
LinkageType
Supported linkage types.
Definition function.h:73
@ kExternal
External linkage.
@ kInternal
Internal linkage.