Halide
CodeGen_Internal.h
Go to the documentation of this file.
1 #ifndef HALIDE_CODEGEN_INTERNAL_H
2 #define HALIDE_CODEGEN_INTERNAL_H
3 
4 /** \file
5  *
6  * Defines functionality that's useful to multiple target-specific
7  * CodeGen paths, but shouldn't live in CodeGen_LLVM.h (because that's the
8  * front-end-facing interface to CodeGen).
9  */
10 
11 #include <memory>
12 #include <string>
13 
14 #include "Closure.h"
15 #include "Expr.h"
16 #include "Scope.h"
17 #include "Target.h"
18 
19 namespace llvm {
20 class ConstantFolder;
21 class ElementCount;
22 class Function;
23 class IRBuilderDefaultInserter;
24 class LLVMContext;
25 class Module;
26 class StructType;
27 class TargetMachine;
28 class TargetOptions;
29 class Type;
30 class Value;
31 template<typename, typename>
32 class IRBuilder;
33 } // namespace llvm
34 
35 namespace Halide {
36 namespace Internal {
37 
38 /** The llvm type of a struct containing all of the externally referenced state of a Closure. */
39 llvm::StructType *build_closure_type(const Closure &closure, llvm::StructType *halide_buffer_t_type, llvm::LLVMContext *context);
40 
41 /** Emit code that builds a struct containing all the externally
42  * referenced state. Requires you to pass it a type and struct to fill in,
43  * a scope to retrieve the llvm values from and a builder to place
44  * the packing code. */
45 void pack_closure(llvm::StructType *type,
46  llvm::Value *dst,
47  const Closure &closure,
48  const Scope<llvm::Value *> &src,
49  llvm::StructType *halide_buffer_t_type,
51 
52 /** Emit code that unpacks a struct containing all the externally
53  * referenced state into a symbol table. Requires you to pass it a
54  * state struct type and value, a scope to fill, and a builder to place the
55  * unpacking code. */
56 void unpack_closure(const Closure &closure,
57  Scope<llvm::Value *> &dst,
58  llvm::StructType *type,
59  llvm::Value *src,
61 
62 /** Get the llvm type equivalent to a given halide type */
63 llvm::Type *llvm_type_of(llvm::LLVMContext *context, Halide::Type t);
64 
65 /** Get the number of elements in an llvm vector type, or return 1 if
66  * it's not a vector type. */
67 int get_vector_num_elements(llvm::Type *);
68 
69 /** Get the scalar type of an llvm vector type. Returns the argument
70  * if it's not a vector type. */
71 llvm::Type *get_vector_element_type(llvm::Type *);
72 
73 #if LLVM_VERSION >= 110
74 const llvm::ElementCount element_count(int e);
75 #else
76 int element_count(int e);
77 #endif
78 
79 llvm::Type *get_vector_type(llvm::Type *, int);
80 
81 /** Which built-in functions require a user-context first argument? */
82 bool function_takes_user_context(const std::string &name);
83 
84 /** Given a size (in bytes), return True if the allocation size can fit
85  * on the stack; otherwise, return False. This routine asserts if size is
86  * non-positive. */
88 
89 /** Given a Halide Euclidean division/mod operation, do constant optimizations
90  * and possibly call lower_euclidean_div/lower_euclidean_mod if necessary.
91  * Can introduce mulhi_shr and sorted_avg intrinsics as well as those from the
92  * lower_euclidean_ operation -- div_round_to_zero or mod_round_to_zero. */
93 ///@{
94 Expr lower_int_uint_div(const Expr &a, const Expr &b);
95 Expr lower_int_uint_mod(const Expr &a, const Expr &b);
96 ///@}
97 
98 /** Given a Halide Euclidean division/mod operation, define it in terms of
99  * div_round_to_zero or mod_round_to_zero. */
100 ///@{
101 Expr lower_euclidean_div(Expr a, Expr b);
102 Expr lower_euclidean_mod(Expr a, Expr b);
103 ///@}
104 
105 /** Given a Halide shift operation with a signed shift amount (may be negative), define
106  * an equivalent expression using only shifts by unsigned amounts. */
107 ///@{
108 Expr lower_signed_shift_left(const Expr &a, const Expr &b);
109 Expr lower_signed_shift_right(const Expr &a, const Expr &b);
110 ///@}
111 
112 /** Given an llvm::Module, set llvm:TargetOptions, cpu and attr information */
113 void get_target_options(const llvm::Module &module, llvm::TargetOptions &options, std::string &mcpu, std::string &mattrs);
114 
115 /** Given two llvm::Modules, clone target options from one to the other */
116 void clone_target_options(const llvm::Module &from, llvm::Module &to);
117 
118 /** Given an llvm::Module, get or create an llvm:TargetMachine */
119 std::unique_ptr<llvm::TargetMachine> make_target_machine(const llvm::Module &module);
120 
121 /** Set the appropriate llvm Function attributes given a Target. */
122 void set_function_attributes_for_target(llvm::Function *, Target);
123 
124 /** Save a copy of the llvm IR currently represented by the module as
125  * data in the __LLVM,__bitcode section. Emulates clang's
126  * -fembed-bitcode flag and is useful to satisfy Apple's bitcode
127  * inclusion requirements. */
128 void embed_bitcode(llvm::Module *M, const std::string &halide_command);
129 
130 } // namespace Internal
131 } // namespace Halide
132 
133 #endif
Scope.h
llvm
Definition: CodeGen_Internal.h:19
Halide::Internal::element_count
int element_count(int e)
Halide::Internal::get_target_options
void get_target_options(const llvm::Module &module, llvm::TargetOptions &options, std::string &mcpu, std::string &mattrs)
Given an llvm::Module, set llvm:TargetOptions, cpu and attr information.
Halide::Internal::make_target_machine
std::unique_ptr< llvm::TargetMachine > make_target_machine(const llvm::Module &module)
Given an llvm::Module, get or create an llvm:TargetMachine.
Halide::Internal::set_function_attributes_for_target
void set_function_attributes_for_target(llvm::Function *, Target)
Set the appropriate llvm Function attributes given a Target.
Target.h
Halide::Internal::lower_euclidean_div
Expr lower_euclidean_div(Expr a, Expr b)
Given a Halide Euclidean division/mod operation, define it in terms of div_round_to_zero or mod_round...
Halide::Type
Types in the halide type system.
Definition: Type.h:269
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::Internal::pack_closure
void pack_closure(llvm::StructType *type, llvm::Value *dst, const Closure &closure, const Scope< llvm::Value * > &src, llvm::StructType *halide_buffer_t_type, llvm::IRBuilder< llvm::ConstantFolder, llvm::IRBuilderDefaultInserter > *builder)
Emit code that builds a struct containing all the externally referenced state.
Halide::Internal::embed_bitcode
void embed_bitcode(llvm::Module *M, const std::string &halide_command)
Save a copy of the llvm IR currently represented by the module as data in the __LLVM,...
Halide::Internal::unpack_closure
void unpack_closure(const Closure &closure, Scope< llvm::Value * > &dst, llvm::StructType *type, llvm::Value *src, llvm::IRBuilder< llvm::ConstantFolder, llvm::IRBuilderDefaultInserter > *builder)
Emit code that unpacks a struct containing all the externally referenced state into a symbol table.
Halide::Internal::llvm_type_of
llvm::Type * llvm_type_of(llvm::LLVMContext *context, Halide::Type t)
Get the llvm type equivalent to a given halide type.
Halide::Internal::clone_target_options
void clone_target_options(const llvm::Module &from, llvm::Module &to)
Given two llvm::Modules, clone target options from one to the other.
Halide::Internal::lower_signed_shift_left
Expr lower_signed_shift_left(const Expr &a, const Expr &b)
Given a Halide shift operation with a signed shift amount (may be negative), define an equivalent exp...
int64_t
signed __INT64_TYPE__ int64_t
Definition: runtime_internal.h:18
Halide::Internal::lower_int_uint_mod
Expr lower_int_uint_mod(const Expr &a, const Expr &b)
Given a Halide Euclidean division/mod operation, do constant optimizations and possibly call lower_eu...
Expr.h
Halide::Internal::get_vector_element_type
llvm::Type * get_vector_element_type(llvm::Type *)
Get the scalar type of an llvm vector type.
Halide::Internal::function_takes_user_context
bool function_takes_user_context(const std::string &name)
Which built-in functions require a user-context first argument?
Halide::Internal::can_allocation_fit_on_stack
bool can_allocation_fit_on_stack(int64_t size)
Given a size (in bytes), return True if the allocation size can fit on the stack; otherwise,...
Halide::Internal::get_vector_num_elements
int get_vector_num_elements(llvm::Type *)
Get the number of elements in an llvm vector type, or return 1 if it's not a vector type.
dst
char * dst
Definition: printer.h:32
Halide::Internal::lower_int_uint_div
Expr lower_int_uint_div(const Expr &a, const Expr &b)
Given a Halide Euclidean division/mod operation, do constant optimizations and possibly call lower_eu...
Halide::Internal::lower_euclidean_mod
Expr lower_euclidean_mod(Expr a, Expr b)
Given a Halide Euclidean division/mod operation, define it in terms of div_round_to_zero or mod_round...
Closure.h
llvm::IRBuilder
Definition: CodeGen_Internal.h:32
Halide::Internal::get_vector_type
llvm::Type * get_vector_type(llvm::Type *, int)
Halide::Internal::lower_signed_shift_right
Expr lower_signed_shift_right(const Expr &a, const Expr &b)
Given a Halide shift operation with a signed shift amount (may be negative), define an equivalent exp...
Halide::Internal::build_closure_type
llvm::StructType * build_closure_type(const Closure &closure, llvm::StructType *halide_buffer_t_type, llvm::LLVMContext *context)
The llvm type of a struct containing all of the externally referenced state of a Closure.