Loading...
Searching...
No Matches
CodeGen_Posix.h
Go to the documentation of this file.
#ifndef HALIDE_CODEGEN_POSIX_H
#define HALIDE_CODEGEN_POSIX_H
/** \file
* Defines a base-class for code-generators on posixy cpu platforms
*/
#include "CodeGen_LLVM.h"
namespace Halide {
namespace Internal {
/** A code generator that emits posix code from a given Halide stmt. */
class CodeGen_Posix : public CodeGen_LLVM {
public:
/** Create an posix code generator. Processor features can be
* enabled using the appropriate arguments */
CodeGen_Posix(const Target &t);
protected:
using CodeGen_LLVM::visit;
/** Posix implementation of Allocate. Small constant-sized allocations go
* on the stack. The rest go on the heap by calling "halide_malloc"
* and "halide_free" in the standard library. */
// @{
void visit(const Allocate *) override;
void visit(const Free *) override;
// @}
/** A struct describing heap or stack allocations. */
struct Allocation {
/** The memory */
llvm::Value *ptr = nullptr;
/** Destructor stack slot for this allocation. */
llvm::Value *destructor = nullptr;
/** Function to accomplish the destruction. */
llvm::Function *destructor_function = nullptr;
/** Pseudostack slot for this allocation. Non-null for
* allocations of type Stack with dynamic size. */
llvm::Value *pseudostack_slot = nullptr;
/** The (Halide) type of the allocation. */
Type type;
/** How many bytes this allocation is, or 0 if not
* constant. */
int constant_bytes = 0;
/** How many bytes of stack space used. 0 implies it was a
* heap allocation. */
int stack_bytes = 0;
/** A unique name for this allocation. May not be equal to the
* Allocate node name in cases where we detect multiple
* Allocate nodes can share a single allocation. */
std::string name;
};
/** The allocations currently in scope. The stack gets pushed when
* we enter a new function. */
Scope<Allocation> allocations;
std::string get_allocation_name(const std::string &n) override;
private:
/** Stack allocations that were freed, but haven't gone out of
* scope yet. This allows us to re-use stack allocations when
* they aren't being used. */
std::vector<Allocation> free_stack_allocs;
/** current size of all alloca instances in use; this is tracked only
* for debug output purposes. */
size_t cur_stack_alloc_total{0};
/** Generates code for computing the size of an allocation from a
* list of its extents and its size. Fires a runtime assert
* (halide_error) if the size overflows 2^31 -1, the maximum
* positive number an int32_t can hold. */
llvm::Value *codegen_allocation_size(const std::string &name, Type type, const std::vector<Expr> &extents, const Expr &condition);
/** Allocates some memory on either the stack or the heap, and
* returns an Allocation object describing it. For heap
* allocations this calls halide_malloc in the runtime, and for
* stack allocations it either reuses an existing block from the
* free_stack_blocks list, or it saves the stack pointer and calls
* alloca.
*
* This call returns the allocation, pushes it onto the
* 'allocations' map, and adds an entry to the symbol table called
* name.host that provides the base pointer.
*
* When the allocation can be freed call 'free_allocation', and
* when it goes out of scope call 'destroy_allocation'. */
Allocation create_allocation(const std::string &name, Type type, MemoryType memory_type,
const std::vector<Expr> &extents, const Expr &condition,
const Expr &new_expr, std::string free_function, int padding);
/** Free an allocation previously allocated with
* create_allocation */
void free_allocation(const std::string &name);
};
} // namespace Internal
} // namespace Halide
#endif
CodeGen_LLVM.h
Defines the base-class for all architecture-specific code generators that use llvm.
Halide::Internal::CodeGen_LLVM::CodeGen_LLVM
CodeGen_LLVM(const Target &t)
Halide::Internal::CodeGen_LLVM::visit
void visit(const IntImm *) override
Generate code for various IR nodes.
Halide::Internal::CodeGen_Posix::CodeGen_Posix
CodeGen_Posix(const Target &t)
Create an posix code generator.
Halide::Internal::CodeGen_Posix::visit
void visit(const Allocate *) override
Posix implementation of Allocate.
Halide::Internal::CodeGen_Posix::get_allocation_name
std::string get_allocation_name(const std::string &n) override
Get a unique name for the actual block of memory that an allocate node uses.
Halide::Internal::CodeGen_Posix::visit
void visit(const Free *) override
Generate code for a free node.
Halide::Internal::CodeGen_Posix::allocations
Scope< Allocation > allocations
The allocations currently in scope.
Definition CodeGen_Posix.h:65
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::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::CodeGen_Posix::Allocation
A struct describing heap or stack allocations.
Definition CodeGen_Posix.h:32
Halide::Internal::CodeGen_Posix::Allocation::type
Type type
The (Halide) type of the allocation.
Definition CodeGen_Posix.h:47
Halide::Internal::CodeGen_Posix::Allocation::constant_bytes
int constant_bytes
How many bytes this allocation is, or 0 if not constant.
Definition CodeGen_Posix.h:51
Halide::Internal::CodeGen_Posix::Allocation::ptr
llvm::Value * ptr
The memory.
Definition CodeGen_Posix.h:34
Halide::Internal::CodeGen_Posix::Allocation::name
std::string name
A unique name for this allocation.
Definition CodeGen_Posix.h:60
Halide::Internal::CodeGen_Posix::Allocation::destructor
llvm::Value * destructor
Destructor stack slot for this allocation.
Definition CodeGen_Posix.h:37
Halide::Internal::CodeGen_Posix::Allocation::pseudostack_slot
llvm::Value * pseudostack_slot
Pseudostack slot for this allocation.
Definition CodeGen_Posix.h:44
Halide::Internal::CodeGen_Posix::Allocation::stack_bytes
int stack_bytes
How many bytes of stack space used.
Definition CodeGen_Posix.h:55
Halide::Internal::CodeGen_Posix::Allocation::destructor_function
llvm::Function * destructor_function
Function to accomplish the destruction.
Definition CodeGen_Posix.h:40
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition IR.h:413
Halide::Target
A struct representing a target machine and os to generate code for.
Definition Target.h:19
Halide::Type
Types in the halide type system.
Definition Type.h:283