isl_ast_codegen.c: add_domain: fix typo in comment
[isl.git] / isl_ast_codegen.c
blob03fb432ce90a9b3a18feb4476a1c6b03cd3022b4
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);
100 list = generate_code(isl_union_map_from_map(executed), build, 1);
102 data->list = isl_ast_graft_list_concat(data->list, list);
104 return isl_stat_ok;
107 /* Call the at_each_domain callback, if requested by the user,
108 * after recording the current inverse schedule in the build.
110 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
111 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
113 if (!graft || !build)
114 return isl_ast_graft_free(graft);
115 if (!build->at_each_domain)
116 return graft;
118 build = isl_ast_build_copy(build);
119 build = isl_ast_build_set_executed(build,
120 isl_union_map_from_map(isl_map_copy(executed)));
121 if (!build)
122 return isl_ast_graft_free(graft);
124 graft->node = build->at_each_domain(graft->node,
125 build, build->at_each_domain_user);
126 isl_ast_build_free(build);
128 if (!graft->node)
129 graft = isl_ast_graft_free(graft);
131 return graft;
134 /* Generate a call expression for the single executed
135 * domain element "map" and put a guard around it based on its (simplified)
136 * domain. "executed" is the original inverse schedule from which "map"
137 * has been derived. In particular, "map" is identical to "executed".
138 * "executed" is only used if there is an at_each_domain callback.
140 * At this stage, any pending constraints in the build can no longer
141 * be simplified with respect to any enforced constraints since
142 * the call node does not have any enforced constraints.
143 * Since all pending constraints not covered by any enforced constraints
144 * will be added as a guard to the graft in create_node_scaled,
145 * even in the eliminated case, the pending constraints
146 * can be considered to have been generated by outer constructs.
148 * If the user has set an at_each_domain callback, it is called
149 * on the constructed call expression node.
151 static isl_stat add_domain(__isl_take isl_map *executed,
152 __isl_take isl_map *map, struct isl_generate_domain_data *data)
154 isl_ast_build *build;
155 isl_ast_graft *graft;
156 isl_ast_graft_list *list;
157 isl_set *guard, *pending;
159 build = isl_ast_build_copy(data->build);
160 pending = isl_ast_build_get_pending(build);
161 build = isl_ast_build_replace_pending_by_guard(build, pending);
163 guard = isl_map_domain(isl_map_copy(map));
164 guard = isl_set_compute_divs(guard);
165 guard = isl_set_coalesce_preserve(guard);
166 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
167 guard = isl_ast_build_specialize(build, guard);
169 graft = isl_ast_graft_alloc_domain(map, build);
170 graft = at_each_domain(graft, executed, build);
171 isl_ast_build_free(build);
172 isl_map_free(executed);
173 graft = isl_ast_graft_add_guard(graft, guard, data->build);
175 list = isl_ast_graft_list_from_ast_graft(graft);
176 data->list = isl_ast_graft_list_concat(data->list, list);
178 return isl_stat_ok;
181 /* Generate an AST for a single domain based on
182 * the inverse schedule "executed" and add it to data->list.
184 * If there is more than one domain element associated to the current
185 * schedule "time", then we need to continue the generation process
186 * in generate_non_single_valued.
187 * Note that the inverse schedule being single-valued may depend
188 * on constraints that are only available in the original context
189 * domain specified by the user. We therefore first introduce
190 * some of the constraints of data->build->domain. In particular,
191 * we intersect with a single-disjunct approximation of this set.
192 * We perform this approximation to avoid further splitting up
193 * the executed relation, possibly introducing a disjunctive guard
194 * on the statement.
196 * Otherwise, call add_domain to generate a call expression (with guard) and
197 * to call the at_each_domain callback, if any.
199 * Coalesce the inverse schedule before checking for single-valuedness.
200 * Skip this if the inverse schedule is obviously single-valued.
202 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
204 struct isl_generate_domain_data *data = user;
205 isl_set *domain;
206 int empty, sv;
208 domain = isl_ast_build_get_domain(data->build);
209 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
210 executed = isl_map_intersect_domain(executed, domain);
211 empty = isl_map_is_empty(executed);
212 if (empty < 0)
213 goto error;
214 if (empty) {
215 isl_map_free(executed);
216 return isl_stat_ok;
219 sv = isl_map_plain_is_single_valued(executed);
220 if (sv < 0)
221 goto error;
222 if (sv)
223 return add_domain(executed, isl_map_copy(executed), data);
225 executed = isl_map_coalesce(executed);
226 sv = isl_map_is_single_valued(executed);
227 if (sv < 0)
228 goto error;
229 if (!sv)
230 return generate_non_single_valued(executed, data);
232 return add_domain(executed, isl_map_copy(executed), data);
233 error:
234 isl_map_free(executed);
235 return isl_stat_error;
238 /* Call build->create_leaf to a create "leaf" node in the AST,
239 * encapsulate the result in an isl_ast_graft and return the result
240 * as a 1-element list.
242 * Note that the node returned by the user may be an entire tree.
244 * Since the node itself cannot enforce any constraints, we turn
245 * all pending constraints into guards and add them to the resulting
246 * graft to ensure that they will be generated.
248 * Before we pass control to the user, we first clear some information
249 * from the build that is (presumbably) only meaningful
250 * for the current code generation.
251 * This includes the create_leaf callback itself, so we make a copy
252 * of the build first.
254 static __isl_give isl_ast_graft_list *call_create_leaf(
255 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
257 isl_set *guard;
258 isl_ast_node *node;
259 isl_ast_graft *graft;
260 isl_ast_build *user_build;
262 guard = isl_ast_build_get_pending(build);
263 user_build = isl_ast_build_copy(build);
264 user_build = isl_ast_build_replace_pending_by_guard(user_build,
265 isl_set_copy(guard));
266 user_build = isl_ast_build_set_executed(user_build, executed);
267 user_build = isl_ast_build_clear_local_info(user_build);
268 if (!user_build)
269 node = NULL;
270 else
271 node = build->create_leaf(user_build, build->create_leaf_user);
272 graft = isl_ast_graft_alloc(node, build);
273 graft = isl_ast_graft_add_guard(graft, guard, build);
274 isl_ast_build_free(build);
275 return isl_ast_graft_list_from_ast_graft(graft);
278 static __isl_give isl_ast_graft_list *build_ast_from_child(
279 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
280 __isl_take isl_union_map *executed);
282 /* Generate an AST after having handled the complete schedule
283 * of this call to the code generator or the complete band
284 * if we are generating an AST from a schedule tree.
286 * If we are inside a band node, then move on to the child of the band.
288 * If the user has specified a create_leaf callback, control
289 * is passed to the user in call_create_leaf.
291 * Otherwise, we generate one or more calls for each individual
292 * domain in generate_domain.
294 static __isl_give isl_ast_graft_list *generate_inner_level(
295 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
297 isl_ctx *ctx;
298 struct isl_generate_domain_data data = { build };
300 if (!build || !executed)
301 goto error;
303 if (isl_ast_build_has_schedule_node(build)) {
304 isl_schedule_node *node;
305 node = isl_ast_build_get_schedule_node(build);
306 build = isl_ast_build_reset_schedule_node(build);
307 return build_ast_from_child(build, node, executed);
310 if (build->create_leaf)
311 return call_create_leaf(executed, build);
313 ctx = isl_union_map_get_ctx(executed);
314 data.list = isl_ast_graft_list_alloc(ctx, 0);
315 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
316 data.list = isl_ast_graft_list_free(data.list);
318 if (0)
319 error: data.list = NULL;
320 isl_ast_build_free(build);
321 isl_union_map_free(executed);
322 return data.list;
325 /* Call the before_each_for callback, if requested by the user.
327 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
328 __isl_keep isl_ast_build *build)
330 isl_id *id;
332 if (!node || !build)
333 return isl_ast_node_free(node);
334 if (!build->before_each_for)
335 return node;
336 id = build->before_each_for(build, build->before_each_for_user);
337 node = isl_ast_node_set_annotation(node, id);
338 return node;
341 /* Call the after_each_for callback, if requested by the user.
343 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
344 __isl_keep isl_ast_build *build)
346 if (!graft || !build)
347 return isl_ast_graft_free(graft);
348 if (!build->after_each_for)
349 return graft;
350 graft->node = build->after_each_for(graft->node, build,
351 build->after_each_for_user);
352 if (!graft->node)
353 return isl_ast_graft_free(graft);
354 return graft;
357 /* Plug in all the know values of the current and outer dimensions
358 * in the domain of "executed". In principle, we only need to plug
359 * in the known value of the current dimension since the values of
360 * outer dimensions have been plugged in already.
361 * However, it turns out to be easier to just plug in all known values.
363 static __isl_give isl_union_map *plug_in_values(
364 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
366 return isl_ast_build_substitute_values_union_map_domain(build,
367 executed);
370 /* Check if the constraint "c" is a lower bound on dimension "pos",
371 * an upper bound, or independent of dimension "pos".
373 static int constraint_type(isl_constraint *c, int pos)
375 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
376 return 1;
377 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
378 return 2;
379 return 0;
382 /* Compare the types of the constraints "a" and "b",
383 * resulting in constraints that are independent of "depth"
384 * to be sorted before the lower bounds on "depth", which in
385 * turn are sorted before the upper bounds on "depth".
387 static int cmp_constraint(__isl_keep isl_constraint *a,
388 __isl_keep isl_constraint *b, void *user)
390 int *depth = user;
391 int t1 = constraint_type(a, *depth);
392 int t2 = constraint_type(b, *depth);
394 return t1 - t2;
397 /* Extract a lower bound on dimension "pos" from constraint "c".
399 * If the constraint is of the form
401 * a x + f(...) >= 0
403 * then we essentially return
405 * l = ceil(-f(...)/a)
407 * However, if the current dimension is strided, then we need to make
408 * sure that the lower bound we construct is of the form
410 * f + s a
412 * with f the offset and s the stride.
413 * We therefore compute
415 * f + s * ceil((l - f)/s)
417 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
418 int pos, __isl_keep isl_ast_build *build)
420 isl_aff *aff;
422 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
423 aff = isl_aff_ceil(aff);
425 if (isl_ast_build_has_stride(build, pos)) {
426 isl_aff *offset;
427 isl_val *stride;
429 offset = isl_ast_build_get_offset(build, pos);
430 stride = isl_ast_build_get_stride(build, pos);
432 aff = isl_aff_sub(aff, isl_aff_copy(offset));
433 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
434 aff = isl_aff_ceil(aff);
435 aff = isl_aff_scale_val(aff, stride);
436 aff = isl_aff_add(aff, offset);
439 aff = isl_ast_build_compute_gist_aff(build, aff);
441 return aff;
444 /* Return the exact lower bound (or upper bound if "upper" is set)
445 * of "domain" as a piecewise affine expression.
447 * If we are computing a lower bound (of a strided dimension), then
448 * we need to make sure it is of the form
450 * f + s a
452 * where f is the offset and s is the stride.
453 * We therefore need to include the stride constraint before computing
454 * the minimum.
456 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
457 __isl_keep isl_ast_build *build, int upper)
459 isl_set *stride;
460 isl_map *it_map;
461 isl_pw_aff *pa;
462 isl_pw_multi_aff *pma;
464 domain = isl_set_copy(domain);
465 if (!upper) {
466 stride = isl_ast_build_get_stride_constraint(build);
467 domain = isl_set_intersect(domain, stride);
469 it_map = isl_ast_build_map_to_iterator(build, domain);
470 if (upper)
471 pma = isl_map_lexmax_pw_multi_aff(it_map);
472 else
473 pma = isl_map_lexmin_pw_multi_aff(it_map);
474 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
475 isl_pw_multi_aff_free(pma);
476 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
477 pa = isl_pw_aff_coalesce(pa);
479 return pa;
482 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
483 * remove_redundant_lower_bounds.
485 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
486 void *user)
488 return isl_pw_aff_plain_cmp(a, b);
491 /* Given a list of lower bounds "list", remove those that are redundant
492 * with respect to the other bounds in "list" and the domain of "build".
494 * We first sort the bounds in the same way as they would be sorted
495 * by set_for_node_expressions so that we can try and remove the last
496 * bounds first.
498 * For a lower bound to be effective, there needs to be at least
499 * one domain element for which it is larger than all other lower bounds.
500 * For each lower bound we therefore intersect the domain with
501 * the conditions that it is larger than all other bounds and
502 * check whether the result is empty. If so, the bound can be removed.
504 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
505 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
507 int i, j;
508 isl_size n;
509 isl_set *domain;
511 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
513 n = isl_pw_aff_list_n_pw_aff(list);
514 if (n < 0)
515 return isl_pw_aff_list_free(list);
516 if (n <= 1)
517 return list;
519 domain = isl_ast_build_get_domain(build);
521 for (i = n - 1; i >= 0; --i) {
522 isl_pw_aff *pa_i;
523 isl_set *domain_i;
524 int empty;
526 domain_i = isl_set_copy(domain);
527 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
529 for (j = 0; j < n; ++j) {
530 isl_pw_aff *pa_j;
531 isl_set *better;
533 if (j == i)
534 continue;
536 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
537 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
538 domain_i = isl_set_intersect(domain_i, better);
541 empty = isl_set_is_empty(domain_i);
543 isl_set_free(domain_i);
544 isl_pw_aff_free(pa_i);
546 if (empty < 0)
547 goto error;
548 if (!empty)
549 continue;
550 list = isl_pw_aff_list_drop(list, i, 1);
551 n--;
554 isl_set_free(domain);
556 return list;
557 error:
558 isl_set_free(domain);
559 return isl_pw_aff_list_free(list);
562 /* Extract a lower bound on dimension "pos" from each constraint
563 * in "constraints" and return the list of lower bounds.
564 * If "constraints" has zero elements, then we extract a lower bound
565 * from "domain" instead.
567 * If the current dimension is strided, then the lower bound
568 * is adjusted by lower_bound to match the stride information.
569 * This modification may make one or more lower bounds redundant
570 * with respect to the other lower bounds. We therefore check
571 * for this condition and remove the redundant lower bounds.
573 static __isl_give isl_pw_aff_list *lower_bounds(
574 __isl_keep isl_constraint_list *constraints, int pos,
575 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
577 isl_ctx *ctx;
578 isl_pw_aff_list *list;
579 int i;
580 isl_size n;
582 if (!build)
583 return NULL;
585 n = isl_constraint_list_n_constraint(constraints);
586 if (n < 0)
587 return NULL;
588 if (n == 0) {
589 isl_pw_aff *pa;
590 pa = exact_bound(domain, build, 0);
591 return isl_pw_aff_list_from_pw_aff(pa);
594 ctx = isl_ast_build_get_ctx(build);
595 list = isl_pw_aff_list_alloc(ctx,n);
597 for (i = 0; i < n; ++i) {
598 isl_aff *aff;
599 isl_constraint *c;
601 c = isl_constraint_list_get_constraint(constraints, i);
602 aff = lower_bound(c, pos, build);
603 isl_constraint_free(c);
604 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
607 if (isl_ast_build_has_stride(build, pos))
608 list = remove_redundant_lower_bounds(list, build);
610 return list;
613 /* Extract an upper bound on dimension "pos" from each constraint
614 * in "constraints" and return the list of upper bounds.
615 * If "constraints" has zero elements, then we extract an upper bound
616 * from "domain" instead.
618 static __isl_give isl_pw_aff_list *upper_bounds(
619 __isl_keep isl_constraint_list *constraints, int pos,
620 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
622 isl_ctx *ctx;
623 isl_pw_aff_list *list;
624 int i;
625 isl_size n;
627 n = isl_constraint_list_n_constraint(constraints);
628 if (n < 0)
629 return NULL;
630 if (n == 0) {
631 isl_pw_aff *pa;
632 pa = exact_bound(domain, build, 1);
633 return isl_pw_aff_list_from_pw_aff(pa);
636 ctx = isl_ast_build_get_ctx(build);
637 list = isl_pw_aff_list_alloc(ctx,n);
639 for (i = 0; i < n; ++i) {
640 isl_aff *aff;
641 isl_constraint *c;
643 c = isl_constraint_list_get_constraint(constraints, i);
644 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
645 isl_constraint_free(c);
646 aff = isl_aff_floor(aff);
647 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
650 return list;
653 /* Return an isl_ast_expr that performs the reduction of type "type"
654 * on AST expressions corresponding to the elements in "list".
656 * The list is assumed to contain at least one element.
657 * If the list contains exactly one element, then the returned isl_ast_expr
658 * simply computes that affine expression.
659 * If the list contains more than one element, then we sort it
660 * using a fairly arbitrary but hopefully reasonably stable order.
662 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_expr_op_type type,
663 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
665 int i;
666 isl_size n;
667 isl_ctx *ctx;
668 isl_ast_expr *expr;
670 n = isl_pw_aff_list_n_pw_aff(list);
671 if (n < 0)
672 return NULL;
674 if (n == 1)
675 return isl_ast_build_expr_from_pw_aff_internal(build,
676 isl_pw_aff_list_get_pw_aff(list, 0));
678 ctx = isl_pw_aff_list_get_ctx(list);
679 expr = isl_ast_expr_alloc_op(ctx, type, n);
681 list = isl_pw_aff_list_copy(list);
682 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
683 if (!list)
684 return isl_ast_expr_free(expr);
686 for (i = 0; i < n; ++i) {
687 isl_ast_expr *expr_i;
689 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
690 isl_pw_aff_list_get_pw_aff(list, i));
691 expr = isl_ast_expr_op_add_arg(expr, expr_i);
694 isl_pw_aff_list_free(list);
695 return expr;
698 /* Add guards implied by the "generated constraints",
699 * but not (necessarily) enforced by the generated AST to "guard".
700 * In particular, if there is any stride constraints,
701 * then add the guard implied by those constraints.
702 * If we have generated a degenerate loop, then add the guard
703 * implied by "bounds" on the outer dimensions, i.e., the guard
704 * that ensures that the single value actually exists.
705 * Since there may also be guards implied by a combination
706 * of these constraints, we first combine them before
707 * deriving the implied constraints.
709 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
710 int degenerate, __isl_keep isl_basic_set *bounds,
711 __isl_keep isl_ast_build *build)
713 isl_size depth;
714 isl_bool has_stride;
715 isl_space *space;
716 isl_set *dom, *set;
718 depth = isl_ast_build_get_depth(build);
719 has_stride = isl_ast_build_has_stride(build, depth);
720 if (depth < 0 || has_stride < 0)
721 return isl_set_free(guard);
722 if (!has_stride && !degenerate)
723 return guard;
725 space = isl_basic_set_get_space(bounds);
726 dom = isl_set_universe(space);
728 if (degenerate) {
729 bounds = isl_basic_set_copy(bounds);
730 bounds = isl_basic_set_drop_constraints_not_involving_dims(
731 bounds, isl_dim_set, depth, 1);
732 set = isl_set_from_basic_set(bounds);
733 dom = isl_set_intersect(dom, set);
736 if (has_stride) {
737 set = isl_ast_build_get_stride_constraint(build);
738 dom = isl_set_intersect(dom, set);
741 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
742 dom = isl_ast_build_compute_gist(build, dom);
743 guard = isl_set_intersect(guard, dom);
745 return guard;
748 /* Update "graft" based on "sub_build" for the degenerate case.
750 * "build" is the build in which graft->node was created
751 * "sub_build" contains information about the current level itself,
752 * including the single value attained.
754 * We set the initialization part of the for loop to the single
755 * value attained by the current dimension.
756 * The increment and condition are not strictly needed as they are known
757 * to be "1" and "iterator <= value" respectively.
759 static __isl_give isl_ast_graft *refine_degenerate(
760 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
761 __isl_keep isl_ast_build *sub_build)
763 isl_pw_aff *value;
764 isl_ast_expr *init;
766 if (!graft || !sub_build)
767 return isl_ast_graft_free(graft);
769 value = isl_pw_aff_copy(sub_build->value);
771 init = isl_ast_build_expr_from_pw_aff_internal(build, value);
772 graft->node = isl_ast_node_for_set_init(graft->node, init);
773 if (!graft->node)
774 return isl_ast_graft_free(graft);
776 return graft;
779 /* Return the intersection of constraints in "list" as a set.
781 static __isl_give isl_set *intersect_constraints(
782 __isl_keep isl_constraint_list *list)
784 int i;
785 isl_size n;
786 isl_basic_set *bset;
788 n = isl_constraint_list_n_constraint(list);
789 if (n < 0)
790 return NULL;
791 if (n < 1)
792 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
793 "expecting at least one constraint", return NULL);
795 bset = isl_basic_set_from_constraint(
796 isl_constraint_list_get_constraint(list, 0));
797 for (i = 1; i < n; ++i) {
798 isl_basic_set *bset_i;
800 bset_i = isl_basic_set_from_constraint(
801 isl_constraint_list_get_constraint(list, i));
802 bset = isl_basic_set_intersect(bset, bset_i);
805 return isl_set_from_basic_set(bset);
808 /* Compute the constraints on the outer dimensions enforced by
809 * graft->node and add those constraints to graft->enforced,
810 * in case the upper bound is expressed as a set "upper".
812 * In particular, if l(...) is a lower bound in "lower", and
814 * -a i + f(...) >= 0 or a i <= f(...)
816 * is an upper bound ocnstraint on the current dimension i,
817 * then the for loop enforces the constraint
819 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
821 * We therefore simply take each lower bound in turn, plug it into
822 * the upper bounds and compute the intersection over all lower bounds.
824 * If a lower bound is a rational expression, then
825 * isl_basic_set_preimage_multi_aff will force this rational
826 * expression to have only integer values. However, the loop
827 * itself does not enforce this integrality constraint. We therefore
828 * use the ceil of the lower bounds instead of the lower bounds themselves.
829 * Other constraints will make sure that the for loop is only executed
830 * when each of the lower bounds attains an integral value.
831 * In particular, potentially rational values only occur in
832 * lower_bound if the offset is a (seemingly) rational expression,
833 * but then outer conditions will make sure that this rational expression
834 * only attains integer values.
836 static __isl_give isl_ast_graft *set_enforced_from_set(
837 __isl_take isl_ast_graft *graft,
838 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
840 isl_space *space;
841 isl_basic_set *enforced;
842 isl_pw_multi_aff *pma;
843 int i;
844 isl_size n;
846 n = isl_pw_aff_list_n_pw_aff(lower);
847 if (!graft || n < 0)
848 return isl_ast_graft_free(graft);
850 space = isl_set_get_space(upper);
851 enforced = isl_basic_set_universe(isl_space_copy(space));
853 space = isl_space_map_from_set(space);
854 pma = isl_pw_multi_aff_identity(space);
856 for (i = 0; i < n; ++i) {
857 isl_pw_aff *pa;
858 isl_set *enforced_i;
859 isl_basic_set *hull;
860 isl_pw_multi_aff *pma_i;
862 pa = isl_pw_aff_list_get_pw_aff(lower, i);
863 pa = isl_pw_aff_ceil(pa);
864 pma_i = isl_pw_multi_aff_copy(pma);
865 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
866 enforced_i = isl_set_copy(upper);
867 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
868 hull = isl_set_simple_hull(enforced_i);
869 enforced = isl_basic_set_intersect(enforced, hull);
872 isl_pw_multi_aff_free(pma);
874 graft = isl_ast_graft_enforce(graft, enforced);
876 return graft;
879 /* Compute the constraints on the outer dimensions enforced by
880 * graft->node and add those constraints to graft->enforced,
881 * in case the upper bound is expressed as
882 * a list of affine expressions "upper".
884 * The enforced condition is that each lower bound expression is less
885 * than or equal to each upper bound expression.
887 static __isl_give isl_ast_graft *set_enforced_from_list(
888 __isl_take isl_ast_graft *graft,
889 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
891 isl_set *cond;
892 isl_basic_set *enforced;
894 lower = isl_pw_aff_list_copy(lower);
895 upper = isl_pw_aff_list_copy(upper);
896 cond = isl_pw_aff_list_le_set(lower, upper);
897 enforced = isl_set_simple_hull(cond);
898 graft = isl_ast_graft_enforce(graft, enforced);
900 return graft;
903 /* Does "aff" have a negative constant term?
905 static isl_bool aff_constant_is_negative(__isl_keep isl_set *set,
906 __isl_keep isl_aff *aff, void *user)
908 isl_bool is_neg;
909 isl_val *v;
911 v = isl_aff_get_constant_val(aff);
912 is_neg = isl_val_is_neg(v);
913 isl_val_free(v);
915 return is_neg;
918 /* Does "pa" have a negative constant term over its entire domain?
920 static isl_bool pw_aff_constant_is_negative(__isl_keep isl_pw_aff *pa,
921 void *user)
923 return isl_pw_aff_every_piece(pa, &aff_constant_is_negative, NULL);
926 /* Does each element in "list" have a negative constant term?
928 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
930 return isl_pw_aff_list_every(list, &pw_aff_constant_is_negative, NULL);
933 /* Add 1 to each of the elements in "list", where each of these elements
934 * is defined over the internal schedule space of "build".
936 static __isl_give isl_pw_aff_list *list_add_one(
937 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
939 int i;
940 isl_size n;
941 isl_space *space;
942 isl_aff *aff;
943 isl_pw_aff *one;
945 n = isl_pw_aff_list_n_pw_aff(list);
946 if (n < 0)
947 return isl_pw_aff_list_free(list);
949 space = isl_ast_build_get_space(build, 1);
950 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
951 aff = isl_aff_add_constant_si(aff, 1);
952 one = isl_pw_aff_from_aff(aff);
954 for (i = 0; i < n; ++i) {
955 isl_pw_aff *pa;
956 pa = isl_pw_aff_list_get_pw_aff(list, i);
957 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
958 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
961 isl_pw_aff_free(one);
963 return list;
966 /* Set the condition part of the for node graft->node in case
967 * the upper bound is represented as a list of piecewise affine expressions.
969 * In particular, set the condition to
971 * iterator <= min(list of upper bounds)
973 * If each of the upper bounds has a negative constant term, then
974 * set the condition to
976 * iterator < min(list of (upper bound + 1)s)
979 static __isl_give isl_ast_graft *set_for_cond_from_list(
980 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
981 __isl_keep isl_ast_build *build)
983 int neg;
984 isl_ast_expr *bound, *iterator, *cond;
985 enum isl_ast_expr_op_type type = isl_ast_expr_op_le;
987 if (!graft || !list)
988 return isl_ast_graft_free(graft);
990 neg = list_constant_is_negative(list);
991 if (neg < 0)
992 return isl_ast_graft_free(graft);
993 list = isl_pw_aff_list_copy(list);
994 if (neg) {
995 list = list_add_one(list, build);
996 type = isl_ast_expr_op_lt;
999 bound = reduce_list(isl_ast_expr_op_min, list, build);
1000 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1001 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1002 graft->node = isl_ast_node_for_set_cond(graft->node, cond);
1004 isl_pw_aff_list_free(list);
1005 if (!graft->node)
1006 return isl_ast_graft_free(graft);
1007 return graft;
1010 /* Set the condition part of the for node graft->node in case
1011 * the upper bound is represented as a set.
1013 static __isl_give isl_ast_graft *set_for_cond_from_set(
1014 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1015 __isl_keep isl_ast_build *build)
1017 isl_ast_expr *cond;
1019 if (!graft)
1020 return NULL;
1022 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1023 graft->node = isl_ast_node_for_set_cond(graft->node, cond);
1024 if (!graft->node)
1025 return isl_ast_graft_free(graft);
1026 return graft;
1029 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1030 * the current dimension.
1032 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1034 isl_size depth;
1035 isl_val *v;
1036 isl_ctx *ctx;
1038 depth = isl_ast_build_get_depth(build);
1039 if (depth < 0)
1040 return NULL;
1041 ctx = isl_ast_build_get_ctx(build);
1043 if (!isl_ast_build_has_stride(build, depth))
1044 return isl_ast_expr_alloc_int_si(ctx, 1);
1046 v = isl_ast_build_get_stride(build, depth);
1047 return isl_ast_expr_from_val(v);
1050 /* Should we express the loop condition as
1052 * iterator <= min(list of upper bounds)
1054 * or as a conjunction of constraints?
1056 * The first is constructed from a list of upper bounds.
1057 * The second is constructed from a set.
1059 * If there are no upper bounds in "constraints", then this could mean
1060 * that "domain" simply doesn't have an upper bound or that we didn't
1061 * pick any upper bound. In the first case, we want to generate the
1062 * loop condition as a(n empty) conjunction of constraints
1063 * In the second case, we will compute
1064 * a single upper bound from "domain" and so we use the list form.
1066 * If there are upper bounds in "constraints",
1067 * then we use the list form iff the atomic_upper_bound option is set.
1069 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1070 __isl_keep isl_set *domain, int depth)
1072 if (n_upper > 0)
1073 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1074 else
1075 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1078 /* Fill in the expressions of the for node in graft->node.
1080 * In particular,
1081 * - set the initialization part of the loop to the maximum of the lower bounds
1082 * - extract the increment from the stride of the current dimension
1083 * - construct the for condition either based on a list of upper bounds
1084 * or on a set of upper bound constraints.
1086 static __isl_give isl_ast_graft *set_for_node_expressions(
1087 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1088 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1089 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1091 isl_ast_expr *init;
1093 if (!graft)
1094 return NULL;
1096 init = reduce_list(isl_ast_expr_op_max, lower, build);
1097 graft->node = isl_ast_node_for_set_init(graft->node, init);
1098 graft->node = isl_ast_node_for_set_inc(graft->node, for_inc(build));
1100 if (!graft->node)
1101 graft = isl_ast_graft_free(graft);
1103 if (use_list)
1104 graft = set_for_cond_from_list(graft, upper_list, build);
1105 else
1106 graft = set_for_cond_from_set(graft, upper_set, build);
1108 return graft;
1111 /* Update "graft" based on "bounds" and "domain" for the generic,
1112 * non-degenerate, case.
1114 * "c_lower" and "c_upper" contain the lower and upper bounds
1115 * that the loop node should express.
1116 * "domain" is the subset of the intersection of the constraints
1117 * for which some code is executed.
1119 * There may be zero lower bounds or zero upper bounds in "constraints"
1120 * in case the list of constraints was created
1121 * based on the atomic option or based on separation with explicit bounds.
1122 * In that case, we use "domain" to derive lower and/or upper bounds.
1124 * We first compute a list of one or more lower bounds.
1126 * Then we decide if we want to express the condition as
1128 * iterator <= min(list of upper bounds)
1130 * or as a conjunction of constraints.
1132 * The set of enforced constraints is then computed either based on
1133 * a list of upper bounds or on a set of upper bound constraints.
1134 * We do not compute any enforced constraints if we were forced
1135 * to compute a lower or upper bound using exact_bound. The domains
1136 * of the resulting expressions may imply some bounds on outer dimensions
1137 * that we do not want to appear in the enforced constraints since
1138 * they are not actually enforced by the corresponding code.
1140 * Finally, we fill in the expressions of the for node.
1142 static __isl_give isl_ast_graft *refine_generic_bounds(
1143 __isl_take isl_ast_graft *graft,
1144 __isl_take isl_constraint_list *c_lower,
1145 __isl_take isl_constraint_list *c_upper,
1146 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1148 isl_size depth;
1149 isl_ctx *ctx;
1150 isl_pw_aff_list *lower;
1151 int use_list;
1152 isl_set *upper_set = NULL;
1153 isl_pw_aff_list *upper_list = NULL;
1154 isl_size n_lower, n_upper;
1156 depth = isl_ast_build_get_depth(build);
1157 if (!graft || !c_lower || !c_upper || depth < 0)
1158 goto error;
1160 ctx = isl_ast_graft_get_ctx(graft);
1162 n_lower = isl_constraint_list_n_constraint(c_lower);
1163 n_upper = isl_constraint_list_n_constraint(c_upper);
1164 if (n_lower < 0 || n_upper < 0)
1165 goto error;
1167 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1169 lower = lower_bounds(c_lower, depth, domain, build);
1171 if (use_list)
1172 upper_list = upper_bounds(c_upper, depth, domain, build);
1173 else if (n_upper > 0)
1174 upper_set = intersect_constraints(c_upper);
1175 else
1176 upper_set = isl_set_universe(isl_set_get_space(domain));
1178 if (n_lower == 0 || n_upper == 0)
1180 else if (use_list)
1181 graft = set_enforced_from_list(graft, lower, upper_list);
1182 else
1183 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1185 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1186 upper_set, build);
1188 isl_pw_aff_list_free(lower);
1189 isl_pw_aff_list_free(upper_list);
1190 isl_set_free(upper_set);
1191 isl_constraint_list_free(c_lower);
1192 isl_constraint_list_free(c_upper);
1194 return graft;
1195 error:
1196 isl_constraint_list_free(c_lower);
1197 isl_constraint_list_free(c_upper);
1198 return isl_ast_graft_free(graft);
1201 /* Internal data structure used inside count_constraints to keep
1202 * track of the number of constraints that are independent of dimension "pos",
1203 * the lower bounds in "pos" and the upper bounds in "pos".
1205 struct isl_ast_count_constraints_data {
1206 int pos;
1208 int n_indep;
1209 int n_lower;
1210 int n_upper;
1213 /* Increment data->n_indep, data->lower or data->upper depending
1214 * on whether "c" is independent of dimensions data->pos,
1215 * a lower bound or an upper bound.
1217 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1219 struct isl_ast_count_constraints_data *data = user;
1221 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1222 data->n_lower++;
1223 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1224 data->n_upper++;
1225 else
1226 data->n_indep++;
1228 isl_constraint_free(c);
1230 return isl_stat_ok;
1233 /* Update "graft" based on "bounds" and "domain" for the generic,
1234 * non-degenerate, case.
1236 * "list" respresent the list of bounds that need to be encoded by
1237 * the for loop. Only the constraints that involve the iterator
1238 * are relevant here. The other constraints are taken care of by
1239 * the caller and are included in the generated constraints of "build".
1240 * "domain" is the subset of the intersection of the constraints
1241 * for which some code is executed.
1242 * "build" is the build in which graft->node was created.
1244 * We separate lower bounds, upper bounds and constraints that
1245 * are independent of the loop iterator.
1247 * The actual for loop bounds are generated in refine_generic_bounds.
1249 static __isl_give isl_ast_graft *refine_generic_split(
1250 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1251 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1253 struct isl_ast_count_constraints_data data;
1254 isl_size depth;
1255 isl_constraint_list *lower;
1256 isl_constraint_list *upper;
1258 depth = isl_ast_build_get_depth(build);
1259 if (depth < 0)
1260 list = isl_constraint_list_free(list);
1261 if (!list)
1262 return isl_ast_graft_free(graft);
1264 data.pos = depth;
1266 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1267 if (!list)
1268 return isl_ast_graft_free(graft);
1270 data.n_indep = data.n_lower = data.n_upper = 0;
1271 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1272 isl_constraint_list_free(list);
1273 return isl_ast_graft_free(graft);
1276 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1277 upper = isl_constraint_list_copy(lower);
1278 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1279 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1281 return refine_generic_bounds(graft, lower, upper, domain, build);
1284 /* Update "graft" based on "bounds" and "domain" for the generic,
1285 * non-degenerate, case.
1287 * "bounds" respresent the bounds that need to be encoded by
1288 * the for loop (or a guard around the for loop).
1289 * "domain" is the subset of "bounds" for which some code is executed.
1290 * "build" is the build in which graft->node was created.
1292 * We break up "bounds" into a list of constraints and continue with
1293 * refine_generic_split.
1295 static __isl_give isl_ast_graft *refine_generic(
1296 __isl_take isl_ast_graft *graft,
1297 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1298 __isl_keep isl_ast_build *build)
1300 isl_constraint_list *list;
1302 if (!build || !graft)
1303 return isl_ast_graft_free(graft);
1305 list = isl_basic_set_get_constraint_list(bounds);
1307 graft = refine_generic_split(graft, list, domain, build);
1309 return graft;
1312 /* Create a for node for the current level.
1314 * Mark the for node degenerate if "degenerate" is set.
1316 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1317 int degenerate)
1319 isl_size depth;
1320 isl_id *id;
1321 isl_ast_node *node;
1323 depth = isl_ast_build_get_depth(build);
1324 if (depth < 0)
1325 return NULL;
1327 id = isl_ast_build_get_iterator_id(build, depth);
1328 node = isl_ast_node_alloc_for(id);
1329 if (degenerate)
1330 node = isl_ast_node_for_mark_degenerate(node);
1332 return node;
1335 /* If the ast_build_exploit_nested_bounds option is set, then return
1336 * the constraints enforced by all elements in "list".
1337 * Otherwise, return the universe.
1339 static __isl_give isl_basic_set *extract_shared_enforced(
1340 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1342 isl_ctx *ctx;
1343 isl_space *space;
1345 if (!list)
1346 return NULL;
1348 ctx = isl_ast_graft_list_get_ctx(list);
1349 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1350 return isl_ast_graft_list_extract_shared_enforced(list, build);
1352 space = isl_ast_build_get_space(build, 1);
1353 return isl_basic_set_universe(space);
1356 /* Return the pending constraints of "build" that are not already taken
1357 * care of (by a combination of "enforced" and the generated constraints
1358 * of "build").
1360 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1361 __isl_keep isl_basic_set *enforced)
1363 isl_set *guard, *context;
1365 guard = isl_ast_build_get_pending(build);
1366 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1367 context = isl_set_intersect(context,
1368 isl_ast_build_get_generated(build));
1369 return isl_set_gist(guard, context);
1372 /* Create an AST node for the current dimension based on
1373 * the schedule domain "bounds" and return the node encapsulated
1374 * in an isl_ast_graft.
1376 * "executed" is the current inverse schedule, taking into account
1377 * the bounds in "bounds"
1378 * "domain" is the domain of "executed", with inner dimensions projected out.
1379 * It may be a strict subset of "bounds" in case "bounds" was created
1380 * based on the atomic option or based on separation with explicit bounds.
1382 * "domain" may satisfy additional equalities that result
1383 * from intersecting "executed" with "bounds" in add_node.
1384 * It may also satisfy some global constraints that were dropped out because
1385 * we performed separation with explicit bounds.
1386 * The very first step is then to copy these constraints to "bounds".
1388 * Since we may be calling before_each_for and after_each_for
1389 * callbacks, we record the current inverse schedule in the build.
1391 * We consider three builds,
1392 * "build" is the one in which the current level is created,
1393 * "body_build" is the build in which the next level is created,
1394 * "sub_build" is essentially the same as "body_build", except that
1395 * the depth has not been increased yet.
1397 * "build" already contains information (in strides and offsets)
1398 * about the strides at the current level, but this information is not
1399 * reflected in the build->domain.
1400 * We first add this information and the "bounds" to the sub_build->domain.
1401 * isl_ast_build_set_loop_bounds adds the stride information and
1402 * checks whether the current dimension attains
1403 * only a single value and whether this single value can be represented using
1404 * a single affine expression.
1405 * In the first case, the current level is considered "degenerate".
1406 * In the second, sub-case, the current level is considered "eliminated".
1407 * Eliminated levels don't need to be reflected in the AST since we can
1408 * simply plug in the affine expression. For degenerate, but non-eliminated,
1409 * levels, we do introduce a for node, but mark is as degenerate so that
1410 * it can be printed as an assignment of the single value to the loop
1411 * "iterator".
1413 * If the current level is eliminated, we explicitly plug in the value
1414 * for the current level found by isl_ast_build_set_loop_bounds in the
1415 * inverse schedule. This ensures that if we are working on a slice
1416 * of the domain based on information available in the inverse schedule
1417 * and the build domain, that then this information is also reflected
1418 * in the inverse schedule. This operation also eliminates the current
1419 * dimension from the inverse schedule making sure no inner dimensions depend
1420 * on the current dimension. Otherwise, we create a for node, marking
1421 * it degenerate if appropriate. The initial for node is still incomplete
1422 * and will be completed in either refine_degenerate or refine_generic.
1424 * We then generate a sequence of grafts for the next level,
1425 * create a surrounding graft for the current level and insert
1426 * the for node we created (if the current level is not eliminated).
1427 * Before creating a graft for the current level, we first extract
1428 * hoistable constraints from the child guards and combine them
1429 * with the pending constraints in the build. These constraints
1430 * are used to simplify the child guards and then added to the guard
1431 * of the current graft to ensure that they will be generated.
1432 * If the hoisted guard is a disjunction, then we use it directly
1433 * to gist the guards on the children before intersect it with the
1434 * pending constraints. We do so because this disjunction is typically
1435 * identical to the guards on the children such that these guards
1436 * can be effectively removed completely. After the intersection,
1437 * the gist operation would have a harder time figuring this out.
1439 * Finally, we set the bounds of the for loop in either
1440 * refine_degenerate or refine_generic.
1441 * We do so in a context where the pending constraints of the build
1442 * have been replaced by the guard of the current graft.
1444 static __isl_give isl_ast_graft *create_node_scaled(
1445 __isl_take isl_union_map *executed,
1446 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1447 __isl_take isl_ast_build *build)
1449 isl_size depth;
1450 int degenerate;
1451 isl_bool eliminated;
1452 isl_size n;
1453 isl_basic_set *hull;
1454 isl_basic_set *enforced;
1455 isl_set *guard, *hoisted;
1456 isl_ast_node *node = NULL;
1457 isl_ast_graft *graft;
1458 isl_ast_graft_list *children;
1459 isl_ast_build *sub_build;
1460 isl_ast_build *body_build;
1462 domain = isl_ast_build_eliminate_divs(build, domain);
1463 domain = isl_set_detect_equalities(domain);
1464 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1465 bounds = isl_basic_set_intersect(bounds, hull);
1466 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1468 depth = isl_ast_build_get_depth(build);
1469 if (depth < 0)
1470 build = isl_ast_build_free(build);
1471 sub_build = isl_ast_build_copy(build);
1472 bounds = isl_basic_set_remove_redundancies(bounds);
1473 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1474 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1475 isl_basic_set_copy(bounds));
1476 degenerate = isl_ast_build_has_value(sub_build);
1477 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1478 if (degenerate < 0 || eliminated < 0)
1479 executed = isl_union_map_free(executed);
1480 if (!degenerate)
1481 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1482 sub_build = isl_ast_build_set_pending_generated(sub_build,
1483 isl_basic_set_copy(bounds));
1484 if (eliminated)
1485 executed = plug_in_values(executed, sub_build);
1486 else
1487 node = create_for(build, degenerate);
1489 body_build = isl_ast_build_copy(sub_build);
1490 body_build = isl_ast_build_increase_depth(body_build);
1491 if (!eliminated)
1492 node = before_each_for(node, body_build);
1493 children = generate_next_level(executed,
1494 isl_ast_build_copy(body_build));
1496 enforced = extract_shared_enforced(children, build);
1497 guard = extract_pending(sub_build, enforced);
1498 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1499 n = isl_set_n_basic_set(hoisted);
1500 if (n < 0)
1501 children = isl_ast_graft_list_free(children);
1502 if (n > 1)
1503 children = isl_ast_graft_list_gist_guards(children,
1504 isl_set_copy(hoisted));
1505 guard = isl_set_intersect(guard, hoisted);
1506 if (!eliminated)
1507 guard = add_implied_guards(guard, degenerate, bounds, build);
1509 graft = isl_ast_graft_alloc_from_children(children,
1510 isl_set_copy(guard), enforced, build, sub_build);
1512 if (!eliminated) {
1513 isl_ast_build *for_build;
1515 graft = isl_ast_graft_insert_for(graft, node);
1516 for_build = isl_ast_build_copy(build);
1517 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1518 isl_set_copy(guard));
1519 if (degenerate)
1520 graft = refine_degenerate(graft, for_build, sub_build);
1521 else
1522 graft = refine_generic(graft, bounds,
1523 domain, for_build);
1524 isl_ast_build_free(for_build);
1526 isl_set_free(guard);
1527 if (!eliminated)
1528 graft = after_each_for(graft, body_build);
1530 isl_ast_build_free(body_build);
1531 isl_ast_build_free(sub_build);
1532 isl_ast_build_free(build);
1533 isl_basic_set_free(bounds);
1534 isl_set_free(domain);
1536 return graft;
1539 /* Internal data structure for checking if all constraints involving
1540 * the input dimension "depth" are such that the other coefficients
1541 * are multiples of "m", reducing "m" if they are not.
1542 * If "m" is reduced all the way down to "1", then the check has failed
1543 * and we break out of the iteration.
1545 struct isl_check_scaled_data {
1546 int depth;
1547 isl_val *m;
1550 /* If constraint "c" involves the input dimension data->depth,
1551 * then make sure that all the other coefficients are multiples of data->m,
1552 * reducing data->m if needed.
1553 * Break out of the iteration if data->m has become equal to "1".
1555 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1556 void *user)
1558 struct isl_check_scaled_data *data = user;
1559 int i, j;
1560 isl_size n;
1561 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1562 isl_dim_div };
1564 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1565 isl_constraint_free(c);
1566 return isl_stat_ok;
1569 for (i = 0; i < 4; ++i) {
1570 n = isl_constraint_dim(c, t[i]);
1571 if (n < 0)
1572 break;
1573 for (j = 0; j < n; ++j) {
1574 isl_val *d;
1576 if (t[i] == isl_dim_in && j == data->depth)
1577 continue;
1578 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1579 continue;
1580 d = isl_constraint_get_coefficient_val(c, t[i], j);
1581 data->m = isl_val_gcd(data->m, d);
1582 if (isl_val_is_one(data->m))
1583 break;
1585 if (j < n)
1586 break;
1589 isl_constraint_free(c);
1591 return i < 4 ? isl_stat_error : isl_stat_ok;
1594 /* For each constraint of "bmap" that involves the input dimension data->depth,
1595 * make sure that all the other coefficients are multiples of data->m,
1596 * reducing data->m if needed.
1597 * Break out of the iteration if data->m has become equal to "1".
1599 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1600 void *user)
1602 isl_stat r;
1604 r = isl_basic_map_foreach_constraint(bmap,
1605 &constraint_check_scaled, user);
1606 isl_basic_map_free(bmap);
1608 return r;
1611 /* For each constraint of "map" that involves the input dimension data->depth,
1612 * make sure that all the other coefficients are multiples of data->m,
1613 * reducing data->m if needed.
1614 * Break out of the iteration if data->m has become equal to "1".
1616 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1618 isl_stat r;
1620 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1621 isl_map_free(map);
1623 return r;
1626 /* Create an AST node for the current dimension based on
1627 * the schedule domain "bounds" and return the node encapsulated
1628 * in an isl_ast_graft.
1630 * "executed" is the current inverse schedule, taking into account
1631 * the bounds in "bounds"
1632 * "domain" is the domain of "executed", with inner dimensions projected out.
1635 * Before moving on to the actual AST node construction in create_node_scaled,
1636 * we first check if the current dimension is strided and if we can scale
1637 * down this stride. Note that we only do this if the ast_build_scale_strides
1638 * option is set.
1640 * In particular, let the current dimension take on values
1642 * f + s a
1644 * with a an integer. We check if we can find an integer m that (obviously)
1645 * divides both f and s.
1647 * If so, we check if the current dimension only appears in constraints
1648 * where the coefficients of the other variables are multiples of m.
1649 * We perform this extra check to avoid the risk of introducing
1650 * divisions by scaling down the current dimension.
1652 * If so, we scale the current dimension down by a factor of m.
1653 * That is, we plug in
1655 * i = m i' (1)
1657 * Note that in principle we could always scale down strided loops
1658 * by plugging in
1660 * i = f + s i'
1662 * but this may result in i' taking on larger values than the original i,
1663 * due to the shift by "f".
1664 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1666 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1667 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1668 __isl_take isl_ast_build *build)
1670 struct isl_check_scaled_data data;
1671 isl_size depth;
1672 isl_ctx *ctx;
1673 isl_aff *offset;
1674 isl_val *d;
1676 ctx = isl_ast_build_get_ctx(build);
1677 if (!isl_options_get_ast_build_scale_strides(ctx))
1678 return create_node_scaled(executed, bounds, domain, build);
1680 depth = isl_ast_build_get_depth(build);
1681 if (depth < 0)
1682 build = isl_ast_build_free(build);
1683 data.depth = depth;
1684 if (!isl_ast_build_has_stride(build, data.depth))
1685 return create_node_scaled(executed, bounds, domain, build);
1687 offset = isl_ast_build_get_offset(build, data.depth);
1688 data.m = isl_ast_build_get_stride(build, data.depth);
1689 if (!data.m)
1690 offset = isl_aff_free(offset);
1691 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1692 d = isl_aff_get_denominator_val(offset);
1693 if (!d)
1694 executed = isl_union_map_free(executed);
1696 if (executed && isl_val_is_divisible_by(data.m, d))
1697 data.m = isl_val_div(data.m, d);
1698 else {
1699 data.m = isl_val_set_si(data.m, 1);
1700 isl_val_free(d);
1703 if (!isl_val_is_one(data.m)) {
1704 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1705 &data) < 0 &&
1706 !isl_val_is_one(data.m))
1707 executed = isl_union_map_free(executed);
1710 if (!isl_val_is_one(data.m)) {
1711 isl_space *space;
1712 isl_multi_aff *ma;
1713 isl_aff *aff;
1714 isl_map *map;
1715 isl_union_map *umap;
1717 space = isl_ast_build_get_space(build, 1);
1718 space = isl_space_map_from_set(space);
1719 ma = isl_multi_aff_identity(space);
1720 aff = isl_multi_aff_get_aff(ma, data.depth);
1721 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1722 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1724 bounds = isl_basic_set_preimage_multi_aff(bounds,
1725 isl_multi_aff_copy(ma));
1726 domain = isl_set_preimage_multi_aff(domain,
1727 isl_multi_aff_copy(ma));
1728 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1729 umap = isl_union_map_from_map(map);
1730 executed = isl_union_map_apply_domain(executed,
1731 isl_union_map_copy(umap));
1732 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1733 umap);
1735 isl_aff_free(offset);
1736 isl_val_free(data.m);
1738 return create_node_scaled(executed, bounds, domain, build);
1741 /* Add the basic set to the list that "user" points to.
1743 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1745 isl_basic_set_list **list = user;
1747 *list = isl_basic_set_list_add(*list, bset);
1749 return isl_stat_ok;
1752 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1754 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1755 __isl_take isl_set *set)
1757 isl_size n;
1758 isl_ctx *ctx;
1759 isl_basic_set_list *list;
1761 n = isl_set_n_basic_set(set);
1762 if (n < 0)
1763 set = isl_set_free(set);
1764 if (!set)
1765 return NULL;
1767 ctx = isl_set_get_ctx(set);
1769 list = isl_basic_set_list_alloc(ctx, n);
1770 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1771 list = isl_basic_set_list_free(list);
1773 isl_set_free(set);
1774 return list;
1777 /* Generate code for the schedule domain "bounds"
1778 * and add the result to "list".
1780 * We mainly detect strides here and check if the bounds do not
1781 * conflict with the current build domain
1782 * and then pass over control to create_node.
1784 * "bounds" reflects the bounds on the current dimension and possibly
1785 * some extra conditions on outer dimensions.
1786 * It does not, however, include any divs involving the current dimension,
1787 * so it does not capture any stride constraints.
1788 * We therefore need to compute that part of the schedule domain that
1789 * intersects with "bounds" and derive the strides from the result.
1791 static __isl_give isl_ast_graft_list *add_node(
1792 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1793 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1795 isl_ast_graft *graft;
1796 isl_set *domain = NULL;
1797 isl_union_set *uset;
1798 int empty, disjoint;
1800 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1801 executed = isl_union_map_intersect_domain(executed, uset);
1802 empty = isl_union_map_is_empty(executed);
1803 if (empty < 0)
1804 goto error;
1805 if (empty)
1806 goto done;
1808 uset = isl_union_map_domain(isl_union_map_copy(executed));
1809 domain = isl_set_from_union_set(uset);
1810 domain = isl_ast_build_specialize(build, domain);
1812 domain = isl_set_compute_divs(domain);
1813 domain = isl_ast_build_eliminate_inner(build, domain);
1814 disjoint = isl_set_is_disjoint(domain, build->domain);
1815 if (disjoint < 0)
1816 goto error;
1817 if (disjoint)
1818 goto done;
1820 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1822 graft = create_node(executed, bounds, domain,
1823 isl_ast_build_copy(build));
1824 list = isl_ast_graft_list_add(list, graft);
1825 isl_ast_build_free(build);
1826 return list;
1827 error:
1828 list = isl_ast_graft_list_free(list);
1829 done:
1830 isl_set_free(domain);
1831 isl_basic_set_free(bounds);
1832 isl_union_map_free(executed);
1833 isl_ast_build_free(build);
1834 return list;
1837 /* Does any element of i follow or coincide with any element of j
1838 * at the current depth for equal values of the outer dimensions?
1840 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1841 __isl_keep isl_basic_set *j, void *user)
1843 int depth = *(int *) user;
1844 isl_basic_map *test;
1845 isl_bool empty;
1846 int l;
1848 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1849 isl_basic_set_copy(j));
1850 for (l = 0; l < depth; ++l)
1851 test = isl_basic_map_equate(test, isl_dim_in, l,
1852 isl_dim_out, l);
1853 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1854 isl_dim_out, depth);
1855 empty = isl_basic_map_is_empty(test);
1856 isl_basic_map_free(test);
1858 return isl_bool_not(empty);
1861 /* Split up each element of "list" into a part that is related to "bset"
1862 * according to "gt" and a part that is not.
1863 * Return a list that consist of "bset" and all the pieces.
1865 static __isl_give isl_basic_set_list *add_split_on(
1866 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1867 __isl_keep isl_basic_map *gt)
1869 int i;
1870 isl_size n;
1871 isl_basic_set_list *res;
1873 n = isl_basic_set_list_n_basic_set(list);
1874 if (n < 0)
1875 bset = isl_basic_set_free(bset);
1877 gt = isl_basic_map_copy(gt);
1878 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1879 res = isl_basic_set_list_from_basic_set(bset);
1880 for (i = 0; res && i < n; ++i) {
1881 isl_basic_set *bset;
1882 isl_set *set1, *set2;
1883 isl_basic_map *bmap;
1884 int empty;
1886 bset = isl_basic_set_list_get_basic_set(list, i);
1887 bmap = isl_basic_map_copy(gt);
1888 bmap = isl_basic_map_intersect_range(bmap, bset);
1889 bset = isl_basic_map_range(bmap);
1890 empty = isl_basic_set_is_empty(bset);
1891 if (empty < 0)
1892 res = isl_basic_set_list_free(res);
1893 if (empty) {
1894 isl_basic_set_free(bset);
1895 bset = isl_basic_set_list_get_basic_set(list, i);
1896 res = isl_basic_set_list_add(res, bset);
1897 continue;
1900 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1901 set1 = isl_set_from_basic_set(bset);
1902 bset = isl_basic_set_list_get_basic_set(list, i);
1903 set2 = isl_set_from_basic_set(bset);
1904 set1 = isl_set_subtract(set2, set1);
1905 set1 = isl_set_make_disjoint(set1);
1907 res = isl_basic_set_list_concat(res,
1908 isl_basic_set_list_from_set(set1));
1910 isl_basic_map_free(gt);
1911 isl_basic_set_list_free(list);
1912 return res;
1915 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1916 __isl_keep isl_basic_set_list *domain_list,
1917 __isl_keep isl_union_map *executed,
1918 __isl_keep isl_ast_build *build);
1920 /* Internal data structure for add_nodes.
1922 * "executed" and "build" are extra arguments to be passed to add_node.
1923 * "list" collects the results.
1925 struct isl_add_nodes_data {
1926 isl_union_map *executed;
1927 isl_ast_build *build;
1929 isl_ast_graft_list *list;
1932 /* Generate code for the schedule domains in "scc"
1933 * and add the results to "list".
1935 * The domains in "scc" form a strongly connected component in the ordering.
1936 * If the number of domains in "scc" is larger than 1, then this means
1937 * that we cannot determine a valid ordering for the domains in the component.
1938 * This should be fairly rare because the individual domains
1939 * have been made disjoint first.
1940 * The problem is that the domains may be integrally disjoint but not
1941 * rationally disjoint. For example, we may have domains
1943 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1945 * These two domains have an empty intersection, but their rational
1946 * relaxations do intersect. It is impossible to order these domains
1947 * in the second dimension because the first should be ordered before
1948 * the second for outer dimension equal to 0, while it should be ordered
1949 * after for outer dimension equal to 1.
1951 * This may happen in particular in case of unrolling since the domain
1952 * of each slice is replaced by its simple hull.
1954 * For each basic set i in "scc" and for each of the following basic sets j,
1955 * we split off that part of the basic set i that shares the outer dimensions
1956 * with j and lies before j in the current dimension.
1957 * We collect all the pieces in a new list that replaces "scc".
1959 * While the elements in "scc" should be disjoint, we double-check
1960 * this property to avoid running into an infinite recursion in case
1961 * they intersect due to some internal error.
1963 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1965 struct isl_add_nodes_data *data = user;
1966 int i;
1967 isl_size depth;
1968 isl_size n;
1969 isl_basic_set *bset, *first;
1970 isl_basic_set_list *list;
1971 isl_space *space;
1972 isl_basic_map *gt;
1974 n = isl_basic_set_list_n_basic_set(scc);
1975 if (n < 0)
1976 goto error;
1977 bset = isl_basic_set_list_get_basic_set(scc, 0);
1978 if (n == 1) {
1979 isl_basic_set_list_free(scc);
1980 data->list = add_node(data->list,
1981 isl_union_map_copy(data->executed), bset,
1982 isl_ast_build_copy(data->build));
1983 return data->list ? isl_stat_ok : isl_stat_error;
1986 depth = isl_ast_build_get_depth(data->build);
1987 if (depth < 0)
1988 bset = isl_basic_set_free(bset);
1989 space = isl_basic_set_get_space(bset);
1990 space = isl_space_map_from_set(space);
1991 gt = isl_basic_map_universe(space);
1992 for (i = 0; i < depth; ++i)
1993 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1994 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1996 first = isl_basic_set_copy(bset);
1997 list = isl_basic_set_list_from_basic_set(bset);
1998 for (i = 1; i < n; ++i) {
1999 int disjoint;
2001 bset = isl_basic_set_list_get_basic_set(scc, i);
2003 disjoint = isl_basic_set_is_disjoint(bset, first);
2004 if (disjoint < 0)
2005 list = isl_basic_set_list_free(list);
2006 else if (!disjoint)
2007 isl_die(isl_basic_set_list_get_ctx(scc),
2008 isl_error_internal,
2009 "basic sets in scc are assumed to be disjoint",
2010 list = isl_basic_set_list_free(list));
2012 list = add_split_on(list, bset, gt);
2014 isl_basic_set_free(first);
2015 isl_basic_map_free(gt);
2016 isl_basic_set_list_free(scc);
2017 scc = list;
2018 data->list = isl_ast_graft_list_concat(data->list,
2019 generate_sorted_domains(scc, data->executed, data->build));
2020 isl_basic_set_list_free(scc);
2022 return data->list ? isl_stat_ok : isl_stat_error;
2023 error:
2024 isl_basic_set_list_free(scc);
2025 return isl_stat_error;
2028 /* Sort the domains in "domain_list" according to the execution order
2029 * at the current depth (for equal values of the outer dimensions),
2030 * generate code for each of them, collecting the results in a list.
2031 * If no code is generated (because the intersection of the inverse schedule
2032 * with the domains turns out to be empty), then an empty list is returned.
2034 * The caller is responsible for ensuring that the basic sets in "domain_list"
2035 * are pair-wise disjoint. It can, however, in principle happen that
2036 * two basic sets should be ordered one way for one value of the outer
2037 * dimensions and the other way for some other value of the outer dimensions.
2038 * We therefore play safe and look for strongly connected components.
2039 * The function add_nodes takes care of handling non-trivial components.
2041 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2042 __isl_keep isl_basic_set_list *domain_list,
2043 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2045 isl_ctx *ctx;
2046 struct isl_add_nodes_data data;
2047 isl_size depth;
2048 isl_size n;
2050 n = isl_basic_set_list_n_basic_set(domain_list);
2051 if (n < 0)
2052 return NULL;
2054 ctx = isl_basic_set_list_get_ctx(domain_list);
2055 data.list = isl_ast_graft_list_alloc(ctx, n);
2056 if (n == 0)
2057 return data.list;
2058 if (n == 1)
2059 return add_node(data.list, isl_union_map_copy(executed),
2060 isl_basic_set_list_get_basic_set(domain_list, 0),
2061 isl_ast_build_copy(build));
2063 depth = isl_ast_build_get_depth(build);
2064 data.executed = executed;
2065 data.build = build;
2066 if (depth < 0 || isl_basic_set_list_foreach_scc(domain_list,
2067 &domain_follows_at_depth, &depth,
2068 &add_nodes, &data) < 0)
2069 data.list = isl_ast_graft_list_free(data.list);
2071 return data.list;
2074 /* Do i and j share any values for the outer dimensions?
2076 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2077 __isl_keep isl_basic_set *j, void *user)
2079 int depth = *(int *) user;
2080 isl_basic_map *test;
2081 isl_bool empty;
2082 int l;
2084 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2085 isl_basic_set_copy(j));
2086 for (l = 0; l < depth; ++l)
2087 test = isl_basic_map_equate(test, isl_dim_in, l,
2088 isl_dim_out, l);
2089 empty = isl_basic_map_is_empty(test);
2090 isl_basic_map_free(test);
2092 return isl_bool_not(empty);
2095 /* Internal data structure for generate_sorted_domains_wrap.
2097 * "n" is the total number of basic sets
2098 * "executed" and "build" are extra arguments to be passed
2099 * to generate_sorted_domains.
2101 * "single" is set to 1 by generate_sorted_domains_wrap if there
2102 * is only a single component.
2103 * "list" collects the results.
2105 struct isl_ast_generate_parallel_domains_data {
2106 isl_size n;
2107 isl_union_map *executed;
2108 isl_ast_build *build;
2110 int single;
2111 isl_ast_graft_list *list;
2114 /* Call generate_sorted_domains on "scc", fuse the result into a list
2115 * with either zero or one graft and collect the these single element
2116 * lists into data->list.
2118 * If there is only one component, i.e., if the number of basic sets
2119 * in the current component is equal to the total number of basic sets,
2120 * then data->single is set to 1 and the result of generate_sorted_domains
2121 * is not fused.
2123 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2124 void *user)
2126 struct isl_ast_generate_parallel_domains_data *data = user;
2127 isl_ast_graft_list *list;
2128 isl_size n;
2130 n = isl_basic_set_list_n_basic_set(scc);
2131 if (n < 0)
2132 scc = isl_basic_set_list_free(scc);
2133 list = generate_sorted_domains(scc, data->executed, data->build);
2134 data->single = n == 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 isl_size depth;
2170 struct isl_ast_generate_parallel_domains_data data;
2172 data.n = isl_basic_set_list_n_basic_set(domain_list);
2173 if (data.n < 0)
2174 return NULL;
2176 if (data.n <= 1)
2177 return generate_sorted_domains(domain_list, executed, build);
2179 depth = isl_ast_build_get_depth(build);
2180 if (depth < 0)
2181 return NULL;
2182 data.list = NULL;
2183 data.executed = executed;
2184 data.build = build;
2185 data.single = 0;
2186 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2187 &generate_sorted_domains_wrap,
2188 &data) < 0)
2189 data.list = isl_ast_graft_list_free(data.list);
2191 if (!data.single)
2192 data.list = isl_ast_graft_list_sort_guard(data.list);
2194 return data.list;
2197 /* Internal data for separate_domain.
2199 * "explicit" is set if we only want to use explicit bounds.
2201 * "domain" collects the separated domains.
2203 struct isl_separate_domain_data {
2204 isl_ast_build *build;
2205 int explicit;
2206 isl_set *domain;
2209 /* Extract implicit bounds on the current dimension for the executed "map".
2211 * The domain of "map" may involve inner dimensions, so we
2212 * need to eliminate them.
2214 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2215 __isl_keep isl_ast_build *build)
2217 isl_set *domain;
2219 domain = isl_map_domain(map);
2220 domain = isl_ast_build_eliminate(build, domain);
2222 return domain;
2225 /* Extract explicit bounds on the current dimension for the executed "map".
2227 * Rather than eliminating the inner dimensions as in implicit_bounds,
2228 * we simply drop any constraints involving those inner dimensions.
2229 * The idea is that most bounds that are implied by constraints on the
2230 * inner dimensions will be enforced by for loops and not by explicit guards.
2231 * There is then no need to separate along those bounds.
2233 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2234 __isl_keep isl_ast_build *build)
2236 isl_set *domain;
2237 isl_size depth;
2238 isl_size dim;
2240 depth = isl_ast_build_get_depth(build);
2241 dim = isl_map_dim(map, isl_dim_out);
2242 if (depth < 0 || dim < 0)
2243 return isl_map_domain(isl_map_free(map));
2244 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2246 domain = isl_map_domain(map);
2247 dim = isl_set_dim(domain, isl_dim_set);
2248 domain = isl_set_detect_equalities(domain);
2249 domain = isl_set_drop_constraints_involving_dims(domain,
2250 isl_dim_set, depth + 1, dim - (depth + 1));
2251 domain = isl_set_remove_divs_involving_dims(domain,
2252 isl_dim_set, depth, 1);
2253 domain = isl_set_remove_unknown_divs(domain);
2255 return domain;
2258 /* Split data->domain into pieces that intersect with the range of "map"
2259 * and pieces that do not intersect with the range of "map"
2260 * and then add that part of the range of "map" that does not intersect
2261 * with data->domain.
2263 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2265 struct isl_separate_domain_data *data = user;
2266 isl_set *domain;
2267 isl_set *d1, *d2;
2269 if (data->explicit)
2270 domain = explicit_bounds(map, data->build);
2271 else
2272 domain = implicit_bounds(map, data->build);
2274 domain = isl_set_coalesce(domain);
2275 domain = isl_set_make_disjoint(domain);
2276 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2277 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2278 data->domain = isl_set_intersect(data->domain, domain);
2279 data->domain = isl_set_union(data->domain, d1);
2280 data->domain = isl_set_union(data->domain, d2);
2282 return isl_stat_ok;
2285 /* Separate the schedule domains of "executed".
2287 * That is, break up the domain of "executed" into basic sets,
2288 * such that for each basic set S, every element in S is associated with
2289 * the same domain spaces.
2291 * "space" is the (single) domain space of "executed".
2293 static __isl_give isl_set *separate_schedule_domains(
2294 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2295 __isl_keep isl_ast_build *build)
2297 struct isl_separate_domain_data data = { build };
2298 isl_ctx *ctx;
2300 ctx = isl_ast_build_get_ctx(build);
2301 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2302 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2303 data.domain = isl_set_empty(space);
2304 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2305 data.domain = isl_set_free(data.domain);
2307 isl_union_map_free(executed);
2308 return data.domain;
2311 /* Temporary data used during the search for a lower bound for unrolling.
2313 * "build" is the build in which the unrolling will be performed
2314 * "domain" is the original set for which to find a lower bound
2315 * "depth" is the dimension for which to find a lower boudn
2316 * "expansion" is the expansion that needs to be applied to "domain"
2317 * in the unrolling that will be performed
2319 * "lower" is the best lower bound found so far. It is NULL if we have not
2320 * found any yet.
2321 * "n" is the corresponding size. If lower is NULL, then the value of n
2322 * is undefined.
2323 * "n_div" is the maximal number of integer divisions in the first
2324 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2325 * been computed yet.
2327 struct isl_find_unroll_data {
2328 isl_ast_build *build;
2329 isl_set *domain;
2330 int depth;
2331 isl_basic_map *expansion;
2333 isl_aff *lower;
2334 int *n;
2335 int n_div;
2338 /* Return the constraint
2340 * i_"depth" = aff + offset
2342 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2343 int offset)
2345 aff = isl_aff_copy(aff);
2346 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2347 aff = isl_aff_add_constant_si(aff, offset);
2348 return isl_equality_from_aff(aff);
2351 /* Update *user to the number of integer divisions in the first element
2352 * of "ma", if it is larger than the current value.
2354 static isl_stat update_n_div(__isl_take isl_set *set,
2355 __isl_take isl_multi_aff *ma, void *user)
2357 isl_aff *aff;
2358 int *n = user;
2359 isl_size n_div;
2361 aff = isl_multi_aff_get_aff(ma, 0);
2362 n_div = isl_aff_dim(aff, isl_dim_div);
2363 isl_aff_free(aff);
2364 isl_multi_aff_free(ma);
2365 isl_set_free(set);
2367 if (n_div > *n)
2368 *n = n_div;
2370 return n_div >= 0 ? isl_stat_ok : isl_stat_error;
2373 /* Get the number of integer divisions in the expression for the iterator
2374 * value at the first slice in the unrolling based on lower bound "lower",
2375 * taking into account the expansion that needs to be performed on this slice.
2377 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2378 __isl_keep isl_aff *lower)
2380 isl_constraint *c;
2381 isl_set *set;
2382 isl_map *it_map, *expansion;
2383 isl_pw_multi_aff *pma;
2384 int n;
2386 c = at_offset(data->depth, lower, 0);
2387 set = isl_set_copy(data->domain);
2388 set = isl_set_add_constraint(set, c);
2389 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2390 set = isl_set_apply(set, expansion);
2391 it_map = isl_ast_build_map_to_iterator(data->build, set);
2392 pma = isl_pw_multi_aff_from_map(it_map);
2393 n = 0;
2394 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2395 n = -1;
2396 isl_pw_multi_aff_free(pma);
2398 return n;
2401 /* Is the lower bound "lower" with corresponding iteration count "n"
2402 * better than the one stored in "data"?
2403 * If there is no upper bound on the iteration count ("n" is infinity) or
2404 * if the count is too large, then we cannot use this lower bound.
2405 * Otherwise, if there was no previous lower bound or
2406 * if the iteration count of the new lower bound is smaller than
2407 * the iteration count of the previous lower bound, then we consider
2408 * the new lower bound to be better.
2409 * If the iteration count is the same, then compare the number
2410 * of integer divisions that would be needed to express
2411 * the iterator value at the first slice in the unrolling
2412 * according to the lower bound. If we end up computing this
2413 * number, then store the lowest value in data->n_div.
2415 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2416 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2418 int cmp;
2419 int n_div;
2421 if (!n)
2422 return -1;
2423 if (isl_val_is_infty(n))
2424 return 0;
2425 if (isl_val_cmp_si(n, INT_MAX) > 0)
2426 return 0;
2427 if (!data->lower)
2428 return 1;
2429 cmp = isl_val_cmp_si(n, *data->n);
2430 if (cmp < 0)
2431 return 1;
2432 if (cmp > 0)
2433 return 0;
2434 if (data->n_div < 0)
2435 data->n_div = get_expanded_n_div(data, data->lower);
2436 if (data->n_div < 0)
2437 return -1;
2438 if (data->n_div == 0)
2439 return 0;
2440 n_div = get_expanded_n_div(data, lower);
2441 if (n_div < 0)
2442 return -1;
2443 if (n_div >= data->n_div)
2444 return 0;
2445 data->n_div = n_div;
2447 return 1;
2450 /* Check if we can use "c" as a lower bound and if it is better than
2451 * any previously found lower bound.
2453 * If "c" does not involve the dimension at the current depth,
2454 * then we cannot use it.
2455 * Otherwise, let "c" be of the form
2457 * i >= f(j)/a
2459 * We compute the maximal value of
2461 * -ceil(f(j)/a)) + i + 1
2463 * over the domain. If there is such a value "n", then we know
2465 * -ceil(f(j)/a)) + i + 1 <= n
2467 * or
2469 * i < ceil(f(j)/a)) + n
2471 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2472 * We just need to check if we have found any lower bound before and
2473 * if the new lower bound is better (smaller n or fewer integer divisions)
2474 * than the previously found lower bounds.
2476 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2477 __isl_keep isl_constraint *c)
2479 isl_aff *aff, *lower;
2480 isl_val *max;
2481 int better;
2483 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2484 return isl_stat_ok;
2486 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2487 lower = isl_aff_ceil(lower);
2488 aff = isl_aff_copy(lower);
2489 aff = isl_aff_neg(aff);
2490 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2491 aff = isl_aff_add_constant_si(aff, 1);
2492 max = isl_set_max_val(data->domain, aff);
2493 isl_aff_free(aff);
2495 better = is_better_lower_bound(data, lower, max);
2496 if (better < 0 || !better) {
2497 isl_val_free(max);
2498 isl_aff_free(lower);
2499 return better < 0 ? isl_stat_error : isl_stat_ok;
2502 isl_aff_free(data->lower);
2503 data->lower = lower;
2504 *data->n = isl_val_get_num_si(max);
2505 isl_val_free(max);
2507 return isl_stat_ok;
2510 /* Check if we can use "c" as a lower bound and if it is better than
2511 * any previously found lower bound.
2513 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2515 struct isl_find_unroll_data *data;
2516 isl_stat r;
2518 data = (struct isl_find_unroll_data *) user;
2519 r = update_unrolling_lower_bound(data, c);
2520 isl_constraint_free(c);
2522 return r;
2525 /* Look for a lower bound l(i) on the dimension at "depth"
2526 * and a size n such that "domain" is a subset of
2528 * { [i] : l(i) <= i_d < l(i) + n }
2530 * where d is "depth" and l(i) depends only on earlier dimensions.
2531 * Furthermore, try and find a lower bound such that n is as small as possible.
2532 * In particular, "n" needs to be finite.
2533 * "build" is the build in which the unrolling will be performed.
2534 * "expansion" is the expansion that needs to be applied to "domain"
2535 * in the unrolling that will be performed.
2537 * Inner dimensions have been eliminated from "domain" by the caller.
2539 * We first construct a collection of lower bounds on the input set
2540 * by computing its simple hull. We then iterate through them,
2541 * discarding those that we cannot use (either because they do not
2542 * involve the dimension at "depth" or because they have no corresponding
2543 * upper bound, meaning that "n" would be unbounded) and pick out the
2544 * best from the remaining ones.
2546 * If we cannot find a suitable lower bound, then we consider that
2547 * to be an error.
2549 static __isl_give isl_aff *find_unroll_lower_bound(
2550 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2551 int depth, __isl_keep isl_basic_map *expansion, int *n)
2553 struct isl_find_unroll_data data =
2554 { build, domain, depth, expansion, NULL, n, -1 };
2555 isl_basic_set *hull;
2557 hull = isl_set_simple_hull(isl_set_copy(domain));
2559 if (isl_basic_set_foreach_constraint(hull,
2560 &constraint_find_unroll, &data) < 0)
2561 goto error;
2563 isl_basic_set_free(hull);
2565 if (!data.lower)
2566 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2567 "cannot find lower bound for unrolling", return NULL);
2569 return data.lower;
2570 error:
2571 isl_basic_set_free(hull);
2572 return isl_aff_free(data.lower);
2575 /* Call "fn" on each iteration of the current dimension of "domain".
2576 * If "init" is not NULL, then it is called with the number of
2577 * iterations before any call to "fn".
2578 * Return -1 on failure.
2580 * Since we are going to be iterating over the individual values,
2581 * we first check if there are any strides on the current dimension.
2582 * If there is, we rewrite the current dimension i as
2584 * i = stride i' + offset
2586 * and then iterate over individual values of i' instead.
2588 * We then look for a lower bound on i' and a size such that the domain
2589 * is a subset of
2591 * { [j,i'] : l(j) <= i' < l(j) + n }
2593 * and then take slices of the domain at values of i'
2594 * between l(j) and l(j) + n - 1.
2596 * We compute the unshifted simple hull of each slice to ensure that
2597 * we have a single basic set per offset. The slicing constraint
2598 * may get simplified away before the unshifted simple hull is taken
2599 * and may therefore in some rare cases disappear from the result.
2600 * We therefore explicitly add the constraint back after computing
2601 * the unshifted simple hull to ensure that the basic sets
2602 * remain disjoint. The constraints that are dropped by taking the hull
2603 * will be taken into account at the next level, as in the case of the
2604 * atomic option.
2606 * Finally, we map i' back to i and call "fn".
2608 static int foreach_iteration(__isl_take isl_set *domain,
2609 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2610 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2612 int i, n;
2613 isl_bool empty;
2614 isl_size depth;
2615 isl_multi_aff *expansion;
2616 isl_basic_map *bmap;
2617 isl_aff *lower = NULL;
2618 isl_ast_build *stride_build;
2620 depth = isl_ast_build_get_depth(build);
2621 if (depth < 0)
2622 domain = isl_set_free(domain);
2624 domain = isl_ast_build_eliminate_inner(build, domain);
2625 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2626 stride_build = isl_ast_build_copy(build);
2627 stride_build = isl_ast_build_detect_strides(stride_build,
2628 isl_set_copy(domain));
2629 expansion = isl_ast_build_get_stride_expansion(stride_build);
2631 domain = isl_set_preimage_multi_aff(domain,
2632 isl_multi_aff_copy(expansion));
2633 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2634 isl_ast_build_free(stride_build);
2636 bmap = isl_basic_map_from_multi_aff(expansion);
2638 empty = isl_set_is_empty(domain);
2639 if (empty < 0) {
2640 n = -1;
2641 } else if (empty) {
2642 n = 0;
2643 } else {
2644 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2645 if (!lower)
2646 n = -1;
2648 if (n >= 0 && init && init(n, user) < 0)
2649 n = -1;
2650 for (i = 0; i < n; ++i) {
2651 isl_set *set;
2652 isl_basic_set *bset;
2653 isl_constraint *slice;
2655 slice = at_offset(depth, lower, i);
2656 set = isl_set_copy(domain);
2657 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2658 bset = isl_set_unshifted_simple_hull(set);
2659 bset = isl_basic_set_add_constraint(bset, slice);
2660 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2662 if (fn(bset, user) < 0)
2663 break;
2666 isl_aff_free(lower);
2667 isl_set_free(domain);
2668 isl_basic_map_free(bmap);
2670 return n < 0 || i < n ? -1 : 0;
2673 /* Data structure for storing the results and the intermediate objects
2674 * of compute_domains.
2676 * "list" is the main result of the function and contains a list
2677 * of disjoint basic sets for which code should be generated.
2679 * "executed" and "build" are inputs to compute_domains.
2680 * "schedule_domain" is the domain of "executed".
2682 * "option" contains the domains at the current depth that should by
2683 * atomic, separated or unrolled. These domains are as specified by
2684 * the user, except that inner dimensions have been eliminated and
2685 * that they have been made pair-wise disjoint.
2687 * "sep_class" contains the user-specified split into separation classes
2688 * specialized to the current depth.
2689 * "done" contains the union of the separation domains that have already
2690 * been handled.
2692 struct isl_codegen_domains {
2693 isl_basic_set_list *list;
2695 isl_union_map *executed;
2696 isl_ast_build *build;
2697 isl_set *schedule_domain;
2699 isl_set *option[4];
2701 isl_map *sep_class;
2702 isl_set *done;
2705 /* Internal data structure for do_unroll.
2707 * "domains" stores the results of compute_domains.
2708 * "class_domain" is the original class domain passed to do_unroll.
2709 * "unroll_domain" collects the unrolled iterations.
2711 struct isl_ast_unroll_data {
2712 struct isl_codegen_domains *domains;
2713 isl_set *class_domain;
2714 isl_set *unroll_domain;
2717 /* Given an iteration of an unrolled domain represented by "bset",
2718 * add it to data->domains->list.
2719 * Since we may have dropped some constraints, we intersect with
2720 * the class domain again to ensure that each element in the list
2721 * is disjoint from the other class domains.
2723 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2725 struct isl_ast_unroll_data *data = user;
2726 isl_set *set;
2727 isl_basic_set_list *list;
2729 set = isl_set_from_basic_set(bset);
2730 data->unroll_domain = isl_set_union(data->unroll_domain,
2731 isl_set_copy(set));
2732 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2733 set = isl_set_make_disjoint(set);
2734 list = isl_basic_set_list_from_set(set);
2735 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2736 list);
2738 return 0;
2741 /* Extend domains->list with a list of basic sets, one for each value
2742 * of the current dimension in "domain" and remove the corresponding
2743 * sets from the class domain. Return the updated class domain.
2744 * The divs that involve the current dimension have not been projected out
2745 * from this domain.
2747 * We call foreach_iteration to iterate over the individual values and
2748 * in do_unroll_iteration we collect the individual basic sets in
2749 * domains->list and their union in data->unroll_domain, which is then
2750 * used to update the class domain.
2752 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2753 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2755 struct isl_ast_unroll_data data;
2757 if (!domain)
2758 return isl_set_free(class_domain);
2759 if (!class_domain)
2760 return isl_set_free(domain);
2762 data.domains = domains;
2763 data.class_domain = class_domain;
2764 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2766 if (foreach_iteration(domain, domains->build, NULL,
2767 &do_unroll_iteration, &data) < 0)
2768 data.unroll_domain = isl_set_free(data.unroll_domain);
2770 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2772 return class_domain;
2775 /* Add domains to domains->list for each individual value of the current
2776 * dimension, for that part of the schedule domain that lies in the
2777 * intersection of the option domain and the class domain.
2778 * Remove the corresponding sets from the class domain and
2779 * return the updated class domain.
2781 * We first break up the unroll option domain into individual pieces
2782 * and then handle each of them separately. The unroll option domain
2783 * has been made disjoint in compute_domains_init_options,
2785 * Note that we actively want to combine different pieces of the
2786 * schedule domain that have the same value at the current dimension.
2787 * We therefore need to break up the unroll option domain before
2788 * intersecting with class and schedule domain, hoping that the
2789 * unroll option domain specified by the user is relatively simple.
2791 static __isl_give isl_set *compute_unroll_domains(
2792 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2794 isl_set *unroll_domain;
2795 isl_basic_set_list *unroll_list;
2796 int i;
2797 isl_size n;
2798 isl_bool empty;
2800 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2801 if (empty < 0)
2802 return isl_set_free(class_domain);
2803 if (empty)
2804 return class_domain;
2806 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2807 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2809 n = isl_basic_set_list_n_basic_set(unroll_list);
2810 if (n < 0)
2811 class_domain = isl_set_free(class_domain);
2812 for (i = 0; i < n; ++i) {
2813 isl_basic_set *bset;
2815 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2816 unroll_domain = isl_set_from_basic_set(bset);
2817 unroll_domain = isl_set_intersect(unroll_domain,
2818 isl_set_copy(class_domain));
2819 unroll_domain = isl_set_intersect(unroll_domain,
2820 isl_set_copy(domains->schedule_domain));
2822 empty = isl_set_is_empty(unroll_domain);
2823 if (empty >= 0 && empty) {
2824 isl_set_free(unroll_domain);
2825 continue;
2828 class_domain = do_unroll(domains, unroll_domain, class_domain);
2831 isl_basic_set_list_free(unroll_list);
2833 return class_domain;
2836 /* Try and construct a single basic set that includes the intersection of
2837 * the schedule domain, the atomic option domain and the class domain.
2838 * Add the resulting basic set(s) to domains->list and remove them
2839 * from class_domain. Return the updated class domain.
2841 * We construct a single domain rather than trying to combine
2842 * the schedule domains of individual domains because we are working
2843 * within a single component so that non-overlapping schedule domains
2844 * should already have been separated.
2845 * We do however need to make sure that this single domains is a subset
2846 * of the class domain so that it would not intersect with any other
2847 * class domains. This means that we may end up splitting up the atomic
2848 * domain in case separation classes are being used.
2850 * "domain" is the intersection of the schedule domain and the class domain,
2851 * with inner dimensions projected out.
2853 static __isl_give isl_set *compute_atomic_domain(
2854 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2856 isl_basic_set *bset;
2857 isl_basic_set_list *list;
2858 isl_set *domain, *atomic_domain;
2859 int empty;
2861 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2862 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2863 domain = isl_set_intersect(domain,
2864 isl_set_copy(domains->schedule_domain));
2865 empty = isl_set_is_empty(domain);
2866 if (empty < 0)
2867 class_domain = isl_set_free(class_domain);
2868 if (empty) {
2869 isl_set_free(domain);
2870 return class_domain;
2873 domain = isl_ast_build_eliminate(domains->build, domain);
2874 domain = isl_set_coalesce_preserve(domain);
2875 bset = isl_set_unshifted_simple_hull(domain);
2876 domain = isl_set_from_basic_set(bset);
2877 atomic_domain = isl_set_copy(domain);
2878 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2879 class_domain = isl_set_subtract(class_domain, atomic_domain);
2880 domain = isl_set_make_disjoint(domain);
2881 list = isl_basic_set_list_from_set(domain);
2882 domains->list = isl_basic_set_list_concat(domains->list, list);
2884 return class_domain;
2887 /* Split up the schedule domain into uniform basic sets,
2888 * in the sense that each element in a basic set is associated to
2889 * elements of the same domains, and add the result to domains->list.
2890 * Do this for that part of the schedule domain that lies in the
2891 * intersection of "class_domain" and the separate option domain.
2893 * "class_domain" may or may not include the constraints
2894 * of the schedule domain, but this does not make a difference
2895 * since we are going to intersect it with the domain of the inverse schedule.
2896 * If it includes schedule domain constraints, then they may involve
2897 * inner dimensions, but we will eliminate them in separation_domain.
2899 static int compute_separate_domain(struct isl_codegen_domains *domains,
2900 __isl_keep isl_set *class_domain)
2902 isl_space *space;
2903 isl_set *domain;
2904 isl_union_map *executed;
2905 isl_basic_set_list *list;
2906 int empty;
2908 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2909 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2910 executed = isl_union_map_copy(domains->executed);
2911 executed = isl_union_map_intersect_domain(executed,
2912 isl_union_set_from_set(domain));
2913 empty = isl_union_map_is_empty(executed);
2914 if (empty < 0 || empty) {
2915 isl_union_map_free(executed);
2916 return empty < 0 ? -1 : 0;
2919 space = isl_set_get_space(class_domain);
2920 domain = separate_schedule_domains(space, executed, domains->build);
2922 list = isl_basic_set_list_from_set(domain);
2923 domains->list = isl_basic_set_list_concat(domains->list, list);
2925 return 0;
2928 /* Split up the domain at the current depth into disjoint
2929 * basic sets for which code should be generated separately
2930 * for the given separation class domain.
2932 * If any separation classes have been defined, then "class_domain"
2933 * is the domain of the current class and does not refer to inner dimensions.
2934 * Otherwise, "class_domain" is the universe domain.
2936 * We first make sure that the class domain is disjoint from
2937 * previously considered class domains.
2939 * The separate domains can be computed directly from the "class_domain".
2941 * The unroll, atomic and remainder domains need the constraints
2942 * from the schedule domain.
2944 * For unrolling, the actual schedule domain is needed (with divs that
2945 * may refer to the current dimension) so that stride detection can be
2946 * performed.
2948 * For atomic and remainder domains, inner dimensions and divs involving
2949 * the current dimensions should be eliminated.
2950 * In case we are working within a separation class, we need to intersect
2951 * the result with the current "class_domain" to ensure that the domains
2952 * are disjoint from those generated from other class domains.
2954 * The domain that has been made atomic may be larger than specified
2955 * by the user since it needs to be representable as a single basic set.
2956 * This possibly larger domain is removed from class_domain by
2957 * compute_atomic_domain. It is computed first so that the extended domain
2958 * would not overlap with any domains computed before.
2959 * Similary, the unrolled domains may have some constraints removed and
2960 * may therefore also be larger than specified by the user.
2962 * If anything is left after handling separate, unroll and atomic,
2963 * we split it up into basic sets and append the basic sets to domains->list.
2965 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2966 __isl_take isl_set *class_domain)
2968 isl_basic_set_list *list;
2969 isl_set *domain;
2971 class_domain = isl_set_subtract(class_domain,
2972 isl_set_copy(domains->done));
2973 domains->done = isl_set_union(domains->done,
2974 isl_set_copy(class_domain));
2976 class_domain = compute_atomic_domain(domains, class_domain);
2977 class_domain = compute_unroll_domains(domains, class_domain);
2979 domain = isl_set_copy(class_domain);
2981 if (compute_separate_domain(domains, domain) < 0)
2982 goto error;
2983 domain = isl_set_subtract(domain,
2984 isl_set_copy(domains->option[isl_ast_loop_separate]));
2986 domain = isl_set_intersect(domain,
2987 isl_set_copy(domains->schedule_domain));
2989 domain = isl_ast_build_eliminate(domains->build, domain);
2990 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2992 domain = isl_set_coalesce_preserve(domain);
2993 domain = isl_set_make_disjoint(domain);
2995 list = isl_basic_set_list_from_set(domain);
2996 domains->list = isl_basic_set_list_concat(domains->list, list);
2998 isl_set_free(class_domain);
3000 return isl_stat_ok;
3001 error:
3002 isl_set_free(domain);
3003 isl_set_free(class_domain);
3004 return isl_stat_error;
3007 /* Split up the domain at the current depth into disjoint
3008 * basic sets for which code should be generated separately
3009 * for the separation class identified by "pnt".
3011 * We extract the corresponding class domain from domains->sep_class,
3012 * eliminate inner dimensions and pass control to compute_partial_domains.
3014 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
3016 struct isl_codegen_domains *domains = user;
3017 isl_set *class_set;
3018 isl_set *domain;
3019 int disjoint;
3021 class_set = isl_set_from_point(pnt);
3022 domain = isl_map_domain(isl_map_intersect_range(
3023 isl_map_copy(domains->sep_class), class_set));
3024 domain = isl_ast_build_compute_gist(domains->build, domain);
3025 domain = isl_ast_build_eliminate(domains->build, domain);
3027 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
3028 if (disjoint < 0)
3029 return isl_stat_error;
3030 if (disjoint) {
3031 isl_set_free(domain);
3032 return isl_stat_ok;
3035 return compute_partial_domains(domains, domain);
3038 /* Extract the domains at the current depth that should be atomic,
3039 * separated or unrolled and store them in option.
3041 * The domains specified by the user might overlap, so we make
3042 * them disjoint by subtracting earlier domains from later domains.
3044 static void compute_domains_init_options(isl_set *option[4],
3045 __isl_keep isl_ast_build *build)
3047 enum isl_ast_loop_type type, type2;
3048 isl_set *unroll;
3050 for (type = isl_ast_loop_atomic;
3051 type <= isl_ast_loop_separate; ++type) {
3052 option[type] = isl_ast_build_get_option_domain(build, type);
3053 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3054 option[type] = isl_set_subtract(option[type],
3055 isl_set_copy(option[type2]));
3058 unroll = option[isl_ast_loop_unroll];
3059 unroll = isl_set_coalesce(unroll);
3060 unroll = isl_set_make_disjoint(unroll);
3061 option[isl_ast_loop_unroll] = unroll;
3064 /* Split up the domain at the current depth into disjoint
3065 * basic sets for which code should be generated separately,
3066 * based on the user-specified options.
3067 * Return the list of disjoint basic sets.
3069 * There are three kinds of domains that we need to keep track of.
3070 * - the "schedule domain" is the domain of "executed"
3071 * - the "class domain" is the domain corresponding to the currrent
3072 * separation class
3073 * - the "option domain" is the domain corresponding to one of the options
3074 * atomic, unroll or separate
3076 * We first consider the individial values of the separation classes
3077 * and split up the domain for each of them separately.
3078 * Finally, we consider the remainder. If no separation classes were
3079 * specified, then we call compute_partial_domains with the universe
3080 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3081 * with inner dimensions removed. We do this because we want to
3082 * avoid computing the complement of the class domains (i.e., the difference
3083 * between the universe and domains->done).
3085 static __isl_give isl_basic_set_list *compute_domains(
3086 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3088 struct isl_codegen_domains domains;
3089 isl_ctx *ctx;
3090 isl_set *domain;
3091 isl_union_set *schedule_domain;
3092 isl_set *classes;
3093 isl_space *space;
3094 int n_param;
3095 enum isl_ast_loop_type type;
3096 isl_bool empty;
3098 if (!executed)
3099 return NULL;
3101 ctx = isl_union_map_get_ctx(executed);
3102 domains.list = isl_basic_set_list_alloc(ctx, 0);
3104 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3105 domain = isl_set_from_union_set(schedule_domain);
3107 compute_domains_init_options(domains.option, build);
3109 domains.sep_class = isl_ast_build_get_separation_class(build);
3110 classes = isl_map_range(isl_map_copy(domains.sep_class));
3111 n_param = isl_set_dim(classes, isl_dim_param);
3112 if (n_param < 0)
3113 classes = isl_set_free(classes);
3114 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3116 space = isl_set_get_space(domain);
3117 domains.build = build;
3118 domains.schedule_domain = isl_set_copy(domain);
3119 domains.executed = executed;
3120 domains.done = isl_set_empty(space);
3122 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3123 domains.list = isl_basic_set_list_free(domains.list);
3124 isl_set_free(classes);
3126 empty = isl_set_is_empty(domains.done);
3127 if (empty < 0) {
3128 domains.list = isl_basic_set_list_free(domains.list);
3129 domain = isl_set_free(domain);
3130 } else if (empty) {
3131 isl_set_free(domain);
3132 domain = isl_set_universe(isl_set_get_space(domains.done));
3133 } else {
3134 domain = isl_ast_build_eliminate(build, domain);
3136 if (compute_partial_domains(&domains, domain) < 0)
3137 domains.list = isl_basic_set_list_free(domains.list);
3139 isl_set_free(domains.schedule_domain);
3140 isl_set_free(domains.done);
3141 isl_map_free(domains.sep_class);
3142 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3143 isl_set_free(domains.option[type]);
3145 return domains.list;
3148 /* Generate code for a single component, after shifting (if any)
3149 * has been applied, in case the schedule was specified as a union map.
3151 * We first split up the domain at the current depth into disjoint
3152 * basic sets based on the user-specified options.
3153 * Then we generated code for each of them and concatenate the results.
3155 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3156 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3158 isl_basic_set_list *domain_list;
3159 isl_ast_graft_list *list = NULL;
3161 domain_list = compute_domains(executed, build);
3162 list = generate_parallel_domains(domain_list, executed, build);
3164 isl_basic_set_list_free(domain_list);
3165 isl_union_map_free(executed);
3166 isl_ast_build_free(build);
3168 return list;
3171 /* Generate code for a single component, after shifting (if any)
3172 * has been applied, in case the schedule was specified as a schedule tree
3173 * and the separate option was specified.
3175 * We perform separation on the domain of "executed" and then generate
3176 * an AST for each of the resulting disjoint basic sets.
3178 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3179 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3181 isl_space *space;
3182 isl_set *domain;
3183 isl_basic_set_list *domain_list;
3184 isl_ast_graft_list *list;
3186 space = isl_ast_build_get_space(build, 1);
3187 domain = separate_schedule_domains(space,
3188 isl_union_map_copy(executed), build);
3189 domain_list = isl_basic_set_list_from_set(domain);
3191 list = generate_parallel_domains(domain_list, executed, build);
3193 isl_basic_set_list_free(domain_list);
3194 isl_union_map_free(executed);
3195 isl_ast_build_free(build);
3197 return list;
3200 /* Internal data structure for generate_shifted_component_tree_unroll.
3202 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3203 * "list" collects the constructs grafts.
3205 struct isl_ast_unroll_tree_data {
3206 isl_union_map *executed;
3207 isl_ast_build *build;
3208 isl_ast_graft_list *list;
3211 /* Initialize data->list to a list of "n" elements.
3213 static int init_unroll_tree(int n, void *user)
3215 struct isl_ast_unroll_tree_data *data = user;
3216 isl_ctx *ctx;
3218 ctx = isl_ast_build_get_ctx(data->build);
3219 data->list = isl_ast_graft_list_alloc(ctx, n);
3221 return 0;
3224 /* Given an iteration of an unrolled domain represented by "bset",
3225 * generate the corresponding AST and add the result to data->list.
3227 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3229 struct isl_ast_unroll_tree_data *data = user;
3231 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3232 bset, isl_ast_build_copy(data->build));
3234 return 0;
3237 /* Generate code for a single component, after shifting (if any)
3238 * has been applied, in case the schedule was specified as a schedule tree
3239 * and the unroll option was specified.
3241 * We call foreach_iteration to iterate over the individual values and
3242 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3244 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3245 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3246 __isl_take isl_ast_build *build)
3248 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3250 if (foreach_iteration(domain, build, &init_unroll_tree,
3251 &do_unroll_tree_iteration, &data) < 0)
3252 data.list = isl_ast_graft_list_free(data.list);
3254 isl_union_map_free(executed);
3255 isl_ast_build_free(build);
3257 return data.list;
3260 /* Does "domain" involve a disjunction that is purely based on
3261 * constraints involving only outer dimension?
3263 * In particular, is there a disjunction such that the constraints
3264 * involving the current and later dimensions are the same over
3265 * all the disjuncts?
3267 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3268 __isl_keep isl_ast_build *build)
3270 isl_basic_set *hull;
3271 isl_set *shared, *inner;
3272 isl_bool equal;
3273 isl_size depth;
3274 isl_size n;
3275 isl_size dim;
3277 n = isl_set_n_basic_set(domain);
3278 if (n < 0)
3279 return isl_bool_error;
3280 if (n <= 1)
3281 return isl_bool_false;
3282 dim = isl_set_dim(domain, isl_dim_set);
3283 depth = isl_ast_build_get_depth(build);
3284 if (dim < 0 || depth < 0)
3285 return isl_bool_error;
3287 inner = isl_set_copy(domain);
3288 inner = isl_set_drop_constraints_not_involving_dims(inner,
3289 isl_dim_set, depth, dim - depth);
3290 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3291 shared = isl_set_from_basic_set(hull);
3292 equal = isl_set_plain_is_equal(inner, shared);
3293 isl_set_free(inner);
3294 isl_set_free(shared);
3296 return equal;
3299 /* Generate code for a single component, after shifting (if any)
3300 * has been applied, in case the schedule was specified as a schedule tree.
3301 * In particular, handle the base case where there is either no isolated
3302 * set or we are within the isolated set (in which case "isolated" is set)
3303 * or the iterations that precede or follow the isolated set.
3305 * The schedule domain is broken up or combined into basic sets
3306 * according to the AST generation option specified in the current
3307 * schedule node, which may be either atomic, separate, unroll or
3308 * unspecified. If the option is unspecified, then we currently simply
3309 * split the schedule domain into disjoint basic sets.
3311 * In case the separate option is specified, the AST generation is
3312 * handled by generate_shifted_component_tree_separate.
3313 * In the other cases, we need the global schedule domain.
3314 * In the unroll case, the AST generation is then handled by
3315 * generate_shifted_component_tree_unroll which needs the actual
3316 * schedule domain (with divs that may refer to the current dimension)
3317 * so that stride detection can be performed.
3318 * In the atomic or unspecified case, inner dimensions and divs involving
3319 * the current dimensions should be eliminated.
3320 * The result is then either combined into a single basic set or
3321 * split up into disjoint basic sets.
3322 * Finally an AST is generated for each basic set and the results are
3323 * concatenated.
3325 * If the schedule domain involves a disjunction that is purely based on
3326 * constraints involving only outer dimension, then it is treated as
3327 * if atomic was specified. This ensures that only a single loop
3328 * is generated instead of a sequence of identical loops with
3329 * different guards.
3331 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3332 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3333 int isolated)
3335 isl_bool outer_disjunction;
3336 isl_union_set *schedule_domain;
3337 isl_set *domain;
3338 isl_basic_set_list *domain_list;
3339 isl_ast_graft_list *list;
3340 enum isl_ast_loop_type type;
3342 type = isl_ast_build_get_loop_type(build, isolated);
3343 if (type < 0)
3344 goto error;
3346 if (type == isl_ast_loop_separate)
3347 return generate_shifted_component_tree_separate(executed,
3348 build);
3350 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3351 domain = isl_set_from_union_set(schedule_domain);
3353 if (type == isl_ast_loop_unroll)
3354 return generate_shifted_component_tree_unroll(executed, domain,
3355 build);
3357 domain = isl_ast_build_eliminate(build, domain);
3358 domain = isl_set_coalesce_preserve(domain);
3360 outer_disjunction = has_pure_outer_disjunction(domain, build);
3361 if (outer_disjunction < 0)
3362 domain = isl_set_free(domain);
3364 if (outer_disjunction || type == isl_ast_loop_atomic) {
3365 isl_basic_set *hull;
3366 hull = isl_set_unshifted_simple_hull(domain);
3367 domain_list = isl_basic_set_list_from_basic_set(hull);
3368 } else {
3369 domain = isl_set_make_disjoint(domain);
3370 domain_list = isl_basic_set_list_from_set(domain);
3373 list = generate_parallel_domains(domain_list, executed, build);
3375 isl_basic_set_list_free(domain_list);
3376 isl_union_map_free(executed);
3377 isl_ast_build_free(build);
3379 return list;
3380 error:
3381 isl_union_map_free(executed);
3382 isl_ast_build_free(build);
3383 return NULL;
3386 /* Extract out the disjunction imposed by "domain" on the outer
3387 * schedule dimensions.
3389 * In particular, remove all inner dimensions from "domain" (including
3390 * the current dimension) and then remove the constraints that are shared
3391 * by all disjuncts in the result.
3393 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3394 __isl_keep isl_ast_build *build)
3396 isl_set *hull;
3397 isl_size depth;
3398 isl_size dim;
3400 domain = isl_ast_build_specialize(build, domain);
3401 depth = isl_ast_build_get_depth(build);
3402 dim = isl_set_dim(domain, isl_dim_set);
3403 if (depth < 0 || dim < 0)
3404 return isl_set_free(domain);
3405 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3406 domain = isl_set_remove_unknown_divs(domain);
3407 hull = isl_set_copy(domain);
3408 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3409 domain = isl_set_gist(domain, hull);
3411 return domain;
3414 /* Add "guard" to the grafts in "list".
3415 * "build" is the outer AST build, while "sub_build" includes "guard"
3416 * in its generated domain.
3418 * First combine the grafts into a single graft and then add the guard.
3419 * If the list is empty, or if some error occurred, then simply return
3420 * the list.
3422 static __isl_give isl_ast_graft_list *list_add_guard(
3423 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3424 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3426 isl_ast_graft *graft;
3427 isl_size n;
3429 list = isl_ast_graft_list_fuse(list, sub_build);
3431 n = isl_ast_graft_list_n_ast_graft(list);
3432 if (n < 0)
3433 return isl_ast_graft_list_free(list);
3434 if (n != 1)
3435 return list;
3437 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3438 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3439 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3441 return list;
3444 /* Generate code for a single component, after shifting (if any)
3445 * has been applied, in case the schedule was specified as a schedule tree.
3446 * In particular, do so for the specified subset of the schedule domain.
3448 * If we are outside of the isolated part, then "domain" may include
3449 * a disjunction. Explicitly generate this disjunction at this point
3450 * instead of relying on the disjunction getting hoisted back up
3451 * to this level.
3453 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3454 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3455 __isl_keep isl_ast_build *build, int isolated)
3457 isl_union_set *uset;
3458 isl_ast_graft_list *list;
3459 isl_ast_build *sub_build;
3460 int empty;
3462 uset = isl_union_set_from_set(isl_set_copy(domain));
3463 executed = isl_union_map_copy(executed);
3464 executed = isl_union_map_intersect_domain(executed, uset);
3465 empty = isl_union_map_is_empty(executed);
3466 if (empty < 0)
3467 goto error;
3468 if (empty) {
3469 isl_ctx *ctx;
3470 isl_union_map_free(executed);
3471 isl_set_free(domain);
3472 ctx = isl_ast_build_get_ctx(build);
3473 return isl_ast_graft_list_alloc(ctx, 0);
3476 sub_build = isl_ast_build_copy(build);
3477 if (!isolated) {
3478 domain = extract_disjunction(domain, build);
3479 sub_build = isl_ast_build_restrict_generated(sub_build,
3480 isl_set_copy(domain));
3482 list = generate_shifted_component_tree_base(executed,
3483 isl_ast_build_copy(sub_build), isolated);
3484 if (!isolated)
3485 list = list_add_guard(list, domain, build, sub_build);
3486 isl_ast_build_free(sub_build);
3487 isl_set_free(domain);
3488 return list;
3489 error:
3490 isl_union_map_free(executed);
3491 isl_set_free(domain);
3492 return NULL;
3495 /* Generate code for a single component, after shifting (if any)
3496 * has been applied, in case the schedule was specified as a schedule tree.
3497 * In particular, do so for the specified sequence of subsets
3498 * of the schedule domain, "before", "isolated", "after" and "other",
3499 * where only the "isolated" part is considered to be isolated.
3501 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3502 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3503 __isl_take isl_set *isolated, __isl_take isl_set *after,
3504 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3506 isl_ast_graft_list *list, *res;
3508 res = generate_shifted_component_tree_part(executed, before, build, 0);
3509 list = generate_shifted_component_tree_part(executed, isolated,
3510 build, 1);
3511 res = isl_ast_graft_list_concat(res, list);
3512 list = generate_shifted_component_tree_part(executed, after, build, 0);
3513 res = isl_ast_graft_list_concat(res, list);
3514 list = generate_shifted_component_tree_part(executed, other, build, 0);
3515 res = isl_ast_graft_list_concat(res, list);
3517 isl_union_map_free(executed);
3518 isl_ast_build_free(build);
3520 return res;
3523 /* Does "set" intersect "first", but not "second"?
3525 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3526 __isl_keep isl_set *first, __isl_keep isl_set *second)
3528 isl_bool disjoint;
3530 disjoint = isl_set_is_disjoint(set, first);
3531 if (disjoint < 0)
3532 return isl_bool_error;
3533 if (disjoint)
3534 return isl_bool_false;
3536 return isl_set_is_disjoint(set, second);
3539 /* Generate code for a single component, after shifting (if any)
3540 * has been applied, in case the schedule was specified as a schedule tree.
3541 * In particular, do so in case of isolation where there is
3542 * only an "isolated" part and an "after" part.
3543 * "dead1" and "dead2" are freed by this function in order to simplify
3544 * the caller.
3546 * The "before" and "other" parts are set to empty sets.
3548 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3549 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3550 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3551 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3553 isl_set *empty;
3555 empty = isl_set_empty(isl_set_get_space(after));
3556 isl_set_free(dead1);
3557 isl_set_free(dead2);
3558 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3559 isolated, after, empty, build);
3562 /* Generate code for a single component, after shifting (if any)
3563 * has been applied, in case the schedule was specified as a schedule tree.
3565 * We first check if the user has specified an isolated schedule domain
3566 * and that we are not already outside of this isolated schedule domain.
3567 * If so, we break up the schedule domain into iterations that
3568 * precede the isolated domain, the isolated domain itself,
3569 * the iterations that follow the isolated domain and
3570 * the remaining iterations (those that are incomparable
3571 * to the isolated domain).
3572 * We generate an AST for each piece and concatenate the results.
3574 * If the isolated domain is not convex, then it is replaced
3575 * by a convex superset to ensure that the sets of preceding and
3576 * following iterations are properly defined and, in particular,
3577 * that there are no intermediate iterations that do not belong
3578 * to the isolated domain.
3580 * In the special case where at least one element of the schedule
3581 * domain that does not belong to the isolated domain needs
3582 * to be scheduled after this isolated domain, but none of those
3583 * elements need to be scheduled before, break up the schedule domain
3584 * in only two parts, the isolated domain, and a part that will be
3585 * scheduled after the isolated domain.
3587 * If no isolated set has been specified, then we generate an
3588 * AST for the entire inverse schedule.
3590 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3591 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3593 int i;
3594 isl_size depth;
3595 int empty, has_isolate;
3596 isl_space *space;
3597 isl_union_set *schedule_domain;
3598 isl_set *domain;
3599 isl_basic_set *hull;
3600 isl_set *isolated, *before, *after, *test;
3601 isl_map *gt, *lt;
3602 isl_bool pure;
3604 build = isl_ast_build_extract_isolated(build);
3605 has_isolate = isl_ast_build_has_isolated(build);
3606 if (has_isolate < 0)
3607 executed = isl_union_map_free(executed);
3608 else if (!has_isolate)
3609 return generate_shifted_component_tree_base(executed, build, 0);
3611 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3612 domain = isl_set_from_union_set(schedule_domain);
3614 isolated = isl_ast_build_get_isolated(build);
3615 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3616 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3617 empty = isl_set_is_empty(test);
3618 isl_set_free(test);
3619 if (empty < 0)
3620 goto error;
3621 if (empty) {
3622 isl_set_free(isolated);
3623 isl_set_free(domain);
3624 return generate_shifted_component_tree_base(executed, build, 0);
3626 depth = isl_ast_build_get_depth(build);
3627 if (depth < 0)
3628 goto error;
3630 isolated = isl_ast_build_eliminate(build, isolated);
3631 hull = isl_set_unshifted_simple_hull(isolated);
3632 isolated = isl_set_from_basic_set(hull);
3634 space = isl_space_map_from_set(isl_set_get_space(isolated));
3635 gt = isl_map_universe(space);
3636 for (i = 0; i < depth; ++i)
3637 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3638 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3639 lt = isl_map_reverse(isl_map_copy(gt));
3640 before = isl_set_apply(isl_set_copy(isolated), gt);
3641 after = isl_set_apply(isl_set_copy(isolated), lt);
3643 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3644 pure = only_intersects_first(domain, after, before);
3645 if (pure < 0)
3646 executed = isl_union_map_free(executed);
3647 else if (pure)
3648 return generate_shifted_component_only_after(executed, isolated,
3649 domain, build, before, after);
3650 domain = isl_set_subtract(domain, isl_set_copy(before));
3651 domain = isl_set_subtract(domain, isl_set_copy(after));
3652 after = isl_set_subtract(after, isl_set_copy(isolated));
3653 after = isl_set_subtract(after, isl_set_copy(before));
3654 before = isl_set_subtract(before, isl_set_copy(isolated));
3656 return generate_shifted_component_parts(executed, before, isolated,
3657 after, domain, build);
3658 error:
3659 isl_set_free(domain);
3660 isl_set_free(isolated);
3661 isl_union_map_free(executed);
3662 isl_ast_build_free(build);
3663 return NULL;
3666 /* Generate code for a single component, after shifting (if any)
3667 * has been applied.
3669 * Call generate_shifted_component_tree or generate_shifted_component_flat
3670 * depending on whether the schedule was specified as a schedule tree.
3672 static __isl_give isl_ast_graft_list *generate_shifted_component(
3673 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3675 if (isl_ast_build_has_schedule_node(build))
3676 return generate_shifted_component_tree(executed, build);
3677 else
3678 return generate_shifted_component_flat(executed, build);
3681 struct isl_set_map_pair {
3682 isl_set *set;
3683 isl_map *map;
3686 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3687 * of indices into the "domain" array,
3688 * return the union of the "map" fields of the elements
3689 * indexed by the first "n" elements of "order".
3691 static __isl_give isl_union_map *construct_component_executed(
3692 struct isl_set_map_pair *domain, int *order, int n)
3694 int i;
3695 isl_map *map;
3696 isl_union_map *executed;
3698 map = isl_map_copy(domain[order[0]].map);
3699 executed = isl_union_map_from_map(map);
3700 for (i = 1; i < n; ++i) {
3701 map = isl_map_copy(domain[order[i]].map);
3702 executed = isl_union_map_add_map(executed, map);
3705 return executed;
3708 /* Generate code for a single component, after shifting (if any)
3709 * has been applied.
3711 * The component inverse schedule is specified as the "map" fields
3712 * of the elements of "domain" indexed by the first "n" elements of "order".
3714 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3715 struct isl_set_map_pair *domain, int *order, int n,
3716 __isl_take isl_ast_build *build)
3718 isl_union_map *executed;
3720 executed = construct_component_executed(domain, order, n);
3721 return generate_shifted_component(executed, build);
3724 /* Does set dimension "pos" of "set" have an obviously fixed value?
3726 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3728 int fixed;
3729 isl_val *v;
3731 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3732 if (!v)
3733 return -1;
3734 fixed = !isl_val_is_nan(v);
3735 isl_val_free(v);
3737 return fixed;
3740 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3741 * of indices into the "domain" array,
3742 * do all (except for at most one) of the "set" field of the elements
3743 * indexed by the first "n" elements of "order" have a fixed value
3744 * at position "depth"?
3746 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3747 int *order, int n, int depth)
3749 int i;
3750 int non_fixed = -1;
3752 for (i = 0; i < n; ++i) {
3753 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 if (non_fixed >= 0)
3761 return 0;
3762 non_fixed = i;
3765 return 1;
3768 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3769 * of indices into the "domain" array,
3770 * eliminate the inner dimensions from the "set" field of the elements
3771 * indexed by the first "n" elements of "order", provided the current
3772 * dimension does not have a fixed value.
3774 * Return the index of the first element in "order" with a corresponding
3775 * "set" field that does not have an (obviously) fixed value.
3777 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3778 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3780 int i;
3781 int base = -1;
3783 for (i = n - 1; i >= 0; --i) {
3784 int f;
3785 f = dim_is_fixed(domain[order[i]].set, depth);
3786 if (f < 0)
3787 return -1;
3788 if (f)
3789 continue;
3790 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3791 domain[order[i]].set);
3792 base = i;
3795 return base;
3798 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3799 * of indices into the "domain" array,
3800 * find the element of "domain" (amongst those indexed by the first "n"
3801 * elements of "order") with the "set" field that has the smallest
3802 * value for the current iterator.
3804 * Note that the domain with the smallest value may depend on the parameters
3805 * and/or outer loop dimension. Since the result of this function is only
3806 * used as heuristic, we only make a reasonable attempt at finding the best
3807 * domain, one that should work in case a single domain provides the smallest
3808 * value for the current dimension over all values of the parameters
3809 * and outer dimensions.
3811 * In particular, we compute the smallest value of the first domain
3812 * and replace it by that of any later domain if that later domain
3813 * has a smallest value that is smaller for at least some value
3814 * of the parameters and outer dimensions.
3816 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3817 __isl_keep isl_ast_build *build)
3819 int i;
3820 isl_map *min_first;
3821 int first = 0;
3823 min_first = isl_ast_build_map_to_iterator(build,
3824 isl_set_copy(domain[order[0]].set));
3825 min_first = isl_map_lexmin(min_first);
3827 for (i = 1; i < n; ++i) {
3828 isl_map *min, *test;
3829 int empty;
3831 min = isl_ast_build_map_to_iterator(build,
3832 isl_set_copy(domain[order[i]].set));
3833 min = isl_map_lexmin(min);
3834 test = isl_map_copy(min);
3835 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3836 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3837 empty = isl_map_is_empty(test);
3838 isl_map_free(test);
3839 if (empty >= 0 && !empty) {
3840 isl_map_free(min_first);
3841 first = i;
3842 min_first = min;
3843 } else
3844 isl_map_free(min);
3846 if (empty < 0)
3847 break;
3850 isl_map_free(min_first);
3852 return i < n ? -1 : first;
3855 /* Construct a shifted inverse schedule based on the original inverse schedule,
3856 * the stride and the offset.
3858 * The original inverse schedule is specified as the "map" fields
3859 * of the elements of "domain" indexed by the first "n" elements of "order".
3861 * "stride" and "offset" are such that the difference
3862 * between the values of the current dimension of domain "i"
3863 * and the values of the current dimension for some reference domain are
3864 * equal to
3866 * stride * integer + offset[i]
3868 * Moreover, 0 <= offset[i] < stride.
3870 * For each domain, we create a map
3872 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3874 * where j refers to the current dimension and the other dimensions are
3875 * unchanged, and apply this map to the original schedule domain.
3877 * For example, for the original schedule
3879 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3881 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3882 * we apply the mapping
3884 * { [j] -> [j, 0] }
3886 * to the schedule of the "A" domain and the mapping
3888 * { [j - 1] -> [j, 1] }
3890 * to the schedule of the "B" domain.
3893 * Note that after the transformation, the differences between pairs
3894 * of values of the current dimension over all domains are multiples
3895 * of stride and that we have therefore exposed the stride.
3898 * To see that the mapping preserves the lexicographic order,
3899 * first note that each of the individual maps above preserves the order.
3900 * If the value of the current iterator is j1 in one domain and j2 in another,
3901 * then if j1 = j2, we know that the same map is applied to both domains
3902 * and the order is preserved.
3903 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3904 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3906 * j1 - c1 < j2 - c2
3908 * and the order is preserved.
3909 * If c1 < c2, then we know
3911 * 0 <= c2 - c1 < s
3913 * We also have
3915 * j2 - j1 = n * s + r
3917 * with n >= 0 and 0 <= r < s.
3918 * In other words, r = c2 - c1.
3919 * If n > 0, then
3921 * j1 - c1 < j2 - c2
3923 * If n = 0, then
3925 * j1 - c1 = j2 - c2
3927 * and so
3929 * (j1 - c1, c1) << (j2 - c2, c2)
3931 * with "<<" the lexicographic order, proving that the order is preserved
3932 * in all cases.
3934 static __isl_give isl_union_map *construct_shifted_executed(
3935 struct isl_set_map_pair *domain, int *order, int n,
3936 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3937 __isl_keep isl_ast_build *build)
3939 int i;
3940 isl_union_map *executed;
3941 isl_space *space;
3942 isl_map *map;
3943 isl_size depth;
3944 isl_constraint *c;
3946 depth = isl_ast_build_get_depth(build);
3947 if (depth < 0)
3948 return NULL;
3949 space = isl_ast_build_get_space(build, 1);
3950 executed = isl_union_map_empty(isl_space_copy(space));
3951 space = isl_space_map_from_set(space);
3952 map = isl_map_identity(isl_space_copy(space));
3953 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3954 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3955 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3957 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3958 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3959 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3961 for (i = 0; i < n; ++i) {
3962 isl_map *map_i;
3963 isl_val *v;
3965 v = isl_multi_val_get_val(offset, i);
3966 if (!v)
3967 break;
3968 map_i = isl_map_copy(map);
3969 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3970 isl_val_copy(v));
3971 v = isl_val_neg(v);
3972 c = isl_constraint_set_constant_val(c, v);
3973 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3975 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3976 map_i);
3977 executed = isl_union_map_add_map(executed, map_i);
3980 isl_constraint_free(c);
3981 isl_map_free(map);
3983 if (i < n)
3984 executed = isl_union_map_free(executed);
3986 return executed;
3989 /* Generate code for a single component, after exposing the stride,
3990 * given that the schedule domain is "shifted strided".
3992 * The component inverse schedule is specified as the "map" fields
3993 * of the elements of "domain" indexed by the first "n" elements of "order".
3995 * The schedule domain being "shifted strided" means that the differences
3996 * between the values of the current dimension of domain "i"
3997 * and the values of the current dimension for some reference domain are
3998 * equal to
4000 * stride * integer + offset[i]
4002 * We first look for the domain with the "smallest" value for the current
4003 * dimension and adjust the offsets such that the offset of the "smallest"
4004 * domain is equal to zero. The other offsets are reduced modulo stride.
4006 * Based on this information, we construct a new inverse schedule in
4007 * construct_shifted_executed that exposes the stride.
4008 * Since this involves the introduction of a new schedule dimension,
4009 * the build needs to be changed accordingly.
4010 * After computing the AST, the newly introduced dimension needs
4011 * to be removed again from the list of grafts. We do this by plugging
4012 * in a mapping that represents the new schedule domain in terms of the
4013 * old schedule domain.
4015 static __isl_give isl_ast_graft_list *generate_shift_component(
4016 struct isl_set_map_pair *domain, int *order, int n,
4017 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
4018 __isl_take isl_ast_build *build)
4020 isl_ast_graft_list *list;
4021 int first;
4022 isl_size depth;
4023 isl_val *val;
4024 isl_multi_val *mv;
4025 isl_space *space;
4026 isl_multi_aff *ma, *zero;
4027 isl_union_map *executed;
4029 depth = isl_ast_build_get_depth(build);
4031 first = first_offset(domain, order, n, build);
4032 if (depth < 0 || first < 0)
4033 goto error;
4035 mv = isl_multi_val_copy(offset);
4036 val = isl_multi_val_get_val(offset, first);
4037 val = isl_val_neg(val);
4038 mv = isl_multi_val_add_val(mv, val);
4039 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
4041 executed = construct_shifted_executed(domain, order, n, stride, mv,
4042 build);
4043 space = isl_ast_build_get_space(build, 1);
4044 space = isl_space_map_from_set(space);
4045 ma = isl_multi_aff_identity(isl_space_copy(space));
4046 space = isl_space_from_domain(isl_space_domain(space));
4047 space = isl_space_add_dims(space, isl_dim_out, 1);
4048 zero = isl_multi_aff_zero(space);
4049 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
4050 build = isl_ast_build_insert_dim(build, depth + 1);
4051 list = generate_shifted_component(executed, build);
4053 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4055 isl_multi_val_free(mv);
4057 return list;
4058 error:
4059 isl_ast_build_free(build);
4060 return NULL;
4063 /* Does any node in the schedule tree rooted at the current schedule node
4064 * of "build" depend on outer schedule nodes?
4066 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4068 isl_schedule_node *node;
4069 int dependent = 0;
4071 node = isl_ast_build_get_schedule_node(build);
4072 dependent = isl_schedule_node_is_subtree_anchored(node);
4073 isl_schedule_node_free(node);
4075 return dependent;
4078 /* Generate code for a single component.
4080 * The component inverse schedule is specified as the "map" fields
4081 * of the elements of "domain" indexed by the first "n" elements of "order".
4083 * This function may modify the "set" fields of "domain".
4085 * Before proceeding with the actual code generation for the component,
4086 * we first check if there are any "shifted" strides, meaning that
4087 * the schedule domains of the individual domains are all strided,
4088 * but that they have different offsets, resulting in the union
4089 * of schedule domains not being strided anymore.
4091 * The simplest example is the schedule
4093 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4095 * Both schedule domains are strided, but their union is not.
4096 * This function detects such cases and then rewrites the schedule to
4098 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4100 * In the new schedule, the schedule domains have the same offset (modulo
4101 * the stride), ensuring that the union of schedule domains is also strided.
4104 * If there is only a single domain in the component, then there is
4105 * nothing to do. Similarly, if the current schedule dimension has
4106 * a fixed value for almost all domains then there is nothing to be done.
4107 * In particular, we need at least two domains where the current schedule
4108 * dimension does not have a fixed value.
4109 * Finally, in case of a schedule map input,
4110 * if any of the options refer to the current schedule dimension,
4111 * then we bail out as well. It would be possible to reformulate the options
4112 * in terms of the new schedule domain, but that would introduce constraints
4113 * that separate the domains in the options and that is something we would
4114 * like to avoid.
4115 * In the case of a schedule tree input, we bail out if any of
4116 * the descendants of the current schedule node refer to outer
4117 * schedule nodes in any way.
4120 * To see if there is any shifted stride, we look at the differences
4121 * between the values of the current dimension in pairs of domains
4122 * for equal values of outer dimensions. These differences should be
4123 * of the form
4125 * m x + r
4127 * with "m" the stride and "r" a constant. Note that we cannot perform
4128 * this analysis on individual domains as the lower bound in each domain
4129 * may depend on parameters or outer dimensions and so the current dimension
4130 * itself may not have a fixed remainder on division by the stride.
4132 * In particular, we compare the first domain that does not have an
4133 * obviously fixed value for the current dimension to itself and all
4134 * other domains and collect the offsets and the gcd of the strides.
4135 * If the gcd becomes one, then we failed to find shifted strides.
4136 * If the gcd is zero, then the differences were all fixed, meaning
4137 * that some domains had non-obviously fixed values for the current dimension.
4138 * If all the offsets are the same (for those domains that do not have
4139 * an obviously fixed value for the current dimension), then we do not
4140 * apply the transformation.
4141 * If none of the domains were skipped, then there is nothing to do.
4142 * If some of them were skipped, then if we apply separation, the schedule
4143 * domain should get split in pieces with a (non-shifted) stride.
4145 * Otherwise, we apply a shift to expose the stride in
4146 * generate_shift_component.
4148 static __isl_give isl_ast_graft_list *generate_component(
4149 struct isl_set_map_pair *domain, int *order, int n,
4150 __isl_take isl_ast_build *build)
4152 int i, d;
4153 isl_size depth;
4154 isl_ctx *ctx;
4155 isl_map *map;
4156 isl_set *deltas;
4157 isl_val *gcd = NULL;
4158 isl_multi_val *mv;
4159 int fixed, skip;
4160 int base;
4161 isl_ast_graft_list *list;
4162 int res = 0;
4164 depth = isl_ast_build_get_depth(build);
4165 if (depth < 0)
4166 goto error;
4168 skip = n == 1;
4169 if (skip >= 0 && !skip)
4170 skip = at_most_one_non_fixed(domain, order, n, depth);
4171 if (skip >= 0 && !skip) {
4172 if (isl_ast_build_has_schedule_node(build))
4173 skip = has_anchored_subtree(build);
4174 else
4175 skip = isl_ast_build_options_involve_depth(build);
4177 if (skip < 0)
4178 goto error;
4179 if (skip)
4180 return generate_shifted_component_from_list(domain,
4181 order, n, build);
4183 base = eliminate_non_fixed(domain, order, n, depth, build);
4184 if (base < 0)
4185 goto error;
4187 ctx = isl_ast_build_get_ctx(build);
4189 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4191 fixed = 1;
4192 for (i = 0; i < n; ++i) {
4193 isl_val *r, *m;
4195 map = isl_map_from_domain_and_range(
4196 isl_set_copy(domain[order[base]].set),
4197 isl_set_copy(domain[order[i]].set));
4198 for (d = 0; d < depth; ++d)
4199 map = isl_map_equate(map, isl_dim_in, d,
4200 isl_dim_out, d);
4201 deltas = isl_map_deltas(map);
4202 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4203 isl_set_free(deltas);
4204 if (res < 0)
4205 break;
4207 if (i == 0)
4208 gcd = m;
4209 else
4210 gcd = isl_val_gcd(gcd, m);
4211 if (isl_val_is_one(gcd)) {
4212 isl_val_free(r);
4213 break;
4215 mv = isl_multi_val_set_val(mv, i, r);
4217 res = dim_is_fixed(domain[order[i]].set, depth);
4218 if (res < 0)
4219 break;
4220 if (res)
4221 continue;
4223 if (fixed && i > base) {
4224 isl_val *a, *b;
4225 a = isl_multi_val_get_val(mv, i);
4226 b = isl_multi_val_get_val(mv, base);
4227 if (isl_val_ne(a, b))
4228 fixed = 0;
4229 isl_val_free(a);
4230 isl_val_free(b);
4234 if (res < 0 || !gcd) {
4235 isl_ast_build_free(build);
4236 list = NULL;
4237 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4238 list = generate_shifted_component_from_list(domain,
4239 order, n, build);
4240 } else {
4241 list = generate_shift_component(domain, order, n, gcd, mv,
4242 build);
4245 isl_val_free(gcd);
4246 isl_multi_val_free(mv);
4248 return list;
4249 error:
4250 isl_ast_build_free(build);
4251 return NULL;
4254 /* Store both "map" itself and its domain in the
4255 * structure pointed to by *next and advance to the next array element.
4257 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4259 struct isl_set_map_pair **next = user;
4261 (*next)->map = isl_map_copy(map);
4262 (*next)->set = isl_map_domain(map);
4263 (*next)++;
4265 return isl_stat_ok;
4268 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4269 __isl_keep isl_schedule_node *node);
4271 /* Is any domain element of "umap" scheduled after any of
4272 * the corresponding image elements by the tree rooted at
4273 * the child of "node"?
4275 static isl_bool after_in_child(__isl_keep isl_union_map *umap,
4276 __isl_keep isl_schedule_node *node)
4278 isl_schedule_node *child;
4279 isl_bool after;
4281 child = isl_schedule_node_get_child(node, 0);
4282 after = after_in_tree(umap, child);
4283 isl_schedule_node_free(child);
4285 return after;
4288 /* Is any domain element of "umap" scheduled after any of
4289 * the corresponding image elements by the tree rooted at
4290 * the band node "node"?
4292 * We first check if any domain element is scheduled after any
4293 * of the corresponding image elements by the band node itself.
4294 * If not, we restrict "map" to those pairs of element that
4295 * are scheduled together by the band node and continue with
4296 * the child of the band node.
4297 * If there are no such pairs then the map passed to after_in_child
4298 * will be empty causing it to return 0.
4300 static isl_bool after_in_band(__isl_keep isl_union_map *umap,
4301 __isl_keep isl_schedule_node *node)
4303 isl_multi_union_pw_aff *mupa;
4304 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4305 isl_union_set *domain, *range;
4306 isl_space *space;
4307 isl_bool empty;
4308 isl_bool after;
4309 isl_size n;
4311 n = isl_schedule_node_band_n_member(node);
4312 if (n < 0)
4313 return isl_bool_error;
4314 if (n == 0)
4315 return after_in_child(umap, node);
4317 mupa = isl_schedule_node_band_get_partial_schedule(node);
4318 space = isl_multi_union_pw_aff_get_space(mupa);
4319 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4320 test = isl_union_map_copy(umap);
4321 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4322 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4323 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4324 test = isl_union_map_intersect(test, gt);
4325 empty = isl_union_map_is_empty(test);
4326 isl_union_map_free(test);
4328 if (empty < 0 || !empty) {
4329 isl_union_map_free(partial);
4330 return isl_bool_not(empty);
4333 universe = isl_union_map_universe(isl_union_map_copy(umap));
4334 domain = isl_union_map_domain(isl_union_map_copy(universe));
4335 range = isl_union_map_range(universe);
4336 umap1 = isl_union_map_copy(partial);
4337 umap1 = isl_union_map_intersect_domain(umap1, domain);
4338 umap2 = isl_union_map_intersect_domain(partial, range);
4339 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4340 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4341 after = after_in_child(test, node);
4342 isl_union_map_free(test);
4343 return after;
4346 /* Is any domain element of "umap" scheduled after any of
4347 * the corresponding image elements by the tree rooted at
4348 * the context node "node"?
4350 * The context constraints apply to the schedule domain,
4351 * so we cannot apply them directly to "umap", which contains
4352 * pairs of statement instances. Instead, we add them
4353 * to the range of the prefix schedule for both domain and
4354 * range of "umap".
4356 static isl_bool after_in_context(__isl_keep isl_union_map *umap,
4357 __isl_keep isl_schedule_node *node)
4359 isl_union_map *prefix, *universe, *umap1, *umap2;
4360 isl_union_set *domain, *range;
4361 isl_set *context;
4362 isl_bool after;
4364 umap = isl_union_map_copy(umap);
4365 context = isl_schedule_node_context_get_context(node);
4366 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4367 universe = isl_union_map_universe(isl_union_map_copy(umap));
4368 domain = isl_union_map_domain(isl_union_map_copy(universe));
4369 range = isl_union_map_range(universe);
4370 umap1 = isl_union_map_copy(prefix);
4371 umap1 = isl_union_map_intersect_domain(umap1, domain);
4372 umap2 = isl_union_map_intersect_domain(prefix, range);
4373 umap1 = isl_union_map_intersect_range(umap1,
4374 isl_union_set_from_set(context));
4375 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4376 umap = isl_union_map_intersect(umap, umap1);
4378 after = after_in_child(umap, node);
4380 isl_union_map_free(umap);
4382 return after;
4385 /* Is any domain element of "umap" scheduled after any of
4386 * the corresponding image elements by the tree rooted at
4387 * the expansion node "node"?
4389 * We apply the expansion to domain and range of "umap" and
4390 * continue with its child.
4392 static isl_bool after_in_expansion(__isl_keep isl_union_map *umap,
4393 __isl_keep isl_schedule_node *node)
4395 isl_union_map *expansion;
4396 isl_bool after;
4398 expansion = isl_schedule_node_expansion_get_expansion(node);
4399 umap = isl_union_map_copy(umap);
4400 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4401 umap = isl_union_map_apply_range(umap, expansion);
4403 after = after_in_child(umap, node);
4405 isl_union_map_free(umap);
4407 return after;
4410 /* Is any domain element of "umap" scheduled after any of
4411 * the corresponding image elements by the tree rooted at
4412 * the extension node "node"?
4414 * Since the extension node may add statement instances before or
4415 * after the pairs of statement instances in "umap", we return isl_bool_true
4416 * to ensure that these pairs are not broken up.
4418 static isl_bool after_in_extension(__isl_keep isl_union_map *umap,
4419 __isl_keep isl_schedule_node *node)
4421 return isl_bool_true;
4424 /* Is any domain element of "umap" scheduled after any of
4425 * the corresponding image elements by the tree rooted at
4426 * the filter node "node"?
4428 * We intersect domain and range of "umap" with the filter and
4429 * continue with its child.
4431 static isl_bool after_in_filter(__isl_keep isl_union_map *umap,
4432 __isl_keep isl_schedule_node *node)
4434 isl_union_set *filter;
4435 isl_bool after;
4437 umap = isl_union_map_copy(umap);
4438 filter = isl_schedule_node_filter_get_filter(node);
4439 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4440 umap = isl_union_map_intersect_range(umap, filter);
4442 after = after_in_child(umap, node);
4444 isl_union_map_free(umap);
4446 return after;
4449 /* Is any domain element of "umap" scheduled after any of
4450 * the corresponding image elements by the tree rooted at
4451 * the set node "node"?
4453 * This is only the case if this condition holds in any
4454 * of the (filter) children of the set node.
4455 * In particular, if the domain and the range of "umap"
4456 * are contained in different children, then the condition
4457 * does not hold.
4459 static isl_bool after_in_set(__isl_keep isl_union_map *umap,
4460 __isl_keep isl_schedule_node *node)
4462 int i;
4463 isl_size n;
4465 n = isl_schedule_node_n_children(node);
4466 if (n < 0)
4467 return isl_bool_error;
4468 for (i = 0; i < n; ++i) {
4469 isl_schedule_node *child;
4470 isl_bool after;
4472 child = isl_schedule_node_get_child(node, i);
4473 after = after_in_tree(umap, child);
4474 isl_schedule_node_free(child);
4476 if (after < 0 || after)
4477 return after;
4480 return isl_bool_false;
4483 /* Return the filter of child "i" of "node".
4485 static __isl_give isl_union_set *child_filter(
4486 __isl_keep isl_schedule_node *node, int i)
4488 isl_schedule_node *child;
4489 isl_union_set *filter;
4491 child = isl_schedule_node_get_child(node, i);
4492 filter = isl_schedule_node_filter_get_filter(child);
4493 isl_schedule_node_free(child);
4495 return filter;
4498 /* Is any domain element of "umap" scheduled after any of
4499 * the corresponding image elements by the tree rooted at
4500 * the sequence node "node"?
4502 * This happens in particular if any domain element is
4503 * contained in a later child than one containing a range element or
4504 * if the condition holds within a given child in the sequence.
4505 * The later part of the condition is checked by after_in_set.
4507 static isl_bool after_in_sequence(__isl_keep isl_union_map *umap,
4508 __isl_keep isl_schedule_node *node)
4510 int i, j;
4511 isl_size n;
4512 isl_union_map *umap_i;
4513 isl_bool empty;
4514 isl_bool after = isl_bool_false;
4516 n = isl_schedule_node_n_children(node);
4517 if (n < 0)
4518 return isl_bool_error;
4519 for (i = 1; i < n; ++i) {
4520 isl_union_set *filter_i;
4522 umap_i = isl_union_map_copy(umap);
4523 filter_i = child_filter(node, i);
4524 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4525 empty = isl_union_map_is_empty(umap_i);
4526 if (empty < 0)
4527 goto error;
4528 if (empty) {
4529 isl_union_map_free(umap_i);
4530 continue;
4533 for (j = 0; j < i; ++j) {
4534 isl_union_set *filter_j;
4535 isl_union_map *umap_ij;
4537 umap_ij = isl_union_map_copy(umap_i);
4538 filter_j = child_filter(node, j);
4539 umap_ij = isl_union_map_intersect_range(umap_ij,
4540 filter_j);
4541 empty = isl_union_map_is_empty(umap_ij);
4542 isl_union_map_free(umap_ij);
4544 if (empty < 0)
4545 goto error;
4546 if (!empty)
4547 after = isl_bool_true;
4548 if (after)
4549 break;
4552 isl_union_map_free(umap_i);
4553 if (after)
4554 break;
4557 if (after < 0 || after)
4558 return after;
4560 return after_in_set(umap, node);
4561 error:
4562 isl_union_map_free(umap_i);
4563 return isl_bool_error;
4566 /* Is any domain element of "umap" scheduled after any of
4567 * the corresponding image elements by the tree rooted at "node"?
4569 * If "umap" is empty, then clearly there is no such element.
4570 * Otherwise, consider the different types of nodes separately.
4572 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4573 __isl_keep isl_schedule_node *node)
4575 isl_bool empty;
4576 enum isl_schedule_node_type type;
4578 empty = isl_union_map_is_empty(umap);
4579 if (empty < 0)
4580 return isl_bool_error;
4581 if (empty)
4582 return isl_bool_false;
4583 if (!node)
4584 return isl_bool_error;
4586 type = isl_schedule_node_get_type(node);
4587 switch (type) {
4588 case isl_schedule_node_error:
4589 return isl_bool_error;
4590 case isl_schedule_node_leaf:
4591 return isl_bool_false;
4592 case isl_schedule_node_band:
4593 return after_in_band(umap, node);
4594 case isl_schedule_node_domain:
4595 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4596 "unexpected internal domain node",
4597 return isl_bool_error);
4598 case isl_schedule_node_context:
4599 return after_in_context(umap, node);
4600 case isl_schedule_node_expansion:
4601 return after_in_expansion(umap, node);
4602 case isl_schedule_node_extension:
4603 return after_in_extension(umap, node);
4604 case isl_schedule_node_filter:
4605 return after_in_filter(umap, node);
4606 case isl_schedule_node_guard:
4607 case isl_schedule_node_mark:
4608 return after_in_child(umap, node);
4609 case isl_schedule_node_set:
4610 return after_in_set(umap, node);
4611 case isl_schedule_node_sequence:
4612 return after_in_sequence(umap, node);
4615 return isl_bool_true;
4618 /* Is any domain element of "map1" scheduled after any domain
4619 * element of "map2" by the subtree underneath the current band node,
4620 * while at the same time being scheduled together by the current
4621 * band node, i.e., by "map1" and "map2?
4623 * If the child of the current band node is a leaf, then
4624 * no element can be scheduled after any other element.
4626 * Otherwise, we construct a relation between domain elements
4627 * of "map1" and domain elements of "map2" that are scheduled
4628 * together and then check if the subtree underneath the current
4629 * band node determines their relative order.
4631 static isl_bool after_in_subtree(__isl_keep isl_ast_build *build,
4632 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4634 isl_schedule_node *node;
4635 isl_map *map;
4636 isl_union_map *umap;
4637 isl_bool after;
4639 node = isl_ast_build_get_schedule_node(build);
4640 if (!node)
4641 return isl_bool_error;
4642 node = isl_schedule_node_child(node, 0);
4643 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4644 isl_schedule_node_free(node);
4645 return isl_bool_false;
4647 map = isl_map_copy(map2);
4648 map = isl_map_apply_domain(map, isl_map_copy(map1));
4649 umap = isl_union_map_from_map(map);
4650 after = after_in_tree(umap, node);
4651 isl_union_map_free(umap);
4652 isl_schedule_node_free(node);
4653 return after;
4656 /* Internal data for any_scheduled_after.
4658 * "build" is the build in which the AST is constructed.
4659 * "depth" is the number of loops that have already been generated
4660 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4661 * "domain" is an array of set-map pairs corresponding to the different
4662 * iteration domains. The set is the schedule domain, i.e., the domain
4663 * of the inverse schedule, while the map is the inverse schedule itself.
4665 struct isl_any_scheduled_after_data {
4666 isl_ast_build *build;
4667 int depth;
4668 int group_coscheduled;
4669 struct isl_set_map_pair *domain;
4672 /* Is any element of domain "i" scheduled after any element of domain "j"
4673 * (for a common iteration of the first data->depth loops)?
4675 * data->domain[i].set contains the domain of the inverse schedule
4676 * for domain "i", i.e., elements in the schedule domain.
4678 * If we are inside a band of a schedule tree and there is a pair
4679 * of elements in the two domains that is schedule together by
4680 * the current band, then we check if any element of "i" may be schedule
4681 * after element of "j" by the descendants of the band node.
4683 * If data->group_coscheduled is set, then we also return 1 if there
4684 * is any pair of elements in the two domains that are scheduled together.
4686 static isl_bool any_scheduled_after(int i, int j, void *user)
4688 struct isl_any_scheduled_after_data *data = user;
4689 isl_size dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4690 int pos;
4692 if (dim < 0)
4693 return isl_bool_error;
4695 for (pos = data->depth; pos < dim; ++pos) {
4696 int follows;
4698 follows = isl_set_follows_at(data->domain[i].set,
4699 data->domain[j].set, pos);
4701 if (follows < -1)
4702 return isl_bool_error;
4703 if (follows > 0)
4704 return isl_bool_true;
4705 if (follows < 0)
4706 return isl_bool_false;
4709 if (isl_ast_build_has_schedule_node(data->build)) {
4710 isl_bool after;
4712 after = after_in_subtree(data->build, data->domain[i].map,
4713 data->domain[j].map);
4714 if (after < 0 || after)
4715 return after;
4718 return isl_bool_ok(data->group_coscheduled);
4721 /* Look for independent components at the current depth and generate code
4722 * for each component separately. The resulting lists of grafts are
4723 * merged in an attempt to combine grafts with identical guards.
4725 * Code for two domains can be generated separately if all the elements
4726 * of one domain are scheduled before (or together with) all the elements
4727 * of the other domain. We therefore consider the graph with as nodes
4728 * the domains and an edge between two nodes if any element of the first
4729 * node is scheduled after any element of the second node.
4730 * If the ast_build_group_coscheduled is set, then we also add an edge if
4731 * there is any pair of elements in the two domains that are scheduled
4732 * together.
4733 * Code is then generated (by generate_component)
4734 * for each of the strongly connected components in this graph
4735 * in their topological order.
4737 * Since the test is performed on the domain of the inverse schedules of
4738 * the different domains, we precompute these domains and store
4739 * them in data.domain.
4741 static __isl_give isl_ast_graft_list *generate_components(
4742 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4744 int i;
4745 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4746 isl_size n = isl_union_map_n_map(executed);
4747 isl_size depth;
4748 struct isl_any_scheduled_after_data data;
4749 struct isl_set_map_pair *next;
4750 struct isl_tarjan_graph *g = NULL;
4751 isl_ast_graft_list *list = NULL;
4752 int n_domain = 0;
4754 data.domain = NULL;
4755 if (n < 0)
4756 goto error;
4757 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4758 if (!data.domain)
4759 goto error;
4760 n_domain = n;
4762 next = data.domain;
4763 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4764 goto error;
4766 depth = isl_ast_build_get_depth(build);
4767 if (depth < 0)
4768 goto error;
4769 data.build = build;
4770 data.depth = depth;
4771 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4772 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4773 if (!g)
4774 goto error;
4776 list = isl_ast_graft_list_alloc(ctx, 0);
4778 i = 0;
4779 while (list && n) {
4780 isl_ast_graft_list *list_c;
4781 int first = i;
4783 if (g->order[i] == -1)
4784 isl_die(ctx, isl_error_internal, "cannot happen",
4785 goto error);
4786 ++i; --n;
4787 while (g->order[i] != -1) {
4788 ++i; --n;
4791 list_c = generate_component(data.domain,
4792 g->order + first, i - first,
4793 isl_ast_build_copy(build));
4794 list = isl_ast_graft_list_merge(list, list_c, build);
4796 ++i;
4799 if (0)
4800 error: list = isl_ast_graft_list_free(list);
4801 isl_tarjan_graph_free(g);
4802 for (i = 0; i < n_domain; ++i) {
4803 isl_map_free(data.domain[i].map);
4804 isl_set_free(data.domain[i].set);
4806 free(data.domain);
4807 isl_union_map_free(executed);
4808 isl_ast_build_free(build);
4810 return list;
4813 /* Generate code for the next level (and all inner levels).
4815 * If "executed" is empty, i.e., no code needs to be generated,
4816 * then we return an empty list.
4818 * If we have already generated code for all loop levels, then we pass
4819 * control to generate_inner_level.
4821 * If "executed" lives in a single space, i.e., if code needs to be
4822 * generated for a single domain, then there can only be a single
4823 * component and we go directly to generate_shifted_component.
4824 * Otherwise, we call generate_components to detect the components
4825 * and to call generate_component on each of them separately.
4827 static __isl_give isl_ast_graft_list *generate_next_level(
4828 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4830 isl_size depth;
4831 isl_size dim;
4832 isl_size n;
4834 if (!build || !executed)
4835 goto error;
4837 if (isl_union_map_is_empty(executed)) {
4838 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4839 isl_union_map_free(executed);
4840 isl_ast_build_free(build);
4841 return isl_ast_graft_list_alloc(ctx, 0);
4844 depth = isl_ast_build_get_depth(build);
4845 dim = isl_ast_build_dim(build, isl_dim_set);
4846 if (depth < 0 || dim < 0)
4847 goto error;
4848 if (depth >= dim)
4849 return generate_inner_level(executed, build);
4851 n = isl_union_map_n_map(executed);
4852 if (n < 0)
4853 goto error;
4854 if (n == 1)
4855 return generate_shifted_component(executed, build);
4857 return generate_components(executed, build);
4858 error:
4859 isl_union_map_free(executed);
4860 isl_ast_build_free(build);
4861 return NULL;
4864 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4865 * internal, executed and build are the inputs to generate_code.
4866 * list collects the output.
4868 struct isl_generate_code_data {
4869 int internal;
4870 isl_union_map *executed;
4871 isl_ast_build *build;
4873 isl_ast_graft_list *list;
4876 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4878 * [E -> S] -> D
4880 * with E the external build schedule and S the additional schedule "space",
4881 * reformulate the inverse schedule in terms of the internal schedule domain,
4882 * i.e., return
4884 * [I -> S] -> D
4886 * We first obtain a mapping
4888 * I -> E
4890 * take the inverse and the product with S -> S, resulting in
4892 * [I -> S] -> [E -> S]
4894 * Applying the map to the input produces the desired result.
4896 static __isl_give isl_union_map *internal_executed(
4897 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4898 __isl_keep isl_ast_build *build)
4900 isl_map *id, *proj;
4902 proj = isl_ast_build_get_schedule_map(build);
4903 proj = isl_map_reverse(proj);
4904 space = isl_space_map_from_set(isl_space_copy(space));
4905 id = isl_map_identity(space);
4906 proj = isl_map_product(proj, id);
4907 executed = isl_union_map_apply_domain(executed,
4908 isl_union_map_from_map(proj));
4909 return executed;
4912 /* Generate an AST that visits the elements in the range of data->executed
4913 * in the relative order specified by the corresponding domain element(s)
4914 * for those domain elements that belong to "set".
4915 * Add the result to data->list.
4917 * The caller ensures that "set" is a universe domain.
4918 * "space" is the space of the additional part of the schedule.
4919 * It is equal to the space of "set" if build->domain is parametric.
4920 * Otherwise, it is equal to the range of the wrapped space of "set".
4922 * If the build space is not parametric and
4923 * if isl_ast_build_node_from_schedule_map
4924 * was called from an outside user (data->internal not set), then
4925 * the (inverse) schedule refers to the external build domain and needs to
4926 * be transformed to refer to the internal build domain.
4928 * If the build space is parametric, then we add some of the parameter
4929 * constraints to the executed relation. Adding these constraints
4930 * allows for an earlier detection of conflicts in some cases.
4931 * However, we do not want to divide the executed relation into
4932 * more disjuncts than necessary. We therefore approximate
4933 * the constraints on the parameters by a single disjunct set.
4935 * The build is extended to include the additional part of the schedule.
4936 * If the original build space was not parametric, then the options
4937 * in data->build refer only to the additional part of the schedule
4938 * and they need to be adjusted to refer to the complete AST build
4939 * domain.
4941 * After having adjusted inverse schedule and build, we start generating
4942 * code with the outer loop of the current code generation
4943 * in generate_next_level.
4945 * If the original build space was not parametric, we undo the embedding
4946 * on the resulting isl_ast_node_list so that it can be used within
4947 * the outer AST build.
4949 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4950 __isl_take isl_set *set, __isl_take isl_space *space)
4952 isl_union_map *executed;
4953 isl_ast_build *build;
4954 isl_ast_graft_list *list;
4955 int embed;
4957 executed = isl_union_map_copy(data->executed);
4958 executed = isl_union_map_intersect_domain(executed,
4959 isl_union_set_from_set(set));
4961 embed = !isl_set_is_params(data->build->domain);
4962 if (embed && !data->internal)
4963 executed = internal_executed(executed, space, data->build);
4964 if (!embed) {
4965 isl_set *domain;
4966 domain = isl_ast_build_get_domain(data->build);
4967 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4968 executed = isl_union_map_intersect_params(executed, domain);
4971 build = isl_ast_build_copy(data->build);
4972 build = isl_ast_build_product(build, space);
4974 list = generate_next_level(executed, build);
4976 list = isl_ast_graft_list_unembed(list, embed);
4978 data->list = isl_ast_graft_list_concat(data->list, list);
4980 return isl_stat_ok;
4983 /* Generate an AST that visits the elements in the range of data->executed
4984 * in the relative order specified by the corresponding domain element(s)
4985 * for those domain elements that belong to "set".
4986 * Add the result to data->list.
4988 * The caller ensures that "set" is a universe domain.
4990 * If the build space S is not parametric, then the space of "set"
4991 * need to be a wrapped relation with S as domain. That is, it needs
4992 * to be of the form
4994 * [S -> T]
4996 * Check this property and pass control to generate_code_in_space
4997 * passing along T.
4998 * If the build space is not parametric, then T is the space of "set".
5000 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
5002 struct isl_generate_code_data *data = user;
5003 isl_space *space, *build_space;
5004 int is_domain;
5006 space = isl_set_get_space(set);
5008 if (isl_set_is_params(data->build->domain))
5009 return generate_code_in_space(data, set, space);
5011 build_space = isl_ast_build_get_space(data->build, data->internal);
5012 space = isl_space_unwrap(space);
5013 is_domain = isl_space_is_domain(build_space, space);
5014 isl_space_free(build_space);
5015 space = isl_space_range(space);
5017 if (is_domain < 0)
5018 goto error;
5019 if (!is_domain)
5020 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5021 "invalid nested schedule space", goto error);
5023 return generate_code_in_space(data, set, space);
5024 error:
5025 isl_set_free(set);
5026 isl_space_free(space);
5027 return isl_stat_error;
5030 /* Generate an AST that visits the elements in the range of "executed"
5031 * in the relative order specified by the corresponding domain element(s).
5033 * "build" is an isl_ast_build that has either been constructed by
5034 * isl_ast_build_from_context or passed to a callback set by
5035 * isl_ast_build_set_create_leaf.
5036 * In the first case, the space of the isl_ast_build is typically
5037 * a parametric space, although this is currently not enforced.
5038 * In the second case, the space is never a parametric space.
5039 * If the space S is not parametric, then the domain space(s) of "executed"
5040 * need to be wrapped relations with S as domain.
5042 * If the domain of "executed" consists of several spaces, then an AST
5043 * is generated for each of them (in arbitrary order) and the results
5044 * are concatenated.
5046 * If "internal" is set, then the domain "S" above refers to the internal
5047 * schedule domain representation. Otherwise, it refers to the external
5048 * representation, as returned by isl_ast_build_get_schedule_space.
5050 * We essentially run over all the spaces in the domain of "executed"
5051 * and call generate_code_set on each of them.
5053 static __isl_give isl_ast_graft_list *generate_code(
5054 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
5055 int internal)
5057 isl_ctx *ctx;
5058 struct isl_generate_code_data data = { 0 };
5059 isl_space *space;
5060 isl_union_set *schedule_domain;
5061 isl_union_map *universe;
5063 if (!build)
5064 goto error;
5065 space = isl_ast_build_get_space(build, 1);
5066 space = isl_space_align_params(space,
5067 isl_union_map_get_space(executed));
5068 space = isl_space_align_params(space,
5069 isl_union_map_get_space(build->options));
5070 build = isl_ast_build_align_params(build, isl_space_copy(space));
5071 executed = isl_union_map_align_params(executed, space);
5072 if (!executed || !build)
5073 goto error;
5075 ctx = isl_ast_build_get_ctx(build);
5077 data.internal = internal;
5078 data.executed = executed;
5079 data.build = build;
5080 data.list = isl_ast_graft_list_alloc(ctx, 0);
5082 universe = isl_union_map_universe(isl_union_map_copy(executed));
5083 schedule_domain = isl_union_map_domain(universe);
5084 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5085 &data) < 0)
5086 data.list = isl_ast_graft_list_free(data.list);
5088 isl_union_set_free(schedule_domain);
5089 isl_union_map_free(executed);
5091 isl_ast_build_free(build);
5092 return data.list;
5093 error:
5094 isl_union_map_free(executed);
5095 isl_ast_build_free(build);
5096 return NULL;
5099 /* Generate an AST that visits the elements in the domain of "schedule"
5100 * in the relative order specified by the corresponding image element(s).
5102 * "build" is an isl_ast_build that has either been constructed by
5103 * isl_ast_build_from_context or passed to a callback set by
5104 * isl_ast_build_set_create_leaf.
5105 * In the first case, the space of the isl_ast_build is typically
5106 * a parametric space, although this is currently not enforced.
5107 * In the second case, the space is never a parametric space.
5108 * If the space S is not parametric, then the range space(s) of "schedule"
5109 * need to be wrapped relations with S as domain.
5111 * If the range of "schedule" consists of several spaces, then an AST
5112 * is generated for each of them (in arbitrary order) and the results
5113 * are concatenated.
5115 * We first initialize the local copies of the relevant options.
5116 * We do this here rather than when the isl_ast_build is created
5117 * because the options may have changed between the construction
5118 * of the isl_ast_build and the call to isl_generate_code.
5120 * The main computation is performed on an inverse schedule (with
5121 * the schedule domain in the domain and the elements to be executed
5122 * in the range) called "executed".
5124 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5125 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5127 isl_ast_graft_list *list;
5128 isl_ast_node *node;
5129 isl_union_map *executed;
5131 schedule = isl_union_map_coalesce(schedule);
5132 schedule = isl_union_map_remove_redundancies(schedule);
5133 executed = isl_union_map_reverse(schedule);
5134 list = generate_code(executed, isl_ast_build_copy(build), 0);
5135 node = isl_ast_node_from_graft_list(list, build);
5137 return node;
5140 /* The old name for isl_ast_build_node_from_schedule_map.
5141 * It is being kept for backward compatibility, but
5142 * it will be removed in the future.
5144 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5145 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5147 return isl_ast_build_node_from_schedule_map(build, schedule);
5150 /* Generate an AST that visits the elements in the domain of "executed"
5151 * in the relative order specified by the leaf node "node".
5153 * The relation "executed" maps the outer generated loop iterators
5154 * to the domain elements executed by those iterations.
5156 * Simply pass control to generate_inner_level.
5157 * Note that the current build does not refer to any band node, so
5158 * that generate_inner_level will not try to visit the child of
5159 * the leaf node.
5161 * If multiple statement instances reach a leaf,
5162 * then they can be executed in any order.
5163 * Group the list of grafts based on shared guards
5164 * such that identical guards are only generated once
5165 * when the list is eventually passed on to isl_ast_graft_list_fuse.
5167 static __isl_give isl_ast_graft_list *build_ast_from_leaf(
5168 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5169 __isl_take isl_union_map *executed)
5171 isl_ast_graft_list *list;
5173 isl_schedule_node_free(node);
5174 list = generate_inner_level(executed, isl_ast_build_copy(build));
5175 list = isl_ast_graft_list_group_on_guard(list, build);
5176 isl_ast_build_free(build);
5178 return list;
5181 /* Check that the band partial schedule "partial" does not filter out
5182 * any statement instances, as specified by the range of "executed".
5184 static isl_stat check_band_schedule_total_on_instances(
5185 __isl_keep isl_multi_union_pw_aff *partial,
5186 __isl_keep isl_union_map *executed)
5188 isl_bool subset;
5189 isl_union_set *domain, *instances;
5191 instances = isl_union_map_range(isl_union_map_copy(executed));
5192 partial = isl_multi_union_pw_aff_copy(partial);
5193 domain = isl_multi_union_pw_aff_domain(partial);
5194 subset = isl_union_set_is_subset(instances, domain);
5195 isl_union_set_free(domain);
5196 isl_union_set_free(instances);
5198 if (subset < 0)
5199 return isl_stat_error;
5200 if (!subset)
5201 isl_die(isl_union_map_get_ctx(executed), isl_error_invalid,
5202 "band node is not allowed to drop statement instances",
5203 return isl_stat_error);
5204 return isl_stat_ok;
5207 /* Generate an AST that visits the elements in the domain of "executed"
5208 * in the relative order specified by the band node "node" and its descendants.
5210 * The relation "executed" maps the outer generated loop iterators
5211 * to the domain elements executed by those iterations.
5213 * If the band is empty, we continue with its descendants.
5214 * Otherwise, we extend the build and the inverse schedule with
5215 * the additional space/partial schedule and continue generating
5216 * an AST in generate_next_level.
5217 * As soon as we have extended the inverse schedule with the additional
5218 * partial schedule, we look for equalities that may exists between
5219 * the old and the new part.
5221 static __isl_give isl_ast_graft_list *build_ast_from_band(
5222 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5223 __isl_take isl_union_map *executed)
5225 isl_space *space;
5226 isl_multi_union_pw_aff *extra;
5227 isl_union_map *extra_umap;
5228 isl_ast_graft_list *list;
5229 isl_size n1, n2;
5230 isl_size n;
5232 n = isl_schedule_node_band_n_member(node);
5233 if (!build || n < 0 || !executed)
5234 goto error;
5236 if (n == 0)
5237 return build_ast_from_child(build, node, executed);
5239 extra = isl_schedule_node_band_get_partial_schedule(node);
5240 extra = isl_multi_union_pw_aff_align_params(extra,
5241 isl_ast_build_get_space(build, 1));
5242 space = isl_multi_union_pw_aff_get_space(extra);
5244 if (check_band_schedule_total_on_instances(extra, executed) < 0)
5245 executed = isl_union_map_free(executed);
5247 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5248 extra_umap = isl_union_map_reverse(extra_umap);
5250 executed = isl_union_map_domain_product(executed, extra_umap);
5251 executed = isl_union_map_detect_equalities(executed);
5253 n1 = isl_ast_build_dim(build, isl_dim_param);
5254 build = isl_ast_build_product(build, space);
5255 n2 = isl_ast_build_dim(build, isl_dim_param);
5256 if (n1 < 0 || n2 < 0)
5257 build = isl_ast_build_free(build);
5258 else if (n2 > n1)
5259 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5260 "band node is not allowed to introduce new parameters",
5261 build = isl_ast_build_free(build));
5262 build = isl_ast_build_set_schedule_node(build, node);
5264 list = generate_next_level(executed, build);
5266 list = isl_ast_graft_list_unembed(list, 1);
5268 return list;
5269 error:
5270 isl_schedule_node_free(node);
5271 isl_union_map_free(executed);
5272 isl_ast_build_free(build);
5273 return NULL;
5276 /* Hoist a list of grafts (in practice containing a single graft)
5277 * from "sub_build" (which includes extra context information)
5278 * to "build".
5280 * In particular, project out all additional parameters introduced
5281 * by the context node from the enforced constraints and the guard
5282 * of the single graft.
5284 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5285 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5286 __isl_keep isl_ast_build *sub_build)
5288 isl_ast_graft *graft;
5289 isl_basic_set *enforced;
5290 isl_set *guard;
5291 isl_size n_param, extra_param;
5293 n_param = isl_ast_build_dim(build, isl_dim_param);
5294 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5295 if (n_param < 0 || extra_param < 0)
5296 return isl_ast_graft_list_free(list);
5298 if (extra_param == n_param)
5299 return list;
5301 extra_param -= n_param;
5302 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5303 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5304 n_param, extra_param);
5305 enforced = isl_basic_set_remove_unknown_divs(enforced);
5306 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5307 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5308 n_param, extra_param);
5309 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5310 guard = isl_set_compute_divs(guard);
5311 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5312 build, sub_build);
5313 list = isl_ast_graft_list_from_ast_graft(graft);
5315 return list;
5318 /* Generate an AST that visits the elements in the domain of "executed"
5319 * in the relative order specified by the context node "node"
5320 * and its descendants.
5322 * The relation "executed" maps the outer generated loop iterators
5323 * to the domain elements executed by those iterations.
5325 * The context node may introduce additional parameters as well as
5326 * constraints on the outer schedule dimensions or original parameters.
5328 * We add the extra parameters to a new build and the context
5329 * constraints to both the build and (as a single disjunct)
5330 * to the domain of "executed". Since the context constraints
5331 * are specified in terms of the input schedule, we first need
5332 * to map them to the internal schedule domain.
5334 * After constructing the AST from the descendants of "node",
5335 * we combine the list of grafts into a single graft within
5336 * the new build, in order to be able to exploit the additional
5337 * context constraints during this combination.
5339 * Additionally, if the current node is the outermost node in
5340 * the schedule tree (apart from the root domain node), we generate
5341 * all pending guards, again to be able to exploit the additional
5342 * context constraints. We currently do not do this for internal
5343 * context nodes since we may still want to hoist conditions
5344 * to outer AST nodes.
5346 * If the context node introduced any new parameters, then they
5347 * are removed from the set of enforced constraints and guard
5348 * in hoist_out_of_context.
5350 static __isl_give isl_ast_graft_list *build_ast_from_context(
5351 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5352 __isl_take isl_union_map *executed)
5354 isl_set *context;
5355 isl_space *space;
5356 isl_multi_aff *internal2input;
5357 isl_ast_build *sub_build;
5358 isl_ast_graft_list *list;
5359 isl_size n;
5360 isl_size depth;
5362 depth = isl_schedule_node_get_tree_depth(node);
5363 if (depth < 0)
5364 build = isl_ast_build_free(build);
5365 space = isl_ast_build_get_space(build, 1);
5366 context = isl_schedule_node_context_get_context(node);
5367 context = isl_set_align_params(context, space);
5368 sub_build = isl_ast_build_copy(build);
5369 space = isl_set_get_space(context);
5370 sub_build = isl_ast_build_align_params(sub_build, space);
5371 internal2input = isl_ast_build_get_internal2input(sub_build);
5372 context = isl_set_preimage_multi_aff(context, internal2input);
5373 sub_build = isl_ast_build_restrict_generated(sub_build,
5374 isl_set_copy(context));
5375 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5376 executed = isl_union_map_intersect_domain(executed,
5377 isl_union_set_from_set(context));
5379 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5380 node, executed);
5381 n = isl_ast_graft_list_n_ast_graft(list);
5382 if (n < 0)
5383 list = isl_ast_graft_list_free(list);
5385 list = isl_ast_graft_list_fuse(list, sub_build);
5386 if (depth == 1)
5387 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5388 sub_build);
5389 if (n >= 1)
5390 list = hoist_out_of_context(list, build, sub_build);
5392 isl_ast_build_free(build);
5393 isl_ast_build_free(sub_build);
5395 return list;
5398 /* Generate an AST that visits the elements in the domain of "executed"
5399 * in the relative order specified by the expansion node "node" and
5400 * its descendants.
5402 * The relation "executed" maps the outer generated loop iterators
5403 * to the domain elements executed by those iterations.
5405 * We expand the domain elements by the expansion and
5406 * continue with the descendants of the node.
5408 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5409 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5410 __isl_take isl_union_map *executed)
5412 isl_union_map *expansion;
5413 isl_size n1, n2;
5415 expansion = isl_schedule_node_expansion_get_expansion(node);
5416 expansion = isl_union_map_align_params(expansion,
5417 isl_union_map_get_space(executed));
5419 n1 = isl_union_map_dim(executed, isl_dim_param);
5420 executed = isl_union_map_apply_range(executed, expansion);
5421 n2 = isl_union_map_dim(executed, isl_dim_param);
5422 if (n1 < 0 || n2 < 0)
5423 goto error;
5424 if (n2 > n1)
5425 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5426 "expansion node is not allowed to introduce "
5427 "new parameters", goto error);
5429 return build_ast_from_child(build, node, executed);
5430 error:
5431 isl_ast_build_free(build);
5432 isl_schedule_node_free(node);
5433 isl_union_map_free(executed);
5434 return NULL;
5437 /* Generate an AST that visits the elements in the domain of "executed"
5438 * in the relative order specified by the extension node "node" and
5439 * its descendants.
5441 * The relation "executed" maps the outer generated loop iterators
5442 * to the domain elements executed by those iterations.
5444 * Extend the inverse schedule with the extension applied to current
5445 * set of generated constraints. Since the extension if formulated
5446 * in terms of the input schedule, it first needs to be transformed
5447 * to refer to the internal schedule.
5449 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5450 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5451 __isl_take isl_union_map *executed)
5453 isl_union_set *schedule_domain;
5454 isl_union_map *extension;
5455 isl_set *set;
5457 set = isl_ast_build_get_generated(build);
5458 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5459 schedule_domain = isl_union_set_from_set(set);
5461 extension = isl_schedule_node_extension_get_extension(node);
5463 extension = isl_union_map_preimage_domain_multi_aff(extension,
5464 isl_multi_aff_copy(build->internal2input));
5465 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5466 extension = isl_ast_build_substitute_values_union_map_domain(build,
5467 extension);
5468 executed = isl_union_map_union(executed, extension);
5470 return build_ast_from_child(build, node, executed);
5473 /* Generate an AST that visits the elements in the domain of "executed"
5474 * in the relative order specified by the filter node "node" and
5475 * its descendants.
5477 * The relation "executed" maps the outer generated loop iterators
5478 * to the domain elements executed by those iterations.
5480 * We simply intersect the iteration domain (i.e., the range of "executed")
5481 * with the filter and continue with the descendants of the node,
5482 * unless the resulting inverse schedule is empty, in which
5483 * case we return an empty list.
5485 * If the result of the intersection is equal to the original "executed"
5486 * relation, then keep the original representation since the intersection
5487 * may have unnecessarily broken up the relation into a greater number
5488 * of disjuncts.
5490 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5491 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5492 __isl_take isl_union_map *executed)
5494 isl_ctx *ctx;
5495 isl_union_set *filter;
5496 isl_union_map *orig;
5497 isl_ast_graft_list *list;
5498 int empty;
5499 isl_bool unchanged;
5500 isl_size n1, n2;
5502 orig = isl_union_map_copy(executed);
5503 if (!build || !node || !executed)
5504 goto error;
5506 filter = isl_schedule_node_filter_get_filter(node);
5507 filter = isl_union_set_align_params(filter,
5508 isl_union_map_get_space(executed));
5509 n1 = isl_union_map_dim(executed, isl_dim_param);
5510 executed = isl_union_map_intersect_range(executed, filter);
5511 n2 = isl_union_map_dim(executed, isl_dim_param);
5512 if (n1 < 0 || n2 < 0)
5513 goto error;
5514 if (n2 > n1)
5515 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5516 "filter node is not allowed to introduce "
5517 "new parameters", goto error);
5519 unchanged = isl_union_map_is_subset(orig, executed);
5520 empty = isl_union_map_is_empty(executed);
5521 if (unchanged < 0 || empty < 0)
5522 goto error;
5523 if (unchanged) {
5524 isl_union_map_free(executed);
5525 return build_ast_from_child(build, node, orig);
5527 isl_union_map_free(orig);
5528 if (!empty)
5529 return build_ast_from_child(build, node, executed);
5531 ctx = isl_ast_build_get_ctx(build);
5532 list = isl_ast_graft_list_alloc(ctx, 0);
5533 isl_ast_build_free(build);
5534 isl_schedule_node_free(node);
5535 isl_union_map_free(executed);
5536 return list;
5537 error:
5538 isl_ast_build_free(build);
5539 isl_schedule_node_free(node);
5540 isl_union_map_free(executed);
5541 isl_union_map_free(orig);
5542 return NULL;
5545 /* Generate an AST that visits the elements in the domain of "executed"
5546 * in the relative order specified by the guard node "node" and
5547 * its descendants.
5549 * The relation "executed" maps the outer generated loop iterators
5550 * to the domain elements executed by those iterations.
5552 * Ensure that the associated guard is enforced by the outer AST
5553 * constructs by adding it to the guard of the graft.
5554 * Since we know that we will enforce the guard, we can also include it
5555 * in the generated constraints used to construct an AST for
5556 * the descendant nodes.
5558 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5559 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5560 __isl_take isl_union_map *executed)
5562 isl_space *space;
5563 isl_set *guard, *hoisted;
5564 isl_basic_set *enforced;
5565 isl_ast_build *sub_build;
5566 isl_ast_graft *graft;
5567 isl_ast_graft_list *list;
5568 isl_size n1, n2, n;
5570 space = isl_ast_build_get_space(build, 1);
5571 guard = isl_schedule_node_guard_get_guard(node);
5572 n1 = isl_space_dim(space, isl_dim_param);
5573 guard = isl_set_align_params(guard, space);
5574 n2 = isl_set_dim(guard, isl_dim_param);
5575 if (n1 < 0 || n2 < 0)
5576 guard = isl_set_free(guard);
5577 else if (n2 > n1)
5578 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5579 "guard node is not allowed to introduce "
5580 "new parameters", guard = isl_set_free(guard));
5581 guard = isl_set_preimage_multi_aff(guard,
5582 isl_multi_aff_copy(build->internal2input));
5583 guard = isl_ast_build_specialize(build, guard);
5584 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5586 sub_build = isl_ast_build_copy(build);
5587 sub_build = isl_ast_build_restrict_generated(sub_build,
5588 isl_set_copy(guard));
5590 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5591 node, executed);
5593 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5594 n = isl_set_n_basic_set(hoisted);
5595 if (n < 0)
5596 list = isl_ast_graft_list_free(list);
5597 if (n > 1)
5598 list = isl_ast_graft_list_gist_guards(list,
5599 isl_set_copy(hoisted));
5600 guard = isl_set_intersect(guard, hoisted);
5601 enforced = extract_shared_enforced(list, build);
5602 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5603 build, sub_build);
5605 isl_ast_build_free(sub_build);
5606 isl_ast_build_free(build);
5607 return isl_ast_graft_list_from_ast_graft(graft);
5610 /* Call the before_each_mark callback, if requested by the user.
5612 * Return 0 on success and -1 on error.
5614 * The caller is responsible for recording the current inverse schedule
5615 * in "build".
5617 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5618 __isl_keep isl_ast_build *build)
5620 if (!build)
5621 return isl_stat_error;
5622 if (!build->before_each_mark)
5623 return isl_stat_ok;
5624 return build->before_each_mark(mark, build,
5625 build->before_each_mark_user);
5628 /* Call the after_each_mark callback, if requested by the user.
5630 * The caller is responsible for recording the current inverse schedule
5631 * in "build".
5633 static __isl_give isl_ast_graft *after_each_mark(
5634 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5636 if (!graft || !build)
5637 return isl_ast_graft_free(graft);
5638 if (!build->after_each_mark)
5639 return graft;
5640 graft->node = build->after_each_mark(graft->node, build,
5641 build->after_each_mark_user);
5642 if (!graft->node)
5643 return isl_ast_graft_free(graft);
5644 return graft;
5648 /* Generate an AST that visits the elements in the domain of "executed"
5649 * in the relative order specified by the mark node "node" and
5650 * its descendants.
5652 * The relation "executed" maps the outer generated loop iterators
5653 * to the domain elements executed by those iterations.
5655 * Since we may be calling before_each_mark and after_each_mark
5656 * callbacks, we record the current inverse schedule in the build.
5658 * We generate an AST for the child of the mark node, combine
5659 * the graft list into a single graft and then insert the mark
5660 * in the AST of that single graft.
5662 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5663 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5664 __isl_take isl_union_map *executed)
5666 isl_id *mark;
5667 isl_ast_graft *graft;
5668 isl_ast_graft_list *list;
5669 isl_size n;
5671 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5673 mark = isl_schedule_node_mark_get_id(node);
5674 if (before_each_mark(mark, build) < 0)
5675 node = isl_schedule_node_free(node);
5677 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5678 list = isl_ast_graft_list_fuse(list, build);
5679 n = isl_ast_graft_list_n_ast_graft(list);
5680 if (n < 0)
5681 list = isl_ast_graft_list_free(list);
5682 if (n == 0) {
5683 isl_id_free(mark);
5684 } else {
5685 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5686 graft = isl_ast_graft_insert_mark(graft, mark);
5687 graft = after_each_mark(graft, build);
5688 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5690 isl_ast_build_free(build);
5692 return list;
5695 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5696 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5697 __isl_take isl_union_map *executed);
5699 /* Generate an AST that visits the elements in the domain of "executed"
5700 * in the relative order specified by the sequence (or set) node "node" and
5701 * its descendants.
5703 * The relation "executed" maps the outer generated loop iterators
5704 * to the domain elements executed by those iterations.
5706 * We simply generate an AST for each of the children and concatenate
5707 * the results.
5709 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5710 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5711 __isl_take isl_union_map *executed)
5713 int i;
5714 isl_size n;
5715 isl_ctx *ctx;
5716 isl_ast_graft_list *list;
5718 ctx = isl_ast_build_get_ctx(build);
5719 list = isl_ast_graft_list_alloc(ctx, 0);
5721 n = isl_schedule_node_n_children(node);
5722 if (n < 0)
5723 list = isl_ast_graft_list_free(list);
5724 for (i = 0; i < n; ++i) {
5725 isl_schedule_node *child;
5726 isl_ast_graft_list *list_i;
5728 child = isl_schedule_node_get_child(node, i);
5729 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5730 child, isl_union_map_copy(executed));
5731 list = isl_ast_graft_list_concat(list, list_i);
5733 isl_ast_build_free(build);
5734 isl_schedule_node_free(node);
5735 isl_union_map_free(executed);
5737 return list;
5740 /* Generate an AST that visits the elements in the domain of "executed"
5741 * in the relative order specified by the node "node" and its descendants.
5743 * The relation "executed" maps the outer generated loop iterators
5744 * to the domain elements executed by those iterations.
5746 * The node types are handled in separate functions.
5747 * Set nodes are currently treated in the same way as sequence nodes.
5748 * The children of a set node may be executed in any order,
5749 * including the order of the children.
5751 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5752 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5753 __isl_take isl_union_map *executed)
5755 enum isl_schedule_node_type type;
5757 type = isl_schedule_node_get_type(node);
5759 switch (type) {
5760 case isl_schedule_node_error:
5761 goto error;
5762 case isl_schedule_node_leaf:
5763 return build_ast_from_leaf(build, node, executed);
5764 case isl_schedule_node_band:
5765 return build_ast_from_band(build, node, executed);
5766 case isl_schedule_node_context:
5767 return build_ast_from_context(build, node, executed);
5768 case isl_schedule_node_domain:
5769 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5770 "unexpected internal domain node", goto error);
5771 case isl_schedule_node_expansion:
5772 return build_ast_from_expansion(build, node, executed);
5773 case isl_schedule_node_extension:
5774 return build_ast_from_extension(build, node, executed);
5775 case isl_schedule_node_filter:
5776 return build_ast_from_filter(build, node, executed);
5777 case isl_schedule_node_guard:
5778 return build_ast_from_guard(build, node, executed);
5779 case isl_schedule_node_mark:
5780 return build_ast_from_mark(build, node, executed);
5781 case isl_schedule_node_sequence:
5782 case isl_schedule_node_set:
5783 return build_ast_from_sequence(build, node, executed);
5786 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5787 "unhandled type", goto error);
5788 error:
5789 isl_union_map_free(executed);
5790 isl_schedule_node_free(node);
5791 isl_ast_build_free(build);
5793 return NULL;
5796 /* Generate an AST that visits the elements in the domain of "executed"
5797 * in the relative order specified by the (single) child of "node" and
5798 * its descendants.
5800 * The relation "executed" maps the outer generated loop iterators
5801 * to the domain elements executed by those iterations.
5803 * This function is never called on a leaf, set or sequence node,
5804 * so the node always has exactly one child.
5806 static __isl_give isl_ast_graft_list *build_ast_from_child(
5807 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5808 __isl_take isl_union_map *executed)
5810 node = isl_schedule_node_child(node, 0);
5811 return build_ast_from_schedule_node(build, node, executed);
5814 /* Generate an AST that visits the elements in the domain of the domain
5815 * node "node" in the relative order specified by its descendants.
5817 * An initial inverse schedule is created that maps a zero-dimensional
5818 * schedule space to the node domain.
5819 * The input "build" is assumed to have a parametric domain and
5820 * is replaced by the same zero-dimensional schedule space.
5822 * We also add some of the parameter constraints in the build domain
5823 * to the executed relation. Adding these constraints
5824 * allows for an earlier detection of conflicts in some cases.
5825 * However, we do not want to divide the executed relation into
5826 * more disjuncts than necessary. We therefore approximate
5827 * the constraints on the parameters by a single disjunct set.
5829 static __isl_give isl_ast_node *build_ast_from_domain(
5830 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5832 isl_ctx *ctx;
5833 isl_union_set *domain, *schedule_domain;
5834 isl_union_map *executed;
5835 isl_space *space;
5836 isl_set *set;
5837 isl_ast_graft_list *list;
5838 isl_ast_node *ast;
5839 int is_params;
5841 if (!build)
5842 goto error;
5844 ctx = isl_ast_build_get_ctx(build);
5845 space = isl_ast_build_get_space(build, 1);
5846 is_params = isl_space_is_params(space);
5847 isl_space_free(space);
5848 if (is_params < 0)
5849 goto error;
5850 if (!is_params)
5851 isl_die(ctx, isl_error_unsupported,
5852 "expecting parametric initial context", goto error);
5854 domain = isl_schedule_node_domain_get_domain(node);
5855 domain = isl_union_set_coalesce(domain);
5857 space = isl_union_set_get_space(domain);
5858 space = isl_space_set_from_params(space);
5859 build = isl_ast_build_product(build, space);
5861 set = isl_ast_build_get_domain(build);
5862 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5863 schedule_domain = isl_union_set_from_set(set);
5865 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5866 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5867 ast = isl_ast_node_from_graft_list(list, build);
5868 isl_ast_build_free(build);
5870 return ast;
5871 error:
5872 isl_schedule_node_free(node);
5873 isl_ast_build_free(build);
5874 return NULL;
5877 /* Generate an AST that visits the elements in the domain of "schedule"
5878 * in the relative order specified by the schedule tree.
5880 * "build" is an isl_ast_build that has been created using
5881 * isl_ast_build_alloc or isl_ast_build_from_context based
5882 * on a parametric set.
5884 * The construction starts at the root node of the schedule,
5885 * which is assumed to be a domain node.
5887 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5888 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5890 isl_ctx *ctx;
5891 isl_schedule_node *node;
5893 if (!build || !schedule)
5894 goto error;
5896 ctx = isl_ast_build_get_ctx(build);
5898 node = isl_schedule_get_root(schedule);
5899 if (!node)
5900 goto error;
5901 isl_schedule_free(schedule);
5903 build = isl_ast_build_copy(build);
5904 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5905 isl_die(ctx, isl_error_unsupported,
5906 "expecting root domain node",
5907 build = isl_ast_build_free(build));
5908 return build_ast_from_domain(build, node);
5909 error:
5910 isl_schedule_free(schedule);
5911 return NULL;