Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
ExprUsesVar.h
Go to the documentation of this file.
1#ifndef HALIDE_EXPR_USES_VAR_H
2#define HALIDE_EXPR_USES_VAR_H
3
4/** \file
5 * Defines a method to determine if an expression depends on some variables.
6 */
7
8#include "IR.h"
9#include "IRVisitor.h"
10#include "Scope.h"
11
12namespace Halide {
13namespace Internal {
14
15template<typename T = void>
18
19 const Scope<T> &vars;
20 Scope<Expr> scope;
21
22 void include(const Expr &e) override {
23 if (result) {
24 return;
25 }
27 }
28
29 void include(const Stmt &s) override {
30 if (result) {
31 return;
32 }
34 }
35
36 void visit_name(const std::string &name) {
37 if (vars.contains(name)) {
38 result = true;
39 } else if (const Expr *e = scope.find(name)) {
41 }
42 }
43
44 void visit(const Variable *op) override {
45 visit_name(op->name);
46 }
47
48 void visit(const Load *op) override {
49 visit_name(op->name);
51 }
52
53 void visit(const Store *op) override {
54 visit_name(op->name);
56 }
57
58 void visit(const Call *op) override {
59 visit_name(op->name);
61 }
62
63 void visit(const Provide *op) override {
64 visit_name(op->name);
66 }
67
68 void visit(const LetStmt *op) override {
69 visit_name(op->name);
71 }
72
73 void visit(const Let *op) override {
74 visit_name(op->name);
76 }
77
78 void visit(const Realize *op) override {
79 visit_name(op->name);
81 }
82
83 void visit(const Allocate *op) override {
84 visit_name(op->name);
86 }
87
88public:
89 ExprUsesVars(const Scope<T> &v, const Scope<Expr> *s = nullptr)
90 : vars(v) {
91 scope.set_containing_scope(s);
92 }
93 bool result = false;
94};
95
96/** Test if a statement or expression references or defines any of the
97 * variables in a scope, additionally considering variables bound to
98 * Expr's in the scope provided in the final argument.
99 */
100template<typename StmtOrExpr, typename T>
101inline bool stmt_or_expr_uses_vars(const StmtOrExpr &e, const Scope<T> &v,
103 ExprUsesVars<T> uses(v, &s);
104 e.accept(&uses);
105 return uses.result;
106}
107
108/** Test if a statement or expression references or defines the given
109 * variable, additionally considering variables bound to Expr's in the
110 * scope provided in the final argument.
111 */
112template<typename StmtOrExpr>
113inline bool stmt_or_expr_uses_var(const StmtOrExpr &e, const std::string &v,
115 Scope<> vars;
116 vars.push(v);
118}
119
120/** Test if an expression references or defines the given variable,
121 * additionally considering variables bound to Expr's in the scope
122 * provided in the final argument.
123 */
124inline bool expr_uses_var(const Expr &e, const std::string &v,
126 return stmt_or_expr_uses_var(e, v, s);
127}
128
129/** Test if a statement references or defines the given variable,
130 * additionally considering variables bound to Expr's in the scope
131 * provided in the final argument.
132 */
133inline bool stmt_uses_var(const Stmt &stmt, const std::string &v,
135 return stmt_or_expr_uses_var(stmt, v, s);
136}
137
138/** Test if an expression references or defines any of the variables
139 * in a scope, additionally considering variables bound to Expr's in
140 * the scope provided in the final argument.
141 */
142template<typename T>
143inline bool expr_uses_vars(const Expr &e, const Scope<T> &v,
145 return stmt_or_expr_uses_vars(e, v, s);
146}
147
148/** Test if a statement references or defines any of the variables in
149 * a scope, additionally considering variables bound to Expr's in the
150 * scope provided in the final argument.
151 */
152template<typename T>
153inline bool stmt_uses_vars(const Stmt &stmt, const Scope<T> &v,
155 return stmt_or_expr_uses_vars(stmt, v, s);
156}
157
158} // namespace Internal
159} // namespace Halide
160
161#endif
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Defines the base class for things that recursively walk over the IR.
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
ExprUsesVars(const Scope< T > &v, const Scope< Expr > *s=nullptr)
Definition ExprUsesVar.h:89
A base class for algorithms that walk recursively over the IR without visiting the same node twice.
Definition IRVisitor.h:85
virtual void include(const Expr &)
By default these methods add the node to the visited set, and return whether or not it was already th...
void visit(const IntImm *) override
These methods should call 'include' on the children to only visit them if they haven't been visited a...
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition Scope.h:94
const T2 * find(const std::string &name) const
Returns a const pointer to an entry if it exists in this scope or any containing scope,...
Definition Scope.h:159
bool contains(const std::string &name) const
Tests if a name is in scope.
Definition Scope.h:186
PushToken push(const std::string &name, T2 &&value)
Add a new (name, value) pair to the current scope.
Definition Scope.h:223
void set_containing_scope(const Scope< T > *s)
Set the parent scope.
Definition Scope.h:113
bool stmt_uses_var(const Stmt &stmt, const std::string &v, const Scope< Expr > &s=Scope< Expr >::empty_scope())
Test if a statement references or defines the given variable, additionally considering variables boun...
bool stmt_or_expr_uses_vars(const StmtOrExpr &e, const Scope< T > &v, const Scope< Expr > &s=Scope< Expr >::empty_scope())
Test if a statement or expression references or defines any of the variables in a scope,...
bool stmt_uses_vars(const Stmt &stmt, const Scope< T > &v, const Scope< Expr > &s=Scope< Expr >::empty_scope())
Test if a statement references or defines any of the variables in a scope, additionally considering v...
bool stmt_or_expr_uses_var(const StmtOrExpr &e, const std::string &v, const Scope< Expr > &s=Scope< Expr >::empty_scope())
Test if a statement or expression references or defines the given variable, additionally considering ...
bool expr_uses_vars(const Expr &e, const Scope< T > &v, const Scope< Expr > &s=Scope< Expr >::empty_scope())
Test if an expression references or defines any of the variables in a scope, additionally considering...
bool expr_uses_var(const Expr &e, const std::string &v, const Scope< Expr > &s=Scope< Expr >::empty_scope())
Test if an expression references or defines the given variable, additionally considering variables bo...
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
A fragment of Halide syntax.
Definition Expr.h:258
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:371
std::string name
Definition IR.h:372
A function call.
Definition IR.h:490
std::string name
Definition IR.h:491
A let expression, like you might find in a functional language.
Definition IR.h:271
std::string name
Definition IR.h:272
The statement form of a let node.
Definition IR.h:282
std::string name
Definition IR.h:283
Load a value from a named symbol if predicate is true.
Definition IR.h:217
std::string name
Definition IR.h:218
This defines the value of a function at a multi-dimensional location.
Definition IR.h:354
std::string name
Definition IR.h:355
Allocate a multi-dimensional buffer of the given type and size.
Definition IR.h:427
std::string name
Definition IR.h:428
A reference-counted handle to a statement node.
Definition Expr.h:427
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:333
std::string name
Definition IR.h:334
A named variable.
Definition IR.h:772
std::string name
Definition IR.h:773