1 #include "HalideRuntime.h"
2 #include "printer.h"
3 
4 extern "C" {
5 
halide_error_bounds_inference_call_failed(void * user_context,const char * extern_stage_name,int result)6 WEAK int halide_error_bounds_inference_call_failed(void *user_context, const char *extern_stage_name, int result) {
7     error(user_context)
8         << "Bounds inference call to external stage " << extern_stage_name
9         << " returned non-zero value: " << result;
10     return result;
11 }
12 
halide_error_extern_stage_failed(void * user_context,const char * extern_stage_name,int result)13 WEAK int halide_error_extern_stage_failed(void *user_context, const char *extern_stage_name, int result) {
14     error(user_context)
15         << "Call to external stage " << extern_stage_name
16         << " returned non-zero value: " << result;
17     return result;
18 }
19 
halide_error_explicit_bounds_too_small(void * user_context,const char * func_name,const char * var_name,int min_bound,int max_bound,int min_required,int max_required)20 WEAK int halide_error_explicit_bounds_too_small(void *user_context, const char *func_name, const char *var_name,
21                                                 int min_bound, int max_bound, int min_required, int max_required) {
22     error(user_context)
23         << "Bounds given for " << var_name << " in " << func_name
24         << " (from " << min_bound << " to " << max_bound
25         << ") do not cover required region (from " << min_required
26         << " to " << max_required << ")";
27     return halide_error_code_explicit_bounds_too_small;
28 }
29 
halide_error_bad_type(void * user_context,const char * func_name,uint32_t type_given_bits,uint32_t correct_type_bits)30 WEAK int halide_error_bad_type(void *user_context, const char *func_name,
31                                uint32_t type_given_bits, uint32_t correct_type_bits) {
32     halide_type_t correct_type, type_given;
33     memcpy(&correct_type, &correct_type_bits, sizeof(uint32_t));
34     memcpy(&type_given, &type_given_bits, sizeof(uint32_t));
35     error(user_context)
36         << func_name << " has type " << correct_type
37         << " but type of the buffer passed in is " << type_given;
38     return halide_error_code_bad_type;
39 }
40 
halide_error_bad_dimensions(void * user_context,const char * func_name,int32_t dimensions_given,int32_t correct_dimensions)41 WEAK int halide_error_bad_dimensions(void *user_context, const char *func_name,
42                                      int32_t dimensions_given, int32_t correct_dimensions) {
43     error(user_context)
44         << func_name << " requires a buffer of exactly " << correct_dimensions
45         << " dimensions, but the buffer passed in has " << dimensions_given << " dimensions";
46     return halide_error_code_bad_dimensions;
47 }
48 
halide_error_access_out_of_bounds(void * user_context,const char * func_name,int dimension,int min_touched,int max_touched,int min_valid,int max_valid)49 WEAK int halide_error_access_out_of_bounds(void *user_context, const char *func_name,
50                                            int dimension, int min_touched, int max_touched,
51                                            int min_valid, int max_valid) {
52     if (min_touched < min_valid) {
53         error(user_context)
54             << func_name << " is accessed at " << min_touched
55             << ", which is before the min (" << min_valid
56             << ") in dimension " << dimension;
57     } else if (max_touched > max_valid) {
58         error(user_context)
59             << func_name << " is accessed at " << max_touched
60             << ", which is beyond the max (" << max_valid
61             << ") in dimension " << dimension;
62     }
63     return halide_error_code_access_out_of_bounds;
64 }
65 
halide_error_buffer_allocation_too_large(void * user_context,const char * buffer_name,uint64_t allocation_size,uint64_t max_size)66 WEAK int halide_error_buffer_allocation_too_large(void *user_context, const char *buffer_name, uint64_t allocation_size, uint64_t max_size) {
67     error(user_context)
68         << "Total allocation for buffer " << buffer_name
69         << " is " << allocation_size
70         << ", which exceeds the maximum size of " << max_size;
71     return halide_error_code_buffer_allocation_too_large;
72 }
73 
halide_error_buffer_extents_negative(void * user_context,const char * buffer_name,int dimension,int extent)74 WEAK int halide_error_buffer_extents_negative(void *user_context, const char *buffer_name, int dimension, int extent) {
75     error(user_context)
76         << "The extents for buffer " << buffer_name
77         << " dimension " << dimension
78         << " is negative (" << extent << ")";
79     return halide_error_code_buffer_extents_negative;
80 }
81 
halide_error_buffer_extents_too_large(void * user_context,const char * buffer_name,int64_t actual_size,int64_t max_size)82 WEAK int halide_error_buffer_extents_too_large(void *user_context, const char *buffer_name, int64_t actual_size, int64_t max_size) {
83     error(user_context)
84         << "Product of extents for buffer " << buffer_name
85         << " is " << actual_size
86         << ", which exceeds the maximum size of " << max_size;
87     return halide_error_code_buffer_extents_too_large;
88 }
89 
halide_error_constraints_make_required_region_smaller(void * user_context,const char * buffer_name,int dimension,int constrained_min,int constrained_extent,int required_min,int required_extent)90 WEAK int halide_error_constraints_make_required_region_smaller(void *user_context, const char *buffer_name,
91                                                                int dimension,
92                                                                int constrained_min, int constrained_extent,
93                                                                int required_min, int required_extent) {
94     int required_max = required_min + required_extent - 1;
95     int constrained_max = constrained_min + constrained_extent - 1;
96     error(user_context)
97         << "Applying the constraints on " << buffer_name
98         << " to the required region made it smaller in dimension " << dimension << ". "
99         << "Required size: " << required_min << " to " << required_max << ". "
100         << "Constrained size: " << constrained_min << " to " << constrained_max << ".";
101     return halide_error_code_constraints_make_required_region_smaller;
102 }
103 
halide_error_constraint_violated(void * user_context,const char * var,int val,const char * constrained_var,int constrained_val)104 WEAK int halide_error_constraint_violated(void *user_context, const char *var, int val,
105                                           const char *constrained_var, int constrained_val) {
106     error(user_context)
107         << "Constraint violated: " << var << " (" << val
108         << ") == " << constrained_var << " (" << constrained_val << ")";
109     return halide_error_code_constraint_violated;
110 }
111 
halide_error_param_too_small_i64(void * user_context,const char * param_name,int64_t val,int64_t min_val)112 WEAK int halide_error_param_too_small_i64(void *user_context, const char *param_name,
113                                           int64_t val, int64_t min_val) {
114     error(user_context)
115         << "Parameter " << param_name
116         << " is " << val
117         << " but must be at least " << min_val;
118     return halide_error_code_param_too_small;
119 }
120 
halide_error_param_too_small_u64(void * user_context,const char * param_name,uint64_t val,uint64_t min_val)121 WEAK int halide_error_param_too_small_u64(void *user_context, const char *param_name,
122                                           uint64_t val, uint64_t min_val) {
123     error(user_context)
124         << "Parameter " << param_name
125         << " is " << val
126         << " but must be at least " << min_val;
127     return halide_error_code_param_too_small;
128 }
129 
halide_error_param_too_small_f64(void * user_context,const char * param_name,double val,double min_val)130 WEAK int halide_error_param_too_small_f64(void *user_context, const char *param_name,
131                                           double val, double min_val) {
132     error(user_context)
133         << "Parameter " << param_name
134         << " is " << val
135         << " but must be at least " << min_val;
136     return halide_error_code_param_too_small;
137 }
138 
halide_error_param_too_large_i64(void * user_context,const char * param_name,int64_t val,int64_t max_val)139 WEAK int halide_error_param_too_large_i64(void *user_context, const char *param_name,
140                                           int64_t val, int64_t max_val) {
141     error(user_context)
142         << "Parameter " << param_name
143         << " is " << val
144         << " but must be at most " << max_val;
145     return halide_error_code_param_too_large;
146 }
147 
halide_error_param_too_large_u64(void * user_context,const char * param_name,uint64_t val,uint64_t max_val)148 WEAK int halide_error_param_too_large_u64(void *user_context, const char *param_name,
149                                           uint64_t val, uint64_t max_val) {
150     error(user_context)
151         << "Parameter " << param_name
152         << " is " << val
153         << " but must be at most " << max_val;
154     return halide_error_code_param_too_large;
155 }
156 
halide_error_param_too_large_f64(void * user_context,const char * param_name,double val,double max_val)157 WEAK int halide_error_param_too_large_f64(void *user_context, const char *param_name,
158                                           double val, double max_val) {
159     error(user_context)
160         << "Parameter " << param_name
161         << " is " << val
162         << " but must be at most " << max_val;
163     return halide_error_code_param_too_large;
164 }
165 
halide_error_out_of_memory(void * user_context)166 WEAK int halide_error_out_of_memory(void *user_context) {
167     // The error message builder uses malloc, so we can't use it here.
168     halide_error(user_context, "Out of memory (halide_malloc returned NULL)");
169     return halide_error_code_out_of_memory;
170 }
171 
halide_error_buffer_argument_is_null(void * user_context,const char * buffer_name)172 WEAK int halide_error_buffer_argument_is_null(void *user_context, const char *buffer_name) {
173     error(user_context)
174         << "Buffer argument " << buffer_name << " is NULL";
175     return halide_error_code_buffer_argument_is_null;
176 }
177 
halide_error_debug_to_file_failed(void * user_context,const char * func,const char * filename,int error_code)178 WEAK int halide_error_debug_to_file_failed(void *user_context, const char *func,
179                                            const char *filename, int error_code) {
180     error(user_context)
181         << "Failed to dump function " << func
182         << " to file " << filename
183         << " with error " << error_code;
184     return halide_error_code_debug_to_file_failed;
185 }
186 
halide_error_unaligned_host_ptr(void * user_context,const char * func,int alignment)187 WEAK int halide_error_unaligned_host_ptr(void *user_context, const char *func,
188                                          int alignment) {
189     error(user_context)
190         << "The host pointer of " << func
191         << " is not aligned to a " << alignment
192         << " bytes boundary.";
193     return halide_error_code_unaligned_host_ptr;
194 }
195 
halide_error_device_dirty_with_no_device_support(void * user_context,const char * func)196 WEAK int halide_error_device_dirty_with_no_device_support(void *user_context, const char *func) {
197     error(user_context)
198         << "The buffer " << func
199         << " is dirty on device, but this pipeline was compiled "
200         << "with no support for device to host copies.";
201     return halide_error_code_device_dirty_with_no_device_support;
202 }
203 
halide_error_host_is_null(void * user_context,const char * func)204 WEAK int halide_error_host_is_null(void *user_context, const char *func) {
205     error(user_context)
206         << "The host pointer of " << func
207         << " is null, but the pipeline will access it on the host.";
208     return halide_error_code_host_is_null;
209 }
210 
halide_error_bad_fold(void * user_context,const char * func_name,const char * var_name,const char * loop_name)211 WEAK int halide_error_bad_fold(void *user_context, const char *func_name, const char *var_name,
212                                const char *loop_name) {
213     error(user_context)
214         << "The folded storage dimension " << var_name << " of " << func_name
215         << " was accessed out of order by loop " << loop_name << ".";
216     return halide_error_code_bad_fold;
217 }
218 
halide_error_bad_extern_fold(void * user_context,const char * func_name,int dim,int min,int extent,int valid_min,int fold_factor)219 WEAK int halide_error_bad_extern_fold(void *user_context, const char *func_name,
220                                       int dim, int min, int extent, int valid_min, int fold_factor) {
221     if (min < valid_min || min + extent > valid_min + fold_factor) {
222         error(user_context)
223             << "Cannot fold dimension " << dim << " of " << func_name
224             << " because an extern stage accesses [" << min << ", " << (min + extent - 1) << "],"
225             << " which is outside the range currently valid: ["
226             << valid_min << ", " << (valid_min + fold_factor - 1) << "].";
227     } else {
228         error(user_context)
229             << "Cannot fold dimension " << dim << " of " << func_name
230             << " because an extern stage accesses [" << min << ", " << (min + extent - 1) << "],"
231             << " which wraps around the boundary of the fold, "
232             << "which occurs at multiples of " << fold_factor << ".";
233     }
234     return halide_error_code_bad_extern_fold;
235 }
236 
halide_error_fold_factor_too_small(void * user_context,const char * func_name,const char * var_name,int fold_factor,const char * loop_name,int required_extent)237 WEAK int halide_error_fold_factor_too_small(void *user_context, const char *func_name, const char *var_name,
238                                             int fold_factor, const char *loop_name, int required_extent) {
239     error(user_context)
240         << "The fold factor (" << fold_factor
241         << ") of dimension " << var_name << " of " << func_name
242         << " is too small to store the required region accessed by loop "
243         << loop_name << " (" << required_extent << ").";
244     return halide_error_code_fold_factor_too_small;
245 }
246 
halide_error_requirement_failed(void * user_context,const char * condition,const char * message)247 WEAK int halide_error_requirement_failed(void *user_context, const char *condition, const char *message) {
248     error(user_context)
249         << "Requirement Failed: (" << condition << ") " << message;
250     return halide_error_code_requirement_failed;
251 }
252 
halide_error_specialize_fail(void * user_context,const char * message)253 WEAK int halide_error_specialize_fail(void *user_context, const char *message) {
254     error(user_context)
255         << "A schedule specialized with specialize_fail() was chosen: " << message;
256     return halide_error_code_specialize_fail;
257 }
258 
halide_error_no_device_interface(void * user_context)259 WEAK int halide_error_no_device_interface(void *user_context) {
260     error(user_context) << "Buffer has a non-zero device but no device interface.\n";
261     return halide_error_code_no_device_interface;
262 }
263 
halide_error_device_interface_no_device(void * user_context)264 WEAK int halide_error_device_interface_no_device(void *user_context) {
265     error(user_context) << "Buffer has a non-null device_interface but device is 0.\n";
266     return halide_error_code_device_interface_no_device;
267 }
268 
halide_error_host_and_device_dirty(void * user_context)269 WEAK int halide_error_host_and_device_dirty(void *user_context) {
270     error(user_context) << "Buffer has both host and device dirty bits set.\n";
271     return halide_error_code_host_and_device_dirty;
272 }
273 
halide_error_buffer_is_null(void * user_context,const char * routine)274 WEAK int halide_error_buffer_is_null(void *user_context, const char *routine) {
275     error(user_context) << "Buffer pointer passed to " << routine << " is null.\n";
276     return halide_error_code_buffer_is_null;
277 }
278 
279 }  // extern "C"
280