tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_TIR_SCHEDULE_STATE_H_
24 #define TVM_TIR_SCHEDULE_STATE_H_
25 
26 #include <tvm/ir/module.h>
27 #include <tvm/tir/function.h>
29 
30 #include <unordered_map>
31 #include <utility>
32 
33 namespace tvm {
34 namespace tir {
35 
43 struct BlockInfo {
45  BlockScope scope{nullptr};
46  // The properties below are information about the current block realization under its parent scope
48  bool affine_binding{false};
53  bool region_cover{false};
54 
55  BlockInfo() = default;
56 
57  explicit BlockInfo(BlockScope scope, bool affine_binding = false, bool region_cover = false)
58  : scope(std::move(scope)), //
61 };
62 
67 enum ScheduleDebugMask : uint32_t {
72 };
73 
86 class ScheduleStateNode : public Object {
87  public:
95  std::unordered_map<StmtSRef, BlockInfo, ObjectPtrHash, ObjectPtrEqual> block_info;
97  std::unordered_map<const StmtNode*, StmtSRef> stmt2ref;
108 
110  v->Visit("mod", &mod);
111  // `block_info` is not visited
112  // `stmt2ref` is not visited
113  v->Visit("debug_mask", &debug_mask);
114  v->Visit("enable_check", &enable_check);
115  }
135  TVM_DLL void Replace(const tir::StmtSRef& src_sref, const Stmt& tgt_stmt,
136  const Map<Block, Block>& block_sref_reuse);
143  TVM_DLL void DebugVerify() const;
144 
145  static constexpr const char* _type_key = "tir.ScheduleState";
147 
148  /******** Property of blocks ********/
150  TVM_DLL BlockInfo GetBlockInfo(const StmtSRef& block_sref) const;
156  TVM_DLL void UpdateScopeBlockInfo(const Stmt& stmt);
162  BlockScope GetBlockScope(const StmtSRef& scope_root) const {
163  return GetBlockInfo(scope_root).scope;
164  }
170  bool IsAffineBlockBinding(const StmtSRef& block_sref) const {
171  return GetBlockInfo(block_sref).affine_binding;
172  }
179  bool IsRegionCoveredConsumer(const StmtSRef& consumer_block_sref) const {
180  return GetBlockInfo(consumer_block_sref).region_cover;
181  }
187  bool IsStagePipeline(const StmtSRef& scope_root) const {
188  return GetBlockScope(scope_root)->stage_pipeline;
189  }
190 };
191 
196 class ScheduleState : public ObjectRef {
197  public:
205  TVM_DLL explicit ScheduleState(IRModule mod, int debug_mask = 0, bool enable_check = true);
206 
208  ScheduleStateNode* get() const { return static_cast<ScheduleStateNode*>(data_.get()); }
209 
211 };
212 
213 } // namespace tir
214 } // namespace tvm
215 
216 #endif // TVM_TIR_SCHEDULE_STATE_H_
ScheduleDebugMask
The bitmask of the debug flag in the ScheduleStateNode.
Definition: state.h:67
BlockScope scope
Property of a block scope rooted at the block, storing dependencies in the scope. ...
Definition: state.h:45
void VisitAttrs(AttrVisitor *v)
Definition: state.h:109
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Verify the correctness of the sref tree.
Definition: state.h:69
Definition of two pillar data structure for TensorIR scheduling: StmtSRef, BlockScope.
Definition: loop_state.h:456
std::unordered_map< const StmtNode *, StmtSRef > stmt2ref
The reverse mapping from block/for-loop to their corresponding srefs.
Definition: state.h:97
TIR Function.
Managed reference to StmtSRefNode.
Definition: block_scope.h:102
base class of all object containers.
Definition: object.h:167
#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:744
bool IsAffineBlockBinding(const StmtSRef &block_sref) const
Check a cached flag indicating if the specific block has quasi-affine bindings.
Definition: state.h:170
Managed reference to BlockScopeNode.
Definition: block_scope.h:255
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
int debug_mask
Do extra correctness checking after the class creation and each time after calling the Replace method...
Definition: state.h:103
Container of all statements.
Definition: stmt.h:59
The state of scheduling, which exposes a Replace method as the primary interface for all the scheduli...
Definition: state.h:86
bool affine_binding
Property of a block, indicating the block realization binding is quasi-affine.
Definition: state.h:48
bool enable_check
Whether to enable prequisite checks for schedule primitives.
Definition: state.h:107
Managed reference to ScheduleStateNode.
Definition: state.h:196
bool region_cover
Property of a block, indicating each of the block&#39;s read regions is fully produced by its producers...
Definition: state.h:53
Base class of all object reference.
Definition: object.h:511
The information about a TensorIR block, it contains two categories of information 1) Info on the bloc...
Definition: state.h:43
BlockInfo(BlockScope scope, bool affine_binding=false, bool region_cover=false)
Definition: state.h:57
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Verify the correctness of affine_binding, region_cover and stage_pipeline.
Definition: state.h:71
Managed reference class to IRModuleNode.
Definition: module.h:348
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:187
bool IsRegionCoveredConsumer(const StmtSRef &consumer_block_sref) const
Check a cached flag indicating if each of the specific consumer block&#39;s read region is fully produced...
Definition: state.h:179
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
IRModule mod
The AST of the module being scheduled.
Definition: state.h:89
std::unordered_map< StmtSRef, BlockInfo, ObjectPtrHash, ObjectPtrEqual > block_info
Mapping from a block sref to its correpsonding BlockInfo, tracking the dependency inside the block sc...
Definition: state.h:95
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
BlockScope GetBlockScope(const StmtSRef &scope_root) const
Get the BlockScope correpsonding to the sref of scope root block.
Definition: state.h:162