Halide 22.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
10 namespace Halide {
11 namespace Internal {
12
13 class IRMutator;
14
15/** A single named dimension of a reduction domain */
16 struct ReductionVariable {
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
23 Expr min, extent;
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
34 struct ReductionDomainContents;
35
36/** A reference-counted handle on a reduction domain, which is just a
37 * vector of ReductionVariable. */
38 class ReductionDomain {
39 IntrusivePtr<ReductionDomainContents> contents;
40
41 public:
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 */
52 ReductionDomain()
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. */
66 ReductionDomain deep_copy() const;
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. */
85 void where(Expr predicate);
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. */
112 void mutate(IRMutator *);
113};
114
115 void split_predicate_test();
116
117} // namespace Internal
118} // namespace Halide
119
120#endif
internal_assert
#define internal_assert(c)
Definition: Error.h:232
Expr.h
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Halide::Internal::IRMutator
A base class for passes over the IR which modify it (e.g.
Definition: IRMutator.h:28
Halide::Internal::IRVisitor
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:19
Halide::Internal::ReductionDomain
A reference-counted handle on a reduction domain, which is just a vector of ReductionVariable.
Definition: Reduction.h:38
Halide::Internal::ReductionDomain::freeze
void freeze()
Mark RDom as frozen, which means it cannot accept new predicates.
Halide::Internal::ReductionDomain::split_predicate
std::vector< Expr > split_predicate() const
Split predicate into vector of ANDs.
Halide::Internal::ReductionDomain::defined
bool defined() const
Is this handle non-nullptr.
Definition: Reduction.h:69
Halide::Internal::ReductionDomain::set_predicate
void set_predicate(const Expr &)
Set the predicate, replacing any previously set predicate.
Halide::Internal::ReductionDomain::deep_copy
ReductionDomain deep_copy() const
Return a deep copy of this ReductionDomain.
Halide::Internal::ReductionDomain::domain
const std::vector< ReductionVariable > & domain() const
Immutable access to the reduction variables.
Halide::Internal::ReductionDomain::ReductionDomain
ReductionDomain(const std::vector< ReductionVariable > &domain, const Expr &predicate, bool frozen)
Construct a reduction domain from deserialization.
Halide::Internal::ReductionDomain::predicate
Expr predicate() const
Return the predicate defined on this reduction domain.
Halide::Internal::ReductionDomain::accept
void accept(IRVisitor *) const
Pass an IRVisitor through to all Exprs referenced in the ReductionDomain.
Halide::Internal::ReductionDomain::ReductionDomain
ReductionDomain()
Construct a new nullptr reduction domain.
Definition: Reduction.h:52
Halide::Internal::ReductionDomain::ReductionDomain
ReductionDomain(const std::vector< ReductionVariable > &domain)
Construct a reduction domain that spans the outer product of all values of the given ReductionVariabl...
Halide::Internal::ReductionDomain::frozen
bool frozen() const
Check if a RDom has been frozen.
Halide::Internal::ReductionDomain::where
void where(Expr predicate)
Add predicate to the reduction domain.
Halide::Internal::ReductionDomain::mutate
void mutate(IRMutator *)
Pass an IRMutator through to all Exprs referenced in the ReductionDomain.
Halide::Internal::ReductionDomain::same_as
bool same_as(const ReductionDomain &other) const
Tests for equality of reference.
Definition: Reduction.h:76
Halide::Internal::split_predicate_test
void split_predicate_test()
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::IntrusivePtr< ReductionDomainContents >
Halide::Internal::IntrusivePtr::defined
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:164
Halide::Internal::IntrusivePtr::same_as
HALIDE_ALWAYS_INLINE bool same_as(const IntrusivePtr &other) const
Definition: IntrusivePtr.h:171
Halide::Internal::ReductionDomain::Compare
This lets you use a ReductionDomain as a key in a map of the form map<ReductionDomain,...
Definition: Reduction.h:44
Halide::Internal::ReductionDomain::Compare::operator()
bool operator()(const ReductionDomain &a, const ReductionDomain &b) const
Definition: Reduction.h:45
Halide::Internal::ReductionVariable::Compare
This lets you use a ReductionVariable as a key in a map of the form map<ReductionVariable,...
Definition: Reduction.h:27
Halide::Internal::ReductionVariable::Compare::operator()
bool operator()(const ReductionVariable &a, const ReductionVariable &b) const
Definition: Reduction.h:28
Halide::Internal::ReductionVariable
A single named dimension of a reduction domain.
Definition: Reduction.h:16
Halide::Internal::ReductionVariable::min
Expr min
Definition: Reduction.h:23
Halide::Internal::ReductionVariable::var
std::string var
A variable name for the reduction variable.
Definition: Reduction.h:21
Halide::Internal::ReductionVariable::extent
Expr extent
Definition: Reduction.h:23