Implement small integer optimization for IMath
[isl.git] / isl_test.c
blob4ec7deb5ef06c0e6a6f74440370320c66ac1f5a7
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/set.h>
25 #include <isl/flow.h>
26 #include <isl_constraint_private.h>
27 #include <isl/polynomial.h>
28 #include <isl/union_set.h>
29 #include <isl/union_map.h>
30 #include <isl_factorization.h>
31 #include <isl/schedule.h>
32 #include <isl/schedule_node.h>
33 #include <isl_options_private.h>
34 #include <isl/vertices.h>
35 #include <isl/ast_build.h>
36 #include <isl/val.h>
37 #include <isl/ilp.h>
38 #include <isl_ast_build_expr.h>
39 #include <isl/options.h>
41 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
43 static char *srcdir;
45 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
46 char *filename;
47 int length;
48 char *pattern = "%s/test_inputs/%s.%s";
50 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
51 + strlen(suffix) + 1;
52 filename = isl_alloc_array(ctx, char, length);
54 if (!filename)
55 return NULL;
57 sprintf(filename, pattern, srcdir, name, suffix);
59 return filename;
62 void test_parse_map(isl_ctx *ctx, const char *str)
64 isl_map *map;
66 map = isl_map_read_from_str(ctx, str);
67 assert(map);
68 isl_map_free(map);
71 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
73 isl_map *map, *map2;
74 int equal;
76 map = isl_map_read_from_str(ctx, str);
77 map2 = isl_map_read_from_str(ctx, str2);
78 equal = isl_map_is_equal(map, map2);
79 isl_map_free(map);
80 isl_map_free(map2);
82 if (equal < 0)
83 return -1;
84 if (!equal)
85 isl_die(ctx, isl_error_unknown, "maps not equal",
86 return -1);
88 return 0;
91 void test_parse_pwqp(isl_ctx *ctx, const char *str)
93 isl_pw_qpolynomial *pwqp;
95 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
96 assert(pwqp);
97 isl_pw_qpolynomial_free(pwqp);
100 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
102 isl_pw_aff *pwaff;
104 pwaff = isl_pw_aff_read_from_str(ctx, str);
105 assert(pwaff);
106 isl_pw_aff_free(pwaff);
109 /* Check that we can read an isl_multi_val from "str" without errors.
111 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
113 isl_multi_val *mv;
115 mv = isl_multi_val_read_from_str(ctx, str);
116 isl_multi_val_free(mv);
118 return mv ? 0 : -1;
121 int test_parse(struct isl_ctx *ctx)
123 isl_map *map, *map2;
124 const char *str, *str2;
126 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
127 return -1;
128 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
129 return -1;
130 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
131 return -1;
133 str = "{ [i] -> [-i] }";
134 map = isl_map_read_from_str(ctx, str);
135 assert(map);
136 isl_map_free(map);
138 str = "{ A[i] -> L[([i/3])] }";
139 map = isl_map_read_from_str(ctx, str);
140 assert(map);
141 isl_map_free(map);
143 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
144 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
145 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
147 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
148 str2 = "{ [x, y] : 2y >= 6 - x }";
149 if (test_parse_map_equal(ctx, str, str2) < 0)
150 return -1;
152 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
153 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
154 return -1;
155 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
156 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
157 str) < 0)
158 return -1;
160 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
161 map = isl_map_read_from_str(ctx, str);
162 str = "{ [new, old] -> [o0, o1] : "
163 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
164 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
165 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
166 map2 = isl_map_read_from_str(ctx, str);
167 assert(isl_map_is_equal(map, map2));
168 isl_map_free(map);
169 isl_map_free(map2);
171 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
172 map = isl_map_read_from_str(ctx, str);
173 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
174 map2 = isl_map_read_from_str(ctx, str);
175 assert(isl_map_is_equal(map, map2));
176 isl_map_free(map);
177 isl_map_free(map2);
179 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
180 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
181 if (test_parse_map_equal(ctx, str, str2) < 0)
182 return -1;
184 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
185 str2 = "{ [i,j] -> [min(i,j)] }";
186 if (test_parse_map_equal(ctx, str, str2) < 0)
187 return -1;
189 str = "{ [i,j] : i != j }";
190 str2 = "{ [i,j] : i < j or i > j }";
191 if (test_parse_map_equal(ctx, str, str2) < 0)
192 return -1;
194 str = "{ [i,j] : (i+1)*2 >= j }";
195 str2 = "{ [i, j] : j <= 2 + 2i }";
196 if (test_parse_map_equal(ctx, str, str2) < 0)
197 return -1;
199 str = "{ [i] -> [i > 0 ? 4 : 5] }";
200 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
201 if (test_parse_map_equal(ctx, str, str2) < 0)
202 return -1;
204 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
205 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
206 if (test_parse_map_equal(ctx, str, str2) < 0)
207 return -1;
209 str = "{ [x] : x >= 0 }";
210 str2 = "{ [x] : x-0 >= 0 }";
211 if (test_parse_map_equal(ctx, str, str2) < 0)
212 return -1;
214 str = "{ [i] : ((i > 10)) }";
215 str2 = "{ [i] : i >= 11 }";
216 if (test_parse_map_equal(ctx, str, str2) < 0)
217 return -1;
219 str = "{ [i] -> [0] }";
220 str2 = "{ [i] -> [0 * i] }";
221 if (test_parse_map_equal(ctx, str, str2) < 0)
222 return -1;
224 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
225 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
226 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
227 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
229 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
230 "{ [a] -> [b] : true }") < 0)
231 return -1;
233 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
234 "{ [i] : i <= 10 }") < 0)
235 return -1;
237 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
238 "[n] -> { [i] : i <= n }") < 0)
239 return -1;
241 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
242 return -1;
244 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
245 "{ [i] : exists a : i = 2 a }") < 0)
246 return -1;
248 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
249 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
250 return -1;
252 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
253 "{ [a] -> [a - 1] : a > 0 }") < 0)
254 return -1;
255 if (test_parse_map_equal(ctx,
256 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
257 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
258 return -1;
259 if (test_parse_map_equal(ctx,
260 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
261 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
262 return -1;
263 if (test_parse_map_equal(ctx,
264 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
265 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
266 return -1;
267 if (test_parse_map_equal(ctx,
268 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
269 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
270 return -1;
271 if (test_parse_map_equal(ctx,
272 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
273 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
274 return -1;
276 return 0;
279 static int test_read(isl_ctx *ctx)
281 char *filename;
282 FILE *input;
283 isl_basic_set *bset1, *bset2;
284 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
285 int equal;
287 filename = get_filename(ctx, "set", "omega");
288 assert(filename);
289 input = fopen(filename, "r");
290 assert(input);
292 bset1 = isl_basic_set_read_from_file(ctx, input);
293 bset2 = isl_basic_set_read_from_str(ctx, str);
295 equal = isl_basic_set_is_equal(bset1, bset2);
297 isl_basic_set_free(bset1);
298 isl_basic_set_free(bset2);
299 free(filename);
301 fclose(input);
303 if (equal < 0)
304 return -1;
305 if (!equal)
306 isl_die(ctx, isl_error_unknown,
307 "read sets not equal", return -1);
309 return 0;
312 static int test_bounded(isl_ctx *ctx)
314 isl_set *set;
315 int bounded;
317 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
318 bounded = isl_set_is_bounded(set);
319 isl_set_free(set);
321 if (bounded < 0)
322 return -1;
323 if (!bounded)
324 isl_die(ctx, isl_error_unknown,
325 "set not considered bounded", return -1);
327 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
328 bounded = isl_set_is_bounded(set);
329 assert(!bounded);
330 isl_set_free(set);
332 if (bounded < 0)
333 return -1;
334 if (bounded)
335 isl_die(ctx, isl_error_unknown,
336 "set considered bounded", return -1);
338 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
339 bounded = isl_set_is_bounded(set);
340 isl_set_free(set);
342 if (bounded < 0)
343 return -1;
344 if (bounded)
345 isl_die(ctx, isl_error_unknown,
346 "set considered bounded", return -1);
348 return 0;
351 /* Construct the basic set { [i] : 5 <= i <= N } */
352 static int test_construction(isl_ctx *ctx)
354 isl_int v;
355 isl_space *dim;
356 isl_local_space *ls;
357 isl_basic_set *bset;
358 isl_constraint *c;
360 isl_int_init(v);
362 dim = isl_space_set_alloc(ctx, 1, 1);
363 bset = isl_basic_set_universe(isl_space_copy(dim));
364 ls = isl_local_space_from_space(dim);
366 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
367 isl_int_set_si(v, -1);
368 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
369 isl_int_set_si(v, 1);
370 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
371 bset = isl_basic_set_add_constraint(bset, c);
373 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
374 isl_int_set_si(v, 1);
375 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
376 isl_int_set_si(v, -5);
377 isl_constraint_set_constant(c, v);
378 bset = isl_basic_set_add_constraint(bset, c);
380 isl_local_space_free(ls);
381 isl_basic_set_free(bset);
383 isl_int_clear(v);
385 return 0;
388 static int test_dim(isl_ctx *ctx)
390 const char *str;
391 isl_map *map1, *map2;
392 int equal;
394 map1 = isl_map_read_from_str(ctx,
395 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
396 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
397 map2 = isl_map_read_from_str(ctx,
398 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
399 equal = isl_map_is_equal(map1, map2);
400 isl_map_free(map2);
402 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
403 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
404 if (equal >= 0 && equal)
405 equal = isl_map_is_equal(map1, map2);
407 isl_map_free(map1);
408 isl_map_free(map2);
410 if (equal < 0)
411 return -1;
412 if (!equal)
413 isl_die(ctx, isl_error_unknown,
414 "unexpected result", return -1);
416 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
417 map1 = isl_map_read_from_str(ctx, str);
418 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
419 map2 = isl_map_read_from_str(ctx, str);
420 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
421 equal = isl_map_is_equal(map1, map2);
422 isl_map_free(map1);
423 isl_map_free(map2);
425 if (equal < 0)
426 return -1;
427 if (!equal)
428 isl_die(ctx, isl_error_unknown,
429 "unexpected result", return -1);
431 return 0;
434 struct {
435 __isl_give isl_val *(*op)(__isl_take isl_val *v);
436 const char *arg;
437 const char *res;
438 } val_un_tests[] = {
439 { &isl_val_neg, "0", "0" },
440 { &isl_val_abs, "0", "0" },
441 { &isl_val_2exp, "0", "1" },
442 { &isl_val_floor, "0", "0" },
443 { &isl_val_ceil, "0", "0" },
444 { &isl_val_neg, "1", "-1" },
445 { &isl_val_neg, "-1", "1" },
446 { &isl_val_neg, "1/2", "-1/2" },
447 { &isl_val_neg, "-1/2", "1/2" },
448 { &isl_val_neg, "infty", "-infty" },
449 { &isl_val_neg, "-infty", "infty" },
450 { &isl_val_neg, "NaN", "NaN" },
451 { &isl_val_abs, "1", "1" },
452 { &isl_val_abs, "-1", "1" },
453 { &isl_val_abs, "1/2", "1/2" },
454 { &isl_val_abs, "-1/2", "1/2" },
455 { &isl_val_abs, "infty", "infty" },
456 { &isl_val_abs, "-infty", "infty" },
457 { &isl_val_abs, "NaN", "NaN" },
458 { &isl_val_floor, "1", "1" },
459 { &isl_val_floor, "-1", "-1" },
460 { &isl_val_floor, "1/2", "0" },
461 { &isl_val_floor, "-1/2", "-1" },
462 { &isl_val_floor, "infty", "infty" },
463 { &isl_val_floor, "-infty", "-infty" },
464 { &isl_val_floor, "NaN", "NaN" },
465 { &isl_val_ceil, "1", "1" },
466 { &isl_val_ceil, "-1", "-1" },
467 { &isl_val_ceil, "1/2", "1" },
468 { &isl_val_ceil, "-1/2", "0" },
469 { &isl_val_ceil, "infty", "infty" },
470 { &isl_val_ceil, "-infty", "-infty" },
471 { &isl_val_ceil, "NaN", "NaN" },
472 { &isl_val_2exp, "-3", "1/8" },
473 { &isl_val_2exp, "-1", "1/2" },
474 { &isl_val_2exp, "1", "2" },
475 { &isl_val_2exp, "2", "4" },
476 { &isl_val_2exp, "3", "8" },
477 { &isl_val_inv, "1", "1" },
478 { &isl_val_inv, "2", "1/2" },
479 { &isl_val_inv, "1/2", "2" },
480 { &isl_val_inv, "-2", "-1/2" },
481 { &isl_val_inv, "-1/2", "-2" },
482 { &isl_val_inv, "0", "NaN" },
483 { &isl_val_inv, "NaN", "NaN" },
484 { &isl_val_inv, "infty", "0" },
485 { &isl_val_inv, "-infty", "0" },
488 /* Perform some basic tests of unary operations on isl_val objects.
490 static int test_un_val(isl_ctx *ctx)
492 int i;
493 isl_val *v, *res;
494 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
495 int ok;
497 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
498 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
499 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
500 fn = val_un_tests[i].op;
501 v = fn(v);
502 if (isl_val_is_nan(res))
503 ok = isl_val_is_nan(v);
504 else
505 ok = isl_val_eq(v, res);
506 isl_val_free(v);
507 isl_val_free(res);
508 if (ok < 0)
509 return -1;
510 if (!ok)
511 isl_die(ctx, isl_error_unknown,
512 "unexpected result", return -1);
515 return 0;
518 struct {
519 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
520 __isl_take isl_val *v2);
521 } val_bin_op[] = {
522 ['+'] = { &isl_val_add },
523 ['-'] = { &isl_val_sub },
524 ['*'] = { &isl_val_mul },
525 ['/'] = { &isl_val_div },
526 ['g'] = { &isl_val_gcd },
527 ['m'] = { &isl_val_min },
528 ['M'] = { &isl_val_max },
531 struct {
532 const char *arg1;
533 unsigned char op;
534 const char *arg2;
535 const char *res;
536 } val_bin_tests[] = {
537 { "0", '+', "0", "0" },
538 { "1", '+', "0", "1" },
539 { "1", '+', "1", "2" },
540 { "1", '-', "1", "0" },
541 { "1", '*', "1", "1" },
542 { "1", '/', "1", "1" },
543 { "2", '*', "3", "6" },
544 { "2", '*', "1/2", "1" },
545 { "2", '*', "1/3", "2/3" },
546 { "2/3", '*', "3/5", "2/5" },
547 { "2/3", '*', "7/5", "14/15" },
548 { "2", '/', "1/2", "4" },
549 { "-2", '/', "-1/2", "4" },
550 { "-2", '/', "1/2", "-4" },
551 { "2", '/', "-1/2", "-4" },
552 { "2", '/', "2", "1" },
553 { "2", '/', "3", "2/3" },
554 { "2/3", '/', "5/3", "2/5" },
555 { "2/3", '/', "5/7", "14/15" },
556 { "0", '/', "0", "NaN" },
557 { "42", '/', "0", "NaN" },
558 { "-42", '/', "0", "NaN" },
559 { "infty", '/', "0", "NaN" },
560 { "-infty", '/', "0", "NaN" },
561 { "NaN", '/', "0", "NaN" },
562 { "0", '/', "NaN", "NaN" },
563 { "42", '/', "NaN", "NaN" },
564 { "-42", '/', "NaN", "NaN" },
565 { "infty", '/', "NaN", "NaN" },
566 { "-infty", '/', "NaN", "NaN" },
567 { "NaN", '/', "NaN", "NaN" },
568 { "0", '/', "infty", "0" },
569 { "42", '/', "infty", "0" },
570 { "-42", '/', "infty", "0" },
571 { "infty", '/', "infty", "NaN" },
572 { "-infty", '/', "infty", "NaN" },
573 { "NaN", '/', "infty", "NaN" },
574 { "0", '/', "-infty", "0" },
575 { "42", '/', "-infty", "0" },
576 { "-42", '/', "-infty", "0" },
577 { "infty", '/', "-infty", "NaN" },
578 { "-infty", '/', "-infty", "NaN" },
579 { "NaN", '/', "-infty", "NaN" },
580 { "1", '-', "1/3", "2/3" },
581 { "1/3", '+', "1/2", "5/6" },
582 { "1/2", '+', "1/2", "1" },
583 { "3/4", '-', "1/4", "1/2" },
584 { "1/2", '-', "1/3", "1/6" },
585 { "infty", '+', "42", "infty" },
586 { "infty", '+', "infty", "infty" },
587 { "42", '+', "infty", "infty" },
588 { "infty", '-', "infty", "NaN" },
589 { "infty", '*', "infty", "infty" },
590 { "infty", '*', "-infty", "-infty" },
591 { "-infty", '*', "infty", "-infty" },
592 { "-infty", '*', "-infty", "infty" },
593 { "0", '*', "infty", "NaN" },
594 { "1", '*', "infty", "infty" },
595 { "infty", '*', "0", "NaN" },
596 { "infty", '*', "42", "infty" },
597 { "42", '-', "infty", "-infty" },
598 { "infty", '+', "-infty", "NaN" },
599 { "4", 'g', "6", "2" },
600 { "5", 'g', "6", "1" },
601 { "42", 'm', "3", "3" },
602 { "42", 'M', "3", "42" },
603 { "3", 'm', "42", "3" },
604 { "3", 'M', "42", "42" },
605 { "42", 'm', "infty", "42" },
606 { "42", 'M', "infty", "infty" },
607 { "42", 'm', "-infty", "-infty" },
608 { "42", 'M', "-infty", "42" },
609 { "42", 'm', "NaN", "NaN" },
610 { "42", 'M', "NaN", "NaN" },
611 { "infty", 'm', "-infty", "-infty" },
612 { "infty", 'M', "-infty", "infty" },
615 /* Perform some basic tests of binary operations on isl_val objects.
617 static int test_bin_val(isl_ctx *ctx)
619 int i;
620 isl_val *v1, *v2, *res;
621 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
622 __isl_take isl_val *v2);
623 int ok;
625 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
626 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
627 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
628 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
629 fn = val_bin_op[val_bin_tests[i].op].fn;
630 v1 = fn(v1, v2);
631 if (isl_val_is_nan(res))
632 ok = isl_val_is_nan(v1);
633 else
634 ok = isl_val_eq(v1, res);
635 isl_val_free(v1);
636 isl_val_free(res);
637 if (ok < 0)
638 return -1;
639 if (!ok)
640 isl_die(ctx, isl_error_unknown,
641 "unexpected result", return -1);
644 return 0;
647 /* Perform some basic tests on isl_val objects.
649 static int test_val(isl_ctx *ctx)
651 if (test_un_val(ctx) < 0)
652 return -1;
653 if (test_bin_val(ctx) < 0)
654 return -1;
655 return 0;
658 static int test_div(isl_ctx *ctx)
660 unsigned n;
661 const char *str;
662 int empty;
663 isl_int v;
664 isl_space *dim;
665 isl_set *set;
666 isl_local_space *ls;
667 struct isl_basic_set *bset;
668 struct isl_constraint *c;
670 isl_int_init(v);
672 /* test 1 */
673 dim = isl_space_set_alloc(ctx, 0, 3);
674 bset = isl_basic_set_universe(isl_space_copy(dim));
675 ls = isl_local_space_from_space(dim);
677 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
678 isl_int_set_si(v, -1);
679 isl_constraint_set_constant(c, v);
680 isl_int_set_si(v, 1);
681 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
682 isl_int_set_si(v, 3);
683 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
684 bset = isl_basic_set_add_constraint(bset, c);
686 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
687 isl_int_set_si(v, 1);
688 isl_constraint_set_constant(c, v);
689 isl_int_set_si(v, -1);
690 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
691 isl_int_set_si(v, 3);
692 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
693 bset = isl_basic_set_add_constraint(bset, c);
695 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
697 assert(bset && bset->n_div == 1);
698 isl_local_space_free(ls);
699 isl_basic_set_free(bset);
701 /* test 2 */
702 dim = isl_space_set_alloc(ctx, 0, 3);
703 bset = isl_basic_set_universe(isl_space_copy(dim));
704 ls = isl_local_space_from_space(dim);
706 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
707 isl_int_set_si(v, 1);
708 isl_constraint_set_constant(c, v);
709 isl_int_set_si(v, -1);
710 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
711 isl_int_set_si(v, 3);
712 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
713 bset = isl_basic_set_add_constraint(bset, c);
715 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
716 isl_int_set_si(v, -1);
717 isl_constraint_set_constant(c, v);
718 isl_int_set_si(v, 1);
719 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
720 isl_int_set_si(v, 3);
721 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
722 bset = isl_basic_set_add_constraint(bset, c);
724 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
726 assert(bset && bset->n_div == 1);
727 isl_local_space_free(ls);
728 isl_basic_set_free(bset);
730 /* test 3 */
731 dim = isl_space_set_alloc(ctx, 0, 3);
732 bset = isl_basic_set_universe(isl_space_copy(dim));
733 ls = isl_local_space_from_space(dim);
735 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
736 isl_int_set_si(v, 1);
737 isl_constraint_set_constant(c, v);
738 isl_int_set_si(v, -1);
739 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
740 isl_int_set_si(v, 3);
741 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
742 bset = isl_basic_set_add_constraint(bset, c);
744 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
745 isl_int_set_si(v, -3);
746 isl_constraint_set_constant(c, v);
747 isl_int_set_si(v, 1);
748 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
749 isl_int_set_si(v, 4);
750 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
751 bset = isl_basic_set_add_constraint(bset, c);
753 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
755 assert(bset && bset->n_div == 1);
756 isl_local_space_free(ls);
757 isl_basic_set_free(bset);
759 /* test 4 */
760 dim = isl_space_set_alloc(ctx, 0, 3);
761 bset = isl_basic_set_universe(isl_space_copy(dim));
762 ls = isl_local_space_from_space(dim);
764 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
765 isl_int_set_si(v, 2);
766 isl_constraint_set_constant(c, v);
767 isl_int_set_si(v, -1);
768 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
769 isl_int_set_si(v, 3);
770 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
771 bset = isl_basic_set_add_constraint(bset, c);
773 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
774 isl_int_set_si(v, -1);
775 isl_constraint_set_constant(c, v);
776 isl_int_set_si(v, 1);
777 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
778 isl_int_set_si(v, 6);
779 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
780 bset = isl_basic_set_add_constraint(bset, c);
782 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
784 assert(isl_basic_set_is_empty(bset));
785 isl_local_space_free(ls);
786 isl_basic_set_free(bset);
788 /* test 5 */
789 dim = isl_space_set_alloc(ctx, 0, 3);
790 bset = isl_basic_set_universe(isl_space_copy(dim));
791 ls = isl_local_space_from_space(dim);
793 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
794 isl_int_set_si(v, -1);
795 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
796 isl_int_set_si(v, 3);
797 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
798 bset = isl_basic_set_add_constraint(bset, c);
800 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
801 isl_int_set_si(v, 1);
802 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
803 isl_int_set_si(v, -3);
804 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
805 bset = isl_basic_set_add_constraint(bset, c);
807 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
809 assert(bset && bset->n_div == 0);
810 isl_basic_set_free(bset);
811 isl_local_space_free(ls);
813 /* test 6 */
814 dim = isl_space_set_alloc(ctx, 0, 3);
815 bset = isl_basic_set_universe(isl_space_copy(dim));
816 ls = isl_local_space_from_space(dim);
818 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
819 isl_int_set_si(v, -1);
820 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
821 isl_int_set_si(v, 6);
822 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
823 bset = isl_basic_set_add_constraint(bset, c);
825 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
826 isl_int_set_si(v, 1);
827 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
828 isl_int_set_si(v, -3);
829 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
830 bset = isl_basic_set_add_constraint(bset, c);
832 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
834 assert(bset && bset->n_div == 1);
835 isl_basic_set_free(bset);
836 isl_local_space_free(ls);
838 /* test 7 */
839 /* This test is a bit tricky. We set up an equality
840 * a + 3b + 3c = 6 e0
841 * Normalization of divs creates _two_ divs
842 * a = 3 e0
843 * c - b - e0 = 2 e1
844 * Afterwards e0 is removed again because it has coefficient -1
845 * and we end up with the original equality and div again.
846 * Perhaps we can avoid the introduction of this temporary div.
848 dim = isl_space_set_alloc(ctx, 0, 4);
849 bset = isl_basic_set_universe(isl_space_copy(dim));
850 ls = isl_local_space_from_space(dim);
852 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
853 isl_int_set_si(v, -1);
854 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
855 isl_int_set_si(v, -3);
856 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
857 isl_int_set_si(v, -3);
858 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
859 isl_int_set_si(v, 6);
860 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
861 bset = isl_basic_set_add_constraint(bset, c);
863 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
865 /* Test disabled for now */
867 assert(bset && bset->n_div == 1);
869 isl_local_space_free(ls);
870 isl_basic_set_free(bset);
872 /* test 8 */
873 dim = isl_space_set_alloc(ctx, 0, 5);
874 bset = isl_basic_set_universe(isl_space_copy(dim));
875 ls = isl_local_space_from_space(dim);
877 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
878 isl_int_set_si(v, -1);
879 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
880 isl_int_set_si(v, -3);
881 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
882 isl_int_set_si(v, -3);
883 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
884 isl_int_set_si(v, 6);
885 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
886 bset = isl_basic_set_add_constraint(bset, c);
888 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
889 isl_int_set_si(v, -1);
890 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
891 isl_int_set_si(v, 1);
892 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
893 isl_int_set_si(v, 1);
894 isl_constraint_set_constant(c, v);
895 bset = isl_basic_set_add_constraint(bset, c);
897 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
899 /* Test disabled for now */
901 assert(bset && bset->n_div == 1);
903 isl_local_space_free(ls);
904 isl_basic_set_free(bset);
906 /* test 9 */
907 dim = isl_space_set_alloc(ctx, 0, 4);
908 bset = isl_basic_set_universe(isl_space_copy(dim));
909 ls = isl_local_space_from_space(dim);
911 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
912 isl_int_set_si(v, 1);
913 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
914 isl_int_set_si(v, -1);
915 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
916 isl_int_set_si(v, -2);
917 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
918 bset = isl_basic_set_add_constraint(bset, c);
920 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
921 isl_int_set_si(v, -1);
922 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
923 isl_int_set_si(v, 3);
924 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
925 isl_int_set_si(v, 2);
926 isl_constraint_set_constant(c, v);
927 bset = isl_basic_set_add_constraint(bset, c);
929 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
931 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
933 assert(!isl_basic_set_is_empty(bset));
935 isl_local_space_free(ls);
936 isl_basic_set_free(bset);
938 /* test 10 */
939 dim = isl_space_set_alloc(ctx, 0, 3);
940 bset = isl_basic_set_universe(isl_space_copy(dim));
941 ls = isl_local_space_from_space(dim);
943 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
944 isl_int_set_si(v, 1);
945 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
946 isl_int_set_si(v, -2);
947 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
948 bset = isl_basic_set_add_constraint(bset, c);
950 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
952 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
954 isl_local_space_free(ls);
955 isl_basic_set_free(bset);
957 isl_int_clear(v);
959 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
960 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
961 set = isl_set_read_from_str(ctx, str);
962 set = isl_set_compute_divs(set);
963 isl_set_free(set);
964 if (!set)
965 return -1;
967 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
968 bset = isl_basic_set_read_from_str(ctx, str);
969 n = isl_basic_set_dim(bset, isl_dim_div);
970 isl_basic_set_free(bset);
971 if (!bset)
972 return -1;
973 if (n != 0)
974 isl_die(ctx, isl_error_unknown,
975 "expecting no existentials", return -1);
977 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
978 set = isl_set_read_from_str(ctx, str);
979 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
980 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
981 empty = isl_set_is_empty(set);
982 isl_set_free(set);
983 if (empty < 0)
984 return -1;
985 if (!empty)
986 isl_die(ctx, isl_error_unknown,
987 "result not as accurate as expected", return -1);
989 return 0;
992 void test_application_case(struct isl_ctx *ctx, const char *name)
994 char *filename;
995 FILE *input;
996 struct isl_basic_set *bset1, *bset2;
997 struct isl_basic_map *bmap;
999 filename = get_filename(ctx, name, "omega");
1000 assert(filename);
1001 input = fopen(filename, "r");
1002 assert(input);
1004 bset1 = isl_basic_set_read_from_file(ctx, input);
1005 bmap = isl_basic_map_read_from_file(ctx, input);
1007 bset1 = isl_basic_set_apply(bset1, bmap);
1009 bset2 = isl_basic_set_read_from_file(ctx, input);
1011 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1013 isl_basic_set_free(bset1);
1014 isl_basic_set_free(bset2);
1015 free(filename);
1017 fclose(input);
1020 static int test_application(isl_ctx *ctx)
1022 test_application_case(ctx, "application");
1023 test_application_case(ctx, "application2");
1025 return 0;
1028 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1030 char *filename;
1031 FILE *input;
1032 struct isl_basic_set *bset1, *bset2;
1034 filename = get_filename(ctx, name, "polylib");
1035 assert(filename);
1036 input = fopen(filename, "r");
1037 assert(input);
1039 bset1 = isl_basic_set_read_from_file(ctx, input);
1040 bset2 = isl_basic_set_read_from_file(ctx, input);
1042 bset1 = isl_basic_set_affine_hull(bset1);
1044 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1046 isl_basic_set_free(bset1);
1047 isl_basic_set_free(bset2);
1048 free(filename);
1050 fclose(input);
1053 int test_affine_hull(struct isl_ctx *ctx)
1055 const char *str;
1056 isl_set *set;
1057 isl_basic_set *bset, *bset2;
1058 int n;
1059 int subset;
1061 test_affine_hull_case(ctx, "affine2");
1062 test_affine_hull_case(ctx, "affine");
1063 test_affine_hull_case(ctx, "affine3");
1065 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1066 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1067 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1068 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1069 set = isl_set_read_from_str(ctx, str);
1070 bset = isl_set_affine_hull(set);
1071 n = isl_basic_set_dim(bset, isl_dim_div);
1072 isl_basic_set_free(bset);
1073 if (n != 0)
1074 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1075 return -1);
1077 /* Check that isl_map_affine_hull is not confused by
1078 * the reordering of divs in isl_map_align_divs.
1080 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1081 "32e0 = b and 32e1 = c); "
1082 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1083 set = isl_set_read_from_str(ctx, str);
1084 bset = isl_set_affine_hull(set);
1085 isl_basic_set_free(bset);
1086 if (!bset)
1087 return -1;
1089 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1090 "32e2 = 31 + 31e0 }";
1091 set = isl_set_read_from_str(ctx, str);
1092 bset = isl_set_affine_hull(set);
1093 str = "{ [a] : exists e : a = 32 e }";
1094 bset2 = isl_basic_set_read_from_str(ctx, str);
1095 subset = isl_basic_set_is_subset(bset, bset2);
1096 isl_basic_set_free(bset);
1097 isl_basic_set_free(bset2);
1098 if (subset < 0)
1099 return -1;
1100 if (!subset)
1101 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1102 return -1);
1104 return 0;
1107 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1109 char *filename;
1110 FILE *input;
1111 struct isl_basic_set *bset1, *bset2;
1112 struct isl_set *set;
1114 filename = get_filename(ctx, name, "polylib");
1115 assert(filename);
1116 input = fopen(filename, "r");
1117 assert(input);
1119 bset1 = isl_basic_set_read_from_file(ctx, input);
1120 bset2 = isl_basic_set_read_from_file(ctx, input);
1122 set = isl_basic_set_union(bset1, bset2);
1123 bset1 = isl_set_convex_hull(set);
1125 bset2 = isl_basic_set_read_from_file(ctx, input);
1127 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1129 isl_basic_set_free(bset1);
1130 isl_basic_set_free(bset2);
1131 free(filename);
1133 fclose(input);
1136 struct {
1137 const char *set;
1138 const char *hull;
1139 } convex_hull_tests[] = {
1140 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1141 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1142 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1143 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1144 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1145 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1146 "i2 <= 5 and i2 >= 4; "
1147 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1148 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1149 "i2 <= 5 + i0 and i2 >= i0 }" },
1150 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1151 "{ [x, y] : 1 = 0 }" },
1154 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1156 int i;
1157 int orig_convex = ctx->opt->convex;
1158 ctx->opt->convex = convex;
1160 test_convex_hull_case(ctx, "convex0");
1161 test_convex_hull_case(ctx, "convex1");
1162 test_convex_hull_case(ctx, "convex2");
1163 test_convex_hull_case(ctx, "convex3");
1164 test_convex_hull_case(ctx, "convex4");
1165 test_convex_hull_case(ctx, "convex5");
1166 test_convex_hull_case(ctx, "convex6");
1167 test_convex_hull_case(ctx, "convex7");
1168 test_convex_hull_case(ctx, "convex8");
1169 test_convex_hull_case(ctx, "convex9");
1170 test_convex_hull_case(ctx, "convex10");
1171 test_convex_hull_case(ctx, "convex11");
1172 test_convex_hull_case(ctx, "convex12");
1173 test_convex_hull_case(ctx, "convex13");
1174 test_convex_hull_case(ctx, "convex14");
1175 test_convex_hull_case(ctx, "convex15");
1177 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1178 isl_set *set1, *set2;
1179 int equal;
1181 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1182 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1183 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1184 equal = isl_set_is_equal(set1, set2);
1185 isl_set_free(set1);
1186 isl_set_free(set2);
1188 if (equal < 0)
1189 return -1;
1190 if (!equal)
1191 isl_die(ctx, isl_error_unknown,
1192 "unexpected convex hull", return -1);
1195 ctx->opt->convex = orig_convex;
1197 return 0;
1200 static int test_convex_hull(isl_ctx *ctx)
1202 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1203 return -1;
1204 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1205 return -1;
1206 return 0;
1209 void test_gist_case(struct isl_ctx *ctx, const char *name)
1211 char *filename;
1212 FILE *input;
1213 struct isl_basic_set *bset1, *bset2;
1215 filename = get_filename(ctx, name, "polylib");
1216 assert(filename);
1217 input = fopen(filename, "r");
1218 assert(input);
1220 bset1 = isl_basic_set_read_from_file(ctx, input);
1221 bset2 = isl_basic_set_read_from_file(ctx, input);
1223 bset1 = isl_basic_set_gist(bset1, bset2);
1225 bset2 = isl_basic_set_read_from_file(ctx, input);
1227 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1229 isl_basic_set_free(bset1);
1230 isl_basic_set_free(bset2);
1231 free(filename);
1233 fclose(input);
1236 struct {
1237 const char *set;
1238 const char *context;
1239 const char *gist;
1240 } gist_tests[] = {
1241 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1242 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1243 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1244 "{ [a, b, c] : a <= 15 }" },
1245 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1246 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1247 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1248 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1249 { "{ [m, n, a, b] : a <= 2147 + n }",
1250 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1251 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1252 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1253 "b >= 0) }",
1254 "{ [m, n, ku, kl] }" },
1257 static int test_gist(struct isl_ctx *ctx)
1259 int i;
1260 const char *str;
1261 isl_basic_set *bset1, *bset2;
1262 isl_map *map1, *map2;
1263 int equal;
1265 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1266 int equal_input;
1267 isl_set *set1, *set2, *copy;
1269 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1270 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1271 copy = isl_set_copy(set1);
1272 set1 = isl_set_gist(set1, set2);
1273 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1274 equal = isl_set_is_equal(set1, set2);
1275 isl_set_free(set1);
1276 isl_set_free(set2);
1277 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1278 equal_input = isl_set_is_equal(set1, copy);
1279 isl_set_free(set1);
1280 isl_set_free(copy);
1281 if (equal < 0 || equal_input < 0)
1282 return -1;
1283 if (!equal)
1284 isl_die(ctx, isl_error_unknown,
1285 "incorrect gist result", return -1);
1286 if (!equal_input)
1287 isl_die(ctx, isl_error_unknown,
1288 "gist modified input", return -1);
1291 test_gist_case(ctx, "gist1");
1293 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1294 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1295 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1296 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1297 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1298 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1299 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1300 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1301 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1302 bset1 = isl_basic_set_read_from_str(ctx, str);
1303 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1304 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1305 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1306 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1307 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1308 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1309 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1310 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1311 bset2 = isl_basic_set_read_from_str(ctx, str);
1312 bset1 = isl_basic_set_gist(bset1, bset2);
1313 assert(bset1 && bset1->n_div == 0);
1314 isl_basic_set_free(bset1);
1316 /* Check that the integer divisions of the second disjunct
1317 * do not spread to the first disjunct.
1319 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1320 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1321 "(exists (e0 = [(-1 + t1)/16], "
1322 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1323 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1324 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1325 "o0 <= 4294967295 and t1 <= -1)) }";
1326 map1 = isl_map_read_from_str(ctx, str);
1327 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1328 map2 = isl_map_read_from_str(ctx, str);
1329 map1 = isl_map_gist(map1, map2);
1330 if (!map1)
1331 return -1;
1332 if (map1->n != 1)
1333 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1334 isl_map_free(map1); return -1);
1335 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1336 isl_die(ctx, isl_error_unknown, "expecting single div",
1337 isl_map_free(map1); return -1);
1338 isl_map_free(map1);
1340 return 0;
1343 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1345 isl_set *set, *set2;
1346 int equal;
1347 int one;
1349 set = isl_set_read_from_str(ctx, str);
1350 set = isl_set_coalesce(set);
1351 set2 = isl_set_read_from_str(ctx, str);
1352 equal = isl_set_is_equal(set, set2);
1353 one = set && set->n == 1;
1354 isl_set_free(set);
1355 isl_set_free(set2);
1357 if (equal < 0)
1358 return -1;
1359 if (!equal)
1360 isl_die(ctx, isl_error_unknown,
1361 "coalesced set not equal to input", return -1);
1362 if (check_one && !one)
1363 isl_die(ctx, isl_error_unknown,
1364 "coalesced set should not be a union", return -1);
1366 return 0;
1369 /* Inputs for coalescing tests with unbounded wrapping.
1370 * "str" is a string representation of the input set.
1371 * "single_disjunct" is set if we expect the result to consist of
1372 * a single disjunct.
1374 struct {
1375 int single_disjunct;
1376 const char *str;
1377 } coalesce_unbounded_tests[] = {
1378 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1379 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1380 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1381 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1382 "-10 <= y <= 0}" },
1383 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1384 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1385 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1386 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1389 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1390 * option turned off.
1392 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1394 int i;
1395 int r = 0;
1396 int bounded;
1398 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1399 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1401 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1402 const char *str = coalesce_unbounded_tests[i].str;
1403 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1404 if (test_coalesce_set(ctx, str, check_one) >= 0)
1405 continue;
1406 r = -1;
1407 break;
1410 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1412 return r;
1415 /* Inputs for coalescing tests.
1416 * "str" is a string representation of the input set.
1417 * "single_disjunct" is set if we expect the result to consist of
1418 * a single disjunct.
1420 struct {
1421 int single_disjunct;
1422 const char *str;
1423 } coalesce_tests[] = {
1424 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1425 "y >= x & x >= 2 & 5 >= y }" },
1426 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1427 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1428 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1429 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1430 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1431 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1432 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1433 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1434 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1435 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1436 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1437 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1438 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1439 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1440 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1441 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1442 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1443 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1444 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1445 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1446 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1447 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1448 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1449 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1450 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1451 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1452 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1453 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1454 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1455 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1456 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1457 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1458 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1459 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1460 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1461 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1462 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1463 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1464 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1465 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1466 "[o0, o1, o2, o3, o4, o5, o6]] : "
1467 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1468 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1469 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1470 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1471 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1472 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1473 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1474 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1475 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1476 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1477 "o6 >= i3 + i6 - o3 and M >= 0 and "
1478 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1479 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1480 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1481 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1482 "(o0 = 0 and M >= 2 and N >= 3) or "
1483 "(M = 0 and o0 = 0 and N >= 3) }" },
1484 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1485 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1486 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1487 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1488 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1489 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1490 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1491 "(y = 3 and x = 1) }" },
1492 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1493 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1494 "i1 <= M and i3 <= M and i4 <= M) or "
1495 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1496 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1497 "i4 <= -1 + M) }" },
1498 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1499 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1500 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1501 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1502 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1503 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1504 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1505 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1506 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1507 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1508 { 0, "{ [a, b] : exists e : 2e = a and "
1509 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1510 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1511 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1512 "j >= 1 and j' <= i + j - i' and i >= 1; "
1513 "[1, 1, 1, 1] }" },
1514 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1515 "[i,j] : exists a : j = 3a }" },
1516 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1517 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1518 "a >= 3) or "
1519 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1520 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1521 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1522 "c <= 6 + 8a and a >= 3; "
1523 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1524 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1525 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1526 "[x,0] : 3 <= x <= 4 }" },
1527 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1528 "[x,0] : 4 <= x <= 5 }" },
1529 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1530 "[x,0] : 3 <= x <= 5 }" },
1531 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1532 "[x,0] : 3 <= x <= 4 }" },
1533 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1534 "i1 <= 0; "
1535 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1536 { 1, "{ [0,0]; [1,1] }" },
1537 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1538 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1539 "ii <= k;"
1540 "[k, 0, k] : k <= 6 and k >= 1 }" },
1541 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1542 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1543 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1544 { 1, "[n] -> { [1] : n >= 0;"
1545 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1546 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1547 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1548 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1549 "2e0 <= x and 2e0 <= n);"
1550 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1551 "n >= 0) }" },
1552 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1553 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1554 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1555 "t1 = 1 }" },
1556 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1557 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1558 "[0, 0] }" },
1559 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1560 "t1 >= 13 and t1 <= 16);"
1561 "[t1] : t1 <= 15 and t1 >= 12 }" },
1562 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1563 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1564 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1565 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1566 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1567 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1568 "i <= 4j + 2 }" },
1569 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1570 "(exists (e0 : 3e0 = -2 + c0)) }" },
1571 { 0, "[n, b0, t0] -> "
1572 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1573 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1574 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1575 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1576 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1577 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1578 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1579 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1580 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1581 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1582 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1583 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1584 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1585 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1586 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1587 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1588 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1589 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1590 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1591 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1592 { 0, "{ [i0, i1, i2] : "
1593 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1594 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1595 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1596 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1597 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1598 "32e0 <= 31 + i0)) or "
1599 "i0 >= 0 }" },
1602 /* A specialized coalescing test case that would result
1603 * in a segmentation fault or a failed assertion in earlier versions of isl.
1605 static int test_coalesce_special(struct isl_ctx *ctx)
1607 const char *str;
1608 isl_map *map1, *map2;
1610 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1611 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1612 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1613 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1614 "o1 <= 239 and o1 >= 212)) or "
1615 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1616 "o1 <= 241 and o1 >= 240));"
1617 "[S_L220_OUT[] -> T7[]] -> "
1618 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1619 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1620 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1621 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1622 map1 = isl_map_read_from_str(ctx, str);
1623 map1 = isl_map_align_divs(map1);
1624 map1 = isl_map_coalesce(map1);
1625 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1626 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1627 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1628 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1629 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1630 map2 = isl_map_read_from_str(ctx, str);
1631 map2 = isl_map_union(map2, map1);
1632 map2 = isl_map_align_divs(map2);
1633 map2 = isl_map_coalesce(map2);
1634 isl_map_free(map2);
1635 if (!map2)
1636 return -1;
1638 return 0;
1641 /* Test the functionality of isl_set_coalesce.
1642 * That is, check that the output is always equal to the input
1643 * and in some cases that the result consists of a single disjunct.
1645 static int test_coalesce(struct isl_ctx *ctx)
1647 int i;
1649 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1650 const char *str = coalesce_tests[i].str;
1651 int check_one = coalesce_tests[i].single_disjunct;
1652 if (test_coalesce_set(ctx, str, check_one) < 0)
1653 return -1;
1656 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1657 return -1;
1658 if (test_coalesce_special(ctx) < 0)
1659 return -1;
1661 return 0;
1664 static int test_closure(isl_ctx *ctx)
1666 const char *str;
1667 isl_set *dom;
1668 isl_map *up, *right;
1669 isl_map *map, *map2;
1670 int exact;
1672 /* COCOA example 1 */
1673 map = isl_map_read_from_str(ctx,
1674 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1675 "1 <= i and i < n and 1 <= j and j < n or "
1676 "i2 = i + 1 and j2 = j - 1 and "
1677 "1 <= i and i < n and 2 <= j and j <= n }");
1678 map = isl_map_power(map, &exact);
1679 assert(exact);
1680 isl_map_free(map);
1682 /* COCOA example 1 */
1683 map = isl_map_read_from_str(ctx,
1684 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1685 "1 <= i and i < n and 1 <= j and j < n or "
1686 "i2 = i + 1 and j2 = j - 1 and "
1687 "1 <= i and i < n and 2 <= j and j <= n }");
1688 map = isl_map_transitive_closure(map, &exact);
1689 assert(exact);
1690 map2 = isl_map_read_from_str(ctx,
1691 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1692 "1 <= i and i < n and 1 <= j and j <= n and "
1693 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1694 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1695 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1696 assert(isl_map_is_equal(map, map2));
1697 isl_map_free(map2);
1698 isl_map_free(map);
1700 map = isl_map_read_from_str(ctx,
1701 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1702 " 0 <= y and y <= n }");
1703 map = isl_map_transitive_closure(map, &exact);
1704 map2 = isl_map_read_from_str(ctx,
1705 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1706 " 0 <= y and y <= n }");
1707 assert(isl_map_is_equal(map, map2));
1708 isl_map_free(map2);
1709 isl_map_free(map);
1711 /* COCOA example 2 */
1712 map = isl_map_read_from_str(ctx,
1713 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1714 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1715 "i2 = i + 2 and j2 = j - 2 and "
1716 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1717 map = isl_map_transitive_closure(map, &exact);
1718 assert(exact);
1719 map2 = isl_map_read_from_str(ctx,
1720 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1721 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1722 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1723 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1724 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1725 assert(isl_map_is_equal(map, map2));
1726 isl_map_free(map);
1727 isl_map_free(map2);
1729 /* COCOA Fig.2 left */
1730 map = isl_map_read_from_str(ctx,
1731 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1732 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1733 "j <= n or "
1734 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1735 "j <= 2 i - 3 and j <= n - 2 or "
1736 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1737 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1738 map = isl_map_transitive_closure(map, &exact);
1739 assert(exact);
1740 isl_map_free(map);
1742 /* COCOA Fig.2 right */
1743 map = isl_map_read_from_str(ctx,
1744 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1745 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1746 "j <= n or "
1747 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1748 "j <= 2 i - 4 and j <= n - 3 or "
1749 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1750 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1751 map = isl_map_power(map, &exact);
1752 assert(exact);
1753 isl_map_free(map);
1755 /* COCOA Fig.2 right */
1756 map = isl_map_read_from_str(ctx,
1757 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1758 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1759 "j <= n or "
1760 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1761 "j <= 2 i - 4 and j <= n - 3 or "
1762 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1763 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1764 map = isl_map_transitive_closure(map, &exact);
1765 assert(exact);
1766 map2 = isl_map_read_from_str(ctx,
1767 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1768 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1769 "j <= n and 3 + i + 2 j <= 3 n and "
1770 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1771 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1772 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1773 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1774 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1775 assert(isl_map_is_equal(map, map2));
1776 isl_map_free(map2);
1777 isl_map_free(map);
1779 /* COCOA Fig.1 right */
1780 dom = isl_set_read_from_str(ctx,
1781 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1782 "2 x - 3 y + 3 >= 0 }");
1783 right = isl_map_read_from_str(ctx,
1784 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1785 up = isl_map_read_from_str(ctx,
1786 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1787 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1788 right = isl_map_intersect_range(right, isl_set_copy(dom));
1789 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1790 up = isl_map_intersect_range(up, dom);
1791 map = isl_map_union(up, right);
1792 map = isl_map_transitive_closure(map, &exact);
1793 assert(exact);
1794 map2 = isl_map_read_from_str(ctx,
1795 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1796 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1797 assert(isl_map_is_equal(map, map2));
1798 isl_map_free(map2);
1799 isl_map_free(map);
1801 /* COCOA Theorem 1 counter example */
1802 map = isl_map_read_from_str(ctx,
1803 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1804 "i2 = 1 and j2 = j or "
1805 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1806 map = isl_map_transitive_closure(map, &exact);
1807 assert(exact);
1808 isl_map_free(map);
1810 map = isl_map_read_from_str(ctx,
1811 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1812 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1813 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1814 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1815 map = isl_map_transitive_closure(map, &exact);
1816 assert(exact);
1817 isl_map_free(map);
1819 /* Kelly et al 1996, fig 12 */
1820 map = isl_map_read_from_str(ctx,
1821 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1822 "1 <= i,j,j+1 <= n or "
1823 "j = n and j2 = 1 and i2 = i + 1 and "
1824 "1 <= i,i+1 <= n }");
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] : 1 <= j < j2 <= n and "
1829 "1 <= i <= n and i = i2 or "
1830 "1 <= i < i2 <= n and 1 <= j <= n and "
1831 "1 <= j2 <= n }");
1832 assert(isl_map_is_equal(map, map2));
1833 isl_map_free(map2);
1834 isl_map_free(map);
1836 /* Omega's closure4 */
1837 map = isl_map_read_from_str(ctx,
1838 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1839 "1 <= x,y <= 10 or "
1840 "x2 = x + 1 and y2 = y and "
1841 "1 <= x <= 20 && 5 <= y <= 15 }");
1842 map = isl_map_transitive_closure(map, &exact);
1843 assert(exact);
1844 isl_map_free(map);
1846 map = isl_map_read_from_str(ctx,
1847 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1848 map = isl_map_transitive_closure(map, &exact);
1849 assert(!exact);
1850 map2 = isl_map_read_from_str(ctx,
1851 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1852 assert(isl_map_is_equal(map, map2));
1853 isl_map_free(map);
1854 isl_map_free(map2);
1856 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1857 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1858 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1859 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1860 map = isl_map_read_from_str(ctx, str);
1861 map = isl_map_transitive_closure(map, &exact);
1862 assert(exact);
1863 map2 = isl_map_read_from_str(ctx, str);
1864 assert(isl_map_is_equal(map, map2));
1865 isl_map_free(map);
1866 isl_map_free(map2);
1868 str = "{[0] -> [1]; [2] -> [3]}";
1869 map = isl_map_read_from_str(ctx, str);
1870 map = isl_map_transitive_closure(map, &exact);
1871 assert(exact);
1872 map2 = isl_map_read_from_str(ctx, str);
1873 assert(isl_map_is_equal(map, map2));
1874 isl_map_free(map);
1875 isl_map_free(map2);
1877 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1878 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1879 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1880 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1881 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1882 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1883 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1884 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1885 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1886 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1887 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1888 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1889 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1890 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1891 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1892 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1893 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1894 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1895 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1896 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1897 map = isl_map_read_from_str(ctx, str);
1898 map = isl_map_transitive_closure(map, NULL);
1899 assert(map);
1900 isl_map_free(map);
1902 return 0;
1905 static int test_lex(struct isl_ctx *ctx)
1907 isl_space *dim;
1908 isl_map *map;
1909 int empty;
1911 dim = isl_space_set_alloc(ctx, 0, 0);
1912 map = isl_map_lex_le(dim);
1913 empty = isl_map_is_empty(map);
1914 isl_map_free(map);
1916 if (empty < 0)
1917 return -1;
1918 if (empty)
1919 isl_die(ctx, isl_error_unknown,
1920 "expecting non-empty result", return -1);
1922 return 0;
1925 static int test_lexmin(struct isl_ctx *ctx)
1927 int equal;
1928 const char *str;
1929 isl_basic_map *bmap;
1930 isl_map *map, *map2;
1931 isl_set *set;
1932 isl_set *set2;
1933 isl_pw_multi_aff *pma;
1935 str = "[p0, p1] -> { [] -> [] : "
1936 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1937 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1938 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1939 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1940 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1941 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1942 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1943 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1944 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1945 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1946 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1947 map = isl_map_read_from_str(ctx, str);
1948 map = isl_map_lexmin(map);
1949 isl_map_free(map);
1951 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1952 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1953 set = isl_set_read_from_str(ctx, str);
1954 set = isl_set_lexmax(set);
1955 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1956 set2 = isl_set_read_from_str(ctx, str);
1957 set = isl_set_intersect(set, set2);
1958 assert(!isl_set_is_empty(set));
1959 isl_set_free(set);
1961 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1962 map = isl_map_read_from_str(ctx, str);
1963 map = isl_map_lexmin(map);
1964 str = "{ [x] -> [5] : 6 <= x <= 8; "
1965 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1966 map2 = isl_map_read_from_str(ctx, str);
1967 assert(isl_map_is_equal(map, map2));
1968 isl_map_free(map);
1969 isl_map_free(map2);
1971 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1972 map = isl_map_read_from_str(ctx, str);
1973 map2 = isl_map_copy(map);
1974 map = isl_map_lexmin(map);
1975 assert(isl_map_is_equal(map, map2));
1976 isl_map_free(map);
1977 isl_map_free(map2);
1979 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1980 map = isl_map_read_from_str(ctx, str);
1981 map = isl_map_lexmin(map);
1982 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1983 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1984 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1985 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1986 map2 = isl_map_read_from_str(ctx, str);
1987 assert(isl_map_is_equal(map, map2));
1988 isl_map_free(map);
1989 isl_map_free(map2);
1991 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1992 " 8i' <= i and 8i' >= -7 + i }";
1993 bmap = isl_basic_map_read_from_str(ctx, str);
1994 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1995 map2 = isl_map_from_pw_multi_aff(pma);
1996 map = isl_map_from_basic_map(bmap);
1997 assert(isl_map_is_equal(map, map2));
1998 isl_map_free(map);
1999 isl_map_free(map2);
2001 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2002 map = isl_map_read_from_str(ctx, str);
2003 map = isl_map_lexmin(map);
2004 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2005 map2 = isl_map_read_from_str(ctx, str);
2006 assert(isl_map_is_equal(map, map2));
2007 isl_map_free(map);
2008 isl_map_free(map2);
2010 /* Check that empty pieces are properly combined. */
2011 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2012 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2013 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2014 map = isl_map_read_from_str(ctx, str);
2015 map = isl_map_lexmin(map);
2016 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2017 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2018 "x >= 4 }";
2019 map2 = isl_map_read_from_str(ctx, str);
2020 assert(isl_map_is_equal(map, map2));
2021 isl_map_free(map);
2022 isl_map_free(map2);
2024 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2025 " 8i' <= i and 8i' >= -7 + i }";
2026 set = isl_set_read_from_str(ctx, str);
2027 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2028 set2 = isl_set_from_pw_multi_aff(pma);
2029 equal = isl_set_is_equal(set, set2);
2030 isl_set_free(set);
2031 isl_set_free(set2);
2032 if (equal < 0)
2033 return -1;
2034 if (!equal)
2035 isl_die(ctx, isl_error_unknown,
2036 "unexpected difference between set and "
2037 "piecewise affine expression", return -1);
2039 return 0;
2042 /* Check that isl_set_min_val and isl_set_max_val compute the correct
2043 * result on non-convex inputs.
2045 static int test_min(struct isl_ctx *ctx)
2047 isl_set *set;
2048 isl_aff *aff;
2049 isl_val *val;
2050 int min_ok, max_ok;
2052 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
2053 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
2054 val = isl_set_min_val(set, aff);
2055 min_ok = isl_val_is_negone(val);
2056 isl_val_free(val);
2057 val = isl_set_max_val(set, aff);
2058 max_ok = isl_val_is_one(val);
2059 isl_val_free(val);
2060 isl_aff_free(aff);
2061 isl_set_free(set);
2063 if (min_ok < 0 || max_ok < 0)
2064 return -1;
2065 if (!min_ok)
2066 isl_die(ctx, isl_error_unknown,
2067 "unexpected minimum", return -1);
2068 if (!max_ok)
2069 isl_die(ctx, isl_error_unknown,
2070 "unexpected maximum", return -1);
2072 return 0;
2075 struct must_may {
2076 isl_map *must;
2077 isl_map *may;
2080 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2081 void *dep_user, void *user)
2083 struct must_may *mm = (struct must_may *)user;
2085 if (must)
2086 mm->must = isl_map_union(mm->must, dep);
2087 else
2088 mm->may = isl_map_union(mm->may, dep);
2090 return isl_stat_ok;
2093 static int common_space(void *first, void *second)
2095 int depth = *(int *)first;
2096 return 2 * depth;
2099 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2101 isl_map *map2;
2102 int equal;
2104 if (!map)
2105 return -1;
2107 map2 = isl_map_read_from_str(map->ctx, str);
2108 equal = isl_map_is_equal(map, map2);
2109 isl_map_free(map2);
2111 return equal;
2114 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2116 int equal;
2118 equal = map_is_equal(map, str);
2119 if (equal < 0)
2120 return -1;
2121 if (!equal)
2122 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2123 "result not as expected", return -1);
2124 return 0;
2127 static int test_dep(struct isl_ctx *ctx)
2129 const char *str;
2130 isl_space *dim;
2131 isl_map *map;
2132 isl_access_info *ai;
2133 isl_flow *flow;
2134 int depth;
2135 struct must_may mm;
2137 depth = 3;
2139 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2140 map = isl_map_read_from_str(ctx, str);
2141 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2143 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2144 map = isl_map_read_from_str(ctx, str);
2145 ai = isl_access_info_add_source(ai, map, 1, &depth);
2147 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2148 map = isl_map_read_from_str(ctx, str);
2149 ai = isl_access_info_add_source(ai, map, 1, &depth);
2151 flow = isl_access_info_compute_flow(ai);
2152 dim = isl_space_alloc(ctx, 0, 3, 3);
2153 mm.must = isl_map_empty(isl_space_copy(dim));
2154 mm.may = isl_map_empty(dim);
2156 isl_flow_foreach(flow, collect_must_may, &mm);
2158 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2159 " [1,10,0] -> [2,5,0] }";
2160 assert(map_is_equal(mm.must, str));
2161 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2162 assert(map_is_equal(mm.may, str));
2164 isl_map_free(mm.must);
2165 isl_map_free(mm.may);
2166 isl_flow_free(flow);
2169 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2170 map = isl_map_read_from_str(ctx, str);
2171 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2173 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2174 map = isl_map_read_from_str(ctx, str);
2175 ai = isl_access_info_add_source(ai, map, 1, &depth);
2177 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2178 map = isl_map_read_from_str(ctx, str);
2179 ai = isl_access_info_add_source(ai, map, 0, &depth);
2181 flow = isl_access_info_compute_flow(ai);
2182 dim = isl_space_alloc(ctx, 0, 3, 3);
2183 mm.must = isl_map_empty(isl_space_copy(dim));
2184 mm.may = isl_map_empty(dim);
2186 isl_flow_foreach(flow, collect_must_may, &mm);
2188 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2189 assert(map_is_equal(mm.must, str));
2190 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2191 assert(map_is_equal(mm.may, str));
2193 isl_map_free(mm.must);
2194 isl_map_free(mm.may);
2195 isl_flow_free(flow);
2198 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2199 map = isl_map_read_from_str(ctx, str);
2200 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2202 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2203 map = isl_map_read_from_str(ctx, str);
2204 ai = isl_access_info_add_source(ai, map, 0, &depth);
2206 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2207 map = isl_map_read_from_str(ctx, str);
2208 ai = isl_access_info_add_source(ai, map, 0, &depth);
2210 flow = isl_access_info_compute_flow(ai);
2211 dim = isl_space_alloc(ctx, 0, 3, 3);
2212 mm.must = isl_map_empty(isl_space_copy(dim));
2213 mm.may = isl_map_empty(dim);
2215 isl_flow_foreach(flow, collect_must_may, &mm);
2217 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2218 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2219 assert(map_is_equal(mm.may, str));
2220 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2221 assert(map_is_equal(mm.must, str));
2223 isl_map_free(mm.must);
2224 isl_map_free(mm.may);
2225 isl_flow_free(flow);
2228 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2229 map = isl_map_read_from_str(ctx, str);
2230 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2232 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2233 map = isl_map_read_from_str(ctx, str);
2234 ai = isl_access_info_add_source(ai, map, 0, &depth);
2236 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2237 map = isl_map_read_from_str(ctx, str);
2238 ai = isl_access_info_add_source(ai, map, 0, &depth);
2240 flow = isl_access_info_compute_flow(ai);
2241 dim = isl_space_alloc(ctx, 0, 3, 3);
2242 mm.must = isl_map_empty(isl_space_copy(dim));
2243 mm.may = isl_map_empty(dim);
2245 isl_flow_foreach(flow, collect_must_may, &mm);
2247 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2248 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2249 assert(map_is_equal(mm.may, str));
2250 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2251 assert(map_is_equal(mm.must, str));
2253 isl_map_free(mm.must);
2254 isl_map_free(mm.may);
2255 isl_flow_free(flow);
2258 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2259 map = isl_map_read_from_str(ctx, str);
2260 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2262 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2263 map = isl_map_read_from_str(ctx, str);
2264 ai = isl_access_info_add_source(ai, map, 0, &depth);
2266 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2267 map = isl_map_read_from_str(ctx, str);
2268 ai = isl_access_info_add_source(ai, map, 0, &depth);
2270 flow = isl_access_info_compute_flow(ai);
2271 dim = isl_space_alloc(ctx, 0, 3, 3);
2272 mm.must = isl_map_empty(isl_space_copy(dim));
2273 mm.may = isl_map_empty(dim);
2275 isl_flow_foreach(flow, collect_must_may, &mm);
2277 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2278 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2279 assert(map_is_equal(mm.may, str));
2280 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2281 assert(map_is_equal(mm.must, str));
2283 isl_map_free(mm.must);
2284 isl_map_free(mm.may);
2285 isl_flow_free(flow);
2288 depth = 5;
2290 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2291 map = isl_map_read_from_str(ctx, str);
2292 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2294 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2295 map = isl_map_read_from_str(ctx, str);
2296 ai = isl_access_info_add_source(ai, map, 1, &depth);
2298 flow = isl_access_info_compute_flow(ai);
2299 dim = isl_space_alloc(ctx, 0, 5, 5);
2300 mm.must = isl_map_empty(isl_space_copy(dim));
2301 mm.may = isl_map_empty(dim);
2303 isl_flow_foreach(flow, collect_must_may, &mm);
2305 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2306 assert(map_is_equal(mm.must, str));
2307 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2308 assert(map_is_equal(mm.may, str));
2310 isl_map_free(mm.must);
2311 isl_map_free(mm.may);
2312 isl_flow_free(flow);
2314 return 0;
2317 /* Check that the dependence analysis proceeds without errors.
2318 * Earlier versions of isl would break down during the analysis
2319 * due to the use of the wrong spaces.
2321 static int test_flow(isl_ctx *ctx)
2323 const char *str;
2324 isl_union_map *access, *schedule;
2325 isl_union_map *must_dep, *may_dep;
2326 int r;
2328 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2329 access = isl_union_map_read_from_str(ctx, str);
2330 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2331 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2332 "S2[] -> [1,0,0,0]; "
2333 "S3[] -> [-1,0,0,0] }";
2334 schedule = isl_union_map_read_from_str(ctx, str);
2335 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2336 isl_union_map_copy(access), schedule,
2337 &must_dep, &may_dep, NULL, NULL);
2338 isl_union_map_free(may_dep);
2339 isl_union_map_free(must_dep);
2341 return r;
2344 struct {
2345 const char *map;
2346 int sv;
2347 } sv_tests[] = {
2348 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2349 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2350 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2351 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2352 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2353 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2354 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2355 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2356 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2359 int test_sv(isl_ctx *ctx)
2361 isl_union_map *umap;
2362 int i;
2363 int sv;
2365 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2366 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2367 sv = isl_union_map_is_single_valued(umap);
2368 isl_union_map_free(umap);
2369 if (sv < 0)
2370 return -1;
2371 if (sv_tests[i].sv && !sv)
2372 isl_die(ctx, isl_error_internal,
2373 "map not detected as single valued", return -1);
2374 if (!sv_tests[i].sv && sv)
2375 isl_die(ctx, isl_error_internal,
2376 "map detected as single valued", return -1);
2379 return 0;
2382 struct {
2383 const char *str;
2384 int bijective;
2385 } bijective_tests[] = {
2386 { "[N,M]->{[i,j] -> [i]}", 0 },
2387 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2388 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2389 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2390 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2391 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2392 { "[N,M]->{[i,j] -> []}", 0 },
2393 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2394 { "[N,M]->{[i,j] -> [2i]}", 0 },
2395 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2396 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2397 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2398 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2401 static int test_bijective(struct isl_ctx *ctx)
2403 isl_map *map;
2404 int i;
2405 int bijective;
2407 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2408 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2409 bijective = isl_map_is_bijective(map);
2410 isl_map_free(map);
2411 if (bijective < 0)
2412 return -1;
2413 if (bijective_tests[i].bijective && !bijective)
2414 isl_die(ctx, isl_error_internal,
2415 "map not detected as bijective", return -1);
2416 if (!bijective_tests[i].bijective && bijective)
2417 isl_die(ctx, isl_error_internal,
2418 "map detected as bijective", return -1);
2421 return 0;
2424 /* Inputs for isl_pw_qpolynomial_gist tests.
2425 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2427 struct {
2428 const char *pwqp;
2429 const char *set;
2430 const char *gist;
2431 } pwqp_gist_tests[] = {
2432 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2433 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2434 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2435 "{ [i] -> -1/2 + 1/2 * i }" },
2436 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2439 static int test_pwqp(struct isl_ctx *ctx)
2441 int i;
2442 const char *str;
2443 isl_set *set;
2444 isl_pw_qpolynomial *pwqp1, *pwqp2;
2445 int equal;
2447 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2448 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2450 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2451 isl_dim_in, 1, 1);
2453 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2454 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2456 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2458 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2460 isl_pw_qpolynomial_free(pwqp1);
2462 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2463 str = pwqp_gist_tests[i].pwqp;
2464 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2465 str = pwqp_gist_tests[i].set;
2466 set = isl_set_read_from_str(ctx, str);
2467 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2468 str = pwqp_gist_tests[i].gist;
2469 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2470 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2471 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2472 isl_pw_qpolynomial_free(pwqp1);
2474 if (equal < 0)
2475 return -1;
2476 if (!equal)
2477 isl_die(ctx, isl_error_unknown,
2478 "unexpected result", return -1);
2481 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2482 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2483 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2484 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2486 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2488 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2490 isl_pw_qpolynomial_free(pwqp1);
2492 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2493 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2494 str = "{ [x] -> x }";
2495 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2497 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2499 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2501 isl_pw_qpolynomial_free(pwqp1);
2503 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2504 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2505 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2506 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2507 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2508 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2509 isl_pw_qpolynomial_free(pwqp1);
2511 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2512 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2513 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2514 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2515 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2516 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2517 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2518 isl_pw_qpolynomial_free(pwqp1);
2519 isl_pw_qpolynomial_free(pwqp2);
2520 if (equal < 0)
2521 return -1;
2522 if (!equal)
2523 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2525 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2526 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2527 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2528 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2529 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2530 isl_val_one(ctx));
2531 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2532 isl_pw_qpolynomial_free(pwqp1);
2533 isl_pw_qpolynomial_free(pwqp2);
2534 if (equal < 0)
2535 return -1;
2536 if (!equal)
2537 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2539 return 0;
2542 static int test_split_periods(isl_ctx *ctx)
2544 const char *str;
2545 isl_pw_qpolynomial *pwqp;
2547 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2548 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2549 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2550 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2552 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2554 isl_pw_qpolynomial_free(pwqp);
2556 if (!pwqp)
2557 return -1;
2559 return 0;
2562 static int test_union(isl_ctx *ctx)
2564 const char *str;
2565 isl_union_set *uset1, *uset2;
2566 isl_union_map *umap1, *umap2;
2567 int equal;
2569 str = "{ [i] : 0 <= i <= 1 }";
2570 uset1 = isl_union_set_read_from_str(ctx, str);
2571 str = "{ [1] -> [0] }";
2572 umap1 = isl_union_map_read_from_str(ctx, str);
2574 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2575 equal = isl_union_map_is_equal(umap1, umap2);
2577 isl_union_map_free(umap1);
2578 isl_union_map_free(umap2);
2580 if (equal < 0)
2581 return -1;
2582 if (!equal)
2583 isl_die(ctx, isl_error_unknown, "union maps not equal",
2584 return -1);
2586 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2587 umap1 = isl_union_map_read_from_str(ctx, str);
2588 str = "{ A[i]; B[i] }";
2589 uset1 = isl_union_set_read_from_str(ctx, str);
2591 uset2 = isl_union_map_domain(umap1);
2593 equal = isl_union_set_is_equal(uset1, uset2);
2595 isl_union_set_free(uset1);
2596 isl_union_set_free(uset2);
2598 if (equal < 0)
2599 return -1;
2600 if (!equal)
2601 isl_die(ctx, isl_error_unknown, "union sets not equal",
2602 return -1);
2604 return 0;
2607 /* Check that computing a bound of a non-zero polynomial over an unbounded
2608 * domain does not produce a rational value.
2609 * Ideally, we want the value to be infinity, but we accept NaN for now.
2610 * We certainly do not want to obtain the value zero.
2612 static int test_bound_unbounded_domain(isl_ctx *ctx)
2614 const char *str;
2615 isl_set *dom;
2616 isl_point *pnt;
2617 isl_pw_qpolynomial *pwqp;
2618 isl_pw_qpolynomial_fold *pwf;
2619 isl_val *v;
2620 int is_rat;
2622 str = "{ [m,n] -> -m * n }";
2623 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2624 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2625 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
2626 pnt = isl_set_sample_point(dom);
2627 v = isl_pw_qpolynomial_fold_eval(pwf, pnt);
2628 is_rat = isl_val_is_rat(v);
2629 isl_val_free(v);
2631 if (is_rat < 0)
2632 return -1;
2633 if (is_rat)
2634 isl_die(ctx, isl_error_unknown,
2635 "unexpected rational value", return -1);
2637 return 0;
2640 static int test_bound(isl_ctx *ctx)
2642 const char *str;
2643 unsigned dim;
2644 isl_pw_qpolynomial *pwqp;
2645 isl_pw_qpolynomial_fold *pwf;
2647 if (test_bound_unbounded_domain(ctx) < 0)
2648 return -1;
2650 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2651 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2652 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2653 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2654 isl_pw_qpolynomial_fold_free(pwf);
2655 if (dim != 4)
2656 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2657 return -1);
2659 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2660 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2661 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2662 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2663 isl_pw_qpolynomial_fold_free(pwf);
2664 if (dim != 1)
2665 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2666 return -1);
2668 return 0;
2671 static int test_lift(isl_ctx *ctx)
2673 const char *str;
2674 isl_basic_map *bmap;
2675 isl_basic_set *bset;
2677 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2678 bset = isl_basic_set_read_from_str(ctx, str);
2679 bset = isl_basic_set_lift(bset);
2680 bmap = isl_basic_map_from_range(bset);
2681 bset = isl_basic_map_domain(bmap);
2682 isl_basic_set_free(bset);
2684 return 0;
2687 struct {
2688 const char *set1;
2689 const char *set2;
2690 int subset;
2691 } subset_tests[] = {
2692 { "{ [112, 0] }",
2693 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2694 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2695 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2696 { "{ [65] }",
2697 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2698 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2699 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2700 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2701 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2702 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2703 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2704 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2705 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2706 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2707 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2708 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2709 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2710 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2711 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2712 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2713 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2714 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2715 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2716 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2717 "4e0 <= -57 + i0 + i1)) or "
2718 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2719 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2720 "4e0 >= -61 + i0 + i1)) or "
2721 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2724 static int test_subset(isl_ctx *ctx)
2726 int i;
2727 isl_set *set1, *set2;
2728 int subset;
2730 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2731 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2732 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2733 subset = isl_set_is_subset(set1, set2);
2734 isl_set_free(set1);
2735 isl_set_free(set2);
2736 if (subset < 0)
2737 return -1;
2738 if (subset != subset_tests[i].subset)
2739 isl_die(ctx, isl_error_unknown,
2740 "incorrect subset result", return -1);
2743 return 0;
2746 struct {
2747 const char *minuend;
2748 const char *subtrahend;
2749 const char *difference;
2750 } subtract_domain_tests[] = {
2751 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2752 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2753 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2756 static int test_subtract(isl_ctx *ctx)
2758 int i;
2759 isl_union_map *umap1, *umap2;
2760 isl_union_pw_multi_aff *upma1, *upma2;
2761 isl_union_set *uset;
2762 int equal;
2764 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2765 umap1 = isl_union_map_read_from_str(ctx,
2766 subtract_domain_tests[i].minuend);
2767 uset = isl_union_set_read_from_str(ctx,
2768 subtract_domain_tests[i].subtrahend);
2769 umap2 = isl_union_map_read_from_str(ctx,
2770 subtract_domain_tests[i].difference);
2771 umap1 = isl_union_map_subtract_domain(umap1, uset);
2772 equal = isl_union_map_is_equal(umap1, umap2);
2773 isl_union_map_free(umap1);
2774 isl_union_map_free(umap2);
2775 if (equal < 0)
2776 return -1;
2777 if (!equal)
2778 isl_die(ctx, isl_error_unknown,
2779 "incorrect subtract domain result", return -1);
2782 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2783 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2784 subtract_domain_tests[i].minuend);
2785 uset = isl_union_set_read_from_str(ctx,
2786 subtract_domain_tests[i].subtrahend);
2787 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2788 subtract_domain_tests[i].difference);
2789 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2790 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2791 isl_union_pw_multi_aff_free(upma1);
2792 isl_union_pw_multi_aff_free(upma2);
2793 if (equal < 0)
2794 return -1;
2795 if (!equal)
2796 isl_die(ctx, isl_error_unknown,
2797 "incorrect subtract domain result", return -1);
2800 return 0;
2803 int test_factorize(isl_ctx *ctx)
2805 const char *str;
2806 isl_basic_set *bset;
2807 isl_factorizer *f;
2809 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2810 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2811 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2812 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2813 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2814 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2815 "3i5 >= -2i0 - i2 + 3i4 }";
2816 bset = isl_basic_set_read_from_str(ctx, str);
2817 f = isl_basic_set_factorizer(bset);
2818 isl_basic_set_free(bset);
2819 isl_factorizer_free(f);
2820 if (!f)
2821 isl_die(ctx, isl_error_unknown,
2822 "failed to construct factorizer", return -1);
2824 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2825 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2826 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2827 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2828 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2829 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2830 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2831 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2832 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2833 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2834 bset = isl_basic_set_read_from_str(ctx, str);
2835 f = isl_basic_set_factorizer(bset);
2836 isl_basic_set_free(bset);
2837 isl_factorizer_free(f);
2838 if (!f)
2839 isl_die(ctx, isl_error_unknown,
2840 "failed to construct factorizer", return -1);
2842 return 0;
2845 static isl_stat check_injective(__isl_take isl_map *map, void *user)
2847 int *injective = user;
2849 *injective = isl_map_is_injective(map);
2850 isl_map_free(map);
2852 if (*injective < 0 || !*injective)
2853 return isl_stat_error;
2855 return isl_stat_ok;
2858 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2859 const char *r, const char *s, int tilable, int parallel)
2861 int i;
2862 isl_union_set *D;
2863 isl_union_map *W, *R, *S;
2864 isl_union_map *empty;
2865 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2866 isl_union_map *validity, *proximity, *coincidence;
2867 isl_union_map *schedule;
2868 isl_union_map *test;
2869 isl_union_set *delta;
2870 isl_union_set *domain;
2871 isl_set *delta_set;
2872 isl_set *slice;
2873 isl_set *origin;
2874 isl_schedule_constraints *sc;
2875 isl_schedule *sched;
2876 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2878 D = isl_union_set_read_from_str(ctx, d);
2879 W = isl_union_map_read_from_str(ctx, w);
2880 R = isl_union_map_read_from_str(ctx, r);
2881 S = isl_union_map_read_from_str(ctx, s);
2883 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2884 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2886 empty = isl_union_map_empty(isl_union_map_get_space(S));
2887 isl_union_map_compute_flow(isl_union_map_copy(R),
2888 isl_union_map_copy(W), empty,
2889 isl_union_map_copy(S),
2890 &dep_raw, NULL, NULL, NULL);
2891 isl_union_map_compute_flow(isl_union_map_copy(W),
2892 isl_union_map_copy(W),
2893 isl_union_map_copy(R),
2894 isl_union_map_copy(S),
2895 &dep_waw, &dep_war, NULL, NULL);
2897 dep = isl_union_map_union(dep_waw, dep_war);
2898 dep = isl_union_map_union(dep, dep_raw);
2899 validity = isl_union_map_copy(dep);
2900 coincidence = isl_union_map_copy(dep);
2901 proximity = isl_union_map_copy(dep);
2903 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2904 sc = isl_schedule_constraints_set_validity(sc, validity);
2905 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2906 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2907 sched = isl_schedule_constraints_compute_schedule(sc);
2908 schedule = isl_schedule_get_map(sched);
2909 isl_schedule_free(sched);
2910 isl_union_map_free(W);
2911 isl_union_map_free(R);
2912 isl_union_map_free(S);
2914 is_injection = 1;
2915 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2917 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2918 is_complete = isl_union_set_is_subset(D, domain);
2919 isl_union_set_free(D);
2920 isl_union_set_free(domain);
2922 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2923 test = isl_union_map_apply_range(test, dep);
2924 test = isl_union_map_apply_range(test, schedule);
2926 delta = isl_union_map_deltas(test);
2927 if (isl_union_set_n_set(delta) == 0) {
2928 is_tilable = 1;
2929 is_parallel = 1;
2930 is_nonneg = 1;
2931 isl_union_set_free(delta);
2932 } else {
2933 delta_set = isl_set_from_union_set(delta);
2935 slice = isl_set_universe(isl_set_get_space(delta_set));
2936 for (i = 0; i < tilable; ++i)
2937 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2938 is_tilable = isl_set_is_subset(delta_set, slice);
2939 isl_set_free(slice);
2941 slice = isl_set_universe(isl_set_get_space(delta_set));
2942 for (i = 0; i < parallel; ++i)
2943 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2944 is_parallel = isl_set_is_subset(delta_set, slice);
2945 isl_set_free(slice);
2947 origin = isl_set_universe(isl_set_get_space(delta_set));
2948 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2949 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2951 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2952 delta_set = isl_set_lexmin(delta_set);
2954 is_nonneg = isl_set_is_equal(delta_set, origin);
2956 isl_set_free(origin);
2957 isl_set_free(delta_set);
2960 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2961 is_injection < 0 || is_complete < 0)
2962 return -1;
2963 if (!is_complete)
2964 isl_die(ctx, isl_error_unknown,
2965 "generated schedule incomplete", return -1);
2966 if (!is_injection)
2967 isl_die(ctx, isl_error_unknown,
2968 "generated schedule not injective on each statement",
2969 return -1);
2970 if (!is_nonneg)
2971 isl_die(ctx, isl_error_unknown,
2972 "negative dependences in generated schedule",
2973 return -1);
2974 if (!is_tilable)
2975 isl_die(ctx, isl_error_unknown,
2976 "generated schedule not as tilable as expected",
2977 return -1);
2978 if (!is_parallel)
2979 isl_die(ctx, isl_error_unknown,
2980 "generated schedule not as parallel as expected",
2981 return -1);
2983 return 0;
2986 /* Compute a schedule for the given instance set, validity constraints,
2987 * proximity constraints and context and return a corresponding union map
2988 * representation.
2990 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
2991 const char *domain, const char *validity, const char *proximity,
2992 const char *context)
2994 isl_set *con;
2995 isl_union_set *dom;
2996 isl_union_map *dep;
2997 isl_union_map *prox;
2998 isl_schedule_constraints *sc;
2999 isl_schedule *schedule;
3000 isl_union_map *sched;
3002 con = isl_set_read_from_str(ctx, context);
3003 dom = isl_union_set_read_from_str(ctx, domain);
3004 dep = isl_union_map_read_from_str(ctx, validity);
3005 prox = isl_union_map_read_from_str(ctx, proximity);
3006 sc = isl_schedule_constraints_on_domain(dom);
3007 sc = isl_schedule_constraints_set_context(sc, con);
3008 sc = isl_schedule_constraints_set_validity(sc, dep);
3009 sc = isl_schedule_constraints_set_proximity(sc, prox);
3010 schedule = isl_schedule_constraints_compute_schedule(sc);
3011 sched = isl_schedule_get_map(schedule);
3012 isl_schedule_free(schedule);
3014 return sched;
3017 /* Compute a schedule for the given instance set, validity constraints and
3018 * proximity constraints and return a corresponding union map representation.
3020 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3021 const char *domain, const char *validity, const char *proximity)
3023 return compute_schedule_with_context(ctx, domain, validity, proximity,
3024 "{ : }");
3027 /* Check that a schedule can be constructed on the given domain
3028 * with the given validity and proximity constraints.
3030 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3031 const char *validity, const char *proximity)
3033 isl_union_map *sched;
3035 sched = compute_schedule(ctx, domain, validity, proximity);
3036 if (!sched)
3037 return -1;
3039 isl_union_map_free(sched);
3040 return 0;
3043 int test_special_schedule(isl_ctx *ctx, const char *domain,
3044 const char *validity, const char *proximity, const char *expected_sched)
3046 isl_union_map *sched1, *sched2;
3047 int equal;
3049 sched1 = compute_schedule(ctx, domain, validity, proximity);
3050 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3052 equal = isl_union_map_is_equal(sched1, sched2);
3053 isl_union_map_free(sched1);
3054 isl_union_map_free(sched2);
3056 if (equal < 0)
3057 return -1;
3058 if (!equal)
3059 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3060 return -1);
3062 return 0;
3065 /* Check that the schedule map is properly padded, even after being
3066 * reconstructed from the band forest.
3068 static int test_padded_schedule(isl_ctx *ctx)
3070 const char *str;
3071 isl_union_set *D;
3072 isl_union_map *validity, *proximity;
3073 isl_schedule_constraints *sc;
3074 isl_schedule *sched;
3075 isl_union_map *map1, *map2;
3076 isl_band_list *list;
3077 int equal;
3079 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3080 D = isl_union_set_read_from_str(ctx, str);
3081 validity = isl_union_map_empty(isl_union_set_get_space(D));
3082 proximity = isl_union_map_copy(validity);
3083 sc = isl_schedule_constraints_on_domain(D);
3084 sc = isl_schedule_constraints_set_validity(sc, validity);
3085 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3086 sched = isl_schedule_constraints_compute_schedule(sc);
3087 map1 = isl_schedule_get_map(sched);
3088 list = isl_schedule_get_band_forest(sched);
3089 isl_band_list_free(list);
3090 map2 = isl_schedule_get_map(sched);
3091 isl_schedule_free(sched);
3092 equal = isl_union_map_is_equal(map1, map2);
3093 isl_union_map_free(map1);
3094 isl_union_map_free(map2);
3096 if (equal < 0)
3097 return -1;
3098 if (!equal)
3099 isl_die(ctx, isl_error_unknown,
3100 "reconstructed schedule map not the same as original",
3101 return -1);
3103 return 0;
3106 /* Check that conditional validity constraints are also taken into
3107 * account across bands.
3108 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3109 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3110 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3111 * is enforced by the rest of the schedule.
3113 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3115 const char *str;
3116 isl_union_set *domain;
3117 isl_union_map *validity, *proximity, *condition;
3118 isl_union_map *sink, *source, *dep;
3119 isl_schedule_constraints *sc;
3120 isl_schedule *schedule;
3121 isl_union_access_info *access;
3122 isl_union_flow *flow;
3123 int empty;
3125 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3126 "A[k] : k >= 1 and k <= -1 + n; "
3127 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3128 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3129 domain = isl_union_set_read_from_str(ctx, str);
3130 sc = isl_schedule_constraints_on_domain(domain);
3131 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3132 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3133 "D[k, i] -> C[1 + k, i] : "
3134 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3135 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3136 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3137 validity = isl_union_map_read_from_str(ctx, str);
3138 sc = isl_schedule_constraints_set_validity(sc, validity);
3139 str = "[n] -> { C[k, i] -> D[k, i] : "
3140 "0 <= i <= -1 + k and k <= -1 + n }";
3141 proximity = isl_union_map_read_from_str(ctx, str);
3142 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3143 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3144 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3145 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3146 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3147 condition = isl_union_map_read_from_str(ctx, str);
3148 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3149 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3150 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3151 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3152 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3153 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3154 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3155 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3156 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3157 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3158 validity = isl_union_map_read_from_str(ctx, str);
3159 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3160 validity);
3161 schedule = isl_schedule_constraints_compute_schedule(sc);
3162 str = "{ D[2,0] -> [] }";
3163 sink = isl_union_map_read_from_str(ctx, str);
3164 access = isl_union_access_info_from_sink(sink);
3165 str = "{ C[2,1] -> [] }";
3166 source = isl_union_map_read_from_str(ctx, str);
3167 access = isl_union_access_info_set_must_source(access, source);
3168 access = isl_union_access_info_set_schedule(access, schedule);
3169 flow = isl_union_access_info_compute_flow(access);
3170 dep = isl_union_flow_get_must_dependence(flow);
3171 isl_union_flow_free(flow);
3172 empty = isl_union_map_is_empty(dep);
3173 isl_union_map_free(dep);
3175 if (empty < 0)
3176 return -1;
3177 if (empty)
3178 isl_die(ctx, isl_error_unknown,
3179 "conditional validity not respected", return -1);
3181 return 0;
3184 /* Input for testing of schedule construction based on
3185 * conditional constraints.
3187 * domain is the iteration domain
3188 * flow are the flow dependences, which determine the validity and
3189 * proximity constraints
3190 * condition are the conditions on the conditional validity constraints
3191 * conditional_validity are the conditional validity constraints
3192 * outer_band_n is the expected number of members in the outer band
3194 struct {
3195 const char *domain;
3196 const char *flow;
3197 const char *condition;
3198 const char *conditional_validity;
3199 int outer_band_n;
3200 } live_range_tests[] = {
3201 /* Contrived example that illustrates that we need to keep
3202 * track of tagged condition dependences and
3203 * tagged conditional validity dependences
3204 * in isl_sched_edge separately.
3205 * In particular, the conditional validity constraints on A
3206 * cannot be satisfied,
3207 * but they can be ignored because there are no corresponding
3208 * condition constraints. However, we do have an additional
3209 * conditional validity constraint that maps to the same
3210 * dependence relation
3211 * as the condition constraint on B. If we did not make a distinction
3212 * between tagged condition and tagged conditional validity
3213 * dependences, then we
3214 * could end up treating this shared dependence as an condition
3215 * constraint on A, forcing a localization of the conditions,
3216 * which is impossible.
3218 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3219 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3220 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3221 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3222 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3223 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3226 /* TACO 2013 Fig. 7 */
3227 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3228 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3229 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3230 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3231 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3232 "0 <= i < n and 0 <= j < n - 1 }",
3233 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3234 "0 <= i < n and 0 <= j < j' < n;"
3235 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3236 "0 <= i < i' < n and 0 <= j,j' < n;"
3237 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3238 "0 <= i,j,j' < n and j < j' }",
3241 /* TACO 2013 Fig. 7, without tags */
3242 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3243 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3244 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3245 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3246 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3247 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3248 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3249 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3252 /* TACO 2013 Fig. 12 */
3253 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3254 "S3[i,3] : 0 <= i <= 1 }",
3255 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3256 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3257 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3258 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3259 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3260 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3261 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3262 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3263 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3264 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3265 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3270 /* Test schedule construction based on conditional constraints.
3271 * In particular, check the number of members in the outer band node
3272 * as an indication of whether tiling is possible or not.
3274 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3276 int i;
3277 isl_union_set *domain;
3278 isl_union_map *condition;
3279 isl_union_map *flow;
3280 isl_union_map *validity;
3281 isl_schedule_constraints *sc;
3282 isl_schedule *schedule;
3283 isl_schedule_node *node;
3284 int n_member;
3286 if (test_special_conditional_schedule_constraints(ctx) < 0)
3287 return -1;
3289 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3290 domain = isl_union_set_read_from_str(ctx,
3291 live_range_tests[i].domain);
3292 flow = isl_union_map_read_from_str(ctx,
3293 live_range_tests[i].flow);
3294 condition = isl_union_map_read_from_str(ctx,
3295 live_range_tests[i].condition);
3296 validity = isl_union_map_read_from_str(ctx,
3297 live_range_tests[i].conditional_validity);
3298 sc = isl_schedule_constraints_on_domain(domain);
3299 sc = isl_schedule_constraints_set_validity(sc,
3300 isl_union_map_copy(flow));
3301 sc = isl_schedule_constraints_set_proximity(sc, flow);
3302 sc = isl_schedule_constraints_set_conditional_validity(sc,
3303 condition, validity);
3304 schedule = isl_schedule_constraints_compute_schedule(sc);
3305 node = isl_schedule_get_root(schedule);
3306 while (node &&
3307 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3308 node = isl_schedule_node_first_child(node);
3309 n_member = isl_schedule_node_band_n_member(node);
3310 isl_schedule_node_free(node);
3311 isl_schedule_free(schedule);
3313 if (!schedule)
3314 return -1;
3315 if (n_member != live_range_tests[i].outer_band_n)
3316 isl_die(ctx, isl_error_unknown,
3317 "unexpected number of members in outer band",
3318 return -1);
3320 return 0;
3323 /* Check that the schedule computed for the given instance set and
3324 * dependence relation strongly satisfies the dependences.
3325 * In particular, check that no instance is scheduled before
3326 * or together with an instance on which it depends.
3327 * Earlier versions of isl would produce a schedule that
3328 * only weakly satisfies the dependences.
3330 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3332 const char *domain, *dep;
3333 isl_union_map *D, *schedule;
3334 isl_map *map, *ge;
3335 int empty;
3337 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3338 "A[i0] : 0 <= i0 <= 1 }";
3339 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3340 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3341 schedule = compute_schedule(ctx, domain, dep, dep);
3342 D = isl_union_map_read_from_str(ctx, dep);
3343 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3344 D = isl_union_map_apply_range(D, schedule);
3345 map = isl_map_from_union_map(D);
3346 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3347 map = isl_map_intersect(map, ge);
3348 empty = isl_map_is_empty(map);
3349 isl_map_free(map);
3351 if (empty < 0)
3352 return -1;
3353 if (!empty)
3354 isl_die(ctx, isl_error_unknown,
3355 "dependences not strongly satisfied", return -1);
3357 return 0;
3360 /* Compute a schedule for input where the instance set constraints
3361 * conflict with the context constraints.
3362 * Earlier versions of isl did not properly handle this situation.
3364 static int test_conflicting_context_schedule(isl_ctx *ctx)
3366 isl_union_map *schedule;
3367 const char *domain, *context;
3369 domain = "[n] -> { A[] : n >= 0 }";
3370 context = "[n] -> { : n < 0 }";
3371 schedule = compute_schedule_with_context(ctx,
3372 domain, "{}", "{}", context);
3373 isl_union_map_free(schedule);
3375 if (!schedule)
3376 return -1;
3378 return 0;
3381 /* Check that the dependence carrying step is not confused by
3382 * a bound on the coefficient size.
3383 * In particular, force the scheduler to move to a dependence carrying
3384 * step by demanding outer coincidence and bound the size of
3385 * the coefficients. Earlier versions of isl would take this
3386 * bound into account while carrying dependences, breaking
3387 * fundamental assumptions.
3389 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3391 const char *domain, *dep;
3392 isl_union_set *I;
3393 isl_union_map *D;
3394 isl_schedule_constraints *sc;
3395 isl_schedule *schedule;
3397 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3398 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3399 "i1 <= -2 + i0; "
3400 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3401 I = isl_union_set_read_from_str(ctx, domain);
3402 D = isl_union_map_read_from_str(ctx, dep);
3403 sc = isl_schedule_constraints_on_domain(I);
3404 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3405 sc = isl_schedule_constraints_set_coincidence(sc, D);
3406 isl_options_set_schedule_outer_coincidence(ctx, 1);
3407 isl_options_set_schedule_max_coefficient(ctx, 20);
3408 schedule = isl_schedule_constraints_compute_schedule(sc);
3409 isl_options_set_schedule_max_coefficient(ctx, -1);
3410 isl_options_set_schedule_outer_coincidence(ctx, 0);
3411 isl_schedule_free(schedule);
3413 if (!schedule)
3414 return -1;
3416 return 0;
3419 int test_schedule(isl_ctx *ctx)
3421 const char *D, *W, *R, *V, *P, *S;
3423 /* Handle resulting schedule with zero bands. */
3424 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3425 return -1;
3427 /* Jacobi */
3428 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3429 W = "{ S1[t,i] -> a[t,i] }";
3430 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3431 "S1[t,i] -> a[t-1,i+1] }";
3432 S = "{ S1[t,i] -> [t,i] }";
3433 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3434 return -1;
3436 /* Fig. 5 of CC2008 */
3437 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3438 "j <= -1 + N }";
3439 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3440 "j >= 2 and j <= -1 + N }";
3441 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3442 "j >= 2 and j <= -1 + N; "
3443 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3444 "j >= 2 and j <= -1 + N }";
3445 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3446 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3447 return -1;
3449 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3450 W = "{ S1[i] -> a[i] }";
3451 R = "{ S2[i] -> a[i+1] }";
3452 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3453 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3454 return -1;
3456 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3457 W = "{ S1[i] -> a[i] }";
3458 R = "{ S2[i] -> a[9-i] }";
3459 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3460 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3461 return -1;
3463 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3464 W = "{ S1[i] -> a[i] }";
3465 R = "[N] -> { S2[i] -> a[N-1-i] }";
3466 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3467 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3468 return -1;
3470 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3471 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3472 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3473 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3474 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3475 return -1;
3477 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3478 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3479 R = "{ S2[i,j] -> a[i-1,j] }";
3480 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3481 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3482 return -1;
3484 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3485 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3486 R = "{ S2[i,j] -> a[i,j-1] }";
3487 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3488 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3489 return -1;
3491 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3492 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3493 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3494 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3495 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3496 "S_0[] -> [0, 0, 0] }";
3497 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3498 return -1;
3499 ctx->opt->schedule_parametric = 0;
3500 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3501 return -1;
3502 ctx->opt->schedule_parametric = 1;
3504 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3505 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3506 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3507 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3508 "S4[i] -> a[i,N] }";
3509 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3510 "S4[i] -> [4,i,0] }";
3511 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3512 return -1;
3514 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3515 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3516 "j <= N }";
3517 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3518 "j <= N; "
3519 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3520 "j <= N }";
3521 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3522 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3523 return -1;
3525 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3526 " S_2[t] : t >= 0 and t <= -1 + N; "
3527 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3528 "i <= -1 + N }";
3529 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3530 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3531 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3532 "i >= 0 and i <= -1 + N }";
3533 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3534 "i >= 0 and i <= -1 + N; "
3535 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3536 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3537 " S_0[t] -> [0, t, 0] }";
3539 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3540 return -1;
3541 ctx->opt->schedule_parametric = 0;
3542 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3543 return -1;
3544 ctx->opt->schedule_parametric = 1;
3546 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3547 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3548 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3549 return -1;
3551 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3552 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3553 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3554 "j >= 0 and j <= -1 + N; "
3555 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3556 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3557 "j >= 0 and j <= -1 + N; "
3558 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3559 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3560 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3561 return -1;
3563 D = "{ S_0[i] : i >= 0 }";
3564 W = "{ S_0[i] -> a[i] : i >= 0 }";
3565 R = "{ S_0[i] -> a[0] : i >= 0 }";
3566 S = "{ S_0[i] -> [0, i, 0] }";
3567 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3568 return -1;
3570 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3571 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3572 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3573 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3574 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3575 return -1;
3577 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3578 "k <= -1 + n and k >= 0 }";
3579 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3580 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3581 "k <= -1 + n and k >= 0; "
3582 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3583 "k <= -1 + n and k >= 0; "
3584 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3585 "k <= -1 + n and k >= 0 }";
3586 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3587 ctx->opt->schedule_outer_coincidence = 1;
3588 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3589 return -1;
3590 ctx->opt->schedule_outer_coincidence = 0;
3592 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3593 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3594 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3595 "Stmt_for_body24[i0, i1, 1, 0]:"
3596 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3597 "Stmt_for_body7[i0, i1, i2]:"
3598 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3599 "i2 <= 7 }";
3601 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3602 "Stmt_for_body24[1, i1, i2, i3]:"
3603 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3604 "i2 >= 1;"
3605 "Stmt_for_body24[0, i1, i2, i3] -> "
3606 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3607 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3608 "i3 >= 0;"
3609 "Stmt_for_body24[0, i1, i2, i3] ->"
3610 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3611 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3612 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3613 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3614 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3615 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3616 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3617 "i1 <= 6 and i1 >= 0;"
3618 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3619 "Stmt_for_body7[i0, i1, i2] -> "
3620 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3621 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3622 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3623 "Stmt_for_body7[i0, i1, i2] -> "
3624 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3625 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3626 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3627 P = V;
3628 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3629 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3630 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3632 if (test_special_schedule(ctx, D, V, P, S) < 0)
3633 return -1;
3635 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3636 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3637 "j >= 1 and j <= 7;"
3638 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3639 "j >= 1 and j <= 8 }";
3640 P = "{ }";
3641 S = "{ S_0[i, j] -> [i + j, j] }";
3642 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3643 if (test_special_schedule(ctx, D, V, P, S) < 0)
3644 return -1;
3645 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3647 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3648 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3649 "j >= 0 and j <= -1 + i }";
3650 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3651 "i <= -1 + N and j >= 0;"
3652 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3653 "i <= -2 + N }";
3654 P = "{ }";
3655 S = "{ S_0[i, j] -> [i, j] }";
3656 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3657 if (test_special_schedule(ctx, D, V, P, S) < 0)
3658 return -1;
3659 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3661 /* Test both algorithms on a case with only proximity dependences. */
3662 D = "{ S[i,j] : 0 <= i <= 10 }";
3663 V = "{ }";
3664 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3665 S = "{ S[i, j] -> [j, i] }";
3666 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3667 if (test_special_schedule(ctx, D, V, P, S) < 0)
3668 return -1;
3669 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3670 if (test_special_schedule(ctx, D, V, P, S) < 0)
3671 return -1;
3673 D = "{ A[a]; B[] }";
3674 V = "{}";
3675 P = "{ A[a] -> B[] }";
3676 if (test_has_schedule(ctx, D, V, P) < 0)
3677 return -1;
3679 if (test_padded_schedule(ctx) < 0)
3680 return -1;
3682 /* Check that check for progress is not confused by rational
3683 * solution.
3685 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3686 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3687 "i0 <= -2 + N; "
3688 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3689 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3690 P = "{}";
3691 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3692 if (test_has_schedule(ctx, D, V, P) < 0)
3693 return -1;
3694 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3696 /* Check that we allow schedule rows that are only non-trivial
3697 * on some full-dimensional domains.
3699 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3700 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3701 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3702 P = "{}";
3703 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3704 if (test_has_schedule(ctx, D, V, P) < 0)
3705 return -1;
3706 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3708 if (test_conditional_schedule_constraints(ctx) < 0)
3709 return -1;
3711 if (test_strongly_satisfying_schedule(ctx) < 0)
3712 return -1;
3714 if (test_conflicting_context_schedule(ctx) < 0)
3715 return -1;
3717 if (test_bounded_coefficients_schedule(ctx) < 0)
3718 return -1;
3720 return 0;
3723 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3725 isl_union_map *umap;
3726 int test;
3728 umap = isl_union_map_read_from_str(ctx, str);
3729 test = isl_union_map_plain_is_injective(umap);
3730 isl_union_map_free(umap);
3731 if (test < 0)
3732 return -1;
3733 if (test == injective)
3734 return 0;
3735 if (injective)
3736 isl_die(ctx, isl_error_unknown,
3737 "map not detected as injective", return -1);
3738 else
3739 isl_die(ctx, isl_error_unknown,
3740 "map detected as injective", return -1);
3743 int test_injective(isl_ctx *ctx)
3745 const char *str;
3747 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3748 return -1;
3749 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3750 return -1;
3751 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3752 return -1;
3753 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3754 return -1;
3755 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3756 return -1;
3757 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3758 return -1;
3759 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3760 return -1;
3761 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3762 return -1;
3764 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3765 if (test_plain_injective(ctx, str, 1))
3766 return -1;
3767 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3768 if (test_plain_injective(ctx, str, 0))
3769 return -1;
3771 return 0;
3774 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3776 isl_aff *aff2;
3777 int equal;
3779 if (!aff)
3780 return -1;
3782 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3783 equal = isl_aff_plain_is_equal(aff, aff2);
3784 isl_aff_free(aff2);
3786 return equal;
3789 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3791 int equal;
3793 equal = aff_plain_is_equal(aff, str);
3794 if (equal < 0)
3795 return -1;
3796 if (!equal)
3797 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3798 "result not as expected", return -1);
3799 return 0;
3802 struct {
3803 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3804 __isl_take isl_aff *aff2);
3805 } aff_bin_op[] = {
3806 ['+'] = { &isl_aff_add },
3807 ['-'] = { &isl_aff_sub },
3808 ['*'] = { &isl_aff_mul },
3809 ['/'] = { &isl_aff_div },
3812 struct {
3813 const char *arg1;
3814 unsigned char op;
3815 const char *arg2;
3816 const char *res;
3817 } aff_bin_tests[] = {
3818 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3819 "{ [i] -> [2i] }" },
3820 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3821 "{ [i] -> [0] }" },
3822 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3823 "{ [i] -> [2i] }" },
3824 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3825 "{ [i] -> [2i] }" },
3826 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3827 "{ [i] -> [i/2] }" },
3828 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3829 "{ [i] -> [i] }" },
3830 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3831 "{ [i] -> [NaN] }" },
3832 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3833 "{ [i] -> [NaN] }" },
3834 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3835 "{ [i] -> [NaN] }" },
3836 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3837 "{ [i] -> [NaN] }" },
3838 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3839 "{ [i] -> [NaN] }" },
3840 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3841 "{ [i] -> [NaN] }" },
3842 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3843 "{ [i] -> [NaN] }" },
3844 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3845 "{ [i] -> [NaN] }" },
3846 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3847 "{ [i] -> [NaN] }" },
3848 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3849 "{ [i] -> [NaN] }" },
3850 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3851 "{ [i] -> [NaN] }" },
3852 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3853 "{ [i] -> [NaN] }" },
3856 /* Perform some basic tests of binary operations on isl_aff objects.
3858 static int test_bin_aff(isl_ctx *ctx)
3860 int i;
3861 isl_aff *aff1, *aff2, *res;
3862 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3863 __isl_take isl_aff *aff2);
3864 int ok;
3866 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3867 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3868 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3869 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3870 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3871 aff1 = fn(aff1, aff2);
3872 if (isl_aff_is_nan(res))
3873 ok = isl_aff_is_nan(aff1);
3874 else
3875 ok = isl_aff_plain_is_equal(aff1, res);
3876 isl_aff_free(aff1);
3877 isl_aff_free(res);
3878 if (ok < 0)
3879 return -1;
3880 if (!ok)
3881 isl_die(ctx, isl_error_unknown,
3882 "unexpected result", return -1);
3885 return 0;
3888 struct {
3889 __isl_give isl_union_pw_multi_aff *(*fn)(
3890 __isl_take isl_union_pw_multi_aff *upma1,
3891 __isl_take isl_union_pw_multi_aff *upma2);
3892 const char *arg1;
3893 const char *arg2;
3894 const char *res;
3895 } upma_bin_tests[] = {
3896 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3897 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3898 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3899 "{ B[x] -> [2] : x >= 0 }",
3900 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3903 /* Perform some basic tests of binary operations on
3904 * isl_union_pw_multi_aff objects.
3906 static int test_bin_upma(isl_ctx *ctx)
3908 int i;
3909 isl_union_pw_multi_aff *upma1, *upma2, *res;
3910 int ok;
3912 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3913 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3914 upma_bin_tests[i].arg1);
3915 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3916 upma_bin_tests[i].arg2);
3917 res = isl_union_pw_multi_aff_read_from_str(ctx,
3918 upma_bin_tests[i].res);
3919 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3920 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3921 isl_union_pw_multi_aff_free(upma1);
3922 isl_union_pw_multi_aff_free(res);
3923 if (ok < 0)
3924 return -1;
3925 if (!ok)
3926 isl_die(ctx, isl_error_unknown,
3927 "unexpected result", return -1);
3930 return 0;
3933 int test_aff(isl_ctx *ctx)
3935 const char *str;
3936 isl_set *set;
3937 isl_space *space;
3938 isl_local_space *ls;
3939 isl_aff *aff;
3940 int zero, equal;
3942 if (test_bin_aff(ctx) < 0)
3943 return -1;
3944 if (test_bin_upma(ctx) < 0)
3945 return -1;
3947 space = isl_space_set_alloc(ctx, 0, 1);
3948 ls = isl_local_space_from_space(space);
3949 aff = isl_aff_zero_on_domain(ls);
3951 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3952 aff = isl_aff_scale_down_ui(aff, 3);
3953 aff = isl_aff_floor(aff);
3954 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3955 aff = isl_aff_scale_down_ui(aff, 2);
3956 aff = isl_aff_floor(aff);
3957 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3959 str = "{ [10] }";
3960 set = isl_set_read_from_str(ctx, str);
3961 aff = isl_aff_gist(aff, set);
3963 aff = isl_aff_add_constant_si(aff, -16);
3964 zero = isl_aff_plain_is_zero(aff);
3965 isl_aff_free(aff);
3967 if (zero < 0)
3968 return -1;
3969 if (!zero)
3970 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3972 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3973 aff = isl_aff_scale_down_ui(aff, 64);
3974 aff = isl_aff_floor(aff);
3975 equal = aff_check_plain_equal(aff, "{ [-1] }");
3976 isl_aff_free(aff);
3977 if (equal < 0)
3978 return -1;
3980 return 0;
3983 int test_dim_max(isl_ctx *ctx)
3985 int equal;
3986 const char *str;
3987 isl_set *set1, *set2;
3988 isl_set *set;
3989 isl_map *map;
3990 isl_pw_aff *pwaff;
3992 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3993 set = isl_set_read_from_str(ctx, str);
3994 pwaff = isl_set_dim_max(set, 0);
3995 set1 = isl_set_from_pw_aff(pwaff);
3996 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3997 set2 = isl_set_read_from_str(ctx, str);
3998 equal = isl_set_is_equal(set1, set2);
3999 isl_set_free(set1);
4000 isl_set_free(set2);
4001 if (equal < 0)
4002 return -1;
4003 if (!equal)
4004 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4006 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4007 set = isl_set_read_from_str(ctx, str);
4008 pwaff = isl_set_dim_max(set, 0);
4009 set1 = isl_set_from_pw_aff(pwaff);
4010 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4011 set2 = isl_set_read_from_str(ctx, str);
4012 equal = isl_set_is_equal(set1, set2);
4013 isl_set_free(set1);
4014 isl_set_free(set2);
4015 if (equal < 0)
4016 return -1;
4017 if (!equal)
4018 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4020 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4021 set = isl_set_read_from_str(ctx, str);
4022 pwaff = isl_set_dim_max(set, 0);
4023 set1 = isl_set_from_pw_aff(pwaff);
4024 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4025 set2 = isl_set_read_from_str(ctx, str);
4026 equal = isl_set_is_equal(set1, set2);
4027 isl_set_free(set1);
4028 isl_set_free(set2);
4029 if (equal < 0)
4030 return -1;
4031 if (!equal)
4032 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4034 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4035 "0 <= i < N and 0 <= j < M }";
4036 map = isl_map_read_from_str(ctx, str);
4037 set = isl_map_range(map);
4039 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4040 set1 = isl_set_from_pw_aff(pwaff);
4041 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4042 set2 = isl_set_read_from_str(ctx, str);
4043 equal = isl_set_is_equal(set1, set2);
4044 isl_set_free(set1);
4045 isl_set_free(set2);
4047 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4048 set1 = isl_set_from_pw_aff(pwaff);
4049 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4050 set2 = isl_set_read_from_str(ctx, str);
4051 if (equal >= 0 && equal)
4052 equal = isl_set_is_equal(set1, set2);
4053 isl_set_free(set1);
4054 isl_set_free(set2);
4056 isl_set_free(set);
4058 if (equal < 0)
4059 return -1;
4060 if (!equal)
4061 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4063 /* Check that solutions are properly merged. */
4064 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4065 "c <= -1 + n - 4a - 2b and c >= -2b and "
4066 "4a >= -4 + n and c >= 0 }";
4067 set = isl_set_read_from_str(ctx, str);
4068 pwaff = isl_set_dim_min(set, 2);
4069 set1 = isl_set_from_pw_aff(pwaff);
4070 str = "[n] -> { [(0)] : n >= 1 }";
4071 set2 = isl_set_read_from_str(ctx, str);
4072 equal = isl_set_is_equal(set1, set2);
4073 isl_set_free(set1);
4074 isl_set_free(set2);
4076 if (equal < 0)
4077 return -1;
4078 if (!equal)
4079 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4081 /* Check that empty solution lie in the right space. */
4082 str = "[n] -> { [t,a] : 1 = 0 }";
4083 set = isl_set_read_from_str(ctx, str);
4084 pwaff = isl_set_dim_max(set, 0);
4085 set1 = isl_set_from_pw_aff(pwaff);
4086 str = "[n] -> { [t] : 1 = 0 }";
4087 set2 = isl_set_read_from_str(ctx, str);
4088 equal = isl_set_is_equal(set1, set2);
4089 isl_set_free(set1);
4090 isl_set_free(set2);
4092 if (equal < 0)
4093 return -1;
4094 if (!equal)
4095 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4097 return 0;
4100 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4102 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4103 const char *str)
4105 isl_ctx *ctx;
4106 isl_pw_multi_aff *pma2;
4107 int equal;
4109 if (!pma)
4110 return -1;
4112 ctx = isl_pw_multi_aff_get_ctx(pma);
4113 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4114 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4115 isl_pw_multi_aff_free(pma2);
4117 return equal;
4120 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4121 * represented by "str".
4123 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4124 const char *str)
4126 int equal;
4128 equal = pw_multi_aff_plain_is_equal(pma, str);
4129 if (equal < 0)
4130 return -1;
4131 if (!equal)
4132 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4133 "result not as expected", return -1);
4134 return 0;
4137 /* Basic test for isl_pw_multi_aff_product.
4139 * Check that multiple pieces are properly handled.
4141 static int test_product_pma(isl_ctx *ctx)
4143 int equal;
4144 const char *str;
4145 isl_pw_multi_aff *pma1, *pma2;
4147 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4148 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4149 str = "{ C[] -> D[] }";
4150 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4151 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4152 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4153 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4154 equal = pw_multi_aff_check_plain_equal(pma1, str);
4155 isl_pw_multi_aff_free(pma1);
4156 if (equal < 0)
4157 return -1;
4159 return 0;
4162 int test_product(isl_ctx *ctx)
4164 const char *str;
4165 isl_set *set;
4166 isl_union_set *uset1, *uset2;
4167 int ok;
4169 str = "{ A[i] }";
4170 set = isl_set_read_from_str(ctx, str);
4171 set = isl_set_product(set, isl_set_copy(set));
4172 ok = isl_set_is_wrapping(set);
4173 isl_set_free(set);
4174 if (ok < 0)
4175 return -1;
4176 if (!ok)
4177 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4179 str = "{ [] }";
4180 uset1 = isl_union_set_read_from_str(ctx, str);
4181 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4182 str = "{ [[] -> []] }";
4183 uset2 = isl_union_set_read_from_str(ctx, str);
4184 ok = isl_union_set_is_equal(uset1, uset2);
4185 isl_union_set_free(uset1);
4186 isl_union_set_free(uset2);
4187 if (ok < 0)
4188 return -1;
4189 if (!ok)
4190 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4192 if (test_product_pma(ctx) < 0)
4193 return -1;
4195 return 0;
4198 /* Check that two sets are not considered disjoint just because
4199 * they have a different set of (named) parameters.
4201 static int test_disjoint(isl_ctx *ctx)
4203 const char *str;
4204 isl_set *set, *set2;
4205 int disjoint;
4207 str = "[n] -> { [[]->[]] }";
4208 set = isl_set_read_from_str(ctx, str);
4209 str = "{ [[]->[]] }";
4210 set2 = isl_set_read_from_str(ctx, str);
4211 disjoint = isl_set_is_disjoint(set, set2);
4212 isl_set_free(set);
4213 isl_set_free(set2);
4214 if (disjoint < 0)
4215 return -1;
4216 if (disjoint)
4217 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4219 return 0;
4222 int test_equal(isl_ctx *ctx)
4224 const char *str;
4225 isl_set *set, *set2;
4226 int equal;
4228 str = "{ S_6[i] }";
4229 set = isl_set_read_from_str(ctx, str);
4230 str = "{ S_7[i] }";
4231 set2 = isl_set_read_from_str(ctx, str);
4232 equal = isl_set_is_equal(set, set2);
4233 isl_set_free(set);
4234 isl_set_free(set2);
4235 if (equal < 0)
4236 return -1;
4237 if (equal)
4238 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4240 return 0;
4243 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4244 enum isl_dim_type type, unsigned pos, int fixed)
4246 int test;
4248 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4249 isl_map_free(map);
4250 if (test < 0)
4251 return -1;
4252 if (test == fixed)
4253 return 0;
4254 if (fixed)
4255 isl_die(ctx, isl_error_unknown,
4256 "map not detected as fixed", return -1);
4257 else
4258 isl_die(ctx, isl_error_unknown,
4259 "map detected as fixed", return -1);
4262 int test_fixed(isl_ctx *ctx)
4264 const char *str;
4265 isl_map *map;
4267 str = "{ [i] -> [i] }";
4268 map = isl_map_read_from_str(ctx, str);
4269 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4270 return -1;
4271 str = "{ [i] -> [1] }";
4272 map = isl_map_read_from_str(ctx, str);
4273 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4274 return -1;
4275 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4276 map = isl_map_read_from_str(ctx, str);
4277 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4278 return -1;
4279 map = isl_map_read_from_str(ctx, str);
4280 map = isl_map_neg(map);
4281 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4282 return -1;
4284 return 0;
4287 struct isl_vertices_test_data {
4288 const char *set;
4289 int n;
4290 const char *vertex[2];
4291 } vertices_tests[] = {
4292 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4293 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4294 { "{ A[t, i] : t = 14 and i = 1 }",
4295 1, { "{ A[14, 1] }" } },
4298 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4300 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4302 struct isl_vertices_test_data *data = user;
4303 isl_ctx *ctx;
4304 isl_multi_aff *ma;
4305 isl_basic_set *bset;
4306 isl_pw_multi_aff *pma;
4307 int i;
4308 isl_bool equal;
4310 ctx = isl_vertex_get_ctx(vertex);
4311 bset = isl_vertex_get_domain(vertex);
4312 ma = isl_vertex_get_expr(vertex);
4313 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4315 for (i = 0; i < data->n; ++i) {
4316 isl_pw_multi_aff *pma_i;
4318 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4319 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4320 isl_pw_multi_aff_free(pma_i);
4322 if (equal < 0 || equal)
4323 break;
4326 isl_pw_multi_aff_free(pma);
4327 isl_vertex_free(vertex);
4329 if (equal < 0)
4330 return isl_stat_error;
4332 return equal ? isl_stat_ok : isl_stat_error;
4335 int test_vertices(isl_ctx *ctx)
4337 int i;
4339 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4340 isl_basic_set *bset;
4341 isl_vertices *vertices;
4342 int ok = 1;
4343 int n;
4345 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4346 vertices = isl_basic_set_compute_vertices(bset);
4347 n = isl_vertices_get_n_vertices(vertices);
4348 if (vertices_tests[i].n != n)
4349 ok = 0;
4350 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4351 &vertices_tests[i]) < 0)
4352 ok = 0;
4353 isl_vertices_free(vertices);
4354 isl_basic_set_free(bset);
4356 if (!vertices)
4357 return -1;
4358 if (!ok)
4359 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4360 return -1);
4363 return 0;
4366 int test_union_pw(isl_ctx *ctx)
4368 int equal;
4369 const char *str;
4370 isl_union_set *uset;
4371 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4373 str = "{ [x] -> x^2 }";
4374 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4375 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4376 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4377 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4378 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4379 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4380 isl_union_pw_qpolynomial_free(upwqp1);
4381 isl_union_pw_qpolynomial_free(upwqp2);
4382 if (equal < 0)
4383 return -1;
4384 if (!equal)
4385 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4387 return 0;
4390 int test_output(isl_ctx *ctx)
4392 char *s;
4393 const char *str;
4394 isl_pw_aff *pa;
4395 isl_printer *p;
4396 int equal;
4398 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4399 pa = isl_pw_aff_read_from_str(ctx, str);
4401 p = isl_printer_to_str(ctx);
4402 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4403 p = isl_printer_print_pw_aff(p, pa);
4404 s = isl_printer_get_str(p);
4405 isl_printer_free(p);
4406 isl_pw_aff_free(pa);
4407 if (!s)
4408 equal = -1;
4409 else
4410 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4411 free(s);
4412 if (equal < 0)
4413 return -1;
4414 if (!equal)
4415 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4417 return 0;
4420 int test_sample(isl_ctx *ctx)
4422 const char *str;
4423 isl_basic_set *bset1, *bset2;
4424 int empty, subset;
4426 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4427 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4428 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4429 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4430 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4431 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4432 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4433 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4434 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4435 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4436 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4437 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4438 "d - 1073741821e and "
4439 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4440 "3j >= 1 - a + b + 2e and "
4441 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4442 "3i <= 4 - a + 4b - e and "
4443 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4444 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4445 "c <= -1 + a and 3i >= -2 - a + 3e and "
4446 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4447 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4448 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4449 "1073741823e >= 1 + 1073741823b - d and "
4450 "3i >= 1073741823b + c - 1073741823e - f and "
4451 "3i >= 1 + 2b + e + 3g }";
4452 bset1 = isl_basic_set_read_from_str(ctx, str);
4453 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4454 empty = isl_basic_set_is_empty(bset2);
4455 subset = isl_basic_set_is_subset(bset2, bset1);
4456 isl_basic_set_free(bset1);
4457 isl_basic_set_free(bset2);
4458 if (empty < 0 || subset < 0)
4459 return -1;
4460 if (empty)
4461 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4462 if (!subset)
4463 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4465 return 0;
4468 int test_fixed_power(isl_ctx *ctx)
4470 const char *str;
4471 isl_map *map;
4472 isl_int exp;
4473 int equal;
4475 isl_int_init(exp);
4476 str = "{ [i] -> [i + 1] }";
4477 map = isl_map_read_from_str(ctx, str);
4478 isl_int_set_si(exp, 23);
4479 map = isl_map_fixed_power(map, exp);
4480 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4481 isl_int_clear(exp);
4482 isl_map_free(map);
4483 if (equal < 0)
4484 return -1;
4486 return 0;
4489 int test_slice(isl_ctx *ctx)
4491 const char *str;
4492 isl_map *map;
4493 int equal;
4495 str = "{ [i] -> [j] }";
4496 map = isl_map_read_from_str(ctx, str);
4497 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4498 equal = map_check_equal(map, "{ [i] -> [i] }");
4499 isl_map_free(map);
4500 if (equal < 0)
4501 return -1;
4503 str = "{ [i] -> [j] }";
4504 map = isl_map_read_from_str(ctx, str);
4505 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4506 equal = map_check_equal(map, "{ [i] -> [j] }");
4507 isl_map_free(map);
4508 if (equal < 0)
4509 return -1;
4511 str = "{ [i] -> [j] }";
4512 map = isl_map_read_from_str(ctx, str);
4513 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4514 equal = map_check_equal(map, "{ [i] -> [-i] }");
4515 isl_map_free(map);
4516 if (equal < 0)
4517 return -1;
4519 str = "{ [i] -> [j] }";
4520 map = isl_map_read_from_str(ctx, str);
4521 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4522 equal = map_check_equal(map, "{ [0] -> [j] }");
4523 isl_map_free(map);
4524 if (equal < 0)
4525 return -1;
4527 str = "{ [i] -> [j] }";
4528 map = isl_map_read_from_str(ctx, str);
4529 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4530 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4531 isl_map_free(map);
4532 if (equal < 0)
4533 return -1;
4535 str = "{ [i] -> [j] }";
4536 map = isl_map_read_from_str(ctx, str);
4537 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4538 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4539 isl_map_free(map);
4540 if (equal < 0)
4541 return -1;
4543 return 0;
4546 int test_eliminate(isl_ctx *ctx)
4548 const char *str;
4549 isl_map *map;
4550 int equal;
4552 str = "{ [i] -> [j] : i = 2j }";
4553 map = isl_map_read_from_str(ctx, str);
4554 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4555 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4556 isl_map_free(map);
4557 if (equal < 0)
4558 return -1;
4560 return 0;
4563 /* Check that isl_set_dim_residue_class detects that the values of j
4564 * in the set below are all odd and that it does not detect any spurious
4565 * strides.
4567 static int test_residue_class(isl_ctx *ctx)
4569 const char *str;
4570 isl_set *set;
4571 isl_int m, r;
4572 int res;
4574 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4575 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4576 set = isl_set_read_from_str(ctx, str);
4577 isl_int_init(m);
4578 isl_int_init(r);
4579 res = isl_set_dim_residue_class(set, 1, &m, &r);
4580 if (res >= 0 &&
4581 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4582 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4583 res = -1);
4584 isl_int_clear(r);
4585 isl_int_clear(m);
4586 isl_set_free(set);
4588 return res;
4591 int test_align_parameters(isl_ctx *ctx)
4593 const char *str;
4594 isl_space *space;
4595 isl_multi_aff *ma1, *ma2;
4596 int equal;
4598 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4599 ma1 = isl_multi_aff_read_from_str(ctx, str);
4601 space = isl_space_params_alloc(ctx, 1);
4602 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4603 ma1 = isl_multi_aff_align_params(ma1, space);
4605 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4606 ma2 = isl_multi_aff_read_from_str(ctx, str);
4608 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4610 isl_multi_aff_free(ma1);
4611 isl_multi_aff_free(ma2);
4613 if (equal < 0)
4614 return -1;
4615 if (!equal)
4616 isl_die(ctx, isl_error_unknown,
4617 "result not as expected", return -1);
4619 return 0;
4622 static int test_list(isl_ctx *ctx)
4624 isl_id *a, *b, *c, *d, *id;
4625 isl_id_list *list;
4626 int ok;
4628 a = isl_id_alloc(ctx, "a", NULL);
4629 b = isl_id_alloc(ctx, "b", NULL);
4630 c = isl_id_alloc(ctx, "c", NULL);
4631 d = isl_id_alloc(ctx, "d", NULL);
4633 list = isl_id_list_alloc(ctx, 4);
4634 list = isl_id_list_add(list, a);
4635 list = isl_id_list_add(list, b);
4636 list = isl_id_list_add(list, c);
4637 list = isl_id_list_add(list, d);
4638 list = isl_id_list_drop(list, 1, 1);
4640 if (isl_id_list_n_id(list) != 3) {
4641 isl_id_list_free(list);
4642 isl_die(ctx, isl_error_unknown,
4643 "unexpected number of elements in list", return -1);
4646 id = isl_id_list_get_id(list, 0);
4647 ok = id == a;
4648 isl_id_free(id);
4649 id = isl_id_list_get_id(list, 1);
4650 ok = ok && id == c;
4651 isl_id_free(id);
4652 id = isl_id_list_get_id(list, 2);
4653 ok = ok && id == d;
4654 isl_id_free(id);
4656 isl_id_list_free(list);
4658 if (!ok)
4659 isl_die(ctx, isl_error_unknown,
4660 "unexpected elements in list", return -1);
4662 return 0;
4665 const char *set_conversion_tests[] = {
4666 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4667 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4668 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4669 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4670 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4673 /* Check that converting from isl_set to isl_pw_multi_aff and back
4674 * to isl_set produces the original isl_set.
4676 static int test_set_conversion(isl_ctx *ctx)
4678 int i;
4679 const char *str;
4680 isl_set *set1, *set2;
4681 isl_pw_multi_aff *pma;
4682 int equal;
4684 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4685 str = set_conversion_tests[i];
4686 set1 = isl_set_read_from_str(ctx, str);
4687 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4688 set2 = isl_set_from_pw_multi_aff(pma);
4689 equal = isl_set_is_equal(set1, set2);
4690 isl_set_free(set1);
4691 isl_set_free(set2);
4693 if (equal < 0)
4694 return -1;
4695 if (!equal)
4696 isl_die(ctx, isl_error_unknown, "bad conversion",
4697 return -1);
4700 return 0;
4703 /* Check that converting from isl_map to isl_pw_multi_aff and back
4704 * to isl_map produces the original isl_map.
4706 static int test_map_conversion(isl_ctx *ctx)
4708 const char *str;
4709 isl_map *map1, *map2;
4710 isl_pw_multi_aff *pma;
4711 int equal;
4713 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4714 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4715 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4716 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4717 "9e <= -2 - 2a) }";
4718 map1 = isl_map_read_from_str(ctx, str);
4719 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4720 map2 = isl_map_from_pw_multi_aff(pma);
4721 equal = isl_map_is_equal(map1, map2);
4722 isl_map_free(map1);
4723 isl_map_free(map2);
4725 if (equal < 0)
4726 return -1;
4727 if (!equal)
4728 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4730 return 0;
4733 static int test_conversion(isl_ctx *ctx)
4735 if (test_set_conversion(ctx) < 0)
4736 return -1;
4737 if (test_map_conversion(ctx) < 0)
4738 return -1;
4739 return 0;
4742 /* Check that isl_basic_map_curry does not modify input.
4744 static int test_curry(isl_ctx *ctx)
4746 const char *str;
4747 isl_basic_map *bmap1, *bmap2;
4748 int equal;
4750 str = "{ [A[] -> B[]] -> C[] }";
4751 bmap1 = isl_basic_map_read_from_str(ctx, str);
4752 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4753 equal = isl_basic_map_is_equal(bmap1, bmap2);
4754 isl_basic_map_free(bmap1);
4755 isl_basic_map_free(bmap2);
4757 if (equal < 0)
4758 return -1;
4759 if (equal)
4760 isl_die(ctx, isl_error_unknown,
4761 "curried map should not be equal to original",
4762 return -1);
4764 return 0;
4767 struct {
4768 const char *set;
4769 const char *ma;
4770 const char *res;
4771 } preimage_tests[] = {
4772 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4773 "{ A[j,i] -> B[i,j] }",
4774 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4775 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4776 "{ A[a,b] -> B[a/2,b/6] }",
4777 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4778 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4779 "{ A[a,b] -> B[a/2,b/6] }",
4780 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4781 "exists i,j : a = 2 i and b = 6 j }" },
4782 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4783 "[n] -> { : 0 <= n <= 100 }" },
4784 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4785 "{ A[a] -> B[2a] }",
4786 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4787 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4788 "{ A[a] -> B[([a/2])] }",
4789 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4790 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4791 "{ A[a] -> B[a,a,a/3] }",
4792 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4793 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4794 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4797 static int test_preimage_basic_set(isl_ctx *ctx)
4799 int i;
4800 isl_basic_set *bset1, *bset2;
4801 isl_multi_aff *ma;
4802 int equal;
4804 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4805 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4806 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4807 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4808 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4809 equal = isl_basic_set_is_equal(bset1, bset2);
4810 isl_basic_set_free(bset1);
4811 isl_basic_set_free(bset2);
4812 if (equal < 0)
4813 return -1;
4814 if (!equal)
4815 isl_die(ctx, isl_error_unknown, "bad preimage",
4816 return -1);
4819 return 0;
4822 struct {
4823 const char *map;
4824 const char *ma;
4825 const char *res;
4826 } preimage_domain_tests[] = {
4827 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4828 "{ A[j,i] -> B[i,j] }",
4829 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4830 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4831 "{ A[i] -> B[i + 1] }",
4832 "{ A[i] -> C[i + 1] }" },
4833 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4834 "{ A[i] -> B[i + 1] }",
4835 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4836 { "{ B[i] -> C[([i/2])] }",
4837 "{ A[i] -> B[2i] }",
4838 "{ A[i] -> C[i] }" },
4839 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4840 "{ A[i] -> B[([i/5]), ([i/7])] }",
4841 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4842 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4843 "[N] -> { A[] -> B[([N/5])] }",
4844 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4845 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4846 "{ A[i] -> B[2i] }",
4847 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4848 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4849 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4850 "{ A[i] -> B[2i] }",
4851 "{ A[i] -> C[2i] }" },
4854 static int test_preimage_union_map(isl_ctx *ctx)
4856 int i;
4857 isl_union_map *umap1, *umap2;
4858 isl_multi_aff *ma;
4859 int equal;
4861 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4862 umap1 = isl_union_map_read_from_str(ctx,
4863 preimage_domain_tests[i].map);
4864 ma = isl_multi_aff_read_from_str(ctx,
4865 preimage_domain_tests[i].ma);
4866 umap2 = isl_union_map_read_from_str(ctx,
4867 preimage_domain_tests[i].res);
4868 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4869 equal = isl_union_map_is_equal(umap1, umap2);
4870 isl_union_map_free(umap1);
4871 isl_union_map_free(umap2);
4872 if (equal < 0)
4873 return -1;
4874 if (!equal)
4875 isl_die(ctx, isl_error_unknown, "bad preimage",
4876 return -1);
4879 return 0;
4882 static int test_preimage(isl_ctx *ctx)
4884 if (test_preimage_basic_set(ctx) < 0)
4885 return -1;
4886 if (test_preimage_union_map(ctx) < 0)
4887 return -1;
4889 return 0;
4892 struct {
4893 const char *ma1;
4894 const char *ma;
4895 const char *res;
4896 } pullback_tests[] = {
4897 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4898 "{ A[a,b] -> C[b + 2a] }" },
4899 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4900 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4901 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4902 "{ A[a] -> C[(a)/6] }" },
4903 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4904 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4905 "{ A[a] -> C[(2a)/3] }" },
4906 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4907 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4908 "{ A[i,j] -> C[i + j, i + j] }"},
4909 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4910 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4911 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4912 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
4913 "{ [i, j] -> [floor((i)/3), j] }",
4914 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
4917 static int test_pullback(isl_ctx *ctx)
4919 int i;
4920 isl_multi_aff *ma1, *ma2;
4921 isl_multi_aff *ma;
4922 int equal;
4924 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4925 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4926 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4927 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4928 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4929 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4930 isl_multi_aff_free(ma1);
4931 isl_multi_aff_free(ma2);
4932 if (equal < 0)
4933 return -1;
4934 if (!equal)
4935 isl_die(ctx, isl_error_unknown, "bad pullback",
4936 return -1);
4939 return 0;
4942 /* Check that negation is printed correctly and that equal expressions
4943 * are correctly identified.
4945 static int test_ast(isl_ctx *ctx)
4947 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4948 char *str;
4949 int ok, equal;
4951 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4952 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4953 expr = isl_ast_expr_add(expr1, expr2);
4954 expr2 = isl_ast_expr_copy(expr);
4955 expr = isl_ast_expr_neg(expr);
4956 expr2 = isl_ast_expr_neg(expr2);
4957 equal = isl_ast_expr_is_equal(expr, expr2);
4958 str = isl_ast_expr_to_str(expr);
4959 ok = str ? !strcmp(str, "-(A + B)") : -1;
4960 free(str);
4961 isl_ast_expr_free(expr);
4962 isl_ast_expr_free(expr2);
4964 if (ok < 0 || equal < 0)
4965 return -1;
4966 if (!equal)
4967 isl_die(ctx, isl_error_unknown,
4968 "equal expressions not considered equal", return -1);
4969 if (!ok)
4970 isl_die(ctx, isl_error_unknown,
4971 "isl_ast_expr printed incorrectly", return -1);
4973 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4974 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4975 expr = isl_ast_expr_add(expr1, expr2);
4976 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4977 expr = isl_ast_expr_sub(expr3, expr);
4978 str = isl_ast_expr_to_str(expr);
4979 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4980 free(str);
4981 isl_ast_expr_free(expr);
4983 if (ok < 0)
4984 return -1;
4985 if (!ok)
4986 isl_die(ctx, isl_error_unknown,
4987 "isl_ast_expr printed incorrectly", return -1);
4989 return 0;
4992 /* Check that isl_ast_build_expr_from_set returns a valid expression
4993 * for an empty set. Note that isl_ast_build_expr_from_set getting
4994 * called on an empty set probably indicates a bug in the caller.
4996 static int test_ast_build(isl_ctx *ctx)
4998 isl_set *set;
4999 isl_ast_build *build;
5000 isl_ast_expr *expr;
5002 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5003 build = isl_ast_build_from_context(set);
5005 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5006 expr = isl_ast_build_expr_from_set(build, set);
5008 isl_ast_expr_free(expr);
5009 isl_ast_build_free(build);
5011 if (!expr)
5012 return -1;
5014 return 0;
5017 /* Internal data structure for before_for and after_for callbacks.
5019 * depth is the current depth
5020 * before is the number of times before_for has been called
5021 * after is the number of times after_for has been called
5023 struct isl_test_codegen_data {
5024 int depth;
5025 int before;
5026 int after;
5029 /* This function is called before each for loop in the AST generated
5030 * from test_ast_gen1.
5032 * Increment the number of calls and the depth.
5033 * Check that the space returned by isl_ast_build_get_schedule_space
5034 * matches the target space of the schedule returned by
5035 * isl_ast_build_get_schedule.
5036 * Return an isl_id that is checked by the corresponding call
5037 * to after_for.
5039 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5040 void *user)
5042 struct isl_test_codegen_data *data = user;
5043 isl_ctx *ctx;
5044 isl_space *space;
5045 isl_union_map *schedule;
5046 isl_union_set *uset;
5047 isl_set *set;
5048 int empty;
5049 char name[] = "d0";
5051 ctx = isl_ast_build_get_ctx(build);
5053 if (data->before >= 3)
5054 isl_die(ctx, isl_error_unknown,
5055 "unexpected number of for nodes", return NULL);
5056 if (data->depth >= 2)
5057 isl_die(ctx, isl_error_unknown,
5058 "unexpected depth", return NULL);
5060 snprintf(name, sizeof(name), "d%d", data->depth);
5061 data->before++;
5062 data->depth++;
5064 schedule = isl_ast_build_get_schedule(build);
5065 uset = isl_union_map_range(schedule);
5066 if (!uset)
5067 return NULL;
5068 if (isl_union_set_n_set(uset) != 1) {
5069 isl_union_set_free(uset);
5070 isl_die(ctx, isl_error_unknown,
5071 "expecting single range space", return NULL);
5074 space = isl_ast_build_get_schedule_space(build);
5075 set = isl_union_set_extract_set(uset, space);
5076 isl_union_set_free(uset);
5077 empty = isl_set_is_empty(set);
5078 isl_set_free(set);
5080 if (empty < 0)
5081 return NULL;
5082 if (empty)
5083 isl_die(ctx, isl_error_unknown,
5084 "spaces don't match", return NULL);
5086 return isl_id_alloc(ctx, name, NULL);
5089 /* This function is called after each for loop in the AST generated
5090 * from test_ast_gen1.
5092 * Increment the number of calls and decrement the depth.
5093 * Check that the annotation attached to the node matches
5094 * the isl_id returned by the corresponding call to before_for.
5096 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5097 __isl_keep isl_ast_build *build, void *user)
5099 struct isl_test_codegen_data *data = user;
5100 isl_id *id;
5101 const char *name;
5102 int valid;
5104 data->after++;
5105 data->depth--;
5107 if (data->after > data->before)
5108 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5109 "mismatch in number of for nodes",
5110 return isl_ast_node_free(node));
5112 id = isl_ast_node_get_annotation(node);
5113 if (!id)
5114 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5115 "missing annotation", return isl_ast_node_free(node));
5117 name = isl_id_get_name(id);
5118 valid = name && atoi(name + 1) == data->depth;
5119 isl_id_free(id);
5121 if (!valid)
5122 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5123 "wrong annotation", return isl_ast_node_free(node));
5125 return node;
5128 /* Check that the before_each_for and after_each_for callbacks
5129 * are called for each for loop in the generated code,
5130 * that they are called in the right order and that the isl_id
5131 * returned from the before_each_for callback is attached to
5132 * the isl_ast_node passed to the corresponding after_each_for call.
5134 static int test_ast_gen1(isl_ctx *ctx)
5136 const char *str;
5137 isl_set *set;
5138 isl_union_map *schedule;
5139 isl_ast_build *build;
5140 isl_ast_node *tree;
5141 struct isl_test_codegen_data data;
5143 str = "[N] -> { : N >= 10 }";
5144 set = isl_set_read_from_str(ctx, str);
5145 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5146 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5147 schedule = isl_union_map_read_from_str(ctx, str);
5149 data.before = 0;
5150 data.after = 0;
5151 data.depth = 0;
5152 build = isl_ast_build_from_context(set);
5153 build = isl_ast_build_set_before_each_for(build,
5154 &before_for, &data);
5155 build = isl_ast_build_set_after_each_for(build,
5156 &after_for, &data);
5157 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5158 isl_ast_build_free(build);
5159 if (!tree)
5160 return -1;
5162 isl_ast_node_free(tree);
5164 if (data.before != 3 || data.after != 3)
5165 isl_die(ctx, isl_error_unknown,
5166 "unexpected number of for nodes", return -1);
5168 return 0;
5171 /* Check that the AST generator handles domains that are integrally disjoint
5172 * but not rationally disjoint.
5174 static int test_ast_gen2(isl_ctx *ctx)
5176 const char *str;
5177 isl_set *set;
5178 isl_union_map *schedule;
5179 isl_union_map *options;
5180 isl_ast_build *build;
5181 isl_ast_node *tree;
5183 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5184 schedule = isl_union_map_read_from_str(ctx, str);
5185 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5186 build = isl_ast_build_from_context(set);
5188 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5189 options = isl_union_map_read_from_str(ctx, str);
5190 build = isl_ast_build_set_options(build, options);
5191 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5192 isl_ast_build_free(build);
5193 if (!tree)
5194 return -1;
5195 isl_ast_node_free(tree);
5197 return 0;
5200 /* Increment *user on each call.
5202 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5203 __isl_keep isl_ast_build *build, void *user)
5205 int *n = user;
5207 (*n)++;
5209 return node;
5212 /* Test that unrolling tries to minimize the number of instances.
5213 * In particular, for the schedule given below, make sure it generates
5214 * 3 nodes (rather than 101).
5216 static int test_ast_gen3(isl_ctx *ctx)
5218 const char *str;
5219 isl_set *set;
5220 isl_union_map *schedule;
5221 isl_union_map *options;
5222 isl_ast_build *build;
5223 isl_ast_node *tree;
5224 int n_domain = 0;
5226 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5227 schedule = isl_union_map_read_from_str(ctx, str);
5228 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5230 str = "{ [i] -> unroll[0] }";
5231 options = isl_union_map_read_from_str(ctx, str);
5233 build = isl_ast_build_from_context(set);
5234 build = isl_ast_build_set_options(build, options);
5235 build = isl_ast_build_set_at_each_domain(build,
5236 &count_domains, &n_domain);
5237 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5238 isl_ast_build_free(build);
5239 if (!tree)
5240 return -1;
5242 isl_ast_node_free(tree);
5244 if (n_domain != 3)
5245 isl_die(ctx, isl_error_unknown,
5246 "unexpected number of for nodes", return -1);
5248 return 0;
5251 /* Check that if the ast_build_exploit_nested_bounds options is set,
5252 * we do not get an outer if node in the generated AST,
5253 * while we do get such an outer if node if the options is not set.
5255 static int test_ast_gen4(isl_ctx *ctx)
5257 const char *str;
5258 isl_set *set;
5259 isl_union_map *schedule;
5260 isl_ast_build *build;
5261 isl_ast_node *tree;
5262 enum isl_ast_node_type type;
5263 int enb;
5265 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5266 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5268 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5270 schedule = isl_union_map_read_from_str(ctx, str);
5271 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5272 build = isl_ast_build_from_context(set);
5273 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5274 isl_ast_build_free(build);
5275 if (!tree)
5276 return -1;
5278 type = isl_ast_node_get_type(tree);
5279 isl_ast_node_free(tree);
5281 if (type == isl_ast_node_if)
5282 isl_die(ctx, isl_error_unknown,
5283 "not expecting if node", return -1);
5285 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5287 schedule = isl_union_map_read_from_str(ctx, str);
5288 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5289 build = isl_ast_build_from_context(set);
5290 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5291 isl_ast_build_free(build);
5292 if (!tree)
5293 return -1;
5295 type = isl_ast_node_get_type(tree);
5296 isl_ast_node_free(tree);
5298 if (type != isl_ast_node_if)
5299 isl_die(ctx, isl_error_unknown,
5300 "expecting if node", return -1);
5302 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5304 return 0;
5307 /* This function is called for each leaf in the AST generated
5308 * from test_ast_gen5.
5310 * We finalize the AST generation by extending the outer schedule
5311 * with a zero-dimensional schedule. If this results in any for loops,
5312 * then this means that we did not pass along enough information
5313 * about the outer schedule to the inner AST generation.
5315 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5316 void *user)
5318 isl_union_map *schedule, *extra;
5319 isl_ast_node *tree;
5321 schedule = isl_ast_build_get_schedule(build);
5322 extra = isl_union_map_copy(schedule);
5323 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5324 schedule = isl_union_map_range_product(schedule, extra);
5325 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5326 isl_ast_build_free(build);
5328 if (!tree)
5329 return NULL;
5331 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5332 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5333 "code should not contain any for loop",
5334 return isl_ast_node_free(tree));
5336 return tree;
5339 /* Check that we do not lose any information when going back and
5340 * forth between internal and external schedule.
5342 * In particular, we create an AST where we unroll the only
5343 * non-constant dimension in the schedule. We therefore do
5344 * not expect any for loops in the AST. However, older versions
5345 * of isl would not pass along enough information about the outer
5346 * schedule when performing an inner code generation from a create_leaf
5347 * callback, resulting in the inner code generation producing a for loop.
5349 static int test_ast_gen5(isl_ctx *ctx)
5351 const char *str;
5352 isl_set *set;
5353 isl_union_map *schedule, *options;
5354 isl_ast_build *build;
5355 isl_ast_node *tree;
5357 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5358 schedule = isl_union_map_read_from_str(ctx, str);
5360 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5361 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5362 options = isl_union_map_read_from_str(ctx, str);
5364 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5365 build = isl_ast_build_from_context(set);
5366 build = isl_ast_build_set_options(build, options);
5367 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5368 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5369 isl_ast_build_free(build);
5370 isl_ast_node_free(tree);
5371 if (!tree)
5372 return -1;
5374 return 0;
5377 static int test_ast_gen(isl_ctx *ctx)
5379 if (test_ast_gen1(ctx) < 0)
5380 return -1;
5381 if (test_ast_gen2(ctx) < 0)
5382 return -1;
5383 if (test_ast_gen3(ctx) < 0)
5384 return -1;
5385 if (test_ast_gen4(ctx) < 0)
5386 return -1;
5387 if (test_ast_gen5(ctx) < 0)
5388 return -1;
5389 return 0;
5392 /* Check if dropping output dimensions from an isl_pw_multi_aff
5393 * works properly.
5395 static int test_pw_multi_aff(isl_ctx *ctx)
5397 const char *str;
5398 isl_pw_multi_aff *pma1, *pma2;
5399 int equal;
5401 str = "{ [i,j] -> [i+j, 4i-j] }";
5402 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5403 str = "{ [i,j] -> [4i-j] }";
5404 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5406 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5408 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5410 isl_pw_multi_aff_free(pma1);
5411 isl_pw_multi_aff_free(pma2);
5412 if (equal < 0)
5413 return -1;
5414 if (!equal)
5415 isl_die(ctx, isl_error_unknown,
5416 "expressions not equal", return -1);
5418 return 0;
5421 /* Check that we can properly parse multi piecewise affine expressions
5422 * where the piecewise affine expressions have different domains.
5424 static int test_multi_pw_aff(isl_ctx *ctx)
5426 const char *str;
5427 isl_set *dom, *dom2;
5428 isl_multi_pw_aff *mpa1, *mpa2;
5429 isl_pw_aff *pa;
5430 int equal;
5431 int equal_domain;
5433 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5434 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5435 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5436 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5437 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5438 str = "{ [i] -> [(i : i > 0), 2i] }";
5439 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5441 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5443 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5444 dom = isl_pw_aff_domain(pa);
5445 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5446 dom2 = isl_pw_aff_domain(pa);
5447 equal_domain = isl_set_is_equal(dom, dom2);
5449 isl_set_free(dom);
5450 isl_set_free(dom2);
5451 isl_multi_pw_aff_free(mpa1);
5452 isl_multi_pw_aff_free(mpa2);
5454 if (equal < 0)
5455 return -1;
5456 if (!equal)
5457 isl_die(ctx, isl_error_unknown,
5458 "expressions not equal", return -1);
5460 if (equal_domain < 0)
5461 return -1;
5462 if (equal_domain)
5463 isl_die(ctx, isl_error_unknown,
5464 "domains unexpectedly equal", return -1);
5466 return 0;
5469 /* This is a regression test for a bug where isl_basic_map_simplify
5470 * would end up in an infinite loop. In particular, we construct
5471 * an empty basic set that is not obviously empty.
5472 * isl_basic_set_is_empty marks the basic set as empty.
5473 * After projecting out i3, the variable can be dropped completely,
5474 * but isl_basic_map_simplify refrains from doing so if the basic set
5475 * is empty and would end up in an infinite loop if it didn't test
5476 * explicitly for empty basic maps in the outer loop.
5478 static int test_simplify(isl_ctx *ctx)
5480 const char *str;
5481 isl_basic_set *bset;
5482 int empty;
5484 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5485 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5486 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5487 "i3 >= i2 }";
5488 bset = isl_basic_set_read_from_str(ctx, str);
5489 empty = isl_basic_set_is_empty(bset);
5490 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5491 isl_basic_set_free(bset);
5492 if (!bset)
5493 return -1;
5494 if (!empty)
5495 isl_die(ctx, isl_error_unknown,
5496 "basic set should be empty", return -1);
5498 return 0;
5501 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5502 * with gbr context would fail to disable the use of the shifted tableau
5503 * when transferring equalities for the input to the context, resulting
5504 * in invalid sample values.
5506 static int test_partial_lexmin(isl_ctx *ctx)
5508 const char *str;
5509 isl_basic_set *bset;
5510 isl_basic_map *bmap;
5511 isl_map *map;
5513 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5514 bmap = isl_basic_map_read_from_str(ctx, str);
5515 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5516 bset = isl_basic_set_read_from_str(ctx, str);
5517 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5518 isl_map_free(map);
5520 if (!map)
5521 return -1;
5523 return 0;
5526 /* Check that the variable compression performed on the existentially
5527 * quantified variables inside isl_basic_set_compute_divs is not confused
5528 * by the implicit equalities among the parameters.
5530 static int test_compute_divs(isl_ctx *ctx)
5532 const char *str;
5533 isl_basic_set *bset;
5534 isl_set *set;
5536 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5537 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5538 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5539 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5540 bset = isl_basic_set_read_from_str(ctx, str);
5541 set = isl_basic_set_compute_divs(bset);
5542 isl_set_free(set);
5543 if (!set)
5544 return -1;
5546 return 0;
5549 /* Check that the reaching domain elements and the prefix schedule
5550 * at a leaf node are the same before and after grouping.
5552 static int test_schedule_tree_group_1(isl_ctx *ctx)
5554 int equal;
5555 const char *str;
5556 isl_id *id;
5557 isl_union_set *uset;
5558 isl_multi_union_pw_aff *mupa;
5559 isl_union_pw_multi_aff *upma1, *upma2;
5560 isl_union_set *domain1, *domain2;
5561 isl_union_map *umap1, *umap2;
5562 isl_schedule_node *node;
5564 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5565 uset = isl_union_set_read_from_str(ctx, str);
5566 node = isl_schedule_node_from_domain(uset);
5567 node = isl_schedule_node_child(node, 0);
5568 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5569 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5570 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5571 node = isl_schedule_node_child(node, 0);
5572 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5573 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5574 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5575 node = isl_schedule_node_child(node, 0);
5576 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5577 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5578 domain1 = isl_schedule_node_get_domain(node);
5579 id = isl_id_alloc(ctx, "group", NULL);
5580 node = isl_schedule_node_parent(node);
5581 node = isl_schedule_node_group(node, id);
5582 node = isl_schedule_node_child(node, 0);
5583 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5584 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5585 domain2 = isl_schedule_node_get_domain(node);
5586 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5587 if (equal >= 0 && equal)
5588 equal = isl_union_set_is_equal(domain1, domain2);
5589 if (equal >= 0 && equal)
5590 equal = isl_union_map_is_equal(umap1, umap2);
5591 isl_union_map_free(umap1);
5592 isl_union_map_free(umap2);
5593 isl_union_set_free(domain1);
5594 isl_union_set_free(domain2);
5595 isl_union_pw_multi_aff_free(upma1);
5596 isl_union_pw_multi_aff_free(upma2);
5597 isl_schedule_node_free(node);
5599 if (equal < 0)
5600 return -1;
5601 if (!equal)
5602 isl_die(ctx, isl_error_unknown,
5603 "expressions not equal", return -1);
5605 return 0;
5608 /* Check that we can have nested groupings and that the union map
5609 * schedule representation is the same before and after the grouping.
5610 * Note that after the grouping, the union map representation contains
5611 * the domain constraints from the ranges of the expansion nodes,
5612 * while they are missing from the union map representation of
5613 * the tree without expansion nodes.
5615 * Also check that the global expansion is as expected.
5617 static int test_schedule_tree_group_2(isl_ctx *ctx)
5619 int equal, equal_expansion;
5620 const char *str;
5621 isl_id *id;
5622 isl_union_set *uset;
5623 isl_union_map *umap1, *umap2;
5624 isl_union_map *expansion1, *expansion2;
5625 isl_union_set_list *filters;
5626 isl_multi_union_pw_aff *mupa;
5627 isl_schedule *schedule;
5628 isl_schedule_node *node;
5630 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
5631 "S3[i,j] : 0 <= i,j < 10 }";
5632 uset = isl_union_set_read_from_str(ctx, str);
5633 node = isl_schedule_node_from_domain(uset);
5634 node = isl_schedule_node_child(node, 0);
5635 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
5636 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5637 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5638 node = isl_schedule_node_child(node, 0);
5639 str = "{ S1[i,j] }";
5640 uset = isl_union_set_read_from_str(ctx, str);
5641 filters = isl_union_set_list_from_union_set(uset);
5642 str = "{ S2[i,j]; S3[i,j] }";
5643 uset = isl_union_set_read_from_str(ctx, str);
5644 filters = isl_union_set_list_add(filters, uset);
5645 node = isl_schedule_node_insert_sequence(node, filters);
5646 node = isl_schedule_node_child(node, 1);
5647 node = isl_schedule_node_child(node, 0);
5648 str = "{ S2[i,j] }";
5649 uset = isl_union_set_read_from_str(ctx, str);
5650 filters = isl_union_set_list_from_union_set(uset);
5651 str = "{ S3[i,j] }";
5652 uset = isl_union_set_read_from_str(ctx, str);
5653 filters = isl_union_set_list_add(filters, uset);
5654 node = isl_schedule_node_insert_sequence(node, filters);
5656 schedule = isl_schedule_node_get_schedule(node);
5657 umap1 = isl_schedule_get_map(schedule);
5658 uset = isl_schedule_get_domain(schedule);
5659 umap1 = isl_union_map_intersect_domain(umap1, uset);
5660 isl_schedule_free(schedule);
5662 node = isl_schedule_node_parent(node);
5663 node = isl_schedule_node_parent(node);
5664 id = isl_id_alloc(ctx, "group1", NULL);
5665 node = isl_schedule_node_group(node, id);
5666 node = isl_schedule_node_child(node, 1);
5667 node = isl_schedule_node_child(node, 0);
5668 id = isl_id_alloc(ctx, "group2", NULL);
5669 node = isl_schedule_node_group(node, id);
5671 schedule = isl_schedule_node_get_schedule(node);
5672 umap2 = isl_schedule_get_map(schedule);
5673 isl_schedule_free(schedule);
5675 node = isl_schedule_node_root(node);
5676 node = isl_schedule_node_child(node, 0);
5677 expansion1 = isl_schedule_node_get_subtree_expansion(node);
5678 isl_schedule_node_free(node);
5680 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
5681 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
5682 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
5684 expansion2 = isl_union_map_read_from_str(ctx, str);
5686 equal = isl_union_map_is_equal(umap1, umap2);
5687 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
5689 isl_union_map_free(umap1);
5690 isl_union_map_free(umap2);
5691 isl_union_map_free(expansion1);
5692 isl_union_map_free(expansion2);
5694 if (equal < 0 || equal_expansion < 0)
5695 return -1;
5696 if (!equal)
5697 isl_die(ctx, isl_error_unknown,
5698 "expressions not equal", return -1);
5699 if (!equal_expansion)
5700 isl_die(ctx, isl_error_unknown,
5701 "unexpected expansion", return -1);
5703 return 0;
5706 /* Some tests for the isl_schedule_node_group function.
5708 static int test_schedule_tree_group(isl_ctx *ctx)
5710 if (test_schedule_tree_group_1(ctx) < 0)
5711 return -1;
5712 if (test_schedule_tree_group_2(ctx) < 0)
5713 return -1;
5714 return 0;
5717 struct {
5718 const char *set;
5719 const char *dual;
5720 } coef_tests[] = {
5721 { "{ rat: [i] : 0 <= i <= 10 }",
5722 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5723 { "{ rat: [i] : FALSE }",
5724 "{ rat: coefficients[[cst] -> [a]] }" },
5725 { "{ rat: [i] : }",
5726 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5729 struct {
5730 const char *set;
5731 const char *dual;
5732 } sol_tests[] = {
5733 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5734 "{ rat: [i] : 0 <= i <= 10 }" },
5735 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5736 "{ rat: [i] }" },
5737 { "{ rat: coefficients[[cst] -> [a]] }",
5738 "{ rat: [i] : FALSE }" },
5741 /* Test the basic functionality of isl_basic_set_coefficients and
5742 * isl_basic_set_solutions.
5744 static int test_dual(isl_ctx *ctx)
5746 int i;
5748 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5749 int equal;
5750 isl_basic_set *bset1, *bset2;
5752 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5753 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5754 bset1 = isl_basic_set_coefficients(bset1);
5755 equal = isl_basic_set_is_equal(bset1, bset2);
5756 isl_basic_set_free(bset1);
5757 isl_basic_set_free(bset2);
5758 if (equal < 0)
5759 return -1;
5760 if (!equal)
5761 isl_die(ctx, isl_error_unknown,
5762 "incorrect dual", return -1);
5765 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5766 int equal;
5767 isl_basic_set *bset1, *bset2;
5769 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5770 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5771 bset1 = isl_basic_set_solutions(bset1);
5772 equal = isl_basic_set_is_equal(bset1, bset2);
5773 isl_basic_set_free(bset1);
5774 isl_basic_set_free(bset2);
5775 if (equal < 0)
5776 return -1;
5777 if (!equal)
5778 isl_die(ctx, isl_error_unknown,
5779 "incorrect dual", return -1);
5782 return 0;
5785 struct {
5786 int scale_tile;
5787 int shift_point;
5788 const char *domain;
5789 const char *schedule;
5790 const char *sizes;
5791 const char *tile;
5792 const char *point;
5793 } tile_tests[] = {
5794 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5795 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5796 "{ [32,32] }",
5797 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5798 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5800 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5801 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5802 "{ [32,32] }",
5803 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5804 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5806 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5807 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5808 "{ [32,32] }",
5809 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5810 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5812 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5813 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5814 "{ [32,32] }",
5815 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5816 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5820 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
5821 * tile the band and then check if the tile and point bands have the
5822 * expected partial schedule.
5824 static int test_tile(isl_ctx *ctx)
5826 int i;
5827 int scale;
5828 int shift;
5830 scale = isl_options_get_tile_scale_tile_loops(ctx);
5831 shift = isl_options_get_tile_shift_point_loops(ctx);
5833 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
5834 int opt;
5835 int equal;
5836 const char *str;
5837 isl_union_set *domain;
5838 isl_multi_union_pw_aff *mupa, *mupa2;
5839 isl_schedule_node *node;
5840 isl_multi_val *sizes;
5842 opt = tile_tests[i].scale_tile;
5843 isl_options_set_tile_scale_tile_loops(ctx, opt);
5844 opt = tile_tests[i].shift_point;
5845 isl_options_set_tile_shift_point_loops(ctx, opt);
5847 str = tile_tests[i].domain;
5848 domain = isl_union_set_read_from_str(ctx, str);
5849 node = isl_schedule_node_from_domain(domain);
5850 node = isl_schedule_node_child(node, 0);
5851 str = tile_tests[i].schedule;
5852 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5853 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5854 str = tile_tests[i].sizes;
5855 sizes = isl_multi_val_read_from_str(ctx, str);
5856 node = isl_schedule_node_band_tile(node, sizes);
5858 str = tile_tests[i].tile;
5859 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5860 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
5861 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
5862 isl_multi_union_pw_aff_free(mupa);
5863 isl_multi_union_pw_aff_free(mupa2);
5865 node = isl_schedule_node_child(node, 0);
5867 str = tile_tests[i].point;
5868 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5869 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
5870 if (equal >= 0 && equal)
5871 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
5872 mupa2);
5873 isl_multi_union_pw_aff_free(mupa);
5874 isl_multi_union_pw_aff_free(mupa2);
5876 isl_schedule_node_free(node);
5878 if (equal < 0)
5879 return -1;
5880 if (!equal)
5881 isl_die(ctx, isl_error_unknown,
5882 "unexpected result", return -1);
5885 isl_options_set_tile_scale_tile_loops(ctx, scale);
5886 isl_options_set_tile_shift_point_loops(ctx, shift);
5888 return 0;
5891 struct {
5892 const char *name;
5893 int (*fn)(isl_ctx *ctx);
5894 } tests [] = {
5895 { "dual", &test_dual },
5896 { "dependence analysis", &test_flow },
5897 { "val", &test_val },
5898 { "compute divs", &test_compute_divs },
5899 { "partial lexmin", &test_partial_lexmin },
5900 { "simplify", &test_simplify },
5901 { "curry", &test_curry },
5902 { "piecewise multi affine expressions", &test_pw_multi_aff },
5903 { "multi piecewise affine expressions", &test_multi_pw_aff },
5904 { "conversion", &test_conversion },
5905 { "list", &test_list },
5906 { "align parameters", &test_align_parameters },
5907 { "preimage", &test_preimage },
5908 { "pullback", &test_pullback },
5909 { "AST", &test_ast },
5910 { "AST build", &test_ast_build },
5911 { "AST generation", &test_ast_gen },
5912 { "eliminate", &test_eliminate },
5913 { "residue class", &test_residue_class },
5914 { "div", &test_div },
5915 { "slice", &test_slice },
5916 { "fixed power", &test_fixed_power },
5917 { "sample", &test_sample },
5918 { "output", &test_output },
5919 { "vertices", &test_vertices },
5920 { "fixed", &test_fixed },
5921 { "equal", &test_equal },
5922 { "disjoint", &test_disjoint },
5923 { "product", &test_product },
5924 { "dim_max", &test_dim_max },
5925 { "affine", &test_aff },
5926 { "injective", &test_injective },
5927 { "schedule", &test_schedule },
5928 { "schedule tree grouping", &test_schedule_tree_group },
5929 { "tile", &test_tile },
5930 { "union_pw", &test_union_pw },
5931 { "parse", &test_parse },
5932 { "single-valued", &test_sv },
5933 { "affine hull", &test_affine_hull },
5934 { "coalesce", &test_coalesce },
5935 { "factorize", &test_factorize },
5936 { "subset", &test_subset },
5937 { "subtract", &test_subtract },
5938 { "lexmin", &test_lexmin },
5939 { "min", &test_min },
5940 { "gist", &test_gist },
5941 { "piecewise quasi-polynomials", &test_pwqp },
5942 { "lift", &test_lift },
5943 { "bound", &test_bound },
5944 { "union", &test_union },
5945 { "split periods", &test_split_periods },
5946 { "lexicographic order", &test_lex },
5947 { "bijectivity", &test_bijective },
5948 { "dataflow analysis", &test_dep },
5949 { "reading", &test_read },
5950 { "bounded", &test_bounded },
5951 { "construction", &test_construction },
5952 { "dimension manipulation", &test_dim },
5953 { "map application", &test_application },
5954 { "convex hull", &test_convex_hull },
5955 { "transitive closure", &test_closure },
5958 int main(int argc, char **argv)
5960 int i;
5961 struct isl_ctx *ctx;
5962 struct isl_options *options;
5964 srcdir = getenv("srcdir");
5965 assert(srcdir);
5967 options = isl_options_new_with_defaults();
5968 assert(options);
5969 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5971 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5972 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5973 printf("%s\n", tests[i].name);
5974 if (tests[i].fn(ctx) < 0)
5975 goto error;
5977 isl_ctx_free(ctx);
5978 return 0;
5979 error:
5980 isl_ctx_free(ctx);
5981 return -1;