Halide  12.0.1
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 "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 = false;
54 
55  /** The buffer is written to. */
56  bool write = false;
57 
58  /** The buffer is a texture */
60 
61  /** The size of the buffer if known, otherwise zero. */
62  size_t size = 0;
63 
64  Buffer() = default;
65  };
66 
67 protected:
68  void found_buffer_ref(const std::string &name, Type type,
69  bool read, bool written, const Halide::Buffer<void> &image);
70 
71 public:
72  Closure() = default;
73 
74  /** Traverse a statement and find all references to external
75  * symbols.
76  *
77  * When the closure encounters a read or write to 'foo', it
78  * assumes that the host pointer is found in the symbol table as
79  * 'foo.host', and any halide_buffer_t pointer is found under
80  * 'foo.buffer'. */
81  Closure(const Stmt &s, const std::string &loop_variable = "");
82 
83  /** External variables referenced. */
84  std::map<std::string, Type> vars;
85 
86  /** External allocations referenced. */
87  std::map<std::string, Buffer> buffers;
88 };
89 
90 } // namespace Internal
91 } // namespace Halide
92 
93 #endif
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Defines the base class for things that recursively walk over the IR.
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
A helper class to manage closures.
Definition: Closure.h:28
std::map< std::string, Type > vars
External variables referenced.
Definition: Closure.h:84
void visit(const LetStmt *op) override
std::map< std::string, Buffer > buffers
External allocations referenced.
Definition: Closure.h:87
void visit(const Let *op) override
void visit(const For *op) override
void visit(const Load *op) override
void found_buffer_ref(const std::string &name, Type type, bool read, bool written, const Halide::Buffer< void > &image)
void visit(const Store *op) override
void visit(const Atomic *op) override
void visit(const Allocate *op) override
void visit(const Variable *op) override
Closure(const Stmt &s, const std::string &loop_variable="")
Traverse a statement and find all references to external symbols.
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:19
virtual void visit(const IntImm *)
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition: Scope.h:94
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:346
@ Auto
Let Halide select a storage type automatically.
unsigned __INT8_TYPE__ uint8_t
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Lock all the Store nodes in the body statement.
Definition: IR.h:853
Information about a buffer reference from a closure.
Definition: Closure.h:45
bool write
The buffer is written to.
Definition: Closure.h:56
size_t size
The size of the buffer if known, otherwise zero.
Definition: Closure.h:62
uint8_t dimensions
The dimensionality of the buffer.
Definition: Closure.h:50
bool read
The buffer is read from.
Definition: Closure.h:53
Type type
The type of the buffer referenced.
Definition: Closure.h:47
MemoryType memory_type
The buffer is a texture.
Definition: Closure.h:59
A for loop.
Definition: IR.h:730
A let expression, like you might find in a functional language.
Definition: IR.h:253
The statement form of a let node.
Definition: IR.h:264
Load a value from a named symbol if predicate is true.
Definition: IR.h:199
A reference-counted handle to a statement node.
Definition: Expr.h:413
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:315
A named variable.
Definition: IR.h:683
Types in the halide type system.
Definition: Type.h:269