tvm
tirx_op.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  */
23 #ifndef TVM_TIRX_TIRX_OP_H_
24 #define TVM_TIRX_TIRX_OP_H_
25 
26 #include <tvm/ir/op.h>
27 #include <tvm/target/target.h>
28 #include <tvm/tirx/exec_scope.h>
29 #include <tvm/tirx/stmt.h>
30 #include <tvm/tirx/tirx_stmt.h>
31 
32 namespace tvm {
33 namespace tirx {
34 
40 using FArgSanitizer = ffi::TypedFunction<void(tvm::Op, ffi::Array<ffi::ObjectRef>)>;
41 
42 namespace callback {
44 constexpr const char* kPrivateAlloc = "private_alloc";
48 constexpr const char* kDeviceInitStmt = "device_init_stmt";
52 constexpr const char* kHostInitStmt = "host_init_stmt";
56 constexpr const char* kPostBufferDefStmt = "post_buffer_def_stmt";
57 } // namespace callback
58 
62 class DispatchContextNode : public ffi::Object {
63  public:
69  ffi::Map<ffi::String, IterVar> launch_params;
71  ffi::Map<Var, Range> var_range_map;
73  bool alloc_only;
75  ffi::Map<ffi::String, ffi::ObjectRef> callbacks;
77  ffi::Map<ffi::String, ffi::ObjectRef> shared_state;
87  ffi::Map<ffi::String, ffi::Array<PrimExpr>> inter;
89  ffi::Map<ffi::String, ffi::Array<PrimExpr>> intra;
91  ffi::String scope_kind;
92 
93  static void RegisterReflection() {
94  namespace refl = tvm::ffi::reflection;
95  refl::ObjectDef<DispatchContextNode>()
96  .def_ro("target", &DispatchContextNode::target)
97  .def_ro("exec_scope", &DispatchContextNode::exec_scope)
98  .def_ro("launch_params", &DispatchContextNode::launch_params)
99  .def_ro("var_range_map", &DispatchContextNode::var_range_map)
100  .def_ro("alloc_only", &DispatchContextNode::alloc_only)
101  .def_ro("callbacks", &DispatchContextNode::callbacks)
102  .def_ro("shared_state", &DispatchContextNode::shared_state)
103  .def_ro("inter", &DispatchContextNode::inter)
104  .def_ro("intra", &DispatchContextNode::intra)
105  .def_ro("scope_kind", &DispatchContextNode::scope_kind);
106  }
107 
109  void AddAllocBuffer(Buffer buffer);
110 
112  void AddInitStmt(Stmt stmt, bool host = false);
113 
115  void AddPostBufferDefStmt(Buffer buffer, Stmt stmt);
116 
118  void SharedStateSet(ffi::String key, ffi::ObjectRef value);
119 
121  ffi::Optional<ffi::ObjectRef> SharedStateGet(ffi::String key);
122 
123  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DispatchContext", DispatchContextNode, ffi::Object);
124 };
125 
129 class DispatchContext : public ffi::ObjectRef {
130  public:
131  TVM_DLL DispatchContext(Target target, ExecScope exec_scope,
132  ffi::Map<ffi::String, IterVar> launch_params = {},
133  ffi::Map<Var, Range> var_range_map = {}, bool alloc_only = false,
134  ffi::Map<ffi::String, ffi::ObjectRef> callbacks = {},
135  ffi::Map<ffi::String, ffi::ObjectRef> shared_state = {},
136  ffi::Map<ffi::String, ffi::Array<PrimExpr>> inter = {},
137  ffi::Map<ffi::String, ffi::Array<PrimExpr>> intra = {},
138  ffi::String scope_kind = "");
139 
141 };
142 
148 TVM_DLL const Op& cast();
149 
155 TVM_DLL const Op& copy();
156 
162 TVM_DLL const Op& copy_async();
163 
169 TVM_DLL const Op& fill();
170 
176 TVM_DLL const Op& gemm();
177 
184 TVM_DLL const Op& gemm_async();
185 
186 TVM_DLL const Op& zero();
187 
188 TVM_DLL const Op& sqrt();
189 
190 TVM_DLL const Op& exp();
191 
192 TVM_DLL const Op& exp2();
193 
194 TVM_DLL const Op& add();
195 
196 TVM_DLL const Op& sub();
197 
198 TVM_DLL const Op& mul();
199 
200 TVM_DLL const Op& fdiv();
201 
202 TVM_DLL const Op& minimum();
203 
204 TVM_DLL const Op& maximum();
205 
206 TVM_DLL const Op& reciprocal();
207 
208 TVM_DLL const Op& sum();
209 
210 TVM_DLL const Op& max();
211 
212 TVM_DLL const Op& min();
213 
214 TVM_DLL const Op& memset();
215 
216 TVM_DLL const Op& reduce_negate();
217 
218 TVM_DLL const Op& binary_reduce();
219 
220 TVM_DLL const Op& unary_reduce();
221 
222 TVM_DLL const Op& binary_chain();
223 
224 TVM_DLL const Op& select();
225 
226 TVM_DLL const Op& fma();
227 
228 TVM_DLL const Op& silu();
229 
230 TVM_DLL const Op& compose_op();
231 
232 TVM_DLL const Op& permute_layout();
233 
234 } // namespace tirx
235 } // namespace tvm
236 
237 #endif // TVM_TIRX_TIRX_OP_H_
Managed reference class to OpNode.
Definition: op.h:163
Managed reference class to TargetNode.
Definition: target.h:134
Buffer is a symbolic n-darray structure. It is a composition of primitive symbolic types,...
Definition: buffer.h:187
The context information of the kernel required by op dispatch.
Definition: tirx_op.h:62
bool alloc_only
Whether the dispatch context is only used for buffer allocation.
Definition: tirx_op.h:73
ffi::Map< ffi::String, ffi::ObjectRef > shared_state
Shared state that persists across dispatch calls within a single lowering pass.
Definition: tirx_op.h:77
ffi::Map< ffi::String, ffi::ObjectRef > callbacks
Callback to be handled when the operator is scheduled.
Definition: tirx_op.h:75
ExecScope exec_scope
The exec scope of the operator.
Definition: tirx_op.h:67
static void RegisterReflection()
Definition: tirx_op.h:93
ffi::Map< ffi::String, IterVar > launch_params
The kernel launch parameters.
Definition: tirx_op.h:69
ffi::String scope_kind
Scope kind string ("kernel"/"cta"/"warpgroup"/"warp"/"thread"/"cluster").
Definition: tirx_op.h:91
Target target
The target of the kernel.
Definition: tirx_op.h:65
void SharedStateSet(ffi::String key, ffi::ObjectRef value)
Set a value in the shared state cache.
ffi::Map< ffi::String, ffi::Array< PrimExpr > > inter
ExecContext inter-team view at this op site.
Definition: tirx_op.h:87
ffi::Map< Var, Range > var_range_map
A map from loop variables to their ranges.
Definition: tirx_op.h:71
void AddAllocBuffer(Buffer buffer)
Add a buffer to be allocated in the kernel.
ffi::Map< ffi::String, ffi::Array< PrimExpr > > intra
ExecContext intra-team view. Same encoding as inter.
Definition: tirx_op.h:89
void AddPostBufferDefStmt(Buffer buffer, Stmt stmt)
Add a statement to be inserted after a buffer's definition.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DispatchContext", DispatchContextNode, ffi::Object)
ffi::Optional< ffi::ObjectRef > SharedStateGet(ffi::String key)
Get a value from the shared state cache.
void AddInitStmt(Stmt stmt, bool host=false)
Add an initialization statement to be inserted.
Managed reference to DispatchContextNode.
Definition: tirx_op.h:129
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DispatchContext, ffi::ObjectRef, DispatchContextNode)
DispatchContext(Target target, ExecScope exec_scope, ffi::Map< ffi::String, IterVar > launch_params={}, ffi::Map< Var, Range > var_range_map={}, bool alloc_only=false, ffi::Map< ffi::String, ffi::ObjectRef > callbacks={}, ffi::Map< ffi::String, ffi::ObjectRef > shared_state={}, ffi::Map< ffi::String, ffi::Array< PrimExpr >> inter={}, ffi::Map< ffi::String, ffi::Array< PrimExpr >> intra={}, ffi::String scope_kind="")
Definition: exec_scope.h:228
Container of all statements.
Definition: stmt.h:64
Primitive operators(builtin intrinsics) and registry for them.
constexpr const char * kPrivateAlloc
The buffers allocated by the operator.
Definition: tirx_op.h:44
constexpr const char * kPostBufferDefStmt
Statements to be inserted after a specific buffer's definition (DeclBuffer/AllocBuffer)....
Definition: tirx_op.h:56
constexpr const char * kDeviceInitStmt
The initialization statement of the operator. which will be inserted at the beginning of the kernel.
Definition: tirx_op.h:48
constexpr const char * kHostInitStmt
The initialization statement of the operator. which will be inserted at the beginning of the kernel.
Definition: tirx_op.h:52
const Op & compose_op()
const Op & binary_chain()
const Op & copy_async()
See pesudo code below:
const Op & unary_reduce()
const Op & sqrt()
const Op & mul()
const Op & silu()
const Op & reciprocal()
const Op & reduce_negate()
const Op & cast()
See pesudo code below:
const Op & gemm_async()
See pesudo code below:
const Op & select()
const Op & minimum()
const Op & binary_reduce()
const Op & fill()
See pesudo code below:
const Op & fma()
const Op & exp2()
const Op & permute_layout()
const Op & add()
const Op & gemm()
See pesudo code below:
const Op & sum()
const Op & copy()
See pesudo code below:
const Op & fdiv()
const Op & zero()
const Op & exp()
ffi::TypedFunction< void(tvm::Op, ffi::Array< ffi::ObjectRef >)> FArgSanitizer
The type of the function that sanitizes the arguments of a TIRX operator.
Definition: tirx_op.h:40
const Op & memset()
const Op & max()
const Op & maximum()
const Op & sub()
const Op & min()
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Compilation target object.
TIR statements.