Halide 13.0.2
Halide compiler and libraries
JITModule.h
Go to the documentation of this file.
1#ifndef HALIDE_JIT_MODULE_H
2#define HALIDE_JIT_MODULE_H
3
4/** \file
5 * Defines the struct representing lifetime and dependencies of
6 * a JIT compiled halide pipeline
7 */
8
9#include <map>
10#include <memory>
11
12#include "IntrusivePtr.h"
13#include "Type.h"
15
16namespace llvm {
17class Module;
18}
19
20namespace Halide {
21
22struct ExternCFunction;
23struct JITExtern;
24struct Target;
25class Module;
26
27namespace Internal {
28
29class JITModuleContents;
30struct LoweredFunc;
31
32struct JITModule {
34
35 struct Symbol {
36 void *address = nullptr;
37 Symbol() = default;
38 explicit Symbol(void *address)
39 : address(address) {
40 }
41 };
42
44 JITModule(const Module &m, const LoweredFunc &fn,
45 const std::vector<JITModule> &dependencies = std::vector<JITModule>());
46
47 /** Take a list of JITExterns and generate trampoline functions
48 * which can be called dynamically via a function pointer that
49 * takes an array of void *'s for each argument and the return
50 * value.
51 */
53 const std::map<std::string, JITExtern> &externs,
54 const std::string &suffix,
55 const std::vector<JITModule> &deps);
56
57 /** The exports map of a JITModule contains all symbols which are
58 * available to other JITModules which depend on this one. For
59 * runtime modules, this is all of the symbols exported from the
60 * runtime. For a JITted Func, it generally only contains the main
61 * result Func of the compilation, which takes its name directly
62 * from the Func declaration. One can also make a module which
63 * contains no code itself but is just an exports maps providing
64 * arbitrary pointers to functions or global variables to JITted
65 * code. */
66 const std::map<std::string, Symbol> &exports() const;
67
68 /** A pointer to the raw halide function. Its true type depends
69 * on the Argument vector passed to CodeGen_LLVM::compile. Image
70 * parameters become (halide_buffer_t *), and scalar parameters become
71 * pointers to the appropriate values. The final argument is a
72 * pointer to the halide_buffer_t defining the output. This will be nullptr for
73 * a JITModule which has not yet been compiled or one that is not
74 * a Halide Func compilation at all. */
75 void *main_function() const;
76
77 /** Returns the Symbol structure for the routine documented in
78 * main_function. Returning a Symbol allows access to the LLVM
79 * type as well as the address. The address and type will be nullptr
80 * if the module has not been compiled. */
82
83 /** Returns the Symbol structure for the argv wrapper routine
84 * corresponding to the entrypoint. The argv wrapper is callable
85 * via an array of void * pointers to the arguments for the
86 * call. Returning a Symbol allows access to the LLVM type as well
87 * as the address. The address and type will be nullptr if the module
88 * has not been compiled. */
90
91 /** A slightly more type-safe wrapper around the raw halide
92 * module. Takes it arguments as an array of pointers that
93 * correspond to the arguments to \ref main_function . This will
94 * be nullptr for a JITModule which has not yet been compiled or one
95 * that is not a Halide Func compilation at all. */
96 // @{
97 typedef int (*argv_wrapper)(const void **args);
99 // @}
100
101 /** Add another JITModule to the dependency chain. Dependencies
102 * are searched to resolve symbols not found in the current
103 * compilation unit while JITting. */
105 /** Registers a single Symbol as available to modules which depend
106 * on this one. The Symbol structure provides both the address and
107 * the LLVM type for the function, which allows type safe linkage of
108 * extenal routines. */
109 void add_symbol_for_export(const std::string &name, const Symbol &extern_symbol);
110 /** Registers a single function as available to modules which
111 * depend on this one. This routine converts the ExternSignature
112 * info into an LLVM type, which allows type safe linkage of
113 * external routines. */
114 void add_extern_for_export(const std::string &name,
115 const ExternCFunction &extern_c_function);
116
117 /** Look up a symbol by name in this module or its dependencies. */
118 Symbol find_symbol_by_name(const std::string &) const;
119
120 /** Take an llvm module and compile it. The requested exports will
121 be available via the exports method. */
122 void compile_module(std::unique_ptr<llvm::Module> mod,
123 const std::string &function_name, const Target &target,
124 const std::vector<JITModule> &dependencies = std::vector<JITModule>(),
125 const std::vector<std::string> &requested_exports = std::vector<std::string>());
126
127 /** See JITSharedRuntime::memoization_cache_set_size */
129
130 /** See JITSharedRuntime::memoization_cache_evict */
131 void memoization_cache_evict(uint64_t eviction_key) const;
132
133 /** See JITSharedRuntime::reuse_device_allocations */
134 void reuse_device_allocations(bool) const;
135
136 /** Return true if compile_module has been called on this module. */
137 bool compiled() const;
138};
139
140typedef int (*halide_task)(void *user_context, int, uint8_t *);
141
143 void (*custom_print)(void *, const char *){nullptr};
144 void *(*custom_malloc)(void *, size_t){nullptr};
145 void (*custom_free)(void *, void *){nullptr};
146 int (*custom_do_task)(void *, halide_task, int, uint8_t *){nullptr};
147 int (*custom_do_par_for)(void *, halide_task, int, int, uint8_t *){nullptr};
148 void (*custom_error)(void *, const char *){nullptr};
149 int32_t (*custom_trace)(void *, const halide_trace_event_t *){nullptr};
150 void *(*custom_get_symbol)(const char *name){nullptr};
151 void *(*custom_load_library)(const char *name){nullptr};
152 void *(*custom_get_library_symbol)(void *lib, const char *name){nullptr};
153};
154
158};
159
161public:
162 // Note only the first llvm::Module passed in here is used. The same shared runtime is used for all JIT.
163 static std::vector<JITModule> get(llvm::Module *m, const Target &target, bool create = true);
164 static void init_jit_user_context(JITUserContext &jit_user_context, void *user_context, const JITHandlers &handlers);
166
167 /** Set the maximum number of bytes used by memoization caching.
168 * If you are compiling statically, you should include HalideRuntime.h
169 * and call halide_memoization_cache_set_size() instead.
170 */
172
173 /** Evict all cache entries that were tagged with the given
174 * eviction_key in the memoize scheduling directive. If you are
175 * compiling statically, you should include HalideRuntime.h and
176 * call halide_memoization_cache_evict() instead.
177 */
178 static void memoization_cache_evict(uint64_t eviction_key);
179
180 /** Set whether or not Halide may hold onto and reuse device
181 * allocations to avoid calling expensive device API allocation
182 * functions. If you are compiling statically, you should include
183 * HalideRuntime.h and call halide_reuse_device_allocations
184 * instead. */
185 static void reuse_device_allocations(bool);
186
187 static void release_all();
188};
189
190void *get_symbol_address(const char *s);
191
192} // namespace Internal
193} // namespace Halide
194
195#endif
This file declares the routines used by Halide internally in its runtime.
Support classes for reference-counting via intrusive shared pointers.
Defines halide types.
static void init_jit_user_context(JITUserContext &jit_user_context, void *user_context, const JITHandlers &handlers)
static void memoization_cache_evict(uint64_t eviction_key)
Evict all cache entries that were tagged with the given eviction_key in the memoize scheduling direct...
static void memoization_cache_set_size(int64_t size)
Set the maximum number of bytes used by memoization caching.
static JITHandlers set_default_handlers(const JITHandlers &handlers)
static void reuse_device_allocations(bool)
Set whether or not Halide may hold onto and reuse device allocations to avoid calling expensive devic...
static std::vector< JITModule > get(llvm::Module *m, const Target &target, bool create=true)
A halide module.
Definition: Module.h:135
HALIDE_ALWAYS_INLINE auto mod(A &&a, B &&b) -> decltype(IRMatcher::operator%(a, b))
Definition: IRMatch.h:1089
void * get_symbol_address(const char *s)
int(* halide_task)(void *user_context, int, uint8_t *)
Definition: JITModule.h:140
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
void * user_context
Definition: printer.h:33
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
signed __INT32_TYPE__ int32_t
unsigned __INT8_TYPE__ uint8_t
__SIZE_TYPE__ size_t
Intrusive shared pointers have a reference count (a RefCount object) stored in the class itself.
Definition: IntrusivePtr.h:68
int(* custom_do_task)(void *, halide_task, int, uint8_t *)
Definition: JITModule.h:146
void(* custom_free)(void *, void *)
Definition: JITModule.h:145
void(* custom_error)(void *, const char *)
Definition: JITModule.h:148
void(* custom_print)(void *, const char *)
Definition: JITModule.h:143
int(* custom_do_par_for)(void *, halide_task, int, int, uint8_t *)
Definition: JITModule.h:147
int32_t(* custom_trace)(void *, const halide_trace_event_t *)
Definition: JITModule.h:149
void memoization_cache_evict(uint64_t eviction_key) const
See JITSharedRuntime::memoization_cache_evict.
void memoization_cache_set_size(int64_t size) const
See JITSharedRuntime::memoization_cache_set_size.
int(* argv_wrapper)(const void **args)
A slightly more type-safe wrapper around the raw halide module.
Definition: JITModule.h:97
void add_symbol_for_export(const std::string &name, const Symbol &extern_symbol)
Registers a single Symbol as available to modules which depend on this one.
void compile_module(std::unique_ptr< llvm::Module > mod, const std::string &function_name, const Target &target, const std::vector< JITModule > &dependencies=std::vector< JITModule >(), const std::vector< std::string > &requested_exports=std::vector< std::string >())
Take an llvm module and compile it.
void add_extern_for_export(const std::string &name, const ExternCFunction &extern_c_function)
Registers a single function as available to modules which depend on this one.
const std::map< std::string, Symbol > & exports() const
The exports map of a JITModule contains all symbols which are available to other JITModules which dep...
void reuse_device_allocations(bool) const
See JITSharedRuntime::reuse_device_allocations.
void add_dependency(JITModule &dep)
Add another JITModule to the dependency chain.
Symbol find_symbol_by_name(const std::string &) const
Look up a symbol by name in this module or its dependencies.
static JITModule make_trampolines_module(const Target &target, const std::map< std::string, JITExtern > &externs, const std::string &suffix, const std::vector< JITModule > &deps)
Take a list of JITExterns and generate trampoline functions which can be called dynamically via a fun...
void * main_function() const
A pointer to the raw halide function.
Symbol argv_entrypoint_symbol() const
Returns the Symbol structure for the argv wrapper routine corresponding to the entrypoint.
bool compiled() const
Return true if compile_module has been called on this module.
Symbol entrypoint_symbol() const
Returns the Symbol structure for the routine documented in main_function.
argv_wrapper argv_function() const
IntrusivePtr< JITModuleContents > jit_module
Definition: JITModule.h:33
JITModule(const Module &m, const LoweredFunc &fn, const std::vector< JITModule > &dependencies=std::vector< JITModule >())
Definition of a lowered function.
Definition: Module.h:96
A struct representing a target machine and os to generate code for.
Definition: Target.h:19