isl_local_space_divs_known: extract out isl_local_divs_known
[isl.git] / isl_ast_codegen.c
blob8f2e9c7c313fbb8fbb138f03b61a4a442d6304a8
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;
1459 isl_bool eliminated;
1460 isl_basic_set *hull;
1461 isl_basic_set *enforced;
1462 isl_set *guard, *hoisted;
1463 isl_ast_node *node = NULL;
1464 isl_ast_graft *graft;
1465 isl_ast_graft_list *children;
1466 isl_ast_build *sub_build;
1467 isl_ast_build *body_build;
1469 domain = isl_ast_build_eliminate_divs(build, domain);
1470 domain = isl_set_detect_equalities(domain);
1471 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1472 bounds = isl_basic_set_intersect(bounds, hull);
1473 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1475 depth = isl_ast_build_get_depth(build);
1476 sub_build = isl_ast_build_copy(build);
1477 bounds = isl_basic_set_remove_redundancies(bounds);
1478 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1479 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1480 isl_basic_set_copy(bounds));
1481 degenerate = isl_ast_build_has_value(sub_build);
1482 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1483 if (degenerate < 0 || eliminated < 0)
1484 executed = isl_union_map_free(executed);
1485 if (!degenerate)
1486 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1487 sub_build = isl_ast_build_set_pending_generated(sub_build,
1488 isl_basic_set_copy(bounds));
1489 if (eliminated)
1490 executed = plug_in_values(executed, sub_build);
1491 else
1492 node = create_for(build, degenerate);
1494 body_build = isl_ast_build_copy(sub_build);
1495 body_build = isl_ast_build_increase_depth(body_build);
1496 if (!eliminated)
1497 node = before_each_for(node, body_build);
1498 children = generate_next_level(executed,
1499 isl_ast_build_copy(body_build));
1501 enforced = extract_shared_enforced(children, build);
1502 guard = extract_pending(sub_build, enforced);
1503 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1504 if (isl_set_n_basic_set(hoisted) > 1)
1505 children = isl_ast_graft_list_gist_guards(children,
1506 isl_set_copy(hoisted));
1507 guard = isl_set_intersect(guard, hoisted);
1508 if (!eliminated)
1509 guard = add_implied_guards(guard, degenerate, bounds, build);
1511 graft = isl_ast_graft_alloc_from_children(children,
1512 isl_set_copy(guard), enforced, build, sub_build);
1514 if (!eliminated) {
1515 isl_ast_build *for_build;
1517 graft = isl_ast_graft_insert_for(graft, node);
1518 for_build = isl_ast_build_copy(build);
1519 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1520 isl_set_copy(guard));
1521 if (degenerate)
1522 graft = refine_degenerate(graft, for_build, sub_build);
1523 else
1524 graft = refine_generic(graft, bounds,
1525 domain, for_build);
1526 isl_ast_build_free(for_build);
1528 isl_set_free(guard);
1529 if (!eliminated)
1530 graft = after_each_for(graft, body_build);
1532 isl_ast_build_free(body_build);
1533 isl_ast_build_free(sub_build);
1534 isl_ast_build_free(build);
1535 isl_basic_set_free(bounds);
1536 isl_set_free(domain);
1538 return graft;
1541 /* Internal data structure for checking if all constraints involving
1542 * the input dimension "depth" are such that the other coefficients
1543 * are multiples of "m", reducing "m" if they are not.
1544 * If "m" is reduced all the way down to "1", then the check has failed
1545 * and we break out of the iteration.
1547 struct isl_check_scaled_data {
1548 int depth;
1549 isl_val *m;
1552 /* If constraint "c" involves the input dimension data->depth,
1553 * then make sure that all the other coefficients are multiples of data->m,
1554 * reducing data->m if needed.
1555 * Break out of the iteration if data->m has become equal to "1".
1557 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1558 void *user)
1560 struct isl_check_scaled_data *data = user;
1561 int i, j, n;
1562 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1563 isl_dim_div };
1565 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1566 isl_constraint_free(c);
1567 return isl_stat_ok;
1570 for (i = 0; i < 4; ++i) {
1571 n = isl_constraint_dim(c, t[i]);
1572 for (j = 0; j < n; ++j) {
1573 isl_val *d;
1575 if (t[i] == isl_dim_in && j == data->depth)
1576 continue;
1577 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1578 continue;
1579 d = isl_constraint_get_coefficient_val(c, t[i], j);
1580 data->m = isl_val_gcd(data->m, d);
1581 if (isl_val_is_one(data->m))
1582 break;
1584 if (j < n)
1585 break;
1588 isl_constraint_free(c);
1590 return i < 4 ? isl_stat_error : isl_stat_ok;
1593 /* For each constraint of "bmap" that involves the input dimension data->depth,
1594 * make sure that all the other coefficients are multiples of data->m,
1595 * reducing data->m if needed.
1596 * Break out of the iteration if data->m has become equal to "1".
1598 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1599 void *user)
1601 isl_stat r;
1603 r = isl_basic_map_foreach_constraint(bmap,
1604 &constraint_check_scaled, user);
1605 isl_basic_map_free(bmap);
1607 return r;
1610 /* For each constraint of "map" that involves the input dimension data->depth,
1611 * make sure that all the other coefficients are multiples of data->m,
1612 * reducing data->m if needed.
1613 * Break out of the iteration if data->m has become equal to "1".
1615 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1617 isl_stat r;
1619 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1620 isl_map_free(map);
1622 return r;
1625 /* Create an AST node for the current dimension based on
1626 * the schedule domain "bounds" and return the node encapsulated
1627 * in an isl_ast_graft.
1629 * "executed" is the current inverse schedule, taking into account
1630 * the bounds in "bounds"
1631 * "domain" is the domain of "executed", with inner dimensions projected out.
1634 * Before moving on to the actual AST node construction in create_node_scaled,
1635 * we first check if the current dimension is strided and if we can scale
1636 * down this stride. Note that we only do this if the ast_build_scale_strides
1637 * option is set.
1639 * In particular, let the current dimension take on values
1641 * f + s a
1643 * with a an integer. We check if we can find an integer m that (obviously)
1644 * divides both f and s.
1646 * If so, we check if the current dimension only appears in constraints
1647 * where the coefficients of the other variables are multiples of m.
1648 * We perform this extra check to avoid the risk of introducing
1649 * divisions by scaling down the current dimension.
1651 * If so, we scale the current dimension down by a factor of m.
1652 * That is, we plug in
1654 * i = m i' (1)
1656 * Note that in principle we could always scale down strided loops
1657 * by plugging in
1659 * i = f + s i'
1661 * but this may result in i' taking on larger values than the original i,
1662 * due to the shift by "f".
1663 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1665 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1666 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1667 __isl_take isl_ast_build *build)
1669 struct isl_check_scaled_data data;
1670 isl_ctx *ctx;
1671 isl_aff *offset;
1672 isl_val *d;
1674 ctx = isl_ast_build_get_ctx(build);
1675 if (!isl_options_get_ast_build_scale_strides(ctx))
1676 return create_node_scaled(executed, bounds, domain, build);
1678 data.depth = isl_ast_build_get_depth(build);
1679 if (!isl_ast_build_has_stride(build, data.depth))
1680 return create_node_scaled(executed, bounds, domain, build);
1682 offset = isl_ast_build_get_offset(build, data.depth);
1683 data.m = isl_ast_build_get_stride(build, data.depth);
1684 if (!data.m)
1685 offset = isl_aff_free(offset);
1686 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1687 d = isl_aff_get_denominator_val(offset);
1688 if (!d)
1689 executed = isl_union_map_free(executed);
1691 if (executed && isl_val_is_divisible_by(data.m, d))
1692 data.m = isl_val_div(data.m, d);
1693 else {
1694 data.m = isl_val_set_si(data.m, 1);
1695 isl_val_free(d);
1698 if (!isl_val_is_one(data.m)) {
1699 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1700 &data) < 0 &&
1701 !isl_val_is_one(data.m))
1702 executed = isl_union_map_free(executed);
1705 if (!isl_val_is_one(data.m)) {
1706 isl_space *space;
1707 isl_multi_aff *ma;
1708 isl_aff *aff;
1709 isl_map *map;
1710 isl_union_map *umap;
1712 space = isl_ast_build_get_space(build, 1);
1713 space = isl_space_map_from_set(space);
1714 ma = isl_multi_aff_identity(space);
1715 aff = isl_multi_aff_get_aff(ma, data.depth);
1716 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1717 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1719 bounds = isl_basic_set_preimage_multi_aff(bounds,
1720 isl_multi_aff_copy(ma));
1721 domain = isl_set_preimage_multi_aff(domain,
1722 isl_multi_aff_copy(ma));
1723 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1724 umap = isl_union_map_from_map(map);
1725 executed = isl_union_map_apply_domain(executed,
1726 isl_union_map_copy(umap));
1727 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1728 umap);
1730 isl_aff_free(offset);
1731 isl_val_free(data.m);
1733 return create_node_scaled(executed, bounds, domain, build);
1736 /* Add the basic set to the list that "user" points to.
1738 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1740 isl_basic_set_list **list = user;
1742 *list = isl_basic_set_list_add(*list, bset);
1744 return isl_stat_ok;
1747 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1749 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1750 __isl_take isl_set *set)
1752 int n;
1753 isl_ctx *ctx;
1754 isl_basic_set_list *list;
1756 if (!set)
1757 return NULL;
1759 ctx = isl_set_get_ctx(set);
1761 n = isl_set_n_basic_set(set);
1762 list = isl_basic_set_list_alloc(ctx, n);
1763 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1764 list = isl_basic_set_list_free(list);
1766 isl_set_free(set);
1767 return list;
1770 /* Generate code for the schedule domain "bounds"
1771 * and add the result to "list".
1773 * We mainly detect strides here and check if the bounds do not
1774 * conflict with the current build domain
1775 * and then pass over control to create_node.
1777 * "bounds" reflects the bounds on the current dimension and possibly
1778 * some extra conditions on outer dimensions.
1779 * It does not, however, include any divs involving the current dimension,
1780 * so it does not capture any stride constraints.
1781 * We therefore need to compute that part of the schedule domain that
1782 * intersects with "bounds" and derive the strides from the result.
1784 static __isl_give isl_ast_graft_list *add_node(
1785 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1786 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1788 isl_ast_graft *graft;
1789 isl_set *domain = NULL;
1790 isl_union_set *uset;
1791 int empty, disjoint;
1793 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1794 executed = isl_union_map_intersect_domain(executed, uset);
1795 empty = isl_union_map_is_empty(executed);
1796 if (empty < 0)
1797 goto error;
1798 if (empty)
1799 goto done;
1801 uset = isl_union_map_domain(isl_union_map_copy(executed));
1802 domain = isl_set_from_union_set(uset);
1803 domain = isl_ast_build_specialize(build, domain);
1805 domain = isl_set_compute_divs(domain);
1806 domain = isl_ast_build_eliminate_inner(build, domain);
1807 disjoint = isl_set_is_disjoint(domain, build->domain);
1808 if (disjoint < 0)
1809 goto error;
1810 if (disjoint)
1811 goto done;
1813 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1815 graft = create_node(executed, bounds, domain,
1816 isl_ast_build_copy(build));
1817 list = isl_ast_graft_list_add(list, graft);
1818 isl_ast_build_free(build);
1819 return list;
1820 error:
1821 list = isl_ast_graft_list_free(list);
1822 done:
1823 isl_set_free(domain);
1824 isl_basic_set_free(bounds);
1825 isl_union_map_free(executed);
1826 isl_ast_build_free(build);
1827 return list;
1830 /* Does any element of i follow or coincide with any element of j
1831 * at the current depth for equal values of the outer dimensions?
1833 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1834 __isl_keep isl_basic_set *j, void *user)
1836 int depth = *(int *) user;
1837 isl_basic_map *test;
1838 isl_bool empty;
1839 int l;
1841 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1842 isl_basic_set_copy(j));
1843 for (l = 0; l < depth; ++l)
1844 test = isl_basic_map_equate(test, isl_dim_in, l,
1845 isl_dim_out, l);
1846 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1847 isl_dim_out, depth);
1848 empty = isl_basic_map_is_empty(test);
1849 isl_basic_map_free(test);
1851 return empty < 0 ? isl_bool_error : !empty;
1854 /* Split up each element of "list" into a part that is related to "bset"
1855 * according to "gt" and a part that is not.
1856 * Return a list that consist of "bset" and all the pieces.
1858 static __isl_give isl_basic_set_list *add_split_on(
1859 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1860 __isl_keep isl_basic_map *gt)
1862 int i, n;
1863 isl_basic_set_list *res;
1865 if (!list)
1866 bset = isl_basic_set_free(bset);
1868 gt = isl_basic_map_copy(gt);
1869 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1870 n = isl_basic_set_list_n_basic_set(list);
1871 res = isl_basic_set_list_from_basic_set(bset);
1872 for (i = 0; res && i < n; ++i) {
1873 isl_basic_set *bset;
1874 isl_set *set1, *set2;
1875 isl_basic_map *bmap;
1876 int empty;
1878 bset = isl_basic_set_list_get_basic_set(list, i);
1879 bmap = isl_basic_map_copy(gt);
1880 bmap = isl_basic_map_intersect_range(bmap, bset);
1881 bset = isl_basic_map_range(bmap);
1882 empty = isl_basic_set_is_empty(bset);
1883 if (empty < 0)
1884 res = isl_basic_set_list_free(res);
1885 if (empty) {
1886 isl_basic_set_free(bset);
1887 bset = isl_basic_set_list_get_basic_set(list, i);
1888 res = isl_basic_set_list_add(res, bset);
1889 continue;
1892 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1893 set1 = isl_set_from_basic_set(bset);
1894 bset = isl_basic_set_list_get_basic_set(list, i);
1895 set2 = isl_set_from_basic_set(bset);
1896 set1 = isl_set_subtract(set2, set1);
1897 set1 = isl_set_make_disjoint(set1);
1899 res = isl_basic_set_list_concat(res,
1900 isl_basic_set_list_from_set(set1));
1902 isl_basic_map_free(gt);
1903 isl_basic_set_list_free(list);
1904 return res;
1907 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1908 __isl_keep isl_basic_set_list *domain_list,
1909 __isl_keep isl_union_map *executed,
1910 __isl_keep isl_ast_build *build);
1912 /* Internal data structure for add_nodes.
1914 * "executed" and "build" are extra arguments to be passed to add_node.
1915 * "list" collects the results.
1917 struct isl_add_nodes_data {
1918 isl_union_map *executed;
1919 isl_ast_build *build;
1921 isl_ast_graft_list *list;
1924 /* Generate code for the schedule domains in "scc"
1925 * and add the results to "list".
1927 * The domains in "scc" form a strongly connected component in the ordering.
1928 * If the number of domains in "scc" is larger than 1, then this means
1929 * that we cannot determine a valid ordering for the domains in the component.
1930 * This should be fairly rare because the individual domains
1931 * have been made disjoint first.
1932 * The problem is that the domains may be integrally disjoint but not
1933 * rationally disjoint. For example, we may have domains
1935 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1937 * These two domains have an empty intersection, but their rational
1938 * relaxations do intersect. It is impossible to order these domains
1939 * in the second dimension because the first should be ordered before
1940 * the second for outer dimension equal to 0, while it should be ordered
1941 * after for outer dimension equal to 1.
1943 * This may happen in particular in case of unrolling since the domain
1944 * of each slice is replaced by its simple hull.
1946 * For each basic set i in "scc" and for each of the following basic sets j,
1947 * we split off that part of the basic set i that shares the outer dimensions
1948 * with j and lies before j in the current dimension.
1949 * We collect all the pieces in a new list that replaces "scc".
1951 * While the elements in "scc" should be disjoint, we double-check
1952 * this property to avoid running into an infinite recursion in case
1953 * they intersect due to some internal error.
1955 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1957 struct isl_add_nodes_data *data = user;
1958 int i, n, depth;
1959 isl_basic_set *bset, *first;
1960 isl_basic_set_list *list;
1961 isl_space *space;
1962 isl_basic_map *gt;
1964 n = isl_basic_set_list_n_basic_set(scc);
1965 bset = isl_basic_set_list_get_basic_set(scc, 0);
1966 if (n == 1) {
1967 isl_basic_set_list_free(scc);
1968 data->list = add_node(data->list,
1969 isl_union_map_copy(data->executed), bset,
1970 isl_ast_build_copy(data->build));
1971 return data->list ? isl_stat_ok : isl_stat_error;
1974 depth = isl_ast_build_get_depth(data->build);
1975 space = isl_basic_set_get_space(bset);
1976 space = isl_space_map_from_set(space);
1977 gt = isl_basic_map_universe(space);
1978 for (i = 0; i < depth; ++i)
1979 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1980 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1982 first = isl_basic_set_copy(bset);
1983 list = isl_basic_set_list_from_basic_set(bset);
1984 for (i = 1; i < n; ++i) {
1985 int disjoint;
1987 bset = isl_basic_set_list_get_basic_set(scc, i);
1989 disjoint = isl_basic_set_is_disjoint(bset, first);
1990 if (disjoint < 0)
1991 list = isl_basic_set_list_free(list);
1992 else if (!disjoint)
1993 isl_die(isl_basic_set_list_get_ctx(scc),
1994 isl_error_internal,
1995 "basic sets in scc are assumed to be disjoint",
1996 list = isl_basic_set_list_free(list));
1998 list = add_split_on(list, bset, gt);
2000 isl_basic_set_free(first);
2001 isl_basic_map_free(gt);
2002 isl_basic_set_list_free(scc);
2003 scc = list;
2004 data->list = isl_ast_graft_list_concat(data->list,
2005 generate_sorted_domains(scc, data->executed, data->build));
2006 isl_basic_set_list_free(scc);
2008 return data->list ? isl_stat_ok : isl_stat_error;
2011 /* Sort the domains in "domain_list" according to the execution order
2012 * at the current depth (for equal values of the outer dimensions),
2013 * generate code for each of them, collecting the results in a list.
2014 * If no code is generated (because the intersection of the inverse schedule
2015 * with the domains turns out to be empty), then an empty list is returned.
2017 * The caller is responsible for ensuring that the basic sets in "domain_list"
2018 * are pair-wise disjoint. It can, however, in principle happen that
2019 * two basic sets should be ordered one way for one value of the outer
2020 * dimensions and the other way for some other value of the outer dimensions.
2021 * We therefore play safe and look for strongly connected components.
2022 * The function add_nodes takes care of handling non-trivial components.
2024 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2025 __isl_keep isl_basic_set_list *domain_list,
2026 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2028 isl_ctx *ctx;
2029 struct isl_add_nodes_data data;
2030 int depth;
2031 int n;
2033 if (!domain_list)
2034 return NULL;
2036 ctx = isl_basic_set_list_get_ctx(domain_list);
2037 n = isl_basic_set_list_n_basic_set(domain_list);
2038 data.list = isl_ast_graft_list_alloc(ctx, n);
2039 if (n == 0)
2040 return data.list;
2041 if (n == 1)
2042 return add_node(data.list, isl_union_map_copy(executed),
2043 isl_basic_set_list_get_basic_set(domain_list, 0),
2044 isl_ast_build_copy(build));
2046 depth = isl_ast_build_get_depth(build);
2047 data.executed = executed;
2048 data.build = build;
2049 if (isl_basic_set_list_foreach_scc(domain_list,
2050 &domain_follows_at_depth, &depth,
2051 &add_nodes, &data) < 0)
2052 data.list = isl_ast_graft_list_free(data.list);
2054 return data.list;
2057 /* Do i and j share any values for the outer dimensions?
2059 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2060 __isl_keep isl_basic_set *j, void *user)
2062 int depth = *(int *) user;
2063 isl_basic_map *test;
2064 isl_bool empty;
2065 int l;
2067 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2068 isl_basic_set_copy(j));
2069 for (l = 0; l < depth; ++l)
2070 test = isl_basic_map_equate(test, isl_dim_in, l,
2071 isl_dim_out, l);
2072 empty = isl_basic_map_is_empty(test);
2073 isl_basic_map_free(test);
2075 return empty < 0 ? isl_bool_error : !empty;
2078 /* Internal data structure for generate_sorted_domains_wrap.
2080 * "n" is the total number of basic sets
2081 * "executed" and "build" are extra arguments to be passed
2082 * to generate_sorted_domains.
2084 * "single" is set to 1 by generate_sorted_domains_wrap if there
2085 * is only a single component.
2086 * "list" collects the results.
2088 struct isl_ast_generate_parallel_domains_data {
2089 int n;
2090 isl_union_map *executed;
2091 isl_ast_build *build;
2093 int single;
2094 isl_ast_graft_list *list;
2097 /* Call generate_sorted_domains on "scc", fuse the result into a list
2098 * with either zero or one graft and collect the these single element
2099 * lists into data->list.
2101 * If there is only one component, i.e., if the number of basic sets
2102 * in the current component is equal to the total number of basic sets,
2103 * then data->single is set to 1 and the result of generate_sorted_domains
2104 * is not fused.
2106 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2107 void *user)
2109 struct isl_ast_generate_parallel_domains_data *data = user;
2110 isl_ast_graft_list *list;
2112 list = generate_sorted_domains(scc, data->executed, data->build);
2113 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2114 if (!data->single)
2115 list = isl_ast_graft_list_fuse(list, data->build);
2116 if (!data->list)
2117 data->list = list;
2118 else
2119 data->list = isl_ast_graft_list_concat(data->list, list);
2121 isl_basic_set_list_free(scc);
2122 if (!data->list)
2123 return isl_stat_error;
2125 return isl_stat_ok;
2128 /* Look for any (weakly connected) components in the "domain_list"
2129 * of domains that share some values of the outer dimensions.
2130 * That is, domains in different components do not share any values
2131 * of the outer dimensions. This means that these components
2132 * can be freely reordered.
2133 * Within each of the components, we sort the domains according
2134 * to the execution order at the current depth.
2136 * If there is more than one component, then generate_sorted_domains_wrap
2137 * fuses the result of each call to generate_sorted_domains
2138 * into a list with either zero or one graft and collects these (at most)
2139 * single element lists into a bigger list. This means that the elements of the
2140 * final list can be freely reordered. In particular, we sort them
2141 * according to an arbitrary but fixed ordering to ease merging of
2142 * graft lists from different components.
2144 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2145 __isl_keep isl_basic_set_list *domain_list,
2146 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2148 int depth;
2149 struct isl_ast_generate_parallel_domains_data data;
2151 if (!domain_list)
2152 return NULL;
2154 data.n = isl_basic_set_list_n_basic_set(domain_list);
2155 if (data.n <= 1)
2156 return generate_sorted_domains(domain_list, executed, build);
2158 depth = isl_ast_build_get_depth(build);
2159 data.list = NULL;
2160 data.executed = executed;
2161 data.build = build;
2162 data.single = 0;
2163 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2164 &generate_sorted_domains_wrap,
2165 &data) < 0)
2166 data.list = isl_ast_graft_list_free(data.list);
2168 if (!data.single)
2169 data.list = isl_ast_graft_list_sort_guard(data.list);
2171 return data.list;
2174 /* Internal data for separate_domain.
2176 * "explicit" is set if we only want to use explicit bounds.
2178 * "domain" collects the separated domains.
2180 struct isl_separate_domain_data {
2181 isl_ast_build *build;
2182 int explicit;
2183 isl_set *domain;
2186 /* Extract implicit bounds on the current dimension for the executed "map".
2188 * The domain of "map" may involve inner dimensions, so we
2189 * need to eliminate them.
2191 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2192 __isl_keep isl_ast_build *build)
2194 isl_set *domain;
2196 domain = isl_map_domain(map);
2197 domain = isl_ast_build_eliminate(build, domain);
2199 return domain;
2202 /* Extract explicit bounds on the current dimension for the executed "map".
2204 * Rather than eliminating the inner dimensions as in implicit_bounds,
2205 * we simply drop any constraints involving those inner dimensions.
2206 * The idea is that most bounds that are implied by constraints on the
2207 * inner dimensions will be enforced by for loops and not by explicit guards.
2208 * There is then no need to separate along those bounds.
2210 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2211 __isl_keep isl_ast_build *build)
2213 isl_set *domain;
2214 int depth, dim;
2216 dim = isl_map_dim(map, isl_dim_out);
2217 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2219 domain = isl_map_domain(map);
2220 depth = isl_ast_build_get_depth(build);
2221 dim = isl_set_dim(domain, isl_dim_set);
2222 domain = isl_set_detect_equalities(domain);
2223 domain = isl_set_drop_constraints_involving_dims(domain,
2224 isl_dim_set, depth + 1, dim - (depth + 1));
2225 domain = isl_set_remove_divs_involving_dims(domain,
2226 isl_dim_set, depth, 1);
2227 domain = isl_set_remove_unknown_divs(domain);
2229 return domain;
2232 /* Split data->domain into pieces that intersect with the range of "map"
2233 * and pieces that do not intersect with the range of "map"
2234 * and then add that part of the range of "map" that does not intersect
2235 * with data->domain.
2237 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2239 struct isl_separate_domain_data *data = user;
2240 isl_set *domain;
2241 isl_set *d1, *d2;
2243 if (data->explicit)
2244 domain = explicit_bounds(map, data->build);
2245 else
2246 domain = implicit_bounds(map, data->build);
2248 domain = isl_set_coalesce(domain);
2249 domain = isl_set_make_disjoint(domain);
2250 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2251 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2252 data->domain = isl_set_intersect(data->domain, domain);
2253 data->domain = isl_set_union(data->domain, d1);
2254 data->domain = isl_set_union(data->domain, d2);
2256 return isl_stat_ok;
2259 /* Separate the schedule domains of "executed".
2261 * That is, break up the domain of "executed" into basic sets,
2262 * such that for each basic set S, every element in S is associated with
2263 * the same domain spaces.
2265 * "space" is the (single) domain space of "executed".
2267 static __isl_give isl_set *separate_schedule_domains(
2268 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2269 __isl_keep isl_ast_build *build)
2271 struct isl_separate_domain_data data = { build };
2272 isl_ctx *ctx;
2274 ctx = isl_ast_build_get_ctx(build);
2275 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2276 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2277 data.domain = isl_set_empty(space);
2278 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2279 data.domain = isl_set_free(data.domain);
2281 isl_union_map_free(executed);
2282 return data.domain;
2285 /* Temporary data used during the search for a lower bound for unrolling.
2287 * "build" is the build in which the unrolling will be performed
2288 * "domain" is the original set for which to find a lower bound
2289 * "depth" is the dimension for which to find a lower boudn
2290 * "expansion" is the expansion that needs to be applied to "domain"
2291 * in the unrolling that will be performed
2293 * "lower" is the best lower bound found so far. It is NULL if we have not
2294 * found any yet.
2295 * "n" is the corresponding size. If lower is NULL, then the value of n
2296 * is undefined.
2297 * "n_div" is the maximal number of integer divisions in the first
2298 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2299 * been computed yet.
2301 struct isl_find_unroll_data {
2302 isl_ast_build *build;
2303 isl_set *domain;
2304 int depth;
2305 isl_basic_map *expansion;
2307 isl_aff *lower;
2308 int *n;
2309 int n_div;
2312 /* Return the constraint
2314 * i_"depth" = aff + offset
2316 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2317 int offset)
2319 aff = isl_aff_copy(aff);
2320 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2321 aff = isl_aff_add_constant_si(aff, offset);
2322 return isl_equality_from_aff(aff);
2325 /* Update *user to the number of integer divsions in the first element
2326 * of "ma", if it is larger than the current value.
2328 static isl_stat update_n_div(__isl_take isl_set *set,
2329 __isl_take isl_multi_aff *ma, void *user)
2331 isl_aff *aff;
2332 int *n = user;
2333 int n_div;
2335 aff = isl_multi_aff_get_aff(ma, 0);
2336 n_div = isl_aff_dim(aff, isl_dim_div);
2337 isl_aff_free(aff);
2338 isl_multi_aff_free(ma);
2339 isl_set_free(set);
2341 if (n_div > *n)
2342 *n = n_div;
2344 return aff ? isl_stat_ok : isl_stat_error;
2347 /* Get the number of integer divisions in the expression for the iterator
2348 * value at the first slice in the unrolling based on lower bound "lower",
2349 * taking into account the expansion that needs to be performed on this slice.
2351 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2352 __isl_keep isl_aff *lower)
2354 isl_constraint *c;
2355 isl_set *set;
2356 isl_map *it_map, *expansion;
2357 isl_pw_multi_aff *pma;
2358 int n;
2360 c = at_offset(data->depth, lower, 0);
2361 set = isl_set_copy(data->domain);
2362 set = isl_set_add_constraint(set, c);
2363 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2364 set = isl_set_apply(set, expansion);
2365 it_map = isl_ast_build_map_to_iterator(data->build, set);
2366 pma = isl_pw_multi_aff_from_map(it_map);
2367 n = 0;
2368 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2369 n = -1;
2370 isl_pw_multi_aff_free(pma);
2372 return n;
2375 /* Is the lower bound "lower" with corresponding iteration count "n"
2376 * better than the one stored in "data"?
2377 * If there is no upper bound on the iteration count ("n" is infinity) or
2378 * if the count is too large, then we cannot use this lower bound.
2379 * Otherwise, if there was no previous lower bound or
2380 * if the iteration count of the new lower bound is smaller than
2381 * the iteration count of the previous lower bound, then we consider
2382 * the new lower bound to be better.
2383 * If the iteration count is the same, then compare the number
2384 * of integer divisions that would be needed to express
2385 * the iterator value at the first slice in the unrolling
2386 * according to the lower bound. If we end up computing this
2387 * number, then store the lowest value in data->n_div.
2389 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2390 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2392 int cmp;
2393 int n_div;
2395 if (!n)
2396 return -1;
2397 if (isl_val_is_infty(n))
2398 return 0;
2399 if (isl_val_cmp_si(n, INT_MAX) > 0)
2400 return 0;
2401 if (!data->lower)
2402 return 1;
2403 cmp = isl_val_cmp_si(n, *data->n);
2404 if (cmp < 0)
2405 return 1;
2406 if (cmp > 0)
2407 return 0;
2408 if (data->n_div < 0)
2409 data->n_div = get_expanded_n_div(data, data->lower);
2410 if (data->n_div < 0)
2411 return -1;
2412 if (data->n_div == 0)
2413 return 0;
2414 n_div = get_expanded_n_div(data, lower);
2415 if (n_div < 0)
2416 return -1;
2417 if (n_div >= data->n_div)
2418 return 0;
2419 data->n_div = n_div;
2421 return 1;
2424 /* Check if we can use "c" as a lower bound and if it is better than
2425 * any previously found lower bound.
2427 * If "c" does not involve the dimension at the current depth,
2428 * then we cannot use it.
2429 * Otherwise, let "c" be of the form
2431 * i >= f(j)/a
2433 * We compute the maximal value of
2435 * -ceil(f(j)/a)) + i + 1
2437 * over the domain. If there is such a value "n", then we know
2439 * -ceil(f(j)/a)) + i + 1 <= n
2441 * or
2443 * i < ceil(f(j)/a)) + n
2445 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2446 * We just need to check if we have found any lower bound before and
2447 * if the new lower bound is better (smaller n or fewer integer divisions)
2448 * than the previously found lower bounds.
2450 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2451 __isl_keep isl_constraint *c)
2453 isl_aff *aff, *lower;
2454 isl_val *max;
2455 int better;
2457 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2458 return isl_stat_ok;
2460 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2461 lower = isl_aff_ceil(lower);
2462 aff = isl_aff_copy(lower);
2463 aff = isl_aff_neg(aff);
2464 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2465 aff = isl_aff_add_constant_si(aff, 1);
2466 max = isl_set_max_val(data->domain, aff);
2467 isl_aff_free(aff);
2469 better = is_better_lower_bound(data, lower, max);
2470 if (better < 0 || !better) {
2471 isl_val_free(max);
2472 isl_aff_free(lower);
2473 return better < 0 ? isl_stat_error : isl_stat_ok;
2476 isl_aff_free(data->lower);
2477 data->lower = lower;
2478 *data->n = isl_val_get_num_si(max);
2479 isl_val_free(max);
2481 return isl_stat_ok;
2484 /* Check if we can use "c" as a lower bound and if it is better than
2485 * any previously found lower bound.
2487 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2489 struct isl_find_unroll_data *data;
2490 isl_stat r;
2492 data = (struct isl_find_unroll_data *) user;
2493 r = update_unrolling_lower_bound(data, c);
2494 isl_constraint_free(c);
2496 return r;
2499 /* Look for a lower bound l(i) on the dimension at "depth"
2500 * and a size n such that "domain" is a subset of
2502 * { [i] : l(i) <= i_d < l(i) + n }
2504 * where d is "depth" and l(i) depends only on earlier dimensions.
2505 * Furthermore, try and find a lower bound such that n is as small as possible.
2506 * In particular, "n" needs to be finite.
2507 * "build" is the build in which the unrolling will be performed.
2508 * "expansion" is the expansion that needs to be applied to "domain"
2509 * in the unrolling that will be performed.
2511 * Inner dimensions have been eliminated from "domain" by the caller.
2513 * We first construct a collection of lower bounds on the input set
2514 * by computing its simple hull. We then iterate through them,
2515 * discarding those that we cannot use (either because they do not
2516 * involve the dimension at "depth" or because they have no corresponding
2517 * upper bound, meaning that "n" would be unbounded) and pick out the
2518 * best from the remaining ones.
2520 * If we cannot find a suitable lower bound, then we consider that
2521 * to be an error.
2523 static __isl_give isl_aff *find_unroll_lower_bound(
2524 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2525 int depth, __isl_keep isl_basic_map *expansion, int *n)
2527 struct isl_find_unroll_data data =
2528 { build, domain, depth, expansion, NULL, n, -1 };
2529 isl_basic_set *hull;
2531 hull = isl_set_simple_hull(isl_set_copy(domain));
2533 if (isl_basic_set_foreach_constraint(hull,
2534 &constraint_find_unroll, &data) < 0)
2535 goto error;
2537 isl_basic_set_free(hull);
2539 if (!data.lower)
2540 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2541 "cannot find lower bound for unrolling", return NULL);
2543 return data.lower;
2544 error:
2545 isl_basic_set_free(hull);
2546 return isl_aff_free(data.lower);
2549 /* Call "fn" on each iteration of the current dimension of "domain".
2550 * If "init" is not NULL, then it is called with the number of
2551 * iterations before any call to "fn".
2552 * Return -1 on failure.
2554 * Since we are going to be iterating over the individual values,
2555 * we first check if there are any strides on the current dimension.
2556 * If there is, we rewrite the current dimension i as
2558 * i = stride i' + offset
2560 * and then iterate over individual values of i' instead.
2562 * We then look for a lower bound on i' and a size such that the domain
2563 * is a subset of
2565 * { [j,i'] : l(j) <= i' < l(j) + n }
2567 * and then take slices of the domain at values of i'
2568 * between l(j) and l(j) + n - 1.
2570 * We compute the unshifted simple hull of each slice to ensure that
2571 * we have a single basic set per offset. The slicing constraint
2572 * may get simplified away before the unshifted simple hull is taken
2573 * and may therefore in some rare cases disappear from the result.
2574 * We therefore explicitly add the constraint back after computing
2575 * the unshifted simple hull to ensure that the basic sets
2576 * remain disjoint. The constraints that are dropped by taking the hull
2577 * will be taken into account at the next level, as in the case of the
2578 * atomic option.
2580 * Finally, we map i' back to i and call "fn".
2582 static int foreach_iteration(__isl_take isl_set *domain,
2583 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2584 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2586 int i, n;
2587 int empty;
2588 int depth;
2589 isl_multi_aff *expansion;
2590 isl_basic_map *bmap;
2591 isl_aff *lower = NULL;
2592 isl_ast_build *stride_build;
2594 depth = isl_ast_build_get_depth(build);
2596 domain = isl_ast_build_eliminate_inner(build, domain);
2597 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2598 stride_build = isl_ast_build_copy(build);
2599 stride_build = isl_ast_build_detect_strides(stride_build,
2600 isl_set_copy(domain));
2601 expansion = isl_ast_build_get_stride_expansion(stride_build);
2603 domain = isl_set_preimage_multi_aff(domain,
2604 isl_multi_aff_copy(expansion));
2605 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2606 isl_ast_build_free(stride_build);
2608 bmap = isl_basic_map_from_multi_aff(expansion);
2610 empty = isl_set_is_empty(domain);
2611 if (empty < 0) {
2612 n = -1;
2613 } else if (empty) {
2614 n = 0;
2615 } else {
2616 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2617 if (!lower)
2618 n = -1;
2620 if (n >= 0 && init && init(n, user) < 0)
2621 n = -1;
2622 for (i = 0; i < n; ++i) {
2623 isl_set *set;
2624 isl_basic_set *bset;
2625 isl_constraint *slice;
2627 slice = at_offset(depth, lower, i);
2628 set = isl_set_copy(domain);
2629 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2630 bset = isl_set_unshifted_simple_hull(set);
2631 bset = isl_basic_set_add_constraint(bset, slice);
2632 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2634 if (fn(bset, user) < 0)
2635 break;
2638 isl_aff_free(lower);
2639 isl_set_free(domain);
2640 isl_basic_map_free(bmap);
2642 return n < 0 || i < n ? -1 : 0;
2645 /* Data structure for storing the results and the intermediate objects
2646 * of compute_domains.
2648 * "list" is the main result of the function and contains a list
2649 * of disjoint basic sets for which code should be generated.
2651 * "executed" and "build" are inputs to compute_domains.
2652 * "schedule_domain" is the domain of "executed".
2654 * "option" constains the domains at the current depth that should by
2655 * atomic, separated or unrolled. These domains are as specified by
2656 * the user, except that inner dimensions have been eliminated and
2657 * that they have been made pair-wise disjoint.
2659 * "sep_class" contains the user-specified split into separation classes
2660 * specialized to the current depth.
2661 * "done" contains the union of the separation domains that have already
2662 * been handled.
2664 struct isl_codegen_domains {
2665 isl_basic_set_list *list;
2667 isl_union_map *executed;
2668 isl_ast_build *build;
2669 isl_set *schedule_domain;
2671 isl_set *option[4];
2673 isl_map *sep_class;
2674 isl_set *done;
2677 /* Internal data structure for do_unroll.
2679 * "domains" stores the results of compute_domains.
2680 * "class_domain" is the original class domain passed to do_unroll.
2681 * "unroll_domain" collects the unrolled iterations.
2683 struct isl_ast_unroll_data {
2684 struct isl_codegen_domains *domains;
2685 isl_set *class_domain;
2686 isl_set *unroll_domain;
2689 /* Given an iteration of an unrolled domain represented by "bset",
2690 * add it to data->domains->list.
2691 * Since we may have dropped some constraints, we intersect with
2692 * the class domain again to ensure that each element in the list
2693 * is disjoint from the other class domains.
2695 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2697 struct isl_ast_unroll_data *data = user;
2698 isl_set *set;
2699 isl_basic_set_list *list;
2701 set = isl_set_from_basic_set(bset);
2702 data->unroll_domain = isl_set_union(data->unroll_domain,
2703 isl_set_copy(set));
2704 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2705 set = isl_set_make_disjoint(set);
2706 list = isl_basic_set_list_from_set(set);
2707 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2708 list);
2710 return 0;
2713 /* Extend domains->list with a list of basic sets, one for each value
2714 * of the current dimension in "domain" and remove the corresponding
2715 * sets from the class domain. Return the updated class domain.
2716 * The divs that involve the current dimension have not been projected out
2717 * from this domain.
2719 * We call foreach_iteration to iterate over the individual values and
2720 * in do_unroll_iteration we collect the individual basic sets in
2721 * domains->list and their union in data->unroll_domain, which is then
2722 * used to update the class domain.
2724 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2725 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2727 struct isl_ast_unroll_data data;
2729 if (!domain)
2730 return isl_set_free(class_domain);
2731 if (!class_domain)
2732 return isl_set_free(domain);
2734 data.domains = domains;
2735 data.class_domain = class_domain;
2736 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2738 if (foreach_iteration(domain, domains->build, NULL,
2739 &do_unroll_iteration, &data) < 0)
2740 data.unroll_domain = isl_set_free(data.unroll_domain);
2742 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2744 return class_domain;
2747 /* Add domains to domains->list for each individual value of the current
2748 * dimension, for that part of the schedule domain that lies in the
2749 * intersection of the option domain and the class domain.
2750 * Remove the corresponding sets from the class domain and
2751 * return the updated class domain.
2753 * We first break up the unroll option domain into individual pieces
2754 * and then handle each of them separately. The unroll option domain
2755 * has been made disjoint in compute_domains_init_options,
2757 * Note that we actively want to combine different pieces of the
2758 * schedule domain that have the same value at the current dimension.
2759 * We therefore need to break up the unroll option domain before
2760 * intersecting with class and schedule domain, hoping that the
2761 * unroll option domain specified by the user is relatively simple.
2763 static __isl_give isl_set *compute_unroll_domains(
2764 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2766 isl_set *unroll_domain;
2767 isl_basic_set_list *unroll_list;
2768 int i, n;
2769 int empty;
2771 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2772 if (empty < 0)
2773 return isl_set_free(class_domain);
2774 if (empty)
2775 return class_domain;
2777 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2778 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2780 n = isl_basic_set_list_n_basic_set(unroll_list);
2781 for (i = 0; i < n; ++i) {
2782 isl_basic_set *bset;
2784 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2785 unroll_domain = isl_set_from_basic_set(bset);
2786 unroll_domain = isl_set_intersect(unroll_domain,
2787 isl_set_copy(class_domain));
2788 unroll_domain = isl_set_intersect(unroll_domain,
2789 isl_set_copy(domains->schedule_domain));
2791 empty = isl_set_is_empty(unroll_domain);
2792 if (empty >= 0 && empty) {
2793 isl_set_free(unroll_domain);
2794 continue;
2797 class_domain = do_unroll(domains, unroll_domain, class_domain);
2800 isl_basic_set_list_free(unroll_list);
2802 return class_domain;
2805 /* Try and construct a single basic set that includes the intersection of
2806 * the schedule domain, the atomic option domain and the class domain.
2807 * Add the resulting basic set(s) to domains->list and remove them
2808 * from class_domain. Return the updated class domain.
2810 * We construct a single domain rather than trying to combine
2811 * the schedule domains of individual domains because we are working
2812 * within a single component so that non-overlapping schedule domains
2813 * should already have been separated.
2814 * We do however need to make sure that this single domains is a subset
2815 * of the class domain so that it would not intersect with any other
2816 * class domains. This means that we may end up splitting up the atomic
2817 * domain in case separation classes are being used.
2819 * "domain" is the intersection of the schedule domain and the class domain,
2820 * with inner dimensions projected out.
2822 static __isl_give isl_set *compute_atomic_domain(
2823 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2825 isl_basic_set *bset;
2826 isl_basic_set_list *list;
2827 isl_set *domain, *atomic_domain;
2828 int empty;
2830 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2831 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2832 domain = isl_set_intersect(domain,
2833 isl_set_copy(domains->schedule_domain));
2834 empty = isl_set_is_empty(domain);
2835 if (empty < 0)
2836 class_domain = isl_set_free(class_domain);
2837 if (empty) {
2838 isl_set_free(domain);
2839 return class_domain;
2842 domain = isl_ast_build_eliminate(domains->build, domain);
2843 domain = isl_set_coalesce(domain);
2844 bset = isl_set_unshifted_simple_hull(domain);
2845 domain = isl_set_from_basic_set(bset);
2846 atomic_domain = isl_set_copy(domain);
2847 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2848 class_domain = isl_set_subtract(class_domain, atomic_domain);
2849 domain = isl_set_make_disjoint(domain);
2850 list = isl_basic_set_list_from_set(domain);
2851 domains->list = isl_basic_set_list_concat(domains->list, list);
2853 return class_domain;
2856 /* Split up the schedule domain into uniform basic sets,
2857 * in the sense that each element in a basic set is associated to
2858 * elements of the same domains, and add the result to domains->list.
2859 * Do this for that part of the schedule domain that lies in the
2860 * intersection of "class_domain" and the separate option domain.
2862 * "class_domain" may or may not include the constraints
2863 * of the schedule domain, but this does not make a difference
2864 * since we are going to intersect it with the domain of the inverse schedule.
2865 * If it includes schedule domain constraints, then they may involve
2866 * inner dimensions, but we will eliminate them in separation_domain.
2868 static int compute_separate_domain(struct isl_codegen_domains *domains,
2869 __isl_keep isl_set *class_domain)
2871 isl_space *space;
2872 isl_set *domain;
2873 isl_union_map *executed;
2874 isl_basic_set_list *list;
2875 int empty;
2877 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2878 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2879 executed = isl_union_map_copy(domains->executed);
2880 executed = isl_union_map_intersect_domain(executed,
2881 isl_union_set_from_set(domain));
2882 empty = isl_union_map_is_empty(executed);
2883 if (empty < 0 || empty) {
2884 isl_union_map_free(executed);
2885 return empty < 0 ? -1 : 0;
2888 space = isl_set_get_space(class_domain);
2889 domain = separate_schedule_domains(space, executed, domains->build);
2891 list = isl_basic_set_list_from_set(domain);
2892 domains->list = isl_basic_set_list_concat(domains->list, list);
2894 return 0;
2897 /* Split up the domain at the current depth into disjoint
2898 * basic sets for which code should be generated separately
2899 * for the given separation class domain.
2901 * If any separation classes have been defined, then "class_domain"
2902 * is the domain of the current class and does not refer to inner dimensions.
2903 * Otherwise, "class_domain" is the universe domain.
2905 * We first make sure that the class domain is disjoint from
2906 * previously considered class domains.
2908 * The separate domains can be computed directly from the "class_domain".
2910 * The unroll, atomic and remainder domains need the constraints
2911 * from the schedule domain.
2913 * For unrolling, the actual schedule domain is needed (with divs that
2914 * may refer to the current dimension) so that stride detection can be
2915 * performed.
2917 * For atomic and remainder domains, inner dimensions and divs involving
2918 * the current dimensions should be eliminated.
2919 * In case we are working within a separation class, we need to intersect
2920 * the result with the current "class_domain" to ensure that the domains
2921 * are disjoint from those generated from other class domains.
2923 * The domain that has been made atomic may be larger than specified
2924 * by the user since it needs to be representable as a single basic set.
2925 * This possibly larger domain is removed from class_domain by
2926 * compute_atomic_domain. It is computed first so that the extended domain
2927 * would not overlap with any domains computed before.
2928 * Similary, the unrolled domains may have some constraints removed and
2929 * may therefore also be larger than specified by the user.
2931 * If anything is left after handling separate, unroll and atomic,
2932 * we split it up into basic sets and append the basic sets to domains->list.
2934 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2935 __isl_take isl_set *class_domain)
2937 isl_basic_set_list *list;
2938 isl_set *domain;
2940 class_domain = isl_set_subtract(class_domain,
2941 isl_set_copy(domains->done));
2942 domains->done = isl_set_union(domains->done,
2943 isl_set_copy(class_domain));
2945 class_domain = compute_atomic_domain(domains, class_domain);
2946 class_domain = compute_unroll_domains(domains, class_domain);
2948 domain = isl_set_copy(class_domain);
2950 if (compute_separate_domain(domains, domain) < 0)
2951 goto error;
2952 domain = isl_set_subtract(domain,
2953 isl_set_copy(domains->option[isl_ast_loop_separate]));
2955 domain = isl_set_intersect(domain,
2956 isl_set_copy(domains->schedule_domain));
2958 domain = isl_ast_build_eliminate(domains->build, domain);
2959 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2961 domain = isl_set_coalesce(domain);
2962 domain = isl_set_make_disjoint(domain);
2964 list = isl_basic_set_list_from_set(domain);
2965 domains->list = isl_basic_set_list_concat(domains->list, list);
2967 isl_set_free(class_domain);
2969 return isl_stat_ok;
2970 error:
2971 isl_set_free(domain);
2972 isl_set_free(class_domain);
2973 return isl_stat_error;
2976 /* Split up the domain at the current depth into disjoint
2977 * basic sets for which code should be generated separately
2978 * for the separation class identified by "pnt".
2980 * We extract the corresponding class domain from domains->sep_class,
2981 * eliminate inner dimensions and pass control to compute_partial_domains.
2983 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2985 struct isl_codegen_domains *domains = user;
2986 isl_set *class_set;
2987 isl_set *domain;
2988 int disjoint;
2990 class_set = isl_set_from_point(pnt);
2991 domain = isl_map_domain(isl_map_intersect_range(
2992 isl_map_copy(domains->sep_class), class_set));
2993 domain = isl_ast_build_compute_gist(domains->build, domain);
2994 domain = isl_ast_build_eliminate(domains->build, domain);
2996 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2997 if (disjoint < 0)
2998 return isl_stat_error;
2999 if (disjoint) {
3000 isl_set_free(domain);
3001 return isl_stat_ok;
3004 return compute_partial_domains(domains, domain);
3007 /* Extract the domains at the current depth that should be atomic,
3008 * separated or unrolled and store them in option.
3010 * The domains specified by the user might overlap, so we make
3011 * them disjoint by subtracting earlier domains from later domains.
3013 static void compute_domains_init_options(isl_set *option[4],
3014 __isl_keep isl_ast_build *build)
3016 enum isl_ast_loop_type type, type2;
3017 isl_set *unroll;
3019 for (type = isl_ast_loop_atomic;
3020 type <= isl_ast_loop_separate; ++type) {
3021 option[type] = isl_ast_build_get_option_domain(build, type);
3022 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3023 option[type] = isl_set_subtract(option[type],
3024 isl_set_copy(option[type2]));
3027 unroll = option[isl_ast_loop_unroll];
3028 unroll = isl_set_coalesce(unroll);
3029 unroll = isl_set_make_disjoint(unroll);
3030 option[isl_ast_loop_unroll] = unroll;
3033 /* Split up the domain at the current depth into disjoint
3034 * basic sets for which code should be generated separately,
3035 * based on the user-specified options.
3036 * Return the list of disjoint basic sets.
3038 * There are three kinds of domains that we need to keep track of.
3039 * - the "schedule domain" is the domain of "executed"
3040 * - the "class domain" is the domain corresponding to the currrent
3041 * separation class
3042 * - the "option domain" is the domain corresponding to one of the options
3043 * atomic, unroll or separate
3045 * We first consider the individial values of the separation classes
3046 * and split up the domain for each of them separately.
3047 * Finally, we consider the remainder. If no separation classes were
3048 * specified, then we call compute_partial_domains with the universe
3049 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3050 * with inner dimensions removed. We do this because we want to
3051 * avoid computing the complement of the class domains (i.e., the difference
3052 * between the universe and domains->done).
3054 static __isl_give isl_basic_set_list *compute_domains(
3055 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3057 struct isl_codegen_domains domains;
3058 isl_ctx *ctx;
3059 isl_set *domain;
3060 isl_union_set *schedule_domain;
3061 isl_set *classes;
3062 isl_space *space;
3063 int n_param;
3064 enum isl_ast_loop_type type;
3065 int empty;
3067 if (!executed)
3068 return NULL;
3070 ctx = isl_union_map_get_ctx(executed);
3071 domains.list = isl_basic_set_list_alloc(ctx, 0);
3073 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3074 domain = isl_set_from_union_set(schedule_domain);
3076 compute_domains_init_options(domains.option, build);
3078 domains.sep_class = isl_ast_build_get_separation_class(build);
3079 classes = isl_map_range(isl_map_copy(domains.sep_class));
3080 n_param = isl_set_dim(classes, isl_dim_param);
3081 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3083 space = isl_set_get_space(domain);
3084 domains.build = build;
3085 domains.schedule_domain = isl_set_copy(domain);
3086 domains.executed = executed;
3087 domains.done = isl_set_empty(space);
3089 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3090 domains.list = isl_basic_set_list_free(domains.list);
3091 isl_set_free(classes);
3093 empty = isl_set_is_empty(domains.done);
3094 if (empty < 0) {
3095 domains.list = isl_basic_set_list_free(domains.list);
3096 domain = isl_set_free(domain);
3097 } else if (empty) {
3098 isl_set_free(domain);
3099 domain = isl_set_universe(isl_set_get_space(domains.done));
3100 } else {
3101 domain = isl_ast_build_eliminate(build, domain);
3103 if (compute_partial_domains(&domains, domain) < 0)
3104 domains.list = isl_basic_set_list_free(domains.list);
3106 isl_set_free(domains.schedule_domain);
3107 isl_set_free(domains.done);
3108 isl_map_free(domains.sep_class);
3109 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3110 isl_set_free(domains.option[type]);
3112 return domains.list;
3115 /* Generate code for a single component, after shifting (if any)
3116 * has been applied, in case the schedule was specified as a union map.
3118 * We first split up the domain at the current depth into disjoint
3119 * basic sets based on the user-specified options.
3120 * Then we generated code for each of them and concatenate the results.
3122 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3123 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3125 isl_basic_set_list *domain_list;
3126 isl_ast_graft_list *list = NULL;
3128 domain_list = compute_domains(executed, build);
3129 list = generate_parallel_domains(domain_list, executed, build);
3131 isl_basic_set_list_free(domain_list);
3132 isl_union_map_free(executed);
3133 isl_ast_build_free(build);
3135 return list;
3138 /* Generate code for a single component, after shifting (if any)
3139 * has been applied, in case the schedule was specified as a schedule tree
3140 * and the separate option was specified.
3142 * We perform separation on the domain of "executed" and then generate
3143 * an AST for each of the resulting disjoint basic sets.
3145 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3146 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3148 isl_space *space;
3149 isl_set *domain;
3150 isl_basic_set_list *domain_list;
3151 isl_ast_graft_list *list;
3153 space = isl_ast_build_get_space(build, 1);
3154 domain = separate_schedule_domains(space,
3155 isl_union_map_copy(executed), build);
3156 domain_list = isl_basic_set_list_from_set(domain);
3158 list = generate_parallel_domains(domain_list, executed, build);
3160 isl_basic_set_list_free(domain_list);
3161 isl_union_map_free(executed);
3162 isl_ast_build_free(build);
3164 return list;
3167 /* Internal data structure for generate_shifted_component_tree_unroll.
3169 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3170 * "list" collects the constructs grafts.
3172 struct isl_ast_unroll_tree_data {
3173 isl_union_map *executed;
3174 isl_ast_build *build;
3175 isl_ast_graft_list *list;
3178 /* Initialize data->list to a list of "n" elements.
3180 static int init_unroll_tree(int n, void *user)
3182 struct isl_ast_unroll_tree_data *data = user;
3183 isl_ctx *ctx;
3185 ctx = isl_ast_build_get_ctx(data->build);
3186 data->list = isl_ast_graft_list_alloc(ctx, n);
3188 return 0;
3191 /* Given an iteration of an unrolled domain represented by "bset",
3192 * generate the corresponding AST and add the result to data->list.
3194 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3196 struct isl_ast_unroll_tree_data *data = user;
3198 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3199 bset, isl_ast_build_copy(data->build));
3201 return 0;
3204 /* Generate code for a single component, after shifting (if any)
3205 * has been applied, in case the schedule was specified as a schedule tree
3206 * and the unroll option was specified.
3208 * We call foreach_iteration to iterate over the individual values and
3209 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3211 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3212 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3213 __isl_take isl_ast_build *build)
3215 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3217 if (foreach_iteration(domain, build, &init_unroll_tree,
3218 &do_unroll_tree_iteration, &data) < 0)
3219 data.list = isl_ast_graft_list_free(data.list);
3221 isl_union_map_free(executed);
3222 isl_ast_build_free(build);
3224 return data.list;
3227 /* Does "domain" involve a disjunction that is purely based on
3228 * constraints involving only outer dimension?
3230 * In particular, is there a disjunction such that the constraints
3231 * involving the current and later dimensions are the same over
3232 * all the disjuncts?
3234 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3235 __isl_keep isl_ast_build *build)
3237 isl_basic_set *hull;
3238 isl_set *shared, *inner;
3239 isl_bool equal;
3240 int depth, dim;
3242 if (isl_set_n_basic_set(domain) <= 1)
3243 return isl_bool_false;
3245 inner = isl_set_copy(domain);
3246 depth = isl_ast_build_get_depth(build);
3247 dim = isl_set_dim(inner, isl_dim_set);
3248 inner = isl_set_drop_constraints_not_involving_dims(inner,
3249 isl_dim_set, depth, dim - depth);
3250 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3251 shared = isl_set_from_basic_set(hull);
3252 equal = isl_set_plain_is_equal(inner, shared);
3253 isl_set_free(inner);
3254 isl_set_free(shared);
3256 return equal;
3259 /* Generate code for a single component, after shifting (if any)
3260 * has been applied, in case the schedule was specified as a schedule tree.
3261 * In particular, handle the base case where there is either no isolated
3262 * set or we are within the isolated set (in which case "isolated" is set)
3263 * or the iterations that precede or follow the isolated set.
3265 * The schedule domain is broken up or combined into basic sets
3266 * according to the AST generation option specified in the current
3267 * schedule node, which may be either atomic, separate, unroll or
3268 * unspecified. If the option is unspecified, then we currently simply
3269 * split the schedule domain into disjoint basic sets.
3271 * In case the separate option is specified, the AST generation is
3272 * handled by generate_shifted_component_tree_separate.
3273 * In the other cases, we need the global schedule domain.
3274 * In the unroll case, the AST generation is then handled by
3275 * generate_shifted_component_tree_unroll which needs the actual
3276 * schedule domain (with divs that may refer to the current dimension)
3277 * so that stride detection can be performed.
3278 * In the atomic or unspecified case, inner dimensions and divs involving
3279 * the current dimensions should be eliminated.
3280 * The result is then either combined into a single basic set or
3281 * split up into disjoint basic sets.
3282 * Finally an AST is generated for each basic set and the results are
3283 * concatenated.
3285 * If the schedule domain involves a disjunction that is purely based on
3286 * constraints involving only outer dimension, then it is treated as
3287 * if atomic was specified. This ensures that only a single loop
3288 * is generated instead of a sequence of identical loops with
3289 * different guards.
3291 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3292 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3293 int isolated)
3295 isl_bool outer_disjunction;
3296 isl_union_set *schedule_domain;
3297 isl_set *domain;
3298 isl_basic_set_list *domain_list;
3299 isl_ast_graft_list *list;
3300 enum isl_ast_loop_type type;
3302 type = isl_ast_build_get_loop_type(build, isolated);
3303 if (type < 0)
3304 goto error;
3306 if (type == isl_ast_loop_separate)
3307 return generate_shifted_component_tree_separate(executed,
3308 build);
3310 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3311 domain = isl_set_from_union_set(schedule_domain);
3313 if (type == isl_ast_loop_unroll)
3314 return generate_shifted_component_tree_unroll(executed, domain,
3315 build);
3317 domain = isl_ast_build_eliminate(build, domain);
3318 domain = isl_set_coalesce(domain);
3320 outer_disjunction = has_pure_outer_disjunction(domain, build);
3321 if (outer_disjunction < 0)
3322 domain = isl_set_free(domain);
3324 if (outer_disjunction || type == isl_ast_loop_atomic) {
3325 isl_basic_set *hull;
3326 hull = isl_set_unshifted_simple_hull(domain);
3327 domain_list = isl_basic_set_list_from_basic_set(hull);
3328 } else {
3329 domain = isl_set_make_disjoint(domain);
3330 domain_list = isl_basic_set_list_from_set(domain);
3333 list = generate_parallel_domains(domain_list, executed, build);
3335 isl_basic_set_list_free(domain_list);
3336 isl_union_map_free(executed);
3337 isl_ast_build_free(build);
3339 return list;
3340 error:
3341 isl_union_map_free(executed);
3342 isl_ast_build_free(build);
3343 return NULL;
3346 /* Extract out the disjunction imposed by "domain" on the outer
3347 * schedule dimensions.
3349 * In particular, remove all inner dimensions from "domain" (including
3350 * the current dimension) and then remove the constraints that are shared
3351 * by all disjuncts in the result.
3353 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3354 __isl_keep isl_ast_build *build)
3356 isl_set *hull;
3357 int depth, dim;
3359 domain = isl_ast_build_specialize(build, domain);
3360 depth = isl_ast_build_get_depth(build);
3361 dim = isl_set_dim(domain, isl_dim_set);
3362 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3363 domain = isl_set_remove_unknown_divs(domain);
3364 hull = isl_set_copy(domain);
3365 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3366 domain = isl_set_gist(domain, hull);
3368 return domain;
3371 /* Add "guard" to the grafts in "list".
3372 * "build" is the outer AST build, while "sub_build" includes "guard"
3373 * in its generated domain.
3375 * First combine the grafts into a single graft and then add the guard.
3376 * If the list is empty, or if some error occurred, then simply return
3377 * the list.
3379 static __isl_give isl_ast_graft_list *list_add_guard(
3380 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3381 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3383 isl_ast_graft *graft;
3385 list = isl_ast_graft_list_fuse(list, sub_build);
3387 if (isl_ast_graft_list_n_ast_graft(list) != 1)
3388 return list;
3390 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3391 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3392 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3394 return list;
3397 /* Generate code for a single component, after shifting (if any)
3398 * has been applied, in case the schedule was specified as a schedule tree.
3399 * In particular, do so for the specified subset of the schedule domain.
3401 * If we are outside of the isolated part, then "domain" may include
3402 * a disjunction. Explicitly generate this disjunction at this point
3403 * instead of relying on the disjunction getting hoisted back up
3404 * to this level.
3406 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3407 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3408 __isl_keep isl_ast_build *build, int isolated)
3410 isl_union_set *uset;
3411 isl_ast_graft_list *list;
3412 isl_ast_build *sub_build;
3413 int empty;
3415 uset = isl_union_set_from_set(isl_set_copy(domain));
3416 executed = isl_union_map_copy(executed);
3417 executed = isl_union_map_intersect_domain(executed, uset);
3418 empty = isl_union_map_is_empty(executed);
3419 if (empty < 0)
3420 goto error;
3421 if (empty) {
3422 isl_ctx *ctx;
3423 isl_union_map_free(executed);
3424 isl_set_free(domain);
3425 ctx = isl_ast_build_get_ctx(build);
3426 return isl_ast_graft_list_alloc(ctx, 0);
3429 sub_build = isl_ast_build_copy(build);
3430 if (!isolated) {
3431 domain = extract_disjunction(domain, build);
3432 sub_build = isl_ast_build_restrict_generated(sub_build,
3433 isl_set_copy(domain));
3435 list = generate_shifted_component_tree_base(executed,
3436 isl_ast_build_copy(sub_build), isolated);
3437 if (!isolated)
3438 list = list_add_guard(list, domain, build, sub_build);
3439 isl_ast_build_free(sub_build);
3440 isl_set_free(domain);
3441 return list;
3442 error:
3443 isl_union_map_free(executed);
3444 isl_set_free(domain);
3445 return NULL;
3448 /* Generate code for a single component, after shifting (if any)
3449 * has been applied, in case the schedule was specified as a schedule tree.
3450 * In particular, do so for the specified sequence of subsets
3451 * of the schedule domain, "before", "isolated", "after" and "other",
3452 * where only the "isolated" part is considered to be isolated.
3454 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3455 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3456 __isl_take isl_set *isolated, __isl_take isl_set *after,
3457 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3459 isl_ast_graft_list *list, *res;
3461 res = generate_shifted_component_tree_part(executed, before, build, 0);
3462 list = generate_shifted_component_tree_part(executed, isolated,
3463 build, 1);
3464 res = isl_ast_graft_list_concat(res, list);
3465 list = generate_shifted_component_tree_part(executed, after, build, 0);
3466 res = isl_ast_graft_list_concat(res, list);
3467 list = generate_shifted_component_tree_part(executed, other, build, 0);
3468 res = isl_ast_graft_list_concat(res, list);
3470 isl_union_map_free(executed);
3471 isl_ast_build_free(build);
3473 return res;
3476 /* Does "set" intersect "first", but not "second"?
3478 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3479 __isl_keep isl_set *first, __isl_keep isl_set *second)
3481 isl_bool disjoint;
3483 disjoint = isl_set_is_disjoint(set, first);
3484 if (disjoint < 0)
3485 return isl_bool_error;
3486 if (disjoint)
3487 return isl_bool_false;
3489 return isl_set_is_disjoint(set, second);
3492 /* Generate code for a single component, after shifting (if any)
3493 * has been applied, in case the schedule was specified as a schedule tree.
3494 * In particular, do so in case of isolation where there is
3495 * only an "isolated" part and an "after" part.
3496 * "dead1" and "dead2" are freed by this function in order to simplify
3497 * the caller.
3499 * The "before" and "other" parts are set to empty sets.
3501 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3502 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3503 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3504 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3506 isl_set *empty;
3508 empty = isl_set_empty(isl_set_get_space(after));
3509 isl_set_free(dead1);
3510 isl_set_free(dead2);
3511 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3512 isolated, after, empty, build);
3515 /* Generate code for a single component, after shifting (if any)
3516 * has been applied, in case the schedule was specified as a schedule tree.
3518 * We first check if the user has specified an isolated schedule domain
3519 * and that we are not already outside of this isolated schedule domain.
3520 * If so, we break up the schedule domain into iterations that
3521 * precede the isolated domain, the isolated domain itself,
3522 * the iterations that follow the isolated domain and
3523 * the remaining iterations (those that are incomparable
3524 * to the isolated domain).
3525 * We generate an AST for each piece and concatenate the results.
3527 * If the isolated domain is not convex, then it is replaced
3528 * by a convex superset to ensure that the sets of preceding and
3529 * following iterations are properly defined and, in particular,
3530 * that there are no intermediate iterations that do not belong
3531 * to the isolated domain.
3533 * In the special case where at least one element of the schedule
3534 * domain that does not belong to the isolated domain needs
3535 * to be scheduled after this isolated domain, but none of those
3536 * elements need to be scheduled before, break up the schedule domain
3537 * in only two parts, the isolated domain, and a part that will be
3538 * scheduled after the isolated domain.
3540 * If no isolated set has been specified, then we generate an
3541 * AST for the entire inverse schedule.
3543 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3544 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3546 int i, depth;
3547 int empty, has_isolate;
3548 isl_space *space;
3549 isl_union_set *schedule_domain;
3550 isl_set *domain;
3551 isl_basic_set *hull;
3552 isl_set *isolated, *before, *after, *test;
3553 isl_map *gt, *lt;
3554 isl_bool pure;
3556 build = isl_ast_build_extract_isolated(build);
3557 has_isolate = isl_ast_build_has_isolated(build);
3558 if (has_isolate < 0)
3559 executed = isl_union_map_free(executed);
3560 else if (!has_isolate)
3561 return generate_shifted_component_tree_base(executed, build, 0);
3563 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3564 domain = isl_set_from_union_set(schedule_domain);
3566 isolated = isl_ast_build_get_isolated(build);
3567 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3568 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3569 empty = isl_set_is_empty(test);
3570 isl_set_free(test);
3571 if (empty < 0)
3572 goto error;
3573 if (empty) {
3574 isl_set_free(isolated);
3575 isl_set_free(domain);
3576 return generate_shifted_component_tree_base(executed, build, 0);
3578 isolated = isl_ast_build_eliminate(build, isolated);
3579 hull = isl_set_unshifted_simple_hull(isolated);
3580 isolated = isl_set_from_basic_set(hull);
3582 depth = isl_ast_build_get_depth(build);
3583 space = isl_space_map_from_set(isl_set_get_space(isolated));
3584 gt = isl_map_universe(space);
3585 for (i = 0; i < depth; ++i)
3586 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3587 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3588 lt = isl_map_reverse(isl_map_copy(gt));
3589 before = isl_set_apply(isl_set_copy(isolated), gt);
3590 after = isl_set_apply(isl_set_copy(isolated), lt);
3592 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3593 pure = only_intersects_first(domain, after, before);
3594 if (pure < 0)
3595 executed = isl_union_map_free(executed);
3596 else if (pure)
3597 return generate_shifted_component_only_after(executed, isolated,
3598 domain, build, before, after);
3599 domain = isl_set_subtract(domain, isl_set_copy(before));
3600 domain = isl_set_subtract(domain, isl_set_copy(after));
3601 after = isl_set_subtract(after, isl_set_copy(isolated));
3602 after = isl_set_subtract(after, isl_set_copy(before));
3603 before = isl_set_subtract(before, isl_set_copy(isolated));
3605 return generate_shifted_component_parts(executed, before, isolated,
3606 after, domain, build);
3607 error:
3608 isl_set_free(domain);
3609 isl_set_free(isolated);
3610 isl_union_map_free(executed);
3611 isl_ast_build_free(build);
3612 return NULL;
3615 /* Generate code for a single component, after shifting (if any)
3616 * has been applied.
3618 * Call generate_shifted_component_tree or generate_shifted_component_flat
3619 * depending on whether the schedule was specified as a schedule tree.
3621 static __isl_give isl_ast_graft_list *generate_shifted_component(
3622 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3624 if (isl_ast_build_has_schedule_node(build))
3625 return generate_shifted_component_tree(executed, build);
3626 else
3627 return generate_shifted_component_flat(executed, build);
3630 struct isl_set_map_pair {
3631 isl_set *set;
3632 isl_map *map;
3635 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3636 * of indices into the "domain" array,
3637 * return the union of the "map" fields of the elements
3638 * indexed by the first "n" elements of "order".
3640 static __isl_give isl_union_map *construct_component_executed(
3641 struct isl_set_map_pair *domain, int *order, int n)
3643 int i;
3644 isl_map *map;
3645 isl_union_map *executed;
3647 map = isl_map_copy(domain[order[0]].map);
3648 executed = isl_union_map_from_map(map);
3649 for (i = 1; i < n; ++i) {
3650 map = isl_map_copy(domain[order[i]].map);
3651 executed = isl_union_map_add_map(executed, map);
3654 return executed;
3657 /* Generate code for a single component, after shifting (if any)
3658 * has been applied.
3660 * The component inverse schedule is specified as the "map" fields
3661 * of the elements of "domain" indexed by the first "n" elements of "order".
3663 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3664 struct isl_set_map_pair *domain, int *order, int n,
3665 __isl_take isl_ast_build *build)
3667 isl_union_map *executed;
3669 executed = construct_component_executed(domain, order, n);
3670 return generate_shifted_component(executed, build);
3673 /* Does set dimension "pos" of "set" have an obviously fixed value?
3675 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3677 int fixed;
3678 isl_val *v;
3680 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3681 if (!v)
3682 return -1;
3683 fixed = !isl_val_is_nan(v);
3684 isl_val_free(v);
3686 return fixed;
3689 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3690 * of indices into the "domain" array,
3691 * do all (except for at most one) of the "set" field of the elements
3692 * indexed by the first "n" elements of "order" have a fixed value
3693 * at position "depth"?
3695 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3696 int *order, int n, int depth)
3698 int i;
3699 int non_fixed = -1;
3701 for (i = 0; i < n; ++i) {
3702 int f;
3704 f = dim_is_fixed(domain[order[i]].set, depth);
3705 if (f < 0)
3706 return -1;
3707 if (f)
3708 continue;
3709 if (non_fixed >= 0)
3710 return 0;
3711 non_fixed = i;
3714 return 1;
3717 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3718 * of indices into the "domain" array,
3719 * eliminate the inner dimensions from the "set" field of the elements
3720 * indexed by the first "n" elements of "order", provided the current
3721 * dimension does not have a fixed value.
3723 * Return the index of the first element in "order" with a corresponding
3724 * "set" field that does not have an (obviously) fixed value.
3726 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3727 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3729 int i;
3730 int base = -1;
3732 for (i = n - 1; i >= 0; --i) {
3733 int f;
3734 f = dim_is_fixed(domain[order[i]].set, depth);
3735 if (f < 0)
3736 return -1;
3737 if (f)
3738 continue;
3739 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3740 domain[order[i]].set);
3741 base = i;
3744 return base;
3747 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3748 * of indices into the "domain" array,
3749 * find the element of "domain" (amongst those indexed by the first "n"
3750 * elements of "order") with the "set" field that has the smallest
3751 * value for the current iterator.
3753 * Note that the domain with the smallest value may depend on the parameters
3754 * and/or outer loop dimension. Since the result of this function is only
3755 * used as heuristic, we only make a reasonable attempt at finding the best
3756 * domain, one that should work in case a single domain provides the smallest
3757 * value for the current dimension over all values of the parameters
3758 * and outer dimensions.
3760 * In particular, we compute the smallest value of the first domain
3761 * and replace it by that of any later domain if that later domain
3762 * has a smallest value that is smaller for at least some value
3763 * of the parameters and outer dimensions.
3765 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3766 __isl_keep isl_ast_build *build)
3768 int i;
3769 isl_map *min_first;
3770 int first = 0;
3772 min_first = isl_ast_build_map_to_iterator(build,
3773 isl_set_copy(domain[order[0]].set));
3774 min_first = isl_map_lexmin(min_first);
3776 for (i = 1; i < n; ++i) {
3777 isl_map *min, *test;
3778 int empty;
3780 min = isl_ast_build_map_to_iterator(build,
3781 isl_set_copy(domain[order[i]].set));
3782 min = isl_map_lexmin(min);
3783 test = isl_map_copy(min);
3784 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3785 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3786 empty = isl_map_is_empty(test);
3787 isl_map_free(test);
3788 if (empty >= 0 && !empty) {
3789 isl_map_free(min_first);
3790 first = i;
3791 min_first = min;
3792 } else
3793 isl_map_free(min);
3795 if (empty < 0)
3796 break;
3799 isl_map_free(min_first);
3801 return i < n ? -1 : first;
3804 /* Construct a shifted inverse schedule based on the original inverse schedule,
3805 * the stride and the offset.
3807 * The original inverse schedule is specified as the "map" fields
3808 * of the elements of "domain" indexed by the first "n" elements of "order".
3810 * "stride" and "offset" are such that the difference
3811 * between the values of the current dimension of domain "i"
3812 * and the values of the current dimension for some reference domain are
3813 * equal to
3815 * stride * integer + offset[i]
3817 * Moreover, 0 <= offset[i] < stride.
3819 * For each domain, we create a map
3821 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3823 * where j refers to the current dimension and the other dimensions are
3824 * unchanged, and apply this map to the original schedule domain.
3826 * For example, for the original schedule
3828 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3830 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3831 * we apply the mapping
3833 * { [j] -> [j, 0] }
3835 * to the schedule of the "A" domain and the mapping
3837 * { [j - 1] -> [j, 1] }
3839 * to the schedule of the "B" domain.
3842 * Note that after the transformation, the differences between pairs
3843 * of values of the current dimension over all domains are multiples
3844 * of stride and that we have therefore exposed the stride.
3847 * To see that the mapping preserves the lexicographic order,
3848 * first note that each of the individual maps above preserves the order.
3849 * If the value of the current iterator is j1 in one domain and j2 in another,
3850 * then if j1 = j2, we know that the same map is applied to both domains
3851 * and the order is preserved.
3852 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3853 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3855 * j1 - c1 < j2 - c2
3857 * and the order is preserved.
3858 * If c1 < c2, then we know
3860 * 0 <= c2 - c1 < s
3862 * We also have
3864 * j2 - j1 = n * s + r
3866 * with n >= 0 and 0 <= r < s.
3867 * In other words, r = c2 - c1.
3868 * If n > 0, then
3870 * j1 - c1 < j2 - c2
3872 * If n = 0, then
3874 * j1 - c1 = j2 - c2
3876 * and so
3878 * (j1 - c1, c1) << (j2 - c2, c2)
3880 * with "<<" the lexicographic order, proving that the order is preserved
3881 * in all cases.
3883 static __isl_give isl_union_map *contruct_shifted_executed(
3884 struct isl_set_map_pair *domain, int *order, int n,
3885 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3886 __isl_take isl_ast_build *build)
3888 int i;
3889 isl_union_map *executed;
3890 isl_space *space;
3891 isl_map *map;
3892 int depth;
3893 isl_constraint *c;
3895 depth = isl_ast_build_get_depth(build);
3896 space = isl_ast_build_get_space(build, 1);
3897 executed = isl_union_map_empty(isl_space_copy(space));
3898 space = isl_space_map_from_set(space);
3899 map = isl_map_identity(isl_space_copy(space));
3900 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3901 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3902 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3904 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3905 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3906 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3908 for (i = 0; i < n; ++i) {
3909 isl_map *map_i;
3910 isl_val *v;
3912 v = isl_multi_val_get_val(offset, i);
3913 if (!v)
3914 break;
3915 map_i = isl_map_copy(map);
3916 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3917 isl_val_copy(v));
3918 v = isl_val_neg(v);
3919 c = isl_constraint_set_constant_val(c, v);
3920 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3922 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3923 map_i);
3924 executed = isl_union_map_add_map(executed, map_i);
3927 isl_constraint_free(c);
3928 isl_map_free(map);
3930 if (i < n)
3931 executed = isl_union_map_free(executed);
3933 return executed;
3936 /* Generate code for a single component, after exposing the stride,
3937 * given that the schedule domain is "shifted strided".
3939 * The component inverse schedule is specified as the "map" fields
3940 * of the elements of "domain" indexed by the first "n" elements of "order".
3942 * The schedule domain being "shifted strided" means that the differences
3943 * between the values of the current dimension of domain "i"
3944 * and the values of the current dimension for some reference domain are
3945 * equal to
3947 * stride * integer + offset[i]
3949 * We first look for the domain with the "smallest" value for the current
3950 * dimension and adjust the offsets such that the offset of the "smallest"
3951 * domain is equal to zero. The other offsets are reduced modulo stride.
3953 * Based on this information, we construct a new inverse schedule in
3954 * contruct_shifted_executed that exposes the stride.
3955 * Since this involves the introduction of a new schedule dimension,
3956 * the build needs to be changed accodingly.
3957 * After computing the AST, the newly introduced dimension needs
3958 * to be removed again from the list of grafts. We do this by plugging
3959 * in a mapping that represents the new schedule domain in terms of the
3960 * old schedule domain.
3962 static __isl_give isl_ast_graft_list *generate_shift_component(
3963 struct isl_set_map_pair *domain, int *order, int n,
3964 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3965 __isl_take isl_ast_build *build)
3967 isl_ast_graft_list *list;
3968 int first;
3969 int depth;
3970 isl_val *val;
3971 isl_multi_val *mv;
3972 isl_space *space;
3973 isl_multi_aff *ma, *zero;
3974 isl_union_map *executed;
3976 depth = isl_ast_build_get_depth(build);
3978 first = first_offset(domain, order, n, build);
3979 if (first < 0)
3980 goto error;
3982 mv = isl_multi_val_copy(offset);
3983 val = isl_multi_val_get_val(offset, first);
3984 val = isl_val_neg(val);
3985 mv = isl_multi_val_add_val(mv, val);
3986 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3988 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3989 build);
3990 space = isl_ast_build_get_space(build, 1);
3991 space = isl_space_map_from_set(space);
3992 ma = isl_multi_aff_identity(isl_space_copy(space));
3993 space = isl_space_from_domain(isl_space_domain(space));
3994 space = isl_space_add_dims(space, isl_dim_out, 1);
3995 zero = isl_multi_aff_zero(space);
3996 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3997 build = isl_ast_build_insert_dim(build, depth + 1);
3998 list = generate_shifted_component(executed, build);
4000 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4002 isl_multi_val_free(mv);
4004 return list;
4005 error:
4006 isl_ast_build_free(build);
4007 return NULL;
4010 /* Does any node in the schedule tree rooted at the current schedule node
4011 * of "build" depend on outer schedule nodes?
4013 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4015 isl_schedule_node *node;
4016 int dependent = 0;
4018 node = isl_ast_build_get_schedule_node(build);
4019 dependent = isl_schedule_node_is_subtree_anchored(node);
4020 isl_schedule_node_free(node);
4022 return dependent;
4025 /* Generate code for a single component.
4027 * The component inverse schedule is specified as the "map" fields
4028 * of the elements of "domain" indexed by the first "n" elements of "order".
4030 * This function may modify the "set" fields of "domain".
4032 * Before proceeding with the actual code generation for the component,
4033 * we first check if there are any "shifted" strides, meaning that
4034 * the schedule domains of the individual domains are all strided,
4035 * but that they have different offsets, resulting in the union
4036 * of schedule domains not being strided anymore.
4038 * The simplest example is the schedule
4040 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4042 * Both schedule domains are strided, but their union is not.
4043 * This function detects such cases and then rewrites the schedule to
4045 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4047 * In the new schedule, the schedule domains have the same offset (modulo
4048 * the stride), ensuring that the union of schedule domains is also strided.
4051 * If there is only a single domain in the component, then there is
4052 * nothing to do. Similarly, if the current schedule dimension has
4053 * a fixed value for almost all domains then there is nothing to be done.
4054 * In particular, we need at least two domains where the current schedule
4055 * dimension does not have a fixed value.
4056 * Finally, in case of a schedule map input,
4057 * if any of the options refer to the current schedule dimension,
4058 * then we bail out as well. It would be possible to reformulate the options
4059 * in terms of the new schedule domain, but that would introduce constraints
4060 * that separate the domains in the options and that is something we would
4061 * like to avoid.
4062 * In the case of a schedule tree input, we bail out if any of
4063 * the descendants of the current schedule node refer to outer
4064 * schedule nodes in any way.
4067 * To see if there is any shifted stride, we look at the differences
4068 * between the values of the current dimension in pairs of domains
4069 * for equal values of outer dimensions. These differences should be
4070 * of the form
4072 * m x + r
4074 * with "m" the stride and "r" a constant. Note that we cannot perform
4075 * this analysis on individual domains as the lower bound in each domain
4076 * may depend on parameters or outer dimensions and so the current dimension
4077 * itself may not have a fixed remainder on division by the stride.
4079 * In particular, we compare the first domain that does not have an
4080 * obviously fixed value for the current dimension to itself and all
4081 * other domains and collect the offsets and the gcd of the strides.
4082 * If the gcd becomes one, then we failed to find shifted strides.
4083 * If the gcd is zero, then the differences were all fixed, meaning
4084 * that some domains had non-obviously fixed values for the current dimension.
4085 * If all the offsets are the same (for those domains that do not have
4086 * an obviously fixed value for the current dimension), then we do not
4087 * apply the transformation.
4088 * If none of the domains were skipped, then there is nothing to do.
4089 * If some of them were skipped, then if we apply separation, the schedule
4090 * domain should get split in pieces with a (non-shifted) stride.
4092 * Otherwise, we apply a shift to expose the stride in
4093 * generate_shift_component.
4095 static __isl_give isl_ast_graft_list *generate_component(
4096 struct isl_set_map_pair *domain, int *order, int n,
4097 __isl_take isl_ast_build *build)
4099 int i, d;
4100 int depth;
4101 isl_ctx *ctx;
4102 isl_map *map;
4103 isl_set *deltas;
4104 isl_val *gcd = NULL;
4105 isl_multi_val *mv;
4106 int fixed, skip;
4107 int base;
4108 isl_ast_graft_list *list;
4109 int res = 0;
4111 depth = isl_ast_build_get_depth(build);
4113 skip = n == 1;
4114 if (skip >= 0 && !skip)
4115 skip = at_most_one_non_fixed(domain, order, n, depth);
4116 if (skip >= 0 && !skip) {
4117 if (isl_ast_build_has_schedule_node(build))
4118 skip = has_anchored_subtree(build);
4119 else
4120 skip = isl_ast_build_options_involve_depth(build);
4122 if (skip < 0)
4123 goto error;
4124 if (skip)
4125 return generate_shifted_component_from_list(domain,
4126 order, n, build);
4128 base = eliminate_non_fixed(domain, order, n, depth, build);
4129 if (base < 0)
4130 goto error;
4132 ctx = isl_ast_build_get_ctx(build);
4134 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4136 fixed = 1;
4137 for (i = 0; i < n; ++i) {
4138 isl_val *r, *m;
4140 map = isl_map_from_domain_and_range(
4141 isl_set_copy(domain[order[base]].set),
4142 isl_set_copy(domain[order[i]].set));
4143 for (d = 0; d < depth; ++d)
4144 map = isl_map_equate(map, isl_dim_in, d,
4145 isl_dim_out, d);
4146 deltas = isl_map_deltas(map);
4147 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4148 isl_set_free(deltas);
4149 if (res < 0)
4150 break;
4152 if (i == 0)
4153 gcd = m;
4154 else
4155 gcd = isl_val_gcd(gcd, m);
4156 if (isl_val_is_one(gcd)) {
4157 isl_val_free(r);
4158 break;
4160 mv = isl_multi_val_set_val(mv, i, r);
4162 res = dim_is_fixed(domain[order[i]].set, depth);
4163 if (res < 0)
4164 break;
4165 if (res)
4166 continue;
4168 if (fixed && i > base) {
4169 isl_val *a, *b;
4170 a = isl_multi_val_get_val(mv, i);
4171 b = isl_multi_val_get_val(mv, base);
4172 if (isl_val_ne(a, b))
4173 fixed = 0;
4174 isl_val_free(a);
4175 isl_val_free(b);
4179 if (res < 0 || !gcd) {
4180 isl_ast_build_free(build);
4181 list = NULL;
4182 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4183 list = generate_shifted_component_from_list(domain,
4184 order, n, build);
4185 } else {
4186 list = generate_shift_component(domain, order, n, gcd, mv,
4187 build);
4190 isl_val_free(gcd);
4191 isl_multi_val_free(mv);
4193 return list;
4194 error:
4195 isl_ast_build_free(build);
4196 return NULL;
4199 /* Store both "map" itself and its domain in the
4200 * structure pointed to by *next and advance to the next array element.
4202 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4204 struct isl_set_map_pair **next = user;
4206 (*next)->map = isl_map_copy(map);
4207 (*next)->set = isl_map_domain(map);
4208 (*next)++;
4210 return isl_stat_ok;
4213 static int after_in_tree(__isl_keep isl_union_map *umap,
4214 __isl_keep isl_schedule_node *node);
4216 /* Is any domain element of "umap" scheduled after any of
4217 * the corresponding image elements by the tree rooted at
4218 * the child of "node"?
4220 static int after_in_child(__isl_keep isl_union_map *umap,
4221 __isl_keep isl_schedule_node *node)
4223 isl_schedule_node *child;
4224 int after;
4226 child = isl_schedule_node_get_child(node, 0);
4227 after = after_in_tree(umap, child);
4228 isl_schedule_node_free(child);
4230 return after;
4233 /* Is any domain element of "umap" scheduled after any of
4234 * the corresponding image elements by the tree rooted at
4235 * the band node "node"?
4237 * We first check if any domain element is scheduled after any
4238 * of the corresponding image elements by the band node itself.
4239 * If not, we restrict "map" to those pairs of element that
4240 * are scheduled together by the band node and continue with
4241 * the child of the band node.
4242 * If there are no such pairs then the map passed to after_in_child
4243 * will be empty causing it to return 0.
4245 static int after_in_band(__isl_keep isl_union_map *umap,
4246 __isl_keep isl_schedule_node *node)
4248 isl_multi_union_pw_aff *mupa;
4249 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4250 isl_union_set *domain, *range;
4251 isl_space *space;
4252 int empty;
4253 int after;
4255 if (isl_schedule_node_band_n_member(node) == 0)
4256 return after_in_child(umap, node);
4258 mupa = isl_schedule_node_band_get_partial_schedule(node);
4259 space = isl_multi_union_pw_aff_get_space(mupa);
4260 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4261 test = isl_union_map_copy(umap);
4262 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4263 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4264 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4265 test = isl_union_map_intersect(test, gt);
4266 empty = isl_union_map_is_empty(test);
4267 isl_union_map_free(test);
4269 if (empty < 0 || !empty) {
4270 isl_union_map_free(partial);
4271 return empty < 0 ? -1 : 1;
4274 universe = isl_union_map_universe(isl_union_map_copy(umap));
4275 domain = isl_union_map_domain(isl_union_map_copy(universe));
4276 range = isl_union_map_range(universe);
4277 umap1 = isl_union_map_copy(partial);
4278 umap1 = isl_union_map_intersect_domain(umap1, domain);
4279 umap2 = isl_union_map_intersect_domain(partial, range);
4280 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4281 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4282 after = after_in_child(test, node);
4283 isl_union_map_free(test);
4284 return after;
4287 /* Is any domain element of "umap" scheduled after any of
4288 * the corresponding image elements by the tree rooted at
4289 * the context node "node"?
4291 * The context constraints apply to the schedule domain,
4292 * so we cannot apply them directly to "umap", which contains
4293 * pairs of statement instances. Instead, we add them
4294 * to the range of the prefix schedule for both domain and
4295 * range of "umap".
4297 static int after_in_context(__isl_keep isl_union_map *umap,
4298 __isl_keep isl_schedule_node *node)
4300 isl_union_map *prefix, *universe, *umap1, *umap2;
4301 isl_union_set *domain, *range;
4302 isl_set *context;
4303 int after;
4305 umap = isl_union_map_copy(umap);
4306 context = isl_schedule_node_context_get_context(node);
4307 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4308 universe = isl_union_map_universe(isl_union_map_copy(umap));
4309 domain = isl_union_map_domain(isl_union_map_copy(universe));
4310 range = isl_union_map_range(universe);
4311 umap1 = isl_union_map_copy(prefix);
4312 umap1 = isl_union_map_intersect_domain(umap1, domain);
4313 umap2 = isl_union_map_intersect_domain(prefix, range);
4314 umap1 = isl_union_map_intersect_range(umap1,
4315 isl_union_set_from_set(context));
4316 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4317 umap = isl_union_map_intersect(umap, umap1);
4319 after = after_in_child(umap, node);
4321 isl_union_map_free(umap);
4323 return after;
4326 /* Is any domain element of "umap" scheduled after any of
4327 * the corresponding image elements by the tree rooted at
4328 * the expansion node "node"?
4330 * We apply the expansion to domain and range of "umap" and
4331 * continue with its child.
4333 static int after_in_expansion(__isl_keep isl_union_map *umap,
4334 __isl_keep isl_schedule_node *node)
4336 isl_union_map *expansion;
4337 int after;
4339 expansion = isl_schedule_node_expansion_get_expansion(node);
4340 umap = isl_union_map_copy(umap);
4341 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4342 umap = isl_union_map_apply_range(umap, expansion);
4344 after = after_in_child(umap, node);
4346 isl_union_map_free(umap);
4348 return after;
4351 /* Is any domain element of "umap" scheduled after any of
4352 * the corresponding image elements by the tree rooted at
4353 * the extension node "node"?
4355 * Since the extension node may add statement instances before or
4356 * after the pairs of statement instances in "umap", we return 1
4357 * to ensure that these pairs are not broken up.
4359 static int after_in_extension(__isl_keep isl_union_map *umap,
4360 __isl_keep isl_schedule_node *node)
4362 return 1;
4365 /* Is any domain element of "umap" scheduled after any of
4366 * the corresponding image elements by the tree rooted at
4367 * the filter node "node"?
4369 * We intersect domain and range of "umap" with the filter and
4370 * continue with its child.
4372 static int after_in_filter(__isl_keep isl_union_map *umap,
4373 __isl_keep isl_schedule_node *node)
4375 isl_union_set *filter;
4376 int after;
4378 umap = isl_union_map_copy(umap);
4379 filter = isl_schedule_node_filter_get_filter(node);
4380 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4381 umap = isl_union_map_intersect_range(umap, filter);
4383 after = after_in_child(umap, node);
4385 isl_union_map_free(umap);
4387 return after;
4390 /* Is any domain element of "umap" scheduled after any of
4391 * the corresponding image elements by the tree rooted at
4392 * the set node "node"?
4394 * This is only the case if this condition holds in any
4395 * of the (filter) children of the set node.
4396 * In particular, if the domain and the range of "umap"
4397 * are contained in different children, then the condition
4398 * does not hold.
4400 static int after_in_set(__isl_keep isl_union_map *umap,
4401 __isl_keep isl_schedule_node *node)
4403 int i, n;
4405 n = isl_schedule_node_n_children(node);
4406 for (i = 0; i < n; ++i) {
4407 isl_schedule_node *child;
4408 int after;
4410 child = isl_schedule_node_get_child(node, i);
4411 after = after_in_tree(umap, child);
4412 isl_schedule_node_free(child);
4414 if (after < 0 || after)
4415 return after;
4418 return 0;
4421 /* Return the filter of child "i" of "node".
4423 static __isl_give isl_union_set *child_filter(
4424 __isl_keep isl_schedule_node *node, int i)
4426 isl_schedule_node *child;
4427 isl_union_set *filter;
4429 child = isl_schedule_node_get_child(node, i);
4430 filter = isl_schedule_node_filter_get_filter(child);
4431 isl_schedule_node_free(child);
4433 return filter;
4436 /* Is any domain element of "umap" scheduled after any of
4437 * the corresponding image elements by the tree rooted at
4438 * the sequence node "node"?
4440 * This happens in particular if any domain element is
4441 * contained in a later child than one containing a range element or
4442 * if the condition holds within a given child in the sequence.
4443 * The later part of the condition is checked by after_in_set.
4445 static int after_in_sequence(__isl_keep isl_union_map *umap,
4446 __isl_keep isl_schedule_node *node)
4448 int i, j, n;
4449 isl_union_map *umap_i;
4450 int empty, after = 0;
4452 n = isl_schedule_node_n_children(node);
4453 for (i = 1; i < n; ++i) {
4454 isl_union_set *filter_i;
4456 umap_i = isl_union_map_copy(umap);
4457 filter_i = child_filter(node, i);
4458 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4459 empty = isl_union_map_is_empty(umap_i);
4460 if (empty < 0)
4461 goto error;
4462 if (empty) {
4463 isl_union_map_free(umap_i);
4464 continue;
4467 for (j = 0; j < i; ++j) {
4468 isl_union_set *filter_j;
4469 isl_union_map *umap_ij;
4471 umap_ij = isl_union_map_copy(umap_i);
4472 filter_j = child_filter(node, j);
4473 umap_ij = isl_union_map_intersect_range(umap_ij,
4474 filter_j);
4475 empty = isl_union_map_is_empty(umap_ij);
4476 isl_union_map_free(umap_ij);
4478 if (empty < 0)
4479 goto error;
4480 if (!empty)
4481 after = 1;
4482 if (after)
4483 break;
4486 isl_union_map_free(umap_i);
4487 if (after)
4488 break;
4491 if (after < 0 || after)
4492 return after;
4494 return after_in_set(umap, node);
4495 error:
4496 isl_union_map_free(umap_i);
4497 return -1;
4500 /* Is any domain element of "umap" scheduled after any of
4501 * the corresponding image elements by the tree rooted at "node"?
4503 * If "umap" is empty, then clearly there is no such element.
4504 * Otherwise, consider the different types of nodes separately.
4506 static int after_in_tree(__isl_keep isl_union_map *umap,
4507 __isl_keep isl_schedule_node *node)
4509 int empty;
4510 enum isl_schedule_node_type type;
4512 empty = isl_union_map_is_empty(umap);
4513 if (empty < 0)
4514 return -1;
4515 if (empty)
4516 return 0;
4517 if (!node)
4518 return -1;
4520 type = isl_schedule_node_get_type(node);
4521 switch (type) {
4522 case isl_schedule_node_error:
4523 return -1;
4524 case isl_schedule_node_leaf:
4525 return 0;
4526 case isl_schedule_node_band:
4527 return after_in_band(umap, node);
4528 case isl_schedule_node_domain:
4529 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4530 "unexpected internal domain node", return -1);
4531 case isl_schedule_node_context:
4532 return after_in_context(umap, node);
4533 case isl_schedule_node_expansion:
4534 return after_in_expansion(umap, node);
4535 case isl_schedule_node_extension:
4536 return after_in_extension(umap, node);
4537 case isl_schedule_node_filter:
4538 return after_in_filter(umap, node);
4539 case isl_schedule_node_guard:
4540 case isl_schedule_node_mark:
4541 return after_in_child(umap, node);
4542 case isl_schedule_node_set:
4543 return after_in_set(umap, node);
4544 case isl_schedule_node_sequence:
4545 return after_in_sequence(umap, node);
4548 return 1;
4551 /* Is any domain element of "map1" scheduled after any domain
4552 * element of "map2" by the subtree underneath the current band node,
4553 * while at the same time being scheduled together by the current
4554 * band node, i.e., by "map1" and "map2?
4556 * If the child of the current band node is a leaf, then
4557 * no element can be scheduled after any other element.
4559 * Otherwise, we construct a relation between domain elements
4560 * of "map1" and domain elements of "map2" that are scheduled
4561 * together and then check if the subtree underneath the current
4562 * band node determines their relative order.
4564 static int after_in_subtree(__isl_keep isl_ast_build *build,
4565 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4567 isl_schedule_node *node;
4568 isl_map *map;
4569 isl_union_map *umap;
4570 int after;
4572 node = isl_ast_build_get_schedule_node(build);
4573 if (!node)
4574 return -1;
4575 node = isl_schedule_node_child(node, 0);
4576 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4577 isl_schedule_node_free(node);
4578 return 0;
4580 map = isl_map_copy(map2);
4581 map = isl_map_apply_domain(map, isl_map_copy(map1));
4582 umap = isl_union_map_from_map(map);
4583 after = after_in_tree(umap, node);
4584 isl_union_map_free(umap);
4585 isl_schedule_node_free(node);
4586 return after;
4589 /* Internal data for any_scheduled_after.
4591 * "build" is the build in which the AST is constructed.
4592 * "depth" is the number of loops that have already been generated
4593 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4594 * "domain" is an array of set-map pairs corresponding to the different
4595 * iteration domains. The set is the schedule domain, i.e., the domain
4596 * of the inverse schedule, while the map is the inverse schedule itself.
4598 struct isl_any_scheduled_after_data {
4599 isl_ast_build *build;
4600 int depth;
4601 int group_coscheduled;
4602 struct isl_set_map_pair *domain;
4605 /* Is any element of domain "i" scheduled after any element of domain "j"
4606 * (for a common iteration of the first data->depth loops)?
4608 * data->domain[i].set contains the domain of the inverse schedule
4609 * for domain "i", i.e., elements in the schedule domain.
4611 * If we are inside a band of a schedule tree and there is a pair
4612 * of elements in the two domains that is schedule together by
4613 * the current band, then we check if any element of "i" may be schedule
4614 * after element of "j" by the descendants of the band node.
4616 * If data->group_coscheduled is set, then we also return 1 if there
4617 * is any pair of elements in the two domains that are scheduled together.
4619 static isl_bool any_scheduled_after(int i, int j, void *user)
4621 struct isl_any_scheduled_after_data *data = user;
4622 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4623 int pos;
4625 for (pos = data->depth; pos < dim; ++pos) {
4626 int follows;
4628 follows = isl_set_follows_at(data->domain[i].set,
4629 data->domain[j].set, pos);
4631 if (follows < -1)
4632 return isl_bool_error;
4633 if (follows > 0)
4634 return isl_bool_true;
4635 if (follows < 0)
4636 return isl_bool_false;
4639 if (isl_ast_build_has_schedule_node(data->build)) {
4640 int after;
4642 after = after_in_subtree(data->build, data->domain[i].map,
4643 data->domain[j].map);
4644 if (after < 0 || after)
4645 return after;
4648 return data->group_coscheduled;
4651 /* Look for independent components at the current depth and generate code
4652 * for each component separately. The resulting lists of grafts are
4653 * merged in an attempt to combine grafts with identical guards.
4655 * Code for two domains can be generated separately if all the elements
4656 * of one domain are scheduled before (or together with) all the elements
4657 * of the other domain. We therefore consider the graph with as nodes
4658 * the domains and an edge between two nodes if any element of the first
4659 * node is scheduled after any element of the second node.
4660 * If the ast_build_group_coscheduled is set, then we also add an edge if
4661 * there is any pair of elements in the two domains that are scheduled
4662 * together.
4663 * Code is then generated (by generate_component)
4664 * for each of the strongly connected components in this graph
4665 * in their topological order.
4667 * Since the test is performed on the domain of the inverse schedules of
4668 * the different domains, we precompute these domains and store
4669 * them in data.domain.
4671 static __isl_give isl_ast_graft_list *generate_components(
4672 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4674 int i;
4675 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4676 int n = isl_union_map_n_map(executed);
4677 struct isl_any_scheduled_after_data data;
4678 struct isl_set_map_pair *next;
4679 struct isl_tarjan_graph *g = NULL;
4680 isl_ast_graft_list *list = NULL;
4681 int n_domain = 0;
4683 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4684 if (!data.domain)
4685 goto error;
4686 n_domain = n;
4688 next = data.domain;
4689 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4690 goto error;
4692 if (!build)
4693 goto error;
4694 data.build = build;
4695 data.depth = isl_ast_build_get_depth(build);
4696 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4697 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4698 if (!g)
4699 goto error;
4701 list = isl_ast_graft_list_alloc(ctx, 0);
4703 i = 0;
4704 while (list && n) {
4705 isl_ast_graft_list *list_c;
4706 int first = i;
4708 if (g->order[i] == -1)
4709 isl_die(ctx, isl_error_internal, "cannot happen",
4710 goto error);
4711 ++i; --n;
4712 while (g->order[i] != -1) {
4713 ++i; --n;
4716 list_c = generate_component(data.domain,
4717 g->order + first, i - first,
4718 isl_ast_build_copy(build));
4719 list = isl_ast_graft_list_merge(list, list_c, build);
4721 ++i;
4724 if (0)
4725 error: list = isl_ast_graft_list_free(list);
4726 isl_tarjan_graph_free(g);
4727 for (i = 0; i < n_domain; ++i) {
4728 isl_map_free(data.domain[i].map);
4729 isl_set_free(data.domain[i].set);
4731 free(data.domain);
4732 isl_union_map_free(executed);
4733 isl_ast_build_free(build);
4735 return list;
4738 /* Generate code for the next level (and all inner levels).
4740 * If "executed" is empty, i.e., no code needs to be generated,
4741 * then we return an empty list.
4743 * If we have already generated code for all loop levels, then we pass
4744 * control to generate_inner_level.
4746 * If "executed" lives in a single space, i.e., if code needs to be
4747 * generated for a single domain, then there can only be a single
4748 * component and we go directly to generate_shifted_component.
4749 * Otherwise, we call generate_components to detect the components
4750 * and to call generate_component on each of them separately.
4752 static __isl_give isl_ast_graft_list *generate_next_level(
4753 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4755 int depth;
4757 if (!build || !executed)
4758 goto error;
4760 if (isl_union_map_is_empty(executed)) {
4761 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4762 isl_union_map_free(executed);
4763 isl_ast_build_free(build);
4764 return isl_ast_graft_list_alloc(ctx, 0);
4767 depth = isl_ast_build_get_depth(build);
4768 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4769 return generate_inner_level(executed, build);
4771 if (isl_union_map_n_map(executed) == 1)
4772 return generate_shifted_component(executed, build);
4774 return generate_components(executed, build);
4775 error:
4776 isl_union_map_free(executed);
4777 isl_ast_build_free(build);
4778 return NULL;
4781 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4782 * internal, executed and build are the inputs to generate_code.
4783 * list collects the output.
4785 struct isl_generate_code_data {
4786 int internal;
4787 isl_union_map *executed;
4788 isl_ast_build *build;
4790 isl_ast_graft_list *list;
4793 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4795 * [E -> S] -> D
4797 * with E the external build schedule and S the additional schedule "space",
4798 * reformulate the inverse schedule in terms of the internal schedule domain,
4799 * i.e., return
4801 * [I -> S] -> D
4803 * We first obtain a mapping
4805 * I -> E
4807 * take the inverse and the product with S -> S, resulting in
4809 * [I -> S] -> [E -> S]
4811 * Applying the map to the input produces the desired result.
4813 static __isl_give isl_union_map *internal_executed(
4814 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4815 __isl_keep isl_ast_build *build)
4817 isl_map *id, *proj;
4819 proj = isl_ast_build_get_schedule_map(build);
4820 proj = isl_map_reverse(proj);
4821 space = isl_space_map_from_set(isl_space_copy(space));
4822 id = isl_map_identity(space);
4823 proj = isl_map_product(proj, id);
4824 executed = isl_union_map_apply_domain(executed,
4825 isl_union_map_from_map(proj));
4826 return executed;
4829 /* Generate an AST that visits the elements in the range of data->executed
4830 * in the relative order specified by the corresponding domain element(s)
4831 * for those domain elements that belong to "set".
4832 * Add the result to data->list.
4834 * The caller ensures that "set" is a universe domain.
4835 * "space" is the space of the additional part of the schedule.
4836 * It is equal to the space of "set" if build->domain is parametric.
4837 * Otherwise, it is equal to the range of the wrapped space of "set".
4839 * If the build space is not parametric and
4840 * if isl_ast_build_node_from_schedule_map
4841 * was called from an outside user (data->internal not set), then
4842 * the (inverse) schedule refers to the external build domain and needs to
4843 * be transformed to refer to the internal build domain.
4845 * If the build space is parametric, then we add some of the parameter
4846 * constraints to the executed relation. Adding these constraints
4847 * allows for an earlier detection of conflicts in some cases.
4848 * However, we do not want to divide the executed relation into
4849 * more disjuncts than necessary. We therefore approximate
4850 * the constraints on the parameters by a single disjunct set.
4852 * The build is extended to include the additional part of the schedule.
4853 * If the original build space was not parametric, then the options
4854 * in data->build refer only to the additional part of the schedule
4855 * and they need to be adjusted to refer to the complete AST build
4856 * domain.
4858 * After having adjusted inverse schedule and build, we start generating
4859 * code with the outer loop of the current code generation
4860 * in generate_next_level.
4862 * If the original build space was not parametric, we undo the embedding
4863 * on the resulting isl_ast_node_list so that it can be used within
4864 * the outer AST build.
4866 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4867 __isl_take isl_set *set, __isl_take isl_space *space)
4869 isl_union_map *executed;
4870 isl_ast_build *build;
4871 isl_ast_graft_list *list;
4872 int embed;
4874 executed = isl_union_map_copy(data->executed);
4875 executed = isl_union_map_intersect_domain(executed,
4876 isl_union_set_from_set(set));
4878 embed = !isl_set_is_params(data->build->domain);
4879 if (embed && !data->internal)
4880 executed = internal_executed(executed, space, data->build);
4881 if (!embed) {
4882 isl_set *domain;
4883 domain = isl_ast_build_get_domain(data->build);
4884 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4885 executed = isl_union_map_intersect_params(executed, domain);
4888 build = isl_ast_build_copy(data->build);
4889 build = isl_ast_build_product(build, space);
4891 list = generate_next_level(executed, build);
4893 list = isl_ast_graft_list_unembed(list, embed);
4895 data->list = isl_ast_graft_list_concat(data->list, list);
4897 return isl_stat_ok;
4900 /* Generate an AST that visits the elements in the range of data->executed
4901 * in the relative order specified by the corresponding domain element(s)
4902 * for those domain elements that belong to "set".
4903 * Add the result to data->list.
4905 * The caller ensures that "set" is a universe domain.
4907 * If the build space S is not parametric, then the space of "set"
4908 * need to be a wrapped relation with S as domain. That is, it needs
4909 * to be of the form
4911 * [S -> T]
4913 * Check this property and pass control to generate_code_in_space
4914 * passing along T.
4915 * If the build space is not parametric, then T is the space of "set".
4917 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4919 struct isl_generate_code_data *data = user;
4920 isl_space *space, *build_space;
4921 int is_domain;
4923 space = isl_set_get_space(set);
4925 if (isl_set_is_params(data->build->domain))
4926 return generate_code_in_space(data, set, space);
4928 build_space = isl_ast_build_get_space(data->build, data->internal);
4929 space = isl_space_unwrap(space);
4930 is_domain = isl_space_is_domain(build_space, space);
4931 isl_space_free(build_space);
4932 space = isl_space_range(space);
4934 if (is_domain < 0)
4935 goto error;
4936 if (!is_domain)
4937 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4938 "invalid nested schedule space", goto error);
4940 return generate_code_in_space(data, set, space);
4941 error:
4942 isl_set_free(set);
4943 isl_space_free(space);
4944 return isl_stat_error;
4947 /* Generate an AST that visits the elements in the range of "executed"
4948 * in the relative order specified by the corresponding domain element(s).
4950 * "build" is an isl_ast_build that has either been constructed by
4951 * isl_ast_build_from_context or passed to a callback set by
4952 * isl_ast_build_set_create_leaf.
4953 * In the first case, the space of the isl_ast_build is typically
4954 * a parametric space, although this is currently not enforced.
4955 * In the second case, the space is never a parametric space.
4956 * If the space S is not parametric, then the domain space(s) of "executed"
4957 * need to be wrapped relations with S as domain.
4959 * If the domain of "executed" consists of several spaces, then an AST
4960 * is generated for each of them (in arbitrary order) and the results
4961 * are concatenated.
4963 * If "internal" is set, then the domain "S" above refers to the internal
4964 * schedule domain representation. Otherwise, it refers to the external
4965 * representation, as returned by isl_ast_build_get_schedule_space.
4967 * We essentially run over all the spaces in the domain of "executed"
4968 * and call generate_code_set on each of them.
4970 static __isl_give isl_ast_graft_list *generate_code(
4971 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4972 int internal)
4974 isl_ctx *ctx;
4975 struct isl_generate_code_data data = { 0 };
4976 isl_space *space;
4977 isl_union_set *schedule_domain;
4978 isl_union_map *universe;
4980 if (!build)
4981 goto error;
4982 space = isl_ast_build_get_space(build, 1);
4983 space = isl_space_align_params(space,
4984 isl_union_map_get_space(executed));
4985 space = isl_space_align_params(space,
4986 isl_union_map_get_space(build->options));
4987 build = isl_ast_build_align_params(build, isl_space_copy(space));
4988 executed = isl_union_map_align_params(executed, space);
4989 if (!executed || !build)
4990 goto error;
4992 ctx = isl_ast_build_get_ctx(build);
4994 data.internal = internal;
4995 data.executed = executed;
4996 data.build = build;
4997 data.list = isl_ast_graft_list_alloc(ctx, 0);
4999 universe = isl_union_map_universe(isl_union_map_copy(executed));
5000 schedule_domain = isl_union_map_domain(universe);
5001 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5002 &data) < 0)
5003 data.list = isl_ast_graft_list_free(data.list);
5005 isl_union_set_free(schedule_domain);
5006 isl_union_map_free(executed);
5008 isl_ast_build_free(build);
5009 return data.list;
5010 error:
5011 isl_union_map_free(executed);
5012 isl_ast_build_free(build);
5013 return NULL;
5016 /* Generate an AST that visits the elements in the domain of "schedule"
5017 * in the relative order specified by the corresponding image element(s).
5019 * "build" is an isl_ast_build that has either been constructed by
5020 * isl_ast_build_from_context or passed to a callback set by
5021 * isl_ast_build_set_create_leaf.
5022 * In the first case, the space of the isl_ast_build is typically
5023 * a parametric space, although this is currently not enforced.
5024 * In the second case, the space is never a parametric space.
5025 * If the space S is not parametric, then the range space(s) of "schedule"
5026 * need to be wrapped relations with S as domain.
5028 * If the range of "schedule" consists of several spaces, then an AST
5029 * is generated for each of them (in arbitrary order) and the results
5030 * are concatenated.
5032 * We first initialize the local copies of the relevant options.
5033 * We do this here rather than when the isl_ast_build is created
5034 * because the options may have changed between the construction
5035 * of the isl_ast_build and the call to isl_generate_code.
5037 * The main computation is performed on an inverse schedule (with
5038 * the schedule domain in the domain and the elements to be executed
5039 * in the range) called "executed".
5041 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5042 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5044 isl_ast_graft_list *list;
5045 isl_ast_node *node;
5046 isl_union_map *executed;
5048 build = isl_ast_build_copy(build);
5049 build = isl_ast_build_set_single_valued(build, 0);
5050 schedule = isl_union_map_coalesce(schedule);
5051 schedule = isl_union_map_remove_redundancies(schedule);
5052 executed = isl_union_map_reverse(schedule);
5053 list = generate_code(executed, isl_ast_build_copy(build), 0);
5054 node = isl_ast_node_from_graft_list(list, build);
5055 isl_ast_build_free(build);
5057 return node;
5060 /* The old name for isl_ast_build_node_from_schedule_map.
5061 * It is being kept for backward compatibility, but
5062 * it will be removed in the future.
5064 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5065 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5067 return isl_ast_build_node_from_schedule_map(build, schedule);
5070 /* Generate an AST that visits the elements in the domain of "executed"
5071 * in the relative order specified by the band node "node" and its descendants.
5073 * The relation "executed" maps the outer generated loop iterators
5074 * to the domain elements executed by those iterations.
5076 * If the band is empty, we continue with its descendants.
5077 * Otherwise, we extend the build and the inverse schedule with
5078 * the additional space/partial schedule and continue generating
5079 * an AST in generate_next_level.
5080 * As soon as we have extended the inverse schedule with the additional
5081 * partial schedule, we look for equalities that may exists between
5082 * the old and the new part.
5084 static __isl_give isl_ast_graft_list *build_ast_from_band(
5085 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5086 __isl_take isl_union_map *executed)
5088 isl_space *space;
5089 isl_multi_union_pw_aff *extra;
5090 isl_union_map *extra_umap;
5091 isl_ast_graft_list *list;
5092 unsigned n1, n2;
5094 if (!build || !node || !executed)
5095 goto error;
5097 if (isl_schedule_node_band_n_member(node) == 0)
5098 return build_ast_from_child(build, node, executed);
5100 extra = isl_schedule_node_band_get_partial_schedule(node);
5101 extra = isl_multi_union_pw_aff_align_params(extra,
5102 isl_ast_build_get_space(build, 1));
5103 space = isl_multi_union_pw_aff_get_space(extra);
5105 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5106 extra_umap = isl_union_map_reverse(extra_umap);
5108 executed = isl_union_map_domain_product(executed, extra_umap);
5109 executed = isl_union_map_detect_equalities(executed);
5111 n1 = isl_ast_build_dim(build, isl_dim_param);
5112 build = isl_ast_build_product(build, space);
5113 n2 = isl_ast_build_dim(build, isl_dim_param);
5114 if (n2 > n1)
5115 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5116 "band node is not allowed to introduce new parameters",
5117 build = isl_ast_build_free(build));
5118 build = isl_ast_build_set_schedule_node(build, node);
5120 list = generate_next_level(executed, build);
5122 list = isl_ast_graft_list_unembed(list, 1);
5124 return list;
5125 error:
5126 isl_schedule_node_free(node);
5127 isl_union_map_free(executed);
5128 isl_ast_build_free(build);
5129 return NULL;
5132 /* Hoist a list of grafts (in practice containing a single graft)
5133 * from "sub_build" (which includes extra context information)
5134 * to "build".
5136 * In particular, project out all additional parameters introduced
5137 * by the context node from the enforced constraints and the guard
5138 * of the single graft.
5140 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5141 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5142 __isl_keep isl_ast_build *sub_build)
5144 isl_ast_graft *graft;
5145 isl_basic_set *enforced;
5146 isl_set *guard;
5147 unsigned n_param, extra_param;
5149 if (!build || !sub_build)
5150 return isl_ast_graft_list_free(list);
5152 n_param = isl_ast_build_dim(build, isl_dim_param);
5153 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5155 if (extra_param == n_param)
5156 return list;
5158 extra_param -= n_param;
5159 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5160 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5161 n_param, extra_param);
5162 enforced = isl_basic_set_remove_unknown_divs(enforced);
5163 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5164 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5165 n_param, extra_param);
5166 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5167 guard = isl_set_compute_divs(guard);
5168 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5169 build, sub_build);
5170 list = isl_ast_graft_list_from_ast_graft(graft);
5172 return list;
5175 /* Generate an AST that visits the elements in the domain of "executed"
5176 * in the relative order specified by the context node "node"
5177 * and its descendants.
5179 * The relation "executed" maps the outer generated loop iterators
5180 * to the domain elements executed by those iterations.
5182 * The context node may introduce additional parameters as well as
5183 * constraints on the outer schedule dimensions or original parameters.
5185 * We add the extra parameters to a new build and the context
5186 * constraints to both the build and (as a single disjunct)
5187 * to the domain of "executed". Since the context constraints
5188 * are specified in terms of the input schedule, we first need
5189 * to map them to the internal schedule domain.
5191 * After constructing the AST from the descendants of "node",
5192 * we combine the list of grafts into a single graft within
5193 * the new build, in order to be able to exploit the additional
5194 * context constraints during this combination.
5196 * Additionally, if the current node is the outermost node in
5197 * the schedule tree (apart from the root domain node), we generate
5198 * all pending guards, again to be able to exploit the additional
5199 * context constraints. We currently do not do this for internal
5200 * context nodes since we may still want to hoist conditions
5201 * to outer AST nodes.
5203 * If the context node introduced any new parameters, then they
5204 * are removed from the set of enforced constraints and guard
5205 * in hoist_out_of_context.
5207 static __isl_give isl_ast_graft_list *build_ast_from_context(
5208 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5209 __isl_take isl_union_map *executed)
5211 isl_set *context;
5212 isl_space *space;
5213 isl_multi_aff *internal2input;
5214 isl_ast_build *sub_build;
5215 isl_ast_graft_list *list;
5216 int n, depth;
5218 depth = isl_schedule_node_get_tree_depth(node);
5219 space = isl_ast_build_get_space(build, 1);
5220 context = isl_schedule_node_context_get_context(node);
5221 context = isl_set_align_params(context, space);
5222 sub_build = isl_ast_build_copy(build);
5223 space = isl_set_get_space(context);
5224 sub_build = isl_ast_build_align_params(sub_build, space);
5225 internal2input = isl_ast_build_get_internal2input(sub_build);
5226 context = isl_set_preimage_multi_aff(context, internal2input);
5227 sub_build = isl_ast_build_restrict_generated(sub_build,
5228 isl_set_copy(context));
5229 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5230 executed = isl_union_map_intersect_domain(executed,
5231 isl_union_set_from_set(context));
5233 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5234 node, executed);
5235 n = isl_ast_graft_list_n_ast_graft(list);
5236 if (n < 0)
5237 list = isl_ast_graft_list_free(list);
5239 list = isl_ast_graft_list_fuse(list, sub_build);
5240 if (depth == 1)
5241 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5242 sub_build);
5243 if (n >= 1)
5244 list = hoist_out_of_context(list, build, sub_build);
5246 isl_ast_build_free(build);
5247 isl_ast_build_free(sub_build);
5249 return list;
5252 /* Generate an AST that visits the elements in the domain of "executed"
5253 * in the relative order specified by the expansion node "node" and
5254 * its descendants.
5256 * The relation "executed" maps the outer generated loop iterators
5257 * to the domain elements executed by those iterations.
5259 * We expand the domain elements by the expansion and
5260 * continue with the descendants of the node.
5262 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5263 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5264 __isl_take isl_union_map *executed)
5266 isl_union_map *expansion;
5267 unsigned n1, n2;
5269 expansion = isl_schedule_node_expansion_get_expansion(node);
5270 expansion = isl_union_map_align_params(expansion,
5271 isl_union_map_get_space(executed));
5273 n1 = isl_union_map_dim(executed, isl_dim_param);
5274 executed = isl_union_map_apply_range(executed, expansion);
5275 n2 = isl_union_map_dim(executed, isl_dim_param);
5276 if (n2 > n1)
5277 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5278 "expansion node is not allowed to introduce "
5279 "new parameters", goto error);
5281 return build_ast_from_child(build, node, executed);
5282 error:
5283 isl_ast_build_free(build);
5284 isl_schedule_node_free(node);
5285 isl_union_map_free(executed);
5286 return NULL;
5289 /* Generate an AST that visits the elements in the domain of "executed"
5290 * in the relative order specified by the extension node "node" and
5291 * its descendants.
5293 * The relation "executed" maps the outer generated loop iterators
5294 * to the domain elements executed by those iterations.
5296 * Extend the inverse schedule with the extension applied to current
5297 * set of generated constraints. Since the extension if formulated
5298 * in terms of the input schedule, it first needs to be transformed
5299 * to refer to the internal schedule.
5301 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5302 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5303 __isl_take isl_union_map *executed)
5305 isl_union_set *schedule_domain;
5306 isl_union_map *extension;
5307 isl_set *set;
5309 set = isl_ast_build_get_generated(build);
5310 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5311 schedule_domain = isl_union_set_from_set(set);
5313 extension = isl_schedule_node_extension_get_extension(node);
5315 extension = isl_union_map_preimage_domain_multi_aff(extension,
5316 isl_multi_aff_copy(build->internal2input));
5317 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5318 extension = isl_ast_build_substitute_values_union_map_domain(build,
5319 extension);
5320 executed = isl_union_map_union(executed, extension);
5322 return build_ast_from_child(build, node, executed);
5325 /* Generate an AST that visits the elements in the domain of "executed"
5326 * in the relative order specified by the filter node "node" and
5327 * its descendants.
5329 * The relation "executed" maps the outer generated loop iterators
5330 * to the domain elements executed by those iterations.
5332 * We simply intersect the iteration domain (i.e., the range of "executed")
5333 * with the filter and continue with the descendants of the node,
5334 * unless the resulting inverse schedule is empty, in which
5335 * case we return an empty list.
5337 * If the result of the intersection is equal to the original "executed"
5338 * relation, then keep the original representation since the intersection
5339 * may have unnecessarily broken up the relation into a greater number
5340 * of disjuncts.
5342 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5343 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5344 __isl_take isl_union_map *executed)
5346 isl_ctx *ctx;
5347 isl_union_set *filter;
5348 isl_union_map *orig;
5349 isl_ast_graft_list *list;
5350 int empty;
5351 isl_bool unchanged;
5352 unsigned n1, n2;
5354 orig = isl_union_map_copy(executed);
5355 if (!build || !node || !executed)
5356 goto error;
5358 filter = isl_schedule_node_filter_get_filter(node);
5359 filter = isl_union_set_align_params(filter,
5360 isl_union_map_get_space(executed));
5361 n1 = isl_union_map_dim(executed, isl_dim_param);
5362 executed = isl_union_map_intersect_range(executed, filter);
5363 n2 = isl_union_map_dim(executed, isl_dim_param);
5364 if (n2 > n1)
5365 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5366 "filter node is not allowed to introduce "
5367 "new parameters", goto error);
5369 unchanged = isl_union_map_is_subset(orig, executed);
5370 empty = isl_union_map_is_empty(executed);
5371 if (unchanged < 0 || empty < 0)
5372 goto error;
5373 if (unchanged) {
5374 isl_union_map_free(executed);
5375 return build_ast_from_child(build, node, orig);
5377 isl_union_map_free(orig);
5378 if (!empty)
5379 return build_ast_from_child(build, node, executed);
5381 ctx = isl_ast_build_get_ctx(build);
5382 list = isl_ast_graft_list_alloc(ctx, 0);
5383 isl_ast_build_free(build);
5384 isl_schedule_node_free(node);
5385 isl_union_map_free(executed);
5386 return list;
5387 error:
5388 isl_ast_build_free(build);
5389 isl_schedule_node_free(node);
5390 isl_union_map_free(executed);
5391 isl_union_map_free(orig);
5392 return NULL;
5395 /* Generate an AST that visits the elements in the domain of "executed"
5396 * in the relative order specified by the guard node "node" and
5397 * its descendants.
5399 * The relation "executed" maps the outer generated loop iterators
5400 * to the domain elements executed by those iterations.
5402 * Ensure that the associated guard is enforced by the outer AST
5403 * constructs by adding it to the guard of the graft.
5404 * Since we know that we will enforce the guard, we can also include it
5405 * in the generated constraints used to construct an AST for
5406 * the descendant nodes.
5408 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5409 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5410 __isl_take isl_union_map *executed)
5412 isl_space *space;
5413 isl_set *guard, *hoisted;
5414 isl_basic_set *enforced;
5415 isl_ast_build *sub_build;
5416 isl_ast_graft *graft;
5417 isl_ast_graft_list *list;
5418 unsigned n1, n2;
5420 space = isl_ast_build_get_space(build, 1);
5421 guard = isl_schedule_node_guard_get_guard(node);
5422 n1 = isl_space_dim(space, isl_dim_param);
5423 guard = isl_set_align_params(guard, space);
5424 n2 = isl_set_dim(guard, isl_dim_param);
5425 if (n2 > n1)
5426 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5427 "guard node is not allowed to introduce "
5428 "new parameters", guard = isl_set_free(guard));
5429 guard = isl_set_preimage_multi_aff(guard,
5430 isl_multi_aff_copy(build->internal2input));
5431 guard = isl_ast_build_specialize(build, guard);
5432 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5434 sub_build = isl_ast_build_copy(build);
5435 sub_build = isl_ast_build_restrict_generated(sub_build,
5436 isl_set_copy(guard));
5438 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5439 node, executed);
5441 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5442 if (isl_set_n_basic_set(hoisted) > 1)
5443 list = isl_ast_graft_list_gist_guards(list,
5444 isl_set_copy(hoisted));
5445 guard = isl_set_intersect(guard, hoisted);
5446 enforced = extract_shared_enforced(list, build);
5447 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5448 build, sub_build);
5450 isl_ast_build_free(sub_build);
5451 isl_ast_build_free(build);
5452 return isl_ast_graft_list_from_ast_graft(graft);
5455 /* Call the before_each_mark callback, if requested by the user.
5457 * Return 0 on success and -1 on error.
5459 * The caller is responsible for recording the current inverse schedule
5460 * in "build".
5462 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5463 __isl_keep isl_ast_build *build)
5465 if (!build)
5466 return isl_stat_error;
5467 if (!build->before_each_mark)
5468 return isl_stat_ok;
5469 return build->before_each_mark(mark, build,
5470 build->before_each_mark_user);
5473 /* Call the after_each_mark callback, if requested by the user.
5475 * The caller is responsible for recording the current inverse schedule
5476 * in "build".
5478 static __isl_give isl_ast_graft *after_each_mark(
5479 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5481 if (!graft || !build)
5482 return isl_ast_graft_free(graft);
5483 if (!build->after_each_mark)
5484 return graft;
5485 graft->node = build->after_each_mark(graft->node, build,
5486 build->after_each_mark_user);
5487 if (!graft->node)
5488 return isl_ast_graft_free(graft);
5489 return graft;
5493 /* Generate an AST that visits the elements in the domain of "executed"
5494 * in the relative order specified by the mark node "node" and
5495 * its descendants.
5497 * The relation "executed" maps the outer generated loop iterators
5498 * to the domain elements executed by those iterations.
5500 * Since we may be calling before_each_mark and after_each_mark
5501 * callbacks, we record the current inverse schedule in the build.
5503 * We generate an AST for the child of the mark node, combine
5504 * the graft list into a single graft and then insert the mark
5505 * in the AST of that single graft.
5507 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5508 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5509 __isl_take isl_union_map *executed)
5511 isl_id *mark;
5512 isl_ast_graft *graft;
5513 isl_ast_graft_list *list;
5514 int n;
5516 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5518 mark = isl_schedule_node_mark_get_id(node);
5519 if (before_each_mark(mark, build) < 0)
5520 node = isl_schedule_node_free(node);
5522 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5523 list = isl_ast_graft_list_fuse(list, build);
5524 n = isl_ast_graft_list_n_ast_graft(list);
5525 if (n < 0)
5526 list = isl_ast_graft_list_free(list);
5527 if (n == 0) {
5528 isl_id_free(mark);
5529 } else {
5530 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5531 graft = isl_ast_graft_insert_mark(graft, mark);
5532 graft = after_each_mark(graft, build);
5533 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5535 isl_ast_build_free(build);
5537 return list;
5540 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5541 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5542 __isl_take isl_union_map *executed);
5544 /* Generate an AST that visits the elements in the domain of "executed"
5545 * in the relative order specified by the sequence (or set) node "node" and
5546 * its descendants.
5548 * The relation "executed" maps the outer generated loop iterators
5549 * to the domain elements executed by those iterations.
5551 * We simply generate an AST for each of the children and concatenate
5552 * the results.
5554 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5555 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5556 __isl_take isl_union_map *executed)
5558 int i, n;
5559 isl_ctx *ctx;
5560 isl_ast_graft_list *list;
5562 ctx = isl_ast_build_get_ctx(build);
5563 list = isl_ast_graft_list_alloc(ctx, 0);
5565 n = isl_schedule_node_n_children(node);
5566 for (i = 0; i < n; ++i) {
5567 isl_schedule_node *child;
5568 isl_ast_graft_list *list_i;
5570 child = isl_schedule_node_get_child(node, i);
5571 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5572 child, isl_union_map_copy(executed));
5573 list = isl_ast_graft_list_concat(list, list_i);
5575 isl_ast_build_free(build);
5576 isl_schedule_node_free(node);
5577 isl_union_map_free(executed);
5579 return list;
5582 /* Generate an AST that visits the elements in the domain of "executed"
5583 * in the relative order specified by the node "node" and its descendants.
5585 * The relation "executed" maps the outer generated loop iterators
5586 * to the domain elements executed by those iterations.
5588 * If the node is a leaf, then we pass control to generate_inner_level.
5589 * Note that the current build does not refer to any band node, so
5590 * that generate_inner_level will not try to visit the child of
5591 * the leaf node.
5593 * The other node types are handled in separate functions.
5594 * Set nodes are currently treated in the same way as sequence nodes.
5595 * The children of a set node may be executed in any order,
5596 * including the order of the children.
5598 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5599 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5600 __isl_take isl_union_map *executed)
5602 enum isl_schedule_node_type type;
5604 type = isl_schedule_node_get_type(node);
5606 switch (type) {
5607 case isl_schedule_node_error:
5608 goto error;
5609 case isl_schedule_node_leaf:
5610 isl_schedule_node_free(node);
5611 return generate_inner_level(executed, build);
5612 case isl_schedule_node_band:
5613 return build_ast_from_band(build, node, executed);
5614 case isl_schedule_node_context:
5615 return build_ast_from_context(build, node, executed);
5616 case isl_schedule_node_domain:
5617 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5618 "unexpected internal domain node", goto error);
5619 case isl_schedule_node_expansion:
5620 return build_ast_from_expansion(build, node, executed);
5621 case isl_schedule_node_extension:
5622 return build_ast_from_extension(build, node, executed);
5623 case isl_schedule_node_filter:
5624 return build_ast_from_filter(build, node, executed);
5625 case isl_schedule_node_guard:
5626 return build_ast_from_guard(build, node, executed);
5627 case isl_schedule_node_mark:
5628 return build_ast_from_mark(build, node, executed);
5629 case isl_schedule_node_sequence:
5630 case isl_schedule_node_set:
5631 return build_ast_from_sequence(build, node, executed);
5634 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5635 "unhandled type", goto error);
5636 error:
5637 isl_union_map_free(executed);
5638 isl_schedule_node_free(node);
5639 isl_ast_build_free(build);
5641 return NULL;
5644 /* Generate an AST that visits the elements in the domain of "executed"
5645 * in the relative order specified by the (single) child of "node" and
5646 * its descendants.
5648 * The relation "executed" maps the outer generated loop iterators
5649 * to the domain elements executed by those iterations.
5651 * This function is never called on a leaf, set or sequence node,
5652 * so the node always has exactly one child.
5654 static __isl_give isl_ast_graft_list *build_ast_from_child(
5655 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5656 __isl_take isl_union_map *executed)
5658 node = isl_schedule_node_child(node, 0);
5659 return build_ast_from_schedule_node(build, node, executed);
5662 /* Generate an AST that visits the elements in the domain of the domain
5663 * node "node" in the relative order specified by its descendants.
5665 * An initial inverse schedule is created that maps a zero-dimensional
5666 * schedule space to the node domain.
5667 * The input "build" is assumed to have a parametric domain and
5668 * is replaced by the same zero-dimensional schedule space.
5670 * We also add some of the parameter constraints in the build domain
5671 * to the executed relation. Adding these constraints
5672 * allows for an earlier detection of conflicts in some cases.
5673 * However, we do not want to divide the executed relation into
5674 * more disjuncts than necessary. We therefore approximate
5675 * the constraints on the parameters by a single disjunct set.
5677 static __isl_give isl_ast_node *build_ast_from_domain(
5678 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5680 isl_ctx *ctx;
5681 isl_union_set *domain, *schedule_domain;
5682 isl_union_map *executed;
5683 isl_space *space;
5684 isl_set *set;
5685 isl_ast_graft_list *list;
5686 isl_ast_node *ast;
5687 int is_params;
5689 if (!build)
5690 goto error;
5692 ctx = isl_ast_build_get_ctx(build);
5693 space = isl_ast_build_get_space(build, 1);
5694 is_params = isl_space_is_params(space);
5695 isl_space_free(space);
5696 if (is_params < 0)
5697 goto error;
5698 if (!is_params)
5699 isl_die(ctx, isl_error_unsupported,
5700 "expecting parametric initial context", goto error);
5702 domain = isl_schedule_node_domain_get_domain(node);
5703 domain = isl_union_set_coalesce(domain);
5705 space = isl_union_set_get_space(domain);
5706 space = isl_space_set_from_params(space);
5707 build = isl_ast_build_product(build, space);
5709 set = isl_ast_build_get_domain(build);
5710 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5711 schedule_domain = isl_union_set_from_set(set);
5713 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5714 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5715 ast = isl_ast_node_from_graft_list(list, build);
5716 isl_ast_build_free(build);
5718 return ast;
5719 error:
5720 isl_schedule_node_free(node);
5721 isl_ast_build_free(build);
5722 return NULL;
5725 /* Generate an AST that visits the elements in the domain of "schedule"
5726 * in the relative order specified by the schedule tree.
5728 * "build" is an isl_ast_build that has been created using
5729 * isl_ast_build_alloc or isl_ast_build_from_context based
5730 * on a parametric set.
5732 * The construction starts at the root node of the schedule,
5733 * which is assumed to be a domain node.
5735 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5736 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5738 isl_ctx *ctx;
5739 isl_schedule_node *node;
5741 if (!build || !schedule)
5742 goto error;
5744 ctx = isl_ast_build_get_ctx(build);
5746 node = isl_schedule_get_root(schedule);
5747 if (!node)
5748 goto error;
5749 isl_schedule_free(schedule);
5751 build = isl_ast_build_copy(build);
5752 build = isl_ast_build_set_single_valued(build, 0);
5753 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5754 isl_die(ctx, isl_error_unsupported,
5755 "expecting root domain node",
5756 build = isl_ast_build_free(build));
5757 return build_ast_from_domain(build, node);
5758 error:
5759 isl_schedule_free(schedule);
5760 return NULL;