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 a call expression for the single executed
113 * domain element "map" and put a guard around it based its (simplified)
114 * domain. "executed" is the original inverse schedule from which "map"
115 * has been derived. In particular, "map" is either identical to "executed"
116 * or it is the result of gisting "executed" with respect to the build domain.
117 * "executed" is only used if there is an at_each_domain callback.
119 * At this stage, any pending constraints in the build can no longer
120 * be simplified with respect to any enforced constraints since
121 * the call node does not have any enforced constraints.
122 * Since all pending constraints not covered by any enforced constraints
123 * will be added as a guard to the graft in create_node_scaled,
124 * even in the eliminated case, the pending constraints
125 * can be considered to have been generated by outer constructs.
127 * If the user has set an at_each_domain callback, it is called
128 * on the constructed call expression node.
130 static isl_stat
add_domain(__isl_take isl_map
*executed
,
131 __isl_take isl_map
*map
, struct isl_generate_domain_data
*data
)
133 isl_ast_build
*build
;
134 isl_ast_graft
*graft
;
135 isl_ast_graft_list
*list
;
136 isl_set
*guard
, *pending
;
138 build
= isl_ast_build_copy(data
->build
);
139 pending
= isl_ast_build_get_pending(build
);
140 build
= isl_ast_build_replace_pending_by_guard(build
, pending
);
142 guard
= isl_map_domain(isl_map_copy(map
));
143 guard
= isl_set_compute_divs(guard
);
144 guard
= isl_set_coalesce(guard
);
145 guard
= isl_set_gist(guard
, isl_ast_build_get_generated(build
));
146 guard
= isl_ast_build_specialize(build
, guard
);
148 graft
= isl_ast_graft_alloc_domain(map
, build
);
149 graft
= at_each_domain(graft
, executed
, build
);
150 isl_ast_build_free(build
);
151 isl_map_free(executed
);
152 graft
= isl_ast_graft_add_guard(graft
, guard
, data
->build
);
154 list
= isl_ast_graft_list_from_ast_graft(graft
);
155 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
160 /* Generate an AST for a single domain based on
161 * the inverse schedule "executed" and add it to data->list.
163 * If there is more than one domain element associated to the current
164 * schedule "time", then we need to continue the generation process
165 * in generate_non_single_valued.
166 * Note that the inverse schedule being single-valued may depend
167 * on constraints that are only available in the original context
168 * domain specified by the user. We therefore first introduce
169 * some of the constraints of data->build->domain. In particular,
170 * we intersect with a single-disjunct approximation of this set.
171 * We perform this approximation to avoid further splitting up
172 * the executed relation, possibly introducing a disjunctive guard
175 * On the other hand, we only perform the test after having taken the gist
176 * of the domain as the resulting map is the one from which the call
177 * expression is constructed. Using this map to construct the call
178 * expression usually yields simpler results in cases where the original
179 * map is not obviously single-valued.
180 * If the original map is obviously single-valued, then the gist
181 * operation is skipped.
183 * Because we perform the single-valuedness test on the gisted map,
184 * we may in rare cases fail to recognize that the inverse schedule
185 * is single-valued. This becomes problematic if this happens
186 * from the recursive call through generate_non_single_valued
187 * as we would then end up in an infinite recursion.
188 * We therefore check if we are inside a call to generate_non_single_valued
189 * and revert to the ungisted map if the gisted map turns out not to be
192 * Otherwise, call add_domain to generate a call expression (with guard) and
193 * to call the at_each_domain callback, if any.
195 static isl_stat
generate_domain(__isl_take isl_map
*executed
, void *user
)
197 struct isl_generate_domain_data
*data
= user
;
202 domain
= isl_ast_build_get_domain(data
->build
);
203 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
204 executed
= isl_map_intersect_domain(executed
, domain
);
205 empty
= isl_map_is_empty(executed
);
209 isl_map_free(executed
);
213 sv
= isl_map_plain_is_single_valued(executed
);
217 return add_domain(executed
, isl_map_copy(executed
), data
);
219 executed
= isl_map_coalesce(executed
);
220 map
= isl_map_copy(executed
);
221 map
= isl_ast_build_compute_gist_map_domain(data
->build
, map
);
222 sv
= isl_map_is_single_valued(map
);
227 if (data
->build
->single_valued
)
228 map
= isl_map_copy(executed
);
230 return generate_non_single_valued(executed
, data
);
233 return add_domain(executed
, map
, data
);
236 isl_map_free(executed
);
237 return isl_stat_error
;
240 /* Call build->create_leaf to a create "leaf" node in the AST,
241 * encapsulate the result in an isl_ast_graft and return the result
242 * as a 1-element list.
244 * Note that the node returned by the user may be an entire tree.
246 * Since the node itself cannot enforce any constraints, we turn
247 * all pending constraints into guards and add them to the resulting
248 * graft to ensure that they will be generated.
250 * Before we pass control to the user, we first clear some information
251 * from the build that is (presumbably) only meaningful
252 * for the current code generation.
253 * This includes the create_leaf callback itself, so we make a copy
254 * of the build first.
256 static __isl_give isl_ast_graft_list
*call_create_leaf(
257 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
261 isl_ast_graft
*graft
;
262 isl_ast_build
*user_build
;
264 guard
= isl_ast_build_get_pending(build
);
265 user_build
= isl_ast_build_copy(build
);
266 user_build
= isl_ast_build_replace_pending_by_guard(user_build
,
267 isl_set_copy(guard
));
268 user_build
= isl_ast_build_set_executed(user_build
, executed
);
269 user_build
= isl_ast_build_clear_local_info(user_build
);
273 node
= build
->create_leaf(user_build
, build
->create_leaf_user
);
274 graft
= isl_ast_graft_alloc(node
, build
);
275 graft
= isl_ast_graft_add_guard(graft
, guard
, build
);
276 isl_ast_build_free(build
);
277 return isl_ast_graft_list_from_ast_graft(graft
);
280 static __isl_give isl_ast_graft_list
*build_ast_from_child(
281 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
282 __isl_take isl_union_map
*executed
);
284 /* Generate an AST after having handled the complete schedule
285 * of this call to the code generator or the complete band
286 * if we are generating an AST from a schedule tree.
288 * If we are inside a band node, then move on to the child of the band.
290 * If the user has specified a create_leaf callback, control
291 * is passed to the user in call_create_leaf.
293 * Otherwise, we generate one or more calls for each individual
294 * domain in generate_domain.
296 static __isl_give isl_ast_graft_list
*generate_inner_level(
297 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
300 struct isl_generate_domain_data data
= { build
};
302 if (!build
|| !executed
)
305 if (isl_ast_build_has_schedule_node(build
)) {
306 isl_schedule_node
*node
;
307 node
= isl_ast_build_get_schedule_node(build
);
308 build
= isl_ast_build_reset_schedule_node(build
);
309 return build_ast_from_child(build
, node
, executed
);
312 if (build
->create_leaf
)
313 return call_create_leaf(executed
, build
);
315 ctx
= isl_union_map_get_ctx(executed
);
316 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
317 if (isl_union_map_foreach_map(executed
, &generate_domain
, &data
) < 0)
318 data
.list
= isl_ast_graft_list_free(data
.list
);
321 error
: data
.list
= NULL
;
322 isl_ast_build_free(build
);
323 isl_union_map_free(executed
);
327 /* Call the before_each_for callback, if requested by the user.
329 static __isl_give isl_ast_node
*before_each_for(__isl_take isl_ast_node
*node
,
330 __isl_keep isl_ast_build
*build
)
335 return isl_ast_node_free(node
);
336 if (!build
->before_each_for
)
338 id
= build
->before_each_for(build
, build
->before_each_for_user
);
339 node
= isl_ast_node_set_annotation(node
, id
);
343 /* Call the after_each_for callback, if requested by the user.
345 static __isl_give isl_ast_graft
*after_each_for(__isl_take isl_ast_graft
*graft
,
346 __isl_keep isl_ast_build
*build
)
348 if (!graft
|| !build
)
349 return isl_ast_graft_free(graft
);
350 if (!build
->after_each_for
)
352 graft
->node
= build
->after_each_for(graft
->node
, build
,
353 build
->after_each_for_user
);
355 return isl_ast_graft_free(graft
);
359 /* Plug in all the know values of the current and outer dimensions
360 * in the domain of "executed". In principle, we only need to plug
361 * in the known value of the current dimension since the values of
362 * outer dimensions have been plugged in already.
363 * However, it turns out to be easier to just plug in all known values.
365 static __isl_give isl_union_map
*plug_in_values(
366 __isl_take isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
368 return isl_ast_build_substitute_values_union_map_domain(build
,
372 /* Check if the constraint "c" is a lower bound on dimension "pos",
373 * an upper bound, or independent of dimension "pos".
375 static int constraint_type(isl_constraint
*c
, int pos
)
377 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, pos
))
379 if (isl_constraint_is_upper_bound(c
, isl_dim_set
, pos
))
384 /* Compare the types of the constraints "a" and "b",
385 * resulting in constraints that are independent of "depth"
386 * to be sorted before the lower bounds on "depth", which in
387 * turn are sorted before the upper bounds on "depth".
389 static int cmp_constraint(__isl_keep isl_constraint
*a
,
390 __isl_keep isl_constraint
*b
, void *user
)
393 int t1
= constraint_type(a
, *depth
);
394 int t2
= constraint_type(b
, *depth
);
399 /* Extract a lower bound on dimension "pos" from constraint "c".
401 * If the constraint is of the form
405 * then we essentially return
407 * l = ceil(-f(...)/a)
409 * However, if the current dimension is strided, then we need to make
410 * sure that the lower bound we construct is of the form
414 * with f the offset and s the stride.
415 * We therefore compute
417 * f + s * ceil((l - f)/s)
419 static __isl_give isl_aff
*lower_bound(__isl_keep isl_constraint
*c
,
420 int pos
, __isl_keep isl_ast_build
*build
)
424 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
425 aff
= isl_aff_ceil(aff
);
427 if (isl_ast_build_has_stride(build
, pos
)) {
431 offset
= isl_ast_build_get_offset(build
, pos
);
432 stride
= isl_ast_build_get_stride(build
, pos
);
434 aff
= isl_aff_sub(aff
, isl_aff_copy(offset
));
435 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(stride
));
436 aff
= isl_aff_ceil(aff
);
437 aff
= isl_aff_scale_val(aff
, stride
);
438 aff
= isl_aff_add(aff
, offset
);
441 aff
= isl_ast_build_compute_gist_aff(build
, aff
);
446 /* Return the exact lower bound (or upper bound if "upper" is set)
447 * of "domain" as a piecewise affine expression.
449 * If we are computing a lower bound (of a strided dimension), then
450 * we need to make sure it is of the form
454 * where f is the offset and s is the stride.
455 * We therefore need to include the stride constraint before computing
458 static __isl_give isl_pw_aff
*exact_bound(__isl_keep isl_set
*domain
,
459 __isl_keep isl_ast_build
*build
, int upper
)
464 isl_pw_multi_aff
*pma
;
466 domain
= isl_set_copy(domain
);
468 stride
= isl_ast_build_get_stride_constraint(build
);
469 domain
= isl_set_intersect(domain
, stride
);
471 it_map
= isl_ast_build_map_to_iterator(build
, domain
);
473 pma
= isl_map_lexmax_pw_multi_aff(it_map
);
475 pma
= isl_map_lexmin_pw_multi_aff(it_map
);
476 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
477 isl_pw_multi_aff_free(pma
);
478 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
479 pa
= isl_pw_aff_coalesce(pa
);
484 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
485 * remove_redundant_lower_bounds.
487 static int reduce_list_cmp(__isl_keep isl_pw_aff
*a
, __isl_keep isl_pw_aff
*b
,
490 return isl_pw_aff_plain_cmp(a
, b
);
493 /* Given a list of lower bounds "list", remove those that are redundant
494 * with respect to the other bounds in "list" and the domain of "build".
496 * We first sort the bounds in the same way as they would be sorted
497 * by set_for_node_expressions so that we can try and remove the last
500 * For a lower bound to be effective, there needs to be at least
501 * one domain element for which it is larger than all other lower bounds.
502 * For each lower bound we therefore intersect the domain with
503 * the conditions that it is larger than all other bounds and
504 * check whether the result is empty. If so, the bound can be removed.
506 static __isl_give isl_pw_aff_list
*remove_redundant_lower_bounds(
507 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
512 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
516 n
= isl_pw_aff_list_n_pw_aff(list
);
520 domain
= isl_ast_build_get_domain(build
);
522 for (i
= n
- 1; i
>= 0; --i
) {
527 domain_i
= isl_set_copy(domain
);
528 pa_i
= isl_pw_aff_list_get_pw_aff(list
, i
);
530 for (j
= 0; j
< n
; ++j
) {
537 pa_j
= isl_pw_aff_list_get_pw_aff(list
, j
);
538 better
= isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i
), pa_j
);
539 domain_i
= isl_set_intersect(domain_i
, better
);
542 empty
= isl_set_is_empty(domain_i
);
544 isl_set_free(domain_i
);
545 isl_pw_aff_free(pa_i
);
551 list
= isl_pw_aff_list_drop(list
, i
, 1);
555 isl_set_free(domain
);
559 isl_set_free(domain
);
560 return isl_pw_aff_list_free(list
);
563 /* Extract a lower bound on dimension "pos" from each constraint
564 * in "constraints" and return the list of lower bounds.
565 * If "constraints" has zero elements, then we extract a lower bound
566 * from "domain" instead.
568 * If the current dimension is strided, then the lower bound
569 * is adjusted by lower_bound to match the stride information.
570 * This modification may make one or more lower bounds redundant
571 * with respect to the other lower bounds. We therefore check
572 * for this condition and remove the redundant lower bounds.
574 static __isl_give isl_pw_aff_list
*lower_bounds(
575 __isl_keep isl_constraint_list
*constraints
, int pos
,
576 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
579 isl_pw_aff_list
*list
;
585 n
= isl_constraint_list_n_constraint(constraints
);
588 pa
= exact_bound(domain
, build
, 0);
589 return isl_pw_aff_list_from_pw_aff(pa
);
592 ctx
= isl_ast_build_get_ctx(build
);
593 list
= isl_pw_aff_list_alloc(ctx
,n
);
595 for (i
= 0; i
< n
; ++i
) {
599 c
= isl_constraint_list_get_constraint(constraints
, i
);
600 aff
= lower_bound(c
, pos
, build
);
601 isl_constraint_free(c
);
602 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
605 if (isl_ast_build_has_stride(build
, pos
))
606 list
= remove_redundant_lower_bounds(list
, build
);
611 /* Extract an upper bound on dimension "pos" from each constraint
612 * in "constraints" and return the list of upper bounds.
613 * If "constraints" has zero elements, then we extract an upper bound
614 * from "domain" instead.
616 static __isl_give isl_pw_aff_list
*upper_bounds(
617 __isl_keep isl_constraint_list
*constraints
, int pos
,
618 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
621 isl_pw_aff_list
*list
;
624 n
= isl_constraint_list_n_constraint(constraints
);
627 pa
= exact_bound(domain
, build
, 1);
628 return isl_pw_aff_list_from_pw_aff(pa
);
631 ctx
= isl_ast_build_get_ctx(build
);
632 list
= isl_pw_aff_list_alloc(ctx
,n
);
634 for (i
= 0; i
< n
; ++i
) {
638 c
= isl_constraint_list_get_constraint(constraints
, i
);
639 aff
= isl_constraint_get_bound(c
, isl_dim_set
, pos
);
640 isl_constraint_free(c
);
641 aff
= isl_aff_floor(aff
);
642 list
= isl_pw_aff_list_add(list
, isl_pw_aff_from_aff(aff
));
648 /* Return an isl_ast_expr that performs the reduction of type "type"
649 * on AST expressions corresponding to the elements in "list".
651 * The list is assumed to contain at least one element.
652 * If the list contains exactly one element, then the returned isl_ast_expr
653 * simply computes that affine expression.
654 * If the list contains more than one element, then we sort it
655 * using a fairly abitrary but hopefully reasonably stable order.
657 static __isl_give isl_ast_expr
*reduce_list(enum isl_ast_op_type type
,
658 __isl_keep isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
667 n
= isl_pw_aff_list_n_pw_aff(list
);
670 return isl_ast_build_expr_from_pw_aff_internal(build
,
671 isl_pw_aff_list_get_pw_aff(list
, 0));
673 ctx
= isl_pw_aff_list_get_ctx(list
);
674 expr
= isl_ast_expr_alloc_op(ctx
, type
, n
);
678 list
= isl_pw_aff_list_copy(list
);
679 list
= isl_pw_aff_list_sort(list
, &reduce_list_cmp
, NULL
);
681 return isl_ast_expr_free(expr
);
683 for (i
= 0; i
< n
; ++i
) {
684 isl_ast_expr
*expr_i
;
686 expr_i
= isl_ast_build_expr_from_pw_aff_internal(build
,
687 isl_pw_aff_list_get_pw_aff(list
, i
));
690 expr
->u
.op
.args
[i
] = expr_i
;
693 isl_pw_aff_list_free(list
);
696 isl_pw_aff_list_free(list
);
697 isl_ast_expr_free(expr
);
701 /* Add guards implied by the "generated constraints",
702 * but not (necessarily) enforced by the generated AST to "guard".
703 * In particular, if there is any stride constraints,
704 * then add the guard implied by those constraints.
705 * If we have generated a degenerate loop, then add the guard
706 * implied by "bounds" on the outer dimensions, i.e., the guard
707 * that ensures that the single value actually exists.
708 * Since there may also be guards implied by a combination
709 * of these constraints, we first combine them before
710 * deriving the implied constraints.
712 static __isl_give isl_set
*add_implied_guards(__isl_take isl_set
*guard
,
713 int degenerate
, __isl_keep isl_basic_set
*bounds
,
714 __isl_keep isl_ast_build
*build
)
716 int depth
, has_stride
;
720 depth
= isl_ast_build_get_depth(build
);
721 has_stride
= isl_ast_build_has_stride(build
, depth
);
722 if (!has_stride
&& !degenerate
)
725 space
= isl_basic_set_get_space(bounds
);
726 dom
= isl_set_universe(space
);
729 bounds
= isl_basic_set_copy(bounds
);
730 bounds
= isl_basic_set_drop_constraints_not_involving_dims(
731 bounds
, isl_dim_set
, depth
, 1);
732 set
= isl_set_from_basic_set(bounds
);
733 dom
= isl_set_intersect(dom
, set
);
737 set
= isl_ast_build_get_stride_constraint(build
);
738 dom
= isl_set_intersect(dom
, set
);
741 dom
= isl_set_eliminate(dom
, isl_dim_set
, depth
, 1);
742 dom
= isl_ast_build_compute_gist(build
, dom
);
743 guard
= isl_set_intersect(guard
, dom
);
748 /* Update "graft" based on "sub_build" for the degenerate case.
750 * "build" is the build in which graft->node was created
751 * "sub_build" contains information about the current level itself,
752 * including the single value attained.
754 * We set the initialization part of the for loop to the single
755 * value attained by the current dimension.
756 * The increment and condition are not strictly needed as the are known
757 * to be "1" and "iterator <= value" respectively.
759 static __isl_give isl_ast_graft
*refine_degenerate(
760 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
,
761 __isl_keep isl_ast_build
*sub_build
)
765 if (!graft
|| !sub_build
)
766 return isl_ast_graft_free(graft
);
768 value
= isl_pw_aff_copy(sub_build
->value
);
770 graft
->node
->u
.f
.init
= isl_ast_build_expr_from_pw_aff_internal(build
,
772 if (!graft
->node
->u
.f
.init
)
773 return isl_ast_graft_free(graft
);
778 /* Return the intersection of constraints in "list" as a set.
780 static __isl_give isl_set
*intersect_constraints(
781 __isl_keep isl_constraint_list
*list
)
786 n
= isl_constraint_list_n_constraint(list
);
788 isl_die(isl_constraint_list_get_ctx(list
), isl_error_internal
,
789 "expecting at least one constraint", return NULL
);
791 bset
= isl_basic_set_from_constraint(
792 isl_constraint_list_get_constraint(list
, 0));
793 for (i
= 1; i
< n
; ++i
) {
794 isl_basic_set
*bset_i
;
796 bset_i
= isl_basic_set_from_constraint(
797 isl_constraint_list_get_constraint(list
, i
));
798 bset
= isl_basic_set_intersect(bset
, bset_i
);
801 return isl_set_from_basic_set(bset
);
804 /* Compute the constraints on the outer dimensions enforced by
805 * graft->node and add those constraints to graft->enforced,
806 * in case the upper bound is expressed as a set "upper".
808 * In particular, if l(...) is a lower bound in "lower", and
810 * -a i + f(...) >= 0 or a i <= f(...)
812 * is an upper bound ocnstraint on the current dimension i,
813 * then the for loop enforces the constraint
815 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
817 * We therefore simply take each lower bound in turn, plug it into
818 * the upper bounds and compute the intersection over all lower bounds.
820 * If a lower bound is a rational expression, then
821 * isl_basic_set_preimage_multi_aff will force this rational
822 * expression to have only integer values. However, the loop
823 * itself does not enforce this integrality constraint. We therefore
824 * use the ceil of the lower bounds instead of the lower bounds themselves.
825 * Other constraints will make sure that the for loop is only executed
826 * when each of the lower bounds attains an integral value.
827 * In particular, potentially rational values only occur in
828 * lower_bound if the offset is a (seemingly) rational expression,
829 * but then outer conditions will make sure that this rational expression
830 * only attains integer values.
832 static __isl_give isl_ast_graft
*set_enforced_from_set(
833 __isl_take isl_ast_graft
*graft
,
834 __isl_keep isl_pw_aff_list
*lower
, int pos
, __isl_keep isl_set
*upper
)
837 isl_basic_set
*enforced
;
838 isl_pw_multi_aff
*pma
;
841 if (!graft
|| !lower
)
842 return isl_ast_graft_free(graft
);
844 space
= isl_set_get_space(upper
);
845 enforced
= isl_basic_set_universe(isl_space_copy(space
));
847 space
= isl_space_map_from_set(space
);
848 pma
= isl_pw_multi_aff_identity(space
);
850 n
= isl_pw_aff_list_n_pw_aff(lower
);
851 for (i
= 0; i
< n
; ++i
) {
855 isl_pw_multi_aff
*pma_i
;
857 pa
= isl_pw_aff_list_get_pw_aff(lower
, i
);
858 pa
= isl_pw_aff_ceil(pa
);
859 pma_i
= isl_pw_multi_aff_copy(pma
);
860 pma_i
= isl_pw_multi_aff_set_pw_aff(pma_i
, pos
, pa
);
861 enforced_i
= isl_set_copy(upper
);
862 enforced_i
= isl_set_preimage_pw_multi_aff(enforced_i
, pma_i
);
863 hull
= isl_set_simple_hull(enforced_i
);
864 enforced
= isl_basic_set_intersect(enforced
, hull
);
867 isl_pw_multi_aff_free(pma
);
869 graft
= isl_ast_graft_enforce(graft
, enforced
);
874 /* Compute the constraints on the outer dimensions enforced by
875 * graft->node and add those constraints to graft->enforced,
876 * in case the upper bound is expressed as
877 * a list of affine expressions "upper".
879 * The enforced condition is that each lower bound expression is less
880 * than or equal to each upper bound expression.
882 static __isl_give isl_ast_graft
*set_enforced_from_list(
883 __isl_take isl_ast_graft
*graft
,
884 __isl_keep isl_pw_aff_list
*lower
, __isl_keep isl_pw_aff_list
*upper
)
887 isl_basic_set
*enforced
;
889 lower
= isl_pw_aff_list_copy(lower
);
890 upper
= isl_pw_aff_list_copy(upper
);
891 cond
= isl_pw_aff_list_le_set(lower
, upper
);
892 enforced
= isl_set_simple_hull(cond
);
893 graft
= isl_ast_graft_enforce(graft
, enforced
);
898 /* Does "aff" have a negative constant term?
900 static isl_stat
aff_constant_is_negative(__isl_take isl_set
*set
,
901 __isl_take isl_aff
*aff
, void *user
)
906 v
= isl_aff_get_constant_val(aff
);
907 *neg
= isl_val_is_neg(v
);
912 return *neg
? isl_stat_ok
: isl_stat_error
;
915 /* Does "pa" have a negative constant term over its entire domain?
917 static isl_stat
pw_aff_constant_is_negative(__isl_take isl_pw_aff
*pa
,
923 r
= isl_pw_aff_foreach_piece(pa
, &aff_constant_is_negative
, user
);
926 return (*neg
&& r
>= 0) ? isl_stat_ok
: isl_stat_error
;
929 /* Does each element in "list" have a negative constant term?
931 * The callback terminates the iteration as soon an element has been
932 * found that does not have a negative constant term.
934 static int list_constant_is_negative(__isl_keep isl_pw_aff_list
*list
)
938 if (isl_pw_aff_list_foreach(list
,
939 &pw_aff_constant_is_negative
, &neg
) < 0 && neg
)
945 /* Add 1 to each of the elements in "list", where each of these elements
946 * is defined over the internal schedule space of "build".
948 static __isl_give isl_pw_aff_list
*list_add_one(
949 __isl_take isl_pw_aff_list
*list
, __isl_keep isl_ast_build
*build
)
956 space
= isl_ast_build_get_space(build
, 1);
957 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
958 aff
= isl_aff_add_constant_si(aff
, 1);
959 one
= isl_pw_aff_from_aff(aff
);
961 n
= isl_pw_aff_list_n_pw_aff(list
);
962 for (i
= 0; i
< n
; ++i
) {
964 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
965 pa
= isl_pw_aff_add(pa
, isl_pw_aff_copy(one
));
966 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
969 isl_pw_aff_free(one
);
974 /* Set the condition part of the for node graft->node in case
975 * the upper bound is represented as a list of piecewise affine expressions.
977 * In particular, set the condition to
979 * iterator <= min(list of upper bounds)
981 * If each of the upper bounds has a negative constant term, then
982 * set the condition to
984 * iterator < min(list of (upper bound + 1)s)
987 static __isl_give isl_ast_graft
*set_for_cond_from_list(
988 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*list
,
989 __isl_keep isl_ast_build
*build
)
992 isl_ast_expr
*bound
, *iterator
, *cond
;
993 enum isl_ast_op_type type
= isl_ast_op_le
;
996 return isl_ast_graft_free(graft
);
998 neg
= list_constant_is_negative(list
);
1000 return isl_ast_graft_free(graft
);
1001 list
= isl_pw_aff_list_copy(list
);
1003 list
= list_add_one(list
, build
);
1004 type
= isl_ast_op_lt
;
1007 bound
= reduce_list(isl_ast_op_min
, list
, build
);
1008 iterator
= isl_ast_expr_copy(graft
->node
->u
.f
.iterator
);
1009 cond
= isl_ast_expr_alloc_binary(type
, iterator
, bound
);
1010 graft
->node
->u
.f
.cond
= cond
;
1012 isl_pw_aff_list_free(list
);
1013 if (!graft
->node
->u
.f
.cond
)
1014 return isl_ast_graft_free(graft
);
1018 /* Set the condition part of the for node graft->node in case
1019 * the upper bound is represented as a set.
1021 static __isl_give isl_ast_graft
*set_for_cond_from_set(
1022 __isl_take isl_ast_graft
*graft
, __isl_keep isl_set
*set
,
1023 __isl_keep isl_ast_build
*build
)
1030 cond
= isl_ast_build_expr_from_set_internal(build
, isl_set_copy(set
));
1031 graft
->node
->u
.f
.cond
= cond
;
1032 if (!graft
->node
->u
.f
.cond
)
1033 return isl_ast_graft_free(graft
);
1037 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1038 * the current dimension.
1040 static __isl_give isl_ast_expr
*for_inc(__isl_keep isl_ast_build
*build
)
1048 ctx
= isl_ast_build_get_ctx(build
);
1049 depth
= isl_ast_build_get_depth(build
);
1051 if (!isl_ast_build_has_stride(build
, depth
))
1052 return isl_ast_expr_alloc_int_si(ctx
, 1);
1054 v
= isl_ast_build_get_stride(build
, depth
);
1055 return isl_ast_expr_from_val(v
);
1058 /* Should we express the loop condition as
1060 * iterator <= min(list of upper bounds)
1062 * or as a conjunction of constraints?
1064 * The first is constructed from a list of upper bounds.
1065 * The second is constructed from a set.
1067 * If there are no upper bounds in "constraints", then this could mean
1068 * that "domain" simply doesn't have an upper bound or that we didn't
1069 * pick any upper bound. In the first case, we want to generate the
1070 * loop condition as a(n empty) conjunction of constraints
1071 * In the second case, we will compute
1072 * a single upper bound from "domain" and so we use the list form.
1074 * If there are upper bounds in "constraints",
1075 * then we use the list form iff the atomic_upper_bound option is set.
1077 static int use_upper_bound_list(isl_ctx
*ctx
, int n_upper
,
1078 __isl_keep isl_set
*domain
, int depth
)
1081 return isl_options_get_ast_build_atomic_upper_bound(ctx
);
1083 return isl_set_dim_has_upper_bound(domain
, isl_dim_set
, depth
);
1086 /* Fill in the expressions of the for node in graft->node.
1089 * - set the initialization part of the loop to the maximum of the lower bounds
1090 * - extract the increment from the stride of the current dimension
1091 * - construct the for condition either based on a list of upper bounds
1092 * or on a set of upper bound constraints.
1094 static __isl_give isl_ast_graft
*set_for_node_expressions(
1095 __isl_take isl_ast_graft
*graft
, __isl_keep isl_pw_aff_list
*lower
,
1096 int use_list
, __isl_keep isl_pw_aff_list
*upper_list
,
1097 __isl_keep isl_set
*upper_set
, __isl_keep isl_ast_build
*build
)
1104 build
= isl_ast_build_copy(build
);
1107 node
->u
.f
.init
= reduce_list(isl_ast_op_max
, lower
, build
);
1108 node
->u
.f
.inc
= for_inc(build
);
1110 if (!node
->u
.f
.init
|| !node
->u
.f
.inc
)
1111 graft
= isl_ast_graft_free(graft
);
1114 graft
= set_for_cond_from_list(graft
, upper_list
, build
);
1116 graft
= set_for_cond_from_set(graft
, upper_set
, build
);
1118 isl_ast_build_free(build
);
1123 /* Update "graft" based on "bounds" and "domain" for the generic,
1124 * non-degenerate, case.
1126 * "c_lower" and "c_upper" contain the lower and upper bounds
1127 * that the loop node should express.
1128 * "domain" is the subset of the intersection of the constraints
1129 * for which some code is executed.
1131 * There may be zero lower bounds or zero upper bounds in "constraints"
1132 * in case the list of constraints was created
1133 * based on the atomic option or based on separation with explicit bounds.
1134 * In that case, we use "domain" to derive lower and/or upper bounds.
1136 * We first compute a list of one or more lower bounds.
1138 * Then we decide if we want to express the condition as
1140 * iterator <= min(list of upper bounds)
1142 * or as a conjunction of constraints.
1144 * The set of enforced constraints is then computed either based on
1145 * a list of upper bounds or on a set of upper bound constraints.
1146 * We do not compute any enforced constraints if we were forced
1147 * to compute a lower or upper bound using exact_bound. The domains
1148 * of the resulting expressions may imply some bounds on outer dimensions
1149 * that we do not want to appear in the enforced constraints since
1150 * they are not actually enforced by the corresponding code.
1152 * Finally, we fill in the expressions of the for node.
1154 static __isl_give isl_ast_graft
*refine_generic_bounds(
1155 __isl_take isl_ast_graft
*graft
,
1156 __isl_take isl_constraint_list
*c_lower
,
1157 __isl_take isl_constraint_list
*c_upper
,
1158 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1162 isl_pw_aff_list
*lower
;
1164 isl_set
*upper_set
= NULL
;
1165 isl_pw_aff_list
*upper_list
= NULL
;
1166 int n_lower
, n_upper
;
1168 if (!graft
|| !c_lower
|| !c_upper
|| !build
)
1171 depth
= isl_ast_build_get_depth(build
);
1172 ctx
= isl_ast_graft_get_ctx(graft
);
1174 n_lower
= isl_constraint_list_n_constraint(c_lower
);
1175 n_upper
= isl_constraint_list_n_constraint(c_upper
);
1177 use_list
= use_upper_bound_list(ctx
, n_upper
, domain
, depth
);
1179 lower
= lower_bounds(c_lower
, depth
, domain
, build
);
1182 upper_list
= upper_bounds(c_upper
, depth
, domain
, build
);
1183 else if (n_upper
> 0)
1184 upper_set
= intersect_constraints(c_upper
);
1186 upper_set
= isl_set_universe(isl_set_get_space(domain
));
1188 if (n_lower
== 0 || n_upper
== 0)
1191 graft
= set_enforced_from_list(graft
, lower
, upper_list
);
1193 graft
= set_enforced_from_set(graft
, lower
, depth
, upper_set
);
1195 graft
= set_for_node_expressions(graft
, lower
, use_list
, upper_list
,
1198 isl_pw_aff_list_free(lower
);
1199 isl_pw_aff_list_free(upper_list
);
1200 isl_set_free(upper_set
);
1201 isl_constraint_list_free(c_lower
);
1202 isl_constraint_list_free(c_upper
);
1206 isl_constraint_list_free(c_lower
);
1207 isl_constraint_list_free(c_upper
);
1208 return isl_ast_graft_free(graft
);
1211 /* Internal data structure used inside count_constraints to keep
1212 * track of the number of constraints that are independent of dimension "pos",
1213 * the lower bounds in "pos" and the upper bounds in "pos".
1215 struct isl_ast_count_constraints_data
{
1223 /* Increment data->n_indep, data->lower or data->upper depending
1224 * on whether "c" is independenct of dimensions data->pos,
1225 * a lower bound or an upper bound.
1227 static isl_stat
count_constraints(__isl_take isl_constraint
*c
, void *user
)
1229 struct isl_ast_count_constraints_data
*data
= user
;
1231 if (isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->pos
))
1233 else if (isl_constraint_is_upper_bound(c
, isl_dim_set
, data
->pos
))
1238 isl_constraint_free(c
);
1243 /* Update "graft" based on "bounds" and "domain" for the generic,
1244 * non-degenerate, case.
1246 * "list" respresent the list of bounds that need to be encoded by
1247 * the for loop. Only the constraints that involve the iterator
1248 * are relevant here. The other constraints are taken care of by
1249 * the caller and are included in the generated constraints of "build".
1250 * "domain" is the subset of the intersection of the constraints
1251 * for which some code is executed.
1252 * "build" is the build in which graft->node was created.
1254 * We separate lower bounds, upper bounds and constraints that
1255 * are independent of the loop iterator.
1257 * The actual for loop bounds are generated in refine_generic_bounds.
1259 static __isl_give isl_ast_graft
*refine_generic_split(
1260 __isl_take isl_ast_graft
*graft
, __isl_take isl_constraint_list
*list
,
1261 __isl_keep isl_set
*domain
, __isl_keep isl_ast_build
*build
)
1263 struct isl_ast_count_constraints_data data
;
1264 isl_constraint_list
*lower
;
1265 isl_constraint_list
*upper
;
1268 return isl_ast_graft_free(graft
);
1270 data
.pos
= isl_ast_build_get_depth(build
);
1272 list
= isl_constraint_list_sort(list
, &cmp_constraint
, &data
.pos
);
1274 return isl_ast_graft_free(graft
);
1276 data
.n_indep
= data
.n_lower
= data
.n_upper
= 0;
1277 if (isl_constraint_list_foreach(list
, &count_constraints
, &data
) < 0) {
1278 isl_constraint_list_free(list
);
1279 return isl_ast_graft_free(graft
);
1282 lower
= isl_constraint_list_drop(list
, 0, data
.n_indep
);
1283 upper
= isl_constraint_list_copy(lower
);
1284 lower
= isl_constraint_list_drop(lower
, data
.n_lower
, data
.n_upper
);
1285 upper
= isl_constraint_list_drop(upper
, 0, data
.n_lower
);
1287 return refine_generic_bounds(graft
, lower
, upper
, domain
, build
);
1290 /* Update "graft" based on "bounds" and "domain" for the generic,
1291 * non-degenerate, case.
1293 * "bounds" respresent the bounds that need to be encoded by
1294 * the for loop (or a guard around the for loop).
1295 * "domain" is the subset of "bounds" for which some code is executed.
1296 * "build" is the build in which graft->node was created.
1298 * We break up "bounds" into a list of constraints and continue with
1299 * refine_generic_split.
1301 static __isl_give isl_ast_graft
*refine_generic(
1302 __isl_take isl_ast_graft
*graft
,
1303 __isl_keep isl_basic_set
*bounds
, __isl_keep isl_set
*domain
,
1304 __isl_keep isl_ast_build
*build
)
1306 isl_constraint_list
*list
;
1308 if (!build
|| !graft
)
1309 return isl_ast_graft_free(graft
);
1311 list
= isl_basic_set_get_constraint_list(bounds
);
1313 graft
= refine_generic_split(graft
, list
, domain
, build
);
1318 /* Create a for node for the current level.
1320 * Mark the for node degenerate if "degenerate" is set.
1322 static __isl_give isl_ast_node
*create_for(__isl_keep isl_ast_build
*build
,
1332 depth
= isl_ast_build_get_depth(build
);
1333 id
= isl_ast_build_get_iterator_id(build
, depth
);
1334 node
= isl_ast_node_alloc_for(id
);
1336 node
= isl_ast_node_for_mark_degenerate(node
);
1341 /* If the ast_build_exploit_nested_bounds option is set, then return
1342 * the constraints enforced by all elements in "list".
1343 * Otherwise, return the universe.
1345 static __isl_give isl_basic_set
*extract_shared_enforced(
1346 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1354 ctx
= isl_ast_graft_list_get_ctx(list
);
1355 if (isl_options_get_ast_build_exploit_nested_bounds(ctx
))
1356 return isl_ast_graft_list_extract_shared_enforced(list
, build
);
1358 space
= isl_ast_build_get_space(build
, 1);
1359 return isl_basic_set_universe(space
);
1362 /* Return the pending constraints of "build" that are not already taken
1363 * care of (by a combination of "enforced" and the generated constraints
1366 static __isl_give isl_set
*extract_pending(__isl_keep isl_ast_build
*build
,
1367 __isl_keep isl_basic_set
*enforced
)
1369 isl_set
*guard
, *context
;
1371 guard
= isl_ast_build_get_pending(build
);
1372 context
= isl_set_from_basic_set(isl_basic_set_copy(enforced
));
1373 context
= isl_set_intersect(context
,
1374 isl_ast_build_get_generated(build
));
1375 return isl_set_gist(guard
, context
);
1378 /* Create an AST node for the current dimension based on
1379 * the schedule domain "bounds" and return the node encapsulated
1380 * in an isl_ast_graft.
1382 * "executed" is the current inverse schedule, taking into account
1383 * the bounds in "bounds"
1384 * "domain" is the domain of "executed", with inner dimensions projected out.
1385 * It may be a strict subset of "bounds" in case "bounds" was created
1386 * based on the atomic option or based on separation with explicit bounds.
1388 * "domain" may satisfy additional equalities that result
1389 * from intersecting "executed" with "bounds" in add_node.
1390 * It may also satisfy some global constraints that were dropped out because
1391 * we performed separation with explicit bounds.
1392 * The very first step is then to copy these constraints to "bounds".
1394 * Since we may be calling before_each_for and after_each_for
1395 * callbacks, we record the current inverse schedule in the build.
1397 * We consider three builds,
1398 * "build" is the one in which the current level is created,
1399 * "body_build" is the build in which the next level is created,
1400 * "sub_build" is essentially the same as "body_build", except that
1401 * the depth has not been increased yet.
1403 * "build" already contains information (in strides and offsets)
1404 * about the strides at the current level, but this information is not
1405 * reflected in the build->domain.
1406 * We first add this information and the "bounds" to the sub_build->domain.
1407 * isl_ast_build_set_loop_bounds adds the stride information and
1408 * checks whether the current dimension attains
1409 * only a single value and whether this single value can be represented using
1410 * a single affine expression.
1411 * In the first case, the current level is considered "degenerate".
1412 * In the second, sub-case, the current level is considered "eliminated".
1413 * Eliminated levels don't need to be reflected in the AST since we can
1414 * simply plug in the affine expression. For degenerate, but non-eliminated,
1415 * levels, we do introduce a for node, but mark is as degenerate so that
1416 * it can be printed as an assignment of the single value to the loop
1419 * If the current level is eliminated, we explicitly plug in the value
1420 * for the current level found by isl_ast_build_set_loop_bounds in the
1421 * inverse schedule. This ensures that if we are working on a slice
1422 * of the domain based on information available in the inverse schedule
1423 * and the build domain, that then this information is also reflected
1424 * in the inverse schedule. This operation also eliminates the current
1425 * dimension from the inverse schedule making sure no inner dimensions depend
1426 * on the current dimension. Otherwise, we create a for node, marking
1427 * it degenerate if appropriate. The initial for node is still incomplete
1428 * and will be completed in either refine_degenerate or refine_generic.
1430 * We then generate a sequence of grafts for the next level,
1431 * create a surrounding graft for the current level and insert
1432 * the for node we created (if the current level is not eliminated).
1433 * Before creating a graft for the current level, we first extract
1434 * hoistable constraints from the child guards and combine them
1435 * with the pending constraints in the build. These constraints
1436 * are used to simplify the child guards and then added to the guard
1437 * of the current graft to ensure that they will be generated.
1438 * If the hoisted guard is a disjunction, then we use it directly
1439 * to gist the guards on the children before intersect it with the
1440 * pending constraints. We do so because this disjunction is typically
1441 * identical to the guards on the children such that these guards
1442 * can be effectively removed completely. After the intersection,
1443 * the gist operation would have a harder time figuring this out.
1445 * Finally, we set the bounds of the for loop in either
1446 * refine_degenerate or refine_generic.
1447 * We do so in a context where the pending constraints of the build
1448 * have been replaced by the guard of the current graft.
1450 static __isl_give isl_ast_graft
*create_node_scaled(
1451 __isl_take isl_union_map
*executed
,
1452 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1453 __isl_take isl_ast_build
*build
)
1457 isl_bool eliminated
;
1458 isl_basic_set
*hull
;
1459 isl_basic_set
*enforced
;
1460 isl_set
*guard
, *hoisted
;
1461 isl_ast_node
*node
= NULL
;
1462 isl_ast_graft
*graft
;
1463 isl_ast_graft_list
*children
;
1464 isl_ast_build
*sub_build
;
1465 isl_ast_build
*body_build
;
1467 domain
= isl_ast_build_eliminate_divs(build
, domain
);
1468 domain
= isl_set_detect_equalities(domain
);
1469 hull
= isl_set_unshifted_simple_hull(isl_set_copy(domain
));
1470 bounds
= isl_basic_set_intersect(bounds
, hull
);
1471 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
1473 depth
= isl_ast_build_get_depth(build
);
1474 sub_build
= isl_ast_build_copy(build
);
1475 bounds
= isl_basic_set_remove_redundancies(bounds
);
1476 bounds
= isl_ast_build_specialize_basic_set(sub_build
, bounds
);
1477 sub_build
= isl_ast_build_set_loop_bounds(sub_build
,
1478 isl_basic_set_copy(bounds
));
1479 degenerate
= isl_ast_build_has_value(sub_build
);
1480 eliminated
= isl_ast_build_has_affine_value(sub_build
, depth
);
1481 if (degenerate
< 0 || eliminated
< 0)
1482 executed
= isl_union_map_free(executed
);
1484 bounds
= isl_ast_build_compute_gist_basic_set(build
, bounds
);
1485 sub_build
= isl_ast_build_set_pending_generated(sub_build
,
1486 isl_basic_set_copy(bounds
));
1488 executed
= plug_in_values(executed
, sub_build
);
1490 node
= create_for(build
, degenerate
);
1492 body_build
= isl_ast_build_copy(sub_build
);
1493 body_build
= isl_ast_build_increase_depth(body_build
);
1495 node
= before_each_for(node
, body_build
);
1496 children
= generate_next_level(executed
,
1497 isl_ast_build_copy(body_build
));
1499 enforced
= extract_shared_enforced(children
, build
);
1500 guard
= extract_pending(sub_build
, enforced
);
1501 hoisted
= isl_ast_graft_list_extract_hoistable_guard(children
, build
);
1502 if (isl_set_n_basic_set(hoisted
) > 1)
1503 children
= isl_ast_graft_list_gist_guards(children
,
1504 isl_set_copy(hoisted
));
1505 guard
= isl_set_intersect(guard
, hoisted
);
1507 guard
= add_implied_guards(guard
, degenerate
, bounds
, build
);
1509 graft
= isl_ast_graft_alloc_from_children(children
,
1510 isl_set_copy(guard
), enforced
, build
, sub_build
);
1513 isl_ast_build
*for_build
;
1515 graft
= isl_ast_graft_insert_for(graft
, node
);
1516 for_build
= isl_ast_build_copy(build
);
1517 for_build
= isl_ast_build_replace_pending_by_guard(for_build
,
1518 isl_set_copy(guard
));
1520 graft
= refine_degenerate(graft
, for_build
, sub_build
);
1522 graft
= refine_generic(graft
, bounds
,
1524 isl_ast_build_free(for_build
);
1526 isl_set_free(guard
);
1528 graft
= after_each_for(graft
, body_build
);
1530 isl_ast_build_free(body_build
);
1531 isl_ast_build_free(sub_build
);
1532 isl_ast_build_free(build
);
1533 isl_basic_set_free(bounds
);
1534 isl_set_free(domain
);
1539 /* Internal data structure for checking if all constraints involving
1540 * the input dimension "depth" are such that the other coefficients
1541 * are multiples of "m", reducing "m" if they are not.
1542 * If "m" is reduced all the way down to "1", then the check has failed
1543 * and we break out of the iteration.
1545 struct isl_check_scaled_data
{
1550 /* If constraint "c" involves the input dimension data->depth,
1551 * then make sure that all the other coefficients are multiples of data->m,
1552 * reducing data->m if needed.
1553 * Break out of the iteration if data->m has become equal to "1".
1555 static isl_stat
constraint_check_scaled(__isl_take isl_constraint
*c
,
1558 struct isl_check_scaled_data
*data
= user
;
1560 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_out
,
1563 if (!isl_constraint_involves_dims(c
, isl_dim_in
, data
->depth
, 1)) {
1564 isl_constraint_free(c
);
1568 for (i
= 0; i
< 4; ++i
) {
1569 n
= isl_constraint_dim(c
, t
[i
]);
1570 for (j
= 0; j
< n
; ++j
) {
1573 if (t
[i
] == isl_dim_in
&& j
== data
->depth
)
1575 if (!isl_constraint_involves_dims(c
, t
[i
], j
, 1))
1577 d
= isl_constraint_get_coefficient_val(c
, t
[i
], j
);
1578 data
->m
= isl_val_gcd(data
->m
, d
);
1579 if (isl_val_is_one(data
->m
))
1586 isl_constraint_free(c
);
1588 return i
< 4 ? isl_stat_error
: isl_stat_ok
;
1591 /* For each constraint of "bmap" that involves the input dimension data->depth,
1592 * make sure that all the other coefficients are multiples of data->m,
1593 * reducing data->m if needed.
1594 * Break out of the iteration if data->m has become equal to "1".
1596 static isl_stat
basic_map_check_scaled(__isl_take isl_basic_map
*bmap
,
1601 r
= isl_basic_map_foreach_constraint(bmap
,
1602 &constraint_check_scaled
, user
);
1603 isl_basic_map_free(bmap
);
1608 /* For each constraint of "map" that involves the input dimension data->depth,
1609 * make sure that all the other coefficients are multiples of data->m,
1610 * reducing data->m if needed.
1611 * Break out of the iteration if data->m has become equal to "1".
1613 static isl_stat
map_check_scaled(__isl_take isl_map
*map
, void *user
)
1617 r
= isl_map_foreach_basic_map(map
, &basic_map_check_scaled
, user
);
1623 /* Create an AST node for the current dimension based on
1624 * the schedule domain "bounds" and return the node encapsulated
1625 * in an isl_ast_graft.
1627 * "executed" is the current inverse schedule, taking into account
1628 * the bounds in "bounds"
1629 * "domain" is the domain of "executed", with inner dimensions projected out.
1632 * Before moving on to the actual AST node construction in create_node_scaled,
1633 * we first check if the current dimension is strided and if we can scale
1634 * down this stride. Note that we only do this if the ast_build_scale_strides
1637 * In particular, let the current dimension take on values
1641 * with a an integer. We check if we can find an integer m that (obviously)
1642 * divides both f and s.
1644 * If so, we check if the current dimension only appears in constraints
1645 * where the coefficients of the other variables are multiples of m.
1646 * We perform this extra check to avoid the risk of introducing
1647 * divisions by scaling down the current dimension.
1649 * If so, we scale the current dimension down by a factor of m.
1650 * That is, we plug in
1654 * Note that in principle we could always scale down strided loops
1659 * but this may result in i' taking on larger values than the original i,
1660 * due to the shift by "f".
1661 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1663 static __isl_give isl_ast_graft
*create_node(__isl_take isl_union_map
*executed
,
1664 __isl_take isl_basic_set
*bounds
, __isl_take isl_set
*domain
,
1665 __isl_take isl_ast_build
*build
)
1667 struct isl_check_scaled_data data
;
1672 ctx
= isl_ast_build_get_ctx(build
);
1673 if (!isl_options_get_ast_build_scale_strides(ctx
))
1674 return create_node_scaled(executed
, bounds
, domain
, build
);
1676 data
.depth
= isl_ast_build_get_depth(build
);
1677 if (!isl_ast_build_has_stride(build
, data
.depth
))
1678 return create_node_scaled(executed
, bounds
, domain
, build
);
1680 offset
= isl_ast_build_get_offset(build
, data
.depth
);
1681 data
.m
= isl_ast_build_get_stride(build
, data
.depth
);
1683 offset
= isl_aff_free(offset
);
1684 offset
= isl_aff_scale_down_val(offset
, isl_val_copy(data
.m
));
1685 d
= isl_aff_get_denominator_val(offset
);
1687 executed
= isl_union_map_free(executed
);
1689 if (executed
&& isl_val_is_divisible_by(data
.m
, d
))
1690 data
.m
= isl_val_div(data
.m
, d
);
1692 data
.m
= isl_val_set_si(data
.m
, 1);
1696 if (!isl_val_is_one(data
.m
)) {
1697 if (isl_union_map_foreach_map(executed
, &map_check_scaled
,
1699 !isl_val_is_one(data
.m
))
1700 executed
= isl_union_map_free(executed
);
1703 if (!isl_val_is_one(data
.m
)) {
1708 isl_union_map
*umap
;
1710 space
= isl_ast_build_get_space(build
, 1);
1711 space
= isl_space_map_from_set(space
);
1712 ma
= isl_multi_aff_identity(space
);
1713 aff
= isl_multi_aff_get_aff(ma
, data
.depth
);
1714 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.m
));
1715 ma
= isl_multi_aff_set_aff(ma
, data
.depth
, aff
);
1717 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
1718 isl_multi_aff_copy(ma
));
1719 domain
= isl_set_preimage_multi_aff(domain
,
1720 isl_multi_aff_copy(ma
));
1721 map
= isl_map_reverse(isl_map_from_multi_aff(ma
));
1722 umap
= isl_union_map_from_map(map
);
1723 executed
= isl_union_map_apply_domain(executed
,
1724 isl_union_map_copy(umap
));
1725 build
= isl_ast_build_scale_down(build
, isl_val_copy(data
.m
),
1728 isl_aff_free(offset
);
1729 isl_val_free(data
.m
);
1731 return create_node_scaled(executed
, bounds
, domain
, build
);
1734 /* Add the basic set to the list that "user" points to.
1736 static isl_stat
collect_basic_set(__isl_take isl_basic_set
*bset
, void *user
)
1738 isl_basic_set_list
**list
= user
;
1740 *list
= isl_basic_set_list_add(*list
, bset
);
1745 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1747 static __isl_give isl_basic_set_list
*isl_basic_set_list_from_set(
1748 __isl_take isl_set
*set
)
1752 isl_basic_set_list
*list
;
1757 ctx
= isl_set_get_ctx(set
);
1759 n
= isl_set_n_basic_set(set
);
1760 list
= isl_basic_set_list_alloc(ctx
, n
);
1761 if (isl_set_foreach_basic_set(set
, &collect_basic_set
, &list
) < 0)
1762 list
= isl_basic_set_list_free(list
);
1768 /* Generate code for the schedule domain "bounds"
1769 * and add the result to "list".
1771 * We mainly detect strides here and check if the bounds do not
1772 * conflict with the current build domain
1773 * and then pass over control to create_node.
1775 * "bounds" reflects the bounds on the current dimension and possibly
1776 * some extra conditions on outer dimensions.
1777 * It does not, however, include any divs involving the current dimension,
1778 * so it does not capture any stride constraints.
1779 * We therefore need to compute that part of the schedule domain that
1780 * intersects with "bounds" and derive the strides from the result.
1782 static __isl_give isl_ast_graft_list
*add_node(
1783 __isl_take isl_ast_graft_list
*list
, __isl_take isl_union_map
*executed
,
1784 __isl_take isl_basic_set
*bounds
, __isl_take isl_ast_build
*build
)
1786 isl_ast_graft
*graft
;
1787 isl_set
*domain
= NULL
;
1788 isl_union_set
*uset
;
1789 int empty
, disjoint
;
1791 uset
= isl_union_set_from_basic_set(isl_basic_set_copy(bounds
));
1792 executed
= isl_union_map_intersect_domain(executed
, uset
);
1793 empty
= isl_union_map_is_empty(executed
);
1799 uset
= isl_union_map_domain(isl_union_map_copy(executed
));
1800 domain
= isl_set_from_union_set(uset
);
1801 domain
= isl_ast_build_specialize(build
, domain
);
1803 domain
= isl_set_compute_divs(domain
);
1804 domain
= isl_ast_build_eliminate_inner(build
, domain
);
1805 disjoint
= isl_set_is_disjoint(domain
, build
->domain
);
1811 build
= isl_ast_build_detect_strides(build
, isl_set_copy(domain
));
1813 graft
= create_node(executed
, bounds
, domain
,
1814 isl_ast_build_copy(build
));
1815 list
= isl_ast_graft_list_add(list
, graft
);
1816 isl_ast_build_free(build
);
1819 list
= isl_ast_graft_list_free(list
);
1821 isl_set_free(domain
);
1822 isl_basic_set_free(bounds
);
1823 isl_union_map_free(executed
);
1824 isl_ast_build_free(build
);
1828 /* Does any element of i follow or coincide with any element of j
1829 * at the current depth for equal values of the outer dimensions?
1831 static isl_bool
domain_follows_at_depth(__isl_keep isl_basic_set
*i
,
1832 __isl_keep isl_basic_set
*j
, void *user
)
1834 int depth
= *(int *) user
;
1835 isl_basic_map
*test
;
1839 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
1840 isl_basic_set_copy(j
));
1841 for (l
= 0; l
< depth
; ++l
)
1842 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
1844 test
= isl_basic_map_order_ge(test
, isl_dim_in
, depth
,
1845 isl_dim_out
, depth
);
1846 empty
= isl_basic_map_is_empty(test
);
1847 isl_basic_map_free(test
);
1849 return empty
< 0 ? isl_bool_error
: !empty
;
1852 /* Split up each element of "list" into a part that is related to "bset"
1853 * according to "gt" and a part that is not.
1854 * Return a list that consist of "bset" and all the pieces.
1856 static __isl_give isl_basic_set_list
*add_split_on(
1857 __isl_take isl_basic_set_list
*list
, __isl_take isl_basic_set
*bset
,
1858 __isl_keep isl_basic_map
*gt
)
1861 isl_basic_set_list
*res
;
1864 bset
= isl_basic_set_free(bset
);
1866 gt
= isl_basic_map_copy(gt
);
1867 gt
= isl_basic_map_intersect_domain(gt
, isl_basic_set_copy(bset
));
1868 n
= isl_basic_set_list_n_basic_set(list
);
1869 res
= isl_basic_set_list_from_basic_set(bset
);
1870 for (i
= 0; res
&& i
< n
; ++i
) {
1871 isl_basic_set
*bset
;
1872 isl_set
*set1
, *set2
;
1873 isl_basic_map
*bmap
;
1876 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1877 bmap
= isl_basic_map_copy(gt
);
1878 bmap
= isl_basic_map_intersect_range(bmap
, bset
);
1879 bset
= isl_basic_map_range(bmap
);
1880 empty
= isl_basic_set_is_empty(bset
);
1882 res
= isl_basic_set_list_free(res
);
1884 isl_basic_set_free(bset
);
1885 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1886 res
= isl_basic_set_list_add(res
, bset
);
1890 res
= isl_basic_set_list_add(res
, isl_basic_set_copy(bset
));
1891 set1
= isl_set_from_basic_set(bset
);
1892 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1893 set2
= isl_set_from_basic_set(bset
);
1894 set1
= isl_set_subtract(set2
, set1
);
1895 set1
= isl_set_make_disjoint(set1
);
1897 res
= isl_basic_set_list_concat(res
,
1898 isl_basic_set_list_from_set(set1
));
1900 isl_basic_map_free(gt
);
1901 isl_basic_set_list_free(list
);
1905 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
1906 __isl_keep isl_basic_set_list
*domain_list
,
1907 __isl_keep isl_union_map
*executed
,
1908 __isl_keep isl_ast_build
*build
);
1910 /* Internal data structure for add_nodes.
1912 * "executed" and "build" are extra arguments to be passed to add_node.
1913 * "list" collects the results.
1915 struct isl_add_nodes_data
{
1916 isl_union_map
*executed
;
1917 isl_ast_build
*build
;
1919 isl_ast_graft_list
*list
;
1922 /* Generate code for the schedule domains in "scc"
1923 * and add the results to "list".
1925 * The domains in "scc" form a strongly connected component in the ordering.
1926 * If the number of domains in "scc" is larger than 1, then this means
1927 * that we cannot determine a valid ordering for the domains in the component.
1928 * This should be fairly rare because the individual domains
1929 * have been made disjoint first.
1930 * The problem is that the domains may be integrally disjoint but not
1931 * rationally disjoint. For example, we may have domains
1933 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1935 * These two domains have an empty intersection, but their rational
1936 * relaxations do intersect. It is impossible to order these domains
1937 * in the second dimension because the first should be ordered before
1938 * the second for outer dimension equal to 0, while it should be ordered
1939 * after for outer dimension equal to 1.
1941 * This may happen in particular in case of unrolling since the domain
1942 * of each slice is replaced by its simple hull.
1944 * For each basic set i in "scc" and for each of the following basic sets j,
1945 * we split off that part of the basic set i that shares the outer dimensions
1946 * with j and lies before j in the current dimension.
1947 * We collect all the pieces in a new list that replaces "scc".
1949 * While the elements in "scc" should be disjoint, we double-check
1950 * this property to avoid running into an infinite recursion in case
1951 * they intersect due to some internal error.
1953 static isl_stat
add_nodes(__isl_take isl_basic_set_list
*scc
, void *user
)
1955 struct isl_add_nodes_data
*data
= user
;
1957 isl_basic_set
*bset
, *first
;
1958 isl_basic_set_list
*list
;
1962 n
= isl_basic_set_list_n_basic_set(scc
);
1963 bset
= isl_basic_set_list_get_basic_set(scc
, 0);
1965 isl_basic_set_list_free(scc
);
1966 data
->list
= add_node(data
->list
,
1967 isl_union_map_copy(data
->executed
), bset
,
1968 isl_ast_build_copy(data
->build
));
1969 return data
->list
? isl_stat_ok
: isl_stat_error
;
1972 depth
= isl_ast_build_get_depth(data
->build
);
1973 space
= isl_basic_set_get_space(bset
);
1974 space
= isl_space_map_from_set(space
);
1975 gt
= isl_basic_map_universe(space
);
1976 for (i
= 0; i
< depth
; ++i
)
1977 gt
= isl_basic_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
1978 gt
= isl_basic_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
1980 first
= isl_basic_set_copy(bset
);
1981 list
= isl_basic_set_list_from_basic_set(bset
);
1982 for (i
= 1; i
< n
; ++i
) {
1985 bset
= isl_basic_set_list_get_basic_set(scc
, i
);
1987 disjoint
= isl_basic_set_is_disjoint(bset
, first
);
1989 list
= isl_basic_set_list_free(list
);
1991 isl_die(isl_basic_set_list_get_ctx(scc
),
1993 "basic sets in scc are assumed to be disjoint",
1994 list
= isl_basic_set_list_free(list
));
1996 list
= add_split_on(list
, bset
, gt
);
1998 isl_basic_set_free(first
);
1999 isl_basic_map_free(gt
);
2000 isl_basic_set_list_free(scc
);
2002 data
->list
= isl_ast_graft_list_concat(data
->list
,
2003 generate_sorted_domains(scc
, data
->executed
, data
->build
));
2004 isl_basic_set_list_free(scc
);
2006 return data
->list
? isl_stat_ok
: isl_stat_error
;
2009 /* Sort the domains in "domain_list" according to the execution order
2010 * at the current depth (for equal values of the outer dimensions),
2011 * generate code for each of them, collecting the results in a list.
2012 * If no code is generated (because the intersection of the inverse schedule
2013 * with the domains turns out to be empty), then an empty list is returned.
2015 * The caller is responsible for ensuring that the basic sets in "domain_list"
2016 * are pair-wise disjoint. It can, however, in principle happen that
2017 * two basic sets should be ordered one way for one value of the outer
2018 * dimensions and the other way for some other value of the outer dimensions.
2019 * We therefore play safe and look for strongly connected components.
2020 * The function add_nodes takes care of handling non-trivial components.
2022 static __isl_give isl_ast_graft_list
*generate_sorted_domains(
2023 __isl_keep isl_basic_set_list
*domain_list
,
2024 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2027 struct isl_add_nodes_data data
;
2034 ctx
= isl_basic_set_list_get_ctx(domain_list
);
2035 n
= isl_basic_set_list_n_basic_set(domain_list
);
2036 data
.list
= isl_ast_graft_list_alloc(ctx
, n
);
2040 return add_node(data
.list
, isl_union_map_copy(executed
),
2041 isl_basic_set_list_get_basic_set(domain_list
, 0),
2042 isl_ast_build_copy(build
));
2044 depth
= isl_ast_build_get_depth(build
);
2045 data
.executed
= executed
;
2047 if (isl_basic_set_list_foreach_scc(domain_list
,
2048 &domain_follows_at_depth
, &depth
,
2049 &add_nodes
, &data
) < 0)
2050 data
.list
= isl_ast_graft_list_free(data
.list
);
2055 /* Do i and j share any values for the outer dimensions?
2057 static isl_bool
shared_outer(__isl_keep isl_basic_set
*i
,
2058 __isl_keep isl_basic_set
*j
, void *user
)
2060 int depth
= *(int *) user
;
2061 isl_basic_map
*test
;
2065 test
= isl_basic_map_from_domain_and_range(isl_basic_set_copy(i
),
2066 isl_basic_set_copy(j
));
2067 for (l
= 0; l
< depth
; ++l
)
2068 test
= isl_basic_map_equate(test
, isl_dim_in
, l
,
2070 empty
= isl_basic_map_is_empty(test
);
2071 isl_basic_map_free(test
);
2073 return empty
< 0 ? isl_bool_error
: !empty
;
2076 /* Internal data structure for generate_sorted_domains_wrap.
2078 * "n" is the total number of basic sets
2079 * "executed" and "build" are extra arguments to be passed
2080 * to generate_sorted_domains.
2082 * "single" is set to 1 by generate_sorted_domains_wrap if there
2083 * is only a single component.
2084 * "list" collects the results.
2086 struct isl_ast_generate_parallel_domains_data
{
2088 isl_union_map
*executed
;
2089 isl_ast_build
*build
;
2092 isl_ast_graft_list
*list
;
2095 /* Call generate_sorted_domains on "scc", fuse the result into a list
2096 * with either zero or one graft and collect the these single element
2097 * lists into data->list.
2099 * If there is only one component, i.e., if the number of basic sets
2100 * in the current component is equal to the total number of basic sets,
2101 * then data->single is set to 1 and the result of generate_sorted_domains
2104 static isl_stat
generate_sorted_domains_wrap(__isl_take isl_basic_set_list
*scc
,
2107 struct isl_ast_generate_parallel_domains_data
*data
= user
;
2108 isl_ast_graft_list
*list
;
2110 list
= generate_sorted_domains(scc
, data
->executed
, data
->build
);
2111 data
->single
= isl_basic_set_list_n_basic_set(scc
) == data
->n
;
2113 list
= isl_ast_graft_list_fuse(list
, data
->build
);
2117 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
2119 isl_basic_set_list_free(scc
);
2121 return isl_stat_error
;
2126 /* Look for any (weakly connected) components in the "domain_list"
2127 * of domains that share some values of the outer dimensions.
2128 * That is, domains in different components do not share any values
2129 * of the outer dimensions. This means that these components
2130 * can be freely reordered.
2131 * Within each of the components, we sort the domains according
2132 * to the execution order at the current depth.
2134 * If there is more than one component, then generate_sorted_domains_wrap
2135 * fuses the result of each call to generate_sorted_domains
2136 * into a list with either zero or one graft and collects these (at most)
2137 * single element lists into a bigger list. This means that the elements of the
2138 * final list can be freely reordered. In particular, we sort them
2139 * according to an arbitrary but fixed ordering to ease merging of
2140 * graft lists from different components.
2142 static __isl_give isl_ast_graft_list
*generate_parallel_domains(
2143 __isl_keep isl_basic_set_list
*domain_list
,
2144 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
2147 struct isl_ast_generate_parallel_domains_data data
;
2152 data
.n
= isl_basic_set_list_n_basic_set(domain_list
);
2154 return generate_sorted_domains(domain_list
, executed
, build
);
2156 depth
= isl_ast_build_get_depth(build
);
2158 data
.executed
= executed
;
2161 if (isl_basic_set_list_foreach_scc(domain_list
, &shared_outer
, &depth
,
2162 &generate_sorted_domains_wrap
,
2164 data
.list
= isl_ast_graft_list_free(data
.list
);
2167 data
.list
= isl_ast_graft_list_sort_guard(data
.list
);
2172 /* Internal data for separate_domain.
2174 * "explicit" is set if we only want to use explicit bounds.
2176 * "domain" collects the separated domains.
2178 struct isl_separate_domain_data
{
2179 isl_ast_build
*build
;
2184 /* Extract implicit bounds on the current dimension for the executed "map".
2186 * The domain of "map" may involve inner dimensions, so we
2187 * need to eliminate them.
2189 static __isl_give isl_set
*implicit_bounds(__isl_take isl_map
*map
,
2190 __isl_keep isl_ast_build
*build
)
2194 domain
= isl_map_domain(map
);
2195 domain
= isl_ast_build_eliminate(build
, domain
);
2200 /* Extract explicit bounds on the current dimension for the executed "map".
2202 * Rather than eliminating the inner dimensions as in implicit_bounds,
2203 * we simply drop any constraints involving those inner dimensions.
2204 * The idea is that most bounds that are implied by constraints on the
2205 * inner dimensions will be enforced by for loops and not by explicit guards.
2206 * There is then no need to separate along those bounds.
2208 static __isl_give isl_set
*explicit_bounds(__isl_take isl_map
*map
,
2209 __isl_keep isl_ast_build
*build
)
2214 dim
= isl_map_dim(map
, isl_dim_out
);
2215 map
= isl_map_drop_constraints_involving_dims(map
, isl_dim_out
, 0, dim
);
2217 domain
= isl_map_domain(map
);
2218 depth
= isl_ast_build_get_depth(build
);
2219 dim
= isl_set_dim(domain
, isl_dim_set
);
2220 domain
= isl_set_detect_equalities(domain
);
2221 domain
= isl_set_drop_constraints_involving_dims(domain
,
2222 isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2223 domain
= isl_set_remove_divs_involving_dims(domain
,
2224 isl_dim_set
, depth
, 1);
2225 domain
= isl_set_remove_unknown_divs(domain
);
2230 /* Split data->domain into pieces that intersect with the range of "map"
2231 * and pieces that do not intersect with the range of "map"
2232 * and then add that part of the range of "map" that does not intersect
2233 * with data->domain.
2235 static isl_stat
separate_domain(__isl_take isl_map
*map
, void *user
)
2237 struct isl_separate_domain_data
*data
= user
;
2242 domain
= explicit_bounds(map
, data
->build
);
2244 domain
= implicit_bounds(map
, data
->build
);
2246 domain
= isl_set_coalesce(domain
);
2247 domain
= isl_set_make_disjoint(domain
);
2248 d1
= isl_set_subtract(isl_set_copy(domain
), isl_set_copy(data
->domain
));
2249 d2
= isl_set_subtract(isl_set_copy(data
->domain
), isl_set_copy(domain
));
2250 data
->domain
= isl_set_intersect(data
->domain
, domain
);
2251 data
->domain
= isl_set_union(data
->domain
, d1
);
2252 data
->domain
= isl_set_union(data
->domain
, d2
);
2257 /* Separate the schedule domains of "executed".
2259 * That is, break up the domain of "executed" into basic sets,
2260 * such that for each basic set S, every element in S is associated with
2261 * the same domain spaces.
2263 * "space" is the (single) domain space of "executed".
2265 static __isl_give isl_set
*separate_schedule_domains(
2266 __isl_take isl_space
*space
, __isl_take isl_union_map
*executed
,
2267 __isl_keep isl_ast_build
*build
)
2269 struct isl_separate_domain_data data
= { build
};
2272 ctx
= isl_ast_build_get_ctx(build
);
2273 data
.explicit = isl_options_get_ast_build_separation_bounds(ctx
) ==
2274 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT
;
2275 data
.domain
= isl_set_empty(space
);
2276 if (isl_union_map_foreach_map(executed
, &separate_domain
, &data
) < 0)
2277 data
.domain
= isl_set_free(data
.domain
);
2279 isl_union_map_free(executed
);
2283 /* Temporary data used during the search for a lower bound for unrolling.
2285 * "build" is the build in which the unrolling will be performed
2286 * "domain" is the original set for which to find a lower bound
2287 * "depth" is the dimension for which to find a lower boudn
2288 * "expansion" is the expansion that needs to be applied to "domain"
2289 * in the unrolling that will be performed
2291 * "lower" is the best lower bound found so far. It is NULL if we have not
2293 * "n" is the corresponding size. If lower is NULL, then the value of n
2295 * "n_div" is the maximal number of integer divisions in the first
2296 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2297 * been computed yet.
2299 struct isl_find_unroll_data
{
2300 isl_ast_build
*build
;
2303 isl_basic_map
*expansion
;
2310 /* Return the constraint
2312 * i_"depth" = aff + offset
2314 static __isl_give isl_constraint
*at_offset(int depth
, __isl_keep isl_aff
*aff
,
2317 aff
= isl_aff_copy(aff
);
2318 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, depth
, -1);
2319 aff
= isl_aff_add_constant_si(aff
, offset
);
2320 return isl_equality_from_aff(aff
);
2323 /* Update *user to the number of integer divsions in the first element
2324 * of "ma", if it is larger than the current value.
2326 static isl_stat
update_n_div(__isl_take isl_set
*set
,
2327 __isl_take isl_multi_aff
*ma
, void *user
)
2333 aff
= isl_multi_aff_get_aff(ma
, 0);
2334 n_div
= isl_aff_dim(aff
, isl_dim_div
);
2336 isl_multi_aff_free(ma
);
2342 return aff
? isl_stat_ok
: isl_stat_error
;
2345 /* Get the number of integer divisions in the expression for the iterator
2346 * value at the first slice in the unrolling based on lower bound "lower",
2347 * taking into account the expansion that needs to be performed on this slice.
2349 static int get_expanded_n_div(struct isl_find_unroll_data
*data
,
2350 __isl_keep isl_aff
*lower
)
2354 isl_map
*it_map
, *expansion
;
2355 isl_pw_multi_aff
*pma
;
2358 c
= at_offset(data
->depth
, lower
, 0);
2359 set
= isl_set_copy(data
->domain
);
2360 set
= isl_set_add_constraint(set
, c
);
2361 expansion
= isl_map_from_basic_map(isl_basic_map_copy(data
->expansion
));
2362 set
= isl_set_apply(set
, expansion
);
2363 it_map
= isl_ast_build_map_to_iterator(data
->build
, set
);
2364 pma
= isl_pw_multi_aff_from_map(it_map
);
2366 if (isl_pw_multi_aff_foreach_piece(pma
, &update_n_div
, &n
) < 0)
2368 isl_pw_multi_aff_free(pma
);
2373 /* Is the lower bound "lower" with corresponding iteration count "n"
2374 * better than the one stored in "data"?
2375 * If there is no upper bound on the iteration count ("n" is infinity) or
2376 * if the count is too large, then we cannot use this lower bound.
2377 * Otherwise, if there was no previous lower bound or
2378 * if the iteration count of the new lower bound is smaller than
2379 * the iteration count of the previous lower bound, then we consider
2380 * the new lower bound to be better.
2381 * If the iteration count is the same, then compare the number
2382 * of integer divisions that would be needed to express
2383 * the iterator value at the first slice in the unrolling
2384 * according to the lower bound. If we end up computing this
2385 * number, then store the lowest value in data->n_div.
2387 static int is_better_lower_bound(struct isl_find_unroll_data
*data
,
2388 __isl_keep isl_aff
*lower
, __isl_keep isl_val
*n
)
2395 if (isl_val_is_infty(n
))
2397 if (isl_val_cmp_si(n
, INT_MAX
) > 0)
2401 cmp
= isl_val_cmp_si(n
, *data
->n
);
2406 if (data
->n_div
< 0)
2407 data
->n_div
= get_expanded_n_div(data
, data
->lower
);
2408 if (data
->n_div
< 0)
2410 if (data
->n_div
== 0)
2412 n_div
= get_expanded_n_div(data
, lower
);
2415 if (n_div
>= data
->n_div
)
2417 data
->n_div
= n_div
;
2422 /* Check if we can use "c" as a lower bound and if it is better than
2423 * any previously found lower bound.
2425 * If "c" does not involve the dimension at the current depth,
2426 * then we cannot use it.
2427 * Otherwise, let "c" be of the form
2431 * We compute the maximal value of
2433 * -ceil(f(j)/a)) + i + 1
2435 * over the domain. If there is such a value "n", then we know
2437 * -ceil(f(j)/a)) + i + 1 <= n
2441 * i < ceil(f(j)/a)) + n
2443 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2444 * We just need to check if we have found any lower bound before and
2445 * if the new lower bound is better (smaller n or fewer integer divisions)
2446 * than the previously found lower bounds.
2448 static isl_stat
update_unrolling_lower_bound(struct isl_find_unroll_data
*data
,
2449 __isl_keep isl_constraint
*c
)
2451 isl_aff
*aff
, *lower
;
2455 if (!isl_constraint_is_lower_bound(c
, isl_dim_set
, data
->depth
))
2458 lower
= isl_constraint_get_bound(c
, isl_dim_set
, data
->depth
);
2459 lower
= isl_aff_ceil(lower
);
2460 aff
= isl_aff_copy(lower
);
2461 aff
= isl_aff_neg(aff
);
2462 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, data
->depth
, 1);
2463 aff
= isl_aff_add_constant_si(aff
, 1);
2464 max
= isl_set_max_val(data
->domain
, aff
);
2467 better
= is_better_lower_bound(data
, lower
, max
);
2468 if (better
< 0 || !better
) {
2470 isl_aff_free(lower
);
2471 return better
< 0 ? isl_stat_error
: isl_stat_ok
;
2474 isl_aff_free(data
->lower
);
2475 data
->lower
= lower
;
2476 *data
->n
= isl_val_get_num_si(max
);
2482 /* Check if we can use "c" as a lower bound and if it is better than
2483 * any previously found lower bound.
2485 static isl_stat
constraint_find_unroll(__isl_take isl_constraint
*c
, void *user
)
2487 struct isl_find_unroll_data
*data
;
2490 data
= (struct isl_find_unroll_data
*) user
;
2491 r
= update_unrolling_lower_bound(data
, c
);
2492 isl_constraint_free(c
);
2497 /* Look for a lower bound l(i) on the dimension at "depth"
2498 * and a size n such that "domain" is a subset of
2500 * { [i] : l(i) <= i_d < l(i) + n }
2502 * where d is "depth" and l(i) depends only on earlier dimensions.
2503 * Furthermore, try and find a lower bound such that n is as small as possible.
2504 * In particular, "n" needs to be finite.
2505 * "build" is the build in which the unrolling will be performed.
2506 * "expansion" is the expansion that needs to be applied to "domain"
2507 * in the unrolling that will be performed.
2509 * Inner dimensions have been eliminated from "domain" by the caller.
2511 * We first construct a collection of lower bounds on the input set
2512 * by computing its simple hull. We then iterate through them,
2513 * discarding those that we cannot use (either because they do not
2514 * involve the dimension at "depth" or because they have no corresponding
2515 * upper bound, meaning that "n" would be unbounded) and pick out the
2516 * best from the remaining ones.
2518 * If we cannot find a suitable lower bound, then we consider that
2521 static __isl_give isl_aff
*find_unroll_lower_bound(
2522 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*domain
,
2523 int depth
, __isl_keep isl_basic_map
*expansion
, int *n
)
2525 struct isl_find_unroll_data data
=
2526 { build
, domain
, depth
, expansion
, NULL
, n
, -1 };
2527 isl_basic_set
*hull
;
2529 hull
= isl_set_simple_hull(isl_set_copy(domain
));
2531 if (isl_basic_set_foreach_constraint(hull
,
2532 &constraint_find_unroll
, &data
) < 0)
2535 isl_basic_set_free(hull
);
2538 isl_die(isl_set_get_ctx(domain
), isl_error_invalid
,
2539 "cannot find lower bound for unrolling", return NULL
);
2543 isl_basic_set_free(hull
);
2544 return isl_aff_free(data
.lower
);
2547 /* Call "fn" on each iteration of the current dimension of "domain".
2548 * If "init" is not NULL, then it is called with the number of
2549 * iterations before any call to "fn".
2550 * Return -1 on failure.
2552 * Since we are going to be iterating over the individual values,
2553 * we first check if there are any strides on the current dimension.
2554 * If there is, we rewrite the current dimension i as
2556 * i = stride i' + offset
2558 * and then iterate over individual values of i' instead.
2560 * We then look for a lower bound on i' and a size such that the domain
2563 * { [j,i'] : l(j) <= i' < l(j) + n }
2565 * and then take slices of the domain at values of i'
2566 * between l(j) and l(j) + n - 1.
2568 * We compute the unshifted simple hull of each slice to ensure that
2569 * we have a single basic set per offset. The slicing constraint
2570 * may get simplified away before the unshifted simple hull is taken
2571 * and may therefore in some rare cases disappear from the result.
2572 * We therefore explicitly add the constraint back after computing
2573 * the unshifted simple hull to ensure that the basic sets
2574 * remain disjoint. The constraints that are dropped by taking the hull
2575 * will be taken into account at the next level, as in the case of the
2578 * Finally, we map i' back to i and call "fn".
2580 static int foreach_iteration(__isl_take isl_set
*domain
,
2581 __isl_keep isl_ast_build
*build
, int (*init
)(int n
, void *user
),
2582 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
2587 isl_multi_aff
*expansion
;
2588 isl_basic_map
*bmap
;
2589 isl_aff
*lower
= NULL
;
2590 isl_ast_build
*stride_build
;
2592 depth
= isl_ast_build_get_depth(build
);
2594 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2595 domain
= isl_set_intersect(domain
, isl_ast_build_get_domain(build
));
2596 stride_build
= isl_ast_build_copy(build
);
2597 stride_build
= isl_ast_build_detect_strides(stride_build
,
2598 isl_set_copy(domain
));
2599 expansion
= isl_ast_build_get_stride_expansion(stride_build
);
2601 domain
= isl_set_preimage_multi_aff(domain
,
2602 isl_multi_aff_copy(expansion
));
2603 domain
= isl_ast_build_eliminate_divs(stride_build
, domain
);
2604 isl_ast_build_free(stride_build
);
2606 bmap
= isl_basic_map_from_multi_aff(expansion
);
2608 empty
= isl_set_is_empty(domain
);
2614 lower
= find_unroll_lower_bound(build
, domain
, depth
, bmap
, &n
);
2618 if (n
>= 0 && init
&& init(n
, user
) < 0)
2620 for (i
= 0; i
< n
; ++i
) {
2622 isl_basic_set
*bset
;
2623 isl_constraint
*slice
;
2625 slice
= at_offset(depth
, lower
, i
);
2626 set
= isl_set_copy(domain
);
2627 set
= isl_set_add_constraint(set
, isl_constraint_copy(slice
));
2628 bset
= isl_set_unshifted_simple_hull(set
);
2629 bset
= isl_basic_set_add_constraint(bset
, slice
);
2630 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(bmap
));
2632 if (fn(bset
, user
) < 0)
2636 isl_aff_free(lower
);
2637 isl_set_free(domain
);
2638 isl_basic_map_free(bmap
);
2640 return n
< 0 || i
< n
? -1 : 0;
2643 /* Data structure for storing the results and the intermediate objects
2644 * of compute_domains.
2646 * "list" is the main result of the function and contains a list
2647 * of disjoint basic sets for which code should be generated.
2649 * "executed" and "build" are inputs to compute_domains.
2650 * "schedule_domain" is the domain of "executed".
2652 * "option" constains the domains at the current depth that should by
2653 * atomic, separated or unrolled. These domains are as specified by
2654 * the user, except that inner dimensions have been eliminated and
2655 * that they have been made pair-wise disjoint.
2657 * "sep_class" contains the user-specified split into separation classes
2658 * specialized to the current depth.
2659 * "done" contains the union of the separation domains that have already
2662 struct isl_codegen_domains
{
2663 isl_basic_set_list
*list
;
2665 isl_union_map
*executed
;
2666 isl_ast_build
*build
;
2667 isl_set
*schedule_domain
;
2675 /* Internal data structure for do_unroll.
2677 * "domains" stores the results of compute_domains.
2678 * "class_domain" is the original class domain passed to do_unroll.
2679 * "unroll_domain" collects the unrolled iterations.
2681 struct isl_ast_unroll_data
{
2682 struct isl_codegen_domains
*domains
;
2683 isl_set
*class_domain
;
2684 isl_set
*unroll_domain
;
2687 /* Given an iteration of an unrolled domain represented by "bset",
2688 * add it to data->domains->list.
2689 * Since we may have dropped some constraints, we intersect with
2690 * the class domain again to ensure that each element in the list
2691 * is disjoint from the other class domains.
2693 static int do_unroll_iteration(__isl_take isl_basic_set
*bset
, void *user
)
2695 struct isl_ast_unroll_data
*data
= user
;
2697 isl_basic_set_list
*list
;
2699 set
= isl_set_from_basic_set(bset
);
2700 data
->unroll_domain
= isl_set_union(data
->unroll_domain
,
2702 set
= isl_set_intersect(set
, isl_set_copy(data
->class_domain
));
2703 set
= isl_set_make_disjoint(set
);
2704 list
= isl_basic_set_list_from_set(set
);
2705 data
->domains
->list
= isl_basic_set_list_concat(data
->domains
->list
,
2711 /* Extend domains->list with a list of basic sets, one for each value
2712 * of the current dimension in "domain" and remove the corresponding
2713 * sets from the class domain. Return the updated class domain.
2714 * The divs that involve the current dimension have not been projected out
2717 * We call foreach_iteration to iterate over the individual values and
2718 * in do_unroll_iteration we collect the individual basic sets in
2719 * domains->list and their union in data->unroll_domain, which is then
2720 * used to update the class domain.
2722 static __isl_give isl_set
*do_unroll(struct isl_codegen_domains
*domains
,
2723 __isl_take isl_set
*domain
, __isl_take isl_set
*class_domain
)
2725 struct isl_ast_unroll_data data
;
2728 return isl_set_free(class_domain
);
2730 return isl_set_free(domain
);
2732 data
.domains
= domains
;
2733 data
.class_domain
= class_domain
;
2734 data
.unroll_domain
= isl_set_empty(isl_set_get_space(domain
));
2736 if (foreach_iteration(domain
, domains
->build
, NULL
,
2737 &do_unroll_iteration
, &data
) < 0)
2738 data
.unroll_domain
= isl_set_free(data
.unroll_domain
);
2740 class_domain
= isl_set_subtract(class_domain
, data
.unroll_domain
);
2742 return class_domain
;
2745 /* Add domains to domains->list for each individual value of the current
2746 * dimension, for that part of the schedule domain that lies in the
2747 * intersection of the option domain and the class domain.
2748 * Remove the corresponding sets from the class domain and
2749 * return the updated class domain.
2751 * We first break up the unroll option domain into individual pieces
2752 * and then handle each of them separately. The unroll option domain
2753 * has been made disjoint in compute_domains_init_options,
2755 * Note that we actively want to combine different pieces of the
2756 * schedule domain that have the same value at the current dimension.
2757 * We therefore need to break up the unroll option domain before
2758 * intersecting with class and schedule domain, hoping that the
2759 * unroll option domain specified by the user is relatively simple.
2761 static __isl_give isl_set
*compute_unroll_domains(
2762 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2764 isl_set
*unroll_domain
;
2765 isl_basic_set_list
*unroll_list
;
2769 empty
= isl_set_is_empty(domains
->option
[isl_ast_loop_unroll
]);
2771 return isl_set_free(class_domain
);
2773 return class_domain
;
2775 unroll_domain
= isl_set_copy(domains
->option
[isl_ast_loop_unroll
]);
2776 unroll_list
= isl_basic_set_list_from_set(unroll_domain
);
2778 n
= isl_basic_set_list_n_basic_set(unroll_list
);
2779 for (i
= 0; i
< n
; ++i
) {
2780 isl_basic_set
*bset
;
2782 bset
= isl_basic_set_list_get_basic_set(unroll_list
, i
);
2783 unroll_domain
= isl_set_from_basic_set(bset
);
2784 unroll_domain
= isl_set_intersect(unroll_domain
,
2785 isl_set_copy(class_domain
));
2786 unroll_domain
= isl_set_intersect(unroll_domain
,
2787 isl_set_copy(domains
->schedule_domain
));
2789 empty
= isl_set_is_empty(unroll_domain
);
2790 if (empty
>= 0 && empty
) {
2791 isl_set_free(unroll_domain
);
2795 class_domain
= do_unroll(domains
, unroll_domain
, class_domain
);
2798 isl_basic_set_list_free(unroll_list
);
2800 return class_domain
;
2803 /* Try and construct a single basic set that includes the intersection of
2804 * the schedule domain, the atomic option domain and the class domain.
2805 * Add the resulting basic set(s) to domains->list and remove them
2806 * from class_domain. Return the updated class domain.
2808 * We construct a single domain rather than trying to combine
2809 * the schedule domains of individual domains because we are working
2810 * within a single component so that non-overlapping schedule domains
2811 * should already have been separated.
2812 * We do however need to make sure that this single domains is a subset
2813 * of the class domain so that it would not intersect with any other
2814 * class domains. This means that we may end up splitting up the atomic
2815 * domain in case separation classes are being used.
2817 * "domain" is the intersection of the schedule domain and the class domain,
2818 * with inner dimensions projected out.
2820 static __isl_give isl_set
*compute_atomic_domain(
2821 struct isl_codegen_domains
*domains
, __isl_take isl_set
*class_domain
)
2823 isl_basic_set
*bset
;
2824 isl_basic_set_list
*list
;
2825 isl_set
*domain
, *atomic_domain
;
2828 domain
= isl_set_copy(domains
->option
[isl_ast_loop_atomic
]);
2829 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2830 domain
= isl_set_intersect(domain
,
2831 isl_set_copy(domains
->schedule_domain
));
2832 empty
= isl_set_is_empty(domain
);
2834 class_domain
= isl_set_free(class_domain
);
2836 isl_set_free(domain
);
2837 return class_domain
;
2840 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2841 domain
= isl_set_coalesce(domain
);
2842 bset
= isl_set_unshifted_simple_hull(domain
);
2843 domain
= isl_set_from_basic_set(bset
);
2844 atomic_domain
= isl_set_copy(domain
);
2845 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2846 class_domain
= isl_set_subtract(class_domain
, atomic_domain
);
2847 domain
= isl_set_make_disjoint(domain
);
2848 list
= isl_basic_set_list_from_set(domain
);
2849 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2851 return class_domain
;
2854 /* Split up the schedule domain into uniform basic sets,
2855 * in the sense that each element in a basic set is associated to
2856 * elements of the same domains, and add the result to domains->list.
2857 * Do this for that part of the schedule domain that lies in the
2858 * intersection of "class_domain" and the separate option domain.
2860 * "class_domain" may or may not include the constraints
2861 * of the schedule domain, but this does not make a difference
2862 * since we are going to intersect it with the domain of the inverse schedule.
2863 * If it includes schedule domain constraints, then they may involve
2864 * inner dimensions, but we will eliminate them in separation_domain.
2866 static int compute_separate_domain(struct isl_codegen_domains
*domains
,
2867 __isl_keep isl_set
*class_domain
)
2871 isl_union_map
*executed
;
2872 isl_basic_set_list
*list
;
2875 domain
= isl_set_copy(domains
->option
[isl_ast_loop_separate
]);
2876 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2877 executed
= isl_union_map_copy(domains
->executed
);
2878 executed
= isl_union_map_intersect_domain(executed
,
2879 isl_union_set_from_set(domain
));
2880 empty
= isl_union_map_is_empty(executed
);
2881 if (empty
< 0 || empty
) {
2882 isl_union_map_free(executed
);
2883 return empty
< 0 ? -1 : 0;
2886 space
= isl_set_get_space(class_domain
);
2887 domain
= separate_schedule_domains(space
, executed
, domains
->build
);
2889 list
= isl_basic_set_list_from_set(domain
);
2890 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2895 /* Split up the domain at the current depth into disjoint
2896 * basic sets for which code should be generated separately
2897 * for the given separation class domain.
2899 * If any separation classes have been defined, then "class_domain"
2900 * is the domain of the current class and does not refer to inner dimensions.
2901 * Otherwise, "class_domain" is the universe domain.
2903 * We first make sure that the class domain is disjoint from
2904 * previously considered class domains.
2906 * The separate domains can be computed directly from the "class_domain".
2908 * The unroll, atomic and remainder domains need the constraints
2909 * from the schedule domain.
2911 * For unrolling, the actual schedule domain is needed (with divs that
2912 * may refer to the current dimension) so that stride detection can be
2915 * For atomic and remainder domains, inner dimensions and divs involving
2916 * the current dimensions should be eliminated.
2917 * In case we are working within a separation class, we need to intersect
2918 * the result with the current "class_domain" to ensure that the domains
2919 * are disjoint from those generated from other class domains.
2921 * The domain that has been made atomic may be larger than specified
2922 * by the user since it needs to be representable as a single basic set.
2923 * This possibly larger domain is removed from class_domain by
2924 * compute_atomic_domain. It is computed first so that the extended domain
2925 * would not overlap with any domains computed before.
2926 * Similary, the unrolled domains may have some constraints removed and
2927 * may therefore also be larger than specified by the user.
2929 * If anything is left after handling separate, unroll and atomic,
2930 * we split it up into basic sets and append the basic sets to domains->list.
2932 static isl_stat
compute_partial_domains(struct isl_codegen_domains
*domains
,
2933 __isl_take isl_set
*class_domain
)
2935 isl_basic_set_list
*list
;
2938 class_domain
= isl_set_subtract(class_domain
,
2939 isl_set_copy(domains
->done
));
2940 domains
->done
= isl_set_union(domains
->done
,
2941 isl_set_copy(class_domain
));
2943 class_domain
= compute_atomic_domain(domains
, class_domain
);
2944 class_domain
= compute_unroll_domains(domains
, class_domain
);
2946 domain
= isl_set_copy(class_domain
);
2948 if (compute_separate_domain(domains
, domain
) < 0)
2950 domain
= isl_set_subtract(domain
,
2951 isl_set_copy(domains
->option
[isl_ast_loop_separate
]));
2953 domain
= isl_set_intersect(domain
,
2954 isl_set_copy(domains
->schedule_domain
));
2956 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2957 domain
= isl_set_intersect(domain
, isl_set_copy(class_domain
));
2959 domain
= isl_set_coalesce(domain
);
2960 domain
= isl_set_make_disjoint(domain
);
2962 list
= isl_basic_set_list_from_set(domain
);
2963 domains
->list
= isl_basic_set_list_concat(domains
->list
, list
);
2965 isl_set_free(class_domain
);
2969 isl_set_free(domain
);
2970 isl_set_free(class_domain
);
2971 return isl_stat_error
;
2974 /* Split up the domain at the current depth into disjoint
2975 * basic sets for which code should be generated separately
2976 * for the separation class identified by "pnt".
2978 * We extract the corresponding class domain from domains->sep_class,
2979 * eliminate inner dimensions and pass control to compute_partial_domains.
2981 static isl_stat
compute_class_domains(__isl_take isl_point
*pnt
, void *user
)
2983 struct isl_codegen_domains
*domains
= user
;
2988 class_set
= isl_set_from_point(pnt
);
2989 domain
= isl_map_domain(isl_map_intersect_range(
2990 isl_map_copy(domains
->sep_class
), class_set
));
2991 domain
= isl_ast_build_compute_gist(domains
->build
, domain
);
2992 domain
= isl_ast_build_eliminate(domains
->build
, domain
);
2994 disjoint
= isl_set_plain_is_disjoint(domain
, domains
->schedule_domain
);
2996 return isl_stat_error
;
2998 isl_set_free(domain
);
3002 return compute_partial_domains(domains
, domain
);
3005 /* Extract the domains at the current depth that should be atomic,
3006 * separated or unrolled and store them in option.
3008 * The domains specified by the user might overlap, so we make
3009 * them disjoint by subtracting earlier domains from later domains.
3011 static void compute_domains_init_options(isl_set
*option
[4],
3012 __isl_keep isl_ast_build
*build
)
3014 enum isl_ast_loop_type type
, type2
;
3017 for (type
= isl_ast_loop_atomic
;
3018 type
<= isl_ast_loop_separate
; ++type
) {
3019 option
[type
] = isl_ast_build_get_option_domain(build
, type
);
3020 for (type2
= isl_ast_loop_atomic
; type2
< type
; ++type2
)
3021 option
[type
] = isl_set_subtract(option
[type
],
3022 isl_set_copy(option
[type2
]));
3025 unroll
= option
[isl_ast_loop_unroll
];
3026 unroll
= isl_set_coalesce(unroll
);
3027 unroll
= isl_set_make_disjoint(unroll
);
3028 option
[isl_ast_loop_unroll
] = unroll
;
3031 /* Split up the domain at the current depth into disjoint
3032 * basic sets for which code should be generated separately,
3033 * based on the user-specified options.
3034 * Return the list of disjoint basic sets.
3036 * There are three kinds of domains that we need to keep track of.
3037 * - the "schedule domain" is the domain of "executed"
3038 * - the "class domain" is the domain corresponding to the currrent
3040 * - the "option domain" is the domain corresponding to one of the options
3041 * atomic, unroll or separate
3043 * We first consider the individial values of the separation classes
3044 * and split up the domain for each of them separately.
3045 * Finally, we consider the remainder. If no separation classes were
3046 * specified, then we call compute_partial_domains with the universe
3047 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3048 * with inner dimensions removed. We do this because we want to
3049 * avoid computing the complement of the class domains (i.e., the difference
3050 * between the universe and domains->done).
3052 static __isl_give isl_basic_set_list
*compute_domains(
3053 __isl_keep isl_union_map
*executed
, __isl_keep isl_ast_build
*build
)
3055 struct isl_codegen_domains domains
;
3058 isl_union_set
*schedule_domain
;
3062 enum isl_ast_loop_type type
;
3068 ctx
= isl_union_map_get_ctx(executed
);
3069 domains
.list
= isl_basic_set_list_alloc(ctx
, 0);
3071 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3072 domain
= isl_set_from_union_set(schedule_domain
);
3074 compute_domains_init_options(domains
.option
, build
);
3076 domains
.sep_class
= isl_ast_build_get_separation_class(build
);
3077 classes
= isl_map_range(isl_map_copy(domains
.sep_class
));
3078 n_param
= isl_set_dim(classes
, isl_dim_param
);
3079 classes
= isl_set_project_out(classes
, isl_dim_param
, 0, n_param
);
3081 space
= isl_set_get_space(domain
);
3082 domains
.build
= build
;
3083 domains
.schedule_domain
= isl_set_copy(domain
);
3084 domains
.executed
= executed
;
3085 domains
.done
= isl_set_empty(space
);
3087 if (isl_set_foreach_point(classes
, &compute_class_domains
, &domains
) < 0)
3088 domains
.list
= isl_basic_set_list_free(domains
.list
);
3089 isl_set_free(classes
);
3091 empty
= isl_set_is_empty(domains
.done
);
3093 domains
.list
= isl_basic_set_list_free(domains
.list
);
3094 domain
= isl_set_free(domain
);
3096 isl_set_free(domain
);
3097 domain
= isl_set_universe(isl_set_get_space(domains
.done
));
3099 domain
= isl_ast_build_eliminate(build
, domain
);
3101 if (compute_partial_domains(&domains
, domain
) < 0)
3102 domains
.list
= isl_basic_set_list_free(domains
.list
);
3104 isl_set_free(domains
.schedule_domain
);
3105 isl_set_free(domains
.done
);
3106 isl_map_free(domains
.sep_class
);
3107 for (type
= isl_ast_loop_atomic
; type
<= isl_ast_loop_separate
; ++type
)
3108 isl_set_free(domains
.option
[type
]);
3110 return domains
.list
;
3113 /* Generate code for a single component, after shifting (if any)
3114 * has been applied, in case the schedule was specified as a union map.
3116 * We first split up the domain at the current depth into disjoint
3117 * basic sets based on the user-specified options.
3118 * Then we generated code for each of them and concatenate the results.
3120 static __isl_give isl_ast_graft_list
*generate_shifted_component_flat(
3121 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3123 isl_basic_set_list
*domain_list
;
3124 isl_ast_graft_list
*list
= NULL
;
3126 domain_list
= compute_domains(executed
, build
);
3127 list
= generate_parallel_domains(domain_list
, executed
, build
);
3129 isl_basic_set_list_free(domain_list
);
3130 isl_union_map_free(executed
);
3131 isl_ast_build_free(build
);
3136 /* Generate code for a single component, after shifting (if any)
3137 * has been applied, in case the schedule was specified as a schedule tree
3138 * and the separate option was specified.
3140 * We perform separation on the domain of "executed" and then generate
3141 * an AST for each of the resulting disjoint basic sets.
3143 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_separate(
3144 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3148 isl_basic_set_list
*domain_list
;
3149 isl_ast_graft_list
*list
;
3151 space
= isl_ast_build_get_space(build
, 1);
3152 domain
= separate_schedule_domains(space
,
3153 isl_union_map_copy(executed
), build
);
3154 domain_list
= isl_basic_set_list_from_set(domain
);
3156 list
= generate_parallel_domains(domain_list
, executed
, build
);
3158 isl_basic_set_list_free(domain_list
);
3159 isl_union_map_free(executed
);
3160 isl_ast_build_free(build
);
3165 /* Internal data structure for generate_shifted_component_tree_unroll.
3167 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3168 * "list" collects the constructs grafts.
3170 struct isl_ast_unroll_tree_data
{
3171 isl_union_map
*executed
;
3172 isl_ast_build
*build
;
3173 isl_ast_graft_list
*list
;
3176 /* Initialize data->list to a list of "n" elements.
3178 static int init_unroll_tree(int n
, void *user
)
3180 struct isl_ast_unroll_tree_data
*data
= user
;
3183 ctx
= isl_ast_build_get_ctx(data
->build
);
3184 data
->list
= isl_ast_graft_list_alloc(ctx
, n
);
3189 /* Given an iteration of an unrolled domain represented by "bset",
3190 * generate the corresponding AST and add the result to data->list.
3192 static int do_unroll_tree_iteration(__isl_take isl_basic_set
*bset
, void *user
)
3194 struct isl_ast_unroll_tree_data
*data
= user
;
3196 data
->list
= add_node(data
->list
, isl_union_map_copy(data
->executed
),
3197 bset
, isl_ast_build_copy(data
->build
));
3202 /* Generate code for a single component, after shifting (if any)
3203 * has been applied, in case the schedule was specified as a schedule tree
3204 * and the unroll option was specified.
3206 * We call foreach_iteration to iterate over the individual values and
3207 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3209 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_unroll(
3210 __isl_take isl_union_map
*executed
, __isl_take isl_set
*domain
,
3211 __isl_take isl_ast_build
*build
)
3213 struct isl_ast_unroll_tree_data data
= { executed
, build
, NULL
};
3215 if (foreach_iteration(domain
, build
, &init_unroll_tree
,
3216 &do_unroll_tree_iteration
, &data
) < 0)
3217 data
.list
= isl_ast_graft_list_free(data
.list
);
3219 isl_union_map_free(executed
);
3220 isl_ast_build_free(build
);
3225 /* Does "domain" involve a disjunction that is purely based on
3226 * constraints involving only outer dimension?
3228 * In particular, is there a disjunction such that the constraints
3229 * involving the current and later dimensions are the same over
3230 * all the disjuncts?
3232 static isl_bool
has_pure_outer_disjunction(__isl_keep isl_set
*domain
,
3233 __isl_keep isl_ast_build
*build
)
3235 isl_basic_set
*hull
;
3236 isl_set
*shared
, *inner
;
3240 if (isl_set_n_basic_set(domain
) <= 1)
3241 return isl_bool_false
;
3243 inner
= isl_set_copy(domain
);
3244 depth
= isl_ast_build_get_depth(build
);
3245 dim
= isl_set_dim(inner
, isl_dim_set
);
3246 inner
= isl_set_drop_constraints_not_involving_dims(inner
,
3247 isl_dim_set
, depth
, dim
- depth
);
3248 hull
= isl_set_plain_unshifted_simple_hull(isl_set_copy(inner
));
3249 shared
= isl_set_from_basic_set(hull
);
3250 equal
= isl_set_plain_is_equal(inner
, shared
);
3251 isl_set_free(inner
);
3252 isl_set_free(shared
);
3257 /* Generate code for a single component, after shifting (if any)
3258 * has been applied, in case the schedule was specified as a schedule tree.
3259 * In particular, handle the base case where there is either no isolated
3260 * set or we are within the isolated set (in which case "isolated" is set)
3261 * or the iterations that precede or follow the isolated set.
3263 * The schedule domain is broken up or combined into basic sets
3264 * according to the AST generation option specified in the current
3265 * schedule node, which may be either atomic, separate, unroll or
3266 * unspecified. If the option is unspecified, then we currently simply
3267 * split the schedule domain into disjoint basic sets.
3269 * In case the separate option is specified, the AST generation is
3270 * handled by generate_shifted_component_tree_separate.
3271 * In the other cases, we need the global schedule domain.
3272 * In the unroll case, the AST generation is then handled by
3273 * generate_shifted_component_tree_unroll which needs the actual
3274 * schedule domain (with divs that may refer to the current dimension)
3275 * so that stride detection can be performed.
3276 * In the atomic or unspecified case, inner dimensions and divs involving
3277 * the current dimensions should be eliminated.
3278 * The result is then either combined into a single basic set or
3279 * split up into disjoint basic sets.
3280 * Finally an AST is generated for each basic set and the results are
3283 * If the schedule domain involves a disjunction that is purely based on
3284 * constraints involving only outer dimension, then it is treated as
3285 * if atomic was specified. This ensures that only a single loop
3286 * is generated instead of a sequence of identical loops with
3289 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_base(
3290 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
3293 isl_bool outer_disjunction
;
3294 isl_union_set
*schedule_domain
;
3296 isl_basic_set_list
*domain_list
;
3297 isl_ast_graft_list
*list
;
3298 enum isl_ast_loop_type type
;
3300 type
= isl_ast_build_get_loop_type(build
, isolated
);
3304 if (type
== isl_ast_loop_separate
)
3305 return generate_shifted_component_tree_separate(executed
,
3308 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3309 domain
= isl_set_from_union_set(schedule_domain
);
3311 if (type
== isl_ast_loop_unroll
)
3312 return generate_shifted_component_tree_unroll(executed
, domain
,
3315 domain
= isl_ast_build_eliminate(build
, domain
);
3316 domain
= isl_set_coalesce(domain
);
3318 outer_disjunction
= has_pure_outer_disjunction(domain
, build
);
3319 if (outer_disjunction
< 0)
3320 domain
= isl_set_free(domain
);
3322 if (outer_disjunction
|| type
== isl_ast_loop_atomic
) {
3323 isl_basic_set
*hull
;
3324 hull
= isl_set_unshifted_simple_hull(domain
);
3325 domain_list
= isl_basic_set_list_from_basic_set(hull
);
3327 domain
= isl_set_make_disjoint(domain
);
3328 domain_list
= isl_basic_set_list_from_set(domain
);
3331 list
= generate_parallel_domains(domain_list
, executed
, build
);
3333 isl_basic_set_list_free(domain_list
);
3334 isl_union_map_free(executed
);
3335 isl_ast_build_free(build
);
3339 isl_union_map_free(executed
);
3340 isl_ast_build_free(build
);
3344 /* Extract out the disjunction imposed by "domain" on the outer
3345 * schedule dimensions.
3347 * In particular, remove all inner dimensions from "domain" (including
3348 * the current dimension) and then remove the constraints that are shared
3349 * by all disjuncts in the result.
3351 static __isl_give isl_set
*extract_disjunction(__isl_take isl_set
*domain
,
3352 __isl_keep isl_ast_build
*build
)
3357 domain
= isl_ast_build_specialize(build
, domain
);
3358 depth
= isl_ast_build_get_depth(build
);
3359 dim
= isl_set_dim(domain
, isl_dim_set
);
3360 domain
= isl_set_eliminate(domain
, isl_dim_set
, depth
, dim
- depth
);
3361 domain
= isl_set_remove_unknown_divs(domain
);
3362 hull
= isl_set_copy(domain
);
3363 hull
= isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull
));
3364 domain
= isl_set_gist(domain
, hull
);
3369 /* Add "guard" to the grafts in "list".
3370 * "build" is the outer AST build, while "sub_build" includes "guard"
3371 * in its generated domain.
3373 * First combine the grafts into a single graft and then add the guard.
3374 * If the list is empty, or if some error occurred, then simply return
3377 static __isl_give isl_ast_graft_list
*list_add_guard(
3378 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*guard
,
3379 __isl_keep isl_ast_build
*build
, __isl_keep isl_ast_build
*sub_build
)
3381 isl_ast_graft
*graft
;
3383 list
= isl_ast_graft_list_fuse(list
, sub_build
);
3385 if (isl_ast_graft_list_n_ast_graft(list
) != 1)
3388 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
3389 graft
= isl_ast_graft_add_guard(graft
, isl_set_copy(guard
), build
);
3390 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
3395 /* Generate code for a single component, after shifting (if any)
3396 * has been applied, in case the schedule was specified as a schedule tree.
3397 * In particular, do so for the specified subset of the schedule domain.
3399 * If we are outside of the isolated part, then "domain" may include
3400 * a disjunction. Explicitly generate this disjunction at this point
3401 * instead of relying on the disjunction getting hoisted back up
3404 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree_part(
3405 __isl_keep isl_union_map
*executed
, __isl_take isl_set
*domain
,
3406 __isl_keep isl_ast_build
*build
, int isolated
)
3408 isl_union_set
*uset
;
3409 isl_ast_graft_list
*list
;
3410 isl_ast_build
*sub_build
;
3413 uset
= isl_union_set_from_set(isl_set_copy(domain
));
3414 executed
= isl_union_map_copy(executed
);
3415 executed
= isl_union_map_intersect_domain(executed
, uset
);
3416 empty
= isl_union_map_is_empty(executed
);
3421 isl_union_map_free(executed
);
3422 isl_set_free(domain
);
3423 ctx
= isl_ast_build_get_ctx(build
);
3424 return isl_ast_graft_list_alloc(ctx
, 0);
3427 sub_build
= isl_ast_build_copy(build
);
3429 domain
= extract_disjunction(domain
, build
);
3430 sub_build
= isl_ast_build_restrict_generated(sub_build
,
3431 isl_set_copy(domain
));
3433 list
= generate_shifted_component_tree_base(executed
,
3434 isl_ast_build_copy(sub_build
), isolated
);
3436 list
= list_add_guard(list
, domain
, build
, sub_build
);
3437 isl_ast_build_free(sub_build
);
3438 isl_set_free(domain
);
3441 isl_union_map_free(executed
);
3442 isl_set_free(domain
);
3446 /* Generate code for a single component, after shifting (if any)
3447 * has been applied, in case the schedule was specified as a schedule tree.
3448 * In particular, do so for the specified sequence of subsets
3449 * of the schedule domain, "before", "isolated", "after" and "other",
3450 * where only the "isolated" part is considered to be isolated.
3452 static __isl_give isl_ast_graft_list
*generate_shifted_component_parts(
3453 __isl_take isl_union_map
*executed
, __isl_take isl_set
*before
,
3454 __isl_take isl_set
*isolated
, __isl_take isl_set
*after
,
3455 __isl_take isl_set
*other
, __isl_take isl_ast_build
*build
)
3457 isl_ast_graft_list
*list
, *res
;
3459 res
= generate_shifted_component_tree_part(executed
, before
, build
, 0);
3460 list
= generate_shifted_component_tree_part(executed
, isolated
,
3462 res
= isl_ast_graft_list_concat(res
, list
);
3463 list
= generate_shifted_component_tree_part(executed
, after
, build
, 0);
3464 res
= isl_ast_graft_list_concat(res
, list
);
3465 list
= generate_shifted_component_tree_part(executed
, other
, build
, 0);
3466 res
= isl_ast_graft_list_concat(res
, list
);
3468 isl_union_map_free(executed
);
3469 isl_ast_build_free(build
);
3474 /* Does "set" intersect "first", but not "second"?
3476 static isl_bool
only_intersects_first(__isl_keep isl_set
*set
,
3477 __isl_keep isl_set
*first
, __isl_keep isl_set
*second
)
3481 disjoint
= isl_set_is_disjoint(set
, first
);
3483 return isl_bool_error
;
3485 return isl_bool_false
;
3487 return isl_set_is_disjoint(set
, second
);
3490 /* Generate code for a single component, after shifting (if any)
3491 * has been applied, in case the schedule was specified as a schedule tree.
3492 * In particular, do so in case of isolation where there is
3493 * only an "isolated" part and an "after" part.
3494 * "dead1" and "dead2" are freed by this function in order to simplify
3497 * The "before" and "other" parts are set to empty sets.
3499 static __isl_give isl_ast_graft_list
*generate_shifted_component_only_after(
3500 __isl_take isl_union_map
*executed
, __isl_take isl_set
*isolated
,
3501 __isl_take isl_set
*after
, __isl_take isl_ast_build
*build
,
3502 __isl_take isl_set
*dead1
, __isl_take isl_set
*dead2
)
3506 empty
= isl_set_empty(isl_set_get_space(after
));
3507 isl_set_free(dead1
);
3508 isl_set_free(dead2
);
3509 return generate_shifted_component_parts(executed
, isl_set_copy(empty
),
3510 isolated
, after
, empty
, build
);
3513 /* Generate code for a single component, after shifting (if any)
3514 * has been applied, in case the schedule was specified as a schedule tree.
3516 * We first check if the user has specified an isolated schedule domain
3517 * and that we are not already outside of this isolated schedule domain.
3518 * If so, we break up the schedule domain into iterations that
3519 * precede the isolated domain, the isolated domain itself,
3520 * the iterations that follow the isolated domain and
3521 * the remaining iterations (those that are incomparable
3522 * to the isolated domain).
3523 * We generate an AST for each piece and concatenate the results.
3525 * In the special case where at least one element of the schedule
3526 * domain that does not belong to the isolated domain needs
3527 * to be scheduled after this isolated domain, but none of those
3528 * elements need to be scheduled before, break up the schedule domain
3529 * in only two parts, the isolated domain, and a part that will be
3530 * scheduled after the isolated domain.
3532 * If no isolated set has been specified, then we generate an
3533 * AST for the entire inverse schedule.
3535 static __isl_give isl_ast_graft_list
*generate_shifted_component_tree(
3536 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3539 int empty
, has_isolate
;
3541 isl_union_set
*schedule_domain
;
3543 isl_basic_set
*hull
;
3544 isl_set
*isolated
, *before
, *after
, *test
;
3548 build
= isl_ast_build_extract_isolated(build
);
3549 has_isolate
= isl_ast_build_has_isolated(build
);
3550 if (has_isolate
< 0)
3551 executed
= isl_union_map_free(executed
);
3552 else if (!has_isolate
)
3553 return generate_shifted_component_tree_base(executed
, build
, 0);
3555 schedule_domain
= isl_union_map_domain(isl_union_map_copy(executed
));
3556 domain
= isl_set_from_union_set(schedule_domain
);
3558 isolated
= isl_ast_build_get_isolated(build
);
3559 isolated
= isl_set_intersect(isolated
, isl_set_copy(domain
));
3560 test
= isl_ast_build_specialize(build
, isl_set_copy(isolated
));
3561 empty
= isl_set_is_empty(test
);
3566 isl_set_free(isolated
);
3567 isl_set_free(domain
);
3568 return generate_shifted_component_tree_base(executed
, build
, 0);
3570 isolated
= isl_ast_build_eliminate(build
, isolated
);
3571 hull
= isl_set_unshifted_simple_hull(isolated
);
3572 isolated
= isl_set_from_basic_set(hull
);
3574 depth
= isl_ast_build_get_depth(build
);
3575 space
= isl_space_map_from_set(isl_set_get_space(isolated
));
3576 gt
= isl_map_universe(space
);
3577 for (i
= 0; i
< depth
; ++i
)
3578 gt
= isl_map_equate(gt
, isl_dim_in
, i
, isl_dim_out
, i
);
3579 gt
= isl_map_order_gt(gt
, isl_dim_in
, depth
, isl_dim_out
, depth
);
3580 lt
= isl_map_reverse(isl_map_copy(gt
));
3581 before
= isl_set_apply(isl_set_copy(isolated
), gt
);
3582 after
= isl_set_apply(isl_set_copy(isolated
), lt
);
3584 domain
= isl_set_subtract(domain
, isl_set_copy(isolated
));
3585 pure
= only_intersects_first(domain
, after
, before
);
3587 executed
= isl_union_map_free(executed
);
3589 return generate_shifted_component_only_after(executed
, isolated
,
3590 domain
, build
, before
, after
);
3591 domain
= isl_set_subtract(domain
, isl_set_copy(before
));
3592 domain
= isl_set_subtract(domain
, isl_set_copy(after
));
3593 after
= isl_set_subtract(after
, isl_set_copy(isolated
));
3594 after
= isl_set_subtract(after
, isl_set_copy(before
));
3595 before
= isl_set_subtract(before
, isl_set_copy(isolated
));
3597 return generate_shifted_component_parts(executed
, before
, isolated
,
3598 after
, domain
, build
);
3600 isl_set_free(domain
);
3601 isl_set_free(isolated
);
3602 isl_union_map_free(executed
);
3603 isl_ast_build_free(build
);
3607 /* Generate code for a single component, after shifting (if any)
3610 * Call generate_shifted_component_tree or generate_shifted_component_flat
3611 * depending on whether the schedule was specified as a schedule tree.
3613 static __isl_give isl_ast_graft_list
*generate_shifted_component(
3614 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
3616 if (isl_ast_build_has_schedule_node(build
))
3617 return generate_shifted_component_tree(executed
, build
);
3619 return generate_shifted_component_flat(executed
, build
);
3622 struct isl_set_map_pair
{
3627 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3628 * of indices into the "domain" array,
3629 * return the union of the "map" fields of the elements
3630 * indexed by the first "n" elements of "order".
3632 static __isl_give isl_union_map
*construct_component_executed(
3633 struct isl_set_map_pair
*domain
, int *order
, int n
)
3637 isl_union_map
*executed
;
3639 map
= isl_map_copy(domain
[order
[0]].map
);
3640 executed
= isl_union_map_from_map(map
);
3641 for (i
= 1; i
< n
; ++i
) {
3642 map
= isl_map_copy(domain
[order
[i
]].map
);
3643 executed
= isl_union_map_add_map(executed
, map
);
3649 /* Generate code for a single component, after shifting (if any)
3652 * The component inverse schedule is specified as the "map" fields
3653 * of the elements of "domain" indexed by the first "n" elements of "order".
3655 static __isl_give isl_ast_graft_list
*generate_shifted_component_from_list(
3656 struct isl_set_map_pair
*domain
, int *order
, int n
,
3657 __isl_take isl_ast_build
*build
)
3659 isl_union_map
*executed
;
3661 executed
= construct_component_executed(domain
, order
, n
);
3662 return generate_shifted_component(executed
, build
);
3665 /* Does set dimension "pos" of "set" have an obviously fixed value?
3667 static int dim_is_fixed(__isl_keep isl_set
*set
, int pos
)
3672 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, pos
);
3675 fixed
= !isl_val_is_nan(v
);
3681 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3682 * of indices into the "domain" array,
3683 * do all (except for at most one) of the "set" field of the elements
3684 * indexed by the first "n" elements of "order" have a fixed value
3685 * at position "depth"?
3687 static int at_most_one_non_fixed(struct isl_set_map_pair
*domain
,
3688 int *order
, int n
, int depth
)
3693 for (i
= 0; i
< n
; ++i
) {
3696 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3709 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3710 * of indices into the "domain" array,
3711 * eliminate the inner dimensions from the "set" field of the elements
3712 * indexed by the first "n" elements of "order", provided the current
3713 * dimension does not have a fixed value.
3715 * Return the index of the first element in "order" with a corresponding
3716 * "set" field that does not have an (obviously) fixed value.
3718 static int eliminate_non_fixed(struct isl_set_map_pair
*domain
,
3719 int *order
, int n
, int depth
, __isl_keep isl_ast_build
*build
)
3724 for (i
= n
- 1; i
>= 0; --i
) {
3726 f
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
3731 domain
[order
[i
]].set
= isl_ast_build_eliminate_inner(build
,
3732 domain
[order
[i
]].set
);
3739 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3740 * of indices into the "domain" array,
3741 * find the element of "domain" (amongst those indexed by the first "n"
3742 * elements of "order") with the "set" field that has the smallest
3743 * value for the current iterator.
3745 * Note that the domain with the smallest value may depend on the parameters
3746 * and/or outer loop dimension. Since the result of this function is only
3747 * used as heuristic, we only make a reasonable attempt at finding the best
3748 * domain, one that should work in case a single domain provides the smallest
3749 * value for the current dimension over all values of the parameters
3750 * and outer dimensions.
3752 * In particular, we compute the smallest value of the first domain
3753 * and replace it by that of any later domain if that later domain
3754 * has a smallest value that is smaller for at least some value
3755 * of the parameters and outer dimensions.
3757 static int first_offset(struct isl_set_map_pair
*domain
, int *order
, int n
,
3758 __isl_keep isl_ast_build
*build
)
3764 min_first
= isl_ast_build_map_to_iterator(build
,
3765 isl_set_copy(domain
[order
[0]].set
));
3766 min_first
= isl_map_lexmin(min_first
);
3768 for (i
= 1; i
< n
; ++i
) {
3769 isl_map
*min
, *test
;
3772 min
= isl_ast_build_map_to_iterator(build
,
3773 isl_set_copy(domain
[order
[i
]].set
));
3774 min
= isl_map_lexmin(min
);
3775 test
= isl_map_copy(min
);
3776 test
= isl_map_apply_domain(isl_map_copy(min_first
), test
);
3777 test
= isl_map_order_lt(test
, isl_dim_in
, 0, isl_dim_out
, 0);
3778 empty
= isl_map_is_empty(test
);
3780 if (empty
>= 0 && !empty
) {
3781 isl_map_free(min_first
);
3791 isl_map_free(min_first
);
3793 return i
< n
? -1 : first
;
3796 /* Construct a shifted inverse schedule based on the original inverse schedule,
3797 * the stride and the offset.
3799 * The original inverse schedule is specified as the "map" fields
3800 * of the elements of "domain" indexed by the first "n" elements of "order".
3802 * "stride" and "offset" are such that the difference
3803 * between the values of the current dimension of domain "i"
3804 * and the values of the current dimension for some reference domain are
3807 * stride * integer + offset[i]
3809 * Moreover, 0 <= offset[i] < stride.
3811 * For each domain, we create a map
3813 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3815 * where j refers to the current dimension and the other dimensions are
3816 * unchanged, and apply this map to the original schedule domain.
3818 * For example, for the original schedule
3820 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3822 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3823 * we apply the mapping
3827 * to the schedule of the "A" domain and the mapping
3829 * { [j - 1] -> [j, 1] }
3831 * to the schedule of the "B" domain.
3834 * Note that after the transformation, the differences between pairs
3835 * of values of the current dimension over all domains are multiples
3836 * of stride and that we have therefore exposed the stride.
3839 * To see that the mapping preserves the lexicographic order,
3840 * first note that each of the individual maps above preserves the order.
3841 * If the value of the current iterator is j1 in one domain and j2 in another,
3842 * then if j1 = j2, we know that the same map is applied to both domains
3843 * and the order is preserved.
3844 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3845 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3849 * and the order is preserved.
3850 * If c1 < c2, then we know
3856 * j2 - j1 = n * s + r
3858 * with n >= 0 and 0 <= r < s.
3859 * In other words, r = c2 - c1.
3870 * (j1 - c1, c1) << (j2 - c2, c2)
3872 * with "<<" the lexicographic order, proving that the order is preserved
3875 static __isl_give isl_union_map
*contruct_shifted_executed(
3876 struct isl_set_map_pair
*domain
, int *order
, int n
,
3877 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3878 __isl_take isl_ast_build
*build
)
3881 isl_union_map
*executed
;
3887 depth
= isl_ast_build_get_depth(build
);
3888 space
= isl_ast_build_get_space(build
, 1);
3889 executed
= isl_union_map_empty(isl_space_copy(space
));
3890 space
= isl_space_map_from_set(space
);
3891 map
= isl_map_identity(isl_space_copy(space
));
3892 map
= isl_map_eliminate(map
, isl_dim_out
, depth
, 1);
3893 map
= isl_map_insert_dims(map
, isl_dim_out
, depth
+ 1, 1);
3894 space
= isl_space_insert_dims(space
, isl_dim_out
, depth
+ 1, 1);
3896 c
= isl_constraint_alloc_equality(isl_local_space_from_space(space
));
3897 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, depth
, 1);
3898 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, depth
, -1);
3900 for (i
= 0; i
< n
; ++i
) {
3904 v
= isl_multi_val_get_val(offset
, i
);
3907 map_i
= isl_map_copy(map
);
3908 map_i
= isl_map_fix_val(map_i
, isl_dim_out
, depth
+ 1,
3911 c
= isl_constraint_set_constant_val(c
, v
);
3912 map_i
= isl_map_add_constraint(map_i
, isl_constraint_copy(c
));
3914 map_i
= isl_map_apply_domain(isl_map_copy(domain
[order
[i
]].map
),
3916 executed
= isl_union_map_add_map(executed
, map_i
);
3919 isl_constraint_free(c
);
3923 executed
= isl_union_map_free(executed
);
3928 /* Generate code for a single component, after exposing the stride,
3929 * given that the schedule domain is "shifted strided".
3931 * The component inverse schedule is specified as the "map" fields
3932 * of the elements of "domain" indexed by the first "n" elements of "order".
3934 * The schedule domain being "shifted strided" means that the differences
3935 * between the values of the current dimension of domain "i"
3936 * and the values of the current dimension for some reference domain are
3939 * stride * integer + offset[i]
3941 * We first look for the domain with the "smallest" value for the current
3942 * dimension and adjust the offsets such that the offset of the "smallest"
3943 * domain is equal to zero. The other offsets are reduced modulo stride.
3945 * Based on this information, we construct a new inverse schedule in
3946 * contruct_shifted_executed that exposes the stride.
3947 * Since this involves the introduction of a new schedule dimension,
3948 * the build needs to be changed accodingly.
3949 * After computing the AST, the newly introduced dimension needs
3950 * to be removed again from the list of grafts. We do this by plugging
3951 * in a mapping that represents the new schedule domain in terms of the
3952 * old schedule domain.
3954 static __isl_give isl_ast_graft_list
*generate_shift_component(
3955 struct isl_set_map_pair
*domain
, int *order
, int n
,
3956 __isl_keep isl_val
*stride
, __isl_keep isl_multi_val
*offset
,
3957 __isl_take isl_ast_build
*build
)
3959 isl_ast_graft_list
*list
;
3965 isl_multi_aff
*ma
, *zero
;
3966 isl_union_map
*executed
;
3968 depth
= isl_ast_build_get_depth(build
);
3970 first
= first_offset(domain
, order
, n
, build
);
3974 mv
= isl_multi_val_copy(offset
);
3975 val
= isl_multi_val_get_val(offset
, first
);
3976 val
= isl_val_neg(val
);
3977 mv
= isl_multi_val_add_val(mv
, val
);
3978 mv
= isl_multi_val_mod_val(mv
, isl_val_copy(stride
));
3980 executed
= contruct_shifted_executed(domain
, order
, n
, stride
, mv
,
3982 space
= isl_ast_build_get_space(build
, 1);
3983 space
= isl_space_map_from_set(space
);
3984 ma
= isl_multi_aff_identity(isl_space_copy(space
));
3985 space
= isl_space_from_domain(isl_space_domain(space
));
3986 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3987 zero
= isl_multi_aff_zero(space
);
3988 ma
= isl_multi_aff_range_splice(ma
, depth
+ 1, zero
);
3989 build
= isl_ast_build_insert_dim(build
, depth
+ 1);
3990 list
= generate_shifted_component(executed
, build
);
3992 list
= isl_ast_graft_list_preimage_multi_aff(list
, ma
);
3994 isl_multi_val_free(mv
);
3998 isl_ast_build_free(build
);
4002 /* Does any node in the schedule tree rooted at the current schedule node
4003 * of "build" depend on outer schedule nodes?
4005 static int has_anchored_subtree(__isl_keep isl_ast_build
*build
)
4007 isl_schedule_node
*node
;
4010 node
= isl_ast_build_get_schedule_node(build
);
4011 dependent
= isl_schedule_node_is_subtree_anchored(node
);
4012 isl_schedule_node_free(node
);
4017 /* Generate code for a single component.
4019 * The component inverse schedule is specified as the "map" fields
4020 * of the elements of "domain" indexed by the first "n" elements of "order".
4022 * This function may modify the "set" fields of "domain".
4024 * Before proceeding with the actual code generation for the component,
4025 * we first check if there are any "shifted" strides, meaning that
4026 * the schedule domains of the individual domains are all strided,
4027 * but that they have different offsets, resulting in the union
4028 * of schedule domains not being strided anymore.
4030 * The simplest example is the schedule
4032 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4034 * Both schedule domains are strided, but their union is not.
4035 * This function detects such cases and then rewrites the schedule to
4037 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4039 * In the new schedule, the schedule domains have the same offset (modulo
4040 * the stride), ensuring that the union of schedule domains is also strided.
4043 * If there is only a single domain in the component, then there is
4044 * nothing to do. Similarly, if the current schedule dimension has
4045 * a fixed value for almost all domains then there is nothing to be done.
4046 * In particular, we need at least two domains where the current schedule
4047 * dimension does not have a fixed value.
4048 * Finally, in case of a schedule map input,
4049 * if any of the options refer to the current schedule dimension,
4050 * then we bail out as well. It would be possible to reformulate the options
4051 * in terms of the new schedule domain, but that would introduce constraints
4052 * that separate the domains in the options and that is something we would
4054 * In the case of a schedule tree input, we bail out if any of
4055 * the descendants of the current schedule node refer to outer
4056 * schedule nodes in any way.
4059 * To see if there is any shifted stride, we look at the differences
4060 * between the values of the current dimension in pairs of domains
4061 * for equal values of outer dimensions. These differences should be
4066 * with "m" the stride and "r" a constant. Note that we cannot perform
4067 * this analysis on individual domains as the lower bound in each domain
4068 * may depend on parameters or outer dimensions and so the current dimension
4069 * itself may not have a fixed remainder on division by the stride.
4071 * In particular, we compare the first domain that does not have an
4072 * obviously fixed value for the current dimension to itself and all
4073 * other domains and collect the offsets and the gcd of the strides.
4074 * If the gcd becomes one, then we failed to find shifted strides.
4075 * If the gcd is zero, then the differences were all fixed, meaning
4076 * that some domains had non-obviously fixed values for the current dimension.
4077 * If all the offsets are the same (for those domains that do not have
4078 * an obviously fixed value for the current dimension), then we do not
4079 * apply the transformation.
4080 * If none of the domains were skipped, then there is nothing to do.
4081 * If some of them were skipped, then if we apply separation, the schedule
4082 * domain should get split in pieces with a (non-shifted) stride.
4084 * Otherwise, we apply a shift to expose the stride in
4085 * generate_shift_component.
4087 static __isl_give isl_ast_graft_list
*generate_component(
4088 struct isl_set_map_pair
*domain
, int *order
, int n
,
4089 __isl_take isl_ast_build
*build
)
4096 isl_val
*gcd
= NULL
;
4100 isl_ast_graft_list
*list
;
4103 depth
= isl_ast_build_get_depth(build
);
4106 if (skip
>= 0 && !skip
)
4107 skip
= at_most_one_non_fixed(domain
, order
, n
, depth
);
4108 if (skip
>= 0 && !skip
) {
4109 if (isl_ast_build_has_schedule_node(build
))
4110 skip
= has_anchored_subtree(build
);
4112 skip
= isl_ast_build_options_involve_depth(build
);
4117 return generate_shifted_component_from_list(domain
,
4120 base
= eliminate_non_fixed(domain
, order
, n
, depth
, build
);
4124 ctx
= isl_ast_build_get_ctx(build
);
4126 mv
= isl_multi_val_zero(isl_space_set_alloc(ctx
, 0, n
));
4129 for (i
= 0; i
< n
; ++i
) {
4132 map
= isl_map_from_domain_and_range(
4133 isl_set_copy(domain
[order
[base
]].set
),
4134 isl_set_copy(domain
[order
[i
]].set
));
4135 for (d
= 0; d
< depth
; ++d
)
4136 map
= isl_map_equate(map
, isl_dim_in
, d
,
4138 deltas
= isl_map_deltas(map
);
4139 res
= isl_set_dim_residue_class_val(deltas
, depth
, &m
, &r
);
4140 isl_set_free(deltas
);
4147 gcd
= isl_val_gcd(gcd
, m
);
4148 if (isl_val_is_one(gcd
)) {
4152 mv
= isl_multi_val_set_val(mv
, i
, r
);
4154 res
= dim_is_fixed(domain
[order
[i
]].set
, depth
);
4160 if (fixed
&& i
> base
) {
4162 a
= isl_multi_val_get_val(mv
, i
);
4163 b
= isl_multi_val_get_val(mv
, base
);
4164 if (isl_val_ne(a
, b
))
4171 if (res
< 0 || !gcd
) {
4172 isl_ast_build_free(build
);
4174 } else if (i
< n
|| fixed
|| isl_val_is_zero(gcd
)) {
4175 list
= generate_shifted_component_from_list(domain
,
4178 list
= generate_shift_component(domain
, order
, n
, gcd
, mv
,
4183 isl_multi_val_free(mv
);
4187 isl_ast_build_free(build
);
4191 /* Store both "map" itself and its domain in the
4192 * structure pointed to by *next and advance to the next array element.
4194 static isl_stat
extract_domain(__isl_take isl_map
*map
, void *user
)
4196 struct isl_set_map_pair
**next
= user
;
4198 (*next
)->map
= isl_map_copy(map
);
4199 (*next
)->set
= isl_map_domain(map
);
4205 static int after_in_tree(__isl_keep isl_union_map
*umap
,
4206 __isl_keep isl_schedule_node
*node
);
4208 /* Is any domain element of "umap" scheduled after any of
4209 * the corresponding image elements by the tree rooted at
4210 * the child of "node"?
4212 static int after_in_child(__isl_keep isl_union_map
*umap
,
4213 __isl_keep isl_schedule_node
*node
)
4215 isl_schedule_node
*child
;
4218 child
= isl_schedule_node_get_child(node
, 0);
4219 after
= after_in_tree(umap
, child
);
4220 isl_schedule_node_free(child
);
4225 /* Is any domain element of "umap" scheduled after any of
4226 * the corresponding image elements by the tree rooted at
4227 * the band node "node"?
4229 * We first check if any domain element is scheduled after any
4230 * of the corresponding image elements by the band node itself.
4231 * If not, we restrict "map" to those pairs of element that
4232 * are scheduled together by the band node and continue with
4233 * the child of the band node.
4234 * If there are no such pairs then the map passed to after_in_child
4235 * will be empty causing it to return 0.
4237 static int after_in_band(__isl_keep isl_union_map
*umap
,
4238 __isl_keep isl_schedule_node
*node
)
4240 isl_multi_union_pw_aff
*mupa
;
4241 isl_union_map
*partial
, *test
, *gt
, *universe
, *umap1
, *umap2
;
4242 isl_union_set
*domain
, *range
;
4247 if (isl_schedule_node_band_n_member(node
) == 0)
4248 return after_in_child(umap
, node
);
4250 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
4251 space
= isl_multi_union_pw_aff_get_space(mupa
);
4252 partial
= isl_union_map_from_multi_union_pw_aff(mupa
);
4253 test
= isl_union_map_copy(umap
);
4254 test
= isl_union_map_apply_domain(test
, isl_union_map_copy(partial
));
4255 test
= isl_union_map_apply_range(test
, isl_union_map_copy(partial
));
4256 gt
= isl_union_map_from_map(isl_map_lex_gt(space
));
4257 test
= isl_union_map_intersect(test
, gt
);
4258 empty
= isl_union_map_is_empty(test
);
4259 isl_union_map_free(test
);
4261 if (empty
< 0 || !empty
) {
4262 isl_union_map_free(partial
);
4263 return empty
< 0 ? -1 : 1;
4266 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4267 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4268 range
= isl_union_map_range(universe
);
4269 umap1
= isl_union_map_copy(partial
);
4270 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4271 umap2
= isl_union_map_intersect_domain(partial
, range
);
4272 test
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4273 test
= isl_union_map_intersect(test
, isl_union_map_copy(umap
));
4274 after
= after_in_child(test
, node
);
4275 isl_union_map_free(test
);
4279 /* Is any domain element of "umap" scheduled after any of
4280 * the corresponding image elements by the tree rooted at
4281 * the context node "node"?
4283 * The context constraints apply to the schedule domain,
4284 * so we cannot apply them directly to "umap", which contains
4285 * pairs of statement instances. Instead, we add them
4286 * to the range of the prefix schedule for both domain and
4289 static int after_in_context(__isl_keep isl_union_map
*umap
,
4290 __isl_keep isl_schedule_node
*node
)
4292 isl_union_map
*prefix
, *universe
, *umap1
, *umap2
;
4293 isl_union_set
*domain
, *range
;
4297 umap
= isl_union_map_copy(umap
);
4298 context
= isl_schedule_node_context_get_context(node
);
4299 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4300 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
4301 domain
= isl_union_map_domain(isl_union_map_copy(universe
));
4302 range
= isl_union_map_range(universe
);
4303 umap1
= isl_union_map_copy(prefix
);
4304 umap1
= isl_union_map_intersect_domain(umap1
, domain
);
4305 umap2
= isl_union_map_intersect_domain(prefix
, range
);
4306 umap1
= isl_union_map_intersect_range(umap1
,
4307 isl_union_set_from_set(context
));
4308 umap1
= isl_union_map_apply_range(umap1
, isl_union_map_reverse(umap2
));
4309 umap
= isl_union_map_intersect(umap
, umap1
);
4311 after
= after_in_child(umap
, node
);
4313 isl_union_map_free(umap
);
4318 /* Is any domain element of "umap" scheduled after any of
4319 * the corresponding image elements by the tree rooted at
4320 * the expansion node "node"?
4322 * We apply the expansion to domain and range of "umap" and
4323 * continue with its child.
4325 static int after_in_expansion(__isl_keep isl_union_map
*umap
,
4326 __isl_keep isl_schedule_node
*node
)
4328 isl_union_map
*expansion
;
4331 expansion
= isl_schedule_node_expansion_get_expansion(node
);
4332 umap
= isl_union_map_copy(umap
);
4333 umap
= isl_union_map_apply_domain(umap
, isl_union_map_copy(expansion
));
4334 umap
= isl_union_map_apply_range(umap
, expansion
);
4336 after
= after_in_child(umap
, node
);
4338 isl_union_map_free(umap
);
4343 /* Is any domain element of "umap" scheduled after any of
4344 * the corresponding image elements by the tree rooted at
4345 * the extension node "node"?
4347 * Since the extension node may add statement instances before or
4348 * after the pairs of statement instances in "umap", we return 1
4349 * to ensure that these pairs are not broken up.
4351 static int after_in_extension(__isl_keep isl_union_map
*umap
,
4352 __isl_keep isl_schedule_node
*node
)
4357 /* Is any domain element of "umap" scheduled after any of
4358 * the corresponding image elements by the tree rooted at
4359 * the filter node "node"?
4361 * We intersect domain and range of "umap" with the filter and
4362 * continue with its child.
4364 static int after_in_filter(__isl_keep isl_union_map
*umap
,
4365 __isl_keep isl_schedule_node
*node
)
4367 isl_union_set
*filter
;
4370 umap
= isl_union_map_copy(umap
);
4371 filter
= isl_schedule_node_filter_get_filter(node
);
4372 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(filter
));
4373 umap
= isl_union_map_intersect_range(umap
, filter
);
4375 after
= after_in_child(umap
, node
);
4377 isl_union_map_free(umap
);
4382 /* Is any domain element of "umap" scheduled after any of
4383 * the corresponding image elements by the tree rooted at
4384 * the set node "node"?
4386 * This is only the case if this condition holds in any
4387 * of the (filter) children of the set node.
4388 * In particular, if the domain and the range of "umap"
4389 * are contained in different children, then the condition
4392 static int after_in_set(__isl_keep isl_union_map
*umap
,
4393 __isl_keep isl_schedule_node
*node
)
4397 n
= isl_schedule_node_n_children(node
);
4398 for (i
= 0; i
< n
; ++i
) {
4399 isl_schedule_node
*child
;
4402 child
= isl_schedule_node_get_child(node
, i
);
4403 after
= after_in_tree(umap
, child
);
4404 isl_schedule_node_free(child
);
4406 if (after
< 0 || after
)
4413 /* Return the filter of child "i" of "node".
4415 static __isl_give isl_union_set
*child_filter(
4416 __isl_keep isl_schedule_node
*node
, int i
)
4418 isl_schedule_node
*child
;
4419 isl_union_set
*filter
;
4421 child
= isl_schedule_node_get_child(node
, i
);
4422 filter
= isl_schedule_node_filter_get_filter(child
);
4423 isl_schedule_node_free(child
);
4428 /* Is any domain element of "umap" scheduled after any of
4429 * the corresponding image elements by the tree rooted at
4430 * the sequence node "node"?
4432 * This happens in particular if any domain element is
4433 * contained in a later child than one containing a range element or
4434 * if the condition holds within a given child in the sequence.
4435 * The later part of the condition is checked by after_in_set.
4437 static int after_in_sequence(__isl_keep isl_union_map
*umap
,
4438 __isl_keep isl_schedule_node
*node
)
4441 isl_union_map
*umap_i
;
4442 int empty
, after
= 0;
4444 n
= isl_schedule_node_n_children(node
);
4445 for (i
= 1; i
< n
; ++i
) {
4446 isl_union_set
*filter_i
;
4448 umap_i
= isl_union_map_copy(umap
);
4449 filter_i
= child_filter(node
, i
);
4450 umap_i
= isl_union_map_intersect_domain(umap_i
, filter_i
);
4451 empty
= isl_union_map_is_empty(umap_i
);
4455 isl_union_map_free(umap_i
);
4459 for (j
= 0; j
< i
; ++j
) {
4460 isl_union_set
*filter_j
;
4461 isl_union_map
*umap_ij
;
4463 umap_ij
= isl_union_map_copy(umap_i
);
4464 filter_j
= child_filter(node
, j
);
4465 umap_ij
= isl_union_map_intersect_range(umap_ij
,
4467 empty
= isl_union_map_is_empty(umap_ij
);
4468 isl_union_map_free(umap_ij
);
4478 isl_union_map_free(umap_i
);
4483 if (after
< 0 || after
)
4486 return after_in_set(umap
, node
);
4488 isl_union_map_free(umap_i
);
4492 /* Is any domain element of "umap" scheduled after any of
4493 * the corresponding image elements by the tree rooted at "node"?
4495 * If "umap" is empty, then clearly there is no such element.
4496 * Otherwise, consider the different types of nodes separately.
4498 static int after_in_tree(__isl_keep isl_union_map
*umap
,
4499 __isl_keep isl_schedule_node
*node
)
4502 enum isl_schedule_node_type type
;
4504 empty
= isl_union_map_is_empty(umap
);
4512 type
= isl_schedule_node_get_type(node
);
4514 case isl_schedule_node_error
:
4516 case isl_schedule_node_leaf
:
4518 case isl_schedule_node_band
:
4519 return after_in_band(umap
, node
);
4520 case isl_schedule_node_domain
:
4521 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
4522 "unexpected internal domain node", return -1);
4523 case isl_schedule_node_context
:
4524 return after_in_context(umap
, node
);
4525 case isl_schedule_node_expansion
:
4526 return after_in_expansion(umap
, node
);
4527 case isl_schedule_node_extension
:
4528 return after_in_extension(umap
, node
);
4529 case isl_schedule_node_filter
:
4530 return after_in_filter(umap
, node
);
4531 case isl_schedule_node_guard
:
4532 case isl_schedule_node_mark
:
4533 return after_in_child(umap
, node
);
4534 case isl_schedule_node_set
:
4535 return after_in_set(umap
, node
);
4536 case isl_schedule_node_sequence
:
4537 return after_in_sequence(umap
, node
);
4543 /* Is any domain element of "map1" scheduled after any domain
4544 * element of "map2" by the subtree underneath the current band node,
4545 * while at the same time being scheduled together by the current
4546 * band node, i.e., by "map1" and "map2?
4548 * If the child of the current band node is a leaf, then
4549 * no element can be scheduled after any other element.
4551 * Otherwise, we construct a relation between domain elements
4552 * of "map1" and domain elements of "map2" that are scheduled
4553 * together and then check if the subtree underneath the current
4554 * band node determines their relative order.
4556 static int after_in_subtree(__isl_keep isl_ast_build
*build
,
4557 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4559 isl_schedule_node
*node
;
4561 isl_union_map
*umap
;
4564 node
= isl_ast_build_get_schedule_node(build
);
4567 node
= isl_schedule_node_child(node
, 0);
4568 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
4569 isl_schedule_node_free(node
);
4572 map
= isl_map_copy(map2
);
4573 map
= isl_map_apply_domain(map
, isl_map_copy(map1
));
4574 umap
= isl_union_map_from_map(map
);
4575 after
= after_in_tree(umap
, node
);
4576 isl_union_map_free(umap
);
4577 isl_schedule_node_free(node
);
4581 /* Internal data for any_scheduled_after.
4583 * "build" is the build in which the AST is constructed.
4584 * "depth" is the number of loops that have already been generated
4585 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4586 * "domain" is an array of set-map pairs corresponding to the different
4587 * iteration domains. The set is the schedule domain, i.e., the domain
4588 * of the inverse schedule, while the map is the inverse schedule itself.
4590 struct isl_any_scheduled_after_data
{
4591 isl_ast_build
*build
;
4593 int group_coscheduled
;
4594 struct isl_set_map_pair
*domain
;
4597 /* Is any element of domain "i" scheduled after any element of domain "j"
4598 * (for a common iteration of the first data->depth loops)?
4600 * data->domain[i].set contains the domain of the inverse schedule
4601 * for domain "i", i.e., elements in the schedule domain.
4603 * If we are inside a band of a schedule tree and there is a pair
4604 * of elements in the two domains that is schedule together by
4605 * the current band, then we check if any element of "i" may be schedule
4606 * after element of "j" by the descendants of the band node.
4608 * If data->group_coscheduled is set, then we also return 1 if there
4609 * is any pair of elements in the two domains that are scheduled together.
4611 static isl_bool
any_scheduled_after(int i
, int j
, void *user
)
4613 struct isl_any_scheduled_after_data
*data
= user
;
4614 int dim
= isl_set_dim(data
->domain
[i
].set
, isl_dim_set
);
4617 for (pos
= data
->depth
; pos
< dim
; ++pos
) {
4620 follows
= isl_set_follows_at(data
->domain
[i
].set
,
4621 data
->domain
[j
].set
, pos
);
4624 return isl_bool_error
;
4626 return isl_bool_true
;
4628 return isl_bool_false
;
4631 if (isl_ast_build_has_schedule_node(data
->build
)) {
4634 after
= after_in_subtree(data
->build
, data
->domain
[i
].map
,
4635 data
->domain
[j
].map
);
4636 if (after
< 0 || after
)
4640 return data
->group_coscheduled
;
4643 /* Look for independent components at the current depth and generate code
4644 * for each component separately. The resulting lists of grafts are
4645 * merged in an attempt to combine grafts with identical guards.
4647 * Code for two domains can be generated separately if all the elements
4648 * of one domain are scheduled before (or together with) all the elements
4649 * of the other domain. We therefore consider the graph with as nodes
4650 * the domains and an edge between two nodes if any element of the first
4651 * node is scheduled after any element of the second node.
4652 * If the ast_build_group_coscheduled is set, then we also add an edge if
4653 * there is any pair of elements in the two domains that are scheduled
4655 * Code is then generated (by generate_component)
4656 * for each of the strongly connected components in this graph
4657 * in their topological order.
4659 * Since the test is performed on the domain of the inverse schedules of
4660 * the different domains, we precompute these domains and store
4661 * them in data.domain.
4663 static __isl_give isl_ast_graft_list
*generate_components(
4664 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4667 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4668 int n
= isl_union_map_n_map(executed
);
4669 struct isl_any_scheduled_after_data data
;
4670 struct isl_set_map_pair
*next
;
4671 struct isl_tarjan_graph
*g
= NULL
;
4672 isl_ast_graft_list
*list
= NULL
;
4675 data
.domain
= isl_calloc_array(ctx
, struct isl_set_map_pair
, n
);
4681 if (isl_union_map_foreach_map(executed
, &extract_domain
, &next
) < 0)
4687 data
.depth
= isl_ast_build_get_depth(build
);
4688 data
.group_coscheduled
= isl_options_get_ast_build_group_coscheduled(ctx
);
4689 g
= isl_tarjan_graph_init(ctx
, n
, &any_scheduled_after
, &data
);
4693 list
= isl_ast_graft_list_alloc(ctx
, 0);
4697 isl_ast_graft_list
*list_c
;
4700 if (g
->order
[i
] == -1)
4701 isl_die(ctx
, isl_error_internal
, "cannot happen",
4704 while (g
->order
[i
] != -1) {
4708 list_c
= generate_component(data
.domain
,
4709 g
->order
+ first
, i
- first
,
4710 isl_ast_build_copy(build
));
4711 list
= isl_ast_graft_list_merge(list
, list_c
, build
);
4717 error
: list
= isl_ast_graft_list_free(list
);
4718 isl_tarjan_graph_free(g
);
4719 for (i
= 0; i
< n_domain
; ++i
) {
4720 isl_map_free(data
.domain
[i
].map
);
4721 isl_set_free(data
.domain
[i
].set
);
4724 isl_union_map_free(executed
);
4725 isl_ast_build_free(build
);
4730 /* Generate code for the next level (and all inner levels).
4732 * If "executed" is empty, i.e., no code needs to be generated,
4733 * then we return an empty list.
4735 * If we have already generated code for all loop levels, then we pass
4736 * control to generate_inner_level.
4738 * If "executed" lives in a single space, i.e., if code needs to be
4739 * generated for a single domain, then there can only be a single
4740 * component and we go directly to generate_shifted_component.
4741 * Otherwise, we call generate_components to detect the components
4742 * and to call generate_component on each of them separately.
4744 static __isl_give isl_ast_graft_list
*generate_next_level(
4745 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
)
4749 if (!build
|| !executed
)
4752 if (isl_union_map_is_empty(executed
)) {
4753 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
4754 isl_union_map_free(executed
);
4755 isl_ast_build_free(build
);
4756 return isl_ast_graft_list_alloc(ctx
, 0);
4759 depth
= isl_ast_build_get_depth(build
);
4760 if (depth
>= isl_ast_build_dim(build
, isl_dim_set
))
4761 return generate_inner_level(executed
, build
);
4763 if (isl_union_map_n_map(executed
) == 1)
4764 return generate_shifted_component(executed
, build
);
4766 return generate_components(executed
, build
);
4768 isl_union_map_free(executed
);
4769 isl_ast_build_free(build
);
4773 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4774 * internal, executed and build are the inputs to generate_code.
4775 * list collects the output.
4777 struct isl_generate_code_data
{
4779 isl_union_map
*executed
;
4780 isl_ast_build
*build
;
4782 isl_ast_graft_list
*list
;
4785 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4789 * with E the external build schedule and S the additional schedule "space",
4790 * reformulate the inverse schedule in terms of the internal schedule domain,
4795 * We first obtain a mapping
4799 * take the inverse and the product with S -> S, resulting in
4801 * [I -> S] -> [E -> S]
4803 * Applying the map to the input produces the desired result.
4805 static __isl_give isl_union_map
*internal_executed(
4806 __isl_take isl_union_map
*executed
, __isl_keep isl_space
*space
,
4807 __isl_keep isl_ast_build
*build
)
4811 proj
= isl_ast_build_get_schedule_map(build
);
4812 proj
= isl_map_reverse(proj
);
4813 space
= isl_space_map_from_set(isl_space_copy(space
));
4814 id
= isl_map_identity(space
);
4815 proj
= isl_map_product(proj
, id
);
4816 executed
= isl_union_map_apply_domain(executed
,
4817 isl_union_map_from_map(proj
));
4821 /* Generate an AST that visits the elements in the range of data->executed
4822 * in the relative order specified by the corresponding domain element(s)
4823 * for those domain elements that belong to "set".
4824 * Add the result to data->list.
4826 * The caller ensures that "set" is a universe domain.
4827 * "space" is the space of the additional part of the schedule.
4828 * It is equal to the space of "set" if build->domain is parametric.
4829 * Otherwise, it is equal to the range of the wrapped space of "set".
4831 * If the build space is not parametric and
4832 * if isl_ast_build_node_from_schedule_map
4833 * was called from an outside user (data->internal not set), then
4834 * the (inverse) schedule refers to the external build domain and needs to
4835 * be transformed to refer to the internal build domain.
4837 * If the build space is parametric, then we add some of the parameter
4838 * constraints to the executed relation. Adding these constraints
4839 * allows for an earlier detection of conflicts in some cases.
4840 * However, we do not want to divide the executed relation into
4841 * more disjuncts than necessary. We therefore approximate
4842 * the constraints on the parameters by a single disjunct set.
4844 * The build is extended to include the additional part of the schedule.
4845 * If the original build space was not parametric, then the options
4846 * in data->build refer only to the additional part of the schedule
4847 * and they need to be adjusted to refer to the complete AST build
4850 * After having adjusted inverse schedule and build, we start generating
4851 * code with the outer loop of the current code generation
4852 * in generate_next_level.
4854 * If the original build space was not parametric, we undo the embedding
4855 * on the resulting isl_ast_node_list so that it can be used within
4856 * the outer AST build.
4858 static isl_stat
generate_code_in_space(struct isl_generate_code_data
*data
,
4859 __isl_take isl_set
*set
, __isl_take isl_space
*space
)
4861 isl_union_map
*executed
;
4862 isl_ast_build
*build
;
4863 isl_ast_graft_list
*list
;
4866 executed
= isl_union_map_copy(data
->executed
);
4867 executed
= isl_union_map_intersect_domain(executed
,
4868 isl_union_set_from_set(set
));
4870 embed
= !isl_set_is_params(data
->build
->domain
);
4871 if (embed
&& !data
->internal
)
4872 executed
= internal_executed(executed
, space
, data
->build
);
4875 domain
= isl_ast_build_get_domain(data
->build
);
4876 domain
= isl_set_from_basic_set(isl_set_simple_hull(domain
));
4877 executed
= isl_union_map_intersect_params(executed
, domain
);
4880 build
= isl_ast_build_copy(data
->build
);
4881 build
= isl_ast_build_product(build
, space
);
4883 list
= generate_next_level(executed
, build
);
4885 list
= isl_ast_graft_list_unembed(list
, embed
);
4887 data
->list
= isl_ast_graft_list_concat(data
->list
, list
);
4892 /* Generate an AST that visits the elements in the range of data->executed
4893 * in the relative order specified by the corresponding domain element(s)
4894 * for those domain elements that belong to "set".
4895 * Add the result to data->list.
4897 * The caller ensures that "set" is a universe domain.
4899 * If the build space S is not parametric, then the space of "set"
4900 * need to be a wrapped relation with S as domain. That is, it needs
4905 * Check this property and pass control to generate_code_in_space
4907 * If the build space is not parametric, then T is the space of "set".
4909 static isl_stat
generate_code_set(__isl_take isl_set
*set
, void *user
)
4911 struct isl_generate_code_data
*data
= user
;
4912 isl_space
*space
, *build_space
;
4915 space
= isl_set_get_space(set
);
4917 if (isl_set_is_params(data
->build
->domain
))
4918 return generate_code_in_space(data
, set
, space
);
4920 build_space
= isl_ast_build_get_space(data
->build
, data
->internal
);
4921 space
= isl_space_unwrap(space
);
4922 is_domain
= isl_space_is_domain(build_space
, space
);
4923 isl_space_free(build_space
);
4924 space
= isl_space_range(space
);
4929 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4930 "invalid nested schedule space", goto error
);
4932 return generate_code_in_space(data
, set
, space
);
4935 isl_space_free(space
);
4936 return isl_stat_error
;
4939 /* Generate an AST that visits the elements in the range of "executed"
4940 * in the relative order specified by the corresponding domain element(s).
4942 * "build" is an isl_ast_build that has either been constructed by
4943 * isl_ast_build_from_context or passed to a callback set by
4944 * isl_ast_build_set_create_leaf.
4945 * In the first case, the space of the isl_ast_build is typically
4946 * a parametric space, although this is currently not enforced.
4947 * In the second case, the space is never a parametric space.
4948 * If the space S is not parametric, then the domain space(s) of "executed"
4949 * need to be wrapped relations with S as domain.
4951 * If the domain of "executed" consists of several spaces, then an AST
4952 * is generated for each of them (in arbitrary order) and the results
4955 * If "internal" is set, then the domain "S" above refers to the internal
4956 * schedule domain representation. Otherwise, it refers to the external
4957 * representation, as returned by isl_ast_build_get_schedule_space.
4959 * We essentially run over all the spaces in the domain of "executed"
4960 * and call generate_code_set on each of them.
4962 static __isl_give isl_ast_graft_list
*generate_code(
4963 __isl_take isl_union_map
*executed
, __isl_take isl_ast_build
*build
,
4967 struct isl_generate_code_data data
= { 0 };
4969 isl_union_set
*schedule_domain
;
4970 isl_union_map
*universe
;
4974 space
= isl_ast_build_get_space(build
, 1);
4975 space
= isl_space_align_params(space
,
4976 isl_union_map_get_space(executed
));
4977 space
= isl_space_align_params(space
,
4978 isl_union_map_get_space(build
->options
));
4979 build
= isl_ast_build_align_params(build
, isl_space_copy(space
));
4980 executed
= isl_union_map_align_params(executed
, space
);
4981 if (!executed
|| !build
)
4984 ctx
= isl_ast_build_get_ctx(build
);
4986 data
.internal
= internal
;
4987 data
.executed
= executed
;
4989 data
.list
= isl_ast_graft_list_alloc(ctx
, 0);
4991 universe
= isl_union_map_universe(isl_union_map_copy(executed
));
4992 schedule_domain
= isl_union_map_domain(universe
);
4993 if (isl_union_set_foreach_set(schedule_domain
, &generate_code_set
,
4995 data
.list
= isl_ast_graft_list_free(data
.list
);
4997 isl_union_set_free(schedule_domain
);
4998 isl_union_map_free(executed
);
5000 isl_ast_build_free(build
);
5003 isl_union_map_free(executed
);
5004 isl_ast_build_free(build
);
5008 /* Generate an AST that visits the elements in the domain of "schedule"
5009 * in the relative order specified by the corresponding image element(s).
5011 * "build" is an isl_ast_build that has either been constructed by
5012 * isl_ast_build_from_context or passed to a callback set by
5013 * isl_ast_build_set_create_leaf.
5014 * In the first case, the space of the isl_ast_build is typically
5015 * a parametric space, although this is currently not enforced.
5016 * In the second case, the space is never a parametric space.
5017 * If the space S is not parametric, then the range space(s) of "schedule"
5018 * need to be wrapped relations with S as domain.
5020 * If the range of "schedule" consists of several spaces, then an AST
5021 * is generated for each of them (in arbitrary order) and the results
5024 * We first initialize the local copies of the relevant options.
5025 * We do this here rather than when the isl_ast_build is created
5026 * because the options may have changed between the construction
5027 * of the isl_ast_build and the call to isl_generate_code.
5029 * The main computation is performed on an inverse schedule (with
5030 * the schedule domain in the domain and the elements to be executed
5031 * in the range) called "executed".
5033 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule_map(
5034 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
5036 isl_ast_graft_list
*list
;
5038 isl_union_map
*executed
;
5040 build
= isl_ast_build_copy(build
);
5041 build
= isl_ast_build_set_single_valued(build
, 0);
5042 schedule
= isl_union_map_coalesce(schedule
);
5043 schedule
= isl_union_map_remove_redundancies(schedule
);
5044 executed
= isl_union_map_reverse(schedule
);
5045 list
= generate_code(executed
, isl_ast_build_copy(build
), 0);
5046 node
= isl_ast_node_from_graft_list(list
, build
);
5047 isl_ast_build_free(build
);
5052 /* The old name for isl_ast_build_node_from_schedule_map.
5053 * It is being kept for backward compatibility, but
5054 * it will be removed in the future.
5056 __isl_give isl_ast_node
*isl_ast_build_ast_from_schedule(
5057 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*schedule
)
5059 return isl_ast_build_node_from_schedule_map(build
, schedule
);
5062 /* Generate an AST that visits the elements in the domain of "executed"
5063 * in the relative order specified by the band node "node" and its descendants.
5065 * The relation "executed" maps the outer generated loop iterators
5066 * to the domain elements executed by those iterations.
5068 * If the band is empty, we continue with its descendants.
5069 * Otherwise, we extend the build and the inverse schedule with
5070 * the additional space/partial schedule and continue generating
5071 * an AST in generate_next_level.
5072 * As soon as we have extended the inverse schedule with the additional
5073 * partial schedule, we look for equalities that may exists between
5074 * the old and the new part.
5076 static __isl_give isl_ast_graft_list
*build_ast_from_band(
5077 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5078 __isl_take isl_union_map
*executed
)
5081 isl_multi_union_pw_aff
*extra
;
5082 isl_union_map
*extra_umap
;
5083 isl_ast_graft_list
*list
;
5086 if (!build
|| !node
|| !executed
)
5089 if (isl_schedule_node_band_n_member(node
) == 0)
5090 return build_ast_from_child(build
, node
, executed
);
5092 extra
= isl_schedule_node_band_get_partial_schedule(node
);
5093 extra
= isl_multi_union_pw_aff_align_params(extra
,
5094 isl_ast_build_get_space(build
, 1));
5095 space
= isl_multi_union_pw_aff_get_space(extra
);
5097 extra_umap
= isl_union_map_from_multi_union_pw_aff(extra
);
5098 extra_umap
= isl_union_map_reverse(extra_umap
);
5100 executed
= isl_union_map_domain_product(executed
, extra_umap
);
5101 executed
= isl_union_map_detect_equalities(executed
);
5103 n1
= isl_ast_build_dim(build
, isl_dim_param
);
5104 build
= isl_ast_build_product(build
, space
);
5105 n2
= isl_ast_build_dim(build
, isl_dim_param
);
5107 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5108 "band node is not allowed to introduce new parameters",
5109 build
= isl_ast_build_free(build
));
5110 build
= isl_ast_build_set_schedule_node(build
, node
);
5112 list
= generate_next_level(executed
, build
);
5114 list
= isl_ast_graft_list_unembed(list
, 1);
5118 isl_schedule_node_free(node
);
5119 isl_union_map_free(executed
);
5120 isl_ast_build_free(build
);
5124 /* Hoist a list of grafts (in practice containing a single graft)
5125 * from "sub_build" (which includes extra context information)
5128 * In particular, project out all additional parameters introduced
5129 * by the context node from the enforced constraints and the guard
5130 * of the single graft.
5132 static __isl_give isl_ast_graft_list
*hoist_out_of_context(
5133 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
,
5134 __isl_keep isl_ast_build
*sub_build
)
5136 isl_ast_graft
*graft
;
5137 isl_basic_set
*enforced
;
5139 unsigned n_param
, extra_param
;
5141 if (!build
|| !sub_build
)
5142 return isl_ast_graft_list_free(list
);
5144 n_param
= isl_ast_build_dim(build
, isl_dim_param
);
5145 extra_param
= isl_ast_build_dim(sub_build
, isl_dim_param
);
5147 if (extra_param
== n_param
)
5150 extra_param
-= n_param
;
5151 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, sub_build
);
5152 enforced
= isl_basic_set_project_out(enforced
, isl_dim_param
,
5153 n_param
, extra_param
);
5154 enforced
= isl_basic_set_remove_unknown_divs(enforced
);
5155 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5156 guard
= isl_set_remove_divs_involving_dims(guard
, isl_dim_param
,
5157 n_param
, extra_param
);
5158 guard
= isl_set_project_out(guard
, isl_dim_param
, n_param
, extra_param
);
5159 guard
= isl_set_compute_divs(guard
);
5160 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5162 list
= isl_ast_graft_list_from_ast_graft(graft
);
5167 /* Generate an AST that visits the elements in the domain of "executed"
5168 * in the relative order specified by the context node "node"
5169 * and its descendants.
5171 * The relation "executed" maps the outer generated loop iterators
5172 * to the domain elements executed by those iterations.
5174 * The context node may introduce additional parameters as well as
5175 * constraints on the outer schedule dimenions or original parameters.
5177 * We add the extra parameters to a new build and the context
5178 * constraints to both the build and (as a single disjunct)
5179 * to the domain of "executed". Since the context constraints
5180 * are specified in terms of the input schedule, we first need
5181 * to map them to the internal schedule domain.
5183 * After constructing the AST from the descendants of "node",
5184 * we combine the list of grafts into a single graft within
5185 * the new build, in order to be able to exploit the additional
5186 * context constraints during this combination.
5188 * Additionally, if the current node is the outermost node in
5189 * the schedule tree (apart from the root domain node), we generate
5190 * all pending guards, again to be able to exploit the additional
5191 * context constraints. We currently do not do this for internal
5192 * context nodes since we may still want to hoist conditions
5193 * to outer AST nodes.
5195 * If the context node introduced any new parameters, then they
5196 * are removed from the set of enforced constraints and guard
5197 * in hoist_out_of_context.
5199 static __isl_give isl_ast_graft_list
*build_ast_from_context(
5200 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5201 __isl_take isl_union_map
*executed
)
5205 isl_multi_aff
*internal2input
;
5206 isl_ast_build
*sub_build
;
5207 isl_ast_graft_list
*list
;
5210 depth
= isl_schedule_node_get_tree_depth(node
);
5211 space
= isl_ast_build_get_space(build
, 1);
5212 context
= isl_schedule_node_context_get_context(node
);
5213 context
= isl_set_align_params(context
, space
);
5214 sub_build
= isl_ast_build_copy(build
);
5215 space
= isl_set_get_space(context
);
5216 sub_build
= isl_ast_build_align_params(sub_build
, space
);
5217 internal2input
= isl_ast_build_get_internal2input(sub_build
);
5218 context
= isl_set_preimage_multi_aff(context
, internal2input
);
5219 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5220 isl_set_copy(context
));
5221 context
= isl_set_from_basic_set(isl_set_simple_hull(context
));
5222 executed
= isl_union_map_intersect_domain(executed
,
5223 isl_union_set_from_set(context
));
5225 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5227 n
= isl_ast_graft_list_n_ast_graft(list
);
5229 list
= isl_ast_graft_list_free(list
);
5231 list
= isl_ast_graft_list_fuse(list
, sub_build
);
5233 list
= isl_ast_graft_list_insert_pending_guard_nodes(list
,
5236 list
= hoist_out_of_context(list
, build
, sub_build
);
5238 isl_ast_build_free(build
);
5239 isl_ast_build_free(sub_build
);
5244 /* Generate an AST that visits the elements in the domain of "executed"
5245 * in the relative order specified by the expansion node "node" and
5248 * The relation "executed" maps the outer generated loop iterators
5249 * to the domain elements executed by those iterations.
5251 * We expand the domain elements by the expansion and
5252 * continue with the descendants of the node.
5254 static __isl_give isl_ast_graft_list
*build_ast_from_expansion(
5255 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5256 __isl_take isl_union_map
*executed
)
5258 isl_union_map
*expansion
;
5261 expansion
= isl_schedule_node_expansion_get_expansion(node
);
5262 expansion
= isl_union_map_align_params(expansion
,
5263 isl_union_map_get_space(executed
));
5265 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5266 executed
= isl_union_map_apply_range(executed
, expansion
);
5267 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5269 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5270 "expansion node is not allowed to introduce "
5271 "new parameters", goto error
);
5273 return build_ast_from_child(build
, node
, executed
);
5275 isl_ast_build_free(build
);
5276 isl_schedule_node_free(node
);
5277 isl_union_map_free(executed
);
5281 /* Generate an AST that visits the elements in the domain of "executed"
5282 * in the relative order specified by the extension node "node" and
5285 * The relation "executed" maps the outer generated loop iterators
5286 * to the domain elements executed by those iterations.
5288 * Extend the inverse schedule with the extension applied to current
5289 * set of generated constraints. Since the extension if formulated
5290 * in terms of the input schedule, it first needs to be transformed
5291 * to refer to the internal schedule.
5293 static __isl_give isl_ast_graft_list
*build_ast_from_extension(
5294 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5295 __isl_take isl_union_map
*executed
)
5297 isl_union_set
*schedule_domain
;
5298 isl_union_map
*extension
;
5301 set
= isl_ast_build_get_generated(build
);
5302 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5303 schedule_domain
= isl_union_set_from_set(set
);
5305 extension
= isl_schedule_node_extension_get_extension(node
);
5307 extension
= isl_union_map_preimage_domain_multi_aff(extension
,
5308 isl_multi_aff_copy(build
->internal2input
));
5309 extension
= isl_union_map_intersect_domain(extension
, schedule_domain
);
5310 extension
= isl_ast_build_substitute_values_union_map_domain(build
,
5312 executed
= isl_union_map_union(executed
, extension
);
5314 return build_ast_from_child(build
, node
, executed
);
5317 /* Generate an AST that visits the elements in the domain of "executed"
5318 * in the relative order specified by the filter node "node" and
5321 * The relation "executed" maps the outer generated loop iterators
5322 * to the domain elements executed by those iterations.
5324 * We simply intersect the iteration domain (i.e., the range of "executed")
5325 * with the filter and continue with the descendants of the node,
5326 * unless the resulting inverse schedule is empty, in which
5327 * case we return an empty list.
5329 * If the result of the intersection is equal to the original "executed"
5330 * relation, then keep the original representation since the intersection
5331 * may have unnecessarily broken up the relation into a greater number
5334 static __isl_give isl_ast_graft_list
*build_ast_from_filter(
5335 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5336 __isl_take isl_union_map
*executed
)
5339 isl_union_set
*filter
;
5340 isl_union_map
*orig
;
5341 isl_ast_graft_list
*list
;
5346 orig
= isl_union_map_copy(executed
);
5347 if (!build
|| !node
|| !executed
)
5350 filter
= isl_schedule_node_filter_get_filter(node
);
5351 filter
= isl_union_set_align_params(filter
,
5352 isl_union_map_get_space(executed
));
5353 n1
= isl_union_map_dim(executed
, isl_dim_param
);
5354 executed
= isl_union_map_intersect_range(executed
, filter
);
5355 n2
= isl_union_map_dim(executed
, isl_dim_param
);
5357 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5358 "filter node is not allowed to introduce "
5359 "new parameters", goto error
);
5361 unchanged
= isl_union_map_is_subset(orig
, executed
);
5362 empty
= isl_union_map_is_empty(executed
);
5363 if (unchanged
< 0 || empty
< 0)
5366 isl_union_map_free(executed
);
5367 return build_ast_from_child(build
, node
, orig
);
5369 isl_union_map_free(orig
);
5371 return build_ast_from_child(build
, node
, executed
);
5373 ctx
= isl_ast_build_get_ctx(build
);
5374 list
= isl_ast_graft_list_alloc(ctx
, 0);
5375 isl_ast_build_free(build
);
5376 isl_schedule_node_free(node
);
5377 isl_union_map_free(executed
);
5380 isl_ast_build_free(build
);
5381 isl_schedule_node_free(node
);
5382 isl_union_map_free(executed
);
5383 isl_union_map_free(orig
);
5387 /* Generate an AST that visits the elements in the domain of "executed"
5388 * in the relative order specified by the guard node "node" and
5391 * The relation "executed" maps the outer generated loop iterators
5392 * to the domain elements executed by those iterations.
5394 * Ensure that the associated guard is enforced by the outer AST
5395 * constructs by adding it to the guard of the graft.
5396 * Since we know that we will enforce the guard, we can also include it
5397 * in the generated constraints used to construct an AST for
5398 * the descendant nodes.
5400 static __isl_give isl_ast_graft_list
*build_ast_from_guard(
5401 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5402 __isl_take isl_union_map
*executed
)
5405 isl_set
*guard
, *hoisted
;
5406 isl_basic_set
*enforced
;
5407 isl_ast_build
*sub_build
;
5408 isl_ast_graft
*graft
;
5409 isl_ast_graft_list
*list
;
5412 space
= isl_ast_build_get_space(build
, 1);
5413 guard
= isl_schedule_node_guard_get_guard(node
);
5414 n1
= isl_space_dim(space
, isl_dim_param
);
5415 guard
= isl_set_align_params(guard
, space
);
5416 n2
= isl_set_dim(guard
, isl_dim_param
);
5418 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
5419 "guard node is not allowed to introduce "
5420 "new parameters", guard
= isl_set_free(guard
));
5421 guard
= isl_set_preimage_multi_aff(guard
,
5422 isl_multi_aff_copy(build
->internal2input
));
5423 guard
= isl_ast_build_specialize(build
, guard
);
5424 guard
= isl_set_gist(guard
, isl_set_copy(build
->generated
));
5426 sub_build
= isl_ast_build_copy(build
);
5427 sub_build
= isl_ast_build_restrict_generated(sub_build
,
5428 isl_set_copy(guard
));
5430 list
= build_ast_from_child(isl_ast_build_copy(sub_build
),
5433 hoisted
= isl_ast_graft_list_extract_hoistable_guard(list
, sub_build
);
5434 if (isl_set_n_basic_set(hoisted
) > 1)
5435 list
= isl_ast_graft_list_gist_guards(list
,
5436 isl_set_copy(hoisted
));
5437 guard
= isl_set_intersect(guard
, hoisted
);
5438 enforced
= extract_shared_enforced(list
, build
);
5439 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
5442 isl_ast_build_free(sub_build
);
5443 isl_ast_build_free(build
);
5444 return isl_ast_graft_list_from_ast_graft(graft
);
5447 /* Call the before_each_mark callback, if requested by the user.
5449 * Return 0 on success and -1 on error.
5451 * The caller is responsible for recording the current inverse schedule
5454 static isl_stat
before_each_mark(__isl_keep isl_id
*mark
,
5455 __isl_keep isl_ast_build
*build
)
5458 return isl_stat_error
;
5459 if (!build
->before_each_mark
)
5461 return build
->before_each_mark(mark
, build
,
5462 build
->before_each_mark_user
);
5465 /* Call the after_each_mark callback, if requested by the user.
5467 * The caller is responsible for recording the current inverse schedule
5470 static __isl_give isl_ast_graft
*after_each_mark(
5471 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
5473 if (!graft
|| !build
)
5474 return isl_ast_graft_free(graft
);
5475 if (!build
->after_each_mark
)
5477 graft
->node
= build
->after_each_mark(graft
->node
, build
,
5478 build
->after_each_mark_user
);
5480 return isl_ast_graft_free(graft
);
5485 /* Generate an AST that visits the elements in the domain of "executed"
5486 * in the relative order specified by the mark node "node" and
5489 * The relation "executed" maps the outer generated loop iterators
5490 * to the domain elements executed by those iterations.
5492 * Since we may be calling before_each_mark and after_each_mark
5493 * callbacks, we record the current inverse schedule in the build.
5495 * We generate an AST for the child of the mark node, combine
5496 * the graft list into a single graft and then insert the mark
5497 * in the AST of that single graft.
5499 static __isl_give isl_ast_graft_list
*build_ast_from_mark(
5500 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5501 __isl_take isl_union_map
*executed
)
5504 isl_ast_graft
*graft
;
5505 isl_ast_graft_list
*list
;
5508 build
= isl_ast_build_set_executed(build
, isl_union_map_copy(executed
));
5510 mark
= isl_schedule_node_mark_get_id(node
);
5511 if (before_each_mark(mark
, build
) < 0)
5512 node
= isl_schedule_node_free(node
);
5514 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5515 list
= isl_ast_graft_list_fuse(list
, build
);
5516 n
= isl_ast_graft_list_n_ast_graft(list
);
5518 list
= isl_ast_graft_list_free(list
);
5522 graft
= isl_ast_graft_list_get_ast_graft(list
, 0);
5523 graft
= isl_ast_graft_insert_mark(graft
, mark
);
5524 graft
= after_each_mark(graft
, build
);
5525 list
= isl_ast_graft_list_set_ast_graft(list
, 0, graft
);
5527 isl_ast_build_free(build
);
5532 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5533 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5534 __isl_take isl_union_map
*executed
);
5536 /* Generate an AST that visits the elements in the domain of "executed"
5537 * in the relative order specified by the sequence (or set) node "node" and
5540 * The relation "executed" maps the outer generated loop iterators
5541 * to the domain elements executed by those iterations.
5543 * We simply generate an AST for each of the children and concatenate
5546 static __isl_give isl_ast_graft_list
*build_ast_from_sequence(
5547 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5548 __isl_take isl_union_map
*executed
)
5552 isl_ast_graft_list
*list
;
5554 ctx
= isl_ast_build_get_ctx(build
);
5555 list
= isl_ast_graft_list_alloc(ctx
, 0);
5557 n
= isl_schedule_node_n_children(node
);
5558 for (i
= 0; i
< n
; ++i
) {
5559 isl_schedule_node
*child
;
5560 isl_ast_graft_list
*list_i
;
5562 child
= isl_schedule_node_get_child(node
, i
);
5563 list_i
= build_ast_from_schedule_node(isl_ast_build_copy(build
),
5564 child
, isl_union_map_copy(executed
));
5565 list
= isl_ast_graft_list_concat(list
, list_i
);
5567 isl_ast_build_free(build
);
5568 isl_schedule_node_free(node
);
5569 isl_union_map_free(executed
);
5574 /* Generate an AST that visits the elements in the domain of "executed"
5575 * in the relative order specified by the node "node" and its descendants.
5577 * The relation "executed" maps the outer generated loop iterators
5578 * to the domain elements executed by those iterations.
5580 * If the node is a leaf, then we pass control to generate_inner_level.
5581 * Note that the current build does not refer to any band node, so
5582 * that generate_inner_level will not try to visit the child of
5585 * The other node types are handled in separate functions.
5586 * Set nodes are currently treated in the same way as sequence nodes.
5587 * The children of a set node may be executed in any order,
5588 * including the order of the children.
5590 static __isl_give isl_ast_graft_list
*build_ast_from_schedule_node(
5591 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5592 __isl_take isl_union_map
*executed
)
5594 enum isl_schedule_node_type type
;
5596 type
= isl_schedule_node_get_type(node
);
5599 case isl_schedule_node_error
:
5601 case isl_schedule_node_leaf
:
5602 isl_schedule_node_free(node
);
5603 return generate_inner_level(executed
, build
);
5604 case isl_schedule_node_band
:
5605 return build_ast_from_band(build
, node
, executed
);
5606 case isl_schedule_node_context
:
5607 return build_ast_from_context(build
, node
, executed
);
5608 case isl_schedule_node_domain
:
5609 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
5610 "unexpected internal domain node", goto error
);
5611 case isl_schedule_node_expansion
:
5612 return build_ast_from_expansion(build
, node
, executed
);
5613 case isl_schedule_node_extension
:
5614 return build_ast_from_extension(build
, node
, executed
);
5615 case isl_schedule_node_filter
:
5616 return build_ast_from_filter(build
, node
, executed
);
5617 case isl_schedule_node_guard
:
5618 return build_ast_from_guard(build
, node
, executed
);
5619 case isl_schedule_node_mark
:
5620 return build_ast_from_mark(build
, node
, executed
);
5621 case isl_schedule_node_sequence
:
5622 case isl_schedule_node_set
:
5623 return build_ast_from_sequence(build
, node
, executed
);
5626 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
5627 "unhandled type", goto error
);
5629 isl_union_map_free(executed
);
5630 isl_schedule_node_free(node
);
5631 isl_ast_build_free(build
);
5636 /* Generate an AST that visits the elements in the domain of "executed"
5637 * in the relative order specified by the (single) child of "node" and
5640 * The relation "executed" maps the outer generated loop iterators
5641 * to the domain elements executed by those iterations.
5643 * This function is never called on a leaf, set or sequence node,
5644 * so the node always has exactly one child.
5646 static __isl_give isl_ast_graft_list
*build_ast_from_child(
5647 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
,
5648 __isl_take isl_union_map
*executed
)
5650 node
= isl_schedule_node_child(node
, 0);
5651 return build_ast_from_schedule_node(build
, node
, executed
);
5654 /* Generate an AST that visits the elements in the domain of the domain
5655 * node "node" in the relative order specified by its descendants.
5657 * An initial inverse schedule is created that maps a zero-dimensional
5658 * schedule space to the node domain.
5659 * The input "build" is assumed to have a parametric domain and
5660 * is replaced by the same zero-dimensional schedule space.
5662 * We also add some of the parameter constraints in the build domain
5663 * to the executed relation. Adding these constraints
5664 * allows for an earlier detection of conflicts in some cases.
5665 * However, we do not want to divide the executed relation into
5666 * more disjuncts than necessary. We therefore approximate
5667 * the constraints on the parameters by a single disjunct set.
5669 static __isl_give isl_ast_node
*build_ast_from_domain(
5670 __isl_take isl_ast_build
*build
, __isl_take isl_schedule_node
*node
)
5673 isl_union_set
*domain
, *schedule_domain
;
5674 isl_union_map
*executed
;
5677 isl_ast_graft_list
*list
;
5684 ctx
= isl_ast_build_get_ctx(build
);
5685 space
= isl_ast_build_get_space(build
, 1);
5686 is_params
= isl_space_is_params(space
);
5687 isl_space_free(space
);
5691 isl_die(ctx
, isl_error_unsupported
,
5692 "expecting parametric initial context", goto error
);
5694 domain
= isl_schedule_node_domain_get_domain(node
);
5695 domain
= isl_union_set_coalesce(domain
);
5697 space
= isl_union_set_get_space(domain
);
5698 space
= isl_space_set_from_params(space
);
5699 build
= isl_ast_build_product(build
, space
);
5701 set
= isl_ast_build_get_domain(build
);
5702 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
5703 schedule_domain
= isl_union_set_from_set(set
);
5705 executed
= isl_union_map_from_domain_and_range(schedule_domain
, domain
);
5706 list
= build_ast_from_child(isl_ast_build_copy(build
), node
, executed
);
5707 ast
= isl_ast_node_from_graft_list(list
, build
);
5708 isl_ast_build_free(build
);
5712 isl_schedule_node_free(node
);
5713 isl_ast_build_free(build
);
5717 /* Generate an AST that visits the elements in the domain of "schedule"
5718 * in the relative order specified by the schedule tree.
5720 * "build" is an isl_ast_build that has been created using
5721 * isl_ast_build_alloc or isl_ast_build_from_context based
5722 * on a parametric set.
5724 * The construction starts at the root node of the schedule,
5725 * which is assumed to be a domain node.
5727 __isl_give isl_ast_node
*isl_ast_build_node_from_schedule(
5728 __isl_keep isl_ast_build
*build
, __isl_take isl_schedule
*schedule
)
5731 isl_schedule_node
*node
;
5733 if (!build
|| !schedule
)
5736 ctx
= isl_ast_build_get_ctx(build
);
5738 node
= isl_schedule_get_root(schedule
);
5741 isl_schedule_free(schedule
);
5743 build
= isl_ast_build_copy(build
);
5744 build
= isl_ast_build_set_single_valued(build
, 0);
5745 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
5746 isl_die(ctx
, isl_error_unsupported
,
5747 "expecting root domain node",
5748 build
= isl_ast_build_free(build
));
5749 return build_ast_from_domain(build
, node
);
5751 isl_schedule_free(schedule
);