2 * Copyright 2012-2014 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>
22 /* Add the constraint to the list that "user" points to, if it is not
25 static int collect_constraint(__isl_take isl_constraint
*constraint
,
28 isl_constraint_list
**list
= user
;
30 if (isl_constraint_is_div_constraint(constraint
))
31 isl_constraint_free(constraint
);
33 *list
= isl_constraint_list_add(*list
, constraint
);
38 /* Extract the constraints of "bset" (except the div constraints)
39 * and collect them in an isl_constraint_list.
41 static __isl_give isl_constraint_list
*isl_constraint_list_from_basic_set(
42 __isl_take isl_basic_set
*bset
)
46 isl_constraint_list
*list
;
51 ctx
= isl_basic_set_get_ctx(bset
);
53 n
= isl_basic_set_n_constraint(bset
);
54 list
= isl_constraint_list_alloc(ctx
, n
);
55 if (isl_basic_set_foreach_constraint(bset
,
56 &collect_constraint
, &list
) < 0)
57 list
= isl_constraint_list_free(list
);
59 isl_basic_set_free(bset
);
63 /* Data used in generate_domain.
65 * "build" is the input build.
66 * "list" collects the results.
68 struct isl_generate_domain_data
{
71 isl_ast_graft_list
*list
;
74 static __isl_give isl_ast_graft_list
*generate_next_level(
75 __isl_take isl_union_map
*executed
,
76 __isl_take isl_ast_build
*build
);
77 static __isl_give isl_ast_graft_list
*generate_code(
78 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
81 /* Generate an AST for a single domain based on
82 * the (non single valued) inverse schedule "executed".
84 * We extend the schedule with the iteration domain
85 * and continue generating through a call to generate_code.
87 * In particular, if executed has the form
91 * then we continue generating code on
95 * The extended inverse schedule is clearly single valued
96 * ensuring that the nested generate_code will not reach this function,
97 * but will instead create calls to all elements of D that need
98 * to be executed from the current schedule domain.
100 static int generate_non_single_valued(__isl_take isl_map
*executed
,
101 struct isl_generate_domain_data
*data
)
104 isl_ast_build
*build
;
105 isl_ast_graft_list
*list
;
107 build
= isl_ast_build_copy(data
->build
);
109 identity
= isl_set_identity(isl_map_range(isl_map_copy(executed
)));
110 executed
= isl_map_domain_product(executed
, identity
);
111 build
= isl_ast_build_set_single_valued(build
, 1);
113 list
= generate_code(isl_union_map_from_map(executed
), build
, 1);
115 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
120 /* Call the at_each_domain callback, if requested by the user,
121 * after recording the current inverse schedule in the build.
123 static __isl_give isl_ast_graft
*at_each_domain(__isl_take isl_ast_graft
*graft
,
124 __isl_keep isl_map
*executed
, __isl_keep isl_ast_build
*build
)
126 if (!graft
|| !build
)
127 return isl_ast_graft_free(graft
);
128 if (!build
->at_each_domain
)
131 build
= isl_ast_build_copy(build
);
132 build
= isl_ast_build_set_executed(build
,
133 isl_union_map_from_map(isl_map_copy(executed
)));
135 return isl_ast_graft_free(graft
);
137 graft
->node
= build
->at_each_domain(graft
->node
,
138 build
, build
->at_each_domain_user
);
139 isl_ast_build_free(build
);
142 graft
= isl_ast_graft_free(graft
);
147 /* Generate an AST for a single domain based on
148 * the inverse schedule "executed" and add it to data->list.
150 * If there is more than one domain element associated to the current
151 * schedule "time", then we need to continue the generation process
152 * in generate_non_single_valued.
153 * Note that the inverse schedule being single-valued may depend
154 * on constraints that are only available in the original context
155 * domain specified by the user. We therefore first introduce
156 * the constraints from data->build->domain.
157 * On the other hand, we only perform the test after having taken the gist
158 * of the domain as the resulting map is the one from which the call
159 * expression is constructed. Using this map to construct the call
160 * expression usually yields simpler results.
161 * Because we perform the single-valuedness test on the gisted map,
162 * we may in rare cases fail to recognize that the inverse schedule
163 * is single-valued. This becomes problematic if this happens
164 * from the recursive call through generate_non_single_valued
165 * as we would then end up in an infinite recursion.
166 * We therefore check if we are inside a call to generate_non_single_valued
167 * and revert to the ungisted map if the gisted map turns out not to be
170 * Otherwise, we generate a call expression for the single executed
171 * domain element and put a guard around it based on the (simplified)
172 * domain of "executed".
174 * If the user has set an at_each_domain callback, it is called
175 * on the constructed call expression node.
177 static int generate_domain(__isl_take isl_map
*executed
, void *user
)
179 struct isl_generate_domain_data
*data
= user
;
180 isl_ast_graft
*graft
;
181 isl_ast_graft_list
*list
;
186 executed
= isl_map_intersect_domain(executed
,
187 isl_set_copy(data
->build
->domain
));
188 empty
= isl_map_is_empty(executed
);
192 isl_map_free(executed
);
196 executed
= isl_map_coalesce(executed
);
197 map
= isl_map_copy(executed
);
198 map
= isl_ast_build_compute_gist_map_domain(data
->build
, map
);
199 sv
= isl_map_is_single_valued(map
);
204 if (data
->build
->single_valued
)
205 map
= isl_map_copy(executed
);
207 return generate_non_single_valued(executed
, data
);
209 guard
= isl_map_domain(isl_map_copy(map
));
210 guard
= isl_set_coalesce(guard
);
211 guard
= isl_ast_build_compute_gist(data
->build
, guard
);
212 graft
= isl_ast_graft_alloc_domain(map
, data
->build
);
213 graft
= at_each_domain(graft
, executed
, data
->build
);
215 isl_map_free(executed
);
216 graft
= isl_ast_graft_add_guard(graft
, guard
, data
->build
);
218 list
= isl_ast_graft_list_from_ast_graft(graft
);
219 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
224 isl_map_free(executed
);
228 /* Call build->create_leaf to a create "leaf" node in the AST,
229 * encapsulate the result in an isl_ast_graft and return the result
230 * as a 1-element list.
232 * Note that the node returned by the user may be an entire tree.
234 * Before we pass control to the user, we first clear some information
235 * from the build that is (presumbably) only meaningful
236 * for the current code generation.
237 * This includes the create_leaf callback itself, so we make a copy
238 * of the build first.
240 static __isl_give isl_ast_graft_list
*call_create_leaf(
241 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
244 isl_ast_graft
*graft
;
245 isl_ast_build
*user_build
;
247 user_build
= isl_ast_build_copy(build
);
248 user_build
= isl_ast_build_set_executed(user_build
, executed
);
249 user_build
= isl_ast_build_clear_local_info(user_build
);
253 node
= build
->create_leaf(user_build
, build
->create_leaf_user
);
254 graft
= isl_ast_graft_alloc(node
, build
);
255 isl_ast_build_free(build
);
256 return isl_ast_graft_list_from_ast_graft(graft
);
259 /* Generate an AST after having handled the complete schedule
260 * of this call to the code generator.
262 * If the user has specified a create_leaf callback, control
263 * is passed to the user in call_create_leaf.
265 * Otherwise, we generate one or more calls for each individual
266 * domain in generate_domain.
268 static __isl_give isl_ast_graft_list
*generate_inner_level(
269 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
272 struct isl_generate_domain_data data
= { build
};
274 if (!build
|| !executed
)
277 if (build
->create_leaf
)
278 return call_create_leaf(executed
, build
);
280 ctx
= isl_union_map_get_ctx(executed
);
281 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
282 if (isl_union_map_foreach_map(executed
, &generate_domain
, &data
) < 0)
283 data
.list
= isl_ast_graft_list_free(data
.list
);
286 error
: data
.list
= NULL
;
287 isl_ast_build_free(build
);
288 isl_union_map_free(executed
);
292 /* Call the before_each_for callback, if requested by the user.
294 static __isl_give isl_ast_node
*before_each_for(__isl_take isl_ast_node
*node
,
295 __isl_keep isl_ast_build
*build
)
300 return isl_ast_node_free(node
);
301 if (!build
->before_each_for
)
303 id
= build
->before_each_for(build
, build
->before_each_for_user
);
304 node
= isl_ast_node_set_annotation(node
, id
);
308 /* Call the after_each_for callback, if requested by the user.
310 static __isl_give isl_ast_graft
*after_each_for(__isl_keep isl_ast_graft
*graft
,
311 __isl_keep isl_ast_build
*build
)
313 if (!graft
|| !build
)
314 return isl_ast_graft_free(graft
);
315 if (!build
->after_each_for
)
317 graft
->node
= build
->after_each_for(graft
->node
, build
,
318 build
->after_each_for_user
);
320 return isl_ast_graft_free(graft
);
324 /* Plug in all the know values of the current and outer dimensions
325 * in the domain of "executed". In principle, we only need to plug
326 * in the known value of the current dimension since the values of
327 * outer dimensions have been plugged in already.
328 * However, it turns out to be easier to just plug in all known values.
330 static __isl_give isl_union_map
*plug_in_values(
331 __isl_take isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
333 return isl_ast_build_substitute_values_union_map_domain(build
,
337 /* Check if the constraint "c" is a lower bound on dimension "pos",
338 * an upper bound, or independent of dimension "pos".
340 static int constraint_type(isl_constraint
*c
, int pos
)
342 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
344 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
349 /* Compare the types of the constraints "a" and "b",
350 * resulting in constraints that are independent of "depth"
351 * to be sorted before the lower bounds on "depth", which in
352 * turn are sorted before the upper bounds on "depth".
354 static int cmp_constraint(__isl_keep isl_constraint
*a
,
355 __isl_keep isl_constraint
*b
, void *user
)
358 int t1
= constraint_type(a
, *depth
);
359 int t2
= constraint_type(b
, *depth
);
364 /* Extract a lower bound on dimension "pos" from constraint "c".
366 * If the constraint is of the form
370 * then we essentially return
372 * l = ceil(-f(...)/a)
374 * However, if the current dimension is strided, then we need to make
375 * sure that the lower bound we construct is of the form
379 * with f the offset and s the stride.
380 * We therefore compute
382 * f + s * ceil((l - f)/s)
384 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
385 int pos
, __isl_keep isl_ast_build
*build
)
389 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
390 aff
= isl_aff_ceil(aff
);
392 if (isl_ast_build_has_stride(build
, pos
)) {
396 offset
= isl_ast_build_get_offset(build
, pos
);
397 stride
= isl_ast_build_get_stride(build
, pos
);
399 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
400 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(stride
));
401 aff
= isl_aff_ceil(aff
);
402 aff
= isl_aff_scale_val(aff
, stride
);
403 aff
= isl_aff_add(aff
, offset
);
406 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
411 /* Return the exact lower bound (or upper bound if "upper" is set)
412 * of "domain" as a piecewise affine expression.
414 * If we are computing a lower bound (of a strided dimension), then
415 * we need to make sure it is of the form
419 * where f is the offset and s is the stride.
420 * We therefore need to include the stride constraint before computing
423 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
424 __isl_keep isl_ast_build
*build
, int upper
)
429 isl_pw_multi_aff
*pma
;
431 domain
= isl_set_copy(domain
);
433 stride
= isl_ast_build_get_stride_constraint(build
);
434 domain
= isl_set_intersect(domain
, stride
);
436 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
438 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
440 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
441 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
442 isl_pw_multi_aff_free(pma
);
443 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
444 pa
= isl_pw_aff_coalesce(pa
);
449 /* Extract a lower bound on dimension "pos" from each constraint
450 * in "constraints" and return the list of lower bounds.
451 * If "constraints" has zero elements, then we extract a lower bound
452 * from "domain" instead.
454 static __isl_give isl_pw_aff_list
*lower_bounds(
455 __isl_keep isl_constraint_list
*constraints
, int pos
,
456 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
459 isl_pw_aff_list
*list
;
465 n
= isl_constraint_list_n_constraint(constraints
);
468 pa
= exact_bound(domain
, build
, 0);
469 return isl_pw_aff_list_from_pw_aff(pa
);
472 ctx
= isl_ast_build_get_ctx(build
);
473 list
= isl_pw_aff_list_alloc(ctx
,n
);
475 for (i
= 0; i
< n
; ++i
) {
479 c
= isl_constraint_list_get_constraint(constraints
, i
);
480 aff
= lower_bound(c
, pos
, build
);
481 isl_constraint_free(c
);
482 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
488 /* Extract an upper bound on dimension "pos" from each constraint
489 * in "constraints" and return the list of upper bounds.
490 * If "constraints" has zero elements, then we extract an upper bound
491 * from "domain" instead.
493 static __isl_give isl_pw_aff_list
*upper_bounds(
494 __isl_keep isl_constraint_list
*constraints
, int pos
,
495 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
498 isl_pw_aff_list
*list
;
501 n
= isl_constraint_list_n_constraint(constraints
);
504 pa
= exact_bound(domain
, build
, 1);
505 return isl_pw_aff_list_from_pw_aff(pa
);
508 ctx
= isl_ast_build_get_ctx(build
);
509 list
= isl_pw_aff_list_alloc(ctx
,n
);
511 for (i
= 0; i
< n
; ++i
) {
515 c
= isl_constraint_list_get_constraint(constraints
, i
);
516 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
517 isl_constraint_free(c
);
518 aff
= isl_aff_floor(aff
);
519 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
525 /* Callback for sorting the isl_pw_aff_list passed to reduce_list.
527 static int reduce_list_cmp(__isl_keep isl_pw_aff
*a
, __isl_keep isl_pw_aff
*b
,
530 return isl_pw_aff_plain_cmp(a
, b
);
533 /* Return an isl_ast_expr that performs the reduction of type "type"
534 * on AST expressions corresponding to the elements in "list".
536 * The list is assumed to contain at least one element.
537 * If the list contains exactly one element, then the returned isl_ast_expr
538 * simply computes that affine expression.
539 * If the list contains more than one element, then we sort it
540 * using a fairly abitrary but hopefully reasonably stable order.
542 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
543 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
552 n
= isl_pw_aff_list_n_pw_aff(list
);
555 return isl_ast_build_expr_from_pw_aff_internal(build
,
556 isl_pw_aff_list_get_pw_aff(list
, 0));
558 ctx
= isl_pw_aff_list_get_ctx(list
);
559 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
563 list
= isl_pw_aff_list_copy(list
);
564 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
566 return isl_ast_expr_free(expr
);
568 for (i
= 0; i
< n
; ++i
) {
569 isl_ast_expr
*expr_i
;
571 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
572 isl_pw_aff_list_get_pw_aff(list
, i
));
575 expr
->u
.op
.args
[i
] = expr_i
;
578 isl_pw_aff_list_free(list
);
581 isl_pw_aff_list_free(list
);
582 isl_ast_expr_free(expr
);
586 /* Add a guard to "graft" based on "bound" in the case of a degenerate
587 * level (including the special case of an eliminated level).
589 * We eliminate the current dimension, simplify the result in the current
590 * build and add the result as guards to the graft.
592 * Note that we cannot simply drop the constraints on the current dimension
593 * even in the eliminated case, because the single affine expression may
594 * not be explicitly available in "bounds". Moreover, the single affine
595 * expression may only be defined on a subset of the build domain,
596 * so we do in some cases need to insert a guard even in the eliminated case.
598 static __isl_give isl_ast_graft
*add_degenerate_guard(
599 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
600 __isl_keep isl_ast_build
*build
)
605 depth
= isl_ast_build_get_depth(build
);
607 dom
= isl_set_from_basic_set(isl_basic_set_copy(bounds
));
608 if (isl_ast_build_has_stride(build
, depth
)) {
611 stride
= isl_ast_build_get_stride_constraint(build
);
612 dom
= isl_set_intersect(dom
, stride
);
614 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
615 dom
= isl_ast_build_compute_gist(build
, dom
);
617 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
622 /* Update "graft" based on "bounds" for the eliminated case.
624 * In the eliminated case, no for node is created, so we only need
625 * to check if "bounds" imply any guards that need to be inserted.
627 static __isl_give isl_ast_graft
*refine_eliminated(
628 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
629 __isl_keep isl_ast_build
*build
)
631 return add_degenerate_guard(graft
, bounds
, build
);
634 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
636 * "build" is the build in which graft->node was created
637 * "sub_build" contains information about the current level itself,
638 * including the single value attained.
640 * We first set the initialization part of the for loop to the single
641 * value attained by the current dimension.
642 * The increment and condition are not strictly needed as the are known
643 * to be "1" and "iterator <= value" respectively.
644 * Then we set the size of the iterator and
645 * check if "bounds" imply any guards that need to be inserted.
647 static __isl_give isl_ast_graft
*refine_degenerate(
648 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
649 __isl_keep isl_ast_build
*build
,
650 __isl_keep isl_ast_build
*sub_build
)
654 if (!graft
|| !sub_build
)
655 return isl_ast_graft_free(graft
);
657 value
= isl_pw_aff_copy(sub_build
->value
);
659 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
661 if (!graft
->node
->u
.f
.init
)
662 return isl_ast_graft_free(graft
);
664 graft
= add_degenerate_guard(graft
, bounds
, build
);
669 /* Return the intersection of constraints in "list" as a set.
671 static __isl_give isl_set
*intersect_constraints(
672 __isl_keep isl_constraint_list
*list
)
677 n
= isl_constraint_list_n_constraint(list
);
679 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
680 "expecting at least one constraint", return NULL
);
682 bset
= isl_basic_set_from_constraint(
683 isl_constraint_list_get_constraint(list
, 0));
684 for (i
= 1; i
< n
; ++i
) {
685 isl_basic_set
*bset_i
;
687 bset_i
= isl_basic_set_from_constraint(
688 isl_constraint_list_get_constraint(list
, i
));
689 bset
= isl_basic_set_intersect(bset
, bset_i
);
692 return isl_set_from_basic_set(bset
);
695 /* Compute the constraints on the outer dimensions enforced by
696 * graft->node and add those constraints to graft->enforced,
697 * in case the upper bound is expressed as a set "upper".
699 * In particular, if l(...) is a lower bound in "lower", and
701 * -a i + f(...) >= 0 or a i <= f(...)
703 * is an upper bound ocnstraint on the current dimension i,
704 * then the for loop enforces the constraint
706 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
708 * We therefore simply take each lower bound in turn, plug it into
709 * the upper bounds and compute the intersection over all lower bounds.
711 * If a lower bound is a rational expression, then
712 * isl_basic_set_preimage_multi_aff will force this rational
713 * expression to have only integer values. However, the loop
714 * itself does not enforce this integrality constraint. We therefore
715 * use the ceil of the lower bounds instead of the lower bounds themselves.
716 * Other constraints will make sure that the for loop is only executed
717 * when each of the lower bounds attains an integral value.
718 * In particular, potentially rational values only occur in
719 * lower_bound if the offset is a (seemingly) rational expression,
720 * but then outer conditions will make sure that this rational expression
721 * only attains integer values.
723 static __isl_give isl_ast_graft
*set_enforced_from_set(
724 __isl_take isl_ast_graft
*graft
,
725 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
728 isl_basic_set
*enforced
;
729 isl_pw_multi_aff
*pma
;
732 if (!graft
|| !lower
)
733 return isl_ast_graft_free(graft
);
735 space
= isl_set_get_space(upper
);
736 enforced
= isl_basic_set_universe(isl_space_copy(space
));
738 space
= isl_space_map_from_set(space
);
739 pma
= isl_pw_multi_aff_identity(space
);
741 n
= isl_pw_aff_list_n_pw_aff(lower
);
742 for (i
= 0; i
< n
; ++i
) {
746 isl_pw_multi_aff
*pma_i
;
748 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
749 pa
= isl_pw_aff_ceil(pa
);
750 pma_i
= isl_pw_multi_aff_copy(pma
);
751 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
752 enforced_i
= isl_set_copy(upper
);
753 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
754 hull
= isl_set_simple_hull(enforced_i
);
755 enforced
= isl_basic_set_intersect(enforced
, hull
);
758 isl_pw_multi_aff_free(pma
);
760 graft
= isl_ast_graft_enforce(graft
, enforced
);
765 /* Compute the constraints on the outer dimensions enforced by
766 * graft->node and add those constraints to graft->enforced,
767 * in case the upper bound is expressed as
768 * a list of affine expressions "upper".
770 * The enforced condition is that each lower bound expression is less
771 * than or equal to each upper bound expression.
773 static __isl_give isl_ast_graft
*set_enforced_from_list(
774 __isl_take isl_ast_graft
*graft
,
775 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
778 isl_basic_set
*enforced
;
780 lower
= isl_pw_aff_list_copy(lower
);
781 upper
= isl_pw_aff_list_copy(upper
);
782 cond
= isl_pw_aff_list_le_set(lower
, upper
);
783 enforced
= isl_set_simple_hull(cond
);
784 graft
= isl_ast_graft_enforce(graft
, enforced
);
789 /* Does "aff" have a negative constant term?
791 static int aff_constant_is_negative(__isl_take isl_set
*set
,
792 __isl_take isl_aff
*aff
, void *user
)
797 v
= isl_aff_get_constant_val(aff
);
798 *neg
= isl_val_is_neg(v
);
803 return *neg
? 0 : -1;
806 /* Does "pa" have a negative constant term over its entire domain?
808 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
, void *user
)
813 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
816 return *neg
? 0 : -1;
819 /* Does each element in "list" have a negative constant term?
821 * The callback terminates the iteration as soon an element has been
822 * found that does not have a negative constant term.
824 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
828 if (isl_pw_aff_list_foreach(list
,
829 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
835 /* Add 1 to each of the elements in "list", where each of these elements
836 * is defined over the internal schedule space of "build".
838 static __isl_give isl_pw_aff_list
*list_add_one(
839 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
846 space
= isl_ast_build_get_space(build
, 1);
847 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
848 aff
= isl_aff_add_constant_si(aff
, 1);
849 one
= isl_pw_aff_from_aff(aff
);
851 n
= isl_pw_aff_list_n_pw_aff(list
);
852 for (i
= 0; i
< n
; ++i
) {
854 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
855 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
856 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
859 isl_pw_aff_free(one
);
864 /* Set the condition part of the for node graft->node in case
865 * the upper bound is represented as a list of piecewise affine expressions.
867 * In particular, set the condition to
869 * iterator <= min(list of upper bounds)
871 * If each of the upper bounds has a negative constant term, then
872 * set the condition to
874 * iterator < min(list of (upper bound + 1)s)
877 static __isl_give isl_ast_graft
*set_for_cond_from_list(
878 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
879 __isl_keep isl_ast_build
*build
)
882 isl_ast_expr
*bound
, *iterator
, *cond
;
883 enum isl_ast_op_type type
= isl_ast_op_le
;
886 return isl_ast_graft_free(graft
);
888 neg
= list_constant_is_negative(list
);
890 return isl_ast_graft_free(graft
);
891 list
= isl_pw_aff_list_copy(list
);
893 list
= list_add_one(list
, build
);
894 type
= isl_ast_op_lt
;
897 bound
= reduce_list(isl_ast_op_min
, list
, build
);
898 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
899 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
900 graft
->node
->u
.f
.cond
= cond
;
902 isl_pw_aff_list_free(list
);
903 if (!graft
->node
->u
.f
.cond
)
904 return isl_ast_graft_free(graft
);
908 /* Set the condition part of the for node graft->node in case
909 * the upper bound is represented as a set.
911 static __isl_give isl_ast_graft
*set_for_cond_from_set(
912 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
913 __isl_keep isl_ast_build
*build
)
920 cond
= isl_ast_build_expr_from_set(build
, isl_set_copy(set
));
921 graft
->node
->u
.f
.cond
= cond
;
922 if (!graft
->node
->u
.f
.cond
)
923 return isl_ast_graft_free(graft
);
927 /* Construct an isl_ast_expr for the increment (i.e., stride) of
928 * the current dimension.
930 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
938 ctx
= isl_ast_build_get_ctx(build
);
939 depth
= isl_ast_build_get_depth(build
);
941 if (!isl_ast_build_has_stride(build
, depth
))
942 return isl_ast_expr_alloc_int_si(ctx
, 1);
944 v
= isl_ast_build_get_stride(build
, depth
);
945 return isl_ast_expr_from_val(v
);
948 /* Should we express the loop condition as
950 * iterator <= min(list of upper bounds)
952 * or as a conjunction of constraints?
954 * The first is constructed from a list of upper bounds.
955 * The second is constructed from a set.
957 * If there are no upper bounds in "constraints", then this could mean
958 * that "domain" simply doesn't have an upper bound or that we didn't
959 * pick any upper bound. In the first case, we want to generate the
960 * loop condition as a(n empty) conjunction of constraints
961 * In the second case, we will compute
962 * a single upper bound from "domain" and so we use the list form.
964 * If there are upper bounds in "constraints",
965 * then we use the list form iff the atomic_upper_bound option is set.
967 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
968 __isl_keep isl_set
*domain
, int depth
)
971 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
973 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
976 /* Fill in the expressions of the for node in graft->node.
979 * - set the initialization part of the loop to the maximum of the lower bounds
980 * - set the size of the iterator based on the values attained by the iterator
981 * - extract the increment from the stride of the current dimension
982 * - construct the for condition either based on a list of upper bounds
983 * or on a set of upper bound constraints.
985 static __isl_give isl_ast_graft
*set_for_node_expressions(
986 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
987 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
988 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
995 build
= isl_ast_build_copy(build
);
996 build
= isl_ast_build_set_enforced(build
,
997 isl_ast_graft_get_enforced(graft
));
1000 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
1001 node
->u
.f
.inc
= for_inc(build
);
1004 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
1006 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
1008 isl_ast_build_free(build
);
1010 if (!node
->u
.f
.iterator
|| !node
->u
.f
.init
||
1011 !node
->u
.f
.cond
|| !node
->u
.f
.inc
)
1012 return isl_ast_graft_free(graft
);
1017 /* Update "graft" based on "bounds" and "domain" for the generic,
1018 * non-degenerate, case.
1020 * "c_lower" and "c_upper" contain the lower and upper bounds
1021 * that the loop node should express.
1022 * "domain" is the subset of the intersection of the constraints
1023 * for which some code is executed.
1025 * There may be zero lower bounds or zero upper bounds in "constraints"
1026 * in case the list of constraints was created
1027 * based on the atomic option or based on separation with explicit bounds.
1028 * In that case, we use "domain" to derive lower and/or upper bounds.
1030 * We first compute a list of one or more lower bounds.
1032 * Then we decide if we want to express the condition as
1034 * iterator <= min(list of upper bounds)
1036 * or as a conjunction of constraints.
1038 * The set of enforced constraints is then computed either based on
1039 * a list of upper bounds or on a set of upper bound constraints.
1040 * We do not compute any enforced constraints if we were forced
1041 * to compute a lower or upper bound using exact_bound. The domains
1042 * of the resulting expressions may imply some bounds on outer dimensions
1043 * that we do not want to appear in the enforced constraints since
1044 * they are not actually enforced by the corresponding code.
1046 * Finally, we fill in the expressions of the for node.
1048 static __isl_give isl_ast_graft
*refine_generic_bounds(
1049 __isl_take isl_ast_graft
*graft
,
1050 __isl_take isl_constraint_list
*c_lower
,
1051 __isl_take isl_constraint_list
*c_upper
,
1052 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1056 isl_pw_aff_list
*lower
;
1058 isl_set
*upper_set
= NULL
;
1059 isl_pw_aff_list
*upper_list
= NULL
;
1060 int n_lower
, n_upper
;
1062 if (!graft
|| !c_lower
|| !c_upper
|| !build
)
1065 depth
= isl_ast_build_get_depth(build
);
1066 ctx
= isl_ast_graft_get_ctx(graft
);
1068 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1069 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1071 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1073 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1076 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1077 else if (n_upper
> 0)
1078 upper_set
= intersect_constraints(c_upper
);
1080 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1082 if (n_lower
== 0 || n_upper
== 0)
1085 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1087 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1089 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1092 isl_pw_aff_list_free(lower
);
1093 isl_pw_aff_list_free(upper_list
);
1094 isl_set_free(upper_set
);
1095 isl_constraint_list_free(c_lower
);
1096 isl_constraint_list_free(c_upper
);
1100 isl_constraint_list_free(c_lower
);
1101 isl_constraint_list_free(c_upper
);
1102 return isl_ast_graft_free(graft
);
1105 /* Internal data structure used inside count_constraints to keep
1106 * track of the number of constraints that are independent of dimension "pos",
1107 * the lower bounds in "pos" and the upper bounds in "pos".
1109 struct isl_ast_count_constraints_data
{
1117 /* Increment data->n_indep, data->lower or data->upper depending
1118 * on whether "c" is independenct of dimensions data->pos,
1119 * a lower bound or an upper bound.
1121 static int count_constraints(__isl_take isl_constraint
*c
, void *user
)
1123 struct isl_ast_count_constraints_data
*data
= user
;
1125 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1127 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1132 isl_constraint_free(c
);
1137 /* Update "graft" based on "bounds" and "domain" for the generic,
1138 * non-degenerate, case.
1140 * "list" respresent the list of bounds that need to be encoded by
1141 * the for loop (or a guard around the for loop).
1142 * "domain" is the subset of the intersection of the constraints
1143 * for which some code is executed.
1144 * "build" is the build in which graft->node was created.
1146 * We separate lower bounds, upper bounds and constraints that
1147 * are independent of the loop iterator.
1149 * The actual for loop bounds are generated in refine_generic_bounds.
1150 * If there are any constraints that are independent of the loop iterator,
1151 * we need to put a guard around the for loop (which may get hoisted up
1152 * to higher levels) and we call refine_generic_bounds in a build
1153 * where this guard is enforced.
1155 static __isl_give isl_ast_graft
*refine_generic_split(
1156 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1157 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1159 isl_ast_build
*for_build
;
1161 struct isl_ast_count_constraints_data data
;
1162 isl_constraint_list
*lower
;
1163 isl_constraint_list
*upper
;
1166 return isl_ast_graft_free(graft
);
1168 data
.pos
= isl_ast_build_get_depth(build
);
1170 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1172 return isl_ast_graft_free(graft
);
1174 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1175 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1176 isl_constraint_list_free(list
);
1177 return isl_ast_graft_free(graft
);
1180 lower
= isl_constraint_list_copy(list
);
1181 lower
= isl_constraint_list_drop(lower
, 0, data
.n_indep
);
1182 upper
= isl_constraint_list_copy(lower
);
1183 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1184 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1186 if (data
.n_indep
== 0) {
1187 isl_constraint_list_free(list
);
1188 return refine_generic_bounds(graft
, lower
, upper
,
1192 list
= isl_constraint_list_drop(list
, data
.n_indep
,
1193 data
.n_lower
+ data
.n_upper
);
1194 guard
= intersect_constraints(list
);
1195 isl_constraint_list_free(list
);
1197 for_build
= isl_ast_build_copy(build
);
1198 for_build
= isl_ast_build_restrict_pending(for_build
,
1199 isl_set_copy(guard
));
1200 graft
= refine_generic_bounds(graft
, lower
, upper
, domain
, for_build
);
1201 isl_ast_build_free(for_build
);
1203 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
1208 /* Add the guard implied by the current stride constraint (if any),
1209 * but not (necessarily) enforced by the generated AST to "graft".
1211 static __isl_give isl_ast_graft
*add_stride_guard(
1212 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
1217 depth
= isl_ast_build_get_depth(build
);
1218 if (!isl_ast_build_has_stride(build
, depth
))
1221 dom
= isl_ast_build_get_stride_constraint(build
);
1222 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
1223 dom
= isl_ast_build_compute_gist(build
, dom
);
1225 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
1230 /* Update "graft" based on "bounds" and "domain" for the generic,
1231 * non-degenerate, case.
1233 * "bounds" respresent the bounds that need to be encoded by
1234 * the for loop (or a guard around the for loop).
1235 * "domain" is the subset of "bounds" for which some code is executed.
1236 * "build" is the build in which graft->node was created.
1238 * We break up "bounds" into a list of constraints and continue with
1239 * refine_generic_split.
1241 static __isl_give isl_ast_graft
*refine_generic(
1242 __isl_take isl_ast_graft
*graft
,
1243 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1244 __isl_keep isl_ast_build
*build
)
1246 isl_constraint_list
*list
;
1248 if (!build
|| !graft
)
1249 return isl_ast_graft_free(graft
);
1251 bounds
= isl_basic_set_copy(bounds
);
1252 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1253 list
= isl_constraint_list_from_basic_set(bounds
);
1255 graft
= refine_generic_split(graft
, list
, domain
, build
);
1256 graft
= add_stride_guard(graft
, build
);
1261 /* Create a for node for the current level.
1263 * Mark the for node degenerate if "degenerate" is set.
1265 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1275 depth
= isl_ast_build_get_depth(build
);
1276 id
= isl_ast_build_get_iterator_id(build
, depth
);
1277 node
= isl_ast_node_alloc_for(id
);
1279 node
= isl_ast_node_for_mark_degenerate(node
);
1284 /* Create an AST node for the current dimension based on
1285 * the schedule domain "bounds" and return the node encapsulated
1286 * in an isl_ast_graft.
1288 * "executed" is the current inverse schedule, taking into account
1289 * the bounds in "bounds"
1290 * "domain" is the domain of "executed", with inner dimensions projected out.
1291 * It may be a strict subset of "bounds" in case "bounds" was created
1292 * based on the atomic option or based on separation with explicit bounds.
1294 * "domain" may satisfy additional equalities that result
1295 * from intersecting "executed" with "bounds" in add_node.
1296 * It may also satisfy some global constraints that were dropped out because
1297 * we performed separation with explicit bounds.
1298 * The very first step is then to copy these constraints to "bounds".
1300 * Since we may be calling before_each_for and after_each_for
1301 * callbacks, we record the current inverse schedule in the build.
1303 * We consider three builds,
1304 * "build" is the one in which the current level is created,
1305 * "body_build" is the build in which the next level is created,
1306 * "sub_build" is essentially the same as "body_build", except that
1307 * the depth has not been increased yet.
1309 * "build" already contains information (in strides and offsets)
1310 * about the strides at the current level, but this information is not
1311 * reflected in the build->domain.
1312 * We first add this information and the "bounds" to the sub_build->domain.
1313 * isl_ast_build_set_loop_bounds checks whether the current dimension attains
1314 * only a single value and whether this single value can be represented using
1315 * a single affine expression.
1316 * In the first case, the current level is considered "degenerate".
1317 * In the second, sub-case, the current level is considered "eliminated".
1318 * Eliminated level don't need to be reflected in the AST since we can
1319 * simply plug in the affine expression. For degenerate, but non-eliminated,
1320 * levels, we do introduce a for node, but mark is as degenerate so that
1321 * it can be printed as an assignment of the single value to the loop
1324 * If the current level is eliminated, we explicitly plug in the value
1325 * for the current level found by isl_ast_build_set_loop_bounds in the
1326 * inverse schedule. This ensures that if we are working on a slice
1327 * of the domain based on information available in the inverse schedule
1328 * and the build domain, that then this information is also reflected
1329 * in the inverse schedule. This operation also eliminates the current
1330 * dimension from the inverse schedule making sure no inner dimensions depend
1331 * on the current dimension. Otherwise, we create a for node, marking
1332 * it degenerate if appropriate. The initial for node is still incomplete
1333 * and will be completed in either refine_degenerate or refine_generic.
1335 * We then generate a sequence of grafts for the next level,
1336 * create a surrounding graft for the current level and insert
1337 * the for node we created (if the current level is not eliminated).
1339 * Finally, we set the bounds of the for loop and insert guards
1340 * (either in the AST or in the graft) in one of
1341 * refine_eliminated, refine_degenerate or refine_generic.
1343 static __isl_give isl_ast_graft
*create_node_scaled(
1344 __isl_take isl_union_map
*executed
,
1345 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1346 __isl_take isl_ast_build
*build
)
1349 int degenerate
, eliminated
;
1350 isl_basic_set
*hull
;
1351 isl_ast_node
*node
= NULL
;
1352 isl_ast_graft
*graft
;
1353 isl_ast_graft_list
*children
;
1354 isl_ast_build
*sub_build
;
1355 isl_ast_build
*body_build
;
1357 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1358 domain
= isl_set_detect_equalities(domain
);
1359 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1360 bounds
= isl_basic_set_intersect(bounds
, hull
);
1361 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1363 depth
= isl_ast_build_get_depth(build
);
1364 sub_build
= isl_ast_build_copy(build
);
1365 sub_build
= isl_ast_build_include_stride(sub_build
);
1366 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1367 isl_basic_set_copy(bounds
));
1368 degenerate
= isl_ast_build_has_value(sub_build
);
1369 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1370 if (degenerate
< 0 || eliminated
< 0)
1371 executed
= isl_union_map_free(executed
);
1373 executed
= plug_in_values(executed
, sub_build
);
1375 node
= create_for(build
, degenerate
);
1377 body_build
= isl_ast_build_copy(sub_build
);
1378 body_build
= isl_ast_build_increase_depth(body_build
);
1380 node
= before_each_for(node
, body_build
);
1381 children
= generate_next_level(executed
,
1382 isl_ast_build_copy(body_build
));
1384 graft
= isl_ast_graft_alloc_level(children
, build
, sub_build
);
1386 graft
= isl_ast_graft_insert_for(graft
, node
);
1388 graft
= refine_eliminated(graft
, bounds
, build
);
1389 else if (degenerate
)
1390 graft
= refine_degenerate(graft
, bounds
, build
, sub_build
);
1392 graft
= refine_generic(graft
, bounds
, domain
, build
);
1394 graft
= after_each_for(graft
, body_build
);
1396 isl_ast_build_free(body_build
);
1397 isl_ast_build_free(sub_build
);
1398 isl_ast_build_free(build
);
1399 isl_basic_set_free(bounds
);
1400 isl_set_free(domain
);
1405 /* Internal data structure for checking if all constraints involving
1406 * the input dimension "depth" are such that the other coefficients
1407 * are multiples of "m", reducing "m" if they are not.
1408 * If "m" is reduced all the way down to "1", then the check has failed
1409 * and we break out of the iteration.
1411 struct isl_check_scaled_data
{
1416 /* If constraint "c" involves the input dimension data->depth,
1417 * then make sure that all the other coefficients are multiples of data->m,
1418 * reducing data->m if needed.
1419 * Break out of the iteration if data->m has become equal to "1".
1421 static int constraint_check_scaled(__isl_take isl_constraint
*c
, void *user
)
1423 struct isl_check_scaled_data
*data
= user
;
1425 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1428 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1429 isl_constraint_free(c
);
1433 for (i
= 0; i
< 4; ++i
) {
1434 n
= isl_constraint_dim(c
, t
[i
]);
1435 for (j
= 0; j
< n
; ++j
) {
1438 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1440 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1442 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1443 data
->m
= isl_val_gcd(data
->m
, d
);
1444 if (isl_val_is_one(data
->m
))
1451 isl_constraint_free(c
);
1453 return i
< 4 ? -1 : 0;
1456 /* For each constraint of "bmap" that involves the input dimension data->depth,
1457 * make sure that all the other coefficients are multiples of data->m,
1458 * reducing data->m if needed.
1459 * Break out of the iteration if data->m has become equal to "1".
1461 static int basic_map_check_scaled(__isl_take isl_basic_map
*bmap
, void *user
)
1465 r
= isl_basic_map_foreach_constraint(bmap
,
1466 &constraint_check_scaled
, user
);
1467 isl_basic_map_free(bmap
);
1472 /* For each constraint of "map" that involves the input dimension data->depth,
1473 * make sure that all the other coefficients are multiples of data->m,
1474 * reducing data->m if needed.
1475 * Break out of the iteration if data->m has become equal to "1".
1477 static int map_check_scaled(__isl_take isl_map
*map
, void *user
)
1481 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1487 /* Create an AST node for the current dimension based on
1488 * the schedule domain "bounds" and return the node encapsulated
1489 * in an isl_ast_graft.
1491 * "executed" is the current inverse schedule, taking into account
1492 * the bounds in "bounds"
1493 * "domain" is the domain of "executed", with inner dimensions projected out.
1496 * Before moving on to the actual AST node construction in create_node_scaled,
1497 * we first check if the current dimension is strided and if we can scale
1498 * down this stride. Note that we only do this if the ast_build_scale_strides
1501 * In particular, let the current dimension take on values
1505 * with a an integer. We check if we can find an integer m that (obviouly)
1506 * divides both f and s.
1508 * If so, we check if the current dimension only appears in constraints
1509 * where the coefficients of the other variables are multiples of m.
1510 * We perform this extra check to avoid the risk of introducing
1511 * divisions by scaling down the current dimension.
1513 * If so, we scale the current dimension down by a factor of m.
1514 * That is, we plug in
1518 * Note that in principle we could always scale down strided loops
1523 * but this may result in i' taking on larger values than the original i,
1524 * due to the shift by "f".
1525 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1527 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1528 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1529 __isl_take isl_ast_build
*build
)
1531 struct isl_check_scaled_data data
;
1536 ctx
= isl_ast_build_get_ctx(build
);
1537 if (!isl_options_get_ast_build_scale_strides(ctx
))
1538 return create_node_scaled(executed
, bounds
, domain
, build
);
1540 data
.depth
= isl_ast_build_get_depth(build
);
1541 if (!isl_ast_build_has_stride(build
, data
.depth
))
1542 return create_node_scaled(executed
, bounds
, domain
, build
);
1544 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1545 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1547 offset
= isl_aff_free(offset
);
1548 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1549 d
= isl_aff_get_denominator_val(offset
);
1551 executed
= isl_union_map_free(executed
);
1553 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1554 data
.m
= isl_val_div(data
.m
, d
);
1556 data
.m
= isl_val_set_si(data
.m
, 1);
1560 if (!isl_val_is_one(data
.m
)) {
1561 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1563 !isl_val_is_one(data
.m
))
1564 executed
= isl_union_map_free(executed
);
1567 if (!isl_val_is_one(data
.m
)) {
1572 isl_union_map
*umap
;
1574 space
= isl_ast_build_get_space(build
, 1);
1575 space
= isl_space_map_from_set(space
);
1576 ma
= isl_multi_aff_identity(space
);
1577 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1578 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1579 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1581 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1582 isl_multi_aff_copy(ma
));
1583 domain
= isl_set_preimage_multi_aff(domain
,
1584 isl_multi_aff_copy(ma
));
1585 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1586 umap
= isl_union_map_from_map(map
);
1587 executed
= isl_union_map_apply_domain(executed
,
1588 isl_union_map_copy(umap
));
1589 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1592 isl_aff_free(offset
);
1593 isl_val_free(data
.m
);
1595 return create_node_scaled(executed
, bounds
, domain
, build
);
1598 /* Add the basic set to the list that "user" points to.
1600 static int collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1602 isl_basic_set_list
**list
= user
;
1604 *list
= isl_basic_set_list_add(*list
, bset
);
1609 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1611 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1612 __isl_take isl_set
*set
)
1616 isl_basic_set_list
*list
;
1621 ctx
= isl_set_get_ctx(set
);
1623 n
= isl_set_n_basic_set(set
);
1624 list
= isl_basic_set_list_alloc(ctx
, n
);
1625 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1626 list
= isl_basic_set_list_free(list
);
1632 /* Generate code for the schedule domain "bounds"
1633 * and add the result to "list".
1635 * We mainly detect strides and additional equalities here
1636 * and then pass over control to create_node.
1638 * "bounds" reflects the bounds on the current dimension and possibly
1639 * some extra conditions on outer dimensions.
1640 * It does not, however, include any divs involving the current dimension,
1641 * so it does not capture any stride constraints.
1642 * We therefore need to compute that part of the schedule domain that
1643 * intersects with "bounds" and derive the strides from the result.
1645 static __isl_give isl_ast_graft_list
*add_node(
1646 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1647 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1649 isl_ast_graft
*graft
;
1650 isl_set
*domain
= NULL
;
1651 isl_union_set
*uset
;
1654 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1655 executed
= isl_union_map_intersect_domain(executed
, uset
);
1656 empty
= isl_union_map_is_empty(executed
);
1662 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1663 domain
= isl_set_from_union_set(uset
);
1664 domain
= isl_ast_build_compute_gist(build
, domain
);
1665 empty
= isl_set_is_empty(domain
);
1671 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1672 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1674 graft
= create_node(executed
, bounds
, domain
,
1675 isl_ast_build_copy(build
));
1676 list
= isl_ast_graft_list_add(list
, graft
);
1677 isl_ast_build_free(build
);
1680 list
= isl_ast_graft_list_free(list
);
1682 isl_set_free(domain
);
1683 isl_basic_set_free(bounds
);
1684 isl_union_map_free(executed
);
1685 isl_ast_build_free(build
);
1689 /* Does any element of i follow or coincide with any element of j
1690 * at the current depth for equal values of the outer dimensions?
1692 static int domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1693 __isl_keep isl_basic_set
*j
, void *user
)
1695 int depth
= *(int *) user
;
1696 isl_basic_map
*test
;
1700 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1701 isl_basic_set_copy(j
));
1702 for (l
= 0; l
< depth
; ++l
)
1703 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1705 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1706 isl_dim_out
, depth
);
1707 empty
= isl_basic_map_is_empty(test
);
1708 isl_basic_map_free(test
);
1710 return empty
< 0 ? -1 : !empty
;
1713 /* Split up each element of "list" into a part that is related to "bset"
1714 * according to "gt" and a part that is not.
1715 * Return a list that consist of "bset" and all the pieces.
1717 static __isl_give isl_basic_set_list
*add_split_on(
1718 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1719 __isl_keep isl_basic_map
*gt
)
1722 isl_basic_set_list
*res
;
1725 bset
= isl_basic_set_free(bset
);
1727 gt
= isl_basic_map_copy(gt
);
1728 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1729 n
= isl_basic_set_list_n_basic_set(list
);
1730 res
= isl_basic_set_list_from_basic_set(bset
);
1731 for (i
= 0; res
&& i
< n
; ++i
) {
1732 isl_basic_set
*bset
;
1733 isl_set
*set1
, *set2
;
1734 isl_basic_map
*bmap
;
1737 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1738 bmap
= isl_basic_map_copy(gt
);
1739 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1740 bset
= isl_basic_map_range(bmap
);
1741 empty
= isl_basic_set_is_empty(bset
);
1743 res
= isl_basic_set_list_free(res
);
1745 isl_basic_set_free(bset
);
1746 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1747 res
= isl_basic_set_list_add(res
, bset
);
1751 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1752 set1
= isl_set_from_basic_set(bset
);
1753 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1754 set2
= isl_set_from_basic_set(bset
);
1755 set1
= isl_set_subtract(set2
, set1
);
1756 set1
= isl_set_make_disjoint(set1
);
1758 res
= isl_basic_set_list_concat(res
,
1759 isl_basic_set_list_from_set(set1
));
1761 isl_basic_map_free(gt
);
1762 isl_basic_set_list_free(list
);
1766 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1767 __isl_keep isl_basic_set_list
*domain_list
,
1768 __isl_keep isl_union_map
*executed
,
1769 __isl_keep isl_ast_build
*build
);
1771 /* Internal data structure for add_nodes.
1773 * "executed" and "build" are extra arguments to be passed to add_node.
1774 * "list" collects the results.
1776 struct isl_add_nodes_data
{
1777 isl_union_map
*executed
;
1778 isl_ast_build
*build
;
1780 isl_ast_graft_list
*list
;
1783 /* Generate code for the schedule domains in "scc"
1784 * and add the results to "list".
1786 * The domains in "scc" form a strongly connected component in the ordering.
1787 * If the number of domains in "scc" is larger than 1, then this means
1788 * that we cannot determine a valid ordering for the domains in the component.
1789 * This should be fairly rare because the individual domains
1790 * have been made disjoint first.
1791 * The problem is that the domains may be integrally disjoint but not
1792 * rationally disjoint. For example, we may have domains
1794 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1796 * These two domains have an empty intersection, but their rational
1797 * relaxations do intersect. It is impossible to order these domains
1798 * in the second dimension because the first should be ordered before
1799 * the second for outer dimension equal to 0, while it should be ordered
1800 * after for outer dimension equal to 1.
1802 * This may happen in particular in case of unrolling since the domain
1803 * of each slice is replaced by its simple hull.
1805 * For each basic set i in "scc" and for each of the following basic sets j,
1806 * we split off that part of the basic set i that shares the outer dimensions
1807 * with j and lies before j in the current dimension.
1808 * We collect all the pieces in a new list that replaces "scc".
1810 * While the elements in "scc" should be disjoint, we double-check
1811 * this property to avoid running into an infinite recursion in case
1812 * they intersect due to some internal error.
1814 static int add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1816 struct isl_add_nodes_data
*data
= user
;
1818 isl_basic_set
*bset
, *first
;
1819 isl_basic_set_list
*list
;
1823 n
= isl_basic_set_list_n_basic_set(scc
);
1824 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
1826 isl_basic_set_list_free(scc
);
1827 data
->list
= add_node(data
->list
,
1828 isl_union_map_copy(data
->executed
), bset
,
1829 isl_ast_build_copy(data
->build
));
1830 return data
->list
? 0 : -1;
1833 depth
= isl_ast_build_get_depth(data
->build
);
1834 space
= isl_basic_set_get_space(bset
);
1835 space
= isl_space_map_from_set(space
);
1836 gt
= isl_basic_map_universe(space
);
1837 for (i
= 0; i
< depth
; ++i
)
1838 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
1839 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
1841 first
= isl_basic_set_copy(bset
);
1842 list
= isl_basic_set_list_from_basic_set(bset
);
1843 for (i
= 1; i
< n
; ++i
) {
1846 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
1848 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
1850 list
= isl_basic_set_list_free(list
);
1852 isl_die(isl_basic_set_list_get_ctx(scc
),
1854 "basic sets in scc are assumed to be disjoint",
1855 list
= isl_basic_set_list_free(list
));
1857 list
= add_split_on(list
, bset
, gt
);
1859 isl_basic_set_free(first
);
1860 isl_basic_map_free(gt
);
1861 isl_basic_set_list_free(scc
);
1863 data
->list
= isl_ast_graft_list_concat(data
->list
,
1864 generate_sorted_domains(scc
, data
->executed
, data
->build
));
1865 isl_basic_set_list_free(scc
);
1867 return data
->list
? 0 : -1;
1870 /* Sort the domains in "domain_list" according to the execution order
1871 * at the current depth (for equal values of the outer dimensions),
1872 * generate code for each of them, collecting the results in a list.
1873 * If no code is generated (because the intersection of the inverse schedule
1874 * with the domains turns out to be empty), then an empty list is returned.
1876 * The caller is responsible for ensuring that the basic sets in "domain_list"
1877 * are pair-wise disjoint. It can, however, in principle happen that
1878 * two basic sets should be ordered one way for one value of the outer
1879 * dimensions and the other way for some other value of the outer dimensions.
1880 * We therefore play safe and look for strongly connected components.
1881 * The function add_nodes takes care of handling non-trivial components.
1883 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1884 __isl_keep isl_basic_set_list
*domain_list
,
1885 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
1888 struct isl_add_nodes_data data
;
1895 ctx
= isl_basic_set_list_get_ctx(domain_list
);
1896 n
= isl_basic_set_list_n_basic_set(domain_list
);
1897 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
1901 return add_node(data
.list
, isl_union_map_copy(executed
),
1902 isl_basic_set_list_get_basic_set(domain_list
, 0),
1903 isl_ast_build_copy(build
));
1905 depth
= isl_ast_build_get_depth(build
);
1906 data
.executed
= executed
;
1908 if (isl_basic_set_list_foreach_scc(domain_list
,
1909 &domain_follows_at_depth
, &depth
,
1910 &add_nodes
, &data
) < 0)
1911 data
.list
= isl_ast_graft_list_free(data
.list
);
1916 /* Do i and j share any values for the outer dimensions?
1918 static int shared_outer(__isl_keep isl_basic_set
*i
,
1919 __isl_keep isl_basic_set
*j
, void *user
)
1921 int depth
= *(int *) user
;
1922 isl_basic_map
*test
;
1926 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1927 isl_basic_set_copy(j
));
1928 for (l
= 0; l
< depth
; ++l
)
1929 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1931 empty
= isl_basic_map_is_empty(test
);
1932 isl_basic_map_free(test
);
1934 return empty
< 0 ? -1 : !empty
;
1937 /* Internal data structure for generate_sorted_domains_wrap.
1939 * "n" is the total number of basic sets
1940 * "executed" and "build" are extra arguments to be passed
1941 * to generate_sorted_domains.
1943 * "single" is set to 1 by generate_sorted_domains_wrap if there
1944 * is only a single component.
1945 * "list" collects the results.
1947 struct isl_ast_generate_parallel_domains_data
{
1949 isl_union_map
*executed
;
1950 isl_ast_build
*build
;
1953 isl_ast_graft_list
*list
;
1956 /* Call generate_sorted_domains on "scc", fuse the result into a list
1957 * with either zero or one graft and collect the these single element
1958 * lists into data->list.
1960 * If there is only one component, i.e., if the number of basic sets
1961 * in the current component is equal to the total number of basic sets,
1962 * then data->single is set to 1 and the result of generate_sorted_domains
1965 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
1968 struct isl_ast_generate_parallel_domains_data
*data
= user
;
1969 isl_ast_graft_list
*list
;
1971 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
1972 data
->single
= isl_basic_set_list_n_basic_set(scc
) == data
->n
;
1974 list
= isl_ast_graft_list_fuse(list
, data
->build
);
1978 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
1980 isl_basic_set_list_free(scc
);
1987 /* Look for any (weakly connected) components in the "domain_list"
1988 * of domains that share some values of the outer dimensions.
1989 * That is, domains in different components do not share any values
1990 * of the outer dimensions. This means that these components
1991 * can be freely reordered.
1992 * Within each of the components, we sort the domains according
1993 * to the execution order at the current depth.
1995 * If there is more than one component, then generate_sorted_domains_wrap
1996 * fuses the result of each call to generate_sorted_domains
1997 * into a list with either zero or one graft and collects these (at most)
1998 * single element lists into a bigger list. This means that the elements of the
1999 * final list can be freely reordered. In particular, we sort them
2000 * according to an arbitrary but fixed ordering to ease merging of
2001 * graft lists from different components.
2003 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2004 __isl_keep isl_basic_set_list
*domain_list
,
2005 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2008 struct isl_ast_generate_parallel_domains_data data
;
2013 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2015 return generate_sorted_domains(domain_list
, executed
, build
);
2017 depth
= isl_ast_build_get_depth(build
);
2019 data
.executed
= executed
;
2022 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2023 &generate_sorted_domains_wrap
,
2025 data
.list
= isl_ast_graft_list_free(data
.list
);
2028 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2033 /* Internal data for separate_domain.
2035 * "explicit" is set if we only want to use explicit bounds.
2037 * "domain" collects the separated domains.
2039 struct isl_separate_domain_data
{
2040 isl_ast_build
*build
;
2045 /* Extract implicit bounds on the current dimension for the executed "map".
2047 * The domain of "map" may involve inner dimensions, so we
2048 * need to eliminate them.
2050 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2051 __isl_keep isl_ast_build
*build
)
2055 domain
= isl_map_domain(map
);
2056 domain
= isl_ast_build_eliminate(build
, domain
);
2061 /* Extract explicit bounds on the current dimension for the executed "map".
2063 * Rather than eliminating the inner dimensions as in implicit_bounds,
2064 * we simply drop any constraints involving those inner dimensions.
2065 * The idea is that most bounds that are implied by constraints on the
2066 * inner dimensions will be enforced by for loops and not by explicit guards.
2067 * There is then no need to separate along those bounds.
2069 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2070 __isl_keep isl_ast_build
*build
)
2075 dim
= isl_map_dim(map
, isl_dim_out
);
2076 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2078 domain
= isl_map_domain(map
);
2079 depth
= isl_ast_build_get_depth(build
);
2080 dim
= isl_set_dim(domain
, isl_dim_set
);
2081 domain
= isl_set_detect_equalities(domain
);
2082 domain
= isl_set_drop_constraints_involving_dims(domain
,
2083 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2084 domain
= isl_set_remove_divs_involving_dims(domain
,
2085 isl_dim_set
, depth
, 1);
2086 domain
= isl_set_remove_unknown_divs(domain
);
2091 /* Split data->domain into pieces that intersect with the range of "map"
2092 * and pieces that do not intersect with the range of "map"
2093 * and then add that part of the range of "map" that does not intersect
2094 * with data->domain.
2096 static int separate_domain(__isl_take isl_map
*map
, void *user
)
2098 struct isl_separate_domain_data
*data
= user
;
2103 domain
= explicit_bounds(map
, data
->build
);
2105 domain
= implicit_bounds(map
, data
->build
);
2107 domain
= isl_set_coalesce(domain
);
2108 domain
= isl_set_make_disjoint(domain
);
2109 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2110 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2111 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2112 data
->domain
= isl_set_union(data
->domain
, d1
);
2113 data
->domain
= isl_set_union(data
->domain
, d2
);
2118 /* Separate the schedule domains of "executed".
2120 * That is, break up the domain of "executed" into basic sets,
2121 * such that for each basic set S, every element in S is associated with
2122 * the same domain spaces.
2124 * "space" is the (single) domain space of "executed".
2126 static __isl_give isl_set
*separate_schedule_domains(
2127 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2128 __isl_keep isl_ast_build
*build
)
2130 struct isl_separate_domain_data data
= { build
};
2133 ctx
= isl_ast_build_get_ctx(build
);
2134 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2135 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2136 data
.domain
= isl_set_empty(space
);
2137 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2138 data
.domain
= isl_set_free(data
.domain
);
2140 isl_union_map_free(executed
);
2144 /* Temporary data used during the search for a lower bound for unrolling.
2146 * "domain" is the original set for which to find a lower bound
2147 * "depth" is the dimension for which to find a lower boudn
2149 * "lower" is the best lower bound found so far. It is NULL if we have not
2151 * "n" is the corresponding size. If lower is NULL, then the value of n
2154 struct isl_find_unroll_data
{
2162 /* Check if we can use "c" as a lower bound and if it is better than
2163 * any previously found lower bound.
2165 * If "c" does not involve the dimension at the current depth,
2166 * then we cannot use it.
2167 * Otherwise, let "c" be of the form
2171 * We compute the maximal value of
2173 * -ceil(f(j)/a)) + i + 1
2175 * over the domain. If there is such a value "n", then we know
2177 * -ceil(f(j)/a)) + i + 1 <= n
2181 * i < ceil(f(j)/a)) + n
2183 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2184 * We just need to check if we have found any lower bound before and
2185 * if the new lower bound is better (smaller n) than the previously found
2188 static int update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2189 __isl_keep isl_constraint
*c
)
2191 isl_aff
*aff
, *lower
;
2194 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2197 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2198 lower
= isl_aff_ceil(lower
);
2199 aff
= isl_aff_copy(lower
);
2200 aff
= isl_aff_neg(aff
);
2201 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2202 aff
= isl_aff_add_constant_si(aff
, 1);
2203 max
= isl_set_max_val(data
->domain
, aff
);
2208 if (isl_val_is_infty(max
)) {
2210 isl_aff_free(lower
);
2214 if (isl_val_cmp_si(max
, INT_MAX
) <= 0 &&
2215 (!data
->lower
|| isl_val_cmp_si(max
, *data
->n
) < 0)) {
2216 isl_aff_free(data
->lower
);
2217 data
->lower
= lower
;
2218 *data
->n
= isl_val_get_num_si(max
);
2220 isl_aff_free(lower
);
2225 isl_aff_free(lower
);
2229 /* Check if we can use "c" as a lower bound and if it is better than
2230 * any previously found lower bound.
2232 static int constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2234 struct isl_find_unroll_data
*data
;
2237 data
= (struct isl_find_unroll_data
*) user
;
2238 r
= update_unrolling_lower_bound(data
, c
);
2239 isl_constraint_free(c
);
2244 /* Look for a lower bound l(i) on the dimension at "depth"
2245 * and a size n such that "domain" is a subset of
2247 * { [i] : l(i) <= i_d < l(i) + n }
2249 * where d is "depth" and l(i) depends only on earlier dimensions.
2250 * Furthermore, try and find a lower bound such that n is as small as possible.
2251 * In particular, "n" needs to be finite.
2253 * Inner dimensions have been eliminated from "domain" by the caller.
2255 * We first construct a collection of lower bounds on the input set
2256 * by computing its simple hull. We then iterate through them,
2257 * discarding those that we cannot use (either because they do not
2258 * involve the dimension at "depth" or because they have no corresponding
2259 * upper bound, meaning that "n" would be unbounded) and pick out the
2260 * best from the remaining ones.
2262 * If we cannot find a suitable lower bound, then we consider that
2265 static __isl_give isl_aff
*find_unroll_lower_bound(__isl_keep isl_set
*domain
,
2268 struct isl_find_unroll_data data
= { domain
, depth
, NULL
, n
};
2269 isl_basic_set
*hull
;
2271 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2273 if (isl_basic_set_foreach_constraint(hull
,
2274 &constraint_find_unroll
, &data
) < 0)
2277 isl_basic_set_free(hull
);
2280 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2281 "cannot find lower bound for unrolling", return NULL
);
2285 isl_basic_set_free(hull
);
2286 return isl_aff_free(data
.lower
);
2289 /* Return the constraint
2291 * i_"depth" = aff + offset
2293 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2296 aff
= isl_aff_copy(aff
);
2297 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2298 aff
= isl_aff_add_constant_si(aff
, offset
);
2299 return isl_equality_from_aff(aff
);
2302 /* Data structure for storing the results and the intermediate objects
2303 * of compute_domains.
2305 * "list" is the main result of the function and contains a list
2306 * of disjoint basic sets for which code should be generated.
2308 * "executed" and "build" are inputs to compute_domains.
2309 * "schedule_domain" is the domain of "executed".
2311 * "option" constains the domains at the current depth that should by
2312 * atomic, separated or unrolled. These domains are as specified by
2313 * the user, except that inner dimensions have been eliminated and
2314 * that they have been made pair-wise disjoint.
2316 * "sep_class" contains the user-specified split into separation classes
2317 * specialized to the current depth.
2318 * "done" contains the union of the separation domains that have already
2321 struct isl_codegen_domains
{
2322 isl_basic_set_list
*list
;
2324 isl_union_map
*executed
;
2325 isl_ast_build
*build
;
2326 isl_set
*schedule_domain
;
2334 /* Extend domains->list with a list of basic sets, one for each value
2335 * of the current dimension in "domain" and remove the corresponding
2336 * sets from the class domain. Return the updated class domain.
2337 * The divs that involve the current dimension have not been projected out
2340 * Since we are going to be iterating over the individual values,
2341 * we first check if there are any strides on the current dimension.
2342 * If there is, we rewrite the current dimension i as
2344 * i = stride i' + offset
2346 * and then iterate over individual values of i' instead.
2348 * We then look for a lower bound on i' and a size such that the domain
2351 * { [j,i'] : l(j) <= i' < l(j) + n }
2353 * and then take slices of the domain at values of i'
2354 * between l(j) and l(j) + n - 1.
2356 * We compute the unshifted simple hull of each slice to ensure that
2357 * we have a single basic set per offset. The slicing constraint
2358 * may get simplified away before the unshifted simple hull is taken
2359 * and may therefore in some rare cases disappear from the result.
2360 * We therefore explicitly add the constraint back after computing
2361 * the unshifted simple hull to ensure that the basic sets
2362 * remain disjoint. The constraints that are dropped by taking the hull
2363 * will be taken into account at the next level, as in the case of the
2366 * Finally, we map i' back to i and add each basic set to the list.
2367 * Since we may have dropped some constraints, we intersect with
2368 * the class domain again to ensure that each element in the list
2369 * is disjoint from the other class domains.
2371 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2372 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2378 isl_multi_aff
*expansion
;
2379 isl_basic_map
*bmap
;
2380 isl_set
*unroll_domain
;
2381 isl_ast_build
*build
;
2384 return isl_set_free(class_domain
);
2386 ctx
= isl_set_get_ctx(domain
);
2387 depth
= isl_ast_build_get_depth(domains
->build
);
2388 build
= isl_ast_build_copy(domains
->build
);
2389 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2390 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
2391 expansion
= isl_ast_build_get_stride_expansion(build
);
2393 domain
= isl_set_preimage_multi_aff(domain
,
2394 isl_multi_aff_copy(expansion
));
2395 domain
= isl_ast_build_eliminate_divs(build
, domain
);
2397 isl_ast_build_free(build
);
2399 lower
= find_unroll_lower_bound(domain
, depth
, &n
);
2401 class_domain
= isl_set_free(class_domain
);
2403 bmap
= isl_basic_map_from_multi_aff(expansion
);
2405 unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2407 for (i
= 0; class_domain
&& i
< n
; ++i
) {
2409 isl_basic_set
*bset
;
2410 isl_constraint
*slice
;
2411 isl_basic_set_list
*list
;
2413 slice
= at_offset(depth
, lower
, i
);
2414 set
= isl_set_copy(domain
);
2415 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2416 bset
= isl_set_unshifted_simple_hull(set
);
2417 bset
= isl_basic_set_add_constraint(bset
, slice
);
2418 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2419 set
= isl_set_from_basic_set(bset
);
2420 unroll_domain
= isl_set_union(unroll_domain
, isl_set_copy(set
));
2421 set
= isl_set_intersect(set
, isl_set_copy(class_domain
));
2422 set
= isl_set_make_disjoint(set
);
2423 list
= isl_basic_set_list_from_set(set
);
2424 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2427 class_domain
= isl_set_subtract(class_domain
, unroll_domain
);
2429 isl_aff_free(lower
);
2430 isl_set_free(domain
);
2431 isl_basic_map_free(bmap
);
2433 return class_domain
;
2436 /* Add domains to domains->list for each individual value of the current
2437 * dimension, for that part of the schedule domain that lies in the
2438 * intersection of the option domain and the class domain.
2439 * Remove the corresponding sets from the class domain and
2440 * return the updated class domain.
2442 * We first break up the unroll option domain into individual pieces
2443 * and then handle each of them separately. The unroll option domain
2444 * has been made disjoint in compute_domains_init_options,
2446 * Note that we actively want to combine different pieces of the
2447 * schedule domain that have the same value at the current dimension.
2448 * We therefore need to break up the unroll option domain before
2449 * intersecting with class and schedule domain, hoping that the
2450 * unroll option domain specified by the user is relatively simple.
2452 static __isl_give isl_set
*compute_unroll_domains(
2453 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2455 isl_set
*unroll_domain
;
2456 isl_basic_set_list
*unroll_list
;
2460 empty
= isl_set_is_empty(domains
->option
[unroll
]);
2462 return isl_set_free(class_domain
);
2464 return class_domain
;
2466 unroll_domain
= isl_set_copy(domains
->option
[unroll
]);
2467 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2469 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2470 for (i
= 0; i
< n
; ++i
) {
2471 isl_basic_set
*bset
;
2473 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2474 unroll_domain
= isl_set_from_basic_set(bset
);
2475 unroll_domain
= isl_set_intersect(unroll_domain
,
2476 isl_set_copy(class_domain
));
2477 unroll_domain
= isl_set_intersect(unroll_domain
,
2478 isl_set_copy(domains
->schedule_domain
));
2480 empty
= isl_set_is_empty(unroll_domain
);
2481 if (empty
>= 0 && empty
) {
2482 isl_set_free(unroll_domain
);
2486 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2489 isl_basic_set_list_free(unroll_list
);
2491 return class_domain
;
2494 /* Try and construct a single basic set that includes the intersection of
2495 * the schedule domain, the atomic option domain and the class domain.
2496 * Add the resulting basic set(s) to domains->list and remove them
2497 * from class_domain. Return the updated class domain.
2499 * We construct a single domain rather than trying to combine
2500 * the schedule domains of individual domains because we are working
2501 * within a single component so that non-overlapping schedule domains
2502 * should already have been separated.
2503 * We do however need to make sure that this single domains is a subset
2504 * of the class domain so that it would not intersect with any other
2505 * class domains. This means that we may end up splitting up the atomic
2506 * domain in case separation classes are being used.
2508 * "domain" is the intersection of the schedule domain and the class domain,
2509 * with inner dimensions projected out.
2511 static __isl_give isl_set
*compute_atomic_domain(
2512 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2514 isl_basic_set
*bset
;
2515 isl_basic_set_list
*list
;
2516 isl_set
*domain
, *atomic_domain
;
2519 domain
= isl_set_copy(domains
->option
[atomic
]);
2520 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2521 domain
= isl_set_intersect(domain
,
2522 isl_set_copy(domains
->schedule_domain
));
2523 empty
= isl_set_is_empty(domain
);
2525 class_domain
= isl_set_free(class_domain
);
2527 isl_set_free(domain
);
2528 return class_domain
;
2531 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2532 domain
= isl_set_coalesce(domain
);
2533 bset
= isl_set_unshifted_simple_hull(domain
);
2534 domain
= isl_set_from_basic_set(bset
);
2535 atomic_domain
= isl_set_copy(domain
);
2536 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2537 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2538 domain
= isl_set_make_disjoint(domain
);
2539 list
= isl_basic_set_list_from_set(domain
);
2540 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2542 return class_domain
;
2545 /* Split up the schedule domain into uniform basic sets,
2546 * in the sense that each element in a basic set is associated to
2547 * elements of the same domains, and add the result to domains->list.
2548 * Do this for that part of the schedule domain that lies in the
2549 * intersection of "class_domain" and the separate option domain.
2551 * "class_domain" may or may not include the constraints
2552 * of the schedule domain, but this does not make a difference
2553 * since we are going to intersect it with the domain of the inverse schedule.
2554 * If it includes schedule domain constraints, then they may involve
2555 * inner dimensions, but we will eliminate them in separation_domain.
2557 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2558 __isl_keep isl_set
*class_domain
)
2562 isl_union_map
*executed
;
2563 isl_basic_set_list
*list
;
2566 domain
= isl_set_copy(domains
->option
[separate
]);
2567 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2568 executed
= isl_union_map_copy(domains
->executed
);
2569 executed
= isl_union_map_intersect_domain(executed
,
2570 isl_union_set_from_set(domain
));
2571 empty
= isl_union_map_is_empty(executed
);
2572 if (empty
< 0 || empty
) {
2573 isl_union_map_free(executed
);
2574 return empty
< 0 ? -1 : 0;
2577 space
= isl_set_get_space(class_domain
);
2578 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2580 list
= isl_basic_set_list_from_set(domain
);
2581 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2586 /* Split up the domain at the current depth into disjoint
2587 * basic sets for which code should be generated separately
2588 * for the given separation class domain.
2590 * If any separation classes have been defined, then "class_domain"
2591 * is the domain of the current class and does not refer to inner dimensions.
2592 * Otherwise, "class_domain" is the universe domain.
2594 * We first make sure that the class domain is disjoint from
2595 * previously considered class domains.
2597 * The separate domains can be computed directly from the "class_domain".
2599 * The unroll, atomic and remainder domains need the constraints
2600 * from the schedule domain.
2602 * For unrolling, the actual schedule domain is needed (with divs that
2603 * may refer to the current dimension) so that stride detection can be
2606 * For atomic and remainder domains, inner dimensions and divs involving
2607 * the current dimensions should be eliminated.
2608 * In case we are working within a separation class, we need to intersect
2609 * the result with the current "class_domain" to ensure that the domains
2610 * are disjoint from those generated from other class domains.
2612 * The domain that has been made atomic may be larger than specified
2613 * by the user since it needs to be representable as a single basic set.
2614 * This possibly larger domain is removed from class_domain by
2615 * compute_atomic_domain. It is computed first so that the extended domain
2616 * would not overlap with any domains computed before.
2617 * Similary, the unrolled domains may have some constraints removed and
2618 * may therefore also be larger than specified by the user.
2620 * If anything is left after handling separate, unroll and atomic,
2621 * we split it up into basic sets and append the basic sets to domains->list.
2623 static int compute_partial_domains(struct isl_codegen_domains
*domains
,
2624 __isl_take isl_set
*class_domain
)
2626 isl_basic_set_list
*list
;
2629 class_domain
= isl_set_subtract(class_domain
,
2630 isl_set_copy(domains
->done
));
2631 domains
->done
= isl_set_union(domains
->done
,
2632 isl_set_copy(class_domain
));
2634 class_domain
= compute_atomic_domain(domains
, class_domain
);
2635 class_domain
= compute_unroll_domains(domains
, class_domain
);
2637 domain
= isl_set_copy(class_domain
);
2639 if (compute_separate_domain(domains
, domain
) < 0)
2641 domain
= isl_set_subtract(domain
,
2642 isl_set_copy(domains
->option
[separate
]));
2644 domain
= isl_set_intersect(domain
,
2645 isl_set_copy(domains
->schedule_domain
));
2647 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2648 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2650 domain
= isl_set_coalesce(domain
);
2651 domain
= isl_set_make_disjoint(domain
);
2653 list
= isl_basic_set_list_from_set(domain
);
2654 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2656 isl_set_free(class_domain
);
2660 isl_set_free(domain
);
2661 isl_set_free(class_domain
);
2665 /* Split up the domain at the current depth into disjoint
2666 * basic sets for which code should be generated separately
2667 * for the separation class identified by "pnt".
2669 * We extract the corresponding class domain from domains->sep_class,
2670 * eliminate inner dimensions and pass control to compute_partial_domains.
2672 static int compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
2674 struct isl_codegen_domains
*domains
= user
;
2679 class_set
= isl_set_from_point(pnt
);
2680 domain
= isl_map_domain(isl_map_intersect_range(
2681 isl_map_copy(domains
->sep_class
), class_set
));
2682 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
2683 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2685 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
2689 isl_set_free(domain
);
2693 return compute_partial_domains(domains
, domain
);
2696 /* Extract the domains at the current depth that should be atomic,
2697 * separated or unrolled and store them in option.
2699 * The domains specified by the user might overlap, so we make
2700 * them disjoint by subtracting earlier domains from later domains.
2702 static void compute_domains_init_options(isl_set
*option
[3],
2703 __isl_keep isl_ast_build
*build
)
2705 enum isl_ast_build_domain_type type
, type2
;
2707 for (type
= atomic
; type
<= separate
; ++type
) {
2708 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
2709 for (type2
= atomic
; type2
< type
; ++type2
)
2710 option
[type
] = isl_set_subtract(option
[type
],
2711 isl_set_copy(option
[type2
]));
2714 option
[unroll
] = isl_set_coalesce(option
[unroll
]);
2715 option
[unroll
] = isl_set_make_disjoint(option
[unroll
]);
2718 /* Split up the domain at the current depth into disjoint
2719 * basic sets for which code should be generated separately,
2720 * based on the user-specified options.
2721 * Return the list of disjoint basic sets.
2723 * There are three kinds of domains that we need to keep track of.
2724 * - the "schedule domain" is the domain of "executed"
2725 * - the "class domain" is the domain corresponding to the currrent
2727 * - the "option domain" is the domain corresponding to one of the options
2728 * atomic, unroll or separate
2730 * We first consider the individial values of the separation classes
2731 * and split up the domain for each of them separately.
2732 * Finally, we consider the remainder. If no separation classes were
2733 * specified, then we call compute_partial_domains with the universe
2734 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2735 * with inner dimensions removed. We do this because we want to
2736 * avoid computing the complement of the class domains (i.e., the difference
2737 * between the universe and domains->done).
2739 static __isl_give isl_basic_set_list
*compute_domains(
2740 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2742 struct isl_codegen_domains domains
;
2745 isl_union_set
*schedule_domain
;
2749 enum isl_ast_build_domain_type type
;
2755 ctx
= isl_union_map_get_ctx(executed
);
2756 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
2758 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
2759 domain
= isl_set_from_union_set(schedule_domain
);
2761 compute_domains_init_options(domains
.option
, build
);
2763 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
2764 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
2765 n_param
= isl_set_dim(classes
, isl_dim_param
);
2766 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
2768 space
= isl_set_get_space(domain
);
2769 domains
.build
= build
;
2770 domains
.schedule_domain
= isl_set_copy(domain
);
2771 domains
.executed
= executed
;
2772 domains
.done
= isl_set_empty(space
);
2774 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
2775 domains
.list
= isl_basic_set_list_free(domains
.list
);
2776 isl_set_free(classes
);
2778 empty
= isl_set_is_empty(domains
.done
);
2780 domains
.list
= isl_basic_set_list_free(domains
.list
);
2781 domain
= isl_set_free(domain
);
2783 isl_set_free(domain
);
2784 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
2786 domain
= isl_ast_build_eliminate(build
, domain
);
2788 if (compute_partial_domains(&domains
, domain
) < 0)
2789 domains
.list
= isl_basic_set_list_free(domains
.list
);
2791 isl_set_free(domains
.schedule_domain
);
2792 isl_set_free(domains
.done
);
2793 isl_map_free(domains
.sep_class
);
2794 for (type
= atomic
; type
<= separate
; ++type
)
2795 isl_set_free(domains
.option
[type
]);
2797 return domains
.list
;
2800 /* Generate code for a single component, after shifting (if any)
2803 * We first split up the domain at the current depth into disjoint
2804 * basic sets based on the user-specified options.
2805 * Then we generated code for each of them and concatenate the results.
2807 static __isl_give isl_ast_graft_list
*generate_shifted_component(
2808 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
2810 isl_basic_set_list
*domain_list
;
2811 isl_ast_graft_list
*list
= NULL
;
2813 domain_list
= compute_domains(executed
, build
);
2814 list
= generate_parallel_domains(domain_list
, executed
, build
);
2816 isl_basic_set_list_free(domain_list
);
2817 isl_union_map_free(executed
);
2818 isl_ast_build_free(build
);
2823 struct isl_set_map_pair
{
2828 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2829 * of indices into the "domain" array,
2830 * return the union of the "map" fields of the elements
2831 * indexed by the first "n" elements of "order".
2833 static __isl_give isl_union_map
*construct_component_executed(
2834 struct isl_set_map_pair
*domain
, int *order
, int n
)
2838 isl_union_map
*executed
;
2840 map
= isl_map_copy(domain
[order
[0]].map
);
2841 executed
= isl_union_map_from_map(map
);
2842 for (i
= 1; i
< n
; ++i
) {
2843 map
= isl_map_copy(domain
[order
[i
]].map
);
2844 executed
= isl_union_map_add_map(executed
, map
);
2850 /* Generate code for a single component, after shifting (if any)
2853 * The component inverse schedule is specified as the "map" fields
2854 * of the elements of "domain" indexed by the first "n" elements of "order".
2856 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
2857 struct isl_set_map_pair
*domain
, int *order
, int n
,
2858 __isl_take isl_ast_build
*build
)
2860 isl_union_map
*executed
;
2862 executed
= construct_component_executed(domain
, order
, n
);
2863 return generate_shifted_component(executed
, build
);
2866 /* Does set dimension "pos" of "set" have an obviously fixed value?
2868 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
2873 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
2876 fixed
= !isl_val_is_nan(v
);
2882 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2883 * of indices into the "domain" array,
2884 * do all (except for at most one) of the "set" field of the elements
2885 * indexed by the first "n" elements of "order" have a fixed value
2886 * at position "depth"?
2888 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
2889 int *order
, int n
, int depth
)
2894 for (i
= 0; i
< n
; ++i
) {
2897 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
2910 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2911 * of indices into the "domain" array,
2912 * eliminate the inner dimensions from the "set" field of the elements
2913 * indexed by the first "n" elements of "order", provided the current
2914 * dimension does not have a fixed value.
2916 * Return the index of the first element in "order" with a corresponding
2917 * "set" field that does not have an (obviously) fixed value.
2919 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
2920 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
2925 for (i
= n
- 1; i
>= 0; --i
) {
2927 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
2932 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
2933 domain
[order
[i
]].set
);
2940 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2941 * of indices into the "domain" array,
2942 * find the element of "domain" (amongst those indexed by the first "n"
2943 * elements of "order") with the "set" field that has the smallest
2944 * value for the current iterator.
2946 * Note that the domain with the smallest value may depend on the parameters
2947 * and/or outer loop dimension. Since the result of this function is only
2948 * used as heuristic, we only make a reasonable attempt at finding the best
2949 * domain, one that should work in case a single domain provides the smallest
2950 * value for the current dimension over all values of the parameters
2951 * and outer dimensions.
2953 * In particular, we compute the smallest value of the first domain
2954 * and replace it by that of any later domain if that later domain
2955 * has a smallest value that is smaller for at least some value
2956 * of the parameters and outer dimensions.
2958 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
2959 __isl_keep isl_ast_build
*build
)
2965 min_first
= isl_ast_build_map_to_iterator(build
,
2966 isl_set_copy(domain
[order
[0]].set
));
2967 min_first
= isl_map_lexmin(min_first
);
2969 for (i
= 1; i
< n
; ++i
) {
2970 isl_map
*min
, *test
;
2973 min
= isl_ast_build_map_to_iterator(build
,
2974 isl_set_copy(domain
[order
[i
]].set
));
2975 min
= isl_map_lexmin(min
);
2976 test
= isl_map_copy(min
);
2977 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
2978 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
2979 empty
= isl_map_is_empty(test
);
2981 if (empty
>= 0 && !empty
) {
2982 isl_map_free(min_first
);
2992 isl_map_free(min_first
);
2994 return i
< n
? -1 : first
;
2997 /* Construct a shifted inverse schedule based on the original inverse schedule,
2998 * the stride and the offset.
3000 * The original inverse schedule is specified as the "map" fields
3001 * of the elements of "domain" indexed by the first "n" elements of "order".
3003 * "stride" and "offset" are such that the difference
3004 * between the values of the current dimension of domain "i"
3005 * and the values of the current dimension for some reference domain are
3008 * stride * integer + offset[i]
3010 * Moreover, 0 <= offset[i] < stride.
3012 * For each domain, we create a map
3014 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3016 * where j refers to the current dimension and the other dimensions are
3017 * unchanged, and apply this map to the original schedule domain.
3019 * For example, for the original schedule
3021 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3023 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3024 * we apply the mapping
3028 * to the schedule of the "A" domain and the mapping
3030 * { [j - 1] -> [j, 1] }
3032 * to the schedule of the "B" domain.
3035 * Note that after the transformation, the differences between pairs
3036 * of values of the current dimension over all domains are multiples
3037 * of stride and that we have therefore exposed the stride.
3040 * To see that the mapping preserves the lexicographic order,
3041 * first note that each of the individual maps above preserves the order.
3042 * If the value of the current iterator is j1 in one domain and j2 in another,
3043 * then if j1 = j2, we know that the same map is applied to both domains
3044 * and the order is preserved.
3045 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3046 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3050 * and the order is preserved.
3051 * If c1 < c2, then we know
3057 * j2 - j1 = n * s + r
3059 * with n >= 0 and 0 <= r < s.
3060 * In other words, r = c2 - c1.
3071 * (j1 - c1, c1) << (j2 - c2, c2)
3073 * with "<<" the lexicographic order, proving that the order is preserved
3076 static __isl_give isl_union_map
*contruct_shifted_executed(
3077 struct isl_set_map_pair
*domain
, int *order
, int n
,
3078 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3079 __isl_take isl_ast_build
*build
)
3082 isl_union_map
*executed
;
3088 depth
= isl_ast_build_get_depth(build
);
3089 space
= isl_ast_build_get_space(build
, 1);
3090 executed
= isl_union_map_empty(isl_space_copy(space
));
3091 space
= isl_space_map_from_set(space
);
3092 map
= isl_map_identity(isl_space_copy(space
));
3093 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3094 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3095 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3097 c
= isl_equality_alloc(isl_local_space_from_space(space
));
3098 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3099 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3101 for (i
= 0; i
< n
; ++i
) {
3105 v
= isl_multi_val_get_val(offset
, i
);
3108 map_i
= isl_map_copy(map
);
3109 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
3112 c
= isl_constraint_set_constant_val(c
, v
);
3113 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
3115 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
3117 executed
= isl_union_map_add_map(executed
, map_i
);
3120 isl_constraint_free(c
);
3124 executed
= isl_union_map_free(executed
);
3129 /* Generate code for a single component, after exposing the stride,
3130 * given that the schedule domain is "shifted strided".
3132 * The component inverse schedule is specified as the "map" fields
3133 * of the elements of "domain" indexed by the first "n" elements of "order".
3135 * The schedule domain being "shifted strided" means that the differences
3136 * between the values of the current dimension of domain "i"
3137 * and the values of the current dimension for some reference domain are
3140 * stride * integer + offset[i]
3142 * We first look for the domain with the "smallest" value for the current
3143 * dimension and adjust the offsets such that the offset of the "smallest"
3144 * domain is equal to zero. The other offsets are reduced modulo stride.
3146 * Based on this information, we construct a new inverse schedule in
3147 * contruct_shifted_executed that exposes the stride.
3148 * Since this involves the introduction of a new schedule dimension,
3149 * the build needs to be changed accodingly.
3150 * After computing the AST, the newly introduced dimension needs
3151 * to be removed again from the list of grafts. We do this by plugging
3152 * in a mapping that represents the new schedule domain in terms of the
3153 * old schedule domain.
3155 static __isl_give isl_ast_graft_list
*generate_shift_component(
3156 struct isl_set_map_pair
*domain
, int *order
, int n
,
3157 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3158 __isl_take isl_ast_build
*build
)
3160 isl_ast_graft_list
*list
;
3167 isl_multi_aff
*ma
, *zero
;
3168 isl_union_map
*executed
;
3170 ctx
= isl_ast_build_get_ctx(build
);
3171 depth
= isl_ast_build_get_depth(build
);
3173 first
= first_offset(domain
, order
, n
, build
);
3177 mv
= isl_multi_val_copy(offset
);
3178 val
= isl_multi_val_get_val(offset
, first
);
3179 val
= isl_val_neg(val
);
3180 mv
= isl_multi_val_add_val(mv
, val
);
3181 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
3183 executed
= contruct_shifted_executed(domain
, order
, n
, stride
, mv
,
3185 space
= isl_ast_build_get_space(build
, 1);
3186 space
= isl_space_map_from_set(space
);
3187 ma
= isl_multi_aff_identity(isl_space_copy(space
));
3188 space
= isl_space_from_domain(isl_space_domain(space
));
3189 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3190 zero
= isl_multi_aff_zero(space
);
3191 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
3192 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
3193 list
= generate_shifted_component(executed
, build
);
3195 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
3197 isl_multi_val_free(mv
);
3201 isl_ast_build_free(build
);
3205 /* Generate code for a single component.
3207 * The component inverse schedule is specified as the "map" fields
3208 * of the elements of "domain" indexed by the first "n" elements of "order".
3210 * This function may modify the "set" fields of "domain".
3212 * Before proceeding with the actual code generation for the component,
3213 * we first check if there are any "shifted" strides, meaning that
3214 * the schedule domains of the individual domains are all strided,
3215 * but that they have different offsets, resulting in the union
3216 * of schedule domains not being strided anymore.
3218 * The simplest example is the schedule
3220 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3222 * Both schedule domains are strided, but their union is not.
3223 * This function detects such cases and then rewrites the schedule to
3225 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3227 * In the new schedule, the schedule domains have the same offset (modulo
3228 * the stride), ensuring that the union of schedule domains is also strided.
3231 * If there is only a single domain in the component, then there is
3232 * nothing to do. Similarly, if the current schedule dimension has
3233 * a fixed value for almost all domains then there is nothing to be done.
3234 * In particular, we need at least two domains where the current schedule
3235 * dimension does not have a fixed value.
3236 * Finally, if any of the options refer to the current schedule dimension,
3237 * then we bail out as well. It would be possible to reformulate the options
3238 * in terms of the new schedule domain, but that would introduce constraints
3239 * that separate the domains in the options and that is something we would
3243 * To see if there is any shifted stride, we look at the differences
3244 * between the values of the current dimension in pairs of domains
3245 * for equal values of outer dimensions. These differences should be
3250 * with "m" the stride and "r" a constant. Note that we cannot perform
3251 * this analysis on individual domains as the lower bound in each domain
3252 * may depend on parameters or outer dimensions and so the current dimension
3253 * itself may not have a fixed remainder on division by the stride.
3255 * In particular, we compare the first domain that does not have an
3256 * obviously fixed value for the current dimension to itself and all
3257 * other domains and collect the offsets and the gcd of the strides.
3258 * If the gcd becomes one, then we failed to find shifted strides.
3259 * If the gcd is zero, then the differences were all fixed, meaning
3260 * that some domains had non-obviously fixed values for the current dimension.
3261 * If all the offsets are the same (for those domains that do not have
3262 * an obviously fixed value for the current dimension), then we do not
3263 * apply the transformation.
3264 * If none of the domains were skipped, then there is nothing to do.
3265 * If some of them were skipped, then if we apply separation, the schedule
3266 * domain should get split in pieces with a (non-shifted) stride.
3268 * Otherwise, we apply a shift to expose the stride in
3269 * generate_shift_component.
3271 static __isl_give isl_ast_graft_list
*generate_component(
3272 struct isl_set_map_pair
*domain
, int *order
, int n
,
3273 __isl_take isl_ast_build
*build
)
3280 isl_val
*gcd
= NULL
;
3284 isl_ast_graft_list
*list
;
3287 depth
= isl_ast_build_get_depth(build
);
3290 if (skip
>= 0 && !skip
)
3291 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
3292 if (skip
>= 0 && !skip
)
3293 skip
= isl_ast_build_options_involve_depth(build
);
3297 return generate_shifted_component_from_list(domain
,
3300 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
3304 ctx
= isl_ast_build_get_ctx(build
);
3306 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
3309 for (i
= 0; i
< n
; ++i
) {
3312 map
= isl_map_from_domain_and_range(
3313 isl_set_copy(domain
[order
[base
]].set
),
3314 isl_set_copy(domain
[order
[i
]].set
));
3315 for (d
= 0; d
< depth
; ++d
)
3316 map
= isl_map_equate(map
, isl_dim_in
, d
,
3318 deltas
= isl_map_deltas(map
);
3319 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
3320 isl_set_free(deltas
);
3327 gcd
= isl_val_gcd(gcd
, m
);
3328 if (isl_val_is_one(gcd
)) {
3332 mv
= isl_multi_val_set_val(mv
, i
, r
);
3334 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3340 if (fixed
&& i
> base
) {
3342 a
= isl_multi_val_get_val(mv
, i
);
3343 b
= isl_multi_val_get_val(mv
, base
);
3344 if (isl_val_ne(a
, b
))
3351 if (res
< 0 || !gcd
) {
3352 isl_ast_build_free(build
);
3354 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
3355 list
= generate_shifted_component_from_list(domain
,
3358 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
3363 isl_multi_val_free(mv
);
3367 isl_ast_build_free(build
);
3371 /* Store both "map" itself and its domain in the
3372 * structure pointed to by *next and advance to the next array element.
3374 static int extract_domain(__isl_take isl_map
*map
, void *user
)
3376 struct isl_set_map_pair
**next
= user
;
3378 (*next
)->map
= isl_map_copy(map
);
3379 (*next
)->set
= isl_map_domain(map
);
3385 /* Internal data for any_scheduled_after.
3387 * "depth" is the number of loops that have already been generated
3388 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3389 * "domain" is an array of set-map pairs corresponding to the different
3390 * iteration domains. The set is the schedule domain, i.e., the domain
3391 * of the inverse schedule, while the map is the inverse schedule itself.
3393 struct isl_any_scheduled_after_data
{
3395 int group_coscheduled
;
3396 struct isl_set_map_pair
*domain
;
3399 /* Is any element of domain "i" scheduled after any element of domain "j"
3400 * (for a common iteration of the first data->depth loops)?
3402 * data->domain[i].set contains the domain of the inverse schedule
3403 * for domain "i", i.e., elements in the schedule domain.
3405 * If data->group_coscheduled is set, then we also return 1 if there
3406 * is any pair of elements in the two domains that are scheduled together.
3408 static int any_scheduled_after(int i
, int j
, void *user
)
3410 struct isl_any_scheduled_after_data
*data
= user
;
3411 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
3414 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
3417 follows
= isl_set_follows_at(data
->domain
[i
].set
,
3418 data
->domain
[j
].set
, pos
);
3428 return data
->group_coscheduled
;
3431 /* Look for independent components at the current depth and generate code
3432 * for each component separately. The resulting lists of grafts are
3433 * merged in an attempt to combine grafts with identical guards.
3435 * Code for two domains can be generated separately if all the elements
3436 * of one domain are scheduled before (or together with) all the elements
3437 * of the other domain. We therefore consider the graph with as nodes
3438 * the domains and an edge between two nodes if any element of the first
3439 * node is scheduled after any element of the second node.
3440 * If the ast_build_group_coscheduled is set, then we also add an edge if
3441 * there is any pair of elements in the two domains that are scheduled
3443 * Code is then generated (by generate_component)
3444 * for each of the strongly connected components in this graph
3445 * in their topological order.
3447 * Since the test is performed on the domain of the inverse schedules of
3448 * the different domains, we precompute these domains and store
3449 * them in data.domain.
3451 static __isl_give isl_ast_graft_list
*generate_components(
3452 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3455 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
3456 int n
= isl_union_map_n_map(executed
);
3457 struct isl_any_scheduled_after_data data
;
3458 struct isl_set_map_pair
*next
;
3459 struct isl_tarjan_graph
*g
= NULL
;
3460 isl_ast_graft_list
*list
= NULL
;
3463 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
3469 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
3474 data
.depth
= isl_ast_build_get_depth(build
);
3475 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
3476 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
3478 list
= isl_ast_graft_list_alloc(ctx
, 0);
3482 isl_ast_graft_list
*list_c
;
3485 if (g
->order
[i
] == -1)
3486 isl_die(ctx
, isl_error_internal
, "cannot happen",
3489 while (g
->order
[i
] != -1) {
3493 list_c
= generate_component(data
.domain
,
3494 g
->order
+ first
, i
- first
,
3495 isl_ast_build_copy(build
));
3496 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
3502 error
: list
= isl_ast_graft_list_free(list
);
3503 isl_tarjan_graph_free(g
);
3504 for (i
= 0; i
< n_domain
; ++i
) {
3505 isl_map_free(data
.domain
[i
].map
);
3506 isl_set_free(data
.domain
[i
].set
);
3509 isl_union_map_free(executed
);
3510 isl_ast_build_free(build
);
3515 /* Generate code for the next level (and all inner levels).
3517 * If "executed" is empty, i.e., no code needs to be generated,
3518 * then we return an empty list.
3520 * If we have already generated code for all loop levels, then we pass
3521 * control to generate_inner_level.
3523 * If "executed" lives in a single space, i.e., if code needs to be
3524 * generated for a single domain, then there can only be a single
3525 * component and we go directly to generate_shifted_component.
3526 * Otherwise, we call generate_components to detect the components
3527 * and to call generate_component on each of them separately.
3529 static __isl_give isl_ast_graft_list
*generate_next_level(
3530 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3534 if (!build
|| !executed
)
3537 if (isl_union_map_is_empty(executed
)) {
3538 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
3539 isl_union_map_free(executed
);
3540 isl_ast_build_free(build
);
3541 return isl_ast_graft_list_alloc(ctx
, 0);
3544 depth
= isl_ast_build_get_depth(build
);
3545 if (depth
>= isl_set_dim(build
->domain
, isl_dim_set
))
3546 return generate_inner_level(executed
, build
);
3548 if (isl_union_map_n_map(executed
) == 1)
3549 return generate_shifted_component(executed
, build
);
3551 return generate_components(executed
, build
);
3553 isl_union_map_free(executed
);
3554 isl_ast_build_free(build
);
3558 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3559 * internal, executed and build are the inputs to generate_code.
3560 * list collects the output.
3562 struct isl_generate_code_data
{
3564 isl_union_map
*executed
;
3565 isl_ast_build
*build
;
3567 isl_ast_graft_list
*list
;
3570 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3574 * with E the external build schedule and S the additional schedule "space",
3575 * reformulate the inverse schedule in terms of the internal schedule domain,
3580 * We first obtain a mapping
3584 * take the inverse and the product with S -> S, resulting in
3586 * [I -> S] -> [E -> S]
3588 * Applying the map to the input produces the desired result.
3590 static __isl_give isl_union_map
*internal_executed(
3591 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
3592 __isl_keep isl_ast_build
*build
)
3596 proj
= isl_ast_build_get_schedule_map(build
);
3597 proj
= isl_map_reverse(proj
);
3598 space
= isl_space_map_from_set(isl_space_copy(space
));
3599 id
= isl_map_identity(space
);
3600 proj
= isl_map_product(proj
, id
);
3601 executed
= isl_union_map_apply_domain(executed
,
3602 isl_union_map_from_map(proj
));
3606 /* Generate an AST that visits the elements in the range of data->executed
3607 * in the relative order specified by the corresponding image element(s)
3608 * for those image elements that belong to "set".
3609 * Add the result to data->list.
3611 * The caller ensures that "set" is a universe domain.
3612 * "space" is the space of the additional part of the schedule.
3613 * It is equal to the space of "set" if build->domain is parametric.
3614 * Otherwise, it is equal to the range of the wrapped space of "set".
3616 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3617 * was called from an outside user (data->internal not set), then
3618 * the (inverse) schedule refers to the external build domain and needs to
3619 * be transformed to refer to the internal build domain.
3621 * The build is extended to include the additional part of the schedule.
3622 * If the original build space was not parametric, then the options
3623 * in data->build refer only to the additional part of the schedule
3624 * and they need to be adjusted to refer to the complete AST build
3627 * After having adjusted inverse schedule and build, we start generating
3628 * code with the outer loop of the current code generation
3629 * in generate_next_level.
3631 * If the original build space was not parametric, we undo the embedding
3632 * on the resulting isl_ast_node_list so that it can be used within
3633 * the outer AST build.
3635 static int generate_code_in_space(struct isl_generate_code_data
*data
,
3636 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
3638 isl_union_map
*executed
;
3639 isl_ast_build
*build
;
3640 isl_ast_graft_list
*list
;
3643 executed
= isl_union_map_copy(data
->executed
);
3644 executed
= isl_union_map_intersect_domain(executed
,
3645 isl_union_set_from_set(set
));
3647 embed
= !isl_set_is_params(data
->build
->domain
);
3648 if (embed
&& !data
->internal
)
3649 executed
= internal_executed(executed
, space
, data
->build
);
3651 build
= isl_ast_build_copy(data
->build
);
3652 build
= isl_ast_build_product(build
, space
);
3654 list
= generate_next_level(executed
, build
);
3656 list
= isl_ast_graft_list_unembed(list
, embed
);
3658 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
3663 /* Generate an AST that visits the elements in the range of data->executed
3664 * in the relative order specified by the corresponding domain element(s)
3665 * for those domain elements that belong to "set".
3666 * Add the result to data->list.
3668 * The caller ensures that "set" is a universe domain.
3670 * If the build space S is not parametric, then the space of "set"
3671 * need to be a wrapped relation with S as domain. That is, it needs
3676 * Check this property and pass control to generate_code_in_space
3678 * If the build space is not parametric, then T is the space of "set".
3680 static int generate_code_set(__isl_take isl_set
*set
, void *user
)
3682 struct isl_generate_code_data
*data
= user
;
3683 isl_space
*space
, *build_space
;
3686 space
= isl_set_get_space(set
);
3688 if (isl_set_is_params(data
->build
->domain
))
3689 return generate_code_in_space(data
, set
, space
);
3691 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
3692 space
= isl_space_unwrap(space
);
3693 is_domain
= isl_space_is_domain(build_space
, space
);
3694 isl_space_free(build_space
);
3695 space
= isl_space_range(space
);
3700 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3701 "invalid nested schedule space", goto error
);
3703 return generate_code_in_space(data
, set
, space
);
3706 isl_space_free(space
);
3710 /* Generate an AST that visits the elements in the range of "executed"
3711 * in the relative order specified by the corresponding domain element(s).
3713 * "build" is an isl_ast_build that has either been constructed by
3714 * isl_ast_build_from_context or passed to a callback set by
3715 * isl_ast_build_set_create_leaf.
3716 * In the first case, the space of the isl_ast_build is typically
3717 * a parametric space, although this is currently not enforced.
3718 * In the second case, the space is never a parametric space.
3719 * If the space S is not parametric, then the domain space(s) of "executed"
3720 * need to be wrapped relations with S as domain.
3722 * If the domain of "executed" consists of several spaces, then an AST
3723 * is generated for each of them (in arbitrary order) and the results
3726 * If "internal" is set, then the domain "S" above refers to the internal
3727 * schedule domain representation. Otherwise, it refers to the external
3728 * representation, as returned by isl_ast_build_get_schedule_space.
3730 * We essentially run over all the spaces in the domain of "executed"
3731 * and call generate_code_set on each of them.
3733 static __isl_give isl_ast_graft_list
*generate_code(
3734 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3738 struct isl_generate_code_data data
= { 0 };
3740 isl_union_set
*schedule_domain
;
3741 isl_union_map
*universe
;
3745 space
= isl_ast_build_get_space(build
, 1);
3746 space
= isl_space_align_params(space
,
3747 isl_union_map_get_space(executed
));
3748 space
= isl_space_align_params(space
,
3749 isl_union_map_get_space(build
->options
));
3750 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
3751 executed
= isl_union_map_align_params(executed
, space
);
3752 if (!executed
|| !build
)
3755 ctx
= isl_ast_build_get_ctx(build
);
3757 data
.internal
= internal
;
3758 data
.executed
= executed
;
3760 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
3762 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
3763 schedule_domain
= isl_union_map_domain(universe
);
3764 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
3766 data
.list
= isl_ast_graft_list_free(data
.list
);
3768 isl_union_set_free(schedule_domain
);
3769 isl_union_map_free(executed
);
3771 isl_ast_build_free(build
);
3774 isl_union_map_free(executed
);
3775 isl_ast_build_free(build
);
3779 /* Generate an AST that visits the elements in the domain of "schedule"
3780 * in the relative order specified by the corresponding image element(s).
3782 * "build" is an isl_ast_build that has either been constructed by
3783 * isl_ast_build_from_context or passed to a callback set by
3784 * isl_ast_build_set_create_leaf.
3785 * In the first case, the space of the isl_ast_build is typically
3786 * a parametric space, although this is currently not enforced.
3787 * In the second case, the space is never a parametric space.
3788 * If the space S is not parametric, then the range space(s) of "schedule"
3789 * need to be wrapped relations with S as domain.
3791 * If the range of "schedule" consists of several spaces, then an AST
3792 * is generated for each of them (in arbitrary order) and the results
3795 * We first initialize the local copies of the relevant options.
3796 * We do this here rather than when the isl_ast_build is created
3797 * because the options may have changed between the construction
3798 * of the isl_ast_build and the call to isl_generate_code.
3800 * The main computation is performed on an inverse schedule (with
3801 * the schedule domain in the domain and the elements to be executed
3802 * in the range) called "executed".
3804 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
3805 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
3807 isl_ast_graft_list
*list
;
3809 isl_union_map
*executed
;
3811 build
= isl_ast_build_copy(build
);
3812 build
= isl_ast_build_set_single_valued(build
, 0);
3813 schedule
= isl_union_map_coalesce(schedule
);
3814 executed
= isl_union_map_reverse(schedule
);
3815 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
3816 node
= isl_ast_node_from_graft_list(list
, build
);
3817 isl_ast_build_free(build
);