2 * Copyright 2012-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl/space.h>
18 #include <isl/constraint.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/schedule_node.h>
24 #include <isl/options.h>
26 #include <isl_tarjan.h>
27 #include <isl_ast_private.h>
28 #include <isl_ast_build_expr.h>
29 #include <isl_ast_build_private.h>
30 #include <isl_ast_graft_private.h>
32 /* Try and reduce the number of disjuncts in the representation of "set",
33 * without dropping explicit representations of local variables.
35 static __isl_give isl_set
*isl_set_coalesce_preserve(__isl_take isl_set
*set
)
43 ctx
= isl_set_get_ctx(set
);
44 save_preserve
= isl_options_get_coalesce_preserve_locals(ctx
);
45 isl_options_set_coalesce_preserve_locals(ctx
, 1);
46 set
= isl_set_coalesce(set
);
47 isl_options_set_coalesce_preserve_locals(ctx
, save_preserve
);
51 /* Data used in generate_domain.
53 * "build" is the input build.
54 * "list" collects the results.
56 struct isl_generate_domain_data
{
59 isl_ast_graft_list
*list
;
62 static __isl_give isl_ast_graft_list
*generate_next_level(
63 __isl_take isl_union_map
*executed
,
64 __isl_take isl_ast_build
*build
);
65 static __isl_give isl_ast_graft_list
*generate_code(
66 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
69 /* Generate an AST for a single domain based on
70 * the (non single valued) inverse schedule "executed".
72 * We extend the schedule with the iteration domain
73 * and continue generating through a call to generate_code.
75 * In particular, if executed has the form
79 * then we continue generating code on
83 * The extended inverse schedule is clearly single valued
84 * ensuring that the nested generate_code will not reach this function,
85 * but will instead create calls to all elements of D that need
86 * to be executed from the current schedule domain.
88 static isl_stat
generate_non_single_valued(__isl_take isl_map
*executed
,
89 struct isl_generate_domain_data
*data
)
93 isl_ast_graft_list
*list
;
95 build
= isl_ast_build_copy(data
->build
);
97 identity
= isl_set_identity(isl_map_range(isl_map_copy(executed
)));
98 executed
= isl_map_domain_product(executed
, identity
);
99 build
= isl_ast_build_set_single_valued(build
, 1);
101 list
= generate_code(isl_union_map_from_map(executed
), build
, 1);
103 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
108 /* Call the at_each_domain callback, if requested by the user,
109 * after recording the current inverse schedule in the build.
111 static __isl_give isl_ast_graft
*at_each_domain(__isl_take isl_ast_graft
*graft
,
112 __isl_keep isl_map
*executed
, __isl_keep isl_ast_build
*build
)
114 if (!graft
|| !build
)
115 return isl_ast_graft_free(graft
);
116 if (!build
->at_each_domain
)
119 build
= isl_ast_build_copy(build
);
120 build
= isl_ast_build_set_executed(build
,
121 isl_union_map_from_map(isl_map_copy(executed
)));
123 return isl_ast_graft_free(graft
);
125 graft
->node
= build
->at_each_domain(graft
->node
,
126 build
, build
->at_each_domain_user
);
127 isl_ast_build_free(build
);
130 graft
= isl_ast_graft_free(graft
);
135 /* Generate a call expression for the single executed
136 * domain element "map" and put a guard around it based its (simplified)
137 * domain. "executed" is the original inverse schedule from which "map"
138 * has been derived. In particular, "map" is either identical to "executed"
139 * or it is the result of gisting "executed" with respect to the build domain.
140 * "executed" is only used if there is an at_each_domain callback.
142 * At this stage, any pending constraints in the build can no longer
143 * be simplified with respect to any enforced constraints since
144 * the call node does not have any enforced constraints.
145 * Since all pending constraints not covered by any enforced constraints
146 * will be added as a guard to the graft in create_node_scaled,
147 * even in the eliminated case, the pending constraints
148 * can be considered to have been generated by outer constructs.
150 * If the user has set an at_each_domain callback, it is called
151 * on the constructed call expression node.
153 static isl_stat
add_domain(__isl_take isl_map
*executed
,
154 __isl_take isl_map
*map
, struct isl_generate_domain_data
*data
)
156 isl_ast_build
*build
;
157 isl_ast_graft
*graft
;
158 isl_ast_graft_list
*list
;
159 isl_set
*guard
, *pending
;
161 build
= isl_ast_build_copy(data
->build
);
162 pending
= isl_ast_build_get_pending(build
);
163 build
= isl_ast_build_replace_pending_by_guard(build
, pending
);
165 guard
= isl_map_domain(isl_map_copy(map
));
166 guard
= isl_set_compute_divs(guard
);
167 guard
= isl_set_coalesce_preserve(guard
);
168 guard
= isl_set_gist(guard
, isl_ast_build_get_generated(build
));
169 guard
= isl_ast_build_specialize(build
, guard
);
171 graft
= isl_ast_graft_alloc_domain(map
, build
);
172 graft
= at_each_domain(graft
, executed
, build
);
173 isl_ast_build_free(build
);
174 isl_map_free(executed
);
175 graft
= isl_ast_graft_add_guard(graft
, guard
, data
->build
);
177 list
= isl_ast_graft_list_from_ast_graft(graft
);
178 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
183 /* Generate an AST for a single domain based on
184 * the inverse schedule "executed" and add it to data->list.
186 * If there is more than one domain element associated to the current
187 * schedule "time", then we need to continue the generation process
188 * in generate_non_single_valued.
189 * Note that the inverse schedule being single-valued may depend
190 * on constraints that are only available in the original context
191 * domain specified by the user. We therefore first introduce
192 * some of the constraints of data->build->domain. In particular,
193 * we intersect with a single-disjunct approximation of this set.
194 * We perform this approximation to avoid further splitting up
195 * the executed relation, possibly introducing a disjunctive guard
198 * On the other hand, we only perform the test after having taken the gist
199 * of the domain as the resulting map is the one from which the call
200 * expression is constructed. Using this map to construct the call
201 * expression usually yields simpler results in cases where the original
202 * map is not obviously single-valued.
203 * If the original map is obviously single-valued, then the gist
204 * operation is skipped.
206 * Because we perform the single-valuedness test on the gisted map,
207 * we may in rare cases fail to recognize that the inverse schedule
208 * is single-valued. This becomes problematic if this happens
209 * from the recursive call through generate_non_single_valued
210 * as we would then end up in an infinite recursion.
211 * We therefore check if we are inside a call to generate_non_single_valued
212 * and revert to the ungisted map if the gisted map turns out not to be
215 * Otherwise, call add_domain to generate a call expression (with guard) and
216 * to call the at_each_domain callback, if any.
218 static isl_stat
generate_domain(__isl_take isl_map
*executed
, void *user
)
220 struct isl_generate_domain_data
*data
= user
;
225 domain
= isl_ast_build_get_domain(data
->build
);
226 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
227 executed
= isl_map_intersect_domain(executed
, domain
);
228 empty
= isl_map_is_empty(executed
);
232 isl_map_free(executed
);
236 sv
= isl_map_plain_is_single_valued(executed
);
240 return add_domain(executed
, isl_map_copy(executed
), data
);
242 executed
= isl_map_coalesce(executed
);
243 map
= isl_map_copy(executed
);
244 map
= isl_ast_build_compute_gist_map_domain(data
->build
, map
);
245 sv
= isl_map_is_single_valued(map
);
250 if (data
->build
->single_valued
)
251 map
= isl_map_copy(executed
);
253 return generate_non_single_valued(executed
, data
);
256 return add_domain(executed
, map
, data
);
259 isl_map_free(executed
);
260 return isl_stat_error
;
263 /* Call build->create_leaf to a create "leaf" node in the AST,
264 * encapsulate the result in an isl_ast_graft and return the result
265 * as a 1-element list.
267 * Note that the node returned by the user may be an entire tree.
269 * Since the node itself cannot enforce any constraints, we turn
270 * all pending constraints into guards and add them to the resulting
271 * graft to ensure that they will be generated.
273 * Before we pass control to the user, we first clear some information
274 * from the build that is (presumbably) only meaningful
275 * for the current code generation.
276 * This includes the create_leaf callback itself, so we make a copy
277 * of the build first.
279 static __isl_give isl_ast_graft_list
*call_create_leaf(
280 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
284 isl_ast_graft
*graft
;
285 isl_ast_build
*user_build
;
287 guard
= isl_ast_build_get_pending(build
);
288 user_build
= isl_ast_build_copy(build
);
289 user_build
= isl_ast_build_replace_pending_by_guard(user_build
,
290 isl_set_copy(guard
));
291 user_build
= isl_ast_build_set_executed(user_build
, executed
);
292 user_build
= isl_ast_build_clear_local_info(user_build
);
296 node
= build
->create_leaf(user_build
, build
->create_leaf_user
);
297 graft
= isl_ast_graft_alloc(node
, build
);
298 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
299 isl_ast_build_free(build
);
300 return isl_ast_graft_list_from_ast_graft(graft
);
303 static __isl_give isl_ast_graft_list
*build_ast_from_child(
304 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
305 __isl_take isl_union_map
*executed
);
307 /* Generate an AST after having handled the complete schedule
308 * of this call to the code generator or the complete band
309 * if we are generating an AST from a schedule tree.
311 * If we are inside a band node, then move on to the child of the band.
313 * If the user has specified a create_leaf callback, control
314 * is passed to the user in call_create_leaf.
316 * Otherwise, we generate one or more calls for each individual
317 * domain in generate_domain.
319 static __isl_give isl_ast_graft_list
*generate_inner_level(
320 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
323 struct isl_generate_domain_data data
= { build
};
325 if (!build
|| !executed
)
328 if (isl_ast_build_has_schedule_node(build
)) {
329 isl_schedule_node
*node
;
330 node
= isl_ast_build_get_schedule_node(build
);
331 build
= isl_ast_build_reset_schedule_node(build
);
332 return build_ast_from_child(build
, node
, executed
);
335 if (build
->create_leaf
)
336 return call_create_leaf(executed
, build
);
338 ctx
= isl_union_map_get_ctx(executed
);
339 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
340 if (isl_union_map_foreach_map(executed
, &generate_domain
, &data
) < 0)
341 data
.list
= isl_ast_graft_list_free(data
.list
);
344 error
: data
.list
= NULL
;
345 isl_ast_build_free(build
);
346 isl_union_map_free(executed
);
350 /* Call the before_each_for callback, if requested by the user.
352 static __isl_give isl_ast_node
*before_each_for(__isl_take isl_ast_node
*node
,
353 __isl_keep isl_ast_build
*build
)
358 return isl_ast_node_free(node
);
359 if (!build
->before_each_for
)
361 id
= build
->before_each_for(build
, build
->before_each_for_user
);
362 node
= isl_ast_node_set_annotation(node
, id
);
366 /* Call the after_each_for callback, if requested by the user.
368 static __isl_give isl_ast_graft
*after_each_for(__isl_take isl_ast_graft
*graft
,
369 __isl_keep isl_ast_build
*build
)
371 if (!graft
|| !build
)
372 return isl_ast_graft_free(graft
);
373 if (!build
->after_each_for
)
375 graft
->node
= build
->after_each_for(graft
->node
, build
,
376 build
->after_each_for_user
);
378 return isl_ast_graft_free(graft
);
382 /* Plug in all the know values of the current and outer dimensions
383 * in the domain of "executed". In principle, we only need to plug
384 * in the known value of the current dimension since the values of
385 * outer dimensions have been plugged in already.
386 * However, it turns out to be easier to just plug in all known values.
388 static __isl_give isl_union_map
*plug_in_values(
389 __isl_take isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
391 return isl_ast_build_substitute_values_union_map_domain(build
,
395 /* Check if the constraint "c" is a lower bound on dimension "pos",
396 * an upper bound, or independent of dimension "pos".
398 static int constraint_type(isl_constraint
*c
, int pos
)
400 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
402 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
407 /* Compare the types of the constraints "a" and "b",
408 * resulting in constraints that are independent of "depth"
409 * to be sorted before the lower bounds on "depth", which in
410 * turn are sorted before the upper bounds on "depth".
412 static int cmp_constraint(__isl_keep isl_constraint
*a
,
413 __isl_keep isl_constraint
*b
, void *user
)
416 int t1
= constraint_type(a
, *depth
);
417 int t2
= constraint_type(b
, *depth
);
422 /* Extract a lower bound on dimension "pos" from constraint "c".
424 * If the constraint is of the form
428 * then we essentially return
430 * l = ceil(-f(...)/a)
432 * However, if the current dimension is strided, then we need to make
433 * sure that the lower bound we construct is of the form
437 * with f the offset and s the stride.
438 * We therefore compute
440 * f + s * ceil((l - f)/s)
442 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
443 int pos
, __isl_keep isl_ast_build
*build
)
447 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
448 aff
= isl_aff_ceil(aff
);
450 if (isl_ast_build_has_stride(build
, pos
)) {
454 offset
= isl_ast_build_get_offset(build
, pos
);
455 stride
= isl_ast_build_get_stride(build
, pos
);
457 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
458 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(stride
));
459 aff
= isl_aff_ceil(aff
);
460 aff
= isl_aff_scale_val(aff
, stride
);
461 aff
= isl_aff_add(aff
, offset
);
464 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
469 /* Return the exact lower bound (or upper bound if "upper" is set)
470 * of "domain" as a piecewise affine expression.
472 * If we are computing a lower bound (of a strided dimension), then
473 * we need to make sure it is of the form
477 * where f is the offset and s is the stride.
478 * We therefore need to include the stride constraint before computing
481 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
482 __isl_keep isl_ast_build
*build
, int upper
)
487 isl_pw_multi_aff
*pma
;
489 domain
= isl_set_copy(domain
);
491 stride
= isl_ast_build_get_stride_constraint(build
);
492 domain
= isl_set_intersect(domain
, stride
);
494 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
496 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
498 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
499 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
500 isl_pw_multi_aff_free(pma
);
501 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
502 pa
= isl_pw_aff_coalesce(pa
);
507 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
508 * remove_redundant_lower_bounds.
510 static int reduce_list_cmp(__isl_keep isl_pw_aff
*a
, __isl_keep isl_pw_aff
*b
,
513 return isl_pw_aff_plain_cmp(a
, b
);
516 /* Given a list of lower bounds "list", remove those that are redundant
517 * with respect to the other bounds in "list" and the domain of "build".
519 * We first sort the bounds in the same way as they would be sorted
520 * by set_for_node_expressions so that we can try and remove the last
523 * For a lower bound to be effective, there needs to be at least
524 * one domain element for which it is larger than all other lower bounds.
525 * For each lower bound we therefore intersect the domain with
526 * the conditions that it is larger than all other bounds and
527 * check whether the result is empty. If so, the bound can be removed.
529 static __isl_give isl_pw_aff_list
*remove_redundant_lower_bounds(
530 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
536 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
538 n
= isl_pw_aff_list_n_pw_aff(list
);
540 return isl_pw_aff_list_free(list
);
544 domain
= isl_ast_build_get_domain(build
);
546 for (i
= n
- 1; i
>= 0; --i
) {
551 domain_i
= isl_set_copy(domain
);
552 pa_i
= isl_pw_aff_list_get_pw_aff(list
, i
);
554 for (j
= 0; j
< n
; ++j
) {
561 pa_j
= isl_pw_aff_list_get_pw_aff(list
, j
);
562 better
= isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i
), pa_j
);
563 domain_i
= isl_set_intersect(domain_i
, better
);
566 empty
= isl_set_is_empty(domain_i
);
568 isl_set_free(domain_i
);
569 isl_pw_aff_free(pa_i
);
575 list
= isl_pw_aff_list_drop(list
, i
, 1);
579 isl_set_free(domain
);
583 isl_set_free(domain
);
584 return isl_pw_aff_list_free(list
);
587 /* Extract a lower bound on dimension "pos" from each constraint
588 * in "constraints" and return the list of lower bounds.
589 * If "constraints" has zero elements, then we extract a lower bound
590 * from "domain" instead.
592 * If the current dimension is strided, then the lower bound
593 * is adjusted by lower_bound to match the stride information.
594 * This modification may make one or more lower bounds redundant
595 * with respect to the other lower bounds. We therefore check
596 * for this condition and remove the redundant lower bounds.
598 static __isl_give isl_pw_aff_list
*lower_bounds(
599 __isl_keep isl_constraint_list
*constraints
, int pos
,
600 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
603 isl_pw_aff_list
*list
;
610 n
= isl_constraint_list_n_constraint(constraints
);
615 pa
= exact_bound(domain
, build
, 0);
616 return isl_pw_aff_list_from_pw_aff(pa
);
619 ctx
= isl_ast_build_get_ctx(build
);
620 list
= isl_pw_aff_list_alloc(ctx
,n
);
622 for (i
= 0; i
< n
; ++i
) {
626 c
= isl_constraint_list_get_constraint(constraints
, i
);
627 aff
= lower_bound(c
, pos
, build
);
628 isl_constraint_free(c
);
629 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
632 if (isl_ast_build_has_stride(build
, pos
))
633 list
= remove_redundant_lower_bounds(list
, build
);
638 /* Extract an upper bound on dimension "pos" from each constraint
639 * in "constraints" and return the list of upper bounds.
640 * If "constraints" has zero elements, then we extract an upper bound
641 * from "domain" instead.
643 static __isl_give isl_pw_aff_list
*upper_bounds(
644 __isl_keep isl_constraint_list
*constraints
, int pos
,
645 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
648 isl_pw_aff_list
*list
;
652 n
= isl_constraint_list_n_constraint(constraints
);
657 pa
= exact_bound(domain
, build
, 1);
658 return isl_pw_aff_list_from_pw_aff(pa
);
661 ctx
= isl_ast_build_get_ctx(build
);
662 list
= isl_pw_aff_list_alloc(ctx
,n
);
664 for (i
= 0; i
< n
; ++i
) {
668 c
= isl_constraint_list_get_constraint(constraints
, i
);
669 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
670 isl_constraint_free(c
);
671 aff
= isl_aff_floor(aff
);
672 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
678 /* Return an isl_ast_expr that performs the reduction of type "type"
679 * on AST expressions corresponding to the elements in "list".
681 * The list is assumed to contain at least one element.
682 * If the list contains exactly one element, then the returned isl_ast_expr
683 * simply computes that affine expression.
684 * If the list contains more than one element, then we sort it
685 * using a fairly arbitrary but hopefully reasonably stable order.
687 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_expr_op_type type
,
688 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
695 n
= isl_pw_aff_list_n_pw_aff(list
);
700 return isl_ast_build_expr_from_pw_aff_internal(build
,
701 isl_pw_aff_list_get_pw_aff(list
, 0));
703 ctx
= isl_pw_aff_list_get_ctx(list
);
704 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
708 list
= isl_pw_aff_list_copy(list
);
709 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
711 return isl_ast_expr_free(expr
);
713 for (i
= 0; i
< n
; ++i
) {
714 isl_ast_expr
*expr_i
;
716 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
717 isl_pw_aff_list_get_pw_aff(list
, i
));
720 expr
->u
.op
.args
[i
] = expr_i
;
723 isl_pw_aff_list_free(list
);
726 isl_pw_aff_list_free(list
);
727 isl_ast_expr_free(expr
);
731 /* Add guards implied by the "generated constraints",
732 * but not (necessarily) enforced by the generated AST to "guard".
733 * In particular, if there is any stride constraints,
734 * then add the guard implied by those constraints.
735 * If we have generated a degenerate loop, then add the guard
736 * implied by "bounds" on the outer dimensions, i.e., the guard
737 * that ensures that the single value actually exists.
738 * Since there may also be guards implied by a combination
739 * of these constraints, we first combine them before
740 * deriving the implied constraints.
742 static __isl_give isl_set
*add_implied_guards(__isl_take isl_set
*guard
,
743 int degenerate
, __isl_keep isl_basic_set
*bounds
,
744 __isl_keep isl_ast_build
*build
)
751 depth
= isl_ast_build_get_depth(build
);
752 has_stride
= isl_ast_build_has_stride(build
, depth
);
753 if (depth
< 0 || has_stride
< 0)
754 return isl_set_free(guard
);
755 if (!has_stride
&& !degenerate
)
758 space
= isl_basic_set_get_space(bounds
);
759 dom
= isl_set_universe(space
);
762 bounds
= isl_basic_set_copy(bounds
);
763 bounds
= isl_basic_set_drop_constraints_not_involving_dims(
764 bounds
, isl_dim_set
, depth
, 1);
765 set
= isl_set_from_basic_set(bounds
);
766 dom
= isl_set_intersect(dom
, set
);
770 set
= isl_ast_build_get_stride_constraint(build
);
771 dom
= isl_set_intersect(dom
, set
);
774 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
775 dom
= isl_ast_build_compute_gist(build
, dom
);
776 guard
= isl_set_intersect(guard
, dom
);
781 /* Update "graft" based on "sub_build" for the degenerate case.
783 * "build" is the build in which graft->node was created
784 * "sub_build" contains information about the current level itself,
785 * including the single value attained.
787 * We set the initialization part of the for loop to the single
788 * value attained by the current dimension.
789 * The increment and condition are not strictly needed as they are known
790 * to be "1" and "iterator <= value" respectively.
792 static __isl_give isl_ast_graft
*refine_degenerate(
793 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
,
794 __isl_keep isl_ast_build
*sub_build
)
798 if (!graft
|| !sub_build
)
799 return isl_ast_graft_free(graft
);
801 value
= isl_pw_aff_copy(sub_build
->value
);
803 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
805 if (!graft
->node
->u
.f
.init
)
806 return isl_ast_graft_free(graft
);
811 /* Return the intersection of constraints in "list" as a set.
813 static __isl_give isl_set
*intersect_constraints(
814 __isl_keep isl_constraint_list
*list
)
820 n
= isl_constraint_list_n_constraint(list
);
824 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
825 "expecting at least one constraint", return NULL
);
827 bset
= isl_basic_set_from_constraint(
828 isl_constraint_list_get_constraint(list
, 0));
829 for (i
= 1; i
< n
; ++i
) {
830 isl_basic_set
*bset_i
;
832 bset_i
= isl_basic_set_from_constraint(
833 isl_constraint_list_get_constraint(list
, i
));
834 bset
= isl_basic_set_intersect(bset
, bset_i
);
837 return isl_set_from_basic_set(bset
);
840 /* Compute the constraints on the outer dimensions enforced by
841 * graft->node and add those constraints to graft->enforced,
842 * in case the upper bound is expressed as a set "upper".
844 * In particular, if l(...) is a lower bound in "lower", and
846 * -a i + f(...) >= 0 or a i <= f(...)
848 * is an upper bound ocnstraint on the current dimension i,
849 * then the for loop enforces the constraint
851 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
853 * We therefore simply take each lower bound in turn, plug it into
854 * the upper bounds and compute the intersection over all lower bounds.
856 * If a lower bound is a rational expression, then
857 * isl_basic_set_preimage_multi_aff will force this rational
858 * expression to have only integer values. However, the loop
859 * itself does not enforce this integrality constraint. We therefore
860 * use the ceil of the lower bounds instead of the lower bounds themselves.
861 * Other constraints will make sure that the for loop is only executed
862 * when each of the lower bounds attains an integral value.
863 * In particular, potentially rational values only occur in
864 * lower_bound if the offset is a (seemingly) rational expression,
865 * but then outer conditions will make sure that this rational expression
866 * only attains integer values.
868 static __isl_give isl_ast_graft
*set_enforced_from_set(
869 __isl_take isl_ast_graft
*graft
,
870 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
873 isl_basic_set
*enforced
;
874 isl_pw_multi_aff
*pma
;
878 n
= isl_pw_aff_list_n_pw_aff(lower
);
880 return isl_ast_graft_free(graft
);
882 space
= isl_set_get_space(upper
);
883 enforced
= isl_basic_set_universe(isl_space_copy(space
));
885 space
= isl_space_map_from_set(space
);
886 pma
= isl_pw_multi_aff_identity(space
);
888 for (i
= 0; i
< n
; ++i
) {
892 isl_pw_multi_aff
*pma_i
;
894 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
895 pa
= isl_pw_aff_ceil(pa
);
896 pma_i
= isl_pw_multi_aff_copy(pma
);
897 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
898 enforced_i
= isl_set_copy(upper
);
899 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
900 hull
= isl_set_simple_hull(enforced_i
);
901 enforced
= isl_basic_set_intersect(enforced
, hull
);
904 isl_pw_multi_aff_free(pma
);
906 graft
= isl_ast_graft_enforce(graft
, enforced
);
911 /* Compute the constraints on the outer dimensions enforced by
912 * graft->node and add those constraints to graft->enforced,
913 * in case the upper bound is expressed as
914 * a list of affine expressions "upper".
916 * The enforced condition is that each lower bound expression is less
917 * than or equal to each upper bound expression.
919 static __isl_give isl_ast_graft
*set_enforced_from_list(
920 __isl_take isl_ast_graft
*graft
,
921 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
924 isl_basic_set
*enforced
;
926 lower
= isl_pw_aff_list_copy(lower
);
927 upper
= isl_pw_aff_list_copy(upper
);
928 cond
= isl_pw_aff_list_le_set(lower
, upper
);
929 enforced
= isl_set_simple_hull(cond
);
930 graft
= isl_ast_graft_enforce(graft
, enforced
);
935 /* Does "aff" have a negative constant term?
937 static isl_bool
aff_constant_is_negative(__isl_keep isl_set
*set
,
938 __isl_keep isl_aff
*aff
, void *user
)
943 v
= isl_aff_get_constant_val(aff
);
944 is_neg
= isl_val_is_neg(v
);
950 /* Does "pa" have a negative constant term over its entire domain?
952 static isl_bool
pw_aff_constant_is_negative(__isl_keep isl_pw_aff
*pa
,
955 return isl_pw_aff_every_piece(pa
, &aff_constant_is_negative
, NULL
);
958 /* Does each element in "list" have a negative constant term?
960 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
962 return isl_pw_aff_list_every(list
, &pw_aff_constant_is_negative
, NULL
);
965 /* Add 1 to each of the elements in "list", where each of these elements
966 * is defined over the internal schedule space of "build".
968 static __isl_give isl_pw_aff_list
*list_add_one(
969 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
977 n
= isl_pw_aff_list_n_pw_aff(list
);
979 return isl_pw_aff_list_free(list
);
981 space
= isl_ast_build_get_space(build
, 1);
982 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
983 aff
= isl_aff_add_constant_si(aff
, 1);
984 one
= isl_pw_aff_from_aff(aff
);
986 for (i
= 0; i
< n
; ++i
) {
988 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
989 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
990 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
993 isl_pw_aff_free(one
);
998 /* Set the condition part of the for node graft->node in case
999 * the upper bound is represented as a list of piecewise affine expressions.
1001 * In particular, set the condition to
1003 * iterator <= min(list of upper bounds)
1005 * If each of the upper bounds has a negative constant term, then
1006 * set the condition to
1008 * iterator < min(list of (upper bound + 1)s)
1011 static __isl_give isl_ast_graft
*set_for_cond_from_list(
1012 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
1013 __isl_keep isl_ast_build
*build
)
1016 isl_ast_expr
*bound
, *iterator
, *cond
;
1017 enum isl_ast_expr_op_type type
= isl_ast_expr_op_le
;
1019 if (!graft
|| !list
)
1020 return isl_ast_graft_free(graft
);
1022 neg
= list_constant_is_negative(list
);
1024 return isl_ast_graft_free(graft
);
1025 list
= isl_pw_aff_list_copy(list
);
1027 list
= list_add_one(list
, build
);
1028 type
= isl_ast_expr_op_lt
;
1031 bound
= reduce_list(isl_ast_expr_op_min
, list
, build
);
1032 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
1033 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
1034 graft
->node
->u
.f
.cond
= cond
;
1036 isl_pw_aff_list_free(list
);
1037 if (!graft
->node
->u
.f
.cond
)
1038 return isl_ast_graft_free(graft
);
1042 /* Set the condition part of the for node graft->node in case
1043 * the upper bound is represented as a set.
1045 static __isl_give isl_ast_graft
*set_for_cond_from_set(
1046 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
1047 __isl_keep isl_ast_build
*build
)
1054 cond
= isl_ast_build_expr_from_set_internal(build
, isl_set_copy(set
));
1055 graft
->node
->u
.f
.cond
= cond
;
1056 if (!graft
->node
->u
.f
.cond
)
1057 return isl_ast_graft_free(graft
);
1061 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1062 * the current dimension.
1064 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
1070 depth
= isl_ast_build_get_depth(build
);
1073 ctx
= isl_ast_build_get_ctx(build
);
1075 if (!isl_ast_build_has_stride(build
, depth
))
1076 return isl_ast_expr_alloc_int_si(ctx
, 1);
1078 v
= isl_ast_build_get_stride(build
, depth
);
1079 return isl_ast_expr_from_val(v
);
1082 /* Should we express the loop condition as
1084 * iterator <= min(list of upper bounds)
1086 * or as a conjunction of constraints?
1088 * The first is constructed from a list of upper bounds.
1089 * The second is constructed from a set.
1091 * If there are no upper bounds in "constraints", then this could mean
1092 * that "domain" simply doesn't have an upper bound or that we didn't
1093 * pick any upper bound. In the first case, we want to generate the
1094 * loop condition as a(n empty) conjunction of constraints
1095 * In the second case, we will compute
1096 * a single upper bound from "domain" and so we use the list form.
1098 * If there are upper bounds in "constraints",
1099 * then we use the list form iff the atomic_upper_bound option is set.
1101 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
1102 __isl_keep isl_set
*domain
, int depth
)
1105 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
1107 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
1110 /* Fill in the expressions of the for node in graft->node.
1113 * - set the initialization part of the loop to the maximum of the lower bounds
1114 * - extract the increment from the stride of the current dimension
1115 * - construct the for condition either based on a list of upper bounds
1116 * or on a set of upper bound constraints.
1118 static __isl_give isl_ast_graft
*set_for_node_expressions(
1119 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
1120 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
1121 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
1129 node
->u
.f
.init
= reduce_list(isl_ast_expr_op_max
, lower
, build
);
1130 node
->u
.f
.inc
= for_inc(build
);
1132 if (!node
->u
.f
.init
|| !node
->u
.f
.inc
)
1133 graft
= isl_ast_graft_free(graft
);
1136 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
1138 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
1143 /* Update "graft" based on "bounds" and "domain" for the generic,
1144 * non-degenerate, case.
1146 * "c_lower" and "c_upper" contain the lower and upper bounds
1147 * that the loop node should express.
1148 * "domain" is the subset of the intersection of the constraints
1149 * for which some code is executed.
1151 * There may be zero lower bounds or zero upper bounds in "constraints"
1152 * in case the list of constraints was created
1153 * based on the atomic option or based on separation with explicit bounds.
1154 * In that case, we use "domain" to derive lower and/or upper bounds.
1156 * We first compute a list of one or more lower bounds.
1158 * Then we decide if we want to express the condition as
1160 * iterator <= min(list of upper bounds)
1162 * or as a conjunction of constraints.
1164 * The set of enforced constraints is then computed either based on
1165 * a list of upper bounds or on a set of upper bound constraints.
1166 * We do not compute any enforced constraints if we were forced
1167 * to compute a lower or upper bound using exact_bound. The domains
1168 * of the resulting expressions may imply some bounds on outer dimensions
1169 * that we do not want to appear in the enforced constraints since
1170 * they are not actually enforced by the corresponding code.
1172 * Finally, we fill in the expressions of the for node.
1174 static __isl_give isl_ast_graft
*refine_generic_bounds(
1175 __isl_take isl_ast_graft
*graft
,
1176 __isl_take isl_constraint_list
*c_lower
,
1177 __isl_take isl_constraint_list
*c_upper
,
1178 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1182 isl_pw_aff_list
*lower
;
1184 isl_set
*upper_set
= NULL
;
1185 isl_pw_aff_list
*upper_list
= NULL
;
1186 isl_size n_lower
, n_upper
;
1188 depth
= isl_ast_build_get_depth(build
);
1189 if (!graft
|| !c_lower
|| !c_upper
|| depth
< 0)
1192 ctx
= isl_ast_graft_get_ctx(graft
);
1194 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1195 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1196 if (n_lower
< 0 || n_upper
< 0)
1199 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1201 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1204 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1205 else if (n_upper
> 0)
1206 upper_set
= intersect_constraints(c_upper
);
1208 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1210 if (n_lower
== 0 || n_upper
== 0)
1213 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1215 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1217 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1220 isl_pw_aff_list_free(lower
);
1221 isl_pw_aff_list_free(upper_list
);
1222 isl_set_free(upper_set
);
1223 isl_constraint_list_free(c_lower
);
1224 isl_constraint_list_free(c_upper
);
1228 isl_constraint_list_free(c_lower
);
1229 isl_constraint_list_free(c_upper
);
1230 return isl_ast_graft_free(graft
);
1233 /* Internal data structure used inside count_constraints to keep
1234 * track of the number of constraints that are independent of dimension "pos",
1235 * the lower bounds in "pos" and the upper bounds in "pos".
1237 struct isl_ast_count_constraints_data
{
1245 /* Increment data->n_indep, data->lower or data->upper depending
1246 * on whether "c" is independent of dimensions data->pos,
1247 * a lower bound or an upper bound.
1249 static isl_stat
count_constraints(__isl_take isl_constraint
*c
, void *user
)
1251 struct isl_ast_count_constraints_data
*data
= user
;
1253 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1255 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1260 isl_constraint_free(c
);
1265 /* Update "graft" based on "bounds" and "domain" for the generic,
1266 * non-degenerate, case.
1268 * "list" respresent the list of bounds that need to be encoded by
1269 * the for loop. Only the constraints that involve the iterator
1270 * are relevant here. The other constraints are taken care of by
1271 * the caller and are included in the generated constraints of "build".
1272 * "domain" is the subset of the intersection of the constraints
1273 * for which some code is executed.
1274 * "build" is the build in which graft->node was created.
1276 * We separate lower bounds, upper bounds and constraints that
1277 * are independent of the loop iterator.
1279 * The actual for loop bounds are generated in refine_generic_bounds.
1281 static __isl_give isl_ast_graft
*refine_generic_split(
1282 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1283 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1285 struct isl_ast_count_constraints_data data
;
1287 isl_constraint_list
*lower
;
1288 isl_constraint_list
*upper
;
1290 depth
= isl_ast_build_get_depth(build
);
1292 list
= isl_constraint_list_free(list
);
1294 return isl_ast_graft_free(graft
);
1298 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1300 return isl_ast_graft_free(graft
);
1302 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1303 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1304 isl_constraint_list_free(list
);
1305 return isl_ast_graft_free(graft
);
1308 lower
= isl_constraint_list_drop(list
, 0, data
.n_indep
);
1309 upper
= isl_constraint_list_copy(lower
);
1310 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1311 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1313 return refine_generic_bounds(graft
, lower
, upper
, domain
, build
);
1316 /* Update "graft" based on "bounds" and "domain" for the generic,
1317 * non-degenerate, case.
1319 * "bounds" respresent the bounds that need to be encoded by
1320 * the for loop (or a guard around the for loop).
1321 * "domain" is the subset of "bounds" for which some code is executed.
1322 * "build" is the build in which graft->node was created.
1324 * We break up "bounds" into a list of constraints and continue with
1325 * refine_generic_split.
1327 static __isl_give isl_ast_graft
*refine_generic(
1328 __isl_take isl_ast_graft
*graft
,
1329 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1330 __isl_keep isl_ast_build
*build
)
1332 isl_constraint_list
*list
;
1334 if (!build
|| !graft
)
1335 return isl_ast_graft_free(graft
);
1337 list
= isl_basic_set_get_constraint_list(bounds
);
1339 graft
= refine_generic_split(graft
, list
, domain
, build
);
1344 /* Create a for node for the current level.
1346 * Mark the for node degenerate if "degenerate" is set.
1348 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1355 depth
= isl_ast_build_get_depth(build
);
1359 id
= isl_ast_build_get_iterator_id(build
, depth
);
1360 node
= isl_ast_node_alloc_for(id
);
1362 node
= isl_ast_node_for_mark_degenerate(node
);
1367 /* If the ast_build_exploit_nested_bounds option is set, then return
1368 * the constraints enforced by all elements in "list".
1369 * Otherwise, return the universe.
1371 static __isl_give isl_basic_set
*extract_shared_enforced(
1372 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1380 ctx
= isl_ast_graft_list_get_ctx(list
);
1381 if (isl_options_get_ast_build_exploit_nested_bounds(ctx
))
1382 return isl_ast_graft_list_extract_shared_enforced(list
, build
);
1384 space
= isl_ast_build_get_space(build
, 1);
1385 return isl_basic_set_universe(space
);
1388 /* Return the pending constraints of "build" that are not already taken
1389 * care of (by a combination of "enforced" and the generated constraints
1392 static __isl_give isl_set
*extract_pending(__isl_keep isl_ast_build
*build
,
1393 __isl_keep isl_basic_set
*enforced
)
1395 isl_set
*guard
, *context
;
1397 guard
= isl_ast_build_get_pending(build
);
1398 context
= isl_set_from_basic_set(isl_basic_set_copy(enforced
));
1399 context
= isl_set_intersect(context
,
1400 isl_ast_build_get_generated(build
));
1401 return isl_set_gist(guard
, context
);
1404 /* Create an AST node for the current dimension based on
1405 * the schedule domain "bounds" and return the node encapsulated
1406 * in an isl_ast_graft.
1408 * "executed" is the current inverse schedule, taking into account
1409 * the bounds in "bounds"
1410 * "domain" is the domain of "executed", with inner dimensions projected out.
1411 * It may be a strict subset of "bounds" in case "bounds" was created
1412 * based on the atomic option or based on separation with explicit bounds.
1414 * "domain" may satisfy additional equalities that result
1415 * from intersecting "executed" with "bounds" in add_node.
1416 * It may also satisfy some global constraints that were dropped out because
1417 * we performed separation with explicit bounds.
1418 * The very first step is then to copy these constraints to "bounds".
1420 * Since we may be calling before_each_for and after_each_for
1421 * callbacks, we record the current inverse schedule in the build.
1423 * We consider three builds,
1424 * "build" is the one in which the current level is created,
1425 * "body_build" is the build in which the next level is created,
1426 * "sub_build" is essentially the same as "body_build", except that
1427 * the depth has not been increased yet.
1429 * "build" already contains information (in strides and offsets)
1430 * about the strides at the current level, but this information is not
1431 * reflected in the build->domain.
1432 * We first add this information and the "bounds" to the sub_build->domain.
1433 * isl_ast_build_set_loop_bounds adds the stride information and
1434 * checks whether the current dimension attains
1435 * only a single value and whether this single value can be represented using
1436 * a single affine expression.
1437 * In the first case, the current level is considered "degenerate".
1438 * In the second, sub-case, the current level is considered "eliminated".
1439 * Eliminated levels don't need to be reflected in the AST since we can
1440 * simply plug in the affine expression. For degenerate, but non-eliminated,
1441 * levels, we do introduce a for node, but mark is as degenerate so that
1442 * it can be printed as an assignment of the single value to the loop
1445 * If the current level is eliminated, we explicitly plug in the value
1446 * for the current level found by isl_ast_build_set_loop_bounds in the
1447 * inverse schedule. This ensures that if we are working on a slice
1448 * of the domain based on information available in the inverse schedule
1449 * and the build domain, that then this information is also reflected
1450 * in the inverse schedule. This operation also eliminates the current
1451 * dimension from the inverse schedule making sure no inner dimensions depend
1452 * on the current dimension. Otherwise, we create a for node, marking
1453 * it degenerate if appropriate. The initial for node is still incomplete
1454 * and will be completed in either refine_degenerate or refine_generic.
1456 * We then generate a sequence of grafts for the next level,
1457 * create a surrounding graft for the current level and insert
1458 * the for node we created (if the current level is not eliminated).
1459 * Before creating a graft for the current level, we first extract
1460 * hoistable constraints from the child guards and combine them
1461 * with the pending constraints in the build. These constraints
1462 * are used to simplify the child guards and then added to the guard
1463 * of the current graft to ensure that they will be generated.
1464 * If the hoisted guard is a disjunction, then we use it directly
1465 * to gist the guards on the children before intersect it with the
1466 * pending constraints. We do so because this disjunction is typically
1467 * identical to the guards on the children such that these guards
1468 * can be effectively removed completely. After the intersection,
1469 * the gist operation would have a harder time figuring this out.
1471 * Finally, we set the bounds of the for loop in either
1472 * refine_degenerate or refine_generic.
1473 * We do so in a context where the pending constraints of the build
1474 * have been replaced by the guard of the current graft.
1476 static __isl_give isl_ast_graft
*create_node_scaled(
1477 __isl_take isl_union_map
*executed
,
1478 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1479 __isl_take isl_ast_build
*build
)
1483 isl_bool eliminated
;
1485 isl_basic_set
*hull
;
1486 isl_basic_set
*enforced
;
1487 isl_set
*guard
, *hoisted
;
1488 isl_ast_node
*node
= NULL
;
1489 isl_ast_graft
*graft
;
1490 isl_ast_graft_list
*children
;
1491 isl_ast_build
*sub_build
;
1492 isl_ast_build
*body_build
;
1494 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1495 domain
= isl_set_detect_equalities(domain
);
1496 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1497 bounds
= isl_basic_set_intersect(bounds
, hull
);
1498 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1500 depth
= isl_ast_build_get_depth(build
);
1502 build
= isl_ast_build_free(build
);
1503 sub_build
= isl_ast_build_copy(build
);
1504 bounds
= isl_basic_set_remove_redundancies(bounds
);
1505 bounds
= isl_ast_build_specialize_basic_set(sub_build
, bounds
);
1506 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1507 isl_basic_set_copy(bounds
));
1508 degenerate
= isl_ast_build_has_value(sub_build
);
1509 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1510 if (degenerate
< 0 || eliminated
< 0)
1511 executed
= isl_union_map_free(executed
);
1513 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1514 sub_build
= isl_ast_build_set_pending_generated(sub_build
,
1515 isl_basic_set_copy(bounds
));
1517 executed
= plug_in_values(executed
, sub_build
);
1519 node
= create_for(build
, degenerate
);
1521 body_build
= isl_ast_build_copy(sub_build
);
1522 body_build
= isl_ast_build_increase_depth(body_build
);
1524 node
= before_each_for(node
, body_build
);
1525 children
= generate_next_level(executed
,
1526 isl_ast_build_copy(body_build
));
1528 enforced
= extract_shared_enforced(children
, build
);
1529 guard
= extract_pending(sub_build
, enforced
);
1530 hoisted
= isl_ast_graft_list_extract_hoistable_guard(children
, build
);
1531 n
= isl_set_n_basic_set(hoisted
);
1533 children
= isl_ast_graft_list_free(children
);
1535 children
= isl_ast_graft_list_gist_guards(children
,
1536 isl_set_copy(hoisted
));
1537 guard
= isl_set_intersect(guard
, hoisted
);
1539 guard
= add_implied_guards(guard
, degenerate
, bounds
, build
);
1541 graft
= isl_ast_graft_alloc_from_children(children
,
1542 isl_set_copy(guard
), enforced
, build
, sub_build
);
1545 isl_ast_build
*for_build
;
1547 graft
= isl_ast_graft_insert_for(graft
, node
);
1548 for_build
= isl_ast_build_copy(build
);
1549 for_build
= isl_ast_build_replace_pending_by_guard(for_build
,
1550 isl_set_copy(guard
));
1552 graft
= refine_degenerate(graft
, for_build
, sub_build
);
1554 graft
= refine_generic(graft
, bounds
,
1556 isl_ast_build_free(for_build
);
1558 isl_set_free(guard
);
1560 graft
= after_each_for(graft
, body_build
);
1562 isl_ast_build_free(body_build
);
1563 isl_ast_build_free(sub_build
);
1564 isl_ast_build_free(build
);
1565 isl_basic_set_free(bounds
);
1566 isl_set_free(domain
);
1571 /* Internal data structure for checking if all constraints involving
1572 * the input dimension "depth" are such that the other coefficients
1573 * are multiples of "m", reducing "m" if they are not.
1574 * If "m" is reduced all the way down to "1", then the check has failed
1575 * and we break out of the iteration.
1577 struct isl_check_scaled_data
{
1582 /* If constraint "c" involves the input dimension data->depth,
1583 * then make sure that all the other coefficients are multiples of data->m,
1584 * reducing data->m if needed.
1585 * Break out of the iteration if data->m has become equal to "1".
1587 static isl_stat
constraint_check_scaled(__isl_take isl_constraint
*c
,
1590 struct isl_check_scaled_data
*data
= user
;
1593 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1596 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1597 isl_constraint_free(c
);
1601 for (i
= 0; i
< 4; ++i
) {
1602 n
= isl_constraint_dim(c
, t
[i
]);
1605 for (j
= 0; j
< n
; ++j
) {
1608 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1610 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1612 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1613 data
->m
= isl_val_gcd(data
->m
, d
);
1614 if (isl_val_is_one(data
->m
))
1621 isl_constraint_free(c
);
1623 return i
< 4 ? isl_stat_error
: isl_stat_ok
;
1626 /* For each constraint of "bmap" that involves the input dimension data->depth,
1627 * make sure that all the other coefficients are multiples of data->m,
1628 * reducing data->m if needed.
1629 * Break out of the iteration if data->m has become equal to "1".
1631 static isl_stat
basic_map_check_scaled(__isl_take isl_basic_map
*bmap
,
1636 r
= isl_basic_map_foreach_constraint(bmap
,
1637 &constraint_check_scaled
, user
);
1638 isl_basic_map_free(bmap
);
1643 /* For each constraint of "map" that involves the input dimension data->depth,
1644 * make sure that all the other coefficients are multiples of data->m,
1645 * reducing data->m if needed.
1646 * Break out of the iteration if data->m has become equal to "1".
1648 static isl_stat
map_check_scaled(__isl_take isl_map
*map
, void *user
)
1652 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1658 /* Create an AST node for the current dimension based on
1659 * the schedule domain "bounds" and return the node encapsulated
1660 * in an isl_ast_graft.
1662 * "executed" is the current inverse schedule, taking into account
1663 * the bounds in "bounds"
1664 * "domain" is the domain of "executed", with inner dimensions projected out.
1667 * Before moving on to the actual AST node construction in create_node_scaled,
1668 * we first check if the current dimension is strided and if we can scale
1669 * down this stride. Note that we only do this if the ast_build_scale_strides
1672 * In particular, let the current dimension take on values
1676 * with a an integer. We check if we can find an integer m that (obviously)
1677 * divides both f and s.
1679 * If so, we check if the current dimension only appears in constraints
1680 * where the coefficients of the other variables are multiples of m.
1681 * We perform this extra check to avoid the risk of introducing
1682 * divisions by scaling down the current dimension.
1684 * If so, we scale the current dimension down by a factor of m.
1685 * That is, we plug in
1689 * Note that in principle we could always scale down strided loops
1694 * but this may result in i' taking on larger values than the original i,
1695 * due to the shift by "f".
1696 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1698 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1699 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1700 __isl_take isl_ast_build
*build
)
1702 struct isl_check_scaled_data data
;
1708 ctx
= isl_ast_build_get_ctx(build
);
1709 if (!isl_options_get_ast_build_scale_strides(ctx
))
1710 return create_node_scaled(executed
, bounds
, domain
, build
);
1712 depth
= isl_ast_build_get_depth(build
);
1714 build
= isl_ast_build_free(build
);
1716 if (!isl_ast_build_has_stride(build
, data
.depth
))
1717 return create_node_scaled(executed
, bounds
, domain
, build
);
1719 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1720 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1722 offset
= isl_aff_free(offset
);
1723 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1724 d
= isl_aff_get_denominator_val(offset
);
1726 executed
= isl_union_map_free(executed
);
1728 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1729 data
.m
= isl_val_div(data
.m
, d
);
1731 data
.m
= isl_val_set_si(data
.m
, 1);
1735 if (!isl_val_is_one(data
.m
)) {
1736 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1738 !isl_val_is_one(data
.m
))
1739 executed
= isl_union_map_free(executed
);
1742 if (!isl_val_is_one(data
.m
)) {
1747 isl_union_map
*umap
;
1749 space
= isl_ast_build_get_space(build
, 1);
1750 space
= isl_space_map_from_set(space
);
1751 ma
= isl_multi_aff_identity(space
);
1752 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1753 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1754 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1756 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1757 isl_multi_aff_copy(ma
));
1758 domain
= isl_set_preimage_multi_aff(domain
,
1759 isl_multi_aff_copy(ma
));
1760 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1761 umap
= isl_union_map_from_map(map
);
1762 executed
= isl_union_map_apply_domain(executed
,
1763 isl_union_map_copy(umap
));
1764 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1767 isl_aff_free(offset
);
1768 isl_val_free(data
.m
);
1770 return create_node_scaled(executed
, bounds
, domain
, build
);
1773 /* Add the basic set to the list that "user" points to.
1775 static isl_stat
collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1777 isl_basic_set_list
**list
= user
;
1779 *list
= isl_basic_set_list_add(*list
, bset
);
1784 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1786 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1787 __isl_take isl_set
*set
)
1791 isl_basic_set_list
*list
;
1793 n
= isl_set_n_basic_set(set
);
1795 set
= isl_set_free(set
);
1799 ctx
= isl_set_get_ctx(set
);
1801 list
= isl_basic_set_list_alloc(ctx
, n
);
1802 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1803 list
= isl_basic_set_list_free(list
);
1809 /* Generate code for the schedule domain "bounds"
1810 * and add the result to "list".
1812 * We mainly detect strides here and check if the bounds do not
1813 * conflict with the current build domain
1814 * and then pass over control to create_node.
1816 * "bounds" reflects the bounds on the current dimension and possibly
1817 * some extra conditions on outer dimensions.
1818 * It does not, however, include any divs involving the current dimension,
1819 * so it does not capture any stride constraints.
1820 * We therefore need to compute that part of the schedule domain that
1821 * intersects with "bounds" and derive the strides from the result.
1823 static __isl_give isl_ast_graft_list
*add_node(
1824 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1825 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1827 isl_ast_graft
*graft
;
1828 isl_set
*domain
= NULL
;
1829 isl_union_set
*uset
;
1830 int empty
, disjoint
;
1832 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1833 executed
= isl_union_map_intersect_domain(executed
, uset
);
1834 empty
= isl_union_map_is_empty(executed
);
1840 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1841 domain
= isl_set_from_union_set(uset
);
1842 domain
= isl_ast_build_specialize(build
, domain
);
1844 domain
= isl_set_compute_divs(domain
);
1845 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1846 disjoint
= isl_set_is_disjoint(domain
, build
->domain
);
1852 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1854 graft
= create_node(executed
, bounds
, domain
,
1855 isl_ast_build_copy(build
));
1856 list
= isl_ast_graft_list_add(list
, graft
);
1857 isl_ast_build_free(build
);
1860 list
= isl_ast_graft_list_free(list
);
1862 isl_set_free(domain
);
1863 isl_basic_set_free(bounds
);
1864 isl_union_map_free(executed
);
1865 isl_ast_build_free(build
);
1869 /* Does any element of i follow or coincide with any element of j
1870 * at the current depth for equal values of the outer dimensions?
1872 static isl_bool
domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1873 __isl_keep isl_basic_set
*j
, void *user
)
1875 int depth
= *(int *) user
;
1876 isl_basic_map
*test
;
1880 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1881 isl_basic_set_copy(j
));
1882 for (l
= 0; l
< depth
; ++l
)
1883 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1885 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1886 isl_dim_out
, depth
);
1887 empty
= isl_basic_map_is_empty(test
);
1888 isl_basic_map_free(test
);
1890 return isl_bool_not(empty
);
1893 /* Split up each element of "list" into a part that is related to "bset"
1894 * according to "gt" and a part that is not.
1895 * Return a list that consist of "bset" and all the pieces.
1897 static __isl_give isl_basic_set_list
*add_split_on(
1898 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1899 __isl_keep isl_basic_map
*gt
)
1903 isl_basic_set_list
*res
;
1905 n
= isl_basic_set_list_n_basic_set(list
);
1907 bset
= isl_basic_set_free(bset
);
1909 gt
= isl_basic_map_copy(gt
);
1910 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1911 res
= isl_basic_set_list_from_basic_set(bset
);
1912 for (i
= 0; res
&& i
< n
; ++i
) {
1913 isl_basic_set
*bset
;
1914 isl_set
*set1
, *set2
;
1915 isl_basic_map
*bmap
;
1918 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1919 bmap
= isl_basic_map_copy(gt
);
1920 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1921 bset
= isl_basic_map_range(bmap
);
1922 empty
= isl_basic_set_is_empty(bset
);
1924 res
= isl_basic_set_list_free(res
);
1926 isl_basic_set_free(bset
);
1927 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1928 res
= isl_basic_set_list_add(res
, bset
);
1932 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1933 set1
= isl_set_from_basic_set(bset
);
1934 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1935 set2
= isl_set_from_basic_set(bset
);
1936 set1
= isl_set_subtract(set2
, set1
);
1937 set1
= isl_set_make_disjoint(set1
);
1939 res
= isl_basic_set_list_concat(res
,
1940 isl_basic_set_list_from_set(set1
));
1942 isl_basic_map_free(gt
);
1943 isl_basic_set_list_free(list
);
1947 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1948 __isl_keep isl_basic_set_list
*domain_list
,
1949 __isl_keep isl_union_map
*executed
,
1950 __isl_keep isl_ast_build
*build
);
1952 /* Internal data structure for add_nodes.
1954 * "executed" and "build" are extra arguments to be passed to add_node.
1955 * "list" collects the results.
1957 struct isl_add_nodes_data
{
1958 isl_union_map
*executed
;
1959 isl_ast_build
*build
;
1961 isl_ast_graft_list
*list
;
1964 /* Generate code for the schedule domains in "scc"
1965 * and add the results to "list".
1967 * The domains in "scc" form a strongly connected component in the ordering.
1968 * If the number of domains in "scc" is larger than 1, then this means
1969 * that we cannot determine a valid ordering for the domains in the component.
1970 * This should be fairly rare because the individual domains
1971 * have been made disjoint first.
1972 * The problem is that the domains may be integrally disjoint but not
1973 * rationally disjoint. For example, we may have domains
1975 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1977 * These two domains have an empty intersection, but their rational
1978 * relaxations do intersect. It is impossible to order these domains
1979 * in the second dimension because the first should be ordered before
1980 * the second for outer dimension equal to 0, while it should be ordered
1981 * after for outer dimension equal to 1.
1983 * This may happen in particular in case of unrolling since the domain
1984 * of each slice is replaced by its simple hull.
1986 * For each basic set i in "scc" and for each of the following basic sets j,
1987 * we split off that part of the basic set i that shares the outer dimensions
1988 * with j and lies before j in the current dimension.
1989 * We collect all the pieces in a new list that replaces "scc".
1991 * While the elements in "scc" should be disjoint, we double-check
1992 * this property to avoid running into an infinite recursion in case
1993 * they intersect due to some internal error.
1995 static isl_stat
add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1997 struct isl_add_nodes_data
*data
= user
;
2001 isl_basic_set
*bset
, *first
;
2002 isl_basic_set_list
*list
;
2006 n
= isl_basic_set_list_n_basic_set(scc
);
2009 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
2011 isl_basic_set_list_free(scc
);
2012 data
->list
= add_node(data
->list
,
2013 isl_union_map_copy(data
->executed
), bset
,
2014 isl_ast_build_copy(data
->build
));
2015 return data
->list
? isl_stat_ok
: isl_stat_error
;
2018 depth
= isl_ast_build_get_depth(data
->build
);
2020 bset
= isl_basic_set_free(bset
);
2021 space
= isl_basic_set_get_space(bset
);
2022 space
= isl_space_map_from_set(space
);
2023 gt
= isl_basic_map_universe(space
);
2024 for (i
= 0; i
< depth
; ++i
)
2025 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
2026 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
2028 first
= isl_basic_set_copy(bset
);
2029 list
= isl_basic_set_list_from_basic_set(bset
);
2030 for (i
= 1; i
< n
; ++i
) {
2033 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
2035 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
2037 list
= isl_basic_set_list_free(list
);
2039 isl_die(isl_basic_set_list_get_ctx(scc
),
2041 "basic sets in scc are assumed to be disjoint",
2042 list
= isl_basic_set_list_free(list
));
2044 list
= add_split_on(list
, bset
, gt
);
2046 isl_basic_set_free(first
);
2047 isl_basic_map_free(gt
);
2048 isl_basic_set_list_free(scc
);
2050 data
->list
= isl_ast_graft_list_concat(data
->list
,
2051 generate_sorted_domains(scc
, data
->executed
, data
->build
));
2052 isl_basic_set_list_free(scc
);
2054 return data
->list
? isl_stat_ok
: isl_stat_error
;
2056 isl_basic_set_list_free(scc
);
2057 return isl_stat_error
;
2060 /* Sort the domains in "domain_list" according to the execution order
2061 * at the current depth (for equal values of the outer dimensions),
2062 * generate code for each of them, collecting the results in a list.
2063 * If no code is generated (because the intersection of the inverse schedule
2064 * with the domains turns out to be empty), then an empty list is returned.
2066 * The caller is responsible for ensuring that the basic sets in "domain_list"
2067 * are pair-wise disjoint. It can, however, in principle happen that
2068 * two basic sets should be ordered one way for one value of the outer
2069 * dimensions and the other way for some other value of the outer dimensions.
2070 * We therefore play safe and look for strongly connected components.
2071 * The function add_nodes takes care of handling non-trivial components.
2073 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
2074 __isl_keep isl_basic_set_list
*domain_list
,
2075 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2078 struct isl_add_nodes_data data
;
2082 n
= isl_basic_set_list_n_basic_set(domain_list
);
2086 ctx
= isl_basic_set_list_get_ctx(domain_list
);
2087 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
2091 return add_node(data
.list
, isl_union_map_copy(executed
),
2092 isl_basic_set_list_get_basic_set(domain_list
, 0),
2093 isl_ast_build_copy(build
));
2095 depth
= isl_ast_build_get_depth(build
);
2096 data
.executed
= executed
;
2098 if (depth
< 0 || isl_basic_set_list_foreach_scc(domain_list
,
2099 &domain_follows_at_depth
, &depth
,
2100 &add_nodes
, &data
) < 0)
2101 data
.list
= isl_ast_graft_list_free(data
.list
);
2106 /* Do i and j share any values for the outer dimensions?
2108 static isl_bool
shared_outer(__isl_keep isl_basic_set
*i
,
2109 __isl_keep isl_basic_set
*j
, void *user
)
2111 int depth
= *(int *) user
;
2112 isl_basic_map
*test
;
2116 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
2117 isl_basic_set_copy(j
));
2118 for (l
= 0; l
< depth
; ++l
)
2119 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
2121 empty
= isl_basic_map_is_empty(test
);
2122 isl_basic_map_free(test
);
2124 return isl_bool_not(empty
);
2127 /* Internal data structure for generate_sorted_domains_wrap.
2129 * "n" is the total number of basic sets
2130 * "executed" and "build" are extra arguments to be passed
2131 * to generate_sorted_domains.
2133 * "single" is set to 1 by generate_sorted_domains_wrap if there
2134 * is only a single component.
2135 * "list" collects the results.
2137 struct isl_ast_generate_parallel_domains_data
{
2139 isl_union_map
*executed
;
2140 isl_ast_build
*build
;
2143 isl_ast_graft_list
*list
;
2146 /* Call generate_sorted_domains on "scc", fuse the result into a list
2147 * with either zero or one graft and collect the these single element
2148 * lists into data->list.
2150 * If there is only one component, i.e., if the number of basic sets
2151 * in the current component is equal to the total number of basic sets,
2152 * then data->single is set to 1 and the result of generate_sorted_domains
2155 static isl_stat
generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
2158 struct isl_ast_generate_parallel_domains_data
*data
= user
;
2159 isl_ast_graft_list
*list
;
2162 n
= isl_basic_set_list_n_basic_set(scc
);
2164 scc
= isl_basic_set_list_free(scc
);
2165 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
2166 data
->single
= n
== data
->n
;
2168 list
= isl_ast_graft_list_fuse(list
, data
->build
);
2172 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
2174 isl_basic_set_list_free(scc
);
2176 return isl_stat_error
;
2181 /* Look for any (weakly connected) components in the "domain_list"
2182 * of domains that share some values of the outer dimensions.
2183 * That is, domains in different components do not share any values
2184 * of the outer dimensions. This means that these components
2185 * can be freely reordered.
2186 * Within each of the components, we sort the domains according
2187 * to the execution order at the current depth.
2189 * If there is more than one component, then generate_sorted_domains_wrap
2190 * fuses the result of each call to generate_sorted_domains
2191 * into a list with either zero or one graft and collects these (at most)
2192 * single element lists into a bigger list. This means that the elements of the
2193 * final list can be freely reordered. In particular, we sort them
2194 * according to an arbitrary but fixed ordering to ease merging of
2195 * graft lists from different components.
2197 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2198 __isl_keep isl_basic_set_list
*domain_list
,
2199 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2202 struct isl_ast_generate_parallel_domains_data data
;
2204 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2209 return generate_sorted_domains(domain_list
, executed
, build
);
2211 depth
= isl_ast_build_get_depth(build
);
2215 data
.executed
= executed
;
2218 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2219 &generate_sorted_domains_wrap
,
2221 data
.list
= isl_ast_graft_list_free(data
.list
);
2224 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2229 /* Internal data for separate_domain.
2231 * "explicit" is set if we only want to use explicit bounds.
2233 * "domain" collects the separated domains.
2235 struct isl_separate_domain_data
{
2236 isl_ast_build
*build
;
2241 /* Extract implicit bounds on the current dimension for the executed "map".
2243 * The domain of "map" may involve inner dimensions, so we
2244 * need to eliminate them.
2246 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2247 __isl_keep isl_ast_build
*build
)
2251 domain
= isl_map_domain(map
);
2252 domain
= isl_ast_build_eliminate(build
, domain
);
2257 /* Extract explicit bounds on the current dimension for the executed "map".
2259 * Rather than eliminating the inner dimensions as in implicit_bounds,
2260 * we simply drop any constraints involving those inner dimensions.
2261 * The idea is that most bounds that are implied by constraints on the
2262 * inner dimensions will be enforced by for loops and not by explicit guards.
2263 * There is then no need to separate along those bounds.
2265 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2266 __isl_keep isl_ast_build
*build
)
2272 depth
= isl_ast_build_get_depth(build
);
2273 dim
= isl_map_dim(map
, isl_dim_out
);
2274 if (depth
< 0 || dim
< 0)
2275 return isl_map_domain(isl_map_free(map
));
2276 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2278 domain
= isl_map_domain(map
);
2279 dim
= isl_set_dim(domain
, isl_dim_set
);
2280 domain
= isl_set_detect_equalities(domain
);
2281 domain
= isl_set_drop_constraints_involving_dims(domain
,
2282 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2283 domain
= isl_set_remove_divs_involving_dims(domain
,
2284 isl_dim_set
, depth
, 1);
2285 domain
= isl_set_remove_unknown_divs(domain
);
2290 /* Split data->domain into pieces that intersect with the range of "map"
2291 * and pieces that do not intersect with the range of "map"
2292 * and then add that part of the range of "map" that does not intersect
2293 * with data->domain.
2295 static isl_stat
separate_domain(__isl_take isl_map
*map
, void *user
)
2297 struct isl_separate_domain_data
*data
= user
;
2302 domain
= explicit_bounds(map
, data
->build
);
2304 domain
= implicit_bounds(map
, data
->build
);
2306 domain
= isl_set_coalesce(domain
);
2307 domain
= isl_set_make_disjoint(domain
);
2308 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2309 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2310 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2311 data
->domain
= isl_set_union(data
->domain
, d1
);
2312 data
->domain
= isl_set_union(data
->domain
, d2
);
2317 /* Separate the schedule domains of "executed".
2319 * That is, break up the domain of "executed" into basic sets,
2320 * such that for each basic set S, every element in S is associated with
2321 * the same domain spaces.
2323 * "space" is the (single) domain space of "executed".
2325 static __isl_give isl_set
*separate_schedule_domains(
2326 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2327 __isl_keep isl_ast_build
*build
)
2329 struct isl_separate_domain_data data
= { build
};
2332 ctx
= isl_ast_build_get_ctx(build
);
2333 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2334 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2335 data
.domain
= isl_set_empty(space
);
2336 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2337 data
.domain
= isl_set_free(data
.domain
);
2339 isl_union_map_free(executed
);
2343 /* Temporary data used during the search for a lower bound for unrolling.
2345 * "build" is the build in which the unrolling will be performed
2346 * "domain" is the original set for which to find a lower bound
2347 * "depth" is the dimension for which to find a lower boudn
2348 * "expansion" is the expansion that needs to be applied to "domain"
2349 * in the unrolling that will be performed
2351 * "lower" is the best lower bound found so far. It is NULL if we have not
2353 * "n" is the corresponding size. If lower is NULL, then the value of n
2355 * "n_div" is the maximal number of integer divisions in the first
2356 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2357 * been computed yet.
2359 struct isl_find_unroll_data
{
2360 isl_ast_build
*build
;
2363 isl_basic_map
*expansion
;
2370 /* Return the constraint
2372 * i_"depth" = aff + offset
2374 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2377 aff
= isl_aff_copy(aff
);
2378 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2379 aff
= isl_aff_add_constant_si(aff
, offset
);
2380 return isl_equality_from_aff(aff
);
2383 /* Update *user to the number of integer divisions in the first element
2384 * of "ma", if it is larger than the current value.
2386 static isl_stat
update_n_div(__isl_take isl_set
*set
,
2387 __isl_take isl_multi_aff
*ma
, void *user
)
2393 aff
= isl_multi_aff_get_aff(ma
, 0);
2394 n_div
= isl_aff_dim(aff
, isl_dim_div
);
2396 isl_multi_aff_free(ma
);
2402 return n_div
>= 0 ? isl_stat_ok
: isl_stat_error
;
2405 /* Get the number of integer divisions in the expression for the iterator
2406 * value at the first slice in the unrolling based on lower bound "lower",
2407 * taking into account the expansion that needs to be performed on this slice.
2409 static int get_expanded_n_div(struct isl_find_unroll_data
*data
,
2410 __isl_keep isl_aff
*lower
)
2414 isl_map
*it_map
, *expansion
;
2415 isl_pw_multi_aff
*pma
;
2418 c
= at_offset(data
->depth
, lower
, 0);
2419 set
= isl_set_copy(data
->domain
);
2420 set
= isl_set_add_constraint(set
, c
);
2421 expansion
= isl_map_from_basic_map(isl_basic_map_copy(data
->expansion
));
2422 set
= isl_set_apply(set
, expansion
);
2423 it_map
= isl_ast_build_map_to_iterator(data
->build
, set
);
2424 pma
= isl_pw_multi_aff_from_map(it_map
);
2426 if (isl_pw_multi_aff_foreach_piece(pma
, &update_n_div
, &n
) < 0)
2428 isl_pw_multi_aff_free(pma
);
2433 /* Is the lower bound "lower" with corresponding iteration count "n"
2434 * better than the one stored in "data"?
2435 * If there is no upper bound on the iteration count ("n" is infinity) or
2436 * if the count is too large, then we cannot use this lower bound.
2437 * Otherwise, if there was no previous lower bound or
2438 * if the iteration count of the new lower bound is smaller than
2439 * the iteration count of the previous lower bound, then we consider
2440 * the new lower bound to be better.
2441 * If the iteration count is the same, then compare the number
2442 * of integer divisions that would be needed to express
2443 * the iterator value at the first slice in the unrolling
2444 * according to the lower bound. If we end up computing this
2445 * number, then store the lowest value in data->n_div.
2447 static int is_better_lower_bound(struct isl_find_unroll_data
*data
,
2448 __isl_keep isl_aff
*lower
, __isl_keep isl_val
*n
)
2455 if (isl_val_is_infty(n
))
2457 if (isl_val_cmp_si(n
, INT_MAX
) > 0)
2461 cmp
= isl_val_cmp_si(n
, *data
->n
);
2466 if (data
->n_div
< 0)
2467 data
->n_div
= get_expanded_n_div(data
, data
->lower
);
2468 if (data
->n_div
< 0)
2470 if (data
->n_div
== 0)
2472 n_div
= get_expanded_n_div(data
, lower
);
2475 if (n_div
>= data
->n_div
)
2477 data
->n_div
= n_div
;
2482 /* Check if we can use "c" as a lower bound and if it is better than
2483 * any previously found lower bound.
2485 * If "c" does not involve the dimension at the current depth,
2486 * then we cannot use it.
2487 * Otherwise, let "c" be of the form
2491 * We compute the maximal value of
2493 * -ceil(f(j)/a)) + i + 1
2495 * over the domain. If there is such a value "n", then we know
2497 * -ceil(f(j)/a)) + i + 1 <= n
2501 * i < ceil(f(j)/a)) + n
2503 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2504 * We just need to check if we have found any lower bound before and
2505 * if the new lower bound is better (smaller n or fewer integer divisions)
2506 * than the previously found lower bounds.
2508 static isl_stat
update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2509 __isl_keep isl_constraint
*c
)
2511 isl_aff
*aff
, *lower
;
2515 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2518 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2519 lower
= isl_aff_ceil(lower
);
2520 aff
= isl_aff_copy(lower
);
2521 aff
= isl_aff_neg(aff
);
2522 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2523 aff
= isl_aff_add_constant_si(aff
, 1);
2524 max
= isl_set_max_val(data
->domain
, aff
);
2527 better
= is_better_lower_bound(data
, lower
, max
);
2528 if (better
< 0 || !better
) {
2530 isl_aff_free(lower
);
2531 return better
< 0 ? isl_stat_error
: isl_stat_ok
;
2534 isl_aff_free(data
->lower
);
2535 data
->lower
= lower
;
2536 *data
->n
= isl_val_get_num_si(max
);
2542 /* Check if we can use "c" as a lower bound and if it is better than
2543 * any previously found lower bound.
2545 static isl_stat
constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2547 struct isl_find_unroll_data
*data
;
2550 data
= (struct isl_find_unroll_data
*) user
;
2551 r
= update_unrolling_lower_bound(data
, c
);
2552 isl_constraint_free(c
);
2557 /* Look for a lower bound l(i) on the dimension at "depth"
2558 * and a size n such that "domain" is a subset of
2560 * { [i] : l(i) <= i_d < l(i) + n }
2562 * where d is "depth" and l(i) depends only on earlier dimensions.
2563 * Furthermore, try and find a lower bound such that n is as small as possible.
2564 * In particular, "n" needs to be finite.
2565 * "build" is the build in which the unrolling will be performed.
2566 * "expansion" is the expansion that needs to be applied to "domain"
2567 * in the unrolling that will be performed.
2569 * Inner dimensions have been eliminated from "domain" by the caller.
2571 * We first construct a collection of lower bounds on the input set
2572 * by computing its simple hull. We then iterate through them,
2573 * discarding those that we cannot use (either because they do not
2574 * involve the dimension at "depth" or because they have no corresponding
2575 * upper bound, meaning that "n" would be unbounded) and pick out the
2576 * best from the remaining ones.
2578 * If we cannot find a suitable lower bound, then we consider that
2581 static __isl_give isl_aff
*find_unroll_lower_bound(
2582 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*domain
,
2583 int depth
, __isl_keep isl_basic_map
*expansion
, int *n
)
2585 struct isl_find_unroll_data data
=
2586 { build
, domain
, depth
, expansion
, NULL
, n
, -1 };
2587 isl_basic_set
*hull
;
2589 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2591 if (isl_basic_set_foreach_constraint(hull
,
2592 &constraint_find_unroll
, &data
) < 0)
2595 isl_basic_set_free(hull
);
2598 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2599 "cannot find lower bound for unrolling", return NULL
);
2603 isl_basic_set_free(hull
);
2604 return isl_aff_free(data
.lower
);
2607 /* Call "fn" on each iteration of the current dimension of "domain".
2608 * If "init" is not NULL, then it is called with the number of
2609 * iterations before any call to "fn".
2610 * Return -1 on failure.
2612 * Since we are going to be iterating over the individual values,
2613 * we first check if there are any strides on the current dimension.
2614 * If there is, we rewrite the current dimension i as
2616 * i = stride i' + offset
2618 * and then iterate over individual values of i' instead.
2620 * We then look for a lower bound on i' and a size such that the domain
2623 * { [j,i'] : l(j) <= i' < l(j) + n }
2625 * and then take slices of the domain at values of i'
2626 * between l(j) and l(j) + n - 1.
2628 * We compute the unshifted simple hull of each slice to ensure that
2629 * we have a single basic set per offset. The slicing constraint
2630 * may get simplified away before the unshifted simple hull is taken
2631 * and may therefore in some rare cases disappear from the result.
2632 * We therefore explicitly add the constraint back after computing
2633 * the unshifted simple hull to ensure that the basic sets
2634 * remain disjoint. The constraints that are dropped by taking the hull
2635 * will be taken into account at the next level, as in the case of the
2638 * Finally, we map i' back to i and call "fn".
2640 static int foreach_iteration(__isl_take isl_set
*domain
,
2641 __isl_keep isl_ast_build
*build
, int (*init
)(int n
, void *user
),
2642 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
2647 isl_multi_aff
*expansion
;
2648 isl_basic_map
*bmap
;
2649 isl_aff
*lower
= NULL
;
2650 isl_ast_build
*stride_build
;
2652 depth
= isl_ast_build_get_depth(build
);
2654 domain
= isl_set_free(domain
);
2656 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2657 domain
= isl_set_intersect(domain
, isl_ast_build_get_domain(build
));
2658 stride_build
= isl_ast_build_copy(build
);
2659 stride_build
= isl_ast_build_detect_strides(stride_build
,
2660 isl_set_copy(domain
));
2661 expansion
= isl_ast_build_get_stride_expansion(stride_build
);
2663 domain
= isl_set_preimage_multi_aff(domain
,
2664 isl_multi_aff_copy(expansion
));
2665 domain
= isl_ast_build_eliminate_divs(stride_build
, domain
);
2666 isl_ast_build_free(stride_build
);
2668 bmap
= isl_basic_map_from_multi_aff(expansion
);
2670 empty
= isl_set_is_empty(domain
);
2676 lower
= find_unroll_lower_bound(build
, domain
, depth
, bmap
, &n
);
2680 if (n
>= 0 && init
&& init(n
, user
) < 0)
2682 for (i
= 0; i
< n
; ++i
) {
2684 isl_basic_set
*bset
;
2685 isl_constraint
*slice
;
2687 slice
= at_offset(depth
, lower
, i
);
2688 set
= isl_set_copy(domain
);
2689 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2690 bset
= isl_set_unshifted_simple_hull(set
);
2691 bset
= isl_basic_set_add_constraint(bset
, slice
);
2692 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2694 if (fn(bset
, user
) < 0)
2698 isl_aff_free(lower
);
2699 isl_set_free(domain
);
2700 isl_basic_map_free(bmap
);
2702 return n
< 0 || i
< n
? -1 : 0;
2705 /* Data structure for storing the results and the intermediate objects
2706 * of compute_domains.
2708 * "list" is the main result of the function and contains a list
2709 * of disjoint basic sets for which code should be generated.
2711 * "executed" and "build" are inputs to compute_domains.
2712 * "schedule_domain" is the domain of "executed".
2714 * "option" contains the domains at the current depth that should by
2715 * atomic, separated or unrolled. These domains are as specified by
2716 * the user, except that inner dimensions have been eliminated and
2717 * that they have been made pair-wise disjoint.
2719 * "sep_class" contains the user-specified split into separation classes
2720 * specialized to the current depth.
2721 * "done" contains the union of the separation domains that have already
2724 struct isl_codegen_domains
{
2725 isl_basic_set_list
*list
;
2727 isl_union_map
*executed
;
2728 isl_ast_build
*build
;
2729 isl_set
*schedule_domain
;
2737 /* Internal data structure for do_unroll.
2739 * "domains" stores the results of compute_domains.
2740 * "class_domain" is the original class domain passed to do_unroll.
2741 * "unroll_domain" collects the unrolled iterations.
2743 struct isl_ast_unroll_data
{
2744 struct isl_codegen_domains
*domains
;
2745 isl_set
*class_domain
;
2746 isl_set
*unroll_domain
;
2749 /* Given an iteration of an unrolled domain represented by "bset",
2750 * add it to data->domains->list.
2751 * Since we may have dropped some constraints, we intersect with
2752 * the class domain again to ensure that each element in the list
2753 * is disjoint from the other class domains.
2755 static int do_unroll_iteration(__isl_take isl_basic_set
*bset
, void *user
)
2757 struct isl_ast_unroll_data
*data
= user
;
2759 isl_basic_set_list
*list
;
2761 set
= isl_set_from_basic_set(bset
);
2762 data
->unroll_domain
= isl_set_union(data
->unroll_domain
,
2764 set
= isl_set_intersect(set
, isl_set_copy(data
->class_domain
));
2765 set
= isl_set_make_disjoint(set
);
2766 list
= isl_basic_set_list_from_set(set
);
2767 data
->domains
->list
= isl_basic_set_list_concat(data
->domains
->list
,
2773 /* Extend domains->list with a list of basic sets, one for each value
2774 * of the current dimension in "domain" and remove the corresponding
2775 * sets from the class domain. Return the updated class domain.
2776 * The divs that involve the current dimension have not been projected out
2779 * We call foreach_iteration to iterate over the individual values and
2780 * in do_unroll_iteration we collect the individual basic sets in
2781 * domains->list and their union in data->unroll_domain, which is then
2782 * used to update the class domain.
2784 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2785 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2787 struct isl_ast_unroll_data data
;
2790 return isl_set_free(class_domain
);
2792 return isl_set_free(domain
);
2794 data
.domains
= domains
;
2795 data
.class_domain
= class_domain
;
2796 data
.unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2798 if (foreach_iteration(domain
, domains
->build
, NULL
,
2799 &do_unroll_iteration
, &data
) < 0)
2800 data
.unroll_domain
= isl_set_free(data
.unroll_domain
);
2802 class_domain
= isl_set_subtract(class_domain
, data
.unroll_domain
);
2804 return class_domain
;
2807 /* Add domains to domains->list for each individual value of the current
2808 * dimension, for that part of the schedule domain that lies in the
2809 * intersection of the option domain and the class domain.
2810 * Remove the corresponding sets from the class domain and
2811 * return the updated class domain.
2813 * We first break up the unroll option domain into individual pieces
2814 * and then handle each of them separately. The unroll option domain
2815 * has been made disjoint in compute_domains_init_options,
2817 * Note that we actively want to combine different pieces of the
2818 * schedule domain that have the same value at the current dimension.
2819 * We therefore need to break up the unroll option domain before
2820 * intersecting with class and schedule domain, hoping that the
2821 * unroll option domain specified by the user is relatively simple.
2823 static __isl_give isl_set
*compute_unroll_domains(
2824 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2826 isl_set
*unroll_domain
;
2827 isl_basic_set_list
*unroll_list
;
2832 empty
= isl_set_is_empty(domains
->option
[isl_ast_loop_unroll
]);
2834 return isl_set_free(class_domain
);
2836 return class_domain
;
2838 unroll_domain
= isl_set_copy(domains
->option
[isl_ast_loop_unroll
]);
2839 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2841 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2843 class_domain
= isl_set_free(class_domain
);
2844 for (i
= 0; i
< n
; ++i
) {
2845 isl_basic_set
*bset
;
2847 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2848 unroll_domain
= isl_set_from_basic_set(bset
);
2849 unroll_domain
= isl_set_intersect(unroll_domain
,
2850 isl_set_copy(class_domain
));
2851 unroll_domain
= isl_set_intersect(unroll_domain
,
2852 isl_set_copy(domains
->schedule_domain
));
2854 empty
= isl_set_is_empty(unroll_domain
);
2855 if (empty
>= 0 && empty
) {
2856 isl_set_free(unroll_domain
);
2860 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2863 isl_basic_set_list_free(unroll_list
);
2865 return class_domain
;
2868 /* Try and construct a single basic set that includes the intersection of
2869 * the schedule domain, the atomic option domain and the class domain.
2870 * Add the resulting basic set(s) to domains->list and remove them
2871 * from class_domain. Return the updated class domain.
2873 * We construct a single domain rather than trying to combine
2874 * the schedule domains of individual domains because we are working
2875 * within a single component so that non-overlapping schedule domains
2876 * should already have been separated.
2877 * We do however need to make sure that this single domains is a subset
2878 * of the class domain so that it would not intersect with any other
2879 * class domains. This means that we may end up splitting up the atomic
2880 * domain in case separation classes are being used.
2882 * "domain" is the intersection of the schedule domain and the class domain,
2883 * with inner dimensions projected out.
2885 static __isl_give isl_set
*compute_atomic_domain(
2886 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2888 isl_basic_set
*bset
;
2889 isl_basic_set_list
*list
;
2890 isl_set
*domain
, *atomic_domain
;
2893 domain
= isl_set_copy(domains
->option
[isl_ast_loop_atomic
]);
2894 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2895 domain
= isl_set_intersect(domain
,
2896 isl_set_copy(domains
->schedule_domain
));
2897 empty
= isl_set_is_empty(domain
);
2899 class_domain
= isl_set_free(class_domain
);
2901 isl_set_free(domain
);
2902 return class_domain
;
2905 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2906 domain
= isl_set_coalesce_preserve(domain
);
2907 bset
= isl_set_unshifted_simple_hull(domain
);
2908 domain
= isl_set_from_basic_set(bset
);
2909 atomic_domain
= isl_set_copy(domain
);
2910 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2911 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2912 domain
= isl_set_make_disjoint(domain
);
2913 list
= isl_basic_set_list_from_set(domain
);
2914 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2916 return class_domain
;
2919 /* Split up the schedule domain into uniform basic sets,
2920 * in the sense that each element in a basic set is associated to
2921 * elements of the same domains, and add the result to domains->list.
2922 * Do this for that part of the schedule domain that lies in the
2923 * intersection of "class_domain" and the separate option domain.
2925 * "class_domain" may or may not include the constraints
2926 * of the schedule domain, but this does not make a difference
2927 * since we are going to intersect it with the domain of the inverse schedule.
2928 * If it includes schedule domain constraints, then they may involve
2929 * inner dimensions, but we will eliminate them in separation_domain.
2931 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2932 __isl_keep isl_set
*class_domain
)
2936 isl_union_map
*executed
;
2937 isl_basic_set_list
*list
;
2940 domain
= isl_set_copy(domains
->option
[isl_ast_loop_separate
]);
2941 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2942 executed
= isl_union_map_copy(domains
->executed
);
2943 executed
= isl_union_map_intersect_domain(executed
,
2944 isl_union_set_from_set(domain
));
2945 empty
= isl_union_map_is_empty(executed
);
2946 if (empty
< 0 || empty
) {
2947 isl_union_map_free(executed
);
2948 return empty
< 0 ? -1 : 0;
2951 space
= isl_set_get_space(class_domain
);
2952 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2954 list
= isl_basic_set_list_from_set(domain
);
2955 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2960 /* Split up the domain at the current depth into disjoint
2961 * basic sets for which code should be generated separately
2962 * for the given separation class domain.
2964 * If any separation classes have been defined, then "class_domain"
2965 * is the domain of the current class and does not refer to inner dimensions.
2966 * Otherwise, "class_domain" is the universe domain.
2968 * We first make sure that the class domain is disjoint from
2969 * previously considered class domains.
2971 * The separate domains can be computed directly from the "class_domain".
2973 * The unroll, atomic and remainder domains need the constraints
2974 * from the schedule domain.
2976 * For unrolling, the actual schedule domain is needed (with divs that
2977 * may refer to the current dimension) so that stride detection can be
2980 * For atomic and remainder domains, inner dimensions and divs involving
2981 * the current dimensions should be eliminated.
2982 * In case we are working within a separation class, we need to intersect
2983 * the result with the current "class_domain" to ensure that the domains
2984 * are disjoint from those generated from other class domains.
2986 * The domain that has been made atomic may be larger than specified
2987 * by the user since it needs to be representable as a single basic set.
2988 * This possibly larger domain is removed from class_domain by
2989 * compute_atomic_domain. It is computed first so that the extended domain
2990 * would not overlap with any domains computed before.
2991 * Similary, the unrolled domains may have some constraints removed and
2992 * may therefore also be larger than specified by the user.
2994 * If anything is left after handling separate, unroll and atomic,
2995 * we split it up into basic sets and append the basic sets to domains->list.
2997 static isl_stat
compute_partial_domains(struct isl_codegen_domains
*domains
,
2998 __isl_take isl_set
*class_domain
)
3000 isl_basic_set_list
*list
;
3003 class_domain
= isl_set_subtract(class_domain
,
3004 isl_set_copy(domains
->done
));
3005 domains
->done
= isl_set_union(domains
->done
,
3006 isl_set_copy(class_domain
));
3008 class_domain
= compute_atomic_domain(domains
, class_domain
);
3009 class_domain
= compute_unroll_domains(domains
, class_domain
);
3011 domain
= isl_set_copy(class_domain
);
3013 if (compute_separate_domain(domains
, domain
) < 0)
3015 domain
= isl_set_subtract(domain
,
3016 isl_set_copy(domains
->option
[isl_ast_loop_separate
]));
3018 domain
= isl_set_intersect(domain
,
3019 isl_set_copy(domains
->schedule_domain
));
3021 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
3022 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
3024 domain
= isl_set_coalesce_preserve(domain
);
3025 domain
= isl_set_make_disjoint(domain
);
3027 list
= isl_basic_set_list_from_set(domain
);
3028 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
3030 isl_set_free(class_domain
);
3034 isl_set_free(domain
);
3035 isl_set_free(class_domain
);
3036 return isl_stat_error
;
3039 /* Split up the domain at the current depth into disjoint
3040 * basic sets for which code should be generated separately
3041 * for the separation class identified by "pnt".
3043 * We extract the corresponding class domain from domains->sep_class,
3044 * eliminate inner dimensions and pass control to compute_partial_domains.
3046 static isl_stat
compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
3048 struct isl_codegen_domains
*domains
= user
;
3053 class_set
= isl_set_from_point(pnt
);
3054 domain
= isl_map_domain(isl_map_intersect_range(
3055 isl_map_copy(domains
->sep_class
), class_set
));
3056 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
3057 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
3059 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
3061 return isl_stat_error
;
3063 isl_set_free(domain
);
3067 return compute_partial_domains(domains
, domain
);
3070 /* Extract the domains at the current depth that should be atomic,
3071 * separated or unrolled and store them in option.
3073 * The domains specified by the user might overlap, so we make
3074 * them disjoint by subtracting earlier domains from later domains.
3076 static void compute_domains_init_options(isl_set
*option
[4],
3077 __isl_keep isl_ast_build
*build
)
3079 enum isl_ast_loop_type type
, type2
;
3082 for (type
= isl_ast_loop_atomic
;
3083 type
<= isl_ast_loop_separate
; ++type
) {
3084 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
3085 for (type2
= isl_ast_loop_atomic
; type2
< type
; ++type2
)
3086 option
[type
] = isl_set_subtract(option
[type
],
3087 isl_set_copy(option
[type2
]));
3090 unroll
= option
[isl_ast_loop_unroll
];
3091 unroll
= isl_set_coalesce(unroll
);
3092 unroll
= isl_set_make_disjoint(unroll
);
3093 option
[isl_ast_loop_unroll
] = unroll
;
3096 /* Split up the domain at the current depth into disjoint
3097 * basic sets for which code should be generated separately,
3098 * based on the user-specified options.
3099 * Return the list of disjoint basic sets.
3101 * There are three kinds of domains that we need to keep track of.
3102 * - the "schedule domain" is the domain of "executed"
3103 * - the "class domain" is the domain corresponding to the currrent
3105 * - the "option domain" is the domain corresponding to one of the options
3106 * atomic, unroll or separate
3108 * We first consider the individial values of the separation classes
3109 * and split up the domain for each of them separately.
3110 * Finally, we consider the remainder. If no separation classes were
3111 * specified, then we call compute_partial_domains with the universe
3112 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3113 * with inner dimensions removed. We do this because we want to
3114 * avoid computing the complement of the class domains (i.e., the difference
3115 * between the universe and domains->done).
3117 static __isl_give isl_basic_set_list
*compute_domains(
3118 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
3120 struct isl_codegen_domains domains
;
3123 isl_union_set
*schedule_domain
;
3127 enum isl_ast_loop_type type
;
3133 ctx
= isl_union_map_get_ctx(executed
);
3134 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
3136 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3137 domain
= isl_set_from_union_set(schedule_domain
);
3139 compute_domains_init_options(domains
.option
, build
);
3141 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
3142 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
3143 n_param
= isl_set_dim(classes
, isl_dim_param
);
3145 classes
= isl_set_free(classes
);
3146 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
3148 space
= isl_set_get_space(domain
);
3149 domains
.build
= build
;
3150 domains
.schedule_domain
= isl_set_copy(domain
);
3151 domains
.executed
= executed
;
3152 domains
.done
= isl_set_empty(space
);
3154 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
3155 domains
.list
= isl_basic_set_list_free(domains
.list
);
3156 isl_set_free(classes
);
3158 empty
= isl_set_is_empty(domains
.done
);
3160 domains
.list
= isl_basic_set_list_free(domains
.list
);
3161 domain
= isl_set_free(domain
);
3163 isl_set_free(domain
);
3164 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
3166 domain
= isl_ast_build_eliminate(build
, domain
);
3168 if (compute_partial_domains(&domains
, domain
) < 0)
3169 domains
.list
= isl_basic_set_list_free(domains
.list
);
3171 isl_set_free(domains
.schedule_domain
);
3172 isl_set_free(domains
.done
);
3173 isl_map_free(domains
.sep_class
);
3174 for (type
= isl_ast_loop_atomic
; type
<= isl_ast_loop_separate
; ++type
)
3175 isl_set_free(domains
.option
[type
]);
3177 return domains
.list
;
3180 /* Generate code for a single component, after shifting (if any)
3181 * has been applied, in case the schedule was specified as a union map.
3183 * We first split up the domain at the current depth into disjoint
3184 * basic sets based on the user-specified options.
3185 * Then we generated code for each of them and concatenate the results.
3187 static __isl_give isl_ast_graft_list
*generate_shifted_component_flat(
3188 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3190 isl_basic_set_list
*domain_list
;
3191 isl_ast_graft_list
*list
= NULL
;
3193 domain_list
= compute_domains(executed
, build
);
3194 list
= generate_parallel_domains(domain_list
, executed
, build
);
3196 isl_basic_set_list_free(domain_list
);
3197 isl_union_map_free(executed
);
3198 isl_ast_build_free(build
);
3203 /* Generate code for a single component, after shifting (if any)
3204 * has been applied, in case the schedule was specified as a schedule tree
3205 * and the separate option was specified.
3207 * We perform separation on the domain of "executed" and then generate
3208 * an AST for each of the resulting disjoint basic sets.
3210 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_separate(
3211 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3215 isl_basic_set_list
*domain_list
;
3216 isl_ast_graft_list
*list
;
3218 space
= isl_ast_build_get_space(build
, 1);
3219 domain
= separate_schedule_domains(space
,
3220 isl_union_map_copy(executed
), build
);
3221 domain_list
= isl_basic_set_list_from_set(domain
);
3223 list
= generate_parallel_domains(domain_list
, executed
, build
);
3225 isl_basic_set_list_free(domain_list
);
3226 isl_union_map_free(executed
);
3227 isl_ast_build_free(build
);
3232 /* Internal data structure for generate_shifted_component_tree_unroll.
3234 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3235 * "list" collects the constructs grafts.
3237 struct isl_ast_unroll_tree_data
{
3238 isl_union_map
*executed
;
3239 isl_ast_build
*build
;
3240 isl_ast_graft_list
*list
;
3243 /* Initialize data->list to a list of "n" elements.
3245 static int init_unroll_tree(int n
, void *user
)
3247 struct isl_ast_unroll_tree_data
*data
= user
;
3250 ctx
= isl_ast_build_get_ctx(data
->build
);
3251 data
->list
= isl_ast_graft_list_alloc(ctx
, n
);
3256 /* Given an iteration of an unrolled domain represented by "bset",
3257 * generate the corresponding AST and add the result to data->list.
3259 static int do_unroll_tree_iteration(__isl_take isl_basic_set
*bset
, void *user
)
3261 struct isl_ast_unroll_tree_data
*data
= user
;
3263 data
->list
= add_node(data
->list
, isl_union_map_copy(data
->executed
),
3264 bset
, isl_ast_build_copy(data
->build
));
3269 /* Generate code for a single component, after shifting (if any)
3270 * has been applied, in case the schedule was specified as a schedule tree
3271 * and the unroll option was specified.
3273 * We call foreach_iteration to iterate over the individual values and
3274 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3276 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_unroll(
3277 __isl_take isl_union_map
*executed
, __isl_take isl_set
*domain
,
3278 __isl_take isl_ast_build
*build
)
3280 struct isl_ast_unroll_tree_data data
= { executed
, build
, NULL
};
3282 if (foreach_iteration(domain
, build
, &init_unroll_tree
,
3283 &do_unroll_tree_iteration
, &data
) < 0)
3284 data
.list
= isl_ast_graft_list_free(data
.list
);
3286 isl_union_map_free(executed
);
3287 isl_ast_build_free(build
);
3292 /* Does "domain" involve a disjunction that is purely based on
3293 * constraints involving only outer dimension?
3295 * In particular, is there a disjunction such that the constraints
3296 * involving the current and later dimensions are the same over
3297 * all the disjuncts?
3299 static isl_bool
has_pure_outer_disjunction(__isl_keep isl_set
*domain
,
3300 __isl_keep isl_ast_build
*build
)
3302 isl_basic_set
*hull
;
3303 isl_set
*shared
, *inner
;
3309 n
= isl_set_n_basic_set(domain
);
3311 return isl_bool_error
;
3313 return isl_bool_false
;
3314 dim
= isl_set_dim(domain
, isl_dim_set
);
3315 depth
= isl_ast_build_get_depth(build
);
3316 if (dim
< 0 || depth
< 0)
3317 return isl_bool_error
;
3319 inner
= isl_set_copy(domain
);
3320 inner
= isl_set_drop_constraints_not_involving_dims(inner
,
3321 isl_dim_set
, depth
, dim
- depth
);
3322 hull
= isl_set_plain_unshifted_simple_hull(isl_set_copy(inner
));
3323 shared
= isl_set_from_basic_set(hull
);
3324 equal
= isl_set_plain_is_equal(inner
, shared
);
3325 isl_set_free(inner
);
3326 isl_set_free(shared
);
3331 /* Generate code for a single component, after shifting (if any)
3332 * has been applied, in case the schedule was specified as a schedule tree.
3333 * In particular, handle the base case where there is either no isolated
3334 * set or we are within the isolated set (in which case "isolated" is set)
3335 * or the iterations that precede or follow the isolated set.
3337 * The schedule domain is broken up or combined into basic sets
3338 * according to the AST generation option specified in the current
3339 * schedule node, which may be either atomic, separate, unroll or
3340 * unspecified. If the option is unspecified, then we currently simply
3341 * split the schedule domain into disjoint basic sets.
3343 * In case the separate option is specified, the AST generation is
3344 * handled by generate_shifted_component_tree_separate.
3345 * In the other cases, we need the global schedule domain.
3346 * In the unroll case, the AST generation is then handled by
3347 * generate_shifted_component_tree_unroll which needs the actual
3348 * schedule domain (with divs that may refer to the current dimension)
3349 * so that stride detection can be performed.
3350 * In the atomic or unspecified case, inner dimensions and divs involving
3351 * the current dimensions should be eliminated.
3352 * The result is then either combined into a single basic set or
3353 * split up into disjoint basic sets.
3354 * Finally an AST is generated for each basic set and the results are
3357 * If the schedule domain involves a disjunction that is purely based on
3358 * constraints involving only outer dimension, then it is treated as
3359 * if atomic was specified. This ensures that only a single loop
3360 * is generated instead of a sequence of identical loops with
3363 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_base(
3364 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3367 isl_bool outer_disjunction
;
3368 isl_union_set
*schedule_domain
;
3370 isl_basic_set_list
*domain_list
;
3371 isl_ast_graft_list
*list
;
3372 enum isl_ast_loop_type type
;
3374 type
= isl_ast_build_get_loop_type(build
, isolated
);
3378 if (type
== isl_ast_loop_separate
)
3379 return generate_shifted_component_tree_separate(executed
,
3382 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3383 domain
= isl_set_from_union_set(schedule_domain
);
3385 if (type
== isl_ast_loop_unroll
)
3386 return generate_shifted_component_tree_unroll(executed
, domain
,
3389 domain
= isl_ast_build_eliminate(build
, domain
);
3390 domain
= isl_set_coalesce_preserve(domain
);
3392 outer_disjunction
= has_pure_outer_disjunction(domain
, build
);
3393 if (outer_disjunction
< 0)
3394 domain
= isl_set_free(domain
);
3396 if (outer_disjunction
|| type
== isl_ast_loop_atomic
) {
3397 isl_basic_set
*hull
;
3398 hull
= isl_set_unshifted_simple_hull(domain
);
3399 domain_list
= isl_basic_set_list_from_basic_set(hull
);
3401 domain
= isl_set_make_disjoint(domain
);
3402 domain_list
= isl_basic_set_list_from_set(domain
);
3405 list
= generate_parallel_domains(domain_list
, executed
, build
);
3407 isl_basic_set_list_free(domain_list
);
3408 isl_union_map_free(executed
);
3409 isl_ast_build_free(build
);
3413 isl_union_map_free(executed
);
3414 isl_ast_build_free(build
);
3418 /* Extract out the disjunction imposed by "domain" on the outer
3419 * schedule dimensions.
3421 * In particular, remove all inner dimensions from "domain" (including
3422 * the current dimension) and then remove the constraints that are shared
3423 * by all disjuncts in the result.
3425 static __isl_give isl_set
*extract_disjunction(__isl_take isl_set
*domain
,
3426 __isl_keep isl_ast_build
*build
)
3432 domain
= isl_ast_build_specialize(build
, domain
);
3433 depth
= isl_ast_build_get_depth(build
);
3434 dim
= isl_set_dim(domain
, isl_dim_set
);
3435 if (depth
< 0 || dim
< 0)
3436 return isl_set_free(domain
);
3437 domain
= isl_set_eliminate(domain
, isl_dim_set
, depth
, dim
- depth
);
3438 domain
= isl_set_remove_unknown_divs(domain
);
3439 hull
= isl_set_copy(domain
);
3440 hull
= isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull
));
3441 domain
= isl_set_gist(domain
, hull
);
3446 /* Add "guard" to the grafts in "list".
3447 * "build" is the outer AST build, while "sub_build" includes "guard"
3448 * in its generated domain.
3450 * First combine the grafts into a single graft and then add the guard.
3451 * If the list is empty, or if some error occurred, then simply return
3454 static __isl_give isl_ast_graft_list
*list_add_guard(
3455 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*guard
,
3456 __isl_keep isl_ast_build
*build
, __isl_keep isl_ast_build
*sub_build
)
3458 isl_ast_graft
*graft
;
3461 list
= isl_ast_graft_list_fuse(list
, sub_build
);
3463 n
= isl_ast_graft_list_n_ast_graft(list
);
3465 return isl_ast_graft_list_free(list
);
3469 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
3470 graft
= isl_ast_graft_add_guard(graft
, isl_set_copy(guard
), build
);
3471 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
3476 /* Generate code for a single component, after shifting (if any)
3477 * has been applied, in case the schedule was specified as a schedule tree.
3478 * In particular, do so for the specified subset of the schedule domain.
3480 * If we are outside of the isolated part, then "domain" may include
3481 * a disjunction. Explicitly generate this disjunction at this point
3482 * instead of relying on the disjunction getting hoisted back up
3485 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_part(
3486 __isl_keep isl_union_map
*executed
, __isl_take isl_set
*domain
,
3487 __isl_keep isl_ast_build
*build
, int isolated
)
3489 isl_union_set
*uset
;
3490 isl_ast_graft_list
*list
;
3491 isl_ast_build
*sub_build
;
3494 uset
= isl_union_set_from_set(isl_set_copy(domain
));
3495 executed
= isl_union_map_copy(executed
);
3496 executed
= isl_union_map_intersect_domain(executed
, uset
);
3497 empty
= isl_union_map_is_empty(executed
);
3502 isl_union_map_free(executed
);
3503 isl_set_free(domain
);
3504 ctx
= isl_ast_build_get_ctx(build
);
3505 return isl_ast_graft_list_alloc(ctx
, 0);
3508 sub_build
= isl_ast_build_copy(build
);
3510 domain
= extract_disjunction(domain
, build
);
3511 sub_build
= isl_ast_build_restrict_generated(sub_build
,
3512 isl_set_copy(domain
));
3514 list
= generate_shifted_component_tree_base(executed
,
3515 isl_ast_build_copy(sub_build
), isolated
);
3517 list
= list_add_guard(list
, domain
, build
, sub_build
);
3518 isl_ast_build_free(sub_build
);
3519 isl_set_free(domain
);
3522 isl_union_map_free(executed
);
3523 isl_set_free(domain
);
3527 /* Generate code for a single component, after shifting (if any)
3528 * has been applied, in case the schedule was specified as a schedule tree.
3529 * In particular, do so for the specified sequence of subsets
3530 * of the schedule domain, "before", "isolated", "after" and "other",
3531 * where only the "isolated" part is considered to be isolated.
3533 static __isl_give isl_ast_graft_list
*generate_shifted_component_parts(
3534 __isl_take isl_union_map
*executed
, __isl_take isl_set
*before
,
3535 __isl_take isl_set
*isolated
, __isl_take isl_set
*after
,
3536 __isl_take isl_set
*other
, __isl_take isl_ast_build
*build
)
3538 isl_ast_graft_list
*list
, *res
;
3540 res
= generate_shifted_component_tree_part(executed
, before
, build
, 0);
3541 list
= generate_shifted_component_tree_part(executed
, isolated
,
3543 res
= isl_ast_graft_list_concat(res
, list
);
3544 list
= generate_shifted_component_tree_part(executed
, after
, build
, 0);
3545 res
= isl_ast_graft_list_concat(res
, list
);
3546 list
= generate_shifted_component_tree_part(executed
, other
, build
, 0);
3547 res
= isl_ast_graft_list_concat(res
, list
);
3549 isl_union_map_free(executed
);
3550 isl_ast_build_free(build
);
3555 /* Does "set" intersect "first", but not "second"?
3557 static isl_bool
only_intersects_first(__isl_keep isl_set
*set
,
3558 __isl_keep isl_set
*first
, __isl_keep isl_set
*second
)
3562 disjoint
= isl_set_is_disjoint(set
, first
);
3564 return isl_bool_error
;
3566 return isl_bool_false
;
3568 return isl_set_is_disjoint(set
, second
);
3571 /* Generate code for a single component, after shifting (if any)
3572 * has been applied, in case the schedule was specified as a schedule tree.
3573 * In particular, do so in case of isolation where there is
3574 * only an "isolated" part and an "after" part.
3575 * "dead1" and "dead2" are freed by this function in order to simplify
3578 * The "before" and "other" parts are set to empty sets.
3580 static __isl_give isl_ast_graft_list
*generate_shifted_component_only_after(
3581 __isl_take isl_union_map
*executed
, __isl_take isl_set
*isolated
,
3582 __isl_take isl_set
*after
, __isl_take isl_ast_build
*build
,
3583 __isl_take isl_set
*dead1
, __isl_take isl_set
*dead2
)
3587 empty
= isl_set_empty(isl_set_get_space(after
));
3588 isl_set_free(dead1
);
3589 isl_set_free(dead2
);
3590 return generate_shifted_component_parts(executed
, isl_set_copy(empty
),
3591 isolated
, after
, empty
, build
);
3594 /* Generate code for a single component, after shifting (if any)
3595 * has been applied, in case the schedule was specified as a schedule tree.
3597 * We first check if the user has specified an isolated schedule domain
3598 * and that we are not already outside of this isolated schedule domain.
3599 * If so, we break up the schedule domain into iterations that
3600 * precede the isolated domain, the isolated domain itself,
3601 * the iterations that follow the isolated domain and
3602 * the remaining iterations (those that are incomparable
3603 * to the isolated domain).
3604 * We generate an AST for each piece and concatenate the results.
3606 * If the isolated domain is not convex, then it is replaced
3607 * by a convex superset to ensure that the sets of preceding and
3608 * following iterations are properly defined and, in particular,
3609 * that there are no intermediate iterations that do not belong
3610 * to the isolated domain.
3612 * In the special case where at least one element of the schedule
3613 * domain that does not belong to the isolated domain needs
3614 * to be scheduled after this isolated domain, but none of those
3615 * elements need to be scheduled before, break up the schedule domain
3616 * in only two parts, the isolated domain, and a part that will be
3617 * scheduled after the isolated domain.
3619 * If no isolated set has been specified, then we generate an
3620 * AST for the entire inverse schedule.
3622 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree(
3623 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3627 int empty
, has_isolate
;
3629 isl_union_set
*schedule_domain
;
3631 isl_basic_set
*hull
;
3632 isl_set
*isolated
, *before
, *after
, *test
;
3636 build
= isl_ast_build_extract_isolated(build
);
3637 has_isolate
= isl_ast_build_has_isolated(build
);
3638 if (has_isolate
< 0)
3639 executed
= isl_union_map_free(executed
);
3640 else if (!has_isolate
)
3641 return generate_shifted_component_tree_base(executed
, build
, 0);
3643 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3644 domain
= isl_set_from_union_set(schedule_domain
);
3646 isolated
= isl_ast_build_get_isolated(build
);
3647 isolated
= isl_set_intersect(isolated
, isl_set_copy(domain
));
3648 test
= isl_ast_build_specialize(build
, isl_set_copy(isolated
));
3649 empty
= isl_set_is_empty(test
);
3654 isl_set_free(isolated
);
3655 isl_set_free(domain
);
3656 return generate_shifted_component_tree_base(executed
, build
, 0);
3658 depth
= isl_ast_build_get_depth(build
);
3662 isolated
= isl_ast_build_eliminate(build
, isolated
);
3663 hull
= isl_set_unshifted_simple_hull(isolated
);
3664 isolated
= isl_set_from_basic_set(hull
);
3666 space
= isl_space_map_from_set(isl_set_get_space(isolated
));
3667 gt
= isl_map_universe(space
);
3668 for (i
= 0; i
< depth
; ++i
)
3669 gt
= isl_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
3670 gt
= isl_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
3671 lt
= isl_map_reverse(isl_map_copy(gt
));
3672 before
= isl_set_apply(isl_set_copy(isolated
), gt
);
3673 after
= isl_set_apply(isl_set_copy(isolated
), lt
);
3675 domain
= isl_set_subtract(domain
, isl_set_copy(isolated
));
3676 pure
= only_intersects_first(domain
, after
, before
);
3678 executed
= isl_union_map_free(executed
);
3680 return generate_shifted_component_only_after(executed
, isolated
,
3681 domain
, build
, before
, after
);
3682 domain
= isl_set_subtract(domain
, isl_set_copy(before
));
3683 domain
= isl_set_subtract(domain
, isl_set_copy(after
));
3684 after
= isl_set_subtract(after
, isl_set_copy(isolated
));
3685 after
= isl_set_subtract(after
, isl_set_copy(before
));
3686 before
= isl_set_subtract(before
, isl_set_copy(isolated
));
3688 return generate_shifted_component_parts(executed
, before
, isolated
,
3689 after
, domain
, build
);
3691 isl_set_free(domain
);
3692 isl_set_free(isolated
);
3693 isl_union_map_free(executed
);
3694 isl_ast_build_free(build
);
3698 /* Generate code for a single component, after shifting (if any)
3701 * Call generate_shifted_component_tree or generate_shifted_component_flat
3702 * depending on whether the schedule was specified as a schedule tree.
3704 static __isl_give isl_ast_graft_list
*generate_shifted_component(
3705 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3707 if (isl_ast_build_has_schedule_node(build
))
3708 return generate_shifted_component_tree(executed
, build
);
3710 return generate_shifted_component_flat(executed
, build
);
3713 struct isl_set_map_pair
{
3718 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3719 * of indices into the "domain" array,
3720 * return the union of the "map" fields of the elements
3721 * indexed by the first "n" elements of "order".
3723 static __isl_give isl_union_map
*construct_component_executed(
3724 struct isl_set_map_pair
*domain
, int *order
, int n
)
3728 isl_union_map
*executed
;
3730 map
= isl_map_copy(domain
[order
[0]].map
);
3731 executed
= isl_union_map_from_map(map
);
3732 for (i
= 1; i
< n
; ++i
) {
3733 map
= isl_map_copy(domain
[order
[i
]].map
);
3734 executed
= isl_union_map_add_map(executed
, map
);
3740 /* Generate code for a single component, after shifting (if any)
3743 * The component inverse schedule is specified as the "map" fields
3744 * of the elements of "domain" indexed by the first "n" elements of "order".
3746 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
3747 struct isl_set_map_pair
*domain
, int *order
, int n
,
3748 __isl_take isl_ast_build
*build
)
3750 isl_union_map
*executed
;
3752 executed
= construct_component_executed(domain
, order
, n
);
3753 return generate_shifted_component(executed
, build
);
3756 /* Does set dimension "pos" of "set" have an obviously fixed value?
3758 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
3763 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
3766 fixed
= !isl_val_is_nan(v
);
3772 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3773 * of indices into the "domain" array,
3774 * do all (except for at most one) of the "set" field of the elements
3775 * indexed by the first "n" elements of "order" have a fixed value
3776 * at position "depth"?
3778 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
3779 int *order
, int n
, int depth
)
3784 for (i
= 0; i
< n
; ++i
) {
3787 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3800 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3801 * of indices into the "domain" array,
3802 * eliminate the inner dimensions from the "set" field of the elements
3803 * indexed by the first "n" elements of "order", provided the current
3804 * dimension does not have a fixed value.
3806 * Return the index of the first element in "order" with a corresponding
3807 * "set" field that does not have an (obviously) fixed value.
3809 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
3810 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
3815 for (i
= n
- 1; i
>= 0; --i
) {
3817 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3822 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
3823 domain
[order
[i
]].set
);
3830 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3831 * of indices into the "domain" array,
3832 * find the element of "domain" (amongst those indexed by the first "n"
3833 * elements of "order") with the "set" field that has the smallest
3834 * value for the current iterator.
3836 * Note that the domain with the smallest value may depend on the parameters
3837 * and/or outer loop dimension. Since the result of this function is only
3838 * used as heuristic, we only make a reasonable attempt at finding the best
3839 * domain, one that should work in case a single domain provides the smallest
3840 * value for the current dimension over all values of the parameters
3841 * and outer dimensions.
3843 * In particular, we compute the smallest value of the first domain
3844 * and replace it by that of any later domain if that later domain
3845 * has a smallest value that is smaller for at least some value
3846 * of the parameters and outer dimensions.
3848 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
3849 __isl_keep isl_ast_build
*build
)
3855 min_first
= isl_ast_build_map_to_iterator(build
,
3856 isl_set_copy(domain
[order
[0]].set
));
3857 min_first
= isl_map_lexmin(min_first
);
3859 for (i
= 1; i
< n
; ++i
) {
3860 isl_map
*min
, *test
;
3863 min
= isl_ast_build_map_to_iterator(build
,
3864 isl_set_copy(domain
[order
[i
]].set
));
3865 min
= isl_map_lexmin(min
);
3866 test
= isl_map_copy(min
);
3867 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
3868 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
3869 empty
= isl_map_is_empty(test
);
3871 if (empty
>= 0 && !empty
) {
3872 isl_map_free(min_first
);
3882 isl_map_free(min_first
);
3884 return i
< n
? -1 : first
;
3887 /* Construct a shifted inverse schedule based on the original inverse schedule,
3888 * the stride and the offset.
3890 * The original inverse schedule is specified as the "map" fields
3891 * of the elements of "domain" indexed by the first "n" elements of "order".
3893 * "stride" and "offset" are such that the difference
3894 * between the values of the current dimension of domain "i"
3895 * and the values of the current dimension for some reference domain are
3898 * stride * integer + offset[i]
3900 * Moreover, 0 <= offset[i] < stride.
3902 * For each domain, we create a map
3904 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3906 * where j refers to the current dimension and the other dimensions are
3907 * unchanged, and apply this map to the original schedule domain.
3909 * For example, for the original schedule
3911 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3913 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3914 * we apply the mapping
3918 * to the schedule of the "A" domain and the mapping
3920 * { [j - 1] -> [j, 1] }
3922 * to the schedule of the "B" domain.
3925 * Note that after the transformation, the differences between pairs
3926 * of values of the current dimension over all domains are multiples
3927 * of stride and that we have therefore exposed the stride.
3930 * To see that the mapping preserves the lexicographic order,
3931 * first note that each of the individual maps above preserves the order.
3932 * If the value of the current iterator is j1 in one domain and j2 in another,
3933 * then if j1 = j2, we know that the same map is applied to both domains
3934 * and the order is preserved.
3935 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3936 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3940 * and the order is preserved.
3941 * If c1 < c2, then we know
3947 * j2 - j1 = n * s + r
3949 * with n >= 0 and 0 <= r < s.
3950 * In other words, r = c2 - c1.
3961 * (j1 - c1, c1) << (j2 - c2, c2)
3963 * with "<<" the lexicographic order, proving that the order is preserved
3966 static __isl_give isl_union_map
*construct_shifted_executed(
3967 struct isl_set_map_pair
*domain
, int *order
, int n
,
3968 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3969 __isl_keep isl_ast_build
*build
)
3972 isl_union_map
*executed
;
3978 depth
= isl_ast_build_get_depth(build
);
3981 space
= isl_ast_build_get_space(build
, 1);
3982 executed
= isl_union_map_empty(isl_space_copy(space
));
3983 space
= isl_space_map_from_set(space
);
3984 map
= isl_map_identity(isl_space_copy(space
));
3985 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3986 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3987 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3989 c
= isl_constraint_alloc_equality(isl_local_space_from_space(space
));
3990 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3991 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3993 for (i
= 0; i
< n
; ++i
) {
3997 v
= isl_multi_val_get_val(offset
, i
);
4000 map_i
= isl_map_copy(map
);
4001 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
4004 c
= isl_constraint_set_constant_val(c
, v
);
4005 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
4007 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
4009 executed
= isl_union_map_add_map(executed
, map_i
);
4012 isl_constraint_free(c
);
4016 executed
= isl_union_map_free(executed
);
4021 /* Generate code for a single component, after exposing the stride,
4022 * given that the schedule domain is "shifted strided".
4024 * The component inverse schedule is specified as the "map" fields
4025 * of the elements of "domain" indexed by the first "n" elements of "order".
4027 * The schedule domain being "shifted strided" means that the differences
4028 * between the values of the current dimension of domain "i"
4029 * and the values of the current dimension for some reference domain are
4032 * stride * integer + offset[i]
4034 * We first look for the domain with the "smallest" value for the current
4035 * dimension and adjust the offsets such that the offset of the "smallest"
4036 * domain is equal to zero. The other offsets are reduced modulo stride.
4038 * Based on this information, we construct a new inverse schedule in
4039 * construct_shifted_executed that exposes the stride.
4040 * Since this involves the introduction of a new schedule dimension,
4041 * the build needs to be changed accordingly.
4042 * After computing the AST, the newly introduced dimension needs
4043 * to be removed again from the list of grafts. We do this by plugging
4044 * in a mapping that represents the new schedule domain in terms of the
4045 * old schedule domain.
4047 static __isl_give isl_ast_graft_list
*generate_shift_component(
4048 struct isl_set_map_pair
*domain
, int *order
, int n
,
4049 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
4050 __isl_take isl_ast_build
*build
)
4052 isl_ast_graft_list
*list
;
4058 isl_multi_aff
*ma
, *zero
;
4059 isl_union_map
*executed
;
4061 depth
= isl_ast_build_get_depth(build
);
4063 first
= first_offset(domain
, order
, n
, build
);
4064 if (depth
< 0 || first
< 0)
4067 mv
= isl_multi_val_copy(offset
);
4068 val
= isl_multi_val_get_val(offset
, first
);
4069 val
= isl_val_neg(val
);
4070 mv
= isl_multi_val_add_val(mv
, val
);
4071 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
4073 executed
= construct_shifted_executed(domain
, order
, n
, stride
, mv
,
4075 space
= isl_ast_build_get_space(build
, 1);
4076 space
= isl_space_map_from_set(space
);
4077 ma
= isl_multi_aff_identity(isl_space_copy(space
));
4078 space
= isl_space_from_domain(isl_space_domain(space
));
4079 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
4080 zero
= isl_multi_aff_zero(space
);
4081 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
4082 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
4083 list
= generate_shifted_component(executed
, build
);
4085 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
4087 isl_multi_val_free(mv
);
4091 isl_ast_build_free(build
);
4095 /* Does any node in the schedule tree rooted at the current schedule node
4096 * of "build" depend on outer schedule nodes?
4098 static int has_anchored_subtree(__isl_keep isl_ast_build
*build
)
4100 isl_schedule_node
*node
;
4103 node
= isl_ast_build_get_schedule_node(build
);
4104 dependent
= isl_schedule_node_is_subtree_anchored(node
);
4105 isl_schedule_node_free(node
);
4110 /* Generate code for a single component.
4112 * The component inverse schedule is specified as the "map" fields
4113 * of the elements of "domain" indexed by the first "n" elements of "order".
4115 * This function may modify the "set" fields of "domain".
4117 * Before proceeding with the actual code generation for the component,
4118 * we first check if there are any "shifted" strides, meaning that
4119 * the schedule domains of the individual domains are all strided,
4120 * but that they have different offsets, resulting in the union
4121 * of schedule domains not being strided anymore.
4123 * The simplest example is the schedule
4125 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4127 * Both schedule domains are strided, but their union is not.
4128 * This function detects such cases and then rewrites the schedule to
4130 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4132 * In the new schedule, the schedule domains have the same offset (modulo
4133 * the stride), ensuring that the union of schedule domains is also strided.
4136 * If there is only a single domain in the component, then there is
4137 * nothing to do. Similarly, if the current schedule dimension has
4138 * a fixed value for almost all domains then there is nothing to be done.
4139 * In particular, we need at least two domains where the current schedule
4140 * dimension does not have a fixed value.
4141 * Finally, in case of a schedule map input,
4142 * if any of the options refer to the current schedule dimension,
4143 * then we bail out as well. It would be possible to reformulate the options
4144 * in terms of the new schedule domain, but that would introduce constraints
4145 * that separate the domains in the options and that is something we would
4147 * In the case of a schedule tree input, we bail out if any of
4148 * the descendants of the current schedule node refer to outer
4149 * schedule nodes in any way.
4152 * To see if there is any shifted stride, we look at the differences
4153 * between the values of the current dimension in pairs of domains
4154 * for equal values of outer dimensions. These differences should be
4159 * with "m" the stride and "r" a constant. Note that we cannot perform
4160 * this analysis on individual domains as the lower bound in each domain
4161 * may depend on parameters or outer dimensions and so the current dimension
4162 * itself may not have a fixed remainder on division by the stride.
4164 * In particular, we compare the first domain that does not have an
4165 * obviously fixed value for the current dimension to itself and all
4166 * other domains and collect the offsets and the gcd of the strides.
4167 * If the gcd becomes one, then we failed to find shifted strides.
4168 * If the gcd is zero, then the differences were all fixed, meaning
4169 * that some domains had non-obviously fixed values for the current dimension.
4170 * If all the offsets are the same (for those domains that do not have
4171 * an obviously fixed value for the current dimension), then we do not
4172 * apply the transformation.
4173 * If none of the domains were skipped, then there is nothing to do.
4174 * If some of them were skipped, then if we apply separation, the schedule
4175 * domain should get split in pieces with a (non-shifted) stride.
4177 * Otherwise, we apply a shift to expose the stride in
4178 * generate_shift_component.
4180 static __isl_give isl_ast_graft_list
*generate_component(
4181 struct isl_set_map_pair
*domain
, int *order
, int n
,
4182 __isl_take isl_ast_build
*build
)
4189 isl_val
*gcd
= NULL
;
4193 isl_ast_graft_list
*list
;
4196 depth
= isl_ast_build_get_depth(build
);
4201 if (skip
>= 0 && !skip
)
4202 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
4203 if (skip
>= 0 && !skip
) {
4204 if (isl_ast_build_has_schedule_node(build
))
4205 skip
= has_anchored_subtree(build
);
4207 skip
= isl_ast_build_options_involve_depth(build
);
4212 return generate_shifted_component_from_list(domain
,
4215 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
4219 ctx
= isl_ast_build_get_ctx(build
);
4221 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
4224 for (i
= 0; i
< n
; ++i
) {
4227 map
= isl_map_from_domain_and_range(
4228 isl_set_copy(domain
[order
[base
]].set
),
4229 isl_set_copy(domain
[order
[i
]].set
));
4230 for (d
= 0; d
< depth
; ++d
)
4231 map
= isl_map_equate(map
, isl_dim_in
, d
,
4233 deltas
= isl_map_deltas(map
);
4234 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
4235 isl_set_free(deltas
);
4242 gcd
= isl_val_gcd(gcd
, m
);
4243 if (isl_val_is_one(gcd
)) {
4247 mv
= isl_multi_val_set_val(mv
, i
, r
);
4249 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
4255 if (fixed
&& i
> base
) {
4257 a
= isl_multi_val_get_val(mv
, i
);
4258 b
= isl_multi_val_get_val(mv
, base
);
4259 if (isl_val_ne(a
, b
))
4266 if (res
< 0 || !gcd
) {
4267 isl_ast_build_free(build
);
4269 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
4270 list
= generate_shifted_component_from_list(domain
,
4273 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
4278 isl_multi_val_free(mv
);
4282 isl_ast_build_free(build
);
4286 /* Store both "map" itself and its domain in the
4287 * structure pointed to by *next and advance to the next array element.
4289 static isl_stat
extract_domain(__isl_take isl_map
*map
, void *user
)
4291 struct isl_set_map_pair
**next
= user
;
4293 (*next
)->map
= isl_map_copy(map
);
4294 (*next
)->set
= isl_map_domain(map
);
4300 static isl_bool
after_in_tree(__isl_keep isl_union_map
*umap
,
4301 __isl_keep isl_schedule_node
*node
);
4303 /* Is any domain element of "umap" scheduled after any of
4304 * the corresponding image elements by the tree rooted at
4305 * the child of "node"?
4307 static isl_bool
after_in_child(__isl_keep isl_union_map
*umap
,
4308 __isl_keep isl_schedule_node
*node
)
4310 isl_schedule_node
*child
;
4313 child
= isl_schedule_node_get_child(node
, 0);
4314 after
= after_in_tree(umap
, child
);
4315 isl_schedule_node_free(child
);
4320 /* Is any domain element of "umap" scheduled after any of
4321 * the corresponding image elements by the tree rooted at
4322 * the band node "node"?
4324 * We first check if any domain element is scheduled after any
4325 * of the corresponding image elements by the band node itself.
4326 * If not, we restrict "map" to those pairs of element that
4327 * are scheduled together by the band node and continue with
4328 * the child of the band node.
4329 * If there are no such pairs then the map passed to after_in_child
4330 * will be empty causing it to return 0.
4332 static isl_bool
after_in_band(__isl_keep isl_union_map
*umap
,
4333 __isl_keep isl_schedule_node
*node
)
4335 isl_multi_union_pw_aff
*mupa
;
4336 isl_union_map
*partial
, *test
, *gt
, *universe
, *umap1
, *umap2
;
4337 isl_union_set
*domain
, *range
;
4343 n
= isl_schedule_node_band_n_member(node
);
4345 return isl_bool_error
;
4347 return after_in_child(umap
, node
);
4349 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
4350 space
= isl_multi_union_pw_aff_get_space(mupa
);
4351 partial
= isl_union_map_from_multi_union_pw_aff(mupa
);
4352 test
= isl_union_map_copy(umap
);
4353 test
= isl_union_map_apply_domain(test
, isl_union_map_copy(partial
));
4354 test
= isl_union_map_apply_range(test
, isl_union_map_copy(partial
));
4355 gt
= isl_union_map_from_map(isl_map_lex_gt(space
));
4356 test
= isl_union_map_intersect(test
, gt
);
4357 empty
= isl_union_map_is_empty(test
);
4358 isl_union_map_free(test
);
4360 if (empty
< 0 || !empty
) {
4361 isl_union_map_free(partial
);
4362 return isl_bool_not(empty
);
4365 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4366 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4367 range
= isl_union_map_range(universe
);
4368 umap1
= isl_union_map_copy(partial
);
4369 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4370 umap2
= isl_union_map_intersect_domain(partial
, range
);
4371 test
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4372 test
= isl_union_map_intersect(test
, isl_union_map_copy(umap
));
4373 after
= after_in_child(test
, node
);
4374 isl_union_map_free(test
);
4378 /* Is any domain element of "umap" scheduled after any of
4379 * the corresponding image elements by the tree rooted at
4380 * the context node "node"?
4382 * The context constraints apply to the schedule domain,
4383 * so we cannot apply them directly to "umap", which contains
4384 * pairs of statement instances. Instead, we add them
4385 * to the range of the prefix schedule for both domain and
4388 static isl_bool
after_in_context(__isl_keep isl_union_map
*umap
,
4389 __isl_keep isl_schedule_node
*node
)
4391 isl_union_map
*prefix
, *universe
, *umap1
, *umap2
;
4392 isl_union_set
*domain
, *range
;
4396 umap
= isl_union_map_copy(umap
);
4397 context
= isl_schedule_node_context_get_context(node
);
4398 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4399 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4400 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4401 range
= isl_union_map_range(universe
);
4402 umap1
= isl_union_map_copy(prefix
);
4403 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4404 umap2
= isl_union_map_intersect_domain(prefix
, range
);
4405 umap1
= isl_union_map_intersect_range(umap1
,
4406 isl_union_set_from_set(context
));
4407 umap1
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4408 umap
= isl_union_map_intersect(umap
, umap1
);
4410 after
= after_in_child(umap
, node
);
4412 isl_union_map_free(umap
);
4417 /* Is any domain element of "umap" scheduled after any of
4418 * the corresponding image elements by the tree rooted at
4419 * the expansion node "node"?
4421 * We apply the expansion to domain and range of "umap" and
4422 * continue with its child.
4424 static isl_bool
after_in_expansion(__isl_keep isl_union_map
*umap
,
4425 __isl_keep isl_schedule_node
*node
)
4427 isl_union_map
*expansion
;
4430 expansion
= isl_schedule_node_expansion_get_expansion(node
);
4431 umap
= isl_union_map_copy(umap
);
4432 umap
= isl_union_map_apply_domain(umap
, isl_union_map_copy(expansion
));
4433 umap
= isl_union_map_apply_range(umap
, expansion
);
4435 after
= after_in_child(umap
, node
);
4437 isl_union_map_free(umap
);
4442 /* Is any domain element of "umap" scheduled after any of
4443 * the corresponding image elements by the tree rooted at
4444 * the extension node "node"?
4446 * Since the extension node may add statement instances before or
4447 * after the pairs of statement instances in "umap", we return isl_bool_true
4448 * to ensure that these pairs are not broken up.
4450 static isl_bool
after_in_extension(__isl_keep isl_union_map
*umap
,
4451 __isl_keep isl_schedule_node
*node
)
4453 return isl_bool_true
;
4456 /* Is any domain element of "umap" scheduled after any of
4457 * the corresponding image elements by the tree rooted at
4458 * the filter node "node"?
4460 * We intersect domain and range of "umap" with the filter and
4461 * continue with its child.
4463 static isl_bool
after_in_filter(__isl_keep isl_union_map
*umap
,
4464 __isl_keep isl_schedule_node
*node
)
4466 isl_union_set
*filter
;
4469 umap
= isl_union_map_copy(umap
);
4470 filter
= isl_schedule_node_filter_get_filter(node
);
4471 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(filter
));
4472 umap
= isl_union_map_intersect_range(umap
, filter
);
4474 after
= after_in_child(umap
, node
);
4476 isl_union_map_free(umap
);
4481 /* Is any domain element of "umap" scheduled after any of
4482 * the corresponding image elements by the tree rooted at
4483 * the set node "node"?
4485 * This is only the case if this condition holds in any
4486 * of the (filter) children of the set node.
4487 * In particular, if the domain and the range of "umap"
4488 * are contained in different children, then the condition
4491 static isl_bool
after_in_set(__isl_keep isl_union_map
*umap
,
4492 __isl_keep isl_schedule_node
*node
)
4497 n
= isl_schedule_node_n_children(node
);
4499 return isl_bool_error
;
4500 for (i
= 0; i
< n
; ++i
) {
4501 isl_schedule_node
*child
;
4504 child
= isl_schedule_node_get_child(node
, i
);
4505 after
= after_in_tree(umap
, child
);
4506 isl_schedule_node_free(child
);
4508 if (after
< 0 || after
)
4512 return isl_bool_false
;
4515 /* Return the filter of child "i" of "node".
4517 static __isl_give isl_union_set
*child_filter(
4518 __isl_keep isl_schedule_node
*node
, int i
)
4520 isl_schedule_node
*child
;
4521 isl_union_set
*filter
;
4523 child
= isl_schedule_node_get_child(node
, i
);
4524 filter
= isl_schedule_node_filter_get_filter(child
);
4525 isl_schedule_node_free(child
);
4530 /* Is any domain element of "umap" scheduled after any of
4531 * the corresponding image elements by the tree rooted at
4532 * the sequence node "node"?
4534 * This happens in particular if any domain element is
4535 * contained in a later child than one containing a range element or
4536 * if the condition holds within a given child in the sequence.
4537 * The later part of the condition is checked by after_in_set.
4539 static isl_bool
after_in_sequence(__isl_keep isl_union_map
*umap
,
4540 __isl_keep isl_schedule_node
*node
)
4544 isl_union_map
*umap_i
;
4546 isl_bool after
= isl_bool_false
;
4548 n
= isl_schedule_node_n_children(node
);
4550 return isl_bool_error
;
4551 for (i
= 1; i
< n
; ++i
) {
4552 isl_union_set
*filter_i
;
4554 umap_i
= isl_union_map_copy(umap
);
4555 filter_i
= child_filter(node
, i
);
4556 umap_i
= isl_union_map_intersect_domain(umap_i
, filter_i
);
4557 empty
= isl_union_map_is_empty(umap_i
);
4561 isl_union_map_free(umap_i
);
4565 for (j
= 0; j
< i
; ++j
) {
4566 isl_union_set
*filter_j
;
4567 isl_union_map
*umap_ij
;
4569 umap_ij
= isl_union_map_copy(umap_i
);
4570 filter_j
= child_filter(node
, j
);
4571 umap_ij
= isl_union_map_intersect_range(umap_ij
,
4573 empty
= isl_union_map_is_empty(umap_ij
);
4574 isl_union_map_free(umap_ij
);
4579 after
= isl_bool_true
;
4584 isl_union_map_free(umap_i
);
4589 if (after
< 0 || after
)
4592 return after_in_set(umap
, node
);
4594 isl_union_map_free(umap_i
);
4595 return isl_bool_error
;
4598 /* Is any domain element of "umap" scheduled after any of
4599 * the corresponding image elements by the tree rooted at "node"?
4601 * If "umap" is empty, then clearly there is no such element.
4602 * Otherwise, consider the different types of nodes separately.
4604 static isl_bool
after_in_tree(__isl_keep isl_union_map
*umap
,
4605 __isl_keep isl_schedule_node
*node
)
4608 enum isl_schedule_node_type type
;
4610 empty
= isl_union_map_is_empty(umap
);
4612 return isl_bool_error
;
4614 return isl_bool_false
;
4616 return isl_bool_error
;
4618 type
= isl_schedule_node_get_type(node
);
4620 case isl_schedule_node_error
:
4621 return isl_bool_error
;
4622 case isl_schedule_node_leaf
:
4623 return isl_bool_false
;
4624 case isl_schedule_node_band
:
4625 return after_in_band(umap
, node
);
4626 case isl_schedule_node_domain
:
4627 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
4628 "unexpected internal domain node",
4629 return isl_bool_error
);
4630 case isl_schedule_node_context
:
4631 return after_in_context(umap
, node
);
4632 case isl_schedule_node_expansion
:
4633 return after_in_expansion(umap
, node
);
4634 case isl_schedule_node_extension
:
4635 return after_in_extension(umap
, node
);
4636 case isl_schedule_node_filter
:
4637 return after_in_filter(umap
, node
);
4638 case isl_schedule_node_guard
:
4639 case isl_schedule_node_mark
:
4640 return after_in_child(umap
, node
);
4641 case isl_schedule_node_set
:
4642 return after_in_set(umap
, node
);
4643 case isl_schedule_node_sequence
:
4644 return after_in_sequence(umap
, node
);
4647 return isl_bool_true
;
4650 /* Is any domain element of "map1" scheduled after any domain
4651 * element of "map2" by the subtree underneath the current band node,
4652 * while at the same time being scheduled together by the current
4653 * band node, i.e., by "map1" and "map2?
4655 * If the child of the current band node is a leaf, then
4656 * no element can be scheduled after any other element.
4658 * Otherwise, we construct a relation between domain elements
4659 * of "map1" and domain elements of "map2" that are scheduled
4660 * together and then check if the subtree underneath the current
4661 * band node determines their relative order.
4663 static isl_bool
after_in_subtree(__isl_keep isl_ast_build
*build
,
4664 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4666 isl_schedule_node
*node
;
4668 isl_union_map
*umap
;
4671 node
= isl_ast_build_get_schedule_node(build
);
4673 return isl_bool_error
;
4674 node
= isl_schedule_node_child(node
, 0);
4675 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
4676 isl_schedule_node_free(node
);
4677 return isl_bool_false
;
4679 map
= isl_map_copy(map2
);
4680 map
= isl_map_apply_domain(map
, isl_map_copy(map1
));
4681 umap
= isl_union_map_from_map(map
);
4682 after
= after_in_tree(umap
, node
);
4683 isl_union_map_free(umap
);
4684 isl_schedule_node_free(node
);
4688 /* Internal data for any_scheduled_after.
4690 * "build" is the build in which the AST is constructed.
4691 * "depth" is the number of loops that have already been generated
4692 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4693 * "domain" is an array of set-map pairs corresponding to the different
4694 * iteration domains. The set is the schedule domain, i.e., the domain
4695 * of the inverse schedule, while the map is the inverse schedule itself.
4697 struct isl_any_scheduled_after_data
{
4698 isl_ast_build
*build
;
4700 int group_coscheduled
;
4701 struct isl_set_map_pair
*domain
;
4704 /* Is any element of domain "i" scheduled after any element of domain "j"
4705 * (for a common iteration of the first data->depth loops)?
4707 * data->domain[i].set contains the domain of the inverse schedule
4708 * for domain "i", i.e., elements in the schedule domain.
4710 * If we are inside a band of a schedule tree and there is a pair
4711 * of elements in the two domains that is schedule together by
4712 * the current band, then we check if any element of "i" may be schedule
4713 * after element of "j" by the descendants of the band node.
4715 * If data->group_coscheduled is set, then we also return 1 if there
4716 * is any pair of elements in the two domains that are scheduled together.
4718 static isl_bool
any_scheduled_after(int i
, int j
, void *user
)
4720 struct isl_any_scheduled_after_data
*data
= user
;
4721 isl_size dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
4725 return isl_bool_error
;
4727 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
4730 follows
= isl_set_follows_at(data
->domain
[i
].set
,
4731 data
->domain
[j
].set
, pos
);
4734 return isl_bool_error
;
4736 return isl_bool_true
;
4738 return isl_bool_false
;
4741 if (isl_ast_build_has_schedule_node(data
->build
)) {
4744 after
= after_in_subtree(data
->build
, data
->domain
[i
].map
,
4745 data
->domain
[j
].map
);
4746 if (after
< 0 || after
)
4750 return isl_bool_ok(data
->group_coscheduled
);
4753 /* Look for independent components at the current depth and generate code
4754 * for each component separately. The resulting lists of grafts are
4755 * merged in an attempt to combine grafts with identical guards.
4757 * Code for two domains can be generated separately if all the elements
4758 * of one domain are scheduled before (or together with) all the elements
4759 * of the other domain. We therefore consider the graph with as nodes
4760 * the domains and an edge between two nodes if any element of the first
4761 * node is scheduled after any element of the second node.
4762 * If the ast_build_group_coscheduled is set, then we also add an edge if
4763 * there is any pair of elements in the two domains that are scheduled
4765 * Code is then generated (by generate_component)
4766 * for each of the strongly connected components in this graph
4767 * in their topological order.
4769 * Since the test is performed on the domain of the inverse schedules of
4770 * the different domains, we precompute these domains and store
4771 * them in data.domain.
4773 static __isl_give isl_ast_graft_list
*generate_components(
4774 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4777 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4778 isl_size n
= isl_union_map_n_map(executed
);
4780 struct isl_any_scheduled_after_data data
;
4781 struct isl_set_map_pair
*next
;
4782 struct isl_tarjan_graph
*g
= NULL
;
4783 isl_ast_graft_list
*list
= NULL
;
4789 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
4795 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
4798 depth
= isl_ast_build_get_depth(build
);
4803 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
4804 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
4808 list
= isl_ast_graft_list_alloc(ctx
, 0);
4812 isl_ast_graft_list
*list_c
;
4815 if (g
->order
[i
] == -1)
4816 isl_die(ctx
, isl_error_internal
, "cannot happen",
4819 while (g
->order
[i
] != -1) {
4823 list_c
= generate_component(data
.domain
,
4824 g
->order
+ first
, i
- first
,
4825 isl_ast_build_copy(build
));
4826 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
4832 error
: list
= isl_ast_graft_list_free(list
);
4833 isl_tarjan_graph_free(g
);
4834 for (i
= 0; i
< n_domain
; ++i
) {
4835 isl_map_free(data
.domain
[i
].map
);
4836 isl_set_free(data
.domain
[i
].set
);
4839 isl_union_map_free(executed
);
4840 isl_ast_build_free(build
);
4845 /* Generate code for the next level (and all inner levels).
4847 * If "executed" is empty, i.e., no code needs to be generated,
4848 * then we return an empty list.
4850 * If we have already generated code for all loop levels, then we pass
4851 * control to generate_inner_level.
4853 * If "executed" lives in a single space, i.e., if code needs to be
4854 * generated for a single domain, then there can only be a single
4855 * component and we go directly to generate_shifted_component.
4856 * Otherwise, we call generate_components to detect the components
4857 * and to call generate_component on each of them separately.
4859 static __isl_give isl_ast_graft_list
*generate_next_level(
4860 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4866 if (!build
|| !executed
)
4869 if (isl_union_map_is_empty(executed
)) {
4870 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4871 isl_union_map_free(executed
);
4872 isl_ast_build_free(build
);
4873 return isl_ast_graft_list_alloc(ctx
, 0);
4876 depth
= isl_ast_build_get_depth(build
);
4877 dim
= isl_ast_build_dim(build
, isl_dim_set
);
4878 if (depth
< 0 || dim
< 0)
4881 return generate_inner_level(executed
, build
);
4883 n
= isl_union_map_n_map(executed
);
4887 return generate_shifted_component(executed
, build
);
4889 return generate_components(executed
, build
);
4891 isl_union_map_free(executed
);
4892 isl_ast_build_free(build
);
4896 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4897 * internal, executed and build are the inputs to generate_code.
4898 * list collects the output.
4900 struct isl_generate_code_data
{
4902 isl_union_map
*executed
;
4903 isl_ast_build
*build
;
4905 isl_ast_graft_list
*list
;
4908 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4912 * with E the external build schedule and S the additional schedule "space",
4913 * reformulate the inverse schedule in terms of the internal schedule domain,
4918 * We first obtain a mapping
4922 * take the inverse and the product with S -> S, resulting in
4924 * [I -> S] -> [E -> S]
4926 * Applying the map to the input produces the desired result.
4928 static __isl_give isl_union_map
*internal_executed(
4929 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
4930 __isl_keep isl_ast_build
*build
)
4934 proj
= isl_ast_build_get_schedule_map(build
);
4935 proj
= isl_map_reverse(proj
);
4936 space
= isl_space_map_from_set(isl_space_copy(space
));
4937 id
= isl_map_identity(space
);
4938 proj
= isl_map_product(proj
, id
);
4939 executed
= isl_union_map_apply_domain(executed
,
4940 isl_union_map_from_map(proj
));
4944 /* Generate an AST that visits the elements in the range of data->executed
4945 * in the relative order specified by the corresponding domain element(s)
4946 * for those domain elements that belong to "set".
4947 * Add the result to data->list.
4949 * The caller ensures that "set" is a universe domain.
4950 * "space" is the space of the additional part of the schedule.
4951 * It is equal to the space of "set" if build->domain is parametric.
4952 * Otherwise, it is equal to the range of the wrapped space of "set".
4954 * If the build space is not parametric and
4955 * if isl_ast_build_node_from_schedule_map
4956 * was called from an outside user (data->internal not set), then
4957 * the (inverse) schedule refers to the external build domain and needs to
4958 * be transformed to refer to the internal build domain.
4960 * If the build space is parametric, then we add some of the parameter
4961 * constraints to the executed relation. Adding these constraints
4962 * allows for an earlier detection of conflicts in some cases.
4963 * However, we do not want to divide the executed relation into
4964 * more disjuncts than necessary. We therefore approximate
4965 * the constraints on the parameters by a single disjunct set.
4967 * The build is extended to include the additional part of the schedule.
4968 * If the original build space was not parametric, then the options
4969 * in data->build refer only to the additional part of the schedule
4970 * and they need to be adjusted to refer to the complete AST build
4973 * After having adjusted inverse schedule and build, we start generating
4974 * code with the outer loop of the current code generation
4975 * in generate_next_level.
4977 * If the original build space was not parametric, we undo the embedding
4978 * on the resulting isl_ast_node_list so that it can be used within
4979 * the outer AST build.
4981 static isl_stat
generate_code_in_space(struct isl_generate_code_data
*data
,
4982 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
4984 isl_union_map
*executed
;
4985 isl_ast_build
*build
;
4986 isl_ast_graft_list
*list
;
4989 executed
= isl_union_map_copy(data
->executed
);
4990 executed
= isl_union_map_intersect_domain(executed
,
4991 isl_union_set_from_set(set
));
4993 embed
= !isl_set_is_params(data
->build
->domain
);
4994 if (embed
&& !data
->internal
)
4995 executed
= internal_executed(executed
, space
, data
->build
);
4998 domain
= isl_ast_build_get_domain(data
->build
);
4999 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
5000 executed
= isl_union_map_intersect_params(executed
, domain
);
5003 build
= isl_ast_build_copy(data
->build
);
5004 build
= isl_ast_build_product(build
, space
);
5006 list
= generate_next_level(executed
, build
);
5008 list
= isl_ast_graft_list_unembed(list
, embed
);
5010 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
5015 /* Generate an AST that visits the elements in the range of data->executed
5016 * in the relative order specified by the corresponding domain element(s)
5017 * for those domain elements that belong to "set".
5018 * Add the result to data->list.
5020 * The caller ensures that "set" is a universe domain.
5022 * If the build space S is not parametric, then the space of "set"
5023 * need to be a wrapped relation with S as domain. That is, it needs
5028 * Check this property and pass control to generate_code_in_space
5030 * If the build space is not parametric, then T is the space of "set".
5032 static isl_stat
generate_code_set(__isl_take isl_set
*set
, void *user
)
5034 struct isl_generate_code_data
*data
= user
;
5035 isl_space
*space
, *build_space
;
5038 space
= isl_set_get_space(set
);
5040 if (isl_set_is_params(data
->build
->domain
))
5041 return generate_code_in_space(data
, set
, space
);
5043 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
5044 space
= isl_space_unwrap(space
);
5045 is_domain
= isl_space_is_domain(build_space
, space
);
5046 isl_space_free(build_space
);
5047 space
= isl_space_range(space
);
5052 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5053 "invalid nested schedule space", goto error
);
5055 return generate_code_in_space(data
, set
, space
);
5058 isl_space_free(space
);
5059 return isl_stat_error
;
5062 /* Generate an AST that visits the elements in the range of "executed"
5063 * in the relative order specified by the corresponding domain element(s).
5065 * "build" is an isl_ast_build that has either been constructed by
5066 * isl_ast_build_from_context or passed to a callback set by
5067 * isl_ast_build_set_create_leaf.
5068 * In the first case, the space of the isl_ast_build is typically
5069 * a parametric space, although this is currently not enforced.
5070 * In the second case, the space is never a parametric space.
5071 * If the space S is not parametric, then the domain space(s) of "executed"
5072 * need to be wrapped relations with S as domain.
5074 * If the domain of "executed" consists of several spaces, then an AST
5075 * is generated for each of them (in arbitrary order) and the results
5078 * If "internal" is set, then the domain "S" above refers to the internal
5079 * schedule domain representation. Otherwise, it refers to the external
5080 * representation, as returned by isl_ast_build_get_schedule_space.
5082 * We essentially run over all the spaces in the domain of "executed"
5083 * and call generate_code_set on each of them.
5085 static __isl_give isl_ast_graft_list
*generate_code(
5086 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
5090 struct isl_generate_code_data data
= { 0 };
5092 isl_union_set
*schedule_domain
;
5093 isl_union_map
*universe
;
5097 space
= isl_ast_build_get_space(build
, 1);
5098 space
= isl_space_align_params(space
,
5099 isl_union_map_get_space(executed
));
5100 space
= isl_space_align_params(space
,
5101 isl_union_map_get_space(build
->options
));
5102 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
5103 executed
= isl_union_map_align_params(executed
, space
);
5104 if (!executed
|| !build
)
5107 ctx
= isl_ast_build_get_ctx(build
);
5109 data
.internal
= internal
;
5110 data
.executed
= executed
;
5112 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
5114 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
5115 schedule_domain
= isl_union_map_domain(universe
);
5116 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
5118 data
.list
= isl_ast_graft_list_free(data
.list
);
5120 isl_union_set_free(schedule_domain
);
5121 isl_union_map_free(executed
);
5123 isl_ast_build_free(build
);
5126 isl_union_map_free(executed
);
5127 isl_ast_build_free(build
);
5131 /* Generate an AST that visits the elements in the domain of "schedule"
5132 * in the relative order specified by the corresponding image element(s).
5134 * "build" is an isl_ast_build that has either been constructed by
5135 * isl_ast_build_from_context or passed to a callback set by
5136 * isl_ast_build_set_create_leaf.
5137 * In the first case, the space of the isl_ast_build is typically
5138 * a parametric space, although this is currently not enforced.
5139 * In the second case, the space is never a parametric space.
5140 * If the space S is not parametric, then the range space(s) of "schedule"
5141 * need to be wrapped relations with S as domain.
5143 * If the range of "schedule" consists of several spaces, then an AST
5144 * is generated for each of them (in arbitrary order) and the results
5147 * We first initialize the local copies of the relevant options.
5148 * We do this here rather than when the isl_ast_build is created
5149 * because the options may have changed between the construction
5150 * of the isl_ast_build and the call to isl_generate_code.
5152 * The main computation is performed on an inverse schedule (with
5153 * the schedule domain in the domain and the elements to be executed
5154 * in the range) called "executed".
5156 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule_map(
5157 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
5159 isl_ast_graft_list
*list
;
5161 isl_union_map
*executed
;
5163 build
= isl_ast_build_copy(build
);
5164 build
= isl_ast_build_set_single_valued(build
, 0);
5165 schedule
= isl_union_map_coalesce(schedule
);
5166 schedule
= isl_union_map_remove_redundancies(schedule
);
5167 executed
= isl_union_map_reverse(schedule
);
5168 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
5169 node
= isl_ast_node_from_graft_list(list
, build
);
5170 isl_ast_build_free(build
);
5175 /* The old name for isl_ast_build_node_from_schedule_map.
5176 * It is being kept for backward compatibility, but
5177 * it will be removed in the future.
5179 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
5180 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
5182 return isl_ast_build_node_from_schedule_map(build
, schedule
);
5185 /* Generate an AST that visits the elements in the domain of "executed"
5186 * in the relative order specified by the leaf node "node".
5188 * The relation "executed" maps the outer generated loop iterators
5189 * to the domain elements executed by those iterations.
5191 * Simply pass control to generate_inner_level.
5192 * Note that the current build does not refer to any band node, so
5193 * that generate_inner_level will not try to visit the child of
5196 * If multiple statement instances reach a leaf,
5197 * then they can be executed in any order.
5198 * Group the list of grafts based on shared guards
5199 * such that identical guards are only generated once
5200 * when the list is eventually passed on to isl_ast_graft_list_fuse.
5202 static __isl_give isl_ast_graft_list
*build_ast_from_leaf(
5203 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5204 __isl_take isl_union_map
*executed
)
5206 isl_ast_graft_list
*list
;
5208 isl_schedule_node_free(node
);
5209 list
= generate_inner_level(executed
, isl_ast_build_copy(build
));
5210 list
= isl_ast_graft_list_group_on_guard(list
, build
);
5211 isl_ast_build_free(build
);
5216 /* Check that the band partial schedule "partial" does not filter out
5217 * any statement instances, as specified by the range of "executed".
5219 static isl_stat
check_band_schedule_total_on_instances(
5220 __isl_keep isl_multi_union_pw_aff
*partial
,
5221 __isl_keep isl_union_map
*executed
)
5224 isl_union_set
*domain
, *instances
;
5226 instances
= isl_union_map_range(isl_union_map_copy(executed
));
5227 partial
= isl_multi_union_pw_aff_copy(partial
);
5228 domain
= isl_multi_union_pw_aff_domain(partial
);
5229 subset
= isl_union_set_is_subset(instances
, domain
);
5230 isl_union_set_free(domain
);
5231 isl_union_set_free(instances
);
5234 return isl_stat_error
;
5236 isl_die(isl_union_map_get_ctx(executed
), isl_error_invalid
,
5237 "band node is not allowed to drop statement instances",
5238 return isl_stat_error
);
5242 /* Generate an AST that visits the elements in the domain of "executed"
5243 * in the relative order specified by the band node "node" and its descendants.
5245 * The relation "executed" maps the outer generated loop iterators
5246 * to the domain elements executed by those iterations.
5248 * If the band is empty, we continue with its descendants.
5249 * Otherwise, we extend the build and the inverse schedule with
5250 * the additional space/partial schedule and continue generating
5251 * an AST in generate_next_level.
5252 * As soon as we have extended the inverse schedule with the additional
5253 * partial schedule, we look for equalities that may exists between
5254 * the old and the new part.
5256 static __isl_give isl_ast_graft_list
*build_ast_from_band(
5257 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5258 __isl_take isl_union_map
*executed
)
5261 isl_multi_union_pw_aff
*extra
;
5262 isl_union_map
*extra_umap
;
5263 isl_ast_graft_list
*list
;
5267 n
= isl_schedule_node_band_n_member(node
);
5268 if (!build
|| n
< 0 || !executed
)
5272 return build_ast_from_child(build
, node
, executed
);
5274 extra
= isl_schedule_node_band_get_partial_schedule(node
);
5275 extra
= isl_multi_union_pw_aff_align_params(extra
,
5276 isl_ast_build_get_space(build
, 1));
5277 space
= isl_multi_union_pw_aff_get_space(extra
);
5279 if (check_band_schedule_total_on_instances(extra
, executed
) < 0)
5280 executed
= isl_union_map_free(executed
);
5282 extra_umap
= isl_union_map_from_multi_union_pw_aff(extra
);
5283 extra_umap
= isl_union_map_reverse(extra_umap
);
5285 executed
= isl_union_map_domain_product(executed
, extra_umap
);
5286 executed
= isl_union_map_detect_equalities(executed
);
5288 n1
= isl_ast_build_dim(build
, isl_dim_param
);
5289 build
= isl_ast_build_product(build
, space
);
5290 n2
= isl_ast_build_dim(build
, isl_dim_param
);
5291 if (n1
< 0 || n2
< 0)
5292 build
= isl_ast_build_free(build
);
5294 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5295 "band node is not allowed to introduce new parameters",
5296 build
= isl_ast_build_free(build
));
5297 build
= isl_ast_build_set_schedule_node(build
, node
);
5299 list
= generate_next_level(executed
, build
);
5301 list
= isl_ast_graft_list_unembed(list
, 1);
5305 isl_schedule_node_free(node
);
5306 isl_union_map_free(executed
);
5307 isl_ast_build_free(build
);
5311 /* Hoist a list of grafts (in practice containing a single graft)
5312 * from "sub_build" (which includes extra context information)
5315 * In particular, project out all additional parameters introduced
5316 * by the context node from the enforced constraints and the guard
5317 * of the single graft.
5319 static __isl_give isl_ast_graft_list
*hoist_out_of_context(
5320 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
,
5321 __isl_keep isl_ast_build
*sub_build
)
5323 isl_ast_graft
*graft
;
5324 isl_basic_set
*enforced
;
5326 isl_size n_param
, extra_param
;
5328 n_param
= isl_ast_build_dim(build
, isl_dim_param
);
5329 extra_param
= isl_ast_build_dim(sub_build
, isl_dim_param
);
5330 if (n_param
< 0 || extra_param
< 0)
5331 return isl_ast_graft_list_free(list
);
5333 if (extra_param
== n_param
)
5336 extra_param
-= n_param
;
5337 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, sub_build
);
5338 enforced
= isl_basic_set_project_out(enforced
, isl_dim_param
,
5339 n_param
, extra_param
);
5340 enforced
= isl_basic_set_remove_unknown_divs(enforced
);
5341 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5342 guard
= isl_set_remove_divs_involving_dims(guard
, isl_dim_param
,
5343 n_param
, extra_param
);
5344 guard
= isl_set_project_out(guard
, isl_dim_param
, n_param
, extra_param
);
5345 guard
= isl_set_compute_divs(guard
);
5346 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5348 list
= isl_ast_graft_list_from_ast_graft(graft
);
5353 /* Generate an AST that visits the elements in the domain of "executed"
5354 * in the relative order specified by the context node "node"
5355 * and its descendants.
5357 * The relation "executed" maps the outer generated loop iterators
5358 * to the domain elements executed by those iterations.
5360 * The context node may introduce additional parameters as well as
5361 * constraints on the outer schedule dimensions or original parameters.
5363 * We add the extra parameters to a new build and the context
5364 * constraints to both the build and (as a single disjunct)
5365 * to the domain of "executed". Since the context constraints
5366 * are specified in terms of the input schedule, we first need
5367 * to map them to the internal schedule domain.
5369 * After constructing the AST from the descendants of "node",
5370 * we combine the list of grafts into a single graft within
5371 * the new build, in order to be able to exploit the additional
5372 * context constraints during this combination.
5374 * Additionally, if the current node is the outermost node in
5375 * the schedule tree (apart from the root domain node), we generate
5376 * all pending guards, again to be able to exploit the additional
5377 * context constraints. We currently do not do this for internal
5378 * context nodes since we may still want to hoist conditions
5379 * to outer AST nodes.
5381 * If the context node introduced any new parameters, then they
5382 * are removed from the set of enforced constraints and guard
5383 * in hoist_out_of_context.
5385 static __isl_give isl_ast_graft_list
*build_ast_from_context(
5386 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5387 __isl_take isl_union_map
*executed
)
5391 isl_multi_aff
*internal2input
;
5392 isl_ast_build
*sub_build
;
5393 isl_ast_graft_list
*list
;
5397 depth
= isl_schedule_node_get_tree_depth(node
);
5399 build
= isl_ast_build_free(build
);
5400 space
= isl_ast_build_get_space(build
, 1);
5401 context
= isl_schedule_node_context_get_context(node
);
5402 context
= isl_set_align_params(context
, space
);
5403 sub_build
= isl_ast_build_copy(build
);
5404 space
= isl_set_get_space(context
);
5405 sub_build
= isl_ast_build_align_params(sub_build
, space
);
5406 internal2input
= isl_ast_build_get_internal2input(sub_build
);
5407 context
= isl_set_preimage_multi_aff(context
, internal2input
);
5408 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5409 isl_set_copy(context
));
5410 context
= isl_set_from_basic_set(isl_set_simple_hull(context
));
5411 executed
= isl_union_map_intersect_domain(executed
,
5412 isl_union_set_from_set(context
));
5414 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5416 n
= isl_ast_graft_list_n_ast_graft(list
);
5418 list
= isl_ast_graft_list_free(list
);
5420 list
= isl_ast_graft_list_fuse(list
, sub_build
);
5422 list
= isl_ast_graft_list_insert_pending_guard_nodes(list
,
5425 list
= hoist_out_of_context(list
, build
, sub_build
);
5427 isl_ast_build_free(build
);
5428 isl_ast_build_free(sub_build
);
5433 /* Generate an AST that visits the elements in the domain of "executed"
5434 * in the relative order specified by the expansion node "node" and
5437 * The relation "executed" maps the outer generated loop iterators
5438 * to the domain elements executed by those iterations.
5440 * We expand the domain elements by the expansion and
5441 * continue with the descendants of the node.
5443 static __isl_give isl_ast_graft_list
*build_ast_from_expansion(
5444 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5445 __isl_take isl_union_map
*executed
)
5447 isl_union_map
*expansion
;
5450 expansion
= isl_schedule_node_expansion_get_expansion(node
);
5451 expansion
= isl_union_map_align_params(expansion
,
5452 isl_union_map_get_space(executed
));
5454 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5455 executed
= isl_union_map_apply_range(executed
, expansion
);
5456 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5457 if (n1
< 0 || n2
< 0)
5460 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5461 "expansion node is not allowed to introduce "
5462 "new parameters", goto error
);
5464 return build_ast_from_child(build
, node
, executed
);
5466 isl_ast_build_free(build
);
5467 isl_schedule_node_free(node
);
5468 isl_union_map_free(executed
);
5472 /* Generate an AST that visits the elements in the domain of "executed"
5473 * in the relative order specified by the extension node "node" and
5476 * The relation "executed" maps the outer generated loop iterators
5477 * to the domain elements executed by those iterations.
5479 * Extend the inverse schedule with the extension applied to current
5480 * set of generated constraints. Since the extension if formulated
5481 * in terms of the input schedule, it first needs to be transformed
5482 * to refer to the internal schedule.
5484 static __isl_give isl_ast_graft_list
*build_ast_from_extension(
5485 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5486 __isl_take isl_union_map
*executed
)
5488 isl_union_set
*schedule_domain
;
5489 isl_union_map
*extension
;
5492 set
= isl_ast_build_get_generated(build
);
5493 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5494 schedule_domain
= isl_union_set_from_set(set
);
5496 extension
= isl_schedule_node_extension_get_extension(node
);
5498 extension
= isl_union_map_preimage_domain_multi_aff(extension
,
5499 isl_multi_aff_copy(build
->internal2input
));
5500 extension
= isl_union_map_intersect_domain(extension
, schedule_domain
);
5501 extension
= isl_ast_build_substitute_values_union_map_domain(build
,
5503 executed
= isl_union_map_union(executed
, extension
);
5505 return build_ast_from_child(build
, node
, executed
);
5508 /* Generate an AST that visits the elements in the domain of "executed"
5509 * in the relative order specified by the filter node "node" and
5512 * The relation "executed" maps the outer generated loop iterators
5513 * to the domain elements executed by those iterations.
5515 * We simply intersect the iteration domain (i.e., the range of "executed")
5516 * with the filter and continue with the descendants of the node,
5517 * unless the resulting inverse schedule is empty, in which
5518 * case we return an empty list.
5520 * If the result of the intersection is equal to the original "executed"
5521 * relation, then keep the original representation since the intersection
5522 * may have unnecessarily broken up the relation into a greater number
5525 static __isl_give isl_ast_graft_list
*build_ast_from_filter(
5526 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5527 __isl_take isl_union_map
*executed
)
5530 isl_union_set
*filter
;
5531 isl_union_map
*orig
;
5532 isl_ast_graft_list
*list
;
5537 orig
= isl_union_map_copy(executed
);
5538 if (!build
|| !node
|| !executed
)
5541 filter
= isl_schedule_node_filter_get_filter(node
);
5542 filter
= isl_union_set_align_params(filter
,
5543 isl_union_map_get_space(executed
));
5544 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5545 executed
= isl_union_map_intersect_range(executed
, filter
);
5546 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5547 if (n1
< 0 || n2
< 0)
5550 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5551 "filter node is not allowed to introduce "
5552 "new parameters", goto error
);
5554 unchanged
= isl_union_map_is_subset(orig
, executed
);
5555 empty
= isl_union_map_is_empty(executed
);
5556 if (unchanged
< 0 || empty
< 0)
5559 isl_union_map_free(executed
);
5560 return build_ast_from_child(build
, node
, orig
);
5562 isl_union_map_free(orig
);
5564 return build_ast_from_child(build
, node
, executed
);
5566 ctx
= isl_ast_build_get_ctx(build
);
5567 list
= isl_ast_graft_list_alloc(ctx
, 0);
5568 isl_ast_build_free(build
);
5569 isl_schedule_node_free(node
);
5570 isl_union_map_free(executed
);
5573 isl_ast_build_free(build
);
5574 isl_schedule_node_free(node
);
5575 isl_union_map_free(executed
);
5576 isl_union_map_free(orig
);
5580 /* Generate an AST that visits the elements in the domain of "executed"
5581 * in the relative order specified by the guard node "node" and
5584 * The relation "executed" maps the outer generated loop iterators
5585 * to the domain elements executed by those iterations.
5587 * Ensure that the associated guard is enforced by the outer AST
5588 * constructs by adding it to the guard of the graft.
5589 * Since we know that we will enforce the guard, we can also include it
5590 * in the generated constraints used to construct an AST for
5591 * the descendant nodes.
5593 static __isl_give isl_ast_graft_list
*build_ast_from_guard(
5594 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5595 __isl_take isl_union_map
*executed
)
5598 isl_set
*guard
, *hoisted
;
5599 isl_basic_set
*enforced
;
5600 isl_ast_build
*sub_build
;
5601 isl_ast_graft
*graft
;
5602 isl_ast_graft_list
*list
;
5605 space
= isl_ast_build_get_space(build
, 1);
5606 guard
= isl_schedule_node_guard_get_guard(node
);
5607 n1
= isl_space_dim(space
, isl_dim_param
);
5608 guard
= isl_set_align_params(guard
, space
);
5609 n2
= isl_set_dim(guard
, isl_dim_param
);
5610 if (n1
< 0 || n2
< 0)
5611 guard
= isl_set_free(guard
);
5613 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5614 "guard node is not allowed to introduce "
5615 "new parameters", guard
= isl_set_free(guard
));
5616 guard
= isl_set_preimage_multi_aff(guard
,
5617 isl_multi_aff_copy(build
->internal2input
));
5618 guard
= isl_ast_build_specialize(build
, guard
);
5619 guard
= isl_set_gist(guard
, isl_set_copy(build
->generated
));
5621 sub_build
= isl_ast_build_copy(build
);
5622 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5623 isl_set_copy(guard
));
5625 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5628 hoisted
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5629 n
= isl_set_n_basic_set(hoisted
);
5631 list
= isl_ast_graft_list_free(list
);
5633 list
= isl_ast_graft_list_gist_guards(list
,
5634 isl_set_copy(hoisted
));
5635 guard
= isl_set_intersect(guard
, hoisted
);
5636 enforced
= extract_shared_enforced(list
, build
);
5637 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5640 isl_ast_build_free(sub_build
);
5641 isl_ast_build_free(build
);
5642 return isl_ast_graft_list_from_ast_graft(graft
);
5645 /* Call the before_each_mark callback, if requested by the user.
5647 * Return 0 on success and -1 on error.
5649 * The caller is responsible for recording the current inverse schedule
5652 static isl_stat
before_each_mark(__isl_keep isl_id
*mark
,
5653 __isl_keep isl_ast_build
*build
)
5656 return isl_stat_error
;
5657 if (!build
->before_each_mark
)
5659 return build
->before_each_mark(mark
, build
,
5660 build
->before_each_mark_user
);
5663 /* Call the after_each_mark callback, if requested by the user.
5665 * The caller is responsible for recording the current inverse schedule
5668 static __isl_give isl_ast_graft
*after_each_mark(
5669 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
5671 if (!graft
|| !build
)
5672 return isl_ast_graft_free(graft
);
5673 if (!build
->after_each_mark
)
5675 graft
->node
= build
->after_each_mark(graft
->node
, build
,
5676 build
->after_each_mark_user
);
5678 return isl_ast_graft_free(graft
);
5683 /* Generate an AST that visits the elements in the domain of "executed"
5684 * in the relative order specified by the mark node "node" and
5687 * The relation "executed" maps the outer generated loop iterators
5688 * to the domain elements executed by those iterations.
5690 * Since we may be calling before_each_mark and after_each_mark
5691 * callbacks, we record the current inverse schedule in the build.
5693 * We generate an AST for the child of the mark node, combine
5694 * the graft list into a single graft and then insert the mark
5695 * in the AST of that single graft.
5697 static __isl_give isl_ast_graft_list
*build_ast_from_mark(
5698 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5699 __isl_take isl_union_map
*executed
)
5702 isl_ast_graft
*graft
;
5703 isl_ast_graft_list
*list
;
5706 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
5708 mark
= isl_schedule_node_mark_get_id(node
);
5709 if (before_each_mark(mark
, build
) < 0)
5710 node
= isl_schedule_node_free(node
);
5712 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5713 list
= isl_ast_graft_list_fuse(list
, build
);
5714 n
= isl_ast_graft_list_n_ast_graft(list
);
5716 list
= isl_ast_graft_list_free(list
);
5720 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
5721 graft
= isl_ast_graft_insert_mark(graft
, mark
);
5722 graft
= after_each_mark(graft
, build
);
5723 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
5725 isl_ast_build_free(build
);
5730 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5731 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5732 __isl_take isl_union_map
*executed
);
5734 /* Generate an AST that visits the elements in the domain of "executed"
5735 * in the relative order specified by the sequence (or set) node "node" and
5738 * The relation "executed" maps the outer generated loop iterators
5739 * to the domain elements executed by those iterations.
5741 * We simply generate an AST for each of the children and concatenate
5744 static __isl_give isl_ast_graft_list
*build_ast_from_sequence(
5745 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5746 __isl_take isl_union_map
*executed
)
5751 isl_ast_graft_list
*list
;
5753 ctx
= isl_ast_build_get_ctx(build
);
5754 list
= isl_ast_graft_list_alloc(ctx
, 0);
5756 n
= isl_schedule_node_n_children(node
);
5758 list
= isl_ast_graft_list_free(list
);
5759 for (i
= 0; i
< n
; ++i
) {
5760 isl_schedule_node
*child
;
5761 isl_ast_graft_list
*list_i
;
5763 child
= isl_schedule_node_get_child(node
, i
);
5764 list_i
= build_ast_from_schedule_node(isl_ast_build_copy(build
),
5765 child
, isl_union_map_copy(executed
));
5766 list
= isl_ast_graft_list_concat(list
, list_i
);
5768 isl_ast_build_free(build
);
5769 isl_schedule_node_free(node
);
5770 isl_union_map_free(executed
);
5775 /* Generate an AST that visits the elements in the domain of "executed"
5776 * in the relative order specified by the node "node" and its descendants.
5778 * The relation "executed" maps the outer generated loop iterators
5779 * to the domain elements executed by those iterations.
5781 * The node types are handled in separate functions.
5782 * Set nodes are currently treated in the same way as sequence nodes.
5783 * The children of a set node may be executed in any order,
5784 * including the order of the children.
5786 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5787 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5788 __isl_take isl_union_map
*executed
)
5790 enum isl_schedule_node_type type
;
5792 type
= isl_schedule_node_get_type(node
);
5795 case isl_schedule_node_error
:
5797 case isl_schedule_node_leaf
:
5798 return build_ast_from_leaf(build
, node
, executed
);
5799 case isl_schedule_node_band
:
5800 return build_ast_from_band(build
, node
, executed
);
5801 case isl_schedule_node_context
:
5802 return build_ast_from_context(build
, node
, executed
);
5803 case isl_schedule_node_domain
:
5804 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
5805 "unexpected internal domain node", goto error
);
5806 case isl_schedule_node_expansion
:
5807 return build_ast_from_expansion(build
, node
, executed
);
5808 case isl_schedule_node_extension
:
5809 return build_ast_from_extension(build
, node
, executed
);
5810 case isl_schedule_node_filter
:
5811 return build_ast_from_filter(build
, node
, executed
);
5812 case isl_schedule_node_guard
:
5813 return build_ast_from_guard(build
, node
, executed
);
5814 case isl_schedule_node_mark
:
5815 return build_ast_from_mark(build
, node
, executed
);
5816 case isl_schedule_node_sequence
:
5817 case isl_schedule_node_set
:
5818 return build_ast_from_sequence(build
, node
, executed
);
5821 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
5822 "unhandled type", goto error
);
5824 isl_union_map_free(executed
);
5825 isl_schedule_node_free(node
);
5826 isl_ast_build_free(build
);
5831 /* Generate an AST that visits the elements in the domain of "executed"
5832 * in the relative order specified by the (single) child of "node" and
5835 * The relation "executed" maps the outer generated loop iterators
5836 * to the domain elements executed by those iterations.
5838 * This function is never called on a leaf, set or sequence node,
5839 * so the node always has exactly one child.
5841 static __isl_give isl_ast_graft_list
*build_ast_from_child(
5842 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5843 __isl_take isl_union_map
*executed
)
5845 node
= isl_schedule_node_child(node
, 0);
5846 return build_ast_from_schedule_node(build
, node
, executed
);
5849 /* Generate an AST that visits the elements in the domain of the domain
5850 * node "node" in the relative order specified by its descendants.
5852 * An initial inverse schedule is created that maps a zero-dimensional
5853 * schedule space to the node domain.
5854 * The input "build" is assumed to have a parametric domain and
5855 * is replaced by the same zero-dimensional schedule space.
5857 * We also add some of the parameter constraints in the build domain
5858 * to the executed relation. Adding these constraints
5859 * allows for an earlier detection of conflicts in some cases.
5860 * However, we do not want to divide the executed relation into
5861 * more disjuncts than necessary. We therefore approximate
5862 * the constraints on the parameters by a single disjunct set.
5864 static __isl_give isl_ast_node
*build_ast_from_domain(
5865 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
)
5868 isl_union_set
*domain
, *schedule_domain
;
5869 isl_union_map
*executed
;
5872 isl_ast_graft_list
*list
;
5879 ctx
= isl_ast_build_get_ctx(build
);
5880 space
= isl_ast_build_get_space(build
, 1);
5881 is_params
= isl_space_is_params(space
);
5882 isl_space_free(space
);
5886 isl_die(ctx
, isl_error_unsupported
,
5887 "expecting parametric initial context", goto error
);
5889 domain
= isl_schedule_node_domain_get_domain(node
);
5890 domain
= isl_union_set_coalesce(domain
);
5892 space
= isl_union_set_get_space(domain
);
5893 space
= isl_space_set_from_params(space
);
5894 build
= isl_ast_build_product(build
, space
);
5896 set
= isl_ast_build_get_domain(build
);
5897 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5898 schedule_domain
= isl_union_set_from_set(set
);
5900 executed
= isl_union_map_from_domain_and_range(schedule_domain
, domain
);
5901 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5902 ast
= isl_ast_node_from_graft_list(list
, build
);
5903 isl_ast_build_free(build
);
5907 isl_schedule_node_free(node
);
5908 isl_ast_build_free(build
);
5912 /* Generate an AST that visits the elements in the domain of "schedule"
5913 * in the relative order specified by the schedule tree.
5915 * "build" is an isl_ast_build that has been created using
5916 * isl_ast_build_alloc or isl_ast_build_from_context based
5917 * on a parametric set.
5919 * The construction starts at the root node of the schedule,
5920 * which is assumed to be a domain node.
5922 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule(
5923 __isl_keep isl_ast_build
*build
, __isl_take isl_schedule
*schedule
)
5926 isl_schedule_node
*node
;
5928 if (!build
|| !schedule
)
5931 ctx
= isl_ast_build_get_ctx(build
);
5933 node
= isl_schedule_get_root(schedule
);
5936 isl_schedule_free(schedule
);
5938 build
= isl_ast_build_copy(build
);
5939 build
= isl_ast_build_set_single_valued(build
, 0);
5940 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
5941 isl_die(ctx
, isl_error_unsupported
,
5942 "expecting root domain node",
5943 build
= isl_ast_build_free(build
));
5944 return build_ast_from_domain(build
, node
);
5946 isl_schedule_free(schedule
);