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_TIRX_FUNCTION_H_
25#define TVM_TIRX_FUNCTION_H_
26
27#include <tvm/ffi/container/map.h>
28#include <tvm/ffi/extra/structural_equal.h>
29#include <tvm/ffi/extra/structural_hash.h>
30#include <tvm/ir/cow.h>
31#include <tvm/ir/function.h>
32#include <tvm/ir/prim/expr.h>
33#include <tvm/runtime/tensor.h>
34#include <tvm/tirx/expr.h>
35#include <tvm/tirx/stmt.h>
36
37#include <string>
38
39namespace tvm {
40namespace tirx {
41
50class FunctionObj : public BaseFuncObj {
51 public:
53 ffi::Array<tvm::Var> params;
58
59 explicit FunctionObj(ffi::UnsafeInit tag) : body(tag) {}
60
61 static void RegisterReflection() {
62 namespace refl = tvm::ffi::reflection;
63 refl::ObjectDef<FunctionObj>()
64 .def_ro("params", &FunctionObj::params, refl::AttachFieldFlag::SEqHashDefPattern())
65 .def_ro("ret_type", &FunctionObj::ret_type)
66 .def_ro("body", &FunctionObj::body);
67 refl::TypeAttrDef<FunctionObj>()
68 .def(tvm::ffi::type_attr::kSEqual.name, &FunctionObj::SEqual)
69 .def(tvm::ffi::type_attr::kSHash.name, &FunctionObj::SHash);
70 }
71
72 bool SEqual(const FunctionObj* other,
73 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
74 // `ty` is derived from the signature; compare its source fields directly.
75 // Establish parameter bindings before comparing attributes that may reference them.
76 return equal(params, other->params, true, "params") &&
77 equal(attrs, other->attrs, false, "attrs") &&
78 equal(ret_type, other->ret_type, false, "ret_type") &&
79 equal(body, other->body, false, "body");
80 }
81
82 int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const {
83 int64_t hash_value = init_hash;
84 hash_value = hash(params, hash_value, true);
85 hash_value = hash(attrs, hash_value, false);
86 hash_value = hash(ret_type, hash_value, false);
87 hash_value = hash(body, hash_value, false);
88 return hash_value;
89 }
90
99
101};
102
107class Function : public BaseFunc {
108 public:
123 TVM_TIRX_DLL Function(ffi::Array<tvm::Var> params, SeqStmt body, Type ret_type = VoidType(),
124 DictAttrs attrs = DictAttrs(), Location loc = UnknownLoc());
125
126 explicit Function(ffi::ObjectPtr<FunctionObj> node) : BaseFunc(std::move(node)) {}
127
130};
131
142
148namespace attr {
149
200inline constexpr ffi::reflection::AttrKey<ffi::Array<ffi::String>> kKernelLaunchParams{
201 "tirx.kernel_launch_params"};
202
208inline constexpr ffi::reflection::AttrKey<bool> kNoAlias{"tirx.noalias"};
209
218inline constexpr ffi::reflection::AttrKey<bool> kIsEntryFunc{"tirx.is_entry_func"};
219
225inline constexpr ffi::reflection::AttrKey<bool> kIsGlobalFunc{"tirx.is_global_func"};
226
232inline constexpr ffi::reflection::AttrKey<bool> kIsHostFunc{"tirx.is_host_func"};
233
237inline constexpr ffi::reflection::AttrKey<bool> kPersistentKernel{"tirx.persistent_kernel"};
238
239} // namespace attr
240} // namespace tirx
241} // namespace tvm
242#endif // TVM_TIRX_FUNCTION_H_
Function nodes.
TIR expressions.
#define TVM_TIRX_DLL
Definition base.h:55
Device-independent tensor allocation, copies, views and serialization.
TIRx tensor expressions and construction helpers.
TIRx statements and shared statement entry header.
Base node of all functions.
Definition function.h:94
DictAttrs attrs
Additional attributes storing the meta-data.
Definition function.h:97
Managed reference to BaseFuncObj.
Definition function.h:202
Managed reference to DictAttrsObj.
Definition attrs.h:108
Managed reference to FuncTypeObj.
Definition type.h:553
Non-nullable source-location metadata.
Definition location.h:91
Sequence statement.
Definition stmt.h:91
Managed reference to TypeObj.
Definition type.h:75
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
Primitive functions that contains TIR statements.
Definition function.h:50
bool SEqual(const FunctionObj *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition function.h:72
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition function.h:82
ffi::Array< tvm::Var > params
Function parameters.
Definition function.h:53
TVM_TIRX_DLL FuncType func_type_annotation() const
Return the derived function annotation of this function.
SeqStmt body
The body of the function, empty for a marked declaration.
Definition function.h:57
FunctionObj(ffi::UnsafeInit tag)
Definition function.h:59
Type ret_type
The return type of the function.
Definition function.h:55
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Function", FunctionObj, BaseFuncObj)
static void RegisterReflection()
Definition function.h:61
Managed reference to FunctionObj.
Definition function.h:107
Function(ffi::ObjectPtr< FunctionObj > node)
Definition function.h:126
TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Function, BaseFunc, FunctionObj)
TVM_TIRX_DLL Function(ffi::Array< tvm::Var > params, SeqStmt body, Type ret_type=VoidType(), DictAttrs attrs=DictAttrs(), Location loc=UnknownLoc())
Constructor.
Copy-on-write helper macro for IR ffi::ObjectRef types.
constexpr ffi::reflection::AttrKey< ffi::Array< ffi::String > > kKernelLaunchParams
Ordered launch-parameter tags for a device kernel.
Definition function.h:200
constexpr ffi::reflection::AttrKey< bool > kNoAlias
Whether to set noalias rule on the function arguments.
Definition function.h:208
constexpr ffi::reflection::AttrKey< bool > kIsGlobalFunc
Mark the function as the global function called from the host.
Definition function.h:225
constexpr ffi::reflection::AttrKey< bool > kIsHostFunc
Mark the function as run on the host, mutually exclusive with kTarget.
Definition function.h:232
constexpr ffi::reflection::AttrKey< bool > kPersistentKernel
Mark the kernel as persistent.
Definition function.h:237
constexpr ffi::reflection::AttrKey< bool > kIsEntryFunc
Mark the function as the entry function of the final generated runtime module.
Definition function.h:218
TVM_TIRX_DLL Function RenewDef(Function func)
Renew variable definitions and remap their uses in a function.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Type VoidType()
Definition type.h:514
TVM_TIRX_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
equal