Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
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
16namespace Halide {
17
18namespace 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. */
26class Closure : public IRVisitor {
27protected:
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
41public:
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
65protected:
66 void found_buffer_ref(const std::string &name, Type type,
67 bool read, bool written, const Halide::Buffer<> &image);
68
69public:
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. */
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
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:26
Closure(Closure &&)=default
std::map< std::string, Type > vars
External variables referenced.
Definition Closure.h:94
void found_buffer_ref(const std::string &name, Type type, bool read, bool written, const Halide::Buffer<> &image)
void visit(const LetStmt *op) override
Closure(const Closure &)=delete
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, Buffer > buffers
External allocations referenced.
Definition Closure.h:97
void visit(const Let *op) override
void visit(const For *op) override
Closure & operator=(const Closure &)=delete
void visit(const Load *op) override
Expr pack_into_struct() const
Pack a closure into a struct.
void include(const Stmt &s, const std::string &loop_variable="")
Traverse a statement and find all references to external symbols.
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 & operator=(Closure &&)=default
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:353
@ Auto
Let Halide select a storage type automatically.
unsigned __INT8_TYPE__ uint8_t
A fragment of Halide syntax.
Definition Expr.h:258
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:371
Lock all the Store nodes in the body statement.
Definition IR.h:961
Information about a buffer reference from a closure.
Definition Closure.h:43
bool write
The buffer is written to.
Definition Closure.h:54
size_t size
The size of the buffer if known, otherwise zero.
Definition Closure.h:60
uint8_t dimensions
The dimensionality of the buffer.
Definition Closure.h:48
bool read
The buffer is read from.
Definition Closure.h:51
Type type
The type of the buffer referenced.
Definition Closure.h:45
MemoryType memory_type
The buffer is a texture.
Definition Closure.h:57
A for loop.
Definition IR.h:819
A let expression, like you might find in a functional language.
Definition IR.h:271
The statement form of a let node.
Definition IR.h:282
Load a value from a named symbol if predicate is true.
Definition IR.h:217
A reference-counted handle to a statement node.
Definition Expr.h:427
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:333
A named variable.
Definition IR.h:772
Types in the halide type system.
Definition Type.h:283