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