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 #define WEAK __attribute__((weak))
34 
35 #ifdef BITS_64
36 #define INT64_C(c) c ## L
37 #define UINT64_C(c) c ## UL
38 typedef uint64_t uintptr_t;
39 typedef int64_t intptr_t;
40 #endif
41 
42 #ifdef BITS_32
43 #define INT64_C(c) c ## LL
44 #define UINT64_C(c) c ## ULL
45 typedef uint32_t uintptr_t;
46 typedef int32_t intptr_t;
47 #endif
48 
49 #define STDOUT_FILENO 1
50 #define STDERR_FILENO 2
51 
52 // Commonly-used extern functions
53 extern "C" {
54 void *halide_malloc(void *user_context, size_t x);
55 void halide_free(void *user_context, void *ptr);
56 WEAK int64_t halide_current_time_ns(void *user_context);
57 WEAK void halide_print(void *user_context, const char *msg);
58 WEAK void halide_error(void *user_context, const char *msg);
59 WEAK void (*halide_set_custom_print(void (*print)(void *, const char *)))(void *, const char *);
60 WEAK void (*halide_set_error_handler(void (*handler)(void *, const char *)))(void *, const char *);
61 
62 char *getenv(const char *);
63 void free(void *);
64 void *malloc(size_t);
65 const char *strstr(const char *, const char *);
66 int atoi(const char *);
67 int strcmp(const char* s, const char* t);
68 int strncmp(const char* s, const char* t, size_t n);
69 size_t strlen(const char* s);
70 const char *strchr(const char* s, int c);
71 void* memcpy(void* s1, const void* s2, size_t n);
72 int memcmp(const void* s1, const void* s2, size_t n);
73 void *memset(void *s, int val, size_t n);
74 // Use fopen+fileno+fclose instead of open+close - the value of the
75 // flags passed to open are different on every platform
76 void *fopen(const char *, const char *);
77 int fileno(void *);
78 int fclose(void *);
79 int close(int);
80 size_t fwrite(const void *, size_t, size_t, void *);
81 ssize_t write(int fd, const void *buf, size_t bytes);
82 int remove(const char *pathname);
83 int ioctl(int fd, unsigned long request, ...);
84 void exit(int);
85 void abort();
86 char *strncpy(char *dst, const char *src, size_t n);
87 
88 // Below are prototypes for various functions called by generated code
89 // and parts of the runtime but not exposed to users:
90 
91 // Similar to strncpy, but with various non-string arguments. Writes
92 // arg to dst. Does not write to pointer end or beyond. Returns
93 // pointer to one beyond the last character written so that calls can
94 // be chained.
95 
96 struct halide_buffer_t;
97 struct halide_type_t;
98 WEAK char *halide_string_to_string(char *dst, char *end, const char *arg);
99 WEAK char *halide_double_to_string(char *dst, char *end, double arg, int scientific);
100 WEAK char *halide_int64_to_string(char *dst, char *end, int64_t arg, int digits);
101 WEAK char *halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits);
102 WEAK char *halide_pointer_to_string(char *dst, char *end, const void *arg);
103 WEAK char *halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg);
104 WEAK char *halide_type_to_string(char *dst, char *end, const halide_type_t *arg);
105 
106 // Search the current process for a symbol with the given name.
107 WEAK void *halide_get_symbol(const char *name);
108 // Platform specific implementations of dlopen/dlsym.
109 WEAK void *halide_load_library(const char *name);
110 // If lib is NULL, this call should be equivalent to halide_get_symbol(name).
111 WEAK void *halide_get_library_symbol(void *lib, const char *name);
112 
113 WEAK int halide_start_clock(void *user_context);
114 WEAK int64_t halide_current_time_ns(void *user_context);
115 WEAK void halide_sleep_ms(void *user_context, int ms);
116 WEAK void halide_device_free_as_destructor(void *user_context, void *obj);
117 WEAK void halide_device_and_host_free_as_destructor(void *user_context, void *obj);
118 WEAK void halide_device_host_nop_free(void *user_context, void *obj);
119 
120 // The pipeline_state is declared as void* type since halide_profiler_pipeline_stats
121 // is defined inside HalideRuntime.h which includes this header file.
122 WEAK void halide_profiler_stack_peak_update(void *user_context,
123  void *pipeline_state,
124  uint64_t *f_values);
125 WEAK void halide_profiler_memory_allocate(void *user_context,
126  void *pipeline_state,
127  int func_id,
128  uint64_t incr);
129 WEAK void halide_profiler_memory_free(void *user_context,
130  void *pipeline_state,
131  int func_id,
132  uint64_t decr);
133 WEAK int halide_profiler_pipeline_start(void *user_context,
134  const char *pipeline_name,
135  int num_funcs,
136  const uint64_t *func_names);
138 
139 WEAK int halide_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf,
140  const struct halide_device_interface_t *device_interface);
141 WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf);
142 
144 
145 struct mxArray;
146 WEAK int halide_matlab_call_pipeline(void *user_context,
147  int (*pipeline)(void **args), const halide_filter_metadata_t *metadata,
148  int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs);
149 
150 
151 // Condition variables. Only available on some platforms (those that use the common thread pool).
152 struct halide_cond {
154 };
155 
156 WEAK void halide_cond_init(struct halide_cond *cond);
157 WEAK void halide_cond_destroy(struct halide_cond *cond);
158 WEAK void halide_cond_broadcast(struct halide_cond *cond);
159 WEAK void halide_cond_wait(struct halide_cond *cond, struct halide_mutex *mutex);
160 
161 WEAK int halide_trace_helper(void *user_context,
162  const char *func,
163  void *value, int *coords,
164  int type_code, int type_bits, int type_lanes,
165  int code,
166  int parent_id, int value_index, int dimensions);
167 
168 } // extern "C"
169 
170 /** A macro that calls halide_print if the supplied condition is
171  * false, then aborts. Used for unrecoverable errors, or
172  * should-never-happen errors. */
173 #define _halide_stringify(x) #x
174 #define _halide_expand_and_stringify(x) _halide_stringify(x)
175 #define halide_assert(user_context, cond) \
176  if (!(cond)) { \
177  halide_print(user_context, __FILE__ ":" _halide_expand_and_stringify(__LINE__) " Assert failed: " #cond "\n"); \
178  abort(); \
179  }
180 
181 // A convenient namespace for weak functions that are internal to the
182 // halide runtime.
183 namespace Halide { namespace Runtime { namespace Internal {
184 
185 extern WEAK void halide_use_jit_module();
186 extern WEAK void halide_release_jit_module();
187 
188 // Return a mask with all CPU-specific features supported by the current CPU set.
189 struct CpuFeatures {
190  uint64_t known; // mask of the CPU features we know how to detect
191  uint64_t available; // mask of the CPU features that are available
192  // (always a subset of 'known')
193 };
195 
196 template <typename T>
197 __attribute__((always_inline)) void swap(T &a, T &b) {
198  T t = a;
199  a = b;
200  b = t;
201 }
202 
203 template <typename T>
204 __attribute__((always_inline)) T max(const T &a, const T &b) {
205  return a > b ? a : b;
206 }
207 
208 template <typename T>
209 __attribute__((always_inline)) T min(const T &a, const T &b) {
210  return a < b ? a : b;
211 }
212 
213 template <typename T, typename U>
214 __attribute__((always_inline)) T reinterpret(const U &x) {
215  T ret;
216  memcpy(&ret, &x, min(sizeof(T), sizeof(U)));
217  return ret;
218 }
219 
220 extern WEAK __attribute__((always_inline)) int halide_malloc_alignment();
221 
222 }}}
223 
224 using namespace Halide::Runtime::Internal;
225 
226 #endif
const char * strchr(const char *s, int c)
Cross-platform mutex.
Definition: HalideRuntime.h:97
void * halide_malloc(void *user_context, size_t x)
uint64_t _private[8]
WEAK int halide_start_clock(void *user_context)
void * user_context
Definition: printer.h:31
WEAK void halide_profiler_stack_peak_update(void *user_context, void *pipeline_state, uint64_t *f_values)
WEAK char * halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits)
WEAK int64_t halide_current_time_ns(void *user_context)
int strncmp(const char *s, const char *t, size_t n)
int ioctl(int fd, unsigned long request,...)
WEAK __attribute__((always_inline)) int halide_malloc_alignment()
int strcmp(const char *s, const char *t)
WEAK int halide_host_cpu_count()
#define WEAK
WEAK void halide_device_free_as_destructor(void *user_context, void *obj)
WEAK void halide_device_host_nop_free(void *user_context, void *obj)
void * memcpy(void *s1, const void *s2, size_t n)
WEAK char * halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg)
size_t strlen(const char *s)
signed __INT8_TYPE__ int8_t
WEAK void * halide_get_symbol(const char *name)
EXPORT Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
WEAK char * halide_int64_to_string(char *dst, char *end, int64_t arg, int digits)
WEAK void(*)(void *, const char *) halide_set_custom_print(void(*print)(void *, const char *))
Defines methods for manipulating and analyzing boolean expressions.
void exit(int)
WEAK int halide_profiler_pipeline_start(void *user_context, const char *pipeline_name, int num_funcs, const uint64_t *func_names)
const char * strstr(const char *, const char *)
WEAK char * halide_pointer_to_string(char *dst, char *end, const void *arg)
unsigned __INT8_TYPE__ uint8_t
WEAK void halide_device_and_host_free_as_destructor(void *user_context, void *obj)
Expr min(FuncRef a, FuncRef b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:418
__PTRDIFF_TYPE__ ptrdiff_t
WEAK char * halide_double_to_string(char *dst, char *end, double arg, int scientific)
WEAK void * halide_load_library(const char *name)
WEAK void halide_cond_init(struct halide_cond *cond)
int fileno(void *)
int atoi(const char *)
WEAK int halide_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf, const struct halide_device_interface_t *device_interface)
WEAK char * halide_type_to_string(char *dst, char *end, const halide_type_t *arg)
Each GPU API provides a halide_device_interface_t struct pointing to the code that manages device all...
char * end
Definition: printer.h:30
WEAK void * halide_get_library_symbol(void *lib, const char *name)
WEAK void halide_release_jit_module()
unsigned __INT32_TYPE__ uint32_t
WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf)
char * dst
Definition: printer.h:30
ssize_t write(int fd, const void *buf, size_t bytes)
Expr reinterpret(Type t, Expr e)
Reinterpret the bits of one value as another type.
Definition: IROperator.h:1327
signed __INT64_TYPE__ int64_t
void * fopen(const char *, const char *)
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)
void halide_free(void *user_context, void *ptr)
ptrdiff_t ssize_t
WEAK void halide_error(void *user_context, const char *msg)
WEAK void halide_cond_destroy(struct halide_cond *cond)
WEAK void halide_profiler_memory_free(void *user_context, void *pipeline_state, int func_id, uint64_t decr)
WEAK void halide_profiler_memory_allocate(void *user_context, void *pipeline_state, int func_id, uint64_t incr)
A runtime tag for a type in the halide type system.
int memcmp(const void *s1, const void *s2, size_t n)
unsigned __INT16_TYPE__ uint16_t
char * buf
Definition: printer.h:30
int fclose(void *)
WEAK void(*)(void *, const char *) halide_set_error_handler(void(*handler)(void *, const char *))
WEAK CpuFeatures halide_get_cpu_features()
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)
void * malloc(size_t)
__SIZE_TYPE__ size_t
WEAK void halide_cond_broadcast(struct halide_cond *cond)
WEAK char * halide_string_to_string(char *dst, char *end, const char *arg)
size_t fwrite(const void *, size_t, size_t, void *)
void * memset(void *s, int val, size_t n)
void abort()
int close(int)
WEAK void halide_sleep_ms(void *user_context, int ms)
The raw representation of an image passed around by generated Halide code.
WEAK void halide_use_jit_module()
WEAK void halide_cond_wait(struct halide_cond *cond, struct halide_mutex *mutex)
unsigned __INT64_TYPE__ uint64_t
WEAK void halide_print(void *user_context, const char *msg)
char * getenv(const char *)
Expr max(FuncRef a, FuncRef b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:419
signed __INT32_TYPE__ int32_t
signed __INT16_TYPE__ int16_t
void free(void *)
char * strncpy(char *dst, const char *src, size_t n)