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 
30 typedef ptrdiff_t ssize_t;
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);
81 WEAK int64_t halide_current_time_ns(void *user_context);
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 
136 WEAK int halide_start_clock(void *user_context);
137 WEAK int64_t halide_current_time_ns(void *user_context);
138 WEAK void halide_sleep_ms(void *user_context, int ms);
139 WEAK void halide_device_free_as_destructor(void *user_context, void *obj);
140 WEAK void halide_device_and_host_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.
145 WEAK void halide_profiler_stack_peak_update(void *user_context,
146                                             void *pipeline_state,
147                                             uint64_t *f_values);
148 WEAK void halide_profiler_memory_allocate(void *user_context,
149                                           void *pipeline_state,
150                                           int func_id,
151                                           uint64_t incr);
152 WEAK void halide_profiler_memory_free(void *user_context,
153                                       void *pipeline_state,
154                                       int func_id,
155                                       uint64_t decr);
156 WEAK int halide_profiler_pipeline_start(void *user_context,
157                                         const char *pipeline_name,
158                                         int num_funcs,
159                                         const uint64_t *func_names);
160 WEAK int halide_host_cpu_count();
161 
162 WEAK int halide_device_and_host_malloc(void *user_context, struct halide_buffer_t *buf,
163                                        const struct halide_device_interface_t *device_interface);
164 WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf);
165 
166 struct halide_filter_metadata_t;
167 
168 struct mxArray;
169 WEAK int halide_matlab_call_pipeline(void *user_context,
170                                      int (*pipeline)(void **args), const halide_filter_metadata_t *metadata,
171                                      int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs);
172 
173 WEAK int halide_trace_helper(void *user_context,
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 
181 struct halide_pseudostack_slot_t {
182     void *ptr;
183     size_t size;
184 };
185 
186 WEAK void halide_use_jit_module();
187 WEAK void halide_release_jit_module();
188 
189 WEAK_INLINE int halide_malloc_alignment();
190 WEAK_INLINE void halide_abort();
191 
192 void halide_thread_yield();
193 
194 }  // extern "C"
195 
196 namespace {
197 template<typename T>
swap(T & a,T & b)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>
max(const T & a,const T & b)205 ALWAYS_INLINE T max(const T &a, const T &b) {
206     return a > b ? a : b;
207 }
208 
209 template<typename T>
min(const T & a,const T & b)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>
reinterpret(const U & x)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     if (!(cond)) {                                                                                                     \
242         halide_print(user_context, __FILE__ ":" _halide_expand_and_stringify(__LINE__) " Assert failed: " #cond "\n"); \
243         halide_abort();                                                                                                \
244     }
245 
246 #endif
247