Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
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
10namespace Halide {
11namespace Internal {
12
13/** A code generator that emits posix code from a given Halide stmt. */
15public:
16 /** Create an posix code generator. Processor features can be
17 * enabled using the appropriate arguments */
19
20protected:
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. */
48
49 /** How many bytes this allocation is, or 0 if not
50 * constant. */
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. */
66
67 std::string get_allocation_name(const std::string &n) override;
68
69private:
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
Defines the base-class for all architecture-specific code generators that use llvm.
A code generator abstract base class.
void visit(const IntImm *) override
Generate code for various IR nodes.
A code generator that emits posix code from a given Halide stmt.
CodeGen_Posix(const Target &t)
Create an posix code generator.
void visit(const Allocate *) override
Posix implementation of Allocate.
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.
void visit(const Free *) override
Generate code for a free node.
Scope< Allocation > allocations
The allocations currently in 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
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
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
A struct describing heap or stack allocations.
Type type
The (Halide) type of the allocation.
int constant_bytes
How many bytes this allocation is, or 0 if not constant.
std::string name
A unique name for this allocation.
llvm::Value * destructor
Destructor stack slot for this allocation.
llvm::Value * pseudostack_slot
Pseudostack slot for this allocation.
int stack_bytes
How many bytes of stack space used.
llvm::Function * destructor_function
Function to accomplish the destruction.
Free the resources associated with the given buffer.
Definition IR.h:413
A struct representing a target machine and os to generate code for.
Definition Target.h:19
Types in the halide type system.
Definition Type.h:283