Halide 14.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
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 /** It can be convenient for backends to assume there is extra
32 * padding beyond the end of a buffer to enable faster
33 * loads/stores. This function gets the padding required by the
34 * implementing target. */
35 virtual int allocation_padding(Type type) const;
36
37 /** A struct describing heap or stack allocations. */
38 struct Allocation {
39 /** The memory */
40 llvm::Value *ptr = nullptr;
41
42 /** Destructor stack slot for this allocation. */
43 llvm::Value *destructor = nullptr;
44
45 /** Function to accomplish the destruction. */
46 llvm::Function *destructor_function = nullptr;
47
48 /** Pseudostack slot for this allocation. Non-null for
49 * allocations of type Stack with dynamic size. */
50 llvm::Value *pseudostack_slot = nullptr;
51
52 /** The (Halide) type of the allocation. */
54
55 /** How many bytes this allocation is, or 0 if not
56 * constant. */
58
59 /** How many bytes of stack space used. 0 implies it was a
60 * heap allocation. */
61 int stack_bytes = 0;
62
63 /** A unique name for this allocation. May not be equal to the
64 * Allocate node name in cases where we detect multiple
65 * Allocate nodes can share a single allocation. */
66 std::string name;
67 };
68
69 /** The allocations currently in scope. The stack gets pushed when
70 * we enter a new function. */
72
73 std::string get_allocation_name(const std::string &n) override;
74
75private:
76 /** Stack allocations that were freed, but haven't gone out of
77 * scope yet. This allows us to re-use stack allocations when
78 * they aren't being used. */
79 std::vector<Allocation> free_stack_allocs;
80
81 /** current size of all alloca instances in use; this is tracked only
82 * for debug output purposes. */
83 size_t cur_stack_alloc_total{0};
84
85 /** Generates code for computing the size of an allocation from a
86 * list of its extents and its size. Fires a runtime assert
87 * (halide_error) if the size overflows 2^31 -1, the maximum
88 * positive number an int32_t can hold. */
89 llvm::Value *codegen_allocation_size(const std::string &name, Type type, const std::vector<Expr> &extents, const Expr &condition);
90
91 /** Allocates some memory on either the stack or the heap, and
92 * returns an Allocation object describing it. For heap
93 * allocations this calls halide_malloc in the runtime, and for
94 * stack allocations it either reuses an existing block from the
95 * free_stack_blocks list, or it saves the stack pointer and calls
96 * alloca.
97 *
98 * This call returns the allocation, pushes it onto the
99 * 'allocations' map, and adds an entry to the symbol table called
100 * name.host that provides the base pointer.
101 *
102 * When the allocation can be freed call 'free_allocation', and
103 * when it goes out of scope call 'destroy_allocation'. */
104 Allocation create_allocation(const std::string &name, Type type, MemoryType memory_type,
105 const std::vector<Expr> &extents,
106 const Expr &condition, const Expr &new_expr, std::string free_function);
107
108 /** Free an allocation previously allocated with
109 * create_allocation */
110 void free_allocation(const std::string &name);
111};
112
113} // namespace Internal
114} // namespace Halide
115
116#endif
Defines the base-class for all architecture-specific code generators that use llvm.
A code generator abstract base class.
Definition: CodeGen_LLVM.h:57
void visit(const IntImm *) override
Generate code for various IR nodes.
A code generator that emits posix code from a given Halide stmt.
Definition: CodeGen_Posix.h:14
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.
Definition: CodeGen_Posix.h:71
virtual int allocation_padding(Type type) const
It can be convenient for backends to assume there is extra padding beyond the end of a buffer to enab...
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:346
A fragment of Halide syntax.
Definition: Expr.h:256
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:353
A struct describing heap or stack allocations.
Definition: CodeGen_Posix.h:38
Type type
The (Halide) type of the allocation.
Definition: CodeGen_Posix.h:53
int constant_bytes
How many bytes this allocation is, or 0 if not constant.
Definition: CodeGen_Posix.h:57
std::string name
A unique name for this allocation.
Definition: CodeGen_Posix.h:66
llvm::Value * destructor
Destructor stack slot for this allocation.
Definition: CodeGen_Posix.h:43
llvm::Value * pseudostack_slot
Pseudostack slot for this allocation.
Definition: CodeGen_Posix.h:50
int stack_bytes
How many bytes of stack space used.
Definition: CodeGen_Posix.h:61
llvm::Function * destructor_function
Function to accomplish the destruction.
Definition: CodeGen_Posix.h:46
Free the resources associated with the given buffer.
Definition: IR.h:389
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:266