25#ifndef TVM_S_TIR_SBLOCK_SCOPE_H_
26#define TVM_S_TIR_SBLOCK_SCOPE_H_
34#include <unordered_map>
72 namespace refl = tvm::ffi::reflection;
82 this->parent =
nullptr;
94 template <
typename StmtType>
96 if (
stmt !=
nullptr &&
stmt->IsInstance<StmtType>()) {
97 return static_cast<const StmtType*
>(
stmt);
152 ffi::Optional<VisitInterrupt>
Visit(ffi::AnyView value)
override {
153 if (value.as<
ExprNode>())
return std::nullopt;
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);
172 return std::move(creator->stmt2ref_);
182 void PushSRef(
const StmtNode* stmt);
185 void PopAndRecordSRef();
187 ffi::Optional<VisitInterrupt> Visit_(
const ForNode* loop)
final;
191 ffi::Optional<VisitInterrupt> Visit_(
const SeqStmtNode* seq_stmt)
final;
195 std::unordered_map<const StmtNode*, StmtSRef> stmt2ref_;
197 std::vector<StmtSRef> srefs_;
233 namespace refl = tvm::ffi::reflection;
234 refl::ObjectDef<DependencyObj>()
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>
283 namespace refl = tvm::ffi::reflection;
284 refl::ObjectDef<SBlockScopeObj>();
314 explicit SBlockScope(ffi::ObjectPtr<SBlockScopeObj> data) : ffi::ObjectRef(data) {
315 TVM_FFI_ICHECK(data !=
nullptr);
325 TVM_DLL
explicit SBlockScope(
const ffi::Array<StmtSRef>& child_block_srefs);
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.
TIRx statements and shared statement entry header.