Loading...
Searching...
No Matches
ExprUsesVar.h
Go to the documentation of this file.
#ifndef HALIDE_EXPR_USES_VAR_H
#define HALIDE_EXPR_USES_VAR_H
/** \file
* Defines a method to determine if an expression depends on some variables.
*/
#include "IR.h"
#include "IRVisitor.h"
#include "Scope.h"
namespace Halide {
namespace Internal {
template<typename T = void>
class ExprUsesVars : public IRGraphVisitor {
using IRGraphVisitor::visit;
const Scope<T> &vars;
Scope<Expr> scope;
void include(const Expr &e) override {
if (result) {
return;
}
IRGraphVisitor::include(e);
}
void include(const Stmt &s) override {
if (result) {
return;
}
IRGraphVisitor::include(s);
}
void visit_name(const std::string &name) {
if (vars.contains(name)) {
result = true;
} else if (const Expr *e = scope.find(name)) {
IRGraphVisitor::include(*e);
}
}
void visit(const Variable *op) override {
visit_name(op->name);
}
void visit(const Load *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const Store *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const Call *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const Provide *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const LetStmt *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const Let *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const Realize *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
void visit(const Allocate *op) override {
visit_name(op->name);
IRGraphVisitor::visit(op);
}
public:
ExprUsesVars(const Scope<T> &v, const Scope<Expr> *s = nullptr)
: vars(v) {
scope.set_containing_scope(s);
}
bool result = false;
};
/** Test if a statement or expression references or defines any of the
* variables in a scope, additionally considering variables bound to
* Expr's in the scope provided in the final argument.
*/
template<typename StmtOrExpr, typename T>
inline bool stmt_or_expr_uses_vars(const StmtOrExpr &e, const Scope<T> &v,
const Scope<Expr> &s = Scope<Expr>::empty_scope()) {
ExprUsesVars<T> uses(v, &s);
e.accept(&uses);
return uses.result;
}
/** Test if a statement or expression references or defines the given
* variable, additionally considering variables bound to Expr's in the
* scope provided in the final argument.
*/
template<typename StmtOrExpr>
inline bool stmt_or_expr_uses_var(const StmtOrExpr &e, const std::string &v,
const Scope<Expr> &s = Scope<Expr>::empty_scope()) {
Scope<> vars;
vars.push(v);
return stmt_or_expr_uses_vars<StmtOrExpr, void>(e, vars, s);
}
/** Test if an expression references or defines the given variable,
* additionally considering variables bound to Expr's in the scope
* provided in the final argument.
*/
inline bool expr_uses_var(const Expr &e, const std::string &v,
const Scope<Expr> &s = Scope<Expr>::empty_scope()) {
return stmt_or_expr_uses_var(e, v, s);
}
/** Test if a statement references or defines the given variable,
* additionally considering variables bound to Expr's in the scope
* provided in the final argument.
*/
inline bool stmt_uses_var(const Stmt &stmt, const std::string &v,
const Scope<Expr> &s = Scope<Expr>::empty_scope()) {
return stmt_or_expr_uses_var(stmt, v, s);
}
/** Test if an expression references or defines any of the variables
* in a scope, additionally considering variables bound to Expr's in
* the scope provided in the final argument.
*/
template<typename T>
inline bool expr_uses_vars(const Expr &e, const Scope<T> &v,
const Scope<Expr> &s = Scope<Expr>::empty_scope()) {
return stmt_or_expr_uses_vars(e, v, s);
}
/** Test if a statement references or defines any of the variables in
* a scope, additionally considering variables bound to Expr's in the
* scope provided in the final argument.
*/
template<typename T>
inline bool stmt_uses_vars(const Stmt &stmt, const Scope<T> &v,
const Scope<Expr> &s = Scope<Expr>::empty_scope()) {
return stmt_or_expr_uses_vars(stmt, v, s);
}
} // namespace Internal
} // namespace Halide
#endif
IR.h
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt).
IRVisitor.h
Defines the base class for things that recursively walk over the IR.
Scope.h
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
Halide::Internal::ExprUsesVars
Definition ExprUsesVar.h:16
Halide::Internal::ExprUsesVars::ExprUsesVars
ExprUsesVars(const Scope< T > &v, const Scope< Expr > *s=nullptr)
Definition ExprUsesVar.h:89
Halide::Internal::ExprUsesVars::result
bool result
Definition ExprUsesVar.h:93
Halide::Internal::IRGraphVisitor
A base class for algorithms that walk recursively over the IR without visiting the same node twice.
Definition IRVisitor.h:85
Halide::Internal::IRGraphVisitor::include
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...
Halide::Internal::IRGraphVisitor::visit
void visit(const IntImm *) override
These methods should call 'include' on the children to only visit them if they haven't been visited a...
Halide::Internal::Scope
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
Halide::Internal::Scope::empty_scope
static const Scope< T > & empty_scope()
A const ref to an empty scope.
Definition Scope.h:120
Halide::Internal::Scope::push
PushToken push(const std::string &name, T2 &&value)
Add a new (name, value) pair to the current scope.
Definition Scope.h:223
Halide::Internal
Definition AbstractGenerator.h:24
Halide::Internal::stmt_uses_var
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...
Definition ExprUsesVar.h:133
Halide::Internal::stmt_or_expr_uses_vars
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,...
Definition ExprUsesVar.h:101
Halide::Internal::stmt_uses_vars
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...
Definition ExprUsesVar.h:153
Halide::Internal::stmt_or_expr_uses_var
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 ...
Definition ExprUsesVar.h:113
Halide::Internal::expr_uses_vars
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...
Definition ExprUsesVar.h:143
Halide::Internal::expr_uses_var
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...
Definition ExprUsesVar.h:124
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::OutputFileType::stmt
@ stmt
Definition Module.h:43
Halide::Expr
A fragment of Halide syntax.
Definition Expr.h:258
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:371
Halide::Internal::Allocate::name
std::string name
Definition IR.h:372
Halide::Internal::Call
A function call.
Definition IR.h:490
Halide::Internal::Call::name
std::string name
Definition IR.h:491
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition IR.h:271
Halide::Internal::Let::name
std::string name
Definition IR.h:272
Halide::Internal::LetStmt
The statement form of a let node.
Definition IR.h:282
Halide::Internal::LetStmt::name
std::string name
Definition IR.h:283
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition IR.h:217
Halide::Internal::Load::name
std::string name
Definition IR.h:218
Halide::Internal::Provide
This defines the value of a function at a multi-dimensional location.
Definition IR.h:354
Halide::Internal::Provide::name
std::string name
Definition IR.h:355
Halide::Internal::Realize
Allocate a multi-dimensional buffer of the given type and size.
Definition IR.h:427
Halide::Internal::Realize::name
std::string name
Definition IR.h:428
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:333
Halide::Internal::Store::name
std::string name
Definition IR.h:334
Halide::Internal::Variable
A named variable.
Definition IR.h:801
Halide::Internal::Variable::name
std::string name
Definition IR.h:802