Halide 22.0.0 Halide compiler and libraries

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 */
61 struct AssociativeOp {
62 struct Replacement {
63 /** Variable name that is used to replace the expr in 'op'. */
64 std::string var;
65 Expr expr;
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. */
81 AssociativePattern pattern;
82 std::vector<Replacement> xs;
83 std::vector<Replacement> ys;
84 bool is_associative = false;
85
86 AssociativeOp() = default;
87 AssociativeOp(size_t size)
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)
92 : pattern(p), xs(xs), ys(ys), is_associative(is_associative) {
93 }
94
95 bool associative() const {
96 return is_associative;
97 }
98 bool commutative() const {
99 return pattern.is_commutative;
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 */
111 AssociativeOp prove_associativity(
112 const std::string &f, std::vector<Expr> args, std::vector<Expr> exprs);
113
114 void associativity_test();
115
116} // namespace Internal
117} // namespace Halide
118
119#endif
AssociativeOpsTable.h
Tables listing associative operators and their identities.
Expr.h
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Halide::Internal::equal
bool equal(const RDom &bounds0, const RDom &bounds1)
Return true if bounds0 and bounds1 represent the same bounds.
Halide::Internal::associativity_test
void associativity_test()
Halide::Internal::prove_associativity
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 ...
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::AssociativeOp::Replacement
Definition: Associativity.h:62
Halide::Internal::AssociativeOp::Replacement::var
std::string var
Variable name that is used to replace the expr in 'op'.
Definition: Associativity.h:64
Halide::Internal::AssociativeOp::Replacement::operator!=
bool operator!=(const Replacement &other) const
Definition: Associativity.h:75
Halide::Internal::AssociativeOp::Replacement::Replacement
Replacement()=default
Halide::Internal::AssociativeOp::Replacement::operator==
bool operator==(const Replacement &other) const
Definition: Associativity.h:72
Halide::Internal::AssociativeOp::Replacement::Replacement
Replacement(const std::string &var, Expr expr)
Definition: Associativity.h:68
Halide::Internal::AssociativeOp::Replacement::expr
Expr expr
Definition: Associativity.h:65
Halide::Internal::AssociativeOp
Represent the equivalent associative op of an update definition.
Definition: Associativity.h:61
Halide::Internal::AssociativeOp::ys
std::vector< Replacement > ys
Definition: Associativity.h:83
Halide::Internal::AssociativeOp::xs
std::vector< Replacement > xs
Definition: Associativity.h:82
Halide::Internal::AssociativeOp::associative
bool associative() const
Definition: Associativity.h:95
Halide::Internal::AssociativeOp::AssociativeOp
AssociativeOp()=default
Halide::Internal::AssociativeOp::size
size_t size() const
Definition: Associativity.h:101
Halide::Internal::AssociativeOp::commutative
bool commutative() const
Definition: Associativity.h:98
Halide::Internal::AssociativeOp::AssociativeOp
AssociativeOp(size_t size)
Definition: Associativity.h:87
Halide::Internal::AssociativeOp::pattern
AssociativePattern pattern
List of pairs of binary associative op and its identity.
Definition: Associativity.h:81
Halide::Internal::AssociativeOp::AssociativeOp
AssociativeOp(const AssociativePattern &p, const std::vector< Replacement > &xs, const std::vector< Replacement > &ys, bool is_associative)
Definition: Associativity.h:90
Halide::Internal::AssociativeOp::is_associative
bool is_associative
Definition: Associativity.h:84
Halide::Internal::AssociativePattern
Represent an associative op with its identity.
Definition: AssociativeOpsTable.h:31
Halide::Internal::AssociativePattern::is_commutative
bool is_commutative
Indicate if the associative op is also commutative.
Definition: AssociativeOpsTable.h:37
Halide::Internal::AssociativePattern::size
size_t size() const
Definition: AssociativeOpsTable.h:64