tvm
Loading...
Searching...
No Matches
state.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_S_TIR_SCHEDULE_STATE_H_
24#define TVM_S_TIR_SCHEDULE_STATE_H_
25
26#include <tvm/base.h>
27#include <tvm/ffi/reflection/registry.h>
28#include <tvm/ir/module.h>
29#include <tvm/ir/prim/expr.h>
31#include <tvm/s_tir/stmt.h>
32#include <tvm/tirx/function.h>
33
34#include <unordered_map>
35#include <utility>
36
37namespace tvm {
38namespace s_tir {
39using namespace tvm::tirx;
40
48struct SBlockInfo {
50 SBlockScope scope{ffi::UnsafeInit()};
51 // The properties below are information about the current block realization under its parent scope
53 bool affine_binding{false};
58 bool region_cover{false};
68 bool stage_pipeline{false};
69
70 SBlockInfo() = default;
71
72 explicit SBlockInfo(SBlockScope scope, bool affine_binding = false, bool region_cover = false,
73 bool stage_pipeline = false)
74 : scope(std::move(scope)), //
78};
79
90
103class ScheduleStateObj : public ffi::Object {
104 public:
112 std::unordered_map<StmtSRef, SBlockInfo, ffi::ObjectPtrHash, ffi::ObjectPtrEqual> block_info;
114 std::unordered_map<const StmtObj*, StmtSRef> stmt2ref;
125
126 static void RegisterReflection() {
127 namespace refl = tvm::ffi::reflection;
128 refl::ObjectDef<ScheduleStateObj>()
129 .def_ro("mod", &ScheduleStateObj::mod)
130 .def_ro("debug_mask", &ScheduleStateObj::debug_mask)
131 .def_ro("enable_check", &ScheduleStateObj::enable_check);
132 }
133
153 TVM_DLL void Replace(const tirx::StmtSRef& src_sref, const Stmt& tgt_stmt,
154 const ffi::Map<SBlock, SBlock>& block_sref_reuse);
161 TVM_DLL void DebugVerify() const;
162
163 static constexpr const bool _type_mutable = true;
164 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.ScheduleState", ScheduleStateObj, ffi::Object);
165
166 /******** Property of blocks ********/
168 TVM_DLL SBlockInfo GetSBlockInfo(const StmtSRef& block_sref) const;
174 TVM_DLL void UpdateScopeSBlockInfo(const Stmt& stmt);
180 SBlockScope GetSBlockScope(const StmtSRef& scope_root) const {
181 return GetSBlockInfo(scope_root).scope;
182 }
188 bool IsAffineBlockBinding(const StmtSRef& block_sref) const {
189 return GetSBlockInfo(block_sref).affine_binding;
190 }
197 bool IsRegionCoveredConsumer(const StmtSRef& consumer_block_sref) const {
198 return GetSBlockInfo(consumer_block_sref).region_cover;
199 }
205 bool IsStagePipeline(const StmtSRef& scope_root) const {
206 return GetSBlockInfo(scope_root).stage_pipeline;
207 }
208};
209
214class ScheduleState : public ffi::ObjectRef {
215 public:
216 explicit ScheduleState(ffi::UnsafeInit tag, ffi::ObjectPtr<ScheduleStateObj> data)
217 : ffi::ObjectRef(tag) {
218 data_ = std::move(data);
219 }
220
228 TVM_DLL explicit ScheduleState(IRModule mod, int debug_mask = 0, bool enable_check = true);
229
231};
232
233} // namespace s_tir
234} // namespace tvm
235
236#endif // TVM_S_TIR_SCHEDULE_STATE_H_
TIR expressions.
TIR Function.
Managed reference class to IRModuleObj.
Definition module.h:292
Container of all statements.
Definition stmt.h:61
The state of scheduling, which exposes a Replace method as the primary interface for all the scheduli...
Definition state.h:103
bool IsRegionCoveredConsumer(const StmtSRef &consumer_block_sref) const
Check a cached flag indicating if each of the specific consumer block's read region is fully produced...
Definition state.h:197
void UpdateScopeSBlockInfo(const Stmt &stmt)
Recalculate the SBlockInfo recursively under stmt. If stmt is a SBlock itself, we will not reset its ...
bool enable_check
Whether to enable prequisite checks for schedule primitives.
Definition state.h:124
std::unordered_map< const StmtObj *, StmtSRef > stmt2ref
The reverse mapping from block/for-loop to their corresponding srefs.
Definition state.h:114
bool IsAffineBlockBinding(const StmtSRef &block_sref) const
Check a cached flag indicating if the specific block has quasi-affine bindings.
Definition state.h:188
std::unordered_map< StmtSRef, SBlockInfo, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > block_info
Mapping from a block sref to its correpsonding SBlockInfo, tracking the dependency inside the block s...
Definition state.h:112
static void RegisterReflection()
Definition state.h:126
int debug_mask
Do extra correctness checking after the class creation and each time after calling the Replace method...
Definition state.h:120
SBlockInfo GetSBlockInfo(const StmtSRef &block_sref) const
Returns the SBlockInfo correpsonding to the block sref.
IRModule mod
The AST of the module being scheduled.
Definition state.h:106
void DebugVerify() const
Trigger the verification according to the debug_mask bitmask. 1) If the bitmask kVerifySRefTree is on...
static constexpr const bool _type_mutable
Definition state.h:163
void Replace(const tirx::StmtSRef &src_sref, const Stmt &tgt_stmt, const ffi::Map< SBlock, SBlock > &block_sref_reuse)
Replace the part of the AST, as being pointed to by src_sref, with a specific statement tgt_stmt,...
SBlockScope GetSBlockScope(const StmtSRef &scope_root) const
Get the SBlockScope correpsonding to the sref of scope root block.
Definition state.h:180
bool IsStagePipeline(const StmtSRef &scope_root) const
Check a cached flag indicating if a block scope is an equivalence of a stage pipeline.
Definition state.h:205
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.ScheduleState", ScheduleStateObj, ffi::Object)
Managed reference to ScheduleStateObj.
Definition state.h:214
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ScheduleState, ffi::ObjectRef, ScheduleStateObj)
ScheduleState(ffi::UnsafeInit tag, ffi::ObjectPtr< ScheduleStateObj > data)
Definition state.h:216
ScheduleState(IRModule mod, int debug_mask=0, bool enable_check=true)
Construct a schedule state from an IRModule.
Managed reference to SBlockScopeObj.
Definition sblock_scope.h:309
Managed reference to StmtSRefObj.
Definition sblock_scope.h:109
Parent compiler symbol visibility.
S-TIR (Schedulable TIR) statements and attributes.
IRModule that holds the functions and type definitions.
ScheduleDebugMask
The bitmask of the debug flag in the ScheduleStateObj.
Definition state.h:84
@ kVerifySRefTree
Verify the correctness of the sref tree.
Definition state.h:86
@ kVerifyCachedFlags
Verify the correctness of affine_binding, region_cover and stage_pipeline.
Definition state.h:88
Definition axis_group_graph.h:41
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Definition of two pillar data structure for TensorIR scheduling: StmtSRef, SBlockScope.
The information about a TensorIR block, it contains two categories of information 1) Info on the bloc...
Definition state.h:48
bool region_cover
Property of a block, indicating each of the block's read regions is fully produced by its producers.
Definition state.h:58
SBlockInfo(SBlockScope scope, bool affine_binding=false, bool region_cover=false, bool stage_pipeline=false)
Definition state.h:72
SBlockScope scope
Property of a block scope rooted at the block, storing dependencies in the scope.
Definition state.h:50
bool affine_binding
Property of a block, indicating the block realization binding is quasi-affine.
Definition state.h:53
bool stage_pipeline
This property indicates that the block scope (rooted at its corresponding block) is equivalent to of ...
Definition state.h:68