tvm
Loading...
Searching...
No Matches
sblock_scope.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 */
25#ifndef TVM_S_TIR_SBLOCK_SCOPE_H_
26#define TVM_S_TIR_SBLOCK_SCOPE_H_
27
28#include <tvm/base.h>
29#include <tvm/ir/module.h>
30#include <tvm/s_tir/stmt.h>
32#include <tvm/tirx/function.h>
33#include <tvm/tirx/stmt.h>
34
35#include <unordered_map>
36#include <utility>
37#include <vector>
38
39namespace tvm {
40namespace tirx {
41
56class StmtSRefObj : public ffi::Object {
57 public:
63 const StmtObj* stmt;
70 int64_t seq_index;
71
72 static void RegisterReflection() {
73 namespace refl = tvm::ffi::reflection;
74 refl::ObjectDef<StmtSRefObj>().def_ro("seq_index", &StmtSRefObj::seq_index);
75 }
76
77 static constexpr const bool _type_mutable = true;
78 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.StmtSRef", StmtSRefObj, ffi::Object);
79
81 void Reset() {
82 this->stmt = nullptr;
83 this->parent = nullptr;
84 this->seq_index = -1;
85 }
86
95 template <typename StmtType>
96 const StmtType* StmtAs() const {
97 if (stmt != nullptr && stmt->IsInstance<StmtType>()) {
98 return static_cast<const StmtType*>(stmt);
99 } else {
100 return nullptr;
101 }
102 }
103};
104
109class StmtSRef : public ffi::ObjectRef {
110 public:
118 TVM_DLL explicit StmtSRef(const StmtObj* stmt, StmtSRefObj* parent, int64_t seq_index);
119
121
122 public:
134 TVM_DLL static StmtSRef InlineMark();
146 TVM_DLL static StmtSRef RootMark();
147};
148
150 public:
152
153 ffi::Optional<VisitInterrupt> Visit(ffi::AnyView value) override {
154 if (value.as<ExprObj>()) return std::nullopt;
155 return s_tir::StmtExprVisitor::Visit(value);
156 }
157
163 static std::unordered_map<const StmtObj*, StmtSRef> Create(IRModule mod,
164 bool include_loops = true) {
165 auto creator = ffi::make_object<SRefTreeCreator>(include_loops);
166 for (const auto& kv : mod->functions) {
167 const BaseFunc& base_func = kv.second;
168 if (auto opt = base_func.as<Function>()) {
169 auto func = opt.value();
170 creator->Visit(func->body);
171 }
172 }
173 return std::move(creator->stmt2ref_);
174 }
175
176 explicit SRefTreeCreator(bool include_loops) : include_loops_(include_loops) {}
177
178 private:
183 void PushSRef(const StmtObj* stmt);
184
186 void PopAndRecordSRef();
187
188 ffi::Optional<VisitInterrupt> Visit_(const ForObj* loop) final;
189
190 ffi::Optional<VisitInterrupt> Visit_(const s_tir::SBlockRealizeObj* realize) final;
191
192 ffi::Optional<VisitInterrupt> Visit_(const SeqStmtObj* seq_stmt) final;
193
194 bool include_loops_;
196 std::unordered_map<const StmtObj*, StmtSRef> stmt2ref_;
198 std::vector<StmtSRef> srefs_;
199};
200
208enum class DepKind : int32_t {
209 kRAW = 0,
210 kWAW = 1,
211 kWAR = 2,
212 kOpaque = 3,
213};
214
220class DependencyObj : public ffi::Object {
221 public:
222 explicit DependencyObj(ffi::UnsafeInit tag) : src(tag), dst(tag) {}
223
225 : src(std::move(src)), dst(std::move(dst)), kind(kind) {}
232
233 static void RegisterReflection() {
234 namespace refl = tvm::ffi::reflection;
235 refl::ObjectDef<DependencyObj>()
236 .def_ro("src", &DependencyObj::src)
237 .def_ro("dst", &DependencyObj::dst)
238 .def_ro("kind", &DependencyObj::kind);
239 }
240 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Dependency", DependencyObj, ffi::Object);
241};
242
247class Dependency : public ffi::ObjectRef {
248 public:
250 TVM_DLL explicit Dependency(StmtSRef src, StmtSRef dst, DepKind kind);
252};
253
267class SBlockScopeObj : public ffi::Object {
268 public:
274 std::unordered_map<StmtSRef, ffi::Array<Dependency>, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>
277 std::unordered_map<StmtSRef, ffi::Array<Dependency>, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>
280 std::unordered_map<TensorVar, ffi::Array<StmtSRef>, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>
282
283 static void RegisterReflection() {
284 namespace refl = tvm::ffi::reflection;
285 refl::ObjectDef<SBlockScopeObj>();
286 }
287 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.SBlockScope", SBlockScopeObj, ffi::Object);
288
289 public:
290 /******** Dependency ********/
296 TVM_DLL ffi::Array<Dependency> GetDepsBySrc(const StmtSRef& src) const;
302 TVM_DLL ffi::Array<Dependency> GetDepsByDst(const StmtSRef& dst) const;
303};
304
309class SBlockScope : public ffi::ObjectRef {
310 public:
315 explicit SBlockScope(ffi::ObjectPtr<SBlockScopeObj> data) : ffi::ObjectRef(data) {
316 TVM_FFI_ICHECK(data != nullptr);
317 }
319 TVM_DLL SBlockScope();
326 TVM_DLL explicit SBlockScope(const ffi::Array<StmtSRef>& child_block_srefs);
327
329};
330
331} // namespace tirx
332} // namespace tvm
333
334#endif // TVM_S_TIR_SBLOCK_SCOPE_H_
TIR Function.
TIRx statements and shared statement entry header.
Managed reference to BaseFuncObj.
Definition function.h:202
Base type of all the expressions.
Definition expr.h:66
A for loop, with possible type annotations.
Definition stmt.h:471
Managed reference class to IRModuleObj.
Definition module.h:292
virtual ffi::Optional< VisitInterrupt > Visit(ffi::AnyView value)
Visit a borrowed object or inline value, throwing on failure.
Definition object_functor.h:242
The container of seq statement. Represent a sequence of statements.
Definition stmt.h:74
Base node of all statements.
Definition stmt.h:37
A block realization node represents execution of the block at the binding values.
Definition stmt.h:188
Extend native TIRX traversal with schedulable block semantics.
Definition stmt_functor.h:77
virtual ffi::Optional< VisitInterrupt > Visit_(const SBlockObj *op)
A tuple (src, dst, kind) representing certain types of dependency. For example, (A,...
Definition sblock_scope.h:220
StmtSRef dst
The destination of the dependency relation.
Definition sblock_scope.h:229
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Dependency", DependencyObj, ffi::Object)
StmtSRef src
The source of the dependency relation.
Definition sblock_scope.h:227
DependencyObj(StmtSRef src, StmtSRef dst, DepKind kind)
Definition sblock_scope.h:224
static void RegisterReflection()
Definition sblock_scope.h:233
DepKind kind
The dependency kind.
Definition sblock_scope.h:231
DependencyObj(ffi::UnsafeInit tag)
Definition sblock_scope.h:222
Managed reference to DependencyObj.
Definition sblock_scope.h:247
Dependency(StmtSRef src, StmtSRef dst, DepKind kind)
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Dependency, ffi::ObjectRef, DependencyObj)
Managed reference to FunctionObj.
Definition function.h:107
An object with 1-to-1 correspondence with each block reference in the sref tree. This data structure ...
Definition sblock_scope.h:267
std::unordered_map< TensorVar, ffi::Array< StmtSRef >, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > tensor_writers
The mapping from the tensor to the blocks who write it.
Definition sblock_scope.h:281
ffi::Array< Dependency > GetDepsBySrc(const StmtSRef &src) const
Get all dependencies whose src equals src
std::unordered_map< StmtSRef, ffi::Array< Dependency >, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > dst2deps
Lookup table for the dst of dependencies.
Definition sblock_scope.h:278
std::unordered_map< StmtSRef, ffi::Array< Dependency >, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > src2deps
Lookup table for the src of dependencies.
Definition sblock_scope.h:275
ffi::Array< Dependency > GetDepsByDst(const StmtSRef &dst) const
Get all dependencies whose dst equals dst
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.SBlockScope", SBlockScopeObj, ffi::Object)
static void RegisterReflection()
Definition sblock_scope.h:283
Managed reference to SBlockScopeObj.
Definition sblock_scope.h:309
SBlockScope(ffi::ObjectPtr< SBlockScopeObj > data)
Constructor from ffi::ObjectPtr<SBlockScopeObj>.
Definition sblock_scope.h:315
SBlockScope()
The constructor creating an empty block scope with on dependency information.
SBlockScope(const ffi::Array< StmtSRef > &child_block_srefs)
Create the object with the specific leaf blocks, and compute the dependency information between the l...
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SBlockScope, ffi::ObjectRef, SBlockScopeObj)
Definition sblock_scope.h:149
static std::unordered_map< const StmtObj *, StmtSRef > Create(IRModule mod, bool include_loops=true)
StmtSRef Tree Creator.
Definition sblock_scope.h:163
SRefTreeCreator(bool include_loops)
Definition sblock_scope.h:176
ffi::Optional< VisitInterrupt > Visit(ffi::AnyView value) override
Construct a visitor with the core expression hooks.
Definition sblock_scope.h:153
An object that refers to schedulable elements (block/for-loop) in TensorIR, aka "sref".
Definition sblock_scope.h:56
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.StmtSRef", StmtSRefObj, ffi::Object)
static void RegisterReflection()
Definition sblock_scope.h:72
StmtSRefObj * parent
The parent sref.
Definition sblock_scope.h:65
const StmtObj * stmt
The block or for stmt the object refers to.
Definition sblock_scope.h:63
void Reset()
Reset the object inplace to the invalid state.
Definition sblock_scope.h:81
const StmtType * StmtAs() const
Get the referenced statement with proper type checking. It serves the same purpose as ffi::ObjectRef:...
Definition sblock_scope.h:96
int64_t seq_index
If the statement the sref points to is an element of a SeqStmt in the AST, then seq_index is set to i...
Definition sblock_scope.h:70
static constexpr const bool _type_mutable
Definition sblock_scope.h:77
Managed reference to StmtSRefObj.
Definition sblock_scope.h:109
StmtSRef(const StmtObj *stmt, StmtSRefObj *parent, int64_t seq_index)
The constructor.
static StmtSRef RootMark()
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtSRef, ffi::ObjectRef, StmtSRefObj)
static StmtSRef InlineMark()
Parent compiler symbol visibility.
S-TIR (Schedulable TIR) statements and attributes.
Native traversal and statement dispatch for schedulable TIR.
IRModule that holds the functions and type definitions.
DepKind
Type of dependency. Right now we have 4 types of dependencies 1) Read-after-write (kRAW) 2) Write-aft...
Definition sblock_scope.h:208
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41