tvm
Loading...
Searching...
No Matches
context.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_TILE_CONTEXT_H_
24#define TVM_TIRX_TILE_CONTEXT_H_
25
26#include <tvm/target/target.h>
27#include <tvm/tirx/stmt.h>
29
30namespace tvm {
31namespace tirx {
32namespace tile {
33
34namespace callback {
36inline constexpr ffi::reflection::AttrKey<ffi::Array<Bind>> kPrivateAlloc{"private_alloc"};
40inline constexpr ffi::reflection::AttrKey<ffi::Array<Stmt>> kDeviceInitStmt{"device_init_stmt"};
44inline constexpr ffi::reflection::AttrKey<ffi::Array<Stmt>> kHostInitStmt{"host_init_stmt"};
48inline constexpr ffi::reflection::AttrKey<ffi::Map<TensorVar, ffi::Array<Stmt>>> kPostTensorDefStmt{
49 "post_tensor_def_stmt"};
50} // namespace callback
51
55class DispatchContextObj : public ffi::Object {
56 public:
59 explicit DispatchContextObj(ffi::UnsafeInit)
60 : target(ffi::UnsafeInit{}), thread_scope(ffi::UnsafeInit{}) {}
61
67 ffi::Map<ffi::String, ffi::Tuple<PrimVar, PrimExpr>> launch_params;
69 ffi::Map<Var, Range> var_range_map;
73 ffi::Map<ffi::String, ffi::ObjectRef> callbacks;
75 ffi::Map<ffi::String, ffi::ObjectRef> shared_state;
85 ffi::Map<ffi::String, ffi::Array<PrimExpr>> inter;
87 ffi::Map<ffi::String, ffi::Array<PrimExpr>> intra;
89 ffi::String scope_kind;
90
91 static void RegisterReflection() {
92 namespace refl = tvm::ffi::reflection;
93 refl::ObjectDef<DispatchContextObj>()
94 .def_ro("target", &DispatchContextObj::target)
95 .def_ro("thread_scope", &DispatchContextObj::thread_scope)
96 .def_ro("launch_params", &DispatchContextObj::launch_params)
97 .def_ro("var_range_map", &DispatchContextObj::var_range_map)
98 .def_ro("alloc_only", &DispatchContextObj::alloc_only)
99 .def_ro("callbacks", &DispatchContextObj::callbacks)
100 .def_ro("shared_state", &DispatchContextObj::shared_state)
101 .def_ro("inter", &DispatchContextObj::inter)
102 .def_ro("intra", &DispatchContextObj::intra)
103 .def_ro("scope_kind", &DispatchContextObj::scope_kind);
104 }
105
107 TVM_TIRX_DLL void AddAllocTensor(TensorVar tensor, ffi::Array<PrimExpr> allocated_addr = {},
108 ffi::Map<ffi::String, ffi::Any> annotations = {});
109
111 TVM_TIRX_DLL void AddInitStmt(Stmt stmt, bool host = false);
112
115
117 void SharedStateSet(ffi::String key, ffi::ObjectRef value) { shared_state.Set(key, value); }
118
120 ffi::Optional<ffi::ObjectRef> SharedStateGet(ffi::String key) {
121 auto it = shared_state.find(key);
122 if (it != shared_state.end()) {
123 return (*it).second;
124 }
125 return ffi::Optional<ffi::ObjectRef>();
126 }
127
128 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DispatchContext", DispatchContextObj, ffi::Object);
129 // Shared instances are mutated in place, so the class is mutable.
130 static constexpr bool _type_mutable = true;
131};
132
136class DispatchContext : public ffi::ObjectRef {
137 public:
139 Target target, ThreadScope thread_scope,
140 ffi::Map<ffi::String, ffi::Tuple<PrimVar, PrimExpr>> launch_params = {},
141 ffi::Map<Var, Range> var_range_map = {}, bool alloc_only = false,
142 ffi::Map<ffi::String, ffi::ObjectRef> callbacks = {},
143 ffi::Map<ffi::String, ffi::ObjectRef> shared_state = {},
144 ffi::Map<ffi::String, ffi::Array<PrimExpr>> inter = {},
145 ffi::Map<ffi::String, ffi::Array<PrimExpr>> intra = {}, ffi::String scope_kind = "");
146
149};
150
151} // namespace tile
152} // namespace tirx
153} // namespace tvm
154
155#endif // TVM_TIRX_TILE_CONTEXT_H_
#define TVM_TIRX_DLL
Definition base.h:55
TIRx statements and shared statement entry header.
Container of all statements.
Definition stmt.h:61
Managed reference class to TargetObj.
Definition target.h:155
Checked zero-state view over an ordinary VarObj with TensorType.
Definition expr.h:48
Canonical thread-scope identity, named as backend.scope.
Definition thread_scope.h:55
The context information of the kernel required by op dispatch.
Definition context.h:55
TVM_TIRX_DLL void AddInitStmt(Stmt stmt, bool host=false)
Add an initialization statement to be inserted.
ffi::String scope_kind
Scope kind string ("kernel"/"cta"/"warpgroup"/"warp"/"thread"/"cluster").
Definition context.h:89
DispatchContextObj(Target target, ThreadScope thread_scope)
Definition context.h:57
bool alloc_only
Whether the dispatch context is only used for tensor allocation.
Definition context.h:71
void SharedStateSet(ffi::String key, ffi::ObjectRef value)
Set a value in the shared state cache.
Definition context.h:117
ffi::Map< ffi::String, ffi::Array< PrimExpr > > inter
ExecContext inter-team view at this op site.
Definition context.h:85
DispatchContextObj(ffi::UnsafeInit)
Definition context.h:59
static constexpr bool _type_mutable
Definition context.h:130
ffi::Map< ffi::String, ffi::ObjectRef > callbacks
Callback to be handled when the operator is scheduled.
Definition context.h:73
ffi::Map< Var, Range > var_range_map
A map from loop variables to their ranges.
Definition context.h:69
ffi::Map< ffi::String, ffi::ObjectRef > shared_state
Shared state that persists across dispatch calls within a single lowering pass.
Definition context.h:75
ffi::Map< ffi::String, ffi::Array< PrimExpr > > intra
ExecContext intra-team view. Same encoding as inter.
Definition context.h:87
ThreadScope thread_scope
The thread scope of the operator.
Definition context.h:65
static void RegisterReflection()
Definition context.h:91
TVM_TIRX_DLL void AddPostTensorDefStmt(TensorVar tensor, Stmt stmt)
Add a statement to be inserted after a tensor's definition.
TVM_TIRX_DLL void AddAllocTensor(TensorVar tensor, ffi::Array< PrimExpr > allocated_addr={}, ffi::Map< ffi::String, ffi::Any > annotations={})
Add a tensor to be allocated in the kernel.
ffi::Optional< ffi::ObjectRef > SharedStateGet(ffi::String key)
Get a value from the shared state cache.
Definition context.h:120
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DispatchContext", DispatchContextObj, ffi::Object)
ffi::Map< ffi::String, ffi::Tuple< PrimVar, PrimExpr > > launch_params
Hardware tag to (index variable, extent) launch parameters.
Definition context.h:67
Target target
The target of the kernel.
Definition context.h:63
Managed reference to DispatchContextObj.
Definition context.h:136
TVM_TIRX_DLL DispatchContext(Target target, ThreadScope thread_scope, ffi::Map< ffi::String, ffi::Tuple< PrimVar, PrimExpr > > 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="")
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DispatchContext, ffi::ObjectRef, DispatchContextObj)
constexpr ffi::reflection::AttrKey< ffi::Array< Stmt > > kHostInitStmt
The initialization statement of the operator. which will be inserted at the beginning of the kernel.
Definition context.h:44
constexpr ffi::reflection::AttrKey< ffi::Array< Stmt > > kDeviceInitStmt
The initialization statement of the operator. which will be inserted at the beginning of the kernel.
Definition context.h:40
constexpr ffi::reflection::AttrKey< ffi::Map< TensorVar, ffi::Array< Stmt > > > kPostTensorDefStmt
Statements to be inserted after a specific tensor's definition (DeclTensor/AllocTensor)....
Definition context.h:48
constexpr ffi::reflection::AttrKey< ffi::Array< Bind > > kPrivateAlloc
The allocation Bind statements requested by the operator.
Definition context.h:36
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Compilation target object.
Enum identities for backend-defined thread hierarchies.