Halide 21.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
Substitute.h
Go to the documentation of this file.
1#ifndef HALIDE_SUBSTITUTE_H
2#define HALIDE_SUBSTITUTE_H
3
4/** \file
5 *
6 * Defines methods for substituting out variables in expressions and
7 * statements. */
8
9#include <algorithm>
10#include <iterator>
11#include <map>
12
13#include "Expr.h"
14
15namespace Halide {
16namespace Internal {
17
18/** Substitute variables with the given name with the replacement
19 * expression within expr. This is a dangerous thing to do if variable
20 * names have not been uniquified. While it won't traverse inside let
21 * statements with the same name as the first argument, moving a piece
22 * of syntax around can change its meaning, because it can cross lets
23 * that redefine variable names that it includes references to. */
24Expr substitute(const std::string &name, const Expr &replacement, const Expr &expr);
25
26/** Substitute variables with the given name with the replacement
27 * expression within stmt. */
28Stmt substitute(const std::string &name, const Expr &replacement, const Stmt &stmt);
29
30/** Substitute variables with names in the map. */
31// @{
32Expr substitute(const std::map<std::string, Expr> &replacements, const Expr &expr);
33Stmt substitute(const std::map<std::string, Expr> &replacements, const Stmt &stmt);
34// @}
35
36/** Substitute expressions for other expressions. */
37// @{
38Expr substitute(const Expr &find, const Expr &replacement, const Expr &expr);
39Stmt substitute(const Expr &find, const Expr &replacement, const Stmt &stmt);
40// @}
41
42/** Substitute a container of Exprs or Stmts out of place */
43template<typename T>
44T substitute(const std::map<std::string, Expr> &replacements, const T &container) {
45 T output;
46 std::transform(container.begin(), container.end(), std::back_inserter(output), [&](const auto &expr_or_stmt) {
47 return substitute(replacements, expr_or_stmt);
48 });
49 return output;
50}
51
52/** Substitutions where the IR may be a general graph (and not just a
53 * DAG). */
54// @{
55Expr graph_substitute(const std::string &name, const Expr &replacement, const Expr &expr);
56Stmt graph_substitute(const std::string &name, const Expr &replacement, const Stmt &stmt);
57Expr graph_substitute(const Expr &find, const Expr &replacement, const Expr &expr);
58Stmt graph_substitute(const Expr &find, const Expr &replacement, const Stmt &stmt);
59// @}
60
61/** Substitute in all let Exprs in a piece of IR. Doesn't substitute
62 * in let stmts, as this may change the meaning of the IR (e.g. by
63 * moving a load after a store). Produces graphs of IR, so don't use
64 * non-graph-aware visitors or mutators on it until you've CSE'd the
65 * result. */
66// @{
69// @}
70
71} // namespace Internal
72} // namespace Halide
73
74#endif
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Expr graph_substitute(const std::string &name, const Expr &replacement, const Expr &expr)
Substitutions where the IR may be a general graph (and not just a DAG).
Expr substitute_in_all_lets(const Expr &expr)
Substitute in all let Exprs in a piece of IR.
Expr substitute(const std::string &name, const Expr &replacement, const Expr &expr)
Substitute variables with the given name with the replacement expression within expr.
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
A fragment of Halide syntax.
Definition Expr.h:258
A reference-counted handle to a statement node.
Definition Expr.h:427