Halide
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 */
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 
29 struct ReductionDomainContents;
30 
31 /** A reference-counted handle on a reduction domain, which is just a
32  * vector of ReductionVariable. */
35 
36 public:
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. */
58  ReductionDomain deep_copy() const;
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. */
77  void where(Expr predicate);
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. */
104  void mutate(IRMutator *);
105 };
106 
107 void split_predicate_test();
108 
109 } // namespace Internal
110 } // namespace Halide
111 
112 #endif
Halide::Internal::ReductionVariable::min
Expr min
Definition: Reduction.h:18
Halide::Internal::ReductionVariable::Compare::operator()
bool operator()(const ReductionVariable &a, const ReductionVariable &b) const
Definition: Reduction.h:23
Halide::Internal::ReductionDomain::ReductionDomain
ReductionDomain()
Construct a new nullptr reduction domain.
Definition: Reduction.h:47
internal_assert
#define internal_assert(c)
Definition: Errors.h:19
Halide::Internal::IRVisitor
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:19
Halide::Internal::ReductionDomain::mutate
void mutate(IRMutator *)
Pass an IRMutator through to all Exprs referenced in the ReductionDomain.
Halide::Internal::ReductionDomain::accept
void accept(IRVisitor *) const
Pass an IRVisitor through to all Exprs referenced in the ReductionDomain.
Halide::Internal::ReductionDomain::domain
const std::vector< ReductionVariable > & domain() const
Immutable access to the reduction variables.
Halide::Internal::IntrusivePtr< ReductionDomainContents >
Halide::Internal::ReductionDomain::Compare
This lets you use a ReductionDomain as a key in a map of the form map<ReductionDomain,...
Definition: Reduction.h:39
Halide::Internal::ReductionDomain::where
void where(Expr predicate)
Add predicate to the reduction domain.
Halide::Internal::ReductionDomain::freeze
void freeze()
Mark RDom as frozen, which means it cannot accept new predicates.
Halide::Internal::ReductionDomain::predicate
Expr predicate() const
Return the predicate defined on this reducation demain.
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::Internal::ReductionDomain::set_predicate
void set_predicate(const Expr &)
Set the predicate, replacing any previously set predicate.
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::ReductionVariable::var
std::string var
Definition: Reduction.h:17
Halide::Internal::ReductionVariable
A single named dimension of a reduction domain.
Definition: Reduction.h:16
Halide::Internal::ReductionDomain::defined
bool defined() const
Is this handle non-nullptr.
Definition: Reduction.h:61
Halide::Internal::ReductionVariable::extent
Expr extent
Definition: Reduction.h:18
Halide::Internal::ReductionDomain::deep_copy
ReductionDomain deep_copy() const
Return a deep copy of this ReductionDomain.
Expr.h
Halide::Internal::IRMutator
A base class for passes over the IR which modify it (e.g.
Definition: IRMutator.h:26
Halide::Internal::ReductionDomain::frozen
bool frozen() const
Check if a RDom has been frozen.
Halide::Internal::ReductionDomain::same_as
bool same_as(const ReductionDomain &other) const
Tests for equality of reference.
Definition: Reduction.h:68
Halide::Internal::IntrusivePtr::defined
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:161
Halide::Internal::ReductionDomain::split_predicate
std::vector< Expr > split_predicate() const
Split predicate into vector of ANDs.
Halide::Internal::ReductionDomain::Compare::operator()
bool operator()(const ReductionDomain &a, const ReductionDomain &b) const
Definition: Reduction.h:40
Halide::Internal::ReductionVariable::Compare
This lets you use a ReductionVariable as a key in a map of the form map<ReductionVariable,...
Definition: Reduction.h:22
Halide::Internal::split_predicate_test
void split_predicate_test()
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:257
Halide::Internal::ReductionDomain
A reference-counted handle on a reduction domain, which is just a vector of ReductionVariable.
Definition: Reduction.h:33
Halide::Internal::IntrusivePtr::same_as
HALIDE_ALWAYS_INLINE bool same_as(const IntrusivePtr &other) const
Definition: IntrusivePtr.h:168