isl_schedule_node.c: collect_filter_prefix: allow caller to initialize filter
[isl.git] / isl_ast_codegen.c
bloba2c1a380bc4c1773277eb2a5c86fe4e4b517369e
1 /*
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
13 #include <limits.h>
14 #include <isl/aff.h>
15 #include <isl/set.h>
16 #include <isl/ilp.h>
17 #include <isl/union_map.h>
18 #include <isl/schedule_node.h>
19 #include <isl_sort.h>
20 #include <isl_tarjan.h>
21 #include <isl_ast_private.h>
22 #include <isl_ast_build_expr.h>
23 #include <isl_ast_build_private.h>
24 #include <isl_ast_graft_private.h>
26 /* Data used in generate_domain.
28 * "build" is the input build.
29 * "list" collects the results.
31 struct isl_generate_domain_data {
32 isl_ast_build *build;
34 isl_ast_graft_list *list;
37 static __isl_give isl_ast_graft_list *generate_next_level(
38 __isl_take isl_union_map *executed,
39 __isl_take isl_ast_build *build);
40 static __isl_give isl_ast_graft_list *generate_code(
41 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
42 int internal);
44 /* Generate an AST for a single domain based on
45 * the (non single valued) inverse schedule "executed".
47 * We extend the schedule with the iteration domain
48 * and continue generating through a call to generate_code.
50 * In particular, if executed has the form
52 * S -> D
54 * then we continue generating code on
56 * [S -> D] -> D
58 * The extended inverse schedule is clearly single valued
59 * ensuring that the nested generate_code will not reach this function,
60 * but will instead create calls to all elements of D that need
61 * to be executed from the current schedule domain.
63 static int generate_non_single_valued(__isl_take isl_map *executed,
64 struct isl_generate_domain_data *data)
66 isl_map *identity;
67 isl_ast_build *build;
68 isl_ast_graft_list *list;
70 build = isl_ast_build_copy(data->build);
72 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
73 executed = isl_map_domain_product(executed, identity);
74 build = isl_ast_build_set_single_valued(build, 1);
76 list = generate_code(isl_union_map_from_map(executed), build, 1);
78 data->list = isl_ast_graft_list_concat(data->list, list);
80 return 0;
83 /* Call the at_each_domain callback, if requested by the user,
84 * after recording the current inverse schedule in the build.
86 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
87 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
89 if (!graft || !build)
90 return isl_ast_graft_free(graft);
91 if (!build->at_each_domain)
92 return graft;
94 build = isl_ast_build_copy(build);
95 build = isl_ast_build_set_executed(build,
96 isl_union_map_from_map(isl_map_copy(executed)));
97 if (!build)
98 return isl_ast_graft_free(graft);
100 graft->node = build->at_each_domain(graft->node,
101 build, build->at_each_domain_user);
102 isl_ast_build_free(build);
104 if (!graft->node)
105 graft = isl_ast_graft_free(graft);
107 return graft;
110 /* Generate an AST for a single domain based on
111 * the inverse schedule "executed" and add it to data->list.
113 * If there is more than one domain element associated to the current
114 * schedule "time", then we need to continue the generation process
115 * in generate_non_single_valued.
116 * Note that the inverse schedule being single-valued may depend
117 * on constraints that are only available in the original context
118 * domain specified by the user. We therefore first introduce
119 * some of the constraints of data->build->domain. In particular,
120 * we intersect with a single-disjunct approximation of this set.
121 * We perform this approximation to avoid further splitting up
122 * the executed relation, possibly introducing a disjunctive guard
123 * on the statement.
125 * On the other hand, we only perform the test after having taken the gist
126 * of the domain as the resulting map is the one from which the call
127 * expression is constructed. Using this map to construct the call
128 * expression usually yields simpler results.
129 * Because we perform the single-valuedness test on the gisted map,
130 * we may in rare cases fail to recognize that the inverse schedule
131 * is single-valued. This becomes problematic if this happens
132 * from the recursive call through generate_non_single_valued
133 * as we would then end up in an infinite recursion.
134 * We therefore check if we are inside a call to generate_non_single_valued
135 * and revert to the ungisted map if the gisted map turns out not to be
136 * single-valued.
138 * Otherwise, we generate a call expression for the single executed
139 * domain element and put a guard around it based on the (simplified)
140 * domain of "executed".
142 * At this stage, any pending constraints in the build can no longer
143 * be simplified with respect to any enforced constraints since
144 * the call node does not have any enforced constraints.
145 * We therefore turn all pending constraints into guards
146 * (after simplifying them with respect to the already generated
147 * constraints) and add them to both the generated constraints
148 * and the guard of the constructed graft. This guard will ensure
149 * that the constraints are effectively generated.
151 * If the user has set an at_each_domain callback, it is called
152 * on the constructed call expression node.
154 static int generate_domain(__isl_take isl_map *executed, void *user)
156 struct isl_generate_domain_data *data = user;
157 isl_ast_build *build;
158 isl_ast_graft *graft;
159 isl_ast_graft_list *list;
160 isl_set *guard, *domain;
161 isl_map *map = NULL;
162 int empty, sv;
164 domain = isl_ast_build_get_domain(data->build);
165 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
166 executed = isl_map_intersect_domain(executed, domain);
167 empty = isl_map_is_empty(executed);
168 if (empty < 0)
169 goto error;
170 if (empty) {
171 isl_map_free(executed);
172 return 0;
175 executed = isl_map_coalesce(executed);
176 map = isl_map_copy(executed);
177 map = isl_ast_build_compute_gist_map_domain(data->build, map);
178 sv = isl_map_is_single_valued(map);
179 if (sv < 0)
180 goto error;
181 if (!sv) {
182 isl_map_free(map);
183 if (data->build->single_valued)
184 map = isl_map_copy(executed);
185 else
186 return generate_non_single_valued(executed, data);
188 guard = isl_map_domain(isl_map_copy(map));
189 guard = isl_set_compute_divs(guard);
190 guard = isl_set_intersect(guard,
191 isl_ast_build_get_pending(data->build));
192 guard = isl_set_coalesce(guard);
193 guard = isl_ast_build_specialize(data->build, guard);
194 guard = isl_set_gist(guard, isl_ast_build_get_generated(data->build));
196 build = isl_ast_build_copy(data->build);
197 build = isl_ast_build_replace_pending_by_guard(build,
198 isl_set_copy(guard));
199 graft = isl_ast_graft_alloc_domain(map, build);
200 graft = at_each_domain(graft, executed, build);
201 isl_ast_build_free(build);
202 isl_map_free(executed);
203 graft = isl_ast_graft_add_guard(graft, guard, data->build);
205 list = isl_ast_graft_list_from_ast_graft(graft);
206 data->list = isl_ast_graft_list_concat(data->list, list);
208 return 0;
209 error:
210 isl_map_free(map);
211 isl_map_free(executed);
212 return -1;
215 /* Call build->create_leaf to a create "leaf" node in the AST,
216 * encapsulate the result in an isl_ast_graft and return the result
217 * as a 1-element list.
219 * Note that the node returned by the user may be an entire tree.
221 * Since the node itself cannot enforce any constraints, we turn
222 * all pending constraints into guards and add them to the resulting
223 * graft to ensure that they will be generated.
225 * Before we pass control to the user, we first clear some information
226 * from the build that is (presumbably) only meaningful
227 * for the current code generation.
228 * This includes the create_leaf callback itself, so we make a copy
229 * of the build first.
231 static __isl_give isl_ast_graft_list *call_create_leaf(
232 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
234 isl_set *guard;
235 isl_ast_node *node;
236 isl_ast_graft *graft;
237 isl_ast_build *user_build;
239 guard = isl_ast_build_get_pending(build);
240 user_build = isl_ast_build_copy(build);
241 user_build = isl_ast_build_replace_pending_by_guard(user_build,
242 isl_set_copy(guard));
243 user_build = isl_ast_build_set_executed(user_build, executed);
244 user_build = isl_ast_build_clear_local_info(user_build);
245 if (!user_build)
246 node = NULL;
247 else
248 node = build->create_leaf(user_build, build->create_leaf_user);
249 graft = isl_ast_graft_alloc(node, build);
250 graft = isl_ast_graft_add_guard(graft, guard, build);
251 isl_ast_build_free(build);
252 return isl_ast_graft_list_from_ast_graft(graft);
255 static __isl_give isl_ast_graft_list *build_ast_from_child(
256 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
257 __isl_take isl_union_map *executed);
259 /* Generate an AST after having handled the complete schedule
260 * of this call to the code generator or the complete band
261 * if we are generating an AST from a schedule tree.
263 * If we are inside a band node, then move on to the child of the band.
265 * If the user has specified a create_leaf callback, control
266 * is passed to the user in call_create_leaf.
268 * Otherwise, we generate one or more calls for each individual
269 * domain in generate_domain.
271 static __isl_give isl_ast_graft_list *generate_inner_level(
272 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
274 isl_ctx *ctx;
275 struct isl_generate_domain_data data = { build };
277 if (!build || !executed)
278 goto error;
280 if (isl_ast_build_has_schedule_node(build)) {
281 isl_schedule_node *node;
282 node = isl_ast_build_get_schedule_node(build);
283 build = isl_ast_build_reset_schedule_node(build);
284 return build_ast_from_child(build, node, executed);
287 if (build->create_leaf)
288 return call_create_leaf(executed, build);
290 ctx = isl_union_map_get_ctx(executed);
291 data.list = isl_ast_graft_list_alloc(ctx, 0);
292 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
293 data.list = isl_ast_graft_list_free(data.list);
295 if (0)
296 error: data.list = NULL;
297 isl_ast_build_free(build);
298 isl_union_map_free(executed);
299 return data.list;
302 /* Call the before_each_for callback, if requested by the user.
304 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
305 __isl_keep isl_ast_build *build)
307 isl_id *id;
309 if (!node || !build)
310 return isl_ast_node_free(node);
311 if (!build->before_each_for)
312 return node;
313 id = build->before_each_for(build, build->before_each_for_user);
314 node = isl_ast_node_set_annotation(node, id);
315 return node;
318 /* Call the after_each_for callback, if requested by the user.
320 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
321 __isl_keep isl_ast_build *build)
323 if (!graft || !build)
324 return isl_ast_graft_free(graft);
325 if (!build->after_each_for)
326 return graft;
327 graft->node = build->after_each_for(graft->node, build,
328 build->after_each_for_user);
329 if (!graft->node)
330 return isl_ast_graft_free(graft);
331 return graft;
334 /* Plug in all the know values of the current and outer dimensions
335 * in the domain of "executed". In principle, we only need to plug
336 * in the known value of the current dimension since the values of
337 * outer dimensions have been plugged in already.
338 * However, it turns out to be easier to just plug in all known values.
340 static __isl_give isl_union_map *plug_in_values(
341 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
343 return isl_ast_build_substitute_values_union_map_domain(build,
344 executed);
347 /* Check if the constraint "c" is a lower bound on dimension "pos",
348 * an upper bound, or independent of dimension "pos".
350 static int constraint_type(isl_constraint *c, int pos)
352 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
353 return 1;
354 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
355 return 2;
356 return 0;
359 /* Compare the types of the constraints "a" and "b",
360 * resulting in constraints that are independent of "depth"
361 * to be sorted before the lower bounds on "depth", which in
362 * turn are sorted before the upper bounds on "depth".
364 static int cmp_constraint(__isl_keep isl_constraint *a,
365 __isl_keep isl_constraint *b, void *user)
367 int *depth = user;
368 int t1 = constraint_type(a, *depth);
369 int t2 = constraint_type(b, *depth);
371 return t1 - t2;
374 /* Extract a lower bound on dimension "pos" from constraint "c".
376 * If the constraint is of the form
378 * a x + f(...) >= 0
380 * then we essentially return
382 * l = ceil(-f(...)/a)
384 * However, if the current dimension is strided, then we need to make
385 * sure that the lower bound we construct is of the form
387 * f + s a
389 * with f the offset and s the stride.
390 * We therefore compute
392 * f + s * ceil((l - f)/s)
394 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
395 int pos, __isl_keep isl_ast_build *build)
397 isl_aff *aff;
399 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
400 aff = isl_aff_ceil(aff);
402 if (isl_ast_build_has_stride(build, pos)) {
403 isl_aff *offset;
404 isl_val *stride;
406 offset = isl_ast_build_get_offset(build, pos);
407 stride = isl_ast_build_get_stride(build, pos);
409 aff = isl_aff_sub(aff, isl_aff_copy(offset));
410 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
411 aff = isl_aff_ceil(aff);
412 aff = isl_aff_scale_val(aff, stride);
413 aff = isl_aff_add(aff, offset);
416 aff = isl_ast_build_compute_gist_aff(build, aff);
418 return aff;
421 /* Return the exact lower bound (or upper bound if "upper" is set)
422 * of "domain" as a piecewise affine expression.
424 * If we are computing a lower bound (of a strided dimension), then
425 * we need to make sure it is of the form
427 * f + s a
429 * where f is the offset and s is the stride.
430 * We therefore need to include the stride constraint before computing
431 * the minimum.
433 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
434 __isl_keep isl_ast_build *build, int upper)
436 isl_set *stride;
437 isl_map *it_map;
438 isl_pw_aff *pa;
439 isl_pw_multi_aff *pma;
441 domain = isl_set_copy(domain);
442 if (!upper) {
443 stride = isl_ast_build_get_stride_constraint(build);
444 domain = isl_set_intersect(domain, stride);
446 it_map = isl_ast_build_map_to_iterator(build, domain);
447 if (upper)
448 pma = isl_map_lexmax_pw_multi_aff(it_map);
449 else
450 pma = isl_map_lexmin_pw_multi_aff(it_map);
451 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
452 isl_pw_multi_aff_free(pma);
453 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
454 pa = isl_pw_aff_coalesce(pa);
456 return pa;
459 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
460 * remove_redundant_lower_bounds.
462 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
463 void *user)
465 return isl_pw_aff_plain_cmp(a, b);
468 /* Given a list of lower bounds "list", remove those that are redundant
469 * with respect to the other bounds in "list" and the domain of "build".
471 * We first sort the bounds in the same way as they would be sorted
472 * by set_for_node_expressions so that we can try and remove the last
473 * bounds first.
475 * For a lower bound to be effective, there needs to be at least
476 * one domain element for which it is larger than all other lower bounds.
477 * For each lower bound we therefore intersect the domain with
478 * the conditions that it is larger than all other bounds and
479 * check whether the result is empty. If so, the bound can be removed.
481 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
482 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
484 int i, j, n;
485 isl_set *domain;
487 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
488 if (!list)
489 return NULL;
491 n = isl_pw_aff_list_n_pw_aff(list);
492 if (n <= 1)
493 return list;
495 domain = isl_ast_build_get_domain(build);
497 for (i = n - 1; i >= 0; --i) {
498 isl_pw_aff *pa_i;
499 isl_set *domain_i;
500 int empty;
502 domain_i = isl_set_copy(domain);
503 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
505 for (j = 0; j < n; ++j) {
506 isl_pw_aff *pa_j;
507 isl_set *better;
509 if (j == i)
510 continue;
512 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
513 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
514 domain_i = isl_set_intersect(domain_i, better);
517 empty = isl_set_is_empty(domain_i);
519 isl_set_free(domain_i);
520 isl_pw_aff_free(pa_i);
522 if (empty < 0)
523 goto error;
524 if (!empty)
525 continue;
526 list = isl_pw_aff_list_drop(list, i, 1);
527 n--;
530 isl_set_free(domain);
532 return list;
533 error:
534 isl_set_free(domain);
535 return isl_pw_aff_list_free(list);
538 /* Extract a lower bound on dimension "pos" from each constraint
539 * in "constraints" and return the list of lower bounds.
540 * If "constraints" has zero elements, then we extract a lower bound
541 * from "domain" instead.
543 * If the current dimension is strided, then the lower bound
544 * is adjusted by lower_bound to match the stride information.
545 * This modification may make one or more lower bounds redundant
546 * with respect to the other lower bounds. We therefore check
547 * for this condition and remove the redundant lower bounds.
549 static __isl_give isl_pw_aff_list *lower_bounds(
550 __isl_keep isl_constraint_list *constraints, int pos,
551 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
553 isl_ctx *ctx;
554 isl_pw_aff_list *list;
555 int i, n;
557 if (!build)
558 return NULL;
560 n = isl_constraint_list_n_constraint(constraints);
561 if (n == 0) {
562 isl_pw_aff *pa;
563 pa = exact_bound(domain, build, 0);
564 return isl_pw_aff_list_from_pw_aff(pa);
567 ctx = isl_ast_build_get_ctx(build);
568 list = isl_pw_aff_list_alloc(ctx,n);
570 for (i = 0; i < n; ++i) {
571 isl_aff *aff;
572 isl_constraint *c;
574 c = isl_constraint_list_get_constraint(constraints, i);
575 aff = lower_bound(c, pos, build);
576 isl_constraint_free(c);
577 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
580 if (isl_ast_build_has_stride(build, pos))
581 list = remove_redundant_lower_bounds(list, build);
583 return list;
586 /* Extract an upper bound on dimension "pos" from each constraint
587 * in "constraints" and return the list of upper bounds.
588 * If "constraints" has zero elements, then we extract an upper bound
589 * from "domain" instead.
591 static __isl_give isl_pw_aff_list *upper_bounds(
592 __isl_keep isl_constraint_list *constraints, int pos,
593 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
595 isl_ctx *ctx;
596 isl_pw_aff_list *list;
597 int i, n;
599 n = isl_constraint_list_n_constraint(constraints);
600 if (n == 0) {
601 isl_pw_aff *pa;
602 pa = exact_bound(domain, build, 1);
603 return isl_pw_aff_list_from_pw_aff(pa);
606 ctx = isl_ast_build_get_ctx(build);
607 list = isl_pw_aff_list_alloc(ctx,n);
609 for (i = 0; i < n; ++i) {
610 isl_aff *aff;
611 isl_constraint *c;
613 c = isl_constraint_list_get_constraint(constraints, i);
614 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
615 isl_constraint_free(c);
616 aff = isl_aff_floor(aff);
617 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
620 return list;
623 /* Return an isl_ast_expr that performs the reduction of type "type"
624 * on AST expressions corresponding to the elements in "list".
626 * The list is assumed to contain at least one element.
627 * If the list contains exactly one element, then the returned isl_ast_expr
628 * simply computes that affine expression.
629 * If the list contains more than one element, then we sort it
630 * using a fairly abitrary but hopefully reasonably stable order.
632 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
633 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
635 int i, n;
636 isl_ctx *ctx;
637 isl_ast_expr *expr;
639 if (!list)
640 return NULL;
642 n = isl_pw_aff_list_n_pw_aff(list);
644 if (n == 1)
645 return isl_ast_build_expr_from_pw_aff_internal(build,
646 isl_pw_aff_list_get_pw_aff(list, 0));
648 ctx = isl_pw_aff_list_get_ctx(list);
649 expr = isl_ast_expr_alloc_op(ctx, type, n);
650 if (!expr)
651 return NULL;
653 list = isl_pw_aff_list_copy(list);
654 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
655 if (!list)
656 return isl_ast_expr_free(expr);
658 for (i = 0; i < n; ++i) {
659 isl_ast_expr *expr_i;
661 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
662 isl_pw_aff_list_get_pw_aff(list, i));
663 if (!expr_i)
664 goto error;
665 expr->u.op.args[i] = expr_i;
668 isl_pw_aff_list_free(list);
669 return expr;
670 error:
671 isl_pw_aff_list_free(list);
672 isl_ast_expr_free(expr);
673 return NULL;
676 /* Add guards implied by the "generated constraints",
677 * but not (necessarily) enforced by the generated AST to "guard".
678 * In particular, if there is any stride constraints,
679 * then add the guard implied by those constraints.
680 * If we have generated a degenerate loop, then add the guard
681 * implied by "bounds" on the outer dimensions, i.e., the guard
682 * that ensures that the single value actually exists.
683 * Since there may also be guards implied by a combination
684 * of these constraints, we first combine them before
685 * deriving the implied constraints.
687 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
688 int degenerate, __isl_keep isl_basic_set *bounds,
689 __isl_keep isl_ast_build *build)
691 int depth, has_stride;
692 isl_space *space;
693 isl_set *dom, *set;
695 depth = isl_ast_build_get_depth(build);
696 has_stride = isl_ast_build_has_stride(build, depth);
697 if (!has_stride && !degenerate)
698 return guard;
700 space = isl_basic_set_get_space(bounds);
701 dom = isl_set_universe(space);
703 if (degenerate) {
704 bounds = isl_basic_set_copy(bounds);
705 bounds = isl_basic_set_drop_constraints_not_involving_dims(
706 bounds, isl_dim_set, depth, 1);
707 set = isl_set_from_basic_set(bounds);
708 dom = isl_set_intersect(dom, set);
711 if (has_stride) {
712 set = isl_ast_build_get_stride_constraint(build);
713 dom = isl_set_intersect(dom, set);
716 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
717 dom = isl_ast_build_compute_gist(build, dom);
718 guard = isl_set_intersect(guard, dom);
720 return guard;
723 /* Update "graft" based on "sub_build" for the degenerate case.
725 * "build" is the build in which graft->node was created
726 * "sub_build" contains information about the current level itself,
727 * including the single value attained.
729 * We set the initialization part of the for loop to the single
730 * value attained by the current dimension.
731 * The increment and condition are not strictly needed as the are known
732 * to be "1" and "iterator <= value" respectively.
734 static __isl_give isl_ast_graft *refine_degenerate(
735 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
736 __isl_keep isl_ast_build *sub_build)
738 isl_pw_aff *value;
740 if (!graft || !sub_build)
741 return isl_ast_graft_free(graft);
743 value = isl_pw_aff_copy(sub_build->value);
745 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
746 value);
747 if (!graft->node->u.f.init)
748 return isl_ast_graft_free(graft);
750 return graft;
753 /* Return the intersection of constraints in "list" as a set.
755 static __isl_give isl_set *intersect_constraints(
756 __isl_keep isl_constraint_list *list)
758 int i, n;
759 isl_basic_set *bset;
761 n = isl_constraint_list_n_constraint(list);
762 if (n < 1)
763 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
764 "expecting at least one constraint", return NULL);
766 bset = isl_basic_set_from_constraint(
767 isl_constraint_list_get_constraint(list, 0));
768 for (i = 1; i < n; ++i) {
769 isl_basic_set *bset_i;
771 bset_i = isl_basic_set_from_constraint(
772 isl_constraint_list_get_constraint(list, i));
773 bset = isl_basic_set_intersect(bset, bset_i);
776 return isl_set_from_basic_set(bset);
779 /* Compute the constraints on the outer dimensions enforced by
780 * graft->node and add those constraints to graft->enforced,
781 * in case the upper bound is expressed as a set "upper".
783 * In particular, if l(...) is a lower bound in "lower", and
785 * -a i + f(...) >= 0 or a i <= f(...)
787 * is an upper bound ocnstraint on the current dimension i,
788 * then the for loop enforces the constraint
790 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
792 * We therefore simply take each lower bound in turn, plug it into
793 * the upper bounds and compute the intersection over all lower bounds.
795 * If a lower bound is a rational expression, then
796 * isl_basic_set_preimage_multi_aff will force this rational
797 * expression to have only integer values. However, the loop
798 * itself does not enforce this integrality constraint. We therefore
799 * use the ceil of the lower bounds instead of the lower bounds themselves.
800 * Other constraints will make sure that the for loop is only executed
801 * when each of the lower bounds attains an integral value.
802 * In particular, potentially rational values only occur in
803 * lower_bound if the offset is a (seemingly) rational expression,
804 * but then outer conditions will make sure that this rational expression
805 * only attains integer values.
807 static __isl_give isl_ast_graft *set_enforced_from_set(
808 __isl_take isl_ast_graft *graft,
809 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
811 isl_space *space;
812 isl_basic_set *enforced;
813 isl_pw_multi_aff *pma;
814 int i, n;
816 if (!graft || !lower)
817 return isl_ast_graft_free(graft);
819 space = isl_set_get_space(upper);
820 enforced = isl_basic_set_universe(isl_space_copy(space));
822 space = isl_space_map_from_set(space);
823 pma = isl_pw_multi_aff_identity(space);
825 n = isl_pw_aff_list_n_pw_aff(lower);
826 for (i = 0; i < n; ++i) {
827 isl_pw_aff *pa;
828 isl_set *enforced_i;
829 isl_basic_set *hull;
830 isl_pw_multi_aff *pma_i;
832 pa = isl_pw_aff_list_get_pw_aff(lower, i);
833 pa = isl_pw_aff_ceil(pa);
834 pma_i = isl_pw_multi_aff_copy(pma);
835 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
836 enforced_i = isl_set_copy(upper);
837 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
838 hull = isl_set_simple_hull(enforced_i);
839 enforced = isl_basic_set_intersect(enforced, hull);
842 isl_pw_multi_aff_free(pma);
844 graft = isl_ast_graft_enforce(graft, enforced);
846 return graft;
849 /* Compute the constraints on the outer dimensions enforced by
850 * graft->node and add those constraints to graft->enforced,
851 * in case the upper bound is expressed as
852 * a list of affine expressions "upper".
854 * The enforced condition is that each lower bound expression is less
855 * than or equal to each upper bound expression.
857 static __isl_give isl_ast_graft *set_enforced_from_list(
858 __isl_take isl_ast_graft *graft,
859 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
861 isl_set *cond;
862 isl_basic_set *enforced;
864 lower = isl_pw_aff_list_copy(lower);
865 upper = isl_pw_aff_list_copy(upper);
866 cond = isl_pw_aff_list_le_set(lower, upper);
867 enforced = isl_set_simple_hull(cond);
868 graft = isl_ast_graft_enforce(graft, enforced);
870 return graft;
873 /* Does "aff" have a negative constant term?
875 static int aff_constant_is_negative(__isl_take isl_set *set,
876 __isl_take isl_aff *aff, void *user)
878 int *neg = user;
879 isl_val *v;
881 v = isl_aff_get_constant_val(aff);
882 *neg = isl_val_is_neg(v);
883 isl_val_free(v);
884 isl_set_free(set);
885 isl_aff_free(aff);
887 return *neg ? 0 : -1;
890 /* Does "pa" have a negative constant term over its entire domain?
892 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
894 int r;
895 int *neg = user;
897 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
898 isl_pw_aff_free(pa);
900 return (*neg && r >= 0) ? 0 : -1;
903 /* Does each element in "list" have a negative constant term?
905 * The callback terminates the iteration as soon an element has been
906 * found that does not have a negative constant term.
908 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
910 int neg = 1;
912 if (isl_pw_aff_list_foreach(list,
913 &pw_aff_constant_is_negative, &neg) < 0 && neg)
914 return -1;
916 return neg;
919 /* Add 1 to each of the elements in "list", where each of these elements
920 * is defined over the internal schedule space of "build".
922 static __isl_give isl_pw_aff_list *list_add_one(
923 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
925 int i, n;
926 isl_space *space;
927 isl_aff *aff;
928 isl_pw_aff *one;
930 space = isl_ast_build_get_space(build, 1);
931 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
932 aff = isl_aff_add_constant_si(aff, 1);
933 one = isl_pw_aff_from_aff(aff);
935 n = isl_pw_aff_list_n_pw_aff(list);
936 for (i = 0; i < n; ++i) {
937 isl_pw_aff *pa;
938 pa = isl_pw_aff_list_get_pw_aff(list, i);
939 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
940 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
943 isl_pw_aff_free(one);
945 return list;
948 /* Set the condition part of the for node graft->node in case
949 * the upper bound is represented as a list of piecewise affine expressions.
951 * In particular, set the condition to
953 * iterator <= min(list of upper bounds)
955 * If each of the upper bounds has a negative constant term, then
956 * set the condition to
958 * iterator < min(list of (upper bound + 1)s)
961 static __isl_give isl_ast_graft *set_for_cond_from_list(
962 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
963 __isl_keep isl_ast_build *build)
965 int neg;
966 isl_ast_expr *bound, *iterator, *cond;
967 enum isl_ast_op_type type = isl_ast_op_le;
969 if (!graft || !list)
970 return isl_ast_graft_free(graft);
972 neg = list_constant_is_negative(list);
973 if (neg < 0)
974 return isl_ast_graft_free(graft);
975 list = isl_pw_aff_list_copy(list);
976 if (neg) {
977 list = list_add_one(list, build);
978 type = isl_ast_op_lt;
981 bound = reduce_list(isl_ast_op_min, list, build);
982 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
983 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
984 graft->node->u.f.cond = cond;
986 isl_pw_aff_list_free(list);
987 if (!graft->node->u.f.cond)
988 return isl_ast_graft_free(graft);
989 return graft;
992 /* Set the condition part of the for node graft->node in case
993 * the upper bound is represented as a set.
995 static __isl_give isl_ast_graft *set_for_cond_from_set(
996 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
997 __isl_keep isl_ast_build *build)
999 isl_ast_expr *cond;
1001 if (!graft)
1002 return NULL;
1004 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1005 graft->node->u.f.cond = cond;
1006 if (!graft->node->u.f.cond)
1007 return isl_ast_graft_free(graft);
1008 return graft;
1011 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1012 * the current dimension.
1014 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1016 int depth;
1017 isl_val *v;
1018 isl_ctx *ctx;
1020 if (!build)
1021 return NULL;
1022 ctx = isl_ast_build_get_ctx(build);
1023 depth = isl_ast_build_get_depth(build);
1025 if (!isl_ast_build_has_stride(build, depth))
1026 return isl_ast_expr_alloc_int_si(ctx, 1);
1028 v = isl_ast_build_get_stride(build, depth);
1029 return isl_ast_expr_from_val(v);
1032 /* Should we express the loop condition as
1034 * iterator <= min(list of upper bounds)
1036 * or as a conjunction of constraints?
1038 * The first is constructed from a list of upper bounds.
1039 * The second is constructed from a set.
1041 * If there are no upper bounds in "constraints", then this could mean
1042 * that "domain" simply doesn't have an upper bound or that we didn't
1043 * pick any upper bound. In the first case, we want to generate the
1044 * loop condition as a(n empty) conjunction of constraints
1045 * In the second case, we will compute
1046 * a single upper bound from "domain" and so we use the list form.
1048 * If there are upper bounds in "constraints",
1049 * then we use the list form iff the atomic_upper_bound option is set.
1051 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1052 __isl_keep isl_set *domain, int depth)
1054 if (n_upper > 0)
1055 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1056 else
1057 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1060 /* Fill in the expressions of the for node in graft->node.
1062 * In particular,
1063 * - set the initialization part of the loop to the maximum of the lower bounds
1064 * - extract the increment from the stride of the current dimension
1065 * - construct the for condition either based on a list of upper bounds
1066 * or on a set of upper bound constraints.
1068 static __isl_give isl_ast_graft *set_for_node_expressions(
1069 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1070 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1071 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1073 isl_ast_node *node;
1075 if (!graft)
1076 return NULL;
1078 build = isl_ast_build_copy(build);
1080 node = graft->node;
1081 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1082 node->u.f.inc = for_inc(build);
1084 if (use_list)
1085 graft = set_for_cond_from_list(graft, upper_list, build);
1086 else
1087 graft = set_for_cond_from_set(graft, upper_set, build);
1089 isl_ast_build_free(build);
1091 if (!node->u.f.iterator || !node->u.f.init ||
1092 !node->u.f.cond || !node->u.f.inc)
1093 return isl_ast_graft_free(graft);
1095 return graft;
1098 /* Update "graft" based on "bounds" and "domain" for the generic,
1099 * non-degenerate, case.
1101 * "c_lower" and "c_upper" contain the lower and upper bounds
1102 * that the loop node should express.
1103 * "domain" is the subset of the intersection of the constraints
1104 * for which some code is executed.
1106 * There may be zero lower bounds or zero upper bounds in "constraints"
1107 * in case the list of constraints was created
1108 * based on the atomic option or based on separation with explicit bounds.
1109 * In that case, we use "domain" to derive lower and/or upper bounds.
1111 * We first compute a list of one or more lower bounds.
1113 * Then we decide if we want to express the condition as
1115 * iterator <= min(list of upper bounds)
1117 * or as a conjunction of constraints.
1119 * The set of enforced constraints is then computed either based on
1120 * a list of upper bounds or on a set of upper bound constraints.
1121 * We do not compute any enforced constraints if we were forced
1122 * to compute a lower or upper bound using exact_bound. The domains
1123 * of the resulting expressions may imply some bounds on outer dimensions
1124 * that we do not want to appear in the enforced constraints since
1125 * they are not actually enforced by the corresponding code.
1127 * Finally, we fill in the expressions of the for node.
1129 static __isl_give isl_ast_graft *refine_generic_bounds(
1130 __isl_take isl_ast_graft *graft,
1131 __isl_take isl_constraint_list *c_lower,
1132 __isl_take isl_constraint_list *c_upper,
1133 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1135 int depth;
1136 isl_ctx *ctx;
1137 isl_pw_aff_list *lower;
1138 int use_list;
1139 isl_set *upper_set = NULL;
1140 isl_pw_aff_list *upper_list = NULL;
1141 int n_lower, n_upper;
1143 if (!graft || !c_lower || !c_upper || !build)
1144 goto error;
1146 depth = isl_ast_build_get_depth(build);
1147 ctx = isl_ast_graft_get_ctx(graft);
1149 n_lower = isl_constraint_list_n_constraint(c_lower);
1150 n_upper = isl_constraint_list_n_constraint(c_upper);
1152 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1154 lower = lower_bounds(c_lower, depth, domain, build);
1156 if (use_list)
1157 upper_list = upper_bounds(c_upper, depth, domain, build);
1158 else if (n_upper > 0)
1159 upper_set = intersect_constraints(c_upper);
1160 else
1161 upper_set = isl_set_universe(isl_set_get_space(domain));
1163 if (n_lower == 0 || n_upper == 0)
1165 else if (use_list)
1166 graft = set_enforced_from_list(graft, lower, upper_list);
1167 else
1168 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1170 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1171 upper_set, build);
1173 isl_pw_aff_list_free(lower);
1174 isl_pw_aff_list_free(upper_list);
1175 isl_set_free(upper_set);
1176 isl_constraint_list_free(c_lower);
1177 isl_constraint_list_free(c_upper);
1179 return graft;
1180 error:
1181 isl_constraint_list_free(c_lower);
1182 isl_constraint_list_free(c_upper);
1183 return isl_ast_graft_free(graft);
1186 /* Internal data structure used inside count_constraints to keep
1187 * track of the number of constraints that are independent of dimension "pos",
1188 * the lower bounds in "pos" and the upper bounds in "pos".
1190 struct isl_ast_count_constraints_data {
1191 int pos;
1193 int n_indep;
1194 int n_lower;
1195 int n_upper;
1198 /* Increment data->n_indep, data->lower or data->upper depending
1199 * on whether "c" is independenct of dimensions data->pos,
1200 * a lower bound or an upper bound.
1202 static int count_constraints(__isl_take isl_constraint *c, void *user)
1204 struct isl_ast_count_constraints_data *data = user;
1206 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1207 data->n_lower++;
1208 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1209 data->n_upper++;
1210 else
1211 data->n_indep++;
1213 isl_constraint_free(c);
1215 return 0;
1218 /* Update "graft" based on "bounds" and "domain" for the generic,
1219 * non-degenerate, case.
1221 * "list" respresent the list of bounds that need to be encoded by
1222 * the for loop. Only the constraints that involve the iterator
1223 * are relevant here. The other constraints are taken care of by
1224 * the caller and are included in the generated constraints of "build".
1225 * "domain" is the subset of the intersection of the constraints
1226 * for which some code is executed.
1227 * "build" is the build in which graft->node was created.
1229 * We separate lower bounds, upper bounds and constraints that
1230 * are independent of the loop iterator.
1232 * The actual for loop bounds are generated in refine_generic_bounds.
1234 static __isl_give isl_ast_graft *refine_generic_split(
1235 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1236 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1238 struct isl_ast_count_constraints_data data;
1239 isl_constraint_list *lower;
1240 isl_constraint_list *upper;
1242 if (!list)
1243 return isl_ast_graft_free(graft);
1245 data.pos = isl_ast_build_get_depth(build);
1247 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1248 if (!list)
1249 return isl_ast_graft_free(graft);
1251 data.n_indep = data.n_lower = data.n_upper = 0;
1252 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1253 isl_constraint_list_free(list);
1254 return isl_ast_graft_free(graft);
1257 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1258 upper = isl_constraint_list_copy(lower);
1259 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1260 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1262 return refine_generic_bounds(graft, lower, upper, domain, build);
1265 /* Update "graft" based on "bounds" and "domain" for the generic,
1266 * non-degenerate, case.
1268 * "bounds" respresent the bounds that need to be encoded by
1269 * the for loop (or a guard around the for loop).
1270 * "domain" is the subset of "bounds" for which some code is executed.
1271 * "build" is the build in which graft->node was created.
1273 * We break up "bounds" into a list of constraints and continue with
1274 * refine_generic_split.
1276 static __isl_give isl_ast_graft *refine_generic(
1277 __isl_take isl_ast_graft *graft,
1278 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1279 __isl_keep isl_ast_build *build)
1281 isl_constraint_list *list;
1283 if (!build || !graft)
1284 return isl_ast_graft_free(graft);
1286 list = isl_basic_set_get_constraint_list(bounds);
1288 graft = refine_generic_split(graft, list, domain, build);
1290 return graft;
1293 /* Create a for node for the current level.
1295 * Mark the for node degenerate if "degenerate" is set.
1297 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1298 int degenerate)
1300 int depth;
1301 isl_id *id;
1302 isl_ast_node *node;
1304 if (!build)
1305 return NULL;
1307 depth = isl_ast_build_get_depth(build);
1308 id = isl_ast_build_get_iterator_id(build, depth);
1309 node = isl_ast_node_alloc_for(id);
1310 if (degenerate)
1311 node = isl_ast_node_for_mark_degenerate(node);
1313 return node;
1316 /* If the ast_build_exploit_nested_bounds option is set, then return
1317 * the constraints enforced by all elements in "list".
1318 * Otherwise, return the universe.
1320 static __isl_give isl_basic_set *extract_shared_enforced(
1321 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1323 isl_ctx *ctx;
1324 isl_space *space;
1326 if (!list)
1327 return NULL;
1329 ctx = isl_ast_graft_list_get_ctx(list);
1330 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1331 return isl_ast_graft_list_extract_shared_enforced(list, build);
1333 space = isl_ast_build_get_space(build, 1);
1334 return isl_basic_set_universe(space);
1337 /* Return the pending constraints of "build" that are not already taken
1338 * care of (by a combination of "enforced" and the generated constraints
1339 * of "build").
1341 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1342 __isl_keep isl_basic_set *enforced)
1344 isl_set *guard, *context;
1346 guard = isl_ast_build_get_pending(build);
1347 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1348 context = isl_set_intersect(context,
1349 isl_ast_build_get_generated(build));
1350 return isl_set_gist(guard, context);
1353 /* Create an AST node for the current dimension based on
1354 * the schedule domain "bounds" and return the node encapsulated
1355 * in an isl_ast_graft.
1357 * "executed" is the current inverse schedule, taking into account
1358 * the bounds in "bounds"
1359 * "domain" is the domain of "executed", with inner dimensions projected out.
1360 * It may be a strict subset of "bounds" in case "bounds" was created
1361 * based on the atomic option or based on separation with explicit bounds.
1363 * "domain" may satisfy additional equalities that result
1364 * from intersecting "executed" with "bounds" in add_node.
1365 * It may also satisfy some global constraints that were dropped out because
1366 * we performed separation with explicit bounds.
1367 * The very first step is then to copy these constraints to "bounds".
1369 * Since we may be calling before_each_for and after_each_for
1370 * callbacks, we record the current inverse schedule in the build.
1372 * We consider three builds,
1373 * "build" is the one in which the current level is created,
1374 * "body_build" is the build in which the next level is created,
1375 * "sub_build" is essentially the same as "body_build", except that
1376 * the depth has not been increased yet.
1378 * "build" already contains information (in strides and offsets)
1379 * about the strides at the current level, but this information is not
1380 * reflected in the build->domain.
1381 * We first add this information and the "bounds" to the sub_build->domain.
1382 * isl_ast_build_set_loop_bounds adds the stride information and
1383 * checks whether the current dimension attains
1384 * only a single value and whether this single value can be represented using
1385 * a single affine expression.
1386 * In the first case, the current level is considered "degenerate".
1387 * In the second, sub-case, the current level is considered "eliminated".
1388 * Eliminated levels don't need to be reflected in the AST since we can
1389 * simply plug in the affine expression. For degenerate, but non-eliminated,
1390 * levels, we do introduce a for node, but mark is as degenerate so that
1391 * it can be printed as an assignment of the single value to the loop
1392 * "iterator".
1394 * If the current level is eliminated, we explicitly plug in the value
1395 * for the current level found by isl_ast_build_set_loop_bounds in the
1396 * inverse schedule. This ensures that if we are working on a slice
1397 * of the domain based on information available in the inverse schedule
1398 * and the build domain, that then this information is also reflected
1399 * in the inverse schedule. This operation also eliminates the current
1400 * dimension from the inverse schedule making sure no inner dimensions depend
1401 * on the current dimension. Otherwise, we create a for node, marking
1402 * it degenerate if appropriate. The initial for node is still incomplete
1403 * and will be completed in either refine_degenerate or refine_generic.
1405 * We then generate a sequence of grafts for the next level,
1406 * create a surrounding graft for the current level and insert
1407 * the for node we created (if the current level is not eliminated).
1408 * Before creating a graft for the current level, we first extract
1409 * hoistable constraints from the child guards and combine them
1410 * with the pending constraints in the build. These constraints
1411 * are used to simplify the child guards and then added to the guard
1412 * of the current graft to ensure that they will be generated.
1413 * If the hoisted guard is a disjunction, then we use it directly
1414 * to gist the guards on the children before intersect it with the
1415 * pending constraints. We do so because this disjunction is typically
1416 * identical to the guards on the children such that these guards
1417 * can be effectively removed completely. After the intersection,
1418 * the gist operation would have a harder time figuring this out.
1420 * Finally, we set the bounds of the for loop in either
1421 * refine_degenerate or refine_generic.
1422 * We do so in a context where the pending constraints of the build
1423 * have been replaced by the guard of the current graft.
1425 static __isl_give isl_ast_graft *create_node_scaled(
1426 __isl_take isl_union_map *executed,
1427 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1428 __isl_take isl_ast_build *build)
1430 int depth;
1431 int degenerate, eliminated;
1432 isl_basic_set *hull;
1433 isl_basic_set *enforced;
1434 isl_set *guard, *hoisted;
1435 isl_ast_node *node = NULL;
1436 isl_ast_graft *graft;
1437 isl_ast_graft_list *children;
1438 isl_ast_build *sub_build;
1439 isl_ast_build *body_build;
1441 domain = isl_ast_build_eliminate_divs(build, domain);
1442 domain = isl_set_detect_equalities(domain);
1443 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1444 bounds = isl_basic_set_intersect(bounds, hull);
1445 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1447 depth = isl_ast_build_get_depth(build);
1448 sub_build = isl_ast_build_copy(build);
1449 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1450 isl_basic_set_copy(bounds));
1451 degenerate = isl_ast_build_has_value(sub_build);
1452 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1453 if (degenerate < 0 || eliminated < 0)
1454 executed = isl_union_map_free(executed);
1455 if (eliminated)
1456 executed = plug_in_values(executed, sub_build);
1457 else
1458 node = create_for(build, degenerate);
1460 body_build = isl_ast_build_copy(sub_build);
1461 body_build = isl_ast_build_increase_depth(body_build);
1462 if (!eliminated)
1463 node = before_each_for(node, body_build);
1464 children = generate_next_level(executed,
1465 isl_ast_build_copy(body_build));
1467 enforced = extract_shared_enforced(children, build);
1468 guard = extract_pending(sub_build, enforced);
1469 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1470 if (isl_set_n_basic_set(hoisted) > 1)
1471 children = isl_ast_graft_list_gist_guards(children,
1472 isl_set_copy(hoisted));
1473 guard = isl_set_intersect(guard, hoisted);
1474 if (!eliminated)
1475 guard = add_implied_guards(guard, degenerate, bounds, build);
1477 graft = isl_ast_graft_alloc_from_children(children,
1478 isl_set_copy(guard), enforced, build, sub_build);
1480 if (!degenerate)
1481 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1482 if (!eliminated) {
1483 isl_ast_build *for_build;
1485 graft = isl_ast_graft_insert_for(graft, node);
1486 for_build = isl_ast_build_copy(build);
1487 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1488 isl_set_copy(guard));
1489 if (degenerate)
1490 graft = refine_degenerate(graft, for_build, sub_build);
1491 else
1492 graft = refine_generic(graft, bounds,
1493 domain, for_build);
1494 isl_ast_build_free(for_build);
1496 isl_set_free(guard);
1497 if (!eliminated)
1498 graft = after_each_for(graft, body_build);
1500 isl_ast_build_free(body_build);
1501 isl_ast_build_free(sub_build);
1502 isl_ast_build_free(build);
1503 isl_basic_set_free(bounds);
1504 isl_set_free(domain);
1506 return graft;
1509 /* Internal data structure for checking if all constraints involving
1510 * the input dimension "depth" are such that the other coefficients
1511 * are multiples of "m", reducing "m" if they are not.
1512 * If "m" is reduced all the way down to "1", then the check has failed
1513 * and we break out of the iteration.
1515 struct isl_check_scaled_data {
1516 int depth;
1517 isl_val *m;
1520 /* If constraint "c" involves the input dimension data->depth,
1521 * then make sure that all the other coefficients are multiples of data->m,
1522 * reducing data->m if needed.
1523 * Break out of the iteration if data->m has become equal to "1".
1525 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1527 struct isl_check_scaled_data *data = user;
1528 int i, j, n;
1529 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1530 isl_dim_div };
1532 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1533 isl_constraint_free(c);
1534 return 0;
1537 for (i = 0; i < 4; ++i) {
1538 n = isl_constraint_dim(c, t[i]);
1539 for (j = 0; j < n; ++j) {
1540 isl_val *d;
1542 if (t[i] == isl_dim_in && j == data->depth)
1543 continue;
1544 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1545 continue;
1546 d = isl_constraint_get_coefficient_val(c, t[i], j);
1547 data->m = isl_val_gcd(data->m, d);
1548 if (isl_val_is_one(data->m))
1549 break;
1551 if (j < n)
1552 break;
1555 isl_constraint_free(c);
1557 return i < 4 ? -1 : 0;
1560 /* For each constraint of "bmap" that involves the input dimension data->depth,
1561 * make sure that all the other coefficients are multiples of data->m,
1562 * reducing data->m if needed.
1563 * Break out of the iteration if data->m has become equal to "1".
1565 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1567 int r;
1569 r = isl_basic_map_foreach_constraint(bmap,
1570 &constraint_check_scaled, user);
1571 isl_basic_map_free(bmap);
1573 return r;
1576 /* For each constraint of "map" that involves the input dimension data->depth,
1577 * make sure that all the other coefficients are multiples of data->m,
1578 * reducing data->m if needed.
1579 * Break out of the iteration if data->m has become equal to "1".
1581 static int map_check_scaled(__isl_take isl_map *map, void *user)
1583 int r;
1585 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1586 isl_map_free(map);
1588 return r;
1591 /* Create an AST node for the current dimension based on
1592 * the schedule domain "bounds" and return the node encapsulated
1593 * in an isl_ast_graft.
1595 * "executed" is the current inverse schedule, taking into account
1596 * the bounds in "bounds"
1597 * "domain" is the domain of "executed", with inner dimensions projected out.
1600 * Before moving on to the actual AST node construction in create_node_scaled,
1601 * we first check if the current dimension is strided and if we can scale
1602 * down this stride. Note that we only do this if the ast_build_scale_strides
1603 * option is set.
1605 * In particular, let the current dimension take on values
1607 * f + s a
1609 * with a an integer. We check if we can find an integer m that (obviously)
1610 * divides both f and s.
1612 * If so, we check if the current dimension only appears in constraints
1613 * where the coefficients of the other variables are multiples of m.
1614 * We perform this extra check to avoid the risk of introducing
1615 * divisions by scaling down the current dimension.
1617 * If so, we scale the current dimension down by a factor of m.
1618 * That is, we plug in
1620 * i = m i' (1)
1622 * Note that in principle we could always scale down strided loops
1623 * by plugging in
1625 * i = f + s i'
1627 * but this may result in i' taking on larger values than the original i,
1628 * due to the shift by "f".
1629 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1631 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1632 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1633 __isl_take isl_ast_build *build)
1635 struct isl_check_scaled_data data;
1636 isl_ctx *ctx;
1637 isl_aff *offset;
1638 isl_val *d;
1640 ctx = isl_ast_build_get_ctx(build);
1641 if (!isl_options_get_ast_build_scale_strides(ctx))
1642 return create_node_scaled(executed, bounds, domain, build);
1644 data.depth = isl_ast_build_get_depth(build);
1645 if (!isl_ast_build_has_stride(build, data.depth))
1646 return create_node_scaled(executed, bounds, domain, build);
1648 offset = isl_ast_build_get_offset(build, data.depth);
1649 data.m = isl_ast_build_get_stride(build, data.depth);
1650 if (!data.m)
1651 offset = isl_aff_free(offset);
1652 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1653 d = isl_aff_get_denominator_val(offset);
1654 if (!d)
1655 executed = isl_union_map_free(executed);
1657 if (executed && isl_val_is_divisible_by(data.m, d))
1658 data.m = isl_val_div(data.m, d);
1659 else {
1660 data.m = isl_val_set_si(data.m, 1);
1661 isl_val_free(d);
1664 if (!isl_val_is_one(data.m)) {
1665 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1666 &data) < 0 &&
1667 !isl_val_is_one(data.m))
1668 executed = isl_union_map_free(executed);
1671 if (!isl_val_is_one(data.m)) {
1672 isl_space *space;
1673 isl_multi_aff *ma;
1674 isl_aff *aff;
1675 isl_map *map;
1676 isl_union_map *umap;
1678 space = isl_ast_build_get_space(build, 1);
1679 space = isl_space_map_from_set(space);
1680 ma = isl_multi_aff_identity(space);
1681 aff = isl_multi_aff_get_aff(ma, data.depth);
1682 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1683 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1685 bounds = isl_basic_set_preimage_multi_aff(bounds,
1686 isl_multi_aff_copy(ma));
1687 domain = isl_set_preimage_multi_aff(domain,
1688 isl_multi_aff_copy(ma));
1689 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1690 umap = isl_union_map_from_map(map);
1691 executed = isl_union_map_apply_domain(executed,
1692 isl_union_map_copy(umap));
1693 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1694 umap);
1696 isl_aff_free(offset);
1697 isl_val_free(data.m);
1699 return create_node_scaled(executed, bounds, domain, build);
1702 /* Add the basic set to the list that "user" points to.
1704 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1706 isl_basic_set_list **list = user;
1708 *list = isl_basic_set_list_add(*list, bset);
1710 return 0;
1713 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1715 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1716 __isl_take isl_set *set)
1718 int n;
1719 isl_ctx *ctx;
1720 isl_basic_set_list *list;
1722 if (!set)
1723 return NULL;
1725 ctx = isl_set_get_ctx(set);
1727 n = isl_set_n_basic_set(set);
1728 list = isl_basic_set_list_alloc(ctx, n);
1729 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1730 list = isl_basic_set_list_free(list);
1732 isl_set_free(set);
1733 return list;
1736 /* Generate code for the schedule domain "bounds"
1737 * and add the result to "list".
1739 * We mainly detect strides here and check if the bounds do not
1740 * conflict with the current build domain
1741 * and then pass over control to create_node.
1743 * "bounds" reflects the bounds on the current dimension and possibly
1744 * some extra conditions on outer dimensions.
1745 * It does not, however, include any divs involving the current dimension,
1746 * so it does not capture any stride constraints.
1747 * We therefore need to compute that part of the schedule domain that
1748 * intersects with "bounds" and derive the strides from the result.
1750 static __isl_give isl_ast_graft_list *add_node(
1751 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1752 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1754 isl_ast_graft *graft;
1755 isl_set *domain = NULL;
1756 isl_union_set *uset;
1757 int empty, disjoint;
1759 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1760 executed = isl_union_map_intersect_domain(executed, uset);
1761 empty = isl_union_map_is_empty(executed);
1762 if (empty < 0)
1763 goto error;
1764 if (empty)
1765 goto done;
1767 uset = isl_union_map_domain(isl_union_map_copy(executed));
1768 domain = isl_set_from_union_set(uset);
1769 domain = isl_ast_build_specialize(build, domain);
1771 domain = isl_set_compute_divs(domain);
1772 domain = isl_ast_build_eliminate_inner(build, domain);
1773 disjoint = isl_set_is_disjoint(domain, build->domain);
1774 if (disjoint < 0)
1775 goto error;
1776 if (disjoint)
1777 goto done;
1779 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1781 graft = create_node(executed, bounds, domain,
1782 isl_ast_build_copy(build));
1783 list = isl_ast_graft_list_add(list, graft);
1784 isl_ast_build_free(build);
1785 return list;
1786 error:
1787 list = isl_ast_graft_list_free(list);
1788 done:
1789 isl_set_free(domain);
1790 isl_basic_set_free(bounds);
1791 isl_union_map_free(executed);
1792 isl_ast_build_free(build);
1793 return list;
1796 /* Does any element of i follow or coincide with any element of j
1797 * at the current depth for equal values of the outer dimensions?
1799 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1800 __isl_keep isl_basic_set *j, void *user)
1802 int depth = *(int *) user;
1803 isl_basic_map *test;
1804 int empty;
1805 int l;
1807 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1808 isl_basic_set_copy(j));
1809 for (l = 0; l < depth; ++l)
1810 test = isl_basic_map_equate(test, isl_dim_in, l,
1811 isl_dim_out, l);
1812 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1813 isl_dim_out, depth);
1814 empty = isl_basic_map_is_empty(test);
1815 isl_basic_map_free(test);
1817 return empty < 0 ? -1 : !empty;
1820 /* Split up each element of "list" into a part that is related to "bset"
1821 * according to "gt" and a part that is not.
1822 * Return a list that consist of "bset" and all the pieces.
1824 static __isl_give isl_basic_set_list *add_split_on(
1825 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1826 __isl_keep isl_basic_map *gt)
1828 int i, n;
1829 isl_basic_set_list *res;
1831 if (!list)
1832 bset = isl_basic_set_free(bset);
1834 gt = isl_basic_map_copy(gt);
1835 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1836 n = isl_basic_set_list_n_basic_set(list);
1837 res = isl_basic_set_list_from_basic_set(bset);
1838 for (i = 0; res && i < n; ++i) {
1839 isl_basic_set *bset;
1840 isl_set *set1, *set2;
1841 isl_basic_map *bmap;
1842 int empty;
1844 bset = isl_basic_set_list_get_basic_set(list, i);
1845 bmap = isl_basic_map_copy(gt);
1846 bmap = isl_basic_map_intersect_range(bmap, bset);
1847 bset = isl_basic_map_range(bmap);
1848 empty = isl_basic_set_is_empty(bset);
1849 if (empty < 0)
1850 res = isl_basic_set_list_free(res);
1851 if (empty) {
1852 isl_basic_set_free(bset);
1853 bset = isl_basic_set_list_get_basic_set(list, i);
1854 res = isl_basic_set_list_add(res, bset);
1855 continue;
1858 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1859 set1 = isl_set_from_basic_set(bset);
1860 bset = isl_basic_set_list_get_basic_set(list, i);
1861 set2 = isl_set_from_basic_set(bset);
1862 set1 = isl_set_subtract(set2, set1);
1863 set1 = isl_set_make_disjoint(set1);
1865 res = isl_basic_set_list_concat(res,
1866 isl_basic_set_list_from_set(set1));
1868 isl_basic_map_free(gt);
1869 isl_basic_set_list_free(list);
1870 return res;
1873 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1874 __isl_keep isl_basic_set_list *domain_list,
1875 __isl_keep isl_union_map *executed,
1876 __isl_keep isl_ast_build *build);
1878 /* Internal data structure for add_nodes.
1880 * "executed" and "build" are extra arguments to be passed to add_node.
1881 * "list" collects the results.
1883 struct isl_add_nodes_data {
1884 isl_union_map *executed;
1885 isl_ast_build *build;
1887 isl_ast_graft_list *list;
1890 /* Generate code for the schedule domains in "scc"
1891 * and add the results to "list".
1893 * The domains in "scc" form a strongly connected component in the ordering.
1894 * If the number of domains in "scc" is larger than 1, then this means
1895 * that we cannot determine a valid ordering for the domains in the component.
1896 * This should be fairly rare because the individual domains
1897 * have been made disjoint first.
1898 * The problem is that the domains may be integrally disjoint but not
1899 * rationally disjoint. For example, we may have domains
1901 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1903 * These two domains have an empty intersection, but their rational
1904 * relaxations do intersect. It is impossible to order these domains
1905 * in the second dimension because the first should be ordered before
1906 * the second for outer dimension equal to 0, while it should be ordered
1907 * after for outer dimension equal to 1.
1909 * This may happen in particular in case of unrolling since the domain
1910 * of each slice is replaced by its simple hull.
1912 * For each basic set i in "scc" and for each of the following basic sets j,
1913 * we split off that part of the basic set i that shares the outer dimensions
1914 * with j and lies before j in the current dimension.
1915 * We collect all the pieces in a new list that replaces "scc".
1917 * While the elements in "scc" should be disjoint, we double-check
1918 * this property to avoid running into an infinite recursion in case
1919 * they intersect due to some internal error.
1921 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1923 struct isl_add_nodes_data *data = user;
1924 int i, n, depth;
1925 isl_basic_set *bset, *first;
1926 isl_basic_set_list *list;
1927 isl_space *space;
1928 isl_basic_map *gt;
1930 n = isl_basic_set_list_n_basic_set(scc);
1931 bset = isl_basic_set_list_get_basic_set(scc, 0);
1932 if (n == 1) {
1933 isl_basic_set_list_free(scc);
1934 data->list = add_node(data->list,
1935 isl_union_map_copy(data->executed), bset,
1936 isl_ast_build_copy(data->build));
1937 return data->list ? 0 : -1;
1940 depth = isl_ast_build_get_depth(data->build);
1941 space = isl_basic_set_get_space(bset);
1942 space = isl_space_map_from_set(space);
1943 gt = isl_basic_map_universe(space);
1944 for (i = 0; i < depth; ++i)
1945 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1946 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1948 first = isl_basic_set_copy(bset);
1949 list = isl_basic_set_list_from_basic_set(bset);
1950 for (i = 1; i < n; ++i) {
1951 int disjoint;
1953 bset = isl_basic_set_list_get_basic_set(scc, i);
1955 disjoint = isl_basic_set_is_disjoint(bset, first);
1956 if (disjoint < 0)
1957 list = isl_basic_set_list_free(list);
1958 else if (!disjoint)
1959 isl_die(isl_basic_set_list_get_ctx(scc),
1960 isl_error_internal,
1961 "basic sets in scc are assumed to be disjoint",
1962 list = isl_basic_set_list_free(list));
1964 list = add_split_on(list, bset, gt);
1966 isl_basic_set_free(first);
1967 isl_basic_map_free(gt);
1968 isl_basic_set_list_free(scc);
1969 scc = list;
1970 data->list = isl_ast_graft_list_concat(data->list,
1971 generate_sorted_domains(scc, data->executed, data->build));
1972 isl_basic_set_list_free(scc);
1974 return data->list ? 0 : -1;
1977 /* Sort the domains in "domain_list" according to the execution order
1978 * at the current depth (for equal values of the outer dimensions),
1979 * generate code for each of them, collecting the results in a list.
1980 * If no code is generated (because the intersection of the inverse schedule
1981 * with the domains turns out to be empty), then an empty list is returned.
1983 * The caller is responsible for ensuring that the basic sets in "domain_list"
1984 * are pair-wise disjoint. It can, however, in principle happen that
1985 * two basic sets should be ordered one way for one value of the outer
1986 * dimensions and the other way for some other value of the outer dimensions.
1987 * We therefore play safe and look for strongly connected components.
1988 * The function add_nodes takes care of handling non-trivial components.
1990 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1991 __isl_keep isl_basic_set_list *domain_list,
1992 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1994 isl_ctx *ctx;
1995 struct isl_add_nodes_data data;
1996 int depth;
1997 int n;
1999 if (!domain_list)
2000 return NULL;
2002 ctx = isl_basic_set_list_get_ctx(domain_list);
2003 n = isl_basic_set_list_n_basic_set(domain_list);
2004 data.list = isl_ast_graft_list_alloc(ctx, n);
2005 if (n == 0)
2006 return data.list;
2007 if (n == 1)
2008 return add_node(data.list, isl_union_map_copy(executed),
2009 isl_basic_set_list_get_basic_set(domain_list, 0),
2010 isl_ast_build_copy(build));
2012 depth = isl_ast_build_get_depth(build);
2013 data.executed = executed;
2014 data.build = build;
2015 if (isl_basic_set_list_foreach_scc(domain_list,
2016 &domain_follows_at_depth, &depth,
2017 &add_nodes, &data) < 0)
2018 data.list = isl_ast_graft_list_free(data.list);
2020 return data.list;
2023 /* Do i and j share any values for the outer dimensions?
2025 static int shared_outer(__isl_keep isl_basic_set *i,
2026 __isl_keep isl_basic_set *j, void *user)
2028 int depth = *(int *) user;
2029 isl_basic_map *test;
2030 int empty;
2031 int l;
2033 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2034 isl_basic_set_copy(j));
2035 for (l = 0; l < depth; ++l)
2036 test = isl_basic_map_equate(test, isl_dim_in, l,
2037 isl_dim_out, l);
2038 empty = isl_basic_map_is_empty(test);
2039 isl_basic_map_free(test);
2041 return empty < 0 ? -1 : !empty;
2044 /* Internal data structure for generate_sorted_domains_wrap.
2046 * "n" is the total number of basic sets
2047 * "executed" and "build" are extra arguments to be passed
2048 * to generate_sorted_domains.
2050 * "single" is set to 1 by generate_sorted_domains_wrap if there
2051 * is only a single component.
2052 * "list" collects the results.
2054 struct isl_ast_generate_parallel_domains_data {
2055 int n;
2056 isl_union_map *executed;
2057 isl_ast_build *build;
2059 int single;
2060 isl_ast_graft_list *list;
2063 /* Call generate_sorted_domains on "scc", fuse the result into a list
2064 * with either zero or one graft and collect the these single element
2065 * lists into data->list.
2067 * If there is only one component, i.e., if the number of basic sets
2068 * in the current component is equal to the total number of basic sets,
2069 * then data->single is set to 1 and the result of generate_sorted_domains
2070 * is not fused.
2072 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2073 void *user)
2075 struct isl_ast_generate_parallel_domains_data *data = user;
2076 isl_ast_graft_list *list;
2078 list = generate_sorted_domains(scc, data->executed, data->build);
2079 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2080 if (!data->single)
2081 list = isl_ast_graft_list_fuse(list, data->build);
2082 if (!data->list)
2083 data->list = list;
2084 else
2085 data->list = isl_ast_graft_list_concat(data->list, list);
2087 isl_basic_set_list_free(scc);
2088 if (!data->list)
2089 return -1;
2091 return 0;
2094 /* Look for any (weakly connected) components in the "domain_list"
2095 * of domains that share some values of the outer dimensions.
2096 * That is, domains in different components do not share any values
2097 * of the outer dimensions. This means that these components
2098 * can be freely reordered.
2099 * Within each of the components, we sort the domains according
2100 * to the execution order at the current depth.
2102 * If there is more than one component, then generate_sorted_domains_wrap
2103 * fuses the result of each call to generate_sorted_domains
2104 * into a list with either zero or one graft and collects these (at most)
2105 * single element lists into a bigger list. This means that the elements of the
2106 * final list can be freely reordered. In particular, we sort them
2107 * according to an arbitrary but fixed ordering to ease merging of
2108 * graft lists from different components.
2110 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2111 __isl_keep isl_basic_set_list *domain_list,
2112 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2114 int depth;
2115 struct isl_ast_generate_parallel_domains_data data;
2117 if (!domain_list)
2118 return NULL;
2120 data.n = isl_basic_set_list_n_basic_set(domain_list);
2121 if (data.n <= 1)
2122 return generate_sorted_domains(domain_list, executed, build);
2124 depth = isl_ast_build_get_depth(build);
2125 data.list = NULL;
2126 data.executed = executed;
2127 data.build = build;
2128 data.single = 0;
2129 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2130 &generate_sorted_domains_wrap,
2131 &data) < 0)
2132 data.list = isl_ast_graft_list_free(data.list);
2134 if (!data.single)
2135 data.list = isl_ast_graft_list_sort_guard(data.list);
2137 return data.list;
2140 /* Internal data for separate_domain.
2142 * "explicit" is set if we only want to use explicit bounds.
2144 * "domain" collects the separated domains.
2146 struct isl_separate_domain_data {
2147 isl_ast_build *build;
2148 int explicit;
2149 isl_set *domain;
2152 /* Extract implicit bounds on the current dimension for the executed "map".
2154 * The domain of "map" may involve inner dimensions, so we
2155 * need to eliminate them.
2157 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2158 __isl_keep isl_ast_build *build)
2160 isl_set *domain;
2162 domain = isl_map_domain(map);
2163 domain = isl_ast_build_eliminate(build, domain);
2165 return domain;
2168 /* Extract explicit bounds on the current dimension for the executed "map".
2170 * Rather than eliminating the inner dimensions as in implicit_bounds,
2171 * we simply drop any constraints involving those inner dimensions.
2172 * The idea is that most bounds that are implied by constraints on the
2173 * inner dimensions will be enforced by for loops and not by explicit guards.
2174 * There is then no need to separate along those bounds.
2176 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2177 __isl_keep isl_ast_build *build)
2179 isl_set *domain;
2180 int depth, dim;
2182 dim = isl_map_dim(map, isl_dim_out);
2183 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2185 domain = isl_map_domain(map);
2186 depth = isl_ast_build_get_depth(build);
2187 dim = isl_set_dim(domain, isl_dim_set);
2188 domain = isl_set_detect_equalities(domain);
2189 domain = isl_set_drop_constraints_involving_dims(domain,
2190 isl_dim_set, depth + 1, dim - (depth + 1));
2191 domain = isl_set_remove_divs_involving_dims(domain,
2192 isl_dim_set, depth, 1);
2193 domain = isl_set_remove_unknown_divs(domain);
2195 return domain;
2198 /* Split data->domain into pieces that intersect with the range of "map"
2199 * and pieces that do not intersect with the range of "map"
2200 * and then add that part of the range of "map" that does not intersect
2201 * with data->domain.
2203 static int separate_domain(__isl_take isl_map *map, void *user)
2205 struct isl_separate_domain_data *data = user;
2206 isl_set *domain;
2207 isl_set *d1, *d2;
2209 if (data->explicit)
2210 domain = explicit_bounds(map, data->build);
2211 else
2212 domain = implicit_bounds(map, data->build);
2214 domain = isl_set_coalesce(domain);
2215 domain = isl_set_make_disjoint(domain);
2216 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2217 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2218 data->domain = isl_set_intersect(data->domain, domain);
2219 data->domain = isl_set_union(data->domain, d1);
2220 data->domain = isl_set_union(data->domain, d2);
2222 return 0;
2225 /* Separate the schedule domains of "executed".
2227 * That is, break up the domain of "executed" into basic sets,
2228 * such that for each basic set S, every element in S is associated with
2229 * the same domain spaces.
2231 * "space" is the (single) domain space of "executed".
2233 static __isl_give isl_set *separate_schedule_domains(
2234 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2235 __isl_keep isl_ast_build *build)
2237 struct isl_separate_domain_data data = { build };
2238 isl_ctx *ctx;
2240 ctx = isl_ast_build_get_ctx(build);
2241 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2242 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2243 data.domain = isl_set_empty(space);
2244 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2245 data.domain = isl_set_free(data.domain);
2247 isl_union_map_free(executed);
2248 return data.domain;
2251 /* Temporary data used during the search for a lower bound for unrolling.
2253 * "build" is the build in which the unrolling will be performed
2254 * "domain" is the original set for which to find a lower bound
2255 * "depth" is the dimension for which to find a lower boudn
2256 * "expansion" is the expansion that needs to be applied to "domain"
2257 * in the unrolling that will be performed
2259 * "lower" is the best lower bound found so far. It is NULL if we have not
2260 * found any yet.
2261 * "n" is the corresponding size. If lower is NULL, then the value of n
2262 * is undefined.
2263 * "n_div" is the maximal number of integer divisions in the first
2264 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2265 * been computed yet.
2267 struct isl_find_unroll_data {
2268 isl_ast_build *build;
2269 isl_set *domain;
2270 int depth;
2271 isl_basic_map *expansion;
2273 isl_aff *lower;
2274 int *n;
2275 int n_div;
2278 /* Return the constraint
2280 * i_"depth" = aff + offset
2282 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2283 int offset)
2285 aff = isl_aff_copy(aff);
2286 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2287 aff = isl_aff_add_constant_si(aff, offset);
2288 return isl_equality_from_aff(aff);
2291 /* Update *user to the number of integer divsions in the first element
2292 * of "ma", if it is larger than the current value.
2294 static int update_n_div(__isl_take isl_set *set, __isl_take isl_multi_aff *ma,
2295 void *user)
2297 isl_aff *aff;
2298 int *n = user;
2299 int n_div;
2301 aff = isl_multi_aff_get_aff(ma, 0);
2302 n_div = isl_aff_dim(aff, isl_dim_div);
2303 isl_aff_free(aff);
2304 isl_multi_aff_free(ma);
2305 isl_set_free(set);
2307 if (n_div > *n)
2308 *n = n_div;
2310 return aff ? 0 : -1;
2313 /* Get the number of integer divisions in the expression for the iterator
2314 * value at the first slice in the unrolling based on lower bound "lower",
2315 * taking into account the expansion that needs to be performed on this slice.
2317 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2318 __isl_keep isl_aff *lower)
2320 isl_constraint *c;
2321 isl_set *set;
2322 isl_map *it_map, *expansion;
2323 isl_pw_multi_aff *pma;
2324 int n;
2326 c = at_offset(data->depth, lower, 0);
2327 set = isl_set_copy(data->domain);
2328 set = isl_set_add_constraint(set, c);
2329 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2330 set = isl_set_apply(set, expansion);
2331 it_map = isl_ast_build_map_to_iterator(data->build, set);
2332 pma = isl_pw_multi_aff_from_map(it_map);
2333 n = 0;
2334 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2335 n = -1;
2336 isl_pw_multi_aff_free(pma);
2338 return n;
2341 /* Is the lower bound "lower" with corresponding iteration count "n"
2342 * better than the one stored in "data"?
2343 * If there is no upper bound on the iteration count ("n" is infinity) or
2344 * if the count is too large, then we cannot use this lower bound.
2345 * Otherwise, if there was no previous lower bound or
2346 * if the iteration count of the new lower bound is smaller than
2347 * the iteration count of the previous lower bound, then we consider
2348 * the new lower bound to be better.
2349 * If the iteration count is the same, then compare the number
2350 * of integer divisions that would be needed to express
2351 * the iterator value at the first slice in the unrolling
2352 * according to the lower bound. If we end up computing this
2353 * number, then store the lowest value in data->n_div.
2355 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2356 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2358 int cmp;
2359 int n_div;
2361 if (!n)
2362 return -1;
2363 if (isl_val_is_infty(n))
2364 return 0;
2365 if (isl_val_cmp_si(n, INT_MAX) > 0)
2366 return 0;
2367 if (!data->lower)
2368 return 1;
2369 cmp = isl_val_cmp_si(n, *data->n);
2370 if (cmp < 0)
2371 return 1;
2372 if (cmp > 0)
2373 return 0;
2374 if (data->n_div < 0)
2375 data->n_div = get_expanded_n_div(data, data->lower);
2376 if (data->n_div < 0)
2377 return -1;
2378 if (data->n_div == 0)
2379 return 0;
2380 n_div = get_expanded_n_div(data, lower);
2381 if (n_div < 0)
2382 return -1;
2383 if (n_div >= data->n_div)
2384 return 0;
2385 data->n_div = n_div;
2387 return 1;
2390 /* Check if we can use "c" as a lower bound and if it is better than
2391 * any previously found lower bound.
2393 * If "c" does not involve the dimension at the current depth,
2394 * then we cannot use it.
2395 * Otherwise, let "c" be of the form
2397 * i >= f(j)/a
2399 * We compute the maximal value of
2401 * -ceil(f(j)/a)) + i + 1
2403 * over the domain. If there is such a value "n", then we know
2405 * -ceil(f(j)/a)) + i + 1 <= n
2407 * or
2409 * i < ceil(f(j)/a)) + n
2411 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2412 * We just need to check if we have found any lower bound before and
2413 * if the new lower bound is better (smaller n or fewer integer divisions)
2414 * than the previously found lower bounds.
2416 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2417 __isl_keep isl_constraint *c)
2419 isl_aff *aff, *lower;
2420 isl_val *max;
2421 int better;
2423 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2424 return 0;
2426 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2427 lower = isl_aff_ceil(lower);
2428 aff = isl_aff_copy(lower);
2429 aff = isl_aff_neg(aff);
2430 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2431 aff = isl_aff_add_constant_si(aff, 1);
2432 max = isl_set_max_val(data->domain, aff);
2433 isl_aff_free(aff);
2435 better = is_better_lower_bound(data, lower, max);
2436 if (better < 0 || !better) {
2437 isl_val_free(max);
2438 isl_aff_free(lower);
2439 return better < 0 ? -1 : 0;
2442 isl_aff_free(data->lower);
2443 data->lower = lower;
2444 *data->n = isl_val_get_num_si(max);
2445 isl_val_free(max);
2447 return 1;
2450 /* Check if we can use "c" as a lower bound and if it is better than
2451 * any previously found lower bound.
2453 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2455 struct isl_find_unroll_data *data;
2456 int r;
2458 data = (struct isl_find_unroll_data *) user;
2459 r = update_unrolling_lower_bound(data, c);
2460 isl_constraint_free(c);
2462 return r;
2465 /* Look for a lower bound l(i) on the dimension at "depth"
2466 * and a size n such that "domain" is a subset of
2468 * { [i] : l(i) <= i_d < l(i) + n }
2470 * where d is "depth" and l(i) depends only on earlier dimensions.
2471 * Furthermore, try and find a lower bound such that n is as small as possible.
2472 * In particular, "n" needs to be finite.
2473 * "build" is the build in which the unrolling will be performed.
2474 * "expansion" is the expansion that needs to be applied to "domain"
2475 * in the unrolling that will be performed.
2477 * Inner dimensions have been eliminated from "domain" by the caller.
2479 * We first construct a collection of lower bounds on the input set
2480 * by computing its simple hull. We then iterate through them,
2481 * discarding those that we cannot use (either because they do not
2482 * involve the dimension at "depth" or because they have no corresponding
2483 * upper bound, meaning that "n" would be unbounded) and pick out the
2484 * best from the remaining ones.
2486 * If we cannot find a suitable lower bound, then we consider that
2487 * to be an error.
2489 static __isl_give isl_aff *find_unroll_lower_bound(
2490 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2491 int depth, __isl_keep isl_basic_map *expansion, int *n)
2493 struct isl_find_unroll_data data =
2494 { build, domain, depth, expansion, NULL, n, -1 };
2495 isl_basic_set *hull;
2497 hull = isl_set_simple_hull(isl_set_copy(domain));
2499 if (isl_basic_set_foreach_constraint(hull,
2500 &constraint_find_unroll, &data) < 0)
2501 goto error;
2503 isl_basic_set_free(hull);
2505 if (!data.lower)
2506 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2507 "cannot find lower bound for unrolling", return NULL);
2509 return data.lower;
2510 error:
2511 isl_basic_set_free(hull);
2512 return isl_aff_free(data.lower);
2515 /* Call "fn" on each iteration of the current dimension of "domain".
2516 * If "init" is not NULL, then it is called with the number of
2517 * iterations before any call to "fn".
2518 * Return -1 on failure.
2520 * Since we are going to be iterating over the individual values,
2521 * we first check if there are any strides on the current dimension.
2522 * If there is, we rewrite the current dimension i as
2524 * i = stride i' + offset
2526 * and then iterate over individual values of i' instead.
2528 * We then look for a lower bound on i' and a size such that the domain
2529 * is a subset of
2531 * { [j,i'] : l(j) <= i' < l(j) + n }
2533 * and then take slices of the domain at values of i'
2534 * between l(j) and l(j) + n - 1.
2536 * We compute the unshifted simple hull of each slice to ensure that
2537 * we have a single basic set per offset. The slicing constraint
2538 * may get simplified away before the unshifted simple hull is taken
2539 * and may therefore in some rare cases disappear from the result.
2540 * We therefore explicitly add the constraint back after computing
2541 * the unshifted simple hull to ensure that the basic sets
2542 * remain disjoint. The constraints that are dropped by taking the hull
2543 * will be taken into account at the next level, as in the case of the
2544 * atomic option.
2546 * Finally, we map i' back to i and call "fn".
2548 static int foreach_iteration(__isl_take isl_set *domain,
2549 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2550 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2552 int i, n;
2553 int depth;
2554 isl_multi_aff *expansion;
2555 isl_basic_map *bmap;
2556 isl_aff *lower;
2557 isl_ast_build *stride_build;
2559 depth = isl_ast_build_get_depth(build);
2561 domain = isl_ast_build_eliminate_inner(build, domain);
2562 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2563 stride_build = isl_ast_build_copy(build);
2564 stride_build = isl_ast_build_detect_strides(stride_build,
2565 isl_set_copy(domain));
2566 expansion = isl_ast_build_get_stride_expansion(stride_build);
2568 domain = isl_set_preimage_multi_aff(domain,
2569 isl_multi_aff_copy(expansion));
2570 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2571 isl_ast_build_free(stride_build);
2573 bmap = isl_basic_map_from_multi_aff(expansion);
2575 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2576 if (!lower)
2577 domain = isl_set_free(domain);
2579 if (init && init(n, user) < 0)
2580 domain = isl_set_free(domain);
2581 for (i = 0; i < n; ++i) {
2582 isl_set *set;
2583 isl_basic_set *bset;
2584 isl_constraint *slice;
2586 slice = at_offset(depth, lower, i);
2587 set = isl_set_copy(domain);
2588 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2589 bset = isl_set_unshifted_simple_hull(set);
2590 bset = isl_basic_set_add_constraint(bset, slice);
2591 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2593 if (fn(bset, user) < 0)
2594 break;
2597 isl_aff_free(lower);
2598 isl_set_free(domain);
2599 isl_basic_map_free(bmap);
2601 return i < n ? -1 : 0;
2604 /* Data structure for storing the results and the intermediate objects
2605 * of compute_domains.
2607 * "list" is the main result of the function and contains a list
2608 * of disjoint basic sets for which code should be generated.
2610 * "executed" and "build" are inputs to compute_domains.
2611 * "schedule_domain" is the domain of "executed".
2613 * "option" constains the domains at the current depth that should by
2614 * atomic, separated or unrolled. These domains are as specified by
2615 * the user, except that inner dimensions have been eliminated and
2616 * that they have been made pair-wise disjoint.
2618 * "sep_class" contains the user-specified split into separation classes
2619 * specialized to the current depth.
2620 * "done" contains the union of the separation domains that have already
2621 * been handled.
2623 struct isl_codegen_domains {
2624 isl_basic_set_list *list;
2626 isl_union_map *executed;
2627 isl_ast_build *build;
2628 isl_set *schedule_domain;
2630 isl_set *option[4];
2632 isl_map *sep_class;
2633 isl_set *done;
2636 /* Internal data structure for do_unroll.
2638 * "domains" stores the results of compute_domains.
2639 * "class_domain" is the original class domain passed to do_unroll.
2640 * "unroll_domain" collects the unrolled iterations.
2642 struct isl_ast_unroll_data {
2643 struct isl_codegen_domains *domains;
2644 isl_set *class_domain;
2645 isl_set *unroll_domain;
2648 /* Given an iteration of an unrolled domain represented by "bset",
2649 * add it to data->domains->list.
2650 * Since we may have dropped some constraints, we intersect with
2651 * the class domain again to ensure that each element in the list
2652 * is disjoint from the other class domains.
2654 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2656 struct isl_ast_unroll_data *data = user;
2657 isl_set *set;
2658 isl_basic_set_list *list;
2660 set = isl_set_from_basic_set(bset);
2661 data->unroll_domain = isl_set_union(data->unroll_domain,
2662 isl_set_copy(set));
2663 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2664 set = isl_set_make_disjoint(set);
2665 list = isl_basic_set_list_from_set(set);
2666 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2667 list);
2669 return 0;
2672 /* Extend domains->list with a list of basic sets, one for each value
2673 * of the current dimension in "domain" and remove the corresponding
2674 * sets from the class domain. Return the updated class domain.
2675 * The divs that involve the current dimension have not been projected out
2676 * from this domain.
2678 * We call foreach_iteration to iterate over the individual values and
2679 * in do_unroll_iteration we collect the individual basic sets in
2680 * domains->list and their union in data->unroll_domain, which is then
2681 * used to update the class domain.
2683 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2684 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2686 struct isl_ast_unroll_data data;
2688 if (!domain)
2689 return isl_set_free(class_domain);
2690 if (!class_domain)
2691 return isl_set_free(domain);
2693 data.domains = domains;
2694 data.class_domain = class_domain;
2695 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2697 if (foreach_iteration(domain, domains->build, NULL,
2698 &do_unroll_iteration, &data) < 0)
2699 data.unroll_domain = isl_set_free(data.unroll_domain);
2701 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2703 return class_domain;
2706 /* Add domains to domains->list for each individual value of the current
2707 * dimension, for that part of the schedule domain that lies in the
2708 * intersection of the option domain and the class domain.
2709 * Remove the corresponding sets from the class domain and
2710 * return the updated class domain.
2712 * We first break up the unroll option domain into individual pieces
2713 * and then handle each of them separately. The unroll option domain
2714 * has been made disjoint in compute_domains_init_options,
2716 * Note that we actively want to combine different pieces of the
2717 * schedule domain that have the same value at the current dimension.
2718 * We therefore need to break up the unroll option domain before
2719 * intersecting with class and schedule domain, hoping that the
2720 * unroll option domain specified by the user is relatively simple.
2722 static __isl_give isl_set *compute_unroll_domains(
2723 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2725 isl_set *unroll_domain;
2726 isl_basic_set_list *unroll_list;
2727 int i, n;
2728 int empty;
2730 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2731 if (empty < 0)
2732 return isl_set_free(class_domain);
2733 if (empty)
2734 return class_domain;
2736 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2737 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2739 n = isl_basic_set_list_n_basic_set(unroll_list);
2740 for (i = 0; i < n; ++i) {
2741 isl_basic_set *bset;
2743 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2744 unroll_domain = isl_set_from_basic_set(bset);
2745 unroll_domain = isl_set_intersect(unroll_domain,
2746 isl_set_copy(class_domain));
2747 unroll_domain = isl_set_intersect(unroll_domain,
2748 isl_set_copy(domains->schedule_domain));
2750 empty = isl_set_is_empty(unroll_domain);
2751 if (empty >= 0 && empty) {
2752 isl_set_free(unroll_domain);
2753 continue;
2756 class_domain = do_unroll(domains, unroll_domain, class_domain);
2759 isl_basic_set_list_free(unroll_list);
2761 return class_domain;
2764 /* Try and construct a single basic set that includes the intersection of
2765 * the schedule domain, the atomic option domain and the class domain.
2766 * Add the resulting basic set(s) to domains->list and remove them
2767 * from class_domain. Return the updated class domain.
2769 * We construct a single domain rather than trying to combine
2770 * the schedule domains of individual domains because we are working
2771 * within a single component so that non-overlapping schedule domains
2772 * should already have been separated.
2773 * We do however need to make sure that this single domains is a subset
2774 * of the class domain so that it would not intersect with any other
2775 * class domains. This means that we may end up splitting up the atomic
2776 * domain in case separation classes are being used.
2778 * "domain" is the intersection of the schedule domain and the class domain,
2779 * with inner dimensions projected out.
2781 static __isl_give isl_set *compute_atomic_domain(
2782 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2784 isl_basic_set *bset;
2785 isl_basic_set_list *list;
2786 isl_set *domain, *atomic_domain;
2787 int empty;
2789 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2790 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2791 domain = isl_set_intersect(domain,
2792 isl_set_copy(domains->schedule_domain));
2793 empty = isl_set_is_empty(domain);
2794 if (empty < 0)
2795 class_domain = isl_set_free(class_domain);
2796 if (empty) {
2797 isl_set_free(domain);
2798 return class_domain;
2801 domain = isl_ast_build_eliminate(domains->build, domain);
2802 domain = isl_set_coalesce(domain);
2803 bset = isl_set_unshifted_simple_hull(domain);
2804 domain = isl_set_from_basic_set(bset);
2805 atomic_domain = isl_set_copy(domain);
2806 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2807 class_domain = isl_set_subtract(class_domain, atomic_domain);
2808 domain = isl_set_make_disjoint(domain);
2809 list = isl_basic_set_list_from_set(domain);
2810 domains->list = isl_basic_set_list_concat(domains->list, list);
2812 return class_domain;
2815 /* Split up the schedule domain into uniform basic sets,
2816 * in the sense that each element in a basic set is associated to
2817 * elements of the same domains, and add the result to domains->list.
2818 * Do this for that part of the schedule domain that lies in the
2819 * intersection of "class_domain" and the separate option domain.
2821 * "class_domain" may or may not include the constraints
2822 * of the schedule domain, but this does not make a difference
2823 * since we are going to intersect it with the domain of the inverse schedule.
2824 * If it includes schedule domain constraints, then they may involve
2825 * inner dimensions, but we will eliminate them in separation_domain.
2827 static int compute_separate_domain(struct isl_codegen_domains *domains,
2828 __isl_keep isl_set *class_domain)
2830 isl_space *space;
2831 isl_set *domain;
2832 isl_union_map *executed;
2833 isl_basic_set_list *list;
2834 int empty;
2836 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2837 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2838 executed = isl_union_map_copy(domains->executed);
2839 executed = isl_union_map_intersect_domain(executed,
2840 isl_union_set_from_set(domain));
2841 empty = isl_union_map_is_empty(executed);
2842 if (empty < 0 || empty) {
2843 isl_union_map_free(executed);
2844 return empty < 0 ? -1 : 0;
2847 space = isl_set_get_space(class_domain);
2848 domain = separate_schedule_domains(space, executed, domains->build);
2850 list = isl_basic_set_list_from_set(domain);
2851 domains->list = isl_basic_set_list_concat(domains->list, list);
2853 return 0;
2856 /* Split up the domain at the current depth into disjoint
2857 * basic sets for which code should be generated separately
2858 * for the given separation class domain.
2860 * If any separation classes have been defined, then "class_domain"
2861 * is the domain of the current class and does not refer to inner dimensions.
2862 * Otherwise, "class_domain" is the universe domain.
2864 * We first make sure that the class domain is disjoint from
2865 * previously considered class domains.
2867 * The separate domains can be computed directly from the "class_domain".
2869 * The unroll, atomic and remainder domains need the constraints
2870 * from the schedule domain.
2872 * For unrolling, the actual schedule domain is needed (with divs that
2873 * may refer to the current dimension) so that stride detection can be
2874 * performed.
2876 * For atomic and remainder domains, inner dimensions and divs involving
2877 * the current dimensions should be eliminated.
2878 * In case we are working within a separation class, we need to intersect
2879 * the result with the current "class_domain" to ensure that the domains
2880 * are disjoint from those generated from other class domains.
2882 * The domain that has been made atomic may be larger than specified
2883 * by the user since it needs to be representable as a single basic set.
2884 * This possibly larger domain is removed from class_domain by
2885 * compute_atomic_domain. It is computed first so that the extended domain
2886 * would not overlap with any domains computed before.
2887 * Similary, the unrolled domains may have some constraints removed and
2888 * may therefore also be larger than specified by the user.
2890 * If anything is left after handling separate, unroll and atomic,
2891 * we split it up into basic sets and append the basic sets to domains->list.
2893 static int compute_partial_domains(struct isl_codegen_domains *domains,
2894 __isl_take isl_set *class_domain)
2896 isl_basic_set_list *list;
2897 isl_set *domain;
2899 class_domain = isl_set_subtract(class_domain,
2900 isl_set_copy(domains->done));
2901 domains->done = isl_set_union(domains->done,
2902 isl_set_copy(class_domain));
2904 class_domain = compute_atomic_domain(domains, class_domain);
2905 class_domain = compute_unroll_domains(domains, class_domain);
2907 domain = isl_set_copy(class_domain);
2909 if (compute_separate_domain(domains, domain) < 0)
2910 goto error;
2911 domain = isl_set_subtract(domain,
2912 isl_set_copy(domains->option[isl_ast_loop_separate]));
2914 domain = isl_set_intersect(domain,
2915 isl_set_copy(domains->schedule_domain));
2917 domain = isl_ast_build_eliminate(domains->build, domain);
2918 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2920 domain = isl_set_coalesce(domain);
2921 domain = isl_set_make_disjoint(domain);
2923 list = isl_basic_set_list_from_set(domain);
2924 domains->list = isl_basic_set_list_concat(domains->list, list);
2926 isl_set_free(class_domain);
2928 return 0;
2929 error:
2930 isl_set_free(domain);
2931 isl_set_free(class_domain);
2932 return -1;
2935 /* Split up the domain at the current depth into disjoint
2936 * basic sets for which code should be generated separately
2937 * for the separation class identified by "pnt".
2939 * We extract the corresponding class domain from domains->sep_class,
2940 * eliminate inner dimensions and pass control to compute_partial_domains.
2942 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2944 struct isl_codegen_domains *domains = user;
2945 isl_set *class_set;
2946 isl_set *domain;
2947 int disjoint;
2949 class_set = isl_set_from_point(pnt);
2950 domain = isl_map_domain(isl_map_intersect_range(
2951 isl_map_copy(domains->sep_class), class_set));
2952 domain = isl_ast_build_compute_gist(domains->build, domain);
2953 domain = isl_ast_build_eliminate(domains->build, domain);
2955 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2956 if (disjoint < 0)
2957 return -1;
2958 if (disjoint) {
2959 isl_set_free(domain);
2960 return 0;
2963 return compute_partial_domains(domains, domain);
2966 /* Extract the domains at the current depth that should be atomic,
2967 * separated or unrolled and store them in option.
2969 * The domains specified by the user might overlap, so we make
2970 * them disjoint by subtracting earlier domains from later domains.
2972 static void compute_domains_init_options(isl_set *option[4],
2973 __isl_keep isl_ast_build *build)
2975 enum isl_ast_loop_type type, type2;
2976 isl_set *unroll;
2978 for (type = isl_ast_loop_atomic;
2979 type <= isl_ast_loop_separate; ++type) {
2980 option[type] = isl_ast_build_get_option_domain(build, type);
2981 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
2982 option[type] = isl_set_subtract(option[type],
2983 isl_set_copy(option[type2]));
2986 unroll = option[isl_ast_loop_unroll];
2987 unroll = isl_set_coalesce(unroll);
2988 unroll = isl_set_make_disjoint(unroll);
2989 option[isl_ast_loop_unroll] = unroll;
2992 /* Split up the domain at the current depth into disjoint
2993 * basic sets for which code should be generated separately,
2994 * based on the user-specified options.
2995 * Return the list of disjoint basic sets.
2997 * There are three kinds of domains that we need to keep track of.
2998 * - the "schedule domain" is the domain of "executed"
2999 * - the "class domain" is the domain corresponding to the currrent
3000 * separation class
3001 * - the "option domain" is the domain corresponding to one of the options
3002 * atomic, unroll or separate
3004 * We first consider the individial values of the separation classes
3005 * and split up the domain for each of them separately.
3006 * Finally, we consider the remainder. If no separation classes were
3007 * specified, then we call compute_partial_domains with the universe
3008 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3009 * with inner dimensions removed. We do this because we want to
3010 * avoid computing the complement of the class domains (i.e., the difference
3011 * between the universe and domains->done).
3013 static __isl_give isl_basic_set_list *compute_domains(
3014 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3016 struct isl_codegen_domains domains;
3017 isl_ctx *ctx;
3018 isl_set *domain;
3019 isl_union_set *schedule_domain;
3020 isl_set *classes;
3021 isl_space *space;
3022 int n_param;
3023 enum isl_ast_loop_type type;
3024 int empty;
3026 if (!executed)
3027 return NULL;
3029 ctx = isl_union_map_get_ctx(executed);
3030 domains.list = isl_basic_set_list_alloc(ctx, 0);
3032 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3033 domain = isl_set_from_union_set(schedule_domain);
3035 compute_domains_init_options(domains.option, build);
3037 domains.sep_class = isl_ast_build_get_separation_class(build);
3038 classes = isl_map_range(isl_map_copy(domains.sep_class));
3039 n_param = isl_set_dim(classes, isl_dim_param);
3040 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3042 space = isl_set_get_space(domain);
3043 domains.build = build;
3044 domains.schedule_domain = isl_set_copy(domain);
3045 domains.executed = executed;
3046 domains.done = isl_set_empty(space);
3048 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3049 domains.list = isl_basic_set_list_free(domains.list);
3050 isl_set_free(classes);
3052 empty = isl_set_is_empty(domains.done);
3053 if (empty < 0) {
3054 domains.list = isl_basic_set_list_free(domains.list);
3055 domain = isl_set_free(domain);
3056 } else if (empty) {
3057 isl_set_free(domain);
3058 domain = isl_set_universe(isl_set_get_space(domains.done));
3059 } else {
3060 domain = isl_ast_build_eliminate(build, domain);
3062 if (compute_partial_domains(&domains, domain) < 0)
3063 domains.list = isl_basic_set_list_free(domains.list);
3065 isl_set_free(domains.schedule_domain);
3066 isl_set_free(domains.done);
3067 isl_map_free(domains.sep_class);
3068 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3069 isl_set_free(domains.option[type]);
3071 return domains.list;
3074 /* Generate code for a single component, after shifting (if any)
3075 * has been applied, in case the schedule was specified as a union map.
3077 * We first split up the domain at the current depth into disjoint
3078 * basic sets based on the user-specified options.
3079 * Then we generated code for each of them and concatenate the results.
3081 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3082 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3084 isl_basic_set_list *domain_list;
3085 isl_ast_graft_list *list = NULL;
3087 domain_list = compute_domains(executed, build);
3088 list = generate_parallel_domains(domain_list, executed, build);
3090 isl_basic_set_list_free(domain_list);
3091 isl_union_map_free(executed);
3092 isl_ast_build_free(build);
3094 return list;
3097 /* Generate code for a single component, after shifting (if any)
3098 * has been applied, in case the schedule was specified as a schedule tree
3099 * and the separate option was specified.
3101 * We perform separation on the domain of "executed" and then generate
3102 * an AST for each of the resulting disjoint basic sets.
3104 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3105 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3107 isl_space *space;
3108 isl_set *domain;
3109 isl_basic_set_list *domain_list;
3110 isl_ast_graft_list *list;
3112 space = isl_ast_build_get_space(build, 1);
3113 domain = separate_schedule_domains(space,
3114 isl_union_map_copy(executed), build);
3115 domain_list = isl_basic_set_list_from_set(domain);
3117 list = generate_parallel_domains(domain_list, executed, build);
3119 isl_basic_set_list_free(domain_list);
3120 isl_union_map_free(executed);
3121 isl_ast_build_free(build);
3123 return list;
3126 /* Internal data structure for generate_shifted_component_tree_unroll.
3128 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3129 * "list" collects the constructs grafts.
3131 struct isl_ast_unroll_tree_data {
3132 isl_union_map *executed;
3133 isl_ast_build *build;
3134 isl_ast_graft_list *list;
3137 /* Initialize data->list to a list of "n" elements.
3139 static int init_unroll_tree(int n, void *user)
3141 struct isl_ast_unroll_tree_data *data = user;
3142 isl_ctx *ctx;
3144 ctx = isl_ast_build_get_ctx(data->build);
3145 data->list = isl_ast_graft_list_alloc(ctx, n);
3147 return 0;
3150 /* Given an iteration of an unrolled domain represented by "bset",
3151 * generate the corresponding AST and add the result to data->list.
3153 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3155 struct isl_ast_unroll_tree_data *data = user;
3157 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3158 bset, isl_ast_build_copy(data->build));
3160 return 0;
3163 /* Generate code for a single component, after shifting (if any)
3164 * has been applied, in case the schedule was specified as a schedule tree
3165 * and the unroll option was specified.
3167 * We call foreach_iteration to iterate over the individual values and
3168 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3170 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3171 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3172 __isl_take isl_ast_build *build)
3174 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3176 if (foreach_iteration(domain, build, &init_unroll_tree,
3177 &do_unroll_tree_iteration, &data) < 0)
3178 data.list = isl_ast_graft_list_free(data.list);
3180 isl_union_map_free(executed);
3181 isl_ast_build_free(build);
3183 return data.list;
3186 /* Generate code for a single component, after shifting (if any)
3187 * has been applied, in case the schedule was specified as a schedule tree.
3188 * In particular, handle the base case where there is either no isolated
3189 * set or we are within the isolated set (in which case "isolated" is set)
3190 * or the iterations that precede or follow the isolated set.
3192 * The schedule domain is broken up or combined into basic sets
3193 * according to the AST generation option specified in the current
3194 * schedule node, which may be either atomic, separate, unroll or
3195 * unspecified. If the option is unspecified, then we currently simply
3196 * split the schedule domain into disjoint basic sets.
3198 * In case the separate option is specified, the AST generation is
3199 * handled by generate_shifted_component_tree_separate.
3200 * In the other cases, we need the global schedule domain.
3201 * In the unroll case, the AST generation is then handled by
3202 * generate_shifted_component_tree_unroll which needs the actual
3203 * schedule domain (with divs that may refer to the current dimension)
3204 * so that stride detection can be performed.
3205 * In the atomic or unspecified case, inner dimensions and divs involving
3206 * the current dimensions should be eliminated.
3207 * The result is then either combined into a single basic set or
3208 * split up into disjoint basic sets.
3209 * Finally an AST is generated for each basic set and the results are
3210 * concatenated.
3212 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3213 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3214 int isolated)
3216 isl_union_set *schedule_domain;
3217 isl_set *domain;
3218 isl_basic_set_list *domain_list;
3219 isl_ast_graft_list *list;
3220 enum isl_ast_loop_type type;
3222 type = isl_ast_build_get_loop_type(build, isolated);
3223 if (type < 0)
3224 goto error;
3226 if (type == isl_ast_loop_separate)
3227 return generate_shifted_component_tree_separate(executed,
3228 build);
3230 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3231 domain = isl_set_from_union_set(schedule_domain);
3233 if (type == isl_ast_loop_unroll)
3234 return generate_shifted_component_tree_unroll(executed, domain,
3235 build);
3237 domain = isl_ast_build_eliminate(build, domain);
3238 domain = isl_set_coalesce(domain);
3240 if (type == isl_ast_loop_atomic) {
3241 isl_basic_set *hull;
3242 hull = isl_set_unshifted_simple_hull(domain);
3243 domain_list = isl_basic_set_list_from_basic_set(hull);
3244 } else {
3245 domain = isl_set_make_disjoint(domain);
3246 domain_list = isl_basic_set_list_from_set(domain);
3249 list = generate_parallel_domains(domain_list, executed, build);
3251 isl_basic_set_list_free(domain_list);
3252 isl_union_map_free(executed);
3253 isl_ast_build_free(build);
3255 return list;
3256 error:
3257 isl_union_map_free(executed);
3258 isl_ast_build_free(build);
3259 return NULL;
3262 /* Generate code for a single component, after shifting (if any)
3263 * has been applied, in case the schedule was specified as a schedule tree.
3264 * In particular, do so for the specified subset of the schedule domsain.
3266 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3267 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3268 __isl_keep isl_ast_build *build, int isolated)
3270 isl_union_set *uset;
3271 int empty;
3273 uset = isl_union_set_from_set(domain);
3274 executed = isl_union_map_copy(executed);
3275 executed = isl_union_map_intersect_domain(executed, uset);
3276 empty = isl_union_map_is_empty(executed);
3277 if (empty < 0)
3278 goto error;
3279 if (empty) {
3280 isl_ctx *ctx;
3281 isl_union_map_free(executed);
3282 ctx = isl_ast_build_get_ctx(build);
3283 return isl_ast_graft_list_alloc(ctx, 0);
3286 build = isl_ast_build_copy(build);
3287 return generate_shifted_component_tree_base(executed, build, isolated);
3288 error:
3289 isl_union_map_free(executed);
3290 return NULL;
3293 /* Generate code for a single component, after shifting (if any)
3294 * has been applied, in case the schedule was specified as a schedule tree.
3296 * We first check if the user has specified a (non-empty) isolated
3297 * schedule domain.
3298 * If so, we break up the schedule domain into iterations that
3299 * precede the isolated domain, the isolated domain itself,
3300 * the iterations that follow the isolated domain and
3301 * the remaining iterations (those that are incomparable
3302 * to the isolated domain).
3303 * We generate an AST for each piece and concatenate the results.
3304 * If no isolated set has been specified, then we generate an
3305 * AST for the entire inverse schedule.
3307 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3308 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3310 int i, depth;
3311 int empty, has_isolate;
3312 isl_space *space;
3313 isl_union_set *schedule_domain;
3314 isl_set *domain;
3315 isl_basic_set *hull;
3316 isl_set *isolated, *before, *after;
3317 isl_map *gt, *lt;
3318 isl_ast_graft_list *list, *res;
3320 build = isl_ast_build_extract_isolated(build);
3321 has_isolate = isl_ast_build_has_isolated(build);
3322 if (has_isolate < 0)
3323 executed = isl_union_map_free(executed);
3324 else if (!has_isolate)
3325 return generate_shifted_component_tree_base(executed, build, 0);
3327 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3328 domain = isl_set_from_union_set(schedule_domain);
3330 isolated = isl_ast_build_get_isolated(build);
3331 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3332 empty = isl_set_is_empty(isolated);
3333 if (empty < 0)
3334 goto error;
3335 if (empty) {
3336 isl_set_free(isolated);
3337 isl_set_free(domain);
3338 return generate_shifted_component_tree_base(executed, build, 0);
3340 isolated = isl_ast_build_eliminate(build, isolated);
3341 hull = isl_set_unshifted_simple_hull(isolated);
3342 isolated = isl_set_from_basic_set(hull);
3344 depth = isl_ast_build_get_depth(build);
3345 space = isl_space_map_from_set(isl_set_get_space(isolated));
3346 gt = isl_map_universe(space);
3347 for (i = 0; i < depth; ++i)
3348 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3349 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3350 lt = isl_map_reverse(isl_map_copy(gt));
3351 before = isl_set_apply(isl_set_copy(isolated), gt);
3352 after = isl_set_apply(isl_set_copy(isolated), lt);
3354 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3355 domain = isl_set_subtract(domain, isl_set_copy(before));
3356 domain = isl_set_subtract(domain, isl_set_copy(after));
3357 after = isl_set_subtract(after, isl_set_copy(isolated));
3358 after = isl_set_subtract(after, isl_set_copy(before));
3359 before = isl_set_subtract(before, isl_set_copy(isolated));
3361 res = generate_shifted_component_tree_part(executed, before, build, 0);
3362 list = generate_shifted_component_tree_part(executed, isolated,
3363 build, 1);
3364 res = isl_ast_graft_list_concat(res, list);
3365 list = generate_shifted_component_tree_part(executed, after, build, 0);
3366 res = isl_ast_graft_list_concat(res, list);
3367 list = generate_shifted_component_tree_part(executed, domain, build, 0);
3368 res = isl_ast_graft_list_concat(res, list);
3370 isl_union_map_free(executed);
3371 isl_ast_build_free(build);
3373 return res;
3374 error:
3375 isl_set_free(domain);
3376 isl_set_free(isolated);
3377 isl_union_map_free(executed);
3378 isl_ast_build_free(build);
3379 return NULL;
3382 /* Generate code for a single component, after shifting (if any)
3383 * has been applied.
3385 * Call generate_shifted_component_tree or generate_shifted_component_flat
3386 * depending on whether the schedule was specified as a schedule tree.
3388 static __isl_give isl_ast_graft_list *generate_shifted_component(
3389 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3391 if (isl_ast_build_has_schedule_node(build))
3392 return generate_shifted_component_tree(executed, build);
3393 else
3394 return generate_shifted_component_flat(executed, build);
3397 struct isl_set_map_pair {
3398 isl_set *set;
3399 isl_map *map;
3402 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3403 * of indices into the "domain" array,
3404 * return the union of the "map" fields of the elements
3405 * indexed by the first "n" elements of "order".
3407 static __isl_give isl_union_map *construct_component_executed(
3408 struct isl_set_map_pair *domain, int *order, int n)
3410 int i;
3411 isl_map *map;
3412 isl_union_map *executed;
3414 map = isl_map_copy(domain[order[0]].map);
3415 executed = isl_union_map_from_map(map);
3416 for (i = 1; i < n; ++i) {
3417 map = isl_map_copy(domain[order[i]].map);
3418 executed = isl_union_map_add_map(executed, map);
3421 return executed;
3424 /* Generate code for a single component, after shifting (if any)
3425 * has been applied.
3427 * The component inverse schedule is specified as the "map" fields
3428 * of the elements of "domain" indexed by the first "n" elements of "order".
3430 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3431 struct isl_set_map_pair *domain, int *order, int n,
3432 __isl_take isl_ast_build *build)
3434 isl_union_map *executed;
3436 executed = construct_component_executed(domain, order, n);
3437 return generate_shifted_component(executed, build);
3440 /* Does set dimension "pos" of "set" have an obviously fixed value?
3442 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3444 int fixed;
3445 isl_val *v;
3447 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3448 if (!v)
3449 return -1;
3450 fixed = !isl_val_is_nan(v);
3451 isl_val_free(v);
3453 return fixed;
3456 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3457 * of indices into the "domain" array,
3458 * do all (except for at most one) of the "set" field of the elements
3459 * indexed by the first "n" elements of "order" have a fixed value
3460 * at position "depth"?
3462 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3463 int *order, int n, int depth)
3465 int i;
3466 int non_fixed = -1;
3468 for (i = 0; i < n; ++i) {
3469 int f;
3471 f = dim_is_fixed(domain[order[i]].set, depth);
3472 if (f < 0)
3473 return -1;
3474 if (f)
3475 continue;
3476 if (non_fixed >= 0)
3477 return 0;
3478 non_fixed = i;
3481 return 1;
3484 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3485 * of indices into the "domain" array,
3486 * eliminate the inner dimensions from the "set" field of the elements
3487 * indexed by the first "n" elements of "order", provided the current
3488 * dimension does not have a fixed value.
3490 * Return the index of the first element in "order" with a corresponding
3491 * "set" field that does not have an (obviously) fixed value.
3493 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3494 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3496 int i;
3497 int base = -1;
3499 for (i = n - 1; i >= 0; --i) {
3500 int f;
3501 f = dim_is_fixed(domain[order[i]].set, depth);
3502 if (f < 0)
3503 return -1;
3504 if (f)
3505 continue;
3506 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3507 domain[order[i]].set);
3508 base = i;
3511 return base;
3514 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3515 * of indices into the "domain" array,
3516 * find the element of "domain" (amongst those indexed by the first "n"
3517 * elements of "order") with the "set" field that has the smallest
3518 * value for the current iterator.
3520 * Note that the domain with the smallest value may depend on the parameters
3521 * and/or outer loop dimension. Since the result of this function is only
3522 * used as heuristic, we only make a reasonable attempt at finding the best
3523 * domain, one that should work in case a single domain provides the smallest
3524 * value for the current dimension over all values of the parameters
3525 * and outer dimensions.
3527 * In particular, we compute the smallest value of the first domain
3528 * and replace it by that of any later domain if that later domain
3529 * has a smallest value that is smaller for at least some value
3530 * of the parameters and outer dimensions.
3532 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3533 __isl_keep isl_ast_build *build)
3535 int i;
3536 isl_map *min_first;
3537 int first = 0;
3539 min_first = isl_ast_build_map_to_iterator(build,
3540 isl_set_copy(domain[order[0]].set));
3541 min_first = isl_map_lexmin(min_first);
3543 for (i = 1; i < n; ++i) {
3544 isl_map *min, *test;
3545 int empty;
3547 min = isl_ast_build_map_to_iterator(build,
3548 isl_set_copy(domain[order[i]].set));
3549 min = isl_map_lexmin(min);
3550 test = isl_map_copy(min);
3551 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3552 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3553 empty = isl_map_is_empty(test);
3554 isl_map_free(test);
3555 if (empty >= 0 && !empty) {
3556 isl_map_free(min_first);
3557 first = i;
3558 min_first = min;
3559 } else
3560 isl_map_free(min);
3562 if (empty < 0)
3563 break;
3566 isl_map_free(min_first);
3568 return i < n ? -1 : first;
3571 /* Construct a shifted inverse schedule based on the original inverse schedule,
3572 * the stride and the offset.
3574 * The original inverse schedule is specified as the "map" fields
3575 * of the elements of "domain" indexed by the first "n" elements of "order".
3577 * "stride" and "offset" are such that the difference
3578 * between the values of the current dimension of domain "i"
3579 * and the values of the current dimension for some reference domain are
3580 * equal to
3582 * stride * integer + offset[i]
3584 * Moreover, 0 <= offset[i] < stride.
3586 * For each domain, we create a map
3588 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3590 * where j refers to the current dimension and the other dimensions are
3591 * unchanged, and apply this map to the original schedule domain.
3593 * For example, for the original schedule
3595 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3597 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3598 * we apply the mapping
3600 * { [j] -> [j, 0] }
3602 * to the schedule of the "A" domain and the mapping
3604 * { [j - 1] -> [j, 1] }
3606 * to the schedule of the "B" domain.
3609 * Note that after the transformation, the differences between pairs
3610 * of values of the current dimension over all domains are multiples
3611 * of stride and that we have therefore exposed the stride.
3614 * To see that the mapping preserves the lexicographic order,
3615 * first note that each of the individual maps above preserves the order.
3616 * If the value of the current iterator is j1 in one domain and j2 in another,
3617 * then if j1 = j2, we know that the same map is applied to both domains
3618 * and the order is preserved.
3619 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3620 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3622 * j1 - c1 < j2 - c2
3624 * and the order is preserved.
3625 * If c1 < c2, then we know
3627 * 0 <= c2 - c1 < s
3629 * We also have
3631 * j2 - j1 = n * s + r
3633 * with n >= 0 and 0 <= r < s.
3634 * In other words, r = c2 - c1.
3635 * If n > 0, then
3637 * j1 - c1 < j2 - c2
3639 * If n = 0, then
3641 * j1 - c1 = j2 - c2
3643 * and so
3645 * (j1 - c1, c1) << (j2 - c2, c2)
3647 * with "<<" the lexicographic order, proving that the order is preserved
3648 * in all cases.
3650 static __isl_give isl_union_map *contruct_shifted_executed(
3651 struct isl_set_map_pair *domain, int *order, int n,
3652 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3653 __isl_take isl_ast_build *build)
3655 int i;
3656 isl_union_map *executed;
3657 isl_space *space;
3658 isl_map *map;
3659 int depth;
3660 isl_constraint *c;
3662 depth = isl_ast_build_get_depth(build);
3663 space = isl_ast_build_get_space(build, 1);
3664 executed = isl_union_map_empty(isl_space_copy(space));
3665 space = isl_space_map_from_set(space);
3666 map = isl_map_identity(isl_space_copy(space));
3667 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3668 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3669 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3671 c = isl_equality_alloc(isl_local_space_from_space(space));
3672 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3673 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3675 for (i = 0; i < n; ++i) {
3676 isl_map *map_i;
3677 isl_val *v;
3679 v = isl_multi_val_get_val(offset, i);
3680 if (!v)
3681 break;
3682 map_i = isl_map_copy(map);
3683 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3684 isl_val_copy(v));
3685 v = isl_val_neg(v);
3686 c = isl_constraint_set_constant_val(c, v);
3687 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3689 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3690 map_i);
3691 executed = isl_union_map_add_map(executed, map_i);
3694 isl_constraint_free(c);
3695 isl_map_free(map);
3697 if (i < n)
3698 executed = isl_union_map_free(executed);
3700 return executed;
3703 /* Generate code for a single component, after exposing the stride,
3704 * given that the schedule domain is "shifted strided".
3706 * The component inverse schedule is specified as the "map" fields
3707 * of the elements of "domain" indexed by the first "n" elements of "order".
3709 * The schedule domain being "shifted strided" means that the differences
3710 * between the values of the current dimension of domain "i"
3711 * and the values of the current dimension for some reference domain are
3712 * equal to
3714 * stride * integer + offset[i]
3716 * We first look for the domain with the "smallest" value for the current
3717 * dimension and adjust the offsets such that the offset of the "smallest"
3718 * domain is equal to zero. The other offsets are reduced modulo stride.
3720 * Based on this information, we construct a new inverse schedule in
3721 * contruct_shifted_executed that exposes the stride.
3722 * Since this involves the introduction of a new schedule dimension,
3723 * the build needs to be changed accodingly.
3724 * After computing the AST, the newly introduced dimension needs
3725 * to be removed again from the list of grafts. We do this by plugging
3726 * in a mapping that represents the new schedule domain in terms of the
3727 * old schedule domain.
3729 static __isl_give isl_ast_graft_list *generate_shift_component(
3730 struct isl_set_map_pair *domain, int *order, int n,
3731 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3732 __isl_take isl_ast_build *build)
3734 isl_ast_graft_list *list;
3735 int first;
3736 int depth;
3737 isl_val *val;
3738 isl_multi_val *mv;
3739 isl_space *space;
3740 isl_multi_aff *ma, *zero;
3741 isl_union_map *executed;
3743 depth = isl_ast_build_get_depth(build);
3745 first = first_offset(domain, order, n, build);
3746 if (first < 0)
3747 goto error;
3749 mv = isl_multi_val_copy(offset);
3750 val = isl_multi_val_get_val(offset, first);
3751 val = isl_val_neg(val);
3752 mv = isl_multi_val_add_val(mv, val);
3753 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3755 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3756 build);
3757 space = isl_ast_build_get_space(build, 1);
3758 space = isl_space_map_from_set(space);
3759 ma = isl_multi_aff_identity(isl_space_copy(space));
3760 space = isl_space_from_domain(isl_space_domain(space));
3761 space = isl_space_add_dims(space, isl_dim_out, 1);
3762 zero = isl_multi_aff_zero(space);
3763 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3764 build = isl_ast_build_insert_dim(build, depth + 1);
3765 list = generate_shifted_component(executed, build);
3767 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3769 isl_multi_val_free(mv);
3771 return list;
3772 error:
3773 isl_ast_build_free(build);
3774 return NULL;
3777 /* Does any node in the schedule tree rooted at the current schedule node
3778 * of "build" depend on outer schedule nodes?
3780 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
3782 isl_schedule_node *node;
3783 int dependent = 0;
3785 node = isl_ast_build_get_schedule_node(build);
3786 dependent = isl_schedule_node_is_subtree_anchored(node);
3787 isl_schedule_node_free(node);
3789 return dependent;
3792 /* Generate code for a single component.
3794 * The component inverse schedule is specified as the "map" fields
3795 * of the elements of "domain" indexed by the first "n" elements of "order".
3797 * This function may modify the "set" fields of "domain".
3799 * Before proceeding with the actual code generation for the component,
3800 * we first check if there are any "shifted" strides, meaning that
3801 * the schedule domains of the individual domains are all strided,
3802 * but that they have different offsets, resulting in the union
3803 * of schedule domains not being strided anymore.
3805 * The simplest example is the schedule
3807 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3809 * Both schedule domains are strided, but their union is not.
3810 * This function detects such cases and then rewrites the schedule to
3812 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3814 * In the new schedule, the schedule domains have the same offset (modulo
3815 * the stride), ensuring that the union of schedule domains is also strided.
3818 * If there is only a single domain in the component, then there is
3819 * nothing to do. Similarly, if the current schedule dimension has
3820 * a fixed value for almost all domains then there is nothing to be done.
3821 * In particular, we need at least two domains where the current schedule
3822 * dimension does not have a fixed value.
3823 * Finally, in case of a schedule map input,
3824 * if any of the options refer to the current schedule dimension,
3825 * then we bail out as well. It would be possible to reformulate the options
3826 * in terms of the new schedule domain, but that would introduce constraints
3827 * that separate the domains in the options and that is something we would
3828 * like to avoid.
3829 * In the case of a schedule tree input, we bail out if any of
3830 * the descendants of the current schedule node refer to outer
3831 * schedule nodes in any way.
3834 * To see if there is any shifted stride, we look at the differences
3835 * between the values of the current dimension in pairs of domains
3836 * for equal values of outer dimensions. These differences should be
3837 * of the form
3839 * m x + r
3841 * with "m" the stride and "r" a constant. Note that we cannot perform
3842 * this analysis on individual domains as the lower bound in each domain
3843 * may depend on parameters or outer dimensions and so the current dimension
3844 * itself may not have a fixed remainder on division by the stride.
3846 * In particular, we compare the first domain that does not have an
3847 * obviously fixed value for the current dimension to itself and all
3848 * other domains and collect the offsets and the gcd of the strides.
3849 * If the gcd becomes one, then we failed to find shifted strides.
3850 * If the gcd is zero, then the differences were all fixed, meaning
3851 * that some domains had non-obviously fixed values for the current dimension.
3852 * If all the offsets are the same (for those domains that do not have
3853 * an obviously fixed value for the current dimension), then we do not
3854 * apply the transformation.
3855 * If none of the domains were skipped, then there is nothing to do.
3856 * If some of them were skipped, then if we apply separation, the schedule
3857 * domain should get split in pieces with a (non-shifted) stride.
3859 * Otherwise, we apply a shift to expose the stride in
3860 * generate_shift_component.
3862 static __isl_give isl_ast_graft_list *generate_component(
3863 struct isl_set_map_pair *domain, int *order, int n,
3864 __isl_take isl_ast_build *build)
3866 int i, d;
3867 int depth;
3868 isl_ctx *ctx;
3869 isl_map *map;
3870 isl_set *deltas;
3871 isl_val *gcd = NULL;
3872 isl_multi_val *mv;
3873 int fixed, skip;
3874 int base;
3875 isl_ast_graft_list *list;
3876 int res = 0;
3878 depth = isl_ast_build_get_depth(build);
3880 skip = n == 1;
3881 if (skip >= 0 && !skip)
3882 skip = at_most_one_non_fixed(domain, order, n, depth);
3883 if (skip >= 0 && !skip) {
3884 if (isl_ast_build_has_schedule_node(build))
3885 skip = has_anchored_subtree(build);
3886 else
3887 skip = isl_ast_build_options_involve_depth(build);
3889 if (skip < 0)
3890 goto error;
3891 if (skip)
3892 return generate_shifted_component_from_list(domain,
3893 order, n, build);
3895 base = eliminate_non_fixed(domain, order, n, depth, build);
3896 if (base < 0)
3897 goto error;
3899 ctx = isl_ast_build_get_ctx(build);
3901 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3903 fixed = 1;
3904 for (i = 0; i < n; ++i) {
3905 isl_val *r, *m;
3907 map = isl_map_from_domain_and_range(
3908 isl_set_copy(domain[order[base]].set),
3909 isl_set_copy(domain[order[i]].set));
3910 for (d = 0; d < depth; ++d)
3911 map = isl_map_equate(map, isl_dim_in, d,
3912 isl_dim_out, d);
3913 deltas = isl_map_deltas(map);
3914 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3915 isl_set_free(deltas);
3916 if (res < 0)
3917 break;
3919 if (i == 0)
3920 gcd = m;
3921 else
3922 gcd = isl_val_gcd(gcd, m);
3923 if (isl_val_is_one(gcd)) {
3924 isl_val_free(r);
3925 break;
3927 mv = isl_multi_val_set_val(mv, i, r);
3929 res = dim_is_fixed(domain[order[i]].set, depth);
3930 if (res < 0)
3931 break;
3932 if (res)
3933 continue;
3935 if (fixed && i > base) {
3936 isl_val *a, *b;
3937 a = isl_multi_val_get_val(mv, i);
3938 b = isl_multi_val_get_val(mv, base);
3939 if (isl_val_ne(a, b))
3940 fixed = 0;
3941 isl_val_free(a);
3942 isl_val_free(b);
3946 if (res < 0 || !gcd) {
3947 isl_ast_build_free(build);
3948 list = NULL;
3949 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3950 list = generate_shifted_component_from_list(domain,
3951 order, n, build);
3952 } else {
3953 list = generate_shift_component(domain, order, n, gcd, mv,
3954 build);
3957 isl_val_free(gcd);
3958 isl_multi_val_free(mv);
3960 return list;
3961 error:
3962 isl_ast_build_free(build);
3963 return NULL;
3966 /* Store both "map" itself and its domain in the
3967 * structure pointed to by *next and advance to the next array element.
3969 static int extract_domain(__isl_take isl_map *map, void *user)
3971 struct isl_set_map_pair **next = user;
3973 (*next)->map = isl_map_copy(map);
3974 (*next)->set = isl_map_domain(map);
3975 (*next)++;
3977 return 0;
3980 static int after_in_tree(__isl_keep isl_union_map *umap,
3981 __isl_keep isl_schedule_node *node);
3983 /* Is any domain element of "umap" scheduled after any of
3984 * the corresponding image elements by the tree rooted at
3985 * the child of "node"?
3987 static int after_in_child(__isl_keep isl_union_map *umap,
3988 __isl_keep isl_schedule_node *node)
3990 isl_schedule_node *child;
3991 int after;
3993 child = isl_schedule_node_get_child(node, 0);
3994 after = after_in_tree(umap, child);
3995 isl_schedule_node_free(child);
3997 return after;
4000 /* Is any domain element of "umap" scheduled after any of
4001 * the corresponding image elements by the tree rooted at
4002 * the band node "node"?
4004 * We first check if any domain element is scheduled after any
4005 * of the corresponding image elements by the band node itself.
4006 * If not, we restrict "map" to those pairs of element that
4007 * are scheduled together by the band node and continue with
4008 * the child of the band node.
4009 * If there are no such pairs then the map passed to after_in_child
4010 * will be empty causing it to return 0.
4012 static int after_in_band(__isl_keep isl_union_map *umap,
4013 __isl_keep isl_schedule_node *node)
4015 isl_multi_union_pw_aff *mupa;
4016 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4017 isl_union_set *domain, *range;
4018 isl_space *space;
4019 int empty;
4020 int after;
4022 if (isl_schedule_node_band_n_member(node) == 0)
4023 return after_in_child(umap, node);
4025 mupa = isl_schedule_node_band_get_partial_schedule(node);
4026 space = isl_multi_union_pw_aff_get_space(mupa);
4027 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4028 test = isl_union_map_copy(umap);
4029 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4030 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4031 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4032 test = isl_union_map_intersect(test, gt);
4033 empty = isl_union_map_is_empty(test);
4034 isl_union_map_free(test);
4036 if (empty < 0 || !empty) {
4037 isl_union_map_free(partial);
4038 return empty < 0 ? -1 : 1;
4041 universe = isl_union_map_universe(isl_union_map_copy(umap));
4042 domain = isl_union_map_domain(isl_union_map_copy(universe));
4043 range = isl_union_map_range(universe);
4044 umap1 = isl_union_map_copy(partial);
4045 umap1 = isl_union_map_intersect_domain(umap1, domain);
4046 umap2 = isl_union_map_intersect_domain(partial, range);
4047 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4048 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4049 after = after_in_child(test, node);
4050 isl_union_map_free(test);
4051 return after;
4054 /* Is any domain element of "umap" scheduled after any of
4055 * the corresponding image elements by the tree rooted at
4056 * the context node "node"?
4058 * The context constraints apply to the schedule domain,
4059 * so we cannot apply them directly to "umap", which contains
4060 * pairs of statement instances. Instead, we add them
4061 * to the range of the prefix schedule for both domain and
4062 * range of "umap".
4064 static int after_in_context(__isl_keep isl_union_map *umap,
4065 __isl_keep isl_schedule_node *node)
4067 isl_union_map *prefix, *universe, *umap1, *umap2;
4068 isl_union_set *domain, *range;
4069 isl_set *context;
4070 int after;
4072 umap = isl_union_map_copy(umap);
4073 context = isl_schedule_node_context_get_context(node);
4074 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4075 universe = isl_union_map_universe(isl_union_map_copy(umap));
4076 domain = isl_union_map_domain(isl_union_map_copy(universe));
4077 range = isl_union_map_range(universe);
4078 umap1 = isl_union_map_copy(prefix);
4079 umap1 = isl_union_map_intersect_domain(umap1, domain);
4080 umap2 = isl_union_map_intersect_domain(prefix, range);
4081 umap1 = isl_union_map_intersect_range(umap1,
4082 isl_union_set_from_set(context));
4083 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4084 umap = isl_union_map_intersect(umap, umap1);
4086 after = after_in_child(umap, node);
4088 isl_union_map_free(umap);
4090 return after;
4093 /* Is any domain element of "umap" scheduled after any of
4094 * the corresponding image elements by the tree rooted at
4095 * the filter node "node"?
4097 * We intersect domain and range of "umap" with the filter and
4098 * continue with its child.
4100 static int after_in_filter(__isl_keep isl_union_map *umap,
4101 __isl_keep isl_schedule_node *node)
4103 isl_union_set *filter;
4104 int after;
4106 umap = isl_union_map_copy(umap);
4107 filter = isl_schedule_node_filter_get_filter(node);
4108 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4109 umap = isl_union_map_intersect_range(umap, filter);
4111 after = after_in_child(umap, node);
4113 isl_union_map_free(umap);
4115 return after;
4118 /* Is any domain element of "umap" scheduled after any of
4119 * the corresponding image elements by the tree rooted at
4120 * the set node "node"?
4122 * This is only the case if this condition holds in any
4123 * of the (filter) children of the set node.
4124 * In particular, if the domain and the range of "umap"
4125 * are contained in different children, then the condition
4126 * does not hold.
4128 static int after_in_set(__isl_keep isl_union_map *umap,
4129 __isl_keep isl_schedule_node *node)
4131 int i, n;
4133 n = isl_schedule_node_n_children(node);
4134 for (i = 0; i < n; ++i) {
4135 isl_schedule_node *child;
4136 int after;
4138 child = isl_schedule_node_get_child(node, i);
4139 after = after_in_tree(umap, child);
4140 isl_schedule_node_free(child);
4142 if (after < 0 || after)
4143 return after;
4146 return 0;
4149 /* Return the filter of child "i" of "node".
4151 static __isl_give isl_union_set *child_filter(
4152 __isl_keep isl_schedule_node *node, int i)
4154 isl_schedule_node *child;
4155 isl_union_set *filter;
4157 child = isl_schedule_node_get_child(node, i);
4158 filter = isl_schedule_node_filter_get_filter(child);
4159 isl_schedule_node_free(child);
4161 return filter;
4164 /* Is any domain element of "umap" scheduled after any of
4165 * the corresponding image elements by the tree rooted at
4166 * the sequence node "node"?
4168 * This happens in particular if any domain element is
4169 * contained in a later child than one containing a range element or
4170 * if the condition holds within a given child in the sequence.
4171 * The later part of the condition is checked by after_in_set.
4173 static int after_in_sequence(__isl_keep isl_union_map *umap,
4174 __isl_keep isl_schedule_node *node)
4176 int i, j, n;
4177 isl_union_map *umap_i;
4178 int empty, after = 0;
4180 n = isl_schedule_node_n_children(node);
4181 for (i = 1; i < n; ++i) {
4182 isl_union_set *filter_i;
4184 umap_i = isl_union_map_copy(umap);
4185 filter_i = child_filter(node, i);
4186 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4187 empty = isl_union_map_is_empty(umap_i);
4188 if (empty < 0)
4189 goto error;
4190 if (empty) {
4191 isl_union_map_free(umap_i);
4192 continue;
4195 for (j = 0; j < i; ++j) {
4196 isl_union_set *filter_j;
4197 isl_union_map *umap_ij;
4199 umap_ij = isl_union_map_copy(umap_i);
4200 filter_j = child_filter(node, j);
4201 umap_ij = isl_union_map_intersect_range(umap_ij,
4202 filter_j);
4203 empty = isl_union_map_is_empty(umap_ij);
4204 isl_union_map_free(umap_ij);
4206 if (empty < 0)
4207 goto error;
4208 if (!empty)
4209 after = 1;
4210 if (after)
4211 break;
4214 isl_union_map_free(umap_i);
4215 if (after)
4216 break;
4219 if (after < 0 || after)
4220 return after;
4222 return after_in_set(umap, node);
4223 error:
4224 isl_union_map_free(umap_i);
4225 return -1;
4228 /* Is any domain element of "umap" scheduled after any of
4229 * the corresponding image elements by the tree rooted at "node"?
4231 * If "umap" is empty, then clearly there is no such element.
4232 * Otherwise, consider the different types of nodes separately.
4234 static int after_in_tree(__isl_keep isl_union_map *umap,
4235 __isl_keep isl_schedule_node *node)
4237 int empty;
4238 enum isl_schedule_node_type type;
4240 empty = isl_union_map_is_empty(umap);
4241 if (empty < 0)
4242 return -1;
4243 if (empty)
4244 return 0;
4245 if (!node)
4246 return -1;
4248 type = isl_schedule_node_get_type(node);
4249 switch (type) {
4250 case isl_schedule_node_error:
4251 return -1;
4252 case isl_schedule_node_leaf:
4253 return 0;
4254 case isl_schedule_node_band:
4255 return after_in_band(umap, node);
4256 case isl_schedule_node_domain:
4257 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4258 "unexpected internal domain node", return -1);
4259 case isl_schedule_node_context:
4260 return after_in_context(umap, node);
4261 case isl_schedule_node_filter:
4262 return after_in_filter(umap, node);
4263 case isl_schedule_node_set:
4264 return after_in_set(umap, node);
4265 case isl_schedule_node_sequence:
4266 return after_in_sequence(umap, node);
4269 return 1;
4272 /* Is any domain element of "map1" scheduled after any domain
4273 * element of "map2" by the subtree underneath the current band node,
4274 * while at the same time being scheduled together by the current
4275 * band node, i.e., by "map1" and "map2?
4277 * If the child of the current band node is a leaf, then
4278 * no element can be scheduled after any other element.
4280 * Otherwise, we construct a relation between domain elements
4281 * of "map1" and domain elements of "map2" that are scheduled
4282 * together and then check if the subtree underneath the current
4283 * band node determines their relative order.
4285 static int after_in_subtree(__isl_keep isl_ast_build *build,
4286 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4288 isl_schedule_node *node;
4289 isl_map *map;
4290 isl_union_map *umap;
4291 int after;
4293 node = isl_ast_build_get_schedule_node(build);
4294 if (!node)
4295 return -1;
4296 node = isl_schedule_node_child(node, 0);
4297 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4298 isl_schedule_node_free(node);
4299 return 0;
4301 map = isl_map_copy(map2);
4302 map = isl_map_apply_domain(map, isl_map_copy(map1));
4303 umap = isl_union_map_from_map(map);
4304 after = after_in_tree(umap, node);
4305 isl_union_map_free(umap);
4306 isl_schedule_node_free(node);
4307 return after;
4310 /* Internal data for any_scheduled_after.
4312 * "build" is the build in which the AST is constructed.
4313 * "depth" is the number of loops that have already been generated
4314 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4315 * "domain" is an array of set-map pairs corresponding to the different
4316 * iteration domains. The set is the schedule domain, i.e., the domain
4317 * of the inverse schedule, while the map is the inverse schedule itself.
4319 struct isl_any_scheduled_after_data {
4320 isl_ast_build *build;
4321 int depth;
4322 int group_coscheduled;
4323 struct isl_set_map_pair *domain;
4326 /* Is any element of domain "i" scheduled after any element of domain "j"
4327 * (for a common iteration of the first data->depth loops)?
4329 * data->domain[i].set contains the domain of the inverse schedule
4330 * for domain "i", i.e., elements in the schedule domain.
4332 * If we are inside a band of a schedule tree and there is a pair
4333 * of elements in the two domains that is schedule together by
4334 * the current band, then we check if any element of "i" may be schedule
4335 * after element of "j" by the descendants of the band node.
4337 * If data->group_coscheduled is set, then we also return 1 if there
4338 * is any pair of elements in the two domains that are scheduled together.
4340 static int any_scheduled_after(int i, int j, void *user)
4342 struct isl_any_scheduled_after_data *data = user;
4343 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4344 int pos;
4346 for (pos = data->depth; pos < dim; ++pos) {
4347 int follows;
4349 follows = isl_set_follows_at(data->domain[i].set,
4350 data->domain[j].set, pos);
4352 if (follows < -1)
4353 return -1;
4354 if (follows > 0)
4355 return 1;
4356 if (follows < 0)
4357 return 0;
4360 if (isl_ast_build_has_schedule_node(data->build)) {
4361 int after;
4363 after = after_in_subtree(data->build, data->domain[i].map,
4364 data->domain[j].map);
4365 if (after < 0 || after)
4366 return after;
4369 return data->group_coscheduled;
4372 /* Look for independent components at the current depth and generate code
4373 * for each component separately. The resulting lists of grafts are
4374 * merged in an attempt to combine grafts with identical guards.
4376 * Code for two domains can be generated separately if all the elements
4377 * of one domain are scheduled before (or together with) all the elements
4378 * of the other domain. We therefore consider the graph with as nodes
4379 * the domains and an edge between two nodes if any element of the first
4380 * node is scheduled after any element of the second node.
4381 * If the ast_build_group_coscheduled is set, then we also add an edge if
4382 * there is any pair of elements in the two domains that are scheduled
4383 * together.
4384 * Code is then generated (by generate_component)
4385 * for each of the strongly connected components in this graph
4386 * in their topological order.
4388 * Since the test is performed on the domain of the inverse schedules of
4389 * the different domains, we precompute these domains and store
4390 * them in data.domain.
4392 static __isl_give isl_ast_graft_list *generate_components(
4393 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4395 int i;
4396 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4397 int n = isl_union_map_n_map(executed);
4398 struct isl_any_scheduled_after_data data;
4399 struct isl_set_map_pair *next;
4400 struct isl_tarjan_graph *g = NULL;
4401 isl_ast_graft_list *list = NULL;
4402 int n_domain = 0;
4404 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4405 if (!data.domain)
4406 goto error;
4407 n_domain = n;
4409 next = data.domain;
4410 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4411 goto error;
4413 if (!build)
4414 goto error;
4415 data.build = build;
4416 data.depth = isl_ast_build_get_depth(build);
4417 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4418 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4419 if (!g)
4420 goto error;
4422 list = isl_ast_graft_list_alloc(ctx, 0);
4424 i = 0;
4425 while (list && n) {
4426 isl_ast_graft_list *list_c;
4427 int first = i;
4429 if (g->order[i] == -1)
4430 isl_die(ctx, isl_error_internal, "cannot happen",
4431 goto error);
4432 ++i; --n;
4433 while (g->order[i] != -1) {
4434 ++i; --n;
4437 list_c = generate_component(data.domain,
4438 g->order + first, i - first,
4439 isl_ast_build_copy(build));
4440 list = isl_ast_graft_list_merge(list, list_c, build);
4442 ++i;
4445 if (0)
4446 error: list = isl_ast_graft_list_free(list);
4447 isl_tarjan_graph_free(g);
4448 for (i = 0; i < n_domain; ++i) {
4449 isl_map_free(data.domain[i].map);
4450 isl_set_free(data.domain[i].set);
4452 free(data.domain);
4453 isl_union_map_free(executed);
4454 isl_ast_build_free(build);
4456 return list;
4459 /* Generate code for the next level (and all inner levels).
4461 * If "executed" is empty, i.e., no code needs to be generated,
4462 * then we return an empty list.
4464 * If we have already generated code for all loop levels, then we pass
4465 * control to generate_inner_level.
4467 * If "executed" lives in a single space, i.e., if code needs to be
4468 * generated for a single domain, then there can only be a single
4469 * component and we go directly to generate_shifted_component.
4470 * Otherwise, we call generate_components to detect the components
4471 * and to call generate_component on each of them separately.
4473 static __isl_give isl_ast_graft_list *generate_next_level(
4474 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4476 int depth;
4478 if (!build || !executed)
4479 goto error;
4481 if (isl_union_map_is_empty(executed)) {
4482 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4483 isl_union_map_free(executed);
4484 isl_ast_build_free(build);
4485 return isl_ast_graft_list_alloc(ctx, 0);
4488 depth = isl_ast_build_get_depth(build);
4489 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4490 return generate_inner_level(executed, build);
4492 if (isl_union_map_n_map(executed) == 1)
4493 return generate_shifted_component(executed, build);
4495 return generate_components(executed, build);
4496 error:
4497 isl_union_map_free(executed);
4498 isl_ast_build_free(build);
4499 return NULL;
4502 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4503 * internal, executed and build are the inputs to generate_code.
4504 * list collects the output.
4506 struct isl_generate_code_data {
4507 int internal;
4508 isl_union_map *executed;
4509 isl_ast_build *build;
4511 isl_ast_graft_list *list;
4514 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4516 * [E -> S] -> D
4518 * with E the external build schedule and S the additional schedule "space",
4519 * reformulate the inverse schedule in terms of the internal schedule domain,
4520 * i.e., return
4522 * [I -> S] -> D
4524 * We first obtain a mapping
4526 * I -> E
4528 * take the inverse and the product with S -> S, resulting in
4530 * [I -> S] -> [E -> S]
4532 * Applying the map to the input produces the desired result.
4534 static __isl_give isl_union_map *internal_executed(
4535 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4536 __isl_keep isl_ast_build *build)
4538 isl_map *id, *proj;
4540 proj = isl_ast_build_get_schedule_map(build);
4541 proj = isl_map_reverse(proj);
4542 space = isl_space_map_from_set(isl_space_copy(space));
4543 id = isl_map_identity(space);
4544 proj = isl_map_product(proj, id);
4545 executed = isl_union_map_apply_domain(executed,
4546 isl_union_map_from_map(proj));
4547 return executed;
4550 /* Generate an AST that visits the elements in the range of data->executed
4551 * in the relative order specified by the corresponding domain element(s)
4552 * for those domain elements that belong to "set".
4553 * Add the result to data->list.
4555 * The caller ensures that "set" is a universe domain.
4556 * "space" is the space of the additional part of the schedule.
4557 * It is equal to the space of "set" if build->domain is parametric.
4558 * Otherwise, it is equal to the range of the wrapped space of "set".
4560 * If the build space is not parametric and
4561 * if isl_ast_build_node_from_schedule_map
4562 * was called from an outside user (data->internal not set), then
4563 * the (inverse) schedule refers to the external build domain and needs to
4564 * be transformed to refer to the internal build domain.
4566 * If the build space is parametric, then we add some of the parameter
4567 * constraints to the executed relation. Adding these constraints
4568 * allows for an earlier detection of conflicts in some cases.
4569 * However, we do not want to divide the executed relation into
4570 * more disjuncts than necessary. We therefore approximate
4571 * the constraints on the parameters by a single disjunct set.
4573 * The build is extended to include the additional part of the schedule.
4574 * If the original build space was not parametric, then the options
4575 * in data->build refer only to the additional part of the schedule
4576 * and they need to be adjusted to refer to the complete AST build
4577 * domain.
4579 * After having adjusted inverse schedule and build, we start generating
4580 * code with the outer loop of the current code generation
4581 * in generate_next_level.
4583 * If the original build space was not parametric, we undo the embedding
4584 * on the resulting isl_ast_node_list so that it can be used within
4585 * the outer AST build.
4587 static int generate_code_in_space(struct isl_generate_code_data *data,
4588 __isl_take isl_set *set, __isl_take isl_space *space)
4590 isl_union_map *executed;
4591 isl_ast_build *build;
4592 isl_ast_graft_list *list;
4593 int embed;
4595 executed = isl_union_map_copy(data->executed);
4596 executed = isl_union_map_intersect_domain(executed,
4597 isl_union_set_from_set(set));
4599 embed = !isl_set_is_params(data->build->domain);
4600 if (embed && !data->internal)
4601 executed = internal_executed(executed, space, data->build);
4602 if (!embed) {
4603 isl_set *domain;
4604 domain = isl_ast_build_get_domain(data->build);
4605 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4606 executed = isl_union_map_intersect_params(executed, domain);
4609 build = isl_ast_build_copy(data->build);
4610 build = isl_ast_build_product(build, space);
4612 list = generate_next_level(executed, build);
4614 list = isl_ast_graft_list_unembed(list, embed);
4616 data->list = isl_ast_graft_list_concat(data->list, list);
4618 return 0;
4621 /* Generate an AST that visits the elements in the range of data->executed
4622 * in the relative order specified by the corresponding domain element(s)
4623 * for those domain elements that belong to "set".
4624 * Add the result to data->list.
4626 * The caller ensures that "set" is a universe domain.
4628 * If the build space S is not parametric, then the space of "set"
4629 * need to be a wrapped relation with S as domain. That is, it needs
4630 * to be of the form
4632 * [S -> T]
4634 * Check this property and pass control to generate_code_in_space
4635 * passing along T.
4636 * If the build space is not parametric, then T is the space of "set".
4638 static int generate_code_set(__isl_take isl_set *set, void *user)
4640 struct isl_generate_code_data *data = user;
4641 isl_space *space, *build_space;
4642 int is_domain;
4644 space = isl_set_get_space(set);
4646 if (isl_set_is_params(data->build->domain))
4647 return generate_code_in_space(data, set, space);
4649 build_space = isl_ast_build_get_space(data->build, data->internal);
4650 space = isl_space_unwrap(space);
4651 is_domain = isl_space_is_domain(build_space, space);
4652 isl_space_free(build_space);
4653 space = isl_space_range(space);
4655 if (is_domain < 0)
4656 goto error;
4657 if (!is_domain)
4658 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4659 "invalid nested schedule space", goto error);
4661 return generate_code_in_space(data, set, space);
4662 error:
4663 isl_set_free(set);
4664 isl_space_free(space);
4665 return -1;
4668 /* Generate an AST that visits the elements in the range of "executed"
4669 * in the relative order specified by the corresponding domain element(s).
4671 * "build" is an isl_ast_build that has either been constructed by
4672 * isl_ast_build_from_context or passed to a callback set by
4673 * isl_ast_build_set_create_leaf.
4674 * In the first case, the space of the isl_ast_build is typically
4675 * a parametric space, although this is currently not enforced.
4676 * In the second case, the space is never a parametric space.
4677 * If the space S is not parametric, then the domain space(s) of "executed"
4678 * need to be wrapped relations with S as domain.
4680 * If the domain of "executed" consists of several spaces, then an AST
4681 * is generated for each of them (in arbitrary order) and the results
4682 * are concatenated.
4684 * If "internal" is set, then the domain "S" above refers to the internal
4685 * schedule domain representation. Otherwise, it refers to the external
4686 * representation, as returned by isl_ast_build_get_schedule_space.
4688 * We essentially run over all the spaces in the domain of "executed"
4689 * and call generate_code_set on each of them.
4691 static __isl_give isl_ast_graft_list *generate_code(
4692 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4693 int internal)
4695 isl_ctx *ctx;
4696 struct isl_generate_code_data data = { 0 };
4697 isl_space *space;
4698 isl_union_set *schedule_domain;
4699 isl_union_map *universe;
4701 if (!build)
4702 goto error;
4703 space = isl_ast_build_get_space(build, 1);
4704 space = isl_space_align_params(space,
4705 isl_union_map_get_space(executed));
4706 space = isl_space_align_params(space,
4707 isl_union_map_get_space(build->options));
4708 build = isl_ast_build_align_params(build, isl_space_copy(space));
4709 executed = isl_union_map_align_params(executed, space);
4710 if (!executed || !build)
4711 goto error;
4713 ctx = isl_ast_build_get_ctx(build);
4715 data.internal = internal;
4716 data.executed = executed;
4717 data.build = build;
4718 data.list = isl_ast_graft_list_alloc(ctx, 0);
4720 universe = isl_union_map_universe(isl_union_map_copy(executed));
4721 schedule_domain = isl_union_map_domain(universe);
4722 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
4723 &data) < 0)
4724 data.list = isl_ast_graft_list_free(data.list);
4726 isl_union_set_free(schedule_domain);
4727 isl_union_map_free(executed);
4729 isl_ast_build_free(build);
4730 return data.list;
4731 error:
4732 isl_union_map_free(executed);
4733 isl_ast_build_free(build);
4734 return NULL;
4737 /* Generate an AST that visits the elements in the domain of "schedule"
4738 * in the relative order specified by the corresponding image element(s).
4740 * "build" is an isl_ast_build that has either been constructed by
4741 * isl_ast_build_from_context or passed to a callback set by
4742 * isl_ast_build_set_create_leaf.
4743 * In the first case, the space of the isl_ast_build is typically
4744 * a parametric space, although this is currently not enforced.
4745 * In the second case, the space is never a parametric space.
4746 * If the space S is not parametric, then the range space(s) of "schedule"
4747 * need to be wrapped relations with S as domain.
4749 * If the range of "schedule" consists of several spaces, then an AST
4750 * is generated for each of them (in arbitrary order) and the results
4751 * are concatenated.
4753 * We first initialize the local copies of the relevant options.
4754 * We do this here rather than when the isl_ast_build is created
4755 * because the options may have changed between the construction
4756 * of the isl_ast_build and the call to isl_generate_code.
4758 * The main computation is performed on an inverse schedule (with
4759 * the schedule domain in the domain and the elements to be executed
4760 * in the range) called "executed".
4762 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
4763 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4765 isl_ast_graft_list *list;
4766 isl_ast_node *node;
4767 isl_union_map *executed;
4769 build = isl_ast_build_copy(build);
4770 build = isl_ast_build_set_single_valued(build, 0);
4771 schedule = isl_union_map_coalesce(schedule);
4772 schedule = isl_union_map_remove_redundancies(schedule);
4773 executed = isl_union_map_reverse(schedule);
4774 list = generate_code(executed, isl_ast_build_copy(build), 0);
4775 node = isl_ast_node_from_graft_list(list, build);
4776 isl_ast_build_free(build);
4778 return node;
4781 /* The old name for isl_ast_build_node_from_schedule_map.
4782 * It is being kept for backward compatibility, but
4783 * it will be removed in the future.
4785 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
4786 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4788 return isl_ast_build_node_from_schedule_map(build, schedule);
4791 /* Generate an AST that visits the elements in the domain of "executed"
4792 * in the relative order specified by the band node "node" and its descendants.
4794 * The relation "executed" maps the outer generated loop iterators
4795 * to the domain elements executed by those iterations.
4797 * If the band is empty, we continue with its descendants.
4798 * Otherwise, we extend the build and the inverse schedule with
4799 * the additional space/partial schedule and continue generating
4800 * an AST in generate_next_level.
4801 * As soon as we have extended the inverse schedule with the additional
4802 * partial schedule, we look for equalities that may exists between
4803 * the old and the new part.
4805 static __isl_give isl_ast_graft_list *build_ast_from_band(
4806 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4807 __isl_take isl_union_map *executed)
4809 isl_space *space;
4810 isl_multi_union_pw_aff *extra;
4811 isl_union_map *extra_umap;
4812 isl_ast_graft_list *list;
4813 unsigned n1, n2;
4815 if (!build || !node || !executed)
4816 goto error;
4818 if (isl_schedule_node_band_n_member(node) == 0)
4819 return build_ast_from_child(build, node, executed);
4821 extra = isl_schedule_node_band_get_partial_schedule(node);
4822 extra = isl_multi_union_pw_aff_align_params(extra,
4823 isl_ast_build_get_space(build, 1));
4824 space = isl_multi_union_pw_aff_get_space(extra);
4826 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
4827 extra_umap = isl_union_map_reverse(extra_umap);
4829 executed = isl_union_map_domain_product(executed, extra_umap);
4830 executed = isl_union_map_detect_equalities(executed);
4832 n1 = isl_ast_build_dim(build, isl_dim_param);
4833 build = isl_ast_build_product(build, space);
4834 n2 = isl_ast_build_dim(build, isl_dim_param);
4835 if (n2 > n1)
4836 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
4837 "band node is not allowed to introduce new parameters",
4838 build = isl_ast_build_free(build));
4839 build = isl_ast_build_set_schedule_node(build, node);
4841 list = generate_next_level(executed, build);
4843 list = isl_ast_graft_list_unembed(list, 1);
4845 return list;
4846 error:
4847 isl_schedule_node_free(node);
4848 isl_union_map_free(executed);
4849 isl_ast_build_free(build);
4850 return NULL;
4853 /* Hoist a list of grafts (in practice containing a single graft)
4854 * from "sub_build" (which includes extra context information)
4855 * to "build".
4857 * In particular, project out all additional parameters introduced
4858 * by the context node from the enforced constraints and the guard
4859 * of the single graft.
4861 static __isl_give isl_ast_graft_list *hoist_out_of_context(
4862 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
4863 __isl_keep isl_ast_build *sub_build)
4865 isl_ast_graft *graft;
4866 isl_basic_set *enforced;
4867 isl_set *guard;
4868 unsigned n_param, extra_param;
4870 if (!build || !sub_build)
4871 return isl_ast_graft_list_free(list);
4873 n_param = isl_ast_build_dim(build, isl_dim_param);
4874 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
4876 if (extra_param == n_param)
4877 return list;
4879 extra_param -= n_param;
4880 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
4881 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
4882 n_param, extra_param);
4883 enforced = isl_basic_set_remove_unknown_divs(enforced);
4884 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
4885 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
4886 n_param, extra_param);
4887 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
4888 guard = isl_set_compute_divs(guard);
4889 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
4890 build, sub_build);
4891 list = isl_ast_graft_list_from_ast_graft(graft);
4893 return list;
4896 /* Generate an AST that visits the elements in the domain of "executed"
4897 * in the relative order specified by the context node "node"
4898 * and its descendants.
4900 * The relation "executed" maps the outer generated loop iterators
4901 * to the domain elements executed by those iterations.
4903 * The context node may introduce additional parameters as well as
4904 * constraints on the outer schedule dimenions or original parameters.
4906 * We add the extra parameters to a new build and the context
4907 * constraints to both the build and (as a single disjunct)
4908 * to the domain of "executed". Since the context constraints
4909 * are specified in terms of the input schedule, we first need
4910 * to map them to the internal schedule domain.
4912 * After constructing the AST from the descendants of "node",
4913 * we combine the list of grafts into a single graft within
4914 * the new build, in order to be able to exploit the additional
4915 * context constraints during this combination.
4917 * Additionally, if the current node is the outermost node in
4918 * the schedule tree (apart from the root domain node), we generate
4919 * all pending guards, again to be able to exploit the additional
4920 * context constraints. We currently do not do this for internal
4921 * context nodes since we may still want to hoist conditions
4922 * to outer AST nodes.
4924 * If the context node introduced any new parameters, then they
4925 * are removed from the set of enforced constraints and guard
4926 * in hoist_out_of_context.
4928 static __isl_give isl_ast_graft_list *build_ast_from_context(
4929 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4930 __isl_take isl_union_map *executed)
4932 isl_set *context;
4933 isl_space *space;
4934 isl_multi_aff *internal2input;
4935 isl_ast_build *sub_build;
4936 isl_ast_graft_list *list;
4937 int n, depth;
4939 depth = isl_schedule_node_get_tree_depth(node);
4940 space = isl_ast_build_get_space(build, 1);
4941 context = isl_schedule_node_context_get_context(node);
4942 context = isl_set_align_params(context, space);
4943 sub_build = isl_ast_build_copy(build);
4944 space = isl_set_get_space(context);
4945 sub_build = isl_ast_build_align_params(sub_build, space);
4946 internal2input = isl_ast_build_get_internal2input(sub_build);
4947 context = isl_set_preimage_multi_aff(context, internal2input);
4948 sub_build = isl_ast_build_restrict_generated(sub_build,
4949 isl_set_copy(context));
4950 context = isl_set_from_basic_set(isl_set_simple_hull(context));
4951 executed = isl_union_map_intersect_domain(executed,
4952 isl_union_set_from_set(context));
4954 list = build_ast_from_child(isl_ast_build_copy(sub_build),
4955 node, executed);
4956 n = isl_ast_graft_list_n_ast_graft(list);
4957 if (n < 0)
4958 list = isl_ast_graft_list_free(list);
4960 list = isl_ast_graft_list_fuse(list, sub_build);
4961 if (depth == 1)
4962 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
4963 sub_build);
4964 if (n >= 1)
4965 list = hoist_out_of_context(list, build, sub_build);
4967 isl_ast_build_free(build);
4968 isl_ast_build_free(sub_build);
4970 return list;
4973 /* Generate an AST that visits the elements in the domain of "executed"
4974 * in the relative order specified by the filter node "node" and
4975 * its descendants.
4977 * The relation "executed" maps the outer generated loop iterators
4978 * to the domain elements executed by those iterations.
4980 * We simply intersect the iteration domain (i.e., the range of "executed")
4981 * with the filter and continue with the descendants of the node,
4982 * unless the resulting inverse schedule is empty, in which
4983 * case we return an empty list.
4985 static __isl_give isl_ast_graft_list *build_ast_from_filter(
4986 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4987 __isl_take isl_union_map *executed)
4989 isl_ctx *ctx;
4990 isl_union_set *filter;
4991 isl_ast_graft_list *list;
4992 int empty;
4993 unsigned n1, n2;
4995 if (!build || !node || !executed)
4996 goto error;
4998 filter = isl_schedule_node_filter_get_filter(node);
4999 filter = isl_union_set_align_params(filter,
5000 isl_union_map_get_space(executed));
5001 n1 = isl_union_map_dim(executed, isl_dim_param);
5002 executed = isl_union_map_intersect_range(executed, filter);
5003 n2 = isl_union_map_dim(executed, isl_dim_param);
5004 if (n2 > n1)
5005 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5006 "filter node is not allowed to introduce "
5007 "new parameters", goto error);
5009 empty = isl_union_map_is_empty(executed);
5010 if (empty < 0)
5011 goto error;
5012 if (!empty)
5013 return build_ast_from_child(build, node, executed);
5015 ctx = isl_ast_build_get_ctx(build);
5016 list = isl_ast_graft_list_alloc(ctx, 0);
5017 isl_ast_build_free(build);
5018 isl_schedule_node_free(node);
5019 isl_union_map_free(executed);
5020 return list;
5021 error:
5022 isl_ast_build_free(build);
5023 isl_schedule_node_free(node);
5024 isl_union_map_free(executed);
5025 return NULL;
5028 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5029 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5030 __isl_take isl_union_map *executed);
5032 /* Generate an AST that visits the elements in the domain of "executed"
5033 * in the relative order specified by the sequence (or set) node "node" and
5034 * its descendants.
5036 * The relation "executed" maps the outer generated loop iterators
5037 * to the domain elements executed by those iterations.
5039 * We simply generate an AST for each of the children and concatenate
5040 * the results.
5042 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5043 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5044 __isl_take isl_union_map *executed)
5046 int i, n;
5047 isl_ctx *ctx;
5048 isl_ast_graft_list *list;
5050 ctx = isl_ast_build_get_ctx(build);
5051 list = isl_ast_graft_list_alloc(ctx, 0);
5053 n = isl_schedule_node_n_children(node);
5054 for (i = 0; i < n; ++i) {
5055 isl_schedule_node *child;
5056 isl_ast_graft_list *list_i;
5058 child = isl_schedule_node_get_child(node, i);
5059 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5060 child, isl_union_map_copy(executed));
5061 list = isl_ast_graft_list_concat(list, list_i);
5063 isl_ast_build_free(build);
5064 isl_schedule_node_free(node);
5065 isl_union_map_free(executed);
5067 return list;
5070 /* Generate an AST that visits the elements in the domain of "executed"
5071 * in the relative order specified by the node "node" and its descendants.
5073 * The relation "executed" maps the outer generated loop iterators
5074 * to the domain elements executed by those iterations.
5076 * If the node is a leaf, then we pass control to generate_inner_level.
5077 * Note that the current build does not refer to any band node, so
5078 * that generate_inner_level will not try to visit the child of
5079 * the leaf node.
5081 * The other node types are handled in separate functions.
5082 * Set nodes are currently treated in the same way as sequence nodes.
5083 * The children of a set node may be executed in any order,
5084 * including the order of the children.
5086 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5087 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5088 __isl_take isl_union_map *executed)
5090 enum isl_schedule_node_type type;
5092 type = isl_schedule_node_get_type(node);
5094 switch (type) {
5095 case isl_schedule_node_error:
5096 goto error;
5097 case isl_schedule_node_leaf:
5098 isl_schedule_node_free(node);
5099 return generate_inner_level(executed, build);
5100 case isl_schedule_node_band:
5101 return build_ast_from_band(build, node, executed);
5102 case isl_schedule_node_context:
5103 return build_ast_from_context(build, node, executed);
5104 case isl_schedule_node_domain:
5105 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5106 "unexpected internal domain node", goto error);
5107 case isl_schedule_node_filter:
5108 return build_ast_from_filter(build, node, executed);
5109 case isl_schedule_node_sequence:
5110 case isl_schedule_node_set:
5111 return build_ast_from_sequence(build, node, executed);
5114 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5115 "unhandled type", goto error);
5116 error:
5117 isl_union_map_free(executed);
5118 isl_schedule_node_free(node);
5119 isl_ast_build_free(build);
5121 return NULL;
5124 /* Generate an AST that visits the elements in the domain of "executed"
5125 * in the relative order specified by the (single) child of "node" and
5126 * its descendants.
5128 * The relation "executed" maps the outer generated loop iterators
5129 * to the domain elements executed by those iterations.
5131 * This function is never called on a leaf, set or sequence node,
5132 * so the node always has exactly one child.
5134 static __isl_give isl_ast_graft_list *build_ast_from_child(
5135 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5136 __isl_take isl_union_map *executed)
5138 node = isl_schedule_node_child(node, 0);
5139 return build_ast_from_schedule_node(build, node, executed);
5142 /* Generate an AST that visits the elements in the domain of the domain
5143 * node "node" in the relative order specified by its descendants.
5145 * An initial inverse schedule is created that maps a zero-dimensional
5146 * schedule space to the node domain.
5147 * The input "build" is assumed to have a parametric domain and
5148 * is replaced by the same zero-dimensional schedule space.
5150 * We also add some of the parameter constraints in the build domain
5151 * to the executed relation. Adding these constraints
5152 * allows for an earlier detection of conflicts in some cases.
5153 * However, we do not want to divide the executed relation into
5154 * more disjuncts than necessary. We therefore approximate
5155 * the constraints on the parameters by a single disjunct set.
5157 static __isl_give isl_ast_node *build_ast_from_domain(
5158 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5160 isl_ctx *ctx;
5161 isl_union_set *domain, *schedule_domain;
5162 isl_union_map *executed;
5163 isl_space *space;
5164 isl_set *set;
5165 isl_ast_graft_list *list;
5166 isl_ast_node *ast;
5167 int is_params;
5169 if (!build)
5170 goto error;
5172 ctx = isl_ast_build_get_ctx(build);
5173 space = isl_ast_build_get_space(build, 1);
5174 is_params = isl_space_is_params(space);
5175 isl_space_free(space);
5176 if (is_params < 0)
5177 goto error;
5178 if (!is_params)
5179 isl_die(ctx, isl_error_unsupported,
5180 "expecting parametric initial context", goto error);
5182 domain = isl_schedule_node_domain_get_domain(node);
5183 domain = isl_union_set_coalesce(domain);
5185 space = isl_union_set_get_space(domain);
5186 space = isl_space_set_from_params(space);
5187 build = isl_ast_build_product(build, space);
5189 set = isl_ast_build_get_domain(build);
5190 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5191 schedule_domain = isl_union_set_from_set(set);
5193 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5194 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5195 ast = isl_ast_node_from_graft_list(list, build);
5196 isl_ast_build_free(build);
5198 return ast;
5199 error:
5200 isl_schedule_node_free(node);
5201 isl_ast_build_free(build);
5202 return NULL;
5205 /* Generate an AST that visits the elements in the domain of "schedule"
5206 * in the relative order specified by the schedule tree.
5208 * "build" is an isl_ast_build that has been created using
5209 * isl_ast_build_alloc or isl_ast_build_from_context based
5210 * on a parametric set.
5212 * The construction starts at the root node of the schedule,
5213 * which is assumed to be a domain node.
5215 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5216 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5218 isl_ctx *ctx;
5219 isl_schedule_node *node;
5221 if (!build || !schedule)
5222 goto error;
5224 ctx = isl_ast_build_get_ctx(build);
5226 node = isl_schedule_get_root(schedule);
5227 isl_schedule_free(schedule);
5229 build = isl_ast_build_copy(build);
5230 build = isl_ast_build_set_single_valued(build, 0);
5231 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5232 isl_die(ctx, isl_error_unsupported,
5233 "expecting root domain node",
5234 build = isl_ast_build_free(build));
5235 return build_ast_from_domain(build, node);
5236 error:
5237 isl_schedule_free(schedule);
5238 return NULL;