Halide 22.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
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:
28 Scope<> ignore;
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. */
45 Type type;
46
47 /** The dimensionality of the buffer. */
48 uint8_t dimensions = 0;
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 */
57 MemoryType memory_type = MemoryType::Auto;
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
Buffer.h
IR.h
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
IRVisitor.h
Defines the base class for things that recursively walk over the IR.
Scope.h
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
Halide::Buffer<>
Halide::Internal::Closure
A helper class to manage closures.
Definition Closure.h:26
Halide::Internal::Closure::Closure
Closure(Closure &&)=default
Halide::Internal::Closure::vars
std::map< std::string, Type > vars
External variables referenced.
Definition Closure.h:94
Halide::Internal::Closure::found_buffer_ref
void found_buffer_ref(const std::string &name, Type type, bool read, bool written, const Halide::Buffer<> &image)
Halide::Internal::Closure::visit
void visit(const LetStmt *op) override
Halide::Internal::Closure::Closure
Closure(const Closure &)=delete
Halide::Internal::Closure::unpack_from_struct
Stmt unpack_from_struct(const Expr &, const Stmt &) const
Unpack a closure around a Stmt, putting all the names in scope.
Halide::Internal::Closure::buffers
std::map< std::string, Buffer > buffers
External allocations referenced.
Definition Closure.h:97
Halide::Internal::Closure::visit
void visit(const Let *op) override
Halide::Internal::Closure::visit
void visit(const For *op) override
Halide::Internal::Closure::operator=
Closure & operator=(const Closure &)=delete
Halide::Internal::Closure::visit
void visit(const Load *op) override
Halide::Internal::Closure::pack_into_struct
Expr pack_into_struct() const
Pack a closure into a struct.
Halide::Internal::Closure::include
void include(const Stmt &s, const std::string &loop_variable="")
Traverse a statement and find all references to external symbols.
Halide::Internal::Closure::visit
void visit(const Store *op) override
Halide::Internal::Closure::visit
void visit(const Atomic *op) override
Halide::Internal::Closure::ignore
Scope ignore
Definition Closure.h:28
Halide::Internal::Closure::Closure
Closure()=default
Halide::Internal::Closure::visit
void visit(const Allocate *op) override
Halide::Internal::Closure::visit
void visit(const Variable *op) override
Halide::Internal::Closure::operator=
Closure & operator=(Closure &&)=default
Halide::Internal::IRVisitor
A base class for algorithms that need to recursively walk over the IR.
Definition IRVisitor.h:19
Halide::Internal::IRVisitor::visit
virtual void visit(const IntImm *)
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 Scope.h:94
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::cast
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
Halide::MemoryType
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition Expr.h:353
Halide::MemoryType::Auto
@ Auto
Let Halide select a storage type automatically.
uint8_t
unsigned __INT8_TYPE__ uint8_t
Definition runtime_internal.h:29
Halide::Expr
A fragment of Halide syntax.
Definition Expr.h:258
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:381
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition IR.h:1004
Halide::Internal::Closure::Buffer
Information about a buffer reference from a closure.
Definition Closure.h:43
Halide::Internal::Closure::Buffer::write
bool write
The buffer is written to.
Definition Closure.h:54
Halide::Internal::Closure::Buffer::size
size_t size
The size of the buffer if known, otherwise zero.
Definition Closure.h:60
Halide::Internal::Closure::Buffer::Buffer
Buffer()=default
Halide::Internal::Closure::Buffer::dimensions
uint8_t dimensions
The dimensionality of the buffer.
Definition Closure.h:48
Halide::Internal::Closure::Buffer::read
bool read
The buffer is read from.
Definition Closure.h:51
Halide::Internal::Closure::Buffer::type
Type type
The type of the buffer referenced.
Definition Closure.h:45
Halide::Internal::Closure::Buffer::memory_type
MemoryType memory_type
The buffer is a texture.
Definition Closure.h:57
Halide::Internal::For
A for loop.
Definition IR.h:858
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition IR.h:281
Halide::Internal::LetStmt
The statement form of a let node.
Definition IR.h:292
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition IR.h:227
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:343
Halide::Internal::Variable
A named variable.
Definition IR.h:811
Halide::Type
Types in the halide type system.
Definition Type.h:281