Halide 22.0.0 Halide compiler and libraries

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
15 namespace Halide {
16 namespace 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. */
24 Expr 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. */
28 Stmt substitute(const std::string &name, const Expr &replacement, const Stmt &stmt);
29
30/** Substitute variables with names in the map. */
31// @{
32 Expr substitute(const std::map<std::string, Expr> &replacements, const Expr &expr);
33 Stmt substitute(const std::map<std::string, Expr> &replacements, const Stmt &stmt);
34// @}
35
36/** Substitute expressions for other expressions. */
37// @{
38 Expr substitute(const Expr &find, const Expr &replacement, const Expr &expr);
39 Stmt substitute(const Expr &find, const Expr &replacement, const Stmt &stmt);
40// @}
41
42/** Substitute a container of Exprs or Stmts out of place */
43 template<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// @{
55 Expr graph_substitute(const std::string &name, const Expr &replacement, const Expr &expr);
56 Stmt graph_substitute(const std::string &name, const Expr &replacement, const Stmt &stmt);
57 Expr graph_substitute(const Expr &find, const Expr &replacement, const Expr &expr);
58 Stmt 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// @{
67 Expr substitute_in_all_lets(const Expr &expr);
68 Stmt substitute_in_all_lets(const Stmt &stmt);
69// @}
70
71} // namespace Internal
72} // namespace Halide
73
74#endif
Expr.h
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Halide::Internal::graph_substitute
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).
Halide::Internal::substitute_in_all_lets
Expr substitute_in_all_lets(const Expr &expr)
Substitute in all let Exprs in a piece of IR.
Halide::Internal::substitute
Expr substitute(const std::string &name, const Expr &replacement, const Expr &expr)
Substitute variables with the given name with the replacement expression within expr.
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:258
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:427