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
15 #include <isl/constraint.h>
18 #include <isl/union_set.h>
19 #include <isl/union_map.h>
20 #include <isl/schedule_node.h>
22 #include <isl_tarjan.h>
23 #include <isl_ast_private.h>
24 #include <isl_ast_build_expr.h>
25 #include <isl_ast_build_private.h>
26 #include <isl_ast_graft_private.h>
28 /* Data used in generate_domain.
30 * "build" is the input build.
31 * "list" collects the results.
33 struct isl_generate_domain_data
{
36 isl_ast_graft_list
*list
;
39 static __isl_give isl_ast_graft_list
*generate_next_level(
40 __isl_take isl_union_map
*executed
,
41 __isl_take isl_ast_build
*build
);
42 static __isl_give isl_ast_graft_list
*generate_code(
43 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
46 /* Generate an AST for a single domain based on
47 * the (non single valued) inverse schedule "executed".
49 * We extend the schedule with the iteration domain
50 * and continue generating through a call to generate_code.
52 * In particular, if executed has the form
56 * then we continue generating code on
60 * The extended inverse schedule is clearly single valued
61 * ensuring that the nested generate_code will not reach this function,
62 * but will instead create calls to all elements of D that need
63 * to be executed from the current schedule domain.
65 static isl_stat
generate_non_single_valued(__isl_take isl_map
*executed
,
66 struct isl_generate_domain_data
*data
)
70 isl_ast_graft_list
*list
;
72 build
= isl_ast_build_copy(data
->build
);
74 identity
= isl_set_identity(isl_map_range(isl_map_copy(executed
)));
75 executed
= isl_map_domain_product(executed
, identity
);
76 build
= isl_ast_build_set_single_valued(build
, 1);
78 list
= generate_code(isl_union_map_from_map(executed
), build
, 1);
80 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
85 /* Call the at_each_domain callback, if requested by the user,
86 * after recording the current inverse schedule in the build.
88 static __isl_give isl_ast_graft
*at_each_domain(__isl_take isl_ast_graft
*graft
,
89 __isl_keep isl_map
*executed
, __isl_keep isl_ast_build
*build
)
92 return isl_ast_graft_free(graft
);
93 if (!build
->at_each_domain
)
96 build
= isl_ast_build_copy(build
);
97 build
= isl_ast_build_set_executed(build
,
98 isl_union_map_from_map(isl_map_copy(executed
)));
100 return isl_ast_graft_free(graft
);
102 graft
->node
= build
->at_each_domain(graft
->node
,
103 build
, build
->at_each_domain_user
);
104 isl_ast_build_free(build
);
107 graft
= isl_ast_graft_free(graft
);
112 /* Generate an AST for a single domain based on
113 * the inverse schedule "executed" and add it to data->list.
115 * If there is more than one domain element associated to the current
116 * schedule "time", then we need to continue the generation process
117 * in generate_non_single_valued.
118 * Note that the inverse schedule being single-valued may depend
119 * on constraints that are only available in the original context
120 * domain specified by the user. We therefore first introduce
121 * some of the constraints of data->build->domain. In particular,
122 * we intersect with a single-disjunct approximation of this set.
123 * We perform this approximation to avoid further splitting up
124 * the executed relation, possibly introducing a disjunctive guard
127 * On the other hand, we only perform the test after having taken the gist
128 * of the domain as the resulting map is the one from which the call
129 * expression is constructed. Using this map to construct the call
130 * expression usually yields simpler results.
131 * Because we perform the single-valuedness test on the gisted map,
132 * we may in rare cases fail to recognize that the inverse schedule
133 * is single-valued. This becomes problematic if this happens
134 * from the recursive call through generate_non_single_valued
135 * as we would then end up in an infinite recursion.
136 * We therefore check if we are inside a call to generate_non_single_valued
137 * and revert to the ungisted map if the gisted map turns out not to be
140 * Otherwise, we generate a call expression for the single executed
141 * domain element and put a guard around it based on the (simplified)
142 * domain of "executed".
144 * At this stage, any pending constraints in the build can no longer
145 * be simplified with respect to any enforced constraints since
146 * the call node does not have any enforced constraints.
147 * We therefore turn all pending constraints into guards
148 * (after simplifying them with respect to the already generated
149 * constraints) and add them to both the generated constraints
150 * and the guard of the constructed graft. This guard will ensure
151 * that the constraints are effectively generated.
153 * If the user has set an at_each_domain callback, it is called
154 * on the constructed call expression node.
156 static isl_stat
generate_domain(__isl_take isl_map
*executed
, void *user
)
158 struct isl_generate_domain_data
*data
= user
;
159 isl_ast_build
*build
;
160 isl_ast_graft
*graft
;
161 isl_ast_graft_list
*list
;
162 isl_set
*guard
, *domain
;
166 domain
= isl_ast_build_get_domain(data
->build
);
167 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
168 executed
= isl_map_intersect_domain(executed
, domain
);
169 empty
= isl_map_is_empty(executed
);
173 isl_map_free(executed
);
177 executed
= isl_map_coalesce(executed
);
178 map
= isl_map_copy(executed
);
179 map
= isl_ast_build_compute_gist_map_domain(data
->build
, map
);
180 sv
= isl_map_is_single_valued(map
);
185 if (data
->build
->single_valued
)
186 map
= isl_map_copy(executed
);
188 return generate_non_single_valued(executed
, data
);
190 guard
= isl_map_domain(isl_map_copy(map
));
191 guard
= isl_set_compute_divs(guard
);
192 guard
= isl_set_intersect(guard
,
193 isl_ast_build_get_pending(data
->build
));
194 guard
= isl_set_coalesce(guard
);
195 guard
= isl_ast_build_specialize(data
->build
, guard
);
196 guard
= isl_set_gist(guard
, isl_ast_build_get_generated(data
->build
));
198 build
= isl_ast_build_copy(data
->build
);
199 build
= isl_ast_build_replace_pending_by_guard(build
,
200 isl_set_copy(guard
));
201 graft
= isl_ast_graft_alloc_domain(map
, build
);
202 graft
= at_each_domain(graft
, executed
, build
);
203 isl_ast_build_free(build
);
204 isl_map_free(executed
);
205 graft
= isl_ast_graft_add_guard(graft
, guard
, data
->build
);
207 list
= isl_ast_graft_list_from_ast_graft(graft
);
208 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
213 isl_map_free(executed
);
214 return isl_stat_error
;
217 /* Call build->create_leaf to a create "leaf" node in the AST,
218 * encapsulate the result in an isl_ast_graft and return the result
219 * as a 1-element list.
221 * Note that the node returned by the user may be an entire tree.
223 * Since the node itself cannot enforce any constraints, we turn
224 * all pending constraints into guards and add them to the resulting
225 * graft to ensure that they will be generated.
227 * Before we pass control to the user, we first clear some information
228 * from the build that is (presumbably) only meaningful
229 * for the current code generation.
230 * This includes the create_leaf callback itself, so we make a copy
231 * of the build first.
233 static __isl_give isl_ast_graft_list
*call_create_leaf(
234 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
238 isl_ast_graft
*graft
;
239 isl_ast_build
*user_build
;
241 guard
= isl_ast_build_get_pending(build
);
242 user_build
= isl_ast_build_copy(build
);
243 user_build
= isl_ast_build_replace_pending_by_guard(user_build
,
244 isl_set_copy(guard
));
245 user_build
= isl_ast_build_set_executed(user_build
, executed
);
246 user_build
= isl_ast_build_clear_local_info(user_build
);
250 node
= build
->create_leaf(user_build
, build
->create_leaf_user
);
251 graft
= isl_ast_graft_alloc(node
, build
);
252 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
253 isl_ast_build_free(build
);
254 return isl_ast_graft_list_from_ast_graft(graft
);
257 static __isl_give isl_ast_graft_list
*build_ast_from_child(
258 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
259 __isl_take isl_union_map
*executed
);
261 /* Generate an AST after having handled the complete schedule
262 * of this call to the code generator or the complete band
263 * if we are generating an AST from a schedule tree.
265 * If we are inside a band node, then move on to the child of the band.
267 * If the user has specified a create_leaf callback, control
268 * is passed to the user in call_create_leaf.
270 * Otherwise, we generate one or more calls for each individual
271 * domain in generate_domain.
273 static __isl_give isl_ast_graft_list
*generate_inner_level(
274 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
277 struct isl_generate_domain_data data
= { build
};
279 if (!build
|| !executed
)
282 if (isl_ast_build_has_schedule_node(build
)) {
283 isl_schedule_node
*node
;
284 node
= isl_ast_build_get_schedule_node(build
);
285 build
= isl_ast_build_reset_schedule_node(build
);
286 return build_ast_from_child(build
, node
, executed
);
289 if (build
->create_leaf
)
290 return call_create_leaf(executed
, build
);
292 ctx
= isl_union_map_get_ctx(executed
);
293 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
294 if (isl_union_map_foreach_map(executed
, &generate_domain
, &data
) < 0)
295 data
.list
= isl_ast_graft_list_free(data
.list
);
298 error
: data
.list
= NULL
;
299 isl_ast_build_free(build
);
300 isl_union_map_free(executed
);
304 /* Call the before_each_for callback, if requested by the user.
306 static __isl_give isl_ast_node
*before_each_for(__isl_take isl_ast_node
*node
,
307 __isl_keep isl_ast_build
*build
)
312 return isl_ast_node_free(node
);
313 if (!build
->before_each_for
)
315 id
= build
->before_each_for(build
, build
->before_each_for_user
);
316 node
= isl_ast_node_set_annotation(node
, id
);
320 /* Call the after_each_for callback, if requested by the user.
322 static __isl_give isl_ast_graft
*after_each_for(__isl_take isl_ast_graft
*graft
,
323 __isl_keep isl_ast_build
*build
)
325 if (!graft
|| !build
)
326 return isl_ast_graft_free(graft
);
327 if (!build
->after_each_for
)
329 graft
->node
= build
->after_each_for(graft
->node
, build
,
330 build
->after_each_for_user
);
332 return isl_ast_graft_free(graft
);
336 /* Plug in all the know values of the current and outer dimensions
337 * in the domain of "executed". In principle, we only need to plug
338 * in the known value of the current dimension since the values of
339 * outer dimensions have been plugged in already.
340 * However, it turns out to be easier to just plug in all known values.
342 static __isl_give isl_union_map
*plug_in_values(
343 __isl_take isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
345 return isl_ast_build_substitute_values_union_map_domain(build
,
349 /* Check if the constraint "c" is a lower bound on dimension "pos",
350 * an upper bound, or independent of dimension "pos".
352 static int constraint_type(isl_constraint
*c
, int pos
)
354 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
356 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
361 /* Compare the types of the constraints "a" and "b",
362 * resulting in constraints that are independent of "depth"
363 * to be sorted before the lower bounds on "depth", which in
364 * turn are sorted before the upper bounds on "depth".
366 static int cmp_constraint(__isl_keep isl_constraint
*a
,
367 __isl_keep isl_constraint
*b
, void *user
)
370 int t1
= constraint_type(a
, *depth
);
371 int t2
= constraint_type(b
, *depth
);
376 /* Extract a lower bound on dimension "pos" from constraint "c".
378 * If the constraint is of the form
382 * then we essentially return
384 * l = ceil(-f(...)/a)
386 * However, if the current dimension is strided, then we need to make
387 * sure that the lower bound we construct is of the form
391 * with f the offset and s the stride.
392 * We therefore compute
394 * f + s * ceil((l - f)/s)
396 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
397 int pos
, __isl_keep isl_ast_build
*build
)
401 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
402 aff
= isl_aff_ceil(aff
);
404 if (isl_ast_build_has_stride(build
, pos
)) {
408 offset
= isl_ast_build_get_offset(build
, pos
);
409 stride
= isl_ast_build_get_stride(build
, pos
);
411 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
412 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(stride
));
413 aff
= isl_aff_ceil(aff
);
414 aff
= isl_aff_scale_val(aff
, stride
);
415 aff
= isl_aff_add(aff
, offset
);
418 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
423 /* Return the exact lower bound (or upper bound if "upper" is set)
424 * of "domain" as a piecewise affine expression.
426 * If we are computing a lower bound (of a strided dimension), then
427 * we need to make sure it is of the form
431 * where f is the offset and s is the stride.
432 * We therefore need to include the stride constraint before computing
435 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
436 __isl_keep isl_ast_build
*build
, int upper
)
441 isl_pw_multi_aff
*pma
;
443 domain
= isl_set_copy(domain
);
445 stride
= isl_ast_build_get_stride_constraint(build
);
446 domain
= isl_set_intersect(domain
, stride
);
448 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
450 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
452 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
453 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
454 isl_pw_multi_aff_free(pma
);
455 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
456 pa
= isl_pw_aff_coalesce(pa
);
461 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
462 * remove_redundant_lower_bounds.
464 static int reduce_list_cmp(__isl_keep isl_pw_aff
*a
, __isl_keep isl_pw_aff
*b
,
467 return isl_pw_aff_plain_cmp(a
, b
);
470 /* Given a list of lower bounds "list", remove those that are redundant
471 * with respect to the other bounds in "list" and the domain of "build".
473 * We first sort the bounds in the same way as they would be sorted
474 * by set_for_node_expressions so that we can try and remove the last
477 * For a lower bound to be effective, there needs to be at least
478 * one domain element for which it is larger than all other lower bounds.
479 * For each lower bound we therefore intersect the domain with
480 * the conditions that it is larger than all other bounds and
481 * check whether the result is empty. If so, the bound can be removed.
483 static __isl_give isl_pw_aff_list
*remove_redundant_lower_bounds(
484 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
489 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
493 n
= isl_pw_aff_list_n_pw_aff(list
);
497 domain
= isl_ast_build_get_domain(build
);
499 for (i
= n
- 1; i
>= 0; --i
) {
504 domain_i
= isl_set_copy(domain
);
505 pa_i
= isl_pw_aff_list_get_pw_aff(list
, i
);
507 for (j
= 0; j
< n
; ++j
) {
514 pa_j
= isl_pw_aff_list_get_pw_aff(list
, j
);
515 better
= isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i
), pa_j
);
516 domain_i
= isl_set_intersect(domain_i
, better
);
519 empty
= isl_set_is_empty(domain_i
);
521 isl_set_free(domain_i
);
522 isl_pw_aff_free(pa_i
);
528 list
= isl_pw_aff_list_drop(list
, i
, 1);
532 isl_set_free(domain
);
536 isl_set_free(domain
);
537 return isl_pw_aff_list_free(list
);
540 /* Extract a lower bound on dimension "pos" from each constraint
541 * in "constraints" and return the list of lower bounds.
542 * If "constraints" has zero elements, then we extract a lower bound
543 * from "domain" instead.
545 * If the current dimension is strided, then the lower bound
546 * is adjusted by lower_bound to match the stride information.
547 * This modification may make one or more lower bounds redundant
548 * with respect to the other lower bounds. We therefore check
549 * for this condition and remove the redundant lower bounds.
551 static __isl_give isl_pw_aff_list
*lower_bounds(
552 __isl_keep isl_constraint_list
*constraints
, int pos
,
553 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
556 isl_pw_aff_list
*list
;
562 n
= isl_constraint_list_n_constraint(constraints
);
565 pa
= exact_bound(domain
, build
, 0);
566 return isl_pw_aff_list_from_pw_aff(pa
);
569 ctx
= isl_ast_build_get_ctx(build
);
570 list
= isl_pw_aff_list_alloc(ctx
,n
);
572 for (i
= 0; i
< n
; ++i
) {
576 c
= isl_constraint_list_get_constraint(constraints
, i
);
577 aff
= lower_bound(c
, pos
, build
);
578 isl_constraint_free(c
);
579 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
582 if (isl_ast_build_has_stride(build
, pos
))
583 list
= remove_redundant_lower_bounds(list
, build
);
588 /* Extract an upper bound on dimension "pos" from each constraint
589 * in "constraints" and return the list of upper bounds.
590 * If "constraints" has zero elements, then we extract an upper bound
591 * from "domain" instead.
593 static __isl_give isl_pw_aff_list
*upper_bounds(
594 __isl_keep isl_constraint_list
*constraints
, int pos
,
595 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
598 isl_pw_aff_list
*list
;
601 n
= isl_constraint_list_n_constraint(constraints
);
604 pa
= exact_bound(domain
, build
, 1);
605 return isl_pw_aff_list_from_pw_aff(pa
);
608 ctx
= isl_ast_build_get_ctx(build
);
609 list
= isl_pw_aff_list_alloc(ctx
,n
);
611 for (i
= 0; i
< n
; ++i
) {
615 c
= isl_constraint_list_get_constraint(constraints
, i
);
616 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
617 isl_constraint_free(c
);
618 aff
= isl_aff_floor(aff
);
619 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
625 /* Return an isl_ast_expr that performs the reduction of type "type"
626 * on AST expressions corresponding to the elements in "list".
628 * The list is assumed to contain at least one element.
629 * If the list contains exactly one element, then the returned isl_ast_expr
630 * simply computes that affine expression.
631 * If the list contains more than one element, then we sort it
632 * using a fairly abitrary but hopefully reasonably stable order.
634 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
635 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
644 n
= isl_pw_aff_list_n_pw_aff(list
);
647 return isl_ast_build_expr_from_pw_aff_internal(build
,
648 isl_pw_aff_list_get_pw_aff(list
, 0));
650 ctx
= isl_pw_aff_list_get_ctx(list
);
651 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
655 list
= isl_pw_aff_list_copy(list
);
656 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
658 return isl_ast_expr_free(expr
);
660 for (i
= 0; i
< n
; ++i
) {
661 isl_ast_expr
*expr_i
;
663 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
664 isl_pw_aff_list_get_pw_aff(list
, i
));
667 expr
->u
.op
.args
[i
] = expr_i
;
670 isl_pw_aff_list_free(list
);
673 isl_pw_aff_list_free(list
);
674 isl_ast_expr_free(expr
);
678 /* Add guards implied by the "generated constraints",
679 * but not (necessarily) enforced by the generated AST to "guard".
680 * In particular, if there is any stride constraints,
681 * then add the guard implied by those constraints.
682 * If we have generated a degenerate loop, then add the guard
683 * implied by "bounds" on the outer dimensions, i.e., the guard
684 * that ensures that the single value actually exists.
685 * Since there may also be guards implied by a combination
686 * of these constraints, we first combine them before
687 * deriving the implied constraints.
689 static __isl_give isl_set
*add_implied_guards(__isl_take isl_set
*guard
,
690 int degenerate
, __isl_keep isl_basic_set
*bounds
,
691 __isl_keep isl_ast_build
*build
)
693 int depth
, has_stride
;
697 depth
= isl_ast_build_get_depth(build
);
698 has_stride
= isl_ast_build_has_stride(build
, depth
);
699 if (!has_stride
&& !degenerate
)
702 space
= isl_basic_set_get_space(bounds
);
703 dom
= isl_set_universe(space
);
706 bounds
= isl_basic_set_copy(bounds
);
707 bounds
= isl_basic_set_drop_constraints_not_involving_dims(
708 bounds
, isl_dim_set
, depth
, 1);
709 set
= isl_set_from_basic_set(bounds
);
710 dom
= isl_set_intersect(dom
, set
);
714 set
= isl_ast_build_get_stride_constraint(build
);
715 dom
= isl_set_intersect(dom
, set
);
718 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
719 dom
= isl_ast_build_compute_gist(build
, dom
);
720 guard
= isl_set_intersect(guard
, dom
);
725 /* Update "graft" based on "sub_build" for the degenerate case.
727 * "build" is the build in which graft->node was created
728 * "sub_build" contains information about the current level itself,
729 * including the single value attained.
731 * We set the initialization part of the for loop to the single
732 * value attained by the current dimension.
733 * The increment and condition are not strictly needed as the are known
734 * to be "1" and "iterator <= value" respectively.
736 static __isl_give isl_ast_graft
*refine_degenerate(
737 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
,
738 __isl_keep isl_ast_build
*sub_build
)
742 if (!graft
|| !sub_build
)
743 return isl_ast_graft_free(graft
);
745 value
= isl_pw_aff_copy(sub_build
->value
);
747 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
749 if (!graft
->node
->u
.f
.init
)
750 return isl_ast_graft_free(graft
);
755 /* Return the intersection of constraints in "list" as a set.
757 static __isl_give isl_set
*intersect_constraints(
758 __isl_keep isl_constraint_list
*list
)
763 n
= isl_constraint_list_n_constraint(list
);
765 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
766 "expecting at least one constraint", return NULL
);
768 bset
= isl_basic_set_from_constraint(
769 isl_constraint_list_get_constraint(list
, 0));
770 for (i
= 1; i
< n
; ++i
) {
771 isl_basic_set
*bset_i
;
773 bset_i
= isl_basic_set_from_constraint(
774 isl_constraint_list_get_constraint(list
, i
));
775 bset
= isl_basic_set_intersect(bset
, bset_i
);
778 return isl_set_from_basic_set(bset
);
781 /* Compute the constraints on the outer dimensions enforced by
782 * graft->node and add those constraints to graft->enforced,
783 * in case the upper bound is expressed as a set "upper".
785 * In particular, if l(...) is a lower bound in "lower", and
787 * -a i + f(...) >= 0 or a i <= f(...)
789 * is an upper bound ocnstraint on the current dimension i,
790 * then the for loop enforces the constraint
792 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
794 * We therefore simply take each lower bound in turn, plug it into
795 * the upper bounds and compute the intersection over all lower bounds.
797 * If a lower bound is a rational expression, then
798 * isl_basic_set_preimage_multi_aff will force this rational
799 * expression to have only integer values. However, the loop
800 * itself does not enforce this integrality constraint. We therefore
801 * use the ceil of the lower bounds instead of the lower bounds themselves.
802 * Other constraints will make sure that the for loop is only executed
803 * when each of the lower bounds attains an integral value.
804 * In particular, potentially rational values only occur in
805 * lower_bound if the offset is a (seemingly) rational expression,
806 * but then outer conditions will make sure that this rational expression
807 * only attains integer values.
809 static __isl_give isl_ast_graft
*set_enforced_from_set(
810 __isl_take isl_ast_graft
*graft
,
811 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
814 isl_basic_set
*enforced
;
815 isl_pw_multi_aff
*pma
;
818 if (!graft
|| !lower
)
819 return isl_ast_graft_free(graft
);
821 space
= isl_set_get_space(upper
);
822 enforced
= isl_basic_set_universe(isl_space_copy(space
));
824 space
= isl_space_map_from_set(space
);
825 pma
= isl_pw_multi_aff_identity(space
);
827 n
= isl_pw_aff_list_n_pw_aff(lower
);
828 for (i
= 0; i
< n
; ++i
) {
832 isl_pw_multi_aff
*pma_i
;
834 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
835 pa
= isl_pw_aff_ceil(pa
);
836 pma_i
= isl_pw_multi_aff_copy(pma
);
837 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
838 enforced_i
= isl_set_copy(upper
);
839 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
840 hull
= isl_set_simple_hull(enforced_i
);
841 enforced
= isl_basic_set_intersect(enforced
, hull
);
844 isl_pw_multi_aff_free(pma
);
846 graft
= isl_ast_graft_enforce(graft
, enforced
);
851 /* Compute the constraints on the outer dimensions enforced by
852 * graft->node and add those constraints to graft->enforced,
853 * in case the upper bound is expressed as
854 * a list of affine expressions "upper".
856 * The enforced condition is that each lower bound expression is less
857 * than or equal to each upper bound expression.
859 static __isl_give isl_ast_graft
*set_enforced_from_list(
860 __isl_take isl_ast_graft
*graft
,
861 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
864 isl_basic_set
*enforced
;
866 lower
= isl_pw_aff_list_copy(lower
);
867 upper
= isl_pw_aff_list_copy(upper
);
868 cond
= isl_pw_aff_list_le_set(lower
, upper
);
869 enforced
= isl_set_simple_hull(cond
);
870 graft
= isl_ast_graft_enforce(graft
, enforced
);
875 /* Does "aff" have a negative constant term?
877 static isl_stat
aff_constant_is_negative(__isl_take isl_set
*set
,
878 __isl_take isl_aff
*aff
, void *user
)
883 v
= isl_aff_get_constant_val(aff
);
884 *neg
= isl_val_is_neg(v
);
889 return *neg
? isl_stat_ok
: isl_stat_error
;
892 /* Does "pa" have a negative constant term over its entire domain?
894 static isl_stat
pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
,
900 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
903 return (*neg
&& r
>= 0) ? isl_stat_ok
: isl_stat_error
;
906 /* Does each element in "list" have a negative constant term?
908 * The callback terminates the iteration as soon an element has been
909 * found that does not have a negative constant term.
911 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
915 if (isl_pw_aff_list_foreach(list
,
916 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
922 /* Add 1 to each of the elements in "list", where each of these elements
923 * is defined over the internal schedule space of "build".
925 static __isl_give isl_pw_aff_list
*list_add_one(
926 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
933 space
= isl_ast_build_get_space(build
, 1);
934 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
935 aff
= isl_aff_add_constant_si(aff
, 1);
936 one
= isl_pw_aff_from_aff(aff
);
938 n
= isl_pw_aff_list_n_pw_aff(list
);
939 for (i
= 0; i
< n
; ++i
) {
941 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
942 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
943 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
946 isl_pw_aff_free(one
);
951 /* Set the condition part of the for node graft->node in case
952 * the upper bound is represented as a list of piecewise affine expressions.
954 * In particular, set the condition to
956 * iterator <= min(list of upper bounds)
958 * If each of the upper bounds has a negative constant term, then
959 * set the condition to
961 * iterator < min(list of (upper bound + 1)s)
964 static __isl_give isl_ast_graft
*set_for_cond_from_list(
965 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
966 __isl_keep isl_ast_build
*build
)
969 isl_ast_expr
*bound
, *iterator
, *cond
;
970 enum isl_ast_op_type type
= isl_ast_op_le
;
973 return isl_ast_graft_free(graft
);
975 neg
= list_constant_is_negative(list
);
977 return isl_ast_graft_free(graft
);
978 list
= isl_pw_aff_list_copy(list
);
980 list
= list_add_one(list
, build
);
981 type
= isl_ast_op_lt
;
984 bound
= reduce_list(isl_ast_op_min
, list
, build
);
985 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
986 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
987 graft
->node
->u
.f
.cond
= cond
;
989 isl_pw_aff_list_free(list
);
990 if (!graft
->node
->u
.f
.cond
)
991 return isl_ast_graft_free(graft
);
995 /* Set the condition part of the for node graft->node in case
996 * the upper bound is represented as a set.
998 static __isl_give isl_ast_graft
*set_for_cond_from_set(
999 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
1000 __isl_keep isl_ast_build
*build
)
1007 cond
= isl_ast_build_expr_from_set_internal(build
, isl_set_copy(set
));
1008 graft
->node
->u
.f
.cond
= cond
;
1009 if (!graft
->node
->u
.f
.cond
)
1010 return isl_ast_graft_free(graft
);
1014 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1015 * the current dimension.
1017 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
1025 ctx
= isl_ast_build_get_ctx(build
);
1026 depth
= isl_ast_build_get_depth(build
);
1028 if (!isl_ast_build_has_stride(build
, depth
))
1029 return isl_ast_expr_alloc_int_si(ctx
, 1);
1031 v
= isl_ast_build_get_stride(build
, depth
);
1032 return isl_ast_expr_from_val(v
);
1035 /* Should we express the loop condition as
1037 * iterator <= min(list of upper bounds)
1039 * or as a conjunction of constraints?
1041 * The first is constructed from a list of upper bounds.
1042 * The second is constructed from a set.
1044 * If there are no upper bounds in "constraints", then this could mean
1045 * that "domain" simply doesn't have an upper bound or that we didn't
1046 * pick any upper bound. In the first case, we want to generate the
1047 * loop condition as a(n empty) conjunction of constraints
1048 * In the second case, we will compute
1049 * a single upper bound from "domain" and so we use the list form.
1051 * If there are upper bounds in "constraints",
1052 * then we use the list form iff the atomic_upper_bound option is set.
1054 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
1055 __isl_keep isl_set
*domain
, int depth
)
1058 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
1060 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
1063 /* Fill in the expressions of the for node in graft->node.
1066 * - set the initialization part of the loop to the maximum of the lower bounds
1067 * - extract the increment from the stride of the current dimension
1068 * - construct the for condition either based on a list of upper bounds
1069 * or on a set of upper bound constraints.
1071 static __isl_give isl_ast_graft
*set_for_node_expressions(
1072 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
1073 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
1074 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
1081 build
= isl_ast_build_copy(build
);
1084 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
1085 node
->u
.f
.inc
= for_inc(build
);
1088 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
1090 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
1092 isl_ast_build_free(build
);
1094 if (!node
->u
.f
.iterator
|| !node
->u
.f
.init
||
1095 !node
->u
.f
.cond
|| !node
->u
.f
.inc
)
1096 return isl_ast_graft_free(graft
);
1101 /* Update "graft" based on "bounds" and "domain" for the generic,
1102 * non-degenerate, case.
1104 * "c_lower" and "c_upper" contain the lower and upper bounds
1105 * that the loop node should express.
1106 * "domain" is the subset of the intersection of the constraints
1107 * for which some code is executed.
1109 * There may be zero lower bounds or zero upper bounds in "constraints"
1110 * in case the list of constraints was created
1111 * based on the atomic option or based on separation with explicit bounds.
1112 * In that case, we use "domain" to derive lower and/or upper bounds.
1114 * We first compute a list of one or more lower bounds.
1116 * Then we decide if we want to express the condition as
1118 * iterator <= min(list of upper bounds)
1120 * or as a conjunction of constraints.
1122 * The set of enforced constraints is then computed either based on
1123 * a list of upper bounds or on a set of upper bound constraints.
1124 * We do not compute any enforced constraints if we were forced
1125 * to compute a lower or upper bound using exact_bound. The domains
1126 * of the resulting expressions may imply some bounds on outer dimensions
1127 * that we do not want to appear in the enforced constraints since
1128 * they are not actually enforced by the corresponding code.
1130 * Finally, we fill in the expressions of the for node.
1132 static __isl_give isl_ast_graft
*refine_generic_bounds(
1133 __isl_take isl_ast_graft
*graft
,
1134 __isl_take isl_constraint_list
*c_lower
,
1135 __isl_take isl_constraint_list
*c_upper
,
1136 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1140 isl_pw_aff_list
*lower
;
1142 isl_set
*upper_set
= NULL
;
1143 isl_pw_aff_list
*upper_list
= NULL
;
1144 int n_lower
, n_upper
;
1146 if (!graft
|| !c_lower
|| !c_upper
|| !build
)
1149 depth
= isl_ast_build_get_depth(build
);
1150 ctx
= isl_ast_graft_get_ctx(graft
);
1152 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1153 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1155 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1157 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1160 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1161 else if (n_upper
> 0)
1162 upper_set
= intersect_constraints(c_upper
);
1164 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1166 if (n_lower
== 0 || n_upper
== 0)
1169 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1171 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1173 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1176 isl_pw_aff_list_free(lower
);
1177 isl_pw_aff_list_free(upper_list
);
1178 isl_set_free(upper_set
);
1179 isl_constraint_list_free(c_lower
);
1180 isl_constraint_list_free(c_upper
);
1184 isl_constraint_list_free(c_lower
);
1185 isl_constraint_list_free(c_upper
);
1186 return isl_ast_graft_free(graft
);
1189 /* Internal data structure used inside count_constraints to keep
1190 * track of the number of constraints that are independent of dimension "pos",
1191 * the lower bounds in "pos" and the upper bounds in "pos".
1193 struct isl_ast_count_constraints_data
{
1201 /* Increment data->n_indep, data->lower or data->upper depending
1202 * on whether "c" is independenct of dimensions data->pos,
1203 * a lower bound or an upper bound.
1205 static isl_stat
count_constraints(__isl_take isl_constraint
*c
, void *user
)
1207 struct isl_ast_count_constraints_data
*data
= user
;
1209 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1211 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1216 isl_constraint_free(c
);
1221 /* Update "graft" based on "bounds" and "domain" for the generic,
1222 * non-degenerate, case.
1224 * "list" respresent the list of bounds that need to be encoded by
1225 * the for loop. Only the constraints that involve the iterator
1226 * are relevant here. The other constraints are taken care of by
1227 * the caller and are included in the generated constraints of "build".
1228 * "domain" is the subset of the intersection of the constraints
1229 * for which some code is executed.
1230 * "build" is the build in which graft->node was created.
1232 * We separate lower bounds, upper bounds and constraints that
1233 * are independent of the loop iterator.
1235 * The actual for loop bounds are generated in refine_generic_bounds.
1237 static __isl_give isl_ast_graft
*refine_generic_split(
1238 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1239 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1241 struct isl_ast_count_constraints_data data
;
1242 isl_constraint_list
*lower
;
1243 isl_constraint_list
*upper
;
1246 return isl_ast_graft_free(graft
);
1248 data
.pos
= isl_ast_build_get_depth(build
);
1250 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1252 return isl_ast_graft_free(graft
);
1254 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1255 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1256 isl_constraint_list_free(list
);
1257 return isl_ast_graft_free(graft
);
1260 lower
= isl_constraint_list_drop(list
, 0, data
.n_indep
);
1261 upper
= isl_constraint_list_copy(lower
);
1262 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1263 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1265 return refine_generic_bounds(graft
, lower
, upper
, domain
, build
);
1268 /* Update "graft" based on "bounds" and "domain" for the generic,
1269 * non-degenerate, case.
1271 * "bounds" respresent the bounds that need to be encoded by
1272 * the for loop (or a guard around the for loop).
1273 * "domain" is the subset of "bounds" for which some code is executed.
1274 * "build" is the build in which graft->node was created.
1276 * We break up "bounds" into a list of constraints and continue with
1277 * refine_generic_split.
1279 static __isl_give isl_ast_graft
*refine_generic(
1280 __isl_take isl_ast_graft
*graft
,
1281 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1282 __isl_keep isl_ast_build
*build
)
1284 isl_constraint_list
*list
;
1286 if (!build
|| !graft
)
1287 return isl_ast_graft_free(graft
);
1289 list
= isl_basic_set_get_constraint_list(bounds
);
1291 graft
= refine_generic_split(graft
, list
, domain
, build
);
1296 /* Create a for node for the current level.
1298 * Mark the for node degenerate if "degenerate" is set.
1300 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1310 depth
= isl_ast_build_get_depth(build
);
1311 id
= isl_ast_build_get_iterator_id(build
, depth
);
1312 node
= isl_ast_node_alloc_for(id
);
1314 node
= isl_ast_node_for_mark_degenerate(node
);
1319 /* If the ast_build_exploit_nested_bounds option is set, then return
1320 * the constraints enforced by all elements in "list".
1321 * Otherwise, return the universe.
1323 static __isl_give isl_basic_set
*extract_shared_enforced(
1324 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1332 ctx
= isl_ast_graft_list_get_ctx(list
);
1333 if (isl_options_get_ast_build_exploit_nested_bounds(ctx
))
1334 return isl_ast_graft_list_extract_shared_enforced(list
, build
);
1336 space
= isl_ast_build_get_space(build
, 1);
1337 return isl_basic_set_universe(space
);
1340 /* Return the pending constraints of "build" that are not already taken
1341 * care of (by a combination of "enforced" and the generated constraints
1344 static __isl_give isl_set
*extract_pending(__isl_keep isl_ast_build
*build
,
1345 __isl_keep isl_basic_set
*enforced
)
1347 isl_set
*guard
, *context
;
1349 guard
= isl_ast_build_get_pending(build
);
1350 context
= isl_set_from_basic_set(isl_basic_set_copy(enforced
));
1351 context
= isl_set_intersect(context
,
1352 isl_ast_build_get_generated(build
));
1353 return isl_set_gist(guard
, context
);
1356 /* Create an AST node for the current dimension based on
1357 * the schedule domain "bounds" and return the node encapsulated
1358 * in an isl_ast_graft.
1360 * "executed" is the current inverse schedule, taking into account
1361 * the bounds in "bounds"
1362 * "domain" is the domain of "executed", with inner dimensions projected out.
1363 * It may be a strict subset of "bounds" in case "bounds" was created
1364 * based on the atomic option or based on separation with explicit bounds.
1366 * "domain" may satisfy additional equalities that result
1367 * from intersecting "executed" with "bounds" in add_node.
1368 * It may also satisfy some global constraints that were dropped out because
1369 * we performed separation with explicit bounds.
1370 * The very first step is then to copy these constraints to "bounds".
1372 * Since we may be calling before_each_for and after_each_for
1373 * callbacks, we record the current inverse schedule in the build.
1375 * We consider three builds,
1376 * "build" is the one in which the current level is created,
1377 * "body_build" is the build in which the next level is created,
1378 * "sub_build" is essentially the same as "body_build", except that
1379 * the depth has not been increased yet.
1381 * "build" already contains information (in strides and offsets)
1382 * about the strides at the current level, but this information is not
1383 * reflected in the build->domain.
1384 * We first add this information and the "bounds" to the sub_build->domain.
1385 * isl_ast_build_set_loop_bounds adds the stride information and
1386 * checks whether the current dimension attains
1387 * only a single value and whether this single value can be represented using
1388 * a single affine expression.
1389 * In the first case, the current level is considered "degenerate".
1390 * In the second, sub-case, the current level is considered "eliminated".
1391 * Eliminated levels don't need to be reflected in the AST since we can
1392 * simply plug in the affine expression. For degenerate, but non-eliminated,
1393 * levels, we do introduce a for node, but mark is as degenerate so that
1394 * it can be printed as an assignment of the single value to the loop
1397 * If the current level is eliminated, we explicitly plug in the value
1398 * for the current level found by isl_ast_build_set_loop_bounds in the
1399 * inverse schedule. This ensures that if we are working on a slice
1400 * of the domain based on information available in the inverse schedule
1401 * and the build domain, that then this information is also reflected
1402 * in the inverse schedule. This operation also eliminates the current
1403 * dimension from the inverse schedule making sure no inner dimensions depend
1404 * on the current dimension. Otherwise, we create a for node, marking
1405 * it degenerate if appropriate. The initial for node is still incomplete
1406 * and will be completed in either refine_degenerate or refine_generic.
1408 * We then generate a sequence of grafts for the next level,
1409 * create a surrounding graft for the current level and insert
1410 * the for node we created (if the current level is not eliminated).
1411 * Before creating a graft for the current level, we first extract
1412 * hoistable constraints from the child guards and combine them
1413 * with the pending constraints in the build. These constraints
1414 * are used to simplify the child guards and then added to the guard
1415 * of the current graft to ensure that they will be generated.
1416 * If the hoisted guard is a disjunction, then we use it directly
1417 * to gist the guards on the children before intersect it with the
1418 * pending constraints. We do so because this disjunction is typically
1419 * identical to the guards on the children such that these guards
1420 * can be effectively removed completely. After the intersection,
1421 * the gist operation would have a harder time figuring this out.
1423 * Finally, we set the bounds of the for loop in either
1424 * refine_degenerate or refine_generic.
1425 * We do so in a context where the pending constraints of the build
1426 * have been replaced by the guard of the current graft.
1428 static __isl_give isl_ast_graft
*create_node_scaled(
1429 __isl_take isl_union_map
*executed
,
1430 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1431 __isl_take isl_ast_build
*build
)
1434 int degenerate
, eliminated
;
1435 isl_basic_set
*hull
;
1436 isl_basic_set
*enforced
;
1437 isl_set
*guard
, *hoisted
;
1438 isl_ast_node
*node
= NULL
;
1439 isl_ast_graft
*graft
;
1440 isl_ast_graft_list
*children
;
1441 isl_ast_build
*sub_build
;
1442 isl_ast_build
*body_build
;
1444 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1445 domain
= isl_set_detect_equalities(domain
);
1446 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1447 bounds
= isl_basic_set_intersect(bounds
, hull
);
1448 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1450 depth
= isl_ast_build_get_depth(build
);
1451 sub_build
= isl_ast_build_copy(build
);
1452 bounds
= isl_basic_set_remove_redundancies(bounds
);
1453 bounds
= isl_ast_build_specialize_basic_set(sub_build
, bounds
);
1454 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1455 isl_basic_set_copy(bounds
));
1456 sub_build
= isl_ast_build_set_pending_generated(sub_build
,
1457 isl_basic_set_copy(bounds
));
1458 degenerate
= isl_ast_build_has_value(sub_build
);
1459 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1460 if (degenerate
< 0 || eliminated
< 0)
1461 executed
= isl_union_map_free(executed
);
1463 executed
= plug_in_values(executed
, sub_build
);
1465 node
= create_for(build
, degenerate
);
1467 body_build
= isl_ast_build_copy(sub_build
);
1468 body_build
= isl_ast_build_increase_depth(body_build
);
1470 node
= before_each_for(node
, body_build
);
1471 children
= generate_next_level(executed
,
1472 isl_ast_build_copy(body_build
));
1474 enforced
= extract_shared_enforced(children
, build
);
1475 guard
= extract_pending(sub_build
, enforced
);
1476 hoisted
= isl_ast_graft_list_extract_hoistable_guard(children
, build
);
1477 if (isl_set_n_basic_set(hoisted
) > 1)
1478 children
= isl_ast_graft_list_gist_guards(children
,
1479 isl_set_copy(hoisted
));
1480 guard
= isl_set_intersect(guard
, hoisted
);
1482 guard
= add_implied_guards(guard
, degenerate
, bounds
, build
);
1484 graft
= isl_ast_graft_alloc_from_children(children
,
1485 isl_set_copy(guard
), enforced
, build
, sub_build
);
1488 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1490 isl_ast_build
*for_build
;
1492 graft
= isl_ast_graft_insert_for(graft
, node
);
1493 for_build
= isl_ast_build_copy(build
);
1494 for_build
= isl_ast_build_replace_pending_by_guard(for_build
,
1495 isl_set_copy(guard
));
1497 graft
= refine_degenerate(graft
, for_build
, sub_build
);
1499 graft
= refine_generic(graft
, bounds
,
1501 isl_ast_build_free(for_build
);
1503 isl_set_free(guard
);
1505 graft
= after_each_for(graft
, body_build
);
1507 isl_ast_build_free(body_build
);
1508 isl_ast_build_free(sub_build
);
1509 isl_ast_build_free(build
);
1510 isl_basic_set_free(bounds
);
1511 isl_set_free(domain
);
1516 /* Internal data structure for checking if all constraints involving
1517 * the input dimension "depth" are such that the other coefficients
1518 * are multiples of "m", reducing "m" if they are not.
1519 * If "m" is reduced all the way down to "1", then the check has failed
1520 * and we break out of the iteration.
1522 struct isl_check_scaled_data
{
1527 /* If constraint "c" involves the input dimension data->depth,
1528 * then make sure that all the other coefficients are multiples of data->m,
1529 * reducing data->m if needed.
1530 * Break out of the iteration if data->m has become equal to "1".
1532 static isl_stat
constraint_check_scaled(__isl_take isl_constraint
*c
,
1535 struct isl_check_scaled_data
*data
= user
;
1537 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1540 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1541 isl_constraint_free(c
);
1545 for (i
= 0; i
< 4; ++i
) {
1546 n
= isl_constraint_dim(c
, t
[i
]);
1547 for (j
= 0; j
< n
; ++j
) {
1550 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1552 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1554 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1555 data
->m
= isl_val_gcd(data
->m
, d
);
1556 if (isl_val_is_one(data
->m
))
1563 isl_constraint_free(c
);
1565 return i
< 4 ? isl_stat_error
: isl_stat_ok
;
1568 /* For each constraint of "bmap" that involves the input dimension data->depth,
1569 * make sure that all the other coefficients are multiples of data->m,
1570 * reducing data->m if needed.
1571 * Break out of the iteration if data->m has become equal to "1".
1573 static isl_stat
basic_map_check_scaled(__isl_take isl_basic_map
*bmap
,
1578 r
= isl_basic_map_foreach_constraint(bmap
,
1579 &constraint_check_scaled
, user
);
1580 isl_basic_map_free(bmap
);
1585 /* For each constraint of "map" that involves the input dimension data->depth,
1586 * make sure that all the other coefficients are multiples of data->m,
1587 * reducing data->m if needed.
1588 * Break out of the iteration if data->m has become equal to "1".
1590 static isl_stat
map_check_scaled(__isl_take isl_map
*map
, void *user
)
1594 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1600 /* Create an AST node for the current dimension based on
1601 * the schedule domain "bounds" and return the node encapsulated
1602 * in an isl_ast_graft.
1604 * "executed" is the current inverse schedule, taking into account
1605 * the bounds in "bounds"
1606 * "domain" is the domain of "executed", with inner dimensions projected out.
1609 * Before moving on to the actual AST node construction in create_node_scaled,
1610 * we first check if the current dimension is strided and if we can scale
1611 * down this stride. Note that we only do this if the ast_build_scale_strides
1614 * In particular, let the current dimension take on values
1618 * with a an integer. We check if we can find an integer m that (obviously)
1619 * divides both f and s.
1621 * If so, we check if the current dimension only appears in constraints
1622 * where the coefficients of the other variables are multiples of m.
1623 * We perform this extra check to avoid the risk of introducing
1624 * divisions by scaling down the current dimension.
1626 * If so, we scale the current dimension down by a factor of m.
1627 * That is, we plug in
1631 * Note that in principle we could always scale down strided loops
1636 * but this may result in i' taking on larger values than the original i,
1637 * due to the shift by "f".
1638 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1640 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1641 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1642 __isl_take isl_ast_build
*build
)
1644 struct isl_check_scaled_data data
;
1649 ctx
= isl_ast_build_get_ctx(build
);
1650 if (!isl_options_get_ast_build_scale_strides(ctx
))
1651 return create_node_scaled(executed
, bounds
, domain
, build
);
1653 data
.depth
= isl_ast_build_get_depth(build
);
1654 if (!isl_ast_build_has_stride(build
, data
.depth
))
1655 return create_node_scaled(executed
, bounds
, domain
, build
);
1657 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1658 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1660 offset
= isl_aff_free(offset
);
1661 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1662 d
= isl_aff_get_denominator_val(offset
);
1664 executed
= isl_union_map_free(executed
);
1666 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1667 data
.m
= isl_val_div(data
.m
, d
);
1669 data
.m
= isl_val_set_si(data
.m
, 1);
1673 if (!isl_val_is_one(data
.m
)) {
1674 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1676 !isl_val_is_one(data
.m
))
1677 executed
= isl_union_map_free(executed
);
1680 if (!isl_val_is_one(data
.m
)) {
1685 isl_union_map
*umap
;
1687 space
= isl_ast_build_get_space(build
, 1);
1688 space
= isl_space_map_from_set(space
);
1689 ma
= isl_multi_aff_identity(space
);
1690 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1691 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1692 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1694 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1695 isl_multi_aff_copy(ma
));
1696 domain
= isl_set_preimage_multi_aff(domain
,
1697 isl_multi_aff_copy(ma
));
1698 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1699 umap
= isl_union_map_from_map(map
);
1700 executed
= isl_union_map_apply_domain(executed
,
1701 isl_union_map_copy(umap
));
1702 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1705 isl_aff_free(offset
);
1706 isl_val_free(data
.m
);
1708 return create_node_scaled(executed
, bounds
, domain
, build
);
1711 /* Add the basic set to the list that "user" points to.
1713 static isl_stat
collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1715 isl_basic_set_list
**list
= user
;
1717 *list
= isl_basic_set_list_add(*list
, bset
);
1722 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1724 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1725 __isl_take isl_set
*set
)
1729 isl_basic_set_list
*list
;
1734 ctx
= isl_set_get_ctx(set
);
1736 n
= isl_set_n_basic_set(set
);
1737 list
= isl_basic_set_list_alloc(ctx
, n
);
1738 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1739 list
= isl_basic_set_list_free(list
);
1745 /* Generate code for the schedule domain "bounds"
1746 * and add the result to "list".
1748 * We mainly detect strides here and check if the bounds do not
1749 * conflict with the current build domain
1750 * and then pass over control to create_node.
1752 * "bounds" reflects the bounds on the current dimension and possibly
1753 * some extra conditions on outer dimensions.
1754 * It does not, however, include any divs involving the current dimension,
1755 * so it does not capture any stride constraints.
1756 * We therefore need to compute that part of the schedule domain that
1757 * intersects with "bounds" and derive the strides from the result.
1759 static __isl_give isl_ast_graft_list
*add_node(
1760 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1761 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1763 isl_ast_graft
*graft
;
1764 isl_set
*domain
= NULL
;
1765 isl_union_set
*uset
;
1766 int empty
, disjoint
;
1768 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1769 executed
= isl_union_map_intersect_domain(executed
, uset
);
1770 empty
= isl_union_map_is_empty(executed
);
1776 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1777 domain
= isl_set_from_union_set(uset
);
1778 domain
= isl_ast_build_specialize(build
, domain
);
1780 domain
= isl_set_compute_divs(domain
);
1781 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1782 disjoint
= isl_set_is_disjoint(domain
, build
->domain
);
1788 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1790 graft
= create_node(executed
, bounds
, domain
,
1791 isl_ast_build_copy(build
));
1792 list
= isl_ast_graft_list_add(list
, graft
);
1793 isl_ast_build_free(build
);
1796 list
= isl_ast_graft_list_free(list
);
1798 isl_set_free(domain
);
1799 isl_basic_set_free(bounds
);
1800 isl_union_map_free(executed
);
1801 isl_ast_build_free(build
);
1805 /* Does any element of i follow or coincide with any element of j
1806 * at the current depth for equal values of the outer dimensions?
1808 static isl_bool
domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1809 __isl_keep isl_basic_set
*j
, void *user
)
1811 int depth
= *(int *) user
;
1812 isl_basic_map
*test
;
1816 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1817 isl_basic_set_copy(j
));
1818 for (l
= 0; l
< depth
; ++l
)
1819 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1821 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1822 isl_dim_out
, depth
);
1823 empty
= isl_basic_map_is_empty(test
);
1824 isl_basic_map_free(test
);
1826 return empty
< 0 ? isl_bool_error
: !empty
;
1829 /* Split up each element of "list" into a part that is related to "bset"
1830 * according to "gt" and a part that is not.
1831 * Return a list that consist of "bset" and all the pieces.
1833 static __isl_give isl_basic_set_list
*add_split_on(
1834 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1835 __isl_keep isl_basic_map
*gt
)
1838 isl_basic_set_list
*res
;
1841 bset
= isl_basic_set_free(bset
);
1843 gt
= isl_basic_map_copy(gt
);
1844 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1845 n
= isl_basic_set_list_n_basic_set(list
);
1846 res
= isl_basic_set_list_from_basic_set(bset
);
1847 for (i
= 0; res
&& i
< n
; ++i
) {
1848 isl_basic_set
*bset
;
1849 isl_set
*set1
, *set2
;
1850 isl_basic_map
*bmap
;
1853 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1854 bmap
= isl_basic_map_copy(gt
);
1855 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1856 bset
= isl_basic_map_range(bmap
);
1857 empty
= isl_basic_set_is_empty(bset
);
1859 res
= isl_basic_set_list_free(res
);
1861 isl_basic_set_free(bset
);
1862 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1863 res
= isl_basic_set_list_add(res
, bset
);
1867 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1868 set1
= isl_set_from_basic_set(bset
);
1869 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1870 set2
= isl_set_from_basic_set(bset
);
1871 set1
= isl_set_subtract(set2
, set1
);
1872 set1
= isl_set_make_disjoint(set1
);
1874 res
= isl_basic_set_list_concat(res
,
1875 isl_basic_set_list_from_set(set1
));
1877 isl_basic_map_free(gt
);
1878 isl_basic_set_list_free(list
);
1882 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1883 __isl_keep isl_basic_set_list
*domain_list
,
1884 __isl_keep isl_union_map
*executed
,
1885 __isl_keep isl_ast_build
*build
);
1887 /* Internal data structure for add_nodes.
1889 * "executed" and "build" are extra arguments to be passed to add_node.
1890 * "list" collects the results.
1892 struct isl_add_nodes_data
{
1893 isl_union_map
*executed
;
1894 isl_ast_build
*build
;
1896 isl_ast_graft_list
*list
;
1899 /* Generate code for the schedule domains in "scc"
1900 * and add the results to "list".
1902 * The domains in "scc" form a strongly connected component in the ordering.
1903 * If the number of domains in "scc" is larger than 1, then this means
1904 * that we cannot determine a valid ordering for the domains in the component.
1905 * This should be fairly rare because the individual domains
1906 * have been made disjoint first.
1907 * The problem is that the domains may be integrally disjoint but not
1908 * rationally disjoint. For example, we may have domains
1910 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1912 * These two domains have an empty intersection, but their rational
1913 * relaxations do intersect. It is impossible to order these domains
1914 * in the second dimension because the first should be ordered before
1915 * the second for outer dimension equal to 0, while it should be ordered
1916 * after for outer dimension equal to 1.
1918 * This may happen in particular in case of unrolling since the domain
1919 * of each slice is replaced by its simple hull.
1921 * For each basic set i in "scc" and for each of the following basic sets j,
1922 * we split off that part of the basic set i that shares the outer dimensions
1923 * with j and lies before j in the current dimension.
1924 * We collect all the pieces in a new list that replaces "scc".
1926 * While the elements in "scc" should be disjoint, we double-check
1927 * this property to avoid running into an infinite recursion in case
1928 * they intersect due to some internal error.
1930 static isl_stat
add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1932 struct isl_add_nodes_data
*data
= user
;
1934 isl_basic_set
*bset
, *first
;
1935 isl_basic_set_list
*list
;
1939 n
= isl_basic_set_list_n_basic_set(scc
);
1940 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
1942 isl_basic_set_list_free(scc
);
1943 data
->list
= add_node(data
->list
,
1944 isl_union_map_copy(data
->executed
), bset
,
1945 isl_ast_build_copy(data
->build
));
1946 return data
->list
? isl_stat_ok
: isl_stat_error
;
1949 depth
= isl_ast_build_get_depth(data
->build
);
1950 space
= isl_basic_set_get_space(bset
);
1951 space
= isl_space_map_from_set(space
);
1952 gt
= isl_basic_map_universe(space
);
1953 for (i
= 0; i
< depth
; ++i
)
1954 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
1955 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
1957 first
= isl_basic_set_copy(bset
);
1958 list
= isl_basic_set_list_from_basic_set(bset
);
1959 for (i
= 1; i
< n
; ++i
) {
1962 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
1964 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
1966 list
= isl_basic_set_list_free(list
);
1968 isl_die(isl_basic_set_list_get_ctx(scc
),
1970 "basic sets in scc are assumed to be disjoint",
1971 list
= isl_basic_set_list_free(list
));
1973 list
= add_split_on(list
, bset
, gt
);
1975 isl_basic_set_free(first
);
1976 isl_basic_map_free(gt
);
1977 isl_basic_set_list_free(scc
);
1979 data
->list
= isl_ast_graft_list_concat(data
->list
,
1980 generate_sorted_domains(scc
, data
->executed
, data
->build
));
1981 isl_basic_set_list_free(scc
);
1983 return data
->list
? isl_stat_ok
: isl_stat_error
;
1986 /* Sort the domains in "domain_list" according to the execution order
1987 * at the current depth (for equal values of the outer dimensions),
1988 * generate code for each of them, collecting the results in a list.
1989 * If no code is generated (because the intersection of the inverse schedule
1990 * with the domains turns out to be empty), then an empty list is returned.
1992 * The caller is responsible for ensuring that the basic sets in "domain_list"
1993 * are pair-wise disjoint. It can, however, in principle happen that
1994 * two basic sets should be ordered one way for one value of the outer
1995 * dimensions and the other way for some other value of the outer dimensions.
1996 * We therefore play safe and look for strongly connected components.
1997 * The function add_nodes takes care of handling non-trivial components.
1999 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
2000 __isl_keep isl_basic_set_list
*domain_list
,
2001 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2004 struct isl_add_nodes_data data
;
2011 ctx
= isl_basic_set_list_get_ctx(domain_list
);
2012 n
= isl_basic_set_list_n_basic_set(domain_list
);
2013 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
2017 return add_node(data
.list
, isl_union_map_copy(executed
),
2018 isl_basic_set_list_get_basic_set(domain_list
, 0),
2019 isl_ast_build_copy(build
));
2021 depth
= isl_ast_build_get_depth(build
);
2022 data
.executed
= executed
;
2024 if (isl_basic_set_list_foreach_scc(domain_list
,
2025 &domain_follows_at_depth
, &depth
,
2026 &add_nodes
, &data
) < 0)
2027 data
.list
= isl_ast_graft_list_free(data
.list
);
2032 /* Do i and j share any values for the outer dimensions?
2034 static isl_bool
shared_outer(__isl_keep isl_basic_set
*i
,
2035 __isl_keep isl_basic_set
*j
, void *user
)
2037 int depth
= *(int *) user
;
2038 isl_basic_map
*test
;
2042 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
2043 isl_basic_set_copy(j
));
2044 for (l
= 0; l
< depth
; ++l
)
2045 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
2047 empty
= isl_basic_map_is_empty(test
);
2048 isl_basic_map_free(test
);
2050 return empty
< 0 ? isl_bool_error
: !empty
;
2053 /* Internal data structure for generate_sorted_domains_wrap.
2055 * "n" is the total number of basic sets
2056 * "executed" and "build" are extra arguments to be passed
2057 * to generate_sorted_domains.
2059 * "single" is set to 1 by generate_sorted_domains_wrap if there
2060 * is only a single component.
2061 * "list" collects the results.
2063 struct isl_ast_generate_parallel_domains_data
{
2065 isl_union_map
*executed
;
2066 isl_ast_build
*build
;
2069 isl_ast_graft_list
*list
;
2072 /* Call generate_sorted_domains on "scc", fuse the result into a list
2073 * with either zero or one graft and collect the these single element
2074 * lists into data->list.
2076 * If there is only one component, i.e., if the number of basic sets
2077 * in the current component is equal to the total number of basic sets,
2078 * then data->single is set to 1 and the result of generate_sorted_domains
2081 static isl_stat
generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
2084 struct isl_ast_generate_parallel_domains_data
*data
= user
;
2085 isl_ast_graft_list
*list
;
2087 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
2088 data
->single
= isl_basic_set_list_n_basic_set(scc
) == data
->n
;
2090 list
= isl_ast_graft_list_fuse(list
, data
->build
);
2094 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
2096 isl_basic_set_list_free(scc
);
2098 return isl_stat_error
;
2103 /* Look for any (weakly connected) components in the "domain_list"
2104 * of domains that share some values of the outer dimensions.
2105 * That is, domains in different components do not share any values
2106 * of the outer dimensions. This means that these components
2107 * can be freely reordered.
2108 * Within each of the components, we sort the domains according
2109 * to the execution order at the current depth.
2111 * If there is more than one component, then generate_sorted_domains_wrap
2112 * fuses the result of each call to generate_sorted_domains
2113 * into a list with either zero or one graft and collects these (at most)
2114 * single element lists into a bigger list. This means that the elements of the
2115 * final list can be freely reordered. In particular, we sort them
2116 * according to an arbitrary but fixed ordering to ease merging of
2117 * graft lists from different components.
2119 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2120 __isl_keep isl_basic_set_list
*domain_list
,
2121 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2124 struct isl_ast_generate_parallel_domains_data data
;
2129 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2131 return generate_sorted_domains(domain_list
, executed
, build
);
2133 depth
= isl_ast_build_get_depth(build
);
2135 data
.executed
= executed
;
2138 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2139 &generate_sorted_domains_wrap
,
2141 data
.list
= isl_ast_graft_list_free(data
.list
);
2144 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2149 /* Internal data for separate_domain.
2151 * "explicit" is set if we only want to use explicit bounds.
2153 * "domain" collects the separated domains.
2155 struct isl_separate_domain_data
{
2156 isl_ast_build
*build
;
2161 /* Extract implicit bounds on the current dimension for the executed "map".
2163 * The domain of "map" may involve inner dimensions, so we
2164 * need to eliminate them.
2166 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2167 __isl_keep isl_ast_build
*build
)
2171 domain
= isl_map_domain(map
);
2172 domain
= isl_ast_build_eliminate(build
, domain
);
2177 /* Extract explicit bounds on the current dimension for the executed "map".
2179 * Rather than eliminating the inner dimensions as in implicit_bounds,
2180 * we simply drop any constraints involving those inner dimensions.
2181 * The idea is that most bounds that are implied by constraints on the
2182 * inner dimensions will be enforced by for loops and not by explicit guards.
2183 * There is then no need to separate along those bounds.
2185 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2186 __isl_keep isl_ast_build
*build
)
2191 dim
= isl_map_dim(map
, isl_dim_out
);
2192 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2194 domain
= isl_map_domain(map
);
2195 depth
= isl_ast_build_get_depth(build
);
2196 dim
= isl_set_dim(domain
, isl_dim_set
);
2197 domain
= isl_set_detect_equalities(domain
);
2198 domain
= isl_set_drop_constraints_involving_dims(domain
,
2199 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2200 domain
= isl_set_remove_divs_involving_dims(domain
,
2201 isl_dim_set
, depth
, 1);
2202 domain
= isl_set_remove_unknown_divs(domain
);
2207 /* Split data->domain into pieces that intersect with the range of "map"
2208 * and pieces that do not intersect with the range of "map"
2209 * and then add that part of the range of "map" that does not intersect
2210 * with data->domain.
2212 static isl_stat
separate_domain(__isl_take isl_map
*map
, void *user
)
2214 struct isl_separate_domain_data
*data
= user
;
2219 domain
= explicit_bounds(map
, data
->build
);
2221 domain
= implicit_bounds(map
, data
->build
);
2223 domain
= isl_set_coalesce(domain
);
2224 domain
= isl_set_make_disjoint(domain
);
2225 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2226 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2227 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2228 data
->domain
= isl_set_union(data
->domain
, d1
);
2229 data
->domain
= isl_set_union(data
->domain
, d2
);
2234 /* Separate the schedule domains of "executed".
2236 * That is, break up the domain of "executed" into basic sets,
2237 * such that for each basic set S, every element in S is associated with
2238 * the same domain spaces.
2240 * "space" is the (single) domain space of "executed".
2242 static __isl_give isl_set
*separate_schedule_domains(
2243 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2244 __isl_keep isl_ast_build
*build
)
2246 struct isl_separate_domain_data data
= { build
};
2249 ctx
= isl_ast_build_get_ctx(build
);
2250 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2251 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2252 data
.domain
= isl_set_empty(space
);
2253 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2254 data
.domain
= isl_set_free(data
.domain
);
2256 isl_union_map_free(executed
);
2260 /* Temporary data used during the search for a lower bound for unrolling.
2262 * "build" is the build in which the unrolling will be performed
2263 * "domain" is the original set for which to find a lower bound
2264 * "depth" is the dimension for which to find a lower boudn
2265 * "expansion" is the expansion that needs to be applied to "domain"
2266 * in the unrolling that will be performed
2268 * "lower" is the best lower bound found so far. It is NULL if we have not
2270 * "n" is the corresponding size. If lower is NULL, then the value of n
2272 * "n_div" is the maximal number of integer divisions in the first
2273 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2274 * been computed yet.
2276 struct isl_find_unroll_data
{
2277 isl_ast_build
*build
;
2280 isl_basic_map
*expansion
;
2287 /* Return the constraint
2289 * i_"depth" = aff + offset
2291 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2294 aff
= isl_aff_copy(aff
);
2295 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2296 aff
= isl_aff_add_constant_si(aff
, offset
);
2297 return isl_equality_from_aff(aff
);
2300 /* Update *user to the number of integer divsions in the first element
2301 * of "ma", if it is larger than the current value.
2303 static isl_stat
update_n_div(__isl_take isl_set
*set
,
2304 __isl_take isl_multi_aff
*ma
, void *user
)
2310 aff
= isl_multi_aff_get_aff(ma
, 0);
2311 n_div
= isl_aff_dim(aff
, isl_dim_div
);
2313 isl_multi_aff_free(ma
);
2319 return aff
? isl_stat_ok
: isl_stat_error
;
2322 /* Get the number of integer divisions in the expression for the iterator
2323 * value at the first slice in the unrolling based on lower bound "lower",
2324 * taking into account the expansion that needs to be performed on this slice.
2326 static int get_expanded_n_div(struct isl_find_unroll_data
*data
,
2327 __isl_keep isl_aff
*lower
)
2331 isl_map
*it_map
, *expansion
;
2332 isl_pw_multi_aff
*pma
;
2335 c
= at_offset(data
->depth
, lower
, 0);
2336 set
= isl_set_copy(data
->domain
);
2337 set
= isl_set_add_constraint(set
, c
);
2338 expansion
= isl_map_from_basic_map(isl_basic_map_copy(data
->expansion
));
2339 set
= isl_set_apply(set
, expansion
);
2340 it_map
= isl_ast_build_map_to_iterator(data
->build
, set
);
2341 pma
= isl_pw_multi_aff_from_map(it_map
);
2343 if (isl_pw_multi_aff_foreach_piece(pma
, &update_n_div
, &n
) < 0)
2345 isl_pw_multi_aff_free(pma
);
2350 /* Is the lower bound "lower" with corresponding iteration count "n"
2351 * better than the one stored in "data"?
2352 * If there is no upper bound on the iteration count ("n" is infinity) or
2353 * if the count is too large, then we cannot use this lower bound.
2354 * Otherwise, if there was no previous lower bound or
2355 * if the iteration count of the new lower bound is smaller than
2356 * the iteration count of the previous lower bound, then we consider
2357 * the new lower bound to be better.
2358 * If the iteration count is the same, then compare the number
2359 * of integer divisions that would be needed to express
2360 * the iterator value at the first slice in the unrolling
2361 * according to the lower bound. If we end up computing this
2362 * number, then store the lowest value in data->n_div.
2364 static int is_better_lower_bound(struct isl_find_unroll_data
*data
,
2365 __isl_keep isl_aff
*lower
, __isl_keep isl_val
*n
)
2372 if (isl_val_is_infty(n
))
2374 if (isl_val_cmp_si(n
, INT_MAX
) > 0)
2378 cmp
= isl_val_cmp_si(n
, *data
->n
);
2383 if (data
->n_div
< 0)
2384 data
->n_div
= get_expanded_n_div(data
, data
->lower
);
2385 if (data
->n_div
< 0)
2387 if (data
->n_div
== 0)
2389 n_div
= get_expanded_n_div(data
, lower
);
2392 if (n_div
>= data
->n_div
)
2394 data
->n_div
= n_div
;
2399 /* Check if we can use "c" as a lower bound and if it is better than
2400 * any previously found lower bound.
2402 * If "c" does not involve the dimension at the current depth,
2403 * then we cannot use it.
2404 * Otherwise, let "c" be of the form
2408 * We compute the maximal value of
2410 * -ceil(f(j)/a)) + i + 1
2412 * over the domain. If there is such a value "n", then we know
2414 * -ceil(f(j)/a)) + i + 1 <= n
2418 * i < ceil(f(j)/a)) + n
2420 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2421 * We just need to check if we have found any lower bound before and
2422 * if the new lower bound is better (smaller n or fewer integer divisions)
2423 * than the previously found lower bounds.
2425 static isl_stat
update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2426 __isl_keep isl_constraint
*c
)
2428 isl_aff
*aff
, *lower
;
2432 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2435 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2436 lower
= isl_aff_ceil(lower
);
2437 aff
= isl_aff_copy(lower
);
2438 aff
= isl_aff_neg(aff
);
2439 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2440 aff
= isl_aff_add_constant_si(aff
, 1);
2441 max
= isl_set_max_val(data
->domain
, aff
);
2444 better
= is_better_lower_bound(data
, lower
, max
);
2445 if (better
< 0 || !better
) {
2447 isl_aff_free(lower
);
2448 return better
< 0 ? isl_stat_error
: isl_stat_ok
;
2451 isl_aff_free(data
->lower
);
2452 data
->lower
= lower
;
2453 *data
->n
= isl_val_get_num_si(max
);
2459 /* Check if we can use "c" as a lower bound and if it is better than
2460 * any previously found lower bound.
2462 static isl_stat
constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2464 struct isl_find_unroll_data
*data
;
2467 data
= (struct isl_find_unroll_data
*) user
;
2468 r
= update_unrolling_lower_bound(data
, c
);
2469 isl_constraint_free(c
);
2474 /* Look for a lower bound l(i) on the dimension at "depth"
2475 * and a size n such that "domain" is a subset of
2477 * { [i] : l(i) <= i_d < l(i) + n }
2479 * where d is "depth" and l(i) depends only on earlier dimensions.
2480 * Furthermore, try and find a lower bound such that n is as small as possible.
2481 * In particular, "n" needs to be finite.
2482 * "build" is the build in which the unrolling will be performed.
2483 * "expansion" is the expansion that needs to be applied to "domain"
2484 * in the unrolling that will be performed.
2486 * Inner dimensions have been eliminated from "domain" by the caller.
2488 * We first construct a collection of lower bounds on the input set
2489 * by computing its simple hull. We then iterate through them,
2490 * discarding those that we cannot use (either because they do not
2491 * involve the dimension at "depth" or because they have no corresponding
2492 * upper bound, meaning that "n" would be unbounded) and pick out the
2493 * best from the remaining ones.
2495 * If we cannot find a suitable lower bound, then we consider that
2498 static __isl_give isl_aff
*find_unroll_lower_bound(
2499 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*domain
,
2500 int depth
, __isl_keep isl_basic_map
*expansion
, int *n
)
2502 struct isl_find_unroll_data data
=
2503 { build
, domain
, depth
, expansion
, NULL
, n
, -1 };
2504 isl_basic_set
*hull
;
2506 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2508 if (isl_basic_set_foreach_constraint(hull
,
2509 &constraint_find_unroll
, &data
) < 0)
2512 isl_basic_set_free(hull
);
2515 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2516 "cannot find lower bound for unrolling", return NULL
);
2520 isl_basic_set_free(hull
);
2521 return isl_aff_free(data
.lower
);
2524 /* Call "fn" on each iteration of the current dimension of "domain".
2525 * If "init" is not NULL, then it is called with the number of
2526 * iterations before any call to "fn".
2527 * Return -1 on failure.
2529 * Since we are going to be iterating over the individual values,
2530 * we first check if there are any strides on the current dimension.
2531 * If there is, we rewrite the current dimension i as
2533 * i = stride i' + offset
2535 * and then iterate over individual values of i' instead.
2537 * We then look for a lower bound on i' and a size such that the domain
2540 * { [j,i'] : l(j) <= i' < l(j) + n }
2542 * and then take slices of the domain at values of i'
2543 * between l(j) and l(j) + n - 1.
2545 * We compute the unshifted simple hull of each slice to ensure that
2546 * we have a single basic set per offset. The slicing constraint
2547 * may get simplified away before the unshifted simple hull is taken
2548 * and may therefore in some rare cases disappear from the result.
2549 * We therefore explicitly add the constraint back after computing
2550 * the unshifted simple hull to ensure that the basic sets
2551 * remain disjoint. The constraints that are dropped by taking the hull
2552 * will be taken into account at the next level, as in the case of the
2555 * Finally, we map i' back to i and call "fn".
2557 static int foreach_iteration(__isl_take isl_set
*domain
,
2558 __isl_keep isl_ast_build
*build
, int (*init
)(int n
, void *user
),
2559 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
2563 isl_multi_aff
*expansion
;
2564 isl_basic_map
*bmap
;
2566 isl_ast_build
*stride_build
;
2568 depth
= isl_ast_build_get_depth(build
);
2570 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2571 domain
= isl_set_intersect(domain
, isl_ast_build_get_domain(build
));
2572 stride_build
= isl_ast_build_copy(build
);
2573 stride_build
= isl_ast_build_detect_strides(stride_build
,
2574 isl_set_copy(domain
));
2575 expansion
= isl_ast_build_get_stride_expansion(stride_build
);
2577 domain
= isl_set_preimage_multi_aff(domain
,
2578 isl_multi_aff_copy(expansion
));
2579 domain
= isl_ast_build_eliminate_divs(stride_build
, domain
);
2580 isl_ast_build_free(stride_build
);
2582 bmap
= isl_basic_map_from_multi_aff(expansion
);
2584 lower
= find_unroll_lower_bound(build
, domain
, depth
, bmap
, &n
);
2587 else if (init
&& init(n
, user
) < 0)
2589 for (i
= 0; i
< n
; ++i
) {
2591 isl_basic_set
*bset
;
2592 isl_constraint
*slice
;
2594 slice
= at_offset(depth
, lower
, i
);
2595 set
= isl_set_copy(domain
);
2596 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2597 bset
= isl_set_unshifted_simple_hull(set
);
2598 bset
= isl_basic_set_add_constraint(bset
, slice
);
2599 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2601 if (fn(bset
, user
) < 0)
2605 isl_aff_free(lower
);
2606 isl_set_free(domain
);
2607 isl_basic_map_free(bmap
);
2609 return n
< 0 || i
< n
? -1 : 0;
2612 /* Data structure for storing the results and the intermediate objects
2613 * of compute_domains.
2615 * "list" is the main result of the function and contains a list
2616 * of disjoint basic sets for which code should be generated.
2618 * "executed" and "build" are inputs to compute_domains.
2619 * "schedule_domain" is the domain of "executed".
2621 * "option" constains the domains at the current depth that should by
2622 * atomic, separated or unrolled. These domains are as specified by
2623 * the user, except that inner dimensions have been eliminated and
2624 * that they have been made pair-wise disjoint.
2626 * "sep_class" contains the user-specified split into separation classes
2627 * specialized to the current depth.
2628 * "done" contains the union of the separation domains that have already
2631 struct isl_codegen_domains
{
2632 isl_basic_set_list
*list
;
2634 isl_union_map
*executed
;
2635 isl_ast_build
*build
;
2636 isl_set
*schedule_domain
;
2644 /* Internal data structure for do_unroll.
2646 * "domains" stores the results of compute_domains.
2647 * "class_domain" is the original class domain passed to do_unroll.
2648 * "unroll_domain" collects the unrolled iterations.
2650 struct isl_ast_unroll_data
{
2651 struct isl_codegen_domains
*domains
;
2652 isl_set
*class_domain
;
2653 isl_set
*unroll_domain
;
2656 /* Given an iteration of an unrolled domain represented by "bset",
2657 * add it to data->domains->list.
2658 * Since we may have dropped some constraints, we intersect with
2659 * the class domain again to ensure that each element in the list
2660 * is disjoint from the other class domains.
2662 static int do_unroll_iteration(__isl_take isl_basic_set
*bset
, void *user
)
2664 struct isl_ast_unroll_data
*data
= user
;
2666 isl_basic_set_list
*list
;
2668 set
= isl_set_from_basic_set(bset
);
2669 data
->unroll_domain
= isl_set_union(data
->unroll_domain
,
2671 set
= isl_set_intersect(set
, isl_set_copy(data
->class_domain
));
2672 set
= isl_set_make_disjoint(set
);
2673 list
= isl_basic_set_list_from_set(set
);
2674 data
->domains
->list
= isl_basic_set_list_concat(data
->domains
->list
,
2680 /* Extend domains->list with a list of basic sets, one for each value
2681 * of the current dimension in "domain" and remove the corresponding
2682 * sets from the class domain. Return the updated class domain.
2683 * The divs that involve the current dimension have not been projected out
2686 * We call foreach_iteration to iterate over the individual values and
2687 * in do_unroll_iteration we collect the individual basic sets in
2688 * domains->list and their union in data->unroll_domain, which is then
2689 * used to update the class domain.
2691 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2692 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2694 struct isl_ast_unroll_data data
;
2697 return isl_set_free(class_domain
);
2699 return isl_set_free(domain
);
2701 data
.domains
= domains
;
2702 data
.class_domain
= class_domain
;
2703 data
.unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2705 if (foreach_iteration(domain
, domains
->build
, NULL
,
2706 &do_unroll_iteration
, &data
) < 0)
2707 data
.unroll_domain
= isl_set_free(data
.unroll_domain
);
2709 class_domain
= isl_set_subtract(class_domain
, data
.unroll_domain
);
2711 return class_domain
;
2714 /* Add domains to domains->list for each individual value of the current
2715 * dimension, for that part of the schedule domain that lies in the
2716 * intersection of the option domain and the class domain.
2717 * Remove the corresponding sets from the class domain and
2718 * return the updated class domain.
2720 * We first break up the unroll option domain into individual pieces
2721 * and then handle each of them separately. The unroll option domain
2722 * has been made disjoint in compute_domains_init_options,
2724 * Note that we actively want to combine different pieces of the
2725 * schedule domain that have the same value at the current dimension.
2726 * We therefore need to break up the unroll option domain before
2727 * intersecting with class and schedule domain, hoping that the
2728 * unroll option domain specified by the user is relatively simple.
2730 static __isl_give isl_set
*compute_unroll_domains(
2731 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2733 isl_set
*unroll_domain
;
2734 isl_basic_set_list
*unroll_list
;
2738 empty
= isl_set_is_empty(domains
->option
[isl_ast_loop_unroll
]);
2740 return isl_set_free(class_domain
);
2742 return class_domain
;
2744 unroll_domain
= isl_set_copy(domains
->option
[isl_ast_loop_unroll
]);
2745 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2747 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2748 for (i
= 0; i
< n
; ++i
) {
2749 isl_basic_set
*bset
;
2751 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2752 unroll_domain
= isl_set_from_basic_set(bset
);
2753 unroll_domain
= isl_set_intersect(unroll_domain
,
2754 isl_set_copy(class_domain
));
2755 unroll_domain
= isl_set_intersect(unroll_domain
,
2756 isl_set_copy(domains
->schedule_domain
));
2758 empty
= isl_set_is_empty(unroll_domain
);
2759 if (empty
>= 0 && empty
) {
2760 isl_set_free(unroll_domain
);
2764 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2767 isl_basic_set_list_free(unroll_list
);
2769 return class_domain
;
2772 /* Try and construct a single basic set that includes the intersection of
2773 * the schedule domain, the atomic option domain and the class domain.
2774 * Add the resulting basic set(s) to domains->list and remove them
2775 * from class_domain. Return the updated class domain.
2777 * We construct a single domain rather than trying to combine
2778 * the schedule domains of individual domains because we are working
2779 * within a single component so that non-overlapping schedule domains
2780 * should already have been separated.
2781 * We do however need to make sure that this single domains is a subset
2782 * of the class domain so that it would not intersect with any other
2783 * class domains. This means that we may end up splitting up the atomic
2784 * domain in case separation classes are being used.
2786 * "domain" is the intersection of the schedule domain and the class domain,
2787 * with inner dimensions projected out.
2789 static __isl_give isl_set
*compute_atomic_domain(
2790 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2792 isl_basic_set
*bset
;
2793 isl_basic_set_list
*list
;
2794 isl_set
*domain
, *atomic_domain
;
2797 domain
= isl_set_copy(domains
->option
[isl_ast_loop_atomic
]);
2798 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2799 domain
= isl_set_intersect(domain
,
2800 isl_set_copy(domains
->schedule_domain
));
2801 empty
= isl_set_is_empty(domain
);
2803 class_domain
= isl_set_free(class_domain
);
2805 isl_set_free(domain
);
2806 return class_domain
;
2809 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2810 domain
= isl_set_coalesce(domain
);
2811 bset
= isl_set_unshifted_simple_hull(domain
);
2812 domain
= isl_set_from_basic_set(bset
);
2813 atomic_domain
= isl_set_copy(domain
);
2814 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2815 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2816 domain
= isl_set_make_disjoint(domain
);
2817 list
= isl_basic_set_list_from_set(domain
);
2818 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2820 return class_domain
;
2823 /* Split up the schedule domain into uniform basic sets,
2824 * in the sense that each element in a basic set is associated to
2825 * elements of the same domains, and add the result to domains->list.
2826 * Do this for that part of the schedule domain that lies in the
2827 * intersection of "class_domain" and the separate option domain.
2829 * "class_domain" may or may not include the constraints
2830 * of the schedule domain, but this does not make a difference
2831 * since we are going to intersect it with the domain of the inverse schedule.
2832 * If it includes schedule domain constraints, then they may involve
2833 * inner dimensions, but we will eliminate them in separation_domain.
2835 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2836 __isl_keep isl_set
*class_domain
)
2840 isl_union_map
*executed
;
2841 isl_basic_set_list
*list
;
2844 domain
= isl_set_copy(domains
->option
[isl_ast_loop_separate
]);
2845 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2846 executed
= isl_union_map_copy(domains
->executed
);
2847 executed
= isl_union_map_intersect_domain(executed
,
2848 isl_union_set_from_set(domain
));
2849 empty
= isl_union_map_is_empty(executed
);
2850 if (empty
< 0 || empty
) {
2851 isl_union_map_free(executed
);
2852 return empty
< 0 ? -1 : 0;
2855 space
= isl_set_get_space(class_domain
);
2856 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2858 list
= isl_basic_set_list_from_set(domain
);
2859 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2864 /* Split up the domain at the current depth into disjoint
2865 * basic sets for which code should be generated separately
2866 * for the given separation class domain.
2868 * If any separation classes have been defined, then "class_domain"
2869 * is the domain of the current class and does not refer to inner dimensions.
2870 * Otherwise, "class_domain" is the universe domain.
2872 * We first make sure that the class domain is disjoint from
2873 * previously considered class domains.
2875 * The separate domains can be computed directly from the "class_domain".
2877 * The unroll, atomic and remainder domains need the constraints
2878 * from the schedule domain.
2880 * For unrolling, the actual schedule domain is needed (with divs that
2881 * may refer to the current dimension) so that stride detection can be
2884 * For atomic and remainder domains, inner dimensions and divs involving
2885 * the current dimensions should be eliminated.
2886 * In case we are working within a separation class, we need to intersect
2887 * the result with the current "class_domain" to ensure that the domains
2888 * are disjoint from those generated from other class domains.
2890 * The domain that has been made atomic may be larger than specified
2891 * by the user since it needs to be representable as a single basic set.
2892 * This possibly larger domain is removed from class_domain by
2893 * compute_atomic_domain. It is computed first so that the extended domain
2894 * would not overlap with any domains computed before.
2895 * Similary, the unrolled domains may have some constraints removed and
2896 * may therefore also be larger than specified by the user.
2898 * If anything is left after handling separate, unroll and atomic,
2899 * we split it up into basic sets and append the basic sets to domains->list.
2901 static isl_stat
compute_partial_domains(struct isl_codegen_domains
*domains
,
2902 __isl_take isl_set
*class_domain
)
2904 isl_basic_set_list
*list
;
2907 class_domain
= isl_set_subtract(class_domain
,
2908 isl_set_copy(domains
->done
));
2909 domains
->done
= isl_set_union(domains
->done
,
2910 isl_set_copy(class_domain
));
2912 class_domain
= compute_atomic_domain(domains
, class_domain
);
2913 class_domain
= compute_unroll_domains(domains
, class_domain
);
2915 domain
= isl_set_copy(class_domain
);
2917 if (compute_separate_domain(domains
, domain
) < 0)
2919 domain
= isl_set_subtract(domain
,
2920 isl_set_copy(domains
->option
[isl_ast_loop_separate
]));
2922 domain
= isl_set_intersect(domain
,
2923 isl_set_copy(domains
->schedule_domain
));
2925 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2926 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2928 domain
= isl_set_coalesce(domain
);
2929 domain
= isl_set_make_disjoint(domain
);
2931 list
= isl_basic_set_list_from_set(domain
);
2932 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2934 isl_set_free(class_domain
);
2938 isl_set_free(domain
);
2939 isl_set_free(class_domain
);
2940 return isl_stat_error
;
2943 /* Split up the domain at the current depth into disjoint
2944 * basic sets for which code should be generated separately
2945 * for the separation class identified by "pnt".
2947 * We extract the corresponding class domain from domains->sep_class,
2948 * eliminate inner dimensions and pass control to compute_partial_domains.
2950 static isl_stat
compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
2952 struct isl_codegen_domains
*domains
= user
;
2957 class_set
= isl_set_from_point(pnt
);
2958 domain
= isl_map_domain(isl_map_intersect_range(
2959 isl_map_copy(domains
->sep_class
), class_set
));
2960 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
2961 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2963 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
2965 return isl_stat_error
;
2967 isl_set_free(domain
);
2971 return compute_partial_domains(domains
, domain
);
2974 /* Extract the domains at the current depth that should be atomic,
2975 * separated or unrolled and store them in option.
2977 * The domains specified by the user might overlap, so we make
2978 * them disjoint by subtracting earlier domains from later domains.
2980 static void compute_domains_init_options(isl_set
*option
[4],
2981 __isl_keep isl_ast_build
*build
)
2983 enum isl_ast_loop_type type
, type2
;
2986 for (type
= isl_ast_loop_atomic
;
2987 type
<= isl_ast_loop_separate
; ++type
) {
2988 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
2989 for (type2
= isl_ast_loop_atomic
; type2
< type
; ++type2
)
2990 option
[type
] = isl_set_subtract(option
[type
],
2991 isl_set_copy(option
[type2
]));
2994 unroll
= option
[isl_ast_loop_unroll
];
2995 unroll
= isl_set_coalesce(unroll
);
2996 unroll
= isl_set_make_disjoint(unroll
);
2997 option
[isl_ast_loop_unroll
] = unroll
;
3000 /* Split up the domain at the current depth into disjoint
3001 * basic sets for which code should be generated separately,
3002 * based on the user-specified options.
3003 * Return the list of disjoint basic sets.
3005 * There are three kinds of domains that we need to keep track of.
3006 * - the "schedule domain" is the domain of "executed"
3007 * - the "class domain" is the domain corresponding to the currrent
3009 * - the "option domain" is the domain corresponding to one of the options
3010 * atomic, unroll or separate
3012 * We first consider the individial values of the separation classes
3013 * and split up the domain for each of them separately.
3014 * Finally, we consider the remainder. If no separation classes were
3015 * specified, then we call compute_partial_domains with the universe
3016 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3017 * with inner dimensions removed. We do this because we want to
3018 * avoid computing the complement of the class domains (i.e., the difference
3019 * between the universe and domains->done).
3021 static __isl_give isl_basic_set_list
*compute_domains(
3022 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
3024 struct isl_codegen_domains domains
;
3027 isl_union_set
*schedule_domain
;
3031 enum isl_ast_loop_type type
;
3037 ctx
= isl_union_map_get_ctx(executed
);
3038 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
3040 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3041 domain
= isl_set_from_union_set(schedule_domain
);
3043 compute_domains_init_options(domains
.option
, build
);
3045 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
3046 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
3047 n_param
= isl_set_dim(classes
, isl_dim_param
);
3048 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
3050 space
= isl_set_get_space(domain
);
3051 domains
.build
= build
;
3052 domains
.schedule_domain
= isl_set_copy(domain
);
3053 domains
.executed
= executed
;
3054 domains
.done
= isl_set_empty(space
);
3056 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
3057 domains
.list
= isl_basic_set_list_free(domains
.list
);
3058 isl_set_free(classes
);
3060 empty
= isl_set_is_empty(domains
.done
);
3062 domains
.list
= isl_basic_set_list_free(domains
.list
);
3063 domain
= isl_set_free(domain
);
3065 isl_set_free(domain
);
3066 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
3068 domain
= isl_ast_build_eliminate(build
, domain
);
3070 if (compute_partial_domains(&domains
, domain
) < 0)
3071 domains
.list
= isl_basic_set_list_free(domains
.list
);
3073 isl_set_free(domains
.schedule_domain
);
3074 isl_set_free(domains
.done
);
3075 isl_map_free(domains
.sep_class
);
3076 for (type
= isl_ast_loop_atomic
; type
<= isl_ast_loop_separate
; ++type
)
3077 isl_set_free(domains
.option
[type
]);
3079 return domains
.list
;
3082 /* Generate code for a single component, after shifting (if any)
3083 * has been applied, in case the schedule was specified as a union map.
3085 * We first split up the domain at the current depth into disjoint
3086 * basic sets based on the user-specified options.
3087 * Then we generated code for each of them and concatenate the results.
3089 static __isl_give isl_ast_graft_list
*generate_shifted_component_flat(
3090 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3092 isl_basic_set_list
*domain_list
;
3093 isl_ast_graft_list
*list
= NULL
;
3095 domain_list
= compute_domains(executed
, build
);
3096 list
= generate_parallel_domains(domain_list
, executed
, build
);
3098 isl_basic_set_list_free(domain_list
);
3099 isl_union_map_free(executed
);
3100 isl_ast_build_free(build
);
3105 /* Generate code for a single component, after shifting (if any)
3106 * has been applied, in case the schedule was specified as a schedule tree
3107 * and the separate option was specified.
3109 * We perform separation on the domain of "executed" and then generate
3110 * an AST for each of the resulting disjoint basic sets.
3112 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_separate(
3113 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3117 isl_basic_set_list
*domain_list
;
3118 isl_ast_graft_list
*list
;
3120 space
= isl_ast_build_get_space(build
, 1);
3121 domain
= separate_schedule_domains(space
,
3122 isl_union_map_copy(executed
), build
);
3123 domain_list
= isl_basic_set_list_from_set(domain
);
3125 list
= generate_parallel_domains(domain_list
, executed
, build
);
3127 isl_basic_set_list_free(domain_list
);
3128 isl_union_map_free(executed
);
3129 isl_ast_build_free(build
);
3134 /* Internal data structure for generate_shifted_component_tree_unroll.
3136 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3137 * "list" collects the constructs grafts.
3139 struct isl_ast_unroll_tree_data
{
3140 isl_union_map
*executed
;
3141 isl_ast_build
*build
;
3142 isl_ast_graft_list
*list
;
3145 /* Initialize data->list to a list of "n" elements.
3147 static int init_unroll_tree(int n
, void *user
)
3149 struct isl_ast_unroll_tree_data
*data
= user
;
3152 ctx
= isl_ast_build_get_ctx(data
->build
);
3153 data
->list
= isl_ast_graft_list_alloc(ctx
, n
);
3158 /* Given an iteration of an unrolled domain represented by "bset",
3159 * generate the corresponding AST and add the result to data->list.
3161 static int do_unroll_tree_iteration(__isl_take isl_basic_set
*bset
, void *user
)
3163 struct isl_ast_unroll_tree_data
*data
= user
;
3165 data
->list
= add_node(data
->list
, isl_union_map_copy(data
->executed
),
3166 bset
, isl_ast_build_copy(data
->build
));
3171 /* Generate code for a single component, after shifting (if any)
3172 * has been applied, in case the schedule was specified as a schedule tree
3173 * and the unroll option was specified.
3175 * We call foreach_iteration to iterate over the individual values and
3176 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3178 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_unroll(
3179 __isl_take isl_union_map
*executed
, __isl_take isl_set
*domain
,
3180 __isl_take isl_ast_build
*build
)
3182 struct isl_ast_unroll_tree_data data
= { executed
, build
, NULL
};
3184 if (foreach_iteration(domain
, build
, &init_unroll_tree
,
3185 &do_unroll_tree_iteration
, &data
) < 0)
3186 data
.list
= isl_ast_graft_list_free(data
.list
);
3188 isl_union_map_free(executed
);
3189 isl_ast_build_free(build
);
3194 /* Generate code for a single component, after shifting (if any)
3195 * has been applied, in case the schedule was specified as a schedule tree.
3196 * In particular, handle the base case where there is either no isolated
3197 * set or we are within the isolated set (in which case "isolated" is set)
3198 * or the iterations that precede or follow the isolated set.
3200 * The schedule domain is broken up or combined into basic sets
3201 * according to the AST generation option specified in the current
3202 * schedule node, which may be either atomic, separate, unroll or
3203 * unspecified. If the option is unspecified, then we currently simply
3204 * split the schedule domain into disjoint basic sets.
3206 * In case the separate option is specified, the AST generation is
3207 * handled by generate_shifted_component_tree_separate.
3208 * In the other cases, we need the global schedule domain.
3209 * In the unroll case, the AST generation is then handled by
3210 * generate_shifted_component_tree_unroll which needs the actual
3211 * schedule domain (with divs that may refer to the current dimension)
3212 * so that stride detection can be performed.
3213 * In the atomic or unspecified case, inner dimensions and divs involving
3214 * the current dimensions should be eliminated.
3215 * The result is then either combined into a single basic set or
3216 * split up into disjoint basic sets.
3217 * Finally an AST is generated for each basic set and the results are
3220 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_base(
3221 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3224 isl_union_set
*schedule_domain
;
3226 isl_basic_set_list
*domain_list
;
3227 isl_ast_graft_list
*list
;
3228 enum isl_ast_loop_type type
;
3230 type
= isl_ast_build_get_loop_type(build
, isolated
);
3234 if (type
== isl_ast_loop_separate
)
3235 return generate_shifted_component_tree_separate(executed
,
3238 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3239 domain
= isl_set_from_union_set(schedule_domain
);
3241 if (type
== isl_ast_loop_unroll
)
3242 return generate_shifted_component_tree_unroll(executed
, domain
,
3245 domain
= isl_ast_build_eliminate(build
, domain
);
3246 domain
= isl_set_coalesce(domain
);
3248 if (type
== isl_ast_loop_atomic
) {
3249 isl_basic_set
*hull
;
3250 hull
= isl_set_unshifted_simple_hull(domain
);
3251 domain_list
= isl_basic_set_list_from_basic_set(hull
);
3253 domain
= isl_set_make_disjoint(domain
);
3254 domain_list
= isl_basic_set_list_from_set(domain
);
3257 list
= generate_parallel_domains(domain_list
, executed
, build
);
3259 isl_basic_set_list_free(domain_list
);
3260 isl_union_map_free(executed
);
3261 isl_ast_build_free(build
);
3265 isl_union_map_free(executed
);
3266 isl_ast_build_free(build
);
3270 /* Generate code for a single component, after shifting (if any)
3271 * has been applied, in case the schedule was specified as a schedule tree.
3272 * In particular, do so for the specified subset of the schedule domain.
3274 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_part(
3275 __isl_keep isl_union_map
*executed
, __isl_take isl_set
*domain
,
3276 __isl_keep isl_ast_build
*build
, int isolated
)
3278 isl_union_set
*uset
;
3281 uset
= isl_union_set_from_set(domain
);
3282 executed
= isl_union_map_copy(executed
);
3283 executed
= isl_union_map_intersect_domain(executed
, uset
);
3284 empty
= isl_union_map_is_empty(executed
);
3289 isl_union_map_free(executed
);
3290 ctx
= isl_ast_build_get_ctx(build
);
3291 return isl_ast_graft_list_alloc(ctx
, 0);
3294 build
= isl_ast_build_copy(build
);
3295 return generate_shifted_component_tree_base(executed
, build
, isolated
);
3297 isl_union_map_free(executed
);
3301 /* Generate code for a single component, after shifting (if any)
3302 * has been applied, in case the schedule was specified as a schedule tree.
3304 * We first check if the user has specified an isolated schedule domain
3305 * and that we are not already outside of this isolated schedule domain.
3306 * If so, we break up the schedule domain into iterations that
3307 * precede the isolated domain, the isolated domain itself,
3308 * the iterations that follow the isolated domain and
3309 * the remaining iterations (those that are incomparable
3310 * to the isolated domain).
3311 * We generate an AST for each piece and concatenate the results.
3312 * If no isolated set has been specified, then we generate an
3313 * AST for the entire inverse schedule.
3315 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree(
3316 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3319 int empty
, has_isolate
;
3321 isl_union_set
*schedule_domain
;
3323 isl_basic_set
*hull
;
3324 isl_set
*isolated
, *before
, *after
, *test
;
3326 isl_ast_graft_list
*list
, *res
;
3328 build
= isl_ast_build_extract_isolated(build
);
3329 has_isolate
= isl_ast_build_has_isolated(build
);
3330 if (has_isolate
< 0)
3331 executed
= isl_union_map_free(executed
);
3332 else if (!has_isolate
)
3333 return generate_shifted_component_tree_base(executed
, build
, 0);
3335 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3336 domain
= isl_set_from_union_set(schedule_domain
);
3338 isolated
= isl_ast_build_get_isolated(build
);
3339 isolated
= isl_set_intersect(isolated
, isl_set_copy(domain
));
3340 test
= isl_ast_build_specialize(build
, isl_set_copy(isolated
));
3341 empty
= isl_set_is_empty(test
);
3346 isl_set_free(isolated
);
3347 isl_set_free(domain
);
3348 return generate_shifted_component_tree_base(executed
, build
, 0);
3350 isolated
= isl_ast_build_eliminate(build
, isolated
);
3351 hull
= isl_set_unshifted_simple_hull(isolated
);
3352 isolated
= isl_set_from_basic_set(hull
);
3354 depth
= isl_ast_build_get_depth(build
);
3355 space
= isl_space_map_from_set(isl_set_get_space(isolated
));
3356 gt
= isl_map_universe(space
);
3357 for (i
= 0; i
< depth
; ++i
)
3358 gt
= isl_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
3359 gt
= isl_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
3360 lt
= isl_map_reverse(isl_map_copy(gt
));
3361 before
= isl_set_apply(isl_set_copy(isolated
), gt
);
3362 after
= isl_set_apply(isl_set_copy(isolated
), lt
);
3364 domain
= isl_set_subtract(domain
, isl_set_copy(isolated
));
3365 domain
= isl_set_subtract(domain
, isl_set_copy(before
));
3366 domain
= isl_set_subtract(domain
, isl_set_copy(after
));
3367 after
= isl_set_subtract(after
, isl_set_copy(isolated
));
3368 after
= isl_set_subtract(after
, isl_set_copy(before
));
3369 before
= isl_set_subtract(before
, isl_set_copy(isolated
));
3371 res
= generate_shifted_component_tree_part(executed
, before
, build
, 0);
3372 list
= generate_shifted_component_tree_part(executed
, isolated
,
3374 res
= isl_ast_graft_list_concat(res
, list
);
3375 list
= generate_shifted_component_tree_part(executed
, after
, build
, 0);
3376 res
= isl_ast_graft_list_concat(res
, list
);
3377 list
= generate_shifted_component_tree_part(executed
, domain
, build
, 0);
3378 res
= isl_ast_graft_list_concat(res
, list
);
3380 isl_union_map_free(executed
);
3381 isl_ast_build_free(build
);
3385 isl_set_free(domain
);
3386 isl_set_free(isolated
);
3387 isl_union_map_free(executed
);
3388 isl_ast_build_free(build
);
3392 /* Generate code for a single component, after shifting (if any)
3395 * Call generate_shifted_component_tree or generate_shifted_component_flat
3396 * depending on whether the schedule was specified as a schedule tree.
3398 static __isl_give isl_ast_graft_list
*generate_shifted_component(
3399 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3401 if (isl_ast_build_has_schedule_node(build
))
3402 return generate_shifted_component_tree(executed
, build
);
3404 return generate_shifted_component_flat(executed
, build
);
3407 struct isl_set_map_pair
{
3412 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3413 * of indices into the "domain" array,
3414 * return the union of the "map" fields of the elements
3415 * indexed by the first "n" elements of "order".
3417 static __isl_give isl_union_map
*construct_component_executed(
3418 struct isl_set_map_pair
*domain
, int *order
, int n
)
3422 isl_union_map
*executed
;
3424 map
= isl_map_copy(domain
[order
[0]].map
);
3425 executed
= isl_union_map_from_map(map
);
3426 for (i
= 1; i
< n
; ++i
) {
3427 map
= isl_map_copy(domain
[order
[i
]].map
);
3428 executed
= isl_union_map_add_map(executed
, map
);
3434 /* Generate code for a single component, after shifting (if any)
3437 * The component inverse schedule is specified as the "map" fields
3438 * of the elements of "domain" indexed by the first "n" elements of "order".
3440 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
3441 struct isl_set_map_pair
*domain
, int *order
, int n
,
3442 __isl_take isl_ast_build
*build
)
3444 isl_union_map
*executed
;
3446 executed
= construct_component_executed(domain
, order
, n
);
3447 return generate_shifted_component(executed
, build
);
3450 /* Does set dimension "pos" of "set" have an obviously fixed value?
3452 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
3457 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
3460 fixed
= !isl_val_is_nan(v
);
3466 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3467 * of indices into the "domain" array,
3468 * do all (except for at most one) of the "set" field of the elements
3469 * indexed by the first "n" elements of "order" have a fixed value
3470 * at position "depth"?
3472 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
3473 int *order
, int n
, int depth
)
3478 for (i
= 0; i
< n
; ++i
) {
3481 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3494 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3495 * of indices into the "domain" array,
3496 * eliminate the inner dimensions from the "set" field of the elements
3497 * indexed by the first "n" elements of "order", provided the current
3498 * dimension does not have a fixed value.
3500 * Return the index of the first element in "order" with a corresponding
3501 * "set" field that does not have an (obviously) fixed value.
3503 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
3504 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
3509 for (i
= n
- 1; i
>= 0; --i
) {
3511 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3516 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
3517 domain
[order
[i
]].set
);
3524 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3525 * of indices into the "domain" array,
3526 * find the element of "domain" (amongst those indexed by the first "n"
3527 * elements of "order") with the "set" field that has the smallest
3528 * value for the current iterator.
3530 * Note that the domain with the smallest value may depend on the parameters
3531 * and/or outer loop dimension. Since the result of this function is only
3532 * used as heuristic, we only make a reasonable attempt at finding the best
3533 * domain, one that should work in case a single domain provides the smallest
3534 * value for the current dimension over all values of the parameters
3535 * and outer dimensions.
3537 * In particular, we compute the smallest value of the first domain
3538 * and replace it by that of any later domain if that later domain
3539 * has a smallest value that is smaller for at least some value
3540 * of the parameters and outer dimensions.
3542 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
3543 __isl_keep isl_ast_build
*build
)
3549 min_first
= isl_ast_build_map_to_iterator(build
,
3550 isl_set_copy(domain
[order
[0]].set
));
3551 min_first
= isl_map_lexmin(min_first
);
3553 for (i
= 1; i
< n
; ++i
) {
3554 isl_map
*min
, *test
;
3557 min
= isl_ast_build_map_to_iterator(build
,
3558 isl_set_copy(domain
[order
[i
]].set
));
3559 min
= isl_map_lexmin(min
);
3560 test
= isl_map_copy(min
);
3561 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
3562 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
3563 empty
= isl_map_is_empty(test
);
3565 if (empty
>= 0 && !empty
) {
3566 isl_map_free(min_first
);
3576 isl_map_free(min_first
);
3578 return i
< n
? -1 : first
;
3581 /* Construct a shifted inverse schedule based on the original inverse schedule,
3582 * the stride and the offset.
3584 * The original inverse schedule is specified as the "map" fields
3585 * of the elements of "domain" indexed by the first "n" elements of "order".
3587 * "stride" and "offset" are such that the difference
3588 * between the values of the current dimension of domain "i"
3589 * and the values of the current dimension for some reference domain are
3592 * stride * integer + offset[i]
3594 * Moreover, 0 <= offset[i] < stride.
3596 * For each domain, we create a map
3598 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3600 * where j refers to the current dimension and the other dimensions are
3601 * unchanged, and apply this map to the original schedule domain.
3603 * For example, for the original schedule
3605 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3607 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3608 * we apply the mapping
3612 * to the schedule of the "A" domain and the mapping
3614 * { [j - 1] -> [j, 1] }
3616 * to the schedule of the "B" domain.
3619 * Note that after the transformation, the differences between pairs
3620 * of values of the current dimension over all domains are multiples
3621 * of stride and that we have therefore exposed the stride.
3624 * To see that the mapping preserves the lexicographic order,
3625 * first note that each of the individual maps above preserves the order.
3626 * If the value of the current iterator is j1 in one domain and j2 in another,
3627 * then if j1 = j2, we know that the same map is applied to both domains
3628 * and the order is preserved.
3629 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3630 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3634 * and the order is preserved.
3635 * If c1 < c2, then we know
3641 * j2 - j1 = n * s + r
3643 * with n >= 0 and 0 <= r < s.
3644 * In other words, r = c2 - c1.
3655 * (j1 - c1, c1) << (j2 - c2, c2)
3657 * with "<<" the lexicographic order, proving that the order is preserved
3660 static __isl_give isl_union_map
*contruct_shifted_executed(
3661 struct isl_set_map_pair
*domain
, int *order
, int n
,
3662 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3663 __isl_take isl_ast_build
*build
)
3666 isl_union_map
*executed
;
3672 depth
= isl_ast_build_get_depth(build
);
3673 space
= isl_ast_build_get_space(build
, 1);
3674 executed
= isl_union_map_empty(isl_space_copy(space
));
3675 space
= isl_space_map_from_set(space
);
3676 map
= isl_map_identity(isl_space_copy(space
));
3677 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3678 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3679 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3681 c
= isl_constraint_alloc_equality(isl_local_space_from_space(space
));
3682 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3683 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3685 for (i
= 0; i
< n
; ++i
) {
3689 v
= isl_multi_val_get_val(offset
, i
);
3692 map_i
= isl_map_copy(map
);
3693 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
3696 c
= isl_constraint_set_constant_val(c
, v
);
3697 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
3699 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
3701 executed
= isl_union_map_add_map(executed
, map_i
);
3704 isl_constraint_free(c
);
3708 executed
= isl_union_map_free(executed
);
3713 /* Generate code for a single component, after exposing the stride,
3714 * given that the schedule domain is "shifted strided".
3716 * The component inverse schedule is specified as the "map" fields
3717 * of the elements of "domain" indexed by the first "n" elements of "order".
3719 * The schedule domain being "shifted strided" means that the differences
3720 * between the values of the current dimension of domain "i"
3721 * and the values of the current dimension for some reference domain are
3724 * stride * integer + offset[i]
3726 * We first look for the domain with the "smallest" value for the current
3727 * dimension and adjust the offsets such that the offset of the "smallest"
3728 * domain is equal to zero. The other offsets are reduced modulo stride.
3730 * Based on this information, we construct a new inverse schedule in
3731 * contruct_shifted_executed that exposes the stride.
3732 * Since this involves the introduction of a new schedule dimension,
3733 * the build needs to be changed accodingly.
3734 * After computing the AST, the newly introduced dimension needs
3735 * to be removed again from the list of grafts. We do this by plugging
3736 * in a mapping that represents the new schedule domain in terms of the
3737 * old schedule domain.
3739 static __isl_give isl_ast_graft_list
*generate_shift_component(
3740 struct isl_set_map_pair
*domain
, int *order
, int n
,
3741 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3742 __isl_take isl_ast_build
*build
)
3744 isl_ast_graft_list
*list
;
3750 isl_multi_aff
*ma
, *zero
;
3751 isl_union_map
*executed
;
3753 depth
= isl_ast_build_get_depth(build
);
3755 first
= first_offset(domain
, order
, n
, build
);
3759 mv
= isl_multi_val_copy(offset
);
3760 val
= isl_multi_val_get_val(offset
, first
);
3761 val
= isl_val_neg(val
);
3762 mv
= isl_multi_val_add_val(mv
, val
);
3763 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
3765 executed
= contruct_shifted_executed(domain
, order
, n
, stride
, mv
,
3767 space
= isl_ast_build_get_space(build
, 1);
3768 space
= isl_space_map_from_set(space
);
3769 ma
= isl_multi_aff_identity(isl_space_copy(space
));
3770 space
= isl_space_from_domain(isl_space_domain(space
));
3771 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3772 zero
= isl_multi_aff_zero(space
);
3773 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
3774 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
3775 list
= generate_shifted_component(executed
, build
);
3777 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
3779 isl_multi_val_free(mv
);
3783 isl_ast_build_free(build
);
3787 /* Does any node in the schedule tree rooted at the current schedule node
3788 * of "build" depend on outer schedule nodes?
3790 static int has_anchored_subtree(__isl_keep isl_ast_build
*build
)
3792 isl_schedule_node
*node
;
3795 node
= isl_ast_build_get_schedule_node(build
);
3796 dependent
= isl_schedule_node_is_subtree_anchored(node
);
3797 isl_schedule_node_free(node
);
3802 /* Generate code for a single component.
3804 * The component inverse schedule is specified as the "map" fields
3805 * of the elements of "domain" indexed by the first "n" elements of "order".
3807 * This function may modify the "set" fields of "domain".
3809 * Before proceeding with the actual code generation for the component,
3810 * we first check if there are any "shifted" strides, meaning that
3811 * the schedule domains of the individual domains are all strided,
3812 * but that they have different offsets, resulting in the union
3813 * of schedule domains not being strided anymore.
3815 * The simplest example is the schedule
3817 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3819 * Both schedule domains are strided, but their union is not.
3820 * This function detects such cases and then rewrites the schedule to
3822 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3824 * In the new schedule, the schedule domains have the same offset (modulo
3825 * the stride), ensuring that the union of schedule domains is also strided.
3828 * If there is only a single domain in the component, then there is
3829 * nothing to do. Similarly, if the current schedule dimension has
3830 * a fixed value for almost all domains then there is nothing to be done.
3831 * In particular, we need at least two domains where the current schedule
3832 * dimension does not have a fixed value.
3833 * Finally, in case of a schedule map input,
3834 * if any of the options refer to the current schedule dimension,
3835 * then we bail out as well. It would be possible to reformulate the options
3836 * in terms of the new schedule domain, but that would introduce constraints
3837 * that separate the domains in the options and that is something we would
3839 * In the case of a schedule tree input, we bail out if any of
3840 * the descendants of the current schedule node refer to outer
3841 * schedule nodes in any way.
3844 * To see if there is any shifted stride, we look at the differences
3845 * between the values of the current dimension in pairs of domains
3846 * for equal values of outer dimensions. These differences should be
3851 * with "m" the stride and "r" a constant. Note that we cannot perform
3852 * this analysis on individual domains as the lower bound in each domain
3853 * may depend on parameters or outer dimensions and so the current dimension
3854 * itself may not have a fixed remainder on division by the stride.
3856 * In particular, we compare the first domain that does not have an
3857 * obviously fixed value for the current dimension to itself and all
3858 * other domains and collect the offsets and the gcd of the strides.
3859 * If the gcd becomes one, then we failed to find shifted strides.
3860 * If the gcd is zero, then the differences were all fixed, meaning
3861 * that some domains had non-obviously fixed values for the current dimension.
3862 * If all the offsets are the same (for those domains that do not have
3863 * an obviously fixed value for the current dimension), then we do not
3864 * apply the transformation.
3865 * If none of the domains were skipped, then there is nothing to do.
3866 * If some of them were skipped, then if we apply separation, the schedule
3867 * domain should get split in pieces with a (non-shifted) stride.
3869 * Otherwise, we apply a shift to expose the stride in
3870 * generate_shift_component.
3872 static __isl_give isl_ast_graft_list
*generate_component(
3873 struct isl_set_map_pair
*domain
, int *order
, int n
,
3874 __isl_take isl_ast_build
*build
)
3881 isl_val
*gcd
= NULL
;
3885 isl_ast_graft_list
*list
;
3888 depth
= isl_ast_build_get_depth(build
);
3891 if (skip
>= 0 && !skip
)
3892 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
3893 if (skip
>= 0 && !skip
) {
3894 if (isl_ast_build_has_schedule_node(build
))
3895 skip
= has_anchored_subtree(build
);
3897 skip
= isl_ast_build_options_involve_depth(build
);
3902 return generate_shifted_component_from_list(domain
,
3905 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
3909 ctx
= isl_ast_build_get_ctx(build
);
3911 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
3914 for (i
= 0; i
< n
; ++i
) {
3917 map
= isl_map_from_domain_and_range(
3918 isl_set_copy(domain
[order
[base
]].set
),
3919 isl_set_copy(domain
[order
[i
]].set
));
3920 for (d
= 0; d
< depth
; ++d
)
3921 map
= isl_map_equate(map
, isl_dim_in
, d
,
3923 deltas
= isl_map_deltas(map
);
3924 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
3925 isl_set_free(deltas
);
3932 gcd
= isl_val_gcd(gcd
, m
);
3933 if (isl_val_is_one(gcd
)) {
3937 mv
= isl_multi_val_set_val(mv
, i
, r
);
3939 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3945 if (fixed
&& i
> base
) {
3947 a
= isl_multi_val_get_val(mv
, i
);
3948 b
= isl_multi_val_get_val(mv
, base
);
3949 if (isl_val_ne(a
, b
))
3956 if (res
< 0 || !gcd
) {
3957 isl_ast_build_free(build
);
3959 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
3960 list
= generate_shifted_component_from_list(domain
,
3963 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
3968 isl_multi_val_free(mv
);
3972 isl_ast_build_free(build
);
3976 /* Store both "map" itself and its domain in the
3977 * structure pointed to by *next and advance to the next array element.
3979 static isl_stat
extract_domain(__isl_take isl_map
*map
, void *user
)
3981 struct isl_set_map_pair
**next
= user
;
3983 (*next
)->map
= isl_map_copy(map
);
3984 (*next
)->set
= isl_map_domain(map
);
3990 static int after_in_tree(__isl_keep isl_union_map
*umap
,
3991 __isl_keep isl_schedule_node
*node
);
3993 /* Is any domain element of "umap" scheduled after any of
3994 * the corresponding image elements by the tree rooted at
3995 * the child of "node"?
3997 static int after_in_child(__isl_keep isl_union_map
*umap
,
3998 __isl_keep isl_schedule_node
*node
)
4000 isl_schedule_node
*child
;
4003 child
= isl_schedule_node_get_child(node
, 0);
4004 after
= after_in_tree(umap
, child
);
4005 isl_schedule_node_free(child
);
4010 /* Is any domain element of "umap" scheduled after any of
4011 * the corresponding image elements by the tree rooted at
4012 * the band node "node"?
4014 * We first check if any domain element is scheduled after any
4015 * of the corresponding image elements by the band node itself.
4016 * If not, we restrict "map" to those pairs of element that
4017 * are scheduled together by the band node and continue with
4018 * the child of the band node.
4019 * If there are no such pairs then the map passed to after_in_child
4020 * will be empty causing it to return 0.
4022 static int after_in_band(__isl_keep isl_union_map
*umap
,
4023 __isl_keep isl_schedule_node
*node
)
4025 isl_multi_union_pw_aff
*mupa
;
4026 isl_union_map
*partial
, *test
, *gt
, *universe
, *umap1
, *umap2
;
4027 isl_union_set
*domain
, *range
;
4032 if (isl_schedule_node_band_n_member(node
) == 0)
4033 return after_in_child(umap
, node
);
4035 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
4036 space
= isl_multi_union_pw_aff_get_space(mupa
);
4037 partial
= isl_union_map_from_multi_union_pw_aff(mupa
);
4038 test
= isl_union_map_copy(umap
);
4039 test
= isl_union_map_apply_domain(test
, isl_union_map_copy(partial
));
4040 test
= isl_union_map_apply_range(test
, isl_union_map_copy(partial
));
4041 gt
= isl_union_map_from_map(isl_map_lex_gt(space
));
4042 test
= isl_union_map_intersect(test
, gt
);
4043 empty
= isl_union_map_is_empty(test
);
4044 isl_union_map_free(test
);
4046 if (empty
< 0 || !empty
) {
4047 isl_union_map_free(partial
);
4048 return empty
< 0 ? -1 : 1;
4051 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4052 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4053 range
= isl_union_map_range(universe
);
4054 umap1
= isl_union_map_copy(partial
);
4055 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4056 umap2
= isl_union_map_intersect_domain(partial
, range
);
4057 test
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4058 test
= isl_union_map_intersect(test
, isl_union_map_copy(umap
));
4059 after
= after_in_child(test
, node
);
4060 isl_union_map_free(test
);
4064 /* Is any domain element of "umap" scheduled after any of
4065 * the corresponding image elements by the tree rooted at
4066 * the context node "node"?
4068 * The context constraints apply to the schedule domain,
4069 * so we cannot apply them directly to "umap", which contains
4070 * pairs of statement instances. Instead, we add them
4071 * to the range of the prefix schedule for both domain and
4074 static int after_in_context(__isl_keep isl_union_map
*umap
,
4075 __isl_keep isl_schedule_node
*node
)
4077 isl_union_map
*prefix
, *universe
, *umap1
, *umap2
;
4078 isl_union_set
*domain
, *range
;
4082 umap
= isl_union_map_copy(umap
);
4083 context
= isl_schedule_node_context_get_context(node
);
4084 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4085 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4086 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4087 range
= isl_union_map_range(universe
);
4088 umap1
= isl_union_map_copy(prefix
);
4089 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4090 umap2
= isl_union_map_intersect_domain(prefix
, range
);
4091 umap1
= isl_union_map_intersect_range(umap1
,
4092 isl_union_set_from_set(context
));
4093 umap1
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4094 umap
= isl_union_map_intersect(umap
, umap1
);
4096 after
= after_in_child(umap
, node
);
4098 isl_union_map_free(umap
);
4103 /* Is any domain element of "umap" scheduled after any of
4104 * the corresponding image elements by the tree rooted at
4105 * the expansion node "node"?
4107 * We apply the expansion to domain and range of "umap" and
4108 * continue with its child.
4110 static int after_in_expansion(__isl_keep isl_union_map
*umap
,
4111 __isl_keep isl_schedule_node
*node
)
4113 isl_union_map
*expansion
;
4116 expansion
= isl_schedule_node_expansion_get_expansion(node
);
4117 umap
= isl_union_map_copy(umap
);
4118 umap
= isl_union_map_apply_domain(umap
, isl_union_map_copy(expansion
));
4119 umap
= isl_union_map_apply_range(umap
, expansion
);
4121 after
= after_in_child(umap
, node
);
4123 isl_union_map_free(umap
);
4128 /* Is any domain element of "umap" scheduled after any of
4129 * the corresponding image elements by the tree rooted at
4130 * the extension node "node"?
4132 * Since the extension node may add statement instances before or
4133 * after the pairs of statement instances in "umap", we return 1
4134 * to ensure that these pairs are not broken up.
4136 static int after_in_extension(__isl_keep isl_union_map
*umap
,
4137 __isl_keep isl_schedule_node
*node
)
4142 /* Is any domain element of "umap" scheduled after any of
4143 * the corresponding image elements by the tree rooted at
4144 * the filter node "node"?
4146 * We intersect domain and range of "umap" with the filter and
4147 * continue with its child.
4149 static int after_in_filter(__isl_keep isl_union_map
*umap
,
4150 __isl_keep isl_schedule_node
*node
)
4152 isl_union_set
*filter
;
4155 umap
= isl_union_map_copy(umap
);
4156 filter
= isl_schedule_node_filter_get_filter(node
);
4157 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(filter
));
4158 umap
= isl_union_map_intersect_range(umap
, filter
);
4160 after
= after_in_child(umap
, node
);
4162 isl_union_map_free(umap
);
4167 /* Is any domain element of "umap" scheduled after any of
4168 * the corresponding image elements by the tree rooted at
4169 * the set node "node"?
4171 * This is only the case if this condition holds in any
4172 * of the (filter) children of the set node.
4173 * In particular, if the domain and the range of "umap"
4174 * are contained in different children, then the condition
4177 static int after_in_set(__isl_keep isl_union_map
*umap
,
4178 __isl_keep isl_schedule_node
*node
)
4182 n
= isl_schedule_node_n_children(node
);
4183 for (i
= 0; i
< n
; ++i
) {
4184 isl_schedule_node
*child
;
4187 child
= isl_schedule_node_get_child(node
, i
);
4188 after
= after_in_tree(umap
, child
);
4189 isl_schedule_node_free(child
);
4191 if (after
< 0 || after
)
4198 /* Return the filter of child "i" of "node".
4200 static __isl_give isl_union_set
*child_filter(
4201 __isl_keep isl_schedule_node
*node
, int i
)
4203 isl_schedule_node
*child
;
4204 isl_union_set
*filter
;
4206 child
= isl_schedule_node_get_child(node
, i
);
4207 filter
= isl_schedule_node_filter_get_filter(child
);
4208 isl_schedule_node_free(child
);
4213 /* Is any domain element of "umap" scheduled after any of
4214 * the corresponding image elements by the tree rooted at
4215 * the sequence node "node"?
4217 * This happens in particular if any domain element is
4218 * contained in a later child than one containing a range element or
4219 * if the condition holds within a given child in the sequence.
4220 * The later part of the condition is checked by after_in_set.
4222 static int after_in_sequence(__isl_keep isl_union_map
*umap
,
4223 __isl_keep isl_schedule_node
*node
)
4226 isl_union_map
*umap_i
;
4227 int empty
, after
= 0;
4229 n
= isl_schedule_node_n_children(node
);
4230 for (i
= 1; i
< n
; ++i
) {
4231 isl_union_set
*filter_i
;
4233 umap_i
= isl_union_map_copy(umap
);
4234 filter_i
= child_filter(node
, i
);
4235 umap_i
= isl_union_map_intersect_domain(umap_i
, filter_i
);
4236 empty
= isl_union_map_is_empty(umap_i
);
4240 isl_union_map_free(umap_i
);
4244 for (j
= 0; j
< i
; ++j
) {
4245 isl_union_set
*filter_j
;
4246 isl_union_map
*umap_ij
;
4248 umap_ij
= isl_union_map_copy(umap_i
);
4249 filter_j
= child_filter(node
, j
);
4250 umap_ij
= isl_union_map_intersect_range(umap_ij
,
4252 empty
= isl_union_map_is_empty(umap_ij
);
4253 isl_union_map_free(umap_ij
);
4263 isl_union_map_free(umap_i
);
4268 if (after
< 0 || after
)
4271 return after_in_set(umap
, node
);
4273 isl_union_map_free(umap_i
);
4277 /* Is any domain element of "umap" scheduled after any of
4278 * the corresponding image elements by the tree rooted at "node"?
4280 * If "umap" is empty, then clearly there is no such element.
4281 * Otherwise, consider the different types of nodes separately.
4283 static int after_in_tree(__isl_keep isl_union_map
*umap
,
4284 __isl_keep isl_schedule_node
*node
)
4287 enum isl_schedule_node_type type
;
4289 empty
= isl_union_map_is_empty(umap
);
4297 type
= isl_schedule_node_get_type(node
);
4299 case isl_schedule_node_error
:
4301 case isl_schedule_node_leaf
:
4303 case isl_schedule_node_band
:
4304 return after_in_band(umap
, node
);
4305 case isl_schedule_node_domain
:
4306 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
4307 "unexpected internal domain node", return -1);
4308 case isl_schedule_node_context
:
4309 return after_in_context(umap
, node
);
4310 case isl_schedule_node_expansion
:
4311 return after_in_expansion(umap
, node
);
4312 case isl_schedule_node_extension
:
4313 return after_in_extension(umap
, node
);
4314 case isl_schedule_node_filter
:
4315 return after_in_filter(umap
, node
);
4316 case isl_schedule_node_guard
:
4317 case isl_schedule_node_mark
:
4318 return after_in_child(umap
, node
);
4319 case isl_schedule_node_set
:
4320 return after_in_set(umap
, node
);
4321 case isl_schedule_node_sequence
:
4322 return after_in_sequence(umap
, node
);
4328 /* Is any domain element of "map1" scheduled after any domain
4329 * element of "map2" by the subtree underneath the current band node,
4330 * while at the same time being scheduled together by the current
4331 * band node, i.e., by "map1" and "map2?
4333 * If the child of the current band node is a leaf, then
4334 * no element can be scheduled after any other element.
4336 * Otherwise, we construct a relation between domain elements
4337 * of "map1" and domain elements of "map2" that are scheduled
4338 * together and then check if the subtree underneath the current
4339 * band node determines their relative order.
4341 static int after_in_subtree(__isl_keep isl_ast_build
*build
,
4342 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4344 isl_schedule_node
*node
;
4346 isl_union_map
*umap
;
4349 node
= isl_ast_build_get_schedule_node(build
);
4352 node
= isl_schedule_node_child(node
, 0);
4353 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
4354 isl_schedule_node_free(node
);
4357 map
= isl_map_copy(map2
);
4358 map
= isl_map_apply_domain(map
, isl_map_copy(map1
));
4359 umap
= isl_union_map_from_map(map
);
4360 after
= after_in_tree(umap
, node
);
4361 isl_union_map_free(umap
);
4362 isl_schedule_node_free(node
);
4366 /* Internal data for any_scheduled_after.
4368 * "build" is the build in which the AST is constructed.
4369 * "depth" is the number of loops that have already been generated
4370 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4371 * "domain" is an array of set-map pairs corresponding to the different
4372 * iteration domains. The set is the schedule domain, i.e., the domain
4373 * of the inverse schedule, while the map is the inverse schedule itself.
4375 struct isl_any_scheduled_after_data
{
4376 isl_ast_build
*build
;
4378 int group_coscheduled
;
4379 struct isl_set_map_pair
*domain
;
4382 /* Is any element of domain "i" scheduled after any element of domain "j"
4383 * (for a common iteration of the first data->depth loops)?
4385 * data->domain[i].set contains the domain of the inverse schedule
4386 * for domain "i", i.e., elements in the schedule domain.
4388 * If we are inside a band of a schedule tree and there is a pair
4389 * of elements in the two domains that is schedule together by
4390 * the current band, then we check if any element of "i" may be schedule
4391 * after element of "j" by the descendants of the band node.
4393 * If data->group_coscheduled is set, then we also return 1 if there
4394 * is any pair of elements in the two domains that are scheduled together.
4396 static isl_bool
any_scheduled_after(int i
, int j
, void *user
)
4398 struct isl_any_scheduled_after_data
*data
= user
;
4399 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
4402 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
4405 follows
= isl_set_follows_at(data
->domain
[i
].set
,
4406 data
->domain
[j
].set
, pos
);
4409 return isl_bool_error
;
4411 return isl_bool_true
;
4413 return isl_bool_false
;
4416 if (isl_ast_build_has_schedule_node(data
->build
)) {
4419 after
= after_in_subtree(data
->build
, data
->domain
[i
].map
,
4420 data
->domain
[j
].map
);
4421 if (after
< 0 || after
)
4425 return data
->group_coscheduled
;
4428 /* Look for independent components at the current depth and generate code
4429 * for each component separately. The resulting lists of grafts are
4430 * merged in an attempt to combine grafts with identical guards.
4432 * Code for two domains can be generated separately if all the elements
4433 * of one domain are scheduled before (or together with) all the elements
4434 * of the other domain. We therefore consider the graph with as nodes
4435 * the domains and an edge between two nodes if any element of the first
4436 * node is scheduled after any element of the second node.
4437 * If the ast_build_group_coscheduled is set, then we also add an edge if
4438 * there is any pair of elements in the two domains that are scheduled
4440 * Code is then generated (by generate_component)
4441 * for each of the strongly connected components in this graph
4442 * in their topological order.
4444 * Since the test is performed on the domain of the inverse schedules of
4445 * the different domains, we precompute these domains and store
4446 * them in data.domain.
4448 static __isl_give isl_ast_graft_list
*generate_components(
4449 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4452 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4453 int n
= isl_union_map_n_map(executed
);
4454 struct isl_any_scheduled_after_data data
;
4455 struct isl_set_map_pair
*next
;
4456 struct isl_tarjan_graph
*g
= NULL
;
4457 isl_ast_graft_list
*list
= NULL
;
4460 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
4466 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
4472 data
.depth
= isl_ast_build_get_depth(build
);
4473 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
4474 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
4478 list
= isl_ast_graft_list_alloc(ctx
, 0);
4482 isl_ast_graft_list
*list_c
;
4485 if (g
->order
[i
] == -1)
4486 isl_die(ctx
, isl_error_internal
, "cannot happen",
4489 while (g
->order
[i
] != -1) {
4493 list_c
= generate_component(data
.domain
,
4494 g
->order
+ first
, i
- first
,
4495 isl_ast_build_copy(build
));
4496 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
4502 error
: list
= isl_ast_graft_list_free(list
);
4503 isl_tarjan_graph_free(g
);
4504 for (i
= 0; i
< n_domain
; ++i
) {
4505 isl_map_free(data
.domain
[i
].map
);
4506 isl_set_free(data
.domain
[i
].set
);
4509 isl_union_map_free(executed
);
4510 isl_ast_build_free(build
);
4515 /* Generate code for the next level (and all inner levels).
4517 * If "executed" is empty, i.e., no code needs to be generated,
4518 * then we return an empty list.
4520 * If we have already generated code for all loop levels, then we pass
4521 * control to generate_inner_level.
4523 * If "executed" lives in a single space, i.e., if code needs to be
4524 * generated for a single domain, then there can only be a single
4525 * component and we go directly to generate_shifted_component.
4526 * Otherwise, we call generate_components to detect the components
4527 * and to call generate_component on each of them separately.
4529 static __isl_give isl_ast_graft_list
*generate_next_level(
4530 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4534 if (!build
|| !executed
)
4537 if (isl_union_map_is_empty(executed
)) {
4538 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4539 isl_union_map_free(executed
);
4540 isl_ast_build_free(build
);
4541 return isl_ast_graft_list_alloc(ctx
, 0);
4544 depth
= isl_ast_build_get_depth(build
);
4545 if (depth
>= isl_ast_build_dim(build
, isl_dim_set
))
4546 return generate_inner_level(executed
, build
);
4548 if (isl_union_map_n_map(executed
) == 1)
4549 return generate_shifted_component(executed
, build
);
4551 return generate_components(executed
, build
);
4553 isl_union_map_free(executed
);
4554 isl_ast_build_free(build
);
4558 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4559 * internal, executed and build are the inputs to generate_code.
4560 * list collects the output.
4562 struct isl_generate_code_data
{
4564 isl_union_map
*executed
;
4565 isl_ast_build
*build
;
4567 isl_ast_graft_list
*list
;
4570 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4574 * with E the external build schedule and S the additional schedule "space",
4575 * reformulate the inverse schedule in terms of the internal schedule domain,
4580 * We first obtain a mapping
4584 * take the inverse and the product with S -> S, resulting in
4586 * [I -> S] -> [E -> S]
4588 * Applying the map to the input produces the desired result.
4590 static __isl_give isl_union_map
*internal_executed(
4591 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
4592 __isl_keep isl_ast_build
*build
)
4596 proj
= isl_ast_build_get_schedule_map(build
);
4597 proj
= isl_map_reverse(proj
);
4598 space
= isl_space_map_from_set(isl_space_copy(space
));
4599 id
= isl_map_identity(space
);
4600 proj
= isl_map_product(proj
, id
);
4601 executed
= isl_union_map_apply_domain(executed
,
4602 isl_union_map_from_map(proj
));
4606 /* Generate an AST that visits the elements in the range of data->executed
4607 * in the relative order specified by the corresponding domain element(s)
4608 * for those domain elements that belong to "set".
4609 * Add the result to data->list.
4611 * The caller ensures that "set" is a universe domain.
4612 * "space" is the space of the additional part of the schedule.
4613 * It is equal to the space of "set" if build->domain is parametric.
4614 * Otherwise, it is equal to the range of the wrapped space of "set".
4616 * If the build space is not parametric and
4617 * if isl_ast_build_node_from_schedule_map
4618 * was called from an outside user (data->internal not set), then
4619 * the (inverse) schedule refers to the external build domain and needs to
4620 * be transformed to refer to the internal build domain.
4622 * If the build space is parametric, then we add some of the parameter
4623 * constraints to the executed relation. Adding these constraints
4624 * allows for an earlier detection of conflicts in some cases.
4625 * However, we do not want to divide the executed relation into
4626 * more disjuncts than necessary. We therefore approximate
4627 * the constraints on the parameters by a single disjunct set.
4629 * The build is extended to include the additional part of the schedule.
4630 * If the original build space was not parametric, then the options
4631 * in data->build refer only to the additional part of the schedule
4632 * and they need to be adjusted to refer to the complete AST build
4635 * After having adjusted inverse schedule and build, we start generating
4636 * code with the outer loop of the current code generation
4637 * in generate_next_level.
4639 * If the original build space was not parametric, we undo the embedding
4640 * on the resulting isl_ast_node_list so that it can be used within
4641 * the outer AST build.
4643 static isl_stat
generate_code_in_space(struct isl_generate_code_data
*data
,
4644 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
4646 isl_union_map
*executed
;
4647 isl_ast_build
*build
;
4648 isl_ast_graft_list
*list
;
4651 executed
= isl_union_map_copy(data
->executed
);
4652 executed
= isl_union_map_intersect_domain(executed
,
4653 isl_union_set_from_set(set
));
4655 embed
= !isl_set_is_params(data
->build
->domain
);
4656 if (embed
&& !data
->internal
)
4657 executed
= internal_executed(executed
, space
, data
->build
);
4660 domain
= isl_ast_build_get_domain(data
->build
);
4661 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
4662 executed
= isl_union_map_intersect_params(executed
, domain
);
4665 build
= isl_ast_build_copy(data
->build
);
4666 build
= isl_ast_build_product(build
, space
);
4668 list
= generate_next_level(executed
, build
);
4670 list
= isl_ast_graft_list_unembed(list
, embed
);
4672 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
4677 /* Generate an AST that visits the elements in the range of data->executed
4678 * in the relative order specified by the corresponding domain element(s)
4679 * for those domain elements that belong to "set".
4680 * Add the result to data->list.
4682 * The caller ensures that "set" is a universe domain.
4684 * If the build space S is not parametric, then the space of "set"
4685 * need to be a wrapped relation with S as domain. That is, it needs
4690 * Check this property and pass control to generate_code_in_space
4692 * If the build space is not parametric, then T is the space of "set".
4694 static isl_stat
generate_code_set(__isl_take isl_set
*set
, void *user
)
4696 struct isl_generate_code_data
*data
= user
;
4697 isl_space
*space
, *build_space
;
4700 space
= isl_set_get_space(set
);
4702 if (isl_set_is_params(data
->build
->domain
))
4703 return generate_code_in_space(data
, set
, space
);
4705 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
4706 space
= isl_space_unwrap(space
);
4707 is_domain
= isl_space_is_domain(build_space
, space
);
4708 isl_space_free(build_space
);
4709 space
= isl_space_range(space
);
4714 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4715 "invalid nested schedule space", goto error
);
4717 return generate_code_in_space(data
, set
, space
);
4720 isl_space_free(space
);
4721 return isl_stat_error
;
4724 /* Generate an AST that visits the elements in the range of "executed"
4725 * in the relative order specified by the corresponding domain element(s).
4727 * "build" is an isl_ast_build that has either been constructed by
4728 * isl_ast_build_from_context or passed to a callback set by
4729 * isl_ast_build_set_create_leaf.
4730 * In the first case, the space of the isl_ast_build is typically
4731 * a parametric space, although this is currently not enforced.
4732 * In the second case, the space is never a parametric space.
4733 * If the space S is not parametric, then the domain space(s) of "executed"
4734 * need to be wrapped relations with S as domain.
4736 * If the domain of "executed" consists of several spaces, then an AST
4737 * is generated for each of them (in arbitrary order) and the results
4740 * If "internal" is set, then the domain "S" above refers to the internal
4741 * schedule domain representation. Otherwise, it refers to the external
4742 * representation, as returned by isl_ast_build_get_schedule_space.
4744 * We essentially run over all the spaces in the domain of "executed"
4745 * and call generate_code_set on each of them.
4747 static __isl_give isl_ast_graft_list
*generate_code(
4748 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
4752 struct isl_generate_code_data data
= { 0 };
4754 isl_union_set
*schedule_domain
;
4755 isl_union_map
*universe
;
4759 space
= isl_ast_build_get_space(build
, 1);
4760 space
= isl_space_align_params(space
,
4761 isl_union_map_get_space(executed
));
4762 space
= isl_space_align_params(space
,
4763 isl_union_map_get_space(build
->options
));
4764 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
4765 executed
= isl_union_map_align_params(executed
, space
);
4766 if (!executed
|| !build
)
4769 ctx
= isl_ast_build_get_ctx(build
);
4771 data
.internal
= internal
;
4772 data
.executed
= executed
;
4774 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
4776 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
4777 schedule_domain
= isl_union_map_domain(universe
);
4778 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
4780 data
.list
= isl_ast_graft_list_free(data
.list
);
4782 isl_union_set_free(schedule_domain
);
4783 isl_union_map_free(executed
);
4785 isl_ast_build_free(build
);
4788 isl_union_map_free(executed
);
4789 isl_ast_build_free(build
);
4793 /* Generate an AST that visits the elements in the domain of "schedule"
4794 * in the relative order specified by the corresponding image element(s).
4796 * "build" is an isl_ast_build that has either been constructed by
4797 * isl_ast_build_from_context or passed to a callback set by
4798 * isl_ast_build_set_create_leaf.
4799 * In the first case, the space of the isl_ast_build is typically
4800 * a parametric space, although this is currently not enforced.
4801 * In the second case, the space is never a parametric space.
4802 * If the space S is not parametric, then the range space(s) of "schedule"
4803 * need to be wrapped relations with S as domain.
4805 * If the range of "schedule" consists of several spaces, then an AST
4806 * is generated for each of them (in arbitrary order) and the results
4809 * We first initialize the local copies of the relevant options.
4810 * We do this here rather than when the isl_ast_build is created
4811 * because the options may have changed between the construction
4812 * of the isl_ast_build and the call to isl_generate_code.
4814 * The main computation is performed on an inverse schedule (with
4815 * the schedule domain in the domain and the elements to be executed
4816 * in the range) called "executed".
4818 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule_map(
4819 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
4821 isl_ast_graft_list
*list
;
4823 isl_union_map
*executed
;
4825 build
= isl_ast_build_copy(build
);
4826 build
= isl_ast_build_set_single_valued(build
, 0);
4827 schedule
= isl_union_map_coalesce(schedule
);
4828 schedule
= isl_union_map_remove_redundancies(schedule
);
4829 executed
= isl_union_map_reverse(schedule
);
4830 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
4831 node
= isl_ast_node_from_graft_list(list
, build
);
4832 isl_ast_build_free(build
);
4837 /* The old name for isl_ast_build_node_from_schedule_map.
4838 * It is being kept for backward compatibility, but
4839 * it will be removed in the future.
4841 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
4842 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
4844 return isl_ast_build_node_from_schedule_map(build
, schedule
);
4847 /* Generate an AST that visits the elements in the domain of "executed"
4848 * in the relative order specified by the band node "node" and its descendants.
4850 * The relation "executed" maps the outer generated loop iterators
4851 * to the domain elements executed by those iterations.
4853 * If the band is empty, we continue with its descendants.
4854 * Otherwise, we extend the build and the inverse schedule with
4855 * the additional space/partial schedule and continue generating
4856 * an AST in generate_next_level.
4857 * As soon as we have extended the inverse schedule with the additional
4858 * partial schedule, we look for equalities that may exists between
4859 * the old and the new part.
4861 static __isl_give isl_ast_graft_list
*build_ast_from_band(
4862 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
4863 __isl_take isl_union_map
*executed
)
4866 isl_multi_union_pw_aff
*extra
;
4867 isl_union_map
*extra_umap
;
4868 isl_ast_graft_list
*list
;
4871 if (!build
|| !node
|| !executed
)
4874 if (isl_schedule_node_band_n_member(node
) == 0)
4875 return build_ast_from_child(build
, node
, executed
);
4877 extra
= isl_schedule_node_band_get_partial_schedule(node
);
4878 extra
= isl_multi_union_pw_aff_align_params(extra
,
4879 isl_ast_build_get_space(build
, 1));
4880 space
= isl_multi_union_pw_aff_get_space(extra
);
4882 extra_umap
= isl_union_map_from_multi_union_pw_aff(extra
);
4883 extra_umap
= isl_union_map_reverse(extra_umap
);
4885 executed
= isl_union_map_domain_product(executed
, extra_umap
);
4886 executed
= isl_union_map_detect_equalities(executed
);
4888 n1
= isl_ast_build_dim(build
, isl_dim_param
);
4889 build
= isl_ast_build_product(build
, space
);
4890 n2
= isl_ast_build_dim(build
, isl_dim_param
);
4892 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
4893 "band node is not allowed to introduce new parameters",
4894 build
= isl_ast_build_free(build
));
4895 build
= isl_ast_build_set_schedule_node(build
, node
);
4897 list
= generate_next_level(executed
, build
);
4899 list
= isl_ast_graft_list_unembed(list
, 1);
4903 isl_schedule_node_free(node
);
4904 isl_union_map_free(executed
);
4905 isl_ast_build_free(build
);
4909 /* Hoist a list of grafts (in practice containing a single graft)
4910 * from "sub_build" (which includes extra context information)
4913 * In particular, project out all additional parameters introduced
4914 * by the context node from the enforced constraints and the guard
4915 * of the single graft.
4917 static __isl_give isl_ast_graft_list
*hoist_out_of_context(
4918 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
,
4919 __isl_keep isl_ast_build
*sub_build
)
4921 isl_ast_graft
*graft
;
4922 isl_basic_set
*enforced
;
4924 unsigned n_param
, extra_param
;
4926 if (!build
|| !sub_build
)
4927 return isl_ast_graft_list_free(list
);
4929 n_param
= isl_ast_build_dim(build
, isl_dim_param
);
4930 extra_param
= isl_ast_build_dim(sub_build
, isl_dim_param
);
4932 if (extra_param
== n_param
)
4935 extra_param
-= n_param
;
4936 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, sub_build
);
4937 enforced
= isl_basic_set_project_out(enforced
, isl_dim_param
,
4938 n_param
, extra_param
);
4939 enforced
= isl_basic_set_remove_unknown_divs(enforced
);
4940 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
4941 guard
= isl_set_remove_divs_involving_dims(guard
, isl_dim_param
,
4942 n_param
, extra_param
);
4943 guard
= isl_set_project_out(guard
, isl_dim_param
, n_param
, extra_param
);
4944 guard
= isl_set_compute_divs(guard
);
4945 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
4947 list
= isl_ast_graft_list_from_ast_graft(graft
);
4952 /* Generate an AST that visits the elements in the domain of "executed"
4953 * in the relative order specified by the context node "node"
4954 * and its descendants.
4956 * The relation "executed" maps the outer generated loop iterators
4957 * to the domain elements executed by those iterations.
4959 * The context node may introduce additional parameters as well as
4960 * constraints on the outer schedule dimenions or original parameters.
4962 * We add the extra parameters to a new build and the context
4963 * constraints to both the build and (as a single disjunct)
4964 * to the domain of "executed". Since the context constraints
4965 * are specified in terms of the input schedule, we first need
4966 * to map them to the internal schedule domain.
4968 * After constructing the AST from the descendants of "node",
4969 * we combine the list of grafts into a single graft within
4970 * the new build, in order to be able to exploit the additional
4971 * context constraints during this combination.
4973 * Additionally, if the current node is the outermost node in
4974 * the schedule tree (apart from the root domain node), we generate
4975 * all pending guards, again to be able to exploit the additional
4976 * context constraints. We currently do not do this for internal
4977 * context nodes since we may still want to hoist conditions
4978 * to outer AST nodes.
4980 * If the context node introduced any new parameters, then they
4981 * are removed from the set of enforced constraints and guard
4982 * in hoist_out_of_context.
4984 static __isl_give isl_ast_graft_list
*build_ast_from_context(
4985 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
4986 __isl_take isl_union_map
*executed
)
4990 isl_multi_aff
*internal2input
;
4991 isl_ast_build
*sub_build
;
4992 isl_ast_graft_list
*list
;
4995 depth
= isl_schedule_node_get_tree_depth(node
);
4996 space
= isl_ast_build_get_space(build
, 1);
4997 context
= isl_schedule_node_context_get_context(node
);
4998 context
= isl_set_align_params(context
, space
);
4999 sub_build
= isl_ast_build_copy(build
);
5000 space
= isl_set_get_space(context
);
5001 sub_build
= isl_ast_build_align_params(sub_build
, space
);
5002 internal2input
= isl_ast_build_get_internal2input(sub_build
);
5003 context
= isl_set_preimage_multi_aff(context
, internal2input
);
5004 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5005 isl_set_copy(context
));
5006 context
= isl_set_from_basic_set(isl_set_simple_hull(context
));
5007 executed
= isl_union_map_intersect_domain(executed
,
5008 isl_union_set_from_set(context
));
5010 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5012 n
= isl_ast_graft_list_n_ast_graft(list
);
5014 list
= isl_ast_graft_list_free(list
);
5016 list
= isl_ast_graft_list_fuse(list
, sub_build
);
5018 list
= isl_ast_graft_list_insert_pending_guard_nodes(list
,
5021 list
= hoist_out_of_context(list
, build
, sub_build
);
5023 isl_ast_build_free(build
);
5024 isl_ast_build_free(sub_build
);
5029 /* Generate an AST that visits the elements in the domain of "executed"
5030 * in the relative order specified by the expansion node "node" and
5033 * The relation "executed" maps the outer generated loop iterators
5034 * to the domain elements executed by those iterations.
5036 * We expand the domain elements by the expansion and
5037 * continue with the descendants of the node.
5039 static __isl_give isl_ast_graft_list
*build_ast_from_expansion(
5040 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5041 __isl_take isl_union_map
*executed
)
5043 isl_union_map
*expansion
;
5046 expansion
= isl_schedule_node_expansion_get_expansion(node
);
5047 expansion
= isl_union_map_align_params(expansion
,
5048 isl_union_map_get_space(executed
));
5050 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5051 executed
= isl_union_map_apply_range(executed
, expansion
);
5052 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5054 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5055 "expansion node is not allowed to introduce "
5056 "new parameters", goto error
);
5058 return build_ast_from_child(build
, node
, executed
);
5060 isl_ast_build_free(build
);
5061 isl_schedule_node_free(node
);
5062 isl_union_map_free(executed
);
5066 /* Generate an AST that visits the elements in the domain of "executed"
5067 * in the relative order specified by the extension node "node" and
5070 * The relation "executed" maps the outer generated loop iterators
5071 * to the domain elements executed by those iterations.
5073 * Extend the inverse schedule with the extension applied to current
5074 * set of generated constraints. Since the extension if formulated
5075 * in terms of the input schedule, it first needs to be transformed
5076 * to refer to the internal schedule.
5078 static __isl_give isl_ast_graft_list
*build_ast_from_extension(
5079 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5080 __isl_take isl_union_map
*executed
)
5082 isl_union_set
*schedule_domain
;
5083 isl_union_map
*extension
;
5086 set
= isl_ast_build_get_generated(build
);
5087 schedule_domain
= isl_union_set_from_set(set
);
5089 extension
= isl_schedule_node_extension_get_extension(node
);
5091 extension
= isl_union_map_preimage_domain_multi_aff(extension
,
5092 isl_multi_aff_copy(build
->internal2input
));
5093 extension
= isl_union_map_intersect_domain(extension
, schedule_domain
);
5094 extension
= isl_ast_build_substitute_values_union_map_domain(build
,
5096 executed
= isl_union_map_union(executed
, extension
);
5098 return build_ast_from_child(build
, node
, executed
);
5101 /* Generate an AST that visits the elements in the domain of "executed"
5102 * in the relative order specified by the filter node "node" and
5105 * The relation "executed" maps the outer generated loop iterators
5106 * to the domain elements executed by those iterations.
5108 * We simply intersect the iteration domain (i.e., the range of "executed")
5109 * with the filter and continue with the descendants of the node,
5110 * unless the resulting inverse schedule is empty, in which
5111 * case we return an empty list.
5113 static __isl_give isl_ast_graft_list
*build_ast_from_filter(
5114 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5115 __isl_take isl_union_map
*executed
)
5118 isl_union_set
*filter
;
5119 isl_ast_graft_list
*list
;
5123 if (!build
|| !node
|| !executed
)
5126 filter
= isl_schedule_node_filter_get_filter(node
);
5127 filter
= isl_union_set_align_params(filter
,
5128 isl_union_map_get_space(executed
));
5129 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5130 executed
= isl_union_map_intersect_range(executed
, filter
);
5131 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5133 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5134 "filter node is not allowed to introduce "
5135 "new parameters", goto error
);
5137 empty
= isl_union_map_is_empty(executed
);
5141 return build_ast_from_child(build
, node
, executed
);
5143 ctx
= isl_ast_build_get_ctx(build
);
5144 list
= isl_ast_graft_list_alloc(ctx
, 0);
5145 isl_ast_build_free(build
);
5146 isl_schedule_node_free(node
);
5147 isl_union_map_free(executed
);
5150 isl_ast_build_free(build
);
5151 isl_schedule_node_free(node
);
5152 isl_union_map_free(executed
);
5156 /* Generate an AST that visits the elements in the domain of "executed"
5157 * in the relative order specified by the guard node "node" and
5160 * The relation "executed" maps the outer generated loop iterators
5161 * to the domain elements executed by those iterations.
5163 * Ensure that the associated guard is enforced by the outer AST
5164 * constructs by adding it to the guard of the graft.
5165 * Since we know that we will enforce the guard, we can also include it
5166 * in the generated constraints used to construct an AST for
5167 * the descendant nodes.
5169 static __isl_give isl_ast_graft_list
*build_ast_from_guard(
5170 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5171 __isl_take isl_union_map
*executed
)
5174 isl_set
*guard
, *hoisted
;
5175 isl_basic_set
*enforced
;
5176 isl_ast_build
*sub_build
;
5177 isl_ast_graft
*graft
;
5178 isl_ast_graft_list
*list
;
5181 space
= isl_ast_build_get_space(build
, 1);
5182 guard
= isl_schedule_node_guard_get_guard(node
);
5183 n1
= isl_space_dim(space
, isl_dim_param
);
5184 guard
= isl_set_align_params(guard
, space
);
5185 n2
= isl_set_dim(guard
, isl_dim_param
);
5187 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5188 "guard node is not allowed to introduce "
5189 "new parameters", guard
= isl_set_free(guard
));
5190 guard
= isl_set_preimage_multi_aff(guard
,
5191 isl_multi_aff_copy(build
->internal2input
));
5192 guard
= isl_ast_build_specialize(build
, guard
);
5193 guard
= isl_set_gist(guard
, isl_set_copy(build
->generated
));
5195 sub_build
= isl_ast_build_copy(build
);
5196 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5197 isl_set_copy(guard
));
5199 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5202 hoisted
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5203 if (isl_set_n_basic_set(hoisted
) > 1)
5204 list
= isl_ast_graft_list_gist_guards(list
,
5205 isl_set_copy(hoisted
));
5206 guard
= isl_set_intersect(guard
, hoisted
);
5207 enforced
= extract_shared_enforced(list
, build
);
5208 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5211 isl_ast_build_free(sub_build
);
5212 isl_ast_build_free(build
);
5213 return isl_ast_graft_list_from_ast_graft(graft
);
5216 /* Call the before_each_mark callback, if requested by the user.
5218 * Return 0 on success and -1 on error.
5220 * The caller is responsible for recording the current inverse schedule
5223 static isl_stat
before_each_mark(__isl_keep isl_id
*mark
,
5224 __isl_keep isl_ast_build
*build
)
5227 return isl_stat_error
;
5228 if (!build
->before_each_mark
)
5230 return build
->before_each_mark(mark
, build
,
5231 build
->before_each_mark_user
);
5234 /* Call the after_each_mark callback, if requested by the user.
5236 * The caller is responsible for recording the current inverse schedule
5239 static __isl_give isl_ast_graft
*after_each_mark(
5240 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
5242 if (!graft
|| !build
)
5243 return isl_ast_graft_free(graft
);
5244 if (!build
->after_each_mark
)
5246 graft
->node
= build
->after_each_mark(graft
->node
, build
,
5247 build
->after_each_mark_user
);
5249 return isl_ast_graft_free(graft
);
5254 /* Generate an AST that visits the elements in the domain of "executed"
5255 * in the relative order specified by the mark node "node" and
5258 * The relation "executed" maps the outer generated loop iterators
5259 * to the domain elements executed by those iterations.
5261 * Since we may be calling before_each_mark and after_each_mark
5262 * callbacks, we record the current inverse schedule in the build.
5264 * We generate an AST for the child of the mark node, combine
5265 * the graft list into a single graft and then insert the mark
5266 * in the AST of that single graft.
5268 static __isl_give isl_ast_graft_list
*build_ast_from_mark(
5269 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5270 __isl_take isl_union_map
*executed
)
5273 isl_ast_graft
*graft
;
5274 isl_ast_graft_list
*list
;
5277 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
5279 mark
= isl_schedule_node_mark_get_id(node
);
5280 if (before_each_mark(mark
, build
) < 0)
5281 node
= isl_schedule_node_free(node
);
5283 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5284 list
= isl_ast_graft_list_fuse(list
, build
);
5285 n
= isl_ast_graft_list_n_ast_graft(list
);
5287 list
= isl_ast_graft_list_free(list
);
5291 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
5292 graft
= isl_ast_graft_insert_mark(graft
, mark
);
5293 graft
= after_each_mark(graft
, build
);
5294 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
5296 isl_ast_build_free(build
);
5301 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5302 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5303 __isl_take isl_union_map
*executed
);
5305 /* Generate an AST that visits the elements in the domain of "executed"
5306 * in the relative order specified by the sequence (or set) node "node" and
5309 * The relation "executed" maps the outer generated loop iterators
5310 * to the domain elements executed by those iterations.
5312 * We simply generate an AST for each of the children and concatenate
5315 static __isl_give isl_ast_graft_list
*build_ast_from_sequence(
5316 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5317 __isl_take isl_union_map
*executed
)
5321 isl_ast_graft_list
*list
;
5323 ctx
= isl_ast_build_get_ctx(build
);
5324 list
= isl_ast_graft_list_alloc(ctx
, 0);
5326 n
= isl_schedule_node_n_children(node
);
5327 for (i
= 0; i
< n
; ++i
) {
5328 isl_schedule_node
*child
;
5329 isl_ast_graft_list
*list_i
;
5331 child
= isl_schedule_node_get_child(node
, i
);
5332 list_i
= build_ast_from_schedule_node(isl_ast_build_copy(build
),
5333 child
, isl_union_map_copy(executed
));
5334 list
= isl_ast_graft_list_concat(list
, list_i
);
5336 isl_ast_build_free(build
);
5337 isl_schedule_node_free(node
);
5338 isl_union_map_free(executed
);
5343 /* Generate an AST that visits the elements in the domain of "executed"
5344 * in the relative order specified by the node "node" and its descendants.
5346 * The relation "executed" maps the outer generated loop iterators
5347 * to the domain elements executed by those iterations.
5349 * If the node is a leaf, then we pass control to generate_inner_level.
5350 * Note that the current build does not refer to any band node, so
5351 * that generate_inner_level will not try to visit the child of
5354 * The other node types are handled in separate functions.
5355 * Set nodes are currently treated in the same way as sequence nodes.
5356 * The children of a set node may be executed in any order,
5357 * including the order of the children.
5359 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5360 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5361 __isl_take isl_union_map
*executed
)
5363 enum isl_schedule_node_type type
;
5365 type
= isl_schedule_node_get_type(node
);
5368 case isl_schedule_node_error
:
5370 case isl_schedule_node_leaf
:
5371 isl_schedule_node_free(node
);
5372 return generate_inner_level(executed
, build
);
5373 case isl_schedule_node_band
:
5374 return build_ast_from_band(build
, node
, executed
);
5375 case isl_schedule_node_context
:
5376 return build_ast_from_context(build
, node
, executed
);
5377 case isl_schedule_node_domain
:
5378 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
5379 "unexpected internal domain node", goto error
);
5380 case isl_schedule_node_expansion
:
5381 return build_ast_from_expansion(build
, node
, executed
);
5382 case isl_schedule_node_extension
:
5383 return build_ast_from_extension(build
, node
, executed
);
5384 case isl_schedule_node_filter
:
5385 return build_ast_from_filter(build
, node
, executed
);
5386 case isl_schedule_node_guard
:
5387 return build_ast_from_guard(build
, node
, executed
);
5388 case isl_schedule_node_mark
:
5389 return build_ast_from_mark(build
, node
, executed
);
5390 case isl_schedule_node_sequence
:
5391 case isl_schedule_node_set
:
5392 return build_ast_from_sequence(build
, node
, executed
);
5395 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
5396 "unhandled type", goto error
);
5398 isl_union_map_free(executed
);
5399 isl_schedule_node_free(node
);
5400 isl_ast_build_free(build
);
5405 /* Generate an AST that visits the elements in the domain of "executed"
5406 * in the relative order specified by the (single) child of "node" and
5409 * The relation "executed" maps the outer generated loop iterators
5410 * to the domain elements executed by those iterations.
5412 * This function is never called on a leaf, set or sequence node,
5413 * so the node always has exactly one child.
5415 static __isl_give isl_ast_graft_list
*build_ast_from_child(
5416 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5417 __isl_take isl_union_map
*executed
)
5419 node
= isl_schedule_node_child(node
, 0);
5420 return build_ast_from_schedule_node(build
, node
, executed
);
5423 /* Generate an AST that visits the elements in the domain of the domain
5424 * node "node" in the relative order specified by its descendants.
5426 * An initial inverse schedule is created that maps a zero-dimensional
5427 * schedule space to the node domain.
5428 * The input "build" is assumed to have a parametric domain and
5429 * is replaced by the same zero-dimensional schedule space.
5431 * We also add some of the parameter constraints in the build domain
5432 * to the executed relation. Adding these constraints
5433 * allows for an earlier detection of conflicts in some cases.
5434 * However, we do not want to divide the executed relation into
5435 * more disjuncts than necessary. We therefore approximate
5436 * the constraints on the parameters by a single disjunct set.
5438 static __isl_give isl_ast_node
*build_ast_from_domain(
5439 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
)
5442 isl_union_set
*domain
, *schedule_domain
;
5443 isl_union_map
*executed
;
5446 isl_ast_graft_list
*list
;
5453 ctx
= isl_ast_build_get_ctx(build
);
5454 space
= isl_ast_build_get_space(build
, 1);
5455 is_params
= isl_space_is_params(space
);
5456 isl_space_free(space
);
5460 isl_die(ctx
, isl_error_unsupported
,
5461 "expecting parametric initial context", goto error
);
5463 domain
= isl_schedule_node_domain_get_domain(node
);
5464 domain
= isl_union_set_coalesce(domain
);
5466 space
= isl_union_set_get_space(domain
);
5467 space
= isl_space_set_from_params(space
);
5468 build
= isl_ast_build_product(build
, space
);
5470 set
= isl_ast_build_get_domain(build
);
5471 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5472 schedule_domain
= isl_union_set_from_set(set
);
5474 executed
= isl_union_map_from_domain_and_range(schedule_domain
, domain
);
5475 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5476 ast
= isl_ast_node_from_graft_list(list
, build
);
5477 isl_ast_build_free(build
);
5481 isl_schedule_node_free(node
);
5482 isl_ast_build_free(build
);
5486 /* Generate an AST that visits the elements in the domain of "schedule"
5487 * in the relative order specified by the schedule tree.
5489 * "build" is an isl_ast_build that has been created using
5490 * isl_ast_build_alloc or isl_ast_build_from_context based
5491 * on a parametric set.
5493 * The construction starts at the root node of the schedule,
5494 * which is assumed to be a domain node.
5496 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule(
5497 __isl_keep isl_ast_build
*build
, __isl_take isl_schedule
*schedule
)
5500 isl_schedule_node
*node
;
5502 if (!build
|| !schedule
)
5505 ctx
= isl_ast_build_get_ctx(build
);
5507 node
= isl_schedule_get_root(schedule
);
5508 isl_schedule_free(schedule
);
5510 build
= isl_ast_build_copy(build
);
5511 build
= isl_ast_build_set_single_valued(build
, 0);
5512 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
5513 isl_die(ctx
, isl_error_unsupported
,
5514 "expecting root domain node",
5515 build
= isl_ast_build_free(build
));
5516 return build_ast_from_domain(build
, node
);
5518 isl_schedule_free(schedule
);