Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
Associativity.h
Go to the documentation of this file.
1#ifndef HALIDE_ASSOCIATIVITY_H
2#define HALIDE_ASSOCIATIVITY_H
3
4/** \file
5 *
6 * Methods for extracting an associative operator from a Func's update definition
7 * if there is any and computing the identity of the associative operator.
8 */
9#include <string>
10#include <vector>
11
12#include "AssociativeOpsTable.h"
13#include "Expr.h"
14
15namespace Halide {
16namespace Internal {
17
18/**
19 * Represent the equivalent associative op of an update definition.
20 * For example, the following associative Expr, min(f(x), g(r.x) + 2),
21 * where f(x) is the self-recurrence term, is represented as:
22 \code
23 AssociativeOp assoc(
24 AssociativePattern(min(x, y), +inf, true),
25 {Replacement("x", f(x))},
26 {Replacement("y", g(r.x) + 2)},
27 true
28 );
29 \endcode
30 *
31 * 'pattern' contains the list of equivalent binary/unary operators (+ identities)
32 * for each Tuple element in the update definition. 'pattern' also contains
33 * a boolean that indicates if the op is also commutative. 'xs' and 'ys'
34 * contain the corresponding definition of each variable in the list of
35 * binary operators.
36 *
37 * For unary operator, 'xs' is not set, i.e. it will be a pair of empty string
38 * and undefined Expr: {"", Expr()}. 'pattern' will only contain the 'y' term in
39 * this case. For example, min(g(r.x), 4), will be represented as:
40 \code
41 AssociativeOp assoc(
42 AssociativePattern(y, 0, false),
43 {Replacement("", Expr())},
44 {Replacement("y", min(g(r.x), 4))},
45 true
46 );
47 \endcode
48 *
49 * Self-assignment, f(x) = f(x), will be represented as:
50 \code
51 AssociativeOp assoc(
52 AssociativePattern(x, 0, true),
53 {Replacement("x", f(x))},
54 {Replacement("", Expr())},
55 true
56 );
57 \endcode
58 * For both unary operator and self-assignment cases, the identity does not
59 * matter. It can be anything.
60 */
62 struct Replacement {
63 /** Variable name that is used to replace the expr in 'op'. */
64 std::string var;
66
67 Replacement() = default;
68 Replacement(const std::string &var, Expr expr)
69 : var(var), expr(std::move(expr)) {
70 }
71
72 bool operator==(const Replacement &other) const {
73 return (var == other.var) && equal(expr, other.expr);
74 }
75 bool operator!=(const Replacement &other) const {
76 return !(*this == other);
77 }
78 };
79
80 /** List of pairs of binary associative op and its identity. */
82 std::vector<Replacement> xs;
83 std::vector<Replacement> ys;
84 bool is_associative = false;
85
86 AssociativeOp() = default;
88 : pattern(size), xs(size), ys(size) {
89 }
90 AssociativeOp(const AssociativePattern &p, const std::vector<Replacement> &xs,
91 const std::vector<Replacement> &ys, bool is_associative)
93 }
94
95 bool associative() const {
96 return is_associative;
97 }
98 bool commutative() const {
100 }
101 size_t size() const {
102 return pattern.size();
103 }
104};
105
106/**
107 * Given an update definition of a Func 'f', determine its equivalent
108 * associative binary/unary operator if there is any. 'is_associative'
109 * indicates if the operation was successfuly proven as associative.
110 */
112 const std::string &f, std::vector<Expr> args, std::vector<Expr> exprs);
113
115
116} // namespace Internal
117} // namespace Halide
118
119#endif
Tables listing associative operators and their identities.
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
bool equal(const RDom &bounds0, const RDom &bounds1)
Return true if bounds0 and bounds1 represent the same bounds.
void associativity_test()
AssociativeOp prove_associativity(const std::string &f, std::vector< Expr > args, std::vector< Expr > exprs)
Given an update definition of a Func 'f', determine its equivalent associative binary/unary operator ...
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
std::string var
Variable name that is used to replace the expr in 'op'.
bool operator!=(const Replacement &other) const
bool operator==(const Replacement &other) const
Replacement(const std::string &var, Expr expr)
Represent the equivalent associative op of an update definition.
std::vector< Replacement > ys
std::vector< Replacement > xs
AssociativePattern pattern
List of pairs of binary associative op and its identity.
AssociativeOp(const AssociativePattern &p, const std::vector< Replacement > &xs, const std::vector< Replacement > &ys, bool is_associative)
Represent an associative op with its identity.
bool is_commutative
Indicate if the associative op is also commutative.