+int test_fixed_power(isl_ctx *ctx)
+{
+ const char *str;
+ isl_map *map;
+ isl_int exp;
+ int equal;
+
+ isl_int_init(exp);
+ str = "{ [i] -> [i + 1] }";
+ map = isl_map_read_from_str(ctx, str);
+ isl_int_set_si(exp, 23);
+ map = isl_map_fixed_power(map, exp);
+ equal = map_check_equal(map, "{ [i] -> [i + 23] }");
+ isl_int_clear(exp);
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ return 0;
+}
+
+int test_slice(isl_ctx *ctx)
+{
+ const char *str;
+ isl_map *map;
+ int equal;
+
+ str = "{ [i] -> [j] }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
+ equal = map_check_equal(map, "{ [i] -> [i] }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ str = "{ [i] -> [j] }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
+ equal = map_check_equal(map, "{ [i] -> [j] }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ str = "{ [i] -> [j] }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
+ equal = map_check_equal(map, "{ [i] -> [-i] }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ str = "{ [i] -> [j] }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
+ equal = map_check_equal(map, "{ [0] -> [j] }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ str = "{ [i] -> [j] }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
+ equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ str = "{ [i] -> [j] }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
+ equal = map_check_equal(map, "{ [i] -> [j] : false }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ return 0;
+}
+
+int test_eliminate(isl_ctx *ctx)
+{
+ const char *str;
+ isl_map *map;
+ int equal;
+
+ str = "{ [i] -> [j] : i = 2j }";
+ map = isl_map_read_from_str(ctx, str);
+ map = isl_map_eliminate(map, isl_dim_out, 0, 1);
+ equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
+ isl_map_free(map);
+ if (equal < 0)
+ return -1;
+
+ return 0;
+}
+
+/* Check that isl_set_dim_residue_class detects that the values of j
+ * in the set below are all odd and that it does not detect any spurious
+ * strides.
+ */
+static int test_residue_class(isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_int m, r;
+ int res;
+
+ str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
+ "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
+ set = isl_set_read_from_str(ctx, str);
+ isl_int_init(m);
+ isl_int_init(r);
+ res = isl_set_dim_residue_class(set, 1, &m, &r);
+ if (res >= 0 &&
+ (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
+ isl_die(ctx, isl_error_unknown, "incorrect residue class",
+ res = -1);
+ isl_int_clear(r);
+ isl_int_clear(m);
+ isl_set_free(set);
+
+ return res;
+}
+
+int test_align_parameters(isl_ctx *ctx)
+{
+ const char *str;
+ isl_space *space;
+ isl_multi_aff *ma1, *ma2;
+ int equal;
+
+ str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
+ ma1 = isl_multi_aff_read_from_str(ctx, str);
+
+ space = isl_space_params_alloc(ctx, 1);
+ space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
+ ma1 = isl_multi_aff_align_params(ma1, space);
+
+ str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
+ ma2 = isl_multi_aff_read_from_str(ctx, str);
+
+ equal = isl_multi_aff_plain_is_equal(ma1, ma2);
+
+ isl_multi_aff_free(ma1);
+ isl_multi_aff_free(ma2);
+
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown,
+ "result not as expected", return -1);
+
+ return 0;
+}
+
+static int test_list(isl_ctx *ctx)
+{
+ isl_id *a, *b, *c, *d, *id;
+ isl_id_list *list;
+ int ok;
+
+ a = isl_id_alloc(ctx, "a", NULL);
+ b = isl_id_alloc(ctx, "b", NULL);
+ c = isl_id_alloc(ctx, "c", NULL);
+ d = isl_id_alloc(ctx, "d", NULL);
+
+ list = isl_id_list_alloc(ctx, 4);
+ list = isl_id_list_add(list, a);
+ list = isl_id_list_add(list, b);
+ list = isl_id_list_add(list, c);
+ list = isl_id_list_add(list, d);
+ list = isl_id_list_drop(list, 1, 1);
+
+ if (isl_id_list_n_id(list) != 3) {
+ isl_id_list_free(list);
+ isl_die(ctx, isl_error_unknown,
+ "unexpected number of elements in list", return -1);
+ }
+
+ id = isl_id_list_get_id(list, 0);
+ ok = id == a;
+ isl_id_free(id);
+ id = isl_id_list_get_id(list, 1);
+ ok = ok && id == c;
+ isl_id_free(id);
+ id = isl_id_list_get_id(list, 2);
+ ok = ok && id == d;
+ isl_id_free(id);
+
+ isl_id_list_free(list);
+
+ if (!ok)
+ isl_die(ctx, isl_error_unknown,
+ "unexpected elements in list", return -1);
+
+ return 0;
+}
+
+const char *set_conversion_tests[] = {
+ "[N] -> { [i] : N - 1 <= 2 i <= N }",
+ "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
+ "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
+ "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
+};
+
+/* Check that converting from isl_set to isl_pw_multi_aff and back
+ * to isl_set produces the original isl_set.
+ */
+static int test_set_conversion(isl_ctx *ctx)
+{
+ int i;
+ const char *str;
+ isl_set *set1, *set2;
+ isl_pw_multi_aff *pma;
+ int equal;
+
+ for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
+ str = set_conversion_tests[i];
+ set1 = isl_set_read_from_str(ctx, str);
+ pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
+ set2 = isl_set_from_pw_multi_aff(pma);
+ equal = isl_set_is_equal(set1, set2);
+ isl_set_free(set1);
+ isl_set_free(set2);
+
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown, "bad conversion",
+ return -1);
+ }
+
+ return 0;
+}
+
+/* Check that converting from isl_map to isl_pw_multi_aff and back
+ * to isl_map produces the original isl_map.
+ */
+static int test_map_conversion(isl_ctx *ctx)
+{
+ const char *str;
+ isl_map *map1, *map2;
+ isl_pw_multi_aff *pma;
+ int equal;
+
+ str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
+ "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
+ "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
+ "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
+ "9e <= -2 - 2a) }";
+ map1 = isl_map_read_from_str(ctx, str);
+ pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
+ map2 = isl_map_from_pw_multi_aff(pma);
+ equal = isl_map_is_equal(map1, map2);
+ isl_map_free(map1);
+ isl_map_free(map2);
+
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
+
+ return 0;
+}
+
+static int test_conversion(isl_ctx *ctx)
+{
+ if (test_set_conversion(ctx) < 0)
+ return -1;
+ if (test_map_conversion(ctx) < 0)
+ return -1;
+ return 0;
+}
+
+/* Check that isl_basic_map_curry does not modify input.
+ */
+static int test_curry(isl_ctx *ctx)
+{
+ const char *str;
+ isl_basic_map *bmap1, *bmap2;
+ int equal;
+
+ str = "{ [A[] -> B[]] -> C[] }";
+ bmap1 = isl_basic_map_read_from_str(ctx, str);
+ bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
+ equal = isl_basic_map_is_equal(bmap1, bmap2);
+ isl_basic_map_free(bmap1);
+ isl_basic_map_free(bmap2);
+
+ if (equal < 0)
+ return -1;
+ if (equal)
+ isl_die(ctx, isl_error_unknown,
+ "curried map should not be equal to original",
+ return -1);
+
+ return 0;
+}
+
+struct {
+ const char *set;
+ const char *ma;
+ const char *res;
+} preimage_tests[] = {
+ { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
+ "{ A[j,i] -> B[i,j] }",
+ "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
+ { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
+ "{ A[a,b] -> B[a/2,b/6] }",
+ "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
+ { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
+ "{ A[a,b] -> B[a/2,b/6] }",
+ "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
+ "exists i,j : a = 2 i and b = 6 j }" },
+ { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
+ "[n] -> { : 0 <= n <= 100 }" },
+ { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
+ "{ A[a] -> B[2a] }",
+ "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
+ { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
+ "{ A[a] -> B[([a/2])] }",
+ "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
+ { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
+ "{ A[a] -> B[a,a,a/3] }",
+ "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
+ { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
+ "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
+};
+
+static int test_preimage_basic_set(isl_ctx *ctx)
+{
+ int i;
+ isl_basic_set *bset1, *bset2;
+ isl_multi_aff *ma;
+ int equal;
+
+ for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
+ bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
+ ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
+ bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
+ bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
+ equal = isl_basic_set_is_equal(bset1, bset2);
+ isl_basic_set_free(bset1);
+ isl_basic_set_free(bset2);
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown, "bad preimage",
+ return -1);
+ }
+
+ return 0;
+}
+
+struct {
+ const char *map;
+ const char *ma;
+ const char *res;
+} preimage_domain_tests[] = {
+ { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
+ "{ A[j,i] -> B[i,j] }",
+ "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
+ { "{ B[i] -> C[i]; D[i] -> E[i] }",
+ "{ A[i] -> B[i + 1] }",
+ "{ A[i] -> C[i + 1] }" },
+ { "{ B[i] -> C[i]; B[i] -> E[i] }",
+ "{ A[i] -> B[i + 1] }",
+ "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
+ { "{ B[i] -> C[([i/2])] }",
+ "{ A[i] -> B[2i] }",
+ "{ A[i] -> C[i] }" },
+ { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
+ "{ A[i] -> B[([i/5]), ([i/7])] }",
+ "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
+ { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
+ "[N] -> { A[] -> B[([N/5])] }",
+ "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
+ { "{ B[i] -> C[i] : exists a : i = 5 a }",
+ "{ A[i] -> B[2i] }",
+ "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
+ { "{ B[i] -> C[i] : exists a : i = 2 a; "
+ "B[i] -> D[i] : exists a : i = 2 a + 1 }",
+ "{ A[i] -> B[2i] }",
+ "{ A[i] -> C[2i] }" },
+};
+
+static int test_preimage_union_map(isl_ctx *ctx)
+{
+ int i;
+ isl_union_map *umap1, *umap2;
+ isl_multi_aff *ma;
+ int equal;
+
+ for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
+ umap1 = isl_union_map_read_from_str(ctx,
+ preimage_domain_tests[i].map);
+ ma = isl_multi_aff_read_from_str(ctx,
+ preimage_domain_tests[i].ma);
+ umap2 = isl_union_map_read_from_str(ctx,
+ preimage_domain_tests[i].res);
+ umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
+ equal = isl_union_map_is_equal(umap1, umap2);
+ isl_union_map_free(umap1);
+ isl_union_map_free(umap2);
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown, "bad preimage",
+ return -1);
+ }
+
+ return 0;
+}
+
+static int test_preimage(isl_ctx *ctx)
+{
+ if (test_preimage_basic_set(ctx) < 0)
+ return -1;
+ if (test_preimage_union_map(ctx) < 0)
+ return -1;
+
+ return 0;
+}
+
+struct {
+ const char *ma1;
+ const char *ma;
+ const char *res;
+} pullback_tests[] = {
+ { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
+ "{ A[a,b] -> C[b + 2a] }" },
+ { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
+ { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
+ { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
+ "{ A[a] -> C[(a)/6] }" },
+ { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
+ { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
+ "{ A[a] -> C[(2a)/3] }" },
+ { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
+ { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
+ "{ A[i,j] -> C[i + j, i + j] }"},
+ { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
+ { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
+ "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
+};
+
+static int test_pullback(isl_ctx *ctx)
+{
+ int i;
+ isl_multi_aff *ma1, *ma2;
+ isl_multi_aff *ma;
+ int equal;
+
+ for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
+ ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
+ ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
+ ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
+ ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
+ equal = isl_multi_aff_plain_is_equal(ma1, ma2);
+ isl_multi_aff_free(ma1);
+ isl_multi_aff_free(ma2);
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown, "bad pullback",
+ return -1);
+ }
+
+ return 0;
+}
+
+/* Check that negation is printed correctly.
+ */
+static int test_ast(isl_ctx *ctx)
+{
+ isl_ast_expr *expr, *expr1, *expr2, *expr3;
+ char *str;
+ int ok;
+
+ expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
+ expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
+ expr = isl_ast_expr_add(expr1, expr2);
+ expr = isl_ast_expr_neg(expr);
+ str = isl_ast_expr_to_str(expr);
+ ok = str ? !strcmp(str, "-(A + B)") : -1;
+ free(str);
+ isl_ast_expr_free(expr);
+
+ if (ok < 0)
+ return -1;
+ if (!ok)
+ isl_die(ctx, isl_error_unknown,
+ "isl_ast_expr printed incorrectly", return -1);
+
+ expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
+ expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
+ expr = isl_ast_expr_add(expr1, expr2);
+ expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
+ expr = isl_ast_expr_sub(expr3, expr);
+ str = isl_ast_expr_to_str(expr);
+ ok = str ? !strcmp(str, "C - (A + B)") : -1;
+ free(str);
+ isl_ast_expr_free(expr);
+
+ if (ok < 0)
+ return -1;
+ if (!ok)
+ isl_die(ctx, isl_error_unknown,
+ "isl_ast_expr printed incorrectly", return -1);
+
+ return 0;
+}
+
+/* Internal data structure for before_for and after_for callbacks.
+ *
+ * depth is the current depth
+ * before is the number of times before_for has been called
+ * after is the number of times after_for has been called
+ */
+struct isl_test_codegen_data {
+ int depth;
+ int before;
+ int after;
+};
+
+/* This function is called before each for loop in the AST generated
+ * from test_ast_gen1.
+ *
+ * Increment the number of calls and the depth.
+ * Check that the space returned by isl_ast_build_get_schedule_space
+ * matches the target space of the schedule returned by
+ * isl_ast_build_get_schedule.
+ * Return an isl_id that is checked by the corresponding call
+ * to after_for.
+ */
+static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
+ void *user)
+{
+ struct isl_test_codegen_data *data = user;
+ isl_ctx *ctx;
+ isl_space *space;
+ isl_union_map *schedule;
+ isl_union_set *uset;
+ isl_set *set;
+ int empty;
+ char name[] = "d0";
+
+ ctx = isl_ast_build_get_ctx(build);
+
+ if (data->before >= 3)
+ isl_die(ctx, isl_error_unknown,
+ "unexpected number of for nodes", return NULL);
+ if (data->depth >= 2)
+ isl_die(ctx, isl_error_unknown,
+ "unexpected depth", return NULL);
+
+ snprintf(name, sizeof(name), "d%d", data->depth);
+ data->before++;
+ data->depth++;
+
+ schedule = isl_ast_build_get_schedule(build);
+ uset = isl_union_map_range(schedule);
+ if (!uset)
+ return NULL;
+ if (isl_union_set_n_set(uset) != 1) {
+ isl_union_set_free(uset);
+ isl_die(ctx, isl_error_unknown,
+ "expecting single range space", return NULL);
+ }
+
+ space = isl_ast_build_get_schedule_space(build);
+ set = isl_union_set_extract_set(uset, space);
+ isl_union_set_free(uset);
+ empty = isl_set_is_empty(set);
+ isl_set_free(set);
+
+ if (empty < 0)
+ return NULL;
+ if (empty)
+ isl_die(ctx, isl_error_unknown,
+ "spaces don't match", return NULL);
+
+ return isl_id_alloc(ctx, name, NULL);
+}
+
+/* This function is called after each for loop in the AST generated
+ * from test_ast_gen1.
+ *
+ * Increment the number of calls and decrement the depth.
+ * Check that the annotation attached to the node matches
+ * the isl_id returned by the corresponding call to before_for.
+ */
+static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
+ __isl_keep isl_ast_build *build, void *user)
+{
+ struct isl_test_codegen_data *data = user;
+ isl_id *id;
+ const char *name;
+ int valid;
+
+ data->after++;
+ data->depth--;
+
+ if (data->after > data->before)
+ isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
+ "mismatch in number of for nodes",
+ return isl_ast_node_free(node));
+
+ id = isl_ast_node_get_annotation(node);
+ if (!id)
+ isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
+ "missing annotation", return isl_ast_node_free(node));
+
+ name = isl_id_get_name(id);
+ valid = name && atoi(name + 1) == data->depth;
+ isl_id_free(id);
+
+ if (!valid)
+ isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
+ "wrong annotation", return isl_ast_node_free(node));
+
+ return node;
+}
+
+/* Check that the before_each_for and after_each_for callbacks
+ * are called for each for loop in the generated code,
+ * that they are called in the right order and that the isl_id
+ * returned from the before_each_for callback is attached to
+ * the isl_ast_node passed to the corresponding after_each_for call.
+ */
+static int test_ast_gen1(isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_union_map *schedule;
+ isl_ast_build *build;
+ isl_ast_node *tree;
+ struct isl_test_codegen_data data;
+
+ str = "[N] -> { : N >= 10 }";
+ set = isl_set_read_from_str(ctx, str);
+ str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
+ "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
+ schedule = isl_union_map_read_from_str(ctx, str);
+
+ data.before = 0;
+ data.after = 0;
+ data.depth = 0;
+ build = isl_ast_build_from_context(set);
+ build = isl_ast_build_set_before_each_for(build,
+ &before_for, &data);
+ build = isl_ast_build_set_after_each_for(build,
+ &after_for, &data);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+ if (!tree)
+ return -1;
+
+ isl_ast_node_free(tree);
+
+ if (data.before != 3 || data.after != 3)
+ isl_die(ctx, isl_error_unknown,
+ "unexpected number of for nodes", return -1);
+
+ return 0;
+}
+
+/* Check that the AST generator handles domains that are integrally disjoint
+ * but not ratinoally disjoint.
+ */
+static int test_ast_gen2(isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_union_map *schedule;
+ isl_union_map *options;
+ isl_ast_build *build;
+ isl_ast_node *tree;
+
+ str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
+ schedule = isl_union_map_read_from_str(ctx, str);
+ set = isl_set_universe(isl_space_params_alloc(ctx, 0));
+ build = isl_ast_build_from_context(set);
+
+ str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
+ options = isl_union_map_read_from_str(ctx, str);
+ build = isl_ast_build_set_options(build, options);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+ if (!tree)
+ return -1;
+ isl_ast_node_free(tree);
+
+ return 0;
+}
+
+/* Increment *user on each call.
+ */
+static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
+ __isl_keep isl_ast_build *build, void *user)
+{
+ int *n = user;
+
+ (*n)++;
+
+ return node;
+}
+
+/* Test that unrolling tries to minimize the number of instances.
+ * In particular, for the schedule given below, make sure it generates
+ * 3 nodes (rather than 101).
+ */
+static int test_ast_gen3(isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_union_map *schedule;
+ isl_union_map *options;
+ isl_ast_build *build;
+ isl_ast_node *tree;
+ int n_domain = 0;
+
+ str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
+ schedule = isl_union_map_read_from_str(ctx, str);
+ set = isl_set_universe(isl_space_params_alloc(ctx, 0));
+
+ str = "{ [i] -> unroll[0] }";
+ options = isl_union_map_read_from_str(ctx, str);
+
+ build = isl_ast_build_from_context(set);
+ build = isl_ast_build_set_options(build, options);
+ build = isl_ast_build_set_at_each_domain(build,
+ &count_domains, &n_domain);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+ if (!tree)
+ return -1;
+
+ isl_ast_node_free(tree);
+
+ if (n_domain != 3)
+ isl_die(ctx, isl_error_unknown,
+ "unexpected number of for nodes", return -1);
+
+ return 0;
+}
+
+/* Check that if the ast_build_exploit_nested_bounds options is set,
+ * we do not get an outer if node in the generated AST,
+ * while we do get such an outer if node if the options is not set.
+ */
+static int test_ast_gen4(isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_union_map *schedule;
+ isl_ast_build *build;
+ isl_ast_node *tree;
+ enum isl_ast_node_type type;
+ int enb;
+
+ enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
+ str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
+
+ isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
+
+ schedule = isl_union_map_read_from_str(ctx, str);
+ set = isl_set_universe(isl_space_params_alloc(ctx, 0));
+ build = isl_ast_build_from_context(set);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+ if (!tree)
+ return -1;
+
+ type = isl_ast_node_get_type(tree);
+ isl_ast_node_free(tree);
+
+ if (type == isl_ast_node_if)
+ isl_die(ctx, isl_error_unknown,
+ "not expecting if node", return -1);
+
+ isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
+
+ schedule = isl_union_map_read_from_str(ctx, str);
+ set = isl_set_universe(isl_space_params_alloc(ctx, 0));
+ build = isl_ast_build_from_context(set);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+ if (!tree)
+ return -1;
+
+ type = isl_ast_node_get_type(tree);
+ isl_ast_node_free(tree);
+
+ if (type != isl_ast_node_if)
+ isl_die(ctx, isl_error_unknown,
+ "expecting if node", return -1);
+
+ isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
+
+ return 0;
+}
+
+/* This function is called for each leaf in the AST generated
+ * from test_ast_gen5.
+ *
+ * We finalize the AST generation by extending the outer schedule
+ * with a zero-dimensional schedule. If this results in any for loops,
+ * then this means that we did not pass along enough information
+ * about the outer schedule to the inner AST generation.
+ */
+static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
+ void *user)
+{
+ isl_union_map *schedule, *extra;
+ isl_ast_node *tree;
+
+ schedule = isl_ast_build_get_schedule(build);
+ extra = isl_union_map_copy(schedule);
+ extra = isl_union_map_from_domain(isl_union_map_domain(extra));
+ schedule = isl_union_map_range_product(schedule, extra);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+
+ if (!tree)
+ return NULL;
+
+ if (isl_ast_node_get_type(tree) == isl_ast_node_for)
+ isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
+ "code should not contain any for loop",
+ return isl_ast_node_free(tree));
+
+ return tree;
+}
+
+/* Check that we do not lose any information when going back and
+ * forth between internal and external schedule.
+ *
+ * In particular, we create an AST where we unroll the only
+ * non-constant dimension in the schedule. We therefore do
+ * not expect any for loops in the AST. However, older versions
+ * of isl would not pass along enough information about the outer
+ * schedule when performing an inner code generation from a create_leaf
+ * callback, resulting in the inner code generation producing a for loop.
+ */
+static int test_ast_gen5(isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_union_map *schedule, *options;
+ isl_ast_build *build;
+ isl_ast_node *tree;
+
+ str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
+ schedule = isl_union_map_read_from_str(ctx, str);
+
+ str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
+ "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
+ options = isl_union_map_read_from_str(ctx, str);
+
+ set = isl_set_universe(isl_space_params_alloc(ctx, 0));
+ build = isl_ast_build_from_context(set);
+ build = isl_ast_build_set_options(build, options);
+ build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
+ tree = isl_ast_build_ast_from_schedule(build, schedule);
+ isl_ast_build_free(build);
+ isl_ast_node_free(tree);
+ if (!tree)
+ return -1;
+
+ return 0;
+}
+
+static int test_ast_gen(isl_ctx *ctx)
+{
+ if (test_ast_gen1(ctx) < 0)
+ return -1;
+ if (test_ast_gen2(ctx) < 0)
+ return -1;
+ if (test_ast_gen3(ctx) < 0)
+ return -1;
+ if (test_ast_gen4(ctx) < 0)
+ return -1;
+ if (test_ast_gen5(ctx) < 0)
+ return -1;
+ return 0;
+}
+
+/* Check if dropping output dimensions from an isl_pw_multi_aff
+ * works properly.
+ */
+static int test_pw_multi_aff(isl_ctx *ctx)
+{
+ const char *str;
+ isl_pw_multi_aff *pma1, *pma2;
+ int equal;
+
+ str = "{ [i,j] -> [i+j, 4i-j] }";
+ pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
+ str = "{ [i,j] -> [4i-j] }";
+ pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
+
+ pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
+
+ equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
+
+ isl_pw_multi_aff_free(pma1);
+ isl_pw_multi_aff_free(pma2);
+ if (equal < 0)
+ return -1;
+ if (!equal)
+ isl_die(ctx, isl_error_unknown,
+ "expressions not equal", return -1);
+
+ return 0;
+}
+
+/* This is a regression test for a bug where isl_basic_map_simplify
+ * would end up in an infinite loop. In particular, we construct
+ * an empty basic set that is not obviously empty.
+ * isl_basic_set_is_empty marks the basic set as empty.
+ * After projecting out i3, the variable can be dropped completely,
+ * but isl_basic_map_simplify refrains from doing so if the basic set
+ * is empty and would end up in an infinite loop if it didn't test
+ * explicitly for empty basic maps in the outer loop.
+ */
+static int test_simplify(isl_ctx *ctx)
+{
+ const char *str;
+ isl_basic_set *bset;
+ int empty;
+
+ str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
+ "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
+ "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
+ "i3 >= i2 }";
+ bset = isl_basic_set_read_from_str(ctx, str);
+ empty = isl_basic_set_is_empty(bset);
+ bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
+ isl_basic_set_free(bset);
+ if (!bset)
+ return -1;
+ if (!empty)
+ isl_die(ctx, isl_error_unknown,
+ "basic set should be empty", return -1);
+
+ return 0;
+}
+
+/* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
+ * with gbr context would fail to disable the use of the shifted tableau
+ * when transferring equalities for the input to the context, resulting
+ * in invalid sample values.
+ */
+static int test_partial_lexmin(isl_ctx *ctx)
+{
+ const char *str;
+ isl_basic_set *bset;
+ isl_basic_map *bmap;
+ isl_map *map;
+
+ str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
+ bmap = isl_basic_map_read_from_str(ctx, str);
+ str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
+ bset = isl_basic_set_read_from_str(ctx, str);
+ map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
+ isl_map_free(map);
+
+ if (!map)
+ return -1;
+
+ return 0;
+}
+
+/* Check that the variable compression performed on the existentially
+ * quantified variables inside isl_basic_set_compute_divs is not confused
+ * by the implicit equalities among the parameters.
+ */
+static int test_compute_divs(isl_ctx *ctx)
+{
+ const char *str;
+ isl_basic_set *bset;
+ isl_set *set;
+
+ str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
+ "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
+ "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
+ "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
+ bset = isl_basic_set_read_from_str(ctx, str);
+ set = isl_basic_set_compute_divs(bset);
+ isl_set_free(set);
+ if (!set)
+ return -1;
+
+ return 0;
+}
+
+struct {
+ const char *name;
+ int (*fn)(isl_ctx *ctx);
+} tests [] = {
+ { "val", &test_val },
+ { "compute divs", &test_compute_divs },
+ { "partial lexmin", &test_partial_lexmin },
+ { "simplify", &test_simplify },
+ { "curry", &test_curry },
+ { "piecewise multi affine expressions", &test_pw_multi_aff },
+ { "conversion", &test_conversion },
+ { "list", &test_list },
+ { "align parameters", &test_align_parameters },
+ { "preimage", &test_preimage },
+ { "pullback", &test_pullback },
+ { "AST", &test_ast },
+ { "AST generation", &test_ast_gen },
+ { "eliminate", &test_eliminate },
+ { "residue class", &test_residue_class },
+ { "div", &test_div },
+ { "slice", &test_slice },
+ { "fixed power", &test_fixed_power },
+ { "sample", &test_sample },
+ { "output", &test_output },
+ { "vertices", &test_vertices },
+ { "fixed", &test_fixed },
+ { "equal", &test_equal },
+ { "product", &test_product },
+ { "dim_max", &test_dim_max },
+ { "affine", &test_aff },
+ { "injective", &test_injective },
+ { "schedule", &test_schedule },
+ { "union_pw", &test_union_pw },
+ { "parse", &test_parse },
+ { "single-valued", &test_sv },
+ { "affine hull", &test_affine_hull },
+ { "coalesce", &test_coalesce },
+ { "factorize", &test_factorize },
+ { "subset", &test_subset },
+ { "subtract", &test_subtract },
+ { "lexmin", &test_lexmin },
+ { "gist", &test_gist },
+ { "piecewise quasi-polynomials", &test_pwqp },
+};
+
+int main()
+{
+ int i;
+ struct isl_ctx *ctx;
+
+ srcdir = getenv("srcdir");
+ assert(srcdir);
+
+ ctx = isl_ctx_alloc();
+ for (i = 0; i < ARRAY_SIZE(tests); ++i) {
+ printf("%s\n", tests[i].name);
+ if (tests[i].fn(ctx) < 0)
+ goto error;
+ }
+ test_lift(ctx);
+ test_bound(ctx);
+ test_union(ctx);
+ test_split_periods(ctx);
+ test_lex(ctx);