Halide
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 */
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  /** 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. */
57  int constant_bytes = 0;
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 
75 private:
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
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
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_LLVM
A code generator abstract base class.
Definition: CodeGen_LLVM.h:58
Halide::Internal::CodeGen_Posix::Allocation::ptr
llvm::Value * ptr
The memory.
Definition: CodeGen_Posix.h:40
Halide::Internal::CodeGen_Posix::Allocation::destructor_function
llvm::Function * destructor_function
Function to accomplish the destruction.
Definition: CodeGen_Posix.h:46
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::visit
void visit(const Allocate *) override
Posix implementation of Allocate.
CodeGen_LLVM.h
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: ModulusRemainder.h:17
Halide::Internal::CodeGen_Posix::Allocation
A struct describing heap or stack allocations.
Definition: CodeGen_Posix.h:38
Halide::Internal::CodeGen_Posix::CodeGen_Posix
CodeGen_Posix(Target t)
Create an posix code generator.
Halide::Type
Types in the halide type system.
Definition: Type.h:269
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition: IR.h:388
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::CodeGen_Posix::Allocation::destructor
llvm::Value * destructor
Destructor stack slot for this allocation.
Definition: CodeGen_Posix.h:43
Halide::Internal::CodeGen_Posix::Allocation::type
Type type
The (Halide) type of the allocation.
Definition: CodeGen_Posix.h:53
Halide::Internal::CodeGen_Posix::Allocation::name
std::string name
A unique name for this allocation.
Definition: CodeGen_Posix.h:66
Halide::Internal::CodeGen_LLVM::visit
void visit(const IntImm *) override
Generate code for various IR nodes.
Halide::Internal::CodeGen_Posix::allocations
Scope< Allocation > allocations
The allocations currently in scope.
Definition: CodeGen_Posix.h:71
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:57
Halide::Internal::CodeGen_Posix::Allocation::pseudostack_slot
llvm::Value * pseudostack_slot
Pseudostack slot for this allocation.
Definition: CodeGen_Posix.h:50
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:256
Halide::Internal::CodeGen_Posix::Allocation::stack_bytes
int stack_bytes
How many bytes of stack space used.
Definition: CodeGen_Posix.h:61
Halide::MemoryType
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:346
Halide::Internal::CodeGen_Posix::allocation_padding
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...
Halide::Target
A struct representing a target machine and os to generate code for.
Definition: Target.h:19