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 /* Eliminate the schedule dimension "pos" from "executed" and return
321 static __isl_give isl_union_map
*eliminate(__isl_take isl_union_map
*executed
,
322 int pos
, __isl_keep isl_ast_build
*build
)
327 space
= isl_ast_build_get_space(build
, 1);
328 space
= isl_space_map_from_set(space
);
329 elim
= isl_map_identity(space
);
330 elim
= isl_map_eliminate(elim
, isl_dim_in
, pos
, 1);
332 executed
= isl_union_map_apply_domain(executed
,
333 isl_union_map_from_map(elim
));
338 /* Check if the constraint "c" is a lower bound on dimension "pos",
339 * an upper bound, or independent of dimension "pos".
341 static int constraint_type(isl_constraint
*c
, int pos
)
343 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
345 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
350 /* Compare the types of the constraints "a" and "b",
351 * resulting in constraints that are independent of "depth"
352 * to be sorted before the lower bounds on "depth", which in
353 * turn are sorted before the upper bounds on "depth".
355 static int cmp_constraint(const void *a
, const void *b
, void *user
)
358 isl_constraint
* const *c1
= a
;
359 isl_constraint
* const *c2
= b
;
360 int t1
= constraint_type(*c1
, *depth
);
361 int t2
= constraint_type(*c2
, *depth
);
366 /* Extract a lower bound on dimension "pos" from constraint "c".
368 * If the constraint is of the form
372 * then we essentially return
374 * l = ceil(-f(...)/a)
376 * However, if the current dimension is strided, then we need to make
377 * sure that the lower bound we construct is of the form
381 * with f the offset and s the stride.
382 * We therefore compute
384 * f + s * ceil((l - f)/s)
386 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
387 int pos
, __isl_keep isl_ast_build
*build
)
391 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
392 aff
= isl_aff_ceil(aff
);
394 if (isl_ast_build_has_stride(build
, pos
)) {
398 isl_int_init(stride
);
400 offset
= isl_ast_build_get_offset(build
, pos
);
401 isl_ast_build_get_stride(build
, pos
, &stride
);
403 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
404 aff
= isl_aff_scale_down(aff
, stride
);
405 aff
= isl_aff_ceil(aff
);
406 aff
= isl_aff_scale(aff
, stride
);
407 aff
= isl_aff_add(aff
, offset
);
409 isl_int_clear(stride
);
412 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
417 /* Return the exact lower bound (or upper bound if "upper" is set)
418 * of "domain" as a piecewise affine expression.
420 * If we are computing a lower bound (of a strided dimension), then
421 * we need to make sure it is of the form
425 * where f is the offset and s is the stride.
426 * We therefore need to include the stride constraint before computing
429 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
430 __isl_keep isl_ast_build
*build
, int upper
)
435 isl_pw_multi_aff
*pma
;
437 domain
= isl_set_copy(domain
);
439 stride
= isl_ast_build_get_stride_constraint(build
);
440 domain
= isl_set_intersect(domain
, stride
);
442 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
444 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
446 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
447 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
448 isl_pw_multi_aff_free(pma
);
449 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
450 pa
= isl_pw_aff_coalesce(pa
);
455 /* Return a list of "n" lower bounds on dimension "pos"
456 * extracted from the "n" constraints starting at "constraint".
457 * If "n" is zero, then we extract a lower bound from "domain" instead.
459 static __isl_give isl_pw_aff_list
*lower_bounds(
460 __isl_keep isl_constraint
**constraint
, int n
, int pos
,
461 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
464 isl_pw_aff_list
*list
;
472 pa
= exact_bound(domain
, build
, 0);
473 return isl_pw_aff_list_from_pw_aff(pa
);
476 ctx
= isl_ast_build_get_ctx(build
);
477 list
= isl_pw_aff_list_alloc(ctx
,n
);
479 for (i
= 0; i
< n
; ++i
) {
482 aff
= lower_bound(constraint
[i
], pos
, build
);
483 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
489 /* Return a list of "n" upper bounds on dimension "pos"
490 * extracted from the "n" constraints starting at "constraint".
491 * If "n" is zero, then we extract an upper bound from "domain" instead.
493 static __isl_give isl_pw_aff_list
*upper_bounds(
494 __isl_keep isl_constraint
**constraint
, int n
, int pos
,
495 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
498 isl_pw_aff_list
*list
;
503 pa
= exact_bound(domain
, build
, 1);
504 return isl_pw_aff_list_from_pw_aff(pa
);
507 ctx
= isl_ast_build_get_ctx(build
);
508 list
= isl_pw_aff_list_alloc(ctx
,n
);
510 for (i
= 0; i
< n
; ++i
) {
513 aff
= isl_constraint_get_bound(constraint
[i
], isl_dim_set
, pos
);
514 aff
= isl_aff_floor(aff
);
515 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
521 /* Return an isl_ast_expr that performs the reduction of type "type"
522 * on AST expressions corresponding to the elements in "list".
524 * The list is assumed to contain at least one element.
525 * If the list contains exactly one element, then the returned isl_ast_expr
526 * simply computes that affine expression.
528 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
529 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
538 n
= isl_pw_aff_list_n_pw_aff(list
);
541 return isl_ast_build_expr_from_pw_aff_internal(build
,
542 isl_pw_aff_list_get_pw_aff(list
, 0));
544 ctx
= isl_pw_aff_list_get_ctx(list
);
545 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
549 for (i
= 0; i
< n
; ++i
) {
550 isl_ast_expr
*expr_i
;
552 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
553 isl_pw_aff_list_get_pw_aff(list
, i
));
555 return isl_ast_expr_free(expr
);
556 expr
->u
.op
.args
[i
] = expr_i
;
562 /* Add a guard to "graft" based on "bound" in the case of a degenerate
563 * level (including the special case of an eliminated level).
565 * We eliminate the current dimension, simplify the result in the current
566 * build and add the result as guards to the graft.
568 * Note that we cannot simply drop the constraints on the current dimension
569 * even in the eliminated case, because the single affine expression may
570 * not be explicitly available in "bounds". Moreover, the single affine
571 * expression may only be defined on a subset of the build domain,
572 * so we do in some cases need to insert a guard even in the eliminated case.
574 static __isl_give isl_ast_graft
*add_degenerate_guard(
575 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
576 __isl_keep isl_ast_build
*build
)
581 depth
= isl_ast_build_get_depth(build
);
583 dom
= isl_set_from_basic_set(isl_basic_set_copy(bounds
));
584 if (isl_ast_build_has_stride(build
, depth
)) {
587 stride
= isl_ast_build_get_stride_constraint(build
);
588 dom
= isl_set_intersect(dom
, stride
);
590 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
591 dom
= isl_ast_build_compute_gist(build
, dom
);
593 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
598 /* Update "graft" based on "bounds" for the eliminated case.
600 * In the eliminated case, no for node is created, so we only need
601 * to check if "bounds" imply any guards that need to be inserted.
603 static __isl_give isl_ast_graft
*refine_eliminated(
604 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
605 __isl_keep isl_ast_build
*build
)
607 return add_degenerate_guard(graft
, bounds
, build
);
610 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
612 * "build" is the build in which graft->node was created
613 * "sub_build" contains information about the current level itself,
614 * including the single value attained.
616 * We first set the initialization part of the for loop to the single
617 * value attained by the current dimension.
618 * The increment and condition are not strictly needed as the are known
619 * to be "1" and "iterator <= value" respectively.
620 * Then we set the size of the iterator and
621 * check if "bounds" imply any guards that need to be inserted.
623 static __isl_give isl_ast_graft
*refine_degenerate(
624 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
625 __isl_keep isl_ast_build
*build
,
626 __isl_keep isl_ast_build
*sub_build
)
630 if (!graft
|| !sub_build
)
631 return isl_ast_graft_free(graft
);
633 value
= isl_pw_aff_copy(sub_build
->value
);
635 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
637 if (!graft
->node
->u
.f
.init
)
638 return isl_ast_graft_free(graft
);
640 graft
= add_degenerate_guard(graft
, bounds
, build
);
645 /* Return the intersection of the "n" constraints starting at "constraint"
648 static __isl_give isl_set
*intersect_constraints(isl_ctx
*ctx
,
649 __isl_keep isl_constraint
**constraint
, int n
)
655 isl_die(ctx
, isl_error_internal
,
656 "expecting at least one constraint", return NULL
);
658 bset
= isl_basic_set_from_constraint(
659 isl_constraint_copy(constraint
[0]));
660 for (i
= 1; i
< n
; ++i
) {
661 isl_basic_set
*bset_i
;
663 bset_i
= isl_basic_set_from_constraint(
664 isl_constraint_copy(constraint
[i
]));
665 bset
= isl_basic_set_intersect(bset
, bset_i
);
668 return isl_set_from_basic_set(bset
);
671 /* Compute the constraints on the outer dimensions enforced by
672 * graft->node and add those constraints to graft->enforced,
673 * in case the upper bound is expressed as a set "upper".
675 * In particular, if l(...) is a lower bound in "lower", and
677 * -a i + f(...) >= 0 or a i <= f(...)
679 * is an upper bound ocnstraint on the current dimension i,
680 * then the for loop enforces the constraint
682 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
684 * We therefore simply take each lower bound in turn, plug it into
685 * the upper bounds and compute the intersection over all lower bounds.
687 * If a lower bound is a rational expression, then
688 * isl_basic_set_preimage_multi_aff will force this rational
689 * expression to have only integer values. However, the loop
690 * itself does not enforce this integrality constraint. We therefore
691 * use the ceil of the lower bounds instead of the lower bounds themselves.
692 * Other constraints will make sure that the for loop is only executed
693 * when each of the lower bounds attains an integral value.
694 * In particular, potentially rational values only occur in
695 * lower_bound if the offset is a (seemingly) rational expression,
696 * but then outer conditions will make sure that this rational expression
697 * only attains integer values.
699 static __isl_give isl_ast_graft
*set_enforced_from_set(
700 __isl_take isl_ast_graft
*graft
,
701 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
704 isl_basic_set
*enforced
;
705 isl_pw_multi_aff
*pma
;
708 if (!graft
|| !lower
)
709 return isl_ast_graft_free(graft
);
711 space
= isl_set_get_space(upper
);
712 enforced
= isl_basic_set_universe(isl_space_copy(space
));
714 space
= isl_space_map_from_set(space
);
715 pma
= isl_pw_multi_aff_identity(space
);
717 n
= isl_pw_aff_list_n_pw_aff(lower
);
718 for (i
= 0; i
< n
; ++i
) {
722 isl_pw_multi_aff
*pma_i
;
724 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
725 pa
= isl_pw_aff_ceil(pa
);
726 pma_i
= isl_pw_multi_aff_copy(pma
);
727 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
728 enforced_i
= isl_set_copy(upper
);
729 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
730 hull
= isl_set_simple_hull(enforced_i
);
731 enforced
= isl_basic_set_intersect(enforced
, hull
);
734 isl_pw_multi_aff_free(pma
);
736 graft
= isl_ast_graft_enforce(graft
, enforced
);
741 /* Compute the constraints on the outer dimensions enforced by
742 * graft->node and add those constraints to graft->enforced,
743 * in case the upper bound is expressed as
744 * a list of affine expressions "upper".
746 * The enforced condition is that each lower bound expression is less
747 * than or equal to each upper bound expression.
749 static __isl_give isl_ast_graft
*set_enforced_from_list(
750 __isl_take isl_ast_graft
*graft
,
751 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
754 isl_basic_set
*enforced
;
756 lower
= isl_pw_aff_list_copy(lower
);
757 upper
= isl_pw_aff_list_copy(upper
);
758 cond
= isl_pw_aff_list_le_set(lower
, upper
);
759 enforced
= isl_set_simple_hull(cond
);
760 graft
= isl_ast_graft_enforce(graft
, enforced
);
765 /* Does "aff" have a negative constant term?
767 static int aff_constant_is_negative(__isl_take isl_set
*set
,
768 __isl_take isl_aff
*aff
, void *user
)
774 isl_aff_get_constant(aff
, &v
);
775 *neg
= isl_int_is_neg(v
);
780 return *neg
? 0 : -1;
783 /* Does "pa" have a negative constant term over its entire domain?
785 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
, void *user
)
790 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
793 return *neg
? 0 : -1;
796 /* Does each element in "list" have a negative constant term?
798 * The callback terminates the iteration as soon an element has been
799 * found that does not have a negative constant term.
801 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
805 if (isl_pw_aff_list_foreach(list
,
806 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
812 /* Add 1 to each of the elements in "list", where each of these elements
813 * is defined over the internal schedule space of "build".
815 static __isl_give isl_pw_aff_list
*list_add_one(
816 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
823 space
= isl_ast_build_get_space(build
, 1);
824 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
825 aff
= isl_aff_add_constant_si(aff
, 1);
826 one
= isl_pw_aff_from_aff(aff
);
828 n
= isl_pw_aff_list_n_pw_aff(list
);
829 for (i
= 0; i
< n
; ++i
) {
831 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
832 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
833 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
836 isl_pw_aff_free(one
);
841 /* Set the condition part of the for node graft->node in case
842 * the upper bound is represented as a list of piecewise affine expressions.
844 * In particular, set the condition to
846 * iterator <= min(list of upper bounds)
848 * If each of the upper bounds has a negative constant term, then
849 * set the condition to
851 * iterator < min(list of (upper bound + 1)s)
854 static __isl_give isl_ast_graft
*set_for_cond_from_list(
855 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
856 __isl_keep isl_ast_build
*build
)
859 isl_ast_expr
*bound
, *iterator
, *cond
;
860 enum isl_ast_op_type type
= isl_ast_op_le
;
863 return isl_ast_graft_free(graft
);
865 neg
= list_constant_is_negative(list
);
867 return isl_ast_graft_free(graft
);
868 list
= isl_pw_aff_list_copy(list
);
870 list
= list_add_one(list
, build
);
871 type
= isl_ast_op_lt
;
874 bound
= reduce_list(isl_ast_op_min
, list
, build
);
875 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
876 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
877 graft
->node
->u
.f
.cond
= cond
;
879 isl_pw_aff_list_free(list
);
880 if (!graft
->node
->u
.f
.cond
)
881 return isl_ast_graft_free(graft
);
885 /* Set the condition part of the for node graft->node in case
886 * the upper bound is represented as a set.
888 static __isl_give isl_ast_graft
*set_for_cond_from_set(
889 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
890 __isl_keep isl_ast_build
*build
)
897 cond
= isl_ast_build_expr_from_set(build
, isl_set_copy(set
));
898 graft
->node
->u
.f
.cond
= cond
;
899 if (!graft
->node
->u
.f
.cond
)
900 return isl_ast_graft_free(graft
);
904 /* Construct an isl_ast_expr for the increment (i.e., stride) of
905 * the current dimension.
907 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
916 ctx
= isl_ast_build_get_ctx(build
);
917 depth
= isl_ast_build_get_depth(build
);
919 if (!isl_ast_build_has_stride(build
, depth
))
920 return isl_ast_expr_alloc_int_si(ctx
, 1);
923 isl_ast_build_get_stride(build
, depth
, &v
);
924 inc
= isl_ast_expr_alloc_int(ctx
, v
);
930 /* Should we express the loop condition as
932 * iterator <= min(list of upper bounds)
934 * or as a conjunction of constraints?
936 * The first is constructed from a list of upper bounds.
937 * The second is constructed from a set.
939 * If there are no upper bounds in "constraints", then this could mean
940 * that "domain" simply doesn't have an upper bound or that we didn't
941 * pick any upper bound. In the first case, we want to generate the
942 * loop condition as a(n empty) conjunction of constraints
943 * In the second case, we will compute
944 * a single upper bound from "domain" and so we use the list form.
946 * If there are upper bounds in "constraints",
947 * then we use the list form iff the atomic_upper_bound option is set.
949 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
950 __isl_keep isl_set
*domain
, int depth
)
953 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
955 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
958 /* Fill in the expressions of the for node in graft->node.
961 * - set the initialization part of the loop to the maximum of the lower bounds
962 * - set the size of the iterator based on the values attained by the iterator
963 * - extract the increment from the stride of the current dimension
964 * - construct the for condition either based on a list of upper bounds
965 * or on a set of upper bound constraints.
967 static __isl_give isl_ast_graft
*set_for_node_expressions(
968 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
969 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
970 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
977 build
= isl_ast_build_copy(build
);
978 build
= isl_ast_build_set_enforced(build
,
979 isl_ast_graft_get_enforced(graft
));
982 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
983 node
->u
.f
.inc
= for_inc(build
);
986 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
988 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
990 isl_ast_build_free(build
);
992 if (!node
->u
.f
.iterator
|| !node
->u
.f
.init
||
993 !node
->u
.f
.cond
|| !node
->u
.f
.inc
)
994 return isl_ast_graft_free(graft
);
999 /* Update "graft" based on "bounds" and "domain" for the generic,
1000 * non-degenerate, case.
1002 * "constraints" contains the "n_lower" lower and "n_upper" upper bounds
1003 * that the loop node should express.
1004 * "domain" is the subset of the intersection of the constraints
1005 * for which some code is executed.
1007 * There may be zero lower bounds or zero upper bounds in "constraints"
1008 * in case the list of constraints was created
1009 * based on the atomic option or based on separation with explicit bounds.
1010 * In that case, we use "domain" to derive lower and/or upper bounds.
1012 * We first compute a list of one or more lower bounds.
1014 * Then we decide if we want to express the condition as
1016 * iterator <= min(list of upper bounds)
1018 * or as a conjunction of constraints.
1020 * The set of enforced constraints is then computed either based on
1021 * a list of upper bounds or on a set of upper bound constraints.
1022 * We do not compute any enforced constraints if we were forced
1023 * to compute a lower or upper bound using exact_bound. The domains
1024 * of the resulting expressions may imply some bounds on outer dimensions
1025 * that we do not want to appear in the enforced constraints since
1026 * they are not actually enforced by the corresponding code.
1028 * Finally, we fill in the expressions of the for node.
1030 static __isl_give isl_ast_graft
*refine_generic_bounds(
1031 __isl_take isl_ast_graft
*graft
,
1032 __isl_keep isl_constraint
**constraint
, int n_lower
, int n_upper
,
1033 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1037 isl_pw_aff_list
*lower
;
1039 isl_set
*upper_set
= NULL
;
1040 isl_pw_aff_list
*upper_list
= NULL
;
1042 if (!graft
|| !build
)
1043 return isl_ast_graft_free(graft
);
1045 depth
= isl_ast_build_get_depth(build
);
1046 ctx
= isl_ast_graft_get_ctx(graft
);
1048 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1050 lower
= lower_bounds(constraint
, n_lower
, depth
, domain
, build
);
1053 upper_list
= upper_bounds(constraint
+ n_lower
, n_upper
, depth
,
1055 else if (n_upper
> 0)
1056 upper_set
= intersect_constraints(ctx
, constraint
+ n_lower
,
1059 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1061 if (n_lower
== 0 || n_upper
== 0)
1064 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1066 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1068 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1071 isl_pw_aff_list_free(lower
);
1072 isl_pw_aff_list_free(upper_list
);
1073 isl_set_free(upper_set
);
1078 /* How many constraints in the "constraint" array, starting at position "first"
1079 * are of the give type? "n" represents the total number of elements
1082 static int count_constraints(isl_constraint
**constraint
, int n
, int first
,
1087 constraint
+= first
;
1089 for (i
= 0; first
+ i
< n
; i
++)
1090 if (constraint_type(constraint
[i
], pos
) != type
)
1096 /* Update "graft" based on "bounds" and "domain" for the generic,
1097 * non-degenerate, case.
1099 * "list" respresent the list of bounds that need to be encoded by
1100 * the for loop (or a guard around the for loop).
1101 * "domain" is the subset of the intersection of the constraints
1102 * for which some code is executed.
1103 * "build" is the build in which graft->node was created.
1105 * We separate lower bounds, upper bounds and constraints that
1106 * are independent of the loop iterator.
1108 * The actual for loop bounds are generated in refine_generic_bounds.
1109 * If there are any constraints that are independent of the loop iterator,
1110 * we need to put a guard around the for loop (which may get hoisted up
1111 * to higher levels) and we call refine_generic_bounds in a build
1112 * where this guard is enforced.
1114 static __isl_give isl_ast_graft
*refine_generic_split(
1115 __isl_take isl_ast_graft
*graft
, __isl_keep isl_constraint_list
*list
,
1116 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1119 isl_ast_build
*for_build
;
1121 int n_indep
, n_lower
, n_upper
;
1126 return isl_ast_graft_free(graft
);
1128 pos
= isl_ast_build_get_depth(build
);
1130 if (isl_sort(list
->p
, list
->n
, sizeof(isl_constraint
*),
1131 &cmp_constraint
, &pos
) < 0)
1132 return isl_ast_graft_free(graft
);
1135 n_indep
= count_constraints(list
->p
, n
, 0, pos
, 0);
1136 n_lower
= count_constraints(list
->p
, n
, n_indep
, pos
, 1);
1137 n_upper
= count_constraints(list
->p
, n
, n_indep
+ n_lower
, pos
, 2);
1140 return refine_generic_bounds(graft
,
1141 list
->p
+ n_indep
, n_lower
, n_upper
, domain
, build
);
1143 ctx
= isl_ast_graft_get_ctx(graft
);
1144 guard
= intersect_constraints(ctx
, list
->p
, n_indep
);
1146 for_build
= isl_ast_build_copy(build
);
1147 for_build
= isl_ast_build_restrict_pending(for_build
,
1148 isl_set_copy(guard
));
1149 graft
= refine_generic_bounds(graft
,
1150 list
->p
+ n_indep
, n_lower
, n_upper
, domain
, for_build
);
1151 isl_ast_build_free(for_build
);
1153 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
1158 /* Add the guard implied by the current stride constraint (if any),
1159 * but not (necessarily) enforced by the generated AST to "graft".
1161 static __isl_give isl_ast_graft
*add_stride_guard(
1162 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
1167 depth
= isl_ast_build_get_depth(build
);
1168 if (!isl_ast_build_has_stride(build
, depth
))
1171 dom
= isl_ast_build_get_stride_constraint(build
);
1172 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
1173 dom
= isl_ast_build_compute_gist(build
, dom
);
1175 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
1180 /* Update "graft" based on "bounds" and "domain" for the generic,
1181 * non-degenerate, case.
1183 * "bounds" respresent the bounds that need to be encoded by
1184 * the for loop (or a guard around the for loop).
1185 * "domain" is the subset of "bounds" for which some code is executed.
1186 * "build" is the build in which graft->node was created.
1188 * We break up "bounds" into a list of constraints and continue with
1189 * refine_generic_split.
1191 static __isl_give isl_ast_graft
*refine_generic(
1192 __isl_take isl_ast_graft
*graft
,
1193 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1194 __isl_keep isl_ast_build
*build
)
1196 isl_constraint_list
*list
;
1198 if (!build
|| !graft
)
1199 return isl_ast_graft_free(graft
);
1201 bounds
= isl_basic_set_copy(bounds
);
1202 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1203 list
= isl_constraint_list_from_basic_set(bounds
);
1205 graft
= refine_generic_split(graft
, list
, domain
, build
);
1206 graft
= add_stride_guard(graft
, build
);
1208 isl_constraint_list_free(list
);
1212 /* Create a for node for the current level.
1214 * Mark the for node degenerate if "degenerate" is set.
1216 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1226 depth
= isl_ast_build_get_depth(build
);
1227 id
= isl_ast_build_get_iterator_id(build
, depth
);
1228 node
= isl_ast_node_alloc_for(id
);
1230 node
= isl_ast_node_for_mark_degenerate(node
);
1235 /* Create an AST node for the current dimension based on
1236 * the schedule domain "bounds" and return the node encapsulated
1237 * in an isl_ast_graft.
1239 * "executed" is the current inverse schedule, taking into account
1240 * the bounds in "bounds"
1241 * "domain" is the domain of "executed", with inner dimensions projected out.
1242 * It may be a strict subset of "bounds" in case "bounds" was created
1243 * based on the atomic option or based on separation with explicit bounds.
1245 * "domain" may satisfy additional equalities that result
1246 * from intersecting "executed" with "bounds" in add_node.
1247 * It may also satisfy some global constraints that were dropped out because
1248 * we performed separation with explicit bounds.
1249 * The very first step is then to copy these constraints to "bounds".
1251 * Since we may be calling before_each_for and after_each_for
1252 * callbacks, we record the current inverse schedule in the build.
1254 * We consider three builds,
1255 * "build" is the one in which the current level is created,
1256 * "body_build" is the build in which the next level is created,
1257 * "sub_build" is essentially the same as "body_build", except that
1258 * the depth has not been increased yet.
1260 * "build" already contains information (in strides and offsets)
1261 * about the strides at the current level, but this information is not
1262 * reflected in the build->domain.
1263 * We first add this information and the "bounds" to the sub_build->domain.
1264 * isl_ast_build_set_loop_bounds checks whether the current dimension attains
1265 * only a single value and whether this single value can be represented using
1266 * a single affine expression.
1267 * In the first case, the current level is considered "degenerate".
1268 * In the second, sub-case, the current level is considered "eliminated".
1269 * Eliminated level don't need to be reflected in the AST since we can
1270 * simply plug in the affine expression. For degenerate, but non-eliminated,
1271 * levels, we do introduce a for node, but mark is as degenerate so that
1272 * it can be printed as an assignment of the single value to the loop
1275 * If the current level is eliminated, we eliminate the current dimension
1276 * from the inverse schedule to make sure no inner dimensions depend
1277 * on the current dimension. Otherwise, we create a for node, marking
1278 * it degenerate if appropriate. The initial for node is still incomplete
1279 * and will be completed in either refine_degenerate or refine_generic.
1281 * We then generate a sequence of grafts for the next level,
1282 * create a surrounding graft for the current level and insert
1283 * the for node we created (if the current level is not eliminated).
1285 * Finally, we set the bounds of the for loop and insert guards
1286 * (either in the AST or in the graft) in one of
1287 * refine_eliminated, refine_degenerate or refine_generic.
1289 static __isl_give isl_ast_graft
*create_node_scaled(
1290 __isl_take isl_union_map
*executed
,
1291 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1292 __isl_take isl_ast_build
*build
)
1295 int degenerate
, eliminated
;
1296 isl_basic_set
*hull
;
1297 isl_ast_node
*node
= NULL
;
1298 isl_ast_graft
*graft
;
1299 isl_ast_graft_list
*children
;
1300 isl_ast_build
*sub_build
;
1301 isl_ast_build
*body_build
;
1303 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1304 domain
= isl_set_detect_equalities(domain
);
1305 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1306 bounds
= isl_basic_set_intersect(bounds
, hull
);
1307 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1309 depth
= isl_ast_build_get_depth(build
);
1310 sub_build
= isl_ast_build_copy(build
);
1311 sub_build
= isl_ast_build_include_stride(sub_build
);
1312 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1313 isl_basic_set_copy(bounds
));
1314 degenerate
= isl_ast_build_has_value(sub_build
);
1315 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1316 if (degenerate
< 0 || eliminated
< 0)
1317 executed
= isl_union_map_free(executed
);
1319 executed
= eliminate(executed
, depth
, build
);
1321 node
= create_for(build
, degenerate
);
1323 body_build
= isl_ast_build_copy(sub_build
);
1324 body_build
= isl_ast_build_increase_depth(body_build
);
1326 node
= before_each_for(node
, body_build
);
1327 children
= generate_next_level(executed
,
1328 isl_ast_build_copy(body_build
));
1330 graft
= isl_ast_graft_alloc_level(children
, build
, sub_build
);
1332 graft
= isl_ast_graft_insert_for(graft
, node
);
1334 graft
= refine_eliminated(graft
, bounds
, build
);
1335 else if (degenerate
)
1336 graft
= refine_degenerate(graft
, bounds
, build
, sub_build
);
1338 graft
= refine_generic(graft
, bounds
, domain
, build
);
1340 graft
= after_each_for(graft
, body_build
);
1342 isl_ast_build_free(body_build
);
1343 isl_ast_build_free(sub_build
);
1344 isl_ast_build_free(build
);
1345 isl_basic_set_free(bounds
);
1346 isl_set_free(domain
);
1351 /* Internal data structure for checking if all constraints involving
1352 * the input dimension "depth" are such that the other coefficients
1353 * are multiples of "m", reducing "m" if they are not.
1354 * If "m" is reduced all the way down to "1", then the check has failed
1355 * and we break out of the iteration.
1356 * "d" is an initialized isl_int that can be used internally.
1358 struct isl_check_scaled_data
{
1363 /* If constraint "c" involves the input dimension data->depth,
1364 * then make sure that all the other coefficients are multiples of data->m,
1365 * reducing data->m if needed.
1366 * Break out of the iteration if data->m has become equal to "1".
1368 static int constraint_check_scaled(__isl_take isl_constraint
*c
, void *user
)
1370 struct isl_check_scaled_data
*data
= user
;
1372 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1375 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1376 isl_constraint_free(c
);
1380 for (i
= 0; i
< 4; ++i
) {
1381 n
= isl_constraint_dim(c
, t
[i
]);
1382 for (j
= 0; j
< n
; ++j
) {
1383 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1385 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1387 isl_constraint_get_coefficient(c
, t
[i
], j
, &data
->d
);
1388 isl_int_gcd(data
->m
, data
->m
, data
->d
);
1389 if (isl_int_is_one(data
->m
))
1396 isl_constraint_free(c
);
1398 return i
< 4 ? -1 : 0;
1401 /* For each constraint of "bmap" that involves the input dimension data->depth,
1402 * make sure that all the other coefficients are multiples of data->m,
1403 * reducing data->m if needed.
1404 * Break out of the iteration if data->m has become equal to "1".
1406 static int basic_map_check_scaled(__isl_take isl_basic_map
*bmap
, void *user
)
1410 r
= isl_basic_map_foreach_constraint(bmap
,
1411 &constraint_check_scaled
, user
);
1412 isl_basic_map_free(bmap
);
1417 /* For each constraint of "map" that involves the input dimension data->depth,
1418 * make sure that all the other coefficients are multiples of data->m,
1419 * reducing data->m if needed.
1420 * Break out of the iteration if data->m has become equal to "1".
1422 static int map_check_scaled(__isl_take isl_map
*map
, void *user
)
1426 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1432 /* Create an AST node for the current dimension based on
1433 * the schedule domain "bounds" and return the node encapsulated
1434 * in an isl_ast_graft.
1436 * "executed" is the current inverse schedule, taking into account
1437 * the bounds in "bounds"
1438 * "domain" is the domain of "executed", with inner dimensions projected out.
1441 * Before moving on to the actual AST node construction in create_node_scaled,
1442 * we first check if the current dimension is strided and if we can scale
1443 * down this stride. Note that we only do this if the ast_build_scale_strides
1446 * In particular, let the current dimension take on values
1450 * with a an integer. We check if we can find an integer m that (obviouly)
1451 * divides both f and s.
1453 * If so, we check if the current dimension only appears in constraints
1454 * where the coefficients of the other variables are multiples of m.
1455 * We perform this extra check to avoid the risk of introducing
1456 * divisions by scaling down the current dimension.
1458 * If so, we scale the current dimension down by a factor of m.
1459 * That is, we plug in
1463 * Note that in principle we could always scale down strided loops
1468 * but this may result in i' taking on larger values than the original i,
1469 * due to the shift by "f".
1470 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1472 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1473 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1474 __isl_take isl_ast_build
*build
)
1476 struct isl_check_scaled_data data
;
1480 ctx
= isl_ast_build_get_ctx(build
);
1481 if (!isl_options_get_ast_build_scale_strides(ctx
))
1482 return create_node_scaled(executed
, bounds
, domain
, build
);
1484 data
.depth
= isl_ast_build_get_depth(build
);
1485 if (!isl_ast_build_has_stride(build
, data
.depth
))
1486 return create_node_scaled(executed
, bounds
, domain
, build
);
1488 isl_int_init(data
.m
);
1489 isl_int_init(data
.d
);
1491 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1492 if (isl_ast_build_get_stride(build
, data
.depth
, &data
.m
) < 0)
1493 offset
= isl_aff_free(offset
);
1494 offset
= isl_aff_scale_down(offset
, data
.m
);
1495 if (isl_aff_get_denominator(offset
, &data
.d
) < 0)
1496 executed
= isl_union_map_free(executed
);
1498 if (executed
&& isl_int_is_divisible_by(data
.m
, data
.d
))
1499 isl_int_divexact(data
.m
, data
.m
, data
.d
);
1501 isl_int_set_si(data
.m
, 1);
1503 if (!isl_int_is_one(data
.m
)) {
1504 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1506 !isl_int_is_one(data
.m
))
1507 executed
= isl_union_map_free(executed
);
1510 if (!isl_int_is_one(data
.m
)) {
1515 isl_union_map
*umap
;
1517 space
= isl_ast_build_get_space(build
, 1);
1518 space
= isl_space_map_from_set(space
);
1519 ma
= isl_multi_aff_identity(space
);
1520 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1521 aff
= isl_aff_scale(aff
, data
.m
);
1522 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1524 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1525 isl_multi_aff_copy(ma
));
1526 domain
= isl_set_preimage_multi_aff(domain
,
1527 isl_multi_aff_copy(ma
));
1528 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1529 umap
= isl_union_map_from_map(map
);
1530 executed
= isl_union_map_apply_domain(executed
,
1531 isl_union_map_copy(umap
));
1532 build
= isl_ast_build_scale_down(build
, data
.m
, umap
);
1534 isl_aff_free(offset
);
1536 isl_int_clear(data
.d
);
1537 isl_int_clear(data
.m
);
1539 return create_node_scaled(executed
, bounds
, domain
, build
);
1542 /* Add the basic set to the list that "user" points to.
1544 static int collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1546 isl_basic_set_list
**list
= user
;
1548 *list
= isl_basic_set_list_add(*list
, bset
);
1553 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1555 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1556 __isl_take isl_set
*set
)
1560 isl_basic_set_list
*list
;
1565 ctx
= isl_set_get_ctx(set
);
1567 n
= isl_set_n_basic_set(set
);
1568 list
= isl_basic_set_list_alloc(ctx
, n
);
1569 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1570 list
= isl_basic_set_list_free(list
);
1576 /* Generate code for the schedule domain "bounds"
1577 * and add the result to "list".
1579 * We mainly detect strides and additional equalities here
1580 * and then pass over control to create_node.
1582 * "bounds" reflects the bounds on the current dimension and possibly
1583 * some extra conditions on outer dimensions.
1584 * It does not, however, include any divs involving the current dimension,
1585 * so it does not capture any stride constraints.
1586 * We therefore need to compute that part of the schedule domain that
1587 * intersects with "bounds" and derive the strides from the result.
1589 static __isl_give isl_ast_graft_list
*add_node(
1590 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1591 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1593 isl_ast_graft
*graft
;
1594 isl_set
*domain
= NULL
;
1595 isl_union_set
*uset
;
1598 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1599 executed
= isl_union_map_intersect_domain(executed
, uset
);
1600 empty
= isl_union_map_is_empty(executed
);
1606 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1607 domain
= isl_set_from_union_set(uset
);
1608 domain
= isl_ast_build_compute_gist(build
, domain
);
1609 empty
= isl_set_is_empty(domain
);
1615 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1616 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1618 graft
= create_node(executed
, bounds
, domain
,
1619 isl_ast_build_copy(build
));
1620 list
= isl_ast_graft_list_add(list
, graft
);
1621 isl_ast_build_free(build
);
1624 list
= isl_ast_graft_list_free(list
);
1626 isl_set_free(domain
);
1627 isl_basic_set_free(bounds
);
1628 isl_union_map_free(executed
);
1629 isl_ast_build_free(build
);
1633 struct isl_domain_follows_at_depth_data
{
1635 isl_basic_set
**piece
;
1638 /* Does any element of i follow or coincide with any element of j
1639 * at the current depth (data->depth) for equal values of the outer
1642 static int domain_follows_at_depth(int i
, int j
, void *user
)
1644 struct isl_domain_follows_at_depth_data
*data
= user
;
1645 isl_basic_map
*test
;
1649 test
= isl_basic_map_from_domain_and_range(
1650 isl_basic_set_copy(data
->piece
[i
]),
1651 isl_basic_set_copy(data
->piece
[j
]));
1652 for (l
= 0; l
< data
->depth
; ++l
)
1653 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1655 test
= isl_basic_map_order_ge(test
, isl_dim_in
, data
->depth
,
1656 isl_dim_out
, data
->depth
);
1657 empty
= isl_basic_map_is_empty(test
);
1658 isl_basic_map_free(test
);
1660 return empty
< 0 ? -1 : !empty
;
1663 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1664 __isl_keep isl_basic_set_list
*domain_list
,
1665 __isl_keep isl_union_map
*executed
,
1666 __isl_keep isl_ast_build
*build
);
1668 /* Generate code for the "n" schedule domains in "domain_list"
1669 * with positions specified by the entries of the "pos" array
1670 * and add the results to "list".
1672 * The "n" domains form a strongly connected component in the ordering.
1673 * If n is larger than 1, then this means that we cannot determine a valid
1674 * ordering for the n domains in the component. This should be fairly
1675 * rare because the individual domains have been made disjoint first.
1676 * The problem is that the domains may be integrally disjoint but not
1677 * rationally disjoint. For example, we may have domains
1679 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1681 * These two domains have an empty intersection, but their rational
1682 * relaxations do intersect. It is impossible to order these domains
1683 * in the second dimension because the first should be ordered before
1684 * the second for outer dimension equal to 0, while it should be ordered
1685 * after for outer dimension equal to 1.
1687 * This may happen in particular in case of unrolling since the domain
1688 * of each slice is replaced by its simple hull.
1690 * We collect the basic sets in the component, call isl_set_make_disjoint
1691 * and try again. Note that we rely here on isl_set_make_disjoint also
1692 * making the basic sets rationally disjoint. If the basic sets
1693 * are rationally disjoint, then the ordering problem does not occur.
1694 * To see this, there can only be a problem if there are points
1695 * (i,a) and (j,b) in one set and (i,c) and (j,d) in the other with
1696 * a < c and b > d. This means that either the interval spanned
1697 * by a en b lies inside that spanned by c and or the other way around.
1698 * In either case, there is a point inside both intervals with the
1699 * convex combination in terms of a and b and in terms of c and d.
1700 * Taking the same combination of i and j gives a point in the intersection.
1702 static __isl_give isl_ast_graft_list
*add_nodes(
1703 __isl_take isl_ast_graft_list
*list
, int *pos
, int n
,
1704 __isl_keep isl_basic_set_list
*domain_list
,
1705 __isl_keep isl_union_map
*executed
,
1706 __isl_keep isl_ast_build
*build
)
1709 isl_basic_set
*bset
;
1712 bset
= isl_basic_set_list_get_basic_set(domain_list
, pos
[0]);
1714 return add_node(list
, isl_union_map_copy(executed
), bset
,
1715 isl_ast_build_copy(build
));
1717 set
= isl_set_from_basic_set(bset
);
1718 for (i
= 1; i
< n
; ++i
) {
1719 bset
= isl_basic_set_list_get_basic_set(domain_list
, pos
[i
]);
1720 set
= isl_set_union(set
, isl_set_from_basic_set(bset
));
1723 set
= isl_set_make_disjoint(set
);
1724 if (isl_set_n_basic_set(set
) == n
)
1725 isl_die(isl_ast_graft_list_get_ctx(list
), isl_error_internal
,
1726 "unable to separate loop parts", goto error
);
1727 domain_list
= isl_basic_set_list_from_set(set
);
1728 list
= isl_ast_graft_list_concat(list
,
1729 generate_sorted_domains(domain_list
, executed
, build
));
1730 isl_basic_set_list_free(domain_list
);
1735 return isl_ast_graft_list_free(list
);
1738 /* Sort the domains in "domain_list" according to the execution order
1739 * at the current depth (for equal values of the outer dimensions),
1740 * generate code for each of them, collecting the results in a list.
1741 * If no code is generated (because the intersection of the inverse schedule
1742 * with the domains turns out to be empty), then an empty list is returned.
1744 * The caller is responsible for ensuring that the basic sets in "domain_list"
1745 * are pair-wise disjoint. It can, however, in principle happen that
1746 * two basic sets should be ordered one way for one value of the outer
1747 * dimensions and the other way for some other value of the outer dimensions.
1748 * We therefore play safe and look for strongly connected components.
1749 * The function add_nodes takes care of handling non-trivial components.
1751 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1752 __isl_keep isl_basic_set_list
*domain_list
,
1753 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
1756 isl_ast_graft_list
*list
;
1757 struct isl_domain_follows_at_depth_data data
;
1758 struct isl_tarjan_graph
*g
;
1764 ctx
= isl_basic_set_list_get_ctx(domain_list
);
1765 n
= isl_basic_set_list_n_basic_set(domain_list
);
1766 list
= isl_ast_graft_list_alloc(ctx
, n
);
1770 return add_node(list
, isl_union_map_copy(executed
),
1771 isl_basic_set_list_get_basic_set(domain_list
, 0),
1772 isl_ast_build_copy(build
));
1774 data
.depth
= isl_ast_build_get_depth(build
);
1775 data
.piece
= domain_list
->p
;
1776 g
= isl_tarjan_graph_init(ctx
, n
, &domain_follows_at_depth
, &data
);
1784 if (g
->order
[i
] == -1)
1785 isl_die(ctx
, isl_error_internal
, "cannot happen",
1788 while (g
->order
[i
] != -1) {
1791 list
= add_nodes(list
, g
->order
+ first
, i
- first
,
1792 domain_list
, executed
, build
);
1797 error
: list
= isl_ast_graft_list_free(list
);
1798 isl_tarjan_graph_free(g
);
1803 struct isl_shared_outer_data
{
1805 isl_basic_set
**piece
;
1808 /* Do elements i and j share any values for the outer dimensions?
1810 static int shared_outer(int i
, int j
, void *user
)
1812 struct isl_shared_outer_data
*data
= user
;
1813 isl_basic_map
*test
;
1817 test
= isl_basic_map_from_domain_and_range(
1818 isl_basic_set_copy(data
->piece
[i
]),
1819 isl_basic_set_copy(data
->piece
[j
]));
1820 for (l
= 0; l
< data
->depth
; ++l
)
1821 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1823 empty
= isl_basic_map_is_empty(test
);
1824 isl_basic_map_free(test
);
1826 return empty
< 0 ? -1 : !empty
;
1829 /* Call generate_sorted_domains on a list containing the elements
1830 * of "domain_list indexed by the first "n" elements of "pos".
1832 static __isl_give isl_ast_graft_list
*generate_sorted_domains_part(
1833 __isl_keep isl_basic_set_list
*domain_list
, int *pos
, int n
,
1834 __isl_keep isl_union_map
*executed
,
1835 __isl_keep isl_ast_build
*build
)
1839 isl_basic_set_list
*slice
;
1840 isl_ast_graft_list
*list
;
1842 ctx
= isl_ast_build_get_ctx(build
);
1843 slice
= isl_basic_set_list_alloc(ctx
, n
);
1844 for (i
= 0; i
< n
; ++i
) {
1845 isl_basic_set
*bset
;
1847 bset
= isl_basic_set_copy(domain_list
->p
[pos
[i
]]);
1848 slice
= isl_basic_set_list_add(slice
, bset
);
1851 list
= generate_sorted_domains(slice
, executed
, build
);
1852 isl_basic_set_list_free(slice
);
1857 /* Look for any (weakly connected) components in the "domain_list"
1858 * of domains that share some values of the outer dimensions.
1859 * That is, domains in different components do not share any values
1860 * of the outer dimensions. This means that these components
1861 * can be freely reorderd.
1862 * Within each of the components, we sort the domains according
1863 * to the execution order at the current depth.
1865 * We fuse the result of each call to generate_sorted_domains_part
1866 * into a list with either zero or one graft and collect these (at most)
1867 * single element lists into a bigger list. This means that the elements of the
1868 * final list can be freely reordered. In particular, we sort them
1869 * according to an arbitrary but fixed ordering to ease merging of
1870 * graft lists from different components.
1872 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
1873 __isl_keep isl_basic_set_list
*domain_list
,
1874 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
1878 isl_ast_graft_list
*list
;
1879 struct isl_shared_outer_data data
;
1880 struct isl_tarjan_graph
*g
;
1885 n
= isl_basic_set_list_n_basic_set(domain_list
);
1887 return generate_sorted_domains(domain_list
, executed
, build
);
1889 ctx
= isl_basic_set_list_get_ctx(domain_list
);
1891 data
.depth
= isl_ast_build_get_depth(build
);
1892 data
.piece
= domain_list
->p
;
1893 g
= isl_tarjan_graph_init(ctx
, n
, &shared_outer
, &data
);
1900 isl_ast_graft_list
*list_c
;
1902 if (g
->order
[i
] == -1)
1903 isl_die(ctx
, isl_error_internal
, "cannot happen",
1906 while (g
->order
[i
] != -1) {
1909 if (first
== 0 && n
== 0) {
1910 isl_tarjan_graph_free(g
);
1911 return generate_sorted_domains(domain_list
,
1914 list_c
= generate_sorted_domains_part(domain_list
,
1915 g
->order
+ first
, i
- first
, executed
, build
);
1916 list_c
= isl_ast_graft_list_fuse(list_c
, build
);
1920 list
= isl_ast_graft_list_concat(list
, list_c
);
1922 } while (list
&& n
);
1925 list
= isl_ast_graft_list_free(list
);
1927 list
= isl_ast_graft_list_sort(list
);
1929 isl_tarjan_graph_free(g
);
1934 /* Internal data for separate_domain.
1936 * "explicit" is set if we only want to use explicit bounds.
1938 * "domain" collects the separated domains.
1940 struct isl_separate_domain_data
{
1941 isl_ast_build
*build
;
1946 /* Extract implicit bounds on the current dimension for the executed "map".
1948 * The domain of "map" may involve inner dimensions, so we
1949 * need to eliminate them.
1951 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
1952 __isl_keep isl_ast_build
*build
)
1956 domain
= isl_map_domain(map
);
1957 domain
= isl_ast_build_eliminate(build
, domain
);
1962 /* Extract explicit bounds on the current dimension for the executed "map".
1964 * Rather than eliminating the inner dimensions as in implicit_bounds,
1965 * we simply drop any constraints involving those inner dimensions.
1966 * The idea is that most bounds that are implied by constraints on the
1967 * inner dimensions will be enforced by for loops and not by explicit guards.
1968 * There is then no need to separate along those bounds.
1970 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
1971 __isl_keep isl_ast_build
*build
)
1976 dim
= isl_map_dim(map
, isl_dim_out
);
1977 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
1979 domain
= isl_map_domain(map
);
1980 depth
= isl_ast_build_get_depth(build
);
1981 dim
= isl_set_dim(domain
, isl_dim_set
);
1982 domain
= isl_set_detect_equalities(domain
);
1983 domain
= isl_set_drop_constraints_involving_dims(domain
,
1984 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
1985 domain
= isl_set_remove_divs_involving_dims(domain
,
1986 isl_dim_set
, depth
, 1);
1987 domain
= isl_set_remove_unknown_divs(domain
);
1992 /* Split data->domain into pieces that intersect with the range of "map"
1993 * and pieces that do not intersect with the range of "map"
1994 * and then add that part of the range of "map" that does not intersect
1995 * with data->domain.
1997 static int separate_domain(__isl_take isl_map
*map
, void *user
)
1999 struct isl_separate_domain_data
*data
= user
;
2004 domain
= explicit_bounds(map
, data
->build
);
2006 domain
= implicit_bounds(map
, data
->build
);
2008 domain
= isl_set_coalesce(domain
);
2009 domain
= isl_set_make_disjoint(domain
);
2010 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2011 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2012 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2013 data
->domain
= isl_set_union(data
->domain
, d1
);
2014 data
->domain
= isl_set_union(data
->domain
, d2
);
2019 /* Separate the schedule domains of "executed".
2021 * That is, break up the domain of "executed" into basic sets,
2022 * such that for each basic set S, every element in S is associated with
2023 * the same domain spaces.
2025 * "space" is the (single) domain space of "executed".
2027 static __isl_give isl_set
*separate_schedule_domains(
2028 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2029 __isl_keep isl_ast_build
*build
)
2031 struct isl_separate_domain_data data
= { build
};
2034 ctx
= isl_ast_build_get_ctx(build
);
2035 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2036 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2037 data
.domain
= isl_set_empty(space
);
2038 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2039 data
.domain
= isl_set_free(data
.domain
);
2041 isl_union_map_free(executed
);
2045 /* Temporary data used during the search for a lower bound for unrolling.
2047 * "domain" is the original set for which to find a lower bound
2048 * "depth" is the dimension for which to find a lower boudn
2050 * "lower" is the best lower bound found so far. It is NULL if we have not
2052 * "n" is the corresponding size. If lower is NULL, then the value of n
2055 * "tmp" is a temporary initialized isl_int.
2057 struct isl_find_unroll_data
{
2066 /* Check if we can use "c" as a lower bound and if it is better than
2067 * any previously found lower bound.
2069 * If "c" does not involve the dimension at the current depth,
2070 * then we cannot use it.
2071 * Otherwise, let "c" be of the form
2075 * We compute the maximal value of
2077 * -ceil(f(j)/a)) + i + 1
2079 * over the domain. If there is such a value "n", then we know
2081 * -ceil(f(j)/a)) + i + 1 <= n
2085 * i < ceil(f(j)/a)) + n
2087 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2088 * We just need to check if we have found any lower bound before and
2089 * if the new lower bound is better (smaller n) than the previously found
2092 static int update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2093 __isl_keep isl_constraint
*c
)
2095 isl_aff
*aff
, *lower
;
2096 enum isl_lp_result res
;
2098 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2101 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2102 lower
= isl_aff_ceil(lower
);
2103 aff
= isl_aff_copy(lower
);
2104 aff
= isl_aff_neg(aff
);
2105 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2106 aff
= isl_aff_add_constant_si(aff
, 1);
2107 res
= isl_set_max(data
->domain
, aff
, &data
->tmp
);
2110 if (res
== isl_lp_error
)
2112 if (res
== isl_lp_unbounded
) {
2113 isl_aff_free(lower
);
2117 if (isl_int_cmp_si(data
->tmp
, INT_MAX
) <= 0 &&
2118 (!data
->lower
|| isl_int_cmp_si(data
->tmp
, *data
->n
) < 0)) {
2119 isl_aff_free(data
->lower
);
2120 data
->lower
= lower
;
2121 *data
->n
= isl_int_get_si(data
->tmp
);
2123 isl_aff_free(lower
);
2127 isl_aff_free(lower
);
2131 /* Check if we can use "c" as a lower bound and if it is better than
2132 * any previously found lower bound.
2134 static int constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2136 struct isl_find_unroll_data
*data
;
2139 data
= (struct isl_find_unroll_data
*) user
;
2140 r
= update_unrolling_lower_bound(data
, c
);
2141 isl_constraint_free(c
);
2146 /* Look for a lower bound l(i) on the dimension at "depth"
2147 * and a size n such that "domain" is a subset of
2149 * { [i] : l(i) <= i_d < l(i) + n }
2151 * where d is "depth" and l(i) depends only on earlier dimensions.
2152 * Furthermore, try and find a lower bound such that n is as small as possible.
2153 * In particular, "n" needs to be finite.
2155 * Inner dimensions have been eliminated from "domain" by the caller.
2157 * We first construct a collection of lower bounds on the input set
2158 * by computing its simple hull. We then iterate through them,
2159 * discarding those that we cannot use (either because they do not
2160 * involve the dimension at "depth" or because they have no corresponding
2161 * upper bound, meaning that "n" would be unbounded) and pick out the
2162 * best from the remaining ones.
2164 * If we cannot find a suitable lower bound, then we consider that
2167 static __isl_give isl_aff
*find_unroll_lower_bound(__isl_keep isl_set
*domain
,
2170 struct isl_find_unroll_data data
= { domain
, depth
, NULL
, n
};
2171 isl_basic_set
*hull
;
2173 isl_int_init(data
.tmp
);
2174 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2176 if (isl_basic_set_foreach_constraint(hull
,
2177 &constraint_find_unroll
, &data
) < 0)
2180 isl_basic_set_free(hull
);
2181 isl_int_clear(data
.tmp
);
2184 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2185 "cannot find lower bound for unrolling", return NULL
);
2189 isl_basic_set_free(hull
);
2190 isl_int_clear(data
.tmp
);
2191 return isl_aff_free(data
.lower
);
2194 /* Return the constraint
2196 * i_"depth" = aff + offset
2198 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2201 aff
= isl_aff_copy(aff
);
2202 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2203 aff
= isl_aff_add_constant_si(aff
, offset
);
2204 return isl_equality_from_aff(aff
);
2207 /* Return a list of basic sets, one for each value of the current dimension
2209 * The divs that involve the current dimension have not been projected out
2212 * Since we are going to be iterating over the individual values,
2213 * we first check if there are any strides on the current dimension.
2214 * If there is, we rewrite the current dimension i as
2216 * i = stride i' + offset
2218 * and then iterate over individual values of i' instead.
2220 * We then look for a lower bound on i' and a size such that the domain
2223 * { [j,i'] : l(j) <= i' < l(j) + n }
2225 * and then take slices of the domain at values of i'
2226 * between l(j) and l(j) + n - 1.
2228 * We compute the unshifted simple hull of each slice to ensure that
2229 * we have a single basic set per offset. The slicing constraint
2230 * may get simplified away before the unshifted simple hull is taken
2231 * and may therefore in some rare cases disappear from the result.
2232 * We therefore explicitly add the constraint back after computing
2233 * the unshifted simple hull to ensure that the basic sets
2234 * remain disjoint. The constraints that are dropped by taking the hull
2235 * will be taken into account at the next level, as in the case of the
2238 * Finally, we map i' back to i and add each basic set to the list.
2240 static __isl_give isl_basic_set_list
*do_unroll(__isl_take isl_set
*domain
,
2241 __isl_keep isl_ast_build
*build
)
2247 isl_basic_set_list
*list
;
2248 isl_multi_aff
*expansion
;
2249 isl_basic_map
*bmap
;
2254 ctx
= isl_set_get_ctx(domain
);
2255 depth
= isl_ast_build_get_depth(build
);
2256 build
= isl_ast_build_copy(build
);
2257 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2258 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
2259 expansion
= isl_ast_build_get_stride_expansion(build
);
2261 domain
= isl_set_preimage_multi_aff(domain
,
2262 isl_multi_aff_copy(expansion
));
2263 domain
= isl_ast_build_eliminate_divs(build
, domain
);
2265 isl_ast_build_free(build
);
2267 list
= isl_basic_set_list_alloc(ctx
, 0);
2269 lower
= find_unroll_lower_bound(domain
, depth
, &n
);
2271 list
= isl_basic_set_list_free(list
);
2273 bmap
= isl_basic_map_from_multi_aff(expansion
);
2275 for (i
= 0; list
&& i
< n
; ++i
) {
2277 isl_basic_set
*bset
;
2278 isl_constraint
*slice
;
2280 slice
= at_offset(depth
, lower
, i
);
2281 set
= isl_set_copy(domain
);
2282 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2283 bset
= isl_set_unshifted_simple_hull(set
);
2284 bset
= isl_basic_set_add_constraint(bset
, slice
);
2285 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2286 list
= isl_basic_set_list_add(list
, bset
);
2289 isl_aff_free(lower
);
2290 isl_set_free(domain
);
2291 isl_basic_map_free(bmap
);
2296 /* Data structure for storing the results and the intermediate objects
2297 * of compute_domains.
2299 * "list" is the main result of the function and contains a list
2300 * of disjoint basic sets for which code should be generated.
2302 * "executed" and "build" are inputs to compute_domains.
2303 * "schedule_domain" is the domain of "executed".
2305 * "option" constains the domains at the current depth that should by
2306 * atomic, separated or unrolled. These domains are as specified by
2307 * the user, except that inner dimensions have been eliminated and
2308 * that they have been made pair-wise disjoint.
2310 * "sep_class" contains the user-specified split into separation classes
2311 * specialized to the current depth.
2312 * "done" contains the union of th separation domains that have already
2314 * "atomic" contains the domain that has effectively been made atomic.
2315 * This domain may be larger than the intersection of option[atomic]
2316 * and the schedule domain.
2318 struct isl_codegen_domains
{
2319 isl_basic_set_list
*list
;
2321 isl_union_map
*executed
;
2322 isl_ast_build
*build
;
2323 isl_set
*schedule_domain
;
2332 /* Add domains to domains->list for each individual value of the current
2333 * dimension, for that part of the schedule domain that lies in the
2334 * intersection of the option domain and the class domain.
2336 * "domain" is the intersection of the class domain and the schedule domain.
2337 * The divs that involve the current dimension have not been projected out
2340 * We first break up the unroll option domain into individual pieces
2341 * and then handle each of them separately. The unroll option domain
2342 * has been made disjoint in compute_domains_init_options,
2344 * Note that we actively want to combine different pieces of the
2345 * schedule domain that have the same value at the current dimension.
2346 * We therefore need to break up the unroll option domain before
2347 * intersecting with class and schedule domain, hoping that the
2348 * unroll option domain specified by the user is relatively simple.
2350 static int compute_unroll_domains(struct isl_codegen_domains
*domains
,
2351 __isl_keep isl_set
*domain
)
2353 isl_set
*unroll_domain
;
2354 isl_basic_set_list
*unroll_list
;
2358 empty
= isl_set_is_empty(domains
->option
[unroll
]);
2364 unroll_domain
= isl_set_copy(domains
->option
[unroll
]);
2365 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2367 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2368 for (i
= 0; i
< n
; ++i
) {
2369 isl_basic_set
*bset
;
2370 isl_basic_set_list
*list
;
2372 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2373 unroll_domain
= isl_set_from_basic_set(bset
);
2374 unroll_domain
= isl_set_intersect(unroll_domain
,
2375 isl_set_copy(domain
));
2377 empty
= isl_set_is_empty(unroll_domain
);
2378 if (empty
>= 0 && empty
) {
2379 isl_set_free(unroll_domain
);
2383 list
= do_unroll(unroll_domain
, domains
->build
);
2384 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2387 isl_basic_set_list_free(unroll_list
);
2392 /* Construct a single basic set that includes the intersection of
2393 * the schedule domain, the atomic option domain and the class domain.
2394 * Add the resulting basic set to domains->list and save a copy
2395 * in domains->atomic for use in compute_partial_domains.
2397 * We construct a single domain rather than trying to combine
2398 * the schedule domains of individual domains because we are working
2399 * within a single component so that non-overlapping schedule domains
2400 * should already have been separated.
2401 * Note, though, that this does not take into account the class domain.
2402 * So, it is possible for a class domain to carve out a piece of the
2403 * schedule domain with independent pieces and then we would only
2404 * generate a single domain for them. If this proves to be problematic
2405 * for some users, then this function will have to be adjusted.
2407 * "domain" is the intersection of the schedule domain and the class domain,
2408 * with inner dimensions projected out.
2410 static int compute_atomic_domain(struct isl_codegen_domains
*domains
,
2411 __isl_keep isl_set
*domain
)
2413 isl_basic_set
*bset
;
2414 isl_set
*atomic_domain
;
2417 atomic_domain
= isl_set_copy(domains
->option
[atomic
]);
2418 atomic_domain
= isl_set_intersect(atomic_domain
, isl_set_copy(domain
));
2419 empty
= isl_set_is_empty(atomic_domain
);
2420 if (empty
< 0 || empty
) {
2421 domains
->atomic
= atomic_domain
;
2422 return empty
< 0 ? -1 : 0;
2425 atomic_domain
= isl_set_coalesce(atomic_domain
);
2426 bset
= isl_set_unshifted_simple_hull(atomic_domain
);
2427 domains
->atomic
= isl_set_from_basic_set(isl_basic_set_copy(bset
));
2428 domains
->list
= isl_basic_set_list_add(domains
->list
, bset
);
2433 /* Split up the schedule domain into uniform basic sets,
2434 * in the sense that each element in a basic set is associated to
2435 * elements of the same domains, and add the result to domains->list.
2436 * Do this for that part of the schedule domain that lies in the
2437 * intersection of "class_domain" and the separate option domain.
2439 * "class_domain" may or may not include the constraints
2440 * of the schedule domain, but this does not make a difference
2441 * since we are going to intersect it with the domain of the inverse schedule.
2442 * If it includes schedule domain constraints, then they may involve
2443 * inner dimensions, but we will eliminate them in separation_domain.
2445 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2446 __isl_keep isl_set
*class_domain
)
2450 isl_union_map
*executed
;
2451 isl_basic_set_list
*list
;
2454 domain
= isl_set_copy(domains
->option
[separate
]);
2455 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2456 executed
= isl_union_map_copy(domains
->executed
);
2457 executed
= isl_union_map_intersect_domain(executed
,
2458 isl_union_set_from_set(domain
));
2459 empty
= isl_union_map_is_empty(executed
);
2460 if (empty
< 0 || empty
) {
2461 isl_union_map_free(executed
);
2462 return empty
< 0 ? -1 : 0;
2465 space
= isl_set_get_space(class_domain
);
2466 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2468 list
= isl_basic_set_list_from_set(domain
);
2469 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2474 /* Split up the domain at the current depth into disjoint
2475 * basic sets for which code should be generated separately
2476 * for the given separation class domain.
2478 * If any separation classes have been defined, then "class_domain"
2479 * is the domain of the current class and does not refer to inner dimensions.
2480 * Otherwise, "class_domain" is the universe domain.
2482 * We first make sure that the class domain is disjoint from
2483 * previously considered class domains.
2485 * The separate domains can be computed directly from the "class_domain".
2487 * The unroll, atomic and remainder domains need the constraints
2488 * from the schedule domain.
2490 * For unrolling, the actual schedule domain is needed (with divs that
2491 * may refer to the current dimension) so that stride detection can be
2494 * For atomic and remainder domains, inner dimensions and divs involving
2495 * the current dimensions should be eliminated.
2496 * In case we are working within a separation class, we need to intersect
2497 * the result with the current "class_domain" to ensure that the domains
2498 * are disjoint from those generated from other class domains.
2500 * The domain that has been made atomic may be larger than specified
2501 * by the user since it needs to be representable as a single basic set.
2502 * This possibly larger domain is stored in domains->atomic by
2503 * compute_atomic_domain.
2505 * If anything is left after handling separate, unroll and atomic,
2506 * we split it up into basic sets and append the basic sets to domains->list.
2508 static int compute_partial_domains(struct isl_codegen_domains
*domains
,
2509 __isl_take isl_set
*class_domain
)
2511 isl_basic_set_list
*list
;
2514 class_domain
= isl_set_subtract(class_domain
,
2515 isl_set_copy(domains
->done
));
2516 domains
->done
= isl_set_union(domains
->done
,
2517 isl_set_copy(class_domain
));
2519 domain
= isl_set_copy(class_domain
);
2521 if (compute_separate_domain(domains
, domain
) < 0)
2523 domain
= isl_set_subtract(domain
,
2524 isl_set_copy(domains
->option
[separate
]));
2526 domain
= isl_set_intersect(domain
,
2527 isl_set_copy(domains
->schedule_domain
));
2529 if (compute_unroll_domains(domains
, domain
) < 0)
2531 domain
= isl_set_subtract(domain
,
2532 isl_set_copy(domains
->option
[unroll
]));
2534 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2535 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2537 if (compute_atomic_domain(domains
, domain
) < 0)
2538 domain
= isl_set_free(domain
);
2539 domain
= isl_set_subtract(domain
, domains
->atomic
);
2541 domain
= isl_set_coalesce(domain
);
2542 domain
= isl_set_make_disjoint(domain
);
2544 list
= isl_basic_set_list_from_set(domain
);
2545 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2547 isl_set_free(class_domain
);
2551 isl_set_free(domain
);
2552 isl_set_free(class_domain
);
2556 /* Split up the domain at the current depth into disjoint
2557 * basic sets for which code should be generated separately
2558 * for the separation class identified by "pnt".
2560 * We extract the corresponding class domain from domains->sep_class,
2561 * eliminate inner dimensions and pass control to compute_partial_domains.
2563 static int compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
2565 struct isl_codegen_domains
*domains
= user
;
2570 class_set
= isl_set_from_point(pnt
);
2571 domain
= isl_map_domain(isl_map_intersect_range(
2572 isl_map_copy(domains
->sep_class
), class_set
));
2573 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
2574 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2576 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
2580 isl_set_free(domain
);
2584 return compute_partial_domains(domains
, domain
);
2587 /* Extract the domains at the current depth that should be atomic,
2588 * separated or unrolled and store them in option.
2590 * The domains specified by the user might overlap, so we make
2591 * them disjoint by subtracting earlier domains from later domains.
2593 static void compute_domains_init_options(isl_set
*option
[3],
2594 __isl_keep isl_ast_build
*build
)
2596 enum isl_ast_build_domain_type type
, type2
;
2598 for (type
= atomic
; type
<= separate
; ++type
) {
2599 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
2600 for (type2
= atomic
; type2
< type
; ++type2
)
2601 option
[type
] = isl_set_subtract(option
[type
],
2602 isl_set_copy(option
[type2
]));
2605 option
[unroll
] = isl_set_coalesce(option
[unroll
]);
2606 option
[unroll
] = isl_set_make_disjoint(option
[unroll
]);
2609 /* Split up the domain at the current depth into disjoint
2610 * basic sets for which code should be generated separately,
2611 * based on the user-specified options.
2612 * Return the list of disjoint basic sets.
2614 * There are three kinds of domains that we need to keep track of.
2615 * - the "schedule domain" is the domain of "executed"
2616 * - the "class domain" is the domain corresponding to the currrent
2618 * - the "option domain" is the domain corresponding to one of the options
2619 * atomic, unroll or separate
2621 * We first consider the individial values of the separation classes
2622 * and split up the domain for each of them separately.
2623 * Finally, we consider the remainder. If no separation classes were
2624 * specified, then we call compute_partial_domains with the universe
2625 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2626 * with inner dimensions removed. We do this because we want to
2627 * avoid computing the complement of the class domains (i.e., the difference
2628 * between the universe and domains->done).
2630 static __isl_give isl_basic_set_list
*compute_domains(
2631 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2633 struct isl_codegen_domains domains
;
2636 isl_union_set
*schedule_domain
;
2640 enum isl_ast_build_domain_type type
;
2646 ctx
= isl_union_map_get_ctx(executed
);
2647 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
2649 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
2650 domain
= isl_set_from_union_set(schedule_domain
);
2652 compute_domains_init_options(domains
.option
, build
);
2654 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
2655 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
2656 n_param
= isl_set_dim(classes
, isl_dim_param
);
2657 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
2659 space
= isl_set_get_space(domain
);
2660 domains
.build
= build
;
2661 domains
.schedule_domain
= isl_set_copy(domain
);
2662 domains
.executed
= executed
;
2663 domains
.done
= isl_set_empty(space
);
2665 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
2666 domains
.list
= isl_basic_set_list_free(domains
.list
);
2667 isl_set_free(classes
);
2669 empty
= isl_set_is_empty(domains
.done
);
2671 domains
.list
= isl_basic_set_list_free(domains
.list
);
2672 domain
= isl_set_free(domain
);
2674 isl_set_free(domain
);
2675 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
2677 domain
= isl_ast_build_eliminate(build
, domain
);
2679 if (compute_partial_domains(&domains
, domain
) < 0)
2680 domains
.list
= isl_basic_set_list_free(domains
.list
);
2682 isl_set_free(domains
.schedule_domain
);
2683 isl_set_free(domains
.done
);
2684 isl_map_free(domains
.sep_class
);
2685 for (type
= atomic
; type
<= separate
; ++type
)
2686 isl_set_free(domains
.option
[type
]);
2688 return domains
.list
;
2691 /* Generate code for a single component, after shifting (if any)
2694 * We first split up the domain at the current depth into disjoint
2695 * basic sets based on the user-specified options.
2696 * Then we generated code for each of them and concatenate the results.
2698 static __isl_give isl_ast_graft_list
*generate_shifted_component(
2699 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
2701 isl_basic_set_list
*domain_list
;
2702 isl_ast_graft_list
*list
= NULL
;
2704 domain_list
= compute_domains(executed
, build
);
2705 list
= generate_parallel_domains(domain_list
, executed
, build
);
2707 isl_basic_set_list_free(domain_list
);
2708 isl_union_map_free(executed
);
2709 isl_ast_build_free(build
);
2714 struct isl_set_map_pair
{
2719 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2720 * of indices into the "domain" array,
2721 * return the union of the "map" fields of the elements
2722 * indexed by the first "n" elements of "order".
2724 static __isl_give isl_union_map
*construct_component_executed(
2725 struct isl_set_map_pair
*domain
, int *order
, int n
)
2729 isl_union_map
*executed
;
2731 map
= isl_map_copy(domain
[order
[0]].map
);
2732 executed
= isl_union_map_from_map(map
);
2733 for (i
= 1; i
< n
; ++i
) {
2734 map
= isl_map_copy(domain
[order
[i
]].map
);
2735 executed
= isl_union_map_add_map(executed
, map
);
2741 /* Generate code for a single component, after shifting (if any)
2744 * The component inverse schedule is specified as the "map" fields
2745 * of the elements of "domain" indexed by the first "n" elements of "order".
2747 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
2748 struct isl_set_map_pair
*domain
, int *order
, int n
,
2749 __isl_take isl_ast_build
*build
)
2751 isl_union_map
*executed
;
2753 executed
= construct_component_executed(domain
, order
, n
);
2754 return generate_shifted_component(executed
, build
);
2757 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2758 * of indices into the "domain" array,
2759 * do all (except for at most one) of the "set" field of the elements
2760 * indexed by the first "n" elements of "order" have a fixed value
2761 * at position "depth"?
2763 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
2764 int *order
, int n
, int depth
)
2769 for (i
= 0; i
< n
; ++i
) {
2772 f
= isl_set_plain_is_fixed(domain
[order
[i
]].set
,
2773 isl_dim_set
, depth
, NULL
);
2786 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2787 * of indices into the "domain" array,
2788 * eliminate the inner dimensions from the "set" field of the elements
2789 * indexed by the first "n" elements of "order", provided the current
2790 * dimension does not have a fixed value.
2792 * Return the index of the first element in "order" with a corresponding
2793 * "set" field that does not have an (obviously) fixed value.
2795 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
2796 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
2801 for (i
= n
- 1; i
>= 0; --i
) {
2803 f
= isl_set_plain_is_fixed(domain
[order
[i
]].set
,
2804 isl_dim_set
, depth
, NULL
);
2809 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
2810 domain
[order
[i
]].set
);
2817 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2818 * of indices into the "domain" array,
2819 * find the element of "domain" (amongst those indexed by the first "n"
2820 * elements of "order") with the "set" field that has the smallest
2821 * value for the current iterator.
2823 * Note that the domain with the smallest value may depend on the parameters
2824 * and/or outer loop dimension. Since the result of this function is only
2825 * used as heuristic, we only make a reasonable attempt at finding the best
2826 * domain, one that should work in case a single domain provides the smallest
2827 * value for the current dimension over all values of the parameters
2828 * and outer dimensions.
2830 * In particular, we compute the smallest value of the first domain
2831 * and replace it by that of any later domain if that later domain
2832 * has a smallest value that is smaller for at least some value
2833 * of the parameters and outer dimensions.
2835 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
2836 __isl_keep isl_ast_build
*build
)
2842 min_first
= isl_ast_build_map_to_iterator(build
,
2843 isl_set_copy(domain
[order
[0]].set
));
2844 min_first
= isl_map_lexmin(min_first
);
2846 for (i
= 1; i
< n
; ++i
) {
2847 isl_map
*min
, *test
;
2850 min
= isl_ast_build_map_to_iterator(build
,
2851 isl_set_copy(domain
[order
[i
]].set
));
2852 min
= isl_map_lexmin(min
);
2853 test
= isl_map_copy(min
);
2854 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
2855 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
2856 empty
= isl_map_is_empty(test
);
2858 if (empty
>= 0 && !empty
) {
2859 isl_map_free(min_first
);
2869 isl_map_free(min_first
);
2871 return i
< n
? -1 : first
;
2874 /* Construct a shifted inverse schedule based on the original inverse schedule,
2875 * the stride and the offset.
2877 * The original inverse schedule is specified as the "map" fields
2878 * of the elements of "domain" indexed by the first "n" elements of "order".
2880 * "stride" and "offset" are such that the difference
2881 * between the values of the current dimension of domain "i"
2882 * and the values of the current dimension for some reference domain are
2885 * stride * integer + offset[i]
2887 * Moreover, 0 <= offset[i] < stride.
2889 * For each domain, we create a map
2891 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
2893 * where j refers to the current dimension and the other dimensions are
2894 * unchanged, and apply this map to the original schedule domain.
2896 * For example, for the original schedule
2898 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
2900 * and assuming the offset is 0 for the A domain and 1 for the B domain,
2901 * we apply the mapping
2905 * to the schedule of the "A" domain and the mapping
2907 * { [j - 1] -> [j, 1] }
2909 * to the schedule of the "B" domain.
2912 * Note that after the transformation, the differences between pairs
2913 * of values of the current dimension over all domains are multiples
2914 * of stride and that we have therefore exposed the stride.
2917 * To see that the mapping preserves the lexicographic order,
2918 * first note that each of the individual maps above preserves the order.
2919 * If the value of the current iterator is j1 in one domain and j2 in another,
2920 * then if j1 = j2, we know that the same map is applied to both domains
2921 * and the order is preserved.
2922 * Otherwise, let us assume, without loss of generality, that j1 < j2.
2923 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
2927 * and the order is preserved.
2928 * If c1 < c2, then we know
2934 * j2 - j1 = n * s + r
2936 * with n >= 0 and 0 <= r < s.
2937 * In other words, r = c2 - c1.
2948 * (j1 - c1, c1) << (j2 - c2, c2)
2950 * with "<<" the lexicographic order, proving that the order is preserved
2953 static __isl_give isl_union_map
*contruct_shifted_executed(
2954 struct isl_set_map_pair
*domain
, int *order
, int n
, isl_int stride
,
2955 __isl_keep isl_vec
*offset
, __isl_keep isl_ast_build
*build
)
2959 isl_union_map
*executed
;
2965 depth
= isl_ast_build_get_depth(build
);
2966 space
= isl_ast_build_get_space(build
, 1);
2967 executed
= isl_union_map_empty(isl_space_copy(space
));
2968 space
= isl_space_map_from_set(space
);
2969 map
= isl_map_identity(isl_space_copy(space
));
2970 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
2971 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
2972 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
2974 c
= isl_equality_alloc(isl_local_space_from_space(space
));
2975 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
2976 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
2980 for (i
= 0; i
< n
; ++i
) {
2983 if (isl_vec_get_element(offset
, i
, &v
) < 0)
2985 map_i
= isl_map_copy(map
);
2986 map_i
= isl_map_fix(map_i
, isl_dim_out
, depth
+ 1, v
);
2988 c
= isl_constraint_set_constant(c
, v
);
2989 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
2991 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
2993 executed
= isl_union_map_add_map(executed
, map_i
);
2996 isl_constraint_free(c
);
3002 executed
= isl_union_map_free(executed
);
3007 /* Generate code for a single component, after exposing the stride,
3008 * given that the schedule domain is "shifted strided".
3010 * The component inverse schedule is specified as the "map" fields
3011 * of the elements of "domain" indexed by the first "n" elements of "order".
3013 * The schedule domain being "shifted strided" means that the differences
3014 * between the values of the current dimension of domain "i"
3015 * and the values of the current dimension for some reference domain are
3018 * stride * integer + offset[i]
3020 * We first look for the domain with the "smallest" value for the current
3021 * dimension and adjust the offsets such that the offset of the "smallest"
3022 * domain is equal to zero. The other offsets are reduced modulo stride.
3024 * Based on this information, we construct a new inverse schedule in
3025 * contruct_shifted_executed that exposes the stride.
3026 * Since this involves the introduction of a new schedule dimension,
3027 * the build needs to be changed accodingly.
3028 * After computing the AST, the newly introduced dimension needs
3029 * to be removed again from the list of grafts. We do this by plugging
3030 * in a mapping that represents the new schedule domain in terms of the
3031 * old schedule domain.
3033 static __isl_give isl_ast_graft_list
*generate_shift_component(
3034 struct isl_set_map_pair
*domain
, int *order
, int n
, isl_int stride
,
3035 __isl_keep isl_vec
*offset
, __isl_take isl_ast_build
*build
)
3037 isl_ast_graft_list
*list
;
3044 isl_multi_aff
*ma
, *zero
;
3045 isl_union_map
*executed
;
3047 ctx
= isl_ast_build_get_ctx(build
);
3048 depth
= isl_ast_build_get_depth(build
);
3050 first
= first_offset(domain
, order
, n
, build
);
3052 return isl_ast_build_free(build
);
3055 v
= isl_vec_alloc(ctx
, n
);
3056 if (isl_vec_get_element(offset
, first
, &val
) < 0)
3057 v
= isl_vec_free(v
);
3058 isl_int_neg(val
, val
);
3059 v
= isl_vec_set(v
, val
);
3060 v
= isl_vec_add(v
, isl_vec_copy(offset
));
3061 v
= isl_vec_fdiv_r(v
, stride
);
3063 executed
= contruct_shifted_executed(domain
, order
, n
, stride
, v
,
3065 space
= isl_ast_build_get_space(build
, 1);
3066 space
= isl_space_map_from_set(space
);
3067 ma
= isl_multi_aff_identity(isl_space_copy(space
));
3068 space
= isl_space_from_domain(isl_space_domain(space
));
3069 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3070 zero
= isl_multi_aff_zero(space
);
3071 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
3072 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
3073 list
= generate_shifted_component(executed
, build
);
3075 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
3083 /* Generate code for a single component.
3085 * The component inverse schedule is specified as the "map" fields
3086 * of the elements of "domain" indexed by the first "n" elements of "order".
3088 * This function may modify the "set" fields of "domain".
3090 * Before proceeding with the actual code generation for the component,
3091 * we first check if there are any "shifted" strides, meaning that
3092 * the schedule domains of the individual domains are all strided,
3093 * but that they have different offsets, resulting in the union
3094 * of schedule domains not being strided anymore.
3096 * The simplest example is the schedule
3098 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3100 * Both schedule domains are strided, but their union is not.
3101 * This function detects such cases and then rewrites the schedule to
3103 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3105 * In the new schedule, the schedule domains have the same offset (modulo
3106 * the stride), ensuring that the union of schedule domains is also strided.
3109 * If there is only a single domain in the component, then there is
3110 * nothing to do. Similarly, if the current schedule dimension has
3111 * a fixed value for almost all domains then there is nothing to be done.
3112 * In particular, we need at least two domains where the current schedule
3113 * dimension does not have a fixed value.
3114 * Finally, if any of the options refer to the current schedule dimension,
3115 * then we bail out as well. It would be possible to reformulate the options
3116 * in terms of the new schedule domain, but that would introduce constraints
3117 * that separate the domains in the options and that is something we would
3121 * To see if there is any shifted stride, we look at the differences
3122 * between the values of the current dimension in pairs of domains
3123 * for equal values of outer dimensions. These differences should be
3128 * with "m" the stride and "r" a constant. Note that we cannot perform
3129 * this analysis on individual domains as the lower bound in each domain
3130 * may depend on parameters or outer dimensions and so the current dimension
3131 * itself may not have a fixed remainder on division by the stride.
3133 * In particular, we compare the first domain that does not have an
3134 * obviously fixed value for the current dimension to itself and all
3135 * other domains and collect the offsets and the gcd of the strides.
3136 * If the gcd becomes one, then we failed to find shifted strides.
3137 * If all the offsets are the same (for those domains that do not have
3138 * an obviously fixed value for the current dimension), then we do not
3139 * apply the transformation.
3140 * If none of the domains were skipped, then there is nothing to do.
3141 * If some of them were skipped, then if we apply separation, the schedule
3142 * domain should get split in pieces with a (non-shifted) stride.
3144 * Otherwise, we apply a shift to expose the stride in
3145 * generate_shift_component.
3147 static __isl_give isl_ast_graft_list
*generate_component(
3148 struct isl_set_map_pair
*domain
, int *order
, int n
,
3149 __isl_take isl_ast_build
*build
)
3160 isl_ast_graft_list
*list
;
3163 depth
= isl_ast_build_get_depth(build
);
3166 if (skip
>= 0 && !skip
)
3167 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
3168 if (skip
>= 0 && !skip
)
3169 skip
= isl_ast_build_options_involve_depth(build
);
3171 return isl_ast_build_free(build
);
3173 return generate_shifted_component_from_list(domain
,
3176 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
3178 return isl_ast_build_free(build
);
3180 ctx
= isl_ast_build_get_ctx(build
);
3185 v
= isl_vec_alloc(ctx
, n
);
3188 for (i
= 0; i
< n
; ++i
) {
3189 map
= isl_map_from_domain_and_range(
3190 isl_set_copy(domain
[order
[base
]].set
),
3191 isl_set_copy(domain
[order
[i
]].set
));
3192 for (d
= 0; d
< depth
; ++d
)
3193 map
= isl_map_equate(map
, isl_dim_in
, d
,
3195 deltas
= isl_map_deltas(map
);
3196 res
= isl_set_dim_residue_class(deltas
, depth
, &m
, &r
);
3197 isl_set_free(deltas
);
3202 isl_int_set(gcd
, m
);
3204 isl_int_gcd(gcd
, gcd
, m
);
3205 if (isl_int_is_one(gcd
))
3207 v
= isl_vec_set_element(v
, i
, r
);
3209 res
= isl_set_plain_is_fixed(domain
[order
[i
]].set
,
3210 isl_dim_set
, depth
, NULL
);
3216 if (fixed
&& i
> base
) {
3217 isl_vec_get_element(v
, base
, &m
);
3218 if (isl_int_ne(m
, r
))
3224 isl_ast_build_free(build
);
3226 } else if (i
< n
|| fixed
) {
3227 list
= generate_shifted_component_from_list(domain
,
3230 list
= generate_shift_component(domain
, order
, n
, gcd
, v
,
3242 /* Store both "map" itself and its domain in the
3243 * structure pointed to by *next and advance to the next array element.
3245 static int extract_domain(__isl_take isl_map
*map
, void *user
)
3247 struct isl_set_map_pair
**next
= user
;
3249 (*next
)->map
= isl_map_copy(map
);
3250 (*next
)->set
= isl_map_domain(map
);
3256 /* Internal data for any_scheduled_after.
3258 * "depth" is the number of loops that have already been generated
3259 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3260 * "domain" is an array of set-map pairs corresponding to the different
3261 * iteration domains. The set is the schedule domain, i.e., the domain
3262 * of the inverse schedule, while the map is the inverse schedule itself.
3264 struct isl_any_scheduled_after_data
{
3266 int group_coscheduled
;
3267 struct isl_set_map_pair
*domain
;
3270 /* Is any element of domain "i" scheduled after any element of domain "j"
3271 * (for a common iteration of the first data->depth loops)?
3273 * data->domain[i].set contains the domain of the inverse schedule
3274 * for domain "i", i.e., elements in the schedule domain.
3276 * If data->group_coscheduled is set, then we also return 1 if there
3277 * is any pair of elements in the two domains that are scheduled together.
3279 static int any_scheduled_after(int i
, int j
, void *user
)
3281 struct isl_any_scheduled_after_data
*data
= user
;
3282 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
3285 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
3288 follows
= isl_set_follows_at(data
->domain
[i
].set
,
3289 data
->domain
[j
].set
, pos
);
3299 return data
->group_coscheduled
;
3302 /* Look for independent components at the current depth and generate code
3303 * for each component separately. The resulting lists of grafts are
3304 * merged in an attempt to combine grafts with identical guards.
3306 * Code for two domains can be generated separately if all the elements
3307 * of one domain are scheduled before (or together with) all the elements
3308 * of the other domain. We therefore consider the graph with as nodes
3309 * the domains and an edge between two nodes if any element of the first
3310 * node is scheduled after any element of the second node.
3311 * If the ast_build_group_coscheduled is set, then we also add an edge if
3312 * there is any pair of elements in the two domains that are scheduled
3314 * Code is then generated (by generate_component)
3315 * for each of the strongly connected components in this graph
3316 * in their topological order.
3318 * Since the test is performed on the domain of the inverse schedules of
3319 * the different domains, we precompute these domains and store
3320 * them in data.domain.
3322 static __isl_give isl_ast_graft_list
*generate_components(
3323 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3326 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
3327 int n
= isl_union_map_n_map(executed
);
3328 struct isl_any_scheduled_after_data data
;
3329 struct isl_set_map_pair
*next
;
3330 struct isl_tarjan_graph
*g
= NULL
;
3331 isl_ast_graft_list
*list
= NULL
;
3334 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
3340 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
3345 data
.depth
= isl_ast_build_get_depth(build
);
3346 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
3347 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
3349 list
= isl_ast_graft_list_alloc(ctx
, 0);
3353 isl_ast_graft_list
*list_c
;
3356 if (g
->order
[i
] == -1)
3357 isl_die(ctx
, isl_error_internal
, "cannot happen",
3360 while (g
->order
[i
] != -1) {
3364 list_c
= generate_component(data
.domain
,
3365 g
->order
+ first
, i
- first
,
3366 isl_ast_build_copy(build
));
3367 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
3373 error
: list
= isl_ast_graft_list_free(list
);
3374 isl_tarjan_graph_free(g
);
3375 for (i
= 0; i
< n_domain
; ++i
) {
3376 isl_map_free(data
.domain
[i
].map
);
3377 isl_set_free(data
.domain
[i
].set
);
3380 isl_union_map_free(executed
);
3381 isl_ast_build_free(build
);
3386 /* Generate code for the next level (and all inner levels).
3388 * If "executed" is empty, i.e., no code needs to be generated,
3389 * then we return an empty list.
3391 * If we have already generated code for all loop levels, then we pass
3392 * control to generate_inner_level.
3394 * If "executed" lives in a single space, i.e., if code needs to be
3395 * generated for a single domain, then there can only be a single
3396 * component and we go directly to generate_shifted_component.
3397 * Otherwise, we call generate_components to detect the components
3398 * and to call generate_component on each of them separately.
3400 static __isl_give isl_ast_graft_list
*generate_next_level(
3401 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3405 if (!build
|| !executed
)
3408 if (isl_union_map_is_empty(executed
)) {
3409 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
3410 isl_union_map_free(executed
);
3411 isl_ast_build_free(build
);
3412 return isl_ast_graft_list_alloc(ctx
, 0);
3415 depth
= isl_ast_build_get_depth(build
);
3416 if (depth
>= isl_set_dim(build
->domain
, isl_dim_set
))
3417 return generate_inner_level(executed
, build
);
3419 if (isl_union_map_n_map(executed
) == 1)
3420 return generate_shifted_component(executed
, build
);
3422 return generate_components(executed
, build
);
3424 isl_union_map_free(executed
);
3425 isl_ast_build_free(build
);
3429 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3430 * internal, executed and build are the inputs to generate_code.
3431 * list collects the output.
3433 struct isl_generate_code_data
{
3435 isl_union_map
*executed
;
3436 isl_ast_build
*build
;
3438 isl_ast_graft_list
*list
;
3441 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3445 * with E the external build schedule and S the additional schedule "space",
3446 * reformulate the inverse schedule in terms of the internal schedule domain,
3451 * We first obtain a mapping
3455 * take the inverse and the product with S -> S, resulting in
3457 * [I -> S] -> [E -> S]
3459 * Applying the map to the input produces the desired result.
3461 static __isl_give isl_union_map
*internal_executed(
3462 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
3463 __isl_keep isl_ast_build
*build
)
3467 proj
= isl_ast_build_get_schedule_map(build
);
3468 proj
= isl_map_reverse(proj
);
3469 space
= isl_space_map_from_set(isl_space_copy(space
));
3470 id
= isl_map_identity(space
);
3471 proj
= isl_map_product(proj
, id
);
3472 executed
= isl_union_map_apply_domain(executed
,
3473 isl_union_map_from_map(proj
));
3477 /* Generate an AST that visits the elements in the range of data->executed
3478 * in the relative order specified by the corresponding image element(s)
3479 * for those image elements that belong to "set".
3480 * Add the result to data->list.
3482 * The caller ensures that "set" is a universe domain.
3483 * "space" is the space of the additional part of the schedule.
3484 * It is equal to the space of "set" if build->domain is parametric.
3485 * Otherwise, it is equal to the range of the wrapped space of "set".
3487 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3488 * was called from an outside user (data->internal not set), then
3489 * the (inverse) schedule refers to the external build domain and needs to
3490 * be transformed to refer to the internal build domain.
3492 * The build is extended to include the additional part of the schedule.
3493 * If the original build space was not parametric, then the options
3494 * in data->build refer only to the additional part of the schedule
3495 * and they need to be adjusted to refer to the complete AST build
3498 * After having adjusted inverse schedule and build, we start generating
3499 * code with the outer loop of the current code generation
3500 * in generate_next_level.
3502 * If the original build space was not parametric, we undo the embedding
3503 * on the resulting isl_ast_node_list so that it can be used within
3504 * the outer AST build.
3506 static int generate_code_in_space(struct isl_generate_code_data
*data
,
3507 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
3509 isl_union_map
*executed
;
3510 isl_ast_build
*build
;
3511 isl_ast_graft_list
*list
;
3514 executed
= isl_union_map_copy(data
->executed
);
3515 executed
= isl_union_map_intersect_domain(executed
,
3516 isl_union_set_from_set(set
));
3518 embed
= !isl_set_is_params(data
->build
->domain
);
3519 if (embed
&& !data
->internal
)
3520 executed
= internal_executed(executed
, space
, data
->build
);
3522 build
= isl_ast_build_copy(data
->build
);
3523 build
= isl_ast_build_product(build
, space
);
3525 list
= generate_next_level(executed
, build
);
3527 list
= isl_ast_graft_list_unembed(list
, embed
);
3529 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
3534 /* Generate an AST that visits the elements in the range of data->executed
3535 * in the relative order specified by the corresponding domain element(s)
3536 * for those domain elements that belong to "set".
3537 * Add the result to data->list.
3539 * The caller ensures that "set" is a universe domain.
3541 * If the build space S is not parametric, then the space of "set"
3542 * need to be a wrapped relation with S as domain. That is, it needs
3547 * Check this property and pass control to generate_code_in_space
3549 * If the build space is not parametric, then T is the space of "set".
3551 static int generate_code_set(__isl_take isl_set
*set
, void *user
)
3553 struct isl_generate_code_data
*data
= user
;
3554 isl_space
*space
, *build_space
;
3557 space
= isl_set_get_space(set
);
3559 if (isl_set_is_params(data
->build
->domain
))
3560 return generate_code_in_space(data
, set
, space
);
3562 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
3563 space
= isl_space_unwrap(space
);
3564 is_domain
= isl_space_is_domain(build_space
, space
);
3565 isl_space_free(build_space
);
3566 space
= isl_space_range(space
);
3571 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3572 "invalid nested schedule space", goto error
);
3574 return generate_code_in_space(data
, set
, space
);
3577 isl_space_free(space
);
3581 /* Generate an AST that visits the elements in the range of "executed"
3582 * in the relative order specified by the corresponding domain element(s).
3584 * "build" is an isl_ast_build that has either been constructed by
3585 * isl_ast_build_from_context or passed to a callback set by
3586 * isl_ast_build_set_create_leaf.
3587 * In the first case, the space of the isl_ast_build is typically
3588 * a parametric space, although this is currently not enforced.
3589 * In the second case, the space is never a parametric space.
3590 * If the space S is not parametric, then the domain space(s) of "executed"
3591 * need to be wrapped relations with S as domain.
3593 * If the domain of "executed" consists of several spaces, then an AST
3594 * is generated for each of them (in arbitrary order) and the results
3597 * If "internal" is set, then the domain "S" above refers to the internal
3598 * schedule domain representation. Otherwise, it refers to the external
3599 * representation, as returned by isl_ast_build_get_schedule_space.
3601 * We essentially run over all the spaces in the domain of "executed"
3602 * and call generate_code_set on each of them.
3604 static __isl_give isl_ast_graft_list
*generate_code(
3605 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3609 struct isl_generate_code_data data
= { 0 };
3611 isl_union_set
*schedule_domain
;
3612 isl_union_map
*universe
;
3616 space
= isl_ast_build_get_space(build
, 1);
3617 space
= isl_space_align_params(space
,
3618 isl_union_map_get_space(executed
));
3619 space
= isl_space_align_params(space
,
3620 isl_union_map_get_space(build
->options
));
3621 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
3622 executed
= isl_union_map_align_params(executed
, space
);
3623 if (!executed
|| !build
)
3626 ctx
= isl_ast_build_get_ctx(build
);
3628 data
.internal
= internal
;
3629 data
.executed
= executed
;
3631 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
3633 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
3634 schedule_domain
= isl_union_map_domain(universe
);
3635 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
3637 data
.list
= isl_ast_graft_list_free(data
.list
);
3639 isl_union_set_free(schedule_domain
);
3640 isl_union_map_free(executed
);
3642 isl_ast_build_free(build
);
3645 isl_union_map_free(executed
);
3646 isl_ast_build_free(build
);
3650 /* Generate an AST that visits the elements in the domain of "schedule"
3651 * in the relative order specified by the corresponding image element(s).
3653 * "build" is an isl_ast_build that has either been constructed by
3654 * isl_ast_build_from_context or passed to a callback set by
3655 * isl_ast_build_set_create_leaf.
3656 * In the first case, the space of the isl_ast_build is typically
3657 * a parametric space, although this is currently not enforced.
3658 * In the second case, the space is never a parametric space.
3659 * If the space S is not parametric, then the range space(s) of "schedule"
3660 * need to be wrapped relations with S as domain.
3662 * If the range of "schedule" consists of several spaces, then an AST
3663 * is generated for each of them (in arbitrary order) and the results
3666 * We first initialize the local copies of the relevant options.
3667 * We do this here rather than when the isl_ast_build is created
3668 * because the options may have changed between the construction
3669 * of the isl_ast_build and the call to isl_generate_code.
3671 * The main computation is performed on an inverse schedule (with
3672 * the schedule domain in the domain and the elements to be executed
3673 * in the range) called "executed".
3675 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
3676 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
3678 isl_ast_graft_list
*list
;
3680 isl_union_map
*executed
;
3682 build
= isl_ast_build_copy(build
);
3683 build
= isl_ast_build_set_single_valued(build
, 0);
3684 executed
= isl_union_map_reverse(schedule
);
3685 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
3686 node
= isl_ast_node_from_graft_list(list
, build
);
3687 isl_ast_build_free(build
);