tvm
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/ffi/reflection/registry.h>
27 #include <tvm/ir/module.h>
28 #include <tvm/tir/block_scope.h>
29 #include <tvm/tir/function.h>
30 
31 #include <unordered_map>
32 #include <utility>
33 
34 namespace tvm {
35 namespace s_tir {
36 using namespace tvm::tir;
37 
45 struct SBlockInfo {
47  SBlockScope scope{ffi::UnsafeInit()};
48  // The properties below are information about the current block realization under its parent scope
50  bool affine_binding{false};
55  bool region_cover{false};
65  bool stage_pipeline{false};
66 
67  SBlockInfo() = default;
68 
69  explicit SBlockInfo(SBlockScope scope, bool affine_binding = false, bool region_cover = false,
70  bool stage_pipeline = false)
71  : scope(std::move(scope)), //
72  affine_binding(affine_binding), //
73  region_cover(region_cover),
74  stage_pipeline(stage_pipeline) {}
75 };
76 
81 enum ScheduleDebugMask : uint32_t {
86 };
87 
100 class ScheduleStateNode : public Object {
101  public:
109  std::unordered_map<StmtSRef, SBlockInfo, ObjectPtrHash, ObjectPtrEqual> block_info;
111  std::unordered_map<const StmtNode*, StmtSRef> stmt2ref;
122 
123  static void RegisterReflection() {
124  namespace refl = tvm::ffi::reflection;
125  refl::ObjectDef<ScheduleStateNode>()
126  .def_ro("mod", &ScheduleStateNode::mod)
127  .def_ro("debug_mask", &ScheduleStateNode::debug_mask)
128  .def_ro("enable_check", &ScheduleStateNode::enable_check);
129  }
130 
150  TVM_DLL void Replace(const tir::StmtSRef& src_sref, const Stmt& tgt_stmt,
151  const ffi::Map<SBlock, SBlock>& block_sref_reuse);
158  TVM_DLL void DebugVerify() const;
159 
160  static constexpr const bool _type_mutable = true;
161  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.ScheduleState", ScheduleStateNode, Object);
162 
163  /******** Property of blocks ********/
165  TVM_DLL SBlockInfo GetSBlockInfo(const StmtSRef& block_sref) const;
171  TVM_DLL void UpdateScopeSBlockInfo(const Stmt& stmt);
177  SBlockScope GetSBlockScope(const StmtSRef& scope_root) const {
178  return GetSBlockInfo(scope_root).scope;
179  }
185  bool IsAffineBlockBinding(const StmtSRef& block_sref) const {
186  return GetSBlockInfo(block_sref).affine_binding;
187  }
194  bool IsRegionCoveredConsumer(const StmtSRef& consumer_block_sref) const {
195  return GetSBlockInfo(consumer_block_sref).region_cover;
196  }
202  bool IsStagePipeline(const StmtSRef& scope_root) const {
203  return GetSBlockInfo(scope_root).stage_pipeline;
204  }
205 };
206 
211 class ScheduleState : public ObjectRef {
212  public:
220  TVM_DLL explicit ScheduleState(IRModule mod, int debug_mask = 0, bool enable_check = true);
221 
223 };
224 
225 } // namespace s_tir
226 } // namespace tvm
227 
228 #endif // TVM_S_TIR_SCHEDULE_STATE_H_
Definition of two pillar data structure for TensorIR scheduling: StmtSRef, SBlockScope.
Managed reference class to IRModuleNode.
Definition: module.h:256
The state of scheduling, which exposes a Replace method as the primary interface for all the scheduli...
Definition: state.h:100
static void RegisterReflection()
Definition: state.h:123
IRModule mod
The AST of the module being scheduled.
Definition: state.h:103
bool IsAffineBlockBinding(const StmtSRef &block_sref) const
Check a cached flag indicating if the specific block has quasi-affine bindings.
Definition: state.h:185
SBlockScope GetSBlockScope(const StmtSRef &scope_root) const
Get the SBlockScope correpsonding to the sref of scope root block.
Definition: state.h:177
SBlockInfo GetSBlockInfo(const StmtSRef &block_sref) const
Returns the SBlockInfo correpsonding to the block sref.
void DebugVerify() const
Trigger the verification according to the debug_mask bitmask. 1) If the bitmask kVerifySRefTree is on...
std::unordered_map< StmtSRef, SBlockInfo, ObjectPtrHash, ObjectPtrEqual > block_info
Mapping from a block sref to its correpsonding SBlockInfo, tracking the dependency inside the block s...
Definition: state.h:109
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:202
int debug_mask
Do extra correctness checking after the class creation and each time after calling the Replace method...
Definition: state.h:117
bool enable_check
Whether to enable prequisite checks for schedule primitives.
Definition: state.h:121
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.ScheduleState", ScheduleStateNode, Object)
void UpdateScopeSBlockInfo(const Stmt &stmt)
Recalculate the SBlockInfo recursively under stmt. If stmt is a SBlock itself, we will not reset its ...
void Replace(const tir::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,...
std::unordered_map< const StmtNode *, StmtSRef > stmt2ref
The reverse mapping from block/for-loop to their corresponding srefs.
Definition: state.h:111
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:194
Managed reference to ScheduleStateNode.
Definition: state.h:211
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ScheduleState, ObjectRef, ScheduleStateNode)
ScheduleState(IRModule mod, int debug_mask=0, bool enable_check=true)
Construct a schedule state from an IRModule.
Managed reference to SBlockScopeNode.
Definition: block_scope.h:292
Managed reference to StmtSRefNode.
Definition: block_scope.h:106
Container of all statements.
Definition: stmt.h:63
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
ScheduleDebugMask
The bitmask of the debug flag in the ScheduleStateNode.
Definition: state.h:81
@ kVerifySRefTree
Verify the correctness of the sref tree.
Definition: state.h:83
@ kVerifyCachedFlags
Verify the correctness of affine_binding, region_cover and stage_pipeline.
Definition: state.h:85
Definition: extracted_task.h:30
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:308
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
The information about a TensorIR block, it contains two categories of information 1) Info on the bloc...
Definition: state.h:45
SBlockInfo(SBlockScope scope, bool affine_binding=false, bool region_cover=false, bool stage_pipeline=false)
Definition: state.h:69
TIR Function.