tvm
Loading...
Searching...
No Matches
stmt_functor_with_analyzer.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
25#ifndef TVM_TIRX_STMT_FUNCTOR_WITH_ANALYZER_H_
26#define TVM_TIRX_STMT_FUNCTOR_WITH_ANALYZER_H_
27
28#include <tvm/ffi/cast.h>
29#include <tvm/ffi/extra/structural_visit.h>
30#include <tvm/ir/prim/expr.h>
31#include <tvm/ir/scope_stack.h>
32#include <tvm/ir/with_context.h>
33#include <tvm/sym/analyzer.h>
34#include <tvm/tirx/analysis.h>
36
37#include <unordered_set>
38#include <utility>
39
40namespace tvm {
41namespace tirx {
42
44 public:
46
47 PrimExpr Simplify(const PrimExpr& expr) { return analyzer_->Simplify(expr); }
48
49 using StmtExprVisitor::Visit_;
50
51 ffi::Optional<VisitInterrupt> Visit_(const ForNode* op);
52 ffi::Optional<VisitInterrupt> Visit_(const BindNode* op);
53 ffi::Optional<VisitInterrupt> Visit_(const IfNode* op);
54 ffi::Optional<VisitInterrupt> Visit_(const RegionStmtNode* op);
55 ffi::Optional<VisitInterrupt> Visit_(const AssertStmtNode* op);
56 ffi::Optional<VisitInterrupt> Visit_(const CallNode* op);
57 ffi::Optional<VisitInterrupt> Visit_(const prim::LetNode* op);
58
59 // StmtExprVisitorWithAnalyzer deliberately does not handle Select nodes,
60 // because both sides of a Select node are visited regardless of the
61 // condition.
62
63 protected:
64 static void InitVTable(VTable* vtable);
65 explicit StmtExprVisitorWithAnalyzer(const VTable* vtable) : StmtExprVisitor(vtable) {}
68
71
78};
79
90 public:
91 using StmtExprMutator::Mutate;
92 using StmtExprMutator::Mutate_;
94 : StmtExprMutatorWithAnalyzer(analyzer.get()) {}
96 : StmtExprMutatorWithAnalyzer(analyzer, GlobalVTable()) {}
97
98 // override functions that need to populate the context information.
99 UnchangedOr<Stmt> Mutate_(const ForNode* op, InplaceMode inplace_mode) override;
100 UnchangedOr<Stmt> Mutate_(const BindNode* op, InplaceMode inplace_mode) override;
101 UnchangedOr<Stmt> Mutate_(const IfNode* op, InplaceMode inplace_mode) override;
102 UnchangedOr<Stmt> Mutate_(const RegionStmtNode* op, InplaceMode inplace_mode) override;
103 UnchangedOr<Stmt> Mutate_(const AssertStmtNode* op, InplaceMode inplace_mode) override;
104 UnchangedOr<Expr> Mutate_(const CallNode* op, InplaceMode inplace_mode) override;
105 UnchangedOr<PrimExpr> Mutate_(const prim::LetNode* op, InplaceMode inplace_mode) override;
106 UnchangedOr<PrimExpr> Mutate_(const prim::SelectNode* op, InplaceMode inplace_mode) override;
107
108 protected:
109 static void InitVTable(VTable* vtable);
111 : StmtExprMutator(vtable), analyzer_(analyzer) {}
112 static const VTable* GlobalVTable();
120
125 ffi::Array<PrimExpr> IterMapSimplifyWithContext(const ffi::Array<PrimExpr>& indices,
126 bool non_trivial_only);
127
132 // the following two fields are useful in case we want
133 // note however that iter map analysis are usually more
134 // expensive and we only encourage doing them during
135 // necessary cases like layout remapping
137 ffi::Map<PrimVar, Range> iter_vars_;
139 ffi::Array<PrimExpr> iter_predicates_;
145 template <typename FLambda>
146 void WithRecordIterPredicate(PrimExpr condition, FLambda callback) {
147 std::unordered_set<const VarNode*> iter_var_nodes;
148 for (const auto& [var, _] : iter_vars_) {
149 iter_var_nodes.insert(var.get());
150 }
151 auto f_use_itervar = [&iter_var_nodes](const tvm::VarNode* v) {
152 return iter_var_nodes.count(v);
153 };
154 auto walkfn = [&](const tvm::Var& var) -> ffi::Expected<ffi::WalkResult> {
155 return f_use_itervar(var.get()) ? ffi::WalkResult::Interrupt(ffi::VisitInterrupt(var))
156 : ffi::WalkResult::Advance();
157 };
158 // simple heuristics for detecting predicate
159 if (ffi::StructuralWalk<ffi::WalkOrder::kPreOrder>(condition, walkfn).has_value()) {
160 iter_predicates_.push_back(condition);
161 callback();
162 iter_predicates_.pop_back();
163 } else {
164 callback();
165 }
166 }
167};
168
169} // namespace tirx
170} // namespace tvm
171#endif // TVM_TIRX_STMT_FUNCTOR_WITH_ANALYZER_H_
Algebra expression simplifications.
Assert condition, if an error occurs, return the error message.
Definition stmt.h:260
Bind a variable to a value in the enclosing scope.
Definition stmt.h:135
Call corresponds to callable invocation.
Definition expr.h:935
A for loop, with possible type annotations.
Definition stmt.h:457
If statement.
Definition stmt.h:387
A dynamically dispatched functor on the type of the first argument.
Definition object_functor.h:85
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
A single-body statement whose semantics are defined by an operator. Operands are evaluated in the enc...
Definition stmt.h:189
A scope stack for maintaining hierarchical state during IR visiting.
Definition scope_stack.h:55
A local variable in the IR.
Definition expr.h:694
Managed reference to VarNode.
Definition expr.h:714
Let binding. Bind var to value then evaluate body.
Definition expr.h:727
return true_value if condition is true, otherwise return false_value.
Definition expr.h:684
Analyzer that contains bunch of sub-analyzers.
Definition analyzer.h:715
Managed reference to AnalyzerObj.
Definition analyzer.h:916
Managed reference to FunctionNode.
Definition function.h:105
IRMutator with an analyzer context.
Definition stmt_functor_with_analyzer.h:89
UnchangedOr< Stmt > Mutate_(const AssertStmtNode *op, InplaceMode inplace_mode) override
UnchangedOr< Stmt > Mutate_(const BindNode *op, InplaceMode inplace_mode) override
UnchangedOr< PrimExpr > Mutate_(const prim::LetNode *op, InplaceMode inplace_mode) override
static void InitVTable(VTable *vtable)
StmtExprMutatorWithAnalyzer(const sym::Analyzer &analyzer)
Definition stmt_functor_with_analyzer.h:93
UnchangedOr< Stmt > Mutate_(const ForNode *op, InplaceMode inplace_mode) override
UnchangedOr< Stmt > Mutate_(const RegionStmtNode *op, InplaceMode inplace_mode) override
StmtExprMutatorWithAnalyzer(sym::AnalyzerObj *analyzer)
Definition stmt_functor_with_analyzer.h:95
void WithRecordIterPredicate(PrimExpr condition, FLambda callback)
Run callback while trying to record iter predicate.
Definition stmt_functor_with_analyzer.h:146
UnchangedOr< Expr > Mutate_(const CallNode *op, InplaceMode inplace_mode) override
UnchangedOr< PrimExpr > Mutate_(const prim::SelectNode *op, InplaceMode inplace_mode) override
static const VTable * GlobalVTable()
UnchangedOr< Stmt > Mutate_(const IfNode *op, InplaceMode inplace_mode) override
void MarkTensorParamShapes(const Function &func)
Mark all tensor-parameter shape values as positive values.
ffi::Array< PrimExpr > IterMapSimplifyWithContext(const ffi::Array< PrimExpr > &indices, bool non_trivial_only)
Use internal bound information to perform inter map simplification of indices.
ffi::Array< PrimExpr > iter_predicates_
iterator predicates
Definition stmt_functor_with_analyzer.h:139
sym::AnalyzerObj * analyzer_
internal analyzer field.
Definition stmt_functor_with_analyzer.h:129
ffi::Map< PrimVar, Range > iter_vars_
Recorded loop iterators.
Definition stmt_functor_with_analyzer.h:137
StmtExprMutatorWithAnalyzer(sym::AnalyzerObj *analyzer, const VTable *vtable)
Definition stmt_functor_with_analyzer.h:110
ScopeStack< WithGroup< sym::ConstraintContext > > constraint_scope_
Scope stack for accumulated assert constraints.
Definition stmt_functor_with_analyzer.h:131
Mutator that recursively mutates stmts and exprs on them.
Definition stmt_functor.h:221
Definition stmt_functor_with_analyzer.h:43
ffi::Optional< VisitInterrupt > Visit_(const BindNode *op)
ScopeStack< WithGroup< sym::ConstraintContext > > constraint_scope_
Scope stack for accumulated assert constraints.
Definition stmt_functor_with_analyzer.h:70
PrimExpr ExtractRealCondition(PrimExpr condition) const
Extract a constraint from a conditional statement.
static void InitVTable(VTable *vtable)
ffi::Optional< VisitInterrupt > Visit_(const CallNode *op)
sym::Analyzer analyzer_
internal analyzer field.
Definition stmt_functor_with_analyzer.h:67
StmtExprVisitorWithAnalyzer(const VTable *vtable)
Definition stmt_functor_with_analyzer.h:65
ffi::Optional< VisitInterrupt > Visit_(const IfNode *op)
ffi::Optional< VisitInterrupt > Visit_(const AssertStmtNode *op)
ffi::Optional< VisitInterrupt > Visit_(const prim::LetNode *op)
ffi::Optional< VisitInterrupt > Visit_(const ForNode *op)
ffi::Optional< VisitInterrupt > Visit_(const RegionStmtNode *op)
Native visitor for TIRx statements and their expression operands.
Definition stmt_functor.h:176
TIR expressions.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define TVM_DEFINE_OBJECT_FUNCTOR_DEFAULT_CONSTRUCTOR(Class, Parent)
Define a default constructor backed by one initialized native dispatch table.
Definition object_functor.h:529
#define TVM_TIRX_DLL
Definition base.h:83
A generic scope stack for managing hierarchical state during IR visiting.
Analysis utilities and passes for TIR.
Functors for tirx stmts utility functions to call common functors.
RAII wrapper function to enter and exit a context object similar to python's with syntax.