tvm
analysis.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  */
19 
24 #ifndef TVM_TIR_ANALYSIS_H_
25 #define TVM_TIR_ANALYSIS_H_
26 
27 #include <tvm/ir/module.h>
28 #include <tvm/ir/transform.h>
29 #include <tvm/target/target.h>
30 #include <tvm/tir/expr.h>
31 #include <tvm/tir/function.h>
32 #include <tvm/tir/op_attr_types.h>
33 #include <tvm/tir/stmt.h>
34 
35 #include <optional>
36 #include <string>
37 
38 namespace tvm {
39 
40 namespace arith {
41 class Analyzer;
42 }
43 
44 namespace tir {
45 
61 struct ExprDeepEqual {
62  public:
63  TVM_DLL bool operator()(const PrimExpr& lhs, const PrimExpr& rhs) const;
64 };
65 
72 template <class FLambda>
73 inline void VisitPrimFuncs(const IRModule& mod, FLambda fvisit) {
74  for (const auto& kv : mod->functions) {
75  const BaseFunc& base_func = kv.second;
76  if (const auto* prim_func = base_func.as<PrimFuncNode>()) {
77  fvisit(prim_func);
78  }
79  }
80 }
81 
87 TVM_DLL double EstimateTIRFlops(const Stmt& stmt);
88 
94 TVM_DLL double EstimateTIRFlops(const IRModule& mod);
95 
102 TVM_DLL Array<Var> UndefinedVars(const Stmt& stmt, const Array<Var>& defs);
103 
109 TVM_DLL Array<Var> UndefinedVars(const PrimExpr& expr);
110 
117 TVM_DLL Array<Var> UndefinedVars(const PrimExpr& expr, const Array<Var>& defs);
118 
125 TVM_DLL CallEffectKind SideEffect(const PrimExpr& expr);
126 
138 TVM_DLL bool IsPureFunction(const PrimFunc& func, bool assert_on_error = false);
139 
146 TVM_DLL bool UsesVar(const Stmt& stmt, std::function<bool(const VarNode*)> vset_contains);
147 
154 TVM_DLL bool UsesVar(const PrimExpr& expr, std::function<bool(const VarNode*)> vset_contains);
155 
165 TVM_DLL bool VerifySSA(const PrimFunc& func);
166 
177 TVM_DLL bool VerifyMemory(const PrimFunc& func);
178 
198 TVM_DLL bool VerifyGPUCode(const PrimFunc& func, Map<String, PrimExpr> constraints);
199 
207 
214 TVM_DLL bool VerifyVTCMLimit(const IRModule& mod, Integer limit);
215 
222 TVM_DLL bool VerifyVTCMLimit(const PrimFunc& func, Integer limit);
223 
237  const Map<Var, Buffer>& buffer_var_map);
238 
248  const Map<Var, Buffer>& buffer_var_map);
249 
260 };
261 
271 TVM_DLL std::optional<MemCpyDetails> IdentifyMemCpy(const For& loop, arith::Analyzer* analyzer);
272 
277 TVM_DLL size_t CalculateExprComplexity(const PrimExpr& expr);
278 
284 TVM_DLL size_t CalculateConstantBytes(const PrimFunc& func, const Integer& constant_byte_alignment);
285 
292 TVM_DLL size_t CalculateWorkspaceBytes(const PrimFunc& func,
293  const Integer& workspace_byte_alignment);
294 
302 
310 
320 
341 TVM_DLL bool VerifyWellFormed(const PrimFunc& func, bool assert_mode = true);
342 
355 TVM_DLL bool VerifyWellFormed(const IRModule& mod, bool assert_mode = true);
356 
364 const PrimFuncNode* FindEntryFunc(const IRModule& mod, GlobalVar* result_g_var);
365 
381 
382 // Pass variants of verification analysis
383 // directly throws RuntimeError when verification fails.
384 namespace transform {
385 
388 
395 TVM_DLL Pass VerifySSA();
396 
403 TVM_DLL Pass VerifyMemory();
404 
414 
425 
435 TVM_DLL Pass OOBChecker();
436 
437 } // namespace transform
438 } // namespace tir
439 } // namespace tvm
440 #endif // TVM_TIR_ANALYSIS_H_
Managed reference to BaseFuncNode.
Definition: function.h:230
Managed reference to GlobalVarNode.
Definition: expr.h:487
Managed reference class to IRModuleNode.
Definition: module.h:366
Container of constant int that adds more constructors.
Definition: expr.h:632
Reference to PrimExprNode.
Definition: expr.h:115
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:629
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:910
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
A block is a basic schedule unit in TIR.
Definition: stmt.h:1258
Managed reference to BlockNode.
Definition: stmt.h:1325
Managed reference to BufferRegionNode.
Definition: stmt.h:1166
Managed reference to ForNode.
Definition: stmt.h:1029
Primitive functions that contains TIR statements.
Definition: function.h:46
Managed reference to PrimFuncNode.
Definition: function.h:145
Container of all statements.
Definition: stmt.h:59
A variable node in the IR.
Definition: var.h:48
Definition: transform.h:426
IRModule that holds the functions and type definitions.
tvm::transform::Pass Pass
Definition: transform.h:39
tvm::transform::PassContext PassContext
Definition: transform.h:41
Pass VerifyMemory()
Pass variant of VerifyMemory.
Pass VerifyVTCMLimit(Optional< Target > target=NullOpt)
Pass to checks if the size of the allocated vtcm memory satisfies the limit.
Pass VerifyGPUCode(Map< String, PrimExpr > constraints)
Pass variant of VerifyGPUCode.
Pass VerifySSA()
Pass variant of VerifySSA.
Pass OOBChecker()
Statically check TIR code for out of bounds array access.
const tir::BlockNode * FindAnchorBlock(const IRModule &mod)
Find the "anchor block" of the given module. We define the anchor block to be the block with (1) an i...
double EstimateTIRFlops(const Stmt &stmt)
Estimate the FLOPs of a TIR fragment.
void VisitPrimFuncs(const IRModule &mod, FLambda fvisit)
Visit the PrimFuncs in the IRModule.
Definition: analysis.h:73
size_t CalculateWorkspaceBytes(const PrimFunc &func, const Integer &workspace_byte_alignment)
Calculate the workspace size in bytes needed by the TIR allocates inside the TIR PrimFunc.
tvm::Map< String, tvm::Map< String, Integer > > CalculateAllocatedBytes(const PrimFunc &func)
Calculate the allocated memory per scope in bytes needed inside the TIR PrimFunc.
bool VerifySSA(const PrimFunc &func)
Verifies whether the IR stmt or Expr is in SSA form. That is: each Var is defined and assigned once(i...
bool VerifyVTCMLimit(const IRModule &mod, Integer limit)
Verifies that the VTCM usage for all prim_funcs in the given IRModule.
bool VerifyGPUCode(const PrimFunc &func, Map< String, PrimExpr > constraints)
Verify the correctness of a GPU code It will check the whether the amount of memory usage or the numb...
Array< Array< BufferRegion > > GetBlockAccessRegion(const Block &block, const Map< Var, Buffer > &buffer_var_map)
Auto detect the block access region according to its body stmt It will detect the access region as an...
size_t CalculateConstantBytes(const PrimFunc &func, const Integer &constant_byte_alignment)
Calculate the constants size in bytes needed by the TIR allocates inside the TIR PrimFunc.
CallEffectKind
The effect type of the call.
Definition: op_attr_types.h:88
Map< Buffer, Optional< Stmt > > DetectBufferAccessLCA(const PrimFunc &func)
Detect the lowest common ancestor(LCA) of buffer access, including both high-level access(BufferLoad,...
bool VerifyMemory(const PrimFunc &func)
Verify if memory accesses are legal for a specific target device type.
Array< tvm::transform::Pass > GetVTCMCompactionPasses()
Utility function to get the list of lowering passes to be applied to calculate the compacted VTCM all...
size_t CalculateExprComplexity(const PrimExpr &expr)
Calculate the expresion complexity based on number of symbols it contains.
Array< Array< BufferRegion > > GetBlockReadWriteRegion(const Block &block, const Map< Var, Buffer > &buffer_var_map)
Auto detect the block read/write region according to its body stmt. An opaque access will be counted ...
bool UsesVar(const Stmt &stmt, std::function< bool(const VarNode *)> vset_contains)
Whether the given Stmt uses any var in the given variable set.
std::optional< MemCpyDetails > IdentifyMemCpy(const For &loop, arith::Analyzer *analyzer)
Identify whether a For loop is semantically equivalent to MemCpy.
Array< Var > UndefinedVars(const Stmt &stmt, const Array< Var > &defs)
Find undefined vars in the statement.
CallEffectKind SideEffect(const PrimExpr &expr)
Analyze the side effect of an expression.
bool VerifyWellFormed(const PrimFunc &func, bool assert_mode=true)
Verify if the given TIR is well-formed. The verification includes:
const PrimFuncNode * FindEntryFunc(const IRModule &mod, GlobalVar *result_g_var)
Find the entry function of the given IRModule, i.e, functions marked by tir::attr::kIsEntryFunc,...
bool IsPureFunction(const PrimFunc &func, bool assert_on_error=false)
Analyze the side effect of a function.
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
TIR statements.
Compare two expressions recursively and check if they are equal to each other without var remapping.
Definition: analysis.h:61
bool operator()(const PrimExpr &lhs, const PrimExpr &rhs) const
Helper struct for return value of IdentifyMemCpy.
Definition: analysis.h:257
BufferRegion dest
Definition: analysis.h:259
BufferRegion source
Definition: analysis.h:258
Compilation target object.
TIR expressions.
TIR Function.
Attribute types in the Op registry for TIR ops.