isl_tab_pip.c: enter_level: extract out finished_all_cases
[isl.git] / isl_ast_codegen.c
blob4feab5f0efc01587576a6e054c9b55b187fa6eeb
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/val.h>
15 #include <isl/space.h>
16 #include <isl/aff.h>
17 #include <isl/constraint.h>
18 #include <isl/set.h>
19 #include <isl/ilp.h>
20 #include <isl/union_set.h>
21 #include <isl/union_map.h>
22 #include <isl/schedule_node.h>
23 #include <isl_sort.h>
24 #include <isl_tarjan.h>
25 #include <isl_ast_private.h>
26 #include <isl_ast_build_expr.h>
27 #include <isl_ast_build_private.h>
28 #include <isl_ast_graft_private.h>
30 /* Data used in generate_domain.
32 * "build" is the input build.
33 * "list" collects the results.
35 struct isl_generate_domain_data {
36 isl_ast_build *build;
38 isl_ast_graft_list *list;
41 static __isl_give isl_ast_graft_list *generate_next_level(
42 __isl_take isl_union_map *executed,
43 __isl_take isl_ast_build *build);
44 static __isl_give isl_ast_graft_list *generate_code(
45 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
46 int internal);
48 /* Generate an AST for a single domain based on
49 * the (non single valued) inverse schedule "executed".
51 * We extend the schedule with the iteration domain
52 * and continue generating through a call to generate_code.
54 * In particular, if executed has the form
56 * S -> D
58 * then we continue generating code on
60 * [S -> D] -> D
62 * The extended inverse schedule is clearly single valued
63 * ensuring that the nested generate_code will not reach this function,
64 * but will instead create calls to all elements of D that need
65 * to be executed from the current schedule domain.
67 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
68 struct isl_generate_domain_data *data)
70 isl_map *identity;
71 isl_ast_build *build;
72 isl_ast_graft_list *list;
74 build = isl_ast_build_copy(data->build);
76 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
77 executed = isl_map_domain_product(executed, identity);
78 build = isl_ast_build_set_single_valued(build, 1);
80 list = generate_code(isl_union_map_from_map(executed), build, 1);
82 data->list = isl_ast_graft_list_concat(data->list, list);
84 return isl_stat_ok;
87 /* Call the at_each_domain callback, if requested by the user,
88 * after recording the current inverse schedule in the build.
90 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
91 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
93 if (!graft || !build)
94 return isl_ast_graft_free(graft);
95 if (!build->at_each_domain)
96 return graft;
98 build = isl_ast_build_copy(build);
99 build = isl_ast_build_set_executed(build,
100 isl_union_map_from_map(isl_map_copy(executed)));
101 if (!build)
102 return isl_ast_graft_free(graft);
104 graft->node = build->at_each_domain(graft->node,
105 build, build->at_each_domain_user);
106 isl_ast_build_free(build);
108 if (!graft->node)
109 graft = isl_ast_graft_free(graft);
111 return graft;
114 /* Generate a call expression for the single executed
115 * domain element "map" and put a guard around it based its (simplified)
116 * domain. "executed" is the original inverse schedule from which "map"
117 * has been derived. In particular, "map" is either identical to "executed"
118 * or it is the result of gisting "executed" with respect to the build domain.
119 * "executed" is only used if there is an at_each_domain callback.
121 * At this stage, any pending constraints in the build can no longer
122 * be simplified with respect to any enforced constraints since
123 * the call node does not have any enforced constraints.
124 * Since all pending constraints not covered by any enforced constraints
125 * will be added as a guard to the graft in create_node_scaled,
126 * even in the eliminated case, the pending constraints
127 * can be considered to have been generated by outer constructs.
129 * If the user has set an at_each_domain callback, it is called
130 * on the constructed call expression node.
132 static isl_stat add_domain(__isl_take isl_map *executed,
133 __isl_take isl_map *map, struct isl_generate_domain_data *data)
135 isl_ast_build *build;
136 isl_ast_graft *graft;
137 isl_ast_graft_list *list;
138 isl_set *guard, *pending;
140 build = isl_ast_build_copy(data->build);
141 pending = isl_ast_build_get_pending(build);
142 build = isl_ast_build_replace_pending_by_guard(build, pending);
144 guard = isl_map_domain(isl_map_copy(map));
145 guard = isl_set_compute_divs(guard);
146 guard = isl_set_coalesce(guard);
147 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
148 guard = isl_ast_build_specialize(build, guard);
150 graft = isl_ast_graft_alloc_domain(map, build);
151 graft = at_each_domain(graft, executed, build);
152 isl_ast_build_free(build);
153 isl_map_free(executed);
154 graft = isl_ast_graft_add_guard(graft, guard, data->build);
156 list = isl_ast_graft_list_from_ast_graft(graft);
157 data->list = isl_ast_graft_list_concat(data->list, list);
159 return isl_stat_ok;
162 /* Generate an AST for a single domain based on
163 * the inverse schedule "executed" and add it to data->list.
165 * If there is more than one domain element associated to the current
166 * schedule "time", then we need to continue the generation process
167 * in generate_non_single_valued.
168 * Note that the inverse schedule being single-valued may depend
169 * on constraints that are only available in the original context
170 * domain specified by the user. We therefore first introduce
171 * some of the constraints of data->build->domain. In particular,
172 * we intersect with a single-disjunct approximation of this set.
173 * We perform this approximation to avoid further splitting up
174 * the executed relation, possibly introducing a disjunctive guard
175 * on the statement.
177 * On the other hand, we only perform the test after having taken the gist
178 * of the domain as the resulting map is the one from which the call
179 * expression is constructed. Using this map to construct the call
180 * expression usually yields simpler results in cases where the original
181 * map is not obviously single-valued.
182 * If the original map is obviously single-valued, then the gist
183 * operation is skipped.
185 * Because we perform the single-valuedness test on the gisted map,
186 * we may in rare cases fail to recognize that the inverse schedule
187 * is single-valued. This becomes problematic if this happens
188 * from the recursive call through generate_non_single_valued
189 * as we would then end up in an infinite recursion.
190 * We therefore check if we are inside a call to generate_non_single_valued
191 * and revert to the ungisted map if the gisted map turns out not to be
192 * single-valued.
194 * Otherwise, call add_domain to generate a call expression (with guard) and
195 * to call the at_each_domain callback, if any.
197 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
199 struct isl_generate_domain_data *data = user;
200 isl_set *domain;
201 isl_map *map = NULL;
202 int empty, sv;
204 domain = isl_ast_build_get_domain(data->build);
205 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
206 executed = isl_map_intersect_domain(executed, domain);
207 empty = isl_map_is_empty(executed);
208 if (empty < 0)
209 goto error;
210 if (empty) {
211 isl_map_free(executed);
212 return isl_stat_ok;
215 sv = isl_map_plain_is_single_valued(executed);
216 if (sv < 0)
217 goto error;
218 if (sv)
219 return add_domain(executed, isl_map_copy(executed), data);
221 executed = isl_map_coalesce(executed);
222 map = isl_map_copy(executed);
223 map = isl_ast_build_compute_gist_map_domain(data->build, map);
224 sv = isl_map_is_single_valued(map);
225 if (sv < 0)
226 goto error;
227 if (!sv) {
228 isl_map_free(map);
229 if (data->build->single_valued)
230 map = isl_map_copy(executed);
231 else
232 return generate_non_single_valued(executed, data);
235 return add_domain(executed, map, data);
236 error:
237 isl_map_free(map);
238 isl_map_free(executed);
239 return isl_stat_error;
242 /* Call build->create_leaf to a create "leaf" node in the AST,
243 * encapsulate the result in an isl_ast_graft and return the result
244 * as a 1-element list.
246 * Note that the node returned by the user may be an entire tree.
248 * Since the node itself cannot enforce any constraints, we turn
249 * all pending constraints into guards and add them to the resulting
250 * graft to ensure that they will be generated.
252 * Before we pass control to the user, we first clear some information
253 * from the build that is (presumbably) only meaningful
254 * for the current code generation.
255 * This includes the create_leaf callback itself, so we make a copy
256 * of the build first.
258 static __isl_give isl_ast_graft_list *call_create_leaf(
259 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
261 isl_set *guard;
262 isl_ast_node *node;
263 isl_ast_graft *graft;
264 isl_ast_build *user_build;
266 guard = isl_ast_build_get_pending(build);
267 user_build = isl_ast_build_copy(build);
268 user_build = isl_ast_build_replace_pending_by_guard(user_build,
269 isl_set_copy(guard));
270 user_build = isl_ast_build_set_executed(user_build, executed);
271 user_build = isl_ast_build_clear_local_info(user_build);
272 if (!user_build)
273 node = NULL;
274 else
275 node = build->create_leaf(user_build, build->create_leaf_user);
276 graft = isl_ast_graft_alloc(node, build);
277 graft = isl_ast_graft_add_guard(graft, guard, build);
278 isl_ast_build_free(build);
279 return isl_ast_graft_list_from_ast_graft(graft);
282 static __isl_give isl_ast_graft_list *build_ast_from_child(
283 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
284 __isl_take isl_union_map *executed);
286 /* Generate an AST after having handled the complete schedule
287 * of this call to the code generator or the complete band
288 * if we are generating an AST from a schedule tree.
290 * If we are inside a band node, then move on to the child of the band.
292 * If the user has specified a create_leaf callback, control
293 * is passed to the user in call_create_leaf.
295 * Otherwise, we generate one or more calls for each individual
296 * domain in generate_domain.
298 static __isl_give isl_ast_graft_list *generate_inner_level(
299 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
301 isl_ctx *ctx;
302 struct isl_generate_domain_data data = { build };
304 if (!build || !executed)
305 goto error;
307 if (isl_ast_build_has_schedule_node(build)) {
308 isl_schedule_node *node;
309 node = isl_ast_build_get_schedule_node(build);
310 build = isl_ast_build_reset_schedule_node(build);
311 return build_ast_from_child(build, node, executed);
314 if (build->create_leaf)
315 return call_create_leaf(executed, build);
317 ctx = isl_union_map_get_ctx(executed);
318 data.list = isl_ast_graft_list_alloc(ctx, 0);
319 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
320 data.list = isl_ast_graft_list_free(data.list);
322 if (0)
323 error: data.list = NULL;
324 isl_ast_build_free(build);
325 isl_union_map_free(executed);
326 return data.list;
329 /* Call the before_each_for callback, if requested by the user.
331 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
332 __isl_keep isl_ast_build *build)
334 isl_id *id;
336 if (!node || !build)
337 return isl_ast_node_free(node);
338 if (!build->before_each_for)
339 return node;
340 id = build->before_each_for(build, build->before_each_for_user);
341 node = isl_ast_node_set_annotation(node, id);
342 return node;
345 /* Call the after_each_for callback, if requested by the user.
347 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
348 __isl_keep isl_ast_build *build)
350 if (!graft || !build)
351 return isl_ast_graft_free(graft);
352 if (!build->after_each_for)
353 return graft;
354 graft->node = build->after_each_for(graft->node, build,
355 build->after_each_for_user);
356 if (!graft->node)
357 return isl_ast_graft_free(graft);
358 return graft;
361 /* Plug in all the know values of the current and outer dimensions
362 * in the domain of "executed". In principle, we only need to plug
363 * in the known value of the current dimension since the values of
364 * outer dimensions have been plugged in already.
365 * However, it turns out to be easier to just plug in all known values.
367 static __isl_give isl_union_map *plug_in_values(
368 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
370 return isl_ast_build_substitute_values_union_map_domain(build,
371 executed);
374 /* Check if the constraint "c" is a lower bound on dimension "pos",
375 * an upper bound, or independent of dimension "pos".
377 static int constraint_type(isl_constraint *c, int pos)
379 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
380 return 1;
381 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
382 return 2;
383 return 0;
386 /* Compare the types of the constraints "a" and "b",
387 * resulting in constraints that are independent of "depth"
388 * to be sorted before the lower bounds on "depth", which in
389 * turn are sorted before the upper bounds on "depth".
391 static int cmp_constraint(__isl_keep isl_constraint *a,
392 __isl_keep isl_constraint *b, void *user)
394 int *depth = user;
395 int t1 = constraint_type(a, *depth);
396 int t2 = constraint_type(b, *depth);
398 return t1 - t2;
401 /* Extract a lower bound on dimension "pos" from constraint "c".
403 * If the constraint is of the form
405 * a x + f(...) >= 0
407 * then we essentially return
409 * l = ceil(-f(...)/a)
411 * However, if the current dimension is strided, then we need to make
412 * sure that the lower bound we construct is of the form
414 * f + s a
416 * with f the offset and s the stride.
417 * We therefore compute
419 * f + s * ceil((l - f)/s)
421 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
422 int pos, __isl_keep isl_ast_build *build)
424 isl_aff *aff;
426 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
427 aff = isl_aff_ceil(aff);
429 if (isl_ast_build_has_stride(build, pos)) {
430 isl_aff *offset;
431 isl_val *stride;
433 offset = isl_ast_build_get_offset(build, pos);
434 stride = isl_ast_build_get_stride(build, pos);
436 aff = isl_aff_sub(aff, isl_aff_copy(offset));
437 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
438 aff = isl_aff_ceil(aff);
439 aff = isl_aff_scale_val(aff, stride);
440 aff = isl_aff_add(aff, offset);
443 aff = isl_ast_build_compute_gist_aff(build, aff);
445 return aff;
448 /* Return the exact lower bound (or upper bound if "upper" is set)
449 * of "domain" as a piecewise affine expression.
451 * If we are computing a lower bound (of a strided dimension), then
452 * we need to make sure it is of the form
454 * f + s a
456 * where f is the offset and s is the stride.
457 * We therefore need to include the stride constraint before computing
458 * the minimum.
460 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
461 __isl_keep isl_ast_build *build, int upper)
463 isl_set *stride;
464 isl_map *it_map;
465 isl_pw_aff *pa;
466 isl_pw_multi_aff *pma;
468 domain = isl_set_copy(domain);
469 if (!upper) {
470 stride = isl_ast_build_get_stride_constraint(build);
471 domain = isl_set_intersect(domain, stride);
473 it_map = isl_ast_build_map_to_iterator(build, domain);
474 if (upper)
475 pma = isl_map_lexmax_pw_multi_aff(it_map);
476 else
477 pma = isl_map_lexmin_pw_multi_aff(it_map);
478 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
479 isl_pw_multi_aff_free(pma);
480 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
481 pa = isl_pw_aff_coalesce(pa);
483 return pa;
486 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
487 * remove_redundant_lower_bounds.
489 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
490 void *user)
492 return isl_pw_aff_plain_cmp(a, b);
495 /* Given a list of lower bounds "list", remove those that are redundant
496 * with respect to the other bounds in "list" and the domain of "build".
498 * We first sort the bounds in the same way as they would be sorted
499 * by set_for_node_expressions so that we can try and remove the last
500 * bounds first.
502 * For a lower bound to be effective, there needs to be at least
503 * one domain element for which it is larger than all other lower bounds.
504 * For each lower bound we therefore intersect the domain with
505 * the conditions that it is larger than all other bounds and
506 * check whether the result is empty. If so, the bound can be removed.
508 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
509 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
511 int i, j, n;
512 isl_set *domain;
514 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
515 if (!list)
516 return NULL;
518 n = isl_pw_aff_list_n_pw_aff(list);
519 if (n <= 1)
520 return list;
522 domain = isl_ast_build_get_domain(build);
524 for (i = n - 1; i >= 0; --i) {
525 isl_pw_aff *pa_i;
526 isl_set *domain_i;
527 int empty;
529 domain_i = isl_set_copy(domain);
530 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
532 for (j = 0; j < n; ++j) {
533 isl_pw_aff *pa_j;
534 isl_set *better;
536 if (j == i)
537 continue;
539 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
540 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
541 domain_i = isl_set_intersect(domain_i, better);
544 empty = isl_set_is_empty(domain_i);
546 isl_set_free(domain_i);
547 isl_pw_aff_free(pa_i);
549 if (empty < 0)
550 goto error;
551 if (!empty)
552 continue;
553 list = isl_pw_aff_list_drop(list, i, 1);
554 n--;
557 isl_set_free(domain);
559 return list;
560 error:
561 isl_set_free(domain);
562 return isl_pw_aff_list_free(list);
565 /* Extract a lower bound on dimension "pos" from each constraint
566 * in "constraints" and return the list of lower bounds.
567 * If "constraints" has zero elements, then we extract a lower bound
568 * from "domain" instead.
570 * If the current dimension is strided, then the lower bound
571 * is adjusted by lower_bound to match the stride information.
572 * This modification may make one or more lower bounds redundant
573 * with respect to the other lower bounds. We therefore check
574 * for this condition and remove the redundant lower bounds.
576 static __isl_give isl_pw_aff_list *lower_bounds(
577 __isl_keep isl_constraint_list *constraints, int pos,
578 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
580 isl_ctx *ctx;
581 isl_pw_aff_list *list;
582 int i, n;
584 if (!build)
585 return NULL;
587 n = isl_constraint_list_n_constraint(constraints);
588 if (n == 0) {
589 isl_pw_aff *pa;
590 pa = exact_bound(domain, build, 0);
591 return isl_pw_aff_list_from_pw_aff(pa);
594 ctx = isl_ast_build_get_ctx(build);
595 list = isl_pw_aff_list_alloc(ctx,n);
597 for (i = 0; i < n; ++i) {
598 isl_aff *aff;
599 isl_constraint *c;
601 c = isl_constraint_list_get_constraint(constraints, i);
602 aff = lower_bound(c, pos, build);
603 isl_constraint_free(c);
604 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
607 if (isl_ast_build_has_stride(build, pos))
608 list = remove_redundant_lower_bounds(list, build);
610 return list;
613 /* Extract an upper bound on dimension "pos" from each constraint
614 * in "constraints" and return the list of upper bounds.
615 * If "constraints" has zero elements, then we extract an upper bound
616 * from "domain" instead.
618 static __isl_give isl_pw_aff_list *upper_bounds(
619 __isl_keep isl_constraint_list *constraints, int pos,
620 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
622 isl_ctx *ctx;
623 isl_pw_aff_list *list;
624 int i, n;
626 n = isl_constraint_list_n_constraint(constraints);
627 if (n == 0) {
628 isl_pw_aff *pa;
629 pa = exact_bound(domain, build, 1);
630 return isl_pw_aff_list_from_pw_aff(pa);
633 ctx = isl_ast_build_get_ctx(build);
634 list = isl_pw_aff_list_alloc(ctx,n);
636 for (i = 0; i < n; ++i) {
637 isl_aff *aff;
638 isl_constraint *c;
640 c = isl_constraint_list_get_constraint(constraints, i);
641 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
642 isl_constraint_free(c);
643 aff = isl_aff_floor(aff);
644 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
647 return list;
650 /* Return an isl_ast_expr that performs the reduction of type "type"
651 * on AST expressions corresponding to the elements in "list".
653 * The list is assumed to contain at least one element.
654 * If the list contains exactly one element, then the returned isl_ast_expr
655 * simply computes that affine expression.
656 * If the list contains more than one element, then we sort it
657 * using a fairly abitrary but hopefully reasonably stable order.
659 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
660 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
662 int i, n;
663 isl_ctx *ctx;
664 isl_ast_expr *expr;
666 if (!list)
667 return NULL;
669 n = isl_pw_aff_list_n_pw_aff(list);
671 if (n == 1)
672 return isl_ast_build_expr_from_pw_aff_internal(build,
673 isl_pw_aff_list_get_pw_aff(list, 0));
675 ctx = isl_pw_aff_list_get_ctx(list);
676 expr = isl_ast_expr_alloc_op(ctx, type, n);
677 if (!expr)
678 return NULL;
680 list = isl_pw_aff_list_copy(list);
681 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
682 if (!list)
683 return isl_ast_expr_free(expr);
685 for (i = 0; i < n; ++i) {
686 isl_ast_expr *expr_i;
688 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
689 isl_pw_aff_list_get_pw_aff(list, i));
690 if (!expr_i)
691 goto error;
692 expr->u.op.args[i] = expr_i;
695 isl_pw_aff_list_free(list);
696 return expr;
697 error:
698 isl_pw_aff_list_free(list);
699 isl_ast_expr_free(expr);
700 return NULL;
703 /* Add guards implied by the "generated constraints",
704 * but not (necessarily) enforced by the generated AST to "guard".
705 * In particular, if there is any stride constraints,
706 * then add the guard implied by those constraints.
707 * If we have generated a degenerate loop, then add the guard
708 * implied by "bounds" on the outer dimensions, i.e., the guard
709 * that ensures that the single value actually exists.
710 * Since there may also be guards implied by a combination
711 * of these constraints, we first combine them before
712 * deriving the implied constraints.
714 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
715 int degenerate, __isl_keep isl_basic_set *bounds,
716 __isl_keep isl_ast_build *build)
718 int depth, has_stride;
719 isl_space *space;
720 isl_set *dom, *set;
722 depth = isl_ast_build_get_depth(build);
723 has_stride = isl_ast_build_has_stride(build, depth);
724 if (!has_stride && !degenerate)
725 return guard;
727 space = isl_basic_set_get_space(bounds);
728 dom = isl_set_universe(space);
730 if (degenerate) {
731 bounds = isl_basic_set_copy(bounds);
732 bounds = isl_basic_set_drop_constraints_not_involving_dims(
733 bounds, isl_dim_set, depth, 1);
734 set = isl_set_from_basic_set(bounds);
735 dom = isl_set_intersect(dom, set);
738 if (has_stride) {
739 set = isl_ast_build_get_stride_constraint(build);
740 dom = isl_set_intersect(dom, set);
743 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
744 dom = isl_ast_build_compute_gist(build, dom);
745 guard = isl_set_intersect(guard, dom);
747 return guard;
750 /* Update "graft" based on "sub_build" for the degenerate case.
752 * "build" is the build in which graft->node was created
753 * "sub_build" contains information about the current level itself,
754 * including the single value attained.
756 * We set the initialization part of the for loop to the single
757 * value attained by the current dimension.
758 * The increment and condition are not strictly needed as the are known
759 * to be "1" and "iterator <= value" respectively.
761 static __isl_give isl_ast_graft *refine_degenerate(
762 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
763 __isl_keep isl_ast_build *sub_build)
765 isl_pw_aff *value;
767 if (!graft || !sub_build)
768 return isl_ast_graft_free(graft);
770 value = isl_pw_aff_copy(sub_build->value);
772 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
773 value);
774 if (!graft->node->u.f.init)
775 return isl_ast_graft_free(graft);
777 return graft;
780 /* Return the intersection of constraints in "list" as a set.
782 static __isl_give isl_set *intersect_constraints(
783 __isl_keep isl_constraint_list *list)
785 int i, n;
786 isl_basic_set *bset;
788 n = isl_constraint_list_n_constraint(list);
789 if (n < 1)
790 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
791 "expecting at least one constraint", return NULL);
793 bset = isl_basic_set_from_constraint(
794 isl_constraint_list_get_constraint(list, 0));
795 for (i = 1; i < n; ++i) {
796 isl_basic_set *bset_i;
798 bset_i = isl_basic_set_from_constraint(
799 isl_constraint_list_get_constraint(list, i));
800 bset = isl_basic_set_intersect(bset, bset_i);
803 return isl_set_from_basic_set(bset);
806 /* Compute the constraints on the outer dimensions enforced by
807 * graft->node and add those constraints to graft->enforced,
808 * in case the upper bound is expressed as a set "upper".
810 * In particular, if l(...) is a lower bound in "lower", and
812 * -a i + f(...) >= 0 or a i <= f(...)
814 * is an upper bound ocnstraint on the current dimension i,
815 * then the for loop enforces the constraint
817 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
819 * We therefore simply take each lower bound in turn, plug it into
820 * the upper bounds and compute the intersection over all lower bounds.
822 * If a lower bound is a rational expression, then
823 * isl_basic_set_preimage_multi_aff will force this rational
824 * expression to have only integer values. However, the loop
825 * itself does not enforce this integrality constraint. We therefore
826 * use the ceil of the lower bounds instead of the lower bounds themselves.
827 * Other constraints will make sure that the for loop is only executed
828 * when each of the lower bounds attains an integral value.
829 * In particular, potentially rational values only occur in
830 * lower_bound if the offset is a (seemingly) rational expression,
831 * but then outer conditions will make sure that this rational expression
832 * only attains integer values.
834 static __isl_give isl_ast_graft *set_enforced_from_set(
835 __isl_take isl_ast_graft *graft,
836 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
838 isl_space *space;
839 isl_basic_set *enforced;
840 isl_pw_multi_aff *pma;
841 int i, n;
843 if (!graft || !lower)
844 return isl_ast_graft_free(graft);
846 space = isl_set_get_space(upper);
847 enforced = isl_basic_set_universe(isl_space_copy(space));
849 space = isl_space_map_from_set(space);
850 pma = isl_pw_multi_aff_identity(space);
852 n = isl_pw_aff_list_n_pw_aff(lower);
853 for (i = 0; i < n; ++i) {
854 isl_pw_aff *pa;
855 isl_set *enforced_i;
856 isl_basic_set *hull;
857 isl_pw_multi_aff *pma_i;
859 pa = isl_pw_aff_list_get_pw_aff(lower, i);
860 pa = isl_pw_aff_ceil(pa);
861 pma_i = isl_pw_multi_aff_copy(pma);
862 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
863 enforced_i = isl_set_copy(upper);
864 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
865 hull = isl_set_simple_hull(enforced_i);
866 enforced = isl_basic_set_intersect(enforced, hull);
869 isl_pw_multi_aff_free(pma);
871 graft = isl_ast_graft_enforce(graft, enforced);
873 return graft;
876 /* Compute the constraints on the outer dimensions enforced by
877 * graft->node and add those constraints to graft->enforced,
878 * in case the upper bound is expressed as
879 * a list of affine expressions "upper".
881 * The enforced condition is that each lower bound expression is less
882 * than or equal to each upper bound expression.
884 static __isl_give isl_ast_graft *set_enforced_from_list(
885 __isl_take isl_ast_graft *graft,
886 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
888 isl_set *cond;
889 isl_basic_set *enforced;
891 lower = isl_pw_aff_list_copy(lower);
892 upper = isl_pw_aff_list_copy(upper);
893 cond = isl_pw_aff_list_le_set(lower, upper);
894 enforced = isl_set_simple_hull(cond);
895 graft = isl_ast_graft_enforce(graft, enforced);
897 return graft;
900 /* Does "aff" have a negative constant term?
902 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
903 __isl_take isl_aff *aff, void *user)
905 int *neg = user;
906 isl_val *v;
908 v = isl_aff_get_constant_val(aff);
909 *neg = isl_val_is_neg(v);
910 isl_val_free(v);
911 isl_set_free(set);
912 isl_aff_free(aff);
914 return *neg ? isl_stat_ok : isl_stat_error;
917 /* Does "pa" have a negative constant term over its entire domain?
919 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
920 void *user)
922 isl_stat r;
923 int *neg = user;
925 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
926 isl_pw_aff_free(pa);
928 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
931 /* Does each element in "list" have a negative constant term?
933 * The callback terminates the iteration as soon an element has been
934 * found that does not have a negative constant term.
936 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
938 int neg = 1;
940 if (isl_pw_aff_list_foreach(list,
941 &pw_aff_constant_is_negative, &neg) < 0 && neg)
942 return -1;
944 return neg;
947 /* Add 1 to each of the elements in "list", where each of these elements
948 * is defined over the internal schedule space of "build".
950 static __isl_give isl_pw_aff_list *list_add_one(
951 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
953 int i, n;
954 isl_space *space;
955 isl_aff *aff;
956 isl_pw_aff *one;
958 space = isl_ast_build_get_space(build, 1);
959 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
960 aff = isl_aff_add_constant_si(aff, 1);
961 one = isl_pw_aff_from_aff(aff);
963 n = isl_pw_aff_list_n_pw_aff(list);
964 for (i = 0; i < n; ++i) {
965 isl_pw_aff *pa;
966 pa = isl_pw_aff_list_get_pw_aff(list, i);
967 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
968 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
971 isl_pw_aff_free(one);
973 return list;
976 /* Set the condition part of the for node graft->node in case
977 * the upper bound is represented as a list of piecewise affine expressions.
979 * In particular, set the condition to
981 * iterator <= min(list of upper bounds)
983 * If each of the upper bounds has a negative constant term, then
984 * set the condition to
986 * iterator < min(list of (upper bound + 1)s)
989 static __isl_give isl_ast_graft *set_for_cond_from_list(
990 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
991 __isl_keep isl_ast_build *build)
993 int neg;
994 isl_ast_expr *bound, *iterator, *cond;
995 enum isl_ast_op_type type = isl_ast_op_le;
997 if (!graft || !list)
998 return isl_ast_graft_free(graft);
1000 neg = list_constant_is_negative(list);
1001 if (neg < 0)
1002 return isl_ast_graft_free(graft);
1003 list = isl_pw_aff_list_copy(list);
1004 if (neg) {
1005 list = list_add_one(list, build);
1006 type = isl_ast_op_lt;
1009 bound = reduce_list(isl_ast_op_min, list, build);
1010 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1011 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1012 graft->node->u.f.cond = cond;
1014 isl_pw_aff_list_free(list);
1015 if (!graft->node->u.f.cond)
1016 return isl_ast_graft_free(graft);
1017 return graft;
1020 /* Set the condition part of the for node graft->node in case
1021 * the upper bound is represented as a set.
1023 static __isl_give isl_ast_graft *set_for_cond_from_set(
1024 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1025 __isl_keep isl_ast_build *build)
1027 isl_ast_expr *cond;
1029 if (!graft)
1030 return NULL;
1032 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1033 graft->node->u.f.cond = cond;
1034 if (!graft->node->u.f.cond)
1035 return isl_ast_graft_free(graft);
1036 return graft;
1039 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1040 * the current dimension.
1042 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1044 int depth;
1045 isl_val *v;
1046 isl_ctx *ctx;
1048 if (!build)
1049 return NULL;
1050 ctx = isl_ast_build_get_ctx(build);
1051 depth = isl_ast_build_get_depth(build);
1053 if (!isl_ast_build_has_stride(build, depth))
1054 return isl_ast_expr_alloc_int_si(ctx, 1);
1056 v = isl_ast_build_get_stride(build, depth);
1057 return isl_ast_expr_from_val(v);
1060 /* Should we express the loop condition as
1062 * iterator <= min(list of upper bounds)
1064 * or as a conjunction of constraints?
1066 * The first is constructed from a list of upper bounds.
1067 * The second is constructed from a set.
1069 * If there are no upper bounds in "constraints", then this could mean
1070 * that "domain" simply doesn't have an upper bound or that we didn't
1071 * pick any upper bound. In the first case, we want to generate the
1072 * loop condition as a(n empty) conjunction of constraints
1073 * In the second case, we will compute
1074 * a single upper bound from "domain" and so we use the list form.
1076 * If there are upper bounds in "constraints",
1077 * then we use the list form iff the atomic_upper_bound option is set.
1079 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1080 __isl_keep isl_set *domain, int depth)
1082 if (n_upper > 0)
1083 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1084 else
1085 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1088 /* Fill in the expressions of the for node in graft->node.
1090 * In particular,
1091 * - set the initialization part of the loop to the maximum of the lower bounds
1092 * - extract the increment from the stride of the current dimension
1093 * - construct the for condition either based on a list of upper bounds
1094 * or on a set of upper bound constraints.
1096 static __isl_give isl_ast_graft *set_for_node_expressions(
1097 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1098 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1099 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1101 isl_ast_node *node;
1103 if (!graft)
1104 return NULL;
1106 build = isl_ast_build_copy(build);
1108 node = graft->node;
1109 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1110 node->u.f.inc = for_inc(build);
1112 if (!node->u.f.init || !node->u.f.inc)
1113 graft = isl_ast_graft_free(graft);
1115 if (use_list)
1116 graft = set_for_cond_from_list(graft, upper_list, build);
1117 else
1118 graft = set_for_cond_from_set(graft, upper_set, build);
1120 isl_ast_build_free(build);
1122 return graft;
1125 /* Update "graft" based on "bounds" and "domain" for the generic,
1126 * non-degenerate, case.
1128 * "c_lower" and "c_upper" contain the lower and upper bounds
1129 * that the loop node should express.
1130 * "domain" is the subset of the intersection of the constraints
1131 * for which some code is executed.
1133 * There may be zero lower bounds or zero upper bounds in "constraints"
1134 * in case the list of constraints was created
1135 * based on the atomic option or based on separation with explicit bounds.
1136 * In that case, we use "domain" to derive lower and/or upper bounds.
1138 * We first compute a list of one or more lower bounds.
1140 * Then we decide if we want to express the condition as
1142 * iterator <= min(list of upper bounds)
1144 * or as a conjunction of constraints.
1146 * The set of enforced constraints is then computed either based on
1147 * a list of upper bounds or on a set of upper bound constraints.
1148 * We do not compute any enforced constraints if we were forced
1149 * to compute a lower or upper bound using exact_bound. The domains
1150 * of the resulting expressions may imply some bounds on outer dimensions
1151 * that we do not want to appear in the enforced constraints since
1152 * they are not actually enforced by the corresponding code.
1154 * Finally, we fill in the expressions of the for node.
1156 static __isl_give isl_ast_graft *refine_generic_bounds(
1157 __isl_take isl_ast_graft *graft,
1158 __isl_take isl_constraint_list *c_lower,
1159 __isl_take isl_constraint_list *c_upper,
1160 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1162 int depth;
1163 isl_ctx *ctx;
1164 isl_pw_aff_list *lower;
1165 int use_list;
1166 isl_set *upper_set = NULL;
1167 isl_pw_aff_list *upper_list = NULL;
1168 int n_lower, n_upper;
1170 if (!graft || !c_lower || !c_upper || !build)
1171 goto error;
1173 depth = isl_ast_build_get_depth(build);
1174 ctx = isl_ast_graft_get_ctx(graft);
1176 n_lower = isl_constraint_list_n_constraint(c_lower);
1177 n_upper = isl_constraint_list_n_constraint(c_upper);
1179 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1181 lower = lower_bounds(c_lower, depth, domain, build);
1183 if (use_list)
1184 upper_list = upper_bounds(c_upper, depth, domain, build);
1185 else if (n_upper > 0)
1186 upper_set = intersect_constraints(c_upper);
1187 else
1188 upper_set = isl_set_universe(isl_set_get_space(domain));
1190 if (n_lower == 0 || n_upper == 0)
1192 else if (use_list)
1193 graft = set_enforced_from_list(graft, lower, upper_list);
1194 else
1195 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1197 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1198 upper_set, build);
1200 isl_pw_aff_list_free(lower);
1201 isl_pw_aff_list_free(upper_list);
1202 isl_set_free(upper_set);
1203 isl_constraint_list_free(c_lower);
1204 isl_constraint_list_free(c_upper);
1206 return graft;
1207 error:
1208 isl_constraint_list_free(c_lower);
1209 isl_constraint_list_free(c_upper);
1210 return isl_ast_graft_free(graft);
1213 /* Internal data structure used inside count_constraints to keep
1214 * track of the number of constraints that are independent of dimension "pos",
1215 * the lower bounds in "pos" and the upper bounds in "pos".
1217 struct isl_ast_count_constraints_data {
1218 int pos;
1220 int n_indep;
1221 int n_lower;
1222 int n_upper;
1225 /* Increment data->n_indep, data->lower or data->upper depending
1226 * on whether "c" is independenct of dimensions data->pos,
1227 * a lower bound or an upper bound.
1229 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1231 struct isl_ast_count_constraints_data *data = user;
1233 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1234 data->n_lower++;
1235 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1236 data->n_upper++;
1237 else
1238 data->n_indep++;
1240 isl_constraint_free(c);
1242 return isl_stat_ok;
1245 /* Update "graft" based on "bounds" and "domain" for the generic,
1246 * non-degenerate, case.
1248 * "list" respresent the list of bounds that need to be encoded by
1249 * the for loop. Only the constraints that involve the iterator
1250 * are relevant here. The other constraints are taken care of by
1251 * the caller and are included in the generated constraints of "build".
1252 * "domain" is the subset of the intersection of the constraints
1253 * for which some code is executed.
1254 * "build" is the build in which graft->node was created.
1256 * We separate lower bounds, upper bounds and constraints that
1257 * are independent of the loop iterator.
1259 * The actual for loop bounds are generated in refine_generic_bounds.
1261 static __isl_give isl_ast_graft *refine_generic_split(
1262 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1263 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1265 struct isl_ast_count_constraints_data data;
1266 isl_constraint_list *lower;
1267 isl_constraint_list *upper;
1269 if (!list)
1270 return isl_ast_graft_free(graft);
1272 data.pos = isl_ast_build_get_depth(build);
1274 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1275 if (!list)
1276 return isl_ast_graft_free(graft);
1278 data.n_indep = data.n_lower = data.n_upper = 0;
1279 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1280 isl_constraint_list_free(list);
1281 return isl_ast_graft_free(graft);
1284 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1285 upper = isl_constraint_list_copy(lower);
1286 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1287 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1289 return refine_generic_bounds(graft, lower, upper, domain, build);
1292 /* Update "graft" based on "bounds" and "domain" for the generic,
1293 * non-degenerate, case.
1295 * "bounds" respresent the bounds that need to be encoded by
1296 * the for loop (or a guard around the for loop).
1297 * "domain" is the subset of "bounds" for which some code is executed.
1298 * "build" is the build in which graft->node was created.
1300 * We break up "bounds" into a list of constraints and continue with
1301 * refine_generic_split.
1303 static __isl_give isl_ast_graft *refine_generic(
1304 __isl_take isl_ast_graft *graft,
1305 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1306 __isl_keep isl_ast_build *build)
1308 isl_constraint_list *list;
1310 if (!build || !graft)
1311 return isl_ast_graft_free(graft);
1313 list = isl_basic_set_get_constraint_list(bounds);
1315 graft = refine_generic_split(graft, list, domain, build);
1317 return graft;
1320 /* Create a for node for the current level.
1322 * Mark the for node degenerate if "degenerate" is set.
1324 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1325 int degenerate)
1327 int depth;
1328 isl_id *id;
1329 isl_ast_node *node;
1331 if (!build)
1332 return NULL;
1334 depth = isl_ast_build_get_depth(build);
1335 id = isl_ast_build_get_iterator_id(build, depth);
1336 node = isl_ast_node_alloc_for(id);
1337 if (degenerate)
1338 node = isl_ast_node_for_mark_degenerate(node);
1340 return node;
1343 /* If the ast_build_exploit_nested_bounds option is set, then return
1344 * the constraints enforced by all elements in "list".
1345 * Otherwise, return the universe.
1347 static __isl_give isl_basic_set *extract_shared_enforced(
1348 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1350 isl_ctx *ctx;
1351 isl_space *space;
1353 if (!list)
1354 return NULL;
1356 ctx = isl_ast_graft_list_get_ctx(list);
1357 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1358 return isl_ast_graft_list_extract_shared_enforced(list, build);
1360 space = isl_ast_build_get_space(build, 1);
1361 return isl_basic_set_universe(space);
1364 /* Return the pending constraints of "build" that are not already taken
1365 * care of (by a combination of "enforced" and the generated constraints
1366 * of "build").
1368 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1369 __isl_keep isl_basic_set *enforced)
1371 isl_set *guard, *context;
1373 guard = isl_ast_build_get_pending(build);
1374 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1375 context = isl_set_intersect(context,
1376 isl_ast_build_get_generated(build));
1377 return isl_set_gist(guard, context);
1380 /* Create an AST node for the current dimension based on
1381 * the schedule domain "bounds" and return the node encapsulated
1382 * in an isl_ast_graft.
1384 * "executed" is the current inverse schedule, taking into account
1385 * the bounds in "bounds"
1386 * "domain" is the domain of "executed", with inner dimensions projected out.
1387 * It may be a strict subset of "bounds" in case "bounds" was created
1388 * based on the atomic option or based on separation with explicit bounds.
1390 * "domain" may satisfy additional equalities that result
1391 * from intersecting "executed" with "bounds" in add_node.
1392 * It may also satisfy some global constraints that were dropped out because
1393 * we performed separation with explicit bounds.
1394 * The very first step is then to copy these constraints to "bounds".
1396 * Since we may be calling before_each_for and after_each_for
1397 * callbacks, we record the current inverse schedule in the build.
1399 * We consider three builds,
1400 * "build" is the one in which the current level is created,
1401 * "body_build" is the build in which the next level is created,
1402 * "sub_build" is essentially the same as "body_build", except that
1403 * the depth has not been increased yet.
1405 * "build" already contains information (in strides and offsets)
1406 * about the strides at the current level, but this information is not
1407 * reflected in the build->domain.
1408 * We first add this information and the "bounds" to the sub_build->domain.
1409 * isl_ast_build_set_loop_bounds adds the stride information and
1410 * checks whether the current dimension attains
1411 * only a single value and whether this single value can be represented using
1412 * a single affine expression.
1413 * In the first case, the current level is considered "degenerate".
1414 * In the second, sub-case, the current level is considered "eliminated".
1415 * Eliminated levels don't need to be reflected in the AST since we can
1416 * simply plug in the affine expression. For degenerate, but non-eliminated,
1417 * levels, we do introduce a for node, but mark is as degenerate so that
1418 * it can be printed as an assignment of the single value to the loop
1419 * "iterator".
1421 * If the current level is eliminated, we explicitly plug in the value
1422 * for the current level found by isl_ast_build_set_loop_bounds in the
1423 * inverse schedule. This ensures that if we are working on a slice
1424 * of the domain based on information available in the inverse schedule
1425 * and the build domain, that then this information is also reflected
1426 * in the inverse schedule. This operation also eliminates the current
1427 * dimension from the inverse schedule making sure no inner dimensions depend
1428 * on the current dimension. Otherwise, we create a for node, marking
1429 * it degenerate if appropriate. The initial for node is still incomplete
1430 * and will be completed in either refine_degenerate or refine_generic.
1432 * We then generate a sequence of grafts for the next level,
1433 * create a surrounding graft for the current level and insert
1434 * the for node we created (if the current level is not eliminated).
1435 * Before creating a graft for the current level, we first extract
1436 * hoistable constraints from the child guards and combine them
1437 * with the pending constraints in the build. These constraints
1438 * are used to simplify the child guards and then added to the guard
1439 * of the current graft to ensure that they will be generated.
1440 * If the hoisted guard is a disjunction, then we use it directly
1441 * to gist the guards on the children before intersect it with the
1442 * pending constraints. We do so because this disjunction is typically
1443 * identical to the guards on the children such that these guards
1444 * can be effectively removed completely. After the intersection,
1445 * the gist operation would have a harder time figuring this out.
1447 * Finally, we set the bounds of the for loop in either
1448 * refine_degenerate or refine_generic.
1449 * We do so in a context where the pending constraints of the build
1450 * have been replaced by the guard of the current graft.
1452 static __isl_give isl_ast_graft *create_node_scaled(
1453 __isl_take isl_union_map *executed,
1454 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1455 __isl_take isl_ast_build *build)
1457 int depth;
1458 int degenerate, eliminated;
1459 isl_basic_set *hull;
1460 isl_basic_set *enforced;
1461 isl_set *guard, *hoisted;
1462 isl_ast_node *node = NULL;
1463 isl_ast_graft *graft;
1464 isl_ast_graft_list *children;
1465 isl_ast_build *sub_build;
1466 isl_ast_build *body_build;
1468 domain = isl_ast_build_eliminate_divs(build, domain);
1469 domain = isl_set_detect_equalities(domain);
1470 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1471 bounds = isl_basic_set_intersect(bounds, hull);
1472 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1474 depth = isl_ast_build_get_depth(build);
1475 sub_build = isl_ast_build_copy(build);
1476 bounds = isl_basic_set_remove_redundancies(bounds);
1477 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1478 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1479 isl_basic_set_copy(bounds));
1480 degenerate = isl_ast_build_has_value(sub_build);
1481 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1482 if (degenerate < 0 || eliminated < 0)
1483 executed = isl_union_map_free(executed);
1484 if (!degenerate)
1485 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1486 sub_build = isl_ast_build_set_pending_generated(sub_build,
1487 isl_basic_set_copy(bounds));
1488 if (eliminated)
1489 executed = plug_in_values(executed, sub_build);
1490 else
1491 node = create_for(build, degenerate);
1493 body_build = isl_ast_build_copy(sub_build);
1494 body_build = isl_ast_build_increase_depth(body_build);
1495 if (!eliminated)
1496 node = before_each_for(node, body_build);
1497 children = generate_next_level(executed,
1498 isl_ast_build_copy(body_build));
1500 enforced = extract_shared_enforced(children, build);
1501 guard = extract_pending(sub_build, enforced);
1502 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1503 if (isl_set_n_basic_set(hoisted) > 1)
1504 children = isl_ast_graft_list_gist_guards(children,
1505 isl_set_copy(hoisted));
1506 guard = isl_set_intersect(guard, hoisted);
1507 if (!eliminated)
1508 guard = add_implied_guards(guard, degenerate, bounds, build);
1510 graft = isl_ast_graft_alloc_from_children(children,
1511 isl_set_copy(guard), enforced, build, sub_build);
1513 if (!eliminated) {
1514 isl_ast_build *for_build;
1516 graft = isl_ast_graft_insert_for(graft, node);
1517 for_build = isl_ast_build_copy(build);
1518 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1519 isl_set_copy(guard));
1520 if (degenerate)
1521 graft = refine_degenerate(graft, for_build, sub_build);
1522 else
1523 graft = refine_generic(graft, bounds,
1524 domain, for_build);
1525 isl_ast_build_free(for_build);
1527 isl_set_free(guard);
1528 if (!eliminated)
1529 graft = after_each_for(graft, body_build);
1531 isl_ast_build_free(body_build);
1532 isl_ast_build_free(sub_build);
1533 isl_ast_build_free(build);
1534 isl_basic_set_free(bounds);
1535 isl_set_free(domain);
1537 return graft;
1540 /* Internal data structure for checking if all constraints involving
1541 * the input dimension "depth" are such that the other coefficients
1542 * are multiples of "m", reducing "m" if they are not.
1543 * If "m" is reduced all the way down to "1", then the check has failed
1544 * and we break out of the iteration.
1546 struct isl_check_scaled_data {
1547 int depth;
1548 isl_val *m;
1551 /* If constraint "c" involves the input dimension data->depth,
1552 * then make sure that all the other coefficients are multiples of data->m,
1553 * reducing data->m if needed.
1554 * Break out of the iteration if data->m has become equal to "1".
1556 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1557 void *user)
1559 struct isl_check_scaled_data *data = user;
1560 int i, j, n;
1561 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1562 isl_dim_div };
1564 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1565 isl_constraint_free(c);
1566 return isl_stat_ok;
1569 for (i = 0; i < 4; ++i) {
1570 n = isl_constraint_dim(c, t[i]);
1571 for (j = 0; j < n; ++j) {
1572 isl_val *d;
1574 if (t[i] == isl_dim_in && j == data->depth)
1575 continue;
1576 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1577 continue;
1578 d = isl_constraint_get_coefficient_val(c, t[i], j);
1579 data->m = isl_val_gcd(data->m, d);
1580 if (isl_val_is_one(data->m))
1581 break;
1583 if (j < n)
1584 break;
1587 isl_constraint_free(c);
1589 return i < 4 ? isl_stat_error : isl_stat_ok;
1592 /* For each constraint of "bmap" that involves the input dimension data->depth,
1593 * make sure that all the other coefficients are multiples of data->m,
1594 * reducing data->m if needed.
1595 * Break out of the iteration if data->m has become equal to "1".
1597 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1598 void *user)
1600 isl_stat r;
1602 r = isl_basic_map_foreach_constraint(bmap,
1603 &constraint_check_scaled, user);
1604 isl_basic_map_free(bmap);
1606 return r;
1609 /* For each constraint of "map" that involves the input dimension data->depth,
1610 * make sure that all the other coefficients are multiples of data->m,
1611 * reducing data->m if needed.
1612 * Break out of the iteration if data->m has become equal to "1".
1614 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1616 isl_stat r;
1618 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1619 isl_map_free(map);
1621 return r;
1624 /* Create an AST node for the current dimension based on
1625 * the schedule domain "bounds" and return the node encapsulated
1626 * in an isl_ast_graft.
1628 * "executed" is the current inverse schedule, taking into account
1629 * the bounds in "bounds"
1630 * "domain" is the domain of "executed", with inner dimensions projected out.
1633 * Before moving on to the actual AST node construction in create_node_scaled,
1634 * we first check if the current dimension is strided and if we can scale
1635 * down this stride. Note that we only do this if the ast_build_scale_strides
1636 * option is set.
1638 * In particular, let the current dimension take on values
1640 * f + s a
1642 * with a an integer. We check if we can find an integer m that (obviously)
1643 * divides both f and s.
1645 * If so, we check if the current dimension only appears in constraints
1646 * where the coefficients of the other variables are multiples of m.
1647 * We perform this extra check to avoid the risk of introducing
1648 * divisions by scaling down the current dimension.
1650 * If so, we scale the current dimension down by a factor of m.
1651 * That is, we plug in
1653 * i = m i' (1)
1655 * Note that in principle we could always scale down strided loops
1656 * by plugging in
1658 * i = f + s i'
1660 * but this may result in i' taking on larger values than the original i,
1661 * due to the shift by "f".
1662 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1664 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1665 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1666 __isl_take isl_ast_build *build)
1668 struct isl_check_scaled_data data;
1669 isl_ctx *ctx;
1670 isl_aff *offset;
1671 isl_val *d;
1673 ctx = isl_ast_build_get_ctx(build);
1674 if (!isl_options_get_ast_build_scale_strides(ctx))
1675 return create_node_scaled(executed, bounds, domain, build);
1677 data.depth = isl_ast_build_get_depth(build);
1678 if (!isl_ast_build_has_stride(build, data.depth))
1679 return create_node_scaled(executed, bounds, domain, build);
1681 offset = isl_ast_build_get_offset(build, data.depth);
1682 data.m = isl_ast_build_get_stride(build, data.depth);
1683 if (!data.m)
1684 offset = isl_aff_free(offset);
1685 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1686 d = isl_aff_get_denominator_val(offset);
1687 if (!d)
1688 executed = isl_union_map_free(executed);
1690 if (executed && isl_val_is_divisible_by(data.m, d))
1691 data.m = isl_val_div(data.m, d);
1692 else {
1693 data.m = isl_val_set_si(data.m, 1);
1694 isl_val_free(d);
1697 if (!isl_val_is_one(data.m)) {
1698 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1699 &data) < 0 &&
1700 !isl_val_is_one(data.m))
1701 executed = isl_union_map_free(executed);
1704 if (!isl_val_is_one(data.m)) {
1705 isl_space *space;
1706 isl_multi_aff *ma;
1707 isl_aff *aff;
1708 isl_map *map;
1709 isl_union_map *umap;
1711 space = isl_ast_build_get_space(build, 1);
1712 space = isl_space_map_from_set(space);
1713 ma = isl_multi_aff_identity(space);
1714 aff = isl_multi_aff_get_aff(ma, data.depth);
1715 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1716 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1718 bounds = isl_basic_set_preimage_multi_aff(bounds,
1719 isl_multi_aff_copy(ma));
1720 domain = isl_set_preimage_multi_aff(domain,
1721 isl_multi_aff_copy(ma));
1722 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1723 umap = isl_union_map_from_map(map);
1724 executed = isl_union_map_apply_domain(executed,
1725 isl_union_map_copy(umap));
1726 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1727 umap);
1729 isl_aff_free(offset);
1730 isl_val_free(data.m);
1732 return create_node_scaled(executed, bounds, domain, build);
1735 /* Add the basic set to the list that "user" points to.
1737 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1739 isl_basic_set_list **list = user;
1741 *list = isl_basic_set_list_add(*list, bset);
1743 return isl_stat_ok;
1746 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1748 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1749 __isl_take isl_set *set)
1751 int n;
1752 isl_ctx *ctx;
1753 isl_basic_set_list *list;
1755 if (!set)
1756 return NULL;
1758 ctx = isl_set_get_ctx(set);
1760 n = isl_set_n_basic_set(set);
1761 list = isl_basic_set_list_alloc(ctx, n);
1762 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1763 list = isl_basic_set_list_free(list);
1765 isl_set_free(set);
1766 return list;
1769 /* Generate code for the schedule domain "bounds"
1770 * and add the result to "list".
1772 * We mainly detect strides here and check if the bounds do not
1773 * conflict with the current build domain
1774 * and then pass over control to create_node.
1776 * "bounds" reflects the bounds on the current dimension and possibly
1777 * some extra conditions on outer dimensions.
1778 * It does not, however, include any divs involving the current dimension,
1779 * so it does not capture any stride constraints.
1780 * We therefore need to compute that part of the schedule domain that
1781 * intersects with "bounds" and derive the strides from the result.
1783 static __isl_give isl_ast_graft_list *add_node(
1784 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1785 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1787 isl_ast_graft *graft;
1788 isl_set *domain = NULL;
1789 isl_union_set *uset;
1790 int empty, disjoint;
1792 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1793 executed = isl_union_map_intersect_domain(executed, uset);
1794 empty = isl_union_map_is_empty(executed);
1795 if (empty < 0)
1796 goto error;
1797 if (empty)
1798 goto done;
1800 uset = isl_union_map_domain(isl_union_map_copy(executed));
1801 domain = isl_set_from_union_set(uset);
1802 domain = isl_ast_build_specialize(build, domain);
1804 domain = isl_set_compute_divs(domain);
1805 domain = isl_ast_build_eliminate_inner(build, domain);
1806 disjoint = isl_set_is_disjoint(domain, build->domain);
1807 if (disjoint < 0)
1808 goto error;
1809 if (disjoint)
1810 goto done;
1812 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1814 graft = create_node(executed, bounds, domain,
1815 isl_ast_build_copy(build));
1816 list = isl_ast_graft_list_add(list, graft);
1817 isl_ast_build_free(build);
1818 return list;
1819 error:
1820 list = isl_ast_graft_list_free(list);
1821 done:
1822 isl_set_free(domain);
1823 isl_basic_set_free(bounds);
1824 isl_union_map_free(executed);
1825 isl_ast_build_free(build);
1826 return list;
1829 /* Does any element of i follow or coincide with any element of j
1830 * at the current depth for equal values of the outer dimensions?
1832 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1833 __isl_keep isl_basic_set *j, void *user)
1835 int depth = *(int *) user;
1836 isl_basic_map *test;
1837 isl_bool empty;
1838 int l;
1840 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1841 isl_basic_set_copy(j));
1842 for (l = 0; l < depth; ++l)
1843 test = isl_basic_map_equate(test, isl_dim_in, l,
1844 isl_dim_out, l);
1845 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1846 isl_dim_out, depth);
1847 empty = isl_basic_map_is_empty(test);
1848 isl_basic_map_free(test);
1850 return empty < 0 ? isl_bool_error : !empty;
1853 /* Split up each element of "list" into a part that is related to "bset"
1854 * according to "gt" and a part that is not.
1855 * Return a list that consist of "bset" and all the pieces.
1857 static __isl_give isl_basic_set_list *add_split_on(
1858 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1859 __isl_keep isl_basic_map *gt)
1861 int i, n;
1862 isl_basic_set_list *res;
1864 if (!list)
1865 bset = isl_basic_set_free(bset);
1867 gt = isl_basic_map_copy(gt);
1868 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1869 n = isl_basic_set_list_n_basic_set(list);
1870 res = isl_basic_set_list_from_basic_set(bset);
1871 for (i = 0; res && i < n; ++i) {
1872 isl_basic_set *bset;
1873 isl_set *set1, *set2;
1874 isl_basic_map *bmap;
1875 int empty;
1877 bset = isl_basic_set_list_get_basic_set(list, i);
1878 bmap = isl_basic_map_copy(gt);
1879 bmap = isl_basic_map_intersect_range(bmap, bset);
1880 bset = isl_basic_map_range(bmap);
1881 empty = isl_basic_set_is_empty(bset);
1882 if (empty < 0)
1883 res = isl_basic_set_list_free(res);
1884 if (empty) {
1885 isl_basic_set_free(bset);
1886 bset = isl_basic_set_list_get_basic_set(list, i);
1887 res = isl_basic_set_list_add(res, bset);
1888 continue;
1891 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1892 set1 = isl_set_from_basic_set(bset);
1893 bset = isl_basic_set_list_get_basic_set(list, i);
1894 set2 = isl_set_from_basic_set(bset);
1895 set1 = isl_set_subtract(set2, set1);
1896 set1 = isl_set_make_disjoint(set1);
1898 res = isl_basic_set_list_concat(res,
1899 isl_basic_set_list_from_set(set1));
1901 isl_basic_map_free(gt);
1902 isl_basic_set_list_free(list);
1903 return res;
1906 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1907 __isl_keep isl_basic_set_list *domain_list,
1908 __isl_keep isl_union_map *executed,
1909 __isl_keep isl_ast_build *build);
1911 /* Internal data structure for add_nodes.
1913 * "executed" and "build" are extra arguments to be passed to add_node.
1914 * "list" collects the results.
1916 struct isl_add_nodes_data {
1917 isl_union_map *executed;
1918 isl_ast_build *build;
1920 isl_ast_graft_list *list;
1923 /* Generate code for the schedule domains in "scc"
1924 * and add the results to "list".
1926 * The domains in "scc" form a strongly connected component in the ordering.
1927 * If the number of domains in "scc" is larger than 1, then this means
1928 * that we cannot determine a valid ordering for the domains in the component.
1929 * This should be fairly rare because the individual domains
1930 * have been made disjoint first.
1931 * The problem is that the domains may be integrally disjoint but not
1932 * rationally disjoint. For example, we may have domains
1934 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1936 * These two domains have an empty intersection, but their rational
1937 * relaxations do intersect. It is impossible to order these domains
1938 * in the second dimension because the first should be ordered before
1939 * the second for outer dimension equal to 0, while it should be ordered
1940 * after for outer dimension equal to 1.
1942 * This may happen in particular in case of unrolling since the domain
1943 * of each slice is replaced by its simple hull.
1945 * For each basic set i in "scc" and for each of the following basic sets j,
1946 * we split off that part of the basic set i that shares the outer dimensions
1947 * with j and lies before j in the current dimension.
1948 * We collect all the pieces in a new list that replaces "scc".
1950 * While the elements in "scc" should be disjoint, we double-check
1951 * this property to avoid running into an infinite recursion in case
1952 * they intersect due to some internal error.
1954 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1956 struct isl_add_nodes_data *data = user;
1957 int i, n, depth;
1958 isl_basic_set *bset, *first;
1959 isl_basic_set_list *list;
1960 isl_space *space;
1961 isl_basic_map *gt;
1963 n = isl_basic_set_list_n_basic_set(scc);
1964 bset = isl_basic_set_list_get_basic_set(scc, 0);
1965 if (n == 1) {
1966 isl_basic_set_list_free(scc);
1967 data->list = add_node(data->list,
1968 isl_union_map_copy(data->executed), bset,
1969 isl_ast_build_copy(data->build));
1970 return data->list ? isl_stat_ok : isl_stat_error;
1973 depth = isl_ast_build_get_depth(data->build);
1974 space = isl_basic_set_get_space(bset);
1975 space = isl_space_map_from_set(space);
1976 gt = isl_basic_map_universe(space);
1977 for (i = 0; i < depth; ++i)
1978 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1979 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1981 first = isl_basic_set_copy(bset);
1982 list = isl_basic_set_list_from_basic_set(bset);
1983 for (i = 1; i < n; ++i) {
1984 int disjoint;
1986 bset = isl_basic_set_list_get_basic_set(scc, i);
1988 disjoint = isl_basic_set_is_disjoint(bset, first);
1989 if (disjoint < 0)
1990 list = isl_basic_set_list_free(list);
1991 else if (!disjoint)
1992 isl_die(isl_basic_set_list_get_ctx(scc),
1993 isl_error_internal,
1994 "basic sets in scc are assumed to be disjoint",
1995 list = isl_basic_set_list_free(list));
1997 list = add_split_on(list, bset, gt);
1999 isl_basic_set_free(first);
2000 isl_basic_map_free(gt);
2001 isl_basic_set_list_free(scc);
2002 scc = list;
2003 data->list = isl_ast_graft_list_concat(data->list,
2004 generate_sorted_domains(scc, data->executed, data->build));
2005 isl_basic_set_list_free(scc);
2007 return data->list ? isl_stat_ok : isl_stat_error;
2010 /* Sort the domains in "domain_list" according to the execution order
2011 * at the current depth (for equal values of the outer dimensions),
2012 * generate code for each of them, collecting the results in a list.
2013 * If no code is generated (because the intersection of the inverse schedule
2014 * with the domains turns out to be empty), then an empty list is returned.
2016 * The caller is responsible for ensuring that the basic sets in "domain_list"
2017 * are pair-wise disjoint. It can, however, in principle happen that
2018 * two basic sets should be ordered one way for one value of the outer
2019 * dimensions and the other way for some other value of the outer dimensions.
2020 * We therefore play safe and look for strongly connected components.
2021 * The function add_nodes takes care of handling non-trivial components.
2023 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2024 __isl_keep isl_basic_set_list *domain_list,
2025 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2027 isl_ctx *ctx;
2028 struct isl_add_nodes_data data;
2029 int depth;
2030 int n;
2032 if (!domain_list)
2033 return NULL;
2035 ctx = isl_basic_set_list_get_ctx(domain_list);
2036 n = isl_basic_set_list_n_basic_set(domain_list);
2037 data.list = isl_ast_graft_list_alloc(ctx, n);
2038 if (n == 0)
2039 return data.list;
2040 if (n == 1)
2041 return add_node(data.list, isl_union_map_copy(executed),
2042 isl_basic_set_list_get_basic_set(domain_list, 0),
2043 isl_ast_build_copy(build));
2045 depth = isl_ast_build_get_depth(build);
2046 data.executed = executed;
2047 data.build = build;
2048 if (isl_basic_set_list_foreach_scc(domain_list,
2049 &domain_follows_at_depth, &depth,
2050 &add_nodes, &data) < 0)
2051 data.list = isl_ast_graft_list_free(data.list);
2053 return data.list;
2056 /* Do i and j share any values for the outer dimensions?
2058 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2059 __isl_keep isl_basic_set *j, void *user)
2061 int depth = *(int *) user;
2062 isl_basic_map *test;
2063 isl_bool empty;
2064 int l;
2066 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2067 isl_basic_set_copy(j));
2068 for (l = 0; l < depth; ++l)
2069 test = isl_basic_map_equate(test, isl_dim_in, l,
2070 isl_dim_out, l);
2071 empty = isl_basic_map_is_empty(test);
2072 isl_basic_map_free(test);
2074 return empty < 0 ? isl_bool_error : !empty;
2077 /* Internal data structure for generate_sorted_domains_wrap.
2079 * "n" is the total number of basic sets
2080 * "executed" and "build" are extra arguments to be passed
2081 * to generate_sorted_domains.
2083 * "single" is set to 1 by generate_sorted_domains_wrap if there
2084 * is only a single component.
2085 * "list" collects the results.
2087 struct isl_ast_generate_parallel_domains_data {
2088 int n;
2089 isl_union_map *executed;
2090 isl_ast_build *build;
2092 int single;
2093 isl_ast_graft_list *list;
2096 /* Call generate_sorted_domains on "scc", fuse the result into a list
2097 * with either zero or one graft and collect the these single element
2098 * lists into data->list.
2100 * If there is only one component, i.e., if the number of basic sets
2101 * in the current component is equal to the total number of basic sets,
2102 * then data->single is set to 1 and the result of generate_sorted_domains
2103 * is not fused.
2105 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2106 void *user)
2108 struct isl_ast_generate_parallel_domains_data *data = user;
2109 isl_ast_graft_list *list;
2111 list = generate_sorted_domains(scc, data->executed, data->build);
2112 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2113 if (!data->single)
2114 list = isl_ast_graft_list_fuse(list, data->build);
2115 if (!data->list)
2116 data->list = list;
2117 else
2118 data->list = isl_ast_graft_list_concat(data->list, list);
2120 isl_basic_set_list_free(scc);
2121 if (!data->list)
2122 return isl_stat_error;
2124 return isl_stat_ok;
2127 /* Look for any (weakly connected) components in the "domain_list"
2128 * of domains that share some values of the outer dimensions.
2129 * That is, domains in different components do not share any values
2130 * of the outer dimensions. This means that these components
2131 * can be freely reordered.
2132 * Within each of the components, we sort the domains according
2133 * to the execution order at the current depth.
2135 * If there is more than one component, then generate_sorted_domains_wrap
2136 * fuses the result of each call to generate_sorted_domains
2137 * into a list with either zero or one graft and collects these (at most)
2138 * single element lists into a bigger list. This means that the elements of the
2139 * final list can be freely reordered. In particular, we sort them
2140 * according to an arbitrary but fixed ordering to ease merging of
2141 * graft lists from different components.
2143 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2144 __isl_keep isl_basic_set_list *domain_list,
2145 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2147 int depth;
2148 struct isl_ast_generate_parallel_domains_data data;
2150 if (!domain_list)
2151 return NULL;
2153 data.n = isl_basic_set_list_n_basic_set(domain_list);
2154 if (data.n <= 1)
2155 return generate_sorted_domains(domain_list, executed, build);
2157 depth = isl_ast_build_get_depth(build);
2158 data.list = NULL;
2159 data.executed = executed;
2160 data.build = build;
2161 data.single = 0;
2162 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2163 &generate_sorted_domains_wrap,
2164 &data) < 0)
2165 data.list = isl_ast_graft_list_free(data.list);
2167 if (!data.single)
2168 data.list = isl_ast_graft_list_sort_guard(data.list);
2170 return data.list;
2173 /* Internal data for separate_domain.
2175 * "explicit" is set if we only want to use explicit bounds.
2177 * "domain" collects the separated domains.
2179 struct isl_separate_domain_data {
2180 isl_ast_build *build;
2181 int explicit;
2182 isl_set *domain;
2185 /* Extract implicit bounds on the current dimension for the executed "map".
2187 * The domain of "map" may involve inner dimensions, so we
2188 * need to eliminate them.
2190 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2191 __isl_keep isl_ast_build *build)
2193 isl_set *domain;
2195 domain = isl_map_domain(map);
2196 domain = isl_ast_build_eliminate(build, domain);
2198 return domain;
2201 /* Extract explicit bounds on the current dimension for the executed "map".
2203 * Rather than eliminating the inner dimensions as in implicit_bounds,
2204 * we simply drop any constraints involving those inner dimensions.
2205 * The idea is that most bounds that are implied by constraints on the
2206 * inner dimensions will be enforced by for loops and not by explicit guards.
2207 * There is then no need to separate along those bounds.
2209 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2210 __isl_keep isl_ast_build *build)
2212 isl_set *domain;
2213 int depth, dim;
2215 dim = isl_map_dim(map, isl_dim_out);
2216 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2218 domain = isl_map_domain(map);
2219 depth = isl_ast_build_get_depth(build);
2220 dim = isl_set_dim(domain, isl_dim_set);
2221 domain = isl_set_detect_equalities(domain);
2222 domain = isl_set_drop_constraints_involving_dims(domain,
2223 isl_dim_set, depth + 1, dim - (depth + 1));
2224 domain = isl_set_remove_divs_involving_dims(domain,
2225 isl_dim_set, depth, 1);
2226 domain = isl_set_remove_unknown_divs(domain);
2228 return domain;
2231 /* Split data->domain into pieces that intersect with the range of "map"
2232 * and pieces that do not intersect with the range of "map"
2233 * and then add that part of the range of "map" that does not intersect
2234 * with data->domain.
2236 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2238 struct isl_separate_domain_data *data = user;
2239 isl_set *domain;
2240 isl_set *d1, *d2;
2242 if (data->explicit)
2243 domain = explicit_bounds(map, data->build);
2244 else
2245 domain = implicit_bounds(map, data->build);
2247 domain = isl_set_coalesce(domain);
2248 domain = isl_set_make_disjoint(domain);
2249 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2250 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2251 data->domain = isl_set_intersect(data->domain, domain);
2252 data->domain = isl_set_union(data->domain, d1);
2253 data->domain = isl_set_union(data->domain, d2);
2255 return isl_stat_ok;
2258 /* Separate the schedule domains of "executed".
2260 * That is, break up the domain of "executed" into basic sets,
2261 * such that for each basic set S, every element in S is associated with
2262 * the same domain spaces.
2264 * "space" is the (single) domain space of "executed".
2266 static __isl_give isl_set *separate_schedule_domains(
2267 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2268 __isl_keep isl_ast_build *build)
2270 struct isl_separate_domain_data data = { build };
2271 isl_ctx *ctx;
2273 ctx = isl_ast_build_get_ctx(build);
2274 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2275 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2276 data.domain = isl_set_empty(space);
2277 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2278 data.domain = isl_set_free(data.domain);
2280 isl_union_map_free(executed);
2281 return data.domain;
2284 /* Temporary data used during the search for a lower bound for unrolling.
2286 * "build" is the build in which the unrolling will be performed
2287 * "domain" is the original set for which to find a lower bound
2288 * "depth" is the dimension for which to find a lower boudn
2289 * "expansion" is the expansion that needs to be applied to "domain"
2290 * in the unrolling that will be performed
2292 * "lower" is the best lower bound found so far. It is NULL if we have not
2293 * found any yet.
2294 * "n" is the corresponding size. If lower is NULL, then the value of n
2295 * is undefined.
2296 * "n_div" is the maximal number of integer divisions in the first
2297 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2298 * been computed yet.
2300 struct isl_find_unroll_data {
2301 isl_ast_build *build;
2302 isl_set *domain;
2303 int depth;
2304 isl_basic_map *expansion;
2306 isl_aff *lower;
2307 int *n;
2308 int n_div;
2311 /* Return the constraint
2313 * i_"depth" = aff + offset
2315 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2316 int offset)
2318 aff = isl_aff_copy(aff);
2319 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2320 aff = isl_aff_add_constant_si(aff, offset);
2321 return isl_equality_from_aff(aff);
2324 /* Update *user to the number of integer divsions in the first element
2325 * of "ma", if it is larger than the current value.
2327 static isl_stat update_n_div(__isl_take isl_set *set,
2328 __isl_take isl_multi_aff *ma, void *user)
2330 isl_aff *aff;
2331 int *n = user;
2332 int n_div;
2334 aff = isl_multi_aff_get_aff(ma, 0);
2335 n_div = isl_aff_dim(aff, isl_dim_div);
2336 isl_aff_free(aff);
2337 isl_multi_aff_free(ma);
2338 isl_set_free(set);
2340 if (n_div > *n)
2341 *n = n_div;
2343 return aff ? isl_stat_ok : isl_stat_error;
2346 /* Get the number of integer divisions in the expression for the iterator
2347 * value at the first slice in the unrolling based on lower bound "lower",
2348 * taking into account the expansion that needs to be performed on this slice.
2350 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2351 __isl_keep isl_aff *lower)
2353 isl_constraint *c;
2354 isl_set *set;
2355 isl_map *it_map, *expansion;
2356 isl_pw_multi_aff *pma;
2357 int n;
2359 c = at_offset(data->depth, lower, 0);
2360 set = isl_set_copy(data->domain);
2361 set = isl_set_add_constraint(set, c);
2362 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2363 set = isl_set_apply(set, expansion);
2364 it_map = isl_ast_build_map_to_iterator(data->build, set);
2365 pma = isl_pw_multi_aff_from_map(it_map);
2366 n = 0;
2367 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2368 n = -1;
2369 isl_pw_multi_aff_free(pma);
2371 return n;
2374 /* Is the lower bound "lower" with corresponding iteration count "n"
2375 * better than the one stored in "data"?
2376 * If there is no upper bound on the iteration count ("n" is infinity) or
2377 * if the count is too large, then we cannot use this lower bound.
2378 * Otherwise, if there was no previous lower bound or
2379 * if the iteration count of the new lower bound is smaller than
2380 * the iteration count of the previous lower bound, then we consider
2381 * the new lower bound to be better.
2382 * If the iteration count is the same, then compare the number
2383 * of integer divisions that would be needed to express
2384 * the iterator value at the first slice in the unrolling
2385 * according to the lower bound. If we end up computing this
2386 * number, then store the lowest value in data->n_div.
2388 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2389 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2391 int cmp;
2392 int n_div;
2394 if (!n)
2395 return -1;
2396 if (isl_val_is_infty(n))
2397 return 0;
2398 if (isl_val_cmp_si(n, INT_MAX) > 0)
2399 return 0;
2400 if (!data->lower)
2401 return 1;
2402 cmp = isl_val_cmp_si(n, *data->n);
2403 if (cmp < 0)
2404 return 1;
2405 if (cmp > 0)
2406 return 0;
2407 if (data->n_div < 0)
2408 data->n_div = get_expanded_n_div(data, data->lower);
2409 if (data->n_div < 0)
2410 return -1;
2411 if (data->n_div == 0)
2412 return 0;
2413 n_div = get_expanded_n_div(data, lower);
2414 if (n_div < 0)
2415 return -1;
2416 if (n_div >= data->n_div)
2417 return 0;
2418 data->n_div = n_div;
2420 return 1;
2423 /* Check if we can use "c" as a lower bound and if it is better than
2424 * any previously found lower bound.
2426 * If "c" does not involve the dimension at the current depth,
2427 * then we cannot use it.
2428 * Otherwise, let "c" be of the form
2430 * i >= f(j)/a
2432 * We compute the maximal value of
2434 * -ceil(f(j)/a)) + i + 1
2436 * over the domain. If there is such a value "n", then we know
2438 * -ceil(f(j)/a)) + i + 1 <= n
2440 * or
2442 * i < ceil(f(j)/a)) + n
2444 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2445 * We just need to check if we have found any lower bound before and
2446 * if the new lower bound is better (smaller n or fewer integer divisions)
2447 * than the previously found lower bounds.
2449 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2450 __isl_keep isl_constraint *c)
2452 isl_aff *aff, *lower;
2453 isl_val *max;
2454 int better;
2456 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2457 return isl_stat_ok;
2459 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2460 lower = isl_aff_ceil(lower);
2461 aff = isl_aff_copy(lower);
2462 aff = isl_aff_neg(aff);
2463 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2464 aff = isl_aff_add_constant_si(aff, 1);
2465 max = isl_set_max_val(data->domain, aff);
2466 isl_aff_free(aff);
2468 better = is_better_lower_bound(data, lower, max);
2469 if (better < 0 || !better) {
2470 isl_val_free(max);
2471 isl_aff_free(lower);
2472 return better < 0 ? isl_stat_error : isl_stat_ok;
2475 isl_aff_free(data->lower);
2476 data->lower = lower;
2477 *data->n = isl_val_get_num_si(max);
2478 isl_val_free(max);
2480 return isl_stat_ok;
2483 /* Check if we can use "c" as a lower bound and if it is better than
2484 * any previously found lower bound.
2486 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2488 struct isl_find_unroll_data *data;
2489 isl_stat r;
2491 data = (struct isl_find_unroll_data *) user;
2492 r = update_unrolling_lower_bound(data, c);
2493 isl_constraint_free(c);
2495 return r;
2498 /* Look for a lower bound l(i) on the dimension at "depth"
2499 * and a size n such that "domain" is a subset of
2501 * { [i] : l(i) <= i_d < l(i) + n }
2503 * where d is "depth" and l(i) depends only on earlier dimensions.
2504 * Furthermore, try and find a lower bound such that n is as small as possible.
2505 * In particular, "n" needs to be finite.
2506 * "build" is the build in which the unrolling will be performed.
2507 * "expansion" is the expansion that needs to be applied to "domain"
2508 * in the unrolling that will be performed.
2510 * Inner dimensions have been eliminated from "domain" by the caller.
2512 * We first construct a collection of lower bounds on the input set
2513 * by computing its simple hull. We then iterate through them,
2514 * discarding those that we cannot use (either because they do not
2515 * involve the dimension at "depth" or because they have no corresponding
2516 * upper bound, meaning that "n" would be unbounded) and pick out the
2517 * best from the remaining ones.
2519 * If we cannot find a suitable lower bound, then we consider that
2520 * to be an error.
2522 static __isl_give isl_aff *find_unroll_lower_bound(
2523 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2524 int depth, __isl_keep isl_basic_map *expansion, int *n)
2526 struct isl_find_unroll_data data =
2527 { build, domain, depth, expansion, NULL, n, -1 };
2528 isl_basic_set *hull;
2530 hull = isl_set_simple_hull(isl_set_copy(domain));
2532 if (isl_basic_set_foreach_constraint(hull,
2533 &constraint_find_unroll, &data) < 0)
2534 goto error;
2536 isl_basic_set_free(hull);
2538 if (!data.lower)
2539 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2540 "cannot find lower bound for unrolling", return NULL);
2542 return data.lower;
2543 error:
2544 isl_basic_set_free(hull);
2545 return isl_aff_free(data.lower);
2548 /* Call "fn" on each iteration of the current dimension of "domain".
2549 * If "init" is not NULL, then it is called with the number of
2550 * iterations before any call to "fn".
2551 * Return -1 on failure.
2553 * Since we are going to be iterating over the individual values,
2554 * we first check if there are any strides on the current dimension.
2555 * If there is, we rewrite the current dimension i as
2557 * i = stride i' + offset
2559 * and then iterate over individual values of i' instead.
2561 * We then look for a lower bound on i' and a size such that the domain
2562 * is a subset of
2564 * { [j,i'] : l(j) <= i' < l(j) + n }
2566 * and then take slices of the domain at values of i'
2567 * between l(j) and l(j) + n - 1.
2569 * We compute the unshifted simple hull of each slice to ensure that
2570 * we have a single basic set per offset. The slicing constraint
2571 * may get simplified away before the unshifted simple hull is taken
2572 * and may therefore in some rare cases disappear from the result.
2573 * We therefore explicitly add the constraint back after computing
2574 * the unshifted simple hull to ensure that the basic sets
2575 * remain disjoint. The constraints that are dropped by taking the hull
2576 * will be taken into account at the next level, as in the case of the
2577 * atomic option.
2579 * Finally, we map i' back to i and call "fn".
2581 static int foreach_iteration(__isl_take isl_set *domain,
2582 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2583 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2585 int i, n;
2586 int empty;
2587 int depth;
2588 isl_multi_aff *expansion;
2589 isl_basic_map *bmap;
2590 isl_aff *lower = NULL;
2591 isl_ast_build *stride_build;
2593 depth = isl_ast_build_get_depth(build);
2595 domain = isl_ast_build_eliminate_inner(build, domain);
2596 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2597 stride_build = isl_ast_build_copy(build);
2598 stride_build = isl_ast_build_detect_strides(stride_build,
2599 isl_set_copy(domain));
2600 expansion = isl_ast_build_get_stride_expansion(stride_build);
2602 domain = isl_set_preimage_multi_aff(domain,
2603 isl_multi_aff_copy(expansion));
2604 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2605 isl_ast_build_free(stride_build);
2607 bmap = isl_basic_map_from_multi_aff(expansion);
2609 empty = isl_set_is_empty(domain);
2610 if (empty < 0) {
2611 n = -1;
2612 } else if (empty) {
2613 n = 0;
2614 } else {
2615 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2616 if (!lower)
2617 n = -1;
2619 if (n >= 0 && init && init(n, user) < 0)
2620 n = -1;
2621 for (i = 0; i < n; ++i) {
2622 isl_set *set;
2623 isl_basic_set *bset;
2624 isl_constraint *slice;
2626 slice = at_offset(depth, lower, i);
2627 set = isl_set_copy(domain);
2628 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2629 bset = isl_set_unshifted_simple_hull(set);
2630 bset = isl_basic_set_add_constraint(bset, slice);
2631 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2633 if (fn(bset, user) < 0)
2634 break;
2637 isl_aff_free(lower);
2638 isl_set_free(domain);
2639 isl_basic_map_free(bmap);
2641 return n < 0 || i < n ? -1 : 0;
2644 /* Data structure for storing the results and the intermediate objects
2645 * of compute_domains.
2647 * "list" is the main result of the function and contains a list
2648 * of disjoint basic sets for which code should be generated.
2650 * "executed" and "build" are inputs to compute_domains.
2651 * "schedule_domain" is the domain of "executed".
2653 * "option" constains the domains at the current depth that should by
2654 * atomic, separated or unrolled. These domains are as specified by
2655 * the user, except that inner dimensions have been eliminated and
2656 * that they have been made pair-wise disjoint.
2658 * "sep_class" contains the user-specified split into separation classes
2659 * specialized to the current depth.
2660 * "done" contains the union of the separation domains that have already
2661 * been handled.
2663 struct isl_codegen_domains {
2664 isl_basic_set_list *list;
2666 isl_union_map *executed;
2667 isl_ast_build *build;
2668 isl_set *schedule_domain;
2670 isl_set *option[4];
2672 isl_map *sep_class;
2673 isl_set *done;
2676 /* Internal data structure for do_unroll.
2678 * "domains" stores the results of compute_domains.
2679 * "class_domain" is the original class domain passed to do_unroll.
2680 * "unroll_domain" collects the unrolled iterations.
2682 struct isl_ast_unroll_data {
2683 struct isl_codegen_domains *domains;
2684 isl_set *class_domain;
2685 isl_set *unroll_domain;
2688 /* Given an iteration of an unrolled domain represented by "bset",
2689 * add it to data->domains->list.
2690 * Since we may have dropped some constraints, we intersect with
2691 * the class domain again to ensure that each element in the list
2692 * is disjoint from the other class domains.
2694 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2696 struct isl_ast_unroll_data *data = user;
2697 isl_set *set;
2698 isl_basic_set_list *list;
2700 set = isl_set_from_basic_set(bset);
2701 data->unroll_domain = isl_set_union(data->unroll_domain,
2702 isl_set_copy(set));
2703 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2704 set = isl_set_make_disjoint(set);
2705 list = isl_basic_set_list_from_set(set);
2706 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2707 list);
2709 return 0;
2712 /* Extend domains->list with a list of basic sets, one for each value
2713 * of the current dimension in "domain" and remove the corresponding
2714 * sets from the class domain. Return the updated class domain.
2715 * The divs that involve the current dimension have not been projected out
2716 * from this domain.
2718 * We call foreach_iteration to iterate over the individual values and
2719 * in do_unroll_iteration we collect the individual basic sets in
2720 * domains->list and their union in data->unroll_domain, which is then
2721 * used to update the class domain.
2723 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2724 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2726 struct isl_ast_unroll_data data;
2728 if (!domain)
2729 return isl_set_free(class_domain);
2730 if (!class_domain)
2731 return isl_set_free(domain);
2733 data.domains = domains;
2734 data.class_domain = class_domain;
2735 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2737 if (foreach_iteration(domain, domains->build, NULL,
2738 &do_unroll_iteration, &data) < 0)
2739 data.unroll_domain = isl_set_free(data.unroll_domain);
2741 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2743 return class_domain;
2746 /* Add domains to domains->list for each individual value of the current
2747 * dimension, for that part of the schedule domain that lies in the
2748 * intersection of the option domain and the class domain.
2749 * Remove the corresponding sets from the class domain and
2750 * return the updated class domain.
2752 * We first break up the unroll option domain into individual pieces
2753 * and then handle each of them separately. The unroll option domain
2754 * has been made disjoint in compute_domains_init_options,
2756 * Note that we actively want to combine different pieces of the
2757 * schedule domain that have the same value at the current dimension.
2758 * We therefore need to break up the unroll option domain before
2759 * intersecting with class and schedule domain, hoping that the
2760 * unroll option domain specified by the user is relatively simple.
2762 static __isl_give isl_set *compute_unroll_domains(
2763 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2765 isl_set *unroll_domain;
2766 isl_basic_set_list *unroll_list;
2767 int i, n;
2768 int empty;
2770 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2771 if (empty < 0)
2772 return isl_set_free(class_domain);
2773 if (empty)
2774 return class_domain;
2776 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2777 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2779 n = isl_basic_set_list_n_basic_set(unroll_list);
2780 for (i = 0; i < n; ++i) {
2781 isl_basic_set *bset;
2783 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2784 unroll_domain = isl_set_from_basic_set(bset);
2785 unroll_domain = isl_set_intersect(unroll_domain,
2786 isl_set_copy(class_domain));
2787 unroll_domain = isl_set_intersect(unroll_domain,
2788 isl_set_copy(domains->schedule_domain));
2790 empty = isl_set_is_empty(unroll_domain);
2791 if (empty >= 0 && empty) {
2792 isl_set_free(unroll_domain);
2793 continue;
2796 class_domain = do_unroll(domains, unroll_domain, class_domain);
2799 isl_basic_set_list_free(unroll_list);
2801 return class_domain;
2804 /* Try and construct a single basic set that includes the intersection of
2805 * the schedule domain, the atomic option domain and the class domain.
2806 * Add the resulting basic set(s) to domains->list and remove them
2807 * from class_domain. Return the updated class domain.
2809 * We construct a single domain rather than trying to combine
2810 * the schedule domains of individual domains because we are working
2811 * within a single component so that non-overlapping schedule domains
2812 * should already have been separated.
2813 * We do however need to make sure that this single domains is a subset
2814 * of the class domain so that it would not intersect with any other
2815 * class domains. This means that we may end up splitting up the atomic
2816 * domain in case separation classes are being used.
2818 * "domain" is the intersection of the schedule domain and the class domain,
2819 * with inner dimensions projected out.
2821 static __isl_give isl_set *compute_atomic_domain(
2822 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2824 isl_basic_set *bset;
2825 isl_basic_set_list *list;
2826 isl_set *domain, *atomic_domain;
2827 int empty;
2829 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2830 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2831 domain = isl_set_intersect(domain,
2832 isl_set_copy(domains->schedule_domain));
2833 empty = isl_set_is_empty(domain);
2834 if (empty < 0)
2835 class_domain = isl_set_free(class_domain);
2836 if (empty) {
2837 isl_set_free(domain);
2838 return class_domain;
2841 domain = isl_ast_build_eliminate(domains->build, domain);
2842 domain = isl_set_coalesce(domain);
2843 bset = isl_set_unshifted_simple_hull(domain);
2844 domain = isl_set_from_basic_set(bset);
2845 atomic_domain = isl_set_copy(domain);
2846 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2847 class_domain = isl_set_subtract(class_domain, atomic_domain);
2848 domain = isl_set_make_disjoint(domain);
2849 list = isl_basic_set_list_from_set(domain);
2850 domains->list = isl_basic_set_list_concat(domains->list, list);
2852 return class_domain;
2855 /* Split up the schedule domain into uniform basic sets,
2856 * in the sense that each element in a basic set is associated to
2857 * elements of the same domains, and add the result to domains->list.
2858 * Do this for that part of the schedule domain that lies in the
2859 * intersection of "class_domain" and the separate option domain.
2861 * "class_domain" may or may not include the constraints
2862 * of the schedule domain, but this does not make a difference
2863 * since we are going to intersect it with the domain of the inverse schedule.
2864 * If it includes schedule domain constraints, then they may involve
2865 * inner dimensions, but we will eliminate them in separation_domain.
2867 static int compute_separate_domain(struct isl_codegen_domains *domains,
2868 __isl_keep isl_set *class_domain)
2870 isl_space *space;
2871 isl_set *domain;
2872 isl_union_map *executed;
2873 isl_basic_set_list *list;
2874 int empty;
2876 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2877 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2878 executed = isl_union_map_copy(domains->executed);
2879 executed = isl_union_map_intersect_domain(executed,
2880 isl_union_set_from_set(domain));
2881 empty = isl_union_map_is_empty(executed);
2882 if (empty < 0 || empty) {
2883 isl_union_map_free(executed);
2884 return empty < 0 ? -1 : 0;
2887 space = isl_set_get_space(class_domain);
2888 domain = separate_schedule_domains(space, executed, domains->build);
2890 list = isl_basic_set_list_from_set(domain);
2891 domains->list = isl_basic_set_list_concat(domains->list, list);
2893 return 0;
2896 /* Split up the domain at the current depth into disjoint
2897 * basic sets for which code should be generated separately
2898 * for the given separation class domain.
2900 * If any separation classes have been defined, then "class_domain"
2901 * is the domain of the current class and does not refer to inner dimensions.
2902 * Otherwise, "class_domain" is the universe domain.
2904 * We first make sure that the class domain is disjoint from
2905 * previously considered class domains.
2907 * The separate domains can be computed directly from the "class_domain".
2909 * The unroll, atomic and remainder domains need the constraints
2910 * from the schedule domain.
2912 * For unrolling, the actual schedule domain is needed (with divs that
2913 * may refer to the current dimension) so that stride detection can be
2914 * performed.
2916 * For atomic and remainder domains, inner dimensions and divs involving
2917 * the current dimensions should be eliminated.
2918 * In case we are working within a separation class, we need to intersect
2919 * the result with the current "class_domain" to ensure that the domains
2920 * are disjoint from those generated from other class domains.
2922 * The domain that has been made atomic may be larger than specified
2923 * by the user since it needs to be representable as a single basic set.
2924 * This possibly larger domain is removed from class_domain by
2925 * compute_atomic_domain. It is computed first so that the extended domain
2926 * would not overlap with any domains computed before.
2927 * Similary, the unrolled domains may have some constraints removed and
2928 * may therefore also be larger than specified by the user.
2930 * If anything is left after handling separate, unroll and atomic,
2931 * we split it up into basic sets and append the basic sets to domains->list.
2933 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2934 __isl_take isl_set *class_domain)
2936 isl_basic_set_list *list;
2937 isl_set *domain;
2939 class_domain = isl_set_subtract(class_domain,
2940 isl_set_copy(domains->done));
2941 domains->done = isl_set_union(domains->done,
2942 isl_set_copy(class_domain));
2944 class_domain = compute_atomic_domain(domains, class_domain);
2945 class_domain = compute_unroll_domains(domains, class_domain);
2947 domain = isl_set_copy(class_domain);
2949 if (compute_separate_domain(domains, domain) < 0)
2950 goto error;
2951 domain = isl_set_subtract(domain,
2952 isl_set_copy(domains->option[isl_ast_loop_separate]));
2954 domain = isl_set_intersect(domain,
2955 isl_set_copy(domains->schedule_domain));
2957 domain = isl_ast_build_eliminate(domains->build, domain);
2958 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2960 domain = isl_set_coalesce(domain);
2961 domain = isl_set_make_disjoint(domain);
2963 list = isl_basic_set_list_from_set(domain);
2964 domains->list = isl_basic_set_list_concat(domains->list, list);
2966 isl_set_free(class_domain);
2968 return isl_stat_ok;
2969 error:
2970 isl_set_free(domain);
2971 isl_set_free(class_domain);
2972 return isl_stat_error;
2975 /* Split up the domain at the current depth into disjoint
2976 * basic sets for which code should be generated separately
2977 * for the separation class identified by "pnt".
2979 * We extract the corresponding class domain from domains->sep_class,
2980 * eliminate inner dimensions and pass control to compute_partial_domains.
2982 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2984 struct isl_codegen_domains *domains = user;
2985 isl_set *class_set;
2986 isl_set *domain;
2987 int disjoint;
2989 class_set = isl_set_from_point(pnt);
2990 domain = isl_map_domain(isl_map_intersect_range(
2991 isl_map_copy(domains->sep_class), class_set));
2992 domain = isl_ast_build_compute_gist(domains->build, domain);
2993 domain = isl_ast_build_eliminate(domains->build, domain);
2995 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2996 if (disjoint < 0)
2997 return isl_stat_error;
2998 if (disjoint) {
2999 isl_set_free(domain);
3000 return isl_stat_ok;
3003 return compute_partial_domains(domains, domain);
3006 /* Extract the domains at the current depth that should be atomic,
3007 * separated or unrolled and store them in option.
3009 * The domains specified by the user might overlap, so we make
3010 * them disjoint by subtracting earlier domains from later domains.
3012 static void compute_domains_init_options(isl_set *option[4],
3013 __isl_keep isl_ast_build *build)
3015 enum isl_ast_loop_type type, type2;
3016 isl_set *unroll;
3018 for (type = isl_ast_loop_atomic;
3019 type <= isl_ast_loop_separate; ++type) {
3020 option[type] = isl_ast_build_get_option_domain(build, type);
3021 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3022 option[type] = isl_set_subtract(option[type],
3023 isl_set_copy(option[type2]));
3026 unroll = option[isl_ast_loop_unroll];
3027 unroll = isl_set_coalesce(unroll);
3028 unroll = isl_set_make_disjoint(unroll);
3029 option[isl_ast_loop_unroll] = unroll;
3032 /* Split up the domain at the current depth into disjoint
3033 * basic sets for which code should be generated separately,
3034 * based on the user-specified options.
3035 * Return the list of disjoint basic sets.
3037 * There are three kinds of domains that we need to keep track of.
3038 * - the "schedule domain" is the domain of "executed"
3039 * - the "class domain" is the domain corresponding to the currrent
3040 * separation class
3041 * - the "option domain" is the domain corresponding to one of the options
3042 * atomic, unroll or separate
3044 * We first consider the individial values of the separation classes
3045 * and split up the domain for each of them separately.
3046 * Finally, we consider the remainder. If no separation classes were
3047 * specified, then we call compute_partial_domains with the universe
3048 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3049 * with inner dimensions removed. We do this because we want to
3050 * avoid computing the complement of the class domains (i.e., the difference
3051 * between the universe and domains->done).
3053 static __isl_give isl_basic_set_list *compute_domains(
3054 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3056 struct isl_codegen_domains domains;
3057 isl_ctx *ctx;
3058 isl_set *domain;
3059 isl_union_set *schedule_domain;
3060 isl_set *classes;
3061 isl_space *space;
3062 int n_param;
3063 enum isl_ast_loop_type type;
3064 int empty;
3066 if (!executed)
3067 return NULL;
3069 ctx = isl_union_map_get_ctx(executed);
3070 domains.list = isl_basic_set_list_alloc(ctx, 0);
3072 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3073 domain = isl_set_from_union_set(schedule_domain);
3075 compute_domains_init_options(domains.option, build);
3077 domains.sep_class = isl_ast_build_get_separation_class(build);
3078 classes = isl_map_range(isl_map_copy(domains.sep_class));
3079 n_param = isl_set_dim(classes, isl_dim_param);
3080 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3082 space = isl_set_get_space(domain);
3083 domains.build = build;
3084 domains.schedule_domain = isl_set_copy(domain);
3085 domains.executed = executed;
3086 domains.done = isl_set_empty(space);
3088 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3089 domains.list = isl_basic_set_list_free(domains.list);
3090 isl_set_free(classes);
3092 empty = isl_set_is_empty(domains.done);
3093 if (empty < 0) {
3094 domains.list = isl_basic_set_list_free(domains.list);
3095 domain = isl_set_free(domain);
3096 } else if (empty) {
3097 isl_set_free(domain);
3098 domain = isl_set_universe(isl_set_get_space(domains.done));
3099 } else {
3100 domain = isl_ast_build_eliminate(build, domain);
3102 if (compute_partial_domains(&domains, domain) < 0)
3103 domains.list = isl_basic_set_list_free(domains.list);
3105 isl_set_free(domains.schedule_domain);
3106 isl_set_free(domains.done);
3107 isl_map_free(domains.sep_class);
3108 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3109 isl_set_free(domains.option[type]);
3111 return domains.list;
3114 /* Generate code for a single component, after shifting (if any)
3115 * has been applied, in case the schedule was specified as a union map.
3117 * We first split up the domain at the current depth into disjoint
3118 * basic sets based on the user-specified options.
3119 * Then we generated code for each of them and concatenate the results.
3121 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3122 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3124 isl_basic_set_list *domain_list;
3125 isl_ast_graft_list *list = NULL;
3127 domain_list = compute_domains(executed, build);
3128 list = generate_parallel_domains(domain_list, executed, build);
3130 isl_basic_set_list_free(domain_list);
3131 isl_union_map_free(executed);
3132 isl_ast_build_free(build);
3134 return list;
3137 /* Generate code for a single component, after shifting (if any)
3138 * has been applied, in case the schedule was specified as a schedule tree
3139 * and the separate option was specified.
3141 * We perform separation on the domain of "executed" and then generate
3142 * an AST for each of the resulting disjoint basic sets.
3144 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3145 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3147 isl_space *space;
3148 isl_set *domain;
3149 isl_basic_set_list *domain_list;
3150 isl_ast_graft_list *list;
3152 space = isl_ast_build_get_space(build, 1);
3153 domain = separate_schedule_domains(space,
3154 isl_union_map_copy(executed), build);
3155 domain_list = isl_basic_set_list_from_set(domain);
3157 list = generate_parallel_domains(domain_list, executed, build);
3159 isl_basic_set_list_free(domain_list);
3160 isl_union_map_free(executed);
3161 isl_ast_build_free(build);
3163 return list;
3166 /* Internal data structure for generate_shifted_component_tree_unroll.
3168 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3169 * "list" collects the constructs grafts.
3171 struct isl_ast_unroll_tree_data {
3172 isl_union_map *executed;
3173 isl_ast_build *build;
3174 isl_ast_graft_list *list;
3177 /* Initialize data->list to a list of "n" elements.
3179 static int init_unroll_tree(int n, void *user)
3181 struct isl_ast_unroll_tree_data *data = user;
3182 isl_ctx *ctx;
3184 ctx = isl_ast_build_get_ctx(data->build);
3185 data->list = isl_ast_graft_list_alloc(ctx, n);
3187 return 0;
3190 /* Given an iteration of an unrolled domain represented by "bset",
3191 * generate the corresponding AST and add the result to data->list.
3193 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3195 struct isl_ast_unroll_tree_data *data = user;
3197 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3198 bset, isl_ast_build_copy(data->build));
3200 return 0;
3203 /* Generate code for a single component, after shifting (if any)
3204 * has been applied, in case the schedule was specified as a schedule tree
3205 * and the unroll option was specified.
3207 * We call foreach_iteration to iterate over the individual values and
3208 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3210 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3211 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3212 __isl_take isl_ast_build *build)
3214 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3216 if (foreach_iteration(domain, build, &init_unroll_tree,
3217 &do_unroll_tree_iteration, &data) < 0)
3218 data.list = isl_ast_graft_list_free(data.list);
3220 isl_union_map_free(executed);
3221 isl_ast_build_free(build);
3223 return data.list;
3226 /* Does "domain" involve a disjunction that is purely based on
3227 * constraints involving only outer dimension?
3229 * In particular, is there a disjunction such that the constraints
3230 * involving the current and later dimensions are the same over
3231 * all the disjuncts?
3233 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3234 __isl_keep isl_ast_build *build)
3236 isl_basic_set *hull;
3237 isl_set *shared, *inner;
3238 isl_bool equal;
3239 int depth, dim;
3241 if (isl_set_n_basic_set(domain) <= 1)
3242 return isl_bool_false;
3244 inner = isl_set_copy(domain);
3245 depth = isl_ast_build_get_depth(build);
3246 dim = isl_set_dim(inner, isl_dim_set);
3247 inner = isl_set_drop_constraints_not_involving_dims(inner,
3248 isl_dim_set, depth, dim - depth);
3249 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3250 shared = isl_set_from_basic_set(hull);
3251 equal = isl_set_plain_is_equal(inner, shared);
3252 isl_set_free(inner);
3253 isl_set_free(shared);
3255 return equal;
3258 /* Generate code for a single component, after shifting (if any)
3259 * has been applied, in case the schedule was specified as a schedule tree.
3260 * In particular, handle the base case where there is either no isolated
3261 * set or we are within the isolated set (in which case "isolated" is set)
3262 * or the iterations that precede or follow the isolated set.
3264 * The schedule domain is broken up or combined into basic sets
3265 * according to the AST generation option specified in the current
3266 * schedule node, which may be either atomic, separate, unroll or
3267 * unspecified. If the option is unspecified, then we currently simply
3268 * split the schedule domain into disjoint basic sets.
3270 * In case the separate option is specified, the AST generation is
3271 * handled by generate_shifted_component_tree_separate.
3272 * In the other cases, we need the global schedule domain.
3273 * In the unroll case, the AST generation is then handled by
3274 * generate_shifted_component_tree_unroll which needs the actual
3275 * schedule domain (with divs that may refer to the current dimension)
3276 * so that stride detection can be performed.
3277 * In the atomic or unspecified case, inner dimensions and divs involving
3278 * the current dimensions should be eliminated.
3279 * The result is then either combined into a single basic set or
3280 * split up into disjoint basic sets.
3281 * Finally an AST is generated for each basic set and the results are
3282 * concatenated.
3284 * If the schedule domain involves a disjunction that is purely based on
3285 * constraints involving only outer dimension, then it is treated as
3286 * if atomic was specified. This ensures that only a single loop
3287 * is generated instead of a sequence of identical loops with
3288 * different guards.
3290 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3291 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3292 int isolated)
3294 isl_bool outer_disjunction;
3295 isl_union_set *schedule_domain;
3296 isl_set *domain;
3297 isl_basic_set_list *domain_list;
3298 isl_ast_graft_list *list;
3299 enum isl_ast_loop_type type;
3301 type = isl_ast_build_get_loop_type(build, isolated);
3302 if (type < 0)
3303 goto error;
3305 if (type == isl_ast_loop_separate)
3306 return generate_shifted_component_tree_separate(executed,
3307 build);
3309 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3310 domain = isl_set_from_union_set(schedule_domain);
3312 if (type == isl_ast_loop_unroll)
3313 return generate_shifted_component_tree_unroll(executed, domain,
3314 build);
3316 domain = isl_ast_build_eliminate(build, domain);
3317 domain = isl_set_coalesce(domain);
3319 outer_disjunction = has_pure_outer_disjunction(domain, build);
3320 if (outer_disjunction < 0)
3321 domain = isl_set_free(domain);
3323 if (outer_disjunction || type == isl_ast_loop_atomic) {
3324 isl_basic_set *hull;
3325 hull = isl_set_unshifted_simple_hull(domain);
3326 domain_list = isl_basic_set_list_from_basic_set(hull);
3327 } else {
3328 domain = isl_set_make_disjoint(domain);
3329 domain_list = isl_basic_set_list_from_set(domain);
3332 list = generate_parallel_domains(domain_list, executed, build);
3334 isl_basic_set_list_free(domain_list);
3335 isl_union_map_free(executed);
3336 isl_ast_build_free(build);
3338 return list;
3339 error:
3340 isl_union_map_free(executed);
3341 isl_ast_build_free(build);
3342 return NULL;
3345 /* Extract out the disjunction imposed by "domain" on the outer
3346 * schedule dimensions.
3348 * In particular, remove all inner dimensions from "domain" (including
3349 * the current dimension) and then remove the constraints that are shared
3350 * by all disjuncts in the result.
3352 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3353 __isl_keep isl_ast_build *build)
3355 isl_set *hull;
3356 int depth, dim;
3358 domain = isl_ast_build_specialize(build, domain);
3359 depth = isl_ast_build_get_depth(build);
3360 dim = isl_set_dim(domain, isl_dim_set);
3361 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3362 domain = isl_set_remove_unknown_divs(domain);
3363 hull = isl_set_copy(domain);
3364 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3365 domain = isl_set_gist(domain, hull);
3367 return domain;
3370 /* Add "guard" to the grafts in "list".
3371 * "build" is the outer AST build, while "sub_build" includes "guard"
3372 * in its generated domain.
3374 * First combine the grafts into a single graft and then add the guard.
3375 * If the list is empty, or if some error occurred, then simply return
3376 * the list.
3378 static __isl_give isl_ast_graft_list *list_add_guard(
3379 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3380 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3382 isl_ast_graft *graft;
3384 list = isl_ast_graft_list_fuse(list, sub_build);
3386 if (isl_ast_graft_list_n_ast_graft(list) != 1)
3387 return list;
3389 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3390 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3391 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3393 return list;
3396 /* Generate code for a single component, after shifting (if any)
3397 * has been applied, in case the schedule was specified as a schedule tree.
3398 * In particular, do so for the specified subset of the schedule domain.
3400 * If we are outside of the isolated part, then "domain" may include
3401 * a disjunction. Explicitly generate this disjunction at this point
3402 * instead of relying on the disjunction getting hoisted back up
3403 * to this level.
3405 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3406 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3407 __isl_keep isl_ast_build *build, int isolated)
3409 isl_union_set *uset;
3410 isl_ast_graft_list *list;
3411 isl_ast_build *sub_build;
3412 int empty;
3414 uset = isl_union_set_from_set(isl_set_copy(domain));
3415 executed = isl_union_map_copy(executed);
3416 executed = isl_union_map_intersect_domain(executed, uset);
3417 empty = isl_union_map_is_empty(executed);
3418 if (empty < 0)
3419 goto error;
3420 if (empty) {
3421 isl_ctx *ctx;
3422 isl_union_map_free(executed);
3423 isl_set_free(domain);
3424 ctx = isl_ast_build_get_ctx(build);
3425 return isl_ast_graft_list_alloc(ctx, 0);
3428 sub_build = isl_ast_build_copy(build);
3429 if (!isolated) {
3430 domain = extract_disjunction(domain, build);
3431 sub_build = isl_ast_build_restrict_generated(sub_build,
3432 isl_set_copy(domain));
3434 list = generate_shifted_component_tree_base(executed,
3435 isl_ast_build_copy(sub_build), isolated);
3436 if (!isolated)
3437 list = list_add_guard(list, domain, build, sub_build);
3438 isl_ast_build_free(sub_build);
3439 isl_set_free(domain);
3440 return list;
3441 error:
3442 isl_union_map_free(executed);
3443 isl_set_free(domain);
3444 return NULL;
3447 /* Generate code for a single component, after shifting (if any)
3448 * has been applied, in case the schedule was specified as a schedule tree.
3449 * In particular, do so for the specified sequence of subsets
3450 * of the schedule domain, "before", "isolated", "after" and "other",
3451 * where only the "isolated" part is considered to be isolated.
3453 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3454 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3455 __isl_take isl_set *isolated, __isl_take isl_set *after,
3456 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3458 isl_ast_graft_list *list, *res;
3460 res = generate_shifted_component_tree_part(executed, before, build, 0);
3461 list = generate_shifted_component_tree_part(executed, isolated,
3462 build, 1);
3463 res = isl_ast_graft_list_concat(res, list);
3464 list = generate_shifted_component_tree_part(executed, after, build, 0);
3465 res = isl_ast_graft_list_concat(res, list);
3466 list = generate_shifted_component_tree_part(executed, other, build, 0);
3467 res = isl_ast_graft_list_concat(res, list);
3469 isl_union_map_free(executed);
3470 isl_ast_build_free(build);
3472 return res;
3475 /* Does "set" intersect "first", but not "second"?
3477 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3478 __isl_keep isl_set *first, __isl_keep isl_set *second)
3480 isl_bool disjoint;
3482 disjoint = isl_set_is_disjoint(set, first);
3483 if (disjoint < 0)
3484 return isl_bool_error;
3485 if (disjoint)
3486 return isl_bool_false;
3488 return isl_set_is_disjoint(set, second);
3491 /* Generate code for a single component, after shifting (if any)
3492 * has been applied, in case the schedule was specified as a schedule tree.
3493 * In particular, do so in case of isolation where there is
3494 * only an "isolated" part and an "after" part.
3495 * "dead1" and "dead2" are freed by this function in order to simplify
3496 * the caller.
3498 * The "before" and "other" parts are set to empty sets.
3500 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3501 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3502 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3503 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3505 isl_set *empty;
3507 empty = isl_set_empty(isl_set_get_space(after));
3508 isl_set_free(dead1);
3509 isl_set_free(dead2);
3510 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3511 isolated, after, empty, build);
3514 /* Generate code for a single component, after shifting (if any)
3515 * has been applied, in case the schedule was specified as a schedule tree.
3517 * We first check if the user has specified an isolated schedule domain
3518 * and that we are not already outside of this isolated schedule domain.
3519 * If so, we break up the schedule domain into iterations that
3520 * precede the isolated domain, the isolated domain itself,
3521 * the iterations that follow the isolated domain and
3522 * the remaining iterations (those that are incomparable
3523 * to the isolated domain).
3524 * We generate an AST for each piece and concatenate the results.
3526 * If the isolated domain is not convex, then it is replaced
3527 * by a convex superset to ensure that the sets of preceding and
3528 * following iterations are properly defined and, in particular,
3529 * that there are no intermediate iterations that do not belong
3530 * to the isolated domain.
3532 * In the special case where at least one element of the schedule
3533 * domain that does not belong to the isolated domain needs
3534 * to be scheduled after this isolated domain, but none of those
3535 * elements need to be scheduled before, break up the schedule domain
3536 * in only two parts, the isolated domain, and a part that will be
3537 * scheduled after the isolated domain.
3539 * If no isolated set has been specified, then we generate an
3540 * AST for the entire inverse schedule.
3542 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3543 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3545 int i, depth;
3546 int empty, has_isolate;
3547 isl_space *space;
3548 isl_union_set *schedule_domain;
3549 isl_set *domain;
3550 isl_basic_set *hull;
3551 isl_set *isolated, *before, *after, *test;
3552 isl_map *gt, *lt;
3553 isl_bool pure;
3555 build = isl_ast_build_extract_isolated(build);
3556 has_isolate = isl_ast_build_has_isolated(build);
3557 if (has_isolate < 0)
3558 executed = isl_union_map_free(executed);
3559 else if (!has_isolate)
3560 return generate_shifted_component_tree_base(executed, build, 0);
3562 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3563 domain = isl_set_from_union_set(schedule_domain);
3565 isolated = isl_ast_build_get_isolated(build);
3566 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3567 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3568 empty = isl_set_is_empty(test);
3569 isl_set_free(test);
3570 if (empty < 0)
3571 goto error;
3572 if (empty) {
3573 isl_set_free(isolated);
3574 isl_set_free(domain);
3575 return generate_shifted_component_tree_base(executed, build, 0);
3577 isolated = isl_ast_build_eliminate(build, isolated);
3578 hull = isl_set_unshifted_simple_hull(isolated);
3579 isolated = isl_set_from_basic_set(hull);
3581 depth = isl_ast_build_get_depth(build);
3582 space = isl_space_map_from_set(isl_set_get_space(isolated));
3583 gt = isl_map_universe(space);
3584 for (i = 0; i < depth; ++i)
3585 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3586 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3587 lt = isl_map_reverse(isl_map_copy(gt));
3588 before = isl_set_apply(isl_set_copy(isolated), gt);
3589 after = isl_set_apply(isl_set_copy(isolated), lt);
3591 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3592 pure = only_intersects_first(domain, after, before);
3593 if (pure < 0)
3594 executed = isl_union_map_free(executed);
3595 else if (pure)
3596 return generate_shifted_component_only_after(executed, isolated,
3597 domain, build, before, after);
3598 domain = isl_set_subtract(domain, isl_set_copy(before));
3599 domain = isl_set_subtract(domain, isl_set_copy(after));
3600 after = isl_set_subtract(after, isl_set_copy(isolated));
3601 after = isl_set_subtract(after, isl_set_copy(before));
3602 before = isl_set_subtract(before, isl_set_copy(isolated));
3604 return generate_shifted_component_parts(executed, before, isolated,
3605 after, domain, build);
3606 error:
3607 isl_set_free(domain);
3608 isl_set_free(isolated);
3609 isl_union_map_free(executed);
3610 isl_ast_build_free(build);
3611 return NULL;
3614 /* Generate code for a single component, after shifting (if any)
3615 * has been applied.
3617 * Call generate_shifted_component_tree or generate_shifted_component_flat
3618 * depending on whether the schedule was specified as a schedule tree.
3620 static __isl_give isl_ast_graft_list *generate_shifted_component(
3621 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3623 if (isl_ast_build_has_schedule_node(build))
3624 return generate_shifted_component_tree(executed, build);
3625 else
3626 return generate_shifted_component_flat(executed, build);
3629 struct isl_set_map_pair {
3630 isl_set *set;
3631 isl_map *map;
3634 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3635 * of indices into the "domain" array,
3636 * return the union of the "map" fields of the elements
3637 * indexed by the first "n" elements of "order".
3639 static __isl_give isl_union_map *construct_component_executed(
3640 struct isl_set_map_pair *domain, int *order, int n)
3642 int i;
3643 isl_map *map;
3644 isl_union_map *executed;
3646 map = isl_map_copy(domain[order[0]].map);
3647 executed = isl_union_map_from_map(map);
3648 for (i = 1; i < n; ++i) {
3649 map = isl_map_copy(domain[order[i]].map);
3650 executed = isl_union_map_add_map(executed, map);
3653 return executed;
3656 /* Generate code for a single component, after shifting (if any)
3657 * has been applied.
3659 * The component inverse schedule is specified as the "map" fields
3660 * of the elements of "domain" indexed by the first "n" elements of "order".
3662 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3663 struct isl_set_map_pair *domain, int *order, int n,
3664 __isl_take isl_ast_build *build)
3666 isl_union_map *executed;
3668 executed = construct_component_executed(domain, order, n);
3669 return generate_shifted_component(executed, build);
3672 /* Does set dimension "pos" of "set" have an obviously fixed value?
3674 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3676 int fixed;
3677 isl_val *v;
3679 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3680 if (!v)
3681 return -1;
3682 fixed = !isl_val_is_nan(v);
3683 isl_val_free(v);
3685 return fixed;
3688 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3689 * of indices into the "domain" array,
3690 * do all (except for at most one) of the "set" field of the elements
3691 * indexed by the first "n" elements of "order" have a fixed value
3692 * at position "depth"?
3694 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3695 int *order, int n, int depth)
3697 int i;
3698 int non_fixed = -1;
3700 for (i = 0; i < n; ++i) {
3701 int f;
3703 f = dim_is_fixed(domain[order[i]].set, depth);
3704 if (f < 0)
3705 return -1;
3706 if (f)
3707 continue;
3708 if (non_fixed >= 0)
3709 return 0;
3710 non_fixed = i;
3713 return 1;
3716 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3717 * of indices into the "domain" array,
3718 * eliminate the inner dimensions from the "set" field of the elements
3719 * indexed by the first "n" elements of "order", provided the current
3720 * dimension does not have a fixed value.
3722 * Return the index of the first element in "order" with a corresponding
3723 * "set" field that does not have an (obviously) fixed value.
3725 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3726 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3728 int i;
3729 int base = -1;
3731 for (i = n - 1; i >= 0; --i) {
3732 int f;
3733 f = dim_is_fixed(domain[order[i]].set, depth);
3734 if (f < 0)
3735 return -1;
3736 if (f)
3737 continue;
3738 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3739 domain[order[i]].set);
3740 base = i;
3743 return base;
3746 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3747 * of indices into the "domain" array,
3748 * find the element of "domain" (amongst those indexed by the first "n"
3749 * elements of "order") with the "set" field that has the smallest
3750 * value for the current iterator.
3752 * Note that the domain with the smallest value may depend on the parameters
3753 * and/or outer loop dimension. Since the result of this function is only
3754 * used as heuristic, we only make a reasonable attempt at finding the best
3755 * domain, one that should work in case a single domain provides the smallest
3756 * value for the current dimension over all values of the parameters
3757 * and outer dimensions.
3759 * In particular, we compute the smallest value of the first domain
3760 * and replace it by that of any later domain if that later domain
3761 * has a smallest value that is smaller for at least some value
3762 * of the parameters and outer dimensions.
3764 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3765 __isl_keep isl_ast_build *build)
3767 int i;
3768 isl_map *min_first;
3769 int first = 0;
3771 min_first = isl_ast_build_map_to_iterator(build,
3772 isl_set_copy(domain[order[0]].set));
3773 min_first = isl_map_lexmin(min_first);
3775 for (i = 1; i < n; ++i) {
3776 isl_map *min, *test;
3777 int empty;
3779 min = isl_ast_build_map_to_iterator(build,
3780 isl_set_copy(domain[order[i]].set));
3781 min = isl_map_lexmin(min);
3782 test = isl_map_copy(min);
3783 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3784 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3785 empty = isl_map_is_empty(test);
3786 isl_map_free(test);
3787 if (empty >= 0 && !empty) {
3788 isl_map_free(min_first);
3789 first = i;
3790 min_first = min;
3791 } else
3792 isl_map_free(min);
3794 if (empty < 0)
3795 break;
3798 isl_map_free(min_first);
3800 return i < n ? -1 : first;
3803 /* Construct a shifted inverse schedule based on the original inverse schedule,
3804 * the stride and the offset.
3806 * The original inverse schedule is specified as the "map" fields
3807 * of the elements of "domain" indexed by the first "n" elements of "order".
3809 * "stride" and "offset" are such that the difference
3810 * between the values of the current dimension of domain "i"
3811 * and the values of the current dimension for some reference domain are
3812 * equal to
3814 * stride * integer + offset[i]
3816 * Moreover, 0 <= offset[i] < stride.
3818 * For each domain, we create a map
3820 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3822 * where j refers to the current dimension and the other dimensions are
3823 * unchanged, and apply this map to the original schedule domain.
3825 * For example, for the original schedule
3827 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3829 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3830 * we apply the mapping
3832 * { [j] -> [j, 0] }
3834 * to the schedule of the "A" domain and the mapping
3836 * { [j - 1] -> [j, 1] }
3838 * to the schedule of the "B" domain.
3841 * Note that after the transformation, the differences between pairs
3842 * of values of the current dimension over all domains are multiples
3843 * of stride and that we have therefore exposed the stride.
3846 * To see that the mapping preserves the lexicographic order,
3847 * first note that each of the individual maps above preserves the order.
3848 * If the value of the current iterator is j1 in one domain and j2 in another,
3849 * then if j1 = j2, we know that the same map is applied to both domains
3850 * and the order is preserved.
3851 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3852 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3854 * j1 - c1 < j2 - c2
3856 * and the order is preserved.
3857 * If c1 < c2, then we know
3859 * 0 <= c2 - c1 < s
3861 * We also have
3863 * j2 - j1 = n * s + r
3865 * with n >= 0 and 0 <= r < s.
3866 * In other words, r = c2 - c1.
3867 * If n > 0, then
3869 * j1 - c1 < j2 - c2
3871 * If n = 0, then
3873 * j1 - c1 = j2 - c2
3875 * and so
3877 * (j1 - c1, c1) << (j2 - c2, c2)
3879 * with "<<" the lexicographic order, proving that the order is preserved
3880 * in all cases.
3882 static __isl_give isl_union_map *contruct_shifted_executed(
3883 struct isl_set_map_pair *domain, int *order, int n,
3884 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3885 __isl_take isl_ast_build *build)
3887 int i;
3888 isl_union_map *executed;
3889 isl_space *space;
3890 isl_map *map;
3891 int depth;
3892 isl_constraint *c;
3894 depth = isl_ast_build_get_depth(build);
3895 space = isl_ast_build_get_space(build, 1);
3896 executed = isl_union_map_empty(isl_space_copy(space));
3897 space = isl_space_map_from_set(space);
3898 map = isl_map_identity(isl_space_copy(space));
3899 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3900 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3901 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3903 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3904 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3905 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3907 for (i = 0; i < n; ++i) {
3908 isl_map *map_i;
3909 isl_val *v;
3911 v = isl_multi_val_get_val(offset, i);
3912 if (!v)
3913 break;
3914 map_i = isl_map_copy(map);
3915 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3916 isl_val_copy(v));
3917 v = isl_val_neg(v);
3918 c = isl_constraint_set_constant_val(c, v);
3919 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3921 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3922 map_i);
3923 executed = isl_union_map_add_map(executed, map_i);
3926 isl_constraint_free(c);
3927 isl_map_free(map);
3929 if (i < n)
3930 executed = isl_union_map_free(executed);
3932 return executed;
3935 /* Generate code for a single component, after exposing the stride,
3936 * given that the schedule domain is "shifted strided".
3938 * The component inverse schedule is specified as the "map" fields
3939 * of the elements of "domain" indexed by the first "n" elements of "order".
3941 * The schedule domain being "shifted strided" means that the differences
3942 * between the values of the current dimension of domain "i"
3943 * and the values of the current dimension for some reference domain are
3944 * equal to
3946 * stride * integer + offset[i]
3948 * We first look for the domain with the "smallest" value for the current
3949 * dimension and adjust the offsets such that the offset of the "smallest"
3950 * domain is equal to zero. The other offsets are reduced modulo stride.
3952 * Based on this information, we construct a new inverse schedule in
3953 * contruct_shifted_executed that exposes the stride.
3954 * Since this involves the introduction of a new schedule dimension,
3955 * the build needs to be changed accodingly.
3956 * After computing the AST, the newly introduced dimension needs
3957 * to be removed again from the list of grafts. We do this by plugging
3958 * in a mapping that represents the new schedule domain in terms of the
3959 * old schedule domain.
3961 static __isl_give isl_ast_graft_list *generate_shift_component(
3962 struct isl_set_map_pair *domain, int *order, int n,
3963 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3964 __isl_take isl_ast_build *build)
3966 isl_ast_graft_list *list;
3967 int first;
3968 int depth;
3969 isl_val *val;
3970 isl_multi_val *mv;
3971 isl_space *space;
3972 isl_multi_aff *ma, *zero;
3973 isl_union_map *executed;
3975 depth = isl_ast_build_get_depth(build);
3977 first = first_offset(domain, order, n, build);
3978 if (first < 0)
3979 goto error;
3981 mv = isl_multi_val_copy(offset);
3982 val = isl_multi_val_get_val(offset, first);
3983 val = isl_val_neg(val);
3984 mv = isl_multi_val_add_val(mv, val);
3985 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3987 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3988 build);
3989 space = isl_ast_build_get_space(build, 1);
3990 space = isl_space_map_from_set(space);
3991 ma = isl_multi_aff_identity(isl_space_copy(space));
3992 space = isl_space_from_domain(isl_space_domain(space));
3993 space = isl_space_add_dims(space, isl_dim_out, 1);
3994 zero = isl_multi_aff_zero(space);
3995 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3996 build = isl_ast_build_insert_dim(build, depth + 1);
3997 list = generate_shifted_component(executed, build);
3999 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4001 isl_multi_val_free(mv);
4003 return list;
4004 error:
4005 isl_ast_build_free(build);
4006 return NULL;
4009 /* Does any node in the schedule tree rooted at the current schedule node
4010 * of "build" depend on outer schedule nodes?
4012 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4014 isl_schedule_node *node;
4015 int dependent = 0;
4017 node = isl_ast_build_get_schedule_node(build);
4018 dependent = isl_schedule_node_is_subtree_anchored(node);
4019 isl_schedule_node_free(node);
4021 return dependent;
4024 /* Generate code for a single component.
4026 * The component inverse schedule is specified as the "map" fields
4027 * of the elements of "domain" indexed by the first "n" elements of "order".
4029 * This function may modify the "set" fields of "domain".
4031 * Before proceeding with the actual code generation for the component,
4032 * we first check if there are any "shifted" strides, meaning that
4033 * the schedule domains of the individual domains are all strided,
4034 * but that they have different offsets, resulting in the union
4035 * of schedule domains not being strided anymore.
4037 * The simplest example is the schedule
4039 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4041 * Both schedule domains are strided, but their union is not.
4042 * This function detects such cases and then rewrites the schedule to
4044 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4046 * In the new schedule, the schedule domains have the same offset (modulo
4047 * the stride), ensuring that the union of schedule domains is also strided.
4050 * If there is only a single domain in the component, then there is
4051 * nothing to do. Similarly, if the current schedule dimension has
4052 * a fixed value for almost all domains then there is nothing to be done.
4053 * In particular, we need at least two domains where the current schedule
4054 * dimension does not have a fixed value.
4055 * Finally, in case of a schedule map input,
4056 * if any of the options refer to the current schedule dimension,
4057 * then we bail out as well. It would be possible to reformulate the options
4058 * in terms of the new schedule domain, but that would introduce constraints
4059 * that separate the domains in the options and that is something we would
4060 * like to avoid.
4061 * In the case of a schedule tree input, we bail out if any of
4062 * the descendants of the current schedule node refer to outer
4063 * schedule nodes in any way.
4066 * To see if there is any shifted stride, we look at the differences
4067 * between the values of the current dimension in pairs of domains
4068 * for equal values of outer dimensions. These differences should be
4069 * of the form
4071 * m x + r
4073 * with "m" the stride and "r" a constant. Note that we cannot perform
4074 * this analysis on individual domains as the lower bound in each domain
4075 * may depend on parameters or outer dimensions and so the current dimension
4076 * itself may not have a fixed remainder on division by the stride.
4078 * In particular, we compare the first domain that does not have an
4079 * obviously fixed value for the current dimension to itself and all
4080 * other domains and collect the offsets and the gcd of the strides.
4081 * If the gcd becomes one, then we failed to find shifted strides.
4082 * If the gcd is zero, then the differences were all fixed, meaning
4083 * that some domains had non-obviously fixed values for the current dimension.
4084 * If all the offsets are the same (for those domains that do not have
4085 * an obviously fixed value for the current dimension), then we do not
4086 * apply the transformation.
4087 * If none of the domains were skipped, then there is nothing to do.
4088 * If some of them were skipped, then if we apply separation, the schedule
4089 * domain should get split in pieces with a (non-shifted) stride.
4091 * Otherwise, we apply a shift to expose the stride in
4092 * generate_shift_component.
4094 static __isl_give isl_ast_graft_list *generate_component(
4095 struct isl_set_map_pair *domain, int *order, int n,
4096 __isl_take isl_ast_build *build)
4098 int i, d;
4099 int depth;
4100 isl_ctx *ctx;
4101 isl_map *map;
4102 isl_set *deltas;
4103 isl_val *gcd = NULL;
4104 isl_multi_val *mv;
4105 int fixed, skip;
4106 int base;
4107 isl_ast_graft_list *list;
4108 int res = 0;
4110 depth = isl_ast_build_get_depth(build);
4112 skip = n == 1;
4113 if (skip >= 0 && !skip)
4114 skip = at_most_one_non_fixed(domain, order, n, depth);
4115 if (skip >= 0 && !skip) {
4116 if (isl_ast_build_has_schedule_node(build))
4117 skip = has_anchored_subtree(build);
4118 else
4119 skip = isl_ast_build_options_involve_depth(build);
4121 if (skip < 0)
4122 goto error;
4123 if (skip)
4124 return generate_shifted_component_from_list(domain,
4125 order, n, build);
4127 base = eliminate_non_fixed(domain, order, n, depth, build);
4128 if (base < 0)
4129 goto error;
4131 ctx = isl_ast_build_get_ctx(build);
4133 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4135 fixed = 1;
4136 for (i = 0; i < n; ++i) {
4137 isl_val *r, *m;
4139 map = isl_map_from_domain_and_range(
4140 isl_set_copy(domain[order[base]].set),
4141 isl_set_copy(domain[order[i]].set));
4142 for (d = 0; d < depth; ++d)
4143 map = isl_map_equate(map, isl_dim_in, d,
4144 isl_dim_out, d);
4145 deltas = isl_map_deltas(map);
4146 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4147 isl_set_free(deltas);
4148 if (res < 0)
4149 break;
4151 if (i == 0)
4152 gcd = m;
4153 else
4154 gcd = isl_val_gcd(gcd, m);
4155 if (isl_val_is_one(gcd)) {
4156 isl_val_free(r);
4157 break;
4159 mv = isl_multi_val_set_val(mv, i, r);
4161 res = dim_is_fixed(domain[order[i]].set, depth);
4162 if (res < 0)
4163 break;
4164 if (res)
4165 continue;
4167 if (fixed && i > base) {
4168 isl_val *a, *b;
4169 a = isl_multi_val_get_val(mv, i);
4170 b = isl_multi_val_get_val(mv, base);
4171 if (isl_val_ne(a, b))
4172 fixed = 0;
4173 isl_val_free(a);
4174 isl_val_free(b);
4178 if (res < 0 || !gcd) {
4179 isl_ast_build_free(build);
4180 list = NULL;
4181 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4182 list = generate_shifted_component_from_list(domain,
4183 order, n, build);
4184 } else {
4185 list = generate_shift_component(domain, order, n, gcd, mv,
4186 build);
4189 isl_val_free(gcd);
4190 isl_multi_val_free(mv);
4192 return list;
4193 error:
4194 isl_ast_build_free(build);
4195 return NULL;
4198 /* Store both "map" itself and its domain in the
4199 * structure pointed to by *next and advance to the next array element.
4201 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4203 struct isl_set_map_pair **next = user;
4205 (*next)->map = isl_map_copy(map);
4206 (*next)->set = isl_map_domain(map);
4207 (*next)++;
4209 return isl_stat_ok;
4212 static int after_in_tree(__isl_keep isl_union_map *umap,
4213 __isl_keep isl_schedule_node *node);
4215 /* Is any domain element of "umap" scheduled after any of
4216 * the corresponding image elements by the tree rooted at
4217 * the child of "node"?
4219 static int after_in_child(__isl_keep isl_union_map *umap,
4220 __isl_keep isl_schedule_node *node)
4222 isl_schedule_node *child;
4223 int after;
4225 child = isl_schedule_node_get_child(node, 0);
4226 after = after_in_tree(umap, child);
4227 isl_schedule_node_free(child);
4229 return after;
4232 /* Is any domain element of "umap" scheduled after any of
4233 * the corresponding image elements by the tree rooted at
4234 * the band node "node"?
4236 * We first check if any domain element is scheduled after any
4237 * of the corresponding image elements by the band node itself.
4238 * If not, we restrict "map" to those pairs of element that
4239 * are scheduled together by the band node and continue with
4240 * the child of the band node.
4241 * If there are no such pairs then the map passed to after_in_child
4242 * will be empty causing it to return 0.
4244 static int after_in_band(__isl_keep isl_union_map *umap,
4245 __isl_keep isl_schedule_node *node)
4247 isl_multi_union_pw_aff *mupa;
4248 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4249 isl_union_set *domain, *range;
4250 isl_space *space;
4251 int empty;
4252 int after;
4254 if (isl_schedule_node_band_n_member(node) == 0)
4255 return after_in_child(umap, node);
4257 mupa = isl_schedule_node_band_get_partial_schedule(node);
4258 space = isl_multi_union_pw_aff_get_space(mupa);
4259 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4260 test = isl_union_map_copy(umap);
4261 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4262 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4263 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4264 test = isl_union_map_intersect(test, gt);
4265 empty = isl_union_map_is_empty(test);
4266 isl_union_map_free(test);
4268 if (empty < 0 || !empty) {
4269 isl_union_map_free(partial);
4270 return empty < 0 ? -1 : 1;
4273 universe = isl_union_map_universe(isl_union_map_copy(umap));
4274 domain = isl_union_map_domain(isl_union_map_copy(universe));
4275 range = isl_union_map_range(universe);
4276 umap1 = isl_union_map_copy(partial);
4277 umap1 = isl_union_map_intersect_domain(umap1, domain);
4278 umap2 = isl_union_map_intersect_domain(partial, range);
4279 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4280 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4281 after = after_in_child(test, node);
4282 isl_union_map_free(test);
4283 return after;
4286 /* Is any domain element of "umap" scheduled after any of
4287 * the corresponding image elements by the tree rooted at
4288 * the context node "node"?
4290 * The context constraints apply to the schedule domain,
4291 * so we cannot apply them directly to "umap", which contains
4292 * pairs of statement instances. Instead, we add them
4293 * to the range of the prefix schedule for both domain and
4294 * range of "umap".
4296 static int after_in_context(__isl_keep isl_union_map *umap,
4297 __isl_keep isl_schedule_node *node)
4299 isl_union_map *prefix, *universe, *umap1, *umap2;
4300 isl_union_set *domain, *range;
4301 isl_set *context;
4302 int after;
4304 umap = isl_union_map_copy(umap);
4305 context = isl_schedule_node_context_get_context(node);
4306 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4307 universe = isl_union_map_universe(isl_union_map_copy(umap));
4308 domain = isl_union_map_domain(isl_union_map_copy(universe));
4309 range = isl_union_map_range(universe);
4310 umap1 = isl_union_map_copy(prefix);
4311 umap1 = isl_union_map_intersect_domain(umap1, domain);
4312 umap2 = isl_union_map_intersect_domain(prefix, range);
4313 umap1 = isl_union_map_intersect_range(umap1,
4314 isl_union_set_from_set(context));
4315 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4316 umap = isl_union_map_intersect(umap, umap1);
4318 after = after_in_child(umap, node);
4320 isl_union_map_free(umap);
4322 return after;
4325 /* Is any domain element of "umap" scheduled after any of
4326 * the corresponding image elements by the tree rooted at
4327 * the expansion node "node"?
4329 * We apply the expansion to domain and range of "umap" and
4330 * continue with its child.
4332 static int after_in_expansion(__isl_keep isl_union_map *umap,
4333 __isl_keep isl_schedule_node *node)
4335 isl_union_map *expansion;
4336 int after;
4338 expansion = isl_schedule_node_expansion_get_expansion(node);
4339 umap = isl_union_map_copy(umap);
4340 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4341 umap = isl_union_map_apply_range(umap, expansion);
4343 after = after_in_child(umap, node);
4345 isl_union_map_free(umap);
4347 return after;
4350 /* Is any domain element of "umap" scheduled after any of
4351 * the corresponding image elements by the tree rooted at
4352 * the extension node "node"?
4354 * Since the extension node may add statement instances before or
4355 * after the pairs of statement instances in "umap", we return 1
4356 * to ensure that these pairs are not broken up.
4358 static int after_in_extension(__isl_keep isl_union_map *umap,
4359 __isl_keep isl_schedule_node *node)
4361 return 1;
4364 /* Is any domain element of "umap" scheduled after any of
4365 * the corresponding image elements by the tree rooted at
4366 * the filter node "node"?
4368 * We intersect domain and range of "umap" with the filter and
4369 * continue with its child.
4371 static int after_in_filter(__isl_keep isl_union_map *umap,
4372 __isl_keep isl_schedule_node *node)
4374 isl_union_set *filter;
4375 int after;
4377 umap = isl_union_map_copy(umap);
4378 filter = isl_schedule_node_filter_get_filter(node);
4379 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4380 umap = isl_union_map_intersect_range(umap, filter);
4382 after = after_in_child(umap, node);
4384 isl_union_map_free(umap);
4386 return after;
4389 /* Is any domain element of "umap" scheduled after any of
4390 * the corresponding image elements by the tree rooted at
4391 * the set node "node"?
4393 * This is only the case if this condition holds in any
4394 * of the (filter) children of the set node.
4395 * In particular, if the domain and the range of "umap"
4396 * are contained in different children, then the condition
4397 * does not hold.
4399 static int after_in_set(__isl_keep isl_union_map *umap,
4400 __isl_keep isl_schedule_node *node)
4402 int i, n;
4404 n = isl_schedule_node_n_children(node);
4405 for (i = 0; i < n; ++i) {
4406 isl_schedule_node *child;
4407 int after;
4409 child = isl_schedule_node_get_child(node, i);
4410 after = after_in_tree(umap, child);
4411 isl_schedule_node_free(child);
4413 if (after < 0 || after)
4414 return after;
4417 return 0;
4420 /* Return the filter of child "i" of "node".
4422 static __isl_give isl_union_set *child_filter(
4423 __isl_keep isl_schedule_node *node, int i)
4425 isl_schedule_node *child;
4426 isl_union_set *filter;
4428 child = isl_schedule_node_get_child(node, i);
4429 filter = isl_schedule_node_filter_get_filter(child);
4430 isl_schedule_node_free(child);
4432 return filter;
4435 /* Is any domain element of "umap" scheduled after any of
4436 * the corresponding image elements by the tree rooted at
4437 * the sequence node "node"?
4439 * This happens in particular if any domain element is
4440 * contained in a later child than one containing a range element or
4441 * if the condition holds within a given child in the sequence.
4442 * The later part of the condition is checked by after_in_set.
4444 static int after_in_sequence(__isl_keep isl_union_map *umap,
4445 __isl_keep isl_schedule_node *node)
4447 int i, j, n;
4448 isl_union_map *umap_i;
4449 int empty, after = 0;
4451 n = isl_schedule_node_n_children(node);
4452 for (i = 1; i < n; ++i) {
4453 isl_union_set *filter_i;
4455 umap_i = isl_union_map_copy(umap);
4456 filter_i = child_filter(node, i);
4457 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4458 empty = isl_union_map_is_empty(umap_i);
4459 if (empty < 0)
4460 goto error;
4461 if (empty) {
4462 isl_union_map_free(umap_i);
4463 continue;
4466 for (j = 0; j < i; ++j) {
4467 isl_union_set *filter_j;
4468 isl_union_map *umap_ij;
4470 umap_ij = isl_union_map_copy(umap_i);
4471 filter_j = child_filter(node, j);
4472 umap_ij = isl_union_map_intersect_range(umap_ij,
4473 filter_j);
4474 empty = isl_union_map_is_empty(umap_ij);
4475 isl_union_map_free(umap_ij);
4477 if (empty < 0)
4478 goto error;
4479 if (!empty)
4480 after = 1;
4481 if (after)
4482 break;
4485 isl_union_map_free(umap_i);
4486 if (after)
4487 break;
4490 if (after < 0 || after)
4491 return after;
4493 return after_in_set(umap, node);
4494 error:
4495 isl_union_map_free(umap_i);
4496 return -1;
4499 /* Is any domain element of "umap" scheduled after any of
4500 * the corresponding image elements by the tree rooted at "node"?
4502 * If "umap" is empty, then clearly there is no such element.
4503 * Otherwise, consider the different types of nodes separately.
4505 static int after_in_tree(__isl_keep isl_union_map *umap,
4506 __isl_keep isl_schedule_node *node)
4508 int empty;
4509 enum isl_schedule_node_type type;
4511 empty = isl_union_map_is_empty(umap);
4512 if (empty < 0)
4513 return -1;
4514 if (empty)
4515 return 0;
4516 if (!node)
4517 return -1;
4519 type = isl_schedule_node_get_type(node);
4520 switch (type) {
4521 case isl_schedule_node_error:
4522 return -1;
4523 case isl_schedule_node_leaf:
4524 return 0;
4525 case isl_schedule_node_band:
4526 return after_in_band(umap, node);
4527 case isl_schedule_node_domain:
4528 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4529 "unexpected internal domain node", return -1);
4530 case isl_schedule_node_context:
4531 return after_in_context(umap, node);
4532 case isl_schedule_node_expansion:
4533 return after_in_expansion(umap, node);
4534 case isl_schedule_node_extension:
4535 return after_in_extension(umap, node);
4536 case isl_schedule_node_filter:
4537 return after_in_filter(umap, node);
4538 case isl_schedule_node_guard:
4539 case isl_schedule_node_mark:
4540 return after_in_child(umap, node);
4541 case isl_schedule_node_set:
4542 return after_in_set(umap, node);
4543 case isl_schedule_node_sequence:
4544 return after_in_sequence(umap, node);
4547 return 1;
4550 /* Is any domain element of "map1" scheduled after any domain
4551 * element of "map2" by the subtree underneath the current band node,
4552 * while at the same time being scheduled together by the current
4553 * band node, i.e., by "map1" and "map2?
4555 * If the child of the current band node is a leaf, then
4556 * no element can be scheduled after any other element.
4558 * Otherwise, we construct a relation between domain elements
4559 * of "map1" and domain elements of "map2" that are scheduled
4560 * together and then check if the subtree underneath the current
4561 * band node determines their relative order.
4563 static int after_in_subtree(__isl_keep isl_ast_build *build,
4564 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4566 isl_schedule_node *node;
4567 isl_map *map;
4568 isl_union_map *umap;
4569 int after;
4571 node = isl_ast_build_get_schedule_node(build);
4572 if (!node)
4573 return -1;
4574 node = isl_schedule_node_child(node, 0);
4575 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4576 isl_schedule_node_free(node);
4577 return 0;
4579 map = isl_map_copy(map2);
4580 map = isl_map_apply_domain(map, isl_map_copy(map1));
4581 umap = isl_union_map_from_map(map);
4582 after = after_in_tree(umap, node);
4583 isl_union_map_free(umap);
4584 isl_schedule_node_free(node);
4585 return after;
4588 /* Internal data for any_scheduled_after.
4590 * "build" is the build in which the AST is constructed.
4591 * "depth" is the number of loops that have already been generated
4592 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4593 * "domain" is an array of set-map pairs corresponding to the different
4594 * iteration domains. The set is the schedule domain, i.e., the domain
4595 * of the inverse schedule, while the map is the inverse schedule itself.
4597 struct isl_any_scheduled_after_data {
4598 isl_ast_build *build;
4599 int depth;
4600 int group_coscheduled;
4601 struct isl_set_map_pair *domain;
4604 /* Is any element of domain "i" scheduled after any element of domain "j"
4605 * (for a common iteration of the first data->depth loops)?
4607 * data->domain[i].set contains the domain of the inverse schedule
4608 * for domain "i", i.e., elements in the schedule domain.
4610 * If we are inside a band of a schedule tree and there is a pair
4611 * of elements in the two domains that is schedule together by
4612 * the current band, then we check if any element of "i" may be schedule
4613 * after element of "j" by the descendants of the band node.
4615 * If data->group_coscheduled is set, then we also return 1 if there
4616 * is any pair of elements in the two domains that are scheduled together.
4618 static isl_bool any_scheduled_after(int i, int j, void *user)
4620 struct isl_any_scheduled_after_data *data = user;
4621 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4622 int pos;
4624 for (pos = data->depth; pos < dim; ++pos) {
4625 int follows;
4627 follows = isl_set_follows_at(data->domain[i].set,
4628 data->domain[j].set, pos);
4630 if (follows < -1)
4631 return isl_bool_error;
4632 if (follows > 0)
4633 return isl_bool_true;
4634 if (follows < 0)
4635 return isl_bool_false;
4638 if (isl_ast_build_has_schedule_node(data->build)) {
4639 int after;
4641 after = after_in_subtree(data->build, data->domain[i].map,
4642 data->domain[j].map);
4643 if (after < 0 || after)
4644 return after;
4647 return data->group_coscheduled;
4650 /* Look for independent components at the current depth and generate code
4651 * for each component separately. The resulting lists of grafts are
4652 * merged in an attempt to combine grafts with identical guards.
4654 * Code for two domains can be generated separately if all the elements
4655 * of one domain are scheduled before (or together with) all the elements
4656 * of the other domain. We therefore consider the graph with as nodes
4657 * the domains and an edge between two nodes if any element of the first
4658 * node is scheduled after any element of the second node.
4659 * If the ast_build_group_coscheduled is set, then we also add an edge if
4660 * there is any pair of elements in the two domains that are scheduled
4661 * together.
4662 * Code is then generated (by generate_component)
4663 * for each of the strongly connected components in this graph
4664 * in their topological order.
4666 * Since the test is performed on the domain of the inverse schedules of
4667 * the different domains, we precompute these domains and store
4668 * them in data.domain.
4670 static __isl_give isl_ast_graft_list *generate_components(
4671 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4673 int i;
4674 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4675 int n = isl_union_map_n_map(executed);
4676 struct isl_any_scheduled_after_data data;
4677 struct isl_set_map_pair *next;
4678 struct isl_tarjan_graph *g = NULL;
4679 isl_ast_graft_list *list = NULL;
4680 int n_domain = 0;
4682 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4683 if (!data.domain)
4684 goto error;
4685 n_domain = n;
4687 next = data.domain;
4688 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4689 goto error;
4691 if (!build)
4692 goto error;
4693 data.build = build;
4694 data.depth = isl_ast_build_get_depth(build);
4695 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4696 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4697 if (!g)
4698 goto error;
4700 list = isl_ast_graft_list_alloc(ctx, 0);
4702 i = 0;
4703 while (list && n) {
4704 isl_ast_graft_list *list_c;
4705 int first = i;
4707 if (g->order[i] == -1)
4708 isl_die(ctx, isl_error_internal, "cannot happen",
4709 goto error);
4710 ++i; --n;
4711 while (g->order[i] != -1) {
4712 ++i; --n;
4715 list_c = generate_component(data.domain,
4716 g->order + first, i - first,
4717 isl_ast_build_copy(build));
4718 list = isl_ast_graft_list_merge(list, list_c, build);
4720 ++i;
4723 if (0)
4724 error: list = isl_ast_graft_list_free(list);
4725 isl_tarjan_graph_free(g);
4726 for (i = 0; i < n_domain; ++i) {
4727 isl_map_free(data.domain[i].map);
4728 isl_set_free(data.domain[i].set);
4730 free(data.domain);
4731 isl_union_map_free(executed);
4732 isl_ast_build_free(build);
4734 return list;
4737 /* Generate code for the next level (and all inner levels).
4739 * If "executed" is empty, i.e., no code needs to be generated,
4740 * then we return an empty list.
4742 * If we have already generated code for all loop levels, then we pass
4743 * control to generate_inner_level.
4745 * If "executed" lives in a single space, i.e., if code needs to be
4746 * generated for a single domain, then there can only be a single
4747 * component and we go directly to generate_shifted_component.
4748 * Otherwise, we call generate_components to detect the components
4749 * and to call generate_component on each of them separately.
4751 static __isl_give isl_ast_graft_list *generate_next_level(
4752 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4754 int depth;
4756 if (!build || !executed)
4757 goto error;
4759 if (isl_union_map_is_empty(executed)) {
4760 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4761 isl_union_map_free(executed);
4762 isl_ast_build_free(build);
4763 return isl_ast_graft_list_alloc(ctx, 0);
4766 depth = isl_ast_build_get_depth(build);
4767 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4768 return generate_inner_level(executed, build);
4770 if (isl_union_map_n_map(executed) == 1)
4771 return generate_shifted_component(executed, build);
4773 return generate_components(executed, build);
4774 error:
4775 isl_union_map_free(executed);
4776 isl_ast_build_free(build);
4777 return NULL;
4780 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4781 * internal, executed and build are the inputs to generate_code.
4782 * list collects the output.
4784 struct isl_generate_code_data {
4785 int internal;
4786 isl_union_map *executed;
4787 isl_ast_build *build;
4789 isl_ast_graft_list *list;
4792 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4794 * [E -> S] -> D
4796 * with E the external build schedule and S the additional schedule "space",
4797 * reformulate the inverse schedule in terms of the internal schedule domain,
4798 * i.e., return
4800 * [I -> S] -> D
4802 * We first obtain a mapping
4804 * I -> E
4806 * take the inverse and the product with S -> S, resulting in
4808 * [I -> S] -> [E -> S]
4810 * Applying the map to the input produces the desired result.
4812 static __isl_give isl_union_map *internal_executed(
4813 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4814 __isl_keep isl_ast_build *build)
4816 isl_map *id, *proj;
4818 proj = isl_ast_build_get_schedule_map(build);
4819 proj = isl_map_reverse(proj);
4820 space = isl_space_map_from_set(isl_space_copy(space));
4821 id = isl_map_identity(space);
4822 proj = isl_map_product(proj, id);
4823 executed = isl_union_map_apply_domain(executed,
4824 isl_union_map_from_map(proj));
4825 return executed;
4828 /* Generate an AST that visits the elements in the range of data->executed
4829 * in the relative order specified by the corresponding domain element(s)
4830 * for those domain elements that belong to "set".
4831 * Add the result to data->list.
4833 * The caller ensures that "set" is a universe domain.
4834 * "space" is the space of the additional part of the schedule.
4835 * It is equal to the space of "set" if build->domain is parametric.
4836 * Otherwise, it is equal to the range of the wrapped space of "set".
4838 * If the build space is not parametric and
4839 * if isl_ast_build_node_from_schedule_map
4840 * was called from an outside user (data->internal not set), then
4841 * the (inverse) schedule refers to the external build domain and needs to
4842 * be transformed to refer to the internal build domain.
4844 * If the build space is parametric, then we add some of the parameter
4845 * constraints to the executed relation. Adding these constraints
4846 * allows for an earlier detection of conflicts in some cases.
4847 * However, we do not want to divide the executed relation into
4848 * more disjuncts than necessary. We therefore approximate
4849 * the constraints on the parameters by a single disjunct set.
4851 * The build is extended to include the additional part of the schedule.
4852 * If the original build space was not parametric, then the options
4853 * in data->build refer only to the additional part of the schedule
4854 * and they need to be adjusted to refer to the complete AST build
4855 * domain.
4857 * After having adjusted inverse schedule and build, we start generating
4858 * code with the outer loop of the current code generation
4859 * in generate_next_level.
4861 * If the original build space was not parametric, we undo the embedding
4862 * on the resulting isl_ast_node_list so that it can be used within
4863 * the outer AST build.
4865 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4866 __isl_take isl_set *set, __isl_take isl_space *space)
4868 isl_union_map *executed;
4869 isl_ast_build *build;
4870 isl_ast_graft_list *list;
4871 int embed;
4873 executed = isl_union_map_copy(data->executed);
4874 executed = isl_union_map_intersect_domain(executed,
4875 isl_union_set_from_set(set));
4877 embed = !isl_set_is_params(data->build->domain);
4878 if (embed && !data->internal)
4879 executed = internal_executed(executed, space, data->build);
4880 if (!embed) {
4881 isl_set *domain;
4882 domain = isl_ast_build_get_domain(data->build);
4883 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4884 executed = isl_union_map_intersect_params(executed, domain);
4887 build = isl_ast_build_copy(data->build);
4888 build = isl_ast_build_product(build, space);
4890 list = generate_next_level(executed, build);
4892 list = isl_ast_graft_list_unembed(list, embed);
4894 data->list = isl_ast_graft_list_concat(data->list, list);
4896 return isl_stat_ok;
4899 /* Generate an AST that visits the elements in the range of data->executed
4900 * in the relative order specified by the corresponding domain element(s)
4901 * for those domain elements that belong to "set".
4902 * Add the result to data->list.
4904 * The caller ensures that "set" is a universe domain.
4906 * If the build space S is not parametric, then the space of "set"
4907 * need to be a wrapped relation with S as domain. That is, it needs
4908 * to be of the form
4910 * [S -> T]
4912 * Check this property and pass control to generate_code_in_space
4913 * passing along T.
4914 * If the build space is not parametric, then T is the space of "set".
4916 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4918 struct isl_generate_code_data *data = user;
4919 isl_space *space, *build_space;
4920 int is_domain;
4922 space = isl_set_get_space(set);
4924 if (isl_set_is_params(data->build->domain))
4925 return generate_code_in_space(data, set, space);
4927 build_space = isl_ast_build_get_space(data->build, data->internal);
4928 space = isl_space_unwrap(space);
4929 is_domain = isl_space_is_domain(build_space, space);
4930 isl_space_free(build_space);
4931 space = isl_space_range(space);
4933 if (is_domain < 0)
4934 goto error;
4935 if (!is_domain)
4936 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4937 "invalid nested schedule space", goto error);
4939 return generate_code_in_space(data, set, space);
4940 error:
4941 isl_set_free(set);
4942 isl_space_free(space);
4943 return isl_stat_error;
4946 /* Generate an AST that visits the elements in the range of "executed"
4947 * in the relative order specified by the corresponding domain element(s).
4949 * "build" is an isl_ast_build that has either been constructed by
4950 * isl_ast_build_from_context or passed to a callback set by
4951 * isl_ast_build_set_create_leaf.
4952 * In the first case, the space of the isl_ast_build is typically
4953 * a parametric space, although this is currently not enforced.
4954 * In the second case, the space is never a parametric space.
4955 * If the space S is not parametric, then the domain space(s) of "executed"
4956 * need to be wrapped relations with S as domain.
4958 * If the domain of "executed" consists of several spaces, then an AST
4959 * is generated for each of them (in arbitrary order) and the results
4960 * are concatenated.
4962 * If "internal" is set, then the domain "S" above refers to the internal
4963 * schedule domain representation. Otherwise, it refers to the external
4964 * representation, as returned by isl_ast_build_get_schedule_space.
4966 * We essentially run over all the spaces in the domain of "executed"
4967 * and call generate_code_set on each of them.
4969 static __isl_give isl_ast_graft_list *generate_code(
4970 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4971 int internal)
4973 isl_ctx *ctx;
4974 struct isl_generate_code_data data = { 0 };
4975 isl_space *space;
4976 isl_union_set *schedule_domain;
4977 isl_union_map *universe;
4979 if (!build)
4980 goto error;
4981 space = isl_ast_build_get_space(build, 1);
4982 space = isl_space_align_params(space,
4983 isl_union_map_get_space(executed));
4984 space = isl_space_align_params(space,
4985 isl_union_map_get_space(build->options));
4986 build = isl_ast_build_align_params(build, isl_space_copy(space));
4987 executed = isl_union_map_align_params(executed, space);
4988 if (!executed || !build)
4989 goto error;
4991 ctx = isl_ast_build_get_ctx(build);
4993 data.internal = internal;
4994 data.executed = executed;
4995 data.build = build;
4996 data.list = isl_ast_graft_list_alloc(ctx, 0);
4998 universe = isl_union_map_universe(isl_union_map_copy(executed));
4999 schedule_domain = isl_union_map_domain(universe);
5000 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5001 &data) < 0)
5002 data.list = isl_ast_graft_list_free(data.list);
5004 isl_union_set_free(schedule_domain);
5005 isl_union_map_free(executed);
5007 isl_ast_build_free(build);
5008 return data.list;
5009 error:
5010 isl_union_map_free(executed);
5011 isl_ast_build_free(build);
5012 return NULL;
5015 /* Generate an AST that visits the elements in the domain of "schedule"
5016 * in the relative order specified by the corresponding image element(s).
5018 * "build" is an isl_ast_build that has either been constructed by
5019 * isl_ast_build_from_context or passed to a callback set by
5020 * isl_ast_build_set_create_leaf.
5021 * In the first case, the space of the isl_ast_build is typically
5022 * a parametric space, although this is currently not enforced.
5023 * In the second case, the space is never a parametric space.
5024 * If the space S is not parametric, then the range space(s) of "schedule"
5025 * need to be wrapped relations with S as domain.
5027 * If the range of "schedule" consists of several spaces, then an AST
5028 * is generated for each of them (in arbitrary order) and the results
5029 * are concatenated.
5031 * We first initialize the local copies of the relevant options.
5032 * We do this here rather than when the isl_ast_build is created
5033 * because the options may have changed between the construction
5034 * of the isl_ast_build and the call to isl_generate_code.
5036 * The main computation is performed on an inverse schedule (with
5037 * the schedule domain in the domain and the elements to be executed
5038 * in the range) called "executed".
5040 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5041 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5043 isl_ast_graft_list *list;
5044 isl_ast_node *node;
5045 isl_union_map *executed;
5047 build = isl_ast_build_copy(build);
5048 build = isl_ast_build_set_single_valued(build, 0);
5049 schedule = isl_union_map_coalesce(schedule);
5050 schedule = isl_union_map_remove_redundancies(schedule);
5051 executed = isl_union_map_reverse(schedule);
5052 list = generate_code(executed, isl_ast_build_copy(build), 0);
5053 node = isl_ast_node_from_graft_list(list, build);
5054 isl_ast_build_free(build);
5056 return node;
5059 /* The old name for isl_ast_build_node_from_schedule_map.
5060 * It is being kept for backward compatibility, but
5061 * it will be removed in the future.
5063 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5064 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5066 return isl_ast_build_node_from_schedule_map(build, schedule);
5069 /* Generate an AST that visits the elements in the domain of "executed"
5070 * in the relative order specified by the band node "node" and its descendants.
5072 * The relation "executed" maps the outer generated loop iterators
5073 * to the domain elements executed by those iterations.
5075 * If the band is empty, we continue with its descendants.
5076 * Otherwise, we extend the build and the inverse schedule with
5077 * the additional space/partial schedule and continue generating
5078 * an AST in generate_next_level.
5079 * As soon as we have extended the inverse schedule with the additional
5080 * partial schedule, we look for equalities that may exists between
5081 * the old and the new part.
5083 static __isl_give isl_ast_graft_list *build_ast_from_band(
5084 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5085 __isl_take isl_union_map *executed)
5087 isl_space *space;
5088 isl_multi_union_pw_aff *extra;
5089 isl_union_map *extra_umap;
5090 isl_ast_graft_list *list;
5091 unsigned n1, n2;
5093 if (!build || !node || !executed)
5094 goto error;
5096 if (isl_schedule_node_band_n_member(node) == 0)
5097 return build_ast_from_child(build, node, executed);
5099 extra = isl_schedule_node_band_get_partial_schedule(node);
5100 extra = isl_multi_union_pw_aff_align_params(extra,
5101 isl_ast_build_get_space(build, 1));
5102 space = isl_multi_union_pw_aff_get_space(extra);
5104 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5105 extra_umap = isl_union_map_reverse(extra_umap);
5107 executed = isl_union_map_domain_product(executed, extra_umap);
5108 executed = isl_union_map_detect_equalities(executed);
5110 n1 = isl_ast_build_dim(build, isl_dim_param);
5111 build = isl_ast_build_product(build, space);
5112 n2 = isl_ast_build_dim(build, isl_dim_param);
5113 if (n2 > n1)
5114 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5115 "band node is not allowed to introduce new parameters",
5116 build = isl_ast_build_free(build));
5117 build = isl_ast_build_set_schedule_node(build, node);
5119 list = generate_next_level(executed, build);
5121 list = isl_ast_graft_list_unembed(list, 1);
5123 return list;
5124 error:
5125 isl_schedule_node_free(node);
5126 isl_union_map_free(executed);
5127 isl_ast_build_free(build);
5128 return NULL;
5131 /* Hoist a list of grafts (in practice containing a single graft)
5132 * from "sub_build" (which includes extra context information)
5133 * to "build".
5135 * In particular, project out all additional parameters introduced
5136 * by the context node from the enforced constraints and the guard
5137 * of the single graft.
5139 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5140 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5141 __isl_keep isl_ast_build *sub_build)
5143 isl_ast_graft *graft;
5144 isl_basic_set *enforced;
5145 isl_set *guard;
5146 unsigned n_param, extra_param;
5148 if (!build || !sub_build)
5149 return isl_ast_graft_list_free(list);
5151 n_param = isl_ast_build_dim(build, isl_dim_param);
5152 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5154 if (extra_param == n_param)
5155 return list;
5157 extra_param -= n_param;
5158 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5159 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5160 n_param, extra_param);
5161 enforced = isl_basic_set_remove_unknown_divs(enforced);
5162 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5163 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5164 n_param, extra_param);
5165 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5166 guard = isl_set_compute_divs(guard);
5167 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5168 build, sub_build);
5169 list = isl_ast_graft_list_from_ast_graft(graft);
5171 return list;
5174 /* Generate an AST that visits the elements in the domain of "executed"
5175 * in the relative order specified by the context node "node"
5176 * and its descendants.
5178 * The relation "executed" maps the outer generated loop iterators
5179 * to the domain elements executed by those iterations.
5181 * The context node may introduce additional parameters as well as
5182 * constraints on the outer schedule dimenions or original parameters.
5184 * We add the extra parameters to a new build and the context
5185 * constraints to both the build and (as a single disjunct)
5186 * to the domain of "executed". Since the context constraints
5187 * are specified in terms of the input schedule, we first need
5188 * to map them to the internal schedule domain.
5190 * After constructing the AST from the descendants of "node",
5191 * we combine the list of grafts into a single graft within
5192 * the new build, in order to be able to exploit the additional
5193 * context constraints during this combination.
5195 * Additionally, if the current node is the outermost node in
5196 * the schedule tree (apart from the root domain node), we generate
5197 * all pending guards, again to be able to exploit the additional
5198 * context constraints. We currently do not do this for internal
5199 * context nodes since we may still want to hoist conditions
5200 * to outer AST nodes.
5202 * If the context node introduced any new parameters, then they
5203 * are removed from the set of enforced constraints and guard
5204 * in hoist_out_of_context.
5206 static __isl_give isl_ast_graft_list *build_ast_from_context(
5207 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5208 __isl_take isl_union_map *executed)
5210 isl_set *context;
5211 isl_space *space;
5212 isl_multi_aff *internal2input;
5213 isl_ast_build *sub_build;
5214 isl_ast_graft_list *list;
5215 int n, depth;
5217 depth = isl_schedule_node_get_tree_depth(node);
5218 space = isl_ast_build_get_space(build, 1);
5219 context = isl_schedule_node_context_get_context(node);
5220 context = isl_set_align_params(context, space);
5221 sub_build = isl_ast_build_copy(build);
5222 space = isl_set_get_space(context);
5223 sub_build = isl_ast_build_align_params(sub_build, space);
5224 internal2input = isl_ast_build_get_internal2input(sub_build);
5225 context = isl_set_preimage_multi_aff(context, internal2input);
5226 sub_build = isl_ast_build_restrict_generated(sub_build,
5227 isl_set_copy(context));
5228 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5229 executed = isl_union_map_intersect_domain(executed,
5230 isl_union_set_from_set(context));
5232 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5233 node, executed);
5234 n = isl_ast_graft_list_n_ast_graft(list);
5235 if (n < 0)
5236 list = isl_ast_graft_list_free(list);
5238 list = isl_ast_graft_list_fuse(list, sub_build);
5239 if (depth == 1)
5240 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5241 sub_build);
5242 if (n >= 1)
5243 list = hoist_out_of_context(list, build, sub_build);
5245 isl_ast_build_free(build);
5246 isl_ast_build_free(sub_build);
5248 return list;
5251 /* Generate an AST that visits the elements in the domain of "executed"
5252 * in the relative order specified by the expansion node "node" and
5253 * its descendants.
5255 * The relation "executed" maps the outer generated loop iterators
5256 * to the domain elements executed by those iterations.
5258 * We expand the domain elements by the expansion and
5259 * continue with the descendants of the node.
5261 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5262 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5263 __isl_take isl_union_map *executed)
5265 isl_union_map *expansion;
5266 unsigned n1, n2;
5268 expansion = isl_schedule_node_expansion_get_expansion(node);
5269 expansion = isl_union_map_align_params(expansion,
5270 isl_union_map_get_space(executed));
5272 n1 = isl_union_map_dim(executed, isl_dim_param);
5273 executed = isl_union_map_apply_range(executed, expansion);
5274 n2 = isl_union_map_dim(executed, isl_dim_param);
5275 if (n2 > n1)
5276 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5277 "expansion node is not allowed to introduce "
5278 "new parameters", goto error);
5280 return build_ast_from_child(build, node, executed);
5281 error:
5282 isl_ast_build_free(build);
5283 isl_schedule_node_free(node);
5284 isl_union_map_free(executed);
5285 return NULL;
5288 /* Generate an AST that visits the elements in the domain of "executed"
5289 * in the relative order specified by the extension node "node" and
5290 * its descendants.
5292 * The relation "executed" maps the outer generated loop iterators
5293 * to the domain elements executed by those iterations.
5295 * Extend the inverse schedule with the extension applied to current
5296 * set of generated constraints. Since the extension if formulated
5297 * in terms of the input schedule, it first needs to be transformed
5298 * to refer to the internal schedule.
5300 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5301 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5302 __isl_take isl_union_map *executed)
5304 isl_union_set *schedule_domain;
5305 isl_union_map *extension;
5306 isl_set *set;
5308 set = isl_ast_build_get_generated(build);
5309 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5310 schedule_domain = isl_union_set_from_set(set);
5312 extension = isl_schedule_node_extension_get_extension(node);
5314 extension = isl_union_map_preimage_domain_multi_aff(extension,
5315 isl_multi_aff_copy(build->internal2input));
5316 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5317 extension = isl_ast_build_substitute_values_union_map_domain(build,
5318 extension);
5319 executed = isl_union_map_union(executed, extension);
5321 return build_ast_from_child(build, node, executed);
5324 /* Generate an AST that visits the elements in the domain of "executed"
5325 * in the relative order specified by the filter node "node" and
5326 * its descendants.
5328 * The relation "executed" maps the outer generated loop iterators
5329 * to the domain elements executed by those iterations.
5331 * We simply intersect the iteration domain (i.e., the range of "executed")
5332 * with the filter and continue with the descendants of the node,
5333 * unless the resulting inverse schedule is empty, in which
5334 * case we return an empty list.
5336 * If the result of the intersection is equal to the original "executed"
5337 * relation, then keep the original representation since the intersection
5338 * may have unnecessarily broken up the relation into a greater number
5339 * of disjuncts.
5341 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5342 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5343 __isl_take isl_union_map *executed)
5345 isl_ctx *ctx;
5346 isl_union_set *filter;
5347 isl_union_map *orig;
5348 isl_ast_graft_list *list;
5349 int empty;
5350 isl_bool unchanged;
5351 unsigned n1, n2;
5353 orig = isl_union_map_copy(executed);
5354 if (!build || !node || !executed)
5355 goto error;
5357 filter = isl_schedule_node_filter_get_filter(node);
5358 filter = isl_union_set_align_params(filter,
5359 isl_union_map_get_space(executed));
5360 n1 = isl_union_map_dim(executed, isl_dim_param);
5361 executed = isl_union_map_intersect_range(executed, filter);
5362 n2 = isl_union_map_dim(executed, isl_dim_param);
5363 if (n2 > n1)
5364 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5365 "filter node is not allowed to introduce "
5366 "new parameters", goto error);
5368 unchanged = isl_union_map_is_subset(orig, executed);
5369 empty = isl_union_map_is_empty(executed);
5370 if (unchanged < 0 || empty < 0)
5371 goto error;
5372 if (unchanged) {
5373 isl_union_map_free(executed);
5374 return build_ast_from_child(build, node, orig);
5376 isl_union_map_free(orig);
5377 if (!empty)
5378 return build_ast_from_child(build, node, executed);
5380 ctx = isl_ast_build_get_ctx(build);
5381 list = isl_ast_graft_list_alloc(ctx, 0);
5382 isl_ast_build_free(build);
5383 isl_schedule_node_free(node);
5384 isl_union_map_free(executed);
5385 return list;
5386 error:
5387 isl_ast_build_free(build);
5388 isl_schedule_node_free(node);
5389 isl_union_map_free(executed);
5390 isl_union_map_free(orig);
5391 return NULL;
5394 /* Generate an AST that visits the elements in the domain of "executed"
5395 * in the relative order specified by the guard node "node" and
5396 * its descendants.
5398 * The relation "executed" maps the outer generated loop iterators
5399 * to the domain elements executed by those iterations.
5401 * Ensure that the associated guard is enforced by the outer AST
5402 * constructs by adding it to the guard of the graft.
5403 * Since we know that we will enforce the guard, we can also include it
5404 * in the generated constraints used to construct an AST for
5405 * the descendant nodes.
5407 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5408 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5409 __isl_take isl_union_map *executed)
5411 isl_space *space;
5412 isl_set *guard, *hoisted;
5413 isl_basic_set *enforced;
5414 isl_ast_build *sub_build;
5415 isl_ast_graft *graft;
5416 isl_ast_graft_list *list;
5417 unsigned n1, n2;
5419 space = isl_ast_build_get_space(build, 1);
5420 guard = isl_schedule_node_guard_get_guard(node);
5421 n1 = isl_space_dim(space, isl_dim_param);
5422 guard = isl_set_align_params(guard, space);
5423 n2 = isl_set_dim(guard, isl_dim_param);
5424 if (n2 > n1)
5425 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5426 "guard node is not allowed to introduce "
5427 "new parameters", guard = isl_set_free(guard));
5428 guard = isl_set_preimage_multi_aff(guard,
5429 isl_multi_aff_copy(build->internal2input));
5430 guard = isl_ast_build_specialize(build, guard);
5431 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5433 sub_build = isl_ast_build_copy(build);
5434 sub_build = isl_ast_build_restrict_generated(sub_build,
5435 isl_set_copy(guard));
5437 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5438 node, executed);
5440 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5441 if (isl_set_n_basic_set(hoisted) > 1)
5442 list = isl_ast_graft_list_gist_guards(list,
5443 isl_set_copy(hoisted));
5444 guard = isl_set_intersect(guard, hoisted);
5445 enforced = extract_shared_enforced(list, build);
5446 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5447 build, sub_build);
5449 isl_ast_build_free(sub_build);
5450 isl_ast_build_free(build);
5451 return isl_ast_graft_list_from_ast_graft(graft);
5454 /* Call the before_each_mark callback, if requested by the user.
5456 * Return 0 on success and -1 on error.
5458 * The caller is responsible for recording the current inverse schedule
5459 * in "build".
5461 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5462 __isl_keep isl_ast_build *build)
5464 if (!build)
5465 return isl_stat_error;
5466 if (!build->before_each_mark)
5467 return isl_stat_ok;
5468 return build->before_each_mark(mark, build,
5469 build->before_each_mark_user);
5472 /* Call the after_each_mark callback, if requested by the user.
5474 * The caller is responsible for recording the current inverse schedule
5475 * in "build".
5477 static __isl_give isl_ast_graft *after_each_mark(
5478 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5480 if (!graft || !build)
5481 return isl_ast_graft_free(graft);
5482 if (!build->after_each_mark)
5483 return graft;
5484 graft->node = build->after_each_mark(graft->node, build,
5485 build->after_each_mark_user);
5486 if (!graft->node)
5487 return isl_ast_graft_free(graft);
5488 return graft;
5492 /* Generate an AST that visits the elements in the domain of "executed"
5493 * in the relative order specified by the mark node "node" and
5494 * its descendants.
5496 * The relation "executed" maps the outer generated loop iterators
5497 * to the domain elements executed by those iterations.
5499 * Since we may be calling before_each_mark and after_each_mark
5500 * callbacks, we record the current inverse schedule in the build.
5502 * We generate an AST for the child of the mark node, combine
5503 * the graft list into a single graft and then insert the mark
5504 * in the AST of that single graft.
5506 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5507 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5508 __isl_take isl_union_map *executed)
5510 isl_id *mark;
5511 isl_ast_graft *graft;
5512 isl_ast_graft_list *list;
5513 int n;
5515 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5517 mark = isl_schedule_node_mark_get_id(node);
5518 if (before_each_mark(mark, build) < 0)
5519 node = isl_schedule_node_free(node);
5521 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5522 list = isl_ast_graft_list_fuse(list, build);
5523 n = isl_ast_graft_list_n_ast_graft(list);
5524 if (n < 0)
5525 list = isl_ast_graft_list_free(list);
5526 if (n == 0) {
5527 isl_id_free(mark);
5528 } else {
5529 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5530 graft = isl_ast_graft_insert_mark(graft, mark);
5531 graft = after_each_mark(graft, build);
5532 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5534 isl_ast_build_free(build);
5536 return list;
5539 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5540 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5541 __isl_take isl_union_map *executed);
5543 /* Generate an AST that visits the elements in the domain of "executed"
5544 * in the relative order specified by the sequence (or set) node "node" and
5545 * its descendants.
5547 * The relation "executed" maps the outer generated loop iterators
5548 * to the domain elements executed by those iterations.
5550 * We simply generate an AST for each of the children and concatenate
5551 * the results.
5553 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5554 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5555 __isl_take isl_union_map *executed)
5557 int i, n;
5558 isl_ctx *ctx;
5559 isl_ast_graft_list *list;
5561 ctx = isl_ast_build_get_ctx(build);
5562 list = isl_ast_graft_list_alloc(ctx, 0);
5564 n = isl_schedule_node_n_children(node);
5565 for (i = 0; i < n; ++i) {
5566 isl_schedule_node *child;
5567 isl_ast_graft_list *list_i;
5569 child = isl_schedule_node_get_child(node, i);
5570 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5571 child, isl_union_map_copy(executed));
5572 list = isl_ast_graft_list_concat(list, list_i);
5574 isl_ast_build_free(build);
5575 isl_schedule_node_free(node);
5576 isl_union_map_free(executed);
5578 return list;
5581 /* Generate an AST that visits the elements in the domain of "executed"
5582 * in the relative order specified by the node "node" and its descendants.
5584 * The relation "executed" maps the outer generated loop iterators
5585 * to the domain elements executed by those iterations.
5587 * If the node is a leaf, then we pass control to generate_inner_level.
5588 * Note that the current build does not refer to any band node, so
5589 * that generate_inner_level will not try to visit the child of
5590 * the leaf node.
5592 * The other node types are handled in separate functions.
5593 * Set nodes are currently treated in the same way as sequence nodes.
5594 * The children of a set node may be executed in any order,
5595 * including the order of the children.
5597 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5598 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5599 __isl_take isl_union_map *executed)
5601 enum isl_schedule_node_type type;
5603 type = isl_schedule_node_get_type(node);
5605 switch (type) {
5606 case isl_schedule_node_error:
5607 goto error;
5608 case isl_schedule_node_leaf:
5609 isl_schedule_node_free(node);
5610 return generate_inner_level(executed, build);
5611 case isl_schedule_node_band:
5612 return build_ast_from_band(build, node, executed);
5613 case isl_schedule_node_context:
5614 return build_ast_from_context(build, node, executed);
5615 case isl_schedule_node_domain:
5616 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5617 "unexpected internal domain node", goto error);
5618 case isl_schedule_node_expansion:
5619 return build_ast_from_expansion(build, node, executed);
5620 case isl_schedule_node_extension:
5621 return build_ast_from_extension(build, node, executed);
5622 case isl_schedule_node_filter:
5623 return build_ast_from_filter(build, node, executed);
5624 case isl_schedule_node_guard:
5625 return build_ast_from_guard(build, node, executed);
5626 case isl_schedule_node_mark:
5627 return build_ast_from_mark(build, node, executed);
5628 case isl_schedule_node_sequence:
5629 case isl_schedule_node_set:
5630 return build_ast_from_sequence(build, node, executed);
5633 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5634 "unhandled type", goto error);
5635 error:
5636 isl_union_map_free(executed);
5637 isl_schedule_node_free(node);
5638 isl_ast_build_free(build);
5640 return NULL;
5643 /* Generate an AST that visits the elements in the domain of "executed"
5644 * in the relative order specified by the (single) child of "node" and
5645 * its descendants.
5647 * The relation "executed" maps the outer generated loop iterators
5648 * to the domain elements executed by those iterations.
5650 * This function is never called on a leaf, set or sequence node,
5651 * so the node always has exactly one child.
5653 static __isl_give isl_ast_graft_list *build_ast_from_child(
5654 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5655 __isl_take isl_union_map *executed)
5657 node = isl_schedule_node_child(node, 0);
5658 return build_ast_from_schedule_node(build, node, executed);
5661 /* Generate an AST that visits the elements in the domain of the domain
5662 * node "node" in the relative order specified by its descendants.
5664 * An initial inverse schedule is created that maps a zero-dimensional
5665 * schedule space to the node domain.
5666 * The input "build" is assumed to have a parametric domain and
5667 * is replaced by the same zero-dimensional schedule space.
5669 * We also add some of the parameter constraints in the build domain
5670 * to the executed relation. Adding these constraints
5671 * allows for an earlier detection of conflicts in some cases.
5672 * However, we do not want to divide the executed relation into
5673 * more disjuncts than necessary. We therefore approximate
5674 * the constraints on the parameters by a single disjunct set.
5676 static __isl_give isl_ast_node *build_ast_from_domain(
5677 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5679 isl_ctx *ctx;
5680 isl_union_set *domain, *schedule_domain;
5681 isl_union_map *executed;
5682 isl_space *space;
5683 isl_set *set;
5684 isl_ast_graft_list *list;
5685 isl_ast_node *ast;
5686 int is_params;
5688 if (!build)
5689 goto error;
5691 ctx = isl_ast_build_get_ctx(build);
5692 space = isl_ast_build_get_space(build, 1);
5693 is_params = isl_space_is_params(space);
5694 isl_space_free(space);
5695 if (is_params < 0)
5696 goto error;
5697 if (!is_params)
5698 isl_die(ctx, isl_error_unsupported,
5699 "expecting parametric initial context", goto error);
5701 domain = isl_schedule_node_domain_get_domain(node);
5702 domain = isl_union_set_coalesce(domain);
5704 space = isl_union_set_get_space(domain);
5705 space = isl_space_set_from_params(space);
5706 build = isl_ast_build_product(build, space);
5708 set = isl_ast_build_get_domain(build);
5709 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5710 schedule_domain = isl_union_set_from_set(set);
5712 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5713 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5714 ast = isl_ast_node_from_graft_list(list, build);
5715 isl_ast_build_free(build);
5717 return ast;
5718 error:
5719 isl_schedule_node_free(node);
5720 isl_ast_build_free(build);
5721 return NULL;
5724 /* Generate an AST that visits the elements in the domain of "schedule"
5725 * in the relative order specified by the schedule tree.
5727 * "build" is an isl_ast_build that has been created using
5728 * isl_ast_build_alloc or isl_ast_build_from_context based
5729 * on a parametric set.
5731 * The construction starts at the root node of the schedule,
5732 * which is assumed to be a domain node.
5734 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5735 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5737 isl_ctx *ctx;
5738 isl_schedule_node *node;
5740 if (!build || !schedule)
5741 goto error;
5743 ctx = isl_ast_build_get_ctx(build);
5745 node = isl_schedule_get_root(schedule);
5746 isl_schedule_free(schedule);
5748 build = isl_ast_build_copy(build);
5749 build = isl_ast_build_set_single_valued(build, 0);
5750 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5751 isl_die(ctx, isl_error_unsupported,
5752 "expecting root domain node",
5753 build = isl_ast_build_free(build));
5754 return build_ast_from_domain(build, node);
5755 error:
5756 isl_schedule_free(schedule);
5757 return NULL;