Loading...
Searching...
No Matches
Closure.h
Go to the documentation of this file.
#ifndef HALIDE_CLOSURE_H
#define HALIDE_CLOSURE_H
/** \file
*
* Provides Closure class.
*/
#include <map>
#include <string>
#include "Buffer.h"
#include "IR.h"
#include "IRVisitor.h"
#include "Scope.h"
namespace Halide {
namespace Internal {
/** A helper class to manage closures. Walks over a statement and
* retrieves all the references within it to external symbols
* (variables and allocations). It then helps you build a struct
* containing the current values of these symbols that you can use as
* a closure if you want to migrate the body of the statement to its
* own function (e.g. because it's the body of a parallel for loop. */
class Closure : public IRVisitor {
protected:
Scope<> ignore;
using IRVisitor::visit;
void visit(const Let *op) override;
void visit(const LetStmt *op) override;
void visit(const For *op) override;
void visit(const Load *op) override;
void visit(const Store *op) override;
void visit(const Allocate *op) override;
void visit(const Variable *op) override;
void visit(const Atomic *op) override;
public:
/** Information about a buffer reference from a closure. */
struct Buffer {
/** The type of the buffer referenced. */
Type type;
/** The dimensionality of the buffer. */
uint8_t dimensions = 0;
/** The buffer is read from. */
bool read = false;
/** The buffer is written to. */
bool write = false;
/** The buffer is a texture */
MemoryType memory_type = MemoryType::Auto;
/** The size of the buffer if known, otherwise zero. */
size_t size = 0;
Buffer() = default;
};
protected:
void found_buffer_ref(const std::string &name, Type type,
bool read, bool written, const Halide::Buffer<> &image);
public:
Closure() = default;
// Movable but not copyable.
Closure(const Closure &) = delete;
Closure &operator=(const Closure &) = delete;
Closure(Closure &&) = default;
Closure &operator=(Closure &&) = default;
/** Traverse a statement and find all references to external
* symbols.
*
* When the closure encounters a read or write to 'foo', it
* assumes that the host pointer is found in the symbol table as
* 'foo.host', and any halide_buffer_t pointer is found under
* 'foo.buffer'.
*
* Calling this multiple times (on multiple statements) is legal
* (and will produce a unified closure).
**/
void include(const Stmt &s, const std::string &loop_variable = "");
/** External variables referenced. There's code that assumes iterating over
* this repeatedly gives a consistent order, so don't swap out the data type
* for something non-deterministic. */
std::map<std::string, Type> vars;
/** External allocations referenced. */
std::map<std::string, Buffer> buffers;
/** Pack a closure into a struct. */
Expr pack_into_struct() const;
/** Unpack a closure around a Stmt, putting all the names in scope. */
Stmt unpack_from_struct(const Expr &, const Stmt &) const;
};
} // namespace Internal
} // namespace Halide
#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::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::visit
virtual void visit(const IntImm *)
Halide::Internal::IRVisitor::IRVisitor
IRVisitor()=default
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::Internal
Definition AbstractGenerator.h:24
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
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.
Definition Expr.h:355
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:371
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition IR.h:994
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:848
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition IR.h:271
Halide::Internal::LetStmt
The statement form of a let node.
Definition IR.h:282
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition IR.h:217
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:333
Halide::Internal::Variable
A named variable.
Definition IR.h:801
Halide::Type
Types in the halide type system.
Definition Type.h:283