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
17 #include <isl/union_map.h>
18 #include <isl/schedule_node.h>
20 #include <isl_tarjan.h>
21 #include <isl_ast_private.h>
22 #include <isl_ast_build_expr.h>
23 #include <isl_ast_build_private.h>
24 #include <isl_ast_graft_private.h>
26 /* Data used in generate_domain.
28 * "build" is the input build.
29 * "list" collects the results.
31 struct isl_generate_domain_data
{
34 isl_ast_graft_list
*list
;
37 static __isl_give isl_ast_graft_list
*generate_next_level(
38 __isl_take isl_union_map
*executed
,
39 __isl_take isl_ast_build
*build
);
40 static __isl_give isl_ast_graft_list
*generate_code(
41 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
44 /* Generate an AST for a single domain based on
45 * the (non single valued) inverse schedule "executed".
47 * We extend the schedule with the iteration domain
48 * and continue generating through a call to generate_code.
50 * In particular, if executed has the form
54 * then we continue generating code on
58 * The extended inverse schedule is clearly single valued
59 * ensuring that the nested generate_code will not reach this function,
60 * but will instead create calls to all elements of D that need
61 * to be executed from the current schedule domain.
63 static int generate_non_single_valued(__isl_take isl_map
*executed
,
64 struct isl_generate_domain_data
*data
)
68 isl_ast_graft_list
*list
;
70 build
= isl_ast_build_copy(data
->build
);
72 identity
= isl_set_identity(isl_map_range(isl_map_copy(executed
)));
73 executed
= isl_map_domain_product(executed
, identity
);
74 build
= isl_ast_build_set_single_valued(build
, 1);
76 list
= generate_code(isl_union_map_from_map(executed
), build
, 1);
78 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
83 /* Call the at_each_domain callback, if requested by the user,
84 * after recording the current inverse schedule in the build.
86 static __isl_give isl_ast_graft
*at_each_domain(__isl_take isl_ast_graft
*graft
,
87 __isl_keep isl_map
*executed
, __isl_keep isl_ast_build
*build
)
90 return isl_ast_graft_free(graft
);
91 if (!build
->at_each_domain
)
94 build
= isl_ast_build_copy(build
);
95 build
= isl_ast_build_set_executed(build
,
96 isl_union_map_from_map(isl_map_copy(executed
)));
98 return isl_ast_graft_free(graft
);
100 graft
->node
= build
->at_each_domain(graft
->node
,
101 build
, build
->at_each_domain_user
);
102 isl_ast_build_free(build
);
105 graft
= isl_ast_graft_free(graft
);
110 /* Generate an AST for a single domain based on
111 * the inverse schedule "executed" and add it to data->list.
113 * If there is more than one domain element associated to the current
114 * schedule "time", then we need to continue the generation process
115 * in generate_non_single_valued.
116 * Note that the inverse schedule being single-valued may depend
117 * on constraints that are only available in the original context
118 * domain specified by the user. We therefore first introduce
119 * some of the constraints of data->build->domain. In particular,
120 * we intersect with a single-disjunct approximation of this set.
121 * We perform this approximation to avoid further splitting up
122 * the executed relation, possibly introducing a disjunctive guard
125 * On the other hand, we only perform the test after having taken the gist
126 * of the domain as the resulting map is the one from which the call
127 * expression is constructed. Using this map to construct the call
128 * expression usually yields simpler results.
129 * Because we perform the single-valuedness test on the gisted map,
130 * we may in rare cases fail to recognize that the inverse schedule
131 * is single-valued. This becomes problematic if this happens
132 * from the recursive call through generate_non_single_valued
133 * as we would then end up in an infinite recursion.
134 * We therefore check if we are inside a call to generate_non_single_valued
135 * and revert to the ungisted map if the gisted map turns out not to be
138 * Otherwise, we generate a call expression for the single executed
139 * domain element and put a guard around it based on the (simplified)
140 * domain of "executed".
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 * We therefore turn all pending constraints into guards
146 * (after simplifying them with respect to the already generated
147 * constraints) and add them to both the generated constraints
148 * and the guard of the constructed graft. This guard will ensure
149 * that the constraints are effectively generated.
151 * If the user has set an at_each_domain callback, it is called
152 * on the constructed call expression node.
154 static int generate_domain(__isl_take isl_map
*executed
, void *user
)
156 struct isl_generate_domain_data
*data
= user
;
157 isl_ast_build
*build
;
158 isl_ast_graft
*graft
;
159 isl_ast_graft_list
*list
;
160 isl_set
*guard
, *domain
;
164 domain
= isl_ast_build_get_domain(data
->build
);
165 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
166 executed
= isl_map_intersect_domain(executed
, domain
);
167 empty
= isl_map_is_empty(executed
);
171 isl_map_free(executed
);
175 executed
= isl_map_coalesce(executed
);
176 map
= isl_map_copy(executed
);
177 map
= isl_ast_build_compute_gist_map_domain(data
->build
, map
);
178 sv
= isl_map_is_single_valued(map
);
183 if (data
->build
->single_valued
)
184 map
= isl_map_copy(executed
);
186 return generate_non_single_valued(executed
, data
);
188 guard
= isl_map_domain(isl_map_copy(map
));
189 guard
= isl_set_compute_divs(guard
);
190 guard
= isl_set_intersect(guard
,
191 isl_ast_build_get_pending(data
->build
));
192 guard
= isl_set_coalesce(guard
);
193 guard
= isl_ast_build_specialize(data
->build
, guard
);
194 guard
= isl_set_gist(guard
, isl_ast_build_get_generated(data
->build
));
196 build
= isl_ast_build_copy(data
->build
);
197 build
= isl_ast_build_replace_pending_by_guard(build
,
198 isl_set_copy(guard
));
199 graft
= isl_ast_graft_alloc_domain(map
, build
);
200 graft
= at_each_domain(graft
, executed
, build
);
201 isl_ast_build_free(build
);
202 isl_map_free(executed
);
203 graft
= isl_ast_graft_add_guard(graft
, guard
, data
->build
);
205 list
= isl_ast_graft_list_from_ast_graft(graft
);
206 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
211 isl_map_free(executed
);
215 /* Call build->create_leaf to a create "leaf" node in the AST,
216 * encapsulate the result in an isl_ast_graft and return the result
217 * as a 1-element list.
219 * Note that the node returned by the user may be an entire tree.
221 * Since the node itself cannot enforce any constraints, we turn
222 * all pending constraints into guards and add them to the resulting
223 * graft to ensure that they will be generated.
225 * Before we pass control to the user, we first clear some information
226 * from the build that is (presumbably) only meaningful
227 * for the current code generation.
228 * This includes the create_leaf callback itself, so we make a copy
229 * of the build first.
231 static __isl_give isl_ast_graft_list
*call_create_leaf(
232 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
236 isl_ast_graft
*graft
;
237 isl_ast_build
*user_build
;
239 guard
= isl_ast_build_get_pending(build
);
240 user_build
= isl_ast_build_copy(build
);
241 user_build
= isl_ast_build_replace_pending_by_guard(user_build
,
242 isl_set_copy(guard
));
243 user_build
= isl_ast_build_set_executed(user_build
, executed
);
244 user_build
= isl_ast_build_clear_local_info(user_build
);
248 node
= build
->create_leaf(user_build
, build
->create_leaf_user
);
249 graft
= isl_ast_graft_alloc(node
, build
);
250 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
251 isl_ast_build_free(build
);
252 return isl_ast_graft_list_from_ast_graft(graft
);
255 static __isl_give isl_ast_graft_list
*build_ast_from_child(
256 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
257 __isl_take isl_union_map
*executed
);
259 /* Generate an AST after having handled the complete schedule
260 * of this call to the code generator or the complete band
261 * if we are generating an AST from a schedule tree.
263 * If we are inside a band node, then move on to the child of the band.
265 * If the user has specified a create_leaf callback, control
266 * is passed to the user in call_create_leaf.
268 * Otherwise, we generate one or more calls for each individual
269 * domain in generate_domain.
271 static __isl_give isl_ast_graft_list
*generate_inner_level(
272 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
275 struct isl_generate_domain_data data
= { build
};
277 if (!build
|| !executed
)
280 if (isl_ast_build_has_schedule_node(build
)) {
281 isl_schedule_node
*node
;
282 node
= isl_ast_build_get_schedule_node(build
);
283 build
= isl_ast_build_reset_schedule_node(build
);
284 return build_ast_from_child(build
, node
, executed
);
287 if (build
->create_leaf
)
288 return call_create_leaf(executed
, build
);
290 ctx
= isl_union_map_get_ctx(executed
);
291 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
292 if (isl_union_map_foreach_map(executed
, &generate_domain
, &data
) < 0)
293 data
.list
= isl_ast_graft_list_free(data
.list
);
296 error
: data
.list
= NULL
;
297 isl_ast_build_free(build
);
298 isl_union_map_free(executed
);
302 /* Call the before_each_for callback, if requested by the user.
304 static __isl_give isl_ast_node
*before_each_for(__isl_take isl_ast_node
*node
,
305 __isl_keep isl_ast_build
*build
)
310 return isl_ast_node_free(node
);
311 if (!build
->before_each_for
)
313 id
= build
->before_each_for(build
, build
->before_each_for_user
);
314 node
= isl_ast_node_set_annotation(node
, id
);
318 /* Call the after_each_for callback, if requested by the user.
320 static __isl_give isl_ast_graft
*after_each_for(__isl_take isl_ast_graft
*graft
,
321 __isl_keep isl_ast_build
*build
)
323 if (!graft
|| !build
)
324 return isl_ast_graft_free(graft
);
325 if (!build
->after_each_for
)
327 graft
->node
= build
->after_each_for(graft
->node
, build
,
328 build
->after_each_for_user
);
330 return isl_ast_graft_free(graft
);
334 /* Plug in all the know values of the current and outer dimensions
335 * in the domain of "executed". In principle, we only need to plug
336 * in the known value of the current dimension since the values of
337 * outer dimensions have been plugged in already.
338 * However, it turns out to be easier to just plug in all known values.
340 static __isl_give isl_union_map
*plug_in_values(
341 __isl_take isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
343 return isl_ast_build_substitute_values_union_map_domain(build
,
347 /* Check if the constraint "c" is a lower bound on dimension "pos",
348 * an upper bound, or independent of dimension "pos".
350 static int constraint_type(isl_constraint
*c
, int pos
)
352 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
354 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
359 /* Compare the types of the constraints "a" and "b",
360 * resulting in constraints that are independent of "depth"
361 * to be sorted before the lower bounds on "depth", which in
362 * turn are sorted before the upper bounds on "depth".
364 static int cmp_constraint(__isl_keep isl_constraint
*a
,
365 __isl_keep isl_constraint
*b
, void *user
)
368 int t1
= constraint_type(a
, *depth
);
369 int t2
= constraint_type(b
, *depth
);
374 /* Extract a lower bound on dimension "pos" from constraint "c".
376 * If the constraint is of the form
380 * then we essentially return
382 * l = ceil(-f(...)/a)
384 * However, if the current dimension is strided, then we need to make
385 * sure that the lower bound we construct is of the form
389 * with f the offset and s the stride.
390 * We therefore compute
392 * f + s * ceil((l - f)/s)
394 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
395 int pos
, __isl_keep isl_ast_build
*build
)
399 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
400 aff
= isl_aff_ceil(aff
);
402 if (isl_ast_build_has_stride(build
, pos
)) {
406 offset
= isl_ast_build_get_offset(build
, pos
);
407 stride
= isl_ast_build_get_stride(build
, pos
);
409 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
410 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(stride
));
411 aff
= isl_aff_ceil(aff
);
412 aff
= isl_aff_scale_val(aff
, stride
);
413 aff
= isl_aff_add(aff
, offset
);
416 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
421 /* Return the exact lower bound (or upper bound if "upper" is set)
422 * of "domain" as a piecewise affine expression.
424 * If we are computing a lower bound (of a strided dimension), then
425 * we need to make sure it is of the form
429 * where f is the offset and s is the stride.
430 * We therefore need to include the stride constraint before computing
433 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
434 __isl_keep isl_ast_build
*build
, int upper
)
439 isl_pw_multi_aff
*pma
;
441 domain
= isl_set_copy(domain
);
443 stride
= isl_ast_build_get_stride_constraint(build
);
444 domain
= isl_set_intersect(domain
, stride
);
446 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
448 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
450 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
451 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
452 isl_pw_multi_aff_free(pma
);
453 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
454 pa
= isl_pw_aff_coalesce(pa
);
459 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
460 * remove_redundant_lower_bounds.
462 static int reduce_list_cmp(__isl_keep isl_pw_aff
*a
, __isl_keep isl_pw_aff
*b
,
465 return isl_pw_aff_plain_cmp(a
, b
);
468 /* Given a list of lower bounds "list", remove those that are redundant
469 * with respect to the other bounds in "list" and the domain of "build".
471 * We first sort the bounds in the same way as they would be sorted
472 * by set_for_node_expressions so that we can try and remove the last
475 * For a lower bound to be effective, there needs to be at least
476 * one domain element for which it is larger than all other lower bounds.
477 * For each lower bound we therefore intersect the domain with
478 * the conditions that it is larger than all other bounds and
479 * check whether the result is empty. If so, the bound can be removed.
481 static __isl_give isl_pw_aff_list
*remove_redundant_lower_bounds(
482 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
487 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
491 n
= isl_pw_aff_list_n_pw_aff(list
);
495 domain
= isl_ast_build_get_domain(build
);
497 for (i
= n
- 1; i
>= 0; --i
) {
502 domain_i
= isl_set_copy(domain
);
503 pa_i
= isl_pw_aff_list_get_pw_aff(list
, i
);
505 for (j
= 0; j
< n
; ++j
) {
512 pa_j
= isl_pw_aff_list_get_pw_aff(list
, j
);
513 better
= isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i
), pa_j
);
514 domain_i
= isl_set_intersect(domain_i
, better
);
517 empty
= isl_set_is_empty(domain_i
);
519 isl_set_free(domain_i
);
520 isl_pw_aff_free(pa_i
);
526 list
= isl_pw_aff_list_drop(list
, i
, 1);
530 isl_set_free(domain
);
534 isl_set_free(domain
);
535 return isl_pw_aff_list_free(list
);
538 /* Extract a lower bound on dimension "pos" from each constraint
539 * in "constraints" and return the list of lower bounds.
540 * If "constraints" has zero elements, then we extract a lower bound
541 * from "domain" instead.
543 * If the current dimension is strided, then the lower bound
544 * is adjusted by lower_bound to match the stride information.
545 * This modification may make one or more lower bounds redundant
546 * with respect to the other lower bounds. We therefore check
547 * for this condition and remove the redundant lower bounds.
549 static __isl_give isl_pw_aff_list
*lower_bounds(
550 __isl_keep isl_constraint_list
*constraints
, int pos
,
551 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
554 isl_pw_aff_list
*list
;
560 n
= isl_constraint_list_n_constraint(constraints
);
563 pa
= exact_bound(domain
, build
, 0);
564 return isl_pw_aff_list_from_pw_aff(pa
);
567 ctx
= isl_ast_build_get_ctx(build
);
568 list
= isl_pw_aff_list_alloc(ctx
,n
);
570 for (i
= 0; i
< n
; ++i
) {
574 c
= isl_constraint_list_get_constraint(constraints
, i
);
575 aff
= lower_bound(c
, pos
, build
);
576 isl_constraint_free(c
);
577 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
580 if (isl_ast_build_has_stride(build
, pos
))
581 list
= remove_redundant_lower_bounds(list
, build
);
586 /* Extract an upper bound on dimension "pos" from each constraint
587 * in "constraints" and return the list of upper bounds.
588 * If "constraints" has zero elements, then we extract an upper bound
589 * from "domain" instead.
591 static __isl_give isl_pw_aff_list
*upper_bounds(
592 __isl_keep isl_constraint_list
*constraints
, int pos
,
593 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
596 isl_pw_aff_list
*list
;
599 n
= isl_constraint_list_n_constraint(constraints
);
602 pa
= exact_bound(domain
, build
, 1);
603 return isl_pw_aff_list_from_pw_aff(pa
);
606 ctx
= isl_ast_build_get_ctx(build
);
607 list
= isl_pw_aff_list_alloc(ctx
,n
);
609 for (i
= 0; i
< n
; ++i
) {
613 c
= isl_constraint_list_get_constraint(constraints
, i
);
614 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
615 isl_constraint_free(c
);
616 aff
= isl_aff_floor(aff
);
617 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
623 /* Return an isl_ast_expr that performs the reduction of type "type"
624 * on AST expressions corresponding to the elements in "list".
626 * The list is assumed to contain at least one element.
627 * If the list contains exactly one element, then the returned isl_ast_expr
628 * simply computes that affine expression.
629 * If the list contains more than one element, then we sort it
630 * using a fairly abitrary but hopefully reasonably stable order.
632 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
633 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
642 n
= isl_pw_aff_list_n_pw_aff(list
);
645 return isl_ast_build_expr_from_pw_aff_internal(build
,
646 isl_pw_aff_list_get_pw_aff(list
, 0));
648 ctx
= isl_pw_aff_list_get_ctx(list
);
649 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
653 list
= isl_pw_aff_list_copy(list
);
654 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
656 return isl_ast_expr_free(expr
);
658 for (i
= 0; i
< n
; ++i
) {
659 isl_ast_expr
*expr_i
;
661 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
662 isl_pw_aff_list_get_pw_aff(list
, i
));
665 expr
->u
.op
.args
[i
] = expr_i
;
668 isl_pw_aff_list_free(list
);
671 isl_pw_aff_list_free(list
);
672 isl_ast_expr_free(expr
);
676 /* Add guards implied by the "generated constraints",
677 * but not (necessarily) enforced by the generated AST to "guard".
678 * In particular, if there is any stride constraints,
679 * then add the guard implied by those constraints.
680 * If we have generated a degenerate loop, then add the guard
681 * implied by "bounds" on the outer dimensions, i.e., the guard
682 * that ensures that the single value actually exists.
683 * Since there may also be guards implied by a combination
684 * of these constraints, we first combine them before
685 * deriving the implied constraints.
687 static __isl_give isl_set
*add_implied_guards(__isl_take isl_set
*guard
,
688 int degenerate
, __isl_keep isl_basic_set
*bounds
,
689 __isl_keep isl_ast_build
*build
)
691 int depth
, has_stride
;
695 depth
= isl_ast_build_get_depth(build
);
696 has_stride
= isl_ast_build_has_stride(build
, depth
);
697 if (!has_stride
&& !degenerate
)
700 space
= isl_basic_set_get_space(bounds
);
701 dom
= isl_set_universe(space
);
704 bounds
= isl_basic_set_copy(bounds
);
705 bounds
= isl_basic_set_drop_constraints_not_involving_dims(
706 bounds
, isl_dim_set
, depth
, 1);
707 set
= isl_set_from_basic_set(bounds
);
708 dom
= isl_set_intersect(dom
, set
);
712 set
= isl_ast_build_get_stride_constraint(build
);
713 dom
= isl_set_intersect(dom
, set
);
716 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
717 dom
= isl_ast_build_compute_gist(build
, dom
);
718 guard
= isl_set_intersect(guard
, dom
);
723 /* Update "graft" based on "sub_build" for the degenerate case.
725 * "build" is the build in which graft->node was created
726 * "sub_build" contains information about the current level itself,
727 * including the single value attained.
729 * We set the initialization part of the for loop to the single
730 * value attained by the current dimension.
731 * The increment and condition are not strictly needed as the are known
732 * to be "1" and "iterator <= value" respectively.
734 static __isl_give isl_ast_graft
*refine_degenerate(
735 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
,
736 __isl_keep isl_ast_build
*sub_build
)
740 if (!graft
|| !sub_build
)
741 return isl_ast_graft_free(graft
);
743 value
= isl_pw_aff_copy(sub_build
->value
);
745 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
747 if (!graft
->node
->u
.f
.init
)
748 return isl_ast_graft_free(graft
);
753 /* Return the intersection of constraints in "list" as a set.
755 static __isl_give isl_set
*intersect_constraints(
756 __isl_keep isl_constraint_list
*list
)
761 n
= isl_constraint_list_n_constraint(list
);
763 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
764 "expecting at least one constraint", return NULL
);
766 bset
= isl_basic_set_from_constraint(
767 isl_constraint_list_get_constraint(list
, 0));
768 for (i
= 1; i
< n
; ++i
) {
769 isl_basic_set
*bset_i
;
771 bset_i
= isl_basic_set_from_constraint(
772 isl_constraint_list_get_constraint(list
, i
));
773 bset
= isl_basic_set_intersect(bset
, bset_i
);
776 return isl_set_from_basic_set(bset
);
779 /* Compute the constraints on the outer dimensions enforced by
780 * graft->node and add those constraints to graft->enforced,
781 * in case the upper bound is expressed as a set "upper".
783 * In particular, if l(...) is a lower bound in "lower", and
785 * -a i + f(...) >= 0 or a i <= f(...)
787 * is an upper bound ocnstraint on the current dimension i,
788 * then the for loop enforces the constraint
790 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
792 * We therefore simply take each lower bound in turn, plug it into
793 * the upper bounds and compute the intersection over all lower bounds.
795 * If a lower bound is a rational expression, then
796 * isl_basic_set_preimage_multi_aff will force this rational
797 * expression to have only integer values. However, the loop
798 * itself does not enforce this integrality constraint. We therefore
799 * use the ceil of the lower bounds instead of the lower bounds themselves.
800 * Other constraints will make sure that the for loop is only executed
801 * when each of the lower bounds attains an integral value.
802 * In particular, potentially rational values only occur in
803 * lower_bound if the offset is a (seemingly) rational expression,
804 * but then outer conditions will make sure that this rational expression
805 * only attains integer values.
807 static __isl_give isl_ast_graft
*set_enforced_from_set(
808 __isl_take isl_ast_graft
*graft
,
809 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
812 isl_basic_set
*enforced
;
813 isl_pw_multi_aff
*pma
;
816 if (!graft
|| !lower
)
817 return isl_ast_graft_free(graft
);
819 space
= isl_set_get_space(upper
);
820 enforced
= isl_basic_set_universe(isl_space_copy(space
));
822 space
= isl_space_map_from_set(space
);
823 pma
= isl_pw_multi_aff_identity(space
);
825 n
= isl_pw_aff_list_n_pw_aff(lower
);
826 for (i
= 0; i
< n
; ++i
) {
830 isl_pw_multi_aff
*pma_i
;
832 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
833 pa
= isl_pw_aff_ceil(pa
);
834 pma_i
= isl_pw_multi_aff_copy(pma
);
835 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
836 enforced_i
= isl_set_copy(upper
);
837 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
838 hull
= isl_set_simple_hull(enforced_i
);
839 enforced
= isl_basic_set_intersect(enforced
, hull
);
842 isl_pw_multi_aff_free(pma
);
844 graft
= isl_ast_graft_enforce(graft
, enforced
);
849 /* Compute the constraints on the outer dimensions enforced by
850 * graft->node and add those constraints to graft->enforced,
851 * in case the upper bound is expressed as
852 * a list of affine expressions "upper".
854 * The enforced condition is that each lower bound expression is less
855 * than or equal to each upper bound expression.
857 static __isl_give isl_ast_graft
*set_enforced_from_list(
858 __isl_take isl_ast_graft
*graft
,
859 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
862 isl_basic_set
*enforced
;
864 lower
= isl_pw_aff_list_copy(lower
);
865 upper
= isl_pw_aff_list_copy(upper
);
866 cond
= isl_pw_aff_list_le_set(lower
, upper
);
867 enforced
= isl_set_simple_hull(cond
);
868 graft
= isl_ast_graft_enforce(graft
, enforced
);
873 /* Does "aff" have a negative constant term?
875 static int aff_constant_is_negative(__isl_take isl_set
*set
,
876 __isl_take isl_aff
*aff
, void *user
)
881 v
= isl_aff_get_constant_val(aff
);
882 *neg
= isl_val_is_neg(v
);
887 return *neg
? 0 : -1;
890 /* Does "pa" have a negative constant term over its entire domain?
892 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
, void *user
)
897 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
900 return (*neg
&& r
>= 0) ? 0 : -1;
903 /* Does each element in "list" have a negative constant term?
905 * The callback terminates the iteration as soon an element has been
906 * found that does not have a negative constant term.
908 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
912 if (isl_pw_aff_list_foreach(list
,
913 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
919 /* Add 1 to each of the elements in "list", where each of these elements
920 * is defined over the internal schedule space of "build".
922 static __isl_give isl_pw_aff_list
*list_add_one(
923 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
930 space
= isl_ast_build_get_space(build
, 1);
931 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
932 aff
= isl_aff_add_constant_si(aff
, 1);
933 one
= isl_pw_aff_from_aff(aff
);
935 n
= isl_pw_aff_list_n_pw_aff(list
);
936 for (i
= 0; i
< n
; ++i
) {
938 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
939 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
940 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
943 isl_pw_aff_free(one
);
948 /* Set the condition part of the for node graft->node in case
949 * the upper bound is represented as a list of piecewise affine expressions.
951 * In particular, set the condition to
953 * iterator <= min(list of upper bounds)
955 * If each of the upper bounds has a negative constant term, then
956 * set the condition to
958 * iterator < min(list of (upper bound + 1)s)
961 static __isl_give isl_ast_graft
*set_for_cond_from_list(
962 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
963 __isl_keep isl_ast_build
*build
)
966 isl_ast_expr
*bound
, *iterator
, *cond
;
967 enum isl_ast_op_type type
= isl_ast_op_le
;
970 return isl_ast_graft_free(graft
);
972 neg
= list_constant_is_negative(list
);
974 return isl_ast_graft_free(graft
);
975 list
= isl_pw_aff_list_copy(list
);
977 list
= list_add_one(list
, build
);
978 type
= isl_ast_op_lt
;
981 bound
= reduce_list(isl_ast_op_min
, list
, build
);
982 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
983 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
984 graft
->node
->u
.f
.cond
= cond
;
986 isl_pw_aff_list_free(list
);
987 if (!graft
->node
->u
.f
.cond
)
988 return isl_ast_graft_free(graft
);
992 /* Set the condition part of the for node graft->node in case
993 * the upper bound is represented as a set.
995 static __isl_give isl_ast_graft
*set_for_cond_from_set(
996 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
997 __isl_keep isl_ast_build
*build
)
1004 cond
= isl_ast_build_expr_from_set_internal(build
, isl_set_copy(set
));
1005 graft
->node
->u
.f
.cond
= cond
;
1006 if (!graft
->node
->u
.f
.cond
)
1007 return isl_ast_graft_free(graft
);
1011 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1012 * the current dimension.
1014 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
1022 ctx
= isl_ast_build_get_ctx(build
);
1023 depth
= isl_ast_build_get_depth(build
);
1025 if (!isl_ast_build_has_stride(build
, depth
))
1026 return isl_ast_expr_alloc_int_si(ctx
, 1);
1028 v
= isl_ast_build_get_stride(build
, depth
);
1029 return isl_ast_expr_from_val(v
);
1032 /* Should we express the loop condition as
1034 * iterator <= min(list of upper bounds)
1036 * or as a conjunction of constraints?
1038 * The first is constructed from a list of upper bounds.
1039 * The second is constructed from a set.
1041 * If there are no upper bounds in "constraints", then this could mean
1042 * that "domain" simply doesn't have an upper bound or that we didn't
1043 * pick any upper bound. In the first case, we want to generate the
1044 * loop condition as a(n empty) conjunction of constraints
1045 * In the second case, we will compute
1046 * a single upper bound from "domain" and so we use the list form.
1048 * If there are upper bounds in "constraints",
1049 * then we use the list form iff the atomic_upper_bound option is set.
1051 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
1052 __isl_keep isl_set
*domain
, int depth
)
1055 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
1057 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
1060 /* Fill in the expressions of the for node in graft->node.
1063 * - set the initialization part of the loop to the maximum of the lower bounds
1064 * - extract the increment from the stride of the current dimension
1065 * - construct the for condition either based on a list of upper bounds
1066 * or on a set of upper bound constraints.
1068 static __isl_give isl_ast_graft
*set_for_node_expressions(
1069 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
1070 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
1071 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
1078 build
= isl_ast_build_copy(build
);
1081 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
1082 node
->u
.f
.inc
= for_inc(build
);
1085 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
1087 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
1089 isl_ast_build_free(build
);
1091 if (!node
->u
.f
.iterator
|| !node
->u
.f
.init
||
1092 !node
->u
.f
.cond
|| !node
->u
.f
.inc
)
1093 return isl_ast_graft_free(graft
);
1098 /* Update "graft" based on "bounds" and "domain" for the generic,
1099 * non-degenerate, case.
1101 * "c_lower" and "c_upper" contain the lower and upper bounds
1102 * that the loop node should express.
1103 * "domain" is the subset of the intersection of the constraints
1104 * for which some code is executed.
1106 * There may be zero lower bounds or zero upper bounds in "constraints"
1107 * in case the list of constraints was created
1108 * based on the atomic option or based on separation with explicit bounds.
1109 * In that case, we use "domain" to derive lower and/or upper bounds.
1111 * We first compute a list of one or more lower bounds.
1113 * Then we decide if we want to express the condition as
1115 * iterator <= min(list of upper bounds)
1117 * or as a conjunction of constraints.
1119 * The set of enforced constraints is then computed either based on
1120 * a list of upper bounds or on a set of upper bound constraints.
1121 * We do not compute any enforced constraints if we were forced
1122 * to compute a lower or upper bound using exact_bound. The domains
1123 * of the resulting expressions may imply some bounds on outer dimensions
1124 * that we do not want to appear in the enforced constraints since
1125 * they are not actually enforced by the corresponding code.
1127 * Finally, we fill in the expressions of the for node.
1129 static __isl_give isl_ast_graft
*refine_generic_bounds(
1130 __isl_take isl_ast_graft
*graft
,
1131 __isl_take isl_constraint_list
*c_lower
,
1132 __isl_take isl_constraint_list
*c_upper
,
1133 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1137 isl_pw_aff_list
*lower
;
1139 isl_set
*upper_set
= NULL
;
1140 isl_pw_aff_list
*upper_list
= NULL
;
1141 int n_lower
, n_upper
;
1143 if (!graft
|| !c_lower
|| !c_upper
|| !build
)
1146 depth
= isl_ast_build_get_depth(build
);
1147 ctx
= isl_ast_graft_get_ctx(graft
);
1149 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1150 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1152 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1154 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1157 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1158 else if (n_upper
> 0)
1159 upper_set
= intersect_constraints(c_upper
);
1161 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1163 if (n_lower
== 0 || n_upper
== 0)
1166 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1168 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1170 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1173 isl_pw_aff_list_free(lower
);
1174 isl_pw_aff_list_free(upper_list
);
1175 isl_set_free(upper_set
);
1176 isl_constraint_list_free(c_lower
);
1177 isl_constraint_list_free(c_upper
);
1181 isl_constraint_list_free(c_lower
);
1182 isl_constraint_list_free(c_upper
);
1183 return isl_ast_graft_free(graft
);
1186 /* Internal data structure used inside count_constraints to keep
1187 * track of the number of constraints that are independent of dimension "pos",
1188 * the lower bounds in "pos" and the upper bounds in "pos".
1190 struct isl_ast_count_constraints_data
{
1198 /* Increment data->n_indep, data->lower or data->upper depending
1199 * on whether "c" is independenct of dimensions data->pos,
1200 * a lower bound or an upper bound.
1202 static int count_constraints(__isl_take isl_constraint
*c
, void *user
)
1204 struct isl_ast_count_constraints_data
*data
= user
;
1206 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1208 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1213 isl_constraint_free(c
);
1218 /* Update "graft" based on "bounds" and "domain" for the generic,
1219 * non-degenerate, case.
1221 * "list" respresent the list of bounds that need to be encoded by
1222 * the for loop. Only the constraints that involve the iterator
1223 * are relevant here. The other constraints are taken care of by
1224 * the caller and are included in the generated constraints of "build".
1225 * "domain" is the subset of the intersection of the constraints
1226 * for which some code is executed.
1227 * "build" is the build in which graft->node was created.
1229 * We separate lower bounds, upper bounds and constraints that
1230 * are independent of the loop iterator.
1232 * The actual for loop bounds are generated in refine_generic_bounds.
1234 static __isl_give isl_ast_graft
*refine_generic_split(
1235 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1236 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1238 struct isl_ast_count_constraints_data data
;
1239 isl_constraint_list
*lower
;
1240 isl_constraint_list
*upper
;
1243 return isl_ast_graft_free(graft
);
1245 data
.pos
= isl_ast_build_get_depth(build
);
1247 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1249 return isl_ast_graft_free(graft
);
1251 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1252 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1253 isl_constraint_list_free(list
);
1254 return isl_ast_graft_free(graft
);
1257 lower
= isl_constraint_list_drop(list
, 0, data
.n_indep
);
1258 upper
= isl_constraint_list_copy(lower
);
1259 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1260 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1262 return refine_generic_bounds(graft
, lower
, upper
, domain
, build
);
1265 /* Update "graft" based on "bounds" and "domain" for the generic,
1266 * non-degenerate, case.
1268 * "bounds" respresent the bounds that need to be encoded by
1269 * the for loop (or a guard around the for loop).
1270 * "domain" is the subset of "bounds" for which some code is executed.
1271 * "build" is the build in which graft->node was created.
1273 * We break up "bounds" into a list of constraints and continue with
1274 * refine_generic_split.
1276 static __isl_give isl_ast_graft
*refine_generic(
1277 __isl_take isl_ast_graft
*graft
,
1278 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1279 __isl_keep isl_ast_build
*build
)
1281 isl_constraint_list
*list
;
1283 if (!build
|| !graft
)
1284 return isl_ast_graft_free(graft
);
1286 list
= isl_basic_set_get_constraint_list(bounds
);
1288 graft
= refine_generic_split(graft
, list
, domain
, build
);
1293 /* Create a for node for the current level.
1295 * Mark the for node degenerate if "degenerate" is set.
1297 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1307 depth
= isl_ast_build_get_depth(build
);
1308 id
= isl_ast_build_get_iterator_id(build
, depth
);
1309 node
= isl_ast_node_alloc_for(id
);
1311 node
= isl_ast_node_for_mark_degenerate(node
);
1316 /* If the ast_build_exploit_nested_bounds option is set, then return
1317 * the constraints enforced by all elements in "list".
1318 * Otherwise, return the universe.
1320 static __isl_give isl_basic_set
*extract_shared_enforced(
1321 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1329 ctx
= isl_ast_graft_list_get_ctx(list
);
1330 if (isl_options_get_ast_build_exploit_nested_bounds(ctx
))
1331 return isl_ast_graft_list_extract_shared_enforced(list
, build
);
1333 space
= isl_ast_build_get_space(build
, 1);
1334 return isl_basic_set_universe(space
);
1337 /* Return the pending constraints of "build" that are not already taken
1338 * care of (by a combination of "enforced" and the generated constraints
1341 static __isl_give isl_set
*extract_pending(__isl_keep isl_ast_build
*build
,
1342 __isl_keep isl_basic_set
*enforced
)
1344 isl_set
*guard
, *context
;
1346 guard
= isl_ast_build_get_pending(build
);
1347 context
= isl_set_from_basic_set(isl_basic_set_copy(enforced
));
1348 context
= isl_set_intersect(context
,
1349 isl_ast_build_get_generated(build
));
1350 return isl_set_gist(guard
, context
);
1353 /* Create an AST node for the current dimension based on
1354 * the schedule domain "bounds" and return the node encapsulated
1355 * in an isl_ast_graft.
1357 * "executed" is the current inverse schedule, taking into account
1358 * the bounds in "bounds"
1359 * "domain" is the domain of "executed", with inner dimensions projected out.
1360 * It may be a strict subset of "bounds" in case "bounds" was created
1361 * based on the atomic option or based on separation with explicit bounds.
1363 * "domain" may satisfy additional equalities that result
1364 * from intersecting "executed" with "bounds" in add_node.
1365 * It may also satisfy some global constraints that were dropped out because
1366 * we performed separation with explicit bounds.
1367 * The very first step is then to copy these constraints to "bounds".
1369 * Since we may be calling before_each_for and after_each_for
1370 * callbacks, we record the current inverse schedule in the build.
1372 * We consider three builds,
1373 * "build" is the one in which the current level is created,
1374 * "body_build" is the build in which the next level is created,
1375 * "sub_build" is essentially the same as "body_build", except that
1376 * the depth has not been increased yet.
1378 * "build" already contains information (in strides and offsets)
1379 * about the strides at the current level, but this information is not
1380 * reflected in the build->domain.
1381 * We first add this information and the "bounds" to the sub_build->domain.
1382 * isl_ast_build_set_loop_bounds adds the stride information and
1383 * checks whether the current dimension attains
1384 * only a single value and whether this single value can be represented using
1385 * a single affine expression.
1386 * In the first case, the current level is considered "degenerate".
1387 * In the second, sub-case, the current level is considered "eliminated".
1388 * Eliminated levels don't need to be reflected in the AST since we can
1389 * simply plug in the affine expression. For degenerate, but non-eliminated,
1390 * levels, we do introduce a for node, but mark is as degenerate so that
1391 * it can be printed as an assignment of the single value to the loop
1394 * If the current level is eliminated, we explicitly plug in the value
1395 * for the current level found by isl_ast_build_set_loop_bounds in the
1396 * inverse schedule. This ensures that if we are working on a slice
1397 * of the domain based on information available in the inverse schedule
1398 * and the build domain, that then this information is also reflected
1399 * in the inverse schedule. This operation also eliminates the current
1400 * dimension from the inverse schedule making sure no inner dimensions depend
1401 * on the current dimension. Otherwise, we create a for node, marking
1402 * it degenerate if appropriate. The initial for node is still incomplete
1403 * and will be completed in either refine_degenerate or refine_generic.
1405 * We then generate a sequence of grafts for the next level,
1406 * create a surrounding graft for the current level and insert
1407 * the for node we created (if the current level is not eliminated).
1408 * Before creating a graft for the current level, we first extract
1409 * hoistable constraints from the child guards and combine them
1410 * with the pending constraints in the build. These constraints
1411 * are used to simplify the child guards and then added to the guard
1412 * of the current graft to ensure that they will be generated.
1413 * If the hoisted guard is a disjunction, then we use it directly
1414 * to gist the guards on the children before intersect it with the
1415 * pending constraints. We do so because this disjunction is typically
1416 * identical to the guards on the children such that these guards
1417 * can be effectively removed completely. After the intersection,
1418 * the gist operation would have a harder time figuring this out.
1420 * Finally, we set the bounds of the for loop in either
1421 * refine_degenerate or refine_generic.
1422 * We do so in a context where the pending constraints of the build
1423 * have been replaced by the guard of the current graft.
1425 static __isl_give isl_ast_graft
*create_node_scaled(
1426 __isl_take isl_union_map
*executed
,
1427 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1428 __isl_take isl_ast_build
*build
)
1431 int degenerate
, eliminated
;
1432 isl_basic_set
*hull
;
1433 isl_basic_set
*enforced
;
1434 isl_set
*guard
, *hoisted
;
1435 isl_ast_node
*node
= NULL
;
1436 isl_ast_graft
*graft
;
1437 isl_ast_graft_list
*children
;
1438 isl_ast_build
*sub_build
;
1439 isl_ast_build
*body_build
;
1441 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1442 domain
= isl_set_detect_equalities(domain
);
1443 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1444 bounds
= isl_basic_set_intersect(bounds
, hull
);
1445 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1447 depth
= isl_ast_build_get_depth(build
);
1448 sub_build
= isl_ast_build_copy(build
);
1449 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1450 isl_basic_set_copy(bounds
));
1451 degenerate
= isl_ast_build_has_value(sub_build
);
1452 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1453 if (degenerate
< 0 || eliminated
< 0)
1454 executed
= isl_union_map_free(executed
);
1456 executed
= plug_in_values(executed
, sub_build
);
1458 node
= create_for(build
, degenerate
);
1460 body_build
= isl_ast_build_copy(sub_build
);
1461 body_build
= isl_ast_build_increase_depth(body_build
);
1463 node
= before_each_for(node
, body_build
);
1464 children
= generate_next_level(executed
,
1465 isl_ast_build_copy(body_build
));
1467 enforced
= extract_shared_enforced(children
, build
);
1468 guard
= extract_pending(sub_build
, enforced
);
1469 hoisted
= isl_ast_graft_list_extract_hoistable_guard(children
, build
);
1470 if (isl_set_n_basic_set(hoisted
) > 1)
1471 children
= isl_ast_graft_list_gist_guards(children
,
1472 isl_set_copy(hoisted
));
1473 guard
= isl_set_intersect(guard
, hoisted
);
1475 guard
= add_implied_guards(guard
, degenerate
, bounds
, build
);
1477 graft
= isl_ast_graft_alloc_from_children(children
,
1478 isl_set_copy(guard
), enforced
, build
, sub_build
);
1481 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1483 isl_ast_build
*for_build
;
1485 graft
= isl_ast_graft_insert_for(graft
, node
);
1486 for_build
= isl_ast_build_copy(build
);
1487 for_build
= isl_ast_build_replace_pending_by_guard(for_build
,
1488 isl_set_copy(guard
));
1490 graft
= refine_degenerate(graft
, for_build
, sub_build
);
1492 graft
= refine_generic(graft
, bounds
,
1494 isl_ast_build_free(for_build
);
1496 isl_set_free(guard
);
1498 graft
= after_each_for(graft
, body_build
);
1500 isl_ast_build_free(body_build
);
1501 isl_ast_build_free(sub_build
);
1502 isl_ast_build_free(build
);
1503 isl_basic_set_free(bounds
);
1504 isl_set_free(domain
);
1509 /* Internal data structure for checking if all constraints involving
1510 * the input dimension "depth" are such that the other coefficients
1511 * are multiples of "m", reducing "m" if they are not.
1512 * If "m" is reduced all the way down to "1", then the check has failed
1513 * and we break out of the iteration.
1515 struct isl_check_scaled_data
{
1520 /* If constraint "c" involves the input dimension data->depth,
1521 * then make sure that all the other coefficients are multiples of data->m,
1522 * reducing data->m if needed.
1523 * Break out of the iteration if data->m has become equal to "1".
1525 static int constraint_check_scaled(__isl_take isl_constraint
*c
, void *user
)
1527 struct isl_check_scaled_data
*data
= user
;
1529 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1532 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1533 isl_constraint_free(c
);
1537 for (i
= 0; i
< 4; ++i
) {
1538 n
= isl_constraint_dim(c
, t
[i
]);
1539 for (j
= 0; j
< n
; ++j
) {
1542 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1544 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1546 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1547 data
->m
= isl_val_gcd(data
->m
, d
);
1548 if (isl_val_is_one(data
->m
))
1555 isl_constraint_free(c
);
1557 return i
< 4 ? -1 : 0;
1560 /* For each constraint of "bmap" that involves the input dimension data->depth,
1561 * make sure that all the other coefficients are multiples of data->m,
1562 * reducing data->m if needed.
1563 * Break out of the iteration if data->m has become equal to "1".
1565 static int basic_map_check_scaled(__isl_take isl_basic_map
*bmap
, void *user
)
1569 r
= isl_basic_map_foreach_constraint(bmap
,
1570 &constraint_check_scaled
, user
);
1571 isl_basic_map_free(bmap
);
1576 /* For each constraint of "map" that involves the input dimension data->depth,
1577 * make sure that all the other coefficients are multiples of data->m,
1578 * reducing data->m if needed.
1579 * Break out of the iteration if data->m has become equal to "1".
1581 static int map_check_scaled(__isl_take isl_map
*map
, void *user
)
1585 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1591 /* Create an AST node for the current dimension based on
1592 * the schedule domain "bounds" and return the node encapsulated
1593 * in an isl_ast_graft.
1595 * "executed" is the current inverse schedule, taking into account
1596 * the bounds in "bounds"
1597 * "domain" is the domain of "executed", with inner dimensions projected out.
1600 * Before moving on to the actual AST node construction in create_node_scaled,
1601 * we first check if the current dimension is strided and if we can scale
1602 * down this stride. Note that we only do this if the ast_build_scale_strides
1605 * In particular, let the current dimension take on values
1609 * with a an integer. We check if we can find an integer m that (obviously)
1610 * divides both f and s.
1612 * If so, we check if the current dimension only appears in constraints
1613 * where the coefficients of the other variables are multiples of m.
1614 * We perform this extra check to avoid the risk of introducing
1615 * divisions by scaling down the current dimension.
1617 * If so, we scale the current dimension down by a factor of m.
1618 * That is, we plug in
1622 * Note that in principle we could always scale down strided loops
1627 * but this may result in i' taking on larger values than the original i,
1628 * due to the shift by "f".
1629 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1631 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1632 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1633 __isl_take isl_ast_build
*build
)
1635 struct isl_check_scaled_data data
;
1640 ctx
= isl_ast_build_get_ctx(build
);
1641 if (!isl_options_get_ast_build_scale_strides(ctx
))
1642 return create_node_scaled(executed
, bounds
, domain
, build
);
1644 data
.depth
= isl_ast_build_get_depth(build
);
1645 if (!isl_ast_build_has_stride(build
, data
.depth
))
1646 return create_node_scaled(executed
, bounds
, domain
, build
);
1648 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1649 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1651 offset
= isl_aff_free(offset
);
1652 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1653 d
= isl_aff_get_denominator_val(offset
);
1655 executed
= isl_union_map_free(executed
);
1657 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1658 data
.m
= isl_val_div(data
.m
, d
);
1660 data
.m
= isl_val_set_si(data
.m
, 1);
1664 if (!isl_val_is_one(data
.m
)) {
1665 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1667 !isl_val_is_one(data
.m
))
1668 executed
= isl_union_map_free(executed
);
1671 if (!isl_val_is_one(data
.m
)) {
1676 isl_union_map
*umap
;
1678 space
= isl_ast_build_get_space(build
, 1);
1679 space
= isl_space_map_from_set(space
);
1680 ma
= isl_multi_aff_identity(space
);
1681 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1682 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1683 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1685 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1686 isl_multi_aff_copy(ma
));
1687 domain
= isl_set_preimage_multi_aff(domain
,
1688 isl_multi_aff_copy(ma
));
1689 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1690 umap
= isl_union_map_from_map(map
);
1691 executed
= isl_union_map_apply_domain(executed
,
1692 isl_union_map_copy(umap
));
1693 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1696 isl_aff_free(offset
);
1697 isl_val_free(data
.m
);
1699 return create_node_scaled(executed
, bounds
, domain
, build
);
1702 /* Add the basic set to the list that "user" points to.
1704 static int collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1706 isl_basic_set_list
**list
= user
;
1708 *list
= isl_basic_set_list_add(*list
, bset
);
1713 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1715 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1716 __isl_take isl_set
*set
)
1720 isl_basic_set_list
*list
;
1725 ctx
= isl_set_get_ctx(set
);
1727 n
= isl_set_n_basic_set(set
);
1728 list
= isl_basic_set_list_alloc(ctx
, n
);
1729 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1730 list
= isl_basic_set_list_free(list
);
1736 /* Generate code for the schedule domain "bounds"
1737 * and add the result to "list".
1739 * We mainly detect strides here and check if the bounds do not
1740 * conflict with the current build domain
1741 * and then pass over control to create_node.
1743 * "bounds" reflects the bounds on the current dimension and possibly
1744 * some extra conditions on outer dimensions.
1745 * It does not, however, include any divs involving the current dimension,
1746 * so it does not capture any stride constraints.
1747 * We therefore need to compute that part of the schedule domain that
1748 * intersects with "bounds" and derive the strides from the result.
1750 static __isl_give isl_ast_graft_list
*add_node(
1751 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1752 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1754 isl_ast_graft
*graft
;
1755 isl_set
*domain
= NULL
;
1756 isl_union_set
*uset
;
1757 int empty
, disjoint
;
1759 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1760 executed
= isl_union_map_intersect_domain(executed
, uset
);
1761 empty
= isl_union_map_is_empty(executed
);
1767 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1768 domain
= isl_set_from_union_set(uset
);
1769 domain
= isl_ast_build_specialize(build
, domain
);
1771 domain
= isl_set_compute_divs(domain
);
1772 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1773 disjoint
= isl_set_is_disjoint(domain
, build
->domain
);
1779 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1781 graft
= create_node(executed
, bounds
, domain
,
1782 isl_ast_build_copy(build
));
1783 list
= isl_ast_graft_list_add(list
, graft
);
1784 isl_ast_build_free(build
);
1787 list
= isl_ast_graft_list_free(list
);
1789 isl_set_free(domain
);
1790 isl_basic_set_free(bounds
);
1791 isl_union_map_free(executed
);
1792 isl_ast_build_free(build
);
1796 /* Does any element of i follow or coincide with any element of j
1797 * at the current depth for equal values of the outer dimensions?
1799 static int domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1800 __isl_keep isl_basic_set
*j
, void *user
)
1802 int depth
= *(int *) user
;
1803 isl_basic_map
*test
;
1807 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1808 isl_basic_set_copy(j
));
1809 for (l
= 0; l
< depth
; ++l
)
1810 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1812 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1813 isl_dim_out
, depth
);
1814 empty
= isl_basic_map_is_empty(test
);
1815 isl_basic_map_free(test
);
1817 return empty
< 0 ? -1 : !empty
;
1820 /* Split up each element of "list" into a part that is related to "bset"
1821 * according to "gt" and a part that is not.
1822 * Return a list that consist of "bset" and all the pieces.
1824 static __isl_give isl_basic_set_list
*add_split_on(
1825 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1826 __isl_keep isl_basic_map
*gt
)
1829 isl_basic_set_list
*res
;
1832 bset
= isl_basic_set_free(bset
);
1834 gt
= isl_basic_map_copy(gt
);
1835 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1836 n
= isl_basic_set_list_n_basic_set(list
);
1837 res
= isl_basic_set_list_from_basic_set(bset
);
1838 for (i
= 0; res
&& i
< n
; ++i
) {
1839 isl_basic_set
*bset
;
1840 isl_set
*set1
, *set2
;
1841 isl_basic_map
*bmap
;
1844 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1845 bmap
= isl_basic_map_copy(gt
);
1846 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1847 bset
= isl_basic_map_range(bmap
);
1848 empty
= isl_basic_set_is_empty(bset
);
1850 res
= isl_basic_set_list_free(res
);
1852 isl_basic_set_free(bset
);
1853 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1854 res
= isl_basic_set_list_add(res
, bset
);
1858 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1859 set1
= isl_set_from_basic_set(bset
);
1860 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1861 set2
= isl_set_from_basic_set(bset
);
1862 set1
= isl_set_subtract(set2
, set1
);
1863 set1
= isl_set_make_disjoint(set1
);
1865 res
= isl_basic_set_list_concat(res
,
1866 isl_basic_set_list_from_set(set1
));
1868 isl_basic_map_free(gt
);
1869 isl_basic_set_list_free(list
);
1873 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1874 __isl_keep isl_basic_set_list
*domain_list
,
1875 __isl_keep isl_union_map
*executed
,
1876 __isl_keep isl_ast_build
*build
);
1878 /* Internal data structure for add_nodes.
1880 * "executed" and "build" are extra arguments to be passed to add_node.
1881 * "list" collects the results.
1883 struct isl_add_nodes_data
{
1884 isl_union_map
*executed
;
1885 isl_ast_build
*build
;
1887 isl_ast_graft_list
*list
;
1890 /* Generate code for the schedule domains in "scc"
1891 * and add the results to "list".
1893 * The domains in "scc" form a strongly connected component in the ordering.
1894 * If the number of domains in "scc" is larger than 1, then this means
1895 * that we cannot determine a valid ordering for the domains in the component.
1896 * This should be fairly rare because the individual domains
1897 * have been made disjoint first.
1898 * The problem is that the domains may be integrally disjoint but not
1899 * rationally disjoint. For example, we may have domains
1901 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1903 * These two domains have an empty intersection, but their rational
1904 * relaxations do intersect. It is impossible to order these domains
1905 * in the second dimension because the first should be ordered before
1906 * the second for outer dimension equal to 0, while it should be ordered
1907 * after for outer dimension equal to 1.
1909 * This may happen in particular in case of unrolling since the domain
1910 * of each slice is replaced by its simple hull.
1912 * For each basic set i in "scc" and for each of the following basic sets j,
1913 * we split off that part of the basic set i that shares the outer dimensions
1914 * with j and lies before j in the current dimension.
1915 * We collect all the pieces in a new list that replaces "scc".
1917 * While the elements in "scc" should be disjoint, we double-check
1918 * this property to avoid running into an infinite recursion in case
1919 * they intersect due to some internal error.
1921 static int add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1923 struct isl_add_nodes_data
*data
= user
;
1925 isl_basic_set
*bset
, *first
;
1926 isl_basic_set_list
*list
;
1930 n
= isl_basic_set_list_n_basic_set(scc
);
1931 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
1933 isl_basic_set_list_free(scc
);
1934 data
->list
= add_node(data
->list
,
1935 isl_union_map_copy(data
->executed
), bset
,
1936 isl_ast_build_copy(data
->build
));
1937 return data
->list
? 0 : -1;
1940 depth
= isl_ast_build_get_depth(data
->build
);
1941 space
= isl_basic_set_get_space(bset
);
1942 space
= isl_space_map_from_set(space
);
1943 gt
= isl_basic_map_universe(space
);
1944 for (i
= 0; i
< depth
; ++i
)
1945 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
1946 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
1948 first
= isl_basic_set_copy(bset
);
1949 list
= isl_basic_set_list_from_basic_set(bset
);
1950 for (i
= 1; i
< n
; ++i
) {
1953 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
1955 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
1957 list
= isl_basic_set_list_free(list
);
1959 isl_die(isl_basic_set_list_get_ctx(scc
),
1961 "basic sets in scc are assumed to be disjoint",
1962 list
= isl_basic_set_list_free(list
));
1964 list
= add_split_on(list
, bset
, gt
);
1966 isl_basic_set_free(first
);
1967 isl_basic_map_free(gt
);
1968 isl_basic_set_list_free(scc
);
1970 data
->list
= isl_ast_graft_list_concat(data
->list
,
1971 generate_sorted_domains(scc
, data
->executed
, data
->build
));
1972 isl_basic_set_list_free(scc
);
1974 return data
->list
? 0 : -1;
1977 /* Sort the domains in "domain_list" according to the execution order
1978 * at the current depth (for equal values of the outer dimensions),
1979 * generate code for each of them, collecting the results in a list.
1980 * If no code is generated (because the intersection of the inverse schedule
1981 * with the domains turns out to be empty), then an empty list is returned.
1983 * The caller is responsible for ensuring that the basic sets in "domain_list"
1984 * are pair-wise disjoint. It can, however, in principle happen that
1985 * two basic sets should be ordered one way for one value of the outer
1986 * dimensions and the other way for some other value of the outer dimensions.
1987 * We therefore play safe and look for strongly connected components.
1988 * The function add_nodes takes care of handling non-trivial components.
1990 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1991 __isl_keep isl_basic_set_list
*domain_list
,
1992 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
1995 struct isl_add_nodes_data data
;
2002 ctx
= isl_basic_set_list_get_ctx(domain_list
);
2003 n
= isl_basic_set_list_n_basic_set(domain_list
);
2004 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
2008 return add_node(data
.list
, isl_union_map_copy(executed
),
2009 isl_basic_set_list_get_basic_set(domain_list
, 0),
2010 isl_ast_build_copy(build
));
2012 depth
= isl_ast_build_get_depth(build
);
2013 data
.executed
= executed
;
2015 if (isl_basic_set_list_foreach_scc(domain_list
,
2016 &domain_follows_at_depth
, &depth
,
2017 &add_nodes
, &data
) < 0)
2018 data
.list
= isl_ast_graft_list_free(data
.list
);
2023 /* Do i and j share any values for the outer dimensions?
2025 static int shared_outer(__isl_keep isl_basic_set
*i
,
2026 __isl_keep isl_basic_set
*j
, void *user
)
2028 int depth
= *(int *) user
;
2029 isl_basic_map
*test
;
2033 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
2034 isl_basic_set_copy(j
));
2035 for (l
= 0; l
< depth
; ++l
)
2036 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
2038 empty
= isl_basic_map_is_empty(test
);
2039 isl_basic_map_free(test
);
2041 return empty
< 0 ? -1 : !empty
;
2044 /* Internal data structure for generate_sorted_domains_wrap.
2046 * "n" is the total number of basic sets
2047 * "executed" and "build" are extra arguments to be passed
2048 * to generate_sorted_domains.
2050 * "single" is set to 1 by generate_sorted_domains_wrap if there
2051 * is only a single component.
2052 * "list" collects the results.
2054 struct isl_ast_generate_parallel_domains_data
{
2056 isl_union_map
*executed
;
2057 isl_ast_build
*build
;
2060 isl_ast_graft_list
*list
;
2063 /* Call generate_sorted_domains on "scc", fuse the result into a list
2064 * with either zero or one graft and collect the these single element
2065 * lists into data->list.
2067 * If there is only one component, i.e., if the number of basic sets
2068 * in the current component is equal to the total number of basic sets,
2069 * then data->single is set to 1 and the result of generate_sorted_domains
2072 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
2075 struct isl_ast_generate_parallel_domains_data
*data
= user
;
2076 isl_ast_graft_list
*list
;
2078 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
2079 data
->single
= isl_basic_set_list_n_basic_set(scc
) == data
->n
;
2081 list
= isl_ast_graft_list_fuse(list
, data
->build
);
2085 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
2087 isl_basic_set_list_free(scc
);
2094 /* Look for any (weakly connected) components in the "domain_list"
2095 * of domains that share some values of the outer dimensions.
2096 * That is, domains in different components do not share any values
2097 * of the outer dimensions. This means that these components
2098 * can be freely reordered.
2099 * Within each of the components, we sort the domains according
2100 * to the execution order at the current depth.
2102 * If there is more than one component, then generate_sorted_domains_wrap
2103 * fuses the result of each call to generate_sorted_domains
2104 * into a list with either zero or one graft and collects these (at most)
2105 * single element lists into a bigger list. This means that the elements of the
2106 * final list can be freely reordered. In particular, we sort them
2107 * according to an arbitrary but fixed ordering to ease merging of
2108 * graft lists from different components.
2110 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2111 __isl_keep isl_basic_set_list
*domain_list
,
2112 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2115 struct isl_ast_generate_parallel_domains_data data
;
2120 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2122 return generate_sorted_domains(domain_list
, executed
, build
);
2124 depth
= isl_ast_build_get_depth(build
);
2126 data
.executed
= executed
;
2129 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2130 &generate_sorted_domains_wrap
,
2132 data
.list
= isl_ast_graft_list_free(data
.list
);
2135 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2140 /* Internal data for separate_domain.
2142 * "explicit" is set if we only want to use explicit bounds.
2144 * "domain" collects the separated domains.
2146 struct isl_separate_domain_data
{
2147 isl_ast_build
*build
;
2152 /* Extract implicit bounds on the current dimension for the executed "map".
2154 * The domain of "map" may involve inner dimensions, so we
2155 * need to eliminate them.
2157 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2158 __isl_keep isl_ast_build
*build
)
2162 domain
= isl_map_domain(map
);
2163 domain
= isl_ast_build_eliminate(build
, domain
);
2168 /* Extract explicit bounds on the current dimension for the executed "map".
2170 * Rather than eliminating the inner dimensions as in implicit_bounds,
2171 * we simply drop any constraints involving those inner dimensions.
2172 * The idea is that most bounds that are implied by constraints on the
2173 * inner dimensions will be enforced by for loops and not by explicit guards.
2174 * There is then no need to separate along those bounds.
2176 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2177 __isl_keep isl_ast_build
*build
)
2182 dim
= isl_map_dim(map
, isl_dim_out
);
2183 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2185 domain
= isl_map_domain(map
);
2186 depth
= isl_ast_build_get_depth(build
);
2187 dim
= isl_set_dim(domain
, isl_dim_set
);
2188 domain
= isl_set_detect_equalities(domain
);
2189 domain
= isl_set_drop_constraints_involving_dims(domain
,
2190 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2191 domain
= isl_set_remove_divs_involving_dims(domain
,
2192 isl_dim_set
, depth
, 1);
2193 domain
= isl_set_remove_unknown_divs(domain
);
2198 /* Split data->domain into pieces that intersect with the range of "map"
2199 * and pieces that do not intersect with the range of "map"
2200 * and then add that part of the range of "map" that does not intersect
2201 * with data->domain.
2203 static int separate_domain(__isl_take isl_map
*map
, void *user
)
2205 struct isl_separate_domain_data
*data
= user
;
2210 domain
= explicit_bounds(map
, data
->build
);
2212 domain
= implicit_bounds(map
, data
->build
);
2214 domain
= isl_set_coalesce(domain
);
2215 domain
= isl_set_make_disjoint(domain
);
2216 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2217 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2218 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2219 data
->domain
= isl_set_union(data
->domain
, d1
);
2220 data
->domain
= isl_set_union(data
->domain
, d2
);
2225 /* Separate the schedule domains of "executed".
2227 * That is, break up the domain of "executed" into basic sets,
2228 * such that for each basic set S, every element in S is associated with
2229 * the same domain spaces.
2231 * "space" is the (single) domain space of "executed".
2233 static __isl_give isl_set
*separate_schedule_domains(
2234 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2235 __isl_keep isl_ast_build
*build
)
2237 struct isl_separate_domain_data data
= { build
};
2240 ctx
= isl_ast_build_get_ctx(build
);
2241 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2242 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2243 data
.domain
= isl_set_empty(space
);
2244 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2245 data
.domain
= isl_set_free(data
.domain
);
2247 isl_union_map_free(executed
);
2251 /* Temporary data used during the search for a lower bound for unrolling.
2253 * "build" is the build in which the unrolling will be performed
2254 * "domain" is the original set for which to find a lower bound
2255 * "depth" is the dimension for which to find a lower boudn
2256 * "expansion" is the expansion that needs to be applied to "domain"
2257 * in the unrolling that will be performed
2259 * "lower" is the best lower bound found so far. It is NULL if we have not
2261 * "n" is the corresponding size. If lower is NULL, then the value of n
2263 * "n_div" is the maximal number of integer divisions in the first
2264 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2265 * been computed yet.
2267 struct isl_find_unroll_data
{
2268 isl_ast_build
*build
;
2271 isl_basic_map
*expansion
;
2278 /* Return the constraint
2280 * i_"depth" = aff + offset
2282 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2285 aff
= isl_aff_copy(aff
);
2286 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2287 aff
= isl_aff_add_constant_si(aff
, offset
);
2288 return isl_equality_from_aff(aff
);
2291 /* Update *user to the number of integer divsions in the first element
2292 * of "ma", if it is larger than the current value.
2294 static int update_n_div(__isl_take isl_set
*set
, __isl_take isl_multi_aff
*ma
,
2301 aff
= isl_multi_aff_get_aff(ma
, 0);
2302 n_div
= isl_aff_dim(aff
, isl_dim_div
);
2304 isl_multi_aff_free(ma
);
2310 return aff
? 0 : -1;
2313 /* Get the number of integer divisions in the expression for the iterator
2314 * value at the first slice in the unrolling based on lower bound "lower",
2315 * taking into account the expansion that needs to be performed on this slice.
2317 static int get_expanded_n_div(struct isl_find_unroll_data
*data
,
2318 __isl_keep isl_aff
*lower
)
2322 isl_map
*it_map
, *expansion
;
2323 isl_pw_multi_aff
*pma
;
2326 c
= at_offset(data
->depth
, lower
, 0);
2327 set
= isl_set_copy(data
->domain
);
2328 set
= isl_set_add_constraint(set
, c
);
2329 expansion
= isl_map_from_basic_map(isl_basic_map_copy(data
->expansion
));
2330 set
= isl_set_apply(set
, expansion
);
2331 it_map
= isl_ast_build_map_to_iterator(data
->build
, set
);
2332 pma
= isl_pw_multi_aff_from_map(it_map
);
2334 if (isl_pw_multi_aff_foreach_piece(pma
, &update_n_div
, &n
) < 0)
2336 isl_pw_multi_aff_free(pma
);
2341 /* Is the lower bound "lower" with corresponding iteration count "n"
2342 * better than the one stored in "data"?
2343 * If there is no upper bound on the iteration count ("n" is infinity) or
2344 * if the count is too large, then we cannot use this lower bound.
2345 * Otherwise, if there was no previous lower bound or
2346 * if the iteration count of the new lower bound is smaller than
2347 * the iteration count of the previous lower bound, then we consider
2348 * the new lower bound to be better.
2349 * If the iteration count is the same, then compare the number
2350 * of integer divisions that would be needed to express
2351 * the iterator value at the first slice in the unrolling
2352 * according to the lower bound. If we end up computing this
2353 * number, then store the lowest value in data->n_div.
2355 static int is_better_lower_bound(struct isl_find_unroll_data
*data
,
2356 __isl_keep isl_aff
*lower
, __isl_keep isl_val
*n
)
2363 if (isl_val_is_infty(n
))
2365 if (isl_val_cmp_si(n
, INT_MAX
) > 0)
2369 cmp
= isl_val_cmp_si(n
, *data
->n
);
2374 if (data
->n_div
< 0)
2375 data
->n_div
= get_expanded_n_div(data
, data
->lower
);
2376 if (data
->n_div
< 0)
2378 if (data
->n_div
== 0)
2380 n_div
= get_expanded_n_div(data
, lower
);
2383 if (n_div
>= data
->n_div
)
2385 data
->n_div
= n_div
;
2390 /* Check if we can use "c" as a lower bound and if it is better than
2391 * any previously found lower bound.
2393 * If "c" does not involve the dimension at the current depth,
2394 * then we cannot use it.
2395 * Otherwise, let "c" be of the form
2399 * We compute the maximal value of
2401 * -ceil(f(j)/a)) + i + 1
2403 * over the domain. If there is such a value "n", then we know
2405 * -ceil(f(j)/a)) + i + 1 <= n
2409 * i < ceil(f(j)/a)) + n
2411 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2412 * We just need to check if we have found any lower bound before and
2413 * if the new lower bound is better (smaller n or fewer integer divisions)
2414 * than the previously found lower bounds.
2416 static int update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2417 __isl_keep isl_constraint
*c
)
2419 isl_aff
*aff
, *lower
;
2423 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2426 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2427 lower
= isl_aff_ceil(lower
);
2428 aff
= isl_aff_copy(lower
);
2429 aff
= isl_aff_neg(aff
);
2430 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2431 aff
= isl_aff_add_constant_si(aff
, 1);
2432 max
= isl_set_max_val(data
->domain
, aff
);
2435 better
= is_better_lower_bound(data
, lower
, max
);
2436 if (better
< 0 || !better
) {
2438 isl_aff_free(lower
);
2439 return better
< 0 ? -1 : 0;
2442 isl_aff_free(data
->lower
);
2443 data
->lower
= lower
;
2444 *data
->n
= isl_val_get_num_si(max
);
2450 /* Check if we can use "c" as a lower bound and if it is better than
2451 * any previously found lower bound.
2453 static int constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2455 struct isl_find_unroll_data
*data
;
2458 data
= (struct isl_find_unroll_data
*) user
;
2459 r
= update_unrolling_lower_bound(data
, c
);
2460 isl_constraint_free(c
);
2465 /* Look for a lower bound l(i) on the dimension at "depth"
2466 * and a size n such that "domain" is a subset of
2468 * { [i] : l(i) <= i_d < l(i) + n }
2470 * where d is "depth" and l(i) depends only on earlier dimensions.
2471 * Furthermore, try and find a lower bound such that n is as small as possible.
2472 * In particular, "n" needs to be finite.
2473 * "build" is the build in which the unrolling will be performed.
2474 * "expansion" is the expansion that needs to be applied to "domain"
2475 * in the unrolling that will be performed.
2477 * Inner dimensions have been eliminated from "domain" by the caller.
2479 * We first construct a collection of lower bounds on the input set
2480 * by computing its simple hull. We then iterate through them,
2481 * discarding those that we cannot use (either because they do not
2482 * involve the dimension at "depth" or because they have no corresponding
2483 * upper bound, meaning that "n" would be unbounded) and pick out the
2484 * best from the remaining ones.
2486 * If we cannot find a suitable lower bound, then we consider that
2489 static __isl_give isl_aff
*find_unroll_lower_bound(
2490 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*domain
,
2491 int depth
, __isl_keep isl_basic_map
*expansion
, int *n
)
2493 struct isl_find_unroll_data data
=
2494 { build
, domain
, depth
, expansion
, NULL
, n
, -1 };
2495 isl_basic_set
*hull
;
2497 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2499 if (isl_basic_set_foreach_constraint(hull
,
2500 &constraint_find_unroll
, &data
) < 0)
2503 isl_basic_set_free(hull
);
2506 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2507 "cannot find lower bound for unrolling", return NULL
);
2511 isl_basic_set_free(hull
);
2512 return isl_aff_free(data
.lower
);
2515 /* Call "fn" on each iteration of the current dimension of "domain".
2516 * If "init" is not NULL, then it is called with the number of
2517 * iterations before any call to "fn".
2518 * Return -1 on failure.
2520 * Since we are going to be iterating over the individual values,
2521 * we first check if there are any strides on the current dimension.
2522 * If there is, we rewrite the current dimension i as
2524 * i = stride i' + offset
2526 * and then iterate over individual values of i' instead.
2528 * We then look for a lower bound on i' and a size such that the domain
2531 * { [j,i'] : l(j) <= i' < l(j) + n }
2533 * and then take slices of the domain at values of i'
2534 * between l(j) and l(j) + n - 1.
2536 * We compute the unshifted simple hull of each slice to ensure that
2537 * we have a single basic set per offset. The slicing constraint
2538 * may get simplified away before the unshifted simple hull is taken
2539 * and may therefore in some rare cases disappear from the result.
2540 * We therefore explicitly add the constraint back after computing
2541 * the unshifted simple hull to ensure that the basic sets
2542 * remain disjoint. The constraints that are dropped by taking the hull
2543 * will be taken into account at the next level, as in the case of the
2546 * Finally, we map i' back to i and call "fn".
2548 static int foreach_iteration(__isl_take isl_set
*domain
,
2549 __isl_keep isl_ast_build
*build
, int (*init
)(int n
, void *user
),
2550 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
2554 isl_multi_aff
*expansion
;
2555 isl_basic_map
*bmap
;
2557 isl_ast_build
*stride_build
;
2559 depth
= isl_ast_build_get_depth(build
);
2561 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2562 domain
= isl_set_intersect(domain
, isl_ast_build_get_domain(build
));
2563 stride_build
= isl_ast_build_copy(build
);
2564 stride_build
= isl_ast_build_detect_strides(stride_build
,
2565 isl_set_copy(domain
));
2566 expansion
= isl_ast_build_get_stride_expansion(stride_build
);
2568 domain
= isl_set_preimage_multi_aff(domain
,
2569 isl_multi_aff_copy(expansion
));
2570 domain
= isl_ast_build_eliminate_divs(stride_build
, domain
);
2571 isl_ast_build_free(stride_build
);
2573 bmap
= isl_basic_map_from_multi_aff(expansion
);
2575 lower
= find_unroll_lower_bound(build
, domain
, depth
, bmap
, &n
);
2577 domain
= isl_set_free(domain
);
2579 if (init
&& init(n
, user
) < 0)
2580 domain
= isl_set_free(domain
);
2581 for (i
= 0; i
< n
; ++i
) {
2583 isl_basic_set
*bset
;
2584 isl_constraint
*slice
;
2586 slice
= at_offset(depth
, lower
, i
);
2587 set
= isl_set_copy(domain
);
2588 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2589 bset
= isl_set_unshifted_simple_hull(set
);
2590 bset
= isl_basic_set_add_constraint(bset
, slice
);
2591 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2593 if (fn(bset
, user
) < 0)
2597 isl_aff_free(lower
);
2598 isl_set_free(domain
);
2599 isl_basic_map_free(bmap
);
2601 return i
< n
? -1 : 0;
2604 /* Data structure for storing the results and the intermediate objects
2605 * of compute_domains.
2607 * "list" is the main result of the function and contains a list
2608 * of disjoint basic sets for which code should be generated.
2610 * "executed" and "build" are inputs to compute_domains.
2611 * "schedule_domain" is the domain of "executed".
2613 * "option" constains the domains at the current depth that should by
2614 * atomic, separated or unrolled. These domains are as specified by
2615 * the user, except that inner dimensions have been eliminated and
2616 * that they have been made pair-wise disjoint.
2618 * "sep_class" contains the user-specified split into separation classes
2619 * specialized to the current depth.
2620 * "done" contains the union of the separation domains that have already
2623 struct isl_codegen_domains
{
2624 isl_basic_set_list
*list
;
2626 isl_union_map
*executed
;
2627 isl_ast_build
*build
;
2628 isl_set
*schedule_domain
;
2636 /* Internal data structure for do_unroll.
2638 * "domains" stores the results of compute_domains.
2639 * "class_domain" is the original class domain passed to do_unroll.
2640 * "unroll_domain" collects the unrolled iterations.
2642 struct isl_ast_unroll_data
{
2643 struct isl_codegen_domains
*domains
;
2644 isl_set
*class_domain
;
2645 isl_set
*unroll_domain
;
2648 /* Given an iteration of an unrolled domain represented by "bset",
2649 * add it to data->domains->list.
2650 * Since we may have dropped some constraints, we intersect with
2651 * the class domain again to ensure that each element in the list
2652 * is disjoint from the other class domains.
2654 static int do_unroll_iteration(__isl_take isl_basic_set
*bset
, void *user
)
2656 struct isl_ast_unroll_data
*data
= user
;
2658 isl_basic_set_list
*list
;
2660 set
= isl_set_from_basic_set(bset
);
2661 data
->unroll_domain
= isl_set_union(data
->unroll_domain
,
2663 set
= isl_set_intersect(set
, isl_set_copy(data
->class_domain
));
2664 set
= isl_set_make_disjoint(set
);
2665 list
= isl_basic_set_list_from_set(set
);
2666 data
->domains
->list
= isl_basic_set_list_concat(data
->domains
->list
,
2672 /* Extend domains->list with a list of basic sets, one for each value
2673 * of the current dimension in "domain" and remove the corresponding
2674 * sets from the class domain. Return the updated class domain.
2675 * The divs that involve the current dimension have not been projected out
2678 * We call foreach_iteration to iterate over the individual values and
2679 * in do_unroll_iteration we collect the individual basic sets in
2680 * domains->list and their union in data->unroll_domain, which is then
2681 * used to update the class domain.
2683 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2684 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2686 struct isl_ast_unroll_data data
;
2689 return isl_set_free(class_domain
);
2691 return isl_set_free(domain
);
2693 data
.domains
= domains
;
2694 data
.class_domain
= class_domain
;
2695 data
.unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2697 if (foreach_iteration(domain
, domains
->build
, NULL
,
2698 &do_unroll_iteration
, &data
) < 0)
2699 data
.unroll_domain
= isl_set_free(data
.unroll_domain
);
2701 class_domain
= isl_set_subtract(class_domain
, data
.unroll_domain
);
2703 return class_domain
;
2706 /* Add domains to domains->list for each individual value of the current
2707 * dimension, for that part of the schedule domain that lies in the
2708 * intersection of the option domain and the class domain.
2709 * Remove the corresponding sets from the class domain and
2710 * return the updated class domain.
2712 * We first break up the unroll option domain into individual pieces
2713 * and then handle each of them separately. The unroll option domain
2714 * has been made disjoint in compute_domains_init_options,
2716 * Note that we actively want to combine different pieces of the
2717 * schedule domain that have the same value at the current dimension.
2718 * We therefore need to break up the unroll option domain before
2719 * intersecting with class and schedule domain, hoping that the
2720 * unroll option domain specified by the user is relatively simple.
2722 static __isl_give isl_set
*compute_unroll_domains(
2723 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2725 isl_set
*unroll_domain
;
2726 isl_basic_set_list
*unroll_list
;
2730 empty
= isl_set_is_empty(domains
->option
[isl_ast_loop_unroll
]);
2732 return isl_set_free(class_domain
);
2734 return class_domain
;
2736 unroll_domain
= isl_set_copy(domains
->option
[isl_ast_loop_unroll
]);
2737 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2739 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2740 for (i
= 0; i
< n
; ++i
) {
2741 isl_basic_set
*bset
;
2743 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2744 unroll_domain
= isl_set_from_basic_set(bset
);
2745 unroll_domain
= isl_set_intersect(unroll_domain
,
2746 isl_set_copy(class_domain
));
2747 unroll_domain
= isl_set_intersect(unroll_domain
,
2748 isl_set_copy(domains
->schedule_domain
));
2750 empty
= isl_set_is_empty(unroll_domain
);
2751 if (empty
>= 0 && empty
) {
2752 isl_set_free(unroll_domain
);
2756 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2759 isl_basic_set_list_free(unroll_list
);
2761 return class_domain
;
2764 /* Try and construct a single basic set that includes the intersection of
2765 * the schedule domain, the atomic option domain and the class domain.
2766 * Add the resulting basic set(s) to domains->list and remove them
2767 * from class_domain. Return the updated class domain.
2769 * We construct a single domain rather than trying to combine
2770 * the schedule domains of individual domains because we are working
2771 * within a single component so that non-overlapping schedule domains
2772 * should already have been separated.
2773 * We do however need to make sure that this single domains is a subset
2774 * of the class domain so that it would not intersect with any other
2775 * class domains. This means that we may end up splitting up the atomic
2776 * domain in case separation classes are being used.
2778 * "domain" is the intersection of the schedule domain and the class domain,
2779 * with inner dimensions projected out.
2781 static __isl_give isl_set
*compute_atomic_domain(
2782 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2784 isl_basic_set
*bset
;
2785 isl_basic_set_list
*list
;
2786 isl_set
*domain
, *atomic_domain
;
2789 domain
= isl_set_copy(domains
->option
[isl_ast_loop_atomic
]);
2790 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2791 domain
= isl_set_intersect(domain
,
2792 isl_set_copy(domains
->schedule_domain
));
2793 empty
= isl_set_is_empty(domain
);
2795 class_domain
= isl_set_free(class_domain
);
2797 isl_set_free(domain
);
2798 return class_domain
;
2801 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2802 domain
= isl_set_coalesce(domain
);
2803 bset
= isl_set_unshifted_simple_hull(domain
);
2804 domain
= isl_set_from_basic_set(bset
);
2805 atomic_domain
= isl_set_copy(domain
);
2806 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2807 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2808 domain
= isl_set_make_disjoint(domain
);
2809 list
= isl_basic_set_list_from_set(domain
);
2810 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2812 return class_domain
;
2815 /* Split up the schedule domain into uniform basic sets,
2816 * in the sense that each element in a basic set is associated to
2817 * elements of the same domains, and add the result to domains->list.
2818 * Do this for that part of the schedule domain that lies in the
2819 * intersection of "class_domain" and the separate option domain.
2821 * "class_domain" may or may not include the constraints
2822 * of the schedule domain, but this does not make a difference
2823 * since we are going to intersect it with the domain of the inverse schedule.
2824 * If it includes schedule domain constraints, then they may involve
2825 * inner dimensions, but we will eliminate them in separation_domain.
2827 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2828 __isl_keep isl_set
*class_domain
)
2832 isl_union_map
*executed
;
2833 isl_basic_set_list
*list
;
2836 domain
= isl_set_copy(domains
->option
[isl_ast_loop_separate
]);
2837 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2838 executed
= isl_union_map_copy(domains
->executed
);
2839 executed
= isl_union_map_intersect_domain(executed
,
2840 isl_union_set_from_set(domain
));
2841 empty
= isl_union_map_is_empty(executed
);
2842 if (empty
< 0 || empty
) {
2843 isl_union_map_free(executed
);
2844 return empty
< 0 ? -1 : 0;
2847 space
= isl_set_get_space(class_domain
);
2848 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2850 list
= isl_basic_set_list_from_set(domain
);
2851 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2856 /* Split up the domain at the current depth into disjoint
2857 * basic sets for which code should be generated separately
2858 * for the given separation class domain.
2860 * If any separation classes have been defined, then "class_domain"
2861 * is the domain of the current class and does not refer to inner dimensions.
2862 * Otherwise, "class_domain" is the universe domain.
2864 * We first make sure that the class domain is disjoint from
2865 * previously considered class domains.
2867 * The separate domains can be computed directly from the "class_domain".
2869 * The unroll, atomic and remainder domains need the constraints
2870 * from the schedule domain.
2872 * For unrolling, the actual schedule domain is needed (with divs that
2873 * may refer to the current dimension) so that stride detection can be
2876 * For atomic and remainder domains, inner dimensions and divs involving
2877 * the current dimensions should be eliminated.
2878 * In case we are working within a separation class, we need to intersect
2879 * the result with the current "class_domain" to ensure that the domains
2880 * are disjoint from those generated from other class domains.
2882 * The domain that has been made atomic may be larger than specified
2883 * by the user since it needs to be representable as a single basic set.
2884 * This possibly larger domain is removed from class_domain by
2885 * compute_atomic_domain. It is computed first so that the extended domain
2886 * would not overlap with any domains computed before.
2887 * Similary, the unrolled domains may have some constraints removed and
2888 * may therefore also be larger than specified by the user.
2890 * If anything is left after handling separate, unroll and atomic,
2891 * we split it up into basic sets and append the basic sets to domains->list.
2893 static int compute_partial_domains(struct isl_codegen_domains
*domains
,
2894 __isl_take isl_set
*class_domain
)
2896 isl_basic_set_list
*list
;
2899 class_domain
= isl_set_subtract(class_domain
,
2900 isl_set_copy(domains
->done
));
2901 domains
->done
= isl_set_union(domains
->done
,
2902 isl_set_copy(class_domain
));
2904 class_domain
= compute_atomic_domain(domains
, class_domain
);
2905 class_domain
= compute_unroll_domains(domains
, class_domain
);
2907 domain
= isl_set_copy(class_domain
);
2909 if (compute_separate_domain(domains
, domain
) < 0)
2911 domain
= isl_set_subtract(domain
,
2912 isl_set_copy(domains
->option
[isl_ast_loop_separate
]));
2914 domain
= isl_set_intersect(domain
,
2915 isl_set_copy(domains
->schedule_domain
));
2917 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2918 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2920 domain
= isl_set_coalesce(domain
);
2921 domain
= isl_set_make_disjoint(domain
);
2923 list
= isl_basic_set_list_from_set(domain
);
2924 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2926 isl_set_free(class_domain
);
2930 isl_set_free(domain
);
2931 isl_set_free(class_domain
);
2935 /* Split up the domain at the current depth into disjoint
2936 * basic sets for which code should be generated separately
2937 * for the separation class identified by "pnt".
2939 * We extract the corresponding class domain from domains->sep_class,
2940 * eliminate inner dimensions and pass control to compute_partial_domains.
2942 static int compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
2944 struct isl_codegen_domains
*domains
= user
;
2949 class_set
= isl_set_from_point(pnt
);
2950 domain
= isl_map_domain(isl_map_intersect_range(
2951 isl_map_copy(domains
->sep_class
), class_set
));
2952 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
2953 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2955 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
2959 isl_set_free(domain
);
2963 return compute_partial_domains(domains
, domain
);
2966 /* Extract the domains at the current depth that should be atomic,
2967 * separated or unrolled and store them in option.
2969 * The domains specified by the user might overlap, so we make
2970 * them disjoint by subtracting earlier domains from later domains.
2972 static void compute_domains_init_options(isl_set
*option
[4],
2973 __isl_keep isl_ast_build
*build
)
2975 enum isl_ast_loop_type type
, type2
;
2978 for (type
= isl_ast_loop_atomic
;
2979 type
<= isl_ast_loop_separate
; ++type
) {
2980 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
2981 for (type2
= isl_ast_loop_atomic
; type2
< type
; ++type2
)
2982 option
[type
] = isl_set_subtract(option
[type
],
2983 isl_set_copy(option
[type2
]));
2986 unroll
= option
[isl_ast_loop_unroll
];
2987 unroll
= isl_set_coalesce(unroll
);
2988 unroll
= isl_set_make_disjoint(unroll
);
2989 option
[isl_ast_loop_unroll
] = unroll
;
2992 /* Split up the domain at the current depth into disjoint
2993 * basic sets for which code should be generated separately,
2994 * based on the user-specified options.
2995 * Return the list of disjoint basic sets.
2997 * There are three kinds of domains that we need to keep track of.
2998 * - the "schedule domain" is the domain of "executed"
2999 * - the "class domain" is the domain corresponding to the currrent
3001 * - the "option domain" is the domain corresponding to one of the options
3002 * atomic, unroll or separate
3004 * We first consider the individial values of the separation classes
3005 * and split up the domain for each of them separately.
3006 * Finally, we consider the remainder. If no separation classes were
3007 * specified, then we call compute_partial_domains with the universe
3008 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3009 * with inner dimensions removed. We do this because we want to
3010 * avoid computing the complement of the class domains (i.e., the difference
3011 * between the universe and domains->done).
3013 static __isl_give isl_basic_set_list
*compute_domains(
3014 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
3016 struct isl_codegen_domains domains
;
3019 isl_union_set
*schedule_domain
;
3023 enum isl_ast_loop_type type
;
3029 ctx
= isl_union_map_get_ctx(executed
);
3030 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
3032 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3033 domain
= isl_set_from_union_set(schedule_domain
);
3035 compute_domains_init_options(domains
.option
, build
);
3037 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
3038 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
3039 n_param
= isl_set_dim(classes
, isl_dim_param
);
3040 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
3042 space
= isl_set_get_space(domain
);
3043 domains
.build
= build
;
3044 domains
.schedule_domain
= isl_set_copy(domain
);
3045 domains
.executed
= executed
;
3046 domains
.done
= isl_set_empty(space
);
3048 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
3049 domains
.list
= isl_basic_set_list_free(domains
.list
);
3050 isl_set_free(classes
);
3052 empty
= isl_set_is_empty(domains
.done
);
3054 domains
.list
= isl_basic_set_list_free(domains
.list
);
3055 domain
= isl_set_free(domain
);
3057 isl_set_free(domain
);
3058 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
3060 domain
= isl_ast_build_eliminate(build
, domain
);
3062 if (compute_partial_domains(&domains
, domain
) < 0)
3063 domains
.list
= isl_basic_set_list_free(domains
.list
);
3065 isl_set_free(domains
.schedule_domain
);
3066 isl_set_free(domains
.done
);
3067 isl_map_free(domains
.sep_class
);
3068 for (type
= isl_ast_loop_atomic
; type
<= isl_ast_loop_separate
; ++type
)
3069 isl_set_free(domains
.option
[type
]);
3071 return domains
.list
;
3074 /* Generate code for a single component, after shifting (if any)
3075 * has been applied, in case the schedule was specified as a union map.
3077 * We first split up the domain at the current depth into disjoint
3078 * basic sets based on the user-specified options.
3079 * Then we generated code for each of them and concatenate the results.
3081 static __isl_give isl_ast_graft_list
*generate_shifted_component_flat(
3082 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3084 isl_basic_set_list
*domain_list
;
3085 isl_ast_graft_list
*list
= NULL
;
3087 domain_list
= compute_domains(executed
, build
);
3088 list
= generate_parallel_domains(domain_list
, executed
, build
);
3090 isl_basic_set_list_free(domain_list
);
3091 isl_union_map_free(executed
);
3092 isl_ast_build_free(build
);
3097 /* Generate code for a single component, after shifting (if any)
3098 * has been applied, in case the schedule was specified as a schedule tree
3099 * and the separate option was specified.
3101 * We perform separation on the domain of "executed" and then generate
3102 * an AST for each of the resulting disjoint basic sets.
3104 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_separate(
3105 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3109 isl_basic_set_list
*domain_list
;
3110 isl_ast_graft_list
*list
;
3112 space
= isl_ast_build_get_space(build
, 1);
3113 domain
= separate_schedule_domains(space
,
3114 isl_union_map_copy(executed
), build
);
3115 domain_list
= isl_basic_set_list_from_set(domain
);
3117 list
= generate_parallel_domains(domain_list
, executed
, build
);
3119 isl_basic_set_list_free(domain_list
);
3120 isl_union_map_free(executed
);
3121 isl_ast_build_free(build
);
3126 /* Internal data structure for generate_shifted_component_tree_unroll.
3128 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3129 * "list" collects the constructs grafts.
3131 struct isl_ast_unroll_tree_data
{
3132 isl_union_map
*executed
;
3133 isl_ast_build
*build
;
3134 isl_ast_graft_list
*list
;
3137 /* Initialize data->list to a list of "n" elements.
3139 static int init_unroll_tree(int n
, void *user
)
3141 struct isl_ast_unroll_tree_data
*data
= user
;
3144 ctx
= isl_ast_build_get_ctx(data
->build
);
3145 data
->list
= isl_ast_graft_list_alloc(ctx
, n
);
3150 /* Given an iteration of an unrolled domain represented by "bset",
3151 * generate the corresponding AST and add the result to data->list.
3153 static int do_unroll_tree_iteration(__isl_take isl_basic_set
*bset
, void *user
)
3155 struct isl_ast_unroll_tree_data
*data
= user
;
3157 data
->list
= add_node(data
->list
, isl_union_map_copy(data
->executed
),
3158 bset
, isl_ast_build_copy(data
->build
));
3163 /* Generate code for a single component, after shifting (if any)
3164 * has been applied, in case the schedule was specified as a schedule tree
3165 * and the unroll option was specified.
3167 * We call foreach_iteration to iterate over the individual values and
3168 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3170 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_unroll(
3171 __isl_take isl_union_map
*executed
, __isl_take isl_set
*domain
,
3172 __isl_take isl_ast_build
*build
)
3174 struct isl_ast_unroll_tree_data data
= { executed
, build
, NULL
};
3176 if (foreach_iteration(domain
, build
, &init_unroll_tree
,
3177 &do_unroll_tree_iteration
, &data
) < 0)
3178 data
.list
= isl_ast_graft_list_free(data
.list
);
3180 isl_union_map_free(executed
);
3181 isl_ast_build_free(build
);
3186 /* Generate code for a single component, after shifting (if any)
3187 * has been applied, in case the schedule was specified as a schedule tree.
3188 * In particular, handle the base case where there is either no isolated
3189 * set or we are within the isolated set (in which case "isolated" is set)
3190 * or the iterations that precede or follow the isolated set.
3192 * The schedule domain is broken up or combined into basic sets
3193 * according to the AST generation option specified in the current
3194 * schedule node, which may be either atomic, separate, unroll or
3195 * unspecified. If the option is unspecified, then we currently simply
3196 * split the schedule domain into disjoint basic sets.
3198 * In case the separate option is specified, the AST generation is
3199 * handled by generate_shifted_component_tree_separate.
3200 * In the other cases, we need the global schedule domain.
3201 * In the unroll case, the AST generation is then handled by
3202 * generate_shifted_component_tree_unroll which needs the actual
3203 * schedule domain (with divs that may refer to the current dimension)
3204 * so that stride detection can be performed.
3205 * In the atomic or unspecified case, inner dimensions and divs involving
3206 * the current dimensions should be eliminated.
3207 * The result is then either combined into a single basic set or
3208 * split up into disjoint basic sets.
3209 * Finally an AST is generated for each basic set and the results are
3212 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_base(
3213 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3216 isl_union_set
*schedule_domain
;
3218 isl_basic_set_list
*domain_list
;
3219 isl_ast_graft_list
*list
;
3220 enum isl_ast_loop_type type
;
3222 type
= isl_ast_build_get_loop_type(build
, isolated
);
3226 if (type
== isl_ast_loop_separate
)
3227 return generate_shifted_component_tree_separate(executed
,
3230 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3231 domain
= isl_set_from_union_set(schedule_domain
);
3233 if (type
== isl_ast_loop_unroll
)
3234 return generate_shifted_component_tree_unroll(executed
, domain
,
3237 domain
= isl_ast_build_eliminate(build
, domain
);
3238 domain
= isl_set_coalesce(domain
);
3240 if (type
== isl_ast_loop_atomic
) {
3241 isl_basic_set
*hull
;
3242 hull
= isl_set_unshifted_simple_hull(domain
);
3243 domain_list
= isl_basic_set_list_from_basic_set(hull
);
3245 domain
= isl_set_make_disjoint(domain
);
3246 domain_list
= isl_basic_set_list_from_set(domain
);
3249 list
= generate_parallel_domains(domain_list
, executed
, build
);
3251 isl_basic_set_list_free(domain_list
);
3252 isl_union_map_free(executed
);
3253 isl_ast_build_free(build
);
3257 isl_union_map_free(executed
);
3258 isl_ast_build_free(build
);
3262 /* Generate code for a single component, after shifting (if any)
3263 * has been applied, in case the schedule was specified as a schedule tree.
3264 * In particular, do so for the specified subset of the schedule domsain.
3266 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_part(
3267 __isl_keep isl_union_map
*executed
, __isl_take isl_set
*domain
,
3268 __isl_keep isl_ast_build
*build
, int isolated
)
3270 isl_union_set
*uset
;
3273 uset
= isl_union_set_from_set(domain
);
3274 executed
= isl_union_map_copy(executed
);
3275 executed
= isl_union_map_intersect_domain(executed
, uset
);
3276 empty
= isl_union_map_is_empty(executed
);
3281 isl_union_map_free(executed
);
3282 ctx
= isl_ast_build_get_ctx(build
);
3283 return isl_ast_graft_list_alloc(ctx
, 0);
3286 build
= isl_ast_build_copy(build
);
3287 return generate_shifted_component_tree_base(executed
, build
, isolated
);
3289 isl_union_map_free(executed
);
3293 /* Generate code for a single component, after shifting (if any)
3294 * has been applied, in case the schedule was specified as a schedule tree.
3296 * We first check if the user has specified a (non-empty) isolated
3298 * If so, we break up the schedule domain into iterations that
3299 * precede the isolated domain, the isolated domain itself,
3300 * the iterations that follow the isolated domain and
3301 * the remaining iterations (those that are incomparable
3302 * to the isolated domain).
3303 * We generate an AST for each piece and concatenate the results.
3304 * If no isolated set has been specified, then we generate an
3305 * AST for the entire inverse schedule.
3307 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree(
3308 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3311 int empty
, has_isolate
;
3313 isl_union_set
*schedule_domain
;
3315 isl_basic_set
*hull
;
3316 isl_set
*isolated
, *before
, *after
;
3318 isl_ast_graft_list
*list
, *res
;
3320 build
= isl_ast_build_extract_isolated(build
);
3321 has_isolate
= isl_ast_build_has_isolated(build
);
3322 if (has_isolate
< 0)
3323 executed
= isl_union_map_free(executed
);
3324 else if (!has_isolate
)
3325 return generate_shifted_component_tree_base(executed
, build
, 0);
3327 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3328 domain
= isl_set_from_union_set(schedule_domain
);
3330 isolated
= isl_ast_build_get_isolated(build
);
3331 isolated
= isl_set_intersect(isolated
, isl_set_copy(domain
));
3332 empty
= isl_set_is_empty(isolated
);
3336 isl_set_free(isolated
);
3337 isl_set_free(domain
);
3338 return generate_shifted_component_tree_base(executed
, build
, 0);
3340 isolated
= isl_ast_build_eliminate(build
, isolated
);
3341 hull
= isl_set_unshifted_simple_hull(isolated
);
3342 isolated
= isl_set_from_basic_set(hull
);
3344 depth
= isl_ast_build_get_depth(build
);
3345 space
= isl_space_map_from_set(isl_set_get_space(isolated
));
3346 gt
= isl_map_universe(space
);
3347 for (i
= 0; i
< depth
; ++i
)
3348 gt
= isl_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
3349 gt
= isl_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
3350 lt
= isl_map_reverse(isl_map_copy(gt
));
3351 before
= isl_set_apply(isl_set_copy(isolated
), gt
);
3352 after
= isl_set_apply(isl_set_copy(isolated
), lt
);
3354 domain
= isl_set_subtract(domain
, isl_set_copy(isolated
));
3355 domain
= isl_set_subtract(domain
, isl_set_copy(before
));
3356 domain
= isl_set_subtract(domain
, isl_set_copy(after
));
3357 after
= isl_set_subtract(after
, isl_set_copy(isolated
));
3358 after
= isl_set_subtract(after
, isl_set_copy(before
));
3359 before
= isl_set_subtract(before
, isl_set_copy(isolated
));
3361 res
= generate_shifted_component_tree_part(executed
, before
, build
, 0);
3362 list
= generate_shifted_component_tree_part(executed
, isolated
,
3364 res
= isl_ast_graft_list_concat(res
, list
);
3365 list
= generate_shifted_component_tree_part(executed
, after
, build
, 0);
3366 res
= isl_ast_graft_list_concat(res
, list
);
3367 list
= generate_shifted_component_tree_part(executed
, domain
, build
, 0);
3368 res
= isl_ast_graft_list_concat(res
, list
);
3370 isl_union_map_free(executed
);
3371 isl_ast_build_free(build
);
3375 isl_set_free(domain
);
3376 isl_set_free(isolated
);
3377 isl_union_map_free(executed
);
3378 isl_ast_build_free(build
);
3382 /* Generate code for a single component, after shifting (if any)
3385 * Call generate_shifted_component_tree or generate_shifted_component_flat
3386 * depending on whether the schedule was specified as a schedule tree.
3388 static __isl_give isl_ast_graft_list
*generate_shifted_component(
3389 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3391 if (isl_ast_build_has_schedule_node(build
))
3392 return generate_shifted_component_tree(executed
, build
);
3394 return generate_shifted_component_flat(executed
, build
);
3397 struct isl_set_map_pair
{
3402 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3403 * of indices into the "domain" array,
3404 * return the union of the "map" fields of the elements
3405 * indexed by the first "n" elements of "order".
3407 static __isl_give isl_union_map
*construct_component_executed(
3408 struct isl_set_map_pair
*domain
, int *order
, int n
)
3412 isl_union_map
*executed
;
3414 map
= isl_map_copy(domain
[order
[0]].map
);
3415 executed
= isl_union_map_from_map(map
);
3416 for (i
= 1; i
< n
; ++i
) {
3417 map
= isl_map_copy(domain
[order
[i
]].map
);
3418 executed
= isl_union_map_add_map(executed
, map
);
3424 /* Generate code for a single component, after shifting (if any)
3427 * The component inverse schedule is specified as the "map" fields
3428 * of the elements of "domain" indexed by the first "n" elements of "order".
3430 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
3431 struct isl_set_map_pair
*domain
, int *order
, int n
,
3432 __isl_take isl_ast_build
*build
)
3434 isl_union_map
*executed
;
3436 executed
= construct_component_executed(domain
, order
, n
);
3437 return generate_shifted_component(executed
, build
);
3440 /* Does set dimension "pos" of "set" have an obviously fixed value?
3442 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
3447 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
3450 fixed
= !isl_val_is_nan(v
);
3456 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3457 * of indices into the "domain" array,
3458 * do all (except for at most one) of the "set" field of the elements
3459 * indexed by the first "n" elements of "order" have a fixed value
3460 * at position "depth"?
3462 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
3463 int *order
, int n
, int depth
)
3468 for (i
= 0; i
< n
; ++i
) {
3471 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3484 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3485 * of indices into the "domain" array,
3486 * eliminate the inner dimensions from the "set" field of the elements
3487 * indexed by the first "n" elements of "order", provided the current
3488 * dimension does not have a fixed value.
3490 * Return the index of the first element in "order" with a corresponding
3491 * "set" field that does not have an (obviously) fixed value.
3493 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
3494 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
3499 for (i
= n
- 1; i
>= 0; --i
) {
3501 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3506 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
3507 domain
[order
[i
]].set
);
3514 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3515 * of indices into the "domain" array,
3516 * find the element of "domain" (amongst those indexed by the first "n"
3517 * elements of "order") with the "set" field that has the smallest
3518 * value for the current iterator.
3520 * Note that the domain with the smallest value may depend on the parameters
3521 * and/or outer loop dimension. Since the result of this function is only
3522 * used as heuristic, we only make a reasonable attempt at finding the best
3523 * domain, one that should work in case a single domain provides the smallest
3524 * value for the current dimension over all values of the parameters
3525 * and outer dimensions.
3527 * In particular, we compute the smallest value of the first domain
3528 * and replace it by that of any later domain if that later domain
3529 * has a smallest value that is smaller for at least some value
3530 * of the parameters and outer dimensions.
3532 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
3533 __isl_keep isl_ast_build
*build
)
3539 min_first
= isl_ast_build_map_to_iterator(build
,
3540 isl_set_copy(domain
[order
[0]].set
));
3541 min_first
= isl_map_lexmin(min_first
);
3543 for (i
= 1; i
< n
; ++i
) {
3544 isl_map
*min
, *test
;
3547 min
= isl_ast_build_map_to_iterator(build
,
3548 isl_set_copy(domain
[order
[i
]].set
));
3549 min
= isl_map_lexmin(min
);
3550 test
= isl_map_copy(min
);
3551 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
3552 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
3553 empty
= isl_map_is_empty(test
);
3555 if (empty
>= 0 && !empty
) {
3556 isl_map_free(min_first
);
3566 isl_map_free(min_first
);
3568 return i
< n
? -1 : first
;
3571 /* Construct a shifted inverse schedule based on the original inverse schedule,
3572 * the stride and the offset.
3574 * The original inverse schedule is specified as the "map" fields
3575 * of the elements of "domain" indexed by the first "n" elements of "order".
3577 * "stride" and "offset" are such that the difference
3578 * between the values of the current dimension of domain "i"
3579 * and the values of the current dimension for some reference domain are
3582 * stride * integer + offset[i]
3584 * Moreover, 0 <= offset[i] < stride.
3586 * For each domain, we create a map
3588 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3590 * where j refers to the current dimension and the other dimensions are
3591 * unchanged, and apply this map to the original schedule domain.
3593 * For example, for the original schedule
3595 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3597 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3598 * we apply the mapping
3602 * to the schedule of the "A" domain and the mapping
3604 * { [j - 1] -> [j, 1] }
3606 * to the schedule of the "B" domain.
3609 * Note that after the transformation, the differences between pairs
3610 * of values of the current dimension over all domains are multiples
3611 * of stride and that we have therefore exposed the stride.
3614 * To see that the mapping preserves the lexicographic order,
3615 * first note that each of the individual maps above preserves the order.
3616 * If the value of the current iterator is j1 in one domain and j2 in another,
3617 * then if j1 = j2, we know that the same map is applied to both domains
3618 * and the order is preserved.
3619 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3620 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3624 * and the order is preserved.
3625 * If c1 < c2, then we know
3631 * j2 - j1 = n * s + r
3633 * with n >= 0 and 0 <= r < s.
3634 * In other words, r = c2 - c1.
3645 * (j1 - c1, c1) << (j2 - c2, c2)
3647 * with "<<" the lexicographic order, proving that the order is preserved
3650 static __isl_give isl_union_map
*contruct_shifted_executed(
3651 struct isl_set_map_pair
*domain
, int *order
, int n
,
3652 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3653 __isl_take isl_ast_build
*build
)
3656 isl_union_map
*executed
;
3662 depth
= isl_ast_build_get_depth(build
);
3663 space
= isl_ast_build_get_space(build
, 1);
3664 executed
= isl_union_map_empty(isl_space_copy(space
));
3665 space
= isl_space_map_from_set(space
);
3666 map
= isl_map_identity(isl_space_copy(space
));
3667 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3668 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3669 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3671 c
= isl_equality_alloc(isl_local_space_from_space(space
));
3672 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3673 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3675 for (i
= 0; i
< n
; ++i
) {
3679 v
= isl_multi_val_get_val(offset
, i
);
3682 map_i
= isl_map_copy(map
);
3683 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
3686 c
= isl_constraint_set_constant_val(c
, v
);
3687 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
3689 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
3691 executed
= isl_union_map_add_map(executed
, map_i
);
3694 isl_constraint_free(c
);
3698 executed
= isl_union_map_free(executed
);
3703 /* Generate code for a single component, after exposing the stride,
3704 * given that the schedule domain is "shifted strided".
3706 * The component inverse schedule is specified as the "map" fields
3707 * of the elements of "domain" indexed by the first "n" elements of "order".
3709 * The schedule domain being "shifted strided" means that the differences
3710 * between the values of the current dimension of domain "i"
3711 * and the values of the current dimension for some reference domain are
3714 * stride * integer + offset[i]
3716 * We first look for the domain with the "smallest" value for the current
3717 * dimension and adjust the offsets such that the offset of the "smallest"
3718 * domain is equal to zero. The other offsets are reduced modulo stride.
3720 * Based on this information, we construct a new inverse schedule in
3721 * contruct_shifted_executed that exposes the stride.
3722 * Since this involves the introduction of a new schedule dimension,
3723 * the build needs to be changed accodingly.
3724 * After computing the AST, the newly introduced dimension needs
3725 * to be removed again from the list of grafts. We do this by plugging
3726 * in a mapping that represents the new schedule domain in terms of the
3727 * old schedule domain.
3729 static __isl_give isl_ast_graft_list
*generate_shift_component(
3730 struct isl_set_map_pair
*domain
, int *order
, int n
,
3731 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3732 __isl_take isl_ast_build
*build
)
3734 isl_ast_graft_list
*list
;
3740 isl_multi_aff
*ma
, *zero
;
3741 isl_union_map
*executed
;
3743 depth
= isl_ast_build_get_depth(build
);
3745 first
= first_offset(domain
, order
, n
, build
);
3749 mv
= isl_multi_val_copy(offset
);
3750 val
= isl_multi_val_get_val(offset
, first
);
3751 val
= isl_val_neg(val
);
3752 mv
= isl_multi_val_add_val(mv
, val
);
3753 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
3755 executed
= contruct_shifted_executed(domain
, order
, n
, stride
, mv
,
3757 space
= isl_ast_build_get_space(build
, 1);
3758 space
= isl_space_map_from_set(space
);
3759 ma
= isl_multi_aff_identity(isl_space_copy(space
));
3760 space
= isl_space_from_domain(isl_space_domain(space
));
3761 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3762 zero
= isl_multi_aff_zero(space
);
3763 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
3764 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
3765 list
= generate_shifted_component(executed
, build
);
3767 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
3769 isl_multi_val_free(mv
);
3773 isl_ast_build_free(build
);
3777 /* Does any node in the schedule tree rooted at the current schedule node
3778 * of "build" depend on outer schedule nodes?
3780 static int has_anchored_subtree(__isl_keep isl_ast_build
*build
)
3782 isl_schedule_node
*node
;
3785 node
= isl_ast_build_get_schedule_node(build
);
3786 dependent
= isl_schedule_node_is_subtree_anchored(node
);
3787 isl_schedule_node_free(node
);
3792 /* Generate code for a single component.
3794 * The component inverse schedule is specified as the "map" fields
3795 * of the elements of "domain" indexed by the first "n" elements of "order".
3797 * This function may modify the "set" fields of "domain".
3799 * Before proceeding with the actual code generation for the component,
3800 * we first check if there are any "shifted" strides, meaning that
3801 * the schedule domains of the individual domains are all strided,
3802 * but that they have different offsets, resulting in the union
3803 * of schedule domains not being strided anymore.
3805 * The simplest example is the schedule
3807 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3809 * Both schedule domains are strided, but their union is not.
3810 * This function detects such cases and then rewrites the schedule to
3812 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3814 * In the new schedule, the schedule domains have the same offset (modulo
3815 * the stride), ensuring that the union of schedule domains is also strided.
3818 * If there is only a single domain in the component, then there is
3819 * nothing to do. Similarly, if the current schedule dimension has
3820 * a fixed value for almost all domains then there is nothing to be done.
3821 * In particular, we need at least two domains where the current schedule
3822 * dimension does not have a fixed value.
3823 * Finally, in case of a schedule map input,
3824 * if any of the options refer to the current schedule dimension,
3825 * then we bail out as well. It would be possible to reformulate the options
3826 * in terms of the new schedule domain, but that would introduce constraints
3827 * that separate the domains in the options and that is something we would
3829 * In the case of a schedule tree input, we bail out if any of
3830 * the descendants of the current schedule node refer to outer
3831 * schedule nodes in any way.
3834 * To see if there is any shifted stride, we look at the differences
3835 * between the values of the current dimension in pairs of domains
3836 * for equal values of outer dimensions. These differences should be
3841 * with "m" the stride and "r" a constant. Note that we cannot perform
3842 * this analysis on individual domains as the lower bound in each domain
3843 * may depend on parameters or outer dimensions and so the current dimension
3844 * itself may not have a fixed remainder on division by the stride.
3846 * In particular, we compare the first domain that does not have an
3847 * obviously fixed value for the current dimension to itself and all
3848 * other domains and collect the offsets and the gcd of the strides.
3849 * If the gcd becomes one, then we failed to find shifted strides.
3850 * If the gcd is zero, then the differences were all fixed, meaning
3851 * that some domains had non-obviously fixed values for the current dimension.
3852 * If all the offsets are the same (for those domains that do not have
3853 * an obviously fixed value for the current dimension), then we do not
3854 * apply the transformation.
3855 * If none of the domains were skipped, then there is nothing to do.
3856 * If some of them were skipped, then if we apply separation, the schedule
3857 * domain should get split in pieces with a (non-shifted) stride.
3859 * Otherwise, we apply a shift to expose the stride in
3860 * generate_shift_component.
3862 static __isl_give isl_ast_graft_list
*generate_component(
3863 struct isl_set_map_pair
*domain
, int *order
, int n
,
3864 __isl_take isl_ast_build
*build
)
3871 isl_val
*gcd
= NULL
;
3875 isl_ast_graft_list
*list
;
3878 depth
= isl_ast_build_get_depth(build
);
3881 if (skip
>= 0 && !skip
)
3882 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
3883 if (skip
>= 0 && !skip
) {
3884 if (isl_ast_build_has_schedule_node(build
))
3885 skip
= has_anchored_subtree(build
);
3887 skip
= isl_ast_build_options_involve_depth(build
);
3892 return generate_shifted_component_from_list(domain
,
3895 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
3899 ctx
= isl_ast_build_get_ctx(build
);
3901 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
3904 for (i
= 0; i
< n
; ++i
) {
3907 map
= isl_map_from_domain_and_range(
3908 isl_set_copy(domain
[order
[base
]].set
),
3909 isl_set_copy(domain
[order
[i
]].set
));
3910 for (d
= 0; d
< depth
; ++d
)
3911 map
= isl_map_equate(map
, isl_dim_in
, d
,
3913 deltas
= isl_map_deltas(map
);
3914 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
3915 isl_set_free(deltas
);
3922 gcd
= isl_val_gcd(gcd
, m
);
3923 if (isl_val_is_one(gcd
)) {
3927 mv
= isl_multi_val_set_val(mv
, i
, r
);
3929 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3935 if (fixed
&& i
> base
) {
3937 a
= isl_multi_val_get_val(mv
, i
);
3938 b
= isl_multi_val_get_val(mv
, base
);
3939 if (isl_val_ne(a
, b
))
3946 if (res
< 0 || !gcd
) {
3947 isl_ast_build_free(build
);
3949 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
3950 list
= generate_shifted_component_from_list(domain
,
3953 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
3958 isl_multi_val_free(mv
);
3962 isl_ast_build_free(build
);
3966 /* Store both "map" itself and its domain in the
3967 * structure pointed to by *next and advance to the next array element.
3969 static int extract_domain(__isl_take isl_map
*map
, void *user
)
3971 struct isl_set_map_pair
**next
= user
;
3973 (*next
)->map
= isl_map_copy(map
);
3974 (*next
)->set
= isl_map_domain(map
);
3980 static int after_in_tree(__isl_keep isl_union_map
*umap
,
3981 __isl_keep isl_schedule_node
*node
);
3983 /* Is any domain element of "umap" scheduled after any of
3984 * the corresponding image elements by the tree rooted at
3985 * the child of "node"?
3987 static int after_in_child(__isl_keep isl_union_map
*umap
,
3988 __isl_keep isl_schedule_node
*node
)
3990 isl_schedule_node
*child
;
3993 child
= isl_schedule_node_get_child(node
, 0);
3994 after
= after_in_tree(umap
, child
);
3995 isl_schedule_node_free(child
);
4000 /* Is any domain element of "umap" scheduled after any of
4001 * the corresponding image elements by the tree rooted at
4002 * the band node "node"?
4004 * We first check if any domain element is scheduled after any
4005 * of the corresponding image elements by the band node itself.
4006 * If not, we restrict "map" to those pairs of element that
4007 * are scheduled together by the band node and continue with
4008 * the child of the band node.
4009 * If there are no such pairs then the map passed to after_in_child
4010 * will be empty causing it to return 0.
4012 static int after_in_band(__isl_keep isl_union_map
*umap
,
4013 __isl_keep isl_schedule_node
*node
)
4015 isl_multi_union_pw_aff
*mupa
;
4016 isl_union_map
*partial
, *test
, *gt
, *universe
, *umap1
, *umap2
;
4017 isl_union_set
*domain
, *range
;
4022 if (isl_schedule_node_band_n_member(node
) == 0)
4023 return after_in_child(umap
, node
);
4025 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
4026 space
= isl_multi_union_pw_aff_get_space(mupa
);
4027 partial
= isl_union_map_from_multi_union_pw_aff(mupa
);
4028 test
= isl_union_map_copy(umap
);
4029 test
= isl_union_map_apply_domain(test
, isl_union_map_copy(partial
));
4030 test
= isl_union_map_apply_range(test
, isl_union_map_copy(partial
));
4031 gt
= isl_union_map_from_map(isl_map_lex_gt(space
));
4032 test
= isl_union_map_intersect(test
, gt
);
4033 empty
= isl_union_map_is_empty(test
);
4034 isl_union_map_free(test
);
4036 if (empty
< 0 || !empty
) {
4037 isl_union_map_free(partial
);
4038 return empty
< 0 ? -1 : 1;
4041 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4042 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4043 range
= isl_union_map_range(universe
);
4044 umap1
= isl_union_map_copy(partial
);
4045 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4046 umap2
= isl_union_map_intersect_domain(partial
, range
);
4047 test
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4048 test
= isl_union_map_intersect(test
, isl_union_map_copy(umap
));
4049 after
= after_in_child(test
, node
);
4050 isl_union_map_free(test
);
4054 /* Is any domain element of "umap" scheduled after any of
4055 * the corresponding image elements by the tree rooted at
4056 * the context node "node"?
4058 * The context constraints apply to the schedule domain,
4059 * so we cannot apply them directly to "umap", which contains
4060 * pairs of statement instances. Instead, we add them
4061 * to the range of the prefix schedule for both domain and
4064 static int after_in_context(__isl_keep isl_union_map
*umap
,
4065 __isl_keep isl_schedule_node
*node
)
4067 isl_union_map
*prefix
, *universe
, *umap1
, *umap2
;
4068 isl_union_set
*domain
, *range
;
4072 umap
= isl_union_map_copy(umap
);
4073 context
= isl_schedule_node_context_get_context(node
);
4074 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4075 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4076 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4077 range
= isl_union_map_range(universe
);
4078 umap1
= isl_union_map_copy(prefix
);
4079 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4080 umap2
= isl_union_map_intersect_domain(prefix
, range
);
4081 umap1
= isl_union_map_intersect_range(umap1
,
4082 isl_union_set_from_set(context
));
4083 umap1
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4084 umap
= isl_union_map_intersect(umap
, umap1
);
4086 after
= after_in_child(umap
, node
);
4088 isl_union_map_free(umap
);
4093 /* Is any domain element of "umap" scheduled after any of
4094 * the corresponding image elements by the tree rooted at
4095 * the expansion node "node"?
4097 * We apply the expansion to domain and range of "umap" and
4098 * continue with its child.
4100 static int after_in_expansion(__isl_keep isl_union_map
*umap
,
4101 __isl_keep isl_schedule_node
*node
)
4103 isl_union_map
*expansion
;
4106 expansion
= isl_schedule_node_expansion_get_expansion(node
);
4107 umap
= isl_union_map_copy(umap
);
4108 umap
= isl_union_map_apply_domain(umap
, isl_union_map_copy(expansion
));
4109 umap
= isl_union_map_apply_range(umap
, expansion
);
4111 after
= after_in_child(umap
, node
);
4113 isl_union_map_free(umap
);
4118 /* Is any domain element of "umap" scheduled after any of
4119 * the corresponding image elements by the tree rooted at
4120 * the filter node "node"?
4122 * We intersect domain and range of "umap" with the filter and
4123 * continue with its child.
4125 static int after_in_filter(__isl_keep isl_union_map
*umap
,
4126 __isl_keep isl_schedule_node
*node
)
4128 isl_union_set
*filter
;
4131 umap
= isl_union_map_copy(umap
);
4132 filter
= isl_schedule_node_filter_get_filter(node
);
4133 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(filter
));
4134 umap
= isl_union_map_intersect_range(umap
, filter
);
4136 after
= after_in_child(umap
, node
);
4138 isl_union_map_free(umap
);
4143 /* Is any domain element of "umap" scheduled after any of
4144 * the corresponding image elements by the tree rooted at
4145 * the set node "node"?
4147 * This is only the case if this condition holds in any
4148 * of the (filter) children of the set node.
4149 * In particular, if the domain and the range of "umap"
4150 * are contained in different children, then the condition
4153 static int after_in_set(__isl_keep isl_union_map
*umap
,
4154 __isl_keep isl_schedule_node
*node
)
4158 n
= isl_schedule_node_n_children(node
);
4159 for (i
= 0; i
< n
; ++i
) {
4160 isl_schedule_node
*child
;
4163 child
= isl_schedule_node_get_child(node
, i
);
4164 after
= after_in_tree(umap
, child
);
4165 isl_schedule_node_free(child
);
4167 if (after
< 0 || after
)
4174 /* Return the filter of child "i" of "node".
4176 static __isl_give isl_union_set
*child_filter(
4177 __isl_keep isl_schedule_node
*node
, int i
)
4179 isl_schedule_node
*child
;
4180 isl_union_set
*filter
;
4182 child
= isl_schedule_node_get_child(node
, i
);
4183 filter
= isl_schedule_node_filter_get_filter(child
);
4184 isl_schedule_node_free(child
);
4189 /* Is any domain element of "umap" scheduled after any of
4190 * the corresponding image elements by the tree rooted at
4191 * the sequence node "node"?
4193 * This happens in particular if any domain element is
4194 * contained in a later child than one containing a range element or
4195 * if the condition holds within a given child in the sequence.
4196 * The later part of the condition is checked by after_in_set.
4198 static int after_in_sequence(__isl_keep isl_union_map
*umap
,
4199 __isl_keep isl_schedule_node
*node
)
4202 isl_union_map
*umap_i
;
4203 int empty
, after
= 0;
4205 n
= isl_schedule_node_n_children(node
);
4206 for (i
= 1; i
< n
; ++i
) {
4207 isl_union_set
*filter_i
;
4209 umap_i
= isl_union_map_copy(umap
);
4210 filter_i
= child_filter(node
, i
);
4211 umap_i
= isl_union_map_intersect_domain(umap_i
, filter_i
);
4212 empty
= isl_union_map_is_empty(umap_i
);
4216 isl_union_map_free(umap_i
);
4220 for (j
= 0; j
< i
; ++j
) {
4221 isl_union_set
*filter_j
;
4222 isl_union_map
*umap_ij
;
4224 umap_ij
= isl_union_map_copy(umap_i
);
4225 filter_j
= child_filter(node
, j
);
4226 umap_ij
= isl_union_map_intersect_range(umap_ij
,
4228 empty
= isl_union_map_is_empty(umap_ij
);
4229 isl_union_map_free(umap_ij
);
4239 isl_union_map_free(umap_i
);
4244 if (after
< 0 || after
)
4247 return after_in_set(umap
, node
);
4249 isl_union_map_free(umap_i
);
4253 /* Is any domain element of "umap" scheduled after any of
4254 * the corresponding image elements by the tree rooted at "node"?
4256 * If "umap" is empty, then clearly there is no such element.
4257 * Otherwise, consider the different types of nodes separately.
4259 static int after_in_tree(__isl_keep isl_union_map
*umap
,
4260 __isl_keep isl_schedule_node
*node
)
4263 enum isl_schedule_node_type type
;
4265 empty
= isl_union_map_is_empty(umap
);
4273 type
= isl_schedule_node_get_type(node
);
4275 case isl_schedule_node_error
:
4277 case isl_schedule_node_leaf
:
4279 case isl_schedule_node_band
:
4280 return after_in_band(umap
, node
);
4281 case isl_schedule_node_domain
:
4282 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
4283 "unexpected internal domain node", return -1);
4284 case isl_schedule_node_context
:
4285 return after_in_context(umap
, node
);
4286 case isl_schedule_node_expansion
:
4287 return after_in_expansion(umap
, node
);
4288 case isl_schedule_node_filter
:
4289 return after_in_filter(umap
, node
);
4290 case isl_schedule_node_set
:
4291 return after_in_set(umap
, node
);
4292 case isl_schedule_node_sequence
:
4293 return after_in_sequence(umap
, node
);
4299 /* Is any domain element of "map1" scheduled after any domain
4300 * element of "map2" by the subtree underneath the current band node,
4301 * while at the same time being scheduled together by the current
4302 * band node, i.e., by "map1" and "map2?
4304 * If the child of the current band node is a leaf, then
4305 * no element can be scheduled after any other element.
4307 * Otherwise, we construct a relation between domain elements
4308 * of "map1" and domain elements of "map2" that are scheduled
4309 * together and then check if the subtree underneath the current
4310 * band node determines their relative order.
4312 static int after_in_subtree(__isl_keep isl_ast_build
*build
,
4313 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4315 isl_schedule_node
*node
;
4317 isl_union_map
*umap
;
4320 node
= isl_ast_build_get_schedule_node(build
);
4323 node
= isl_schedule_node_child(node
, 0);
4324 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
4325 isl_schedule_node_free(node
);
4328 map
= isl_map_copy(map2
);
4329 map
= isl_map_apply_domain(map
, isl_map_copy(map1
));
4330 umap
= isl_union_map_from_map(map
);
4331 after
= after_in_tree(umap
, node
);
4332 isl_union_map_free(umap
);
4333 isl_schedule_node_free(node
);
4337 /* Internal data for any_scheduled_after.
4339 * "build" is the build in which the AST is constructed.
4340 * "depth" is the number of loops that have already been generated
4341 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4342 * "domain" is an array of set-map pairs corresponding to the different
4343 * iteration domains. The set is the schedule domain, i.e., the domain
4344 * of the inverse schedule, while the map is the inverse schedule itself.
4346 struct isl_any_scheduled_after_data
{
4347 isl_ast_build
*build
;
4349 int group_coscheduled
;
4350 struct isl_set_map_pair
*domain
;
4353 /* Is any element of domain "i" scheduled after any element of domain "j"
4354 * (for a common iteration of the first data->depth loops)?
4356 * data->domain[i].set contains the domain of the inverse schedule
4357 * for domain "i", i.e., elements in the schedule domain.
4359 * If we are inside a band of a schedule tree and there is a pair
4360 * of elements in the two domains that is schedule together by
4361 * the current band, then we check if any element of "i" may be schedule
4362 * after element of "j" by the descendants of the band node.
4364 * If data->group_coscheduled is set, then we also return 1 if there
4365 * is any pair of elements in the two domains that are scheduled together.
4367 static int any_scheduled_after(int i
, int j
, void *user
)
4369 struct isl_any_scheduled_after_data
*data
= user
;
4370 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
4373 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
4376 follows
= isl_set_follows_at(data
->domain
[i
].set
,
4377 data
->domain
[j
].set
, pos
);
4387 if (isl_ast_build_has_schedule_node(data
->build
)) {
4390 after
= after_in_subtree(data
->build
, data
->domain
[i
].map
,
4391 data
->domain
[j
].map
);
4392 if (after
< 0 || after
)
4396 return data
->group_coscheduled
;
4399 /* Look for independent components at the current depth and generate code
4400 * for each component separately. The resulting lists of grafts are
4401 * merged in an attempt to combine grafts with identical guards.
4403 * Code for two domains can be generated separately if all the elements
4404 * of one domain are scheduled before (or together with) all the elements
4405 * of the other domain. We therefore consider the graph with as nodes
4406 * the domains and an edge between two nodes if any element of the first
4407 * node is scheduled after any element of the second node.
4408 * If the ast_build_group_coscheduled is set, then we also add an edge if
4409 * there is any pair of elements in the two domains that are scheduled
4411 * Code is then generated (by generate_component)
4412 * for each of the strongly connected components in this graph
4413 * in their topological order.
4415 * Since the test is performed on the domain of the inverse schedules of
4416 * the different domains, we precompute these domains and store
4417 * them in data.domain.
4419 static __isl_give isl_ast_graft_list
*generate_components(
4420 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4423 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4424 int n
= isl_union_map_n_map(executed
);
4425 struct isl_any_scheduled_after_data data
;
4426 struct isl_set_map_pair
*next
;
4427 struct isl_tarjan_graph
*g
= NULL
;
4428 isl_ast_graft_list
*list
= NULL
;
4431 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
4437 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
4443 data
.depth
= isl_ast_build_get_depth(build
);
4444 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
4445 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
4449 list
= isl_ast_graft_list_alloc(ctx
, 0);
4453 isl_ast_graft_list
*list_c
;
4456 if (g
->order
[i
] == -1)
4457 isl_die(ctx
, isl_error_internal
, "cannot happen",
4460 while (g
->order
[i
] != -1) {
4464 list_c
= generate_component(data
.domain
,
4465 g
->order
+ first
, i
- first
,
4466 isl_ast_build_copy(build
));
4467 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
4473 error
: list
= isl_ast_graft_list_free(list
);
4474 isl_tarjan_graph_free(g
);
4475 for (i
= 0; i
< n_domain
; ++i
) {
4476 isl_map_free(data
.domain
[i
].map
);
4477 isl_set_free(data
.domain
[i
].set
);
4480 isl_union_map_free(executed
);
4481 isl_ast_build_free(build
);
4486 /* Generate code for the next level (and all inner levels).
4488 * If "executed" is empty, i.e., no code needs to be generated,
4489 * then we return an empty list.
4491 * If we have already generated code for all loop levels, then we pass
4492 * control to generate_inner_level.
4494 * If "executed" lives in a single space, i.e., if code needs to be
4495 * generated for a single domain, then there can only be a single
4496 * component and we go directly to generate_shifted_component.
4497 * Otherwise, we call generate_components to detect the components
4498 * and to call generate_component on each of them separately.
4500 static __isl_give isl_ast_graft_list
*generate_next_level(
4501 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4505 if (!build
|| !executed
)
4508 if (isl_union_map_is_empty(executed
)) {
4509 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4510 isl_union_map_free(executed
);
4511 isl_ast_build_free(build
);
4512 return isl_ast_graft_list_alloc(ctx
, 0);
4515 depth
= isl_ast_build_get_depth(build
);
4516 if (depth
>= isl_ast_build_dim(build
, isl_dim_set
))
4517 return generate_inner_level(executed
, build
);
4519 if (isl_union_map_n_map(executed
) == 1)
4520 return generate_shifted_component(executed
, build
);
4522 return generate_components(executed
, build
);
4524 isl_union_map_free(executed
);
4525 isl_ast_build_free(build
);
4529 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4530 * internal, executed and build are the inputs to generate_code.
4531 * list collects the output.
4533 struct isl_generate_code_data
{
4535 isl_union_map
*executed
;
4536 isl_ast_build
*build
;
4538 isl_ast_graft_list
*list
;
4541 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4545 * with E the external build schedule and S the additional schedule "space",
4546 * reformulate the inverse schedule in terms of the internal schedule domain,
4551 * We first obtain a mapping
4555 * take the inverse and the product with S -> S, resulting in
4557 * [I -> S] -> [E -> S]
4559 * Applying the map to the input produces the desired result.
4561 static __isl_give isl_union_map
*internal_executed(
4562 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
4563 __isl_keep isl_ast_build
*build
)
4567 proj
= isl_ast_build_get_schedule_map(build
);
4568 proj
= isl_map_reverse(proj
);
4569 space
= isl_space_map_from_set(isl_space_copy(space
));
4570 id
= isl_map_identity(space
);
4571 proj
= isl_map_product(proj
, id
);
4572 executed
= isl_union_map_apply_domain(executed
,
4573 isl_union_map_from_map(proj
));
4577 /* Generate an AST that visits the elements in the range of data->executed
4578 * in the relative order specified by the corresponding domain element(s)
4579 * for those domain elements that belong to "set".
4580 * Add the result to data->list.
4582 * The caller ensures that "set" is a universe domain.
4583 * "space" is the space of the additional part of the schedule.
4584 * It is equal to the space of "set" if build->domain is parametric.
4585 * Otherwise, it is equal to the range of the wrapped space of "set".
4587 * If the build space is not parametric and
4588 * if isl_ast_build_node_from_schedule_map
4589 * was called from an outside user (data->internal not set), then
4590 * the (inverse) schedule refers to the external build domain and needs to
4591 * be transformed to refer to the internal build domain.
4593 * If the build space is parametric, then we add some of the parameter
4594 * constraints to the executed relation. Adding these constraints
4595 * allows for an earlier detection of conflicts in some cases.
4596 * However, we do not want to divide the executed relation into
4597 * more disjuncts than necessary. We therefore approximate
4598 * the constraints on the parameters by a single disjunct set.
4600 * The build is extended to include the additional part of the schedule.
4601 * If the original build space was not parametric, then the options
4602 * in data->build refer only to the additional part of the schedule
4603 * and they need to be adjusted to refer to the complete AST build
4606 * After having adjusted inverse schedule and build, we start generating
4607 * code with the outer loop of the current code generation
4608 * in generate_next_level.
4610 * If the original build space was not parametric, we undo the embedding
4611 * on the resulting isl_ast_node_list so that it can be used within
4612 * the outer AST build.
4614 static int generate_code_in_space(struct isl_generate_code_data
*data
,
4615 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
4617 isl_union_map
*executed
;
4618 isl_ast_build
*build
;
4619 isl_ast_graft_list
*list
;
4622 executed
= isl_union_map_copy(data
->executed
);
4623 executed
= isl_union_map_intersect_domain(executed
,
4624 isl_union_set_from_set(set
));
4626 embed
= !isl_set_is_params(data
->build
->domain
);
4627 if (embed
&& !data
->internal
)
4628 executed
= internal_executed(executed
, space
, data
->build
);
4631 domain
= isl_ast_build_get_domain(data
->build
);
4632 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
4633 executed
= isl_union_map_intersect_params(executed
, domain
);
4636 build
= isl_ast_build_copy(data
->build
);
4637 build
= isl_ast_build_product(build
, space
);
4639 list
= generate_next_level(executed
, build
);
4641 list
= isl_ast_graft_list_unembed(list
, embed
);
4643 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
4648 /* Generate an AST that visits the elements in the range of data->executed
4649 * in the relative order specified by the corresponding domain element(s)
4650 * for those domain elements that belong to "set".
4651 * Add the result to data->list.
4653 * The caller ensures that "set" is a universe domain.
4655 * If the build space S is not parametric, then the space of "set"
4656 * need to be a wrapped relation with S as domain. That is, it needs
4661 * Check this property and pass control to generate_code_in_space
4663 * If the build space is not parametric, then T is the space of "set".
4665 static int generate_code_set(__isl_take isl_set
*set
, void *user
)
4667 struct isl_generate_code_data
*data
= user
;
4668 isl_space
*space
, *build_space
;
4671 space
= isl_set_get_space(set
);
4673 if (isl_set_is_params(data
->build
->domain
))
4674 return generate_code_in_space(data
, set
, space
);
4676 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
4677 space
= isl_space_unwrap(space
);
4678 is_domain
= isl_space_is_domain(build_space
, space
);
4679 isl_space_free(build_space
);
4680 space
= isl_space_range(space
);
4685 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4686 "invalid nested schedule space", goto error
);
4688 return generate_code_in_space(data
, set
, space
);
4691 isl_space_free(space
);
4695 /* Generate an AST that visits the elements in the range of "executed"
4696 * in the relative order specified by the corresponding domain element(s).
4698 * "build" is an isl_ast_build that has either been constructed by
4699 * isl_ast_build_from_context or passed to a callback set by
4700 * isl_ast_build_set_create_leaf.
4701 * In the first case, the space of the isl_ast_build is typically
4702 * a parametric space, although this is currently not enforced.
4703 * In the second case, the space is never a parametric space.
4704 * If the space S is not parametric, then the domain space(s) of "executed"
4705 * need to be wrapped relations with S as domain.
4707 * If the domain of "executed" consists of several spaces, then an AST
4708 * is generated for each of them (in arbitrary order) and the results
4711 * If "internal" is set, then the domain "S" above refers to the internal
4712 * schedule domain representation. Otherwise, it refers to the external
4713 * representation, as returned by isl_ast_build_get_schedule_space.
4715 * We essentially run over all the spaces in the domain of "executed"
4716 * and call generate_code_set on each of them.
4718 static __isl_give isl_ast_graft_list
*generate_code(
4719 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
4723 struct isl_generate_code_data data
= { 0 };
4725 isl_union_set
*schedule_domain
;
4726 isl_union_map
*universe
;
4730 space
= isl_ast_build_get_space(build
, 1);
4731 space
= isl_space_align_params(space
,
4732 isl_union_map_get_space(executed
));
4733 space
= isl_space_align_params(space
,
4734 isl_union_map_get_space(build
->options
));
4735 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
4736 executed
= isl_union_map_align_params(executed
, space
);
4737 if (!executed
|| !build
)
4740 ctx
= isl_ast_build_get_ctx(build
);
4742 data
.internal
= internal
;
4743 data
.executed
= executed
;
4745 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
4747 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
4748 schedule_domain
= isl_union_map_domain(universe
);
4749 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
4751 data
.list
= isl_ast_graft_list_free(data
.list
);
4753 isl_union_set_free(schedule_domain
);
4754 isl_union_map_free(executed
);
4756 isl_ast_build_free(build
);
4759 isl_union_map_free(executed
);
4760 isl_ast_build_free(build
);
4764 /* Generate an AST that visits the elements in the domain of "schedule"
4765 * in the relative order specified by the corresponding image element(s).
4767 * "build" is an isl_ast_build that has either been constructed by
4768 * isl_ast_build_from_context or passed to a callback set by
4769 * isl_ast_build_set_create_leaf.
4770 * In the first case, the space of the isl_ast_build is typically
4771 * a parametric space, although this is currently not enforced.
4772 * In the second case, the space is never a parametric space.
4773 * If the space S is not parametric, then the range space(s) of "schedule"
4774 * need to be wrapped relations with S as domain.
4776 * If the range of "schedule" consists of several spaces, then an AST
4777 * is generated for each of them (in arbitrary order) and the results
4780 * We first initialize the local copies of the relevant options.
4781 * We do this here rather than when the isl_ast_build is created
4782 * because the options may have changed between the construction
4783 * of the isl_ast_build and the call to isl_generate_code.
4785 * The main computation is performed on an inverse schedule (with
4786 * the schedule domain in the domain and the elements to be executed
4787 * in the range) called "executed".
4789 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule_map(
4790 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
4792 isl_ast_graft_list
*list
;
4794 isl_union_map
*executed
;
4796 build
= isl_ast_build_copy(build
);
4797 build
= isl_ast_build_set_single_valued(build
, 0);
4798 schedule
= isl_union_map_coalesce(schedule
);
4799 schedule
= isl_union_map_remove_redundancies(schedule
);
4800 executed
= isl_union_map_reverse(schedule
);
4801 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
4802 node
= isl_ast_node_from_graft_list(list
, build
);
4803 isl_ast_build_free(build
);
4808 /* The old name for isl_ast_build_node_from_schedule_map.
4809 * It is being kept for backward compatibility, but
4810 * it will be removed in the future.
4812 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
4813 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
4815 return isl_ast_build_node_from_schedule_map(build
, schedule
);
4818 /* Generate an AST that visits the elements in the domain of "executed"
4819 * in the relative order specified by the band node "node" and its descendants.
4821 * The relation "executed" maps the outer generated loop iterators
4822 * to the domain elements executed by those iterations.
4824 * If the band is empty, we continue with its descendants.
4825 * Otherwise, we extend the build and the inverse schedule with
4826 * the additional space/partial schedule and continue generating
4827 * an AST in generate_next_level.
4828 * As soon as we have extended the inverse schedule with the additional
4829 * partial schedule, we look for equalities that may exists between
4830 * the old and the new part.
4832 static __isl_give isl_ast_graft_list
*build_ast_from_band(
4833 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
4834 __isl_take isl_union_map
*executed
)
4837 isl_multi_union_pw_aff
*extra
;
4838 isl_union_map
*extra_umap
;
4839 isl_ast_graft_list
*list
;
4842 if (!build
|| !node
|| !executed
)
4845 if (isl_schedule_node_band_n_member(node
) == 0)
4846 return build_ast_from_child(build
, node
, executed
);
4848 extra
= isl_schedule_node_band_get_partial_schedule(node
);
4849 extra
= isl_multi_union_pw_aff_align_params(extra
,
4850 isl_ast_build_get_space(build
, 1));
4851 space
= isl_multi_union_pw_aff_get_space(extra
);
4853 extra_umap
= isl_union_map_from_multi_union_pw_aff(extra
);
4854 extra_umap
= isl_union_map_reverse(extra_umap
);
4856 executed
= isl_union_map_domain_product(executed
, extra_umap
);
4857 executed
= isl_union_map_detect_equalities(executed
);
4859 n1
= isl_ast_build_dim(build
, isl_dim_param
);
4860 build
= isl_ast_build_product(build
, space
);
4861 n2
= isl_ast_build_dim(build
, isl_dim_param
);
4863 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
4864 "band node is not allowed to introduce new parameters",
4865 build
= isl_ast_build_free(build
));
4866 build
= isl_ast_build_set_schedule_node(build
, node
);
4868 list
= generate_next_level(executed
, build
);
4870 list
= isl_ast_graft_list_unembed(list
, 1);
4874 isl_schedule_node_free(node
);
4875 isl_union_map_free(executed
);
4876 isl_ast_build_free(build
);
4880 /* Hoist a list of grafts (in practice containing a single graft)
4881 * from "sub_build" (which includes extra context information)
4884 * In particular, project out all additional parameters introduced
4885 * by the context node from the enforced constraints and the guard
4886 * of the single graft.
4888 static __isl_give isl_ast_graft_list
*hoist_out_of_context(
4889 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
,
4890 __isl_keep isl_ast_build
*sub_build
)
4892 isl_ast_graft
*graft
;
4893 isl_basic_set
*enforced
;
4895 unsigned n_param
, extra_param
;
4897 if (!build
|| !sub_build
)
4898 return isl_ast_graft_list_free(list
);
4900 n_param
= isl_ast_build_dim(build
, isl_dim_param
);
4901 extra_param
= isl_ast_build_dim(sub_build
, isl_dim_param
);
4903 if (extra_param
== n_param
)
4906 extra_param
-= n_param
;
4907 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, sub_build
);
4908 enforced
= isl_basic_set_project_out(enforced
, isl_dim_param
,
4909 n_param
, extra_param
);
4910 enforced
= isl_basic_set_remove_unknown_divs(enforced
);
4911 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
4912 guard
= isl_set_remove_divs_involving_dims(guard
, isl_dim_param
,
4913 n_param
, extra_param
);
4914 guard
= isl_set_project_out(guard
, isl_dim_param
, n_param
, extra_param
);
4915 guard
= isl_set_compute_divs(guard
);
4916 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
4918 list
= isl_ast_graft_list_from_ast_graft(graft
);
4923 /* Generate an AST that visits the elements in the domain of "executed"
4924 * in the relative order specified by the context node "node"
4925 * and its descendants.
4927 * The relation "executed" maps the outer generated loop iterators
4928 * to the domain elements executed by those iterations.
4930 * The context node may introduce additional parameters as well as
4931 * constraints on the outer schedule dimenions or original parameters.
4933 * We add the extra parameters to a new build and the context
4934 * constraints to both the build and (as a single disjunct)
4935 * to the domain of "executed". Since the context constraints
4936 * are specified in terms of the input schedule, we first need
4937 * to map them to the internal schedule domain.
4939 * After constructing the AST from the descendants of "node",
4940 * we combine the list of grafts into a single graft within
4941 * the new build, in order to be able to exploit the additional
4942 * context constraints during this combination.
4944 * Additionally, if the current node is the outermost node in
4945 * the schedule tree (apart from the root domain node), we generate
4946 * all pending guards, again to be able to exploit the additional
4947 * context constraints. We currently do not do this for internal
4948 * context nodes since we may still want to hoist conditions
4949 * to outer AST nodes.
4951 * If the context node introduced any new parameters, then they
4952 * are removed from the set of enforced constraints and guard
4953 * in hoist_out_of_context.
4955 static __isl_give isl_ast_graft_list
*build_ast_from_context(
4956 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
4957 __isl_take isl_union_map
*executed
)
4961 isl_multi_aff
*internal2input
;
4962 isl_ast_build
*sub_build
;
4963 isl_ast_graft_list
*list
;
4966 depth
= isl_schedule_node_get_tree_depth(node
);
4967 space
= isl_ast_build_get_space(build
, 1);
4968 context
= isl_schedule_node_context_get_context(node
);
4969 context
= isl_set_align_params(context
, space
);
4970 sub_build
= isl_ast_build_copy(build
);
4971 space
= isl_set_get_space(context
);
4972 sub_build
= isl_ast_build_align_params(sub_build
, space
);
4973 internal2input
= isl_ast_build_get_internal2input(sub_build
);
4974 context
= isl_set_preimage_multi_aff(context
, internal2input
);
4975 sub_build
= isl_ast_build_restrict_generated(sub_build
,
4976 isl_set_copy(context
));
4977 context
= isl_set_from_basic_set(isl_set_simple_hull(context
));
4978 executed
= isl_union_map_intersect_domain(executed
,
4979 isl_union_set_from_set(context
));
4981 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
4983 n
= isl_ast_graft_list_n_ast_graft(list
);
4985 list
= isl_ast_graft_list_free(list
);
4987 list
= isl_ast_graft_list_fuse(list
, sub_build
);
4989 list
= isl_ast_graft_list_insert_pending_guard_nodes(list
,
4992 list
= hoist_out_of_context(list
, build
, sub_build
);
4994 isl_ast_build_free(build
);
4995 isl_ast_build_free(sub_build
);
5000 /* Generate an AST that visits the elements in the domain of "executed"
5001 * in the relative order specified by the expansion node "node" and
5004 * The relation "executed" maps the outer generated loop iterators
5005 * to the domain elements executed by those iterations.
5007 * We expand the domain elements by the expansion and
5008 * continue with the descendants of the node.
5010 static __isl_give isl_ast_graft_list
*build_ast_from_expansion(
5011 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5012 __isl_take isl_union_map
*executed
)
5014 isl_union_map
*expansion
;
5017 expansion
= isl_schedule_node_expansion_get_expansion(node
);
5018 expansion
= isl_union_map_align_params(expansion
,
5019 isl_union_map_get_space(executed
));
5021 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5022 executed
= isl_union_map_apply_range(executed
, expansion
);
5023 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5025 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5026 "expansion node is not allowed to introduce "
5027 "new parameters", goto error
);
5029 return build_ast_from_child(build
, node
, executed
);
5031 isl_ast_build_free(build
);
5032 isl_schedule_node_free(node
);
5033 isl_union_map_free(executed
);
5037 /* Generate an AST that visits the elements in the domain of "executed"
5038 * in the relative order specified by the filter node "node" and
5041 * The relation "executed" maps the outer generated loop iterators
5042 * to the domain elements executed by those iterations.
5044 * We simply intersect the iteration domain (i.e., the range of "executed")
5045 * with the filter and continue with the descendants of the node,
5046 * unless the resulting inverse schedule is empty, in which
5047 * case we return an empty list.
5049 static __isl_give isl_ast_graft_list
*build_ast_from_filter(
5050 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5051 __isl_take isl_union_map
*executed
)
5054 isl_union_set
*filter
;
5055 isl_ast_graft_list
*list
;
5059 if (!build
|| !node
|| !executed
)
5062 filter
= isl_schedule_node_filter_get_filter(node
);
5063 filter
= isl_union_set_align_params(filter
,
5064 isl_union_map_get_space(executed
));
5065 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5066 executed
= isl_union_map_intersect_range(executed
, filter
);
5067 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5069 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5070 "filter node is not allowed to introduce "
5071 "new parameters", goto error
);
5073 empty
= isl_union_map_is_empty(executed
);
5077 return build_ast_from_child(build
, node
, executed
);
5079 ctx
= isl_ast_build_get_ctx(build
);
5080 list
= isl_ast_graft_list_alloc(ctx
, 0);
5081 isl_ast_build_free(build
);
5082 isl_schedule_node_free(node
);
5083 isl_union_map_free(executed
);
5086 isl_ast_build_free(build
);
5087 isl_schedule_node_free(node
);
5088 isl_union_map_free(executed
);
5092 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5093 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5094 __isl_take isl_union_map
*executed
);
5096 /* Generate an AST that visits the elements in the domain of "executed"
5097 * in the relative order specified by the sequence (or set) node "node" and
5100 * The relation "executed" maps the outer generated loop iterators
5101 * to the domain elements executed by those iterations.
5103 * We simply generate an AST for each of the children and concatenate
5106 static __isl_give isl_ast_graft_list
*build_ast_from_sequence(
5107 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5108 __isl_take isl_union_map
*executed
)
5112 isl_ast_graft_list
*list
;
5114 ctx
= isl_ast_build_get_ctx(build
);
5115 list
= isl_ast_graft_list_alloc(ctx
, 0);
5117 n
= isl_schedule_node_n_children(node
);
5118 for (i
= 0; i
< n
; ++i
) {
5119 isl_schedule_node
*child
;
5120 isl_ast_graft_list
*list_i
;
5122 child
= isl_schedule_node_get_child(node
, i
);
5123 list_i
= build_ast_from_schedule_node(isl_ast_build_copy(build
),
5124 child
, isl_union_map_copy(executed
));
5125 list
= isl_ast_graft_list_concat(list
, list_i
);
5127 isl_ast_build_free(build
);
5128 isl_schedule_node_free(node
);
5129 isl_union_map_free(executed
);
5134 /* Generate an AST that visits the elements in the domain of "executed"
5135 * in the relative order specified by the node "node" and its descendants.
5137 * The relation "executed" maps the outer generated loop iterators
5138 * to the domain elements executed by those iterations.
5140 * If the node is a leaf, then we pass control to generate_inner_level.
5141 * Note that the current build does not refer to any band node, so
5142 * that generate_inner_level will not try to visit the child of
5145 * The other node types are handled in separate functions.
5146 * Set nodes are currently treated in the same way as sequence nodes.
5147 * The children of a set node may be executed in any order,
5148 * including the order of the children.
5150 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5151 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5152 __isl_take isl_union_map
*executed
)
5154 enum isl_schedule_node_type type
;
5156 type
= isl_schedule_node_get_type(node
);
5159 case isl_schedule_node_error
:
5161 case isl_schedule_node_leaf
:
5162 isl_schedule_node_free(node
);
5163 return generate_inner_level(executed
, build
);
5164 case isl_schedule_node_band
:
5165 return build_ast_from_band(build
, node
, executed
);
5166 case isl_schedule_node_context
:
5167 return build_ast_from_context(build
, node
, executed
);
5168 case isl_schedule_node_domain
:
5169 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
5170 "unexpected internal domain node", goto error
);
5171 case isl_schedule_node_expansion
:
5172 return build_ast_from_expansion(build
, node
, executed
);
5173 case isl_schedule_node_filter
:
5174 return build_ast_from_filter(build
, node
, executed
);
5175 case isl_schedule_node_sequence
:
5176 case isl_schedule_node_set
:
5177 return build_ast_from_sequence(build
, node
, executed
);
5180 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
5181 "unhandled type", goto error
);
5183 isl_union_map_free(executed
);
5184 isl_schedule_node_free(node
);
5185 isl_ast_build_free(build
);
5190 /* Generate an AST that visits the elements in the domain of "executed"
5191 * in the relative order specified by the (single) child of "node" and
5194 * The relation "executed" maps the outer generated loop iterators
5195 * to the domain elements executed by those iterations.
5197 * This function is never called on a leaf, set or sequence node,
5198 * so the node always has exactly one child.
5200 static __isl_give isl_ast_graft_list
*build_ast_from_child(
5201 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5202 __isl_take isl_union_map
*executed
)
5204 node
= isl_schedule_node_child(node
, 0);
5205 return build_ast_from_schedule_node(build
, node
, executed
);
5208 /* Generate an AST that visits the elements in the domain of the domain
5209 * node "node" in the relative order specified by its descendants.
5211 * An initial inverse schedule is created that maps a zero-dimensional
5212 * schedule space to the node domain.
5213 * The input "build" is assumed to have a parametric domain and
5214 * is replaced by the same zero-dimensional schedule space.
5216 * We also add some of the parameter constraints in the build domain
5217 * to the executed relation. Adding these constraints
5218 * allows for an earlier detection of conflicts in some cases.
5219 * However, we do not want to divide the executed relation into
5220 * more disjuncts than necessary. We therefore approximate
5221 * the constraints on the parameters by a single disjunct set.
5223 static __isl_give isl_ast_node
*build_ast_from_domain(
5224 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
)
5227 isl_union_set
*domain
, *schedule_domain
;
5228 isl_union_map
*executed
;
5231 isl_ast_graft_list
*list
;
5238 ctx
= isl_ast_build_get_ctx(build
);
5239 space
= isl_ast_build_get_space(build
, 1);
5240 is_params
= isl_space_is_params(space
);
5241 isl_space_free(space
);
5245 isl_die(ctx
, isl_error_unsupported
,
5246 "expecting parametric initial context", goto error
);
5248 domain
= isl_schedule_node_domain_get_domain(node
);
5249 domain
= isl_union_set_coalesce(domain
);
5251 space
= isl_union_set_get_space(domain
);
5252 space
= isl_space_set_from_params(space
);
5253 build
= isl_ast_build_product(build
, space
);
5255 set
= isl_ast_build_get_domain(build
);
5256 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5257 schedule_domain
= isl_union_set_from_set(set
);
5259 executed
= isl_union_map_from_domain_and_range(schedule_domain
, domain
);
5260 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5261 ast
= isl_ast_node_from_graft_list(list
, build
);
5262 isl_ast_build_free(build
);
5266 isl_schedule_node_free(node
);
5267 isl_ast_build_free(build
);
5271 /* Generate an AST that visits the elements in the domain of "schedule"
5272 * in the relative order specified by the schedule tree.
5274 * "build" is an isl_ast_build that has been created using
5275 * isl_ast_build_alloc or isl_ast_build_from_context based
5276 * on a parametric set.
5278 * The construction starts at the root node of the schedule,
5279 * which is assumed to be a domain node.
5281 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule(
5282 __isl_keep isl_ast_build
*build
, __isl_take isl_schedule
*schedule
)
5285 isl_schedule_node
*node
;
5287 if (!build
|| !schedule
)
5290 ctx
= isl_ast_build_get_ctx(build
);
5292 node
= isl_schedule_get_root(schedule
);
5293 isl_schedule_free(schedule
);
5295 build
= isl_ast_build_copy(build
);
5296 build
= isl_ast_build_set_single_valued(build
, 0);
5297 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
5298 isl_die(ctx
, isl_error_unsupported
,
5299 "expecting root domain node",
5300 build
= isl_ast_build_free(build
));
5301 return build_ast_from_domain(build
, node
);
5303 isl_schedule_free(schedule
);