Halide 21.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
Reduction.h
Go to the documentation of this file.
1#ifndef HALIDE_REDUCTION_H
2#define HALIDE_REDUCTION_H
3
4/** \file
5 * Defines internal classes related to Reduction Domains
6 */
7
8#include "Expr.h"
9
10namespace Halide {
11namespace Internal {
12
13class IRMutator;
14
15/** A single named dimension of a reduction domain */
17 /**
18 * A variable name for the reduction variable. This name must be a
19 * valid Var name, i.e. it must not contain a <tt>.</tt> character.
20 */
21 std::string var;
22
24
25 /** This lets you use a ReductionVariable as a key in a map of the form
26 * map<ReductionVariable, Foo, ReductionVariable::Compare> */
27 struct Compare {
28 bool operator()(const ReductionVariable &a, const ReductionVariable &b) const {
29 return a.var < b.var;
30 }
31 };
32};
33
34struct ReductionDomainContents;
35
36/** A reference-counted handle on a reduction domain, which is just a
37 * vector of ReductionVariable. */
40
41public:
42 /** This lets you use a ReductionDomain as a key in a map of the form
43 * map<ReductionDomain, Foo, ReductionDomain::Compare> */
44 struct Compare {
45 bool operator()(const ReductionDomain &a, const ReductionDomain &b) const {
46 internal_assert(a.contents.defined() && b.contents.defined());
47 return a.contents < b.contents;
48 }
49 };
50
51 /** Construct a new nullptr reduction domain */
53 : contents(nullptr) {
54 }
55
56 /** Construct a reduction domain that spans the outer product of
57 * all values of the given ReductionVariable in scanline order,
58 * with the start of the vector being innermost, and the end of
59 * the vector being outermost. */
60 ReductionDomain(const std::vector<ReductionVariable> &domain);
61
62 /** Construct a reduction domain from deserialization */
63 ReductionDomain(const std::vector<ReductionVariable> &domain, const Expr &predicate, bool frozen);
64
65 /** Return a deep copy of this ReductionDomain. */
67
68 /** Is this handle non-nullptr */
69 bool defined() const {
70 return contents.defined();
71 }
72
73 /** Tests for equality of reference. Only one reduction domain is
74 * allowed per reduction function, and this is used to verify
75 * that */
76 bool same_as(const ReductionDomain &other) const {
77 return contents.same_as(other.contents);
78 }
79
80 /** Immutable access to the reduction variables. */
81 const std::vector<ReductionVariable> &domain() const;
82
83 /** Add predicate to the reduction domain. See \ref RDom::where
84 * for more details. */
86
87 /** Return the predicate defined on this reduction domain. */
88 Expr predicate() const;
89
90 /** Set the predicate, replacing any previously set predicate. */
91 void set_predicate(const Expr &);
92
93 /** Split predicate into vector of ANDs. If there is no predicate (i.e. all
94 * iteration domain in this reduction domain is valid), this returns an
95 * empty vector. */
96 std::vector<Expr> split_predicate() const;
97
98 /** Mark RDom as frozen, which means it cannot accept new predicates. An
99 * RDom is frozen once it is used in a Func's update definition. */
100 void freeze();
101
102 /** Check if a RDom has been frozen. If so, it is an error to add new
103 * predicates. */
104 bool frozen() const;
105
106 /** Pass an IRVisitor through to all Exprs referenced in the
107 * ReductionDomain. */
108 void accept(IRVisitor *) const;
109
110 /** Pass an IRMutator through to all Exprs referenced in the
111 * ReductionDomain. */
113};
114
116
117} // namespace Internal
118} // namespace Halide
119
120#endif
#define internal_assert(c)
Definition Error.h:218
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
A base class for passes over the IR which modify it (e.g.
Definition IRMutator.h:26
A base class for algorithms that need to recursively walk over the IR.
Definition IRVisitor.h:19
void freeze()
Mark RDom as frozen, which means it cannot accept new predicates.
std::vector< Expr > split_predicate() const
Split predicate into vector of ANDs.
bool defined() const
Is this handle non-nullptr.
Definition Reduction.h:69
void set_predicate(const Expr &)
Set the predicate, replacing any previously set predicate.
ReductionDomain deep_copy() const
Return a deep copy of this ReductionDomain.
const std::vector< ReductionVariable > & domain() const
Immutable access to the reduction variables.
ReductionDomain(const std::vector< ReductionVariable > &domain, const Expr &predicate, bool frozen)
Construct a reduction domain from deserialization.
Expr predicate() const
Return the predicate defined on this reduction domain.
void accept(IRVisitor *) const
Pass an IRVisitor through to all Exprs referenced in the ReductionDomain.
ReductionDomain()
Construct a new nullptr reduction domain.
Definition Reduction.h:52
ReductionDomain(const std::vector< ReductionVariable > &domain)
Construct a reduction domain that spans the outer product of all values of the given ReductionVariabl...
bool frozen() const
Check if a RDom has been frozen.
void where(Expr predicate)
Add predicate to the reduction domain.
void mutate(IRMutator *)
Pass an IRMutator through to all Exprs referenced in the ReductionDomain.
bool same_as(const ReductionDomain &other) const
Tests for equality of reference.
Definition Reduction.h:76
void split_predicate_test()
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
A fragment of Halide syntax.
Definition Expr.h:258
Intrusive shared pointers have a reference count (a RefCount object) stored in the class itself.
HALIDE_ALWAYS_INLINE bool defined() const
This lets you use a ReductionDomain as a key in a map of the form map<ReductionDomain,...
Definition Reduction.h:44
bool operator()(const ReductionDomain &a, const ReductionDomain &b) const
Definition Reduction.h:45
This lets you use a ReductionVariable as a key in a map of the form map<ReductionVariable,...
Definition Reduction.h:27
bool operator()(const ReductionVariable &a, const ReductionVariable &b) const
Definition Reduction.h:28
A single named dimension of a reduction domain.
Definition Reduction.h:16
std::string var
A variable name for the reduction variable.
Definition Reduction.h:21