Halide 22.0.0 Halide compiler and libraries

CodeGen_Posix.h
Go to the documentation of this file.
1#ifndef HALIDE_CODEGEN_POSIX_H
2#define HALIDE_CODEGEN_POSIX_H
3
4/** \file
5 * Defines a base-class for code-generators on posixy cpu platforms
6 */
7
8#include "CodeGen_LLVM.h"
9
10 namespace Halide {
11 namespace Internal {
12
13/** A code generator that emits posix code from a given Halide stmt. */
14 class CodeGen_Posix : public CodeGen_LLVM {
15 public:
16 /** Create an posix code generator. Processor features can be
17 * enabled using the appropriate arguments */
18 CodeGen_Posix(const Target &t);
19
20 protected:
21 using CodeGen_LLVM::visit;
22
23 /** Posix implementation of Allocate. Small constant-sized allocations go
24 * on the stack. The rest go on the heap by calling "halide_malloc"
25 * and "halide_free" in the standard library. */
26 // @{
27 void visit(const Allocate *) override;
28 void visit(const Free *) override;
29 // @}
30
31 /** A struct describing heap or stack allocations. */
32 struct Allocation {
33 /** The memory */
34 llvm::Value *ptr = nullptr;
35
36 /** Destructor stack slot for this allocation. */
37 llvm::Value *destructor = nullptr;
38
39 /** Function to accomplish the destruction. */
40 llvm::Function *destructor_function = nullptr;
41
42 /** Pseudostack slot for this allocation. Non-null for
43 * allocations of type Stack with dynamic size. */
44 llvm::Value *pseudostack_slot = nullptr;
45
46 /** The (Halide) type of the allocation. */
47 Type type;
48
49 /** How many bytes this allocation is, or 0 if not
50 * constant. */
51 int constant_bytes = 0;
52
53 /** How many bytes of stack space used. 0 implies it was a
54 * heap allocation. */
55 int stack_bytes = 0;
56
57 /** A unique name for this allocation. May not be equal to the
58 * Allocate node name in cases where we detect multiple
59 * Allocate nodes can share a single allocation. */
60 std::string name;
61 };
62
63 /** The allocations currently in scope. The stack gets pushed when
64 * we enter a new function. */
65 Scope<Allocation> allocations;
66
67 std::string get_allocation_name(const std::string &n) override;
68
69 private:
70 /** Stack allocations that were freed, but haven't gone out of
71 * scope yet. This allows us to re-use stack allocations when
72 * they aren't being used. */
73 std::vector<Allocation> free_stack_allocs;
74
75 /** current size of all alloca instances in use; this is tracked only
76 * for debug output purposes. */
77 size_t cur_stack_alloc_total{0};
78
79 /** Generates code for computing the size of an allocation from a
80 * list of its extents and its size. Fires a runtime assert
81 * (halide_error) if the size overflows 2^31 -1, the maximum
82 * positive number an int32_t can hold. */
83 llvm::Value *codegen_allocation_size(const std::string &name, Type type, const std::vector<Expr> &extents, const Expr &condition);
84
85 /** Allocates some memory on either the stack or the heap, and
86 * returns an Allocation object describing it. For heap
87 * allocations this calls halide_malloc in the runtime, and for
88 * stack allocations it either reuses an existing block from the
89 * free_stack_blocks list, or it saves the stack pointer and calls
90 * alloca.
91 *
92 * This call returns the allocation, pushes it onto the
93 * 'allocations' map, and adds an entry to the symbol table called
94 * name.host that provides the base pointer.
95 *
96 * When the allocation can be freed call 'free_allocation', and
97 * when it goes out of scope call 'destroy_allocation'. */
98 Allocation create_allocation(const std::string &name, Type type, MemoryType memory_type,
99 const std::vector<Expr> &extents, const Expr &condition,
100 const Expr &new_expr, std::string free_function, int padding);
101
102 /** Free an allocation previously allocated with
103 * create_allocation */
104 void free_allocation(const std::string &name);
105};
106
107} // namespace Internal
108} // namespace Halide
109
110#endif
CodeGen_LLVM.h
Defines the base-class for all architecture-specific code generators that use llvm.
Halide::Internal::CodeGen_LLVM
A code generator abstract base class.
Definition: CodeGen_LLVM.h:60
Halide::Internal::CodeGen_LLVM::visit
void visit(const IntImm *) override
Generate code for various IR nodes.
Halide::Internal::CodeGen_Posix
A code generator that emits posix code from a given Halide stmt.
Definition: CodeGen_Posix.h:14
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
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::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