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_S_TIR_ANALYSIS_H_
25 #define TVM_S_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/tirx/function.h>
31 #include <tvm/tirx/stmt.h>
32 
33 #include <optional>
34 
35 namespace tvm {
36 namespace tirx {
37 
50 TVM_DLL ffi::Array<ffi::Array<BufferRegion>> GetSBlockAccessRegion(
51  const SBlock& block, const ffi::Map<Var, Buffer>& buffer_var_map);
52 
61 TVM_DLL ffi::Array<ffi::Array<BufferRegion>> GetSBlockReadWriteRegion(
62  const SBlock& block, const ffi::Map<Var, Buffer>& buffer_var_map);
63 
72 TVM_DLL ffi::Map<Buffer, ffi::Optional<Stmt>> DetectBufferAccessLCA(const PrimFunc& func);
73 
89 
90 } // namespace tirx
91 
92 namespace arith {
93 class AnalyzerObj;
94 class Analyzer;
95 } // namespace arith
96 
97 namespace s_tir {
98 
99 using namespace tvm::tirx;
100 
106 TVM_DLL double EstimateTIRFlops(const Stmt& stmt);
107 
113 TVM_DLL double EstimateTIRFlops(const IRModule& mod);
114 
121 TVM_DLL bool IsPureFunction(const PrimFunc& func, bool assert_on_error = false);
122 
129 TVM_DLL bool VerifyGPUCode(const PrimFunc& func, ffi::Map<ffi::String, PrimExpr> constraints);
130 
135 };
136 
142 TVM_DLL std::optional<MemCpyDetails> IdentifyMemCpy(const For& loop,
143  const arith::Analyzer& analyzer);
144 
150 TVM_DLL ffi::Map<ffi::String, ffi::Map<ffi::String, int64_t>> CalculateAllocatedBytes(
151  const PrimFunc& func);
152 
158 TVM_DLL ffi::Map<ffi::String, ffi::Map<ffi::String, int64_t>> CalculateAllocatedBytes(
159  const IRModule& mod);
160 
165 TVM_DLL ffi::Array<tvm::transform::Pass> GetVTCMCompactionPasses();
166 
173 TVM_DLL bool VerifyVTCMLimit(const IRModule& mod, int64_t limit);
174 
181 TVM_DLL bool VerifyVTCMLimit(const PrimFunc& func, int64_t limit);
182 
183 namespace transform {
184 
187 
193 TVM_DLL Pass VerifyGPUCode(ffi::Map<ffi::String, PrimExpr> constraints);
194 
200 TVM_DLL Pass VerifyVTCMLimit(ffi::Optional<Target> default_target = std::nullopt);
201 
206 TVM_DLL Pass OOBChecker();
207 
208 } // namespace transform
209 } // namespace s_tir
210 } // namespace tvm
211 #endif // TVM_S_TIR_ANALYSIS_H_
Managed reference class to IRModuleNode.
Definition: module.h:255
Managed reference to AnalyzerObj.
Definition: analyzer.h:913
Managed reference to BufferRegionNode.
Definition: stmt.h:767
Managed reference to ForNode.
Definition: stmt.h:641
Managed reference to PrimFuncNode.
Definition: function.h:154
A block is a basic schedule unit in TIR.
Definition: stmt.h:851
Managed reference to SBlockNode.
Definition: stmt.h:898
Container of all statements.
Definition: stmt.h:64
Definition: transform.h:417
IRModule that holds the functions and type definitions.
tvm::transform::Pass Pass
Definition: transform.h:35
tvm::transform::PassContext PassContext
Definition: transform.h:37
Pass OOBChecker()
Statically check TIR code for out of bounds array access.
bool VerifyGPUCode(const PrimFunc &func, ffi::Map< ffi::String, PrimExpr > constraints)
Verify the correctness of a GPU code.
bool IsPureFunction(const PrimFunc &func, bool assert_on_error=false)
Analyze the side effect of a function.
bool VerifyVTCMLimit(const IRModule &mod, int64_t limit)
Verifies that the VTCM usage for all prim_funcs in the given IRModule.
ffi::Map< ffi::String, ffi::Map< ffi::String, int64_t > > CalculateAllocatedBytes(const PrimFunc &func)
Calculate the allocated memory per scope in bytes needed inside the TIR PrimFunc.
double EstimateTIRFlops(const Stmt &stmt)
Estimate the FLOPs of a TIR fragment.
std::optional< MemCpyDetails > IdentifyMemCpy(const For &loop, const arith::Analyzer &analyzer)
Identify whether a For loop is semantically equivalent to MemCpy.
ffi::Array< tvm::transform::Pass > GetVTCMCompactionPasses()
Get the list of lowering passes to calculate the compacted VTCM allocation size.
Definition: axis_group_graph.h:39
ffi::Map< Buffer, ffi::Optional< Stmt > > DetectBufferAccessLCA(const PrimFunc &func)
Detect the lowest common ancestor(LCA) of buffer access, including both high-level access(BufferLoad,...
const tirx::SBlockNode * 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...
ffi::Array< ffi::Array< BufferRegion > > GetSBlockReadWriteRegion(const SBlock &block, const ffi::Map< Var, Buffer > &buffer_var_map)
Auto detect the block read/write region according to its body stmt. An opaque access will be counted ...
ffi::Array< ffi::Array< BufferRegion > > GetSBlockAccessRegion(const SBlock &block, const ffi::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...
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:310
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Helper struct for return value of IdentifyMemCpy.
Definition: analysis.h:132
BufferRegion source
Definition: analysis.h:133
BufferRegion dest
Definition: analysis.h:134
Compilation target object.
TIR Function.
TIR statements.