isl_map_simplify.c: uset_gist: do not introduce variables that were not involved
[isl.git] / isl_test.c
blobf7e6791ded79b5c685586ed8f97adf436ef7d474
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 int test_parse(struct isl_ctx *ctx)
124 isl_map *map, *map2;
125 const char *str, *str2;
127 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
128 return -1;
129 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
130 return -1;
131 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
132 return -1;
134 str = "{ [i] -> [-i] }";
135 map = isl_map_read_from_str(ctx, str);
136 assert(map);
137 isl_map_free(map);
139 str = "{ A[i] -> L[([i/3])] }";
140 map = isl_map_read_from_str(ctx, str);
141 assert(map);
142 isl_map_free(map);
144 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
145 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
146 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
148 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
149 str2 = "{ [x, y] : 2y >= 6 - x }";
150 if (test_parse_map_equal(ctx, str, str2) < 0)
151 return -1;
153 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
154 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
155 return -1;
156 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
157 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
158 str) < 0)
159 return -1;
161 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
162 map = isl_map_read_from_str(ctx, str);
163 str = "{ [new, old] -> [o0, o1] : "
164 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
165 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
166 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
167 map2 = isl_map_read_from_str(ctx, str);
168 assert(isl_map_is_equal(map, map2));
169 isl_map_free(map);
170 isl_map_free(map2);
172 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
173 map = isl_map_read_from_str(ctx, str);
174 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
175 map2 = isl_map_read_from_str(ctx, str);
176 assert(isl_map_is_equal(map, map2));
177 isl_map_free(map);
178 isl_map_free(map2);
180 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
181 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
182 if (test_parse_map_equal(ctx, str, str2) < 0)
183 return -1;
185 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
186 str2 = "{ [i,j] -> [min(i,j)] }";
187 if (test_parse_map_equal(ctx, str, str2) < 0)
188 return -1;
190 str = "{ [i,j] : i != j }";
191 str2 = "{ [i,j] : i < j or i > j }";
192 if (test_parse_map_equal(ctx, str, str2) < 0)
193 return -1;
195 str = "{ [i,j] : (i+1)*2 >= j }";
196 str2 = "{ [i, j] : j <= 2 + 2i }";
197 if (test_parse_map_equal(ctx, str, str2) < 0)
198 return -1;
200 str = "{ [i] -> [i > 0 ? 4 : 5] }";
201 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
202 if (test_parse_map_equal(ctx, str, str2) < 0)
203 return -1;
205 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
206 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
207 if (test_parse_map_equal(ctx, str, str2) < 0)
208 return -1;
210 str = "{ [x] : x >= 0 }";
211 str2 = "{ [x] : x-0 >= 0 }";
212 if (test_parse_map_equal(ctx, str, str2) < 0)
213 return -1;
215 str = "{ [i] : ((i > 10)) }";
216 str2 = "{ [i] : i >= 11 }";
217 if (test_parse_map_equal(ctx, str, str2) < 0)
218 return -1;
220 str = "{ [i] -> [0] }";
221 str2 = "{ [i] -> [0 * i] }";
222 if (test_parse_map_equal(ctx, str, str2) < 0)
223 return -1;
225 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
226 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
227 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
228 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
230 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
231 "{ [a] -> [b] : true }") < 0)
232 return -1;
234 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
235 "{ [i] : i <= 10 }") < 0)
236 return -1;
238 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
239 "[n] -> { [i] : i <= n }") < 0)
240 return -1;
242 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
243 return -1;
245 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
246 "{ [i] : exists a : i = 2 a }") < 0)
247 return -1;
249 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
250 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
251 return -1;
253 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
254 "{ [a] -> [a - 1] : a > 0 }") < 0)
255 return -1;
256 if (test_parse_map_equal(ctx,
257 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
258 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
259 return -1;
260 if (test_parse_map_equal(ctx,
261 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
262 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
263 return -1;
264 if (test_parse_map_equal(ctx,
265 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
266 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
267 return -1;
268 if (test_parse_map_equal(ctx,
269 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
270 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
271 return -1;
272 if (test_parse_map_equal(ctx,
273 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
274 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
275 return -1;
277 return 0;
280 static int test_read(isl_ctx *ctx)
282 char *filename;
283 FILE *input;
284 isl_basic_set *bset1, *bset2;
285 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
286 int equal;
288 filename = get_filename(ctx, "set", "omega");
289 assert(filename);
290 input = fopen(filename, "r");
291 assert(input);
293 bset1 = isl_basic_set_read_from_file(ctx, input);
294 bset2 = isl_basic_set_read_from_str(ctx, str);
296 equal = isl_basic_set_is_equal(bset1, bset2);
298 isl_basic_set_free(bset1);
299 isl_basic_set_free(bset2);
300 free(filename);
302 fclose(input);
304 if (equal < 0)
305 return -1;
306 if (!equal)
307 isl_die(ctx, isl_error_unknown,
308 "read sets not equal", return -1);
310 return 0;
313 static int test_bounded(isl_ctx *ctx)
315 isl_set *set;
316 int bounded;
318 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
319 bounded = isl_set_is_bounded(set);
320 isl_set_free(set);
322 if (bounded < 0)
323 return -1;
324 if (!bounded)
325 isl_die(ctx, isl_error_unknown,
326 "set not considered bounded", return -1);
328 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
329 bounded = isl_set_is_bounded(set);
330 assert(!bounded);
331 isl_set_free(set);
333 if (bounded < 0)
334 return -1;
335 if (bounded)
336 isl_die(ctx, isl_error_unknown,
337 "set considered bounded", return -1);
339 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
340 bounded = isl_set_is_bounded(set);
341 isl_set_free(set);
343 if (bounded < 0)
344 return -1;
345 if (bounded)
346 isl_die(ctx, isl_error_unknown,
347 "set considered bounded", return -1);
349 return 0;
352 /* Construct the basic set { [i] : 5 <= i <= N } */
353 static int test_construction(isl_ctx *ctx)
355 isl_int v;
356 isl_space *dim;
357 isl_local_space *ls;
358 isl_basic_set *bset;
359 isl_constraint *c;
361 isl_int_init(v);
363 dim = isl_space_set_alloc(ctx, 1, 1);
364 bset = isl_basic_set_universe(isl_space_copy(dim));
365 ls = isl_local_space_from_space(dim);
367 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
368 isl_int_set_si(v, -1);
369 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
370 isl_int_set_si(v, 1);
371 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
372 bset = isl_basic_set_add_constraint(bset, c);
374 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
375 isl_int_set_si(v, 1);
376 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
377 isl_int_set_si(v, -5);
378 isl_constraint_set_constant(c, v);
379 bset = isl_basic_set_add_constraint(bset, c);
381 isl_local_space_free(ls);
382 isl_basic_set_free(bset);
384 isl_int_clear(v);
386 return 0;
389 static int test_dim(isl_ctx *ctx)
391 const char *str;
392 isl_map *map1, *map2;
393 int equal;
395 map1 = isl_map_read_from_str(ctx,
396 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
397 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
398 map2 = isl_map_read_from_str(ctx,
399 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
400 equal = isl_map_is_equal(map1, map2);
401 isl_map_free(map2);
403 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
404 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
405 if (equal >= 0 && equal)
406 equal = isl_map_is_equal(map1, map2);
408 isl_map_free(map1);
409 isl_map_free(map2);
411 if (equal < 0)
412 return -1;
413 if (!equal)
414 isl_die(ctx, isl_error_unknown,
415 "unexpected result", return -1);
417 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
418 map1 = isl_map_read_from_str(ctx, str);
419 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
420 map2 = isl_map_read_from_str(ctx, str);
421 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
422 equal = isl_map_is_equal(map1, map2);
423 isl_map_free(map1);
424 isl_map_free(map2);
426 if (equal < 0)
427 return -1;
428 if (!equal)
429 isl_die(ctx, isl_error_unknown,
430 "unexpected result", return -1);
432 return 0;
435 struct {
436 __isl_give isl_val *(*op)(__isl_take isl_val *v);
437 const char *arg;
438 const char *res;
439 } val_un_tests[] = {
440 { &isl_val_neg, "0", "0" },
441 { &isl_val_abs, "0", "0" },
442 { &isl_val_2exp, "0", "1" },
443 { &isl_val_floor, "0", "0" },
444 { &isl_val_ceil, "0", "0" },
445 { &isl_val_neg, "1", "-1" },
446 { &isl_val_neg, "-1", "1" },
447 { &isl_val_neg, "1/2", "-1/2" },
448 { &isl_val_neg, "-1/2", "1/2" },
449 { &isl_val_neg, "infty", "-infty" },
450 { &isl_val_neg, "-infty", "infty" },
451 { &isl_val_neg, "NaN", "NaN" },
452 { &isl_val_abs, "1", "1" },
453 { &isl_val_abs, "-1", "1" },
454 { &isl_val_abs, "1/2", "1/2" },
455 { &isl_val_abs, "-1/2", "1/2" },
456 { &isl_val_abs, "infty", "infty" },
457 { &isl_val_abs, "-infty", "infty" },
458 { &isl_val_abs, "NaN", "NaN" },
459 { &isl_val_floor, "1", "1" },
460 { &isl_val_floor, "-1", "-1" },
461 { &isl_val_floor, "1/2", "0" },
462 { &isl_val_floor, "-1/2", "-1" },
463 { &isl_val_floor, "infty", "infty" },
464 { &isl_val_floor, "-infty", "-infty" },
465 { &isl_val_floor, "NaN", "NaN" },
466 { &isl_val_ceil, "1", "1" },
467 { &isl_val_ceil, "-1", "-1" },
468 { &isl_val_ceil, "1/2", "1" },
469 { &isl_val_ceil, "-1/2", "0" },
470 { &isl_val_ceil, "infty", "infty" },
471 { &isl_val_ceil, "-infty", "-infty" },
472 { &isl_val_ceil, "NaN", "NaN" },
473 { &isl_val_2exp, "-3", "1/8" },
474 { &isl_val_2exp, "-1", "1/2" },
475 { &isl_val_2exp, "1", "2" },
476 { &isl_val_2exp, "2", "4" },
477 { &isl_val_2exp, "3", "8" },
478 { &isl_val_inv, "1", "1" },
479 { &isl_val_inv, "2", "1/2" },
480 { &isl_val_inv, "1/2", "2" },
481 { &isl_val_inv, "-2", "-1/2" },
482 { &isl_val_inv, "-1/2", "-2" },
483 { &isl_val_inv, "0", "NaN" },
484 { &isl_val_inv, "NaN", "NaN" },
485 { &isl_val_inv, "infty", "0" },
486 { &isl_val_inv, "-infty", "0" },
489 /* Perform some basic tests of unary operations on isl_val objects.
491 static int test_un_val(isl_ctx *ctx)
493 int i;
494 isl_val *v, *res;
495 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
496 int ok;
498 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
499 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
500 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
501 fn = val_un_tests[i].op;
502 v = fn(v);
503 if (isl_val_is_nan(res))
504 ok = isl_val_is_nan(v);
505 else
506 ok = isl_val_eq(v, res);
507 isl_val_free(v);
508 isl_val_free(res);
509 if (ok < 0)
510 return -1;
511 if (!ok)
512 isl_die(ctx, isl_error_unknown,
513 "unexpected result", return -1);
516 return 0;
519 struct {
520 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
521 __isl_take isl_val *v2);
522 } val_bin_op[] = {
523 ['+'] = { &isl_val_add },
524 ['-'] = { &isl_val_sub },
525 ['*'] = { &isl_val_mul },
526 ['/'] = { &isl_val_div },
527 ['g'] = { &isl_val_gcd },
528 ['m'] = { &isl_val_min },
529 ['M'] = { &isl_val_max },
532 struct {
533 const char *arg1;
534 unsigned char op;
535 const char *arg2;
536 const char *res;
537 } val_bin_tests[] = {
538 { "0", '+', "0", "0" },
539 { "1", '+', "0", "1" },
540 { "1", '+', "1", "2" },
541 { "1", '-', "1", "0" },
542 { "1", '*', "1", "1" },
543 { "1", '/', "1", "1" },
544 { "2", '*', "3", "6" },
545 { "2", '*', "1/2", "1" },
546 { "2", '*', "1/3", "2/3" },
547 { "2/3", '*', "3/5", "2/5" },
548 { "2/3", '*', "7/5", "14/15" },
549 { "2", '/', "1/2", "4" },
550 { "-2", '/', "-1/2", "4" },
551 { "-2", '/', "1/2", "-4" },
552 { "2", '/', "-1/2", "-4" },
553 { "2", '/', "2", "1" },
554 { "2", '/', "3", "2/3" },
555 { "2/3", '/', "5/3", "2/5" },
556 { "2/3", '/', "5/7", "14/15" },
557 { "0", '/', "0", "NaN" },
558 { "42", '/', "0", "NaN" },
559 { "-42", '/', "0", "NaN" },
560 { "infty", '/', "0", "NaN" },
561 { "-infty", '/', "0", "NaN" },
562 { "NaN", '/', "0", "NaN" },
563 { "0", '/', "NaN", "NaN" },
564 { "42", '/', "NaN", "NaN" },
565 { "-42", '/', "NaN", "NaN" },
566 { "infty", '/', "NaN", "NaN" },
567 { "-infty", '/', "NaN", "NaN" },
568 { "NaN", '/', "NaN", "NaN" },
569 { "0", '/', "infty", "0" },
570 { "42", '/', "infty", "0" },
571 { "-42", '/', "infty", "0" },
572 { "infty", '/', "infty", "NaN" },
573 { "-infty", '/', "infty", "NaN" },
574 { "NaN", '/', "infty", "NaN" },
575 { "0", '/', "-infty", "0" },
576 { "42", '/', "-infty", "0" },
577 { "-42", '/', "-infty", "0" },
578 { "infty", '/', "-infty", "NaN" },
579 { "-infty", '/', "-infty", "NaN" },
580 { "NaN", '/', "-infty", "NaN" },
581 { "1", '-', "1/3", "2/3" },
582 { "1/3", '+', "1/2", "5/6" },
583 { "1/2", '+', "1/2", "1" },
584 { "3/4", '-', "1/4", "1/2" },
585 { "1/2", '-', "1/3", "1/6" },
586 { "infty", '+', "42", "infty" },
587 { "infty", '+', "infty", "infty" },
588 { "42", '+', "infty", "infty" },
589 { "infty", '-', "infty", "NaN" },
590 { "infty", '*', "infty", "infty" },
591 { "infty", '*', "-infty", "-infty" },
592 { "-infty", '*', "infty", "-infty" },
593 { "-infty", '*', "-infty", "infty" },
594 { "0", '*', "infty", "NaN" },
595 { "1", '*', "infty", "infty" },
596 { "infty", '*', "0", "NaN" },
597 { "infty", '*', "42", "infty" },
598 { "42", '-', "infty", "-infty" },
599 { "infty", '+', "-infty", "NaN" },
600 { "4", 'g', "6", "2" },
601 { "5", 'g', "6", "1" },
602 { "42", 'm', "3", "3" },
603 { "42", 'M', "3", "42" },
604 { "3", 'm', "42", "3" },
605 { "3", 'M', "42", "42" },
606 { "42", 'm', "infty", "42" },
607 { "42", 'M', "infty", "infty" },
608 { "42", 'm', "-infty", "-infty" },
609 { "42", 'M', "-infty", "42" },
610 { "42", 'm', "NaN", "NaN" },
611 { "42", 'M', "NaN", "NaN" },
612 { "infty", 'm', "-infty", "-infty" },
613 { "infty", 'M', "-infty", "infty" },
616 /* Perform some basic tests of binary operations on isl_val objects.
618 static int test_bin_val(isl_ctx *ctx)
620 int i;
621 isl_val *v1, *v2, *res;
622 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
623 __isl_take isl_val *v2);
624 int ok;
626 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
627 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
628 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
629 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
630 fn = val_bin_op[val_bin_tests[i].op].fn;
631 v1 = fn(v1, v2);
632 if (isl_val_is_nan(res))
633 ok = isl_val_is_nan(v1);
634 else
635 ok = isl_val_eq(v1, res);
636 isl_val_free(v1);
637 isl_val_free(res);
638 if (ok < 0)
639 return -1;
640 if (!ok)
641 isl_die(ctx, isl_error_unknown,
642 "unexpected result", return -1);
645 return 0;
648 /* Perform some basic tests on isl_val objects.
650 static int test_val(isl_ctx *ctx)
652 if (test_un_val(ctx) < 0)
653 return -1;
654 if (test_bin_val(ctx) < 0)
655 return -1;
656 return 0;
659 static int test_div(isl_ctx *ctx)
661 unsigned n;
662 const char *str;
663 int empty;
664 isl_int v;
665 isl_space *dim;
666 isl_set *set;
667 isl_local_space *ls;
668 struct isl_basic_set *bset;
669 struct isl_constraint *c;
671 isl_int_init(v);
673 /* test 1 */
674 dim = isl_space_set_alloc(ctx, 0, 3);
675 bset = isl_basic_set_universe(isl_space_copy(dim));
676 ls = isl_local_space_from_space(dim);
678 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
679 isl_int_set_si(v, -1);
680 isl_constraint_set_constant(c, v);
681 isl_int_set_si(v, 1);
682 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
683 isl_int_set_si(v, 3);
684 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
685 bset = isl_basic_set_add_constraint(bset, c);
687 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
688 isl_int_set_si(v, 1);
689 isl_constraint_set_constant(c, v);
690 isl_int_set_si(v, -1);
691 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
692 isl_int_set_si(v, 3);
693 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
694 bset = isl_basic_set_add_constraint(bset, c);
696 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
698 assert(bset && bset->n_div == 1);
699 isl_local_space_free(ls);
700 isl_basic_set_free(bset);
702 /* test 2 */
703 dim = isl_space_set_alloc(ctx, 0, 3);
704 bset = isl_basic_set_universe(isl_space_copy(dim));
705 ls = isl_local_space_from_space(dim);
707 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
708 isl_int_set_si(v, 1);
709 isl_constraint_set_constant(c, v);
710 isl_int_set_si(v, -1);
711 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
712 isl_int_set_si(v, 3);
713 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
714 bset = isl_basic_set_add_constraint(bset, c);
716 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
717 isl_int_set_si(v, -1);
718 isl_constraint_set_constant(c, v);
719 isl_int_set_si(v, 1);
720 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
721 isl_int_set_si(v, 3);
722 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
723 bset = isl_basic_set_add_constraint(bset, c);
725 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
727 assert(bset && bset->n_div == 1);
728 isl_local_space_free(ls);
729 isl_basic_set_free(bset);
731 /* test 3 */
732 dim = isl_space_set_alloc(ctx, 0, 3);
733 bset = isl_basic_set_universe(isl_space_copy(dim));
734 ls = isl_local_space_from_space(dim);
736 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
737 isl_int_set_si(v, 1);
738 isl_constraint_set_constant(c, v);
739 isl_int_set_si(v, -1);
740 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
741 isl_int_set_si(v, 3);
742 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
743 bset = isl_basic_set_add_constraint(bset, c);
745 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
746 isl_int_set_si(v, -3);
747 isl_constraint_set_constant(c, v);
748 isl_int_set_si(v, 1);
749 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
750 isl_int_set_si(v, 4);
751 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
752 bset = isl_basic_set_add_constraint(bset, c);
754 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
756 assert(bset && bset->n_div == 1);
757 isl_local_space_free(ls);
758 isl_basic_set_free(bset);
760 /* test 4 */
761 dim = isl_space_set_alloc(ctx, 0, 3);
762 bset = isl_basic_set_universe(isl_space_copy(dim));
763 ls = isl_local_space_from_space(dim);
765 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
766 isl_int_set_si(v, 2);
767 isl_constraint_set_constant(c, v);
768 isl_int_set_si(v, -1);
769 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
770 isl_int_set_si(v, 3);
771 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
772 bset = isl_basic_set_add_constraint(bset, c);
774 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
775 isl_int_set_si(v, -1);
776 isl_constraint_set_constant(c, v);
777 isl_int_set_si(v, 1);
778 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
779 isl_int_set_si(v, 6);
780 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
781 bset = isl_basic_set_add_constraint(bset, c);
783 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
785 assert(isl_basic_set_is_empty(bset));
786 isl_local_space_free(ls);
787 isl_basic_set_free(bset);
789 /* test 5 */
790 dim = isl_space_set_alloc(ctx, 0, 3);
791 bset = isl_basic_set_universe(isl_space_copy(dim));
792 ls = isl_local_space_from_space(dim);
794 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
795 isl_int_set_si(v, -1);
796 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
797 isl_int_set_si(v, 3);
798 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
799 bset = isl_basic_set_add_constraint(bset, c);
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, 1, v);
806 bset = isl_basic_set_add_constraint(bset, c);
808 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
810 assert(bset && bset->n_div == 0);
811 isl_basic_set_free(bset);
812 isl_local_space_free(ls);
814 /* test 6 */
815 dim = isl_space_set_alloc(ctx, 0, 3);
816 bset = isl_basic_set_universe(isl_space_copy(dim));
817 ls = isl_local_space_from_space(dim);
819 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
820 isl_int_set_si(v, -1);
821 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
822 isl_int_set_si(v, 6);
823 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
824 bset = isl_basic_set_add_constraint(bset, c);
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, -3);
830 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
831 bset = isl_basic_set_add_constraint(bset, c);
833 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
835 assert(bset && bset->n_div == 1);
836 isl_basic_set_free(bset);
837 isl_local_space_free(ls);
839 /* test 7 */
840 /* This test is a bit tricky. We set up an equality
841 * a + 3b + 3c = 6 e0
842 * Normalization of divs creates _two_ divs
843 * a = 3 e0
844 * c - b - e0 = 2 e1
845 * Afterwards e0 is removed again because it has coefficient -1
846 * and we end up with the original equality and div again.
847 * Perhaps we can avoid the introduction of this temporary div.
849 dim = isl_space_set_alloc(ctx, 0, 4);
850 bset = isl_basic_set_universe(isl_space_copy(dim));
851 ls = isl_local_space_from_space(dim);
853 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
854 isl_int_set_si(v, -1);
855 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
856 isl_int_set_si(v, -3);
857 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
858 isl_int_set_si(v, -3);
859 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
860 isl_int_set_si(v, 6);
861 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
862 bset = isl_basic_set_add_constraint(bset, c);
864 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
866 /* Test disabled for now */
868 assert(bset && bset->n_div == 1);
870 isl_local_space_free(ls);
871 isl_basic_set_free(bset);
873 /* test 8 */
874 dim = isl_space_set_alloc(ctx, 0, 5);
875 bset = isl_basic_set_universe(isl_space_copy(dim));
876 ls = isl_local_space_from_space(dim);
878 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
879 isl_int_set_si(v, -1);
880 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
881 isl_int_set_si(v, -3);
882 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
883 isl_int_set_si(v, -3);
884 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
885 isl_int_set_si(v, 6);
886 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
887 bset = isl_basic_set_add_constraint(bset, c);
889 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
890 isl_int_set_si(v, -1);
891 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
892 isl_int_set_si(v, 1);
893 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
894 isl_int_set_si(v, 1);
895 isl_constraint_set_constant(c, v);
896 bset = isl_basic_set_add_constraint(bset, c);
898 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
900 /* Test disabled for now */
902 assert(bset && bset->n_div == 1);
904 isl_local_space_free(ls);
905 isl_basic_set_free(bset);
907 /* test 9 */
908 dim = isl_space_set_alloc(ctx, 0, 4);
909 bset = isl_basic_set_universe(isl_space_copy(dim));
910 ls = isl_local_space_from_space(dim);
912 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
913 isl_int_set_si(v, 1);
914 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
915 isl_int_set_si(v, -1);
916 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
917 isl_int_set_si(v, -2);
918 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
919 bset = isl_basic_set_add_constraint(bset, c);
921 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
922 isl_int_set_si(v, -1);
923 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
924 isl_int_set_si(v, 3);
925 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
926 isl_int_set_si(v, 2);
927 isl_constraint_set_constant(c, v);
928 bset = isl_basic_set_add_constraint(bset, c);
930 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
932 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
934 assert(!isl_basic_set_is_empty(bset));
936 isl_local_space_free(ls);
937 isl_basic_set_free(bset);
939 /* test 10 */
940 dim = isl_space_set_alloc(ctx, 0, 3);
941 bset = isl_basic_set_universe(isl_space_copy(dim));
942 ls = isl_local_space_from_space(dim);
944 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
945 isl_int_set_si(v, 1);
946 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
947 isl_int_set_si(v, -2);
948 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
949 bset = isl_basic_set_add_constraint(bset, c);
951 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
953 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
955 isl_local_space_free(ls);
956 isl_basic_set_free(bset);
958 isl_int_clear(v);
960 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
961 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
962 set = isl_set_read_from_str(ctx, str);
963 set = isl_set_compute_divs(set);
964 isl_set_free(set);
965 if (!set)
966 return -1;
968 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
969 bset = isl_basic_set_read_from_str(ctx, str);
970 n = isl_basic_set_dim(bset, isl_dim_div);
971 isl_basic_set_free(bset);
972 if (!bset)
973 return -1;
974 if (n != 0)
975 isl_die(ctx, isl_error_unknown,
976 "expecting no existentials", 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 static int test_simple_hull(struct isl_ctx *ctx)
1110 const char *str;
1111 isl_set *set;
1112 isl_basic_set *bset;
1113 isl_bool is_empty;
1115 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1116 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1117 set = isl_set_read_from_str(ctx, str);
1118 bset = isl_set_simple_hull(set);
1119 is_empty = isl_basic_set_is_empty(bset);
1120 isl_basic_set_free(bset);
1122 if (is_empty == isl_bool_error)
1123 return -1;
1125 if (is_empty == isl_bool_false)
1126 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1127 return -1);
1129 return 0;
1132 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1134 char *filename;
1135 FILE *input;
1136 struct isl_basic_set *bset1, *bset2;
1137 struct isl_set *set;
1139 filename = get_filename(ctx, name, "polylib");
1140 assert(filename);
1141 input = fopen(filename, "r");
1142 assert(input);
1144 bset1 = isl_basic_set_read_from_file(ctx, input);
1145 bset2 = isl_basic_set_read_from_file(ctx, input);
1147 set = isl_basic_set_union(bset1, bset2);
1148 bset1 = isl_set_convex_hull(set);
1150 bset2 = isl_basic_set_read_from_file(ctx, input);
1152 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1154 isl_basic_set_free(bset1);
1155 isl_basic_set_free(bset2);
1156 free(filename);
1158 fclose(input);
1161 struct {
1162 const char *set;
1163 const char *hull;
1164 } convex_hull_tests[] = {
1165 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1166 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1167 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1168 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1169 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1170 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1171 "i2 <= 5 and i2 >= 4; "
1172 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1173 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1174 "i2 <= 5 + i0 and i2 >= i0 }" },
1175 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1176 "{ [x, y] : 1 = 0 }" },
1179 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1181 int i;
1182 int orig_convex = ctx->opt->convex;
1183 ctx->opt->convex = convex;
1185 test_convex_hull_case(ctx, "convex0");
1186 test_convex_hull_case(ctx, "convex1");
1187 test_convex_hull_case(ctx, "convex2");
1188 test_convex_hull_case(ctx, "convex3");
1189 test_convex_hull_case(ctx, "convex4");
1190 test_convex_hull_case(ctx, "convex5");
1191 test_convex_hull_case(ctx, "convex6");
1192 test_convex_hull_case(ctx, "convex7");
1193 test_convex_hull_case(ctx, "convex8");
1194 test_convex_hull_case(ctx, "convex9");
1195 test_convex_hull_case(ctx, "convex10");
1196 test_convex_hull_case(ctx, "convex11");
1197 test_convex_hull_case(ctx, "convex12");
1198 test_convex_hull_case(ctx, "convex13");
1199 test_convex_hull_case(ctx, "convex14");
1200 test_convex_hull_case(ctx, "convex15");
1202 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1203 isl_set *set1, *set2;
1204 int equal;
1206 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1207 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1208 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1209 equal = isl_set_is_equal(set1, set2);
1210 isl_set_free(set1);
1211 isl_set_free(set2);
1213 if (equal < 0)
1214 return -1;
1215 if (!equal)
1216 isl_die(ctx, isl_error_unknown,
1217 "unexpected convex hull", return -1);
1220 ctx->opt->convex = orig_convex;
1222 return 0;
1225 static int test_convex_hull(isl_ctx *ctx)
1227 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1228 return -1;
1229 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1230 return -1;
1231 return 0;
1234 void test_gist_case(struct isl_ctx *ctx, const char *name)
1236 char *filename;
1237 FILE *input;
1238 struct isl_basic_set *bset1, *bset2;
1240 filename = get_filename(ctx, name, "polylib");
1241 assert(filename);
1242 input = fopen(filename, "r");
1243 assert(input);
1245 bset1 = isl_basic_set_read_from_file(ctx, input);
1246 bset2 = isl_basic_set_read_from_file(ctx, input);
1248 bset1 = isl_basic_set_gist(bset1, bset2);
1250 bset2 = isl_basic_set_read_from_file(ctx, input);
1252 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1254 isl_basic_set_free(bset1);
1255 isl_basic_set_free(bset2);
1256 free(filename);
1258 fclose(input);
1261 struct {
1262 const char *set;
1263 const char *context;
1264 const char *gist;
1265 } gist_tests[] = {
1266 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1267 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1268 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1269 "{ [a, b, c] : a <= 15 }" },
1270 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1271 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1272 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1273 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1274 { "{ [m, n, a, b] : a <= 2147 + n }",
1275 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1276 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1277 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1278 "b >= 0) }",
1279 "{ [m, n, ku, kl] }" },
1280 { "{ [a, a, b] : a >= 10 }",
1281 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1282 "{ [a, a, b] : a >= 10 }" },
1283 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1284 "{ [0, j] : j >= 0 }" },
1285 /* Check that no constraints on i6 are introduced in the gist */
1286 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1287 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1288 "5e0 <= 381 - t1 and i4 <= 1) }",
1289 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1290 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1291 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1292 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1293 "20e0 >= 1511 - 4t1 - 5i4) }" },
1296 static int test_gist(struct isl_ctx *ctx)
1298 int i;
1299 const char *str;
1300 isl_basic_set *bset1, *bset2;
1301 isl_map *map1, *map2;
1302 int equal;
1304 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1305 int equal_input;
1306 isl_set *set1, *set2, *copy;
1308 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1309 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1310 copy = isl_set_copy(set1);
1311 set1 = isl_set_gist(set1, set2);
1312 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1313 equal = isl_set_is_equal(set1, set2);
1314 isl_set_free(set1);
1315 isl_set_free(set2);
1316 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1317 equal_input = isl_set_is_equal(set1, copy);
1318 isl_set_free(set1);
1319 isl_set_free(copy);
1320 if (equal < 0 || equal_input < 0)
1321 return -1;
1322 if (!equal)
1323 isl_die(ctx, isl_error_unknown,
1324 "incorrect gist result", return -1);
1325 if (!equal_input)
1326 isl_die(ctx, isl_error_unknown,
1327 "gist modified input", return -1);
1330 test_gist_case(ctx, "gist1");
1332 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1333 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1334 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1335 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1336 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1337 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1338 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1339 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1340 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1341 bset1 = isl_basic_set_read_from_str(ctx, str);
1342 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1343 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1344 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1345 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1346 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1347 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1348 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1349 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1350 bset2 = isl_basic_set_read_from_str(ctx, str);
1351 bset1 = isl_basic_set_gist(bset1, bset2);
1352 assert(bset1 && bset1->n_div == 0);
1353 isl_basic_set_free(bset1);
1355 /* Check that the integer divisions of the second disjunct
1356 * do not spread to the first disjunct.
1358 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1359 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1360 "(exists (e0 = [(-1 + t1)/16], "
1361 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1362 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1363 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1364 "o0 <= 4294967295 and t1 <= -1)) }";
1365 map1 = isl_map_read_from_str(ctx, str);
1366 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1367 map2 = isl_map_read_from_str(ctx, str);
1368 map1 = isl_map_gist(map1, map2);
1369 if (!map1)
1370 return -1;
1371 if (map1->n != 1)
1372 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1373 isl_map_free(map1); return -1);
1374 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1375 isl_die(ctx, isl_error_unknown, "expecting single div",
1376 isl_map_free(map1); return -1);
1377 isl_map_free(map1);
1379 return 0;
1382 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1384 isl_set *set, *set2;
1385 int equal;
1386 int one;
1388 set = isl_set_read_from_str(ctx, str);
1389 set = isl_set_coalesce(set);
1390 set2 = isl_set_read_from_str(ctx, str);
1391 equal = isl_set_is_equal(set, set2);
1392 one = set && set->n == 1;
1393 isl_set_free(set);
1394 isl_set_free(set2);
1396 if (equal < 0)
1397 return -1;
1398 if (!equal)
1399 isl_die(ctx, isl_error_unknown,
1400 "coalesced set not equal to input", return -1);
1401 if (check_one && !one)
1402 isl_die(ctx, isl_error_unknown,
1403 "coalesced set should not be a union", return -1);
1405 return 0;
1408 /* Inputs for coalescing tests with unbounded wrapping.
1409 * "str" is a string representation of the input set.
1410 * "single_disjunct" is set if we expect the result to consist of
1411 * a single disjunct.
1413 struct {
1414 int single_disjunct;
1415 const char *str;
1416 } coalesce_unbounded_tests[] = {
1417 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1418 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1419 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1420 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1421 "-10 <= y <= 0}" },
1422 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1423 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1424 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1425 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1428 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1429 * option turned off.
1431 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1433 int i;
1434 int r = 0;
1435 int bounded;
1437 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1438 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1440 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1441 const char *str = coalesce_unbounded_tests[i].str;
1442 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1443 if (test_coalesce_set(ctx, str, check_one) >= 0)
1444 continue;
1445 r = -1;
1446 break;
1449 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1451 return r;
1454 /* Inputs for coalescing tests.
1455 * "str" is a string representation of the input set.
1456 * "single_disjunct" is set if we expect the result to consist of
1457 * a single disjunct.
1459 struct {
1460 int single_disjunct;
1461 const char *str;
1462 } coalesce_tests[] = {
1463 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1464 "y >= x & x >= 2 & 5 >= y }" },
1465 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1466 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1467 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1468 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1469 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1470 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1471 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1472 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1473 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1474 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1475 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1476 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1477 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1478 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1479 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1480 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1481 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1482 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1483 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1484 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1485 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1486 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1487 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1488 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1489 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1490 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1491 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1492 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1493 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1494 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1495 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1496 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1497 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1498 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1499 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1500 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1501 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1502 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1503 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1504 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1505 "[o0, o1, o2, o3, o4, o5, o6]] : "
1506 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1507 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1508 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1509 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1510 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1511 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1512 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1513 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1514 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1515 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1516 "o6 >= i3 + i6 - o3 and M >= 0 and "
1517 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1518 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1519 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1520 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1521 "(o0 = 0 and M >= 2 and N >= 3) or "
1522 "(M = 0 and o0 = 0 and N >= 3) }" },
1523 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1524 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1525 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1526 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1527 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1528 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1529 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1530 "(y = 3 and x = 1) }" },
1531 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1532 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1533 "i1 <= M and i3 <= M and i4 <= M) or "
1534 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1535 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1536 "i4 <= -1 + M) }" },
1537 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1538 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1539 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1540 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1541 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1542 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1543 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1544 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1545 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1546 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1547 { 0, "{ [a, b] : exists e : 2e = a and "
1548 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1549 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1550 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1551 "j >= 1 and j' <= i + j - i' and i >= 1; "
1552 "[1, 1, 1, 1] }" },
1553 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1554 "[i,j] : exists a : j = 3a }" },
1555 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1556 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1557 "a >= 3) or "
1558 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1559 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1560 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1561 "c <= 6 + 8a and a >= 3; "
1562 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1563 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1564 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1565 "[x,0] : 3 <= x <= 4 }" },
1566 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1567 "[x,0] : 4 <= x <= 5 }" },
1568 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1569 "[x,0] : 3 <= x <= 5 }" },
1570 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1571 "[x,0] : 3 <= x <= 4 }" },
1572 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1573 "i1 <= 0; "
1574 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1575 { 1, "{ [0,0]; [1,1] }" },
1576 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1577 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1578 "ii <= k;"
1579 "[k, 0, k] : k <= 6 and k >= 1 }" },
1580 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1581 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1582 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1583 { 1, "[n] -> { [1] : n >= 0;"
1584 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1585 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1586 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1587 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1588 "2e0 <= x and 2e0 <= n);"
1589 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1590 "n >= 0) }" },
1591 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1592 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1593 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1594 "t1 = 1 }" },
1595 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1596 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1597 "[0, 0] }" },
1598 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1599 "t1 >= 13 and t1 <= 16);"
1600 "[t1] : t1 <= 15 and t1 >= 12 }" },
1601 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1602 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1603 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1604 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1605 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1606 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1607 "i <= 4j + 2 }" },
1608 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1609 "(exists (e0 : 3e0 = -2 + c0)) }" },
1610 { 0, "[n, b0, t0] -> "
1611 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1612 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1613 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1614 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1615 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1616 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1617 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1618 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1619 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1620 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1621 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1622 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1623 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1624 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1625 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1626 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1627 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1628 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1629 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1630 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1631 { 0, "{ [i0, i1, i2] : "
1632 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1633 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1634 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1635 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1636 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1637 "32e0 <= 31 + i0)) or "
1638 "i0 >= 0 }" },
1639 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1642 /* A specialized coalescing test case that would result
1643 * in a segmentation fault or a failed assertion in earlier versions of isl.
1645 static int test_coalesce_special(struct isl_ctx *ctx)
1647 const char *str;
1648 isl_map *map1, *map2;
1650 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1651 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1652 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1653 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1654 "o1 <= 239 and o1 >= 212)) or "
1655 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1656 "o1 <= 241 and o1 >= 240));"
1657 "[S_L220_OUT[] -> T7[]] -> "
1658 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1659 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1660 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1661 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1662 map1 = isl_map_read_from_str(ctx, str);
1663 map1 = isl_map_align_divs(map1);
1664 map1 = isl_map_coalesce(map1);
1665 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1666 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1667 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1668 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1669 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1670 map2 = isl_map_read_from_str(ctx, str);
1671 map2 = isl_map_union(map2, map1);
1672 map2 = isl_map_align_divs(map2);
1673 map2 = isl_map_coalesce(map2);
1674 isl_map_free(map2);
1675 if (!map2)
1676 return -1;
1678 return 0;
1681 /* Test the functionality of isl_set_coalesce.
1682 * That is, check that the output is always equal to the input
1683 * and in some cases that the result consists of a single disjunct.
1685 static int test_coalesce(struct isl_ctx *ctx)
1687 int i;
1689 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1690 const char *str = coalesce_tests[i].str;
1691 int check_one = coalesce_tests[i].single_disjunct;
1692 if (test_coalesce_set(ctx, str, check_one) < 0)
1693 return -1;
1696 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1697 return -1;
1698 if (test_coalesce_special(ctx) < 0)
1699 return -1;
1701 return 0;
1704 static int test_closure(isl_ctx *ctx)
1706 const char *str;
1707 isl_set *dom;
1708 isl_map *up, *right;
1709 isl_map *map, *map2;
1710 int exact;
1712 /* COCOA example 1 */
1713 map = isl_map_read_from_str(ctx,
1714 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1715 "1 <= i and i < n and 1 <= j and j < n or "
1716 "i2 = i + 1 and j2 = j - 1 and "
1717 "1 <= i and i < n and 2 <= j and j <= n }");
1718 map = isl_map_power(map, &exact);
1719 assert(exact);
1720 isl_map_free(map);
1722 /* COCOA example 1 */
1723 map = isl_map_read_from_str(ctx,
1724 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1725 "1 <= i and i < n and 1 <= j and j < n or "
1726 "i2 = i + 1 and j2 = j - 1 and "
1727 "1 <= i and i < n and 2 <= j and j <= n }");
1728 map = isl_map_transitive_closure(map, &exact);
1729 assert(exact);
1730 map2 = isl_map_read_from_str(ctx,
1731 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1732 "1 <= i and i < n and 1 <= j and j <= n and "
1733 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1734 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1735 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1736 assert(isl_map_is_equal(map, map2));
1737 isl_map_free(map2);
1738 isl_map_free(map);
1740 map = isl_map_read_from_str(ctx,
1741 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1742 " 0 <= y and y <= n }");
1743 map = isl_map_transitive_closure(map, &exact);
1744 map2 = isl_map_read_from_str(ctx,
1745 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1746 " 0 <= y and y <= n }");
1747 assert(isl_map_is_equal(map, map2));
1748 isl_map_free(map2);
1749 isl_map_free(map);
1751 /* COCOA example 2 */
1752 map = isl_map_read_from_str(ctx,
1753 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1754 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1755 "i2 = i + 2 and j2 = j - 2 and "
1756 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1757 map = isl_map_transitive_closure(map, &exact);
1758 assert(exact);
1759 map2 = isl_map_read_from_str(ctx,
1760 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1761 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1762 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1763 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1764 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1765 assert(isl_map_is_equal(map, map2));
1766 isl_map_free(map);
1767 isl_map_free(map2);
1769 /* COCOA Fig.2 left */
1770 map = isl_map_read_from_str(ctx,
1771 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1772 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1773 "j <= n or "
1774 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1775 "j <= 2 i - 3 and j <= n - 2 or "
1776 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1777 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1778 map = isl_map_transitive_closure(map, &exact);
1779 assert(exact);
1780 isl_map_free(map);
1782 /* COCOA Fig.2 right */
1783 map = isl_map_read_from_str(ctx,
1784 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1785 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1786 "j <= n or "
1787 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1788 "j <= 2 i - 4 and j <= n - 3 or "
1789 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1790 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1791 map = isl_map_power(map, &exact);
1792 assert(exact);
1793 isl_map_free(map);
1795 /* COCOA Fig.2 right */
1796 map = isl_map_read_from_str(ctx,
1797 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1798 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1799 "j <= n or "
1800 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1801 "j <= 2 i - 4 and j <= n - 3 or "
1802 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1803 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1804 map = isl_map_transitive_closure(map, &exact);
1805 assert(exact);
1806 map2 = isl_map_read_from_str(ctx,
1807 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1808 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1809 "j <= n and 3 + i + 2 j <= 3 n and "
1810 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1811 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1812 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1813 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1814 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1815 assert(isl_map_is_equal(map, map2));
1816 isl_map_free(map2);
1817 isl_map_free(map);
1819 /* COCOA Fig.1 right */
1820 dom = isl_set_read_from_str(ctx,
1821 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1822 "2 x - 3 y + 3 >= 0 }");
1823 right = isl_map_read_from_str(ctx,
1824 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1825 up = isl_map_read_from_str(ctx,
1826 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1827 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1828 right = isl_map_intersect_range(right, isl_set_copy(dom));
1829 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1830 up = isl_map_intersect_range(up, dom);
1831 map = isl_map_union(up, right);
1832 map = isl_map_transitive_closure(map, &exact);
1833 assert(exact);
1834 map2 = isl_map_read_from_str(ctx,
1835 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1836 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1837 assert(isl_map_is_equal(map, map2));
1838 isl_map_free(map2);
1839 isl_map_free(map);
1841 /* COCOA Theorem 1 counter example */
1842 map = isl_map_read_from_str(ctx,
1843 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1844 "i2 = 1 and j2 = j or "
1845 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1846 map = isl_map_transitive_closure(map, &exact);
1847 assert(exact);
1848 isl_map_free(map);
1850 map = isl_map_read_from_str(ctx,
1851 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1852 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1853 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1854 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1855 map = isl_map_transitive_closure(map, &exact);
1856 assert(exact);
1857 isl_map_free(map);
1859 /* Kelly et al 1996, fig 12 */
1860 map = isl_map_read_from_str(ctx,
1861 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1862 "1 <= i,j,j+1 <= n or "
1863 "j = n and j2 = 1 and i2 = i + 1 and "
1864 "1 <= i,i+1 <= n }");
1865 map = isl_map_transitive_closure(map, &exact);
1866 assert(exact);
1867 map2 = isl_map_read_from_str(ctx,
1868 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1869 "1 <= i <= n and i = i2 or "
1870 "1 <= i < i2 <= n and 1 <= j <= n and "
1871 "1 <= j2 <= n }");
1872 assert(isl_map_is_equal(map, map2));
1873 isl_map_free(map2);
1874 isl_map_free(map);
1876 /* Omega's closure4 */
1877 map = isl_map_read_from_str(ctx,
1878 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1879 "1 <= x,y <= 10 or "
1880 "x2 = x + 1 and y2 = y and "
1881 "1 <= x <= 20 && 5 <= y <= 15 }");
1882 map = isl_map_transitive_closure(map, &exact);
1883 assert(exact);
1884 isl_map_free(map);
1886 map = isl_map_read_from_str(ctx,
1887 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1888 map = isl_map_transitive_closure(map, &exact);
1889 assert(!exact);
1890 map2 = isl_map_read_from_str(ctx,
1891 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1892 assert(isl_map_is_equal(map, map2));
1893 isl_map_free(map);
1894 isl_map_free(map2);
1896 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1897 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1898 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1899 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1900 map = isl_map_read_from_str(ctx, str);
1901 map = isl_map_transitive_closure(map, &exact);
1902 assert(exact);
1903 map2 = isl_map_read_from_str(ctx, str);
1904 assert(isl_map_is_equal(map, map2));
1905 isl_map_free(map);
1906 isl_map_free(map2);
1908 str = "{[0] -> [1]; [2] -> [3]}";
1909 map = isl_map_read_from_str(ctx, str);
1910 map = isl_map_transitive_closure(map, &exact);
1911 assert(exact);
1912 map2 = isl_map_read_from_str(ctx, str);
1913 assert(isl_map_is_equal(map, map2));
1914 isl_map_free(map);
1915 isl_map_free(map2);
1917 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1918 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1919 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1920 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1921 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1922 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1923 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1924 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1925 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1926 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1927 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1928 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1929 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1930 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1931 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1932 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1933 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1934 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1935 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1936 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1937 map = isl_map_read_from_str(ctx, str);
1938 map = isl_map_transitive_closure(map, NULL);
1939 assert(map);
1940 isl_map_free(map);
1942 return 0;
1945 static int test_lex(struct isl_ctx *ctx)
1947 isl_space *dim;
1948 isl_map *map;
1949 int empty;
1951 dim = isl_space_set_alloc(ctx, 0, 0);
1952 map = isl_map_lex_le(dim);
1953 empty = isl_map_is_empty(map);
1954 isl_map_free(map);
1956 if (empty < 0)
1957 return -1;
1958 if (empty)
1959 isl_die(ctx, isl_error_unknown,
1960 "expecting non-empty result", return -1);
1962 return 0;
1965 static int test_lexmin(struct isl_ctx *ctx)
1967 int equal;
1968 const char *str;
1969 isl_basic_map *bmap;
1970 isl_map *map, *map2;
1971 isl_set *set;
1972 isl_set *set2;
1973 isl_pw_multi_aff *pma;
1975 str = "[p0, p1] -> { [] -> [] : "
1976 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1977 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1978 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1979 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1980 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1981 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1982 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1983 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1984 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1985 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1986 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1987 map = isl_map_read_from_str(ctx, str);
1988 map = isl_map_lexmin(map);
1989 isl_map_free(map);
1991 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1992 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1993 set = isl_set_read_from_str(ctx, str);
1994 set = isl_set_lexmax(set);
1995 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1996 set2 = isl_set_read_from_str(ctx, str);
1997 set = isl_set_intersect(set, set2);
1998 assert(!isl_set_is_empty(set));
1999 isl_set_free(set);
2001 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2002 map = isl_map_read_from_str(ctx, str);
2003 map = isl_map_lexmin(map);
2004 str = "{ [x] -> [5] : 6 <= x <= 8; "
2005 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2006 map2 = isl_map_read_from_str(ctx, str);
2007 assert(isl_map_is_equal(map, map2));
2008 isl_map_free(map);
2009 isl_map_free(map2);
2011 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2012 map = isl_map_read_from_str(ctx, str);
2013 map2 = isl_map_copy(map);
2014 map = isl_map_lexmin(map);
2015 assert(isl_map_is_equal(map, map2));
2016 isl_map_free(map);
2017 isl_map_free(map2);
2019 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2020 map = isl_map_read_from_str(ctx, str);
2021 map = isl_map_lexmin(map);
2022 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2023 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2024 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2025 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2026 map2 = isl_map_read_from_str(ctx, str);
2027 assert(isl_map_is_equal(map, map2));
2028 isl_map_free(map);
2029 isl_map_free(map2);
2031 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2032 " 8i' <= i and 8i' >= -7 + i }";
2033 bmap = isl_basic_map_read_from_str(ctx, str);
2034 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2035 map2 = isl_map_from_pw_multi_aff(pma);
2036 map = isl_map_from_basic_map(bmap);
2037 assert(isl_map_is_equal(map, map2));
2038 isl_map_free(map);
2039 isl_map_free(map2);
2041 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2042 map = isl_map_read_from_str(ctx, str);
2043 map = isl_map_lexmin(map);
2044 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2045 map2 = isl_map_read_from_str(ctx, str);
2046 assert(isl_map_is_equal(map, map2));
2047 isl_map_free(map);
2048 isl_map_free(map2);
2050 /* Check that empty pieces are properly combined. */
2051 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2052 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2053 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2054 map = isl_map_read_from_str(ctx, str);
2055 map = isl_map_lexmin(map);
2056 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2057 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2058 "x >= 4 }";
2059 map2 = isl_map_read_from_str(ctx, str);
2060 assert(isl_map_is_equal(map, map2));
2061 isl_map_free(map);
2062 isl_map_free(map2);
2064 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2065 " 8i' <= i and 8i' >= -7 + i }";
2066 set = isl_set_read_from_str(ctx, str);
2067 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2068 set2 = isl_set_from_pw_multi_aff(pma);
2069 equal = isl_set_is_equal(set, set2);
2070 isl_set_free(set);
2071 isl_set_free(set2);
2072 if (equal < 0)
2073 return -1;
2074 if (!equal)
2075 isl_die(ctx, isl_error_unknown,
2076 "unexpected difference between set and "
2077 "piecewise affine expression", return -1);
2079 return 0;
2082 /* Check that isl_set_min_val and isl_set_max_val compute the correct
2083 * result on non-convex inputs.
2085 static int test_min(struct isl_ctx *ctx)
2087 isl_set *set;
2088 isl_aff *aff;
2089 isl_val *val;
2090 int min_ok, max_ok;
2092 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
2093 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
2094 val = isl_set_min_val(set, aff);
2095 min_ok = isl_val_is_negone(val);
2096 isl_val_free(val);
2097 val = isl_set_max_val(set, aff);
2098 max_ok = isl_val_is_one(val);
2099 isl_val_free(val);
2100 isl_aff_free(aff);
2101 isl_set_free(set);
2103 if (min_ok < 0 || max_ok < 0)
2104 return -1;
2105 if (!min_ok)
2106 isl_die(ctx, isl_error_unknown,
2107 "unexpected minimum", return -1);
2108 if (!max_ok)
2109 isl_die(ctx, isl_error_unknown,
2110 "unexpected maximum", return -1);
2112 return 0;
2115 struct must_may {
2116 isl_map *must;
2117 isl_map *may;
2120 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2121 void *dep_user, void *user)
2123 struct must_may *mm = (struct must_may *)user;
2125 if (must)
2126 mm->must = isl_map_union(mm->must, dep);
2127 else
2128 mm->may = isl_map_union(mm->may, dep);
2130 return isl_stat_ok;
2133 static int common_space(void *first, void *second)
2135 int depth = *(int *)first;
2136 return 2 * depth;
2139 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2141 isl_map *map2;
2142 int equal;
2144 if (!map)
2145 return -1;
2147 map2 = isl_map_read_from_str(map->ctx, str);
2148 equal = isl_map_is_equal(map, map2);
2149 isl_map_free(map2);
2151 return equal;
2154 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2156 int equal;
2158 equal = map_is_equal(map, str);
2159 if (equal < 0)
2160 return -1;
2161 if (!equal)
2162 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2163 "result not as expected", return -1);
2164 return 0;
2167 static int test_dep(struct isl_ctx *ctx)
2169 const char *str;
2170 isl_space *dim;
2171 isl_map *map;
2172 isl_access_info *ai;
2173 isl_flow *flow;
2174 int depth;
2175 struct must_may mm;
2177 depth = 3;
2179 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2180 map = isl_map_read_from_str(ctx, str);
2181 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2183 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2184 map = isl_map_read_from_str(ctx, str);
2185 ai = isl_access_info_add_source(ai, map, 1, &depth);
2187 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2188 map = isl_map_read_from_str(ctx, str);
2189 ai = isl_access_info_add_source(ai, map, 1, &depth);
2191 flow = isl_access_info_compute_flow(ai);
2192 dim = isl_space_alloc(ctx, 0, 3, 3);
2193 mm.must = isl_map_empty(isl_space_copy(dim));
2194 mm.may = isl_map_empty(dim);
2196 isl_flow_foreach(flow, collect_must_may, &mm);
2198 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2199 " [1,10,0] -> [2,5,0] }";
2200 assert(map_is_equal(mm.must, str));
2201 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2202 assert(map_is_equal(mm.may, str));
2204 isl_map_free(mm.must);
2205 isl_map_free(mm.may);
2206 isl_flow_free(flow);
2209 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2210 map = isl_map_read_from_str(ctx, str);
2211 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2213 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2214 map = isl_map_read_from_str(ctx, str);
2215 ai = isl_access_info_add_source(ai, map, 1, &depth);
2217 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2218 map = isl_map_read_from_str(ctx, str);
2219 ai = isl_access_info_add_source(ai, map, 0, &depth);
2221 flow = isl_access_info_compute_flow(ai);
2222 dim = isl_space_alloc(ctx, 0, 3, 3);
2223 mm.must = isl_map_empty(isl_space_copy(dim));
2224 mm.may = isl_map_empty(dim);
2226 isl_flow_foreach(flow, collect_must_may, &mm);
2228 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2229 assert(map_is_equal(mm.must, str));
2230 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2231 assert(map_is_equal(mm.may, str));
2233 isl_map_free(mm.must);
2234 isl_map_free(mm.may);
2235 isl_flow_free(flow);
2238 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2239 map = isl_map_read_from_str(ctx, str);
2240 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2242 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2243 map = isl_map_read_from_str(ctx, str);
2244 ai = isl_access_info_add_source(ai, map, 0, &depth);
2246 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2247 map = isl_map_read_from_str(ctx, str);
2248 ai = isl_access_info_add_source(ai, map, 0, &depth);
2250 flow = isl_access_info_compute_flow(ai);
2251 dim = isl_space_alloc(ctx, 0, 3, 3);
2252 mm.must = isl_map_empty(isl_space_copy(dim));
2253 mm.may = isl_map_empty(dim);
2255 isl_flow_foreach(flow, collect_must_may, &mm);
2257 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2258 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2259 assert(map_is_equal(mm.may, str));
2260 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2261 assert(map_is_equal(mm.must, str));
2263 isl_map_free(mm.must);
2264 isl_map_free(mm.may);
2265 isl_flow_free(flow);
2268 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2269 map = isl_map_read_from_str(ctx, str);
2270 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2272 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2273 map = isl_map_read_from_str(ctx, str);
2274 ai = isl_access_info_add_source(ai, map, 0, &depth);
2276 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2277 map = isl_map_read_from_str(ctx, str);
2278 ai = isl_access_info_add_source(ai, map, 0, &depth);
2280 flow = isl_access_info_compute_flow(ai);
2281 dim = isl_space_alloc(ctx, 0, 3, 3);
2282 mm.must = isl_map_empty(isl_space_copy(dim));
2283 mm.may = isl_map_empty(dim);
2285 isl_flow_foreach(flow, collect_must_may, &mm);
2287 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2288 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2289 assert(map_is_equal(mm.may, str));
2290 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2291 assert(map_is_equal(mm.must, str));
2293 isl_map_free(mm.must);
2294 isl_map_free(mm.may);
2295 isl_flow_free(flow);
2298 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2299 map = isl_map_read_from_str(ctx, str);
2300 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2302 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2303 map = isl_map_read_from_str(ctx, str);
2304 ai = isl_access_info_add_source(ai, map, 0, &depth);
2306 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2307 map = isl_map_read_from_str(ctx, str);
2308 ai = isl_access_info_add_source(ai, map, 0, &depth);
2310 flow = isl_access_info_compute_flow(ai);
2311 dim = isl_space_alloc(ctx, 0, 3, 3);
2312 mm.must = isl_map_empty(isl_space_copy(dim));
2313 mm.may = isl_map_empty(dim);
2315 isl_flow_foreach(flow, collect_must_may, &mm);
2317 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2318 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2319 assert(map_is_equal(mm.may, str));
2320 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2321 assert(map_is_equal(mm.must, str));
2323 isl_map_free(mm.must);
2324 isl_map_free(mm.may);
2325 isl_flow_free(flow);
2328 depth = 5;
2330 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2331 map = isl_map_read_from_str(ctx, str);
2332 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2334 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2335 map = isl_map_read_from_str(ctx, str);
2336 ai = isl_access_info_add_source(ai, map, 1, &depth);
2338 flow = isl_access_info_compute_flow(ai);
2339 dim = isl_space_alloc(ctx, 0, 5, 5);
2340 mm.must = isl_map_empty(isl_space_copy(dim));
2341 mm.may = isl_map_empty(dim);
2343 isl_flow_foreach(flow, collect_must_may, &mm);
2345 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2346 assert(map_is_equal(mm.must, str));
2347 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2348 assert(map_is_equal(mm.may, str));
2350 isl_map_free(mm.must);
2351 isl_map_free(mm.may);
2352 isl_flow_free(flow);
2354 return 0;
2357 /* Check that the dependence analysis proceeds without errors.
2358 * Earlier versions of isl would break down during the analysis
2359 * due to the use of the wrong spaces.
2361 static int test_flow(isl_ctx *ctx)
2363 const char *str;
2364 isl_union_map *access, *schedule;
2365 isl_union_map *must_dep, *may_dep;
2366 int r;
2368 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2369 access = isl_union_map_read_from_str(ctx, str);
2370 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2371 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2372 "S2[] -> [1,0,0,0]; "
2373 "S3[] -> [-1,0,0,0] }";
2374 schedule = isl_union_map_read_from_str(ctx, str);
2375 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2376 isl_union_map_copy(access), schedule,
2377 &must_dep, &may_dep, NULL, NULL);
2378 isl_union_map_free(may_dep);
2379 isl_union_map_free(must_dep);
2381 return r;
2384 struct {
2385 const char *map;
2386 int sv;
2387 } sv_tests[] = {
2388 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2389 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2390 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2391 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2392 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2393 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2394 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2395 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2396 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2399 int test_sv(isl_ctx *ctx)
2401 isl_union_map *umap;
2402 int i;
2403 int sv;
2405 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2406 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2407 sv = isl_union_map_is_single_valued(umap);
2408 isl_union_map_free(umap);
2409 if (sv < 0)
2410 return -1;
2411 if (sv_tests[i].sv && !sv)
2412 isl_die(ctx, isl_error_internal,
2413 "map not detected as single valued", return -1);
2414 if (!sv_tests[i].sv && sv)
2415 isl_die(ctx, isl_error_internal,
2416 "map detected as single valued", return -1);
2419 return 0;
2422 struct {
2423 const char *str;
2424 int bijective;
2425 } bijective_tests[] = {
2426 { "[N,M]->{[i,j] -> [i]}", 0 },
2427 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2428 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2429 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2430 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2431 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2432 { "[N,M]->{[i,j] -> []}", 0 },
2433 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2434 { "[N,M]->{[i,j] -> [2i]}", 0 },
2435 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2436 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2437 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2438 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2441 static int test_bijective(struct isl_ctx *ctx)
2443 isl_map *map;
2444 int i;
2445 int bijective;
2447 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2448 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2449 bijective = isl_map_is_bijective(map);
2450 isl_map_free(map);
2451 if (bijective < 0)
2452 return -1;
2453 if (bijective_tests[i].bijective && !bijective)
2454 isl_die(ctx, isl_error_internal,
2455 "map not detected as bijective", return -1);
2456 if (!bijective_tests[i].bijective && bijective)
2457 isl_die(ctx, isl_error_internal,
2458 "map detected as bijective", return -1);
2461 return 0;
2464 /* Inputs for isl_pw_qpolynomial_gist tests.
2465 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2467 struct {
2468 const char *pwqp;
2469 const char *set;
2470 const char *gist;
2471 } pwqp_gist_tests[] = {
2472 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2473 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2474 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2475 "{ [i] -> -1/2 + 1/2 * i }" },
2476 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2479 static int test_pwqp(struct isl_ctx *ctx)
2481 int i;
2482 const char *str;
2483 isl_set *set;
2484 isl_pw_qpolynomial *pwqp1, *pwqp2;
2485 int equal;
2487 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2488 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2490 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2491 isl_dim_in, 1, 1);
2493 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2494 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2496 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2498 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2500 isl_pw_qpolynomial_free(pwqp1);
2502 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2503 str = pwqp_gist_tests[i].pwqp;
2504 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2505 str = pwqp_gist_tests[i].set;
2506 set = isl_set_read_from_str(ctx, str);
2507 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2508 str = pwqp_gist_tests[i].gist;
2509 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2510 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2511 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2512 isl_pw_qpolynomial_free(pwqp1);
2514 if (equal < 0)
2515 return -1;
2516 if (!equal)
2517 isl_die(ctx, isl_error_unknown,
2518 "unexpected result", return -1);
2521 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2522 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2523 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2524 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2526 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2528 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2530 isl_pw_qpolynomial_free(pwqp1);
2532 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2533 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2534 str = "{ [x] -> x }";
2535 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2537 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2539 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2541 isl_pw_qpolynomial_free(pwqp1);
2543 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2544 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2545 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2546 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2547 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2548 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2549 isl_pw_qpolynomial_free(pwqp1);
2551 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2552 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2553 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2554 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2555 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2556 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2557 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2558 isl_pw_qpolynomial_free(pwqp1);
2559 isl_pw_qpolynomial_free(pwqp2);
2560 if (equal < 0)
2561 return -1;
2562 if (!equal)
2563 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2565 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2566 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2567 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2568 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2569 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2570 isl_val_one(ctx));
2571 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2572 isl_pw_qpolynomial_free(pwqp1);
2573 isl_pw_qpolynomial_free(pwqp2);
2574 if (equal < 0)
2575 return -1;
2576 if (!equal)
2577 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2579 return 0;
2582 static int test_split_periods(isl_ctx *ctx)
2584 const char *str;
2585 isl_pw_qpolynomial *pwqp;
2587 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2588 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2589 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2590 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2592 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2594 isl_pw_qpolynomial_free(pwqp);
2596 if (!pwqp)
2597 return -1;
2599 return 0;
2602 static int test_union(isl_ctx *ctx)
2604 const char *str;
2605 isl_union_set *uset1, *uset2;
2606 isl_union_map *umap1, *umap2;
2607 int equal;
2609 str = "{ [i] : 0 <= i <= 1 }";
2610 uset1 = isl_union_set_read_from_str(ctx, str);
2611 str = "{ [1] -> [0] }";
2612 umap1 = isl_union_map_read_from_str(ctx, str);
2614 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2615 equal = isl_union_map_is_equal(umap1, umap2);
2617 isl_union_map_free(umap1);
2618 isl_union_map_free(umap2);
2620 if (equal < 0)
2621 return -1;
2622 if (!equal)
2623 isl_die(ctx, isl_error_unknown, "union maps not equal",
2624 return -1);
2626 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2627 umap1 = isl_union_map_read_from_str(ctx, str);
2628 str = "{ A[i]; B[i] }";
2629 uset1 = isl_union_set_read_from_str(ctx, str);
2631 uset2 = isl_union_map_domain(umap1);
2633 equal = isl_union_set_is_equal(uset1, uset2);
2635 isl_union_set_free(uset1);
2636 isl_union_set_free(uset2);
2638 if (equal < 0)
2639 return -1;
2640 if (!equal)
2641 isl_die(ctx, isl_error_unknown, "union sets not equal",
2642 return -1);
2644 return 0;
2647 /* Check that computing a bound of a non-zero polynomial over an unbounded
2648 * domain does not produce a rational value.
2649 * In particular, check that the upper bound is infinity.
2651 static int test_bound_unbounded_domain(isl_ctx *ctx)
2653 const char *str;
2654 isl_pw_qpolynomial *pwqp;
2655 isl_pw_qpolynomial_fold *pwf, *pwf2;
2656 isl_bool equal;
2658 str = "{ [m,n] -> -m * n }";
2659 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2660 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2661 str = "{ infty }";
2662 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2663 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2664 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2665 isl_pw_qpolynomial_fold_free(pwf);
2666 isl_pw_qpolynomial_fold_free(pwf2);
2668 if (equal < 0)
2669 return -1;
2670 if (!equal)
2671 isl_die(ctx, isl_error_unknown,
2672 "expecting infinite polynomial bound", return -1);
2674 return 0;
2677 static int test_bound(isl_ctx *ctx)
2679 const char *str;
2680 unsigned dim;
2681 isl_pw_qpolynomial *pwqp;
2682 isl_pw_qpolynomial_fold *pwf;
2684 if (test_bound_unbounded_domain(ctx) < 0)
2685 return -1;
2687 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2688 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2689 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2690 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2691 isl_pw_qpolynomial_fold_free(pwf);
2692 if (dim != 4)
2693 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2694 return -1);
2696 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2697 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2698 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2699 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2700 isl_pw_qpolynomial_fold_free(pwf);
2701 if (dim != 1)
2702 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2703 return -1);
2705 return 0;
2708 static int test_lift(isl_ctx *ctx)
2710 const char *str;
2711 isl_basic_map *bmap;
2712 isl_basic_set *bset;
2714 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2715 bset = isl_basic_set_read_from_str(ctx, str);
2716 bset = isl_basic_set_lift(bset);
2717 bmap = isl_basic_map_from_range(bset);
2718 bset = isl_basic_map_domain(bmap);
2719 isl_basic_set_free(bset);
2721 return 0;
2724 struct {
2725 const char *set1;
2726 const char *set2;
2727 int subset;
2728 } subset_tests[] = {
2729 { "{ [112, 0] }",
2730 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2731 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2732 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2733 { "{ [65] }",
2734 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2735 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2736 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2737 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2738 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2739 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2740 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2741 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2742 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2743 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2744 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2745 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2746 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2747 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2748 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2749 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2750 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2751 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2752 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2753 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2754 "4e0 <= -57 + i0 + i1)) or "
2755 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2756 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2757 "4e0 >= -61 + i0 + i1)) or "
2758 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2761 static int test_subset(isl_ctx *ctx)
2763 int i;
2764 isl_set *set1, *set2;
2765 int subset;
2767 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2768 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2769 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2770 subset = isl_set_is_subset(set1, set2);
2771 isl_set_free(set1);
2772 isl_set_free(set2);
2773 if (subset < 0)
2774 return -1;
2775 if (subset != subset_tests[i].subset)
2776 isl_die(ctx, isl_error_unknown,
2777 "incorrect subset result", return -1);
2780 return 0;
2783 struct {
2784 const char *minuend;
2785 const char *subtrahend;
2786 const char *difference;
2787 } subtract_domain_tests[] = {
2788 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2789 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2790 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2793 static int test_subtract(isl_ctx *ctx)
2795 int i;
2796 isl_union_map *umap1, *umap2;
2797 isl_union_pw_multi_aff *upma1, *upma2;
2798 isl_union_set *uset;
2799 int equal;
2801 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2802 umap1 = isl_union_map_read_from_str(ctx,
2803 subtract_domain_tests[i].minuend);
2804 uset = isl_union_set_read_from_str(ctx,
2805 subtract_domain_tests[i].subtrahend);
2806 umap2 = isl_union_map_read_from_str(ctx,
2807 subtract_domain_tests[i].difference);
2808 umap1 = isl_union_map_subtract_domain(umap1, uset);
2809 equal = isl_union_map_is_equal(umap1, umap2);
2810 isl_union_map_free(umap1);
2811 isl_union_map_free(umap2);
2812 if (equal < 0)
2813 return -1;
2814 if (!equal)
2815 isl_die(ctx, isl_error_unknown,
2816 "incorrect subtract domain result", return -1);
2819 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2820 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2821 subtract_domain_tests[i].minuend);
2822 uset = isl_union_set_read_from_str(ctx,
2823 subtract_domain_tests[i].subtrahend);
2824 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2825 subtract_domain_tests[i].difference);
2826 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2827 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2828 isl_union_pw_multi_aff_free(upma1);
2829 isl_union_pw_multi_aff_free(upma2);
2830 if (equal < 0)
2831 return -1;
2832 if (!equal)
2833 isl_die(ctx, isl_error_unknown,
2834 "incorrect subtract domain result", return -1);
2837 return 0;
2840 int test_factorize(isl_ctx *ctx)
2842 const char *str;
2843 isl_basic_set *bset;
2844 isl_factorizer *f;
2846 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2847 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2848 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2849 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2850 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2851 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2852 "3i5 >= -2i0 - i2 + 3i4 }";
2853 bset = isl_basic_set_read_from_str(ctx, str);
2854 f = isl_basic_set_factorizer(bset);
2855 isl_basic_set_free(bset);
2856 isl_factorizer_free(f);
2857 if (!f)
2858 isl_die(ctx, isl_error_unknown,
2859 "failed to construct factorizer", return -1);
2861 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2862 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2863 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2864 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2865 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2866 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2867 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2868 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2869 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2870 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2871 bset = isl_basic_set_read_from_str(ctx, str);
2872 f = isl_basic_set_factorizer(bset);
2873 isl_basic_set_free(bset);
2874 isl_factorizer_free(f);
2875 if (!f)
2876 isl_die(ctx, isl_error_unknown,
2877 "failed to construct factorizer", return -1);
2879 return 0;
2882 static isl_stat check_injective(__isl_take isl_map *map, void *user)
2884 int *injective = user;
2886 *injective = isl_map_is_injective(map);
2887 isl_map_free(map);
2889 if (*injective < 0 || !*injective)
2890 return isl_stat_error;
2892 return isl_stat_ok;
2895 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2896 const char *r, const char *s, int tilable, int parallel)
2898 int i;
2899 isl_union_set *D;
2900 isl_union_map *W, *R, *S;
2901 isl_union_map *empty;
2902 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2903 isl_union_map *validity, *proximity, *coincidence;
2904 isl_union_map *schedule;
2905 isl_union_map *test;
2906 isl_union_set *delta;
2907 isl_union_set *domain;
2908 isl_set *delta_set;
2909 isl_set *slice;
2910 isl_set *origin;
2911 isl_schedule_constraints *sc;
2912 isl_schedule *sched;
2913 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2915 D = isl_union_set_read_from_str(ctx, d);
2916 W = isl_union_map_read_from_str(ctx, w);
2917 R = isl_union_map_read_from_str(ctx, r);
2918 S = isl_union_map_read_from_str(ctx, s);
2920 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2921 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2923 empty = isl_union_map_empty(isl_union_map_get_space(S));
2924 isl_union_map_compute_flow(isl_union_map_copy(R),
2925 isl_union_map_copy(W), empty,
2926 isl_union_map_copy(S),
2927 &dep_raw, NULL, NULL, NULL);
2928 isl_union_map_compute_flow(isl_union_map_copy(W),
2929 isl_union_map_copy(W),
2930 isl_union_map_copy(R),
2931 isl_union_map_copy(S),
2932 &dep_waw, &dep_war, NULL, NULL);
2934 dep = isl_union_map_union(dep_waw, dep_war);
2935 dep = isl_union_map_union(dep, dep_raw);
2936 validity = isl_union_map_copy(dep);
2937 coincidence = isl_union_map_copy(dep);
2938 proximity = isl_union_map_copy(dep);
2940 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2941 sc = isl_schedule_constraints_set_validity(sc, validity);
2942 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2943 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2944 sched = isl_schedule_constraints_compute_schedule(sc);
2945 schedule = isl_schedule_get_map(sched);
2946 isl_schedule_free(sched);
2947 isl_union_map_free(W);
2948 isl_union_map_free(R);
2949 isl_union_map_free(S);
2951 is_injection = 1;
2952 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2954 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2955 is_complete = isl_union_set_is_subset(D, domain);
2956 isl_union_set_free(D);
2957 isl_union_set_free(domain);
2959 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2960 test = isl_union_map_apply_range(test, dep);
2961 test = isl_union_map_apply_range(test, schedule);
2963 delta = isl_union_map_deltas(test);
2964 if (isl_union_set_n_set(delta) == 0) {
2965 is_tilable = 1;
2966 is_parallel = 1;
2967 is_nonneg = 1;
2968 isl_union_set_free(delta);
2969 } else {
2970 delta_set = isl_set_from_union_set(delta);
2972 slice = isl_set_universe(isl_set_get_space(delta_set));
2973 for (i = 0; i < tilable; ++i)
2974 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2975 is_tilable = isl_set_is_subset(delta_set, slice);
2976 isl_set_free(slice);
2978 slice = isl_set_universe(isl_set_get_space(delta_set));
2979 for (i = 0; i < parallel; ++i)
2980 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2981 is_parallel = isl_set_is_subset(delta_set, slice);
2982 isl_set_free(slice);
2984 origin = isl_set_universe(isl_set_get_space(delta_set));
2985 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2986 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2988 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2989 delta_set = isl_set_lexmin(delta_set);
2991 is_nonneg = isl_set_is_equal(delta_set, origin);
2993 isl_set_free(origin);
2994 isl_set_free(delta_set);
2997 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2998 is_injection < 0 || is_complete < 0)
2999 return -1;
3000 if (!is_complete)
3001 isl_die(ctx, isl_error_unknown,
3002 "generated schedule incomplete", return -1);
3003 if (!is_injection)
3004 isl_die(ctx, isl_error_unknown,
3005 "generated schedule not injective on each statement",
3006 return -1);
3007 if (!is_nonneg)
3008 isl_die(ctx, isl_error_unknown,
3009 "negative dependences in generated schedule",
3010 return -1);
3011 if (!is_tilable)
3012 isl_die(ctx, isl_error_unknown,
3013 "generated schedule not as tilable as expected",
3014 return -1);
3015 if (!is_parallel)
3016 isl_die(ctx, isl_error_unknown,
3017 "generated schedule not as parallel as expected",
3018 return -1);
3020 return 0;
3023 /* Compute a schedule for the given instance set, validity constraints,
3024 * proximity constraints and context and return a corresponding union map
3025 * representation.
3027 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3028 const char *domain, const char *validity, const char *proximity,
3029 const char *context)
3031 isl_set *con;
3032 isl_union_set *dom;
3033 isl_union_map *dep;
3034 isl_union_map *prox;
3035 isl_schedule_constraints *sc;
3036 isl_schedule *schedule;
3037 isl_union_map *sched;
3039 con = isl_set_read_from_str(ctx, context);
3040 dom = isl_union_set_read_from_str(ctx, domain);
3041 dep = isl_union_map_read_from_str(ctx, validity);
3042 prox = isl_union_map_read_from_str(ctx, proximity);
3043 sc = isl_schedule_constraints_on_domain(dom);
3044 sc = isl_schedule_constraints_set_context(sc, con);
3045 sc = isl_schedule_constraints_set_validity(sc, dep);
3046 sc = isl_schedule_constraints_set_proximity(sc, prox);
3047 schedule = isl_schedule_constraints_compute_schedule(sc);
3048 sched = isl_schedule_get_map(schedule);
3049 isl_schedule_free(schedule);
3051 return sched;
3054 /* Compute a schedule for the given instance set, validity constraints and
3055 * proximity constraints and return a corresponding union map representation.
3057 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3058 const char *domain, const char *validity, const char *proximity)
3060 return compute_schedule_with_context(ctx, domain, validity, proximity,
3061 "{ : }");
3064 /* Check that a schedule can be constructed on the given domain
3065 * with the given validity and proximity constraints.
3067 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3068 const char *validity, const char *proximity)
3070 isl_union_map *sched;
3072 sched = compute_schedule(ctx, domain, validity, proximity);
3073 if (!sched)
3074 return -1;
3076 isl_union_map_free(sched);
3077 return 0;
3080 int test_special_schedule(isl_ctx *ctx, const char *domain,
3081 const char *validity, const char *proximity, const char *expected_sched)
3083 isl_union_map *sched1, *sched2;
3084 int equal;
3086 sched1 = compute_schedule(ctx, domain, validity, proximity);
3087 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3089 equal = isl_union_map_is_equal(sched1, sched2);
3090 isl_union_map_free(sched1);
3091 isl_union_map_free(sched2);
3093 if (equal < 0)
3094 return -1;
3095 if (!equal)
3096 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3097 return -1);
3099 return 0;
3102 /* Check that the schedule map is properly padded, even after being
3103 * reconstructed from the band forest.
3105 static int test_padded_schedule(isl_ctx *ctx)
3107 const char *str;
3108 isl_union_set *D;
3109 isl_union_map *validity, *proximity;
3110 isl_schedule_constraints *sc;
3111 isl_schedule *sched;
3112 isl_union_map *map1, *map2;
3113 isl_band_list *list;
3114 int equal;
3116 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3117 D = isl_union_set_read_from_str(ctx, str);
3118 validity = isl_union_map_empty(isl_union_set_get_space(D));
3119 proximity = isl_union_map_copy(validity);
3120 sc = isl_schedule_constraints_on_domain(D);
3121 sc = isl_schedule_constraints_set_validity(sc, validity);
3122 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3123 sched = isl_schedule_constraints_compute_schedule(sc);
3124 map1 = isl_schedule_get_map(sched);
3125 list = isl_schedule_get_band_forest(sched);
3126 isl_band_list_free(list);
3127 map2 = isl_schedule_get_map(sched);
3128 isl_schedule_free(sched);
3129 equal = isl_union_map_is_equal(map1, map2);
3130 isl_union_map_free(map1);
3131 isl_union_map_free(map2);
3133 if (equal < 0)
3134 return -1;
3135 if (!equal)
3136 isl_die(ctx, isl_error_unknown,
3137 "reconstructed schedule map not the same as original",
3138 return -1);
3140 return 0;
3143 /* Check that conditional validity constraints are also taken into
3144 * account across bands.
3145 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3146 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3147 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3148 * is enforced by the rest of the schedule.
3150 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3152 const char *str;
3153 isl_union_set *domain;
3154 isl_union_map *validity, *proximity, *condition;
3155 isl_union_map *sink, *source, *dep;
3156 isl_schedule_constraints *sc;
3157 isl_schedule *schedule;
3158 isl_union_access_info *access;
3159 isl_union_flow *flow;
3160 int empty;
3162 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3163 "A[k] : k >= 1 and k <= -1 + n; "
3164 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3165 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3166 domain = isl_union_set_read_from_str(ctx, str);
3167 sc = isl_schedule_constraints_on_domain(domain);
3168 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3169 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3170 "D[k, i] -> C[1 + k, i] : "
3171 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3172 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3173 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3174 validity = isl_union_map_read_from_str(ctx, str);
3175 sc = isl_schedule_constraints_set_validity(sc, validity);
3176 str = "[n] -> { C[k, i] -> D[k, i] : "
3177 "0 <= i <= -1 + k and k <= -1 + n }";
3178 proximity = isl_union_map_read_from_str(ctx, str);
3179 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3180 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3181 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3182 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3183 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3184 condition = isl_union_map_read_from_str(ctx, str);
3185 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3186 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3187 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3188 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3189 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3190 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3191 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3192 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3193 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3194 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3195 validity = isl_union_map_read_from_str(ctx, str);
3196 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3197 validity);
3198 schedule = isl_schedule_constraints_compute_schedule(sc);
3199 str = "{ D[2,0] -> [] }";
3200 sink = isl_union_map_read_from_str(ctx, str);
3201 access = isl_union_access_info_from_sink(sink);
3202 str = "{ C[2,1] -> [] }";
3203 source = isl_union_map_read_from_str(ctx, str);
3204 access = isl_union_access_info_set_must_source(access, source);
3205 access = isl_union_access_info_set_schedule(access, schedule);
3206 flow = isl_union_access_info_compute_flow(access);
3207 dep = isl_union_flow_get_must_dependence(flow);
3208 isl_union_flow_free(flow);
3209 empty = isl_union_map_is_empty(dep);
3210 isl_union_map_free(dep);
3212 if (empty < 0)
3213 return -1;
3214 if (empty)
3215 isl_die(ctx, isl_error_unknown,
3216 "conditional validity not respected", return -1);
3218 return 0;
3221 /* Input for testing of schedule construction based on
3222 * conditional constraints.
3224 * domain is the iteration domain
3225 * flow are the flow dependences, which determine the validity and
3226 * proximity constraints
3227 * condition are the conditions on the conditional validity constraints
3228 * conditional_validity are the conditional validity constraints
3229 * outer_band_n is the expected number of members in the outer band
3231 struct {
3232 const char *domain;
3233 const char *flow;
3234 const char *condition;
3235 const char *conditional_validity;
3236 int outer_band_n;
3237 } live_range_tests[] = {
3238 /* Contrived example that illustrates that we need to keep
3239 * track of tagged condition dependences and
3240 * tagged conditional validity dependences
3241 * in isl_sched_edge separately.
3242 * In particular, the conditional validity constraints on A
3243 * cannot be satisfied,
3244 * but they can be ignored because there are no corresponding
3245 * condition constraints. However, we do have an additional
3246 * conditional validity constraint that maps to the same
3247 * dependence relation
3248 * as the condition constraint on B. If we did not make a distinction
3249 * between tagged condition and tagged conditional validity
3250 * dependences, then we
3251 * could end up treating this shared dependence as an condition
3252 * constraint on A, forcing a localization of the conditions,
3253 * which is impossible.
3255 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3256 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3257 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3258 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3259 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3260 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3263 /* TACO 2013 Fig. 7 */
3264 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3265 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3266 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3267 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3268 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3269 "0 <= i < n and 0 <= j < n - 1 }",
3270 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3271 "0 <= i < n and 0 <= j < j' < n;"
3272 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3273 "0 <= i < i' < n and 0 <= j,j' < n;"
3274 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3275 "0 <= i,j,j' < n and j < j' }",
3278 /* TACO 2013 Fig. 7, without tags */
3279 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3280 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3281 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3282 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3283 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3284 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3285 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3286 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3289 /* TACO 2013 Fig. 12 */
3290 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3291 "S3[i,3] : 0 <= i <= 1 }",
3292 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3293 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3294 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3295 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3296 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3297 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3298 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3299 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3300 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3301 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3302 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3307 /* Test schedule construction based on conditional constraints.
3308 * In particular, check the number of members in the outer band node
3309 * as an indication of whether tiling is possible or not.
3311 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3313 int i;
3314 isl_union_set *domain;
3315 isl_union_map *condition;
3316 isl_union_map *flow;
3317 isl_union_map *validity;
3318 isl_schedule_constraints *sc;
3319 isl_schedule *schedule;
3320 isl_schedule_node *node;
3321 int n_member;
3323 if (test_special_conditional_schedule_constraints(ctx) < 0)
3324 return -1;
3326 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3327 domain = isl_union_set_read_from_str(ctx,
3328 live_range_tests[i].domain);
3329 flow = isl_union_map_read_from_str(ctx,
3330 live_range_tests[i].flow);
3331 condition = isl_union_map_read_from_str(ctx,
3332 live_range_tests[i].condition);
3333 validity = isl_union_map_read_from_str(ctx,
3334 live_range_tests[i].conditional_validity);
3335 sc = isl_schedule_constraints_on_domain(domain);
3336 sc = isl_schedule_constraints_set_validity(sc,
3337 isl_union_map_copy(flow));
3338 sc = isl_schedule_constraints_set_proximity(sc, flow);
3339 sc = isl_schedule_constraints_set_conditional_validity(sc,
3340 condition, validity);
3341 schedule = isl_schedule_constraints_compute_schedule(sc);
3342 node = isl_schedule_get_root(schedule);
3343 while (node &&
3344 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3345 node = isl_schedule_node_first_child(node);
3346 n_member = isl_schedule_node_band_n_member(node);
3347 isl_schedule_node_free(node);
3348 isl_schedule_free(schedule);
3350 if (!schedule)
3351 return -1;
3352 if (n_member != live_range_tests[i].outer_band_n)
3353 isl_die(ctx, isl_error_unknown,
3354 "unexpected number of members in outer band",
3355 return -1);
3357 return 0;
3360 /* Check that the schedule computed for the given instance set and
3361 * dependence relation strongly satisfies the dependences.
3362 * In particular, check that no instance is scheduled before
3363 * or together with an instance on which it depends.
3364 * Earlier versions of isl would produce a schedule that
3365 * only weakly satisfies the dependences.
3367 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3369 const char *domain, *dep;
3370 isl_union_map *D, *schedule;
3371 isl_map *map, *ge;
3372 int empty;
3374 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3375 "A[i0] : 0 <= i0 <= 1 }";
3376 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3377 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3378 schedule = compute_schedule(ctx, domain, dep, dep);
3379 D = isl_union_map_read_from_str(ctx, dep);
3380 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3381 D = isl_union_map_apply_range(D, schedule);
3382 map = isl_map_from_union_map(D);
3383 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3384 map = isl_map_intersect(map, ge);
3385 empty = isl_map_is_empty(map);
3386 isl_map_free(map);
3388 if (empty < 0)
3389 return -1;
3390 if (!empty)
3391 isl_die(ctx, isl_error_unknown,
3392 "dependences not strongly satisfied", return -1);
3394 return 0;
3397 /* Compute a schedule for input where the instance set constraints
3398 * conflict with the context constraints.
3399 * Earlier versions of isl did not properly handle this situation.
3401 static int test_conflicting_context_schedule(isl_ctx *ctx)
3403 isl_union_map *schedule;
3404 const char *domain, *context;
3406 domain = "[n] -> { A[] : n >= 0 }";
3407 context = "[n] -> { : n < 0 }";
3408 schedule = compute_schedule_with_context(ctx,
3409 domain, "{}", "{}", context);
3410 isl_union_map_free(schedule);
3412 if (!schedule)
3413 return -1;
3415 return 0;
3418 /* Check that the dependence carrying step is not confused by
3419 * a bound on the coefficient size.
3420 * In particular, force the scheduler to move to a dependence carrying
3421 * step by demanding outer coincidence and bound the size of
3422 * the coefficients. Earlier versions of isl would take this
3423 * bound into account while carrying dependences, breaking
3424 * fundamental assumptions.
3426 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3428 const char *domain, *dep;
3429 isl_union_set *I;
3430 isl_union_map *D;
3431 isl_schedule_constraints *sc;
3432 isl_schedule *schedule;
3434 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3435 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3436 "i1 <= -2 + i0; "
3437 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3438 I = isl_union_set_read_from_str(ctx, domain);
3439 D = isl_union_map_read_from_str(ctx, dep);
3440 sc = isl_schedule_constraints_on_domain(I);
3441 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3442 sc = isl_schedule_constraints_set_coincidence(sc, D);
3443 isl_options_set_schedule_outer_coincidence(ctx, 1);
3444 isl_options_set_schedule_max_coefficient(ctx, 20);
3445 schedule = isl_schedule_constraints_compute_schedule(sc);
3446 isl_options_set_schedule_max_coefficient(ctx, -1);
3447 isl_options_set_schedule_outer_coincidence(ctx, 0);
3448 isl_schedule_free(schedule);
3450 if (!schedule)
3451 return -1;
3453 return 0;
3456 int test_schedule(isl_ctx *ctx)
3458 const char *D, *W, *R, *V, *P, *S;
3460 /* Handle resulting schedule with zero bands. */
3461 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3462 return -1;
3464 /* Jacobi */
3465 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3466 W = "{ S1[t,i] -> a[t,i] }";
3467 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3468 "S1[t,i] -> a[t-1,i+1] }";
3469 S = "{ S1[t,i] -> [t,i] }";
3470 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3471 return -1;
3473 /* Fig. 5 of CC2008 */
3474 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3475 "j <= -1 + N }";
3476 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3477 "j >= 2 and j <= -1 + N }";
3478 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3479 "j >= 2 and j <= -1 + N; "
3480 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3481 "j >= 2 and j <= -1 + N }";
3482 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3483 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3484 return -1;
3486 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3487 W = "{ S1[i] -> a[i] }";
3488 R = "{ S2[i] -> a[i+1] }";
3489 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3490 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3491 return -1;
3493 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3494 W = "{ S1[i] -> a[i] }";
3495 R = "{ S2[i] -> a[9-i] }";
3496 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3497 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3498 return -1;
3500 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3501 W = "{ S1[i] -> a[i] }";
3502 R = "[N] -> { S2[i] -> a[N-1-i] }";
3503 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3504 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3505 return -1;
3507 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3508 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3509 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3510 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3511 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3512 return -1;
3514 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3515 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3516 R = "{ S2[i,j] -> a[i-1,j] }";
3517 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3518 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3519 return -1;
3521 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3522 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3523 R = "{ S2[i,j] -> a[i,j-1] }";
3524 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3525 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3526 return -1;
3528 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3529 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3530 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3531 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3532 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3533 "S_0[] -> [0, 0, 0] }";
3534 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3535 return -1;
3536 ctx->opt->schedule_parametric = 0;
3537 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3538 return -1;
3539 ctx->opt->schedule_parametric = 1;
3541 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3542 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3543 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3544 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3545 "S4[i] -> a[i,N] }";
3546 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3547 "S4[i] -> [4,i,0] }";
3548 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3549 return -1;
3551 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3552 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3553 "j <= N }";
3554 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3555 "j <= N; "
3556 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3557 "j <= N }";
3558 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3559 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3560 return -1;
3562 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3563 " S_2[t] : t >= 0 and t <= -1 + N; "
3564 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3565 "i <= -1 + N }";
3566 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3567 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3568 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3569 "i >= 0 and i <= -1 + N }";
3570 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3571 "i >= 0 and i <= -1 + N; "
3572 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3573 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3574 " S_0[t] -> [0, t, 0] }";
3576 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3577 return -1;
3578 ctx->opt->schedule_parametric = 0;
3579 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3580 return -1;
3581 ctx->opt->schedule_parametric = 1;
3583 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3584 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3585 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3586 return -1;
3588 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3589 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3590 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3591 "j >= 0 and j <= -1 + N; "
3592 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3593 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3594 "j >= 0 and j <= -1 + N; "
3595 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3596 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3597 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3598 return -1;
3600 D = "{ S_0[i] : i >= 0 }";
3601 W = "{ S_0[i] -> a[i] : i >= 0 }";
3602 R = "{ S_0[i] -> a[0] : i >= 0 }";
3603 S = "{ S_0[i] -> [0, i, 0] }";
3604 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3605 return -1;
3607 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3608 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3609 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3610 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3611 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3612 return -1;
3614 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3615 "k <= -1 + n and k >= 0 }";
3616 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3617 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3618 "k <= -1 + n and k >= 0; "
3619 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3620 "k <= -1 + n and k >= 0; "
3621 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3622 "k <= -1 + n and k >= 0 }";
3623 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3624 ctx->opt->schedule_outer_coincidence = 1;
3625 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3626 return -1;
3627 ctx->opt->schedule_outer_coincidence = 0;
3629 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3630 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3631 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3632 "Stmt_for_body24[i0, i1, 1, 0]:"
3633 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3634 "Stmt_for_body7[i0, i1, i2]:"
3635 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3636 "i2 <= 7 }";
3638 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3639 "Stmt_for_body24[1, i1, i2, i3]:"
3640 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3641 "i2 >= 1;"
3642 "Stmt_for_body24[0, i1, i2, i3] -> "
3643 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3644 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3645 "i3 >= 0;"
3646 "Stmt_for_body24[0, i1, i2, i3] ->"
3647 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3648 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3649 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3650 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3651 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3652 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3653 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3654 "i1 <= 6 and i1 >= 0;"
3655 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3656 "Stmt_for_body7[i0, i1, i2] -> "
3657 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3658 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3659 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3660 "Stmt_for_body7[i0, i1, i2] -> "
3661 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3662 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3663 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3664 P = V;
3665 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3666 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3667 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3669 if (test_special_schedule(ctx, D, V, P, S) < 0)
3670 return -1;
3672 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3673 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3674 "j >= 1 and j <= 7;"
3675 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3676 "j >= 1 and j <= 8 }";
3677 P = "{ }";
3678 S = "{ S_0[i, j] -> [i + j, j] }";
3679 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3680 if (test_special_schedule(ctx, D, V, P, S) < 0)
3681 return -1;
3682 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3684 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3685 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3686 "j >= 0 and j <= -1 + i }";
3687 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3688 "i <= -1 + N and j >= 0;"
3689 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3690 "i <= -2 + N }";
3691 P = "{ }";
3692 S = "{ S_0[i, j] -> [i, j] }";
3693 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3694 if (test_special_schedule(ctx, D, V, P, S) < 0)
3695 return -1;
3696 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3698 /* Test both algorithms on a case with only proximity dependences. */
3699 D = "{ S[i,j] : 0 <= i <= 10 }";
3700 V = "{ }";
3701 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3702 S = "{ S[i, j] -> [j, i] }";
3703 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3704 if (test_special_schedule(ctx, D, V, P, S) < 0)
3705 return -1;
3706 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3707 if (test_special_schedule(ctx, D, V, P, S) < 0)
3708 return -1;
3710 D = "{ A[a]; B[] }";
3711 V = "{}";
3712 P = "{ A[a] -> B[] }";
3713 if (test_has_schedule(ctx, D, V, P) < 0)
3714 return -1;
3716 if (test_padded_schedule(ctx) < 0)
3717 return -1;
3719 /* Check that check for progress is not confused by rational
3720 * solution.
3722 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3723 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3724 "i0 <= -2 + N; "
3725 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3726 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3727 P = "{}";
3728 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3729 if (test_has_schedule(ctx, D, V, P) < 0)
3730 return -1;
3731 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3733 /* Check that we allow schedule rows that are only non-trivial
3734 * on some full-dimensional domains.
3736 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3737 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3738 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3739 P = "{}";
3740 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3741 if (test_has_schedule(ctx, D, V, P) < 0)
3742 return -1;
3743 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3745 if (test_conditional_schedule_constraints(ctx) < 0)
3746 return -1;
3748 if (test_strongly_satisfying_schedule(ctx) < 0)
3749 return -1;
3751 if (test_conflicting_context_schedule(ctx) < 0)
3752 return -1;
3754 if (test_bounded_coefficients_schedule(ctx) < 0)
3755 return -1;
3757 return 0;
3760 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3762 isl_union_map *umap;
3763 int test;
3765 umap = isl_union_map_read_from_str(ctx, str);
3766 test = isl_union_map_plain_is_injective(umap);
3767 isl_union_map_free(umap);
3768 if (test < 0)
3769 return -1;
3770 if (test == injective)
3771 return 0;
3772 if (injective)
3773 isl_die(ctx, isl_error_unknown,
3774 "map not detected as injective", return -1);
3775 else
3776 isl_die(ctx, isl_error_unknown,
3777 "map detected as injective", return -1);
3780 int test_injective(isl_ctx *ctx)
3782 const char *str;
3784 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3785 return -1;
3786 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3787 return -1;
3788 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3789 return -1;
3790 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3791 return -1;
3792 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3793 return -1;
3794 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3795 return -1;
3796 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3797 return -1;
3798 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3799 return -1;
3801 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3802 if (test_plain_injective(ctx, str, 1))
3803 return -1;
3804 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3805 if (test_plain_injective(ctx, str, 0))
3806 return -1;
3808 return 0;
3811 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3813 isl_aff *aff2;
3814 int equal;
3816 if (!aff)
3817 return -1;
3819 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3820 equal = isl_aff_plain_is_equal(aff, aff2);
3821 isl_aff_free(aff2);
3823 return equal;
3826 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3828 int equal;
3830 equal = aff_plain_is_equal(aff, str);
3831 if (equal < 0)
3832 return -1;
3833 if (!equal)
3834 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3835 "result not as expected", return -1);
3836 return 0;
3839 struct {
3840 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3841 __isl_take isl_aff *aff2);
3842 } aff_bin_op[] = {
3843 ['+'] = { &isl_aff_add },
3844 ['-'] = { &isl_aff_sub },
3845 ['*'] = { &isl_aff_mul },
3846 ['/'] = { &isl_aff_div },
3849 struct {
3850 const char *arg1;
3851 unsigned char op;
3852 const char *arg2;
3853 const char *res;
3854 } aff_bin_tests[] = {
3855 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3856 "{ [i] -> [2i] }" },
3857 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3858 "{ [i] -> [0] }" },
3859 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3860 "{ [i] -> [2i] }" },
3861 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3862 "{ [i] -> [2i] }" },
3863 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3864 "{ [i] -> [i/2] }" },
3865 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3866 "{ [i] -> [i] }" },
3867 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3868 "{ [i] -> [NaN] }" },
3869 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3870 "{ [i] -> [NaN] }" },
3871 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3872 "{ [i] -> [NaN] }" },
3873 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3874 "{ [i] -> [NaN] }" },
3875 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3876 "{ [i] -> [NaN] }" },
3877 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3878 "{ [i] -> [NaN] }" },
3879 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3880 "{ [i] -> [NaN] }" },
3881 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3882 "{ [i] -> [NaN] }" },
3883 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3884 "{ [i] -> [NaN] }" },
3885 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3886 "{ [i] -> [NaN] }" },
3887 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3888 "{ [i] -> [NaN] }" },
3889 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3890 "{ [i] -> [NaN] }" },
3893 /* Perform some basic tests of binary operations on isl_aff objects.
3895 static int test_bin_aff(isl_ctx *ctx)
3897 int i;
3898 isl_aff *aff1, *aff2, *res;
3899 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3900 __isl_take isl_aff *aff2);
3901 int ok;
3903 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3904 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3905 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3906 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3907 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3908 aff1 = fn(aff1, aff2);
3909 if (isl_aff_is_nan(res))
3910 ok = isl_aff_is_nan(aff1);
3911 else
3912 ok = isl_aff_plain_is_equal(aff1, res);
3913 isl_aff_free(aff1);
3914 isl_aff_free(res);
3915 if (ok < 0)
3916 return -1;
3917 if (!ok)
3918 isl_die(ctx, isl_error_unknown,
3919 "unexpected result", return -1);
3922 return 0;
3925 struct {
3926 __isl_give isl_union_pw_multi_aff *(*fn)(
3927 __isl_take isl_union_pw_multi_aff *upma1,
3928 __isl_take isl_union_pw_multi_aff *upma2);
3929 const char *arg1;
3930 const char *arg2;
3931 const char *res;
3932 } upma_bin_tests[] = {
3933 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3934 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3935 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3936 "{ B[x] -> [2] : x >= 0 }",
3937 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3938 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
3939 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
3940 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
3941 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
3942 "D[i] -> B[2] : i >= 5 }" },
3943 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
3944 "{ B[x] -> C[2] : x > 0 }",
3945 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
3946 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
3947 "{ B[x] -> A[2] : x >= 0 }",
3948 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
3951 /* Perform some basic tests of binary operations on
3952 * isl_union_pw_multi_aff objects.
3954 static int test_bin_upma(isl_ctx *ctx)
3956 int i;
3957 isl_union_pw_multi_aff *upma1, *upma2, *res;
3958 int ok;
3960 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3961 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3962 upma_bin_tests[i].arg1);
3963 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3964 upma_bin_tests[i].arg2);
3965 res = isl_union_pw_multi_aff_read_from_str(ctx,
3966 upma_bin_tests[i].res);
3967 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3968 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3969 isl_union_pw_multi_aff_free(upma1);
3970 isl_union_pw_multi_aff_free(res);
3971 if (ok < 0)
3972 return -1;
3973 if (!ok)
3974 isl_die(ctx, isl_error_unknown,
3975 "unexpected result", return -1);
3978 return 0;
3981 struct {
3982 __isl_give isl_union_pw_multi_aff *(*fn)(
3983 __isl_take isl_union_pw_multi_aff *upma1,
3984 __isl_take isl_union_pw_multi_aff *upma2);
3985 const char *arg1;
3986 const char *arg2;
3987 } upma_bin_fail_tests[] = {
3988 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
3989 "{ B[x] -> C[2] : x >= 0 }" },
3992 /* Perform some basic tests of binary operations on
3993 * isl_union_pw_multi_aff objects that are expected to fail.
3995 static int test_bin_upma_fail(isl_ctx *ctx)
3997 int i, n;
3998 isl_union_pw_multi_aff *upma1, *upma2;
3999 int on_error;
4001 on_error = isl_options_get_on_error(ctx);
4002 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4003 n = ARRAY_SIZE(upma_bin_fail_tests);
4004 for (i = 0; i < n; ++i) {
4005 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4006 upma_bin_fail_tests[i].arg1);
4007 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4008 upma_bin_fail_tests[i].arg2);
4009 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4010 isl_union_pw_multi_aff_free(upma1);
4011 if (upma1)
4012 break;
4014 isl_options_set_on_error(ctx, on_error);
4015 if (i < n)
4016 isl_die(ctx, isl_error_unknown,
4017 "operation not expected to succeed", return -1);
4019 return 0;
4022 int test_aff(isl_ctx *ctx)
4024 const char *str;
4025 isl_set *set;
4026 isl_space *space;
4027 isl_local_space *ls;
4028 isl_aff *aff;
4029 int zero, equal;
4031 if (test_bin_aff(ctx) < 0)
4032 return -1;
4033 if (test_bin_upma(ctx) < 0)
4034 return -1;
4035 if (test_bin_upma_fail(ctx) < 0)
4036 return -1;
4038 space = isl_space_set_alloc(ctx, 0, 1);
4039 ls = isl_local_space_from_space(space);
4040 aff = isl_aff_zero_on_domain(ls);
4042 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4043 aff = isl_aff_scale_down_ui(aff, 3);
4044 aff = isl_aff_floor(aff);
4045 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4046 aff = isl_aff_scale_down_ui(aff, 2);
4047 aff = isl_aff_floor(aff);
4048 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4050 str = "{ [10] }";
4051 set = isl_set_read_from_str(ctx, str);
4052 aff = isl_aff_gist(aff, set);
4054 aff = isl_aff_add_constant_si(aff, -16);
4055 zero = isl_aff_plain_is_zero(aff);
4056 isl_aff_free(aff);
4058 if (zero < 0)
4059 return -1;
4060 if (!zero)
4061 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4063 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4064 aff = isl_aff_scale_down_ui(aff, 64);
4065 aff = isl_aff_floor(aff);
4066 equal = aff_check_plain_equal(aff, "{ [-1] }");
4067 isl_aff_free(aff);
4068 if (equal < 0)
4069 return -1;
4071 return 0;
4074 int test_dim_max(isl_ctx *ctx)
4076 int equal;
4077 const char *str;
4078 isl_set *set1, *set2;
4079 isl_set *set;
4080 isl_map *map;
4081 isl_pw_aff *pwaff;
4083 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4084 set = isl_set_read_from_str(ctx, str);
4085 pwaff = isl_set_dim_max(set, 0);
4086 set1 = isl_set_from_pw_aff(pwaff);
4087 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4088 set2 = isl_set_read_from_str(ctx, str);
4089 equal = isl_set_is_equal(set1, set2);
4090 isl_set_free(set1);
4091 isl_set_free(set2);
4092 if (equal < 0)
4093 return -1;
4094 if (!equal)
4095 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4097 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4098 set = isl_set_read_from_str(ctx, str);
4099 pwaff = isl_set_dim_max(set, 0);
4100 set1 = isl_set_from_pw_aff(pwaff);
4101 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4102 set2 = isl_set_read_from_str(ctx, str);
4103 equal = isl_set_is_equal(set1, set2);
4104 isl_set_free(set1);
4105 isl_set_free(set2);
4106 if (equal < 0)
4107 return -1;
4108 if (!equal)
4109 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4111 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4112 set = isl_set_read_from_str(ctx, str);
4113 pwaff = isl_set_dim_max(set, 0);
4114 set1 = isl_set_from_pw_aff(pwaff);
4115 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4116 set2 = isl_set_read_from_str(ctx, str);
4117 equal = isl_set_is_equal(set1, set2);
4118 isl_set_free(set1);
4119 isl_set_free(set2);
4120 if (equal < 0)
4121 return -1;
4122 if (!equal)
4123 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4125 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4126 "0 <= i < N and 0 <= j < M }";
4127 map = isl_map_read_from_str(ctx, str);
4128 set = isl_map_range(map);
4130 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4131 set1 = isl_set_from_pw_aff(pwaff);
4132 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4133 set2 = isl_set_read_from_str(ctx, str);
4134 equal = isl_set_is_equal(set1, set2);
4135 isl_set_free(set1);
4136 isl_set_free(set2);
4138 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4139 set1 = isl_set_from_pw_aff(pwaff);
4140 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4141 set2 = isl_set_read_from_str(ctx, str);
4142 if (equal >= 0 && equal)
4143 equal = isl_set_is_equal(set1, set2);
4144 isl_set_free(set1);
4145 isl_set_free(set2);
4147 isl_set_free(set);
4149 if (equal < 0)
4150 return -1;
4151 if (!equal)
4152 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4154 /* Check that solutions are properly merged. */
4155 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4156 "c <= -1 + n - 4a - 2b and c >= -2b and "
4157 "4a >= -4 + n and c >= 0 }";
4158 set = isl_set_read_from_str(ctx, str);
4159 pwaff = isl_set_dim_min(set, 2);
4160 set1 = isl_set_from_pw_aff(pwaff);
4161 str = "[n] -> { [(0)] : n >= 1 }";
4162 set2 = isl_set_read_from_str(ctx, str);
4163 equal = isl_set_is_equal(set1, set2);
4164 isl_set_free(set1);
4165 isl_set_free(set2);
4167 if (equal < 0)
4168 return -1;
4169 if (!equal)
4170 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4172 /* Check that empty solution lie in the right space. */
4173 str = "[n] -> { [t,a] : 1 = 0 }";
4174 set = isl_set_read_from_str(ctx, str);
4175 pwaff = isl_set_dim_max(set, 0);
4176 set1 = isl_set_from_pw_aff(pwaff);
4177 str = "[n] -> { [t] : 1 = 0 }";
4178 set2 = isl_set_read_from_str(ctx, str);
4179 equal = isl_set_is_equal(set1, set2);
4180 isl_set_free(set1);
4181 isl_set_free(set2);
4183 if (equal < 0)
4184 return -1;
4185 if (!equal)
4186 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4188 return 0;
4191 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4193 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4194 const char *str)
4196 isl_ctx *ctx;
4197 isl_pw_multi_aff *pma2;
4198 int equal;
4200 if (!pma)
4201 return -1;
4203 ctx = isl_pw_multi_aff_get_ctx(pma);
4204 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4205 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4206 isl_pw_multi_aff_free(pma2);
4208 return equal;
4211 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4212 * represented by "str".
4214 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4215 const char *str)
4217 int equal;
4219 equal = pw_multi_aff_plain_is_equal(pma, str);
4220 if (equal < 0)
4221 return -1;
4222 if (!equal)
4223 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4224 "result not as expected", return -1);
4225 return 0;
4228 /* Basic test for isl_pw_multi_aff_product.
4230 * Check that multiple pieces are properly handled.
4232 static int test_product_pma(isl_ctx *ctx)
4234 int equal;
4235 const char *str;
4236 isl_pw_multi_aff *pma1, *pma2;
4238 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4239 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4240 str = "{ C[] -> D[] }";
4241 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4242 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4243 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4244 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4245 equal = pw_multi_aff_check_plain_equal(pma1, str);
4246 isl_pw_multi_aff_free(pma1);
4247 if (equal < 0)
4248 return -1;
4250 return 0;
4253 int test_product(isl_ctx *ctx)
4255 const char *str;
4256 isl_set *set;
4257 isl_union_set *uset1, *uset2;
4258 int ok;
4260 str = "{ A[i] }";
4261 set = isl_set_read_from_str(ctx, str);
4262 set = isl_set_product(set, isl_set_copy(set));
4263 ok = isl_set_is_wrapping(set);
4264 isl_set_free(set);
4265 if (ok < 0)
4266 return -1;
4267 if (!ok)
4268 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4270 str = "{ [] }";
4271 uset1 = isl_union_set_read_from_str(ctx, str);
4272 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4273 str = "{ [[] -> []] }";
4274 uset2 = isl_union_set_read_from_str(ctx, str);
4275 ok = isl_union_set_is_equal(uset1, uset2);
4276 isl_union_set_free(uset1);
4277 isl_union_set_free(uset2);
4278 if (ok < 0)
4279 return -1;
4280 if (!ok)
4281 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4283 if (test_product_pma(ctx) < 0)
4284 return -1;
4286 return 0;
4289 /* Check that two sets are not considered disjoint just because
4290 * they have a different set of (named) parameters.
4292 static int test_disjoint(isl_ctx *ctx)
4294 const char *str;
4295 isl_set *set, *set2;
4296 int disjoint;
4298 str = "[n] -> { [[]->[]] }";
4299 set = isl_set_read_from_str(ctx, str);
4300 str = "{ [[]->[]] }";
4301 set2 = isl_set_read_from_str(ctx, str);
4302 disjoint = isl_set_is_disjoint(set, set2);
4303 isl_set_free(set);
4304 isl_set_free(set2);
4305 if (disjoint < 0)
4306 return -1;
4307 if (disjoint)
4308 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4310 return 0;
4313 int test_equal(isl_ctx *ctx)
4315 const char *str;
4316 isl_set *set, *set2;
4317 int equal;
4319 str = "{ S_6[i] }";
4320 set = isl_set_read_from_str(ctx, str);
4321 str = "{ S_7[i] }";
4322 set2 = isl_set_read_from_str(ctx, str);
4323 equal = isl_set_is_equal(set, set2);
4324 isl_set_free(set);
4325 isl_set_free(set2);
4326 if (equal < 0)
4327 return -1;
4328 if (equal)
4329 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4331 return 0;
4334 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4335 enum isl_dim_type type, unsigned pos, int fixed)
4337 int test;
4339 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4340 isl_map_free(map);
4341 if (test < 0)
4342 return -1;
4343 if (test == fixed)
4344 return 0;
4345 if (fixed)
4346 isl_die(ctx, isl_error_unknown,
4347 "map not detected as fixed", return -1);
4348 else
4349 isl_die(ctx, isl_error_unknown,
4350 "map detected as fixed", return -1);
4353 int test_fixed(isl_ctx *ctx)
4355 const char *str;
4356 isl_map *map;
4358 str = "{ [i] -> [i] }";
4359 map = isl_map_read_from_str(ctx, str);
4360 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4361 return -1;
4362 str = "{ [i] -> [1] }";
4363 map = isl_map_read_from_str(ctx, str);
4364 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4365 return -1;
4366 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4367 map = isl_map_read_from_str(ctx, str);
4368 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4369 return -1;
4370 map = isl_map_read_from_str(ctx, str);
4371 map = isl_map_neg(map);
4372 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4373 return -1;
4375 return 0;
4378 struct isl_vertices_test_data {
4379 const char *set;
4380 int n;
4381 const char *vertex[2];
4382 } vertices_tests[] = {
4383 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4384 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4385 { "{ A[t, i] : t = 14 and i = 1 }",
4386 1, { "{ A[14, 1] }" } },
4389 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4391 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4393 struct isl_vertices_test_data *data = user;
4394 isl_ctx *ctx;
4395 isl_multi_aff *ma;
4396 isl_basic_set *bset;
4397 isl_pw_multi_aff *pma;
4398 int i;
4399 isl_bool equal;
4401 ctx = isl_vertex_get_ctx(vertex);
4402 bset = isl_vertex_get_domain(vertex);
4403 ma = isl_vertex_get_expr(vertex);
4404 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4406 for (i = 0; i < data->n; ++i) {
4407 isl_pw_multi_aff *pma_i;
4409 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4410 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4411 isl_pw_multi_aff_free(pma_i);
4413 if (equal < 0 || equal)
4414 break;
4417 isl_pw_multi_aff_free(pma);
4418 isl_vertex_free(vertex);
4420 if (equal < 0)
4421 return isl_stat_error;
4423 return equal ? isl_stat_ok : isl_stat_error;
4426 int test_vertices(isl_ctx *ctx)
4428 int i;
4430 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4431 isl_basic_set *bset;
4432 isl_vertices *vertices;
4433 int ok = 1;
4434 int n;
4436 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4437 vertices = isl_basic_set_compute_vertices(bset);
4438 n = isl_vertices_get_n_vertices(vertices);
4439 if (vertices_tests[i].n != n)
4440 ok = 0;
4441 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4442 &vertices_tests[i]) < 0)
4443 ok = 0;
4444 isl_vertices_free(vertices);
4445 isl_basic_set_free(bset);
4447 if (!vertices)
4448 return -1;
4449 if (!ok)
4450 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4451 return -1);
4454 return 0;
4457 int test_union_pw(isl_ctx *ctx)
4459 int equal;
4460 const char *str;
4461 isl_union_set *uset;
4462 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4464 str = "{ [x] -> x^2 }";
4465 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4466 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4467 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4468 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4469 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4470 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4471 isl_union_pw_qpolynomial_free(upwqp1);
4472 isl_union_pw_qpolynomial_free(upwqp2);
4473 if (equal < 0)
4474 return -1;
4475 if (!equal)
4476 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4478 return 0;
4481 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4482 * defined over the correct domain space.
4484 static int test_eval(isl_ctx *ctx)
4486 const char *str;
4487 isl_point *pnt;
4488 isl_set *set;
4489 isl_union_pw_qpolynomial *upwqp;
4490 isl_val *v;
4491 int cmp;
4493 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4494 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4495 str = "{ A[6] }";
4496 set = isl_set_read_from_str(ctx, str);
4497 pnt = isl_set_sample_point(set);
4498 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4499 cmp = isl_val_cmp_si(v, 36);
4500 isl_val_free(v);
4502 if (!v)
4503 return -1;
4504 if (cmp != 0)
4505 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4507 return 0;
4510 int test_output(isl_ctx *ctx)
4512 char *s;
4513 const char *str;
4514 isl_pw_aff *pa;
4515 isl_printer *p;
4516 int equal;
4518 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4519 pa = isl_pw_aff_read_from_str(ctx, str);
4521 p = isl_printer_to_str(ctx);
4522 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4523 p = isl_printer_print_pw_aff(p, pa);
4524 s = isl_printer_get_str(p);
4525 isl_printer_free(p);
4526 isl_pw_aff_free(pa);
4527 if (!s)
4528 equal = -1;
4529 else
4530 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4531 free(s);
4532 if (equal < 0)
4533 return -1;
4534 if (!equal)
4535 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4537 return 0;
4540 int test_sample(isl_ctx *ctx)
4542 const char *str;
4543 isl_basic_set *bset1, *bset2;
4544 int empty, subset;
4546 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4547 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4548 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4549 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4550 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4551 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4552 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4553 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4554 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4555 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4556 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4557 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4558 "d - 1073741821e and "
4559 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4560 "3j >= 1 - a + b + 2e and "
4561 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4562 "3i <= 4 - a + 4b - e and "
4563 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4564 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4565 "c <= -1 + a and 3i >= -2 - a + 3e and "
4566 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4567 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4568 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4569 "1073741823e >= 1 + 1073741823b - d and "
4570 "3i >= 1073741823b + c - 1073741823e - f and "
4571 "3i >= 1 + 2b + e + 3g }";
4572 bset1 = isl_basic_set_read_from_str(ctx, str);
4573 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4574 empty = isl_basic_set_is_empty(bset2);
4575 subset = isl_basic_set_is_subset(bset2, bset1);
4576 isl_basic_set_free(bset1);
4577 isl_basic_set_free(bset2);
4578 if (empty < 0 || subset < 0)
4579 return -1;
4580 if (empty)
4581 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4582 if (!subset)
4583 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4585 return 0;
4588 int test_fixed_power(isl_ctx *ctx)
4590 const char *str;
4591 isl_map *map;
4592 isl_int exp;
4593 int equal;
4595 isl_int_init(exp);
4596 str = "{ [i] -> [i + 1] }";
4597 map = isl_map_read_from_str(ctx, str);
4598 isl_int_set_si(exp, 23);
4599 map = isl_map_fixed_power(map, exp);
4600 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4601 isl_int_clear(exp);
4602 isl_map_free(map);
4603 if (equal < 0)
4604 return -1;
4606 return 0;
4609 int test_slice(isl_ctx *ctx)
4611 const char *str;
4612 isl_map *map;
4613 int equal;
4615 str = "{ [i] -> [j] }";
4616 map = isl_map_read_from_str(ctx, str);
4617 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4618 equal = map_check_equal(map, "{ [i] -> [i] }");
4619 isl_map_free(map);
4620 if (equal < 0)
4621 return -1;
4623 str = "{ [i] -> [j] }";
4624 map = isl_map_read_from_str(ctx, str);
4625 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4626 equal = map_check_equal(map, "{ [i] -> [j] }");
4627 isl_map_free(map);
4628 if (equal < 0)
4629 return -1;
4631 str = "{ [i] -> [j] }";
4632 map = isl_map_read_from_str(ctx, str);
4633 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4634 equal = map_check_equal(map, "{ [i] -> [-i] }");
4635 isl_map_free(map);
4636 if (equal < 0)
4637 return -1;
4639 str = "{ [i] -> [j] }";
4640 map = isl_map_read_from_str(ctx, str);
4641 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4642 equal = map_check_equal(map, "{ [0] -> [j] }");
4643 isl_map_free(map);
4644 if (equal < 0)
4645 return -1;
4647 str = "{ [i] -> [j] }";
4648 map = isl_map_read_from_str(ctx, str);
4649 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4650 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4651 isl_map_free(map);
4652 if (equal < 0)
4653 return -1;
4655 str = "{ [i] -> [j] }";
4656 map = isl_map_read_from_str(ctx, str);
4657 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4658 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4659 isl_map_free(map);
4660 if (equal < 0)
4661 return -1;
4663 return 0;
4666 int test_eliminate(isl_ctx *ctx)
4668 const char *str;
4669 isl_map *map;
4670 int equal;
4672 str = "{ [i] -> [j] : i = 2j }";
4673 map = isl_map_read_from_str(ctx, str);
4674 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4675 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4676 isl_map_free(map);
4677 if (equal < 0)
4678 return -1;
4680 return 0;
4683 /* Check that isl_set_dim_residue_class detects that the values of j
4684 * in the set below are all odd and that it does not detect any spurious
4685 * strides.
4687 static int test_residue_class(isl_ctx *ctx)
4689 const char *str;
4690 isl_set *set;
4691 isl_int m, r;
4692 int res;
4694 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4695 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4696 set = isl_set_read_from_str(ctx, str);
4697 isl_int_init(m);
4698 isl_int_init(r);
4699 res = isl_set_dim_residue_class(set, 1, &m, &r);
4700 if (res >= 0 &&
4701 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4702 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4703 res = -1);
4704 isl_int_clear(r);
4705 isl_int_clear(m);
4706 isl_set_free(set);
4708 return res;
4711 int test_align_parameters(isl_ctx *ctx)
4713 const char *str;
4714 isl_space *space;
4715 isl_multi_aff *ma1, *ma2;
4716 int equal;
4718 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4719 ma1 = isl_multi_aff_read_from_str(ctx, str);
4721 space = isl_space_params_alloc(ctx, 1);
4722 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4723 ma1 = isl_multi_aff_align_params(ma1, space);
4725 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4726 ma2 = isl_multi_aff_read_from_str(ctx, str);
4728 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4730 isl_multi_aff_free(ma1);
4731 isl_multi_aff_free(ma2);
4733 if (equal < 0)
4734 return -1;
4735 if (!equal)
4736 isl_die(ctx, isl_error_unknown,
4737 "result not as expected", return -1);
4739 return 0;
4742 static int test_list(isl_ctx *ctx)
4744 isl_id *a, *b, *c, *d, *id;
4745 isl_id_list *list;
4746 int ok;
4748 a = isl_id_alloc(ctx, "a", NULL);
4749 b = isl_id_alloc(ctx, "b", NULL);
4750 c = isl_id_alloc(ctx, "c", NULL);
4751 d = isl_id_alloc(ctx, "d", NULL);
4753 list = isl_id_list_alloc(ctx, 4);
4754 list = isl_id_list_add(list, a);
4755 list = isl_id_list_add(list, b);
4756 list = isl_id_list_add(list, c);
4757 list = isl_id_list_add(list, d);
4758 list = isl_id_list_drop(list, 1, 1);
4760 if (isl_id_list_n_id(list) != 3) {
4761 isl_id_list_free(list);
4762 isl_die(ctx, isl_error_unknown,
4763 "unexpected number of elements in list", return -1);
4766 id = isl_id_list_get_id(list, 0);
4767 ok = id == a;
4768 isl_id_free(id);
4769 id = isl_id_list_get_id(list, 1);
4770 ok = ok && id == c;
4771 isl_id_free(id);
4772 id = isl_id_list_get_id(list, 2);
4773 ok = ok && id == d;
4774 isl_id_free(id);
4776 isl_id_list_free(list);
4778 if (!ok)
4779 isl_die(ctx, isl_error_unknown,
4780 "unexpected elements in list", return -1);
4782 return 0;
4785 const char *set_conversion_tests[] = {
4786 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4787 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4788 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4789 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4790 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4793 /* Check that converting from isl_set to isl_pw_multi_aff and back
4794 * to isl_set produces the original isl_set.
4796 static int test_set_conversion(isl_ctx *ctx)
4798 int i;
4799 const char *str;
4800 isl_set *set1, *set2;
4801 isl_pw_multi_aff *pma;
4802 int equal;
4804 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4805 str = set_conversion_tests[i];
4806 set1 = isl_set_read_from_str(ctx, str);
4807 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4808 set2 = isl_set_from_pw_multi_aff(pma);
4809 equal = isl_set_is_equal(set1, set2);
4810 isl_set_free(set1);
4811 isl_set_free(set2);
4813 if (equal < 0)
4814 return -1;
4815 if (!equal)
4816 isl_die(ctx, isl_error_unknown, "bad conversion",
4817 return -1);
4820 return 0;
4823 /* Check that converting from isl_map to isl_pw_multi_aff and back
4824 * to isl_map produces the original isl_map.
4826 static int test_map_conversion(isl_ctx *ctx)
4828 const char *str;
4829 isl_map *map1, *map2;
4830 isl_pw_multi_aff *pma;
4831 int equal;
4833 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4834 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4835 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4836 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4837 "9e <= -2 - 2a) }";
4838 map1 = isl_map_read_from_str(ctx, str);
4839 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4840 map2 = isl_map_from_pw_multi_aff(pma);
4841 equal = isl_map_is_equal(map1, map2);
4842 isl_map_free(map1);
4843 isl_map_free(map2);
4845 if (equal < 0)
4846 return -1;
4847 if (!equal)
4848 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4850 return 0;
4853 static int test_conversion(isl_ctx *ctx)
4855 if (test_set_conversion(ctx) < 0)
4856 return -1;
4857 if (test_map_conversion(ctx) < 0)
4858 return -1;
4859 return 0;
4862 /* Check that isl_basic_map_curry does not modify input.
4864 static int test_curry(isl_ctx *ctx)
4866 const char *str;
4867 isl_basic_map *bmap1, *bmap2;
4868 int equal;
4870 str = "{ [A[] -> B[]] -> C[] }";
4871 bmap1 = isl_basic_map_read_from_str(ctx, str);
4872 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4873 equal = isl_basic_map_is_equal(bmap1, bmap2);
4874 isl_basic_map_free(bmap1);
4875 isl_basic_map_free(bmap2);
4877 if (equal < 0)
4878 return -1;
4879 if (equal)
4880 isl_die(ctx, isl_error_unknown,
4881 "curried map should not be equal to original",
4882 return -1);
4884 return 0;
4887 struct {
4888 const char *set;
4889 const char *ma;
4890 const char *res;
4891 } preimage_tests[] = {
4892 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4893 "{ A[j,i] -> B[i,j] }",
4894 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4895 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4896 "{ A[a,b] -> B[a/2,b/6] }",
4897 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4898 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4899 "{ A[a,b] -> B[a/2,b/6] }",
4900 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4901 "exists i,j : a = 2 i and b = 6 j }" },
4902 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4903 "[n] -> { : 0 <= n <= 100 }" },
4904 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4905 "{ A[a] -> B[2a] }",
4906 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4907 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4908 "{ A[a] -> B[([a/2])] }",
4909 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4910 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4911 "{ A[a] -> B[a,a,a/3] }",
4912 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4913 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4914 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4917 static int test_preimage_basic_set(isl_ctx *ctx)
4919 int i;
4920 isl_basic_set *bset1, *bset2;
4921 isl_multi_aff *ma;
4922 int equal;
4924 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4925 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4926 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4927 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4928 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4929 equal = isl_basic_set_is_equal(bset1, bset2);
4930 isl_basic_set_free(bset1);
4931 isl_basic_set_free(bset2);
4932 if (equal < 0)
4933 return -1;
4934 if (!equal)
4935 isl_die(ctx, isl_error_unknown, "bad preimage",
4936 return -1);
4939 return 0;
4942 struct {
4943 const char *map;
4944 const char *ma;
4945 const char *res;
4946 } preimage_domain_tests[] = {
4947 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4948 "{ A[j,i] -> B[i,j] }",
4949 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4950 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4951 "{ A[i] -> B[i + 1] }",
4952 "{ A[i] -> C[i + 1] }" },
4953 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4954 "{ A[i] -> B[i + 1] }",
4955 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4956 { "{ B[i] -> C[([i/2])] }",
4957 "{ A[i] -> B[2i] }",
4958 "{ A[i] -> C[i] }" },
4959 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4960 "{ A[i] -> B[([i/5]), ([i/7])] }",
4961 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4962 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4963 "[N] -> { A[] -> B[([N/5])] }",
4964 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4965 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4966 "{ A[i] -> B[2i] }",
4967 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4968 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4969 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4970 "{ A[i] -> B[2i] }",
4971 "{ A[i] -> C[2i] }" },
4974 static int test_preimage_union_map(isl_ctx *ctx)
4976 int i;
4977 isl_union_map *umap1, *umap2;
4978 isl_multi_aff *ma;
4979 int equal;
4981 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4982 umap1 = isl_union_map_read_from_str(ctx,
4983 preimage_domain_tests[i].map);
4984 ma = isl_multi_aff_read_from_str(ctx,
4985 preimage_domain_tests[i].ma);
4986 umap2 = isl_union_map_read_from_str(ctx,
4987 preimage_domain_tests[i].res);
4988 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4989 equal = isl_union_map_is_equal(umap1, umap2);
4990 isl_union_map_free(umap1);
4991 isl_union_map_free(umap2);
4992 if (equal < 0)
4993 return -1;
4994 if (!equal)
4995 isl_die(ctx, isl_error_unknown, "bad preimage",
4996 return -1);
4999 return 0;
5002 static int test_preimage(isl_ctx *ctx)
5004 if (test_preimage_basic_set(ctx) < 0)
5005 return -1;
5006 if (test_preimage_union_map(ctx) < 0)
5007 return -1;
5009 return 0;
5012 struct {
5013 const char *ma1;
5014 const char *ma;
5015 const char *res;
5016 } pullback_tests[] = {
5017 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5018 "{ A[a,b] -> C[b + 2a] }" },
5019 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5020 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5021 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5022 "{ A[a] -> C[(a)/6] }" },
5023 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5024 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5025 "{ A[a] -> C[(2a)/3] }" },
5026 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5027 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5028 "{ A[i,j] -> C[i + j, i + j] }"},
5029 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5030 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5031 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5032 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5033 "{ [i, j] -> [floor((i)/3), j] }",
5034 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5037 static int test_pullback(isl_ctx *ctx)
5039 int i;
5040 isl_multi_aff *ma1, *ma2;
5041 isl_multi_aff *ma;
5042 int equal;
5044 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5045 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5046 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5047 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5048 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5049 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5050 isl_multi_aff_free(ma1);
5051 isl_multi_aff_free(ma2);
5052 if (equal < 0)
5053 return -1;
5054 if (!equal)
5055 isl_die(ctx, isl_error_unknown, "bad pullback",
5056 return -1);
5059 return 0;
5062 /* Check that negation is printed correctly and that equal expressions
5063 * are correctly identified.
5065 static int test_ast(isl_ctx *ctx)
5067 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5068 char *str;
5069 int ok, equal;
5071 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5072 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5073 expr = isl_ast_expr_add(expr1, expr2);
5074 expr2 = isl_ast_expr_copy(expr);
5075 expr = isl_ast_expr_neg(expr);
5076 expr2 = isl_ast_expr_neg(expr2);
5077 equal = isl_ast_expr_is_equal(expr, expr2);
5078 str = isl_ast_expr_to_str(expr);
5079 ok = str ? !strcmp(str, "-(A + B)") : -1;
5080 free(str);
5081 isl_ast_expr_free(expr);
5082 isl_ast_expr_free(expr2);
5084 if (ok < 0 || equal < 0)
5085 return -1;
5086 if (!equal)
5087 isl_die(ctx, isl_error_unknown,
5088 "equal expressions not considered equal", return -1);
5089 if (!ok)
5090 isl_die(ctx, isl_error_unknown,
5091 "isl_ast_expr printed incorrectly", return -1);
5093 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5094 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5095 expr = isl_ast_expr_add(expr1, expr2);
5096 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5097 expr = isl_ast_expr_sub(expr3, expr);
5098 str = isl_ast_expr_to_str(expr);
5099 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5100 free(str);
5101 isl_ast_expr_free(expr);
5103 if (ok < 0)
5104 return -1;
5105 if (!ok)
5106 isl_die(ctx, isl_error_unknown,
5107 "isl_ast_expr printed incorrectly", return -1);
5109 return 0;
5112 /* Check that isl_ast_build_expr_from_set returns a valid expression
5113 * for an empty set. Note that isl_ast_build_expr_from_set getting
5114 * called on an empty set probably indicates a bug in the caller.
5116 static int test_ast_build(isl_ctx *ctx)
5118 isl_set *set;
5119 isl_ast_build *build;
5120 isl_ast_expr *expr;
5122 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5123 build = isl_ast_build_from_context(set);
5125 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5126 expr = isl_ast_build_expr_from_set(build, set);
5128 isl_ast_expr_free(expr);
5129 isl_ast_build_free(build);
5131 if (!expr)
5132 return -1;
5134 return 0;
5137 /* Internal data structure for before_for and after_for callbacks.
5139 * depth is the current depth
5140 * before is the number of times before_for has been called
5141 * after is the number of times after_for has been called
5143 struct isl_test_codegen_data {
5144 int depth;
5145 int before;
5146 int after;
5149 /* This function is called before each for loop in the AST generated
5150 * from test_ast_gen1.
5152 * Increment the number of calls and the depth.
5153 * Check that the space returned by isl_ast_build_get_schedule_space
5154 * matches the target space of the schedule returned by
5155 * isl_ast_build_get_schedule.
5156 * Return an isl_id that is checked by the corresponding call
5157 * to after_for.
5159 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5160 void *user)
5162 struct isl_test_codegen_data *data = user;
5163 isl_ctx *ctx;
5164 isl_space *space;
5165 isl_union_map *schedule;
5166 isl_union_set *uset;
5167 isl_set *set;
5168 int empty;
5169 char name[] = "d0";
5171 ctx = isl_ast_build_get_ctx(build);
5173 if (data->before >= 3)
5174 isl_die(ctx, isl_error_unknown,
5175 "unexpected number of for nodes", return NULL);
5176 if (data->depth >= 2)
5177 isl_die(ctx, isl_error_unknown,
5178 "unexpected depth", return NULL);
5180 snprintf(name, sizeof(name), "d%d", data->depth);
5181 data->before++;
5182 data->depth++;
5184 schedule = isl_ast_build_get_schedule(build);
5185 uset = isl_union_map_range(schedule);
5186 if (!uset)
5187 return NULL;
5188 if (isl_union_set_n_set(uset) != 1) {
5189 isl_union_set_free(uset);
5190 isl_die(ctx, isl_error_unknown,
5191 "expecting single range space", return NULL);
5194 space = isl_ast_build_get_schedule_space(build);
5195 set = isl_union_set_extract_set(uset, space);
5196 isl_union_set_free(uset);
5197 empty = isl_set_is_empty(set);
5198 isl_set_free(set);
5200 if (empty < 0)
5201 return NULL;
5202 if (empty)
5203 isl_die(ctx, isl_error_unknown,
5204 "spaces don't match", return NULL);
5206 return isl_id_alloc(ctx, name, NULL);
5209 /* This function is called after each for loop in the AST generated
5210 * from test_ast_gen1.
5212 * Increment the number of calls and decrement the depth.
5213 * Check that the annotation attached to the node matches
5214 * the isl_id returned by the corresponding call to before_for.
5216 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5217 __isl_keep isl_ast_build *build, void *user)
5219 struct isl_test_codegen_data *data = user;
5220 isl_id *id;
5221 const char *name;
5222 int valid;
5224 data->after++;
5225 data->depth--;
5227 if (data->after > data->before)
5228 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5229 "mismatch in number of for nodes",
5230 return isl_ast_node_free(node));
5232 id = isl_ast_node_get_annotation(node);
5233 if (!id)
5234 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5235 "missing annotation", return isl_ast_node_free(node));
5237 name = isl_id_get_name(id);
5238 valid = name && atoi(name + 1) == data->depth;
5239 isl_id_free(id);
5241 if (!valid)
5242 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5243 "wrong annotation", return isl_ast_node_free(node));
5245 return node;
5248 /* Check that the before_each_for and after_each_for callbacks
5249 * are called for each for loop in the generated code,
5250 * that they are called in the right order and that the isl_id
5251 * returned from the before_each_for callback is attached to
5252 * the isl_ast_node passed to the corresponding after_each_for call.
5254 static int test_ast_gen1(isl_ctx *ctx)
5256 const char *str;
5257 isl_set *set;
5258 isl_union_map *schedule;
5259 isl_ast_build *build;
5260 isl_ast_node *tree;
5261 struct isl_test_codegen_data data;
5263 str = "[N] -> { : N >= 10 }";
5264 set = isl_set_read_from_str(ctx, str);
5265 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5266 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5267 schedule = isl_union_map_read_from_str(ctx, str);
5269 data.before = 0;
5270 data.after = 0;
5271 data.depth = 0;
5272 build = isl_ast_build_from_context(set);
5273 build = isl_ast_build_set_before_each_for(build,
5274 &before_for, &data);
5275 build = isl_ast_build_set_after_each_for(build,
5276 &after_for, &data);
5277 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5278 isl_ast_build_free(build);
5279 if (!tree)
5280 return -1;
5282 isl_ast_node_free(tree);
5284 if (data.before != 3 || data.after != 3)
5285 isl_die(ctx, isl_error_unknown,
5286 "unexpected number of for nodes", return -1);
5288 return 0;
5291 /* Check that the AST generator handles domains that are integrally disjoint
5292 * but not rationally disjoint.
5294 static int test_ast_gen2(isl_ctx *ctx)
5296 const char *str;
5297 isl_set *set;
5298 isl_union_map *schedule;
5299 isl_union_map *options;
5300 isl_ast_build *build;
5301 isl_ast_node *tree;
5303 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5304 schedule = isl_union_map_read_from_str(ctx, str);
5305 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5306 build = isl_ast_build_from_context(set);
5308 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5309 options = isl_union_map_read_from_str(ctx, str);
5310 build = isl_ast_build_set_options(build, options);
5311 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5312 isl_ast_build_free(build);
5313 if (!tree)
5314 return -1;
5315 isl_ast_node_free(tree);
5317 return 0;
5320 /* Increment *user on each call.
5322 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5323 __isl_keep isl_ast_build *build, void *user)
5325 int *n = user;
5327 (*n)++;
5329 return node;
5332 /* Test that unrolling tries to minimize the number of instances.
5333 * In particular, for the schedule given below, make sure it generates
5334 * 3 nodes (rather than 101).
5336 static int test_ast_gen3(isl_ctx *ctx)
5338 const char *str;
5339 isl_set *set;
5340 isl_union_map *schedule;
5341 isl_union_map *options;
5342 isl_ast_build *build;
5343 isl_ast_node *tree;
5344 int n_domain = 0;
5346 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5347 schedule = isl_union_map_read_from_str(ctx, str);
5348 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5350 str = "{ [i] -> unroll[0] }";
5351 options = isl_union_map_read_from_str(ctx, str);
5353 build = isl_ast_build_from_context(set);
5354 build = isl_ast_build_set_options(build, options);
5355 build = isl_ast_build_set_at_each_domain(build,
5356 &count_domains, &n_domain);
5357 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5358 isl_ast_build_free(build);
5359 if (!tree)
5360 return -1;
5362 isl_ast_node_free(tree);
5364 if (n_domain != 3)
5365 isl_die(ctx, isl_error_unknown,
5366 "unexpected number of for nodes", return -1);
5368 return 0;
5371 /* Check that if the ast_build_exploit_nested_bounds options is set,
5372 * we do not get an outer if node in the generated AST,
5373 * while we do get such an outer if node if the options is not set.
5375 static int test_ast_gen4(isl_ctx *ctx)
5377 const char *str;
5378 isl_set *set;
5379 isl_union_map *schedule;
5380 isl_ast_build *build;
5381 isl_ast_node *tree;
5382 enum isl_ast_node_type type;
5383 int enb;
5385 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5386 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5388 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5390 schedule = isl_union_map_read_from_str(ctx, str);
5391 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5392 build = isl_ast_build_from_context(set);
5393 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5394 isl_ast_build_free(build);
5395 if (!tree)
5396 return -1;
5398 type = isl_ast_node_get_type(tree);
5399 isl_ast_node_free(tree);
5401 if (type == isl_ast_node_if)
5402 isl_die(ctx, isl_error_unknown,
5403 "not expecting if node", return -1);
5405 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5407 schedule = isl_union_map_read_from_str(ctx, str);
5408 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5409 build = isl_ast_build_from_context(set);
5410 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5411 isl_ast_build_free(build);
5412 if (!tree)
5413 return -1;
5415 type = isl_ast_node_get_type(tree);
5416 isl_ast_node_free(tree);
5418 if (type != isl_ast_node_if)
5419 isl_die(ctx, isl_error_unknown,
5420 "expecting if node", return -1);
5422 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5424 return 0;
5427 /* This function is called for each leaf in the AST generated
5428 * from test_ast_gen5.
5430 * We finalize the AST generation by extending the outer schedule
5431 * with a zero-dimensional schedule. If this results in any for loops,
5432 * then this means that we did not pass along enough information
5433 * about the outer schedule to the inner AST generation.
5435 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5436 void *user)
5438 isl_union_map *schedule, *extra;
5439 isl_ast_node *tree;
5441 schedule = isl_ast_build_get_schedule(build);
5442 extra = isl_union_map_copy(schedule);
5443 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5444 schedule = isl_union_map_range_product(schedule, extra);
5445 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5446 isl_ast_build_free(build);
5448 if (!tree)
5449 return NULL;
5451 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5452 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5453 "code should not contain any for loop",
5454 return isl_ast_node_free(tree));
5456 return tree;
5459 /* Check that we do not lose any information when going back and
5460 * forth between internal and external schedule.
5462 * In particular, we create an AST where we unroll the only
5463 * non-constant dimension in the schedule. We therefore do
5464 * not expect any for loops in the AST. However, older versions
5465 * of isl would not pass along enough information about the outer
5466 * schedule when performing an inner code generation from a create_leaf
5467 * callback, resulting in the inner code generation producing a for loop.
5469 static int test_ast_gen5(isl_ctx *ctx)
5471 const char *str;
5472 isl_set *set;
5473 isl_union_map *schedule, *options;
5474 isl_ast_build *build;
5475 isl_ast_node *tree;
5477 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5478 schedule = isl_union_map_read_from_str(ctx, str);
5480 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5481 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5482 options = isl_union_map_read_from_str(ctx, str);
5484 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5485 build = isl_ast_build_from_context(set);
5486 build = isl_ast_build_set_options(build, options);
5487 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5488 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5489 isl_ast_build_free(build);
5490 isl_ast_node_free(tree);
5491 if (!tree)
5492 return -1;
5494 return 0;
5497 static int test_ast_gen(isl_ctx *ctx)
5499 if (test_ast_gen1(ctx) < 0)
5500 return -1;
5501 if (test_ast_gen2(ctx) < 0)
5502 return -1;
5503 if (test_ast_gen3(ctx) < 0)
5504 return -1;
5505 if (test_ast_gen4(ctx) < 0)
5506 return -1;
5507 if (test_ast_gen5(ctx) < 0)
5508 return -1;
5509 return 0;
5512 /* Check if dropping output dimensions from an isl_pw_multi_aff
5513 * works properly.
5515 static int test_pw_multi_aff(isl_ctx *ctx)
5517 const char *str;
5518 isl_pw_multi_aff *pma1, *pma2;
5519 int equal;
5521 str = "{ [i,j] -> [i+j, 4i-j] }";
5522 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5523 str = "{ [i,j] -> [4i-j] }";
5524 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5526 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5528 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5530 isl_pw_multi_aff_free(pma1);
5531 isl_pw_multi_aff_free(pma2);
5532 if (equal < 0)
5533 return -1;
5534 if (!equal)
5535 isl_die(ctx, isl_error_unknown,
5536 "expressions not equal", return -1);
5538 return 0;
5541 /* Check that we can properly parse multi piecewise affine expressions
5542 * where the piecewise affine expressions have different domains.
5544 static int test_multi_pw_aff(isl_ctx *ctx)
5546 const char *str;
5547 isl_set *dom, *dom2;
5548 isl_multi_pw_aff *mpa1, *mpa2;
5549 isl_pw_aff *pa;
5550 int equal;
5551 int equal_domain;
5553 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5554 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5555 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5556 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5557 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5558 str = "{ [i] -> [(i : i > 0), 2i] }";
5559 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5561 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5563 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5564 dom = isl_pw_aff_domain(pa);
5565 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5566 dom2 = isl_pw_aff_domain(pa);
5567 equal_domain = isl_set_is_equal(dom, dom2);
5569 isl_set_free(dom);
5570 isl_set_free(dom2);
5571 isl_multi_pw_aff_free(mpa1);
5572 isl_multi_pw_aff_free(mpa2);
5574 if (equal < 0)
5575 return -1;
5576 if (!equal)
5577 isl_die(ctx, isl_error_unknown,
5578 "expressions not equal", return -1);
5580 if (equal_domain < 0)
5581 return -1;
5582 if (equal_domain)
5583 isl_die(ctx, isl_error_unknown,
5584 "domains unexpectedly equal", return -1);
5586 return 0;
5589 /* This is a regression test for a bug where isl_basic_map_simplify
5590 * would end up in an infinite loop. In particular, we construct
5591 * an empty basic set that is not obviously empty.
5592 * isl_basic_set_is_empty marks the basic set as empty.
5593 * After projecting out i3, the variable can be dropped completely,
5594 * but isl_basic_map_simplify refrains from doing so if the basic set
5595 * is empty and would end up in an infinite loop if it didn't test
5596 * explicitly for empty basic maps in the outer loop.
5598 static int test_simplify(isl_ctx *ctx)
5600 const char *str;
5601 isl_basic_set *bset;
5602 int empty;
5604 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5605 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5606 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5607 "i3 >= i2 }";
5608 bset = isl_basic_set_read_from_str(ctx, str);
5609 empty = isl_basic_set_is_empty(bset);
5610 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5611 isl_basic_set_free(bset);
5612 if (!bset)
5613 return -1;
5614 if (!empty)
5615 isl_die(ctx, isl_error_unknown,
5616 "basic set should be empty", return -1);
5618 return 0;
5621 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5622 * with gbr context would fail to disable the use of the shifted tableau
5623 * when transferring equalities for the input to the context, resulting
5624 * in invalid sample values.
5626 static int test_partial_lexmin(isl_ctx *ctx)
5628 const char *str;
5629 isl_basic_set *bset;
5630 isl_basic_map *bmap;
5631 isl_map *map;
5633 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5634 bmap = isl_basic_map_read_from_str(ctx, str);
5635 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5636 bset = isl_basic_set_read_from_str(ctx, str);
5637 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5638 isl_map_free(map);
5640 if (!map)
5641 return -1;
5643 return 0;
5646 /* Check that the variable compression performed on the existentially
5647 * quantified variables inside isl_basic_set_compute_divs is not confused
5648 * by the implicit equalities among the parameters.
5650 static int test_compute_divs(isl_ctx *ctx)
5652 const char *str;
5653 isl_basic_set *bset;
5654 isl_set *set;
5656 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5657 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5658 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5659 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5660 bset = isl_basic_set_read_from_str(ctx, str);
5661 set = isl_basic_set_compute_divs(bset);
5662 isl_set_free(set);
5663 if (!set)
5664 return -1;
5666 return 0;
5669 /* Check that the reaching domain elements and the prefix schedule
5670 * at a leaf node are the same before and after grouping.
5672 static int test_schedule_tree_group_1(isl_ctx *ctx)
5674 int equal;
5675 const char *str;
5676 isl_id *id;
5677 isl_union_set *uset;
5678 isl_multi_union_pw_aff *mupa;
5679 isl_union_pw_multi_aff *upma1, *upma2;
5680 isl_union_set *domain1, *domain2;
5681 isl_union_map *umap1, *umap2;
5682 isl_schedule_node *node;
5684 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5685 uset = isl_union_set_read_from_str(ctx, str);
5686 node = isl_schedule_node_from_domain(uset);
5687 node = isl_schedule_node_child(node, 0);
5688 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5689 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5690 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5691 node = isl_schedule_node_child(node, 0);
5692 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5693 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5694 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5695 node = isl_schedule_node_child(node, 0);
5696 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5697 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5698 domain1 = isl_schedule_node_get_domain(node);
5699 id = isl_id_alloc(ctx, "group", NULL);
5700 node = isl_schedule_node_parent(node);
5701 node = isl_schedule_node_group(node, id);
5702 node = isl_schedule_node_child(node, 0);
5703 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5704 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5705 domain2 = isl_schedule_node_get_domain(node);
5706 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5707 if (equal >= 0 && equal)
5708 equal = isl_union_set_is_equal(domain1, domain2);
5709 if (equal >= 0 && equal)
5710 equal = isl_union_map_is_equal(umap1, umap2);
5711 isl_union_map_free(umap1);
5712 isl_union_map_free(umap2);
5713 isl_union_set_free(domain1);
5714 isl_union_set_free(domain2);
5715 isl_union_pw_multi_aff_free(upma1);
5716 isl_union_pw_multi_aff_free(upma2);
5717 isl_schedule_node_free(node);
5719 if (equal < 0)
5720 return -1;
5721 if (!equal)
5722 isl_die(ctx, isl_error_unknown,
5723 "expressions not equal", return -1);
5725 return 0;
5728 /* Check that we can have nested groupings and that the union map
5729 * schedule representation is the same before and after the grouping.
5730 * Note that after the grouping, the union map representation contains
5731 * the domain constraints from the ranges of the expansion nodes,
5732 * while they are missing from the union map representation of
5733 * the tree without expansion nodes.
5735 * Also check that the global expansion is as expected.
5737 static int test_schedule_tree_group_2(isl_ctx *ctx)
5739 int equal, equal_expansion;
5740 const char *str;
5741 isl_id *id;
5742 isl_union_set *uset;
5743 isl_union_map *umap1, *umap2;
5744 isl_union_map *expansion1, *expansion2;
5745 isl_union_set_list *filters;
5746 isl_multi_union_pw_aff *mupa;
5747 isl_schedule *schedule;
5748 isl_schedule_node *node;
5750 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
5751 "S3[i,j] : 0 <= i,j < 10 }";
5752 uset = isl_union_set_read_from_str(ctx, str);
5753 node = isl_schedule_node_from_domain(uset);
5754 node = isl_schedule_node_child(node, 0);
5755 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
5756 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5757 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5758 node = isl_schedule_node_child(node, 0);
5759 str = "{ S1[i,j] }";
5760 uset = isl_union_set_read_from_str(ctx, str);
5761 filters = isl_union_set_list_from_union_set(uset);
5762 str = "{ S2[i,j]; S3[i,j] }";
5763 uset = isl_union_set_read_from_str(ctx, str);
5764 filters = isl_union_set_list_add(filters, uset);
5765 node = isl_schedule_node_insert_sequence(node, filters);
5766 node = isl_schedule_node_child(node, 1);
5767 node = isl_schedule_node_child(node, 0);
5768 str = "{ S2[i,j] }";
5769 uset = isl_union_set_read_from_str(ctx, str);
5770 filters = isl_union_set_list_from_union_set(uset);
5771 str = "{ S3[i,j] }";
5772 uset = isl_union_set_read_from_str(ctx, str);
5773 filters = isl_union_set_list_add(filters, uset);
5774 node = isl_schedule_node_insert_sequence(node, filters);
5776 schedule = isl_schedule_node_get_schedule(node);
5777 umap1 = isl_schedule_get_map(schedule);
5778 uset = isl_schedule_get_domain(schedule);
5779 umap1 = isl_union_map_intersect_domain(umap1, uset);
5780 isl_schedule_free(schedule);
5782 node = isl_schedule_node_parent(node);
5783 node = isl_schedule_node_parent(node);
5784 id = isl_id_alloc(ctx, "group1", NULL);
5785 node = isl_schedule_node_group(node, id);
5786 node = isl_schedule_node_child(node, 1);
5787 node = isl_schedule_node_child(node, 0);
5788 id = isl_id_alloc(ctx, "group2", NULL);
5789 node = isl_schedule_node_group(node, id);
5791 schedule = isl_schedule_node_get_schedule(node);
5792 umap2 = isl_schedule_get_map(schedule);
5793 isl_schedule_free(schedule);
5795 node = isl_schedule_node_root(node);
5796 node = isl_schedule_node_child(node, 0);
5797 expansion1 = isl_schedule_node_get_subtree_expansion(node);
5798 isl_schedule_node_free(node);
5800 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
5801 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
5802 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
5804 expansion2 = isl_union_map_read_from_str(ctx, str);
5806 equal = isl_union_map_is_equal(umap1, umap2);
5807 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
5809 isl_union_map_free(umap1);
5810 isl_union_map_free(umap2);
5811 isl_union_map_free(expansion1);
5812 isl_union_map_free(expansion2);
5814 if (equal < 0 || equal_expansion < 0)
5815 return -1;
5816 if (!equal)
5817 isl_die(ctx, isl_error_unknown,
5818 "expressions not equal", return -1);
5819 if (!equal_expansion)
5820 isl_die(ctx, isl_error_unknown,
5821 "unexpected expansion", return -1);
5823 return 0;
5826 /* Some tests for the isl_schedule_node_group function.
5828 static int test_schedule_tree_group(isl_ctx *ctx)
5830 if (test_schedule_tree_group_1(ctx) < 0)
5831 return -1;
5832 if (test_schedule_tree_group_2(ctx) < 0)
5833 return -1;
5834 return 0;
5837 struct {
5838 const char *set;
5839 const char *dual;
5840 } coef_tests[] = {
5841 { "{ rat: [i] : 0 <= i <= 10 }",
5842 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5843 { "{ rat: [i] : FALSE }",
5844 "{ rat: coefficients[[cst] -> [a]] }" },
5845 { "{ rat: [i] : }",
5846 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5849 struct {
5850 const char *set;
5851 const char *dual;
5852 } sol_tests[] = {
5853 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5854 "{ rat: [i] : 0 <= i <= 10 }" },
5855 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5856 "{ rat: [i] }" },
5857 { "{ rat: coefficients[[cst] -> [a]] }",
5858 "{ rat: [i] : FALSE }" },
5861 /* Test the basic functionality of isl_basic_set_coefficients and
5862 * isl_basic_set_solutions.
5864 static int test_dual(isl_ctx *ctx)
5866 int i;
5868 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5869 int equal;
5870 isl_basic_set *bset1, *bset2;
5872 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5873 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5874 bset1 = isl_basic_set_coefficients(bset1);
5875 equal = isl_basic_set_is_equal(bset1, bset2);
5876 isl_basic_set_free(bset1);
5877 isl_basic_set_free(bset2);
5878 if (equal < 0)
5879 return -1;
5880 if (!equal)
5881 isl_die(ctx, isl_error_unknown,
5882 "incorrect dual", return -1);
5885 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5886 int equal;
5887 isl_basic_set *bset1, *bset2;
5889 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5890 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5891 bset1 = isl_basic_set_solutions(bset1);
5892 equal = isl_basic_set_is_equal(bset1, bset2);
5893 isl_basic_set_free(bset1);
5894 isl_basic_set_free(bset2);
5895 if (equal < 0)
5896 return -1;
5897 if (!equal)
5898 isl_die(ctx, isl_error_unknown,
5899 "incorrect dual", return -1);
5902 return 0;
5905 struct {
5906 int scale_tile;
5907 int shift_point;
5908 const char *domain;
5909 const char *schedule;
5910 const char *sizes;
5911 const char *tile;
5912 const char *point;
5913 } tile_tests[] = {
5914 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5915 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5916 "{ [32,32] }",
5917 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5918 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5920 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5921 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5922 "{ [32,32] }",
5923 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5924 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5926 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5927 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5928 "{ [32,32] }",
5929 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5930 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5932 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5933 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5934 "{ [32,32] }",
5935 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5936 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5940 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
5941 * tile the band and then check if the tile and point bands have the
5942 * expected partial schedule.
5944 static int test_tile(isl_ctx *ctx)
5946 int i;
5947 int scale;
5948 int shift;
5950 scale = isl_options_get_tile_scale_tile_loops(ctx);
5951 shift = isl_options_get_tile_shift_point_loops(ctx);
5953 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
5954 int opt;
5955 int equal;
5956 const char *str;
5957 isl_union_set *domain;
5958 isl_multi_union_pw_aff *mupa, *mupa2;
5959 isl_schedule_node *node;
5960 isl_multi_val *sizes;
5962 opt = tile_tests[i].scale_tile;
5963 isl_options_set_tile_scale_tile_loops(ctx, opt);
5964 opt = tile_tests[i].shift_point;
5965 isl_options_set_tile_shift_point_loops(ctx, opt);
5967 str = tile_tests[i].domain;
5968 domain = isl_union_set_read_from_str(ctx, str);
5969 node = isl_schedule_node_from_domain(domain);
5970 node = isl_schedule_node_child(node, 0);
5971 str = tile_tests[i].schedule;
5972 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5973 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5974 str = tile_tests[i].sizes;
5975 sizes = isl_multi_val_read_from_str(ctx, str);
5976 node = isl_schedule_node_band_tile(node, sizes);
5978 str = tile_tests[i].tile;
5979 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5980 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
5981 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
5982 isl_multi_union_pw_aff_free(mupa);
5983 isl_multi_union_pw_aff_free(mupa2);
5985 node = isl_schedule_node_child(node, 0);
5987 str = tile_tests[i].point;
5988 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5989 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
5990 if (equal >= 0 && equal)
5991 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
5992 mupa2);
5993 isl_multi_union_pw_aff_free(mupa);
5994 isl_multi_union_pw_aff_free(mupa2);
5996 isl_schedule_node_free(node);
5998 if (equal < 0)
5999 return -1;
6000 if (!equal)
6001 isl_die(ctx, isl_error_unknown,
6002 "unexpected result", return -1);
6005 isl_options_set_tile_scale_tile_loops(ctx, scale);
6006 isl_options_set_tile_shift_point_loops(ctx, shift);
6008 return 0;
6011 /* Check that the domain hash of a space is equal to the hash
6012 * of the domain of the space.
6014 static int test_domain_hash(isl_ctx *ctx)
6016 isl_map *map;
6017 isl_space *space;
6018 uint32_t hash1, hash2;
6020 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6021 space = isl_map_get_space(map);
6022 isl_map_free(map);
6023 hash1 = isl_space_get_domain_hash(space);
6024 space = isl_space_domain(space);
6025 hash2 = isl_space_get_hash(space);
6026 isl_space_free(space);
6028 if (!space)
6029 return -1;
6030 if (hash1 != hash2)
6031 isl_die(ctx, isl_error_unknown,
6032 "domain hash not equal to hash of domain", return -1);
6034 return 0;
6037 struct {
6038 const char *name;
6039 int (*fn)(isl_ctx *ctx);
6040 } tests [] = {
6041 { "domain hash", &test_domain_hash },
6042 { "dual", &test_dual },
6043 { "dependence analysis", &test_flow },
6044 { "val", &test_val },
6045 { "compute divs", &test_compute_divs },
6046 { "partial lexmin", &test_partial_lexmin },
6047 { "simplify", &test_simplify },
6048 { "curry", &test_curry },
6049 { "piecewise multi affine expressions", &test_pw_multi_aff },
6050 { "multi piecewise affine expressions", &test_multi_pw_aff },
6051 { "conversion", &test_conversion },
6052 { "list", &test_list },
6053 { "align parameters", &test_align_parameters },
6054 { "preimage", &test_preimage },
6055 { "pullback", &test_pullback },
6056 { "AST", &test_ast },
6057 { "AST build", &test_ast_build },
6058 { "AST generation", &test_ast_gen },
6059 { "eliminate", &test_eliminate },
6060 { "residue class", &test_residue_class },
6061 { "div", &test_div },
6062 { "slice", &test_slice },
6063 { "fixed power", &test_fixed_power },
6064 { "sample", &test_sample },
6065 { "output", &test_output },
6066 { "vertices", &test_vertices },
6067 { "fixed", &test_fixed },
6068 { "equal", &test_equal },
6069 { "disjoint", &test_disjoint },
6070 { "product", &test_product },
6071 { "dim_max", &test_dim_max },
6072 { "affine", &test_aff },
6073 { "injective", &test_injective },
6074 { "schedule", &test_schedule },
6075 { "schedule tree grouping", &test_schedule_tree_group },
6076 { "tile", &test_tile },
6077 { "union_pw", &test_union_pw },
6078 { "eval", &test_eval },
6079 { "parse", &test_parse },
6080 { "single-valued", &test_sv },
6081 { "affine hull", &test_affine_hull },
6082 { "simple_hull", &test_simple_hull },
6083 { "coalesce", &test_coalesce },
6084 { "factorize", &test_factorize },
6085 { "subset", &test_subset },
6086 { "subtract", &test_subtract },
6087 { "lexmin", &test_lexmin },
6088 { "min", &test_min },
6089 { "gist", &test_gist },
6090 { "piecewise quasi-polynomials", &test_pwqp },
6091 { "lift", &test_lift },
6092 { "bound", &test_bound },
6093 { "union", &test_union },
6094 { "split periods", &test_split_periods },
6095 { "lexicographic order", &test_lex },
6096 { "bijectivity", &test_bijective },
6097 { "dataflow analysis", &test_dep },
6098 { "reading", &test_read },
6099 { "bounded", &test_bounded },
6100 { "construction", &test_construction },
6101 { "dimension manipulation", &test_dim },
6102 { "map application", &test_application },
6103 { "convex hull", &test_convex_hull },
6104 { "transitive closure", &test_closure },
6107 int main(int argc, char **argv)
6109 int i;
6110 struct isl_ctx *ctx;
6111 struct isl_options *options;
6113 srcdir = getenv("srcdir");
6114 assert(srcdir);
6116 options = isl_options_new_with_defaults();
6117 assert(options);
6118 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6120 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6121 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6122 printf("%s\n", tests[i].name);
6123 if (tests[i].fn(ctx) < 0)
6124 goto error;
6126 isl_ctx_free(ctx);
6127 return 0;
6128 error:
6129 isl_ctx_free(ctx);
6130 return -1;