isl_ast_build_expr_from_set_internal: only exploit individual earlier disjuncts
[isl.git] / isl_test.c
blob26f7e0293e31ffb1165da249e18fe26d4bcaf791
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) }" },
1294 /* Check that no constraints on i6 are introduced in the gist */
1295 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1296 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1297 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1298 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1299 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1300 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1301 "20e1 <= 1530 - 4t1 - 5i4 and "
1302 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1303 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1304 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1305 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1306 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1307 "10e0 <= -91 + 5i4 + 4i6 and "
1308 "10e0 >= -105 + 5i4 + 4i6) }",
1309 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1310 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1311 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1312 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1313 "{ [a, b, q, p] : b >= 1 + a }",
1314 "{ [a, b, q, p] : false }" },
1317 static int test_gist(struct isl_ctx *ctx)
1319 int i;
1320 const char *str;
1321 isl_basic_set *bset1, *bset2;
1322 isl_map *map1, *map2;
1323 int equal;
1325 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1326 int equal_input;
1327 isl_set *set1, *set2, *copy;
1329 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1330 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1331 copy = isl_set_copy(set1);
1332 set1 = isl_set_gist(set1, set2);
1333 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1334 equal = isl_set_is_equal(set1, set2);
1335 isl_set_free(set1);
1336 isl_set_free(set2);
1337 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1338 equal_input = isl_set_is_equal(set1, copy);
1339 isl_set_free(set1);
1340 isl_set_free(copy);
1341 if (equal < 0 || equal_input < 0)
1342 return -1;
1343 if (!equal)
1344 isl_die(ctx, isl_error_unknown,
1345 "incorrect gist result", return -1);
1346 if (!equal_input)
1347 isl_die(ctx, isl_error_unknown,
1348 "gist modified input", return -1);
1351 test_gist_case(ctx, "gist1");
1353 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1354 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1355 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1356 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1357 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1358 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1359 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1360 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1361 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1362 bset1 = isl_basic_set_read_from_str(ctx, str);
1363 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1364 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1365 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1366 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1367 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1368 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1369 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1370 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1371 bset2 = isl_basic_set_read_from_str(ctx, str);
1372 bset1 = isl_basic_set_gist(bset1, bset2);
1373 assert(bset1 && bset1->n_div == 0);
1374 isl_basic_set_free(bset1);
1376 /* Check that the integer divisions of the second disjunct
1377 * do not spread to the first disjunct.
1379 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1380 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1381 "(exists (e0 = [(-1 + t1)/16], "
1382 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1383 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1384 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1385 "o0 <= 4294967295 and t1 <= -1)) }";
1386 map1 = isl_map_read_from_str(ctx, str);
1387 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1388 map2 = isl_map_read_from_str(ctx, str);
1389 map1 = isl_map_gist(map1, map2);
1390 if (!map1)
1391 return -1;
1392 if (map1->n != 1)
1393 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1394 isl_map_free(map1); return -1);
1395 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1396 isl_die(ctx, isl_error_unknown, "expecting single div",
1397 isl_map_free(map1); return -1);
1398 isl_map_free(map1);
1400 return 0;
1403 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1405 isl_set *set, *set2;
1406 int equal;
1407 int one;
1409 set = isl_set_read_from_str(ctx, str);
1410 set = isl_set_coalesce(set);
1411 set2 = isl_set_read_from_str(ctx, str);
1412 equal = isl_set_is_equal(set, set2);
1413 one = set && set->n == 1;
1414 isl_set_free(set);
1415 isl_set_free(set2);
1417 if (equal < 0)
1418 return -1;
1419 if (!equal)
1420 isl_die(ctx, isl_error_unknown,
1421 "coalesced set not equal to input", return -1);
1422 if (check_one && !one)
1423 isl_die(ctx, isl_error_unknown,
1424 "coalesced set should not be a union", return -1);
1426 return 0;
1429 /* Inputs for coalescing tests with unbounded wrapping.
1430 * "str" is a string representation of the input set.
1431 * "single_disjunct" is set if we expect the result to consist of
1432 * a single disjunct.
1434 struct {
1435 int single_disjunct;
1436 const char *str;
1437 } coalesce_unbounded_tests[] = {
1438 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1439 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1440 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1441 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1442 "-10 <= y <= 0}" },
1443 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1444 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1445 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1446 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1449 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1450 * option turned off.
1452 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1454 int i;
1455 int r = 0;
1456 int bounded;
1458 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1459 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1461 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1462 const char *str = coalesce_unbounded_tests[i].str;
1463 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1464 if (test_coalesce_set(ctx, str, check_one) >= 0)
1465 continue;
1466 r = -1;
1467 break;
1470 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1472 return r;
1475 /* Inputs for coalescing tests.
1476 * "str" is a string representation of the input set.
1477 * "single_disjunct" is set if we expect the result to consist of
1478 * a single disjunct.
1480 struct {
1481 int single_disjunct;
1482 const char *str;
1483 } coalesce_tests[] = {
1484 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1485 "y >= x & x >= 2 & 5 >= y }" },
1486 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1487 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1488 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1489 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1490 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1491 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1492 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1493 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1494 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1495 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1496 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1497 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1498 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1499 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1500 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1501 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1502 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1503 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1504 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1505 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1506 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1507 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1508 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1509 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1510 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1511 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1512 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1513 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1514 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1515 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1516 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1517 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1518 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1519 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1520 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1521 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1522 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1523 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1524 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1525 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1526 "[o0, o1, o2, o3, o4, o5, o6]] : "
1527 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1528 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1529 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1530 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1531 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1532 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1533 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1534 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1535 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1536 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1537 "o6 >= i3 + i6 - o3 and M >= 0 and "
1538 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1539 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1540 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1541 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1542 "(o0 = 0 and M >= 2 and N >= 3) or "
1543 "(M = 0 and o0 = 0 and N >= 3) }" },
1544 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1545 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1546 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1547 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1548 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1549 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1550 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1551 "(y = 3 and x = 1) }" },
1552 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1553 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1554 "i1 <= M and i3 <= M and i4 <= M) or "
1555 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1556 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1557 "i4 <= -1 + M) }" },
1558 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1559 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1560 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1561 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1562 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1563 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1564 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1565 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1566 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1567 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1568 { 0, "{ [a, b] : exists e : 2e = a and "
1569 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1570 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1571 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1572 "j >= 1 and j' <= i + j - i' and i >= 1; "
1573 "[1, 1, 1, 1] }" },
1574 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1575 "[i,j] : exists a : j = 3a }" },
1576 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1577 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1578 "a >= 3) or "
1579 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1580 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1581 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1582 "c <= 6 + 8a and a >= 3; "
1583 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1584 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1585 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1586 "[x,0] : 3 <= x <= 4 }" },
1587 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1588 "[x,0] : 4 <= x <= 5 }" },
1589 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1590 "[x,0] : 3 <= x <= 5 }" },
1591 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1592 "[x,0] : 3 <= x <= 4 }" },
1593 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1594 "i1 <= 0; "
1595 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1596 { 1, "{ [0,0]; [1,1] }" },
1597 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1598 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1599 "ii <= k;"
1600 "[k, 0, k] : k <= 6 and k >= 1 }" },
1601 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1602 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1603 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1604 { 1, "[n] -> { [1] : n >= 0;"
1605 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1606 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1607 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1608 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1609 "2e0 <= x and 2e0 <= n);"
1610 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1611 "n >= 0) }" },
1612 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1613 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1614 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1615 "t1 = 1 }" },
1616 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1617 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1618 "[0, 0] }" },
1619 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1620 "t1 >= 13 and t1 <= 16);"
1621 "[t1] : t1 <= 15 and t1 >= 12 }" },
1622 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1623 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1624 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1625 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1626 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1627 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1628 "i <= 4j + 2 }" },
1629 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1630 "(exists (e0 : 3e0 = -2 + c0)) }" },
1631 { 0, "[n, b0, t0] -> "
1632 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1633 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1634 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1635 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1636 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1637 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1638 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1639 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1640 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1641 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1642 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1643 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1644 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1645 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1646 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1647 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1648 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1649 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1650 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1651 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1652 { 0, "{ [i0, i1, i2] : "
1653 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1654 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1655 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1656 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1657 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1658 "32e0 <= 31 + i0)) or "
1659 "i0 >= 0 }" },
1660 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1663 /* A specialized coalescing test case that would result
1664 * in a segmentation fault or a failed assertion in earlier versions of isl.
1666 static int test_coalesce_special(struct isl_ctx *ctx)
1668 const char *str;
1669 isl_map *map1, *map2;
1671 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1672 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1673 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1674 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1675 "o1 <= 239 and o1 >= 212)) or "
1676 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1677 "o1 <= 241 and o1 >= 240));"
1678 "[S_L220_OUT[] -> T7[]] -> "
1679 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1680 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1681 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1682 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1683 map1 = isl_map_read_from_str(ctx, str);
1684 map1 = isl_map_align_divs(map1);
1685 map1 = isl_map_coalesce(map1);
1686 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1687 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1688 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1689 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1690 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1691 map2 = isl_map_read_from_str(ctx, str);
1692 map2 = isl_map_union(map2, map1);
1693 map2 = isl_map_align_divs(map2);
1694 map2 = isl_map_coalesce(map2);
1695 isl_map_free(map2);
1696 if (!map2)
1697 return -1;
1699 return 0;
1702 /* Test the functionality of isl_set_coalesce.
1703 * That is, check that the output is always equal to the input
1704 * and in some cases that the result consists of a single disjunct.
1706 static int test_coalesce(struct isl_ctx *ctx)
1708 int i;
1710 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1711 const char *str = coalesce_tests[i].str;
1712 int check_one = coalesce_tests[i].single_disjunct;
1713 if (test_coalesce_set(ctx, str, check_one) < 0)
1714 return -1;
1717 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1718 return -1;
1719 if (test_coalesce_special(ctx) < 0)
1720 return -1;
1722 return 0;
1725 static int test_closure(isl_ctx *ctx)
1727 const char *str;
1728 isl_set *dom;
1729 isl_map *up, *right;
1730 isl_map *map, *map2;
1731 int exact;
1733 /* COCOA example 1 */
1734 map = isl_map_read_from_str(ctx,
1735 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1736 "1 <= i and i < n and 1 <= j and j < n or "
1737 "i2 = i + 1 and j2 = j - 1 and "
1738 "1 <= i and i < n and 2 <= j and j <= n }");
1739 map = isl_map_power(map, &exact);
1740 assert(exact);
1741 isl_map_free(map);
1743 /* COCOA example 1 */
1744 map = isl_map_read_from_str(ctx,
1745 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1746 "1 <= i and i < n and 1 <= j and j < n or "
1747 "i2 = i + 1 and j2 = j - 1 and "
1748 "1 <= i and i < n and 2 <= j and j <= n }");
1749 map = isl_map_transitive_closure(map, &exact);
1750 assert(exact);
1751 map2 = isl_map_read_from_str(ctx,
1752 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1753 "1 <= i and i < n and 1 <= j and j <= n and "
1754 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1755 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1756 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1757 assert(isl_map_is_equal(map, map2));
1758 isl_map_free(map2);
1759 isl_map_free(map);
1761 map = isl_map_read_from_str(ctx,
1762 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1763 " 0 <= y and y <= n }");
1764 map = isl_map_transitive_closure(map, &exact);
1765 map2 = isl_map_read_from_str(ctx,
1766 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1767 " 0 <= y and y <= n }");
1768 assert(isl_map_is_equal(map, map2));
1769 isl_map_free(map2);
1770 isl_map_free(map);
1772 /* COCOA example 2 */
1773 map = isl_map_read_from_str(ctx,
1774 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1775 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1776 "i2 = i + 2 and j2 = j - 2 and "
1777 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1778 map = isl_map_transitive_closure(map, &exact);
1779 assert(exact);
1780 map2 = isl_map_read_from_str(ctx,
1781 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1782 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1783 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1784 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1785 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1786 assert(isl_map_is_equal(map, map2));
1787 isl_map_free(map);
1788 isl_map_free(map2);
1790 /* COCOA Fig.2 left */
1791 map = isl_map_read_from_str(ctx,
1792 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1793 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1794 "j <= n or "
1795 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1796 "j <= 2 i - 3 and j <= n - 2 or "
1797 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1798 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1799 map = isl_map_transitive_closure(map, &exact);
1800 assert(exact);
1801 isl_map_free(map);
1803 /* COCOA Fig.2 right */
1804 map = isl_map_read_from_str(ctx,
1805 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1806 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1807 "j <= n or "
1808 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1809 "j <= 2 i - 4 and j <= n - 3 or "
1810 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1811 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1812 map = isl_map_power(map, &exact);
1813 assert(exact);
1814 isl_map_free(map);
1816 /* COCOA Fig.2 right */
1817 map = isl_map_read_from_str(ctx,
1818 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1819 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1820 "j <= n or "
1821 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1822 "j <= 2 i - 4 and j <= n - 3 or "
1823 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1824 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1825 map = isl_map_transitive_closure(map, &exact);
1826 assert(exact);
1827 map2 = isl_map_read_from_str(ctx,
1828 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1829 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1830 "j <= n and 3 + i + 2 j <= 3 n and "
1831 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1832 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1833 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1834 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1835 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1836 assert(isl_map_is_equal(map, map2));
1837 isl_map_free(map2);
1838 isl_map_free(map);
1840 /* COCOA Fig.1 right */
1841 dom = isl_set_read_from_str(ctx,
1842 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1843 "2 x - 3 y + 3 >= 0 }");
1844 right = isl_map_read_from_str(ctx,
1845 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1846 up = isl_map_read_from_str(ctx,
1847 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1848 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1849 right = isl_map_intersect_range(right, isl_set_copy(dom));
1850 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1851 up = isl_map_intersect_range(up, dom);
1852 map = isl_map_union(up, right);
1853 map = isl_map_transitive_closure(map, &exact);
1854 assert(exact);
1855 map2 = isl_map_read_from_str(ctx,
1856 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1857 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1858 assert(isl_map_is_equal(map, map2));
1859 isl_map_free(map2);
1860 isl_map_free(map);
1862 /* COCOA Theorem 1 counter example */
1863 map = isl_map_read_from_str(ctx,
1864 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1865 "i2 = 1 and j2 = j or "
1866 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1867 map = isl_map_transitive_closure(map, &exact);
1868 assert(exact);
1869 isl_map_free(map);
1871 map = isl_map_read_from_str(ctx,
1872 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1873 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1874 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1875 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1876 map = isl_map_transitive_closure(map, &exact);
1877 assert(exact);
1878 isl_map_free(map);
1880 /* Kelly et al 1996, fig 12 */
1881 map = isl_map_read_from_str(ctx,
1882 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1883 "1 <= i,j,j+1 <= n or "
1884 "j = n and j2 = 1 and i2 = i + 1 and "
1885 "1 <= i,i+1 <= n }");
1886 map = isl_map_transitive_closure(map, &exact);
1887 assert(exact);
1888 map2 = isl_map_read_from_str(ctx,
1889 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1890 "1 <= i <= n and i = i2 or "
1891 "1 <= i < i2 <= n and 1 <= j <= n and "
1892 "1 <= j2 <= n }");
1893 assert(isl_map_is_equal(map, map2));
1894 isl_map_free(map2);
1895 isl_map_free(map);
1897 /* Omega's closure4 */
1898 map = isl_map_read_from_str(ctx,
1899 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1900 "1 <= x,y <= 10 or "
1901 "x2 = x + 1 and y2 = y and "
1902 "1 <= x <= 20 && 5 <= y <= 15 }");
1903 map = isl_map_transitive_closure(map, &exact);
1904 assert(exact);
1905 isl_map_free(map);
1907 map = isl_map_read_from_str(ctx,
1908 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1909 map = isl_map_transitive_closure(map, &exact);
1910 assert(!exact);
1911 map2 = isl_map_read_from_str(ctx,
1912 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1913 assert(isl_map_is_equal(map, map2));
1914 isl_map_free(map);
1915 isl_map_free(map2);
1917 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1918 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1919 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1920 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1921 map = isl_map_read_from_str(ctx, str);
1922 map = isl_map_transitive_closure(map, &exact);
1923 assert(exact);
1924 map2 = isl_map_read_from_str(ctx, str);
1925 assert(isl_map_is_equal(map, map2));
1926 isl_map_free(map);
1927 isl_map_free(map2);
1929 str = "{[0] -> [1]; [2] -> [3]}";
1930 map = isl_map_read_from_str(ctx, str);
1931 map = isl_map_transitive_closure(map, &exact);
1932 assert(exact);
1933 map2 = isl_map_read_from_str(ctx, str);
1934 assert(isl_map_is_equal(map, map2));
1935 isl_map_free(map);
1936 isl_map_free(map2);
1938 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1939 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1940 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1941 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1942 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1943 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1944 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1945 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1946 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1947 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1948 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1949 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1950 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1951 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1952 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1953 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1954 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1955 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1956 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1957 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1958 map = isl_map_read_from_str(ctx, str);
1959 map = isl_map_transitive_closure(map, NULL);
1960 assert(map);
1961 isl_map_free(map);
1963 return 0;
1966 static int test_lex(struct isl_ctx *ctx)
1968 isl_space *dim;
1969 isl_map *map;
1970 int empty;
1972 dim = isl_space_set_alloc(ctx, 0, 0);
1973 map = isl_map_lex_le(dim);
1974 empty = isl_map_is_empty(map);
1975 isl_map_free(map);
1977 if (empty < 0)
1978 return -1;
1979 if (empty)
1980 isl_die(ctx, isl_error_unknown,
1981 "expecting non-empty result", return -1);
1983 return 0;
1986 static int test_lexmin(struct isl_ctx *ctx)
1988 int equal;
1989 const char *str;
1990 isl_basic_map *bmap;
1991 isl_map *map, *map2;
1992 isl_set *set;
1993 isl_set *set2;
1994 isl_pw_multi_aff *pma;
1996 str = "[p0, p1] -> { [] -> [] : "
1997 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1998 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1999 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2000 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2001 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2002 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2003 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2004 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2005 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2006 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2007 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2008 map = isl_map_read_from_str(ctx, str);
2009 map = isl_map_lexmin(map);
2010 isl_map_free(map);
2012 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2013 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2014 set = isl_set_read_from_str(ctx, str);
2015 set = isl_set_lexmax(set);
2016 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2017 set2 = isl_set_read_from_str(ctx, str);
2018 set = isl_set_intersect(set, set2);
2019 assert(!isl_set_is_empty(set));
2020 isl_set_free(set);
2022 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2023 map = isl_map_read_from_str(ctx, str);
2024 map = isl_map_lexmin(map);
2025 str = "{ [x] -> [5] : 6 <= x <= 8; "
2026 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2027 map2 = isl_map_read_from_str(ctx, str);
2028 assert(isl_map_is_equal(map, map2));
2029 isl_map_free(map);
2030 isl_map_free(map2);
2032 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2033 map = isl_map_read_from_str(ctx, str);
2034 map2 = isl_map_copy(map);
2035 map = isl_map_lexmin(map);
2036 assert(isl_map_is_equal(map, map2));
2037 isl_map_free(map);
2038 isl_map_free(map2);
2040 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2041 map = isl_map_read_from_str(ctx, str);
2042 map = isl_map_lexmin(map);
2043 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2044 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2045 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2046 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2047 map2 = isl_map_read_from_str(ctx, str);
2048 assert(isl_map_is_equal(map, map2));
2049 isl_map_free(map);
2050 isl_map_free(map2);
2052 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2053 " 8i' <= i and 8i' >= -7 + i }";
2054 bmap = isl_basic_map_read_from_str(ctx, str);
2055 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2056 map2 = isl_map_from_pw_multi_aff(pma);
2057 map = isl_map_from_basic_map(bmap);
2058 assert(isl_map_is_equal(map, map2));
2059 isl_map_free(map);
2060 isl_map_free(map2);
2062 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2063 map = isl_map_read_from_str(ctx, str);
2064 map = isl_map_lexmin(map);
2065 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2066 map2 = isl_map_read_from_str(ctx, str);
2067 assert(isl_map_is_equal(map, map2));
2068 isl_map_free(map);
2069 isl_map_free(map2);
2071 /* Check that empty pieces are properly combined. */
2072 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2073 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2074 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2075 map = isl_map_read_from_str(ctx, str);
2076 map = isl_map_lexmin(map);
2077 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2078 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2079 "x >= 4 }";
2080 map2 = isl_map_read_from_str(ctx, str);
2081 assert(isl_map_is_equal(map, map2));
2082 isl_map_free(map);
2083 isl_map_free(map2);
2085 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2086 " 8i' <= i and 8i' >= -7 + i }";
2087 set = isl_set_read_from_str(ctx, str);
2088 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2089 set2 = isl_set_from_pw_multi_aff(pma);
2090 equal = isl_set_is_equal(set, set2);
2091 isl_set_free(set);
2092 isl_set_free(set2);
2093 if (equal < 0)
2094 return -1;
2095 if (!equal)
2096 isl_die(ctx, isl_error_unknown,
2097 "unexpected difference between set and "
2098 "piecewise affine expression", return -1);
2100 return 0;
2103 /* Check that isl_set_min_val and isl_set_max_val compute the correct
2104 * result on non-convex inputs.
2106 static int test_min(struct isl_ctx *ctx)
2108 isl_set *set;
2109 isl_aff *aff;
2110 isl_val *val;
2111 int min_ok, max_ok;
2113 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
2114 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
2115 val = isl_set_min_val(set, aff);
2116 min_ok = isl_val_is_negone(val);
2117 isl_val_free(val);
2118 val = isl_set_max_val(set, aff);
2119 max_ok = isl_val_is_one(val);
2120 isl_val_free(val);
2121 isl_aff_free(aff);
2122 isl_set_free(set);
2124 if (min_ok < 0 || max_ok < 0)
2125 return -1;
2126 if (!min_ok)
2127 isl_die(ctx, isl_error_unknown,
2128 "unexpected minimum", return -1);
2129 if (!max_ok)
2130 isl_die(ctx, isl_error_unknown,
2131 "unexpected maximum", return -1);
2133 return 0;
2136 struct must_may {
2137 isl_map *must;
2138 isl_map *may;
2141 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2142 void *dep_user, void *user)
2144 struct must_may *mm = (struct must_may *)user;
2146 if (must)
2147 mm->must = isl_map_union(mm->must, dep);
2148 else
2149 mm->may = isl_map_union(mm->may, dep);
2151 return isl_stat_ok;
2154 static int common_space(void *first, void *second)
2156 int depth = *(int *)first;
2157 return 2 * depth;
2160 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2162 isl_map *map2;
2163 int equal;
2165 if (!map)
2166 return -1;
2168 map2 = isl_map_read_from_str(map->ctx, str);
2169 equal = isl_map_is_equal(map, map2);
2170 isl_map_free(map2);
2172 return equal;
2175 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2177 int equal;
2179 equal = map_is_equal(map, str);
2180 if (equal < 0)
2181 return -1;
2182 if (!equal)
2183 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2184 "result not as expected", return -1);
2185 return 0;
2188 static int test_dep(struct isl_ctx *ctx)
2190 const char *str;
2191 isl_space *dim;
2192 isl_map *map;
2193 isl_access_info *ai;
2194 isl_flow *flow;
2195 int depth;
2196 struct must_may mm;
2198 depth = 3;
2200 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2201 map = isl_map_read_from_str(ctx, str);
2202 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2204 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2205 map = isl_map_read_from_str(ctx, str);
2206 ai = isl_access_info_add_source(ai, map, 1, &depth);
2208 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2209 map = isl_map_read_from_str(ctx, str);
2210 ai = isl_access_info_add_source(ai, map, 1, &depth);
2212 flow = isl_access_info_compute_flow(ai);
2213 dim = isl_space_alloc(ctx, 0, 3, 3);
2214 mm.must = isl_map_empty(isl_space_copy(dim));
2215 mm.may = isl_map_empty(dim);
2217 isl_flow_foreach(flow, collect_must_may, &mm);
2219 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2220 " [1,10,0] -> [2,5,0] }";
2221 assert(map_is_equal(mm.must, str));
2222 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2223 assert(map_is_equal(mm.may, str));
2225 isl_map_free(mm.must);
2226 isl_map_free(mm.may);
2227 isl_flow_free(flow);
2230 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2231 map = isl_map_read_from_str(ctx, str);
2232 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2234 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2235 map = isl_map_read_from_str(ctx, str);
2236 ai = isl_access_info_add_source(ai, map, 1, &depth);
2238 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2239 map = isl_map_read_from_str(ctx, str);
2240 ai = isl_access_info_add_source(ai, map, 0, &depth);
2242 flow = isl_access_info_compute_flow(ai);
2243 dim = isl_space_alloc(ctx, 0, 3, 3);
2244 mm.must = isl_map_empty(isl_space_copy(dim));
2245 mm.may = isl_map_empty(dim);
2247 isl_flow_foreach(flow, collect_must_may, &mm);
2249 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2250 assert(map_is_equal(mm.must, str));
2251 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2252 assert(map_is_equal(mm.may, str));
2254 isl_map_free(mm.must);
2255 isl_map_free(mm.may);
2256 isl_flow_free(flow);
2259 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2260 map = isl_map_read_from_str(ctx, str);
2261 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2263 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2264 map = isl_map_read_from_str(ctx, str);
2265 ai = isl_access_info_add_source(ai, map, 0, &depth);
2267 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2268 map = isl_map_read_from_str(ctx, str);
2269 ai = isl_access_info_add_source(ai, map, 0, &depth);
2271 flow = isl_access_info_compute_flow(ai);
2272 dim = isl_space_alloc(ctx, 0, 3, 3);
2273 mm.must = isl_map_empty(isl_space_copy(dim));
2274 mm.may = isl_map_empty(dim);
2276 isl_flow_foreach(flow, collect_must_may, &mm);
2278 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2279 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2280 assert(map_is_equal(mm.may, str));
2281 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2282 assert(map_is_equal(mm.must, str));
2284 isl_map_free(mm.must);
2285 isl_map_free(mm.may);
2286 isl_flow_free(flow);
2289 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2290 map = isl_map_read_from_str(ctx, str);
2291 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2293 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2294 map = isl_map_read_from_str(ctx, str);
2295 ai = isl_access_info_add_source(ai, map, 0, &depth);
2297 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2298 map = isl_map_read_from_str(ctx, str);
2299 ai = isl_access_info_add_source(ai, map, 0, &depth);
2301 flow = isl_access_info_compute_flow(ai);
2302 dim = isl_space_alloc(ctx, 0, 3, 3);
2303 mm.must = isl_map_empty(isl_space_copy(dim));
2304 mm.may = isl_map_empty(dim);
2306 isl_flow_foreach(flow, collect_must_may, &mm);
2308 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2309 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2310 assert(map_is_equal(mm.may, str));
2311 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2312 assert(map_is_equal(mm.must, str));
2314 isl_map_free(mm.must);
2315 isl_map_free(mm.may);
2316 isl_flow_free(flow);
2319 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2320 map = isl_map_read_from_str(ctx, str);
2321 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2323 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2324 map = isl_map_read_from_str(ctx, str);
2325 ai = isl_access_info_add_source(ai, map, 0, &depth);
2327 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2328 map = isl_map_read_from_str(ctx, str);
2329 ai = isl_access_info_add_source(ai, map, 0, &depth);
2331 flow = isl_access_info_compute_flow(ai);
2332 dim = isl_space_alloc(ctx, 0, 3, 3);
2333 mm.must = isl_map_empty(isl_space_copy(dim));
2334 mm.may = isl_map_empty(dim);
2336 isl_flow_foreach(flow, collect_must_may, &mm);
2338 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2339 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2340 assert(map_is_equal(mm.may, str));
2341 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2342 assert(map_is_equal(mm.must, str));
2344 isl_map_free(mm.must);
2345 isl_map_free(mm.may);
2346 isl_flow_free(flow);
2349 depth = 5;
2351 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2352 map = isl_map_read_from_str(ctx, str);
2353 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2355 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2356 map = isl_map_read_from_str(ctx, str);
2357 ai = isl_access_info_add_source(ai, map, 1, &depth);
2359 flow = isl_access_info_compute_flow(ai);
2360 dim = isl_space_alloc(ctx, 0, 5, 5);
2361 mm.must = isl_map_empty(isl_space_copy(dim));
2362 mm.may = isl_map_empty(dim);
2364 isl_flow_foreach(flow, collect_must_may, &mm);
2366 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2367 assert(map_is_equal(mm.must, str));
2368 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2369 assert(map_is_equal(mm.may, str));
2371 isl_map_free(mm.must);
2372 isl_map_free(mm.may);
2373 isl_flow_free(flow);
2375 return 0;
2378 /* Check that the dependence analysis proceeds without errors.
2379 * Earlier versions of isl would break down during the analysis
2380 * due to the use of the wrong spaces.
2382 static int test_flow(isl_ctx *ctx)
2384 const char *str;
2385 isl_union_map *access, *schedule;
2386 isl_union_map *must_dep, *may_dep;
2387 int r;
2389 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2390 access = isl_union_map_read_from_str(ctx, str);
2391 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2392 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2393 "S2[] -> [1,0,0,0]; "
2394 "S3[] -> [-1,0,0,0] }";
2395 schedule = isl_union_map_read_from_str(ctx, str);
2396 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2397 isl_union_map_copy(access), schedule,
2398 &must_dep, &may_dep, NULL, NULL);
2399 isl_union_map_free(may_dep);
2400 isl_union_map_free(must_dep);
2402 return r;
2405 struct {
2406 const char *map;
2407 int sv;
2408 } sv_tests[] = {
2409 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2410 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2411 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2412 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2413 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2414 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2415 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2416 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2417 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2420 int test_sv(isl_ctx *ctx)
2422 isl_union_map *umap;
2423 int i;
2424 int sv;
2426 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2427 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2428 sv = isl_union_map_is_single_valued(umap);
2429 isl_union_map_free(umap);
2430 if (sv < 0)
2431 return -1;
2432 if (sv_tests[i].sv && !sv)
2433 isl_die(ctx, isl_error_internal,
2434 "map not detected as single valued", return -1);
2435 if (!sv_tests[i].sv && sv)
2436 isl_die(ctx, isl_error_internal,
2437 "map detected as single valued", return -1);
2440 return 0;
2443 struct {
2444 const char *str;
2445 int bijective;
2446 } bijective_tests[] = {
2447 { "[N,M]->{[i,j] -> [i]}", 0 },
2448 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2449 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2450 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2451 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2452 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2453 { "[N,M]->{[i,j] -> []}", 0 },
2454 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2455 { "[N,M]->{[i,j] -> [2i]}", 0 },
2456 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2457 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2458 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2459 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2462 static int test_bijective(struct isl_ctx *ctx)
2464 isl_map *map;
2465 int i;
2466 int bijective;
2468 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2469 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2470 bijective = isl_map_is_bijective(map);
2471 isl_map_free(map);
2472 if (bijective < 0)
2473 return -1;
2474 if (bijective_tests[i].bijective && !bijective)
2475 isl_die(ctx, isl_error_internal,
2476 "map not detected as bijective", return -1);
2477 if (!bijective_tests[i].bijective && bijective)
2478 isl_die(ctx, isl_error_internal,
2479 "map detected as bijective", return -1);
2482 return 0;
2485 /* Inputs for isl_pw_qpolynomial_gist tests.
2486 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2488 struct {
2489 const char *pwqp;
2490 const char *set;
2491 const char *gist;
2492 } pwqp_gist_tests[] = {
2493 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2494 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2495 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2496 "{ [i] -> -1/2 + 1/2 * i }" },
2497 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2500 static int test_pwqp(struct isl_ctx *ctx)
2502 int i;
2503 const char *str;
2504 isl_set *set;
2505 isl_pw_qpolynomial *pwqp1, *pwqp2;
2506 int equal;
2508 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2509 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2511 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2512 isl_dim_in, 1, 1);
2514 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2515 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2517 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2519 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2521 isl_pw_qpolynomial_free(pwqp1);
2523 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2524 str = pwqp_gist_tests[i].pwqp;
2525 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2526 str = pwqp_gist_tests[i].set;
2527 set = isl_set_read_from_str(ctx, str);
2528 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2529 str = pwqp_gist_tests[i].gist;
2530 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2531 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2532 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2533 isl_pw_qpolynomial_free(pwqp1);
2535 if (equal < 0)
2536 return -1;
2537 if (!equal)
2538 isl_die(ctx, isl_error_unknown,
2539 "unexpected result", return -1);
2542 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2543 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2544 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2545 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2547 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2549 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2551 isl_pw_qpolynomial_free(pwqp1);
2553 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2554 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2555 str = "{ [x] -> x }";
2556 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2558 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2560 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2562 isl_pw_qpolynomial_free(pwqp1);
2564 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2565 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2566 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2567 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2568 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2569 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2570 isl_pw_qpolynomial_free(pwqp1);
2572 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2573 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2574 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2575 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2576 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2577 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2578 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2579 isl_pw_qpolynomial_free(pwqp1);
2580 isl_pw_qpolynomial_free(pwqp2);
2581 if (equal < 0)
2582 return -1;
2583 if (!equal)
2584 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2586 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2587 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2588 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2589 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2590 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2591 isl_val_one(ctx));
2592 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2593 isl_pw_qpolynomial_free(pwqp1);
2594 isl_pw_qpolynomial_free(pwqp2);
2595 if (equal < 0)
2596 return -1;
2597 if (!equal)
2598 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2600 return 0;
2603 static int test_split_periods(isl_ctx *ctx)
2605 const char *str;
2606 isl_pw_qpolynomial *pwqp;
2608 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2609 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2610 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2611 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2613 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2615 isl_pw_qpolynomial_free(pwqp);
2617 if (!pwqp)
2618 return -1;
2620 return 0;
2623 static int test_union(isl_ctx *ctx)
2625 const char *str;
2626 isl_union_set *uset1, *uset2;
2627 isl_union_map *umap1, *umap2;
2628 int equal;
2630 str = "{ [i] : 0 <= i <= 1 }";
2631 uset1 = isl_union_set_read_from_str(ctx, str);
2632 str = "{ [1] -> [0] }";
2633 umap1 = isl_union_map_read_from_str(ctx, str);
2635 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2636 equal = isl_union_map_is_equal(umap1, umap2);
2638 isl_union_map_free(umap1);
2639 isl_union_map_free(umap2);
2641 if (equal < 0)
2642 return -1;
2643 if (!equal)
2644 isl_die(ctx, isl_error_unknown, "union maps not equal",
2645 return -1);
2647 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2648 umap1 = isl_union_map_read_from_str(ctx, str);
2649 str = "{ A[i]; B[i] }";
2650 uset1 = isl_union_set_read_from_str(ctx, str);
2652 uset2 = isl_union_map_domain(umap1);
2654 equal = isl_union_set_is_equal(uset1, uset2);
2656 isl_union_set_free(uset1);
2657 isl_union_set_free(uset2);
2659 if (equal < 0)
2660 return -1;
2661 if (!equal)
2662 isl_die(ctx, isl_error_unknown, "union sets not equal",
2663 return -1);
2665 return 0;
2668 /* Check that computing a bound of a non-zero polynomial over an unbounded
2669 * domain does not produce a rational value.
2670 * In particular, check that the upper bound is infinity.
2672 static int test_bound_unbounded_domain(isl_ctx *ctx)
2674 const char *str;
2675 isl_pw_qpolynomial *pwqp;
2676 isl_pw_qpolynomial_fold *pwf, *pwf2;
2677 isl_bool equal;
2679 str = "{ [m,n] -> -m * n }";
2680 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2681 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2682 str = "{ infty }";
2683 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2684 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2685 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2686 isl_pw_qpolynomial_fold_free(pwf);
2687 isl_pw_qpolynomial_fold_free(pwf2);
2689 if (equal < 0)
2690 return -1;
2691 if (!equal)
2692 isl_die(ctx, isl_error_unknown,
2693 "expecting infinite polynomial bound", return -1);
2695 return 0;
2698 static int test_bound(isl_ctx *ctx)
2700 const char *str;
2701 unsigned dim;
2702 isl_pw_qpolynomial *pwqp;
2703 isl_pw_qpolynomial_fold *pwf;
2705 if (test_bound_unbounded_domain(ctx) < 0)
2706 return -1;
2708 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2709 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2710 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2711 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2712 isl_pw_qpolynomial_fold_free(pwf);
2713 if (dim != 4)
2714 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2715 return -1);
2717 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2718 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2719 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2720 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2721 isl_pw_qpolynomial_fold_free(pwf);
2722 if (dim != 1)
2723 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2724 return -1);
2726 return 0;
2729 static int test_lift(isl_ctx *ctx)
2731 const char *str;
2732 isl_basic_map *bmap;
2733 isl_basic_set *bset;
2735 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2736 bset = isl_basic_set_read_from_str(ctx, str);
2737 bset = isl_basic_set_lift(bset);
2738 bmap = isl_basic_map_from_range(bset);
2739 bset = isl_basic_map_domain(bmap);
2740 isl_basic_set_free(bset);
2742 return 0;
2745 struct {
2746 const char *set1;
2747 const char *set2;
2748 int subset;
2749 } subset_tests[] = {
2750 { "{ [112, 0] }",
2751 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2752 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2753 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2754 { "{ [65] }",
2755 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2756 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2757 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2758 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2759 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2760 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2761 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2762 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2763 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2764 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2765 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2766 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2767 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2768 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2769 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2770 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2771 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2772 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2773 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2774 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2775 "4e0 <= -57 + i0 + i1)) or "
2776 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2777 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2778 "4e0 >= -61 + i0 + i1)) or "
2779 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2782 static int test_subset(isl_ctx *ctx)
2784 int i;
2785 isl_set *set1, *set2;
2786 int subset;
2788 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2789 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2790 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2791 subset = isl_set_is_subset(set1, set2);
2792 isl_set_free(set1);
2793 isl_set_free(set2);
2794 if (subset < 0)
2795 return -1;
2796 if (subset != subset_tests[i].subset)
2797 isl_die(ctx, isl_error_unknown,
2798 "incorrect subset result", return -1);
2801 return 0;
2804 struct {
2805 const char *minuend;
2806 const char *subtrahend;
2807 const char *difference;
2808 } subtract_domain_tests[] = {
2809 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2810 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2811 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2814 static int test_subtract(isl_ctx *ctx)
2816 int i;
2817 isl_union_map *umap1, *umap2;
2818 isl_union_pw_multi_aff *upma1, *upma2;
2819 isl_union_set *uset;
2820 int equal;
2822 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2823 umap1 = isl_union_map_read_from_str(ctx,
2824 subtract_domain_tests[i].minuend);
2825 uset = isl_union_set_read_from_str(ctx,
2826 subtract_domain_tests[i].subtrahend);
2827 umap2 = isl_union_map_read_from_str(ctx,
2828 subtract_domain_tests[i].difference);
2829 umap1 = isl_union_map_subtract_domain(umap1, uset);
2830 equal = isl_union_map_is_equal(umap1, umap2);
2831 isl_union_map_free(umap1);
2832 isl_union_map_free(umap2);
2833 if (equal < 0)
2834 return -1;
2835 if (!equal)
2836 isl_die(ctx, isl_error_unknown,
2837 "incorrect subtract domain result", return -1);
2840 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2841 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2842 subtract_domain_tests[i].minuend);
2843 uset = isl_union_set_read_from_str(ctx,
2844 subtract_domain_tests[i].subtrahend);
2845 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2846 subtract_domain_tests[i].difference);
2847 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2848 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2849 isl_union_pw_multi_aff_free(upma1);
2850 isl_union_pw_multi_aff_free(upma2);
2851 if (equal < 0)
2852 return -1;
2853 if (!equal)
2854 isl_die(ctx, isl_error_unknown,
2855 "incorrect subtract domain result", return -1);
2858 return 0;
2861 /* Check that intersecting the empty basic set with another basic set
2862 * does not increase the number of constraints. In particular,
2863 * the empty basic set should maintain its canonical representation.
2865 static int test_intersect(isl_ctx *ctx)
2867 int n1, n2;
2868 isl_basic_set *bset1, *bset2;
2870 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
2871 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
2872 n1 = isl_basic_set_n_constraint(bset1);
2873 bset1 = isl_basic_set_intersect(bset1, bset2);
2874 n2 = isl_basic_set_n_constraint(bset1);
2875 isl_basic_set_free(bset1);
2876 if (!bset1)
2877 return -1;
2878 if (n1 != n2)
2879 isl_die(ctx, isl_error_unknown,
2880 "number of constraints of empty set changed",
2881 return -1);
2883 return 0;
2886 int test_factorize(isl_ctx *ctx)
2888 const char *str;
2889 isl_basic_set *bset;
2890 isl_factorizer *f;
2892 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2893 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2894 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2895 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2896 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2897 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2898 "3i5 >= -2i0 - i2 + 3i4 }";
2899 bset = isl_basic_set_read_from_str(ctx, str);
2900 f = isl_basic_set_factorizer(bset);
2901 isl_basic_set_free(bset);
2902 isl_factorizer_free(f);
2903 if (!f)
2904 isl_die(ctx, isl_error_unknown,
2905 "failed to construct factorizer", return -1);
2907 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2908 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2909 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2910 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2911 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2912 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2913 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2914 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2915 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2916 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2917 bset = isl_basic_set_read_from_str(ctx, str);
2918 f = isl_basic_set_factorizer(bset);
2919 isl_basic_set_free(bset);
2920 isl_factorizer_free(f);
2921 if (!f)
2922 isl_die(ctx, isl_error_unknown,
2923 "failed to construct factorizer", return -1);
2925 return 0;
2928 static isl_stat check_injective(__isl_take isl_map *map, void *user)
2930 int *injective = user;
2932 *injective = isl_map_is_injective(map);
2933 isl_map_free(map);
2935 if (*injective < 0 || !*injective)
2936 return isl_stat_error;
2938 return isl_stat_ok;
2941 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2942 const char *r, const char *s, int tilable, int parallel)
2944 int i;
2945 isl_union_set *D;
2946 isl_union_map *W, *R, *S;
2947 isl_union_map *empty;
2948 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2949 isl_union_map *validity, *proximity, *coincidence;
2950 isl_union_map *schedule;
2951 isl_union_map *test;
2952 isl_union_set *delta;
2953 isl_union_set *domain;
2954 isl_set *delta_set;
2955 isl_set *slice;
2956 isl_set *origin;
2957 isl_schedule_constraints *sc;
2958 isl_schedule *sched;
2959 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2961 D = isl_union_set_read_from_str(ctx, d);
2962 W = isl_union_map_read_from_str(ctx, w);
2963 R = isl_union_map_read_from_str(ctx, r);
2964 S = isl_union_map_read_from_str(ctx, s);
2966 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2967 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2969 empty = isl_union_map_empty(isl_union_map_get_space(S));
2970 isl_union_map_compute_flow(isl_union_map_copy(R),
2971 isl_union_map_copy(W), empty,
2972 isl_union_map_copy(S),
2973 &dep_raw, NULL, NULL, NULL);
2974 isl_union_map_compute_flow(isl_union_map_copy(W),
2975 isl_union_map_copy(W),
2976 isl_union_map_copy(R),
2977 isl_union_map_copy(S),
2978 &dep_waw, &dep_war, NULL, NULL);
2980 dep = isl_union_map_union(dep_waw, dep_war);
2981 dep = isl_union_map_union(dep, dep_raw);
2982 validity = isl_union_map_copy(dep);
2983 coincidence = isl_union_map_copy(dep);
2984 proximity = isl_union_map_copy(dep);
2986 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2987 sc = isl_schedule_constraints_set_validity(sc, validity);
2988 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2989 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2990 sched = isl_schedule_constraints_compute_schedule(sc);
2991 schedule = isl_schedule_get_map(sched);
2992 isl_schedule_free(sched);
2993 isl_union_map_free(W);
2994 isl_union_map_free(R);
2995 isl_union_map_free(S);
2997 is_injection = 1;
2998 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3000 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3001 is_complete = isl_union_set_is_subset(D, domain);
3002 isl_union_set_free(D);
3003 isl_union_set_free(domain);
3005 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3006 test = isl_union_map_apply_range(test, dep);
3007 test = isl_union_map_apply_range(test, schedule);
3009 delta = isl_union_map_deltas(test);
3010 if (isl_union_set_n_set(delta) == 0) {
3011 is_tilable = 1;
3012 is_parallel = 1;
3013 is_nonneg = 1;
3014 isl_union_set_free(delta);
3015 } else {
3016 delta_set = isl_set_from_union_set(delta);
3018 slice = isl_set_universe(isl_set_get_space(delta_set));
3019 for (i = 0; i < tilable; ++i)
3020 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3021 is_tilable = isl_set_is_subset(delta_set, slice);
3022 isl_set_free(slice);
3024 slice = isl_set_universe(isl_set_get_space(delta_set));
3025 for (i = 0; i < parallel; ++i)
3026 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3027 is_parallel = isl_set_is_subset(delta_set, slice);
3028 isl_set_free(slice);
3030 origin = isl_set_universe(isl_set_get_space(delta_set));
3031 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3032 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3034 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3035 delta_set = isl_set_lexmin(delta_set);
3037 is_nonneg = isl_set_is_equal(delta_set, origin);
3039 isl_set_free(origin);
3040 isl_set_free(delta_set);
3043 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3044 is_injection < 0 || is_complete < 0)
3045 return -1;
3046 if (!is_complete)
3047 isl_die(ctx, isl_error_unknown,
3048 "generated schedule incomplete", return -1);
3049 if (!is_injection)
3050 isl_die(ctx, isl_error_unknown,
3051 "generated schedule not injective on each statement",
3052 return -1);
3053 if (!is_nonneg)
3054 isl_die(ctx, isl_error_unknown,
3055 "negative dependences in generated schedule",
3056 return -1);
3057 if (!is_tilable)
3058 isl_die(ctx, isl_error_unknown,
3059 "generated schedule not as tilable as expected",
3060 return -1);
3061 if (!is_parallel)
3062 isl_die(ctx, isl_error_unknown,
3063 "generated schedule not as parallel as expected",
3064 return -1);
3066 return 0;
3069 /* Compute a schedule for the given instance set, validity constraints,
3070 * proximity constraints and context and return a corresponding union map
3071 * representation.
3073 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3074 const char *domain, const char *validity, const char *proximity,
3075 const char *context)
3077 isl_set *con;
3078 isl_union_set *dom;
3079 isl_union_map *dep;
3080 isl_union_map *prox;
3081 isl_schedule_constraints *sc;
3082 isl_schedule *schedule;
3083 isl_union_map *sched;
3085 con = isl_set_read_from_str(ctx, context);
3086 dom = isl_union_set_read_from_str(ctx, domain);
3087 dep = isl_union_map_read_from_str(ctx, validity);
3088 prox = isl_union_map_read_from_str(ctx, proximity);
3089 sc = isl_schedule_constraints_on_domain(dom);
3090 sc = isl_schedule_constraints_set_context(sc, con);
3091 sc = isl_schedule_constraints_set_validity(sc, dep);
3092 sc = isl_schedule_constraints_set_proximity(sc, prox);
3093 schedule = isl_schedule_constraints_compute_schedule(sc);
3094 sched = isl_schedule_get_map(schedule);
3095 isl_schedule_free(schedule);
3097 return sched;
3100 /* Compute a schedule for the given instance set, validity constraints and
3101 * proximity constraints and return a corresponding union map representation.
3103 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3104 const char *domain, const char *validity, const char *proximity)
3106 return compute_schedule_with_context(ctx, domain, validity, proximity,
3107 "{ : }");
3110 /* Check that a schedule can be constructed on the given domain
3111 * with the given validity and proximity constraints.
3113 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3114 const char *validity, const char *proximity)
3116 isl_union_map *sched;
3118 sched = compute_schedule(ctx, domain, validity, proximity);
3119 if (!sched)
3120 return -1;
3122 isl_union_map_free(sched);
3123 return 0;
3126 int test_special_schedule(isl_ctx *ctx, const char *domain,
3127 const char *validity, const char *proximity, const char *expected_sched)
3129 isl_union_map *sched1, *sched2;
3130 int equal;
3132 sched1 = compute_schedule(ctx, domain, validity, proximity);
3133 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3135 equal = isl_union_map_is_equal(sched1, sched2);
3136 isl_union_map_free(sched1);
3137 isl_union_map_free(sched2);
3139 if (equal < 0)
3140 return -1;
3141 if (!equal)
3142 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3143 return -1);
3145 return 0;
3148 /* Check that the schedule map is properly padded, even after being
3149 * reconstructed from the band forest.
3151 static int test_padded_schedule(isl_ctx *ctx)
3153 const char *str;
3154 isl_union_set *D;
3155 isl_union_map *validity, *proximity;
3156 isl_schedule_constraints *sc;
3157 isl_schedule *sched;
3158 isl_union_map *map1, *map2;
3159 isl_band_list *list;
3160 int equal;
3162 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3163 D = isl_union_set_read_from_str(ctx, str);
3164 validity = isl_union_map_empty(isl_union_set_get_space(D));
3165 proximity = isl_union_map_copy(validity);
3166 sc = isl_schedule_constraints_on_domain(D);
3167 sc = isl_schedule_constraints_set_validity(sc, validity);
3168 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3169 sched = isl_schedule_constraints_compute_schedule(sc);
3170 map1 = isl_schedule_get_map(sched);
3171 list = isl_schedule_get_band_forest(sched);
3172 isl_band_list_free(list);
3173 map2 = isl_schedule_get_map(sched);
3174 isl_schedule_free(sched);
3175 equal = isl_union_map_is_equal(map1, map2);
3176 isl_union_map_free(map1);
3177 isl_union_map_free(map2);
3179 if (equal < 0)
3180 return -1;
3181 if (!equal)
3182 isl_die(ctx, isl_error_unknown,
3183 "reconstructed schedule map not the same as original",
3184 return -1);
3186 return 0;
3189 /* Check that conditional validity constraints are also taken into
3190 * account across bands.
3191 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3192 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3193 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3194 * is enforced by the rest of the schedule.
3196 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3198 const char *str;
3199 isl_union_set *domain;
3200 isl_union_map *validity, *proximity, *condition;
3201 isl_union_map *sink, *source, *dep;
3202 isl_schedule_constraints *sc;
3203 isl_schedule *schedule;
3204 isl_union_access_info *access;
3205 isl_union_flow *flow;
3206 int empty;
3208 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3209 "A[k] : k >= 1 and k <= -1 + n; "
3210 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3211 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3212 domain = isl_union_set_read_from_str(ctx, str);
3213 sc = isl_schedule_constraints_on_domain(domain);
3214 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3215 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3216 "D[k, i] -> C[1 + k, i] : "
3217 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3218 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3219 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3220 validity = isl_union_map_read_from_str(ctx, str);
3221 sc = isl_schedule_constraints_set_validity(sc, validity);
3222 str = "[n] -> { C[k, i] -> D[k, i] : "
3223 "0 <= i <= -1 + k and k <= -1 + n }";
3224 proximity = isl_union_map_read_from_str(ctx, str);
3225 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3226 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3227 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3228 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3229 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3230 condition = isl_union_map_read_from_str(ctx, str);
3231 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3232 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3233 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3234 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3235 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3236 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3237 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3238 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3239 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3240 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3241 validity = isl_union_map_read_from_str(ctx, str);
3242 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3243 validity);
3244 schedule = isl_schedule_constraints_compute_schedule(sc);
3245 str = "{ D[2,0] -> [] }";
3246 sink = isl_union_map_read_from_str(ctx, str);
3247 access = isl_union_access_info_from_sink(sink);
3248 str = "{ C[2,1] -> [] }";
3249 source = isl_union_map_read_from_str(ctx, str);
3250 access = isl_union_access_info_set_must_source(access, source);
3251 access = isl_union_access_info_set_schedule(access, schedule);
3252 flow = isl_union_access_info_compute_flow(access);
3253 dep = isl_union_flow_get_must_dependence(flow);
3254 isl_union_flow_free(flow);
3255 empty = isl_union_map_is_empty(dep);
3256 isl_union_map_free(dep);
3258 if (empty < 0)
3259 return -1;
3260 if (empty)
3261 isl_die(ctx, isl_error_unknown,
3262 "conditional validity not respected", return -1);
3264 return 0;
3267 /* Input for testing of schedule construction based on
3268 * conditional constraints.
3270 * domain is the iteration domain
3271 * flow are the flow dependences, which determine the validity and
3272 * proximity constraints
3273 * condition are the conditions on the conditional validity constraints
3274 * conditional_validity are the conditional validity constraints
3275 * outer_band_n is the expected number of members in the outer band
3277 struct {
3278 const char *domain;
3279 const char *flow;
3280 const char *condition;
3281 const char *conditional_validity;
3282 int outer_band_n;
3283 } live_range_tests[] = {
3284 /* Contrived example that illustrates that we need to keep
3285 * track of tagged condition dependences and
3286 * tagged conditional validity dependences
3287 * in isl_sched_edge separately.
3288 * In particular, the conditional validity constraints on A
3289 * cannot be satisfied,
3290 * but they can be ignored because there are no corresponding
3291 * condition constraints. However, we do have an additional
3292 * conditional validity constraint that maps to the same
3293 * dependence relation
3294 * as the condition constraint on B. If we did not make a distinction
3295 * between tagged condition and tagged conditional validity
3296 * dependences, then we
3297 * could end up treating this shared dependence as an condition
3298 * constraint on A, forcing a localization of the conditions,
3299 * which is impossible.
3301 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3302 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3303 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3304 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3305 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3306 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3309 /* TACO 2013 Fig. 7 */
3310 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3311 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3312 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3313 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3314 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3315 "0 <= i < n and 0 <= j < n - 1 }",
3316 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3317 "0 <= i < n and 0 <= j < j' < n;"
3318 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3319 "0 <= i < i' < n and 0 <= j,j' < n;"
3320 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3321 "0 <= i,j,j' < n and j < j' }",
3324 /* TACO 2013 Fig. 7, without tags */
3325 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3326 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3327 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3328 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3329 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3330 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3331 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3332 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3335 /* TACO 2013 Fig. 12 */
3336 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3337 "S3[i,3] : 0 <= i <= 1 }",
3338 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3339 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3340 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3341 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3342 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3343 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3344 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3345 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3346 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3347 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3348 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3353 /* Test schedule construction based on conditional constraints.
3354 * In particular, check the number of members in the outer band node
3355 * as an indication of whether tiling is possible or not.
3357 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3359 int i;
3360 isl_union_set *domain;
3361 isl_union_map *condition;
3362 isl_union_map *flow;
3363 isl_union_map *validity;
3364 isl_schedule_constraints *sc;
3365 isl_schedule *schedule;
3366 isl_schedule_node *node;
3367 int n_member;
3369 if (test_special_conditional_schedule_constraints(ctx) < 0)
3370 return -1;
3372 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3373 domain = isl_union_set_read_from_str(ctx,
3374 live_range_tests[i].domain);
3375 flow = isl_union_map_read_from_str(ctx,
3376 live_range_tests[i].flow);
3377 condition = isl_union_map_read_from_str(ctx,
3378 live_range_tests[i].condition);
3379 validity = isl_union_map_read_from_str(ctx,
3380 live_range_tests[i].conditional_validity);
3381 sc = isl_schedule_constraints_on_domain(domain);
3382 sc = isl_schedule_constraints_set_validity(sc,
3383 isl_union_map_copy(flow));
3384 sc = isl_schedule_constraints_set_proximity(sc, flow);
3385 sc = isl_schedule_constraints_set_conditional_validity(sc,
3386 condition, validity);
3387 schedule = isl_schedule_constraints_compute_schedule(sc);
3388 node = isl_schedule_get_root(schedule);
3389 while (node &&
3390 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3391 node = isl_schedule_node_first_child(node);
3392 n_member = isl_schedule_node_band_n_member(node);
3393 isl_schedule_node_free(node);
3394 isl_schedule_free(schedule);
3396 if (!schedule)
3397 return -1;
3398 if (n_member != live_range_tests[i].outer_band_n)
3399 isl_die(ctx, isl_error_unknown,
3400 "unexpected number of members in outer band",
3401 return -1);
3403 return 0;
3406 /* Check that the schedule computed for the given instance set and
3407 * dependence relation strongly satisfies the dependences.
3408 * In particular, check that no instance is scheduled before
3409 * or together with an instance on which it depends.
3410 * Earlier versions of isl would produce a schedule that
3411 * only weakly satisfies the dependences.
3413 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3415 const char *domain, *dep;
3416 isl_union_map *D, *schedule;
3417 isl_map *map, *ge;
3418 int empty;
3420 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3421 "A[i0] : 0 <= i0 <= 1 }";
3422 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3423 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3424 schedule = compute_schedule(ctx, domain, dep, dep);
3425 D = isl_union_map_read_from_str(ctx, dep);
3426 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3427 D = isl_union_map_apply_range(D, schedule);
3428 map = isl_map_from_union_map(D);
3429 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3430 map = isl_map_intersect(map, ge);
3431 empty = isl_map_is_empty(map);
3432 isl_map_free(map);
3434 if (empty < 0)
3435 return -1;
3436 if (!empty)
3437 isl_die(ctx, isl_error_unknown,
3438 "dependences not strongly satisfied", return -1);
3440 return 0;
3443 /* Compute a schedule for input where the instance set constraints
3444 * conflict with the context constraints.
3445 * Earlier versions of isl did not properly handle this situation.
3447 static int test_conflicting_context_schedule(isl_ctx *ctx)
3449 isl_union_map *schedule;
3450 const char *domain, *context;
3452 domain = "[n] -> { A[] : n >= 0 }";
3453 context = "[n] -> { : n < 0 }";
3454 schedule = compute_schedule_with_context(ctx,
3455 domain, "{}", "{}", context);
3456 isl_union_map_free(schedule);
3458 if (!schedule)
3459 return -1;
3461 return 0;
3464 /* Check that the dependence carrying step is not confused by
3465 * a bound on the coefficient size.
3466 * In particular, force the scheduler to move to a dependence carrying
3467 * step by demanding outer coincidence and bound the size of
3468 * the coefficients. Earlier versions of isl would take this
3469 * bound into account while carrying dependences, breaking
3470 * fundamental assumptions.
3472 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3474 const char *domain, *dep;
3475 isl_union_set *I;
3476 isl_union_map *D;
3477 isl_schedule_constraints *sc;
3478 isl_schedule *schedule;
3480 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3481 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3482 "i1 <= -2 + i0; "
3483 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3484 I = isl_union_set_read_from_str(ctx, domain);
3485 D = isl_union_map_read_from_str(ctx, dep);
3486 sc = isl_schedule_constraints_on_domain(I);
3487 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3488 sc = isl_schedule_constraints_set_coincidence(sc, D);
3489 isl_options_set_schedule_outer_coincidence(ctx, 1);
3490 isl_options_set_schedule_max_coefficient(ctx, 20);
3491 schedule = isl_schedule_constraints_compute_schedule(sc);
3492 isl_options_set_schedule_max_coefficient(ctx, -1);
3493 isl_options_set_schedule_outer_coincidence(ctx, 0);
3494 isl_schedule_free(schedule);
3496 if (!schedule)
3497 return -1;
3499 return 0;
3502 int test_schedule(isl_ctx *ctx)
3504 const char *D, *W, *R, *V, *P, *S;
3506 /* Handle resulting schedule with zero bands. */
3507 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3508 return -1;
3510 /* Jacobi */
3511 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3512 W = "{ S1[t,i] -> a[t,i] }";
3513 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3514 "S1[t,i] -> a[t-1,i+1] }";
3515 S = "{ S1[t,i] -> [t,i] }";
3516 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3517 return -1;
3519 /* Fig. 5 of CC2008 */
3520 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3521 "j <= -1 + N }";
3522 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3523 "j >= 2 and j <= -1 + N }";
3524 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3525 "j >= 2 and j <= -1 + N; "
3526 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3527 "j >= 2 and j <= -1 + N }";
3528 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3529 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3530 return -1;
3532 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3533 W = "{ S1[i] -> a[i] }";
3534 R = "{ S2[i] -> a[i+1] }";
3535 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3536 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3537 return -1;
3539 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3540 W = "{ S1[i] -> a[i] }";
3541 R = "{ S2[i] -> a[9-i] }";
3542 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3543 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3544 return -1;
3546 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3547 W = "{ S1[i] -> a[i] }";
3548 R = "[N] -> { S2[i] -> a[N-1-i] }";
3549 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3550 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3551 return -1;
3553 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3554 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3555 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3556 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3557 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3558 return -1;
3560 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3561 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3562 R = "{ S2[i,j] -> a[i-1,j] }";
3563 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3564 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3565 return -1;
3567 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3568 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3569 R = "{ S2[i,j] -> a[i,j-1] }";
3570 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3571 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3572 return -1;
3574 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3575 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3576 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3577 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3578 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3579 "S_0[] -> [0, 0, 0] }";
3580 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3581 return -1;
3582 ctx->opt->schedule_parametric = 0;
3583 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3584 return -1;
3585 ctx->opt->schedule_parametric = 1;
3587 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3588 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3589 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3590 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3591 "S4[i] -> a[i,N] }";
3592 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3593 "S4[i] -> [4,i,0] }";
3594 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3595 return -1;
3597 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3598 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3599 "j <= N }";
3600 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3601 "j <= N; "
3602 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3603 "j <= N }";
3604 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3605 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3606 return -1;
3608 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3609 " S_2[t] : t >= 0 and t <= -1 + N; "
3610 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3611 "i <= -1 + N }";
3612 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3613 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3614 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3615 "i >= 0 and i <= -1 + N }";
3616 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3617 "i >= 0 and i <= -1 + N; "
3618 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3619 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3620 " S_0[t] -> [0, t, 0] }";
3622 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3623 return -1;
3624 ctx->opt->schedule_parametric = 0;
3625 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3626 return -1;
3627 ctx->opt->schedule_parametric = 1;
3629 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3630 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3631 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3632 return -1;
3634 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3635 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3636 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3637 "j >= 0 and j <= -1 + N; "
3638 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3639 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3640 "j >= 0 and j <= -1 + N; "
3641 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3642 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3643 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3644 return -1;
3646 D = "{ S_0[i] : i >= 0 }";
3647 W = "{ S_0[i] -> a[i] : i >= 0 }";
3648 R = "{ S_0[i] -> a[0] : i >= 0 }";
3649 S = "{ S_0[i] -> [0, i, 0] }";
3650 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3651 return -1;
3653 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3654 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3655 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3656 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3657 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3658 return -1;
3660 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3661 "k <= -1 + n and k >= 0 }";
3662 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3663 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3664 "k <= -1 + n and k >= 0; "
3665 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3666 "k <= -1 + n and k >= 0; "
3667 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3668 "k <= -1 + n and k >= 0 }";
3669 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3670 ctx->opt->schedule_outer_coincidence = 1;
3671 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3672 return -1;
3673 ctx->opt->schedule_outer_coincidence = 0;
3675 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3676 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3677 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3678 "Stmt_for_body24[i0, i1, 1, 0]:"
3679 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3680 "Stmt_for_body7[i0, i1, i2]:"
3681 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3682 "i2 <= 7 }";
3684 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3685 "Stmt_for_body24[1, i1, i2, i3]:"
3686 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3687 "i2 >= 1;"
3688 "Stmt_for_body24[0, i1, i2, i3] -> "
3689 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3690 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3691 "i3 >= 0;"
3692 "Stmt_for_body24[0, i1, i2, i3] ->"
3693 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3694 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3695 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3696 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3697 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3698 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3699 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3700 "i1 <= 6 and i1 >= 0;"
3701 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3702 "Stmt_for_body7[i0, i1, i2] -> "
3703 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3704 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3705 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3706 "Stmt_for_body7[i0, i1, i2] -> "
3707 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3708 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3709 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3710 P = V;
3711 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3712 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3713 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3715 if (test_special_schedule(ctx, D, V, P, S) < 0)
3716 return -1;
3718 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3719 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3720 "j >= 1 and j <= 7;"
3721 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3722 "j >= 1 and j <= 8 }";
3723 P = "{ }";
3724 S = "{ S_0[i, j] -> [i + j, j] }";
3725 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3726 if (test_special_schedule(ctx, D, V, P, S) < 0)
3727 return -1;
3728 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3730 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3731 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3732 "j >= 0 and j <= -1 + i }";
3733 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3734 "i <= -1 + N and j >= 0;"
3735 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3736 "i <= -2 + N }";
3737 P = "{ }";
3738 S = "{ S_0[i, j] -> [i, j] }";
3739 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3740 if (test_special_schedule(ctx, D, V, P, S) < 0)
3741 return -1;
3742 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3744 /* Test both algorithms on a case with only proximity dependences. */
3745 D = "{ S[i,j] : 0 <= i <= 10 }";
3746 V = "{ }";
3747 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3748 S = "{ S[i, j] -> [j, i] }";
3749 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3750 if (test_special_schedule(ctx, D, V, P, S) < 0)
3751 return -1;
3752 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3753 if (test_special_schedule(ctx, D, V, P, S) < 0)
3754 return -1;
3756 D = "{ A[a]; B[] }";
3757 V = "{}";
3758 P = "{ A[a] -> B[] }";
3759 if (test_has_schedule(ctx, D, V, P) < 0)
3760 return -1;
3762 if (test_padded_schedule(ctx) < 0)
3763 return -1;
3765 /* Check that check for progress is not confused by rational
3766 * solution.
3768 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3769 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3770 "i0 <= -2 + N; "
3771 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3772 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3773 P = "{}";
3774 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3775 if (test_has_schedule(ctx, D, V, P) < 0)
3776 return -1;
3777 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3779 /* Check that we allow schedule rows that are only non-trivial
3780 * on some full-dimensional domains.
3782 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3783 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3784 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3785 P = "{}";
3786 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3787 if (test_has_schedule(ctx, D, V, P) < 0)
3788 return -1;
3789 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3791 if (test_conditional_schedule_constraints(ctx) < 0)
3792 return -1;
3794 if (test_strongly_satisfying_schedule(ctx) < 0)
3795 return -1;
3797 if (test_conflicting_context_schedule(ctx) < 0)
3798 return -1;
3800 if (test_bounded_coefficients_schedule(ctx) < 0)
3801 return -1;
3803 return 0;
3806 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3808 isl_union_map *umap;
3809 int test;
3811 umap = isl_union_map_read_from_str(ctx, str);
3812 test = isl_union_map_plain_is_injective(umap);
3813 isl_union_map_free(umap);
3814 if (test < 0)
3815 return -1;
3816 if (test == injective)
3817 return 0;
3818 if (injective)
3819 isl_die(ctx, isl_error_unknown,
3820 "map not detected as injective", return -1);
3821 else
3822 isl_die(ctx, isl_error_unknown,
3823 "map detected as injective", return -1);
3826 int test_injective(isl_ctx *ctx)
3828 const char *str;
3830 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3831 return -1;
3832 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3833 return -1;
3834 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3835 return -1;
3836 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3837 return -1;
3838 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3839 return -1;
3840 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3841 return -1;
3842 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3843 return -1;
3844 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3845 return -1;
3847 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3848 if (test_plain_injective(ctx, str, 1))
3849 return -1;
3850 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3851 if (test_plain_injective(ctx, str, 0))
3852 return -1;
3854 return 0;
3857 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3859 isl_aff *aff2;
3860 int equal;
3862 if (!aff)
3863 return -1;
3865 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3866 equal = isl_aff_plain_is_equal(aff, aff2);
3867 isl_aff_free(aff2);
3869 return equal;
3872 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3874 int equal;
3876 equal = aff_plain_is_equal(aff, str);
3877 if (equal < 0)
3878 return -1;
3879 if (!equal)
3880 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3881 "result not as expected", return -1);
3882 return 0;
3885 struct {
3886 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3887 __isl_take isl_aff *aff2);
3888 } aff_bin_op[] = {
3889 ['+'] = { &isl_aff_add },
3890 ['-'] = { &isl_aff_sub },
3891 ['*'] = { &isl_aff_mul },
3892 ['/'] = { &isl_aff_div },
3895 struct {
3896 const char *arg1;
3897 unsigned char op;
3898 const char *arg2;
3899 const char *res;
3900 } aff_bin_tests[] = {
3901 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3902 "{ [i] -> [2i] }" },
3903 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3904 "{ [i] -> [0] }" },
3905 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3906 "{ [i] -> [2i] }" },
3907 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3908 "{ [i] -> [2i] }" },
3909 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3910 "{ [i] -> [i/2] }" },
3911 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3912 "{ [i] -> [i] }" },
3913 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3914 "{ [i] -> [NaN] }" },
3915 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3916 "{ [i] -> [NaN] }" },
3917 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3918 "{ [i] -> [NaN] }" },
3919 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3920 "{ [i] -> [NaN] }" },
3921 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3922 "{ [i] -> [NaN] }" },
3923 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3924 "{ [i] -> [NaN] }" },
3925 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3926 "{ [i] -> [NaN] }" },
3927 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3928 "{ [i] -> [NaN] }" },
3929 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3930 "{ [i] -> [NaN] }" },
3931 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3932 "{ [i] -> [NaN] }" },
3933 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3934 "{ [i] -> [NaN] }" },
3935 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3936 "{ [i] -> [NaN] }" },
3939 /* Perform some basic tests of binary operations on isl_aff objects.
3941 static int test_bin_aff(isl_ctx *ctx)
3943 int i;
3944 isl_aff *aff1, *aff2, *res;
3945 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3946 __isl_take isl_aff *aff2);
3947 int ok;
3949 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3950 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3951 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3952 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3953 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3954 aff1 = fn(aff1, aff2);
3955 if (isl_aff_is_nan(res))
3956 ok = isl_aff_is_nan(aff1);
3957 else
3958 ok = isl_aff_plain_is_equal(aff1, res);
3959 isl_aff_free(aff1);
3960 isl_aff_free(res);
3961 if (ok < 0)
3962 return -1;
3963 if (!ok)
3964 isl_die(ctx, isl_error_unknown,
3965 "unexpected result", return -1);
3968 return 0;
3971 struct {
3972 __isl_give isl_union_pw_multi_aff *(*fn)(
3973 __isl_take isl_union_pw_multi_aff *upma1,
3974 __isl_take isl_union_pw_multi_aff *upma2);
3975 const char *arg1;
3976 const char *arg2;
3977 const char *res;
3978 } upma_bin_tests[] = {
3979 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3980 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3981 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3982 "{ B[x] -> [2] : x >= 0 }",
3983 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3984 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
3985 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
3986 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
3987 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
3988 "D[i] -> B[2] : i >= 5 }" },
3989 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
3990 "{ B[x] -> C[2] : x > 0 }",
3991 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
3992 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
3993 "{ B[x] -> A[2] : x >= 0 }",
3994 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
3997 /* Perform some basic tests of binary operations on
3998 * isl_union_pw_multi_aff objects.
4000 static int test_bin_upma(isl_ctx *ctx)
4002 int i;
4003 isl_union_pw_multi_aff *upma1, *upma2, *res;
4004 int ok;
4006 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4007 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4008 upma_bin_tests[i].arg1);
4009 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4010 upma_bin_tests[i].arg2);
4011 res = isl_union_pw_multi_aff_read_from_str(ctx,
4012 upma_bin_tests[i].res);
4013 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4014 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4015 isl_union_pw_multi_aff_free(upma1);
4016 isl_union_pw_multi_aff_free(res);
4017 if (ok < 0)
4018 return -1;
4019 if (!ok)
4020 isl_die(ctx, isl_error_unknown,
4021 "unexpected result", return -1);
4024 return 0;
4027 struct {
4028 __isl_give isl_union_pw_multi_aff *(*fn)(
4029 __isl_take isl_union_pw_multi_aff *upma1,
4030 __isl_take isl_union_pw_multi_aff *upma2);
4031 const char *arg1;
4032 const char *arg2;
4033 } upma_bin_fail_tests[] = {
4034 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4035 "{ B[x] -> C[2] : x >= 0 }" },
4038 /* Perform some basic tests of binary operations on
4039 * isl_union_pw_multi_aff objects that are expected to fail.
4041 static int test_bin_upma_fail(isl_ctx *ctx)
4043 int i, n;
4044 isl_union_pw_multi_aff *upma1, *upma2;
4045 int on_error;
4047 on_error = isl_options_get_on_error(ctx);
4048 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4049 n = ARRAY_SIZE(upma_bin_fail_tests);
4050 for (i = 0; i < n; ++i) {
4051 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4052 upma_bin_fail_tests[i].arg1);
4053 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4054 upma_bin_fail_tests[i].arg2);
4055 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4056 isl_union_pw_multi_aff_free(upma1);
4057 if (upma1)
4058 break;
4060 isl_options_set_on_error(ctx, on_error);
4061 if (i < n)
4062 isl_die(ctx, isl_error_unknown,
4063 "operation not expected to succeed", return -1);
4065 return 0;
4068 int test_aff(isl_ctx *ctx)
4070 const char *str;
4071 isl_set *set;
4072 isl_space *space;
4073 isl_local_space *ls;
4074 isl_aff *aff;
4075 int zero, equal;
4077 if (test_bin_aff(ctx) < 0)
4078 return -1;
4079 if (test_bin_upma(ctx) < 0)
4080 return -1;
4081 if (test_bin_upma_fail(ctx) < 0)
4082 return -1;
4084 space = isl_space_set_alloc(ctx, 0, 1);
4085 ls = isl_local_space_from_space(space);
4086 aff = isl_aff_zero_on_domain(ls);
4088 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4089 aff = isl_aff_scale_down_ui(aff, 3);
4090 aff = isl_aff_floor(aff);
4091 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4092 aff = isl_aff_scale_down_ui(aff, 2);
4093 aff = isl_aff_floor(aff);
4094 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4096 str = "{ [10] }";
4097 set = isl_set_read_from_str(ctx, str);
4098 aff = isl_aff_gist(aff, set);
4100 aff = isl_aff_add_constant_si(aff, -16);
4101 zero = isl_aff_plain_is_zero(aff);
4102 isl_aff_free(aff);
4104 if (zero < 0)
4105 return -1;
4106 if (!zero)
4107 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4109 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4110 aff = isl_aff_scale_down_ui(aff, 64);
4111 aff = isl_aff_floor(aff);
4112 equal = aff_check_plain_equal(aff, "{ [-1] }");
4113 isl_aff_free(aff);
4114 if (equal < 0)
4115 return -1;
4117 return 0;
4120 int test_dim_max(isl_ctx *ctx)
4122 int equal;
4123 const char *str;
4124 isl_set *set1, *set2;
4125 isl_set *set;
4126 isl_map *map;
4127 isl_pw_aff *pwaff;
4129 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4130 set = isl_set_read_from_str(ctx, str);
4131 pwaff = isl_set_dim_max(set, 0);
4132 set1 = isl_set_from_pw_aff(pwaff);
4133 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4134 set2 = isl_set_read_from_str(ctx, str);
4135 equal = isl_set_is_equal(set1, set2);
4136 isl_set_free(set1);
4137 isl_set_free(set2);
4138 if (equal < 0)
4139 return -1;
4140 if (!equal)
4141 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4143 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4144 set = isl_set_read_from_str(ctx, str);
4145 pwaff = isl_set_dim_max(set, 0);
4146 set1 = isl_set_from_pw_aff(pwaff);
4147 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4148 set2 = isl_set_read_from_str(ctx, str);
4149 equal = isl_set_is_equal(set1, set2);
4150 isl_set_free(set1);
4151 isl_set_free(set2);
4152 if (equal < 0)
4153 return -1;
4154 if (!equal)
4155 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4157 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4158 set = isl_set_read_from_str(ctx, str);
4159 pwaff = isl_set_dim_max(set, 0);
4160 set1 = isl_set_from_pw_aff(pwaff);
4161 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
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);
4166 if (equal < 0)
4167 return -1;
4168 if (!equal)
4169 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4171 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4172 "0 <= i < N and 0 <= j < M }";
4173 map = isl_map_read_from_str(ctx, str);
4174 set = isl_map_range(map);
4176 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4177 set1 = isl_set_from_pw_aff(pwaff);
4178 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4179 set2 = isl_set_read_from_str(ctx, str);
4180 equal = isl_set_is_equal(set1, set2);
4181 isl_set_free(set1);
4182 isl_set_free(set2);
4184 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4185 set1 = isl_set_from_pw_aff(pwaff);
4186 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4187 set2 = isl_set_read_from_str(ctx, str);
4188 if (equal >= 0 && equal)
4189 equal = isl_set_is_equal(set1, set2);
4190 isl_set_free(set1);
4191 isl_set_free(set2);
4193 isl_set_free(set);
4195 if (equal < 0)
4196 return -1;
4197 if (!equal)
4198 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4200 /* Check that solutions are properly merged. */
4201 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4202 "c <= -1 + n - 4a - 2b and c >= -2b and "
4203 "4a >= -4 + n and c >= 0 }";
4204 set = isl_set_read_from_str(ctx, str);
4205 pwaff = isl_set_dim_min(set, 2);
4206 set1 = isl_set_from_pw_aff(pwaff);
4207 str = "[n] -> { [(0)] : n >= 1 }";
4208 set2 = isl_set_read_from_str(ctx, str);
4209 equal = isl_set_is_equal(set1, set2);
4210 isl_set_free(set1);
4211 isl_set_free(set2);
4213 if (equal < 0)
4214 return -1;
4215 if (!equal)
4216 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4218 /* Check that empty solution lie in the right space. */
4219 str = "[n] -> { [t,a] : 1 = 0 }";
4220 set = isl_set_read_from_str(ctx, str);
4221 pwaff = isl_set_dim_max(set, 0);
4222 set1 = isl_set_from_pw_aff(pwaff);
4223 str = "[n] -> { [t] : 1 = 0 }";
4224 set2 = isl_set_read_from_str(ctx, str);
4225 equal = isl_set_is_equal(set1, set2);
4226 isl_set_free(set1);
4227 isl_set_free(set2);
4229 if (equal < 0)
4230 return -1;
4231 if (!equal)
4232 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4234 return 0;
4237 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4239 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4240 const char *str)
4242 isl_ctx *ctx;
4243 isl_pw_multi_aff *pma2;
4244 int equal;
4246 if (!pma)
4247 return -1;
4249 ctx = isl_pw_multi_aff_get_ctx(pma);
4250 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4251 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4252 isl_pw_multi_aff_free(pma2);
4254 return equal;
4257 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4258 * represented by "str".
4260 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4261 const char *str)
4263 int equal;
4265 equal = pw_multi_aff_plain_is_equal(pma, str);
4266 if (equal < 0)
4267 return -1;
4268 if (!equal)
4269 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4270 "result not as expected", return -1);
4271 return 0;
4274 /* Basic test for isl_pw_multi_aff_product.
4276 * Check that multiple pieces are properly handled.
4278 static int test_product_pma(isl_ctx *ctx)
4280 int equal;
4281 const char *str;
4282 isl_pw_multi_aff *pma1, *pma2;
4284 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4285 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4286 str = "{ C[] -> D[] }";
4287 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4288 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4289 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4290 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4291 equal = pw_multi_aff_check_plain_equal(pma1, str);
4292 isl_pw_multi_aff_free(pma1);
4293 if (equal < 0)
4294 return -1;
4296 return 0;
4299 int test_product(isl_ctx *ctx)
4301 const char *str;
4302 isl_set *set;
4303 isl_union_set *uset1, *uset2;
4304 int ok;
4306 str = "{ A[i] }";
4307 set = isl_set_read_from_str(ctx, str);
4308 set = isl_set_product(set, isl_set_copy(set));
4309 ok = isl_set_is_wrapping(set);
4310 isl_set_free(set);
4311 if (ok < 0)
4312 return -1;
4313 if (!ok)
4314 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4316 str = "{ [] }";
4317 uset1 = isl_union_set_read_from_str(ctx, str);
4318 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4319 str = "{ [[] -> []] }";
4320 uset2 = isl_union_set_read_from_str(ctx, str);
4321 ok = isl_union_set_is_equal(uset1, uset2);
4322 isl_union_set_free(uset1);
4323 isl_union_set_free(uset2);
4324 if (ok < 0)
4325 return -1;
4326 if (!ok)
4327 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4329 if (test_product_pma(ctx) < 0)
4330 return -1;
4332 return 0;
4335 /* Check that two sets are not considered disjoint just because
4336 * they have a different set of (named) parameters.
4338 static int test_disjoint(isl_ctx *ctx)
4340 const char *str;
4341 isl_set *set, *set2;
4342 int disjoint;
4344 str = "[n] -> { [[]->[]] }";
4345 set = isl_set_read_from_str(ctx, str);
4346 str = "{ [[]->[]] }";
4347 set2 = isl_set_read_from_str(ctx, str);
4348 disjoint = isl_set_is_disjoint(set, set2);
4349 isl_set_free(set);
4350 isl_set_free(set2);
4351 if (disjoint < 0)
4352 return -1;
4353 if (disjoint)
4354 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4356 return 0;
4359 int test_equal(isl_ctx *ctx)
4361 const char *str;
4362 isl_set *set, *set2;
4363 int equal;
4365 str = "{ S_6[i] }";
4366 set = isl_set_read_from_str(ctx, str);
4367 str = "{ S_7[i] }";
4368 set2 = isl_set_read_from_str(ctx, str);
4369 equal = isl_set_is_equal(set, set2);
4370 isl_set_free(set);
4371 isl_set_free(set2);
4372 if (equal < 0)
4373 return -1;
4374 if (equal)
4375 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4377 return 0;
4380 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4381 enum isl_dim_type type, unsigned pos, int fixed)
4383 int test;
4385 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4386 isl_map_free(map);
4387 if (test < 0)
4388 return -1;
4389 if (test == fixed)
4390 return 0;
4391 if (fixed)
4392 isl_die(ctx, isl_error_unknown,
4393 "map not detected as fixed", return -1);
4394 else
4395 isl_die(ctx, isl_error_unknown,
4396 "map detected as fixed", return -1);
4399 int test_fixed(isl_ctx *ctx)
4401 const char *str;
4402 isl_map *map;
4404 str = "{ [i] -> [i] }";
4405 map = isl_map_read_from_str(ctx, str);
4406 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4407 return -1;
4408 str = "{ [i] -> [1] }";
4409 map = isl_map_read_from_str(ctx, str);
4410 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4411 return -1;
4412 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4413 map = isl_map_read_from_str(ctx, str);
4414 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4415 return -1;
4416 map = isl_map_read_from_str(ctx, str);
4417 map = isl_map_neg(map);
4418 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4419 return -1;
4421 return 0;
4424 struct isl_vertices_test_data {
4425 const char *set;
4426 int n;
4427 const char *vertex[2];
4428 } vertices_tests[] = {
4429 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4430 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4431 { "{ A[t, i] : t = 14 and i = 1 }",
4432 1, { "{ A[14, 1] }" } },
4435 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4437 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4439 struct isl_vertices_test_data *data = user;
4440 isl_ctx *ctx;
4441 isl_multi_aff *ma;
4442 isl_basic_set *bset;
4443 isl_pw_multi_aff *pma;
4444 int i;
4445 isl_bool equal;
4447 ctx = isl_vertex_get_ctx(vertex);
4448 bset = isl_vertex_get_domain(vertex);
4449 ma = isl_vertex_get_expr(vertex);
4450 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4452 for (i = 0; i < data->n; ++i) {
4453 isl_pw_multi_aff *pma_i;
4455 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4456 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4457 isl_pw_multi_aff_free(pma_i);
4459 if (equal < 0 || equal)
4460 break;
4463 isl_pw_multi_aff_free(pma);
4464 isl_vertex_free(vertex);
4466 if (equal < 0)
4467 return isl_stat_error;
4469 return equal ? isl_stat_ok : isl_stat_error;
4472 int test_vertices(isl_ctx *ctx)
4474 int i;
4476 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4477 isl_basic_set *bset;
4478 isl_vertices *vertices;
4479 int ok = 1;
4480 int n;
4482 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4483 vertices = isl_basic_set_compute_vertices(bset);
4484 n = isl_vertices_get_n_vertices(vertices);
4485 if (vertices_tests[i].n != n)
4486 ok = 0;
4487 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4488 &vertices_tests[i]) < 0)
4489 ok = 0;
4490 isl_vertices_free(vertices);
4491 isl_basic_set_free(bset);
4493 if (!vertices)
4494 return -1;
4495 if (!ok)
4496 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4497 return -1);
4500 return 0;
4503 int test_union_pw(isl_ctx *ctx)
4505 int equal;
4506 const char *str;
4507 isl_union_set *uset;
4508 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4510 str = "{ [x] -> x^2 }";
4511 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4512 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4513 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4514 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4515 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4516 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4517 isl_union_pw_qpolynomial_free(upwqp1);
4518 isl_union_pw_qpolynomial_free(upwqp2);
4519 if (equal < 0)
4520 return -1;
4521 if (!equal)
4522 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4524 return 0;
4527 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4528 * defined over the correct domain space.
4530 static int test_eval(isl_ctx *ctx)
4532 const char *str;
4533 isl_point *pnt;
4534 isl_set *set;
4535 isl_union_pw_qpolynomial *upwqp;
4536 isl_val *v;
4537 int cmp;
4539 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4540 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4541 str = "{ A[6] }";
4542 set = isl_set_read_from_str(ctx, str);
4543 pnt = isl_set_sample_point(set);
4544 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4545 cmp = isl_val_cmp_si(v, 36);
4546 isl_val_free(v);
4548 if (!v)
4549 return -1;
4550 if (cmp != 0)
4551 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4553 return 0;
4556 int test_output(isl_ctx *ctx)
4558 char *s;
4559 const char *str;
4560 isl_pw_aff *pa;
4561 isl_printer *p;
4562 int equal;
4564 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4565 pa = isl_pw_aff_read_from_str(ctx, str);
4567 p = isl_printer_to_str(ctx);
4568 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4569 p = isl_printer_print_pw_aff(p, pa);
4570 s = isl_printer_get_str(p);
4571 isl_printer_free(p);
4572 isl_pw_aff_free(pa);
4573 if (!s)
4574 equal = -1;
4575 else
4576 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4577 free(s);
4578 if (equal < 0)
4579 return -1;
4580 if (!equal)
4581 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4583 return 0;
4586 int test_sample(isl_ctx *ctx)
4588 const char *str;
4589 isl_basic_set *bset1, *bset2;
4590 int empty, subset;
4592 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4593 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4594 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4595 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4596 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4597 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4598 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4599 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4600 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4601 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4602 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4603 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4604 "d - 1073741821e and "
4605 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4606 "3j >= 1 - a + b + 2e and "
4607 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4608 "3i <= 4 - a + 4b - e and "
4609 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4610 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4611 "c <= -1 + a and 3i >= -2 - a + 3e and "
4612 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4613 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4614 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4615 "1073741823e >= 1 + 1073741823b - d and "
4616 "3i >= 1073741823b + c - 1073741823e - f and "
4617 "3i >= 1 + 2b + e + 3g }";
4618 bset1 = isl_basic_set_read_from_str(ctx, str);
4619 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4620 empty = isl_basic_set_is_empty(bset2);
4621 subset = isl_basic_set_is_subset(bset2, bset1);
4622 isl_basic_set_free(bset1);
4623 isl_basic_set_free(bset2);
4624 if (empty < 0 || subset < 0)
4625 return -1;
4626 if (empty)
4627 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4628 if (!subset)
4629 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4631 return 0;
4634 int test_fixed_power(isl_ctx *ctx)
4636 const char *str;
4637 isl_map *map;
4638 isl_int exp;
4639 int equal;
4641 isl_int_init(exp);
4642 str = "{ [i] -> [i + 1] }";
4643 map = isl_map_read_from_str(ctx, str);
4644 isl_int_set_si(exp, 23);
4645 map = isl_map_fixed_power(map, exp);
4646 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4647 isl_int_clear(exp);
4648 isl_map_free(map);
4649 if (equal < 0)
4650 return -1;
4652 return 0;
4655 int test_slice(isl_ctx *ctx)
4657 const char *str;
4658 isl_map *map;
4659 int equal;
4661 str = "{ [i] -> [j] }";
4662 map = isl_map_read_from_str(ctx, str);
4663 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4664 equal = map_check_equal(map, "{ [i] -> [i] }");
4665 isl_map_free(map);
4666 if (equal < 0)
4667 return -1;
4669 str = "{ [i] -> [j] }";
4670 map = isl_map_read_from_str(ctx, str);
4671 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4672 equal = map_check_equal(map, "{ [i] -> [j] }");
4673 isl_map_free(map);
4674 if (equal < 0)
4675 return -1;
4677 str = "{ [i] -> [j] }";
4678 map = isl_map_read_from_str(ctx, str);
4679 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4680 equal = map_check_equal(map, "{ [i] -> [-i] }");
4681 isl_map_free(map);
4682 if (equal < 0)
4683 return -1;
4685 str = "{ [i] -> [j] }";
4686 map = isl_map_read_from_str(ctx, str);
4687 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4688 equal = map_check_equal(map, "{ [0] -> [j] }");
4689 isl_map_free(map);
4690 if (equal < 0)
4691 return -1;
4693 str = "{ [i] -> [j] }";
4694 map = isl_map_read_from_str(ctx, str);
4695 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4696 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4697 isl_map_free(map);
4698 if (equal < 0)
4699 return -1;
4701 str = "{ [i] -> [j] }";
4702 map = isl_map_read_from_str(ctx, str);
4703 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4704 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4705 isl_map_free(map);
4706 if (equal < 0)
4707 return -1;
4709 return 0;
4712 int test_eliminate(isl_ctx *ctx)
4714 const char *str;
4715 isl_map *map;
4716 int equal;
4718 str = "{ [i] -> [j] : i = 2j }";
4719 map = isl_map_read_from_str(ctx, str);
4720 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4721 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4722 isl_map_free(map);
4723 if (equal < 0)
4724 return -1;
4726 return 0;
4729 /* Check that isl_set_dim_residue_class detects that the values of j
4730 * in the set below are all odd and that it does not detect any spurious
4731 * strides.
4733 static int test_residue_class(isl_ctx *ctx)
4735 const char *str;
4736 isl_set *set;
4737 isl_int m, r;
4738 int res;
4740 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4741 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4742 set = isl_set_read_from_str(ctx, str);
4743 isl_int_init(m);
4744 isl_int_init(r);
4745 res = isl_set_dim_residue_class(set, 1, &m, &r);
4746 if (res >= 0 &&
4747 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4748 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4749 res = -1);
4750 isl_int_clear(r);
4751 isl_int_clear(m);
4752 isl_set_free(set);
4754 return res;
4757 int test_align_parameters(isl_ctx *ctx)
4759 const char *str;
4760 isl_space *space;
4761 isl_multi_aff *ma1, *ma2;
4762 int equal;
4764 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4765 ma1 = isl_multi_aff_read_from_str(ctx, str);
4767 space = isl_space_params_alloc(ctx, 1);
4768 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4769 ma1 = isl_multi_aff_align_params(ma1, space);
4771 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4772 ma2 = isl_multi_aff_read_from_str(ctx, str);
4774 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4776 isl_multi_aff_free(ma1);
4777 isl_multi_aff_free(ma2);
4779 if (equal < 0)
4780 return -1;
4781 if (!equal)
4782 isl_die(ctx, isl_error_unknown,
4783 "result not as expected", return -1);
4785 return 0;
4788 static int test_list(isl_ctx *ctx)
4790 isl_id *a, *b, *c, *d, *id;
4791 isl_id_list *list;
4792 int ok;
4794 a = isl_id_alloc(ctx, "a", NULL);
4795 b = isl_id_alloc(ctx, "b", NULL);
4796 c = isl_id_alloc(ctx, "c", NULL);
4797 d = isl_id_alloc(ctx, "d", NULL);
4799 list = isl_id_list_alloc(ctx, 4);
4800 list = isl_id_list_add(list, a);
4801 list = isl_id_list_add(list, b);
4802 list = isl_id_list_add(list, c);
4803 list = isl_id_list_add(list, d);
4804 list = isl_id_list_drop(list, 1, 1);
4806 if (isl_id_list_n_id(list) != 3) {
4807 isl_id_list_free(list);
4808 isl_die(ctx, isl_error_unknown,
4809 "unexpected number of elements in list", return -1);
4812 id = isl_id_list_get_id(list, 0);
4813 ok = id == a;
4814 isl_id_free(id);
4815 id = isl_id_list_get_id(list, 1);
4816 ok = ok && id == c;
4817 isl_id_free(id);
4818 id = isl_id_list_get_id(list, 2);
4819 ok = ok && id == d;
4820 isl_id_free(id);
4822 isl_id_list_free(list);
4824 if (!ok)
4825 isl_die(ctx, isl_error_unknown,
4826 "unexpected elements in list", return -1);
4828 return 0;
4831 const char *set_conversion_tests[] = {
4832 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4833 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4834 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4835 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4836 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4839 /* Check that converting from isl_set to isl_pw_multi_aff and back
4840 * to isl_set produces the original isl_set.
4842 static int test_set_conversion(isl_ctx *ctx)
4844 int i;
4845 const char *str;
4846 isl_set *set1, *set2;
4847 isl_pw_multi_aff *pma;
4848 int equal;
4850 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4851 str = set_conversion_tests[i];
4852 set1 = isl_set_read_from_str(ctx, str);
4853 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4854 set2 = isl_set_from_pw_multi_aff(pma);
4855 equal = isl_set_is_equal(set1, set2);
4856 isl_set_free(set1);
4857 isl_set_free(set2);
4859 if (equal < 0)
4860 return -1;
4861 if (!equal)
4862 isl_die(ctx, isl_error_unknown, "bad conversion",
4863 return -1);
4866 return 0;
4869 /* Check that converting from isl_map to isl_pw_multi_aff and back
4870 * to isl_map produces the original isl_map.
4872 static int test_map_conversion(isl_ctx *ctx)
4874 const char *str;
4875 isl_map *map1, *map2;
4876 isl_pw_multi_aff *pma;
4877 int equal;
4879 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4880 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4881 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4882 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4883 "9e <= -2 - 2a) }";
4884 map1 = isl_map_read_from_str(ctx, str);
4885 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4886 map2 = isl_map_from_pw_multi_aff(pma);
4887 equal = isl_map_is_equal(map1, map2);
4888 isl_map_free(map1);
4889 isl_map_free(map2);
4891 if (equal < 0)
4892 return -1;
4893 if (!equal)
4894 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4896 return 0;
4899 static int test_conversion(isl_ctx *ctx)
4901 if (test_set_conversion(ctx) < 0)
4902 return -1;
4903 if (test_map_conversion(ctx) < 0)
4904 return -1;
4905 return 0;
4908 /* Check that isl_basic_map_curry does not modify input.
4910 static int test_curry(isl_ctx *ctx)
4912 const char *str;
4913 isl_basic_map *bmap1, *bmap2;
4914 int equal;
4916 str = "{ [A[] -> B[]] -> C[] }";
4917 bmap1 = isl_basic_map_read_from_str(ctx, str);
4918 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4919 equal = isl_basic_map_is_equal(bmap1, bmap2);
4920 isl_basic_map_free(bmap1);
4921 isl_basic_map_free(bmap2);
4923 if (equal < 0)
4924 return -1;
4925 if (equal)
4926 isl_die(ctx, isl_error_unknown,
4927 "curried map should not be equal to original",
4928 return -1);
4930 return 0;
4933 struct {
4934 const char *set;
4935 const char *ma;
4936 const char *res;
4937 } preimage_tests[] = {
4938 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4939 "{ A[j,i] -> B[i,j] }",
4940 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4941 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4942 "{ A[a,b] -> B[a/2,b/6] }",
4943 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4944 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4945 "{ A[a,b] -> B[a/2,b/6] }",
4946 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4947 "exists i,j : a = 2 i and b = 6 j }" },
4948 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4949 "[n] -> { : 0 <= n <= 100 }" },
4950 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4951 "{ A[a] -> B[2a] }",
4952 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4953 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4954 "{ A[a] -> B[([a/2])] }",
4955 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4956 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4957 "{ A[a] -> B[a,a,a/3] }",
4958 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4959 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4960 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4963 static int test_preimage_basic_set(isl_ctx *ctx)
4965 int i;
4966 isl_basic_set *bset1, *bset2;
4967 isl_multi_aff *ma;
4968 int equal;
4970 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4971 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4972 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4973 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4974 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4975 equal = isl_basic_set_is_equal(bset1, bset2);
4976 isl_basic_set_free(bset1);
4977 isl_basic_set_free(bset2);
4978 if (equal < 0)
4979 return -1;
4980 if (!equal)
4981 isl_die(ctx, isl_error_unknown, "bad preimage",
4982 return -1);
4985 return 0;
4988 struct {
4989 const char *map;
4990 const char *ma;
4991 const char *res;
4992 } preimage_domain_tests[] = {
4993 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4994 "{ A[j,i] -> B[i,j] }",
4995 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4996 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4997 "{ A[i] -> B[i + 1] }",
4998 "{ A[i] -> C[i + 1] }" },
4999 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5000 "{ A[i] -> B[i + 1] }",
5001 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5002 { "{ B[i] -> C[([i/2])] }",
5003 "{ A[i] -> B[2i] }",
5004 "{ A[i] -> C[i] }" },
5005 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5006 "{ A[i] -> B[([i/5]), ([i/7])] }",
5007 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5008 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5009 "[N] -> { A[] -> B[([N/5])] }",
5010 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5011 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5012 "{ A[i] -> B[2i] }",
5013 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5014 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5015 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5016 "{ A[i] -> B[2i] }",
5017 "{ A[i] -> C[2i] }" },
5020 static int test_preimage_union_map(isl_ctx *ctx)
5022 int i;
5023 isl_union_map *umap1, *umap2;
5024 isl_multi_aff *ma;
5025 int equal;
5027 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5028 umap1 = isl_union_map_read_from_str(ctx,
5029 preimage_domain_tests[i].map);
5030 ma = isl_multi_aff_read_from_str(ctx,
5031 preimage_domain_tests[i].ma);
5032 umap2 = isl_union_map_read_from_str(ctx,
5033 preimage_domain_tests[i].res);
5034 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5035 equal = isl_union_map_is_equal(umap1, umap2);
5036 isl_union_map_free(umap1);
5037 isl_union_map_free(umap2);
5038 if (equal < 0)
5039 return -1;
5040 if (!equal)
5041 isl_die(ctx, isl_error_unknown, "bad preimage",
5042 return -1);
5045 return 0;
5048 static int test_preimage(isl_ctx *ctx)
5050 if (test_preimage_basic_set(ctx) < 0)
5051 return -1;
5052 if (test_preimage_union_map(ctx) < 0)
5053 return -1;
5055 return 0;
5058 struct {
5059 const char *ma1;
5060 const char *ma;
5061 const char *res;
5062 } pullback_tests[] = {
5063 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5064 "{ A[a,b] -> C[b + 2a] }" },
5065 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5066 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5067 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5068 "{ A[a] -> C[(a)/6] }" },
5069 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5070 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5071 "{ A[a] -> C[(2a)/3] }" },
5072 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5073 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5074 "{ A[i,j] -> C[i + j, i + j] }"},
5075 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5076 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5077 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5078 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5079 "{ [i, j] -> [floor((i)/3), j] }",
5080 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5083 static int test_pullback(isl_ctx *ctx)
5085 int i;
5086 isl_multi_aff *ma1, *ma2;
5087 isl_multi_aff *ma;
5088 int equal;
5090 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5091 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5092 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5093 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5094 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5095 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5096 isl_multi_aff_free(ma1);
5097 isl_multi_aff_free(ma2);
5098 if (equal < 0)
5099 return -1;
5100 if (!equal)
5101 isl_die(ctx, isl_error_unknown, "bad pullback",
5102 return -1);
5105 return 0;
5108 /* Check that negation is printed correctly and that equal expressions
5109 * are correctly identified.
5111 static int test_ast(isl_ctx *ctx)
5113 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5114 char *str;
5115 int ok, equal;
5117 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5118 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5119 expr = isl_ast_expr_add(expr1, expr2);
5120 expr2 = isl_ast_expr_copy(expr);
5121 expr = isl_ast_expr_neg(expr);
5122 expr2 = isl_ast_expr_neg(expr2);
5123 equal = isl_ast_expr_is_equal(expr, expr2);
5124 str = isl_ast_expr_to_str(expr);
5125 ok = str ? !strcmp(str, "-(A + B)") : -1;
5126 free(str);
5127 isl_ast_expr_free(expr);
5128 isl_ast_expr_free(expr2);
5130 if (ok < 0 || equal < 0)
5131 return -1;
5132 if (!equal)
5133 isl_die(ctx, isl_error_unknown,
5134 "equal expressions not considered equal", return -1);
5135 if (!ok)
5136 isl_die(ctx, isl_error_unknown,
5137 "isl_ast_expr printed incorrectly", return -1);
5139 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5140 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5141 expr = isl_ast_expr_add(expr1, expr2);
5142 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5143 expr = isl_ast_expr_sub(expr3, expr);
5144 str = isl_ast_expr_to_str(expr);
5145 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5146 free(str);
5147 isl_ast_expr_free(expr);
5149 if (ok < 0)
5150 return -1;
5151 if (!ok)
5152 isl_die(ctx, isl_error_unknown,
5153 "isl_ast_expr printed incorrectly", return -1);
5155 return 0;
5158 /* Check that isl_ast_build_expr_from_set returns a valid expression
5159 * for an empty set. Note that isl_ast_build_expr_from_set getting
5160 * called on an empty set probably indicates a bug in the caller.
5162 static int test_ast_build(isl_ctx *ctx)
5164 isl_set *set;
5165 isl_ast_build *build;
5166 isl_ast_expr *expr;
5168 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5169 build = isl_ast_build_from_context(set);
5171 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5172 expr = isl_ast_build_expr_from_set(build, set);
5174 isl_ast_expr_free(expr);
5175 isl_ast_build_free(build);
5177 if (!expr)
5178 return -1;
5180 return 0;
5183 /* Internal data structure for before_for and after_for callbacks.
5185 * depth is the current depth
5186 * before is the number of times before_for has been called
5187 * after is the number of times after_for has been called
5189 struct isl_test_codegen_data {
5190 int depth;
5191 int before;
5192 int after;
5195 /* This function is called before each for loop in the AST generated
5196 * from test_ast_gen1.
5198 * Increment the number of calls and the depth.
5199 * Check that the space returned by isl_ast_build_get_schedule_space
5200 * matches the target space of the schedule returned by
5201 * isl_ast_build_get_schedule.
5202 * Return an isl_id that is checked by the corresponding call
5203 * to after_for.
5205 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5206 void *user)
5208 struct isl_test_codegen_data *data = user;
5209 isl_ctx *ctx;
5210 isl_space *space;
5211 isl_union_map *schedule;
5212 isl_union_set *uset;
5213 isl_set *set;
5214 int empty;
5215 char name[] = "d0";
5217 ctx = isl_ast_build_get_ctx(build);
5219 if (data->before >= 3)
5220 isl_die(ctx, isl_error_unknown,
5221 "unexpected number of for nodes", return NULL);
5222 if (data->depth >= 2)
5223 isl_die(ctx, isl_error_unknown,
5224 "unexpected depth", return NULL);
5226 snprintf(name, sizeof(name), "d%d", data->depth);
5227 data->before++;
5228 data->depth++;
5230 schedule = isl_ast_build_get_schedule(build);
5231 uset = isl_union_map_range(schedule);
5232 if (!uset)
5233 return NULL;
5234 if (isl_union_set_n_set(uset) != 1) {
5235 isl_union_set_free(uset);
5236 isl_die(ctx, isl_error_unknown,
5237 "expecting single range space", return NULL);
5240 space = isl_ast_build_get_schedule_space(build);
5241 set = isl_union_set_extract_set(uset, space);
5242 isl_union_set_free(uset);
5243 empty = isl_set_is_empty(set);
5244 isl_set_free(set);
5246 if (empty < 0)
5247 return NULL;
5248 if (empty)
5249 isl_die(ctx, isl_error_unknown,
5250 "spaces don't match", return NULL);
5252 return isl_id_alloc(ctx, name, NULL);
5255 /* This function is called after each for loop in the AST generated
5256 * from test_ast_gen1.
5258 * Increment the number of calls and decrement the depth.
5259 * Check that the annotation attached to the node matches
5260 * the isl_id returned by the corresponding call to before_for.
5262 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5263 __isl_keep isl_ast_build *build, void *user)
5265 struct isl_test_codegen_data *data = user;
5266 isl_id *id;
5267 const char *name;
5268 int valid;
5270 data->after++;
5271 data->depth--;
5273 if (data->after > data->before)
5274 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5275 "mismatch in number of for nodes",
5276 return isl_ast_node_free(node));
5278 id = isl_ast_node_get_annotation(node);
5279 if (!id)
5280 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5281 "missing annotation", return isl_ast_node_free(node));
5283 name = isl_id_get_name(id);
5284 valid = name && atoi(name + 1) == data->depth;
5285 isl_id_free(id);
5287 if (!valid)
5288 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5289 "wrong annotation", return isl_ast_node_free(node));
5291 return node;
5294 /* Check that the before_each_for and after_each_for callbacks
5295 * are called for each for loop in the generated code,
5296 * that they are called in the right order and that the isl_id
5297 * returned from the before_each_for callback is attached to
5298 * the isl_ast_node passed to the corresponding after_each_for call.
5300 static int test_ast_gen1(isl_ctx *ctx)
5302 const char *str;
5303 isl_set *set;
5304 isl_union_map *schedule;
5305 isl_ast_build *build;
5306 isl_ast_node *tree;
5307 struct isl_test_codegen_data data;
5309 str = "[N] -> { : N >= 10 }";
5310 set = isl_set_read_from_str(ctx, str);
5311 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5312 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5313 schedule = isl_union_map_read_from_str(ctx, str);
5315 data.before = 0;
5316 data.after = 0;
5317 data.depth = 0;
5318 build = isl_ast_build_from_context(set);
5319 build = isl_ast_build_set_before_each_for(build,
5320 &before_for, &data);
5321 build = isl_ast_build_set_after_each_for(build,
5322 &after_for, &data);
5323 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5324 isl_ast_build_free(build);
5325 if (!tree)
5326 return -1;
5328 isl_ast_node_free(tree);
5330 if (data.before != 3 || data.after != 3)
5331 isl_die(ctx, isl_error_unknown,
5332 "unexpected number of for nodes", return -1);
5334 return 0;
5337 /* Check that the AST generator handles domains that are integrally disjoint
5338 * but not rationally disjoint.
5340 static int test_ast_gen2(isl_ctx *ctx)
5342 const char *str;
5343 isl_set *set;
5344 isl_union_map *schedule;
5345 isl_union_map *options;
5346 isl_ast_build *build;
5347 isl_ast_node *tree;
5349 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5350 schedule = isl_union_map_read_from_str(ctx, str);
5351 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5352 build = isl_ast_build_from_context(set);
5354 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5355 options = isl_union_map_read_from_str(ctx, str);
5356 build = isl_ast_build_set_options(build, options);
5357 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5358 isl_ast_build_free(build);
5359 if (!tree)
5360 return -1;
5361 isl_ast_node_free(tree);
5363 return 0;
5366 /* Increment *user on each call.
5368 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5369 __isl_keep isl_ast_build *build, void *user)
5371 int *n = user;
5373 (*n)++;
5375 return node;
5378 /* Test that unrolling tries to minimize the number of instances.
5379 * In particular, for the schedule given below, make sure it generates
5380 * 3 nodes (rather than 101).
5382 static int test_ast_gen3(isl_ctx *ctx)
5384 const char *str;
5385 isl_set *set;
5386 isl_union_map *schedule;
5387 isl_union_map *options;
5388 isl_ast_build *build;
5389 isl_ast_node *tree;
5390 int n_domain = 0;
5392 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5393 schedule = isl_union_map_read_from_str(ctx, str);
5394 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5396 str = "{ [i] -> unroll[0] }";
5397 options = isl_union_map_read_from_str(ctx, str);
5399 build = isl_ast_build_from_context(set);
5400 build = isl_ast_build_set_options(build, options);
5401 build = isl_ast_build_set_at_each_domain(build,
5402 &count_domains, &n_domain);
5403 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5404 isl_ast_build_free(build);
5405 if (!tree)
5406 return -1;
5408 isl_ast_node_free(tree);
5410 if (n_domain != 3)
5411 isl_die(ctx, isl_error_unknown,
5412 "unexpected number of for nodes", return -1);
5414 return 0;
5417 /* Check that if the ast_build_exploit_nested_bounds options is set,
5418 * we do not get an outer if node in the generated AST,
5419 * while we do get such an outer if node if the options is not set.
5421 static int test_ast_gen4(isl_ctx *ctx)
5423 const char *str;
5424 isl_set *set;
5425 isl_union_map *schedule;
5426 isl_ast_build *build;
5427 isl_ast_node *tree;
5428 enum isl_ast_node_type type;
5429 int enb;
5431 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5432 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5434 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5436 schedule = isl_union_map_read_from_str(ctx, str);
5437 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5438 build = isl_ast_build_from_context(set);
5439 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5440 isl_ast_build_free(build);
5441 if (!tree)
5442 return -1;
5444 type = isl_ast_node_get_type(tree);
5445 isl_ast_node_free(tree);
5447 if (type == isl_ast_node_if)
5448 isl_die(ctx, isl_error_unknown,
5449 "not expecting if node", return -1);
5451 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5453 schedule = isl_union_map_read_from_str(ctx, str);
5454 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5455 build = isl_ast_build_from_context(set);
5456 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5457 isl_ast_build_free(build);
5458 if (!tree)
5459 return -1;
5461 type = isl_ast_node_get_type(tree);
5462 isl_ast_node_free(tree);
5464 if (type != isl_ast_node_if)
5465 isl_die(ctx, isl_error_unknown,
5466 "expecting if node", return -1);
5468 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5470 return 0;
5473 /* This function is called for each leaf in the AST generated
5474 * from test_ast_gen5.
5476 * We finalize the AST generation by extending the outer schedule
5477 * with a zero-dimensional schedule. If this results in any for loops,
5478 * then this means that we did not pass along enough information
5479 * about the outer schedule to the inner AST generation.
5481 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5482 void *user)
5484 isl_union_map *schedule, *extra;
5485 isl_ast_node *tree;
5487 schedule = isl_ast_build_get_schedule(build);
5488 extra = isl_union_map_copy(schedule);
5489 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5490 schedule = isl_union_map_range_product(schedule, extra);
5491 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5492 isl_ast_build_free(build);
5494 if (!tree)
5495 return NULL;
5497 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5498 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5499 "code should not contain any for loop",
5500 return isl_ast_node_free(tree));
5502 return tree;
5505 /* Check that we do not lose any information when going back and
5506 * forth between internal and external schedule.
5508 * In particular, we create an AST where we unroll the only
5509 * non-constant dimension in the schedule. We therefore do
5510 * not expect any for loops in the AST. However, older versions
5511 * of isl would not pass along enough information about the outer
5512 * schedule when performing an inner code generation from a create_leaf
5513 * callback, resulting in the inner code generation producing a for loop.
5515 static int test_ast_gen5(isl_ctx *ctx)
5517 const char *str;
5518 isl_set *set;
5519 isl_union_map *schedule, *options;
5520 isl_ast_build *build;
5521 isl_ast_node *tree;
5523 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5524 schedule = isl_union_map_read_from_str(ctx, str);
5526 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5527 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5528 options = isl_union_map_read_from_str(ctx, str);
5530 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5531 build = isl_ast_build_from_context(set);
5532 build = isl_ast_build_set_options(build, options);
5533 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5534 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5535 isl_ast_build_free(build);
5536 isl_ast_node_free(tree);
5537 if (!tree)
5538 return -1;
5540 return 0;
5543 static int test_ast_gen(isl_ctx *ctx)
5545 if (test_ast_gen1(ctx) < 0)
5546 return -1;
5547 if (test_ast_gen2(ctx) < 0)
5548 return -1;
5549 if (test_ast_gen3(ctx) < 0)
5550 return -1;
5551 if (test_ast_gen4(ctx) < 0)
5552 return -1;
5553 if (test_ast_gen5(ctx) < 0)
5554 return -1;
5555 return 0;
5558 /* Check if dropping output dimensions from an isl_pw_multi_aff
5559 * works properly.
5561 static int test_pw_multi_aff(isl_ctx *ctx)
5563 const char *str;
5564 isl_pw_multi_aff *pma1, *pma2;
5565 int equal;
5567 str = "{ [i,j] -> [i+j, 4i-j] }";
5568 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5569 str = "{ [i,j] -> [4i-j] }";
5570 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5572 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5574 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5576 isl_pw_multi_aff_free(pma1);
5577 isl_pw_multi_aff_free(pma2);
5578 if (equal < 0)
5579 return -1;
5580 if (!equal)
5581 isl_die(ctx, isl_error_unknown,
5582 "expressions not equal", return -1);
5584 return 0;
5587 /* Check that we can properly parse multi piecewise affine expressions
5588 * where the piecewise affine expressions have different domains.
5590 static int test_multi_pw_aff(isl_ctx *ctx)
5592 const char *str;
5593 isl_set *dom, *dom2;
5594 isl_multi_pw_aff *mpa1, *mpa2;
5595 isl_pw_aff *pa;
5596 int equal;
5597 int equal_domain;
5599 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5600 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5601 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5602 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5603 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5604 str = "{ [i] -> [(i : i > 0), 2i] }";
5605 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5607 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5609 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5610 dom = isl_pw_aff_domain(pa);
5611 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5612 dom2 = isl_pw_aff_domain(pa);
5613 equal_domain = isl_set_is_equal(dom, dom2);
5615 isl_set_free(dom);
5616 isl_set_free(dom2);
5617 isl_multi_pw_aff_free(mpa1);
5618 isl_multi_pw_aff_free(mpa2);
5620 if (equal < 0)
5621 return -1;
5622 if (!equal)
5623 isl_die(ctx, isl_error_unknown,
5624 "expressions not equal", return -1);
5626 if (equal_domain < 0)
5627 return -1;
5628 if (equal_domain)
5629 isl_die(ctx, isl_error_unknown,
5630 "domains unexpectedly equal", return -1);
5632 return 0;
5635 /* This is a regression test for a bug where isl_basic_map_simplify
5636 * would end up in an infinite loop. In particular, we construct
5637 * an empty basic set that is not obviously empty.
5638 * isl_basic_set_is_empty marks the basic set as empty.
5639 * After projecting out i3, the variable can be dropped completely,
5640 * but isl_basic_map_simplify refrains from doing so if the basic set
5641 * is empty and would end up in an infinite loop if it didn't test
5642 * explicitly for empty basic maps in the outer loop.
5644 static int test_simplify(isl_ctx *ctx)
5646 const char *str;
5647 isl_basic_set *bset;
5648 int empty;
5650 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5651 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5652 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5653 "i3 >= i2 }";
5654 bset = isl_basic_set_read_from_str(ctx, str);
5655 empty = isl_basic_set_is_empty(bset);
5656 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5657 isl_basic_set_free(bset);
5658 if (!bset)
5659 return -1;
5660 if (!empty)
5661 isl_die(ctx, isl_error_unknown,
5662 "basic set should be empty", return -1);
5664 return 0;
5667 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5668 * with gbr context would fail to disable the use of the shifted tableau
5669 * when transferring equalities for the input to the context, resulting
5670 * in invalid sample values.
5672 static int test_partial_lexmin(isl_ctx *ctx)
5674 const char *str;
5675 isl_basic_set *bset;
5676 isl_basic_map *bmap;
5677 isl_map *map;
5679 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5680 bmap = isl_basic_map_read_from_str(ctx, str);
5681 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5682 bset = isl_basic_set_read_from_str(ctx, str);
5683 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5684 isl_map_free(map);
5686 if (!map)
5687 return -1;
5689 return 0;
5692 /* Check that the variable compression performed on the existentially
5693 * quantified variables inside isl_basic_set_compute_divs is not confused
5694 * by the implicit equalities among the parameters.
5696 static int test_compute_divs(isl_ctx *ctx)
5698 const char *str;
5699 isl_basic_set *bset;
5700 isl_set *set;
5702 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5703 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5704 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5705 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5706 bset = isl_basic_set_read_from_str(ctx, str);
5707 set = isl_basic_set_compute_divs(bset);
5708 isl_set_free(set);
5709 if (!set)
5710 return -1;
5712 return 0;
5715 /* Check that the reaching domain elements and the prefix schedule
5716 * at a leaf node are the same before and after grouping.
5718 static int test_schedule_tree_group_1(isl_ctx *ctx)
5720 int equal;
5721 const char *str;
5722 isl_id *id;
5723 isl_union_set *uset;
5724 isl_multi_union_pw_aff *mupa;
5725 isl_union_pw_multi_aff *upma1, *upma2;
5726 isl_union_set *domain1, *domain2;
5727 isl_union_map *umap1, *umap2;
5728 isl_schedule_node *node;
5730 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5731 uset = isl_union_set_read_from_str(ctx, str);
5732 node = isl_schedule_node_from_domain(uset);
5733 node = isl_schedule_node_child(node, 0);
5734 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5735 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5736 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5737 node = isl_schedule_node_child(node, 0);
5738 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5739 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5740 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5741 node = isl_schedule_node_child(node, 0);
5742 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5743 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5744 domain1 = isl_schedule_node_get_domain(node);
5745 id = isl_id_alloc(ctx, "group", NULL);
5746 node = isl_schedule_node_parent(node);
5747 node = isl_schedule_node_group(node, id);
5748 node = isl_schedule_node_child(node, 0);
5749 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5750 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5751 domain2 = isl_schedule_node_get_domain(node);
5752 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5753 if (equal >= 0 && equal)
5754 equal = isl_union_set_is_equal(domain1, domain2);
5755 if (equal >= 0 && equal)
5756 equal = isl_union_map_is_equal(umap1, umap2);
5757 isl_union_map_free(umap1);
5758 isl_union_map_free(umap2);
5759 isl_union_set_free(domain1);
5760 isl_union_set_free(domain2);
5761 isl_union_pw_multi_aff_free(upma1);
5762 isl_union_pw_multi_aff_free(upma2);
5763 isl_schedule_node_free(node);
5765 if (equal < 0)
5766 return -1;
5767 if (!equal)
5768 isl_die(ctx, isl_error_unknown,
5769 "expressions not equal", return -1);
5771 return 0;
5774 /* Check that we can have nested groupings and that the union map
5775 * schedule representation is the same before and after the grouping.
5776 * Note that after the grouping, the union map representation contains
5777 * the domain constraints from the ranges of the expansion nodes,
5778 * while they are missing from the union map representation of
5779 * the tree without expansion nodes.
5781 * Also check that the global expansion is as expected.
5783 static int test_schedule_tree_group_2(isl_ctx *ctx)
5785 int equal, equal_expansion;
5786 const char *str;
5787 isl_id *id;
5788 isl_union_set *uset;
5789 isl_union_map *umap1, *umap2;
5790 isl_union_map *expansion1, *expansion2;
5791 isl_union_set_list *filters;
5792 isl_multi_union_pw_aff *mupa;
5793 isl_schedule *schedule;
5794 isl_schedule_node *node;
5796 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
5797 "S3[i,j] : 0 <= i,j < 10 }";
5798 uset = isl_union_set_read_from_str(ctx, str);
5799 node = isl_schedule_node_from_domain(uset);
5800 node = isl_schedule_node_child(node, 0);
5801 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
5802 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5803 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5804 node = isl_schedule_node_child(node, 0);
5805 str = "{ S1[i,j] }";
5806 uset = isl_union_set_read_from_str(ctx, str);
5807 filters = isl_union_set_list_from_union_set(uset);
5808 str = "{ S2[i,j]; S3[i,j] }";
5809 uset = isl_union_set_read_from_str(ctx, str);
5810 filters = isl_union_set_list_add(filters, uset);
5811 node = isl_schedule_node_insert_sequence(node, filters);
5812 node = isl_schedule_node_child(node, 1);
5813 node = isl_schedule_node_child(node, 0);
5814 str = "{ S2[i,j] }";
5815 uset = isl_union_set_read_from_str(ctx, str);
5816 filters = isl_union_set_list_from_union_set(uset);
5817 str = "{ S3[i,j] }";
5818 uset = isl_union_set_read_from_str(ctx, str);
5819 filters = isl_union_set_list_add(filters, uset);
5820 node = isl_schedule_node_insert_sequence(node, filters);
5822 schedule = isl_schedule_node_get_schedule(node);
5823 umap1 = isl_schedule_get_map(schedule);
5824 uset = isl_schedule_get_domain(schedule);
5825 umap1 = isl_union_map_intersect_domain(umap1, uset);
5826 isl_schedule_free(schedule);
5828 node = isl_schedule_node_parent(node);
5829 node = isl_schedule_node_parent(node);
5830 id = isl_id_alloc(ctx, "group1", NULL);
5831 node = isl_schedule_node_group(node, id);
5832 node = isl_schedule_node_child(node, 1);
5833 node = isl_schedule_node_child(node, 0);
5834 id = isl_id_alloc(ctx, "group2", NULL);
5835 node = isl_schedule_node_group(node, id);
5837 schedule = isl_schedule_node_get_schedule(node);
5838 umap2 = isl_schedule_get_map(schedule);
5839 isl_schedule_free(schedule);
5841 node = isl_schedule_node_root(node);
5842 node = isl_schedule_node_child(node, 0);
5843 expansion1 = isl_schedule_node_get_subtree_expansion(node);
5844 isl_schedule_node_free(node);
5846 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
5847 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
5848 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
5850 expansion2 = isl_union_map_read_from_str(ctx, str);
5852 equal = isl_union_map_is_equal(umap1, umap2);
5853 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
5855 isl_union_map_free(umap1);
5856 isl_union_map_free(umap2);
5857 isl_union_map_free(expansion1);
5858 isl_union_map_free(expansion2);
5860 if (equal < 0 || equal_expansion < 0)
5861 return -1;
5862 if (!equal)
5863 isl_die(ctx, isl_error_unknown,
5864 "expressions not equal", return -1);
5865 if (!equal_expansion)
5866 isl_die(ctx, isl_error_unknown,
5867 "unexpected expansion", return -1);
5869 return 0;
5872 /* Some tests for the isl_schedule_node_group function.
5874 static int test_schedule_tree_group(isl_ctx *ctx)
5876 if (test_schedule_tree_group_1(ctx) < 0)
5877 return -1;
5878 if (test_schedule_tree_group_2(ctx) < 0)
5879 return -1;
5880 return 0;
5883 struct {
5884 const char *set;
5885 const char *dual;
5886 } coef_tests[] = {
5887 { "{ rat: [i] : 0 <= i <= 10 }",
5888 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5889 { "{ rat: [i] : FALSE }",
5890 "{ rat: coefficients[[cst] -> [a]] }" },
5891 { "{ rat: [i] : }",
5892 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5895 struct {
5896 const char *set;
5897 const char *dual;
5898 } sol_tests[] = {
5899 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5900 "{ rat: [i] : 0 <= i <= 10 }" },
5901 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5902 "{ rat: [i] }" },
5903 { "{ rat: coefficients[[cst] -> [a]] }",
5904 "{ rat: [i] : FALSE }" },
5907 /* Test the basic functionality of isl_basic_set_coefficients and
5908 * isl_basic_set_solutions.
5910 static int test_dual(isl_ctx *ctx)
5912 int i;
5914 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5915 int equal;
5916 isl_basic_set *bset1, *bset2;
5918 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5919 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5920 bset1 = isl_basic_set_coefficients(bset1);
5921 equal = isl_basic_set_is_equal(bset1, bset2);
5922 isl_basic_set_free(bset1);
5923 isl_basic_set_free(bset2);
5924 if (equal < 0)
5925 return -1;
5926 if (!equal)
5927 isl_die(ctx, isl_error_unknown,
5928 "incorrect dual", return -1);
5931 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5932 int equal;
5933 isl_basic_set *bset1, *bset2;
5935 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5936 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5937 bset1 = isl_basic_set_solutions(bset1);
5938 equal = isl_basic_set_is_equal(bset1, bset2);
5939 isl_basic_set_free(bset1);
5940 isl_basic_set_free(bset2);
5941 if (equal < 0)
5942 return -1;
5943 if (!equal)
5944 isl_die(ctx, isl_error_unknown,
5945 "incorrect dual", return -1);
5948 return 0;
5951 struct {
5952 int scale_tile;
5953 int shift_point;
5954 const char *domain;
5955 const char *schedule;
5956 const char *sizes;
5957 const char *tile;
5958 const char *point;
5959 } tile_tests[] = {
5960 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5961 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5962 "{ [32,32] }",
5963 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5964 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5966 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5967 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5968 "{ [32,32] }",
5969 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5970 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5972 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5973 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5974 "{ [32,32] }",
5975 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5976 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5978 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5979 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5980 "{ [32,32] }",
5981 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5982 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5986 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
5987 * tile the band and then check if the tile and point bands have the
5988 * expected partial schedule.
5990 static int test_tile(isl_ctx *ctx)
5992 int i;
5993 int scale;
5994 int shift;
5996 scale = isl_options_get_tile_scale_tile_loops(ctx);
5997 shift = isl_options_get_tile_shift_point_loops(ctx);
5999 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6000 int opt;
6001 int equal;
6002 const char *str;
6003 isl_union_set *domain;
6004 isl_multi_union_pw_aff *mupa, *mupa2;
6005 isl_schedule_node *node;
6006 isl_multi_val *sizes;
6008 opt = tile_tests[i].scale_tile;
6009 isl_options_set_tile_scale_tile_loops(ctx, opt);
6010 opt = tile_tests[i].shift_point;
6011 isl_options_set_tile_shift_point_loops(ctx, opt);
6013 str = tile_tests[i].domain;
6014 domain = isl_union_set_read_from_str(ctx, str);
6015 node = isl_schedule_node_from_domain(domain);
6016 node = isl_schedule_node_child(node, 0);
6017 str = tile_tests[i].schedule;
6018 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6019 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6020 str = tile_tests[i].sizes;
6021 sizes = isl_multi_val_read_from_str(ctx, str);
6022 node = isl_schedule_node_band_tile(node, sizes);
6024 str = tile_tests[i].tile;
6025 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6026 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6027 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6028 isl_multi_union_pw_aff_free(mupa);
6029 isl_multi_union_pw_aff_free(mupa2);
6031 node = isl_schedule_node_child(node, 0);
6033 str = tile_tests[i].point;
6034 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6035 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6036 if (equal >= 0 && equal)
6037 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6038 mupa2);
6039 isl_multi_union_pw_aff_free(mupa);
6040 isl_multi_union_pw_aff_free(mupa2);
6042 isl_schedule_node_free(node);
6044 if (equal < 0)
6045 return -1;
6046 if (!equal)
6047 isl_die(ctx, isl_error_unknown,
6048 "unexpected result", return -1);
6051 isl_options_set_tile_scale_tile_loops(ctx, scale);
6052 isl_options_set_tile_shift_point_loops(ctx, shift);
6054 return 0;
6057 /* Check that the domain hash of a space is equal to the hash
6058 * of the domain of the space.
6060 static int test_domain_hash(isl_ctx *ctx)
6062 isl_map *map;
6063 isl_space *space;
6064 uint32_t hash1, hash2;
6066 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6067 space = isl_map_get_space(map);
6068 isl_map_free(map);
6069 hash1 = isl_space_get_domain_hash(space);
6070 space = isl_space_domain(space);
6071 hash2 = isl_space_get_hash(space);
6072 isl_space_free(space);
6074 if (!space)
6075 return -1;
6076 if (hash1 != hash2)
6077 isl_die(ctx, isl_error_unknown,
6078 "domain hash not equal to hash of domain", return -1);
6080 return 0;
6083 struct {
6084 const char *name;
6085 int (*fn)(isl_ctx *ctx);
6086 } tests [] = {
6087 { "domain hash", &test_domain_hash },
6088 { "dual", &test_dual },
6089 { "dependence analysis", &test_flow },
6090 { "val", &test_val },
6091 { "compute divs", &test_compute_divs },
6092 { "partial lexmin", &test_partial_lexmin },
6093 { "simplify", &test_simplify },
6094 { "curry", &test_curry },
6095 { "piecewise multi affine expressions", &test_pw_multi_aff },
6096 { "multi piecewise affine expressions", &test_multi_pw_aff },
6097 { "conversion", &test_conversion },
6098 { "list", &test_list },
6099 { "align parameters", &test_align_parameters },
6100 { "preimage", &test_preimage },
6101 { "pullback", &test_pullback },
6102 { "AST", &test_ast },
6103 { "AST build", &test_ast_build },
6104 { "AST generation", &test_ast_gen },
6105 { "eliminate", &test_eliminate },
6106 { "residue class", &test_residue_class },
6107 { "div", &test_div },
6108 { "slice", &test_slice },
6109 { "fixed power", &test_fixed_power },
6110 { "sample", &test_sample },
6111 { "output", &test_output },
6112 { "vertices", &test_vertices },
6113 { "fixed", &test_fixed },
6114 { "equal", &test_equal },
6115 { "disjoint", &test_disjoint },
6116 { "product", &test_product },
6117 { "dim_max", &test_dim_max },
6118 { "affine", &test_aff },
6119 { "injective", &test_injective },
6120 { "schedule", &test_schedule },
6121 { "schedule tree grouping", &test_schedule_tree_group },
6122 { "tile", &test_tile },
6123 { "union_pw", &test_union_pw },
6124 { "eval", &test_eval },
6125 { "parse", &test_parse },
6126 { "single-valued", &test_sv },
6127 { "affine hull", &test_affine_hull },
6128 { "simple_hull", &test_simple_hull },
6129 { "coalesce", &test_coalesce },
6130 { "factorize", &test_factorize },
6131 { "subset", &test_subset },
6132 { "subtract", &test_subtract },
6133 { "intersect", &test_intersect },
6134 { "lexmin", &test_lexmin },
6135 { "min", &test_min },
6136 { "gist", &test_gist },
6137 { "piecewise quasi-polynomials", &test_pwqp },
6138 { "lift", &test_lift },
6139 { "bound", &test_bound },
6140 { "union", &test_union },
6141 { "split periods", &test_split_periods },
6142 { "lexicographic order", &test_lex },
6143 { "bijectivity", &test_bijective },
6144 { "dataflow analysis", &test_dep },
6145 { "reading", &test_read },
6146 { "bounded", &test_bounded },
6147 { "construction", &test_construction },
6148 { "dimension manipulation", &test_dim },
6149 { "map application", &test_application },
6150 { "convex hull", &test_convex_hull },
6151 { "transitive closure", &test_closure },
6154 int main(int argc, char **argv)
6156 int i;
6157 struct isl_ctx *ctx;
6158 struct isl_options *options;
6160 srcdir = getenv("srcdir");
6161 assert(srcdir);
6163 options = isl_options_new_with_defaults();
6164 assert(options);
6165 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6167 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6168 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6169 printf("%s\n", tests[i].name);
6170 if (tests[i].fn(ctx) < 0)
6171 goto error;
6173 isl_ctx_free(ctx);
6174 return 0;
6175 error:
6176 isl_ctx_free(ctx);
6177 return -1;