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/buffer.h>
34#include <tvm/tirx/stmt.h>
35
36#include <string>
37
38namespace tvm {
39namespace tirx {
40
49class PrimFuncNode : public BaseFuncNode {
50 public:
52 ffi::Array<tirx::Var> params;
57
58 static void RegisterReflection() {
59 namespace refl = tvm::ffi::reflection;
60 refl::ObjectDef<PrimFuncNode>()
61 .def_ro("params", &PrimFuncNode::params, refl::AttachFieldFlag::SEqHashDefRecursive())
62 .def_ro("ret_type", &PrimFuncNode::ret_type)
63 .def_ro("body", &PrimFuncNode::body);
64 refl::TypeAttrDef<PrimFuncNode>()
65 .def("__s_equal__", &PrimFuncNode::SEqual)
66 .def("__s_hash__", &PrimFuncNode::SHash);
67 }
68
70 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
71 // `ty` is derived from the fields below. PrimFunc transformations update
72 // those source fields without maintaining this redundant cache eagerly.
73 // Remove this exception once all PrimFunc 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 PrimFunc : public BaseFunc {
106 public:
120 TVM_DLL PrimFunc(ffi::Array<tirx::Var> params, Stmt body, Type ret_type = VoidType(),
121 DictAttrs attrs = DictAttrs(), Span span = Span());
122
125};
126
130class TensorIntrinNode : public ffi::Object {
131 public:
136
137 static void RegisterReflection() {
138 namespace refl = tvm::ffi::reflection;
139 refl::ObjectDef<TensorIntrinNode>()
140 .def_ro("desc", &TensorIntrinNode::desc)
141 .def_ro("impl", &TensorIntrinNode::impl);
142 }
143 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TensorIntrin", TensorIntrinNode, ffi::Object);
144};
145
149class TensorIntrin : public ffi::ObjectRef {
150 public:
156 TVM_DLL explicit TensorIntrin(PrimFunc desc, PrimFunc impl);
157
167 TVM_DLL static void Register(ffi::String name, TensorIntrin intrin, bool override = false);
168
178 TVM_DLL static ffi::Optional<TensorIntrin> Get(ffi::String name, bool allow_missing = false);
179
181};
182
221PrimFunc Specialize(PrimFunc func, const ffi::Map<Var, ffi::Variant<BufferVar, Expr>>& param_map);
222
228namespace attr {
229
280constexpr const char* kKernelLaunchParams = "tirx.kernel_launch_params";
281
287constexpr const char* kLaunchBoundsMinBlocksPerSM = "tirx.launch_bounds_min_blocks_per_sm";
288
294constexpr const char* kLaunchBoundsMaxBlocksPerCluster =
295 "tirx.launch_bounds_max_blocks_per_cluster";
296
305constexpr const char* kMaxRegisters = "tirx.max_registers";
306
317constexpr const char* kRequiredBlockSize = "tirx.required_block_size";
318
324constexpr const char* kNoAlias = "tirx.noalias";
325
334constexpr const char* kIsEntryFunc = "tirx.is_entry_func";
335
341constexpr const char* kIsGlobalFunc = "tirx.is_global_func";
342
348constexpr const char* kIsHostFunc = "tirx.is_host_func";
349
355constexpr const char* kIsScheduled = "tirx.is_scheduled";
356
357} // namespace attr
358} // namespace tirx
359} // namespace tvm
360#endif // TVM_TIR_FUNCTION_H_
Symbolic n-dimensional array, to represent a memory buffer.
Base node of all functions.
Definition function.h:156
DictAttrs attrs
Additional attributes storing the meta-data.
Definition function.h:159
Managed reference to BaseFuncNode.
Definition function.h:250
Managed reference to DictAttrsNode.
Definition attrs.h:102
Managed reference to FuncTypeNode.
Definition type.h:168
Definition source_map.h:111
Managed reference to TypeNode.
Definition base_expr.h:77
static Type Missing()
Sentinel for a type that has not been populated yet.
Managed reference to VarNode.
Definition expr.h:372
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Primitive functions that contains TIR statements.
Definition function.h:49
ffi::Array< tirx::Var > params
Function parameters.
Definition function.h:52
static void RegisterReflection()
Definition function.h:58
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition function.h:80
FuncType func_type_annotation() const
Return the derived function annotation of this function.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.PrimFunc", PrimFuncNode, BaseFuncNode)
tirx::Stmt body
The body of the function.
Definition function.h:56
bool SEqual(const PrimFuncNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition function.h:69
Type ret_type
The return type of the function.
Definition function.h:54
Managed reference to PrimFuncNode.
Definition function.h:105
PrimFunc(ffi::Array< tirx::Var > params, Stmt body, Type ret_type=VoidType(), DictAttrs attrs=DictAttrs(), Span span=Span())
Constructor.
TVM_DEFINE_OBJECT_REF_COW_METHOD(PrimFuncNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PrimFunc, BaseFunc, PrimFuncNode)
Container of all statements.
Definition stmt.h:67
Tensor intrinsics for tensorization.
Definition function.h:130
PrimFunc desc
The function to describe the computation.
Definition function.h:133
PrimFunc impl
The function of the implementation for the execution.
Definition function.h:135
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TensorIntrin", TensorIntrinNode, ffi::Object)
static void RegisterReflection()
Definition function.h:137
Managed reference to TensorIntrinNode.
Definition function.h:149
static void Register(ffi::String name, TensorIntrin intrin, bool override=false)
Create and register a TensorIntrin. After registration, the TensorIntrin can be looked up with its na...
static ffi::Optional< TensorIntrin > Get(ffi::String name, bool allow_missing=false)
Look up TensorIntrin by name. Raises an exception if not found.
TensorIntrin(PrimFunc desc, PrimFunc impl)
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TensorIntrin, ffi::ObjectRef, TensorIntrinNode)
Copy-on-write helper macro for IR ffi::ObjectRef types.
Function nodes.
TIR expressions.
constexpr const char * kLaunchBoundsMaxBlocksPerCluster
CUDA launch bound maximum CTAs per cluster.
Definition function.h:294
constexpr const char * kMaxRegisters
CUDA maximum registers per thread.
Definition function.h:305
constexpr const char * kIsHostFunc
Mark the function as run on the host, mutually exclusive with kTarget.
Definition function.h:348
constexpr const char * kLaunchBoundsMinBlocksPerSM
CUDA launch bound minimum CTAs per SM.
Definition function.h:287
constexpr const char * kIsEntryFunc
Mark the function as the entry function of the final generated runtime module.
Definition function.h:334
constexpr const char * kIsGlobalFunc
Mark the function as the global function called from the host.
Definition function.h:341
constexpr const char * kRequiredBlockSize
Require CUDA to use the statically-declared block and cluster dimensions.
Definition function.h:317
constexpr const char * kIsScheduled
Mark the function as scheduled, so the default schedule will pass will skip it.
Definition function.h:355
constexpr const char * kNoAlias
Whether to set noalias rule on the function arguments.
Definition function.h:324
constexpr const char * kKernelLaunchParams
List of thread IterVar that a DeviceLaunch function corresponds to.
Definition function.h:280
PrimFunc Specialize(PrimFunc func, const ffi::Map< Var, ffi::Variant< BufferVar, Expr > > &param_map)
Specialize parameters of PrimFunc.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Type VoidType()
Definition type.h:129
A device-independent managed Tensor abstraction.
TIR statements.