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_TIR_FUNCTION_H_
25#define TVM_TIR_FUNCTION_H_
26
27#include <tvm/ffi/container/map.h>
28#include <tvm/ffi/container/variant.h>
29#include <tvm/ir/cow.h>
30#include <tvm/ir/function.h>
31#include <tvm/ir/prim/expr.h>
32#include <tvm/runtime/tensor.h>
33#include <tvm/tirx/expr.h>
34#include <tvm/tirx/stmt.h>
35
36#include <string>
37
38namespace tvm {
39namespace tirx {
40
49class FunctionNode : public BaseFuncNode {
50 public:
52 ffi::Array<tvm::Var> params;
56 ffi::Optional<SeqStmt> body;
57
58 static void RegisterReflection() {
59 namespace refl = tvm::ffi::reflection;
60 refl::ObjectDef<FunctionNode>()
61 .def_ro("params", &FunctionNode::params, refl::AttachFieldFlag::SEqHashDefPattern())
62 .def_ro("ret_type", &FunctionNode::ret_type)
63 .def_ro("body", &FunctionNode::body);
64 refl::TypeAttrDef<FunctionNode>()
65 .def(tvm::ffi::reflection::type_attr::kSEqual, &FunctionNode::SEqual)
66 .def(tvm::ffi::reflection::type_attr::kSHash, &FunctionNode::SHash);
67 }
68
70 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
71 // `ty` is derived from the fields below. Function transformations update
72 // those source fields without maintaining this redundant cache eagerly.
73 // Remove this exception once all Function mutation paths recompute `ty`.
74 return equal(attrs, other->attrs, false, "attrs") &&
75 equal(params, other->params, true, "params") &&
76 equal(ret_type, other->ret_type, false, "ret_type") &&
77 equal(body, other->body, false, "body");
78 }
79
80 int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const {
85 hash_value = hash(body, hash_value, false);
86 return hash_value;
87 }
88
97
99};
100
105class Function : public BaseFunc {
106 public:
120 TVM_TIRX_DLL Function(ffi::Array<tvm::Var> params, ffi::Optional<SeqStmt> body,
121 Type ret_type = VoidType(), DictAttrs attrs = DictAttrs(),
122 Location loc = UnknownLoc());
123
124 explicit Function(ffi::ObjectPtr<FunctionNode> node) : BaseFunc(std::move(node)) {}
125
128};
129
140
179Function Specialize(Function func, const ffi::Map<Var, ffi::Variant<TensorVar, Expr>>& param_map);
180
186namespace attr {
187
238constexpr const char* kKernelLaunchParams = "tirx.kernel_launch_params";
239
245constexpr const char* kNoAlias = "tirx.noalias";
246
255constexpr const char* kIsEntryFunc = "tirx.is_entry_func";
256
262constexpr const char* kIsGlobalFunc = "tirx.is_global_func";
263
269constexpr const char* kIsHostFunc = "tirx.is_host_func";
270
274constexpr const char* kPersistentKernel = "tirx.persistent_kernel";
275
276} // namespace attr
277} // namespace tirx
278} // namespace tvm
279#endif // TVM_TIR_FUNCTION_H_
Base node of all functions.
Definition function.h:92
DictAttrs attrs
Additional attributes storing the meta-data.
Definition function.h:95
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=std::nullopt) const
Get a function attribute.
Definition function.h:117
Managed reference to BaseFuncNode.
Definition function.h:181
Managed reference to DictAttrsNode.
Definition attrs.h:108
Managed reference to FuncTypeNode.
Definition type.h:224
Non-nullable source-location metadata.
Definition location.h:91
Managed reference to TypeNode.
Definition base_expr.h:80
static TVM_TIRX_DLL Type Missing()
Construct a MissingType for type information not yet populated.
Reference to the shared immutable unknown location.
Definition location.h:105
Managed reference to VarNode.
Definition expr.h:714
Primitive functions that contains TIR statements.
Definition function.h:49
bool SEqual(const FunctionNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition function.h:69
TVM_TIRX_DLL FuncType func_type_annotation() const
Return the derived function annotation of this function.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Function", FunctionNode, BaseFuncNode)
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition function.h:80
Type ret_type
The return type of the function.
Definition function.h:54
static void RegisterReflection()
Definition function.h:58
ffi::Array< tvm::Var > params
Function parameters.
Definition function.h:52
ffi::Optional< SeqStmt > body
The body of the function, absent for a declaration.
Definition function.h:56
Managed reference to FunctionNode.
Definition function.h:105
TVM_TIRX_DLL Function(ffi::Array< tvm::Var > params, ffi::Optional< SeqStmt > body, Type ret_type=VoidType(), DictAttrs attrs=DictAttrs(), Location loc=UnknownLoc())
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Function, BaseFunc, FunctionNode)
Function(ffi::ObjectPtr< FunctionNode > node)
Definition function.h:124
TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionNode)
Copy-on-write helper macro for IR ffi::ObjectRef types.
Function nodes.
TIR expressions.
constexpr const char * kIsHostFunc
Mark the function as run on the host, mutually exclusive with kTarget.
Definition function.h:269
constexpr const char * kIsEntryFunc
Mark the function as the entry function of the final generated runtime module.
Definition function.h:255
constexpr const char * kPersistentKernel
Mark the kernel as persistent.
Definition function.h:274
constexpr const char * kIsGlobalFunc
Mark the function as the global function called from the host.
Definition function.h:262
constexpr const char * kNoAlias
Whether to set noalias rule on the function arguments.
Definition function.h:245
constexpr const char * kKernelLaunchParams
Ordered launch-parameter tags for a device kernel.
Definition function.h:238
TVM_TIRX_DLL Function RenewDef(Function func)
Renew variable definitions and remap their uses in a function.
Function Specialize(Function func, const ffi::Map< Var, ffi::Variant< TensorVar, Expr > > &param_map)
Specialize parameters of Function.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Type VoidType()
Definition type.h:185
TVM_TIRX_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
equal
#define TVM_TIRX_DLL
Definition base.h:83
Device-independent tensor allocation, copies, views and serialization.
TIRx tensor expressions and construction helpers.
TIRx statements and shared statement entry header.