2 * Copyright 2012 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl/union_map.h>
16 #include <isl_tarjan.h>
17 #include <isl_ast_private.h>
18 #include <isl_ast_build_expr.h>
19 #include <isl_ast_build_private.h>
20 #include <isl_ast_graft_private.h>
21 #include <isl_list_private.h>
23 /* Add the constraint to the list that "user" points to, if it is not
26 static int collect_constraint(__isl_take isl_constraint *constraint,
29 isl_constraint_list **list = user;
31 if (isl_constraint_is_div_constraint(constraint))
32 isl_constraint_free(constraint);
34 *list = isl_constraint_list_add(*list, constraint);
39 /* Extract the constraints of "bset" (except the div constraints)
40 * and collect them in an isl_constraint_list.
42 static __isl_give isl_constraint_list *isl_constraint_list_from_basic_set(
43 __isl_take isl_basic_set *bset)
47 isl_constraint_list *list;
52 ctx = isl_basic_set_get_ctx(bset);
54 n = isl_basic_set_n_constraint(bset);
55 list = isl_constraint_list_alloc(ctx, n);
56 if (isl_basic_set_foreach_constraint(bset,
57 &collect_constraint, &list) < 0)
58 list = isl_constraint_list_free(list);
60 isl_basic_set_free(bset);
64 /* Data used in generate_domain.
66 * "build" is the input build.
67 * "list" collects the results.
69 struct isl_generate_domain_data {
72 isl_ast_graft_list *list;
75 static __isl_give isl_ast_graft_list *generate_next_level(
76 __isl_take isl_union_map *executed,
77 __isl_take isl_ast_build *build);
78 static __isl_give isl_ast_graft_list *generate_code(
79 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
82 /* Generate an AST for a single domain based on
83 * the (non single valued) inverse schedule "executed".
85 * We extend the schedule with the iteration domain
86 * and continue generating through a call to generate_code.
88 * In particular, if executed has the form
92 * then we continue generating code on
96 * The extended inverse schedule is clearly single valued
97 * ensuring that the nested generate_code will not reach this function,
98 * but will instead create calls to all elements of D that need
99 * to be executed from the current schedule domain.
101 static int generate_non_single_valued(__isl_take isl_map *executed,
102 struct isl_generate_domain_data *data)
105 isl_ast_build *build;
106 isl_ast_graft_list *list;
108 build = isl_ast_build_copy(data->build);
110 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
111 executed = isl_map_domain_product(executed, identity);
112 build = isl_ast_build_set_single_valued(build, 1);
114 list = generate_code(isl_union_map_from_map(executed), build, 1);
116 data->list = isl_ast_graft_list_concat(data->list, list);
121 /* Call the at_each_domain callback, if requested by the user,
122 * after recording the current inverse schedule in the build.
124 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
125 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
127 if (!graft || !build)
128 return isl_ast_graft_free(graft);
129 if (!build->at_each_domain)
132 build = isl_ast_build_copy(build);
133 build = isl_ast_build_set_executed(build,
134 isl_union_map_from_map(isl_map_copy(executed)));
136 return isl_ast_graft_free(graft);
138 graft->node = build->at_each_domain(graft->node,
139 build, build->at_each_domain_user);
140 isl_ast_build_free(build);
143 graft = isl_ast_graft_free(graft);
148 /* Generate an AST for a single domain based on
149 * the inverse schedule "executed".
151 * If there is more than one domain element associated to the current
152 * schedule "time", then we need to continue the generation process
153 * in generate_non_single_valued.
154 * Note that the inverse schedule being single-valued may depend
155 * on constraints that are only available in the original context
156 * domain specified by the user. We therefore first introduce
157 * the constraints from data->build->domain.
158 * On the other hand, we only perform the test after having taken the gist
159 * of the domain as the resulting map is the one from which the call
160 * expression is constructed. Using this map to construct the call
161 * expression usually yields simpler results.
162 * Because we perform the single-valuedness test on the gisted map,
163 * we may in rare cases fail to recognize that the inverse schedule
164 * is single-valued. This becomes problematic if this happens
165 * from the recursive call through generate_non_single_valued
166 * as we would then end up in an infinite recursion.
167 * We therefore check if we are inside a call to generate_non_single_valued
168 * and revert to the ungisted map if the gisted map turns out not to be
171 * Otherwise, we generate a call expression for the single executed
172 * domain element and put a guard around it based on the (simplified)
173 * domain of "executed".
175 * If the user has set an at_each_domain callback, it is called
176 * on the constructed call expression node.
178 static int generate_domain(__isl_take isl_map *executed, void *user)
180 struct isl_generate_domain_data *data = user;
181 isl_ast_graft *graft;
182 isl_ast_graft_list *list;
187 executed = isl_map_intersect_domain(executed,
188 isl_set_copy(data->build->domain));
190 executed = isl_map_coalesce(executed);
191 map = isl_map_copy(executed);
192 map = isl_ast_build_compute_gist_map_domain(data->build, map);
193 sv = isl_map_is_single_valued(map);
198 if (data->build->single_valued)
199 map = isl_map_copy(executed);
201 return generate_non_single_valued(executed, data);
203 guard = isl_map_domain(isl_map_copy(map));
204 guard = isl_set_coalesce(guard);
205 guard = isl_ast_build_compute_gist(data->build, guard);
206 graft = isl_ast_graft_alloc_domain(map, data->build);
207 graft = at_each_domain(graft, executed, data->build);
209 isl_map_free(executed);
210 graft = isl_ast_graft_add_guard(graft, guard, data->build);
212 list = isl_ast_graft_list_from_ast_graft(graft);
213 data->list = isl_ast_graft_list_concat(data->list, list);
218 isl_map_free(executed);
222 /* Call build->create_leaf to a create "leaf" node in the AST,
223 * encapsulate the result in an isl_ast_graft and return the result
224 * as a 1-element list.
226 * Note that the node returned by the user may be an entire tree.
228 * Before we pass control to the user, we first clear some information
229 * from the build that is (presumbably) only meaningful
230 * for the current code generation.
231 * This includes the create_leaf callback itself, so we make a copy
232 * of the build first.
234 static __isl_give isl_ast_graft_list *call_create_leaf(
235 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
238 isl_ast_graft *graft;
239 isl_ast_build *user_build;
241 user_build = isl_ast_build_copy(build);
242 user_build = isl_ast_build_set_executed(user_build, executed);
243 user_build = isl_ast_build_clear_local_info(user_build);
247 node = build->create_leaf(user_build, build->create_leaf_user);
248 graft = isl_ast_graft_alloc(node, build);
249 isl_ast_build_free(build);
250 return isl_ast_graft_list_from_ast_graft(graft);
253 /* Generate an AST after having handled the complete schedule
254 * of this call to the code generator.
256 * If the user has specified a create_leaf callback, control
257 * is passed to the user in call_create_leaf.
259 * Otherwise, we generate one or more calls for each individual
260 * domain in generate_domain.
262 static __isl_give isl_ast_graft_list *generate_inner_level(
263 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
266 struct isl_generate_domain_data data = { build };
268 if (!build || !executed)
271 if (build->create_leaf)
272 return call_create_leaf(executed, build);
274 ctx = isl_union_map_get_ctx(executed);
275 data.list = isl_ast_graft_list_alloc(ctx, 0);
276 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
277 data.list = isl_ast_graft_list_free(data.list);
280 error: data.list = NULL;
281 isl_ast_build_free(build);
282 isl_union_map_free(executed);
286 /* Call the before_each_for callback, if requested by the user.
288 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
289 __isl_keep isl_ast_build *build)
294 return isl_ast_node_free(node);
295 if (!build->before_each_for)
297 id = build->before_each_for(build, build->before_each_for_user);
298 node = isl_ast_node_set_annotation(node, id);
302 /* Call the after_each_for callback, if requested by the user.
304 static __isl_give isl_ast_graft *after_each_for(__isl_keep isl_ast_graft *graft,
305 __isl_keep isl_ast_build *build)
307 if (!graft || !build)
308 return isl_ast_graft_free(graft);
309 if (!build->after_each_for)
311 graft->node = build->after_each_for(graft->node, build,
312 build->after_each_for_user);
314 return isl_ast_graft_free(graft);
318 /* Plug in all the know values of the current and outer dimensions
319 * in the domain of "executed". In principle, we only need to plug
320 * in the known value of the current dimension since the values of
321 * outer dimensions have been plugged in already.
322 * However, it turns out to be easier to just plug in all known values.
324 static __isl_give isl_union_map *plug_in_values(
325 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
327 return isl_ast_build_substitute_values_union_map_domain(build,
331 /* Check if the constraint "c" is a lower bound on dimension "pos",
332 * an upper bound, or independent of dimension "pos".
334 static int constraint_type(isl_constraint *c, int pos)
336 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
338 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
343 /* Compare the types of the constraints "a" and "b",
344 * resulting in constraints that are independent of "depth"
345 * to be sorted before the lower bounds on "depth", which in
346 * turn are sorted before the upper bounds on "depth".
348 static int cmp_constraint(const void *a, const void *b, void *user)
351 isl_constraint * const *c1 = a;
352 isl_constraint * const *c2 = b;
353 int t1 = constraint_type(*c1, *depth);
354 int t2 = constraint_type(*c2, *depth);
359 /* Extract a lower bound on dimension "pos" from constraint "c".
361 * If the constraint is of the form
365 * then we essentially return
367 * l = ceil(-f(...)/a)
369 * However, if the current dimension is strided, then we need to make
370 * sure that the lower bound we construct is of the form
374 * with f the offset and s the stride.
375 * We therefore compute
377 * f + s * ceil((l - f)/s)
379 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
380 int pos, __isl_keep isl_ast_build *build)
384 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
385 aff = isl_aff_ceil(aff);
387 if (isl_ast_build_has_stride(build, pos)) {
391 isl_int_init(stride);
393 offset = isl_ast_build_get_offset(build, pos);
394 isl_ast_build_get_stride(build, pos, &stride);
396 aff = isl_aff_sub(aff, isl_aff_copy(offset));
397 aff = isl_aff_scale_down(aff, stride);
398 aff = isl_aff_ceil(aff);
399 aff = isl_aff_scale(aff, stride);
400 aff = isl_aff_add(aff, offset);
402 isl_int_clear(stride);
405 aff = isl_ast_build_compute_gist_aff(build, aff);
410 /* Return the exact lower bound (or upper bound if "upper" is set)
411 * of "domain" as a piecewise affine expression.
413 * If we are computing a lower bound (of a strided dimension), then
414 * we need to make sure it is of the form
418 * where f is the offset and s is the stride.
419 * We therefore need to include the stride constraint before computing
422 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
423 __isl_keep isl_ast_build *build, int upper)
428 isl_pw_multi_aff *pma;
430 domain = isl_set_copy(domain);
432 stride = isl_ast_build_get_stride_constraint(build);
433 domain = isl_set_intersect(domain, stride);
435 it_map = isl_ast_build_map_to_iterator(build, domain);
437 pma = isl_map_lexmax_pw_multi_aff(it_map);
439 pma = isl_map_lexmin_pw_multi_aff(it_map);
440 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
441 isl_pw_multi_aff_free(pma);
442 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
443 pa = isl_pw_aff_coalesce(pa);
448 /* Return a list of "n" lower bounds on dimension "pos"
449 * extracted from the "n" constraints starting at "constraint".
450 * If "n" is zero, then we extract a lower bound from "domain" instead.
452 static __isl_give isl_pw_aff_list *lower_bounds(
453 __isl_keep isl_constraint **constraint, int n, int pos,
454 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
457 isl_pw_aff_list *list;
465 pa = exact_bound(domain, build, 0);
466 return isl_pw_aff_list_from_pw_aff(pa);
469 ctx = isl_ast_build_get_ctx(build);
470 list = isl_pw_aff_list_alloc(ctx,n);
472 for (i = 0; i < n; ++i) {
475 aff = lower_bound(constraint[i], pos, build);
476 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
482 /* Return a list of "n" upper bounds on dimension "pos"
483 * extracted from the "n" constraints starting at "constraint".
484 * If "n" is zero, then we extract an upper bound from "domain" instead.
486 static __isl_give isl_pw_aff_list *upper_bounds(
487 __isl_keep isl_constraint **constraint, int n, int pos,
488 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
491 isl_pw_aff_list *list;
496 pa = exact_bound(domain, build, 1);
497 return isl_pw_aff_list_from_pw_aff(pa);
500 ctx = isl_ast_build_get_ctx(build);
501 list = isl_pw_aff_list_alloc(ctx,n);
503 for (i = 0; i < n; ++i) {
506 aff = isl_constraint_get_bound(constraint[i], isl_dim_set, pos);
507 aff = isl_aff_floor(aff);
508 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
514 /* Return an isl_ast_expr that performs the reduction of type "type"
515 * on AST expressions corresponding to the elements in "list".
517 * The list is assumed to contain at least one element.
518 * If the list contains exactly one element, then the returned isl_ast_expr
519 * simply computes that affine expression.
521 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
522 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
531 n = isl_pw_aff_list_n_pw_aff(list);
534 return isl_ast_build_expr_from_pw_aff_internal(build,
535 isl_pw_aff_list_get_pw_aff(list, 0));
537 ctx = isl_pw_aff_list_get_ctx(list);
538 expr = isl_ast_expr_alloc_op(ctx, type, n);
542 for (i = 0; i < n; ++i) {
543 isl_ast_expr *expr_i;
545 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
546 isl_pw_aff_list_get_pw_aff(list, i));
548 return isl_ast_expr_free(expr);
549 expr->u.op.args[i] = expr_i;
555 /* Add a guard to "graft" based on "bound" in the case of a degenerate
556 * level (including the special case of an eliminated level).
558 * We eliminate the current dimension, simplify the result in the current
559 * build and add the result as guards to the graft.
561 * Note that we cannot simply drop the constraints on the current dimension
562 * even in the eliminated case, because the single affine expression may
563 * not be explicitly available in "bounds". Moreover, the single affine
564 * expression may only be defined on a subset of the build domain,
565 * so we do in some cases need to insert a guard even in the eliminated case.
567 static __isl_give isl_ast_graft *add_degenerate_guard(
568 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
569 __isl_keep isl_ast_build *build)
574 depth = isl_ast_build_get_depth(build);
576 dom = isl_set_from_basic_set(isl_basic_set_copy(bounds));
577 if (isl_ast_build_has_stride(build, depth)) {
580 stride = isl_ast_build_get_stride_constraint(build);
581 dom = isl_set_intersect(dom, stride);
583 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
584 dom = isl_ast_build_compute_gist(build, dom);
586 graft = isl_ast_graft_add_guard(graft, dom, build);
591 /* Update "graft" based on "bounds" for the eliminated case.
593 * In the eliminated case, no for node is created, so we only need
594 * to check if "bounds" imply any guards that need to be inserted.
596 static __isl_give isl_ast_graft *refine_eliminated(
597 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
598 __isl_keep isl_ast_build *build)
600 return add_degenerate_guard(graft, bounds, build);
603 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
605 * "build" is the build in which graft->node was created
606 * "sub_build" contains information about the current level itself,
607 * including the single value attained.
609 * We first set the initialization part of the for loop to the single
610 * value attained by the current dimension.
611 * The increment and condition are not strictly needed as the are known
612 * to be "1" and "iterator <= value" respectively.
613 * Then we set the size of the iterator and
614 * check if "bounds" imply any guards that need to be inserted.
616 static __isl_give isl_ast_graft *refine_degenerate(
617 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
618 __isl_keep isl_ast_build *build,
619 __isl_keep isl_ast_build *sub_build)
623 if (!graft || !sub_build)
624 return isl_ast_graft_free(graft);
626 value = isl_pw_aff_copy(sub_build->value);
628 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
630 if (!graft->node->u.f.init)
631 return isl_ast_graft_free(graft);
633 graft = add_degenerate_guard(graft, bounds, build);
638 /* Return the intersection of the "n" constraints starting at "constraint"
641 static __isl_give isl_set *intersect_constraints(isl_ctx *ctx,
642 __isl_keep isl_constraint **constraint, int n)
648 isl_die(ctx, isl_error_internal,
649 "expecting at least one constraint", return NULL);
651 bset = isl_basic_set_from_constraint(
652 isl_constraint_copy(constraint[0]));
653 for (i = 1; i < n; ++i) {
654 isl_basic_set *bset_i;
656 bset_i = isl_basic_set_from_constraint(
657 isl_constraint_copy(constraint[i]));
658 bset = isl_basic_set_intersect(bset, bset_i);
661 return isl_set_from_basic_set(bset);
664 /* Compute the constraints on the outer dimensions enforced by
665 * graft->node and add those constraints to graft->enforced,
666 * in case the upper bound is expressed as a set "upper".
668 * In particular, if l(...) is a lower bound in "lower", and
670 * -a i + f(...) >= 0 or a i <= f(...)
672 * is an upper bound ocnstraint on the current dimension i,
673 * then the for loop enforces the constraint
675 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
677 * We therefore simply take each lower bound in turn, plug it into
678 * the upper bounds and compute the intersection over all lower bounds.
680 * If a lower bound is a rational expression, then
681 * isl_basic_set_preimage_multi_aff will force this rational
682 * expression to have only integer values. However, the loop
683 * itself does not enforce this integrality constraint. We therefore
684 * use the ceil of the lower bounds instead of the lower bounds themselves.
685 * Other constraints will make sure that the for loop is only executed
686 * when each of the lower bounds attains an integral value.
687 * In particular, potentially rational values only occur in
688 * lower_bound if the offset is a (seemingly) rational expression,
689 * but then outer conditions will make sure that this rational expression
690 * only attains integer values.
692 static __isl_give isl_ast_graft *set_enforced_from_set(
693 __isl_take isl_ast_graft *graft,
694 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
697 isl_basic_set *enforced;
698 isl_pw_multi_aff *pma;
701 if (!graft || !lower)
702 return isl_ast_graft_free(graft);
704 space = isl_set_get_space(upper);
705 enforced = isl_basic_set_universe(isl_space_copy(space));
707 space = isl_space_map_from_set(space);
708 pma = isl_pw_multi_aff_identity(space);
710 n = isl_pw_aff_list_n_pw_aff(lower);
711 for (i = 0; i < n; ++i) {
715 isl_pw_multi_aff *pma_i;
717 pa = isl_pw_aff_list_get_pw_aff(lower, i);
718 pa = isl_pw_aff_ceil(pa);
719 pma_i = isl_pw_multi_aff_copy(pma);
720 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
721 enforced_i = isl_set_copy(upper);
722 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
723 hull = isl_set_simple_hull(enforced_i);
724 enforced = isl_basic_set_intersect(enforced, hull);
727 isl_pw_multi_aff_free(pma);
729 graft = isl_ast_graft_enforce(graft, enforced);
734 /* Compute the constraints on the outer dimensions enforced by
735 * graft->node and add those constraints to graft->enforced,
736 * in case the upper bound is expressed as
737 * a list of affine expressions "upper".
739 * The enforced condition is that each lower bound expression is less
740 * than or equal to each upper bound expression.
742 static __isl_give isl_ast_graft *set_enforced_from_list(
743 __isl_take isl_ast_graft *graft,
744 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
747 isl_basic_set *enforced;
749 lower = isl_pw_aff_list_copy(lower);
750 upper = isl_pw_aff_list_copy(upper);
751 cond = isl_pw_aff_list_le_set(lower, upper);
752 enforced = isl_set_simple_hull(cond);
753 graft = isl_ast_graft_enforce(graft, enforced);
758 /* Does "aff" have a negative constant term?
760 static int aff_constant_is_negative(__isl_take isl_set *set,
761 __isl_take isl_aff *aff, void *user)
767 isl_aff_get_constant(aff, &v);
768 *neg = isl_int_is_neg(v);
773 return *neg ? 0 : -1;
776 /* Does "pa" have a negative constant term over its entire domain?
778 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
783 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
786 return *neg ? 0 : -1;
789 /* Does each element in "list" have a negative constant term?
791 * The callback terminates the iteration as soon an element has been
792 * found that does not have a negative constant term.
794 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
798 if (isl_pw_aff_list_foreach(list,
799 &pw_aff_constant_is_negative, &neg) < 0 && neg)
805 /* Add 1 to each of the elements in "list", where each of these elements
806 * is defined over the internal schedule space of "build".
808 static __isl_give isl_pw_aff_list *list_add_one(
809 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
816 space = isl_ast_build_get_space(build, 1);
817 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
818 aff = isl_aff_add_constant_si(aff, 1);
819 one = isl_pw_aff_from_aff(aff);
821 n = isl_pw_aff_list_n_pw_aff(list);
822 for (i = 0; i < n; ++i) {
824 pa = isl_pw_aff_list_get_pw_aff(list, i);
825 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
826 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
829 isl_pw_aff_free(one);
834 /* Set the condition part of the for node graft->node in case
835 * the upper bound is represented as a list of piecewise affine expressions.
837 * In particular, set the condition to
839 * iterator <= min(list of upper bounds)
841 * If each of the upper bounds has a negative constant term, then
842 * set the condition to
844 * iterator < min(list of (upper bound + 1)s)
847 static __isl_give isl_ast_graft *set_for_cond_from_list(
848 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
849 __isl_keep isl_ast_build *build)
852 isl_ast_expr *bound, *iterator, *cond;
853 enum isl_ast_op_type type = isl_ast_op_le;
856 return isl_ast_graft_free(graft);
858 neg = list_constant_is_negative(list);
860 return isl_ast_graft_free(graft);
861 list = isl_pw_aff_list_copy(list);
863 list = list_add_one(list, build);
864 type = isl_ast_op_lt;
867 bound = reduce_list(isl_ast_op_min, list, build);
868 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
869 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
870 graft->node->u.f.cond = cond;
872 isl_pw_aff_list_free(list);
873 if (!graft->node->u.f.cond)
874 return isl_ast_graft_free(graft);
878 /* Set the condition part of the for node graft->node in case
879 * the upper bound is represented as a set.
881 static __isl_give isl_ast_graft *set_for_cond_from_set(
882 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
883 __isl_keep isl_ast_build *build)
890 cond = isl_ast_build_expr_from_set(build, isl_set_copy(set));
891 graft->node->u.f.cond = cond;
892 if (!graft->node->u.f.cond)
893 return isl_ast_graft_free(graft);
897 /* Construct an isl_ast_expr for the increment (i.e., stride) of
898 * the current dimension.
900 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
909 ctx = isl_ast_build_get_ctx(build);
910 depth = isl_ast_build_get_depth(build);
912 if (!isl_ast_build_has_stride(build, depth))
913 return isl_ast_expr_alloc_int_si(ctx, 1);
916 isl_ast_build_get_stride(build, depth, &v);
917 inc = isl_ast_expr_alloc_int(ctx, v);
923 /* Should we express the loop condition as
925 * iterator <= min(list of upper bounds)
927 * or as a conjunction of constraints?
929 * The first is constructed from a list of upper bounds.
930 * The second is constructed from a set.
932 * If there are no upper bounds in "constraints", then this could mean
933 * that "domain" simply doesn't have an upper bound or that we didn't
934 * pick any upper bound. In the first case, we want to generate the
935 * loop condition as a(n empty) conjunction of constraints
936 * In the second case, we will compute
937 * a single upper bound from "domain" and so we use the list form.
939 * If there are upper bounds in "constraints",
940 * then we use the list form iff the atomic_upper_bound option is set.
942 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
943 __isl_keep isl_set *domain, int depth)
946 return isl_options_get_ast_build_atomic_upper_bound(ctx);
948 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
951 /* Fill in the expressions of the for node in graft->node.
954 * - set the initialization part of the loop to the maximum of the lower bounds
955 * - set the size of the iterator based on the values attained by the iterator
956 * - extract the increment from the stride of the current dimension
957 * - construct the for condition either based on a list of upper bounds
958 * or on a set of upper bound constraints.
960 static __isl_give isl_ast_graft *set_for_node_expressions(
961 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
962 int use_list, __isl_keep isl_pw_aff_list *upper_list,
963 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
970 build = isl_ast_build_copy(build);
971 build = isl_ast_build_set_enforced(build,
972 isl_ast_graft_get_enforced(graft));
975 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
976 node->u.f.inc = for_inc(build);
979 graft = set_for_cond_from_list(graft, upper_list, build);
981 graft = set_for_cond_from_set(graft, upper_set, build);
983 isl_ast_build_free(build);
985 if (!node->u.f.iterator || !node->u.f.init ||
986 !node->u.f.cond || !node->u.f.inc)
987 return isl_ast_graft_free(graft);
992 /* Update "graft" based on "bounds" and "domain" for the generic,
993 * non-degenerate, case.
995 * "constraints" contains the "n_lower" lower and "n_upper" upper bounds
996 * that the loop node should express.
997 * "domain" is the subset of the intersection of the constraints
998 * for which some code is executed.
1000 * There may be zero lower bounds or zero upper bounds in "constraints"
1001 * in case the list of constraints was created
1002 * based on the atomic option or based on separation with explicit bounds.
1003 * In that case, we use "domain" to derive lower and/or upper bounds.
1005 * We first compute a list of one or more lower bounds.
1007 * Then we decide if we want to express the condition as
1009 * iterator <= min(list of upper bounds)
1011 * or as a conjunction of constraints.
1013 * The set of enforced constraints is then computed either based on
1014 * a list of upper bounds or on a set of upper bound constraints.
1015 * We do not compute any enforced constraints if we were forced
1016 * to compute a lower or upper bound using exact_bound. The domains
1017 * of the resulting expressions may imply some bounds on outer dimensions
1018 * that we do not want to appear in the enforced constraints since
1019 * they are not actually enforced by the corresponding code.
1021 * Finally, we fill in the expressions of the for node.
1023 static __isl_give isl_ast_graft *refine_generic_bounds(
1024 __isl_take isl_ast_graft *graft,
1025 __isl_keep isl_constraint **constraint, int n_lower, int n_upper,
1026 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1030 isl_pw_aff_list *lower;
1032 isl_set *upper_set = NULL;
1033 isl_pw_aff_list *upper_list = NULL;
1035 if (!graft || !build)
1036 return isl_ast_graft_free(graft);
1038 depth = isl_ast_build_get_depth(build);
1039 ctx = isl_ast_graft_get_ctx(graft);
1041 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1043 lower = lower_bounds(constraint, n_lower, depth, domain, build);
1046 upper_list = upper_bounds(constraint + n_lower, n_upper, depth,
1048 else if (n_upper > 0)
1049 upper_set = intersect_constraints(ctx, constraint + n_lower,
1052 upper_set = isl_set_universe(isl_set_get_space(domain));
1054 if (n_lower == 0 || n_upper == 0)
1057 graft = set_enforced_from_list(graft, lower, upper_list);
1059 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1061 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1064 isl_pw_aff_list_free(lower);
1065 isl_pw_aff_list_free(upper_list);
1066 isl_set_free(upper_set);
1071 /* How many constraints in the "constraint" array, starting at position "first"
1072 * are of the give type? "n" represents the total number of elements
1075 static int count_constraints(isl_constraint **constraint, int n, int first,
1080 constraint += first;
1082 for (i = 0; first + i < n; i++)
1083 if (constraint_type(constraint[i], pos) != type)
1089 /* Update "graft" based on "bounds" and "domain" for the generic,
1090 * non-degenerate, case.
1092 * "list" respresent the list of bounds that need to be encoded by
1093 * the for loop (or a guard around the for loop).
1094 * "domain" is the subset of the intersection of the constraints
1095 * for which some code is executed.
1096 * "build" is the build in which graft->node was created.
1098 * We separate lower bounds, upper bounds and constraints that
1099 * are independent of the loop iterator.
1101 * The actual for loop bounds are generated in refine_generic_bounds.
1102 * If there are any constraints that are independent of the loop iterator,
1103 * we need to put a guard around the for loop (which may get hoisted up
1104 * to higher levels) and we call refine_generic_bounds in a build
1105 * where this guard is enforced.
1107 static __isl_give isl_ast_graft *refine_generic_split(
1108 __isl_take isl_ast_graft *graft, __isl_keep isl_constraint_list *list,
1109 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1112 isl_ast_build *for_build;
1114 int n_indep, n_lower, n_upper;
1119 return isl_ast_graft_free(graft);
1121 pos = isl_ast_build_get_depth(build);
1123 if (isl_sort(list->p, list->n, sizeof(isl_constraint *),
1124 &cmp_constraint, &pos) < 0)
1125 return isl_ast_graft_free(graft);
1128 n_indep = count_constraints(list->p, n, 0, pos, 0);
1129 n_lower = count_constraints(list->p, n, n_indep, pos, 1);
1130 n_upper = count_constraints(list->p, n, n_indep + n_lower, pos, 2);
1133 return refine_generic_bounds(graft,
1134 list->p + n_indep, n_lower, n_upper, domain, build);
1136 ctx = isl_ast_graft_get_ctx(graft);
1137 guard = intersect_constraints(ctx, list->p, n_indep);
1139 for_build = isl_ast_build_copy(build);
1140 for_build = isl_ast_build_restrict_pending(for_build,
1141 isl_set_copy(guard));
1142 graft = refine_generic_bounds(graft,
1143 list->p + n_indep, n_lower, n_upper, domain, for_build);
1144 isl_ast_build_free(for_build);
1146 graft = isl_ast_graft_add_guard(graft, guard, build);
1151 /* Update "graft" based on "bounds" and "domain" for the generic,
1152 * non-degenerate, case.
1154 * "bounds" respresent the bounds that need to be encoded by
1155 * the for loop (or a guard around the for loop).
1156 * "domain" is the subset of "bounds" for which some code is executed.
1157 * "build" is the build in which graft->node was created.
1159 * We break up "bounds" into a list of constraints and continue with
1160 * refine_generic_split.
1162 static __isl_give isl_ast_graft *refine_generic(
1163 __isl_take isl_ast_graft *graft,
1164 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1165 __isl_keep isl_ast_build *build)
1167 isl_constraint_list *list;
1169 if (!build || !graft)
1170 return isl_ast_graft_free(graft);
1172 bounds = isl_basic_set_copy(bounds);
1173 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1174 list = isl_constraint_list_from_basic_set(bounds);
1176 graft = refine_generic_split(graft, list, domain, build);
1178 isl_constraint_list_free(list);
1182 /* Create a for node for the current level.
1184 * Mark the for node degenerate if "degenerate" is set.
1186 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1196 depth = isl_ast_build_get_depth(build);
1197 id = isl_ast_build_get_iterator_id(build, depth);
1198 node = isl_ast_node_alloc_for(id);
1200 node = isl_ast_node_for_mark_degenerate(node);
1205 /* Create an AST node for the current dimension based on
1206 * the schedule domain "bounds" and return the node encapsulated
1207 * in an isl_ast_graft.
1209 * "executed" is the current inverse schedule, taking into account
1210 * the bounds in "bounds"
1211 * "domain" is the domain of "executed", with inner dimensions projected out.
1212 * It may be a strict subset of "bounds" in case "bounds" was created
1213 * based on the atomic option or based on separation with explicit bounds.
1215 * "domain" may satisfy additional equalities that result
1216 * from intersecting "executed" with "bounds" in add_node.
1217 * It may also satisfy some global constraints that were dropped out because
1218 * we performed separation with explicit bounds.
1219 * The very first step is then to copy these constraints to "bounds".
1221 * Since we may be calling before_each_for and after_each_for
1222 * callbacks, we record the current inverse schedule in the build.
1224 * We consider three builds,
1225 * "build" is the one in which the current level is created,
1226 * "body_build" is the build in which the next level is created,
1227 * "sub_build" is essentially the same as "body_build", except that
1228 * the depth has not been increased yet.
1230 * "build" already contains information (in strides and offsets)
1231 * about the strides at the current level, but this information is not
1232 * reflected in the build->domain.
1233 * We first add this information and the "bounds" to the sub_build->domain.
1234 * isl_ast_build_set_loop_bounds checks whether the current dimension attains
1235 * only a single value and whether this single value can be represented using
1236 * a single affine expression.
1237 * In the first case, the current level is considered "degenerate".
1238 * In the second, sub-case, the current level is considered "eliminated".
1239 * Eliminated level don't need to be reflected in the AST since we can
1240 * simply plug in the affine expression. For degenerate, but non-eliminated,
1241 * levels, we do introduce a for node, but mark is as degenerate so that
1242 * it can be printed as an assignment of the single value to the loop
1245 * If the current level is eliminated, we explicitly plug in the value
1246 * for the current level found by isl_ast_build_set_loop_bounds in the
1247 * inverse schedule. This ensures that if we are working on a slice
1248 * of the domain based on information available in the inverse schedule
1249 * and the build domain, that then this information is also reflected
1250 * in the inverse schedule. This operation also eliminates the current
1251 * dimension from the inverse schedule making sure no inner dimensions depend
1252 * on the current dimension. Otherwise, we create a for node, marking
1253 * it degenerate if appropriate. The initial for node is still incomplete
1254 * and will be completed in either refine_degenerate or refine_generic.
1256 * We then generate a sequence of grafts for the next level,
1257 * create a surrounding graft for the current level and insert
1258 * the for node we created (if the current level is not eliminated).
1260 * Finally, we set the bounds of the for loop and insert guards
1261 * (either in the AST or in the graft) in one of
1262 * refine_eliminated, refine_degenerate or refine_generic.
1264 static __isl_give isl_ast_graft *create_node_scaled(
1265 __isl_take isl_union_map *executed,
1266 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1267 __isl_take isl_ast_build *build)
1270 int degenerate, eliminated;
1271 isl_basic_set *hull;
1272 isl_ast_node *node = NULL;
1273 isl_ast_graft *graft;
1274 isl_ast_graft_list *children;
1275 isl_ast_build *sub_build;
1276 isl_ast_build *body_build;
1278 domain = isl_ast_build_eliminate_divs(build, domain);
1279 domain = isl_set_detect_equalities(domain);
1280 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1281 bounds = isl_basic_set_intersect(bounds, hull);
1282 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1284 depth = isl_ast_build_get_depth(build);
1285 sub_build = isl_ast_build_copy(build);
1286 sub_build = isl_ast_build_include_stride(sub_build);
1287 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1288 isl_basic_set_copy(bounds));
1289 degenerate = isl_ast_build_has_value(sub_build);
1290 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1291 if (degenerate < 0 || eliminated < 0)
1292 executed = isl_union_map_free(executed);
1294 executed = plug_in_values(executed, sub_build);
1296 node = create_for(build, degenerate);
1298 body_build = isl_ast_build_copy(sub_build);
1299 body_build = isl_ast_build_increase_depth(body_build);
1301 node = before_each_for(node, body_build);
1302 children = generate_next_level(executed,
1303 isl_ast_build_copy(body_build));
1305 graft = isl_ast_graft_alloc_level(children, build, sub_build);
1307 graft = isl_ast_graft_insert_for(graft, node);
1309 graft = refine_eliminated(graft, bounds, build);
1310 else if (degenerate)
1311 graft = refine_degenerate(graft, bounds, build, sub_build);
1313 graft = refine_generic(graft, bounds, domain, build);
1315 graft = after_each_for(graft, body_build);
1317 isl_ast_build_free(body_build);
1318 isl_ast_build_free(sub_build);
1319 isl_ast_build_free(build);
1320 isl_basic_set_free(bounds);
1321 isl_set_free(domain);
1326 /* Internal data structure for checking if all constraints involving
1327 * the input dimension "depth" are such that the other coefficients
1328 * are multiples of "m", reducing "m" if they are not.
1329 * If "m" is reduced all the way down to "1", then the check has failed
1330 * and we break out of the iteration.
1331 * "d" is an initialized isl_int that can be used internally.
1333 struct isl_check_scaled_data {
1338 /* If constraint "c" involves the input dimension data->depth,
1339 * then make sure that all the other coefficients are multiples of data->m,
1340 * reducing data->m if needed.
1341 * Break out of the iteration if data->m has become equal to "1".
1343 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1345 struct isl_check_scaled_data *data = user;
1347 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1350 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1351 isl_constraint_free(c);
1355 for (i = 0; i < 4; ++i) {
1356 n = isl_constraint_dim(c, t[i]);
1357 for (j = 0; j < n; ++j) {
1358 if (t[i] == isl_dim_in && j == data->depth)
1360 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1362 isl_constraint_get_coefficient(c, t[i], j, &data->d);
1363 isl_int_gcd(data->m, data->m, data->d);
1364 if (isl_int_is_one(data->m))
1371 isl_constraint_free(c);
1373 return i < 4 ? -1 : 0;
1376 /* For each constraint of "bmap" that involves the input dimension data->depth,
1377 * make sure that all the other coefficients are multiples of data->m,
1378 * reducing data->m if needed.
1379 * Break out of the iteration if data->m has become equal to "1".
1381 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1385 r = isl_basic_map_foreach_constraint(bmap,
1386 &constraint_check_scaled, user);
1387 isl_basic_map_free(bmap);
1392 /* For each constraint of "map" that involves the input dimension data->depth,
1393 * make sure that all the other coefficients are multiples of data->m,
1394 * reducing data->m if needed.
1395 * Break out of the iteration if data->m has become equal to "1".
1397 static int map_check_scaled(__isl_take isl_map *map, void *user)
1401 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1407 /* Create an AST node for the current dimension based on
1408 * the schedule domain "bounds" and return the node encapsulated
1409 * in an isl_ast_graft.
1411 * "executed" is the current inverse schedule, taking into account
1412 * the bounds in "bounds"
1413 * "domain" is the domain of "executed", with inner dimensions projected out.
1416 * Before moving on to the actual AST node construction in create_node_scaled,
1417 * we first check if the current dimension is strided and if we can scale
1418 * down this stride. Note that we only do this if the ast_build_scale_strides
1421 * In particular, let the current dimension take on values
1425 * with a an integer. We check if we can find an integer m that (obviouly)
1426 * divides both f and s.
1428 * If so, we check if the current dimension only appears in constraints
1429 * where the coefficients of the other variables are multiples of m.
1430 * We perform this extra check to avoid the risk of introducing
1431 * divisions by scaling down the current dimension.
1433 * If so, we scale the current dimension down by a factor of m.
1434 * That is, we plug in
1438 * Note that in principle we could always scale down strided loops
1443 * but this may result in i' taking on larger values than the original i,
1444 * due to the shift by "f".
1445 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1447 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1448 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1449 __isl_take isl_ast_build *build)
1451 struct isl_check_scaled_data data;
1455 ctx = isl_ast_build_get_ctx(build);
1456 if (!isl_options_get_ast_build_scale_strides(ctx))
1457 return create_node_scaled(executed, bounds, domain, build);
1459 data.depth = isl_ast_build_get_depth(build);
1460 if (!isl_ast_build_has_stride(build, data.depth))
1461 return create_node_scaled(executed, bounds, domain, build);
1463 isl_int_init(data.m);
1464 isl_int_init(data.d);
1466 offset = isl_ast_build_get_offset(build, data.depth);
1467 if (isl_ast_build_get_stride(build, data.depth, &data.m) < 0)
1468 offset = isl_aff_free(offset);
1469 offset = isl_aff_scale_down(offset, data.m);
1470 if (isl_aff_get_denominator(offset, &data.d) < 0)
1471 executed = isl_union_map_free(executed);
1473 if (executed && isl_int_is_divisible_by(data.m, data.d))
1474 isl_int_divexact(data.m, data.m, data.d);
1476 isl_int_set_si(data.m, 1);
1478 if (!isl_int_is_one(data.m)) {
1479 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1481 !isl_int_is_one(data.m))
1482 executed = isl_union_map_free(executed);
1485 if (!isl_int_is_one(data.m)) {
1490 isl_union_map *umap;
1492 space = isl_ast_build_get_space(build, 1);
1493 space = isl_space_map_from_set(space);
1494 ma = isl_multi_aff_identity(space);
1495 aff = isl_multi_aff_get_aff(ma, data.depth);
1496 aff = isl_aff_scale(aff, data.m);
1497 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1499 bounds = isl_basic_set_preimage_multi_aff(bounds,
1500 isl_multi_aff_copy(ma));
1501 domain = isl_set_preimage_multi_aff(domain,
1502 isl_multi_aff_copy(ma));
1503 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1504 umap = isl_union_map_from_map(map);
1505 executed = isl_union_map_apply_domain(executed,
1506 isl_union_map_copy(umap));
1507 build = isl_ast_build_scale_down(build, data.m, umap);
1509 isl_aff_free(offset);
1511 isl_int_clear(data.d);
1512 isl_int_clear(data.m);
1514 return create_node_scaled(executed, bounds, domain, build);
1517 /* Add the basic set to the list that "user" points to.
1519 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1521 isl_basic_set_list **list = user;
1523 *list = isl_basic_set_list_add(*list, bset);
1528 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1530 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1531 __isl_take isl_set *set)
1535 isl_basic_set_list *list;
1540 ctx = isl_set_get_ctx(set);
1542 n = isl_set_n_basic_set(set);
1543 list = isl_basic_set_list_alloc(ctx, n);
1544 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1545 list = isl_basic_set_list_free(list);
1551 /* Generate code for the schedule domain "bounds"
1552 * and add the result to "list".
1554 * We mainly detect strides and additional equalities here
1555 * and then pass over control to create_node.
1557 * "bounds" reflects the bounds on the current dimension and possibly
1558 * some extra conditions on outer dimensions.
1559 * It does not, however, include any divs involving the current dimension,
1560 * so it does not capture any stride constraints.
1561 * We therefore need to compute that part of the schedule domain that
1562 * intersects with "bounds" and derive the strides from the result.
1564 static __isl_give isl_ast_graft_list *add_node(
1565 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1566 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1568 isl_ast_graft *graft;
1569 isl_set *domain = NULL;
1570 isl_union_set *uset;
1573 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1574 executed = isl_union_map_intersect_domain(executed, uset);
1575 empty = isl_union_map_is_empty(executed);
1581 uset = isl_union_map_domain(isl_union_map_copy(executed));
1582 domain = isl_set_from_union_set(uset);
1583 domain = isl_ast_build_compute_gist(build, domain);
1584 empty = isl_set_is_empty(domain);
1590 domain = isl_ast_build_eliminate_inner(build, domain);
1591 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1593 graft = create_node(executed, bounds, domain,
1594 isl_ast_build_copy(build));
1595 list = isl_ast_graft_list_add(list, graft);
1596 isl_ast_build_free(build);
1599 list = isl_ast_graft_list_free(list);
1601 isl_set_free(domain);
1602 isl_basic_set_free(bounds);
1603 isl_union_map_free(executed);
1604 isl_ast_build_free(build);
1608 struct isl_domain_follows_at_depth_data {
1610 isl_basic_set **piece;
1613 /* Does any element of i follow or coincide with any element of j
1614 * at the current depth (data->depth) for equal values of the outer
1617 static int domain_follows_at_depth(int i, int j, void *user)
1619 struct isl_domain_follows_at_depth_data *data = user;
1620 isl_basic_map *test;
1624 test = isl_basic_map_from_domain_and_range(
1625 isl_basic_set_copy(data->piece[i]),
1626 isl_basic_set_copy(data->piece[j]));
1627 for (l = 0; l < data->depth; ++l)
1628 test = isl_basic_map_equate(test, isl_dim_in, l,
1630 test = isl_basic_map_order_ge(test, isl_dim_in, data->depth,
1631 isl_dim_out, data->depth);
1632 empty = isl_basic_map_is_empty(test);
1633 isl_basic_map_free(test);
1635 return empty < 0 ? -1 : !empty;
1638 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1639 __isl_keep isl_basic_set_list *domain_list,
1640 __isl_keep isl_union_map *executed,
1641 __isl_keep isl_ast_build *build);
1643 /* Generate code for the "n" schedule domains in "domain_list"
1644 * with positions specified by the entries of the "pos" array
1645 * and add the results to "list".
1647 * The "n" domains form a strongly connected component in the ordering.
1648 * If n is larger than 1, then this means that we cannot determine a valid
1649 * ordering for the n domains in the component. This should be fairly
1650 * rare because the individual domains have been made disjoint first.
1651 * The problem is that the domains may be integrally disjoint but not
1652 * rationally disjoint. For example, we may have domains
1654 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1656 * These two domains have an empty intersection, but their rational
1657 * relaxations do intersect. It is impossible to order these domains
1658 * in the second dimension because the first should be ordered before
1659 * the second for outer dimension equal to 0, while it should be ordered
1660 * after for outer dimension equal to 1.
1662 * This may happen in particular in case of unrolling since the domain
1663 * of each slice is replaced by its simple hull.
1665 * We collect the basic sets in the component, call isl_set_make_disjoint
1666 * and try again. Note that we rely here on isl_set_make_disjoint also
1667 * making the basic sets rationally disjoint. If the basic sets
1668 * are rationally disjoint, then the ordering problem does not occur.
1669 * To see this, there can only be a problem if there are points
1670 * (i,a) and (j,b) in one set and (i,c) and (j,d) in the other with
1671 * a < c and b > d. This means that either the interval spanned
1672 * by a en b lies inside that spanned by c and or the other way around.
1673 * In either case, there is a point inside both intervals with the
1674 * convex combination in terms of a and b and in terms of c and d.
1675 * Taking the same combination of i and j gives a point in the intersection.
1677 static __isl_give isl_ast_graft_list *add_nodes(
1678 __isl_take isl_ast_graft_list *list, int *pos, int n,
1679 __isl_keep isl_basic_set_list *domain_list,
1680 __isl_keep isl_union_map *executed,
1681 __isl_keep isl_ast_build *build)
1684 isl_basic_set *bset;
1687 bset = isl_basic_set_list_get_basic_set(domain_list, pos[0]);
1689 return add_node(list, isl_union_map_copy(executed), bset,
1690 isl_ast_build_copy(build));
1692 set = isl_set_from_basic_set(bset);
1693 for (i = 1; i < n; ++i) {
1694 bset = isl_basic_set_list_get_basic_set(domain_list, pos[i]);
1695 set = isl_set_union(set, isl_set_from_basic_set(bset));
1698 set = isl_set_make_disjoint(set);
1699 if (isl_set_n_basic_set(set) == n)
1700 isl_die(isl_ast_graft_list_get_ctx(list), isl_error_internal,
1701 "unable to separate loop parts", goto error);
1702 domain_list = isl_basic_set_list_from_set(set);
1703 list = isl_ast_graft_list_concat(list,
1704 generate_sorted_domains(domain_list, executed, build));
1705 isl_basic_set_list_free(domain_list);
1710 return isl_ast_graft_list_free(list);
1713 /* Sort the domains in "domain_list" according to the execution order
1714 * at the current depth (for equal values of the outer dimensions),
1715 * generate code for each of them, collecting the results in a list.
1716 * If no code is generated (because the intersection of the inverse schedule
1717 * with the domains turns out to be empty), then an empty list is returned.
1719 * The caller is responsible for ensuring that the basic sets in "domain_list"
1720 * are pair-wise disjoint. It can, however, in principle happen that
1721 * two basic sets should be ordered one way for one value of the outer
1722 * dimensions and the other way for some other value of the outer dimensions.
1723 * We therefore play safe and look for strongly connected components.
1724 * The function add_nodes takes care of handling non-trivial components.
1726 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1727 __isl_keep isl_basic_set_list *domain_list,
1728 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1731 isl_ast_graft_list *list;
1732 struct isl_domain_follows_at_depth_data data;
1733 struct isl_tarjan_graph *g;
1739 ctx = isl_basic_set_list_get_ctx(domain_list);
1740 n = isl_basic_set_list_n_basic_set(domain_list);
1741 list = isl_ast_graft_list_alloc(ctx, n);
1745 return add_node(list, isl_union_map_copy(executed),
1746 isl_basic_set_list_get_basic_set(domain_list, 0),
1747 isl_ast_build_copy(build));
1749 data.depth = isl_ast_build_get_depth(build);
1750 data.piece = domain_list->p;
1751 g = isl_tarjan_graph_init(ctx, n, &domain_follows_at_depth, &data);
1759 if (g->order[i] == -1)
1760 isl_die(ctx, isl_error_internal, "cannot happen",
1763 while (g->order[i] != -1) {
1766 list = add_nodes(list, g->order + first, i - first,
1767 domain_list, executed, build);
1772 error: list = isl_ast_graft_list_free(list);
1773 isl_tarjan_graph_free(g);
1778 struct isl_shared_outer_data {
1780 isl_basic_set **piece;
1783 /* Do elements i and j share any values for the outer dimensions?
1785 static int shared_outer(int i, int j, void *user)
1787 struct isl_shared_outer_data *data = user;
1788 isl_basic_map *test;
1792 test = isl_basic_map_from_domain_and_range(
1793 isl_basic_set_copy(data->piece[i]),
1794 isl_basic_set_copy(data->piece[j]));
1795 for (l = 0; l < data->depth; ++l)
1796 test = isl_basic_map_equate(test, isl_dim_in, l,
1798 empty = isl_basic_map_is_empty(test);
1799 isl_basic_map_free(test);
1801 return empty < 0 ? -1 : !empty;
1804 /* Call generate_sorted_domains on a list containing the elements
1805 * of "domain_list indexed by the first "n" elements of "pos".
1807 static __isl_give isl_ast_graft_list *generate_sorted_domains_part(
1808 __isl_keep isl_basic_set_list *domain_list, int *pos, int n,
1809 __isl_keep isl_union_map *executed,
1810 __isl_keep isl_ast_build *build)
1814 isl_basic_set_list *slice;
1815 isl_ast_graft_list *list;
1817 ctx = isl_ast_build_get_ctx(build);
1818 slice = isl_basic_set_list_alloc(ctx, n);
1819 for (i = 0; i < n; ++i) {
1820 isl_basic_set *bset;
1822 bset = isl_basic_set_copy(domain_list->p[pos[i]]);
1823 slice = isl_basic_set_list_add(slice, bset);
1826 list = generate_sorted_domains(slice, executed, build);
1827 isl_basic_set_list_free(slice);
1832 /* Look for any (weakly connected) components in the "domain_list"
1833 * of domains that share some values of the outer dimensions.
1834 * That is, domains in different components do not share any values
1835 * of the outer dimensions. This means that these components
1836 * can be freely reordered.
1837 * Within each of the components, we sort the domains according
1838 * to the execution order at the current depth.
1840 * We fuse the result of each call to generate_sorted_domains_part
1841 * into a list with either zero or one graft and collect these (at most)
1842 * single element lists into a bigger list. This means that the elements of the
1843 * final list can be freely reordered. In particular, we sort them
1844 * according to an arbitrary but fixed ordering to ease merging of
1845 * graft lists from different components.
1847 static __isl_give isl_ast_graft_list *generate_parallel_domains(
1848 __isl_keep isl_basic_set_list *domain_list,
1849 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1853 isl_ast_graft_list *list;
1854 struct isl_shared_outer_data data;
1855 struct isl_tarjan_graph *g;
1860 n = isl_basic_set_list_n_basic_set(domain_list);
1862 return generate_sorted_domains(domain_list, executed, build);
1864 ctx = isl_basic_set_list_get_ctx(domain_list);
1866 data.depth = isl_ast_build_get_depth(build);
1867 data.piece = domain_list->p;
1868 g = isl_tarjan_graph_init(ctx, n, &shared_outer, &data);
1875 isl_ast_graft_list *list_c;
1877 if (g->order[i] == -1)
1878 isl_die(ctx, isl_error_internal, "cannot happen",
1881 while (g->order[i] != -1) {
1884 if (first == 0 && n == 0) {
1885 isl_tarjan_graph_free(g);
1886 return generate_sorted_domains(domain_list,
1889 list_c = generate_sorted_domains_part(domain_list,
1890 g->order + first, i - first, executed, build);
1891 list_c = isl_ast_graft_list_fuse(list_c, build);
1895 list = isl_ast_graft_list_concat(list, list_c);
1897 } while (list && n);
1900 list = isl_ast_graft_list_free(list);
1902 list = isl_ast_graft_list_sort_guard(list);
1904 isl_tarjan_graph_free(g);
1909 /* Internal data for separate_domain.
1911 * "explicit" is set if we only want to use explicit bounds.
1913 * "domain" collects the separated domains.
1915 struct isl_separate_domain_data {
1916 isl_ast_build *build;
1921 /* Extract implicit bounds on the current dimension for the executed "map".
1923 * The domain of "map" may involve inner dimensions, so we
1924 * need to eliminate them.
1926 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
1927 __isl_keep isl_ast_build *build)
1931 domain = isl_map_domain(map);
1932 domain = isl_ast_build_eliminate(build, domain);
1937 /* Extract explicit bounds on the current dimension for the executed "map".
1939 * Rather than eliminating the inner dimensions as in implicit_bounds,
1940 * we simply drop any constraints involving those inner dimensions.
1941 * The idea is that most bounds that are implied by constraints on the
1942 * inner dimensions will be enforced by for loops and not by explicit guards.
1943 * There is then no need to separate along those bounds.
1945 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
1946 __isl_keep isl_ast_build *build)
1951 dim = isl_map_dim(map, isl_dim_out);
1952 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
1954 domain = isl_map_domain(map);
1955 depth = isl_ast_build_get_depth(build);
1956 dim = isl_set_dim(domain, isl_dim_set);
1957 domain = isl_set_detect_equalities(domain);
1958 domain = isl_set_drop_constraints_involving_dims(domain,
1959 isl_dim_set, depth + 1, dim - (depth + 1));
1960 domain = isl_set_remove_divs_involving_dims(domain,
1961 isl_dim_set, depth, 1);
1962 domain = isl_set_remove_unknown_divs(domain);
1967 /* Split data->domain into pieces that intersect with the range of "map"
1968 * and pieces that do not intersect with the range of "map"
1969 * and then add that part of the range of "map" that does not intersect
1970 * with data->domain.
1972 static int separate_domain(__isl_take isl_map *map, void *user)
1974 struct isl_separate_domain_data *data = user;
1979 domain = explicit_bounds(map, data->build);
1981 domain = implicit_bounds(map, data->build);
1983 domain = isl_set_coalesce(domain);
1984 domain = isl_set_make_disjoint(domain);
1985 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
1986 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
1987 data->domain = isl_set_intersect(data->domain, domain);
1988 data->domain = isl_set_union(data->domain, d1);
1989 data->domain = isl_set_union(data->domain, d2);
1994 /* Separate the schedule domains of "executed".
1996 * That is, break up the domain of "executed" into basic sets,
1997 * such that for each basic set S, every element in S is associated with
1998 * the same domain spaces.
2000 * "space" is the (single) domain space of "executed".
2002 static __isl_give isl_set *separate_schedule_domains(
2003 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2004 __isl_keep isl_ast_build *build)
2006 struct isl_separate_domain_data data = { build };
2009 ctx = isl_ast_build_get_ctx(build);
2010 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2011 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2012 data.domain = isl_set_empty(space);
2013 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2014 data.domain = isl_set_free(data.domain);
2016 isl_union_map_free(executed);
2020 /* Temporary data used during the search for a lower bound for unrolling.
2022 * "domain" is the original set for which to find a lower bound
2023 * "depth" is the dimension for which to find a lower boudn
2025 * "lower" is the best lower bound found so far. It is NULL if we have not
2027 * "n" is the corresponding size. If lower is NULL, then the value of n
2030 * "tmp" is a temporary initialized isl_int.
2032 struct isl_find_unroll_data {
2041 /* Check if we can use "c" as a lower bound and if it is better than
2042 * any previously found lower bound.
2044 * If "c" does not involve the dimension at the current depth,
2045 * then we cannot use it.
2046 * Otherwise, let "c" be of the form
2050 * We compute the maximal value of
2052 * -ceil(f(j)/a)) + i + 1
2054 * over the domain. If there is such a value "n", then we know
2056 * -ceil(f(j)/a)) + i + 1 <= n
2060 * i < ceil(f(j)/a)) + n
2062 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2063 * We just need to check if we have found any lower bound before and
2064 * if the new lower bound is better (smaller n) than the previously found
2067 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2068 __isl_keep isl_constraint *c)
2070 isl_aff *aff, *lower;
2071 enum isl_lp_result res;
2073 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2076 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2077 lower = isl_aff_ceil(lower);
2078 aff = isl_aff_copy(lower);
2079 aff = isl_aff_neg(aff);
2080 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2081 aff = isl_aff_add_constant_si(aff, 1);
2082 res = isl_set_max(data->domain, aff, &data->tmp);
2085 if (res == isl_lp_error)
2087 if (res == isl_lp_unbounded) {
2088 isl_aff_free(lower);
2092 if (isl_int_cmp_si(data->tmp, INT_MAX) <= 0 &&
2093 (!data->lower || isl_int_cmp_si(data->tmp, *data->n) < 0)) {
2094 isl_aff_free(data->lower);
2095 data->lower = lower;
2096 *data->n = isl_int_get_si(data->tmp);
2098 isl_aff_free(lower);
2102 isl_aff_free(lower);
2106 /* Check if we can use "c" as a lower bound and if it is better than
2107 * any previously found lower bound.
2109 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2111 struct isl_find_unroll_data *data;
2114 data = (struct isl_find_unroll_data *) user;
2115 r = update_unrolling_lower_bound(data, c);
2116 isl_constraint_free(c);
2121 /* Look for a lower bound l(i) on the dimension at "depth"
2122 * and a size n such that "domain" is a subset of
2124 * { [i] : l(i) <= i_d < l(i) + n }
2126 * where d is "depth" and l(i) depends only on earlier dimensions.
2127 * Furthermore, try and find a lower bound such that n is as small as possible.
2128 * In particular, "n" needs to be finite.
2130 * Inner dimensions have been eliminated from "domain" by the caller.
2132 * We first construct a collection of lower bounds on the input set
2133 * by computing its simple hull. We then iterate through them,
2134 * discarding those that we cannot use (either because they do not
2135 * involve the dimension at "depth" or because they have no corresponding
2136 * upper bound, meaning that "n" would be unbounded) and pick out the
2137 * best from the remaining ones.
2139 * If we cannot find a suitable lower bound, then we consider that
2142 static __isl_give isl_aff *find_unroll_lower_bound(__isl_keep isl_set *domain,
2145 struct isl_find_unroll_data data = { domain, depth, NULL, n };
2146 isl_basic_set *hull;
2148 isl_int_init(data.tmp);
2149 hull = isl_set_simple_hull(isl_set_copy(domain));
2151 if (isl_basic_set_foreach_constraint(hull,
2152 &constraint_find_unroll, &data) < 0)
2155 isl_basic_set_free(hull);
2156 isl_int_clear(data.tmp);
2159 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2160 "cannot find lower bound for unrolling", return NULL);
2164 isl_basic_set_free(hull);
2165 isl_int_clear(data.tmp);
2166 return isl_aff_free(data.lower);
2169 /* Return the constraint
2171 * i_"depth" = aff + offset
2173 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2176 aff = isl_aff_copy(aff);
2177 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2178 aff = isl_aff_add_constant_si(aff, offset);
2179 return isl_equality_from_aff(aff);
2182 /* Return a list of basic sets, one for each value of the current dimension
2184 * The divs that involve the current dimension have not been projected out
2187 * Since we are going to be iterating over the individual values,
2188 * we first check if there are any strides on the current dimension.
2189 * If there is, we rewrite the current dimension i as
2191 * i = stride i' + offset
2193 * and then iterate over individual values of i' instead.
2195 * We then look for a lower bound on i' and a size such that the domain
2198 * { [j,i'] : l(j) <= i' < l(j) + n }
2200 * and then take slices of the domain at values of i'
2201 * between l(j) and l(j) + n - 1.
2203 * We compute the unshifted simple hull of each slice to ensure that
2204 * we have a single basic set per offset. The slicing constraint
2205 * may get simplified away before the unshifted simple hull is taken
2206 * and may therefore in some rare cases disappear from the result.
2207 * We therefore explicitly add the constraint back after computing
2208 * the unshifted simple hull to ensure that the basic sets
2209 * remain disjoint. The constraints that are dropped by taking the hull
2210 * will be taken into account at the next level, as in the case of the
2213 * Finally, we map i' back to i and add each basic set to the list.
2215 static __isl_give isl_basic_set_list *do_unroll(__isl_take isl_set *domain,
2216 __isl_keep isl_ast_build *build)
2222 isl_basic_set_list *list;
2223 isl_multi_aff *expansion;
2224 isl_basic_map *bmap;
2229 ctx = isl_set_get_ctx(domain);
2230 depth = isl_ast_build_get_depth(build);
2231 build = isl_ast_build_copy(build);
2232 domain = isl_ast_build_eliminate_inner(build, domain);
2233 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
2234 expansion = isl_ast_build_get_stride_expansion(build);
2236 domain = isl_set_preimage_multi_aff(domain,
2237 isl_multi_aff_copy(expansion));
2238 domain = isl_ast_build_eliminate_divs(build, domain);
2240 isl_ast_build_free(build);
2242 list = isl_basic_set_list_alloc(ctx, 0);
2244 lower = find_unroll_lower_bound(domain, depth, &n);
2246 list = isl_basic_set_list_free(list);
2248 bmap = isl_basic_map_from_multi_aff(expansion);
2250 for (i = 0; list && i < n; ++i) {
2252 isl_basic_set *bset;
2253 isl_constraint *slice;
2255 slice = at_offset(depth, lower, i);
2256 set = isl_set_copy(domain);
2257 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2258 bset = isl_set_unshifted_simple_hull(set);
2259 bset = isl_basic_set_add_constraint(bset, slice);
2260 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2261 list = isl_basic_set_list_add(list, bset);
2264 isl_aff_free(lower);
2265 isl_set_free(domain);
2266 isl_basic_map_free(bmap);
2271 /* Data structure for storing the results and the intermediate objects
2272 * of compute_domains.
2274 * "list" is the main result of the function and contains a list
2275 * of disjoint basic sets for which code should be generated.
2277 * "executed" and "build" are inputs to compute_domains.
2278 * "schedule_domain" is the domain of "executed".
2280 * "option" constains the domains at the current depth that should by
2281 * atomic, separated or unrolled. These domains are as specified by
2282 * the user, except that inner dimensions have been eliminated and
2283 * that they have been made pair-wise disjoint.
2285 * "sep_class" contains the user-specified split into separation classes
2286 * specialized to the current depth.
2287 * "done" contains the union of the separation domains that have already
2289 * "atomic" contains the domain that has effectively been made atomic.
2290 * This domain may be larger than the intersection of option[atomic]
2291 * and the schedule domain.
2293 struct isl_codegen_domains {
2294 isl_basic_set_list *list;
2296 isl_union_map *executed;
2297 isl_ast_build *build;
2298 isl_set *schedule_domain;
2307 /* Add domains to domains->list for each individual value of the current
2308 * dimension, for that part of the schedule domain that lies in the
2309 * intersection of the option domain and the class domain.
2311 * "domain" is the intersection of the class domain and the schedule domain.
2312 * The divs that involve the current dimension have not been projected out
2315 * We first break up the unroll option domain into individual pieces
2316 * and then handle each of them separately. The unroll option domain
2317 * has been made disjoint in compute_domains_init_options,
2319 * Note that we actively want to combine different pieces of the
2320 * schedule domain that have the same value at the current dimension.
2321 * We therefore need to break up the unroll option domain before
2322 * intersecting with class and schedule domain, hoping that the
2323 * unroll option domain specified by the user is relatively simple.
2325 static int compute_unroll_domains(struct isl_codegen_domains *domains,
2326 __isl_keep isl_set *domain)
2328 isl_set *unroll_domain;
2329 isl_basic_set_list *unroll_list;
2333 empty = isl_set_is_empty(domains->option[unroll]);
2339 unroll_domain = isl_set_copy(domains->option[unroll]);
2340 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2342 n = isl_basic_set_list_n_basic_set(unroll_list);
2343 for (i = 0; i < n; ++i) {
2344 isl_basic_set *bset;
2345 isl_basic_set_list *list;
2347 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2348 unroll_domain = isl_set_from_basic_set(bset);
2349 unroll_domain = isl_set_intersect(unroll_domain,
2350 isl_set_copy(domain));
2352 empty = isl_set_is_empty(unroll_domain);
2353 if (empty >= 0 && empty) {
2354 isl_set_free(unroll_domain);
2358 list = do_unroll(unroll_domain, domains->build);
2359 domains->list = isl_basic_set_list_concat(domains->list, list);
2362 isl_basic_set_list_free(unroll_list);
2367 /* Construct a single basic set that includes the intersection of
2368 * the schedule domain, the atomic option domain and the class domain.
2369 * Add the resulting basic set to domains->list and save a copy
2370 * in domains->atomic for use in compute_partial_domains.
2372 * We construct a single domain rather than trying to combine
2373 * the schedule domains of individual domains because we are working
2374 * within a single component so that non-overlapping schedule domains
2375 * should already have been separated.
2376 * Note, though, that this does not take into account the class domain.
2377 * So, it is possible for a class domain to carve out a piece of the
2378 * schedule domain with independent pieces and then we would only
2379 * generate a single domain for them. If this proves to be problematic
2380 * for some users, then this function will have to be adjusted.
2382 * "domain" is the intersection of the schedule domain and the class domain,
2383 * with inner dimensions projected out.
2385 static int compute_atomic_domain(struct isl_codegen_domains *domains,
2386 __isl_keep isl_set *domain)
2388 isl_basic_set *bset;
2389 isl_set *atomic_domain;
2392 atomic_domain = isl_set_copy(domains->option[atomic]);
2393 atomic_domain = isl_set_intersect(atomic_domain, isl_set_copy(domain));
2394 empty = isl_set_is_empty(atomic_domain);
2395 if (empty < 0 || empty) {
2396 domains->atomic = atomic_domain;
2397 return empty < 0 ? -1 : 0;
2400 atomic_domain = isl_set_coalesce(atomic_domain);
2401 bset = isl_set_unshifted_simple_hull(atomic_domain);
2402 domains->atomic = isl_set_from_basic_set(isl_basic_set_copy(bset));
2403 domains->list = isl_basic_set_list_add(domains->list, bset);
2408 /* Split up the schedule domain into uniform basic sets,
2409 * in the sense that each element in a basic set is associated to
2410 * elements of the same domains, and add the result to domains->list.
2411 * Do this for that part of the schedule domain that lies in the
2412 * intersection of "class_domain" and the separate option domain.
2414 * "class_domain" may or may not include the constraints
2415 * of the schedule domain, but this does not make a difference
2416 * since we are going to intersect it with the domain of the inverse schedule.
2417 * If it includes schedule domain constraints, then they may involve
2418 * inner dimensions, but we will eliminate them in separation_domain.
2420 static int compute_separate_domain(struct isl_codegen_domains *domains,
2421 __isl_keep isl_set *class_domain)
2425 isl_union_map *executed;
2426 isl_basic_set_list *list;
2429 domain = isl_set_copy(domains->option[separate]);
2430 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2431 executed = isl_union_map_copy(domains->executed);
2432 executed = isl_union_map_intersect_domain(executed,
2433 isl_union_set_from_set(domain));
2434 empty = isl_union_map_is_empty(executed);
2435 if (empty < 0 || empty) {
2436 isl_union_map_free(executed);
2437 return empty < 0 ? -1 : 0;
2440 space = isl_set_get_space(class_domain);
2441 domain = separate_schedule_domains(space, executed, domains->build);
2443 list = isl_basic_set_list_from_set(domain);
2444 domains->list = isl_basic_set_list_concat(domains->list, list);
2449 /* Split up the domain at the current depth into disjoint
2450 * basic sets for which code should be generated separately
2451 * for the given separation class domain.
2453 * If any separation classes have been defined, then "class_domain"
2454 * is the domain of the current class and does not refer to inner dimensions.
2455 * Otherwise, "class_domain" is the universe domain.
2457 * We first make sure that the class domain is disjoint from
2458 * previously considered class domains.
2460 * The separate domains can be computed directly from the "class_domain".
2462 * The unroll, atomic and remainder domains need the constraints
2463 * from the schedule domain.
2465 * For unrolling, the actual schedule domain is needed (with divs that
2466 * may refer to the current dimension) so that stride detection can be
2469 * For atomic and remainder domains, inner dimensions and divs involving
2470 * the current dimensions should be eliminated.
2471 * In case we are working within a separation class, we need to intersect
2472 * the result with the current "class_domain" to ensure that the domains
2473 * are disjoint from those generated from other class domains.
2475 * The domain that has been made atomic may be larger than specified
2476 * by the user since it needs to be representable as a single basic set.
2477 * This possibly larger domain is stored in domains->atomic by
2478 * compute_atomic_domain.
2480 * If anything is left after handling separate, unroll and atomic,
2481 * we split it up into basic sets and append the basic sets to domains->list.
2483 static int compute_partial_domains(struct isl_codegen_domains *domains,
2484 __isl_take isl_set *class_domain)
2486 isl_basic_set_list *list;
2489 class_domain = isl_set_subtract(class_domain,
2490 isl_set_copy(domains->done));
2491 domains->done = isl_set_union(domains->done,
2492 isl_set_copy(class_domain));
2494 domain = isl_set_copy(class_domain);
2496 if (compute_separate_domain(domains, domain) < 0)
2498 domain = isl_set_subtract(domain,
2499 isl_set_copy(domains->option[separate]));
2501 domain = isl_set_intersect(domain,
2502 isl_set_copy(domains->schedule_domain));
2504 if (compute_unroll_domains(domains, domain) < 0)
2506 domain = isl_set_subtract(domain,
2507 isl_set_copy(domains->option[unroll]));
2509 domain = isl_ast_build_eliminate(domains->build, domain);
2510 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2512 if (compute_atomic_domain(domains, domain) < 0)
2513 domain = isl_set_free(domain);
2514 domain = isl_set_subtract(domain, domains->atomic);
2516 domain = isl_set_coalesce(domain);
2517 domain = isl_set_make_disjoint(domain);
2519 list = isl_basic_set_list_from_set(domain);
2520 domains->list = isl_basic_set_list_concat(domains->list, list);
2522 isl_set_free(class_domain);
2526 isl_set_free(domain);
2527 isl_set_free(class_domain);
2531 /* Split up the domain at the current depth into disjoint
2532 * basic sets for which code should be generated separately
2533 * for the separation class identified by "pnt".
2535 * We extract the corresponding class domain from domains->sep_class,
2536 * eliminate inner dimensions and pass control to compute_partial_domains.
2538 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2540 struct isl_codegen_domains *domains = user;
2545 class_set = isl_set_from_point(pnt);
2546 domain = isl_map_domain(isl_map_intersect_range(
2547 isl_map_copy(domains->sep_class), class_set));
2548 domain = isl_ast_build_compute_gist(domains->build, domain);
2549 domain = isl_ast_build_eliminate(domains->build, domain);
2551 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2555 isl_set_free(domain);
2559 return compute_partial_domains(domains, domain);
2562 /* Extract the domains at the current depth that should be atomic,
2563 * separated or unrolled and store them in option.
2565 * The domains specified by the user might overlap, so we make
2566 * them disjoint by subtracting earlier domains from later domains.
2568 static void compute_domains_init_options(isl_set *option[3],
2569 __isl_keep isl_ast_build *build)
2571 enum isl_ast_build_domain_type type, type2;
2573 for (type = atomic; type <= separate; ++type) {
2574 option[type] = isl_ast_build_get_option_domain(build, type);
2575 for (type2 = atomic; type2 < type; ++type2)
2576 option[type] = isl_set_subtract(option[type],
2577 isl_set_copy(option[type2]));
2580 option[unroll] = isl_set_coalesce(option[unroll]);
2581 option[unroll] = isl_set_make_disjoint(option[unroll]);
2584 /* Split up the domain at the current depth into disjoint
2585 * basic sets for which code should be generated separately,
2586 * based on the user-specified options.
2587 * Return the list of disjoint basic sets.
2589 * There are three kinds of domains that we need to keep track of.
2590 * - the "schedule domain" is the domain of "executed"
2591 * - the "class domain" is the domain corresponding to the currrent
2593 * - the "option domain" is the domain corresponding to one of the options
2594 * atomic, unroll or separate
2596 * We first consider the individial values of the separation classes
2597 * and split up the domain for each of them separately.
2598 * Finally, we consider the remainder. If no separation classes were
2599 * specified, then we call compute_partial_domains with the universe
2600 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2601 * with inner dimensions removed. We do this because we want to
2602 * avoid computing the complement of the class domains (i.e., the difference
2603 * between the universe and domains->done).
2605 static __isl_give isl_basic_set_list *compute_domains(
2606 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2608 struct isl_codegen_domains domains;
2611 isl_union_set *schedule_domain;
2615 enum isl_ast_build_domain_type type;
2621 ctx = isl_union_map_get_ctx(executed);
2622 domains.list = isl_basic_set_list_alloc(ctx, 0);
2624 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
2625 domain = isl_set_from_union_set(schedule_domain);
2627 compute_domains_init_options(domains.option, build);
2629 domains.sep_class = isl_ast_build_get_separation_class(build);
2630 classes = isl_map_range(isl_map_copy(domains.sep_class));
2631 n_param = isl_set_dim(classes, isl_dim_param);
2632 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
2634 space = isl_set_get_space(domain);
2635 domains.build = build;
2636 domains.schedule_domain = isl_set_copy(domain);
2637 domains.executed = executed;
2638 domains.done = isl_set_empty(space);
2640 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
2641 domains.list = isl_basic_set_list_free(domains.list);
2642 isl_set_free(classes);
2644 empty = isl_set_is_empty(domains.done);
2646 domains.list = isl_basic_set_list_free(domains.list);
2647 domain = isl_set_free(domain);
2649 isl_set_free(domain);
2650 domain = isl_set_universe(isl_set_get_space(domains.done));
2652 domain = isl_ast_build_eliminate(build, domain);
2654 if (compute_partial_domains(&domains, domain) < 0)
2655 domains.list = isl_basic_set_list_free(domains.list);
2657 isl_set_free(domains.schedule_domain);
2658 isl_set_free(domains.done);
2659 isl_map_free(domains.sep_class);
2660 for (type = atomic; type <= separate; ++type)
2661 isl_set_free(domains.option[type]);
2663 return domains.list;
2666 /* Generate code for a single component, after shifting (if any)
2669 * We first split up the domain at the current depth into disjoint
2670 * basic sets based on the user-specified options.
2671 * Then we generated code for each of them and concatenate the results.
2673 static __isl_give isl_ast_graft_list *generate_shifted_component(
2674 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
2676 isl_basic_set_list *domain_list;
2677 isl_ast_graft_list *list = NULL;
2679 domain_list = compute_domains(executed, build);
2680 list = generate_parallel_domains(domain_list, executed, build);
2682 isl_basic_set_list_free(domain_list);
2683 isl_union_map_free(executed);
2684 isl_ast_build_free(build);
2689 struct isl_set_map_pair {
2694 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2695 * of indices into the "domain" array,
2696 * return the union of the "map" fields of the elements
2697 * indexed by the first "n" elements of "order".
2699 static __isl_give isl_union_map *construct_component_executed(
2700 struct isl_set_map_pair *domain, int *order, int n)
2704 isl_union_map *executed;
2706 map = isl_map_copy(domain[order[0]].map);
2707 executed = isl_union_map_from_map(map);
2708 for (i = 1; i < n; ++i) {
2709 map = isl_map_copy(domain[order[i]].map);
2710 executed = isl_union_map_add_map(executed, map);
2716 /* Generate code for a single component, after shifting (if any)
2719 * The component inverse schedule is specified as the "map" fields
2720 * of the elements of "domain" indexed by the first "n" elements of "order".
2722 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
2723 struct isl_set_map_pair *domain, int *order, int n,
2724 __isl_take isl_ast_build *build)
2726 isl_union_map *executed;
2728 executed = construct_component_executed(domain, order, n);
2729 return generate_shifted_component(executed, build);
2732 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2733 * of indices into the "domain" array,
2734 * do all (except for at most one) of the "set" field of the elements
2735 * indexed by the first "n" elements of "order" have a fixed value
2736 * at position "depth"?
2738 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
2739 int *order, int n, int depth)
2744 for (i = 0; i < n; ++i) {
2747 f = isl_set_plain_is_fixed(domain[order[i]].set,
2748 isl_dim_set, depth, NULL);
2761 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2762 * of indices into the "domain" array,
2763 * eliminate the inner dimensions from the "set" field of the elements
2764 * indexed by the first "n" elements of "order", provided the current
2765 * dimension does not have a fixed value.
2767 * Return the index of the first element in "order" with a corresponding
2768 * "set" field that does not have an (obviously) fixed value.
2770 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
2771 int *order, int n, int depth, __isl_keep isl_ast_build *build)
2776 for (i = n - 1; i >= 0; --i) {
2778 f = isl_set_plain_is_fixed(domain[order[i]].set,
2779 isl_dim_set, depth, NULL);
2784 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
2785 domain[order[i]].set);
2792 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2793 * of indices into the "domain" array,
2794 * find the element of "domain" (amongst those indexed by the first "n"
2795 * elements of "order") with the "set" field that has the smallest
2796 * value for the current iterator.
2798 * Note that the domain with the smallest value may depend on the parameters
2799 * and/or outer loop dimension. Since the result of this function is only
2800 * used as heuristic, we only make a reasonable attempt at finding the best
2801 * domain, one that should work in case a single domain provides the smallest
2802 * value for the current dimension over all values of the parameters
2803 * and outer dimensions.
2805 * In particular, we compute the smallest value of the first domain
2806 * and replace it by that of any later domain if that later domain
2807 * has a smallest value that is smaller for at least some value
2808 * of the parameters and outer dimensions.
2810 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
2811 __isl_keep isl_ast_build *build)
2817 min_first = isl_ast_build_map_to_iterator(build,
2818 isl_set_copy(domain[order[0]].set));
2819 min_first = isl_map_lexmin(min_first);
2821 for (i = 1; i < n; ++i) {
2822 isl_map *min, *test;
2825 min = isl_ast_build_map_to_iterator(build,
2826 isl_set_copy(domain[order[i]].set));
2827 min = isl_map_lexmin(min);
2828 test = isl_map_copy(min);
2829 test = isl_map_apply_domain(isl_map_copy(min_first), test);
2830 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
2831 empty = isl_map_is_empty(test);
2833 if (empty >= 0 && !empty) {
2834 isl_map_free(min_first);
2844 isl_map_free(min_first);
2846 return i < n ? -1 : first;
2849 /* Construct a shifted inverse schedule based on the original inverse schedule,
2850 * the stride and the offset.
2852 * The original inverse schedule is specified as the "map" fields
2853 * of the elements of "domain" indexed by the first "n" elements of "order".
2855 * "stride" and "offset" are such that the difference
2856 * between the values of the current dimension of domain "i"
2857 * and the values of the current dimension for some reference domain are
2860 * stride * integer + offset[i]
2862 * Moreover, 0 <= offset[i] < stride.
2864 * For each domain, we create a map
2866 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
2868 * where j refers to the current dimension and the other dimensions are
2869 * unchanged, and apply this map to the original schedule domain.
2871 * For example, for the original schedule
2873 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
2875 * and assuming the offset is 0 for the A domain and 1 for the B domain,
2876 * we apply the mapping
2880 * to the schedule of the "A" domain and the mapping
2882 * { [j - 1] -> [j, 1] }
2884 * to the schedule of the "B" domain.
2887 * Note that after the transformation, the differences between pairs
2888 * of values of the current dimension over all domains are multiples
2889 * of stride and that we have therefore exposed the stride.
2892 * To see that the mapping preserves the lexicographic order,
2893 * first note that each of the individual maps above preserves the order.
2894 * If the value of the current iterator is j1 in one domain and j2 in another,
2895 * then if j1 = j2, we know that the same map is applied to both domains
2896 * and the order is preserved.
2897 * Otherwise, let us assume, without loss of generality, that j1 < j2.
2898 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
2902 * and the order is preserved.
2903 * If c1 < c2, then we know
2909 * j2 - j1 = n * s + r
2911 * with n >= 0 and 0 <= r < s.
2912 * In other words, r = c2 - c1.
2923 * (j1 - c1, c1) << (j2 - c2, c2)
2925 * with "<<" the lexicographic order, proving that the order is preserved
2928 static __isl_give isl_union_map *contruct_shifted_executed(
2929 struct isl_set_map_pair *domain, int *order, int n, isl_int stride,
2930 __isl_keep isl_vec *offset, __isl_keep isl_ast_build *build)
2934 isl_union_map *executed;
2940 depth = isl_ast_build_get_depth(build);
2941 space = isl_ast_build_get_space(build, 1);
2942 executed = isl_union_map_empty(isl_space_copy(space));
2943 space = isl_space_map_from_set(space);
2944 map = isl_map_identity(isl_space_copy(space));
2945 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
2946 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
2947 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
2949 c = isl_equality_alloc(isl_local_space_from_space(space));
2950 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
2951 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
2955 for (i = 0; i < n; ++i) {
2958 if (isl_vec_get_element(offset, i, &v) < 0)
2960 map_i = isl_map_copy(map);
2961 map_i = isl_map_fix(map_i, isl_dim_out, depth + 1, v);
2963 c = isl_constraint_set_constant(c, v);
2964 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
2966 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
2968 executed = isl_union_map_add_map(executed, map_i);
2971 isl_constraint_free(c);
2977 executed = isl_union_map_free(executed);
2982 /* Generate code for a single component, after exposing the stride,
2983 * given that the schedule domain is "shifted strided".
2985 * The component inverse schedule is specified as the "map" fields
2986 * of the elements of "domain" indexed by the first "n" elements of "order".
2988 * The schedule domain being "shifted strided" means that the differences
2989 * between the values of the current dimension of domain "i"
2990 * and the values of the current dimension for some reference domain are
2993 * stride * integer + offset[i]
2995 * We first look for the domain with the "smallest" value for the current
2996 * dimension and adjust the offsets such that the offset of the "smallest"
2997 * domain is equal to zero. The other offsets are reduced modulo stride.
2999 * Based on this information, we construct a new inverse schedule in
3000 * contruct_shifted_executed that exposes the stride.
3001 * Since this involves the introduction of a new schedule dimension,
3002 * the build needs to be changed accodingly.
3003 * After computing the AST, the newly introduced dimension needs
3004 * to be removed again from the list of grafts. We do this by plugging
3005 * in a mapping that represents the new schedule domain in terms of the
3006 * old schedule domain.
3008 static __isl_give isl_ast_graft_list *generate_shift_component(
3009 struct isl_set_map_pair *domain, int *order, int n, isl_int stride,
3010 __isl_keep isl_vec *offset, __isl_take isl_ast_build *build)
3012 isl_ast_graft_list *list;
3019 isl_multi_aff *ma, *zero;
3020 isl_union_map *executed;
3022 ctx = isl_ast_build_get_ctx(build);
3023 depth = isl_ast_build_get_depth(build);
3025 first = first_offset(domain, order, n, build);
3027 return isl_ast_build_free(build);
3030 v = isl_vec_alloc(ctx, n);
3031 if (isl_vec_get_element(offset, first, &val) < 0)
3032 v = isl_vec_free(v);
3033 isl_int_neg(val, val);
3034 v = isl_vec_set(v, val);
3035 v = isl_vec_add(v, isl_vec_copy(offset));
3036 v = isl_vec_fdiv_r(v, stride);
3038 executed = contruct_shifted_executed(domain, order, n, stride, v,
3040 space = isl_ast_build_get_space(build, 1);
3041 space = isl_space_map_from_set(space);
3042 ma = isl_multi_aff_identity(isl_space_copy(space));
3043 space = isl_space_from_domain(isl_space_domain(space));
3044 space = isl_space_add_dims(space, isl_dim_out, 1);
3045 zero = isl_multi_aff_zero(space);
3046 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3047 build = isl_ast_build_insert_dim(build, depth + 1);
3048 list = generate_shifted_component(executed, build);
3050 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3058 /* Generate code for a single component.
3060 * The component inverse schedule is specified as the "map" fields
3061 * of the elements of "domain" indexed by the first "n" elements of "order".
3063 * This function may modify the "set" fields of "domain".
3065 * Before proceeding with the actual code generation for the component,
3066 * we first check if there are any "shifted" strides, meaning that
3067 * the schedule domains of the individual domains are all strided,
3068 * but that they have different offsets, resulting in the union
3069 * of schedule domains not being strided anymore.
3071 * The simplest example is the schedule
3073 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3075 * Both schedule domains are strided, but their union is not.
3076 * This function detects such cases and then rewrites the schedule to
3078 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3080 * In the new schedule, the schedule domains have the same offset (modulo
3081 * the stride), ensuring that the union of schedule domains is also strided.
3084 * If there is only a single domain in the component, then there is
3085 * nothing to do. Similarly, if the current schedule dimension has
3086 * a fixed value for almost all domains then there is nothing to be done.
3087 * In particular, we need at least two domains where the current schedule
3088 * dimension does not have a fixed value.
3089 * Finally, if any of the options refer to the current schedule dimension,
3090 * then we bail out as well. It would be possible to reformulate the options
3091 * in terms of the new schedule domain, but that would introduce constraints
3092 * that separate the domains in the options and that is something we would
3096 * To see if there is any shifted stride, we look at the differences
3097 * between the values of the current dimension in pairs of domains
3098 * for equal values of outer dimensions. These differences should be
3103 * with "m" the stride and "r" a constant. Note that we cannot perform
3104 * this analysis on individual domains as the lower bound in each domain
3105 * may depend on parameters or outer dimensions and so the current dimension
3106 * itself may not have a fixed remainder on division by the stride.
3108 * In particular, we compare the first domain that does not have an
3109 * obviously fixed value for the current dimension to itself and all
3110 * other domains and collect the offsets and the gcd of the strides.
3111 * If the gcd becomes one, then we failed to find shifted strides.
3112 * If all the offsets are the same (for those domains that do not have
3113 * an obviously fixed value for the current dimension), then we do not
3114 * apply the transformation.
3115 * If none of the domains were skipped, then there is nothing to do.
3116 * If some of them were skipped, then if we apply separation, the schedule
3117 * domain should get split in pieces with a (non-shifted) stride.
3119 * Otherwise, we apply a shift to expose the stride in
3120 * generate_shift_component.
3122 static __isl_give isl_ast_graft_list *generate_component(
3123 struct isl_set_map_pair *domain, int *order, int n,
3124 __isl_take isl_ast_build *build)
3135 isl_ast_graft_list *list;
3138 depth = isl_ast_build_get_depth(build);
3141 if (skip >= 0 && !skip)
3142 skip = at_most_one_non_fixed(domain, order, n, depth);
3143 if (skip >= 0 && !skip)
3144 skip = isl_ast_build_options_involve_depth(build);
3146 return isl_ast_build_free(build);
3148 return generate_shifted_component_from_list(domain,
3151 base = eliminate_non_fixed(domain, order, n, depth, build);
3153 return isl_ast_build_free(build);
3155 ctx = isl_ast_build_get_ctx(build);
3160 v = isl_vec_alloc(ctx, n);
3163 for (i = 0; i < n; ++i) {
3164 map = isl_map_from_domain_and_range(
3165 isl_set_copy(domain[order[base]].set),
3166 isl_set_copy(domain[order[i]].set));
3167 for (d = 0; d < depth; ++d)
3168 map = isl_map_equate(map, isl_dim_in, d,
3170 deltas = isl_map_deltas(map);
3171 res = isl_set_dim_residue_class(deltas, depth, &m, &r);
3172 isl_set_free(deltas);
3177 isl_int_set(gcd, m);
3179 isl_int_gcd(gcd, gcd, m);
3180 if (isl_int_is_one(gcd))
3182 v = isl_vec_set_element(v, i, r);
3184 res = isl_set_plain_is_fixed(domain[order[i]].set,
3185 isl_dim_set, depth, NULL);
3191 if (fixed && i > base) {
3192 isl_vec_get_element(v, base, &m);
3193 if (isl_int_ne(m, r))
3199 isl_ast_build_free(build);
3201 } else if (i < n || fixed) {
3202 list = generate_shifted_component_from_list(domain,
3205 list = generate_shift_component(domain, order, n, gcd, v,
3217 /* Store both "map" itself and its domain in the
3218 * structure pointed to by *next and advance to the next array element.
3220 static int extract_domain(__isl_take isl_map *map, void *user)
3222 struct isl_set_map_pair **next = user;
3224 (*next)->map = isl_map_copy(map);
3225 (*next)->set = isl_map_domain(map);
3231 /* Internal data for any_scheduled_after.
3233 * "depth" is the number of loops that have already been generated
3234 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3235 * "domain" is an array of set-map pairs corresponding to the different
3236 * iteration domains. The set is the schedule domain, i.e., the domain
3237 * of the inverse schedule, while the map is the inverse schedule itself.
3239 struct isl_any_scheduled_after_data {
3241 int group_coscheduled;
3242 struct isl_set_map_pair *domain;
3245 /* Is any element of domain "i" scheduled after any element of domain "j"
3246 * (for a common iteration of the first data->depth loops)?
3248 * data->domain[i].set contains the domain of the inverse schedule
3249 * for domain "i", i.e., elements in the schedule domain.
3251 * If data->group_coscheduled is set, then we also return 1 if there
3252 * is any pair of elements in the two domains that are scheduled together.
3254 static int any_scheduled_after(int i, int j, void *user)
3256 struct isl_any_scheduled_after_data *data = user;
3257 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
3260 for (pos = data->depth; pos < dim; ++pos) {
3263 follows = isl_set_follows_at(data->domain[i].set,
3264 data->domain[j].set, pos);
3274 return data->group_coscheduled;
3277 /* Look for independent components at the current depth and generate code
3278 * for each component separately. The resulting lists of grafts are
3279 * merged in an attempt to combine grafts with identical guards.
3281 * Code for two domains can be generated separately if all the elements
3282 * of one domain are scheduled before (or together with) all the elements
3283 * of the other domain. We therefore consider the graph with as nodes
3284 * the domains and an edge between two nodes if any element of the first
3285 * node is scheduled after any element of the second node.
3286 * If the ast_build_group_coscheduled is set, then we also add an edge if
3287 * there is any pair of elements in the two domains that are scheduled
3289 * Code is then generated (by generate_component)
3290 * for each of the strongly connected components in this graph
3291 * in their topological order.
3293 * Since the test is performed on the domain of the inverse schedules of
3294 * the different domains, we precompute these domains and store
3295 * them in data.domain.
3297 static __isl_give isl_ast_graft_list *generate_components(
3298 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3301 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3302 int n = isl_union_map_n_map(executed);
3303 struct isl_any_scheduled_after_data data;
3304 struct isl_set_map_pair *next;
3305 struct isl_tarjan_graph *g = NULL;
3306 isl_ast_graft_list *list = NULL;
3309 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
3315 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
3320 data.depth = isl_ast_build_get_depth(build);
3321 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
3322 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
3324 list = isl_ast_graft_list_alloc(ctx, 0);
3328 isl_ast_graft_list *list_c;
3331 if (g->order[i] == -1)
3332 isl_die(ctx, isl_error_internal, "cannot happen",
3335 while (g->order[i] != -1) {
3339 list_c = generate_component(data.domain,
3340 g->order + first, i - first,
3341 isl_ast_build_copy(build));
3342 list = isl_ast_graft_list_merge(list, list_c, build);
3348 error: list = isl_ast_graft_list_free(list);
3349 isl_tarjan_graph_free(g);
3350 for (i = 0; i < n_domain; ++i) {
3351 isl_map_free(data.domain[i].map);
3352 isl_set_free(data.domain[i].set);
3355 isl_union_map_free(executed);
3356 isl_ast_build_free(build);
3361 /* Generate code for the next level (and all inner levels).
3363 * If "executed" is empty, i.e., no code needs to be generated,
3364 * then we return an empty list.
3366 * If we have already generated code for all loop levels, then we pass
3367 * control to generate_inner_level.
3369 * If "executed" lives in a single space, i.e., if code needs to be
3370 * generated for a single domain, then there can only be a single
3371 * component and we go directly to generate_shifted_component.
3372 * Otherwise, we call generate_components to detect the components
3373 * and to call generate_component on each of them separately.
3375 static __isl_give isl_ast_graft_list *generate_next_level(
3376 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3380 if (!build || !executed)
3383 if (isl_union_map_is_empty(executed)) {
3384 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3385 isl_union_map_free(executed);
3386 isl_ast_build_free(build);
3387 return isl_ast_graft_list_alloc(ctx, 0);
3390 depth = isl_ast_build_get_depth(build);
3391 if (depth >= isl_set_dim(build->domain, isl_dim_set))
3392 return generate_inner_level(executed, build);
3394 if (isl_union_map_n_map(executed) == 1)
3395 return generate_shifted_component(executed, build);
3397 return generate_components(executed, build);
3399 isl_union_map_free(executed);
3400 isl_ast_build_free(build);
3404 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3405 * internal, executed and build are the inputs to generate_code.
3406 * list collects the output.
3408 struct isl_generate_code_data {
3410 isl_union_map *executed;
3411 isl_ast_build *build;
3413 isl_ast_graft_list *list;
3416 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3420 * with E the external build schedule and S the additional schedule "space",
3421 * reformulate the inverse schedule in terms of the internal schedule domain,
3426 * We first obtain a mapping
3430 * take the inverse and the product with S -> S, resulting in
3432 * [I -> S] -> [E -> S]
3434 * Applying the map to the input produces the desired result.
3436 static __isl_give isl_union_map *internal_executed(
3437 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
3438 __isl_keep isl_ast_build *build)
3442 proj = isl_ast_build_get_schedule_map(build);
3443 proj = isl_map_reverse(proj);
3444 space = isl_space_map_from_set(isl_space_copy(space));
3445 id = isl_map_identity(space);
3446 proj = isl_map_product(proj, id);
3447 executed = isl_union_map_apply_domain(executed,
3448 isl_union_map_from_map(proj));
3452 /* Generate an AST that visits the elements in the range of data->executed
3453 * in the relative order specified by the corresponding image element(s)
3454 * for those image elements that belong to "set".
3455 * Add the result to data->list.
3457 * The caller ensures that "set" is a universe domain.
3458 * "space" is the space of the additional part of the schedule.
3459 * It is equal to the space of "set" if build->domain is parametric.
3460 * Otherwise, it is equal to the range of the wrapped space of "set".
3462 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3463 * was called from an outside user (data->internal not set), then
3464 * the (inverse) schedule refers to the external build domain and needs to
3465 * be transformed to refer to the internal build domain.
3467 * The build is extended to include the additional part of the schedule.
3468 * If the original build space was not parametric, then the options
3469 * in data->build refer only to the additional part of the schedule
3470 * and they need to be adjusted to refer to the complete AST build
3473 * After having adjusted inverse schedule and build, we start generating
3474 * code with the outer loop of the current code generation
3475 * in generate_next_level.
3477 * If the original build space was not parametric, we undo the embedding
3478 * on the resulting isl_ast_node_list so that it can be used within
3479 * the outer AST build.
3481 static int generate_code_in_space(struct isl_generate_code_data *data,
3482 __isl_take isl_set *set, __isl_take isl_space *space)
3484 isl_union_map *executed;
3485 isl_ast_build *build;
3486 isl_ast_graft_list *list;
3489 executed = isl_union_map_copy(data->executed);
3490 executed = isl_union_map_intersect_domain(executed,
3491 isl_union_set_from_set(set));
3493 embed = !isl_set_is_params(data->build->domain);
3494 if (embed && !data->internal)
3495 executed = internal_executed(executed, space, data->build);
3497 build = isl_ast_build_copy(data->build);
3498 build = isl_ast_build_product(build, space);
3500 list = generate_next_level(executed, build);
3502 list = isl_ast_graft_list_unembed(list, embed);
3504 data->list = isl_ast_graft_list_concat(data->list, list);
3509 /* Generate an AST that visits the elements in the range of data->executed
3510 * in the relative order specified by the corresponding domain element(s)
3511 * for those domain elements that belong to "set".
3512 * Add the result to data->list.
3514 * The caller ensures that "set" is a universe domain.
3516 * If the build space S is not parametric, then the space of "set"
3517 * need to be a wrapped relation with S as domain. That is, it needs
3522 * Check this property and pass control to generate_code_in_space
3524 * If the build space is not parametric, then T is the space of "set".
3526 static int generate_code_set(__isl_take isl_set *set, void *user)
3528 struct isl_generate_code_data *data = user;
3529 isl_space *space, *build_space;
3532 space = isl_set_get_space(set);
3534 if (isl_set_is_params(data->build->domain))
3535 return generate_code_in_space(data, set, space);
3537 build_space = isl_ast_build_get_space(data->build, data->internal);
3538 space = isl_space_unwrap(space);
3539 is_domain = isl_space_is_domain(build_space, space);
3540 isl_space_free(build_space);
3541 space = isl_space_range(space);
3546 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3547 "invalid nested schedule space", goto error);
3549 return generate_code_in_space(data, set, space);
3552 isl_space_free(space);
3556 /* Generate an AST that visits the elements in the range of "executed"
3557 * in the relative order specified by the corresponding domain element(s).
3559 * "build" is an isl_ast_build that has either been constructed by
3560 * isl_ast_build_from_context or passed to a callback set by
3561 * isl_ast_build_set_create_leaf.
3562 * In the first case, the space of the isl_ast_build is typically
3563 * a parametric space, although this is currently not enforced.
3564 * In the second case, the space is never a parametric space.
3565 * If the space S is not parametric, then the domain space(s) of "executed"
3566 * need to be wrapped relations with S as domain.
3568 * If the domain of "executed" consists of several spaces, then an AST
3569 * is generated for each of them (in arbitrary order) and the results
3572 * If "internal" is set, then the domain "S" above refers to the internal
3573 * schedule domain representation. Otherwise, it refers to the external
3574 * representation, as returned by isl_ast_build_get_schedule_space.
3576 * We essentially run over all the spaces in the domain of "executed"
3577 * and call generate_code_set on each of them.
3579 static __isl_give isl_ast_graft_list *generate_code(
3580 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3584 struct isl_generate_code_data data = { 0 };
3586 isl_union_set *schedule_domain;
3587 isl_union_map *universe;
3591 space = isl_ast_build_get_space(build, 1);
3592 space = isl_space_align_params(space,
3593 isl_union_map_get_space(executed));
3594 space = isl_space_align_params(space,
3595 isl_union_map_get_space(build->options));
3596 build = isl_ast_build_align_params(build, isl_space_copy(space));
3597 executed = isl_union_map_align_params(executed, space);
3598 if (!executed || !build)
3601 ctx = isl_ast_build_get_ctx(build);
3603 data.internal = internal;
3604 data.executed = executed;
3606 data.list = isl_ast_graft_list_alloc(ctx, 0);
3608 universe = isl_union_map_universe(isl_union_map_copy(executed));
3609 schedule_domain = isl_union_map_domain(universe);
3610 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
3612 data.list = isl_ast_graft_list_free(data.list);
3614 isl_union_set_free(schedule_domain);
3615 isl_union_map_free(executed);
3617 isl_ast_build_free(build);
3620 isl_union_map_free(executed);
3621 isl_ast_build_free(build);
3625 /* Generate an AST that visits the elements in the domain of "schedule"
3626 * in the relative order specified by the corresponding image element(s).
3628 * "build" is an isl_ast_build that has either been constructed by
3629 * isl_ast_build_from_context or passed to a callback set by
3630 * isl_ast_build_set_create_leaf.
3631 * In the first case, the space of the isl_ast_build is typically
3632 * a parametric space, although this is currently not enforced.
3633 * In the second case, the space is never a parametric space.
3634 * If the space S is not parametric, then the range space(s) of "schedule"
3635 * need to be wrapped relations with S as domain.
3637 * If the range of "schedule" consists of several spaces, then an AST
3638 * is generated for each of them (in arbitrary order) and the results
3641 * We first initialize the local copies of the relevant options.
3642 * We do this here rather than when the isl_ast_build is created
3643 * because the options may have changed between the construction
3644 * of the isl_ast_build and the call to isl_generate_code.
3646 * The main computation is performed on an inverse schedule (with
3647 * the schedule domain in the domain and the elements to be executed
3648 * in the range) called "executed".
3650 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
3651 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
3653 isl_ast_graft_list *list;
3655 isl_union_map *executed;
3657 build = isl_ast_build_copy(build);
3658 build = isl_ast_build_set_single_valued(build, 0);
3659 executed = isl_union_map_reverse(schedule);
3660 list = generate_code(executed, isl_ast_build_copy(build), 0);
3661 node = isl_ast_node_from_graft_list(list, build);
3662 isl_ast_build_free(build);