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/ir/module.h>
29#include <tvm/s_tir/stmt.h>
31#include <tvm/tirx/function.h>
32#include <tvm/tirx/stmt.h>
33
34#include <unordered_map>
35#include <utility>
36#include <vector>
37
38namespace tvm {
39namespace tirx {
40
55class StmtSRefObj : public ffi::Object {
56 public:
62 const StmtNode* stmt;
69 int64_t seq_index;
70
71 static void RegisterReflection() {
72 namespace refl = tvm::ffi::reflection;
73 refl::ObjectDef<StmtSRefObj>().def_ro("seq_index", &StmtSRefObj::seq_index);
74 }
75
76 static constexpr const bool _type_mutable = true;
77 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.StmtSRef", StmtSRefObj, ffi::Object);
78
80 void Reset() {
81 this->stmt = nullptr;
82 this->parent = nullptr;
83 this->seq_index = -1;
84 }
85
94 template <typename StmtType>
95 const StmtType* StmtAs() const {
96 if (stmt != nullptr && stmt->IsInstance<StmtType>()) {
97 return static_cast<const StmtType*>(stmt);
98 } else {
99 return nullptr;
100 }
101 }
102};
103
108class StmtSRef : public ffi::ObjectRef {
109 public:
117 TVM_DLL explicit StmtSRef(const StmtNode* stmt, StmtSRefObj* parent, int64_t seq_index);
118
120
121 public:
133 TVM_DLL static StmtSRef InlineMark();
145 TVM_DLL static StmtSRef RootMark();
146};
147
149 public:
151
152 ffi::Optional<VisitInterrupt> Visit(ffi::AnyView value) override {
153 if (value.as<ExprNode>()) return std::nullopt;
154 return s_tir::StmtExprVisitor::Visit(value);
155 }
156
162 static std::unordered_map<const StmtNode*, StmtSRef> Create(IRModule mod,
163 bool include_loops = true) {
164 auto creator = ffi::make_object<SRefTreeCreator>(include_loops);
165 for (const auto& kv : mod->functions) {
166 const BaseFunc& base_func = kv.second;
167 if (auto opt = base_func.as<Function>()) {
168 auto func = opt.value();
169 creator->Visit(func->body);
170 }
171 }
172 return std::move(creator->stmt2ref_);
173 }
174
175 explicit SRefTreeCreator(bool include_loops) : include_loops_(include_loops) {}
176
177 private:
182 void PushSRef(const StmtNode* stmt);
183
185 void PopAndRecordSRef();
186
187 ffi::Optional<VisitInterrupt> Visit_(const ForNode* loop) final;
188
189 ffi::Optional<VisitInterrupt> Visit_(const s_tir::SBlockRealizeNode* realize) final;
190
191 ffi::Optional<VisitInterrupt> Visit_(const SeqStmtNode* seq_stmt) final;
192
193 bool include_loops_;
195 std::unordered_map<const StmtNode*, StmtSRef> stmt2ref_;
197 std::vector<StmtSRef> srefs_;
198};
199
207enum class DepKind : int32_t {
208 kRAW = 0,
209 kWAW = 1,
210 kWAR = 2,
211 kOpaque = 3,
212};
213
219class DependencyObj : public ffi::Object {
220 public:
221 explicit DependencyObj(ffi::UnsafeInit tag) : src(tag), dst(tag) {}
222
224 : src(std::move(src)), dst(std::move(dst)), kind(kind) {}
231
232 static void RegisterReflection() {
233 namespace refl = tvm::ffi::reflection;
234 refl::ObjectDef<DependencyObj>()
235 .def_ro("src", &DependencyObj::src)
236 .def_ro("dst", &DependencyObj::dst)
237 .def_ro("kind", &DependencyObj::kind);
238 }
239 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Dependency", DependencyObj, ffi::Object);
240};
241
246class Dependency : public ffi::ObjectRef {
247 public:
249 TVM_DLL explicit Dependency(StmtSRef src, StmtSRef dst, DepKind kind);
251};
252
266class SBlockScopeObj : public ffi::Object {
267 public:
273 std::unordered_map<StmtSRef, ffi::Array<Dependency>, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>
276 std::unordered_map<StmtSRef, ffi::Array<Dependency>, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>
279 std::unordered_map<TensorVar, ffi::Array<StmtSRef>, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>
281
282 static void RegisterReflection() {
283 namespace refl = tvm::ffi::reflection;
284 refl::ObjectDef<SBlockScopeObj>();
285 }
286 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.SBlockScope", SBlockScopeObj, ffi::Object);
287
288 public:
289 /******** Dependency ********/
295 TVM_DLL ffi::Array<Dependency> GetDepsBySrc(const StmtSRef& src) const;
301 TVM_DLL ffi::Array<Dependency> GetDepsByDst(const StmtSRef& dst) const;
302};
303
308class SBlockScope : public ffi::ObjectRef {
309 public:
314 explicit SBlockScope(ffi::ObjectPtr<SBlockScopeObj> data) : ffi::ObjectRef(data) {
315 TVM_FFI_ICHECK(data != nullptr);
316 }
318 TVM_DLL SBlockScope();
325 TVM_DLL explicit SBlockScope(const ffi::Array<StmtSRef>& child_block_srefs);
326
328};
329
330} // namespace tirx
331} // namespace tvm
332
333#endif // TVM_S_TIR_SBLOCK_SCOPE_H_
Managed reference to BaseFuncNode.
Definition function.h:181
Base type of all the expressions.
Definition base_expr.h:324
A for loop, with possible type annotations.
Definition stmt.h:457
Managed reference class to IRModuleNode.
Definition module.h:255
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:75
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:76
virtual ffi::Optional< VisitInterrupt > Visit_(const SBlockNode *op)
A tuple (src, dst, kind) representing certain types of dependency. For example, (A,...
Definition sblock_scope.h:219
StmtSRef dst
The destination of the dependency relation.
Definition sblock_scope.h:228
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Dependency", DependencyObj, ffi::Object)
StmtSRef src
The source of the dependency relation.
Definition sblock_scope.h:226
DependencyObj(StmtSRef src, StmtSRef dst, DepKind kind)
Definition sblock_scope.h:223
static void RegisterReflection()
Definition sblock_scope.h:232
DepKind kind
The dependency kind.
Definition sblock_scope.h:230
DependencyObj(ffi::UnsafeInit tag)
Definition sblock_scope.h:221
Managed reference to DependencyObj.
Definition sblock_scope.h:246
Dependency(StmtSRef src, StmtSRef dst, DepKind kind)
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Dependency, ffi::ObjectRef, DependencyObj)
Managed reference to FunctionNode.
Definition function.h:105
An object with 1-to-1 correspondence with each block reference in the sref tree. This data structure ...
Definition sblock_scope.h:266
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:280
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:277
std::unordered_map< StmtSRef, ffi::Array< Dependency >, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > src2deps
Lookup table for the src of dependencies.
Definition sblock_scope.h:274
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:282
Managed reference to SBlockScopeObj.
Definition sblock_scope.h:308
SBlockScope(ffi::ObjectPtr< SBlockScopeObj > data)
Constructor from ffi::ObjectPtr<SBlockScopeObj>.
Definition sblock_scope.h:314
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:148
static std::unordered_map< const StmtNode *, StmtSRef > Create(IRModule mod, bool include_loops=true)
StmtSRef Tree Creator.
Definition sblock_scope.h:162
SRefTreeCreator(bool include_loops)
Definition sblock_scope.h:175
ffi::Optional< VisitInterrupt > Visit(ffi::AnyView value) override
Construct a visitor with the core expression hooks.
Definition sblock_scope.h:152
An object that refers to schedulable elements (block/for-loop) in TensorIR, aka "sref".
Definition sblock_scope.h:55
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.StmtSRef", StmtSRefObj, ffi::Object)
static void RegisterReflection()
Definition sblock_scope.h:71
StmtSRefObj * parent
The parent sref.
Definition sblock_scope.h:64
const StmtNode * stmt
The block or for stmt the object refers to.
Definition sblock_scope.h:62
void Reset()
Reset the object inplace to the invalid state.
Definition sblock_scope.h:80
const StmtType * StmtAs() const
Get the referenced statement with proper type checking. It serves the same purpose as ffi::ObjectRef:...
Definition sblock_scope.h:95
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:69
static constexpr const bool _type_mutable
Definition sblock_scope.h:76
Managed reference to StmtSRefObj.
Definition sblock_scope.h:108
static StmtSRef RootMark()
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtSRef, ffi::ObjectRef, StmtSRefObj)
static StmtSRef InlineMark()
StmtSRef(const StmtNode *stmt, StmtSRefObj *parent, int64_t seq_index)
The constructor.
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:207
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
S-TIR (Schedulable TIR) statements and attributes.
Native traversal and statement dispatch for schedulable TIR.
TIR Function.
TIRx statements and shared statement entry header.