Halide  17.0.2
Halide compiler and libraries
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 "Buffer.h"
12 #include "IR.h"
13 #include "IRVisitor.h"
14 #include "Scope.h"
15 
16 namespace Halide {
17 
18 namespace Internal {
19 
20 /** A helper class to manage closures. Walks over a statement and
21  * retrieves all the references within it to external symbols
22  * (variables and allocations). It then helps you build a struct
23  * containing the current values of these symbols that you can use as
24  * a closure if you want to migrate the body of the statement to its
25  * own function (e.g. because it's the body of a parallel for loop. */
26 class Closure : public IRVisitor {
27 protected:
29 
30  using IRVisitor::visit;
31 
32  void visit(const Let *op) override;
33  void visit(const LetStmt *op) override;
34  void visit(const For *op) override;
35  void visit(const Load *op) override;
36  void visit(const Store *op) override;
37  void visit(const Allocate *op) override;
38  void visit(const Variable *op) override;
39  void visit(const Atomic *op) override;
40 
41 public:
42  /** Information about a buffer reference from a closure. */
43  struct Buffer {
44  /** The type of the buffer referenced. */
46 
47  /** The dimensionality of the buffer. */
49 
50  /** The buffer is read from. */
51  bool read = false;
52 
53  /** The buffer is written to. */
54  bool write = false;
55 
56  /** The buffer is a texture */
58 
59  /** The size of the buffer if known, otherwise zero. */
60  size_t size = 0;
61 
62  Buffer() = default;
63  };
64 
65 protected:
66  void found_buffer_ref(const std::string &name, Type type,
67  bool read, bool written, const Halide::Buffer<> &image);
68 
69 public:
70  Closure() = default;
71 
72  // Movable but not copyable.
73  Closure(const Closure &) = delete;
74  Closure &operator=(const Closure &) = delete;
75  Closure(Closure &&) = default;
76  Closure &operator=(Closure &&) = default;
77 
78  /** Traverse a statement and find all references to external
79  * symbols.
80  *
81  * When the closure encounters a read or write to 'foo', it
82  * assumes that the host pointer is found in the symbol table as
83  * 'foo.host', and any halide_buffer_t pointer is found under
84  * 'foo.buffer'.
85  *
86  * Calling this multiple times (on multiple statements) is legal
87  * (and will produce a unified closure).
88  **/
89  void include(const Stmt &s, const std::string &loop_variable = "");
90 
91  /** External variables referenced. There's code that assumes iterating over
92  * this repeatedly gives a consistent order, so don't swap out the data type
93  * for something non-deterministic. */
94  std::map<std::string, Type> vars;
95 
96  /** External allocations referenced. */
97  std::map<std::string, Buffer> buffers;
98 
99  /** Pack a closure into a struct. */
100  Expr pack_into_struct() const;
101 
102  /** Unpack a closure around a Stmt, putting all the names in scope. */
103  Stmt unpack_from_struct(const Expr &, const Stmt &) const;
104 };
105 
106 } // namespace Internal
107 } // namespace Halide
108 
109 #endif
void include(const Stmt &s, const std::string &loop_variable="")
Traverse a statement and find all references to external symbols.
void found_buffer_ref(const std::string &name, Type type, bool read, bool written, const Halide::Buffer<> &image)
A named variable.
Definition: IR.h:758
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:19
A fragment of Halide syntax.
Definition: Expr.h:258
bool write
The buffer is written to.
Definition: Closure.h:54
A reference-counted handle to a statement node.
Definition: Expr.h:419
Load a value from a named symbol if predicate is true.
Definition: IR.h:217
MemoryType memory_type
The buffer is a texture.
Definition: Closure.h:57
Lock all the Store nodes in the body statement.
Definition: IR.h:948
Closure & operator=(const Closure &)=delete
A helper class to manage closures.
Definition: Closure.h:26
This file defines the class FunctionDAG, which is our representation of a Halide pipeline, and contains methods to using Halide&#39;s bounds tools to query properties of it.
unsigned __INT8_TYPE__ uint8_t
Store a &#39;value&#39; to the buffer called &#39;name&#39; at a given &#39;index&#39; if &#39;predicate&#39; is true.
Definition: IR.h:333
std::map< std::string, Buffer > buffers
External allocations referenced.
Definition: Closure.h:97
Expr pack_into_struct() const
Pack a closure into a struct.
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:371
bool read
The buffer is read from.
Definition: Closure.h:51
A for loop.
Definition: IR.h:805
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Stmt unpack_from_struct(const Expr &, const Stmt &) const
Unpack a closure around a Stmt, putting all the names in scope.
std::map< std::string, Type > vars
External variables referenced.
Definition: Closure.h:94
size_t size
The size of the buffer if known, otherwise zero.
Definition: Closure.h:60
void visit(const Let *op) override
Let Halide select a storage type automatically.
Not visible externally, similar to &#39;static&#39; linkage in C.
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR...
A let expression, like you might find in a functional language.
Definition: IR.h:271
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt) ...
uint8_t dimensions
The dimensionality of the buffer.
Definition: Closure.h:48
Types in the halide type system.
Definition: Type.h:276
Information about a buffer reference from a closure.
Definition: Closure.h:43
Type type
The type of the buffer referenced.
Definition: Closure.h:45
The statement form of a let node.
Definition: IR.h:282
Defines the base class for things that recursively walk over the IR.
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:348
virtual void visit(const IntImm *)