Merge branch 'maint'
[isl.git] / isl_test.c
blob311374b081611c9efbaf181b14ec2612fa5fb975
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <assert.h>
16 #include <stdio.h>
17 #include <limits.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl/set.h>
22 #include <isl/flow.h>
23 #include <isl_constraint_private.h>
24 #include <isl/polynomial.h>
25 #include <isl/union_map.h>
26 #include <isl_factorization.h>
27 #include <isl/schedule.h>
28 #include <isl_options_private.h>
29 #include <isl/vertices.h>
30 #include <isl/ast_build.h>
31 #include <isl/val.h>
32 #include <isl/ilp.h>
34 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
36 static char *srcdir;
38 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
39 char *filename;
40 int length;
41 char *pattern = "%s/test_inputs/%s.%s";
43 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
44 + strlen(suffix) + 1;
45 filename = isl_alloc_array(ctx, char, length);
47 if (!filename)
48 return NULL;
50 sprintf(filename, pattern, srcdir, name, suffix);
52 return filename;
55 void test_parse_map(isl_ctx *ctx, const char *str)
57 isl_map *map;
59 map = isl_map_read_from_str(ctx, str);
60 assert(map);
61 isl_map_free(map);
64 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
66 isl_map *map, *map2;
67 int equal;
69 map = isl_map_read_from_str(ctx, str);
70 map2 = isl_map_read_from_str(ctx, str2);
71 equal = isl_map_is_equal(map, map2);
72 isl_map_free(map);
73 isl_map_free(map2);
75 if (equal < 0)
76 return -1;
77 if (!equal)
78 isl_die(ctx, isl_error_unknown, "maps not equal",
79 return -1);
81 return 0;
84 void test_parse_pwqp(isl_ctx *ctx, const char *str)
86 isl_pw_qpolynomial *pwqp;
88 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
89 assert(pwqp);
90 isl_pw_qpolynomial_free(pwqp);
93 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
95 isl_pw_aff *pwaff;
97 pwaff = isl_pw_aff_read_from_str(ctx, str);
98 assert(pwaff);
99 isl_pw_aff_free(pwaff);
102 int test_parse(struct isl_ctx *ctx)
104 isl_map *map, *map2;
105 const char *str, *str2;
107 str = "{ [i] -> [-i] }";
108 map = isl_map_read_from_str(ctx, str);
109 assert(map);
110 isl_map_free(map);
112 str = "{ A[i] -> L[([i/3])] }";
113 map = isl_map_read_from_str(ctx, str);
114 assert(map);
115 isl_map_free(map);
117 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
118 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
119 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
121 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
122 str2 = "{ [x, y] : 2y >= 6 - x }";
123 if (test_parse_map_equal(ctx, str, str2) < 0)
124 return -1;
126 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
127 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
128 return -1;
129 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
130 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
131 str) < 0)
132 return -1;
134 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
135 map = isl_map_read_from_str(ctx, str);
136 str = "{ [new, old] -> [o0, o1] : "
137 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
138 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
139 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
140 map2 = isl_map_read_from_str(ctx, str);
141 assert(isl_map_is_equal(map, map2));
142 isl_map_free(map);
143 isl_map_free(map2);
145 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
146 map = isl_map_read_from_str(ctx, str);
147 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
148 map2 = isl_map_read_from_str(ctx, str);
149 assert(isl_map_is_equal(map, map2));
150 isl_map_free(map);
151 isl_map_free(map2);
153 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
154 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
155 if (test_parse_map_equal(ctx, str, str2) < 0)
156 return -1;
158 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
159 str2 = "{ [i,j] -> [min(i,j)] }";
160 if (test_parse_map_equal(ctx, str, str2) < 0)
161 return -1;
163 str = "{ [i,j] : i != j }";
164 str2 = "{ [i,j] : i < j or i > j }";
165 if (test_parse_map_equal(ctx, str, str2) < 0)
166 return -1;
168 str = "{ [i,j] : (i+1)*2 >= j }";
169 str2 = "{ [i, j] : j <= 2 + 2i }";
170 if (test_parse_map_equal(ctx, str, str2) < 0)
171 return -1;
173 str = "{ [i] -> [i > 0 ? 4 : 5] }";
174 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
175 if (test_parse_map_equal(ctx, str, str2) < 0)
176 return -1;
178 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
179 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
180 if (test_parse_map_equal(ctx, str, str2) < 0)
181 return -1;
183 str = "{ [x] : x >= 0 }";
184 str2 = "{ [x] : x-0 >= 0 }";
185 if (test_parse_map_equal(ctx, str, str2) < 0)
186 return -1;
188 str = "{ [i] : ((i > 10)) }";
189 str2 = "{ [i] : i >= 11 }";
190 if (test_parse_map_equal(ctx, str, str2) < 0)
191 return -1;
193 str = "{ [i] -> [0] }";
194 str2 = "{ [i] -> [0 * i] }";
195 if (test_parse_map_equal(ctx, str, str2) < 0)
196 return -1;
198 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
199 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
200 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
201 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
203 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
204 "{ [a] -> [b] : true }") < 0)
205 return -1;
207 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
208 "{ [i] : i <= 10 }") < 0)
209 return -1;
211 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
212 "[n] -> { [i] : i <= n }") < 0)
213 return -1;
215 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
216 return -1;
218 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
219 "{ [i] : exists a : i = 2 a }") < 0)
220 return -1;
222 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
223 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
224 return -1;
226 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
227 "{ [a] -> [a - 1] : a > 0 }") < 0)
228 return -1;
229 if (test_parse_map_equal(ctx,
230 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
231 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
232 return -1;
233 if (test_parse_map_equal(ctx,
234 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
235 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
236 return -1;
237 if (test_parse_map_equal(ctx,
238 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
239 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
240 return -1;
241 if (test_parse_map_equal(ctx,
242 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
243 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
244 return -1;
245 if (test_parse_map_equal(ctx,
246 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
247 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
248 return -1;
250 return 0;
253 void test_read(struct isl_ctx *ctx)
255 char *filename;
256 FILE *input;
257 struct isl_basic_set *bset1, *bset2;
258 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
260 filename = get_filename(ctx, "set", "omega");
261 assert(filename);
262 input = fopen(filename, "r");
263 assert(input);
265 bset1 = isl_basic_set_read_from_file(ctx, input);
266 bset2 = isl_basic_set_read_from_str(ctx, str);
268 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
270 isl_basic_set_free(bset1);
271 isl_basic_set_free(bset2);
272 free(filename);
274 fclose(input);
277 void test_bounded(struct isl_ctx *ctx)
279 isl_set *set;
280 int bounded;
282 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
283 bounded = isl_set_is_bounded(set);
284 assert(bounded);
285 isl_set_free(set);
287 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
288 bounded = isl_set_is_bounded(set);
289 assert(!bounded);
290 isl_set_free(set);
292 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
293 bounded = isl_set_is_bounded(set);
294 assert(!bounded);
295 isl_set_free(set);
298 /* Construct the basic set { [i] : 5 <= i <= N } */
299 void test_construction(struct isl_ctx *ctx)
301 isl_int v;
302 isl_space *dim;
303 isl_local_space *ls;
304 struct isl_basic_set *bset;
305 struct isl_constraint *c;
307 isl_int_init(v);
309 dim = isl_space_set_alloc(ctx, 1, 1);
310 bset = isl_basic_set_universe(isl_space_copy(dim));
311 ls = isl_local_space_from_space(dim);
313 c = isl_inequality_alloc(isl_local_space_copy(ls));
314 isl_int_set_si(v, -1);
315 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
316 isl_int_set_si(v, 1);
317 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
318 bset = isl_basic_set_add_constraint(bset, c);
320 c = isl_inequality_alloc(isl_local_space_copy(ls));
321 isl_int_set_si(v, 1);
322 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
323 isl_int_set_si(v, -5);
324 isl_constraint_set_constant(c, v);
325 bset = isl_basic_set_add_constraint(bset, c);
327 isl_local_space_free(ls);
328 isl_basic_set_free(bset);
330 isl_int_clear(v);
333 void test_dim(struct isl_ctx *ctx)
335 const char *str;
336 isl_map *map1, *map2;
338 map1 = isl_map_read_from_str(ctx,
339 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
340 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
341 map2 = isl_map_read_from_str(ctx,
342 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
343 assert(isl_map_is_equal(map1, map2));
344 isl_map_free(map2);
346 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
347 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
348 assert(isl_map_is_equal(map1, map2));
350 isl_map_free(map1);
351 isl_map_free(map2);
353 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
354 map1 = isl_map_read_from_str(ctx, str);
355 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
356 map2 = isl_map_read_from_str(ctx, str);
357 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
358 assert(isl_map_is_equal(map1, map2));
360 isl_map_free(map1);
361 isl_map_free(map2);
364 struct {
365 __isl_give isl_val *(*op)(__isl_take isl_val *v);
366 const char *arg;
367 const char *res;
368 } val_un_tests[] = {
369 { &isl_val_neg, "0", "0" },
370 { &isl_val_abs, "0", "0" },
371 { &isl_val_2exp, "0", "1" },
372 { &isl_val_floor, "0", "0" },
373 { &isl_val_ceil, "0", "0" },
374 { &isl_val_neg, "1", "-1" },
375 { &isl_val_neg, "-1", "1" },
376 { &isl_val_neg, "1/2", "-1/2" },
377 { &isl_val_neg, "-1/2", "1/2" },
378 { &isl_val_neg, "infty", "-infty" },
379 { &isl_val_neg, "-infty", "infty" },
380 { &isl_val_neg, "NaN", "NaN" },
381 { &isl_val_abs, "1", "1" },
382 { &isl_val_abs, "-1", "1" },
383 { &isl_val_abs, "1/2", "1/2" },
384 { &isl_val_abs, "-1/2", "1/2" },
385 { &isl_val_abs, "infty", "infty" },
386 { &isl_val_abs, "-infty", "infty" },
387 { &isl_val_abs, "NaN", "NaN" },
388 { &isl_val_floor, "1", "1" },
389 { &isl_val_floor, "-1", "-1" },
390 { &isl_val_floor, "1/2", "0" },
391 { &isl_val_floor, "-1/2", "-1" },
392 { &isl_val_floor, "infty", "infty" },
393 { &isl_val_floor, "-infty", "-infty" },
394 { &isl_val_floor, "NaN", "NaN" },
395 { &isl_val_ceil, "1", "1" },
396 { &isl_val_ceil, "-1", "-1" },
397 { &isl_val_ceil, "1/2", "1" },
398 { &isl_val_ceil, "-1/2", "0" },
399 { &isl_val_ceil, "infty", "infty" },
400 { &isl_val_ceil, "-infty", "-infty" },
401 { &isl_val_ceil, "NaN", "NaN" },
402 { &isl_val_2exp, "-3", "1/8" },
403 { &isl_val_2exp, "-1", "1/2" },
404 { &isl_val_2exp, "1", "2" },
405 { &isl_val_2exp, "2", "4" },
406 { &isl_val_2exp, "3", "8" },
409 /* Perform some basic tests of unary operations on isl_val objects.
411 static int test_un_val(isl_ctx *ctx)
413 int i;
414 isl_val *v, *res;
415 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
416 int ok;
418 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
419 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
420 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
421 fn = val_un_tests[i].op;
422 v = fn(v);
423 if (isl_val_is_nan(res))
424 ok = isl_val_is_nan(v);
425 else
426 ok = isl_val_eq(v, res);
427 isl_val_free(v);
428 isl_val_free(res);
429 if (ok < 0)
430 return -1;
431 if (!ok)
432 isl_die(ctx, isl_error_unknown,
433 "unexpected result", return -1);
436 return 0;
439 struct {
440 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
441 __isl_take isl_val *v2);
442 } val_bin_op[] = {
443 ['+'] = { &isl_val_add },
444 ['-'] = { &isl_val_sub },
445 ['*'] = { &isl_val_mul },
446 ['/'] = { &isl_val_div },
447 ['g'] = { &isl_val_gcd },
448 ['m'] = { &isl_val_min },
449 ['M'] = { &isl_val_max },
452 struct {
453 const char *arg1;
454 unsigned char op;
455 const char *arg2;
456 const char *res;
457 } val_bin_tests[] = {
458 { "0", '+', "0", "0" },
459 { "1", '+', "0", "1" },
460 { "1", '+', "1", "2" },
461 { "1", '-', "1", "0" },
462 { "1", '*', "1", "1" },
463 { "1", '/', "1", "1" },
464 { "2", '*', "3", "6" },
465 { "2", '*', "1/2", "1" },
466 { "2", '*', "1/3", "2/3" },
467 { "2/3", '*', "3/5", "2/5" },
468 { "2/3", '*', "7/5", "14/15" },
469 { "2", '/', "1/2", "4" },
470 { "-2", '/', "-1/2", "4" },
471 { "-2", '/', "1/2", "-4" },
472 { "2", '/', "-1/2", "-4" },
473 { "2", '/', "2", "1" },
474 { "2", '/', "3", "2/3" },
475 { "2/3", '/', "5/3", "2/5" },
476 { "2/3", '/', "5/7", "14/15" },
477 { "0", '/', "0", "NaN" },
478 { "42", '/', "0", "NaN" },
479 { "-42", '/', "0", "NaN" },
480 { "infty", '/', "0", "NaN" },
481 { "-infty", '/', "0", "NaN" },
482 { "NaN", '/', "0", "NaN" },
483 { "0", '/', "NaN", "NaN" },
484 { "42", '/', "NaN", "NaN" },
485 { "-42", '/', "NaN", "NaN" },
486 { "infty", '/', "NaN", "NaN" },
487 { "-infty", '/', "NaN", "NaN" },
488 { "NaN", '/', "NaN", "NaN" },
489 { "0", '/', "infty", "0" },
490 { "42", '/', "infty", "0" },
491 { "-42", '/', "infty", "0" },
492 { "infty", '/', "infty", "NaN" },
493 { "-infty", '/', "infty", "NaN" },
494 { "NaN", '/', "infty", "NaN" },
495 { "0", '/', "-infty", "0" },
496 { "42", '/', "-infty", "0" },
497 { "-42", '/', "-infty", "0" },
498 { "infty", '/', "-infty", "NaN" },
499 { "-infty", '/', "-infty", "NaN" },
500 { "NaN", '/', "-infty", "NaN" },
501 { "1", '-', "1/3", "2/3" },
502 { "1/3", '+', "1/2", "5/6" },
503 { "1/2", '+', "1/2", "1" },
504 { "3/4", '-', "1/4", "1/2" },
505 { "1/2", '-', "1/3", "1/6" },
506 { "infty", '+', "42", "infty" },
507 { "infty", '+', "infty", "infty" },
508 { "42", '+', "infty", "infty" },
509 { "infty", '-', "infty", "NaN" },
510 { "infty", '*', "infty", "infty" },
511 { "infty", '*', "-infty", "-infty" },
512 { "-infty", '*', "infty", "-infty" },
513 { "-infty", '*', "-infty", "infty" },
514 { "0", '*', "infty", "NaN" },
515 { "1", '*', "infty", "infty" },
516 { "infty", '*', "0", "NaN" },
517 { "infty", '*', "42", "infty" },
518 { "42", '-', "infty", "-infty" },
519 { "infty", '+', "-infty", "NaN" },
520 { "4", 'g', "6", "2" },
521 { "5", 'g', "6", "1" },
522 { "42", 'm', "3", "3" },
523 { "42", 'M', "3", "42" },
524 { "3", 'm', "42", "3" },
525 { "3", 'M', "42", "42" },
526 { "42", 'm', "infty", "42" },
527 { "42", 'M', "infty", "infty" },
528 { "42", 'm', "-infty", "-infty" },
529 { "42", 'M', "-infty", "42" },
530 { "42", 'm', "NaN", "NaN" },
531 { "42", 'M', "NaN", "NaN" },
532 { "infty", 'm', "-infty", "-infty" },
533 { "infty", 'M', "-infty", "infty" },
536 /* Perform some basic tests of binary operations on isl_val objects.
538 static int test_bin_val(isl_ctx *ctx)
540 int i;
541 isl_val *v1, *v2, *res;
542 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
543 __isl_take isl_val *v2);
544 int ok;
546 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
547 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
548 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
549 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
550 fn = val_bin_op[val_bin_tests[i].op].fn;
551 v1 = fn(v1, v2);
552 if (isl_val_is_nan(res))
553 ok = isl_val_is_nan(v1);
554 else
555 ok = isl_val_eq(v1, res);
556 isl_val_free(v1);
557 isl_val_free(res);
558 if (ok < 0)
559 return -1;
560 if (!ok)
561 isl_die(ctx, isl_error_unknown,
562 "unexpected result", return -1);
565 return 0;
568 /* Perform some basic tests on isl_val objects.
570 static int test_val(isl_ctx *ctx)
572 if (test_un_val(ctx) < 0)
573 return -1;
574 if (test_bin_val(ctx) < 0)
575 return -1;
576 return 0;
579 static int test_div(isl_ctx *ctx)
581 unsigned n;
582 const char *str;
583 int empty;
584 isl_int v;
585 isl_space *dim;
586 isl_set *set;
587 isl_local_space *ls;
588 struct isl_basic_set *bset;
589 struct isl_constraint *c;
591 isl_int_init(v);
593 /* test 1 */
594 dim = isl_space_set_alloc(ctx, 0, 3);
595 bset = isl_basic_set_universe(isl_space_copy(dim));
596 ls = isl_local_space_from_space(dim);
598 c = isl_equality_alloc(isl_local_space_copy(ls));
599 isl_int_set_si(v, -1);
600 isl_constraint_set_constant(c, v);
601 isl_int_set_si(v, 1);
602 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
603 isl_int_set_si(v, 3);
604 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
605 bset = isl_basic_set_add_constraint(bset, c);
607 c = isl_equality_alloc(isl_local_space_copy(ls));
608 isl_int_set_si(v, 1);
609 isl_constraint_set_constant(c, v);
610 isl_int_set_si(v, -1);
611 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
612 isl_int_set_si(v, 3);
613 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
614 bset = isl_basic_set_add_constraint(bset, c);
616 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
618 assert(bset && bset->n_div == 1);
619 isl_local_space_free(ls);
620 isl_basic_set_free(bset);
622 /* test 2 */
623 dim = isl_space_set_alloc(ctx, 0, 3);
624 bset = isl_basic_set_universe(isl_space_copy(dim));
625 ls = isl_local_space_from_space(dim);
627 c = isl_equality_alloc(isl_local_space_copy(ls));
628 isl_int_set_si(v, 1);
629 isl_constraint_set_constant(c, v);
630 isl_int_set_si(v, -1);
631 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
632 isl_int_set_si(v, 3);
633 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
634 bset = isl_basic_set_add_constraint(bset, c);
636 c = isl_equality_alloc(isl_local_space_copy(ls));
637 isl_int_set_si(v, -1);
638 isl_constraint_set_constant(c, v);
639 isl_int_set_si(v, 1);
640 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
641 isl_int_set_si(v, 3);
642 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
643 bset = isl_basic_set_add_constraint(bset, c);
645 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
647 assert(bset && bset->n_div == 1);
648 isl_local_space_free(ls);
649 isl_basic_set_free(bset);
651 /* test 3 */
652 dim = isl_space_set_alloc(ctx, 0, 3);
653 bset = isl_basic_set_universe(isl_space_copy(dim));
654 ls = isl_local_space_from_space(dim);
656 c = isl_equality_alloc(isl_local_space_copy(ls));
657 isl_int_set_si(v, 1);
658 isl_constraint_set_constant(c, v);
659 isl_int_set_si(v, -1);
660 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
661 isl_int_set_si(v, 3);
662 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
663 bset = isl_basic_set_add_constraint(bset, c);
665 c = isl_equality_alloc(isl_local_space_copy(ls));
666 isl_int_set_si(v, -3);
667 isl_constraint_set_constant(c, v);
668 isl_int_set_si(v, 1);
669 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
670 isl_int_set_si(v, 4);
671 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
672 bset = isl_basic_set_add_constraint(bset, c);
674 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
676 assert(bset && bset->n_div == 1);
677 isl_local_space_free(ls);
678 isl_basic_set_free(bset);
680 /* test 4 */
681 dim = isl_space_set_alloc(ctx, 0, 3);
682 bset = isl_basic_set_universe(isl_space_copy(dim));
683 ls = isl_local_space_from_space(dim);
685 c = isl_equality_alloc(isl_local_space_copy(ls));
686 isl_int_set_si(v, 2);
687 isl_constraint_set_constant(c, v);
688 isl_int_set_si(v, -1);
689 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
690 isl_int_set_si(v, 3);
691 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
692 bset = isl_basic_set_add_constraint(bset, c);
694 c = isl_equality_alloc(isl_local_space_copy(ls));
695 isl_int_set_si(v, -1);
696 isl_constraint_set_constant(c, v);
697 isl_int_set_si(v, 1);
698 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
699 isl_int_set_si(v, 6);
700 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
701 bset = isl_basic_set_add_constraint(bset, c);
703 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
705 assert(isl_basic_set_is_empty(bset));
706 isl_local_space_free(ls);
707 isl_basic_set_free(bset);
709 /* test 5 */
710 dim = isl_space_set_alloc(ctx, 0, 3);
711 bset = isl_basic_set_universe(isl_space_copy(dim));
712 ls = isl_local_space_from_space(dim);
714 c = isl_equality_alloc(isl_local_space_copy(ls));
715 isl_int_set_si(v, -1);
716 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
717 isl_int_set_si(v, 3);
718 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
719 bset = isl_basic_set_add_constraint(bset, c);
721 c = isl_equality_alloc(isl_local_space_copy(ls));
722 isl_int_set_si(v, 1);
723 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
724 isl_int_set_si(v, -3);
725 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
726 bset = isl_basic_set_add_constraint(bset, c);
728 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
730 assert(bset && bset->n_div == 0);
731 isl_basic_set_free(bset);
732 isl_local_space_free(ls);
734 /* test 6 */
735 dim = isl_space_set_alloc(ctx, 0, 3);
736 bset = isl_basic_set_universe(isl_space_copy(dim));
737 ls = isl_local_space_from_space(dim);
739 c = isl_equality_alloc(isl_local_space_copy(ls));
740 isl_int_set_si(v, -1);
741 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
742 isl_int_set_si(v, 6);
743 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
744 bset = isl_basic_set_add_constraint(bset, c);
746 c = isl_equality_alloc(isl_local_space_copy(ls));
747 isl_int_set_si(v, 1);
748 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
749 isl_int_set_si(v, -3);
750 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
751 bset = isl_basic_set_add_constraint(bset, c);
753 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
755 assert(bset && bset->n_div == 1);
756 isl_basic_set_free(bset);
757 isl_local_space_free(ls);
759 /* test 7 */
760 /* This test is a bit tricky. We set up an equality
761 * a + 3b + 3c = 6 e0
762 * Normalization of divs creates _two_ divs
763 * a = 3 e0
764 * c - b - e0 = 2 e1
765 * Afterwards e0 is removed again because it has coefficient -1
766 * and we end up with the original equality and div again.
767 * Perhaps we can avoid the introduction of this temporary div.
769 dim = isl_space_set_alloc(ctx, 0, 4);
770 bset = isl_basic_set_universe(isl_space_copy(dim));
771 ls = isl_local_space_from_space(dim);
773 c = isl_equality_alloc(isl_local_space_copy(ls));
774 isl_int_set_si(v, -1);
775 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
776 isl_int_set_si(v, -3);
777 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
778 isl_int_set_si(v, -3);
779 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
780 isl_int_set_si(v, 6);
781 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
782 bset = isl_basic_set_add_constraint(bset, c);
784 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
786 /* Test disabled for now */
788 assert(bset && bset->n_div == 1);
790 isl_local_space_free(ls);
791 isl_basic_set_free(bset);
793 /* test 8 */
794 dim = isl_space_set_alloc(ctx, 0, 5);
795 bset = isl_basic_set_universe(isl_space_copy(dim));
796 ls = isl_local_space_from_space(dim);
798 c = isl_equality_alloc(isl_local_space_copy(ls));
799 isl_int_set_si(v, -1);
800 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
801 isl_int_set_si(v, -3);
802 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
803 isl_int_set_si(v, -3);
804 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
805 isl_int_set_si(v, 6);
806 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
807 bset = isl_basic_set_add_constraint(bset, c);
809 c = isl_equality_alloc(isl_local_space_copy(ls));
810 isl_int_set_si(v, -1);
811 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
812 isl_int_set_si(v, 1);
813 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
814 isl_int_set_si(v, 1);
815 isl_constraint_set_constant(c, v);
816 bset = isl_basic_set_add_constraint(bset, c);
818 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
820 /* Test disabled for now */
822 assert(bset && bset->n_div == 1);
824 isl_local_space_free(ls);
825 isl_basic_set_free(bset);
827 /* test 9 */
828 dim = isl_space_set_alloc(ctx, 0, 4);
829 bset = isl_basic_set_universe(isl_space_copy(dim));
830 ls = isl_local_space_from_space(dim);
832 c = isl_equality_alloc(isl_local_space_copy(ls));
833 isl_int_set_si(v, 1);
834 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
835 isl_int_set_si(v, -1);
836 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
837 isl_int_set_si(v, -2);
838 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
839 bset = isl_basic_set_add_constraint(bset, c);
841 c = isl_equality_alloc(isl_local_space_copy(ls));
842 isl_int_set_si(v, -1);
843 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
844 isl_int_set_si(v, 3);
845 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
846 isl_int_set_si(v, 2);
847 isl_constraint_set_constant(c, v);
848 bset = isl_basic_set_add_constraint(bset, c);
850 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
852 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
854 assert(!isl_basic_set_is_empty(bset));
856 isl_local_space_free(ls);
857 isl_basic_set_free(bset);
859 /* test 10 */
860 dim = isl_space_set_alloc(ctx, 0, 3);
861 bset = isl_basic_set_universe(isl_space_copy(dim));
862 ls = isl_local_space_from_space(dim);
864 c = isl_equality_alloc(isl_local_space_copy(ls));
865 isl_int_set_si(v, 1);
866 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
867 isl_int_set_si(v, -2);
868 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
869 bset = isl_basic_set_add_constraint(bset, c);
871 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
873 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
875 isl_local_space_free(ls);
876 isl_basic_set_free(bset);
878 isl_int_clear(v);
880 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
881 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
882 set = isl_set_read_from_str(ctx, str);
883 set = isl_set_compute_divs(set);
884 isl_set_free(set);
885 if (!set)
886 return -1;
888 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
889 bset = isl_basic_set_read_from_str(ctx, str);
890 n = isl_basic_set_dim(bset, isl_dim_div);
891 isl_basic_set_free(bset);
892 if (!bset)
893 return -1;
894 if (n != 0)
895 isl_die(ctx, isl_error_unknown,
896 "expecting no existentials", return -1);
898 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
899 set = isl_set_read_from_str(ctx, str);
900 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
901 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
902 empty = isl_set_is_empty(set);
903 isl_set_free(set);
904 if (empty < 0)
905 return -1;
906 if (!empty)
907 isl_die(ctx, isl_error_unknown,
908 "result not as accurate as expected", return -1);
910 return 0;
913 void test_application_case(struct isl_ctx *ctx, const char *name)
915 char *filename;
916 FILE *input;
917 struct isl_basic_set *bset1, *bset2;
918 struct isl_basic_map *bmap;
920 filename = get_filename(ctx, name, "omega");
921 assert(filename);
922 input = fopen(filename, "r");
923 assert(input);
925 bset1 = isl_basic_set_read_from_file(ctx, input);
926 bmap = isl_basic_map_read_from_file(ctx, input);
928 bset1 = isl_basic_set_apply(bset1, bmap);
930 bset2 = isl_basic_set_read_from_file(ctx, input);
932 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
934 isl_basic_set_free(bset1);
935 isl_basic_set_free(bset2);
936 free(filename);
938 fclose(input);
941 void test_application(struct isl_ctx *ctx)
943 test_application_case(ctx, "application");
944 test_application_case(ctx, "application2");
947 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
949 char *filename;
950 FILE *input;
951 struct isl_basic_set *bset1, *bset2;
953 filename = get_filename(ctx, name, "polylib");
954 assert(filename);
955 input = fopen(filename, "r");
956 assert(input);
958 bset1 = isl_basic_set_read_from_file(ctx, input);
959 bset2 = isl_basic_set_read_from_file(ctx, input);
961 bset1 = isl_basic_set_affine_hull(bset1);
963 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
965 isl_basic_set_free(bset1);
966 isl_basic_set_free(bset2);
967 free(filename);
969 fclose(input);
972 int test_affine_hull(struct isl_ctx *ctx)
974 const char *str;
975 isl_set *set;
976 isl_basic_set *bset, *bset2;
977 int n;
978 int subset;
980 test_affine_hull_case(ctx, "affine2");
981 test_affine_hull_case(ctx, "affine");
982 test_affine_hull_case(ctx, "affine3");
984 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
985 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
986 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
987 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
988 set = isl_set_read_from_str(ctx, str);
989 bset = isl_set_affine_hull(set);
990 n = isl_basic_set_dim(bset, isl_dim_div);
991 isl_basic_set_free(bset);
992 if (n != 0)
993 isl_die(ctx, isl_error_unknown, "not expecting any divs",
994 return -1);
996 /* Check that isl_map_affine_hull is not confused by
997 * the reordering of divs in isl_map_align_divs.
999 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1000 "32e0 = b and 32e1 = c); "
1001 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1002 set = isl_set_read_from_str(ctx, str);
1003 bset = isl_set_affine_hull(set);
1004 isl_basic_set_free(bset);
1005 if (!bset)
1006 return -1;
1008 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1009 "32e2 = 31 + 31e0 }";
1010 set = isl_set_read_from_str(ctx, str);
1011 bset = isl_set_affine_hull(set);
1012 str = "{ [a] : exists e : a = 32 e }";
1013 bset2 = isl_basic_set_read_from_str(ctx, str);
1014 subset = isl_basic_set_is_subset(bset, bset2);
1015 isl_basic_set_free(bset);
1016 isl_basic_set_free(bset2);
1017 if (subset < 0)
1018 return -1;
1019 if (!subset)
1020 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1021 return -1);
1023 return 0;
1026 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1028 char *filename;
1029 FILE *input;
1030 struct isl_basic_set *bset1, *bset2;
1031 struct isl_set *set;
1033 filename = get_filename(ctx, name, "polylib");
1034 assert(filename);
1035 input = fopen(filename, "r");
1036 assert(input);
1038 bset1 = isl_basic_set_read_from_file(ctx, input);
1039 bset2 = isl_basic_set_read_from_file(ctx, input);
1041 set = isl_basic_set_union(bset1, bset2);
1042 bset1 = isl_set_convex_hull(set);
1044 bset2 = isl_basic_set_read_from_file(ctx, input);
1046 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1048 isl_basic_set_free(bset1);
1049 isl_basic_set_free(bset2);
1050 free(filename);
1052 fclose(input);
1055 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1057 const char *str1, *str2;
1058 isl_set *set1, *set2;
1059 int orig_convex = ctx->opt->convex;
1060 ctx->opt->convex = convex;
1062 test_convex_hull_case(ctx, "convex0");
1063 test_convex_hull_case(ctx, "convex1");
1064 test_convex_hull_case(ctx, "convex2");
1065 test_convex_hull_case(ctx, "convex3");
1066 test_convex_hull_case(ctx, "convex4");
1067 test_convex_hull_case(ctx, "convex5");
1068 test_convex_hull_case(ctx, "convex6");
1069 test_convex_hull_case(ctx, "convex7");
1070 test_convex_hull_case(ctx, "convex8");
1071 test_convex_hull_case(ctx, "convex9");
1072 test_convex_hull_case(ctx, "convex10");
1073 test_convex_hull_case(ctx, "convex11");
1074 test_convex_hull_case(ctx, "convex12");
1075 test_convex_hull_case(ctx, "convex13");
1076 test_convex_hull_case(ctx, "convex14");
1077 test_convex_hull_case(ctx, "convex15");
1079 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1080 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1081 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1082 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1083 set1 = isl_set_read_from_str(ctx, str1);
1084 set2 = isl_set_read_from_str(ctx, str2);
1085 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1086 assert(isl_set_is_equal(set1, set2));
1087 isl_set_free(set1);
1088 isl_set_free(set2);
1090 ctx->opt->convex = orig_convex;
1093 void test_convex_hull(struct isl_ctx *ctx)
1095 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1096 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1099 void test_gist_case(struct isl_ctx *ctx, const char *name)
1101 char *filename;
1102 FILE *input;
1103 struct isl_basic_set *bset1, *bset2;
1105 filename = get_filename(ctx, name, "polylib");
1106 assert(filename);
1107 input = fopen(filename, "r");
1108 assert(input);
1110 bset1 = isl_basic_set_read_from_file(ctx, input);
1111 bset2 = isl_basic_set_read_from_file(ctx, input);
1113 bset1 = isl_basic_set_gist(bset1, bset2);
1115 bset2 = isl_basic_set_read_from_file(ctx, input);
1117 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1119 isl_basic_set_free(bset1);
1120 isl_basic_set_free(bset2);
1121 free(filename);
1123 fclose(input);
1126 static int test_gist(struct isl_ctx *ctx)
1128 const char *str;
1129 isl_basic_set *bset1, *bset2;
1130 isl_map *map1, *map2;
1132 test_gist_case(ctx, "gist1");
1134 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1135 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1136 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1137 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1138 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1139 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1140 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1141 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1142 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1143 bset1 = isl_basic_set_read_from_str(ctx, str);
1144 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1145 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1146 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1147 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1148 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1149 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1150 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1151 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1152 bset2 = isl_basic_set_read_from_str(ctx, str);
1153 bset1 = isl_basic_set_gist(bset1, bset2);
1154 assert(bset1 && bset1->n_div == 0);
1155 isl_basic_set_free(bset1);
1157 /* Check that the integer divisions of the second disjunct
1158 * do not spread to the first disjunct.
1160 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1161 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1162 "(exists (e0 = [(-1 + t1)/16], "
1163 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1164 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1165 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1166 "o0 <= 4294967295 and t1 <= -1)) }";
1167 map1 = isl_map_read_from_str(ctx, str);
1168 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1169 map2 = isl_map_read_from_str(ctx, str);
1170 map1 = isl_map_gist(map1, map2);
1171 if (!map1)
1172 return -1;
1173 if (map1->n != 1)
1174 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1175 isl_map_free(map1); return -1);
1176 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1177 isl_die(ctx, isl_error_unknown, "expecting single div",
1178 isl_map_free(map1); return -1);
1179 isl_map_free(map1);
1181 return 0;
1184 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1186 isl_set *set, *set2;
1187 int equal;
1188 int one;
1190 set = isl_set_read_from_str(ctx, str);
1191 set = isl_set_coalesce(set);
1192 set2 = isl_set_read_from_str(ctx, str);
1193 equal = isl_set_is_equal(set, set2);
1194 one = set && set->n == 1;
1195 isl_set_free(set);
1196 isl_set_free(set2);
1198 if (equal < 0)
1199 return -1;
1200 if (!equal)
1201 isl_die(ctx, isl_error_unknown,
1202 "coalesced set not equal to input", return -1);
1203 if (check_one && !one)
1204 isl_die(ctx, isl_error_unknown,
1205 "coalesced set should not be a union", return -1);
1207 return 0;
1210 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1212 int r = 0;
1213 int bounded;
1215 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1216 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1218 if (test_coalesce_set(ctx,
1219 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1220 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1221 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1222 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1223 "-10 <= y <= 0}", 1) < 0)
1224 goto error;
1225 if (test_coalesce_set(ctx,
1226 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1227 goto error;
1228 if (test_coalesce_set(ctx,
1229 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1230 goto error;
1232 if (0) {
1233 error:
1234 r = -1;
1237 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1239 return r;
1242 /* Inputs for coalescing tests.
1243 * "str" is a string representation of the input set.
1244 * "single_disjunct" is set if we expect the result to consist of
1245 * a single disjunct.
1247 struct {
1248 int single_disjunct;
1249 const char *str;
1250 } coalesce_tests[] = {
1251 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1252 "y >= x & x >= 2 & 5 >= y }" },
1253 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1254 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1255 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1256 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1257 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1258 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1259 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1260 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1261 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1262 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1263 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1264 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1265 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1266 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1267 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1268 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1269 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1270 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1271 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1272 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1273 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1274 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1275 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1276 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1277 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1278 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1279 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1280 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1281 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1282 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1283 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1284 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1285 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1286 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1287 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1288 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1289 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1290 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1291 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1292 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1293 "[o0, o1, o2, o3, o4, o5, o6]] : "
1294 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1295 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1296 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1297 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1298 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1299 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1300 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1301 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1302 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1303 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1304 "o6 >= i3 + i6 - o3 and M >= 0 and "
1305 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1306 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1307 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1308 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1309 "(o0 = 0 and M >= 2 and N >= 3) or "
1310 "(M = 0 and o0 = 0 and N >= 3) }" },
1311 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1312 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1313 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1314 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1315 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1316 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1317 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1318 "(y = 3 and x = 1) }" },
1319 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1320 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1321 "i1 <= M and i3 <= M and i4 <= M) or "
1322 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1323 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1324 "i4 <= -1 + M) }" },
1325 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1326 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1327 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1328 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1329 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1330 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1331 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1332 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1333 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1334 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1335 { 0, "{ [a, b] : exists e : 2e = a and "
1336 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1337 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1338 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1339 "j >= 1 and j' <= i + j - i' and i >= 1; "
1340 "[1, 1, 1, 1] }" },
1341 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1342 "[i,j] : exists a : j = 3a }" },
1343 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1344 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1345 "a >= 3) or "
1346 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1347 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1348 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1349 "c <= 6 + 8a and a >= 3; "
1350 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1351 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1352 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1353 "[x,0] : 3 <= x <= 4 }" },
1354 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1355 "[x,0] : 4 <= x <= 5 }" },
1356 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1357 "[x,0] : 3 <= x <= 5 }" },
1358 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1359 "[x,0] : 3 <= x <= 4 }" },
1360 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1361 "i1 <= 0; "
1362 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1365 /* Test the functionality of isl_set_coalesce.
1366 * That is, check that the output is always equal to the input
1367 * and in some cases that the result consists of a single disjunct.
1369 static int test_coalesce(struct isl_ctx *ctx)
1371 int i;
1373 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1374 const char *str = coalesce_tests[i].str;
1375 int check_one = coalesce_tests[i].single_disjunct;
1376 if (test_coalesce_set(ctx, str, check_one) < 0)
1377 return -1;
1380 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1381 return -1;
1383 return 0;
1386 void test_closure(struct isl_ctx *ctx)
1388 const char *str;
1389 isl_set *dom;
1390 isl_map *up, *right;
1391 isl_map *map, *map2;
1392 int exact;
1394 /* COCOA example 1 */
1395 map = isl_map_read_from_str(ctx,
1396 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1397 "1 <= i and i < n and 1 <= j and j < n or "
1398 "i2 = i + 1 and j2 = j - 1 and "
1399 "1 <= i and i < n and 2 <= j and j <= n }");
1400 map = isl_map_power(map, &exact);
1401 assert(exact);
1402 isl_map_free(map);
1404 /* COCOA example 1 */
1405 map = isl_map_read_from_str(ctx,
1406 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1407 "1 <= i and i < n and 1 <= j and j < n or "
1408 "i2 = i + 1 and j2 = j - 1 and "
1409 "1 <= i and i < n and 2 <= j and j <= n }");
1410 map = isl_map_transitive_closure(map, &exact);
1411 assert(exact);
1412 map2 = isl_map_read_from_str(ctx,
1413 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1414 "1 <= i and i < n and 1 <= j and j <= n and "
1415 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1416 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1417 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1418 assert(isl_map_is_equal(map, map2));
1419 isl_map_free(map2);
1420 isl_map_free(map);
1422 map = isl_map_read_from_str(ctx,
1423 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1424 " 0 <= y and y <= n }");
1425 map = isl_map_transitive_closure(map, &exact);
1426 map2 = isl_map_read_from_str(ctx,
1427 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1428 " 0 <= y and y <= n }");
1429 assert(isl_map_is_equal(map, map2));
1430 isl_map_free(map2);
1431 isl_map_free(map);
1433 /* COCOA example 2 */
1434 map = isl_map_read_from_str(ctx,
1435 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1436 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1437 "i2 = i + 2 and j2 = j - 2 and "
1438 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1439 map = isl_map_transitive_closure(map, &exact);
1440 assert(exact);
1441 map2 = isl_map_read_from_str(ctx,
1442 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1443 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1444 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1445 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1446 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1447 assert(isl_map_is_equal(map, map2));
1448 isl_map_free(map);
1449 isl_map_free(map2);
1451 /* COCOA Fig.2 left */
1452 map = isl_map_read_from_str(ctx,
1453 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1454 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1455 "j <= n or "
1456 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1457 "j <= 2 i - 3 and j <= n - 2 or "
1458 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1459 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1460 map = isl_map_transitive_closure(map, &exact);
1461 assert(exact);
1462 isl_map_free(map);
1464 /* COCOA Fig.2 right */
1465 map = isl_map_read_from_str(ctx,
1466 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1467 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1468 "j <= n or "
1469 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1470 "j <= 2 i - 4 and j <= n - 3 or "
1471 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1472 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1473 map = isl_map_power(map, &exact);
1474 assert(exact);
1475 isl_map_free(map);
1477 /* COCOA Fig.2 right */
1478 map = isl_map_read_from_str(ctx,
1479 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1480 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1481 "j <= n or "
1482 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1483 "j <= 2 i - 4 and j <= n - 3 or "
1484 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1485 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1486 map = isl_map_transitive_closure(map, &exact);
1487 assert(exact);
1488 map2 = isl_map_read_from_str(ctx,
1489 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1490 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1491 "j <= n and 3 + i + 2 j <= 3 n and "
1492 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1493 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1494 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1495 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1496 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1497 assert(isl_map_is_equal(map, map2));
1498 isl_map_free(map2);
1499 isl_map_free(map);
1501 /* COCOA Fig.1 right */
1502 dom = isl_set_read_from_str(ctx,
1503 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1504 "2 x - 3 y + 3 >= 0 }");
1505 right = isl_map_read_from_str(ctx,
1506 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1507 up = isl_map_read_from_str(ctx,
1508 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1509 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1510 right = isl_map_intersect_range(right, isl_set_copy(dom));
1511 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1512 up = isl_map_intersect_range(up, dom);
1513 map = isl_map_union(up, right);
1514 map = isl_map_transitive_closure(map, &exact);
1515 assert(exact);
1516 map2 = isl_map_read_from_str(ctx,
1517 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1518 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1519 assert(isl_map_is_equal(map, map2));
1520 isl_map_free(map2);
1521 isl_map_free(map);
1523 /* COCOA Theorem 1 counter example */
1524 map = isl_map_read_from_str(ctx,
1525 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1526 "i2 = 1 and j2 = j or "
1527 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1528 map = isl_map_transitive_closure(map, &exact);
1529 assert(exact);
1530 isl_map_free(map);
1532 map = isl_map_read_from_str(ctx,
1533 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1534 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1535 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1536 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1537 map = isl_map_transitive_closure(map, &exact);
1538 assert(exact);
1539 isl_map_free(map);
1541 /* Kelly et al 1996, fig 12 */
1542 map = isl_map_read_from_str(ctx,
1543 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1544 "1 <= i,j,j+1 <= n or "
1545 "j = n and j2 = 1 and i2 = i + 1 and "
1546 "1 <= i,i+1 <= n }");
1547 map = isl_map_transitive_closure(map, &exact);
1548 assert(exact);
1549 map2 = isl_map_read_from_str(ctx,
1550 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1551 "1 <= i <= n and i = i2 or "
1552 "1 <= i < i2 <= n and 1 <= j <= n and "
1553 "1 <= j2 <= n }");
1554 assert(isl_map_is_equal(map, map2));
1555 isl_map_free(map2);
1556 isl_map_free(map);
1558 /* Omega's closure4 */
1559 map = isl_map_read_from_str(ctx,
1560 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1561 "1 <= x,y <= 10 or "
1562 "x2 = x + 1 and y2 = y and "
1563 "1 <= x <= 20 && 5 <= y <= 15 }");
1564 map = isl_map_transitive_closure(map, &exact);
1565 assert(exact);
1566 isl_map_free(map);
1568 map = isl_map_read_from_str(ctx,
1569 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1570 map = isl_map_transitive_closure(map, &exact);
1571 assert(!exact);
1572 map2 = isl_map_read_from_str(ctx,
1573 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1574 assert(isl_map_is_equal(map, map2));
1575 isl_map_free(map);
1576 isl_map_free(map2);
1578 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1579 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1580 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1581 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1582 map = isl_map_read_from_str(ctx, str);
1583 map = isl_map_transitive_closure(map, &exact);
1584 assert(exact);
1585 map2 = isl_map_read_from_str(ctx, str);
1586 assert(isl_map_is_equal(map, map2));
1587 isl_map_free(map);
1588 isl_map_free(map2);
1590 str = "{[0] -> [1]; [2] -> [3]}";
1591 map = isl_map_read_from_str(ctx, str);
1592 map = isl_map_transitive_closure(map, &exact);
1593 assert(exact);
1594 map2 = isl_map_read_from_str(ctx, str);
1595 assert(isl_map_is_equal(map, map2));
1596 isl_map_free(map);
1597 isl_map_free(map2);
1599 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1600 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1601 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1602 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1603 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1604 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1605 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1606 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1607 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1608 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1609 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1610 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1611 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1612 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1613 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1614 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1615 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1616 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1617 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1618 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1619 map = isl_map_read_from_str(ctx, str);
1620 map = isl_map_transitive_closure(map, NULL);
1621 assert(map);
1622 isl_map_free(map);
1625 void test_lex(struct isl_ctx *ctx)
1627 isl_space *dim;
1628 isl_map *map;
1630 dim = isl_space_set_alloc(ctx, 0, 0);
1631 map = isl_map_lex_le(dim);
1632 assert(!isl_map_is_empty(map));
1633 isl_map_free(map);
1636 static int test_lexmin(struct isl_ctx *ctx)
1638 int equal;
1639 const char *str;
1640 isl_basic_map *bmap;
1641 isl_map *map, *map2;
1642 isl_set *set;
1643 isl_set *set2;
1644 isl_pw_multi_aff *pma;
1646 str = "[p0, p1] -> { [] -> [] : "
1647 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1648 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1649 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1650 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1651 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1652 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1653 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1654 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1655 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1656 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1657 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1658 map = isl_map_read_from_str(ctx, str);
1659 map = isl_map_lexmin(map);
1660 isl_map_free(map);
1662 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1663 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1664 set = isl_set_read_from_str(ctx, str);
1665 set = isl_set_lexmax(set);
1666 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1667 set2 = isl_set_read_from_str(ctx, str);
1668 set = isl_set_intersect(set, set2);
1669 assert(!isl_set_is_empty(set));
1670 isl_set_free(set);
1672 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1673 map = isl_map_read_from_str(ctx, str);
1674 map = isl_map_lexmin(map);
1675 str = "{ [x] -> [5] : 6 <= x <= 8; "
1676 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1677 map2 = isl_map_read_from_str(ctx, str);
1678 assert(isl_map_is_equal(map, map2));
1679 isl_map_free(map);
1680 isl_map_free(map2);
1682 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1683 map = isl_map_read_from_str(ctx, str);
1684 map2 = isl_map_copy(map);
1685 map = isl_map_lexmin(map);
1686 assert(isl_map_is_equal(map, map2));
1687 isl_map_free(map);
1688 isl_map_free(map2);
1690 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1691 map = isl_map_read_from_str(ctx, str);
1692 map = isl_map_lexmin(map);
1693 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1694 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1695 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1696 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1697 map2 = isl_map_read_from_str(ctx, str);
1698 assert(isl_map_is_equal(map, map2));
1699 isl_map_free(map);
1700 isl_map_free(map2);
1702 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1703 " 8i' <= i and 8i' >= -7 + i }";
1704 bmap = isl_basic_map_read_from_str(ctx, str);
1705 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1706 map2 = isl_map_from_pw_multi_aff(pma);
1707 map = isl_map_from_basic_map(bmap);
1708 assert(isl_map_is_equal(map, map2));
1709 isl_map_free(map);
1710 isl_map_free(map2);
1712 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1713 map = isl_map_read_from_str(ctx, str);
1714 map = isl_map_lexmin(map);
1715 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1716 map2 = isl_map_read_from_str(ctx, str);
1717 assert(isl_map_is_equal(map, map2));
1718 isl_map_free(map);
1719 isl_map_free(map2);
1721 /* Check that empty pieces are properly combined. */
1722 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1723 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1724 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1725 map = isl_map_read_from_str(ctx, str);
1726 map = isl_map_lexmin(map);
1727 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1728 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1729 "x >= 4 }";
1730 map2 = isl_map_read_from_str(ctx, str);
1731 assert(isl_map_is_equal(map, map2));
1732 isl_map_free(map);
1733 isl_map_free(map2);
1735 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1736 " 8i' <= i and 8i' >= -7 + i }";
1737 set = isl_set_read_from_str(ctx, str);
1738 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1739 set2 = isl_set_from_pw_multi_aff(pma);
1740 equal = isl_set_is_equal(set, set2);
1741 isl_set_free(set);
1742 isl_set_free(set2);
1743 if (equal < 0)
1744 return -1;
1745 if (!equal)
1746 isl_die(ctx, isl_error_unknown,
1747 "unexpected difference between set and "
1748 "piecewise affine expression", return -1);
1750 return 0;
1753 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1754 * result on non-convex inputs.
1756 static int test_min(struct isl_ctx *ctx)
1758 isl_set *set;
1759 isl_aff *aff;
1760 isl_val *val;
1761 int min_ok, max_ok;
1763 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1764 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1765 val = isl_set_min_val(set, aff);
1766 min_ok = isl_val_is_negone(val);
1767 isl_val_free(val);
1768 val = isl_set_max_val(set, aff);
1769 max_ok = isl_val_is_one(val);
1770 isl_val_free(val);
1771 isl_aff_free(aff);
1772 isl_set_free(set);
1774 if (min_ok < 0 || max_ok < 0)
1775 return -1;
1776 if (!min_ok)
1777 isl_die(ctx, isl_error_unknown,
1778 "unexpected minimum", return -1);
1779 if (!max_ok)
1780 isl_die(ctx, isl_error_unknown,
1781 "unexpected maximum", return -1);
1783 return 0;
1786 struct must_may {
1787 isl_map *must;
1788 isl_map *may;
1791 static int collect_must_may(__isl_take isl_map *dep, int must,
1792 void *dep_user, void *user)
1794 struct must_may *mm = (struct must_may *)user;
1796 if (must)
1797 mm->must = isl_map_union(mm->must, dep);
1798 else
1799 mm->may = isl_map_union(mm->may, dep);
1801 return 0;
1804 static int common_space(void *first, void *second)
1806 int depth = *(int *)first;
1807 return 2 * depth;
1810 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1812 isl_map *map2;
1813 int equal;
1815 if (!map)
1816 return -1;
1818 map2 = isl_map_read_from_str(map->ctx, str);
1819 equal = isl_map_is_equal(map, map2);
1820 isl_map_free(map2);
1822 return equal;
1825 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1827 int equal;
1829 equal = map_is_equal(map, str);
1830 if (equal < 0)
1831 return -1;
1832 if (!equal)
1833 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1834 "result not as expected", return -1);
1835 return 0;
1838 void test_dep(struct isl_ctx *ctx)
1840 const char *str;
1841 isl_space *dim;
1842 isl_map *map;
1843 isl_access_info *ai;
1844 isl_flow *flow;
1845 int depth;
1846 struct must_may mm;
1848 depth = 3;
1850 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1851 map = isl_map_read_from_str(ctx, str);
1852 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1854 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1855 map = isl_map_read_from_str(ctx, str);
1856 ai = isl_access_info_add_source(ai, map, 1, &depth);
1858 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1859 map = isl_map_read_from_str(ctx, str);
1860 ai = isl_access_info_add_source(ai, map, 1, &depth);
1862 flow = isl_access_info_compute_flow(ai);
1863 dim = isl_space_alloc(ctx, 0, 3, 3);
1864 mm.must = isl_map_empty(isl_space_copy(dim));
1865 mm.may = isl_map_empty(dim);
1867 isl_flow_foreach(flow, collect_must_may, &mm);
1869 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1870 " [1,10,0] -> [2,5,0] }";
1871 assert(map_is_equal(mm.must, str));
1872 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1873 assert(map_is_equal(mm.may, str));
1875 isl_map_free(mm.must);
1876 isl_map_free(mm.may);
1877 isl_flow_free(flow);
1880 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1881 map = isl_map_read_from_str(ctx, str);
1882 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1884 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1885 map = isl_map_read_from_str(ctx, str);
1886 ai = isl_access_info_add_source(ai, map, 1, &depth);
1888 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1889 map = isl_map_read_from_str(ctx, str);
1890 ai = isl_access_info_add_source(ai, map, 0, &depth);
1892 flow = isl_access_info_compute_flow(ai);
1893 dim = isl_space_alloc(ctx, 0, 3, 3);
1894 mm.must = isl_map_empty(isl_space_copy(dim));
1895 mm.may = isl_map_empty(dim);
1897 isl_flow_foreach(flow, collect_must_may, &mm);
1899 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1900 assert(map_is_equal(mm.must, str));
1901 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1902 assert(map_is_equal(mm.may, str));
1904 isl_map_free(mm.must);
1905 isl_map_free(mm.may);
1906 isl_flow_free(flow);
1909 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1910 map = isl_map_read_from_str(ctx, str);
1911 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1913 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1914 map = isl_map_read_from_str(ctx, str);
1915 ai = isl_access_info_add_source(ai, map, 0, &depth);
1917 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1918 map = isl_map_read_from_str(ctx, str);
1919 ai = isl_access_info_add_source(ai, map, 0, &depth);
1921 flow = isl_access_info_compute_flow(ai);
1922 dim = isl_space_alloc(ctx, 0, 3, 3);
1923 mm.must = isl_map_empty(isl_space_copy(dim));
1924 mm.may = isl_map_empty(dim);
1926 isl_flow_foreach(flow, collect_must_may, &mm);
1928 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1929 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1930 assert(map_is_equal(mm.may, str));
1931 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1932 assert(map_is_equal(mm.must, str));
1934 isl_map_free(mm.must);
1935 isl_map_free(mm.may);
1936 isl_flow_free(flow);
1939 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1940 map = isl_map_read_from_str(ctx, str);
1941 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1943 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1944 map = isl_map_read_from_str(ctx, str);
1945 ai = isl_access_info_add_source(ai, map, 0, &depth);
1947 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1948 map = isl_map_read_from_str(ctx, str);
1949 ai = isl_access_info_add_source(ai, map, 0, &depth);
1951 flow = isl_access_info_compute_flow(ai);
1952 dim = isl_space_alloc(ctx, 0, 3, 3);
1953 mm.must = isl_map_empty(isl_space_copy(dim));
1954 mm.may = isl_map_empty(dim);
1956 isl_flow_foreach(flow, collect_must_may, &mm);
1958 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1959 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1960 assert(map_is_equal(mm.may, str));
1961 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1962 assert(map_is_equal(mm.must, str));
1964 isl_map_free(mm.must);
1965 isl_map_free(mm.may);
1966 isl_flow_free(flow);
1969 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1970 map = isl_map_read_from_str(ctx, str);
1971 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1973 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1974 map = isl_map_read_from_str(ctx, str);
1975 ai = isl_access_info_add_source(ai, map, 0, &depth);
1977 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1978 map = isl_map_read_from_str(ctx, str);
1979 ai = isl_access_info_add_source(ai, map, 0, &depth);
1981 flow = isl_access_info_compute_flow(ai);
1982 dim = isl_space_alloc(ctx, 0, 3, 3);
1983 mm.must = isl_map_empty(isl_space_copy(dim));
1984 mm.may = isl_map_empty(dim);
1986 isl_flow_foreach(flow, collect_must_may, &mm);
1988 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1989 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1990 assert(map_is_equal(mm.may, str));
1991 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1992 assert(map_is_equal(mm.must, str));
1994 isl_map_free(mm.must);
1995 isl_map_free(mm.may);
1996 isl_flow_free(flow);
1999 depth = 5;
2001 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2002 map = isl_map_read_from_str(ctx, str);
2003 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2005 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2006 map = isl_map_read_from_str(ctx, str);
2007 ai = isl_access_info_add_source(ai, map, 1, &depth);
2009 flow = isl_access_info_compute_flow(ai);
2010 dim = isl_space_alloc(ctx, 0, 5, 5);
2011 mm.must = isl_map_empty(isl_space_copy(dim));
2012 mm.may = isl_map_empty(dim);
2014 isl_flow_foreach(flow, collect_must_may, &mm);
2016 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2017 assert(map_is_equal(mm.must, str));
2018 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2019 assert(map_is_equal(mm.may, str));
2021 isl_map_free(mm.must);
2022 isl_map_free(mm.may);
2023 isl_flow_free(flow);
2026 int test_sv(isl_ctx *ctx)
2028 const char *str;
2029 isl_map *map;
2030 isl_union_map *umap;
2031 int sv;
2033 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2034 map = isl_map_read_from_str(ctx, str);
2035 sv = isl_map_is_single_valued(map);
2036 isl_map_free(map);
2037 if (sv < 0)
2038 return -1;
2039 if (!sv)
2040 isl_die(ctx, isl_error_internal,
2041 "map not detected as single valued", return -1);
2043 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2044 map = isl_map_read_from_str(ctx, str);
2045 sv = isl_map_is_single_valued(map);
2046 isl_map_free(map);
2047 if (sv < 0)
2048 return -1;
2049 if (sv)
2050 isl_die(ctx, isl_error_internal,
2051 "map detected as single valued", return -1);
2053 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2054 umap = isl_union_map_read_from_str(ctx, str);
2055 sv = isl_union_map_is_single_valued(umap);
2056 isl_union_map_free(umap);
2057 if (sv < 0)
2058 return -1;
2059 if (!sv)
2060 isl_die(ctx, isl_error_internal,
2061 "map not detected as single valued", return -1);
2063 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2064 umap = isl_union_map_read_from_str(ctx, str);
2065 sv = isl_union_map_is_single_valued(umap);
2066 isl_union_map_free(umap);
2067 if (sv < 0)
2068 return -1;
2069 if (sv)
2070 isl_die(ctx, isl_error_internal,
2071 "map detected as single valued", return -1);
2073 return 0;
2076 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2078 isl_map *map;
2080 map = isl_map_read_from_str(ctx, str);
2081 if (bijective)
2082 assert(isl_map_is_bijective(map));
2083 else
2084 assert(!isl_map_is_bijective(map));
2085 isl_map_free(map);
2088 void test_bijective(struct isl_ctx *ctx)
2090 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2091 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2092 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2093 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2094 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2095 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2096 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2097 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2098 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2099 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2100 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2101 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2102 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2105 static int test_pwqp(struct isl_ctx *ctx)
2107 const char *str;
2108 isl_set *set;
2109 isl_pw_qpolynomial *pwqp1, *pwqp2;
2110 int equal;
2112 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2113 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2115 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2116 isl_dim_in, 1, 1);
2118 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2119 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2121 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2123 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2125 isl_pw_qpolynomial_free(pwqp1);
2127 str = "{ [i] -> i }";
2128 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2129 str = "{ [k] : exists a : k = 2a }";
2130 set = isl_set_read_from_str(ctx, str);
2131 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2132 str = "{ [i] -> i }";
2133 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2135 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2137 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2139 isl_pw_qpolynomial_free(pwqp1);
2141 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2142 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2143 str = "{ [10] }";
2144 set = isl_set_read_from_str(ctx, str);
2145 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2146 str = "{ [i] -> 16 }";
2147 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2149 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2151 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2153 isl_pw_qpolynomial_free(pwqp1);
2155 str = "{ [i] -> ([(i)/2]) }";
2156 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2157 str = "{ [k] : exists a : k = 2a+1 }";
2158 set = isl_set_read_from_str(ctx, str);
2159 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2160 str = "{ [i] -> -1/2 + 1/2 * i }";
2161 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2163 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2165 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2167 isl_pw_qpolynomial_free(pwqp1);
2169 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2170 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2171 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2172 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2174 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2176 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2178 isl_pw_qpolynomial_free(pwqp1);
2180 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2181 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2182 str = "{ [x] -> x }";
2183 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2185 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2187 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2189 isl_pw_qpolynomial_free(pwqp1);
2191 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2192 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2193 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2194 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2195 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2196 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2197 isl_pw_qpolynomial_free(pwqp1);
2199 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2200 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2201 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2202 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2203 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2204 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2205 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2206 isl_pw_qpolynomial_free(pwqp1);
2207 isl_pw_qpolynomial_free(pwqp2);
2208 if (equal < 0)
2209 return -1;
2210 if (!equal)
2211 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2213 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2214 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2215 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2216 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2217 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2218 isl_val_one(ctx));
2219 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2220 isl_pw_qpolynomial_free(pwqp1);
2221 isl_pw_qpolynomial_free(pwqp2);
2222 if (equal < 0)
2223 return -1;
2224 if (!equal)
2225 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2227 return 0;
2230 void test_split_periods(isl_ctx *ctx)
2232 const char *str;
2233 isl_pw_qpolynomial *pwqp;
2235 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2236 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2237 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2238 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2240 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2241 assert(pwqp);
2243 isl_pw_qpolynomial_free(pwqp);
2246 void test_union(isl_ctx *ctx)
2248 const char *str;
2249 isl_union_set *uset1, *uset2;
2250 isl_union_map *umap1, *umap2;
2252 str = "{ [i] : 0 <= i <= 1 }";
2253 uset1 = isl_union_set_read_from_str(ctx, str);
2254 str = "{ [1] -> [0] }";
2255 umap1 = isl_union_map_read_from_str(ctx, str);
2257 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2258 assert(isl_union_map_is_equal(umap1, umap2));
2260 isl_union_map_free(umap1);
2261 isl_union_map_free(umap2);
2263 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2264 umap1 = isl_union_map_read_from_str(ctx, str);
2265 str = "{ A[i]; B[i] }";
2266 uset1 = isl_union_set_read_from_str(ctx, str);
2268 uset2 = isl_union_map_domain(umap1);
2270 assert(isl_union_set_is_equal(uset1, uset2));
2272 isl_union_set_free(uset1);
2273 isl_union_set_free(uset2);
2276 void test_bound(isl_ctx *ctx)
2278 const char *str;
2279 isl_pw_qpolynomial *pwqp;
2280 isl_pw_qpolynomial_fold *pwf;
2282 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2283 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2284 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2285 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2286 isl_pw_qpolynomial_fold_free(pwf);
2288 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2289 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2290 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2291 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2292 isl_pw_qpolynomial_fold_free(pwf);
2295 void test_lift(isl_ctx *ctx)
2297 const char *str;
2298 isl_basic_map *bmap;
2299 isl_basic_set *bset;
2301 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2302 bset = isl_basic_set_read_from_str(ctx, str);
2303 bset = isl_basic_set_lift(bset);
2304 bmap = isl_basic_map_from_range(bset);
2305 bset = isl_basic_map_domain(bmap);
2306 isl_basic_set_free(bset);
2309 struct {
2310 const char *set1;
2311 const char *set2;
2312 int subset;
2313 } subset_tests[] = {
2314 { "{ [112, 0] }",
2315 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2316 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2317 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2318 { "{ [65] }",
2319 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2320 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2321 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2322 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2323 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2324 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2325 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2326 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2327 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2328 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2329 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2330 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2331 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2332 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2333 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2334 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2335 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2336 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2337 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2338 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2339 "4e0 <= -57 + i0 + i1)) or "
2340 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2341 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2342 "4e0 >= -61 + i0 + i1)) or "
2343 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2346 static int test_subset(isl_ctx *ctx)
2348 int i;
2349 isl_set *set1, *set2;
2350 int subset;
2352 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2353 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2354 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2355 subset = isl_set_is_subset(set1, set2);
2356 isl_set_free(set1);
2357 isl_set_free(set2);
2358 if (subset < 0)
2359 return -1;
2360 if (subset != subset_tests[i].subset)
2361 isl_die(ctx, isl_error_unknown,
2362 "incorrect subset result", return -1);
2365 return 0;
2368 struct {
2369 const char *minuend;
2370 const char *subtrahend;
2371 const char *difference;
2372 } subtract_domain_tests[] = {
2373 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2374 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2375 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2378 static int test_subtract(isl_ctx *ctx)
2380 int i;
2381 isl_union_map *umap1, *umap2;
2382 isl_union_set *uset;
2383 int equal;
2385 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2386 umap1 = isl_union_map_read_from_str(ctx,
2387 subtract_domain_tests[i].minuend);
2388 uset = isl_union_set_read_from_str(ctx,
2389 subtract_domain_tests[i].subtrahend);
2390 umap2 = isl_union_map_read_from_str(ctx,
2391 subtract_domain_tests[i].difference);
2392 umap1 = isl_union_map_subtract_domain(umap1, uset);
2393 equal = isl_union_map_is_equal(umap1, umap2);
2394 isl_union_map_free(umap1);
2395 isl_union_map_free(umap2);
2396 if (equal < 0)
2397 return -1;
2398 if (!equal)
2399 isl_die(ctx, isl_error_unknown,
2400 "incorrect subtract domain result", return -1);
2403 return 0;
2406 int test_factorize(isl_ctx *ctx)
2408 const char *str;
2409 isl_basic_set *bset;
2410 isl_factorizer *f;
2412 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2413 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2414 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2415 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2416 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2417 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2418 "3i5 >= -2i0 - i2 + 3i4 }";
2419 bset = isl_basic_set_read_from_str(ctx, str);
2420 f = isl_basic_set_factorizer(bset);
2421 isl_basic_set_free(bset);
2422 isl_factorizer_free(f);
2423 if (!f)
2424 isl_die(ctx, isl_error_unknown,
2425 "failed to construct factorizer", return -1);
2427 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2428 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2429 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2430 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2431 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2432 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2433 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2434 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2435 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2436 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2437 bset = isl_basic_set_read_from_str(ctx, str);
2438 f = isl_basic_set_factorizer(bset);
2439 isl_basic_set_free(bset);
2440 isl_factorizer_free(f);
2441 if (!f)
2442 isl_die(ctx, isl_error_unknown,
2443 "failed to construct factorizer", return -1);
2445 return 0;
2448 static int check_injective(__isl_take isl_map *map, void *user)
2450 int *injective = user;
2452 *injective = isl_map_is_injective(map);
2453 isl_map_free(map);
2455 if (*injective < 0 || !*injective)
2456 return -1;
2458 return 0;
2461 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2462 const char *r, const char *s, int tilable, int parallel)
2464 int i;
2465 isl_union_set *D;
2466 isl_union_map *W, *R, *S;
2467 isl_union_map *empty;
2468 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2469 isl_union_map *validity, *proximity, *coincidence;
2470 isl_union_map *schedule;
2471 isl_union_map *test;
2472 isl_union_set *delta;
2473 isl_union_set *domain;
2474 isl_set *delta_set;
2475 isl_set *slice;
2476 isl_set *origin;
2477 isl_schedule_constraints *sc;
2478 isl_schedule *sched;
2479 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2481 D = isl_union_set_read_from_str(ctx, d);
2482 W = isl_union_map_read_from_str(ctx, w);
2483 R = isl_union_map_read_from_str(ctx, r);
2484 S = isl_union_map_read_from_str(ctx, s);
2486 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2487 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2489 empty = isl_union_map_empty(isl_union_map_get_space(S));
2490 isl_union_map_compute_flow(isl_union_map_copy(R),
2491 isl_union_map_copy(W), empty,
2492 isl_union_map_copy(S),
2493 &dep_raw, NULL, NULL, NULL);
2494 isl_union_map_compute_flow(isl_union_map_copy(W),
2495 isl_union_map_copy(W),
2496 isl_union_map_copy(R),
2497 isl_union_map_copy(S),
2498 &dep_waw, &dep_war, NULL, NULL);
2500 dep = isl_union_map_union(dep_waw, dep_war);
2501 dep = isl_union_map_union(dep, dep_raw);
2502 validity = isl_union_map_copy(dep);
2503 coincidence = isl_union_map_copy(dep);
2504 proximity = isl_union_map_copy(dep);
2506 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2507 sc = isl_schedule_constraints_set_validity(sc, validity);
2508 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2509 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2510 sched = isl_schedule_constraints_compute_schedule(sc);
2511 schedule = isl_schedule_get_map(sched);
2512 isl_schedule_free(sched);
2513 isl_union_map_free(W);
2514 isl_union_map_free(R);
2515 isl_union_map_free(S);
2517 is_injection = 1;
2518 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2520 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2521 is_complete = isl_union_set_is_subset(D, domain);
2522 isl_union_set_free(D);
2523 isl_union_set_free(domain);
2525 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2526 test = isl_union_map_apply_range(test, dep);
2527 test = isl_union_map_apply_range(test, schedule);
2529 delta = isl_union_map_deltas(test);
2530 if (isl_union_set_n_set(delta) == 0) {
2531 is_tilable = 1;
2532 is_parallel = 1;
2533 is_nonneg = 1;
2534 isl_union_set_free(delta);
2535 } else {
2536 delta_set = isl_set_from_union_set(delta);
2538 slice = isl_set_universe(isl_set_get_space(delta_set));
2539 for (i = 0; i < tilable; ++i)
2540 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2541 is_tilable = isl_set_is_subset(delta_set, slice);
2542 isl_set_free(slice);
2544 slice = isl_set_universe(isl_set_get_space(delta_set));
2545 for (i = 0; i < parallel; ++i)
2546 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2547 is_parallel = isl_set_is_subset(delta_set, slice);
2548 isl_set_free(slice);
2550 origin = isl_set_universe(isl_set_get_space(delta_set));
2551 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2552 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2554 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2555 delta_set = isl_set_lexmin(delta_set);
2557 is_nonneg = isl_set_is_equal(delta_set, origin);
2559 isl_set_free(origin);
2560 isl_set_free(delta_set);
2563 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2564 is_injection < 0 || is_complete < 0)
2565 return -1;
2566 if (!is_complete)
2567 isl_die(ctx, isl_error_unknown,
2568 "generated schedule incomplete", return -1);
2569 if (!is_injection)
2570 isl_die(ctx, isl_error_unknown,
2571 "generated schedule not injective on each statement",
2572 return -1);
2573 if (!is_nonneg)
2574 isl_die(ctx, isl_error_unknown,
2575 "negative dependences in generated schedule",
2576 return -1);
2577 if (!is_tilable)
2578 isl_die(ctx, isl_error_unknown,
2579 "generated schedule not as tilable as expected",
2580 return -1);
2581 if (!is_parallel)
2582 isl_die(ctx, isl_error_unknown,
2583 "generated schedule not as parallel as expected",
2584 return -1);
2586 return 0;
2589 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2590 const char *domain, const char *validity, const char *proximity)
2592 isl_union_set *dom;
2593 isl_union_map *dep;
2594 isl_union_map *prox;
2595 isl_schedule_constraints *sc;
2596 isl_schedule *schedule;
2597 isl_union_map *sched;
2599 dom = isl_union_set_read_from_str(ctx, domain);
2600 dep = isl_union_map_read_from_str(ctx, validity);
2601 prox = isl_union_map_read_from_str(ctx, proximity);
2602 sc = isl_schedule_constraints_on_domain(dom);
2603 sc = isl_schedule_constraints_set_validity(sc, dep);
2604 sc = isl_schedule_constraints_set_proximity(sc, prox);
2605 schedule = isl_schedule_constraints_compute_schedule(sc);
2606 sched = isl_schedule_get_map(schedule);
2607 isl_schedule_free(schedule);
2609 return sched;
2612 /* Check that a schedule can be constructed on the given domain
2613 * with the given validity and proximity constraints.
2615 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2616 const char *validity, const char *proximity)
2618 isl_union_map *sched;
2620 sched = compute_schedule(ctx, domain, validity, proximity);
2621 if (!sched)
2622 return -1;
2624 isl_union_map_free(sched);
2625 return 0;
2628 int test_special_schedule(isl_ctx *ctx, const char *domain,
2629 const char *validity, const char *proximity, const char *expected_sched)
2631 isl_union_map *sched1, *sched2;
2632 int equal;
2634 sched1 = compute_schedule(ctx, domain, validity, proximity);
2635 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2637 equal = isl_union_map_is_equal(sched1, sched2);
2638 isl_union_map_free(sched1);
2639 isl_union_map_free(sched2);
2641 if (equal < 0)
2642 return -1;
2643 if (!equal)
2644 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2645 return -1);
2647 return 0;
2650 /* Check that the schedule map is properly padded, even after being
2651 * reconstructed from the band forest.
2653 static int test_padded_schedule(isl_ctx *ctx)
2655 const char *str;
2656 isl_union_set *D;
2657 isl_union_map *validity, *proximity;
2658 isl_schedule_constraints *sc;
2659 isl_schedule *sched;
2660 isl_union_map *map1, *map2;
2661 isl_band_list *list;
2662 int equal;
2664 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2665 D = isl_union_set_read_from_str(ctx, str);
2666 validity = isl_union_map_empty(isl_union_set_get_space(D));
2667 proximity = isl_union_map_copy(validity);
2668 sc = isl_schedule_constraints_on_domain(D);
2669 sc = isl_schedule_constraints_set_validity(sc, validity);
2670 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2671 sched = isl_schedule_constraints_compute_schedule(sc);
2672 map1 = isl_schedule_get_map(sched);
2673 list = isl_schedule_get_band_forest(sched);
2674 isl_band_list_free(list);
2675 map2 = isl_schedule_get_map(sched);
2676 isl_schedule_free(sched);
2677 equal = isl_union_map_is_equal(map1, map2);
2678 isl_union_map_free(map1);
2679 isl_union_map_free(map2);
2681 if (equal < 0)
2682 return -1;
2683 if (!equal)
2684 isl_die(ctx, isl_error_unknown,
2685 "reconstructed schedule map not the same as original",
2686 return -1);
2688 return 0;
2691 /* Input for testing of schedule construction based on
2692 * conditional constraints.
2694 * domain is the iteration domain
2695 * flow are the flow dependences, which determine the validity and
2696 * proximity constraints
2697 * condition are the conditions on the conditional validity constraints
2698 * conditional_validity are the conditional validity constraints
2699 * outer_band_n is the expected number of members in the outer band
2701 struct {
2702 const char *domain;
2703 const char *flow;
2704 const char *condition;
2705 const char *conditional_validity;
2706 int outer_band_n;
2707 } live_range_tests[] = {
2708 /* Contrived example that illustrates that we need to keep
2709 * track of tagged condition dependences and
2710 * tagged conditional validity dependences
2711 * in isl_sched_edge separately.
2712 * In particular, the conditional validity constraints on A
2713 * cannot be satisfied,
2714 * but they can be ignored because there are no corresponding
2715 * condition constraints. However, we do have an additional
2716 * conditional validity constraint that maps to the same
2717 * dependence relation
2718 * as the condition constraint on B. If we did not make a distinction
2719 * between tagged condition and tagged conditional validity
2720 * dependences, then we
2721 * could end up treating this shared dependence as an condition
2722 * constraint on A, forcing a localization of the conditions,
2723 * which is impossible.
2725 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2726 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2727 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2728 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2729 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2730 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2733 /* TACO 2013 Fig. 7 */
2734 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2735 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2736 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2737 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2738 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2739 "0 <= i < n and 0 <= j < n - 1 }",
2740 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2741 "0 <= i < n and 0 <= j < j' < n;"
2742 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2743 "0 <= i < i' < n and 0 <= j,j' < n;"
2744 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2745 "0 <= i,j,j' < n and j < j' }",
2748 /* TACO 2013 Fig. 7, without tags */
2749 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2750 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2751 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2752 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2753 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2754 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2755 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2756 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2759 /* TACO 2013 Fig. 12 */
2760 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2761 "S3[i,3] : 0 <= i <= 1 }",
2762 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2763 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2764 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2765 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2766 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2767 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2768 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2769 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2770 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2771 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2772 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2777 /* Test schedule construction based on conditional constraints.
2778 * In particular, check the number of members in the outer band
2779 * as an indication of whether tiling is possible or not.
2781 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2783 int i;
2784 isl_union_set *domain;
2785 isl_union_map *condition;
2786 isl_union_map *flow;
2787 isl_union_map *validity;
2788 isl_schedule_constraints *sc;
2789 isl_schedule *schedule;
2790 isl_band_list *list;
2791 isl_band *band;
2792 int n_member;
2794 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2795 domain = isl_union_set_read_from_str(ctx,
2796 live_range_tests[i].domain);
2797 flow = isl_union_map_read_from_str(ctx,
2798 live_range_tests[i].flow);
2799 condition = isl_union_map_read_from_str(ctx,
2800 live_range_tests[i].condition);
2801 validity = isl_union_map_read_from_str(ctx,
2802 live_range_tests[i].conditional_validity);
2803 sc = isl_schedule_constraints_on_domain(domain);
2804 sc = isl_schedule_constraints_set_validity(sc,
2805 isl_union_map_copy(flow));
2806 sc = isl_schedule_constraints_set_proximity(sc, flow);
2807 sc = isl_schedule_constraints_set_conditional_validity(sc,
2808 condition, validity);
2809 schedule = isl_schedule_constraints_compute_schedule(sc);
2810 list = isl_schedule_get_band_forest(schedule);
2811 band = isl_band_list_get_band(list, 0);
2812 n_member = isl_band_n_member(band);
2813 isl_band_free(band);
2814 isl_band_list_free(list);
2815 isl_schedule_free(schedule);
2817 if (!schedule)
2818 return -1;
2819 if (n_member != live_range_tests[i].outer_band_n)
2820 isl_die(ctx, isl_error_unknown,
2821 "unexpected number of members in outer band",
2822 return -1);
2824 return 0;
2827 int test_schedule(isl_ctx *ctx)
2829 const char *D, *W, *R, *V, *P, *S;
2831 /* Handle resulting schedule with zero bands. */
2832 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2833 return -1;
2835 /* Jacobi */
2836 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2837 W = "{ S1[t,i] -> a[t,i] }";
2838 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2839 "S1[t,i] -> a[t-1,i+1] }";
2840 S = "{ S1[t,i] -> [t,i] }";
2841 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2842 return -1;
2844 /* Fig. 5 of CC2008 */
2845 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2846 "j <= -1 + N }";
2847 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2848 "j >= 2 and j <= -1 + N }";
2849 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2850 "j >= 2 and j <= -1 + N; "
2851 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2852 "j >= 2 and j <= -1 + N }";
2853 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2854 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2855 return -1;
2857 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2858 W = "{ S1[i] -> a[i] }";
2859 R = "{ S2[i] -> a[i+1] }";
2860 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2861 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2862 return -1;
2864 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2865 W = "{ S1[i] -> a[i] }";
2866 R = "{ S2[i] -> a[9-i] }";
2867 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2868 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2869 return -1;
2871 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2872 W = "{ S1[i] -> a[i] }";
2873 R = "[N] -> { S2[i] -> a[N-1-i] }";
2874 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2875 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2876 return -1;
2878 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2879 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2880 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2881 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2882 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2883 return -1;
2885 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2886 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2887 R = "{ S2[i,j] -> a[i-1,j] }";
2888 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2889 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2890 return -1;
2892 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2893 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2894 R = "{ S2[i,j] -> a[i,j-1] }";
2895 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2896 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2897 return -1;
2899 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2900 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2901 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2902 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2903 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2904 "S_0[] -> [0, 0, 0] }";
2905 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2906 return -1;
2907 ctx->opt->schedule_parametric = 0;
2908 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2909 return -1;
2910 ctx->opt->schedule_parametric = 1;
2912 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2913 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2914 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2915 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2916 "S4[i] -> a[i,N] }";
2917 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2918 "S4[i] -> [4,i,0] }";
2919 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2920 return -1;
2922 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2923 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2924 "j <= N }";
2925 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2926 "j <= N; "
2927 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2928 "j <= N }";
2929 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2930 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2931 return -1;
2933 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2934 " S_2[t] : t >= 0 and t <= -1 + N; "
2935 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2936 "i <= -1 + N }";
2937 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2938 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2939 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2940 "i >= 0 and i <= -1 + N }";
2941 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2942 "i >= 0 and i <= -1 + N; "
2943 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2944 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2945 " S_0[t] -> [0, t, 0] }";
2947 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2948 return -1;
2949 ctx->opt->schedule_parametric = 0;
2950 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2951 return -1;
2952 ctx->opt->schedule_parametric = 1;
2954 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2955 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2956 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2957 return -1;
2959 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2960 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2961 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2962 "j >= 0 and j <= -1 + N; "
2963 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2964 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2965 "j >= 0 and j <= -1 + N; "
2966 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2967 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2968 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2969 return -1;
2971 D = "{ S_0[i] : i >= 0 }";
2972 W = "{ S_0[i] -> a[i] : i >= 0 }";
2973 R = "{ S_0[i] -> a[0] : i >= 0 }";
2974 S = "{ S_0[i] -> [0, i, 0] }";
2975 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2976 return -1;
2978 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2979 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2980 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2981 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2982 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2983 return -1;
2985 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2986 "k <= -1 + n and k >= 0 }";
2987 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2988 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2989 "k <= -1 + n and k >= 0; "
2990 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2991 "k <= -1 + n and k >= 0; "
2992 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2993 "k <= -1 + n and k >= 0 }";
2994 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2995 ctx->opt->schedule_outer_coincidence = 1;
2996 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2997 return -1;
2998 ctx->opt->schedule_outer_coincidence = 0;
3000 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3001 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3002 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3003 "Stmt_for_body24[i0, i1, 1, 0]:"
3004 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3005 "Stmt_for_body7[i0, i1, i2]:"
3006 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3007 "i2 <= 7 }";
3009 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3010 "Stmt_for_body24[1, i1, i2, i3]:"
3011 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3012 "i2 >= 1;"
3013 "Stmt_for_body24[0, i1, i2, i3] -> "
3014 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3015 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3016 "i3 >= 0;"
3017 "Stmt_for_body24[0, i1, i2, i3] ->"
3018 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3019 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3020 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3021 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3022 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3023 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3024 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3025 "i1 <= 6 and i1 >= 0;"
3026 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3027 "Stmt_for_body7[i0, i1, i2] -> "
3028 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3029 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3030 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3031 "Stmt_for_body7[i0, i1, i2] -> "
3032 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3033 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3034 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3035 P = V;
3036 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3037 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3038 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3040 if (test_special_schedule(ctx, D, V, P, S) < 0)
3041 return -1;
3043 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3044 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3045 "j >= 1 and j <= 7;"
3046 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3047 "j >= 1 and j <= 8 }";
3048 P = "{ }";
3049 S = "{ S_0[i, j] -> [i + j, j] }";
3050 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3051 if (test_special_schedule(ctx, D, V, P, S) < 0)
3052 return -1;
3053 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3055 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3056 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3057 "j >= 0 and j <= -1 + i }";
3058 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3059 "i <= -1 + N and j >= 0;"
3060 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3061 "i <= -2 + N }";
3062 P = "{ }";
3063 S = "{ S_0[i, j] -> [i, j] }";
3064 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3065 if (test_special_schedule(ctx, D, V, P, S) < 0)
3066 return -1;
3067 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3069 /* Test both algorithms on a case with only proximity dependences. */
3070 D = "{ S[i,j] : 0 <= i <= 10 }";
3071 V = "{ }";
3072 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3073 S = "{ S[i, j] -> [j, i] }";
3074 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3075 if (test_special_schedule(ctx, D, V, P, S) < 0)
3076 return -1;
3077 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3078 if (test_special_schedule(ctx, D, V, P, S) < 0)
3079 return -1;
3081 D = "{ A[a]; B[] }";
3082 V = "{}";
3083 P = "{ A[a] -> B[] }";
3084 if (test_has_schedule(ctx, D, V, P) < 0)
3085 return -1;
3087 if (test_padded_schedule(ctx) < 0)
3088 return -1;
3090 /* Check that check for progress is not confused by rational
3091 * solution.
3093 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3094 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3095 "i0 <= -2 + N; "
3096 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3097 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3098 P = "{}";
3099 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3100 if (test_has_schedule(ctx, D, V, P) < 0)
3101 return -1;
3102 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3104 if (test_conditional_schedule_constraints(ctx) < 0)
3105 return -1;
3107 return 0;
3110 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3112 isl_union_map *umap;
3113 int test;
3115 umap = isl_union_map_read_from_str(ctx, str);
3116 test = isl_union_map_plain_is_injective(umap);
3117 isl_union_map_free(umap);
3118 if (test < 0)
3119 return -1;
3120 if (test == injective)
3121 return 0;
3122 if (injective)
3123 isl_die(ctx, isl_error_unknown,
3124 "map not detected as injective", return -1);
3125 else
3126 isl_die(ctx, isl_error_unknown,
3127 "map detected as injective", return -1);
3130 int test_injective(isl_ctx *ctx)
3132 const char *str;
3134 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3135 return -1;
3136 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3137 return -1;
3138 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3139 return -1;
3140 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3141 return -1;
3142 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3143 return -1;
3144 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3145 return -1;
3146 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3147 return -1;
3148 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3149 return -1;
3151 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3152 if (test_plain_injective(ctx, str, 1))
3153 return -1;
3154 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3155 if (test_plain_injective(ctx, str, 0))
3156 return -1;
3158 return 0;
3161 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3163 isl_aff *aff2;
3164 int equal;
3166 if (!aff)
3167 return -1;
3169 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3170 equal = isl_aff_plain_is_equal(aff, aff2);
3171 isl_aff_free(aff2);
3173 return equal;
3176 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3178 int equal;
3180 equal = aff_plain_is_equal(aff, str);
3181 if (equal < 0)
3182 return -1;
3183 if (!equal)
3184 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3185 "result not as expected", return -1);
3186 return 0;
3189 int test_aff(isl_ctx *ctx)
3191 const char *str;
3192 isl_set *set;
3193 isl_space *space;
3194 isl_local_space *ls;
3195 isl_aff *aff;
3196 int zero, equal;
3198 space = isl_space_set_alloc(ctx, 0, 1);
3199 ls = isl_local_space_from_space(space);
3200 aff = isl_aff_zero_on_domain(ls);
3202 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3203 aff = isl_aff_scale_down_ui(aff, 3);
3204 aff = isl_aff_floor(aff);
3205 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3206 aff = isl_aff_scale_down_ui(aff, 2);
3207 aff = isl_aff_floor(aff);
3208 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3210 str = "{ [10] }";
3211 set = isl_set_read_from_str(ctx, str);
3212 aff = isl_aff_gist(aff, set);
3214 aff = isl_aff_add_constant_si(aff, -16);
3215 zero = isl_aff_plain_is_zero(aff);
3216 isl_aff_free(aff);
3218 if (zero < 0)
3219 return -1;
3220 if (!zero)
3221 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3223 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3224 aff = isl_aff_scale_down_ui(aff, 64);
3225 aff = isl_aff_floor(aff);
3226 equal = aff_check_plain_equal(aff, "{ [-1] }");
3227 isl_aff_free(aff);
3228 if (equal < 0)
3229 return -1;
3231 return 0;
3234 int test_dim_max(isl_ctx *ctx)
3236 int equal;
3237 const char *str;
3238 isl_set *set1, *set2;
3239 isl_set *set;
3240 isl_map *map;
3241 isl_pw_aff *pwaff;
3243 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3244 set = isl_set_read_from_str(ctx, str);
3245 pwaff = isl_set_dim_max(set, 0);
3246 set1 = isl_set_from_pw_aff(pwaff);
3247 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3248 set2 = isl_set_read_from_str(ctx, str);
3249 equal = isl_set_is_equal(set1, set2);
3250 isl_set_free(set1);
3251 isl_set_free(set2);
3252 if (equal < 0)
3253 return -1;
3254 if (!equal)
3255 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3257 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3258 set = isl_set_read_from_str(ctx, str);
3259 pwaff = isl_set_dim_max(set, 0);
3260 set1 = isl_set_from_pw_aff(pwaff);
3261 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3262 set2 = isl_set_read_from_str(ctx, str);
3263 equal = isl_set_is_equal(set1, set2);
3264 isl_set_free(set1);
3265 isl_set_free(set2);
3266 if (equal < 0)
3267 return -1;
3268 if (!equal)
3269 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3271 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3272 set = isl_set_read_from_str(ctx, str);
3273 pwaff = isl_set_dim_max(set, 0);
3274 set1 = isl_set_from_pw_aff(pwaff);
3275 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3276 set2 = isl_set_read_from_str(ctx, str);
3277 equal = isl_set_is_equal(set1, set2);
3278 isl_set_free(set1);
3279 isl_set_free(set2);
3280 if (equal < 0)
3281 return -1;
3282 if (!equal)
3283 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3285 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3286 "0 <= i < N and 0 <= j < M }";
3287 map = isl_map_read_from_str(ctx, str);
3288 set = isl_map_range(map);
3290 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3291 set1 = isl_set_from_pw_aff(pwaff);
3292 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3293 set2 = isl_set_read_from_str(ctx, str);
3294 equal = isl_set_is_equal(set1, set2);
3295 isl_set_free(set1);
3296 isl_set_free(set2);
3298 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3299 set1 = isl_set_from_pw_aff(pwaff);
3300 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3301 set2 = isl_set_read_from_str(ctx, str);
3302 if (equal >= 0 && equal)
3303 equal = isl_set_is_equal(set1, set2);
3304 isl_set_free(set1);
3305 isl_set_free(set2);
3307 isl_set_free(set);
3309 if (equal < 0)
3310 return -1;
3311 if (!equal)
3312 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3314 /* Check that solutions are properly merged. */
3315 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3316 "c <= -1 + n - 4a - 2b and c >= -2b and "
3317 "4a >= -4 + n and c >= 0 }";
3318 set = isl_set_read_from_str(ctx, str);
3319 pwaff = isl_set_dim_min(set, 2);
3320 set1 = isl_set_from_pw_aff(pwaff);
3321 str = "[n] -> { [(0)] : n >= 1 }";
3322 set2 = isl_set_read_from_str(ctx, str);
3323 equal = isl_set_is_equal(set1, set2);
3324 isl_set_free(set1);
3325 isl_set_free(set2);
3327 if (equal < 0)
3328 return -1;
3329 if (!equal)
3330 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3332 return 0;
3335 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3337 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3338 const char *str)
3340 isl_ctx *ctx;
3341 isl_pw_multi_aff *pma2;
3342 int equal;
3344 if (!pma)
3345 return -1;
3347 ctx = isl_pw_multi_aff_get_ctx(pma);
3348 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3349 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3350 isl_pw_multi_aff_free(pma2);
3352 return equal;
3355 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3356 * represented by "str".
3358 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3359 const char *str)
3361 int equal;
3363 equal = pw_multi_aff_plain_is_equal(pma, str);
3364 if (equal < 0)
3365 return -1;
3366 if (!equal)
3367 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3368 "result not as expected", return -1);
3369 return 0;
3372 /* Basic test for isl_pw_multi_aff_product.
3374 * Check that multiple pieces are properly handled.
3376 static int test_product_pma(isl_ctx *ctx)
3378 int equal;
3379 const char *str;
3380 isl_pw_multi_aff *pma1, *pma2;
3382 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3383 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3384 str = "{ C[] -> D[] }";
3385 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3386 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3387 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3388 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3389 equal = pw_multi_aff_check_plain_equal(pma1, str);
3390 isl_pw_multi_aff_free(pma1);
3391 if (equal < 0)
3392 return -1;
3394 return 0;
3397 int test_product(isl_ctx *ctx)
3399 const char *str;
3400 isl_set *set;
3401 isl_union_set *uset1, *uset2;
3402 int ok;
3404 str = "{ A[i] }";
3405 set = isl_set_read_from_str(ctx, str);
3406 set = isl_set_product(set, isl_set_copy(set));
3407 ok = isl_set_is_wrapping(set);
3408 isl_set_free(set);
3409 if (ok < 0)
3410 return -1;
3411 if (!ok)
3412 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3414 str = "{ [] }";
3415 uset1 = isl_union_set_read_from_str(ctx, str);
3416 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3417 str = "{ [[] -> []] }";
3418 uset2 = isl_union_set_read_from_str(ctx, str);
3419 ok = isl_union_set_is_equal(uset1, uset2);
3420 isl_union_set_free(uset1);
3421 isl_union_set_free(uset2);
3422 if (ok < 0)
3423 return -1;
3424 if (!ok)
3425 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3427 if (test_product_pma(ctx) < 0)
3428 return -1;
3430 return 0;
3433 /* Check that two sets are not considered disjoint just because
3434 * they have a different set of (named) parameters.
3436 static int test_disjoint(isl_ctx *ctx)
3438 const char *str;
3439 isl_set *set, *set2;
3440 int disjoint;
3442 str = "[n] -> { [[]->[]] }";
3443 set = isl_set_read_from_str(ctx, str);
3444 str = "{ [[]->[]] }";
3445 set2 = isl_set_read_from_str(ctx, str);
3446 disjoint = isl_set_is_disjoint(set, set2);
3447 isl_set_free(set);
3448 isl_set_free(set2);
3449 if (disjoint < 0)
3450 return -1;
3451 if (disjoint)
3452 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3454 return 0;
3457 int test_equal(isl_ctx *ctx)
3459 const char *str;
3460 isl_set *set, *set2;
3461 int equal;
3463 str = "{ S_6[i] }";
3464 set = isl_set_read_from_str(ctx, str);
3465 str = "{ S_7[i] }";
3466 set2 = isl_set_read_from_str(ctx, str);
3467 equal = isl_set_is_equal(set, set2);
3468 isl_set_free(set);
3469 isl_set_free(set2);
3470 if (equal < 0)
3471 return -1;
3472 if (equal)
3473 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3475 return 0;
3478 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3479 enum isl_dim_type type, unsigned pos, int fixed)
3481 int test;
3483 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3484 isl_map_free(map);
3485 if (test < 0)
3486 return -1;
3487 if (test == fixed)
3488 return 0;
3489 if (fixed)
3490 isl_die(ctx, isl_error_unknown,
3491 "map not detected as fixed", return -1);
3492 else
3493 isl_die(ctx, isl_error_unknown,
3494 "map detected as fixed", return -1);
3497 int test_fixed(isl_ctx *ctx)
3499 const char *str;
3500 isl_map *map;
3502 str = "{ [i] -> [i] }";
3503 map = isl_map_read_from_str(ctx, str);
3504 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3505 return -1;
3506 str = "{ [i] -> [1] }";
3507 map = isl_map_read_from_str(ctx, str);
3508 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3509 return -1;
3510 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3511 map = isl_map_read_from_str(ctx, str);
3512 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3513 return -1;
3514 map = isl_map_read_from_str(ctx, str);
3515 map = isl_map_neg(map);
3516 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3517 return -1;
3519 return 0;
3522 int test_vertices(isl_ctx *ctx)
3524 const char *str;
3525 isl_basic_set *bset;
3526 isl_vertices *vertices;
3528 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3529 bset = isl_basic_set_read_from_str(ctx, str);
3530 vertices = isl_basic_set_compute_vertices(bset);
3531 isl_basic_set_free(bset);
3532 isl_vertices_free(vertices);
3533 if (!vertices)
3534 return -1;
3536 str = "{ A[t, i] : t = 14 and i = 1 }";
3537 bset = isl_basic_set_read_from_str(ctx, str);
3538 vertices = isl_basic_set_compute_vertices(bset);
3539 isl_basic_set_free(bset);
3540 isl_vertices_free(vertices);
3541 if (!vertices)
3542 return -1;
3544 return 0;
3547 int test_union_pw(isl_ctx *ctx)
3549 int equal;
3550 const char *str;
3551 isl_union_set *uset;
3552 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3554 str = "{ [x] -> x^2 }";
3555 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3556 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3557 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3558 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3559 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3560 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3561 isl_union_pw_qpolynomial_free(upwqp1);
3562 isl_union_pw_qpolynomial_free(upwqp2);
3563 if (equal < 0)
3564 return -1;
3565 if (!equal)
3566 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3568 return 0;
3571 int test_output(isl_ctx *ctx)
3573 char *s;
3574 const char *str;
3575 isl_pw_aff *pa;
3576 isl_printer *p;
3577 int equal;
3579 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3580 pa = isl_pw_aff_read_from_str(ctx, str);
3582 p = isl_printer_to_str(ctx);
3583 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3584 p = isl_printer_print_pw_aff(p, pa);
3585 s = isl_printer_get_str(p);
3586 isl_printer_free(p);
3587 isl_pw_aff_free(pa);
3588 if (!s)
3589 equal = -1;
3590 else
3591 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3592 free(s);
3593 if (equal < 0)
3594 return -1;
3595 if (!equal)
3596 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3598 return 0;
3601 int test_sample(isl_ctx *ctx)
3603 const char *str;
3604 isl_basic_set *bset1, *bset2;
3605 int empty, subset;
3607 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3608 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3609 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3610 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3611 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3612 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3613 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3614 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3615 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3616 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3617 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3618 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3619 "d - 1073741821e and "
3620 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3621 "3j >= 1 - a + b + 2e and "
3622 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3623 "3i <= 4 - a + 4b - e and "
3624 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3625 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3626 "c <= -1 + a and 3i >= -2 - a + 3e and "
3627 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3628 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3629 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3630 "1073741823e >= 1 + 1073741823b - d and "
3631 "3i >= 1073741823b + c - 1073741823e - f and "
3632 "3i >= 1 + 2b + e + 3g }";
3633 bset1 = isl_basic_set_read_from_str(ctx, str);
3634 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3635 empty = isl_basic_set_is_empty(bset2);
3636 subset = isl_basic_set_is_subset(bset2, bset1);
3637 isl_basic_set_free(bset1);
3638 isl_basic_set_free(bset2);
3639 if (empty < 0 || subset < 0)
3640 return -1;
3641 if (empty)
3642 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3643 if (!subset)
3644 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3646 return 0;
3649 int test_fixed_power(isl_ctx *ctx)
3651 const char *str;
3652 isl_map *map;
3653 isl_int exp;
3654 int equal;
3656 isl_int_init(exp);
3657 str = "{ [i] -> [i + 1] }";
3658 map = isl_map_read_from_str(ctx, str);
3659 isl_int_set_si(exp, 23);
3660 map = isl_map_fixed_power(map, exp);
3661 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3662 isl_int_clear(exp);
3663 isl_map_free(map);
3664 if (equal < 0)
3665 return -1;
3667 return 0;
3670 int test_slice(isl_ctx *ctx)
3672 const char *str;
3673 isl_map *map;
3674 int equal;
3676 str = "{ [i] -> [j] }";
3677 map = isl_map_read_from_str(ctx, str);
3678 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3679 equal = map_check_equal(map, "{ [i] -> [i] }");
3680 isl_map_free(map);
3681 if (equal < 0)
3682 return -1;
3684 str = "{ [i] -> [j] }";
3685 map = isl_map_read_from_str(ctx, str);
3686 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3687 equal = map_check_equal(map, "{ [i] -> [j] }");
3688 isl_map_free(map);
3689 if (equal < 0)
3690 return -1;
3692 str = "{ [i] -> [j] }";
3693 map = isl_map_read_from_str(ctx, str);
3694 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3695 equal = map_check_equal(map, "{ [i] -> [-i] }");
3696 isl_map_free(map);
3697 if (equal < 0)
3698 return -1;
3700 str = "{ [i] -> [j] }";
3701 map = isl_map_read_from_str(ctx, str);
3702 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3703 equal = map_check_equal(map, "{ [0] -> [j] }");
3704 isl_map_free(map);
3705 if (equal < 0)
3706 return -1;
3708 str = "{ [i] -> [j] }";
3709 map = isl_map_read_from_str(ctx, str);
3710 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3711 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3712 isl_map_free(map);
3713 if (equal < 0)
3714 return -1;
3716 str = "{ [i] -> [j] }";
3717 map = isl_map_read_from_str(ctx, str);
3718 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3719 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3720 isl_map_free(map);
3721 if (equal < 0)
3722 return -1;
3724 return 0;
3727 int test_eliminate(isl_ctx *ctx)
3729 const char *str;
3730 isl_map *map;
3731 int equal;
3733 str = "{ [i] -> [j] : i = 2j }";
3734 map = isl_map_read_from_str(ctx, str);
3735 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3736 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3737 isl_map_free(map);
3738 if (equal < 0)
3739 return -1;
3741 return 0;
3744 /* Check that isl_set_dim_residue_class detects that the values of j
3745 * in the set below are all odd and that it does not detect any spurious
3746 * strides.
3748 static int test_residue_class(isl_ctx *ctx)
3750 const char *str;
3751 isl_set *set;
3752 isl_int m, r;
3753 int res;
3755 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3756 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3757 set = isl_set_read_from_str(ctx, str);
3758 isl_int_init(m);
3759 isl_int_init(r);
3760 res = isl_set_dim_residue_class(set, 1, &m, &r);
3761 if (res >= 0 &&
3762 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3763 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3764 res = -1);
3765 isl_int_clear(r);
3766 isl_int_clear(m);
3767 isl_set_free(set);
3769 return res;
3772 int test_align_parameters(isl_ctx *ctx)
3774 const char *str;
3775 isl_space *space;
3776 isl_multi_aff *ma1, *ma2;
3777 int equal;
3779 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3780 ma1 = isl_multi_aff_read_from_str(ctx, str);
3782 space = isl_space_params_alloc(ctx, 1);
3783 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3784 ma1 = isl_multi_aff_align_params(ma1, space);
3786 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3787 ma2 = isl_multi_aff_read_from_str(ctx, str);
3789 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3791 isl_multi_aff_free(ma1);
3792 isl_multi_aff_free(ma2);
3794 if (equal < 0)
3795 return -1;
3796 if (!equal)
3797 isl_die(ctx, isl_error_unknown,
3798 "result not as expected", return -1);
3800 return 0;
3803 static int test_list(isl_ctx *ctx)
3805 isl_id *a, *b, *c, *d, *id;
3806 isl_id_list *list;
3807 int ok;
3809 a = isl_id_alloc(ctx, "a", NULL);
3810 b = isl_id_alloc(ctx, "b", NULL);
3811 c = isl_id_alloc(ctx, "c", NULL);
3812 d = isl_id_alloc(ctx, "d", NULL);
3814 list = isl_id_list_alloc(ctx, 4);
3815 list = isl_id_list_add(list, a);
3816 list = isl_id_list_add(list, b);
3817 list = isl_id_list_add(list, c);
3818 list = isl_id_list_add(list, d);
3819 list = isl_id_list_drop(list, 1, 1);
3821 if (isl_id_list_n_id(list) != 3) {
3822 isl_id_list_free(list);
3823 isl_die(ctx, isl_error_unknown,
3824 "unexpected number of elements in list", return -1);
3827 id = isl_id_list_get_id(list, 0);
3828 ok = id == a;
3829 isl_id_free(id);
3830 id = isl_id_list_get_id(list, 1);
3831 ok = ok && id == c;
3832 isl_id_free(id);
3833 id = isl_id_list_get_id(list, 2);
3834 ok = ok && id == d;
3835 isl_id_free(id);
3837 isl_id_list_free(list);
3839 if (!ok)
3840 isl_die(ctx, isl_error_unknown,
3841 "unexpected elements in list", return -1);
3843 return 0;
3846 const char *set_conversion_tests[] = {
3847 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3848 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3849 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3850 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3853 /* Check that converting from isl_set to isl_pw_multi_aff and back
3854 * to isl_set produces the original isl_set.
3856 static int test_set_conversion(isl_ctx *ctx)
3858 int i;
3859 const char *str;
3860 isl_set *set1, *set2;
3861 isl_pw_multi_aff *pma;
3862 int equal;
3864 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3865 str = set_conversion_tests[i];
3866 set1 = isl_set_read_from_str(ctx, str);
3867 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3868 set2 = isl_set_from_pw_multi_aff(pma);
3869 equal = isl_set_is_equal(set1, set2);
3870 isl_set_free(set1);
3871 isl_set_free(set2);
3873 if (equal < 0)
3874 return -1;
3875 if (!equal)
3876 isl_die(ctx, isl_error_unknown, "bad conversion",
3877 return -1);
3880 return 0;
3883 /* Check that converting from isl_map to isl_pw_multi_aff and back
3884 * to isl_map produces the original isl_map.
3886 static int test_map_conversion(isl_ctx *ctx)
3888 const char *str;
3889 isl_map *map1, *map2;
3890 isl_pw_multi_aff *pma;
3891 int equal;
3893 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3894 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3895 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3896 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3897 "9e <= -2 - 2a) }";
3898 map1 = isl_map_read_from_str(ctx, str);
3899 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3900 map2 = isl_map_from_pw_multi_aff(pma);
3901 equal = isl_map_is_equal(map1, map2);
3902 isl_map_free(map1);
3903 isl_map_free(map2);
3905 if (equal < 0)
3906 return -1;
3907 if (!equal)
3908 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3910 return 0;
3913 static int test_conversion(isl_ctx *ctx)
3915 if (test_set_conversion(ctx) < 0)
3916 return -1;
3917 if (test_map_conversion(ctx) < 0)
3918 return -1;
3919 return 0;
3922 /* Check that isl_basic_map_curry does not modify input.
3924 static int test_curry(isl_ctx *ctx)
3926 const char *str;
3927 isl_basic_map *bmap1, *bmap2;
3928 int equal;
3930 str = "{ [A[] -> B[]] -> C[] }";
3931 bmap1 = isl_basic_map_read_from_str(ctx, str);
3932 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3933 equal = isl_basic_map_is_equal(bmap1, bmap2);
3934 isl_basic_map_free(bmap1);
3935 isl_basic_map_free(bmap2);
3937 if (equal < 0)
3938 return -1;
3939 if (equal)
3940 isl_die(ctx, isl_error_unknown,
3941 "curried map should not be equal to original",
3942 return -1);
3944 return 0;
3947 struct {
3948 const char *set;
3949 const char *ma;
3950 const char *res;
3951 } preimage_tests[] = {
3952 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3953 "{ A[j,i] -> B[i,j] }",
3954 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3955 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3956 "{ A[a,b] -> B[a/2,b/6] }",
3957 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3958 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3959 "{ A[a,b] -> B[a/2,b/6] }",
3960 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3961 "exists i,j : a = 2 i and b = 6 j }" },
3962 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3963 "[n] -> { : 0 <= n <= 100 }" },
3964 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3965 "{ A[a] -> B[2a] }",
3966 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3967 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3968 "{ A[a] -> B[([a/2])] }",
3969 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3970 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3971 "{ A[a] -> B[a,a,a/3] }",
3972 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3973 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3974 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3977 static int test_preimage_basic_set(isl_ctx *ctx)
3979 int i;
3980 isl_basic_set *bset1, *bset2;
3981 isl_multi_aff *ma;
3982 int equal;
3984 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3985 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3986 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3987 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3988 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3989 equal = isl_basic_set_is_equal(bset1, bset2);
3990 isl_basic_set_free(bset1);
3991 isl_basic_set_free(bset2);
3992 if (equal < 0)
3993 return -1;
3994 if (!equal)
3995 isl_die(ctx, isl_error_unknown, "bad preimage",
3996 return -1);
3999 return 0;
4002 struct {
4003 const char *map;
4004 const char *ma;
4005 const char *res;
4006 } preimage_domain_tests[] = {
4007 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4008 "{ A[j,i] -> B[i,j] }",
4009 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4010 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4011 "{ A[i] -> B[i + 1] }",
4012 "{ A[i] -> C[i + 1] }" },
4013 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4014 "{ A[i] -> B[i + 1] }",
4015 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4016 { "{ B[i] -> C[([i/2])] }",
4017 "{ A[i] -> B[2i] }",
4018 "{ A[i] -> C[i] }" },
4019 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4020 "{ A[i] -> B[([i/5]), ([i/7])] }",
4021 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4022 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4023 "[N] -> { A[] -> B[([N/5])] }",
4024 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4025 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4026 "{ A[i] -> B[2i] }",
4027 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4028 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4029 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4030 "{ A[i] -> B[2i] }",
4031 "{ A[i] -> C[2i] }" },
4034 static int test_preimage_union_map(isl_ctx *ctx)
4036 int i;
4037 isl_union_map *umap1, *umap2;
4038 isl_multi_aff *ma;
4039 int equal;
4041 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4042 umap1 = isl_union_map_read_from_str(ctx,
4043 preimage_domain_tests[i].map);
4044 ma = isl_multi_aff_read_from_str(ctx,
4045 preimage_domain_tests[i].ma);
4046 umap2 = isl_union_map_read_from_str(ctx,
4047 preimage_domain_tests[i].res);
4048 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4049 equal = isl_union_map_is_equal(umap1, umap2);
4050 isl_union_map_free(umap1);
4051 isl_union_map_free(umap2);
4052 if (equal < 0)
4053 return -1;
4054 if (!equal)
4055 isl_die(ctx, isl_error_unknown, "bad preimage",
4056 return -1);
4059 return 0;
4062 static int test_preimage(isl_ctx *ctx)
4064 if (test_preimage_basic_set(ctx) < 0)
4065 return -1;
4066 if (test_preimage_union_map(ctx) < 0)
4067 return -1;
4069 return 0;
4072 struct {
4073 const char *ma1;
4074 const char *ma;
4075 const char *res;
4076 } pullback_tests[] = {
4077 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4078 "{ A[a,b] -> C[b + 2a] }" },
4079 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4080 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4081 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4082 "{ A[a] -> C[(a)/6] }" },
4083 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4084 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4085 "{ A[a] -> C[(2a)/3] }" },
4086 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4087 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4088 "{ A[i,j] -> C[i + j, i + j] }"},
4089 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4090 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4091 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4094 static int test_pullback(isl_ctx *ctx)
4096 int i;
4097 isl_multi_aff *ma1, *ma2;
4098 isl_multi_aff *ma;
4099 int equal;
4101 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4102 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4103 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4104 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4105 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4106 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4107 isl_multi_aff_free(ma1);
4108 isl_multi_aff_free(ma2);
4109 if (equal < 0)
4110 return -1;
4111 if (!equal)
4112 isl_die(ctx, isl_error_unknown, "bad pullback",
4113 return -1);
4116 return 0;
4119 /* Check that negation is printed correctly.
4121 static int test_ast(isl_ctx *ctx)
4123 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4124 char *str;
4125 int ok;
4127 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4128 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4129 expr = isl_ast_expr_add(expr1, expr2);
4130 expr = isl_ast_expr_neg(expr);
4131 str = isl_ast_expr_to_str(expr);
4132 ok = str ? !strcmp(str, "-(A + B)") : -1;
4133 free(str);
4134 isl_ast_expr_free(expr);
4136 if (ok < 0)
4137 return -1;
4138 if (!ok)
4139 isl_die(ctx, isl_error_unknown,
4140 "isl_ast_expr printed incorrectly", return -1);
4142 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4143 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4144 expr = isl_ast_expr_add(expr1, expr2);
4145 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4146 expr = isl_ast_expr_sub(expr3, expr);
4147 str = isl_ast_expr_to_str(expr);
4148 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4149 free(str);
4150 isl_ast_expr_free(expr);
4152 if (ok < 0)
4153 return -1;
4154 if (!ok)
4155 isl_die(ctx, isl_error_unknown,
4156 "isl_ast_expr printed incorrectly", return -1);
4158 return 0;
4161 /* Internal data structure for before_for and after_for callbacks.
4163 * depth is the current depth
4164 * before is the number of times before_for has been called
4165 * after is the number of times after_for has been called
4167 struct isl_test_codegen_data {
4168 int depth;
4169 int before;
4170 int after;
4173 /* This function is called before each for loop in the AST generated
4174 * from test_ast_gen1.
4176 * Increment the number of calls and the depth.
4177 * Check that the space returned by isl_ast_build_get_schedule_space
4178 * matches the target space of the schedule returned by
4179 * isl_ast_build_get_schedule.
4180 * Return an isl_id that is checked by the corresponding call
4181 * to after_for.
4183 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4184 void *user)
4186 struct isl_test_codegen_data *data = user;
4187 isl_ctx *ctx;
4188 isl_space *space;
4189 isl_union_map *schedule;
4190 isl_union_set *uset;
4191 isl_set *set;
4192 int empty;
4193 char name[] = "d0";
4195 ctx = isl_ast_build_get_ctx(build);
4197 if (data->before >= 3)
4198 isl_die(ctx, isl_error_unknown,
4199 "unexpected number of for nodes", return NULL);
4200 if (data->depth >= 2)
4201 isl_die(ctx, isl_error_unknown,
4202 "unexpected depth", return NULL);
4204 snprintf(name, sizeof(name), "d%d", data->depth);
4205 data->before++;
4206 data->depth++;
4208 schedule = isl_ast_build_get_schedule(build);
4209 uset = isl_union_map_range(schedule);
4210 if (!uset)
4211 return NULL;
4212 if (isl_union_set_n_set(uset) != 1) {
4213 isl_union_set_free(uset);
4214 isl_die(ctx, isl_error_unknown,
4215 "expecting single range space", return NULL);
4218 space = isl_ast_build_get_schedule_space(build);
4219 set = isl_union_set_extract_set(uset, space);
4220 isl_union_set_free(uset);
4221 empty = isl_set_is_empty(set);
4222 isl_set_free(set);
4224 if (empty < 0)
4225 return NULL;
4226 if (empty)
4227 isl_die(ctx, isl_error_unknown,
4228 "spaces don't match", return NULL);
4230 return isl_id_alloc(ctx, name, NULL);
4233 /* This function is called after each for loop in the AST generated
4234 * from test_ast_gen1.
4236 * Increment the number of calls and decrement the depth.
4237 * Check that the annotation attached to the node matches
4238 * the isl_id returned by the corresponding call to before_for.
4240 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4241 __isl_keep isl_ast_build *build, void *user)
4243 struct isl_test_codegen_data *data = user;
4244 isl_id *id;
4245 const char *name;
4246 int valid;
4248 data->after++;
4249 data->depth--;
4251 if (data->after > data->before)
4252 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4253 "mismatch in number of for nodes",
4254 return isl_ast_node_free(node));
4256 id = isl_ast_node_get_annotation(node);
4257 if (!id)
4258 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4259 "missing annotation", return isl_ast_node_free(node));
4261 name = isl_id_get_name(id);
4262 valid = name && atoi(name + 1) == data->depth;
4263 isl_id_free(id);
4265 if (!valid)
4266 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4267 "wrong annotation", return isl_ast_node_free(node));
4269 return node;
4272 /* Check that the before_each_for and after_each_for callbacks
4273 * are called for each for loop in the generated code,
4274 * that they are called in the right order and that the isl_id
4275 * returned from the before_each_for callback is attached to
4276 * the isl_ast_node passed to the corresponding after_each_for call.
4278 static int test_ast_gen1(isl_ctx *ctx)
4280 const char *str;
4281 isl_set *set;
4282 isl_union_map *schedule;
4283 isl_ast_build *build;
4284 isl_ast_node *tree;
4285 struct isl_test_codegen_data data;
4287 str = "[N] -> { : N >= 10 }";
4288 set = isl_set_read_from_str(ctx, str);
4289 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4290 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4291 schedule = isl_union_map_read_from_str(ctx, str);
4293 data.before = 0;
4294 data.after = 0;
4295 data.depth = 0;
4296 build = isl_ast_build_from_context(set);
4297 build = isl_ast_build_set_before_each_for(build,
4298 &before_for, &data);
4299 build = isl_ast_build_set_after_each_for(build,
4300 &after_for, &data);
4301 tree = isl_ast_build_ast_from_schedule(build, schedule);
4302 isl_ast_build_free(build);
4303 if (!tree)
4304 return -1;
4306 isl_ast_node_free(tree);
4308 if (data.before != 3 || data.after != 3)
4309 isl_die(ctx, isl_error_unknown,
4310 "unexpected number of for nodes", return -1);
4312 return 0;
4315 /* Check that the AST generator handles domains that are integrally disjoint
4316 * but not ratinoally disjoint.
4318 static int test_ast_gen2(isl_ctx *ctx)
4320 const char *str;
4321 isl_set *set;
4322 isl_union_map *schedule;
4323 isl_union_map *options;
4324 isl_ast_build *build;
4325 isl_ast_node *tree;
4327 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4328 schedule = isl_union_map_read_from_str(ctx, str);
4329 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4330 build = isl_ast_build_from_context(set);
4332 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4333 options = isl_union_map_read_from_str(ctx, str);
4334 build = isl_ast_build_set_options(build, options);
4335 tree = isl_ast_build_ast_from_schedule(build, schedule);
4336 isl_ast_build_free(build);
4337 if (!tree)
4338 return -1;
4339 isl_ast_node_free(tree);
4341 return 0;
4344 /* Increment *user on each call.
4346 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4347 __isl_keep isl_ast_build *build, void *user)
4349 int *n = user;
4351 (*n)++;
4353 return node;
4356 /* Test that unrolling tries to minimize the number of instances.
4357 * In particular, for the schedule given below, make sure it generates
4358 * 3 nodes (rather than 101).
4360 static int test_ast_gen3(isl_ctx *ctx)
4362 const char *str;
4363 isl_set *set;
4364 isl_union_map *schedule;
4365 isl_union_map *options;
4366 isl_ast_build *build;
4367 isl_ast_node *tree;
4368 int n_domain = 0;
4370 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4371 schedule = isl_union_map_read_from_str(ctx, str);
4372 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4374 str = "{ [i] -> unroll[0] }";
4375 options = isl_union_map_read_from_str(ctx, str);
4377 build = isl_ast_build_from_context(set);
4378 build = isl_ast_build_set_options(build, options);
4379 build = isl_ast_build_set_at_each_domain(build,
4380 &count_domains, &n_domain);
4381 tree = isl_ast_build_ast_from_schedule(build, schedule);
4382 isl_ast_build_free(build);
4383 if (!tree)
4384 return -1;
4386 isl_ast_node_free(tree);
4388 if (n_domain != 3)
4389 isl_die(ctx, isl_error_unknown,
4390 "unexpected number of for nodes", return -1);
4392 return 0;
4395 /* Check that if the ast_build_exploit_nested_bounds options is set,
4396 * we do not get an outer if node in the generated AST,
4397 * while we do get such an outer if node if the options is not set.
4399 static int test_ast_gen4(isl_ctx *ctx)
4401 const char *str;
4402 isl_set *set;
4403 isl_union_map *schedule;
4404 isl_ast_build *build;
4405 isl_ast_node *tree;
4406 enum isl_ast_node_type type;
4407 int enb;
4409 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4410 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4412 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4414 schedule = isl_union_map_read_from_str(ctx, str);
4415 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4416 build = isl_ast_build_from_context(set);
4417 tree = isl_ast_build_ast_from_schedule(build, schedule);
4418 isl_ast_build_free(build);
4419 if (!tree)
4420 return -1;
4422 type = isl_ast_node_get_type(tree);
4423 isl_ast_node_free(tree);
4425 if (type == isl_ast_node_if)
4426 isl_die(ctx, isl_error_unknown,
4427 "not expecting if node", return -1);
4429 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4431 schedule = isl_union_map_read_from_str(ctx, str);
4432 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4433 build = isl_ast_build_from_context(set);
4434 tree = isl_ast_build_ast_from_schedule(build, schedule);
4435 isl_ast_build_free(build);
4436 if (!tree)
4437 return -1;
4439 type = isl_ast_node_get_type(tree);
4440 isl_ast_node_free(tree);
4442 if (type != isl_ast_node_if)
4443 isl_die(ctx, isl_error_unknown,
4444 "expecting if node", return -1);
4446 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4448 return 0;
4451 /* This function is called for each leaf in the AST generated
4452 * from test_ast_gen5.
4454 * We finalize the AST generation by extending the outer schedule
4455 * with a zero-dimensional schedule. If this results in any for loops,
4456 * then this means that we did not pass along enough information
4457 * about the outer schedule to the inner AST generation.
4459 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4460 void *user)
4462 isl_union_map *schedule, *extra;
4463 isl_ast_node *tree;
4465 schedule = isl_ast_build_get_schedule(build);
4466 extra = isl_union_map_copy(schedule);
4467 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4468 schedule = isl_union_map_range_product(schedule, extra);
4469 tree = isl_ast_build_ast_from_schedule(build, schedule);
4470 isl_ast_build_free(build);
4472 if (!tree)
4473 return NULL;
4475 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4476 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4477 "code should not contain any for loop",
4478 return isl_ast_node_free(tree));
4480 return tree;
4483 /* Check that we do not lose any information when going back and
4484 * forth between internal and external schedule.
4486 * In particular, we create an AST where we unroll the only
4487 * non-constant dimension in the schedule. We therefore do
4488 * not expect any for loops in the AST. However, older versions
4489 * of isl would not pass along enough information about the outer
4490 * schedule when performing an inner code generation from a create_leaf
4491 * callback, resulting in the inner code generation producing a for loop.
4493 static int test_ast_gen5(isl_ctx *ctx)
4495 const char *str;
4496 isl_set *set;
4497 isl_union_map *schedule, *options;
4498 isl_ast_build *build;
4499 isl_ast_node *tree;
4501 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4502 schedule = isl_union_map_read_from_str(ctx, str);
4504 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4505 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4506 options = isl_union_map_read_from_str(ctx, str);
4508 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4509 build = isl_ast_build_from_context(set);
4510 build = isl_ast_build_set_options(build, options);
4511 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4512 tree = isl_ast_build_ast_from_schedule(build, schedule);
4513 isl_ast_build_free(build);
4514 isl_ast_node_free(tree);
4515 if (!tree)
4516 return -1;
4518 return 0;
4521 static int test_ast_gen(isl_ctx *ctx)
4523 if (test_ast_gen1(ctx) < 0)
4524 return -1;
4525 if (test_ast_gen2(ctx) < 0)
4526 return -1;
4527 if (test_ast_gen3(ctx) < 0)
4528 return -1;
4529 if (test_ast_gen4(ctx) < 0)
4530 return -1;
4531 if (test_ast_gen5(ctx) < 0)
4532 return -1;
4533 return 0;
4536 /* Check if dropping output dimensions from an isl_pw_multi_aff
4537 * works properly.
4539 static int test_pw_multi_aff(isl_ctx *ctx)
4541 const char *str;
4542 isl_pw_multi_aff *pma1, *pma2;
4543 int equal;
4545 str = "{ [i,j] -> [i+j, 4i-j] }";
4546 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4547 str = "{ [i,j] -> [4i-j] }";
4548 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4550 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4552 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4554 isl_pw_multi_aff_free(pma1);
4555 isl_pw_multi_aff_free(pma2);
4556 if (equal < 0)
4557 return -1;
4558 if (!equal)
4559 isl_die(ctx, isl_error_unknown,
4560 "expressions not equal", return -1);
4562 return 0;
4565 /* Check that we can properly parse multi piecewise affine expressions
4566 * where the piecewise affine expressions have different domains.
4568 static int test_multi_pw_aff(isl_ctx *ctx)
4570 const char *str;
4571 isl_set *dom, *dom2;
4572 isl_multi_pw_aff *mpa1, *mpa2;
4573 isl_pw_aff *pa;
4574 int equal;
4575 int equal_domain;
4577 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4578 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4579 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4580 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4581 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4582 str = "{ [i] -> [(i : i > 0), 2i] }";
4583 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4585 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4587 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4588 dom = isl_pw_aff_domain(pa);
4589 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4590 dom2 = isl_pw_aff_domain(pa);
4591 equal_domain = isl_set_is_equal(dom, dom2);
4593 isl_set_free(dom);
4594 isl_set_free(dom2);
4595 isl_multi_pw_aff_free(mpa1);
4596 isl_multi_pw_aff_free(mpa2);
4598 if (equal < 0)
4599 return -1;
4600 if (!equal)
4601 isl_die(ctx, isl_error_unknown,
4602 "expressions not equal", return -1);
4604 if (equal_domain < 0)
4605 return -1;
4606 if (equal_domain)
4607 isl_die(ctx, isl_error_unknown,
4608 "domains unexpectedly equal", return -1);
4610 return 0;
4613 /* This is a regression test for a bug where isl_basic_map_simplify
4614 * would end up in an infinite loop. In particular, we construct
4615 * an empty basic set that is not obviously empty.
4616 * isl_basic_set_is_empty marks the basic set as empty.
4617 * After projecting out i3, the variable can be dropped completely,
4618 * but isl_basic_map_simplify refrains from doing so if the basic set
4619 * is empty and would end up in an infinite loop if it didn't test
4620 * explicitly for empty basic maps in the outer loop.
4622 static int test_simplify(isl_ctx *ctx)
4624 const char *str;
4625 isl_basic_set *bset;
4626 int empty;
4628 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4629 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4630 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4631 "i3 >= i2 }";
4632 bset = isl_basic_set_read_from_str(ctx, str);
4633 empty = isl_basic_set_is_empty(bset);
4634 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4635 isl_basic_set_free(bset);
4636 if (!bset)
4637 return -1;
4638 if (!empty)
4639 isl_die(ctx, isl_error_unknown,
4640 "basic set should be empty", return -1);
4642 return 0;
4645 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4646 * with gbr context would fail to disable the use of the shifted tableau
4647 * when transferring equalities for the input to the context, resulting
4648 * in invalid sample values.
4650 static int test_partial_lexmin(isl_ctx *ctx)
4652 const char *str;
4653 isl_basic_set *bset;
4654 isl_basic_map *bmap;
4655 isl_map *map;
4657 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4658 bmap = isl_basic_map_read_from_str(ctx, str);
4659 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4660 bset = isl_basic_set_read_from_str(ctx, str);
4661 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4662 isl_map_free(map);
4664 if (!map)
4665 return -1;
4667 return 0;
4670 /* Check that the variable compression performed on the existentially
4671 * quantified variables inside isl_basic_set_compute_divs is not confused
4672 * by the implicit equalities among the parameters.
4674 static int test_compute_divs(isl_ctx *ctx)
4676 const char *str;
4677 isl_basic_set *bset;
4678 isl_set *set;
4680 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4681 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4682 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4683 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4684 bset = isl_basic_set_read_from_str(ctx, str);
4685 set = isl_basic_set_compute_divs(bset);
4686 isl_set_free(set);
4687 if (!set)
4688 return -1;
4690 return 0;
4693 struct {
4694 const char *name;
4695 int (*fn)(isl_ctx *ctx);
4696 } tests [] = {
4697 { "val", &test_val },
4698 { "compute divs", &test_compute_divs },
4699 { "partial lexmin", &test_partial_lexmin },
4700 { "simplify", &test_simplify },
4701 { "curry", &test_curry },
4702 { "piecewise multi affine expressions", &test_pw_multi_aff },
4703 { "multi piecewise affine expressions", &test_multi_pw_aff },
4704 { "conversion", &test_conversion },
4705 { "list", &test_list },
4706 { "align parameters", &test_align_parameters },
4707 { "preimage", &test_preimage },
4708 { "pullback", &test_pullback },
4709 { "AST", &test_ast },
4710 { "AST generation", &test_ast_gen },
4711 { "eliminate", &test_eliminate },
4712 { "residue class", &test_residue_class },
4713 { "div", &test_div },
4714 { "slice", &test_slice },
4715 { "fixed power", &test_fixed_power },
4716 { "sample", &test_sample },
4717 { "output", &test_output },
4718 { "vertices", &test_vertices },
4719 { "fixed", &test_fixed },
4720 { "equal", &test_equal },
4721 { "disjoint", &test_disjoint },
4722 { "product", &test_product },
4723 { "dim_max", &test_dim_max },
4724 { "affine", &test_aff },
4725 { "injective", &test_injective },
4726 { "schedule", &test_schedule },
4727 { "union_pw", &test_union_pw },
4728 { "parse", &test_parse },
4729 { "single-valued", &test_sv },
4730 { "affine hull", &test_affine_hull },
4731 { "coalesce", &test_coalesce },
4732 { "factorize", &test_factorize },
4733 { "subset", &test_subset },
4734 { "subtract", &test_subtract },
4735 { "lexmin", &test_lexmin },
4736 { "min", &test_min },
4737 { "gist", &test_gist },
4738 { "piecewise quasi-polynomials", &test_pwqp },
4741 int main()
4743 int i;
4744 struct isl_ctx *ctx;
4746 srcdir = getenv("srcdir");
4747 assert(srcdir);
4749 ctx = isl_ctx_alloc();
4750 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4751 printf("%s\n", tests[i].name);
4752 if (tests[i].fn(ctx) < 0)
4753 goto error;
4755 test_lift(ctx);
4756 test_bound(ctx);
4757 test_union(ctx);
4758 test_split_periods(ctx);
4759 test_lex(ctx);
4760 test_bijective(ctx);
4761 test_dep(ctx);
4762 test_read(ctx);
4763 test_bounded(ctx);
4764 test_construction(ctx);
4765 test_dim(ctx);
4766 test_application(ctx);
4767 test_convex_hull(ctx);
4768 test_closure(ctx);
4769 isl_ctx_free(ctx);
4770 return 0;
4771 error:
4772 isl_ctx_free(ctx);
4773 return -1;