isl_aff.c: union_pw_multi_aff_scale_multi_val_entry: improve error handling
[isl.git] / isl_ast_codegen.c
blobd337c4e4e2f6c6bc6c0a3a6fc13851caf6c80b62
1 /*
2 * Copyright 2012-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <limits.h>
14 #include <isl/id.h>
15 #include <isl/val.h>
16 #include <isl/space.h>
17 #include <isl/aff.h>
18 #include <isl/constraint.h>
19 #include <isl/set.h>
20 #include <isl/ilp.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/schedule_node.h>
24 #include <isl/options.h>
25 #include <isl_sort.h>
26 #include <isl_tarjan.h>
27 #include <isl_ast_private.h>
28 #include <isl_ast_build_expr.h>
29 #include <isl_ast_build_private.h>
30 #include <isl_ast_graft_private.h>
32 /* Try and reduce the number of disjuncts in the representation of "set",
33 * without dropping explicit representations of local variables.
35 static __isl_give isl_set *isl_set_coalesce_preserve(__isl_take isl_set *set)
37 isl_ctx *ctx;
38 int save_preserve;
40 if (!set)
41 return NULL;
43 ctx = isl_set_get_ctx(set);
44 save_preserve = isl_options_get_coalesce_preserve_locals(ctx);
45 isl_options_set_coalesce_preserve_locals(ctx, 1);
46 set = isl_set_coalesce(set);
47 isl_options_set_coalesce_preserve_locals(ctx, save_preserve);
48 return set;
51 /* Data used in generate_domain.
53 * "build" is the input build.
54 * "list" collects the results.
56 struct isl_generate_domain_data {
57 isl_ast_build *build;
59 isl_ast_graft_list *list;
62 static __isl_give isl_ast_graft_list *generate_next_level(
63 __isl_take isl_union_map *executed,
64 __isl_take isl_ast_build *build);
65 static __isl_give isl_ast_graft_list *generate_code(
66 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
67 int internal);
69 /* Generate an AST for a single domain based on
70 * the (non single valued) inverse schedule "executed".
72 * We extend the schedule with the iteration domain
73 * and continue generating through a call to generate_code.
75 * In particular, if executed has the form
77 * S -> D
79 * then we continue generating code on
81 * [S -> D] -> D
83 * The extended inverse schedule is clearly single valued
84 * ensuring that the nested generate_code will not reach this function,
85 * but will instead create calls to all elements of D that need
86 * to be executed from the current schedule domain.
88 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
89 struct isl_generate_domain_data *data)
91 isl_map *identity;
92 isl_ast_build *build;
93 isl_ast_graft_list *list;
95 build = isl_ast_build_copy(data->build);
97 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
98 executed = isl_map_domain_product(executed, identity);
99 build = isl_ast_build_set_single_valued(build, 1);
101 list = generate_code(isl_union_map_from_map(executed), build, 1);
103 data->list = isl_ast_graft_list_concat(data->list, list);
105 return isl_stat_ok;
108 /* Call the at_each_domain callback, if requested by the user,
109 * after recording the current inverse schedule in the build.
111 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
112 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
114 if (!graft || !build)
115 return isl_ast_graft_free(graft);
116 if (!build->at_each_domain)
117 return graft;
119 build = isl_ast_build_copy(build);
120 build = isl_ast_build_set_executed(build,
121 isl_union_map_from_map(isl_map_copy(executed)));
122 if (!build)
123 return isl_ast_graft_free(graft);
125 graft->node = build->at_each_domain(graft->node,
126 build, build->at_each_domain_user);
127 isl_ast_build_free(build);
129 if (!graft->node)
130 graft = isl_ast_graft_free(graft);
132 return graft;
135 /* Generate a call expression for the single executed
136 * domain element "map" and put a guard around it based its (simplified)
137 * domain. "executed" is the original inverse schedule from which "map"
138 * has been derived. In particular, "map" is either identical to "executed"
139 * or it is the result of gisting "executed" with respect to the build domain.
140 * "executed" is only used if there is an at_each_domain callback.
142 * At this stage, any pending constraints in the build can no longer
143 * be simplified with respect to any enforced constraints since
144 * the call node does not have any enforced constraints.
145 * Since all pending constraints not covered by any enforced constraints
146 * will be added as a guard to the graft in create_node_scaled,
147 * even in the eliminated case, the pending constraints
148 * can be considered to have been generated by outer constructs.
150 * If the user has set an at_each_domain callback, it is called
151 * on the constructed call expression node.
153 static isl_stat add_domain(__isl_take isl_map *executed,
154 __isl_take isl_map *map, struct isl_generate_domain_data *data)
156 isl_ast_build *build;
157 isl_ast_graft *graft;
158 isl_ast_graft_list *list;
159 isl_set *guard, *pending;
161 build = isl_ast_build_copy(data->build);
162 pending = isl_ast_build_get_pending(build);
163 build = isl_ast_build_replace_pending_by_guard(build, pending);
165 guard = isl_map_domain(isl_map_copy(map));
166 guard = isl_set_compute_divs(guard);
167 guard = isl_set_coalesce_preserve(guard);
168 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
169 guard = isl_ast_build_specialize(build, guard);
171 graft = isl_ast_graft_alloc_domain(map, build);
172 graft = at_each_domain(graft, executed, build);
173 isl_ast_build_free(build);
174 isl_map_free(executed);
175 graft = isl_ast_graft_add_guard(graft, guard, data->build);
177 list = isl_ast_graft_list_from_ast_graft(graft);
178 data->list = isl_ast_graft_list_concat(data->list, list);
180 return isl_stat_ok;
183 /* Generate an AST for a single domain based on
184 * the inverse schedule "executed" and add it to data->list.
186 * If there is more than one domain element associated to the current
187 * schedule "time", then we need to continue the generation process
188 * in generate_non_single_valued.
189 * Note that the inverse schedule being single-valued may depend
190 * on constraints that are only available in the original context
191 * domain specified by the user. We therefore first introduce
192 * some of the constraints of data->build->domain. In particular,
193 * we intersect with a single-disjunct approximation of this set.
194 * We perform this approximation to avoid further splitting up
195 * the executed relation, possibly introducing a disjunctive guard
196 * on the statement.
198 * On the other hand, we only perform the test after having taken the gist
199 * of the domain as the resulting map is the one from which the call
200 * expression is constructed. Using this map to construct the call
201 * expression usually yields simpler results in cases where the original
202 * map is not obviously single-valued.
203 * If the original map is obviously single-valued, then the gist
204 * operation is skipped.
206 * Because we perform the single-valuedness test on the gisted map,
207 * we may in rare cases fail to recognize that the inverse schedule
208 * is single-valued. This becomes problematic if this happens
209 * from the recursive call through generate_non_single_valued
210 * as we would then end up in an infinite recursion.
211 * We therefore check if we are inside a call to generate_non_single_valued
212 * and revert to the ungisted map if the gisted map turns out not to be
213 * single-valued.
215 * Otherwise, call add_domain to generate a call expression (with guard) and
216 * to call the at_each_domain callback, if any.
218 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
220 struct isl_generate_domain_data *data = user;
221 isl_set *domain;
222 isl_map *map = NULL;
223 int empty, sv;
225 domain = isl_ast_build_get_domain(data->build);
226 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
227 executed = isl_map_intersect_domain(executed, domain);
228 empty = isl_map_is_empty(executed);
229 if (empty < 0)
230 goto error;
231 if (empty) {
232 isl_map_free(executed);
233 return isl_stat_ok;
236 sv = isl_map_plain_is_single_valued(executed);
237 if (sv < 0)
238 goto error;
239 if (sv)
240 return add_domain(executed, isl_map_copy(executed), data);
242 executed = isl_map_coalesce(executed);
243 map = isl_map_copy(executed);
244 map = isl_ast_build_compute_gist_map_domain(data->build, map);
245 sv = isl_map_is_single_valued(map);
246 if (sv < 0)
247 goto error;
248 if (!sv) {
249 isl_map_free(map);
250 if (data->build->single_valued)
251 map = isl_map_copy(executed);
252 else
253 return generate_non_single_valued(executed, data);
256 return add_domain(executed, map, data);
257 error:
258 isl_map_free(map);
259 isl_map_free(executed);
260 return isl_stat_error;
263 /* Call build->create_leaf to a create "leaf" node in the AST,
264 * encapsulate the result in an isl_ast_graft and return the result
265 * as a 1-element list.
267 * Note that the node returned by the user may be an entire tree.
269 * Since the node itself cannot enforce any constraints, we turn
270 * all pending constraints into guards and add them to the resulting
271 * graft to ensure that they will be generated.
273 * Before we pass control to the user, we first clear some information
274 * from the build that is (presumbably) only meaningful
275 * for the current code generation.
276 * This includes the create_leaf callback itself, so we make a copy
277 * of the build first.
279 static __isl_give isl_ast_graft_list *call_create_leaf(
280 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
282 isl_set *guard;
283 isl_ast_node *node;
284 isl_ast_graft *graft;
285 isl_ast_build *user_build;
287 guard = isl_ast_build_get_pending(build);
288 user_build = isl_ast_build_copy(build);
289 user_build = isl_ast_build_replace_pending_by_guard(user_build,
290 isl_set_copy(guard));
291 user_build = isl_ast_build_set_executed(user_build, executed);
292 user_build = isl_ast_build_clear_local_info(user_build);
293 if (!user_build)
294 node = NULL;
295 else
296 node = build->create_leaf(user_build, build->create_leaf_user);
297 graft = isl_ast_graft_alloc(node, build);
298 graft = isl_ast_graft_add_guard(graft, guard, build);
299 isl_ast_build_free(build);
300 return isl_ast_graft_list_from_ast_graft(graft);
303 static __isl_give isl_ast_graft_list *build_ast_from_child(
304 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
305 __isl_take isl_union_map *executed);
307 /* Generate an AST after having handled the complete schedule
308 * of this call to the code generator or the complete band
309 * if we are generating an AST from a schedule tree.
311 * If we are inside a band node, then move on to the child of the band.
313 * If the user has specified a create_leaf callback, control
314 * is passed to the user in call_create_leaf.
316 * Otherwise, we generate one or more calls for each individual
317 * domain in generate_domain.
319 static __isl_give isl_ast_graft_list *generate_inner_level(
320 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
322 isl_ctx *ctx;
323 struct isl_generate_domain_data data = { build };
325 if (!build || !executed)
326 goto error;
328 if (isl_ast_build_has_schedule_node(build)) {
329 isl_schedule_node *node;
330 node = isl_ast_build_get_schedule_node(build);
331 build = isl_ast_build_reset_schedule_node(build);
332 return build_ast_from_child(build, node, executed);
335 if (build->create_leaf)
336 return call_create_leaf(executed, build);
338 ctx = isl_union_map_get_ctx(executed);
339 data.list = isl_ast_graft_list_alloc(ctx, 0);
340 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
341 data.list = isl_ast_graft_list_free(data.list);
343 if (0)
344 error: data.list = NULL;
345 isl_ast_build_free(build);
346 isl_union_map_free(executed);
347 return data.list;
350 /* Call the before_each_for callback, if requested by the user.
352 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
353 __isl_keep isl_ast_build *build)
355 isl_id *id;
357 if (!node || !build)
358 return isl_ast_node_free(node);
359 if (!build->before_each_for)
360 return node;
361 id = build->before_each_for(build, build->before_each_for_user);
362 node = isl_ast_node_set_annotation(node, id);
363 return node;
366 /* Call the after_each_for callback, if requested by the user.
368 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
369 __isl_keep isl_ast_build *build)
371 if (!graft || !build)
372 return isl_ast_graft_free(graft);
373 if (!build->after_each_for)
374 return graft;
375 graft->node = build->after_each_for(graft->node, build,
376 build->after_each_for_user);
377 if (!graft->node)
378 return isl_ast_graft_free(graft);
379 return graft;
382 /* Plug in all the know values of the current and outer dimensions
383 * in the domain of "executed". In principle, we only need to plug
384 * in the known value of the current dimension since the values of
385 * outer dimensions have been plugged in already.
386 * However, it turns out to be easier to just plug in all known values.
388 static __isl_give isl_union_map *plug_in_values(
389 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
391 return isl_ast_build_substitute_values_union_map_domain(build,
392 executed);
395 /* Check if the constraint "c" is a lower bound on dimension "pos",
396 * an upper bound, or independent of dimension "pos".
398 static int constraint_type(isl_constraint *c, int pos)
400 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
401 return 1;
402 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
403 return 2;
404 return 0;
407 /* Compare the types of the constraints "a" and "b",
408 * resulting in constraints that are independent of "depth"
409 * to be sorted before the lower bounds on "depth", which in
410 * turn are sorted before the upper bounds on "depth".
412 static int cmp_constraint(__isl_keep isl_constraint *a,
413 __isl_keep isl_constraint *b, void *user)
415 int *depth = user;
416 int t1 = constraint_type(a, *depth);
417 int t2 = constraint_type(b, *depth);
419 return t1 - t2;
422 /* Extract a lower bound on dimension "pos" from constraint "c".
424 * If the constraint is of the form
426 * a x + f(...) >= 0
428 * then we essentially return
430 * l = ceil(-f(...)/a)
432 * However, if the current dimension is strided, then we need to make
433 * sure that the lower bound we construct is of the form
435 * f + s a
437 * with f the offset and s the stride.
438 * We therefore compute
440 * f + s * ceil((l - f)/s)
442 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
443 int pos, __isl_keep isl_ast_build *build)
445 isl_aff *aff;
447 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
448 aff = isl_aff_ceil(aff);
450 if (isl_ast_build_has_stride(build, pos)) {
451 isl_aff *offset;
452 isl_val *stride;
454 offset = isl_ast_build_get_offset(build, pos);
455 stride = isl_ast_build_get_stride(build, pos);
457 aff = isl_aff_sub(aff, isl_aff_copy(offset));
458 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
459 aff = isl_aff_ceil(aff);
460 aff = isl_aff_scale_val(aff, stride);
461 aff = isl_aff_add(aff, offset);
464 aff = isl_ast_build_compute_gist_aff(build, aff);
466 return aff;
469 /* Return the exact lower bound (or upper bound if "upper" is set)
470 * of "domain" as a piecewise affine expression.
472 * If we are computing a lower bound (of a strided dimension), then
473 * we need to make sure it is of the form
475 * f + s a
477 * where f is the offset and s is the stride.
478 * We therefore need to include the stride constraint before computing
479 * the minimum.
481 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
482 __isl_keep isl_ast_build *build, int upper)
484 isl_set *stride;
485 isl_map *it_map;
486 isl_pw_aff *pa;
487 isl_pw_multi_aff *pma;
489 domain = isl_set_copy(domain);
490 if (!upper) {
491 stride = isl_ast_build_get_stride_constraint(build);
492 domain = isl_set_intersect(domain, stride);
494 it_map = isl_ast_build_map_to_iterator(build, domain);
495 if (upper)
496 pma = isl_map_lexmax_pw_multi_aff(it_map);
497 else
498 pma = isl_map_lexmin_pw_multi_aff(it_map);
499 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
500 isl_pw_multi_aff_free(pma);
501 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
502 pa = isl_pw_aff_coalesce(pa);
504 return pa;
507 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
508 * remove_redundant_lower_bounds.
510 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
511 void *user)
513 return isl_pw_aff_plain_cmp(a, b);
516 /* Given a list of lower bounds "list", remove those that are redundant
517 * with respect to the other bounds in "list" and the domain of "build".
519 * We first sort the bounds in the same way as they would be sorted
520 * by set_for_node_expressions so that we can try and remove the last
521 * bounds first.
523 * For a lower bound to be effective, there needs to be at least
524 * one domain element for which it is larger than all other lower bounds.
525 * For each lower bound we therefore intersect the domain with
526 * the conditions that it is larger than all other bounds and
527 * check whether the result is empty. If so, the bound can be removed.
529 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
530 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
532 int i, j;
533 isl_size n;
534 isl_set *domain;
536 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
538 n = isl_pw_aff_list_n_pw_aff(list);
539 if (n < 0)
540 return isl_pw_aff_list_free(list);
541 if (n <= 1)
542 return list;
544 domain = isl_ast_build_get_domain(build);
546 for (i = n - 1; i >= 0; --i) {
547 isl_pw_aff *pa_i;
548 isl_set *domain_i;
549 int empty;
551 domain_i = isl_set_copy(domain);
552 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
554 for (j = 0; j < n; ++j) {
555 isl_pw_aff *pa_j;
556 isl_set *better;
558 if (j == i)
559 continue;
561 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
562 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
563 domain_i = isl_set_intersect(domain_i, better);
566 empty = isl_set_is_empty(domain_i);
568 isl_set_free(domain_i);
569 isl_pw_aff_free(pa_i);
571 if (empty < 0)
572 goto error;
573 if (!empty)
574 continue;
575 list = isl_pw_aff_list_drop(list, i, 1);
576 n--;
579 isl_set_free(domain);
581 return list;
582 error:
583 isl_set_free(domain);
584 return isl_pw_aff_list_free(list);
587 /* Extract a lower bound on dimension "pos" from each constraint
588 * in "constraints" and return the list of lower bounds.
589 * If "constraints" has zero elements, then we extract a lower bound
590 * from "domain" instead.
592 * If the current dimension is strided, then the lower bound
593 * is adjusted by lower_bound to match the stride information.
594 * This modification may make one or more lower bounds redundant
595 * with respect to the other lower bounds. We therefore check
596 * for this condition and remove the redundant lower bounds.
598 static __isl_give isl_pw_aff_list *lower_bounds(
599 __isl_keep isl_constraint_list *constraints, int pos,
600 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
602 isl_ctx *ctx;
603 isl_pw_aff_list *list;
604 int i;
605 isl_size n;
607 if (!build)
608 return NULL;
610 n = isl_constraint_list_n_constraint(constraints);
611 if (n < 0)
612 return NULL;
613 if (n == 0) {
614 isl_pw_aff *pa;
615 pa = exact_bound(domain, build, 0);
616 return isl_pw_aff_list_from_pw_aff(pa);
619 ctx = isl_ast_build_get_ctx(build);
620 list = isl_pw_aff_list_alloc(ctx,n);
622 for (i = 0; i < n; ++i) {
623 isl_aff *aff;
624 isl_constraint *c;
626 c = isl_constraint_list_get_constraint(constraints, i);
627 aff = lower_bound(c, pos, build);
628 isl_constraint_free(c);
629 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
632 if (isl_ast_build_has_stride(build, pos))
633 list = remove_redundant_lower_bounds(list, build);
635 return list;
638 /* Extract an upper bound on dimension "pos" from each constraint
639 * in "constraints" and return the list of upper bounds.
640 * If "constraints" has zero elements, then we extract an upper bound
641 * from "domain" instead.
643 static __isl_give isl_pw_aff_list *upper_bounds(
644 __isl_keep isl_constraint_list *constraints, int pos,
645 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
647 isl_ctx *ctx;
648 isl_pw_aff_list *list;
649 int i;
650 isl_size n;
652 n = isl_constraint_list_n_constraint(constraints);
653 if (n < 0)
654 return NULL;
655 if (n == 0) {
656 isl_pw_aff *pa;
657 pa = exact_bound(domain, build, 1);
658 return isl_pw_aff_list_from_pw_aff(pa);
661 ctx = isl_ast_build_get_ctx(build);
662 list = isl_pw_aff_list_alloc(ctx,n);
664 for (i = 0; i < n; ++i) {
665 isl_aff *aff;
666 isl_constraint *c;
668 c = isl_constraint_list_get_constraint(constraints, i);
669 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
670 isl_constraint_free(c);
671 aff = isl_aff_floor(aff);
672 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
675 return list;
678 /* Return an isl_ast_expr that performs the reduction of type "type"
679 * on AST expressions corresponding to the elements in "list".
681 * The list is assumed to contain at least one element.
682 * If the list contains exactly one element, then the returned isl_ast_expr
683 * simply computes that affine expression.
684 * If the list contains more than one element, then we sort it
685 * using a fairly arbitrary but hopefully reasonably stable order.
687 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_expr_op_type type,
688 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
690 int i;
691 isl_size n;
692 isl_ctx *ctx;
693 isl_ast_expr *expr;
695 n = isl_pw_aff_list_n_pw_aff(list);
696 if (n < 0)
697 return NULL;
699 if (n == 1)
700 return isl_ast_build_expr_from_pw_aff_internal(build,
701 isl_pw_aff_list_get_pw_aff(list, 0));
703 ctx = isl_pw_aff_list_get_ctx(list);
704 expr = isl_ast_expr_alloc_op(ctx, type, n);
705 if (!expr)
706 return NULL;
708 list = isl_pw_aff_list_copy(list);
709 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
710 if (!list)
711 return isl_ast_expr_free(expr);
713 for (i = 0; i < n; ++i) {
714 isl_ast_expr *expr_i;
716 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
717 isl_pw_aff_list_get_pw_aff(list, i));
718 if (!expr_i)
719 goto error;
720 expr->u.op.args[i] = expr_i;
723 isl_pw_aff_list_free(list);
724 return expr;
725 error:
726 isl_pw_aff_list_free(list);
727 isl_ast_expr_free(expr);
728 return NULL;
731 /* Add guards implied by the "generated constraints",
732 * but not (necessarily) enforced by the generated AST to "guard".
733 * In particular, if there is any stride constraints,
734 * then add the guard implied by those constraints.
735 * If we have generated a degenerate loop, then add the guard
736 * implied by "bounds" on the outer dimensions, i.e., the guard
737 * that ensures that the single value actually exists.
738 * Since there may also be guards implied by a combination
739 * of these constraints, we first combine them before
740 * deriving the implied constraints.
742 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
743 int degenerate, __isl_keep isl_basic_set *bounds,
744 __isl_keep isl_ast_build *build)
746 isl_size depth;
747 isl_bool has_stride;
748 isl_space *space;
749 isl_set *dom, *set;
751 depth = isl_ast_build_get_depth(build);
752 has_stride = isl_ast_build_has_stride(build, depth);
753 if (depth < 0 || has_stride < 0)
754 return isl_set_free(guard);
755 if (!has_stride && !degenerate)
756 return guard;
758 space = isl_basic_set_get_space(bounds);
759 dom = isl_set_universe(space);
761 if (degenerate) {
762 bounds = isl_basic_set_copy(bounds);
763 bounds = isl_basic_set_drop_constraints_not_involving_dims(
764 bounds, isl_dim_set, depth, 1);
765 set = isl_set_from_basic_set(bounds);
766 dom = isl_set_intersect(dom, set);
769 if (has_stride) {
770 set = isl_ast_build_get_stride_constraint(build);
771 dom = isl_set_intersect(dom, set);
774 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
775 dom = isl_ast_build_compute_gist(build, dom);
776 guard = isl_set_intersect(guard, dom);
778 return guard;
781 /* Update "graft" based on "sub_build" for the degenerate case.
783 * "build" is the build in which graft->node was created
784 * "sub_build" contains information about the current level itself,
785 * including the single value attained.
787 * We set the initialization part of the for loop to the single
788 * value attained by the current dimension.
789 * The increment and condition are not strictly needed as they are known
790 * to be "1" and "iterator <= value" respectively.
792 static __isl_give isl_ast_graft *refine_degenerate(
793 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
794 __isl_keep isl_ast_build *sub_build)
796 isl_pw_aff *value;
798 if (!graft || !sub_build)
799 return isl_ast_graft_free(graft);
801 value = isl_pw_aff_copy(sub_build->value);
803 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
804 value);
805 if (!graft->node->u.f.init)
806 return isl_ast_graft_free(graft);
808 return graft;
811 /* Return the intersection of constraints in "list" as a set.
813 static __isl_give isl_set *intersect_constraints(
814 __isl_keep isl_constraint_list *list)
816 int i;
817 isl_size n;
818 isl_basic_set *bset;
820 n = isl_constraint_list_n_constraint(list);
821 if (n < 0)
822 return NULL;
823 if (n < 1)
824 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
825 "expecting at least one constraint", return NULL);
827 bset = isl_basic_set_from_constraint(
828 isl_constraint_list_get_constraint(list, 0));
829 for (i = 1; i < n; ++i) {
830 isl_basic_set *bset_i;
832 bset_i = isl_basic_set_from_constraint(
833 isl_constraint_list_get_constraint(list, i));
834 bset = isl_basic_set_intersect(bset, bset_i);
837 return isl_set_from_basic_set(bset);
840 /* Compute the constraints on the outer dimensions enforced by
841 * graft->node and add those constraints to graft->enforced,
842 * in case the upper bound is expressed as a set "upper".
844 * In particular, if l(...) is a lower bound in "lower", and
846 * -a i + f(...) >= 0 or a i <= f(...)
848 * is an upper bound ocnstraint on the current dimension i,
849 * then the for loop enforces the constraint
851 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
853 * We therefore simply take each lower bound in turn, plug it into
854 * the upper bounds and compute the intersection over all lower bounds.
856 * If a lower bound is a rational expression, then
857 * isl_basic_set_preimage_multi_aff will force this rational
858 * expression to have only integer values. However, the loop
859 * itself does not enforce this integrality constraint. We therefore
860 * use the ceil of the lower bounds instead of the lower bounds themselves.
861 * Other constraints will make sure that the for loop is only executed
862 * when each of the lower bounds attains an integral value.
863 * In particular, potentially rational values only occur in
864 * lower_bound if the offset is a (seemingly) rational expression,
865 * but then outer conditions will make sure that this rational expression
866 * only attains integer values.
868 static __isl_give isl_ast_graft *set_enforced_from_set(
869 __isl_take isl_ast_graft *graft,
870 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
872 isl_space *space;
873 isl_basic_set *enforced;
874 isl_pw_multi_aff *pma;
875 int i;
876 isl_size n;
878 n = isl_pw_aff_list_n_pw_aff(lower);
879 if (!graft || n < 0)
880 return isl_ast_graft_free(graft);
882 space = isl_set_get_space(upper);
883 enforced = isl_basic_set_universe(isl_space_copy(space));
885 space = isl_space_map_from_set(space);
886 pma = isl_pw_multi_aff_identity(space);
888 for (i = 0; i < n; ++i) {
889 isl_pw_aff *pa;
890 isl_set *enforced_i;
891 isl_basic_set *hull;
892 isl_pw_multi_aff *pma_i;
894 pa = isl_pw_aff_list_get_pw_aff(lower, i);
895 pa = isl_pw_aff_ceil(pa);
896 pma_i = isl_pw_multi_aff_copy(pma);
897 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
898 enforced_i = isl_set_copy(upper);
899 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
900 hull = isl_set_simple_hull(enforced_i);
901 enforced = isl_basic_set_intersect(enforced, hull);
904 isl_pw_multi_aff_free(pma);
906 graft = isl_ast_graft_enforce(graft, enforced);
908 return graft;
911 /* Compute the constraints on the outer dimensions enforced by
912 * graft->node and add those constraints to graft->enforced,
913 * in case the upper bound is expressed as
914 * a list of affine expressions "upper".
916 * The enforced condition is that each lower bound expression is less
917 * than or equal to each upper bound expression.
919 static __isl_give isl_ast_graft *set_enforced_from_list(
920 __isl_take isl_ast_graft *graft,
921 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
923 isl_set *cond;
924 isl_basic_set *enforced;
926 lower = isl_pw_aff_list_copy(lower);
927 upper = isl_pw_aff_list_copy(upper);
928 cond = isl_pw_aff_list_le_set(lower, upper);
929 enforced = isl_set_simple_hull(cond);
930 graft = isl_ast_graft_enforce(graft, enforced);
932 return graft;
935 /* Does "aff" have a negative constant term?
937 static isl_bool aff_constant_is_negative(__isl_keep isl_set *set,
938 __isl_keep isl_aff *aff, void *user)
940 isl_bool is_neg;
941 isl_val *v;
943 v = isl_aff_get_constant_val(aff);
944 is_neg = isl_val_is_neg(v);
945 isl_val_free(v);
947 return is_neg;
950 /* Does "pa" have a negative constant term over its entire domain?
952 static isl_bool pw_aff_constant_is_negative(__isl_keep isl_pw_aff *pa,
953 void *user)
955 return isl_pw_aff_every_piece(pa, &aff_constant_is_negative, NULL);
958 /* Does each element in "list" have a negative constant term?
960 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
962 return isl_pw_aff_list_every(list, &pw_aff_constant_is_negative, NULL);
965 /* Add 1 to each of the elements in "list", where each of these elements
966 * is defined over the internal schedule space of "build".
968 static __isl_give isl_pw_aff_list *list_add_one(
969 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
971 int i;
972 isl_size n;
973 isl_space *space;
974 isl_aff *aff;
975 isl_pw_aff *one;
977 n = isl_pw_aff_list_n_pw_aff(list);
978 if (n < 0)
979 return isl_pw_aff_list_free(list);
981 space = isl_ast_build_get_space(build, 1);
982 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
983 aff = isl_aff_add_constant_si(aff, 1);
984 one = isl_pw_aff_from_aff(aff);
986 for (i = 0; i < n; ++i) {
987 isl_pw_aff *pa;
988 pa = isl_pw_aff_list_get_pw_aff(list, i);
989 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
990 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
993 isl_pw_aff_free(one);
995 return list;
998 /* Set the condition part of the for node graft->node in case
999 * the upper bound is represented as a list of piecewise affine expressions.
1001 * In particular, set the condition to
1003 * iterator <= min(list of upper bounds)
1005 * If each of the upper bounds has a negative constant term, then
1006 * set the condition to
1008 * iterator < min(list of (upper bound + 1)s)
1011 static __isl_give isl_ast_graft *set_for_cond_from_list(
1012 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
1013 __isl_keep isl_ast_build *build)
1015 int neg;
1016 isl_ast_expr *bound, *iterator, *cond;
1017 enum isl_ast_expr_op_type type = isl_ast_expr_op_le;
1019 if (!graft || !list)
1020 return isl_ast_graft_free(graft);
1022 neg = list_constant_is_negative(list);
1023 if (neg < 0)
1024 return isl_ast_graft_free(graft);
1025 list = isl_pw_aff_list_copy(list);
1026 if (neg) {
1027 list = list_add_one(list, build);
1028 type = isl_ast_expr_op_lt;
1031 bound = reduce_list(isl_ast_expr_op_min, list, build);
1032 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1033 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1034 graft->node->u.f.cond = cond;
1036 isl_pw_aff_list_free(list);
1037 if (!graft->node->u.f.cond)
1038 return isl_ast_graft_free(graft);
1039 return graft;
1042 /* Set the condition part of the for node graft->node in case
1043 * the upper bound is represented as a set.
1045 static __isl_give isl_ast_graft *set_for_cond_from_set(
1046 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1047 __isl_keep isl_ast_build *build)
1049 isl_ast_expr *cond;
1051 if (!graft)
1052 return NULL;
1054 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1055 graft->node->u.f.cond = cond;
1056 if (!graft->node->u.f.cond)
1057 return isl_ast_graft_free(graft);
1058 return graft;
1061 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1062 * the current dimension.
1064 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1066 isl_size depth;
1067 isl_val *v;
1068 isl_ctx *ctx;
1070 depth = isl_ast_build_get_depth(build);
1071 if (depth < 0)
1072 return NULL;
1073 ctx = isl_ast_build_get_ctx(build);
1075 if (!isl_ast_build_has_stride(build, depth))
1076 return isl_ast_expr_alloc_int_si(ctx, 1);
1078 v = isl_ast_build_get_stride(build, depth);
1079 return isl_ast_expr_from_val(v);
1082 /* Should we express the loop condition as
1084 * iterator <= min(list of upper bounds)
1086 * or as a conjunction of constraints?
1088 * The first is constructed from a list of upper bounds.
1089 * The second is constructed from a set.
1091 * If there are no upper bounds in "constraints", then this could mean
1092 * that "domain" simply doesn't have an upper bound or that we didn't
1093 * pick any upper bound. In the first case, we want to generate the
1094 * loop condition as a(n empty) conjunction of constraints
1095 * In the second case, we will compute
1096 * a single upper bound from "domain" and so we use the list form.
1098 * If there are upper bounds in "constraints",
1099 * then we use the list form iff the atomic_upper_bound option is set.
1101 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1102 __isl_keep isl_set *domain, int depth)
1104 if (n_upper > 0)
1105 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1106 else
1107 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1110 /* Fill in the expressions of the for node in graft->node.
1112 * In particular,
1113 * - set the initialization part of the loop to the maximum of the lower bounds
1114 * - extract the increment from the stride of the current dimension
1115 * - construct the for condition either based on a list of upper bounds
1116 * or on a set of upper bound constraints.
1118 static __isl_give isl_ast_graft *set_for_node_expressions(
1119 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1120 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1121 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1123 isl_ast_node *node;
1125 if (!graft)
1126 return NULL;
1128 build = isl_ast_build_copy(build);
1130 node = graft->node;
1131 node->u.f.init = reduce_list(isl_ast_expr_op_max, lower, build);
1132 node->u.f.inc = for_inc(build);
1134 if (!node->u.f.init || !node->u.f.inc)
1135 graft = isl_ast_graft_free(graft);
1137 if (use_list)
1138 graft = set_for_cond_from_list(graft, upper_list, build);
1139 else
1140 graft = set_for_cond_from_set(graft, upper_set, build);
1142 isl_ast_build_free(build);
1144 return graft;
1147 /* Update "graft" based on "bounds" and "domain" for the generic,
1148 * non-degenerate, case.
1150 * "c_lower" and "c_upper" contain the lower and upper bounds
1151 * that the loop node should express.
1152 * "domain" is the subset of the intersection of the constraints
1153 * for which some code is executed.
1155 * There may be zero lower bounds or zero upper bounds in "constraints"
1156 * in case the list of constraints was created
1157 * based on the atomic option or based on separation with explicit bounds.
1158 * In that case, we use "domain" to derive lower and/or upper bounds.
1160 * We first compute a list of one or more lower bounds.
1162 * Then we decide if we want to express the condition as
1164 * iterator <= min(list of upper bounds)
1166 * or as a conjunction of constraints.
1168 * The set of enforced constraints is then computed either based on
1169 * a list of upper bounds or on a set of upper bound constraints.
1170 * We do not compute any enforced constraints if we were forced
1171 * to compute a lower or upper bound using exact_bound. The domains
1172 * of the resulting expressions may imply some bounds on outer dimensions
1173 * that we do not want to appear in the enforced constraints since
1174 * they are not actually enforced by the corresponding code.
1176 * Finally, we fill in the expressions of the for node.
1178 static __isl_give isl_ast_graft *refine_generic_bounds(
1179 __isl_take isl_ast_graft *graft,
1180 __isl_take isl_constraint_list *c_lower,
1181 __isl_take isl_constraint_list *c_upper,
1182 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1184 isl_size depth;
1185 isl_ctx *ctx;
1186 isl_pw_aff_list *lower;
1187 int use_list;
1188 isl_set *upper_set = NULL;
1189 isl_pw_aff_list *upper_list = NULL;
1190 isl_size n_lower, n_upper;
1192 depth = isl_ast_build_get_depth(build);
1193 if (!graft || !c_lower || !c_upper || depth < 0)
1194 goto error;
1196 ctx = isl_ast_graft_get_ctx(graft);
1198 n_lower = isl_constraint_list_n_constraint(c_lower);
1199 n_upper = isl_constraint_list_n_constraint(c_upper);
1200 if (n_lower < 0 || n_upper < 0)
1201 goto error;
1203 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1205 lower = lower_bounds(c_lower, depth, domain, build);
1207 if (use_list)
1208 upper_list = upper_bounds(c_upper, depth, domain, build);
1209 else if (n_upper > 0)
1210 upper_set = intersect_constraints(c_upper);
1211 else
1212 upper_set = isl_set_universe(isl_set_get_space(domain));
1214 if (n_lower == 0 || n_upper == 0)
1216 else if (use_list)
1217 graft = set_enforced_from_list(graft, lower, upper_list);
1218 else
1219 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1221 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1222 upper_set, build);
1224 isl_pw_aff_list_free(lower);
1225 isl_pw_aff_list_free(upper_list);
1226 isl_set_free(upper_set);
1227 isl_constraint_list_free(c_lower);
1228 isl_constraint_list_free(c_upper);
1230 return graft;
1231 error:
1232 isl_constraint_list_free(c_lower);
1233 isl_constraint_list_free(c_upper);
1234 return isl_ast_graft_free(graft);
1237 /* Internal data structure used inside count_constraints to keep
1238 * track of the number of constraints that are independent of dimension "pos",
1239 * the lower bounds in "pos" and the upper bounds in "pos".
1241 struct isl_ast_count_constraints_data {
1242 int pos;
1244 int n_indep;
1245 int n_lower;
1246 int n_upper;
1249 /* Increment data->n_indep, data->lower or data->upper depending
1250 * on whether "c" is independenct of dimensions data->pos,
1251 * a lower bound or an upper bound.
1253 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1255 struct isl_ast_count_constraints_data *data = user;
1257 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1258 data->n_lower++;
1259 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1260 data->n_upper++;
1261 else
1262 data->n_indep++;
1264 isl_constraint_free(c);
1266 return isl_stat_ok;
1269 /* Update "graft" based on "bounds" and "domain" for the generic,
1270 * non-degenerate, case.
1272 * "list" respresent the list of bounds that need to be encoded by
1273 * the for loop. Only the constraints that involve the iterator
1274 * are relevant here. The other constraints are taken care of by
1275 * the caller and are included in the generated constraints of "build".
1276 * "domain" is the subset of the intersection of the constraints
1277 * for which some code is executed.
1278 * "build" is the build in which graft->node was created.
1280 * We separate lower bounds, upper bounds and constraints that
1281 * are independent of the loop iterator.
1283 * The actual for loop bounds are generated in refine_generic_bounds.
1285 static __isl_give isl_ast_graft *refine_generic_split(
1286 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1287 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1289 struct isl_ast_count_constraints_data data;
1290 isl_size depth;
1291 isl_constraint_list *lower;
1292 isl_constraint_list *upper;
1294 depth = isl_ast_build_get_depth(build);
1295 if (depth < 0)
1296 list = isl_constraint_list_free(list);
1297 if (!list)
1298 return isl_ast_graft_free(graft);
1300 data.pos = depth;
1302 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1303 if (!list)
1304 return isl_ast_graft_free(graft);
1306 data.n_indep = data.n_lower = data.n_upper = 0;
1307 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1308 isl_constraint_list_free(list);
1309 return isl_ast_graft_free(graft);
1312 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1313 upper = isl_constraint_list_copy(lower);
1314 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1315 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1317 return refine_generic_bounds(graft, lower, upper, domain, build);
1320 /* Update "graft" based on "bounds" and "domain" for the generic,
1321 * non-degenerate, case.
1323 * "bounds" respresent the bounds that need to be encoded by
1324 * the for loop (or a guard around the for loop).
1325 * "domain" is the subset of "bounds" for which some code is executed.
1326 * "build" is the build in which graft->node was created.
1328 * We break up "bounds" into a list of constraints and continue with
1329 * refine_generic_split.
1331 static __isl_give isl_ast_graft *refine_generic(
1332 __isl_take isl_ast_graft *graft,
1333 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1334 __isl_keep isl_ast_build *build)
1336 isl_constraint_list *list;
1338 if (!build || !graft)
1339 return isl_ast_graft_free(graft);
1341 list = isl_basic_set_get_constraint_list(bounds);
1343 graft = refine_generic_split(graft, list, domain, build);
1345 return graft;
1348 /* Create a for node for the current level.
1350 * Mark the for node degenerate if "degenerate" is set.
1352 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1353 int degenerate)
1355 isl_size depth;
1356 isl_id *id;
1357 isl_ast_node *node;
1359 depth = isl_ast_build_get_depth(build);
1360 if (depth < 0)
1361 return NULL;
1363 id = isl_ast_build_get_iterator_id(build, depth);
1364 node = isl_ast_node_alloc_for(id);
1365 if (degenerate)
1366 node = isl_ast_node_for_mark_degenerate(node);
1368 return node;
1371 /* If the ast_build_exploit_nested_bounds option is set, then return
1372 * the constraints enforced by all elements in "list".
1373 * Otherwise, return the universe.
1375 static __isl_give isl_basic_set *extract_shared_enforced(
1376 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1378 isl_ctx *ctx;
1379 isl_space *space;
1381 if (!list)
1382 return NULL;
1384 ctx = isl_ast_graft_list_get_ctx(list);
1385 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1386 return isl_ast_graft_list_extract_shared_enforced(list, build);
1388 space = isl_ast_build_get_space(build, 1);
1389 return isl_basic_set_universe(space);
1392 /* Return the pending constraints of "build" that are not already taken
1393 * care of (by a combination of "enforced" and the generated constraints
1394 * of "build").
1396 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1397 __isl_keep isl_basic_set *enforced)
1399 isl_set *guard, *context;
1401 guard = isl_ast_build_get_pending(build);
1402 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1403 context = isl_set_intersect(context,
1404 isl_ast_build_get_generated(build));
1405 return isl_set_gist(guard, context);
1408 /* Create an AST node for the current dimension based on
1409 * the schedule domain "bounds" and return the node encapsulated
1410 * in an isl_ast_graft.
1412 * "executed" is the current inverse schedule, taking into account
1413 * the bounds in "bounds"
1414 * "domain" is the domain of "executed", with inner dimensions projected out.
1415 * It may be a strict subset of "bounds" in case "bounds" was created
1416 * based on the atomic option or based on separation with explicit bounds.
1418 * "domain" may satisfy additional equalities that result
1419 * from intersecting "executed" with "bounds" in add_node.
1420 * It may also satisfy some global constraints that were dropped out because
1421 * we performed separation with explicit bounds.
1422 * The very first step is then to copy these constraints to "bounds".
1424 * Since we may be calling before_each_for and after_each_for
1425 * callbacks, we record the current inverse schedule in the build.
1427 * We consider three builds,
1428 * "build" is the one in which the current level is created,
1429 * "body_build" is the build in which the next level is created,
1430 * "sub_build" is essentially the same as "body_build", except that
1431 * the depth has not been increased yet.
1433 * "build" already contains information (in strides and offsets)
1434 * about the strides at the current level, but this information is not
1435 * reflected in the build->domain.
1436 * We first add this information and the "bounds" to the sub_build->domain.
1437 * isl_ast_build_set_loop_bounds adds the stride information and
1438 * checks whether the current dimension attains
1439 * only a single value and whether this single value can be represented using
1440 * a single affine expression.
1441 * In the first case, the current level is considered "degenerate".
1442 * In the second, sub-case, the current level is considered "eliminated".
1443 * Eliminated levels don't need to be reflected in the AST since we can
1444 * simply plug in the affine expression. For degenerate, but non-eliminated,
1445 * levels, we do introduce a for node, but mark is as degenerate so that
1446 * it can be printed as an assignment of the single value to the loop
1447 * "iterator".
1449 * If the current level is eliminated, we explicitly plug in the value
1450 * for the current level found by isl_ast_build_set_loop_bounds in the
1451 * inverse schedule. This ensures that if we are working on a slice
1452 * of the domain based on information available in the inverse schedule
1453 * and the build domain, that then this information is also reflected
1454 * in the inverse schedule. This operation also eliminates the current
1455 * dimension from the inverse schedule making sure no inner dimensions depend
1456 * on the current dimension. Otherwise, we create a for node, marking
1457 * it degenerate if appropriate. The initial for node is still incomplete
1458 * and will be completed in either refine_degenerate or refine_generic.
1460 * We then generate a sequence of grafts for the next level,
1461 * create a surrounding graft for the current level and insert
1462 * the for node we created (if the current level is not eliminated).
1463 * Before creating a graft for the current level, we first extract
1464 * hoistable constraints from the child guards and combine them
1465 * with the pending constraints in the build. These constraints
1466 * are used to simplify the child guards and then added to the guard
1467 * of the current graft to ensure that they will be generated.
1468 * If the hoisted guard is a disjunction, then we use it directly
1469 * to gist the guards on the children before intersect it with the
1470 * pending constraints. We do so because this disjunction is typically
1471 * identical to the guards on the children such that these guards
1472 * can be effectively removed completely. After the intersection,
1473 * the gist operation would have a harder time figuring this out.
1475 * Finally, we set the bounds of the for loop in either
1476 * refine_degenerate or refine_generic.
1477 * We do so in a context where the pending constraints of the build
1478 * have been replaced by the guard of the current graft.
1480 static __isl_give isl_ast_graft *create_node_scaled(
1481 __isl_take isl_union_map *executed,
1482 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1483 __isl_take isl_ast_build *build)
1485 isl_size depth;
1486 int degenerate;
1487 isl_bool eliminated;
1488 isl_size n;
1489 isl_basic_set *hull;
1490 isl_basic_set *enforced;
1491 isl_set *guard, *hoisted;
1492 isl_ast_node *node = NULL;
1493 isl_ast_graft *graft;
1494 isl_ast_graft_list *children;
1495 isl_ast_build *sub_build;
1496 isl_ast_build *body_build;
1498 domain = isl_ast_build_eliminate_divs(build, domain);
1499 domain = isl_set_detect_equalities(domain);
1500 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1501 bounds = isl_basic_set_intersect(bounds, hull);
1502 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1504 depth = isl_ast_build_get_depth(build);
1505 if (depth < 0)
1506 build = isl_ast_build_free(build);
1507 sub_build = isl_ast_build_copy(build);
1508 bounds = isl_basic_set_remove_redundancies(bounds);
1509 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1510 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1511 isl_basic_set_copy(bounds));
1512 degenerate = isl_ast_build_has_value(sub_build);
1513 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1514 if (degenerate < 0 || eliminated < 0)
1515 executed = isl_union_map_free(executed);
1516 if (!degenerate)
1517 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1518 sub_build = isl_ast_build_set_pending_generated(sub_build,
1519 isl_basic_set_copy(bounds));
1520 if (eliminated)
1521 executed = plug_in_values(executed, sub_build);
1522 else
1523 node = create_for(build, degenerate);
1525 body_build = isl_ast_build_copy(sub_build);
1526 body_build = isl_ast_build_increase_depth(body_build);
1527 if (!eliminated)
1528 node = before_each_for(node, body_build);
1529 children = generate_next_level(executed,
1530 isl_ast_build_copy(body_build));
1532 enforced = extract_shared_enforced(children, build);
1533 guard = extract_pending(sub_build, enforced);
1534 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1535 n = isl_set_n_basic_set(hoisted);
1536 if (n < 0)
1537 children = isl_ast_graft_list_free(children);
1538 if (n > 1)
1539 children = isl_ast_graft_list_gist_guards(children,
1540 isl_set_copy(hoisted));
1541 guard = isl_set_intersect(guard, hoisted);
1542 if (!eliminated)
1543 guard = add_implied_guards(guard, degenerate, bounds, build);
1545 graft = isl_ast_graft_alloc_from_children(children,
1546 isl_set_copy(guard), enforced, build, sub_build);
1548 if (!eliminated) {
1549 isl_ast_build *for_build;
1551 graft = isl_ast_graft_insert_for(graft, node);
1552 for_build = isl_ast_build_copy(build);
1553 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1554 isl_set_copy(guard));
1555 if (degenerate)
1556 graft = refine_degenerate(graft, for_build, sub_build);
1557 else
1558 graft = refine_generic(graft, bounds,
1559 domain, for_build);
1560 isl_ast_build_free(for_build);
1562 isl_set_free(guard);
1563 if (!eliminated)
1564 graft = after_each_for(graft, body_build);
1566 isl_ast_build_free(body_build);
1567 isl_ast_build_free(sub_build);
1568 isl_ast_build_free(build);
1569 isl_basic_set_free(bounds);
1570 isl_set_free(domain);
1572 return graft;
1575 /* Internal data structure for checking if all constraints involving
1576 * the input dimension "depth" are such that the other coefficients
1577 * are multiples of "m", reducing "m" if they are not.
1578 * If "m" is reduced all the way down to "1", then the check has failed
1579 * and we break out of the iteration.
1581 struct isl_check_scaled_data {
1582 int depth;
1583 isl_val *m;
1586 /* If constraint "c" involves the input dimension data->depth,
1587 * then make sure that all the other coefficients are multiples of data->m,
1588 * reducing data->m if needed.
1589 * Break out of the iteration if data->m has become equal to "1".
1591 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1592 void *user)
1594 struct isl_check_scaled_data *data = user;
1595 int i, j;
1596 isl_size n;
1597 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1598 isl_dim_div };
1600 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1601 isl_constraint_free(c);
1602 return isl_stat_ok;
1605 for (i = 0; i < 4; ++i) {
1606 n = isl_constraint_dim(c, t[i]);
1607 if (n < 0)
1608 break;
1609 for (j = 0; j < n; ++j) {
1610 isl_val *d;
1612 if (t[i] == isl_dim_in && j == data->depth)
1613 continue;
1614 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1615 continue;
1616 d = isl_constraint_get_coefficient_val(c, t[i], j);
1617 data->m = isl_val_gcd(data->m, d);
1618 if (isl_val_is_one(data->m))
1619 break;
1621 if (j < n)
1622 break;
1625 isl_constraint_free(c);
1627 return i < 4 ? isl_stat_error : isl_stat_ok;
1630 /* For each constraint of "bmap" that involves the input dimension data->depth,
1631 * make sure that all the other coefficients are multiples of data->m,
1632 * reducing data->m if needed.
1633 * Break out of the iteration if data->m has become equal to "1".
1635 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1636 void *user)
1638 isl_stat r;
1640 r = isl_basic_map_foreach_constraint(bmap,
1641 &constraint_check_scaled, user);
1642 isl_basic_map_free(bmap);
1644 return r;
1647 /* For each constraint of "map" that involves the input dimension data->depth,
1648 * make sure that all the other coefficients are multiples of data->m,
1649 * reducing data->m if needed.
1650 * Break out of the iteration if data->m has become equal to "1".
1652 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1654 isl_stat r;
1656 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1657 isl_map_free(map);
1659 return r;
1662 /* Create an AST node for the current dimension based on
1663 * the schedule domain "bounds" and return the node encapsulated
1664 * in an isl_ast_graft.
1666 * "executed" is the current inverse schedule, taking into account
1667 * the bounds in "bounds"
1668 * "domain" is the domain of "executed", with inner dimensions projected out.
1671 * Before moving on to the actual AST node construction in create_node_scaled,
1672 * we first check if the current dimension is strided and if we can scale
1673 * down this stride. Note that we only do this if the ast_build_scale_strides
1674 * option is set.
1676 * In particular, let the current dimension take on values
1678 * f + s a
1680 * with a an integer. We check if we can find an integer m that (obviously)
1681 * divides both f and s.
1683 * If so, we check if the current dimension only appears in constraints
1684 * where the coefficients of the other variables are multiples of m.
1685 * We perform this extra check to avoid the risk of introducing
1686 * divisions by scaling down the current dimension.
1688 * If so, we scale the current dimension down by a factor of m.
1689 * That is, we plug in
1691 * i = m i' (1)
1693 * Note that in principle we could always scale down strided loops
1694 * by plugging in
1696 * i = f + s i'
1698 * but this may result in i' taking on larger values than the original i,
1699 * due to the shift by "f".
1700 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1702 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1703 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1704 __isl_take isl_ast_build *build)
1706 struct isl_check_scaled_data data;
1707 isl_size depth;
1708 isl_ctx *ctx;
1709 isl_aff *offset;
1710 isl_val *d;
1712 ctx = isl_ast_build_get_ctx(build);
1713 if (!isl_options_get_ast_build_scale_strides(ctx))
1714 return create_node_scaled(executed, bounds, domain, build);
1716 depth = isl_ast_build_get_depth(build);
1717 if (depth < 0)
1718 build = isl_ast_build_free(build);
1719 data.depth = depth;
1720 if (!isl_ast_build_has_stride(build, data.depth))
1721 return create_node_scaled(executed, bounds, domain, build);
1723 offset = isl_ast_build_get_offset(build, data.depth);
1724 data.m = isl_ast_build_get_stride(build, data.depth);
1725 if (!data.m)
1726 offset = isl_aff_free(offset);
1727 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1728 d = isl_aff_get_denominator_val(offset);
1729 if (!d)
1730 executed = isl_union_map_free(executed);
1732 if (executed && isl_val_is_divisible_by(data.m, d))
1733 data.m = isl_val_div(data.m, d);
1734 else {
1735 data.m = isl_val_set_si(data.m, 1);
1736 isl_val_free(d);
1739 if (!isl_val_is_one(data.m)) {
1740 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1741 &data) < 0 &&
1742 !isl_val_is_one(data.m))
1743 executed = isl_union_map_free(executed);
1746 if (!isl_val_is_one(data.m)) {
1747 isl_space *space;
1748 isl_multi_aff *ma;
1749 isl_aff *aff;
1750 isl_map *map;
1751 isl_union_map *umap;
1753 space = isl_ast_build_get_space(build, 1);
1754 space = isl_space_map_from_set(space);
1755 ma = isl_multi_aff_identity(space);
1756 aff = isl_multi_aff_get_aff(ma, data.depth);
1757 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1758 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1760 bounds = isl_basic_set_preimage_multi_aff(bounds,
1761 isl_multi_aff_copy(ma));
1762 domain = isl_set_preimage_multi_aff(domain,
1763 isl_multi_aff_copy(ma));
1764 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1765 umap = isl_union_map_from_map(map);
1766 executed = isl_union_map_apply_domain(executed,
1767 isl_union_map_copy(umap));
1768 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1769 umap);
1771 isl_aff_free(offset);
1772 isl_val_free(data.m);
1774 return create_node_scaled(executed, bounds, domain, build);
1777 /* Add the basic set to the list that "user" points to.
1779 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1781 isl_basic_set_list **list = user;
1783 *list = isl_basic_set_list_add(*list, bset);
1785 return isl_stat_ok;
1788 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1790 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1791 __isl_take isl_set *set)
1793 isl_size n;
1794 isl_ctx *ctx;
1795 isl_basic_set_list *list;
1797 n = isl_set_n_basic_set(set);
1798 if (n < 0)
1799 set = isl_set_free(set);
1800 if (!set)
1801 return NULL;
1803 ctx = isl_set_get_ctx(set);
1805 list = isl_basic_set_list_alloc(ctx, n);
1806 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1807 list = isl_basic_set_list_free(list);
1809 isl_set_free(set);
1810 return list;
1813 /* Generate code for the schedule domain "bounds"
1814 * and add the result to "list".
1816 * We mainly detect strides here and check if the bounds do not
1817 * conflict with the current build domain
1818 * and then pass over control to create_node.
1820 * "bounds" reflects the bounds on the current dimension and possibly
1821 * some extra conditions on outer dimensions.
1822 * It does not, however, include any divs involving the current dimension,
1823 * so it does not capture any stride constraints.
1824 * We therefore need to compute that part of the schedule domain that
1825 * intersects with "bounds" and derive the strides from the result.
1827 static __isl_give isl_ast_graft_list *add_node(
1828 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1829 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1831 isl_ast_graft *graft;
1832 isl_set *domain = NULL;
1833 isl_union_set *uset;
1834 int empty, disjoint;
1836 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1837 executed = isl_union_map_intersect_domain(executed, uset);
1838 empty = isl_union_map_is_empty(executed);
1839 if (empty < 0)
1840 goto error;
1841 if (empty)
1842 goto done;
1844 uset = isl_union_map_domain(isl_union_map_copy(executed));
1845 domain = isl_set_from_union_set(uset);
1846 domain = isl_ast_build_specialize(build, domain);
1848 domain = isl_set_compute_divs(domain);
1849 domain = isl_ast_build_eliminate_inner(build, domain);
1850 disjoint = isl_set_is_disjoint(domain, build->domain);
1851 if (disjoint < 0)
1852 goto error;
1853 if (disjoint)
1854 goto done;
1856 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1858 graft = create_node(executed, bounds, domain,
1859 isl_ast_build_copy(build));
1860 list = isl_ast_graft_list_add(list, graft);
1861 isl_ast_build_free(build);
1862 return list;
1863 error:
1864 list = isl_ast_graft_list_free(list);
1865 done:
1866 isl_set_free(domain);
1867 isl_basic_set_free(bounds);
1868 isl_union_map_free(executed);
1869 isl_ast_build_free(build);
1870 return list;
1873 /* Does any element of i follow or coincide with any element of j
1874 * at the current depth for equal values of the outer dimensions?
1876 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1877 __isl_keep isl_basic_set *j, void *user)
1879 int depth = *(int *) user;
1880 isl_basic_map *test;
1881 isl_bool empty;
1882 int l;
1884 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1885 isl_basic_set_copy(j));
1886 for (l = 0; l < depth; ++l)
1887 test = isl_basic_map_equate(test, isl_dim_in, l,
1888 isl_dim_out, l);
1889 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1890 isl_dim_out, depth);
1891 empty = isl_basic_map_is_empty(test);
1892 isl_basic_map_free(test);
1894 return isl_bool_not(empty);
1897 /* Split up each element of "list" into a part that is related to "bset"
1898 * according to "gt" and a part that is not.
1899 * Return a list that consist of "bset" and all the pieces.
1901 static __isl_give isl_basic_set_list *add_split_on(
1902 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1903 __isl_keep isl_basic_map *gt)
1905 int i;
1906 isl_size n;
1907 isl_basic_set_list *res;
1909 n = isl_basic_set_list_n_basic_set(list);
1910 if (n < 0)
1911 bset = isl_basic_set_free(bset);
1913 gt = isl_basic_map_copy(gt);
1914 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1915 res = isl_basic_set_list_from_basic_set(bset);
1916 for (i = 0; res && i < n; ++i) {
1917 isl_basic_set *bset;
1918 isl_set *set1, *set2;
1919 isl_basic_map *bmap;
1920 int empty;
1922 bset = isl_basic_set_list_get_basic_set(list, i);
1923 bmap = isl_basic_map_copy(gt);
1924 bmap = isl_basic_map_intersect_range(bmap, bset);
1925 bset = isl_basic_map_range(bmap);
1926 empty = isl_basic_set_is_empty(bset);
1927 if (empty < 0)
1928 res = isl_basic_set_list_free(res);
1929 if (empty) {
1930 isl_basic_set_free(bset);
1931 bset = isl_basic_set_list_get_basic_set(list, i);
1932 res = isl_basic_set_list_add(res, bset);
1933 continue;
1936 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1937 set1 = isl_set_from_basic_set(bset);
1938 bset = isl_basic_set_list_get_basic_set(list, i);
1939 set2 = isl_set_from_basic_set(bset);
1940 set1 = isl_set_subtract(set2, set1);
1941 set1 = isl_set_make_disjoint(set1);
1943 res = isl_basic_set_list_concat(res,
1944 isl_basic_set_list_from_set(set1));
1946 isl_basic_map_free(gt);
1947 isl_basic_set_list_free(list);
1948 return res;
1951 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1952 __isl_keep isl_basic_set_list *domain_list,
1953 __isl_keep isl_union_map *executed,
1954 __isl_keep isl_ast_build *build);
1956 /* Internal data structure for add_nodes.
1958 * "executed" and "build" are extra arguments to be passed to add_node.
1959 * "list" collects the results.
1961 struct isl_add_nodes_data {
1962 isl_union_map *executed;
1963 isl_ast_build *build;
1965 isl_ast_graft_list *list;
1968 /* Generate code for the schedule domains in "scc"
1969 * and add the results to "list".
1971 * The domains in "scc" form a strongly connected component in the ordering.
1972 * If the number of domains in "scc" is larger than 1, then this means
1973 * that we cannot determine a valid ordering for the domains in the component.
1974 * This should be fairly rare because the individual domains
1975 * have been made disjoint first.
1976 * The problem is that the domains may be integrally disjoint but not
1977 * rationally disjoint. For example, we may have domains
1979 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1981 * These two domains have an empty intersection, but their rational
1982 * relaxations do intersect. It is impossible to order these domains
1983 * in the second dimension because the first should be ordered before
1984 * the second for outer dimension equal to 0, while it should be ordered
1985 * after for outer dimension equal to 1.
1987 * This may happen in particular in case of unrolling since the domain
1988 * of each slice is replaced by its simple hull.
1990 * For each basic set i in "scc" and for each of the following basic sets j,
1991 * we split off that part of the basic set i that shares the outer dimensions
1992 * with j and lies before j in the current dimension.
1993 * We collect all the pieces in a new list that replaces "scc".
1995 * While the elements in "scc" should be disjoint, we double-check
1996 * this property to avoid running into an infinite recursion in case
1997 * they intersect due to some internal error.
1999 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
2001 struct isl_add_nodes_data *data = user;
2002 int i;
2003 isl_size depth;
2004 isl_size n;
2005 isl_basic_set *bset, *first;
2006 isl_basic_set_list *list;
2007 isl_space *space;
2008 isl_basic_map *gt;
2010 n = isl_basic_set_list_n_basic_set(scc);
2011 if (n < 0)
2012 goto error;
2013 bset = isl_basic_set_list_get_basic_set(scc, 0);
2014 if (n == 1) {
2015 isl_basic_set_list_free(scc);
2016 data->list = add_node(data->list,
2017 isl_union_map_copy(data->executed), bset,
2018 isl_ast_build_copy(data->build));
2019 return data->list ? isl_stat_ok : isl_stat_error;
2022 depth = isl_ast_build_get_depth(data->build);
2023 if (depth < 0)
2024 bset = isl_basic_set_free(bset);
2025 space = isl_basic_set_get_space(bset);
2026 space = isl_space_map_from_set(space);
2027 gt = isl_basic_map_universe(space);
2028 for (i = 0; i < depth; ++i)
2029 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
2030 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
2032 first = isl_basic_set_copy(bset);
2033 list = isl_basic_set_list_from_basic_set(bset);
2034 for (i = 1; i < n; ++i) {
2035 int disjoint;
2037 bset = isl_basic_set_list_get_basic_set(scc, i);
2039 disjoint = isl_basic_set_is_disjoint(bset, first);
2040 if (disjoint < 0)
2041 list = isl_basic_set_list_free(list);
2042 else if (!disjoint)
2043 isl_die(isl_basic_set_list_get_ctx(scc),
2044 isl_error_internal,
2045 "basic sets in scc are assumed to be disjoint",
2046 list = isl_basic_set_list_free(list));
2048 list = add_split_on(list, bset, gt);
2050 isl_basic_set_free(first);
2051 isl_basic_map_free(gt);
2052 isl_basic_set_list_free(scc);
2053 scc = list;
2054 data->list = isl_ast_graft_list_concat(data->list,
2055 generate_sorted_domains(scc, data->executed, data->build));
2056 isl_basic_set_list_free(scc);
2058 return data->list ? isl_stat_ok : isl_stat_error;
2059 error:
2060 isl_basic_set_list_free(scc);
2061 return isl_stat_error;
2064 /* Sort the domains in "domain_list" according to the execution order
2065 * at the current depth (for equal values of the outer dimensions),
2066 * generate code for each of them, collecting the results in a list.
2067 * If no code is generated (because the intersection of the inverse schedule
2068 * with the domains turns out to be empty), then an empty list is returned.
2070 * The caller is responsible for ensuring that the basic sets in "domain_list"
2071 * are pair-wise disjoint. It can, however, in principle happen that
2072 * two basic sets should be ordered one way for one value of the outer
2073 * dimensions and the other way for some other value of the outer dimensions.
2074 * We therefore play safe and look for strongly connected components.
2075 * The function add_nodes takes care of handling non-trivial components.
2077 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2078 __isl_keep isl_basic_set_list *domain_list,
2079 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2081 isl_ctx *ctx;
2082 struct isl_add_nodes_data data;
2083 isl_size depth;
2084 isl_size n;
2086 n = isl_basic_set_list_n_basic_set(domain_list);
2087 if (n < 0)
2088 return NULL;
2090 ctx = isl_basic_set_list_get_ctx(domain_list);
2091 data.list = isl_ast_graft_list_alloc(ctx, n);
2092 if (n == 0)
2093 return data.list;
2094 if (n == 1)
2095 return add_node(data.list, isl_union_map_copy(executed),
2096 isl_basic_set_list_get_basic_set(domain_list, 0),
2097 isl_ast_build_copy(build));
2099 depth = isl_ast_build_get_depth(build);
2100 data.executed = executed;
2101 data.build = build;
2102 if (depth < 0 || isl_basic_set_list_foreach_scc(domain_list,
2103 &domain_follows_at_depth, &depth,
2104 &add_nodes, &data) < 0)
2105 data.list = isl_ast_graft_list_free(data.list);
2107 return data.list;
2110 /* Do i and j share any values for the outer dimensions?
2112 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2113 __isl_keep isl_basic_set *j, void *user)
2115 int depth = *(int *) user;
2116 isl_basic_map *test;
2117 isl_bool empty;
2118 int l;
2120 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2121 isl_basic_set_copy(j));
2122 for (l = 0; l < depth; ++l)
2123 test = isl_basic_map_equate(test, isl_dim_in, l,
2124 isl_dim_out, l);
2125 empty = isl_basic_map_is_empty(test);
2126 isl_basic_map_free(test);
2128 return isl_bool_not(empty);
2131 /* Internal data structure for generate_sorted_domains_wrap.
2133 * "n" is the total number of basic sets
2134 * "executed" and "build" are extra arguments to be passed
2135 * to generate_sorted_domains.
2137 * "single" is set to 1 by generate_sorted_domains_wrap if there
2138 * is only a single component.
2139 * "list" collects the results.
2141 struct isl_ast_generate_parallel_domains_data {
2142 isl_size n;
2143 isl_union_map *executed;
2144 isl_ast_build *build;
2146 int single;
2147 isl_ast_graft_list *list;
2150 /* Call generate_sorted_domains on "scc", fuse the result into a list
2151 * with either zero or one graft and collect the these single element
2152 * lists into data->list.
2154 * If there is only one component, i.e., if the number of basic sets
2155 * in the current component is equal to the total number of basic sets,
2156 * then data->single is set to 1 and the result of generate_sorted_domains
2157 * is not fused.
2159 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2160 void *user)
2162 struct isl_ast_generate_parallel_domains_data *data = user;
2163 isl_ast_graft_list *list;
2164 isl_size n;
2166 n = isl_basic_set_list_n_basic_set(scc);
2167 if (n < 0)
2168 scc = isl_basic_set_list_free(scc);
2169 list = generate_sorted_domains(scc, data->executed, data->build);
2170 data->single = n == data->n;
2171 if (!data->single)
2172 list = isl_ast_graft_list_fuse(list, data->build);
2173 if (!data->list)
2174 data->list = list;
2175 else
2176 data->list = isl_ast_graft_list_concat(data->list, list);
2178 isl_basic_set_list_free(scc);
2179 if (!data->list)
2180 return isl_stat_error;
2182 return isl_stat_ok;
2185 /* Look for any (weakly connected) components in the "domain_list"
2186 * of domains that share some values of the outer dimensions.
2187 * That is, domains in different components do not share any values
2188 * of the outer dimensions. This means that these components
2189 * can be freely reordered.
2190 * Within each of the components, we sort the domains according
2191 * to the execution order at the current depth.
2193 * If there is more than one component, then generate_sorted_domains_wrap
2194 * fuses the result of each call to generate_sorted_domains
2195 * into a list with either zero or one graft and collects these (at most)
2196 * single element lists into a bigger list. This means that the elements of the
2197 * final list can be freely reordered. In particular, we sort them
2198 * according to an arbitrary but fixed ordering to ease merging of
2199 * graft lists from different components.
2201 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2202 __isl_keep isl_basic_set_list *domain_list,
2203 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2205 isl_size depth;
2206 struct isl_ast_generate_parallel_domains_data data;
2208 data.n = isl_basic_set_list_n_basic_set(domain_list);
2209 if (data.n < 0)
2210 return NULL;
2212 if (data.n <= 1)
2213 return generate_sorted_domains(domain_list, executed, build);
2215 depth = isl_ast_build_get_depth(build);
2216 if (depth < 0)
2217 return NULL;
2218 data.list = NULL;
2219 data.executed = executed;
2220 data.build = build;
2221 data.single = 0;
2222 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2223 &generate_sorted_domains_wrap,
2224 &data) < 0)
2225 data.list = isl_ast_graft_list_free(data.list);
2227 if (!data.single)
2228 data.list = isl_ast_graft_list_sort_guard(data.list);
2230 return data.list;
2233 /* Internal data for separate_domain.
2235 * "explicit" is set if we only want to use explicit bounds.
2237 * "domain" collects the separated domains.
2239 struct isl_separate_domain_data {
2240 isl_ast_build *build;
2241 int explicit;
2242 isl_set *domain;
2245 /* Extract implicit bounds on the current dimension for the executed "map".
2247 * The domain of "map" may involve inner dimensions, so we
2248 * need to eliminate them.
2250 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2251 __isl_keep isl_ast_build *build)
2253 isl_set *domain;
2255 domain = isl_map_domain(map);
2256 domain = isl_ast_build_eliminate(build, domain);
2258 return domain;
2261 /* Extract explicit bounds on the current dimension for the executed "map".
2263 * Rather than eliminating the inner dimensions as in implicit_bounds,
2264 * we simply drop any constraints involving those inner dimensions.
2265 * The idea is that most bounds that are implied by constraints on the
2266 * inner dimensions will be enforced by for loops and not by explicit guards.
2267 * There is then no need to separate along those bounds.
2269 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2270 __isl_keep isl_ast_build *build)
2272 isl_set *domain;
2273 isl_size depth;
2274 isl_size dim;
2276 depth = isl_ast_build_get_depth(build);
2277 dim = isl_map_dim(map, isl_dim_out);
2278 if (depth < 0 || dim < 0)
2279 return isl_map_domain(isl_map_free(map));
2280 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2282 domain = isl_map_domain(map);
2283 dim = isl_set_dim(domain, isl_dim_set);
2284 domain = isl_set_detect_equalities(domain);
2285 domain = isl_set_drop_constraints_involving_dims(domain,
2286 isl_dim_set, depth + 1, dim - (depth + 1));
2287 domain = isl_set_remove_divs_involving_dims(domain,
2288 isl_dim_set, depth, 1);
2289 domain = isl_set_remove_unknown_divs(domain);
2291 return domain;
2294 /* Split data->domain into pieces that intersect with the range of "map"
2295 * and pieces that do not intersect with the range of "map"
2296 * and then add that part of the range of "map" that does not intersect
2297 * with data->domain.
2299 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2301 struct isl_separate_domain_data *data = user;
2302 isl_set *domain;
2303 isl_set *d1, *d2;
2305 if (data->explicit)
2306 domain = explicit_bounds(map, data->build);
2307 else
2308 domain = implicit_bounds(map, data->build);
2310 domain = isl_set_coalesce(domain);
2311 domain = isl_set_make_disjoint(domain);
2312 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2313 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2314 data->domain = isl_set_intersect(data->domain, domain);
2315 data->domain = isl_set_union(data->domain, d1);
2316 data->domain = isl_set_union(data->domain, d2);
2318 return isl_stat_ok;
2321 /* Separate the schedule domains of "executed".
2323 * That is, break up the domain of "executed" into basic sets,
2324 * such that for each basic set S, every element in S is associated with
2325 * the same domain spaces.
2327 * "space" is the (single) domain space of "executed".
2329 static __isl_give isl_set *separate_schedule_domains(
2330 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2331 __isl_keep isl_ast_build *build)
2333 struct isl_separate_domain_data data = { build };
2334 isl_ctx *ctx;
2336 ctx = isl_ast_build_get_ctx(build);
2337 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2338 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2339 data.domain = isl_set_empty(space);
2340 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2341 data.domain = isl_set_free(data.domain);
2343 isl_union_map_free(executed);
2344 return data.domain;
2347 /* Temporary data used during the search for a lower bound for unrolling.
2349 * "build" is the build in which the unrolling will be performed
2350 * "domain" is the original set for which to find a lower bound
2351 * "depth" is the dimension for which to find a lower boudn
2352 * "expansion" is the expansion that needs to be applied to "domain"
2353 * in the unrolling that will be performed
2355 * "lower" is the best lower bound found so far. It is NULL if we have not
2356 * found any yet.
2357 * "n" is the corresponding size. If lower is NULL, then the value of n
2358 * is undefined.
2359 * "n_div" is the maximal number of integer divisions in the first
2360 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2361 * been computed yet.
2363 struct isl_find_unroll_data {
2364 isl_ast_build *build;
2365 isl_set *domain;
2366 int depth;
2367 isl_basic_map *expansion;
2369 isl_aff *lower;
2370 int *n;
2371 int n_div;
2374 /* Return the constraint
2376 * i_"depth" = aff + offset
2378 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2379 int offset)
2381 aff = isl_aff_copy(aff);
2382 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2383 aff = isl_aff_add_constant_si(aff, offset);
2384 return isl_equality_from_aff(aff);
2387 /* Update *user to the number of integer divisions in the first element
2388 * of "ma", if it is larger than the current value.
2390 static isl_stat update_n_div(__isl_take isl_set *set,
2391 __isl_take isl_multi_aff *ma, void *user)
2393 isl_aff *aff;
2394 int *n = user;
2395 isl_size n_div;
2397 aff = isl_multi_aff_get_aff(ma, 0);
2398 n_div = isl_aff_dim(aff, isl_dim_div);
2399 isl_aff_free(aff);
2400 isl_multi_aff_free(ma);
2401 isl_set_free(set);
2403 if (n_div > *n)
2404 *n = n_div;
2406 return n_div >= 0 ? isl_stat_ok : isl_stat_error;
2409 /* Get the number of integer divisions in the expression for the iterator
2410 * value at the first slice in the unrolling based on lower bound "lower",
2411 * taking into account the expansion that needs to be performed on this slice.
2413 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2414 __isl_keep isl_aff *lower)
2416 isl_constraint *c;
2417 isl_set *set;
2418 isl_map *it_map, *expansion;
2419 isl_pw_multi_aff *pma;
2420 int n;
2422 c = at_offset(data->depth, lower, 0);
2423 set = isl_set_copy(data->domain);
2424 set = isl_set_add_constraint(set, c);
2425 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2426 set = isl_set_apply(set, expansion);
2427 it_map = isl_ast_build_map_to_iterator(data->build, set);
2428 pma = isl_pw_multi_aff_from_map(it_map);
2429 n = 0;
2430 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2431 n = -1;
2432 isl_pw_multi_aff_free(pma);
2434 return n;
2437 /* Is the lower bound "lower" with corresponding iteration count "n"
2438 * better than the one stored in "data"?
2439 * If there is no upper bound on the iteration count ("n" is infinity) or
2440 * if the count is too large, then we cannot use this lower bound.
2441 * Otherwise, if there was no previous lower bound or
2442 * if the iteration count of the new lower bound is smaller than
2443 * the iteration count of the previous lower bound, then we consider
2444 * the new lower bound to be better.
2445 * If the iteration count is the same, then compare the number
2446 * of integer divisions that would be needed to express
2447 * the iterator value at the first slice in the unrolling
2448 * according to the lower bound. If we end up computing this
2449 * number, then store the lowest value in data->n_div.
2451 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2452 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2454 int cmp;
2455 int n_div;
2457 if (!n)
2458 return -1;
2459 if (isl_val_is_infty(n))
2460 return 0;
2461 if (isl_val_cmp_si(n, INT_MAX) > 0)
2462 return 0;
2463 if (!data->lower)
2464 return 1;
2465 cmp = isl_val_cmp_si(n, *data->n);
2466 if (cmp < 0)
2467 return 1;
2468 if (cmp > 0)
2469 return 0;
2470 if (data->n_div < 0)
2471 data->n_div = get_expanded_n_div(data, data->lower);
2472 if (data->n_div < 0)
2473 return -1;
2474 if (data->n_div == 0)
2475 return 0;
2476 n_div = get_expanded_n_div(data, lower);
2477 if (n_div < 0)
2478 return -1;
2479 if (n_div >= data->n_div)
2480 return 0;
2481 data->n_div = n_div;
2483 return 1;
2486 /* Check if we can use "c" as a lower bound and if it is better than
2487 * any previously found lower bound.
2489 * If "c" does not involve the dimension at the current depth,
2490 * then we cannot use it.
2491 * Otherwise, let "c" be of the form
2493 * i >= f(j)/a
2495 * We compute the maximal value of
2497 * -ceil(f(j)/a)) + i + 1
2499 * over the domain. If there is such a value "n", then we know
2501 * -ceil(f(j)/a)) + i + 1 <= n
2503 * or
2505 * i < ceil(f(j)/a)) + n
2507 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2508 * We just need to check if we have found any lower bound before and
2509 * if the new lower bound is better (smaller n or fewer integer divisions)
2510 * than the previously found lower bounds.
2512 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2513 __isl_keep isl_constraint *c)
2515 isl_aff *aff, *lower;
2516 isl_val *max;
2517 int better;
2519 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2520 return isl_stat_ok;
2522 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2523 lower = isl_aff_ceil(lower);
2524 aff = isl_aff_copy(lower);
2525 aff = isl_aff_neg(aff);
2526 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2527 aff = isl_aff_add_constant_si(aff, 1);
2528 max = isl_set_max_val(data->domain, aff);
2529 isl_aff_free(aff);
2531 better = is_better_lower_bound(data, lower, max);
2532 if (better < 0 || !better) {
2533 isl_val_free(max);
2534 isl_aff_free(lower);
2535 return better < 0 ? isl_stat_error : isl_stat_ok;
2538 isl_aff_free(data->lower);
2539 data->lower = lower;
2540 *data->n = isl_val_get_num_si(max);
2541 isl_val_free(max);
2543 return isl_stat_ok;
2546 /* Check if we can use "c" as a lower bound and if it is better than
2547 * any previously found lower bound.
2549 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2551 struct isl_find_unroll_data *data;
2552 isl_stat r;
2554 data = (struct isl_find_unroll_data *) user;
2555 r = update_unrolling_lower_bound(data, c);
2556 isl_constraint_free(c);
2558 return r;
2561 /* Look for a lower bound l(i) on the dimension at "depth"
2562 * and a size n such that "domain" is a subset of
2564 * { [i] : l(i) <= i_d < l(i) + n }
2566 * where d is "depth" and l(i) depends only on earlier dimensions.
2567 * Furthermore, try and find a lower bound such that n is as small as possible.
2568 * In particular, "n" needs to be finite.
2569 * "build" is the build in which the unrolling will be performed.
2570 * "expansion" is the expansion that needs to be applied to "domain"
2571 * in the unrolling that will be performed.
2573 * Inner dimensions have been eliminated from "domain" by the caller.
2575 * We first construct a collection of lower bounds on the input set
2576 * by computing its simple hull. We then iterate through them,
2577 * discarding those that we cannot use (either because they do not
2578 * involve the dimension at "depth" or because they have no corresponding
2579 * upper bound, meaning that "n" would be unbounded) and pick out the
2580 * best from the remaining ones.
2582 * If we cannot find a suitable lower bound, then we consider that
2583 * to be an error.
2585 static __isl_give isl_aff *find_unroll_lower_bound(
2586 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2587 int depth, __isl_keep isl_basic_map *expansion, int *n)
2589 struct isl_find_unroll_data data =
2590 { build, domain, depth, expansion, NULL, n, -1 };
2591 isl_basic_set *hull;
2593 hull = isl_set_simple_hull(isl_set_copy(domain));
2595 if (isl_basic_set_foreach_constraint(hull,
2596 &constraint_find_unroll, &data) < 0)
2597 goto error;
2599 isl_basic_set_free(hull);
2601 if (!data.lower)
2602 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2603 "cannot find lower bound for unrolling", return NULL);
2605 return data.lower;
2606 error:
2607 isl_basic_set_free(hull);
2608 return isl_aff_free(data.lower);
2611 /* Call "fn" on each iteration of the current dimension of "domain".
2612 * If "init" is not NULL, then it is called with the number of
2613 * iterations before any call to "fn".
2614 * Return -1 on failure.
2616 * Since we are going to be iterating over the individual values,
2617 * we first check if there are any strides on the current dimension.
2618 * If there is, we rewrite the current dimension i as
2620 * i = stride i' + offset
2622 * and then iterate over individual values of i' instead.
2624 * We then look for a lower bound on i' and a size such that the domain
2625 * is a subset of
2627 * { [j,i'] : l(j) <= i' < l(j) + n }
2629 * and then take slices of the domain at values of i'
2630 * between l(j) and l(j) + n - 1.
2632 * We compute the unshifted simple hull of each slice to ensure that
2633 * we have a single basic set per offset. The slicing constraint
2634 * may get simplified away before the unshifted simple hull is taken
2635 * and may therefore in some rare cases disappear from the result.
2636 * We therefore explicitly add the constraint back after computing
2637 * the unshifted simple hull to ensure that the basic sets
2638 * remain disjoint. The constraints that are dropped by taking the hull
2639 * will be taken into account at the next level, as in the case of the
2640 * atomic option.
2642 * Finally, we map i' back to i and call "fn".
2644 static int foreach_iteration(__isl_take isl_set *domain,
2645 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2646 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2648 int i, n;
2649 isl_bool empty;
2650 isl_size depth;
2651 isl_multi_aff *expansion;
2652 isl_basic_map *bmap;
2653 isl_aff *lower = NULL;
2654 isl_ast_build *stride_build;
2656 depth = isl_ast_build_get_depth(build);
2657 if (depth < 0)
2658 domain = isl_set_free(domain);
2660 domain = isl_ast_build_eliminate_inner(build, domain);
2661 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2662 stride_build = isl_ast_build_copy(build);
2663 stride_build = isl_ast_build_detect_strides(stride_build,
2664 isl_set_copy(domain));
2665 expansion = isl_ast_build_get_stride_expansion(stride_build);
2667 domain = isl_set_preimage_multi_aff(domain,
2668 isl_multi_aff_copy(expansion));
2669 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2670 isl_ast_build_free(stride_build);
2672 bmap = isl_basic_map_from_multi_aff(expansion);
2674 empty = isl_set_is_empty(domain);
2675 if (empty < 0) {
2676 n = -1;
2677 } else if (empty) {
2678 n = 0;
2679 } else {
2680 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2681 if (!lower)
2682 n = -1;
2684 if (n >= 0 && init && init(n, user) < 0)
2685 n = -1;
2686 for (i = 0; i < n; ++i) {
2687 isl_set *set;
2688 isl_basic_set *bset;
2689 isl_constraint *slice;
2691 slice = at_offset(depth, lower, i);
2692 set = isl_set_copy(domain);
2693 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2694 bset = isl_set_unshifted_simple_hull(set);
2695 bset = isl_basic_set_add_constraint(bset, slice);
2696 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2698 if (fn(bset, user) < 0)
2699 break;
2702 isl_aff_free(lower);
2703 isl_set_free(domain);
2704 isl_basic_map_free(bmap);
2706 return n < 0 || i < n ? -1 : 0;
2709 /* Data structure for storing the results and the intermediate objects
2710 * of compute_domains.
2712 * "list" is the main result of the function and contains a list
2713 * of disjoint basic sets for which code should be generated.
2715 * "executed" and "build" are inputs to compute_domains.
2716 * "schedule_domain" is the domain of "executed".
2718 * "option" contains the domains at the current depth that should by
2719 * atomic, separated or unrolled. These domains are as specified by
2720 * the user, except that inner dimensions have been eliminated and
2721 * that they have been made pair-wise disjoint.
2723 * "sep_class" contains the user-specified split into separation classes
2724 * specialized to the current depth.
2725 * "done" contains the union of the separation domains that have already
2726 * been handled.
2728 struct isl_codegen_domains {
2729 isl_basic_set_list *list;
2731 isl_union_map *executed;
2732 isl_ast_build *build;
2733 isl_set *schedule_domain;
2735 isl_set *option[4];
2737 isl_map *sep_class;
2738 isl_set *done;
2741 /* Internal data structure for do_unroll.
2743 * "domains" stores the results of compute_domains.
2744 * "class_domain" is the original class domain passed to do_unroll.
2745 * "unroll_domain" collects the unrolled iterations.
2747 struct isl_ast_unroll_data {
2748 struct isl_codegen_domains *domains;
2749 isl_set *class_domain;
2750 isl_set *unroll_domain;
2753 /* Given an iteration of an unrolled domain represented by "bset",
2754 * add it to data->domains->list.
2755 * Since we may have dropped some constraints, we intersect with
2756 * the class domain again to ensure that each element in the list
2757 * is disjoint from the other class domains.
2759 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2761 struct isl_ast_unroll_data *data = user;
2762 isl_set *set;
2763 isl_basic_set_list *list;
2765 set = isl_set_from_basic_set(bset);
2766 data->unroll_domain = isl_set_union(data->unroll_domain,
2767 isl_set_copy(set));
2768 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2769 set = isl_set_make_disjoint(set);
2770 list = isl_basic_set_list_from_set(set);
2771 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2772 list);
2774 return 0;
2777 /* Extend domains->list with a list of basic sets, one for each value
2778 * of the current dimension in "domain" and remove the corresponding
2779 * sets from the class domain. Return the updated class domain.
2780 * The divs that involve the current dimension have not been projected out
2781 * from this domain.
2783 * We call foreach_iteration to iterate over the individual values and
2784 * in do_unroll_iteration we collect the individual basic sets in
2785 * domains->list and their union in data->unroll_domain, which is then
2786 * used to update the class domain.
2788 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2789 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2791 struct isl_ast_unroll_data data;
2793 if (!domain)
2794 return isl_set_free(class_domain);
2795 if (!class_domain)
2796 return isl_set_free(domain);
2798 data.domains = domains;
2799 data.class_domain = class_domain;
2800 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2802 if (foreach_iteration(domain, domains->build, NULL,
2803 &do_unroll_iteration, &data) < 0)
2804 data.unroll_domain = isl_set_free(data.unroll_domain);
2806 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2808 return class_domain;
2811 /* Add domains to domains->list for each individual value of the current
2812 * dimension, for that part of the schedule domain that lies in the
2813 * intersection of the option domain and the class domain.
2814 * Remove the corresponding sets from the class domain and
2815 * return the updated class domain.
2817 * We first break up the unroll option domain into individual pieces
2818 * and then handle each of them separately. The unroll option domain
2819 * has been made disjoint in compute_domains_init_options,
2821 * Note that we actively want to combine different pieces of the
2822 * schedule domain that have the same value at the current dimension.
2823 * We therefore need to break up the unroll option domain before
2824 * intersecting with class and schedule domain, hoping that the
2825 * unroll option domain specified by the user is relatively simple.
2827 static __isl_give isl_set *compute_unroll_domains(
2828 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2830 isl_set *unroll_domain;
2831 isl_basic_set_list *unroll_list;
2832 int i;
2833 isl_size n;
2834 isl_bool empty;
2836 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2837 if (empty < 0)
2838 return isl_set_free(class_domain);
2839 if (empty)
2840 return class_domain;
2842 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2843 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2845 n = isl_basic_set_list_n_basic_set(unroll_list);
2846 if (n < 0)
2847 class_domain = isl_set_free(class_domain);
2848 for (i = 0; i < n; ++i) {
2849 isl_basic_set *bset;
2851 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2852 unroll_domain = isl_set_from_basic_set(bset);
2853 unroll_domain = isl_set_intersect(unroll_domain,
2854 isl_set_copy(class_domain));
2855 unroll_domain = isl_set_intersect(unroll_domain,
2856 isl_set_copy(domains->schedule_domain));
2858 empty = isl_set_is_empty(unroll_domain);
2859 if (empty >= 0 && empty) {
2860 isl_set_free(unroll_domain);
2861 continue;
2864 class_domain = do_unroll(domains, unroll_domain, class_domain);
2867 isl_basic_set_list_free(unroll_list);
2869 return class_domain;
2872 /* Try and construct a single basic set that includes the intersection of
2873 * the schedule domain, the atomic option domain and the class domain.
2874 * Add the resulting basic set(s) to domains->list and remove them
2875 * from class_domain. Return the updated class domain.
2877 * We construct a single domain rather than trying to combine
2878 * the schedule domains of individual domains because we are working
2879 * within a single component so that non-overlapping schedule domains
2880 * should already have been separated.
2881 * We do however need to make sure that this single domains is a subset
2882 * of the class domain so that it would not intersect with any other
2883 * class domains. This means that we may end up splitting up the atomic
2884 * domain in case separation classes are being used.
2886 * "domain" is the intersection of the schedule domain and the class domain,
2887 * with inner dimensions projected out.
2889 static __isl_give isl_set *compute_atomic_domain(
2890 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2892 isl_basic_set *bset;
2893 isl_basic_set_list *list;
2894 isl_set *domain, *atomic_domain;
2895 int empty;
2897 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2898 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2899 domain = isl_set_intersect(domain,
2900 isl_set_copy(domains->schedule_domain));
2901 empty = isl_set_is_empty(domain);
2902 if (empty < 0)
2903 class_domain = isl_set_free(class_domain);
2904 if (empty) {
2905 isl_set_free(domain);
2906 return class_domain;
2909 domain = isl_ast_build_eliminate(domains->build, domain);
2910 domain = isl_set_coalesce_preserve(domain);
2911 bset = isl_set_unshifted_simple_hull(domain);
2912 domain = isl_set_from_basic_set(bset);
2913 atomic_domain = isl_set_copy(domain);
2914 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2915 class_domain = isl_set_subtract(class_domain, atomic_domain);
2916 domain = isl_set_make_disjoint(domain);
2917 list = isl_basic_set_list_from_set(domain);
2918 domains->list = isl_basic_set_list_concat(domains->list, list);
2920 return class_domain;
2923 /* Split up the schedule domain into uniform basic sets,
2924 * in the sense that each element in a basic set is associated to
2925 * elements of the same domains, and add the result to domains->list.
2926 * Do this for that part of the schedule domain that lies in the
2927 * intersection of "class_domain" and the separate option domain.
2929 * "class_domain" may or may not include the constraints
2930 * of the schedule domain, but this does not make a difference
2931 * since we are going to intersect it with the domain of the inverse schedule.
2932 * If it includes schedule domain constraints, then they may involve
2933 * inner dimensions, but we will eliminate them in separation_domain.
2935 static int compute_separate_domain(struct isl_codegen_domains *domains,
2936 __isl_keep isl_set *class_domain)
2938 isl_space *space;
2939 isl_set *domain;
2940 isl_union_map *executed;
2941 isl_basic_set_list *list;
2942 int empty;
2944 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2945 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2946 executed = isl_union_map_copy(domains->executed);
2947 executed = isl_union_map_intersect_domain(executed,
2948 isl_union_set_from_set(domain));
2949 empty = isl_union_map_is_empty(executed);
2950 if (empty < 0 || empty) {
2951 isl_union_map_free(executed);
2952 return empty < 0 ? -1 : 0;
2955 space = isl_set_get_space(class_domain);
2956 domain = separate_schedule_domains(space, executed, domains->build);
2958 list = isl_basic_set_list_from_set(domain);
2959 domains->list = isl_basic_set_list_concat(domains->list, list);
2961 return 0;
2964 /* Split up the domain at the current depth into disjoint
2965 * basic sets for which code should be generated separately
2966 * for the given separation class domain.
2968 * If any separation classes have been defined, then "class_domain"
2969 * is the domain of the current class and does not refer to inner dimensions.
2970 * Otherwise, "class_domain" is the universe domain.
2972 * We first make sure that the class domain is disjoint from
2973 * previously considered class domains.
2975 * The separate domains can be computed directly from the "class_domain".
2977 * The unroll, atomic and remainder domains need the constraints
2978 * from the schedule domain.
2980 * For unrolling, the actual schedule domain is needed (with divs that
2981 * may refer to the current dimension) so that stride detection can be
2982 * performed.
2984 * For atomic and remainder domains, inner dimensions and divs involving
2985 * the current dimensions should be eliminated.
2986 * In case we are working within a separation class, we need to intersect
2987 * the result with the current "class_domain" to ensure that the domains
2988 * are disjoint from those generated from other class domains.
2990 * The domain that has been made atomic may be larger than specified
2991 * by the user since it needs to be representable as a single basic set.
2992 * This possibly larger domain is removed from class_domain by
2993 * compute_atomic_domain. It is computed first so that the extended domain
2994 * would not overlap with any domains computed before.
2995 * Similary, the unrolled domains may have some constraints removed and
2996 * may therefore also be larger than specified by the user.
2998 * If anything is left after handling separate, unroll and atomic,
2999 * we split it up into basic sets and append the basic sets to domains->list.
3001 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
3002 __isl_take isl_set *class_domain)
3004 isl_basic_set_list *list;
3005 isl_set *domain;
3007 class_domain = isl_set_subtract(class_domain,
3008 isl_set_copy(domains->done));
3009 domains->done = isl_set_union(domains->done,
3010 isl_set_copy(class_domain));
3012 class_domain = compute_atomic_domain(domains, class_domain);
3013 class_domain = compute_unroll_domains(domains, class_domain);
3015 domain = isl_set_copy(class_domain);
3017 if (compute_separate_domain(domains, domain) < 0)
3018 goto error;
3019 domain = isl_set_subtract(domain,
3020 isl_set_copy(domains->option[isl_ast_loop_separate]));
3022 domain = isl_set_intersect(domain,
3023 isl_set_copy(domains->schedule_domain));
3025 domain = isl_ast_build_eliminate(domains->build, domain);
3026 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
3028 domain = isl_set_coalesce_preserve(domain);
3029 domain = isl_set_make_disjoint(domain);
3031 list = isl_basic_set_list_from_set(domain);
3032 domains->list = isl_basic_set_list_concat(domains->list, list);
3034 isl_set_free(class_domain);
3036 return isl_stat_ok;
3037 error:
3038 isl_set_free(domain);
3039 isl_set_free(class_domain);
3040 return isl_stat_error;
3043 /* Split up the domain at the current depth into disjoint
3044 * basic sets for which code should be generated separately
3045 * for the separation class identified by "pnt".
3047 * We extract the corresponding class domain from domains->sep_class,
3048 * eliminate inner dimensions and pass control to compute_partial_domains.
3050 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
3052 struct isl_codegen_domains *domains = user;
3053 isl_set *class_set;
3054 isl_set *domain;
3055 int disjoint;
3057 class_set = isl_set_from_point(pnt);
3058 domain = isl_map_domain(isl_map_intersect_range(
3059 isl_map_copy(domains->sep_class), class_set));
3060 domain = isl_ast_build_compute_gist(domains->build, domain);
3061 domain = isl_ast_build_eliminate(domains->build, domain);
3063 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
3064 if (disjoint < 0)
3065 return isl_stat_error;
3066 if (disjoint) {
3067 isl_set_free(domain);
3068 return isl_stat_ok;
3071 return compute_partial_domains(domains, domain);
3074 /* Extract the domains at the current depth that should be atomic,
3075 * separated or unrolled and store them in option.
3077 * The domains specified by the user might overlap, so we make
3078 * them disjoint by subtracting earlier domains from later domains.
3080 static void compute_domains_init_options(isl_set *option[4],
3081 __isl_keep isl_ast_build *build)
3083 enum isl_ast_loop_type type, type2;
3084 isl_set *unroll;
3086 for (type = isl_ast_loop_atomic;
3087 type <= isl_ast_loop_separate; ++type) {
3088 option[type] = isl_ast_build_get_option_domain(build, type);
3089 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3090 option[type] = isl_set_subtract(option[type],
3091 isl_set_copy(option[type2]));
3094 unroll = option[isl_ast_loop_unroll];
3095 unroll = isl_set_coalesce(unroll);
3096 unroll = isl_set_make_disjoint(unroll);
3097 option[isl_ast_loop_unroll] = unroll;
3100 /* Split up the domain at the current depth into disjoint
3101 * basic sets for which code should be generated separately,
3102 * based on the user-specified options.
3103 * Return the list of disjoint basic sets.
3105 * There are three kinds of domains that we need to keep track of.
3106 * - the "schedule domain" is the domain of "executed"
3107 * - the "class domain" is the domain corresponding to the currrent
3108 * separation class
3109 * - the "option domain" is the domain corresponding to one of the options
3110 * atomic, unroll or separate
3112 * We first consider the individial values of the separation classes
3113 * and split up the domain for each of them separately.
3114 * Finally, we consider the remainder. If no separation classes were
3115 * specified, then we call compute_partial_domains with the universe
3116 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3117 * with inner dimensions removed. We do this because we want to
3118 * avoid computing the complement of the class domains (i.e., the difference
3119 * between the universe and domains->done).
3121 static __isl_give isl_basic_set_list *compute_domains(
3122 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3124 struct isl_codegen_domains domains;
3125 isl_ctx *ctx;
3126 isl_set *domain;
3127 isl_union_set *schedule_domain;
3128 isl_set *classes;
3129 isl_space *space;
3130 int n_param;
3131 enum isl_ast_loop_type type;
3132 isl_bool empty;
3134 if (!executed)
3135 return NULL;
3137 ctx = isl_union_map_get_ctx(executed);
3138 domains.list = isl_basic_set_list_alloc(ctx, 0);
3140 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3141 domain = isl_set_from_union_set(schedule_domain);
3143 compute_domains_init_options(domains.option, build);
3145 domains.sep_class = isl_ast_build_get_separation_class(build);
3146 classes = isl_map_range(isl_map_copy(domains.sep_class));
3147 n_param = isl_set_dim(classes, isl_dim_param);
3148 if (n_param < 0)
3149 classes = isl_set_free(classes);
3150 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3152 space = isl_set_get_space(domain);
3153 domains.build = build;
3154 domains.schedule_domain = isl_set_copy(domain);
3155 domains.executed = executed;
3156 domains.done = isl_set_empty(space);
3158 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3159 domains.list = isl_basic_set_list_free(domains.list);
3160 isl_set_free(classes);
3162 empty = isl_set_is_empty(domains.done);
3163 if (empty < 0) {
3164 domains.list = isl_basic_set_list_free(domains.list);
3165 domain = isl_set_free(domain);
3166 } else if (empty) {
3167 isl_set_free(domain);
3168 domain = isl_set_universe(isl_set_get_space(domains.done));
3169 } else {
3170 domain = isl_ast_build_eliminate(build, domain);
3172 if (compute_partial_domains(&domains, domain) < 0)
3173 domains.list = isl_basic_set_list_free(domains.list);
3175 isl_set_free(domains.schedule_domain);
3176 isl_set_free(domains.done);
3177 isl_map_free(domains.sep_class);
3178 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3179 isl_set_free(domains.option[type]);
3181 return domains.list;
3184 /* Generate code for a single component, after shifting (if any)
3185 * has been applied, in case the schedule was specified as a union map.
3187 * We first split up the domain at the current depth into disjoint
3188 * basic sets based on the user-specified options.
3189 * Then we generated code for each of them and concatenate the results.
3191 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3192 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3194 isl_basic_set_list *domain_list;
3195 isl_ast_graft_list *list = NULL;
3197 domain_list = compute_domains(executed, build);
3198 list = generate_parallel_domains(domain_list, executed, build);
3200 isl_basic_set_list_free(domain_list);
3201 isl_union_map_free(executed);
3202 isl_ast_build_free(build);
3204 return list;
3207 /* Generate code for a single component, after shifting (if any)
3208 * has been applied, in case the schedule was specified as a schedule tree
3209 * and the separate option was specified.
3211 * We perform separation on the domain of "executed" and then generate
3212 * an AST for each of the resulting disjoint basic sets.
3214 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3215 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3217 isl_space *space;
3218 isl_set *domain;
3219 isl_basic_set_list *domain_list;
3220 isl_ast_graft_list *list;
3222 space = isl_ast_build_get_space(build, 1);
3223 domain = separate_schedule_domains(space,
3224 isl_union_map_copy(executed), build);
3225 domain_list = isl_basic_set_list_from_set(domain);
3227 list = generate_parallel_domains(domain_list, executed, build);
3229 isl_basic_set_list_free(domain_list);
3230 isl_union_map_free(executed);
3231 isl_ast_build_free(build);
3233 return list;
3236 /* Internal data structure for generate_shifted_component_tree_unroll.
3238 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3239 * "list" collects the constructs grafts.
3241 struct isl_ast_unroll_tree_data {
3242 isl_union_map *executed;
3243 isl_ast_build *build;
3244 isl_ast_graft_list *list;
3247 /* Initialize data->list to a list of "n" elements.
3249 static int init_unroll_tree(int n, void *user)
3251 struct isl_ast_unroll_tree_data *data = user;
3252 isl_ctx *ctx;
3254 ctx = isl_ast_build_get_ctx(data->build);
3255 data->list = isl_ast_graft_list_alloc(ctx, n);
3257 return 0;
3260 /* Given an iteration of an unrolled domain represented by "bset",
3261 * generate the corresponding AST and add the result to data->list.
3263 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3265 struct isl_ast_unroll_tree_data *data = user;
3267 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3268 bset, isl_ast_build_copy(data->build));
3270 return 0;
3273 /* Generate code for a single component, after shifting (if any)
3274 * has been applied, in case the schedule was specified as a schedule tree
3275 * and the unroll option was specified.
3277 * We call foreach_iteration to iterate over the individual values and
3278 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3280 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3281 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3282 __isl_take isl_ast_build *build)
3284 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3286 if (foreach_iteration(domain, build, &init_unroll_tree,
3287 &do_unroll_tree_iteration, &data) < 0)
3288 data.list = isl_ast_graft_list_free(data.list);
3290 isl_union_map_free(executed);
3291 isl_ast_build_free(build);
3293 return data.list;
3296 /* Does "domain" involve a disjunction that is purely based on
3297 * constraints involving only outer dimension?
3299 * In particular, is there a disjunction such that the constraints
3300 * involving the current and later dimensions are the same over
3301 * all the disjuncts?
3303 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3304 __isl_keep isl_ast_build *build)
3306 isl_basic_set *hull;
3307 isl_set *shared, *inner;
3308 isl_bool equal;
3309 isl_size depth;
3310 isl_size n;
3311 isl_size dim;
3313 n = isl_set_n_basic_set(domain);
3314 if (n < 0)
3315 return isl_bool_error;
3316 if (n <= 1)
3317 return isl_bool_false;
3318 dim = isl_set_dim(domain, isl_dim_set);
3319 depth = isl_ast_build_get_depth(build);
3320 if (dim < 0 || depth < 0)
3321 return isl_bool_error;
3323 inner = isl_set_copy(domain);
3324 inner = isl_set_drop_constraints_not_involving_dims(inner,
3325 isl_dim_set, depth, dim - depth);
3326 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3327 shared = isl_set_from_basic_set(hull);
3328 equal = isl_set_plain_is_equal(inner, shared);
3329 isl_set_free(inner);
3330 isl_set_free(shared);
3332 return equal;
3335 /* Generate code for a single component, after shifting (if any)
3336 * has been applied, in case the schedule was specified as a schedule tree.
3337 * In particular, handle the base case where there is either no isolated
3338 * set or we are within the isolated set (in which case "isolated" is set)
3339 * or the iterations that precede or follow the isolated set.
3341 * The schedule domain is broken up or combined into basic sets
3342 * according to the AST generation option specified in the current
3343 * schedule node, which may be either atomic, separate, unroll or
3344 * unspecified. If the option is unspecified, then we currently simply
3345 * split the schedule domain into disjoint basic sets.
3347 * In case the separate option is specified, the AST generation is
3348 * handled by generate_shifted_component_tree_separate.
3349 * In the other cases, we need the global schedule domain.
3350 * In the unroll case, the AST generation is then handled by
3351 * generate_shifted_component_tree_unroll which needs the actual
3352 * schedule domain (with divs that may refer to the current dimension)
3353 * so that stride detection can be performed.
3354 * In the atomic or unspecified case, inner dimensions and divs involving
3355 * the current dimensions should be eliminated.
3356 * The result is then either combined into a single basic set or
3357 * split up into disjoint basic sets.
3358 * Finally an AST is generated for each basic set and the results are
3359 * concatenated.
3361 * If the schedule domain involves a disjunction that is purely based on
3362 * constraints involving only outer dimension, then it is treated as
3363 * if atomic was specified. This ensures that only a single loop
3364 * is generated instead of a sequence of identical loops with
3365 * different guards.
3367 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3368 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3369 int isolated)
3371 isl_bool outer_disjunction;
3372 isl_union_set *schedule_domain;
3373 isl_set *domain;
3374 isl_basic_set_list *domain_list;
3375 isl_ast_graft_list *list;
3376 enum isl_ast_loop_type type;
3378 type = isl_ast_build_get_loop_type(build, isolated);
3379 if (type < 0)
3380 goto error;
3382 if (type == isl_ast_loop_separate)
3383 return generate_shifted_component_tree_separate(executed,
3384 build);
3386 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3387 domain = isl_set_from_union_set(schedule_domain);
3389 if (type == isl_ast_loop_unroll)
3390 return generate_shifted_component_tree_unroll(executed, domain,
3391 build);
3393 domain = isl_ast_build_eliminate(build, domain);
3394 domain = isl_set_coalesce_preserve(domain);
3396 outer_disjunction = has_pure_outer_disjunction(domain, build);
3397 if (outer_disjunction < 0)
3398 domain = isl_set_free(domain);
3400 if (outer_disjunction || type == isl_ast_loop_atomic) {
3401 isl_basic_set *hull;
3402 hull = isl_set_unshifted_simple_hull(domain);
3403 domain_list = isl_basic_set_list_from_basic_set(hull);
3404 } else {
3405 domain = isl_set_make_disjoint(domain);
3406 domain_list = isl_basic_set_list_from_set(domain);
3409 list = generate_parallel_domains(domain_list, executed, build);
3411 isl_basic_set_list_free(domain_list);
3412 isl_union_map_free(executed);
3413 isl_ast_build_free(build);
3415 return list;
3416 error:
3417 isl_union_map_free(executed);
3418 isl_ast_build_free(build);
3419 return NULL;
3422 /* Extract out the disjunction imposed by "domain" on the outer
3423 * schedule dimensions.
3425 * In particular, remove all inner dimensions from "domain" (including
3426 * the current dimension) and then remove the constraints that are shared
3427 * by all disjuncts in the result.
3429 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3430 __isl_keep isl_ast_build *build)
3432 isl_set *hull;
3433 isl_size depth;
3434 isl_size dim;
3436 domain = isl_ast_build_specialize(build, domain);
3437 depth = isl_ast_build_get_depth(build);
3438 dim = isl_set_dim(domain, isl_dim_set);
3439 if (depth < 0 || dim < 0)
3440 return isl_set_free(domain);
3441 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3442 domain = isl_set_remove_unknown_divs(domain);
3443 hull = isl_set_copy(domain);
3444 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3445 domain = isl_set_gist(domain, hull);
3447 return domain;
3450 /* Add "guard" to the grafts in "list".
3451 * "build" is the outer AST build, while "sub_build" includes "guard"
3452 * in its generated domain.
3454 * First combine the grafts into a single graft and then add the guard.
3455 * If the list is empty, or if some error occurred, then simply return
3456 * the list.
3458 static __isl_give isl_ast_graft_list *list_add_guard(
3459 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3460 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3462 isl_ast_graft *graft;
3463 isl_size n;
3465 list = isl_ast_graft_list_fuse(list, sub_build);
3467 n = isl_ast_graft_list_n_ast_graft(list);
3468 if (n < 0)
3469 return isl_ast_graft_list_free(list);
3470 if (n != 1)
3471 return list;
3473 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3474 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3475 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3477 return list;
3480 /* Generate code for a single component, after shifting (if any)
3481 * has been applied, in case the schedule was specified as a schedule tree.
3482 * In particular, do so for the specified subset of the schedule domain.
3484 * If we are outside of the isolated part, then "domain" may include
3485 * a disjunction. Explicitly generate this disjunction at this point
3486 * instead of relying on the disjunction getting hoisted back up
3487 * to this level.
3489 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3490 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3491 __isl_keep isl_ast_build *build, int isolated)
3493 isl_union_set *uset;
3494 isl_ast_graft_list *list;
3495 isl_ast_build *sub_build;
3496 int empty;
3498 uset = isl_union_set_from_set(isl_set_copy(domain));
3499 executed = isl_union_map_copy(executed);
3500 executed = isl_union_map_intersect_domain(executed, uset);
3501 empty = isl_union_map_is_empty(executed);
3502 if (empty < 0)
3503 goto error;
3504 if (empty) {
3505 isl_ctx *ctx;
3506 isl_union_map_free(executed);
3507 isl_set_free(domain);
3508 ctx = isl_ast_build_get_ctx(build);
3509 return isl_ast_graft_list_alloc(ctx, 0);
3512 sub_build = isl_ast_build_copy(build);
3513 if (!isolated) {
3514 domain = extract_disjunction(domain, build);
3515 sub_build = isl_ast_build_restrict_generated(sub_build,
3516 isl_set_copy(domain));
3518 list = generate_shifted_component_tree_base(executed,
3519 isl_ast_build_copy(sub_build), isolated);
3520 if (!isolated)
3521 list = list_add_guard(list, domain, build, sub_build);
3522 isl_ast_build_free(sub_build);
3523 isl_set_free(domain);
3524 return list;
3525 error:
3526 isl_union_map_free(executed);
3527 isl_set_free(domain);
3528 return NULL;
3531 /* Generate code for a single component, after shifting (if any)
3532 * has been applied, in case the schedule was specified as a schedule tree.
3533 * In particular, do so for the specified sequence of subsets
3534 * of the schedule domain, "before", "isolated", "after" and "other",
3535 * where only the "isolated" part is considered to be isolated.
3537 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3538 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3539 __isl_take isl_set *isolated, __isl_take isl_set *after,
3540 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3542 isl_ast_graft_list *list, *res;
3544 res = generate_shifted_component_tree_part(executed, before, build, 0);
3545 list = generate_shifted_component_tree_part(executed, isolated,
3546 build, 1);
3547 res = isl_ast_graft_list_concat(res, list);
3548 list = generate_shifted_component_tree_part(executed, after, build, 0);
3549 res = isl_ast_graft_list_concat(res, list);
3550 list = generate_shifted_component_tree_part(executed, other, build, 0);
3551 res = isl_ast_graft_list_concat(res, list);
3553 isl_union_map_free(executed);
3554 isl_ast_build_free(build);
3556 return res;
3559 /* Does "set" intersect "first", but not "second"?
3561 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3562 __isl_keep isl_set *first, __isl_keep isl_set *second)
3564 isl_bool disjoint;
3566 disjoint = isl_set_is_disjoint(set, first);
3567 if (disjoint < 0)
3568 return isl_bool_error;
3569 if (disjoint)
3570 return isl_bool_false;
3572 return isl_set_is_disjoint(set, second);
3575 /* Generate code for a single component, after shifting (if any)
3576 * has been applied, in case the schedule was specified as a schedule tree.
3577 * In particular, do so in case of isolation where there is
3578 * only an "isolated" part and an "after" part.
3579 * "dead1" and "dead2" are freed by this function in order to simplify
3580 * the caller.
3582 * The "before" and "other" parts are set to empty sets.
3584 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3585 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3586 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3587 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3589 isl_set *empty;
3591 empty = isl_set_empty(isl_set_get_space(after));
3592 isl_set_free(dead1);
3593 isl_set_free(dead2);
3594 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3595 isolated, after, empty, build);
3598 /* Generate code for a single component, after shifting (if any)
3599 * has been applied, in case the schedule was specified as a schedule tree.
3601 * We first check if the user has specified an isolated schedule domain
3602 * and that we are not already outside of this isolated schedule domain.
3603 * If so, we break up the schedule domain into iterations that
3604 * precede the isolated domain, the isolated domain itself,
3605 * the iterations that follow the isolated domain and
3606 * the remaining iterations (those that are incomparable
3607 * to the isolated domain).
3608 * We generate an AST for each piece and concatenate the results.
3610 * If the isolated domain is not convex, then it is replaced
3611 * by a convex superset to ensure that the sets of preceding and
3612 * following iterations are properly defined and, in particular,
3613 * that there are no intermediate iterations that do not belong
3614 * to the isolated domain.
3616 * In the special case where at least one element of the schedule
3617 * domain that does not belong to the isolated domain needs
3618 * to be scheduled after this isolated domain, but none of those
3619 * elements need to be scheduled before, break up the schedule domain
3620 * in only two parts, the isolated domain, and a part that will be
3621 * scheduled after the isolated domain.
3623 * If no isolated set has been specified, then we generate an
3624 * AST for the entire inverse schedule.
3626 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3627 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3629 int i;
3630 isl_size depth;
3631 int empty, has_isolate;
3632 isl_space *space;
3633 isl_union_set *schedule_domain;
3634 isl_set *domain;
3635 isl_basic_set *hull;
3636 isl_set *isolated, *before, *after, *test;
3637 isl_map *gt, *lt;
3638 isl_bool pure;
3640 build = isl_ast_build_extract_isolated(build);
3641 has_isolate = isl_ast_build_has_isolated(build);
3642 if (has_isolate < 0)
3643 executed = isl_union_map_free(executed);
3644 else if (!has_isolate)
3645 return generate_shifted_component_tree_base(executed, build, 0);
3647 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3648 domain = isl_set_from_union_set(schedule_domain);
3650 isolated = isl_ast_build_get_isolated(build);
3651 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3652 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3653 empty = isl_set_is_empty(test);
3654 isl_set_free(test);
3655 if (empty < 0)
3656 goto error;
3657 if (empty) {
3658 isl_set_free(isolated);
3659 isl_set_free(domain);
3660 return generate_shifted_component_tree_base(executed, build, 0);
3662 depth = isl_ast_build_get_depth(build);
3663 if (depth < 0)
3664 goto error;
3666 isolated = isl_ast_build_eliminate(build, isolated);
3667 hull = isl_set_unshifted_simple_hull(isolated);
3668 isolated = isl_set_from_basic_set(hull);
3670 space = isl_space_map_from_set(isl_set_get_space(isolated));
3671 gt = isl_map_universe(space);
3672 for (i = 0; i < depth; ++i)
3673 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3674 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3675 lt = isl_map_reverse(isl_map_copy(gt));
3676 before = isl_set_apply(isl_set_copy(isolated), gt);
3677 after = isl_set_apply(isl_set_copy(isolated), lt);
3679 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3680 pure = only_intersects_first(domain, after, before);
3681 if (pure < 0)
3682 executed = isl_union_map_free(executed);
3683 else if (pure)
3684 return generate_shifted_component_only_after(executed, isolated,
3685 domain, build, before, after);
3686 domain = isl_set_subtract(domain, isl_set_copy(before));
3687 domain = isl_set_subtract(domain, isl_set_copy(after));
3688 after = isl_set_subtract(after, isl_set_copy(isolated));
3689 after = isl_set_subtract(after, isl_set_copy(before));
3690 before = isl_set_subtract(before, isl_set_copy(isolated));
3692 return generate_shifted_component_parts(executed, before, isolated,
3693 after, domain, build);
3694 error:
3695 isl_set_free(domain);
3696 isl_set_free(isolated);
3697 isl_union_map_free(executed);
3698 isl_ast_build_free(build);
3699 return NULL;
3702 /* Generate code for a single component, after shifting (if any)
3703 * has been applied.
3705 * Call generate_shifted_component_tree or generate_shifted_component_flat
3706 * depending on whether the schedule was specified as a schedule tree.
3708 static __isl_give isl_ast_graft_list *generate_shifted_component(
3709 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3711 if (isl_ast_build_has_schedule_node(build))
3712 return generate_shifted_component_tree(executed, build);
3713 else
3714 return generate_shifted_component_flat(executed, build);
3717 struct isl_set_map_pair {
3718 isl_set *set;
3719 isl_map *map;
3722 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3723 * of indices into the "domain" array,
3724 * return the union of the "map" fields of the elements
3725 * indexed by the first "n" elements of "order".
3727 static __isl_give isl_union_map *construct_component_executed(
3728 struct isl_set_map_pair *domain, int *order, int n)
3730 int i;
3731 isl_map *map;
3732 isl_union_map *executed;
3734 map = isl_map_copy(domain[order[0]].map);
3735 executed = isl_union_map_from_map(map);
3736 for (i = 1; i < n; ++i) {
3737 map = isl_map_copy(domain[order[i]].map);
3738 executed = isl_union_map_add_map(executed, map);
3741 return executed;
3744 /* Generate code for a single component, after shifting (if any)
3745 * has been applied.
3747 * The component inverse schedule is specified as the "map" fields
3748 * of the elements of "domain" indexed by the first "n" elements of "order".
3750 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3751 struct isl_set_map_pair *domain, int *order, int n,
3752 __isl_take isl_ast_build *build)
3754 isl_union_map *executed;
3756 executed = construct_component_executed(domain, order, n);
3757 return generate_shifted_component(executed, build);
3760 /* Does set dimension "pos" of "set" have an obviously fixed value?
3762 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3764 int fixed;
3765 isl_val *v;
3767 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3768 if (!v)
3769 return -1;
3770 fixed = !isl_val_is_nan(v);
3771 isl_val_free(v);
3773 return fixed;
3776 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3777 * of indices into the "domain" array,
3778 * do all (except for at most one) of the "set" field of the elements
3779 * indexed by the first "n" elements of "order" have a fixed value
3780 * at position "depth"?
3782 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3783 int *order, int n, int depth)
3785 int i;
3786 int non_fixed = -1;
3788 for (i = 0; i < n; ++i) {
3789 int f;
3791 f = dim_is_fixed(domain[order[i]].set, depth);
3792 if (f < 0)
3793 return -1;
3794 if (f)
3795 continue;
3796 if (non_fixed >= 0)
3797 return 0;
3798 non_fixed = i;
3801 return 1;
3804 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3805 * of indices into the "domain" array,
3806 * eliminate the inner dimensions from the "set" field of the elements
3807 * indexed by the first "n" elements of "order", provided the current
3808 * dimension does not have a fixed value.
3810 * Return the index of the first element in "order" with a corresponding
3811 * "set" field that does not have an (obviously) fixed value.
3813 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3814 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3816 int i;
3817 int base = -1;
3819 for (i = n - 1; i >= 0; --i) {
3820 int f;
3821 f = dim_is_fixed(domain[order[i]].set, depth);
3822 if (f < 0)
3823 return -1;
3824 if (f)
3825 continue;
3826 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3827 domain[order[i]].set);
3828 base = i;
3831 return base;
3834 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3835 * of indices into the "domain" array,
3836 * find the element of "domain" (amongst those indexed by the first "n"
3837 * elements of "order") with the "set" field that has the smallest
3838 * value for the current iterator.
3840 * Note that the domain with the smallest value may depend on the parameters
3841 * and/or outer loop dimension. Since the result of this function is only
3842 * used as heuristic, we only make a reasonable attempt at finding the best
3843 * domain, one that should work in case a single domain provides the smallest
3844 * value for the current dimension over all values of the parameters
3845 * and outer dimensions.
3847 * In particular, we compute the smallest value of the first domain
3848 * and replace it by that of any later domain if that later domain
3849 * has a smallest value that is smaller for at least some value
3850 * of the parameters and outer dimensions.
3852 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3853 __isl_keep isl_ast_build *build)
3855 int i;
3856 isl_map *min_first;
3857 int first = 0;
3859 min_first = isl_ast_build_map_to_iterator(build,
3860 isl_set_copy(domain[order[0]].set));
3861 min_first = isl_map_lexmin(min_first);
3863 for (i = 1; i < n; ++i) {
3864 isl_map *min, *test;
3865 int empty;
3867 min = isl_ast_build_map_to_iterator(build,
3868 isl_set_copy(domain[order[i]].set));
3869 min = isl_map_lexmin(min);
3870 test = isl_map_copy(min);
3871 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3872 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3873 empty = isl_map_is_empty(test);
3874 isl_map_free(test);
3875 if (empty >= 0 && !empty) {
3876 isl_map_free(min_first);
3877 first = i;
3878 min_first = min;
3879 } else
3880 isl_map_free(min);
3882 if (empty < 0)
3883 break;
3886 isl_map_free(min_first);
3888 return i < n ? -1 : first;
3891 /* Construct a shifted inverse schedule based on the original inverse schedule,
3892 * the stride and the offset.
3894 * The original inverse schedule is specified as the "map" fields
3895 * of the elements of "domain" indexed by the first "n" elements of "order".
3897 * "stride" and "offset" are such that the difference
3898 * between the values of the current dimension of domain "i"
3899 * and the values of the current dimension for some reference domain are
3900 * equal to
3902 * stride * integer + offset[i]
3904 * Moreover, 0 <= offset[i] < stride.
3906 * For each domain, we create a map
3908 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3910 * where j refers to the current dimension and the other dimensions are
3911 * unchanged, and apply this map to the original schedule domain.
3913 * For example, for the original schedule
3915 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3917 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3918 * we apply the mapping
3920 * { [j] -> [j, 0] }
3922 * to the schedule of the "A" domain and the mapping
3924 * { [j - 1] -> [j, 1] }
3926 * to the schedule of the "B" domain.
3929 * Note that after the transformation, the differences between pairs
3930 * of values of the current dimension over all domains are multiples
3931 * of stride and that we have therefore exposed the stride.
3934 * To see that the mapping preserves the lexicographic order,
3935 * first note that each of the individual maps above preserves the order.
3936 * If the value of the current iterator is j1 in one domain and j2 in another,
3937 * then if j1 = j2, we know that the same map is applied to both domains
3938 * and the order is preserved.
3939 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3940 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3942 * j1 - c1 < j2 - c2
3944 * and the order is preserved.
3945 * If c1 < c2, then we know
3947 * 0 <= c2 - c1 < s
3949 * We also have
3951 * j2 - j1 = n * s + r
3953 * with n >= 0 and 0 <= r < s.
3954 * In other words, r = c2 - c1.
3955 * If n > 0, then
3957 * j1 - c1 < j2 - c2
3959 * If n = 0, then
3961 * j1 - c1 = j2 - c2
3963 * and so
3965 * (j1 - c1, c1) << (j2 - c2, c2)
3967 * with "<<" the lexicographic order, proving that the order is preserved
3968 * in all cases.
3970 static __isl_give isl_union_map *construct_shifted_executed(
3971 struct isl_set_map_pair *domain, int *order, int n,
3972 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3973 __isl_keep isl_ast_build *build)
3975 int i;
3976 isl_union_map *executed;
3977 isl_space *space;
3978 isl_map *map;
3979 isl_size depth;
3980 isl_constraint *c;
3982 depth = isl_ast_build_get_depth(build);
3983 if (depth < 0)
3984 return NULL;
3985 space = isl_ast_build_get_space(build, 1);
3986 executed = isl_union_map_empty(isl_space_copy(space));
3987 space = isl_space_map_from_set(space);
3988 map = isl_map_identity(isl_space_copy(space));
3989 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3990 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3991 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3993 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3994 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3995 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3997 for (i = 0; i < n; ++i) {
3998 isl_map *map_i;
3999 isl_val *v;
4001 v = isl_multi_val_get_val(offset, i);
4002 if (!v)
4003 break;
4004 map_i = isl_map_copy(map);
4005 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
4006 isl_val_copy(v));
4007 v = isl_val_neg(v);
4008 c = isl_constraint_set_constant_val(c, v);
4009 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
4011 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
4012 map_i);
4013 executed = isl_union_map_add_map(executed, map_i);
4016 isl_constraint_free(c);
4017 isl_map_free(map);
4019 if (i < n)
4020 executed = isl_union_map_free(executed);
4022 return executed;
4025 /* Generate code for a single component, after exposing the stride,
4026 * given that the schedule domain is "shifted strided".
4028 * The component inverse schedule is specified as the "map" fields
4029 * of the elements of "domain" indexed by the first "n" elements of "order".
4031 * The schedule domain being "shifted strided" means that the differences
4032 * between the values of the current dimension of domain "i"
4033 * and the values of the current dimension for some reference domain are
4034 * equal to
4036 * stride * integer + offset[i]
4038 * We first look for the domain with the "smallest" value for the current
4039 * dimension and adjust the offsets such that the offset of the "smallest"
4040 * domain is equal to zero. The other offsets are reduced modulo stride.
4042 * Based on this information, we construct a new inverse schedule in
4043 * construct_shifted_executed that exposes the stride.
4044 * Since this involves the introduction of a new schedule dimension,
4045 * the build needs to be changed accordingly.
4046 * After computing the AST, the newly introduced dimension needs
4047 * to be removed again from the list of grafts. We do this by plugging
4048 * in a mapping that represents the new schedule domain in terms of the
4049 * old schedule domain.
4051 static __isl_give isl_ast_graft_list *generate_shift_component(
4052 struct isl_set_map_pair *domain, int *order, int n,
4053 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
4054 __isl_take isl_ast_build *build)
4056 isl_ast_graft_list *list;
4057 int first;
4058 isl_size depth;
4059 isl_val *val;
4060 isl_multi_val *mv;
4061 isl_space *space;
4062 isl_multi_aff *ma, *zero;
4063 isl_union_map *executed;
4065 depth = isl_ast_build_get_depth(build);
4067 first = first_offset(domain, order, n, build);
4068 if (depth < 0 || first < 0)
4069 goto error;
4071 mv = isl_multi_val_copy(offset);
4072 val = isl_multi_val_get_val(offset, first);
4073 val = isl_val_neg(val);
4074 mv = isl_multi_val_add_val(mv, val);
4075 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
4077 executed = construct_shifted_executed(domain, order, n, stride, mv,
4078 build);
4079 space = isl_ast_build_get_space(build, 1);
4080 space = isl_space_map_from_set(space);
4081 ma = isl_multi_aff_identity(isl_space_copy(space));
4082 space = isl_space_from_domain(isl_space_domain(space));
4083 space = isl_space_add_dims(space, isl_dim_out, 1);
4084 zero = isl_multi_aff_zero(space);
4085 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
4086 build = isl_ast_build_insert_dim(build, depth + 1);
4087 list = generate_shifted_component(executed, build);
4089 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4091 isl_multi_val_free(mv);
4093 return list;
4094 error:
4095 isl_ast_build_free(build);
4096 return NULL;
4099 /* Does any node in the schedule tree rooted at the current schedule node
4100 * of "build" depend on outer schedule nodes?
4102 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4104 isl_schedule_node *node;
4105 int dependent = 0;
4107 node = isl_ast_build_get_schedule_node(build);
4108 dependent = isl_schedule_node_is_subtree_anchored(node);
4109 isl_schedule_node_free(node);
4111 return dependent;
4114 /* Generate code for a single component.
4116 * The component inverse schedule is specified as the "map" fields
4117 * of the elements of "domain" indexed by the first "n" elements of "order".
4119 * This function may modify the "set" fields of "domain".
4121 * Before proceeding with the actual code generation for the component,
4122 * we first check if there are any "shifted" strides, meaning that
4123 * the schedule domains of the individual domains are all strided,
4124 * but that they have different offsets, resulting in the union
4125 * of schedule domains not being strided anymore.
4127 * The simplest example is the schedule
4129 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4131 * Both schedule domains are strided, but their union is not.
4132 * This function detects such cases and then rewrites the schedule to
4134 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4136 * In the new schedule, the schedule domains have the same offset (modulo
4137 * the stride), ensuring that the union of schedule domains is also strided.
4140 * If there is only a single domain in the component, then there is
4141 * nothing to do. Similarly, if the current schedule dimension has
4142 * a fixed value for almost all domains then there is nothing to be done.
4143 * In particular, we need at least two domains where the current schedule
4144 * dimension does not have a fixed value.
4145 * Finally, in case of a schedule map input,
4146 * if any of the options refer to the current schedule dimension,
4147 * then we bail out as well. It would be possible to reformulate the options
4148 * in terms of the new schedule domain, but that would introduce constraints
4149 * that separate the domains in the options and that is something we would
4150 * like to avoid.
4151 * In the case of a schedule tree input, we bail out if any of
4152 * the descendants of the current schedule node refer to outer
4153 * schedule nodes in any way.
4156 * To see if there is any shifted stride, we look at the differences
4157 * between the values of the current dimension in pairs of domains
4158 * for equal values of outer dimensions. These differences should be
4159 * of the form
4161 * m x + r
4163 * with "m" the stride and "r" a constant. Note that we cannot perform
4164 * this analysis on individual domains as the lower bound in each domain
4165 * may depend on parameters or outer dimensions and so the current dimension
4166 * itself may not have a fixed remainder on division by the stride.
4168 * In particular, we compare the first domain that does not have an
4169 * obviously fixed value for the current dimension to itself and all
4170 * other domains and collect the offsets and the gcd of the strides.
4171 * If the gcd becomes one, then we failed to find shifted strides.
4172 * If the gcd is zero, then the differences were all fixed, meaning
4173 * that some domains had non-obviously fixed values for the current dimension.
4174 * If all the offsets are the same (for those domains that do not have
4175 * an obviously fixed value for the current dimension), then we do not
4176 * apply the transformation.
4177 * If none of the domains were skipped, then there is nothing to do.
4178 * If some of them were skipped, then if we apply separation, the schedule
4179 * domain should get split in pieces with a (non-shifted) stride.
4181 * Otherwise, we apply a shift to expose the stride in
4182 * generate_shift_component.
4184 static __isl_give isl_ast_graft_list *generate_component(
4185 struct isl_set_map_pair *domain, int *order, int n,
4186 __isl_take isl_ast_build *build)
4188 int i, d;
4189 isl_size depth;
4190 isl_ctx *ctx;
4191 isl_map *map;
4192 isl_set *deltas;
4193 isl_val *gcd = NULL;
4194 isl_multi_val *mv;
4195 int fixed, skip;
4196 int base;
4197 isl_ast_graft_list *list;
4198 int res = 0;
4200 depth = isl_ast_build_get_depth(build);
4201 if (depth < 0)
4202 goto error;
4204 skip = n == 1;
4205 if (skip >= 0 && !skip)
4206 skip = at_most_one_non_fixed(domain, order, n, depth);
4207 if (skip >= 0 && !skip) {
4208 if (isl_ast_build_has_schedule_node(build))
4209 skip = has_anchored_subtree(build);
4210 else
4211 skip = isl_ast_build_options_involve_depth(build);
4213 if (skip < 0)
4214 goto error;
4215 if (skip)
4216 return generate_shifted_component_from_list(domain,
4217 order, n, build);
4219 base = eliminate_non_fixed(domain, order, n, depth, build);
4220 if (base < 0)
4221 goto error;
4223 ctx = isl_ast_build_get_ctx(build);
4225 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4227 fixed = 1;
4228 for (i = 0; i < n; ++i) {
4229 isl_val *r, *m;
4231 map = isl_map_from_domain_and_range(
4232 isl_set_copy(domain[order[base]].set),
4233 isl_set_copy(domain[order[i]].set));
4234 for (d = 0; d < depth; ++d)
4235 map = isl_map_equate(map, isl_dim_in, d,
4236 isl_dim_out, d);
4237 deltas = isl_map_deltas(map);
4238 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4239 isl_set_free(deltas);
4240 if (res < 0)
4241 break;
4243 if (i == 0)
4244 gcd = m;
4245 else
4246 gcd = isl_val_gcd(gcd, m);
4247 if (isl_val_is_one(gcd)) {
4248 isl_val_free(r);
4249 break;
4251 mv = isl_multi_val_set_val(mv, i, r);
4253 res = dim_is_fixed(domain[order[i]].set, depth);
4254 if (res < 0)
4255 break;
4256 if (res)
4257 continue;
4259 if (fixed && i > base) {
4260 isl_val *a, *b;
4261 a = isl_multi_val_get_val(mv, i);
4262 b = isl_multi_val_get_val(mv, base);
4263 if (isl_val_ne(a, b))
4264 fixed = 0;
4265 isl_val_free(a);
4266 isl_val_free(b);
4270 if (res < 0 || !gcd) {
4271 isl_ast_build_free(build);
4272 list = NULL;
4273 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4274 list = generate_shifted_component_from_list(domain,
4275 order, n, build);
4276 } else {
4277 list = generate_shift_component(domain, order, n, gcd, mv,
4278 build);
4281 isl_val_free(gcd);
4282 isl_multi_val_free(mv);
4284 return list;
4285 error:
4286 isl_ast_build_free(build);
4287 return NULL;
4290 /* Store both "map" itself and its domain in the
4291 * structure pointed to by *next and advance to the next array element.
4293 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4295 struct isl_set_map_pair **next = user;
4297 (*next)->map = isl_map_copy(map);
4298 (*next)->set = isl_map_domain(map);
4299 (*next)++;
4301 return isl_stat_ok;
4304 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4305 __isl_keep isl_schedule_node *node);
4307 /* Is any domain element of "umap" scheduled after any of
4308 * the corresponding image elements by the tree rooted at
4309 * the child of "node"?
4311 static isl_bool after_in_child(__isl_keep isl_union_map *umap,
4312 __isl_keep isl_schedule_node *node)
4314 isl_schedule_node *child;
4315 isl_bool after;
4317 child = isl_schedule_node_get_child(node, 0);
4318 after = after_in_tree(umap, child);
4319 isl_schedule_node_free(child);
4321 return after;
4324 /* Is any domain element of "umap" scheduled after any of
4325 * the corresponding image elements by the tree rooted at
4326 * the band node "node"?
4328 * We first check if any domain element is scheduled after any
4329 * of the corresponding image elements by the band node itself.
4330 * If not, we restrict "map" to those pairs of element that
4331 * are scheduled together by the band node and continue with
4332 * the child of the band node.
4333 * If there are no such pairs then the map passed to after_in_child
4334 * will be empty causing it to return 0.
4336 static isl_bool after_in_band(__isl_keep isl_union_map *umap,
4337 __isl_keep isl_schedule_node *node)
4339 isl_multi_union_pw_aff *mupa;
4340 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4341 isl_union_set *domain, *range;
4342 isl_space *space;
4343 isl_bool empty;
4344 isl_bool after;
4345 isl_size n;
4347 n = isl_schedule_node_band_n_member(node);
4348 if (n < 0)
4349 return isl_bool_error;
4350 if (n == 0)
4351 return after_in_child(umap, node);
4353 mupa = isl_schedule_node_band_get_partial_schedule(node);
4354 space = isl_multi_union_pw_aff_get_space(mupa);
4355 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4356 test = isl_union_map_copy(umap);
4357 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4358 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4359 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4360 test = isl_union_map_intersect(test, gt);
4361 empty = isl_union_map_is_empty(test);
4362 isl_union_map_free(test);
4364 if (empty < 0 || !empty) {
4365 isl_union_map_free(partial);
4366 return isl_bool_not(empty);
4369 universe = isl_union_map_universe(isl_union_map_copy(umap));
4370 domain = isl_union_map_domain(isl_union_map_copy(universe));
4371 range = isl_union_map_range(universe);
4372 umap1 = isl_union_map_copy(partial);
4373 umap1 = isl_union_map_intersect_domain(umap1, domain);
4374 umap2 = isl_union_map_intersect_domain(partial, range);
4375 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4376 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4377 after = after_in_child(test, node);
4378 isl_union_map_free(test);
4379 return after;
4382 /* Is any domain element of "umap" scheduled after any of
4383 * the corresponding image elements by the tree rooted at
4384 * the context node "node"?
4386 * The context constraints apply to the schedule domain,
4387 * so we cannot apply them directly to "umap", which contains
4388 * pairs of statement instances. Instead, we add them
4389 * to the range of the prefix schedule for both domain and
4390 * range of "umap".
4392 static isl_bool after_in_context(__isl_keep isl_union_map *umap,
4393 __isl_keep isl_schedule_node *node)
4395 isl_union_map *prefix, *universe, *umap1, *umap2;
4396 isl_union_set *domain, *range;
4397 isl_set *context;
4398 isl_bool after;
4400 umap = isl_union_map_copy(umap);
4401 context = isl_schedule_node_context_get_context(node);
4402 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4403 universe = isl_union_map_universe(isl_union_map_copy(umap));
4404 domain = isl_union_map_domain(isl_union_map_copy(universe));
4405 range = isl_union_map_range(universe);
4406 umap1 = isl_union_map_copy(prefix);
4407 umap1 = isl_union_map_intersect_domain(umap1, domain);
4408 umap2 = isl_union_map_intersect_domain(prefix, range);
4409 umap1 = isl_union_map_intersect_range(umap1,
4410 isl_union_set_from_set(context));
4411 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4412 umap = isl_union_map_intersect(umap, umap1);
4414 after = after_in_child(umap, node);
4416 isl_union_map_free(umap);
4418 return after;
4421 /* Is any domain element of "umap" scheduled after any of
4422 * the corresponding image elements by the tree rooted at
4423 * the expansion node "node"?
4425 * We apply the expansion to domain and range of "umap" and
4426 * continue with its child.
4428 static isl_bool after_in_expansion(__isl_keep isl_union_map *umap,
4429 __isl_keep isl_schedule_node *node)
4431 isl_union_map *expansion;
4432 isl_bool after;
4434 expansion = isl_schedule_node_expansion_get_expansion(node);
4435 umap = isl_union_map_copy(umap);
4436 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4437 umap = isl_union_map_apply_range(umap, expansion);
4439 after = after_in_child(umap, node);
4441 isl_union_map_free(umap);
4443 return after;
4446 /* Is any domain element of "umap" scheduled after any of
4447 * the corresponding image elements by the tree rooted at
4448 * the extension node "node"?
4450 * Since the extension node may add statement instances before or
4451 * after the pairs of statement instances in "umap", we return isl_bool_true
4452 * to ensure that these pairs are not broken up.
4454 static isl_bool after_in_extension(__isl_keep isl_union_map *umap,
4455 __isl_keep isl_schedule_node *node)
4457 return isl_bool_true;
4460 /* Is any domain element of "umap" scheduled after any of
4461 * the corresponding image elements by the tree rooted at
4462 * the filter node "node"?
4464 * We intersect domain and range of "umap" with the filter and
4465 * continue with its child.
4467 static isl_bool after_in_filter(__isl_keep isl_union_map *umap,
4468 __isl_keep isl_schedule_node *node)
4470 isl_union_set *filter;
4471 isl_bool after;
4473 umap = isl_union_map_copy(umap);
4474 filter = isl_schedule_node_filter_get_filter(node);
4475 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4476 umap = isl_union_map_intersect_range(umap, filter);
4478 after = after_in_child(umap, node);
4480 isl_union_map_free(umap);
4482 return after;
4485 /* Is any domain element of "umap" scheduled after any of
4486 * the corresponding image elements by the tree rooted at
4487 * the set node "node"?
4489 * This is only the case if this condition holds in any
4490 * of the (filter) children of the set node.
4491 * In particular, if the domain and the range of "umap"
4492 * are contained in different children, then the condition
4493 * does not hold.
4495 static isl_bool after_in_set(__isl_keep isl_union_map *umap,
4496 __isl_keep isl_schedule_node *node)
4498 int i;
4499 isl_size n;
4501 n = isl_schedule_node_n_children(node);
4502 if (n < 0)
4503 return isl_bool_error;
4504 for (i = 0; i < n; ++i) {
4505 isl_schedule_node *child;
4506 isl_bool after;
4508 child = isl_schedule_node_get_child(node, i);
4509 after = after_in_tree(umap, child);
4510 isl_schedule_node_free(child);
4512 if (after < 0 || after)
4513 return after;
4516 return isl_bool_false;
4519 /* Return the filter of child "i" of "node".
4521 static __isl_give isl_union_set *child_filter(
4522 __isl_keep isl_schedule_node *node, int i)
4524 isl_schedule_node *child;
4525 isl_union_set *filter;
4527 child = isl_schedule_node_get_child(node, i);
4528 filter = isl_schedule_node_filter_get_filter(child);
4529 isl_schedule_node_free(child);
4531 return filter;
4534 /* Is any domain element of "umap" scheduled after any of
4535 * the corresponding image elements by the tree rooted at
4536 * the sequence node "node"?
4538 * This happens in particular if any domain element is
4539 * contained in a later child than one containing a range element or
4540 * if the condition holds within a given child in the sequence.
4541 * The later part of the condition is checked by after_in_set.
4543 static isl_bool after_in_sequence(__isl_keep isl_union_map *umap,
4544 __isl_keep isl_schedule_node *node)
4546 int i, j;
4547 isl_size n;
4548 isl_union_map *umap_i;
4549 isl_bool empty;
4550 isl_bool after = isl_bool_false;
4552 n = isl_schedule_node_n_children(node);
4553 if (n < 0)
4554 return isl_bool_error;
4555 for (i = 1; i < n; ++i) {
4556 isl_union_set *filter_i;
4558 umap_i = isl_union_map_copy(umap);
4559 filter_i = child_filter(node, i);
4560 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4561 empty = isl_union_map_is_empty(umap_i);
4562 if (empty < 0)
4563 goto error;
4564 if (empty) {
4565 isl_union_map_free(umap_i);
4566 continue;
4569 for (j = 0; j < i; ++j) {
4570 isl_union_set *filter_j;
4571 isl_union_map *umap_ij;
4573 umap_ij = isl_union_map_copy(umap_i);
4574 filter_j = child_filter(node, j);
4575 umap_ij = isl_union_map_intersect_range(umap_ij,
4576 filter_j);
4577 empty = isl_union_map_is_empty(umap_ij);
4578 isl_union_map_free(umap_ij);
4580 if (empty < 0)
4581 goto error;
4582 if (!empty)
4583 after = isl_bool_true;
4584 if (after)
4585 break;
4588 isl_union_map_free(umap_i);
4589 if (after)
4590 break;
4593 if (after < 0 || after)
4594 return after;
4596 return after_in_set(umap, node);
4597 error:
4598 isl_union_map_free(umap_i);
4599 return isl_bool_error;
4602 /* Is any domain element of "umap" scheduled after any of
4603 * the corresponding image elements by the tree rooted at "node"?
4605 * If "umap" is empty, then clearly there is no such element.
4606 * Otherwise, consider the different types of nodes separately.
4608 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4609 __isl_keep isl_schedule_node *node)
4611 isl_bool empty;
4612 enum isl_schedule_node_type type;
4614 empty = isl_union_map_is_empty(umap);
4615 if (empty < 0)
4616 return isl_bool_error;
4617 if (empty)
4618 return isl_bool_false;
4619 if (!node)
4620 return isl_bool_error;
4622 type = isl_schedule_node_get_type(node);
4623 switch (type) {
4624 case isl_schedule_node_error:
4625 return isl_bool_error;
4626 case isl_schedule_node_leaf:
4627 return isl_bool_false;
4628 case isl_schedule_node_band:
4629 return after_in_band(umap, node);
4630 case isl_schedule_node_domain:
4631 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4632 "unexpected internal domain node",
4633 return isl_bool_error);
4634 case isl_schedule_node_context:
4635 return after_in_context(umap, node);
4636 case isl_schedule_node_expansion:
4637 return after_in_expansion(umap, node);
4638 case isl_schedule_node_extension:
4639 return after_in_extension(umap, node);
4640 case isl_schedule_node_filter:
4641 return after_in_filter(umap, node);
4642 case isl_schedule_node_guard:
4643 case isl_schedule_node_mark:
4644 return after_in_child(umap, node);
4645 case isl_schedule_node_set:
4646 return after_in_set(umap, node);
4647 case isl_schedule_node_sequence:
4648 return after_in_sequence(umap, node);
4651 return isl_bool_true;
4654 /* Is any domain element of "map1" scheduled after any domain
4655 * element of "map2" by the subtree underneath the current band node,
4656 * while at the same time being scheduled together by the current
4657 * band node, i.e., by "map1" and "map2?
4659 * If the child of the current band node is a leaf, then
4660 * no element can be scheduled after any other element.
4662 * Otherwise, we construct a relation between domain elements
4663 * of "map1" and domain elements of "map2" that are scheduled
4664 * together and then check if the subtree underneath the current
4665 * band node determines their relative order.
4667 static isl_bool after_in_subtree(__isl_keep isl_ast_build *build,
4668 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4670 isl_schedule_node *node;
4671 isl_map *map;
4672 isl_union_map *umap;
4673 isl_bool after;
4675 node = isl_ast_build_get_schedule_node(build);
4676 if (!node)
4677 return isl_bool_error;
4678 node = isl_schedule_node_child(node, 0);
4679 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4680 isl_schedule_node_free(node);
4681 return isl_bool_false;
4683 map = isl_map_copy(map2);
4684 map = isl_map_apply_domain(map, isl_map_copy(map1));
4685 umap = isl_union_map_from_map(map);
4686 after = after_in_tree(umap, node);
4687 isl_union_map_free(umap);
4688 isl_schedule_node_free(node);
4689 return after;
4692 /* Internal data for any_scheduled_after.
4694 * "build" is the build in which the AST is constructed.
4695 * "depth" is the number of loops that have already been generated
4696 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4697 * "domain" is an array of set-map pairs corresponding to the different
4698 * iteration domains. The set is the schedule domain, i.e., the domain
4699 * of the inverse schedule, while the map is the inverse schedule itself.
4701 struct isl_any_scheduled_after_data {
4702 isl_ast_build *build;
4703 int depth;
4704 int group_coscheduled;
4705 struct isl_set_map_pair *domain;
4708 /* Is any element of domain "i" scheduled after any element of domain "j"
4709 * (for a common iteration of the first data->depth loops)?
4711 * data->domain[i].set contains the domain of the inverse schedule
4712 * for domain "i", i.e., elements in the schedule domain.
4714 * If we are inside a band of a schedule tree and there is a pair
4715 * of elements in the two domains that is schedule together by
4716 * the current band, then we check if any element of "i" may be schedule
4717 * after element of "j" by the descendants of the band node.
4719 * If data->group_coscheduled is set, then we also return 1 if there
4720 * is any pair of elements in the two domains that are scheduled together.
4722 static isl_bool any_scheduled_after(int i, int j, void *user)
4724 struct isl_any_scheduled_after_data *data = user;
4725 isl_size dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4726 int pos;
4728 if (dim < 0)
4729 return isl_bool_error;
4731 for (pos = data->depth; pos < dim; ++pos) {
4732 int follows;
4734 follows = isl_set_follows_at(data->domain[i].set,
4735 data->domain[j].set, pos);
4737 if (follows < -1)
4738 return isl_bool_error;
4739 if (follows > 0)
4740 return isl_bool_true;
4741 if (follows < 0)
4742 return isl_bool_false;
4745 if (isl_ast_build_has_schedule_node(data->build)) {
4746 isl_bool after;
4748 after = after_in_subtree(data->build, data->domain[i].map,
4749 data->domain[j].map);
4750 if (after < 0 || after)
4751 return after;
4754 return isl_bool_ok(data->group_coscheduled);
4757 /* Look for independent components at the current depth and generate code
4758 * for each component separately. The resulting lists of grafts are
4759 * merged in an attempt to combine grafts with identical guards.
4761 * Code for two domains can be generated separately if all the elements
4762 * of one domain are scheduled before (or together with) all the elements
4763 * of the other domain. We therefore consider the graph with as nodes
4764 * the domains and an edge between two nodes if any element of the first
4765 * node is scheduled after any element of the second node.
4766 * If the ast_build_group_coscheduled is set, then we also add an edge if
4767 * there is any pair of elements in the two domains that are scheduled
4768 * together.
4769 * Code is then generated (by generate_component)
4770 * for each of the strongly connected components in this graph
4771 * in their topological order.
4773 * Since the test is performed on the domain of the inverse schedules of
4774 * the different domains, we precompute these domains and store
4775 * them in data.domain.
4777 static __isl_give isl_ast_graft_list *generate_components(
4778 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4780 int i;
4781 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4782 isl_size n = isl_union_map_n_map(executed);
4783 isl_size depth;
4784 struct isl_any_scheduled_after_data data;
4785 struct isl_set_map_pair *next;
4786 struct isl_tarjan_graph *g = NULL;
4787 isl_ast_graft_list *list = NULL;
4788 int n_domain = 0;
4790 data.domain = NULL;
4791 if (n < 0)
4792 goto error;
4793 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4794 if (!data.domain)
4795 goto error;
4796 n_domain = n;
4798 next = data.domain;
4799 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4800 goto error;
4802 depth = isl_ast_build_get_depth(build);
4803 if (depth < 0)
4804 goto error;
4805 data.build = build;
4806 data.depth = depth;
4807 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4808 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4809 if (!g)
4810 goto error;
4812 list = isl_ast_graft_list_alloc(ctx, 0);
4814 i = 0;
4815 while (list && n) {
4816 isl_ast_graft_list *list_c;
4817 int first = i;
4819 if (g->order[i] == -1)
4820 isl_die(ctx, isl_error_internal, "cannot happen",
4821 goto error);
4822 ++i; --n;
4823 while (g->order[i] != -1) {
4824 ++i; --n;
4827 list_c = generate_component(data.domain,
4828 g->order + first, i - first,
4829 isl_ast_build_copy(build));
4830 list = isl_ast_graft_list_merge(list, list_c, build);
4832 ++i;
4835 if (0)
4836 error: list = isl_ast_graft_list_free(list);
4837 isl_tarjan_graph_free(g);
4838 for (i = 0; i < n_domain; ++i) {
4839 isl_map_free(data.domain[i].map);
4840 isl_set_free(data.domain[i].set);
4842 free(data.domain);
4843 isl_union_map_free(executed);
4844 isl_ast_build_free(build);
4846 return list;
4849 /* Generate code for the next level (and all inner levels).
4851 * If "executed" is empty, i.e., no code needs to be generated,
4852 * then we return an empty list.
4854 * If we have already generated code for all loop levels, then we pass
4855 * control to generate_inner_level.
4857 * If "executed" lives in a single space, i.e., if code needs to be
4858 * generated for a single domain, then there can only be a single
4859 * component and we go directly to generate_shifted_component.
4860 * Otherwise, we call generate_components to detect the components
4861 * and to call generate_component on each of them separately.
4863 static __isl_give isl_ast_graft_list *generate_next_level(
4864 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4866 isl_size depth;
4867 isl_size dim;
4868 isl_size n;
4870 if (!build || !executed)
4871 goto error;
4873 if (isl_union_map_is_empty(executed)) {
4874 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4875 isl_union_map_free(executed);
4876 isl_ast_build_free(build);
4877 return isl_ast_graft_list_alloc(ctx, 0);
4880 depth = isl_ast_build_get_depth(build);
4881 dim = isl_ast_build_dim(build, isl_dim_set);
4882 if (depth < 0 || dim < 0)
4883 goto error;
4884 if (depth >= dim)
4885 return generate_inner_level(executed, build);
4887 n = isl_union_map_n_map(executed);
4888 if (n < 0)
4889 goto error;
4890 if (n == 1)
4891 return generate_shifted_component(executed, build);
4893 return generate_components(executed, build);
4894 error:
4895 isl_union_map_free(executed);
4896 isl_ast_build_free(build);
4897 return NULL;
4900 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4901 * internal, executed and build are the inputs to generate_code.
4902 * list collects the output.
4904 struct isl_generate_code_data {
4905 int internal;
4906 isl_union_map *executed;
4907 isl_ast_build *build;
4909 isl_ast_graft_list *list;
4912 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4914 * [E -> S] -> D
4916 * with E the external build schedule and S the additional schedule "space",
4917 * reformulate the inverse schedule in terms of the internal schedule domain,
4918 * i.e., return
4920 * [I -> S] -> D
4922 * We first obtain a mapping
4924 * I -> E
4926 * take the inverse and the product with S -> S, resulting in
4928 * [I -> S] -> [E -> S]
4930 * Applying the map to the input produces the desired result.
4932 static __isl_give isl_union_map *internal_executed(
4933 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4934 __isl_keep isl_ast_build *build)
4936 isl_map *id, *proj;
4938 proj = isl_ast_build_get_schedule_map(build);
4939 proj = isl_map_reverse(proj);
4940 space = isl_space_map_from_set(isl_space_copy(space));
4941 id = isl_map_identity(space);
4942 proj = isl_map_product(proj, id);
4943 executed = isl_union_map_apply_domain(executed,
4944 isl_union_map_from_map(proj));
4945 return executed;
4948 /* Generate an AST that visits the elements in the range of data->executed
4949 * in the relative order specified by the corresponding domain element(s)
4950 * for those domain elements that belong to "set".
4951 * Add the result to data->list.
4953 * The caller ensures that "set" is a universe domain.
4954 * "space" is the space of the additional part of the schedule.
4955 * It is equal to the space of "set" if build->domain is parametric.
4956 * Otherwise, it is equal to the range of the wrapped space of "set".
4958 * If the build space is not parametric and
4959 * if isl_ast_build_node_from_schedule_map
4960 * was called from an outside user (data->internal not set), then
4961 * the (inverse) schedule refers to the external build domain and needs to
4962 * be transformed to refer to the internal build domain.
4964 * If the build space is parametric, then we add some of the parameter
4965 * constraints to the executed relation. Adding these constraints
4966 * allows for an earlier detection of conflicts in some cases.
4967 * However, we do not want to divide the executed relation into
4968 * more disjuncts than necessary. We therefore approximate
4969 * the constraints on the parameters by a single disjunct set.
4971 * The build is extended to include the additional part of the schedule.
4972 * If the original build space was not parametric, then the options
4973 * in data->build refer only to the additional part of the schedule
4974 * and they need to be adjusted to refer to the complete AST build
4975 * domain.
4977 * After having adjusted inverse schedule and build, we start generating
4978 * code with the outer loop of the current code generation
4979 * in generate_next_level.
4981 * If the original build space was not parametric, we undo the embedding
4982 * on the resulting isl_ast_node_list so that it can be used within
4983 * the outer AST build.
4985 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4986 __isl_take isl_set *set, __isl_take isl_space *space)
4988 isl_union_map *executed;
4989 isl_ast_build *build;
4990 isl_ast_graft_list *list;
4991 int embed;
4993 executed = isl_union_map_copy(data->executed);
4994 executed = isl_union_map_intersect_domain(executed,
4995 isl_union_set_from_set(set));
4997 embed = !isl_set_is_params(data->build->domain);
4998 if (embed && !data->internal)
4999 executed = internal_executed(executed, space, data->build);
5000 if (!embed) {
5001 isl_set *domain;
5002 domain = isl_ast_build_get_domain(data->build);
5003 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
5004 executed = isl_union_map_intersect_params(executed, domain);
5007 build = isl_ast_build_copy(data->build);
5008 build = isl_ast_build_product(build, space);
5010 list = generate_next_level(executed, build);
5012 list = isl_ast_graft_list_unembed(list, embed);
5014 data->list = isl_ast_graft_list_concat(data->list, list);
5016 return isl_stat_ok;
5019 /* Generate an AST that visits the elements in the range of data->executed
5020 * in the relative order specified by the corresponding domain element(s)
5021 * for those domain elements that belong to "set".
5022 * Add the result to data->list.
5024 * The caller ensures that "set" is a universe domain.
5026 * If the build space S is not parametric, then the space of "set"
5027 * need to be a wrapped relation with S as domain. That is, it needs
5028 * to be of the form
5030 * [S -> T]
5032 * Check this property and pass control to generate_code_in_space
5033 * passing along T.
5034 * If the build space is not parametric, then T is the space of "set".
5036 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
5038 struct isl_generate_code_data *data = user;
5039 isl_space *space, *build_space;
5040 int is_domain;
5042 space = isl_set_get_space(set);
5044 if (isl_set_is_params(data->build->domain))
5045 return generate_code_in_space(data, set, space);
5047 build_space = isl_ast_build_get_space(data->build, data->internal);
5048 space = isl_space_unwrap(space);
5049 is_domain = isl_space_is_domain(build_space, space);
5050 isl_space_free(build_space);
5051 space = isl_space_range(space);
5053 if (is_domain < 0)
5054 goto error;
5055 if (!is_domain)
5056 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5057 "invalid nested schedule space", goto error);
5059 return generate_code_in_space(data, set, space);
5060 error:
5061 isl_set_free(set);
5062 isl_space_free(space);
5063 return isl_stat_error;
5066 /* Generate an AST that visits the elements in the range of "executed"
5067 * in the relative order specified by the corresponding domain element(s).
5069 * "build" is an isl_ast_build that has either been constructed by
5070 * isl_ast_build_from_context or passed to a callback set by
5071 * isl_ast_build_set_create_leaf.
5072 * In the first case, the space of the isl_ast_build is typically
5073 * a parametric space, although this is currently not enforced.
5074 * In the second case, the space is never a parametric space.
5075 * If the space S is not parametric, then the domain space(s) of "executed"
5076 * need to be wrapped relations with S as domain.
5078 * If the domain of "executed" consists of several spaces, then an AST
5079 * is generated for each of them (in arbitrary order) and the results
5080 * are concatenated.
5082 * If "internal" is set, then the domain "S" above refers to the internal
5083 * schedule domain representation. Otherwise, it refers to the external
5084 * representation, as returned by isl_ast_build_get_schedule_space.
5086 * We essentially run over all the spaces in the domain of "executed"
5087 * and call generate_code_set on each of them.
5089 static __isl_give isl_ast_graft_list *generate_code(
5090 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
5091 int internal)
5093 isl_ctx *ctx;
5094 struct isl_generate_code_data data = { 0 };
5095 isl_space *space;
5096 isl_union_set *schedule_domain;
5097 isl_union_map *universe;
5099 if (!build)
5100 goto error;
5101 space = isl_ast_build_get_space(build, 1);
5102 space = isl_space_align_params(space,
5103 isl_union_map_get_space(executed));
5104 space = isl_space_align_params(space,
5105 isl_union_map_get_space(build->options));
5106 build = isl_ast_build_align_params(build, isl_space_copy(space));
5107 executed = isl_union_map_align_params(executed, space);
5108 if (!executed || !build)
5109 goto error;
5111 ctx = isl_ast_build_get_ctx(build);
5113 data.internal = internal;
5114 data.executed = executed;
5115 data.build = build;
5116 data.list = isl_ast_graft_list_alloc(ctx, 0);
5118 universe = isl_union_map_universe(isl_union_map_copy(executed));
5119 schedule_domain = isl_union_map_domain(universe);
5120 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5121 &data) < 0)
5122 data.list = isl_ast_graft_list_free(data.list);
5124 isl_union_set_free(schedule_domain);
5125 isl_union_map_free(executed);
5127 isl_ast_build_free(build);
5128 return data.list;
5129 error:
5130 isl_union_map_free(executed);
5131 isl_ast_build_free(build);
5132 return NULL;
5135 /* Generate an AST that visits the elements in the domain of "schedule"
5136 * in the relative order specified by the corresponding image element(s).
5138 * "build" is an isl_ast_build that has either been constructed by
5139 * isl_ast_build_from_context or passed to a callback set by
5140 * isl_ast_build_set_create_leaf.
5141 * In the first case, the space of the isl_ast_build is typically
5142 * a parametric space, although this is currently not enforced.
5143 * In the second case, the space is never a parametric space.
5144 * If the space S is not parametric, then the range space(s) of "schedule"
5145 * need to be wrapped relations with S as domain.
5147 * If the range of "schedule" consists of several spaces, then an AST
5148 * is generated for each of them (in arbitrary order) and the results
5149 * are concatenated.
5151 * We first initialize the local copies of the relevant options.
5152 * We do this here rather than when the isl_ast_build is created
5153 * because the options may have changed between the construction
5154 * of the isl_ast_build and the call to isl_generate_code.
5156 * The main computation is performed on an inverse schedule (with
5157 * the schedule domain in the domain and the elements to be executed
5158 * in the range) called "executed".
5160 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5161 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5163 isl_ast_graft_list *list;
5164 isl_ast_node *node;
5165 isl_union_map *executed;
5167 build = isl_ast_build_copy(build);
5168 build = isl_ast_build_set_single_valued(build, 0);
5169 schedule = isl_union_map_coalesce(schedule);
5170 schedule = isl_union_map_remove_redundancies(schedule);
5171 executed = isl_union_map_reverse(schedule);
5172 list = generate_code(executed, isl_ast_build_copy(build), 0);
5173 node = isl_ast_node_from_graft_list(list, build);
5174 isl_ast_build_free(build);
5176 return node;
5179 /* The old name for isl_ast_build_node_from_schedule_map.
5180 * It is being kept for backward compatibility, but
5181 * it will be removed in the future.
5183 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5184 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5186 return isl_ast_build_node_from_schedule_map(build, schedule);
5189 /* Generate an AST that visits the elements in the domain of "executed"
5190 * in the relative order specified by the leaf node "node".
5192 * The relation "executed" maps the outer generated loop iterators
5193 * to the domain elements executed by those iterations.
5195 * Simply pass control to generate_inner_level.
5196 * Note that the current build does not refer to any band node, so
5197 * that generate_inner_level will not try to visit the child of
5198 * the leaf node.
5200 * If multiple statement instances reach a leaf,
5201 * then they can be executed in any order.
5202 * Group the list of grafts based on shared guards
5203 * such that identical guards are only generated once
5204 * when the list is eventually passed on to isl_ast_graft_list_fuse.
5206 static __isl_give isl_ast_graft_list *build_ast_from_leaf(
5207 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5208 __isl_take isl_union_map *executed)
5210 isl_ast_graft_list *list;
5212 isl_schedule_node_free(node);
5213 list = generate_inner_level(executed, isl_ast_build_copy(build));
5214 list = isl_ast_graft_list_group_on_guard(list, build);
5215 isl_ast_build_free(build);
5217 return list;
5220 /* Check that the band partial schedule "partial" does not filter out
5221 * any statement instances, as specified by the range of "executed".
5223 static isl_stat check_band_schedule_total_on_instances(
5224 __isl_keep isl_multi_union_pw_aff *partial,
5225 __isl_keep isl_union_map *executed)
5227 isl_bool subset;
5228 isl_union_set *domain, *instances;
5230 instances = isl_union_map_range(isl_union_map_copy(executed));
5231 partial = isl_multi_union_pw_aff_copy(partial);
5232 domain = isl_multi_union_pw_aff_domain(partial);
5233 subset = isl_union_set_is_subset(instances, domain);
5234 isl_union_set_free(domain);
5235 isl_union_set_free(instances);
5237 if (subset < 0)
5238 return isl_stat_error;
5239 if (!subset)
5240 isl_die(isl_union_map_get_ctx(executed), isl_error_invalid,
5241 "band node is not allowed to drop statement instances",
5242 return isl_stat_error);
5243 return isl_stat_ok;
5246 /* Generate an AST that visits the elements in the domain of "executed"
5247 * in the relative order specified by the band node "node" and its descendants.
5249 * The relation "executed" maps the outer generated loop iterators
5250 * to the domain elements executed by those iterations.
5252 * If the band is empty, we continue with its descendants.
5253 * Otherwise, we extend the build and the inverse schedule with
5254 * the additional space/partial schedule and continue generating
5255 * an AST in generate_next_level.
5256 * As soon as we have extended the inverse schedule with the additional
5257 * partial schedule, we look for equalities that may exists between
5258 * the old and the new part.
5260 static __isl_give isl_ast_graft_list *build_ast_from_band(
5261 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5262 __isl_take isl_union_map *executed)
5264 isl_space *space;
5265 isl_multi_union_pw_aff *extra;
5266 isl_union_map *extra_umap;
5267 isl_ast_graft_list *list;
5268 isl_size n1, n2;
5269 isl_size n;
5271 n = isl_schedule_node_band_n_member(node);
5272 if (!build || n < 0 || !executed)
5273 goto error;
5275 if (n == 0)
5276 return build_ast_from_child(build, node, executed);
5278 extra = isl_schedule_node_band_get_partial_schedule(node);
5279 extra = isl_multi_union_pw_aff_align_params(extra,
5280 isl_ast_build_get_space(build, 1));
5281 space = isl_multi_union_pw_aff_get_space(extra);
5283 if (check_band_schedule_total_on_instances(extra, executed) < 0)
5284 executed = isl_union_map_free(executed);
5286 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5287 extra_umap = isl_union_map_reverse(extra_umap);
5289 executed = isl_union_map_domain_product(executed, extra_umap);
5290 executed = isl_union_map_detect_equalities(executed);
5292 n1 = isl_ast_build_dim(build, isl_dim_param);
5293 build = isl_ast_build_product(build, space);
5294 n2 = isl_ast_build_dim(build, isl_dim_param);
5295 if (n1 < 0 || n2 < 0)
5296 build = isl_ast_build_free(build);
5297 else if (n2 > n1)
5298 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5299 "band node is not allowed to introduce new parameters",
5300 build = isl_ast_build_free(build));
5301 build = isl_ast_build_set_schedule_node(build, node);
5303 list = generate_next_level(executed, build);
5305 list = isl_ast_graft_list_unembed(list, 1);
5307 return list;
5308 error:
5309 isl_schedule_node_free(node);
5310 isl_union_map_free(executed);
5311 isl_ast_build_free(build);
5312 return NULL;
5315 /* Hoist a list of grafts (in practice containing a single graft)
5316 * from "sub_build" (which includes extra context information)
5317 * to "build".
5319 * In particular, project out all additional parameters introduced
5320 * by the context node from the enforced constraints and the guard
5321 * of the single graft.
5323 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5324 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5325 __isl_keep isl_ast_build *sub_build)
5327 isl_ast_graft *graft;
5328 isl_basic_set *enforced;
5329 isl_set *guard;
5330 isl_size n_param, extra_param;
5332 n_param = isl_ast_build_dim(build, isl_dim_param);
5333 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5334 if (n_param < 0 || extra_param < 0)
5335 return isl_ast_graft_list_free(list);
5337 if (extra_param == n_param)
5338 return list;
5340 extra_param -= n_param;
5341 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5342 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5343 n_param, extra_param);
5344 enforced = isl_basic_set_remove_unknown_divs(enforced);
5345 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5346 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5347 n_param, extra_param);
5348 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5349 guard = isl_set_compute_divs(guard);
5350 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5351 build, sub_build);
5352 list = isl_ast_graft_list_from_ast_graft(graft);
5354 return list;
5357 /* Generate an AST that visits the elements in the domain of "executed"
5358 * in the relative order specified by the context node "node"
5359 * and its descendants.
5361 * The relation "executed" maps the outer generated loop iterators
5362 * to the domain elements executed by those iterations.
5364 * The context node may introduce additional parameters as well as
5365 * constraints on the outer schedule dimensions or original parameters.
5367 * We add the extra parameters to a new build and the context
5368 * constraints to both the build and (as a single disjunct)
5369 * to the domain of "executed". Since the context constraints
5370 * are specified in terms of the input schedule, we first need
5371 * to map them to the internal schedule domain.
5373 * After constructing the AST from the descendants of "node",
5374 * we combine the list of grafts into a single graft within
5375 * the new build, in order to be able to exploit the additional
5376 * context constraints during this combination.
5378 * Additionally, if the current node is the outermost node in
5379 * the schedule tree (apart from the root domain node), we generate
5380 * all pending guards, again to be able to exploit the additional
5381 * context constraints. We currently do not do this for internal
5382 * context nodes since we may still want to hoist conditions
5383 * to outer AST nodes.
5385 * If the context node introduced any new parameters, then they
5386 * are removed from the set of enforced constraints and guard
5387 * in hoist_out_of_context.
5389 static __isl_give isl_ast_graft_list *build_ast_from_context(
5390 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5391 __isl_take isl_union_map *executed)
5393 isl_set *context;
5394 isl_space *space;
5395 isl_multi_aff *internal2input;
5396 isl_ast_build *sub_build;
5397 isl_ast_graft_list *list;
5398 isl_size n;
5399 isl_size depth;
5401 depth = isl_schedule_node_get_tree_depth(node);
5402 if (depth < 0)
5403 build = isl_ast_build_free(build);
5404 space = isl_ast_build_get_space(build, 1);
5405 context = isl_schedule_node_context_get_context(node);
5406 context = isl_set_align_params(context, space);
5407 sub_build = isl_ast_build_copy(build);
5408 space = isl_set_get_space(context);
5409 sub_build = isl_ast_build_align_params(sub_build, space);
5410 internal2input = isl_ast_build_get_internal2input(sub_build);
5411 context = isl_set_preimage_multi_aff(context, internal2input);
5412 sub_build = isl_ast_build_restrict_generated(sub_build,
5413 isl_set_copy(context));
5414 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5415 executed = isl_union_map_intersect_domain(executed,
5416 isl_union_set_from_set(context));
5418 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5419 node, executed);
5420 n = isl_ast_graft_list_n_ast_graft(list);
5421 if (n < 0)
5422 list = isl_ast_graft_list_free(list);
5424 list = isl_ast_graft_list_fuse(list, sub_build);
5425 if (depth == 1)
5426 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5427 sub_build);
5428 if (n >= 1)
5429 list = hoist_out_of_context(list, build, sub_build);
5431 isl_ast_build_free(build);
5432 isl_ast_build_free(sub_build);
5434 return list;
5437 /* Generate an AST that visits the elements in the domain of "executed"
5438 * in the relative order specified by the expansion 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 * We expand the domain elements by the expansion and
5445 * continue with the descendants of the node.
5447 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5448 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5449 __isl_take isl_union_map *executed)
5451 isl_union_map *expansion;
5452 isl_size n1, n2;
5454 expansion = isl_schedule_node_expansion_get_expansion(node);
5455 expansion = isl_union_map_align_params(expansion,
5456 isl_union_map_get_space(executed));
5458 n1 = isl_union_map_dim(executed, isl_dim_param);
5459 executed = isl_union_map_apply_range(executed, expansion);
5460 n2 = isl_union_map_dim(executed, isl_dim_param);
5461 if (n1 < 0 || n2 < 0)
5462 goto error;
5463 if (n2 > n1)
5464 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5465 "expansion node is not allowed to introduce "
5466 "new parameters", goto error);
5468 return build_ast_from_child(build, node, executed);
5469 error:
5470 isl_ast_build_free(build);
5471 isl_schedule_node_free(node);
5472 isl_union_map_free(executed);
5473 return NULL;
5476 /* Generate an AST that visits the elements in the domain of "executed"
5477 * in the relative order specified by the extension node "node" and
5478 * its descendants.
5480 * The relation "executed" maps the outer generated loop iterators
5481 * to the domain elements executed by those iterations.
5483 * Extend the inverse schedule with the extension applied to current
5484 * set of generated constraints. Since the extension if formulated
5485 * in terms of the input schedule, it first needs to be transformed
5486 * to refer to the internal schedule.
5488 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5489 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5490 __isl_take isl_union_map *executed)
5492 isl_union_set *schedule_domain;
5493 isl_union_map *extension;
5494 isl_set *set;
5496 set = isl_ast_build_get_generated(build);
5497 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5498 schedule_domain = isl_union_set_from_set(set);
5500 extension = isl_schedule_node_extension_get_extension(node);
5502 extension = isl_union_map_preimage_domain_multi_aff(extension,
5503 isl_multi_aff_copy(build->internal2input));
5504 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5505 extension = isl_ast_build_substitute_values_union_map_domain(build,
5506 extension);
5507 executed = isl_union_map_union(executed, extension);
5509 return build_ast_from_child(build, node, executed);
5512 /* Generate an AST that visits the elements in the domain of "executed"
5513 * in the relative order specified by the filter node "node" and
5514 * its descendants.
5516 * The relation "executed" maps the outer generated loop iterators
5517 * to the domain elements executed by those iterations.
5519 * We simply intersect the iteration domain (i.e., the range of "executed")
5520 * with the filter and continue with the descendants of the node,
5521 * unless the resulting inverse schedule is empty, in which
5522 * case we return an empty list.
5524 * If the result of the intersection is equal to the original "executed"
5525 * relation, then keep the original representation since the intersection
5526 * may have unnecessarily broken up the relation into a greater number
5527 * of disjuncts.
5529 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5530 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5531 __isl_take isl_union_map *executed)
5533 isl_ctx *ctx;
5534 isl_union_set *filter;
5535 isl_union_map *orig;
5536 isl_ast_graft_list *list;
5537 int empty;
5538 isl_bool unchanged;
5539 isl_size n1, n2;
5541 orig = isl_union_map_copy(executed);
5542 if (!build || !node || !executed)
5543 goto error;
5545 filter = isl_schedule_node_filter_get_filter(node);
5546 filter = isl_union_set_align_params(filter,
5547 isl_union_map_get_space(executed));
5548 n1 = isl_union_map_dim(executed, isl_dim_param);
5549 executed = isl_union_map_intersect_range(executed, filter);
5550 n2 = isl_union_map_dim(executed, isl_dim_param);
5551 if (n1 < 0 || n2 < 0)
5552 goto error;
5553 if (n2 > n1)
5554 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5555 "filter node is not allowed to introduce "
5556 "new parameters", goto error);
5558 unchanged = isl_union_map_is_subset(orig, executed);
5559 empty = isl_union_map_is_empty(executed);
5560 if (unchanged < 0 || empty < 0)
5561 goto error;
5562 if (unchanged) {
5563 isl_union_map_free(executed);
5564 return build_ast_from_child(build, node, orig);
5566 isl_union_map_free(orig);
5567 if (!empty)
5568 return build_ast_from_child(build, node, executed);
5570 ctx = isl_ast_build_get_ctx(build);
5571 list = isl_ast_graft_list_alloc(ctx, 0);
5572 isl_ast_build_free(build);
5573 isl_schedule_node_free(node);
5574 isl_union_map_free(executed);
5575 return list;
5576 error:
5577 isl_ast_build_free(build);
5578 isl_schedule_node_free(node);
5579 isl_union_map_free(executed);
5580 isl_union_map_free(orig);
5581 return NULL;
5584 /* Generate an AST that visits the elements in the domain of "executed"
5585 * in the relative order specified by the guard node "node" and
5586 * its descendants.
5588 * The relation "executed" maps the outer generated loop iterators
5589 * to the domain elements executed by those iterations.
5591 * Ensure that the associated guard is enforced by the outer AST
5592 * constructs by adding it to the guard of the graft.
5593 * Since we know that we will enforce the guard, we can also include it
5594 * in the generated constraints used to construct an AST for
5595 * the descendant nodes.
5597 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5598 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5599 __isl_take isl_union_map *executed)
5601 isl_space *space;
5602 isl_set *guard, *hoisted;
5603 isl_basic_set *enforced;
5604 isl_ast_build *sub_build;
5605 isl_ast_graft *graft;
5606 isl_ast_graft_list *list;
5607 isl_size n1, n2, n;
5609 space = isl_ast_build_get_space(build, 1);
5610 guard = isl_schedule_node_guard_get_guard(node);
5611 n1 = isl_space_dim(space, isl_dim_param);
5612 guard = isl_set_align_params(guard, space);
5613 n2 = isl_set_dim(guard, isl_dim_param);
5614 if (n1 < 0 || n2 < 0)
5615 guard = isl_set_free(guard);
5616 else if (n2 > n1)
5617 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5618 "guard node is not allowed to introduce "
5619 "new parameters", guard = isl_set_free(guard));
5620 guard = isl_set_preimage_multi_aff(guard,
5621 isl_multi_aff_copy(build->internal2input));
5622 guard = isl_ast_build_specialize(build, guard);
5623 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5625 sub_build = isl_ast_build_copy(build);
5626 sub_build = isl_ast_build_restrict_generated(sub_build,
5627 isl_set_copy(guard));
5629 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5630 node, executed);
5632 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5633 n = isl_set_n_basic_set(hoisted);
5634 if (n < 0)
5635 list = isl_ast_graft_list_free(list);
5636 if (n > 1)
5637 list = isl_ast_graft_list_gist_guards(list,
5638 isl_set_copy(hoisted));
5639 guard = isl_set_intersect(guard, hoisted);
5640 enforced = extract_shared_enforced(list, build);
5641 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5642 build, sub_build);
5644 isl_ast_build_free(sub_build);
5645 isl_ast_build_free(build);
5646 return isl_ast_graft_list_from_ast_graft(graft);
5649 /* Call the before_each_mark callback, if requested by the user.
5651 * Return 0 on success and -1 on error.
5653 * The caller is responsible for recording the current inverse schedule
5654 * in "build".
5656 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5657 __isl_keep isl_ast_build *build)
5659 if (!build)
5660 return isl_stat_error;
5661 if (!build->before_each_mark)
5662 return isl_stat_ok;
5663 return build->before_each_mark(mark, build,
5664 build->before_each_mark_user);
5667 /* Call the after_each_mark callback, if requested by the user.
5669 * The caller is responsible for recording the current inverse schedule
5670 * in "build".
5672 static __isl_give isl_ast_graft *after_each_mark(
5673 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5675 if (!graft || !build)
5676 return isl_ast_graft_free(graft);
5677 if (!build->after_each_mark)
5678 return graft;
5679 graft->node = build->after_each_mark(graft->node, build,
5680 build->after_each_mark_user);
5681 if (!graft->node)
5682 return isl_ast_graft_free(graft);
5683 return graft;
5687 /* Generate an AST that visits the elements in the domain of "executed"
5688 * in the relative order specified by the mark node "node" and
5689 * its descendants.
5691 * The relation "executed" maps the outer generated loop iterators
5692 * to the domain elements executed by those iterations.
5694 * Since we may be calling before_each_mark and after_each_mark
5695 * callbacks, we record the current inverse schedule in the build.
5697 * We generate an AST for the child of the mark node, combine
5698 * the graft list into a single graft and then insert the mark
5699 * in the AST of that single graft.
5701 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5702 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5703 __isl_take isl_union_map *executed)
5705 isl_id *mark;
5706 isl_ast_graft *graft;
5707 isl_ast_graft_list *list;
5708 isl_size n;
5710 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5712 mark = isl_schedule_node_mark_get_id(node);
5713 if (before_each_mark(mark, build) < 0)
5714 node = isl_schedule_node_free(node);
5716 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5717 list = isl_ast_graft_list_fuse(list, build);
5718 n = isl_ast_graft_list_n_ast_graft(list);
5719 if (n < 0)
5720 list = isl_ast_graft_list_free(list);
5721 if (n == 0) {
5722 isl_id_free(mark);
5723 } else {
5724 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5725 graft = isl_ast_graft_insert_mark(graft, mark);
5726 graft = after_each_mark(graft, build);
5727 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5729 isl_ast_build_free(build);
5731 return list;
5734 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5735 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5736 __isl_take isl_union_map *executed);
5738 /* Generate an AST that visits the elements in the domain of "executed"
5739 * in the relative order specified by the sequence (or set) node "node" and
5740 * its descendants.
5742 * The relation "executed" maps the outer generated loop iterators
5743 * to the domain elements executed by those iterations.
5745 * We simply generate an AST for each of the children and concatenate
5746 * the results.
5748 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5749 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5750 __isl_take isl_union_map *executed)
5752 int i;
5753 isl_size n;
5754 isl_ctx *ctx;
5755 isl_ast_graft_list *list;
5757 ctx = isl_ast_build_get_ctx(build);
5758 list = isl_ast_graft_list_alloc(ctx, 0);
5760 n = isl_schedule_node_n_children(node);
5761 if (n < 0)
5762 list = isl_ast_graft_list_free(list);
5763 for (i = 0; i < n; ++i) {
5764 isl_schedule_node *child;
5765 isl_ast_graft_list *list_i;
5767 child = isl_schedule_node_get_child(node, i);
5768 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5769 child, isl_union_map_copy(executed));
5770 list = isl_ast_graft_list_concat(list, list_i);
5772 isl_ast_build_free(build);
5773 isl_schedule_node_free(node);
5774 isl_union_map_free(executed);
5776 return list;
5779 /* Generate an AST that visits the elements in the domain of "executed"
5780 * in the relative order specified by the node "node" and its descendants.
5782 * The relation "executed" maps the outer generated loop iterators
5783 * to the domain elements executed by those iterations.
5785 * The node types are handled in separate functions.
5786 * Set nodes are currently treated in the same way as sequence nodes.
5787 * The children of a set node may be executed in any order,
5788 * including the order of the children.
5790 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5791 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5792 __isl_take isl_union_map *executed)
5794 enum isl_schedule_node_type type;
5796 type = isl_schedule_node_get_type(node);
5798 switch (type) {
5799 case isl_schedule_node_error:
5800 goto error;
5801 case isl_schedule_node_leaf:
5802 return build_ast_from_leaf(build, node, executed);
5803 case isl_schedule_node_band:
5804 return build_ast_from_band(build, node, executed);
5805 case isl_schedule_node_context:
5806 return build_ast_from_context(build, node, executed);
5807 case isl_schedule_node_domain:
5808 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5809 "unexpected internal domain node", goto error);
5810 case isl_schedule_node_expansion:
5811 return build_ast_from_expansion(build, node, executed);
5812 case isl_schedule_node_extension:
5813 return build_ast_from_extension(build, node, executed);
5814 case isl_schedule_node_filter:
5815 return build_ast_from_filter(build, node, executed);
5816 case isl_schedule_node_guard:
5817 return build_ast_from_guard(build, node, executed);
5818 case isl_schedule_node_mark:
5819 return build_ast_from_mark(build, node, executed);
5820 case isl_schedule_node_sequence:
5821 case isl_schedule_node_set:
5822 return build_ast_from_sequence(build, node, executed);
5825 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5826 "unhandled type", goto error);
5827 error:
5828 isl_union_map_free(executed);
5829 isl_schedule_node_free(node);
5830 isl_ast_build_free(build);
5832 return NULL;
5835 /* Generate an AST that visits the elements in the domain of "executed"
5836 * in the relative order specified by the (single) child of "node" and
5837 * its descendants.
5839 * The relation "executed" maps the outer generated loop iterators
5840 * to the domain elements executed by those iterations.
5842 * This function is never called on a leaf, set or sequence node,
5843 * so the node always has exactly one child.
5845 static __isl_give isl_ast_graft_list *build_ast_from_child(
5846 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5847 __isl_take isl_union_map *executed)
5849 node = isl_schedule_node_child(node, 0);
5850 return build_ast_from_schedule_node(build, node, executed);
5853 /* Generate an AST that visits the elements in the domain of the domain
5854 * node "node" in the relative order specified by its descendants.
5856 * An initial inverse schedule is created that maps a zero-dimensional
5857 * schedule space to the node domain.
5858 * The input "build" is assumed to have a parametric domain and
5859 * is replaced by the same zero-dimensional schedule space.
5861 * We also add some of the parameter constraints in the build domain
5862 * to the executed relation. Adding these constraints
5863 * allows for an earlier detection of conflicts in some cases.
5864 * However, we do not want to divide the executed relation into
5865 * more disjuncts than necessary. We therefore approximate
5866 * the constraints on the parameters by a single disjunct set.
5868 static __isl_give isl_ast_node *build_ast_from_domain(
5869 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5871 isl_ctx *ctx;
5872 isl_union_set *domain, *schedule_domain;
5873 isl_union_map *executed;
5874 isl_space *space;
5875 isl_set *set;
5876 isl_ast_graft_list *list;
5877 isl_ast_node *ast;
5878 int is_params;
5880 if (!build)
5881 goto error;
5883 ctx = isl_ast_build_get_ctx(build);
5884 space = isl_ast_build_get_space(build, 1);
5885 is_params = isl_space_is_params(space);
5886 isl_space_free(space);
5887 if (is_params < 0)
5888 goto error;
5889 if (!is_params)
5890 isl_die(ctx, isl_error_unsupported,
5891 "expecting parametric initial context", goto error);
5893 domain = isl_schedule_node_domain_get_domain(node);
5894 domain = isl_union_set_coalesce(domain);
5896 space = isl_union_set_get_space(domain);
5897 space = isl_space_set_from_params(space);
5898 build = isl_ast_build_product(build, space);
5900 set = isl_ast_build_get_domain(build);
5901 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5902 schedule_domain = isl_union_set_from_set(set);
5904 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5905 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5906 ast = isl_ast_node_from_graft_list(list, build);
5907 isl_ast_build_free(build);
5909 return ast;
5910 error:
5911 isl_schedule_node_free(node);
5912 isl_ast_build_free(build);
5913 return NULL;
5916 /* Generate an AST that visits the elements in the domain of "schedule"
5917 * in the relative order specified by the schedule tree.
5919 * "build" is an isl_ast_build that has been created using
5920 * isl_ast_build_alloc or isl_ast_build_from_context based
5921 * on a parametric set.
5923 * The construction starts at the root node of the schedule,
5924 * which is assumed to be a domain node.
5926 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5927 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5929 isl_ctx *ctx;
5930 isl_schedule_node *node;
5932 if (!build || !schedule)
5933 goto error;
5935 ctx = isl_ast_build_get_ctx(build);
5937 node = isl_schedule_get_root(schedule);
5938 if (!node)
5939 goto error;
5940 isl_schedule_free(schedule);
5942 build = isl_ast_build_copy(build);
5943 build = isl_ast_build_set_single_valued(build, 0);
5944 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5945 isl_die(ctx, isl_error_unsupported,
5946 "expecting root domain node",
5947 build = isl_ast_build_free(build));
5948 return build_ast_from_domain(build, node);
5949 error:
5950 isl_schedule_free(schedule);
5951 return NULL;