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/s_tir/analysis.h>
30 #include <tvm/target/target.h>
31 #include <tvm/tirx/expr.h>
32 #include <tvm/tirx/function.h>
33 #include <tvm/tirx/op_attr_types.h>
34 #include <tvm/tirx/stmt.h>
35 
36 #include <string>
37 
38 namespace tvm {
39 
40 namespace arith {
41 class Analyzer;
42 }
43 
44 namespace tirx {
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 
88 TVM_DLL ffi::Array<Var> UndefinedVars(const Stmt& stmt, const ffi::Array<Var>& defs);
89 
95 TVM_DLL ffi::Array<Var> UndefinedVars(const PrimExpr& expr);
96 
103 TVM_DLL ffi::Array<Var> UndefinedVars(const PrimExpr& expr, const ffi::Array<Var>& defs);
104 
111 TVM_DLL CallEffectKind SideEffect(const PrimExpr& expr);
112 
119 TVM_DLL bool UsesVar(const Stmt& stmt, std::function<bool(const VarNode*)> vset_contains);
120 
127 TVM_DLL bool UsesVar(const PrimExpr& expr, std::function<bool(const VarNode*)> vset_contains);
128 
138 TVM_DLL bool VerifySSA(const PrimFunc& func);
139 
150 TVM_DLL bool VerifyMemory(const PrimFunc& func);
151 
156 TVM_DLL size_t CalculateExprComplexity(const PrimExpr& expr);
157 
163 TVM_DLL size_t CalculateConstantBytes(const PrimFunc& func, const Integer& constant_byte_alignment);
164 
171 TVM_DLL size_t CalculateWorkspaceBytes(const PrimFunc& func,
172  const Integer& workspace_byte_alignment);
173 
194 TVM_DLL bool VerifyWellFormed(const PrimFunc& func, bool assert_mode = true);
195 
208 TVM_DLL bool VerifyWellFormed(const IRModule& mod, bool assert_mode = true);
209 
217 const PrimFuncNode* FindEntryFunc(const IRModule& mod, GlobalVar* result_g_var);
218 
219 // Pass variants of verification analysis
220 // directly throws RuntimeError when verification fails.
221 namespace transform {
222 
225 
232 TVM_DLL Pass VerifySSA();
233 
240 TVM_DLL Pass VerifyMemory();
241 
250 /******** TIRx analysis helpers ********/
251 
259 TVM_DLL bool VerifyTIRxWellFormed(const PrimFunc& func, bool assert_mode = true,
260  bool device_func = false);
261 
269 TVM_DLL bool VerifyTIRxWellFormed(const IRModule& mod, bool assert_mode = true,
270  bool device_func = false);
271 
272 } // namespace transform
273 } // namespace tirx
274 } // namespace tvm
275 #endif // TVM_TIR_ANALYSIS_H_
Managed reference to BaseFuncNode.
Definition: function.h:250
Managed reference to GlobalVarNode.
Definition: expr.h:482
Managed reference class to IRModuleNode.
Definition: module.h:258
Container of constant int that adds more constructors.
Definition: expr.h:601
Reference to PrimExprNode.
Definition: expr.h:126
Primitive functions that contains TIR statements.
Definition: function.h:50
Managed reference to PrimFuncNode.
Definition: function.h:131
Container of all statements.
Definition: stmt.h:67
A variable node in the IR.
Definition: var.h:48
Definition: transform.h:400
IRModule that holds the functions and type definitions.
tvm::transform::Pass Pass
Definition: transform.h:35
tvm::transform::PassContext PassContext
Definition: transform.h:37
bool VerifyTIRxWellFormed(const PrimFunc &func, bool assert_mode=true, bool device_func=false)
Pass variant of VerifyGPUCode.
Pass VerifySSA()
Pass variant of VerifySSA.
Pass VerifyMemory()
Pass variant of VerifyMemory.
size_t CalculateExprComplexity(const PrimExpr &expr)
Calculate the expression complexity based on number of symbols it contains.
bool VerifyMemory(const PrimFunc &func)
Verify if memory accesses are legal for a specific target device type.
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 tirx::attr::kIsEntryFunc,...
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...
CallEffectKind SideEffect(const PrimExpr &expr)
Analyze the side effect of an expression.
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.
bool UsesVar(const Stmt &stmt, std::function< bool(const VarNode *)> vset_contains)
Whether the given Stmt uses any var in the given variable set.
ffi::Array< Var > UndefinedVars(const Stmt &stmt, const ffi::Array< Var > &defs)
Find undefined vars in the statement.
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
void VisitPrimFuncs(const IRModule &mod, FLambda fvisit)
Visit the PrimFuncs in the IRModule.
Definition: analysis.h:73
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:308
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
Analysis utilities for Schedulable TensorIR (S-TIR).
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
Compilation target object.
TIR expressions.
TIR Function.
Attribute types in the Op registry for TIR ops.
TIR statements.