interface/extract_interface.cc: add missing include
[isl.git] / isl_test.c
blob1eda6e0789a3ec47c2dbaad54bd677547f1752e9
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl/vertices.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
44 static char *srcdir;
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
195 int test_parse(struct isl_ctx *ctx)
197 int i;
198 isl_map *map, *map2;
199 const char *str, *str2;
201 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
202 return -1;
203 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
206 return -1;
208 str = "{ [i] -> [-i] }";
209 map = isl_map_read_from_str(ctx, str);
210 assert(map);
211 isl_map_free(map);
213 str = "{ A[i] -> L[([i/3])] }";
214 map = isl_map_read_from_str(ctx, str);
215 assert(map);
216 isl_map_free(map);
218 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
219 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
220 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
222 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
223 str = parse_map_equal_tests[i].map1;
224 str2 = parse_map_equal_tests[i].map2;
225 if (test_parse_map_equal(ctx, str, str2) < 0)
226 return -1;
229 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
230 map = isl_map_read_from_str(ctx, str);
231 str = "{ [new, old] -> [o0, o1] : "
232 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
233 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
234 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
235 map2 = isl_map_read_from_str(ctx, str);
236 assert(isl_map_is_equal(map, map2));
237 isl_map_free(map);
238 isl_map_free(map2);
240 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
241 map = isl_map_read_from_str(ctx, str);
242 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
243 map2 = isl_map_read_from_str(ctx, str);
244 assert(isl_map_is_equal(map, map2));
245 isl_map_free(map);
246 isl_map_free(map2);
248 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
249 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
250 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
251 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
253 return 0;
256 static int test_read(isl_ctx *ctx)
258 char *filename;
259 FILE *input;
260 isl_basic_set *bset1, *bset2;
261 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
262 int equal;
264 filename = get_filename(ctx, "set", "omega");
265 assert(filename);
266 input = fopen(filename, "r");
267 assert(input);
269 bset1 = isl_basic_set_read_from_file(ctx, input);
270 bset2 = isl_basic_set_read_from_str(ctx, str);
272 equal = isl_basic_set_is_equal(bset1, bset2);
274 isl_basic_set_free(bset1);
275 isl_basic_set_free(bset2);
276 free(filename);
278 fclose(input);
280 if (equal < 0)
281 return -1;
282 if (!equal)
283 isl_die(ctx, isl_error_unknown,
284 "read sets not equal", return -1);
286 return 0;
289 static int test_bounded(isl_ctx *ctx)
291 isl_set *set;
292 int bounded;
294 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
295 bounded = isl_set_is_bounded(set);
296 isl_set_free(set);
298 if (bounded < 0)
299 return -1;
300 if (!bounded)
301 isl_die(ctx, isl_error_unknown,
302 "set not considered bounded", return -1);
304 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
305 bounded = isl_set_is_bounded(set);
306 assert(!bounded);
307 isl_set_free(set);
309 if (bounded < 0)
310 return -1;
311 if (bounded)
312 isl_die(ctx, isl_error_unknown,
313 "set considered bounded", return -1);
315 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
316 bounded = isl_set_is_bounded(set);
317 isl_set_free(set);
319 if (bounded < 0)
320 return -1;
321 if (bounded)
322 isl_die(ctx, isl_error_unknown,
323 "set considered bounded", return -1);
325 return 0;
328 /* Construct the basic set { [i] : 5 <= i <= N } */
329 static int test_construction(isl_ctx *ctx)
331 isl_int v;
332 isl_space *dim;
333 isl_local_space *ls;
334 isl_basic_set *bset;
335 isl_constraint *c;
337 isl_int_init(v);
339 dim = isl_space_set_alloc(ctx, 1, 1);
340 bset = isl_basic_set_universe(isl_space_copy(dim));
341 ls = isl_local_space_from_space(dim);
343 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
344 isl_int_set_si(v, -1);
345 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346 isl_int_set_si(v, 1);
347 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
348 bset = isl_basic_set_add_constraint(bset, c);
350 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
351 isl_int_set_si(v, 1);
352 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
353 isl_int_set_si(v, -5);
354 isl_constraint_set_constant(c, v);
355 bset = isl_basic_set_add_constraint(bset, c);
357 isl_local_space_free(ls);
358 isl_basic_set_free(bset);
360 isl_int_clear(v);
362 return 0;
365 static int test_dim(isl_ctx *ctx)
367 const char *str;
368 isl_map *map1, *map2;
369 int equal;
371 map1 = isl_map_read_from_str(ctx,
372 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
373 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
374 map2 = isl_map_read_from_str(ctx,
375 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
376 equal = isl_map_is_equal(map1, map2);
377 isl_map_free(map2);
379 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
380 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
381 if (equal >= 0 && equal)
382 equal = isl_map_is_equal(map1, map2);
384 isl_map_free(map1);
385 isl_map_free(map2);
387 if (equal < 0)
388 return -1;
389 if (!equal)
390 isl_die(ctx, isl_error_unknown,
391 "unexpected result", return -1);
393 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
394 map1 = isl_map_read_from_str(ctx, str);
395 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
396 map2 = isl_map_read_from_str(ctx, str);
397 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
398 equal = isl_map_is_equal(map1, map2);
399 isl_map_free(map1);
400 isl_map_free(map2);
402 if (equal < 0)
403 return -1;
404 if (!equal)
405 isl_die(ctx, isl_error_unknown,
406 "unexpected result", return -1);
408 return 0;
411 struct {
412 __isl_give isl_val *(*op)(__isl_take isl_val *v);
413 const char *arg;
414 const char *res;
415 } val_un_tests[] = {
416 { &isl_val_neg, "0", "0" },
417 { &isl_val_abs, "0", "0" },
418 { &isl_val_2exp, "0", "1" },
419 { &isl_val_floor, "0", "0" },
420 { &isl_val_ceil, "0", "0" },
421 { &isl_val_neg, "1", "-1" },
422 { &isl_val_neg, "-1", "1" },
423 { &isl_val_neg, "1/2", "-1/2" },
424 { &isl_val_neg, "-1/2", "1/2" },
425 { &isl_val_neg, "infty", "-infty" },
426 { &isl_val_neg, "-infty", "infty" },
427 { &isl_val_neg, "NaN", "NaN" },
428 { &isl_val_abs, "1", "1" },
429 { &isl_val_abs, "-1", "1" },
430 { &isl_val_abs, "1/2", "1/2" },
431 { &isl_val_abs, "-1/2", "1/2" },
432 { &isl_val_abs, "infty", "infty" },
433 { &isl_val_abs, "-infty", "infty" },
434 { &isl_val_abs, "NaN", "NaN" },
435 { &isl_val_floor, "1", "1" },
436 { &isl_val_floor, "-1", "-1" },
437 { &isl_val_floor, "1/2", "0" },
438 { &isl_val_floor, "-1/2", "-1" },
439 { &isl_val_floor, "infty", "infty" },
440 { &isl_val_floor, "-infty", "-infty" },
441 { &isl_val_floor, "NaN", "NaN" },
442 { &isl_val_ceil, "1", "1" },
443 { &isl_val_ceil, "-1", "-1" },
444 { &isl_val_ceil, "1/2", "1" },
445 { &isl_val_ceil, "-1/2", "0" },
446 { &isl_val_ceil, "infty", "infty" },
447 { &isl_val_ceil, "-infty", "-infty" },
448 { &isl_val_ceil, "NaN", "NaN" },
449 { &isl_val_2exp, "-3", "1/8" },
450 { &isl_val_2exp, "-1", "1/2" },
451 { &isl_val_2exp, "1", "2" },
452 { &isl_val_2exp, "2", "4" },
453 { &isl_val_2exp, "3", "8" },
454 { &isl_val_inv, "1", "1" },
455 { &isl_val_inv, "2", "1/2" },
456 { &isl_val_inv, "1/2", "2" },
457 { &isl_val_inv, "-2", "-1/2" },
458 { &isl_val_inv, "-1/2", "-2" },
459 { &isl_val_inv, "0", "NaN" },
460 { &isl_val_inv, "NaN", "NaN" },
461 { &isl_val_inv, "infty", "0" },
462 { &isl_val_inv, "-infty", "0" },
465 /* Perform some basic tests of unary operations on isl_val objects.
467 static int test_un_val(isl_ctx *ctx)
469 int i;
470 isl_val *v, *res;
471 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
472 int ok;
474 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
475 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
476 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
477 fn = val_un_tests[i].op;
478 v = fn(v);
479 if (isl_val_is_nan(res))
480 ok = isl_val_is_nan(v);
481 else
482 ok = isl_val_eq(v, res);
483 isl_val_free(v);
484 isl_val_free(res);
485 if (ok < 0)
486 return -1;
487 if (!ok)
488 isl_die(ctx, isl_error_unknown,
489 "unexpected result", return -1);
492 return 0;
495 struct {
496 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
497 __isl_take isl_val *v2);
498 } val_bin_op[] = {
499 ['+'] = { &isl_val_add },
500 ['-'] = { &isl_val_sub },
501 ['*'] = { &isl_val_mul },
502 ['/'] = { &isl_val_div },
503 ['g'] = { &isl_val_gcd },
504 ['m'] = { &isl_val_min },
505 ['M'] = { &isl_val_max },
508 struct {
509 const char *arg1;
510 unsigned char op;
511 const char *arg2;
512 const char *res;
513 } val_bin_tests[] = {
514 { "0", '+', "0", "0" },
515 { "1", '+', "0", "1" },
516 { "1", '+', "1", "2" },
517 { "1", '-', "1", "0" },
518 { "1", '*', "1", "1" },
519 { "1", '/', "1", "1" },
520 { "2", '*', "3", "6" },
521 { "2", '*', "1/2", "1" },
522 { "2", '*', "1/3", "2/3" },
523 { "2/3", '*', "3/5", "2/5" },
524 { "2/3", '*', "7/5", "14/15" },
525 { "2", '/', "1/2", "4" },
526 { "-2", '/', "-1/2", "4" },
527 { "-2", '/', "1/2", "-4" },
528 { "2", '/', "-1/2", "-4" },
529 { "2", '/', "2", "1" },
530 { "2", '/', "3", "2/3" },
531 { "2/3", '/', "5/3", "2/5" },
532 { "2/3", '/', "5/7", "14/15" },
533 { "0", '/', "0", "NaN" },
534 { "42", '/', "0", "NaN" },
535 { "-42", '/', "0", "NaN" },
536 { "infty", '/', "0", "NaN" },
537 { "-infty", '/', "0", "NaN" },
538 { "NaN", '/', "0", "NaN" },
539 { "0", '/', "NaN", "NaN" },
540 { "42", '/', "NaN", "NaN" },
541 { "-42", '/', "NaN", "NaN" },
542 { "infty", '/', "NaN", "NaN" },
543 { "-infty", '/', "NaN", "NaN" },
544 { "NaN", '/', "NaN", "NaN" },
545 { "0", '/', "infty", "0" },
546 { "42", '/', "infty", "0" },
547 { "-42", '/', "infty", "0" },
548 { "infty", '/', "infty", "NaN" },
549 { "-infty", '/', "infty", "NaN" },
550 { "NaN", '/', "infty", "NaN" },
551 { "0", '/', "-infty", "0" },
552 { "42", '/', "-infty", "0" },
553 { "-42", '/', "-infty", "0" },
554 { "infty", '/', "-infty", "NaN" },
555 { "-infty", '/', "-infty", "NaN" },
556 { "NaN", '/', "-infty", "NaN" },
557 { "1", '-', "1/3", "2/3" },
558 { "1/3", '+', "1/2", "5/6" },
559 { "1/2", '+', "1/2", "1" },
560 { "3/4", '-', "1/4", "1/2" },
561 { "1/2", '-', "1/3", "1/6" },
562 { "infty", '+', "42", "infty" },
563 { "infty", '+', "infty", "infty" },
564 { "42", '+', "infty", "infty" },
565 { "infty", '-', "infty", "NaN" },
566 { "infty", '*', "infty", "infty" },
567 { "infty", '*', "-infty", "-infty" },
568 { "-infty", '*', "infty", "-infty" },
569 { "-infty", '*', "-infty", "infty" },
570 { "0", '*', "infty", "NaN" },
571 { "1", '*', "infty", "infty" },
572 { "infty", '*', "0", "NaN" },
573 { "infty", '*', "42", "infty" },
574 { "42", '-', "infty", "-infty" },
575 { "infty", '+', "-infty", "NaN" },
576 { "4", 'g', "6", "2" },
577 { "5", 'g', "6", "1" },
578 { "42", 'm', "3", "3" },
579 { "42", 'M', "3", "42" },
580 { "3", 'm', "42", "3" },
581 { "3", 'M', "42", "42" },
582 { "42", 'm', "infty", "42" },
583 { "42", 'M', "infty", "infty" },
584 { "42", 'm', "-infty", "-infty" },
585 { "42", 'M', "-infty", "42" },
586 { "42", 'm', "NaN", "NaN" },
587 { "42", 'M', "NaN", "NaN" },
588 { "infty", 'm', "-infty", "-infty" },
589 { "infty", 'M', "-infty", "infty" },
592 /* Perform some basic tests of binary operations on isl_val objects.
594 static int test_bin_val(isl_ctx *ctx)
596 int i;
597 isl_val *v1, *v2, *res;
598 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
599 __isl_take isl_val *v2);
600 int ok;
602 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
603 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
604 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
605 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
606 fn = val_bin_op[val_bin_tests[i].op].fn;
607 v1 = fn(v1, v2);
608 if (isl_val_is_nan(res))
609 ok = isl_val_is_nan(v1);
610 else
611 ok = isl_val_eq(v1, res);
612 isl_val_free(v1);
613 isl_val_free(res);
614 if (ok < 0)
615 return -1;
616 if (!ok)
617 isl_die(ctx, isl_error_unknown,
618 "unexpected result", return -1);
621 return 0;
624 /* Perform some basic tests on isl_val objects.
626 static int test_val(isl_ctx *ctx)
628 if (test_un_val(ctx) < 0)
629 return -1;
630 if (test_bin_val(ctx) < 0)
631 return -1;
632 return 0;
635 /* Sets described using existentially quantified variables that
636 * can also be described without.
638 static const char *elimination_tests[] = {
639 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
640 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
641 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
644 /* Check that redundant existentially quantified variables are
645 * getting removed.
647 static int test_elimination(isl_ctx *ctx)
649 int i;
650 unsigned n;
651 isl_basic_set *bset;
653 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
654 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
655 n = isl_basic_set_dim(bset, isl_dim_div);
656 isl_basic_set_free(bset);
657 if (!bset)
658 return -1;
659 if (n != 0)
660 isl_die(ctx, isl_error_unknown,
661 "expecting no existentials", return -1);
664 return 0;
667 static int test_div(isl_ctx *ctx)
669 const char *str;
670 int empty;
671 isl_int v;
672 isl_space *dim;
673 isl_set *set;
674 isl_local_space *ls;
675 struct isl_basic_set *bset;
676 struct isl_constraint *c;
678 isl_int_init(v);
680 /* test 1 */
681 dim = isl_space_set_alloc(ctx, 0, 3);
682 bset = isl_basic_set_universe(isl_space_copy(dim));
683 ls = isl_local_space_from_space(dim);
685 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
686 isl_int_set_si(v, -1);
687 isl_constraint_set_constant(c, v);
688 isl_int_set_si(v, 1);
689 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
690 isl_int_set_si(v, 3);
691 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
692 bset = isl_basic_set_add_constraint(bset, c);
694 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
695 isl_int_set_si(v, 1);
696 isl_constraint_set_constant(c, v);
697 isl_int_set_si(v, -1);
698 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
699 isl_int_set_si(v, 3);
700 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
701 bset = isl_basic_set_add_constraint(bset, c);
703 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
705 assert(bset && bset->n_div == 1);
706 isl_local_space_free(ls);
707 isl_basic_set_free(bset);
709 /* test 2 */
710 dim = isl_space_set_alloc(ctx, 0, 3);
711 bset = isl_basic_set_universe(isl_space_copy(dim));
712 ls = isl_local_space_from_space(dim);
714 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
715 isl_int_set_si(v, 1);
716 isl_constraint_set_constant(c, v);
717 isl_int_set_si(v, -1);
718 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
719 isl_int_set_si(v, 3);
720 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
721 bset = isl_basic_set_add_constraint(bset, c);
723 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
724 isl_int_set_si(v, -1);
725 isl_constraint_set_constant(c, v);
726 isl_int_set_si(v, 1);
727 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
728 isl_int_set_si(v, 3);
729 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
730 bset = isl_basic_set_add_constraint(bset, c);
732 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
734 assert(bset && bset->n_div == 1);
735 isl_local_space_free(ls);
736 isl_basic_set_free(bset);
738 /* test 3 */
739 dim = isl_space_set_alloc(ctx, 0, 3);
740 bset = isl_basic_set_universe(isl_space_copy(dim));
741 ls = isl_local_space_from_space(dim);
743 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
744 isl_int_set_si(v, 1);
745 isl_constraint_set_constant(c, v);
746 isl_int_set_si(v, -1);
747 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
748 isl_int_set_si(v, 3);
749 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
750 bset = isl_basic_set_add_constraint(bset, c);
752 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
753 isl_int_set_si(v, -3);
754 isl_constraint_set_constant(c, v);
755 isl_int_set_si(v, 1);
756 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
757 isl_int_set_si(v, 4);
758 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
759 bset = isl_basic_set_add_constraint(bset, c);
761 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
763 assert(bset && bset->n_div == 1);
764 isl_local_space_free(ls);
765 isl_basic_set_free(bset);
767 /* test 4 */
768 dim = isl_space_set_alloc(ctx, 0, 3);
769 bset = isl_basic_set_universe(isl_space_copy(dim));
770 ls = isl_local_space_from_space(dim);
772 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
773 isl_int_set_si(v, 2);
774 isl_constraint_set_constant(c, v);
775 isl_int_set_si(v, -1);
776 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
777 isl_int_set_si(v, 3);
778 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
779 bset = isl_basic_set_add_constraint(bset, c);
781 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
782 isl_int_set_si(v, -1);
783 isl_constraint_set_constant(c, v);
784 isl_int_set_si(v, 1);
785 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
786 isl_int_set_si(v, 6);
787 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
788 bset = isl_basic_set_add_constraint(bset, c);
790 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
792 assert(isl_basic_set_is_empty(bset));
793 isl_local_space_free(ls);
794 isl_basic_set_free(bset);
796 /* test 5 */
797 dim = isl_space_set_alloc(ctx, 0, 3);
798 bset = isl_basic_set_universe(isl_space_copy(dim));
799 ls = isl_local_space_from_space(dim);
801 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
802 isl_int_set_si(v, -1);
803 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
804 isl_int_set_si(v, 3);
805 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
806 bset = isl_basic_set_add_constraint(bset, c);
808 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
809 isl_int_set_si(v, 1);
810 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
811 isl_int_set_si(v, -3);
812 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
813 bset = isl_basic_set_add_constraint(bset, c);
815 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
817 assert(bset && bset->n_div == 0);
818 isl_basic_set_free(bset);
819 isl_local_space_free(ls);
821 /* test 6 */
822 dim = isl_space_set_alloc(ctx, 0, 3);
823 bset = isl_basic_set_universe(isl_space_copy(dim));
824 ls = isl_local_space_from_space(dim);
826 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
827 isl_int_set_si(v, -1);
828 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
829 isl_int_set_si(v, 6);
830 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
831 bset = isl_basic_set_add_constraint(bset, c);
833 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
834 isl_int_set_si(v, 1);
835 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
836 isl_int_set_si(v, -3);
837 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
838 bset = isl_basic_set_add_constraint(bset, c);
840 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
842 assert(bset && bset->n_div == 1);
843 isl_basic_set_free(bset);
844 isl_local_space_free(ls);
846 /* test 7 */
847 /* This test is a bit tricky. We set up an equality
848 * a + 3b + 3c = 6 e0
849 * Normalization of divs creates _two_ divs
850 * a = 3 e0
851 * c - b - e0 = 2 e1
852 * Afterwards e0 is removed again because it has coefficient -1
853 * and we end up with the original equality and div again.
854 * Perhaps we can avoid the introduction of this temporary div.
856 dim = isl_space_set_alloc(ctx, 0, 4);
857 bset = isl_basic_set_universe(isl_space_copy(dim));
858 ls = isl_local_space_from_space(dim);
860 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
861 isl_int_set_si(v, -1);
862 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
863 isl_int_set_si(v, -3);
864 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
865 isl_int_set_si(v, -3);
866 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
867 isl_int_set_si(v, 6);
868 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
869 bset = isl_basic_set_add_constraint(bset, c);
871 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
873 /* Test disabled for now */
875 assert(bset && bset->n_div == 1);
877 isl_local_space_free(ls);
878 isl_basic_set_free(bset);
880 /* test 8 */
881 dim = isl_space_set_alloc(ctx, 0, 5);
882 bset = isl_basic_set_universe(isl_space_copy(dim));
883 ls = isl_local_space_from_space(dim);
885 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
886 isl_int_set_si(v, -1);
887 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
888 isl_int_set_si(v, -3);
889 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
890 isl_int_set_si(v, -3);
891 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
892 isl_int_set_si(v, 6);
893 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
894 bset = isl_basic_set_add_constraint(bset, c);
896 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
897 isl_int_set_si(v, -1);
898 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
899 isl_int_set_si(v, 1);
900 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
901 isl_int_set_si(v, 1);
902 isl_constraint_set_constant(c, v);
903 bset = isl_basic_set_add_constraint(bset, c);
905 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
907 /* Test disabled for now */
909 assert(bset && bset->n_div == 1);
911 isl_local_space_free(ls);
912 isl_basic_set_free(bset);
914 /* test 9 */
915 dim = isl_space_set_alloc(ctx, 0, 4);
916 bset = isl_basic_set_universe(isl_space_copy(dim));
917 ls = isl_local_space_from_space(dim);
919 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
920 isl_int_set_si(v, 1);
921 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
922 isl_int_set_si(v, -1);
923 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
924 isl_int_set_si(v, -2);
925 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
926 bset = isl_basic_set_add_constraint(bset, c);
928 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
929 isl_int_set_si(v, -1);
930 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
931 isl_int_set_si(v, 3);
932 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
933 isl_int_set_si(v, 2);
934 isl_constraint_set_constant(c, v);
935 bset = isl_basic_set_add_constraint(bset, c);
937 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
939 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
941 assert(!isl_basic_set_is_empty(bset));
943 isl_local_space_free(ls);
944 isl_basic_set_free(bset);
946 /* test 10 */
947 dim = isl_space_set_alloc(ctx, 0, 3);
948 bset = isl_basic_set_universe(isl_space_copy(dim));
949 ls = isl_local_space_from_space(dim);
951 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
952 isl_int_set_si(v, 1);
953 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
954 isl_int_set_si(v, -2);
955 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
956 bset = isl_basic_set_add_constraint(bset, c);
958 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
960 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
962 isl_local_space_free(ls);
963 isl_basic_set_free(bset);
965 isl_int_clear(v);
967 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
968 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
969 set = isl_set_read_from_str(ctx, str);
970 set = isl_set_compute_divs(set);
971 isl_set_free(set);
972 if (!set)
973 return -1;
975 if (test_elimination(ctx) < 0)
976 return -1;
978 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
979 set = isl_set_read_from_str(ctx, str);
980 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
981 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
982 empty = isl_set_is_empty(set);
983 isl_set_free(set);
984 if (empty < 0)
985 return -1;
986 if (!empty)
987 isl_die(ctx, isl_error_unknown,
988 "result not as accurate as expected", return -1);
990 return 0;
993 void test_application_case(struct isl_ctx *ctx, const char *name)
995 char *filename;
996 FILE *input;
997 struct isl_basic_set *bset1, *bset2;
998 struct isl_basic_map *bmap;
1000 filename = get_filename(ctx, name, "omega");
1001 assert(filename);
1002 input = fopen(filename, "r");
1003 assert(input);
1005 bset1 = isl_basic_set_read_from_file(ctx, input);
1006 bmap = isl_basic_map_read_from_file(ctx, input);
1008 bset1 = isl_basic_set_apply(bset1, bmap);
1010 bset2 = isl_basic_set_read_from_file(ctx, input);
1012 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1014 isl_basic_set_free(bset1);
1015 isl_basic_set_free(bset2);
1016 free(filename);
1018 fclose(input);
1021 static int test_application(isl_ctx *ctx)
1023 test_application_case(ctx, "application");
1024 test_application_case(ctx, "application2");
1026 return 0;
1029 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1031 char *filename;
1032 FILE *input;
1033 struct isl_basic_set *bset1, *bset2;
1035 filename = get_filename(ctx, name, "polylib");
1036 assert(filename);
1037 input = fopen(filename, "r");
1038 assert(input);
1040 bset1 = isl_basic_set_read_from_file(ctx, input);
1041 bset2 = isl_basic_set_read_from_file(ctx, input);
1043 bset1 = isl_basic_set_affine_hull(bset1);
1045 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1047 isl_basic_set_free(bset1);
1048 isl_basic_set_free(bset2);
1049 free(filename);
1051 fclose(input);
1054 int test_affine_hull(struct isl_ctx *ctx)
1056 const char *str;
1057 isl_set *set;
1058 isl_basic_set *bset, *bset2;
1059 int n;
1060 int subset;
1062 test_affine_hull_case(ctx, "affine2");
1063 test_affine_hull_case(ctx, "affine");
1064 test_affine_hull_case(ctx, "affine3");
1066 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1067 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1068 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1069 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1070 set = isl_set_read_from_str(ctx, str);
1071 bset = isl_set_affine_hull(set);
1072 n = isl_basic_set_dim(bset, isl_dim_div);
1073 isl_basic_set_free(bset);
1074 if (n != 0)
1075 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1076 return -1);
1078 /* Check that isl_map_affine_hull is not confused by
1079 * the reordering of divs in isl_map_align_divs.
1081 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1082 "32e0 = b and 32e1 = c); "
1083 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1084 set = isl_set_read_from_str(ctx, str);
1085 bset = isl_set_affine_hull(set);
1086 isl_basic_set_free(bset);
1087 if (!bset)
1088 return -1;
1090 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1091 "32e2 = 31 + 31e0 }";
1092 set = isl_set_read_from_str(ctx, str);
1093 bset = isl_set_affine_hull(set);
1094 str = "{ [a] : exists e : a = 32 e }";
1095 bset2 = isl_basic_set_read_from_str(ctx, str);
1096 subset = isl_basic_set_is_subset(bset, bset2);
1097 isl_basic_set_free(bset);
1098 isl_basic_set_free(bset2);
1099 if (subset < 0)
1100 return -1;
1101 if (!subset)
1102 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1103 return -1);
1105 return 0;
1108 /* Pairs of maps and the corresponding expected results of
1109 * isl_map_plain_unshifted_simple_hull.
1111 struct {
1112 const char *map;
1113 const char *hull;
1114 } plain_unshifted_simple_hull_tests[] = {
1115 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1116 "{ [i] -> [j] : i >= 1 }" },
1117 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1118 "(j mod 4 = 2 and k mod 6 = n) }",
1119 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1122 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1124 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1126 int i;
1127 isl_map *map;
1128 isl_basic_map *hull, *expected;
1129 isl_bool equal;
1131 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1132 const char *str;
1133 str = plain_unshifted_simple_hull_tests[i].map;
1134 map = isl_map_read_from_str(ctx, str);
1135 str = plain_unshifted_simple_hull_tests[i].hull;
1136 expected = isl_basic_map_read_from_str(ctx, str);
1137 hull = isl_map_plain_unshifted_simple_hull(map);
1138 equal = isl_basic_map_is_equal(hull, expected);
1139 isl_basic_map_free(hull);
1140 isl_basic_map_free(expected);
1141 if (equal < 0)
1142 return -1;
1143 if (!equal)
1144 isl_die(ctx, isl_error_unknown, "unexpected hull",
1145 return -1);
1148 return 0;
1151 /* Pairs of sets and the corresponding expected results of
1152 * isl_set_unshifted_simple_hull.
1154 struct {
1155 const char *set;
1156 const char *hull;
1157 } unshifted_simple_hull_tests[] = {
1158 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1159 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1162 /* Basic tests for isl_set_unshifted_simple_hull.
1164 static int test_unshifted_simple_hull(isl_ctx *ctx)
1166 int i;
1167 isl_set *set;
1168 isl_basic_set *hull, *expected;
1169 isl_bool equal;
1171 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1172 const char *str;
1173 str = unshifted_simple_hull_tests[i].set;
1174 set = isl_set_read_from_str(ctx, str);
1175 str = unshifted_simple_hull_tests[i].hull;
1176 expected = isl_basic_set_read_from_str(ctx, str);
1177 hull = isl_set_unshifted_simple_hull(set);
1178 equal = isl_basic_set_is_equal(hull, expected);
1179 isl_basic_set_free(hull);
1180 isl_basic_set_free(expected);
1181 if (equal < 0)
1182 return -1;
1183 if (!equal)
1184 isl_die(ctx, isl_error_unknown, "unexpected hull",
1185 return -1);
1188 return 0;
1191 static int test_simple_hull(struct isl_ctx *ctx)
1193 const char *str;
1194 isl_set *set;
1195 isl_basic_set *bset;
1196 isl_bool is_empty;
1198 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1199 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1200 set = isl_set_read_from_str(ctx, str);
1201 bset = isl_set_simple_hull(set);
1202 is_empty = isl_basic_set_is_empty(bset);
1203 isl_basic_set_free(bset);
1205 if (is_empty == isl_bool_error)
1206 return -1;
1208 if (is_empty == isl_bool_false)
1209 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1210 return -1);
1212 if (test_plain_unshifted_simple_hull(ctx) < 0)
1213 return -1;
1214 if (test_unshifted_simple_hull(ctx) < 0)
1215 return -1;
1217 return 0;
1220 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1222 char *filename;
1223 FILE *input;
1224 struct isl_basic_set *bset1, *bset2;
1225 struct isl_set *set;
1227 filename = get_filename(ctx, name, "polylib");
1228 assert(filename);
1229 input = fopen(filename, "r");
1230 assert(input);
1232 bset1 = isl_basic_set_read_from_file(ctx, input);
1233 bset2 = isl_basic_set_read_from_file(ctx, input);
1235 set = isl_basic_set_union(bset1, bset2);
1236 bset1 = isl_set_convex_hull(set);
1238 bset2 = isl_basic_set_read_from_file(ctx, input);
1240 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1242 isl_basic_set_free(bset1);
1243 isl_basic_set_free(bset2);
1244 free(filename);
1246 fclose(input);
1249 struct {
1250 const char *set;
1251 const char *hull;
1252 } convex_hull_tests[] = {
1253 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1254 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1255 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1256 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1257 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1258 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1259 "i2 <= 5 and i2 >= 4; "
1260 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1261 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1262 "i2 <= 5 + i0 and i2 >= i0 }" },
1263 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1264 "{ [x, y] : 1 = 0 }" },
1267 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1269 int i;
1270 int orig_convex = ctx->opt->convex;
1271 ctx->opt->convex = convex;
1273 test_convex_hull_case(ctx, "convex0");
1274 test_convex_hull_case(ctx, "convex1");
1275 test_convex_hull_case(ctx, "convex2");
1276 test_convex_hull_case(ctx, "convex3");
1277 test_convex_hull_case(ctx, "convex4");
1278 test_convex_hull_case(ctx, "convex5");
1279 test_convex_hull_case(ctx, "convex6");
1280 test_convex_hull_case(ctx, "convex7");
1281 test_convex_hull_case(ctx, "convex8");
1282 test_convex_hull_case(ctx, "convex9");
1283 test_convex_hull_case(ctx, "convex10");
1284 test_convex_hull_case(ctx, "convex11");
1285 test_convex_hull_case(ctx, "convex12");
1286 test_convex_hull_case(ctx, "convex13");
1287 test_convex_hull_case(ctx, "convex14");
1288 test_convex_hull_case(ctx, "convex15");
1290 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1291 isl_set *set1, *set2;
1292 int equal;
1294 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1295 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1296 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1297 equal = isl_set_is_equal(set1, set2);
1298 isl_set_free(set1);
1299 isl_set_free(set2);
1301 if (equal < 0)
1302 return -1;
1303 if (!equal)
1304 isl_die(ctx, isl_error_unknown,
1305 "unexpected convex hull", return -1);
1308 ctx->opt->convex = orig_convex;
1310 return 0;
1313 static int test_convex_hull(isl_ctx *ctx)
1315 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1316 return -1;
1317 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1318 return -1;
1319 return 0;
1322 void test_gist_case(struct isl_ctx *ctx, const char *name)
1324 char *filename;
1325 FILE *input;
1326 struct isl_basic_set *bset1, *bset2;
1328 filename = get_filename(ctx, name, "polylib");
1329 assert(filename);
1330 input = fopen(filename, "r");
1331 assert(input);
1333 bset1 = isl_basic_set_read_from_file(ctx, input);
1334 bset2 = isl_basic_set_read_from_file(ctx, input);
1336 bset1 = isl_basic_set_gist(bset1, bset2);
1338 bset2 = isl_basic_set_read_from_file(ctx, input);
1340 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1342 isl_basic_set_free(bset1);
1343 isl_basic_set_free(bset2);
1344 free(filename);
1346 fclose(input);
1349 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1351 struct {
1352 const char *map;
1353 const char *context;
1354 const char *gist;
1355 } plain_gist_tests[] = {
1356 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1357 "{ [i] -> [j] : i >= 1 }",
1358 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1359 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1360 "(j mod 4 = 2 and k mod 6 = n) }",
1361 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1362 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1363 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1364 "{ [i] -> [j] : i > j }",
1365 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1368 /* Basic tests for isl_map_plain_gist_basic_map.
1370 static int test_plain_gist(isl_ctx *ctx)
1372 int i;
1374 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1375 const char *str;
1376 int equal;
1377 isl_map *map, *gist;
1378 isl_basic_map *context;
1380 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1381 str = plain_gist_tests[i].context;
1382 context = isl_basic_map_read_from_str(ctx, str);
1383 map = isl_map_plain_gist_basic_map(map, context);
1384 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1385 equal = isl_map_is_equal(map, gist);
1386 isl_map_free(map);
1387 isl_map_free(gist);
1388 if (equal < 0)
1389 return -1;
1390 if (!equal)
1391 isl_die(ctx, isl_error_unknown,
1392 "incorrect gist result", return -1);
1395 return 0;
1398 struct {
1399 const char *set;
1400 const char *context;
1401 const char *gist;
1402 } gist_tests[] = {
1403 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1404 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1405 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1406 "{ [a, b, c] : a <= 15 }" },
1407 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1408 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1409 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1410 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1411 { "{ [m, n, a, b] : a <= 2147 + n }",
1412 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1413 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1414 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1415 "b >= 0) }",
1416 "{ [m, n, ku, kl] }" },
1417 { "{ [a, a, b] : a >= 10 }",
1418 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1419 "{ [a, a, b] : a >= 10 }" },
1420 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1421 "{ [0, j] : j >= 0 }" },
1422 /* Check that no constraints on i6 are introduced in the gist */
1423 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1424 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1425 "5e0 <= 381 - t1 and i4 <= 1) }",
1426 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1427 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1428 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1429 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1430 "20e0 >= 1511 - 4t1 - 5i4) }" },
1431 /* Check that no constraints on i6 are introduced in the gist */
1432 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1433 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1434 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1435 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1436 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1437 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1438 "20e1 <= 1530 - 4t1 - 5i4 and "
1439 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1440 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1441 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1442 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1443 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1444 "10e0 <= -91 + 5i4 + 4i6 and "
1445 "10e0 >= -105 + 5i4 + 4i6) }",
1446 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1447 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1448 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1449 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1450 "{ [a, b, q, p] : b >= 1 + a }",
1451 "{ [a, b, q, p] : false }" },
1452 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1453 "[n] -> { [x] : x mod 32 = 0 }",
1454 "[n] -> { [x = n] }" },
1455 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1456 "{ [x] : x mod 2 = 0 }" },
1457 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1458 "{ [x] : x mod 128 = 0 }" },
1459 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1460 "{ [x] : x mod 3200 = 0 }" },
1461 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1462 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1463 "{ [a, b, c = a] }" },
1464 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1465 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1466 "{ [a, b, c = a] : a mod 3 = 0 }" },
1467 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1468 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1469 "{ [x] }" },
1470 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1471 "{ [x,y] : 1 <= y <= 3 }",
1472 "{ [x,y] }" },
1475 /* Check that isl_set_gist behaves as expected.
1477 * For the test cases in gist_tests, besides checking that the result
1478 * is as expected, also check that applying the gist operation does
1479 * not modify the input set (an earlier version of isl would do that) and
1480 * that the test case is consistent, i.e., that the gist has the same
1481 * intersection with the context as the input set.
1483 static int test_gist(struct isl_ctx *ctx)
1485 int i;
1486 const char *str;
1487 isl_basic_set *bset1, *bset2;
1488 isl_map *map1, *map2;
1489 int equal;
1491 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1492 int equal_input, equal_intersection;
1493 isl_set *set1, *set2, *copy, *context;
1495 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1496 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1497 copy = isl_set_copy(set1);
1498 set1 = isl_set_gist(set1, isl_set_copy(context));
1499 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1500 equal = isl_set_is_equal(set1, set2);
1501 isl_set_free(set1);
1502 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1503 equal_input = isl_set_is_equal(set1, copy);
1504 isl_set_free(copy);
1505 set1 = isl_set_intersect(set1, isl_set_copy(context));
1506 set2 = isl_set_intersect(set2, context);
1507 equal_intersection = isl_set_is_equal(set1, set2);
1508 isl_set_free(set2);
1509 isl_set_free(set1);
1510 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1511 return -1;
1512 if (!equal)
1513 isl_die(ctx, isl_error_unknown,
1514 "incorrect gist result", return -1);
1515 if (!equal_input)
1516 isl_die(ctx, isl_error_unknown,
1517 "gist modified input", return -1);
1518 if (!equal_input)
1519 isl_die(ctx, isl_error_unknown,
1520 "inconsistent gist test case", return -1);
1523 test_gist_case(ctx, "gist1");
1525 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1526 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1527 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1528 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1529 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1530 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1531 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1532 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1533 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1534 bset1 = isl_basic_set_read_from_str(ctx, str);
1535 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1536 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1537 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1538 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1539 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1540 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1541 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1542 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1543 bset2 = isl_basic_set_read_from_str(ctx, str);
1544 bset1 = isl_basic_set_gist(bset1, bset2);
1545 assert(bset1 && bset1->n_div == 0);
1546 isl_basic_set_free(bset1);
1548 /* Check that the integer divisions of the second disjunct
1549 * do not spread to the first disjunct.
1551 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1552 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1553 "(exists (e0 = [(-1 + t1)/16], "
1554 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1555 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1556 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1557 "o0 <= 4294967295 and t1 <= -1)) }";
1558 map1 = isl_map_read_from_str(ctx, str);
1559 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1560 map2 = isl_map_read_from_str(ctx, str);
1561 map1 = isl_map_gist(map1, map2);
1562 if (!map1)
1563 return -1;
1564 if (map1->n != 1)
1565 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1566 isl_map_free(map1); return -1);
1567 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1568 isl_die(ctx, isl_error_unknown, "expecting single div",
1569 isl_map_free(map1); return -1);
1570 isl_map_free(map1);
1572 if (test_plain_gist(ctx) < 0)
1573 return -1;
1575 return 0;
1578 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1580 isl_set *set, *set2;
1581 int equal;
1582 int one;
1584 set = isl_set_read_from_str(ctx, str);
1585 set = isl_set_coalesce(set);
1586 set2 = isl_set_read_from_str(ctx, str);
1587 equal = isl_set_is_equal(set, set2);
1588 one = set && set->n == 1;
1589 isl_set_free(set);
1590 isl_set_free(set2);
1592 if (equal < 0)
1593 return -1;
1594 if (!equal)
1595 isl_die(ctx, isl_error_unknown,
1596 "coalesced set not equal to input", return -1);
1597 if (check_one && !one)
1598 isl_die(ctx, isl_error_unknown,
1599 "coalesced set should not be a union", return -1);
1601 return 0;
1604 /* Inputs for coalescing tests with unbounded wrapping.
1605 * "str" is a string representation of the input set.
1606 * "single_disjunct" is set if we expect the result to consist of
1607 * a single disjunct.
1609 struct {
1610 int single_disjunct;
1611 const char *str;
1612 } coalesce_unbounded_tests[] = {
1613 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1614 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1615 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1616 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1617 "-10 <= y <= 0}" },
1618 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1619 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1620 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1621 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1624 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1625 * option turned off.
1627 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1629 int i;
1630 int r = 0;
1631 int bounded;
1633 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1634 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1636 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1637 const char *str = coalesce_unbounded_tests[i].str;
1638 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1639 if (test_coalesce_set(ctx, str, check_one) >= 0)
1640 continue;
1641 r = -1;
1642 break;
1645 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1647 return r;
1650 /* Inputs for coalescing tests.
1651 * "str" is a string representation of the input set.
1652 * "single_disjunct" is set if we expect the result to consist of
1653 * a single disjunct.
1655 struct {
1656 int single_disjunct;
1657 const char *str;
1658 } coalesce_tests[] = {
1659 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1660 "y >= x & x >= 2 & 5 >= y }" },
1661 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1662 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1663 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1664 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1665 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1666 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1667 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1668 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1669 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1670 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1671 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1672 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1673 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1674 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1675 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1676 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1677 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1678 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1679 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1680 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1681 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1682 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1683 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1684 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1685 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1686 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1687 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1688 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1689 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1690 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1691 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1692 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1693 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1694 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1695 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1696 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1697 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1698 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1699 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1700 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1701 "[o0, o1, o2, o3, o4, o5, o6]] : "
1702 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1703 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1704 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1705 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1706 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1707 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1708 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1709 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1710 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1711 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1712 "o6 >= i3 + i6 - o3 and M >= 0 and "
1713 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1714 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1715 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1716 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1717 "(o0 = 0 and M >= 2 and N >= 3) or "
1718 "(M = 0 and o0 = 0 and N >= 3) }" },
1719 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1720 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1721 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1722 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1723 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1724 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1725 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1726 "(y = 3 and x = 1) }" },
1727 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1728 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1729 "i1 <= M and i3 <= M and i4 <= M) or "
1730 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1731 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1732 "i4 <= -1 + M) }" },
1733 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1734 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1735 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1736 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1737 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1738 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1739 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1740 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1741 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1742 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1743 { 0, "{ [a, b] : exists e : 2e = a and "
1744 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1745 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1746 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1747 "j >= 1 and j' <= i + j - i' and i >= 1; "
1748 "[1, 1, 1, 1] }" },
1749 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1750 "[i,j] : exists a : j = 3a }" },
1751 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1752 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1753 "a >= 3) or "
1754 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1755 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1756 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1757 "c <= 6 + 8a and a >= 3; "
1758 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1759 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1760 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1761 "[x,0] : 3 <= x <= 4 }" },
1762 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1763 "[x,0] : 4 <= x <= 5 }" },
1764 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1765 "[x,0] : 3 <= x <= 5 }" },
1766 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1767 "[x,0] : 3 <= x <= 4 }" },
1768 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1769 "i1 <= 0; "
1770 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1771 { 1, "{ [0,0]; [1,1] }" },
1772 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1773 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1774 "ii <= k;"
1775 "[k, 0, k] : k <= 6 and k >= 1 }" },
1776 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1777 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1778 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1779 { 1, "[n] -> { [1] : n >= 0;"
1780 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1781 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1782 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1783 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1784 "2e0 <= x and 2e0 <= n);"
1785 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1786 "n >= 0) }" },
1787 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1788 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1789 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1790 "t1 = 1 }" },
1791 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1792 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1793 "[0, 0] }" },
1794 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1795 "t1 >= 13 and t1 <= 16);"
1796 "[t1] : t1 <= 15 and t1 >= 12 }" },
1797 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1798 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1799 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1800 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1801 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1802 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1803 "i <= 4j + 2 }" },
1804 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1805 "(exists (e0 : 3e0 = -2 + c0)) }" },
1806 { 0, "[n, b0, t0] -> "
1807 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1808 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1809 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1810 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1811 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1812 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1813 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1814 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1815 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1816 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1817 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1818 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1819 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1820 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1821 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1822 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1823 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1824 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1825 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1826 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1827 { 0, "{ [i0, i1, i2] : "
1828 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1829 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1830 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1831 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1832 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1833 "32e0 <= 31 + i0)) or "
1834 "i0 >= 0 }" },
1835 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1836 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1837 "2*floor((c)/2) = c and 0 <= a <= 192;"
1838 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1840 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1841 "(0 <= a <= b <= n) }" },
1842 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1843 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1844 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1845 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1846 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1847 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1848 "b = 3 and 9e0 <= -19 + 2c)) }" },
1849 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1850 "(b = -1 + a and 0 < a <= 3 and "
1851 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1852 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1853 "b = 3 and 9e0 <= -19 + 2c)) }" },
1854 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1855 "[1, 0] }" },
1856 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1857 "[0, 1] }" },
1858 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1859 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1860 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1861 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1862 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1863 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1864 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1865 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1866 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1869 /* A specialized coalescing test case that would result
1870 * in a segmentation fault or a failed assertion in earlier versions of isl.
1872 static int test_coalesce_special(struct isl_ctx *ctx)
1874 const char *str;
1875 isl_map *map1, *map2;
1877 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1878 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1879 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1880 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1881 "o1 <= 239 and o1 >= 212)) or "
1882 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1883 "o1 <= 241 and o1 >= 240));"
1884 "[S_L220_OUT[] -> T7[]] -> "
1885 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1886 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1887 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1888 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1889 map1 = isl_map_read_from_str(ctx, str);
1890 map1 = isl_map_align_divs(map1);
1891 map1 = isl_map_coalesce(map1);
1892 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1893 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1894 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1895 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1896 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1897 map2 = isl_map_read_from_str(ctx, str);
1898 map2 = isl_map_union(map2, map1);
1899 map2 = isl_map_align_divs(map2);
1900 map2 = isl_map_coalesce(map2);
1901 isl_map_free(map2);
1902 if (!map2)
1903 return -1;
1905 return 0;
1908 /* A specialized coalescing test case that would result in an assertion
1909 * in an earlier version of isl.
1910 * The explicit call to isl_basic_set_union prevents the implicit
1911 * equality constraints in the first basic map from being detected prior
1912 * to the call to isl_set_coalesce, at least at the point
1913 * where this test case was introduced.
1915 static int test_coalesce_special2(struct isl_ctx *ctx)
1917 const char *str;
1918 isl_basic_set *bset1, *bset2;
1919 isl_set *set;
1921 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
1922 bset1 = isl_basic_set_read_from_str(ctx, str);
1923 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
1924 bset2 = isl_basic_set_read_from_str(ctx, str);
1925 set = isl_basic_set_union(bset1, bset2);
1926 set = isl_set_coalesce(set);
1927 isl_set_free(set);
1929 if (!set)
1930 return -1;
1931 return 0;
1934 /* Test the functionality of isl_set_coalesce.
1935 * That is, check that the output is always equal to the input
1936 * and in some cases that the result consists of a single disjunct.
1938 static int test_coalesce(struct isl_ctx *ctx)
1940 int i;
1942 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1943 const char *str = coalesce_tests[i].str;
1944 int check_one = coalesce_tests[i].single_disjunct;
1945 if (test_coalesce_set(ctx, str, check_one) < 0)
1946 return -1;
1949 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1950 return -1;
1951 if (test_coalesce_special(ctx) < 0)
1952 return -1;
1953 if (test_coalesce_special2(ctx) < 0)
1954 return -1;
1956 return 0;
1959 /* Construct a representation of the graph on the right of Figure 1
1960 * in "Computing the Transitive Closure of a Union of
1961 * Affine Integer Tuple Relations".
1963 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
1965 isl_set *dom;
1966 isl_map *up, *right;
1968 dom = isl_set_read_from_str(ctx,
1969 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1970 "2 x - 3 y + 3 >= 0 }");
1971 right = isl_map_read_from_str(ctx,
1972 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1973 up = isl_map_read_from_str(ctx,
1974 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1975 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1976 right = isl_map_intersect_range(right, isl_set_copy(dom));
1977 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1978 up = isl_map_intersect_range(up, dom);
1979 return isl_map_union(up, right);
1982 /* Construct a representation of the power of the graph
1983 * on the right of Figure 1 in "Computing the Transitive Closure of
1984 * a Union of Affine Integer Tuple Relations".
1986 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
1988 return isl_map_read_from_str(ctx,
1989 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
1990 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
1991 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
1994 /* Construct a representation of the transitive closure of the graph
1995 * on the right of Figure 1 in "Computing the Transitive Closure of
1996 * a Union of Affine Integer Tuple Relations".
1998 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2000 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2003 static int test_closure(isl_ctx *ctx)
2005 const char *str;
2006 isl_map *map, *map2;
2007 int exact, equal;
2009 /* COCOA example 1 */
2010 map = isl_map_read_from_str(ctx,
2011 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2012 "1 <= i and i < n and 1 <= j and j < n or "
2013 "i2 = i + 1 and j2 = j - 1 and "
2014 "1 <= i and i < n and 2 <= j and j <= n }");
2015 map = isl_map_power(map, &exact);
2016 assert(exact);
2017 isl_map_free(map);
2019 /* COCOA example 1 */
2020 map = isl_map_read_from_str(ctx,
2021 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2022 "1 <= i and i < n and 1 <= j and j < n or "
2023 "i2 = i + 1 and j2 = j - 1 and "
2024 "1 <= i and i < n and 2 <= j and j <= n }");
2025 map = isl_map_transitive_closure(map, &exact);
2026 assert(exact);
2027 map2 = isl_map_read_from_str(ctx,
2028 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2029 "1 <= i and i < n and 1 <= j and j <= n and "
2030 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2031 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2032 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2033 assert(isl_map_is_equal(map, map2));
2034 isl_map_free(map2);
2035 isl_map_free(map);
2037 map = isl_map_read_from_str(ctx,
2038 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2039 " 0 <= y and y <= n }");
2040 map = isl_map_transitive_closure(map, &exact);
2041 map2 = isl_map_read_from_str(ctx,
2042 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2043 " 0 <= y and y <= n }");
2044 assert(isl_map_is_equal(map, map2));
2045 isl_map_free(map2);
2046 isl_map_free(map);
2048 /* COCOA example 2 */
2049 map = isl_map_read_from_str(ctx,
2050 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2051 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2052 "i2 = i + 2 and j2 = j - 2 and "
2053 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2054 map = isl_map_transitive_closure(map, &exact);
2055 assert(exact);
2056 map2 = isl_map_read_from_str(ctx,
2057 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2058 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2059 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2060 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2061 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2062 assert(isl_map_is_equal(map, map2));
2063 isl_map_free(map);
2064 isl_map_free(map2);
2066 /* COCOA Fig.2 left */
2067 map = isl_map_read_from_str(ctx,
2068 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2069 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2070 "j <= n or "
2071 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2072 "j <= 2 i - 3 and j <= n - 2 or "
2073 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2074 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2075 map = isl_map_transitive_closure(map, &exact);
2076 assert(exact);
2077 isl_map_free(map);
2079 /* COCOA Fig.2 right */
2080 map = isl_map_read_from_str(ctx,
2081 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2082 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2083 "j <= n or "
2084 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2085 "j <= 2 i - 4 and j <= n - 3 or "
2086 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2087 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2088 map = isl_map_power(map, &exact);
2089 assert(exact);
2090 isl_map_free(map);
2092 /* COCOA Fig.2 right */
2093 map = isl_map_read_from_str(ctx,
2094 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2095 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2096 "j <= n or "
2097 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2098 "j <= 2 i - 4 and j <= n - 3 or "
2099 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2100 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2101 map = isl_map_transitive_closure(map, &exact);
2102 assert(exact);
2103 map2 = isl_map_read_from_str(ctx,
2104 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2105 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2106 "j <= n and 3 + i + 2 j <= 3 n and "
2107 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2108 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2109 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2110 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2111 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2112 assert(isl_map_is_equal(map, map2));
2113 isl_map_free(map2);
2114 isl_map_free(map);
2116 map = cocoa_fig_1_right_graph(ctx);
2117 map = isl_map_transitive_closure(map, &exact);
2118 assert(exact);
2119 map2 = cocoa_fig_1_right_tc(ctx);
2120 assert(isl_map_is_equal(map, map2));
2121 isl_map_free(map2);
2122 isl_map_free(map);
2124 map = cocoa_fig_1_right_graph(ctx);
2125 map = isl_map_power(map, &exact);
2126 map2 = cocoa_fig_1_right_power(ctx);
2127 equal = isl_map_is_equal(map, map2);
2128 isl_map_free(map2);
2129 isl_map_free(map);
2130 if (equal < 0)
2131 return -1;
2132 if (!exact)
2133 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2134 if (!equal)
2135 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2137 /* COCOA Theorem 1 counter example */
2138 map = isl_map_read_from_str(ctx,
2139 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2140 "i2 = 1 and j2 = j or "
2141 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2142 map = isl_map_transitive_closure(map, &exact);
2143 assert(exact);
2144 isl_map_free(map);
2146 map = isl_map_read_from_str(ctx,
2147 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2148 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2149 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2150 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2151 map = isl_map_transitive_closure(map, &exact);
2152 assert(exact);
2153 isl_map_free(map);
2155 /* Kelly et al 1996, fig 12 */
2156 map = isl_map_read_from_str(ctx,
2157 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2158 "1 <= i,j,j+1 <= n or "
2159 "j = n and j2 = 1 and i2 = i + 1 and "
2160 "1 <= i,i+1 <= n }");
2161 map = isl_map_transitive_closure(map, &exact);
2162 assert(exact);
2163 map2 = isl_map_read_from_str(ctx,
2164 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2165 "1 <= i <= n and i = i2 or "
2166 "1 <= i < i2 <= n and 1 <= j <= n and "
2167 "1 <= j2 <= n }");
2168 assert(isl_map_is_equal(map, map2));
2169 isl_map_free(map2);
2170 isl_map_free(map);
2172 /* Omega's closure4 */
2173 map = isl_map_read_from_str(ctx,
2174 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2175 "1 <= x,y <= 10 or "
2176 "x2 = x + 1 and y2 = y and "
2177 "1 <= x <= 20 && 5 <= y <= 15 }");
2178 map = isl_map_transitive_closure(map, &exact);
2179 assert(exact);
2180 isl_map_free(map);
2182 map = isl_map_read_from_str(ctx,
2183 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2184 map = isl_map_transitive_closure(map, &exact);
2185 assert(!exact);
2186 map2 = isl_map_read_from_str(ctx,
2187 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2188 assert(isl_map_is_equal(map, map2));
2189 isl_map_free(map);
2190 isl_map_free(map2);
2192 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2193 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2194 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2195 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2196 map = isl_map_read_from_str(ctx, str);
2197 map = isl_map_transitive_closure(map, &exact);
2198 assert(exact);
2199 map2 = isl_map_read_from_str(ctx, str);
2200 assert(isl_map_is_equal(map, map2));
2201 isl_map_free(map);
2202 isl_map_free(map2);
2204 str = "{[0] -> [1]; [2] -> [3]}";
2205 map = isl_map_read_from_str(ctx, str);
2206 map = isl_map_transitive_closure(map, &exact);
2207 assert(exact);
2208 map2 = isl_map_read_from_str(ctx, str);
2209 assert(isl_map_is_equal(map, map2));
2210 isl_map_free(map);
2211 isl_map_free(map2);
2213 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2214 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2215 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2216 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2217 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2218 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2219 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2220 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2221 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2222 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2223 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2224 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2225 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2226 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2227 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2228 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2229 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2230 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2231 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2232 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2233 map = isl_map_read_from_str(ctx, str);
2234 map = isl_map_transitive_closure(map, NULL);
2235 assert(map);
2236 isl_map_free(map);
2238 return 0;
2241 static int test_lex(struct isl_ctx *ctx)
2243 isl_space *dim;
2244 isl_map *map;
2245 int empty;
2247 dim = isl_space_set_alloc(ctx, 0, 0);
2248 map = isl_map_lex_le(dim);
2249 empty = isl_map_is_empty(map);
2250 isl_map_free(map);
2252 if (empty < 0)
2253 return -1;
2254 if (empty)
2255 isl_die(ctx, isl_error_unknown,
2256 "expecting non-empty result", return -1);
2258 return 0;
2261 /* Inputs for isl_map_lexmin tests.
2262 * "map" is the input and "lexmin" is the expected result.
2264 struct {
2265 const char *map;
2266 const char *lexmin;
2267 } lexmin_tests [] = {
2268 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2269 "{ [x] -> [5] : 6 <= x <= 8; "
2270 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2271 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2272 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2273 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2274 "{ [x] -> [y] : (4y = x and x >= 0) or "
2275 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2276 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2277 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2278 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2279 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2280 /* Check that empty pieces are properly combined. */
2281 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2282 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2283 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2284 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2285 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2286 "x >= 4 }" },
2287 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2288 "a <= 255 and c <= 255 and d <= 255 - j and "
2289 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2290 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2291 "247d <= 247 + i and 248 - b <= 248d <= c and "
2292 "254d >= i - a + b and 254d >= -a + b and "
2293 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2294 "{ [i, k, j] -> "
2295 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2296 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2297 "247j >= 62738 - i and 509j <= 129795 + i and "
2298 "742j >= 188724 - i; "
2299 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2300 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2301 "16*floor((8 + b)/16) <= 7 + b; "
2302 "[a] -> [1] }",
2303 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2304 "[a] -> [b = 0] : 0 < a <= 509 }" },
2307 static int test_lexmin(struct isl_ctx *ctx)
2309 int i;
2310 int equal;
2311 const char *str;
2312 isl_basic_map *bmap;
2313 isl_map *map, *map2;
2314 isl_set *set;
2315 isl_set *set2;
2316 isl_pw_multi_aff *pma;
2318 str = "[p0, p1] -> { [] -> [] : "
2319 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2320 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2321 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2322 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2323 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2324 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2325 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2326 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2327 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2328 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2329 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2330 map = isl_map_read_from_str(ctx, str);
2331 map = isl_map_lexmin(map);
2332 isl_map_free(map);
2334 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2335 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2336 set = isl_set_read_from_str(ctx, str);
2337 set = isl_set_lexmax(set);
2338 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2339 set2 = isl_set_read_from_str(ctx, str);
2340 set = isl_set_intersect(set, set2);
2341 assert(!isl_set_is_empty(set));
2342 isl_set_free(set);
2344 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2345 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2346 map = isl_map_lexmin(map);
2347 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2348 equal = isl_map_is_equal(map, map2);
2349 isl_map_free(map);
2350 isl_map_free(map2);
2352 if (equal < 0)
2353 return -1;
2354 if (!equal)
2355 isl_die(ctx, isl_error_unknown,
2356 "unexpected result", return -1);
2359 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2360 " 8i' <= i and 8i' >= -7 + i }";
2361 bmap = isl_basic_map_read_from_str(ctx, str);
2362 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2363 map2 = isl_map_from_pw_multi_aff(pma);
2364 map = isl_map_from_basic_map(bmap);
2365 assert(isl_map_is_equal(map, map2));
2366 isl_map_free(map);
2367 isl_map_free(map2);
2369 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2370 " 8i' <= i and 8i' >= -7 + i }";
2371 set = isl_set_read_from_str(ctx, str);
2372 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2373 set2 = isl_set_from_pw_multi_aff(pma);
2374 equal = isl_set_is_equal(set, set2);
2375 isl_set_free(set);
2376 isl_set_free(set2);
2377 if (equal < 0)
2378 return -1;
2379 if (!equal)
2380 isl_die(ctx, isl_error_unknown,
2381 "unexpected difference between set and "
2382 "piecewise affine expression", return -1);
2384 return 0;
2387 struct {
2388 const char *set;
2389 const char *obj;
2390 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2391 __isl_keep isl_aff *obj);
2392 const char *res;
2393 } opt_tests[] = {
2394 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2395 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2396 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2397 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2398 &isl_set_max_val, "30" },
2402 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2403 * In particular, check the results on non-convex inputs.
2405 static int test_min(struct isl_ctx *ctx)
2407 int i;
2408 isl_set *set;
2409 isl_aff *obj;
2410 isl_val *val, *res;
2411 isl_bool ok;
2413 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2414 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2415 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2416 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2417 val = opt_tests[i].fn(set, obj);
2418 ok = isl_val_eq(res, val);
2419 isl_val_free(res);
2420 isl_val_free(val);
2421 isl_aff_free(obj);
2422 isl_set_free(set);
2424 if (ok < 0)
2425 return -1;
2426 if (!ok)
2427 isl_die(ctx, isl_error_unknown,
2428 "unexpected optimum", return -1);
2431 return 0;
2434 struct must_may {
2435 isl_map *must;
2436 isl_map *may;
2439 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2440 void *dep_user, void *user)
2442 struct must_may *mm = (struct must_may *)user;
2444 if (must)
2445 mm->must = isl_map_union(mm->must, dep);
2446 else
2447 mm->may = isl_map_union(mm->may, dep);
2449 return isl_stat_ok;
2452 static int common_space(void *first, void *second)
2454 int depth = *(int *)first;
2455 return 2 * depth;
2458 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2460 isl_map *map2;
2461 int equal;
2463 if (!map)
2464 return -1;
2466 map2 = isl_map_read_from_str(map->ctx, str);
2467 equal = isl_map_is_equal(map, map2);
2468 isl_map_free(map2);
2470 return equal;
2473 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2475 int equal;
2477 equal = map_is_equal(map, str);
2478 if (equal < 0)
2479 return -1;
2480 if (!equal)
2481 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2482 "result not as expected", return -1);
2483 return 0;
2486 static int test_dep(struct isl_ctx *ctx)
2488 const char *str;
2489 isl_space *dim;
2490 isl_map *map;
2491 isl_access_info *ai;
2492 isl_flow *flow;
2493 int depth;
2494 struct must_may mm;
2496 depth = 3;
2498 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2499 map = isl_map_read_from_str(ctx, str);
2500 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2502 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2503 map = isl_map_read_from_str(ctx, str);
2504 ai = isl_access_info_add_source(ai, map, 1, &depth);
2506 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2507 map = isl_map_read_from_str(ctx, str);
2508 ai = isl_access_info_add_source(ai, map, 1, &depth);
2510 flow = isl_access_info_compute_flow(ai);
2511 dim = isl_space_alloc(ctx, 0, 3, 3);
2512 mm.must = isl_map_empty(isl_space_copy(dim));
2513 mm.may = isl_map_empty(dim);
2515 isl_flow_foreach(flow, collect_must_may, &mm);
2517 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2518 " [1,10,0] -> [2,5,0] }";
2519 assert(map_is_equal(mm.must, str));
2520 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2521 assert(map_is_equal(mm.may, str));
2523 isl_map_free(mm.must);
2524 isl_map_free(mm.may);
2525 isl_flow_free(flow);
2528 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2529 map = isl_map_read_from_str(ctx, str);
2530 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2532 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2533 map = isl_map_read_from_str(ctx, str);
2534 ai = isl_access_info_add_source(ai, map, 1, &depth);
2536 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2537 map = isl_map_read_from_str(ctx, str);
2538 ai = isl_access_info_add_source(ai, map, 0, &depth);
2540 flow = isl_access_info_compute_flow(ai);
2541 dim = isl_space_alloc(ctx, 0, 3, 3);
2542 mm.must = isl_map_empty(isl_space_copy(dim));
2543 mm.may = isl_map_empty(dim);
2545 isl_flow_foreach(flow, collect_must_may, &mm);
2547 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2548 assert(map_is_equal(mm.must, str));
2549 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2550 assert(map_is_equal(mm.may, str));
2552 isl_map_free(mm.must);
2553 isl_map_free(mm.may);
2554 isl_flow_free(flow);
2557 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2558 map = isl_map_read_from_str(ctx, str);
2559 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2561 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2562 map = isl_map_read_from_str(ctx, str);
2563 ai = isl_access_info_add_source(ai, map, 0, &depth);
2565 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2566 map = isl_map_read_from_str(ctx, str);
2567 ai = isl_access_info_add_source(ai, map, 0, &depth);
2569 flow = isl_access_info_compute_flow(ai);
2570 dim = isl_space_alloc(ctx, 0, 3, 3);
2571 mm.must = isl_map_empty(isl_space_copy(dim));
2572 mm.may = isl_map_empty(dim);
2574 isl_flow_foreach(flow, collect_must_may, &mm);
2576 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2577 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2578 assert(map_is_equal(mm.may, str));
2579 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2580 assert(map_is_equal(mm.must, str));
2582 isl_map_free(mm.must);
2583 isl_map_free(mm.may);
2584 isl_flow_free(flow);
2587 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2588 map = isl_map_read_from_str(ctx, str);
2589 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2591 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2592 map = isl_map_read_from_str(ctx, str);
2593 ai = isl_access_info_add_source(ai, map, 0, &depth);
2595 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2596 map = isl_map_read_from_str(ctx, str);
2597 ai = isl_access_info_add_source(ai, map, 0, &depth);
2599 flow = isl_access_info_compute_flow(ai);
2600 dim = isl_space_alloc(ctx, 0, 3, 3);
2601 mm.must = isl_map_empty(isl_space_copy(dim));
2602 mm.may = isl_map_empty(dim);
2604 isl_flow_foreach(flow, collect_must_may, &mm);
2606 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2607 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2608 assert(map_is_equal(mm.may, str));
2609 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2610 assert(map_is_equal(mm.must, str));
2612 isl_map_free(mm.must);
2613 isl_map_free(mm.may);
2614 isl_flow_free(flow);
2617 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2618 map = isl_map_read_from_str(ctx, str);
2619 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2621 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2622 map = isl_map_read_from_str(ctx, str);
2623 ai = isl_access_info_add_source(ai, map, 0, &depth);
2625 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2626 map = isl_map_read_from_str(ctx, str);
2627 ai = isl_access_info_add_source(ai, map, 0, &depth);
2629 flow = isl_access_info_compute_flow(ai);
2630 dim = isl_space_alloc(ctx, 0, 3, 3);
2631 mm.must = isl_map_empty(isl_space_copy(dim));
2632 mm.may = isl_map_empty(dim);
2634 isl_flow_foreach(flow, collect_must_may, &mm);
2636 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2637 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2638 assert(map_is_equal(mm.may, str));
2639 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2640 assert(map_is_equal(mm.must, str));
2642 isl_map_free(mm.must);
2643 isl_map_free(mm.may);
2644 isl_flow_free(flow);
2647 depth = 5;
2649 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2650 map = isl_map_read_from_str(ctx, str);
2651 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2653 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2654 map = isl_map_read_from_str(ctx, str);
2655 ai = isl_access_info_add_source(ai, map, 1, &depth);
2657 flow = isl_access_info_compute_flow(ai);
2658 dim = isl_space_alloc(ctx, 0, 5, 5);
2659 mm.must = isl_map_empty(isl_space_copy(dim));
2660 mm.may = isl_map_empty(dim);
2662 isl_flow_foreach(flow, collect_must_may, &mm);
2664 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2665 assert(map_is_equal(mm.must, str));
2666 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2667 assert(map_is_equal(mm.may, str));
2669 isl_map_free(mm.must);
2670 isl_map_free(mm.may);
2671 isl_flow_free(flow);
2673 return 0;
2676 /* Check that the dependence analysis proceeds without errors.
2677 * Earlier versions of isl would break down during the analysis
2678 * due to the use of the wrong spaces.
2680 static int test_flow(isl_ctx *ctx)
2682 const char *str;
2683 isl_union_map *access, *schedule;
2684 isl_union_map *must_dep, *may_dep;
2685 int r;
2687 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2688 access = isl_union_map_read_from_str(ctx, str);
2689 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2690 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2691 "S2[] -> [1,0,0,0]; "
2692 "S3[] -> [-1,0,0,0] }";
2693 schedule = isl_union_map_read_from_str(ctx, str);
2694 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2695 isl_union_map_copy(access), schedule,
2696 &must_dep, &may_dep, NULL, NULL);
2697 isl_union_map_free(may_dep);
2698 isl_union_map_free(must_dep);
2700 return r;
2703 struct {
2704 const char *map;
2705 int sv;
2706 } sv_tests[] = {
2707 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2708 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2709 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2710 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2711 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2712 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2713 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2714 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2715 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2718 int test_sv(isl_ctx *ctx)
2720 isl_union_map *umap;
2721 int i;
2722 int sv;
2724 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2725 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2726 sv = isl_union_map_is_single_valued(umap);
2727 isl_union_map_free(umap);
2728 if (sv < 0)
2729 return -1;
2730 if (sv_tests[i].sv && !sv)
2731 isl_die(ctx, isl_error_internal,
2732 "map not detected as single valued", return -1);
2733 if (!sv_tests[i].sv && sv)
2734 isl_die(ctx, isl_error_internal,
2735 "map detected as single valued", return -1);
2738 return 0;
2741 struct {
2742 const char *str;
2743 int bijective;
2744 } bijective_tests[] = {
2745 { "[N,M]->{[i,j] -> [i]}", 0 },
2746 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2747 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2748 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2749 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2750 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2751 { "[N,M]->{[i,j] -> []}", 0 },
2752 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2753 { "[N,M]->{[i,j] -> [2i]}", 0 },
2754 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2755 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2756 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2757 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2760 static int test_bijective(struct isl_ctx *ctx)
2762 isl_map *map;
2763 int i;
2764 int bijective;
2766 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2767 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2768 bijective = isl_map_is_bijective(map);
2769 isl_map_free(map);
2770 if (bijective < 0)
2771 return -1;
2772 if (bijective_tests[i].bijective && !bijective)
2773 isl_die(ctx, isl_error_internal,
2774 "map not detected as bijective", return -1);
2775 if (!bijective_tests[i].bijective && bijective)
2776 isl_die(ctx, isl_error_internal,
2777 "map detected as bijective", return -1);
2780 return 0;
2783 /* Inputs for isl_pw_qpolynomial_gist tests.
2784 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2786 struct {
2787 const char *pwqp;
2788 const char *set;
2789 const char *gist;
2790 } pwqp_gist_tests[] = {
2791 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2792 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2793 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2794 "{ [i] -> -1/2 + 1/2 * i }" },
2795 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2798 static int test_pwqp(struct isl_ctx *ctx)
2800 int i;
2801 const char *str;
2802 isl_set *set;
2803 isl_pw_qpolynomial *pwqp1, *pwqp2;
2804 int equal;
2806 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2807 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2809 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2810 isl_dim_in, 1, 1);
2812 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2813 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2815 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2817 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2819 isl_pw_qpolynomial_free(pwqp1);
2821 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2822 str = pwqp_gist_tests[i].pwqp;
2823 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2824 str = pwqp_gist_tests[i].set;
2825 set = isl_set_read_from_str(ctx, str);
2826 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2827 str = pwqp_gist_tests[i].gist;
2828 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2829 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2830 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2831 isl_pw_qpolynomial_free(pwqp1);
2833 if (equal < 0)
2834 return -1;
2835 if (!equal)
2836 isl_die(ctx, isl_error_unknown,
2837 "unexpected result", return -1);
2840 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2841 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2842 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2843 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2845 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2847 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2849 isl_pw_qpolynomial_free(pwqp1);
2851 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2852 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2853 str = "{ [x] -> x }";
2854 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2856 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2858 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2860 isl_pw_qpolynomial_free(pwqp1);
2862 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2863 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2864 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2865 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2866 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2867 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2868 isl_pw_qpolynomial_free(pwqp1);
2870 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2871 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2872 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2873 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2874 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2875 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2876 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2877 isl_pw_qpolynomial_free(pwqp1);
2878 isl_pw_qpolynomial_free(pwqp2);
2879 if (equal < 0)
2880 return -1;
2881 if (!equal)
2882 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2884 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2885 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2886 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2887 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2888 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2889 isl_val_one(ctx));
2890 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2891 isl_pw_qpolynomial_free(pwqp1);
2892 isl_pw_qpolynomial_free(pwqp2);
2893 if (equal < 0)
2894 return -1;
2895 if (!equal)
2896 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2898 return 0;
2901 static int test_split_periods(isl_ctx *ctx)
2903 const char *str;
2904 isl_pw_qpolynomial *pwqp;
2906 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2907 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2908 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2909 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2911 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2913 isl_pw_qpolynomial_free(pwqp);
2915 if (!pwqp)
2916 return -1;
2918 return 0;
2921 static int test_union(isl_ctx *ctx)
2923 const char *str;
2924 isl_union_set *uset1, *uset2;
2925 isl_union_map *umap1, *umap2;
2926 int equal;
2928 str = "{ [i] : 0 <= i <= 1 }";
2929 uset1 = isl_union_set_read_from_str(ctx, str);
2930 str = "{ [1] -> [0] }";
2931 umap1 = isl_union_map_read_from_str(ctx, str);
2933 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2934 equal = isl_union_map_is_equal(umap1, umap2);
2936 isl_union_map_free(umap1);
2937 isl_union_map_free(umap2);
2939 if (equal < 0)
2940 return -1;
2941 if (!equal)
2942 isl_die(ctx, isl_error_unknown, "union maps not equal",
2943 return -1);
2945 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2946 umap1 = isl_union_map_read_from_str(ctx, str);
2947 str = "{ A[i]; B[i] }";
2948 uset1 = isl_union_set_read_from_str(ctx, str);
2950 uset2 = isl_union_map_domain(umap1);
2952 equal = isl_union_set_is_equal(uset1, uset2);
2954 isl_union_set_free(uset1);
2955 isl_union_set_free(uset2);
2957 if (equal < 0)
2958 return -1;
2959 if (!equal)
2960 isl_die(ctx, isl_error_unknown, "union sets not equal",
2961 return -1);
2963 return 0;
2966 /* Check that computing a bound of a non-zero polynomial over an unbounded
2967 * domain does not produce a rational value.
2968 * In particular, check that the upper bound is infinity.
2970 static int test_bound_unbounded_domain(isl_ctx *ctx)
2972 const char *str;
2973 isl_pw_qpolynomial *pwqp;
2974 isl_pw_qpolynomial_fold *pwf, *pwf2;
2975 isl_bool equal;
2977 str = "{ [m,n] -> -m * n }";
2978 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2979 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2980 str = "{ infty }";
2981 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2982 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2983 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2984 isl_pw_qpolynomial_fold_free(pwf);
2985 isl_pw_qpolynomial_fold_free(pwf2);
2987 if (equal < 0)
2988 return -1;
2989 if (!equal)
2990 isl_die(ctx, isl_error_unknown,
2991 "expecting infinite polynomial bound", return -1);
2993 return 0;
2996 static int test_bound(isl_ctx *ctx)
2998 const char *str;
2999 unsigned dim;
3000 isl_pw_qpolynomial *pwqp;
3001 isl_pw_qpolynomial_fold *pwf;
3003 if (test_bound_unbounded_domain(ctx) < 0)
3004 return -1;
3006 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3007 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3008 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3009 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3010 isl_pw_qpolynomial_fold_free(pwf);
3011 if (dim != 4)
3012 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3013 return -1);
3015 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3016 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3017 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3018 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3019 isl_pw_qpolynomial_fold_free(pwf);
3020 if (dim != 1)
3021 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3022 return -1);
3024 return 0;
3027 static int test_lift(isl_ctx *ctx)
3029 const char *str;
3030 isl_basic_map *bmap;
3031 isl_basic_set *bset;
3033 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3034 bset = isl_basic_set_read_from_str(ctx, str);
3035 bset = isl_basic_set_lift(bset);
3036 bmap = isl_basic_map_from_range(bset);
3037 bset = isl_basic_map_domain(bmap);
3038 isl_basic_set_free(bset);
3040 return 0;
3043 struct {
3044 const char *set1;
3045 const char *set2;
3046 int subset;
3047 } subset_tests[] = {
3048 { "{ [112, 0] }",
3049 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3050 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3051 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3052 { "{ [65] }",
3053 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3054 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3055 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3056 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3057 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3058 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3059 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3060 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3061 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3062 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3063 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3064 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3065 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3066 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3067 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3068 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3069 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3070 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3071 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3072 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3073 "4e0 <= -57 + i0 + i1)) or "
3074 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3075 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3076 "4e0 >= -61 + i0 + i1)) or "
3077 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3078 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3081 static int test_subset(isl_ctx *ctx)
3083 int i;
3084 isl_set *set1, *set2;
3085 int subset;
3087 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3088 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3089 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3090 subset = isl_set_is_subset(set1, set2);
3091 isl_set_free(set1);
3092 isl_set_free(set2);
3093 if (subset < 0)
3094 return -1;
3095 if (subset != subset_tests[i].subset)
3096 isl_die(ctx, isl_error_unknown,
3097 "incorrect subset result", return -1);
3100 return 0;
3103 struct {
3104 const char *minuend;
3105 const char *subtrahend;
3106 const char *difference;
3107 } subtract_domain_tests[] = {
3108 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3109 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3110 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3113 static int test_subtract(isl_ctx *ctx)
3115 int i;
3116 isl_union_map *umap1, *umap2;
3117 isl_union_pw_multi_aff *upma1, *upma2;
3118 isl_union_set *uset;
3119 int equal;
3121 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3122 umap1 = isl_union_map_read_from_str(ctx,
3123 subtract_domain_tests[i].minuend);
3124 uset = isl_union_set_read_from_str(ctx,
3125 subtract_domain_tests[i].subtrahend);
3126 umap2 = isl_union_map_read_from_str(ctx,
3127 subtract_domain_tests[i].difference);
3128 umap1 = isl_union_map_subtract_domain(umap1, uset);
3129 equal = isl_union_map_is_equal(umap1, umap2);
3130 isl_union_map_free(umap1);
3131 isl_union_map_free(umap2);
3132 if (equal < 0)
3133 return -1;
3134 if (!equal)
3135 isl_die(ctx, isl_error_unknown,
3136 "incorrect subtract domain result", return -1);
3139 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3140 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3141 subtract_domain_tests[i].minuend);
3142 uset = isl_union_set_read_from_str(ctx,
3143 subtract_domain_tests[i].subtrahend);
3144 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3145 subtract_domain_tests[i].difference);
3146 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3147 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3148 isl_union_pw_multi_aff_free(upma1);
3149 isl_union_pw_multi_aff_free(upma2);
3150 if (equal < 0)
3151 return -1;
3152 if (!equal)
3153 isl_die(ctx, isl_error_unknown,
3154 "incorrect subtract domain result", return -1);
3157 return 0;
3160 /* Check that intersecting the empty basic set with another basic set
3161 * does not increase the number of constraints. In particular,
3162 * the empty basic set should maintain its canonical representation.
3164 static int test_intersect(isl_ctx *ctx)
3166 int n1, n2;
3167 isl_basic_set *bset1, *bset2;
3169 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3170 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3171 n1 = isl_basic_set_n_constraint(bset1);
3172 bset1 = isl_basic_set_intersect(bset1, bset2);
3173 n2 = isl_basic_set_n_constraint(bset1);
3174 isl_basic_set_free(bset1);
3175 if (!bset1)
3176 return -1;
3177 if (n1 != n2)
3178 isl_die(ctx, isl_error_unknown,
3179 "number of constraints of empty set changed",
3180 return -1);
3182 return 0;
3185 int test_factorize(isl_ctx *ctx)
3187 const char *str;
3188 isl_basic_set *bset;
3189 isl_factorizer *f;
3191 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3192 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3193 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3194 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3195 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3196 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3197 "3i5 >= -2i0 - i2 + 3i4 }";
3198 bset = isl_basic_set_read_from_str(ctx, str);
3199 f = isl_basic_set_factorizer(bset);
3200 isl_basic_set_free(bset);
3201 isl_factorizer_free(f);
3202 if (!f)
3203 isl_die(ctx, isl_error_unknown,
3204 "failed to construct factorizer", return -1);
3206 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3207 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3208 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3209 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3210 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3211 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3212 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3213 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3214 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3215 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3216 bset = isl_basic_set_read_from_str(ctx, str);
3217 f = isl_basic_set_factorizer(bset);
3218 isl_basic_set_free(bset);
3219 isl_factorizer_free(f);
3220 if (!f)
3221 isl_die(ctx, isl_error_unknown,
3222 "failed to construct factorizer", return -1);
3224 return 0;
3227 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3229 int *injective = user;
3231 *injective = isl_map_is_injective(map);
3232 isl_map_free(map);
3234 if (*injective < 0 || !*injective)
3235 return isl_stat_error;
3237 return isl_stat_ok;
3240 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3241 const char *r, const char *s, int tilable, int parallel)
3243 int i;
3244 isl_union_set *D;
3245 isl_union_map *W, *R, *S;
3246 isl_union_map *empty;
3247 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3248 isl_union_map *validity, *proximity, *coincidence;
3249 isl_union_map *schedule;
3250 isl_union_map *test;
3251 isl_union_set *delta;
3252 isl_union_set *domain;
3253 isl_set *delta_set;
3254 isl_set *slice;
3255 isl_set *origin;
3256 isl_schedule_constraints *sc;
3257 isl_schedule *sched;
3258 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3260 D = isl_union_set_read_from_str(ctx, d);
3261 W = isl_union_map_read_from_str(ctx, w);
3262 R = isl_union_map_read_from_str(ctx, r);
3263 S = isl_union_map_read_from_str(ctx, s);
3265 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3266 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3268 empty = isl_union_map_empty(isl_union_map_get_space(S));
3269 isl_union_map_compute_flow(isl_union_map_copy(R),
3270 isl_union_map_copy(W), empty,
3271 isl_union_map_copy(S),
3272 &dep_raw, NULL, NULL, NULL);
3273 isl_union_map_compute_flow(isl_union_map_copy(W),
3274 isl_union_map_copy(W),
3275 isl_union_map_copy(R),
3276 isl_union_map_copy(S),
3277 &dep_waw, &dep_war, NULL, NULL);
3279 dep = isl_union_map_union(dep_waw, dep_war);
3280 dep = isl_union_map_union(dep, dep_raw);
3281 validity = isl_union_map_copy(dep);
3282 coincidence = isl_union_map_copy(dep);
3283 proximity = isl_union_map_copy(dep);
3285 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3286 sc = isl_schedule_constraints_set_validity(sc, validity);
3287 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3288 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3289 sched = isl_schedule_constraints_compute_schedule(sc);
3290 schedule = isl_schedule_get_map(sched);
3291 isl_schedule_free(sched);
3292 isl_union_map_free(W);
3293 isl_union_map_free(R);
3294 isl_union_map_free(S);
3296 is_injection = 1;
3297 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3299 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3300 is_complete = isl_union_set_is_subset(D, domain);
3301 isl_union_set_free(D);
3302 isl_union_set_free(domain);
3304 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3305 test = isl_union_map_apply_range(test, dep);
3306 test = isl_union_map_apply_range(test, schedule);
3308 delta = isl_union_map_deltas(test);
3309 if (isl_union_set_n_set(delta) == 0) {
3310 is_tilable = 1;
3311 is_parallel = 1;
3312 is_nonneg = 1;
3313 isl_union_set_free(delta);
3314 } else {
3315 delta_set = isl_set_from_union_set(delta);
3317 slice = isl_set_universe(isl_set_get_space(delta_set));
3318 for (i = 0; i < tilable; ++i)
3319 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3320 is_tilable = isl_set_is_subset(delta_set, slice);
3321 isl_set_free(slice);
3323 slice = isl_set_universe(isl_set_get_space(delta_set));
3324 for (i = 0; i < parallel; ++i)
3325 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3326 is_parallel = isl_set_is_subset(delta_set, slice);
3327 isl_set_free(slice);
3329 origin = isl_set_universe(isl_set_get_space(delta_set));
3330 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3331 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3333 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3334 delta_set = isl_set_lexmin(delta_set);
3336 is_nonneg = isl_set_is_equal(delta_set, origin);
3338 isl_set_free(origin);
3339 isl_set_free(delta_set);
3342 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3343 is_injection < 0 || is_complete < 0)
3344 return -1;
3345 if (!is_complete)
3346 isl_die(ctx, isl_error_unknown,
3347 "generated schedule incomplete", return -1);
3348 if (!is_injection)
3349 isl_die(ctx, isl_error_unknown,
3350 "generated schedule not injective on each statement",
3351 return -1);
3352 if (!is_nonneg)
3353 isl_die(ctx, isl_error_unknown,
3354 "negative dependences in generated schedule",
3355 return -1);
3356 if (!is_tilable)
3357 isl_die(ctx, isl_error_unknown,
3358 "generated schedule not as tilable as expected",
3359 return -1);
3360 if (!is_parallel)
3361 isl_die(ctx, isl_error_unknown,
3362 "generated schedule not as parallel as expected",
3363 return -1);
3365 return 0;
3368 /* Compute a schedule for the given instance set, validity constraints,
3369 * proximity constraints and context and return a corresponding union map
3370 * representation.
3372 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3373 const char *domain, const char *validity, const char *proximity,
3374 const char *context)
3376 isl_set *con;
3377 isl_union_set *dom;
3378 isl_union_map *dep;
3379 isl_union_map *prox;
3380 isl_schedule_constraints *sc;
3381 isl_schedule *schedule;
3382 isl_union_map *sched;
3384 con = isl_set_read_from_str(ctx, context);
3385 dom = isl_union_set_read_from_str(ctx, domain);
3386 dep = isl_union_map_read_from_str(ctx, validity);
3387 prox = isl_union_map_read_from_str(ctx, proximity);
3388 sc = isl_schedule_constraints_on_domain(dom);
3389 sc = isl_schedule_constraints_set_context(sc, con);
3390 sc = isl_schedule_constraints_set_validity(sc, dep);
3391 sc = isl_schedule_constraints_set_proximity(sc, prox);
3392 schedule = isl_schedule_constraints_compute_schedule(sc);
3393 sched = isl_schedule_get_map(schedule);
3394 isl_schedule_free(schedule);
3396 return sched;
3399 /* Compute a schedule for the given instance set, validity constraints and
3400 * proximity constraints and return a corresponding union map representation.
3402 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3403 const char *domain, const char *validity, const char *proximity)
3405 return compute_schedule_with_context(ctx, domain, validity, proximity,
3406 "{ : }");
3409 /* Check that a schedule can be constructed on the given domain
3410 * with the given validity and proximity constraints.
3412 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3413 const char *validity, const char *proximity)
3415 isl_union_map *sched;
3417 sched = compute_schedule(ctx, domain, validity, proximity);
3418 if (!sched)
3419 return -1;
3421 isl_union_map_free(sched);
3422 return 0;
3425 int test_special_schedule(isl_ctx *ctx, const char *domain,
3426 const char *validity, const char *proximity, const char *expected_sched)
3428 isl_union_map *sched1, *sched2;
3429 int equal;
3431 sched1 = compute_schedule(ctx, domain, validity, proximity);
3432 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3434 equal = isl_union_map_is_equal(sched1, sched2);
3435 isl_union_map_free(sched1);
3436 isl_union_map_free(sched2);
3438 if (equal < 0)
3439 return -1;
3440 if (!equal)
3441 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3442 return -1);
3444 return 0;
3447 /* Check that the schedule map is properly padded, even after being
3448 * reconstructed from the band forest.
3450 static int test_padded_schedule(isl_ctx *ctx)
3452 const char *str;
3453 isl_union_set *D;
3454 isl_union_map *validity, *proximity;
3455 isl_schedule_constraints *sc;
3456 isl_schedule *sched;
3457 isl_union_map *map1, *map2;
3458 isl_band_list *list;
3459 int equal;
3461 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3462 D = isl_union_set_read_from_str(ctx, str);
3463 validity = isl_union_map_empty(isl_union_set_get_space(D));
3464 proximity = isl_union_map_copy(validity);
3465 sc = isl_schedule_constraints_on_domain(D);
3466 sc = isl_schedule_constraints_set_validity(sc, validity);
3467 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3468 sched = isl_schedule_constraints_compute_schedule(sc);
3469 map1 = isl_schedule_get_map(sched);
3470 list = isl_schedule_get_band_forest(sched);
3471 isl_band_list_free(list);
3472 map2 = isl_schedule_get_map(sched);
3473 isl_schedule_free(sched);
3474 equal = isl_union_map_is_equal(map1, map2);
3475 isl_union_map_free(map1);
3476 isl_union_map_free(map2);
3478 if (equal < 0)
3479 return -1;
3480 if (!equal)
3481 isl_die(ctx, isl_error_unknown,
3482 "reconstructed schedule map not the same as original",
3483 return -1);
3485 return 0;
3488 /* Check that conditional validity constraints are also taken into
3489 * account across bands.
3490 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3491 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3492 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3493 * is enforced by the rest of the schedule.
3495 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3497 const char *str;
3498 isl_union_set *domain;
3499 isl_union_map *validity, *proximity, *condition;
3500 isl_union_map *sink, *source, *dep;
3501 isl_schedule_constraints *sc;
3502 isl_schedule *schedule;
3503 isl_union_access_info *access;
3504 isl_union_flow *flow;
3505 int empty;
3507 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3508 "A[k] : k >= 1 and k <= -1 + n; "
3509 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3510 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3511 domain = isl_union_set_read_from_str(ctx, str);
3512 sc = isl_schedule_constraints_on_domain(domain);
3513 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3514 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3515 "D[k, i] -> C[1 + k, i] : "
3516 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3517 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3518 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3519 validity = isl_union_map_read_from_str(ctx, str);
3520 sc = isl_schedule_constraints_set_validity(sc, validity);
3521 str = "[n] -> { C[k, i] -> D[k, i] : "
3522 "0 <= i <= -1 + k and k <= -1 + n }";
3523 proximity = isl_union_map_read_from_str(ctx, str);
3524 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3525 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3526 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3527 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3528 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3529 condition = isl_union_map_read_from_str(ctx, str);
3530 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3531 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3532 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3533 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3534 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3535 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3536 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3537 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3538 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3539 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3540 validity = isl_union_map_read_from_str(ctx, str);
3541 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3542 validity);
3543 schedule = isl_schedule_constraints_compute_schedule(sc);
3544 str = "{ D[2,0] -> [] }";
3545 sink = isl_union_map_read_from_str(ctx, str);
3546 access = isl_union_access_info_from_sink(sink);
3547 str = "{ C[2,1] -> [] }";
3548 source = isl_union_map_read_from_str(ctx, str);
3549 access = isl_union_access_info_set_must_source(access, source);
3550 access = isl_union_access_info_set_schedule(access, schedule);
3551 flow = isl_union_access_info_compute_flow(access);
3552 dep = isl_union_flow_get_must_dependence(flow);
3553 isl_union_flow_free(flow);
3554 empty = isl_union_map_is_empty(dep);
3555 isl_union_map_free(dep);
3557 if (empty < 0)
3558 return -1;
3559 if (empty)
3560 isl_die(ctx, isl_error_unknown,
3561 "conditional validity not respected", return -1);
3563 return 0;
3566 /* Input for testing of schedule construction based on
3567 * conditional constraints.
3569 * domain is the iteration domain
3570 * flow are the flow dependences, which determine the validity and
3571 * proximity constraints
3572 * condition are the conditions on the conditional validity constraints
3573 * conditional_validity are the conditional validity constraints
3574 * outer_band_n is the expected number of members in the outer band
3576 struct {
3577 const char *domain;
3578 const char *flow;
3579 const char *condition;
3580 const char *conditional_validity;
3581 int outer_band_n;
3582 } live_range_tests[] = {
3583 /* Contrived example that illustrates that we need to keep
3584 * track of tagged condition dependences and
3585 * tagged conditional validity dependences
3586 * in isl_sched_edge separately.
3587 * In particular, the conditional validity constraints on A
3588 * cannot be satisfied,
3589 * but they can be ignored because there are no corresponding
3590 * condition constraints. However, we do have an additional
3591 * conditional validity constraint that maps to the same
3592 * dependence relation
3593 * as the condition constraint on B. If we did not make a distinction
3594 * between tagged condition and tagged conditional validity
3595 * dependences, then we
3596 * could end up treating this shared dependence as an condition
3597 * constraint on A, forcing a localization of the conditions,
3598 * which is impossible.
3600 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3601 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3602 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3603 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3604 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3605 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3608 /* TACO 2013 Fig. 7 */
3609 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3610 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3611 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3612 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3613 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3614 "0 <= i < n and 0 <= j < n - 1 }",
3615 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3616 "0 <= i < n and 0 <= j < j' < n;"
3617 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3618 "0 <= i < i' < n and 0 <= j,j' < n;"
3619 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3620 "0 <= i,j,j' < n and j < j' }",
3623 /* TACO 2013 Fig. 7, without tags */
3624 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3625 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3626 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3627 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3628 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3629 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3630 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3631 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3634 /* TACO 2013 Fig. 12 */
3635 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3636 "S3[i,3] : 0 <= i <= 1 }",
3637 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3638 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3639 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3640 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3641 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3642 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3643 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3644 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3645 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3646 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3647 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3652 /* Test schedule construction based on conditional constraints.
3653 * In particular, check the number of members in the outer band node
3654 * as an indication of whether tiling is possible or not.
3656 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3658 int i;
3659 isl_union_set *domain;
3660 isl_union_map *condition;
3661 isl_union_map *flow;
3662 isl_union_map *validity;
3663 isl_schedule_constraints *sc;
3664 isl_schedule *schedule;
3665 isl_schedule_node *node;
3666 int n_member;
3668 if (test_special_conditional_schedule_constraints(ctx) < 0)
3669 return -1;
3671 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3672 domain = isl_union_set_read_from_str(ctx,
3673 live_range_tests[i].domain);
3674 flow = isl_union_map_read_from_str(ctx,
3675 live_range_tests[i].flow);
3676 condition = isl_union_map_read_from_str(ctx,
3677 live_range_tests[i].condition);
3678 validity = isl_union_map_read_from_str(ctx,
3679 live_range_tests[i].conditional_validity);
3680 sc = isl_schedule_constraints_on_domain(domain);
3681 sc = isl_schedule_constraints_set_validity(sc,
3682 isl_union_map_copy(flow));
3683 sc = isl_schedule_constraints_set_proximity(sc, flow);
3684 sc = isl_schedule_constraints_set_conditional_validity(sc,
3685 condition, validity);
3686 schedule = isl_schedule_constraints_compute_schedule(sc);
3687 node = isl_schedule_get_root(schedule);
3688 while (node &&
3689 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3690 node = isl_schedule_node_first_child(node);
3691 n_member = isl_schedule_node_band_n_member(node);
3692 isl_schedule_node_free(node);
3693 isl_schedule_free(schedule);
3695 if (!schedule)
3696 return -1;
3697 if (n_member != live_range_tests[i].outer_band_n)
3698 isl_die(ctx, isl_error_unknown,
3699 "unexpected number of members in outer band",
3700 return -1);
3702 return 0;
3705 /* Check that the schedule computed for the given instance set and
3706 * dependence relation strongly satisfies the dependences.
3707 * In particular, check that no instance is scheduled before
3708 * or together with an instance on which it depends.
3709 * Earlier versions of isl would produce a schedule that
3710 * only weakly satisfies the dependences.
3712 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3714 const char *domain, *dep;
3715 isl_union_map *D, *schedule;
3716 isl_map *map, *ge;
3717 int empty;
3719 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3720 "A[i0] : 0 <= i0 <= 1 }";
3721 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3722 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3723 schedule = compute_schedule(ctx, domain, dep, dep);
3724 D = isl_union_map_read_from_str(ctx, dep);
3725 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3726 D = isl_union_map_apply_range(D, schedule);
3727 map = isl_map_from_union_map(D);
3728 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3729 map = isl_map_intersect(map, ge);
3730 empty = isl_map_is_empty(map);
3731 isl_map_free(map);
3733 if (empty < 0)
3734 return -1;
3735 if (!empty)
3736 isl_die(ctx, isl_error_unknown,
3737 "dependences not strongly satisfied", return -1);
3739 return 0;
3742 /* Compute a schedule for input where the instance set constraints
3743 * conflict with the context constraints.
3744 * Earlier versions of isl did not properly handle this situation.
3746 static int test_conflicting_context_schedule(isl_ctx *ctx)
3748 isl_union_map *schedule;
3749 const char *domain, *context;
3751 domain = "[n] -> { A[] : n >= 0 }";
3752 context = "[n] -> { : n < 0 }";
3753 schedule = compute_schedule_with_context(ctx,
3754 domain, "{}", "{}", context);
3755 isl_union_map_free(schedule);
3757 if (!schedule)
3758 return -1;
3760 return 0;
3763 /* Check that the dependence carrying step is not confused by
3764 * a bound on the coefficient size.
3765 * In particular, force the scheduler to move to a dependence carrying
3766 * step by demanding outer coincidence and bound the size of
3767 * the coefficients. Earlier versions of isl would take this
3768 * bound into account while carrying dependences, breaking
3769 * fundamental assumptions.
3770 * On the other hand, the dependence carrying step now tries
3771 * to prevent loop coalescing by default, so check that indeed
3772 * no loop coalescing occurs by comparing the computed schedule
3773 * to the expected non-coalescing schedule.
3775 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3777 const char *domain, *dep;
3778 isl_union_set *I;
3779 isl_union_map *D;
3780 isl_schedule_constraints *sc;
3781 isl_schedule *schedule;
3782 isl_union_map *sched1, *sched2;
3783 isl_bool equal;
3785 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3786 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3787 "i1 <= -2 + i0; "
3788 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3789 I = isl_union_set_read_from_str(ctx, domain);
3790 D = isl_union_map_read_from_str(ctx, dep);
3791 sc = isl_schedule_constraints_on_domain(I);
3792 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3793 sc = isl_schedule_constraints_set_coincidence(sc, D);
3794 isl_options_set_schedule_outer_coincidence(ctx, 1);
3795 isl_options_set_schedule_max_coefficient(ctx, 20);
3796 schedule = isl_schedule_constraints_compute_schedule(sc);
3797 isl_options_set_schedule_max_coefficient(ctx, -1);
3798 isl_options_set_schedule_outer_coincidence(ctx, 0);
3799 sched1 = isl_schedule_get_map(schedule);
3800 isl_schedule_free(schedule);
3802 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
3803 equal = isl_union_map_is_equal(sched1, sched2);
3804 isl_union_map_free(sched1);
3805 isl_union_map_free(sched2);
3807 if (equal < 0)
3808 return -1;
3809 if (!equal)
3810 isl_die(ctx, isl_error_unknown,
3811 "unexpected schedule", return -1);
3813 return 0;
3816 /* Check that a set of schedule constraints that only allow for
3817 * a coalescing schedule still produces a schedule even if the user
3818 * request a non-coalescing schedule. Earlier versions of isl
3819 * would not handle this case correctly.
3821 static int test_coalescing_schedule(isl_ctx *ctx)
3823 const char *domain, *dep;
3824 isl_union_set *I;
3825 isl_union_map *D;
3826 isl_schedule_constraints *sc;
3827 isl_schedule *schedule;
3828 int treat_coalescing;
3830 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
3831 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
3832 I = isl_union_set_read_from_str(ctx, domain);
3833 D = isl_union_map_read_from_str(ctx, dep);
3834 sc = isl_schedule_constraints_on_domain(I);
3835 sc = isl_schedule_constraints_set_validity(sc, D);
3836 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
3837 isl_options_set_schedule_treat_coalescing(ctx, 1);
3838 schedule = isl_schedule_constraints_compute_schedule(sc);
3839 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
3840 isl_schedule_free(schedule);
3841 if (!schedule)
3842 return -1;
3843 return 0;
3846 int test_schedule(isl_ctx *ctx)
3848 const char *D, *W, *R, *V, *P, *S;
3849 int max_coincidence;
3850 int treat_coalescing;
3852 /* Handle resulting schedule with zero bands. */
3853 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3854 return -1;
3856 /* Jacobi */
3857 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3858 W = "{ S1[t,i] -> a[t,i] }";
3859 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3860 "S1[t,i] -> a[t-1,i+1] }";
3861 S = "{ S1[t,i] -> [t,i] }";
3862 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3863 return -1;
3865 /* Fig. 5 of CC2008 */
3866 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3867 "j <= -1 + N }";
3868 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3869 "j >= 2 and j <= -1 + N }";
3870 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3871 "j >= 2 and j <= -1 + N; "
3872 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3873 "j >= 2 and j <= -1 + N }";
3874 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3875 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3876 return -1;
3878 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3879 W = "{ S1[i] -> a[i] }";
3880 R = "{ S2[i] -> a[i+1] }";
3881 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3882 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3883 return -1;
3885 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3886 W = "{ S1[i] -> a[i] }";
3887 R = "{ S2[i] -> a[9-i] }";
3888 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3889 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3890 return -1;
3892 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3893 W = "{ S1[i] -> a[i] }";
3894 R = "[N] -> { S2[i] -> a[N-1-i] }";
3895 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3896 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3897 return -1;
3899 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3900 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3901 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3902 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3903 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3904 return -1;
3906 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3907 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3908 R = "{ S2[i,j] -> a[i-1,j] }";
3909 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3910 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3911 return -1;
3913 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3914 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3915 R = "{ S2[i,j] -> a[i,j-1] }";
3916 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3917 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3918 return -1;
3920 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3921 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3922 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3923 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3924 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3925 "S_0[] -> [0, 0, 0] }";
3926 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3927 return -1;
3928 ctx->opt->schedule_parametric = 0;
3929 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3930 return -1;
3931 ctx->opt->schedule_parametric = 1;
3933 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3934 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3935 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3936 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3937 "S4[i] -> a[i,N] }";
3938 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3939 "S4[i] -> [4,i,0] }";
3940 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
3941 isl_options_set_schedule_maximize_coincidence(ctx, 0);
3942 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3943 return -1;
3944 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
3946 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3947 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3948 "j <= N }";
3949 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3950 "j <= N; "
3951 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3952 "j <= N }";
3953 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3954 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3955 return -1;
3957 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3958 " S_2[t] : t >= 0 and t <= -1 + N; "
3959 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3960 "i <= -1 + N }";
3961 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3962 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3963 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3964 "i >= 0 and i <= -1 + N }";
3965 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3966 "i >= 0 and i <= -1 + N; "
3967 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3968 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3969 " S_0[t] -> [0, t, 0] }";
3971 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3972 return -1;
3973 ctx->opt->schedule_parametric = 0;
3974 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3975 return -1;
3976 ctx->opt->schedule_parametric = 1;
3978 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3979 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3980 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3981 return -1;
3983 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3984 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3985 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3986 "j >= 0 and j <= -1 + N; "
3987 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3988 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3989 "j >= 0 and j <= -1 + N; "
3990 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3991 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3992 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3993 return -1;
3995 D = "{ S_0[i] : i >= 0 }";
3996 W = "{ S_0[i] -> a[i] : i >= 0 }";
3997 R = "{ S_0[i] -> a[0] : i >= 0 }";
3998 S = "{ S_0[i] -> [0, i, 0] }";
3999 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4000 return -1;
4002 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4003 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4004 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4005 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4006 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4007 return -1;
4009 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4010 "k <= -1 + n and k >= 0 }";
4011 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4012 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4013 "k <= -1 + n and k >= 0; "
4014 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4015 "k <= -1 + n and k >= 0; "
4016 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4017 "k <= -1 + n and k >= 0 }";
4018 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4019 ctx->opt->schedule_outer_coincidence = 1;
4020 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4021 return -1;
4022 ctx->opt->schedule_outer_coincidence = 0;
4024 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4025 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4026 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4027 "Stmt_for_body24[i0, i1, 1, 0]:"
4028 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4029 "Stmt_for_body7[i0, i1, i2]:"
4030 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4031 "i2 <= 7 }";
4033 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4034 "Stmt_for_body24[1, i1, i2, i3]:"
4035 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4036 "i2 >= 1;"
4037 "Stmt_for_body24[0, i1, i2, i3] -> "
4038 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4039 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4040 "i3 >= 0;"
4041 "Stmt_for_body24[0, i1, i2, i3] ->"
4042 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4043 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4044 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4045 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4046 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4047 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4048 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4049 "i1 <= 6 and i1 >= 0;"
4050 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4051 "Stmt_for_body7[i0, i1, i2] -> "
4052 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4053 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4054 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4055 "Stmt_for_body7[i0, i1, i2] -> "
4056 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4057 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4058 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4059 P = V;
4060 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
4061 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
4062 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
4064 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4065 isl_options_set_schedule_treat_coalescing(ctx, 0);
4066 if (test_special_schedule(ctx, D, V, P, S) < 0)
4067 return -1;
4068 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4070 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4071 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4072 "j >= 1 and j <= 7;"
4073 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4074 "j >= 1 and j <= 8 }";
4075 P = "{ }";
4076 S = "{ S_0[i, j] -> [i + j, j] }";
4077 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4078 if (test_special_schedule(ctx, D, V, P, S) < 0)
4079 return -1;
4080 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4082 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4083 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4084 "j >= 0 and j <= -1 + i }";
4085 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4086 "i <= -1 + N and j >= 0;"
4087 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4088 "i <= -2 + N }";
4089 P = "{ }";
4090 S = "{ S_0[i, j] -> [i, j] }";
4091 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4092 if (test_special_schedule(ctx, D, V, P, S) < 0)
4093 return -1;
4094 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4096 /* Test both algorithms on a case with only proximity dependences. */
4097 D = "{ S[i,j] : 0 <= i <= 10 }";
4098 V = "{ }";
4099 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4100 S = "{ S[i, j] -> [j, i] }";
4101 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4102 if (test_special_schedule(ctx, D, V, P, S) < 0)
4103 return -1;
4104 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4105 if (test_special_schedule(ctx, D, V, P, S) < 0)
4106 return -1;
4108 D = "{ A[a]; B[] }";
4109 V = "{}";
4110 P = "{ A[a] -> B[] }";
4111 if (test_has_schedule(ctx, D, V, P) < 0)
4112 return -1;
4114 if (test_padded_schedule(ctx) < 0)
4115 return -1;
4117 /* Check that check for progress is not confused by rational
4118 * solution.
4120 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4121 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4122 "i0 <= -2 + N; "
4123 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4124 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4125 P = "{}";
4126 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4127 if (test_has_schedule(ctx, D, V, P) < 0)
4128 return -1;
4129 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4131 /* Check that we allow schedule rows that are only non-trivial
4132 * on some full-dimensional domains.
4134 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4135 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4136 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4137 P = "{}";
4138 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4139 if (test_has_schedule(ctx, D, V, P) < 0)
4140 return -1;
4141 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4143 if (test_conditional_schedule_constraints(ctx) < 0)
4144 return -1;
4146 if (test_strongly_satisfying_schedule(ctx) < 0)
4147 return -1;
4149 if (test_conflicting_context_schedule(ctx) < 0)
4150 return -1;
4152 if (test_bounded_coefficients_schedule(ctx) < 0)
4153 return -1;
4154 if (test_coalescing_schedule(ctx) < 0)
4155 return -1;
4157 return 0;
4160 /* Perform scheduling tests using the whole component scheduler.
4162 static int test_schedule_whole(isl_ctx *ctx)
4164 int whole;
4165 int r;
4167 whole = isl_options_get_schedule_whole_component(ctx);
4168 isl_options_set_schedule_whole_component(ctx, 1);
4169 r = test_schedule(ctx);
4170 isl_options_set_schedule_whole_component(ctx, whole);
4172 return r;
4175 /* Perform scheduling tests using the incremental scheduler.
4177 static int test_schedule_incremental(isl_ctx *ctx)
4179 int whole;
4180 int r;
4182 whole = isl_options_get_schedule_whole_component(ctx);
4183 isl_options_set_schedule_whole_component(ctx, 0);
4184 r = test_schedule(ctx);
4185 isl_options_set_schedule_whole_component(ctx, whole);
4187 return r;
4190 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4192 isl_union_map *umap;
4193 int test;
4195 umap = isl_union_map_read_from_str(ctx, str);
4196 test = isl_union_map_plain_is_injective(umap);
4197 isl_union_map_free(umap);
4198 if (test < 0)
4199 return -1;
4200 if (test == injective)
4201 return 0;
4202 if (injective)
4203 isl_die(ctx, isl_error_unknown,
4204 "map not detected as injective", return -1);
4205 else
4206 isl_die(ctx, isl_error_unknown,
4207 "map detected as injective", return -1);
4210 int test_injective(isl_ctx *ctx)
4212 const char *str;
4214 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4215 return -1;
4216 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4217 return -1;
4218 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4219 return -1;
4220 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4221 return -1;
4222 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4223 return -1;
4224 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4225 return -1;
4226 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4227 return -1;
4228 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4229 return -1;
4231 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4232 if (test_plain_injective(ctx, str, 1))
4233 return -1;
4234 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4235 if (test_plain_injective(ctx, str, 0))
4236 return -1;
4238 return 0;
4241 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4243 isl_aff *aff2;
4244 int equal;
4246 if (!aff)
4247 return -1;
4249 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4250 equal = isl_aff_plain_is_equal(aff, aff2);
4251 isl_aff_free(aff2);
4253 return equal;
4256 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4258 int equal;
4260 equal = aff_plain_is_equal(aff, str);
4261 if (equal < 0)
4262 return -1;
4263 if (!equal)
4264 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4265 "result not as expected", return -1);
4266 return 0;
4269 struct {
4270 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4271 __isl_take isl_aff *aff2);
4272 } aff_bin_op[] = {
4273 ['+'] = { &isl_aff_add },
4274 ['-'] = { &isl_aff_sub },
4275 ['*'] = { &isl_aff_mul },
4276 ['/'] = { &isl_aff_div },
4279 struct {
4280 const char *arg1;
4281 unsigned char op;
4282 const char *arg2;
4283 const char *res;
4284 } aff_bin_tests[] = {
4285 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4286 "{ [i] -> [2i] }" },
4287 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4288 "{ [i] -> [0] }" },
4289 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4290 "{ [i] -> [2i] }" },
4291 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4292 "{ [i] -> [2i] }" },
4293 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4294 "{ [i] -> [i/2] }" },
4295 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4296 "{ [i] -> [i] }" },
4297 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4298 "{ [i] -> [NaN] }" },
4299 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4300 "{ [i] -> [NaN] }" },
4301 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4302 "{ [i] -> [NaN] }" },
4303 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4304 "{ [i] -> [NaN] }" },
4305 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4306 "{ [i] -> [NaN] }" },
4307 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4308 "{ [i] -> [NaN] }" },
4309 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4310 "{ [i] -> [NaN] }" },
4311 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4312 "{ [i] -> [NaN] }" },
4313 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4314 "{ [i] -> [NaN] }" },
4315 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4316 "{ [i] -> [NaN] }" },
4317 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4318 "{ [i] -> [NaN] }" },
4319 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4320 "{ [i] -> [NaN] }" },
4323 /* Perform some basic tests of binary operations on isl_aff objects.
4325 static int test_bin_aff(isl_ctx *ctx)
4327 int i;
4328 isl_aff *aff1, *aff2, *res;
4329 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4330 __isl_take isl_aff *aff2);
4331 int ok;
4333 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4334 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4335 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4336 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4337 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4338 aff1 = fn(aff1, aff2);
4339 if (isl_aff_is_nan(res))
4340 ok = isl_aff_is_nan(aff1);
4341 else
4342 ok = isl_aff_plain_is_equal(aff1, res);
4343 isl_aff_free(aff1);
4344 isl_aff_free(res);
4345 if (ok < 0)
4346 return -1;
4347 if (!ok)
4348 isl_die(ctx, isl_error_unknown,
4349 "unexpected result", return -1);
4352 return 0;
4355 struct {
4356 __isl_give isl_union_pw_multi_aff *(*fn)(
4357 __isl_take isl_union_pw_multi_aff *upma1,
4358 __isl_take isl_union_pw_multi_aff *upma2);
4359 const char *arg1;
4360 const char *arg2;
4361 const char *res;
4362 } upma_bin_tests[] = {
4363 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4364 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4365 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4366 "{ B[x] -> [2] : x >= 0 }",
4367 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4368 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4369 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4370 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4371 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4372 "D[i] -> B[2] : i >= 5 }" },
4373 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4374 "{ B[x] -> C[2] : x > 0 }",
4375 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4376 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4377 "{ B[x] -> A[2] : x >= 0 }",
4378 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4381 /* Perform some basic tests of binary operations on
4382 * isl_union_pw_multi_aff objects.
4384 static int test_bin_upma(isl_ctx *ctx)
4386 int i;
4387 isl_union_pw_multi_aff *upma1, *upma2, *res;
4388 int ok;
4390 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4391 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4392 upma_bin_tests[i].arg1);
4393 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4394 upma_bin_tests[i].arg2);
4395 res = isl_union_pw_multi_aff_read_from_str(ctx,
4396 upma_bin_tests[i].res);
4397 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4398 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4399 isl_union_pw_multi_aff_free(upma1);
4400 isl_union_pw_multi_aff_free(res);
4401 if (ok < 0)
4402 return -1;
4403 if (!ok)
4404 isl_die(ctx, isl_error_unknown,
4405 "unexpected result", return -1);
4408 return 0;
4411 struct {
4412 __isl_give isl_union_pw_multi_aff *(*fn)(
4413 __isl_take isl_union_pw_multi_aff *upma1,
4414 __isl_take isl_union_pw_multi_aff *upma2);
4415 const char *arg1;
4416 const char *arg2;
4417 } upma_bin_fail_tests[] = {
4418 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4419 "{ B[x] -> C[2] : x >= 0 }" },
4422 /* Perform some basic tests of binary operations on
4423 * isl_union_pw_multi_aff objects that are expected to fail.
4425 static int test_bin_upma_fail(isl_ctx *ctx)
4427 int i, n;
4428 isl_union_pw_multi_aff *upma1, *upma2;
4429 int on_error;
4431 on_error = isl_options_get_on_error(ctx);
4432 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4433 n = ARRAY_SIZE(upma_bin_fail_tests);
4434 for (i = 0; i < n; ++i) {
4435 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4436 upma_bin_fail_tests[i].arg1);
4437 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4438 upma_bin_fail_tests[i].arg2);
4439 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4440 isl_union_pw_multi_aff_free(upma1);
4441 if (upma1)
4442 break;
4444 isl_options_set_on_error(ctx, on_error);
4445 if (i < n)
4446 isl_die(ctx, isl_error_unknown,
4447 "operation not expected to succeed", return -1);
4449 return 0;
4452 int test_aff(isl_ctx *ctx)
4454 const char *str;
4455 isl_set *set;
4456 isl_space *space;
4457 isl_local_space *ls;
4458 isl_aff *aff;
4459 int zero, equal;
4461 if (test_bin_aff(ctx) < 0)
4462 return -1;
4463 if (test_bin_upma(ctx) < 0)
4464 return -1;
4465 if (test_bin_upma_fail(ctx) < 0)
4466 return -1;
4468 space = isl_space_set_alloc(ctx, 0, 1);
4469 ls = isl_local_space_from_space(space);
4470 aff = isl_aff_zero_on_domain(ls);
4472 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4473 aff = isl_aff_scale_down_ui(aff, 3);
4474 aff = isl_aff_floor(aff);
4475 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4476 aff = isl_aff_scale_down_ui(aff, 2);
4477 aff = isl_aff_floor(aff);
4478 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4480 str = "{ [10] }";
4481 set = isl_set_read_from_str(ctx, str);
4482 aff = isl_aff_gist(aff, set);
4484 aff = isl_aff_add_constant_si(aff, -16);
4485 zero = isl_aff_plain_is_zero(aff);
4486 isl_aff_free(aff);
4488 if (zero < 0)
4489 return -1;
4490 if (!zero)
4491 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4493 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4494 aff = isl_aff_scale_down_ui(aff, 64);
4495 aff = isl_aff_floor(aff);
4496 equal = aff_check_plain_equal(aff, "{ [-1] }");
4497 isl_aff_free(aff);
4498 if (equal < 0)
4499 return -1;
4501 return 0;
4504 int test_dim_max(isl_ctx *ctx)
4506 int equal;
4507 const char *str;
4508 isl_set *set1, *set2;
4509 isl_set *set;
4510 isl_map *map;
4511 isl_pw_aff *pwaff;
4513 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4514 set = isl_set_read_from_str(ctx, str);
4515 pwaff = isl_set_dim_max(set, 0);
4516 set1 = isl_set_from_pw_aff(pwaff);
4517 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4518 set2 = isl_set_read_from_str(ctx, str);
4519 equal = isl_set_is_equal(set1, set2);
4520 isl_set_free(set1);
4521 isl_set_free(set2);
4522 if (equal < 0)
4523 return -1;
4524 if (!equal)
4525 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4527 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4528 set = isl_set_read_from_str(ctx, str);
4529 pwaff = isl_set_dim_max(set, 0);
4530 set1 = isl_set_from_pw_aff(pwaff);
4531 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4532 set2 = isl_set_read_from_str(ctx, str);
4533 equal = isl_set_is_equal(set1, set2);
4534 isl_set_free(set1);
4535 isl_set_free(set2);
4536 if (equal < 0)
4537 return -1;
4538 if (!equal)
4539 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4541 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4542 set = isl_set_read_from_str(ctx, str);
4543 pwaff = isl_set_dim_max(set, 0);
4544 set1 = isl_set_from_pw_aff(pwaff);
4545 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4546 set2 = isl_set_read_from_str(ctx, str);
4547 equal = isl_set_is_equal(set1, set2);
4548 isl_set_free(set1);
4549 isl_set_free(set2);
4550 if (equal < 0)
4551 return -1;
4552 if (!equal)
4553 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4555 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4556 "0 <= i < N and 0 <= j < M }";
4557 map = isl_map_read_from_str(ctx, str);
4558 set = isl_map_range(map);
4560 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4561 set1 = isl_set_from_pw_aff(pwaff);
4562 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4563 set2 = isl_set_read_from_str(ctx, str);
4564 equal = isl_set_is_equal(set1, set2);
4565 isl_set_free(set1);
4566 isl_set_free(set2);
4568 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4569 set1 = isl_set_from_pw_aff(pwaff);
4570 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4571 set2 = isl_set_read_from_str(ctx, str);
4572 if (equal >= 0 && equal)
4573 equal = isl_set_is_equal(set1, set2);
4574 isl_set_free(set1);
4575 isl_set_free(set2);
4577 isl_set_free(set);
4579 if (equal < 0)
4580 return -1;
4581 if (!equal)
4582 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4584 /* Check that solutions are properly merged. */
4585 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4586 "c <= -1 + n - 4a - 2b and c >= -2b and "
4587 "4a >= -4 + n and c >= 0 }";
4588 set = isl_set_read_from_str(ctx, str);
4589 pwaff = isl_set_dim_min(set, 2);
4590 set1 = isl_set_from_pw_aff(pwaff);
4591 str = "[n] -> { [(0)] : n >= 1 }";
4592 set2 = isl_set_read_from_str(ctx, str);
4593 equal = isl_set_is_equal(set1, set2);
4594 isl_set_free(set1);
4595 isl_set_free(set2);
4597 if (equal < 0)
4598 return -1;
4599 if (!equal)
4600 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4602 /* Check that empty solution lie in the right space. */
4603 str = "[n] -> { [t,a] : 1 = 0 }";
4604 set = isl_set_read_from_str(ctx, str);
4605 pwaff = isl_set_dim_max(set, 0);
4606 set1 = isl_set_from_pw_aff(pwaff);
4607 str = "[n] -> { [t] : 1 = 0 }";
4608 set2 = isl_set_read_from_str(ctx, str);
4609 equal = isl_set_is_equal(set1, set2);
4610 isl_set_free(set1);
4611 isl_set_free(set2);
4613 if (equal < 0)
4614 return -1;
4615 if (!equal)
4616 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4618 return 0;
4621 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4623 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4624 const char *str)
4626 isl_ctx *ctx;
4627 isl_pw_multi_aff *pma2;
4628 int equal;
4630 if (!pma)
4631 return -1;
4633 ctx = isl_pw_multi_aff_get_ctx(pma);
4634 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4635 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4636 isl_pw_multi_aff_free(pma2);
4638 return equal;
4641 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4642 * represented by "str".
4644 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4645 const char *str)
4647 int equal;
4649 equal = pw_multi_aff_plain_is_equal(pma, str);
4650 if (equal < 0)
4651 return -1;
4652 if (!equal)
4653 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4654 "result not as expected", return -1);
4655 return 0;
4658 /* Basic test for isl_pw_multi_aff_product.
4660 * Check that multiple pieces are properly handled.
4662 static int test_product_pma(isl_ctx *ctx)
4664 int equal;
4665 const char *str;
4666 isl_pw_multi_aff *pma1, *pma2;
4668 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4669 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4670 str = "{ C[] -> D[] }";
4671 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4672 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4673 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4674 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4675 equal = pw_multi_aff_check_plain_equal(pma1, str);
4676 isl_pw_multi_aff_free(pma1);
4677 if (equal < 0)
4678 return -1;
4680 return 0;
4683 int test_product(isl_ctx *ctx)
4685 const char *str;
4686 isl_set *set;
4687 isl_union_set *uset1, *uset2;
4688 int ok;
4690 str = "{ A[i] }";
4691 set = isl_set_read_from_str(ctx, str);
4692 set = isl_set_product(set, isl_set_copy(set));
4693 ok = isl_set_is_wrapping(set);
4694 isl_set_free(set);
4695 if (ok < 0)
4696 return -1;
4697 if (!ok)
4698 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4700 str = "{ [] }";
4701 uset1 = isl_union_set_read_from_str(ctx, str);
4702 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4703 str = "{ [[] -> []] }";
4704 uset2 = isl_union_set_read_from_str(ctx, str);
4705 ok = isl_union_set_is_equal(uset1, uset2);
4706 isl_union_set_free(uset1);
4707 isl_union_set_free(uset2);
4708 if (ok < 0)
4709 return -1;
4710 if (!ok)
4711 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4713 if (test_product_pma(ctx) < 0)
4714 return -1;
4716 return 0;
4719 /* Check that two sets are not considered disjoint just because
4720 * they have a different set of (named) parameters.
4722 static int test_disjoint(isl_ctx *ctx)
4724 const char *str;
4725 isl_set *set, *set2;
4726 int disjoint;
4728 str = "[n] -> { [[]->[]] }";
4729 set = isl_set_read_from_str(ctx, str);
4730 str = "{ [[]->[]] }";
4731 set2 = isl_set_read_from_str(ctx, str);
4732 disjoint = isl_set_is_disjoint(set, set2);
4733 isl_set_free(set);
4734 isl_set_free(set2);
4735 if (disjoint < 0)
4736 return -1;
4737 if (disjoint)
4738 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4740 return 0;
4743 int test_equal(isl_ctx *ctx)
4745 const char *str;
4746 isl_set *set, *set2;
4747 int equal;
4749 str = "{ S_6[i] }";
4750 set = isl_set_read_from_str(ctx, str);
4751 str = "{ S_7[i] }";
4752 set2 = isl_set_read_from_str(ctx, str);
4753 equal = isl_set_is_equal(set, set2);
4754 isl_set_free(set);
4755 isl_set_free(set2);
4756 if (equal < 0)
4757 return -1;
4758 if (equal)
4759 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4761 return 0;
4764 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4765 enum isl_dim_type type, unsigned pos, int fixed)
4767 int test;
4769 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4770 isl_map_free(map);
4771 if (test < 0)
4772 return -1;
4773 if (test == fixed)
4774 return 0;
4775 if (fixed)
4776 isl_die(ctx, isl_error_unknown,
4777 "map not detected as fixed", return -1);
4778 else
4779 isl_die(ctx, isl_error_unknown,
4780 "map detected as fixed", return -1);
4783 int test_fixed(isl_ctx *ctx)
4785 const char *str;
4786 isl_map *map;
4788 str = "{ [i] -> [i] }";
4789 map = isl_map_read_from_str(ctx, str);
4790 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4791 return -1;
4792 str = "{ [i] -> [1] }";
4793 map = isl_map_read_from_str(ctx, str);
4794 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4795 return -1;
4796 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4797 map = isl_map_read_from_str(ctx, str);
4798 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4799 return -1;
4800 map = isl_map_read_from_str(ctx, str);
4801 map = isl_map_neg(map);
4802 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4803 return -1;
4805 return 0;
4808 struct isl_vertices_test_data {
4809 const char *set;
4810 int n;
4811 const char *vertex[2];
4812 } vertices_tests[] = {
4813 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4814 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4815 { "{ A[t, i] : t = 14 and i = 1 }",
4816 1, { "{ A[14, 1] }" } },
4819 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4821 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4823 struct isl_vertices_test_data *data = user;
4824 isl_ctx *ctx;
4825 isl_multi_aff *ma;
4826 isl_basic_set *bset;
4827 isl_pw_multi_aff *pma;
4828 int i;
4829 isl_bool equal;
4831 ctx = isl_vertex_get_ctx(vertex);
4832 bset = isl_vertex_get_domain(vertex);
4833 ma = isl_vertex_get_expr(vertex);
4834 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4836 for (i = 0; i < data->n; ++i) {
4837 isl_pw_multi_aff *pma_i;
4839 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4840 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4841 isl_pw_multi_aff_free(pma_i);
4843 if (equal < 0 || equal)
4844 break;
4847 isl_pw_multi_aff_free(pma);
4848 isl_vertex_free(vertex);
4850 if (equal < 0)
4851 return isl_stat_error;
4853 return equal ? isl_stat_ok : isl_stat_error;
4856 int test_vertices(isl_ctx *ctx)
4858 int i;
4860 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4861 isl_basic_set *bset;
4862 isl_vertices *vertices;
4863 int ok = 1;
4864 int n;
4866 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4867 vertices = isl_basic_set_compute_vertices(bset);
4868 n = isl_vertices_get_n_vertices(vertices);
4869 if (vertices_tests[i].n != n)
4870 ok = 0;
4871 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4872 &vertices_tests[i]) < 0)
4873 ok = 0;
4874 isl_vertices_free(vertices);
4875 isl_basic_set_free(bset);
4877 if (!vertices)
4878 return -1;
4879 if (!ok)
4880 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4881 return -1);
4884 return 0;
4887 int test_union_pw(isl_ctx *ctx)
4889 int equal;
4890 const char *str;
4891 isl_union_set *uset;
4892 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4894 str = "{ [x] -> x^2 }";
4895 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4896 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4897 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4898 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4899 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4900 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4901 isl_union_pw_qpolynomial_free(upwqp1);
4902 isl_union_pw_qpolynomial_free(upwqp2);
4903 if (equal < 0)
4904 return -1;
4905 if (!equal)
4906 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4908 return 0;
4911 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4912 * defined over the correct domain space.
4914 static int test_eval(isl_ctx *ctx)
4916 const char *str;
4917 isl_point *pnt;
4918 isl_set *set;
4919 isl_union_pw_qpolynomial *upwqp;
4920 isl_val *v;
4921 int cmp;
4923 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4924 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4925 str = "{ A[6] }";
4926 set = isl_set_read_from_str(ctx, str);
4927 pnt = isl_set_sample_point(set);
4928 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4929 cmp = isl_val_cmp_si(v, 36);
4930 isl_val_free(v);
4932 if (!v)
4933 return -1;
4934 if (cmp != 0)
4935 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4937 return 0;
4940 /* Descriptions of sets that are tested for reparsing after printing.
4942 const char *output_tests[] = {
4943 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
4946 /* Check that printing a set and reparsing a set from the printed output
4947 * results in the same set.
4949 static int test_output_set(isl_ctx *ctx)
4951 int i;
4952 char *str;
4953 isl_set *set1, *set2;
4954 isl_bool equal;
4956 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
4957 set1 = isl_set_read_from_str(ctx, output_tests[i]);
4958 str = isl_set_to_str(set1);
4959 set2 = isl_set_read_from_str(ctx, str);
4960 free(str);
4961 equal = isl_set_is_equal(set1, set2);
4962 isl_set_free(set1);
4963 isl_set_free(set2);
4964 if (equal < 0)
4965 return -1;
4966 if (!equal)
4967 isl_die(ctx, isl_error_unknown,
4968 "parsed output not the same", return -1);
4971 return 0;
4974 int test_output(isl_ctx *ctx)
4976 char *s;
4977 const char *str;
4978 isl_pw_aff *pa;
4979 isl_printer *p;
4980 int equal;
4982 if (test_output_set(ctx) < 0)
4983 return -1;
4985 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4986 pa = isl_pw_aff_read_from_str(ctx, str);
4988 p = isl_printer_to_str(ctx);
4989 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4990 p = isl_printer_print_pw_aff(p, pa);
4991 s = isl_printer_get_str(p);
4992 isl_printer_free(p);
4993 isl_pw_aff_free(pa);
4994 if (!s)
4995 equal = -1;
4996 else
4997 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4998 free(s);
4999 if (equal < 0)
5000 return -1;
5001 if (!equal)
5002 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5004 return 0;
5007 int test_sample(isl_ctx *ctx)
5009 const char *str;
5010 isl_basic_set *bset1, *bset2;
5011 int empty, subset;
5013 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5014 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5015 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5016 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5017 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5018 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5019 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5020 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5021 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5022 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5023 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5024 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5025 "d - 1073741821e and "
5026 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5027 "3j >= 1 - a + b + 2e and "
5028 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5029 "3i <= 4 - a + 4b - e and "
5030 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5031 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5032 "c <= -1 + a and 3i >= -2 - a + 3e and "
5033 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5034 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5035 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5036 "1073741823e >= 1 + 1073741823b - d and "
5037 "3i >= 1073741823b + c - 1073741823e - f and "
5038 "3i >= 1 + 2b + e + 3g }";
5039 bset1 = isl_basic_set_read_from_str(ctx, str);
5040 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5041 empty = isl_basic_set_is_empty(bset2);
5042 subset = isl_basic_set_is_subset(bset2, bset1);
5043 isl_basic_set_free(bset1);
5044 isl_basic_set_free(bset2);
5045 if (empty < 0 || subset < 0)
5046 return -1;
5047 if (empty)
5048 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5049 if (!subset)
5050 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5052 return 0;
5055 int test_fixed_power(isl_ctx *ctx)
5057 const char *str;
5058 isl_map *map;
5059 isl_int exp;
5060 int equal;
5062 isl_int_init(exp);
5063 str = "{ [i] -> [i + 1] }";
5064 map = isl_map_read_from_str(ctx, str);
5065 isl_int_set_si(exp, 23);
5066 map = isl_map_fixed_power(map, exp);
5067 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5068 isl_int_clear(exp);
5069 isl_map_free(map);
5070 if (equal < 0)
5071 return -1;
5073 return 0;
5076 int test_slice(isl_ctx *ctx)
5078 const char *str;
5079 isl_map *map;
5080 int equal;
5082 str = "{ [i] -> [j] }";
5083 map = isl_map_read_from_str(ctx, str);
5084 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5085 equal = map_check_equal(map, "{ [i] -> [i] }");
5086 isl_map_free(map);
5087 if (equal < 0)
5088 return -1;
5090 str = "{ [i] -> [j] }";
5091 map = isl_map_read_from_str(ctx, str);
5092 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5093 equal = map_check_equal(map, "{ [i] -> [j] }");
5094 isl_map_free(map);
5095 if (equal < 0)
5096 return -1;
5098 str = "{ [i] -> [j] }";
5099 map = isl_map_read_from_str(ctx, str);
5100 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5101 equal = map_check_equal(map, "{ [i] -> [-i] }");
5102 isl_map_free(map);
5103 if (equal < 0)
5104 return -1;
5106 str = "{ [i] -> [j] }";
5107 map = isl_map_read_from_str(ctx, str);
5108 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5109 equal = map_check_equal(map, "{ [0] -> [j] }");
5110 isl_map_free(map);
5111 if (equal < 0)
5112 return -1;
5114 str = "{ [i] -> [j] }";
5115 map = isl_map_read_from_str(ctx, str);
5116 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5117 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5118 isl_map_free(map);
5119 if (equal < 0)
5120 return -1;
5122 str = "{ [i] -> [j] }";
5123 map = isl_map_read_from_str(ctx, str);
5124 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5125 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5126 isl_map_free(map);
5127 if (equal < 0)
5128 return -1;
5130 return 0;
5133 int test_eliminate(isl_ctx *ctx)
5135 const char *str;
5136 isl_map *map;
5137 int equal;
5139 str = "{ [i] -> [j] : i = 2j }";
5140 map = isl_map_read_from_str(ctx, str);
5141 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5142 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5143 isl_map_free(map);
5144 if (equal < 0)
5145 return -1;
5147 return 0;
5150 /* Check that isl_set_dim_residue_class detects that the values of j
5151 * in the set below are all odd and that it does not detect any spurious
5152 * strides.
5154 static int test_residue_class(isl_ctx *ctx)
5156 const char *str;
5157 isl_set *set;
5158 isl_int m, r;
5159 int res;
5161 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5162 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5163 set = isl_set_read_from_str(ctx, str);
5164 isl_int_init(m);
5165 isl_int_init(r);
5166 res = isl_set_dim_residue_class(set, 1, &m, &r);
5167 if (res >= 0 &&
5168 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5169 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5170 res = -1);
5171 isl_int_clear(r);
5172 isl_int_clear(m);
5173 isl_set_free(set);
5175 return res;
5178 int test_align_parameters(isl_ctx *ctx)
5180 const char *str;
5181 isl_space *space;
5182 isl_multi_aff *ma1, *ma2;
5183 int equal;
5185 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5186 ma1 = isl_multi_aff_read_from_str(ctx, str);
5188 space = isl_space_params_alloc(ctx, 1);
5189 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5190 ma1 = isl_multi_aff_align_params(ma1, space);
5192 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5193 ma2 = isl_multi_aff_read_from_str(ctx, str);
5195 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5197 isl_multi_aff_free(ma1);
5198 isl_multi_aff_free(ma2);
5200 if (equal < 0)
5201 return -1;
5202 if (!equal)
5203 isl_die(ctx, isl_error_unknown,
5204 "result not as expected", return -1);
5206 return 0;
5209 static int test_list(isl_ctx *ctx)
5211 isl_id *a, *b, *c, *d, *id;
5212 isl_id_list *list;
5213 int ok;
5215 a = isl_id_alloc(ctx, "a", NULL);
5216 b = isl_id_alloc(ctx, "b", NULL);
5217 c = isl_id_alloc(ctx, "c", NULL);
5218 d = isl_id_alloc(ctx, "d", NULL);
5220 list = isl_id_list_alloc(ctx, 4);
5221 list = isl_id_list_add(list, a);
5222 list = isl_id_list_add(list, b);
5223 list = isl_id_list_add(list, c);
5224 list = isl_id_list_add(list, d);
5225 list = isl_id_list_drop(list, 1, 1);
5227 if (isl_id_list_n_id(list) != 3) {
5228 isl_id_list_free(list);
5229 isl_die(ctx, isl_error_unknown,
5230 "unexpected number of elements in list", return -1);
5233 id = isl_id_list_get_id(list, 0);
5234 ok = id == a;
5235 isl_id_free(id);
5236 id = isl_id_list_get_id(list, 1);
5237 ok = ok && id == c;
5238 isl_id_free(id);
5239 id = isl_id_list_get_id(list, 2);
5240 ok = ok && id == d;
5241 isl_id_free(id);
5243 isl_id_list_free(list);
5245 if (!ok)
5246 isl_die(ctx, isl_error_unknown,
5247 "unexpected elements in list", return -1);
5249 return 0;
5252 const char *set_conversion_tests[] = {
5253 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5254 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5255 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5256 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5257 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5258 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5259 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5260 "-3 + c <= d <= 28 + c) }",
5263 /* Check that converting from isl_set to isl_pw_multi_aff and back
5264 * to isl_set produces the original isl_set.
5266 static int test_set_conversion(isl_ctx *ctx)
5268 int i;
5269 const char *str;
5270 isl_set *set1, *set2;
5271 isl_pw_multi_aff *pma;
5272 int equal;
5274 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5275 str = set_conversion_tests[i];
5276 set1 = isl_set_read_from_str(ctx, str);
5277 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5278 set2 = isl_set_from_pw_multi_aff(pma);
5279 equal = isl_set_is_equal(set1, set2);
5280 isl_set_free(set1);
5281 isl_set_free(set2);
5283 if (equal < 0)
5284 return -1;
5285 if (!equal)
5286 isl_die(ctx, isl_error_unknown, "bad conversion",
5287 return -1);
5290 return 0;
5293 const char *conversion_tests[] = {
5294 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5295 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5296 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5297 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5298 "9e <= -2 - 2a) }",
5299 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5300 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5301 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5304 /* Check that converting from isl_map to isl_pw_multi_aff and back
5305 * to isl_map produces the original isl_map.
5307 static int test_map_conversion(isl_ctx *ctx)
5309 int i;
5310 isl_map *map1, *map2;
5311 isl_pw_multi_aff *pma;
5312 int equal;
5314 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5315 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5316 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5317 map2 = isl_map_from_pw_multi_aff(pma);
5318 equal = isl_map_is_equal(map1, map2);
5319 isl_map_free(map1);
5320 isl_map_free(map2);
5322 if (equal < 0)
5323 return -1;
5324 if (!equal)
5325 isl_die(ctx, isl_error_unknown, "bad conversion",
5326 return -1);
5329 return 0;
5332 static int test_conversion(isl_ctx *ctx)
5334 if (test_set_conversion(ctx) < 0)
5335 return -1;
5336 if (test_map_conversion(ctx) < 0)
5337 return -1;
5338 return 0;
5341 /* Check that isl_basic_map_curry does not modify input.
5343 static int test_curry(isl_ctx *ctx)
5345 const char *str;
5346 isl_basic_map *bmap1, *bmap2;
5347 int equal;
5349 str = "{ [A[] -> B[]] -> C[] }";
5350 bmap1 = isl_basic_map_read_from_str(ctx, str);
5351 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5352 equal = isl_basic_map_is_equal(bmap1, bmap2);
5353 isl_basic_map_free(bmap1);
5354 isl_basic_map_free(bmap2);
5356 if (equal < 0)
5357 return -1;
5358 if (equal)
5359 isl_die(ctx, isl_error_unknown,
5360 "curried map should not be equal to original",
5361 return -1);
5363 return 0;
5366 struct {
5367 const char *set;
5368 const char *ma;
5369 const char *res;
5370 } preimage_tests[] = {
5371 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5372 "{ A[j,i] -> B[i,j] }",
5373 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5374 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5375 "{ A[a,b] -> B[a/2,b/6] }",
5376 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5377 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5378 "{ A[a,b] -> B[a/2,b/6] }",
5379 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5380 "exists i,j : a = 2 i and b = 6 j }" },
5381 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5382 "[n] -> { : 0 <= n <= 100 }" },
5383 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5384 "{ A[a] -> B[2a] }",
5385 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5386 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5387 "{ A[a] -> B[([a/2])] }",
5388 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5389 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5390 "{ A[a] -> B[a,a,a/3] }",
5391 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5392 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5393 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5396 static int test_preimage_basic_set(isl_ctx *ctx)
5398 int i;
5399 isl_basic_set *bset1, *bset2;
5400 isl_multi_aff *ma;
5401 int equal;
5403 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5404 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5405 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5406 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5407 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5408 equal = isl_basic_set_is_equal(bset1, bset2);
5409 isl_basic_set_free(bset1);
5410 isl_basic_set_free(bset2);
5411 if (equal < 0)
5412 return -1;
5413 if (!equal)
5414 isl_die(ctx, isl_error_unknown, "bad preimage",
5415 return -1);
5418 return 0;
5421 struct {
5422 const char *map;
5423 const char *ma;
5424 const char *res;
5425 } preimage_domain_tests[] = {
5426 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5427 "{ A[j,i] -> B[i,j] }",
5428 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5429 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5430 "{ A[i] -> B[i + 1] }",
5431 "{ A[i] -> C[i + 1] }" },
5432 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5433 "{ A[i] -> B[i + 1] }",
5434 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5435 { "{ B[i] -> C[([i/2])] }",
5436 "{ A[i] -> B[2i] }",
5437 "{ A[i] -> C[i] }" },
5438 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5439 "{ A[i] -> B[([i/5]), ([i/7])] }",
5440 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5441 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5442 "[N] -> { A[] -> B[([N/5])] }",
5443 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5444 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5445 "{ A[i] -> B[2i] }",
5446 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5447 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5448 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5449 "{ A[i] -> B[2i] }",
5450 "{ A[i] -> C[2i] }" },
5451 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5452 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5455 static int test_preimage_union_map(isl_ctx *ctx)
5457 int i;
5458 isl_union_map *umap1, *umap2;
5459 isl_multi_aff *ma;
5460 int equal;
5462 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5463 umap1 = isl_union_map_read_from_str(ctx,
5464 preimage_domain_tests[i].map);
5465 ma = isl_multi_aff_read_from_str(ctx,
5466 preimage_domain_tests[i].ma);
5467 umap2 = isl_union_map_read_from_str(ctx,
5468 preimage_domain_tests[i].res);
5469 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5470 equal = isl_union_map_is_equal(umap1, umap2);
5471 isl_union_map_free(umap1);
5472 isl_union_map_free(umap2);
5473 if (equal < 0)
5474 return -1;
5475 if (!equal)
5476 isl_die(ctx, isl_error_unknown, "bad preimage",
5477 return -1);
5480 return 0;
5483 static int test_preimage(isl_ctx *ctx)
5485 if (test_preimage_basic_set(ctx) < 0)
5486 return -1;
5487 if (test_preimage_union_map(ctx) < 0)
5488 return -1;
5490 return 0;
5493 struct {
5494 const char *ma1;
5495 const char *ma;
5496 const char *res;
5497 } pullback_tests[] = {
5498 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5499 "{ A[a,b] -> C[b + 2a] }" },
5500 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5501 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5502 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5503 "{ A[a] -> C[(a)/6] }" },
5504 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5505 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5506 "{ A[a] -> C[(2a)/3] }" },
5507 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5508 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5509 "{ A[i,j] -> C[i + j, i + j] }"},
5510 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5511 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5512 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5513 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5514 "{ [i, j] -> [floor((i)/3), j] }",
5515 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5518 static int test_pullback(isl_ctx *ctx)
5520 int i;
5521 isl_multi_aff *ma1, *ma2;
5522 isl_multi_aff *ma;
5523 int equal;
5525 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5526 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5527 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5528 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5529 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5530 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5531 isl_multi_aff_free(ma1);
5532 isl_multi_aff_free(ma2);
5533 if (equal < 0)
5534 return -1;
5535 if (!equal)
5536 isl_die(ctx, isl_error_unknown, "bad pullback",
5537 return -1);
5540 return 0;
5543 /* Check that negation is printed correctly and that equal expressions
5544 * are correctly identified.
5546 static int test_ast(isl_ctx *ctx)
5548 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5549 char *str;
5550 int ok, equal;
5552 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5553 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5554 expr = isl_ast_expr_add(expr1, expr2);
5555 expr2 = isl_ast_expr_copy(expr);
5556 expr = isl_ast_expr_neg(expr);
5557 expr2 = isl_ast_expr_neg(expr2);
5558 equal = isl_ast_expr_is_equal(expr, expr2);
5559 str = isl_ast_expr_to_C_str(expr);
5560 ok = str ? !strcmp(str, "-(A + B)") : -1;
5561 free(str);
5562 isl_ast_expr_free(expr);
5563 isl_ast_expr_free(expr2);
5565 if (ok < 0 || equal < 0)
5566 return -1;
5567 if (!equal)
5568 isl_die(ctx, isl_error_unknown,
5569 "equal expressions not considered equal", return -1);
5570 if (!ok)
5571 isl_die(ctx, isl_error_unknown,
5572 "isl_ast_expr printed incorrectly", return -1);
5574 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5575 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5576 expr = isl_ast_expr_add(expr1, expr2);
5577 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5578 expr = isl_ast_expr_sub(expr3, expr);
5579 str = isl_ast_expr_to_C_str(expr);
5580 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5581 free(str);
5582 isl_ast_expr_free(expr);
5584 if (ok < 0)
5585 return -1;
5586 if (!ok)
5587 isl_die(ctx, isl_error_unknown,
5588 "isl_ast_expr printed incorrectly", return -1);
5590 return 0;
5593 /* Check that isl_ast_build_expr_from_set returns a valid expression
5594 * for an empty set. Note that isl_ast_build_expr_from_set getting
5595 * called on an empty set probably indicates a bug in the caller.
5597 static int test_ast_build(isl_ctx *ctx)
5599 isl_set *set;
5600 isl_ast_build *build;
5601 isl_ast_expr *expr;
5603 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5604 build = isl_ast_build_from_context(set);
5606 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5607 expr = isl_ast_build_expr_from_set(build, set);
5609 isl_ast_expr_free(expr);
5610 isl_ast_build_free(build);
5612 if (!expr)
5613 return -1;
5615 return 0;
5618 /* Internal data structure for before_for and after_for callbacks.
5620 * depth is the current depth
5621 * before is the number of times before_for has been called
5622 * after is the number of times after_for has been called
5624 struct isl_test_codegen_data {
5625 int depth;
5626 int before;
5627 int after;
5630 /* This function is called before each for loop in the AST generated
5631 * from test_ast_gen1.
5633 * Increment the number of calls and the depth.
5634 * Check that the space returned by isl_ast_build_get_schedule_space
5635 * matches the target space of the schedule returned by
5636 * isl_ast_build_get_schedule.
5637 * Return an isl_id that is checked by the corresponding call
5638 * to after_for.
5640 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5641 void *user)
5643 struct isl_test_codegen_data *data = user;
5644 isl_ctx *ctx;
5645 isl_space *space;
5646 isl_union_map *schedule;
5647 isl_union_set *uset;
5648 isl_set *set;
5649 int empty;
5650 char name[] = "d0";
5652 ctx = isl_ast_build_get_ctx(build);
5654 if (data->before >= 3)
5655 isl_die(ctx, isl_error_unknown,
5656 "unexpected number of for nodes", return NULL);
5657 if (data->depth >= 2)
5658 isl_die(ctx, isl_error_unknown,
5659 "unexpected depth", return NULL);
5661 snprintf(name, sizeof(name), "d%d", data->depth);
5662 data->before++;
5663 data->depth++;
5665 schedule = isl_ast_build_get_schedule(build);
5666 uset = isl_union_map_range(schedule);
5667 if (!uset)
5668 return NULL;
5669 if (isl_union_set_n_set(uset) != 1) {
5670 isl_union_set_free(uset);
5671 isl_die(ctx, isl_error_unknown,
5672 "expecting single range space", return NULL);
5675 space = isl_ast_build_get_schedule_space(build);
5676 set = isl_union_set_extract_set(uset, space);
5677 isl_union_set_free(uset);
5678 empty = isl_set_is_empty(set);
5679 isl_set_free(set);
5681 if (empty < 0)
5682 return NULL;
5683 if (empty)
5684 isl_die(ctx, isl_error_unknown,
5685 "spaces don't match", return NULL);
5687 return isl_id_alloc(ctx, name, NULL);
5690 /* This function is called after each for loop in the AST generated
5691 * from test_ast_gen1.
5693 * Increment the number of calls and decrement the depth.
5694 * Check that the annotation attached to the node matches
5695 * the isl_id returned by the corresponding call to before_for.
5697 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5698 __isl_keep isl_ast_build *build, void *user)
5700 struct isl_test_codegen_data *data = user;
5701 isl_id *id;
5702 const char *name;
5703 int valid;
5705 data->after++;
5706 data->depth--;
5708 if (data->after > data->before)
5709 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5710 "mismatch in number of for nodes",
5711 return isl_ast_node_free(node));
5713 id = isl_ast_node_get_annotation(node);
5714 if (!id)
5715 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5716 "missing annotation", return isl_ast_node_free(node));
5718 name = isl_id_get_name(id);
5719 valid = name && atoi(name + 1) == data->depth;
5720 isl_id_free(id);
5722 if (!valid)
5723 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5724 "wrong annotation", return isl_ast_node_free(node));
5726 return node;
5729 /* Check that the before_each_for and after_each_for callbacks
5730 * are called for each for loop in the generated code,
5731 * that they are called in the right order and that the isl_id
5732 * returned from the before_each_for callback is attached to
5733 * the isl_ast_node passed to the corresponding after_each_for call.
5735 static int test_ast_gen1(isl_ctx *ctx)
5737 const char *str;
5738 isl_set *set;
5739 isl_union_map *schedule;
5740 isl_ast_build *build;
5741 isl_ast_node *tree;
5742 struct isl_test_codegen_data data;
5744 str = "[N] -> { : N >= 10 }";
5745 set = isl_set_read_from_str(ctx, str);
5746 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5747 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5748 schedule = isl_union_map_read_from_str(ctx, str);
5750 data.before = 0;
5751 data.after = 0;
5752 data.depth = 0;
5753 build = isl_ast_build_from_context(set);
5754 build = isl_ast_build_set_before_each_for(build,
5755 &before_for, &data);
5756 build = isl_ast_build_set_after_each_for(build,
5757 &after_for, &data);
5758 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5759 isl_ast_build_free(build);
5760 if (!tree)
5761 return -1;
5763 isl_ast_node_free(tree);
5765 if (data.before != 3 || data.after != 3)
5766 isl_die(ctx, isl_error_unknown,
5767 "unexpected number of for nodes", return -1);
5769 return 0;
5772 /* Check that the AST generator handles domains that are integrally disjoint
5773 * but not rationally disjoint.
5775 static int test_ast_gen2(isl_ctx *ctx)
5777 const char *str;
5778 isl_set *set;
5779 isl_union_map *schedule;
5780 isl_union_map *options;
5781 isl_ast_build *build;
5782 isl_ast_node *tree;
5784 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5785 schedule = isl_union_map_read_from_str(ctx, str);
5786 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5787 build = isl_ast_build_from_context(set);
5789 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5790 options = isl_union_map_read_from_str(ctx, str);
5791 build = isl_ast_build_set_options(build, options);
5792 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5793 isl_ast_build_free(build);
5794 if (!tree)
5795 return -1;
5796 isl_ast_node_free(tree);
5798 return 0;
5801 /* Increment *user on each call.
5803 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5804 __isl_keep isl_ast_build *build, void *user)
5806 int *n = user;
5808 (*n)++;
5810 return node;
5813 /* Test that unrolling tries to minimize the number of instances.
5814 * In particular, for the schedule given below, make sure it generates
5815 * 3 nodes (rather than 101).
5817 static int test_ast_gen3(isl_ctx *ctx)
5819 const char *str;
5820 isl_set *set;
5821 isl_union_map *schedule;
5822 isl_union_map *options;
5823 isl_ast_build *build;
5824 isl_ast_node *tree;
5825 int n_domain = 0;
5827 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5828 schedule = isl_union_map_read_from_str(ctx, str);
5829 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5831 str = "{ [i] -> unroll[0] }";
5832 options = isl_union_map_read_from_str(ctx, str);
5834 build = isl_ast_build_from_context(set);
5835 build = isl_ast_build_set_options(build, options);
5836 build = isl_ast_build_set_at_each_domain(build,
5837 &count_domains, &n_domain);
5838 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5839 isl_ast_build_free(build);
5840 if (!tree)
5841 return -1;
5843 isl_ast_node_free(tree);
5845 if (n_domain != 3)
5846 isl_die(ctx, isl_error_unknown,
5847 "unexpected number of for nodes", return -1);
5849 return 0;
5852 /* Check that if the ast_build_exploit_nested_bounds options is set,
5853 * we do not get an outer if node in the generated AST,
5854 * while we do get such an outer if node if the options is not set.
5856 static int test_ast_gen4(isl_ctx *ctx)
5858 const char *str;
5859 isl_set *set;
5860 isl_union_map *schedule;
5861 isl_ast_build *build;
5862 isl_ast_node *tree;
5863 enum isl_ast_node_type type;
5864 int enb;
5866 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5867 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5869 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5871 schedule = isl_union_map_read_from_str(ctx, str);
5872 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5873 build = isl_ast_build_from_context(set);
5874 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5875 isl_ast_build_free(build);
5876 if (!tree)
5877 return -1;
5879 type = isl_ast_node_get_type(tree);
5880 isl_ast_node_free(tree);
5882 if (type == isl_ast_node_if)
5883 isl_die(ctx, isl_error_unknown,
5884 "not expecting if node", return -1);
5886 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5888 schedule = isl_union_map_read_from_str(ctx, str);
5889 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5890 build = isl_ast_build_from_context(set);
5891 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5892 isl_ast_build_free(build);
5893 if (!tree)
5894 return -1;
5896 type = isl_ast_node_get_type(tree);
5897 isl_ast_node_free(tree);
5899 if (type != isl_ast_node_if)
5900 isl_die(ctx, isl_error_unknown,
5901 "expecting if node", return -1);
5903 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5905 return 0;
5908 /* This function is called for each leaf in the AST generated
5909 * from test_ast_gen5.
5911 * We finalize the AST generation by extending the outer schedule
5912 * with a zero-dimensional schedule. If this results in any for loops,
5913 * then this means that we did not pass along enough information
5914 * about the outer schedule to the inner AST generation.
5916 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5917 void *user)
5919 isl_union_map *schedule, *extra;
5920 isl_ast_node *tree;
5922 schedule = isl_ast_build_get_schedule(build);
5923 extra = isl_union_map_copy(schedule);
5924 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5925 schedule = isl_union_map_range_product(schedule, extra);
5926 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5927 isl_ast_build_free(build);
5929 if (!tree)
5930 return NULL;
5932 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5933 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5934 "code should not contain any for loop",
5935 return isl_ast_node_free(tree));
5937 return tree;
5940 /* Check that we do not lose any information when going back and
5941 * forth between internal and external schedule.
5943 * In particular, we create an AST where we unroll the only
5944 * non-constant dimension in the schedule. We therefore do
5945 * not expect any for loops in the AST. However, older versions
5946 * of isl would not pass along enough information about the outer
5947 * schedule when performing an inner code generation from a create_leaf
5948 * callback, resulting in the inner code generation producing a for loop.
5950 static int test_ast_gen5(isl_ctx *ctx)
5952 const char *str;
5953 isl_set *set;
5954 isl_union_map *schedule, *options;
5955 isl_ast_build *build;
5956 isl_ast_node *tree;
5958 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5959 schedule = isl_union_map_read_from_str(ctx, str);
5961 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5962 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5963 options = isl_union_map_read_from_str(ctx, str);
5965 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5966 build = isl_ast_build_from_context(set);
5967 build = isl_ast_build_set_options(build, options);
5968 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5969 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5970 isl_ast_build_free(build);
5971 isl_ast_node_free(tree);
5972 if (!tree)
5973 return -1;
5975 return 0;
5978 static int test_ast_gen(isl_ctx *ctx)
5980 if (test_ast_gen1(ctx) < 0)
5981 return -1;
5982 if (test_ast_gen2(ctx) < 0)
5983 return -1;
5984 if (test_ast_gen3(ctx) < 0)
5985 return -1;
5986 if (test_ast_gen4(ctx) < 0)
5987 return -1;
5988 if (test_ast_gen5(ctx) < 0)
5989 return -1;
5990 return 0;
5993 /* Check if dropping output dimensions from an isl_pw_multi_aff
5994 * works properly.
5996 static int test_pw_multi_aff(isl_ctx *ctx)
5998 const char *str;
5999 isl_pw_multi_aff *pma1, *pma2;
6000 int equal;
6002 str = "{ [i,j] -> [i+j, 4i-j] }";
6003 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6004 str = "{ [i,j] -> [4i-j] }";
6005 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6007 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6009 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6011 isl_pw_multi_aff_free(pma1);
6012 isl_pw_multi_aff_free(pma2);
6013 if (equal < 0)
6014 return -1;
6015 if (!equal)
6016 isl_die(ctx, isl_error_unknown,
6017 "expressions not equal", return -1);
6019 return 0;
6022 /* Check that we can properly parse multi piecewise affine expressions
6023 * where the piecewise affine expressions have different domains.
6025 static int test_multi_pw_aff(isl_ctx *ctx)
6027 const char *str;
6028 isl_set *dom, *dom2;
6029 isl_multi_pw_aff *mpa1, *mpa2;
6030 isl_pw_aff *pa;
6031 int equal;
6032 int equal_domain;
6034 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6035 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6036 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6037 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6038 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6039 str = "{ [i] -> [(i : i > 0), 2i] }";
6040 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6042 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6044 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6045 dom = isl_pw_aff_domain(pa);
6046 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6047 dom2 = isl_pw_aff_domain(pa);
6048 equal_domain = isl_set_is_equal(dom, dom2);
6050 isl_set_free(dom);
6051 isl_set_free(dom2);
6052 isl_multi_pw_aff_free(mpa1);
6053 isl_multi_pw_aff_free(mpa2);
6055 if (equal < 0)
6056 return -1;
6057 if (!equal)
6058 isl_die(ctx, isl_error_unknown,
6059 "expressions not equal", return -1);
6061 if (equal_domain < 0)
6062 return -1;
6063 if (equal_domain)
6064 isl_die(ctx, isl_error_unknown,
6065 "domains unexpectedly equal", return -1);
6067 return 0;
6070 /* This is a regression test for a bug where isl_basic_map_simplify
6071 * would end up in an infinite loop. In particular, we construct
6072 * an empty basic set that is not obviously empty.
6073 * isl_basic_set_is_empty marks the basic set as empty.
6074 * After projecting out i3, the variable can be dropped completely,
6075 * but isl_basic_map_simplify refrains from doing so if the basic set
6076 * is empty and would end up in an infinite loop if it didn't test
6077 * explicitly for empty basic maps in the outer loop.
6079 static int test_simplify_1(isl_ctx *ctx)
6081 const char *str;
6082 isl_basic_set *bset;
6083 int empty;
6085 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6086 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6087 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6088 "i3 >= i2 }";
6089 bset = isl_basic_set_read_from_str(ctx, str);
6090 empty = isl_basic_set_is_empty(bset);
6091 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6092 isl_basic_set_free(bset);
6093 if (!bset)
6094 return -1;
6095 if (!empty)
6096 isl_die(ctx, isl_error_unknown,
6097 "basic set should be empty", return -1);
6099 return 0;
6102 /* Check that the equality in the set description below
6103 * is simplified away.
6105 static int test_simplify_2(isl_ctx *ctx)
6107 const char *str;
6108 isl_basic_set *bset;
6109 isl_bool universe;
6111 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6112 bset = isl_basic_set_read_from_str(ctx, str);
6113 universe = isl_basic_set_plain_is_universe(bset);
6114 isl_basic_set_free(bset);
6116 if (universe < 0)
6117 return -1;
6118 if (!universe)
6119 isl_die(ctx, isl_error_unknown,
6120 "equality not simplified away", return -1);
6121 return 0;
6124 /* Some simplification tests.
6126 static int test_simplify(isl_ctx *ctx)
6128 if (test_simplify_1(ctx) < 0)
6129 return -1;
6130 if (test_simplify_2(ctx) < 0)
6131 return -1;
6132 return 0;
6135 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6136 * with gbr context would fail to disable the use of the shifted tableau
6137 * when transferring equalities for the input to the context, resulting
6138 * in invalid sample values.
6140 static int test_partial_lexmin(isl_ctx *ctx)
6142 const char *str;
6143 isl_basic_set *bset;
6144 isl_basic_map *bmap;
6145 isl_map *map;
6147 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6148 bmap = isl_basic_map_read_from_str(ctx, str);
6149 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6150 bset = isl_basic_set_read_from_str(ctx, str);
6151 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6152 isl_map_free(map);
6154 if (!map)
6155 return -1;
6157 return 0;
6160 /* Check that the variable compression performed on the existentially
6161 * quantified variables inside isl_basic_set_compute_divs is not confused
6162 * by the implicit equalities among the parameters.
6164 static int test_compute_divs(isl_ctx *ctx)
6166 const char *str;
6167 isl_basic_set *bset;
6168 isl_set *set;
6170 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6171 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6172 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6173 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6174 bset = isl_basic_set_read_from_str(ctx, str);
6175 set = isl_basic_set_compute_divs(bset);
6176 isl_set_free(set);
6177 if (!set)
6178 return -1;
6180 return 0;
6183 /* Check that the reaching domain elements and the prefix schedule
6184 * at a leaf node are the same before and after grouping.
6186 static int test_schedule_tree_group_1(isl_ctx *ctx)
6188 int equal;
6189 const char *str;
6190 isl_id *id;
6191 isl_union_set *uset;
6192 isl_multi_union_pw_aff *mupa;
6193 isl_union_pw_multi_aff *upma1, *upma2;
6194 isl_union_set *domain1, *domain2;
6195 isl_union_map *umap1, *umap2;
6196 isl_schedule_node *node;
6198 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6199 uset = isl_union_set_read_from_str(ctx, str);
6200 node = isl_schedule_node_from_domain(uset);
6201 node = isl_schedule_node_child(node, 0);
6202 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6203 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6204 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6205 node = isl_schedule_node_child(node, 0);
6206 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6207 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6208 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6209 node = isl_schedule_node_child(node, 0);
6210 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6211 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6212 domain1 = isl_schedule_node_get_domain(node);
6213 id = isl_id_alloc(ctx, "group", NULL);
6214 node = isl_schedule_node_parent(node);
6215 node = isl_schedule_node_group(node, id);
6216 node = isl_schedule_node_child(node, 0);
6217 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6218 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6219 domain2 = isl_schedule_node_get_domain(node);
6220 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6221 if (equal >= 0 && equal)
6222 equal = isl_union_set_is_equal(domain1, domain2);
6223 if (equal >= 0 && equal)
6224 equal = isl_union_map_is_equal(umap1, umap2);
6225 isl_union_map_free(umap1);
6226 isl_union_map_free(umap2);
6227 isl_union_set_free(domain1);
6228 isl_union_set_free(domain2);
6229 isl_union_pw_multi_aff_free(upma1);
6230 isl_union_pw_multi_aff_free(upma2);
6231 isl_schedule_node_free(node);
6233 if (equal < 0)
6234 return -1;
6235 if (!equal)
6236 isl_die(ctx, isl_error_unknown,
6237 "expressions not equal", return -1);
6239 return 0;
6242 /* Check that we can have nested groupings and that the union map
6243 * schedule representation is the same before and after the grouping.
6244 * Note that after the grouping, the union map representation contains
6245 * the domain constraints from the ranges of the expansion nodes,
6246 * while they are missing from the union map representation of
6247 * the tree without expansion nodes.
6249 * Also check that the global expansion is as expected.
6251 static int test_schedule_tree_group_2(isl_ctx *ctx)
6253 int equal, equal_expansion;
6254 const char *str;
6255 isl_id *id;
6256 isl_union_set *uset;
6257 isl_union_map *umap1, *umap2;
6258 isl_union_map *expansion1, *expansion2;
6259 isl_union_set_list *filters;
6260 isl_multi_union_pw_aff *mupa;
6261 isl_schedule *schedule;
6262 isl_schedule_node *node;
6264 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6265 "S3[i,j] : 0 <= i,j < 10 }";
6266 uset = isl_union_set_read_from_str(ctx, str);
6267 node = isl_schedule_node_from_domain(uset);
6268 node = isl_schedule_node_child(node, 0);
6269 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6270 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6271 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6272 node = isl_schedule_node_child(node, 0);
6273 str = "{ S1[i,j] }";
6274 uset = isl_union_set_read_from_str(ctx, str);
6275 filters = isl_union_set_list_from_union_set(uset);
6276 str = "{ S2[i,j]; S3[i,j] }";
6277 uset = isl_union_set_read_from_str(ctx, str);
6278 filters = isl_union_set_list_add(filters, uset);
6279 node = isl_schedule_node_insert_sequence(node, filters);
6280 node = isl_schedule_node_child(node, 1);
6281 node = isl_schedule_node_child(node, 0);
6282 str = "{ S2[i,j] }";
6283 uset = isl_union_set_read_from_str(ctx, str);
6284 filters = isl_union_set_list_from_union_set(uset);
6285 str = "{ S3[i,j] }";
6286 uset = isl_union_set_read_from_str(ctx, str);
6287 filters = isl_union_set_list_add(filters, uset);
6288 node = isl_schedule_node_insert_sequence(node, filters);
6290 schedule = isl_schedule_node_get_schedule(node);
6291 umap1 = isl_schedule_get_map(schedule);
6292 uset = isl_schedule_get_domain(schedule);
6293 umap1 = isl_union_map_intersect_domain(umap1, uset);
6294 isl_schedule_free(schedule);
6296 node = isl_schedule_node_parent(node);
6297 node = isl_schedule_node_parent(node);
6298 id = isl_id_alloc(ctx, "group1", NULL);
6299 node = isl_schedule_node_group(node, id);
6300 node = isl_schedule_node_child(node, 1);
6301 node = isl_schedule_node_child(node, 0);
6302 id = isl_id_alloc(ctx, "group2", NULL);
6303 node = isl_schedule_node_group(node, id);
6305 schedule = isl_schedule_node_get_schedule(node);
6306 umap2 = isl_schedule_get_map(schedule);
6307 isl_schedule_free(schedule);
6309 node = isl_schedule_node_root(node);
6310 node = isl_schedule_node_child(node, 0);
6311 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6312 isl_schedule_node_free(node);
6314 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6315 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6316 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6318 expansion2 = isl_union_map_read_from_str(ctx, str);
6320 equal = isl_union_map_is_equal(umap1, umap2);
6321 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6323 isl_union_map_free(umap1);
6324 isl_union_map_free(umap2);
6325 isl_union_map_free(expansion1);
6326 isl_union_map_free(expansion2);
6328 if (equal < 0 || equal_expansion < 0)
6329 return -1;
6330 if (!equal)
6331 isl_die(ctx, isl_error_unknown,
6332 "expressions not equal", return -1);
6333 if (!equal_expansion)
6334 isl_die(ctx, isl_error_unknown,
6335 "unexpected expansion", return -1);
6337 return 0;
6340 /* Some tests for the isl_schedule_node_group function.
6342 static int test_schedule_tree_group(isl_ctx *ctx)
6344 if (test_schedule_tree_group_1(ctx) < 0)
6345 return -1;
6346 if (test_schedule_tree_group_2(ctx) < 0)
6347 return -1;
6348 return 0;
6351 struct {
6352 const char *set;
6353 const char *dual;
6354 } coef_tests[] = {
6355 { "{ rat: [i] : 0 <= i <= 10 }",
6356 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6357 { "{ rat: [i] : FALSE }",
6358 "{ rat: coefficients[[cst] -> [a]] }" },
6359 { "{ rat: [i] : }",
6360 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6363 struct {
6364 const char *set;
6365 const char *dual;
6366 } sol_tests[] = {
6367 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6368 "{ rat: [i] : 0 <= i <= 10 }" },
6369 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6370 "{ rat: [i] }" },
6371 { "{ rat: coefficients[[cst] -> [a]] }",
6372 "{ rat: [i] : FALSE }" },
6375 /* Test the basic functionality of isl_basic_set_coefficients and
6376 * isl_basic_set_solutions.
6378 static int test_dual(isl_ctx *ctx)
6380 int i;
6382 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6383 int equal;
6384 isl_basic_set *bset1, *bset2;
6386 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6387 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6388 bset1 = isl_basic_set_coefficients(bset1);
6389 equal = isl_basic_set_is_equal(bset1, bset2);
6390 isl_basic_set_free(bset1);
6391 isl_basic_set_free(bset2);
6392 if (equal < 0)
6393 return -1;
6394 if (!equal)
6395 isl_die(ctx, isl_error_unknown,
6396 "incorrect dual", return -1);
6399 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6400 int equal;
6401 isl_basic_set *bset1, *bset2;
6403 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6404 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6405 bset1 = isl_basic_set_solutions(bset1);
6406 equal = isl_basic_set_is_equal(bset1, bset2);
6407 isl_basic_set_free(bset1);
6408 isl_basic_set_free(bset2);
6409 if (equal < 0)
6410 return -1;
6411 if (!equal)
6412 isl_die(ctx, isl_error_unknown,
6413 "incorrect dual", return -1);
6416 return 0;
6419 struct {
6420 int scale_tile;
6421 int shift_point;
6422 const char *domain;
6423 const char *schedule;
6424 const char *sizes;
6425 const char *tile;
6426 const char *point;
6427 } tile_tests[] = {
6428 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6429 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6430 "{ [32,32] }",
6431 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6432 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6434 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6435 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6436 "{ [32,32] }",
6437 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6438 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6440 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6441 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6442 "{ [32,32] }",
6443 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6444 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6446 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6447 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6448 "{ [32,32] }",
6449 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6450 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6454 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6455 * tile the band and then check if the tile and point bands have the
6456 * expected partial schedule.
6458 static int test_tile(isl_ctx *ctx)
6460 int i;
6461 int scale;
6462 int shift;
6464 scale = isl_options_get_tile_scale_tile_loops(ctx);
6465 shift = isl_options_get_tile_shift_point_loops(ctx);
6467 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6468 int opt;
6469 int equal;
6470 const char *str;
6471 isl_union_set *domain;
6472 isl_multi_union_pw_aff *mupa, *mupa2;
6473 isl_schedule_node *node;
6474 isl_multi_val *sizes;
6476 opt = tile_tests[i].scale_tile;
6477 isl_options_set_tile_scale_tile_loops(ctx, opt);
6478 opt = tile_tests[i].shift_point;
6479 isl_options_set_tile_shift_point_loops(ctx, opt);
6481 str = tile_tests[i].domain;
6482 domain = isl_union_set_read_from_str(ctx, str);
6483 node = isl_schedule_node_from_domain(domain);
6484 node = isl_schedule_node_child(node, 0);
6485 str = tile_tests[i].schedule;
6486 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6487 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6488 str = tile_tests[i].sizes;
6489 sizes = isl_multi_val_read_from_str(ctx, str);
6490 node = isl_schedule_node_band_tile(node, sizes);
6492 str = tile_tests[i].tile;
6493 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6494 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6495 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6496 isl_multi_union_pw_aff_free(mupa);
6497 isl_multi_union_pw_aff_free(mupa2);
6499 node = isl_schedule_node_child(node, 0);
6501 str = tile_tests[i].point;
6502 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6503 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6504 if (equal >= 0 && equal)
6505 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6506 mupa2);
6507 isl_multi_union_pw_aff_free(mupa);
6508 isl_multi_union_pw_aff_free(mupa2);
6510 isl_schedule_node_free(node);
6512 if (equal < 0)
6513 return -1;
6514 if (!equal)
6515 isl_die(ctx, isl_error_unknown,
6516 "unexpected result", return -1);
6519 isl_options_set_tile_scale_tile_loops(ctx, scale);
6520 isl_options_set_tile_shift_point_loops(ctx, shift);
6522 return 0;
6525 /* Check that the domain hash of a space is equal to the hash
6526 * of the domain of the space.
6528 static int test_domain_hash(isl_ctx *ctx)
6530 isl_map *map;
6531 isl_space *space;
6532 uint32_t hash1, hash2;
6534 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6535 space = isl_map_get_space(map);
6536 isl_map_free(map);
6537 hash1 = isl_space_get_domain_hash(space);
6538 space = isl_space_domain(space);
6539 hash2 = isl_space_get_hash(space);
6540 isl_space_free(space);
6542 if (!space)
6543 return -1;
6544 if (hash1 != hash2)
6545 isl_die(ctx, isl_error_unknown,
6546 "domain hash not equal to hash of domain", return -1);
6548 return 0;
6551 /* Check that a universe basic set that is not obviously equal to the universe
6552 * is still recognized as being equal to the universe.
6554 static int test_universe(isl_ctx *ctx)
6556 const char *s;
6557 isl_basic_set *bset;
6558 isl_bool is_univ;
6560 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6561 bset = isl_basic_set_read_from_str(ctx, s);
6562 is_univ = isl_basic_set_is_universe(bset);
6563 isl_basic_set_free(bset);
6565 if (is_univ < 0)
6566 return -1;
6567 if (!is_univ)
6568 isl_die(ctx, isl_error_unknown,
6569 "not recognized as universe set", return -1);
6571 return 0;
6574 struct {
6575 const char *name;
6576 int (*fn)(isl_ctx *ctx);
6577 } tests [] = {
6578 { "universe", &test_universe },
6579 { "domain hash", &test_domain_hash },
6580 { "dual", &test_dual },
6581 { "dependence analysis", &test_flow },
6582 { "val", &test_val },
6583 { "compute divs", &test_compute_divs },
6584 { "partial lexmin", &test_partial_lexmin },
6585 { "simplify", &test_simplify },
6586 { "curry", &test_curry },
6587 { "piecewise multi affine expressions", &test_pw_multi_aff },
6588 { "multi piecewise affine expressions", &test_multi_pw_aff },
6589 { "conversion", &test_conversion },
6590 { "list", &test_list },
6591 { "align parameters", &test_align_parameters },
6592 { "preimage", &test_preimage },
6593 { "pullback", &test_pullback },
6594 { "AST", &test_ast },
6595 { "AST build", &test_ast_build },
6596 { "AST generation", &test_ast_gen },
6597 { "eliminate", &test_eliminate },
6598 { "residue class", &test_residue_class },
6599 { "div", &test_div },
6600 { "slice", &test_slice },
6601 { "fixed power", &test_fixed_power },
6602 { "sample", &test_sample },
6603 { "output", &test_output },
6604 { "vertices", &test_vertices },
6605 { "fixed", &test_fixed },
6606 { "equal", &test_equal },
6607 { "disjoint", &test_disjoint },
6608 { "product", &test_product },
6609 { "dim_max", &test_dim_max },
6610 { "affine", &test_aff },
6611 { "injective", &test_injective },
6612 { "schedule (whole component)", &test_schedule_whole },
6613 { "schedule (incremental)", &test_schedule_incremental },
6614 { "schedule tree grouping", &test_schedule_tree_group },
6615 { "tile", &test_tile },
6616 { "union_pw", &test_union_pw },
6617 { "eval", &test_eval },
6618 { "parse", &test_parse },
6619 { "single-valued", &test_sv },
6620 { "affine hull", &test_affine_hull },
6621 { "simple_hull", &test_simple_hull },
6622 { "coalesce", &test_coalesce },
6623 { "factorize", &test_factorize },
6624 { "subset", &test_subset },
6625 { "subtract", &test_subtract },
6626 { "intersect", &test_intersect },
6627 { "lexmin", &test_lexmin },
6628 { "min", &test_min },
6629 { "gist", &test_gist },
6630 { "piecewise quasi-polynomials", &test_pwqp },
6631 { "lift", &test_lift },
6632 { "bound", &test_bound },
6633 { "union", &test_union },
6634 { "split periods", &test_split_periods },
6635 { "lexicographic order", &test_lex },
6636 { "bijectivity", &test_bijective },
6637 { "dataflow analysis", &test_dep },
6638 { "reading", &test_read },
6639 { "bounded", &test_bounded },
6640 { "construction", &test_construction },
6641 { "dimension manipulation", &test_dim },
6642 { "map application", &test_application },
6643 { "convex hull", &test_convex_hull },
6644 { "transitive closure", &test_closure },
6647 int main(int argc, char **argv)
6649 int i;
6650 struct isl_ctx *ctx;
6651 struct isl_options *options;
6653 srcdir = getenv("srcdir");
6654 assert(srcdir);
6656 options = isl_options_new_with_defaults();
6657 assert(options);
6658 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6660 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6661 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6662 printf("%s\n", tests[i].name);
6663 if (tests[i].fn(ctx) < 0)
6664 goto error;
6666 isl_ctx_free(ctx);
6667 return 0;
6668 error:
6669 isl_ctx_free(ctx);
6670 return -1;