isl_map_simplify.c: eliminate_divs_eq: isl_basic_map_offset
[isl.git] / isl_ast_codegen.c
blobaedf64f3968ba147a801a6939ff0872c8490068c
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/id.h>
15 #include <isl/val.h>
16 #include <isl/space.h>
17 #include <isl/aff.h>
18 #include <isl/constraint.h>
19 #include <isl/set.h>
20 #include <isl/ilp.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/schedule_node.h>
24 #include <isl/options.h>
25 #include <isl_sort.h>
26 #include <isl_tarjan.h>
27 #include <isl_ast_private.h>
28 #include <isl_ast_build_expr.h>
29 #include <isl_ast_build_private.h>
30 #include <isl_ast_graft_private.h>
32 /* Try and reduce the number of disjuncts in the representation of "set",
33 * without dropping explicit representations of local variables.
35 static __isl_give isl_set *isl_set_coalesce_preserve(__isl_take isl_set *set)
37 isl_ctx *ctx;
38 int save_preserve;
40 if (!set)
41 return NULL;
43 ctx = isl_set_get_ctx(set);
44 save_preserve = isl_options_get_coalesce_preserve_locals(ctx);
45 isl_options_set_coalesce_preserve_locals(ctx, 1);
46 set = isl_set_coalesce(set);
47 isl_options_set_coalesce_preserve_locals(ctx, save_preserve);
48 return set;
51 /* Data used in generate_domain.
53 * "build" is the input build.
54 * "list" collects the results.
56 struct isl_generate_domain_data {
57 isl_ast_build *build;
59 isl_ast_graft_list *list;
62 static __isl_give isl_ast_graft_list *generate_next_level(
63 __isl_take isl_union_map *executed,
64 __isl_take isl_ast_build *build);
65 static __isl_give isl_ast_graft_list *generate_code(
66 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
67 int internal);
69 /* Generate an AST for a single domain based on
70 * the (non single valued) inverse schedule "executed".
72 * We extend the schedule with the iteration domain
73 * and continue generating through a call to generate_code.
75 * In particular, if executed has the form
77 * S -> D
79 * then we continue generating code on
81 * [S -> D] -> D
83 * The extended inverse schedule is clearly single valued
84 * ensuring that the nested generate_code will not reach this function,
85 * but will instead create calls to all elements of D that need
86 * to be executed from the current schedule domain.
88 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
89 struct isl_generate_domain_data *data)
91 isl_map *identity;
92 isl_ast_build *build;
93 isl_ast_graft_list *list;
95 build = isl_ast_build_copy(data->build);
97 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
98 executed = isl_map_domain_product(executed, identity);
99 build = isl_ast_build_set_single_valued(build, 1);
101 list = generate_code(isl_union_map_from_map(executed), build, 1);
103 data->list = isl_ast_graft_list_concat(data->list, list);
105 return isl_stat_ok;
108 /* Call the at_each_domain callback, if requested by the user,
109 * after recording the current inverse schedule in the build.
111 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
112 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
114 if (!graft || !build)
115 return isl_ast_graft_free(graft);
116 if (!build->at_each_domain)
117 return graft;
119 build = isl_ast_build_copy(build);
120 build = isl_ast_build_set_executed(build,
121 isl_union_map_from_map(isl_map_copy(executed)));
122 if (!build)
123 return isl_ast_graft_free(graft);
125 graft->node = build->at_each_domain(graft->node,
126 build, build->at_each_domain_user);
127 isl_ast_build_free(build);
129 if (!graft->node)
130 graft = isl_ast_graft_free(graft);
132 return graft;
135 /* Generate a call expression for the single executed
136 * domain element "map" and put a guard around it based its (simplified)
137 * domain. "executed" is the original inverse schedule from which "map"
138 * has been derived. In particular, "map" is either identical to "executed"
139 * or it is the result of gisting "executed" with respect to the build domain.
140 * "executed" is only used if there is an at_each_domain callback.
142 * At this stage, any pending constraints in the build can no longer
143 * be simplified with respect to any enforced constraints since
144 * the call node does not have any enforced constraints.
145 * Since all pending constraints not covered by any enforced constraints
146 * will be added as a guard to the graft in create_node_scaled,
147 * even in the eliminated case, the pending constraints
148 * can be considered to have been generated by outer constructs.
150 * If the user has set an at_each_domain callback, it is called
151 * on the constructed call expression node.
153 static isl_stat add_domain(__isl_take isl_map *executed,
154 __isl_take isl_map *map, struct isl_generate_domain_data *data)
156 isl_ast_build *build;
157 isl_ast_graft *graft;
158 isl_ast_graft_list *list;
159 isl_set *guard, *pending;
161 build = isl_ast_build_copy(data->build);
162 pending = isl_ast_build_get_pending(build);
163 build = isl_ast_build_replace_pending_by_guard(build, pending);
165 guard = isl_map_domain(isl_map_copy(map));
166 guard = isl_set_compute_divs(guard);
167 guard = isl_set_coalesce_preserve(guard);
168 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
169 guard = isl_ast_build_specialize(build, guard);
171 graft = isl_ast_graft_alloc_domain(map, build);
172 graft = at_each_domain(graft, executed, build);
173 isl_ast_build_free(build);
174 isl_map_free(executed);
175 graft = isl_ast_graft_add_guard(graft, guard, data->build);
177 list = isl_ast_graft_list_from_ast_graft(graft);
178 data->list = isl_ast_graft_list_concat(data->list, list);
180 return isl_stat_ok;
183 /* Generate an AST for a single domain based on
184 * the inverse schedule "executed" and add it to data->list.
186 * If there is more than one domain element associated to the current
187 * schedule "time", then we need to continue the generation process
188 * in generate_non_single_valued.
189 * Note that the inverse schedule being single-valued may depend
190 * on constraints that are only available in the original context
191 * domain specified by the user. We therefore first introduce
192 * some of the constraints of data->build->domain. In particular,
193 * we intersect with a single-disjunct approximation of this set.
194 * We perform this approximation to avoid further splitting up
195 * the executed relation, possibly introducing a disjunctive guard
196 * on the statement.
198 * On the other hand, we only perform the test after having taken the gist
199 * of the domain as the resulting map is the one from which the call
200 * expression is constructed. Using this map to construct the call
201 * expression usually yields simpler results in cases where the original
202 * map is not obviously single-valued.
203 * If the original map is obviously single-valued, then the gist
204 * operation is skipped.
206 * Because we perform the single-valuedness test on the gisted map,
207 * we may in rare cases fail to recognize that the inverse schedule
208 * is single-valued. This becomes problematic if this happens
209 * from the recursive call through generate_non_single_valued
210 * as we would then end up in an infinite recursion.
211 * We therefore check if we are inside a call to generate_non_single_valued
212 * and revert to the ungisted map if the gisted map turns out not to be
213 * single-valued.
215 * Otherwise, call add_domain to generate a call expression (with guard) and
216 * to call the at_each_domain callback, if any.
218 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
220 struct isl_generate_domain_data *data = user;
221 isl_set *domain;
222 isl_map *map = NULL;
223 int empty, sv;
225 domain = isl_ast_build_get_domain(data->build);
226 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
227 executed = isl_map_intersect_domain(executed, domain);
228 empty = isl_map_is_empty(executed);
229 if (empty < 0)
230 goto error;
231 if (empty) {
232 isl_map_free(executed);
233 return isl_stat_ok;
236 sv = isl_map_plain_is_single_valued(executed);
237 if (sv < 0)
238 goto error;
239 if (sv)
240 return add_domain(executed, isl_map_copy(executed), data);
242 executed = isl_map_coalesce(executed);
243 map = isl_map_copy(executed);
244 map = isl_ast_build_compute_gist_map_domain(data->build, map);
245 sv = isl_map_is_single_valued(map);
246 if (sv < 0)
247 goto error;
248 if (!sv) {
249 isl_map_free(map);
250 if (data->build->single_valued)
251 map = isl_map_copy(executed);
252 else
253 return generate_non_single_valued(executed, data);
256 return add_domain(executed, map, data);
257 error:
258 isl_map_free(map);
259 isl_map_free(executed);
260 return isl_stat_error;
263 /* Call build->create_leaf to a create "leaf" node in the AST,
264 * encapsulate the result in an isl_ast_graft and return the result
265 * as a 1-element list.
267 * Note that the node returned by the user may be an entire tree.
269 * Since the node itself cannot enforce any constraints, we turn
270 * all pending constraints into guards and add them to the resulting
271 * graft to ensure that they will be generated.
273 * Before we pass control to the user, we first clear some information
274 * from the build that is (presumbably) only meaningful
275 * for the current code generation.
276 * This includes the create_leaf callback itself, so we make a copy
277 * of the build first.
279 static __isl_give isl_ast_graft_list *call_create_leaf(
280 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
282 isl_set *guard;
283 isl_ast_node *node;
284 isl_ast_graft *graft;
285 isl_ast_build *user_build;
287 guard = isl_ast_build_get_pending(build);
288 user_build = isl_ast_build_copy(build);
289 user_build = isl_ast_build_replace_pending_by_guard(user_build,
290 isl_set_copy(guard));
291 user_build = isl_ast_build_set_executed(user_build, executed);
292 user_build = isl_ast_build_clear_local_info(user_build);
293 if (!user_build)
294 node = NULL;
295 else
296 node = build->create_leaf(user_build, build->create_leaf_user);
297 graft = isl_ast_graft_alloc(node, build);
298 graft = isl_ast_graft_add_guard(graft, guard, build);
299 isl_ast_build_free(build);
300 return isl_ast_graft_list_from_ast_graft(graft);
303 static __isl_give isl_ast_graft_list *build_ast_from_child(
304 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
305 __isl_take isl_union_map *executed);
307 /* Generate an AST after having handled the complete schedule
308 * of this call to the code generator or the complete band
309 * if we are generating an AST from a schedule tree.
311 * If we are inside a band node, then move on to the child of the band.
313 * If the user has specified a create_leaf callback, control
314 * is passed to the user in call_create_leaf.
316 * Otherwise, we generate one or more calls for each individual
317 * domain in generate_domain.
319 static __isl_give isl_ast_graft_list *generate_inner_level(
320 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
322 isl_ctx *ctx;
323 struct isl_generate_domain_data data = { build };
325 if (!build || !executed)
326 goto error;
328 if (isl_ast_build_has_schedule_node(build)) {
329 isl_schedule_node *node;
330 node = isl_ast_build_get_schedule_node(build);
331 build = isl_ast_build_reset_schedule_node(build);
332 return build_ast_from_child(build, node, executed);
335 if (build->create_leaf)
336 return call_create_leaf(executed, build);
338 ctx = isl_union_map_get_ctx(executed);
339 data.list = isl_ast_graft_list_alloc(ctx, 0);
340 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
341 data.list = isl_ast_graft_list_free(data.list);
343 if (0)
344 error: data.list = NULL;
345 isl_ast_build_free(build);
346 isl_union_map_free(executed);
347 return data.list;
350 /* Call the before_each_for callback, if requested by the user.
352 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
353 __isl_keep isl_ast_build *build)
355 isl_id *id;
357 if (!node || !build)
358 return isl_ast_node_free(node);
359 if (!build->before_each_for)
360 return node;
361 id = build->before_each_for(build, build->before_each_for_user);
362 node = isl_ast_node_set_annotation(node, id);
363 return node;
366 /* Call the after_each_for callback, if requested by the user.
368 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
369 __isl_keep isl_ast_build *build)
371 if (!graft || !build)
372 return isl_ast_graft_free(graft);
373 if (!build->after_each_for)
374 return graft;
375 graft->node = build->after_each_for(graft->node, build,
376 build->after_each_for_user);
377 if (!graft->node)
378 return isl_ast_graft_free(graft);
379 return graft;
382 /* Plug in all the know values of the current and outer dimensions
383 * in the domain of "executed". In principle, we only need to plug
384 * in the known value of the current dimension since the values of
385 * outer dimensions have been plugged in already.
386 * However, it turns out to be easier to just plug in all known values.
388 static __isl_give isl_union_map *plug_in_values(
389 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
391 return isl_ast_build_substitute_values_union_map_domain(build,
392 executed);
395 /* Check if the constraint "c" is a lower bound on dimension "pos",
396 * an upper bound, or independent of dimension "pos".
398 static int constraint_type(isl_constraint *c, int pos)
400 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
401 return 1;
402 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
403 return 2;
404 return 0;
407 /* Compare the types of the constraints "a" and "b",
408 * resulting in constraints that are independent of "depth"
409 * to be sorted before the lower bounds on "depth", which in
410 * turn are sorted before the upper bounds on "depth".
412 static int cmp_constraint(__isl_keep isl_constraint *a,
413 __isl_keep isl_constraint *b, void *user)
415 int *depth = user;
416 int t1 = constraint_type(a, *depth);
417 int t2 = constraint_type(b, *depth);
419 return t1 - t2;
422 /* Extract a lower bound on dimension "pos" from constraint "c".
424 * If the constraint is of the form
426 * a x + f(...) >= 0
428 * then we essentially return
430 * l = ceil(-f(...)/a)
432 * However, if the current dimension is strided, then we need to make
433 * sure that the lower bound we construct is of the form
435 * f + s a
437 * with f the offset and s the stride.
438 * We therefore compute
440 * f + s * ceil((l - f)/s)
442 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
443 int pos, __isl_keep isl_ast_build *build)
445 isl_aff *aff;
447 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
448 aff = isl_aff_ceil(aff);
450 if (isl_ast_build_has_stride(build, pos)) {
451 isl_aff *offset;
452 isl_val *stride;
454 offset = isl_ast_build_get_offset(build, pos);
455 stride = isl_ast_build_get_stride(build, pos);
457 aff = isl_aff_sub(aff, isl_aff_copy(offset));
458 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
459 aff = isl_aff_ceil(aff);
460 aff = isl_aff_scale_val(aff, stride);
461 aff = isl_aff_add(aff, offset);
464 aff = isl_ast_build_compute_gist_aff(build, aff);
466 return aff;
469 /* Return the exact lower bound (or upper bound if "upper" is set)
470 * of "domain" as a piecewise affine expression.
472 * If we are computing a lower bound (of a strided dimension), then
473 * we need to make sure it is of the form
475 * f + s a
477 * where f is the offset and s is the stride.
478 * We therefore need to include the stride constraint before computing
479 * the minimum.
481 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
482 __isl_keep isl_ast_build *build, int upper)
484 isl_set *stride;
485 isl_map *it_map;
486 isl_pw_aff *pa;
487 isl_pw_multi_aff *pma;
489 domain = isl_set_copy(domain);
490 if (!upper) {
491 stride = isl_ast_build_get_stride_constraint(build);
492 domain = isl_set_intersect(domain, stride);
494 it_map = isl_ast_build_map_to_iterator(build, domain);
495 if (upper)
496 pma = isl_map_lexmax_pw_multi_aff(it_map);
497 else
498 pma = isl_map_lexmin_pw_multi_aff(it_map);
499 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
500 isl_pw_multi_aff_free(pma);
501 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
502 pa = isl_pw_aff_coalesce(pa);
504 return pa;
507 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
508 * remove_redundant_lower_bounds.
510 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
511 void *user)
513 return isl_pw_aff_plain_cmp(a, b);
516 /* Given a list of lower bounds "list", remove those that are redundant
517 * with respect to the other bounds in "list" and the domain of "build".
519 * We first sort the bounds in the same way as they would be sorted
520 * by set_for_node_expressions so that we can try and remove the last
521 * bounds first.
523 * For a lower bound to be effective, there needs to be at least
524 * one domain element for which it is larger than all other lower bounds.
525 * For each lower bound we therefore intersect the domain with
526 * the conditions that it is larger than all other bounds and
527 * check whether the result is empty. If so, the bound can be removed.
529 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
530 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
532 int i, j, n;
533 isl_set *domain;
535 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
536 if (!list)
537 return NULL;
539 n = isl_pw_aff_list_n_pw_aff(list);
540 if (n <= 1)
541 return list;
543 domain = isl_ast_build_get_domain(build);
545 for (i = n - 1; i >= 0; --i) {
546 isl_pw_aff *pa_i;
547 isl_set *domain_i;
548 int empty;
550 domain_i = isl_set_copy(domain);
551 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
553 for (j = 0; j < n; ++j) {
554 isl_pw_aff *pa_j;
555 isl_set *better;
557 if (j == i)
558 continue;
560 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
561 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
562 domain_i = isl_set_intersect(domain_i, better);
565 empty = isl_set_is_empty(domain_i);
567 isl_set_free(domain_i);
568 isl_pw_aff_free(pa_i);
570 if (empty < 0)
571 goto error;
572 if (!empty)
573 continue;
574 list = isl_pw_aff_list_drop(list, i, 1);
575 n--;
578 isl_set_free(domain);
580 return list;
581 error:
582 isl_set_free(domain);
583 return isl_pw_aff_list_free(list);
586 /* Extract a lower bound on dimension "pos" from each constraint
587 * in "constraints" and return the list of lower bounds.
588 * If "constraints" has zero elements, then we extract a lower bound
589 * from "domain" instead.
591 * If the current dimension is strided, then the lower bound
592 * is adjusted by lower_bound to match the stride information.
593 * This modification may make one or more lower bounds redundant
594 * with respect to the other lower bounds. We therefore check
595 * for this condition and remove the redundant lower bounds.
597 static __isl_give isl_pw_aff_list *lower_bounds(
598 __isl_keep isl_constraint_list *constraints, int pos,
599 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
601 isl_ctx *ctx;
602 isl_pw_aff_list *list;
603 int i, n;
605 if (!build)
606 return NULL;
608 n = isl_constraint_list_n_constraint(constraints);
609 if (n == 0) {
610 isl_pw_aff *pa;
611 pa = exact_bound(domain, build, 0);
612 return isl_pw_aff_list_from_pw_aff(pa);
615 ctx = isl_ast_build_get_ctx(build);
616 list = isl_pw_aff_list_alloc(ctx,n);
618 for (i = 0; i < n; ++i) {
619 isl_aff *aff;
620 isl_constraint *c;
622 c = isl_constraint_list_get_constraint(constraints, i);
623 aff = lower_bound(c, pos, build);
624 isl_constraint_free(c);
625 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
628 if (isl_ast_build_has_stride(build, pos))
629 list = remove_redundant_lower_bounds(list, build);
631 return list;
634 /* Extract an upper bound on dimension "pos" from each constraint
635 * in "constraints" and return the list of upper bounds.
636 * If "constraints" has zero elements, then we extract an upper bound
637 * from "domain" instead.
639 static __isl_give isl_pw_aff_list *upper_bounds(
640 __isl_keep isl_constraint_list *constraints, int pos,
641 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
643 isl_ctx *ctx;
644 isl_pw_aff_list *list;
645 int i, n;
647 n = isl_constraint_list_n_constraint(constraints);
648 if (n == 0) {
649 isl_pw_aff *pa;
650 pa = exact_bound(domain, build, 1);
651 return isl_pw_aff_list_from_pw_aff(pa);
654 ctx = isl_ast_build_get_ctx(build);
655 list = isl_pw_aff_list_alloc(ctx,n);
657 for (i = 0; i < n; ++i) {
658 isl_aff *aff;
659 isl_constraint *c;
661 c = isl_constraint_list_get_constraint(constraints, i);
662 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
663 isl_constraint_free(c);
664 aff = isl_aff_floor(aff);
665 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
668 return list;
671 /* Return an isl_ast_expr that performs the reduction of type "type"
672 * on AST expressions corresponding to the elements in "list".
674 * The list is assumed to contain at least one element.
675 * If the list contains exactly one element, then the returned isl_ast_expr
676 * simply computes that affine expression.
677 * If the list contains more than one element, then we sort it
678 * using a fairly abitrary but hopefully reasonably stable order.
680 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
681 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
683 int i, n;
684 isl_ctx *ctx;
685 isl_ast_expr *expr;
687 if (!list)
688 return NULL;
690 n = isl_pw_aff_list_n_pw_aff(list);
692 if (n == 1)
693 return isl_ast_build_expr_from_pw_aff_internal(build,
694 isl_pw_aff_list_get_pw_aff(list, 0));
696 ctx = isl_pw_aff_list_get_ctx(list);
697 expr = isl_ast_expr_alloc_op(ctx, type, n);
698 if (!expr)
699 return NULL;
701 list = isl_pw_aff_list_copy(list);
702 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
703 if (!list)
704 return isl_ast_expr_free(expr);
706 for (i = 0; i < n; ++i) {
707 isl_ast_expr *expr_i;
709 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
710 isl_pw_aff_list_get_pw_aff(list, i));
711 if (!expr_i)
712 goto error;
713 expr->u.op.args[i] = expr_i;
716 isl_pw_aff_list_free(list);
717 return expr;
718 error:
719 isl_pw_aff_list_free(list);
720 isl_ast_expr_free(expr);
721 return NULL;
724 /* Add guards implied by the "generated constraints",
725 * but not (necessarily) enforced by the generated AST to "guard".
726 * In particular, if there is any stride constraints,
727 * then add the guard implied by those constraints.
728 * If we have generated a degenerate loop, then add the guard
729 * implied by "bounds" on the outer dimensions, i.e., the guard
730 * that ensures that the single value actually exists.
731 * Since there may also be guards implied by a combination
732 * of these constraints, we first combine them before
733 * deriving the implied constraints.
735 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
736 int degenerate, __isl_keep isl_basic_set *bounds,
737 __isl_keep isl_ast_build *build)
739 int depth, has_stride;
740 isl_space *space;
741 isl_set *dom, *set;
743 depth = isl_ast_build_get_depth(build);
744 has_stride = isl_ast_build_has_stride(build, depth);
745 if (!has_stride && !degenerate)
746 return guard;
748 space = isl_basic_set_get_space(bounds);
749 dom = isl_set_universe(space);
751 if (degenerate) {
752 bounds = isl_basic_set_copy(bounds);
753 bounds = isl_basic_set_drop_constraints_not_involving_dims(
754 bounds, isl_dim_set, depth, 1);
755 set = isl_set_from_basic_set(bounds);
756 dom = isl_set_intersect(dom, set);
759 if (has_stride) {
760 set = isl_ast_build_get_stride_constraint(build);
761 dom = isl_set_intersect(dom, set);
764 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
765 dom = isl_ast_build_compute_gist(build, dom);
766 guard = isl_set_intersect(guard, dom);
768 return guard;
771 /* Update "graft" based on "sub_build" for the degenerate case.
773 * "build" is the build in which graft->node was created
774 * "sub_build" contains information about the current level itself,
775 * including the single value attained.
777 * We set the initialization part of the for loop to the single
778 * value attained by the current dimension.
779 * The increment and condition are not strictly needed as the are known
780 * to be "1" and "iterator <= value" respectively.
782 static __isl_give isl_ast_graft *refine_degenerate(
783 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
784 __isl_keep isl_ast_build *sub_build)
786 isl_pw_aff *value;
788 if (!graft || !sub_build)
789 return isl_ast_graft_free(graft);
791 value = isl_pw_aff_copy(sub_build->value);
793 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
794 value);
795 if (!graft->node->u.f.init)
796 return isl_ast_graft_free(graft);
798 return graft;
801 /* Return the intersection of constraints in "list" as a set.
803 static __isl_give isl_set *intersect_constraints(
804 __isl_keep isl_constraint_list *list)
806 int i, n;
807 isl_basic_set *bset;
809 n = isl_constraint_list_n_constraint(list);
810 if (n < 1)
811 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
812 "expecting at least one constraint", return NULL);
814 bset = isl_basic_set_from_constraint(
815 isl_constraint_list_get_constraint(list, 0));
816 for (i = 1; i < n; ++i) {
817 isl_basic_set *bset_i;
819 bset_i = isl_basic_set_from_constraint(
820 isl_constraint_list_get_constraint(list, i));
821 bset = isl_basic_set_intersect(bset, bset_i);
824 return isl_set_from_basic_set(bset);
827 /* Compute the constraints on the outer dimensions enforced by
828 * graft->node and add those constraints to graft->enforced,
829 * in case the upper bound is expressed as a set "upper".
831 * In particular, if l(...) is a lower bound in "lower", and
833 * -a i + f(...) >= 0 or a i <= f(...)
835 * is an upper bound ocnstraint on the current dimension i,
836 * then the for loop enforces the constraint
838 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
840 * We therefore simply take each lower bound in turn, plug it into
841 * the upper bounds and compute the intersection over all lower bounds.
843 * If a lower bound is a rational expression, then
844 * isl_basic_set_preimage_multi_aff will force this rational
845 * expression to have only integer values. However, the loop
846 * itself does not enforce this integrality constraint. We therefore
847 * use the ceil of the lower bounds instead of the lower bounds themselves.
848 * Other constraints will make sure that the for loop is only executed
849 * when each of the lower bounds attains an integral value.
850 * In particular, potentially rational values only occur in
851 * lower_bound if the offset is a (seemingly) rational expression,
852 * but then outer conditions will make sure that this rational expression
853 * only attains integer values.
855 static __isl_give isl_ast_graft *set_enforced_from_set(
856 __isl_take isl_ast_graft *graft,
857 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
859 isl_space *space;
860 isl_basic_set *enforced;
861 isl_pw_multi_aff *pma;
862 int i, n;
864 if (!graft || !lower)
865 return isl_ast_graft_free(graft);
867 space = isl_set_get_space(upper);
868 enforced = isl_basic_set_universe(isl_space_copy(space));
870 space = isl_space_map_from_set(space);
871 pma = isl_pw_multi_aff_identity(space);
873 n = isl_pw_aff_list_n_pw_aff(lower);
874 for (i = 0; i < n; ++i) {
875 isl_pw_aff *pa;
876 isl_set *enforced_i;
877 isl_basic_set *hull;
878 isl_pw_multi_aff *pma_i;
880 pa = isl_pw_aff_list_get_pw_aff(lower, i);
881 pa = isl_pw_aff_ceil(pa);
882 pma_i = isl_pw_multi_aff_copy(pma);
883 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
884 enforced_i = isl_set_copy(upper);
885 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
886 hull = isl_set_simple_hull(enforced_i);
887 enforced = isl_basic_set_intersect(enforced, hull);
890 isl_pw_multi_aff_free(pma);
892 graft = isl_ast_graft_enforce(graft, enforced);
894 return graft;
897 /* Compute the constraints on the outer dimensions enforced by
898 * graft->node and add those constraints to graft->enforced,
899 * in case the upper bound is expressed as
900 * a list of affine expressions "upper".
902 * The enforced condition is that each lower bound expression is less
903 * than or equal to each upper bound expression.
905 static __isl_give isl_ast_graft *set_enforced_from_list(
906 __isl_take isl_ast_graft *graft,
907 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
909 isl_set *cond;
910 isl_basic_set *enforced;
912 lower = isl_pw_aff_list_copy(lower);
913 upper = isl_pw_aff_list_copy(upper);
914 cond = isl_pw_aff_list_le_set(lower, upper);
915 enforced = isl_set_simple_hull(cond);
916 graft = isl_ast_graft_enforce(graft, enforced);
918 return graft;
921 /* Does "aff" have a negative constant term?
923 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
924 __isl_take isl_aff *aff, void *user)
926 int *neg = user;
927 isl_val *v;
929 v = isl_aff_get_constant_val(aff);
930 *neg = isl_val_is_neg(v);
931 isl_val_free(v);
932 isl_set_free(set);
933 isl_aff_free(aff);
935 return *neg ? isl_stat_ok : isl_stat_error;
938 /* Does "pa" have a negative constant term over its entire domain?
940 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
941 void *user)
943 isl_stat r;
944 int *neg = user;
946 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
947 isl_pw_aff_free(pa);
949 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
952 /* Does each element in "list" have a negative constant term?
954 * The callback terminates the iteration as soon an element has been
955 * found that does not have a negative constant term.
957 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
959 int neg = 1;
961 if (isl_pw_aff_list_foreach(list,
962 &pw_aff_constant_is_negative, &neg) < 0 && neg)
963 return -1;
965 return neg;
968 /* Add 1 to each of the elements in "list", where each of these elements
969 * is defined over the internal schedule space of "build".
971 static __isl_give isl_pw_aff_list *list_add_one(
972 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
974 int i, n;
975 isl_space *space;
976 isl_aff *aff;
977 isl_pw_aff *one;
979 space = isl_ast_build_get_space(build, 1);
980 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
981 aff = isl_aff_add_constant_si(aff, 1);
982 one = isl_pw_aff_from_aff(aff);
984 n = isl_pw_aff_list_n_pw_aff(list);
985 for (i = 0; i < n; ++i) {
986 isl_pw_aff *pa;
987 pa = isl_pw_aff_list_get_pw_aff(list, i);
988 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
989 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
992 isl_pw_aff_free(one);
994 return list;
997 /* Set the condition part of the for node graft->node in case
998 * the upper bound is represented as a list of piecewise affine expressions.
1000 * In particular, set the condition to
1002 * iterator <= min(list of upper bounds)
1004 * If each of the upper bounds has a negative constant term, then
1005 * set the condition to
1007 * iterator < min(list of (upper bound + 1)s)
1010 static __isl_give isl_ast_graft *set_for_cond_from_list(
1011 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
1012 __isl_keep isl_ast_build *build)
1014 int neg;
1015 isl_ast_expr *bound, *iterator, *cond;
1016 enum isl_ast_op_type type = isl_ast_op_le;
1018 if (!graft || !list)
1019 return isl_ast_graft_free(graft);
1021 neg = list_constant_is_negative(list);
1022 if (neg < 0)
1023 return isl_ast_graft_free(graft);
1024 list = isl_pw_aff_list_copy(list);
1025 if (neg) {
1026 list = list_add_one(list, build);
1027 type = isl_ast_op_lt;
1030 bound = reduce_list(isl_ast_op_min, list, build);
1031 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1032 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1033 graft->node->u.f.cond = cond;
1035 isl_pw_aff_list_free(list);
1036 if (!graft->node->u.f.cond)
1037 return isl_ast_graft_free(graft);
1038 return graft;
1041 /* Set the condition part of the for node graft->node in case
1042 * the upper bound is represented as a set.
1044 static __isl_give isl_ast_graft *set_for_cond_from_set(
1045 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1046 __isl_keep isl_ast_build *build)
1048 isl_ast_expr *cond;
1050 if (!graft)
1051 return NULL;
1053 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1054 graft->node->u.f.cond = cond;
1055 if (!graft->node->u.f.cond)
1056 return isl_ast_graft_free(graft);
1057 return graft;
1060 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1061 * the current dimension.
1063 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1065 int depth;
1066 isl_val *v;
1067 isl_ctx *ctx;
1069 if (!build)
1070 return NULL;
1071 ctx = isl_ast_build_get_ctx(build);
1072 depth = isl_ast_build_get_depth(build);
1074 if (!isl_ast_build_has_stride(build, depth))
1075 return isl_ast_expr_alloc_int_si(ctx, 1);
1077 v = isl_ast_build_get_stride(build, depth);
1078 return isl_ast_expr_from_val(v);
1081 /* Should we express the loop condition as
1083 * iterator <= min(list of upper bounds)
1085 * or as a conjunction of constraints?
1087 * The first is constructed from a list of upper bounds.
1088 * The second is constructed from a set.
1090 * If there are no upper bounds in "constraints", then this could mean
1091 * that "domain" simply doesn't have an upper bound or that we didn't
1092 * pick any upper bound. In the first case, we want to generate the
1093 * loop condition as a(n empty) conjunction of constraints
1094 * In the second case, we will compute
1095 * a single upper bound from "domain" and so we use the list form.
1097 * If there are upper bounds in "constraints",
1098 * then we use the list form iff the atomic_upper_bound option is set.
1100 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1101 __isl_keep isl_set *domain, int depth)
1103 if (n_upper > 0)
1104 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1105 else
1106 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1109 /* Fill in the expressions of the for node in graft->node.
1111 * In particular,
1112 * - set the initialization part of the loop to the maximum of the lower bounds
1113 * - extract the increment from the stride of the current dimension
1114 * - construct the for condition either based on a list of upper bounds
1115 * or on a set of upper bound constraints.
1117 static __isl_give isl_ast_graft *set_for_node_expressions(
1118 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1119 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1120 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1122 isl_ast_node *node;
1124 if (!graft)
1125 return NULL;
1127 build = isl_ast_build_copy(build);
1129 node = graft->node;
1130 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1131 node->u.f.inc = for_inc(build);
1133 if (!node->u.f.init || !node->u.f.inc)
1134 graft = isl_ast_graft_free(graft);
1136 if (use_list)
1137 graft = set_for_cond_from_list(graft, upper_list, build);
1138 else
1139 graft = set_for_cond_from_set(graft, upper_set, build);
1141 isl_ast_build_free(build);
1143 return graft;
1146 /* Update "graft" based on "bounds" and "domain" for the generic,
1147 * non-degenerate, case.
1149 * "c_lower" and "c_upper" contain the lower and upper bounds
1150 * that the loop node should express.
1151 * "domain" is the subset of the intersection of the constraints
1152 * for which some code is executed.
1154 * There may be zero lower bounds or zero upper bounds in "constraints"
1155 * in case the list of constraints was created
1156 * based on the atomic option or based on separation with explicit bounds.
1157 * In that case, we use "domain" to derive lower and/or upper bounds.
1159 * We first compute a list of one or more lower bounds.
1161 * Then we decide if we want to express the condition as
1163 * iterator <= min(list of upper bounds)
1165 * or as a conjunction of constraints.
1167 * The set of enforced constraints is then computed either based on
1168 * a list of upper bounds or on a set of upper bound constraints.
1169 * We do not compute any enforced constraints if we were forced
1170 * to compute a lower or upper bound using exact_bound. The domains
1171 * of the resulting expressions may imply some bounds on outer dimensions
1172 * that we do not want to appear in the enforced constraints since
1173 * they are not actually enforced by the corresponding code.
1175 * Finally, we fill in the expressions of the for node.
1177 static __isl_give isl_ast_graft *refine_generic_bounds(
1178 __isl_take isl_ast_graft *graft,
1179 __isl_take isl_constraint_list *c_lower,
1180 __isl_take isl_constraint_list *c_upper,
1181 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1183 int depth;
1184 isl_ctx *ctx;
1185 isl_pw_aff_list *lower;
1186 int use_list;
1187 isl_set *upper_set = NULL;
1188 isl_pw_aff_list *upper_list = NULL;
1189 int n_lower, n_upper;
1191 if (!graft || !c_lower || !c_upper || !build)
1192 goto error;
1194 depth = isl_ast_build_get_depth(build);
1195 ctx = isl_ast_graft_get_ctx(graft);
1197 n_lower = isl_constraint_list_n_constraint(c_lower);
1198 n_upper = isl_constraint_list_n_constraint(c_upper);
1200 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1202 lower = lower_bounds(c_lower, depth, domain, build);
1204 if (use_list)
1205 upper_list = upper_bounds(c_upper, depth, domain, build);
1206 else if (n_upper > 0)
1207 upper_set = intersect_constraints(c_upper);
1208 else
1209 upper_set = isl_set_universe(isl_set_get_space(domain));
1211 if (n_lower == 0 || n_upper == 0)
1213 else if (use_list)
1214 graft = set_enforced_from_list(graft, lower, upper_list);
1215 else
1216 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1218 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1219 upper_set, build);
1221 isl_pw_aff_list_free(lower);
1222 isl_pw_aff_list_free(upper_list);
1223 isl_set_free(upper_set);
1224 isl_constraint_list_free(c_lower);
1225 isl_constraint_list_free(c_upper);
1227 return graft;
1228 error:
1229 isl_constraint_list_free(c_lower);
1230 isl_constraint_list_free(c_upper);
1231 return isl_ast_graft_free(graft);
1234 /* Internal data structure used inside count_constraints to keep
1235 * track of the number of constraints that are independent of dimension "pos",
1236 * the lower bounds in "pos" and the upper bounds in "pos".
1238 struct isl_ast_count_constraints_data {
1239 int pos;
1241 int n_indep;
1242 int n_lower;
1243 int n_upper;
1246 /* Increment data->n_indep, data->lower or data->upper depending
1247 * on whether "c" is independenct of dimensions data->pos,
1248 * a lower bound or an upper bound.
1250 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1252 struct isl_ast_count_constraints_data *data = user;
1254 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1255 data->n_lower++;
1256 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1257 data->n_upper++;
1258 else
1259 data->n_indep++;
1261 isl_constraint_free(c);
1263 return isl_stat_ok;
1266 /* Update "graft" based on "bounds" and "domain" for the generic,
1267 * non-degenerate, case.
1269 * "list" respresent the list of bounds that need to be encoded by
1270 * the for loop. Only the constraints that involve the iterator
1271 * are relevant here. The other constraints are taken care of by
1272 * the caller and are included in the generated constraints of "build".
1273 * "domain" is the subset of the intersection of the constraints
1274 * for which some code is executed.
1275 * "build" is the build in which graft->node was created.
1277 * We separate lower bounds, upper bounds and constraints that
1278 * are independent of the loop iterator.
1280 * The actual for loop bounds are generated in refine_generic_bounds.
1282 static __isl_give isl_ast_graft *refine_generic_split(
1283 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1284 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1286 struct isl_ast_count_constraints_data data;
1287 isl_constraint_list *lower;
1288 isl_constraint_list *upper;
1290 if (!list)
1291 return isl_ast_graft_free(graft);
1293 data.pos = isl_ast_build_get_depth(build);
1295 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1296 if (!list)
1297 return isl_ast_graft_free(graft);
1299 data.n_indep = data.n_lower = data.n_upper = 0;
1300 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1301 isl_constraint_list_free(list);
1302 return isl_ast_graft_free(graft);
1305 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1306 upper = isl_constraint_list_copy(lower);
1307 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1308 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1310 return refine_generic_bounds(graft, lower, upper, domain, build);
1313 /* Update "graft" based on "bounds" and "domain" for the generic,
1314 * non-degenerate, case.
1316 * "bounds" respresent the bounds that need to be encoded by
1317 * the for loop (or a guard around the for loop).
1318 * "domain" is the subset of "bounds" for which some code is executed.
1319 * "build" is the build in which graft->node was created.
1321 * We break up "bounds" into a list of constraints and continue with
1322 * refine_generic_split.
1324 static __isl_give isl_ast_graft *refine_generic(
1325 __isl_take isl_ast_graft *graft,
1326 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1327 __isl_keep isl_ast_build *build)
1329 isl_constraint_list *list;
1331 if (!build || !graft)
1332 return isl_ast_graft_free(graft);
1334 list = isl_basic_set_get_constraint_list(bounds);
1336 graft = refine_generic_split(graft, list, domain, build);
1338 return graft;
1341 /* Create a for node for the current level.
1343 * Mark the for node degenerate if "degenerate" is set.
1345 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1346 int degenerate)
1348 int depth;
1349 isl_id *id;
1350 isl_ast_node *node;
1352 if (!build)
1353 return NULL;
1355 depth = isl_ast_build_get_depth(build);
1356 id = isl_ast_build_get_iterator_id(build, depth);
1357 node = isl_ast_node_alloc_for(id);
1358 if (degenerate)
1359 node = isl_ast_node_for_mark_degenerate(node);
1361 return node;
1364 /* If the ast_build_exploit_nested_bounds option is set, then return
1365 * the constraints enforced by all elements in "list".
1366 * Otherwise, return the universe.
1368 static __isl_give isl_basic_set *extract_shared_enforced(
1369 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1371 isl_ctx *ctx;
1372 isl_space *space;
1374 if (!list)
1375 return NULL;
1377 ctx = isl_ast_graft_list_get_ctx(list);
1378 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1379 return isl_ast_graft_list_extract_shared_enforced(list, build);
1381 space = isl_ast_build_get_space(build, 1);
1382 return isl_basic_set_universe(space);
1385 /* Return the pending constraints of "build" that are not already taken
1386 * care of (by a combination of "enforced" and the generated constraints
1387 * of "build").
1389 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1390 __isl_keep isl_basic_set *enforced)
1392 isl_set *guard, *context;
1394 guard = isl_ast_build_get_pending(build);
1395 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1396 context = isl_set_intersect(context,
1397 isl_ast_build_get_generated(build));
1398 return isl_set_gist(guard, context);
1401 /* Create an AST node for the current dimension based on
1402 * the schedule domain "bounds" and return the node encapsulated
1403 * in an isl_ast_graft.
1405 * "executed" is the current inverse schedule, taking into account
1406 * the bounds in "bounds"
1407 * "domain" is the domain of "executed", with inner dimensions projected out.
1408 * It may be a strict subset of "bounds" in case "bounds" was created
1409 * based on the atomic option or based on separation with explicit bounds.
1411 * "domain" may satisfy additional equalities that result
1412 * from intersecting "executed" with "bounds" in add_node.
1413 * It may also satisfy some global constraints that were dropped out because
1414 * we performed separation with explicit bounds.
1415 * The very first step is then to copy these constraints to "bounds".
1417 * Since we may be calling before_each_for and after_each_for
1418 * callbacks, we record the current inverse schedule in the build.
1420 * We consider three builds,
1421 * "build" is the one in which the current level is created,
1422 * "body_build" is the build in which the next level is created,
1423 * "sub_build" is essentially the same as "body_build", except that
1424 * the depth has not been increased yet.
1426 * "build" already contains information (in strides and offsets)
1427 * about the strides at the current level, but this information is not
1428 * reflected in the build->domain.
1429 * We first add this information and the "bounds" to the sub_build->domain.
1430 * isl_ast_build_set_loop_bounds adds the stride information and
1431 * checks whether the current dimension attains
1432 * only a single value and whether this single value can be represented using
1433 * a single affine expression.
1434 * In the first case, the current level is considered "degenerate".
1435 * In the second, sub-case, the current level is considered "eliminated".
1436 * Eliminated levels don't need to be reflected in the AST since we can
1437 * simply plug in the affine expression. For degenerate, but non-eliminated,
1438 * levels, we do introduce a for node, but mark is as degenerate so that
1439 * it can be printed as an assignment of the single value to the loop
1440 * "iterator".
1442 * If the current level is eliminated, we explicitly plug in the value
1443 * for the current level found by isl_ast_build_set_loop_bounds in the
1444 * inverse schedule. This ensures that if we are working on a slice
1445 * of the domain based on information available in the inverse schedule
1446 * and the build domain, that then this information is also reflected
1447 * in the inverse schedule. This operation also eliminates the current
1448 * dimension from the inverse schedule making sure no inner dimensions depend
1449 * on the current dimension. Otherwise, we create a for node, marking
1450 * it degenerate if appropriate. The initial for node is still incomplete
1451 * and will be completed in either refine_degenerate or refine_generic.
1453 * We then generate a sequence of grafts for the next level,
1454 * create a surrounding graft for the current level and insert
1455 * the for node we created (if the current level is not eliminated).
1456 * Before creating a graft for the current level, we first extract
1457 * hoistable constraints from the child guards and combine them
1458 * with the pending constraints in the build. These constraints
1459 * are used to simplify the child guards and then added to the guard
1460 * of the current graft to ensure that they will be generated.
1461 * If the hoisted guard is a disjunction, then we use it directly
1462 * to gist the guards on the children before intersect it with the
1463 * pending constraints. We do so because this disjunction is typically
1464 * identical to the guards on the children such that these guards
1465 * can be effectively removed completely. After the intersection,
1466 * the gist operation would have a harder time figuring this out.
1468 * Finally, we set the bounds of the for loop in either
1469 * refine_degenerate or refine_generic.
1470 * We do so in a context where the pending constraints of the build
1471 * have been replaced by the guard of the current graft.
1473 static __isl_give isl_ast_graft *create_node_scaled(
1474 __isl_take isl_union_map *executed,
1475 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1476 __isl_take isl_ast_build *build)
1478 int depth;
1479 int degenerate;
1480 isl_bool eliminated;
1481 isl_basic_set *hull;
1482 isl_basic_set *enforced;
1483 isl_set *guard, *hoisted;
1484 isl_ast_node *node = NULL;
1485 isl_ast_graft *graft;
1486 isl_ast_graft_list *children;
1487 isl_ast_build *sub_build;
1488 isl_ast_build *body_build;
1490 domain = isl_ast_build_eliminate_divs(build, domain);
1491 domain = isl_set_detect_equalities(domain);
1492 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1493 bounds = isl_basic_set_intersect(bounds, hull);
1494 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1496 depth = isl_ast_build_get_depth(build);
1497 sub_build = isl_ast_build_copy(build);
1498 bounds = isl_basic_set_remove_redundancies(bounds);
1499 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1500 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1501 isl_basic_set_copy(bounds));
1502 degenerate = isl_ast_build_has_value(sub_build);
1503 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1504 if (degenerate < 0 || eliminated < 0)
1505 executed = isl_union_map_free(executed);
1506 if (!degenerate)
1507 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1508 sub_build = isl_ast_build_set_pending_generated(sub_build,
1509 isl_basic_set_copy(bounds));
1510 if (eliminated)
1511 executed = plug_in_values(executed, sub_build);
1512 else
1513 node = create_for(build, degenerate);
1515 body_build = isl_ast_build_copy(sub_build);
1516 body_build = isl_ast_build_increase_depth(body_build);
1517 if (!eliminated)
1518 node = before_each_for(node, body_build);
1519 children = generate_next_level(executed,
1520 isl_ast_build_copy(body_build));
1522 enforced = extract_shared_enforced(children, build);
1523 guard = extract_pending(sub_build, enforced);
1524 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1525 if (isl_set_n_basic_set(hoisted) > 1)
1526 children = isl_ast_graft_list_gist_guards(children,
1527 isl_set_copy(hoisted));
1528 guard = isl_set_intersect(guard, hoisted);
1529 if (!eliminated)
1530 guard = add_implied_guards(guard, degenerate, bounds, build);
1532 graft = isl_ast_graft_alloc_from_children(children,
1533 isl_set_copy(guard), enforced, build, sub_build);
1535 if (!eliminated) {
1536 isl_ast_build *for_build;
1538 graft = isl_ast_graft_insert_for(graft, node);
1539 for_build = isl_ast_build_copy(build);
1540 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1541 isl_set_copy(guard));
1542 if (degenerate)
1543 graft = refine_degenerate(graft, for_build, sub_build);
1544 else
1545 graft = refine_generic(graft, bounds,
1546 domain, for_build);
1547 isl_ast_build_free(for_build);
1549 isl_set_free(guard);
1550 if (!eliminated)
1551 graft = after_each_for(graft, body_build);
1553 isl_ast_build_free(body_build);
1554 isl_ast_build_free(sub_build);
1555 isl_ast_build_free(build);
1556 isl_basic_set_free(bounds);
1557 isl_set_free(domain);
1559 return graft;
1562 /* Internal data structure for checking if all constraints involving
1563 * the input dimension "depth" are such that the other coefficients
1564 * are multiples of "m", reducing "m" if they are not.
1565 * If "m" is reduced all the way down to "1", then the check has failed
1566 * and we break out of the iteration.
1568 struct isl_check_scaled_data {
1569 int depth;
1570 isl_val *m;
1573 /* If constraint "c" involves the input dimension data->depth,
1574 * then make sure that all the other coefficients are multiples of data->m,
1575 * reducing data->m if needed.
1576 * Break out of the iteration if data->m has become equal to "1".
1578 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1579 void *user)
1581 struct isl_check_scaled_data *data = user;
1582 int i, j, n;
1583 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1584 isl_dim_div };
1586 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1587 isl_constraint_free(c);
1588 return isl_stat_ok;
1591 for (i = 0; i < 4; ++i) {
1592 n = isl_constraint_dim(c, t[i]);
1593 for (j = 0; j < n; ++j) {
1594 isl_val *d;
1596 if (t[i] == isl_dim_in && j == data->depth)
1597 continue;
1598 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1599 continue;
1600 d = isl_constraint_get_coefficient_val(c, t[i], j);
1601 data->m = isl_val_gcd(data->m, d);
1602 if (isl_val_is_one(data->m))
1603 break;
1605 if (j < n)
1606 break;
1609 isl_constraint_free(c);
1611 return i < 4 ? isl_stat_error : isl_stat_ok;
1614 /* For each constraint of "bmap" that involves the input dimension data->depth,
1615 * make sure that all the other coefficients are multiples of data->m,
1616 * reducing data->m if needed.
1617 * Break out of the iteration if data->m has become equal to "1".
1619 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1620 void *user)
1622 isl_stat r;
1624 r = isl_basic_map_foreach_constraint(bmap,
1625 &constraint_check_scaled, user);
1626 isl_basic_map_free(bmap);
1628 return r;
1631 /* For each constraint of "map" that involves the input dimension data->depth,
1632 * make sure that all the other coefficients are multiples of data->m,
1633 * reducing data->m if needed.
1634 * Break out of the iteration if data->m has become equal to "1".
1636 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1638 isl_stat r;
1640 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1641 isl_map_free(map);
1643 return r;
1646 /* Create an AST node for the current dimension based on
1647 * the schedule domain "bounds" and return the node encapsulated
1648 * in an isl_ast_graft.
1650 * "executed" is the current inverse schedule, taking into account
1651 * the bounds in "bounds"
1652 * "domain" is the domain of "executed", with inner dimensions projected out.
1655 * Before moving on to the actual AST node construction in create_node_scaled,
1656 * we first check if the current dimension is strided and if we can scale
1657 * down this stride. Note that we only do this if the ast_build_scale_strides
1658 * option is set.
1660 * In particular, let the current dimension take on values
1662 * f + s a
1664 * with a an integer. We check if we can find an integer m that (obviously)
1665 * divides both f and s.
1667 * If so, we check if the current dimension only appears in constraints
1668 * where the coefficients of the other variables are multiples of m.
1669 * We perform this extra check to avoid the risk of introducing
1670 * divisions by scaling down the current dimension.
1672 * If so, we scale the current dimension down by a factor of m.
1673 * That is, we plug in
1675 * i = m i' (1)
1677 * Note that in principle we could always scale down strided loops
1678 * by plugging in
1680 * i = f + s i'
1682 * but this may result in i' taking on larger values than the original i,
1683 * due to the shift by "f".
1684 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1686 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1687 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1688 __isl_take isl_ast_build *build)
1690 struct isl_check_scaled_data data;
1691 isl_ctx *ctx;
1692 isl_aff *offset;
1693 isl_val *d;
1695 ctx = isl_ast_build_get_ctx(build);
1696 if (!isl_options_get_ast_build_scale_strides(ctx))
1697 return create_node_scaled(executed, bounds, domain, build);
1699 data.depth = isl_ast_build_get_depth(build);
1700 if (!isl_ast_build_has_stride(build, data.depth))
1701 return create_node_scaled(executed, bounds, domain, build);
1703 offset = isl_ast_build_get_offset(build, data.depth);
1704 data.m = isl_ast_build_get_stride(build, data.depth);
1705 if (!data.m)
1706 offset = isl_aff_free(offset);
1707 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1708 d = isl_aff_get_denominator_val(offset);
1709 if (!d)
1710 executed = isl_union_map_free(executed);
1712 if (executed && isl_val_is_divisible_by(data.m, d))
1713 data.m = isl_val_div(data.m, d);
1714 else {
1715 data.m = isl_val_set_si(data.m, 1);
1716 isl_val_free(d);
1719 if (!isl_val_is_one(data.m)) {
1720 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1721 &data) < 0 &&
1722 !isl_val_is_one(data.m))
1723 executed = isl_union_map_free(executed);
1726 if (!isl_val_is_one(data.m)) {
1727 isl_space *space;
1728 isl_multi_aff *ma;
1729 isl_aff *aff;
1730 isl_map *map;
1731 isl_union_map *umap;
1733 space = isl_ast_build_get_space(build, 1);
1734 space = isl_space_map_from_set(space);
1735 ma = isl_multi_aff_identity(space);
1736 aff = isl_multi_aff_get_aff(ma, data.depth);
1737 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1738 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1740 bounds = isl_basic_set_preimage_multi_aff(bounds,
1741 isl_multi_aff_copy(ma));
1742 domain = isl_set_preimage_multi_aff(domain,
1743 isl_multi_aff_copy(ma));
1744 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1745 umap = isl_union_map_from_map(map);
1746 executed = isl_union_map_apply_domain(executed,
1747 isl_union_map_copy(umap));
1748 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1749 umap);
1751 isl_aff_free(offset);
1752 isl_val_free(data.m);
1754 return create_node_scaled(executed, bounds, domain, build);
1757 /* Add the basic set to the list that "user" points to.
1759 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1761 isl_basic_set_list **list = user;
1763 *list = isl_basic_set_list_add(*list, bset);
1765 return isl_stat_ok;
1768 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1770 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1771 __isl_take isl_set *set)
1773 int n;
1774 isl_ctx *ctx;
1775 isl_basic_set_list *list;
1777 if (!set)
1778 return NULL;
1780 ctx = isl_set_get_ctx(set);
1782 n = isl_set_n_basic_set(set);
1783 list = isl_basic_set_list_alloc(ctx, n);
1784 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1785 list = isl_basic_set_list_free(list);
1787 isl_set_free(set);
1788 return list;
1791 /* Generate code for the schedule domain "bounds"
1792 * and add the result to "list".
1794 * We mainly detect strides here and check if the bounds do not
1795 * conflict with the current build domain
1796 * and then pass over control to create_node.
1798 * "bounds" reflects the bounds on the current dimension and possibly
1799 * some extra conditions on outer dimensions.
1800 * It does not, however, include any divs involving the current dimension,
1801 * so it does not capture any stride constraints.
1802 * We therefore need to compute that part of the schedule domain that
1803 * intersects with "bounds" and derive the strides from the result.
1805 static __isl_give isl_ast_graft_list *add_node(
1806 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1807 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1809 isl_ast_graft *graft;
1810 isl_set *domain = NULL;
1811 isl_union_set *uset;
1812 int empty, disjoint;
1814 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1815 executed = isl_union_map_intersect_domain(executed, uset);
1816 empty = isl_union_map_is_empty(executed);
1817 if (empty < 0)
1818 goto error;
1819 if (empty)
1820 goto done;
1822 uset = isl_union_map_domain(isl_union_map_copy(executed));
1823 domain = isl_set_from_union_set(uset);
1824 domain = isl_ast_build_specialize(build, domain);
1826 domain = isl_set_compute_divs(domain);
1827 domain = isl_ast_build_eliminate_inner(build, domain);
1828 disjoint = isl_set_is_disjoint(domain, build->domain);
1829 if (disjoint < 0)
1830 goto error;
1831 if (disjoint)
1832 goto done;
1834 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1836 graft = create_node(executed, bounds, domain,
1837 isl_ast_build_copy(build));
1838 list = isl_ast_graft_list_add(list, graft);
1839 isl_ast_build_free(build);
1840 return list;
1841 error:
1842 list = isl_ast_graft_list_free(list);
1843 done:
1844 isl_set_free(domain);
1845 isl_basic_set_free(bounds);
1846 isl_union_map_free(executed);
1847 isl_ast_build_free(build);
1848 return list;
1851 /* Does any element of i follow or coincide with any element of j
1852 * at the current depth for equal values of the outer dimensions?
1854 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1855 __isl_keep isl_basic_set *j, void *user)
1857 int depth = *(int *) user;
1858 isl_basic_map *test;
1859 isl_bool empty;
1860 int l;
1862 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1863 isl_basic_set_copy(j));
1864 for (l = 0; l < depth; ++l)
1865 test = isl_basic_map_equate(test, isl_dim_in, l,
1866 isl_dim_out, l);
1867 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1868 isl_dim_out, depth);
1869 empty = isl_basic_map_is_empty(test);
1870 isl_basic_map_free(test);
1872 return isl_bool_not(empty);
1875 /* Split up each element of "list" into a part that is related to "bset"
1876 * according to "gt" and a part that is not.
1877 * Return a list that consist of "bset" and all the pieces.
1879 static __isl_give isl_basic_set_list *add_split_on(
1880 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1881 __isl_keep isl_basic_map *gt)
1883 int i, n;
1884 isl_basic_set_list *res;
1886 if (!list)
1887 bset = isl_basic_set_free(bset);
1889 gt = isl_basic_map_copy(gt);
1890 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1891 n = isl_basic_set_list_n_basic_set(list);
1892 res = isl_basic_set_list_from_basic_set(bset);
1893 for (i = 0; res && i < n; ++i) {
1894 isl_basic_set *bset;
1895 isl_set *set1, *set2;
1896 isl_basic_map *bmap;
1897 int empty;
1899 bset = isl_basic_set_list_get_basic_set(list, i);
1900 bmap = isl_basic_map_copy(gt);
1901 bmap = isl_basic_map_intersect_range(bmap, bset);
1902 bset = isl_basic_map_range(bmap);
1903 empty = isl_basic_set_is_empty(bset);
1904 if (empty < 0)
1905 res = isl_basic_set_list_free(res);
1906 if (empty) {
1907 isl_basic_set_free(bset);
1908 bset = isl_basic_set_list_get_basic_set(list, i);
1909 res = isl_basic_set_list_add(res, bset);
1910 continue;
1913 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1914 set1 = isl_set_from_basic_set(bset);
1915 bset = isl_basic_set_list_get_basic_set(list, i);
1916 set2 = isl_set_from_basic_set(bset);
1917 set1 = isl_set_subtract(set2, set1);
1918 set1 = isl_set_make_disjoint(set1);
1920 res = isl_basic_set_list_concat(res,
1921 isl_basic_set_list_from_set(set1));
1923 isl_basic_map_free(gt);
1924 isl_basic_set_list_free(list);
1925 return res;
1928 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1929 __isl_keep isl_basic_set_list *domain_list,
1930 __isl_keep isl_union_map *executed,
1931 __isl_keep isl_ast_build *build);
1933 /* Internal data structure for add_nodes.
1935 * "executed" and "build" are extra arguments to be passed to add_node.
1936 * "list" collects the results.
1938 struct isl_add_nodes_data {
1939 isl_union_map *executed;
1940 isl_ast_build *build;
1942 isl_ast_graft_list *list;
1945 /* Generate code for the schedule domains in "scc"
1946 * and add the results to "list".
1948 * The domains in "scc" form a strongly connected component in the ordering.
1949 * If the number of domains in "scc" is larger than 1, then this means
1950 * that we cannot determine a valid ordering for the domains in the component.
1951 * This should be fairly rare because the individual domains
1952 * have been made disjoint first.
1953 * The problem is that the domains may be integrally disjoint but not
1954 * rationally disjoint. For example, we may have domains
1956 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1958 * These two domains have an empty intersection, but their rational
1959 * relaxations do intersect. It is impossible to order these domains
1960 * in the second dimension because the first should be ordered before
1961 * the second for outer dimension equal to 0, while it should be ordered
1962 * after for outer dimension equal to 1.
1964 * This may happen in particular in case of unrolling since the domain
1965 * of each slice is replaced by its simple hull.
1967 * For each basic set i in "scc" and for each of the following basic sets j,
1968 * we split off that part of the basic set i that shares the outer dimensions
1969 * with j and lies before j in the current dimension.
1970 * We collect all the pieces in a new list that replaces "scc".
1972 * While the elements in "scc" should be disjoint, we double-check
1973 * this property to avoid running into an infinite recursion in case
1974 * they intersect due to some internal error.
1976 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1978 struct isl_add_nodes_data *data = user;
1979 int i, n, depth;
1980 isl_basic_set *bset, *first;
1981 isl_basic_set_list *list;
1982 isl_space *space;
1983 isl_basic_map *gt;
1985 n = isl_basic_set_list_n_basic_set(scc);
1986 bset = isl_basic_set_list_get_basic_set(scc, 0);
1987 if (n == 1) {
1988 isl_basic_set_list_free(scc);
1989 data->list = add_node(data->list,
1990 isl_union_map_copy(data->executed), bset,
1991 isl_ast_build_copy(data->build));
1992 return data->list ? isl_stat_ok : isl_stat_error;
1995 depth = isl_ast_build_get_depth(data->build);
1996 space = isl_basic_set_get_space(bset);
1997 space = isl_space_map_from_set(space);
1998 gt = isl_basic_map_universe(space);
1999 for (i = 0; i < depth; ++i)
2000 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
2001 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
2003 first = isl_basic_set_copy(bset);
2004 list = isl_basic_set_list_from_basic_set(bset);
2005 for (i = 1; i < n; ++i) {
2006 int disjoint;
2008 bset = isl_basic_set_list_get_basic_set(scc, i);
2010 disjoint = isl_basic_set_is_disjoint(bset, first);
2011 if (disjoint < 0)
2012 list = isl_basic_set_list_free(list);
2013 else if (!disjoint)
2014 isl_die(isl_basic_set_list_get_ctx(scc),
2015 isl_error_internal,
2016 "basic sets in scc are assumed to be disjoint",
2017 list = isl_basic_set_list_free(list));
2019 list = add_split_on(list, bset, gt);
2021 isl_basic_set_free(first);
2022 isl_basic_map_free(gt);
2023 isl_basic_set_list_free(scc);
2024 scc = list;
2025 data->list = isl_ast_graft_list_concat(data->list,
2026 generate_sorted_domains(scc, data->executed, data->build));
2027 isl_basic_set_list_free(scc);
2029 return data->list ? isl_stat_ok : isl_stat_error;
2032 /* Sort the domains in "domain_list" according to the execution order
2033 * at the current depth (for equal values of the outer dimensions),
2034 * generate code for each of them, collecting the results in a list.
2035 * If no code is generated (because the intersection of the inverse schedule
2036 * with the domains turns out to be empty), then an empty list is returned.
2038 * The caller is responsible for ensuring that the basic sets in "domain_list"
2039 * are pair-wise disjoint. It can, however, in principle happen that
2040 * two basic sets should be ordered one way for one value of the outer
2041 * dimensions and the other way for some other value of the outer dimensions.
2042 * We therefore play safe and look for strongly connected components.
2043 * The function add_nodes takes care of handling non-trivial components.
2045 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2046 __isl_keep isl_basic_set_list *domain_list,
2047 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2049 isl_ctx *ctx;
2050 struct isl_add_nodes_data data;
2051 int depth;
2052 int n;
2054 if (!domain_list)
2055 return NULL;
2057 ctx = isl_basic_set_list_get_ctx(domain_list);
2058 n = isl_basic_set_list_n_basic_set(domain_list);
2059 data.list = isl_ast_graft_list_alloc(ctx, n);
2060 if (n == 0)
2061 return data.list;
2062 if (n == 1)
2063 return add_node(data.list, isl_union_map_copy(executed),
2064 isl_basic_set_list_get_basic_set(domain_list, 0),
2065 isl_ast_build_copy(build));
2067 depth = isl_ast_build_get_depth(build);
2068 data.executed = executed;
2069 data.build = build;
2070 if (isl_basic_set_list_foreach_scc(domain_list,
2071 &domain_follows_at_depth, &depth,
2072 &add_nodes, &data) < 0)
2073 data.list = isl_ast_graft_list_free(data.list);
2075 return data.list;
2078 /* Do i and j share any values for the outer dimensions?
2080 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2081 __isl_keep isl_basic_set *j, void *user)
2083 int depth = *(int *) user;
2084 isl_basic_map *test;
2085 isl_bool empty;
2086 int l;
2088 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2089 isl_basic_set_copy(j));
2090 for (l = 0; l < depth; ++l)
2091 test = isl_basic_map_equate(test, isl_dim_in, l,
2092 isl_dim_out, l);
2093 empty = isl_basic_map_is_empty(test);
2094 isl_basic_map_free(test);
2096 return isl_bool_not(empty);
2099 /* Internal data structure for generate_sorted_domains_wrap.
2101 * "n" is the total number of basic sets
2102 * "executed" and "build" are extra arguments to be passed
2103 * to generate_sorted_domains.
2105 * "single" is set to 1 by generate_sorted_domains_wrap if there
2106 * is only a single component.
2107 * "list" collects the results.
2109 struct isl_ast_generate_parallel_domains_data {
2110 int n;
2111 isl_union_map *executed;
2112 isl_ast_build *build;
2114 int single;
2115 isl_ast_graft_list *list;
2118 /* Call generate_sorted_domains on "scc", fuse the result into a list
2119 * with either zero or one graft and collect the these single element
2120 * lists into data->list.
2122 * If there is only one component, i.e., if the number of basic sets
2123 * in the current component is equal to the total number of basic sets,
2124 * then data->single is set to 1 and the result of generate_sorted_domains
2125 * is not fused.
2127 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2128 void *user)
2130 struct isl_ast_generate_parallel_domains_data *data = user;
2131 isl_ast_graft_list *list;
2133 list = generate_sorted_domains(scc, data->executed, data->build);
2134 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2135 if (!data->single)
2136 list = isl_ast_graft_list_fuse(list, data->build);
2137 if (!data->list)
2138 data->list = list;
2139 else
2140 data->list = isl_ast_graft_list_concat(data->list, list);
2142 isl_basic_set_list_free(scc);
2143 if (!data->list)
2144 return isl_stat_error;
2146 return isl_stat_ok;
2149 /* Look for any (weakly connected) components in the "domain_list"
2150 * of domains that share some values of the outer dimensions.
2151 * That is, domains in different components do not share any values
2152 * of the outer dimensions. This means that these components
2153 * can be freely reordered.
2154 * Within each of the components, we sort the domains according
2155 * to the execution order at the current depth.
2157 * If there is more than one component, then generate_sorted_domains_wrap
2158 * fuses the result of each call to generate_sorted_domains
2159 * into a list with either zero or one graft and collects these (at most)
2160 * single element lists into a bigger list. This means that the elements of the
2161 * final list can be freely reordered. In particular, we sort them
2162 * according to an arbitrary but fixed ordering to ease merging of
2163 * graft lists from different components.
2165 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2166 __isl_keep isl_basic_set_list *domain_list,
2167 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2169 int depth;
2170 struct isl_ast_generate_parallel_domains_data data;
2172 if (!domain_list)
2173 return NULL;
2175 data.n = isl_basic_set_list_n_basic_set(domain_list);
2176 if (data.n <= 1)
2177 return generate_sorted_domains(domain_list, executed, build);
2179 depth = isl_ast_build_get_depth(build);
2180 data.list = NULL;
2181 data.executed = executed;
2182 data.build = build;
2183 data.single = 0;
2184 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2185 &generate_sorted_domains_wrap,
2186 &data) < 0)
2187 data.list = isl_ast_graft_list_free(data.list);
2189 if (!data.single)
2190 data.list = isl_ast_graft_list_sort_guard(data.list);
2192 return data.list;
2195 /* Internal data for separate_domain.
2197 * "explicit" is set if we only want to use explicit bounds.
2199 * "domain" collects the separated domains.
2201 struct isl_separate_domain_data {
2202 isl_ast_build *build;
2203 int explicit;
2204 isl_set *domain;
2207 /* Extract implicit bounds on the current dimension for the executed "map".
2209 * The domain of "map" may involve inner dimensions, so we
2210 * need to eliminate them.
2212 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2213 __isl_keep isl_ast_build *build)
2215 isl_set *domain;
2217 domain = isl_map_domain(map);
2218 domain = isl_ast_build_eliminate(build, domain);
2220 return domain;
2223 /* Extract explicit bounds on the current dimension for the executed "map".
2225 * Rather than eliminating the inner dimensions as in implicit_bounds,
2226 * we simply drop any constraints involving those inner dimensions.
2227 * The idea is that most bounds that are implied by constraints on the
2228 * inner dimensions will be enforced by for loops and not by explicit guards.
2229 * There is then no need to separate along those bounds.
2231 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2232 __isl_keep isl_ast_build *build)
2234 isl_set *domain;
2235 int depth, dim;
2237 dim = isl_map_dim(map, isl_dim_out);
2238 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2240 domain = isl_map_domain(map);
2241 depth = isl_ast_build_get_depth(build);
2242 dim = isl_set_dim(domain, isl_dim_set);
2243 domain = isl_set_detect_equalities(domain);
2244 domain = isl_set_drop_constraints_involving_dims(domain,
2245 isl_dim_set, depth + 1, dim - (depth + 1));
2246 domain = isl_set_remove_divs_involving_dims(domain,
2247 isl_dim_set, depth, 1);
2248 domain = isl_set_remove_unknown_divs(domain);
2250 return domain;
2253 /* Split data->domain into pieces that intersect with the range of "map"
2254 * and pieces that do not intersect with the range of "map"
2255 * and then add that part of the range of "map" that does not intersect
2256 * with data->domain.
2258 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2260 struct isl_separate_domain_data *data = user;
2261 isl_set *domain;
2262 isl_set *d1, *d2;
2264 if (data->explicit)
2265 domain = explicit_bounds(map, data->build);
2266 else
2267 domain = implicit_bounds(map, data->build);
2269 domain = isl_set_coalesce(domain);
2270 domain = isl_set_make_disjoint(domain);
2271 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2272 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2273 data->domain = isl_set_intersect(data->domain, domain);
2274 data->domain = isl_set_union(data->domain, d1);
2275 data->domain = isl_set_union(data->domain, d2);
2277 return isl_stat_ok;
2280 /* Separate the schedule domains of "executed".
2282 * That is, break up the domain of "executed" into basic sets,
2283 * such that for each basic set S, every element in S is associated with
2284 * the same domain spaces.
2286 * "space" is the (single) domain space of "executed".
2288 static __isl_give isl_set *separate_schedule_domains(
2289 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2290 __isl_keep isl_ast_build *build)
2292 struct isl_separate_domain_data data = { build };
2293 isl_ctx *ctx;
2295 ctx = isl_ast_build_get_ctx(build);
2296 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2297 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2298 data.domain = isl_set_empty(space);
2299 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2300 data.domain = isl_set_free(data.domain);
2302 isl_union_map_free(executed);
2303 return data.domain;
2306 /* Temporary data used during the search for a lower bound for unrolling.
2308 * "build" is the build in which the unrolling will be performed
2309 * "domain" is the original set for which to find a lower bound
2310 * "depth" is the dimension for which to find a lower boudn
2311 * "expansion" is the expansion that needs to be applied to "domain"
2312 * in the unrolling that will be performed
2314 * "lower" is the best lower bound found so far. It is NULL if we have not
2315 * found any yet.
2316 * "n" is the corresponding size. If lower is NULL, then the value of n
2317 * is undefined.
2318 * "n_div" is the maximal number of integer divisions in the first
2319 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2320 * been computed yet.
2322 struct isl_find_unroll_data {
2323 isl_ast_build *build;
2324 isl_set *domain;
2325 int depth;
2326 isl_basic_map *expansion;
2328 isl_aff *lower;
2329 int *n;
2330 int n_div;
2333 /* Return the constraint
2335 * i_"depth" = aff + offset
2337 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2338 int offset)
2340 aff = isl_aff_copy(aff);
2341 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2342 aff = isl_aff_add_constant_si(aff, offset);
2343 return isl_equality_from_aff(aff);
2346 /* Update *user to the number of integer divsions in the first element
2347 * of "ma", if it is larger than the current value.
2349 static isl_stat update_n_div(__isl_take isl_set *set,
2350 __isl_take isl_multi_aff *ma, void *user)
2352 isl_aff *aff;
2353 int *n = user;
2354 int n_div;
2356 aff = isl_multi_aff_get_aff(ma, 0);
2357 n_div = isl_aff_dim(aff, isl_dim_div);
2358 isl_aff_free(aff);
2359 isl_multi_aff_free(ma);
2360 isl_set_free(set);
2362 if (n_div > *n)
2363 *n = n_div;
2365 return aff ? isl_stat_ok : isl_stat_error;
2368 /* Get the number of integer divisions in the expression for the iterator
2369 * value at the first slice in the unrolling based on lower bound "lower",
2370 * taking into account the expansion that needs to be performed on this slice.
2372 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2373 __isl_keep isl_aff *lower)
2375 isl_constraint *c;
2376 isl_set *set;
2377 isl_map *it_map, *expansion;
2378 isl_pw_multi_aff *pma;
2379 int n;
2381 c = at_offset(data->depth, lower, 0);
2382 set = isl_set_copy(data->domain);
2383 set = isl_set_add_constraint(set, c);
2384 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2385 set = isl_set_apply(set, expansion);
2386 it_map = isl_ast_build_map_to_iterator(data->build, set);
2387 pma = isl_pw_multi_aff_from_map(it_map);
2388 n = 0;
2389 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2390 n = -1;
2391 isl_pw_multi_aff_free(pma);
2393 return n;
2396 /* Is the lower bound "lower" with corresponding iteration count "n"
2397 * better than the one stored in "data"?
2398 * If there is no upper bound on the iteration count ("n" is infinity) or
2399 * if the count is too large, then we cannot use this lower bound.
2400 * Otherwise, if there was no previous lower bound or
2401 * if the iteration count of the new lower bound is smaller than
2402 * the iteration count of the previous lower bound, then we consider
2403 * the new lower bound to be better.
2404 * If the iteration count is the same, then compare the number
2405 * of integer divisions that would be needed to express
2406 * the iterator value at the first slice in the unrolling
2407 * according to the lower bound. If we end up computing this
2408 * number, then store the lowest value in data->n_div.
2410 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2411 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2413 int cmp;
2414 int n_div;
2416 if (!n)
2417 return -1;
2418 if (isl_val_is_infty(n))
2419 return 0;
2420 if (isl_val_cmp_si(n, INT_MAX) > 0)
2421 return 0;
2422 if (!data->lower)
2423 return 1;
2424 cmp = isl_val_cmp_si(n, *data->n);
2425 if (cmp < 0)
2426 return 1;
2427 if (cmp > 0)
2428 return 0;
2429 if (data->n_div < 0)
2430 data->n_div = get_expanded_n_div(data, data->lower);
2431 if (data->n_div < 0)
2432 return -1;
2433 if (data->n_div == 0)
2434 return 0;
2435 n_div = get_expanded_n_div(data, lower);
2436 if (n_div < 0)
2437 return -1;
2438 if (n_div >= data->n_div)
2439 return 0;
2440 data->n_div = n_div;
2442 return 1;
2445 /* Check if we can use "c" as a lower bound and if it is better than
2446 * any previously found lower bound.
2448 * If "c" does not involve the dimension at the current depth,
2449 * then we cannot use it.
2450 * Otherwise, let "c" be of the form
2452 * i >= f(j)/a
2454 * We compute the maximal value of
2456 * -ceil(f(j)/a)) + i + 1
2458 * over the domain. If there is such a value "n", then we know
2460 * -ceil(f(j)/a)) + i + 1 <= n
2462 * or
2464 * i < ceil(f(j)/a)) + n
2466 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2467 * We just need to check if we have found any lower bound before and
2468 * if the new lower bound is better (smaller n or fewer integer divisions)
2469 * than the previously found lower bounds.
2471 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2472 __isl_keep isl_constraint *c)
2474 isl_aff *aff, *lower;
2475 isl_val *max;
2476 int better;
2478 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2479 return isl_stat_ok;
2481 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2482 lower = isl_aff_ceil(lower);
2483 aff = isl_aff_copy(lower);
2484 aff = isl_aff_neg(aff);
2485 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2486 aff = isl_aff_add_constant_si(aff, 1);
2487 max = isl_set_max_val(data->domain, aff);
2488 isl_aff_free(aff);
2490 better = is_better_lower_bound(data, lower, max);
2491 if (better < 0 || !better) {
2492 isl_val_free(max);
2493 isl_aff_free(lower);
2494 return better < 0 ? isl_stat_error : isl_stat_ok;
2497 isl_aff_free(data->lower);
2498 data->lower = lower;
2499 *data->n = isl_val_get_num_si(max);
2500 isl_val_free(max);
2502 return isl_stat_ok;
2505 /* Check if we can use "c" as a lower bound and if it is better than
2506 * any previously found lower bound.
2508 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2510 struct isl_find_unroll_data *data;
2511 isl_stat r;
2513 data = (struct isl_find_unroll_data *) user;
2514 r = update_unrolling_lower_bound(data, c);
2515 isl_constraint_free(c);
2517 return r;
2520 /* Look for a lower bound l(i) on the dimension at "depth"
2521 * and a size n such that "domain" is a subset of
2523 * { [i] : l(i) <= i_d < l(i) + n }
2525 * where d is "depth" and l(i) depends only on earlier dimensions.
2526 * Furthermore, try and find a lower bound such that n is as small as possible.
2527 * In particular, "n" needs to be finite.
2528 * "build" is the build in which the unrolling will be performed.
2529 * "expansion" is the expansion that needs to be applied to "domain"
2530 * in the unrolling that will be performed.
2532 * Inner dimensions have been eliminated from "domain" by the caller.
2534 * We first construct a collection of lower bounds on the input set
2535 * by computing its simple hull. We then iterate through them,
2536 * discarding those that we cannot use (either because they do not
2537 * involve the dimension at "depth" or because they have no corresponding
2538 * upper bound, meaning that "n" would be unbounded) and pick out the
2539 * best from the remaining ones.
2541 * If we cannot find a suitable lower bound, then we consider that
2542 * to be an error.
2544 static __isl_give isl_aff *find_unroll_lower_bound(
2545 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2546 int depth, __isl_keep isl_basic_map *expansion, int *n)
2548 struct isl_find_unroll_data data =
2549 { build, domain, depth, expansion, NULL, n, -1 };
2550 isl_basic_set *hull;
2552 hull = isl_set_simple_hull(isl_set_copy(domain));
2554 if (isl_basic_set_foreach_constraint(hull,
2555 &constraint_find_unroll, &data) < 0)
2556 goto error;
2558 isl_basic_set_free(hull);
2560 if (!data.lower)
2561 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2562 "cannot find lower bound for unrolling", return NULL);
2564 return data.lower;
2565 error:
2566 isl_basic_set_free(hull);
2567 return isl_aff_free(data.lower);
2570 /* Call "fn" on each iteration of the current dimension of "domain".
2571 * If "init" is not NULL, then it is called with the number of
2572 * iterations before any call to "fn".
2573 * Return -1 on failure.
2575 * Since we are going to be iterating over the individual values,
2576 * we first check if there are any strides on the current dimension.
2577 * If there is, we rewrite the current dimension i as
2579 * i = stride i' + offset
2581 * and then iterate over individual values of i' instead.
2583 * We then look for a lower bound on i' and a size such that the domain
2584 * is a subset of
2586 * { [j,i'] : l(j) <= i' < l(j) + n }
2588 * and then take slices of the domain at values of i'
2589 * between l(j) and l(j) + n - 1.
2591 * We compute the unshifted simple hull of each slice to ensure that
2592 * we have a single basic set per offset. The slicing constraint
2593 * may get simplified away before the unshifted simple hull is taken
2594 * and may therefore in some rare cases disappear from the result.
2595 * We therefore explicitly add the constraint back after computing
2596 * the unshifted simple hull to ensure that the basic sets
2597 * remain disjoint. The constraints that are dropped by taking the hull
2598 * will be taken into account at the next level, as in the case of the
2599 * atomic option.
2601 * Finally, we map i' back to i and call "fn".
2603 static int foreach_iteration(__isl_take isl_set *domain,
2604 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2605 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2607 int i, n;
2608 int empty;
2609 int depth;
2610 isl_multi_aff *expansion;
2611 isl_basic_map *bmap;
2612 isl_aff *lower = NULL;
2613 isl_ast_build *stride_build;
2615 depth = isl_ast_build_get_depth(build);
2617 domain = isl_ast_build_eliminate_inner(build, domain);
2618 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2619 stride_build = isl_ast_build_copy(build);
2620 stride_build = isl_ast_build_detect_strides(stride_build,
2621 isl_set_copy(domain));
2622 expansion = isl_ast_build_get_stride_expansion(stride_build);
2624 domain = isl_set_preimage_multi_aff(domain,
2625 isl_multi_aff_copy(expansion));
2626 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2627 isl_ast_build_free(stride_build);
2629 bmap = isl_basic_map_from_multi_aff(expansion);
2631 empty = isl_set_is_empty(domain);
2632 if (empty < 0) {
2633 n = -1;
2634 } else if (empty) {
2635 n = 0;
2636 } else {
2637 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2638 if (!lower)
2639 n = -1;
2641 if (n >= 0 && init && init(n, user) < 0)
2642 n = -1;
2643 for (i = 0; i < n; ++i) {
2644 isl_set *set;
2645 isl_basic_set *bset;
2646 isl_constraint *slice;
2648 slice = at_offset(depth, lower, i);
2649 set = isl_set_copy(domain);
2650 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2651 bset = isl_set_unshifted_simple_hull(set);
2652 bset = isl_basic_set_add_constraint(bset, slice);
2653 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2655 if (fn(bset, user) < 0)
2656 break;
2659 isl_aff_free(lower);
2660 isl_set_free(domain);
2661 isl_basic_map_free(bmap);
2663 return n < 0 || i < n ? -1 : 0;
2666 /* Data structure for storing the results and the intermediate objects
2667 * of compute_domains.
2669 * "list" is the main result of the function and contains a list
2670 * of disjoint basic sets for which code should be generated.
2672 * "executed" and "build" are inputs to compute_domains.
2673 * "schedule_domain" is the domain of "executed".
2675 * "option" contains the domains at the current depth that should by
2676 * atomic, separated or unrolled. These domains are as specified by
2677 * the user, except that inner dimensions have been eliminated and
2678 * that they have been made pair-wise disjoint.
2680 * "sep_class" contains the user-specified split into separation classes
2681 * specialized to the current depth.
2682 * "done" contains the union of the separation domains that have already
2683 * been handled.
2685 struct isl_codegen_domains {
2686 isl_basic_set_list *list;
2688 isl_union_map *executed;
2689 isl_ast_build *build;
2690 isl_set *schedule_domain;
2692 isl_set *option[4];
2694 isl_map *sep_class;
2695 isl_set *done;
2698 /* Internal data structure for do_unroll.
2700 * "domains" stores the results of compute_domains.
2701 * "class_domain" is the original class domain passed to do_unroll.
2702 * "unroll_domain" collects the unrolled iterations.
2704 struct isl_ast_unroll_data {
2705 struct isl_codegen_domains *domains;
2706 isl_set *class_domain;
2707 isl_set *unroll_domain;
2710 /* Given an iteration of an unrolled domain represented by "bset",
2711 * add it to data->domains->list.
2712 * Since we may have dropped some constraints, we intersect with
2713 * the class domain again to ensure that each element in the list
2714 * is disjoint from the other class domains.
2716 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2718 struct isl_ast_unroll_data *data = user;
2719 isl_set *set;
2720 isl_basic_set_list *list;
2722 set = isl_set_from_basic_set(bset);
2723 data->unroll_domain = isl_set_union(data->unroll_domain,
2724 isl_set_copy(set));
2725 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2726 set = isl_set_make_disjoint(set);
2727 list = isl_basic_set_list_from_set(set);
2728 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2729 list);
2731 return 0;
2734 /* Extend domains->list with a list of basic sets, one for each value
2735 * of the current dimension in "domain" and remove the corresponding
2736 * sets from the class domain. Return the updated class domain.
2737 * The divs that involve the current dimension have not been projected out
2738 * from this domain.
2740 * We call foreach_iteration to iterate over the individual values and
2741 * in do_unroll_iteration we collect the individual basic sets in
2742 * domains->list and their union in data->unroll_domain, which is then
2743 * used to update the class domain.
2745 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2746 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2748 struct isl_ast_unroll_data data;
2750 if (!domain)
2751 return isl_set_free(class_domain);
2752 if (!class_domain)
2753 return isl_set_free(domain);
2755 data.domains = domains;
2756 data.class_domain = class_domain;
2757 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2759 if (foreach_iteration(domain, domains->build, NULL,
2760 &do_unroll_iteration, &data) < 0)
2761 data.unroll_domain = isl_set_free(data.unroll_domain);
2763 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2765 return class_domain;
2768 /* Add domains to domains->list for each individual value of the current
2769 * dimension, for that part of the schedule domain that lies in the
2770 * intersection of the option domain and the class domain.
2771 * Remove the corresponding sets from the class domain and
2772 * return the updated class domain.
2774 * We first break up the unroll option domain into individual pieces
2775 * and then handle each of them separately. The unroll option domain
2776 * has been made disjoint in compute_domains_init_options,
2778 * Note that we actively want to combine different pieces of the
2779 * schedule domain that have the same value at the current dimension.
2780 * We therefore need to break up the unroll option domain before
2781 * intersecting with class and schedule domain, hoping that the
2782 * unroll option domain specified by the user is relatively simple.
2784 static __isl_give isl_set *compute_unroll_domains(
2785 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2787 isl_set *unroll_domain;
2788 isl_basic_set_list *unroll_list;
2789 int i, n;
2790 isl_bool empty;
2792 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2793 if (empty < 0)
2794 return isl_set_free(class_domain);
2795 if (empty)
2796 return class_domain;
2798 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2799 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2801 n = isl_basic_set_list_n_basic_set(unroll_list);
2802 for (i = 0; i < n; ++i) {
2803 isl_basic_set *bset;
2805 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2806 unroll_domain = isl_set_from_basic_set(bset);
2807 unroll_domain = isl_set_intersect(unroll_domain,
2808 isl_set_copy(class_domain));
2809 unroll_domain = isl_set_intersect(unroll_domain,
2810 isl_set_copy(domains->schedule_domain));
2812 empty = isl_set_is_empty(unroll_domain);
2813 if (empty >= 0 && empty) {
2814 isl_set_free(unroll_domain);
2815 continue;
2818 class_domain = do_unroll(domains, unroll_domain, class_domain);
2821 isl_basic_set_list_free(unroll_list);
2823 return class_domain;
2826 /* Try and construct a single basic set that includes the intersection of
2827 * the schedule domain, the atomic option domain and the class domain.
2828 * Add the resulting basic set(s) to domains->list and remove them
2829 * from class_domain. Return the updated class domain.
2831 * We construct a single domain rather than trying to combine
2832 * the schedule domains of individual domains because we are working
2833 * within a single component so that non-overlapping schedule domains
2834 * should already have been separated.
2835 * We do however need to make sure that this single domains is a subset
2836 * of the class domain so that it would not intersect with any other
2837 * class domains. This means that we may end up splitting up the atomic
2838 * domain in case separation classes are being used.
2840 * "domain" is the intersection of the schedule domain and the class domain,
2841 * with inner dimensions projected out.
2843 static __isl_give isl_set *compute_atomic_domain(
2844 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2846 isl_basic_set *bset;
2847 isl_basic_set_list *list;
2848 isl_set *domain, *atomic_domain;
2849 int empty;
2851 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2852 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2853 domain = isl_set_intersect(domain,
2854 isl_set_copy(domains->schedule_domain));
2855 empty = isl_set_is_empty(domain);
2856 if (empty < 0)
2857 class_domain = isl_set_free(class_domain);
2858 if (empty) {
2859 isl_set_free(domain);
2860 return class_domain;
2863 domain = isl_ast_build_eliminate(domains->build, domain);
2864 domain = isl_set_coalesce_preserve(domain);
2865 bset = isl_set_unshifted_simple_hull(domain);
2866 domain = isl_set_from_basic_set(bset);
2867 atomic_domain = isl_set_copy(domain);
2868 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2869 class_domain = isl_set_subtract(class_domain, atomic_domain);
2870 domain = isl_set_make_disjoint(domain);
2871 list = isl_basic_set_list_from_set(domain);
2872 domains->list = isl_basic_set_list_concat(domains->list, list);
2874 return class_domain;
2877 /* Split up the schedule domain into uniform basic sets,
2878 * in the sense that each element in a basic set is associated to
2879 * elements of the same domains, and add the result to domains->list.
2880 * Do this for that part of the schedule domain that lies in the
2881 * intersection of "class_domain" and the separate option domain.
2883 * "class_domain" may or may not include the constraints
2884 * of the schedule domain, but this does not make a difference
2885 * since we are going to intersect it with the domain of the inverse schedule.
2886 * If it includes schedule domain constraints, then they may involve
2887 * inner dimensions, but we will eliminate them in separation_domain.
2889 static int compute_separate_domain(struct isl_codegen_domains *domains,
2890 __isl_keep isl_set *class_domain)
2892 isl_space *space;
2893 isl_set *domain;
2894 isl_union_map *executed;
2895 isl_basic_set_list *list;
2896 int empty;
2898 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2899 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2900 executed = isl_union_map_copy(domains->executed);
2901 executed = isl_union_map_intersect_domain(executed,
2902 isl_union_set_from_set(domain));
2903 empty = isl_union_map_is_empty(executed);
2904 if (empty < 0 || empty) {
2905 isl_union_map_free(executed);
2906 return empty < 0 ? -1 : 0;
2909 space = isl_set_get_space(class_domain);
2910 domain = separate_schedule_domains(space, executed, domains->build);
2912 list = isl_basic_set_list_from_set(domain);
2913 domains->list = isl_basic_set_list_concat(domains->list, list);
2915 return 0;
2918 /* Split up the domain at the current depth into disjoint
2919 * basic sets for which code should be generated separately
2920 * for the given separation class domain.
2922 * If any separation classes have been defined, then "class_domain"
2923 * is the domain of the current class and does not refer to inner dimensions.
2924 * Otherwise, "class_domain" is the universe domain.
2926 * We first make sure that the class domain is disjoint from
2927 * previously considered class domains.
2929 * The separate domains can be computed directly from the "class_domain".
2931 * The unroll, atomic and remainder domains need the constraints
2932 * from the schedule domain.
2934 * For unrolling, the actual schedule domain is needed (with divs that
2935 * may refer to the current dimension) so that stride detection can be
2936 * performed.
2938 * For atomic and remainder domains, inner dimensions and divs involving
2939 * the current dimensions should be eliminated.
2940 * In case we are working within a separation class, we need to intersect
2941 * the result with the current "class_domain" to ensure that the domains
2942 * are disjoint from those generated from other class domains.
2944 * The domain that has been made atomic may be larger than specified
2945 * by the user since it needs to be representable as a single basic set.
2946 * This possibly larger domain is removed from class_domain by
2947 * compute_atomic_domain. It is computed first so that the extended domain
2948 * would not overlap with any domains computed before.
2949 * Similary, the unrolled domains may have some constraints removed and
2950 * may therefore also be larger than specified by the user.
2952 * If anything is left after handling separate, unroll and atomic,
2953 * we split it up into basic sets and append the basic sets to domains->list.
2955 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2956 __isl_take isl_set *class_domain)
2958 isl_basic_set_list *list;
2959 isl_set *domain;
2961 class_domain = isl_set_subtract(class_domain,
2962 isl_set_copy(domains->done));
2963 domains->done = isl_set_union(domains->done,
2964 isl_set_copy(class_domain));
2966 class_domain = compute_atomic_domain(domains, class_domain);
2967 class_domain = compute_unroll_domains(domains, class_domain);
2969 domain = isl_set_copy(class_domain);
2971 if (compute_separate_domain(domains, domain) < 0)
2972 goto error;
2973 domain = isl_set_subtract(domain,
2974 isl_set_copy(domains->option[isl_ast_loop_separate]));
2976 domain = isl_set_intersect(domain,
2977 isl_set_copy(domains->schedule_domain));
2979 domain = isl_ast_build_eliminate(domains->build, domain);
2980 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2982 domain = isl_set_coalesce_preserve(domain);
2983 domain = isl_set_make_disjoint(domain);
2985 list = isl_basic_set_list_from_set(domain);
2986 domains->list = isl_basic_set_list_concat(domains->list, list);
2988 isl_set_free(class_domain);
2990 return isl_stat_ok;
2991 error:
2992 isl_set_free(domain);
2993 isl_set_free(class_domain);
2994 return isl_stat_error;
2997 /* Split up the domain at the current depth into disjoint
2998 * basic sets for which code should be generated separately
2999 * for the separation class identified by "pnt".
3001 * We extract the corresponding class domain from domains->sep_class,
3002 * eliminate inner dimensions and pass control to compute_partial_domains.
3004 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
3006 struct isl_codegen_domains *domains = user;
3007 isl_set *class_set;
3008 isl_set *domain;
3009 int disjoint;
3011 class_set = isl_set_from_point(pnt);
3012 domain = isl_map_domain(isl_map_intersect_range(
3013 isl_map_copy(domains->sep_class), class_set));
3014 domain = isl_ast_build_compute_gist(domains->build, domain);
3015 domain = isl_ast_build_eliminate(domains->build, domain);
3017 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
3018 if (disjoint < 0)
3019 return isl_stat_error;
3020 if (disjoint) {
3021 isl_set_free(domain);
3022 return isl_stat_ok;
3025 return compute_partial_domains(domains, domain);
3028 /* Extract the domains at the current depth that should be atomic,
3029 * separated or unrolled and store them in option.
3031 * The domains specified by the user might overlap, so we make
3032 * them disjoint by subtracting earlier domains from later domains.
3034 static void compute_domains_init_options(isl_set *option[4],
3035 __isl_keep isl_ast_build *build)
3037 enum isl_ast_loop_type type, type2;
3038 isl_set *unroll;
3040 for (type = isl_ast_loop_atomic;
3041 type <= isl_ast_loop_separate; ++type) {
3042 option[type] = isl_ast_build_get_option_domain(build, type);
3043 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3044 option[type] = isl_set_subtract(option[type],
3045 isl_set_copy(option[type2]));
3048 unroll = option[isl_ast_loop_unroll];
3049 unroll = isl_set_coalesce(unroll);
3050 unroll = isl_set_make_disjoint(unroll);
3051 option[isl_ast_loop_unroll] = unroll;
3054 /* Split up the domain at the current depth into disjoint
3055 * basic sets for which code should be generated separately,
3056 * based on the user-specified options.
3057 * Return the list of disjoint basic sets.
3059 * There are three kinds of domains that we need to keep track of.
3060 * - the "schedule domain" is the domain of "executed"
3061 * - the "class domain" is the domain corresponding to the currrent
3062 * separation class
3063 * - the "option domain" is the domain corresponding to one of the options
3064 * atomic, unroll or separate
3066 * We first consider the individial values of the separation classes
3067 * and split up the domain for each of them separately.
3068 * Finally, we consider the remainder. If no separation classes were
3069 * specified, then we call compute_partial_domains with the universe
3070 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3071 * with inner dimensions removed. We do this because we want to
3072 * avoid computing the complement of the class domains (i.e., the difference
3073 * between the universe and domains->done).
3075 static __isl_give isl_basic_set_list *compute_domains(
3076 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3078 struct isl_codegen_domains domains;
3079 isl_ctx *ctx;
3080 isl_set *domain;
3081 isl_union_set *schedule_domain;
3082 isl_set *classes;
3083 isl_space *space;
3084 int n_param;
3085 enum isl_ast_loop_type type;
3086 isl_bool empty;
3088 if (!executed)
3089 return NULL;
3091 ctx = isl_union_map_get_ctx(executed);
3092 domains.list = isl_basic_set_list_alloc(ctx, 0);
3094 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3095 domain = isl_set_from_union_set(schedule_domain);
3097 compute_domains_init_options(domains.option, build);
3099 domains.sep_class = isl_ast_build_get_separation_class(build);
3100 classes = isl_map_range(isl_map_copy(domains.sep_class));
3101 n_param = isl_set_dim(classes, isl_dim_param);
3102 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3104 space = isl_set_get_space(domain);
3105 domains.build = build;
3106 domains.schedule_domain = isl_set_copy(domain);
3107 domains.executed = executed;
3108 domains.done = isl_set_empty(space);
3110 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3111 domains.list = isl_basic_set_list_free(domains.list);
3112 isl_set_free(classes);
3114 empty = isl_set_is_empty(domains.done);
3115 if (empty < 0) {
3116 domains.list = isl_basic_set_list_free(domains.list);
3117 domain = isl_set_free(domain);
3118 } else if (empty) {
3119 isl_set_free(domain);
3120 domain = isl_set_universe(isl_set_get_space(domains.done));
3121 } else {
3122 domain = isl_ast_build_eliminate(build, domain);
3124 if (compute_partial_domains(&domains, domain) < 0)
3125 domains.list = isl_basic_set_list_free(domains.list);
3127 isl_set_free(domains.schedule_domain);
3128 isl_set_free(domains.done);
3129 isl_map_free(domains.sep_class);
3130 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3131 isl_set_free(domains.option[type]);
3133 return domains.list;
3136 /* Generate code for a single component, after shifting (if any)
3137 * has been applied, in case the schedule was specified as a union map.
3139 * We first split up the domain at the current depth into disjoint
3140 * basic sets based on the user-specified options.
3141 * Then we generated code for each of them and concatenate the results.
3143 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3144 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3146 isl_basic_set_list *domain_list;
3147 isl_ast_graft_list *list = NULL;
3149 domain_list = compute_domains(executed, build);
3150 list = generate_parallel_domains(domain_list, executed, build);
3152 isl_basic_set_list_free(domain_list);
3153 isl_union_map_free(executed);
3154 isl_ast_build_free(build);
3156 return list;
3159 /* Generate code for a single component, after shifting (if any)
3160 * has been applied, in case the schedule was specified as a schedule tree
3161 * and the separate option was specified.
3163 * We perform separation on the domain of "executed" and then generate
3164 * an AST for each of the resulting disjoint basic sets.
3166 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3167 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3169 isl_space *space;
3170 isl_set *domain;
3171 isl_basic_set_list *domain_list;
3172 isl_ast_graft_list *list;
3174 space = isl_ast_build_get_space(build, 1);
3175 domain = separate_schedule_domains(space,
3176 isl_union_map_copy(executed), build);
3177 domain_list = isl_basic_set_list_from_set(domain);
3179 list = generate_parallel_domains(domain_list, executed, build);
3181 isl_basic_set_list_free(domain_list);
3182 isl_union_map_free(executed);
3183 isl_ast_build_free(build);
3185 return list;
3188 /* Internal data structure for generate_shifted_component_tree_unroll.
3190 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3191 * "list" collects the constructs grafts.
3193 struct isl_ast_unroll_tree_data {
3194 isl_union_map *executed;
3195 isl_ast_build *build;
3196 isl_ast_graft_list *list;
3199 /* Initialize data->list to a list of "n" elements.
3201 static int init_unroll_tree(int n, void *user)
3203 struct isl_ast_unroll_tree_data *data = user;
3204 isl_ctx *ctx;
3206 ctx = isl_ast_build_get_ctx(data->build);
3207 data->list = isl_ast_graft_list_alloc(ctx, n);
3209 return 0;
3212 /* Given an iteration of an unrolled domain represented by "bset",
3213 * generate the corresponding AST and add the result to data->list.
3215 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3217 struct isl_ast_unroll_tree_data *data = user;
3219 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3220 bset, isl_ast_build_copy(data->build));
3222 return 0;
3225 /* Generate code for a single component, after shifting (if any)
3226 * has been applied, in case the schedule was specified as a schedule tree
3227 * and the unroll option was specified.
3229 * We call foreach_iteration to iterate over the individual values and
3230 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3232 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3233 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3234 __isl_take isl_ast_build *build)
3236 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3238 if (foreach_iteration(domain, build, &init_unroll_tree,
3239 &do_unroll_tree_iteration, &data) < 0)
3240 data.list = isl_ast_graft_list_free(data.list);
3242 isl_union_map_free(executed);
3243 isl_ast_build_free(build);
3245 return data.list;
3248 /* Does "domain" involve a disjunction that is purely based on
3249 * constraints involving only outer dimension?
3251 * In particular, is there a disjunction such that the constraints
3252 * involving the current and later dimensions are the same over
3253 * all the disjuncts?
3255 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3256 __isl_keep isl_ast_build *build)
3258 isl_basic_set *hull;
3259 isl_set *shared, *inner;
3260 isl_bool equal;
3261 int depth, dim;
3263 if (isl_set_n_basic_set(domain) <= 1)
3264 return isl_bool_false;
3266 inner = isl_set_copy(domain);
3267 depth = isl_ast_build_get_depth(build);
3268 dim = isl_set_dim(inner, isl_dim_set);
3269 inner = isl_set_drop_constraints_not_involving_dims(inner,
3270 isl_dim_set, depth, dim - depth);
3271 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3272 shared = isl_set_from_basic_set(hull);
3273 equal = isl_set_plain_is_equal(inner, shared);
3274 isl_set_free(inner);
3275 isl_set_free(shared);
3277 return equal;
3280 /* Generate code for a single component, after shifting (if any)
3281 * has been applied, in case the schedule was specified as a schedule tree.
3282 * In particular, handle the base case where there is either no isolated
3283 * set or we are within the isolated set (in which case "isolated" is set)
3284 * or the iterations that precede or follow the isolated set.
3286 * The schedule domain is broken up or combined into basic sets
3287 * according to the AST generation option specified in the current
3288 * schedule node, which may be either atomic, separate, unroll or
3289 * unspecified. If the option is unspecified, then we currently simply
3290 * split the schedule domain into disjoint basic sets.
3292 * In case the separate option is specified, the AST generation is
3293 * handled by generate_shifted_component_tree_separate.
3294 * In the other cases, we need the global schedule domain.
3295 * In the unroll case, the AST generation is then handled by
3296 * generate_shifted_component_tree_unroll which needs the actual
3297 * schedule domain (with divs that may refer to the current dimension)
3298 * so that stride detection can be performed.
3299 * In the atomic or unspecified case, inner dimensions and divs involving
3300 * the current dimensions should be eliminated.
3301 * The result is then either combined into a single basic set or
3302 * split up into disjoint basic sets.
3303 * Finally an AST is generated for each basic set and the results are
3304 * concatenated.
3306 * If the schedule domain involves a disjunction that is purely based on
3307 * constraints involving only outer dimension, then it is treated as
3308 * if atomic was specified. This ensures that only a single loop
3309 * is generated instead of a sequence of identical loops with
3310 * different guards.
3312 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3313 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3314 int isolated)
3316 isl_bool outer_disjunction;
3317 isl_union_set *schedule_domain;
3318 isl_set *domain;
3319 isl_basic_set_list *domain_list;
3320 isl_ast_graft_list *list;
3321 enum isl_ast_loop_type type;
3323 type = isl_ast_build_get_loop_type(build, isolated);
3324 if (type < 0)
3325 goto error;
3327 if (type == isl_ast_loop_separate)
3328 return generate_shifted_component_tree_separate(executed,
3329 build);
3331 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3332 domain = isl_set_from_union_set(schedule_domain);
3334 if (type == isl_ast_loop_unroll)
3335 return generate_shifted_component_tree_unroll(executed, domain,
3336 build);
3338 domain = isl_ast_build_eliminate(build, domain);
3339 domain = isl_set_coalesce_preserve(domain);
3341 outer_disjunction = has_pure_outer_disjunction(domain, build);
3342 if (outer_disjunction < 0)
3343 domain = isl_set_free(domain);
3345 if (outer_disjunction || type == isl_ast_loop_atomic) {
3346 isl_basic_set *hull;
3347 hull = isl_set_unshifted_simple_hull(domain);
3348 domain_list = isl_basic_set_list_from_basic_set(hull);
3349 } else {
3350 domain = isl_set_make_disjoint(domain);
3351 domain_list = isl_basic_set_list_from_set(domain);
3354 list = generate_parallel_domains(domain_list, executed, build);
3356 isl_basic_set_list_free(domain_list);
3357 isl_union_map_free(executed);
3358 isl_ast_build_free(build);
3360 return list;
3361 error:
3362 isl_union_map_free(executed);
3363 isl_ast_build_free(build);
3364 return NULL;
3367 /* Extract out the disjunction imposed by "domain" on the outer
3368 * schedule dimensions.
3370 * In particular, remove all inner dimensions from "domain" (including
3371 * the current dimension) and then remove the constraints that are shared
3372 * by all disjuncts in the result.
3374 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3375 __isl_keep isl_ast_build *build)
3377 isl_set *hull;
3378 int depth, dim;
3380 domain = isl_ast_build_specialize(build, domain);
3381 depth = isl_ast_build_get_depth(build);
3382 dim = isl_set_dim(domain, isl_dim_set);
3383 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3384 domain = isl_set_remove_unknown_divs(domain);
3385 hull = isl_set_copy(domain);
3386 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3387 domain = isl_set_gist(domain, hull);
3389 return domain;
3392 /* Add "guard" to the grafts in "list".
3393 * "build" is the outer AST build, while "sub_build" includes "guard"
3394 * in its generated domain.
3396 * First combine the grafts into a single graft and then add the guard.
3397 * If the list is empty, or if some error occurred, then simply return
3398 * the list.
3400 static __isl_give isl_ast_graft_list *list_add_guard(
3401 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3402 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3404 isl_ast_graft *graft;
3406 list = isl_ast_graft_list_fuse(list, sub_build);
3408 if (isl_ast_graft_list_n_ast_graft(list) != 1)
3409 return list;
3411 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3412 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3413 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3415 return list;
3418 /* Generate code for a single component, after shifting (if any)
3419 * has been applied, in case the schedule was specified as a schedule tree.
3420 * In particular, do so for the specified subset of the schedule domain.
3422 * If we are outside of the isolated part, then "domain" may include
3423 * a disjunction. Explicitly generate this disjunction at this point
3424 * instead of relying on the disjunction getting hoisted back up
3425 * to this level.
3427 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3428 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3429 __isl_keep isl_ast_build *build, int isolated)
3431 isl_union_set *uset;
3432 isl_ast_graft_list *list;
3433 isl_ast_build *sub_build;
3434 int empty;
3436 uset = isl_union_set_from_set(isl_set_copy(domain));
3437 executed = isl_union_map_copy(executed);
3438 executed = isl_union_map_intersect_domain(executed, uset);
3439 empty = isl_union_map_is_empty(executed);
3440 if (empty < 0)
3441 goto error;
3442 if (empty) {
3443 isl_ctx *ctx;
3444 isl_union_map_free(executed);
3445 isl_set_free(domain);
3446 ctx = isl_ast_build_get_ctx(build);
3447 return isl_ast_graft_list_alloc(ctx, 0);
3450 sub_build = isl_ast_build_copy(build);
3451 if (!isolated) {
3452 domain = extract_disjunction(domain, build);
3453 sub_build = isl_ast_build_restrict_generated(sub_build,
3454 isl_set_copy(domain));
3456 list = generate_shifted_component_tree_base(executed,
3457 isl_ast_build_copy(sub_build), isolated);
3458 if (!isolated)
3459 list = list_add_guard(list, domain, build, sub_build);
3460 isl_ast_build_free(sub_build);
3461 isl_set_free(domain);
3462 return list;
3463 error:
3464 isl_union_map_free(executed);
3465 isl_set_free(domain);
3466 return NULL;
3469 /* Generate code for a single component, after shifting (if any)
3470 * has been applied, in case the schedule was specified as a schedule tree.
3471 * In particular, do so for the specified sequence of subsets
3472 * of the schedule domain, "before", "isolated", "after" and "other",
3473 * where only the "isolated" part is considered to be isolated.
3475 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3476 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3477 __isl_take isl_set *isolated, __isl_take isl_set *after,
3478 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3480 isl_ast_graft_list *list, *res;
3482 res = generate_shifted_component_tree_part(executed, before, build, 0);
3483 list = generate_shifted_component_tree_part(executed, isolated,
3484 build, 1);
3485 res = isl_ast_graft_list_concat(res, list);
3486 list = generate_shifted_component_tree_part(executed, after, build, 0);
3487 res = isl_ast_graft_list_concat(res, list);
3488 list = generate_shifted_component_tree_part(executed, other, build, 0);
3489 res = isl_ast_graft_list_concat(res, list);
3491 isl_union_map_free(executed);
3492 isl_ast_build_free(build);
3494 return res;
3497 /* Does "set" intersect "first", but not "second"?
3499 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3500 __isl_keep isl_set *first, __isl_keep isl_set *second)
3502 isl_bool disjoint;
3504 disjoint = isl_set_is_disjoint(set, first);
3505 if (disjoint < 0)
3506 return isl_bool_error;
3507 if (disjoint)
3508 return isl_bool_false;
3510 return isl_set_is_disjoint(set, second);
3513 /* Generate code for a single component, after shifting (if any)
3514 * has been applied, in case the schedule was specified as a schedule tree.
3515 * In particular, do so in case of isolation where there is
3516 * only an "isolated" part and an "after" part.
3517 * "dead1" and "dead2" are freed by this function in order to simplify
3518 * the caller.
3520 * The "before" and "other" parts are set to empty sets.
3522 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3523 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3524 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3525 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3527 isl_set *empty;
3529 empty = isl_set_empty(isl_set_get_space(after));
3530 isl_set_free(dead1);
3531 isl_set_free(dead2);
3532 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3533 isolated, after, empty, build);
3536 /* Generate code for a single component, after shifting (if any)
3537 * has been applied, in case the schedule was specified as a schedule tree.
3539 * We first check if the user has specified an isolated schedule domain
3540 * and that we are not already outside of this isolated schedule domain.
3541 * If so, we break up the schedule domain into iterations that
3542 * precede the isolated domain, the isolated domain itself,
3543 * the iterations that follow the isolated domain and
3544 * the remaining iterations (those that are incomparable
3545 * to the isolated domain).
3546 * We generate an AST for each piece and concatenate the results.
3548 * If the isolated domain is not convex, then it is replaced
3549 * by a convex superset to ensure that the sets of preceding and
3550 * following iterations are properly defined and, in particular,
3551 * that there are no intermediate iterations that do not belong
3552 * to the isolated domain.
3554 * In the special case where at least one element of the schedule
3555 * domain that does not belong to the isolated domain needs
3556 * to be scheduled after this isolated domain, but none of those
3557 * elements need to be scheduled before, break up the schedule domain
3558 * in only two parts, the isolated domain, and a part that will be
3559 * scheduled after the isolated domain.
3561 * If no isolated set has been specified, then we generate an
3562 * AST for the entire inverse schedule.
3564 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3565 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3567 int i, depth;
3568 int empty, has_isolate;
3569 isl_space *space;
3570 isl_union_set *schedule_domain;
3571 isl_set *domain;
3572 isl_basic_set *hull;
3573 isl_set *isolated, *before, *after, *test;
3574 isl_map *gt, *lt;
3575 isl_bool pure;
3577 build = isl_ast_build_extract_isolated(build);
3578 has_isolate = isl_ast_build_has_isolated(build);
3579 if (has_isolate < 0)
3580 executed = isl_union_map_free(executed);
3581 else if (!has_isolate)
3582 return generate_shifted_component_tree_base(executed, build, 0);
3584 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3585 domain = isl_set_from_union_set(schedule_domain);
3587 isolated = isl_ast_build_get_isolated(build);
3588 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3589 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3590 empty = isl_set_is_empty(test);
3591 isl_set_free(test);
3592 if (empty < 0)
3593 goto error;
3594 if (empty) {
3595 isl_set_free(isolated);
3596 isl_set_free(domain);
3597 return generate_shifted_component_tree_base(executed, build, 0);
3599 isolated = isl_ast_build_eliminate(build, isolated);
3600 hull = isl_set_unshifted_simple_hull(isolated);
3601 isolated = isl_set_from_basic_set(hull);
3603 depth = isl_ast_build_get_depth(build);
3604 space = isl_space_map_from_set(isl_set_get_space(isolated));
3605 gt = isl_map_universe(space);
3606 for (i = 0; i < depth; ++i)
3607 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3608 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3609 lt = isl_map_reverse(isl_map_copy(gt));
3610 before = isl_set_apply(isl_set_copy(isolated), gt);
3611 after = isl_set_apply(isl_set_copy(isolated), lt);
3613 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3614 pure = only_intersects_first(domain, after, before);
3615 if (pure < 0)
3616 executed = isl_union_map_free(executed);
3617 else if (pure)
3618 return generate_shifted_component_only_after(executed, isolated,
3619 domain, build, before, after);
3620 domain = isl_set_subtract(domain, isl_set_copy(before));
3621 domain = isl_set_subtract(domain, isl_set_copy(after));
3622 after = isl_set_subtract(after, isl_set_copy(isolated));
3623 after = isl_set_subtract(after, isl_set_copy(before));
3624 before = isl_set_subtract(before, isl_set_copy(isolated));
3626 return generate_shifted_component_parts(executed, before, isolated,
3627 after, domain, build);
3628 error:
3629 isl_set_free(domain);
3630 isl_set_free(isolated);
3631 isl_union_map_free(executed);
3632 isl_ast_build_free(build);
3633 return NULL;
3636 /* Generate code for a single component, after shifting (if any)
3637 * has been applied.
3639 * Call generate_shifted_component_tree or generate_shifted_component_flat
3640 * depending on whether the schedule was specified as a schedule tree.
3642 static __isl_give isl_ast_graft_list *generate_shifted_component(
3643 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3645 if (isl_ast_build_has_schedule_node(build))
3646 return generate_shifted_component_tree(executed, build);
3647 else
3648 return generate_shifted_component_flat(executed, build);
3651 struct isl_set_map_pair {
3652 isl_set *set;
3653 isl_map *map;
3656 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3657 * of indices into the "domain" array,
3658 * return the union of the "map" fields of the elements
3659 * indexed by the first "n" elements of "order".
3661 static __isl_give isl_union_map *construct_component_executed(
3662 struct isl_set_map_pair *domain, int *order, int n)
3664 int i;
3665 isl_map *map;
3666 isl_union_map *executed;
3668 map = isl_map_copy(domain[order[0]].map);
3669 executed = isl_union_map_from_map(map);
3670 for (i = 1; i < n; ++i) {
3671 map = isl_map_copy(domain[order[i]].map);
3672 executed = isl_union_map_add_map(executed, map);
3675 return executed;
3678 /* Generate code for a single component, after shifting (if any)
3679 * has been applied.
3681 * The component inverse schedule is specified as the "map" fields
3682 * of the elements of "domain" indexed by the first "n" elements of "order".
3684 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3685 struct isl_set_map_pair *domain, int *order, int n,
3686 __isl_take isl_ast_build *build)
3688 isl_union_map *executed;
3690 executed = construct_component_executed(domain, order, n);
3691 return generate_shifted_component(executed, build);
3694 /* Does set dimension "pos" of "set" have an obviously fixed value?
3696 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3698 int fixed;
3699 isl_val *v;
3701 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3702 if (!v)
3703 return -1;
3704 fixed = !isl_val_is_nan(v);
3705 isl_val_free(v);
3707 return fixed;
3710 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3711 * of indices into the "domain" array,
3712 * do all (except for at most one) of the "set" field of the elements
3713 * indexed by the first "n" elements of "order" have a fixed value
3714 * at position "depth"?
3716 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3717 int *order, int n, int depth)
3719 int i;
3720 int non_fixed = -1;
3722 for (i = 0; i < n; ++i) {
3723 int f;
3725 f = dim_is_fixed(domain[order[i]].set, depth);
3726 if (f < 0)
3727 return -1;
3728 if (f)
3729 continue;
3730 if (non_fixed >= 0)
3731 return 0;
3732 non_fixed = i;
3735 return 1;
3738 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3739 * of indices into the "domain" array,
3740 * eliminate the inner dimensions from the "set" field of the elements
3741 * indexed by the first "n" elements of "order", provided the current
3742 * dimension does not have a fixed value.
3744 * Return the index of the first element in "order" with a corresponding
3745 * "set" field that does not have an (obviously) fixed value.
3747 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3748 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3750 int i;
3751 int base = -1;
3753 for (i = n - 1; i >= 0; --i) {
3754 int f;
3755 f = dim_is_fixed(domain[order[i]].set, depth);
3756 if (f < 0)
3757 return -1;
3758 if (f)
3759 continue;
3760 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3761 domain[order[i]].set);
3762 base = i;
3765 return base;
3768 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3769 * of indices into the "domain" array,
3770 * find the element of "domain" (amongst those indexed by the first "n"
3771 * elements of "order") with the "set" field that has the smallest
3772 * value for the current iterator.
3774 * Note that the domain with the smallest value may depend on the parameters
3775 * and/or outer loop dimension. Since the result of this function is only
3776 * used as heuristic, we only make a reasonable attempt at finding the best
3777 * domain, one that should work in case a single domain provides the smallest
3778 * value for the current dimension over all values of the parameters
3779 * and outer dimensions.
3781 * In particular, we compute the smallest value of the first domain
3782 * and replace it by that of any later domain if that later domain
3783 * has a smallest value that is smaller for at least some value
3784 * of the parameters and outer dimensions.
3786 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3787 __isl_keep isl_ast_build *build)
3789 int i;
3790 isl_map *min_first;
3791 int first = 0;
3793 min_first = isl_ast_build_map_to_iterator(build,
3794 isl_set_copy(domain[order[0]].set));
3795 min_first = isl_map_lexmin(min_first);
3797 for (i = 1; i < n; ++i) {
3798 isl_map *min, *test;
3799 int empty;
3801 min = isl_ast_build_map_to_iterator(build,
3802 isl_set_copy(domain[order[i]].set));
3803 min = isl_map_lexmin(min);
3804 test = isl_map_copy(min);
3805 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3806 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3807 empty = isl_map_is_empty(test);
3808 isl_map_free(test);
3809 if (empty >= 0 && !empty) {
3810 isl_map_free(min_first);
3811 first = i;
3812 min_first = min;
3813 } else
3814 isl_map_free(min);
3816 if (empty < 0)
3817 break;
3820 isl_map_free(min_first);
3822 return i < n ? -1 : first;
3825 /* Construct a shifted inverse schedule based on the original inverse schedule,
3826 * the stride and the offset.
3828 * The original inverse schedule is specified as the "map" fields
3829 * of the elements of "domain" indexed by the first "n" elements of "order".
3831 * "stride" and "offset" are such that the difference
3832 * between the values of the current dimension of domain "i"
3833 * and the values of the current dimension for some reference domain are
3834 * equal to
3836 * stride * integer + offset[i]
3838 * Moreover, 0 <= offset[i] < stride.
3840 * For each domain, we create a map
3842 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3844 * where j refers to the current dimension and the other dimensions are
3845 * unchanged, and apply this map to the original schedule domain.
3847 * For example, for the original schedule
3849 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3851 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3852 * we apply the mapping
3854 * { [j] -> [j, 0] }
3856 * to the schedule of the "A" domain and the mapping
3858 * { [j - 1] -> [j, 1] }
3860 * to the schedule of the "B" domain.
3863 * Note that after the transformation, the differences between pairs
3864 * of values of the current dimension over all domains are multiples
3865 * of stride and that we have therefore exposed the stride.
3868 * To see that the mapping preserves the lexicographic order,
3869 * first note that each of the individual maps above preserves the order.
3870 * If the value of the current iterator is j1 in one domain and j2 in another,
3871 * then if j1 = j2, we know that the same map is applied to both domains
3872 * and the order is preserved.
3873 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3874 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3876 * j1 - c1 < j2 - c2
3878 * and the order is preserved.
3879 * If c1 < c2, then we know
3881 * 0 <= c2 - c1 < s
3883 * We also have
3885 * j2 - j1 = n * s + r
3887 * with n >= 0 and 0 <= r < s.
3888 * In other words, r = c2 - c1.
3889 * If n > 0, then
3891 * j1 - c1 < j2 - c2
3893 * If n = 0, then
3895 * j1 - c1 = j2 - c2
3897 * and so
3899 * (j1 - c1, c1) << (j2 - c2, c2)
3901 * with "<<" the lexicographic order, proving that the order is preserved
3902 * in all cases.
3904 static __isl_give isl_union_map *construct_shifted_executed(
3905 struct isl_set_map_pair *domain, int *order, int n,
3906 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3907 __isl_take isl_ast_build *build)
3909 int i;
3910 isl_union_map *executed;
3911 isl_space *space;
3912 isl_map *map;
3913 int depth;
3914 isl_constraint *c;
3916 depth = isl_ast_build_get_depth(build);
3917 space = isl_ast_build_get_space(build, 1);
3918 executed = isl_union_map_empty(isl_space_copy(space));
3919 space = isl_space_map_from_set(space);
3920 map = isl_map_identity(isl_space_copy(space));
3921 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3922 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3923 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3925 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3926 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3927 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3929 for (i = 0; i < n; ++i) {
3930 isl_map *map_i;
3931 isl_val *v;
3933 v = isl_multi_val_get_val(offset, i);
3934 if (!v)
3935 break;
3936 map_i = isl_map_copy(map);
3937 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3938 isl_val_copy(v));
3939 v = isl_val_neg(v);
3940 c = isl_constraint_set_constant_val(c, v);
3941 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3943 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3944 map_i);
3945 executed = isl_union_map_add_map(executed, map_i);
3948 isl_constraint_free(c);
3949 isl_map_free(map);
3951 if (i < n)
3952 executed = isl_union_map_free(executed);
3954 return executed;
3957 /* Generate code for a single component, after exposing the stride,
3958 * given that the schedule domain is "shifted strided".
3960 * The component inverse schedule is specified as the "map" fields
3961 * of the elements of "domain" indexed by the first "n" elements of "order".
3963 * The schedule domain being "shifted strided" means that the differences
3964 * between the values of the current dimension of domain "i"
3965 * and the values of the current dimension for some reference domain are
3966 * equal to
3968 * stride * integer + offset[i]
3970 * We first look for the domain with the "smallest" value for the current
3971 * dimension and adjust the offsets such that the offset of the "smallest"
3972 * domain is equal to zero. The other offsets are reduced modulo stride.
3974 * Based on this information, we construct a new inverse schedule in
3975 * construct_shifted_executed that exposes the stride.
3976 * Since this involves the introduction of a new schedule dimension,
3977 * the build needs to be changed accordingly.
3978 * After computing the AST, the newly introduced dimension needs
3979 * to be removed again from the list of grafts. We do this by plugging
3980 * in a mapping that represents the new schedule domain in terms of the
3981 * old schedule domain.
3983 static __isl_give isl_ast_graft_list *generate_shift_component(
3984 struct isl_set_map_pair *domain, int *order, int n,
3985 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3986 __isl_take isl_ast_build *build)
3988 isl_ast_graft_list *list;
3989 int first;
3990 int depth;
3991 isl_val *val;
3992 isl_multi_val *mv;
3993 isl_space *space;
3994 isl_multi_aff *ma, *zero;
3995 isl_union_map *executed;
3997 depth = isl_ast_build_get_depth(build);
3999 first = first_offset(domain, order, n, build);
4000 if (first < 0)
4001 goto error;
4003 mv = isl_multi_val_copy(offset);
4004 val = isl_multi_val_get_val(offset, first);
4005 val = isl_val_neg(val);
4006 mv = isl_multi_val_add_val(mv, val);
4007 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
4009 executed = construct_shifted_executed(domain, order, n, stride, mv,
4010 build);
4011 space = isl_ast_build_get_space(build, 1);
4012 space = isl_space_map_from_set(space);
4013 ma = isl_multi_aff_identity(isl_space_copy(space));
4014 space = isl_space_from_domain(isl_space_domain(space));
4015 space = isl_space_add_dims(space, isl_dim_out, 1);
4016 zero = isl_multi_aff_zero(space);
4017 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
4018 build = isl_ast_build_insert_dim(build, depth + 1);
4019 list = generate_shifted_component(executed, build);
4021 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4023 isl_multi_val_free(mv);
4025 return list;
4026 error:
4027 isl_ast_build_free(build);
4028 return NULL;
4031 /* Does any node in the schedule tree rooted at the current schedule node
4032 * of "build" depend on outer schedule nodes?
4034 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4036 isl_schedule_node *node;
4037 int dependent = 0;
4039 node = isl_ast_build_get_schedule_node(build);
4040 dependent = isl_schedule_node_is_subtree_anchored(node);
4041 isl_schedule_node_free(node);
4043 return dependent;
4046 /* Generate code for a single component.
4048 * The component inverse schedule is specified as the "map" fields
4049 * of the elements of "domain" indexed by the first "n" elements of "order".
4051 * This function may modify the "set" fields of "domain".
4053 * Before proceeding with the actual code generation for the component,
4054 * we first check if there are any "shifted" strides, meaning that
4055 * the schedule domains of the individual domains are all strided,
4056 * but that they have different offsets, resulting in the union
4057 * of schedule domains not being strided anymore.
4059 * The simplest example is the schedule
4061 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4063 * Both schedule domains are strided, but their union is not.
4064 * This function detects such cases and then rewrites the schedule to
4066 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4068 * In the new schedule, the schedule domains have the same offset (modulo
4069 * the stride), ensuring that the union of schedule domains is also strided.
4072 * If there is only a single domain in the component, then there is
4073 * nothing to do. Similarly, if the current schedule dimension has
4074 * a fixed value for almost all domains then there is nothing to be done.
4075 * In particular, we need at least two domains where the current schedule
4076 * dimension does not have a fixed value.
4077 * Finally, in case of a schedule map input,
4078 * if any of the options refer to the current schedule dimension,
4079 * then we bail out as well. It would be possible to reformulate the options
4080 * in terms of the new schedule domain, but that would introduce constraints
4081 * that separate the domains in the options and that is something we would
4082 * like to avoid.
4083 * In the case of a schedule tree input, we bail out if any of
4084 * the descendants of the current schedule node refer to outer
4085 * schedule nodes in any way.
4088 * To see if there is any shifted stride, we look at the differences
4089 * between the values of the current dimension in pairs of domains
4090 * for equal values of outer dimensions. These differences should be
4091 * of the form
4093 * m x + r
4095 * with "m" the stride and "r" a constant. Note that we cannot perform
4096 * this analysis on individual domains as the lower bound in each domain
4097 * may depend on parameters or outer dimensions and so the current dimension
4098 * itself may not have a fixed remainder on division by the stride.
4100 * In particular, we compare the first domain that does not have an
4101 * obviously fixed value for the current dimension to itself and all
4102 * other domains and collect the offsets and the gcd of the strides.
4103 * If the gcd becomes one, then we failed to find shifted strides.
4104 * If the gcd is zero, then the differences were all fixed, meaning
4105 * that some domains had non-obviously fixed values for the current dimension.
4106 * If all the offsets are the same (for those domains that do not have
4107 * an obviously fixed value for the current dimension), then we do not
4108 * apply the transformation.
4109 * If none of the domains were skipped, then there is nothing to do.
4110 * If some of them were skipped, then if we apply separation, the schedule
4111 * domain should get split in pieces with a (non-shifted) stride.
4113 * Otherwise, we apply a shift to expose the stride in
4114 * generate_shift_component.
4116 static __isl_give isl_ast_graft_list *generate_component(
4117 struct isl_set_map_pair *domain, int *order, int n,
4118 __isl_take isl_ast_build *build)
4120 int i, d;
4121 int depth;
4122 isl_ctx *ctx;
4123 isl_map *map;
4124 isl_set *deltas;
4125 isl_val *gcd = NULL;
4126 isl_multi_val *mv;
4127 int fixed, skip;
4128 int base;
4129 isl_ast_graft_list *list;
4130 int res = 0;
4132 depth = isl_ast_build_get_depth(build);
4134 skip = n == 1;
4135 if (skip >= 0 && !skip)
4136 skip = at_most_one_non_fixed(domain, order, n, depth);
4137 if (skip >= 0 && !skip) {
4138 if (isl_ast_build_has_schedule_node(build))
4139 skip = has_anchored_subtree(build);
4140 else
4141 skip = isl_ast_build_options_involve_depth(build);
4143 if (skip < 0)
4144 goto error;
4145 if (skip)
4146 return generate_shifted_component_from_list(domain,
4147 order, n, build);
4149 base = eliminate_non_fixed(domain, order, n, depth, build);
4150 if (base < 0)
4151 goto error;
4153 ctx = isl_ast_build_get_ctx(build);
4155 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4157 fixed = 1;
4158 for (i = 0; i < n; ++i) {
4159 isl_val *r, *m;
4161 map = isl_map_from_domain_and_range(
4162 isl_set_copy(domain[order[base]].set),
4163 isl_set_copy(domain[order[i]].set));
4164 for (d = 0; d < depth; ++d)
4165 map = isl_map_equate(map, isl_dim_in, d,
4166 isl_dim_out, d);
4167 deltas = isl_map_deltas(map);
4168 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4169 isl_set_free(deltas);
4170 if (res < 0)
4171 break;
4173 if (i == 0)
4174 gcd = m;
4175 else
4176 gcd = isl_val_gcd(gcd, m);
4177 if (isl_val_is_one(gcd)) {
4178 isl_val_free(r);
4179 break;
4181 mv = isl_multi_val_set_val(mv, i, r);
4183 res = dim_is_fixed(domain[order[i]].set, depth);
4184 if (res < 0)
4185 break;
4186 if (res)
4187 continue;
4189 if (fixed && i > base) {
4190 isl_val *a, *b;
4191 a = isl_multi_val_get_val(mv, i);
4192 b = isl_multi_val_get_val(mv, base);
4193 if (isl_val_ne(a, b))
4194 fixed = 0;
4195 isl_val_free(a);
4196 isl_val_free(b);
4200 if (res < 0 || !gcd) {
4201 isl_ast_build_free(build);
4202 list = NULL;
4203 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4204 list = generate_shifted_component_from_list(domain,
4205 order, n, build);
4206 } else {
4207 list = generate_shift_component(domain, order, n, gcd, mv,
4208 build);
4211 isl_val_free(gcd);
4212 isl_multi_val_free(mv);
4214 return list;
4215 error:
4216 isl_ast_build_free(build);
4217 return NULL;
4220 /* Store both "map" itself and its domain in the
4221 * structure pointed to by *next and advance to the next array element.
4223 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4225 struct isl_set_map_pair **next = user;
4227 (*next)->map = isl_map_copy(map);
4228 (*next)->set = isl_map_domain(map);
4229 (*next)++;
4231 return isl_stat_ok;
4234 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4235 __isl_keep isl_schedule_node *node);
4237 /* Is any domain element of "umap" scheduled after any of
4238 * the corresponding image elements by the tree rooted at
4239 * the child of "node"?
4241 static isl_bool after_in_child(__isl_keep isl_union_map *umap,
4242 __isl_keep isl_schedule_node *node)
4244 isl_schedule_node *child;
4245 isl_bool after;
4247 child = isl_schedule_node_get_child(node, 0);
4248 after = after_in_tree(umap, child);
4249 isl_schedule_node_free(child);
4251 return after;
4254 /* Is any domain element of "umap" scheduled after any of
4255 * the corresponding image elements by the tree rooted at
4256 * the band node "node"?
4258 * We first check if any domain element is scheduled after any
4259 * of the corresponding image elements by the band node itself.
4260 * If not, we restrict "map" to those pairs of element that
4261 * are scheduled together by the band node and continue with
4262 * the child of the band node.
4263 * If there are no such pairs then the map passed to after_in_child
4264 * will be empty causing it to return 0.
4266 static isl_bool after_in_band(__isl_keep isl_union_map *umap,
4267 __isl_keep isl_schedule_node *node)
4269 isl_multi_union_pw_aff *mupa;
4270 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4271 isl_union_set *domain, *range;
4272 isl_space *space;
4273 isl_bool empty;
4274 isl_bool after;
4276 if (isl_schedule_node_band_n_member(node) == 0)
4277 return after_in_child(umap, node);
4279 mupa = isl_schedule_node_band_get_partial_schedule(node);
4280 space = isl_multi_union_pw_aff_get_space(mupa);
4281 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4282 test = isl_union_map_copy(umap);
4283 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4284 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4285 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4286 test = isl_union_map_intersect(test, gt);
4287 empty = isl_union_map_is_empty(test);
4288 isl_union_map_free(test);
4290 if (empty < 0 || !empty) {
4291 isl_union_map_free(partial);
4292 return isl_bool_not(empty);
4295 universe = isl_union_map_universe(isl_union_map_copy(umap));
4296 domain = isl_union_map_domain(isl_union_map_copy(universe));
4297 range = isl_union_map_range(universe);
4298 umap1 = isl_union_map_copy(partial);
4299 umap1 = isl_union_map_intersect_domain(umap1, domain);
4300 umap2 = isl_union_map_intersect_domain(partial, range);
4301 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4302 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4303 after = after_in_child(test, node);
4304 isl_union_map_free(test);
4305 return after;
4308 /* Is any domain element of "umap" scheduled after any of
4309 * the corresponding image elements by the tree rooted at
4310 * the context node "node"?
4312 * The context constraints apply to the schedule domain,
4313 * so we cannot apply them directly to "umap", which contains
4314 * pairs of statement instances. Instead, we add them
4315 * to the range of the prefix schedule for both domain and
4316 * range of "umap".
4318 static isl_bool after_in_context(__isl_keep isl_union_map *umap,
4319 __isl_keep isl_schedule_node *node)
4321 isl_union_map *prefix, *universe, *umap1, *umap2;
4322 isl_union_set *domain, *range;
4323 isl_set *context;
4324 isl_bool after;
4326 umap = isl_union_map_copy(umap);
4327 context = isl_schedule_node_context_get_context(node);
4328 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4329 universe = isl_union_map_universe(isl_union_map_copy(umap));
4330 domain = isl_union_map_domain(isl_union_map_copy(universe));
4331 range = isl_union_map_range(universe);
4332 umap1 = isl_union_map_copy(prefix);
4333 umap1 = isl_union_map_intersect_domain(umap1, domain);
4334 umap2 = isl_union_map_intersect_domain(prefix, range);
4335 umap1 = isl_union_map_intersect_range(umap1,
4336 isl_union_set_from_set(context));
4337 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4338 umap = isl_union_map_intersect(umap, umap1);
4340 after = after_in_child(umap, node);
4342 isl_union_map_free(umap);
4344 return after;
4347 /* Is any domain element of "umap" scheduled after any of
4348 * the corresponding image elements by the tree rooted at
4349 * the expansion node "node"?
4351 * We apply the expansion to domain and range of "umap" and
4352 * continue with its child.
4354 static isl_bool after_in_expansion(__isl_keep isl_union_map *umap,
4355 __isl_keep isl_schedule_node *node)
4357 isl_union_map *expansion;
4358 isl_bool after;
4360 expansion = isl_schedule_node_expansion_get_expansion(node);
4361 umap = isl_union_map_copy(umap);
4362 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4363 umap = isl_union_map_apply_range(umap, expansion);
4365 after = after_in_child(umap, node);
4367 isl_union_map_free(umap);
4369 return after;
4372 /* Is any domain element of "umap" scheduled after any of
4373 * the corresponding image elements by the tree rooted at
4374 * the extension node "node"?
4376 * Since the extension node may add statement instances before or
4377 * after the pairs of statement instances in "umap", we return isl_bool_true
4378 * to ensure that these pairs are not broken up.
4380 static isl_bool after_in_extension(__isl_keep isl_union_map *umap,
4381 __isl_keep isl_schedule_node *node)
4383 return isl_bool_true;
4386 /* Is any domain element of "umap" scheduled after any of
4387 * the corresponding image elements by the tree rooted at
4388 * the filter node "node"?
4390 * We intersect domain and range of "umap" with the filter and
4391 * continue with its child.
4393 static isl_bool after_in_filter(__isl_keep isl_union_map *umap,
4394 __isl_keep isl_schedule_node *node)
4396 isl_union_set *filter;
4397 isl_bool after;
4399 umap = isl_union_map_copy(umap);
4400 filter = isl_schedule_node_filter_get_filter(node);
4401 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4402 umap = isl_union_map_intersect_range(umap, filter);
4404 after = after_in_child(umap, node);
4406 isl_union_map_free(umap);
4408 return after;
4411 /* Is any domain element of "umap" scheduled after any of
4412 * the corresponding image elements by the tree rooted at
4413 * the set node "node"?
4415 * This is only the case if this condition holds in any
4416 * of the (filter) children of the set node.
4417 * In particular, if the domain and the range of "umap"
4418 * are contained in different children, then the condition
4419 * does not hold.
4421 static isl_bool after_in_set(__isl_keep isl_union_map *umap,
4422 __isl_keep isl_schedule_node *node)
4424 int i, n;
4426 n = isl_schedule_node_n_children(node);
4427 for (i = 0; i < n; ++i) {
4428 isl_schedule_node *child;
4429 isl_bool after;
4431 child = isl_schedule_node_get_child(node, i);
4432 after = after_in_tree(umap, child);
4433 isl_schedule_node_free(child);
4435 if (after < 0 || after)
4436 return after;
4439 return isl_bool_false;
4442 /* Return the filter of child "i" of "node".
4444 static __isl_give isl_union_set *child_filter(
4445 __isl_keep isl_schedule_node *node, int i)
4447 isl_schedule_node *child;
4448 isl_union_set *filter;
4450 child = isl_schedule_node_get_child(node, i);
4451 filter = isl_schedule_node_filter_get_filter(child);
4452 isl_schedule_node_free(child);
4454 return filter;
4457 /* Is any domain element of "umap" scheduled after any of
4458 * the corresponding image elements by the tree rooted at
4459 * the sequence node "node"?
4461 * This happens in particular if any domain element is
4462 * contained in a later child than one containing a range element or
4463 * if the condition holds within a given child in the sequence.
4464 * The later part of the condition is checked by after_in_set.
4466 static isl_bool after_in_sequence(__isl_keep isl_union_map *umap,
4467 __isl_keep isl_schedule_node *node)
4469 int i, j, n;
4470 isl_union_map *umap_i;
4471 isl_bool empty;
4472 isl_bool after = isl_bool_false;
4474 n = isl_schedule_node_n_children(node);
4475 for (i = 1; i < n; ++i) {
4476 isl_union_set *filter_i;
4478 umap_i = isl_union_map_copy(umap);
4479 filter_i = child_filter(node, i);
4480 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4481 empty = isl_union_map_is_empty(umap_i);
4482 if (empty < 0)
4483 goto error;
4484 if (empty) {
4485 isl_union_map_free(umap_i);
4486 continue;
4489 for (j = 0; j < i; ++j) {
4490 isl_union_set *filter_j;
4491 isl_union_map *umap_ij;
4493 umap_ij = isl_union_map_copy(umap_i);
4494 filter_j = child_filter(node, j);
4495 umap_ij = isl_union_map_intersect_range(umap_ij,
4496 filter_j);
4497 empty = isl_union_map_is_empty(umap_ij);
4498 isl_union_map_free(umap_ij);
4500 if (empty < 0)
4501 goto error;
4502 if (!empty)
4503 after = isl_bool_true;
4504 if (after)
4505 break;
4508 isl_union_map_free(umap_i);
4509 if (after)
4510 break;
4513 if (after < 0 || after)
4514 return after;
4516 return after_in_set(umap, node);
4517 error:
4518 isl_union_map_free(umap_i);
4519 return isl_bool_error;
4522 /* Is any domain element of "umap" scheduled after any of
4523 * the corresponding image elements by the tree rooted at "node"?
4525 * If "umap" is empty, then clearly there is no such element.
4526 * Otherwise, consider the different types of nodes separately.
4528 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4529 __isl_keep isl_schedule_node *node)
4531 isl_bool empty;
4532 enum isl_schedule_node_type type;
4534 empty = isl_union_map_is_empty(umap);
4535 if (empty < 0)
4536 return isl_bool_error;
4537 if (empty)
4538 return isl_bool_false;
4539 if (!node)
4540 return isl_bool_error;
4542 type = isl_schedule_node_get_type(node);
4543 switch (type) {
4544 case isl_schedule_node_error:
4545 return isl_bool_error;
4546 case isl_schedule_node_leaf:
4547 return isl_bool_false;
4548 case isl_schedule_node_band:
4549 return after_in_band(umap, node);
4550 case isl_schedule_node_domain:
4551 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4552 "unexpected internal domain node",
4553 return isl_bool_error);
4554 case isl_schedule_node_context:
4555 return after_in_context(umap, node);
4556 case isl_schedule_node_expansion:
4557 return after_in_expansion(umap, node);
4558 case isl_schedule_node_extension:
4559 return after_in_extension(umap, node);
4560 case isl_schedule_node_filter:
4561 return after_in_filter(umap, node);
4562 case isl_schedule_node_guard:
4563 case isl_schedule_node_mark:
4564 return after_in_child(umap, node);
4565 case isl_schedule_node_set:
4566 return after_in_set(umap, node);
4567 case isl_schedule_node_sequence:
4568 return after_in_sequence(umap, node);
4571 return isl_bool_true;
4574 /* Is any domain element of "map1" scheduled after any domain
4575 * element of "map2" by the subtree underneath the current band node,
4576 * while at the same time being scheduled together by the current
4577 * band node, i.e., by "map1" and "map2?
4579 * If the child of the current band node is a leaf, then
4580 * no element can be scheduled after any other element.
4582 * Otherwise, we construct a relation between domain elements
4583 * of "map1" and domain elements of "map2" that are scheduled
4584 * together and then check if the subtree underneath the current
4585 * band node determines their relative order.
4587 static isl_bool after_in_subtree(__isl_keep isl_ast_build *build,
4588 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4590 isl_schedule_node *node;
4591 isl_map *map;
4592 isl_union_map *umap;
4593 isl_bool after;
4595 node = isl_ast_build_get_schedule_node(build);
4596 if (!node)
4597 return isl_bool_error;
4598 node = isl_schedule_node_child(node, 0);
4599 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4600 isl_schedule_node_free(node);
4601 return isl_bool_false;
4603 map = isl_map_copy(map2);
4604 map = isl_map_apply_domain(map, isl_map_copy(map1));
4605 umap = isl_union_map_from_map(map);
4606 after = after_in_tree(umap, node);
4607 isl_union_map_free(umap);
4608 isl_schedule_node_free(node);
4609 return after;
4612 /* Internal data for any_scheduled_after.
4614 * "build" is the build in which the AST is constructed.
4615 * "depth" is the number of loops that have already been generated
4616 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4617 * "domain" is an array of set-map pairs corresponding to the different
4618 * iteration domains. The set is the schedule domain, i.e., the domain
4619 * of the inverse schedule, while the map is the inverse schedule itself.
4621 struct isl_any_scheduled_after_data {
4622 isl_ast_build *build;
4623 int depth;
4624 int group_coscheduled;
4625 struct isl_set_map_pair *domain;
4628 /* Is any element of domain "i" scheduled after any element of domain "j"
4629 * (for a common iteration of the first data->depth loops)?
4631 * data->domain[i].set contains the domain of the inverse schedule
4632 * for domain "i", i.e., elements in the schedule domain.
4634 * If we are inside a band of a schedule tree and there is a pair
4635 * of elements in the two domains that is schedule together by
4636 * the current band, then we check if any element of "i" may be schedule
4637 * after element of "j" by the descendants of the band node.
4639 * If data->group_coscheduled is set, then we also return 1 if there
4640 * is any pair of elements in the two domains that are scheduled together.
4642 static isl_bool any_scheduled_after(int i, int j, void *user)
4644 struct isl_any_scheduled_after_data *data = user;
4645 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4646 int pos;
4648 for (pos = data->depth; pos < dim; ++pos) {
4649 int follows;
4651 follows = isl_set_follows_at(data->domain[i].set,
4652 data->domain[j].set, pos);
4654 if (follows < -1)
4655 return isl_bool_error;
4656 if (follows > 0)
4657 return isl_bool_true;
4658 if (follows < 0)
4659 return isl_bool_false;
4662 if (isl_ast_build_has_schedule_node(data->build)) {
4663 isl_bool after;
4665 after = after_in_subtree(data->build, data->domain[i].map,
4666 data->domain[j].map);
4667 if (after < 0 || after)
4668 return after;
4671 return data->group_coscheduled;
4674 /* Look for independent components at the current depth and generate code
4675 * for each component separately. The resulting lists of grafts are
4676 * merged in an attempt to combine grafts with identical guards.
4678 * Code for two domains can be generated separately if all the elements
4679 * of one domain are scheduled before (or together with) all the elements
4680 * of the other domain. We therefore consider the graph with as nodes
4681 * the domains and an edge between two nodes if any element of the first
4682 * node is scheduled after any element of the second node.
4683 * If the ast_build_group_coscheduled is set, then we also add an edge if
4684 * there is any pair of elements in the two domains that are scheduled
4685 * together.
4686 * Code is then generated (by generate_component)
4687 * for each of the strongly connected components in this graph
4688 * in their topological order.
4690 * Since the test is performed on the domain of the inverse schedules of
4691 * the different domains, we precompute these domains and store
4692 * them in data.domain.
4694 static __isl_give isl_ast_graft_list *generate_components(
4695 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4697 int i;
4698 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4699 int n = isl_union_map_n_map(executed);
4700 struct isl_any_scheduled_after_data data;
4701 struct isl_set_map_pair *next;
4702 struct isl_tarjan_graph *g = NULL;
4703 isl_ast_graft_list *list = NULL;
4704 int n_domain = 0;
4706 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4707 if (!data.domain)
4708 goto error;
4709 n_domain = n;
4711 next = data.domain;
4712 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4713 goto error;
4715 if (!build)
4716 goto error;
4717 data.build = build;
4718 data.depth = isl_ast_build_get_depth(build);
4719 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4720 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4721 if (!g)
4722 goto error;
4724 list = isl_ast_graft_list_alloc(ctx, 0);
4726 i = 0;
4727 while (list && n) {
4728 isl_ast_graft_list *list_c;
4729 int first = i;
4731 if (g->order[i] == -1)
4732 isl_die(ctx, isl_error_internal, "cannot happen",
4733 goto error);
4734 ++i; --n;
4735 while (g->order[i] != -1) {
4736 ++i; --n;
4739 list_c = generate_component(data.domain,
4740 g->order + first, i - first,
4741 isl_ast_build_copy(build));
4742 list = isl_ast_graft_list_merge(list, list_c, build);
4744 ++i;
4747 if (0)
4748 error: list = isl_ast_graft_list_free(list);
4749 isl_tarjan_graph_free(g);
4750 for (i = 0; i < n_domain; ++i) {
4751 isl_map_free(data.domain[i].map);
4752 isl_set_free(data.domain[i].set);
4754 free(data.domain);
4755 isl_union_map_free(executed);
4756 isl_ast_build_free(build);
4758 return list;
4761 /* Generate code for the next level (and all inner levels).
4763 * If "executed" is empty, i.e., no code needs to be generated,
4764 * then we return an empty list.
4766 * If we have already generated code for all loop levels, then we pass
4767 * control to generate_inner_level.
4769 * If "executed" lives in a single space, i.e., if code needs to be
4770 * generated for a single domain, then there can only be a single
4771 * component and we go directly to generate_shifted_component.
4772 * Otherwise, we call generate_components to detect the components
4773 * and to call generate_component on each of them separately.
4775 static __isl_give isl_ast_graft_list *generate_next_level(
4776 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4778 int depth;
4780 if (!build || !executed)
4781 goto error;
4783 if (isl_union_map_is_empty(executed)) {
4784 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4785 isl_union_map_free(executed);
4786 isl_ast_build_free(build);
4787 return isl_ast_graft_list_alloc(ctx, 0);
4790 depth = isl_ast_build_get_depth(build);
4791 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4792 return generate_inner_level(executed, build);
4794 if (isl_union_map_n_map(executed) == 1)
4795 return generate_shifted_component(executed, build);
4797 return generate_components(executed, build);
4798 error:
4799 isl_union_map_free(executed);
4800 isl_ast_build_free(build);
4801 return NULL;
4804 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4805 * internal, executed and build are the inputs to generate_code.
4806 * list collects the output.
4808 struct isl_generate_code_data {
4809 int internal;
4810 isl_union_map *executed;
4811 isl_ast_build *build;
4813 isl_ast_graft_list *list;
4816 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4818 * [E -> S] -> D
4820 * with E the external build schedule and S the additional schedule "space",
4821 * reformulate the inverse schedule in terms of the internal schedule domain,
4822 * i.e., return
4824 * [I -> S] -> D
4826 * We first obtain a mapping
4828 * I -> E
4830 * take the inverse and the product with S -> S, resulting in
4832 * [I -> S] -> [E -> S]
4834 * Applying the map to the input produces the desired result.
4836 static __isl_give isl_union_map *internal_executed(
4837 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4838 __isl_keep isl_ast_build *build)
4840 isl_map *id, *proj;
4842 proj = isl_ast_build_get_schedule_map(build);
4843 proj = isl_map_reverse(proj);
4844 space = isl_space_map_from_set(isl_space_copy(space));
4845 id = isl_map_identity(space);
4846 proj = isl_map_product(proj, id);
4847 executed = isl_union_map_apply_domain(executed,
4848 isl_union_map_from_map(proj));
4849 return executed;
4852 /* Generate an AST that visits the elements in the range of data->executed
4853 * in the relative order specified by the corresponding domain element(s)
4854 * for those domain elements that belong to "set".
4855 * Add the result to data->list.
4857 * The caller ensures that "set" is a universe domain.
4858 * "space" is the space of the additional part of the schedule.
4859 * It is equal to the space of "set" if build->domain is parametric.
4860 * Otherwise, it is equal to the range of the wrapped space of "set".
4862 * If the build space is not parametric and
4863 * if isl_ast_build_node_from_schedule_map
4864 * was called from an outside user (data->internal not set), then
4865 * the (inverse) schedule refers to the external build domain and needs to
4866 * be transformed to refer to the internal build domain.
4868 * If the build space is parametric, then we add some of the parameter
4869 * constraints to the executed relation. Adding these constraints
4870 * allows for an earlier detection of conflicts in some cases.
4871 * However, we do not want to divide the executed relation into
4872 * more disjuncts than necessary. We therefore approximate
4873 * the constraints on the parameters by a single disjunct set.
4875 * The build is extended to include the additional part of the schedule.
4876 * If the original build space was not parametric, then the options
4877 * in data->build refer only to the additional part of the schedule
4878 * and they need to be adjusted to refer to the complete AST build
4879 * domain.
4881 * After having adjusted inverse schedule and build, we start generating
4882 * code with the outer loop of the current code generation
4883 * in generate_next_level.
4885 * If the original build space was not parametric, we undo the embedding
4886 * on the resulting isl_ast_node_list so that it can be used within
4887 * the outer AST build.
4889 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4890 __isl_take isl_set *set, __isl_take isl_space *space)
4892 isl_union_map *executed;
4893 isl_ast_build *build;
4894 isl_ast_graft_list *list;
4895 int embed;
4897 executed = isl_union_map_copy(data->executed);
4898 executed = isl_union_map_intersect_domain(executed,
4899 isl_union_set_from_set(set));
4901 embed = !isl_set_is_params(data->build->domain);
4902 if (embed && !data->internal)
4903 executed = internal_executed(executed, space, data->build);
4904 if (!embed) {
4905 isl_set *domain;
4906 domain = isl_ast_build_get_domain(data->build);
4907 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4908 executed = isl_union_map_intersect_params(executed, domain);
4911 build = isl_ast_build_copy(data->build);
4912 build = isl_ast_build_product(build, space);
4914 list = generate_next_level(executed, build);
4916 list = isl_ast_graft_list_unembed(list, embed);
4918 data->list = isl_ast_graft_list_concat(data->list, list);
4920 return isl_stat_ok;
4923 /* Generate an AST that visits the elements in the range of data->executed
4924 * in the relative order specified by the corresponding domain element(s)
4925 * for those domain elements that belong to "set".
4926 * Add the result to data->list.
4928 * The caller ensures that "set" is a universe domain.
4930 * If the build space S is not parametric, then the space of "set"
4931 * need to be a wrapped relation with S as domain. That is, it needs
4932 * to be of the form
4934 * [S -> T]
4936 * Check this property and pass control to generate_code_in_space
4937 * passing along T.
4938 * If the build space is not parametric, then T is the space of "set".
4940 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4942 struct isl_generate_code_data *data = user;
4943 isl_space *space, *build_space;
4944 int is_domain;
4946 space = isl_set_get_space(set);
4948 if (isl_set_is_params(data->build->domain))
4949 return generate_code_in_space(data, set, space);
4951 build_space = isl_ast_build_get_space(data->build, data->internal);
4952 space = isl_space_unwrap(space);
4953 is_domain = isl_space_is_domain(build_space, space);
4954 isl_space_free(build_space);
4955 space = isl_space_range(space);
4957 if (is_domain < 0)
4958 goto error;
4959 if (!is_domain)
4960 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4961 "invalid nested schedule space", goto error);
4963 return generate_code_in_space(data, set, space);
4964 error:
4965 isl_set_free(set);
4966 isl_space_free(space);
4967 return isl_stat_error;
4970 /* Generate an AST that visits the elements in the range of "executed"
4971 * in the relative order specified by the corresponding domain element(s).
4973 * "build" is an isl_ast_build that has either been constructed by
4974 * isl_ast_build_from_context or passed to a callback set by
4975 * isl_ast_build_set_create_leaf.
4976 * In the first case, the space of the isl_ast_build is typically
4977 * a parametric space, although this is currently not enforced.
4978 * In the second case, the space is never a parametric space.
4979 * If the space S is not parametric, then the domain space(s) of "executed"
4980 * need to be wrapped relations with S as domain.
4982 * If the domain of "executed" consists of several spaces, then an AST
4983 * is generated for each of them (in arbitrary order) and the results
4984 * are concatenated.
4986 * If "internal" is set, then the domain "S" above refers to the internal
4987 * schedule domain representation. Otherwise, it refers to the external
4988 * representation, as returned by isl_ast_build_get_schedule_space.
4990 * We essentially run over all the spaces in the domain of "executed"
4991 * and call generate_code_set on each of them.
4993 static __isl_give isl_ast_graft_list *generate_code(
4994 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4995 int internal)
4997 isl_ctx *ctx;
4998 struct isl_generate_code_data data = { 0 };
4999 isl_space *space;
5000 isl_union_set *schedule_domain;
5001 isl_union_map *universe;
5003 if (!build)
5004 goto error;
5005 space = isl_ast_build_get_space(build, 1);
5006 space = isl_space_align_params(space,
5007 isl_union_map_get_space(executed));
5008 space = isl_space_align_params(space,
5009 isl_union_map_get_space(build->options));
5010 build = isl_ast_build_align_params(build, isl_space_copy(space));
5011 executed = isl_union_map_align_params(executed, space);
5012 if (!executed || !build)
5013 goto error;
5015 ctx = isl_ast_build_get_ctx(build);
5017 data.internal = internal;
5018 data.executed = executed;
5019 data.build = build;
5020 data.list = isl_ast_graft_list_alloc(ctx, 0);
5022 universe = isl_union_map_universe(isl_union_map_copy(executed));
5023 schedule_domain = isl_union_map_domain(universe);
5024 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5025 &data) < 0)
5026 data.list = isl_ast_graft_list_free(data.list);
5028 isl_union_set_free(schedule_domain);
5029 isl_union_map_free(executed);
5031 isl_ast_build_free(build);
5032 return data.list;
5033 error:
5034 isl_union_map_free(executed);
5035 isl_ast_build_free(build);
5036 return NULL;
5039 /* Generate an AST that visits the elements in the domain of "schedule"
5040 * in the relative order specified by the corresponding image element(s).
5042 * "build" is an isl_ast_build that has either been constructed by
5043 * isl_ast_build_from_context or passed to a callback set by
5044 * isl_ast_build_set_create_leaf.
5045 * In the first case, the space of the isl_ast_build is typically
5046 * a parametric space, although this is currently not enforced.
5047 * In the second case, the space is never a parametric space.
5048 * If the space S is not parametric, then the range space(s) of "schedule"
5049 * need to be wrapped relations with S as domain.
5051 * If the range of "schedule" consists of several spaces, then an AST
5052 * is generated for each of them (in arbitrary order) and the results
5053 * are concatenated.
5055 * We first initialize the local copies of the relevant options.
5056 * We do this here rather than when the isl_ast_build is created
5057 * because the options may have changed between the construction
5058 * of the isl_ast_build and the call to isl_generate_code.
5060 * The main computation is performed on an inverse schedule (with
5061 * the schedule domain in the domain and the elements to be executed
5062 * in the range) called "executed".
5064 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5065 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5067 isl_ast_graft_list *list;
5068 isl_ast_node *node;
5069 isl_union_map *executed;
5071 build = isl_ast_build_copy(build);
5072 build = isl_ast_build_set_single_valued(build, 0);
5073 schedule = isl_union_map_coalesce(schedule);
5074 schedule = isl_union_map_remove_redundancies(schedule);
5075 executed = isl_union_map_reverse(schedule);
5076 list = generate_code(executed, isl_ast_build_copy(build), 0);
5077 node = isl_ast_node_from_graft_list(list, build);
5078 isl_ast_build_free(build);
5080 return node;
5083 /* The old name for isl_ast_build_node_from_schedule_map.
5084 * It is being kept for backward compatibility, but
5085 * it will be removed in the future.
5087 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5088 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5090 return isl_ast_build_node_from_schedule_map(build, schedule);
5093 /* Generate an AST that visits the elements in the domain of "executed"
5094 * in the relative order specified by the band node "node" and its descendants.
5096 * The relation "executed" maps the outer generated loop iterators
5097 * to the domain elements executed by those iterations.
5099 * If the band is empty, we continue with its descendants.
5100 * Otherwise, we extend the build and the inverse schedule with
5101 * the additional space/partial schedule and continue generating
5102 * an AST in generate_next_level.
5103 * As soon as we have extended the inverse schedule with the additional
5104 * partial schedule, we look for equalities that may exists between
5105 * the old and the new part.
5107 static __isl_give isl_ast_graft_list *build_ast_from_band(
5108 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5109 __isl_take isl_union_map *executed)
5111 isl_space *space;
5112 isl_multi_union_pw_aff *extra;
5113 isl_union_map *extra_umap;
5114 isl_ast_graft_list *list;
5115 unsigned n1, n2;
5117 if (!build || !node || !executed)
5118 goto error;
5120 if (isl_schedule_node_band_n_member(node) == 0)
5121 return build_ast_from_child(build, node, executed);
5123 extra = isl_schedule_node_band_get_partial_schedule(node);
5124 extra = isl_multi_union_pw_aff_align_params(extra,
5125 isl_ast_build_get_space(build, 1));
5126 space = isl_multi_union_pw_aff_get_space(extra);
5128 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5129 extra_umap = isl_union_map_reverse(extra_umap);
5131 executed = isl_union_map_domain_product(executed, extra_umap);
5132 executed = isl_union_map_detect_equalities(executed);
5134 n1 = isl_ast_build_dim(build, isl_dim_param);
5135 build = isl_ast_build_product(build, space);
5136 n2 = isl_ast_build_dim(build, isl_dim_param);
5137 if (n2 > n1)
5138 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5139 "band node is not allowed to introduce new parameters",
5140 build = isl_ast_build_free(build));
5141 build = isl_ast_build_set_schedule_node(build, node);
5143 list = generate_next_level(executed, build);
5145 list = isl_ast_graft_list_unembed(list, 1);
5147 return list;
5148 error:
5149 isl_schedule_node_free(node);
5150 isl_union_map_free(executed);
5151 isl_ast_build_free(build);
5152 return NULL;
5155 /* Hoist a list of grafts (in practice containing a single graft)
5156 * from "sub_build" (which includes extra context information)
5157 * to "build".
5159 * In particular, project out all additional parameters introduced
5160 * by the context node from the enforced constraints and the guard
5161 * of the single graft.
5163 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5164 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5165 __isl_keep isl_ast_build *sub_build)
5167 isl_ast_graft *graft;
5168 isl_basic_set *enforced;
5169 isl_set *guard;
5170 unsigned n_param, extra_param;
5172 if (!build || !sub_build)
5173 return isl_ast_graft_list_free(list);
5175 n_param = isl_ast_build_dim(build, isl_dim_param);
5176 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5178 if (extra_param == n_param)
5179 return list;
5181 extra_param -= n_param;
5182 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5183 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5184 n_param, extra_param);
5185 enforced = isl_basic_set_remove_unknown_divs(enforced);
5186 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5187 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5188 n_param, extra_param);
5189 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5190 guard = isl_set_compute_divs(guard);
5191 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5192 build, sub_build);
5193 list = isl_ast_graft_list_from_ast_graft(graft);
5195 return list;
5198 /* Generate an AST that visits the elements in the domain of "executed"
5199 * in the relative order specified by the context node "node"
5200 * and its descendants.
5202 * The relation "executed" maps the outer generated loop iterators
5203 * to the domain elements executed by those iterations.
5205 * The context node may introduce additional parameters as well as
5206 * constraints on the outer schedule dimensions or original parameters.
5208 * We add the extra parameters to a new build and the context
5209 * constraints to both the build and (as a single disjunct)
5210 * to the domain of "executed". Since the context constraints
5211 * are specified in terms of the input schedule, we first need
5212 * to map them to the internal schedule domain.
5214 * After constructing the AST from the descendants of "node",
5215 * we combine the list of grafts into a single graft within
5216 * the new build, in order to be able to exploit the additional
5217 * context constraints during this combination.
5219 * Additionally, if the current node is the outermost node in
5220 * the schedule tree (apart from the root domain node), we generate
5221 * all pending guards, again to be able to exploit the additional
5222 * context constraints. We currently do not do this for internal
5223 * context nodes since we may still want to hoist conditions
5224 * to outer AST nodes.
5226 * If the context node introduced any new parameters, then they
5227 * are removed from the set of enforced constraints and guard
5228 * in hoist_out_of_context.
5230 static __isl_give isl_ast_graft_list *build_ast_from_context(
5231 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5232 __isl_take isl_union_map *executed)
5234 isl_set *context;
5235 isl_space *space;
5236 isl_multi_aff *internal2input;
5237 isl_ast_build *sub_build;
5238 isl_ast_graft_list *list;
5239 int n, depth;
5241 depth = isl_schedule_node_get_tree_depth(node);
5242 space = isl_ast_build_get_space(build, 1);
5243 context = isl_schedule_node_context_get_context(node);
5244 context = isl_set_align_params(context, space);
5245 sub_build = isl_ast_build_copy(build);
5246 space = isl_set_get_space(context);
5247 sub_build = isl_ast_build_align_params(sub_build, space);
5248 internal2input = isl_ast_build_get_internal2input(sub_build);
5249 context = isl_set_preimage_multi_aff(context, internal2input);
5250 sub_build = isl_ast_build_restrict_generated(sub_build,
5251 isl_set_copy(context));
5252 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5253 executed = isl_union_map_intersect_domain(executed,
5254 isl_union_set_from_set(context));
5256 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5257 node, executed);
5258 n = isl_ast_graft_list_n_ast_graft(list);
5259 if (n < 0)
5260 list = isl_ast_graft_list_free(list);
5262 list = isl_ast_graft_list_fuse(list, sub_build);
5263 if (depth == 1)
5264 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5265 sub_build);
5266 if (n >= 1)
5267 list = hoist_out_of_context(list, build, sub_build);
5269 isl_ast_build_free(build);
5270 isl_ast_build_free(sub_build);
5272 return list;
5275 /* Generate an AST that visits the elements in the domain of "executed"
5276 * in the relative order specified by the expansion node "node" and
5277 * its descendants.
5279 * The relation "executed" maps the outer generated loop iterators
5280 * to the domain elements executed by those iterations.
5282 * We expand the domain elements by the expansion and
5283 * continue with the descendants of the node.
5285 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5286 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5287 __isl_take isl_union_map *executed)
5289 isl_union_map *expansion;
5290 unsigned n1, n2;
5292 expansion = isl_schedule_node_expansion_get_expansion(node);
5293 expansion = isl_union_map_align_params(expansion,
5294 isl_union_map_get_space(executed));
5296 n1 = isl_union_map_dim(executed, isl_dim_param);
5297 executed = isl_union_map_apply_range(executed, expansion);
5298 n2 = isl_union_map_dim(executed, isl_dim_param);
5299 if (n2 > n1)
5300 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5301 "expansion node is not allowed to introduce "
5302 "new parameters", goto error);
5304 return build_ast_from_child(build, node, executed);
5305 error:
5306 isl_ast_build_free(build);
5307 isl_schedule_node_free(node);
5308 isl_union_map_free(executed);
5309 return NULL;
5312 /* Generate an AST that visits the elements in the domain of "executed"
5313 * in the relative order specified by the extension node "node" and
5314 * its descendants.
5316 * The relation "executed" maps the outer generated loop iterators
5317 * to the domain elements executed by those iterations.
5319 * Extend the inverse schedule with the extension applied to current
5320 * set of generated constraints. Since the extension if formulated
5321 * in terms of the input schedule, it first needs to be transformed
5322 * to refer to the internal schedule.
5324 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5325 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5326 __isl_take isl_union_map *executed)
5328 isl_union_set *schedule_domain;
5329 isl_union_map *extension;
5330 isl_set *set;
5332 set = isl_ast_build_get_generated(build);
5333 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5334 schedule_domain = isl_union_set_from_set(set);
5336 extension = isl_schedule_node_extension_get_extension(node);
5338 extension = isl_union_map_preimage_domain_multi_aff(extension,
5339 isl_multi_aff_copy(build->internal2input));
5340 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5341 extension = isl_ast_build_substitute_values_union_map_domain(build,
5342 extension);
5343 executed = isl_union_map_union(executed, extension);
5345 return build_ast_from_child(build, node, executed);
5348 /* Generate an AST that visits the elements in the domain of "executed"
5349 * in the relative order specified by the filter node "node" and
5350 * its descendants.
5352 * The relation "executed" maps the outer generated loop iterators
5353 * to the domain elements executed by those iterations.
5355 * We simply intersect the iteration domain (i.e., the range of "executed")
5356 * with the filter and continue with the descendants of the node,
5357 * unless the resulting inverse schedule is empty, in which
5358 * case we return an empty list.
5360 * If the result of the intersection is equal to the original "executed"
5361 * relation, then keep the original representation since the intersection
5362 * may have unnecessarily broken up the relation into a greater number
5363 * of disjuncts.
5365 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5366 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5367 __isl_take isl_union_map *executed)
5369 isl_ctx *ctx;
5370 isl_union_set *filter;
5371 isl_union_map *orig;
5372 isl_ast_graft_list *list;
5373 int empty;
5374 isl_bool unchanged;
5375 unsigned n1, n2;
5377 orig = isl_union_map_copy(executed);
5378 if (!build || !node || !executed)
5379 goto error;
5381 filter = isl_schedule_node_filter_get_filter(node);
5382 filter = isl_union_set_align_params(filter,
5383 isl_union_map_get_space(executed));
5384 n1 = isl_union_map_dim(executed, isl_dim_param);
5385 executed = isl_union_map_intersect_range(executed, filter);
5386 n2 = isl_union_map_dim(executed, isl_dim_param);
5387 if (n2 > n1)
5388 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5389 "filter node is not allowed to introduce "
5390 "new parameters", goto error);
5392 unchanged = isl_union_map_is_subset(orig, executed);
5393 empty = isl_union_map_is_empty(executed);
5394 if (unchanged < 0 || empty < 0)
5395 goto error;
5396 if (unchanged) {
5397 isl_union_map_free(executed);
5398 return build_ast_from_child(build, node, orig);
5400 isl_union_map_free(orig);
5401 if (!empty)
5402 return build_ast_from_child(build, node, executed);
5404 ctx = isl_ast_build_get_ctx(build);
5405 list = isl_ast_graft_list_alloc(ctx, 0);
5406 isl_ast_build_free(build);
5407 isl_schedule_node_free(node);
5408 isl_union_map_free(executed);
5409 return list;
5410 error:
5411 isl_ast_build_free(build);
5412 isl_schedule_node_free(node);
5413 isl_union_map_free(executed);
5414 isl_union_map_free(orig);
5415 return NULL;
5418 /* Generate an AST that visits the elements in the domain of "executed"
5419 * in the relative order specified by the guard node "node" and
5420 * its descendants.
5422 * The relation "executed" maps the outer generated loop iterators
5423 * to the domain elements executed by those iterations.
5425 * Ensure that the associated guard is enforced by the outer AST
5426 * constructs by adding it to the guard of the graft.
5427 * Since we know that we will enforce the guard, we can also include it
5428 * in the generated constraints used to construct an AST for
5429 * the descendant nodes.
5431 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5432 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5433 __isl_take isl_union_map *executed)
5435 isl_space *space;
5436 isl_set *guard, *hoisted;
5437 isl_basic_set *enforced;
5438 isl_ast_build *sub_build;
5439 isl_ast_graft *graft;
5440 isl_ast_graft_list *list;
5441 unsigned n1, n2;
5443 space = isl_ast_build_get_space(build, 1);
5444 guard = isl_schedule_node_guard_get_guard(node);
5445 n1 = isl_space_dim(space, isl_dim_param);
5446 guard = isl_set_align_params(guard, space);
5447 n2 = isl_set_dim(guard, isl_dim_param);
5448 if (n2 > n1)
5449 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5450 "guard node is not allowed to introduce "
5451 "new parameters", guard = isl_set_free(guard));
5452 guard = isl_set_preimage_multi_aff(guard,
5453 isl_multi_aff_copy(build->internal2input));
5454 guard = isl_ast_build_specialize(build, guard);
5455 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5457 sub_build = isl_ast_build_copy(build);
5458 sub_build = isl_ast_build_restrict_generated(sub_build,
5459 isl_set_copy(guard));
5461 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5462 node, executed);
5464 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5465 if (isl_set_n_basic_set(hoisted) > 1)
5466 list = isl_ast_graft_list_gist_guards(list,
5467 isl_set_copy(hoisted));
5468 guard = isl_set_intersect(guard, hoisted);
5469 enforced = extract_shared_enforced(list, build);
5470 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5471 build, sub_build);
5473 isl_ast_build_free(sub_build);
5474 isl_ast_build_free(build);
5475 return isl_ast_graft_list_from_ast_graft(graft);
5478 /* Call the before_each_mark callback, if requested by the user.
5480 * Return 0 on success and -1 on error.
5482 * The caller is responsible for recording the current inverse schedule
5483 * in "build".
5485 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5486 __isl_keep isl_ast_build *build)
5488 if (!build)
5489 return isl_stat_error;
5490 if (!build->before_each_mark)
5491 return isl_stat_ok;
5492 return build->before_each_mark(mark, build,
5493 build->before_each_mark_user);
5496 /* Call the after_each_mark callback, if requested by the user.
5498 * The caller is responsible for recording the current inverse schedule
5499 * in "build".
5501 static __isl_give isl_ast_graft *after_each_mark(
5502 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5504 if (!graft || !build)
5505 return isl_ast_graft_free(graft);
5506 if (!build->after_each_mark)
5507 return graft;
5508 graft->node = build->after_each_mark(graft->node, build,
5509 build->after_each_mark_user);
5510 if (!graft->node)
5511 return isl_ast_graft_free(graft);
5512 return graft;
5516 /* Generate an AST that visits the elements in the domain of "executed"
5517 * in the relative order specified by the mark node "node" and
5518 * its descendants.
5520 * The relation "executed" maps the outer generated loop iterators
5521 * to the domain elements executed by those iterations.
5523 * Since we may be calling before_each_mark and after_each_mark
5524 * callbacks, we record the current inverse schedule in the build.
5526 * We generate an AST for the child of the mark node, combine
5527 * the graft list into a single graft and then insert the mark
5528 * in the AST of that single graft.
5530 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5531 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5532 __isl_take isl_union_map *executed)
5534 isl_id *mark;
5535 isl_ast_graft *graft;
5536 isl_ast_graft_list *list;
5537 int n;
5539 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5541 mark = isl_schedule_node_mark_get_id(node);
5542 if (before_each_mark(mark, build) < 0)
5543 node = isl_schedule_node_free(node);
5545 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5546 list = isl_ast_graft_list_fuse(list, build);
5547 n = isl_ast_graft_list_n_ast_graft(list);
5548 if (n < 0)
5549 list = isl_ast_graft_list_free(list);
5550 if (n == 0) {
5551 isl_id_free(mark);
5552 } else {
5553 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5554 graft = isl_ast_graft_insert_mark(graft, mark);
5555 graft = after_each_mark(graft, build);
5556 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5558 isl_ast_build_free(build);
5560 return list;
5563 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5564 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5565 __isl_take isl_union_map *executed);
5567 /* Generate an AST that visits the elements in the domain of "executed"
5568 * in the relative order specified by the sequence (or set) node "node" and
5569 * its descendants.
5571 * The relation "executed" maps the outer generated loop iterators
5572 * to the domain elements executed by those iterations.
5574 * We simply generate an AST for each of the children and concatenate
5575 * the results.
5577 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5578 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5579 __isl_take isl_union_map *executed)
5581 int i, n;
5582 isl_ctx *ctx;
5583 isl_ast_graft_list *list;
5585 ctx = isl_ast_build_get_ctx(build);
5586 list = isl_ast_graft_list_alloc(ctx, 0);
5588 n = isl_schedule_node_n_children(node);
5589 for (i = 0; i < n; ++i) {
5590 isl_schedule_node *child;
5591 isl_ast_graft_list *list_i;
5593 child = isl_schedule_node_get_child(node, i);
5594 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5595 child, isl_union_map_copy(executed));
5596 list = isl_ast_graft_list_concat(list, list_i);
5598 isl_ast_build_free(build);
5599 isl_schedule_node_free(node);
5600 isl_union_map_free(executed);
5602 return list;
5605 /* Generate an AST that visits the elements in the domain of "executed"
5606 * in the relative order specified by the node "node" and its descendants.
5608 * The relation "executed" maps the outer generated loop iterators
5609 * to the domain elements executed by those iterations.
5611 * If the node is a leaf, then we pass control to generate_inner_level.
5612 * Note that the current build does not refer to any band node, so
5613 * that generate_inner_level will not try to visit the child of
5614 * the leaf node.
5616 * The other node types are handled in separate functions.
5617 * Set nodes are currently treated in the same way as sequence nodes.
5618 * The children of a set node may be executed in any order,
5619 * including the order of the children.
5621 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5622 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5623 __isl_take isl_union_map *executed)
5625 enum isl_schedule_node_type type;
5627 type = isl_schedule_node_get_type(node);
5629 switch (type) {
5630 case isl_schedule_node_error:
5631 goto error;
5632 case isl_schedule_node_leaf:
5633 isl_schedule_node_free(node);
5634 return generate_inner_level(executed, build);
5635 case isl_schedule_node_band:
5636 return build_ast_from_band(build, node, executed);
5637 case isl_schedule_node_context:
5638 return build_ast_from_context(build, node, executed);
5639 case isl_schedule_node_domain:
5640 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5641 "unexpected internal domain node", goto error);
5642 case isl_schedule_node_expansion:
5643 return build_ast_from_expansion(build, node, executed);
5644 case isl_schedule_node_extension:
5645 return build_ast_from_extension(build, node, executed);
5646 case isl_schedule_node_filter:
5647 return build_ast_from_filter(build, node, executed);
5648 case isl_schedule_node_guard:
5649 return build_ast_from_guard(build, node, executed);
5650 case isl_schedule_node_mark:
5651 return build_ast_from_mark(build, node, executed);
5652 case isl_schedule_node_sequence:
5653 case isl_schedule_node_set:
5654 return build_ast_from_sequence(build, node, executed);
5657 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5658 "unhandled type", goto error);
5659 error:
5660 isl_union_map_free(executed);
5661 isl_schedule_node_free(node);
5662 isl_ast_build_free(build);
5664 return NULL;
5667 /* Generate an AST that visits the elements in the domain of "executed"
5668 * in the relative order specified by the (single) child of "node" and
5669 * its descendants.
5671 * The relation "executed" maps the outer generated loop iterators
5672 * to the domain elements executed by those iterations.
5674 * This function is never called on a leaf, set or sequence node,
5675 * so the node always has exactly one child.
5677 static __isl_give isl_ast_graft_list *build_ast_from_child(
5678 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5679 __isl_take isl_union_map *executed)
5681 node = isl_schedule_node_child(node, 0);
5682 return build_ast_from_schedule_node(build, node, executed);
5685 /* Generate an AST that visits the elements in the domain of the domain
5686 * node "node" in the relative order specified by its descendants.
5688 * An initial inverse schedule is created that maps a zero-dimensional
5689 * schedule space to the node domain.
5690 * The input "build" is assumed to have a parametric domain and
5691 * is replaced by the same zero-dimensional schedule space.
5693 * We also add some of the parameter constraints in the build domain
5694 * to the executed relation. Adding these constraints
5695 * allows for an earlier detection of conflicts in some cases.
5696 * However, we do not want to divide the executed relation into
5697 * more disjuncts than necessary. We therefore approximate
5698 * the constraints on the parameters by a single disjunct set.
5700 static __isl_give isl_ast_node *build_ast_from_domain(
5701 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5703 isl_ctx *ctx;
5704 isl_union_set *domain, *schedule_domain;
5705 isl_union_map *executed;
5706 isl_space *space;
5707 isl_set *set;
5708 isl_ast_graft_list *list;
5709 isl_ast_node *ast;
5710 int is_params;
5712 if (!build)
5713 goto error;
5715 ctx = isl_ast_build_get_ctx(build);
5716 space = isl_ast_build_get_space(build, 1);
5717 is_params = isl_space_is_params(space);
5718 isl_space_free(space);
5719 if (is_params < 0)
5720 goto error;
5721 if (!is_params)
5722 isl_die(ctx, isl_error_unsupported,
5723 "expecting parametric initial context", goto error);
5725 domain = isl_schedule_node_domain_get_domain(node);
5726 domain = isl_union_set_coalesce(domain);
5728 space = isl_union_set_get_space(domain);
5729 space = isl_space_set_from_params(space);
5730 build = isl_ast_build_product(build, space);
5732 set = isl_ast_build_get_domain(build);
5733 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5734 schedule_domain = isl_union_set_from_set(set);
5736 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5737 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5738 ast = isl_ast_node_from_graft_list(list, build);
5739 isl_ast_build_free(build);
5741 return ast;
5742 error:
5743 isl_schedule_node_free(node);
5744 isl_ast_build_free(build);
5745 return NULL;
5748 /* Generate an AST that visits the elements in the domain of "schedule"
5749 * in the relative order specified by the schedule tree.
5751 * "build" is an isl_ast_build that has been created using
5752 * isl_ast_build_alloc or isl_ast_build_from_context based
5753 * on a parametric set.
5755 * The construction starts at the root node of the schedule,
5756 * which is assumed to be a domain node.
5758 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5759 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5761 isl_ctx *ctx;
5762 isl_schedule_node *node;
5764 if (!build || !schedule)
5765 goto error;
5767 ctx = isl_ast_build_get_ctx(build);
5769 node = isl_schedule_get_root(schedule);
5770 if (!node)
5771 goto error;
5772 isl_schedule_free(schedule);
5774 build = isl_ast_build_copy(build);
5775 build = isl_ast_build_set_single_valued(build, 0);
5776 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5777 isl_die(ctx, isl_error_unsupported,
5778 "expecting root domain node",
5779 build = isl_ast_build_free(build));
5780 return build_ast_from_domain(build, node);
5781 error:
5782 isl_schedule_free(schedule);
5783 return NULL;