isl_basic_map_make_strides_explicit: move down isl_basic_map_get_ctx call
[isl.git] / isl_ast_codegen.c
blob89cc725c888918e6faf909cabbc7f8302a31d2ea
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 int depth, has_stride;
747 isl_space *space;
748 isl_set *dom, *set;
750 depth = isl_ast_build_get_depth(build);
751 has_stride = isl_ast_build_has_stride(build, depth);
752 if (!has_stride && !degenerate)
753 return guard;
755 space = isl_basic_set_get_space(bounds);
756 dom = isl_set_universe(space);
758 if (degenerate) {
759 bounds = isl_basic_set_copy(bounds);
760 bounds = isl_basic_set_drop_constraints_not_involving_dims(
761 bounds, isl_dim_set, depth, 1);
762 set = isl_set_from_basic_set(bounds);
763 dom = isl_set_intersect(dom, set);
766 if (has_stride) {
767 set = isl_ast_build_get_stride_constraint(build);
768 dom = isl_set_intersect(dom, set);
771 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
772 dom = isl_ast_build_compute_gist(build, dom);
773 guard = isl_set_intersect(guard, dom);
775 return guard;
778 /* Update "graft" based on "sub_build" for the degenerate case.
780 * "build" is the build in which graft->node was created
781 * "sub_build" contains information about the current level itself,
782 * including the single value attained.
784 * We set the initialization part of the for loop to the single
785 * value attained by the current dimension.
786 * The increment and condition are not strictly needed as the are known
787 * to be "1" and "iterator <= value" respectively.
789 static __isl_give isl_ast_graft *refine_degenerate(
790 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
791 __isl_keep isl_ast_build *sub_build)
793 isl_pw_aff *value;
795 if (!graft || !sub_build)
796 return isl_ast_graft_free(graft);
798 value = isl_pw_aff_copy(sub_build->value);
800 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
801 value);
802 if (!graft->node->u.f.init)
803 return isl_ast_graft_free(graft);
805 return graft;
808 /* Return the intersection of constraints in "list" as a set.
810 static __isl_give isl_set *intersect_constraints(
811 __isl_keep isl_constraint_list *list)
813 int i;
814 isl_size n;
815 isl_basic_set *bset;
817 n = isl_constraint_list_n_constraint(list);
818 if (n < 0)
819 return NULL;
820 if (n < 1)
821 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
822 "expecting at least one constraint", return NULL);
824 bset = isl_basic_set_from_constraint(
825 isl_constraint_list_get_constraint(list, 0));
826 for (i = 1; i < n; ++i) {
827 isl_basic_set *bset_i;
829 bset_i = isl_basic_set_from_constraint(
830 isl_constraint_list_get_constraint(list, i));
831 bset = isl_basic_set_intersect(bset, bset_i);
834 return isl_set_from_basic_set(bset);
837 /* Compute the constraints on the outer dimensions enforced by
838 * graft->node and add those constraints to graft->enforced,
839 * in case the upper bound is expressed as a set "upper".
841 * In particular, if l(...) is a lower bound in "lower", and
843 * -a i + f(...) >= 0 or a i <= f(...)
845 * is an upper bound ocnstraint on the current dimension i,
846 * then the for loop enforces the constraint
848 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
850 * We therefore simply take each lower bound in turn, plug it into
851 * the upper bounds and compute the intersection over all lower bounds.
853 * If a lower bound is a rational expression, then
854 * isl_basic_set_preimage_multi_aff will force this rational
855 * expression to have only integer values. However, the loop
856 * itself does not enforce this integrality constraint. We therefore
857 * use the ceil of the lower bounds instead of the lower bounds themselves.
858 * Other constraints will make sure that the for loop is only executed
859 * when each of the lower bounds attains an integral value.
860 * In particular, potentially rational values only occur in
861 * lower_bound if the offset is a (seemingly) rational expression,
862 * but then outer conditions will make sure that this rational expression
863 * only attains integer values.
865 static __isl_give isl_ast_graft *set_enforced_from_set(
866 __isl_take isl_ast_graft *graft,
867 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
869 isl_space *space;
870 isl_basic_set *enforced;
871 isl_pw_multi_aff *pma;
872 int i;
873 isl_size n;
875 n = isl_pw_aff_list_n_pw_aff(lower);
876 if (!graft || n < 0)
877 return isl_ast_graft_free(graft);
879 space = isl_set_get_space(upper);
880 enforced = isl_basic_set_universe(isl_space_copy(space));
882 space = isl_space_map_from_set(space);
883 pma = isl_pw_multi_aff_identity(space);
885 for (i = 0; i < n; ++i) {
886 isl_pw_aff *pa;
887 isl_set *enforced_i;
888 isl_basic_set *hull;
889 isl_pw_multi_aff *pma_i;
891 pa = isl_pw_aff_list_get_pw_aff(lower, i);
892 pa = isl_pw_aff_ceil(pa);
893 pma_i = isl_pw_multi_aff_copy(pma);
894 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
895 enforced_i = isl_set_copy(upper);
896 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
897 hull = isl_set_simple_hull(enforced_i);
898 enforced = isl_basic_set_intersect(enforced, hull);
901 isl_pw_multi_aff_free(pma);
903 graft = isl_ast_graft_enforce(graft, enforced);
905 return graft;
908 /* Compute the constraints on the outer dimensions enforced by
909 * graft->node and add those constraints to graft->enforced,
910 * in case the upper bound is expressed as
911 * a list of affine expressions "upper".
913 * The enforced condition is that each lower bound expression is less
914 * than or equal to each upper bound expression.
916 static __isl_give isl_ast_graft *set_enforced_from_list(
917 __isl_take isl_ast_graft *graft,
918 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
920 isl_set *cond;
921 isl_basic_set *enforced;
923 lower = isl_pw_aff_list_copy(lower);
924 upper = isl_pw_aff_list_copy(upper);
925 cond = isl_pw_aff_list_le_set(lower, upper);
926 enforced = isl_set_simple_hull(cond);
927 graft = isl_ast_graft_enforce(graft, enforced);
929 return graft;
932 /* Does "aff" have a negative constant term?
934 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
935 __isl_take isl_aff *aff, void *user)
937 int *neg = user;
938 isl_val *v;
940 v = isl_aff_get_constant_val(aff);
941 *neg = isl_val_is_neg(v);
942 isl_val_free(v);
943 isl_set_free(set);
944 isl_aff_free(aff);
946 return *neg ? isl_stat_ok : isl_stat_error;
949 /* Does "pa" have a negative constant term over its entire domain?
951 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
952 void *user)
954 isl_stat r;
955 int *neg = user;
957 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
958 isl_pw_aff_free(pa);
960 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
963 /* Does each element in "list" have a negative constant term?
965 * The callback terminates the iteration as soon an element has been
966 * found that does not have a negative constant term.
968 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
970 int neg = 1;
972 if (isl_pw_aff_list_foreach(list,
973 &pw_aff_constant_is_negative, &neg) < 0 && neg)
974 return -1;
976 return neg;
979 /* Add 1 to each of the elements in "list", where each of these elements
980 * is defined over the internal schedule space of "build".
982 static __isl_give isl_pw_aff_list *list_add_one(
983 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
985 int i;
986 isl_size n;
987 isl_space *space;
988 isl_aff *aff;
989 isl_pw_aff *one;
991 n = isl_pw_aff_list_n_pw_aff(list);
992 if (n < 0)
993 return isl_pw_aff_list_free(list);
995 space = isl_ast_build_get_space(build, 1);
996 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
997 aff = isl_aff_add_constant_si(aff, 1);
998 one = isl_pw_aff_from_aff(aff);
1000 for (i = 0; i < n; ++i) {
1001 isl_pw_aff *pa;
1002 pa = isl_pw_aff_list_get_pw_aff(list, i);
1003 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
1004 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1007 isl_pw_aff_free(one);
1009 return list;
1012 /* Set the condition part of the for node graft->node in case
1013 * the upper bound is represented as a list of piecewise affine expressions.
1015 * In particular, set the condition to
1017 * iterator <= min(list of upper bounds)
1019 * If each of the upper bounds has a negative constant term, then
1020 * set the condition to
1022 * iterator < min(list of (upper bound + 1)s)
1025 static __isl_give isl_ast_graft *set_for_cond_from_list(
1026 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
1027 __isl_keep isl_ast_build *build)
1029 int neg;
1030 isl_ast_expr *bound, *iterator, *cond;
1031 enum isl_ast_expr_op_type type = isl_ast_expr_op_le;
1033 if (!graft || !list)
1034 return isl_ast_graft_free(graft);
1036 neg = list_constant_is_negative(list);
1037 if (neg < 0)
1038 return isl_ast_graft_free(graft);
1039 list = isl_pw_aff_list_copy(list);
1040 if (neg) {
1041 list = list_add_one(list, build);
1042 type = isl_ast_expr_op_lt;
1045 bound = reduce_list(isl_ast_expr_op_min, list, build);
1046 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1047 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1048 graft->node->u.f.cond = cond;
1050 isl_pw_aff_list_free(list);
1051 if (!graft->node->u.f.cond)
1052 return isl_ast_graft_free(graft);
1053 return graft;
1056 /* Set the condition part of the for node graft->node in case
1057 * the upper bound is represented as a set.
1059 static __isl_give isl_ast_graft *set_for_cond_from_set(
1060 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1061 __isl_keep isl_ast_build *build)
1063 isl_ast_expr *cond;
1065 if (!graft)
1066 return NULL;
1068 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1069 graft->node->u.f.cond = cond;
1070 if (!graft->node->u.f.cond)
1071 return isl_ast_graft_free(graft);
1072 return graft;
1075 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1076 * the current dimension.
1078 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1080 int depth;
1081 isl_val *v;
1082 isl_ctx *ctx;
1084 if (!build)
1085 return NULL;
1086 ctx = isl_ast_build_get_ctx(build);
1087 depth = isl_ast_build_get_depth(build);
1089 if (!isl_ast_build_has_stride(build, depth))
1090 return isl_ast_expr_alloc_int_si(ctx, 1);
1092 v = isl_ast_build_get_stride(build, depth);
1093 return isl_ast_expr_from_val(v);
1096 /* Should we express the loop condition as
1098 * iterator <= min(list of upper bounds)
1100 * or as a conjunction of constraints?
1102 * The first is constructed from a list of upper bounds.
1103 * The second is constructed from a set.
1105 * If there are no upper bounds in "constraints", then this could mean
1106 * that "domain" simply doesn't have an upper bound or that we didn't
1107 * pick any upper bound. In the first case, we want to generate the
1108 * loop condition as a(n empty) conjunction of constraints
1109 * In the second case, we will compute
1110 * a single upper bound from "domain" and so we use the list form.
1112 * If there are upper bounds in "constraints",
1113 * then we use the list form iff the atomic_upper_bound option is set.
1115 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1116 __isl_keep isl_set *domain, int depth)
1118 if (n_upper > 0)
1119 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1120 else
1121 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1124 /* Fill in the expressions of the for node in graft->node.
1126 * In particular,
1127 * - set the initialization part of the loop to the maximum of the lower bounds
1128 * - extract the increment from the stride of the current dimension
1129 * - construct the for condition either based on a list of upper bounds
1130 * or on a set of upper bound constraints.
1132 static __isl_give isl_ast_graft *set_for_node_expressions(
1133 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1134 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1135 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1137 isl_ast_node *node;
1139 if (!graft)
1140 return NULL;
1142 build = isl_ast_build_copy(build);
1144 node = graft->node;
1145 node->u.f.init = reduce_list(isl_ast_expr_op_max, lower, build);
1146 node->u.f.inc = for_inc(build);
1148 if (!node->u.f.init || !node->u.f.inc)
1149 graft = isl_ast_graft_free(graft);
1151 if (use_list)
1152 graft = set_for_cond_from_list(graft, upper_list, build);
1153 else
1154 graft = set_for_cond_from_set(graft, upper_set, build);
1156 isl_ast_build_free(build);
1158 return graft;
1161 /* Update "graft" based on "bounds" and "domain" for the generic,
1162 * non-degenerate, case.
1164 * "c_lower" and "c_upper" contain the lower and upper bounds
1165 * that the loop node should express.
1166 * "domain" is the subset of the intersection of the constraints
1167 * for which some code is executed.
1169 * There may be zero lower bounds or zero upper bounds in "constraints"
1170 * in case the list of constraints was created
1171 * based on the atomic option or based on separation with explicit bounds.
1172 * In that case, we use "domain" to derive lower and/or upper bounds.
1174 * We first compute a list of one or more lower bounds.
1176 * Then we decide if we want to express the condition as
1178 * iterator <= min(list of upper bounds)
1180 * or as a conjunction of constraints.
1182 * The set of enforced constraints is then computed either based on
1183 * a list of upper bounds or on a set of upper bound constraints.
1184 * We do not compute any enforced constraints if we were forced
1185 * to compute a lower or upper bound using exact_bound. The domains
1186 * of the resulting expressions may imply some bounds on outer dimensions
1187 * that we do not want to appear in the enforced constraints since
1188 * they are not actually enforced by the corresponding code.
1190 * Finally, we fill in the expressions of the for node.
1192 static __isl_give isl_ast_graft *refine_generic_bounds(
1193 __isl_take isl_ast_graft *graft,
1194 __isl_take isl_constraint_list *c_lower,
1195 __isl_take isl_constraint_list *c_upper,
1196 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1198 int depth;
1199 isl_ctx *ctx;
1200 isl_pw_aff_list *lower;
1201 int use_list;
1202 isl_set *upper_set = NULL;
1203 isl_pw_aff_list *upper_list = NULL;
1204 isl_size n_lower, n_upper;
1206 if (!graft || !c_lower || !c_upper || !build)
1207 goto error;
1209 depth = isl_ast_build_get_depth(build);
1210 ctx = isl_ast_graft_get_ctx(graft);
1212 n_lower = isl_constraint_list_n_constraint(c_lower);
1213 n_upper = isl_constraint_list_n_constraint(c_upper);
1214 if (n_lower < 0 || n_upper < 0)
1215 goto error;
1217 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1219 lower = lower_bounds(c_lower, depth, domain, build);
1221 if (use_list)
1222 upper_list = upper_bounds(c_upper, depth, domain, build);
1223 else if (n_upper > 0)
1224 upper_set = intersect_constraints(c_upper);
1225 else
1226 upper_set = isl_set_universe(isl_set_get_space(domain));
1228 if (n_lower == 0 || n_upper == 0)
1230 else if (use_list)
1231 graft = set_enforced_from_list(graft, lower, upper_list);
1232 else
1233 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1235 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1236 upper_set, build);
1238 isl_pw_aff_list_free(lower);
1239 isl_pw_aff_list_free(upper_list);
1240 isl_set_free(upper_set);
1241 isl_constraint_list_free(c_lower);
1242 isl_constraint_list_free(c_upper);
1244 return graft;
1245 error:
1246 isl_constraint_list_free(c_lower);
1247 isl_constraint_list_free(c_upper);
1248 return isl_ast_graft_free(graft);
1251 /* Internal data structure used inside count_constraints to keep
1252 * track of the number of constraints that are independent of dimension "pos",
1253 * the lower bounds in "pos" and the upper bounds in "pos".
1255 struct isl_ast_count_constraints_data {
1256 int pos;
1258 int n_indep;
1259 int n_lower;
1260 int n_upper;
1263 /* Increment data->n_indep, data->lower or data->upper depending
1264 * on whether "c" is independenct of dimensions data->pos,
1265 * a lower bound or an upper bound.
1267 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1269 struct isl_ast_count_constraints_data *data = user;
1271 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1272 data->n_lower++;
1273 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1274 data->n_upper++;
1275 else
1276 data->n_indep++;
1278 isl_constraint_free(c);
1280 return isl_stat_ok;
1283 /* Update "graft" based on "bounds" and "domain" for the generic,
1284 * non-degenerate, case.
1286 * "list" respresent the list of bounds that need to be encoded by
1287 * the for loop. Only the constraints that involve the iterator
1288 * are relevant here. The other constraints are taken care of by
1289 * the caller and are included in the generated constraints of "build".
1290 * "domain" is the subset of the intersection of the constraints
1291 * for which some code is executed.
1292 * "build" is the build in which graft->node was created.
1294 * We separate lower bounds, upper bounds and constraints that
1295 * are independent of the loop iterator.
1297 * The actual for loop bounds are generated in refine_generic_bounds.
1299 static __isl_give isl_ast_graft *refine_generic_split(
1300 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1301 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1303 struct isl_ast_count_constraints_data data;
1304 isl_constraint_list *lower;
1305 isl_constraint_list *upper;
1307 if (!list)
1308 return isl_ast_graft_free(graft);
1310 data.pos = isl_ast_build_get_depth(build);
1312 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1313 if (!list)
1314 return isl_ast_graft_free(graft);
1316 data.n_indep = data.n_lower = data.n_upper = 0;
1317 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1318 isl_constraint_list_free(list);
1319 return isl_ast_graft_free(graft);
1322 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1323 upper = isl_constraint_list_copy(lower);
1324 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1325 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1327 return refine_generic_bounds(graft, lower, upper, domain, build);
1330 /* Update "graft" based on "bounds" and "domain" for the generic,
1331 * non-degenerate, case.
1333 * "bounds" respresent the bounds that need to be encoded by
1334 * the for loop (or a guard around the for loop).
1335 * "domain" is the subset of "bounds" for which some code is executed.
1336 * "build" is the build in which graft->node was created.
1338 * We break up "bounds" into a list of constraints and continue with
1339 * refine_generic_split.
1341 static __isl_give isl_ast_graft *refine_generic(
1342 __isl_take isl_ast_graft *graft,
1343 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1344 __isl_keep isl_ast_build *build)
1346 isl_constraint_list *list;
1348 if (!build || !graft)
1349 return isl_ast_graft_free(graft);
1351 list = isl_basic_set_get_constraint_list(bounds);
1353 graft = refine_generic_split(graft, list, domain, build);
1355 return graft;
1358 /* Create a for node for the current level.
1360 * Mark the for node degenerate if "degenerate" is set.
1362 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1363 int degenerate)
1365 int depth;
1366 isl_id *id;
1367 isl_ast_node *node;
1369 if (!build)
1370 return NULL;
1372 depth = isl_ast_build_get_depth(build);
1373 id = isl_ast_build_get_iterator_id(build, depth);
1374 node = isl_ast_node_alloc_for(id);
1375 if (degenerate)
1376 node = isl_ast_node_for_mark_degenerate(node);
1378 return node;
1381 /* If the ast_build_exploit_nested_bounds option is set, then return
1382 * the constraints enforced by all elements in "list".
1383 * Otherwise, return the universe.
1385 static __isl_give isl_basic_set *extract_shared_enforced(
1386 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1388 isl_ctx *ctx;
1389 isl_space *space;
1391 if (!list)
1392 return NULL;
1394 ctx = isl_ast_graft_list_get_ctx(list);
1395 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1396 return isl_ast_graft_list_extract_shared_enforced(list, build);
1398 space = isl_ast_build_get_space(build, 1);
1399 return isl_basic_set_universe(space);
1402 /* Return the pending constraints of "build" that are not already taken
1403 * care of (by a combination of "enforced" and the generated constraints
1404 * of "build").
1406 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1407 __isl_keep isl_basic_set *enforced)
1409 isl_set *guard, *context;
1411 guard = isl_ast_build_get_pending(build);
1412 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1413 context = isl_set_intersect(context,
1414 isl_ast_build_get_generated(build));
1415 return isl_set_gist(guard, context);
1418 /* Create an AST node for the current dimension based on
1419 * the schedule domain "bounds" and return the node encapsulated
1420 * in an isl_ast_graft.
1422 * "executed" is the current inverse schedule, taking into account
1423 * the bounds in "bounds"
1424 * "domain" is the domain of "executed", with inner dimensions projected out.
1425 * It may be a strict subset of "bounds" in case "bounds" was created
1426 * based on the atomic option or based on separation with explicit bounds.
1428 * "domain" may satisfy additional equalities that result
1429 * from intersecting "executed" with "bounds" in add_node.
1430 * It may also satisfy some global constraints that were dropped out because
1431 * we performed separation with explicit bounds.
1432 * The very first step is then to copy these constraints to "bounds".
1434 * Since we may be calling before_each_for and after_each_for
1435 * callbacks, we record the current inverse schedule in the build.
1437 * We consider three builds,
1438 * "build" is the one in which the current level is created,
1439 * "body_build" is the build in which the next level is created,
1440 * "sub_build" is essentially the same as "body_build", except that
1441 * the depth has not been increased yet.
1443 * "build" already contains information (in strides and offsets)
1444 * about the strides at the current level, but this information is not
1445 * reflected in the build->domain.
1446 * We first add this information and the "bounds" to the sub_build->domain.
1447 * isl_ast_build_set_loop_bounds adds the stride information and
1448 * checks whether the current dimension attains
1449 * only a single value and whether this single value can be represented using
1450 * a single affine expression.
1451 * In the first case, the current level is considered "degenerate".
1452 * In the second, sub-case, the current level is considered "eliminated".
1453 * Eliminated levels don't need to be reflected in the AST since we can
1454 * simply plug in the affine expression. For degenerate, but non-eliminated,
1455 * levels, we do introduce a for node, but mark is as degenerate so that
1456 * it can be printed as an assignment of the single value to the loop
1457 * "iterator".
1459 * If the current level is eliminated, we explicitly plug in the value
1460 * for the current level found by isl_ast_build_set_loop_bounds in the
1461 * inverse schedule. This ensures that if we are working on a slice
1462 * of the domain based on information available in the inverse schedule
1463 * and the build domain, that then this information is also reflected
1464 * in the inverse schedule. This operation also eliminates the current
1465 * dimension from the inverse schedule making sure no inner dimensions depend
1466 * on the current dimension. Otherwise, we create a for node, marking
1467 * it degenerate if appropriate. The initial for node is still incomplete
1468 * and will be completed in either refine_degenerate or refine_generic.
1470 * We then generate a sequence of grafts for the next level,
1471 * create a surrounding graft for the current level and insert
1472 * the for node we created (if the current level is not eliminated).
1473 * Before creating a graft for the current level, we first extract
1474 * hoistable constraints from the child guards and combine them
1475 * with the pending constraints in the build. These constraints
1476 * are used to simplify the child guards and then added to the guard
1477 * of the current graft to ensure that they will be generated.
1478 * If the hoisted guard is a disjunction, then we use it directly
1479 * to gist the guards on the children before intersect it with the
1480 * pending constraints. We do so because this disjunction is typically
1481 * identical to the guards on the children such that these guards
1482 * can be effectively removed completely. After the intersection,
1483 * the gist operation would have a harder time figuring this out.
1485 * Finally, we set the bounds of the for loop in either
1486 * refine_degenerate or refine_generic.
1487 * We do so in a context where the pending constraints of the build
1488 * have been replaced by the guard of the current graft.
1490 static __isl_give isl_ast_graft *create_node_scaled(
1491 __isl_take isl_union_map *executed,
1492 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1493 __isl_take isl_ast_build *build)
1495 int depth;
1496 int degenerate;
1497 isl_bool eliminated;
1498 isl_size n;
1499 isl_basic_set *hull;
1500 isl_basic_set *enforced;
1501 isl_set *guard, *hoisted;
1502 isl_ast_node *node = NULL;
1503 isl_ast_graft *graft;
1504 isl_ast_graft_list *children;
1505 isl_ast_build *sub_build;
1506 isl_ast_build *body_build;
1508 domain = isl_ast_build_eliminate_divs(build, domain);
1509 domain = isl_set_detect_equalities(domain);
1510 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1511 bounds = isl_basic_set_intersect(bounds, hull);
1512 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1514 depth = isl_ast_build_get_depth(build);
1515 sub_build = isl_ast_build_copy(build);
1516 bounds = isl_basic_set_remove_redundancies(bounds);
1517 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1518 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1519 isl_basic_set_copy(bounds));
1520 degenerate = isl_ast_build_has_value(sub_build);
1521 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1522 if (degenerate < 0 || eliminated < 0)
1523 executed = isl_union_map_free(executed);
1524 if (!degenerate)
1525 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1526 sub_build = isl_ast_build_set_pending_generated(sub_build,
1527 isl_basic_set_copy(bounds));
1528 if (eliminated)
1529 executed = plug_in_values(executed, sub_build);
1530 else
1531 node = create_for(build, degenerate);
1533 body_build = isl_ast_build_copy(sub_build);
1534 body_build = isl_ast_build_increase_depth(body_build);
1535 if (!eliminated)
1536 node = before_each_for(node, body_build);
1537 children = generate_next_level(executed,
1538 isl_ast_build_copy(body_build));
1540 enforced = extract_shared_enforced(children, build);
1541 guard = extract_pending(sub_build, enforced);
1542 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1543 n = isl_set_n_basic_set(hoisted);
1544 if (n < 0)
1545 children = isl_ast_graft_list_free(children);
1546 if (n > 1)
1547 children = isl_ast_graft_list_gist_guards(children,
1548 isl_set_copy(hoisted));
1549 guard = isl_set_intersect(guard, hoisted);
1550 if (!eliminated)
1551 guard = add_implied_guards(guard, degenerate, bounds, build);
1553 graft = isl_ast_graft_alloc_from_children(children,
1554 isl_set_copy(guard), enforced, build, sub_build);
1556 if (!eliminated) {
1557 isl_ast_build *for_build;
1559 graft = isl_ast_graft_insert_for(graft, node);
1560 for_build = isl_ast_build_copy(build);
1561 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1562 isl_set_copy(guard));
1563 if (degenerate)
1564 graft = refine_degenerate(graft, for_build, sub_build);
1565 else
1566 graft = refine_generic(graft, bounds,
1567 domain, for_build);
1568 isl_ast_build_free(for_build);
1570 isl_set_free(guard);
1571 if (!eliminated)
1572 graft = after_each_for(graft, body_build);
1574 isl_ast_build_free(body_build);
1575 isl_ast_build_free(sub_build);
1576 isl_ast_build_free(build);
1577 isl_basic_set_free(bounds);
1578 isl_set_free(domain);
1580 return graft;
1583 /* Internal data structure for checking if all constraints involving
1584 * the input dimension "depth" are such that the other coefficients
1585 * are multiples of "m", reducing "m" if they are not.
1586 * If "m" is reduced all the way down to "1", then the check has failed
1587 * and we break out of the iteration.
1589 struct isl_check_scaled_data {
1590 int depth;
1591 isl_val *m;
1594 /* If constraint "c" involves the input dimension data->depth,
1595 * then make sure that all the other coefficients are multiples of data->m,
1596 * reducing data->m if needed.
1597 * Break out of the iteration if data->m has become equal to "1".
1599 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1600 void *user)
1602 struct isl_check_scaled_data *data = user;
1603 int i, j;
1604 isl_size n;
1605 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1606 isl_dim_div };
1608 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1609 isl_constraint_free(c);
1610 return isl_stat_ok;
1613 for (i = 0; i < 4; ++i) {
1614 n = isl_constraint_dim(c, t[i]);
1615 if (n < 0)
1616 break;
1617 for (j = 0; j < n; ++j) {
1618 isl_val *d;
1620 if (t[i] == isl_dim_in && j == data->depth)
1621 continue;
1622 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1623 continue;
1624 d = isl_constraint_get_coefficient_val(c, t[i], j);
1625 data->m = isl_val_gcd(data->m, d);
1626 if (isl_val_is_one(data->m))
1627 break;
1629 if (j < n)
1630 break;
1633 isl_constraint_free(c);
1635 return i < 4 ? isl_stat_error : isl_stat_ok;
1638 /* For each constraint of "bmap" that involves the input dimension data->depth,
1639 * make sure that all the other coefficients are multiples of data->m,
1640 * reducing data->m if needed.
1641 * Break out of the iteration if data->m has become equal to "1".
1643 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1644 void *user)
1646 isl_stat r;
1648 r = isl_basic_map_foreach_constraint(bmap,
1649 &constraint_check_scaled, user);
1650 isl_basic_map_free(bmap);
1652 return r;
1655 /* For each constraint of "map" that involves the input dimension data->depth,
1656 * make sure that all the other coefficients are multiples of data->m,
1657 * reducing data->m if needed.
1658 * Break out of the iteration if data->m has become equal to "1".
1660 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1662 isl_stat r;
1664 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1665 isl_map_free(map);
1667 return r;
1670 /* Create an AST node for the current dimension based on
1671 * the schedule domain "bounds" and return the node encapsulated
1672 * in an isl_ast_graft.
1674 * "executed" is the current inverse schedule, taking into account
1675 * the bounds in "bounds"
1676 * "domain" is the domain of "executed", with inner dimensions projected out.
1679 * Before moving on to the actual AST node construction in create_node_scaled,
1680 * we first check if the current dimension is strided and if we can scale
1681 * down this stride. Note that we only do this if the ast_build_scale_strides
1682 * option is set.
1684 * In particular, let the current dimension take on values
1686 * f + s a
1688 * with a an integer. We check if we can find an integer m that (obviously)
1689 * divides both f and s.
1691 * If so, we check if the current dimension only appears in constraints
1692 * where the coefficients of the other variables are multiples of m.
1693 * We perform this extra check to avoid the risk of introducing
1694 * divisions by scaling down the current dimension.
1696 * If so, we scale the current dimension down by a factor of m.
1697 * That is, we plug in
1699 * i = m i' (1)
1701 * Note that in principle we could always scale down strided loops
1702 * by plugging in
1704 * i = f + s i'
1706 * but this may result in i' taking on larger values than the original i,
1707 * due to the shift by "f".
1708 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1710 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1711 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1712 __isl_take isl_ast_build *build)
1714 struct isl_check_scaled_data data;
1715 isl_ctx *ctx;
1716 isl_aff *offset;
1717 isl_val *d;
1719 ctx = isl_ast_build_get_ctx(build);
1720 if (!isl_options_get_ast_build_scale_strides(ctx))
1721 return create_node_scaled(executed, bounds, domain, build);
1723 data.depth = isl_ast_build_get_depth(build);
1724 if (!isl_ast_build_has_stride(build, data.depth))
1725 return create_node_scaled(executed, bounds, domain, build);
1727 offset = isl_ast_build_get_offset(build, data.depth);
1728 data.m = isl_ast_build_get_stride(build, data.depth);
1729 if (!data.m)
1730 offset = isl_aff_free(offset);
1731 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1732 d = isl_aff_get_denominator_val(offset);
1733 if (!d)
1734 executed = isl_union_map_free(executed);
1736 if (executed && isl_val_is_divisible_by(data.m, d))
1737 data.m = isl_val_div(data.m, d);
1738 else {
1739 data.m = isl_val_set_si(data.m, 1);
1740 isl_val_free(d);
1743 if (!isl_val_is_one(data.m)) {
1744 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1745 &data) < 0 &&
1746 !isl_val_is_one(data.m))
1747 executed = isl_union_map_free(executed);
1750 if (!isl_val_is_one(data.m)) {
1751 isl_space *space;
1752 isl_multi_aff *ma;
1753 isl_aff *aff;
1754 isl_map *map;
1755 isl_union_map *umap;
1757 space = isl_ast_build_get_space(build, 1);
1758 space = isl_space_map_from_set(space);
1759 ma = isl_multi_aff_identity(space);
1760 aff = isl_multi_aff_get_aff(ma, data.depth);
1761 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1762 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1764 bounds = isl_basic_set_preimage_multi_aff(bounds,
1765 isl_multi_aff_copy(ma));
1766 domain = isl_set_preimage_multi_aff(domain,
1767 isl_multi_aff_copy(ma));
1768 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1769 umap = isl_union_map_from_map(map);
1770 executed = isl_union_map_apply_domain(executed,
1771 isl_union_map_copy(umap));
1772 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1773 umap);
1775 isl_aff_free(offset);
1776 isl_val_free(data.m);
1778 return create_node_scaled(executed, bounds, domain, build);
1781 /* Add the basic set to the list that "user" points to.
1783 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1785 isl_basic_set_list **list = user;
1787 *list = isl_basic_set_list_add(*list, bset);
1789 return isl_stat_ok;
1792 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1794 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1795 __isl_take isl_set *set)
1797 isl_size n;
1798 isl_ctx *ctx;
1799 isl_basic_set_list *list;
1801 n = isl_set_n_basic_set(set);
1802 if (n < 0)
1803 set = isl_set_free(set);
1804 if (!set)
1805 return NULL;
1807 ctx = isl_set_get_ctx(set);
1809 list = isl_basic_set_list_alloc(ctx, n);
1810 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1811 list = isl_basic_set_list_free(list);
1813 isl_set_free(set);
1814 return list;
1817 /* Generate code for the schedule domain "bounds"
1818 * and add the result to "list".
1820 * We mainly detect strides here and check if the bounds do not
1821 * conflict with the current build domain
1822 * and then pass over control to create_node.
1824 * "bounds" reflects the bounds on the current dimension and possibly
1825 * some extra conditions on outer dimensions.
1826 * It does not, however, include any divs involving the current dimension,
1827 * so it does not capture any stride constraints.
1828 * We therefore need to compute that part of the schedule domain that
1829 * intersects with "bounds" and derive the strides from the result.
1831 static __isl_give isl_ast_graft_list *add_node(
1832 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1833 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1835 isl_ast_graft *graft;
1836 isl_set *domain = NULL;
1837 isl_union_set *uset;
1838 int empty, disjoint;
1840 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1841 executed = isl_union_map_intersect_domain(executed, uset);
1842 empty = isl_union_map_is_empty(executed);
1843 if (empty < 0)
1844 goto error;
1845 if (empty)
1846 goto done;
1848 uset = isl_union_map_domain(isl_union_map_copy(executed));
1849 domain = isl_set_from_union_set(uset);
1850 domain = isl_ast_build_specialize(build, domain);
1852 domain = isl_set_compute_divs(domain);
1853 domain = isl_ast_build_eliminate_inner(build, domain);
1854 disjoint = isl_set_is_disjoint(domain, build->domain);
1855 if (disjoint < 0)
1856 goto error;
1857 if (disjoint)
1858 goto done;
1860 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1862 graft = create_node(executed, bounds, domain,
1863 isl_ast_build_copy(build));
1864 list = isl_ast_graft_list_add(list, graft);
1865 isl_ast_build_free(build);
1866 return list;
1867 error:
1868 list = isl_ast_graft_list_free(list);
1869 done:
1870 isl_set_free(domain);
1871 isl_basic_set_free(bounds);
1872 isl_union_map_free(executed);
1873 isl_ast_build_free(build);
1874 return list;
1877 /* Does any element of i follow or coincide with any element of j
1878 * at the current depth for equal values of the outer dimensions?
1880 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1881 __isl_keep isl_basic_set *j, void *user)
1883 int depth = *(int *) user;
1884 isl_basic_map *test;
1885 isl_bool empty;
1886 int l;
1888 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1889 isl_basic_set_copy(j));
1890 for (l = 0; l < depth; ++l)
1891 test = isl_basic_map_equate(test, isl_dim_in, l,
1892 isl_dim_out, l);
1893 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1894 isl_dim_out, depth);
1895 empty = isl_basic_map_is_empty(test);
1896 isl_basic_map_free(test);
1898 return isl_bool_not(empty);
1901 /* Split up each element of "list" into a part that is related to "bset"
1902 * according to "gt" and a part that is not.
1903 * Return a list that consist of "bset" and all the pieces.
1905 static __isl_give isl_basic_set_list *add_split_on(
1906 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1907 __isl_keep isl_basic_map *gt)
1909 int i;
1910 isl_size n;
1911 isl_basic_set_list *res;
1913 n = isl_basic_set_list_n_basic_set(list);
1914 if (n < 0)
1915 bset = isl_basic_set_free(bset);
1917 gt = isl_basic_map_copy(gt);
1918 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1919 res = isl_basic_set_list_from_basic_set(bset);
1920 for (i = 0; res && i < n; ++i) {
1921 isl_basic_set *bset;
1922 isl_set *set1, *set2;
1923 isl_basic_map *bmap;
1924 int empty;
1926 bset = isl_basic_set_list_get_basic_set(list, i);
1927 bmap = isl_basic_map_copy(gt);
1928 bmap = isl_basic_map_intersect_range(bmap, bset);
1929 bset = isl_basic_map_range(bmap);
1930 empty = isl_basic_set_is_empty(bset);
1931 if (empty < 0)
1932 res = isl_basic_set_list_free(res);
1933 if (empty) {
1934 isl_basic_set_free(bset);
1935 bset = isl_basic_set_list_get_basic_set(list, i);
1936 res = isl_basic_set_list_add(res, bset);
1937 continue;
1940 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1941 set1 = isl_set_from_basic_set(bset);
1942 bset = isl_basic_set_list_get_basic_set(list, i);
1943 set2 = isl_set_from_basic_set(bset);
1944 set1 = isl_set_subtract(set2, set1);
1945 set1 = isl_set_make_disjoint(set1);
1947 res = isl_basic_set_list_concat(res,
1948 isl_basic_set_list_from_set(set1));
1950 isl_basic_map_free(gt);
1951 isl_basic_set_list_free(list);
1952 return res;
1955 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1956 __isl_keep isl_basic_set_list *domain_list,
1957 __isl_keep isl_union_map *executed,
1958 __isl_keep isl_ast_build *build);
1960 /* Internal data structure for add_nodes.
1962 * "executed" and "build" are extra arguments to be passed to add_node.
1963 * "list" collects the results.
1965 struct isl_add_nodes_data {
1966 isl_union_map *executed;
1967 isl_ast_build *build;
1969 isl_ast_graft_list *list;
1972 /* Generate code for the schedule domains in "scc"
1973 * and add the results to "list".
1975 * The domains in "scc" form a strongly connected component in the ordering.
1976 * If the number of domains in "scc" is larger than 1, then this means
1977 * that we cannot determine a valid ordering for the domains in the component.
1978 * This should be fairly rare because the individual domains
1979 * have been made disjoint first.
1980 * The problem is that the domains may be integrally disjoint but not
1981 * rationally disjoint. For example, we may have domains
1983 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1985 * These two domains have an empty intersection, but their rational
1986 * relaxations do intersect. It is impossible to order these domains
1987 * in the second dimension because the first should be ordered before
1988 * the second for outer dimension equal to 0, while it should be ordered
1989 * after for outer dimension equal to 1.
1991 * This may happen in particular in case of unrolling since the domain
1992 * of each slice is replaced by its simple hull.
1994 * For each basic set i in "scc" and for each of the following basic sets j,
1995 * we split off that part of the basic set i that shares the outer dimensions
1996 * with j and lies before j in the current dimension.
1997 * We collect all the pieces in a new list that replaces "scc".
1999 * While the elements in "scc" should be disjoint, we double-check
2000 * this property to avoid running into an infinite recursion in case
2001 * they intersect due to some internal error.
2003 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
2005 struct isl_add_nodes_data *data = user;
2006 int i, depth;
2007 isl_size n;
2008 isl_basic_set *bset, *first;
2009 isl_basic_set_list *list;
2010 isl_space *space;
2011 isl_basic_map *gt;
2013 n = isl_basic_set_list_n_basic_set(scc);
2014 if (n < 0)
2015 goto error;
2016 bset = isl_basic_set_list_get_basic_set(scc, 0);
2017 if (n == 1) {
2018 isl_basic_set_list_free(scc);
2019 data->list = add_node(data->list,
2020 isl_union_map_copy(data->executed), bset,
2021 isl_ast_build_copy(data->build));
2022 return data->list ? isl_stat_ok : isl_stat_error;
2025 depth = isl_ast_build_get_depth(data->build);
2026 space = isl_basic_set_get_space(bset);
2027 space = isl_space_map_from_set(space);
2028 gt = isl_basic_map_universe(space);
2029 for (i = 0; i < depth; ++i)
2030 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
2031 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
2033 first = isl_basic_set_copy(bset);
2034 list = isl_basic_set_list_from_basic_set(bset);
2035 for (i = 1; i < n; ++i) {
2036 int disjoint;
2038 bset = isl_basic_set_list_get_basic_set(scc, i);
2040 disjoint = isl_basic_set_is_disjoint(bset, first);
2041 if (disjoint < 0)
2042 list = isl_basic_set_list_free(list);
2043 else if (!disjoint)
2044 isl_die(isl_basic_set_list_get_ctx(scc),
2045 isl_error_internal,
2046 "basic sets in scc are assumed to be disjoint",
2047 list = isl_basic_set_list_free(list));
2049 list = add_split_on(list, bset, gt);
2051 isl_basic_set_free(first);
2052 isl_basic_map_free(gt);
2053 isl_basic_set_list_free(scc);
2054 scc = list;
2055 data->list = isl_ast_graft_list_concat(data->list,
2056 generate_sorted_domains(scc, data->executed, data->build));
2057 isl_basic_set_list_free(scc);
2059 return data->list ? isl_stat_ok : isl_stat_error;
2060 error:
2061 isl_basic_set_list_free(scc);
2062 return isl_stat_error;
2065 /* Sort the domains in "domain_list" according to the execution order
2066 * at the current depth (for equal values of the outer dimensions),
2067 * generate code for each of them, collecting the results in a list.
2068 * If no code is generated (because the intersection of the inverse schedule
2069 * with the domains turns out to be empty), then an empty list is returned.
2071 * The caller is responsible for ensuring that the basic sets in "domain_list"
2072 * are pair-wise disjoint. It can, however, in principle happen that
2073 * two basic sets should be ordered one way for one value of the outer
2074 * dimensions and the other way for some other value of the outer dimensions.
2075 * We therefore play safe and look for strongly connected components.
2076 * The function add_nodes takes care of handling non-trivial components.
2078 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2079 __isl_keep isl_basic_set_list *domain_list,
2080 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2082 isl_ctx *ctx;
2083 struct isl_add_nodes_data data;
2084 int depth;
2085 isl_size n;
2087 n = isl_basic_set_list_n_basic_set(domain_list);
2088 if (n < 0)
2089 return NULL;
2091 ctx = isl_basic_set_list_get_ctx(domain_list);
2092 data.list = isl_ast_graft_list_alloc(ctx, n);
2093 if (n == 0)
2094 return data.list;
2095 if (n == 1)
2096 return add_node(data.list, isl_union_map_copy(executed),
2097 isl_basic_set_list_get_basic_set(domain_list, 0),
2098 isl_ast_build_copy(build));
2100 depth = isl_ast_build_get_depth(build);
2101 data.executed = executed;
2102 data.build = build;
2103 if (isl_basic_set_list_foreach_scc(domain_list,
2104 &domain_follows_at_depth, &depth,
2105 &add_nodes, &data) < 0)
2106 data.list = isl_ast_graft_list_free(data.list);
2108 return data.list;
2111 /* Do i and j share any values for the outer dimensions?
2113 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2114 __isl_keep isl_basic_set *j, void *user)
2116 int depth = *(int *) user;
2117 isl_basic_map *test;
2118 isl_bool empty;
2119 int l;
2121 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2122 isl_basic_set_copy(j));
2123 for (l = 0; l < depth; ++l)
2124 test = isl_basic_map_equate(test, isl_dim_in, l,
2125 isl_dim_out, l);
2126 empty = isl_basic_map_is_empty(test);
2127 isl_basic_map_free(test);
2129 return isl_bool_not(empty);
2132 /* Internal data structure for generate_sorted_domains_wrap.
2134 * "n" is the total number of basic sets
2135 * "executed" and "build" are extra arguments to be passed
2136 * to generate_sorted_domains.
2138 * "single" is set to 1 by generate_sorted_domains_wrap if there
2139 * is only a single component.
2140 * "list" collects the results.
2142 struct isl_ast_generate_parallel_domains_data {
2143 isl_size n;
2144 isl_union_map *executed;
2145 isl_ast_build *build;
2147 int single;
2148 isl_ast_graft_list *list;
2151 /* Call generate_sorted_domains on "scc", fuse the result into a list
2152 * with either zero or one graft and collect the these single element
2153 * lists into data->list.
2155 * If there is only one component, i.e., if the number of basic sets
2156 * in the current component is equal to the total number of basic sets,
2157 * then data->single is set to 1 and the result of generate_sorted_domains
2158 * is not fused.
2160 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2161 void *user)
2163 struct isl_ast_generate_parallel_domains_data *data = user;
2164 isl_ast_graft_list *list;
2165 isl_size n;
2167 n = isl_basic_set_list_n_basic_set(scc);
2168 if (n < 0)
2169 scc = isl_basic_set_list_free(scc);
2170 list = generate_sorted_domains(scc, data->executed, data->build);
2171 data->single = n == data->n;
2172 if (!data->single)
2173 list = isl_ast_graft_list_fuse(list, data->build);
2174 if (!data->list)
2175 data->list = list;
2176 else
2177 data->list = isl_ast_graft_list_concat(data->list, list);
2179 isl_basic_set_list_free(scc);
2180 if (!data->list)
2181 return isl_stat_error;
2183 return isl_stat_ok;
2186 /* Look for any (weakly connected) components in the "domain_list"
2187 * of domains that share some values of the outer dimensions.
2188 * That is, domains in different components do not share any values
2189 * of the outer dimensions. This means that these components
2190 * can be freely reordered.
2191 * Within each of the components, we sort the domains according
2192 * to the execution order at the current depth.
2194 * If there is more than one component, then generate_sorted_domains_wrap
2195 * fuses the result of each call to generate_sorted_domains
2196 * into a list with either zero or one graft and collects these (at most)
2197 * single element lists into a bigger list. This means that the elements of the
2198 * final list can be freely reordered. In particular, we sort them
2199 * according to an arbitrary but fixed ordering to ease merging of
2200 * graft lists from different components.
2202 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2203 __isl_keep isl_basic_set_list *domain_list,
2204 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2206 int depth;
2207 struct isl_ast_generate_parallel_domains_data data;
2209 data.n = isl_basic_set_list_n_basic_set(domain_list);
2210 if (data.n < 0)
2211 return NULL;
2213 if (data.n <= 1)
2214 return generate_sorted_domains(domain_list, executed, build);
2216 depth = isl_ast_build_get_depth(build);
2217 data.list = NULL;
2218 data.executed = executed;
2219 data.build = build;
2220 data.single = 0;
2221 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2222 &generate_sorted_domains_wrap,
2223 &data) < 0)
2224 data.list = isl_ast_graft_list_free(data.list);
2226 if (!data.single)
2227 data.list = isl_ast_graft_list_sort_guard(data.list);
2229 return data.list;
2232 /* Internal data for separate_domain.
2234 * "explicit" is set if we only want to use explicit bounds.
2236 * "domain" collects the separated domains.
2238 struct isl_separate_domain_data {
2239 isl_ast_build *build;
2240 int explicit;
2241 isl_set *domain;
2244 /* Extract implicit bounds on the current dimension for the executed "map".
2246 * The domain of "map" may involve inner dimensions, so we
2247 * need to eliminate them.
2249 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2250 __isl_keep isl_ast_build *build)
2252 isl_set *domain;
2254 domain = isl_map_domain(map);
2255 domain = isl_ast_build_eliminate(build, domain);
2257 return domain;
2260 /* Extract explicit bounds on the current dimension for the executed "map".
2262 * Rather than eliminating the inner dimensions as in implicit_bounds,
2263 * we simply drop any constraints involving those inner dimensions.
2264 * The idea is that most bounds that are implied by constraints on the
2265 * inner dimensions will be enforced by for loops and not by explicit guards.
2266 * There is then no need to separate along those bounds.
2268 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2269 __isl_keep isl_ast_build *build)
2271 isl_set *domain;
2272 int depth;
2273 isl_size dim;
2275 dim = isl_map_dim(map, isl_dim_out);
2276 if (dim < 0)
2277 return isl_map_domain(isl_map_free(map));
2278 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2280 domain = isl_map_domain(map);
2281 depth = isl_ast_build_get_depth(build);
2282 dim = isl_set_dim(domain, isl_dim_set);
2283 domain = isl_set_detect_equalities(domain);
2284 domain = isl_set_drop_constraints_involving_dims(domain,
2285 isl_dim_set, depth + 1, dim - (depth + 1));
2286 domain = isl_set_remove_divs_involving_dims(domain,
2287 isl_dim_set, depth, 1);
2288 domain = isl_set_remove_unknown_divs(domain);
2290 return domain;
2293 /* Split data->domain into pieces that intersect with the range of "map"
2294 * and pieces that do not intersect with the range of "map"
2295 * and then add that part of the range of "map" that does not intersect
2296 * with data->domain.
2298 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2300 struct isl_separate_domain_data *data = user;
2301 isl_set *domain;
2302 isl_set *d1, *d2;
2304 if (data->explicit)
2305 domain = explicit_bounds(map, data->build);
2306 else
2307 domain = implicit_bounds(map, data->build);
2309 domain = isl_set_coalesce(domain);
2310 domain = isl_set_make_disjoint(domain);
2311 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2312 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2313 data->domain = isl_set_intersect(data->domain, domain);
2314 data->domain = isl_set_union(data->domain, d1);
2315 data->domain = isl_set_union(data->domain, d2);
2317 return isl_stat_ok;
2320 /* Separate the schedule domains of "executed".
2322 * That is, break up the domain of "executed" into basic sets,
2323 * such that for each basic set S, every element in S is associated with
2324 * the same domain spaces.
2326 * "space" is the (single) domain space of "executed".
2328 static __isl_give isl_set *separate_schedule_domains(
2329 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2330 __isl_keep isl_ast_build *build)
2332 struct isl_separate_domain_data data = { build };
2333 isl_ctx *ctx;
2335 ctx = isl_ast_build_get_ctx(build);
2336 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2337 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2338 data.domain = isl_set_empty(space);
2339 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2340 data.domain = isl_set_free(data.domain);
2342 isl_union_map_free(executed);
2343 return data.domain;
2346 /* Temporary data used during the search for a lower bound for unrolling.
2348 * "build" is the build in which the unrolling will be performed
2349 * "domain" is the original set for which to find a lower bound
2350 * "depth" is the dimension for which to find a lower boudn
2351 * "expansion" is the expansion that needs to be applied to "domain"
2352 * in the unrolling that will be performed
2354 * "lower" is the best lower bound found so far. It is NULL if we have not
2355 * found any yet.
2356 * "n" is the corresponding size. If lower is NULL, then the value of n
2357 * is undefined.
2358 * "n_div" is the maximal number of integer divisions in the first
2359 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2360 * been computed yet.
2362 struct isl_find_unroll_data {
2363 isl_ast_build *build;
2364 isl_set *domain;
2365 int depth;
2366 isl_basic_map *expansion;
2368 isl_aff *lower;
2369 int *n;
2370 int n_div;
2373 /* Return the constraint
2375 * i_"depth" = aff + offset
2377 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2378 int offset)
2380 aff = isl_aff_copy(aff);
2381 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2382 aff = isl_aff_add_constant_si(aff, offset);
2383 return isl_equality_from_aff(aff);
2386 /* Update *user to the number of integer divisions in the first element
2387 * of "ma", if it is larger than the current value.
2389 static isl_stat update_n_div(__isl_take isl_set *set,
2390 __isl_take isl_multi_aff *ma, void *user)
2392 isl_aff *aff;
2393 int *n = user;
2394 isl_size n_div;
2396 aff = isl_multi_aff_get_aff(ma, 0);
2397 n_div = isl_aff_dim(aff, isl_dim_div);
2398 isl_aff_free(aff);
2399 isl_multi_aff_free(ma);
2400 isl_set_free(set);
2402 if (n_div > *n)
2403 *n = n_div;
2405 return n_div >= 0 ? isl_stat_ok : isl_stat_error;
2408 /* Get the number of integer divisions in the expression for the iterator
2409 * value at the first slice in the unrolling based on lower bound "lower",
2410 * taking into account the expansion that needs to be performed on this slice.
2412 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2413 __isl_keep isl_aff *lower)
2415 isl_constraint *c;
2416 isl_set *set;
2417 isl_map *it_map, *expansion;
2418 isl_pw_multi_aff *pma;
2419 int n;
2421 c = at_offset(data->depth, lower, 0);
2422 set = isl_set_copy(data->domain);
2423 set = isl_set_add_constraint(set, c);
2424 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2425 set = isl_set_apply(set, expansion);
2426 it_map = isl_ast_build_map_to_iterator(data->build, set);
2427 pma = isl_pw_multi_aff_from_map(it_map);
2428 n = 0;
2429 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2430 n = -1;
2431 isl_pw_multi_aff_free(pma);
2433 return n;
2436 /* Is the lower bound "lower" with corresponding iteration count "n"
2437 * better than the one stored in "data"?
2438 * If there is no upper bound on the iteration count ("n" is infinity) or
2439 * if the count is too large, then we cannot use this lower bound.
2440 * Otherwise, if there was no previous lower bound or
2441 * if the iteration count of the new lower bound is smaller than
2442 * the iteration count of the previous lower bound, then we consider
2443 * the new lower bound to be better.
2444 * If the iteration count is the same, then compare the number
2445 * of integer divisions that would be needed to express
2446 * the iterator value at the first slice in the unrolling
2447 * according to the lower bound. If we end up computing this
2448 * number, then store the lowest value in data->n_div.
2450 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2451 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2453 int cmp;
2454 int n_div;
2456 if (!n)
2457 return -1;
2458 if (isl_val_is_infty(n))
2459 return 0;
2460 if (isl_val_cmp_si(n, INT_MAX) > 0)
2461 return 0;
2462 if (!data->lower)
2463 return 1;
2464 cmp = isl_val_cmp_si(n, *data->n);
2465 if (cmp < 0)
2466 return 1;
2467 if (cmp > 0)
2468 return 0;
2469 if (data->n_div < 0)
2470 data->n_div = get_expanded_n_div(data, data->lower);
2471 if (data->n_div < 0)
2472 return -1;
2473 if (data->n_div == 0)
2474 return 0;
2475 n_div = get_expanded_n_div(data, lower);
2476 if (n_div < 0)
2477 return -1;
2478 if (n_div >= data->n_div)
2479 return 0;
2480 data->n_div = n_div;
2482 return 1;
2485 /* Check if we can use "c" as a lower bound and if it is better than
2486 * any previously found lower bound.
2488 * If "c" does not involve the dimension at the current depth,
2489 * then we cannot use it.
2490 * Otherwise, let "c" be of the form
2492 * i >= f(j)/a
2494 * We compute the maximal value of
2496 * -ceil(f(j)/a)) + i + 1
2498 * over the domain. If there is such a value "n", then we know
2500 * -ceil(f(j)/a)) + i + 1 <= n
2502 * or
2504 * i < ceil(f(j)/a)) + n
2506 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2507 * We just need to check if we have found any lower bound before and
2508 * if the new lower bound is better (smaller n or fewer integer divisions)
2509 * than the previously found lower bounds.
2511 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2512 __isl_keep isl_constraint *c)
2514 isl_aff *aff, *lower;
2515 isl_val *max;
2516 int better;
2518 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2519 return isl_stat_ok;
2521 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2522 lower = isl_aff_ceil(lower);
2523 aff = isl_aff_copy(lower);
2524 aff = isl_aff_neg(aff);
2525 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2526 aff = isl_aff_add_constant_si(aff, 1);
2527 max = isl_set_max_val(data->domain, aff);
2528 isl_aff_free(aff);
2530 better = is_better_lower_bound(data, lower, max);
2531 if (better < 0 || !better) {
2532 isl_val_free(max);
2533 isl_aff_free(lower);
2534 return better < 0 ? isl_stat_error : isl_stat_ok;
2537 isl_aff_free(data->lower);
2538 data->lower = lower;
2539 *data->n = isl_val_get_num_si(max);
2540 isl_val_free(max);
2542 return isl_stat_ok;
2545 /* Check if we can use "c" as a lower bound and if it is better than
2546 * any previously found lower bound.
2548 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2550 struct isl_find_unroll_data *data;
2551 isl_stat r;
2553 data = (struct isl_find_unroll_data *) user;
2554 r = update_unrolling_lower_bound(data, c);
2555 isl_constraint_free(c);
2557 return r;
2560 /* Look for a lower bound l(i) on the dimension at "depth"
2561 * and a size n such that "domain" is a subset of
2563 * { [i] : l(i) <= i_d < l(i) + n }
2565 * where d is "depth" and l(i) depends only on earlier dimensions.
2566 * Furthermore, try and find a lower bound such that n is as small as possible.
2567 * In particular, "n" needs to be finite.
2568 * "build" is the build in which the unrolling will be performed.
2569 * "expansion" is the expansion that needs to be applied to "domain"
2570 * in the unrolling that will be performed.
2572 * Inner dimensions have been eliminated from "domain" by the caller.
2574 * We first construct a collection of lower bounds on the input set
2575 * by computing its simple hull. We then iterate through them,
2576 * discarding those that we cannot use (either because they do not
2577 * involve the dimension at "depth" or because they have no corresponding
2578 * upper bound, meaning that "n" would be unbounded) and pick out the
2579 * best from the remaining ones.
2581 * If we cannot find a suitable lower bound, then we consider that
2582 * to be an error.
2584 static __isl_give isl_aff *find_unroll_lower_bound(
2585 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2586 int depth, __isl_keep isl_basic_map *expansion, int *n)
2588 struct isl_find_unroll_data data =
2589 { build, domain, depth, expansion, NULL, n, -1 };
2590 isl_basic_set *hull;
2592 hull = isl_set_simple_hull(isl_set_copy(domain));
2594 if (isl_basic_set_foreach_constraint(hull,
2595 &constraint_find_unroll, &data) < 0)
2596 goto error;
2598 isl_basic_set_free(hull);
2600 if (!data.lower)
2601 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2602 "cannot find lower bound for unrolling", return NULL);
2604 return data.lower;
2605 error:
2606 isl_basic_set_free(hull);
2607 return isl_aff_free(data.lower);
2610 /* Call "fn" on each iteration of the current dimension of "domain".
2611 * If "init" is not NULL, then it is called with the number of
2612 * iterations before any call to "fn".
2613 * Return -1 on failure.
2615 * Since we are going to be iterating over the individual values,
2616 * we first check if there are any strides on the current dimension.
2617 * If there is, we rewrite the current dimension i as
2619 * i = stride i' + offset
2621 * and then iterate over individual values of i' instead.
2623 * We then look for a lower bound on i' and a size such that the domain
2624 * is a subset of
2626 * { [j,i'] : l(j) <= i' < l(j) + n }
2628 * and then take slices of the domain at values of i'
2629 * between l(j) and l(j) + n - 1.
2631 * We compute the unshifted simple hull of each slice to ensure that
2632 * we have a single basic set per offset. The slicing constraint
2633 * may get simplified away before the unshifted simple hull is taken
2634 * and may therefore in some rare cases disappear from the result.
2635 * We therefore explicitly add the constraint back after computing
2636 * the unshifted simple hull to ensure that the basic sets
2637 * remain disjoint. The constraints that are dropped by taking the hull
2638 * will be taken into account at the next level, as in the case of the
2639 * atomic option.
2641 * Finally, we map i' back to i and call "fn".
2643 static int foreach_iteration(__isl_take isl_set *domain,
2644 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2645 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2647 int i, n;
2648 int empty;
2649 int depth;
2650 isl_multi_aff *expansion;
2651 isl_basic_map *bmap;
2652 isl_aff *lower = NULL;
2653 isl_ast_build *stride_build;
2655 depth = isl_ast_build_get_depth(build);
2657 domain = isl_ast_build_eliminate_inner(build, domain);
2658 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2659 stride_build = isl_ast_build_copy(build);
2660 stride_build = isl_ast_build_detect_strides(stride_build,
2661 isl_set_copy(domain));
2662 expansion = isl_ast_build_get_stride_expansion(stride_build);
2664 domain = isl_set_preimage_multi_aff(domain,
2665 isl_multi_aff_copy(expansion));
2666 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2667 isl_ast_build_free(stride_build);
2669 bmap = isl_basic_map_from_multi_aff(expansion);
2671 empty = isl_set_is_empty(domain);
2672 if (empty < 0) {
2673 n = -1;
2674 } else if (empty) {
2675 n = 0;
2676 } else {
2677 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2678 if (!lower)
2679 n = -1;
2681 if (n >= 0 && init && init(n, user) < 0)
2682 n = -1;
2683 for (i = 0; i < n; ++i) {
2684 isl_set *set;
2685 isl_basic_set *bset;
2686 isl_constraint *slice;
2688 slice = at_offset(depth, lower, i);
2689 set = isl_set_copy(domain);
2690 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2691 bset = isl_set_unshifted_simple_hull(set);
2692 bset = isl_basic_set_add_constraint(bset, slice);
2693 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2695 if (fn(bset, user) < 0)
2696 break;
2699 isl_aff_free(lower);
2700 isl_set_free(domain);
2701 isl_basic_map_free(bmap);
2703 return n < 0 || i < n ? -1 : 0;
2706 /* Data structure for storing the results and the intermediate objects
2707 * of compute_domains.
2709 * "list" is the main result of the function and contains a list
2710 * of disjoint basic sets for which code should be generated.
2712 * "executed" and "build" are inputs to compute_domains.
2713 * "schedule_domain" is the domain of "executed".
2715 * "option" contains the domains at the current depth that should by
2716 * atomic, separated or unrolled. These domains are as specified by
2717 * the user, except that inner dimensions have been eliminated and
2718 * that they have been made pair-wise disjoint.
2720 * "sep_class" contains the user-specified split into separation classes
2721 * specialized to the current depth.
2722 * "done" contains the union of the separation domains that have already
2723 * been handled.
2725 struct isl_codegen_domains {
2726 isl_basic_set_list *list;
2728 isl_union_map *executed;
2729 isl_ast_build *build;
2730 isl_set *schedule_domain;
2732 isl_set *option[4];
2734 isl_map *sep_class;
2735 isl_set *done;
2738 /* Internal data structure for do_unroll.
2740 * "domains" stores the results of compute_domains.
2741 * "class_domain" is the original class domain passed to do_unroll.
2742 * "unroll_domain" collects the unrolled iterations.
2744 struct isl_ast_unroll_data {
2745 struct isl_codegen_domains *domains;
2746 isl_set *class_domain;
2747 isl_set *unroll_domain;
2750 /* Given an iteration of an unrolled domain represented by "bset",
2751 * add it to data->domains->list.
2752 * Since we may have dropped some constraints, we intersect with
2753 * the class domain again to ensure that each element in the list
2754 * is disjoint from the other class domains.
2756 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2758 struct isl_ast_unroll_data *data = user;
2759 isl_set *set;
2760 isl_basic_set_list *list;
2762 set = isl_set_from_basic_set(bset);
2763 data->unroll_domain = isl_set_union(data->unroll_domain,
2764 isl_set_copy(set));
2765 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2766 set = isl_set_make_disjoint(set);
2767 list = isl_basic_set_list_from_set(set);
2768 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2769 list);
2771 return 0;
2774 /* Extend domains->list with a list of basic sets, one for each value
2775 * of the current dimension in "domain" and remove the corresponding
2776 * sets from the class domain. Return the updated class domain.
2777 * The divs that involve the current dimension have not been projected out
2778 * from this domain.
2780 * We call foreach_iteration to iterate over the individual values and
2781 * in do_unroll_iteration we collect the individual basic sets in
2782 * domains->list and their union in data->unroll_domain, which is then
2783 * used to update the class domain.
2785 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2786 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2788 struct isl_ast_unroll_data data;
2790 if (!domain)
2791 return isl_set_free(class_domain);
2792 if (!class_domain)
2793 return isl_set_free(domain);
2795 data.domains = domains;
2796 data.class_domain = class_domain;
2797 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2799 if (foreach_iteration(domain, domains->build, NULL,
2800 &do_unroll_iteration, &data) < 0)
2801 data.unroll_domain = isl_set_free(data.unroll_domain);
2803 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2805 return class_domain;
2808 /* Add domains to domains->list for each individual value of the current
2809 * dimension, for that part of the schedule domain that lies in the
2810 * intersection of the option domain and the class domain.
2811 * Remove the corresponding sets from the class domain and
2812 * return the updated class domain.
2814 * We first break up the unroll option domain into individual pieces
2815 * and then handle each of them separately. The unroll option domain
2816 * has been made disjoint in compute_domains_init_options,
2818 * Note that we actively want to combine different pieces of the
2819 * schedule domain that have the same value at the current dimension.
2820 * We therefore need to break up the unroll option domain before
2821 * intersecting with class and schedule domain, hoping that the
2822 * unroll option domain specified by the user is relatively simple.
2824 static __isl_give isl_set *compute_unroll_domains(
2825 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2827 isl_set *unroll_domain;
2828 isl_basic_set_list *unroll_list;
2829 int i;
2830 isl_size n;
2831 isl_bool empty;
2833 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2834 if (empty < 0)
2835 return isl_set_free(class_domain);
2836 if (empty)
2837 return class_domain;
2839 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2840 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2842 n = isl_basic_set_list_n_basic_set(unroll_list);
2843 if (n < 0)
2844 class_domain = isl_set_free(class_domain);
2845 for (i = 0; i < n; ++i) {
2846 isl_basic_set *bset;
2848 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2849 unroll_domain = isl_set_from_basic_set(bset);
2850 unroll_domain = isl_set_intersect(unroll_domain,
2851 isl_set_copy(class_domain));
2852 unroll_domain = isl_set_intersect(unroll_domain,
2853 isl_set_copy(domains->schedule_domain));
2855 empty = isl_set_is_empty(unroll_domain);
2856 if (empty >= 0 && empty) {
2857 isl_set_free(unroll_domain);
2858 continue;
2861 class_domain = do_unroll(domains, unroll_domain, class_domain);
2864 isl_basic_set_list_free(unroll_list);
2866 return class_domain;
2869 /* Try and construct a single basic set that includes the intersection of
2870 * the schedule domain, the atomic option domain and the class domain.
2871 * Add the resulting basic set(s) to domains->list and remove them
2872 * from class_domain. Return the updated class domain.
2874 * We construct a single domain rather than trying to combine
2875 * the schedule domains of individual domains because we are working
2876 * within a single component so that non-overlapping schedule domains
2877 * should already have been separated.
2878 * We do however need to make sure that this single domains is a subset
2879 * of the class domain so that it would not intersect with any other
2880 * class domains. This means that we may end up splitting up the atomic
2881 * domain in case separation classes are being used.
2883 * "domain" is the intersection of the schedule domain and the class domain,
2884 * with inner dimensions projected out.
2886 static __isl_give isl_set *compute_atomic_domain(
2887 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2889 isl_basic_set *bset;
2890 isl_basic_set_list *list;
2891 isl_set *domain, *atomic_domain;
2892 int empty;
2894 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2895 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2896 domain = isl_set_intersect(domain,
2897 isl_set_copy(domains->schedule_domain));
2898 empty = isl_set_is_empty(domain);
2899 if (empty < 0)
2900 class_domain = isl_set_free(class_domain);
2901 if (empty) {
2902 isl_set_free(domain);
2903 return class_domain;
2906 domain = isl_ast_build_eliminate(domains->build, domain);
2907 domain = isl_set_coalesce_preserve(domain);
2908 bset = isl_set_unshifted_simple_hull(domain);
2909 domain = isl_set_from_basic_set(bset);
2910 atomic_domain = isl_set_copy(domain);
2911 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2912 class_domain = isl_set_subtract(class_domain, atomic_domain);
2913 domain = isl_set_make_disjoint(domain);
2914 list = isl_basic_set_list_from_set(domain);
2915 domains->list = isl_basic_set_list_concat(domains->list, list);
2917 return class_domain;
2920 /* Split up the schedule domain into uniform basic sets,
2921 * in the sense that each element in a basic set is associated to
2922 * elements of the same domains, and add the result to domains->list.
2923 * Do this for that part of the schedule domain that lies in the
2924 * intersection of "class_domain" and the separate option domain.
2926 * "class_domain" may or may not include the constraints
2927 * of the schedule domain, but this does not make a difference
2928 * since we are going to intersect it with the domain of the inverse schedule.
2929 * If it includes schedule domain constraints, then they may involve
2930 * inner dimensions, but we will eliminate them in separation_domain.
2932 static int compute_separate_domain(struct isl_codegen_domains *domains,
2933 __isl_keep isl_set *class_domain)
2935 isl_space *space;
2936 isl_set *domain;
2937 isl_union_map *executed;
2938 isl_basic_set_list *list;
2939 int empty;
2941 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2942 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2943 executed = isl_union_map_copy(domains->executed);
2944 executed = isl_union_map_intersect_domain(executed,
2945 isl_union_set_from_set(domain));
2946 empty = isl_union_map_is_empty(executed);
2947 if (empty < 0 || empty) {
2948 isl_union_map_free(executed);
2949 return empty < 0 ? -1 : 0;
2952 space = isl_set_get_space(class_domain);
2953 domain = separate_schedule_domains(space, executed, domains->build);
2955 list = isl_basic_set_list_from_set(domain);
2956 domains->list = isl_basic_set_list_concat(domains->list, list);
2958 return 0;
2961 /* Split up the domain at the current depth into disjoint
2962 * basic sets for which code should be generated separately
2963 * for the given separation class domain.
2965 * If any separation classes have been defined, then "class_domain"
2966 * is the domain of the current class and does not refer to inner dimensions.
2967 * Otherwise, "class_domain" is the universe domain.
2969 * We first make sure that the class domain is disjoint from
2970 * previously considered class domains.
2972 * The separate domains can be computed directly from the "class_domain".
2974 * The unroll, atomic and remainder domains need the constraints
2975 * from the schedule domain.
2977 * For unrolling, the actual schedule domain is needed (with divs that
2978 * may refer to the current dimension) so that stride detection can be
2979 * performed.
2981 * For atomic and remainder domains, inner dimensions and divs involving
2982 * the current dimensions should be eliminated.
2983 * In case we are working within a separation class, we need to intersect
2984 * the result with the current "class_domain" to ensure that the domains
2985 * are disjoint from those generated from other class domains.
2987 * The domain that has been made atomic may be larger than specified
2988 * by the user since it needs to be representable as a single basic set.
2989 * This possibly larger domain is removed from class_domain by
2990 * compute_atomic_domain. It is computed first so that the extended domain
2991 * would not overlap with any domains computed before.
2992 * Similary, the unrolled domains may have some constraints removed and
2993 * may therefore also be larger than specified by the user.
2995 * If anything is left after handling separate, unroll and atomic,
2996 * we split it up into basic sets and append the basic sets to domains->list.
2998 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2999 __isl_take isl_set *class_domain)
3001 isl_basic_set_list *list;
3002 isl_set *domain;
3004 class_domain = isl_set_subtract(class_domain,
3005 isl_set_copy(domains->done));
3006 domains->done = isl_set_union(domains->done,
3007 isl_set_copy(class_domain));
3009 class_domain = compute_atomic_domain(domains, class_domain);
3010 class_domain = compute_unroll_domains(domains, class_domain);
3012 domain = isl_set_copy(class_domain);
3014 if (compute_separate_domain(domains, domain) < 0)
3015 goto error;
3016 domain = isl_set_subtract(domain,
3017 isl_set_copy(domains->option[isl_ast_loop_separate]));
3019 domain = isl_set_intersect(domain,
3020 isl_set_copy(domains->schedule_domain));
3022 domain = isl_ast_build_eliminate(domains->build, domain);
3023 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
3025 domain = isl_set_coalesce_preserve(domain);
3026 domain = isl_set_make_disjoint(domain);
3028 list = isl_basic_set_list_from_set(domain);
3029 domains->list = isl_basic_set_list_concat(domains->list, list);
3031 isl_set_free(class_domain);
3033 return isl_stat_ok;
3034 error:
3035 isl_set_free(domain);
3036 isl_set_free(class_domain);
3037 return isl_stat_error;
3040 /* Split up the domain at the current depth into disjoint
3041 * basic sets for which code should be generated separately
3042 * for the separation class identified by "pnt".
3044 * We extract the corresponding class domain from domains->sep_class,
3045 * eliminate inner dimensions and pass control to compute_partial_domains.
3047 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
3049 struct isl_codegen_domains *domains = user;
3050 isl_set *class_set;
3051 isl_set *domain;
3052 int disjoint;
3054 class_set = isl_set_from_point(pnt);
3055 domain = isl_map_domain(isl_map_intersect_range(
3056 isl_map_copy(domains->sep_class), class_set));
3057 domain = isl_ast_build_compute_gist(domains->build, domain);
3058 domain = isl_ast_build_eliminate(domains->build, domain);
3060 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
3061 if (disjoint < 0)
3062 return isl_stat_error;
3063 if (disjoint) {
3064 isl_set_free(domain);
3065 return isl_stat_ok;
3068 return compute_partial_domains(domains, domain);
3071 /* Extract the domains at the current depth that should be atomic,
3072 * separated or unrolled and store them in option.
3074 * The domains specified by the user might overlap, so we make
3075 * them disjoint by subtracting earlier domains from later domains.
3077 static void compute_domains_init_options(isl_set *option[4],
3078 __isl_keep isl_ast_build *build)
3080 enum isl_ast_loop_type type, type2;
3081 isl_set *unroll;
3083 for (type = isl_ast_loop_atomic;
3084 type <= isl_ast_loop_separate; ++type) {
3085 option[type] = isl_ast_build_get_option_domain(build, type);
3086 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3087 option[type] = isl_set_subtract(option[type],
3088 isl_set_copy(option[type2]));
3091 unroll = option[isl_ast_loop_unroll];
3092 unroll = isl_set_coalesce(unroll);
3093 unroll = isl_set_make_disjoint(unroll);
3094 option[isl_ast_loop_unroll] = unroll;
3097 /* Split up the domain at the current depth into disjoint
3098 * basic sets for which code should be generated separately,
3099 * based on the user-specified options.
3100 * Return the list of disjoint basic sets.
3102 * There are three kinds of domains that we need to keep track of.
3103 * - the "schedule domain" is the domain of "executed"
3104 * - the "class domain" is the domain corresponding to the currrent
3105 * separation class
3106 * - the "option domain" is the domain corresponding to one of the options
3107 * atomic, unroll or separate
3109 * We first consider the individial values of the separation classes
3110 * and split up the domain for each of them separately.
3111 * Finally, we consider the remainder. If no separation classes were
3112 * specified, then we call compute_partial_domains with the universe
3113 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3114 * with inner dimensions removed. We do this because we want to
3115 * avoid computing the complement of the class domains (i.e., the difference
3116 * between the universe and domains->done).
3118 static __isl_give isl_basic_set_list *compute_domains(
3119 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3121 struct isl_codegen_domains domains;
3122 isl_ctx *ctx;
3123 isl_set *domain;
3124 isl_union_set *schedule_domain;
3125 isl_set *classes;
3126 isl_space *space;
3127 int n_param;
3128 enum isl_ast_loop_type type;
3129 isl_bool empty;
3131 if (!executed)
3132 return NULL;
3134 ctx = isl_union_map_get_ctx(executed);
3135 domains.list = isl_basic_set_list_alloc(ctx, 0);
3137 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3138 domain = isl_set_from_union_set(schedule_domain);
3140 compute_domains_init_options(domains.option, build);
3142 domains.sep_class = isl_ast_build_get_separation_class(build);
3143 classes = isl_map_range(isl_map_copy(domains.sep_class));
3144 n_param = isl_set_dim(classes, isl_dim_param);
3145 if (n_param < 0)
3146 classes = isl_set_free(classes);
3147 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3149 space = isl_set_get_space(domain);
3150 domains.build = build;
3151 domains.schedule_domain = isl_set_copy(domain);
3152 domains.executed = executed;
3153 domains.done = isl_set_empty(space);
3155 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3156 domains.list = isl_basic_set_list_free(domains.list);
3157 isl_set_free(classes);
3159 empty = isl_set_is_empty(domains.done);
3160 if (empty < 0) {
3161 domains.list = isl_basic_set_list_free(domains.list);
3162 domain = isl_set_free(domain);
3163 } else if (empty) {
3164 isl_set_free(domain);
3165 domain = isl_set_universe(isl_set_get_space(domains.done));
3166 } else {
3167 domain = isl_ast_build_eliminate(build, domain);
3169 if (compute_partial_domains(&domains, domain) < 0)
3170 domains.list = isl_basic_set_list_free(domains.list);
3172 isl_set_free(domains.schedule_domain);
3173 isl_set_free(domains.done);
3174 isl_map_free(domains.sep_class);
3175 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3176 isl_set_free(domains.option[type]);
3178 return domains.list;
3181 /* Generate code for a single component, after shifting (if any)
3182 * has been applied, in case the schedule was specified as a union map.
3184 * We first split up the domain at the current depth into disjoint
3185 * basic sets based on the user-specified options.
3186 * Then we generated code for each of them and concatenate the results.
3188 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3189 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3191 isl_basic_set_list *domain_list;
3192 isl_ast_graft_list *list = NULL;
3194 domain_list = compute_domains(executed, build);
3195 list = generate_parallel_domains(domain_list, executed, build);
3197 isl_basic_set_list_free(domain_list);
3198 isl_union_map_free(executed);
3199 isl_ast_build_free(build);
3201 return list;
3204 /* Generate code for a single component, after shifting (if any)
3205 * has been applied, in case the schedule was specified as a schedule tree
3206 * and the separate option was specified.
3208 * We perform separation on the domain of "executed" and then generate
3209 * an AST for each of the resulting disjoint basic sets.
3211 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3212 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3214 isl_space *space;
3215 isl_set *domain;
3216 isl_basic_set_list *domain_list;
3217 isl_ast_graft_list *list;
3219 space = isl_ast_build_get_space(build, 1);
3220 domain = separate_schedule_domains(space,
3221 isl_union_map_copy(executed), build);
3222 domain_list = isl_basic_set_list_from_set(domain);
3224 list = generate_parallel_domains(domain_list, executed, build);
3226 isl_basic_set_list_free(domain_list);
3227 isl_union_map_free(executed);
3228 isl_ast_build_free(build);
3230 return list;
3233 /* Internal data structure for generate_shifted_component_tree_unroll.
3235 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3236 * "list" collects the constructs grafts.
3238 struct isl_ast_unroll_tree_data {
3239 isl_union_map *executed;
3240 isl_ast_build *build;
3241 isl_ast_graft_list *list;
3244 /* Initialize data->list to a list of "n" elements.
3246 static int init_unroll_tree(int n, void *user)
3248 struct isl_ast_unroll_tree_data *data = user;
3249 isl_ctx *ctx;
3251 ctx = isl_ast_build_get_ctx(data->build);
3252 data->list = isl_ast_graft_list_alloc(ctx, n);
3254 return 0;
3257 /* Given an iteration of an unrolled domain represented by "bset",
3258 * generate the corresponding AST and add the result to data->list.
3260 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3262 struct isl_ast_unroll_tree_data *data = user;
3264 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3265 bset, isl_ast_build_copy(data->build));
3267 return 0;
3270 /* Generate code for a single component, after shifting (if any)
3271 * has been applied, in case the schedule was specified as a schedule tree
3272 * and the unroll option was specified.
3274 * We call foreach_iteration to iterate over the individual values and
3275 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3277 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3278 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3279 __isl_take isl_ast_build *build)
3281 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3283 if (foreach_iteration(domain, build, &init_unroll_tree,
3284 &do_unroll_tree_iteration, &data) < 0)
3285 data.list = isl_ast_graft_list_free(data.list);
3287 isl_union_map_free(executed);
3288 isl_ast_build_free(build);
3290 return data.list;
3293 /* Does "domain" involve a disjunction that is purely based on
3294 * constraints involving only outer dimension?
3296 * In particular, is there a disjunction such that the constraints
3297 * involving the current and later dimensions are the same over
3298 * all the disjuncts?
3300 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3301 __isl_keep isl_ast_build *build)
3303 isl_basic_set *hull;
3304 isl_set *shared, *inner;
3305 isl_bool equal;
3306 int depth;
3307 isl_size n;
3308 isl_size dim;
3310 n = isl_set_n_basic_set(domain);
3311 if (n < 0)
3312 return isl_bool_error;
3313 if (n <= 1)
3314 return isl_bool_false;
3315 dim = isl_set_dim(domain, isl_dim_set);
3316 if (dim < 0)
3317 return isl_bool_error;
3319 inner = isl_set_copy(domain);
3320 depth = isl_ast_build_get_depth(build);
3321 inner = isl_set_drop_constraints_not_involving_dims(inner,
3322 isl_dim_set, depth, dim - depth);
3323 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3324 shared = isl_set_from_basic_set(hull);
3325 equal = isl_set_plain_is_equal(inner, shared);
3326 isl_set_free(inner);
3327 isl_set_free(shared);
3329 return equal;
3332 /* Generate code for a single component, after shifting (if any)
3333 * has been applied, in case the schedule was specified as a schedule tree.
3334 * In particular, handle the base case where there is either no isolated
3335 * set or we are within the isolated set (in which case "isolated" is set)
3336 * or the iterations that precede or follow the isolated set.
3338 * The schedule domain is broken up or combined into basic sets
3339 * according to the AST generation option specified in the current
3340 * schedule node, which may be either atomic, separate, unroll or
3341 * unspecified. If the option is unspecified, then we currently simply
3342 * split the schedule domain into disjoint basic sets.
3344 * In case the separate option is specified, the AST generation is
3345 * handled by generate_shifted_component_tree_separate.
3346 * In the other cases, we need the global schedule domain.
3347 * In the unroll case, the AST generation is then handled by
3348 * generate_shifted_component_tree_unroll which needs the actual
3349 * schedule domain (with divs that may refer to the current dimension)
3350 * so that stride detection can be performed.
3351 * In the atomic or unspecified case, inner dimensions and divs involving
3352 * the current dimensions should be eliminated.
3353 * The result is then either combined into a single basic set or
3354 * split up into disjoint basic sets.
3355 * Finally an AST is generated for each basic set and the results are
3356 * concatenated.
3358 * If the schedule domain involves a disjunction that is purely based on
3359 * constraints involving only outer dimension, then it is treated as
3360 * if atomic was specified. This ensures that only a single loop
3361 * is generated instead of a sequence of identical loops with
3362 * different guards.
3364 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3365 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3366 int isolated)
3368 isl_bool outer_disjunction;
3369 isl_union_set *schedule_domain;
3370 isl_set *domain;
3371 isl_basic_set_list *domain_list;
3372 isl_ast_graft_list *list;
3373 enum isl_ast_loop_type type;
3375 type = isl_ast_build_get_loop_type(build, isolated);
3376 if (type < 0)
3377 goto error;
3379 if (type == isl_ast_loop_separate)
3380 return generate_shifted_component_tree_separate(executed,
3381 build);
3383 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3384 domain = isl_set_from_union_set(schedule_domain);
3386 if (type == isl_ast_loop_unroll)
3387 return generate_shifted_component_tree_unroll(executed, domain,
3388 build);
3390 domain = isl_ast_build_eliminate(build, domain);
3391 domain = isl_set_coalesce_preserve(domain);
3393 outer_disjunction = has_pure_outer_disjunction(domain, build);
3394 if (outer_disjunction < 0)
3395 domain = isl_set_free(domain);
3397 if (outer_disjunction || type == isl_ast_loop_atomic) {
3398 isl_basic_set *hull;
3399 hull = isl_set_unshifted_simple_hull(domain);
3400 domain_list = isl_basic_set_list_from_basic_set(hull);
3401 } else {
3402 domain = isl_set_make_disjoint(domain);
3403 domain_list = isl_basic_set_list_from_set(domain);
3406 list = generate_parallel_domains(domain_list, executed, build);
3408 isl_basic_set_list_free(domain_list);
3409 isl_union_map_free(executed);
3410 isl_ast_build_free(build);
3412 return list;
3413 error:
3414 isl_union_map_free(executed);
3415 isl_ast_build_free(build);
3416 return NULL;
3419 /* Extract out the disjunction imposed by "domain" on the outer
3420 * schedule dimensions.
3422 * In particular, remove all inner dimensions from "domain" (including
3423 * the current dimension) and then remove the constraints that are shared
3424 * by all disjuncts in the result.
3426 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3427 __isl_keep isl_ast_build *build)
3429 isl_set *hull;
3430 int depth;
3431 isl_size dim;
3433 domain = isl_ast_build_specialize(build, domain);
3434 depth = isl_ast_build_get_depth(build);
3435 dim = isl_set_dim(domain, isl_dim_set);
3436 if (dim < 0)
3437 return isl_set_free(domain);
3438 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3439 domain = isl_set_remove_unknown_divs(domain);
3440 hull = isl_set_copy(domain);
3441 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3442 domain = isl_set_gist(domain, hull);
3444 return domain;
3447 /* Add "guard" to the grafts in "list".
3448 * "build" is the outer AST build, while "sub_build" includes "guard"
3449 * in its generated domain.
3451 * First combine the grafts into a single graft and then add the guard.
3452 * If the list is empty, or if some error occurred, then simply return
3453 * the list.
3455 static __isl_give isl_ast_graft_list *list_add_guard(
3456 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3457 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3459 isl_ast_graft *graft;
3460 isl_size n;
3462 list = isl_ast_graft_list_fuse(list, sub_build);
3464 n = isl_ast_graft_list_n_ast_graft(list);
3465 if (n < 0)
3466 return isl_ast_graft_list_free(list);
3467 if (n != 1)
3468 return list;
3470 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3471 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3472 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3474 return list;
3477 /* Generate code for a single component, after shifting (if any)
3478 * has been applied, in case the schedule was specified as a schedule tree.
3479 * In particular, do so for the specified subset of the schedule domain.
3481 * If we are outside of the isolated part, then "domain" may include
3482 * a disjunction. Explicitly generate this disjunction at this point
3483 * instead of relying on the disjunction getting hoisted back up
3484 * to this level.
3486 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3487 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3488 __isl_keep isl_ast_build *build, int isolated)
3490 isl_union_set *uset;
3491 isl_ast_graft_list *list;
3492 isl_ast_build *sub_build;
3493 int empty;
3495 uset = isl_union_set_from_set(isl_set_copy(domain));
3496 executed = isl_union_map_copy(executed);
3497 executed = isl_union_map_intersect_domain(executed, uset);
3498 empty = isl_union_map_is_empty(executed);
3499 if (empty < 0)
3500 goto error;
3501 if (empty) {
3502 isl_ctx *ctx;
3503 isl_union_map_free(executed);
3504 isl_set_free(domain);
3505 ctx = isl_ast_build_get_ctx(build);
3506 return isl_ast_graft_list_alloc(ctx, 0);
3509 sub_build = isl_ast_build_copy(build);
3510 if (!isolated) {
3511 domain = extract_disjunction(domain, build);
3512 sub_build = isl_ast_build_restrict_generated(sub_build,
3513 isl_set_copy(domain));
3515 list = generate_shifted_component_tree_base(executed,
3516 isl_ast_build_copy(sub_build), isolated);
3517 if (!isolated)
3518 list = list_add_guard(list, domain, build, sub_build);
3519 isl_ast_build_free(sub_build);
3520 isl_set_free(domain);
3521 return list;
3522 error:
3523 isl_union_map_free(executed);
3524 isl_set_free(domain);
3525 return NULL;
3528 /* Generate code for a single component, after shifting (if any)
3529 * has been applied, in case the schedule was specified as a schedule tree.
3530 * In particular, do so for the specified sequence of subsets
3531 * of the schedule domain, "before", "isolated", "after" and "other",
3532 * where only the "isolated" part is considered to be isolated.
3534 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3535 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3536 __isl_take isl_set *isolated, __isl_take isl_set *after,
3537 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3539 isl_ast_graft_list *list, *res;
3541 res = generate_shifted_component_tree_part(executed, before, build, 0);
3542 list = generate_shifted_component_tree_part(executed, isolated,
3543 build, 1);
3544 res = isl_ast_graft_list_concat(res, list);
3545 list = generate_shifted_component_tree_part(executed, after, build, 0);
3546 res = isl_ast_graft_list_concat(res, list);
3547 list = generate_shifted_component_tree_part(executed, other, build, 0);
3548 res = isl_ast_graft_list_concat(res, list);
3550 isl_union_map_free(executed);
3551 isl_ast_build_free(build);
3553 return res;
3556 /* Does "set" intersect "first", but not "second"?
3558 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3559 __isl_keep isl_set *first, __isl_keep isl_set *second)
3561 isl_bool disjoint;
3563 disjoint = isl_set_is_disjoint(set, first);
3564 if (disjoint < 0)
3565 return isl_bool_error;
3566 if (disjoint)
3567 return isl_bool_false;
3569 return isl_set_is_disjoint(set, second);
3572 /* Generate code for a single component, after shifting (if any)
3573 * has been applied, in case the schedule was specified as a schedule tree.
3574 * In particular, do so in case of isolation where there is
3575 * only an "isolated" part and an "after" part.
3576 * "dead1" and "dead2" are freed by this function in order to simplify
3577 * the caller.
3579 * The "before" and "other" parts are set to empty sets.
3581 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3582 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3583 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3584 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3586 isl_set *empty;
3588 empty = isl_set_empty(isl_set_get_space(after));
3589 isl_set_free(dead1);
3590 isl_set_free(dead2);
3591 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3592 isolated, after, empty, build);
3595 /* Generate code for a single component, after shifting (if any)
3596 * has been applied, in case the schedule was specified as a schedule tree.
3598 * We first check if the user has specified an isolated schedule domain
3599 * and that we are not already outside of this isolated schedule domain.
3600 * If so, we break up the schedule domain into iterations that
3601 * precede the isolated domain, the isolated domain itself,
3602 * the iterations that follow the isolated domain and
3603 * the remaining iterations (those that are incomparable
3604 * to the isolated domain).
3605 * We generate an AST for each piece and concatenate the results.
3607 * If the isolated domain is not convex, then it is replaced
3608 * by a convex superset to ensure that the sets of preceding and
3609 * following iterations are properly defined and, in particular,
3610 * that there are no intermediate iterations that do not belong
3611 * to the isolated domain.
3613 * In the special case where at least one element of the schedule
3614 * domain that does not belong to the isolated domain needs
3615 * to be scheduled after this isolated domain, but none of those
3616 * elements need to be scheduled before, break up the schedule domain
3617 * in only two parts, the isolated domain, and a part that will be
3618 * scheduled after the isolated domain.
3620 * If no isolated set has been specified, then we generate an
3621 * AST for the entire inverse schedule.
3623 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3624 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3626 int i, depth;
3627 int empty, has_isolate;
3628 isl_space *space;
3629 isl_union_set *schedule_domain;
3630 isl_set *domain;
3631 isl_basic_set *hull;
3632 isl_set *isolated, *before, *after, *test;
3633 isl_map *gt, *lt;
3634 isl_bool pure;
3636 build = isl_ast_build_extract_isolated(build);
3637 has_isolate = isl_ast_build_has_isolated(build);
3638 if (has_isolate < 0)
3639 executed = isl_union_map_free(executed);
3640 else if (!has_isolate)
3641 return generate_shifted_component_tree_base(executed, build, 0);
3643 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3644 domain = isl_set_from_union_set(schedule_domain);
3646 isolated = isl_ast_build_get_isolated(build);
3647 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3648 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3649 empty = isl_set_is_empty(test);
3650 isl_set_free(test);
3651 if (empty < 0)
3652 goto error;
3653 if (empty) {
3654 isl_set_free(isolated);
3655 isl_set_free(domain);
3656 return generate_shifted_component_tree_base(executed, build, 0);
3658 isolated = isl_ast_build_eliminate(build, isolated);
3659 hull = isl_set_unshifted_simple_hull(isolated);
3660 isolated = isl_set_from_basic_set(hull);
3662 depth = isl_ast_build_get_depth(build);
3663 space = isl_space_map_from_set(isl_set_get_space(isolated));
3664 gt = isl_map_universe(space);
3665 for (i = 0; i < depth; ++i)
3666 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3667 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3668 lt = isl_map_reverse(isl_map_copy(gt));
3669 before = isl_set_apply(isl_set_copy(isolated), gt);
3670 after = isl_set_apply(isl_set_copy(isolated), lt);
3672 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3673 pure = only_intersects_first(domain, after, before);
3674 if (pure < 0)
3675 executed = isl_union_map_free(executed);
3676 else if (pure)
3677 return generate_shifted_component_only_after(executed, isolated,
3678 domain, build, before, after);
3679 domain = isl_set_subtract(domain, isl_set_copy(before));
3680 domain = isl_set_subtract(domain, isl_set_copy(after));
3681 after = isl_set_subtract(after, isl_set_copy(isolated));
3682 after = isl_set_subtract(after, isl_set_copy(before));
3683 before = isl_set_subtract(before, isl_set_copy(isolated));
3685 return generate_shifted_component_parts(executed, before, isolated,
3686 after, domain, build);
3687 error:
3688 isl_set_free(domain);
3689 isl_set_free(isolated);
3690 isl_union_map_free(executed);
3691 isl_ast_build_free(build);
3692 return NULL;
3695 /* Generate code for a single component, after shifting (if any)
3696 * has been applied.
3698 * Call generate_shifted_component_tree or generate_shifted_component_flat
3699 * depending on whether the schedule was specified as a schedule tree.
3701 static __isl_give isl_ast_graft_list *generate_shifted_component(
3702 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3704 if (isl_ast_build_has_schedule_node(build))
3705 return generate_shifted_component_tree(executed, build);
3706 else
3707 return generate_shifted_component_flat(executed, build);
3710 struct isl_set_map_pair {
3711 isl_set *set;
3712 isl_map *map;
3715 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3716 * of indices into the "domain" array,
3717 * return the union of the "map" fields of the elements
3718 * indexed by the first "n" elements of "order".
3720 static __isl_give isl_union_map *construct_component_executed(
3721 struct isl_set_map_pair *domain, int *order, int n)
3723 int i;
3724 isl_map *map;
3725 isl_union_map *executed;
3727 map = isl_map_copy(domain[order[0]].map);
3728 executed = isl_union_map_from_map(map);
3729 for (i = 1; i < n; ++i) {
3730 map = isl_map_copy(domain[order[i]].map);
3731 executed = isl_union_map_add_map(executed, map);
3734 return executed;
3737 /* Generate code for a single component, after shifting (if any)
3738 * has been applied.
3740 * The component inverse schedule is specified as the "map" fields
3741 * of the elements of "domain" indexed by the first "n" elements of "order".
3743 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3744 struct isl_set_map_pair *domain, int *order, int n,
3745 __isl_take isl_ast_build *build)
3747 isl_union_map *executed;
3749 executed = construct_component_executed(domain, order, n);
3750 return generate_shifted_component(executed, build);
3753 /* Does set dimension "pos" of "set" have an obviously fixed value?
3755 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3757 int fixed;
3758 isl_val *v;
3760 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3761 if (!v)
3762 return -1;
3763 fixed = !isl_val_is_nan(v);
3764 isl_val_free(v);
3766 return fixed;
3769 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3770 * of indices into the "domain" array,
3771 * do all (except for at most one) of the "set" field of the elements
3772 * indexed by the first "n" elements of "order" have a fixed value
3773 * at position "depth"?
3775 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3776 int *order, int n, int depth)
3778 int i;
3779 int non_fixed = -1;
3781 for (i = 0; i < n; ++i) {
3782 int f;
3784 f = dim_is_fixed(domain[order[i]].set, depth);
3785 if (f < 0)
3786 return -1;
3787 if (f)
3788 continue;
3789 if (non_fixed >= 0)
3790 return 0;
3791 non_fixed = i;
3794 return 1;
3797 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3798 * of indices into the "domain" array,
3799 * eliminate the inner dimensions from the "set" field of the elements
3800 * indexed by the first "n" elements of "order", provided the current
3801 * dimension does not have a fixed value.
3803 * Return the index of the first element in "order" with a corresponding
3804 * "set" field that does not have an (obviously) fixed value.
3806 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3807 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3809 int i;
3810 int base = -1;
3812 for (i = n - 1; i >= 0; --i) {
3813 int f;
3814 f = dim_is_fixed(domain[order[i]].set, depth);
3815 if (f < 0)
3816 return -1;
3817 if (f)
3818 continue;
3819 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3820 domain[order[i]].set);
3821 base = i;
3824 return base;
3827 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3828 * of indices into the "domain" array,
3829 * find the element of "domain" (amongst those indexed by the first "n"
3830 * elements of "order") with the "set" field that has the smallest
3831 * value for the current iterator.
3833 * Note that the domain with the smallest value may depend on the parameters
3834 * and/or outer loop dimension. Since the result of this function is only
3835 * used as heuristic, we only make a reasonable attempt at finding the best
3836 * domain, one that should work in case a single domain provides the smallest
3837 * value for the current dimension over all values of the parameters
3838 * and outer dimensions.
3840 * In particular, we compute the smallest value of the first domain
3841 * and replace it by that of any later domain if that later domain
3842 * has a smallest value that is smaller for at least some value
3843 * of the parameters and outer dimensions.
3845 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3846 __isl_keep isl_ast_build *build)
3848 int i;
3849 isl_map *min_first;
3850 int first = 0;
3852 min_first = isl_ast_build_map_to_iterator(build,
3853 isl_set_copy(domain[order[0]].set));
3854 min_first = isl_map_lexmin(min_first);
3856 for (i = 1; i < n; ++i) {
3857 isl_map *min, *test;
3858 int empty;
3860 min = isl_ast_build_map_to_iterator(build,
3861 isl_set_copy(domain[order[i]].set));
3862 min = isl_map_lexmin(min);
3863 test = isl_map_copy(min);
3864 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3865 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3866 empty = isl_map_is_empty(test);
3867 isl_map_free(test);
3868 if (empty >= 0 && !empty) {
3869 isl_map_free(min_first);
3870 first = i;
3871 min_first = min;
3872 } else
3873 isl_map_free(min);
3875 if (empty < 0)
3876 break;
3879 isl_map_free(min_first);
3881 return i < n ? -1 : first;
3884 /* Construct a shifted inverse schedule based on the original inverse schedule,
3885 * the stride and the offset.
3887 * The original inverse schedule is specified as the "map" fields
3888 * of the elements of "domain" indexed by the first "n" elements of "order".
3890 * "stride" and "offset" are such that the difference
3891 * between the values of the current dimension of domain "i"
3892 * and the values of the current dimension for some reference domain are
3893 * equal to
3895 * stride * integer + offset[i]
3897 * Moreover, 0 <= offset[i] < stride.
3899 * For each domain, we create a map
3901 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3903 * where j refers to the current dimension and the other dimensions are
3904 * unchanged, and apply this map to the original schedule domain.
3906 * For example, for the original schedule
3908 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3910 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3911 * we apply the mapping
3913 * { [j] -> [j, 0] }
3915 * to the schedule of the "A" domain and the mapping
3917 * { [j - 1] -> [j, 1] }
3919 * to the schedule of the "B" domain.
3922 * Note that after the transformation, the differences between pairs
3923 * of values of the current dimension over all domains are multiples
3924 * of stride and that we have therefore exposed the stride.
3927 * To see that the mapping preserves the lexicographic order,
3928 * first note that each of the individual maps above preserves the order.
3929 * If the value of the current iterator is j1 in one domain and j2 in another,
3930 * then if j1 = j2, we know that the same map is applied to both domains
3931 * and the order is preserved.
3932 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3933 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3935 * j1 - c1 < j2 - c2
3937 * and the order is preserved.
3938 * If c1 < c2, then we know
3940 * 0 <= c2 - c1 < s
3942 * We also have
3944 * j2 - j1 = n * s + r
3946 * with n >= 0 and 0 <= r < s.
3947 * In other words, r = c2 - c1.
3948 * If n > 0, then
3950 * j1 - c1 < j2 - c2
3952 * If n = 0, then
3954 * j1 - c1 = j2 - c2
3956 * and so
3958 * (j1 - c1, c1) << (j2 - c2, c2)
3960 * with "<<" the lexicographic order, proving that the order is preserved
3961 * in all cases.
3963 static __isl_give isl_union_map *construct_shifted_executed(
3964 struct isl_set_map_pair *domain, int *order, int n,
3965 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3966 __isl_take isl_ast_build *build)
3968 int i;
3969 isl_union_map *executed;
3970 isl_space *space;
3971 isl_map *map;
3972 int depth;
3973 isl_constraint *c;
3975 depth = isl_ast_build_get_depth(build);
3976 space = isl_ast_build_get_space(build, 1);
3977 executed = isl_union_map_empty(isl_space_copy(space));
3978 space = isl_space_map_from_set(space);
3979 map = isl_map_identity(isl_space_copy(space));
3980 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3981 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3982 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3984 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3985 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3986 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3988 for (i = 0; i < n; ++i) {
3989 isl_map *map_i;
3990 isl_val *v;
3992 v = isl_multi_val_get_val(offset, i);
3993 if (!v)
3994 break;
3995 map_i = isl_map_copy(map);
3996 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3997 isl_val_copy(v));
3998 v = isl_val_neg(v);
3999 c = isl_constraint_set_constant_val(c, v);
4000 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
4002 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
4003 map_i);
4004 executed = isl_union_map_add_map(executed, map_i);
4007 isl_constraint_free(c);
4008 isl_map_free(map);
4010 if (i < n)
4011 executed = isl_union_map_free(executed);
4013 return executed;
4016 /* Generate code for a single component, after exposing the stride,
4017 * given that the schedule domain is "shifted strided".
4019 * The component inverse schedule is specified as the "map" fields
4020 * of the elements of "domain" indexed by the first "n" elements of "order".
4022 * The schedule domain being "shifted strided" means that the differences
4023 * between the values of the current dimension of domain "i"
4024 * and the values of the current dimension for some reference domain are
4025 * equal to
4027 * stride * integer + offset[i]
4029 * We first look for the domain with the "smallest" value for the current
4030 * dimension and adjust the offsets such that the offset of the "smallest"
4031 * domain is equal to zero. The other offsets are reduced modulo stride.
4033 * Based on this information, we construct a new inverse schedule in
4034 * construct_shifted_executed that exposes the stride.
4035 * Since this involves the introduction of a new schedule dimension,
4036 * the build needs to be changed accordingly.
4037 * After computing the AST, the newly introduced dimension needs
4038 * to be removed again from the list of grafts. We do this by plugging
4039 * in a mapping that represents the new schedule domain in terms of the
4040 * old schedule domain.
4042 static __isl_give isl_ast_graft_list *generate_shift_component(
4043 struct isl_set_map_pair *domain, int *order, int n,
4044 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
4045 __isl_take isl_ast_build *build)
4047 isl_ast_graft_list *list;
4048 int first;
4049 int depth;
4050 isl_val *val;
4051 isl_multi_val *mv;
4052 isl_space *space;
4053 isl_multi_aff *ma, *zero;
4054 isl_union_map *executed;
4056 depth = isl_ast_build_get_depth(build);
4058 first = first_offset(domain, order, n, build);
4059 if (first < 0)
4060 goto error;
4062 mv = isl_multi_val_copy(offset);
4063 val = isl_multi_val_get_val(offset, first);
4064 val = isl_val_neg(val);
4065 mv = isl_multi_val_add_val(mv, val);
4066 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
4068 executed = construct_shifted_executed(domain, order, n, stride, mv,
4069 build);
4070 space = isl_ast_build_get_space(build, 1);
4071 space = isl_space_map_from_set(space);
4072 ma = isl_multi_aff_identity(isl_space_copy(space));
4073 space = isl_space_from_domain(isl_space_domain(space));
4074 space = isl_space_add_dims(space, isl_dim_out, 1);
4075 zero = isl_multi_aff_zero(space);
4076 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
4077 build = isl_ast_build_insert_dim(build, depth + 1);
4078 list = generate_shifted_component(executed, build);
4080 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4082 isl_multi_val_free(mv);
4084 return list;
4085 error:
4086 isl_ast_build_free(build);
4087 return NULL;
4090 /* Does any node in the schedule tree rooted at the current schedule node
4091 * of "build" depend on outer schedule nodes?
4093 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4095 isl_schedule_node *node;
4096 int dependent = 0;
4098 node = isl_ast_build_get_schedule_node(build);
4099 dependent = isl_schedule_node_is_subtree_anchored(node);
4100 isl_schedule_node_free(node);
4102 return dependent;
4105 /* Generate code for a single component.
4107 * The component inverse schedule is specified as the "map" fields
4108 * of the elements of "domain" indexed by the first "n" elements of "order".
4110 * This function may modify the "set" fields of "domain".
4112 * Before proceeding with the actual code generation for the component,
4113 * we first check if there are any "shifted" strides, meaning that
4114 * the schedule domains of the individual domains are all strided,
4115 * but that they have different offsets, resulting in the union
4116 * of schedule domains not being strided anymore.
4118 * The simplest example is the schedule
4120 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4122 * Both schedule domains are strided, but their union is not.
4123 * This function detects such cases and then rewrites the schedule to
4125 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4127 * In the new schedule, the schedule domains have the same offset (modulo
4128 * the stride), ensuring that the union of schedule domains is also strided.
4131 * If there is only a single domain in the component, then there is
4132 * nothing to do. Similarly, if the current schedule dimension has
4133 * a fixed value for almost all domains then there is nothing to be done.
4134 * In particular, we need at least two domains where the current schedule
4135 * dimension does not have a fixed value.
4136 * Finally, in case of a schedule map input,
4137 * if any of the options refer to the current schedule dimension,
4138 * then we bail out as well. It would be possible to reformulate the options
4139 * in terms of the new schedule domain, but that would introduce constraints
4140 * that separate the domains in the options and that is something we would
4141 * like to avoid.
4142 * In the case of a schedule tree input, we bail out if any of
4143 * the descendants of the current schedule node refer to outer
4144 * schedule nodes in any way.
4147 * To see if there is any shifted stride, we look at the differences
4148 * between the values of the current dimension in pairs of domains
4149 * for equal values of outer dimensions. These differences should be
4150 * of the form
4152 * m x + r
4154 * with "m" the stride and "r" a constant. Note that we cannot perform
4155 * this analysis on individual domains as the lower bound in each domain
4156 * may depend on parameters or outer dimensions and so the current dimension
4157 * itself may not have a fixed remainder on division by the stride.
4159 * In particular, we compare the first domain that does not have an
4160 * obviously fixed value for the current dimension to itself and all
4161 * other domains and collect the offsets and the gcd of the strides.
4162 * If the gcd becomes one, then we failed to find shifted strides.
4163 * If the gcd is zero, then the differences were all fixed, meaning
4164 * that some domains had non-obviously fixed values for the current dimension.
4165 * If all the offsets are the same (for those domains that do not have
4166 * an obviously fixed value for the current dimension), then we do not
4167 * apply the transformation.
4168 * If none of the domains were skipped, then there is nothing to do.
4169 * If some of them were skipped, then if we apply separation, the schedule
4170 * domain should get split in pieces with a (non-shifted) stride.
4172 * Otherwise, we apply a shift to expose the stride in
4173 * generate_shift_component.
4175 static __isl_give isl_ast_graft_list *generate_component(
4176 struct isl_set_map_pair *domain, int *order, int n,
4177 __isl_take isl_ast_build *build)
4179 int i, d;
4180 int depth;
4181 isl_ctx *ctx;
4182 isl_map *map;
4183 isl_set *deltas;
4184 isl_val *gcd = NULL;
4185 isl_multi_val *mv;
4186 int fixed, skip;
4187 int base;
4188 isl_ast_graft_list *list;
4189 int res = 0;
4191 depth = isl_ast_build_get_depth(build);
4193 skip = n == 1;
4194 if (skip >= 0 && !skip)
4195 skip = at_most_one_non_fixed(domain, order, n, depth);
4196 if (skip >= 0 && !skip) {
4197 if (isl_ast_build_has_schedule_node(build))
4198 skip = has_anchored_subtree(build);
4199 else
4200 skip = isl_ast_build_options_involve_depth(build);
4202 if (skip < 0)
4203 goto error;
4204 if (skip)
4205 return generate_shifted_component_from_list(domain,
4206 order, n, build);
4208 base = eliminate_non_fixed(domain, order, n, depth, build);
4209 if (base < 0)
4210 goto error;
4212 ctx = isl_ast_build_get_ctx(build);
4214 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4216 fixed = 1;
4217 for (i = 0; i < n; ++i) {
4218 isl_val *r, *m;
4220 map = isl_map_from_domain_and_range(
4221 isl_set_copy(domain[order[base]].set),
4222 isl_set_copy(domain[order[i]].set));
4223 for (d = 0; d < depth; ++d)
4224 map = isl_map_equate(map, isl_dim_in, d,
4225 isl_dim_out, d);
4226 deltas = isl_map_deltas(map);
4227 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4228 isl_set_free(deltas);
4229 if (res < 0)
4230 break;
4232 if (i == 0)
4233 gcd = m;
4234 else
4235 gcd = isl_val_gcd(gcd, m);
4236 if (isl_val_is_one(gcd)) {
4237 isl_val_free(r);
4238 break;
4240 mv = isl_multi_val_set_val(mv, i, r);
4242 res = dim_is_fixed(domain[order[i]].set, depth);
4243 if (res < 0)
4244 break;
4245 if (res)
4246 continue;
4248 if (fixed && i > base) {
4249 isl_val *a, *b;
4250 a = isl_multi_val_get_val(mv, i);
4251 b = isl_multi_val_get_val(mv, base);
4252 if (isl_val_ne(a, b))
4253 fixed = 0;
4254 isl_val_free(a);
4255 isl_val_free(b);
4259 if (res < 0 || !gcd) {
4260 isl_ast_build_free(build);
4261 list = NULL;
4262 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4263 list = generate_shifted_component_from_list(domain,
4264 order, n, build);
4265 } else {
4266 list = generate_shift_component(domain, order, n, gcd, mv,
4267 build);
4270 isl_val_free(gcd);
4271 isl_multi_val_free(mv);
4273 return list;
4274 error:
4275 isl_ast_build_free(build);
4276 return NULL;
4279 /* Store both "map" itself and its domain in the
4280 * structure pointed to by *next and advance to the next array element.
4282 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4284 struct isl_set_map_pair **next = user;
4286 (*next)->map = isl_map_copy(map);
4287 (*next)->set = isl_map_domain(map);
4288 (*next)++;
4290 return isl_stat_ok;
4293 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4294 __isl_keep isl_schedule_node *node);
4296 /* Is any domain element of "umap" scheduled after any of
4297 * the corresponding image elements by the tree rooted at
4298 * the child of "node"?
4300 static isl_bool after_in_child(__isl_keep isl_union_map *umap,
4301 __isl_keep isl_schedule_node *node)
4303 isl_schedule_node *child;
4304 isl_bool after;
4306 child = isl_schedule_node_get_child(node, 0);
4307 after = after_in_tree(umap, child);
4308 isl_schedule_node_free(child);
4310 return after;
4313 /* Is any domain element of "umap" scheduled after any of
4314 * the corresponding image elements by the tree rooted at
4315 * the band node "node"?
4317 * We first check if any domain element is scheduled after any
4318 * of the corresponding image elements by the band node itself.
4319 * If not, we restrict "map" to those pairs of element that
4320 * are scheduled together by the band node and continue with
4321 * the child of the band node.
4322 * If there are no such pairs then the map passed to after_in_child
4323 * will be empty causing it to return 0.
4325 static isl_bool after_in_band(__isl_keep isl_union_map *umap,
4326 __isl_keep isl_schedule_node *node)
4328 isl_multi_union_pw_aff *mupa;
4329 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4330 isl_union_set *domain, *range;
4331 isl_space *space;
4332 isl_bool empty;
4333 isl_bool after;
4334 isl_size n;
4336 n = isl_schedule_node_band_n_member(node);
4337 if (n < 0)
4338 return isl_bool_error;
4339 if (n == 0)
4340 return after_in_child(umap, node);
4342 mupa = isl_schedule_node_band_get_partial_schedule(node);
4343 space = isl_multi_union_pw_aff_get_space(mupa);
4344 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4345 test = isl_union_map_copy(umap);
4346 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4347 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4348 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4349 test = isl_union_map_intersect(test, gt);
4350 empty = isl_union_map_is_empty(test);
4351 isl_union_map_free(test);
4353 if (empty < 0 || !empty) {
4354 isl_union_map_free(partial);
4355 return isl_bool_not(empty);
4358 universe = isl_union_map_universe(isl_union_map_copy(umap));
4359 domain = isl_union_map_domain(isl_union_map_copy(universe));
4360 range = isl_union_map_range(universe);
4361 umap1 = isl_union_map_copy(partial);
4362 umap1 = isl_union_map_intersect_domain(umap1, domain);
4363 umap2 = isl_union_map_intersect_domain(partial, range);
4364 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4365 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4366 after = after_in_child(test, node);
4367 isl_union_map_free(test);
4368 return after;
4371 /* Is any domain element of "umap" scheduled after any of
4372 * the corresponding image elements by the tree rooted at
4373 * the context node "node"?
4375 * The context constraints apply to the schedule domain,
4376 * so we cannot apply them directly to "umap", which contains
4377 * pairs of statement instances. Instead, we add them
4378 * to the range of the prefix schedule for both domain and
4379 * range of "umap".
4381 static isl_bool after_in_context(__isl_keep isl_union_map *umap,
4382 __isl_keep isl_schedule_node *node)
4384 isl_union_map *prefix, *universe, *umap1, *umap2;
4385 isl_union_set *domain, *range;
4386 isl_set *context;
4387 isl_bool after;
4389 umap = isl_union_map_copy(umap);
4390 context = isl_schedule_node_context_get_context(node);
4391 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4392 universe = isl_union_map_universe(isl_union_map_copy(umap));
4393 domain = isl_union_map_domain(isl_union_map_copy(universe));
4394 range = isl_union_map_range(universe);
4395 umap1 = isl_union_map_copy(prefix);
4396 umap1 = isl_union_map_intersect_domain(umap1, domain);
4397 umap2 = isl_union_map_intersect_domain(prefix, range);
4398 umap1 = isl_union_map_intersect_range(umap1,
4399 isl_union_set_from_set(context));
4400 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4401 umap = isl_union_map_intersect(umap, umap1);
4403 after = after_in_child(umap, node);
4405 isl_union_map_free(umap);
4407 return after;
4410 /* Is any domain element of "umap" scheduled after any of
4411 * the corresponding image elements by the tree rooted at
4412 * the expansion node "node"?
4414 * We apply the expansion to domain and range of "umap" and
4415 * continue with its child.
4417 static isl_bool after_in_expansion(__isl_keep isl_union_map *umap,
4418 __isl_keep isl_schedule_node *node)
4420 isl_union_map *expansion;
4421 isl_bool after;
4423 expansion = isl_schedule_node_expansion_get_expansion(node);
4424 umap = isl_union_map_copy(umap);
4425 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4426 umap = isl_union_map_apply_range(umap, expansion);
4428 after = after_in_child(umap, node);
4430 isl_union_map_free(umap);
4432 return after;
4435 /* Is any domain element of "umap" scheduled after any of
4436 * the corresponding image elements by the tree rooted at
4437 * the extension node "node"?
4439 * Since the extension node may add statement instances before or
4440 * after the pairs of statement instances in "umap", we return isl_bool_true
4441 * to ensure that these pairs are not broken up.
4443 static isl_bool after_in_extension(__isl_keep isl_union_map *umap,
4444 __isl_keep isl_schedule_node *node)
4446 return isl_bool_true;
4449 /* Is any domain element of "umap" scheduled after any of
4450 * the corresponding image elements by the tree rooted at
4451 * the filter node "node"?
4453 * We intersect domain and range of "umap" with the filter and
4454 * continue with its child.
4456 static isl_bool after_in_filter(__isl_keep isl_union_map *umap,
4457 __isl_keep isl_schedule_node *node)
4459 isl_union_set *filter;
4460 isl_bool after;
4462 umap = isl_union_map_copy(umap);
4463 filter = isl_schedule_node_filter_get_filter(node);
4464 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4465 umap = isl_union_map_intersect_range(umap, filter);
4467 after = after_in_child(umap, node);
4469 isl_union_map_free(umap);
4471 return after;
4474 /* Is any domain element of "umap" scheduled after any of
4475 * the corresponding image elements by the tree rooted at
4476 * the set node "node"?
4478 * This is only the case if this condition holds in any
4479 * of the (filter) children of the set node.
4480 * In particular, if the domain and the range of "umap"
4481 * are contained in different children, then the condition
4482 * does not hold.
4484 static isl_bool after_in_set(__isl_keep isl_union_map *umap,
4485 __isl_keep isl_schedule_node *node)
4487 int i;
4488 isl_size n;
4490 n = isl_schedule_node_n_children(node);
4491 if (n < 0)
4492 return isl_bool_error;
4493 for (i = 0; i < n; ++i) {
4494 isl_schedule_node *child;
4495 isl_bool after;
4497 child = isl_schedule_node_get_child(node, i);
4498 after = after_in_tree(umap, child);
4499 isl_schedule_node_free(child);
4501 if (after < 0 || after)
4502 return after;
4505 return isl_bool_false;
4508 /* Return the filter of child "i" of "node".
4510 static __isl_give isl_union_set *child_filter(
4511 __isl_keep isl_schedule_node *node, int i)
4513 isl_schedule_node *child;
4514 isl_union_set *filter;
4516 child = isl_schedule_node_get_child(node, i);
4517 filter = isl_schedule_node_filter_get_filter(child);
4518 isl_schedule_node_free(child);
4520 return filter;
4523 /* Is any domain element of "umap" scheduled after any of
4524 * the corresponding image elements by the tree rooted at
4525 * the sequence node "node"?
4527 * This happens in particular if any domain element is
4528 * contained in a later child than one containing a range element or
4529 * if the condition holds within a given child in the sequence.
4530 * The later part of the condition is checked by after_in_set.
4532 static isl_bool after_in_sequence(__isl_keep isl_union_map *umap,
4533 __isl_keep isl_schedule_node *node)
4535 int i, j;
4536 isl_size n;
4537 isl_union_map *umap_i;
4538 isl_bool empty;
4539 isl_bool after = isl_bool_false;
4541 n = isl_schedule_node_n_children(node);
4542 if (n < 0)
4543 return isl_bool_error;
4544 for (i = 1; i < n; ++i) {
4545 isl_union_set *filter_i;
4547 umap_i = isl_union_map_copy(umap);
4548 filter_i = child_filter(node, i);
4549 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4550 empty = isl_union_map_is_empty(umap_i);
4551 if (empty < 0)
4552 goto error;
4553 if (empty) {
4554 isl_union_map_free(umap_i);
4555 continue;
4558 for (j = 0; j < i; ++j) {
4559 isl_union_set *filter_j;
4560 isl_union_map *umap_ij;
4562 umap_ij = isl_union_map_copy(umap_i);
4563 filter_j = child_filter(node, j);
4564 umap_ij = isl_union_map_intersect_range(umap_ij,
4565 filter_j);
4566 empty = isl_union_map_is_empty(umap_ij);
4567 isl_union_map_free(umap_ij);
4569 if (empty < 0)
4570 goto error;
4571 if (!empty)
4572 after = isl_bool_true;
4573 if (after)
4574 break;
4577 isl_union_map_free(umap_i);
4578 if (after)
4579 break;
4582 if (after < 0 || after)
4583 return after;
4585 return after_in_set(umap, node);
4586 error:
4587 isl_union_map_free(umap_i);
4588 return isl_bool_error;
4591 /* Is any domain element of "umap" scheduled after any of
4592 * the corresponding image elements by the tree rooted at "node"?
4594 * If "umap" is empty, then clearly there is no such element.
4595 * Otherwise, consider the different types of nodes separately.
4597 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4598 __isl_keep isl_schedule_node *node)
4600 isl_bool empty;
4601 enum isl_schedule_node_type type;
4603 empty = isl_union_map_is_empty(umap);
4604 if (empty < 0)
4605 return isl_bool_error;
4606 if (empty)
4607 return isl_bool_false;
4608 if (!node)
4609 return isl_bool_error;
4611 type = isl_schedule_node_get_type(node);
4612 switch (type) {
4613 case isl_schedule_node_error:
4614 return isl_bool_error;
4615 case isl_schedule_node_leaf:
4616 return isl_bool_false;
4617 case isl_schedule_node_band:
4618 return after_in_band(umap, node);
4619 case isl_schedule_node_domain:
4620 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4621 "unexpected internal domain node",
4622 return isl_bool_error);
4623 case isl_schedule_node_context:
4624 return after_in_context(umap, node);
4625 case isl_schedule_node_expansion:
4626 return after_in_expansion(umap, node);
4627 case isl_schedule_node_extension:
4628 return after_in_extension(umap, node);
4629 case isl_schedule_node_filter:
4630 return after_in_filter(umap, node);
4631 case isl_schedule_node_guard:
4632 case isl_schedule_node_mark:
4633 return after_in_child(umap, node);
4634 case isl_schedule_node_set:
4635 return after_in_set(umap, node);
4636 case isl_schedule_node_sequence:
4637 return after_in_sequence(umap, node);
4640 return isl_bool_true;
4643 /* Is any domain element of "map1" scheduled after any domain
4644 * element of "map2" by the subtree underneath the current band node,
4645 * while at the same time being scheduled together by the current
4646 * band node, i.e., by "map1" and "map2?
4648 * If the child of the current band node is a leaf, then
4649 * no element can be scheduled after any other element.
4651 * Otherwise, we construct a relation between domain elements
4652 * of "map1" and domain elements of "map2" that are scheduled
4653 * together and then check if the subtree underneath the current
4654 * band node determines their relative order.
4656 static isl_bool after_in_subtree(__isl_keep isl_ast_build *build,
4657 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4659 isl_schedule_node *node;
4660 isl_map *map;
4661 isl_union_map *umap;
4662 isl_bool after;
4664 node = isl_ast_build_get_schedule_node(build);
4665 if (!node)
4666 return isl_bool_error;
4667 node = isl_schedule_node_child(node, 0);
4668 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4669 isl_schedule_node_free(node);
4670 return isl_bool_false;
4672 map = isl_map_copy(map2);
4673 map = isl_map_apply_domain(map, isl_map_copy(map1));
4674 umap = isl_union_map_from_map(map);
4675 after = after_in_tree(umap, node);
4676 isl_union_map_free(umap);
4677 isl_schedule_node_free(node);
4678 return after;
4681 /* Internal data for any_scheduled_after.
4683 * "build" is the build in which the AST is constructed.
4684 * "depth" is the number of loops that have already been generated
4685 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4686 * "domain" is an array of set-map pairs corresponding to the different
4687 * iteration domains. The set is the schedule domain, i.e., the domain
4688 * of the inverse schedule, while the map is the inverse schedule itself.
4690 struct isl_any_scheduled_after_data {
4691 isl_ast_build *build;
4692 int depth;
4693 int group_coscheduled;
4694 struct isl_set_map_pair *domain;
4697 /* Is any element of domain "i" scheduled after any element of domain "j"
4698 * (for a common iteration of the first data->depth loops)?
4700 * data->domain[i].set contains the domain of the inverse schedule
4701 * for domain "i", i.e., elements in the schedule domain.
4703 * If we are inside a band of a schedule tree and there is a pair
4704 * of elements in the two domains that is schedule together by
4705 * the current band, then we check if any element of "i" may be schedule
4706 * after element of "j" by the descendants of the band node.
4708 * If data->group_coscheduled is set, then we also return 1 if there
4709 * is any pair of elements in the two domains that are scheduled together.
4711 static isl_bool any_scheduled_after(int i, int j, void *user)
4713 struct isl_any_scheduled_after_data *data = user;
4714 isl_size dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4715 int pos;
4717 if (dim < 0)
4718 return isl_bool_error;
4720 for (pos = data->depth; pos < dim; ++pos) {
4721 int follows;
4723 follows = isl_set_follows_at(data->domain[i].set,
4724 data->domain[j].set, pos);
4726 if (follows < -1)
4727 return isl_bool_error;
4728 if (follows > 0)
4729 return isl_bool_true;
4730 if (follows < 0)
4731 return isl_bool_false;
4734 if (isl_ast_build_has_schedule_node(data->build)) {
4735 isl_bool after;
4737 after = after_in_subtree(data->build, data->domain[i].map,
4738 data->domain[j].map);
4739 if (after < 0 || after)
4740 return after;
4743 return isl_bool_ok(data->group_coscheduled);
4746 /* Look for independent components at the current depth and generate code
4747 * for each component separately. The resulting lists of grafts are
4748 * merged in an attempt to combine grafts with identical guards.
4750 * Code for two domains can be generated separately if all the elements
4751 * of one domain are scheduled before (or together with) all the elements
4752 * of the other domain. We therefore consider the graph with as nodes
4753 * the domains and an edge between two nodes if any element of the first
4754 * node is scheduled after any element of the second node.
4755 * If the ast_build_group_coscheduled is set, then we also add an edge if
4756 * there is any pair of elements in the two domains that are scheduled
4757 * together.
4758 * Code is then generated (by generate_component)
4759 * for each of the strongly connected components in this graph
4760 * in their topological order.
4762 * Since the test is performed on the domain of the inverse schedules of
4763 * the different domains, we precompute these domains and store
4764 * them in data.domain.
4766 static __isl_give isl_ast_graft_list *generate_components(
4767 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4769 int i;
4770 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4771 isl_size n = isl_union_map_n_map(executed);
4772 struct isl_any_scheduled_after_data data;
4773 struct isl_set_map_pair *next;
4774 struct isl_tarjan_graph *g = NULL;
4775 isl_ast_graft_list *list = NULL;
4776 int n_domain = 0;
4778 data.domain = NULL;
4779 if (n < 0)
4780 goto error;
4781 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4782 if (!data.domain)
4783 goto error;
4784 n_domain = n;
4786 next = data.domain;
4787 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4788 goto error;
4790 if (!build)
4791 goto error;
4792 data.build = build;
4793 data.depth = isl_ast_build_get_depth(build);
4794 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4795 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4796 if (!g)
4797 goto error;
4799 list = isl_ast_graft_list_alloc(ctx, 0);
4801 i = 0;
4802 while (list && n) {
4803 isl_ast_graft_list *list_c;
4804 int first = i;
4806 if (g->order[i] == -1)
4807 isl_die(ctx, isl_error_internal, "cannot happen",
4808 goto error);
4809 ++i; --n;
4810 while (g->order[i] != -1) {
4811 ++i; --n;
4814 list_c = generate_component(data.domain,
4815 g->order + first, i - first,
4816 isl_ast_build_copy(build));
4817 list = isl_ast_graft_list_merge(list, list_c, build);
4819 ++i;
4822 if (0)
4823 error: list = isl_ast_graft_list_free(list);
4824 isl_tarjan_graph_free(g);
4825 for (i = 0; i < n_domain; ++i) {
4826 isl_map_free(data.domain[i].map);
4827 isl_set_free(data.domain[i].set);
4829 free(data.domain);
4830 isl_union_map_free(executed);
4831 isl_ast_build_free(build);
4833 return list;
4836 /* Generate code for the next level (and all inner levels).
4838 * If "executed" is empty, i.e., no code needs to be generated,
4839 * then we return an empty list.
4841 * If we have already generated code for all loop levels, then we pass
4842 * control to generate_inner_level.
4844 * If "executed" lives in a single space, i.e., if code needs to be
4845 * generated for a single domain, then there can only be a single
4846 * component and we go directly to generate_shifted_component.
4847 * Otherwise, we call generate_components to detect the components
4848 * and to call generate_component on each of them separately.
4850 static __isl_give isl_ast_graft_list *generate_next_level(
4851 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4853 int depth;
4854 isl_size dim;
4855 isl_size n;
4857 if (!build || !executed)
4858 goto error;
4860 if (isl_union_map_is_empty(executed)) {
4861 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4862 isl_union_map_free(executed);
4863 isl_ast_build_free(build);
4864 return isl_ast_graft_list_alloc(ctx, 0);
4867 depth = isl_ast_build_get_depth(build);
4868 dim = isl_ast_build_dim(build, isl_dim_set);
4869 if (dim < 0)
4870 goto error;
4871 if (depth >= dim)
4872 return generate_inner_level(executed, build);
4874 n = isl_union_map_n_map(executed);
4875 if (n < 0)
4876 goto error;
4877 if (n == 1)
4878 return generate_shifted_component(executed, build);
4880 return generate_components(executed, build);
4881 error:
4882 isl_union_map_free(executed);
4883 isl_ast_build_free(build);
4884 return NULL;
4887 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4888 * internal, executed and build are the inputs to generate_code.
4889 * list collects the output.
4891 struct isl_generate_code_data {
4892 int internal;
4893 isl_union_map *executed;
4894 isl_ast_build *build;
4896 isl_ast_graft_list *list;
4899 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4901 * [E -> S] -> D
4903 * with E the external build schedule and S the additional schedule "space",
4904 * reformulate the inverse schedule in terms of the internal schedule domain,
4905 * i.e., return
4907 * [I -> S] -> D
4909 * We first obtain a mapping
4911 * I -> E
4913 * take the inverse and the product with S -> S, resulting in
4915 * [I -> S] -> [E -> S]
4917 * Applying the map to the input produces the desired result.
4919 static __isl_give isl_union_map *internal_executed(
4920 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4921 __isl_keep isl_ast_build *build)
4923 isl_map *id, *proj;
4925 proj = isl_ast_build_get_schedule_map(build);
4926 proj = isl_map_reverse(proj);
4927 space = isl_space_map_from_set(isl_space_copy(space));
4928 id = isl_map_identity(space);
4929 proj = isl_map_product(proj, id);
4930 executed = isl_union_map_apply_domain(executed,
4931 isl_union_map_from_map(proj));
4932 return executed;
4935 /* Generate an AST that visits the elements in the range of data->executed
4936 * in the relative order specified by the corresponding domain element(s)
4937 * for those domain elements that belong to "set".
4938 * Add the result to data->list.
4940 * The caller ensures that "set" is a universe domain.
4941 * "space" is the space of the additional part of the schedule.
4942 * It is equal to the space of "set" if build->domain is parametric.
4943 * Otherwise, it is equal to the range of the wrapped space of "set".
4945 * If the build space is not parametric and
4946 * if isl_ast_build_node_from_schedule_map
4947 * was called from an outside user (data->internal not set), then
4948 * the (inverse) schedule refers to the external build domain and needs to
4949 * be transformed to refer to the internal build domain.
4951 * If the build space is parametric, then we add some of the parameter
4952 * constraints to the executed relation. Adding these constraints
4953 * allows for an earlier detection of conflicts in some cases.
4954 * However, we do not want to divide the executed relation into
4955 * more disjuncts than necessary. We therefore approximate
4956 * the constraints on the parameters by a single disjunct set.
4958 * The build is extended to include the additional part of the schedule.
4959 * If the original build space was not parametric, then the options
4960 * in data->build refer only to the additional part of the schedule
4961 * and they need to be adjusted to refer to the complete AST build
4962 * domain.
4964 * After having adjusted inverse schedule and build, we start generating
4965 * code with the outer loop of the current code generation
4966 * in generate_next_level.
4968 * If the original build space was not parametric, we undo the embedding
4969 * on the resulting isl_ast_node_list so that it can be used within
4970 * the outer AST build.
4972 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4973 __isl_take isl_set *set, __isl_take isl_space *space)
4975 isl_union_map *executed;
4976 isl_ast_build *build;
4977 isl_ast_graft_list *list;
4978 int embed;
4980 executed = isl_union_map_copy(data->executed);
4981 executed = isl_union_map_intersect_domain(executed,
4982 isl_union_set_from_set(set));
4984 embed = !isl_set_is_params(data->build->domain);
4985 if (embed && !data->internal)
4986 executed = internal_executed(executed, space, data->build);
4987 if (!embed) {
4988 isl_set *domain;
4989 domain = isl_ast_build_get_domain(data->build);
4990 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4991 executed = isl_union_map_intersect_params(executed, domain);
4994 build = isl_ast_build_copy(data->build);
4995 build = isl_ast_build_product(build, space);
4997 list = generate_next_level(executed, build);
4999 list = isl_ast_graft_list_unembed(list, embed);
5001 data->list = isl_ast_graft_list_concat(data->list, list);
5003 return isl_stat_ok;
5006 /* Generate an AST that visits the elements in the range of data->executed
5007 * in the relative order specified by the corresponding domain element(s)
5008 * for those domain elements that belong to "set".
5009 * Add the result to data->list.
5011 * The caller ensures that "set" is a universe domain.
5013 * If the build space S is not parametric, then the space of "set"
5014 * need to be a wrapped relation with S as domain. That is, it needs
5015 * to be of the form
5017 * [S -> T]
5019 * Check this property and pass control to generate_code_in_space
5020 * passing along T.
5021 * If the build space is not parametric, then T is the space of "set".
5023 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
5025 struct isl_generate_code_data *data = user;
5026 isl_space *space, *build_space;
5027 int is_domain;
5029 space = isl_set_get_space(set);
5031 if (isl_set_is_params(data->build->domain))
5032 return generate_code_in_space(data, set, space);
5034 build_space = isl_ast_build_get_space(data->build, data->internal);
5035 space = isl_space_unwrap(space);
5036 is_domain = isl_space_is_domain(build_space, space);
5037 isl_space_free(build_space);
5038 space = isl_space_range(space);
5040 if (is_domain < 0)
5041 goto error;
5042 if (!is_domain)
5043 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5044 "invalid nested schedule space", goto error);
5046 return generate_code_in_space(data, set, space);
5047 error:
5048 isl_set_free(set);
5049 isl_space_free(space);
5050 return isl_stat_error;
5053 /* Generate an AST that visits the elements in the range of "executed"
5054 * in the relative order specified by the corresponding domain element(s).
5056 * "build" is an isl_ast_build that has either been constructed by
5057 * isl_ast_build_from_context or passed to a callback set by
5058 * isl_ast_build_set_create_leaf.
5059 * In the first case, the space of the isl_ast_build is typically
5060 * a parametric space, although this is currently not enforced.
5061 * In the second case, the space is never a parametric space.
5062 * If the space S is not parametric, then the domain space(s) of "executed"
5063 * need to be wrapped relations with S as domain.
5065 * If the domain of "executed" consists of several spaces, then an AST
5066 * is generated for each of them (in arbitrary order) and the results
5067 * are concatenated.
5069 * If "internal" is set, then the domain "S" above refers to the internal
5070 * schedule domain representation. Otherwise, it refers to the external
5071 * representation, as returned by isl_ast_build_get_schedule_space.
5073 * We essentially run over all the spaces in the domain of "executed"
5074 * and call generate_code_set on each of them.
5076 static __isl_give isl_ast_graft_list *generate_code(
5077 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
5078 int internal)
5080 isl_ctx *ctx;
5081 struct isl_generate_code_data data = { 0 };
5082 isl_space *space;
5083 isl_union_set *schedule_domain;
5084 isl_union_map *universe;
5086 if (!build)
5087 goto error;
5088 space = isl_ast_build_get_space(build, 1);
5089 space = isl_space_align_params(space,
5090 isl_union_map_get_space(executed));
5091 space = isl_space_align_params(space,
5092 isl_union_map_get_space(build->options));
5093 build = isl_ast_build_align_params(build, isl_space_copy(space));
5094 executed = isl_union_map_align_params(executed, space);
5095 if (!executed || !build)
5096 goto error;
5098 ctx = isl_ast_build_get_ctx(build);
5100 data.internal = internal;
5101 data.executed = executed;
5102 data.build = build;
5103 data.list = isl_ast_graft_list_alloc(ctx, 0);
5105 universe = isl_union_map_universe(isl_union_map_copy(executed));
5106 schedule_domain = isl_union_map_domain(universe);
5107 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5108 &data) < 0)
5109 data.list = isl_ast_graft_list_free(data.list);
5111 isl_union_set_free(schedule_domain);
5112 isl_union_map_free(executed);
5114 isl_ast_build_free(build);
5115 return data.list;
5116 error:
5117 isl_union_map_free(executed);
5118 isl_ast_build_free(build);
5119 return NULL;
5122 /* Generate an AST that visits the elements in the domain of "schedule"
5123 * in the relative order specified by the corresponding image element(s).
5125 * "build" is an isl_ast_build that has either been constructed by
5126 * isl_ast_build_from_context or passed to a callback set by
5127 * isl_ast_build_set_create_leaf.
5128 * In the first case, the space of the isl_ast_build is typically
5129 * a parametric space, although this is currently not enforced.
5130 * In the second case, the space is never a parametric space.
5131 * If the space S is not parametric, then the range space(s) of "schedule"
5132 * need to be wrapped relations with S as domain.
5134 * If the range of "schedule" consists of several spaces, then an AST
5135 * is generated for each of them (in arbitrary order) and the results
5136 * are concatenated.
5138 * We first initialize the local copies of the relevant options.
5139 * We do this here rather than when the isl_ast_build is created
5140 * because the options may have changed between the construction
5141 * of the isl_ast_build and the call to isl_generate_code.
5143 * The main computation is performed on an inverse schedule (with
5144 * the schedule domain in the domain and the elements to be executed
5145 * in the range) called "executed".
5147 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5148 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5150 isl_ast_graft_list *list;
5151 isl_ast_node *node;
5152 isl_union_map *executed;
5154 build = isl_ast_build_copy(build);
5155 build = isl_ast_build_set_single_valued(build, 0);
5156 schedule = isl_union_map_coalesce(schedule);
5157 schedule = isl_union_map_remove_redundancies(schedule);
5158 executed = isl_union_map_reverse(schedule);
5159 list = generate_code(executed, isl_ast_build_copy(build), 0);
5160 node = isl_ast_node_from_graft_list(list, build);
5161 isl_ast_build_free(build);
5163 return node;
5166 /* The old name for isl_ast_build_node_from_schedule_map.
5167 * It is being kept for backward compatibility, but
5168 * it will be removed in the future.
5170 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5171 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5173 return isl_ast_build_node_from_schedule_map(build, schedule);
5176 /* Generate an AST that visits the elements in the domain of "executed"
5177 * in the relative order specified by the leaf node "node".
5179 * The relation "executed" maps the outer generated loop iterators
5180 * to the domain elements executed by those iterations.
5182 * Simply pass control to generate_inner_level.
5183 * Note that the current build does not refer to any band node, so
5184 * that generate_inner_level will not try to visit the child of
5185 * the leaf node.
5187 * If multiple statement instances reach a leaf,
5188 * then they can be executed in any order.
5189 * Group the list of grafts based on shared guards
5190 * such that identical guards are only generated once
5191 * when the list is eventually passed on to isl_ast_graft_list_fuse.
5193 static __isl_give isl_ast_graft_list *build_ast_from_leaf(
5194 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5195 __isl_take isl_union_map *executed)
5197 isl_ast_graft_list *list;
5199 isl_schedule_node_free(node);
5200 list = generate_inner_level(executed, isl_ast_build_copy(build));
5201 list = isl_ast_graft_list_group_on_guard(list, build);
5202 isl_ast_build_free(build);
5204 return list;
5207 /* Generate an AST that visits the elements in the domain of "executed"
5208 * in the relative order specified by the band node "node" and its descendants.
5210 * The relation "executed" maps the outer generated loop iterators
5211 * to the domain elements executed by those iterations.
5213 * If the band is empty, we continue with its descendants.
5214 * Otherwise, we extend the build and the inverse schedule with
5215 * the additional space/partial schedule and continue generating
5216 * an AST in generate_next_level.
5217 * As soon as we have extended the inverse schedule with the additional
5218 * partial schedule, we look for equalities that may exists between
5219 * the old and the new part.
5221 static __isl_give isl_ast_graft_list *build_ast_from_band(
5222 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5223 __isl_take isl_union_map *executed)
5225 isl_space *space;
5226 isl_multi_union_pw_aff *extra;
5227 isl_union_map *extra_umap;
5228 isl_ast_graft_list *list;
5229 isl_size n1, n2;
5230 isl_size n;
5232 n = isl_schedule_node_band_n_member(node);
5233 if (!build || n < 0 || !executed)
5234 goto error;
5236 if (n == 0)
5237 return build_ast_from_child(build, node, executed);
5239 extra = isl_schedule_node_band_get_partial_schedule(node);
5240 extra = isl_multi_union_pw_aff_align_params(extra,
5241 isl_ast_build_get_space(build, 1));
5242 space = isl_multi_union_pw_aff_get_space(extra);
5244 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5245 extra_umap = isl_union_map_reverse(extra_umap);
5247 executed = isl_union_map_domain_product(executed, extra_umap);
5248 executed = isl_union_map_detect_equalities(executed);
5250 n1 = isl_ast_build_dim(build, isl_dim_param);
5251 build = isl_ast_build_product(build, space);
5252 n2 = isl_ast_build_dim(build, isl_dim_param);
5253 if (n1 < 0 || n2 < 0)
5254 build = isl_ast_build_free(build);
5255 else if (n2 > n1)
5256 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5257 "band node is not allowed to introduce new parameters",
5258 build = isl_ast_build_free(build));
5259 build = isl_ast_build_set_schedule_node(build, node);
5261 list = generate_next_level(executed, build);
5263 list = isl_ast_graft_list_unembed(list, 1);
5265 return list;
5266 error:
5267 isl_schedule_node_free(node);
5268 isl_union_map_free(executed);
5269 isl_ast_build_free(build);
5270 return NULL;
5273 /* Hoist a list of grafts (in practice containing a single graft)
5274 * from "sub_build" (which includes extra context information)
5275 * to "build".
5277 * In particular, project out all additional parameters introduced
5278 * by the context node from the enforced constraints and the guard
5279 * of the single graft.
5281 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5282 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5283 __isl_keep isl_ast_build *sub_build)
5285 isl_ast_graft *graft;
5286 isl_basic_set *enforced;
5287 isl_set *guard;
5288 isl_size n_param, extra_param;
5290 n_param = isl_ast_build_dim(build, isl_dim_param);
5291 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5292 if (n_param < 0 || extra_param < 0)
5293 return isl_ast_graft_list_free(list);
5295 if (extra_param == n_param)
5296 return list;
5298 extra_param -= n_param;
5299 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5300 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5301 n_param, extra_param);
5302 enforced = isl_basic_set_remove_unknown_divs(enforced);
5303 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5304 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5305 n_param, extra_param);
5306 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5307 guard = isl_set_compute_divs(guard);
5308 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5309 build, sub_build);
5310 list = isl_ast_graft_list_from_ast_graft(graft);
5312 return list;
5315 /* Generate an AST that visits the elements in the domain of "executed"
5316 * in the relative order specified by the context node "node"
5317 * and its descendants.
5319 * The relation "executed" maps the outer generated loop iterators
5320 * to the domain elements executed by those iterations.
5322 * The context node may introduce additional parameters as well as
5323 * constraints on the outer schedule dimensions or original parameters.
5325 * We add the extra parameters to a new build and the context
5326 * constraints to both the build and (as a single disjunct)
5327 * to the domain of "executed". Since the context constraints
5328 * are specified in terms of the input schedule, we first need
5329 * to map them to the internal schedule domain.
5331 * After constructing the AST from the descendants of "node",
5332 * we combine the list of grafts into a single graft within
5333 * the new build, in order to be able to exploit the additional
5334 * context constraints during this combination.
5336 * Additionally, if the current node is the outermost node in
5337 * the schedule tree (apart from the root domain node), we generate
5338 * all pending guards, again to be able to exploit the additional
5339 * context constraints. We currently do not do this for internal
5340 * context nodes since we may still want to hoist conditions
5341 * to outer AST nodes.
5343 * If the context node introduced any new parameters, then they
5344 * are removed from the set of enforced constraints and guard
5345 * in hoist_out_of_context.
5347 static __isl_give isl_ast_graft_list *build_ast_from_context(
5348 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5349 __isl_take isl_union_map *executed)
5351 isl_set *context;
5352 isl_space *space;
5353 isl_multi_aff *internal2input;
5354 isl_ast_build *sub_build;
5355 isl_ast_graft_list *list;
5356 isl_size n;
5357 isl_size depth;
5359 depth = isl_schedule_node_get_tree_depth(node);
5360 if (depth < 0)
5361 build = isl_ast_build_free(build);
5362 space = isl_ast_build_get_space(build, 1);
5363 context = isl_schedule_node_context_get_context(node);
5364 context = isl_set_align_params(context, space);
5365 sub_build = isl_ast_build_copy(build);
5366 space = isl_set_get_space(context);
5367 sub_build = isl_ast_build_align_params(sub_build, space);
5368 internal2input = isl_ast_build_get_internal2input(sub_build);
5369 context = isl_set_preimage_multi_aff(context, internal2input);
5370 sub_build = isl_ast_build_restrict_generated(sub_build,
5371 isl_set_copy(context));
5372 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5373 executed = isl_union_map_intersect_domain(executed,
5374 isl_union_set_from_set(context));
5376 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5377 node, executed);
5378 n = isl_ast_graft_list_n_ast_graft(list);
5379 if (n < 0)
5380 list = isl_ast_graft_list_free(list);
5382 list = isl_ast_graft_list_fuse(list, sub_build);
5383 if (depth == 1)
5384 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5385 sub_build);
5386 if (n >= 1)
5387 list = hoist_out_of_context(list, build, sub_build);
5389 isl_ast_build_free(build);
5390 isl_ast_build_free(sub_build);
5392 return list;
5395 /* Generate an AST that visits the elements in the domain of "executed"
5396 * in the relative order specified by the expansion node "node" and
5397 * its descendants.
5399 * The relation "executed" maps the outer generated loop iterators
5400 * to the domain elements executed by those iterations.
5402 * We expand the domain elements by the expansion and
5403 * continue with the descendants of the node.
5405 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5406 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5407 __isl_take isl_union_map *executed)
5409 isl_union_map *expansion;
5410 isl_size n1, n2;
5412 expansion = isl_schedule_node_expansion_get_expansion(node);
5413 expansion = isl_union_map_align_params(expansion,
5414 isl_union_map_get_space(executed));
5416 n1 = isl_union_map_dim(executed, isl_dim_param);
5417 executed = isl_union_map_apply_range(executed, expansion);
5418 n2 = isl_union_map_dim(executed, isl_dim_param);
5419 if (n1 < 0 || n2 < 0)
5420 goto error;
5421 if (n2 > n1)
5422 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5423 "expansion node is not allowed to introduce "
5424 "new parameters", goto error);
5426 return build_ast_from_child(build, node, executed);
5427 error:
5428 isl_ast_build_free(build);
5429 isl_schedule_node_free(node);
5430 isl_union_map_free(executed);
5431 return NULL;
5434 /* Generate an AST that visits the elements in the domain of "executed"
5435 * in the relative order specified by the extension node "node" and
5436 * its descendants.
5438 * The relation "executed" maps the outer generated loop iterators
5439 * to the domain elements executed by those iterations.
5441 * Extend the inverse schedule with the extension applied to current
5442 * set of generated constraints. Since the extension if formulated
5443 * in terms of the input schedule, it first needs to be transformed
5444 * to refer to the internal schedule.
5446 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5447 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5448 __isl_take isl_union_map *executed)
5450 isl_union_set *schedule_domain;
5451 isl_union_map *extension;
5452 isl_set *set;
5454 set = isl_ast_build_get_generated(build);
5455 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5456 schedule_domain = isl_union_set_from_set(set);
5458 extension = isl_schedule_node_extension_get_extension(node);
5460 extension = isl_union_map_preimage_domain_multi_aff(extension,
5461 isl_multi_aff_copy(build->internal2input));
5462 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5463 extension = isl_ast_build_substitute_values_union_map_domain(build,
5464 extension);
5465 executed = isl_union_map_union(executed, extension);
5467 return build_ast_from_child(build, node, executed);
5470 /* Generate an AST that visits the elements in the domain of "executed"
5471 * in the relative order specified by the filter node "node" and
5472 * its descendants.
5474 * The relation "executed" maps the outer generated loop iterators
5475 * to the domain elements executed by those iterations.
5477 * We simply intersect the iteration domain (i.e., the range of "executed")
5478 * with the filter and continue with the descendants of the node,
5479 * unless the resulting inverse schedule is empty, in which
5480 * case we return an empty list.
5482 * If the result of the intersection is equal to the original "executed"
5483 * relation, then keep the original representation since the intersection
5484 * may have unnecessarily broken up the relation into a greater number
5485 * of disjuncts.
5487 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5488 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5489 __isl_take isl_union_map *executed)
5491 isl_ctx *ctx;
5492 isl_union_set *filter;
5493 isl_union_map *orig;
5494 isl_ast_graft_list *list;
5495 int empty;
5496 isl_bool unchanged;
5497 isl_size n1, n2;
5499 orig = isl_union_map_copy(executed);
5500 if (!build || !node || !executed)
5501 goto error;
5503 filter = isl_schedule_node_filter_get_filter(node);
5504 filter = isl_union_set_align_params(filter,
5505 isl_union_map_get_space(executed));
5506 n1 = isl_union_map_dim(executed, isl_dim_param);
5507 executed = isl_union_map_intersect_range(executed, filter);
5508 n2 = isl_union_map_dim(executed, isl_dim_param);
5509 if (n1 < 0 || n2 < 0)
5510 goto error;
5511 if (n2 > n1)
5512 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5513 "filter node is not allowed to introduce "
5514 "new parameters", goto error);
5516 unchanged = isl_union_map_is_subset(orig, executed);
5517 empty = isl_union_map_is_empty(executed);
5518 if (unchanged < 0 || empty < 0)
5519 goto error;
5520 if (unchanged) {
5521 isl_union_map_free(executed);
5522 return build_ast_from_child(build, node, orig);
5524 isl_union_map_free(orig);
5525 if (!empty)
5526 return build_ast_from_child(build, node, executed);
5528 ctx = isl_ast_build_get_ctx(build);
5529 list = isl_ast_graft_list_alloc(ctx, 0);
5530 isl_ast_build_free(build);
5531 isl_schedule_node_free(node);
5532 isl_union_map_free(executed);
5533 return list;
5534 error:
5535 isl_ast_build_free(build);
5536 isl_schedule_node_free(node);
5537 isl_union_map_free(executed);
5538 isl_union_map_free(orig);
5539 return NULL;
5542 /* Generate an AST that visits the elements in the domain of "executed"
5543 * in the relative order specified by the guard node "node" and
5544 * its descendants.
5546 * The relation "executed" maps the outer generated loop iterators
5547 * to the domain elements executed by those iterations.
5549 * Ensure that the associated guard is enforced by the outer AST
5550 * constructs by adding it to the guard of the graft.
5551 * Since we know that we will enforce the guard, we can also include it
5552 * in the generated constraints used to construct an AST for
5553 * the descendant nodes.
5555 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5556 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5557 __isl_take isl_union_map *executed)
5559 isl_space *space;
5560 isl_set *guard, *hoisted;
5561 isl_basic_set *enforced;
5562 isl_ast_build *sub_build;
5563 isl_ast_graft *graft;
5564 isl_ast_graft_list *list;
5565 isl_size n1, n2, n;
5567 space = isl_ast_build_get_space(build, 1);
5568 guard = isl_schedule_node_guard_get_guard(node);
5569 n1 = isl_space_dim(space, isl_dim_param);
5570 guard = isl_set_align_params(guard, space);
5571 n2 = isl_set_dim(guard, isl_dim_param);
5572 if (n1 < 0 || n2 < 0)
5573 guard = isl_set_free(guard);
5574 else if (n2 > n1)
5575 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5576 "guard node is not allowed to introduce "
5577 "new parameters", guard = isl_set_free(guard));
5578 guard = isl_set_preimage_multi_aff(guard,
5579 isl_multi_aff_copy(build->internal2input));
5580 guard = isl_ast_build_specialize(build, guard);
5581 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5583 sub_build = isl_ast_build_copy(build);
5584 sub_build = isl_ast_build_restrict_generated(sub_build,
5585 isl_set_copy(guard));
5587 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5588 node, executed);
5590 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5591 n = isl_set_n_basic_set(hoisted);
5592 if (n < 0)
5593 list = isl_ast_graft_list_free(list);
5594 if (n > 1)
5595 list = isl_ast_graft_list_gist_guards(list,
5596 isl_set_copy(hoisted));
5597 guard = isl_set_intersect(guard, hoisted);
5598 enforced = extract_shared_enforced(list, build);
5599 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5600 build, sub_build);
5602 isl_ast_build_free(sub_build);
5603 isl_ast_build_free(build);
5604 return isl_ast_graft_list_from_ast_graft(graft);
5607 /* Call the before_each_mark callback, if requested by the user.
5609 * Return 0 on success and -1 on error.
5611 * The caller is responsible for recording the current inverse schedule
5612 * in "build".
5614 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5615 __isl_keep isl_ast_build *build)
5617 if (!build)
5618 return isl_stat_error;
5619 if (!build->before_each_mark)
5620 return isl_stat_ok;
5621 return build->before_each_mark(mark, build,
5622 build->before_each_mark_user);
5625 /* Call the after_each_mark callback, if requested by the user.
5627 * The caller is responsible for recording the current inverse schedule
5628 * in "build".
5630 static __isl_give isl_ast_graft *after_each_mark(
5631 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5633 if (!graft || !build)
5634 return isl_ast_graft_free(graft);
5635 if (!build->after_each_mark)
5636 return graft;
5637 graft->node = build->after_each_mark(graft->node, build,
5638 build->after_each_mark_user);
5639 if (!graft->node)
5640 return isl_ast_graft_free(graft);
5641 return graft;
5645 /* Generate an AST that visits the elements in the domain of "executed"
5646 * in the relative order specified by the mark node "node" and
5647 * its descendants.
5649 * The relation "executed" maps the outer generated loop iterators
5650 * to the domain elements executed by those iterations.
5652 * Since we may be calling before_each_mark and after_each_mark
5653 * callbacks, we record the current inverse schedule in the build.
5655 * We generate an AST for the child of the mark node, combine
5656 * the graft list into a single graft and then insert the mark
5657 * in the AST of that single graft.
5659 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5660 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5661 __isl_take isl_union_map *executed)
5663 isl_id *mark;
5664 isl_ast_graft *graft;
5665 isl_ast_graft_list *list;
5666 isl_size n;
5668 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5670 mark = isl_schedule_node_mark_get_id(node);
5671 if (before_each_mark(mark, build) < 0)
5672 node = isl_schedule_node_free(node);
5674 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5675 list = isl_ast_graft_list_fuse(list, build);
5676 n = isl_ast_graft_list_n_ast_graft(list);
5677 if (n < 0)
5678 list = isl_ast_graft_list_free(list);
5679 if (n == 0) {
5680 isl_id_free(mark);
5681 } else {
5682 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5683 graft = isl_ast_graft_insert_mark(graft, mark);
5684 graft = after_each_mark(graft, build);
5685 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5687 isl_ast_build_free(build);
5689 return list;
5692 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5693 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5694 __isl_take isl_union_map *executed);
5696 /* Generate an AST that visits the elements in the domain of "executed"
5697 * in the relative order specified by the sequence (or set) node "node" and
5698 * its descendants.
5700 * The relation "executed" maps the outer generated loop iterators
5701 * to the domain elements executed by those iterations.
5703 * We simply generate an AST for each of the children and concatenate
5704 * the results.
5706 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5707 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5708 __isl_take isl_union_map *executed)
5710 int i;
5711 isl_size n;
5712 isl_ctx *ctx;
5713 isl_ast_graft_list *list;
5715 ctx = isl_ast_build_get_ctx(build);
5716 list = isl_ast_graft_list_alloc(ctx, 0);
5718 n = isl_schedule_node_n_children(node);
5719 if (n < 0)
5720 list = isl_ast_graft_list_free(list);
5721 for (i = 0; i < n; ++i) {
5722 isl_schedule_node *child;
5723 isl_ast_graft_list *list_i;
5725 child = isl_schedule_node_get_child(node, i);
5726 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5727 child, isl_union_map_copy(executed));
5728 list = isl_ast_graft_list_concat(list, list_i);
5730 isl_ast_build_free(build);
5731 isl_schedule_node_free(node);
5732 isl_union_map_free(executed);
5734 return list;
5737 /* Generate an AST that visits the elements in the domain of "executed"
5738 * in the relative order specified by the node "node" and its descendants.
5740 * The relation "executed" maps the outer generated loop iterators
5741 * to the domain elements executed by those iterations.
5743 * The node types are handled in separate functions.
5744 * Set nodes are currently treated in the same way as sequence nodes.
5745 * The children of a set node may be executed in any order,
5746 * including the order of the children.
5748 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5749 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5750 __isl_take isl_union_map *executed)
5752 enum isl_schedule_node_type type;
5754 type = isl_schedule_node_get_type(node);
5756 switch (type) {
5757 case isl_schedule_node_error:
5758 goto error;
5759 case isl_schedule_node_leaf:
5760 return build_ast_from_leaf(build, node, executed);
5761 case isl_schedule_node_band:
5762 return build_ast_from_band(build, node, executed);
5763 case isl_schedule_node_context:
5764 return build_ast_from_context(build, node, executed);
5765 case isl_schedule_node_domain:
5766 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5767 "unexpected internal domain node", goto error);
5768 case isl_schedule_node_expansion:
5769 return build_ast_from_expansion(build, node, executed);
5770 case isl_schedule_node_extension:
5771 return build_ast_from_extension(build, node, executed);
5772 case isl_schedule_node_filter:
5773 return build_ast_from_filter(build, node, executed);
5774 case isl_schedule_node_guard:
5775 return build_ast_from_guard(build, node, executed);
5776 case isl_schedule_node_mark:
5777 return build_ast_from_mark(build, node, executed);
5778 case isl_schedule_node_sequence:
5779 case isl_schedule_node_set:
5780 return build_ast_from_sequence(build, node, executed);
5783 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5784 "unhandled type", goto error);
5785 error:
5786 isl_union_map_free(executed);
5787 isl_schedule_node_free(node);
5788 isl_ast_build_free(build);
5790 return NULL;
5793 /* Generate an AST that visits the elements in the domain of "executed"
5794 * in the relative order specified by the (single) child of "node" and
5795 * its descendants.
5797 * The relation "executed" maps the outer generated loop iterators
5798 * to the domain elements executed by those iterations.
5800 * This function is never called on a leaf, set or sequence node,
5801 * so the node always has exactly one child.
5803 static __isl_give isl_ast_graft_list *build_ast_from_child(
5804 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5805 __isl_take isl_union_map *executed)
5807 node = isl_schedule_node_child(node, 0);
5808 return build_ast_from_schedule_node(build, node, executed);
5811 /* Generate an AST that visits the elements in the domain of the domain
5812 * node "node" in the relative order specified by its descendants.
5814 * An initial inverse schedule is created that maps a zero-dimensional
5815 * schedule space to the node domain.
5816 * The input "build" is assumed to have a parametric domain and
5817 * is replaced by the same zero-dimensional schedule space.
5819 * We also add some of the parameter constraints in the build domain
5820 * to the executed relation. Adding these constraints
5821 * allows for an earlier detection of conflicts in some cases.
5822 * However, we do not want to divide the executed relation into
5823 * more disjuncts than necessary. We therefore approximate
5824 * the constraints on the parameters by a single disjunct set.
5826 static __isl_give isl_ast_node *build_ast_from_domain(
5827 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5829 isl_ctx *ctx;
5830 isl_union_set *domain, *schedule_domain;
5831 isl_union_map *executed;
5832 isl_space *space;
5833 isl_set *set;
5834 isl_ast_graft_list *list;
5835 isl_ast_node *ast;
5836 int is_params;
5838 if (!build)
5839 goto error;
5841 ctx = isl_ast_build_get_ctx(build);
5842 space = isl_ast_build_get_space(build, 1);
5843 is_params = isl_space_is_params(space);
5844 isl_space_free(space);
5845 if (is_params < 0)
5846 goto error;
5847 if (!is_params)
5848 isl_die(ctx, isl_error_unsupported,
5849 "expecting parametric initial context", goto error);
5851 domain = isl_schedule_node_domain_get_domain(node);
5852 domain = isl_union_set_coalesce(domain);
5854 space = isl_union_set_get_space(domain);
5855 space = isl_space_set_from_params(space);
5856 build = isl_ast_build_product(build, space);
5858 set = isl_ast_build_get_domain(build);
5859 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5860 schedule_domain = isl_union_set_from_set(set);
5862 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5863 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5864 ast = isl_ast_node_from_graft_list(list, build);
5865 isl_ast_build_free(build);
5867 return ast;
5868 error:
5869 isl_schedule_node_free(node);
5870 isl_ast_build_free(build);
5871 return NULL;
5874 /* Generate an AST that visits the elements in the domain of "schedule"
5875 * in the relative order specified by the schedule tree.
5877 * "build" is an isl_ast_build that has been created using
5878 * isl_ast_build_alloc or isl_ast_build_from_context based
5879 * on a parametric set.
5881 * The construction starts at the root node of the schedule,
5882 * which is assumed to be a domain node.
5884 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5885 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5887 isl_ctx *ctx;
5888 isl_schedule_node *node;
5890 if (!build || !schedule)
5891 goto error;
5893 ctx = isl_ast_build_get_ctx(build);
5895 node = isl_schedule_get_root(schedule);
5896 if (!node)
5897 goto error;
5898 isl_schedule_free(schedule);
5900 build = isl_ast_build_copy(build);
5901 build = isl_ast_build_set_single_valued(build, 0);
5902 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5903 isl_die(ctx, isl_error_unsupported,
5904 "expecting root domain node",
5905 build = isl_ast_build_free(build));
5906 return build_ast_from_domain(build, node);
5907 error:
5908 isl_schedule_free(schedule);
5909 return NULL;