Halide
Closure.h
Go to the documentation of this file.
1 #ifndef HALIDE_CLOSURE_H
2 #define HALIDE_CLOSURE_H
3 
4 /** \file
5  *
6  * Provides Closure class.
7  */
8 #include <map>
9 #include <string>
10 
11 #include "IR.h"
12 #include "IRVisitor.h"
13 #include "Scope.h"
14 
15 namespace Halide {
16 
17 template<typename T>
18 class Buffer;
19 
20 namespace Internal {
21 
22 /** A helper class to manage closures. Walks over a statement and
23  * retrieves all the references within it to external symbols
24  * (variables and allocations). It then helps you build a struct
25  * containing the current values of these symbols that you can use as
26  * a closure if you want to migrate the body of the statement to its
27  * own function (e.g. because it's the body of a parallel for loop. */
28 class Closure : public IRVisitor {
29 protected:
31 
32  using IRVisitor::visit;
33 
34  void visit(const Let *op) override;
35  void visit(const LetStmt *op) override;
36  void visit(const For *op) override;
37  void visit(const Load *op) override;
38  void visit(const Store *op) override;
39  void visit(const Allocate *op) override;
40  void visit(const Variable *op) override;
41  void visit(const Atomic *op) override;
42 
43 public:
44  /** Information about a buffer reference from a closure. */
45  struct Buffer {
46  /** The type of the buffer referenced. */
48 
49  /** The dimensionality of the buffer. */
51 
52  /** The buffer is read from. */
53  bool read;
54 
55  /** The buffer is written to. */
56  bool write;
57 
58  /** The size of the buffer if known, otherwise zero. */
59  size_t size;
60 
62  : dimensions(0), read(false), write(false), size(0) {
63  }
64  };
65 
66 protected:
67  void found_buffer_ref(const std::string &name, Type type,
68  bool read, bool written, const Halide::Buffer<void> &image);
69 
70 public:
71  Closure() = default;
72 
73  /** Traverse a statement and find all references to external
74  * symbols.
75  *
76  * When the closure encounters a read or write to 'foo', it
77  * assumes that the host pointer is found in the symbol table as
78  * 'foo.host', and any halide_buffer_t pointer is found under
79  * 'foo.buffer'. */
80  Closure(const Stmt &s, const std::string &loop_variable = "");
81 
82  /** External variables referenced. */
83  std::map<std::string, Type> vars;
84 
85  /** External allocations referenced. */
86  std::map<std::string, Buffer> buffers;
87 };
88 
89 } // namespace Internal
90 } // namespace Halide
91 
92 #endif
Halide::Internal::Closure::ignore
Scope ignore
Definition: Closure.h:30
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Scope.h
Halide::Internal::IOKind::Buffer
@ Buffer
Halide::Internal::IRVisitor::visit
virtual void visit(const IntImm *)
uint8_t
unsigned __INT8_TYPE__ uint8_t
Definition: runtime_internal.h:25
Halide::Internal::IRVisitor
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:21
Halide::Internal::For
A for loop.
Definition: IR.h:698
Halide::Internal::LetStmt
The statement form of a let node.
Definition: IR.h:264
Halide::Internal::Scope
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition: ModulusRemainder.h:17
IR.h
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:409
Halide::Internal::Closure::Buffer::write
bool write
The buffer is written to.
Definition: Closure.h:56
Halide::Type
Types in the halide type system.
Definition: Type.h:269
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition: IR.h:199
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::Internal::Closure::buffers
std::map< std::string, Buffer > buffers
External allocations referenced.
Definition: Closure.h:86
Halide::Internal::Closure::visit
void visit(const Let *op) override
Halide::Internal::Closure::Buffer::type
Type type
The type of the buffer referenced.
Definition: Closure.h:47
Halide::Internal::Closure::Buffer
Information about a buffer reference from a closure.
Definition: Closure.h:45
Halide::Internal::Closure::vars
std::map< std::string, Type > vars
External variables referenced.
Definition: Closure.h:83
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::Closure::Closure
Closure()=default
Halide::Buffer< void >
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition: IR.h:253
IRVisitor.h
Halide::Internal::Closure::Buffer::size
size_t size
The size of the buffer if known, otherwise zero.
Definition: Closure.h:59
Halide::Internal::Closure::Buffer::dimensions
uint8_t dimensions
The dimensionality of the buffer.
Definition: Closure.h:50
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:315
Halide::Internal::Variable
A named variable.
Definition: IR.h:651
Halide::Internal::Closure
A helper class to manage closures.
Definition: Closure.h:28
Halide::Internal::Closure::Buffer::Buffer
Buffer()
Definition: Closure.h:61
Halide::Internal::Closure::found_buffer_ref
void found_buffer_ref(const std::string &name, Type type, bool read, bool written, const Halide::Buffer< void > &image)
Halide::Internal::Closure::Buffer::read
bool read
The buffer is read from.
Definition: Closure.h:53
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition: IR.h:809