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
)
535 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
539 n
= isl_pw_aff_list_n_pw_aff(list
);
543 domain
= isl_ast_build_get_domain(build
);
545 for (i
= n
- 1; i
>= 0; --i
) {
550 domain_i
= isl_set_copy(domain
);
551 pa_i
= isl_pw_aff_list_get_pw_aff(list
, i
);
553 for (j
= 0; j
< n
; ++j
) {
560 pa_j
= isl_pw_aff_list_get_pw_aff(list
, j
);
561 better
= isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i
), pa_j
);
562 domain_i
= isl_set_intersect(domain_i
, better
);
565 empty
= isl_set_is_empty(domain_i
);
567 isl_set_free(domain_i
);
568 isl_pw_aff_free(pa_i
);
574 list
= isl_pw_aff_list_drop(list
, i
, 1);
578 isl_set_free(domain
);
582 isl_set_free(domain
);
583 return isl_pw_aff_list_free(list
);
586 /* Extract a lower bound on dimension "pos" from each constraint
587 * in "constraints" and return the list of lower bounds.
588 * If "constraints" has zero elements, then we extract a lower bound
589 * from "domain" instead.
591 * If the current dimension is strided, then the lower bound
592 * is adjusted by lower_bound to match the stride information.
593 * This modification may make one or more lower bounds redundant
594 * with respect to the other lower bounds. We therefore check
595 * for this condition and remove the redundant lower bounds.
597 static __isl_give isl_pw_aff_list
*lower_bounds(
598 __isl_keep isl_constraint_list
*constraints
, int pos
,
599 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
602 isl_pw_aff_list
*list
;
608 n
= isl_constraint_list_n_constraint(constraints
);
611 pa
= exact_bound(domain
, build
, 0);
612 return isl_pw_aff_list_from_pw_aff(pa
);
615 ctx
= isl_ast_build_get_ctx(build
);
616 list
= isl_pw_aff_list_alloc(ctx
,n
);
618 for (i
= 0; i
< n
; ++i
) {
622 c
= isl_constraint_list_get_constraint(constraints
, i
);
623 aff
= lower_bound(c
, pos
, build
);
624 isl_constraint_free(c
);
625 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
628 if (isl_ast_build_has_stride(build
, pos
))
629 list
= remove_redundant_lower_bounds(list
, build
);
634 /* Extract an upper bound on dimension "pos" from each constraint
635 * in "constraints" and return the list of upper bounds.
636 * If "constraints" has zero elements, then we extract an upper bound
637 * from "domain" instead.
639 static __isl_give isl_pw_aff_list
*upper_bounds(
640 __isl_keep isl_constraint_list
*constraints
, int pos
,
641 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
644 isl_pw_aff_list
*list
;
647 n
= isl_constraint_list_n_constraint(constraints
);
650 pa
= exact_bound(domain
, build
, 1);
651 return isl_pw_aff_list_from_pw_aff(pa
);
654 ctx
= isl_ast_build_get_ctx(build
);
655 list
= isl_pw_aff_list_alloc(ctx
,n
);
657 for (i
= 0; i
< n
; ++i
) {
661 c
= isl_constraint_list_get_constraint(constraints
, i
);
662 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
663 isl_constraint_free(c
);
664 aff
= isl_aff_floor(aff
);
665 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
671 /* Return an isl_ast_expr that performs the reduction of type "type"
672 * on AST expressions corresponding to the elements in "list".
674 * The list is assumed to contain at least one element.
675 * If the list contains exactly one element, then the returned isl_ast_expr
676 * simply computes that affine expression.
677 * If the list contains more than one element, then we sort it
678 * using a fairly abitrary but hopefully reasonably stable order.
680 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
681 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
690 n
= isl_pw_aff_list_n_pw_aff(list
);
693 return isl_ast_build_expr_from_pw_aff_internal(build
,
694 isl_pw_aff_list_get_pw_aff(list
, 0));
696 ctx
= isl_pw_aff_list_get_ctx(list
);
697 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
701 list
= isl_pw_aff_list_copy(list
);
702 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
704 return isl_ast_expr_free(expr
);
706 for (i
= 0; i
< n
; ++i
) {
707 isl_ast_expr
*expr_i
;
709 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
710 isl_pw_aff_list_get_pw_aff(list
, i
));
713 expr
->u
.op
.args
[i
] = expr_i
;
716 isl_pw_aff_list_free(list
);
719 isl_pw_aff_list_free(list
);
720 isl_ast_expr_free(expr
);
724 /* Add guards implied by the "generated constraints",
725 * but not (necessarily) enforced by the generated AST to "guard".
726 * In particular, if there is any stride constraints,
727 * then add the guard implied by those constraints.
728 * If we have generated a degenerate loop, then add the guard
729 * implied by "bounds" on the outer dimensions, i.e., the guard
730 * that ensures that the single value actually exists.
731 * Since there may also be guards implied by a combination
732 * of these constraints, we first combine them before
733 * deriving the implied constraints.
735 static __isl_give isl_set
*add_implied_guards(__isl_take isl_set
*guard
,
736 int degenerate
, __isl_keep isl_basic_set
*bounds
,
737 __isl_keep isl_ast_build
*build
)
739 int depth
, has_stride
;
743 depth
= isl_ast_build_get_depth(build
);
744 has_stride
= isl_ast_build_has_stride(build
, depth
);
745 if (!has_stride
&& !degenerate
)
748 space
= isl_basic_set_get_space(bounds
);
749 dom
= isl_set_universe(space
);
752 bounds
= isl_basic_set_copy(bounds
);
753 bounds
= isl_basic_set_drop_constraints_not_involving_dims(
754 bounds
, isl_dim_set
, depth
, 1);
755 set
= isl_set_from_basic_set(bounds
);
756 dom
= isl_set_intersect(dom
, set
);
760 set
= isl_ast_build_get_stride_constraint(build
);
761 dom
= isl_set_intersect(dom
, set
);
764 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
765 dom
= isl_ast_build_compute_gist(build
, dom
);
766 guard
= isl_set_intersect(guard
, dom
);
771 /* Update "graft" based on "sub_build" for the degenerate case.
773 * "build" is the build in which graft->node was created
774 * "sub_build" contains information about the current level itself,
775 * including the single value attained.
777 * We set the initialization part of the for loop to the single
778 * value attained by the current dimension.
779 * The increment and condition are not strictly needed as the are known
780 * to be "1" and "iterator <= value" respectively.
782 static __isl_give isl_ast_graft
*refine_degenerate(
783 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
,
784 __isl_keep isl_ast_build
*sub_build
)
788 if (!graft
|| !sub_build
)
789 return isl_ast_graft_free(graft
);
791 value
= isl_pw_aff_copy(sub_build
->value
);
793 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
795 if (!graft
->node
->u
.f
.init
)
796 return isl_ast_graft_free(graft
);
801 /* Return the intersection of constraints in "list" as a set.
803 static __isl_give isl_set
*intersect_constraints(
804 __isl_keep isl_constraint_list
*list
)
809 n
= isl_constraint_list_n_constraint(list
);
811 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
812 "expecting at least one constraint", return NULL
);
814 bset
= isl_basic_set_from_constraint(
815 isl_constraint_list_get_constraint(list
, 0));
816 for (i
= 1; i
< n
; ++i
) {
817 isl_basic_set
*bset_i
;
819 bset_i
= isl_basic_set_from_constraint(
820 isl_constraint_list_get_constraint(list
, i
));
821 bset
= isl_basic_set_intersect(bset
, bset_i
);
824 return isl_set_from_basic_set(bset
);
827 /* Compute the constraints on the outer dimensions enforced by
828 * graft->node and add those constraints to graft->enforced,
829 * in case the upper bound is expressed as a set "upper".
831 * In particular, if l(...) is a lower bound in "lower", and
833 * -a i + f(...) >= 0 or a i <= f(...)
835 * is an upper bound ocnstraint on the current dimension i,
836 * then the for loop enforces the constraint
838 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
840 * We therefore simply take each lower bound in turn, plug it into
841 * the upper bounds and compute the intersection over all lower bounds.
843 * If a lower bound is a rational expression, then
844 * isl_basic_set_preimage_multi_aff will force this rational
845 * expression to have only integer values. However, the loop
846 * itself does not enforce this integrality constraint. We therefore
847 * use the ceil of the lower bounds instead of the lower bounds themselves.
848 * Other constraints will make sure that the for loop is only executed
849 * when each of the lower bounds attains an integral value.
850 * In particular, potentially rational values only occur in
851 * lower_bound if the offset is a (seemingly) rational expression,
852 * but then outer conditions will make sure that this rational expression
853 * only attains integer values.
855 static __isl_give isl_ast_graft
*set_enforced_from_set(
856 __isl_take isl_ast_graft
*graft
,
857 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
860 isl_basic_set
*enforced
;
861 isl_pw_multi_aff
*pma
;
864 if (!graft
|| !lower
)
865 return isl_ast_graft_free(graft
);
867 space
= isl_set_get_space(upper
);
868 enforced
= isl_basic_set_universe(isl_space_copy(space
));
870 space
= isl_space_map_from_set(space
);
871 pma
= isl_pw_multi_aff_identity(space
);
873 n
= isl_pw_aff_list_n_pw_aff(lower
);
874 for (i
= 0; i
< n
; ++i
) {
878 isl_pw_multi_aff
*pma_i
;
880 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
881 pa
= isl_pw_aff_ceil(pa
);
882 pma_i
= isl_pw_multi_aff_copy(pma
);
883 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
884 enforced_i
= isl_set_copy(upper
);
885 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
886 hull
= isl_set_simple_hull(enforced_i
);
887 enforced
= isl_basic_set_intersect(enforced
, hull
);
890 isl_pw_multi_aff_free(pma
);
892 graft
= isl_ast_graft_enforce(graft
, enforced
);
897 /* Compute the constraints on the outer dimensions enforced by
898 * graft->node and add those constraints to graft->enforced,
899 * in case the upper bound is expressed as
900 * a list of affine expressions "upper".
902 * The enforced condition is that each lower bound expression is less
903 * than or equal to each upper bound expression.
905 static __isl_give isl_ast_graft
*set_enforced_from_list(
906 __isl_take isl_ast_graft
*graft
,
907 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
910 isl_basic_set
*enforced
;
912 lower
= isl_pw_aff_list_copy(lower
);
913 upper
= isl_pw_aff_list_copy(upper
);
914 cond
= isl_pw_aff_list_le_set(lower
, upper
);
915 enforced
= isl_set_simple_hull(cond
);
916 graft
= isl_ast_graft_enforce(graft
, enforced
);
921 /* Does "aff" have a negative constant term?
923 static isl_stat
aff_constant_is_negative(__isl_take isl_set
*set
,
924 __isl_take isl_aff
*aff
, void *user
)
929 v
= isl_aff_get_constant_val(aff
);
930 *neg
= isl_val_is_neg(v
);
935 return *neg
? isl_stat_ok
: isl_stat_error
;
938 /* Does "pa" have a negative constant term over its entire domain?
940 static isl_stat
pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
,
946 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
949 return (*neg
&& r
>= 0) ? isl_stat_ok
: isl_stat_error
;
952 /* Does each element in "list" have a negative constant term?
954 * The callback terminates the iteration as soon an element has been
955 * found that does not have a negative constant term.
957 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
961 if (isl_pw_aff_list_foreach(list
,
962 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
968 /* Add 1 to each of the elements in "list", where each of these elements
969 * is defined over the internal schedule space of "build".
971 static __isl_give isl_pw_aff_list
*list_add_one(
972 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
979 space
= isl_ast_build_get_space(build
, 1);
980 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
981 aff
= isl_aff_add_constant_si(aff
, 1);
982 one
= isl_pw_aff_from_aff(aff
);
984 n
= isl_pw_aff_list_n_pw_aff(list
);
985 for (i
= 0; i
< n
; ++i
) {
987 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
988 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
989 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
992 isl_pw_aff_free(one
);
997 /* Set the condition part of the for node graft->node in case
998 * the upper bound is represented as a list of piecewise affine expressions.
1000 * In particular, set the condition to
1002 * iterator <= min(list of upper bounds)
1004 * If each of the upper bounds has a negative constant term, then
1005 * set the condition to
1007 * iterator < min(list of (upper bound + 1)s)
1010 static __isl_give isl_ast_graft
*set_for_cond_from_list(
1011 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
1012 __isl_keep isl_ast_build
*build
)
1015 isl_ast_expr
*bound
, *iterator
, *cond
;
1016 enum isl_ast_op_type type
= isl_ast_op_le
;
1018 if (!graft
|| !list
)
1019 return isl_ast_graft_free(graft
);
1021 neg
= list_constant_is_negative(list
);
1023 return isl_ast_graft_free(graft
);
1024 list
= isl_pw_aff_list_copy(list
);
1026 list
= list_add_one(list
, build
);
1027 type
= isl_ast_op_lt
;
1030 bound
= reduce_list(isl_ast_op_min
, list
, build
);
1031 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
1032 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
1033 graft
->node
->u
.f
.cond
= cond
;
1035 isl_pw_aff_list_free(list
);
1036 if (!graft
->node
->u
.f
.cond
)
1037 return isl_ast_graft_free(graft
);
1041 /* Set the condition part of the for node graft->node in case
1042 * the upper bound is represented as a set.
1044 static __isl_give isl_ast_graft
*set_for_cond_from_set(
1045 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
1046 __isl_keep isl_ast_build
*build
)
1053 cond
= isl_ast_build_expr_from_set_internal(build
, isl_set_copy(set
));
1054 graft
->node
->u
.f
.cond
= cond
;
1055 if (!graft
->node
->u
.f
.cond
)
1056 return isl_ast_graft_free(graft
);
1060 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1061 * the current dimension.
1063 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
1071 ctx
= isl_ast_build_get_ctx(build
);
1072 depth
= isl_ast_build_get_depth(build
);
1074 if (!isl_ast_build_has_stride(build
, depth
))
1075 return isl_ast_expr_alloc_int_si(ctx
, 1);
1077 v
= isl_ast_build_get_stride(build
, depth
);
1078 return isl_ast_expr_from_val(v
);
1081 /* Should we express the loop condition as
1083 * iterator <= min(list of upper bounds)
1085 * or as a conjunction of constraints?
1087 * The first is constructed from a list of upper bounds.
1088 * The second is constructed from a set.
1090 * If there are no upper bounds in "constraints", then this could mean
1091 * that "domain" simply doesn't have an upper bound or that we didn't
1092 * pick any upper bound. In the first case, we want to generate the
1093 * loop condition as a(n empty) conjunction of constraints
1094 * In the second case, we will compute
1095 * a single upper bound from "domain" and so we use the list form.
1097 * If there are upper bounds in "constraints",
1098 * then we use the list form iff the atomic_upper_bound option is set.
1100 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
1101 __isl_keep isl_set
*domain
, int depth
)
1104 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
1106 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
1109 /* Fill in the expressions of the for node in graft->node.
1112 * - set the initialization part of the loop to the maximum of the lower bounds
1113 * - extract the increment from the stride of the current dimension
1114 * - construct the for condition either based on a list of upper bounds
1115 * or on a set of upper bound constraints.
1117 static __isl_give isl_ast_graft
*set_for_node_expressions(
1118 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
1119 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
1120 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
1127 build
= isl_ast_build_copy(build
);
1130 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
1131 node
->u
.f
.inc
= for_inc(build
);
1133 if (!node
->u
.f
.init
|| !node
->u
.f
.inc
)
1134 graft
= isl_ast_graft_free(graft
);
1137 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
1139 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
1141 isl_ast_build_free(build
);
1146 /* Update "graft" based on "bounds" and "domain" for the generic,
1147 * non-degenerate, case.
1149 * "c_lower" and "c_upper" contain the lower and upper bounds
1150 * that the loop node should express.
1151 * "domain" is the subset of the intersection of the constraints
1152 * for which some code is executed.
1154 * There may be zero lower bounds or zero upper bounds in "constraints"
1155 * in case the list of constraints was created
1156 * based on the atomic option or based on separation with explicit bounds.
1157 * In that case, we use "domain" to derive lower and/or upper bounds.
1159 * We first compute a list of one or more lower bounds.
1161 * Then we decide if we want to express the condition as
1163 * iterator <= min(list of upper bounds)
1165 * or as a conjunction of constraints.
1167 * The set of enforced constraints is then computed either based on
1168 * a list of upper bounds or on a set of upper bound constraints.
1169 * We do not compute any enforced constraints if we were forced
1170 * to compute a lower or upper bound using exact_bound. The domains
1171 * of the resulting expressions may imply some bounds on outer dimensions
1172 * that we do not want to appear in the enforced constraints since
1173 * they are not actually enforced by the corresponding code.
1175 * Finally, we fill in the expressions of the for node.
1177 static __isl_give isl_ast_graft
*refine_generic_bounds(
1178 __isl_take isl_ast_graft
*graft
,
1179 __isl_take isl_constraint_list
*c_lower
,
1180 __isl_take isl_constraint_list
*c_upper
,
1181 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1185 isl_pw_aff_list
*lower
;
1187 isl_set
*upper_set
= NULL
;
1188 isl_pw_aff_list
*upper_list
= NULL
;
1189 int n_lower
, n_upper
;
1191 if (!graft
|| !c_lower
|| !c_upper
|| !build
)
1194 depth
= isl_ast_build_get_depth(build
);
1195 ctx
= isl_ast_graft_get_ctx(graft
);
1197 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1198 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1200 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1202 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1205 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1206 else if (n_upper
> 0)
1207 upper_set
= intersect_constraints(c_upper
);
1209 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1211 if (n_lower
== 0 || n_upper
== 0)
1214 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1216 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1218 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1221 isl_pw_aff_list_free(lower
);
1222 isl_pw_aff_list_free(upper_list
);
1223 isl_set_free(upper_set
);
1224 isl_constraint_list_free(c_lower
);
1225 isl_constraint_list_free(c_upper
);
1229 isl_constraint_list_free(c_lower
);
1230 isl_constraint_list_free(c_upper
);
1231 return isl_ast_graft_free(graft
);
1234 /* Internal data structure used inside count_constraints to keep
1235 * track of the number of constraints that are independent of dimension "pos",
1236 * the lower bounds in "pos" and the upper bounds in "pos".
1238 struct isl_ast_count_constraints_data
{
1246 /* Increment data->n_indep, data->lower or data->upper depending
1247 * on whether "c" is independenct of dimensions data->pos,
1248 * a lower bound or an upper bound.
1250 static isl_stat
count_constraints(__isl_take isl_constraint
*c
, void *user
)
1252 struct isl_ast_count_constraints_data
*data
= user
;
1254 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1256 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1261 isl_constraint_free(c
);
1266 /* Update "graft" based on "bounds" and "domain" for the generic,
1267 * non-degenerate, case.
1269 * "list" respresent the list of bounds that need to be encoded by
1270 * the for loop. Only the constraints that involve the iterator
1271 * are relevant here. The other constraints are taken care of by
1272 * the caller and are included in the generated constraints of "build".
1273 * "domain" is the subset of the intersection of the constraints
1274 * for which some code is executed.
1275 * "build" is the build in which graft->node was created.
1277 * We separate lower bounds, upper bounds and constraints that
1278 * are independent of the loop iterator.
1280 * The actual for loop bounds are generated in refine_generic_bounds.
1282 static __isl_give isl_ast_graft
*refine_generic_split(
1283 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1284 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1286 struct isl_ast_count_constraints_data data
;
1287 isl_constraint_list
*lower
;
1288 isl_constraint_list
*upper
;
1291 return isl_ast_graft_free(graft
);
1293 data
.pos
= isl_ast_build_get_depth(build
);
1295 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1297 return isl_ast_graft_free(graft
);
1299 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1300 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1301 isl_constraint_list_free(list
);
1302 return isl_ast_graft_free(graft
);
1305 lower
= isl_constraint_list_drop(list
, 0, data
.n_indep
);
1306 upper
= isl_constraint_list_copy(lower
);
1307 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1308 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1310 return refine_generic_bounds(graft
, lower
, upper
, domain
, build
);
1313 /* Update "graft" based on "bounds" and "domain" for the generic,
1314 * non-degenerate, case.
1316 * "bounds" respresent the bounds that need to be encoded by
1317 * the for loop (or a guard around the for loop).
1318 * "domain" is the subset of "bounds" for which some code is executed.
1319 * "build" is the build in which graft->node was created.
1321 * We break up "bounds" into a list of constraints and continue with
1322 * refine_generic_split.
1324 static __isl_give isl_ast_graft
*refine_generic(
1325 __isl_take isl_ast_graft
*graft
,
1326 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1327 __isl_keep isl_ast_build
*build
)
1329 isl_constraint_list
*list
;
1331 if (!build
|| !graft
)
1332 return isl_ast_graft_free(graft
);
1334 list
= isl_basic_set_get_constraint_list(bounds
);
1336 graft
= refine_generic_split(graft
, list
, domain
, build
);
1341 /* Create a for node for the current level.
1343 * Mark the for node degenerate if "degenerate" is set.
1345 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1355 depth
= isl_ast_build_get_depth(build
);
1356 id
= isl_ast_build_get_iterator_id(build
, depth
);
1357 node
= isl_ast_node_alloc_for(id
);
1359 node
= isl_ast_node_for_mark_degenerate(node
);
1364 /* If the ast_build_exploit_nested_bounds option is set, then return
1365 * the constraints enforced by all elements in "list".
1366 * Otherwise, return the universe.
1368 static __isl_give isl_basic_set
*extract_shared_enforced(
1369 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1377 ctx
= isl_ast_graft_list_get_ctx(list
);
1378 if (isl_options_get_ast_build_exploit_nested_bounds(ctx
))
1379 return isl_ast_graft_list_extract_shared_enforced(list
, build
);
1381 space
= isl_ast_build_get_space(build
, 1);
1382 return isl_basic_set_universe(space
);
1385 /* Return the pending constraints of "build" that are not already taken
1386 * care of (by a combination of "enforced" and the generated constraints
1389 static __isl_give isl_set
*extract_pending(__isl_keep isl_ast_build
*build
,
1390 __isl_keep isl_basic_set
*enforced
)
1392 isl_set
*guard
, *context
;
1394 guard
= isl_ast_build_get_pending(build
);
1395 context
= isl_set_from_basic_set(isl_basic_set_copy(enforced
));
1396 context
= isl_set_intersect(context
,
1397 isl_ast_build_get_generated(build
));
1398 return isl_set_gist(guard
, context
);
1401 /* Create an AST node for the current dimension based on
1402 * the schedule domain "bounds" and return the node encapsulated
1403 * in an isl_ast_graft.
1405 * "executed" is the current inverse schedule, taking into account
1406 * the bounds in "bounds"
1407 * "domain" is the domain of "executed", with inner dimensions projected out.
1408 * It may be a strict subset of "bounds" in case "bounds" was created
1409 * based on the atomic option or based on separation with explicit bounds.
1411 * "domain" may satisfy additional equalities that result
1412 * from intersecting "executed" with "bounds" in add_node.
1413 * It may also satisfy some global constraints that were dropped out because
1414 * we performed separation with explicit bounds.
1415 * The very first step is then to copy these constraints to "bounds".
1417 * Since we may be calling before_each_for and after_each_for
1418 * callbacks, we record the current inverse schedule in the build.
1420 * We consider three builds,
1421 * "build" is the one in which the current level is created,
1422 * "body_build" is the build in which the next level is created,
1423 * "sub_build" is essentially the same as "body_build", except that
1424 * the depth has not been increased yet.
1426 * "build" already contains information (in strides and offsets)
1427 * about the strides at the current level, but this information is not
1428 * reflected in the build->domain.
1429 * We first add this information and the "bounds" to the sub_build->domain.
1430 * isl_ast_build_set_loop_bounds adds the stride information and
1431 * checks whether the current dimension attains
1432 * only a single value and whether this single value can be represented using
1433 * a single affine expression.
1434 * In the first case, the current level is considered "degenerate".
1435 * In the second, sub-case, the current level is considered "eliminated".
1436 * Eliminated levels don't need to be reflected in the AST since we can
1437 * simply plug in the affine expression. For degenerate, but non-eliminated,
1438 * levels, we do introduce a for node, but mark is as degenerate so that
1439 * it can be printed as an assignment of the single value to the loop
1442 * If the current level is eliminated, we explicitly plug in the value
1443 * for the current level found by isl_ast_build_set_loop_bounds in the
1444 * inverse schedule. This ensures that if we are working on a slice
1445 * of the domain based on information available in the inverse schedule
1446 * and the build domain, that then this information is also reflected
1447 * in the inverse schedule. This operation also eliminates the current
1448 * dimension from the inverse schedule making sure no inner dimensions depend
1449 * on the current dimension. Otherwise, we create a for node, marking
1450 * it degenerate if appropriate. The initial for node is still incomplete
1451 * and will be completed in either refine_degenerate or refine_generic.
1453 * We then generate a sequence of grafts for the next level,
1454 * create a surrounding graft for the current level and insert
1455 * the for node we created (if the current level is not eliminated).
1456 * Before creating a graft for the current level, we first extract
1457 * hoistable constraints from the child guards and combine them
1458 * with the pending constraints in the build. These constraints
1459 * are used to simplify the child guards and then added to the guard
1460 * of the current graft to ensure that they will be generated.
1461 * If the hoisted guard is a disjunction, then we use it directly
1462 * to gist the guards on the children before intersect it with the
1463 * pending constraints. We do so because this disjunction is typically
1464 * identical to the guards on the children such that these guards
1465 * can be effectively removed completely. After the intersection,
1466 * the gist operation would have a harder time figuring this out.
1468 * Finally, we set the bounds of the for loop in either
1469 * refine_degenerate or refine_generic.
1470 * We do so in a context where the pending constraints of the build
1471 * have been replaced by the guard of the current graft.
1473 static __isl_give isl_ast_graft
*create_node_scaled(
1474 __isl_take isl_union_map
*executed
,
1475 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1476 __isl_take isl_ast_build
*build
)
1480 isl_bool eliminated
;
1481 isl_basic_set
*hull
;
1482 isl_basic_set
*enforced
;
1483 isl_set
*guard
, *hoisted
;
1484 isl_ast_node
*node
= NULL
;
1485 isl_ast_graft
*graft
;
1486 isl_ast_graft_list
*children
;
1487 isl_ast_build
*sub_build
;
1488 isl_ast_build
*body_build
;
1490 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1491 domain
= isl_set_detect_equalities(domain
);
1492 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1493 bounds
= isl_basic_set_intersect(bounds
, hull
);
1494 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1496 depth
= isl_ast_build_get_depth(build
);
1497 sub_build
= isl_ast_build_copy(build
);
1498 bounds
= isl_basic_set_remove_redundancies(bounds
);
1499 bounds
= isl_ast_build_specialize_basic_set(sub_build
, bounds
);
1500 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1501 isl_basic_set_copy(bounds
));
1502 degenerate
= isl_ast_build_has_value(sub_build
);
1503 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1504 if (degenerate
< 0 || eliminated
< 0)
1505 executed
= isl_union_map_free(executed
);
1507 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1508 sub_build
= isl_ast_build_set_pending_generated(sub_build
,
1509 isl_basic_set_copy(bounds
));
1511 executed
= plug_in_values(executed
, sub_build
);
1513 node
= create_for(build
, degenerate
);
1515 body_build
= isl_ast_build_copy(sub_build
);
1516 body_build
= isl_ast_build_increase_depth(body_build
);
1518 node
= before_each_for(node
, body_build
);
1519 children
= generate_next_level(executed
,
1520 isl_ast_build_copy(body_build
));
1522 enforced
= extract_shared_enforced(children
, build
);
1523 guard
= extract_pending(sub_build
, enforced
);
1524 hoisted
= isl_ast_graft_list_extract_hoistable_guard(children
, build
);
1525 if (isl_set_n_basic_set(hoisted
) > 1)
1526 children
= isl_ast_graft_list_gist_guards(children
,
1527 isl_set_copy(hoisted
));
1528 guard
= isl_set_intersect(guard
, hoisted
);
1530 guard
= add_implied_guards(guard
, degenerate
, bounds
, build
);
1532 graft
= isl_ast_graft_alloc_from_children(children
,
1533 isl_set_copy(guard
), enforced
, build
, sub_build
);
1536 isl_ast_build
*for_build
;
1538 graft
= isl_ast_graft_insert_for(graft
, node
);
1539 for_build
= isl_ast_build_copy(build
);
1540 for_build
= isl_ast_build_replace_pending_by_guard(for_build
,
1541 isl_set_copy(guard
));
1543 graft
= refine_degenerate(graft
, for_build
, sub_build
);
1545 graft
= refine_generic(graft
, bounds
,
1547 isl_ast_build_free(for_build
);
1549 isl_set_free(guard
);
1551 graft
= after_each_for(graft
, body_build
);
1553 isl_ast_build_free(body_build
);
1554 isl_ast_build_free(sub_build
);
1555 isl_ast_build_free(build
);
1556 isl_basic_set_free(bounds
);
1557 isl_set_free(domain
);
1562 /* Internal data structure for checking if all constraints involving
1563 * the input dimension "depth" are such that the other coefficients
1564 * are multiples of "m", reducing "m" if they are not.
1565 * If "m" is reduced all the way down to "1", then the check has failed
1566 * and we break out of the iteration.
1568 struct isl_check_scaled_data
{
1573 /* If constraint "c" involves the input dimension data->depth,
1574 * then make sure that all the other coefficients are multiples of data->m,
1575 * reducing data->m if needed.
1576 * Break out of the iteration if data->m has become equal to "1".
1578 static isl_stat
constraint_check_scaled(__isl_take isl_constraint
*c
,
1581 struct isl_check_scaled_data
*data
= user
;
1583 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1586 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1587 isl_constraint_free(c
);
1591 for (i
= 0; i
< 4; ++i
) {
1592 n
= isl_constraint_dim(c
, t
[i
]);
1593 for (j
= 0; j
< n
; ++j
) {
1596 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1598 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1600 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1601 data
->m
= isl_val_gcd(data
->m
, d
);
1602 if (isl_val_is_one(data
->m
))
1609 isl_constraint_free(c
);
1611 return i
< 4 ? isl_stat_error
: isl_stat_ok
;
1614 /* For each constraint of "bmap" that involves the input dimension data->depth,
1615 * make sure that all the other coefficients are multiples of data->m,
1616 * reducing data->m if needed.
1617 * Break out of the iteration if data->m has become equal to "1".
1619 static isl_stat
basic_map_check_scaled(__isl_take isl_basic_map
*bmap
,
1624 r
= isl_basic_map_foreach_constraint(bmap
,
1625 &constraint_check_scaled
, user
);
1626 isl_basic_map_free(bmap
);
1631 /* For each constraint of "map" that involves the input dimension data->depth,
1632 * make sure that all the other coefficients are multiples of data->m,
1633 * reducing data->m if needed.
1634 * Break out of the iteration if data->m has become equal to "1".
1636 static isl_stat
map_check_scaled(__isl_take isl_map
*map
, void *user
)
1640 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1646 /* Create an AST node for the current dimension based on
1647 * the schedule domain "bounds" and return the node encapsulated
1648 * in an isl_ast_graft.
1650 * "executed" is the current inverse schedule, taking into account
1651 * the bounds in "bounds"
1652 * "domain" is the domain of "executed", with inner dimensions projected out.
1655 * Before moving on to the actual AST node construction in create_node_scaled,
1656 * we first check if the current dimension is strided and if we can scale
1657 * down this stride. Note that we only do this if the ast_build_scale_strides
1660 * In particular, let the current dimension take on values
1664 * with a an integer. We check if we can find an integer m that (obviously)
1665 * divides both f and s.
1667 * If so, we check if the current dimension only appears in constraints
1668 * where the coefficients of the other variables are multiples of m.
1669 * We perform this extra check to avoid the risk of introducing
1670 * divisions by scaling down the current dimension.
1672 * If so, we scale the current dimension down by a factor of m.
1673 * That is, we plug in
1677 * Note that in principle we could always scale down strided loops
1682 * but this may result in i' taking on larger values than the original i,
1683 * due to the shift by "f".
1684 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1686 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1687 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1688 __isl_take isl_ast_build
*build
)
1690 struct isl_check_scaled_data data
;
1695 ctx
= isl_ast_build_get_ctx(build
);
1696 if (!isl_options_get_ast_build_scale_strides(ctx
))
1697 return create_node_scaled(executed
, bounds
, domain
, build
);
1699 data
.depth
= isl_ast_build_get_depth(build
);
1700 if (!isl_ast_build_has_stride(build
, data
.depth
))
1701 return create_node_scaled(executed
, bounds
, domain
, build
);
1703 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1704 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1706 offset
= isl_aff_free(offset
);
1707 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1708 d
= isl_aff_get_denominator_val(offset
);
1710 executed
= isl_union_map_free(executed
);
1712 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1713 data
.m
= isl_val_div(data
.m
, d
);
1715 data
.m
= isl_val_set_si(data
.m
, 1);
1719 if (!isl_val_is_one(data
.m
)) {
1720 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1722 !isl_val_is_one(data
.m
))
1723 executed
= isl_union_map_free(executed
);
1726 if (!isl_val_is_one(data
.m
)) {
1731 isl_union_map
*umap
;
1733 space
= isl_ast_build_get_space(build
, 1);
1734 space
= isl_space_map_from_set(space
);
1735 ma
= isl_multi_aff_identity(space
);
1736 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1737 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1738 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1740 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1741 isl_multi_aff_copy(ma
));
1742 domain
= isl_set_preimage_multi_aff(domain
,
1743 isl_multi_aff_copy(ma
));
1744 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1745 umap
= isl_union_map_from_map(map
);
1746 executed
= isl_union_map_apply_domain(executed
,
1747 isl_union_map_copy(umap
));
1748 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1751 isl_aff_free(offset
);
1752 isl_val_free(data
.m
);
1754 return create_node_scaled(executed
, bounds
, domain
, build
);
1757 /* Add the basic set to the list that "user" points to.
1759 static isl_stat
collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1761 isl_basic_set_list
**list
= user
;
1763 *list
= isl_basic_set_list_add(*list
, bset
);
1768 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1770 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1771 __isl_take isl_set
*set
)
1775 isl_basic_set_list
*list
;
1780 ctx
= isl_set_get_ctx(set
);
1782 n
= isl_set_n_basic_set(set
);
1783 list
= isl_basic_set_list_alloc(ctx
, n
);
1784 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1785 list
= isl_basic_set_list_free(list
);
1791 /* Generate code for the schedule domain "bounds"
1792 * and add the result to "list".
1794 * We mainly detect strides here and check if the bounds do not
1795 * conflict with the current build domain
1796 * and then pass over control to create_node.
1798 * "bounds" reflects the bounds on the current dimension and possibly
1799 * some extra conditions on outer dimensions.
1800 * It does not, however, include any divs involving the current dimension,
1801 * so it does not capture any stride constraints.
1802 * We therefore need to compute that part of the schedule domain that
1803 * intersects with "bounds" and derive the strides from the result.
1805 static __isl_give isl_ast_graft_list
*add_node(
1806 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1807 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1809 isl_ast_graft
*graft
;
1810 isl_set
*domain
= NULL
;
1811 isl_union_set
*uset
;
1812 int empty
, disjoint
;
1814 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1815 executed
= isl_union_map_intersect_domain(executed
, uset
);
1816 empty
= isl_union_map_is_empty(executed
);
1822 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1823 domain
= isl_set_from_union_set(uset
);
1824 domain
= isl_ast_build_specialize(build
, domain
);
1826 domain
= isl_set_compute_divs(domain
);
1827 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1828 disjoint
= isl_set_is_disjoint(domain
, build
->domain
);
1834 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1836 graft
= create_node(executed
, bounds
, domain
,
1837 isl_ast_build_copy(build
));
1838 list
= isl_ast_graft_list_add(list
, graft
);
1839 isl_ast_build_free(build
);
1842 list
= isl_ast_graft_list_free(list
);
1844 isl_set_free(domain
);
1845 isl_basic_set_free(bounds
);
1846 isl_union_map_free(executed
);
1847 isl_ast_build_free(build
);
1851 /* Does any element of i follow or coincide with any element of j
1852 * at the current depth for equal values of the outer dimensions?
1854 static isl_bool
domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1855 __isl_keep isl_basic_set
*j
, void *user
)
1857 int depth
= *(int *) user
;
1858 isl_basic_map
*test
;
1862 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1863 isl_basic_set_copy(j
));
1864 for (l
= 0; l
< depth
; ++l
)
1865 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1867 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1868 isl_dim_out
, depth
);
1869 empty
= isl_basic_map_is_empty(test
);
1870 isl_basic_map_free(test
);
1872 return isl_bool_not(empty
);
1875 /* Split up each element of "list" into a part that is related to "bset"
1876 * according to "gt" and a part that is not.
1877 * Return a list that consist of "bset" and all the pieces.
1879 static __isl_give isl_basic_set_list
*add_split_on(
1880 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1881 __isl_keep isl_basic_map
*gt
)
1884 isl_basic_set_list
*res
;
1887 bset
= isl_basic_set_free(bset
);
1889 gt
= isl_basic_map_copy(gt
);
1890 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1891 n
= isl_basic_set_list_n_basic_set(list
);
1892 res
= isl_basic_set_list_from_basic_set(bset
);
1893 for (i
= 0; res
&& i
< n
; ++i
) {
1894 isl_basic_set
*bset
;
1895 isl_set
*set1
, *set2
;
1896 isl_basic_map
*bmap
;
1899 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1900 bmap
= isl_basic_map_copy(gt
);
1901 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1902 bset
= isl_basic_map_range(bmap
);
1903 empty
= isl_basic_set_is_empty(bset
);
1905 res
= isl_basic_set_list_free(res
);
1907 isl_basic_set_free(bset
);
1908 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1909 res
= isl_basic_set_list_add(res
, bset
);
1913 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1914 set1
= isl_set_from_basic_set(bset
);
1915 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1916 set2
= isl_set_from_basic_set(bset
);
1917 set1
= isl_set_subtract(set2
, set1
);
1918 set1
= isl_set_make_disjoint(set1
);
1920 res
= isl_basic_set_list_concat(res
,
1921 isl_basic_set_list_from_set(set1
));
1923 isl_basic_map_free(gt
);
1924 isl_basic_set_list_free(list
);
1928 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1929 __isl_keep isl_basic_set_list
*domain_list
,
1930 __isl_keep isl_union_map
*executed
,
1931 __isl_keep isl_ast_build
*build
);
1933 /* Internal data structure for add_nodes.
1935 * "executed" and "build" are extra arguments to be passed to add_node.
1936 * "list" collects the results.
1938 struct isl_add_nodes_data
{
1939 isl_union_map
*executed
;
1940 isl_ast_build
*build
;
1942 isl_ast_graft_list
*list
;
1945 /* Generate code for the schedule domains in "scc"
1946 * and add the results to "list".
1948 * The domains in "scc" form a strongly connected component in the ordering.
1949 * If the number of domains in "scc" is larger than 1, then this means
1950 * that we cannot determine a valid ordering for the domains in the component.
1951 * This should be fairly rare because the individual domains
1952 * have been made disjoint first.
1953 * The problem is that the domains may be integrally disjoint but not
1954 * rationally disjoint. For example, we may have domains
1956 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1958 * These two domains have an empty intersection, but their rational
1959 * relaxations do intersect. It is impossible to order these domains
1960 * in the second dimension because the first should be ordered before
1961 * the second for outer dimension equal to 0, while it should be ordered
1962 * after for outer dimension equal to 1.
1964 * This may happen in particular in case of unrolling since the domain
1965 * of each slice is replaced by its simple hull.
1967 * For each basic set i in "scc" and for each of the following basic sets j,
1968 * we split off that part of the basic set i that shares the outer dimensions
1969 * with j and lies before j in the current dimension.
1970 * We collect all the pieces in a new list that replaces "scc".
1972 * While the elements in "scc" should be disjoint, we double-check
1973 * this property to avoid running into an infinite recursion in case
1974 * they intersect due to some internal error.
1976 static isl_stat
add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1978 struct isl_add_nodes_data
*data
= user
;
1980 isl_basic_set
*bset
, *first
;
1981 isl_basic_set_list
*list
;
1985 n
= isl_basic_set_list_n_basic_set(scc
);
1986 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
1988 isl_basic_set_list_free(scc
);
1989 data
->list
= add_node(data
->list
,
1990 isl_union_map_copy(data
->executed
), bset
,
1991 isl_ast_build_copy(data
->build
));
1992 return data
->list
? isl_stat_ok
: isl_stat_error
;
1995 depth
= isl_ast_build_get_depth(data
->build
);
1996 space
= isl_basic_set_get_space(bset
);
1997 space
= isl_space_map_from_set(space
);
1998 gt
= isl_basic_map_universe(space
);
1999 for (i
= 0; i
< depth
; ++i
)
2000 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
2001 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
2003 first
= isl_basic_set_copy(bset
);
2004 list
= isl_basic_set_list_from_basic_set(bset
);
2005 for (i
= 1; i
< n
; ++i
) {
2008 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
2010 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
2012 list
= isl_basic_set_list_free(list
);
2014 isl_die(isl_basic_set_list_get_ctx(scc
),
2016 "basic sets in scc are assumed to be disjoint",
2017 list
= isl_basic_set_list_free(list
));
2019 list
= add_split_on(list
, bset
, gt
);
2021 isl_basic_set_free(first
);
2022 isl_basic_map_free(gt
);
2023 isl_basic_set_list_free(scc
);
2025 data
->list
= isl_ast_graft_list_concat(data
->list
,
2026 generate_sorted_domains(scc
, data
->executed
, data
->build
));
2027 isl_basic_set_list_free(scc
);
2029 return data
->list
? isl_stat_ok
: isl_stat_error
;
2032 /* Sort the domains in "domain_list" according to the execution order
2033 * at the current depth (for equal values of the outer dimensions),
2034 * generate code for each of them, collecting the results in a list.
2035 * If no code is generated (because the intersection of the inverse schedule
2036 * with the domains turns out to be empty), then an empty list is returned.
2038 * The caller is responsible for ensuring that the basic sets in "domain_list"
2039 * are pair-wise disjoint. It can, however, in principle happen that
2040 * two basic sets should be ordered one way for one value of the outer
2041 * dimensions and the other way for some other value of the outer dimensions.
2042 * We therefore play safe and look for strongly connected components.
2043 * The function add_nodes takes care of handling non-trivial components.
2045 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
2046 __isl_keep isl_basic_set_list
*domain_list
,
2047 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2050 struct isl_add_nodes_data data
;
2057 ctx
= isl_basic_set_list_get_ctx(domain_list
);
2058 n
= isl_basic_set_list_n_basic_set(domain_list
);
2059 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
2063 return add_node(data
.list
, isl_union_map_copy(executed
),
2064 isl_basic_set_list_get_basic_set(domain_list
, 0),
2065 isl_ast_build_copy(build
));
2067 depth
= isl_ast_build_get_depth(build
);
2068 data
.executed
= executed
;
2070 if (isl_basic_set_list_foreach_scc(domain_list
,
2071 &domain_follows_at_depth
, &depth
,
2072 &add_nodes
, &data
) < 0)
2073 data
.list
= isl_ast_graft_list_free(data
.list
);
2078 /* Do i and j share any values for the outer dimensions?
2080 static isl_bool
shared_outer(__isl_keep isl_basic_set
*i
,
2081 __isl_keep isl_basic_set
*j
, void *user
)
2083 int depth
= *(int *) user
;
2084 isl_basic_map
*test
;
2088 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
2089 isl_basic_set_copy(j
));
2090 for (l
= 0; l
< depth
; ++l
)
2091 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
2093 empty
= isl_basic_map_is_empty(test
);
2094 isl_basic_map_free(test
);
2096 return isl_bool_not(empty
);
2099 /* Internal data structure for generate_sorted_domains_wrap.
2101 * "n" is the total number of basic sets
2102 * "executed" and "build" are extra arguments to be passed
2103 * to generate_sorted_domains.
2105 * "single" is set to 1 by generate_sorted_domains_wrap if there
2106 * is only a single component.
2107 * "list" collects the results.
2109 struct isl_ast_generate_parallel_domains_data
{
2111 isl_union_map
*executed
;
2112 isl_ast_build
*build
;
2115 isl_ast_graft_list
*list
;
2118 /* Call generate_sorted_domains on "scc", fuse the result into a list
2119 * with either zero or one graft and collect the these single element
2120 * lists into data->list.
2122 * If there is only one component, i.e., if the number of basic sets
2123 * in the current component is equal to the total number of basic sets,
2124 * then data->single is set to 1 and the result of generate_sorted_domains
2127 static isl_stat
generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
2130 struct isl_ast_generate_parallel_domains_data
*data
= user
;
2131 isl_ast_graft_list
*list
;
2133 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
2134 data
->single
= isl_basic_set_list_n_basic_set(scc
) == data
->n
;
2136 list
= isl_ast_graft_list_fuse(list
, data
->build
);
2140 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
2142 isl_basic_set_list_free(scc
);
2144 return isl_stat_error
;
2149 /* Look for any (weakly connected) components in the "domain_list"
2150 * of domains that share some values of the outer dimensions.
2151 * That is, domains in different components do not share any values
2152 * of the outer dimensions. This means that these components
2153 * can be freely reordered.
2154 * Within each of the components, we sort the domains according
2155 * to the execution order at the current depth.
2157 * If there is more than one component, then generate_sorted_domains_wrap
2158 * fuses the result of each call to generate_sorted_domains
2159 * into a list with either zero or one graft and collects these (at most)
2160 * single element lists into a bigger list. This means that the elements of the
2161 * final list can be freely reordered. In particular, we sort them
2162 * according to an arbitrary but fixed ordering to ease merging of
2163 * graft lists from different components.
2165 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2166 __isl_keep isl_basic_set_list
*domain_list
,
2167 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2170 struct isl_ast_generate_parallel_domains_data data
;
2175 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2177 return generate_sorted_domains(domain_list
, executed
, build
);
2179 depth
= isl_ast_build_get_depth(build
);
2181 data
.executed
= executed
;
2184 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2185 &generate_sorted_domains_wrap
,
2187 data
.list
= isl_ast_graft_list_free(data
.list
);
2190 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2195 /* Internal data for separate_domain.
2197 * "explicit" is set if we only want to use explicit bounds.
2199 * "domain" collects the separated domains.
2201 struct isl_separate_domain_data
{
2202 isl_ast_build
*build
;
2207 /* Extract implicit bounds on the current dimension for the executed "map".
2209 * The domain of "map" may involve inner dimensions, so we
2210 * need to eliminate them.
2212 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2213 __isl_keep isl_ast_build
*build
)
2217 domain
= isl_map_domain(map
);
2218 domain
= isl_ast_build_eliminate(build
, domain
);
2223 /* Extract explicit bounds on the current dimension for the executed "map".
2225 * Rather than eliminating the inner dimensions as in implicit_bounds,
2226 * we simply drop any constraints involving those inner dimensions.
2227 * The idea is that most bounds that are implied by constraints on the
2228 * inner dimensions will be enforced by for loops and not by explicit guards.
2229 * There is then no need to separate along those bounds.
2231 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2232 __isl_keep isl_ast_build
*build
)
2237 dim
= isl_map_dim(map
, isl_dim_out
);
2238 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2240 domain
= isl_map_domain(map
);
2241 depth
= isl_ast_build_get_depth(build
);
2242 dim
= isl_set_dim(domain
, isl_dim_set
);
2243 domain
= isl_set_detect_equalities(domain
);
2244 domain
= isl_set_drop_constraints_involving_dims(domain
,
2245 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2246 domain
= isl_set_remove_divs_involving_dims(domain
,
2247 isl_dim_set
, depth
, 1);
2248 domain
= isl_set_remove_unknown_divs(domain
);
2253 /* Split data->domain into pieces that intersect with the range of "map"
2254 * and pieces that do not intersect with the range of "map"
2255 * and then add that part of the range of "map" that does not intersect
2256 * with data->domain.
2258 static isl_stat
separate_domain(__isl_take isl_map
*map
, void *user
)
2260 struct isl_separate_domain_data
*data
= user
;
2265 domain
= explicit_bounds(map
, data
->build
);
2267 domain
= implicit_bounds(map
, data
->build
);
2269 domain
= isl_set_coalesce(domain
);
2270 domain
= isl_set_make_disjoint(domain
);
2271 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2272 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2273 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2274 data
->domain
= isl_set_union(data
->domain
, d1
);
2275 data
->domain
= isl_set_union(data
->domain
, d2
);
2280 /* Separate the schedule domains of "executed".
2282 * That is, break up the domain of "executed" into basic sets,
2283 * such that for each basic set S, every element in S is associated with
2284 * the same domain spaces.
2286 * "space" is the (single) domain space of "executed".
2288 static __isl_give isl_set
*separate_schedule_domains(
2289 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2290 __isl_keep isl_ast_build
*build
)
2292 struct isl_separate_domain_data data
= { build
};
2295 ctx
= isl_ast_build_get_ctx(build
);
2296 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2297 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2298 data
.domain
= isl_set_empty(space
);
2299 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2300 data
.domain
= isl_set_free(data
.domain
);
2302 isl_union_map_free(executed
);
2306 /* Temporary data used during the search for a lower bound for unrolling.
2308 * "build" is the build in which the unrolling will be performed
2309 * "domain" is the original set for which to find a lower bound
2310 * "depth" is the dimension for which to find a lower boudn
2311 * "expansion" is the expansion that needs to be applied to "domain"
2312 * in the unrolling that will be performed
2314 * "lower" is the best lower bound found so far. It is NULL if we have not
2316 * "n" is the corresponding size. If lower is NULL, then the value of n
2318 * "n_div" is the maximal number of integer divisions in the first
2319 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2320 * been computed yet.
2322 struct isl_find_unroll_data
{
2323 isl_ast_build
*build
;
2326 isl_basic_map
*expansion
;
2333 /* Return the constraint
2335 * i_"depth" = aff + offset
2337 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2340 aff
= isl_aff_copy(aff
);
2341 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2342 aff
= isl_aff_add_constant_si(aff
, offset
);
2343 return isl_equality_from_aff(aff
);
2346 /* Update *user to the number of integer divsions in the first element
2347 * of "ma", if it is larger than the current value.
2349 static isl_stat
update_n_div(__isl_take isl_set
*set
,
2350 __isl_take isl_multi_aff
*ma
, void *user
)
2356 aff
= isl_multi_aff_get_aff(ma
, 0);
2357 n_div
= isl_aff_dim(aff
, isl_dim_div
);
2359 isl_multi_aff_free(ma
);
2365 return aff
? isl_stat_ok
: isl_stat_error
;
2368 /* Get the number of integer divisions in the expression for the iterator
2369 * value at the first slice in the unrolling based on lower bound "lower",
2370 * taking into account the expansion that needs to be performed on this slice.
2372 static int get_expanded_n_div(struct isl_find_unroll_data
*data
,
2373 __isl_keep isl_aff
*lower
)
2377 isl_map
*it_map
, *expansion
;
2378 isl_pw_multi_aff
*pma
;
2381 c
= at_offset(data
->depth
, lower
, 0);
2382 set
= isl_set_copy(data
->domain
);
2383 set
= isl_set_add_constraint(set
, c
);
2384 expansion
= isl_map_from_basic_map(isl_basic_map_copy(data
->expansion
));
2385 set
= isl_set_apply(set
, expansion
);
2386 it_map
= isl_ast_build_map_to_iterator(data
->build
, set
);
2387 pma
= isl_pw_multi_aff_from_map(it_map
);
2389 if (isl_pw_multi_aff_foreach_piece(pma
, &update_n_div
, &n
) < 0)
2391 isl_pw_multi_aff_free(pma
);
2396 /* Is the lower bound "lower" with corresponding iteration count "n"
2397 * better than the one stored in "data"?
2398 * If there is no upper bound on the iteration count ("n" is infinity) or
2399 * if the count is too large, then we cannot use this lower bound.
2400 * Otherwise, if there was no previous lower bound or
2401 * if the iteration count of the new lower bound is smaller than
2402 * the iteration count of the previous lower bound, then we consider
2403 * the new lower bound to be better.
2404 * If the iteration count is the same, then compare the number
2405 * of integer divisions that would be needed to express
2406 * the iterator value at the first slice in the unrolling
2407 * according to the lower bound. If we end up computing this
2408 * number, then store the lowest value in data->n_div.
2410 static int is_better_lower_bound(struct isl_find_unroll_data
*data
,
2411 __isl_keep isl_aff
*lower
, __isl_keep isl_val
*n
)
2418 if (isl_val_is_infty(n
))
2420 if (isl_val_cmp_si(n
, INT_MAX
) > 0)
2424 cmp
= isl_val_cmp_si(n
, *data
->n
);
2429 if (data
->n_div
< 0)
2430 data
->n_div
= get_expanded_n_div(data
, data
->lower
);
2431 if (data
->n_div
< 0)
2433 if (data
->n_div
== 0)
2435 n_div
= get_expanded_n_div(data
, lower
);
2438 if (n_div
>= data
->n_div
)
2440 data
->n_div
= n_div
;
2445 /* Check if we can use "c" as a lower bound and if it is better than
2446 * any previously found lower bound.
2448 * If "c" does not involve the dimension at the current depth,
2449 * then we cannot use it.
2450 * Otherwise, let "c" be of the form
2454 * We compute the maximal value of
2456 * -ceil(f(j)/a)) + i + 1
2458 * over the domain. If there is such a value "n", then we know
2460 * -ceil(f(j)/a)) + i + 1 <= n
2464 * i < ceil(f(j)/a)) + n
2466 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2467 * We just need to check if we have found any lower bound before and
2468 * if the new lower bound is better (smaller n or fewer integer divisions)
2469 * than the previously found lower bounds.
2471 static isl_stat
update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2472 __isl_keep isl_constraint
*c
)
2474 isl_aff
*aff
, *lower
;
2478 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2481 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2482 lower
= isl_aff_ceil(lower
);
2483 aff
= isl_aff_copy(lower
);
2484 aff
= isl_aff_neg(aff
);
2485 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2486 aff
= isl_aff_add_constant_si(aff
, 1);
2487 max
= isl_set_max_val(data
->domain
, aff
);
2490 better
= is_better_lower_bound(data
, lower
, max
);
2491 if (better
< 0 || !better
) {
2493 isl_aff_free(lower
);
2494 return better
< 0 ? isl_stat_error
: isl_stat_ok
;
2497 isl_aff_free(data
->lower
);
2498 data
->lower
= lower
;
2499 *data
->n
= isl_val_get_num_si(max
);
2505 /* Check if we can use "c" as a lower bound and if it is better than
2506 * any previously found lower bound.
2508 static isl_stat
constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2510 struct isl_find_unroll_data
*data
;
2513 data
= (struct isl_find_unroll_data
*) user
;
2514 r
= update_unrolling_lower_bound(data
, c
);
2515 isl_constraint_free(c
);
2520 /* Look for a lower bound l(i) on the dimension at "depth"
2521 * and a size n such that "domain" is a subset of
2523 * { [i] : l(i) <= i_d < l(i) + n }
2525 * where d is "depth" and l(i) depends only on earlier dimensions.
2526 * Furthermore, try and find a lower bound such that n is as small as possible.
2527 * In particular, "n" needs to be finite.
2528 * "build" is the build in which the unrolling will be performed.
2529 * "expansion" is the expansion that needs to be applied to "domain"
2530 * in the unrolling that will be performed.
2532 * Inner dimensions have been eliminated from "domain" by the caller.
2534 * We first construct a collection of lower bounds on the input set
2535 * by computing its simple hull. We then iterate through them,
2536 * discarding those that we cannot use (either because they do not
2537 * involve the dimension at "depth" or because they have no corresponding
2538 * upper bound, meaning that "n" would be unbounded) and pick out the
2539 * best from the remaining ones.
2541 * If we cannot find a suitable lower bound, then we consider that
2544 static __isl_give isl_aff
*find_unroll_lower_bound(
2545 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*domain
,
2546 int depth
, __isl_keep isl_basic_map
*expansion
, int *n
)
2548 struct isl_find_unroll_data data
=
2549 { build
, domain
, depth
, expansion
, NULL
, n
, -1 };
2550 isl_basic_set
*hull
;
2552 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2554 if (isl_basic_set_foreach_constraint(hull
,
2555 &constraint_find_unroll
, &data
) < 0)
2558 isl_basic_set_free(hull
);
2561 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2562 "cannot find lower bound for unrolling", return NULL
);
2566 isl_basic_set_free(hull
);
2567 return isl_aff_free(data
.lower
);
2570 /* Call "fn" on each iteration of the current dimension of "domain".
2571 * If "init" is not NULL, then it is called with the number of
2572 * iterations before any call to "fn".
2573 * Return -1 on failure.
2575 * Since we are going to be iterating over the individual values,
2576 * we first check if there are any strides on the current dimension.
2577 * If there is, we rewrite the current dimension i as
2579 * i = stride i' + offset
2581 * and then iterate over individual values of i' instead.
2583 * We then look for a lower bound on i' and a size such that the domain
2586 * { [j,i'] : l(j) <= i' < l(j) + n }
2588 * and then take slices of the domain at values of i'
2589 * between l(j) and l(j) + n - 1.
2591 * We compute the unshifted simple hull of each slice to ensure that
2592 * we have a single basic set per offset. The slicing constraint
2593 * may get simplified away before the unshifted simple hull is taken
2594 * and may therefore in some rare cases disappear from the result.
2595 * We therefore explicitly add the constraint back after computing
2596 * the unshifted simple hull to ensure that the basic sets
2597 * remain disjoint. The constraints that are dropped by taking the hull
2598 * will be taken into account at the next level, as in the case of the
2601 * Finally, we map i' back to i and call "fn".
2603 static int foreach_iteration(__isl_take isl_set
*domain
,
2604 __isl_keep isl_ast_build
*build
, int (*init
)(int n
, void *user
),
2605 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
2610 isl_multi_aff
*expansion
;
2611 isl_basic_map
*bmap
;
2612 isl_aff
*lower
= NULL
;
2613 isl_ast_build
*stride_build
;
2615 depth
= isl_ast_build_get_depth(build
);
2617 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2618 domain
= isl_set_intersect(domain
, isl_ast_build_get_domain(build
));
2619 stride_build
= isl_ast_build_copy(build
);
2620 stride_build
= isl_ast_build_detect_strides(stride_build
,
2621 isl_set_copy(domain
));
2622 expansion
= isl_ast_build_get_stride_expansion(stride_build
);
2624 domain
= isl_set_preimage_multi_aff(domain
,
2625 isl_multi_aff_copy(expansion
));
2626 domain
= isl_ast_build_eliminate_divs(stride_build
, domain
);
2627 isl_ast_build_free(stride_build
);
2629 bmap
= isl_basic_map_from_multi_aff(expansion
);
2631 empty
= isl_set_is_empty(domain
);
2637 lower
= find_unroll_lower_bound(build
, domain
, depth
, bmap
, &n
);
2641 if (n
>= 0 && init
&& init(n
, user
) < 0)
2643 for (i
= 0; i
< n
; ++i
) {
2645 isl_basic_set
*bset
;
2646 isl_constraint
*slice
;
2648 slice
= at_offset(depth
, lower
, i
);
2649 set
= isl_set_copy(domain
);
2650 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2651 bset
= isl_set_unshifted_simple_hull(set
);
2652 bset
= isl_basic_set_add_constraint(bset
, slice
);
2653 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2655 if (fn(bset
, user
) < 0)
2659 isl_aff_free(lower
);
2660 isl_set_free(domain
);
2661 isl_basic_map_free(bmap
);
2663 return n
< 0 || i
< n
? -1 : 0;
2666 /* Data structure for storing the results and the intermediate objects
2667 * of compute_domains.
2669 * "list" is the main result of the function and contains a list
2670 * of disjoint basic sets for which code should be generated.
2672 * "executed" and "build" are inputs to compute_domains.
2673 * "schedule_domain" is the domain of "executed".
2675 * "option" contains the domains at the current depth that should by
2676 * atomic, separated or unrolled. These domains are as specified by
2677 * the user, except that inner dimensions have been eliminated and
2678 * that they have been made pair-wise disjoint.
2680 * "sep_class" contains the user-specified split into separation classes
2681 * specialized to the current depth.
2682 * "done" contains the union of the separation domains that have already
2685 struct isl_codegen_domains
{
2686 isl_basic_set_list
*list
;
2688 isl_union_map
*executed
;
2689 isl_ast_build
*build
;
2690 isl_set
*schedule_domain
;
2698 /* Internal data structure for do_unroll.
2700 * "domains" stores the results of compute_domains.
2701 * "class_domain" is the original class domain passed to do_unroll.
2702 * "unroll_domain" collects the unrolled iterations.
2704 struct isl_ast_unroll_data
{
2705 struct isl_codegen_domains
*domains
;
2706 isl_set
*class_domain
;
2707 isl_set
*unroll_domain
;
2710 /* Given an iteration of an unrolled domain represented by "bset",
2711 * add it to data->domains->list.
2712 * Since we may have dropped some constraints, we intersect with
2713 * the class domain again to ensure that each element in the list
2714 * is disjoint from the other class domains.
2716 static int do_unroll_iteration(__isl_take isl_basic_set
*bset
, void *user
)
2718 struct isl_ast_unroll_data
*data
= user
;
2720 isl_basic_set_list
*list
;
2722 set
= isl_set_from_basic_set(bset
);
2723 data
->unroll_domain
= isl_set_union(data
->unroll_domain
,
2725 set
= isl_set_intersect(set
, isl_set_copy(data
->class_domain
));
2726 set
= isl_set_make_disjoint(set
);
2727 list
= isl_basic_set_list_from_set(set
);
2728 data
->domains
->list
= isl_basic_set_list_concat(data
->domains
->list
,
2734 /* Extend domains->list with a list of basic sets, one for each value
2735 * of the current dimension in "domain" and remove the corresponding
2736 * sets from the class domain. Return the updated class domain.
2737 * The divs that involve the current dimension have not been projected out
2740 * We call foreach_iteration to iterate over the individual values and
2741 * in do_unroll_iteration we collect the individual basic sets in
2742 * domains->list and their union in data->unroll_domain, which is then
2743 * used to update the class domain.
2745 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2746 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2748 struct isl_ast_unroll_data data
;
2751 return isl_set_free(class_domain
);
2753 return isl_set_free(domain
);
2755 data
.domains
= domains
;
2756 data
.class_domain
= class_domain
;
2757 data
.unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2759 if (foreach_iteration(domain
, domains
->build
, NULL
,
2760 &do_unroll_iteration
, &data
) < 0)
2761 data
.unroll_domain
= isl_set_free(data
.unroll_domain
);
2763 class_domain
= isl_set_subtract(class_domain
, data
.unroll_domain
);
2765 return class_domain
;
2768 /* Add domains to domains->list for each individual value of the current
2769 * dimension, for that part of the schedule domain that lies in the
2770 * intersection of the option domain and the class domain.
2771 * Remove the corresponding sets from the class domain and
2772 * return the updated class domain.
2774 * We first break up the unroll option domain into individual pieces
2775 * and then handle each of them separately. The unroll option domain
2776 * has been made disjoint in compute_domains_init_options,
2778 * Note that we actively want to combine different pieces of the
2779 * schedule domain that have the same value at the current dimension.
2780 * We therefore need to break up the unroll option domain before
2781 * intersecting with class and schedule domain, hoping that the
2782 * unroll option domain specified by the user is relatively simple.
2784 static __isl_give isl_set
*compute_unroll_domains(
2785 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2787 isl_set
*unroll_domain
;
2788 isl_basic_set_list
*unroll_list
;
2792 empty
= isl_set_is_empty(domains
->option
[isl_ast_loop_unroll
]);
2794 return isl_set_free(class_domain
);
2796 return class_domain
;
2798 unroll_domain
= isl_set_copy(domains
->option
[isl_ast_loop_unroll
]);
2799 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2801 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2802 for (i
= 0; i
< n
; ++i
) {
2803 isl_basic_set
*bset
;
2805 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2806 unroll_domain
= isl_set_from_basic_set(bset
);
2807 unroll_domain
= isl_set_intersect(unroll_domain
,
2808 isl_set_copy(class_domain
));
2809 unroll_domain
= isl_set_intersect(unroll_domain
,
2810 isl_set_copy(domains
->schedule_domain
));
2812 empty
= isl_set_is_empty(unroll_domain
);
2813 if (empty
>= 0 && empty
) {
2814 isl_set_free(unroll_domain
);
2818 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2821 isl_basic_set_list_free(unroll_list
);
2823 return class_domain
;
2826 /* Try and construct a single basic set that includes the intersection of
2827 * the schedule domain, the atomic option domain and the class domain.
2828 * Add the resulting basic set(s) to domains->list and remove them
2829 * from class_domain. Return the updated class domain.
2831 * We construct a single domain rather than trying to combine
2832 * the schedule domains of individual domains because we are working
2833 * within a single component so that non-overlapping schedule domains
2834 * should already have been separated.
2835 * We do however need to make sure that this single domains is a subset
2836 * of the class domain so that it would not intersect with any other
2837 * class domains. This means that we may end up splitting up the atomic
2838 * domain in case separation classes are being used.
2840 * "domain" is the intersection of the schedule domain and the class domain,
2841 * with inner dimensions projected out.
2843 static __isl_give isl_set
*compute_atomic_domain(
2844 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2846 isl_basic_set
*bset
;
2847 isl_basic_set_list
*list
;
2848 isl_set
*domain
, *atomic_domain
;
2851 domain
= isl_set_copy(domains
->option
[isl_ast_loop_atomic
]);
2852 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2853 domain
= isl_set_intersect(domain
,
2854 isl_set_copy(domains
->schedule_domain
));
2855 empty
= isl_set_is_empty(domain
);
2857 class_domain
= isl_set_free(class_domain
);
2859 isl_set_free(domain
);
2860 return class_domain
;
2863 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2864 domain
= isl_set_coalesce_preserve(domain
);
2865 bset
= isl_set_unshifted_simple_hull(domain
);
2866 domain
= isl_set_from_basic_set(bset
);
2867 atomic_domain
= isl_set_copy(domain
);
2868 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2869 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2870 domain
= isl_set_make_disjoint(domain
);
2871 list
= isl_basic_set_list_from_set(domain
);
2872 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2874 return class_domain
;
2877 /* Split up the schedule domain into uniform basic sets,
2878 * in the sense that each element in a basic set is associated to
2879 * elements of the same domains, and add the result to domains->list.
2880 * Do this for that part of the schedule domain that lies in the
2881 * intersection of "class_domain" and the separate option domain.
2883 * "class_domain" may or may not include the constraints
2884 * of the schedule domain, but this does not make a difference
2885 * since we are going to intersect it with the domain of the inverse schedule.
2886 * If it includes schedule domain constraints, then they may involve
2887 * inner dimensions, but we will eliminate them in separation_domain.
2889 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2890 __isl_keep isl_set
*class_domain
)
2894 isl_union_map
*executed
;
2895 isl_basic_set_list
*list
;
2898 domain
= isl_set_copy(domains
->option
[isl_ast_loop_separate
]);
2899 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2900 executed
= isl_union_map_copy(domains
->executed
);
2901 executed
= isl_union_map_intersect_domain(executed
,
2902 isl_union_set_from_set(domain
));
2903 empty
= isl_union_map_is_empty(executed
);
2904 if (empty
< 0 || empty
) {
2905 isl_union_map_free(executed
);
2906 return empty
< 0 ? -1 : 0;
2909 space
= isl_set_get_space(class_domain
);
2910 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2912 list
= isl_basic_set_list_from_set(domain
);
2913 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2918 /* Split up the domain at the current depth into disjoint
2919 * basic sets for which code should be generated separately
2920 * for the given separation class domain.
2922 * If any separation classes have been defined, then "class_domain"
2923 * is the domain of the current class and does not refer to inner dimensions.
2924 * Otherwise, "class_domain" is the universe domain.
2926 * We first make sure that the class domain is disjoint from
2927 * previously considered class domains.
2929 * The separate domains can be computed directly from the "class_domain".
2931 * The unroll, atomic and remainder domains need the constraints
2932 * from the schedule domain.
2934 * For unrolling, the actual schedule domain is needed (with divs that
2935 * may refer to the current dimension) so that stride detection can be
2938 * For atomic and remainder domains, inner dimensions and divs involving
2939 * the current dimensions should be eliminated.
2940 * In case we are working within a separation class, we need to intersect
2941 * the result with the current "class_domain" to ensure that the domains
2942 * are disjoint from those generated from other class domains.
2944 * The domain that has been made atomic may be larger than specified
2945 * by the user since it needs to be representable as a single basic set.
2946 * This possibly larger domain is removed from class_domain by
2947 * compute_atomic_domain. It is computed first so that the extended domain
2948 * would not overlap with any domains computed before.
2949 * Similary, the unrolled domains may have some constraints removed and
2950 * may therefore also be larger than specified by the user.
2952 * If anything is left after handling separate, unroll and atomic,
2953 * we split it up into basic sets and append the basic sets to domains->list.
2955 static isl_stat
compute_partial_domains(struct isl_codegen_domains
*domains
,
2956 __isl_take isl_set
*class_domain
)
2958 isl_basic_set_list
*list
;
2961 class_domain
= isl_set_subtract(class_domain
,
2962 isl_set_copy(domains
->done
));
2963 domains
->done
= isl_set_union(domains
->done
,
2964 isl_set_copy(class_domain
));
2966 class_domain
= compute_atomic_domain(domains
, class_domain
);
2967 class_domain
= compute_unroll_domains(domains
, class_domain
);
2969 domain
= isl_set_copy(class_domain
);
2971 if (compute_separate_domain(domains
, domain
) < 0)
2973 domain
= isl_set_subtract(domain
,
2974 isl_set_copy(domains
->option
[isl_ast_loop_separate
]));
2976 domain
= isl_set_intersect(domain
,
2977 isl_set_copy(domains
->schedule_domain
));
2979 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2980 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2982 domain
= isl_set_coalesce_preserve(domain
);
2983 domain
= isl_set_make_disjoint(domain
);
2985 list
= isl_basic_set_list_from_set(domain
);
2986 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2988 isl_set_free(class_domain
);
2992 isl_set_free(domain
);
2993 isl_set_free(class_domain
);
2994 return isl_stat_error
;
2997 /* Split up the domain at the current depth into disjoint
2998 * basic sets for which code should be generated separately
2999 * for the separation class identified by "pnt".
3001 * We extract the corresponding class domain from domains->sep_class,
3002 * eliminate inner dimensions and pass control to compute_partial_domains.
3004 static isl_stat
compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
3006 struct isl_codegen_domains
*domains
= user
;
3011 class_set
= isl_set_from_point(pnt
);
3012 domain
= isl_map_domain(isl_map_intersect_range(
3013 isl_map_copy(domains
->sep_class
), class_set
));
3014 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
3015 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
3017 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
3019 return isl_stat_error
;
3021 isl_set_free(domain
);
3025 return compute_partial_domains(domains
, domain
);
3028 /* Extract the domains at the current depth that should be atomic,
3029 * separated or unrolled and store them in option.
3031 * The domains specified by the user might overlap, so we make
3032 * them disjoint by subtracting earlier domains from later domains.
3034 static void compute_domains_init_options(isl_set
*option
[4],
3035 __isl_keep isl_ast_build
*build
)
3037 enum isl_ast_loop_type type
, type2
;
3040 for (type
= isl_ast_loop_atomic
;
3041 type
<= isl_ast_loop_separate
; ++type
) {
3042 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
3043 for (type2
= isl_ast_loop_atomic
; type2
< type
; ++type2
)
3044 option
[type
] = isl_set_subtract(option
[type
],
3045 isl_set_copy(option
[type2
]));
3048 unroll
= option
[isl_ast_loop_unroll
];
3049 unroll
= isl_set_coalesce(unroll
);
3050 unroll
= isl_set_make_disjoint(unroll
);
3051 option
[isl_ast_loop_unroll
] = unroll
;
3054 /* Split up the domain at the current depth into disjoint
3055 * basic sets for which code should be generated separately,
3056 * based on the user-specified options.
3057 * Return the list of disjoint basic sets.
3059 * There are three kinds of domains that we need to keep track of.
3060 * - the "schedule domain" is the domain of "executed"
3061 * - the "class domain" is the domain corresponding to the currrent
3063 * - the "option domain" is the domain corresponding to one of the options
3064 * atomic, unroll or separate
3066 * We first consider the individial values of the separation classes
3067 * and split up the domain for each of them separately.
3068 * Finally, we consider the remainder. If no separation classes were
3069 * specified, then we call compute_partial_domains with the universe
3070 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3071 * with inner dimensions removed. We do this because we want to
3072 * avoid computing the complement of the class domains (i.e., the difference
3073 * between the universe and domains->done).
3075 static __isl_give isl_basic_set_list
*compute_domains(
3076 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
3078 struct isl_codegen_domains domains
;
3081 isl_union_set
*schedule_domain
;
3085 enum isl_ast_loop_type type
;
3091 ctx
= isl_union_map_get_ctx(executed
);
3092 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
3094 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3095 domain
= isl_set_from_union_set(schedule_domain
);
3097 compute_domains_init_options(domains
.option
, build
);
3099 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
3100 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
3101 n_param
= isl_set_dim(classes
, isl_dim_param
);
3102 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
3104 space
= isl_set_get_space(domain
);
3105 domains
.build
= build
;
3106 domains
.schedule_domain
= isl_set_copy(domain
);
3107 domains
.executed
= executed
;
3108 domains
.done
= isl_set_empty(space
);
3110 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
3111 domains
.list
= isl_basic_set_list_free(domains
.list
);
3112 isl_set_free(classes
);
3114 empty
= isl_set_is_empty(domains
.done
);
3116 domains
.list
= isl_basic_set_list_free(domains
.list
);
3117 domain
= isl_set_free(domain
);
3119 isl_set_free(domain
);
3120 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
3122 domain
= isl_ast_build_eliminate(build
, domain
);
3124 if (compute_partial_domains(&domains
, domain
) < 0)
3125 domains
.list
= isl_basic_set_list_free(domains
.list
);
3127 isl_set_free(domains
.schedule_domain
);
3128 isl_set_free(domains
.done
);
3129 isl_map_free(domains
.sep_class
);
3130 for (type
= isl_ast_loop_atomic
; type
<= isl_ast_loop_separate
; ++type
)
3131 isl_set_free(domains
.option
[type
]);
3133 return domains
.list
;
3136 /* Generate code for a single component, after shifting (if any)
3137 * has been applied, in case the schedule was specified as a union map.
3139 * We first split up the domain at the current depth into disjoint
3140 * basic sets based on the user-specified options.
3141 * Then we generated code for each of them and concatenate the results.
3143 static __isl_give isl_ast_graft_list
*generate_shifted_component_flat(
3144 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3146 isl_basic_set_list
*domain_list
;
3147 isl_ast_graft_list
*list
= NULL
;
3149 domain_list
= compute_domains(executed
, build
);
3150 list
= generate_parallel_domains(domain_list
, executed
, build
);
3152 isl_basic_set_list_free(domain_list
);
3153 isl_union_map_free(executed
);
3154 isl_ast_build_free(build
);
3159 /* Generate code for a single component, after shifting (if any)
3160 * has been applied, in case the schedule was specified as a schedule tree
3161 * and the separate option was specified.
3163 * We perform separation on the domain of "executed" and then generate
3164 * an AST for each of the resulting disjoint basic sets.
3166 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_separate(
3167 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3171 isl_basic_set_list
*domain_list
;
3172 isl_ast_graft_list
*list
;
3174 space
= isl_ast_build_get_space(build
, 1);
3175 domain
= separate_schedule_domains(space
,
3176 isl_union_map_copy(executed
), build
);
3177 domain_list
= isl_basic_set_list_from_set(domain
);
3179 list
= generate_parallel_domains(domain_list
, executed
, build
);
3181 isl_basic_set_list_free(domain_list
);
3182 isl_union_map_free(executed
);
3183 isl_ast_build_free(build
);
3188 /* Internal data structure for generate_shifted_component_tree_unroll.
3190 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3191 * "list" collects the constructs grafts.
3193 struct isl_ast_unroll_tree_data
{
3194 isl_union_map
*executed
;
3195 isl_ast_build
*build
;
3196 isl_ast_graft_list
*list
;
3199 /* Initialize data->list to a list of "n" elements.
3201 static int init_unroll_tree(int n
, void *user
)
3203 struct isl_ast_unroll_tree_data
*data
= user
;
3206 ctx
= isl_ast_build_get_ctx(data
->build
);
3207 data
->list
= isl_ast_graft_list_alloc(ctx
, n
);
3212 /* Given an iteration of an unrolled domain represented by "bset",
3213 * generate the corresponding AST and add the result to data->list.
3215 static int do_unroll_tree_iteration(__isl_take isl_basic_set
*bset
, void *user
)
3217 struct isl_ast_unroll_tree_data
*data
= user
;
3219 data
->list
= add_node(data
->list
, isl_union_map_copy(data
->executed
),
3220 bset
, isl_ast_build_copy(data
->build
));
3225 /* Generate code for a single component, after shifting (if any)
3226 * has been applied, in case the schedule was specified as a schedule tree
3227 * and the unroll option was specified.
3229 * We call foreach_iteration to iterate over the individual values and
3230 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3232 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_unroll(
3233 __isl_take isl_union_map
*executed
, __isl_take isl_set
*domain
,
3234 __isl_take isl_ast_build
*build
)
3236 struct isl_ast_unroll_tree_data data
= { executed
, build
, NULL
};
3238 if (foreach_iteration(domain
, build
, &init_unroll_tree
,
3239 &do_unroll_tree_iteration
, &data
) < 0)
3240 data
.list
= isl_ast_graft_list_free(data
.list
);
3242 isl_union_map_free(executed
);
3243 isl_ast_build_free(build
);
3248 /* Does "domain" involve a disjunction that is purely based on
3249 * constraints involving only outer dimension?
3251 * In particular, is there a disjunction such that the constraints
3252 * involving the current and later dimensions are the same over
3253 * all the disjuncts?
3255 static isl_bool
has_pure_outer_disjunction(__isl_keep isl_set
*domain
,
3256 __isl_keep isl_ast_build
*build
)
3258 isl_basic_set
*hull
;
3259 isl_set
*shared
, *inner
;
3263 if (isl_set_n_basic_set(domain
) <= 1)
3264 return isl_bool_false
;
3266 inner
= isl_set_copy(domain
);
3267 depth
= isl_ast_build_get_depth(build
);
3268 dim
= isl_set_dim(inner
, isl_dim_set
);
3269 inner
= isl_set_drop_constraints_not_involving_dims(inner
,
3270 isl_dim_set
, depth
, dim
- depth
);
3271 hull
= isl_set_plain_unshifted_simple_hull(isl_set_copy(inner
));
3272 shared
= isl_set_from_basic_set(hull
);
3273 equal
= isl_set_plain_is_equal(inner
, shared
);
3274 isl_set_free(inner
);
3275 isl_set_free(shared
);
3280 /* Generate code for a single component, after shifting (if any)
3281 * has been applied, in case the schedule was specified as a schedule tree.
3282 * In particular, handle the base case where there is either no isolated
3283 * set or we are within the isolated set (in which case "isolated" is set)
3284 * or the iterations that precede or follow the isolated set.
3286 * The schedule domain is broken up or combined into basic sets
3287 * according to the AST generation option specified in the current
3288 * schedule node, which may be either atomic, separate, unroll or
3289 * unspecified. If the option is unspecified, then we currently simply
3290 * split the schedule domain into disjoint basic sets.
3292 * In case the separate option is specified, the AST generation is
3293 * handled by generate_shifted_component_tree_separate.
3294 * In the other cases, we need the global schedule domain.
3295 * In the unroll case, the AST generation is then handled by
3296 * generate_shifted_component_tree_unroll which needs the actual
3297 * schedule domain (with divs that may refer to the current dimension)
3298 * so that stride detection can be performed.
3299 * In the atomic or unspecified case, inner dimensions and divs involving
3300 * the current dimensions should be eliminated.
3301 * The result is then either combined into a single basic set or
3302 * split up into disjoint basic sets.
3303 * Finally an AST is generated for each basic set and the results are
3306 * If the schedule domain involves a disjunction that is purely based on
3307 * constraints involving only outer dimension, then it is treated as
3308 * if atomic was specified. This ensures that only a single loop
3309 * is generated instead of a sequence of identical loops with
3312 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_base(
3313 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3316 isl_bool outer_disjunction
;
3317 isl_union_set
*schedule_domain
;
3319 isl_basic_set_list
*domain_list
;
3320 isl_ast_graft_list
*list
;
3321 enum isl_ast_loop_type type
;
3323 type
= isl_ast_build_get_loop_type(build
, isolated
);
3327 if (type
== isl_ast_loop_separate
)
3328 return generate_shifted_component_tree_separate(executed
,
3331 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3332 domain
= isl_set_from_union_set(schedule_domain
);
3334 if (type
== isl_ast_loop_unroll
)
3335 return generate_shifted_component_tree_unroll(executed
, domain
,
3338 domain
= isl_ast_build_eliminate(build
, domain
);
3339 domain
= isl_set_coalesce_preserve(domain
);
3341 outer_disjunction
= has_pure_outer_disjunction(domain
, build
);
3342 if (outer_disjunction
< 0)
3343 domain
= isl_set_free(domain
);
3345 if (outer_disjunction
|| type
== isl_ast_loop_atomic
) {
3346 isl_basic_set
*hull
;
3347 hull
= isl_set_unshifted_simple_hull(domain
);
3348 domain_list
= isl_basic_set_list_from_basic_set(hull
);
3350 domain
= isl_set_make_disjoint(domain
);
3351 domain_list
= isl_basic_set_list_from_set(domain
);
3354 list
= generate_parallel_domains(domain_list
, executed
, build
);
3356 isl_basic_set_list_free(domain_list
);
3357 isl_union_map_free(executed
);
3358 isl_ast_build_free(build
);
3362 isl_union_map_free(executed
);
3363 isl_ast_build_free(build
);
3367 /* Extract out the disjunction imposed by "domain" on the outer
3368 * schedule dimensions.
3370 * In particular, remove all inner dimensions from "domain" (including
3371 * the current dimension) and then remove the constraints that are shared
3372 * by all disjuncts in the result.
3374 static __isl_give isl_set
*extract_disjunction(__isl_take isl_set
*domain
,
3375 __isl_keep isl_ast_build
*build
)
3380 domain
= isl_ast_build_specialize(build
, domain
);
3381 depth
= isl_ast_build_get_depth(build
);
3382 dim
= isl_set_dim(domain
, isl_dim_set
);
3383 domain
= isl_set_eliminate(domain
, isl_dim_set
, depth
, dim
- depth
);
3384 domain
= isl_set_remove_unknown_divs(domain
);
3385 hull
= isl_set_copy(domain
);
3386 hull
= isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull
));
3387 domain
= isl_set_gist(domain
, hull
);
3392 /* Add "guard" to the grafts in "list".
3393 * "build" is the outer AST build, while "sub_build" includes "guard"
3394 * in its generated domain.
3396 * First combine the grafts into a single graft and then add the guard.
3397 * If the list is empty, or if some error occurred, then simply return
3400 static __isl_give isl_ast_graft_list
*list_add_guard(
3401 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*guard
,
3402 __isl_keep isl_ast_build
*build
, __isl_keep isl_ast_build
*sub_build
)
3404 isl_ast_graft
*graft
;
3406 list
= isl_ast_graft_list_fuse(list
, sub_build
);
3408 if (isl_ast_graft_list_n_ast_graft(list
) != 1)
3411 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
3412 graft
= isl_ast_graft_add_guard(graft
, isl_set_copy(guard
), build
);
3413 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
3418 /* Generate code for a single component, after shifting (if any)
3419 * has been applied, in case the schedule was specified as a schedule tree.
3420 * In particular, do so for the specified subset of the schedule domain.
3422 * If we are outside of the isolated part, then "domain" may include
3423 * a disjunction. Explicitly generate this disjunction at this point
3424 * instead of relying on the disjunction getting hoisted back up
3427 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_part(
3428 __isl_keep isl_union_map
*executed
, __isl_take isl_set
*domain
,
3429 __isl_keep isl_ast_build
*build
, int isolated
)
3431 isl_union_set
*uset
;
3432 isl_ast_graft_list
*list
;
3433 isl_ast_build
*sub_build
;
3436 uset
= isl_union_set_from_set(isl_set_copy(domain
));
3437 executed
= isl_union_map_copy(executed
);
3438 executed
= isl_union_map_intersect_domain(executed
, uset
);
3439 empty
= isl_union_map_is_empty(executed
);
3444 isl_union_map_free(executed
);
3445 isl_set_free(domain
);
3446 ctx
= isl_ast_build_get_ctx(build
);
3447 return isl_ast_graft_list_alloc(ctx
, 0);
3450 sub_build
= isl_ast_build_copy(build
);
3452 domain
= extract_disjunction(domain
, build
);
3453 sub_build
= isl_ast_build_restrict_generated(sub_build
,
3454 isl_set_copy(domain
));
3456 list
= generate_shifted_component_tree_base(executed
,
3457 isl_ast_build_copy(sub_build
), isolated
);
3459 list
= list_add_guard(list
, domain
, build
, sub_build
);
3460 isl_ast_build_free(sub_build
);
3461 isl_set_free(domain
);
3464 isl_union_map_free(executed
);
3465 isl_set_free(domain
);
3469 /* Generate code for a single component, after shifting (if any)
3470 * has been applied, in case the schedule was specified as a schedule tree.
3471 * In particular, do so for the specified sequence of subsets
3472 * of the schedule domain, "before", "isolated", "after" and "other",
3473 * where only the "isolated" part is considered to be isolated.
3475 static __isl_give isl_ast_graft_list
*generate_shifted_component_parts(
3476 __isl_take isl_union_map
*executed
, __isl_take isl_set
*before
,
3477 __isl_take isl_set
*isolated
, __isl_take isl_set
*after
,
3478 __isl_take isl_set
*other
, __isl_take isl_ast_build
*build
)
3480 isl_ast_graft_list
*list
, *res
;
3482 res
= generate_shifted_component_tree_part(executed
, before
, build
, 0);
3483 list
= generate_shifted_component_tree_part(executed
, isolated
,
3485 res
= isl_ast_graft_list_concat(res
, list
);
3486 list
= generate_shifted_component_tree_part(executed
, after
, build
, 0);
3487 res
= isl_ast_graft_list_concat(res
, list
);
3488 list
= generate_shifted_component_tree_part(executed
, other
, build
, 0);
3489 res
= isl_ast_graft_list_concat(res
, list
);
3491 isl_union_map_free(executed
);
3492 isl_ast_build_free(build
);
3497 /* Does "set" intersect "first", but not "second"?
3499 static isl_bool
only_intersects_first(__isl_keep isl_set
*set
,
3500 __isl_keep isl_set
*first
, __isl_keep isl_set
*second
)
3504 disjoint
= isl_set_is_disjoint(set
, first
);
3506 return isl_bool_error
;
3508 return isl_bool_false
;
3510 return isl_set_is_disjoint(set
, second
);
3513 /* Generate code for a single component, after shifting (if any)
3514 * has been applied, in case the schedule was specified as a schedule tree.
3515 * In particular, do so in case of isolation where there is
3516 * only an "isolated" part and an "after" part.
3517 * "dead1" and "dead2" are freed by this function in order to simplify
3520 * The "before" and "other" parts are set to empty sets.
3522 static __isl_give isl_ast_graft_list
*generate_shifted_component_only_after(
3523 __isl_take isl_union_map
*executed
, __isl_take isl_set
*isolated
,
3524 __isl_take isl_set
*after
, __isl_take isl_ast_build
*build
,
3525 __isl_take isl_set
*dead1
, __isl_take isl_set
*dead2
)
3529 empty
= isl_set_empty(isl_set_get_space(after
));
3530 isl_set_free(dead1
);
3531 isl_set_free(dead2
);
3532 return generate_shifted_component_parts(executed
, isl_set_copy(empty
),
3533 isolated
, after
, empty
, build
);
3536 /* Generate code for a single component, after shifting (if any)
3537 * has been applied, in case the schedule was specified as a schedule tree.
3539 * We first check if the user has specified an isolated schedule domain
3540 * and that we are not already outside of this isolated schedule domain.
3541 * If so, we break up the schedule domain into iterations that
3542 * precede the isolated domain, the isolated domain itself,
3543 * the iterations that follow the isolated domain and
3544 * the remaining iterations (those that are incomparable
3545 * to the isolated domain).
3546 * We generate an AST for each piece and concatenate the results.
3548 * If the isolated domain is not convex, then it is replaced
3549 * by a convex superset to ensure that the sets of preceding and
3550 * following iterations are properly defined and, in particular,
3551 * that there are no intermediate iterations that do not belong
3552 * to the isolated domain.
3554 * In the special case where at least one element of the schedule
3555 * domain that does not belong to the isolated domain needs
3556 * to be scheduled after this isolated domain, but none of those
3557 * elements need to be scheduled before, break up the schedule domain
3558 * in only two parts, the isolated domain, and a part that will be
3559 * scheduled after the isolated domain.
3561 * If no isolated set has been specified, then we generate an
3562 * AST for the entire inverse schedule.
3564 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree(
3565 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3568 int empty
, has_isolate
;
3570 isl_union_set
*schedule_domain
;
3572 isl_basic_set
*hull
;
3573 isl_set
*isolated
, *before
, *after
, *test
;
3577 build
= isl_ast_build_extract_isolated(build
);
3578 has_isolate
= isl_ast_build_has_isolated(build
);
3579 if (has_isolate
< 0)
3580 executed
= isl_union_map_free(executed
);
3581 else if (!has_isolate
)
3582 return generate_shifted_component_tree_base(executed
, build
, 0);
3584 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3585 domain
= isl_set_from_union_set(schedule_domain
);
3587 isolated
= isl_ast_build_get_isolated(build
);
3588 isolated
= isl_set_intersect(isolated
, isl_set_copy(domain
));
3589 test
= isl_ast_build_specialize(build
, isl_set_copy(isolated
));
3590 empty
= isl_set_is_empty(test
);
3595 isl_set_free(isolated
);
3596 isl_set_free(domain
);
3597 return generate_shifted_component_tree_base(executed
, build
, 0);
3599 isolated
= isl_ast_build_eliminate(build
, isolated
);
3600 hull
= isl_set_unshifted_simple_hull(isolated
);
3601 isolated
= isl_set_from_basic_set(hull
);
3603 depth
= isl_ast_build_get_depth(build
);
3604 space
= isl_space_map_from_set(isl_set_get_space(isolated
));
3605 gt
= isl_map_universe(space
);
3606 for (i
= 0; i
< depth
; ++i
)
3607 gt
= isl_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
3608 gt
= isl_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
3609 lt
= isl_map_reverse(isl_map_copy(gt
));
3610 before
= isl_set_apply(isl_set_copy(isolated
), gt
);
3611 after
= isl_set_apply(isl_set_copy(isolated
), lt
);
3613 domain
= isl_set_subtract(domain
, isl_set_copy(isolated
));
3614 pure
= only_intersects_first(domain
, after
, before
);
3616 executed
= isl_union_map_free(executed
);
3618 return generate_shifted_component_only_after(executed
, isolated
,
3619 domain
, build
, before
, after
);
3620 domain
= isl_set_subtract(domain
, isl_set_copy(before
));
3621 domain
= isl_set_subtract(domain
, isl_set_copy(after
));
3622 after
= isl_set_subtract(after
, isl_set_copy(isolated
));
3623 after
= isl_set_subtract(after
, isl_set_copy(before
));
3624 before
= isl_set_subtract(before
, isl_set_copy(isolated
));
3626 return generate_shifted_component_parts(executed
, before
, isolated
,
3627 after
, domain
, build
);
3629 isl_set_free(domain
);
3630 isl_set_free(isolated
);
3631 isl_union_map_free(executed
);
3632 isl_ast_build_free(build
);
3636 /* Generate code for a single component, after shifting (if any)
3639 * Call generate_shifted_component_tree or generate_shifted_component_flat
3640 * depending on whether the schedule was specified as a schedule tree.
3642 static __isl_give isl_ast_graft_list
*generate_shifted_component(
3643 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3645 if (isl_ast_build_has_schedule_node(build
))
3646 return generate_shifted_component_tree(executed
, build
);
3648 return generate_shifted_component_flat(executed
, build
);
3651 struct isl_set_map_pair
{
3656 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3657 * of indices into the "domain" array,
3658 * return the union of the "map" fields of the elements
3659 * indexed by the first "n" elements of "order".
3661 static __isl_give isl_union_map
*construct_component_executed(
3662 struct isl_set_map_pair
*domain
, int *order
, int n
)
3666 isl_union_map
*executed
;
3668 map
= isl_map_copy(domain
[order
[0]].map
);
3669 executed
= isl_union_map_from_map(map
);
3670 for (i
= 1; i
< n
; ++i
) {
3671 map
= isl_map_copy(domain
[order
[i
]].map
);
3672 executed
= isl_union_map_add_map(executed
, map
);
3678 /* Generate code for a single component, after shifting (if any)
3681 * The component inverse schedule is specified as the "map" fields
3682 * of the elements of "domain" indexed by the first "n" elements of "order".
3684 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
3685 struct isl_set_map_pair
*domain
, int *order
, int n
,
3686 __isl_take isl_ast_build
*build
)
3688 isl_union_map
*executed
;
3690 executed
= construct_component_executed(domain
, order
, n
);
3691 return generate_shifted_component(executed
, build
);
3694 /* Does set dimension "pos" of "set" have an obviously fixed value?
3696 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
3701 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
3704 fixed
= !isl_val_is_nan(v
);
3710 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3711 * of indices into the "domain" array,
3712 * do all (except for at most one) of the "set" field of the elements
3713 * indexed by the first "n" elements of "order" have a fixed value
3714 * at position "depth"?
3716 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
3717 int *order
, int n
, int depth
)
3722 for (i
= 0; i
< n
; ++i
) {
3725 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3738 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3739 * of indices into the "domain" array,
3740 * eliminate the inner dimensions from the "set" field of the elements
3741 * indexed by the first "n" elements of "order", provided the current
3742 * dimension does not have a fixed value.
3744 * Return the index of the first element in "order" with a corresponding
3745 * "set" field that does not have an (obviously) fixed value.
3747 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
3748 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
3753 for (i
= n
- 1; i
>= 0; --i
) {
3755 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3760 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
3761 domain
[order
[i
]].set
);
3768 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3769 * of indices into the "domain" array,
3770 * find the element of "domain" (amongst those indexed by the first "n"
3771 * elements of "order") with the "set" field that has the smallest
3772 * value for the current iterator.
3774 * Note that the domain with the smallest value may depend on the parameters
3775 * and/or outer loop dimension. Since the result of this function is only
3776 * used as heuristic, we only make a reasonable attempt at finding the best
3777 * domain, one that should work in case a single domain provides the smallest
3778 * value for the current dimension over all values of the parameters
3779 * and outer dimensions.
3781 * In particular, we compute the smallest value of the first domain
3782 * and replace it by that of any later domain if that later domain
3783 * has a smallest value that is smaller for at least some value
3784 * of the parameters and outer dimensions.
3786 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
3787 __isl_keep isl_ast_build
*build
)
3793 min_first
= isl_ast_build_map_to_iterator(build
,
3794 isl_set_copy(domain
[order
[0]].set
));
3795 min_first
= isl_map_lexmin(min_first
);
3797 for (i
= 1; i
< n
; ++i
) {
3798 isl_map
*min
, *test
;
3801 min
= isl_ast_build_map_to_iterator(build
,
3802 isl_set_copy(domain
[order
[i
]].set
));
3803 min
= isl_map_lexmin(min
);
3804 test
= isl_map_copy(min
);
3805 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
3806 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
3807 empty
= isl_map_is_empty(test
);
3809 if (empty
>= 0 && !empty
) {
3810 isl_map_free(min_first
);
3820 isl_map_free(min_first
);
3822 return i
< n
? -1 : first
;
3825 /* Construct a shifted inverse schedule based on the original inverse schedule,
3826 * the stride and the offset.
3828 * The original inverse schedule is specified as the "map" fields
3829 * of the elements of "domain" indexed by the first "n" elements of "order".
3831 * "stride" and "offset" are such that the difference
3832 * between the values of the current dimension of domain "i"
3833 * and the values of the current dimension for some reference domain are
3836 * stride * integer + offset[i]
3838 * Moreover, 0 <= offset[i] < stride.
3840 * For each domain, we create a map
3842 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3844 * where j refers to the current dimension and the other dimensions are
3845 * unchanged, and apply this map to the original schedule domain.
3847 * For example, for the original schedule
3849 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3851 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3852 * we apply the mapping
3856 * to the schedule of the "A" domain and the mapping
3858 * { [j - 1] -> [j, 1] }
3860 * to the schedule of the "B" domain.
3863 * Note that after the transformation, the differences between pairs
3864 * of values of the current dimension over all domains are multiples
3865 * of stride and that we have therefore exposed the stride.
3868 * To see that the mapping preserves the lexicographic order,
3869 * first note that each of the individual maps above preserves the order.
3870 * If the value of the current iterator is j1 in one domain and j2 in another,
3871 * then if j1 = j2, we know that the same map is applied to both domains
3872 * and the order is preserved.
3873 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3874 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3878 * and the order is preserved.
3879 * If c1 < c2, then we know
3885 * j2 - j1 = n * s + r
3887 * with n >= 0 and 0 <= r < s.
3888 * In other words, r = c2 - c1.
3899 * (j1 - c1, c1) << (j2 - c2, c2)
3901 * with "<<" the lexicographic order, proving that the order is preserved
3904 static __isl_give isl_union_map
*construct_shifted_executed(
3905 struct isl_set_map_pair
*domain
, int *order
, int n
,
3906 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3907 __isl_take isl_ast_build
*build
)
3910 isl_union_map
*executed
;
3916 depth
= isl_ast_build_get_depth(build
);
3917 space
= isl_ast_build_get_space(build
, 1);
3918 executed
= isl_union_map_empty(isl_space_copy(space
));
3919 space
= isl_space_map_from_set(space
);
3920 map
= isl_map_identity(isl_space_copy(space
));
3921 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3922 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3923 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3925 c
= isl_constraint_alloc_equality(isl_local_space_from_space(space
));
3926 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3927 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3929 for (i
= 0; i
< n
; ++i
) {
3933 v
= isl_multi_val_get_val(offset
, i
);
3936 map_i
= isl_map_copy(map
);
3937 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
3940 c
= isl_constraint_set_constant_val(c
, v
);
3941 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
3943 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
3945 executed
= isl_union_map_add_map(executed
, map_i
);
3948 isl_constraint_free(c
);
3952 executed
= isl_union_map_free(executed
);
3957 /* Generate code for a single component, after exposing the stride,
3958 * given that the schedule domain is "shifted strided".
3960 * The component inverse schedule is specified as the "map" fields
3961 * of the elements of "domain" indexed by the first "n" elements of "order".
3963 * The schedule domain being "shifted strided" means that the differences
3964 * between the values of the current dimension of domain "i"
3965 * and the values of the current dimension for some reference domain are
3968 * stride * integer + offset[i]
3970 * We first look for the domain with the "smallest" value for the current
3971 * dimension and adjust the offsets such that the offset of the "smallest"
3972 * domain is equal to zero. The other offsets are reduced modulo stride.
3974 * Based on this information, we construct a new inverse schedule in
3975 * construct_shifted_executed that exposes the stride.
3976 * Since this involves the introduction of a new schedule dimension,
3977 * the build needs to be changed accordingly.
3978 * After computing the AST, the newly introduced dimension needs
3979 * to be removed again from the list of grafts. We do this by plugging
3980 * in a mapping that represents the new schedule domain in terms of the
3981 * old schedule domain.
3983 static __isl_give isl_ast_graft_list
*generate_shift_component(
3984 struct isl_set_map_pair
*domain
, int *order
, int n
,
3985 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3986 __isl_take isl_ast_build
*build
)
3988 isl_ast_graft_list
*list
;
3994 isl_multi_aff
*ma
, *zero
;
3995 isl_union_map
*executed
;
3997 depth
= isl_ast_build_get_depth(build
);
3999 first
= first_offset(domain
, order
, n
, build
);
4003 mv
= isl_multi_val_copy(offset
);
4004 val
= isl_multi_val_get_val(offset
, first
);
4005 val
= isl_val_neg(val
);
4006 mv
= isl_multi_val_add_val(mv
, val
);
4007 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
4009 executed
= construct_shifted_executed(domain
, order
, n
, stride
, mv
,
4011 space
= isl_ast_build_get_space(build
, 1);
4012 space
= isl_space_map_from_set(space
);
4013 ma
= isl_multi_aff_identity(isl_space_copy(space
));
4014 space
= isl_space_from_domain(isl_space_domain(space
));
4015 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
4016 zero
= isl_multi_aff_zero(space
);
4017 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
4018 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
4019 list
= generate_shifted_component(executed
, build
);
4021 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
4023 isl_multi_val_free(mv
);
4027 isl_ast_build_free(build
);
4031 /* Does any node in the schedule tree rooted at the current schedule node
4032 * of "build" depend on outer schedule nodes?
4034 static int has_anchored_subtree(__isl_keep isl_ast_build
*build
)
4036 isl_schedule_node
*node
;
4039 node
= isl_ast_build_get_schedule_node(build
);
4040 dependent
= isl_schedule_node_is_subtree_anchored(node
);
4041 isl_schedule_node_free(node
);
4046 /* Generate code for a single component.
4048 * The component inverse schedule is specified as the "map" fields
4049 * of the elements of "domain" indexed by the first "n" elements of "order".
4051 * This function may modify the "set" fields of "domain".
4053 * Before proceeding with the actual code generation for the component,
4054 * we first check if there are any "shifted" strides, meaning that
4055 * the schedule domains of the individual domains are all strided,
4056 * but that they have different offsets, resulting in the union
4057 * of schedule domains not being strided anymore.
4059 * The simplest example is the schedule
4061 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4063 * Both schedule domains are strided, but their union is not.
4064 * This function detects such cases and then rewrites the schedule to
4066 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4068 * In the new schedule, the schedule domains have the same offset (modulo
4069 * the stride), ensuring that the union of schedule domains is also strided.
4072 * If there is only a single domain in the component, then there is
4073 * nothing to do. Similarly, if the current schedule dimension has
4074 * a fixed value for almost all domains then there is nothing to be done.
4075 * In particular, we need at least two domains where the current schedule
4076 * dimension does not have a fixed value.
4077 * Finally, in case of a schedule map input,
4078 * if any of the options refer to the current schedule dimension,
4079 * then we bail out as well. It would be possible to reformulate the options
4080 * in terms of the new schedule domain, but that would introduce constraints
4081 * that separate the domains in the options and that is something we would
4083 * In the case of a schedule tree input, we bail out if any of
4084 * the descendants of the current schedule node refer to outer
4085 * schedule nodes in any way.
4088 * To see if there is any shifted stride, we look at the differences
4089 * between the values of the current dimension in pairs of domains
4090 * for equal values of outer dimensions. These differences should be
4095 * with "m" the stride and "r" a constant. Note that we cannot perform
4096 * this analysis on individual domains as the lower bound in each domain
4097 * may depend on parameters or outer dimensions and so the current dimension
4098 * itself may not have a fixed remainder on division by the stride.
4100 * In particular, we compare the first domain that does not have an
4101 * obviously fixed value for the current dimension to itself and all
4102 * other domains and collect the offsets and the gcd of the strides.
4103 * If the gcd becomes one, then we failed to find shifted strides.
4104 * If the gcd is zero, then the differences were all fixed, meaning
4105 * that some domains had non-obviously fixed values for the current dimension.
4106 * If all the offsets are the same (for those domains that do not have
4107 * an obviously fixed value for the current dimension), then we do not
4108 * apply the transformation.
4109 * If none of the domains were skipped, then there is nothing to do.
4110 * If some of them were skipped, then if we apply separation, the schedule
4111 * domain should get split in pieces with a (non-shifted) stride.
4113 * Otherwise, we apply a shift to expose the stride in
4114 * generate_shift_component.
4116 static __isl_give isl_ast_graft_list
*generate_component(
4117 struct isl_set_map_pair
*domain
, int *order
, int n
,
4118 __isl_take isl_ast_build
*build
)
4125 isl_val
*gcd
= NULL
;
4129 isl_ast_graft_list
*list
;
4132 depth
= isl_ast_build_get_depth(build
);
4135 if (skip
>= 0 && !skip
)
4136 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
4137 if (skip
>= 0 && !skip
) {
4138 if (isl_ast_build_has_schedule_node(build
))
4139 skip
= has_anchored_subtree(build
);
4141 skip
= isl_ast_build_options_involve_depth(build
);
4146 return generate_shifted_component_from_list(domain
,
4149 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
4153 ctx
= isl_ast_build_get_ctx(build
);
4155 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
4158 for (i
= 0; i
< n
; ++i
) {
4161 map
= isl_map_from_domain_and_range(
4162 isl_set_copy(domain
[order
[base
]].set
),
4163 isl_set_copy(domain
[order
[i
]].set
));
4164 for (d
= 0; d
< depth
; ++d
)
4165 map
= isl_map_equate(map
, isl_dim_in
, d
,
4167 deltas
= isl_map_deltas(map
);
4168 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
4169 isl_set_free(deltas
);
4176 gcd
= isl_val_gcd(gcd
, m
);
4177 if (isl_val_is_one(gcd
)) {
4181 mv
= isl_multi_val_set_val(mv
, i
, r
);
4183 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
4189 if (fixed
&& i
> base
) {
4191 a
= isl_multi_val_get_val(mv
, i
);
4192 b
= isl_multi_val_get_val(mv
, base
);
4193 if (isl_val_ne(a
, b
))
4200 if (res
< 0 || !gcd
) {
4201 isl_ast_build_free(build
);
4203 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
4204 list
= generate_shifted_component_from_list(domain
,
4207 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
4212 isl_multi_val_free(mv
);
4216 isl_ast_build_free(build
);
4220 /* Store both "map" itself and its domain in the
4221 * structure pointed to by *next and advance to the next array element.
4223 static isl_stat
extract_domain(__isl_take isl_map
*map
, void *user
)
4225 struct isl_set_map_pair
**next
= user
;
4227 (*next
)->map
= isl_map_copy(map
);
4228 (*next
)->set
= isl_map_domain(map
);
4234 static int after_in_tree(__isl_keep isl_union_map
*umap
,
4235 __isl_keep isl_schedule_node
*node
);
4237 /* Is any domain element of "umap" scheduled after any of
4238 * the corresponding image elements by the tree rooted at
4239 * the child of "node"?
4241 static int after_in_child(__isl_keep isl_union_map
*umap
,
4242 __isl_keep isl_schedule_node
*node
)
4244 isl_schedule_node
*child
;
4247 child
= isl_schedule_node_get_child(node
, 0);
4248 after
= after_in_tree(umap
, child
);
4249 isl_schedule_node_free(child
);
4254 /* Is any domain element of "umap" scheduled after any of
4255 * the corresponding image elements by the tree rooted at
4256 * the band node "node"?
4258 * We first check if any domain element is scheduled after any
4259 * of the corresponding image elements by the band node itself.
4260 * If not, we restrict "map" to those pairs of element that
4261 * are scheduled together by the band node and continue with
4262 * the child of the band node.
4263 * If there are no such pairs then the map passed to after_in_child
4264 * will be empty causing it to return 0.
4266 static int after_in_band(__isl_keep isl_union_map
*umap
,
4267 __isl_keep isl_schedule_node
*node
)
4269 isl_multi_union_pw_aff
*mupa
;
4270 isl_union_map
*partial
, *test
, *gt
, *universe
, *umap1
, *umap2
;
4271 isl_union_set
*domain
, *range
;
4276 if (isl_schedule_node_band_n_member(node
) == 0)
4277 return after_in_child(umap
, node
);
4279 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
4280 space
= isl_multi_union_pw_aff_get_space(mupa
);
4281 partial
= isl_union_map_from_multi_union_pw_aff(mupa
);
4282 test
= isl_union_map_copy(umap
);
4283 test
= isl_union_map_apply_domain(test
, isl_union_map_copy(partial
));
4284 test
= isl_union_map_apply_range(test
, isl_union_map_copy(partial
));
4285 gt
= isl_union_map_from_map(isl_map_lex_gt(space
));
4286 test
= isl_union_map_intersect(test
, gt
);
4287 empty
= isl_union_map_is_empty(test
);
4288 isl_union_map_free(test
);
4290 if (empty
< 0 || !empty
) {
4291 isl_union_map_free(partial
);
4292 return empty
< 0 ? -1 : 1;
4295 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4296 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4297 range
= isl_union_map_range(universe
);
4298 umap1
= isl_union_map_copy(partial
);
4299 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4300 umap2
= isl_union_map_intersect_domain(partial
, range
);
4301 test
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4302 test
= isl_union_map_intersect(test
, isl_union_map_copy(umap
));
4303 after
= after_in_child(test
, node
);
4304 isl_union_map_free(test
);
4308 /* Is any domain element of "umap" scheduled after any of
4309 * the corresponding image elements by the tree rooted at
4310 * the context node "node"?
4312 * The context constraints apply to the schedule domain,
4313 * so we cannot apply them directly to "umap", which contains
4314 * pairs of statement instances. Instead, we add them
4315 * to the range of the prefix schedule for both domain and
4318 static int after_in_context(__isl_keep isl_union_map
*umap
,
4319 __isl_keep isl_schedule_node
*node
)
4321 isl_union_map
*prefix
, *universe
, *umap1
, *umap2
;
4322 isl_union_set
*domain
, *range
;
4326 umap
= isl_union_map_copy(umap
);
4327 context
= isl_schedule_node_context_get_context(node
);
4328 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4329 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4330 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4331 range
= isl_union_map_range(universe
);
4332 umap1
= isl_union_map_copy(prefix
);
4333 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4334 umap2
= isl_union_map_intersect_domain(prefix
, range
);
4335 umap1
= isl_union_map_intersect_range(umap1
,
4336 isl_union_set_from_set(context
));
4337 umap1
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4338 umap
= isl_union_map_intersect(umap
, umap1
);
4340 after
= after_in_child(umap
, node
);
4342 isl_union_map_free(umap
);
4347 /* Is any domain element of "umap" scheduled after any of
4348 * the corresponding image elements by the tree rooted at
4349 * the expansion node "node"?
4351 * We apply the expansion to domain and range of "umap" and
4352 * continue with its child.
4354 static int after_in_expansion(__isl_keep isl_union_map
*umap
,
4355 __isl_keep isl_schedule_node
*node
)
4357 isl_union_map
*expansion
;
4360 expansion
= isl_schedule_node_expansion_get_expansion(node
);
4361 umap
= isl_union_map_copy(umap
);
4362 umap
= isl_union_map_apply_domain(umap
, isl_union_map_copy(expansion
));
4363 umap
= isl_union_map_apply_range(umap
, expansion
);
4365 after
= after_in_child(umap
, node
);
4367 isl_union_map_free(umap
);
4372 /* Is any domain element of "umap" scheduled after any of
4373 * the corresponding image elements by the tree rooted at
4374 * the extension node "node"?
4376 * Since the extension node may add statement instances before or
4377 * after the pairs of statement instances in "umap", we return 1
4378 * to ensure that these pairs are not broken up.
4380 static int after_in_extension(__isl_keep isl_union_map
*umap
,
4381 __isl_keep isl_schedule_node
*node
)
4386 /* Is any domain element of "umap" scheduled after any of
4387 * the corresponding image elements by the tree rooted at
4388 * the filter node "node"?
4390 * We intersect domain and range of "umap" with the filter and
4391 * continue with its child.
4393 static int after_in_filter(__isl_keep isl_union_map
*umap
,
4394 __isl_keep isl_schedule_node
*node
)
4396 isl_union_set
*filter
;
4399 umap
= isl_union_map_copy(umap
);
4400 filter
= isl_schedule_node_filter_get_filter(node
);
4401 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(filter
));
4402 umap
= isl_union_map_intersect_range(umap
, filter
);
4404 after
= after_in_child(umap
, node
);
4406 isl_union_map_free(umap
);
4411 /* Is any domain element of "umap" scheduled after any of
4412 * the corresponding image elements by the tree rooted at
4413 * the set node "node"?
4415 * This is only the case if this condition holds in any
4416 * of the (filter) children of the set node.
4417 * In particular, if the domain and the range of "umap"
4418 * are contained in different children, then the condition
4421 static int after_in_set(__isl_keep isl_union_map
*umap
,
4422 __isl_keep isl_schedule_node
*node
)
4426 n
= isl_schedule_node_n_children(node
);
4427 for (i
= 0; i
< n
; ++i
) {
4428 isl_schedule_node
*child
;
4431 child
= isl_schedule_node_get_child(node
, i
);
4432 after
= after_in_tree(umap
, child
);
4433 isl_schedule_node_free(child
);
4435 if (after
< 0 || after
)
4442 /* Return the filter of child "i" of "node".
4444 static __isl_give isl_union_set
*child_filter(
4445 __isl_keep isl_schedule_node
*node
, int i
)
4447 isl_schedule_node
*child
;
4448 isl_union_set
*filter
;
4450 child
= isl_schedule_node_get_child(node
, i
);
4451 filter
= isl_schedule_node_filter_get_filter(child
);
4452 isl_schedule_node_free(child
);
4457 /* Is any domain element of "umap" scheduled after any of
4458 * the corresponding image elements by the tree rooted at
4459 * the sequence node "node"?
4461 * This happens in particular if any domain element is
4462 * contained in a later child than one containing a range element or
4463 * if the condition holds within a given child in the sequence.
4464 * The later part of the condition is checked by after_in_set.
4466 static int after_in_sequence(__isl_keep isl_union_map
*umap
,
4467 __isl_keep isl_schedule_node
*node
)
4470 isl_union_map
*umap_i
;
4474 n
= isl_schedule_node_n_children(node
);
4475 for (i
= 1; i
< n
; ++i
) {
4476 isl_union_set
*filter_i
;
4478 umap_i
= isl_union_map_copy(umap
);
4479 filter_i
= child_filter(node
, i
);
4480 umap_i
= isl_union_map_intersect_domain(umap_i
, filter_i
);
4481 empty
= isl_union_map_is_empty(umap_i
);
4485 isl_union_map_free(umap_i
);
4489 for (j
= 0; j
< i
; ++j
) {
4490 isl_union_set
*filter_j
;
4491 isl_union_map
*umap_ij
;
4493 umap_ij
= isl_union_map_copy(umap_i
);
4494 filter_j
= child_filter(node
, j
);
4495 umap_ij
= isl_union_map_intersect_range(umap_ij
,
4497 empty
= isl_union_map_is_empty(umap_ij
);
4498 isl_union_map_free(umap_ij
);
4508 isl_union_map_free(umap_i
);
4513 if (after
< 0 || after
)
4516 return after_in_set(umap
, node
);
4518 isl_union_map_free(umap_i
);
4522 /* Is any domain element of "umap" scheduled after any of
4523 * the corresponding image elements by the tree rooted at "node"?
4525 * If "umap" is empty, then clearly there is no such element.
4526 * Otherwise, consider the different types of nodes separately.
4528 static int after_in_tree(__isl_keep isl_union_map
*umap
,
4529 __isl_keep isl_schedule_node
*node
)
4532 enum isl_schedule_node_type type
;
4534 empty
= isl_union_map_is_empty(umap
);
4542 type
= isl_schedule_node_get_type(node
);
4544 case isl_schedule_node_error
:
4546 case isl_schedule_node_leaf
:
4548 case isl_schedule_node_band
:
4549 return after_in_band(umap
, node
);
4550 case isl_schedule_node_domain
:
4551 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
4552 "unexpected internal domain node", return -1);
4553 case isl_schedule_node_context
:
4554 return after_in_context(umap
, node
);
4555 case isl_schedule_node_expansion
:
4556 return after_in_expansion(umap
, node
);
4557 case isl_schedule_node_extension
:
4558 return after_in_extension(umap
, node
);
4559 case isl_schedule_node_filter
:
4560 return after_in_filter(umap
, node
);
4561 case isl_schedule_node_guard
:
4562 case isl_schedule_node_mark
:
4563 return after_in_child(umap
, node
);
4564 case isl_schedule_node_set
:
4565 return after_in_set(umap
, node
);
4566 case isl_schedule_node_sequence
:
4567 return after_in_sequence(umap
, node
);
4573 /* Is any domain element of "map1" scheduled after any domain
4574 * element of "map2" by the subtree underneath the current band node,
4575 * while at the same time being scheduled together by the current
4576 * band node, i.e., by "map1" and "map2?
4578 * If the child of the current band node is a leaf, then
4579 * no element can be scheduled after any other element.
4581 * Otherwise, we construct a relation between domain elements
4582 * of "map1" and domain elements of "map2" that are scheduled
4583 * together and then check if the subtree underneath the current
4584 * band node determines their relative order.
4586 static int after_in_subtree(__isl_keep isl_ast_build
*build
,
4587 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4589 isl_schedule_node
*node
;
4591 isl_union_map
*umap
;
4594 node
= isl_ast_build_get_schedule_node(build
);
4597 node
= isl_schedule_node_child(node
, 0);
4598 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
4599 isl_schedule_node_free(node
);
4602 map
= isl_map_copy(map2
);
4603 map
= isl_map_apply_domain(map
, isl_map_copy(map1
));
4604 umap
= isl_union_map_from_map(map
);
4605 after
= after_in_tree(umap
, node
);
4606 isl_union_map_free(umap
);
4607 isl_schedule_node_free(node
);
4611 /* Internal data for any_scheduled_after.
4613 * "build" is the build in which the AST is constructed.
4614 * "depth" is the number of loops that have already been generated
4615 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4616 * "domain" is an array of set-map pairs corresponding to the different
4617 * iteration domains. The set is the schedule domain, i.e., the domain
4618 * of the inverse schedule, while the map is the inverse schedule itself.
4620 struct isl_any_scheduled_after_data
{
4621 isl_ast_build
*build
;
4623 int group_coscheduled
;
4624 struct isl_set_map_pair
*domain
;
4627 /* Is any element of domain "i" scheduled after any element of domain "j"
4628 * (for a common iteration of the first data->depth loops)?
4630 * data->domain[i].set contains the domain of the inverse schedule
4631 * for domain "i", i.e., elements in the schedule domain.
4633 * If we are inside a band of a schedule tree and there is a pair
4634 * of elements in the two domains that is schedule together by
4635 * the current band, then we check if any element of "i" may be schedule
4636 * after element of "j" by the descendants of the band node.
4638 * If data->group_coscheduled is set, then we also return 1 if there
4639 * is any pair of elements in the two domains that are scheduled together.
4641 static isl_bool
any_scheduled_after(int i
, int j
, void *user
)
4643 struct isl_any_scheduled_after_data
*data
= user
;
4644 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
4647 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
4650 follows
= isl_set_follows_at(data
->domain
[i
].set
,
4651 data
->domain
[j
].set
, pos
);
4654 return isl_bool_error
;
4656 return isl_bool_true
;
4658 return isl_bool_false
;
4661 if (isl_ast_build_has_schedule_node(data
->build
)) {
4664 after
= after_in_subtree(data
->build
, data
->domain
[i
].map
,
4665 data
->domain
[j
].map
);
4666 if (after
< 0 || after
)
4670 return data
->group_coscheduled
;
4673 /* Look for independent components at the current depth and generate code
4674 * for each component separately. The resulting lists of grafts are
4675 * merged in an attempt to combine grafts with identical guards.
4677 * Code for two domains can be generated separately if all the elements
4678 * of one domain are scheduled before (or together with) all the elements
4679 * of the other domain. We therefore consider the graph with as nodes
4680 * the domains and an edge between two nodes if any element of the first
4681 * node is scheduled after any element of the second node.
4682 * If the ast_build_group_coscheduled is set, then we also add an edge if
4683 * there is any pair of elements in the two domains that are scheduled
4685 * Code is then generated (by generate_component)
4686 * for each of the strongly connected components in this graph
4687 * in their topological order.
4689 * Since the test is performed on the domain of the inverse schedules of
4690 * the different domains, we precompute these domains and store
4691 * them in data.domain.
4693 static __isl_give isl_ast_graft_list
*generate_components(
4694 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4697 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4698 int n
= isl_union_map_n_map(executed
);
4699 struct isl_any_scheduled_after_data data
;
4700 struct isl_set_map_pair
*next
;
4701 struct isl_tarjan_graph
*g
= NULL
;
4702 isl_ast_graft_list
*list
= NULL
;
4705 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
4711 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
4717 data
.depth
= isl_ast_build_get_depth(build
);
4718 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
4719 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
4723 list
= isl_ast_graft_list_alloc(ctx
, 0);
4727 isl_ast_graft_list
*list_c
;
4730 if (g
->order
[i
] == -1)
4731 isl_die(ctx
, isl_error_internal
, "cannot happen",
4734 while (g
->order
[i
] != -1) {
4738 list_c
= generate_component(data
.domain
,
4739 g
->order
+ first
, i
- first
,
4740 isl_ast_build_copy(build
));
4741 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
4747 error
: list
= isl_ast_graft_list_free(list
);
4748 isl_tarjan_graph_free(g
);
4749 for (i
= 0; i
< n_domain
; ++i
) {
4750 isl_map_free(data
.domain
[i
].map
);
4751 isl_set_free(data
.domain
[i
].set
);
4754 isl_union_map_free(executed
);
4755 isl_ast_build_free(build
);
4760 /* Generate code for the next level (and all inner levels).
4762 * If "executed" is empty, i.e., no code needs to be generated,
4763 * then we return an empty list.
4765 * If we have already generated code for all loop levels, then we pass
4766 * control to generate_inner_level.
4768 * If "executed" lives in a single space, i.e., if code needs to be
4769 * generated for a single domain, then there can only be a single
4770 * component and we go directly to generate_shifted_component.
4771 * Otherwise, we call generate_components to detect the components
4772 * and to call generate_component on each of them separately.
4774 static __isl_give isl_ast_graft_list
*generate_next_level(
4775 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4779 if (!build
|| !executed
)
4782 if (isl_union_map_is_empty(executed
)) {
4783 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4784 isl_union_map_free(executed
);
4785 isl_ast_build_free(build
);
4786 return isl_ast_graft_list_alloc(ctx
, 0);
4789 depth
= isl_ast_build_get_depth(build
);
4790 if (depth
>= isl_ast_build_dim(build
, isl_dim_set
))
4791 return generate_inner_level(executed
, build
);
4793 if (isl_union_map_n_map(executed
) == 1)
4794 return generate_shifted_component(executed
, build
);
4796 return generate_components(executed
, build
);
4798 isl_union_map_free(executed
);
4799 isl_ast_build_free(build
);
4803 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4804 * internal, executed and build are the inputs to generate_code.
4805 * list collects the output.
4807 struct isl_generate_code_data
{
4809 isl_union_map
*executed
;
4810 isl_ast_build
*build
;
4812 isl_ast_graft_list
*list
;
4815 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4819 * with E the external build schedule and S the additional schedule "space",
4820 * reformulate the inverse schedule in terms of the internal schedule domain,
4825 * We first obtain a mapping
4829 * take the inverse and the product with S -> S, resulting in
4831 * [I -> S] -> [E -> S]
4833 * Applying the map to the input produces the desired result.
4835 static __isl_give isl_union_map
*internal_executed(
4836 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
4837 __isl_keep isl_ast_build
*build
)
4841 proj
= isl_ast_build_get_schedule_map(build
);
4842 proj
= isl_map_reverse(proj
);
4843 space
= isl_space_map_from_set(isl_space_copy(space
));
4844 id
= isl_map_identity(space
);
4845 proj
= isl_map_product(proj
, id
);
4846 executed
= isl_union_map_apply_domain(executed
,
4847 isl_union_map_from_map(proj
));
4851 /* Generate an AST that visits the elements in the range of data->executed
4852 * in the relative order specified by the corresponding domain element(s)
4853 * for those domain elements that belong to "set".
4854 * Add the result to data->list.
4856 * The caller ensures that "set" is a universe domain.
4857 * "space" is the space of the additional part of the schedule.
4858 * It is equal to the space of "set" if build->domain is parametric.
4859 * Otherwise, it is equal to the range of the wrapped space of "set".
4861 * If the build space is not parametric and
4862 * if isl_ast_build_node_from_schedule_map
4863 * was called from an outside user (data->internal not set), then
4864 * the (inverse) schedule refers to the external build domain and needs to
4865 * be transformed to refer to the internal build domain.
4867 * If the build space is parametric, then we add some of the parameter
4868 * constraints to the executed relation. Adding these constraints
4869 * allows for an earlier detection of conflicts in some cases.
4870 * However, we do not want to divide the executed relation into
4871 * more disjuncts than necessary. We therefore approximate
4872 * the constraints on the parameters by a single disjunct set.
4874 * The build is extended to include the additional part of the schedule.
4875 * If the original build space was not parametric, then the options
4876 * in data->build refer only to the additional part of the schedule
4877 * and they need to be adjusted to refer to the complete AST build
4880 * After having adjusted inverse schedule and build, we start generating
4881 * code with the outer loop of the current code generation
4882 * in generate_next_level.
4884 * If the original build space was not parametric, we undo the embedding
4885 * on the resulting isl_ast_node_list so that it can be used within
4886 * the outer AST build.
4888 static isl_stat
generate_code_in_space(struct isl_generate_code_data
*data
,
4889 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
4891 isl_union_map
*executed
;
4892 isl_ast_build
*build
;
4893 isl_ast_graft_list
*list
;
4896 executed
= isl_union_map_copy(data
->executed
);
4897 executed
= isl_union_map_intersect_domain(executed
,
4898 isl_union_set_from_set(set
));
4900 embed
= !isl_set_is_params(data
->build
->domain
);
4901 if (embed
&& !data
->internal
)
4902 executed
= internal_executed(executed
, space
, data
->build
);
4905 domain
= isl_ast_build_get_domain(data
->build
);
4906 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
4907 executed
= isl_union_map_intersect_params(executed
, domain
);
4910 build
= isl_ast_build_copy(data
->build
);
4911 build
= isl_ast_build_product(build
, space
);
4913 list
= generate_next_level(executed
, build
);
4915 list
= isl_ast_graft_list_unembed(list
, embed
);
4917 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
4922 /* Generate an AST that visits the elements in the range of data->executed
4923 * in the relative order specified by the corresponding domain element(s)
4924 * for those domain elements that belong to "set".
4925 * Add the result to data->list.
4927 * The caller ensures that "set" is a universe domain.
4929 * If the build space S is not parametric, then the space of "set"
4930 * need to be a wrapped relation with S as domain. That is, it needs
4935 * Check this property and pass control to generate_code_in_space
4937 * If the build space is not parametric, then T is the space of "set".
4939 static isl_stat
generate_code_set(__isl_take isl_set
*set
, void *user
)
4941 struct isl_generate_code_data
*data
= user
;
4942 isl_space
*space
, *build_space
;
4945 space
= isl_set_get_space(set
);
4947 if (isl_set_is_params(data
->build
->domain
))
4948 return generate_code_in_space(data
, set
, space
);
4950 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
4951 space
= isl_space_unwrap(space
);
4952 is_domain
= isl_space_is_domain(build_space
, space
);
4953 isl_space_free(build_space
);
4954 space
= isl_space_range(space
);
4959 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4960 "invalid nested schedule space", goto error
);
4962 return generate_code_in_space(data
, set
, space
);
4965 isl_space_free(space
);
4966 return isl_stat_error
;
4969 /* Generate an AST that visits the elements in the range of "executed"
4970 * in the relative order specified by the corresponding domain element(s).
4972 * "build" is an isl_ast_build that has either been constructed by
4973 * isl_ast_build_from_context or passed to a callback set by
4974 * isl_ast_build_set_create_leaf.
4975 * In the first case, the space of the isl_ast_build is typically
4976 * a parametric space, although this is currently not enforced.
4977 * In the second case, the space is never a parametric space.
4978 * If the space S is not parametric, then the domain space(s) of "executed"
4979 * need to be wrapped relations with S as domain.
4981 * If the domain of "executed" consists of several spaces, then an AST
4982 * is generated for each of them (in arbitrary order) and the results
4985 * If "internal" is set, then the domain "S" above refers to the internal
4986 * schedule domain representation. Otherwise, it refers to the external
4987 * representation, as returned by isl_ast_build_get_schedule_space.
4989 * We essentially run over all the spaces in the domain of "executed"
4990 * and call generate_code_set on each of them.
4992 static __isl_give isl_ast_graft_list
*generate_code(
4993 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
4997 struct isl_generate_code_data data
= { 0 };
4999 isl_union_set
*schedule_domain
;
5000 isl_union_map
*universe
;
5004 space
= isl_ast_build_get_space(build
, 1);
5005 space
= isl_space_align_params(space
,
5006 isl_union_map_get_space(executed
));
5007 space
= isl_space_align_params(space
,
5008 isl_union_map_get_space(build
->options
));
5009 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
5010 executed
= isl_union_map_align_params(executed
, space
);
5011 if (!executed
|| !build
)
5014 ctx
= isl_ast_build_get_ctx(build
);
5016 data
.internal
= internal
;
5017 data
.executed
= executed
;
5019 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
5021 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
5022 schedule_domain
= isl_union_map_domain(universe
);
5023 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
5025 data
.list
= isl_ast_graft_list_free(data
.list
);
5027 isl_union_set_free(schedule_domain
);
5028 isl_union_map_free(executed
);
5030 isl_ast_build_free(build
);
5033 isl_union_map_free(executed
);
5034 isl_ast_build_free(build
);
5038 /* Generate an AST that visits the elements in the domain of "schedule"
5039 * in the relative order specified by the corresponding image element(s).
5041 * "build" is an isl_ast_build that has either been constructed by
5042 * isl_ast_build_from_context or passed to a callback set by
5043 * isl_ast_build_set_create_leaf.
5044 * In the first case, the space of the isl_ast_build is typically
5045 * a parametric space, although this is currently not enforced.
5046 * In the second case, the space is never a parametric space.
5047 * If the space S is not parametric, then the range space(s) of "schedule"
5048 * need to be wrapped relations with S as domain.
5050 * If the range of "schedule" consists of several spaces, then an AST
5051 * is generated for each of them (in arbitrary order) and the results
5054 * We first initialize the local copies of the relevant options.
5055 * We do this here rather than when the isl_ast_build is created
5056 * because the options may have changed between the construction
5057 * of the isl_ast_build and the call to isl_generate_code.
5059 * The main computation is performed on an inverse schedule (with
5060 * the schedule domain in the domain and the elements to be executed
5061 * in the range) called "executed".
5063 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule_map(
5064 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
5066 isl_ast_graft_list
*list
;
5068 isl_union_map
*executed
;
5070 build
= isl_ast_build_copy(build
);
5071 build
= isl_ast_build_set_single_valued(build
, 0);
5072 schedule
= isl_union_map_coalesce(schedule
);
5073 schedule
= isl_union_map_remove_redundancies(schedule
);
5074 executed
= isl_union_map_reverse(schedule
);
5075 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
5076 node
= isl_ast_node_from_graft_list(list
, build
);
5077 isl_ast_build_free(build
);
5082 /* The old name for isl_ast_build_node_from_schedule_map.
5083 * It is being kept for backward compatibility, but
5084 * it will be removed in the future.
5086 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
5087 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
5089 return isl_ast_build_node_from_schedule_map(build
, schedule
);
5092 /* Generate an AST that visits the elements in the domain of "executed"
5093 * in the relative order specified by the band node "node" and its descendants.
5095 * The relation "executed" maps the outer generated loop iterators
5096 * to the domain elements executed by those iterations.
5098 * If the band is empty, we continue with its descendants.
5099 * Otherwise, we extend the build and the inverse schedule with
5100 * the additional space/partial schedule and continue generating
5101 * an AST in generate_next_level.
5102 * As soon as we have extended the inverse schedule with the additional
5103 * partial schedule, we look for equalities that may exists between
5104 * the old and the new part.
5106 static __isl_give isl_ast_graft_list
*build_ast_from_band(
5107 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5108 __isl_take isl_union_map
*executed
)
5111 isl_multi_union_pw_aff
*extra
;
5112 isl_union_map
*extra_umap
;
5113 isl_ast_graft_list
*list
;
5116 if (!build
|| !node
|| !executed
)
5119 if (isl_schedule_node_band_n_member(node
) == 0)
5120 return build_ast_from_child(build
, node
, executed
);
5122 extra
= isl_schedule_node_band_get_partial_schedule(node
);
5123 extra
= isl_multi_union_pw_aff_align_params(extra
,
5124 isl_ast_build_get_space(build
, 1));
5125 space
= isl_multi_union_pw_aff_get_space(extra
);
5127 extra_umap
= isl_union_map_from_multi_union_pw_aff(extra
);
5128 extra_umap
= isl_union_map_reverse(extra_umap
);
5130 executed
= isl_union_map_domain_product(executed
, extra_umap
);
5131 executed
= isl_union_map_detect_equalities(executed
);
5133 n1
= isl_ast_build_dim(build
, isl_dim_param
);
5134 build
= isl_ast_build_product(build
, space
);
5135 n2
= isl_ast_build_dim(build
, isl_dim_param
);
5137 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5138 "band node is not allowed to introduce new parameters",
5139 build
= isl_ast_build_free(build
));
5140 build
= isl_ast_build_set_schedule_node(build
, node
);
5142 list
= generate_next_level(executed
, build
);
5144 list
= isl_ast_graft_list_unembed(list
, 1);
5148 isl_schedule_node_free(node
);
5149 isl_union_map_free(executed
);
5150 isl_ast_build_free(build
);
5154 /* Hoist a list of grafts (in practice containing a single graft)
5155 * from "sub_build" (which includes extra context information)
5158 * In particular, project out all additional parameters introduced
5159 * by the context node from the enforced constraints and the guard
5160 * of the single graft.
5162 static __isl_give isl_ast_graft_list
*hoist_out_of_context(
5163 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
,
5164 __isl_keep isl_ast_build
*sub_build
)
5166 isl_ast_graft
*graft
;
5167 isl_basic_set
*enforced
;
5169 unsigned n_param
, extra_param
;
5171 if (!build
|| !sub_build
)
5172 return isl_ast_graft_list_free(list
);
5174 n_param
= isl_ast_build_dim(build
, isl_dim_param
);
5175 extra_param
= isl_ast_build_dim(sub_build
, isl_dim_param
);
5177 if (extra_param
== n_param
)
5180 extra_param
-= n_param
;
5181 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, sub_build
);
5182 enforced
= isl_basic_set_project_out(enforced
, isl_dim_param
,
5183 n_param
, extra_param
);
5184 enforced
= isl_basic_set_remove_unknown_divs(enforced
);
5185 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5186 guard
= isl_set_remove_divs_involving_dims(guard
, isl_dim_param
,
5187 n_param
, extra_param
);
5188 guard
= isl_set_project_out(guard
, isl_dim_param
, n_param
, extra_param
);
5189 guard
= isl_set_compute_divs(guard
);
5190 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5192 list
= isl_ast_graft_list_from_ast_graft(graft
);
5197 /* Generate an AST that visits the elements in the domain of "executed"
5198 * in the relative order specified by the context node "node"
5199 * and its descendants.
5201 * The relation "executed" maps the outer generated loop iterators
5202 * to the domain elements executed by those iterations.
5204 * The context node may introduce additional parameters as well as
5205 * constraints on the outer schedule dimensions or original parameters.
5207 * We add the extra parameters to a new build and the context
5208 * constraints to both the build and (as a single disjunct)
5209 * to the domain of "executed". Since the context constraints
5210 * are specified in terms of the input schedule, we first need
5211 * to map them to the internal schedule domain.
5213 * After constructing the AST from the descendants of "node",
5214 * we combine the list of grafts into a single graft within
5215 * the new build, in order to be able to exploit the additional
5216 * context constraints during this combination.
5218 * Additionally, if the current node is the outermost node in
5219 * the schedule tree (apart from the root domain node), we generate
5220 * all pending guards, again to be able to exploit the additional
5221 * context constraints. We currently do not do this for internal
5222 * context nodes since we may still want to hoist conditions
5223 * to outer AST nodes.
5225 * If the context node introduced any new parameters, then they
5226 * are removed from the set of enforced constraints and guard
5227 * in hoist_out_of_context.
5229 static __isl_give isl_ast_graft_list
*build_ast_from_context(
5230 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5231 __isl_take isl_union_map
*executed
)
5235 isl_multi_aff
*internal2input
;
5236 isl_ast_build
*sub_build
;
5237 isl_ast_graft_list
*list
;
5240 depth
= isl_schedule_node_get_tree_depth(node
);
5241 space
= isl_ast_build_get_space(build
, 1);
5242 context
= isl_schedule_node_context_get_context(node
);
5243 context
= isl_set_align_params(context
, space
);
5244 sub_build
= isl_ast_build_copy(build
);
5245 space
= isl_set_get_space(context
);
5246 sub_build
= isl_ast_build_align_params(sub_build
, space
);
5247 internal2input
= isl_ast_build_get_internal2input(sub_build
);
5248 context
= isl_set_preimage_multi_aff(context
, internal2input
);
5249 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5250 isl_set_copy(context
));
5251 context
= isl_set_from_basic_set(isl_set_simple_hull(context
));
5252 executed
= isl_union_map_intersect_domain(executed
,
5253 isl_union_set_from_set(context
));
5255 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5257 n
= isl_ast_graft_list_n_ast_graft(list
);
5259 list
= isl_ast_graft_list_free(list
);
5261 list
= isl_ast_graft_list_fuse(list
, sub_build
);
5263 list
= isl_ast_graft_list_insert_pending_guard_nodes(list
,
5266 list
= hoist_out_of_context(list
, build
, sub_build
);
5268 isl_ast_build_free(build
);
5269 isl_ast_build_free(sub_build
);
5274 /* Generate an AST that visits the elements in the domain of "executed"
5275 * in the relative order specified by the expansion node "node" and
5278 * The relation "executed" maps the outer generated loop iterators
5279 * to the domain elements executed by those iterations.
5281 * We expand the domain elements by the expansion and
5282 * continue with the descendants of the node.
5284 static __isl_give isl_ast_graft_list
*build_ast_from_expansion(
5285 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5286 __isl_take isl_union_map
*executed
)
5288 isl_union_map
*expansion
;
5291 expansion
= isl_schedule_node_expansion_get_expansion(node
);
5292 expansion
= isl_union_map_align_params(expansion
,
5293 isl_union_map_get_space(executed
));
5295 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5296 executed
= isl_union_map_apply_range(executed
, expansion
);
5297 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5299 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5300 "expansion node is not allowed to introduce "
5301 "new parameters", goto error
);
5303 return build_ast_from_child(build
, node
, executed
);
5305 isl_ast_build_free(build
);
5306 isl_schedule_node_free(node
);
5307 isl_union_map_free(executed
);
5311 /* Generate an AST that visits the elements in the domain of "executed"
5312 * in the relative order specified by the extension node "node" and
5315 * The relation "executed" maps the outer generated loop iterators
5316 * to the domain elements executed by those iterations.
5318 * Extend the inverse schedule with the extension applied to current
5319 * set of generated constraints. Since the extension if formulated
5320 * in terms of the input schedule, it first needs to be transformed
5321 * to refer to the internal schedule.
5323 static __isl_give isl_ast_graft_list
*build_ast_from_extension(
5324 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5325 __isl_take isl_union_map
*executed
)
5327 isl_union_set
*schedule_domain
;
5328 isl_union_map
*extension
;
5331 set
= isl_ast_build_get_generated(build
);
5332 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5333 schedule_domain
= isl_union_set_from_set(set
);
5335 extension
= isl_schedule_node_extension_get_extension(node
);
5337 extension
= isl_union_map_preimage_domain_multi_aff(extension
,
5338 isl_multi_aff_copy(build
->internal2input
));
5339 extension
= isl_union_map_intersect_domain(extension
, schedule_domain
);
5340 extension
= isl_ast_build_substitute_values_union_map_domain(build
,
5342 executed
= isl_union_map_union(executed
, extension
);
5344 return build_ast_from_child(build
, node
, executed
);
5347 /* Generate an AST that visits the elements in the domain of "executed"
5348 * in the relative order specified by the filter node "node" and
5351 * The relation "executed" maps the outer generated loop iterators
5352 * to the domain elements executed by those iterations.
5354 * We simply intersect the iteration domain (i.e., the range of "executed")
5355 * with the filter and continue with the descendants of the node,
5356 * unless the resulting inverse schedule is empty, in which
5357 * case we return an empty list.
5359 * If the result of the intersection is equal to the original "executed"
5360 * relation, then keep the original representation since the intersection
5361 * may have unnecessarily broken up the relation into a greater number
5364 static __isl_give isl_ast_graft_list
*build_ast_from_filter(
5365 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5366 __isl_take isl_union_map
*executed
)
5369 isl_union_set
*filter
;
5370 isl_union_map
*orig
;
5371 isl_ast_graft_list
*list
;
5376 orig
= isl_union_map_copy(executed
);
5377 if (!build
|| !node
|| !executed
)
5380 filter
= isl_schedule_node_filter_get_filter(node
);
5381 filter
= isl_union_set_align_params(filter
,
5382 isl_union_map_get_space(executed
));
5383 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5384 executed
= isl_union_map_intersect_range(executed
, filter
);
5385 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5387 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5388 "filter node is not allowed to introduce "
5389 "new parameters", goto error
);
5391 unchanged
= isl_union_map_is_subset(orig
, executed
);
5392 empty
= isl_union_map_is_empty(executed
);
5393 if (unchanged
< 0 || empty
< 0)
5396 isl_union_map_free(executed
);
5397 return build_ast_from_child(build
, node
, orig
);
5399 isl_union_map_free(orig
);
5401 return build_ast_from_child(build
, node
, executed
);
5403 ctx
= isl_ast_build_get_ctx(build
);
5404 list
= isl_ast_graft_list_alloc(ctx
, 0);
5405 isl_ast_build_free(build
);
5406 isl_schedule_node_free(node
);
5407 isl_union_map_free(executed
);
5410 isl_ast_build_free(build
);
5411 isl_schedule_node_free(node
);
5412 isl_union_map_free(executed
);
5413 isl_union_map_free(orig
);
5417 /* Generate an AST that visits the elements in the domain of "executed"
5418 * in the relative order specified by the guard node "node" and
5421 * The relation "executed" maps the outer generated loop iterators
5422 * to the domain elements executed by those iterations.
5424 * Ensure that the associated guard is enforced by the outer AST
5425 * constructs by adding it to the guard of the graft.
5426 * Since we know that we will enforce the guard, we can also include it
5427 * in the generated constraints used to construct an AST for
5428 * the descendant nodes.
5430 static __isl_give isl_ast_graft_list
*build_ast_from_guard(
5431 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5432 __isl_take isl_union_map
*executed
)
5435 isl_set
*guard
, *hoisted
;
5436 isl_basic_set
*enforced
;
5437 isl_ast_build
*sub_build
;
5438 isl_ast_graft
*graft
;
5439 isl_ast_graft_list
*list
;
5442 space
= isl_ast_build_get_space(build
, 1);
5443 guard
= isl_schedule_node_guard_get_guard(node
);
5444 n1
= isl_space_dim(space
, isl_dim_param
);
5445 guard
= isl_set_align_params(guard
, space
);
5446 n2
= isl_set_dim(guard
, isl_dim_param
);
5448 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5449 "guard node is not allowed to introduce "
5450 "new parameters", guard
= isl_set_free(guard
));
5451 guard
= isl_set_preimage_multi_aff(guard
,
5452 isl_multi_aff_copy(build
->internal2input
));
5453 guard
= isl_ast_build_specialize(build
, guard
);
5454 guard
= isl_set_gist(guard
, isl_set_copy(build
->generated
));
5456 sub_build
= isl_ast_build_copy(build
);
5457 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5458 isl_set_copy(guard
));
5460 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5463 hoisted
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5464 if (isl_set_n_basic_set(hoisted
) > 1)
5465 list
= isl_ast_graft_list_gist_guards(list
,
5466 isl_set_copy(hoisted
));
5467 guard
= isl_set_intersect(guard
, hoisted
);
5468 enforced
= extract_shared_enforced(list
, build
);
5469 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5472 isl_ast_build_free(sub_build
);
5473 isl_ast_build_free(build
);
5474 return isl_ast_graft_list_from_ast_graft(graft
);
5477 /* Call the before_each_mark callback, if requested by the user.
5479 * Return 0 on success and -1 on error.
5481 * The caller is responsible for recording the current inverse schedule
5484 static isl_stat
before_each_mark(__isl_keep isl_id
*mark
,
5485 __isl_keep isl_ast_build
*build
)
5488 return isl_stat_error
;
5489 if (!build
->before_each_mark
)
5491 return build
->before_each_mark(mark
, build
,
5492 build
->before_each_mark_user
);
5495 /* Call the after_each_mark callback, if requested by the user.
5497 * The caller is responsible for recording the current inverse schedule
5500 static __isl_give isl_ast_graft
*after_each_mark(
5501 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
5503 if (!graft
|| !build
)
5504 return isl_ast_graft_free(graft
);
5505 if (!build
->after_each_mark
)
5507 graft
->node
= build
->after_each_mark(graft
->node
, build
,
5508 build
->after_each_mark_user
);
5510 return isl_ast_graft_free(graft
);
5515 /* Generate an AST that visits the elements in the domain of "executed"
5516 * in the relative order specified by the mark node "node" and
5519 * The relation "executed" maps the outer generated loop iterators
5520 * to the domain elements executed by those iterations.
5522 * Since we may be calling before_each_mark and after_each_mark
5523 * callbacks, we record the current inverse schedule in the build.
5525 * We generate an AST for the child of the mark node, combine
5526 * the graft list into a single graft and then insert the mark
5527 * in the AST of that single graft.
5529 static __isl_give isl_ast_graft_list
*build_ast_from_mark(
5530 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5531 __isl_take isl_union_map
*executed
)
5534 isl_ast_graft
*graft
;
5535 isl_ast_graft_list
*list
;
5538 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
5540 mark
= isl_schedule_node_mark_get_id(node
);
5541 if (before_each_mark(mark
, build
) < 0)
5542 node
= isl_schedule_node_free(node
);
5544 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5545 list
= isl_ast_graft_list_fuse(list
, build
);
5546 n
= isl_ast_graft_list_n_ast_graft(list
);
5548 list
= isl_ast_graft_list_free(list
);
5552 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
5553 graft
= isl_ast_graft_insert_mark(graft
, mark
);
5554 graft
= after_each_mark(graft
, build
);
5555 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
5557 isl_ast_build_free(build
);
5562 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5563 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5564 __isl_take isl_union_map
*executed
);
5566 /* Generate an AST that visits the elements in the domain of "executed"
5567 * in the relative order specified by the sequence (or set) node "node" and
5570 * The relation "executed" maps the outer generated loop iterators
5571 * to the domain elements executed by those iterations.
5573 * We simply generate an AST for each of the children and concatenate
5576 static __isl_give isl_ast_graft_list
*build_ast_from_sequence(
5577 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5578 __isl_take isl_union_map
*executed
)
5582 isl_ast_graft_list
*list
;
5584 ctx
= isl_ast_build_get_ctx(build
);
5585 list
= isl_ast_graft_list_alloc(ctx
, 0);
5587 n
= isl_schedule_node_n_children(node
);
5588 for (i
= 0; i
< n
; ++i
) {
5589 isl_schedule_node
*child
;
5590 isl_ast_graft_list
*list_i
;
5592 child
= isl_schedule_node_get_child(node
, i
);
5593 list_i
= build_ast_from_schedule_node(isl_ast_build_copy(build
),
5594 child
, isl_union_map_copy(executed
));
5595 list
= isl_ast_graft_list_concat(list
, list_i
);
5597 isl_ast_build_free(build
);
5598 isl_schedule_node_free(node
);
5599 isl_union_map_free(executed
);
5604 /* Generate an AST that visits the elements in the domain of "executed"
5605 * in the relative order specified by the node "node" and its descendants.
5607 * The relation "executed" maps the outer generated loop iterators
5608 * to the domain elements executed by those iterations.
5610 * If the node is a leaf, then we pass control to generate_inner_level.
5611 * Note that the current build does not refer to any band node, so
5612 * that generate_inner_level will not try to visit the child of
5615 * The other node types are handled in separate functions.
5616 * Set nodes are currently treated in the same way as sequence nodes.
5617 * The children of a set node may be executed in any order,
5618 * including the order of the children.
5620 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5621 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5622 __isl_take isl_union_map
*executed
)
5624 enum isl_schedule_node_type type
;
5626 type
= isl_schedule_node_get_type(node
);
5629 case isl_schedule_node_error
:
5631 case isl_schedule_node_leaf
:
5632 isl_schedule_node_free(node
);
5633 return generate_inner_level(executed
, build
);
5634 case isl_schedule_node_band
:
5635 return build_ast_from_band(build
, node
, executed
);
5636 case isl_schedule_node_context
:
5637 return build_ast_from_context(build
, node
, executed
);
5638 case isl_schedule_node_domain
:
5639 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
5640 "unexpected internal domain node", goto error
);
5641 case isl_schedule_node_expansion
:
5642 return build_ast_from_expansion(build
, node
, executed
);
5643 case isl_schedule_node_extension
:
5644 return build_ast_from_extension(build
, node
, executed
);
5645 case isl_schedule_node_filter
:
5646 return build_ast_from_filter(build
, node
, executed
);
5647 case isl_schedule_node_guard
:
5648 return build_ast_from_guard(build
, node
, executed
);
5649 case isl_schedule_node_mark
:
5650 return build_ast_from_mark(build
, node
, executed
);
5651 case isl_schedule_node_sequence
:
5652 case isl_schedule_node_set
:
5653 return build_ast_from_sequence(build
, node
, executed
);
5656 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
5657 "unhandled type", goto error
);
5659 isl_union_map_free(executed
);
5660 isl_schedule_node_free(node
);
5661 isl_ast_build_free(build
);
5666 /* Generate an AST that visits the elements in the domain of "executed"
5667 * in the relative order specified by the (single) child of "node" and
5670 * The relation "executed" maps the outer generated loop iterators
5671 * to the domain elements executed by those iterations.
5673 * This function is never called on a leaf, set or sequence node,
5674 * so the node always has exactly one child.
5676 static __isl_give isl_ast_graft_list
*build_ast_from_child(
5677 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5678 __isl_take isl_union_map
*executed
)
5680 node
= isl_schedule_node_child(node
, 0);
5681 return build_ast_from_schedule_node(build
, node
, executed
);
5684 /* Generate an AST that visits the elements in the domain of the domain
5685 * node "node" in the relative order specified by its descendants.
5687 * An initial inverse schedule is created that maps a zero-dimensional
5688 * schedule space to the node domain.
5689 * The input "build" is assumed to have a parametric domain and
5690 * is replaced by the same zero-dimensional schedule space.
5692 * We also add some of the parameter constraints in the build domain
5693 * to the executed relation. Adding these constraints
5694 * allows for an earlier detection of conflicts in some cases.
5695 * However, we do not want to divide the executed relation into
5696 * more disjuncts than necessary. We therefore approximate
5697 * the constraints on the parameters by a single disjunct set.
5699 static __isl_give isl_ast_node
*build_ast_from_domain(
5700 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
)
5703 isl_union_set
*domain
, *schedule_domain
;
5704 isl_union_map
*executed
;
5707 isl_ast_graft_list
*list
;
5714 ctx
= isl_ast_build_get_ctx(build
);
5715 space
= isl_ast_build_get_space(build
, 1);
5716 is_params
= isl_space_is_params(space
);
5717 isl_space_free(space
);
5721 isl_die(ctx
, isl_error_unsupported
,
5722 "expecting parametric initial context", goto error
);
5724 domain
= isl_schedule_node_domain_get_domain(node
);
5725 domain
= isl_union_set_coalesce(domain
);
5727 space
= isl_union_set_get_space(domain
);
5728 space
= isl_space_set_from_params(space
);
5729 build
= isl_ast_build_product(build
, space
);
5731 set
= isl_ast_build_get_domain(build
);
5732 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5733 schedule_domain
= isl_union_set_from_set(set
);
5735 executed
= isl_union_map_from_domain_and_range(schedule_domain
, domain
);
5736 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5737 ast
= isl_ast_node_from_graft_list(list
, build
);
5738 isl_ast_build_free(build
);
5742 isl_schedule_node_free(node
);
5743 isl_ast_build_free(build
);
5747 /* Generate an AST that visits the elements in the domain of "schedule"
5748 * in the relative order specified by the schedule tree.
5750 * "build" is an isl_ast_build that has been created using
5751 * isl_ast_build_alloc or isl_ast_build_from_context based
5752 * on a parametric set.
5754 * The construction starts at the root node of the schedule,
5755 * which is assumed to be a domain node.
5757 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule(
5758 __isl_keep isl_ast_build
*build
, __isl_take isl_schedule
*schedule
)
5761 isl_schedule_node
*node
;
5763 if (!build
|| !schedule
)
5766 ctx
= isl_ast_build_get_ctx(build
);
5768 node
= isl_schedule_get_root(schedule
);
5771 isl_schedule_free(schedule
);
5773 build
= isl_ast_build_copy(build
);
5774 build
= isl_ast_build_set_single_valued(build
, 0);
5775 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
5776 isl_die(ctx
, isl_error_unsupported
,
5777 "expecting root domain node",
5778 build
= isl_ast_build_free(build
));
5779 return build_ast_from_domain(build
, node
);
5781 isl_schedule_free(schedule
);