Halide 20.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 <map>
11
12#include "Expr.h"
13
14namespace Halide {
15namespace Internal {
16
17/** Substitute variables with the given name with the replacement
18 * expression within expr. This is a dangerous thing to do if variable
19 * names have not been uniquified. While it won't traverse inside let
20 * statements with the same name as the first argument, moving a piece
21 * of syntax around can change its meaning, because it can cross lets
22 * that redefine variable names that it includes references to. */
23Expr substitute(const std::string &name, const Expr &replacement, const Expr &expr);
24
25/** Substitute variables with the given name with the replacement
26 * expression within stmt. */
27Stmt substitute(const std::string &name, const Expr &replacement, const Stmt &stmt);
28
29/** Substitute variables with names in the map. */
30// @{
31Expr substitute(const std::map<std::string, Expr> &replacements, const Expr &expr);
32Stmt substitute(const std::map<std::string, Expr> &replacements, const Stmt &stmt);
33// @}
34
35/** Substitute expressions for other expressions. */
36// @{
37Expr substitute(const Expr &find, const Expr &replacement, const Expr &expr);
38Stmt substitute(const Expr &find, const Expr &replacement, const Stmt &stmt);
39// @}
40
41/** Substitute a container of Exprs or Stmts out of place */
42template<typename T>
43T substitute(const std::map<std::string, Expr> &replacements, const T &container) {
44 T output;
45 std::transform(container.begin(), container.end(), std::back_inserter(output), [&](const auto &expr_or_stmt) {
46 return substitute(replacements, expr_or_stmt);
47 });
48 return output;
49}
50
51/** Substitutions where the IR may be a general graph (and not just a
52 * DAG). */
53// @{
54Expr graph_substitute(const std::string &name, const Expr &replacement, const Expr &expr);
55Stmt graph_substitute(const std::string &name, const Expr &replacement, const Stmt &stmt);
56Expr graph_substitute(const Expr &find, const Expr &replacement, const Expr &expr);
57Stmt graph_substitute(const Expr &find, const Expr &replacement, const Stmt &stmt);
58// @}
59
60/** Substitute in all let Exprs in a piece of IR. Doesn't substitute
61 * in let stmts, as this may change the meaning of the IR (e.g. by
62 * moving a load after a store). Produces graphs of IR, so don't use
63 * non-graph-aware visitors or mutators on it until you've CSE'd the
64 * result. */
65// @{
68// @}
69
70} // namespace Internal
71} // namespace Halide
72
73#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,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
A fragment of Halide syntax.
Definition Expr.h:258
A reference-counted handle to a statement node.
Definition Expr.h:427