Halide 14.0.0
Halide compiler and libraries
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 std::string var;
19
20 /** This lets you use a ReductionVariable as a key in a map of the form
21 * map<ReductionVariable, Foo, ReductionVariable::Compare> */
22 struct Compare {
23 bool operator()(const ReductionVariable &a, const ReductionVariable &b) const {
24 return a.var < b.var;
25 }
26 };
27};
28
29struct ReductionDomainContents;
30
31/** A reference-counted handle on a reduction domain, which is just a
32 * vector of ReductionVariable. */
35
36public:
37 /** This lets you use a ReductionDomain as a key in a map of the form
38 * map<ReductionDomain, Foo, ReductionDomain::Compare> */
39 struct Compare {
40 bool operator()(const ReductionDomain &a, const ReductionDomain &b) const {
41 internal_assert(a.contents.defined() && b.contents.defined());
42 return a.contents < b.contents;
43 }
44 };
45
46 /** Construct a new nullptr reduction domain */
48 : contents(nullptr) {
49 }
50
51 /** Construct a reduction domain that spans the outer product of
52 * all values of the given ReductionVariable in scanline order,
53 * with the start of the vector being innermost, and the end of
54 * the vector being outermost. */
55 ReductionDomain(const std::vector<ReductionVariable> &domain);
56
57 /** Return a deep copy of this ReductionDomain. */
59
60 /** Is this handle non-nullptr */
61 bool defined() const {
62 return contents.defined();
63 }
64
65 /** Tests for equality of reference. Only one reduction domain is
66 * allowed per reduction function, and this is used to verify
67 * that */
68 bool same_as(const ReductionDomain &other) const {
69 return contents.same_as(other.contents);
70 }
71
72 /** Immutable access to the reduction variables. */
73 const std::vector<ReductionVariable> &domain() const;
74
75 /** Add predicate to the reduction domain. See \ref RDom::where
76 * for more details. */
78
79 /** Return the predicate defined on this reducation demain. */
80 Expr predicate() const;
81
82 /** Set the predicate, replacing any previously set predicate. */
83 void set_predicate(const Expr &);
84
85 /** Split predicate into vector of ANDs. If there is no predicate (i.e. all
86 * iteration domain in this reduction domain is valid), this returns an
87 * empty vector. */
88 std::vector<Expr> split_predicate() const;
89
90 /** Mark RDom as frozen, which means it cannot accept new predicates. An
91 * RDom is frozen once it is used in a Func's update definition. */
92 void freeze();
93
94 /** Check if a RDom has been frozen. If so, it is an error to add new
95 * predicates. */
96 bool frozen() const;
97
98 /** Pass an IRVisitor through to all Exprs referenced in the
99 * ReductionDomain. */
100 void accept(IRVisitor *) const;
101
102 /** Pass an IRMutator through to all Exprs referenced in the
103 * ReductionDomain. */
105};
106
108
109} // namespace Internal
110} // namespace Halide
111
112#endif
#define internal_assert(c)
Definition: Errors.h:19
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
A reference-counted handle on a reduction domain, which is just a vector of ReductionVariable.
Definition: Reduction.h:33
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:61
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.
Expr predicate() const
Return the predicate defined on this reducation demain.
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:47
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:68
void split_predicate_test()
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:256
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:161
HALIDE_ALWAYS_INLINE bool same_as(const IntrusivePtr &other) const
Definition: IntrusivePtr.h:168
This lets you use a ReductionDomain as a key in a map of the form map<ReductionDomain,...
Definition: Reduction.h:39
bool operator()(const ReductionDomain &a, const ReductionDomain &b) const
Definition: Reduction.h:40
This lets you use a ReductionVariable as a key in a map of the form map<ReductionVariable,...
Definition: Reduction.h:22
bool operator()(const ReductionVariable &a, const ReductionVariable &b) const
Definition: Reduction.h:23
A single named dimension of a reduction domain.
Definition: Reduction.h:16