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 /* Data used in generate_domain.
24 * "build" is the input build.
25 * "list" collects the results.
27 struct isl_generate_domain_data
{
30 isl_ast_graft_list
*list
;
33 static __isl_give isl_ast_graft_list
*generate_next_level(
34 __isl_take isl_union_map
*executed
,
35 __isl_take isl_ast_build
*build
);
36 static __isl_give isl_ast_graft_list
*generate_code(
37 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
40 /* Generate an AST for a single domain based on
41 * the (non single valued) inverse schedule "executed".
43 * We extend the schedule with the iteration domain
44 * and continue generating through a call to generate_code.
46 * In particular, if executed has the form
50 * then we continue generating code on
54 * The extended inverse schedule is clearly single valued
55 * ensuring that the nested generate_code will not reach this function,
56 * but will instead create calls to all elements of D that need
57 * to be executed from the current schedule domain.
59 static int generate_non_single_valued(__isl_take isl_map
*executed
,
60 struct isl_generate_domain_data
*data
)
64 isl_ast_graft_list
*list
;
66 build
= isl_ast_build_copy(data
->build
);
68 identity
= isl_set_identity(isl_map_range(isl_map_copy(executed
)));
69 executed
= isl_map_domain_product(executed
, identity
);
70 build
= isl_ast_build_set_single_valued(build
, 1);
72 list
= generate_code(isl_union_map_from_map(executed
), build
, 1);
74 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
79 /* Call the at_each_domain callback, if requested by the user,
80 * after recording the current inverse schedule in the build.
82 static __isl_give isl_ast_graft
*at_each_domain(__isl_take isl_ast_graft
*graft
,
83 __isl_keep isl_map
*executed
, __isl_keep isl_ast_build
*build
)
86 return isl_ast_graft_free(graft
);
87 if (!build
->at_each_domain
)
90 build
= isl_ast_build_copy(build
);
91 build
= isl_ast_build_set_executed(build
,
92 isl_union_map_from_map(isl_map_copy(executed
)));
94 return isl_ast_graft_free(graft
);
96 graft
->node
= build
->at_each_domain(graft
->node
,
97 build
, build
->at_each_domain_user
);
98 isl_ast_build_free(build
);
101 graft
= isl_ast_graft_free(graft
);
106 /* Generate an AST for a single domain based on
107 * the inverse schedule "executed" and add it to data->list.
109 * If there is more than one domain element associated to the current
110 * schedule "time", then we need to continue the generation process
111 * in generate_non_single_valued.
112 * Note that the inverse schedule being single-valued may depend
113 * on constraints that are only available in the original context
114 * domain specified by the user. We therefore first introduce
115 * the constraints from data->build->domain.
116 * On the other hand, we only perform the test after having taken the gist
117 * of the domain as the resulting map is the one from which the call
118 * expression is constructed. Using this map to construct the call
119 * expression usually yields simpler results.
120 * Because we perform the single-valuedness test on the gisted map,
121 * we may in rare cases fail to recognize that the inverse schedule
122 * is single-valued. This becomes problematic if this happens
123 * from the recursive call through generate_non_single_valued
124 * as we would then end up in an infinite recursion.
125 * We therefore check if we are inside a call to generate_non_single_valued
126 * and revert to the ungisted map if the gisted map turns out not to be
129 * Otherwise, we generate a call expression for the single executed
130 * domain element and put a guard around it based on the (simplified)
131 * domain of "executed".
133 * If the user has set an at_each_domain callback, it is called
134 * on the constructed call expression node.
136 static int generate_domain(__isl_take isl_map
*executed
, void *user
)
138 struct isl_generate_domain_data
*data
= user
;
139 isl_ast_graft
*graft
;
140 isl_ast_graft_list
*list
;
145 executed
= isl_map_intersect_domain(executed
,
146 isl_set_copy(data
->build
->domain
));
147 empty
= isl_map_is_empty(executed
);
151 isl_map_free(executed
);
155 executed
= isl_map_coalesce(executed
);
156 map
= isl_map_copy(executed
);
157 map
= isl_ast_build_compute_gist_map_domain(data
->build
, map
);
158 sv
= isl_map_is_single_valued(map
);
163 if (data
->build
->single_valued
)
164 map
= isl_map_copy(executed
);
166 return generate_non_single_valued(executed
, data
);
168 guard
= isl_map_domain(isl_map_copy(map
));
169 guard
= isl_set_compute_divs(guard
);
170 guard
= isl_set_coalesce(guard
);
171 guard
= isl_ast_build_compute_gist(data
->build
, guard
);
172 graft
= isl_ast_graft_alloc_domain(map
, data
->build
);
173 graft
= at_each_domain(graft
, executed
, data
->build
);
175 isl_map_free(executed
);
176 graft
= isl_ast_graft_add_guard(graft
, guard
, data
->build
);
178 list
= isl_ast_graft_list_from_ast_graft(graft
);
179 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
184 isl_map_free(executed
);
188 /* Call build->create_leaf to a create "leaf" node in the AST,
189 * encapsulate the result in an isl_ast_graft and return the result
190 * as a 1-element list.
192 * Note that the node returned by the user may be an entire tree.
194 * Before we pass control to the user, we first clear some information
195 * from the build that is (presumbably) only meaningful
196 * for the current code generation.
197 * This includes the create_leaf callback itself, so we make a copy
198 * of the build first.
200 static __isl_give isl_ast_graft_list
*call_create_leaf(
201 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
204 isl_ast_graft
*graft
;
205 isl_ast_build
*user_build
;
207 user_build
= isl_ast_build_copy(build
);
208 user_build
= isl_ast_build_set_executed(user_build
, executed
);
209 user_build
= isl_ast_build_clear_local_info(user_build
);
213 node
= build
->create_leaf(user_build
, build
->create_leaf_user
);
214 graft
= isl_ast_graft_alloc(node
, build
);
215 isl_ast_build_free(build
);
216 return isl_ast_graft_list_from_ast_graft(graft
);
219 /* Generate an AST after having handled the complete schedule
220 * of this call to the code generator.
222 * If the user has specified a create_leaf callback, control
223 * is passed to the user in call_create_leaf.
225 * Otherwise, we generate one or more calls for each individual
226 * domain in generate_domain.
228 static __isl_give isl_ast_graft_list
*generate_inner_level(
229 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
232 struct isl_generate_domain_data data
= { build
};
234 if (!build
|| !executed
)
237 if (build
->create_leaf
)
238 return call_create_leaf(executed
, build
);
240 ctx
= isl_union_map_get_ctx(executed
);
241 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
242 if (isl_union_map_foreach_map(executed
, &generate_domain
, &data
) < 0)
243 data
.list
= isl_ast_graft_list_free(data
.list
);
246 error
: data
.list
= NULL
;
247 isl_ast_build_free(build
);
248 isl_union_map_free(executed
);
252 /* Call the before_each_for callback, if requested by the user.
254 static __isl_give isl_ast_node
*before_each_for(__isl_take isl_ast_node
*node
,
255 __isl_keep isl_ast_build
*build
)
260 return isl_ast_node_free(node
);
261 if (!build
->before_each_for
)
263 id
= build
->before_each_for(build
, build
->before_each_for_user
);
264 node
= isl_ast_node_set_annotation(node
, id
);
268 /* Call the after_each_for callback, if requested by the user.
270 static __isl_give isl_ast_graft
*after_each_for(__isl_take isl_ast_graft
*graft
,
271 __isl_keep isl_ast_build
*build
)
273 if (!graft
|| !build
)
274 return isl_ast_graft_free(graft
);
275 if (!build
->after_each_for
)
277 graft
->node
= build
->after_each_for(graft
->node
, build
,
278 build
->after_each_for_user
);
280 return isl_ast_graft_free(graft
);
284 /* Plug in all the know values of the current and outer dimensions
285 * in the domain of "executed". In principle, we only need to plug
286 * in the known value of the current dimension since the values of
287 * outer dimensions have been plugged in already.
288 * However, it turns out to be easier to just plug in all known values.
290 static __isl_give isl_union_map
*plug_in_values(
291 __isl_take isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
293 return isl_ast_build_substitute_values_union_map_domain(build
,
297 /* Check if the constraint "c" is a lower bound on dimension "pos",
298 * an upper bound, or independent of dimension "pos".
300 static int constraint_type(isl_constraint
*c
, int pos
)
302 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
304 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
309 /* Compare the types of the constraints "a" and "b",
310 * resulting in constraints that are independent of "depth"
311 * to be sorted before the lower bounds on "depth", which in
312 * turn are sorted before the upper bounds on "depth".
314 static int cmp_constraint(__isl_keep isl_constraint
*a
,
315 __isl_keep isl_constraint
*b
, void *user
)
318 int t1
= constraint_type(a
, *depth
);
319 int t2
= constraint_type(b
, *depth
);
324 /* Extract a lower bound on dimension "pos" from constraint "c".
326 * If the constraint is of the form
330 * then we essentially return
332 * l = ceil(-f(...)/a)
334 * However, if the current dimension is strided, then we need to make
335 * sure that the lower bound we construct is of the form
339 * with f the offset and s the stride.
340 * We therefore compute
342 * f + s * ceil((l - f)/s)
344 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
345 int pos
, __isl_keep isl_ast_build
*build
)
349 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
350 aff
= isl_aff_ceil(aff
);
352 if (isl_ast_build_has_stride(build
, pos
)) {
356 offset
= isl_ast_build_get_offset(build
, pos
);
357 stride
= isl_ast_build_get_stride(build
, pos
);
359 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
360 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(stride
));
361 aff
= isl_aff_ceil(aff
);
362 aff
= isl_aff_scale_val(aff
, stride
);
363 aff
= isl_aff_add(aff
, offset
);
366 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
371 /* Return the exact lower bound (or upper bound if "upper" is set)
372 * of "domain" as a piecewise affine expression.
374 * If we are computing a lower bound (of a strided dimension), then
375 * we need to make sure it is of the form
379 * where f is the offset and s is the stride.
380 * We therefore need to include the stride constraint before computing
383 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
384 __isl_keep isl_ast_build
*build
, int upper
)
389 isl_pw_multi_aff
*pma
;
391 domain
= isl_set_copy(domain
);
393 stride
= isl_ast_build_get_stride_constraint(build
);
394 domain
= isl_set_intersect(domain
, stride
);
396 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
398 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
400 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
401 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
402 isl_pw_multi_aff_free(pma
);
403 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
404 pa
= isl_pw_aff_coalesce(pa
);
409 /* Extract a lower bound on dimension "pos" from each constraint
410 * in "constraints" and return the list of lower bounds.
411 * If "constraints" has zero elements, then we extract a lower bound
412 * from "domain" instead.
414 static __isl_give isl_pw_aff_list
*lower_bounds(
415 __isl_keep isl_constraint_list
*constraints
, int pos
,
416 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
419 isl_pw_aff_list
*list
;
425 n
= isl_constraint_list_n_constraint(constraints
);
428 pa
= exact_bound(domain
, build
, 0);
429 return isl_pw_aff_list_from_pw_aff(pa
);
432 ctx
= isl_ast_build_get_ctx(build
);
433 list
= isl_pw_aff_list_alloc(ctx
,n
);
435 for (i
= 0; i
< n
; ++i
) {
439 c
= isl_constraint_list_get_constraint(constraints
, i
);
440 aff
= lower_bound(c
, pos
, build
);
441 isl_constraint_free(c
);
442 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
448 /* Extract an upper bound on dimension "pos" from each constraint
449 * in "constraints" and return the list of upper bounds.
450 * If "constraints" has zero elements, then we extract an upper bound
451 * from "domain" instead.
453 static __isl_give isl_pw_aff_list
*upper_bounds(
454 __isl_keep isl_constraint_list
*constraints
, int pos
,
455 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
458 isl_pw_aff_list
*list
;
461 n
= isl_constraint_list_n_constraint(constraints
);
464 pa
= exact_bound(domain
, build
, 1);
465 return isl_pw_aff_list_from_pw_aff(pa
);
468 ctx
= isl_ast_build_get_ctx(build
);
469 list
= isl_pw_aff_list_alloc(ctx
,n
);
471 for (i
= 0; i
< n
; ++i
) {
475 c
= isl_constraint_list_get_constraint(constraints
, i
);
476 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
477 isl_constraint_free(c
);
478 aff
= isl_aff_floor(aff
);
479 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
485 /* Callback for sorting the isl_pw_aff_list passed to reduce_list.
487 static int reduce_list_cmp(__isl_keep isl_pw_aff
*a
, __isl_keep isl_pw_aff
*b
,
490 return isl_pw_aff_plain_cmp(a
, b
);
493 /* Return an isl_ast_expr that performs the reduction of type "type"
494 * on AST expressions corresponding to the elements in "list".
496 * The list is assumed to contain at least one element.
497 * If the list contains exactly one element, then the returned isl_ast_expr
498 * simply computes that affine expression.
499 * If the list contains more than one element, then we sort it
500 * using a fairly abitrary but hopefully reasonably stable order.
502 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
503 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
512 n
= isl_pw_aff_list_n_pw_aff(list
);
515 return isl_ast_build_expr_from_pw_aff_internal(build
,
516 isl_pw_aff_list_get_pw_aff(list
, 0));
518 ctx
= isl_pw_aff_list_get_ctx(list
);
519 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
523 list
= isl_pw_aff_list_copy(list
);
524 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
526 return isl_ast_expr_free(expr
);
528 for (i
= 0; i
< n
; ++i
) {
529 isl_ast_expr
*expr_i
;
531 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
532 isl_pw_aff_list_get_pw_aff(list
, i
));
535 expr
->u
.op
.args
[i
] = expr_i
;
538 isl_pw_aff_list_free(list
);
541 isl_pw_aff_list_free(list
);
542 isl_ast_expr_free(expr
);
546 /* Add a guard to "graft" based on "bound" in the case of a degenerate
547 * level (including the special case of an eliminated level).
549 * We eliminate the current dimension, simplify the result in the current
550 * build and add the result as guards to the graft.
552 * Note that we cannot simply drop the constraints on the current dimension
553 * even in the eliminated case, because the single affine expression may
554 * not be explicitly available in "bounds". Moreover, the single affine
555 * expression may only be defined on a subset of the build domain,
556 * so we do in some cases need to insert a guard even in the eliminated case.
558 static __isl_give isl_ast_graft
*add_degenerate_guard(
559 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
560 __isl_keep isl_ast_build
*build
)
565 depth
= isl_ast_build_get_depth(build
);
567 dom
= isl_set_from_basic_set(isl_basic_set_copy(bounds
));
568 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
569 dom
= isl_ast_build_compute_gist(build
, dom
);
571 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
576 /* Add guards implied by the "generated constraints",
577 * but not (necessarily) enforced by the generated AST to "graft".
578 * In particular, if there is any stride constraints,
579 * then add the guard implied by those constraints.
580 * If we have generated a degenerate loop, then add the guard
581 * implied by "bounds" on the outer dimensions, i.e., the guard
582 * that ensures that the single value actually exists.
584 static __isl_give isl_ast_graft
*add_implied_guards(
585 __isl_take isl_ast_graft
*graft
, int degenerate
,
586 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_ast_build
*build
)
588 int depth
, has_stride
;
591 depth
= isl_ast_build_get_depth(build
);
592 has_stride
= isl_ast_build_has_stride(build
, depth
);
593 if (!has_stride
&& !degenerate
)
597 bounds
= isl_basic_set_copy(bounds
);
598 dom
= isl_set_from_basic_set(bounds
);
599 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
600 dom
= isl_ast_build_compute_gist(build
, dom
);
601 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
605 dom
= isl_ast_build_get_stride_constraint(build
);
606 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
607 dom
= isl_ast_build_compute_gist(build
, dom
);
608 graft
= isl_ast_graft_add_guard(graft
, dom
, build
);
614 /* Update "graft" based on "bounds" for the eliminated case.
616 * In the eliminated case, no for node is created, so we only need
617 * to check if "bounds" imply any guards that need to be inserted.
619 static __isl_give isl_ast_graft
*refine_eliminated(
620 __isl_take isl_ast_graft
*graft
, __isl_keep isl_basic_set
*bounds
,
621 __isl_keep isl_ast_build
*build
)
623 return add_degenerate_guard(graft
, bounds
, build
);
626 /* Update "graft" based on "sub_build" for the degenerate case.
628 * "build" is the build in which graft->node was created
629 * "sub_build" contains information about the current level itself,
630 * including the single value attained.
632 * We first set the initialization part of the for loop to the single
633 * value attained by the current dimension.
634 * The increment and condition are not strictly needed as the are known
635 * to be "1" and "iterator <= value" respectively.
637 static __isl_give isl_ast_graft
*refine_degenerate(
638 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
,
639 __isl_keep isl_ast_build
*sub_build
)
643 if (!graft
|| !sub_build
)
644 return isl_ast_graft_free(graft
);
646 value
= isl_pw_aff_copy(sub_build
->value
);
648 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
650 if (!graft
->node
->u
.f
.init
)
651 return isl_ast_graft_free(graft
);
656 /* Return the intersection of constraints in "list" as a set.
658 static __isl_give isl_set
*intersect_constraints(
659 __isl_keep isl_constraint_list
*list
)
664 n
= isl_constraint_list_n_constraint(list
);
666 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
667 "expecting at least one constraint", return NULL
);
669 bset
= isl_basic_set_from_constraint(
670 isl_constraint_list_get_constraint(list
, 0));
671 for (i
= 1; i
< n
; ++i
) {
672 isl_basic_set
*bset_i
;
674 bset_i
= isl_basic_set_from_constraint(
675 isl_constraint_list_get_constraint(list
, i
));
676 bset
= isl_basic_set_intersect(bset
, bset_i
);
679 return isl_set_from_basic_set(bset
);
682 /* Compute the constraints on the outer dimensions enforced by
683 * graft->node and add those constraints to graft->enforced,
684 * in case the upper bound is expressed as a set "upper".
686 * In particular, if l(...) is a lower bound in "lower", and
688 * -a i + f(...) >= 0 or a i <= f(...)
690 * is an upper bound ocnstraint on the current dimension i,
691 * then the for loop enforces the constraint
693 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
695 * We therefore simply take each lower bound in turn, plug it into
696 * the upper bounds and compute the intersection over all lower bounds.
698 * If a lower bound is a rational expression, then
699 * isl_basic_set_preimage_multi_aff will force this rational
700 * expression to have only integer values. However, the loop
701 * itself does not enforce this integrality constraint. We therefore
702 * use the ceil of the lower bounds instead of the lower bounds themselves.
703 * Other constraints will make sure that the for loop is only executed
704 * when each of the lower bounds attains an integral value.
705 * In particular, potentially rational values only occur in
706 * lower_bound if the offset is a (seemingly) rational expression,
707 * but then outer conditions will make sure that this rational expression
708 * only attains integer values.
710 static __isl_give isl_ast_graft
*set_enforced_from_set(
711 __isl_take isl_ast_graft
*graft
,
712 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
715 isl_basic_set
*enforced
;
716 isl_pw_multi_aff
*pma
;
719 if (!graft
|| !lower
)
720 return isl_ast_graft_free(graft
);
722 space
= isl_set_get_space(upper
);
723 enforced
= isl_basic_set_universe(isl_space_copy(space
));
725 space
= isl_space_map_from_set(space
);
726 pma
= isl_pw_multi_aff_identity(space
);
728 n
= isl_pw_aff_list_n_pw_aff(lower
);
729 for (i
= 0; i
< n
; ++i
) {
733 isl_pw_multi_aff
*pma_i
;
735 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
736 pa
= isl_pw_aff_ceil(pa
);
737 pma_i
= isl_pw_multi_aff_copy(pma
);
738 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
739 enforced_i
= isl_set_copy(upper
);
740 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
741 hull
= isl_set_simple_hull(enforced_i
);
742 enforced
= isl_basic_set_intersect(enforced
, hull
);
745 isl_pw_multi_aff_free(pma
);
747 graft
= isl_ast_graft_enforce(graft
, enforced
);
752 /* Compute the constraints on the outer dimensions enforced by
753 * graft->node and add those constraints to graft->enforced,
754 * in case the upper bound is expressed as
755 * a list of affine expressions "upper".
757 * The enforced condition is that each lower bound expression is less
758 * than or equal to each upper bound expression.
760 static __isl_give isl_ast_graft
*set_enforced_from_list(
761 __isl_take isl_ast_graft
*graft
,
762 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
765 isl_basic_set
*enforced
;
767 lower
= isl_pw_aff_list_copy(lower
);
768 upper
= isl_pw_aff_list_copy(upper
);
769 cond
= isl_pw_aff_list_le_set(lower
, upper
);
770 enforced
= isl_set_simple_hull(cond
);
771 graft
= isl_ast_graft_enforce(graft
, enforced
);
776 /* Does "aff" have a negative constant term?
778 static int aff_constant_is_negative(__isl_take isl_set
*set
,
779 __isl_take isl_aff
*aff
, void *user
)
784 v
= isl_aff_get_constant_val(aff
);
785 *neg
= isl_val_is_neg(v
);
790 return *neg
? 0 : -1;
793 /* Does "pa" have a negative constant term over its entire domain?
795 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
, void *user
)
800 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
803 return *neg
? 0 : -1;
806 /* Does each element in "list" have a negative constant term?
808 * The callback terminates the iteration as soon an element has been
809 * found that does not have a negative constant term.
811 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
815 if (isl_pw_aff_list_foreach(list
,
816 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
822 /* Add 1 to each of the elements in "list", where each of these elements
823 * is defined over the internal schedule space of "build".
825 static __isl_give isl_pw_aff_list
*list_add_one(
826 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
833 space
= isl_ast_build_get_space(build
, 1);
834 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
835 aff
= isl_aff_add_constant_si(aff
, 1);
836 one
= isl_pw_aff_from_aff(aff
);
838 n
= isl_pw_aff_list_n_pw_aff(list
);
839 for (i
= 0; i
< n
; ++i
) {
841 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
842 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
843 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
846 isl_pw_aff_free(one
);
851 /* Set the condition part of the for node graft->node in case
852 * the upper bound is represented as a list of piecewise affine expressions.
854 * In particular, set the condition to
856 * iterator <= min(list of upper bounds)
858 * If each of the upper bounds has a negative constant term, then
859 * set the condition to
861 * iterator < min(list of (upper bound + 1)s)
864 static __isl_give isl_ast_graft
*set_for_cond_from_list(
865 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
866 __isl_keep isl_ast_build
*build
)
869 isl_ast_expr
*bound
, *iterator
, *cond
;
870 enum isl_ast_op_type type
= isl_ast_op_le
;
873 return isl_ast_graft_free(graft
);
875 neg
= list_constant_is_negative(list
);
877 return isl_ast_graft_free(graft
);
878 list
= isl_pw_aff_list_copy(list
);
880 list
= list_add_one(list
, build
);
881 type
= isl_ast_op_lt
;
884 bound
= reduce_list(isl_ast_op_min
, list
, build
);
885 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
886 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
887 graft
->node
->u
.f
.cond
= cond
;
889 isl_pw_aff_list_free(list
);
890 if (!graft
->node
->u
.f
.cond
)
891 return isl_ast_graft_free(graft
);
895 /* Set the condition part of the for node graft->node in case
896 * the upper bound is represented as a set.
898 static __isl_give isl_ast_graft
*set_for_cond_from_set(
899 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
900 __isl_keep isl_ast_build
*build
)
907 cond
= isl_ast_build_expr_from_set(build
, isl_set_copy(set
));
908 graft
->node
->u
.f
.cond
= cond
;
909 if (!graft
->node
->u
.f
.cond
)
910 return isl_ast_graft_free(graft
);
914 /* Construct an isl_ast_expr for the increment (i.e., stride) of
915 * the current dimension.
917 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
925 ctx
= isl_ast_build_get_ctx(build
);
926 depth
= isl_ast_build_get_depth(build
);
928 if (!isl_ast_build_has_stride(build
, depth
))
929 return isl_ast_expr_alloc_int_si(ctx
, 1);
931 v
= isl_ast_build_get_stride(build
, depth
);
932 return isl_ast_expr_from_val(v
);
935 /* Should we express the loop condition as
937 * iterator <= min(list of upper bounds)
939 * or as a conjunction of constraints?
941 * The first is constructed from a list of upper bounds.
942 * The second is constructed from a set.
944 * If there are no upper bounds in "constraints", then this could mean
945 * that "domain" simply doesn't have an upper bound or that we didn't
946 * pick any upper bound. In the first case, we want to generate the
947 * loop condition as a(n empty) conjunction of constraints
948 * In the second case, we will compute
949 * a single upper bound from "domain" and so we use the list form.
951 * If there are upper bounds in "constraints",
952 * then we use the list form iff the atomic_upper_bound option is set.
954 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
955 __isl_keep isl_set
*domain
, int depth
)
958 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
960 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
963 /* Fill in the expressions of the for node in graft->node.
966 * - set the initialization part of the loop to the maximum of the lower bounds
967 * - extract the increment from the stride of the current dimension
968 * - construct the for condition either based on a list of upper bounds
969 * or on a set of upper bound constraints.
971 static __isl_give isl_ast_graft
*set_for_node_expressions(
972 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
973 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
974 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
981 build
= isl_ast_build_copy(build
);
982 build
= isl_ast_build_set_enforced(build
,
983 isl_ast_graft_get_enforced(graft
));
986 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
987 node
->u
.f
.inc
= for_inc(build
);
990 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
992 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
994 isl_ast_build_free(build
);
996 if (!node
->u
.f
.iterator
|| !node
->u
.f
.init
||
997 !node
->u
.f
.cond
|| !node
->u
.f
.inc
)
998 return isl_ast_graft_free(graft
);
1003 /* Update "graft" based on "bounds" and "domain" for the generic,
1004 * non-degenerate, case.
1006 * "c_lower" and "c_upper" contain the lower and upper bounds
1007 * that the loop node should express.
1008 * "domain" is the subset of the intersection of the constraints
1009 * for which some code is executed.
1011 * There may be zero lower bounds or zero upper bounds in "constraints"
1012 * in case the list of constraints was created
1013 * based on the atomic option or based on separation with explicit bounds.
1014 * In that case, we use "domain" to derive lower and/or upper bounds.
1016 * We first compute a list of one or more lower bounds.
1018 * Then we decide if we want to express the condition as
1020 * iterator <= min(list of upper bounds)
1022 * or as a conjunction of constraints.
1024 * The set of enforced constraints is then computed either based on
1025 * a list of upper bounds or on a set of upper bound constraints.
1026 * We do not compute any enforced constraints if we were forced
1027 * to compute a lower or upper bound using exact_bound. The domains
1028 * of the resulting expressions may imply some bounds on outer dimensions
1029 * that we do not want to appear in the enforced constraints since
1030 * they are not actually enforced by the corresponding code.
1032 * Finally, we fill in the expressions of the for node.
1034 static __isl_give isl_ast_graft
*refine_generic_bounds(
1035 __isl_take isl_ast_graft
*graft
,
1036 __isl_take isl_constraint_list
*c_lower
,
1037 __isl_take isl_constraint_list
*c_upper
,
1038 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1042 isl_pw_aff_list
*lower
;
1044 isl_set
*upper_set
= NULL
;
1045 isl_pw_aff_list
*upper_list
= NULL
;
1046 int n_lower
, n_upper
;
1048 if (!graft
|| !c_lower
|| !c_upper
|| !build
)
1051 depth
= isl_ast_build_get_depth(build
);
1052 ctx
= isl_ast_graft_get_ctx(graft
);
1054 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1055 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1057 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1059 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1062 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1063 else if (n_upper
> 0)
1064 upper_set
= intersect_constraints(c_upper
);
1066 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1068 if (n_lower
== 0 || n_upper
== 0)
1071 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1073 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1075 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1078 isl_pw_aff_list_free(lower
);
1079 isl_pw_aff_list_free(upper_list
);
1080 isl_set_free(upper_set
);
1081 isl_constraint_list_free(c_lower
);
1082 isl_constraint_list_free(c_upper
);
1086 isl_constraint_list_free(c_lower
);
1087 isl_constraint_list_free(c_upper
);
1088 return isl_ast_graft_free(graft
);
1091 /* Internal data structure used inside count_constraints to keep
1092 * track of the number of constraints that are independent of dimension "pos",
1093 * the lower bounds in "pos" and the upper bounds in "pos".
1095 struct isl_ast_count_constraints_data
{
1103 /* Increment data->n_indep, data->lower or data->upper depending
1104 * on whether "c" is independenct of dimensions data->pos,
1105 * a lower bound or an upper bound.
1107 static int count_constraints(__isl_take isl_constraint
*c
, void *user
)
1109 struct isl_ast_count_constraints_data
*data
= user
;
1111 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1113 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1118 isl_constraint_free(c
);
1123 /* Update "graft" based on "bounds" and "domain" for the generic,
1124 * non-degenerate, case.
1126 * "list" respresent the list of bounds that need to be encoded by
1127 * the for loop (or a guard around the for loop).
1128 * "domain" is the subset of the intersection of the constraints
1129 * for which some code is executed.
1130 * "build" is the build in which graft->node was created.
1132 * We separate lower bounds, upper bounds and constraints that
1133 * are independent of the loop iterator.
1135 * The actual for loop bounds are generated in refine_generic_bounds.
1136 * If there are any constraints that are independent of the loop iterator,
1137 * we need to put a guard around the for loop (which may get hoisted up
1138 * to higher levels) and we call refine_generic_bounds in a build
1139 * where this guard is enforced.
1141 static __isl_give isl_ast_graft
*refine_generic_split(
1142 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1143 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1145 isl_ast_build
*for_build
;
1147 struct isl_ast_count_constraints_data data
;
1148 isl_constraint_list
*lower
;
1149 isl_constraint_list
*upper
;
1152 return isl_ast_graft_free(graft
);
1154 data
.pos
= isl_ast_build_get_depth(build
);
1156 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1158 return isl_ast_graft_free(graft
);
1160 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1161 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1162 isl_constraint_list_free(list
);
1163 return isl_ast_graft_free(graft
);
1166 lower
= isl_constraint_list_copy(list
);
1167 lower
= isl_constraint_list_drop(lower
, 0, data
.n_indep
);
1168 upper
= isl_constraint_list_copy(lower
);
1169 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1170 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1172 if (data
.n_indep
== 0) {
1173 isl_constraint_list_free(list
);
1174 return refine_generic_bounds(graft
, lower
, upper
,
1178 list
= isl_constraint_list_drop(list
, data
.n_indep
,
1179 data
.n_lower
+ data
.n_upper
);
1180 guard
= intersect_constraints(list
);
1181 isl_constraint_list_free(list
);
1183 for_build
= isl_ast_build_copy(build
);
1184 for_build
= isl_ast_build_restrict_pending(for_build
,
1185 isl_set_copy(guard
));
1186 graft
= refine_generic_bounds(graft
, lower
, upper
, domain
, for_build
);
1187 isl_ast_build_free(for_build
);
1189 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
1194 /* Update "graft" based on "bounds" and "domain" for the generic,
1195 * non-degenerate, case.
1197 * "bounds" respresent the bounds that need to be encoded by
1198 * the for loop (or a guard around the for loop).
1199 * "domain" is the subset of "bounds" for which some code is executed.
1200 * "build" is the build in which graft->node was created.
1202 * We break up "bounds" into a list of constraints and continue with
1203 * refine_generic_split.
1205 static __isl_give isl_ast_graft
*refine_generic(
1206 __isl_take isl_ast_graft
*graft
,
1207 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1208 __isl_keep isl_ast_build
*build
)
1210 isl_constraint_list
*list
;
1212 if (!build
|| !graft
)
1213 return isl_ast_graft_free(graft
);
1215 list
= isl_basic_set_get_constraint_list(bounds
);
1217 graft
= refine_generic_split(graft
, list
, domain
, build
);
1222 /* Create a for node for the current level.
1224 * Mark the for node degenerate if "degenerate" is set.
1226 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1236 depth
= isl_ast_build_get_depth(build
);
1237 id
= isl_ast_build_get_iterator_id(build
, depth
);
1238 node
= isl_ast_node_alloc_for(id
);
1240 node
= isl_ast_node_for_mark_degenerate(node
);
1245 /* If the ast_build_exploit_nested_bounds option is set, then return
1246 * the constraints enforced by all elements in "list".
1247 * Otherwise, return the universe.
1249 static __isl_give isl_basic_set
*extract_shared_enforced(
1250 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1258 ctx
= isl_ast_graft_list_get_ctx(list
);
1259 if (isl_options_get_ast_build_exploit_nested_bounds(ctx
))
1260 return isl_ast_graft_list_extract_shared_enforced(list
, build
);
1262 space
= isl_ast_build_get_space(build
, 1);
1263 return isl_basic_set_universe(space
);
1266 /* Create an AST node for the current dimension based on
1267 * the schedule domain "bounds" and return the node encapsulated
1268 * in an isl_ast_graft.
1270 * "executed" is the current inverse schedule, taking into account
1271 * the bounds in "bounds"
1272 * "domain" is the domain of "executed", with inner dimensions projected out.
1273 * It may be a strict subset of "bounds" in case "bounds" was created
1274 * based on the atomic option or based on separation with explicit bounds.
1276 * "domain" may satisfy additional equalities that result
1277 * from intersecting "executed" with "bounds" in add_node.
1278 * It may also satisfy some global constraints that were dropped out because
1279 * we performed separation with explicit bounds.
1280 * The very first step is then to copy these constraints to "bounds".
1282 * Since we may be calling before_each_for and after_each_for
1283 * callbacks, we record the current inverse schedule in the build.
1285 * We consider three builds,
1286 * "build" is the one in which the current level is created,
1287 * "body_build" is the build in which the next level is created,
1288 * "sub_build" is essentially the same as "body_build", except that
1289 * the depth has not been increased yet.
1291 * "build" already contains information (in strides and offsets)
1292 * about the strides at the current level, but this information is not
1293 * reflected in the build->domain.
1294 * We first add this information and the "bounds" to the sub_build->domain.
1295 * isl_ast_build_set_loop_bounds adds the stride information and
1296 * checks whether the current dimension attains
1297 * only a single value and whether this single value can be represented using
1298 * a single affine expression.
1299 * In the first case, the current level is considered "degenerate".
1300 * In the second, sub-case, the current level is considered "eliminated".
1301 * Eliminated levels don't need to be reflected in the AST since we can
1302 * simply plug in the affine expression. For degenerate, but non-eliminated,
1303 * levels, we do introduce a for node, but mark is as degenerate so that
1304 * it can be printed as an assignment of the single value to the loop
1307 * If the current level is eliminated, we explicitly plug in the value
1308 * for the current level found by isl_ast_build_set_loop_bounds in the
1309 * inverse schedule. This ensures that if we are working on a slice
1310 * of the domain based on information available in the inverse schedule
1311 * and the build domain, that then this information is also reflected
1312 * in the inverse schedule. This operation also eliminates the current
1313 * dimension from the inverse schedule making sure no inner dimensions depend
1314 * on the current dimension. Otherwise, we create a for node, marking
1315 * it degenerate if appropriate. The initial for node is still incomplete
1316 * and will be completed in either refine_degenerate or refine_generic.
1318 * We then generate a sequence of grafts for the next level,
1319 * create a surrounding graft for the current level and insert
1320 * the for node we created (if the current level is not eliminated).
1321 * Before creating a graft for the current level, we first extract
1322 * hoistable constraints from the child guards. These constraints
1323 * are used to simplify the child guards and then added to the guard
1324 * of the current graft.
1326 * Finally, we set the bounds of the for loop and insert guards
1327 * (either in the AST or in the graft) in one of
1328 * refine_eliminated, refine_degenerate or refine_generic.
1330 static __isl_give isl_ast_graft
*create_node_scaled(
1331 __isl_take isl_union_map
*executed
,
1332 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1333 __isl_take isl_ast_build
*build
)
1336 int degenerate
, eliminated
;
1337 isl_basic_set
*hull
;
1338 isl_basic_set
*enforced
;
1340 isl_ast_node
*node
= NULL
;
1341 isl_ast_graft
*graft
;
1342 isl_ast_graft_list
*children
;
1343 isl_ast_build
*sub_build
;
1344 isl_ast_build
*body_build
;
1346 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1347 domain
= isl_set_detect_equalities(domain
);
1348 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1349 bounds
= isl_basic_set_intersect(bounds
, hull
);
1350 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1352 depth
= isl_ast_build_get_depth(build
);
1353 sub_build
= isl_ast_build_copy(build
);
1354 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1355 isl_basic_set_copy(bounds
));
1356 degenerate
= isl_ast_build_has_value(sub_build
);
1357 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1358 if (degenerate
< 0 || eliminated
< 0)
1359 executed
= isl_union_map_free(executed
);
1361 executed
= plug_in_values(executed
, sub_build
);
1363 node
= create_for(build
, degenerate
);
1365 body_build
= isl_ast_build_copy(sub_build
);
1366 body_build
= isl_ast_build_increase_depth(body_build
);
1368 node
= before_each_for(node
, body_build
);
1369 children
= generate_next_level(executed
,
1370 isl_ast_build_copy(body_build
));
1372 enforced
= extract_shared_enforced(children
, build
);
1373 hoisted
= isl_ast_graft_list_extract_hoistable_guard(children
, build
);
1374 graft
= isl_ast_graft_alloc_from_children(children
, hoisted
, enforced
,
1377 graft
= isl_ast_graft_insert_for(graft
, node
);
1379 graft
= refine_eliminated(graft
, bounds
, build
);
1380 else if (degenerate
)
1381 graft
= refine_degenerate(graft
, build
, sub_build
);
1383 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1384 graft
= refine_generic(graft
, bounds
, domain
, build
);
1387 graft
= add_implied_guards(graft
, degenerate
, bounds
, build
);
1388 graft
= after_each_for(graft
, body_build
);
1391 isl_ast_build_free(body_build
);
1392 isl_ast_build_free(sub_build
);
1393 isl_ast_build_free(build
);
1394 isl_basic_set_free(bounds
);
1395 isl_set_free(domain
);
1400 /* Internal data structure for checking if all constraints involving
1401 * the input dimension "depth" are such that the other coefficients
1402 * are multiples of "m", reducing "m" if they are not.
1403 * If "m" is reduced all the way down to "1", then the check has failed
1404 * and we break out of the iteration.
1406 struct isl_check_scaled_data
{
1411 /* If constraint "c" involves the input dimension data->depth,
1412 * then make sure that all the other coefficients are multiples of data->m,
1413 * reducing data->m if needed.
1414 * Break out of the iteration if data->m has become equal to "1".
1416 static int constraint_check_scaled(__isl_take isl_constraint
*c
, void *user
)
1418 struct isl_check_scaled_data
*data
= user
;
1420 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1423 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1424 isl_constraint_free(c
);
1428 for (i
= 0; i
< 4; ++i
) {
1429 n
= isl_constraint_dim(c
, t
[i
]);
1430 for (j
= 0; j
< n
; ++j
) {
1433 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1435 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1437 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1438 data
->m
= isl_val_gcd(data
->m
, d
);
1439 if (isl_val_is_one(data
->m
))
1446 isl_constraint_free(c
);
1448 return i
< 4 ? -1 : 0;
1451 /* For each constraint of "bmap" that involves the input dimension data->depth,
1452 * make sure that all the other coefficients are multiples of data->m,
1453 * reducing data->m if needed.
1454 * Break out of the iteration if data->m has become equal to "1".
1456 static int basic_map_check_scaled(__isl_take isl_basic_map
*bmap
, void *user
)
1460 r
= isl_basic_map_foreach_constraint(bmap
,
1461 &constraint_check_scaled
, user
);
1462 isl_basic_map_free(bmap
);
1467 /* For each constraint of "map" that involves the input dimension data->depth,
1468 * make sure that all the other coefficients are multiples of data->m,
1469 * reducing data->m if needed.
1470 * Break out of the iteration if data->m has become equal to "1".
1472 static int map_check_scaled(__isl_take isl_map
*map
, void *user
)
1476 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1482 /* Create an AST node for the current dimension based on
1483 * the schedule domain "bounds" and return the node encapsulated
1484 * in an isl_ast_graft.
1486 * "executed" is the current inverse schedule, taking into account
1487 * the bounds in "bounds"
1488 * "domain" is the domain of "executed", with inner dimensions projected out.
1491 * Before moving on to the actual AST node construction in create_node_scaled,
1492 * we first check if the current dimension is strided and if we can scale
1493 * down this stride. Note that we only do this if the ast_build_scale_strides
1496 * In particular, let the current dimension take on values
1500 * with a an integer. We check if we can find an integer m that (obviously)
1501 * divides both f and s.
1503 * If so, we check if the current dimension only appears in constraints
1504 * where the coefficients of the other variables are multiples of m.
1505 * We perform this extra check to avoid the risk of introducing
1506 * divisions by scaling down the current dimension.
1508 * If so, we scale the current dimension down by a factor of m.
1509 * That is, we plug in
1513 * Note that in principle we could always scale down strided loops
1518 * but this may result in i' taking on larger values than the original i,
1519 * due to the shift by "f".
1520 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1522 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1523 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1524 __isl_take isl_ast_build
*build
)
1526 struct isl_check_scaled_data data
;
1531 ctx
= isl_ast_build_get_ctx(build
);
1532 if (!isl_options_get_ast_build_scale_strides(ctx
))
1533 return create_node_scaled(executed
, bounds
, domain
, build
);
1535 data
.depth
= isl_ast_build_get_depth(build
);
1536 if (!isl_ast_build_has_stride(build
, data
.depth
))
1537 return create_node_scaled(executed
, bounds
, domain
, build
);
1539 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1540 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1542 offset
= isl_aff_free(offset
);
1543 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1544 d
= isl_aff_get_denominator_val(offset
);
1546 executed
= isl_union_map_free(executed
);
1548 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1549 data
.m
= isl_val_div(data
.m
, d
);
1551 data
.m
= isl_val_set_si(data
.m
, 1);
1555 if (!isl_val_is_one(data
.m
)) {
1556 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1558 !isl_val_is_one(data
.m
))
1559 executed
= isl_union_map_free(executed
);
1562 if (!isl_val_is_one(data
.m
)) {
1567 isl_union_map
*umap
;
1569 space
= isl_ast_build_get_space(build
, 1);
1570 space
= isl_space_map_from_set(space
);
1571 ma
= isl_multi_aff_identity(space
);
1572 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1573 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1574 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1576 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1577 isl_multi_aff_copy(ma
));
1578 domain
= isl_set_preimage_multi_aff(domain
,
1579 isl_multi_aff_copy(ma
));
1580 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1581 umap
= isl_union_map_from_map(map
);
1582 executed
= isl_union_map_apply_domain(executed
,
1583 isl_union_map_copy(umap
));
1584 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1587 isl_aff_free(offset
);
1588 isl_val_free(data
.m
);
1590 return create_node_scaled(executed
, bounds
, domain
, build
);
1593 /* Add the basic set to the list that "user" points to.
1595 static int collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1597 isl_basic_set_list
**list
= user
;
1599 *list
= isl_basic_set_list_add(*list
, bset
);
1604 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1606 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1607 __isl_take isl_set
*set
)
1611 isl_basic_set_list
*list
;
1616 ctx
= isl_set_get_ctx(set
);
1618 n
= isl_set_n_basic_set(set
);
1619 list
= isl_basic_set_list_alloc(ctx
, n
);
1620 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1621 list
= isl_basic_set_list_free(list
);
1627 /* Generate code for the schedule domain "bounds"
1628 * and add the result to "list".
1630 * We mainly detect strides here and check if the bounds do not
1631 * conflict with the current build domain
1632 * and then pass over control to create_node.
1634 * "bounds" reflects the bounds on the current dimension and possibly
1635 * some extra conditions on outer dimensions.
1636 * It does not, however, include any divs involving the current dimension,
1637 * so it does not capture any stride constraints.
1638 * We therefore need to compute that part of the schedule domain that
1639 * intersects with "bounds" and derive the strides from the result.
1641 static __isl_give isl_ast_graft_list
*add_node(
1642 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1643 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1645 isl_ast_graft
*graft
;
1646 isl_set
*domain
= NULL
;
1647 isl_union_set
*uset
;
1648 int empty
, disjoint
;
1650 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1651 executed
= isl_union_map_intersect_domain(executed
, uset
);
1652 empty
= isl_union_map_is_empty(executed
);
1658 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1659 domain
= isl_set_from_union_set(uset
);
1660 domain
= isl_ast_build_specialize(build
, domain
);
1662 domain
= isl_set_compute_divs(domain
);
1663 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1664 disjoint
= isl_set_is_disjoint(domain
, build
->domain
);
1670 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1672 graft
= create_node(executed
, bounds
, domain
,
1673 isl_ast_build_copy(build
));
1674 list
= isl_ast_graft_list_add(list
, graft
);
1675 isl_ast_build_free(build
);
1678 list
= isl_ast_graft_list_free(list
);
1680 isl_set_free(domain
);
1681 isl_basic_set_free(bounds
);
1682 isl_union_map_free(executed
);
1683 isl_ast_build_free(build
);
1687 /* Does any element of i follow or coincide with any element of j
1688 * at the current depth for equal values of the outer dimensions?
1690 static int domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1691 __isl_keep isl_basic_set
*j
, void *user
)
1693 int depth
= *(int *) user
;
1694 isl_basic_map
*test
;
1698 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1699 isl_basic_set_copy(j
));
1700 for (l
= 0; l
< depth
; ++l
)
1701 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1703 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1704 isl_dim_out
, depth
);
1705 empty
= isl_basic_map_is_empty(test
);
1706 isl_basic_map_free(test
);
1708 return empty
< 0 ? -1 : !empty
;
1711 /* Split up each element of "list" into a part that is related to "bset"
1712 * according to "gt" and a part that is not.
1713 * Return a list that consist of "bset" and all the pieces.
1715 static __isl_give isl_basic_set_list
*add_split_on(
1716 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1717 __isl_keep isl_basic_map
*gt
)
1720 isl_basic_set_list
*res
;
1723 bset
= isl_basic_set_free(bset
);
1725 gt
= isl_basic_map_copy(gt
);
1726 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1727 n
= isl_basic_set_list_n_basic_set(list
);
1728 res
= isl_basic_set_list_from_basic_set(bset
);
1729 for (i
= 0; res
&& i
< n
; ++i
) {
1730 isl_basic_set
*bset
;
1731 isl_set
*set1
, *set2
;
1732 isl_basic_map
*bmap
;
1735 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1736 bmap
= isl_basic_map_copy(gt
);
1737 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1738 bset
= isl_basic_map_range(bmap
);
1739 empty
= isl_basic_set_is_empty(bset
);
1741 res
= isl_basic_set_list_free(res
);
1743 isl_basic_set_free(bset
);
1744 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1745 res
= isl_basic_set_list_add(res
, bset
);
1749 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1750 set1
= isl_set_from_basic_set(bset
);
1751 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1752 set2
= isl_set_from_basic_set(bset
);
1753 set1
= isl_set_subtract(set2
, set1
);
1754 set1
= isl_set_make_disjoint(set1
);
1756 res
= isl_basic_set_list_concat(res
,
1757 isl_basic_set_list_from_set(set1
));
1759 isl_basic_map_free(gt
);
1760 isl_basic_set_list_free(list
);
1764 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1765 __isl_keep isl_basic_set_list
*domain_list
,
1766 __isl_keep isl_union_map
*executed
,
1767 __isl_keep isl_ast_build
*build
);
1769 /* Internal data structure for add_nodes.
1771 * "executed" and "build" are extra arguments to be passed to add_node.
1772 * "list" collects the results.
1774 struct isl_add_nodes_data
{
1775 isl_union_map
*executed
;
1776 isl_ast_build
*build
;
1778 isl_ast_graft_list
*list
;
1781 /* Generate code for the schedule domains in "scc"
1782 * and add the results to "list".
1784 * The domains in "scc" form a strongly connected component in the ordering.
1785 * If the number of domains in "scc" is larger than 1, then this means
1786 * that we cannot determine a valid ordering for the domains in the component.
1787 * This should be fairly rare because the individual domains
1788 * have been made disjoint first.
1789 * The problem is that the domains may be integrally disjoint but not
1790 * rationally disjoint. For example, we may have domains
1792 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1794 * These two domains have an empty intersection, but their rational
1795 * relaxations do intersect. It is impossible to order these domains
1796 * in the second dimension because the first should be ordered before
1797 * the second for outer dimension equal to 0, while it should be ordered
1798 * after for outer dimension equal to 1.
1800 * This may happen in particular in case of unrolling since the domain
1801 * of each slice is replaced by its simple hull.
1803 * For each basic set i in "scc" and for each of the following basic sets j,
1804 * we split off that part of the basic set i that shares the outer dimensions
1805 * with j and lies before j in the current dimension.
1806 * We collect all the pieces in a new list that replaces "scc".
1808 * While the elements in "scc" should be disjoint, we double-check
1809 * this property to avoid running into an infinite recursion in case
1810 * they intersect due to some internal error.
1812 static int add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1814 struct isl_add_nodes_data
*data
= user
;
1816 isl_basic_set
*bset
, *first
;
1817 isl_basic_set_list
*list
;
1821 n
= isl_basic_set_list_n_basic_set(scc
);
1822 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
1824 isl_basic_set_list_free(scc
);
1825 data
->list
= add_node(data
->list
,
1826 isl_union_map_copy(data
->executed
), bset
,
1827 isl_ast_build_copy(data
->build
));
1828 return data
->list
? 0 : -1;
1831 depth
= isl_ast_build_get_depth(data
->build
);
1832 space
= isl_basic_set_get_space(bset
);
1833 space
= isl_space_map_from_set(space
);
1834 gt
= isl_basic_map_universe(space
);
1835 for (i
= 0; i
< depth
; ++i
)
1836 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
1837 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
1839 first
= isl_basic_set_copy(bset
);
1840 list
= isl_basic_set_list_from_basic_set(bset
);
1841 for (i
= 1; i
< n
; ++i
) {
1844 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
1846 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
1848 list
= isl_basic_set_list_free(list
);
1850 isl_die(isl_basic_set_list_get_ctx(scc
),
1852 "basic sets in scc are assumed to be disjoint",
1853 list
= isl_basic_set_list_free(list
));
1855 list
= add_split_on(list
, bset
, gt
);
1857 isl_basic_set_free(first
);
1858 isl_basic_map_free(gt
);
1859 isl_basic_set_list_free(scc
);
1861 data
->list
= isl_ast_graft_list_concat(data
->list
,
1862 generate_sorted_domains(scc
, data
->executed
, data
->build
));
1863 isl_basic_set_list_free(scc
);
1865 return data
->list
? 0 : -1;
1868 /* Sort the domains in "domain_list" according to the execution order
1869 * at the current depth (for equal values of the outer dimensions),
1870 * generate code for each of them, collecting the results in a list.
1871 * If no code is generated (because the intersection of the inverse schedule
1872 * with the domains turns out to be empty), then an empty list is returned.
1874 * The caller is responsible for ensuring that the basic sets in "domain_list"
1875 * are pair-wise disjoint. It can, however, in principle happen that
1876 * two basic sets should be ordered one way for one value of the outer
1877 * dimensions and the other way for some other value of the outer dimensions.
1878 * We therefore play safe and look for strongly connected components.
1879 * The function add_nodes takes care of handling non-trivial components.
1881 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1882 __isl_keep isl_basic_set_list
*domain_list
,
1883 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
1886 struct isl_add_nodes_data data
;
1893 ctx
= isl_basic_set_list_get_ctx(domain_list
);
1894 n
= isl_basic_set_list_n_basic_set(domain_list
);
1895 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
1899 return add_node(data
.list
, isl_union_map_copy(executed
),
1900 isl_basic_set_list_get_basic_set(domain_list
, 0),
1901 isl_ast_build_copy(build
));
1903 depth
= isl_ast_build_get_depth(build
);
1904 data
.executed
= executed
;
1906 if (isl_basic_set_list_foreach_scc(domain_list
,
1907 &domain_follows_at_depth
, &depth
,
1908 &add_nodes
, &data
) < 0)
1909 data
.list
= isl_ast_graft_list_free(data
.list
);
1914 /* Do i and j share any values for the outer dimensions?
1916 static int shared_outer(__isl_keep isl_basic_set
*i
,
1917 __isl_keep isl_basic_set
*j
, void *user
)
1919 int depth
= *(int *) user
;
1920 isl_basic_map
*test
;
1924 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1925 isl_basic_set_copy(j
));
1926 for (l
= 0; l
< depth
; ++l
)
1927 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1929 empty
= isl_basic_map_is_empty(test
);
1930 isl_basic_map_free(test
);
1932 return empty
< 0 ? -1 : !empty
;
1935 /* Internal data structure for generate_sorted_domains_wrap.
1937 * "n" is the total number of basic sets
1938 * "executed" and "build" are extra arguments to be passed
1939 * to generate_sorted_domains.
1941 * "single" is set to 1 by generate_sorted_domains_wrap if there
1942 * is only a single component.
1943 * "list" collects the results.
1945 struct isl_ast_generate_parallel_domains_data
{
1947 isl_union_map
*executed
;
1948 isl_ast_build
*build
;
1951 isl_ast_graft_list
*list
;
1954 /* Call generate_sorted_domains on "scc", fuse the result into a list
1955 * with either zero or one graft and collect the these single element
1956 * lists into data->list.
1958 * If there is only one component, i.e., if the number of basic sets
1959 * in the current component is equal to the total number of basic sets,
1960 * then data->single is set to 1 and the result of generate_sorted_domains
1963 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
1966 struct isl_ast_generate_parallel_domains_data
*data
= user
;
1967 isl_ast_graft_list
*list
;
1969 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
1970 data
->single
= isl_basic_set_list_n_basic_set(scc
) == data
->n
;
1972 list
= isl_ast_graft_list_fuse(list
, data
->build
);
1976 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
1978 isl_basic_set_list_free(scc
);
1985 /* Look for any (weakly connected) components in the "domain_list"
1986 * of domains that share some values of the outer dimensions.
1987 * That is, domains in different components do not share any values
1988 * of the outer dimensions. This means that these components
1989 * can be freely reordered.
1990 * Within each of the components, we sort the domains according
1991 * to the execution order at the current depth.
1993 * If there is more than one component, then generate_sorted_domains_wrap
1994 * fuses the result of each call to generate_sorted_domains
1995 * into a list with either zero or one graft and collects these (at most)
1996 * single element lists into a bigger list. This means that the elements of the
1997 * final list can be freely reordered. In particular, we sort them
1998 * according to an arbitrary but fixed ordering to ease merging of
1999 * graft lists from different components.
2001 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2002 __isl_keep isl_basic_set_list
*domain_list
,
2003 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2006 struct isl_ast_generate_parallel_domains_data data
;
2011 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2013 return generate_sorted_domains(domain_list
, executed
, build
);
2015 depth
= isl_ast_build_get_depth(build
);
2017 data
.executed
= executed
;
2020 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2021 &generate_sorted_domains_wrap
,
2023 data
.list
= isl_ast_graft_list_free(data
.list
);
2026 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2031 /* Internal data for separate_domain.
2033 * "explicit" is set if we only want to use explicit bounds.
2035 * "domain" collects the separated domains.
2037 struct isl_separate_domain_data
{
2038 isl_ast_build
*build
;
2043 /* Extract implicit bounds on the current dimension for the executed "map".
2045 * The domain of "map" may involve inner dimensions, so we
2046 * need to eliminate them.
2048 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2049 __isl_keep isl_ast_build
*build
)
2053 domain
= isl_map_domain(map
);
2054 domain
= isl_ast_build_eliminate(build
, domain
);
2059 /* Extract explicit bounds on the current dimension for the executed "map".
2061 * Rather than eliminating the inner dimensions as in implicit_bounds,
2062 * we simply drop any constraints involving those inner dimensions.
2063 * The idea is that most bounds that are implied by constraints on the
2064 * inner dimensions will be enforced by for loops and not by explicit guards.
2065 * There is then no need to separate along those bounds.
2067 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2068 __isl_keep isl_ast_build
*build
)
2073 dim
= isl_map_dim(map
, isl_dim_out
);
2074 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2076 domain
= isl_map_domain(map
);
2077 depth
= isl_ast_build_get_depth(build
);
2078 dim
= isl_set_dim(domain
, isl_dim_set
);
2079 domain
= isl_set_detect_equalities(domain
);
2080 domain
= isl_set_drop_constraints_involving_dims(domain
,
2081 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2082 domain
= isl_set_remove_divs_involving_dims(domain
,
2083 isl_dim_set
, depth
, 1);
2084 domain
= isl_set_remove_unknown_divs(domain
);
2089 /* Split data->domain into pieces that intersect with the range of "map"
2090 * and pieces that do not intersect with the range of "map"
2091 * and then add that part of the range of "map" that does not intersect
2092 * with data->domain.
2094 static int separate_domain(__isl_take isl_map
*map
, void *user
)
2096 struct isl_separate_domain_data
*data
= user
;
2101 domain
= explicit_bounds(map
, data
->build
);
2103 domain
= implicit_bounds(map
, data
->build
);
2105 domain
= isl_set_coalesce(domain
);
2106 domain
= isl_set_make_disjoint(domain
);
2107 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2108 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2109 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2110 data
->domain
= isl_set_union(data
->domain
, d1
);
2111 data
->domain
= isl_set_union(data
->domain
, d2
);
2116 /* Separate the schedule domains of "executed".
2118 * That is, break up the domain of "executed" into basic sets,
2119 * such that for each basic set S, every element in S is associated with
2120 * the same domain spaces.
2122 * "space" is the (single) domain space of "executed".
2124 static __isl_give isl_set
*separate_schedule_domains(
2125 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2126 __isl_keep isl_ast_build
*build
)
2128 struct isl_separate_domain_data data
= { build
};
2131 ctx
= isl_ast_build_get_ctx(build
);
2132 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2133 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2134 data
.domain
= isl_set_empty(space
);
2135 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2136 data
.domain
= isl_set_free(data
.domain
);
2138 isl_union_map_free(executed
);
2142 /* Temporary data used during the search for a lower bound for unrolling.
2144 * "domain" is the original set for which to find a lower bound
2145 * "depth" is the dimension for which to find a lower boudn
2147 * "lower" is the best lower bound found so far. It is NULL if we have not
2149 * "n" is the corresponding size. If lower is NULL, then the value of n
2152 struct isl_find_unroll_data
{
2160 /* Check if we can use "c" as a lower bound and if it is better than
2161 * any previously found lower bound.
2163 * If "c" does not involve the dimension at the current depth,
2164 * then we cannot use it.
2165 * Otherwise, let "c" be of the form
2169 * We compute the maximal value of
2171 * -ceil(f(j)/a)) + i + 1
2173 * over the domain. If there is such a value "n", then we know
2175 * -ceil(f(j)/a)) + i + 1 <= n
2179 * i < ceil(f(j)/a)) + n
2181 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2182 * We just need to check if we have found any lower bound before and
2183 * if the new lower bound is better (smaller n) than the previously found
2186 static int update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2187 __isl_keep isl_constraint
*c
)
2189 isl_aff
*aff
, *lower
;
2192 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2195 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2196 lower
= isl_aff_ceil(lower
);
2197 aff
= isl_aff_copy(lower
);
2198 aff
= isl_aff_neg(aff
);
2199 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2200 aff
= isl_aff_add_constant_si(aff
, 1);
2201 max
= isl_set_max_val(data
->domain
, aff
);
2206 if (isl_val_is_infty(max
)) {
2208 isl_aff_free(lower
);
2212 if (isl_val_cmp_si(max
, INT_MAX
) <= 0 &&
2213 (!data
->lower
|| isl_val_cmp_si(max
, *data
->n
) < 0)) {
2214 isl_aff_free(data
->lower
);
2215 data
->lower
= lower
;
2216 *data
->n
= isl_val_get_num_si(max
);
2218 isl_aff_free(lower
);
2223 isl_aff_free(lower
);
2227 /* Check if we can use "c" as a lower bound and if it is better than
2228 * any previously found lower bound.
2230 static int constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2232 struct isl_find_unroll_data
*data
;
2235 data
= (struct isl_find_unroll_data
*) user
;
2236 r
= update_unrolling_lower_bound(data
, c
);
2237 isl_constraint_free(c
);
2242 /* Look for a lower bound l(i) on the dimension at "depth"
2243 * and a size n such that "domain" is a subset of
2245 * { [i] : l(i) <= i_d < l(i) + n }
2247 * where d is "depth" and l(i) depends only on earlier dimensions.
2248 * Furthermore, try and find a lower bound such that n is as small as possible.
2249 * In particular, "n" needs to be finite.
2251 * Inner dimensions have been eliminated from "domain" by the caller.
2253 * We first construct a collection of lower bounds on the input set
2254 * by computing its simple hull. We then iterate through them,
2255 * discarding those that we cannot use (either because they do not
2256 * involve the dimension at "depth" or because they have no corresponding
2257 * upper bound, meaning that "n" would be unbounded) and pick out the
2258 * best from the remaining ones.
2260 * If we cannot find a suitable lower bound, then we consider that
2263 static __isl_give isl_aff
*find_unroll_lower_bound(__isl_keep isl_set
*domain
,
2266 struct isl_find_unroll_data data
= { domain
, depth
, NULL
, n
};
2267 isl_basic_set
*hull
;
2269 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2271 if (isl_basic_set_foreach_constraint(hull
,
2272 &constraint_find_unroll
, &data
) < 0)
2275 isl_basic_set_free(hull
);
2278 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2279 "cannot find lower bound for unrolling", return NULL
);
2283 isl_basic_set_free(hull
);
2284 return isl_aff_free(data
.lower
);
2287 /* Return the constraint
2289 * i_"depth" = aff + offset
2291 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2294 aff
= isl_aff_copy(aff
);
2295 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2296 aff
= isl_aff_add_constant_si(aff
, offset
);
2297 return isl_equality_from_aff(aff
);
2300 /* Data structure for storing the results and the intermediate objects
2301 * of compute_domains.
2303 * "list" is the main result of the function and contains a list
2304 * of disjoint basic sets for which code should be generated.
2306 * "executed" and "build" are inputs to compute_domains.
2307 * "schedule_domain" is the domain of "executed".
2309 * "option" constains the domains at the current depth that should by
2310 * atomic, separated or unrolled. These domains are as specified by
2311 * the user, except that inner dimensions have been eliminated and
2312 * that they have been made pair-wise disjoint.
2314 * "sep_class" contains the user-specified split into separation classes
2315 * specialized to the current depth.
2316 * "done" contains the union of the separation domains that have already
2319 struct isl_codegen_domains
{
2320 isl_basic_set_list
*list
;
2322 isl_union_map
*executed
;
2323 isl_ast_build
*build
;
2324 isl_set
*schedule_domain
;
2332 /* Extend domains->list with a list of basic sets, one for each value
2333 * of the current dimension in "domain" and remove the corresponding
2334 * sets from the class domain. Return the updated class domain.
2335 * The divs that involve the current dimension have not been projected out
2338 * Since we are going to be iterating over the individual values,
2339 * we first check if there are any strides on the current dimension.
2340 * If there is, we rewrite the current dimension i as
2342 * i = stride i' + offset
2344 * and then iterate over individual values of i' instead.
2346 * We then look for a lower bound on i' and a size such that the domain
2349 * { [j,i'] : l(j) <= i' < l(j) + n }
2351 * and then take slices of the domain at values of i'
2352 * between l(j) and l(j) + n - 1.
2354 * We compute the unshifted simple hull of each slice to ensure that
2355 * we have a single basic set per offset. The slicing constraint
2356 * may get simplified away before the unshifted simple hull is taken
2357 * and may therefore in some rare cases disappear from the result.
2358 * We therefore explicitly add the constraint back after computing
2359 * the unshifted simple hull to ensure that the basic sets
2360 * remain disjoint. The constraints that are dropped by taking the hull
2361 * will be taken into account at the next level, as in the case of the
2364 * Finally, we map i' back to i and add each basic set to the list.
2365 * Since we may have dropped some constraints, we intersect with
2366 * the class domain again to ensure that each element in the list
2367 * is disjoint from the other class domains.
2369 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2370 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2376 isl_multi_aff
*expansion
;
2377 isl_basic_map
*bmap
;
2378 isl_set
*unroll_domain
;
2379 isl_ast_build
*build
;
2382 return isl_set_free(class_domain
);
2384 ctx
= isl_set_get_ctx(domain
);
2385 depth
= isl_ast_build_get_depth(domains
->build
);
2386 build
= isl_ast_build_copy(domains
->build
);
2387 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2388 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
2389 expansion
= isl_ast_build_get_stride_expansion(build
);
2391 domain
= isl_set_preimage_multi_aff(domain
,
2392 isl_multi_aff_copy(expansion
));
2393 domain
= isl_ast_build_eliminate_divs(build
, domain
);
2395 isl_ast_build_free(build
);
2397 lower
= find_unroll_lower_bound(domain
, depth
, &n
);
2399 class_domain
= isl_set_free(class_domain
);
2401 bmap
= isl_basic_map_from_multi_aff(expansion
);
2403 unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2405 for (i
= 0; class_domain
&& i
< n
; ++i
) {
2407 isl_basic_set
*bset
;
2408 isl_constraint
*slice
;
2409 isl_basic_set_list
*list
;
2411 slice
= at_offset(depth
, lower
, i
);
2412 set
= isl_set_copy(domain
);
2413 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2414 bset
= isl_set_unshifted_simple_hull(set
);
2415 bset
= isl_basic_set_add_constraint(bset
, slice
);
2416 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2417 set
= isl_set_from_basic_set(bset
);
2418 unroll_domain
= isl_set_union(unroll_domain
, isl_set_copy(set
));
2419 set
= isl_set_intersect(set
, isl_set_copy(class_domain
));
2420 set
= isl_set_make_disjoint(set
);
2421 list
= isl_basic_set_list_from_set(set
);
2422 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2425 class_domain
= isl_set_subtract(class_domain
, unroll_domain
);
2427 isl_aff_free(lower
);
2428 isl_set_free(domain
);
2429 isl_basic_map_free(bmap
);
2431 return class_domain
;
2434 /* Add domains to domains->list for each individual value of the current
2435 * dimension, for that part of the schedule domain that lies in the
2436 * intersection of the option domain and the class domain.
2437 * Remove the corresponding sets from the class domain and
2438 * return the updated class domain.
2440 * We first break up the unroll option domain into individual pieces
2441 * and then handle each of them separately. The unroll option domain
2442 * has been made disjoint in compute_domains_init_options,
2444 * Note that we actively want to combine different pieces of the
2445 * schedule domain that have the same value at the current dimension.
2446 * We therefore need to break up the unroll option domain before
2447 * intersecting with class and schedule domain, hoping that the
2448 * unroll option domain specified by the user is relatively simple.
2450 static __isl_give isl_set
*compute_unroll_domains(
2451 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2453 isl_set
*unroll_domain
;
2454 isl_basic_set_list
*unroll_list
;
2458 empty
= isl_set_is_empty(domains
->option
[unroll
]);
2460 return isl_set_free(class_domain
);
2462 return class_domain
;
2464 unroll_domain
= isl_set_copy(domains
->option
[unroll
]);
2465 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2467 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2468 for (i
= 0; i
< n
; ++i
) {
2469 isl_basic_set
*bset
;
2471 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2472 unroll_domain
= isl_set_from_basic_set(bset
);
2473 unroll_domain
= isl_set_intersect(unroll_domain
,
2474 isl_set_copy(class_domain
));
2475 unroll_domain
= isl_set_intersect(unroll_domain
,
2476 isl_set_copy(domains
->schedule_domain
));
2478 empty
= isl_set_is_empty(unroll_domain
);
2479 if (empty
>= 0 && empty
) {
2480 isl_set_free(unroll_domain
);
2484 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2487 isl_basic_set_list_free(unroll_list
);
2489 return class_domain
;
2492 /* Try and construct a single basic set that includes the intersection of
2493 * the schedule domain, the atomic option domain and the class domain.
2494 * Add the resulting basic set(s) to domains->list and remove them
2495 * from class_domain. Return the updated class domain.
2497 * We construct a single domain rather than trying to combine
2498 * the schedule domains of individual domains because we are working
2499 * within a single component so that non-overlapping schedule domains
2500 * should already have been separated.
2501 * We do however need to make sure that this single domains is a subset
2502 * of the class domain so that it would not intersect with any other
2503 * class domains. This means that we may end up splitting up the atomic
2504 * domain in case separation classes are being used.
2506 * "domain" is the intersection of the schedule domain and the class domain,
2507 * with inner dimensions projected out.
2509 static __isl_give isl_set
*compute_atomic_domain(
2510 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2512 isl_basic_set
*bset
;
2513 isl_basic_set_list
*list
;
2514 isl_set
*domain
, *atomic_domain
;
2517 domain
= isl_set_copy(domains
->option
[atomic
]);
2518 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2519 domain
= isl_set_intersect(domain
,
2520 isl_set_copy(domains
->schedule_domain
));
2521 empty
= isl_set_is_empty(domain
);
2523 class_domain
= isl_set_free(class_domain
);
2525 isl_set_free(domain
);
2526 return class_domain
;
2529 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2530 domain
= isl_set_coalesce(domain
);
2531 bset
= isl_set_unshifted_simple_hull(domain
);
2532 domain
= isl_set_from_basic_set(bset
);
2533 atomic_domain
= isl_set_copy(domain
);
2534 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2535 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2536 domain
= isl_set_make_disjoint(domain
);
2537 list
= isl_basic_set_list_from_set(domain
);
2538 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2540 return class_domain
;
2543 /* Split up the schedule domain into uniform basic sets,
2544 * in the sense that each element in a basic set is associated to
2545 * elements of the same domains, and add the result to domains->list.
2546 * Do this for that part of the schedule domain that lies in the
2547 * intersection of "class_domain" and the separate option domain.
2549 * "class_domain" may or may not include the constraints
2550 * of the schedule domain, but this does not make a difference
2551 * since we are going to intersect it with the domain of the inverse schedule.
2552 * If it includes schedule domain constraints, then they may involve
2553 * inner dimensions, but we will eliminate them in separation_domain.
2555 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2556 __isl_keep isl_set
*class_domain
)
2560 isl_union_map
*executed
;
2561 isl_basic_set_list
*list
;
2564 domain
= isl_set_copy(domains
->option
[separate
]);
2565 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2566 executed
= isl_union_map_copy(domains
->executed
);
2567 executed
= isl_union_map_intersect_domain(executed
,
2568 isl_union_set_from_set(domain
));
2569 empty
= isl_union_map_is_empty(executed
);
2570 if (empty
< 0 || empty
) {
2571 isl_union_map_free(executed
);
2572 return empty
< 0 ? -1 : 0;
2575 space
= isl_set_get_space(class_domain
);
2576 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2578 list
= isl_basic_set_list_from_set(domain
);
2579 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2584 /* Split up the domain at the current depth into disjoint
2585 * basic sets for which code should be generated separately
2586 * for the given separation class domain.
2588 * If any separation classes have been defined, then "class_domain"
2589 * is the domain of the current class and does not refer to inner dimensions.
2590 * Otherwise, "class_domain" is the universe domain.
2592 * We first make sure that the class domain is disjoint from
2593 * previously considered class domains.
2595 * The separate domains can be computed directly from the "class_domain".
2597 * The unroll, atomic and remainder domains need the constraints
2598 * from the schedule domain.
2600 * For unrolling, the actual schedule domain is needed (with divs that
2601 * may refer to the current dimension) so that stride detection can be
2604 * For atomic and remainder domains, inner dimensions and divs involving
2605 * the current dimensions should be eliminated.
2606 * In case we are working within a separation class, we need to intersect
2607 * the result with the current "class_domain" to ensure that the domains
2608 * are disjoint from those generated from other class domains.
2610 * The domain that has been made atomic may be larger than specified
2611 * by the user since it needs to be representable as a single basic set.
2612 * This possibly larger domain is removed from class_domain by
2613 * compute_atomic_domain. It is computed first so that the extended domain
2614 * would not overlap with any domains computed before.
2615 * Similary, the unrolled domains may have some constraints removed and
2616 * may therefore also be larger than specified by the user.
2618 * If anything is left after handling separate, unroll and atomic,
2619 * we split it up into basic sets and append the basic sets to domains->list.
2621 static int compute_partial_domains(struct isl_codegen_domains
*domains
,
2622 __isl_take isl_set
*class_domain
)
2624 isl_basic_set_list
*list
;
2627 class_domain
= isl_set_subtract(class_domain
,
2628 isl_set_copy(domains
->done
));
2629 domains
->done
= isl_set_union(domains
->done
,
2630 isl_set_copy(class_domain
));
2632 class_domain
= compute_atomic_domain(domains
, class_domain
);
2633 class_domain
= compute_unroll_domains(domains
, class_domain
);
2635 domain
= isl_set_copy(class_domain
);
2637 if (compute_separate_domain(domains
, domain
) < 0)
2639 domain
= isl_set_subtract(domain
,
2640 isl_set_copy(domains
->option
[separate
]));
2642 domain
= isl_set_intersect(domain
,
2643 isl_set_copy(domains
->schedule_domain
));
2645 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2646 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2648 domain
= isl_set_coalesce(domain
);
2649 domain
= isl_set_make_disjoint(domain
);
2651 list
= isl_basic_set_list_from_set(domain
);
2652 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2654 isl_set_free(class_domain
);
2658 isl_set_free(domain
);
2659 isl_set_free(class_domain
);
2663 /* Split up the domain at the current depth into disjoint
2664 * basic sets for which code should be generated separately
2665 * for the separation class identified by "pnt".
2667 * We extract the corresponding class domain from domains->sep_class,
2668 * eliminate inner dimensions and pass control to compute_partial_domains.
2670 static int compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
2672 struct isl_codegen_domains
*domains
= user
;
2677 class_set
= isl_set_from_point(pnt
);
2678 domain
= isl_map_domain(isl_map_intersect_range(
2679 isl_map_copy(domains
->sep_class
), class_set
));
2680 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
2681 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2683 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
2687 isl_set_free(domain
);
2691 return compute_partial_domains(domains
, domain
);
2694 /* Extract the domains at the current depth that should be atomic,
2695 * separated or unrolled and store them in option.
2697 * The domains specified by the user might overlap, so we make
2698 * them disjoint by subtracting earlier domains from later domains.
2700 static void compute_domains_init_options(isl_set
*option
[3],
2701 __isl_keep isl_ast_build
*build
)
2703 enum isl_ast_build_domain_type type
, type2
;
2705 for (type
= atomic
; type
<= separate
; ++type
) {
2706 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
2707 for (type2
= atomic
; type2
< type
; ++type2
)
2708 option
[type
] = isl_set_subtract(option
[type
],
2709 isl_set_copy(option
[type2
]));
2712 option
[unroll
] = isl_set_coalesce(option
[unroll
]);
2713 option
[unroll
] = isl_set_make_disjoint(option
[unroll
]);
2716 /* Split up the domain at the current depth into disjoint
2717 * basic sets for which code should be generated separately,
2718 * based on the user-specified options.
2719 * Return the list of disjoint basic sets.
2721 * There are three kinds of domains that we need to keep track of.
2722 * - the "schedule domain" is the domain of "executed"
2723 * - the "class domain" is the domain corresponding to the currrent
2725 * - the "option domain" is the domain corresponding to one of the options
2726 * atomic, unroll or separate
2728 * We first consider the individial values of the separation classes
2729 * and split up the domain for each of them separately.
2730 * Finally, we consider the remainder. If no separation classes were
2731 * specified, then we call compute_partial_domains with the universe
2732 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2733 * with inner dimensions removed. We do this because we want to
2734 * avoid computing the complement of the class domains (i.e., the difference
2735 * between the universe and domains->done).
2737 static __isl_give isl_basic_set_list
*compute_domains(
2738 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2740 struct isl_codegen_domains domains
;
2743 isl_union_set
*schedule_domain
;
2747 enum isl_ast_build_domain_type type
;
2753 ctx
= isl_union_map_get_ctx(executed
);
2754 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
2756 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
2757 domain
= isl_set_from_union_set(schedule_domain
);
2759 compute_domains_init_options(domains
.option
, build
);
2761 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
2762 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
2763 n_param
= isl_set_dim(classes
, isl_dim_param
);
2764 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
2766 space
= isl_set_get_space(domain
);
2767 domains
.build
= build
;
2768 domains
.schedule_domain
= isl_set_copy(domain
);
2769 domains
.executed
= executed
;
2770 domains
.done
= isl_set_empty(space
);
2772 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
2773 domains
.list
= isl_basic_set_list_free(domains
.list
);
2774 isl_set_free(classes
);
2776 empty
= isl_set_is_empty(domains
.done
);
2778 domains
.list
= isl_basic_set_list_free(domains
.list
);
2779 domain
= isl_set_free(domain
);
2781 isl_set_free(domain
);
2782 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
2784 domain
= isl_ast_build_eliminate(build
, domain
);
2786 if (compute_partial_domains(&domains
, domain
) < 0)
2787 domains
.list
= isl_basic_set_list_free(domains
.list
);
2789 isl_set_free(domains
.schedule_domain
);
2790 isl_set_free(domains
.done
);
2791 isl_map_free(domains
.sep_class
);
2792 for (type
= atomic
; type
<= separate
; ++type
)
2793 isl_set_free(domains
.option
[type
]);
2795 return domains
.list
;
2798 /* Generate code for a single component, after shifting (if any)
2801 * We first split up the domain at the current depth into disjoint
2802 * basic sets based on the user-specified options.
2803 * Then we generated code for each of them and concatenate the results.
2805 static __isl_give isl_ast_graft_list
*generate_shifted_component(
2806 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
2808 isl_basic_set_list
*domain_list
;
2809 isl_ast_graft_list
*list
= NULL
;
2811 domain_list
= compute_domains(executed
, build
);
2812 list
= generate_parallel_domains(domain_list
, executed
, build
);
2814 isl_basic_set_list_free(domain_list
);
2815 isl_union_map_free(executed
);
2816 isl_ast_build_free(build
);
2821 struct isl_set_map_pair
{
2826 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2827 * of indices into the "domain" array,
2828 * return the union of the "map" fields of the elements
2829 * indexed by the first "n" elements of "order".
2831 static __isl_give isl_union_map
*construct_component_executed(
2832 struct isl_set_map_pair
*domain
, int *order
, int n
)
2836 isl_union_map
*executed
;
2838 map
= isl_map_copy(domain
[order
[0]].map
);
2839 executed
= isl_union_map_from_map(map
);
2840 for (i
= 1; i
< n
; ++i
) {
2841 map
= isl_map_copy(domain
[order
[i
]].map
);
2842 executed
= isl_union_map_add_map(executed
, map
);
2848 /* Generate code for a single component, after shifting (if any)
2851 * The component inverse schedule is specified as the "map" fields
2852 * of the elements of "domain" indexed by the first "n" elements of "order".
2854 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
2855 struct isl_set_map_pair
*domain
, int *order
, int n
,
2856 __isl_take isl_ast_build
*build
)
2858 isl_union_map
*executed
;
2860 executed
= construct_component_executed(domain
, order
, n
);
2861 return generate_shifted_component(executed
, build
);
2864 /* Does set dimension "pos" of "set" have an obviously fixed value?
2866 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
2871 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
2874 fixed
= !isl_val_is_nan(v
);
2880 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2881 * of indices into the "domain" array,
2882 * do all (except for at most one) of the "set" field of the elements
2883 * indexed by the first "n" elements of "order" have a fixed value
2884 * at position "depth"?
2886 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
2887 int *order
, int n
, int depth
)
2892 for (i
= 0; i
< n
; ++i
) {
2895 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
2908 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2909 * of indices into the "domain" array,
2910 * eliminate the inner dimensions from the "set" field of the elements
2911 * indexed by the first "n" elements of "order", provided the current
2912 * dimension does not have a fixed value.
2914 * Return the index of the first element in "order" with a corresponding
2915 * "set" field that does not have an (obviously) fixed value.
2917 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
2918 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
2923 for (i
= n
- 1; i
>= 0; --i
) {
2925 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
2930 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
2931 domain
[order
[i
]].set
);
2938 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2939 * of indices into the "domain" array,
2940 * find the element of "domain" (amongst those indexed by the first "n"
2941 * elements of "order") with the "set" field that has the smallest
2942 * value for the current iterator.
2944 * Note that the domain with the smallest value may depend on the parameters
2945 * and/or outer loop dimension. Since the result of this function is only
2946 * used as heuristic, we only make a reasonable attempt at finding the best
2947 * domain, one that should work in case a single domain provides the smallest
2948 * value for the current dimension over all values of the parameters
2949 * and outer dimensions.
2951 * In particular, we compute the smallest value of the first domain
2952 * and replace it by that of any later domain if that later domain
2953 * has a smallest value that is smaller for at least some value
2954 * of the parameters and outer dimensions.
2956 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
2957 __isl_keep isl_ast_build
*build
)
2963 min_first
= isl_ast_build_map_to_iterator(build
,
2964 isl_set_copy(domain
[order
[0]].set
));
2965 min_first
= isl_map_lexmin(min_first
);
2967 for (i
= 1; i
< n
; ++i
) {
2968 isl_map
*min
, *test
;
2971 min
= isl_ast_build_map_to_iterator(build
,
2972 isl_set_copy(domain
[order
[i
]].set
));
2973 min
= isl_map_lexmin(min
);
2974 test
= isl_map_copy(min
);
2975 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
2976 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
2977 empty
= isl_map_is_empty(test
);
2979 if (empty
>= 0 && !empty
) {
2980 isl_map_free(min_first
);
2990 isl_map_free(min_first
);
2992 return i
< n
? -1 : first
;
2995 /* Construct a shifted inverse schedule based on the original inverse schedule,
2996 * the stride and the offset.
2998 * The original inverse schedule is specified as the "map" fields
2999 * of the elements of "domain" indexed by the first "n" elements of "order".
3001 * "stride" and "offset" are such that the difference
3002 * between the values of the current dimension of domain "i"
3003 * and the values of the current dimension for some reference domain are
3006 * stride * integer + offset[i]
3008 * Moreover, 0 <= offset[i] < stride.
3010 * For each domain, we create a map
3012 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3014 * where j refers to the current dimension and the other dimensions are
3015 * unchanged, and apply this map to the original schedule domain.
3017 * For example, for the original schedule
3019 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3021 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3022 * we apply the mapping
3026 * to the schedule of the "A" domain and the mapping
3028 * { [j - 1] -> [j, 1] }
3030 * to the schedule of the "B" domain.
3033 * Note that after the transformation, the differences between pairs
3034 * of values of the current dimension over all domains are multiples
3035 * of stride and that we have therefore exposed the stride.
3038 * To see that the mapping preserves the lexicographic order,
3039 * first note that each of the individual maps above preserves the order.
3040 * If the value of the current iterator is j1 in one domain and j2 in another,
3041 * then if j1 = j2, we know that the same map is applied to both domains
3042 * and the order is preserved.
3043 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3044 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3048 * and the order is preserved.
3049 * If c1 < c2, then we know
3055 * j2 - j1 = n * s + r
3057 * with n >= 0 and 0 <= r < s.
3058 * In other words, r = c2 - c1.
3069 * (j1 - c1, c1) << (j2 - c2, c2)
3071 * with "<<" the lexicographic order, proving that the order is preserved
3074 static __isl_give isl_union_map
*contruct_shifted_executed(
3075 struct isl_set_map_pair
*domain
, int *order
, int n
,
3076 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3077 __isl_take isl_ast_build
*build
)
3080 isl_union_map
*executed
;
3086 depth
= isl_ast_build_get_depth(build
);
3087 space
= isl_ast_build_get_space(build
, 1);
3088 executed
= isl_union_map_empty(isl_space_copy(space
));
3089 space
= isl_space_map_from_set(space
);
3090 map
= isl_map_identity(isl_space_copy(space
));
3091 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3092 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3093 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3095 c
= isl_equality_alloc(isl_local_space_from_space(space
));
3096 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3097 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3099 for (i
= 0; i
< n
; ++i
) {
3103 v
= isl_multi_val_get_val(offset
, i
);
3106 map_i
= isl_map_copy(map
);
3107 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
3110 c
= isl_constraint_set_constant_val(c
, v
);
3111 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
3113 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
3115 executed
= isl_union_map_add_map(executed
, map_i
);
3118 isl_constraint_free(c
);
3122 executed
= isl_union_map_free(executed
);
3127 /* Generate code for a single component, after exposing the stride,
3128 * given that the schedule domain is "shifted strided".
3130 * The component inverse schedule is specified as the "map" fields
3131 * of the elements of "domain" indexed by the first "n" elements of "order".
3133 * The schedule domain being "shifted strided" means that the differences
3134 * between the values of the current dimension of domain "i"
3135 * and the values of the current dimension for some reference domain are
3138 * stride * integer + offset[i]
3140 * We first look for the domain with the "smallest" value for the current
3141 * dimension and adjust the offsets such that the offset of the "smallest"
3142 * domain is equal to zero. The other offsets are reduced modulo stride.
3144 * Based on this information, we construct a new inverse schedule in
3145 * contruct_shifted_executed that exposes the stride.
3146 * Since this involves the introduction of a new schedule dimension,
3147 * the build needs to be changed accodingly.
3148 * After computing the AST, the newly introduced dimension needs
3149 * to be removed again from the list of grafts. We do this by plugging
3150 * in a mapping that represents the new schedule domain in terms of the
3151 * old schedule domain.
3153 static __isl_give isl_ast_graft_list
*generate_shift_component(
3154 struct isl_set_map_pair
*domain
, int *order
, int n
,
3155 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3156 __isl_take isl_ast_build
*build
)
3158 isl_ast_graft_list
*list
;
3165 isl_multi_aff
*ma
, *zero
;
3166 isl_union_map
*executed
;
3168 ctx
= isl_ast_build_get_ctx(build
);
3169 depth
= isl_ast_build_get_depth(build
);
3171 first
= first_offset(domain
, order
, n
, build
);
3175 mv
= isl_multi_val_copy(offset
);
3176 val
= isl_multi_val_get_val(offset
, first
);
3177 val
= isl_val_neg(val
);
3178 mv
= isl_multi_val_add_val(mv
, val
);
3179 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
3181 executed
= contruct_shifted_executed(domain
, order
, n
, stride
, mv
,
3183 space
= isl_ast_build_get_space(build
, 1);
3184 space
= isl_space_map_from_set(space
);
3185 ma
= isl_multi_aff_identity(isl_space_copy(space
));
3186 space
= isl_space_from_domain(isl_space_domain(space
));
3187 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3188 zero
= isl_multi_aff_zero(space
);
3189 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
3190 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
3191 list
= generate_shifted_component(executed
, build
);
3193 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
3195 isl_multi_val_free(mv
);
3199 isl_ast_build_free(build
);
3203 /* Generate code for a single component.
3205 * The component inverse schedule is specified as the "map" fields
3206 * of the elements of "domain" indexed by the first "n" elements of "order".
3208 * This function may modify the "set" fields of "domain".
3210 * Before proceeding with the actual code generation for the component,
3211 * we first check if there are any "shifted" strides, meaning that
3212 * the schedule domains of the individual domains are all strided,
3213 * but that they have different offsets, resulting in the union
3214 * of schedule domains not being strided anymore.
3216 * The simplest example is the schedule
3218 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3220 * Both schedule domains are strided, but their union is not.
3221 * This function detects such cases and then rewrites the schedule to
3223 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3225 * In the new schedule, the schedule domains have the same offset (modulo
3226 * the stride), ensuring that the union of schedule domains is also strided.
3229 * If there is only a single domain in the component, then there is
3230 * nothing to do. Similarly, if the current schedule dimension has
3231 * a fixed value for almost all domains then there is nothing to be done.
3232 * In particular, we need at least two domains where the current schedule
3233 * dimension does not have a fixed value.
3234 * Finally, if any of the options refer to the current schedule dimension,
3235 * then we bail out as well. It would be possible to reformulate the options
3236 * in terms of the new schedule domain, but that would introduce constraints
3237 * that separate the domains in the options and that is something we would
3241 * To see if there is any shifted stride, we look at the differences
3242 * between the values of the current dimension in pairs of domains
3243 * for equal values of outer dimensions. These differences should be
3248 * with "m" the stride and "r" a constant. Note that we cannot perform
3249 * this analysis on individual domains as the lower bound in each domain
3250 * may depend on parameters or outer dimensions and so the current dimension
3251 * itself may not have a fixed remainder on division by the stride.
3253 * In particular, we compare the first domain that does not have an
3254 * obviously fixed value for the current dimension to itself and all
3255 * other domains and collect the offsets and the gcd of the strides.
3256 * If the gcd becomes one, then we failed to find shifted strides.
3257 * If the gcd is zero, then the differences were all fixed, meaning
3258 * that some domains had non-obviously fixed values for the current dimension.
3259 * If all the offsets are the same (for those domains that do not have
3260 * an obviously fixed value for the current dimension), then we do not
3261 * apply the transformation.
3262 * If none of the domains were skipped, then there is nothing to do.
3263 * If some of them were skipped, then if we apply separation, the schedule
3264 * domain should get split in pieces with a (non-shifted) stride.
3266 * Otherwise, we apply a shift to expose the stride in
3267 * generate_shift_component.
3269 static __isl_give isl_ast_graft_list
*generate_component(
3270 struct isl_set_map_pair
*domain
, int *order
, int n
,
3271 __isl_take isl_ast_build
*build
)
3278 isl_val
*gcd
= NULL
;
3282 isl_ast_graft_list
*list
;
3285 depth
= isl_ast_build_get_depth(build
);
3288 if (skip
>= 0 && !skip
)
3289 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
3290 if (skip
>= 0 && !skip
)
3291 skip
= isl_ast_build_options_involve_depth(build
);
3295 return generate_shifted_component_from_list(domain
,
3298 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
3302 ctx
= isl_ast_build_get_ctx(build
);
3304 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
3307 for (i
= 0; i
< n
; ++i
) {
3310 map
= isl_map_from_domain_and_range(
3311 isl_set_copy(domain
[order
[base
]].set
),
3312 isl_set_copy(domain
[order
[i
]].set
));
3313 for (d
= 0; d
< depth
; ++d
)
3314 map
= isl_map_equate(map
, isl_dim_in
, d
,
3316 deltas
= isl_map_deltas(map
);
3317 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
3318 isl_set_free(deltas
);
3325 gcd
= isl_val_gcd(gcd
, m
);
3326 if (isl_val_is_one(gcd
)) {
3330 mv
= isl_multi_val_set_val(mv
, i
, r
);
3332 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3338 if (fixed
&& i
> base
) {
3340 a
= isl_multi_val_get_val(mv
, i
);
3341 b
= isl_multi_val_get_val(mv
, base
);
3342 if (isl_val_ne(a
, b
))
3349 if (res
< 0 || !gcd
) {
3350 isl_ast_build_free(build
);
3352 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
3353 list
= generate_shifted_component_from_list(domain
,
3356 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
3361 isl_multi_val_free(mv
);
3365 isl_ast_build_free(build
);
3369 /* Store both "map" itself and its domain in the
3370 * structure pointed to by *next and advance to the next array element.
3372 static int extract_domain(__isl_take isl_map
*map
, void *user
)
3374 struct isl_set_map_pair
**next
= user
;
3376 (*next
)->map
= isl_map_copy(map
);
3377 (*next
)->set
= isl_map_domain(map
);
3383 /* Internal data for any_scheduled_after.
3385 * "depth" is the number of loops that have already been generated
3386 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3387 * "domain" is an array of set-map pairs corresponding to the different
3388 * iteration domains. The set is the schedule domain, i.e., the domain
3389 * of the inverse schedule, while the map is the inverse schedule itself.
3391 struct isl_any_scheduled_after_data
{
3393 int group_coscheduled
;
3394 struct isl_set_map_pair
*domain
;
3397 /* Is any element of domain "i" scheduled after any element of domain "j"
3398 * (for a common iteration of the first data->depth loops)?
3400 * data->domain[i].set contains the domain of the inverse schedule
3401 * for domain "i", i.e., elements in the schedule domain.
3403 * If data->group_coscheduled is set, then we also return 1 if there
3404 * is any pair of elements in the two domains that are scheduled together.
3406 static int any_scheduled_after(int i
, int j
, void *user
)
3408 struct isl_any_scheduled_after_data
*data
= user
;
3409 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
3412 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
3415 follows
= isl_set_follows_at(data
->domain
[i
].set
,
3416 data
->domain
[j
].set
, pos
);
3426 return data
->group_coscheduled
;
3429 /* Look for independent components at the current depth and generate code
3430 * for each component separately. The resulting lists of grafts are
3431 * merged in an attempt to combine grafts with identical guards.
3433 * Code for two domains can be generated separately if all the elements
3434 * of one domain are scheduled before (or together with) all the elements
3435 * of the other domain. We therefore consider the graph with as nodes
3436 * the domains and an edge between two nodes if any element of the first
3437 * node is scheduled after any element of the second node.
3438 * If the ast_build_group_coscheduled is set, then we also add an edge if
3439 * there is any pair of elements in the two domains that are scheduled
3441 * Code is then generated (by generate_component)
3442 * for each of the strongly connected components in this graph
3443 * in their topological order.
3445 * Since the test is performed on the domain of the inverse schedules of
3446 * the different domains, we precompute these domains and store
3447 * them in data.domain.
3449 static __isl_give isl_ast_graft_list
*generate_components(
3450 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3453 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
3454 int n
= isl_union_map_n_map(executed
);
3455 struct isl_any_scheduled_after_data data
;
3456 struct isl_set_map_pair
*next
;
3457 struct isl_tarjan_graph
*g
= NULL
;
3458 isl_ast_graft_list
*list
= NULL
;
3461 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
3467 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
3472 data
.depth
= isl_ast_build_get_depth(build
);
3473 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
3474 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_ast_build_dim(build
, 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 domain element(s)
3608 * for those domain 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 * If the build space is parametric, then we add some of the parameter
3622 * constraints to the executed relation. Adding these constraints
3623 * allows for an earlier detection of conflicts in some cases.
3624 * However, we do not want to divide the executed relation into
3625 * more disjuncts than necessary. We therefore approximate
3626 * the constraints on the parameters by a single disjunct set.
3628 * The build is extended to include the additional part of the schedule.
3629 * If the original build space was not parametric, then the options
3630 * in data->build refer only to the additional part of the schedule
3631 * and they need to be adjusted to refer to the complete AST build
3634 * After having adjusted inverse schedule and build, we start generating
3635 * code with the outer loop of the current code generation
3636 * in generate_next_level.
3638 * If the original build space was not parametric, we undo the embedding
3639 * on the resulting isl_ast_node_list so that it can be used within
3640 * the outer AST build.
3642 static int generate_code_in_space(struct isl_generate_code_data
*data
,
3643 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
3645 isl_union_map
*executed
;
3646 isl_ast_build
*build
;
3647 isl_ast_graft_list
*list
;
3650 executed
= isl_union_map_copy(data
->executed
);
3651 executed
= isl_union_map_intersect_domain(executed
,
3652 isl_union_set_from_set(set
));
3654 embed
= !isl_set_is_params(data
->build
->domain
);
3655 if (embed
&& !data
->internal
)
3656 executed
= internal_executed(executed
, space
, data
->build
);
3659 domain
= isl_ast_build_get_domain(data
->build
);
3660 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
3661 executed
= isl_union_map_intersect_params(executed
, domain
);
3664 build
= isl_ast_build_copy(data
->build
);
3665 build
= isl_ast_build_product(build
, space
);
3667 list
= generate_next_level(executed
, build
);
3669 list
= isl_ast_graft_list_unembed(list
, embed
);
3671 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
3676 /* Generate an AST that visits the elements in the range of data->executed
3677 * in the relative order specified by the corresponding domain element(s)
3678 * for those domain elements that belong to "set".
3679 * Add the result to data->list.
3681 * The caller ensures that "set" is a universe domain.
3683 * If the build space S is not parametric, then the space of "set"
3684 * need to be a wrapped relation with S as domain. That is, it needs
3689 * Check this property and pass control to generate_code_in_space
3691 * If the build space is not parametric, then T is the space of "set".
3693 static int generate_code_set(__isl_take isl_set
*set
, void *user
)
3695 struct isl_generate_code_data
*data
= user
;
3696 isl_space
*space
, *build_space
;
3699 space
= isl_set_get_space(set
);
3701 if (isl_set_is_params(data
->build
->domain
))
3702 return generate_code_in_space(data
, set
, space
);
3704 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
3705 space
= isl_space_unwrap(space
);
3706 is_domain
= isl_space_is_domain(build_space
, space
);
3707 isl_space_free(build_space
);
3708 space
= isl_space_range(space
);
3713 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3714 "invalid nested schedule space", goto error
);
3716 return generate_code_in_space(data
, set
, space
);
3719 isl_space_free(space
);
3723 /* Generate an AST that visits the elements in the range of "executed"
3724 * in the relative order specified by the corresponding domain element(s).
3726 * "build" is an isl_ast_build that has either been constructed by
3727 * isl_ast_build_from_context or passed to a callback set by
3728 * isl_ast_build_set_create_leaf.
3729 * In the first case, the space of the isl_ast_build is typically
3730 * a parametric space, although this is currently not enforced.
3731 * In the second case, the space is never a parametric space.
3732 * If the space S is not parametric, then the domain space(s) of "executed"
3733 * need to be wrapped relations with S as domain.
3735 * If the domain of "executed" consists of several spaces, then an AST
3736 * is generated for each of them (in arbitrary order) and the results
3739 * If "internal" is set, then the domain "S" above refers to the internal
3740 * schedule domain representation. Otherwise, it refers to the external
3741 * representation, as returned by isl_ast_build_get_schedule_space.
3743 * We essentially run over all the spaces in the domain of "executed"
3744 * and call generate_code_set on each of them.
3746 static __isl_give isl_ast_graft_list
*generate_code(
3747 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3751 struct isl_generate_code_data data
= { 0 };
3753 isl_union_set
*schedule_domain
;
3754 isl_union_map
*universe
;
3758 space
= isl_ast_build_get_space(build
, 1);
3759 space
= isl_space_align_params(space
,
3760 isl_union_map_get_space(executed
));
3761 space
= isl_space_align_params(space
,
3762 isl_union_map_get_space(build
->options
));
3763 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
3764 executed
= isl_union_map_align_params(executed
, space
);
3765 if (!executed
|| !build
)
3768 ctx
= isl_ast_build_get_ctx(build
);
3770 data
.internal
= internal
;
3771 data
.executed
= executed
;
3773 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
3775 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
3776 schedule_domain
= isl_union_map_domain(universe
);
3777 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
3779 data
.list
= isl_ast_graft_list_free(data
.list
);
3781 isl_union_set_free(schedule_domain
);
3782 isl_union_map_free(executed
);
3784 isl_ast_build_free(build
);
3787 isl_union_map_free(executed
);
3788 isl_ast_build_free(build
);
3792 /* Generate an AST that visits the elements in the domain of "schedule"
3793 * in the relative order specified by the corresponding image element(s).
3795 * "build" is an isl_ast_build that has either been constructed by
3796 * isl_ast_build_from_context or passed to a callback set by
3797 * isl_ast_build_set_create_leaf.
3798 * In the first case, the space of the isl_ast_build is typically
3799 * a parametric space, although this is currently not enforced.
3800 * In the second case, the space is never a parametric space.
3801 * If the space S is not parametric, then the range space(s) of "schedule"
3802 * need to be wrapped relations with S as domain.
3804 * If the range of "schedule" consists of several spaces, then an AST
3805 * is generated for each of them (in arbitrary order) and the results
3808 * We first initialize the local copies of the relevant options.
3809 * We do this here rather than when the isl_ast_build is created
3810 * because the options may have changed between the construction
3811 * of the isl_ast_build and the call to isl_generate_code.
3813 * The main computation is performed on an inverse schedule (with
3814 * the schedule domain in the domain and the elements to be executed
3815 * in the range) called "executed".
3817 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
3818 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
3820 isl_ast_graft_list
*list
;
3822 isl_union_map
*executed
;
3824 build
= isl_ast_build_copy(build
);
3825 build
= isl_ast_build_set_single_valued(build
, 0);
3826 schedule
= isl_union_map_coalesce(schedule
);
3827 executed
= isl_union_map_reverse(schedule
);
3828 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
3829 node
= isl_ast_node_from_graft_list(list
, build
);
3830 isl_ast_build_free(build
);