Halide  12.0.1
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__
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 // --------------
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
61 typedef uint64_t uintptr_t;
62 typedef 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
68 typedef uint32_t uintptr_t;
69 typedef int32_t intptr_t;
70 #endif
71 
72 #define STDOUT_FILENO 1
73 #define STDERR_FILENO 2
74 
75 // Commonly-used extern functions
76 extern "C" {
77 void *halide_malloc(void *user_context, size_t x);
78 void halide_free(void *user_context, void *ptr);
80 WEAK void halide_print(void *user_context, const char *msg);
81 WEAK void halide_error(void *user_context, const char *msg);
82 WEAK void (*halide_set_custom_print(void (*print)(void *, const char *)))(void *, const char *);
83 WEAK void (*halide_set_error_handler(void (*handler)(void *, const char *)))(void *, const char *);
84 
85 char *getenv(const char *);
86 void free(void *);
87 void *malloc(size_t);
88 const char *strstr(const char *, const char *);
89 int atoi(const char *);
90 int strcmp(const char *s, const char *t);
91 int strncmp(const char *s, const char *t, size_t n);
92 size_t strlen(const char *s);
93 const char *strchr(const char *s, int c);
94 void *memcpy(void *s1, const void *s2, size_t n);
95 int memcmp(const void *s1, const void *s2, size_t n);
96 void *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
99 void *fopen(const char *, const char *);
100 int fileno(void *);
101 int fclose(void *);
102 int close(int);
103 size_t fwrite(const void *, size_t, size_t, void *);
104 ssize_t write(int fd, const void *buf, size_t bytes);
105 int remove(const char *pathname);
106 int ioctl(int fd, unsigned long request, ...);
107 char *strncpy(char *dst, const char *src, size_t n);
108 void 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 
118 struct halide_buffer_t;
119 struct halide_type_t;
120 WEAK char *halide_string_to_string(char *dst, char *end, const char *arg);
121 WEAK char *halide_double_to_string(char *dst, char *end, double arg, int scientific);
122 WEAK char *halide_int64_to_string(char *dst, char *end, int64_t arg, int digits);
123 WEAK char *halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits);
124 WEAK char *halide_pointer_to_string(char *dst, char *end, const void *arg);
125 WEAK char *halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg);
126 WEAK 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.
129 WEAK void *halide_get_symbol(const char *name);
130 // Platform specific implementations of dlopen/dlsym.
131 WEAK void *halide_load_library(const char *name);
132 // If lib is nullptr, this call should be equivalent to halide_get_symbol(name).
133 WEAK void *halide_get_library_symbol(void *lib, const char *name);
134 
137 WEAK void halide_sleep_ms(void *user_context, int ms);
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);
148  void *pipeline_state,
149  int func_id,
150  uint64_t incr);
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 
162  const struct halide_device_interface_t *device_interface);
164 
166 
167 struct mxArray;
169  int (*pipeline)(void **args), const halide_filter_metadata_t *metadata,
170  int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs);
171 
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;
183 };
184 
187 
189 
191 
192 } // extern "C"
193 
194 namespace {
195 template<typename T>
196 ALWAYS_INLINE void swap(T &a, T &b) {
197  T t = a;
198  a = b;
199  b = t;
200 }
201 
202 template<typename T>
203 ALWAYS_INLINE T max(const T &a, const T &b) {
204  return a > b ? a : b;
205 }
206 
207 template<typename T>
208 ALWAYS_INLINE T min(const T &a, const T &b) {
209  return a < b ? a : b;
210 }
211 
212 } // namespace
213 
214 // A namespace for runtime modules to store their internal state
215 // in. Should not be for things communicated between runtime modules,
216 // because it's possible for them to be compiled with different c++
217 // name mangling due to mixing and matching target triples.
218 namespace Halide {
219 namespace Runtime {
220 namespace Internal {
221 // Empty
222 }
223 } // namespace Runtime
224 } // namespace Halide
225 using namespace Halide::Runtime::Internal;
226 
227 /** A macro that calls halide_print if the supplied condition is
228  * false, then aborts. Used for unrecoverable errors, or
229  * should-never-happen errors. */
230 #define _halide_stringify(x) #x
231 #define _halide_expand_and_stringify(x) _halide_stringify(x)
232 #define halide_assert(user_context, cond) \
233  do { \
234  if (!(cond)) { \
235  halide_print(user_context, __FILE__ ":" _halide_expand_and_stringify(__LINE__) " Assert failed: " #cond "\n"); \
236  abort(); \
237  } \
238  } while (0)
239 
240 #endif
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:578
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:581
char * buf
Definition: printer.h:32
char * dst
Definition: printer.h:32
void * user_context
Definition: printer.h:33
char * end
Definition: printer.h:32
unsigned __INT64_TYPE__ uint64_t
int remove(const char *pathname)
WEAK char * halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg)
signed __INT64_TYPE__ int64_t
WEAK int halide_host_cpu_count()
int ioctl(int fd, unsigned long request,...)
int fileno(void *)
void * malloc(size_t)
int strcmp(const char *s, const char *t)
#define WEAK_INLINE
const char * strchr(const char *s, int c)
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_get_symbol(const char *name)
WEAK void halide_device_free_as_destructor(void *user_context, void *obj)
WEAK char * halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits)
WEAK void halide_sleep_ms(void *user_context, int ms)
size_t fwrite(const void *, size_t, size_t, void *)
WEAK char * halide_double_to_string(char *dst, char *end, double arg, int scientific)
int fclose(void *)
int atoi(const char *)
WEAK int halide_device_and_host_free(void *user_context, struct halide_buffer_t *buf)
WEAK void halide_release_jit_module()
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)
void * memset(void *s, int val, size_t n)
WEAK char * halide_string_to_string(char *dst, char *end, const char *arg)
__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()
unsigned __INT16_TYPE__ uint16_t
char * getenv(const char *)
WEAK char * halide_type_to_string(char *dst, char *end, const halide_type_t *arg)
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)
const char * strstr(const char *, const char *)
void * halide_malloc(void *user_context, size_t x)
__SIZE_TYPE__ size_t
unsigned __INT32_TYPE__ uint32_t
ptrdiff_t ssize_t
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_get_library_symbol(void *lib, const char *name)
WEAK void halide_error(void *user_context, const char *msg)
WEAK void halide_use_jit_module()
void abort()
WEAK void * halide_load_library(const char *name)
signed __INT8_TYPE__ int8_t
#define WEAK
char * strncpy(char *dst, const char *src, size_t n)
void * memcpy(void *s1, const void *s2, size_t n)
int close(int)
WEAK char * halide_pointer_to_string(char *dst, char *end, const void *arg)
WEAK void(*)(void *, const char *) halide_set_error_handler(void(*handler)(void *, const char *))
void * fopen(const char *, const char *)
void free(void *)
WEAK void halide_device_host_nop_free(void *user_context, void *obj)
WEAK char * halide_int64_to_string(char *dst, char *end, int64_t arg, int digits)
WEAK void halide_profiler_memory_free(void *user_context, void *pipeline_state, int func_id, uint64_t decr)
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.