Halide 14.0.0
Halide compiler and libraries
runtime_internal.h
Go to the documentation of this file.
1#ifndef HALIDE_RUNTIME_INTERNAL_H
2#define HALIDE_RUNTIME_INTERNAL_H
3
4#if __STDC_HOSTED__
5#error "Halide runtime files must be compiled with clang in freestanding mode."
6#endif
7
8#ifdef __UINT8_TYPE__
9typedef __INT64_TYPE__ int64_t;
10typedef __UINT64_TYPE__ uint64_t;
11typedef __INT32_TYPE__ int32_t;
12typedef __UINT32_TYPE__ uint32_t;
13typedef __INT16_TYPE__ int16_t;
14typedef __UINT16_TYPE__ uint16_t;
15typedef __INT8_TYPE__ int8_t;
16typedef __UINT8_TYPE__ uint8_t;
17#else
18typedef signed __INT64_TYPE__ int64_t;
19typedef unsigned __INT64_TYPE__ uint64_t;
20typedef signed __INT32_TYPE__ int32_t;
21typedef unsigned __INT32_TYPE__ uint32_t;
22typedef signed __INT16_TYPE__ int16_t;
23typedef unsigned __INT16_TYPE__ uint16_t;
24typedef signed __INT8_TYPE__ int8_t;
25typedef unsigned __INT8_TYPE__ uint8_t;
26#endif
27typedef __SIZE_TYPE__ size_t;
28typedef __PTRDIFF_TYPE__ ptrdiff_t;
29
31
32// --------------
33
34// In Halide runtime code, most functions should just be WEAK, whether or not
35// they're part of the public API.
36//
37// ALWAYS_INLINE is for things that either should be inlined for performance
38// reasons, or for things that go into every compiled pipeline (not just the
39// standalone runtime), as those things have to either disappear entirely by
40// being inlined away, or have "inline" linkage to avoid multiple definition
41// errors on platforms that have no weak linkage.
42//
43// WEAK_INLINED is a special case where we are 'inlining' the bitcode at
44// bitcode-compilation time (rather than C++ compilation time); it's needed
45// for a few places in the runtime where we can't inline in the traditional
46// way.
47
48#define WEAK __attribute__((weak))
49
50// Note that ALWAYS_INLINE should *always* also be `inline`.
51#define ALWAYS_INLINE inline __attribute__((always_inline))
52
53// Note that WEAK_INLINE should *not* also be `inline`
54#define WEAK_INLINE __attribute__((weak, always_inline))
55
56// --------------
57
58#ifdef BITS_64
59#define INT64_C(c) c##L
60#define UINT64_C(c) c##UL
61typedef uint64_t uintptr_t;
62typedef int64_t intptr_t;
63#endif
64
65#ifdef BITS_32
66#define INT64_C(c) c##LL
67#define UINT64_C(c) c##ULL
68typedef uint32_t uintptr_t;
69typedef int32_t intptr_t;
70#endif
71
72#define STDOUT_FILENO 1
73#define STDERR_FILENO 2
74
75// Commonly-used extern functions
76extern "C" {
77void *halide_malloc(void *user_context, size_t x);
78void halide_free(void *user_context, void *ptr);
80WEAK void halide_print(void *user_context, const char *msg);
81WEAK void halide_error(void *user_context, const char *msg);
82WEAK void (*halide_set_custom_print(void (*print)(void *, const char *)))(void *, const char *);
83WEAK void (*halide_set_error_handler(void (*handler)(void *, const char *)))(void *, const char *);
84
85char *getenv(const char *);
86void free(void *);
87void *malloc(size_t);
88const char *strstr(const char *, const char *);
89int atoi(const char *);
90int strcmp(const char *s, const char *t);
91int strncmp(const char *s, const char *t, size_t n);
92size_t strlen(const char *s);
93const char *strchr(const char *s, int c);
94void *memcpy(void *s1, const void *s2, size_t n);
95int memcmp(const void *s1, const void *s2, size_t n);
96void *memset(void *s, int val, size_t n);
97// Use fopen+fileno+fclose instead of open+close - the value of the
98// flags passed to open are different on every platform
99void *fopen(const char *, const char *);
100int fileno(void *);
101int fclose(void *);
102int close(int);
103size_t fwrite(const void *, size_t, size_t, void *);
104ssize_t write(int fd, const void *buf, size_t bytes);
105int remove(const char *pathname);
106int ioctl(int fd, unsigned long request, ...);
107char *strncpy(char *dst, const char *src, size_t n);
108void abort();
109
110// Below are prototypes for various functions called by generated code
111// and parts of the runtime but not exposed to users:
112
113// Similar to strncpy, but with various non-string arguments. Writes
114// arg to dst. Does not write to pointer end or beyond. Returns
115// pointer to one beyond the last character written so that calls can
116// be chained.
117
118struct halide_buffer_t;
119struct halide_type_t;
120WEAK char *halide_string_to_string(char *dst, char *end, const char *arg);
121WEAK char *halide_double_to_string(char *dst, char *end, double arg, int scientific);
122WEAK char *halide_int64_to_string(char *dst, char *end, int64_t arg, int digits);
123WEAK char *halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits);
124WEAK char *halide_pointer_to_string(char *dst, char *end, const void *arg);
125WEAK char *halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg);
126WEAK char *halide_type_to_string(char *dst, char *end, const halide_type_t *arg);
127
128// Search the current process for a symbol with the given name.
129WEAK void *halide_get_symbol(const char *name);
130// Platform specific implementations of dlopen/dlsym.
131WEAK void *halide_load_library(const char *name);
132// If lib is nullptr, this call should be equivalent to halide_get_symbol(name).
133WEAK void *halide_get_library_symbol(void *lib, const char *name);
134
135WEAK int halide_start_clock(void *user_context);
136WEAK int64_t halide_current_time_ns(void *user_context);
137WEAK void halide_sleep_ms(void *user_context, int ms);
138WEAK void halide_device_free_as_destructor(void *user_context, void *obj);
139WEAK void halide_device_and_host_free_as_destructor(void *user_context, void *obj);
140WEAK void halide_device_host_nop_free(void *user_context, void *obj);
141
142// The pipeline_state is declared as void* type since halide_profiler_pipeline_stats
143// is defined inside HalideRuntime.h which includes this header file.
145 void *pipeline_state,
146 uint64_t *f_values);
147WEAK void halide_profiler_memory_allocate(void *user_context,
148 void *pipeline_state,
149 int func_id,
150 uint64_t incr);
151WEAK void halide_profiler_memory_free(void *user_context,
152 void *pipeline_state,
153 int func_id,
154 uint64_t decr);
156 const char *pipeline_name,
157 int num_funcs,
158 const uint64_t *func_names);
160
161WEAK int halide_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf,
162 const struct halide_device_interface_t *device_interface);
163WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf);
164
166
167struct mxArray;
168WEAK int halide_matlab_call_pipeline(void *user_context,
169 int (*pipeline)(void **args), const halide_filter_metadata_t *metadata,
170 int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs);
171
172WEAK int halide_trace_helper(void *user_context,
173 const char *func,
174 void *value, int *coords,
175 int type_code, int type_bits, int type_lanes,
176 int code,
177 int parent_id, int value_index, int dimensions,
178 const char *trace_tag);
179
181 void *ptr;
182 size_t size;
184};
185
188
190
192
193} // extern "C"
194
195namespace {
196template<typename T>
197ALWAYS_INLINE void swap(T &a, T &b) {
198 T t = a;
199 a = b;
200 b = t;
201}
202
203template<typename T>
204ALWAYS_INLINE T max(const T &a, const T &b) {
205 return a > b ? a : b;
206}
207
208template<typename T>
209ALWAYS_INLINE T min(const T &a, const T &b) {
210 return a < b ? a : b;
211}
212
213} // namespace
214
215// A namespace for runtime modules to store their internal state
216// in. Should not be for things communicated between runtime modules,
217// because it's possible for them to be compiled with different c++
218// name mangling due to mixing and matching target triples.
219namespace Halide {
220namespace Runtime {
221namespace Internal {
222// Empty
223}
224} // namespace Runtime
225} // namespace Halide
226using namespace Halide::Runtime::Internal;
227
228/** halide_abort_if_false() is a macro that calls halide_print if the supplied condition is
229 * false, then aborts. Used for unrecoverable errors, or should-never-happen errors.
230 *
231 * Note that this is *NOT* a debug-only macro;
232 * the condition will be checked in *all* build modes! */
233#define _halide_stringify(x) #x
234#define _halide_expand_and_stringify(x) _halide_stringify(x)
235#define halide_abort_if_false(user_context, cond) \
236 do { \
237 if (!(cond)) { \
238 halide_print(user_context, __FILE__ ":" _halide_expand_and_stringify(__LINE__) " halide_abort_if_false() failed: " #cond "\n"); \
239 abort(); \
240 } \
241 } while (0)
242
243/** halide_debug_assert() is like halide_assert(), but only expands into a check when
244 * DEBUG_RUNTIME is defined. It is what you want to use in almost all cases. */
245#ifdef DEBUG_RUNTIME
246#define halide_debug_assert(user_context, cond) \
247 do { \
248 if (!(cond)) { \
249 halide_print(user_context, __FILE__ ":" _halide_expand_and_stringify(__LINE__) " halide_debug_assert() failed: " #cond "\n"); \
250 abort(); \
251 } \
252 } while (0)
253#else
254#define halide_debug_assert(user_context, cond)
255#endif
256
257#endif // HALIDE_RUNTIME_INTERNAL_H
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:600
Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
Expr max(const FuncRef &a, const FuncRef &b)
Definition: Func.h:603
unsigned __INT64_TYPE__ uint64_t
int remove(const char *pathname)
const char * strchr(const char *s, int c)
signed __INT64_TYPE__ int64_t
WEAK int halide_host_cpu_count()
int ioctl(int fd, unsigned long request,...)
int fileno(void *)
WEAK void * halide_get_library_symbol(void *lib, const char *name)
WEAK char * halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits)
int strcmp(const char *s, const char *t)
#define WEAK_INLINE
void * halide_malloc(void *user_context, size_t x)
void halide_free(void *user_context, void *ptr)
WEAK int halide_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf, const struct halide_device_interface_t *device_interface)
WEAK int halide_trace_helper(void *user_context, const char *func, void *value, int *coords, int type_code, int type_bits, int type_lanes, int code, int parent_id, int value_index, int dimensions, const char *trace_tag)
int strncmp(const char *s, const char *t, size_t n)
WEAK int halide_start_clock(void *user_context)
WEAK void * halide_load_library(const char *name)
WEAK void halide_device_free_as_destructor(void *user_context, void *obj)
WEAK void halide_sleep_ms(void *user_context, int ms)
size_t fwrite(const void *, size_t, size_t, void *)
int fclose(void *)
int atoi(const char *)
void * malloc(size_t)
WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf)
WEAK char * halide_type_to_string(char *dst, char *end, const halide_type_t *arg)
WEAK void halide_release_jit_module()
WEAK void * halide_get_symbol(const char *name)
signed __INT32_TYPE__ int32_t
WEAK int halide_profiler_pipeline_start(void *user_context, const char *pipeline_name, int num_funcs, const uint64_t *func_names)
unsigned __INT8_TYPE__ uint8_t
WEAK void halide_profiler_stack_peak_update(void *user_context, void *pipeline_state, uint64_t *f_values)
WEAK void halide_device_and_host_free_as_destructor(void *user_context, void *obj)
__PTRDIFF_TYPE__ ptrdiff_t
WEAK int halide_matlab_call_pipeline(void *user_context, int(*pipeline)(void **args), const halide_filter_metadata_t *metadata, int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs)
WEAK_INLINE int halide_malloc_alignment()
WEAK void(*)(void *, const char *) halide_set_custom_print(void(*print)(void *, const char *))
void halide_thread_yield()
char * strncpy(char *dst, const char *src, size_t n)
unsigned __INT16_TYPE__ uint16_t
WEAK char * halide_int64_to_string(char *dst, char *end, int64_t arg, int digits)
WEAK char * halide_pointer_to_string(char *dst, char *end, const void *arg)
void * memcpy(void *s1, const void *s2, size_t n)
ssize_t write(int fd, const void *buf, size_t bytes)
WEAK void halide_print(void *user_context, const char *msg)
int memcmp(const void *s1, const void *s2, size_t n)
#define ALWAYS_INLINE
size_t strlen(const char *s)
__SIZE_TYPE__ size_t
void * memset(void *s, int val, size_t n)
unsigned __INT32_TYPE__ uint32_t
ptrdiff_t ssize_t
WEAK char * halide_double_to_string(char *dst, char *end, double arg, int scientific)
WEAK void halide_profiler_memory_allocate(void *user_context, void *pipeline_state, int func_id, uint64_t incr)
WEAK int64_t halide_current_time_ns(void *user_context)
signed __INT16_TYPE__ int16_t
WEAK void halide_error(void *user_context, const char *msg)
WEAK void halide_use_jit_module()
void abort()
const char * strstr(const char *, const char *)
signed __INT8_TYPE__ int8_t
#define WEAK
int close(int)
WEAK char * halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg)
WEAK void(*)(void *, const char *) halide_set_error_handler(void(*handler)(void *, const char *))
void free(void *)
void * fopen(const char *, const char *)
WEAK void halide_device_host_nop_free(void *user_context, void *obj)
char * getenv(const char *)
WEAK void halide_profiler_memory_free(void *user_context, void *pipeline_state, int func_id, uint64_t decr)
WEAK char * halide_string_to_string(char *dst, char *end, const char *arg)
The raw representation of an image passed around by generated Halide code.
Each GPU API provides a halide_device_interface_t struct pointing to the code that manages device all...
A runtime tag for a type in the halide type system.