isl_id_to_id.c: add missing include
[isl.git] / isl_ast_codegen.c
blob5175b89f0d5fc5063ae7fde60e21de000738e596
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_sort.h>
25 #include <isl_tarjan.h>
26 #include <isl_ast_private.h>
27 #include <isl_ast_build_expr.h>
28 #include <isl_ast_build_private.h>
29 #include <isl_ast_graft_private.h>
31 /* Data used in generate_domain.
33 * "build" is the input build.
34 * "list" collects the results.
36 struct isl_generate_domain_data {
37 isl_ast_build *build;
39 isl_ast_graft_list *list;
42 static __isl_give isl_ast_graft_list *generate_next_level(
43 __isl_take isl_union_map *executed,
44 __isl_take isl_ast_build *build);
45 static __isl_give isl_ast_graft_list *generate_code(
46 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
47 int internal);
49 /* Generate an AST for a single domain based on
50 * the (non single valued) inverse schedule "executed".
52 * We extend the schedule with the iteration domain
53 * and continue generating through a call to generate_code.
55 * In particular, if executed has the form
57 * S -> D
59 * then we continue generating code on
61 * [S -> D] -> D
63 * The extended inverse schedule is clearly single valued
64 * ensuring that the nested generate_code will not reach this function,
65 * but will instead create calls to all elements of D that need
66 * to be executed from the current schedule domain.
68 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
69 struct isl_generate_domain_data *data)
71 isl_map *identity;
72 isl_ast_build *build;
73 isl_ast_graft_list *list;
75 build = isl_ast_build_copy(data->build);
77 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
78 executed = isl_map_domain_product(executed, identity);
79 build = isl_ast_build_set_single_valued(build, 1);
81 list = generate_code(isl_union_map_from_map(executed), build, 1);
83 data->list = isl_ast_graft_list_concat(data->list, list);
85 return isl_stat_ok;
88 /* Call the at_each_domain callback, if requested by the user,
89 * after recording the current inverse schedule in the build.
91 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
92 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
94 if (!graft || !build)
95 return isl_ast_graft_free(graft);
96 if (!build->at_each_domain)
97 return graft;
99 build = isl_ast_build_copy(build);
100 build = isl_ast_build_set_executed(build,
101 isl_union_map_from_map(isl_map_copy(executed)));
102 if (!build)
103 return isl_ast_graft_free(graft);
105 graft->node = build->at_each_domain(graft->node,
106 build, build->at_each_domain_user);
107 isl_ast_build_free(build);
109 if (!graft->node)
110 graft = isl_ast_graft_free(graft);
112 return graft;
115 /* Generate a call expression for the single executed
116 * domain element "map" and put a guard around it based its (simplified)
117 * domain. "executed" is the original inverse schedule from which "map"
118 * has been derived. In particular, "map" is either identical to "executed"
119 * or it is the result of gisting "executed" with respect to the build domain.
120 * "executed" is only used if there is an at_each_domain callback.
122 * At this stage, any pending constraints in the build can no longer
123 * be simplified with respect to any enforced constraints since
124 * the call node does not have any enforced constraints.
125 * Since all pending constraints not covered by any enforced constraints
126 * will be added as a guard to the graft in create_node_scaled,
127 * even in the eliminated case, the pending constraints
128 * can be considered to have been generated by outer constructs.
130 * If the user has set an at_each_domain callback, it is called
131 * on the constructed call expression node.
133 static isl_stat add_domain(__isl_take isl_map *executed,
134 __isl_take isl_map *map, struct isl_generate_domain_data *data)
136 isl_ast_build *build;
137 isl_ast_graft *graft;
138 isl_ast_graft_list *list;
139 isl_set *guard, *pending;
141 build = isl_ast_build_copy(data->build);
142 pending = isl_ast_build_get_pending(build);
143 build = isl_ast_build_replace_pending_by_guard(build, pending);
145 guard = isl_map_domain(isl_map_copy(map));
146 guard = isl_set_compute_divs(guard);
147 guard = isl_set_coalesce(guard);
148 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
149 guard = isl_ast_build_specialize(build, guard);
151 graft = isl_ast_graft_alloc_domain(map, build);
152 graft = at_each_domain(graft, executed, build);
153 isl_ast_build_free(build);
154 isl_map_free(executed);
155 graft = isl_ast_graft_add_guard(graft, guard, data->build);
157 list = isl_ast_graft_list_from_ast_graft(graft);
158 data->list = isl_ast_graft_list_concat(data->list, list);
160 return isl_stat_ok;
163 /* Generate an AST for a single domain based on
164 * the inverse schedule "executed" and add it to data->list.
166 * If there is more than one domain element associated to the current
167 * schedule "time", then we need to continue the generation process
168 * in generate_non_single_valued.
169 * Note that the inverse schedule being single-valued may depend
170 * on constraints that are only available in the original context
171 * domain specified by the user. We therefore first introduce
172 * some of the constraints of data->build->domain. In particular,
173 * we intersect with a single-disjunct approximation of this set.
174 * We perform this approximation to avoid further splitting up
175 * the executed relation, possibly introducing a disjunctive guard
176 * on the statement.
178 * On the other hand, we only perform the test after having taken the gist
179 * of the domain as the resulting map is the one from which the call
180 * expression is constructed. Using this map to construct the call
181 * expression usually yields simpler results in cases where the original
182 * map is not obviously single-valued.
183 * If the original map is obviously single-valued, then the gist
184 * operation is skipped.
186 * Because we perform the single-valuedness test on the gisted map,
187 * we may in rare cases fail to recognize that the inverse schedule
188 * is single-valued. This becomes problematic if this happens
189 * from the recursive call through generate_non_single_valued
190 * as we would then end up in an infinite recursion.
191 * We therefore check if we are inside a call to generate_non_single_valued
192 * and revert to the ungisted map if the gisted map turns out not to be
193 * single-valued.
195 * Otherwise, call add_domain to generate a call expression (with guard) and
196 * to call the at_each_domain callback, if any.
198 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
200 struct isl_generate_domain_data *data = user;
201 isl_set *domain;
202 isl_map *map = NULL;
203 int empty, sv;
205 domain = isl_ast_build_get_domain(data->build);
206 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
207 executed = isl_map_intersect_domain(executed, domain);
208 empty = isl_map_is_empty(executed);
209 if (empty < 0)
210 goto error;
211 if (empty) {
212 isl_map_free(executed);
213 return isl_stat_ok;
216 sv = isl_map_plain_is_single_valued(executed);
217 if (sv < 0)
218 goto error;
219 if (sv)
220 return add_domain(executed, isl_map_copy(executed), data);
222 executed = isl_map_coalesce(executed);
223 map = isl_map_copy(executed);
224 map = isl_ast_build_compute_gist_map_domain(data->build, map);
225 sv = isl_map_is_single_valued(map);
226 if (sv < 0)
227 goto error;
228 if (!sv) {
229 isl_map_free(map);
230 if (data->build->single_valued)
231 map = isl_map_copy(executed);
232 else
233 return generate_non_single_valued(executed, data);
236 return add_domain(executed, map, data);
237 error:
238 isl_map_free(map);
239 isl_map_free(executed);
240 return isl_stat_error;
243 /* Call build->create_leaf to a create "leaf" node in the AST,
244 * encapsulate the result in an isl_ast_graft and return the result
245 * as a 1-element list.
247 * Note that the node returned by the user may be an entire tree.
249 * Since the node itself cannot enforce any constraints, we turn
250 * all pending constraints into guards and add them to the resulting
251 * graft to ensure that they will be generated.
253 * Before we pass control to the user, we first clear some information
254 * from the build that is (presumbably) only meaningful
255 * for the current code generation.
256 * This includes the create_leaf callback itself, so we make a copy
257 * of the build first.
259 static __isl_give isl_ast_graft_list *call_create_leaf(
260 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
262 isl_set *guard;
263 isl_ast_node *node;
264 isl_ast_graft *graft;
265 isl_ast_build *user_build;
267 guard = isl_ast_build_get_pending(build);
268 user_build = isl_ast_build_copy(build);
269 user_build = isl_ast_build_replace_pending_by_guard(user_build,
270 isl_set_copy(guard));
271 user_build = isl_ast_build_set_executed(user_build, executed);
272 user_build = isl_ast_build_clear_local_info(user_build);
273 if (!user_build)
274 node = NULL;
275 else
276 node = build->create_leaf(user_build, build->create_leaf_user);
277 graft = isl_ast_graft_alloc(node, build);
278 graft = isl_ast_graft_add_guard(graft, guard, build);
279 isl_ast_build_free(build);
280 return isl_ast_graft_list_from_ast_graft(graft);
283 static __isl_give isl_ast_graft_list *build_ast_from_child(
284 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
285 __isl_take isl_union_map *executed);
287 /* Generate an AST after having handled the complete schedule
288 * of this call to the code generator or the complete band
289 * if we are generating an AST from a schedule tree.
291 * If we are inside a band node, then move on to the child of the band.
293 * If the user has specified a create_leaf callback, control
294 * is passed to the user in call_create_leaf.
296 * Otherwise, we generate one or more calls for each individual
297 * domain in generate_domain.
299 static __isl_give isl_ast_graft_list *generate_inner_level(
300 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
302 isl_ctx *ctx;
303 struct isl_generate_domain_data data = { build };
305 if (!build || !executed)
306 goto error;
308 if (isl_ast_build_has_schedule_node(build)) {
309 isl_schedule_node *node;
310 node = isl_ast_build_get_schedule_node(build);
311 build = isl_ast_build_reset_schedule_node(build);
312 return build_ast_from_child(build, node, executed);
315 if (build->create_leaf)
316 return call_create_leaf(executed, build);
318 ctx = isl_union_map_get_ctx(executed);
319 data.list = isl_ast_graft_list_alloc(ctx, 0);
320 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
321 data.list = isl_ast_graft_list_free(data.list);
323 if (0)
324 error: data.list = NULL;
325 isl_ast_build_free(build);
326 isl_union_map_free(executed);
327 return data.list;
330 /* Call the before_each_for callback, if requested by the user.
332 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
333 __isl_keep isl_ast_build *build)
335 isl_id *id;
337 if (!node || !build)
338 return isl_ast_node_free(node);
339 if (!build->before_each_for)
340 return node;
341 id = build->before_each_for(build, build->before_each_for_user);
342 node = isl_ast_node_set_annotation(node, id);
343 return node;
346 /* Call the after_each_for callback, if requested by the user.
348 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
349 __isl_keep isl_ast_build *build)
351 if (!graft || !build)
352 return isl_ast_graft_free(graft);
353 if (!build->after_each_for)
354 return graft;
355 graft->node = build->after_each_for(graft->node, build,
356 build->after_each_for_user);
357 if (!graft->node)
358 return isl_ast_graft_free(graft);
359 return graft;
362 /* Plug in all the know values of the current and outer dimensions
363 * in the domain of "executed". In principle, we only need to plug
364 * in the known value of the current dimension since the values of
365 * outer dimensions have been plugged in already.
366 * However, it turns out to be easier to just plug in all known values.
368 static __isl_give isl_union_map *plug_in_values(
369 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
371 return isl_ast_build_substitute_values_union_map_domain(build,
372 executed);
375 /* Check if the constraint "c" is a lower bound on dimension "pos",
376 * an upper bound, or independent of dimension "pos".
378 static int constraint_type(isl_constraint *c, int pos)
380 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
381 return 1;
382 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
383 return 2;
384 return 0;
387 /* Compare the types of the constraints "a" and "b",
388 * resulting in constraints that are independent of "depth"
389 * to be sorted before the lower bounds on "depth", which in
390 * turn are sorted before the upper bounds on "depth".
392 static int cmp_constraint(__isl_keep isl_constraint *a,
393 __isl_keep isl_constraint *b, void *user)
395 int *depth = user;
396 int t1 = constraint_type(a, *depth);
397 int t2 = constraint_type(b, *depth);
399 return t1 - t2;
402 /* Extract a lower bound on dimension "pos" from constraint "c".
404 * If the constraint is of the form
406 * a x + f(...) >= 0
408 * then we essentially return
410 * l = ceil(-f(...)/a)
412 * However, if the current dimension is strided, then we need to make
413 * sure that the lower bound we construct is of the form
415 * f + s a
417 * with f the offset and s the stride.
418 * We therefore compute
420 * f + s * ceil((l - f)/s)
422 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
423 int pos, __isl_keep isl_ast_build *build)
425 isl_aff *aff;
427 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
428 aff = isl_aff_ceil(aff);
430 if (isl_ast_build_has_stride(build, pos)) {
431 isl_aff *offset;
432 isl_val *stride;
434 offset = isl_ast_build_get_offset(build, pos);
435 stride = isl_ast_build_get_stride(build, pos);
437 aff = isl_aff_sub(aff, isl_aff_copy(offset));
438 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
439 aff = isl_aff_ceil(aff);
440 aff = isl_aff_scale_val(aff, stride);
441 aff = isl_aff_add(aff, offset);
444 aff = isl_ast_build_compute_gist_aff(build, aff);
446 return aff;
449 /* Return the exact lower bound (or upper bound if "upper" is set)
450 * of "domain" as a piecewise affine expression.
452 * If we are computing a lower bound (of a strided dimension), then
453 * we need to make sure it is of the form
455 * f + s a
457 * where f is the offset and s is the stride.
458 * We therefore need to include the stride constraint before computing
459 * the minimum.
461 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
462 __isl_keep isl_ast_build *build, int upper)
464 isl_set *stride;
465 isl_map *it_map;
466 isl_pw_aff *pa;
467 isl_pw_multi_aff *pma;
469 domain = isl_set_copy(domain);
470 if (!upper) {
471 stride = isl_ast_build_get_stride_constraint(build);
472 domain = isl_set_intersect(domain, stride);
474 it_map = isl_ast_build_map_to_iterator(build, domain);
475 if (upper)
476 pma = isl_map_lexmax_pw_multi_aff(it_map);
477 else
478 pma = isl_map_lexmin_pw_multi_aff(it_map);
479 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
480 isl_pw_multi_aff_free(pma);
481 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
482 pa = isl_pw_aff_coalesce(pa);
484 return pa;
487 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
488 * remove_redundant_lower_bounds.
490 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
491 void *user)
493 return isl_pw_aff_plain_cmp(a, b);
496 /* Given a list of lower bounds "list", remove those that are redundant
497 * with respect to the other bounds in "list" and the domain of "build".
499 * We first sort the bounds in the same way as they would be sorted
500 * by set_for_node_expressions so that we can try and remove the last
501 * bounds first.
503 * For a lower bound to be effective, there needs to be at least
504 * one domain element for which it is larger than all other lower bounds.
505 * For each lower bound we therefore intersect the domain with
506 * the conditions that it is larger than all other bounds and
507 * check whether the result is empty. If so, the bound can be removed.
509 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
510 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
512 int i, j, n;
513 isl_set *domain;
515 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
516 if (!list)
517 return NULL;
519 n = isl_pw_aff_list_n_pw_aff(list);
520 if (n <= 1)
521 return list;
523 domain = isl_ast_build_get_domain(build);
525 for (i = n - 1; i >= 0; --i) {
526 isl_pw_aff *pa_i;
527 isl_set *domain_i;
528 int empty;
530 domain_i = isl_set_copy(domain);
531 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
533 for (j = 0; j < n; ++j) {
534 isl_pw_aff *pa_j;
535 isl_set *better;
537 if (j == i)
538 continue;
540 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
541 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
542 domain_i = isl_set_intersect(domain_i, better);
545 empty = isl_set_is_empty(domain_i);
547 isl_set_free(domain_i);
548 isl_pw_aff_free(pa_i);
550 if (empty < 0)
551 goto error;
552 if (!empty)
553 continue;
554 list = isl_pw_aff_list_drop(list, i, 1);
555 n--;
558 isl_set_free(domain);
560 return list;
561 error:
562 isl_set_free(domain);
563 return isl_pw_aff_list_free(list);
566 /* Extract a lower bound on dimension "pos" from each constraint
567 * in "constraints" and return the list of lower bounds.
568 * If "constraints" has zero elements, then we extract a lower bound
569 * from "domain" instead.
571 * If the current dimension is strided, then the lower bound
572 * is adjusted by lower_bound to match the stride information.
573 * This modification may make one or more lower bounds redundant
574 * with respect to the other lower bounds. We therefore check
575 * for this condition and remove the redundant lower bounds.
577 static __isl_give isl_pw_aff_list *lower_bounds(
578 __isl_keep isl_constraint_list *constraints, int pos,
579 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
581 isl_ctx *ctx;
582 isl_pw_aff_list *list;
583 int i, n;
585 if (!build)
586 return NULL;
588 n = isl_constraint_list_n_constraint(constraints);
589 if (n == 0) {
590 isl_pw_aff *pa;
591 pa = exact_bound(domain, build, 0);
592 return isl_pw_aff_list_from_pw_aff(pa);
595 ctx = isl_ast_build_get_ctx(build);
596 list = isl_pw_aff_list_alloc(ctx,n);
598 for (i = 0; i < n; ++i) {
599 isl_aff *aff;
600 isl_constraint *c;
602 c = isl_constraint_list_get_constraint(constraints, i);
603 aff = lower_bound(c, pos, build);
604 isl_constraint_free(c);
605 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
608 if (isl_ast_build_has_stride(build, pos))
609 list = remove_redundant_lower_bounds(list, build);
611 return list;
614 /* Extract an upper bound on dimension "pos" from each constraint
615 * in "constraints" and return the list of upper bounds.
616 * If "constraints" has zero elements, then we extract an upper bound
617 * from "domain" instead.
619 static __isl_give isl_pw_aff_list *upper_bounds(
620 __isl_keep isl_constraint_list *constraints, int pos,
621 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
623 isl_ctx *ctx;
624 isl_pw_aff_list *list;
625 int i, n;
627 n = isl_constraint_list_n_constraint(constraints);
628 if (n == 0) {
629 isl_pw_aff *pa;
630 pa = exact_bound(domain, build, 1);
631 return isl_pw_aff_list_from_pw_aff(pa);
634 ctx = isl_ast_build_get_ctx(build);
635 list = isl_pw_aff_list_alloc(ctx,n);
637 for (i = 0; i < n; ++i) {
638 isl_aff *aff;
639 isl_constraint *c;
641 c = isl_constraint_list_get_constraint(constraints, i);
642 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
643 isl_constraint_free(c);
644 aff = isl_aff_floor(aff);
645 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
648 return list;
651 /* Return an isl_ast_expr that performs the reduction of type "type"
652 * on AST expressions corresponding to the elements in "list".
654 * The list is assumed to contain at least one element.
655 * If the list contains exactly one element, then the returned isl_ast_expr
656 * simply computes that affine expression.
657 * If the list contains more than one element, then we sort it
658 * using a fairly abitrary but hopefully reasonably stable order.
660 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
661 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
663 int i, n;
664 isl_ctx *ctx;
665 isl_ast_expr *expr;
667 if (!list)
668 return NULL;
670 n = isl_pw_aff_list_n_pw_aff(list);
672 if (n == 1)
673 return isl_ast_build_expr_from_pw_aff_internal(build,
674 isl_pw_aff_list_get_pw_aff(list, 0));
676 ctx = isl_pw_aff_list_get_ctx(list);
677 expr = isl_ast_expr_alloc_op(ctx, type, n);
678 if (!expr)
679 return NULL;
681 list = isl_pw_aff_list_copy(list);
682 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
683 if (!list)
684 return isl_ast_expr_free(expr);
686 for (i = 0; i < n; ++i) {
687 isl_ast_expr *expr_i;
689 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
690 isl_pw_aff_list_get_pw_aff(list, i));
691 if (!expr_i)
692 goto error;
693 expr->u.op.args[i] = expr_i;
696 isl_pw_aff_list_free(list);
697 return expr;
698 error:
699 isl_pw_aff_list_free(list);
700 isl_ast_expr_free(expr);
701 return NULL;
704 /* Add guards implied by the "generated constraints",
705 * but not (necessarily) enforced by the generated AST to "guard".
706 * In particular, if there is any stride constraints,
707 * then add the guard implied by those constraints.
708 * If we have generated a degenerate loop, then add the guard
709 * implied by "bounds" on the outer dimensions, i.e., the guard
710 * that ensures that the single value actually exists.
711 * Since there may also be guards implied by a combination
712 * of these constraints, we first combine them before
713 * deriving the implied constraints.
715 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
716 int degenerate, __isl_keep isl_basic_set *bounds,
717 __isl_keep isl_ast_build *build)
719 int depth, has_stride;
720 isl_space *space;
721 isl_set *dom, *set;
723 depth = isl_ast_build_get_depth(build);
724 has_stride = isl_ast_build_has_stride(build, depth);
725 if (!has_stride && !degenerate)
726 return guard;
728 space = isl_basic_set_get_space(bounds);
729 dom = isl_set_universe(space);
731 if (degenerate) {
732 bounds = isl_basic_set_copy(bounds);
733 bounds = isl_basic_set_drop_constraints_not_involving_dims(
734 bounds, isl_dim_set, depth, 1);
735 set = isl_set_from_basic_set(bounds);
736 dom = isl_set_intersect(dom, set);
739 if (has_stride) {
740 set = isl_ast_build_get_stride_constraint(build);
741 dom = isl_set_intersect(dom, set);
744 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
745 dom = isl_ast_build_compute_gist(build, dom);
746 guard = isl_set_intersect(guard, dom);
748 return guard;
751 /* Update "graft" based on "sub_build" for the degenerate case.
753 * "build" is the build in which graft->node was created
754 * "sub_build" contains information about the current level itself,
755 * including the single value attained.
757 * We set the initialization part of the for loop to the single
758 * value attained by the current dimension.
759 * The increment and condition are not strictly needed as the are known
760 * to be "1" and "iterator <= value" respectively.
762 static __isl_give isl_ast_graft *refine_degenerate(
763 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
764 __isl_keep isl_ast_build *sub_build)
766 isl_pw_aff *value;
768 if (!graft || !sub_build)
769 return isl_ast_graft_free(graft);
771 value = isl_pw_aff_copy(sub_build->value);
773 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
774 value);
775 if (!graft->node->u.f.init)
776 return isl_ast_graft_free(graft);
778 return graft;
781 /* Return the intersection of constraints in "list" as a set.
783 static __isl_give isl_set *intersect_constraints(
784 __isl_keep isl_constraint_list *list)
786 int i, n;
787 isl_basic_set *bset;
789 n = isl_constraint_list_n_constraint(list);
790 if (n < 1)
791 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
792 "expecting at least one constraint", return NULL);
794 bset = isl_basic_set_from_constraint(
795 isl_constraint_list_get_constraint(list, 0));
796 for (i = 1; i < n; ++i) {
797 isl_basic_set *bset_i;
799 bset_i = isl_basic_set_from_constraint(
800 isl_constraint_list_get_constraint(list, i));
801 bset = isl_basic_set_intersect(bset, bset_i);
804 return isl_set_from_basic_set(bset);
807 /* Compute the constraints on the outer dimensions enforced by
808 * graft->node and add those constraints to graft->enforced,
809 * in case the upper bound is expressed as a set "upper".
811 * In particular, if l(...) is a lower bound in "lower", and
813 * -a i + f(...) >= 0 or a i <= f(...)
815 * is an upper bound ocnstraint on the current dimension i,
816 * then the for loop enforces the constraint
818 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
820 * We therefore simply take each lower bound in turn, plug it into
821 * the upper bounds and compute the intersection over all lower bounds.
823 * If a lower bound is a rational expression, then
824 * isl_basic_set_preimage_multi_aff will force this rational
825 * expression to have only integer values. However, the loop
826 * itself does not enforce this integrality constraint. We therefore
827 * use the ceil of the lower bounds instead of the lower bounds themselves.
828 * Other constraints will make sure that the for loop is only executed
829 * when each of the lower bounds attains an integral value.
830 * In particular, potentially rational values only occur in
831 * lower_bound if the offset is a (seemingly) rational expression,
832 * but then outer conditions will make sure that this rational expression
833 * only attains integer values.
835 static __isl_give isl_ast_graft *set_enforced_from_set(
836 __isl_take isl_ast_graft *graft,
837 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
839 isl_space *space;
840 isl_basic_set *enforced;
841 isl_pw_multi_aff *pma;
842 int i, n;
844 if (!graft || !lower)
845 return isl_ast_graft_free(graft);
847 space = isl_set_get_space(upper);
848 enforced = isl_basic_set_universe(isl_space_copy(space));
850 space = isl_space_map_from_set(space);
851 pma = isl_pw_multi_aff_identity(space);
853 n = isl_pw_aff_list_n_pw_aff(lower);
854 for (i = 0; i < n; ++i) {
855 isl_pw_aff *pa;
856 isl_set *enforced_i;
857 isl_basic_set *hull;
858 isl_pw_multi_aff *pma_i;
860 pa = isl_pw_aff_list_get_pw_aff(lower, i);
861 pa = isl_pw_aff_ceil(pa);
862 pma_i = isl_pw_multi_aff_copy(pma);
863 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
864 enforced_i = isl_set_copy(upper);
865 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
866 hull = isl_set_simple_hull(enforced_i);
867 enforced = isl_basic_set_intersect(enforced, hull);
870 isl_pw_multi_aff_free(pma);
872 graft = isl_ast_graft_enforce(graft, enforced);
874 return graft;
877 /* Compute the constraints on the outer dimensions enforced by
878 * graft->node and add those constraints to graft->enforced,
879 * in case the upper bound is expressed as
880 * a list of affine expressions "upper".
882 * The enforced condition is that each lower bound expression is less
883 * than or equal to each upper bound expression.
885 static __isl_give isl_ast_graft *set_enforced_from_list(
886 __isl_take isl_ast_graft *graft,
887 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
889 isl_set *cond;
890 isl_basic_set *enforced;
892 lower = isl_pw_aff_list_copy(lower);
893 upper = isl_pw_aff_list_copy(upper);
894 cond = isl_pw_aff_list_le_set(lower, upper);
895 enforced = isl_set_simple_hull(cond);
896 graft = isl_ast_graft_enforce(graft, enforced);
898 return graft;
901 /* Does "aff" have a negative constant term?
903 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
904 __isl_take isl_aff *aff, void *user)
906 int *neg = user;
907 isl_val *v;
909 v = isl_aff_get_constant_val(aff);
910 *neg = isl_val_is_neg(v);
911 isl_val_free(v);
912 isl_set_free(set);
913 isl_aff_free(aff);
915 return *neg ? isl_stat_ok : isl_stat_error;
918 /* Does "pa" have a negative constant term over its entire domain?
920 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
921 void *user)
923 isl_stat r;
924 int *neg = user;
926 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
927 isl_pw_aff_free(pa);
929 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
932 /* Does each element in "list" have a negative constant term?
934 * The callback terminates the iteration as soon an element has been
935 * found that does not have a negative constant term.
937 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
939 int neg = 1;
941 if (isl_pw_aff_list_foreach(list,
942 &pw_aff_constant_is_negative, &neg) < 0 && neg)
943 return -1;
945 return neg;
948 /* Add 1 to each of the elements in "list", where each of these elements
949 * is defined over the internal schedule space of "build".
951 static __isl_give isl_pw_aff_list *list_add_one(
952 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
954 int i, n;
955 isl_space *space;
956 isl_aff *aff;
957 isl_pw_aff *one;
959 space = isl_ast_build_get_space(build, 1);
960 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
961 aff = isl_aff_add_constant_si(aff, 1);
962 one = isl_pw_aff_from_aff(aff);
964 n = isl_pw_aff_list_n_pw_aff(list);
965 for (i = 0; i < n; ++i) {
966 isl_pw_aff *pa;
967 pa = isl_pw_aff_list_get_pw_aff(list, i);
968 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
969 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
972 isl_pw_aff_free(one);
974 return list;
977 /* Set the condition part of the for node graft->node in case
978 * the upper bound is represented as a list of piecewise affine expressions.
980 * In particular, set the condition to
982 * iterator <= min(list of upper bounds)
984 * If each of the upper bounds has a negative constant term, then
985 * set the condition to
987 * iterator < min(list of (upper bound + 1)s)
990 static __isl_give isl_ast_graft *set_for_cond_from_list(
991 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
992 __isl_keep isl_ast_build *build)
994 int neg;
995 isl_ast_expr *bound, *iterator, *cond;
996 enum isl_ast_op_type type = isl_ast_op_le;
998 if (!graft || !list)
999 return isl_ast_graft_free(graft);
1001 neg = list_constant_is_negative(list);
1002 if (neg < 0)
1003 return isl_ast_graft_free(graft);
1004 list = isl_pw_aff_list_copy(list);
1005 if (neg) {
1006 list = list_add_one(list, build);
1007 type = isl_ast_op_lt;
1010 bound = reduce_list(isl_ast_op_min, list, build);
1011 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1012 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1013 graft->node->u.f.cond = cond;
1015 isl_pw_aff_list_free(list);
1016 if (!graft->node->u.f.cond)
1017 return isl_ast_graft_free(graft);
1018 return graft;
1021 /* Set the condition part of the for node graft->node in case
1022 * the upper bound is represented as a set.
1024 static __isl_give isl_ast_graft *set_for_cond_from_set(
1025 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1026 __isl_keep isl_ast_build *build)
1028 isl_ast_expr *cond;
1030 if (!graft)
1031 return NULL;
1033 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1034 graft->node->u.f.cond = cond;
1035 if (!graft->node->u.f.cond)
1036 return isl_ast_graft_free(graft);
1037 return graft;
1040 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1041 * the current dimension.
1043 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1045 int depth;
1046 isl_val *v;
1047 isl_ctx *ctx;
1049 if (!build)
1050 return NULL;
1051 ctx = isl_ast_build_get_ctx(build);
1052 depth = isl_ast_build_get_depth(build);
1054 if (!isl_ast_build_has_stride(build, depth))
1055 return isl_ast_expr_alloc_int_si(ctx, 1);
1057 v = isl_ast_build_get_stride(build, depth);
1058 return isl_ast_expr_from_val(v);
1061 /* Should we express the loop condition as
1063 * iterator <= min(list of upper bounds)
1065 * or as a conjunction of constraints?
1067 * The first is constructed from a list of upper bounds.
1068 * The second is constructed from a set.
1070 * If there are no upper bounds in "constraints", then this could mean
1071 * that "domain" simply doesn't have an upper bound or that we didn't
1072 * pick any upper bound. In the first case, we want to generate the
1073 * loop condition as a(n empty) conjunction of constraints
1074 * In the second case, we will compute
1075 * a single upper bound from "domain" and so we use the list form.
1077 * If there are upper bounds in "constraints",
1078 * then we use the list form iff the atomic_upper_bound option is set.
1080 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1081 __isl_keep isl_set *domain, int depth)
1083 if (n_upper > 0)
1084 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1085 else
1086 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1089 /* Fill in the expressions of the for node in graft->node.
1091 * In particular,
1092 * - set the initialization part of the loop to the maximum of the lower bounds
1093 * - extract the increment from the stride of the current dimension
1094 * - construct the for condition either based on a list of upper bounds
1095 * or on a set of upper bound constraints.
1097 static __isl_give isl_ast_graft *set_for_node_expressions(
1098 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1099 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1100 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1102 isl_ast_node *node;
1104 if (!graft)
1105 return NULL;
1107 build = isl_ast_build_copy(build);
1109 node = graft->node;
1110 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1111 node->u.f.inc = for_inc(build);
1113 if (!node->u.f.init || !node->u.f.inc)
1114 graft = isl_ast_graft_free(graft);
1116 if (use_list)
1117 graft = set_for_cond_from_list(graft, upper_list, build);
1118 else
1119 graft = set_for_cond_from_set(graft, upper_set, build);
1121 isl_ast_build_free(build);
1123 return graft;
1126 /* Update "graft" based on "bounds" and "domain" for the generic,
1127 * non-degenerate, case.
1129 * "c_lower" and "c_upper" contain the lower and upper bounds
1130 * that the loop node should express.
1131 * "domain" is the subset of the intersection of the constraints
1132 * for which some code is executed.
1134 * There may be zero lower bounds or zero upper bounds in "constraints"
1135 * in case the list of constraints was created
1136 * based on the atomic option or based on separation with explicit bounds.
1137 * In that case, we use "domain" to derive lower and/or upper bounds.
1139 * We first compute a list of one or more lower bounds.
1141 * Then we decide if we want to express the condition as
1143 * iterator <= min(list of upper bounds)
1145 * or as a conjunction of constraints.
1147 * The set of enforced constraints is then computed either based on
1148 * a list of upper bounds or on a set of upper bound constraints.
1149 * We do not compute any enforced constraints if we were forced
1150 * to compute a lower or upper bound using exact_bound. The domains
1151 * of the resulting expressions may imply some bounds on outer dimensions
1152 * that we do not want to appear in the enforced constraints since
1153 * they are not actually enforced by the corresponding code.
1155 * Finally, we fill in the expressions of the for node.
1157 static __isl_give isl_ast_graft *refine_generic_bounds(
1158 __isl_take isl_ast_graft *graft,
1159 __isl_take isl_constraint_list *c_lower,
1160 __isl_take isl_constraint_list *c_upper,
1161 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1163 int depth;
1164 isl_ctx *ctx;
1165 isl_pw_aff_list *lower;
1166 int use_list;
1167 isl_set *upper_set = NULL;
1168 isl_pw_aff_list *upper_list = NULL;
1169 int n_lower, n_upper;
1171 if (!graft || !c_lower || !c_upper || !build)
1172 goto error;
1174 depth = isl_ast_build_get_depth(build);
1175 ctx = isl_ast_graft_get_ctx(graft);
1177 n_lower = isl_constraint_list_n_constraint(c_lower);
1178 n_upper = isl_constraint_list_n_constraint(c_upper);
1180 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1182 lower = lower_bounds(c_lower, depth, domain, build);
1184 if (use_list)
1185 upper_list = upper_bounds(c_upper, depth, domain, build);
1186 else if (n_upper > 0)
1187 upper_set = intersect_constraints(c_upper);
1188 else
1189 upper_set = isl_set_universe(isl_set_get_space(domain));
1191 if (n_lower == 0 || n_upper == 0)
1193 else if (use_list)
1194 graft = set_enforced_from_list(graft, lower, upper_list);
1195 else
1196 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1198 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1199 upper_set, build);
1201 isl_pw_aff_list_free(lower);
1202 isl_pw_aff_list_free(upper_list);
1203 isl_set_free(upper_set);
1204 isl_constraint_list_free(c_lower);
1205 isl_constraint_list_free(c_upper);
1207 return graft;
1208 error:
1209 isl_constraint_list_free(c_lower);
1210 isl_constraint_list_free(c_upper);
1211 return isl_ast_graft_free(graft);
1214 /* Internal data structure used inside count_constraints to keep
1215 * track of the number of constraints that are independent of dimension "pos",
1216 * the lower bounds in "pos" and the upper bounds in "pos".
1218 struct isl_ast_count_constraints_data {
1219 int pos;
1221 int n_indep;
1222 int n_lower;
1223 int n_upper;
1226 /* Increment data->n_indep, data->lower or data->upper depending
1227 * on whether "c" is independenct of dimensions data->pos,
1228 * a lower bound or an upper bound.
1230 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1232 struct isl_ast_count_constraints_data *data = user;
1234 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1235 data->n_lower++;
1236 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1237 data->n_upper++;
1238 else
1239 data->n_indep++;
1241 isl_constraint_free(c);
1243 return isl_stat_ok;
1246 /* Update "graft" based on "bounds" and "domain" for the generic,
1247 * non-degenerate, case.
1249 * "list" respresent the list of bounds that need to be encoded by
1250 * the for loop. Only the constraints that involve the iterator
1251 * are relevant here. The other constraints are taken care of by
1252 * the caller and are included in the generated constraints of "build".
1253 * "domain" is the subset of the intersection of the constraints
1254 * for which some code is executed.
1255 * "build" is the build in which graft->node was created.
1257 * We separate lower bounds, upper bounds and constraints that
1258 * are independent of the loop iterator.
1260 * The actual for loop bounds are generated in refine_generic_bounds.
1262 static __isl_give isl_ast_graft *refine_generic_split(
1263 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1264 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1266 struct isl_ast_count_constraints_data data;
1267 isl_constraint_list *lower;
1268 isl_constraint_list *upper;
1270 if (!list)
1271 return isl_ast_graft_free(graft);
1273 data.pos = isl_ast_build_get_depth(build);
1275 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1276 if (!list)
1277 return isl_ast_graft_free(graft);
1279 data.n_indep = data.n_lower = data.n_upper = 0;
1280 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1281 isl_constraint_list_free(list);
1282 return isl_ast_graft_free(graft);
1285 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1286 upper = isl_constraint_list_copy(lower);
1287 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1288 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1290 return refine_generic_bounds(graft, lower, upper, domain, build);
1293 /* Update "graft" based on "bounds" and "domain" for the generic,
1294 * non-degenerate, case.
1296 * "bounds" respresent the bounds that need to be encoded by
1297 * the for loop (or a guard around the for loop).
1298 * "domain" is the subset of "bounds" for which some code is executed.
1299 * "build" is the build in which graft->node was created.
1301 * We break up "bounds" into a list of constraints and continue with
1302 * refine_generic_split.
1304 static __isl_give isl_ast_graft *refine_generic(
1305 __isl_take isl_ast_graft *graft,
1306 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1307 __isl_keep isl_ast_build *build)
1309 isl_constraint_list *list;
1311 if (!build || !graft)
1312 return isl_ast_graft_free(graft);
1314 list = isl_basic_set_get_constraint_list(bounds);
1316 graft = refine_generic_split(graft, list, domain, build);
1318 return graft;
1321 /* Create a for node for the current level.
1323 * Mark the for node degenerate if "degenerate" is set.
1325 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1326 int degenerate)
1328 int depth;
1329 isl_id *id;
1330 isl_ast_node *node;
1332 if (!build)
1333 return NULL;
1335 depth = isl_ast_build_get_depth(build);
1336 id = isl_ast_build_get_iterator_id(build, depth);
1337 node = isl_ast_node_alloc_for(id);
1338 if (degenerate)
1339 node = isl_ast_node_for_mark_degenerate(node);
1341 return node;
1344 /* If the ast_build_exploit_nested_bounds option is set, then return
1345 * the constraints enforced by all elements in "list".
1346 * Otherwise, return the universe.
1348 static __isl_give isl_basic_set *extract_shared_enforced(
1349 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1351 isl_ctx *ctx;
1352 isl_space *space;
1354 if (!list)
1355 return NULL;
1357 ctx = isl_ast_graft_list_get_ctx(list);
1358 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1359 return isl_ast_graft_list_extract_shared_enforced(list, build);
1361 space = isl_ast_build_get_space(build, 1);
1362 return isl_basic_set_universe(space);
1365 /* Return the pending constraints of "build" that are not already taken
1366 * care of (by a combination of "enforced" and the generated constraints
1367 * of "build").
1369 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1370 __isl_keep isl_basic_set *enforced)
1372 isl_set *guard, *context;
1374 guard = isl_ast_build_get_pending(build);
1375 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1376 context = isl_set_intersect(context,
1377 isl_ast_build_get_generated(build));
1378 return isl_set_gist(guard, context);
1381 /* Create an AST node for the current dimension based on
1382 * the schedule domain "bounds" and return the node encapsulated
1383 * in an isl_ast_graft.
1385 * "executed" is the current inverse schedule, taking into account
1386 * the bounds in "bounds"
1387 * "domain" is the domain of "executed", with inner dimensions projected out.
1388 * It may be a strict subset of "bounds" in case "bounds" was created
1389 * based on the atomic option or based on separation with explicit bounds.
1391 * "domain" may satisfy additional equalities that result
1392 * from intersecting "executed" with "bounds" in add_node.
1393 * It may also satisfy some global constraints that were dropped out because
1394 * we performed separation with explicit bounds.
1395 * The very first step is then to copy these constraints to "bounds".
1397 * Since we may be calling before_each_for and after_each_for
1398 * callbacks, we record the current inverse schedule in the build.
1400 * We consider three builds,
1401 * "build" is the one in which the current level is created,
1402 * "body_build" is the build in which the next level is created,
1403 * "sub_build" is essentially the same as "body_build", except that
1404 * the depth has not been increased yet.
1406 * "build" already contains information (in strides and offsets)
1407 * about the strides at the current level, but this information is not
1408 * reflected in the build->domain.
1409 * We first add this information and the "bounds" to the sub_build->domain.
1410 * isl_ast_build_set_loop_bounds adds the stride information and
1411 * checks whether the current dimension attains
1412 * only a single value and whether this single value can be represented using
1413 * a single affine expression.
1414 * In the first case, the current level is considered "degenerate".
1415 * In the second, sub-case, the current level is considered "eliminated".
1416 * Eliminated levels don't need to be reflected in the AST since we can
1417 * simply plug in the affine expression. For degenerate, but non-eliminated,
1418 * levels, we do introduce a for node, but mark is as degenerate so that
1419 * it can be printed as an assignment of the single value to the loop
1420 * "iterator".
1422 * If the current level is eliminated, we explicitly plug in the value
1423 * for the current level found by isl_ast_build_set_loop_bounds in the
1424 * inverse schedule. This ensures that if we are working on a slice
1425 * of the domain based on information available in the inverse schedule
1426 * and the build domain, that then this information is also reflected
1427 * in the inverse schedule. This operation also eliminates the current
1428 * dimension from the inverse schedule making sure no inner dimensions depend
1429 * on the current dimension. Otherwise, we create a for node, marking
1430 * it degenerate if appropriate. The initial for node is still incomplete
1431 * and will be completed in either refine_degenerate or refine_generic.
1433 * We then generate a sequence of grafts for the next level,
1434 * create a surrounding graft for the current level and insert
1435 * the for node we created (if the current level is not eliminated).
1436 * Before creating a graft for the current level, we first extract
1437 * hoistable constraints from the child guards and combine them
1438 * with the pending constraints in the build. These constraints
1439 * are used to simplify the child guards and then added to the guard
1440 * of the current graft to ensure that they will be generated.
1441 * If the hoisted guard is a disjunction, then we use it directly
1442 * to gist the guards on the children before intersect it with the
1443 * pending constraints. We do so because this disjunction is typically
1444 * identical to the guards on the children such that these guards
1445 * can be effectively removed completely. After the intersection,
1446 * the gist operation would have a harder time figuring this out.
1448 * Finally, we set the bounds of the for loop in either
1449 * refine_degenerate or refine_generic.
1450 * We do so in a context where the pending constraints of the build
1451 * have been replaced by the guard of the current graft.
1453 static __isl_give isl_ast_graft *create_node_scaled(
1454 __isl_take isl_union_map *executed,
1455 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1456 __isl_take isl_ast_build *build)
1458 int depth;
1459 int degenerate;
1460 isl_bool eliminated;
1461 isl_basic_set *hull;
1462 isl_basic_set *enforced;
1463 isl_set *guard, *hoisted;
1464 isl_ast_node *node = NULL;
1465 isl_ast_graft *graft;
1466 isl_ast_graft_list *children;
1467 isl_ast_build *sub_build;
1468 isl_ast_build *body_build;
1470 domain = isl_ast_build_eliminate_divs(build, domain);
1471 domain = isl_set_detect_equalities(domain);
1472 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1473 bounds = isl_basic_set_intersect(bounds, hull);
1474 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1476 depth = isl_ast_build_get_depth(build);
1477 sub_build = isl_ast_build_copy(build);
1478 bounds = isl_basic_set_remove_redundancies(bounds);
1479 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1480 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1481 isl_basic_set_copy(bounds));
1482 degenerate = isl_ast_build_has_value(sub_build);
1483 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1484 if (degenerate < 0 || eliminated < 0)
1485 executed = isl_union_map_free(executed);
1486 if (!degenerate)
1487 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1488 sub_build = isl_ast_build_set_pending_generated(sub_build,
1489 isl_basic_set_copy(bounds));
1490 if (eliminated)
1491 executed = plug_in_values(executed, sub_build);
1492 else
1493 node = create_for(build, degenerate);
1495 body_build = isl_ast_build_copy(sub_build);
1496 body_build = isl_ast_build_increase_depth(body_build);
1497 if (!eliminated)
1498 node = before_each_for(node, body_build);
1499 children = generate_next_level(executed,
1500 isl_ast_build_copy(body_build));
1502 enforced = extract_shared_enforced(children, build);
1503 guard = extract_pending(sub_build, enforced);
1504 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1505 if (isl_set_n_basic_set(hoisted) > 1)
1506 children = isl_ast_graft_list_gist_guards(children,
1507 isl_set_copy(hoisted));
1508 guard = isl_set_intersect(guard, hoisted);
1509 if (!eliminated)
1510 guard = add_implied_guards(guard, degenerate, bounds, build);
1512 graft = isl_ast_graft_alloc_from_children(children,
1513 isl_set_copy(guard), enforced, build, sub_build);
1515 if (!eliminated) {
1516 isl_ast_build *for_build;
1518 graft = isl_ast_graft_insert_for(graft, node);
1519 for_build = isl_ast_build_copy(build);
1520 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1521 isl_set_copy(guard));
1522 if (degenerate)
1523 graft = refine_degenerate(graft, for_build, sub_build);
1524 else
1525 graft = refine_generic(graft, bounds,
1526 domain, for_build);
1527 isl_ast_build_free(for_build);
1529 isl_set_free(guard);
1530 if (!eliminated)
1531 graft = after_each_for(graft, body_build);
1533 isl_ast_build_free(body_build);
1534 isl_ast_build_free(sub_build);
1535 isl_ast_build_free(build);
1536 isl_basic_set_free(bounds);
1537 isl_set_free(domain);
1539 return graft;
1542 /* Internal data structure for checking if all constraints involving
1543 * the input dimension "depth" are such that the other coefficients
1544 * are multiples of "m", reducing "m" if they are not.
1545 * If "m" is reduced all the way down to "1", then the check has failed
1546 * and we break out of the iteration.
1548 struct isl_check_scaled_data {
1549 int depth;
1550 isl_val *m;
1553 /* If constraint "c" involves the input dimension data->depth,
1554 * then make sure that all the other coefficients are multiples of data->m,
1555 * reducing data->m if needed.
1556 * Break out of the iteration if data->m has become equal to "1".
1558 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1559 void *user)
1561 struct isl_check_scaled_data *data = user;
1562 int i, j, n;
1563 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1564 isl_dim_div };
1566 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1567 isl_constraint_free(c);
1568 return isl_stat_ok;
1571 for (i = 0; i < 4; ++i) {
1572 n = isl_constraint_dim(c, t[i]);
1573 for (j = 0; j < n; ++j) {
1574 isl_val *d;
1576 if (t[i] == isl_dim_in && j == data->depth)
1577 continue;
1578 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1579 continue;
1580 d = isl_constraint_get_coefficient_val(c, t[i], j);
1581 data->m = isl_val_gcd(data->m, d);
1582 if (isl_val_is_one(data->m))
1583 break;
1585 if (j < n)
1586 break;
1589 isl_constraint_free(c);
1591 return i < 4 ? isl_stat_error : isl_stat_ok;
1594 /* For each constraint of "bmap" that involves the input dimension data->depth,
1595 * make sure that all the other coefficients are multiples of data->m,
1596 * reducing data->m if needed.
1597 * Break out of the iteration if data->m has become equal to "1".
1599 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1600 void *user)
1602 isl_stat r;
1604 r = isl_basic_map_foreach_constraint(bmap,
1605 &constraint_check_scaled, user);
1606 isl_basic_map_free(bmap);
1608 return r;
1611 /* For each constraint of "map" that involves the input dimension data->depth,
1612 * make sure that all the other coefficients are multiples of data->m,
1613 * reducing data->m if needed.
1614 * Break out of the iteration if data->m has become equal to "1".
1616 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1618 isl_stat r;
1620 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1621 isl_map_free(map);
1623 return r;
1626 /* Create an AST node for the current dimension based on
1627 * the schedule domain "bounds" and return the node encapsulated
1628 * in an isl_ast_graft.
1630 * "executed" is the current inverse schedule, taking into account
1631 * the bounds in "bounds"
1632 * "domain" is the domain of "executed", with inner dimensions projected out.
1635 * Before moving on to the actual AST node construction in create_node_scaled,
1636 * we first check if the current dimension is strided and if we can scale
1637 * down this stride. Note that we only do this if the ast_build_scale_strides
1638 * option is set.
1640 * In particular, let the current dimension take on values
1642 * f + s a
1644 * with a an integer. We check if we can find an integer m that (obviously)
1645 * divides both f and s.
1647 * If so, we check if the current dimension only appears in constraints
1648 * where the coefficients of the other variables are multiples of m.
1649 * We perform this extra check to avoid the risk of introducing
1650 * divisions by scaling down the current dimension.
1652 * If so, we scale the current dimension down by a factor of m.
1653 * That is, we plug in
1655 * i = m i' (1)
1657 * Note that in principle we could always scale down strided loops
1658 * by plugging in
1660 * i = f + s i'
1662 * but this may result in i' taking on larger values than the original i,
1663 * due to the shift by "f".
1664 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1666 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1667 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1668 __isl_take isl_ast_build *build)
1670 struct isl_check_scaled_data data;
1671 isl_ctx *ctx;
1672 isl_aff *offset;
1673 isl_val *d;
1675 ctx = isl_ast_build_get_ctx(build);
1676 if (!isl_options_get_ast_build_scale_strides(ctx))
1677 return create_node_scaled(executed, bounds, domain, build);
1679 data.depth = isl_ast_build_get_depth(build);
1680 if (!isl_ast_build_has_stride(build, data.depth))
1681 return create_node_scaled(executed, bounds, domain, build);
1683 offset = isl_ast_build_get_offset(build, data.depth);
1684 data.m = isl_ast_build_get_stride(build, data.depth);
1685 if (!data.m)
1686 offset = isl_aff_free(offset);
1687 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1688 d = isl_aff_get_denominator_val(offset);
1689 if (!d)
1690 executed = isl_union_map_free(executed);
1692 if (executed && isl_val_is_divisible_by(data.m, d))
1693 data.m = isl_val_div(data.m, d);
1694 else {
1695 data.m = isl_val_set_si(data.m, 1);
1696 isl_val_free(d);
1699 if (!isl_val_is_one(data.m)) {
1700 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1701 &data) < 0 &&
1702 !isl_val_is_one(data.m))
1703 executed = isl_union_map_free(executed);
1706 if (!isl_val_is_one(data.m)) {
1707 isl_space *space;
1708 isl_multi_aff *ma;
1709 isl_aff *aff;
1710 isl_map *map;
1711 isl_union_map *umap;
1713 space = isl_ast_build_get_space(build, 1);
1714 space = isl_space_map_from_set(space);
1715 ma = isl_multi_aff_identity(space);
1716 aff = isl_multi_aff_get_aff(ma, data.depth);
1717 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1718 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1720 bounds = isl_basic_set_preimage_multi_aff(bounds,
1721 isl_multi_aff_copy(ma));
1722 domain = isl_set_preimage_multi_aff(domain,
1723 isl_multi_aff_copy(ma));
1724 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1725 umap = isl_union_map_from_map(map);
1726 executed = isl_union_map_apply_domain(executed,
1727 isl_union_map_copy(umap));
1728 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1729 umap);
1731 isl_aff_free(offset);
1732 isl_val_free(data.m);
1734 return create_node_scaled(executed, bounds, domain, build);
1737 /* Add the basic set to the list that "user" points to.
1739 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1741 isl_basic_set_list **list = user;
1743 *list = isl_basic_set_list_add(*list, bset);
1745 return isl_stat_ok;
1748 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1750 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1751 __isl_take isl_set *set)
1753 int n;
1754 isl_ctx *ctx;
1755 isl_basic_set_list *list;
1757 if (!set)
1758 return NULL;
1760 ctx = isl_set_get_ctx(set);
1762 n = isl_set_n_basic_set(set);
1763 list = isl_basic_set_list_alloc(ctx, n);
1764 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1765 list = isl_basic_set_list_free(list);
1767 isl_set_free(set);
1768 return list;
1771 /* Generate code for the schedule domain "bounds"
1772 * and add the result to "list".
1774 * We mainly detect strides here and check if the bounds do not
1775 * conflict with the current build domain
1776 * and then pass over control to create_node.
1778 * "bounds" reflects the bounds on the current dimension and possibly
1779 * some extra conditions on outer dimensions.
1780 * It does not, however, include any divs involving the current dimension,
1781 * so it does not capture any stride constraints.
1782 * We therefore need to compute that part of the schedule domain that
1783 * intersects with "bounds" and derive the strides from the result.
1785 static __isl_give isl_ast_graft_list *add_node(
1786 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1787 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1789 isl_ast_graft *graft;
1790 isl_set *domain = NULL;
1791 isl_union_set *uset;
1792 int empty, disjoint;
1794 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1795 executed = isl_union_map_intersect_domain(executed, uset);
1796 empty = isl_union_map_is_empty(executed);
1797 if (empty < 0)
1798 goto error;
1799 if (empty)
1800 goto done;
1802 uset = isl_union_map_domain(isl_union_map_copy(executed));
1803 domain = isl_set_from_union_set(uset);
1804 domain = isl_ast_build_specialize(build, domain);
1806 domain = isl_set_compute_divs(domain);
1807 domain = isl_ast_build_eliminate_inner(build, domain);
1808 disjoint = isl_set_is_disjoint(domain, build->domain);
1809 if (disjoint < 0)
1810 goto error;
1811 if (disjoint)
1812 goto done;
1814 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1816 graft = create_node(executed, bounds, domain,
1817 isl_ast_build_copy(build));
1818 list = isl_ast_graft_list_add(list, graft);
1819 isl_ast_build_free(build);
1820 return list;
1821 error:
1822 list = isl_ast_graft_list_free(list);
1823 done:
1824 isl_set_free(domain);
1825 isl_basic_set_free(bounds);
1826 isl_union_map_free(executed);
1827 isl_ast_build_free(build);
1828 return list;
1831 /* Does any element of i follow or coincide with any element of j
1832 * at the current depth for equal values of the outer dimensions?
1834 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1835 __isl_keep isl_basic_set *j, void *user)
1837 int depth = *(int *) user;
1838 isl_basic_map *test;
1839 isl_bool empty;
1840 int l;
1842 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1843 isl_basic_set_copy(j));
1844 for (l = 0; l < depth; ++l)
1845 test = isl_basic_map_equate(test, isl_dim_in, l,
1846 isl_dim_out, l);
1847 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1848 isl_dim_out, depth);
1849 empty = isl_basic_map_is_empty(test);
1850 isl_basic_map_free(test);
1852 return empty < 0 ? isl_bool_error : !empty;
1855 /* Split up each element of "list" into a part that is related to "bset"
1856 * according to "gt" and a part that is not.
1857 * Return a list that consist of "bset" and all the pieces.
1859 static __isl_give isl_basic_set_list *add_split_on(
1860 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1861 __isl_keep isl_basic_map *gt)
1863 int i, n;
1864 isl_basic_set_list *res;
1866 if (!list)
1867 bset = isl_basic_set_free(bset);
1869 gt = isl_basic_map_copy(gt);
1870 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1871 n = isl_basic_set_list_n_basic_set(list);
1872 res = isl_basic_set_list_from_basic_set(bset);
1873 for (i = 0; res && i < n; ++i) {
1874 isl_basic_set *bset;
1875 isl_set *set1, *set2;
1876 isl_basic_map *bmap;
1877 int empty;
1879 bset = isl_basic_set_list_get_basic_set(list, i);
1880 bmap = isl_basic_map_copy(gt);
1881 bmap = isl_basic_map_intersect_range(bmap, bset);
1882 bset = isl_basic_map_range(bmap);
1883 empty = isl_basic_set_is_empty(bset);
1884 if (empty < 0)
1885 res = isl_basic_set_list_free(res);
1886 if (empty) {
1887 isl_basic_set_free(bset);
1888 bset = isl_basic_set_list_get_basic_set(list, i);
1889 res = isl_basic_set_list_add(res, bset);
1890 continue;
1893 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1894 set1 = isl_set_from_basic_set(bset);
1895 bset = isl_basic_set_list_get_basic_set(list, i);
1896 set2 = isl_set_from_basic_set(bset);
1897 set1 = isl_set_subtract(set2, set1);
1898 set1 = isl_set_make_disjoint(set1);
1900 res = isl_basic_set_list_concat(res,
1901 isl_basic_set_list_from_set(set1));
1903 isl_basic_map_free(gt);
1904 isl_basic_set_list_free(list);
1905 return res;
1908 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1909 __isl_keep isl_basic_set_list *domain_list,
1910 __isl_keep isl_union_map *executed,
1911 __isl_keep isl_ast_build *build);
1913 /* Internal data structure for add_nodes.
1915 * "executed" and "build" are extra arguments to be passed to add_node.
1916 * "list" collects the results.
1918 struct isl_add_nodes_data {
1919 isl_union_map *executed;
1920 isl_ast_build *build;
1922 isl_ast_graft_list *list;
1925 /* Generate code for the schedule domains in "scc"
1926 * and add the results to "list".
1928 * The domains in "scc" form a strongly connected component in the ordering.
1929 * If the number of domains in "scc" is larger than 1, then this means
1930 * that we cannot determine a valid ordering for the domains in the component.
1931 * This should be fairly rare because the individual domains
1932 * have been made disjoint first.
1933 * The problem is that the domains may be integrally disjoint but not
1934 * rationally disjoint. For example, we may have domains
1936 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1938 * These two domains have an empty intersection, but their rational
1939 * relaxations do intersect. It is impossible to order these domains
1940 * in the second dimension because the first should be ordered before
1941 * the second for outer dimension equal to 0, while it should be ordered
1942 * after for outer dimension equal to 1.
1944 * This may happen in particular in case of unrolling since the domain
1945 * of each slice is replaced by its simple hull.
1947 * For each basic set i in "scc" and for each of the following basic sets j,
1948 * we split off that part of the basic set i that shares the outer dimensions
1949 * with j and lies before j in the current dimension.
1950 * We collect all the pieces in a new list that replaces "scc".
1952 * While the elements in "scc" should be disjoint, we double-check
1953 * this property to avoid running into an infinite recursion in case
1954 * they intersect due to some internal error.
1956 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1958 struct isl_add_nodes_data *data = user;
1959 int i, n, depth;
1960 isl_basic_set *bset, *first;
1961 isl_basic_set_list *list;
1962 isl_space *space;
1963 isl_basic_map *gt;
1965 n = isl_basic_set_list_n_basic_set(scc);
1966 bset = isl_basic_set_list_get_basic_set(scc, 0);
1967 if (n == 1) {
1968 isl_basic_set_list_free(scc);
1969 data->list = add_node(data->list,
1970 isl_union_map_copy(data->executed), bset,
1971 isl_ast_build_copy(data->build));
1972 return data->list ? isl_stat_ok : isl_stat_error;
1975 depth = isl_ast_build_get_depth(data->build);
1976 space = isl_basic_set_get_space(bset);
1977 space = isl_space_map_from_set(space);
1978 gt = isl_basic_map_universe(space);
1979 for (i = 0; i < depth; ++i)
1980 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1981 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1983 first = isl_basic_set_copy(bset);
1984 list = isl_basic_set_list_from_basic_set(bset);
1985 for (i = 1; i < n; ++i) {
1986 int disjoint;
1988 bset = isl_basic_set_list_get_basic_set(scc, i);
1990 disjoint = isl_basic_set_is_disjoint(bset, first);
1991 if (disjoint < 0)
1992 list = isl_basic_set_list_free(list);
1993 else if (!disjoint)
1994 isl_die(isl_basic_set_list_get_ctx(scc),
1995 isl_error_internal,
1996 "basic sets in scc are assumed to be disjoint",
1997 list = isl_basic_set_list_free(list));
1999 list = add_split_on(list, bset, gt);
2001 isl_basic_set_free(first);
2002 isl_basic_map_free(gt);
2003 isl_basic_set_list_free(scc);
2004 scc = list;
2005 data->list = isl_ast_graft_list_concat(data->list,
2006 generate_sorted_domains(scc, data->executed, data->build));
2007 isl_basic_set_list_free(scc);
2009 return data->list ? isl_stat_ok : isl_stat_error;
2012 /* Sort the domains in "domain_list" according to the execution order
2013 * at the current depth (for equal values of the outer dimensions),
2014 * generate code for each of them, collecting the results in a list.
2015 * If no code is generated (because the intersection of the inverse schedule
2016 * with the domains turns out to be empty), then an empty list is returned.
2018 * The caller is responsible for ensuring that the basic sets in "domain_list"
2019 * are pair-wise disjoint. It can, however, in principle happen that
2020 * two basic sets should be ordered one way for one value of the outer
2021 * dimensions and the other way for some other value of the outer dimensions.
2022 * We therefore play safe and look for strongly connected components.
2023 * The function add_nodes takes care of handling non-trivial components.
2025 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2026 __isl_keep isl_basic_set_list *domain_list,
2027 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2029 isl_ctx *ctx;
2030 struct isl_add_nodes_data data;
2031 int depth;
2032 int n;
2034 if (!domain_list)
2035 return NULL;
2037 ctx = isl_basic_set_list_get_ctx(domain_list);
2038 n = isl_basic_set_list_n_basic_set(domain_list);
2039 data.list = isl_ast_graft_list_alloc(ctx, n);
2040 if (n == 0)
2041 return data.list;
2042 if (n == 1)
2043 return add_node(data.list, isl_union_map_copy(executed),
2044 isl_basic_set_list_get_basic_set(domain_list, 0),
2045 isl_ast_build_copy(build));
2047 depth = isl_ast_build_get_depth(build);
2048 data.executed = executed;
2049 data.build = build;
2050 if (isl_basic_set_list_foreach_scc(domain_list,
2051 &domain_follows_at_depth, &depth,
2052 &add_nodes, &data) < 0)
2053 data.list = isl_ast_graft_list_free(data.list);
2055 return data.list;
2058 /* Do i and j share any values for the outer dimensions?
2060 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2061 __isl_keep isl_basic_set *j, void *user)
2063 int depth = *(int *) user;
2064 isl_basic_map *test;
2065 isl_bool empty;
2066 int l;
2068 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2069 isl_basic_set_copy(j));
2070 for (l = 0; l < depth; ++l)
2071 test = isl_basic_map_equate(test, isl_dim_in, l,
2072 isl_dim_out, l);
2073 empty = isl_basic_map_is_empty(test);
2074 isl_basic_map_free(test);
2076 return empty < 0 ? isl_bool_error : !empty;
2079 /* Internal data structure for generate_sorted_domains_wrap.
2081 * "n" is the total number of basic sets
2082 * "executed" and "build" are extra arguments to be passed
2083 * to generate_sorted_domains.
2085 * "single" is set to 1 by generate_sorted_domains_wrap if there
2086 * is only a single component.
2087 * "list" collects the results.
2089 struct isl_ast_generate_parallel_domains_data {
2090 int n;
2091 isl_union_map *executed;
2092 isl_ast_build *build;
2094 int single;
2095 isl_ast_graft_list *list;
2098 /* Call generate_sorted_domains on "scc", fuse the result into a list
2099 * with either zero or one graft and collect the these single element
2100 * lists into data->list.
2102 * If there is only one component, i.e., if the number of basic sets
2103 * in the current component is equal to the total number of basic sets,
2104 * then data->single is set to 1 and the result of generate_sorted_domains
2105 * is not fused.
2107 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2108 void *user)
2110 struct isl_ast_generate_parallel_domains_data *data = user;
2111 isl_ast_graft_list *list;
2113 list = generate_sorted_domains(scc, data->executed, data->build);
2114 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2115 if (!data->single)
2116 list = isl_ast_graft_list_fuse(list, data->build);
2117 if (!data->list)
2118 data->list = list;
2119 else
2120 data->list = isl_ast_graft_list_concat(data->list, list);
2122 isl_basic_set_list_free(scc);
2123 if (!data->list)
2124 return isl_stat_error;
2126 return isl_stat_ok;
2129 /* Look for any (weakly connected) components in the "domain_list"
2130 * of domains that share some values of the outer dimensions.
2131 * That is, domains in different components do not share any values
2132 * of the outer dimensions. This means that these components
2133 * can be freely reordered.
2134 * Within each of the components, we sort the domains according
2135 * to the execution order at the current depth.
2137 * If there is more than one component, then generate_sorted_domains_wrap
2138 * fuses the result of each call to generate_sorted_domains
2139 * into a list with either zero or one graft and collects these (at most)
2140 * single element lists into a bigger list. This means that the elements of the
2141 * final list can be freely reordered. In particular, we sort them
2142 * according to an arbitrary but fixed ordering to ease merging of
2143 * graft lists from different components.
2145 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2146 __isl_keep isl_basic_set_list *domain_list,
2147 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2149 int depth;
2150 struct isl_ast_generate_parallel_domains_data data;
2152 if (!domain_list)
2153 return NULL;
2155 data.n = isl_basic_set_list_n_basic_set(domain_list);
2156 if (data.n <= 1)
2157 return generate_sorted_domains(domain_list, executed, build);
2159 depth = isl_ast_build_get_depth(build);
2160 data.list = NULL;
2161 data.executed = executed;
2162 data.build = build;
2163 data.single = 0;
2164 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2165 &generate_sorted_domains_wrap,
2166 &data) < 0)
2167 data.list = isl_ast_graft_list_free(data.list);
2169 if (!data.single)
2170 data.list = isl_ast_graft_list_sort_guard(data.list);
2172 return data.list;
2175 /* Internal data for separate_domain.
2177 * "explicit" is set if we only want to use explicit bounds.
2179 * "domain" collects the separated domains.
2181 struct isl_separate_domain_data {
2182 isl_ast_build *build;
2183 int explicit;
2184 isl_set *domain;
2187 /* Extract implicit bounds on the current dimension for the executed "map".
2189 * The domain of "map" may involve inner dimensions, so we
2190 * need to eliminate them.
2192 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2193 __isl_keep isl_ast_build *build)
2195 isl_set *domain;
2197 domain = isl_map_domain(map);
2198 domain = isl_ast_build_eliminate(build, domain);
2200 return domain;
2203 /* Extract explicit bounds on the current dimension for the executed "map".
2205 * Rather than eliminating the inner dimensions as in implicit_bounds,
2206 * we simply drop any constraints involving those inner dimensions.
2207 * The idea is that most bounds that are implied by constraints on the
2208 * inner dimensions will be enforced by for loops and not by explicit guards.
2209 * There is then no need to separate along those bounds.
2211 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2212 __isl_keep isl_ast_build *build)
2214 isl_set *domain;
2215 int depth, dim;
2217 dim = isl_map_dim(map, isl_dim_out);
2218 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2220 domain = isl_map_domain(map);
2221 depth = isl_ast_build_get_depth(build);
2222 dim = isl_set_dim(domain, isl_dim_set);
2223 domain = isl_set_detect_equalities(domain);
2224 domain = isl_set_drop_constraints_involving_dims(domain,
2225 isl_dim_set, depth + 1, dim - (depth + 1));
2226 domain = isl_set_remove_divs_involving_dims(domain,
2227 isl_dim_set, depth, 1);
2228 domain = isl_set_remove_unknown_divs(domain);
2230 return domain;
2233 /* Split data->domain into pieces that intersect with the range of "map"
2234 * and pieces that do not intersect with the range of "map"
2235 * and then add that part of the range of "map" that does not intersect
2236 * with data->domain.
2238 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2240 struct isl_separate_domain_data *data = user;
2241 isl_set *domain;
2242 isl_set *d1, *d2;
2244 if (data->explicit)
2245 domain = explicit_bounds(map, data->build);
2246 else
2247 domain = implicit_bounds(map, data->build);
2249 domain = isl_set_coalesce(domain);
2250 domain = isl_set_make_disjoint(domain);
2251 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2252 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2253 data->domain = isl_set_intersect(data->domain, domain);
2254 data->domain = isl_set_union(data->domain, d1);
2255 data->domain = isl_set_union(data->domain, d2);
2257 return isl_stat_ok;
2260 /* Separate the schedule domains of "executed".
2262 * That is, break up the domain of "executed" into basic sets,
2263 * such that for each basic set S, every element in S is associated with
2264 * the same domain spaces.
2266 * "space" is the (single) domain space of "executed".
2268 static __isl_give isl_set *separate_schedule_domains(
2269 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2270 __isl_keep isl_ast_build *build)
2272 struct isl_separate_domain_data data = { build };
2273 isl_ctx *ctx;
2275 ctx = isl_ast_build_get_ctx(build);
2276 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2277 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2278 data.domain = isl_set_empty(space);
2279 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2280 data.domain = isl_set_free(data.domain);
2282 isl_union_map_free(executed);
2283 return data.domain;
2286 /* Temporary data used during the search for a lower bound for unrolling.
2288 * "build" is the build in which the unrolling will be performed
2289 * "domain" is the original set for which to find a lower bound
2290 * "depth" is the dimension for which to find a lower boudn
2291 * "expansion" is the expansion that needs to be applied to "domain"
2292 * in the unrolling that will be performed
2294 * "lower" is the best lower bound found so far. It is NULL if we have not
2295 * found any yet.
2296 * "n" is the corresponding size. If lower is NULL, then the value of n
2297 * is undefined.
2298 * "n_div" is the maximal number of integer divisions in the first
2299 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2300 * been computed yet.
2302 struct isl_find_unroll_data {
2303 isl_ast_build *build;
2304 isl_set *domain;
2305 int depth;
2306 isl_basic_map *expansion;
2308 isl_aff *lower;
2309 int *n;
2310 int n_div;
2313 /* Return the constraint
2315 * i_"depth" = aff + offset
2317 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2318 int offset)
2320 aff = isl_aff_copy(aff);
2321 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2322 aff = isl_aff_add_constant_si(aff, offset);
2323 return isl_equality_from_aff(aff);
2326 /* Update *user to the number of integer divsions in the first element
2327 * of "ma", if it is larger than the current value.
2329 static isl_stat update_n_div(__isl_take isl_set *set,
2330 __isl_take isl_multi_aff *ma, void *user)
2332 isl_aff *aff;
2333 int *n = user;
2334 int n_div;
2336 aff = isl_multi_aff_get_aff(ma, 0);
2337 n_div = isl_aff_dim(aff, isl_dim_div);
2338 isl_aff_free(aff);
2339 isl_multi_aff_free(ma);
2340 isl_set_free(set);
2342 if (n_div > *n)
2343 *n = n_div;
2345 return aff ? isl_stat_ok : isl_stat_error;
2348 /* Get the number of integer divisions in the expression for the iterator
2349 * value at the first slice in the unrolling based on lower bound "lower",
2350 * taking into account the expansion that needs to be performed on this slice.
2352 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2353 __isl_keep isl_aff *lower)
2355 isl_constraint *c;
2356 isl_set *set;
2357 isl_map *it_map, *expansion;
2358 isl_pw_multi_aff *pma;
2359 int n;
2361 c = at_offset(data->depth, lower, 0);
2362 set = isl_set_copy(data->domain);
2363 set = isl_set_add_constraint(set, c);
2364 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2365 set = isl_set_apply(set, expansion);
2366 it_map = isl_ast_build_map_to_iterator(data->build, set);
2367 pma = isl_pw_multi_aff_from_map(it_map);
2368 n = 0;
2369 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2370 n = -1;
2371 isl_pw_multi_aff_free(pma);
2373 return n;
2376 /* Is the lower bound "lower" with corresponding iteration count "n"
2377 * better than the one stored in "data"?
2378 * If there is no upper bound on the iteration count ("n" is infinity) or
2379 * if the count is too large, then we cannot use this lower bound.
2380 * Otherwise, if there was no previous lower bound or
2381 * if the iteration count of the new lower bound is smaller than
2382 * the iteration count of the previous lower bound, then we consider
2383 * the new lower bound to be better.
2384 * If the iteration count is the same, then compare the number
2385 * of integer divisions that would be needed to express
2386 * the iterator value at the first slice in the unrolling
2387 * according to the lower bound. If we end up computing this
2388 * number, then store the lowest value in data->n_div.
2390 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2391 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2393 int cmp;
2394 int n_div;
2396 if (!n)
2397 return -1;
2398 if (isl_val_is_infty(n))
2399 return 0;
2400 if (isl_val_cmp_si(n, INT_MAX) > 0)
2401 return 0;
2402 if (!data->lower)
2403 return 1;
2404 cmp = isl_val_cmp_si(n, *data->n);
2405 if (cmp < 0)
2406 return 1;
2407 if (cmp > 0)
2408 return 0;
2409 if (data->n_div < 0)
2410 data->n_div = get_expanded_n_div(data, data->lower);
2411 if (data->n_div < 0)
2412 return -1;
2413 if (data->n_div == 0)
2414 return 0;
2415 n_div = get_expanded_n_div(data, lower);
2416 if (n_div < 0)
2417 return -1;
2418 if (n_div >= data->n_div)
2419 return 0;
2420 data->n_div = n_div;
2422 return 1;
2425 /* Check if we can use "c" as a lower bound and if it is better than
2426 * any previously found lower bound.
2428 * If "c" does not involve the dimension at the current depth,
2429 * then we cannot use it.
2430 * Otherwise, let "c" be of the form
2432 * i >= f(j)/a
2434 * We compute the maximal value of
2436 * -ceil(f(j)/a)) + i + 1
2438 * over the domain. If there is such a value "n", then we know
2440 * -ceil(f(j)/a)) + i + 1 <= n
2442 * or
2444 * i < ceil(f(j)/a)) + n
2446 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2447 * We just need to check if we have found any lower bound before and
2448 * if the new lower bound is better (smaller n or fewer integer divisions)
2449 * than the previously found lower bounds.
2451 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2452 __isl_keep isl_constraint *c)
2454 isl_aff *aff, *lower;
2455 isl_val *max;
2456 int better;
2458 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2459 return isl_stat_ok;
2461 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2462 lower = isl_aff_ceil(lower);
2463 aff = isl_aff_copy(lower);
2464 aff = isl_aff_neg(aff);
2465 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2466 aff = isl_aff_add_constant_si(aff, 1);
2467 max = isl_set_max_val(data->domain, aff);
2468 isl_aff_free(aff);
2470 better = is_better_lower_bound(data, lower, max);
2471 if (better < 0 || !better) {
2472 isl_val_free(max);
2473 isl_aff_free(lower);
2474 return better < 0 ? isl_stat_error : isl_stat_ok;
2477 isl_aff_free(data->lower);
2478 data->lower = lower;
2479 *data->n = isl_val_get_num_si(max);
2480 isl_val_free(max);
2482 return isl_stat_ok;
2485 /* Check if we can use "c" as a lower bound and if it is better than
2486 * any previously found lower bound.
2488 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2490 struct isl_find_unroll_data *data;
2491 isl_stat r;
2493 data = (struct isl_find_unroll_data *) user;
2494 r = update_unrolling_lower_bound(data, c);
2495 isl_constraint_free(c);
2497 return r;
2500 /* Look for a lower bound l(i) on the dimension at "depth"
2501 * and a size n such that "domain" is a subset of
2503 * { [i] : l(i) <= i_d < l(i) + n }
2505 * where d is "depth" and l(i) depends only on earlier dimensions.
2506 * Furthermore, try and find a lower bound such that n is as small as possible.
2507 * In particular, "n" needs to be finite.
2508 * "build" is the build in which the unrolling will be performed.
2509 * "expansion" is the expansion that needs to be applied to "domain"
2510 * in the unrolling that will be performed.
2512 * Inner dimensions have been eliminated from "domain" by the caller.
2514 * We first construct a collection of lower bounds on the input set
2515 * by computing its simple hull. We then iterate through them,
2516 * discarding those that we cannot use (either because they do not
2517 * involve the dimension at "depth" or because they have no corresponding
2518 * upper bound, meaning that "n" would be unbounded) and pick out the
2519 * best from the remaining ones.
2521 * If we cannot find a suitable lower bound, then we consider that
2522 * to be an error.
2524 static __isl_give isl_aff *find_unroll_lower_bound(
2525 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2526 int depth, __isl_keep isl_basic_map *expansion, int *n)
2528 struct isl_find_unroll_data data =
2529 { build, domain, depth, expansion, NULL, n, -1 };
2530 isl_basic_set *hull;
2532 hull = isl_set_simple_hull(isl_set_copy(domain));
2534 if (isl_basic_set_foreach_constraint(hull,
2535 &constraint_find_unroll, &data) < 0)
2536 goto error;
2538 isl_basic_set_free(hull);
2540 if (!data.lower)
2541 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2542 "cannot find lower bound for unrolling", return NULL);
2544 return data.lower;
2545 error:
2546 isl_basic_set_free(hull);
2547 return isl_aff_free(data.lower);
2550 /* Call "fn" on each iteration of the current dimension of "domain".
2551 * If "init" is not NULL, then it is called with the number of
2552 * iterations before any call to "fn".
2553 * Return -1 on failure.
2555 * Since we are going to be iterating over the individual values,
2556 * we first check if there are any strides on the current dimension.
2557 * If there is, we rewrite the current dimension i as
2559 * i = stride i' + offset
2561 * and then iterate over individual values of i' instead.
2563 * We then look for a lower bound on i' and a size such that the domain
2564 * is a subset of
2566 * { [j,i'] : l(j) <= i' < l(j) + n }
2568 * and then take slices of the domain at values of i'
2569 * between l(j) and l(j) + n - 1.
2571 * We compute the unshifted simple hull of each slice to ensure that
2572 * we have a single basic set per offset. The slicing constraint
2573 * may get simplified away before the unshifted simple hull is taken
2574 * and may therefore in some rare cases disappear from the result.
2575 * We therefore explicitly add the constraint back after computing
2576 * the unshifted simple hull to ensure that the basic sets
2577 * remain disjoint. The constraints that are dropped by taking the hull
2578 * will be taken into account at the next level, as in the case of the
2579 * atomic option.
2581 * Finally, we map i' back to i and call "fn".
2583 static int foreach_iteration(__isl_take isl_set *domain,
2584 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2585 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2587 int i, n;
2588 int empty;
2589 int depth;
2590 isl_multi_aff *expansion;
2591 isl_basic_map *bmap;
2592 isl_aff *lower = NULL;
2593 isl_ast_build *stride_build;
2595 depth = isl_ast_build_get_depth(build);
2597 domain = isl_ast_build_eliminate_inner(build, domain);
2598 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2599 stride_build = isl_ast_build_copy(build);
2600 stride_build = isl_ast_build_detect_strides(stride_build,
2601 isl_set_copy(domain));
2602 expansion = isl_ast_build_get_stride_expansion(stride_build);
2604 domain = isl_set_preimage_multi_aff(domain,
2605 isl_multi_aff_copy(expansion));
2606 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2607 isl_ast_build_free(stride_build);
2609 bmap = isl_basic_map_from_multi_aff(expansion);
2611 empty = isl_set_is_empty(domain);
2612 if (empty < 0) {
2613 n = -1;
2614 } else if (empty) {
2615 n = 0;
2616 } else {
2617 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2618 if (!lower)
2619 n = -1;
2621 if (n >= 0 && init && init(n, user) < 0)
2622 n = -1;
2623 for (i = 0; i < n; ++i) {
2624 isl_set *set;
2625 isl_basic_set *bset;
2626 isl_constraint *slice;
2628 slice = at_offset(depth, lower, i);
2629 set = isl_set_copy(domain);
2630 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2631 bset = isl_set_unshifted_simple_hull(set);
2632 bset = isl_basic_set_add_constraint(bset, slice);
2633 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2635 if (fn(bset, user) < 0)
2636 break;
2639 isl_aff_free(lower);
2640 isl_set_free(domain);
2641 isl_basic_map_free(bmap);
2643 return n < 0 || i < n ? -1 : 0;
2646 /* Data structure for storing the results and the intermediate objects
2647 * of compute_domains.
2649 * "list" is the main result of the function and contains a list
2650 * of disjoint basic sets for which code should be generated.
2652 * "executed" and "build" are inputs to compute_domains.
2653 * "schedule_domain" is the domain of "executed".
2655 * "option" contains the domains at the current depth that should by
2656 * atomic, separated or unrolled. These domains are as specified by
2657 * the user, except that inner dimensions have been eliminated and
2658 * that they have been made pair-wise disjoint.
2660 * "sep_class" contains the user-specified split into separation classes
2661 * specialized to the current depth.
2662 * "done" contains the union of the separation domains that have already
2663 * been handled.
2665 struct isl_codegen_domains {
2666 isl_basic_set_list *list;
2668 isl_union_map *executed;
2669 isl_ast_build *build;
2670 isl_set *schedule_domain;
2672 isl_set *option[4];
2674 isl_map *sep_class;
2675 isl_set *done;
2678 /* Internal data structure for do_unroll.
2680 * "domains" stores the results of compute_domains.
2681 * "class_domain" is the original class domain passed to do_unroll.
2682 * "unroll_domain" collects the unrolled iterations.
2684 struct isl_ast_unroll_data {
2685 struct isl_codegen_domains *domains;
2686 isl_set *class_domain;
2687 isl_set *unroll_domain;
2690 /* Given an iteration of an unrolled domain represented by "bset",
2691 * add it to data->domains->list.
2692 * Since we may have dropped some constraints, we intersect with
2693 * the class domain again to ensure that each element in the list
2694 * is disjoint from the other class domains.
2696 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2698 struct isl_ast_unroll_data *data = user;
2699 isl_set *set;
2700 isl_basic_set_list *list;
2702 set = isl_set_from_basic_set(bset);
2703 data->unroll_domain = isl_set_union(data->unroll_domain,
2704 isl_set_copy(set));
2705 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2706 set = isl_set_make_disjoint(set);
2707 list = isl_basic_set_list_from_set(set);
2708 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2709 list);
2711 return 0;
2714 /* Extend domains->list with a list of basic sets, one for each value
2715 * of the current dimension in "domain" and remove the corresponding
2716 * sets from the class domain. Return the updated class domain.
2717 * The divs that involve the current dimension have not been projected out
2718 * from this domain.
2720 * We call foreach_iteration to iterate over the individual values and
2721 * in do_unroll_iteration we collect the individual basic sets in
2722 * domains->list and their union in data->unroll_domain, which is then
2723 * used to update the class domain.
2725 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2726 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2728 struct isl_ast_unroll_data data;
2730 if (!domain)
2731 return isl_set_free(class_domain);
2732 if (!class_domain)
2733 return isl_set_free(domain);
2735 data.domains = domains;
2736 data.class_domain = class_domain;
2737 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2739 if (foreach_iteration(domain, domains->build, NULL,
2740 &do_unroll_iteration, &data) < 0)
2741 data.unroll_domain = isl_set_free(data.unroll_domain);
2743 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2745 return class_domain;
2748 /* Add domains to domains->list for each individual value of the current
2749 * dimension, for that part of the schedule domain that lies in the
2750 * intersection of the option domain and the class domain.
2751 * Remove the corresponding sets from the class domain and
2752 * return the updated class domain.
2754 * We first break up the unroll option domain into individual pieces
2755 * and then handle each of them separately. The unroll option domain
2756 * has been made disjoint in compute_domains_init_options,
2758 * Note that we actively want to combine different pieces of the
2759 * schedule domain that have the same value at the current dimension.
2760 * We therefore need to break up the unroll option domain before
2761 * intersecting with class and schedule domain, hoping that the
2762 * unroll option domain specified by the user is relatively simple.
2764 static __isl_give isl_set *compute_unroll_domains(
2765 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2767 isl_set *unroll_domain;
2768 isl_basic_set_list *unroll_list;
2769 int i, n;
2770 int empty;
2772 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2773 if (empty < 0)
2774 return isl_set_free(class_domain);
2775 if (empty)
2776 return class_domain;
2778 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2779 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2781 n = isl_basic_set_list_n_basic_set(unroll_list);
2782 for (i = 0; i < n; ++i) {
2783 isl_basic_set *bset;
2785 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2786 unroll_domain = isl_set_from_basic_set(bset);
2787 unroll_domain = isl_set_intersect(unroll_domain,
2788 isl_set_copy(class_domain));
2789 unroll_domain = isl_set_intersect(unroll_domain,
2790 isl_set_copy(domains->schedule_domain));
2792 empty = isl_set_is_empty(unroll_domain);
2793 if (empty >= 0 && empty) {
2794 isl_set_free(unroll_domain);
2795 continue;
2798 class_domain = do_unroll(domains, unroll_domain, class_domain);
2801 isl_basic_set_list_free(unroll_list);
2803 return class_domain;
2806 /* Try and construct a single basic set that includes the intersection of
2807 * the schedule domain, the atomic option domain and the class domain.
2808 * Add the resulting basic set(s) to domains->list and remove them
2809 * from class_domain. Return the updated class domain.
2811 * We construct a single domain rather than trying to combine
2812 * the schedule domains of individual domains because we are working
2813 * within a single component so that non-overlapping schedule domains
2814 * should already have been separated.
2815 * We do however need to make sure that this single domains is a subset
2816 * of the class domain so that it would not intersect with any other
2817 * class domains. This means that we may end up splitting up the atomic
2818 * domain in case separation classes are being used.
2820 * "domain" is the intersection of the schedule domain and the class domain,
2821 * with inner dimensions projected out.
2823 static __isl_give isl_set *compute_atomic_domain(
2824 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2826 isl_basic_set *bset;
2827 isl_basic_set_list *list;
2828 isl_set *domain, *atomic_domain;
2829 int empty;
2831 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2832 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2833 domain = isl_set_intersect(domain,
2834 isl_set_copy(domains->schedule_domain));
2835 empty = isl_set_is_empty(domain);
2836 if (empty < 0)
2837 class_domain = isl_set_free(class_domain);
2838 if (empty) {
2839 isl_set_free(domain);
2840 return class_domain;
2843 domain = isl_ast_build_eliminate(domains->build, domain);
2844 domain = isl_set_coalesce(domain);
2845 bset = isl_set_unshifted_simple_hull(domain);
2846 domain = isl_set_from_basic_set(bset);
2847 atomic_domain = isl_set_copy(domain);
2848 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2849 class_domain = isl_set_subtract(class_domain, atomic_domain);
2850 domain = isl_set_make_disjoint(domain);
2851 list = isl_basic_set_list_from_set(domain);
2852 domains->list = isl_basic_set_list_concat(domains->list, list);
2854 return class_domain;
2857 /* Split up the schedule domain into uniform basic sets,
2858 * in the sense that each element in a basic set is associated to
2859 * elements of the same domains, and add the result to domains->list.
2860 * Do this for that part of the schedule domain that lies in the
2861 * intersection of "class_domain" and the separate option domain.
2863 * "class_domain" may or may not include the constraints
2864 * of the schedule domain, but this does not make a difference
2865 * since we are going to intersect it with the domain of the inverse schedule.
2866 * If it includes schedule domain constraints, then they may involve
2867 * inner dimensions, but we will eliminate them in separation_domain.
2869 static int compute_separate_domain(struct isl_codegen_domains *domains,
2870 __isl_keep isl_set *class_domain)
2872 isl_space *space;
2873 isl_set *domain;
2874 isl_union_map *executed;
2875 isl_basic_set_list *list;
2876 int empty;
2878 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2879 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2880 executed = isl_union_map_copy(domains->executed);
2881 executed = isl_union_map_intersect_domain(executed,
2882 isl_union_set_from_set(domain));
2883 empty = isl_union_map_is_empty(executed);
2884 if (empty < 0 || empty) {
2885 isl_union_map_free(executed);
2886 return empty < 0 ? -1 : 0;
2889 space = isl_set_get_space(class_domain);
2890 domain = separate_schedule_domains(space, executed, domains->build);
2892 list = isl_basic_set_list_from_set(domain);
2893 domains->list = isl_basic_set_list_concat(domains->list, list);
2895 return 0;
2898 /* Split up the domain at the current depth into disjoint
2899 * basic sets for which code should be generated separately
2900 * for the given separation class domain.
2902 * If any separation classes have been defined, then "class_domain"
2903 * is the domain of the current class and does not refer to inner dimensions.
2904 * Otherwise, "class_domain" is the universe domain.
2906 * We first make sure that the class domain is disjoint from
2907 * previously considered class domains.
2909 * The separate domains can be computed directly from the "class_domain".
2911 * The unroll, atomic and remainder domains need the constraints
2912 * from the schedule domain.
2914 * For unrolling, the actual schedule domain is needed (with divs that
2915 * may refer to the current dimension) so that stride detection can be
2916 * performed.
2918 * For atomic and remainder domains, inner dimensions and divs involving
2919 * the current dimensions should be eliminated.
2920 * In case we are working within a separation class, we need to intersect
2921 * the result with the current "class_domain" to ensure that the domains
2922 * are disjoint from those generated from other class domains.
2924 * The domain that has been made atomic may be larger than specified
2925 * by the user since it needs to be representable as a single basic set.
2926 * This possibly larger domain is removed from class_domain by
2927 * compute_atomic_domain. It is computed first so that the extended domain
2928 * would not overlap with any domains computed before.
2929 * Similary, the unrolled domains may have some constraints removed and
2930 * may therefore also be larger than specified by the user.
2932 * If anything is left after handling separate, unroll and atomic,
2933 * we split it up into basic sets and append the basic sets to domains->list.
2935 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2936 __isl_take isl_set *class_domain)
2938 isl_basic_set_list *list;
2939 isl_set *domain;
2941 class_domain = isl_set_subtract(class_domain,
2942 isl_set_copy(domains->done));
2943 domains->done = isl_set_union(domains->done,
2944 isl_set_copy(class_domain));
2946 class_domain = compute_atomic_domain(domains, class_domain);
2947 class_domain = compute_unroll_domains(domains, class_domain);
2949 domain = isl_set_copy(class_domain);
2951 if (compute_separate_domain(domains, domain) < 0)
2952 goto error;
2953 domain = isl_set_subtract(domain,
2954 isl_set_copy(domains->option[isl_ast_loop_separate]));
2956 domain = isl_set_intersect(domain,
2957 isl_set_copy(domains->schedule_domain));
2959 domain = isl_ast_build_eliminate(domains->build, domain);
2960 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2962 domain = isl_set_coalesce(domain);
2963 domain = isl_set_make_disjoint(domain);
2965 list = isl_basic_set_list_from_set(domain);
2966 domains->list = isl_basic_set_list_concat(domains->list, list);
2968 isl_set_free(class_domain);
2970 return isl_stat_ok;
2971 error:
2972 isl_set_free(domain);
2973 isl_set_free(class_domain);
2974 return isl_stat_error;
2977 /* Split up the domain at the current depth into disjoint
2978 * basic sets for which code should be generated separately
2979 * for the separation class identified by "pnt".
2981 * We extract the corresponding class domain from domains->sep_class,
2982 * eliminate inner dimensions and pass control to compute_partial_domains.
2984 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2986 struct isl_codegen_domains *domains = user;
2987 isl_set *class_set;
2988 isl_set *domain;
2989 int disjoint;
2991 class_set = isl_set_from_point(pnt);
2992 domain = isl_map_domain(isl_map_intersect_range(
2993 isl_map_copy(domains->sep_class), class_set));
2994 domain = isl_ast_build_compute_gist(domains->build, domain);
2995 domain = isl_ast_build_eliminate(domains->build, domain);
2997 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2998 if (disjoint < 0)
2999 return isl_stat_error;
3000 if (disjoint) {
3001 isl_set_free(domain);
3002 return isl_stat_ok;
3005 return compute_partial_domains(domains, domain);
3008 /* Extract the domains at the current depth that should be atomic,
3009 * separated or unrolled and store them in option.
3011 * The domains specified by the user might overlap, so we make
3012 * them disjoint by subtracting earlier domains from later domains.
3014 static void compute_domains_init_options(isl_set *option[4],
3015 __isl_keep isl_ast_build *build)
3017 enum isl_ast_loop_type type, type2;
3018 isl_set *unroll;
3020 for (type = isl_ast_loop_atomic;
3021 type <= isl_ast_loop_separate; ++type) {
3022 option[type] = isl_ast_build_get_option_domain(build, type);
3023 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3024 option[type] = isl_set_subtract(option[type],
3025 isl_set_copy(option[type2]));
3028 unroll = option[isl_ast_loop_unroll];
3029 unroll = isl_set_coalesce(unroll);
3030 unroll = isl_set_make_disjoint(unroll);
3031 option[isl_ast_loop_unroll] = unroll;
3034 /* Split up the domain at the current depth into disjoint
3035 * basic sets for which code should be generated separately,
3036 * based on the user-specified options.
3037 * Return the list of disjoint basic sets.
3039 * There are three kinds of domains that we need to keep track of.
3040 * - the "schedule domain" is the domain of "executed"
3041 * - the "class domain" is the domain corresponding to the currrent
3042 * separation class
3043 * - the "option domain" is the domain corresponding to one of the options
3044 * atomic, unroll or separate
3046 * We first consider the individial values of the separation classes
3047 * and split up the domain for each of them separately.
3048 * Finally, we consider the remainder. If no separation classes were
3049 * specified, then we call compute_partial_domains with the universe
3050 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3051 * with inner dimensions removed. We do this because we want to
3052 * avoid computing the complement of the class domains (i.e., the difference
3053 * between the universe and domains->done).
3055 static __isl_give isl_basic_set_list *compute_domains(
3056 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3058 struct isl_codegen_domains domains;
3059 isl_ctx *ctx;
3060 isl_set *domain;
3061 isl_union_set *schedule_domain;
3062 isl_set *classes;
3063 isl_space *space;
3064 int n_param;
3065 enum isl_ast_loop_type type;
3066 int empty;
3068 if (!executed)
3069 return NULL;
3071 ctx = isl_union_map_get_ctx(executed);
3072 domains.list = isl_basic_set_list_alloc(ctx, 0);
3074 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3075 domain = isl_set_from_union_set(schedule_domain);
3077 compute_domains_init_options(domains.option, build);
3079 domains.sep_class = isl_ast_build_get_separation_class(build);
3080 classes = isl_map_range(isl_map_copy(domains.sep_class));
3081 n_param = isl_set_dim(classes, isl_dim_param);
3082 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3084 space = isl_set_get_space(domain);
3085 domains.build = build;
3086 domains.schedule_domain = isl_set_copy(domain);
3087 domains.executed = executed;
3088 domains.done = isl_set_empty(space);
3090 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3091 domains.list = isl_basic_set_list_free(domains.list);
3092 isl_set_free(classes);
3094 empty = isl_set_is_empty(domains.done);
3095 if (empty < 0) {
3096 domains.list = isl_basic_set_list_free(domains.list);
3097 domain = isl_set_free(domain);
3098 } else if (empty) {
3099 isl_set_free(domain);
3100 domain = isl_set_universe(isl_set_get_space(domains.done));
3101 } else {
3102 domain = isl_ast_build_eliminate(build, domain);
3104 if (compute_partial_domains(&domains, domain) < 0)
3105 domains.list = isl_basic_set_list_free(domains.list);
3107 isl_set_free(domains.schedule_domain);
3108 isl_set_free(domains.done);
3109 isl_map_free(domains.sep_class);
3110 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3111 isl_set_free(domains.option[type]);
3113 return domains.list;
3116 /* Generate code for a single component, after shifting (if any)
3117 * has been applied, in case the schedule was specified as a union map.
3119 * We first split up the domain at the current depth into disjoint
3120 * basic sets based on the user-specified options.
3121 * Then we generated code for each of them and concatenate the results.
3123 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3124 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3126 isl_basic_set_list *domain_list;
3127 isl_ast_graft_list *list = NULL;
3129 domain_list = compute_domains(executed, build);
3130 list = generate_parallel_domains(domain_list, executed, build);
3132 isl_basic_set_list_free(domain_list);
3133 isl_union_map_free(executed);
3134 isl_ast_build_free(build);
3136 return list;
3139 /* Generate code for a single component, after shifting (if any)
3140 * has been applied, in case the schedule was specified as a schedule tree
3141 * and the separate option was specified.
3143 * We perform separation on the domain of "executed" and then generate
3144 * an AST for each of the resulting disjoint basic sets.
3146 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3147 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3149 isl_space *space;
3150 isl_set *domain;
3151 isl_basic_set_list *domain_list;
3152 isl_ast_graft_list *list;
3154 space = isl_ast_build_get_space(build, 1);
3155 domain = separate_schedule_domains(space,
3156 isl_union_map_copy(executed), build);
3157 domain_list = isl_basic_set_list_from_set(domain);
3159 list = generate_parallel_domains(domain_list, executed, build);
3161 isl_basic_set_list_free(domain_list);
3162 isl_union_map_free(executed);
3163 isl_ast_build_free(build);
3165 return list;
3168 /* Internal data structure for generate_shifted_component_tree_unroll.
3170 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3171 * "list" collects the constructs grafts.
3173 struct isl_ast_unroll_tree_data {
3174 isl_union_map *executed;
3175 isl_ast_build *build;
3176 isl_ast_graft_list *list;
3179 /* Initialize data->list to a list of "n" elements.
3181 static int init_unroll_tree(int n, void *user)
3183 struct isl_ast_unroll_tree_data *data = user;
3184 isl_ctx *ctx;
3186 ctx = isl_ast_build_get_ctx(data->build);
3187 data->list = isl_ast_graft_list_alloc(ctx, n);
3189 return 0;
3192 /* Given an iteration of an unrolled domain represented by "bset",
3193 * generate the corresponding AST and add the result to data->list.
3195 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3197 struct isl_ast_unroll_tree_data *data = user;
3199 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3200 bset, isl_ast_build_copy(data->build));
3202 return 0;
3205 /* Generate code for a single component, after shifting (if any)
3206 * has been applied, in case the schedule was specified as a schedule tree
3207 * and the unroll option was specified.
3209 * We call foreach_iteration to iterate over the individual values and
3210 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3212 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3213 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3214 __isl_take isl_ast_build *build)
3216 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3218 if (foreach_iteration(domain, build, &init_unroll_tree,
3219 &do_unroll_tree_iteration, &data) < 0)
3220 data.list = isl_ast_graft_list_free(data.list);
3222 isl_union_map_free(executed);
3223 isl_ast_build_free(build);
3225 return data.list;
3228 /* Does "domain" involve a disjunction that is purely based on
3229 * constraints involving only outer dimension?
3231 * In particular, is there a disjunction such that the constraints
3232 * involving the current and later dimensions are the same over
3233 * all the disjuncts?
3235 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3236 __isl_keep isl_ast_build *build)
3238 isl_basic_set *hull;
3239 isl_set *shared, *inner;
3240 isl_bool equal;
3241 int depth, dim;
3243 if (isl_set_n_basic_set(domain) <= 1)
3244 return isl_bool_false;
3246 inner = isl_set_copy(domain);
3247 depth = isl_ast_build_get_depth(build);
3248 dim = isl_set_dim(inner, isl_dim_set);
3249 inner = isl_set_drop_constraints_not_involving_dims(inner,
3250 isl_dim_set, depth, dim - depth);
3251 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3252 shared = isl_set_from_basic_set(hull);
3253 equal = isl_set_plain_is_equal(inner, shared);
3254 isl_set_free(inner);
3255 isl_set_free(shared);
3257 return equal;
3260 /* Generate code for a single component, after shifting (if any)
3261 * has been applied, in case the schedule was specified as a schedule tree.
3262 * In particular, handle the base case where there is either no isolated
3263 * set or we are within the isolated set (in which case "isolated" is set)
3264 * or the iterations that precede or follow the isolated set.
3266 * The schedule domain is broken up or combined into basic sets
3267 * according to the AST generation option specified in the current
3268 * schedule node, which may be either atomic, separate, unroll or
3269 * unspecified. If the option is unspecified, then we currently simply
3270 * split the schedule domain into disjoint basic sets.
3272 * In case the separate option is specified, the AST generation is
3273 * handled by generate_shifted_component_tree_separate.
3274 * In the other cases, we need the global schedule domain.
3275 * In the unroll case, the AST generation is then handled by
3276 * generate_shifted_component_tree_unroll which needs the actual
3277 * schedule domain (with divs that may refer to the current dimension)
3278 * so that stride detection can be performed.
3279 * In the atomic or unspecified case, inner dimensions and divs involving
3280 * the current dimensions should be eliminated.
3281 * The result is then either combined into a single basic set or
3282 * split up into disjoint basic sets.
3283 * Finally an AST is generated for each basic set and the results are
3284 * concatenated.
3286 * If the schedule domain involves a disjunction that is purely based on
3287 * constraints involving only outer dimension, then it is treated as
3288 * if atomic was specified. This ensures that only a single loop
3289 * is generated instead of a sequence of identical loops with
3290 * different guards.
3292 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3293 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3294 int isolated)
3296 isl_bool outer_disjunction;
3297 isl_union_set *schedule_domain;
3298 isl_set *domain;
3299 isl_basic_set_list *domain_list;
3300 isl_ast_graft_list *list;
3301 enum isl_ast_loop_type type;
3303 type = isl_ast_build_get_loop_type(build, isolated);
3304 if (type < 0)
3305 goto error;
3307 if (type == isl_ast_loop_separate)
3308 return generate_shifted_component_tree_separate(executed,
3309 build);
3311 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3312 domain = isl_set_from_union_set(schedule_domain);
3314 if (type == isl_ast_loop_unroll)
3315 return generate_shifted_component_tree_unroll(executed, domain,
3316 build);
3318 domain = isl_ast_build_eliminate(build, domain);
3319 domain = isl_set_coalesce(domain);
3321 outer_disjunction = has_pure_outer_disjunction(domain, build);
3322 if (outer_disjunction < 0)
3323 domain = isl_set_free(domain);
3325 if (outer_disjunction || type == isl_ast_loop_atomic) {
3326 isl_basic_set *hull;
3327 hull = isl_set_unshifted_simple_hull(domain);
3328 domain_list = isl_basic_set_list_from_basic_set(hull);
3329 } else {
3330 domain = isl_set_make_disjoint(domain);
3331 domain_list = isl_basic_set_list_from_set(domain);
3334 list = generate_parallel_domains(domain_list, executed, build);
3336 isl_basic_set_list_free(domain_list);
3337 isl_union_map_free(executed);
3338 isl_ast_build_free(build);
3340 return list;
3341 error:
3342 isl_union_map_free(executed);
3343 isl_ast_build_free(build);
3344 return NULL;
3347 /* Extract out the disjunction imposed by "domain" on the outer
3348 * schedule dimensions.
3350 * In particular, remove all inner dimensions from "domain" (including
3351 * the current dimension) and then remove the constraints that are shared
3352 * by all disjuncts in the result.
3354 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3355 __isl_keep isl_ast_build *build)
3357 isl_set *hull;
3358 int depth, dim;
3360 domain = isl_ast_build_specialize(build, domain);
3361 depth = isl_ast_build_get_depth(build);
3362 dim = isl_set_dim(domain, isl_dim_set);
3363 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3364 domain = isl_set_remove_unknown_divs(domain);
3365 hull = isl_set_copy(domain);
3366 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3367 domain = isl_set_gist(domain, hull);
3369 return domain;
3372 /* Add "guard" to the grafts in "list".
3373 * "build" is the outer AST build, while "sub_build" includes "guard"
3374 * in its generated domain.
3376 * First combine the grafts into a single graft and then add the guard.
3377 * If the list is empty, or if some error occurred, then simply return
3378 * the list.
3380 static __isl_give isl_ast_graft_list *list_add_guard(
3381 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3382 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3384 isl_ast_graft *graft;
3386 list = isl_ast_graft_list_fuse(list, sub_build);
3388 if (isl_ast_graft_list_n_ast_graft(list) != 1)
3389 return list;
3391 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3392 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3393 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3395 return list;
3398 /* Generate code for a single component, after shifting (if any)
3399 * has been applied, in case the schedule was specified as a schedule tree.
3400 * In particular, do so for the specified subset of the schedule domain.
3402 * If we are outside of the isolated part, then "domain" may include
3403 * a disjunction. Explicitly generate this disjunction at this point
3404 * instead of relying on the disjunction getting hoisted back up
3405 * to this level.
3407 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3408 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3409 __isl_keep isl_ast_build *build, int isolated)
3411 isl_union_set *uset;
3412 isl_ast_graft_list *list;
3413 isl_ast_build *sub_build;
3414 int empty;
3416 uset = isl_union_set_from_set(isl_set_copy(domain));
3417 executed = isl_union_map_copy(executed);
3418 executed = isl_union_map_intersect_domain(executed, uset);
3419 empty = isl_union_map_is_empty(executed);
3420 if (empty < 0)
3421 goto error;
3422 if (empty) {
3423 isl_ctx *ctx;
3424 isl_union_map_free(executed);
3425 isl_set_free(domain);
3426 ctx = isl_ast_build_get_ctx(build);
3427 return isl_ast_graft_list_alloc(ctx, 0);
3430 sub_build = isl_ast_build_copy(build);
3431 if (!isolated) {
3432 domain = extract_disjunction(domain, build);
3433 sub_build = isl_ast_build_restrict_generated(sub_build,
3434 isl_set_copy(domain));
3436 list = generate_shifted_component_tree_base(executed,
3437 isl_ast_build_copy(sub_build), isolated);
3438 if (!isolated)
3439 list = list_add_guard(list, domain, build, sub_build);
3440 isl_ast_build_free(sub_build);
3441 isl_set_free(domain);
3442 return list;
3443 error:
3444 isl_union_map_free(executed);
3445 isl_set_free(domain);
3446 return NULL;
3449 /* Generate code for a single component, after shifting (if any)
3450 * has been applied, in case the schedule was specified as a schedule tree.
3451 * In particular, do so for the specified sequence of subsets
3452 * of the schedule domain, "before", "isolated", "after" and "other",
3453 * where only the "isolated" part is considered to be isolated.
3455 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3456 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3457 __isl_take isl_set *isolated, __isl_take isl_set *after,
3458 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3460 isl_ast_graft_list *list, *res;
3462 res = generate_shifted_component_tree_part(executed, before, build, 0);
3463 list = generate_shifted_component_tree_part(executed, isolated,
3464 build, 1);
3465 res = isl_ast_graft_list_concat(res, list);
3466 list = generate_shifted_component_tree_part(executed, after, build, 0);
3467 res = isl_ast_graft_list_concat(res, list);
3468 list = generate_shifted_component_tree_part(executed, other, build, 0);
3469 res = isl_ast_graft_list_concat(res, list);
3471 isl_union_map_free(executed);
3472 isl_ast_build_free(build);
3474 return res;
3477 /* Does "set" intersect "first", but not "second"?
3479 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3480 __isl_keep isl_set *first, __isl_keep isl_set *second)
3482 isl_bool disjoint;
3484 disjoint = isl_set_is_disjoint(set, first);
3485 if (disjoint < 0)
3486 return isl_bool_error;
3487 if (disjoint)
3488 return isl_bool_false;
3490 return isl_set_is_disjoint(set, second);
3493 /* Generate code for a single component, after shifting (if any)
3494 * has been applied, in case the schedule was specified as a schedule tree.
3495 * In particular, do so in case of isolation where there is
3496 * only an "isolated" part and an "after" part.
3497 * "dead1" and "dead2" are freed by this function in order to simplify
3498 * the caller.
3500 * The "before" and "other" parts are set to empty sets.
3502 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3503 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3504 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3505 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3507 isl_set *empty;
3509 empty = isl_set_empty(isl_set_get_space(after));
3510 isl_set_free(dead1);
3511 isl_set_free(dead2);
3512 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3513 isolated, after, empty, build);
3516 /* Generate code for a single component, after shifting (if any)
3517 * has been applied, in case the schedule was specified as a schedule tree.
3519 * We first check if the user has specified an isolated schedule domain
3520 * and that we are not already outside of this isolated schedule domain.
3521 * If so, we break up the schedule domain into iterations that
3522 * precede the isolated domain, the isolated domain itself,
3523 * the iterations that follow the isolated domain and
3524 * the remaining iterations (those that are incomparable
3525 * to the isolated domain).
3526 * We generate an AST for each piece and concatenate the results.
3528 * If the isolated domain is not convex, then it is replaced
3529 * by a convex superset to ensure that the sets of preceding and
3530 * following iterations are properly defined and, in particular,
3531 * that there are no intermediate iterations that do not belong
3532 * to the isolated domain.
3534 * In the special case where at least one element of the schedule
3535 * domain that does not belong to the isolated domain needs
3536 * to be scheduled after this isolated domain, but none of those
3537 * elements need to be scheduled before, break up the schedule domain
3538 * in only two parts, the isolated domain, and a part that will be
3539 * scheduled after the isolated domain.
3541 * If no isolated set has been specified, then we generate an
3542 * AST for the entire inverse schedule.
3544 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3545 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3547 int i, depth;
3548 int empty, has_isolate;
3549 isl_space *space;
3550 isl_union_set *schedule_domain;
3551 isl_set *domain;
3552 isl_basic_set *hull;
3553 isl_set *isolated, *before, *after, *test;
3554 isl_map *gt, *lt;
3555 isl_bool pure;
3557 build = isl_ast_build_extract_isolated(build);
3558 has_isolate = isl_ast_build_has_isolated(build);
3559 if (has_isolate < 0)
3560 executed = isl_union_map_free(executed);
3561 else if (!has_isolate)
3562 return generate_shifted_component_tree_base(executed, build, 0);
3564 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3565 domain = isl_set_from_union_set(schedule_domain);
3567 isolated = isl_ast_build_get_isolated(build);
3568 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3569 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3570 empty = isl_set_is_empty(test);
3571 isl_set_free(test);
3572 if (empty < 0)
3573 goto error;
3574 if (empty) {
3575 isl_set_free(isolated);
3576 isl_set_free(domain);
3577 return generate_shifted_component_tree_base(executed, build, 0);
3579 isolated = isl_ast_build_eliminate(build, isolated);
3580 hull = isl_set_unshifted_simple_hull(isolated);
3581 isolated = isl_set_from_basic_set(hull);
3583 depth = isl_ast_build_get_depth(build);
3584 space = isl_space_map_from_set(isl_set_get_space(isolated));
3585 gt = isl_map_universe(space);
3586 for (i = 0; i < depth; ++i)
3587 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3588 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3589 lt = isl_map_reverse(isl_map_copy(gt));
3590 before = isl_set_apply(isl_set_copy(isolated), gt);
3591 after = isl_set_apply(isl_set_copy(isolated), lt);
3593 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3594 pure = only_intersects_first(domain, after, before);
3595 if (pure < 0)
3596 executed = isl_union_map_free(executed);
3597 else if (pure)
3598 return generate_shifted_component_only_after(executed, isolated,
3599 domain, build, before, after);
3600 domain = isl_set_subtract(domain, isl_set_copy(before));
3601 domain = isl_set_subtract(domain, isl_set_copy(after));
3602 after = isl_set_subtract(after, isl_set_copy(isolated));
3603 after = isl_set_subtract(after, isl_set_copy(before));
3604 before = isl_set_subtract(before, isl_set_copy(isolated));
3606 return generate_shifted_component_parts(executed, before, isolated,
3607 after, domain, build);
3608 error:
3609 isl_set_free(domain);
3610 isl_set_free(isolated);
3611 isl_union_map_free(executed);
3612 isl_ast_build_free(build);
3613 return NULL;
3616 /* Generate code for a single component, after shifting (if any)
3617 * has been applied.
3619 * Call generate_shifted_component_tree or generate_shifted_component_flat
3620 * depending on whether the schedule was specified as a schedule tree.
3622 static __isl_give isl_ast_graft_list *generate_shifted_component(
3623 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3625 if (isl_ast_build_has_schedule_node(build))
3626 return generate_shifted_component_tree(executed, build);
3627 else
3628 return generate_shifted_component_flat(executed, build);
3631 struct isl_set_map_pair {
3632 isl_set *set;
3633 isl_map *map;
3636 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3637 * of indices into the "domain" array,
3638 * return the union of the "map" fields of the elements
3639 * indexed by the first "n" elements of "order".
3641 static __isl_give isl_union_map *construct_component_executed(
3642 struct isl_set_map_pair *domain, int *order, int n)
3644 int i;
3645 isl_map *map;
3646 isl_union_map *executed;
3648 map = isl_map_copy(domain[order[0]].map);
3649 executed = isl_union_map_from_map(map);
3650 for (i = 1; i < n; ++i) {
3651 map = isl_map_copy(domain[order[i]].map);
3652 executed = isl_union_map_add_map(executed, map);
3655 return executed;
3658 /* Generate code for a single component, after shifting (if any)
3659 * has been applied.
3661 * The component inverse schedule is specified as the "map" fields
3662 * of the elements of "domain" indexed by the first "n" elements of "order".
3664 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3665 struct isl_set_map_pair *domain, int *order, int n,
3666 __isl_take isl_ast_build *build)
3668 isl_union_map *executed;
3670 executed = construct_component_executed(domain, order, n);
3671 return generate_shifted_component(executed, build);
3674 /* Does set dimension "pos" of "set" have an obviously fixed value?
3676 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3678 int fixed;
3679 isl_val *v;
3681 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3682 if (!v)
3683 return -1;
3684 fixed = !isl_val_is_nan(v);
3685 isl_val_free(v);
3687 return fixed;
3690 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3691 * of indices into the "domain" array,
3692 * do all (except for at most one) of the "set" field of the elements
3693 * indexed by the first "n" elements of "order" have a fixed value
3694 * at position "depth"?
3696 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3697 int *order, int n, int depth)
3699 int i;
3700 int non_fixed = -1;
3702 for (i = 0; i < n; ++i) {
3703 int f;
3705 f = dim_is_fixed(domain[order[i]].set, depth);
3706 if (f < 0)
3707 return -1;
3708 if (f)
3709 continue;
3710 if (non_fixed >= 0)
3711 return 0;
3712 non_fixed = i;
3715 return 1;
3718 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3719 * of indices into the "domain" array,
3720 * eliminate the inner dimensions from the "set" field of the elements
3721 * indexed by the first "n" elements of "order", provided the current
3722 * dimension does not have a fixed value.
3724 * Return the index of the first element in "order" with a corresponding
3725 * "set" field that does not have an (obviously) fixed value.
3727 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3728 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3730 int i;
3731 int base = -1;
3733 for (i = n - 1; i >= 0; --i) {
3734 int f;
3735 f = dim_is_fixed(domain[order[i]].set, depth);
3736 if (f < 0)
3737 return -1;
3738 if (f)
3739 continue;
3740 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3741 domain[order[i]].set);
3742 base = i;
3745 return base;
3748 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3749 * of indices into the "domain" array,
3750 * find the element of "domain" (amongst those indexed by the first "n"
3751 * elements of "order") with the "set" field that has the smallest
3752 * value for the current iterator.
3754 * Note that the domain with the smallest value may depend on the parameters
3755 * and/or outer loop dimension. Since the result of this function is only
3756 * used as heuristic, we only make a reasonable attempt at finding the best
3757 * domain, one that should work in case a single domain provides the smallest
3758 * value for the current dimension over all values of the parameters
3759 * and outer dimensions.
3761 * In particular, we compute the smallest value of the first domain
3762 * and replace it by that of any later domain if that later domain
3763 * has a smallest value that is smaller for at least some value
3764 * of the parameters and outer dimensions.
3766 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3767 __isl_keep isl_ast_build *build)
3769 int i;
3770 isl_map *min_first;
3771 int first = 0;
3773 min_first = isl_ast_build_map_to_iterator(build,
3774 isl_set_copy(domain[order[0]].set));
3775 min_first = isl_map_lexmin(min_first);
3777 for (i = 1; i < n; ++i) {
3778 isl_map *min, *test;
3779 int empty;
3781 min = isl_ast_build_map_to_iterator(build,
3782 isl_set_copy(domain[order[i]].set));
3783 min = isl_map_lexmin(min);
3784 test = isl_map_copy(min);
3785 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3786 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3787 empty = isl_map_is_empty(test);
3788 isl_map_free(test);
3789 if (empty >= 0 && !empty) {
3790 isl_map_free(min_first);
3791 first = i;
3792 min_first = min;
3793 } else
3794 isl_map_free(min);
3796 if (empty < 0)
3797 break;
3800 isl_map_free(min_first);
3802 return i < n ? -1 : first;
3805 /* Construct a shifted inverse schedule based on the original inverse schedule,
3806 * the stride and the offset.
3808 * The original inverse schedule is specified as the "map" fields
3809 * of the elements of "domain" indexed by the first "n" elements of "order".
3811 * "stride" and "offset" are such that the difference
3812 * between the values of the current dimension of domain "i"
3813 * and the values of the current dimension for some reference domain are
3814 * equal to
3816 * stride * integer + offset[i]
3818 * Moreover, 0 <= offset[i] < stride.
3820 * For each domain, we create a map
3822 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3824 * where j refers to the current dimension and the other dimensions are
3825 * unchanged, and apply this map to the original schedule domain.
3827 * For example, for the original schedule
3829 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3831 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3832 * we apply the mapping
3834 * { [j] -> [j, 0] }
3836 * to the schedule of the "A" domain and the mapping
3838 * { [j - 1] -> [j, 1] }
3840 * to the schedule of the "B" domain.
3843 * Note that after the transformation, the differences between pairs
3844 * of values of the current dimension over all domains are multiples
3845 * of stride and that we have therefore exposed the stride.
3848 * To see that the mapping preserves the lexicographic order,
3849 * first note that each of the individual maps above preserves the order.
3850 * If the value of the current iterator is j1 in one domain and j2 in another,
3851 * then if j1 = j2, we know that the same map is applied to both domains
3852 * and the order is preserved.
3853 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3854 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3856 * j1 - c1 < j2 - c2
3858 * and the order is preserved.
3859 * If c1 < c2, then we know
3861 * 0 <= c2 - c1 < s
3863 * We also have
3865 * j2 - j1 = n * s + r
3867 * with n >= 0 and 0 <= r < s.
3868 * In other words, r = c2 - c1.
3869 * If n > 0, then
3871 * j1 - c1 < j2 - c2
3873 * If n = 0, then
3875 * j1 - c1 = j2 - c2
3877 * and so
3879 * (j1 - c1, c1) << (j2 - c2, c2)
3881 * with "<<" the lexicographic order, proving that the order is preserved
3882 * in all cases.
3884 static __isl_give isl_union_map *construct_shifted_executed(
3885 struct isl_set_map_pair *domain, int *order, int n,
3886 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3887 __isl_take isl_ast_build *build)
3889 int i;
3890 isl_union_map *executed;
3891 isl_space *space;
3892 isl_map *map;
3893 int depth;
3894 isl_constraint *c;
3896 depth = isl_ast_build_get_depth(build);
3897 space = isl_ast_build_get_space(build, 1);
3898 executed = isl_union_map_empty(isl_space_copy(space));
3899 space = isl_space_map_from_set(space);
3900 map = isl_map_identity(isl_space_copy(space));
3901 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3902 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3903 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3905 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3906 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3907 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3909 for (i = 0; i < n; ++i) {
3910 isl_map *map_i;
3911 isl_val *v;
3913 v = isl_multi_val_get_val(offset, i);
3914 if (!v)
3915 break;
3916 map_i = isl_map_copy(map);
3917 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3918 isl_val_copy(v));
3919 v = isl_val_neg(v);
3920 c = isl_constraint_set_constant_val(c, v);
3921 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3923 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3924 map_i);
3925 executed = isl_union_map_add_map(executed, map_i);
3928 isl_constraint_free(c);
3929 isl_map_free(map);
3931 if (i < n)
3932 executed = isl_union_map_free(executed);
3934 return executed;
3937 /* Generate code for a single component, after exposing the stride,
3938 * given that the schedule domain is "shifted strided".
3940 * The component inverse schedule is specified as the "map" fields
3941 * of the elements of "domain" indexed by the first "n" elements of "order".
3943 * The schedule domain being "shifted strided" means that the differences
3944 * between the values of the current dimension of domain "i"
3945 * and the values of the current dimension for some reference domain are
3946 * equal to
3948 * stride * integer + offset[i]
3950 * We first look for the domain with the "smallest" value for the current
3951 * dimension and adjust the offsets such that the offset of the "smallest"
3952 * domain is equal to zero. The other offsets are reduced modulo stride.
3954 * Based on this information, we construct a new inverse schedule in
3955 * construct_shifted_executed that exposes the stride.
3956 * Since this involves the introduction of a new schedule dimension,
3957 * the build needs to be changed accordingly.
3958 * After computing the AST, the newly introduced dimension needs
3959 * to be removed again from the list of grafts. We do this by plugging
3960 * in a mapping that represents the new schedule domain in terms of the
3961 * old schedule domain.
3963 static __isl_give isl_ast_graft_list *generate_shift_component(
3964 struct isl_set_map_pair *domain, int *order, int n,
3965 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3966 __isl_take isl_ast_build *build)
3968 isl_ast_graft_list *list;
3969 int first;
3970 int depth;
3971 isl_val *val;
3972 isl_multi_val *mv;
3973 isl_space *space;
3974 isl_multi_aff *ma, *zero;
3975 isl_union_map *executed;
3977 depth = isl_ast_build_get_depth(build);
3979 first = first_offset(domain, order, n, build);
3980 if (first < 0)
3981 goto error;
3983 mv = isl_multi_val_copy(offset);
3984 val = isl_multi_val_get_val(offset, first);
3985 val = isl_val_neg(val);
3986 mv = isl_multi_val_add_val(mv, val);
3987 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3989 executed = construct_shifted_executed(domain, order, n, stride, mv,
3990 build);
3991 space = isl_ast_build_get_space(build, 1);
3992 space = isl_space_map_from_set(space);
3993 ma = isl_multi_aff_identity(isl_space_copy(space));
3994 space = isl_space_from_domain(isl_space_domain(space));
3995 space = isl_space_add_dims(space, isl_dim_out, 1);
3996 zero = isl_multi_aff_zero(space);
3997 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3998 build = isl_ast_build_insert_dim(build, depth + 1);
3999 list = generate_shifted_component(executed, build);
4001 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4003 isl_multi_val_free(mv);
4005 return list;
4006 error:
4007 isl_ast_build_free(build);
4008 return NULL;
4011 /* Does any node in the schedule tree rooted at the current schedule node
4012 * of "build" depend on outer schedule nodes?
4014 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4016 isl_schedule_node *node;
4017 int dependent = 0;
4019 node = isl_ast_build_get_schedule_node(build);
4020 dependent = isl_schedule_node_is_subtree_anchored(node);
4021 isl_schedule_node_free(node);
4023 return dependent;
4026 /* Generate code for a single component.
4028 * The component inverse schedule is specified as the "map" fields
4029 * of the elements of "domain" indexed by the first "n" elements of "order".
4031 * This function may modify the "set" fields of "domain".
4033 * Before proceeding with the actual code generation for the component,
4034 * we first check if there are any "shifted" strides, meaning that
4035 * the schedule domains of the individual domains are all strided,
4036 * but that they have different offsets, resulting in the union
4037 * of schedule domains not being strided anymore.
4039 * The simplest example is the schedule
4041 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4043 * Both schedule domains are strided, but their union is not.
4044 * This function detects such cases and then rewrites the schedule to
4046 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4048 * In the new schedule, the schedule domains have the same offset (modulo
4049 * the stride), ensuring that the union of schedule domains is also strided.
4052 * If there is only a single domain in the component, then there is
4053 * nothing to do. Similarly, if the current schedule dimension has
4054 * a fixed value for almost all domains then there is nothing to be done.
4055 * In particular, we need at least two domains where the current schedule
4056 * dimension does not have a fixed value.
4057 * Finally, in case of a schedule map input,
4058 * if any of the options refer to the current schedule dimension,
4059 * then we bail out as well. It would be possible to reformulate the options
4060 * in terms of the new schedule domain, but that would introduce constraints
4061 * that separate the domains in the options and that is something we would
4062 * like to avoid.
4063 * In the case of a schedule tree input, we bail out if any of
4064 * the descendants of the current schedule node refer to outer
4065 * schedule nodes in any way.
4068 * To see if there is any shifted stride, we look at the differences
4069 * between the values of the current dimension in pairs of domains
4070 * for equal values of outer dimensions. These differences should be
4071 * of the form
4073 * m x + r
4075 * with "m" the stride and "r" a constant. Note that we cannot perform
4076 * this analysis on individual domains as the lower bound in each domain
4077 * may depend on parameters or outer dimensions and so the current dimension
4078 * itself may not have a fixed remainder on division by the stride.
4080 * In particular, we compare the first domain that does not have an
4081 * obviously fixed value for the current dimension to itself and all
4082 * other domains and collect the offsets and the gcd of the strides.
4083 * If the gcd becomes one, then we failed to find shifted strides.
4084 * If the gcd is zero, then the differences were all fixed, meaning
4085 * that some domains had non-obviously fixed values for the current dimension.
4086 * If all the offsets are the same (for those domains that do not have
4087 * an obviously fixed value for the current dimension), then we do not
4088 * apply the transformation.
4089 * If none of the domains were skipped, then there is nothing to do.
4090 * If some of them were skipped, then if we apply separation, the schedule
4091 * domain should get split in pieces with a (non-shifted) stride.
4093 * Otherwise, we apply a shift to expose the stride in
4094 * generate_shift_component.
4096 static __isl_give isl_ast_graft_list *generate_component(
4097 struct isl_set_map_pair *domain, int *order, int n,
4098 __isl_take isl_ast_build *build)
4100 int i, d;
4101 int depth;
4102 isl_ctx *ctx;
4103 isl_map *map;
4104 isl_set *deltas;
4105 isl_val *gcd = NULL;
4106 isl_multi_val *mv;
4107 int fixed, skip;
4108 int base;
4109 isl_ast_graft_list *list;
4110 int res = 0;
4112 depth = isl_ast_build_get_depth(build);
4114 skip = n == 1;
4115 if (skip >= 0 && !skip)
4116 skip = at_most_one_non_fixed(domain, order, n, depth);
4117 if (skip >= 0 && !skip) {
4118 if (isl_ast_build_has_schedule_node(build))
4119 skip = has_anchored_subtree(build);
4120 else
4121 skip = isl_ast_build_options_involve_depth(build);
4123 if (skip < 0)
4124 goto error;
4125 if (skip)
4126 return generate_shifted_component_from_list(domain,
4127 order, n, build);
4129 base = eliminate_non_fixed(domain, order, n, depth, build);
4130 if (base < 0)
4131 goto error;
4133 ctx = isl_ast_build_get_ctx(build);
4135 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4137 fixed = 1;
4138 for (i = 0; i < n; ++i) {
4139 isl_val *r, *m;
4141 map = isl_map_from_domain_and_range(
4142 isl_set_copy(domain[order[base]].set),
4143 isl_set_copy(domain[order[i]].set));
4144 for (d = 0; d < depth; ++d)
4145 map = isl_map_equate(map, isl_dim_in, d,
4146 isl_dim_out, d);
4147 deltas = isl_map_deltas(map);
4148 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4149 isl_set_free(deltas);
4150 if (res < 0)
4151 break;
4153 if (i == 0)
4154 gcd = m;
4155 else
4156 gcd = isl_val_gcd(gcd, m);
4157 if (isl_val_is_one(gcd)) {
4158 isl_val_free(r);
4159 break;
4161 mv = isl_multi_val_set_val(mv, i, r);
4163 res = dim_is_fixed(domain[order[i]].set, depth);
4164 if (res < 0)
4165 break;
4166 if (res)
4167 continue;
4169 if (fixed && i > base) {
4170 isl_val *a, *b;
4171 a = isl_multi_val_get_val(mv, i);
4172 b = isl_multi_val_get_val(mv, base);
4173 if (isl_val_ne(a, b))
4174 fixed = 0;
4175 isl_val_free(a);
4176 isl_val_free(b);
4180 if (res < 0 || !gcd) {
4181 isl_ast_build_free(build);
4182 list = NULL;
4183 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4184 list = generate_shifted_component_from_list(domain,
4185 order, n, build);
4186 } else {
4187 list = generate_shift_component(domain, order, n, gcd, mv,
4188 build);
4191 isl_val_free(gcd);
4192 isl_multi_val_free(mv);
4194 return list;
4195 error:
4196 isl_ast_build_free(build);
4197 return NULL;
4200 /* Store both "map" itself and its domain in the
4201 * structure pointed to by *next and advance to the next array element.
4203 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4205 struct isl_set_map_pair **next = user;
4207 (*next)->map = isl_map_copy(map);
4208 (*next)->set = isl_map_domain(map);
4209 (*next)++;
4211 return isl_stat_ok;
4214 static int after_in_tree(__isl_keep isl_union_map *umap,
4215 __isl_keep isl_schedule_node *node);
4217 /* Is any domain element of "umap" scheduled after any of
4218 * the corresponding image elements by the tree rooted at
4219 * the child of "node"?
4221 static int after_in_child(__isl_keep isl_union_map *umap,
4222 __isl_keep isl_schedule_node *node)
4224 isl_schedule_node *child;
4225 int after;
4227 child = isl_schedule_node_get_child(node, 0);
4228 after = after_in_tree(umap, child);
4229 isl_schedule_node_free(child);
4231 return after;
4234 /* Is any domain element of "umap" scheduled after any of
4235 * the corresponding image elements by the tree rooted at
4236 * the band node "node"?
4238 * We first check if any domain element is scheduled after any
4239 * of the corresponding image elements by the band node itself.
4240 * If not, we restrict "map" to those pairs of element that
4241 * are scheduled together by the band node and continue with
4242 * the child of the band node.
4243 * If there are no such pairs then the map passed to after_in_child
4244 * will be empty causing it to return 0.
4246 static int after_in_band(__isl_keep isl_union_map *umap,
4247 __isl_keep isl_schedule_node *node)
4249 isl_multi_union_pw_aff *mupa;
4250 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4251 isl_union_set *domain, *range;
4252 isl_space *space;
4253 int empty;
4254 int after;
4256 if (isl_schedule_node_band_n_member(node) == 0)
4257 return after_in_child(umap, node);
4259 mupa = isl_schedule_node_band_get_partial_schedule(node);
4260 space = isl_multi_union_pw_aff_get_space(mupa);
4261 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4262 test = isl_union_map_copy(umap);
4263 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4264 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4265 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4266 test = isl_union_map_intersect(test, gt);
4267 empty = isl_union_map_is_empty(test);
4268 isl_union_map_free(test);
4270 if (empty < 0 || !empty) {
4271 isl_union_map_free(partial);
4272 return empty < 0 ? -1 : 1;
4275 universe = isl_union_map_universe(isl_union_map_copy(umap));
4276 domain = isl_union_map_domain(isl_union_map_copy(universe));
4277 range = isl_union_map_range(universe);
4278 umap1 = isl_union_map_copy(partial);
4279 umap1 = isl_union_map_intersect_domain(umap1, domain);
4280 umap2 = isl_union_map_intersect_domain(partial, range);
4281 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4282 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4283 after = after_in_child(test, node);
4284 isl_union_map_free(test);
4285 return after;
4288 /* Is any domain element of "umap" scheduled after any of
4289 * the corresponding image elements by the tree rooted at
4290 * the context node "node"?
4292 * The context constraints apply to the schedule domain,
4293 * so we cannot apply them directly to "umap", which contains
4294 * pairs of statement instances. Instead, we add them
4295 * to the range of the prefix schedule for both domain and
4296 * range of "umap".
4298 static int after_in_context(__isl_keep isl_union_map *umap,
4299 __isl_keep isl_schedule_node *node)
4301 isl_union_map *prefix, *universe, *umap1, *umap2;
4302 isl_union_set *domain, *range;
4303 isl_set *context;
4304 int after;
4306 umap = isl_union_map_copy(umap);
4307 context = isl_schedule_node_context_get_context(node);
4308 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4309 universe = isl_union_map_universe(isl_union_map_copy(umap));
4310 domain = isl_union_map_domain(isl_union_map_copy(universe));
4311 range = isl_union_map_range(universe);
4312 umap1 = isl_union_map_copy(prefix);
4313 umap1 = isl_union_map_intersect_domain(umap1, domain);
4314 umap2 = isl_union_map_intersect_domain(prefix, range);
4315 umap1 = isl_union_map_intersect_range(umap1,
4316 isl_union_set_from_set(context));
4317 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4318 umap = isl_union_map_intersect(umap, umap1);
4320 after = after_in_child(umap, node);
4322 isl_union_map_free(umap);
4324 return after;
4327 /* Is any domain element of "umap" scheduled after any of
4328 * the corresponding image elements by the tree rooted at
4329 * the expansion node "node"?
4331 * We apply the expansion to domain and range of "umap" and
4332 * continue with its child.
4334 static int after_in_expansion(__isl_keep isl_union_map *umap,
4335 __isl_keep isl_schedule_node *node)
4337 isl_union_map *expansion;
4338 int after;
4340 expansion = isl_schedule_node_expansion_get_expansion(node);
4341 umap = isl_union_map_copy(umap);
4342 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4343 umap = isl_union_map_apply_range(umap, expansion);
4345 after = after_in_child(umap, node);
4347 isl_union_map_free(umap);
4349 return after;
4352 /* Is any domain element of "umap" scheduled after any of
4353 * the corresponding image elements by the tree rooted at
4354 * the extension node "node"?
4356 * Since the extension node may add statement instances before or
4357 * after the pairs of statement instances in "umap", we return 1
4358 * to ensure that these pairs are not broken up.
4360 static int after_in_extension(__isl_keep isl_union_map *umap,
4361 __isl_keep isl_schedule_node *node)
4363 return 1;
4366 /* Is any domain element of "umap" scheduled after any of
4367 * the corresponding image elements by the tree rooted at
4368 * the filter node "node"?
4370 * We intersect domain and range of "umap" with the filter and
4371 * continue with its child.
4373 static int after_in_filter(__isl_keep isl_union_map *umap,
4374 __isl_keep isl_schedule_node *node)
4376 isl_union_set *filter;
4377 int after;
4379 umap = isl_union_map_copy(umap);
4380 filter = isl_schedule_node_filter_get_filter(node);
4381 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4382 umap = isl_union_map_intersect_range(umap, filter);
4384 after = after_in_child(umap, node);
4386 isl_union_map_free(umap);
4388 return after;
4391 /* Is any domain element of "umap" scheduled after any of
4392 * the corresponding image elements by the tree rooted at
4393 * the set node "node"?
4395 * This is only the case if this condition holds in any
4396 * of the (filter) children of the set node.
4397 * In particular, if the domain and the range of "umap"
4398 * are contained in different children, then the condition
4399 * does not hold.
4401 static int after_in_set(__isl_keep isl_union_map *umap,
4402 __isl_keep isl_schedule_node *node)
4404 int i, n;
4406 n = isl_schedule_node_n_children(node);
4407 for (i = 0; i < n; ++i) {
4408 isl_schedule_node *child;
4409 int after;
4411 child = isl_schedule_node_get_child(node, i);
4412 after = after_in_tree(umap, child);
4413 isl_schedule_node_free(child);
4415 if (after < 0 || after)
4416 return after;
4419 return 0;
4422 /* Return the filter of child "i" of "node".
4424 static __isl_give isl_union_set *child_filter(
4425 __isl_keep isl_schedule_node *node, int i)
4427 isl_schedule_node *child;
4428 isl_union_set *filter;
4430 child = isl_schedule_node_get_child(node, i);
4431 filter = isl_schedule_node_filter_get_filter(child);
4432 isl_schedule_node_free(child);
4434 return filter;
4437 /* Is any domain element of "umap" scheduled after any of
4438 * the corresponding image elements by the tree rooted at
4439 * the sequence node "node"?
4441 * This happens in particular if any domain element is
4442 * contained in a later child than one containing a range element or
4443 * if the condition holds within a given child in the sequence.
4444 * The later part of the condition is checked by after_in_set.
4446 static int after_in_sequence(__isl_keep isl_union_map *umap,
4447 __isl_keep isl_schedule_node *node)
4449 int i, j, n;
4450 isl_union_map *umap_i;
4451 int empty, after = 0;
4453 n = isl_schedule_node_n_children(node);
4454 for (i = 1; i < n; ++i) {
4455 isl_union_set *filter_i;
4457 umap_i = isl_union_map_copy(umap);
4458 filter_i = child_filter(node, i);
4459 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4460 empty = isl_union_map_is_empty(umap_i);
4461 if (empty < 0)
4462 goto error;
4463 if (empty) {
4464 isl_union_map_free(umap_i);
4465 continue;
4468 for (j = 0; j < i; ++j) {
4469 isl_union_set *filter_j;
4470 isl_union_map *umap_ij;
4472 umap_ij = isl_union_map_copy(umap_i);
4473 filter_j = child_filter(node, j);
4474 umap_ij = isl_union_map_intersect_range(umap_ij,
4475 filter_j);
4476 empty = isl_union_map_is_empty(umap_ij);
4477 isl_union_map_free(umap_ij);
4479 if (empty < 0)
4480 goto error;
4481 if (!empty)
4482 after = 1;
4483 if (after)
4484 break;
4487 isl_union_map_free(umap_i);
4488 if (after)
4489 break;
4492 if (after < 0 || after)
4493 return after;
4495 return after_in_set(umap, node);
4496 error:
4497 isl_union_map_free(umap_i);
4498 return -1;
4501 /* Is any domain element of "umap" scheduled after any of
4502 * the corresponding image elements by the tree rooted at "node"?
4504 * If "umap" is empty, then clearly there is no such element.
4505 * Otherwise, consider the different types of nodes separately.
4507 static int after_in_tree(__isl_keep isl_union_map *umap,
4508 __isl_keep isl_schedule_node *node)
4510 int empty;
4511 enum isl_schedule_node_type type;
4513 empty = isl_union_map_is_empty(umap);
4514 if (empty < 0)
4515 return -1;
4516 if (empty)
4517 return 0;
4518 if (!node)
4519 return -1;
4521 type = isl_schedule_node_get_type(node);
4522 switch (type) {
4523 case isl_schedule_node_error:
4524 return -1;
4525 case isl_schedule_node_leaf:
4526 return 0;
4527 case isl_schedule_node_band:
4528 return after_in_band(umap, node);
4529 case isl_schedule_node_domain:
4530 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4531 "unexpected internal domain node", return -1);
4532 case isl_schedule_node_context:
4533 return after_in_context(umap, node);
4534 case isl_schedule_node_expansion:
4535 return after_in_expansion(umap, node);
4536 case isl_schedule_node_extension:
4537 return after_in_extension(umap, node);
4538 case isl_schedule_node_filter:
4539 return after_in_filter(umap, node);
4540 case isl_schedule_node_guard:
4541 case isl_schedule_node_mark:
4542 return after_in_child(umap, node);
4543 case isl_schedule_node_set:
4544 return after_in_set(umap, node);
4545 case isl_schedule_node_sequence:
4546 return after_in_sequence(umap, node);
4549 return 1;
4552 /* Is any domain element of "map1" scheduled after any domain
4553 * element of "map2" by the subtree underneath the current band node,
4554 * while at the same time being scheduled together by the current
4555 * band node, i.e., by "map1" and "map2?
4557 * If the child of the current band node is a leaf, then
4558 * no element can be scheduled after any other element.
4560 * Otherwise, we construct a relation between domain elements
4561 * of "map1" and domain elements of "map2" that are scheduled
4562 * together and then check if the subtree underneath the current
4563 * band node determines their relative order.
4565 static int after_in_subtree(__isl_keep isl_ast_build *build,
4566 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4568 isl_schedule_node *node;
4569 isl_map *map;
4570 isl_union_map *umap;
4571 int after;
4573 node = isl_ast_build_get_schedule_node(build);
4574 if (!node)
4575 return -1;
4576 node = isl_schedule_node_child(node, 0);
4577 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4578 isl_schedule_node_free(node);
4579 return 0;
4581 map = isl_map_copy(map2);
4582 map = isl_map_apply_domain(map, isl_map_copy(map1));
4583 umap = isl_union_map_from_map(map);
4584 after = after_in_tree(umap, node);
4585 isl_union_map_free(umap);
4586 isl_schedule_node_free(node);
4587 return after;
4590 /* Internal data for any_scheduled_after.
4592 * "build" is the build in which the AST is constructed.
4593 * "depth" is the number of loops that have already been generated
4594 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4595 * "domain" is an array of set-map pairs corresponding to the different
4596 * iteration domains. The set is the schedule domain, i.e., the domain
4597 * of the inverse schedule, while the map is the inverse schedule itself.
4599 struct isl_any_scheduled_after_data {
4600 isl_ast_build *build;
4601 int depth;
4602 int group_coscheduled;
4603 struct isl_set_map_pair *domain;
4606 /* Is any element of domain "i" scheduled after any element of domain "j"
4607 * (for a common iteration of the first data->depth loops)?
4609 * data->domain[i].set contains the domain of the inverse schedule
4610 * for domain "i", i.e., elements in the schedule domain.
4612 * If we are inside a band of a schedule tree and there is a pair
4613 * of elements in the two domains that is schedule together by
4614 * the current band, then we check if any element of "i" may be schedule
4615 * after element of "j" by the descendants of the band node.
4617 * If data->group_coscheduled is set, then we also return 1 if there
4618 * is any pair of elements in the two domains that are scheduled together.
4620 static isl_bool any_scheduled_after(int i, int j, void *user)
4622 struct isl_any_scheduled_after_data *data = user;
4623 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4624 int pos;
4626 for (pos = data->depth; pos < dim; ++pos) {
4627 int follows;
4629 follows = isl_set_follows_at(data->domain[i].set,
4630 data->domain[j].set, pos);
4632 if (follows < -1)
4633 return isl_bool_error;
4634 if (follows > 0)
4635 return isl_bool_true;
4636 if (follows < 0)
4637 return isl_bool_false;
4640 if (isl_ast_build_has_schedule_node(data->build)) {
4641 int after;
4643 after = after_in_subtree(data->build, data->domain[i].map,
4644 data->domain[j].map);
4645 if (after < 0 || after)
4646 return after;
4649 return data->group_coscheduled;
4652 /* Look for independent components at the current depth and generate code
4653 * for each component separately. The resulting lists of grafts are
4654 * merged in an attempt to combine grafts with identical guards.
4656 * Code for two domains can be generated separately if all the elements
4657 * of one domain are scheduled before (or together with) all the elements
4658 * of the other domain. We therefore consider the graph with as nodes
4659 * the domains and an edge between two nodes if any element of the first
4660 * node is scheduled after any element of the second node.
4661 * If the ast_build_group_coscheduled is set, then we also add an edge if
4662 * there is any pair of elements in the two domains that are scheduled
4663 * together.
4664 * Code is then generated (by generate_component)
4665 * for each of the strongly connected components in this graph
4666 * in their topological order.
4668 * Since the test is performed on the domain of the inverse schedules of
4669 * the different domains, we precompute these domains and store
4670 * them in data.domain.
4672 static __isl_give isl_ast_graft_list *generate_components(
4673 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4675 int i;
4676 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4677 int n = isl_union_map_n_map(executed);
4678 struct isl_any_scheduled_after_data data;
4679 struct isl_set_map_pair *next;
4680 struct isl_tarjan_graph *g = NULL;
4681 isl_ast_graft_list *list = NULL;
4682 int n_domain = 0;
4684 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4685 if (!data.domain)
4686 goto error;
4687 n_domain = n;
4689 next = data.domain;
4690 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4691 goto error;
4693 if (!build)
4694 goto error;
4695 data.build = build;
4696 data.depth = isl_ast_build_get_depth(build);
4697 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4698 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4699 if (!g)
4700 goto error;
4702 list = isl_ast_graft_list_alloc(ctx, 0);
4704 i = 0;
4705 while (list && n) {
4706 isl_ast_graft_list *list_c;
4707 int first = i;
4709 if (g->order[i] == -1)
4710 isl_die(ctx, isl_error_internal, "cannot happen",
4711 goto error);
4712 ++i; --n;
4713 while (g->order[i] != -1) {
4714 ++i; --n;
4717 list_c = generate_component(data.domain,
4718 g->order + first, i - first,
4719 isl_ast_build_copy(build));
4720 list = isl_ast_graft_list_merge(list, list_c, build);
4722 ++i;
4725 if (0)
4726 error: list = isl_ast_graft_list_free(list);
4727 isl_tarjan_graph_free(g);
4728 for (i = 0; i < n_domain; ++i) {
4729 isl_map_free(data.domain[i].map);
4730 isl_set_free(data.domain[i].set);
4732 free(data.domain);
4733 isl_union_map_free(executed);
4734 isl_ast_build_free(build);
4736 return list;
4739 /* Generate code for the next level (and all inner levels).
4741 * If "executed" is empty, i.e., no code needs to be generated,
4742 * then we return an empty list.
4744 * If we have already generated code for all loop levels, then we pass
4745 * control to generate_inner_level.
4747 * If "executed" lives in a single space, i.e., if code needs to be
4748 * generated for a single domain, then there can only be a single
4749 * component and we go directly to generate_shifted_component.
4750 * Otherwise, we call generate_components to detect the components
4751 * and to call generate_component on each of them separately.
4753 static __isl_give isl_ast_graft_list *generate_next_level(
4754 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4756 int depth;
4758 if (!build || !executed)
4759 goto error;
4761 if (isl_union_map_is_empty(executed)) {
4762 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4763 isl_union_map_free(executed);
4764 isl_ast_build_free(build);
4765 return isl_ast_graft_list_alloc(ctx, 0);
4768 depth = isl_ast_build_get_depth(build);
4769 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4770 return generate_inner_level(executed, build);
4772 if (isl_union_map_n_map(executed) == 1)
4773 return generate_shifted_component(executed, build);
4775 return generate_components(executed, build);
4776 error:
4777 isl_union_map_free(executed);
4778 isl_ast_build_free(build);
4779 return NULL;
4782 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4783 * internal, executed and build are the inputs to generate_code.
4784 * list collects the output.
4786 struct isl_generate_code_data {
4787 int internal;
4788 isl_union_map *executed;
4789 isl_ast_build *build;
4791 isl_ast_graft_list *list;
4794 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4796 * [E -> S] -> D
4798 * with E the external build schedule and S the additional schedule "space",
4799 * reformulate the inverse schedule in terms of the internal schedule domain,
4800 * i.e., return
4802 * [I -> S] -> D
4804 * We first obtain a mapping
4806 * I -> E
4808 * take the inverse and the product with S -> S, resulting in
4810 * [I -> S] -> [E -> S]
4812 * Applying the map to the input produces the desired result.
4814 static __isl_give isl_union_map *internal_executed(
4815 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4816 __isl_keep isl_ast_build *build)
4818 isl_map *id, *proj;
4820 proj = isl_ast_build_get_schedule_map(build);
4821 proj = isl_map_reverse(proj);
4822 space = isl_space_map_from_set(isl_space_copy(space));
4823 id = isl_map_identity(space);
4824 proj = isl_map_product(proj, id);
4825 executed = isl_union_map_apply_domain(executed,
4826 isl_union_map_from_map(proj));
4827 return executed;
4830 /* Generate an AST that visits the elements in the range of data->executed
4831 * in the relative order specified by the corresponding domain element(s)
4832 * for those domain elements that belong to "set".
4833 * Add the result to data->list.
4835 * The caller ensures that "set" is a universe domain.
4836 * "space" is the space of the additional part of the schedule.
4837 * It is equal to the space of "set" if build->domain is parametric.
4838 * Otherwise, it is equal to the range of the wrapped space of "set".
4840 * If the build space is not parametric and
4841 * if isl_ast_build_node_from_schedule_map
4842 * was called from an outside user (data->internal not set), then
4843 * the (inverse) schedule refers to the external build domain and needs to
4844 * be transformed to refer to the internal build domain.
4846 * If the build space is parametric, then we add some of the parameter
4847 * constraints to the executed relation. Adding these constraints
4848 * allows for an earlier detection of conflicts in some cases.
4849 * However, we do not want to divide the executed relation into
4850 * more disjuncts than necessary. We therefore approximate
4851 * the constraints on the parameters by a single disjunct set.
4853 * The build is extended to include the additional part of the schedule.
4854 * If the original build space was not parametric, then the options
4855 * in data->build refer only to the additional part of the schedule
4856 * and they need to be adjusted to refer to the complete AST build
4857 * domain.
4859 * After having adjusted inverse schedule and build, we start generating
4860 * code with the outer loop of the current code generation
4861 * in generate_next_level.
4863 * If the original build space was not parametric, we undo the embedding
4864 * on the resulting isl_ast_node_list so that it can be used within
4865 * the outer AST build.
4867 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4868 __isl_take isl_set *set, __isl_take isl_space *space)
4870 isl_union_map *executed;
4871 isl_ast_build *build;
4872 isl_ast_graft_list *list;
4873 int embed;
4875 executed = isl_union_map_copy(data->executed);
4876 executed = isl_union_map_intersect_domain(executed,
4877 isl_union_set_from_set(set));
4879 embed = !isl_set_is_params(data->build->domain);
4880 if (embed && !data->internal)
4881 executed = internal_executed(executed, space, data->build);
4882 if (!embed) {
4883 isl_set *domain;
4884 domain = isl_ast_build_get_domain(data->build);
4885 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4886 executed = isl_union_map_intersect_params(executed, domain);
4889 build = isl_ast_build_copy(data->build);
4890 build = isl_ast_build_product(build, space);
4892 list = generate_next_level(executed, build);
4894 list = isl_ast_graft_list_unembed(list, embed);
4896 data->list = isl_ast_graft_list_concat(data->list, list);
4898 return isl_stat_ok;
4901 /* Generate an AST that visits the elements in the range of data->executed
4902 * in the relative order specified by the corresponding domain element(s)
4903 * for those domain elements that belong to "set".
4904 * Add the result to data->list.
4906 * The caller ensures that "set" is a universe domain.
4908 * If the build space S is not parametric, then the space of "set"
4909 * need to be a wrapped relation with S as domain. That is, it needs
4910 * to be of the form
4912 * [S -> T]
4914 * Check this property and pass control to generate_code_in_space
4915 * passing along T.
4916 * If the build space is not parametric, then T is the space of "set".
4918 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4920 struct isl_generate_code_data *data = user;
4921 isl_space *space, *build_space;
4922 int is_domain;
4924 space = isl_set_get_space(set);
4926 if (isl_set_is_params(data->build->domain))
4927 return generate_code_in_space(data, set, space);
4929 build_space = isl_ast_build_get_space(data->build, data->internal);
4930 space = isl_space_unwrap(space);
4931 is_domain = isl_space_is_domain(build_space, space);
4932 isl_space_free(build_space);
4933 space = isl_space_range(space);
4935 if (is_domain < 0)
4936 goto error;
4937 if (!is_domain)
4938 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4939 "invalid nested schedule space", goto error);
4941 return generate_code_in_space(data, set, space);
4942 error:
4943 isl_set_free(set);
4944 isl_space_free(space);
4945 return isl_stat_error;
4948 /* Generate an AST that visits the elements in the range of "executed"
4949 * in the relative order specified by the corresponding domain element(s).
4951 * "build" is an isl_ast_build that has either been constructed by
4952 * isl_ast_build_from_context or passed to a callback set by
4953 * isl_ast_build_set_create_leaf.
4954 * In the first case, the space of the isl_ast_build is typically
4955 * a parametric space, although this is currently not enforced.
4956 * In the second case, the space is never a parametric space.
4957 * If the space S is not parametric, then the domain space(s) of "executed"
4958 * need to be wrapped relations with S as domain.
4960 * If the domain of "executed" consists of several spaces, then an AST
4961 * is generated for each of them (in arbitrary order) and the results
4962 * are concatenated.
4964 * If "internal" is set, then the domain "S" above refers to the internal
4965 * schedule domain representation. Otherwise, it refers to the external
4966 * representation, as returned by isl_ast_build_get_schedule_space.
4968 * We essentially run over all the spaces in the domain of "executed"
4969 * and call generate_code_set on each of them.
4971 static __isl_give isl_ast_graft_list *generate_code(
4972 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4973 int internal)
4975 isl_ctx *ctx;
4976 struct isl_generate_code_data data = { 0 };
4977 isl_space *space;
4978 isl_union_set *schedule_domain;
4979 isl_union_map *universe;
4981 if (!build)
4982 goto error;
4983 space = isl_ast_build_get_space(build, 1);
4984 space = isl_space_align_params(space,
4985 isl_union_map_get_space(executed));
4986 space = isl_space_align_params(space,
4987 isl_union_map_get_space(build->options));
4988 build = isl_ast_build_align_params(build, isl_space_copy(space));
4989 executed = isl_union_map_align_params(executed, space);
4990 if (!executed || !build)
4991 goto error;
4993 ctx = isl_ast_build_get_ctx(build);
4995 data.internal = internal;
4996 data.executed = executed;
4997 data.build = build;
4998 data.list = isl_ast_graft_list_alloc(ctx, 0);
5000 universe = isl_union_map_universe(isl_union_map_copy(executed));
5001 schedule_domain = isl_union_map_domain(universe);
5002 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5003 &data) < 0)
5004 data.list = isl_ast_graft_list_free(data.list);
5006 isl_union_set_free(schedule_domain);
5007 isl_union_map_free(executed);
5009 isl_ast_build_free(build);
5010 return data.list;
5011 error:
5012 isl_union_map_free(executed);
5013 isl_ast_build_free(build);
5014 return NULL;
5017 /* Generate an AST that visits the elements in the domain of "schedule"
5018 * in the relative order specified by the corresponding image element(s).
5020 * "build" is an isl_ast_build that has either been constructed by
5021 * isl_ast_build_from_context or passed to a callback set by
5022 * isl_ast_build_set_create_leaf.
5023 * In the first case, the space of the isl_ast_build is typically
5024 * a parametric space, although this is currently not enforced.
5025 * In the second case, the space is never a parametric space.
5026 * If the space S is not parametric, then the range space(s) of "schedule"
5027 * need to be wrapped relations with S as domain.
5029 * If the range of "schedule" consists of several spaces, then an AST
5030 * is generated for each of them (in arbitrary order) and the results
5031 * are concatenated.
5033 * We first initialize the local copies of the relevant options.
5034 * We do this here rather than when the isl_ast_build is created
5035 * because the options may have changed between the construction
5036 * of the isl_ast_build and the call to isl_generate_code.
5038 * The main computation is performed on an inverse schedule (with
5039 * the schedule domain in the domain and the elements to be executed
5040 * in the range) called "executed".
5042 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5043 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5045 isl_ast_graft_list *list;
5046 isl_ast_node *node;
5047 isl_union_map *executed;
5049 build = isl_ast_build_copy(build);
5050 build = isl_ast_build_set_single_valued(build, 0);
5051 schedule = isl_union_map_coalesce(schedule);
5052 schedule = isl_union_map_remove_redundancies(schedule);
5053 executed = isl_union_map_reverse(schedule);
5054 list = generate_code(executed, isl_ast_build_copy(build), 0);
5055 node = isl_ast_node_from_graft_list(list, build);
5056 isl_ast_build_free(build);
5058 return node;
5061 /* The old name for isl_ast_build_node_from_schedule_map.
5062 * It is being kept for backward compatibility, but
5063 * it will be removed in the future.
5065 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5066 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5068 return isl_ast_build_node_from_schedule_map(build, schedule);
5071 /* Generate an AST that visits the elements in the domain of "executed"
5072 * in the relative order specified by the band node "node" and its descendants.
5074 * The relation "executed" maps the outer generated loop iterators
5075 * to the domain elements executed by those iterations.
5077 * If the band is empty, we continue with its descendants.
5078 * Otherwise, we extend the build and the inverse schedule with
5079 * the additional space/partial schedule and continue generating
5080 * an AST in generate_next_level.
5081 * As soon as we have extended the inverse schedule with the additional
5082 * partial schedule, we look for equalities that may exists between
5083 * the old and the new part.
5085 static __isl_give isl_ast_graft_list *build_ast_from_band(
5086 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5087 __isl_take isl_union_map *executed)
5089 isl_space *space;
5090 isl_multi_union_pw_aff *extra;
5091 isl_union_map *extra_umap;
5092 isl_ast_graft_list *list;
5093 unsigned n1, n2;
5095 if (!build || !node || !executed)
5096 goto error;
5098 if (isl_schedule_node_band_n_member(node) == 0)
5099 return build_ast_from_child(build, node, executed);
5101 extra = isl_schedule_node_band_get_partial_schedule(node);
5102 extra = isl_multi_union_pw_aff_align_params(extra,
5103 isl_ast_build_get_space(build, 1));
5104 space = isl_multi_union_pw_aff_get_space(extra);
5106 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5107 extra_umap = isl_union_map_reverse(extra_umap);
5109 executed = isl_union_map_domain_product(executed, extra_umap);
5110 executed = isl_union_map_detect_equalities(executed);
5112 n1 = isl_ast_build_dim(build, isl_dim_param);
5113 build = isl_ast_build_product(build, space);
5114 n2 = isl_ast_build_dim(build, isl_dim_param);
5115 if (n2 > n1)
5116 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5117 "band node is not allowed to introduce new parameters",
5118 build = isl_ast_build_free(build));
5119 build = isl_ast_build_set_schedule_node(build, node);
5121 list = generate_next_level(executed, build);
5123 list = isl_ast_graft_list_unembed(list, 1);
5125 return list;
5126 error:
5127 isl_schedule_node_free(node);
5128 isl_union_map_free(executed);
5129 isl_ast_build_free(build);
5130 return NULL;
5133 /* Hoist a list of grafts (in practice containing a single graft)
5134 * from "sub_build" (which includes extra context information)
5135 * to "build".
5137 * In particular, project out all additional parameters introduced
5138 * by the context node from the enforced constraints and the guard
5139 * of the single graft.
5141 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5142 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5143 __isl_keep isl_ast_build *sub_build)
5145 isl_ast_graft *graft;
5146 isl_basic_set *enforced;
5147 isl_set *guard;
5148 unsigned n_param, extra_param;
5150 if (!build || !sub_build)
5151 return isl_ast_graft_list_free(list);
5153 n_param = isl_ast_build_dim(build, isl_dim_param);
5154 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5156 if (extra_param == n_param)
5157 return list;
5159 extra_param -= n_param;
5160 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5161 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5162 n_param, extra_param);
5163 enforced = isl_basic_set_remove_unknown_divs(enforced);
5164 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5165 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5166 n_param, extra_param);
5167 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5168 guard = isl_set_compute_divs(guard);
5169 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5170 build, sub_build);
5171 list = isl_ast_graft_list_from_ast_graft(graft);
5173 return list;
5176 /* Generate an AST that visits the elements in the domain of "executed"
5177 * in the relative order specified by the context node "node"
5178 * and its descendants.
5180 * The relation "executed" maps the outer generated loop iterators
5181 * to the domain elements executed by those iterations.
5183 * The context node may introduce additional parameters as well as
5184 * constraints on the outer schedule dimensions or original parameters.
5186 * We add the extra parameters to a new build and the context
5187 * constraints to both the build and (as a single disjunct)
5188 * to the domain of "executed". Since the context constraints
5189 * are specified in terms of the input schedule, we first need
5190 * to map them to the internal schedule domain.
5192 * After constructing the AST from the descendants of "node",
5193 * we combine the list of grafts into a single graft within
5194 * the new build, in order to be able to exploit the additional
5195 * context constraints during this combination.
5197 * Additionally, if the current node is the outermost node in
5198 * the schedule tree (apart from the root domain node), we generate
5199 * all pending guards, again to be able to exploit the additional
5200 * context constraints. We currently do not do this for internal
5201 * context nodes since we may still want to hoist conditions
5202 * to outer AST nodes.
5204 * If the context node introduced any new parameters, then they
5205 * are removed from the set of enforced constraints and guard
5206 * in hoist_out_of_context.
5208 static __isl_give isl_ast_graft_list *build_ast_from_context(
5209 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5210 __isl_take isl_union_map *executed)
5212 isl_set *context;
5213 isl_space *space;
5214 isl_multi_aff *internal2input;
5215 isl_ast_build *sub_build;
5216 isl_ast_graft_list *list;
5217 int n, depth;
5219 depth = isl_schedule_node_get_tree_depth(node);
5220 space = isl_ast_build_get_space(build, 1);
5221 context = isl_schedule_node_context_get_context(node);
5222 context = isl_set_align_params(context, space);
5223 sub_build = isl_ast_build_copy(build);
5224 space = isl_set_get_space(context);
5225 sub_build = isl_ast_build_align_params(sub_build, space);
5226 internal2input = isl_ast_build_get_internal2input(sub_build);
5227 context = isl_set_preimage_multi_aff(context, internal2input);
5228 sub_build = isl_ast_build_restrict_generated(sub_build,
5229 isl_set_copy(context));
5230 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5231 executed = isl_union_map_intersect_domain(executed,
5232 isl_union_set_from_set(context));
5234 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5235 node, executed);
5236 n = isl_ast_graft_list_n_ast_graft(list);
5237 if (n < 0)
5238 list = isl_ast_graft_list_free(list);
5240 list = isl_ast_graft_list_fuse(list, sub_build);
5241 if (depth == 1)
5242 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5243 sub_build);
5244 if (n >= 1)
5245 list = hoist_out_of_context(list, build, sub_build);
5247 isl_ast_build_free(build);
5248 isl_ast_build_free(sub_build);
5250 return list;
5253 /* Generate an AST that visits the elements in the domain of "executed"
5254 * in the relative order specified by the expansion node "node" and
5255 * its descendants.
5257 * The relation "executed" maps the outer generated loop iterators
5258 * to the domain elements executed by those iterations.
5260 * We expand the domain elements by the expansion and
5261 * continue with the descendants of the node.
5263 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5264 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5265 __isl_take isl_union_map *executed)
5267 isl_union_map *expansion;
5268 unsigned n1, n2;
5270 expansion = isl_schedule_node_expansion_get_expansion(node);
5271 expansion = isl_union_map_align_params(expansion,
5272 isl_union_map_get_space(executed));
5274 n1 = isl_union_map_dim(executed, isl_dim_param);
5275 executed = isl_union_map_apply_range(executed, expansion);
5276 n2 = isl_union_map_dim(executed, isl_dim_param);
5277 if (n2 > n1)
5278 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5279 "expansion node is not allowed to introduce "
5280 "new parameters", goto error);
5282 return build_ast_from_child(build, node, executed);
5283 error:
5284 isl_ast_build_free(build);
5285 isl_schedule_node_free(node);
5286 isl_union_map_free(executed);
5287 return NULL;
5290 /* Generate an AST that visits the elements in the domain of "executed"
5291 * in the relative order specified by the extension node "node" and
5292 * its descendants.
5294 * The relation "executed" maps the outer generated loop iterators
5295 * to the domain elements executed by those iterations.
5297 * Extend the inverse schedule with the extension applied to current
5298 * set of generated constraints. Since the extension if formulated
5299 * in terms of the input schedule, it first needs to be transformed
5300 * to refer to the internal schedule.
5302 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5303 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5304 __isl_take isl_union_map *executed)
5306 isl_union_set *schedule_domain;
5307 isl_union_map *extension;
5308 isl_set *set;
5310 set = isl_ast_build_get_generated(build);
5311 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5312 schedule_domain = isl_union_set_from_set(set);
5314 extension = isl_schedule_node_extension_get_extension(node);
5316 extension = isl_union_map_preimage_domain_multi_aff(extension,
5317 isl_multi_aff_copy(build->internal2input));
5318 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5319 extension = isl_ast_build_substitute_values_union_map_domain(build,
5320 extension);
5321 executed = isl_union_map_union(executed, extension);
5323 return build_ast_from_child(build, node, executed);
5326 /* Generate an AST that visits the elements in the domain of "executed"
5327 * in the relative order specified by the filter node "node" and
5328 * its descendants.
5330 * The relation "executed" maps the outer generated loop iterators
5331 * to the domain elements executed by those iterations.
5333 * We simply intersect the iteration domain (i.e., the range of "executed")
5334 * with the filter and continue with the descendants of the node,
5335 * unless the resulting inverse schedule is empty, in which
5336 * case we return an empty list.
5338 * If the result of the intersection is equal to the original "executed"
5339 * relation, then keep the original representation since the intersection
5340 * may have unnecessarily broken up the relation into a greater number
5341 * of disjuncts.
5343 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5344 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5345 __isl_take isl_union_map *executed)
5347 isl_ctx *ctx;
5348 isl_union_set *filter;
5349 isl_union_map *orig;
5350 isl_ast_graft_list *list;
5351 int empty;
5352 isl_bool unchanged;
5353 unsigned n1, n2;
5355 orig = isl_union_map_copy(executed);
5356 if (!build || !node || !executed)
5357 goto error;
5359 filter = isl_schedule_node_filter_get_filter(node);
5360 filter = isl_union_set_align_params(filter,
5361 isl_union_map_get_space(executed));
5362 n1 = isl_union_map_dim(executed, isl_dim_param);
5363 executed = isl_union_map_intersect_range(executed, filter);
5364 n2 = isl_union_map_dim(executed, isl_dim_param);
5365 if (n2 > n1)
5366 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5367 "filter node is not allowed to introduce "
5368 "new parameters", goto error);
5370 unchanged = isl_union_map_is_subset(orig, executed);
5371 empty = isl_union_map_is_empty(executed);
5372 if (unchanged < 0 || empty < 0)
5373 goto error;
5374 if (unchanged) {
5375 isl_union_map_free(executed);
5376 return build_ast_from_child(build, node, orig);
5378 isl_union_map_free(orig);
5379 if (!empty)
5380 return build_ast_from_child(build, node, executed);
5382 ctx = isl_ast_build_get_ctx(build);
5383 list = isl_ast_graft_list_alloc(ctx, 0);
5384 isl_ast_build_free(build);
5385 isl_schedule_node_free(node);
5386 isl_union_map_free(executed);
5387 return list;
5388 error:
5389 isl_ast_build_free(build);
5390 isl_schedule_node_free(node);
5391 isl_union_map_free(executed);
5392 isl_union_map_free(orig);
5393 return NULL;
5396 /* Generate an AST that visits the elements in the domain of "executed"
5397 * in the relative order specified by the guard node "node" and
5398 * its descendants.
5400 * The relation "executed" maps the outer generated loop iterators
5401 * to the domain elements executed by those iterations.
5403 * Ensure that the associated guard is enforced by the outer AST
5404 * constructs by adding it to the guard of the graft.
5405 * Since we know that we will enforce the guard, we can also include it
5406 * in the generated constraints used to construct an AST for
5407 * the descendant nodes.
5409 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5410 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5411 __isl_take isl_union_map *executed)
5413 isl_space *space;
5414 isl_set *guard, *hoisted;
5415 isl_basic_set *enforced;
5416 isl_ast_build *sub_build;
5417 isl_ast_graft *graft;
5418 isl_ast_graft_list *list;
5419 unsigned n1, n2;
5421 space = isl_ast_build_get_space(build, 1);
5422 guard = isl_schedule_node_guard_get_guard(node);
5423 n1 = isl_space_dim(space, isl_dim_param);
5424 guard = isl_set_align_params(guard, space);
5425 n2 = isl_set_dim(guard, isl_dim_param);
5426 if (n2 > n1)
5427 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5428 "guard node is not allowed to introduce "
5429 "new parameters", guard = isl_set_free(guard));
5430 guard = isl_set_preimage_multi_aff(guard,
5431 isl_multi_aff_copy(build->internal2input));
5432 guard = isl_ast_build_specialize(build, guard);
5433 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5435 sub_build = isl_ast_build_copy(build);
5436 sub_build = isl_ast_build_restrict_generated(sub_build,
5437 isl_set_copy(guard));
5439 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5440 node, executed);
5442 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5443 if (isl_set_n_basic_set(hoisted) > 1)
5444 list = isl_ast_graft_list_gist_guards(list,
5445 isl_set_copy(hoisted));
5446 guard = isl_set_intersect(guard, hoisted);
5447 enforced = extract_shared_enforced(list, build);
5448 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5449 build, sub_build);
5451 isl_ast_build_free(sub_build);
5452 isl_ast_build_free(build);
5453 return isl_ast_graft_list_from_ast_graft(graft);
5456 /* Call the before_each_mark callback, if requested by the user.
5458 * Return 0 on success and -1 on error.
5460 * The caller is responsible for recording the current inverse schedule
5461 * in "build".
5463 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5464 __isl_keep isl_ast_build *build)
5466 if (!build)
5467 return isl_stat_error;
5468 if (!build->before_each_mark)
5469 return isl_stat_ok;
5470 return build->before_each_mark(mark, build,
5471 build->before_each_mark_user);
5474 /* Call the after_each_mark callback, if requested by the user.
5476 * The caller is responsible for recording the current inverse schedule
5477 * in "build".
5479 static __isl_give isl_ast_graft *after_each_mark(
5480 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5482 if (!graft || !build)
5483 return isl_ast_graft_free(graft);
5484 if (!build->after_each_mark)
5485 return graft;
5486 graft->node = build->after_each_mark(graft->node, build,
5487 build->after_each_mark_user);
5488 if (!graft->node)
5489 return isl_ast_graft_free(graft);
5490 return graft;
5494 /* Generate an AST that visits the elements in the domain of "executed"
5495 * in the relative order specified by the mark node "node" and
5496 * its descendants.
5498 * The relation "executed" maps the outer generated loop iterators
5499 * to the domain elements executed by those iterations.
5501 * Since we may be calling before_each_mark and after_each_mark
5502 * callbacks, we record the current inverse schedule in the build.
5504 * We generate an AST for the child of the mark node, combine
5505 * the graft list into a single graft and then insert the mark
5506 * in the AST of that single graft.
5508 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5509 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5510 __isl_take isl_union_map *executed)
5512 isl_id *mark;
5513 isl_ast_graft *graft;
5514 isl_ast_graft_list *list;
5515 int n;
5517 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5519 mark = isl_schedule_node_mark_get_id(node);
5520 if (before_each_mark(mark, build) < 0)
5521 node = isl_schedule_node_free(node);
5523 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5524 list = isl_ast_graft_list_fuse(list, build);
5525 n = isl_ast_graft_list_n_ast_graft(list);
5526 if (n < 0)
5527 list = isl_ast_graft_list_free(list);
5528 if (n == 0) {
5529 isl_id_free(mark);
5530 } else {
5531 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5532 graft = isl_ast_graft_insert_mark(graft, mark);
5533 graft = after_each_mark(graft, build);
5534 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5536 isl_ast_build_free(build);
5538 return list;
5541 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5542 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5543 __isl_take isl_union_map *executed);
5545 /* Generate an AST that visits the elements in the domain of "executed"
5546 * in the relative order specified by the sequence (or set) node "node" and
5547 * its descendants.
5549 * The relation "executed" maps the outer generated loop iterators
5550 * to the domain elements executed by those iterations.
5552 * We simply generate an AST for each of the children and concatenate
5553 * the results.
5555 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5556 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5557 __isl_take isl_union_map *executed)
5559 int i, n;
5560 isl_ctx *ctx;
5561 isl_ast_graft_list *list;
5563 ctx = isl_ast_build_get_ctx(build);
5564 list = isl_ast_graft_list_alloc(ctx, 0);
5566 n = isl_schedule_node_n_children(node);
5567 for (i = 0; i < n; ++i) {
5568 isl_schedule_node *child;
5569 isl_ast_graft_list *list_i;
5571 child = isl_schedule_node_get_child(node, i);
5572 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5573 child, isl_union_map_copy(executed));
5574 list = isl_ast_graft_list_concat(list, list_i);
5576 isl_ast_build_free(build);
5577 isl_schedule_node_free(node);
5578 isl_union_map_free(executed);
5580 return list;
5583 /* Generate an AST that visits the elements in the domain of "executed"
5584 * in the relative order specified by the node "node" and its descendants.
5586 * The relation "executed" maps the outer generated loop iterators
5587 * to the domain elements executed by those iterations.
5589 * If the node is a leaf, then we pass control to generate_inner_level.
5590 * Note that the current build does not refer to any band node, so
5591 * that generate_inner_level will not try to visit the child of
5592 * the leaf node.
5594 * The other node types are handled in separate functions.
5595 * Set nodes are currently treated in the same way as sequence nodes.
5596 * The children of a set node may be executed in any order,
5597 * including the order of the children.
5599 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5600 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5601 __isl_take isl_union_map *executed)
5603 enum isl_schedule_node_type type;
5605 type = isl_schedule_node_get_type(node);
5607 switch (type) {
5608 case isl_schedule_node_error:
5609 goto error;
5610 case isl_schedule_node_leaf:
5611 isl_schedule_node_free(node);
5612 return generate_inner_level(executed, build);
5613 case isl_schedule_node_band:
5614 return build_ast_from_band(build, node, executed);
5615 case isl_schedule_node_context:
5616 return build_ast_from_context(build, node, executed);
5617 case isl_schedule_node_domain:
5618 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5619 "unexpected internal domain node", goto error);
5620 case isl_schedule_node_expansion:
5621 return build_ast_from_expansion(build, node, executed);
5622 case isl_schedule_node_extension:
5623 return build_ast_from_extension(build, node, executed);
5624 case isl_schedule_node_filter:
5625 return build_ast_from_filter(build, node, executed);
5626 case isl_schedule_node_guard:
5627 return build_ast_from_guard(build, node, executed);
5628 case isl_schedule_node_mark:
5629 return build_ast_from_mark(build, node, executed);
5630 case isl_schedule_node_sequence:
5631 case isl_schedule_node_set:
5632 return build_ast_from_sequence(build, node, executed);
5635 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5636 "unhandled type", goto error);
5637 error:
5638 isl_union_map_free(executed);
5639 isl_schedule_node_free(node);
5640 isl_ast_build_free(build);
5642 return NULL;
5645 /* Generate an AST that visits the elements in the domain of "executed"
5646 * in the relative order specified by the (single) child of "node" and
5647 * its descendants.
5649 * The relation "executed" maps the outer generated loop iterators
5650 * to the domain elements executed by those iterations.
5652 * This function is never called on a leaf, set or sequence node,
5653 * so the node always has exactly one child.
5655 static __isl_give isl_ast_graft_list *build_ast_from_child(
5656 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5657 __isl_take isl_union_map *executed)
5659 node = isl_schedule_node_child(node, 0);
5660 return build_ast_from_schedule_node(build, node, executed);
5663 /* Generate an AST that visits the elements in the domain of the domain
5664 * node "node" in the relative order specified by its descendants.
5666 * An initial inverse schedule is created that maps a zero-dimensional
5667 * schedule space to the node domain.
5668 * The input "build" is assumed to have a parametric domain and
5669 * is replaced by the same zero-dimensional schedule space.
5671 * We also add some of the parameter constraints in the build domain
5672 * to the executed relation. Adding these constraints
5673 * allows for an earlier detection of conflicts in some cases.
5674 * However, we do not want to divide the executed relation into
5675 * more disjuncts than necessary. We therefore approximate
5676 * the constraints on the parameters by a single disjunct set.
5678 static __isl_give isl_ast_node *build_ast_from_domain(
5679 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5681 isl_ctx *ctx;
5682 isl_union_set *domain, *schedule_domain;
5683 isl_union_map *executed;
5684 isl_space *space;
5685 isl_set *set;
5686 isl_ast_graft_list *list;
5687 isl_ast_node *ast;
5688 int is_params;
5690 if (!build)
5691 goto error;
5693 ctx = isl_ast_build_get_ctx(build);
5694 space = isl_ast_build_get_space(build, 1);
5695 is_params = isl_space_is_params(space);
5696 isl_space_free(space);
5697 if (is_params < 0)
5698 goto error;
5699 if (!is_params)
5700 isl_die(ctx, isl_error_unsupported,
5701 "expecting parametric initial context", goto error);
5703 domain = isl_schedule_node_domain_get_domain(node);
5704 domain = isl_union_set_coalesce(domain);
5706 space = isl_union_set_get_space(domain);
5707 space = isl_space_set_from_params(space);
5708 build = isl_ast_build_product(build, space);
5710 set = isl_ast_build_get_domain(build);
5711 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5712 schedule_domain = isl_union_set_from_set(set);
5714 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5715 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5716 ast = isl_ast_node_from_graft_list(list, build);
5717 isl_ast_build_free(build);
5719 return ast;
5720 error:
5721 isl_schedule_node_free(node);
5722 isl_ast_build_free(build);
5723 return NULL;
5726 /* Generate an AST that visits the elements in the domain of "schedule"
5727 * in the relative order specified by the schedule tree.
5729 * "build" is an isl_ast_build that has been created using
5730 * isl_ast_build_alloc or isl_ast_build_from_context based
5731 * on a parametric set.
5733 * The construction starts at the root node of the schedule,
5734 * which is assumed to be a domain node.
5736 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5737 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5739 isl_ctx *ctx;
5740 isl_schedule_node *node;
5742 if (!build || !schedule)
5743 goto error;
5745 ctx = isl_ast_build_get_ctx(build);
5747 node = isl_schedule_get_root(schedule);
5748 if (!node)
5749 goto error;
5750 isl_schedule_free(schedule);
5752 build = isl_ast_build_copy(build);
5753 build = isl_ast_build_set_single_valued(build, 0);
5754 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5755 isl_die(ctx, isl_error_unsupported,
5756 "expecting root domain node",
5757 build = isl_ast_build_free(build));
5758 return build_ast_from_domain(build, node);
5759 error:
5760 isl_schedule_free(schedule);
5761 return NULL;