isl_test.c: before_for: explicitly check that depth has not become negative
[isl.git] / isl_ast_codegen.c
blob4215484725cc80e2feede18692dbec095cee38ca
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_bool aff_constant_is_negative(__isl_keep isl_set *set,
935 __isl_keep isl_aff *aff, void *user)
937 isl_bool is_neg;
938 isl_val *v;
940 v = isl_aff_get_constant_val(aff);
941 is_neg = isl_val_is_neg(v);
942 isl_val_free(v);
944 return is_neg;
947 /* Does "pa" have a negative constant term over its entire domain?
949 static isl_bool pw_aff_constant_is_negative(__isl_keep isl_pw_aff *pa,
950 void *user)
952 return isl_pw_aff_every_piece(pa, &aff_constant_is_negative, NULL);
955 /* Does each element in "list" have a negative constant term?
957 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
959 return isl_pw_aff_list_every(list, &pw_aff_constant_is_negative, NULL);
962 /* Add 1 to each of the elements in "list", where each of these elements
963 * is defined over the internal schedule space of "build".
965 static __isl_give isl_pw_aff_list *list_add_one(
966 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
968 int i;
969 isl_size n;
970 isl_space *space;
971 isl_aff *aff;
972 isl_pw_aff *one;
974 n = isl_pw_aff_list_n_pw_aff(list);
975 if (n < 0)
976 return isl_pw_aff_list_free(list);
978 space = isl_ast_build_get_space(build, 1);
979 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
980 aff = isl_aff_add_constant_si(aff, 1);
981 one = isl_pw_aff_from_aff(aff);
983 for (i = 0; i < n; ++i) {
984 isl_pw_aff *pa;
985 pa = isl_pw_aff_list_get_pw_aff(list, i);
986 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
987 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
990 isl_pw_aff_free(one);
992 return list;
995 /* Set the condition part of the for node graft->node in case
996 * the upper bound is represented as a list of piecewise affine expressions.
998 * In particular, set the condition to
1000 * iterator <= min(list of upper bounds)
1002 * If each of the upper bounds has a negative constant term, then
1003 * set the condition to
1005 * iterator < min(list of (upper bound + 1)s)
1008 static __isl_give isl_ast_graft *set_for_cond_from_list(
1009 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
1010 __isl_keep isl_ast_build *build)
1012 int neg;
1013 isl_ast_expr *bound, *iterator, *cond;
1014 enum isl_ast_expr_op_type type = isl_ast_expr_op_le;
1016 if (!graft || !list)
1017 return isl_ast_graft_free(graft);
1019 neg = list_constant_is_negative(list);
1020 if (neg < 0)
1021 return isl_ast_graft_free(graft);
1022 list = isl_pw_aff_list_copy(list);
1023 if (neg) {
1024 list = list_add_one(list, build);
1025 type = isl_ast_expr_op_lt;
1028 bound = reduce_list(isl_ast_expr_op_min, list, build);
1029 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1030 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1031 graft->node->u.f.cond = cond;
1033 isl_pw_aff_list_free(list);
1034 if (!graft->node->u.f.cond)
1035 return isl_ast_graft_free(graft);
1036 return graft;
1039 /* Set the condition part of the for node graft->node in case
1040 * the upper bound is represented as a set.
1042 static __isl_give isl_ast_graft *set_for_cond_from_set(
1043 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1044 __isl_keep isl_ast_build *build)
1046 isl_ast_expr *cond;
1048 if (!graft)
1049 return NULL;
1051 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1052 graft->node->u.f.cond = cond;
1053 if (!graft->node->u.f.cond)
1054 return isl_ast_graft_free(graft);
1055 return graft;
1058 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1059 * the current dimension.
1061 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1063 int depth;
1064 isl_val *v;
1065 isl_ctx *ctx;
1067 if (!build)
1068 return NULL;
1069 ctx = isl_ast_build_get_ctx(build);
1070 depth = isl_ast_build_get_depth(build);
1072 if (!isl_ast_build_has_stride(build, depth))
1073 return isl_ast_expr_alloc_int_si(ctx, 1);
1075 v = isl_ast_build_get_stride(build, depth);
1076 return isl_ast_expr_from_val(v);
1079 /* Should we express the loop condition as
1081 * iterator <= min(list of upper bounds)
1083 * or as a conjunction of constraints?
1085 * The first is constructed from a list of upper bounds.
1086 * The second is constructed from a set.
1088 * If there are no upper bounds in "constraints", then this could mean
1089 * that "domain" simply doesn't have an upper bound or that we didn't
1090 * pick any upper bound. In the first case, we want to generate the
1091 * loop condition as a(n empty) conjunction of constraints
1092 * In the second case, we will compute
1093 * a single upper bound from "domain" and so we use the list form.
1095 * If there are upper bounds in "constraints",
1096 * then we use the list form iff the atomic_upper_bound option is set.
1098 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1099 __isl_keep isl_set *domain, int depth)
1101 if (n_upper > 0)
1102 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1103 else
1104 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1107 /* Fill in the expressions of the for node in graft->node.
1109 * In particular,
1110 * - set the initialization part of the loop to the maximum of the lower bounds
1111 * - extract the increment from the stride of the current dimension
1112 * - construct the for condition either based on a list of upper bounds
1113 * or on a set of upper bound constraints.
1115 static __isl_give isl_ast_graft *set_for_node_expressions(
1116 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1117 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1118 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1120 isl_ast_node *node;
1122 if (!graft)
1123 return NULL;
1125 build = isl_ast_build_copy(build);
1127 node = graft->node;
1128 node->u.f.init = reduce_list(isl_ast_expr_op_max, lower, build);
1129 node->u.f.inc = for_inc(build);
1131 if (!node->u.f.init || !node->u.f.inc)
1132 graft = isl_ast_graft_free(graft);
1134 if (use_list)
1135 graft = set_for_cond_from_list(graft, upper_list, build);
1136 else
1137 graft = set_for_cond_from_set(graft, upper_set, build);
1139 isl_ast_build_free(build);
1141 return graft;
1144 /* Update "graft" based on "bounds" and "domain" for the generic,
1145 * non-degenerate, case.
1147 * "c_lower" and "c_upper" contain the lower and upper bounds
1148 * that the loop node should express.
1149 * "domain" is the subset of the intersection of the constraints
1150 * for which some code is executed.
1152 * There may be zero lower bounds or zero upper bounds in "constraints"
1153 * in case the list of constraints was created
1154 * based on the atomic option or based on separation with explicit bounds.
1155 * In that case, we use "domain" to derive lower and/or upper bounds.
1157 * We first compute a list of one or more lower bounds.
1159 * Then we decide if we want to express the condition as
1161 * iterator <= min(list of upper bounds)
1163 * or as a conjunction of constraints.
1165 * The set of enforced constraints is then computed either based on
1166 * a list of upper bounds or on a set of upper bound constraints.
1167 * We do not compute any enforced constraints if we were forced
1168 * to compute a lower or upper bound using exact_bound. The domains
1169 * of the resulting expressions may imply some bounds on outer dimensions
1170 * that we do not want to appear in the enforced constraints since
1171 * they are not actually enforced by the corresponding code.
1173 * Finally, we fill in the expressions of the for node.
1175 static __isl_give isl_ast_graft *refine_generic_bounds(
1176 __isl_take isl_ast_graft *graft,
1177 __isl_take isl_constraint_list *c_lower,
1178 __isl_take isl_constraint_list *c_upper,
1179 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1181 int depth;
1182 isl_ctx *ctx;
1183 isl_pw_aff_list *lower;
1184 int use_list;
1185 isl_set *upper_set = NULL;
1186 isl_pw_aff_list *upper_list = NULL;
1187 isl_size n_lower, n_upper;
1189 if (!graft || !c_lower || !c_upper || !build)
1190 goto error;
1192 depth = isl_ast_build_get_depth(build);
1193 ctx = isl_ast_graft_get_ctx(graft);
1195 n_lower = isl_constraint_list_n_constraint(c_lower);
1196 n_upper = isl_constraint_list_n_constraint(c_upper);
1197 if (n_lower < 0 || n_upper < 0)
1198 goto error;
1200 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1202 lower = lower_bounds(c_lower, depth, domain, build);
1204 if (use_list)
1205 upper_list = upper_bounds(c_upper, depth, domain, build);
1206 else if (n_upper > 0)
1207 upper_set = intersect_constraints(c_upper);
1208 else
1209 upper_set = isl_set_universe(isl_set_get_space(domain));
1211 if (n_lower == 0 || n_upper == 0)
1213 else if (use_list)
1214 graft = set_enforced_from_list(graft, lower, upper_list);
1215 else
1216 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1218 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1219 upper_set, build);
1221 isl_pw_aff_list_free(lower);
1222 isl_pw_aff_list_free(upper_list);
1223 isl_set_free(upper_set);
1224 isl_constraint_list_free(c_lower);
1225 isl_constraint_list_free(c_upper);
1227 return graft;
1228 error:
1229 isl_constraint_list_free(c_lower);
1230 isl_constraint_list_free(c_upper);
1231 return isl_ast_graft_free(graft);
1234 /* Internal data structure used inside count_constraints to keep
1235 * track of the number of constraints that are independent of dimension "pos",
1236 * the lower bounds in "pos" and the upper bounds in "pos".
1238 struct isl_ast_count_constraints_data {
1239 int pos;
1241 int n_indep;
1242 int n_lower;
1243 int n_upper;
1246 /* Increment data->n_indep, data->lower or data->upper depending
1247 * on whether "c" is independenct of dimensions data->pos,
1248 * a lower bound or an upper bound.
1250 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1252 struct isl_ast_count_constraints_data *data = user;
1254 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1255 data->n_lower++;
1256 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1257 data->n_upper++;
1258 else
1259 data->n_indep++;
1261 isl_constraint_free(c);
1263 return isl_stat_ok;
1266 /* Update "graft" based on "bounds" and "domain" for the generic,
1267 * non-degenerate, case.
1269 * "list" respresent the list of bounds that need to be encoded by
1270 * the for loop. Only the constraints that involve the iterator
1271 * are relevant here. The other constraints are taken care of by
1272 * the caller and are included in the generated constraints of "build".
1273 * "domain" is the subset of the intersection of the constraints
1274 * for which some code is executed.
1275 * "build" is the build in which graft->node was created.
1277 * We separate lower bounds, upper bounds and constraints that
1278 * are independent of the loop iterator.
1280 * The actual for loop bounds are generated in refine_generic_bounds.
1282 static __isl_give isl_ast_graft *refine_generic_split(
1283 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1284 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1286 struct isl_ast_count_constraints_data data;
1287 isl_constraint_list *lower;
1288 isl_constraint_list *upper;
1290 if (!list)
1291 return isl_ast_graft_free(graft);
1293 data.pos = isl_ast_build_get_depth(build);
1295 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1296 if (!list)
1297 return isl_ast_graft_free(graft);
1299 data.n_indep = data.n_lower = data.n_upper = 0;
1300 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1301 isl_constraint_list_free(list);
1302 return isl_ast_graft_free(graft);
1305 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1306 upper = isl_constraint_list_copy(lower);
1307 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1308 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1310 return refine_generic_bounds(graft, lower, upper, domain, build);
1313 /* Update "graft" based on "bounds" and "domain" for the generic,
1314 * non-degenerate, case.
1316 * "bounds" respresent the bounds that need to be encoded by
1317 * the for loop (or a guard around the for loop).
1318 * "domain" is the subset of "bounds" for which some code is executed.
1319 * "build" is the build in which graft->node was created.
1321 * We break up "bounds" into a list of constraints and continue with
1322 * refine_generic_split.
1324 static __isl_give isl_ast_graft *refine_generic(
1325 __isl_take isl_ast_graft *graft,
1326 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1327 __isl_keep isl_ast_build *build)
1329 isl_constraint_list *list;
1331 if (!build || !graft)
1332 return isl_ast_graft_free(graft);
1334 list = isl_basic_set_get_constraint_list(bounds);
1336 graft = refine_generic_split(graft, list, domain, build);
1338 return graft;
1341 /* Create a for node for the current level.
1343 * Mark the for node degenerate if "degenerate" is set.
1345 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1346 int degenerate)
1348 int depth;
1349 isl_id *id;
1350 isl_ast_node *node;
1352 if (!build)
1353 return NULL;
1355 depth = isl_ast_build_get_depth(build);
1356 id = isl_ast_build_get_iterator_id(build, depth);
1357 node = isl_ast_node_alloc_for(id);
1358 if (degenerate)
1359 node = isl_ast_node_for_mark_degenerate(node);
1361 return node;
1364 /* If the ast_build_exploit_nested_bounds option is set, then return
1365 * the constraints enforced by all elements in "list".
1366 * Otherwise, return the universe.
1368 static __isl_give isl_basic_set *extract_shared_enforced(
1369 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1371 isl_ctx *ctx;
1372 isl_space *space;
1374 if (!list)
1375 return NULL;
1377 ctx = isl_ast_graft_list_get_ctx(list);
1378 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1379 return isl_ast_graft_list_extract_shared_enforced(list, build);
1381 space = isl_ast_build_get_space(build, 1);
1382 return isl_basic_set_universe(space);
1385 /* Return the pending constraints of "build" that are not already taken
1386 * care of (by a combination of "enforced" and the generated constraints
1387 * of "build").
1389 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1390 __isl_keep isl_basic_set *enforced)
1392 isl_set *guard, *context;
1394 guard = isl_ast_build_get_pending(build);
1395 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1396 context = isl_set_intersect(context,
1397 isl_ast_build_get_generated(build));
1398 return isl_set_gist(guard, context);
1401 /* Create an AST node for the current dimension based on
1402 * the schedule domain "bounds" and return the node encapsulated
1403 * in an isl_ast_graft.
1405 * "executed" is the current inverse schedule, taking into account
1406 * the bounds in "bounds"
1407 * "domain" is the domain of "executed", with inner dimensions projected out.
1408 * It may be a strict subset of "bounds" in case "bounds" was created
1409 * based on the atomic option or based on separation with explicit bounds.
1411 * "domain" may satisfy additional equalities that result
1412 * from intersecting "executed" with "bounds" in add_node.
1413 * It may also satisfy some global constraints that were dropped out because
1414 * we performed separation with explicit bounds.
1415 * The very first step is then to copy these constraints to "bounds".
1417 * Since we may be calling before_each_for and after_each_for
1418 * callbacks, we record the current inverse schedule in the build.
1420 * We consider three builds,
1421 * "build" is the one in which the current level is created,
1422 * "body_build" is the build in which the next level is created,
1423 * "sub_build" is essentially the same as "body_build", except that
1424 * the depth has not been increased yet.
1426 * "build" already contains information (in strides and offsets)
1427 * about the strides at the current level, but this information is not
1428 * reflected in the build->domain.
1429 * We first add this information and the "bounds" to the sub_build->domain.
1430 * isl_ast_build_set_loop_bounds adds the stride information and
1431 * checks whether the current dimension attains
1432 * only a single value and whether this single value can be represented using
1433 * a single affine expression.
1434 * In the first case, the current level is considered "degenerate".
1435 * In the second, sub-case, the current level is considered "eliminated".
1436 * Eliminated levels don't need to be reflected in the AST since we can
1437 * simply plug in the affine expression. For degenerate, but non-eliminated,
1438 * levels, we do introduce a for node, but mark is as degenerate so that
1439 * it can be printed as an assignment of the single value to the loop
1440 * "iterator".
1442 * If the current level is eliminated, we explicitly plug in the value
1443 * for the current level found by isl_ast_build_set_loop_bounds in the
1444 * inverse schedule. This ensures that if we are working on a slice
1445 * of the domain based on information available in the inverse schedule
1446 * and the build domain, that then this information is also reflected
1447 * in the inverse schedule. This operation also eliminates the current
1448 * dimension from the inverse schedule making sure no inner dimensions depend
1449 * on the current dimension. Otherwise, we create a for node, marking
1450 * it degenerate if appropriate. The initial for node is still incomplete
1451 * and will be completed in either refine_degenerate or refine_generic.
1453 * We then generate a sequence of grafts for the next level,
1454 * create a surrounding graft for the current level and insert
1455 * the for node we created (if the current level is not eliminated).
1456 * Before creating a graft for the current level, we first extract
1457 * hoistable constraints from the child guards and combine them
1458 * with the pending constraints in the build. These constraints
1459 * are used to simplify the child guards and then added to the guard
1460 * of the current graft to ensure that they will be generated.
1461 * If the hoisted guard is a disjunction, then we use it directly
1462 * to gist the guards on the children before intersect it with the
1463 * pending constraints. We do so because this disjunction is typically
1464 * identical to the guards on the children such that these guards
1465 * can be effectively removed completely. After the intersection,
1466 * the gist operation would have a harder time figuring this out.
1468 * Finally, we set the bounds of the for loop in either
1469 * refine_degenerate or refine_generic.
1470 * We do so in a context where the pending constraints of the build
1471 * have been replaced by the guard of the current graft.
1473 static __isl_give isl_ast_graft *create_node_scaled(
1474 __isl_take isl_union_map *executed,
1475 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1476 __isl_take isl_ast_build *build)
1478 int depth;
1479 int degenerate;
1480 isl_bool eliminated;
1481 isl_size n;
1482 isl_basic_set *hull;
1483 isl_basic_set *enforced;
1484 isl_set *guard, *hoisted;
1485 isl_ast_node *node = NULL;
1486 isl_ast_graft *graft;
1487 isl_ast_graft_list *children;
1488 isl_ast_build *sub_build;
1489 isl_ast_build *body_build;
1491 domain = isl_ast_build_eliminate_divs(build, domain);
1492 domain = isl_set_detect_equalities(domain);
1493 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1494 bounds = isl_basic_set_intersect(bounds, hull);
1495 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1497 depth = isl_ast_build_get_depth(build);
1498 sub_build = isl_ast_build_copy(build);
1499 bounds = isl_basic_set_remove_redundancies(bounds);
1500 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1501 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1502 isl_basic_set_copy(bounds));
1503 degenerate = isl_ast_build_has_value(sub_build);
1504 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1505 if (degenerate < 0 || eliminated < 0)
1506 executed = isl_union_map_free(executed);
1507 if (!degenerate)
1508 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1509 sub_build = isl_ast_build_set_pending_generated(sub_build,
1510 isl_basic_set_copy(bounds));
1511 if (eliminated)
1512 executed = plug_in_values(executed, sub_build);
1513 else
1514 node = create_for(build, degenerate);
1516 body_build = isl_ast_build_copy(sub_build);
1517 body_build = isl_ast_build_increase_depth(body_build);
1518 if (!eliminated)
1519 node = before_each_for(node, body_build);
1520 children = generate_next_level(executed,
1521 isl_ast_build_copy(body_build));
1523 enforced = extract_shared_enforced(children, build);
1524 guard = extract_pending(sub_build, enforced);
1525 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1526 n = isl_set_n_basic_set(hoisted);
1527 if (n < 0)
1528 children = isl_ast_graft_list_free(children);
1529 if (n > 1)
1530 children = isl_ast_graft_list_gist_guards(children,
1531 isl_set_copy(hoisted));
1532 guard = isl_set_intersect(guard, hoisted);
1533 if (!eliminated)
1534 guard = add_implied_guards(guard, degenerate, bounds, build);
1536 graft = isl_ast_graft_alloc_from_children(children,
1537 isl_set_copy(guard), enforced, build, sub_build);
1539 if (!eliminated) {
1540 isl_ast_build *for_build;
1542 graft = isl_ast_graft_insert_for(graft, node);
1543 for_build = isl_ast_build_copy(build);
1544 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1545 isl_set_copy(guard));
1546 if (degenerate)
1547 graft = refine_degenerate(graft, for_build, sub_build);
1548 else
1549 graft = refine_generic(graft, bounds,
1550 domain, for_build);
1551 isl_ast_build_free(for_build);
1553 isl_set_free(guard);
1554 if (!eliminated)
1555 graft = after_each_for(graft, body_build);
1557 isl_ast_build_free(body_build);
1558 isl_ast_build_free(sub_build);
1559 isl_ast_build_free(build);
1560 isl_basic_set_free(bounds);
1561 isl_set_free(domain);
1563 return graft;
1566 /* Internal data structure for checking if all constraints involving
1567 * the input dimension "depth" are such that the other coefficients
1568 * are multiples of "m", reducing "m" if they are not.
1569 * If "m" is reduced all the way down to "1", then the check has failed
1570 * and we break out of the iteration.
1572 struct isl_check_scaled_data {
1573 int depth;
1574 isl_val *m;
1577 /* If constraint "c" involves the input dimension data->depth,
1578 * then make sure that all the other coefficients are multiples of data->m,
1579 * reducing data->m if needed.
1580 * Break out of the iteration if data->m has become equal to "1".
1582 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1583 void *user)
1585 struct isl_check_scaled_data *data = user;
1586 int i, j;
1587 isl_size n;
1588 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1589 isl_dim_div };
1591 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1592 isl_constraint_free(c);
1593 return isl_stat_ok;
1596 for (i = 0; i < 4; ++i) {
1597 n = isl_constraint_dim(c, t[i]);
1598 if (n < 0)
1599 break;
1600 for (j = 0; j < n; ++j) {
1601 isl_val *d;
1603 if (t[i] == isl_dim_in && j == data->depth)
1604 continue;
1605 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1606 continue;
1607 d = isl_constraint_get_coefficient_val(c, t[i], j);
1608 data->m = isl_val_gcd(data->m, d);
1609 if (isl_val_is_one(data->m))
1610 break;
1612 if (j < n)
1613 break;
1616 isl_constraint_free(c);
1618 return i < 4 ? isl_stat_error : isl_stat_ok;
1621 /* For each constraint of "bmap" that involves the input dimension data->depth,
1622 * make sure that all the other coefficients are multiples of data->m,
1623 * reducing data->m if needed.
1624 * Break out of the iteration if data->m has become equal to "1".
1626 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1627 void *user)
1629 isl_stat r;
1631 r = isl_basic_map_foreach_constraint(bmap,
1632 &constraint_check_scaled, user);
1633 isl_basic_map_free(bmap);
1635 return r;
1638 /* For each constraint of "map" 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 map_check_scaled(__isl_take isl_map *map, void *user)
1645 isl_stat r;
1647 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1648 isl_map_free(map);
1650 return r;
1653 /* Create an AST node for the current dimension based on
1654 * the schedule domain "bounds" and return the node encapsulated
1655 * in an isl_ast_graft.
1657 * "executed" is the current inverse schedule, taking into account
1658 * the bounds in "bounds"
1659 * "domain" is the domain of "executed", with inner dimensions projected out.
1662 * Before moving on to the actual AST node construction in create_node_scaled,
1663 * we first check if the current dimension is strided and if we can scale
1664 * down this stride. Note that we only do this if the ast_build_scale_strides
1665 * option is set.
1667 * In particular, let the current dimension take on values
1669 * f + s a
1671 * with a an integer. We check if we can find an integer m that (obviously)
1672 * divides both f and s.
1674 * If so, we check if the current dimension only appears in constraints
1675 * where the coefficients of the other variables are multiples of m.
1676 * We perform this extra check to avoid the risk of introducing
1677 * divisions by scaling down the current dimension.
1679 * If so, we scale the current dimension down by a factor of m.
1680 * That is, we plug in
1682 * i = m i' (1)
1684 * Note that in principle we could always scale down strided loops
1685 * by plugging in
1687 * i = f + s i'
1689 * but this may result in i' taking on larger values than the original i,
1690 * due to the shift by "f".
1691 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1693 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1694 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1695 __isl_take isl_ast_build *build)
1697 struct isl_check_scaled_data data;
1698 isl_ctx *ctx;
1699 isl_aff *offset;
1700 isl_val *d;
1702 ctx = isl_ast_build_get_ctx(build);
1703 if (!isl_options_get_ast_build_scale_strides(ctx))
1704 return create_node_scaled(executed, bounds, domain, build);
1706 data.depth = isl_ast_build_get_depth(build);
1707 if (!isl_ast_build_has_stride(build, data.depth))
1708 return create_node_scaled(executed, bounds, domain, build);
1710 offset = isl_ast_build_get_offset(build, data.depth);
1711 data.m = isl_ast_build_get_stride(build, data.depth);
1712 if (!data.m)
1713 offset = isl_aff_free(offset);
1714 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1715 d = isl_aff_get_denominator_val(offset);
1716 if (!d)
1717 executed = isl_union_map_free(executed);
1719 if (executed && isl_val_is_divisible_by(data.m, d))
1720 data.m = isl_val_div(data.m, d);
1721 else {
1722 data.m = isl_val_set_si(data.m, 1);
1723 isl_val_free(d);
1726 if (!isl_val_is_one(data.m)) {
1727 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1728 &data) < 0 &&
1729 !isl_val_is_one(data.m))
1730 executed = isl_union_map_free(executed);
1733 if (!isl_val_is_one(data.m)) {
1734 isl_space *space;
1735 isl_multi_aff *ma;
1736 isl_aff *aff;
1737 isl_map *map;
1738 isl_union_map *umap;
1740 space = isl_ast_build_get_space(build, 1);
1741 space = isl_space_map_from_set(space);
1742 ma = isl_multi_aff_identity(space);
1743 aff = isl_multi_aff_get_aff(ma, data.depth);
1744 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1745 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1747 bounds = isl_basic_set_preimage_multi_aff(bounds,
1748 isl_multi_aff_copy(ma));
1749 domain = isl_set_preimage_multi_aff(domain,
1750 isl_multi_aff_copy(ma));
1751 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1752 umap = isl_union_map_from_map(map);
1753 executed = isl_union_map_apply_domain(executed,
1754 isl_union_map_copy(umap));
1755 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1756 umap);
1758 isl_aff_free(offset);
1759 isl_val_free(data.m);
1761 return create_node_scaled(executed, bounds, domain, build);
1764 /* Add the basic set to the list that "user" points to.
1766 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1768 isl_basic_set_list **list = user;
1770 *list = isl_basic_set_list_add(*list, bset);
1772 return isl_stat_ok;
1775 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1777 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1778 __isl_take isl_set *set)
1780 isl_size n;
1781 isl_ctx *ctx;
1782 isl_basic_set_list *list;
1784 n = isl_set_n_basic_set(set);
1785 if (n < 0)
1786 set = isl_set_free(set);
1787 if (!set)
1788 return NULL;
1790 ctx = isl_set_get_ctx(set);
1792 list = isl_basic_set_list_alloc(ctx, n);
1793 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1794 list = isl_basic_set_list_free(list);
1796 isl_set_free(set);
1797 return list;
1800 /* Generate code for the schedule domain "bounds"
1801 * and add the result to "list".
1803 * We mainly detect strides here and check if the bounds do not
1804 * conflict with the current build domain
1805 * and then pass over control to create_node.
1807 * "bounds" reflects the bounds on the current dimension and possibly
1808 * some extra conditions on outer dimensions.
1809 * It does not, however, include any divs involving the current dimension,
1810 * so it does not capture any stride constraints.
1811 * We therefore need to compute that part of the schedule domain that
1812 * intersects with "bounds" and derive the strides from the result.
1814 static __isl_give isl_ast_graft_list *add_node(
1815 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1816 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1818 isl_ast_graft *graft;
1819 isl_set *domain = NULL;
1820 isl_union_set *uset;
1821 int empty, disjoint;
1823 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1824 executed = isl_union_map_intersect_domain(executed, uset);
1825 empty = isl_union_map_is_empty(executed);
1826 if (empty < 0)
1827 goto error;
1828 if (empty)
1829 goto done;
1831 uset = isl_union_map_domain(isl_union_map_copy(executed));
1832 domain = isl_set_from_union_set(uset);
1833 domain = isl_ast_build_specialize(build, domain);
1835 domain = isl_set_compute_divs(domain);
1836 domain = isl_ast_build_eliminate_inner(build, domain);
1837 disjoint = isl_set_is_disjoint(domain, build->domain);
1838 if (disjoint < 0)
1839 goto error;
1840 if (disjoint)
1841 goto done;
1843 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1845 graft = create_node(executed, bounds, domain,
1846 isl_ast_build_copy(build));
1847 list = isl_ast_graft_list_add(list, graft);
1848 isl_ast_build_free(build);
1849 return list;
1850 error:
1851 list = isl_ast_graft_list_free(list);
1852 done:
1853 isl_set_free(domain);
1854 isl_basic_set_free(bounds);
1855 isl_union_map_free(executed);
1856 isl_ast_build_free(build);
1857 return list;
1860 /* Does any element of i follow or coincide with any element of j
1861 * at the current depth for equal values of the outer dimensions?
1863 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1864 __isl_keep isl_basic_set *j, void *user)
1866 int depth = *(int *) user;
1867 isl_basic_map *test;
1868 isl_bool empty;
1869 int l;
1871 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1872 isl_basic_set_copy(j));
1873 for (l = 0; l < depth; ++l)
1874 test = isl_basic_map_equate(test, isl_dim_in, l,
1875 isl_dim_out, l);
1876 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1877 isl_dim_out, depth);
1878 empty = isl_basic_map_is_empty(test);
1879 isl_basic_map_free(test);
1881 return isl_bool_not(empty);
1884 /* Split up each element of "list" into a part that is related to "bset"
1885 * according to "gt" and a part that is not.
1886 * Return a list that consist of "bset" and all the pieces.
1888 static __isl_give isl_basic_set_list *add_split_on(
1889 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1890 __isl_keep isl_basic_map *gt)
1892 int i;
1893 isl_size n;
1894 isl_basic_set_list *res;
1896 n = isl_basic_set_list_n_basic_set(list);
1897 if (n < 0)
1898 bset = isl_basic_set_free(bset);
1900 gt = isl_basic_map_copy(gt);
1901 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1902 res = isl_basic_set_list_from_basic_set(bset);
1903 for (i = 0; res && i < n; ++i) {
1904 isl_basic_set *bset;
1905 isl_set *set1, *set2;
1906 isl_basic_map *bmap;
1907 int empty;
1909 bset = isl_basic_set_list_get_basic_set(list, i);
1910 bmap = isl_basic_map_copy(gt);
1911 bmap = isl_basic_map_intersect_range(bmap, bset);
1912 bset = isl_basic_map_range(bmap);
1913 empty = isl_basic_set_is_empty(bset);
1914 if (empty < 0)
1915 res = isl_basic_set_list_free(res);
1916 if (empty) {
1917 isl_basic_set_free(bset);
1918 bset = isl_basic_set_list_get_basic_set(list, i);
1919 res = isl_basic_set_list_add(res, bset);
1920 continue;
1923 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1924 set1 = isl_set_from_basic_set(bset);
1925 bset = isl_basic_set_list_get_basic_set(list, i);
1926 set2 = isl_set_from_basic_set(bset);
1927 set1 = isl_set_subtract(set2, set1);
1928 set1 = isl_set_make_disjoint(set1);
1930 res = isl_basic_set_list_concat(res,
1931 isl_basic_set_list_from_set(set1));
1933 isl_basic_map_free(gt);
1934 isl_basic_set_list_free(list);
1935 return res;
1938 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1939 __isl_keep isl_basic_set_list *domain_list,
1940 __isl_keep isl_union_map *executed,
1941 __isl_keep isl_ast_build *build);
1943 /* Internal data structure for add_nodes.
1945 * "executed" and "build" are extra arguments to be passed to add_node.
1946 * "list" collects the results.
1948 struct isl_add_nodes_data {
1949 isl_union_map *executed;
1950 isl_ast_build *build;
1952 isl_ast_graft_list *list;
1955 /* Generate code for the schedule domains in "scc"
1956 * and add the results to "list".
1958 * The domains in "scc" form a strongly connected component in the ordering.
1959 * If the number of domains in "scc" is larger than 1, then this means
1960 * that we cannot determine a valid ordering for the domains in the component.
1961 * This should be fairly rare because the individual domains
1962 * have been made disjoint first.
1963 * The problem is that the domains may be integrally disjoint but not
1964 * rationally disjoint. For example, we may have domains
1966 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1968 * These two domains have an empty intersection, but their rational
1969 * relaxations do intersect. It is impossible to order these domains
1970 * in the second dimension because the first should be ordered before
1971 * the second for outer dimension equal to 0, while it should be ordered
1972 * after for outer dimension equal to 1.
1974 * This may happen in particular in case of unrolling since the domain
1975 * of each slice is replaced by its simple hull.
1977 * For each basic set i in "scc" and for each of the following basic sets j,
1978 * we split off that part of the basic set i that shares the outer dimensions
1979 * with j and lies before j in the current dimension.
1980 * We collect all the pieces in a new list that replaces "scc".
1982 * While the elements in "scc" should be disjoint, we double-check
1983 * this property to avoid running into an infinite recursion in case
1984 * they intersect due to some internal error.
1986 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1988 struct isl_add_nodes_data *data = user;
1989 int i, depth;
1990 isl_size n;
1991 isl_basic_set *bset, *first;
1992 isl_basic_set_list *list;
1993 isl_space *space;
1994 isl_basic_map *gt;
1996 n = isl_basic_set_list_n_basic_set(scc);
1997 if (n < 0)
1998 goto error;
1999 bset = isl_basic_set_list_get_basic_set(scc, 0);
2000 if (n == 1) {
2001 isl_basic_set_list_free(scc);
2002 data->list = add_node(data->list,
2003 isl_union_map_copy(data->executed), bset,
2004 isl_ast_build_copy(data->build));
2005 return data->list ? isl_stat_ok : isl_stat_error;
2008 depth = isl_ast_build_get_depth(data->build);
2009 space = isl_basic_set_get_space(bset);
2010 space = isl_space_map_from_set(space);
2011 gt = isl_basic_map_universe(space);
2012 for (i = 0; i < depth; ++i)
2013 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
2014 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
2016 first = isl_basic_set_copy(bset);
2017 list = isl_basic_set_list_from_basic_set(bset);
2018 for (i = 1; i < n; ++i) {
2019 int disjoint;
2021 bset = isl_basic_set_list_get_basic_set(scc, i);
2023 disjoint = isl_basic_set_is_disjoint(bset, first);
2024 if (disjoint < 0)
2025 list = isl_basic_set_list_free(list);
2026 else if (!disjoint)
2027 isl_die(isl_basic_set_list_get_ctx(scc),
2028 isl_error_internal,
2029 "basic sets in scc are assumed to be disjoint",
2030 list = isl_basic_set_list_free(list));
2032 list = add_split_on(list, bset, gt);
2034 isl_basic_set_free(first);
2035 isl_basic_map_free(gt);
2036 isl_basic_set_list_free(scc);
2037 scc = list;
2038 data->list = isl_ast_graft_list_concat(data->list,
2039 generate_sorted_domains(scc, data->executed, data->build));
2040 isl_basic_set_list_free(scc);
2042 return data->list ? isl_stat_ok : isl_stat_error;
2043 error:
2044 isl_basic_set_list_free(scc);
2045 return isl_stat_error;
2048 /* Sort the domains in "domain_list" according to the execution order
2049 * at the current depth (for equal values of the outer dimensions),
2050 * generate code for each of them, collecting the results in a list.
2051 * If no code is generated (because the intersection of the inverse schedule
2052 * with the domains turns out to be empty), then an empty list is returned.
2054 * The caller is responsible for ensuring that the basic sets in "domain_list"
2055 * are pair-wise disjoint. It can, however, in principle happen that
2056 * two basic sets should be ordered one way for one value of the outer
2057 * dimensions and the other way for some other value of the outer dimensions.
2058 * We therefore play safe and look for strongly connected components.
2059 * The function add_nodes takes care of handling non-trivial components.
2061 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2062 __isl_keep isl_basic_set_list *domain_list,
2063 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2065 isl_ctx *ctx;
2066 struct isl_add_nodes_data data;
2067 int depth;
2068 isl_size n;
2070 n = isl_basic_set_list_n_basic_set(domain_list);
2071 if (n < 0)
2072 return NULL;
2074 ctx = isl_basic_set_list_get_ctx(domain_list);
2075 data.list = isl_ast_graft_list_alloc(ctx, n);
2076 if (n == 0)
2077 return data.list;
2078 if (n == 1)
2079 return add_node(data.list, isl_union_map_copy(executed),
2080 isl_basic_set_list_get_basic_set(domain_list, 0),
2081 isl_ast_build_copy(build));
2083 depth = isl_ast_build_get_depth(build);
2084 data.executed = executed;
2085 data.build = build;
2086 if (isl_basic_set_list_foreach_scc(domain_list,
2087 &domain_follows_at_depth, &depth,
2088 &add_nodes, &data) < 0)
2089 data.list = isl_ast_graft_list_free(data.list);
2091 return data.list;
2094 /* Do i and j share any values for the outer dimensions?
2096 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2097 __isl_keep isl_basic_set *j, void *user)
2099 int depth = *(int *) user;
2100 isl_basic_map *test;
2101 isl_bool empty;
2102 int l;
2104 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2105 isl_basic_set_copy(j));
2106 for (l = 0; l < depth; ++l)
2107 test = isl_basic_map_equate(test, isl_dim_in, l,
2108 isl_dim_out, l);
2109 empty = isl_basic_map_is_empty(test);
2110 isl_basic_map_free(test);
2112 return isl_bool_not(empty);
2115 /* Internal data structure for generate_sorted_domains_wrap.
2117 * "n" is the total number of basic sets
2118 * "executed" and "build" are extra arguments to be passed
2119 * to generate_sorted_domains.
2121 * "single" is set to 1 by generate_sorted_domains_wrap if there
2122 * is only a single component.
2123 * "list" collects the results.
2125 struct isl_ast_generate_parallel_domains_data {
2126 isl_size n;
2127 isl_union_map *executed;
2128 isl_ast_build *build;
2130 int single;
2131 isl_ast_graft_list *list;
2134 /* Call generate_sorted_domains on "scc", fuse the result into a list
2135 * with either zero or one graft and collect the these single element
2136 * lists into data->list.
2138 * If there is only one component, i.e., if the number of basic sets
2139 * in the current component is equal to the total number of basic sets,
2140 * then data->single is set to 1 and the result of generate_sorted_domains
2141 * is not fused.
2143 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2144 void *user)
2146 struct isl_ast_generate_parallel_domains_data *data = user;
2147 isl_ast_graft_list *list;
2148 isl_size n;
2150 n = isl_basic_set_list_n_basic_set(scc);
2151 if (n < 0)
2152 scc = isl_basic_set_list_free(scc);
2153 list = generate_sorted_domains(scc, data->executed, data->build);
2154 data->single = n == data->n;
2155 if (!data->single)
2156 list = isl_ast_graft_list_fuse(list, data->build);
2157 if (!data->list)
2158 data->list = list;
2159 else
2160 data->list = isl_ast_graft_list_concat(data->list, list);
2162 isl_basic_set_list_free(scc);
2163 if (!data->list)
2164 return isl_stat_error;
2166 return isl_stat_ok;
2169 /* Look for any (weakly connected) components in the "domain_list"
2170 * of domains that share some values of the outer dimensions.
2171 * That is, domains in different components do not share any values
2172 * of the outer dimensions. This means that these components
2173 * can be freely reordered.
2174 * Within each of the components, we sort the domains according
2175 * to the execution order at the current depth.
2177 * If there is more than one component, then generate_sorted_domains_wrap
2178 * fuses the result of each call to generate_sorted_domains
2179 * into a list with either zero or one graft and collects these (at most)
2180 * single element lists into a bigger list. This means that the elements of the
2181 * final list can be freely reordered. In particular, we sort them
2182 * according to an arbitrary but fixed ordering to ease merging of
2183 * graft lists from different components.
2185 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2186 __isl_keep isl_basic_set_list *domain_list,
2187 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2189 int depth;
2190 struct isl_ast_generate_parallel_domains_data data;
2192 data.n = isl_basic_set_list_n_basic_set(domain_list);
2193 if (data.n < 0)
2194 return NULL;
2196 if (data.n <= 1)
2197 return generate_sorted_domains(domain_list, executed, build);
2199 depth = isl_ast_build_get_depth(build);
2200 data.list = NULL;
2201 data.executed = executed;
2202 data.build = build;
2203 data.single = 0;
2204 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2205 &generate_sorted_domains_wrap,
2206 &data) < 0)
2207 data.list = isl_ast_graft_list_free(data.list);
2209 if (!data.single)
2210 data.list = isl_ast_graft_list_sort_guard(data.list);
2212 return data.list;
2215 /* Internal data for separate_domain.
2217 * "explicit" is set if we only want to use explicit bounds.
2219 * "domain" collects the separated domains.
2221 struct isl_separate_domain_data {
2222 isl_ast_build *build;
2223 int explicit;
2224 isl_set *domain;
2227 /* Extract implicit bounds on the current dimension for the executed "map".
2229 * The domain of "map" may involve inner dimensions, so we
2230 * need to eliminate them.
2232 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2233 __isl_keep isl_ast_build *build)
2235 isl_set *domain;
2237 domain = isl_map_domain(map);
2238 domain = isl_ast_build_eliminate(build, domain);
2240 return domain;
2243 /* Extract explicit bounds on the current dimension for the executed "map".
2245 * Rather than eliminating the inner dimensions as in implicit_bounds,
2246 * we simply drop any constraints involving those inner dimensions.
2247 * The idea is that most bounds that are implied by constraints on the
2248 * inner dimensions will be enforced by for loops and not by explicit guards.
2249 * There is then no need to separate along those bounds.
2251 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2252 __isl_keep isl_ast_build *build)
2254 isl_set *domain;
2255 int depth;
2256 isl_size dim;
2258 dim = isl_map_dim(map, isl_dim_out);
2259 if (dim < 0)
2260 return isl_map_domain(isl_map_free(map));
2261 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2263 domain = isl_map_domain(map);
2264 depth = isl_ast_build_get_depth(build);
2265 dim = isl_set_dim(domain, isl_dim_set);
2266 domain = isl_set_detect_equalities(domain);
2267 domain = isl_set_drop_constraints_involving_dims(domain,
2268 isl_dim_set, depth + 1, dim - (depth + 1));
2269 domain = isl_set_remove_divs_involving_dims(domain,
2270 isl_dim_set, depth, 1);
2271 domain = isl_set_remove_unknown_divs(domain);
2273 return domain;
2276 /* Split data->domain into pieces that intersect with the range of "map"
2277 * and pieces that do not intersect with the range of "map"
2278 * and then add that part of the range of "map" that does not intersect
2279 * with data->domain.
2281 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2283 struct isl_separate_domain_data *data = user;
2284 isl_set *domain;
2285 isl_set *d1, *d2;
2287 if (data->explicit)
2288 domain = explicit_bounds(map, data->build);
2289 else
2290 domain = implicit_bounds(map, data->build);
2292 domain = isl_set_coalesce(domain);
2293 domain = isl_set_make_disjoint(domain);
2294 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2295 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2296 data->domain = isl_set_intersect(data->domain, domain);
2297 data->domain = isl_set_union(data->domain, d1);
2298 data->domain = isl_set_union(data->domain, d2);
2300 return isl_stat_ok;
2303 /* Separate the schedule domains of "executed".
2305 * That is, break up the domain of "executed" into basic sets,
2306 * such that for each basic set S, every element in S is associated with
2307 * the same domain spaces.
2309 * "space" is the (single) domain space of "executed".
2311 static __isl_give isl_set *separate_schedule_domains(
2312 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2313 __isl_keep isl_ast_build *build)
2315 struct isl_separate_domain_data data = { build };
2316 isl_ctx *ctx;
2318 ctx = isl_ast_build_get_ctx(build);
2319 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2320 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2321 data.domain = isl_set_empty(space);
2322 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2323 data.domain = isl_set_free(data.domain);
2325 isl_union_map_free(executed);
2326 return data.domain;
2329 /* Temporary data used during the search for a lower bound for unrolling.
2331 * "build" is the build in which the unrolling will be performed
2332 * "domain" is the original set for which to find a lower bound
2333 * "depth" is the dimension for which to find a lower boudn
2334 * "expansion" is the expansion that needs to be applied to "domain"
2335 * in the unrolling that will be performed
2337 * "lower" is the best lower bound found so far. It is NULL if we have not
2338 * found any yet.
2339 * "n" is the corresponding size. If lower is NULL, then the value of n
2340 * is undefined.
2341 * "n_div" is the maximal number of integer divisions in the first
2342 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2343 * been computed yet.
2345 struct isl_find_unroll_data {
2346 isl_ast_build *build;
2347 isl_set *domain;
2348 int depth;
2349 isl_basic_map *expansion;
2351 isl_aff *lower;
2352 int *n;
2353 int n_div;
2356 /* Return the constraint
2358 * i_"depth" = aff + offset
2360 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2361 int offset)
2363 aff = isl_aff_copy(aff);
2364 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2365 aff = isl_aff_add_constant_si(aff, offset);
2366 return isl_equality_from_aff(aff);
2369 /* Update *user to the number of integer divisions in the first element
2370 * of "ma", if it is larger than the current value.
2372 static isl_stat update_n_div(__isl_take isl_set *set,
2373 __isl_take isl_multi_aff *ma, void *user)
2375 isl_aff *aff;
2376 int *n = user;
2377 isl_size n_div;
2379 aff = isl_multi_aff_get_aff(ma, 0);
2380 n_div = isl_aff_dim(aff, isl_dim_div);
2381 isl_aff_free(aff);
2382 isl_multi_aff_free(ma);
2383 isl_set_free(set);
2385 if (n_div > *n)
2386 *n = n_div;
2388 return n_div >= 0 ? isl_stat_ok : isl_stat_error;
2391 /* Get the number of integer divisions in the expression for the iterator
2392 * value at the first slice in the unrolling based on lower bound "lower",
2393 * taking into account the expansion that needs to be performed on this slice.
2395 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2396 __isl_keep isl_aff *lower)
2398 isl_constraint *c;
2399 isl_set *set;
2400 isl_map *it_map, *expansion;
2401 isl_pw_multi_aff *pma;
2402 int n;
2404 c = at_offset(data->depth, lower, 0);
2405 set = isl_set_copy(data->domain);
2406 set = isl_set_add_constraint(set, c);
2407 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2408 set = isl_set_apply(set, expansion);
2409 it_map = isl_ast_build_map_to_iterator(data->build, set);
2410 pma = isl_pw_multi_aff_from_map(it_map);
2411 n = 0;
2412 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2413 n = -1;
2414 isl_pw_multi_aff_free(pma);
2416 return n;
2419 /* Is the lower bound "lower" with corresponding iteration count "n"
2420 * better than the one stored in "data"?
2421 * If there is no upper bound on the iteration count ("n" is infinity) or
2422 * if the count is too large, then we cannot use this lower bound.
2423 * Otherwise, if there was no previous lower bound or
2424 * if the iteration count of the new lower bound is smaller than
2425 * the iteration count of the previous lower bound, then we consider
2426 * the new lower bound to be better.
2427 * If the iteration count is the same, then compare the number
2428 * of integer divisions that would be needed to express
2429 * the iterator value at the first slice in the unrolling
2430 * according to the lower bound. If we end up computing this
2431 * number, then store the lowest value in data->n_div.
2433 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2434 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2436 int cmp;
2437 int n_div;
2439 if (!n)
2440 return -1;
2441 if (isl_val_is_infty(n))
2442 return 0;
2443 if (isl_val_cmp_si(n, INT_MAX) > 0)
2444 return 0;
2445 if (!data->lower)
2446 return 1;
2447 cmp = isl_val_cmp_si(n, *data->n);
2448 if (cmp < 0)
2449 return 1;
2450 if (cmp > 0)
2451 return 0;
2452 if (data->n_div < 0)
2453 data->n_div = get_expanded_n_div(data, data->lower);
2454 if (data->n_div < 0)
2455 return -1;
2456 if (data->n_div == 0)
2457 return 0;
2458 n_div = get_expanded_n_div(data, lower);
2459 if (n_div < 0)
2460 return -1;
2461 if (n_div >= data->n_div)
2462 return 0;
2463 data->n_div = n_div;
2465 return 1;
2468 /* Check if we can use "c" as a lower bound and if it is better than
2469 * any previously found lower bound.
2471 * If "c" does not involve the dimension at the current depth,
2472 * then we cannot use it.
2473 * Otherwise, let "c" be of the form
2475 * i >= f(j)/a
2477 * We compute the maximal value of
2479 * -ceil(f(j)/a)) + i + 1
2481 * over the domain. If there is such a value "n", then we know
2483 * -ceil(f(j)/a)) + i + 1 <= n
2485 * or
2487 * i < ceil(f(j)/a)) + n
2489 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2490 * We just need to check if we have found any lower bound before and
2491 * if the new lower bound is better (smaller n or fewer integer divisions)
2492 * than the previously found lower bounds.
2494 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2495 __isl_keep isl_constraint *c)
2497 isl_aff *aff, *lower;
2498 isl_val *max;
2499 int better;
2501 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2502 return isl_stat_ok;
2504 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2505 lower = isl_aff_ceil(lower);
2506 aff = isl_aff_copy(lower);
2507 aff = isl_aff_neg(aff);
2508 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2509 aff = isl_aff_add_constant_si(aff, 1);
2510 max = isl_set_max_val(data->domain, aff);
2511 isl_aff_free(aff);
2513 better = is_better_lower_bound(data, lower, max);
2514 if (better < 0 || !better) {
2515 isl_val_free(max);
2516 isl_aff_free(lower);
2517 return better < 0 ? isl_stat_error : isl_stat_ok;
2520 isl_aff_free(data->lower);
2521 data->lower = lower;
2522 *data->n = isl_val_get_num_si(max);
2523 isl_val_free(max);
2525 return isl_stat_ok;
2528 /* Check if we can use "c" as a lower bound and if it is better than
2529 * any previously found lower bound.
2531 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2533 struct isl_find_unroll_data *data;
2534 isl_stat r;
2536 data = (struct isl_find_unroll_data *) user;
2537 r = update_unrolling_lower_bound(data, c);
2538 isl_constraint_free(c);
2540 return r;
2543 /* Look for a lower bound l(i) on the dimension at "depth"
2544 * and a size n such that "domain" is a subset of
2546 * { [i] : l(i) <= i_d < l(i) + n }
2548 * where d is "depth" and l(i) depends only on earlier dimensions.
2549 * Furthermore, try and find a lower bound such that n is as small as possible.
2550 * In particular, "n" needs to be finite.
2551 * "build" is the build in which the unrolling will be performed.
2552 * "expansion" is the expansion that needs to be applied to "domain"
2553 * in the unrolling that will be performed.
2555 * Inner dimensions have been eliminated from "domain" by the caller.
2557 * We first construct a collection of lower bounds on the input set
2558 * by computing its simple hull. We then iterate through them,
2559 * discarding those that we cannot use (either because they do not
2560 * involve the dimension at "depth" or because they have no corresponding
2561 * upper bound, meaning that "n" would be unbounded) and pick out the
2562 * best from the remaining ones.
2564 * If we cannot find a suitable lower bound, then we consider that
2565 * to be an error.
2567 static __isl_give isl_aff *find_unroll_lower_bound(
2568 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2569 int depth, __isl_keep isl_basic_map *expansion, int *n)
2571 struct isl_find_unroll_data data =
2572 { build, domain, depth, expansion, NULL, n, -1 };
2573 isl_basic_set *hull;
2575 hull = isl_set_simple_hull(isl_set_copy(domain));
2577 if (isl_basic_set_foreach_constraint(hull,
2578 &constraint_find_unroll, &data) < 0)
2579 goto error;
2581 isl_basic_set_free(hull);
2583 if (!data.lower)
2584 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2585 "cannot find lower bound for unrolling", return NULL);
2587 return data.lower;
2588 error:
2589 isl_basic_set_free(hull);
2590 return isl_aff_free(data.lower);
2593 /* Call "fn" on each iteration of the current dimension of "domain".
2594 * If "init" is not NULL, then it is called with the number of
2595 * iterations before any call to "fn".
2596 * Return -1 on failure.
2598 * Since we are going to be iterating over the individual values,
2599 * we first check if there are any strides on the current dimension.
2600 * If there is, we rewrite the current dimension i as
2602 * i = stride i' + offset
2604 * and then iterate over individual values of i' instead.
2606 * We then look for a lower bound on i' and a size such that the domain
2607 * is a subset of
2609 * { [j,i'] : l(j) <= i' < l(j) + n }
2611 * and then take slices of the domain at values of i'
2612 * between l(j) and l(j) + n - 1.
2614 * We compute the unshifted simple hull of each slice to ensure that
2615 * we have a single basic set per offset. The slicing constraint
2616 * may get simplified away before the unshifted simple hull is taken
2617 * and may therefore in some rare cases disappear from the result.
2618 * We therefore explicitly add the constraint back after computing
2619 * the unshifted simple hull to ensure that the basic sets
2620 * remain disjoint. The constraints that are dropped by taking the hull
2621 * will be taken into account at the next level, as in the case of the
2622 * atomic option.
2624 * Finally, we map i' back to i and call "fn".
2626 static int foreach_iteration(__isl_take isl_set *domain,
2627 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2628 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2630 int i, n;
2631 int empty;
2632 int depth;
2633 isl_multi_aff *expansion;
2634 isl_basic_map *bmap;
2635 isl_aff *lower = NULL;
2636 isl_ast_build *stride_build;
2638 depth = isl_ast_build_get_depth(build);
2640 domain = isl_ast_build_eliminate_inner(build, domain);
2641 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2642 stride_build = isl_ast_build_copy(build);
2643 stride_build = isl_ast_build_detect_strides(stride_build,
2644 isl_set_copy(domain));
2645 expansion = isl_ast_build_get_stride_expansion(stride_build);
2647 domain = isl_set_preimage_multi_aff(domain,
2648 isl_multi_aff_copy(expansion));
2649 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2650 isl_ast_build_free(stride_build);
2652 bmap = isl_basic_map_from_multi_aff(expansion);
2654 empty = isl_set_is_empty(domain);
2655 if (empty < 0) {
2656 n = -1;
2657 } else if (empty) {
2658 n = 0;
2659 } else {
2660 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2661 if (!lower)
2662 n = -1;
2664 if (n >= 0 && init && init(n, user) < 0)
2665 n = -1;
2666 for (i = 0; i < n; ++i) {
2667 isl_set *set;
2668 isl_basic_set *bset;
2669 isl_constraint *slice;
2671 slice = at_offset(depth, lower, i);
2672 set = isl_set_copy(domain);
2673 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2674 bset = isl_set_unshifted_simple_hull(set);
2675 bset = isl_basic_set_add_constraint(bset, slice);
2676 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2678 if (fn(bset, user) < 0)
2679 break;
2682 isl_aff_free(lower);
2683 isl_set_free(domain);
2684 isl_basic_map_free(bmap);
2686 return n < 0 || i < n ? -1 : 0;
2689 /* Data structure for storing the results and the intermediate objects
2690 * of compute_domains.
2692 * "list" is the main result of the function and contains a list
2693 * of disjoint basic sets for which code should be generated.
2695 * "executed" and "build" are inputs to compute_domains.
2696 * "schedule_domain" is the domain of "executed".
2698 * "option" contains the domains at the current depth that should by
2699 * atomic, separated or unrolled. These domains are as specified by
2700 * the user, except that inner dimensions have been eliminated and
2701 * that they have been made pair-wise disjoint.
2703 * "sep_class" contains the user-specified split into separation classes
2704 * specialized to the current depth.
2705 * "done" contains the union of the separation domains that have already
2706 * been handled.
2708 struct isl_codegen_domains {
2709 isl_basic_set_list *list;
2711 isl_union_map *executed;
2712 isl_ast_build *build;
2713 isl_set *schedule_domain;
2715 isl_set *option[4];
2717 isl_map *sep_class;
2718 isl_set *done;
2721 /* Internal data structure for do_unroll.
2723 * "domains" stores the results of compute_domains.
2724 * "class_domain" is the original class domain passed to do_unroll.
2725 * "unroll_domain" collects the unrolled iterations.
2727 struct isl_ast_unroll_data {
2728 struct isl_codegen_domains *domains;
2729 isl_set *class_domain;
2730 isl_set *unroll_domain;
2733 /* Given an iteration of an unrolled domain represented by "bset",
2734 * add it to data->domains->list.
2735 * Since we may have dropped some constraints, we intersect with
2736 * the class domain again to ensure that each element in the list
2737 * is disjoint from the other class domains.
2739 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2741 struct isl_ast_unroll_data *data = user;
2742 isl_set *set;
2743 isl_basic_set_list *list;
2745 set = isl_set_from_basic_set(bset);
2746 data->unroll_domain = isl_set_union(data->unroll_domain,
2747 isl_set_copy(set));
2748 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2749 set = isl_set_make_disjoint(set);
2750 list = isl_basic_set_list_from_set(set);
2751 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2752 list);
2754 return 0;
2757 /* Extend domains->list with a list of basic sets, one for each value
2758 * of the current dimension in "domain" and remove the corresponding
2759 * sets from the class domain. Return the updated class domain.
2760 * The divs that involve the current dimension have not been projected out
2761 * from this domain.
2763 * We call foreach_iteration to iterate over the individual values and
2764 * in do_unroll_iteration we collect the individual basic sets in
2765 * domains->list and their union in data->unroll_domain, which is then
2766 * used to update the class domain.
2768 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2769 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2771 struct isl_ast_unroll_data data;
2773 if (!domain)
2774 return isl_set_free(class_domain);
2775 if (!class_domain)
2776 return isl_set_free(domain);
2778 data.domains = domains;
2779 data.class_domain = class_domain;
2780 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2782 if (foreach_iteration(domain, domains->build, NULL,
2783 &do_unroll_iteration, &data) < 0)
2784 data.unroll_domain = isl_set_free(data.unroll_domain);
2786 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2788 return class_domain;
2791 /* Add domains to domains->list for each individual value of the current
2792 * dimension, for that part of the schedule domain that lies in the
2793 * intersection of the option domain and the class domain.
2794 * Remove the corresponding sets from the class domain and
2795 * return the updated class domain.
2797 * We first break up the unroll option domain into individual pieces
2798 * and then handle each of them separately. The unroll option domain
2799 * has been made disjoint in compute_domains_init_options,
2801 * Note that we actively want to combine different pieces of the
2802 * schedule domain that have the same value at the current dimension.
2803 * We therefore need to break up the unroll option domain before
2804 * intersecting with class and schedule domain, hoping that the
2805 * unroll option domain specified by the user is relatively simple.
2807 static __isl_give isl_set *compute_unroll_domains(
2808 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2810 isl_set *unroll_domain;
2811 isl_basic_set_list *unroll_list;
2812 int i;
2813 isl_size n;
2814 isl_bool empty;
2816 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2817 if (empty < 0)
2818 return isl_set_free(class_domain);
2819 if (empty)
2820 return class_domain;
2822 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2823 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2825 n = isl_basic_set_list_n_basic_set(unroll_list);
2826 if (n < 0)
2827 class_domain = isl_set_free(class_domain);
2828 for (i = 0; i < n; ++i) {
2829 isl_basic_set *bset;
2831 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2832 unroll_domain = isl_set_from_basic_set(bset);
2833 unroll_domain = isl_set_intersect(unroll_domain,
2834 isl_set_copy(class_domain));
2835 unroll_domain = isl_set_intersect(unroll_domain,
2836 isl_set_copy(domains->schedule_domain));
2838 empty = isl_set_is_empty(unroll_domain);
2839 if (empty >= 0 && empty) {
2840 isl_set_free(unroll_domain);
2841 continue;
2844 class_domain = do_unroll(domains, unroll_domain, class_domain);
2847 isl_basic_set_list_free(unroll_list);
2849 return class_domain;
2852 /* Try and construct a single basic set that includes the intersection of
2853 * the schedule domain, the atomic option domain and the class domain.
2854 * Add the resulting basic set(s) to domains->list and remove them
2855 * from class_domain. Return the updated class domain.
2857 * We construct a single domain rather than trying to combine
2858 * the schedule domains of individual domains because we are working
2859 * within a single component so that non-overlapping schedule domains
2860 * should already have been separated.
2861 * We do however need to make sure that this single domains is a subset
2862 * of the class domain so that it would not intersect with any other
2863 * class domains. This means that we may end up splitting up the atomic
2864 * domain in case separation classes are being used.
2866 * "domain" is the intersection of the schedule domain and the class domain,
2867 * with inner dimensions projected out.
2869 static __isl_give isl_set *compute_atomic_domain(
2870 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2872 isl_basic_set *bset;
2873 isl_basic_set_list *list;
2874 isl_set *domain, *atomic_domain;
2875 int empty;
2877 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2878 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2879 domain = isl_set_intersect(domain,
2880 isl_set_copy(domains->schedule_domain));
2881 empty = isl_set_is_empty(domain);
2882 if (empty < 0)
2883 class_domain = isl_set_free(class_domain);
2884 if (empty) {
2885 isl_set_free(domain);
2886 return class_domain;
2889 domain = isl_ast_build_eliminate(domains->build, domain);
2890 domain = isl_set_coalesce_preserve(domain);
2891 bset = isl_set_unshifted_simple_hull(domain);
2892 domain = isl_set_from_basic_set(bset);
2893 atomic_domain = isl_set_copy(domain);
2894 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2895 class_domain = isl_set_subtract(class_domain, atomic_domain);
2896 domain = isl_set_make_disjoint(domain);
2897 list = isl_basic_set_list_from_set(domain);
2898 domains->list = isl_basic_set_list_concat(domains->list, list);
2900 return class_domain;
2903 /* Split up the schedule domain into uniform basic sets,
2904 * in the sense that each element in a basic set is associated to
2905 * elements of the same domains, and add the result to domains->list.
2906 * Do this for that part of the schedule domain that lies in the
2907 * intersection of "class_domain" and the separate option domain.
2909 * "class_domain" may or may not include the constraints
2910 * of the schedule domain, but this does not make a difference
2911 * since we are going to intersect it with the domain of the inverse schedule.
2912 * If it includes schedule domain constraints, then they may involve
2913 * inner dimensions, but we will eliminate them in separation_domain.
2915 static int compute_separate_domain(struct isl_codegen_domains *domains,
2916 __isl_keep isl_set *class_domain)
2918 isl_space *space;
2919 isl_set *domain;
2920 isl_union_map *executed;
2921 isl_basic_set_list *list;
2922 int empty;
2924 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2925 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2926 executed = isl_union_map_copy(domains->executed);
2927 executed = isl_union_map_intersect_domain(executed,
2928 isl_union_set_from_set(domain));
2929 empty = isl_union_map_is_empty(executed);
2930 if (empty < 0 || empty) {
2931 isl_union_map_free(executed);
2932 return empty < 0 ? -1 : 0;
2935 space = isl_set_get_space(class_domain);
2936 domain = separate_schedule_domains(space, executed, domains->build);
2938 list = isl_basic_set_list_from_set(domain);
2939 domains->list = isl_basic_set_list_concat(domains->list, list);
2941 return 0;
2944 /* Split up the domain at the current depth into disjoint
2945 * basic sets for which code should be generated separately
2946 * for the given separation class domain.
2948 * If any separation classes have been defined, then "class_domain"
2949 * is the domain of the current class and does not refer to inner dimensions.
2950 * Otherwise, "class_domain" is the universe domain.
2952 * We first make sure that the class domain is disjoint from
2953 * previously considered class domains.
2955 * The separate domains can be computed directly from the "class_domain".
2957 * The unroll, atomic and remainder domains need the constraints
2958 * from the schedule domain.
2960 * For unrolling, the actual schedule domain is needed (with divs that
2961 * may refer to the current dimension) so that stride detection can be
2962 * performed.
2964 * For atomic and remainder domains, inner dimensions and divs involving
2965 * the current dimensions should be eliminated.
2966 * In case we are working within a separation class, we need to intersect
2967 * the result with the current "class_domain" to ensure that the domains
2968 * are disjoint from those generated from other class domains.
2970 * The domain that has been made atomic may be larger than specified
2971 * by the user since it needs to be representable as a single basic set.
2972 * This possibly larger domain is removed from class_domain by
2973 * compute_atomic_domain. It is computed first so that the extended domain
2974 * would not overlap with any domains computed before.
2975 * Similary, the unrolled domains may have some constraints removed and
2976 * may therefore also be larger than specified by the user.
2978 * If anything is left after handling separate, unroll and atomic,
2979 * we split it up into basic sets and append the basic sets to domains->list.
2981 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2982 __isl_take isl_set *class_domain)
2984 isl_basic_set_list *list;
2985 isl_set *domain;
2987 class_domain = isl_set_subtract(class_domain,
2988 isl_set_copy(domains->done));
2989 domains->done = isl_set_union(domains->done,
2990 isl_set_copy(class_domain));
2992 class_domain = compute_atomic_domain(domains, class_domain);
2993 class_domain = compute_unroll_domains(domains, class_domain);
2995 domain = isl_set_copy(class_domain);
2997 if (compute_separate_domain(domains, domain) < 0)
2998 goto error;
2999 domain = isl_set_subtract(domain,
3000 isl_set_copy(domains->option[isl_ast_loop_separate]));
3002 domain = isl_set_intersect(domain,
3003 isl_set_copy(domains->schedule_domain));
3005 domain = isl_ast_build_eliminate(domains->build, domain);
3006 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
3008 domain = isl_set_coalesce_preserve(domain);
3009 domain = isl_set_make_disjoint(domain);
3011 list = isl_basic_set_list_from_set(domain);
3012 domains->list = isl_basic_set_list_concat(domains->list, list);
3014 isl_set_free(class_domain);
3016 return isl_stat_ok;
3017 error:
3018 isl_set_free(domain);
3019 isl_set_free(class_domain);
3020 return isl_stat_error;
3023 /* Split up the domain at the current depth into disjoint
3024 * basic sets for which code should be generated separately
3025 * for the separation class identified by "pnt".
3027 * We extract the corresponding class domain from domains->sep_class,
3028 * eliminate inner dimensions and pass control to compute_partial_domains.
3030 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
3032 struct isl_codegen_domains *domains = user;
3033 isl_set *class_set;
3034 isl_set *domain;
3035 int disjoint;
3037 class_set = isl_set_from_point(pnt);
3038 domain = isl_map_domain(isl_map_intersect_range(
3039 isl_map_copy(domains->sep_class), class_set));
3040 domain = isl_ast_build_compute_gist(domains->build, domain);
3041 domain = isl_ast_build_eliminate(domains->build, domain);
3043 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
3044 if (disjoint < 0)
3045 return isl_stat_error;
3046 if (disjoint) {
3047 isl_set_free(domain);
3048 return isl_stat_ok;
3051 return compute_partial_domains(domains, domain);
3054 /* Extract the domains at the current depth that should be atomic,
3055 * separated or unrolled and store them in option.
3057 * The domains specified by the user might overlap, so we make
3058 * them disjoint by subtracting earlier domains from later domains.
3060 static void compute_domains_init_options(isl_set *option[4],
3061 __isl_keep isl_ast_build *build)
3063 enum isl_ast_loop_type type, type2;
3064 isl_set *unroll;
3066 for (type = isl_ast_loop_atomic;
3067 type <= isl_ast_loop_separate; ++type) {
3068 option[type] = isl_ast_build_get_option_domain(build, type);
3069 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3070 option[type] = isl_set_subtract(option[type],
3071 isl_set_copy(option[type2]));
3074 unroll = option[isl_ast_loop_unroll];
3075 unroll = isl_set_coalesce(unroll);
3076 unroll = isl_set_make_disjoint(unroll);
3077 option[isl_ast_loop_unroll] = unroll;
3080 /* Split up the domain at the current depth into disjoint
3081 * basic sets for which code should be generated separately,
3082 * based on the user-specified options.
3083 * Return the list of disjoint basic sets.
3085 * There are three kinds of domains that we need to keep track of.
3086 * - the "schedule domain" is the domain of "executed"
3087 * - the "class domain" is the domain corresponding to the currrent
3088 * separation class
3089 * - the "option domain" is the domain corresponding to one of the options
3090 * atomic, unroll or separate
3092 * We first consider the individial values of the separation classes
3093 * and split up the domain for each of them separately.
3094 * Finally, we consider the remainder. If no separation classes were
3095 * specified, then we call compute_partial_domains with the universe
3096 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3097 * with inner dimensions removed. We do this because we want to
3098 * avoid computing the complement of the class domains (i.e., the difference
3099 * between the universe and domains->done).
3101 static __isl_give isl_basic_set_list *compute_domains(
3102 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3104 struct isl_codegen_domains domains;
3105 isl_ctx *ctx;
3106 isl_set *domain;
3107 isl_union_set *schedule_domain;
3108 isl_set *classes;
3109 isl_space *space;
3110 int n_param;
3111 enum isl_ast_loop_type type;
3112 isl_bool empty;
3114 if (!executed)
3115 return NULL;
3117 ctx = isl_union_map_get_ctx(executed);
3118 domains.list = isl_basic_set_list_alloc(ctx, 0);
3120 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3121 domain = isl_set_from_union_set(schedule_domain);
3123 compute_domains_init_options(domains.option, build);
3125 domains.sep_class = isl_ast_build_get_separation_class(build);
3126 classes = isl_map_range(isl_map_copy(domains.sep_class));
3127 n_param = isl_set_dim(classes, isl_dim_param);
3128 if (n_param < 0)
3129 classes = isl_set_free(classes);
3130 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3132 space = isl_set_get_space(domain);
3133 domains.build = build;
3134 domains.schedule_domain = isl_set_copy(domain);
3135 domains.executed = executed;
3136 domains.done = isl_set_empty(space);
3138 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3139 domains.list = isl_basic_set_list_free(domains.list);
3140 isl_set_free(classes);
3142 empty = isl_set_is_empty(domains.done);
3143 if (empty < 0) {
3144 domains.list = isl_basic_set_list_free(domains.list);
3145 domain = isl_set_free(domain);
3146 } else if (empty) {
3147 isl_set_free(domain);
3148 domain = isl_set_universe(isl_set_get_space(domains.done));
3149 } else {
3150 domain = isl_ast_build_eliminate(build, domain);
3152 if (compute_partial_domains(&domains, domain) < 0)
3153 domains.list = isl_basic_set_list_free(domains.list);
3155 isl_set_free(domains.schedule_domain);
3156 isl_set_free(domains.done);
3157 isl_map_free(domains.sep_class);
3158 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3159 isl_set_free(domains.option[type]);
3161 return domains.list;
3164 /* Generate code for a single component, after shifting (if any)
3165 * has been applied, in case the schedule was specified as a union map.
3167 * We first split up the domain at the current depth into disjoint
3168 * basic sets based on the user-specified options.
3169 * Then we generated code for each of them and concatenate the results.
3171 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3172 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3174 isl_basic_set_list *domain_list;
3175 isl_ast_graft_list *list = NULL;
3177 domain_list = compute_domains(executed, build);
3178 list = generate_parallel_domains(domain_list, executed, build);
3180 isl_basic_set_list_free(domain_list);
3181 isl_union_map_free(executed);
3182 isl_ast_build_free(build);
3184 return list;
3187 /* Generate code for a single component, after shifting (if any)
3188 * has been applied, in case the schedule was specified as a schedule tree
3189 * and the separate option was specified.
3191 * We perform separation on the domain of "executed" and then generate
3192 * an AST for each of the resulting disjoint basic sets.
3194 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3195 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3197 isl_space *space;
3198 isl_set *domain;
3199 isl_basic_set_list *domain_list;
3200 isl_ast_graft_list *list;
3202 space = isl_ast_build_get_space(build, 1);
3203 domain = separate_schedule_domains(space,
3204 isl_union_map_copy(executed), build);
3205 domain_list = isl_basic_set_list_from_set(domain);
3207 list = generate_parallel_domains(domain_list, executed, build);
3209 isl_basic_set_list_free(domain_list);
3210 isl_union_map_free(executed);
3211 isl_ast_build_free(build);
3213 return list;
3216 /* Internal data structure for generate_shifted_component_tree_unroll.
3218 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3219 * "list" collects the constructs grafts.
3221 struct isl_ast_unroll_tree_data {
3222 isl_union_map *executed;
3223 isl_ast_build *build;
3224 isl_ast_graft_list *list;
3227 /* Initialize data->list to a list of "n" elements.
3229 static int init_unroll_tree(int n, void *user)
3231 struct isl_ast_unroll_tree_data *data = user;
3232 isl_ctx *ctx;
3234 ctx = isl_ast_build_get_ctx(data->build);
3235 data->list = isl_ast_graft_list_alloc(ctx, n);
3237 return 0;
3240 /* Given an iteration of an unrolled domain represented by "bset",
3241 * generate the corresponding AST and add the result to data->list.
3243 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3245 struct isl_ast_unroll_tree_data *data = user;
3247 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3248 bset, isl_ast_build_copy(data->build));
3250 return 0;
3253 /* Generate code for a single component, after shifting (if any)
3254 * has been applied, in case the schedule was specified as a schedule tree
3255 * and the unroll option was specified.
3257 * We call foreach_iteration to iterate over the individual values and
3258 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3260 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3261 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3262 __isl_take isl_ast_build *build)
3264 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3266 if (foreach_iteration(domain, build, &init_unroll_tree,
3267 &do_unroll_tree_iteration, &data) < 0)
3268 data.list = isl_ast_graft_list_free(data.list);
3270 isl_union_map_free(executed);
3271 isl_ast_build_free(build);
3273 return data.list;
3276 /* Does "domain" involve a disjunction that is purely based on
3277 * constraints involving only outer dimension?
3279 * In particular, is there a disjunction such that the constraints
3280 * involving the current and later dimensions are the same over
3281 * all the disjuncts?
3283 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3284 __isl_keep isl_ast_build *build)
3286 isl_basic_set *hull;
3287 isl_set *shared, *inner;
3288 isl_bool equal;
3289 int depth;
3290 isl_size n;
3291 isl_size dim;
3293 n = isl_set_n_basic_set(domain);
3294 if (n < 0)
3295 return isl_bool_error;
3296 if (n <= 1)
3297 return isl_bool_false;
3298 dim = isl_set_dim(domain, isl_dim_set);
3299 if (dim < 0)
3300 return isl_bool_error;
3302 inner = isl_set_copy(domain);
3303 depth = isl_ast_build_get_depth(build);
3304 inner = isl_set_drop_constraints_not_involving_dims(inner,
3305 isl_dim_set, depth, dim - depth);
3306 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3307 shared = isl_set_from_basic_set(hull);
3308 equal = isl_set_plain_is_equal(inner, shared);
3309 isl_set_free(inner);
3310 isl_set_free(shared);
3312 return equal;
3315 /* Generate code for a single component, after shifting (if any)
3316 * has been applied, in case the schedule was specified as a schedule tree.
3317 * In particular, handle the base case where there is either no isolated
3318 * set or we are within the isolated set (in which case "isolated" is set)
3319 * or the iterations that precede or follow the isolated set.
3321 * The schedule domain is broken up or combined into basic sets
3322 * according to the AST generation option specified in the current
3323 * schedule node, which may be either atomic, separate, unroll or
3324 * unspecified. If the option is unspecified, then we currently simply
3325 * split the schedule domain into disjoint basic sets.
3327 * In case the separate option is specified, the AST generation is
3328 * handled by generate_shifted_component_tree_separate.
3329 * In the other cases, we need the global schedule domain.
3330 * In the unroll case, the AST generation is then handled by
3331 * generate_shifted_component_tree_unroll which needs the actual
3332 * schedule domain (with divs that may refer to the current dimension)
3333 * so that stride detection can be performed.
3334 * In the atomic or unspecified case, inner dimensions and divs involving
3335 * the current dimensions should be eliminated.
3336 * The result is then either combined into a single basic set or
3337 * split up into disjoint basic sets.
3338 * Finally an AST is generated for each basic set and the results are
3339 * concatenated.
3341 * If the schedule domain involves a disjunction that is purely based on
3342 * constraints involving only outer dimension, then it is treated as
3343 * if atomic was specified. This ensures that only a single loop
3344 * is generated instead of a sequence of identical loops with
3345 * different guards.
3347 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3348 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3349 int isolated)
3351 isl_bool outer_disjunction;
3352 isl_union_set *schedule_domain;
3353 isl_set *domain;
3354 isl_basic_set_list *domain_list;
3355 isl_ast_graft_list *list;
3356 enum isl_ast_loop_type type;
3358 type = isl_ast_build_get_loop_type(build, isolated);
3359 if (type < 0)
3360 goto error;
3362 if (type == isl_ast_loop_separate)
3363 return generate_shifted_component_tree_separate(executed,
3364 build);
3366 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3367 domain = isl_set_from_union_set(schedule_domain);
3369 if (type == isl_ast_loop_unroll)
3370 return generate_shifted_component_tree_unroll(executed, domain,
3371 build);
3373 domain = isl_ast_build_eliminate(build, domain);
3374 domain = isl_set_coalesce_preserve(domain);
3376 outer_disjunction = has_pure_outer_disjunction(domain, build);
3377 if (outer_disjunction < 0)
3378 domain = isl_set_free(domain);
3380 if (outer_disjunction || type == isl_ast_loop_atomic) {
3381 isl_basic_set *hull;
3382 hull = isl_set_unshifted_simple_hull(domain);
3383 domain_list = isl_basic_set_list_from_basic_set(hull);
3384 } else {
3385 domain = isl_set_make_disjoint(domain);
3386 domain_list = isl_basic_set_list_from_set(domain);
3389 list = generate_parallel_domains(domain_list, executed, build);
3391 isl_basic_set_list_free(domain_list);
3392 isl_union_map_free(executed);
3393 isl_ast_build_free(build);
3395 return list;
3396 error:
3397 isl_union_map_free(executed);
3398 isl_ast_build_free(build);
3399 return NULL;
3402 /* Extract out the disjunction imposed by "domain" on the outer
3403 * schedule dimensions.
3405 * In particular, remove all inner dimensions from "domain" (including
3406 * the current dimension) and then remove the constraints that are shared
3407 * by all disjuncts in the result.
3409 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3410 __isl_keep isl_ast_build *build)
3412 isl_set *hull;
3413 int depth;
3414 isl_size dim;
3416 domain = isl_ast_build_specialize(build, domain);
3417 depth = isl_ast_build_get_depth(build);
3418 dim = isl_set_dim(domain, isl_dim_set);
3419 if (dim < 0)
3420 return isl_set_free(domain);
3421 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3422 domain = isl_set_remove_unknown_divs(domain);
3423 hull = isl_set_copy(domain);
3424 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3425 domain = isl_set_gist(domain, hull);
3427 return domain;
3430 /* Add "guard" to the grafts in "list".
3431 * "build" is the outer AST build, while "sub_build" includes "guard"
3432 * in its generated domain.
3434 * First combine the grafts into a single graft and then add the guard.
3435 * If the list is empty, or if some error occurred, then simply return
3436 * the list.
3438 static __isl_give isl_ast_graft_list *list_add_guard(
3439 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3440 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3442 isl_ast_graft *graft;
3443 isl_size n;
3445 list = isl_ast_graft_list_fuse(list, sub_build);
3447 n = isl_ast_graft_list_n_ast_graft(list);
3448 if (n < 0)
3449 return isl_ast_graft_list_free(list);
3450 if (n != 1)
3451 return list;
3453 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3454 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3455 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3457 return list;
3460 /* Generate code for a single component, after shifting (if any)
3461 * has been applied, in case the schedule was specified as a schedule tree.
3462 * In particular, do so for the specified subset of the schedule domain.
3464 * If we are outside of the isolated part, then "domain" may include
3465 * a disjunction. Explicitly generate this disjunction at this point
3466 * instead of relying on the disjunction getting hoisted back up
3467 * to this level.
3469 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3470 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3471 __isl_keep isl_ast_build *build, int isolated)
3473 isl_union_set *uset;
3474 isl_ast_graft_list *list;
3475 isl_ast_build *sub_build;
3476 int empty;
3478 uset = isl_union_set_from_set(isl_set_copy(domain));
3479 executed = isl_union_map_copy(executed);
3480 executed = isl_union_map_intersect_domain(executed, uset);
3481 empty = isl_union_map_is_empty(executed);
3482 if (empty < 0)
3483 goto error;
3484 if (empty) {
3485 isl_ctx *ctx;
3486 isl_union_map_free(executed);
3487 isl_set_free(domain);
3488 ctx = isl_ast_build_get_ctx(build);
3489 return isl_ast_graft_list_alloc(ctx, 0);
3492 sub_build = isl_ast_build_copy(build);
3493 if (!isolated) {
3494 domain = extract_disjunction(domain, build);
3495 sub_build = isl_ast_build_restrict_generated(sub_build,
3496 isl_set_copy(domain));
3498 list = generate_shifted_component_tree_base(executed,
3499 isl_ast_build_copy(sub_build), isolated);
3500 if (!isolated)
3501 list = list_add_guard(list, domain, build, sub_build);
3502 isl_ast_build_free(sub_build);
3503 isl_set_free(domain);
3504 return list;
3505 error:
3506 isl_union_map_free(executed);
3507 isl_set_free(domain);
3508 return NULL;
3511 /* Generate code for a single component, after shifting (if any)
3512 * has been applied, in case the schedule was specified as a schedule tree.
3513 * In particular, do so for the specified sequence of subsets
3514 * of the schedule domain, "before", "isolated", "after" and "other",
3515 * where only the "isolated" part is considered to be isolated.
3517 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3518 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3519 __isl_take isl_set *isolated, __isl_take isl_set *after,
3520 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3522 isl_ast_graft_list *list, *res;
3524 res = generate_shifted_component_tree_part(executed, before, build, 0);
3525 list = generate_shifted_component_tree_part(executed, isolated,
3526 build, 1);
3527 res = isl_ast_graft_list_concat(res, list);
3528 list = generate_shifted_component_tree_part(executed, after, build, 0);
3529 res = isl_ast_graft_list_concat(res, list);
3530 list = generate_shifted_component_tree_part(executed, other, build, 0);
3531 res = isl_ast_graft_list_concat(res, list);
3533 isl_union_map_free(executed);
3534 isl_ast_build_free(build);
3536 return res;
3539 /* Does "set" intersect "first", but not "second"?
3541 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3542 __isl_keep isl_set *first, __isl_keep isl_set *second)
3544 isl_bool disjoint;
3546 disjoint = isl_set_is_disjoint(set, first);
3547 if (disjoint < 0)
3548 return isl_bool_error;
3549 if (disjoint)
3550 return isl_bool_false;
3552 return isl_set_is_disjoint(set, second);
3555 /* Generate code for a single component, after shifting (if any)
3556 * has been applied, in case the schedule was specified as a schedule tree.
3557 * In particular, do so in case of isolation where there is
3558 * only an "isolated" part and an "after" part.
3559 * "dead1" and "dead2" are freed by this function in order to simplify
3560 * the caller.
3562 * The "before" and "other" parts are set to empty sets.
3564 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3565 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3566 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3567 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3569 isl_set *empty;
3571 empty = isl_set_empty(isl_set_get_space(after));
3572 isl_set_free(dead1);
3573 isl_set_free(dead2);
3574 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3575 isolated, after, empty, build);
3578 /* Generate code for a single component, after shifting (if any)
3579 * has been applied, in case the schedule was specified as a schedule tree.
3581 * We first check if the user has specified an isolated schedule domain
3582 * and that we are not already outside of this isolated schedule domain.
3583 * If so, we break up the schedule domain into iterations that
3584 * precede the isolated domain, the isolated domain itself,
3585 * the iterations that follow the isolated domain and
3586 * the remaining iterations (those that are incomparable
3587 * to the isolated domain).
3588 * We generate an AST for each piece and concatenate the results.
3590 * If the isolated domain is not convex, then it is replaced
3591 * by a convex superset to ensure that the sets of preceding and
3592 * following iterations are properly defined and, in particular,
3593 * that there are no intermediate iterations that do not belong
3594 * to the isolated domain.
3596 * In the special case where at least one element of the schedule
3597 * domain that does not belong to the isolated domain needs
3598 * to be scheduled after this isolated domain, but none of those
3599 * elements need to be scheduled before, break up the schedule domain
3600 * in only two parts, the isolated domain, and a part that will be
3601 * scheduled after the isolated domain.
3603 * If no isolated set has been specified, then we generate an
3604 * AST for the entire inverse schedule.
3606 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3607 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3609 int i, depth;
3610 int empty, has_isolate;
3611 isl_space *space;
3612 isl_union_set *schedule_domain;
3613 isl_set *domain;
3614 isl_basic_set *hull;
3615 isl_set *isolated, *before, *after, *test;
3616 isl_map *gt, *lt;
3617 isl_bool pure;
3619 build = isl_ast_build_extract_isolated(build);
3620 has_isolate = isl_ast_build_has_isolated(build);
3621 if (has_isolate < 0)
3622 executed = isl_union_map_free(executed);
3623 else if (!has_isolate)
3624 return generate_shifted_component_tree_base(executed, build, 0);
3626 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3627 domain = isl_set_from_union_set(schedule_domain);
3629 isolated = isl_ast_build_get_isolated(build);
3630 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3631 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3632 empty = isl_set_is_empty(test);
3633 isl_set_free(test);
3634 if (empty < 0)
3635 goto error;
3636 if (empty) {
3637 isl_set_free(isolated);
3638 isl_set_free(domain);
3639 return generate_shifted_component_tree_base(executed, build, 0);
3641 isolated = isl_ast_build_eliminate(build, isolated);
3642 hull = isl_set_unshifted_simple_hull(isolated);
3643 isolated = isl_set_from_basic_set(hull);
3645 depth = isl_ast_build_get_depth(build);
3646 space = isl_space_map_from_set(isl_set_get_space(isolated));
3647 gt = isl_map_universe(space);
3648 for (i = 0; i < depth; ++i)
3649 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3650 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3651 lt = isl_map_reverse(isl_map_copy(gt));
3652 before = isl_set_apply(isl_set_copy(isolated), gt);
3653 after = isl_set_apply(isl_set_copy(isolated), lt);
3655 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3656 pure = only_intersects_first(domain, after, before);
3657 if (pure < 0)
3658 executed = isl_union_map_free(executed);
3659 else if (pure)
3660 return generate_shifted_component_only_after(executed, isolated,
3661 domain, build, before, after);
3662 domain = isl_set_subtract(domain, isl_set_copy(before));
3663 domain = isl_set_subtract(domain, isl_set_copy(after));
3664 after = isl_set_subtract(after, isl_set_copy(isolated));
3665 after = isl_set_subtract(after, isl_set_copy(before));
3666 before = isl_set_subtract(before, isl_set_copy(isolated));
3668 return generate_shifted_component_parts(executed, before, isolated,
3669 after, domain, build);
3670 error:
3671 isl_set_free(domain);
3672 isl_set_free(isolated);
3673 isl_union_map_free(executed);
3674 isl_ast_build_free(build);
3675 return NULL;
3678 /* Generate code for a single component, after shifting (if any)
3679 * has been applied.
3681 * Call generate_shifted_component_tree or generate_shifted_component_flat
3682 * depending on whether the schedule was specified as a schedule tree.
3684 static __isl_give isl_ast_graft_list *generate_shifted_component(
3685 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3687 if (isl_ast_build_has_schedule_node(build))
3688 return generate_shifted_component_tree(executed, build);
3689 else
3690 return generate_shifted_component_flat(executed, build);
3693 struct isl_set_map_pair {
3694 isl_set *set;
3695 isl_map *map;
3698 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3699 * of indices into the "domain" array,
3700 * return the union of the "map" fields of the elements
3701 * indexed by the first "n" elements of "order".
3703 static __isl_give isl_union_map *construct_component_executed(
3704 struct isl_set_map_pair *domain, int *order, int n)
3706 int i;
3707 isl_map *map;
3708 isl_union_map *executed;
3710 map = isl_map_copy(domain[order[0]].map);
3711 executed = isl_union_map_from_map(map);
3712 for (i = 1; i < n; ++i) {
3713 map = isl_map_copy(domain[order[i]].map);
3714 executed = isl_union_map_add_map(executed, map);
3717 return executed;
3720 /* Generate code for a single component, after shifting (if any)
3721 * has been applied.
3723 * The component inverse schedule is specified as the "map" fields
3724 * of the elements of "domain" indexed by the first "n" elements of "order".
3726 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3727 struct isl_set_map_pair *domain, int *order, int n,
3728 __isl_take isl_ast_build *build)
3730 isl_union_map *executed;
3732 executed = construct_component_executed(domain, order, n);
3733 return generate_shifted_component(executed, build);
3736 /* Does set dimension "pos" of "set" have an obviously fixed value?
3738 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3740 int fixed;
3741 isl_val *v;
3743 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3744 if (!v)
3745 return -1;
3746 fixed = !isl_val_is_nan(v);
3747 isl_val_free(v);
3749 return fixed;
3752 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3753 * of indices into the "domain" array,
3754 * do all (except for at most one) of the "set" field of the elements
3755 * indexed by the first "n" elements of "order" have a fixed value
3756 * at position "depth"?
3758 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3759 int *order, int n, int depth)
3761 int i;
3762 int non_fixed = -1;
3764 for (i = 0; i < n; ++i) {
3765 int f;
3767 f = dim_is_fixed(domain[order[i]].set, depth);
3768 if (f < 0)
3769 return -1;
3770 if (f)
3771 continue;
3772 if (non_fixed >= 0)
3773 return 0;
3774 non_fixed = i;
3777 return 1;
3780 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3781 * of indices into the "domain" array,
3782 * eliminate the inner dimensions from the "set" field of the elements
3783 * indexed by the first "n" elements of "order", provided the current
3784 * dimension does not have a fixed value.
3786 * Return the index of the first element in "order" with a corresponding
3787 * "set" field that does not have an (obviously) fixed value.
3789 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3790 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3792 int i;
3793 int base = -1;
3795 for (i = n - 1; i >= 0; --i) {
3796 int f;
3797 f = dim_is_fixed(domain[order[i]].set, depth);
3798 if (f < 0)
3799 return -1;
3800 if (f)
3801 continue;
3802 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3803 domain[order[i]].set);
3804 base = i;
3807 return base;
3810 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3811 * of indices into the "domain" array,
3812 * find the element of "domain" (amongst those indexed by the first "n"
3813 * elements of "order") with the "set" field that has the smallest
3814 * value for the current iterator.
3816 * Note that the domain with the smallest value may depend on the parameters
3817 * and/or outer loop dimension. Since the result of this function is only
3818 * used as heuristic, we only make a reasonable attempt at finding the best
3819 * domain, one that should work in case a single domain provides the smallest
3820 * value for the current dimension over all values of the parameters
3821 * and outer dimensions.
3823 * In particular, we compute the smallest value of the first domain
3824 * and replace it by that of any later domain if that later domain
3825 * has a smallest value that is smaller for at least some value
3826 * of the parameters and outer dimensions.
3828 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3829 __isl_keep isl_ast_build *build)
3831 int i;
3832 isl_map *min_first;
3833 int first = 0;
3835 min_first = isl_ast_build_map_to_iterator(build,
3836 isl_set_copy(domain[order[0]].set));
3837 min_first = isl_map_lexmin(min_first);
3839 for (i = 1; i < n; ++i) {
3840 isl_map *min, *test;
3841 int empty;
3843 min = isl_ast_build_map_to_iterator(build,
3844 isl_set_copy(domain[order[i]].set));
3845 min = isl_map_lexmin(min);
3846 test = isl_map_copy(min);
3847 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3848 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3849 empty = isl_map_is_empty(test);
3850 isl_map_free(test);
3851 if (empty >= 0 && !empty) {
3852 isl_map_free(min_first);
3853 first = i;
3854 min_first = min;
3855 } else
3856 isl_map_free(min);
3858 if (empty < 0)
3859 break;
3862 isl_map_free(min_first);
3864 return i < n ? -1 : first;
3867 /* Construct a shifted inverse schedule based on the original inverse schedule,
3868 * the stride and the offset.
3870 * The original inverse schedule is specified as the "map" fields
3871 * of the elements of "domain" indexed by the first "n" elements of "order".
3873 * "stride" and "offset" are such that the difference
3874 * between the values of the current dimension of domain "i"
3875 * and the values of the current dimension for some reference domain are
3876 * equal to
3878 * stride * integer + offset[i]
3880 * Moreover, 0 <= offset[i] < stride.
3882 * For each domain, we create a map
3884 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3886 * where j refers to the current dimension and the other dimensions are
3887 * unchanged, and apply this map to the original schedule domain.
3889 * For example, for the original schedule
3891 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3893 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3894 * we apply the mapping
3896 * { [j] -> [j, 0] }
3898 * to the schedule of the "A" domain and the mapping
3900 * { [j - 1] -> [j, 1] }
3902 * to the schedule of the "B" domain.
3905 * Note that after the transformation, the differences between pairs
3906 * of values of the current dimension over all domains are multiples
3907 * of stride and that we have therefore exposed the stride.
3910 * To see that the mapping preserves the lexicographic order,
3911 * first note that each of the individual maps above preserves the order.
3912 * If the value of the current iterator is j1 in one domain and j2 in another,
3913 * then if j1 = j2, we know that the same map is applied to both domains
3914 * and the order is preserved.
3915 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3916 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3918 * j1 - c1 < j2 - c2
3920 * and the order is preserved.
3921 * If c1 < c2, then we know
3923 * 0 <= c2 - c1 < s
3925 * We also have
3927 * j2 - j1 = n * s + r
3929 * with n >= 0 and 0 <= r < s.
3930 * In other words, r = c2 - c1.
3931 * If n > 0, then
3933 * j1 - c1 < j2 - c2
3935 * If n = 0, then
3937 * j1 - c1 = j2 - c2
3939 * and so
3941 * (j1 - c1, c1) << (j2 - c2, c2)
3943 * with "<<" the lexicographic order, proving that the order is preserved
3944 * in all cases.
3946 static __isl_give isl_union_map *construct_shifted_executed(
3947 struct isl_set_map_pair *domain, int *order, int n,
3948 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3949 __isl_take isl_ast_build *build)
3951 int i;
3952 isl_union_map *executed;
3953 isl_space *space;
3954 isl_map *map;
3955 int depth;
3956 isl_constraint *c;
3958 depth = isl_ast_build_get_depth(build);
3959 space = isl_ast_build_get_space(build, 1);
3960 executed = isl_union_map_empty(isl_space_copy(space));
3961 space = isl_space_map_from_set(space);
3962 map = isl_map_identity(isl_space_copy(space));
3963 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3964 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3965 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3967 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3968 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3969 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3971 for (i = 0; i < n; ++i) {
3972 isl_map *map_i;
3973 isl_val *v;
3975 v = isl_multi_val_get_val(offset, i);
3976 if (!v)
3977 break;
3978 map_i = isl_map_copy(map);
3979 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3980 isl_val_copy(v));
3981 v = isl_val_neg(v);
3982 c = isl_constraint_set_constant_val(c, v);
3983 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3985 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3986 map_i);
3987 executed = isl_union_map_add_map(executed, map_i);
3990 isl_constraint_free(c);
3991 isl_map_free(map);
3993 if (i < n)
3994 executed = isl_union_map_free(executed);
3996 return executed;
3999 /* Generate code for a single component, after exposing the stride,
4000 * given that the schedule domain is "shifted strided".
4002 * The component inverse schedule is specified as the "map" fields
4003 * of the elements of "domain" indexed by the first "n" elements of "order".
4005 * The schedule domain being "shifted strided" means that the differences
4006 * between the values of the current dimension of domain "i"
4007 * and the values of the current dimension for some reference domain are
4008 * equal to
4010 * stride * integer + offset[i]
4012 * We first look for the domain with the "smallest" value for the current
4013 * dimension and adjust the offsets such that the offset of the "smallest"
4014 * domain is equal to zero. The other offsets are reduced modulo stride.
4016 * Based on this information, we construct a new inverse schedule in
4017 * construct_shifted_executed that exposes the stride.
4018 * Since this involves the introduction of a new schedule dimension,
4019 * the build needs to be changed accordingly.
4020 * After computing the AST, the newly introduced dimension needs
4021 * to be removed again from the list of grafts. We do this by plugging
4022 * in a mapping that represents the new schedule domain in terms of the
4023 * old schedule domain.
4025 static __isl_give isl_ast_graft_list *generate_shift_component(
4026 struct isl_set_map_pair *domain, int *order, int n,
4027 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
4028 __isl_take isl_ast_build *build)
4030 isl_ast_graft_list *list;
4031 int first;
4032 int depth;
4033 isl_val *val;
4034 isl_multi_val *mv;
4035 isl_space *space;
4036 isl_multi_aff *ma, *zero;
4037 isl_union_map *executed;
4039 depth = isl_ast_build_get_depth(build);
4041 first = first_offset(domain, order, n, build);
4042 if (first < 0)
4043 goto error;
4045 mv = isl_multi_val_copy(offset);
4046 val = isl_multi_val_get_val(offset, first);
4047 val = isl_val_neg(val);
4048 mv = isl_multi_val_add_val(mv, val);
4049 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
4051 executed = construct_shifted_executed(domain, order, n, stride, mv,
4052 build);
4053 space = isl_ast_build_get_space(build, 1);
4054 space = isl_space_map_from_set(space);
4055 ma = isl_multi_aff_identity(isl_space_copy(space));
4056 space = isl_space_from_domain(isl_space_domain(space));
4057 space = isl_space_add_dims(space, isl_dim_out, 1);
4058 zero = isl_multi_aff_zero(space);
4059 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
4060 build = isl_ast_build_insert_dim(build, depth + 1);
4061 list = generate_shifted_component(executed, build);
4063 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4065 isl_multi_val_free(mv);
4067 return list;
4068 error:
4069 isl_ast_build_free(build);
4070 return NULL;
4073 /* Does any node in the schedule tree rooted at the current schedule node
4074 * of "build" depend on outer schedule nodes?
4076 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4078 isl_schedule_node *node;
4079 int dependent = 0;
4081 node = isl_ast_build_get_schedule_node(build);
4082 dependent = isl_schedule_node_is_subtree_anchored(node);
4083 isl_schedule_node_free(node);
4085 return dependent;
4088 /* Generate code for a single component.
4090 * The component inverse schedule is specified as the "map" fields
4091 * of the elements of "domain" indexed by the first "n" elements of "order".
4093 * This function may modify the "set" fields of "domain".
4095 * Before proceeding with the actual code generation for the component,
4096 * we first check if there are any "shifted" strides, meaning that
4097 * the schedule domains of the individual domains are all strided,
4098 * but that they have different offsets, resulting in the union
4099 * of schedule domains not being strided anymore.
4101 * The simplest example is the schedule
4103 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4105 * Both schedule domains are strided, but their union is not.
4106 * This function detects such cases and then rewrites the schedule to
4108 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4110 * In the new schedule, the schedule domains have the same offset (modulo
4111 * the stride), ensuring that the union of schedule domains is also strided.
4114 * If there is only a single domain in the component, then there is
4115 * nothing to do. Similarly, if the current schedule dimension has
4116 * a fixed value for almost all domains then there is nothing to be done.
4117 * In particular, we need at least two domains where the current schedule
4118 * dimension does not have a fixed value.
4119 * Finally, in case of a schedule map input,
4120 * if any of the options refer to the current schedule dimension,
4121 * then we bail out as well. It would be possible to reformulate the options
4122 * in terms of the new schedule domain, but that would introduce constraints
4123 * that separate the domains in the options and that is something we would
4124 * like to avoid.
4125 * In the case of a schedule tree input, we bail out if any of
4126 * the descendants of the current schedule node refer to outer
4127 * schedule nodes in any way.
4130 * To see if there is any shifted stride, we look at the differences
4131 * between the values of the current dimension in pairs of domains
4132 * for equal values of outer dimensions. These differences should be
4133 * of the form
4135 * m x + r
4137 * with "m" the stride and "r" a constant. Note that we cannot perform
4138 * this analysis on individual domains as the lower bound in each domain
4139 * may depend on parameters or outer dimensions and so the current dimension
4140 * itself may not have a fixed remainder on division by the stride.
4142 * In particular, we compare the first domain that does not have an
4143 * obviously fixed value for the current dimension to itself and all
4144 * other domains and collect the offsets and the gcd of the strides.
4145 * If the gcd becomes one, then we failed to find shifted strides.
4146 * If the gcd is zero, then the differences were all fixed, meaning
4147 * that some domains had non-obviously fixed values for the current dimension.
4148 * If all the offsets are the same (for those domains that do not have
4149 * an obviously fixed value for the current dimension), then we do not
4150 * apply the transformation.
4151 * If none of the domains were skipped, then there is nothing to do.
4152 * If some of them were skipped, then if we apply separation, the schedule
4153 * domain should get split in pieces with a (non-shifted) stride.
4155 * Otherwise, we apply a shift to expose the stride in
4156 * generate_shift_component.
4158 static __isl_give isl_ast_graft_list *generate_component(
4159 struct isl_set_map_pair *domain, int *order, int n,
4160 __isl_take isl_ast_build *build)
4162 int i, d;
4163 int depth;
4164 isl_ctx *ctx;
4165 isl_map *map;
4166 isl_set *deltas;
4167 isl_val *gcd = NULL;
4168 isl_multi_val *mv;
4169 int fixed, skip;
4170 int base;
4171 isl_ast_graft_list *list;
4172 int res = 0;
4174 depth = isl_ast_build_get_depth(build);
4176 skip = n == 1;
4177 if (skip >= 0 && !skip)
4178 skip = at_most_one_non_fixed(domain, order, n, depth);
4179 if (skip >= 0 && !skip) {
4180 if (isl_ast_build_has_schedule_node(build))
4181 skip = has_anchored_subtree(build);
4182 else
4183 skip = isl_ast_build_options_involve_depth(build);
4185 if (skip < 0)
4186 goto error;
4187 if (skip)
4188 return generate_shifted_component_from_list(domain,
4189 order, n, build);
4191 base = eliminate_non_fixed(domain, order, n, depth, build);
4192 if (base < 0)
4193 goto error;
4195 ctx = isl_ast_build_get_ctx(build);
4197 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4199 fixed = 1;
4200 for (i = 0; i < n; ++i) {
4201 isl_val *r, *m;
4203 map = isl_map_from_domain_and_range(
4204 isl_set_copy(domain[order[base]].set),
4205 isl_set_copy(domain[order[i]].set));
4206 for (d = 0; d < depth; ++d)
4207 map = isl_map_equate(map, isl_dim_in, d,
4208 isl_dim_out, d);
4209 deltas = isl_map_deltas(map);
4210 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4211 isl_set_free(deltas);
4212 if (res < 0)
4213 break;
4215 if (i == 0)
4216 gcd = m;
4217 else
4218 gcd = isl_val_gcd(gcd, m);
4219 if (isl_val_is_one(gcd)) {
4220 isl_val_free(r);
4221 break;
4223 mv = isl_multi_val_set_val(mv, i, r);
4225 res = dim_is_fixed(domain[order[i]].set, depth);
4226 if (res < 0)
4227 break;
4228 if (res)
4229 continue;
4231 if (fixed && i > base) {
4232 isl_val *a, *b;
4233 a = isl_multi_val_get_val(mv, i);
4234 b = isl_multi_val_get_val(mv, base);
4235 if (isl_val_ne(a, b))
4236 fixed = 0;
4237 isl_val_free(a);
4238 isl_val_free(b);
4242 if (res < 0 || !gcd) {
4243 isl_ast_build_free(build);
4244 list = NULL;
4245 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4246 list = generate_shifted_component_from_list(domain,
4247 order, n, build);
4248 } else {
4249 list = generate_shift_component(domain, order, n, gcd, mv,
4250 build);
4253 isl_val_free(gcd);
4254 isl_multi_val_free(mv);
4256 return list;
4257 error:
4258 isl_ast_build_free(build);
4259 return NULL;
4262 /* Store both "map" itself and its domain in the
4263 * structure pointed to by *next and advance to the next array element.
4265 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4267 struct isl_set_map_pair **next = user;
4269 (*next)->map = isl_map_copy(map);
4270 (*next)->set = isl_map_domain(map);
4271 (*next)++;
4273 return isl_stat_ok;
4276 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4277 __isl_keep isl_schedule_node *node);
4279 /* Is any domain element of "umap" scheduled after any of
4280 * the corresponding image elements by the tree rooted at
4281 * the child of "node"?
4283 static isl_bool after_in_child(__isl_keep isl_union_map *umap,
4284 __isl_keep isl_schedule_node *node)
4286 isl_schedule_node *child;
4287 isl_bool after;
4289 child = isl_schedule_node_get_child(node, 0);
4290 after = after_in_tree(umap, child);
4291 isl_schedule_node_free(child);
4293 return after;
4296 /* Is any domain element of "umap" scheduled after any of
4297 * the corresponding image elements by the tree rooted at
4298 * the band node "node"?
4300 * We first check if any domain element is scheduled after any
4301 * of the corresponding image elements by the band node itself.
4302 * If not, we restrict "map" to those pairs of element that
4303 * are scheduled together by the band node and continue with
4304 * the child of the band node.
4305 * If there are no such pairs then the map passed to after_in_child
4306 * will be empty causing it to return 0.
4308 static isl_bool after_in_band(__isl_keep isl_union_map *umap,
4309 __isl_keep isl_schedule_node *node)
4311 isl_multi_union_pw_aff *mupa;
4312 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4313 isl_union_set *domain, *range;
4314 isl_space *space;
4315 isl_bool empty;
4316 isl_bool after;
4317 isl_size n;
4319 n = isl_schedule_node_band_n_member(node);
4320 if (n < 0)
4321 return isl_bool_error;
4322 if (n == 0)
4323 return after_in_child(umap, node);
4325 mupa = isl_schedule_node_band_get_partial_schedule(node);
4326 space = isl_multi_union_pw_aff_get_space(mupa);
4327 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4328 test = isl_union_map_copy(umap);
4329 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4330 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4331 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4332 test = isl_union_map_intersect(test, gt);
4333 empty = isl_union_map_is_empty(test);
4334 isl_union_map_free(test);
4336 if (empty < 0 || !empty) {
4337 isl_union_map_free(partial);
4338 return isl_bool_not(empty);
4341 universe = isl_union_map_universe(isl_union_map_copy(umap));
4342 domain = isl_union_map_domain(isl_union_map_copy(universe));
4343 range = isl_union_map_range(universe);
4344 umap1 = isl_union_map_copy(partial);
4345 umap1 = isl_union_map_intersect_domain(umap1, domain);
4346 umap2 = isl_union_map_intersect_domain(partial, range);
4347 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4348 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4349 after = after_in_child(test, node);
4350 isl_union_map_free(test);
4351 return after;
4354 /* Is any domain element of "umap" scheduled after any of
4355 * the corresponding image elements by the tree rooted at
4356 * the context node "node"?
4358 * The context constraints apply to the schedule domain,
4359 * so we cannot apply them directly to "umap", which contains
4360 * pairs of statement instances. Instead, we add them
4361 * to the range of the prefix schedule for both domain and
4362 * range of "umap".
4364 static isl_bool after_in_context(__isl_keep isl_union_map *umap,
4365 __isl_keep isl_schedule_node *node)
4367 isl_union_map *prefix, *universe, *umap1, *umap2;
4368 isl_union_set *domain, *range;
4369 isl_set *context;
4370 isl_bool after;
4372 umap = isl_union_map_copy(umap);
4373 context = isl_schedule_node_context_get_context(node);
4374 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4375 universe = isl_union_map_universe(isl_union_map_copy(umap));
4376 domain = isl_union_map_domain(isl_union_map_copy(universe));
4377 range = isl_union_map_range(universe);
4378 umap1 = isl_union_map_copy(prefix);
4379 umap1 = isl_union_map_intersect_domain(umap1, domain);
4380 umap2 = isl_union_map_intersect_domain(prefix, range);
4381 umap1 = isl_union_map_intersect_range(umap1,
4382 isl_union_set_from_set(context));
4383 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4384 umap = isl_union_map_intersect(umap, umap1);
4386 after = after_in_child(umap, node);
4388 isl_union_map_free(umap);
4390 return after;
4393 /* Is any domain element of "umap" scheduled after any of
4394 * the corresponding image elements by the tree rooted at
4395 * the expansion node "node"?
4397 * We apply the expansion to domain and range of "umap" and
4398 * continue with its child.
4400 static isl_bool after_in_expansion(__isl_keep isl_union_map *umap,
4401 __isl_keep isl_schedule_node *node)
4403 isl_union_map *expansion;
4404 isl_bool after;
4406 expansion = isl_schedule_node_expansion_get_expansion(node);
4407 umap = isl_union_map_copy(umap);
4408 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4409 umap = isl_union_map_apply_range(umap, expansion);
4411 after = after_in_child(umap, node);
4413 isl_union_map_free(umap);
4415 return after;
4418 /* Is any domain element of "umap" scheduled after any of
4419 * the corresponding image elements by the tree rooted at
4420 * the extension node "node"?
4422 * Since the extension node may add statement instances before or
4423 * after the pairs of statement instances in "umap", we return isl_bool_true
4424 * to ensure that these pairs are not broken up.
4426 static isl_bool after_in_extension(__isl_keep isl_union_map *umap,
4427 __isl_keep isl_schedule_node *node)
4429 return isl_bool_true;
4432 /* Is any domain element of "umap" scheduled after any of
4433 * the corresponding image elements by the tree rooted at
4434 * the filter node "node"?
4436 * We intersect domain and range of "umap" with the filter and
4437 * continue with its child.
4439 static isl_bool after_in_filter(__isl_keep isl_union_map *umap,
4440 __isl_keep isl_schedule_node *node)
4442 isl_union_set *filter;
4443 isl_bool after;
4445 umap = isl_union_map_copy(umap);
4446 filter = isl_schedule_node_filter_get_filter(node);
4447 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4448 umap = isl_union_map_intersect_range(umap, filter);
4450 after = after_in_child(umap, node);
4452 isl_union_map_free(umap);
4454 return after;
4457 /* Is any domain element of "umap" scheduled after any of
4458 * the corresponding image elements by the tree rooted at
4459 * the set node "node"?
4461 * This is only the case if this condition holds in any
4462 * of the (filter) children of the set node.
4463 * In particular, if the domain and the range of "umap"
4464 * are contained in different children, then the condition
4465 * does not hold.
4467 static isl_bool after_in_set(__isl_keep isl_union_map *umap,
4468 __isl_keep isl_schedule_node *node)
4470 int i;
4471 isl_size n;
4473 n = isl_schedule_node_n_children(node);
4474 if (n < 0)
4475 return isl_bool_error;
4476 for (i = 0; i < n; ++i) {
4477 isl_schedule_node *child;
4478 isl_bool after;
4480 child = isl_schedule_node_get_child(node, i);
4481 after = after_in_tree(umap, child);
4482 isl_schedule_node_free(child);
4484 if (after < 0 || after)
4485 return after;
4488 return isl_bool_false;
4491 /* Return the filter of child "i" of "node".
4493 static __isl_give isl_union_set *child_filter(
4494 __isl_keep isl_schedule_node *node, int i)
4496 isl_schedule_node *child;
4497 isl_union_set *filter;
4499 child = isl_schedule_node_get_child(node, i);
4500 filter = isl_schedule_node_filter_get_filter(child);
4501 isl_schedule_node_free(child);
4503 return filter;
4506 /* Is any domain element of "umap" scheduled after any of
4507 * the corresponding image elements by the tree rooted at
4508 * the sequence node "node"?
4510 * This happens in particular if any domain element is
4511 * contained in a later child than one containing a range element or
4512 * if the condition holds within a given child in the sequence.
4513 * The later part of the condition is checked by after_in_set.
4515 static isl_bool after_in_sequence(__isl_keep isl_union_map *umap,
4516 __isl_keep isl_schedule_node *node)
4518 int i, j;
4519 isl_size n;
4520 isl_union_map *umap_i;
4521 isl_bool empty;
4522 isl_bool after = isl_bool_false;
4524 n = isl_schedule_node_n_children(node);
4525 if (n < 0)
4526 return isl_bool_error;
4527 for (i = 1; i < n; ++i) {
4528 isl_union_set *filter_i;
4530 umap_i = isl_union_map_copy(umap);
4531 filter_i = child_filter(node, i);
4532 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4533 empty = isl_union_map_is_empty(umap_i);
4534 if (empty < 0)
4535 goto error;
4536 if (empty) {
4537 isl_union_map_free(umap_i);
4538 continue;
4541 for (j = 0; j < i; ++j) {
4542 isl_union_set *filter_j;
4543 isl_union_map *umap_ij;
4545 umap_ij = isl_union_map_copy(umap_i);
4546 filter_j = child_filter(node, j);
4547 umap_ij = isl_union_map_intersect_range(umap_ij,
4548 filter_j);
4549 empty = isl_union_map_is_empty(umap_ij);
4550 isl_union_map_free(umap_ij);
4552 if (empty < 0)
4553 goto error;
4554 if (!empty)
4555 after = isl_bool_true;
4556 if (after)
4557 break;
4560 isl_union_map_free(umap_i);
4561 if (after)
4562 break;
4565 if (after < 0 || after)
4566 return after;
4568 return after_in_set(umap, node);
4569 error:
4570 isl_union_map_free(umap_i);
4571 return isl_bool_error;
4574 /* Is any domain element of "umap" scheduled after any of
4575 * the corresponding image elements by the tree rooted at "node"?
4577 * If "umap" is empty, then clearly there is no such element.
4578 * Otherwise, consider the different types of nodes separately.
4580 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4581 __isl_keep isl_schedule_node *node)
4583 isl_bool empty;
4584 enum isl_schedule_node_type type;
4586 empty = isl_union_map_is_empty(umap);
4587 if (empty < 0)
4588 return isl_bool_error;
4589 if (empty)
4590 return isl_bool_false;
4591 if (!node)
4592 return isl_bool_error;
4594 type = isl_schedule_node_get_type(node);
4595 switch (type) {
4596 case isl_schedule_node_error:
4597 return isl_bool_error;
4598 case isl_schedule_node_leaf:
4599 return isl_bool_false;
4600 case isl_schedule_node_band:
4601 return after_in_band(umap, node);
4602 case isl_schedule_node_domain:
4603 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4604 "unexpected internal domain node",
4605 return isl_bool_error);
4606 case isl_schedule_node_context:
4607 return after_in_context(umap, node);
4608 case isl_schedule_node_expansion:
4609 return after_in_expansion(umap, node);
4610 case isl_schedule_node_extension:
4611 return after_in_extension(umap, node);
4612 case isl_schedule_node_filter:
4613 return after_in_filter(umap, node);
4614 case isl_schedule_node_guard:
4615 case isl_schedule_node_mark:
4616 return after_in_child(umap, node);
4617 case isl_schedule_node_set:
4618 return after_in_set(umap, node);
4619 case isl_schedule_node_sequence:
4620 return after_in_sequence(umap, node);
4623 return isl_bool_true;
4626 /* Is any domain element of "map1" scheduled after any domain
4627 * element of "map2" by the subtree underneath the current band node,
4628 * while at the same time being scheduled together by the current
4629 * band node, i.e., by "map1" and "map2?
4631 * If the child of the current band node is a leaf, then
4632 * no element can be scheduled after any other element.
4634 * Otherwise, we construct a relation between domain elements
4635 * of "map1" and domain elements of "map2" that are scheduled
4636 * together and then check if the subtree underneath the current
4637 * band node determines their relative order.
4639 static isl_bool after_in_subtree(__isl_keep isl_ast_build *build,
4640 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4642 isl_schedule_node *node;
4643 isl_map *map;
4644 isl_union_map *umap;
4645 isl_bool after;
4647 node = isl_ast_build_get_schedule_node(build);
4648 if (!node)
4649 return isl_bool_error;
4650 node = isl_schedule_node_child(node, 0);
4651 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4652 isl_schedule_node_free(node);
4653 return isl_bool_false;
4655 map = isl_map_copy(map2);
4656 map = isl_map_apply_domain(map, isl_map_copy(map1));
4657 umap = isl_union_map_from_map(map);
4658 after = after_in_tree(umap, node);
4659 isl_union_map_free(umap);
4660 isl_schedule_node_free(node);
4661 return after;
4664 /* Internal data for any_scheduled_after.
4666 * "build" is the build in which the AST is constructed.
4667 * "depth" is the number of loops that have already been generated
4668 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4669 * "domain" is an array of set-map pairs corresponding to the different
4670 * iteration domains. The set is the schedule domain, i.e., the domain
4671 * of the inverse schedule, while the map is the inverse schedule itself.
4673 struct isl_any_scheduled_after_data {
4674 isl_ast_build *build;
4675 int depth;
4676 int group_coscheduled;
4677 struct isl_set_map_pair *domain;
4680 /* Is any element of domain "i" scheduled after any element of domain "j"
4681 * (for a common iteration of the first data->depth loops)?
4683 * data->domain[i].set contains the domain of the inverse schedule
4684 * for domain "i", i.e., elements in the schedule domain.
4686 * If we are inside a band of a schedule tree and there is a pair
4687 * of elements in the two domains that is schedule together by
4688 * the current band, then we check if any element of "i" may be schedule
4689 * after element of "j" by the descendants of the band node.
4691 * If data->group_coscheduled is set, then we also return 1 if there
4692 * is any pair of elements in the two domains that are scheduled together.
4694 static isl_bool any_scheduled_after(int i, int j, void *user)
4696 struct isl_any_scheduled_after_data *data = user;
4697 isl_size dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4698 int pos;
4700 if (dim < 0)
4701 return isl_bool_error;
4703 for (pos = data->depth; pos < dim; ++pos) {
4704 int follows;
4706 follows = isl_set_follows_at(data->domain[i].set,
4707 data->domain[j].set, pos);
4709 if (follows < -1)
4710 return isl_bool_error;
4711 if (follows > 0)
4712 return isl_bool_true;
4713 if (follows < 0)
4714 return isl_bool_false;
4717 if (isl_ast_build_has_schedule_node(data->build)) {
4718 isl_bool after;
4720 after = after_in_subtree(data->build, data->domain[i].map,
4721 data->domain[j].map);
4722 if (after < 0 || after)
4723 return after;
4726 return isl_bool_ok(data->group_coscheduled);
4729 /* Look for independent components at the current depth and generate code
4730 * for each component separately. The resulting lists of grafts are
4731 * merged in an attempt to combine grafts with identical guards.
4733 * Code for two domains can be generated separately if all the elements
4734 * of one domain are scheduled before (or together with) all the elements
4735 * of the other domain. We therefore consider the graph with as nodes
4736 * the domains and an edge between two nodes if any element of the first
4737 * node is scheduled after any element of the second node.
4738 * If the ast_build_group_coscheduled is set, then we also add an edge if
4739 * there is any pair of elements in the two domains that are scheduled
4740 * together.
4741 * Code is then generated (by generate_component)
4742 * for each of the strongly connected components in this graph
4743 * in their topological order.
4745 * Since the test is performed on the domain of the inverse schedules of
4746 * the different domains, we precompute these domains and store
4747 * them in data.domain.
4749 static __isl_give isl_ast_graft_list *generate_components(
4750 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4752 int i;
4753 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4754 isl_size n = isl_union_map_n_map(executed);
4755 struct isl_any_scheduled_after_data data;
4756 struct isl_set_map_pair *next;
4757 struct isl_tarjan_graph *g = NULL;
4758 isl_ast_graft_list *list = NULL;
4759 int n_domain = 0;
4761 data.domain = NULL;
4762 if (n < 0)
4763 goto error;
4764 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4765 if (!data.domain)
4766 goto error;
4767 n_domain = n;
4769 next = data.domain;
4770 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4771 goto error;
4773 if (!build)
4774 goto error;
4775 data.build = build;
4776 data.depth = isl_ast_build_get_depth(build);
4777 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4778 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4779 if (!g)
4780 goto error;
4782 list = isl_ast_graft_list_alloc(ctx, 0);
4784 i = 0;
4785 while (list && n) {
4786 isl_ast_graft_list *list_c;
4787 int first = i;
4789 if (g->order[i] == -1)
4790 isl_die(ctx, isl_error_internal, "cannot happen",
4791 goto error);
4792 ++i; --n;
4793 while (g->order[i] != -1) {
4794 ++i; --n;
4797 list_c = generate_component(data.domain,
4798 g->order + first, i - first,
4799 isl_ast_build_copy(build));
4800 list = isl_ast_graft_list_merge(list, list_c, build);
4802 ++i;
4805 if (0)
4806 error: list = isl_ast_graft_list_free(list);
4807 isl_tarjan_graph_free(g);
4808 for (i = 0; i < n_domain; ++i) {
4809 isl_map_free(data.domain[i].map);
4810 isl_set_free(data.domain[i].set);
4812 free(data.domain);
4813 isl_union_map_free(executed);
4814 isl_ast_build_free(build);
4816 return list;
4819 /* Generate code for the next level (and all inner levels).
4821 * If "executed" is empty, i.e., no code needs to be generated,
4822 * then we return an empty list.
4824 * If we have already generated code for all loop levels, then we pass
4825 * control to generate_inner_level.
4827 * If "executed" lives in a single space, i.e., if code needs to be
4828 * generated for a single domain, then there can only be a single
4829 * component and we go directly to generate_shifted_component.
4830 * Otherwise, we call generate_components to detect the components
4831 * and to call generate_component on each of them separately.
4833 static __isl_give isl_ast_graft_list *generate_next_level(
4834 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4836 int depth;
4837 isl_size dim;
4838 isl_size n;
4840 if (!build || !executed)
4841 goto error;
4843 if (isl_union_map_is_empty(executed)) {
4844 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4845 isl_union_map_free(executed);
4846 isl_ast_build_free(build);
4847 return isl_ast_graft_list_alloc(ctx, 0);
4850 depth = isl_ast_build_get_depth(build);
4851 dim = isl_ast_build_dim(build, isl_dim_set);
4852 if (dim < 0)
4853 goto error;
4854 if (depth >= dim)
4855 return generate_inner_level(executed, build);
4857 n = isl_union_map_n_map(executed);
4858 if (n < 0)
4859 goto error;
4860 if (n == 1)
4861 return generate_shifted_component(executed, build);
4863 return generate_components(executed, build);
4864 error:
4865 isl_union_map_free(executed);
4866 isl_ast_build_free(build);
4867 return NULL;
4870 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4871 * internal, executed and build are the inputs to generate_code.
4872 * list collects the output.
4874 struct isl_generate_code_data {
4875 int internal;
4876 isl_union_map *executed;
4877 isl_ast_build *build;
4879 isl_ast_graft_list *list;
4882 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4884 * [E -> S] -> D
4886 * with E the external build schedule and S the additional schedule "space",
4887 * reformulate the inverse schedule in terms of the internal schedule domain,
4888 * i.e., return
4890 * [I -> S] -> D
4892 * We first obtain a mapping
4894 * I -> E
4896 * take the inverse and the product with S -> S, resulting in
4898 * [I -> S] -> [E -> S]
4900 * Applying the map to the input produces the desired result.
4902 static __isl_give isl_union_map *internal_executed(
4903 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4904 __isl_keep isl_ast_build *build)
4906 isl_map *id, *proj;
4908 proj = isl_ast_build_get_schedule_map(build);
4909 proj = isl_map_reverse(proj);
4910 space = isl_space_map_from_set(isl_space_copy(space));
4911 id = isl_map_identity(space);
4912 proj = isl_map_product(proj, id);
4913 executed = isl_union_map_apply_domain(executed,
4914 isl_union_map_from_map(proj));
4915 return executed;
4918 /* Generate an AST that visits the elements in the range of data->executed
4919 * in the relative order specified by the corresponding domain element(s)
4920 * for those domain elements that belong to "set".
4921 * Add the result to data->list.
4923 * The caller ensures that "set" is a universe domain.
4924 * "space" is the space of the additional part of the schedule.
4925 * It is equal to the space of "set" if build->domain is parametric.
4926 * Otherwise, it is equal to the range of the wrapped space of "set".
4928 * If the build space is not parametric and
4929 * if isl_ast_build_node_from_schedule_map
4930 * was called from an outside user (data->internal not set), then
4931 * the (inverse) schedule refers to the external build domain and needs to
4932 * be transformed to refer to the internal build domain.
4934 * If the build space is parametric, then we add some of the parameter
4935 * constraints to the executed relation. Adding these constraints
4936 * allows for an earlier detection of conflicts in some cases.
4937 * However, we do not want to divide the executed relation into
4938 * more disjuncts than necessary. We therefore approximate
4939 * the constraints on the parameters by a single disjunct set.
4941 * The build is extended to include the additional part of the schedule.
4942 * If the original build space was not parametric, then the options
4943 * in data->build refer only to the additional part of the schedule
4944 * and they need to be adjusted to refer to the complete AST build
4945 * domain.
4947 * After having adjusted inverse schedule and build, we start generating
4948 * code with the outer loop of the current code generation
4949 * in generate_next_level.
4951 * If the original build space was not parametric, we undo the embedding
4952 * on the resulting isl_ast_node_list so that it can be used within
4953 * the outer AST build.
4955 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4956 __isl_take isl_set *set, __isl_take isl_space *space)
4958 isl_union_map *executed;
4959 isl_ast_build *build;
4960 isl_ast_graft_list *list;
4961 int embed;
4963 executed = isl_union_map_copy(data->executed);
4964 executed = isl_union_map_intersect_domain(executed,
4965 isl_union_set_from_set(set));
4967 embed = !isl_set_is_params(data->build->domain);
4968 if (embed && !data->internal)
4969 executed = internal_executed(executed, space, data->build);
4970 if (!embed) {
4971 isl_set *domain;
4972 domain = isl_ast_build_get_domain(data->build);
4973 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4974 executed = isl_union_map_intersect_params(executed, domain);
4977 build = isl_ast_build_copy(data->build);
4978 build = isl_ast_build_product(build, space);
4980 list = generate_next_level(executed, build);
4982 list = isl_ast_graft_list_unembed(list, embed);
4984 data->list = isl_ast_graft_list_concat(data->list, list);
4986 return isl_stat_ok;
4989 /* Generate an AST that visits the elements in the range of data->executed
4990 * in the relative order specified by the corresponding domain element(s)
4991 * for those domain elements that belong to "set".
4992 * Add the result to data->list.
4994 * The caller ensures that "set" is a universe domain.
4996 * If the build space S is not parametric, then the space of "set"
4997 * need to be a wrapped relation with S as domain. That is, it needs
4998 * to be of the form
5000 * [S -> T]
5002 * Check this property and pass control to generate_code_in_space
5003 * passing along T.
5004 * If the build space is not parametric, then T is the space of "set".
5006 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
5008 struct isl_generate_code_data *data = user;
5009 isl_space *space, *build_space;
5010 int is_domain;
5012 space = isl_set_get_space(set);
5014 if (isl_set_is_params(data->build->domain))
5015 return generate_code_in_space(data, set, space);
5017 build_space = isl_ast_build_get_space(data->build, data->internal);
5018 space = isl_space_unwrap(space);
5019 is_domain = isl_space_is_domain(build_space, space);
5020 isl_space_free(build_space);
5021 space = isl_space_range(space);
5023 if (is_domain < 0)
5024 goto error;
5025 if (!is_domain)
5026 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5027 "invalid nested schedule space", goto error);
5029 return generate_code_in_space(data, set, space);
5030 error:
5031 isl_set_free(set);
5032 isl_space_free(space);
5033 return isl_stat_error;
5036 /* Generate an AST that visits the elements in the range of "executed"
5037 * in the relative order specified by the corresponding domain element(s).
5039 * "build" is an isl_ast_build that has either been constructed by
5040 * isl_ast_build_from_context or passed to a callback set by
5041 * isl_ast_build_set_create_leaf.
5042 * In the first case, the space of the isl_ast_build is typically
5043 * a parametric space, although this is currently not enforced.
5044 * In the second case, the space is never a parametric space.
5045 * If the space S is not parametric, then the domain space(s) of "executed"
5046 * need to be wrapped relations with S as domain.
5048 * If the domain of "executed" consists of several spaces, then an AST
5049 * is generated for each of them (in arbitrary order) and the results
5050 * are concatenated.
5052 * If "internal" is set, then the domain "S" above refers to the internal
5053 * schedule domain representation. Otherwise, it refers to the external
5054 * representation, as returned by isl_ast_build_get_schedule_space.
5056 * We essentially run over all the spaces in the domain of "executed"
5057 * and call generate_code_set on each of them.
5059 static __isl_give isl_ast_graft_list *generate_code(
5060 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
5061 int internal)
5063 isl_ctx *ctx;
5064 struct isl_generate_code_data data = { 0 };
5065 isl_space *space;
5066 isl_union_set *schedule_domain;
5067 isl_union_map *universe;
5069 if (!build)
5070 goto error;
5071 space = isl_ast_build_get_space(build, 1);
5072 space = isl_space_align_params(space,
5073 isl_union_map_get_space(executed));
5074 space = isl_space_align_params(space,
5075 isl_union_map_get_space(build->options));
5076 build = isl_ast_build_align_params(build, isl_space_copy(space));
5077 executed = isl_union_map_align_params(executed, space);
5078 if (!executed || !build)
5079 goto error;
5081 ctx = isl_ast_build_get_ctx(build);
5083 data.internal = internal;
5084 data.executed = executed;
5085 data.build = build;
5086 data.list = isl_ast_graft_list_alloc(ctx, 0);
5088 universe = isl_union_map_universe(isl_union_map_copy(executed));
5089 schedule_domain = isl_union_map_domain(universe);
5090 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5091 &data) < 0)
5092 data.list = isl_ast_graft_list_free(data.list);
5094 isl_union_set_free(schedule_domain);
5095 isl_union_map_free(executed);
5097 isl_ast_build_free(build);
5098 return data.list;
5099 error:
5100 isl_union_map_free(executed);
5101 isl_ast_build_free(build);
5102 return NULL;
5105 /* Generate an AST that visits the elements in the domain of "schedule"
5106 * in the relative order specified by the corresponding image element(s).
5108 * "build" is an isl_ast_build that has either been constructed by
5109 * isl_ast_build_from_context or passed to a callback set by
5110 * isl_ast_build_set_create_leaf.
5111 * In the first case, the space of the isl_ast_build is typically
5112 * a parametric space, although this is currently not enforced.
5113 * In the second case, the space is never a parametric space.
5114 * If the space S is not parametric, then the range space(s) of "schedule"
5115 * need to be wrapped relations with S as domain.
5117 * If the range of "schedule" consists of several spaces, then an AST
5118 * is generated for each of them (in arbitrary order) and the results
5119 * are concatenated.
5121 * We first initialize the local copies of the relevant options.
5122 * We do this here rather than when the isl_ast_build is created
5123 * because the options may have changed between the construction
5124 * of the isl_ast_build and the call to isl_generate_code.
5126 * The main computation is performed on an inverse schedule (with
5127 * the schedule domain in the domain and the elements to be executed
5128 * in the range) called "executed".
5130 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5131 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5133 isl_ast_graft_list *list;
5134 isl_ast_node *node;
5135 isl_union_map *executed;
5137 build = isl_ast_build_copy(build);
5138 build = isl_ast_build_set_single_valued(build, 0);
5139 schedule = isl_union_map_coalesce(schedule);
5140 schedule = isl_union_map_remove_redundancies(schedule);
5141 executed = isl_union_map_reverse(schedule);
5142 list = generate_code(executed, isl_ast_build_copy(build), 0);
5143 node = isl_ast_node_from_graft_list(list, build);
5144 isl_ast_build_free(build);
5146 return node;
5149 /* The old name for isl_ast_build_node_from_schedule_map.
5150 * It is being kept for backward compatibility, but
5151 * it will be removed in the future.
5153 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5154 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5156 return isl_ast_build_node_from_schedule_map(build, schedule);
5159 /* Generate an AST that visits the elements in the domain of "executed"
5160 * in the relative order specified by the leaf node "node".
5162 * The relation "executed" maps the outer generated loop iterators
5163 * to the domain elements executed by those iterations.
5165 * Simply pass control to generate_inner_level.
5166 * Note that the current build does not refer to any band node, so
5167 * that generate_inner_level will not try to visit the child of
5168 * the leaf node.
5170 * If multiple statement instances reach a leaf,
5171 * then they can be executed in any order.
5172 * Group the list of grafts based on shared guards
5173 * such that identical guards are only generated once
5174 * when the list is eventually passed on to isl_ast_graft_list_fuse.
5176 static __isl_give isl_ast_graft_list *build_ast_from_leaf(
5177 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5178 __isl_take isl_union_map *executed)
5180 isl_ast_graft_list *list;
5182 isl_schedule_node_free(node);
5183 list = generate_inner_level(executed, isl_ast_build_copy(build));
5184 list = isl_ast_graft_list_group_on_guard(list, build);
5185 isl_ast_build_free(build);
5187 return list;
5190 /* Generate an AST that visits the elements in the domain of "executed"
5191 * in the relative order specified by the band node "node" and its descendants.
5193 * The relation "executed" maps the outer generated loop iterators
5194 * to the domain elements executed by those iterations.
5196 * If the band is empty, we continue with its descendants.
5197 * Otherwise, we extend the build and the inverse schedule with
5198 * the additional space/partial schedule and continue generating
5199 * an AST in generate_next_level.
5200 * As soon as we have extended the inverse schedule with the additional
5201 * partial schedule, we look for equalities that may exists between
5202 * the old and the new part.
5204 static __isl_give isl_ast_graft_list *build_ast_from_band(
5205 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5206 __isl_take isl_union_map *executed)
5208 isl_space *space;
5209 isl_multi_union_pw_aff *extra;
5210 isl_union_map *extra_umap;
5211 isl_ast_graft_list *list;
5212 isl_size n1, n2;
5213 isl_size n;
5215 n = isl_schedule_node_band_n_member(node);
5216 if (!build || n < 0 || !executed)
5217 goto error;
5219 if (n == 0)
5220 return build_ast_from_child(build, node, executed);
5222 extra = isl_schedule_node_band_get_partial_schedule(node);
5223 extra = isl_multi_union_pw_aff_align_params(extra,
5224 isl_ast_build_get_space(build, 1));
5225 space = isl_multi_union_pw_aff_get_space(extra);
5227 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5228 extra_umap = isl_union_map_reverse(extra_umap);
5230 executed = isl_union_map_domain_product(executed, extra_umap);
5231 executed = isl_union_map_detect_equalities(executed);
5233 n1 = isl_ast_build_dim(build, isl_dim_param);
5234 build = isl_ast_build_product(build, space);
5235 n2 = isl_ast_build_dim(build, isl_dim_param);
5236 if (n1 < 0 || n2 < 0)
5237 build = isl_ast_build_free(build);
5238 else if (n2 > n1)
5239 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5240 "band node is not allowed to introduce new parameters",
5241 build = isl_ast_build_free(build));
5242 build = isl_ast_build_set_schedule_node(build, node);
5244 list = generate_next_level(executed, build);
5246 list = isl_ast_graft_list_unembed(list, 1);
5248 return list;
5249 error:
5250 isl_schedule_node_free(node);
5251 isl_union_map_free(executed);
5252 isl_ast_build_free(build);
5253 return NULL;
5256 /* Hoist a list of grafts (in practice containing a single graft)
5257 * from "sub_build" (which includes extra context information)
5258 * to "build".
5260 * In particular, project out all additional parameters introduced
5261 * by the context node from the enforced constraints and the guard
5262 * of the single graft.
5264 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5265 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5266 __isl_keep isl_ast_build *sub_build)
5268 isl_ast_graft *graft;
5269 isl_basic_set *enforced;
5270 isl_set *guard;
5271 isl_size n_param, extra_param;
5273 n_param = isl_ast_build_dim(build, isl_dim_param);
5274 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5275 if (n_param < 0 || extra_param < 0)
5276 return isl_ast_graft_list_free(list);
5278 if (extra_param == n_param)
5279 return list;
5281 extra_param -= n_param;
5282 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5283 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5284 n_param, extra_param);
5285 enforced = isl_basic_set_remove_unknown_divs(enforced);
5286 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5287 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5288 n_param, extra_param);
5289 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5290 guard = isl_set_compute_divs(guard);
5291 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5292 build, sub_build);
5293 list = isl_ast_graft_list_from_ast_graft(graft);
5295 return list;
5298 /* Generate an AST that visits the elements in the domain of "executed"
5299 * in the relative order specified by the context node "node"
5300 * and its descendants.
5302 * The relation "executed" maps the outer generated loop iterators
5303 * to the domain elements executed by those iterations.
5305 * The context node may introduce additional parameters as well as
5306 * constraints on the outer schedule dimensions or original parameters.
5308 * We add the extra parameters to a new build and the context
5309 * constraints to both the build and (as a single disjunct)
5310 * to the domain of "executed". Since the context constraints
5311 * are specified in terms of the input schedule, we first need
5312 * to map them to the internal schedule domain.
5314 * After constructing the AST from the descendants of "node",
5315 * we combine the list of grafts into a single graft within
5316 * the new build, in order to be able to exploit the additional
5317 * context constraints during this combination.
5319 * Additionally, if the current node is the outermost node in
5320 * the schedule tree (apart from the root domain node), we generate
5321 * all pending guards, again to be able to exploit the additional
5322 * context constraints. We currently do not do this for internal
5323 * context nodes since we may still want to hoist conditions
5324 * to outer AST nodes.
5326 * If the context node introduced any new parameters, then they
5327 * are removed from the set of enforced constraints and guard
5328 * in hoist_out_of_context.
5330 static __isl_give isl_ast_graft_list *build_ast_from_context(
5331 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5332 __isl_take isl_union_map *executed)
5334 isl_set *context;
5335 isl_space *space;
5336 isl_multi_aff *internal2input;
5337 isl_ast_build *sub_build;
5338 isl_ast_graft_list *list;
5339 isl_size n;
5340 isl_size depth;
5342 depth = isl_schedule_node_get_tree_depth(node);
5343 if (depth < 0)
5344 build = isl_ast_build_free(build);
5345 space = isl_ast_build_get_space(build, 1);
5346 context = isl_schedule_node_context_get_context(node);
5347 context = isl_set_align_params(context, space);
5348 sub_build = isl_ast_build_copy(build);
5349 space = isl_set_get_space(context);
5350 sub_build = isl_ast_build_align_params(sub_build, space);
5351 internal2input = isl_ast_build_get_internal2input(sub_build);
5352 context = isl_set_preimage_multi_aff(context, internal2input);
5353 sub_build = isl_ast_build_restrict_generated(sub_build,
5354 isl_set_copy(context));
5355 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5356 executed = isl_union_map_intersect_domain(executed,
5357 isl_union_set_from_set(context));
5359 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5360 node, executed);
5361 n = isl_ast_graft_list_n_ast_graft(list);
5362 if (n < 0)
5363 list = isl_ast_graft_list_free(list);
5365 list = isl_ast_graft_list_fuse(list, sub_build);
5366 if (depth == 1)
5367 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5368 sub_build);
5369 if (n >= 1)
5370 list = hoist_out_of_context(list, build, sub_build);
5372 isl_ast_build_free(build);
5373 isl_ast_build_free(sub_build);
5375 return list;
5378 /* Generate an AST that visits the elements in the domain of "executed"
5379 * in the relative order specified by the expansion node "node" and
5380 * its descendants.
5382 * The relation "executed" maps the outer generated loop iterators
5383 * to the domain elements executed by those iterations.
5385 * We expand the domain elements by the expansion and
5386 * continue with the descendants of the node.
5388 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5389 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5390 __isl_take isl_union_map *executed)
5392 isl_union_map *expansion;
5393 isl_size n1, n2;
5395 expansion = isl_schedule_node_expansion_get_expansion(node);
5396 expansion = isl_union_map_align_params(expansion,
5397 isl_union_map_get_space(executed));
5399 n1 = isl_union_map_dim(executed, isl_dim_param);
5400 executed = isl_union_map_apply_range(executed, expansion);
5401 n2 = isl_union_map_dim(executed, isl_dim_param);
5402 if (n1 < 0 || n2 < 0)
5403 goto error;
5404 if (n2 > n1)
5405 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5406 "expansion node is not allowed to introduce "
5407 "new parameters", goto error);
5409 return build_ast_from_child(build, node, executed);
5410 error:
5411 isl_ast_build_free(build);
5412 isl_schedule_node_free(node);
5413 isl_union_map_free(executed);
5414 return NULL;
5417 /* Generate an AST that visits the elements in the domain of "executed"
5418 * in the relative order specified by the extension node "node" and
5419 * its descendants.
5421 * The relation "executed" maps the outer generated loop iterators
5422 * to the domain elements executed by those iterations.
5424 * Extend the inverse schedule with the extension applied to current
5425 * set of generated constraints. Since the extension if formulated
5426 * in terms of the input schedule, it first needs to be transformed
5427 * to refer to the internal schedule.
5429 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5430 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5431 __isl_take isl_union_map *executed)
5433 isl_union_set *schedule_domain;
5434 isl_union_map *extension;
5435 isl_set *set;
5437 set = isl_ast_build_get_generated(build);
5438 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5439 schedule_domain = isl_union_set_from_set(set);
5441 extension = isl_schedule_node_extension_get_extension(node);
5443 extension = isl_union_map_preimage_domain_multi_aff(extension,
5444 isl_multi_aff_copy(build->internal2input));
5445 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5446 extension = isl_ast_build_substitute_values_union_map_domain(build,
5447 extension);
5448 executed = isl_union_map_union(executed, extension);
5450 return build_ast_from_child(build, node, executed);
5453 /* Generate an AST that visits the elements in the domain of "executed"
5454 * in the relative order specified by the filter node "node" and
5455 * its descendants.
5457 * The relation "executed" maps the outer generated loop iterators
5458 * to the domain elements executed by those iterations.
5460 * We simply intersect the iteration domain (i.e., the range of "executed")
5461 * with the filter and continue with the descendants of the node,
5462 * unless the resulting inverse schedule is empty, in which
5463 * case we return an empty list.
5465 * If the result of the intersection is equal to the original "executed"
5466 * relation, then keep the original representation since the intersection
5467 * may have unnecessarily broken up the relation into a greater number
5468 * of disjuncts.
5470 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5471 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5472 __isl_take isl_union_map *executed)
5474 isl_ctx *ctx;
5475 isl_union_set *filter;
5476 isl_union_map *orig;
5477 isl_ast_graft_list *list;
5478 int empty;
5479 isl_bool unchanged;
5480 isl_size n1, n2;
5482 orig = isl_union_map_copy(executed);
5483 if (!build || !node || !executed)
5484 goto error;
5486 filter = isl_schedule_node_filter_get_filter(node);
5487 filter = isl_union_set_align_params(filter,
5488 isl_union_map_get_space(executed));
5489 n1 = isl_union_map_dim(executed, isl_dim_param);
5490 executed = isl_union_map_intersect_range(executed, filter);
5491 n2 = isl_union_map_dim(executed, isl_dim_param);
5492 if (n1 < 0 || n2 < 0)
5493 goto error;
5494 if (n2 > n1)
5495 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5496 "filter node is not allowed to introduce "
5497 "new parameters", goto error);
5499 unchanged = isl_union_map_is_subset(orig, executed);
5500 empty = isl_union_map_is_empty(executed);
5501 if (unchanged < 0 || empty < 0)
5502 goto error;
5503 if (unchanged) {
5504 isl_union_map_free(executed);
5505 return build_ast_from_child(build, node, orig);
5507 isl_union_map_free(orig);
5508 if (!empty)
5509 return build_ast_from_child(build, node, executed);
5511 ctx = isl_ast_build_get_ctx(build);
5512 list = isl_ast_graft_list_alloc(ctx, 0);
5513 isl_ast_build_free(build);
5514 isl_schedule_node_free(node);
5515 isl_union_map_free(executed);
5516 return list;
5517 error:
5518 isl_ast_build_free(build);
5519 isl_schedule_node_free(node);
5520 isl_union_map_free(executed);
5521 isl_union_map_free(orig);
5522 return NULL;
5525 /* Generate an AST that visits the elements in the domain of "executed"
5526 * in the relative order specified by the guard node "node" and
5527 * its descendants.
5529 * The relation "executed" maps the outer generated loop iterators
5530 * to the domain elements executed by those iterations.
5532 * Ensure that the associated guard is enforced by the outer AST
5533 * constructs by adding it to the guard of the graft.
5534 * Since we know that we will enforce the guard, we can also include it
5535 * in the generated constraints used to construct an AST for
5536 * the descendant nodes.
5538 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5539 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5540 __isl_take isl_union_map *executed)
5542 isl_space *space;
5543 isl_set *guard, *hoisted;
5544 isl_basic_set *enforced;
5545 isl_ast_build *sub_build;
5546 isl_ast_graft *graft;
5547 isl_ast_graft_list *list;
5548 isl_size n1, n2, n;
5550 space = isl_ast_build_get_space(build, 1);
5551 guard = isl_schedule_node_guard_get_guard(node);
5552 n1 = isl_space_dim(space, isl_dim_param);
5553 guard = isl_set_align_params(guard, space);
5554 n2 = isl_set_dim(guard, isl_dim_param);
5555 if (n1 < 0 || n2 < 0)
5556 guard = isl_set_free(guard);
5557 else if (n2 > n1)
5558 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5559 "guard node is not allowed to introduce "
5560 "new parameters", guard = isl_set_free(guard));
5561 guard = isl_set_preimage_multi_aff(guard,
5562 isl_multi_aff_copy(build->internal2input));
5563 guard = isl_ast_build_specialize(build, guard);
5564 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5566 sub_build = isl_ast_build_copy(build);
5567 sub_build = isl_ast_build_restrict_generated(sub_build,
5568 isl_set_copy(guard));
5570 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5571 node, executed);
5573 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5574 n = isl_set_n_basic_set(hoisted);
5575 if (n < 0)
5576 list = isl_ast_graft_list_free(list);
5577 if (n > 1)
5578 list = isl_ast_graft_list_gist_guards(list,
5579 isl_set_copy(hoisted));
5580 guard = isl_set_intersect(guard, hoisted);
5581 enforced = extract_shared_enforced(list, build);
5582 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5583 build, sub_build);
5585 isl_ast_build_free(sub_build);
5586 isl_ast_build_free(build);
5587 return isl_ast_graft_list_from_ast_graft(graft);
5590 /* Call the before_each_mark callback, if requested by the user.
5592 * Return 0 on success and -1 on error.
5594 * The caller is responsible for recording the current inverse schedule
5595 * in "build".
5597 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5598 __isl_keep isl_ast_build *build)
5600 if (!build)
5601 return isl_stat_error;
5602 if (!build->before_each_mark)
5603 return isl_stat_ok;
5604 return build->before_each_mark(mark, build,
5605 build->before_each_mark_user);
5608 /* Call the after_each_mark callback, if requested by the user.
5610 * The caller is responsible for recording the current inverse schedule
5611 * in "build".
5613 static __isl_give isl_ast_graft *after_each_mark(
5614 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5616 if (!graft || !build)
5617 return isl_ast_graft_free(graft);
5618 if (!build->after_each_mark)
5619 return graft;
5620 graft->node = build->after_each_mark(graft->node, build,
5621 build->after_each_mark_user);
5622 if (!graft->node)
5623 return isl_ast_graft_free(graft);
5624 return graft;
5628 /* Generate an AST that visits the elements in the domain of "executed"
5629 * in the relative order specified by the mark node "node" and
5630 * its descendants.
5632 * The relation "executed" maps the outer generated loop iterators
5633 * to the domain elements executed by those iterations.
5635 * Since we may be calling before_each_mark and after_each_mark
5636 * callbacks, we record the current inverse schedule in the build.
5638 * We generate an AST for the child of the mark node, combine
5639 * the graft list into a single graft and then insert the mark
5640 * in the AST of that single graft.
5642 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5643 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5644 __isl_take isl_union_map *executed)
5646 isl_id *mark;
5647 isl_ast_graft *graft;
5648 isl_ast_graft_list *list;
5649 isl_size n;
5651 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5653 mark = isl_schedule_node_mark_get_id(node);
5654 if (before_each_mark(mark, build) < 0)
5655 node = isl_schedule_node_free(node);
5657 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5658 list = isl_ast_graft_list_fuse(list, build);
5659 n = isl_ast_graft_list_n_ast_graft(list);
5660 if (n < 0)
5661 list = isl_ast_graft_list_free(list);
5662 if (n == 0) {
5663 isl_id_free(mark);
5664 } else {
5665 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5666 graft = isl_ast_graft_insert_mark(graft, mark);
5667 graft = after_each_mark(graft, build);
5668 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5670 isl_ast_build_free(build);
5672 return list;
5675 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5676 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5677 __isl_take isl_union_map *executed);
5679 /* Generate an AST that visits the elements in the domain of "executed"
5680 * in the relative order specified by the sequence (or set) node "node" and
5681 * its descendants.
5683 * The relation "executed" maps the outer generated loop iterators
5684 * to the domain elements executed by those iterations.
5686 * We simply generate an AST for each of the children and concatenate
5687 * the results.
5689 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5690 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5691 __isl_take isl_union_map *executed)
5693 int i;
5694 isl_size n;
5695 isl_ctx *ctx;
5696 isl_ast_graft_list *list;
5698 ctx = isl_ast_build_get_ctx(build);
5699 list = isl_ast_graft_list_alloc(ctx, 0);
5701 n = isl_schedule_node_n_children(node);
5702 if (n < 0)
5703 list = isl_ast_graft_list_free(list);
5704 for (i = 0; i < n; ++i) {
5705 isl_schedule_node *child;
5706 isl_ast_graft_list *list_i;
5708 child = isl_schedule_node_get_child(node, i);
5709 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5710 child, isl_union_map_copy(executed));
5711 list = isl_ast_graft_list_concat(list, list_i);
5713 isl_ast_build_free(build);
5714 isl_schedule_node_free(node);
5715 isl_union_map_free(executed);
5717 return list;
5720 /* Generate an AST that visits the elements in the domain of "executed"
5721 * in the relative order specified by the node "node" and its descendants.
5723 * The relation "executed" maps the outer generated loop iterators
5724 * to the domain elements executed by those iterations.
5726 * The node types are handled in separate functions.
5727 * Set nodes are currently treated in the same way as sequence nodes.
5728 * The children of a set node may be executed in any order,
5729 * including the order of the children.
5731 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5732 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5733 __isl_take isl_union_map *executed)
5735 enum isl_schedule_node_type type;
5737 type = isl_schedule_node_get_type(node);
5739 switch (type) {
5740 case isl_schedule_node_error:
5741 goto error;
5742 case isl_schedule_node_leaf:
5743 return build_ast_from_leaf(build, node, executed);
5744 case isl_schedule_node_band:
5745 return build_ast_from_band(build, node, executed);
5746 case isl_schedule_node_context:
5747 return build_ast_from_context(build, node, executed);
5748 case isl_schedule_node_domain:
5749 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5750 "unexpected internal domain node", goto error);
5751 case isl_schedule_node_expansion:
5752 return build_ast_from_expansion(build, node, executed);
5753 case isl_schedule_node_extension:
5754 return build_ast_from_extension(build, node, executed);
5755 case isl_schedule_node_filter:
5756 return build_ast_from_filter(build, node, executed);
5757 case isl_schedule_node_guard:
5758 return build_ast_from_guard(build, node, executed);
5759 case isl_schedule_node_mark:
5760 return build_ast_from_mark(build, node, executed);
5761 case isl_schedule_node_sequence:
5762 case isl_schedule_node_set:
5763 return build_ast_from_sequence(build, node, executed);
5766 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5767 "unhandled type", goto error);
5768 error:
5769 isl_union_map_free(executed);
5770 isl_schedule_node_free(node);
5771 isl_ast_build_free(build);
5773 return NULL;
5776 /* Generate an AST that visits the elements in the domain of "executed"
5777 * in the relative order specified by the (single) child of "node" and
5778 * its descendants.
5780 * The relation "executed" maps the outer generated loop iterators
5781 * to the domain elements executed by those iterations.
5783 * This function is never called on a leaf, set or sequence node,
5784 * so the node always has exactly one child.
5786 static __isl_give isl_ast_graft_list *build_ast_from_child(
5787 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5788 __isl_take isl_union_map *executed)
5790 node = isl_schedule_node_child(node, 0);
5791 return build_ast_from_schedule_node(build, node, executed);
5794 /* Generate an AST that visits the elements in the domain of the domain
5795 * node "node" in the relative order specified by its descendants.
5797 * An initial inverse schedule is created that maps a zero-dimensional
5798 * schedule space to the node domain.
5799 * The input "build" is assumed to have a parametric domain and
5800 * is replaced by the same zero-dimensional schedule space.
5802 * We also add some of the parameter constraints in the build domain
5803 * to the executed relation. Adding these constraints
5804 * allows for an earlier detection of conflicts in some cases.
5805 * However, we do not want to divide the executed relation into
5806 * more disjuncts than necessary. We therefore approximate
5807 * the constraints on the parameters by a single disjunct set.
5809 static __isl_give isl_ast_node *build_ast_from_domain(
5810 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5812 isl_ctx *ctx;
5813 isl_union_set *domain, *schedule_domain;
5814 isl_union_map *executed;
5815 isl_space *space;
5816 isl_set *set;
5817 isl_ast_graft_list *list;
5818 isl_ast_node *ast;
5819 int is_params;
5821 if (!build)
5822 goto error;
5824 ctx = isl_ast_build_get_ctx(build);
5825 space = isl_ast_build_get_space(build, 1);
5826 is_params = isl_space_is_params(space);
5827 isl_space_free(space);
5828 if (is_params < 0)
5829 goto error;
5830 if (!is_params)
5831 isl_die(ctx, isl_error_unsupported,
5832 "expecting parametric initial context", goto error);
5834 domain = isl_schedule_node_domain_get_domain(node);
5835 domain = isl_union_set_coalesce(domain);
5837 space = isl_union_set_get_space(domain);
5838 space = isl_space_set_from_params(space);
5839 build = isl_ast_build_product(build, space);
5841 set = isl_ast_build_get_domain(build);
5842 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5843 schedule_domain = isl_union_set_from_set(set);
5845 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5846 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5847 ast = isl_ast_node_from_graft_list(list, build);
5848 isl_ast_build_free(build);
5850 return ast;
5851 error:
5852 isl_schedule_node_free(node);
5853 isl_ast_build_free(build);
5854 return NULL;
5857 /* Generate an AST that visits the elements in the domain of "schedule"
5858 * in the relative order specified by the schedule tree.
5860 * "build" is an isl_ast_build that has been created using
5861 * isl_ast_build_alloc or isl_ast_build_from_context based
5862 * on a parametric set.
5864 * The construction starts at the root node of the schedule,
5865 * which is assumed to be a domain node.
5867 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5868 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5870 isl_ctx *ctx;
5871 isl_schedule_node *node;
5873 if (!build || !schedule)
5874 goto error;
5876 ctx = isl_ast_build_get_ctx(build);
5878 node = isl_schedule_get_root(schedule);
5879 if (!node)
5880 goto error;
5881 isl_schedule_free(schedule);
5883 build = isl_ast_build_copy(build);
5884 build = isl_ast_build_set_single_valued(build, 0);
5885 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5886 isl_die(ctx, isl_error_unsupported,
5887 "expecting root domain node",
5888 build = isl_ast_build_free(build));
5889 return build_ast_from_domain(build, node);
5890 error:
5891 isl_schedule_free(schedule);
5892 return NULL;