isl_union_*_drop_dims: use isl_union_*_transform_space
[isl.git] / isl_ast_codegen.c
blob2b791a5935bfd3be955439c0b6550c30444403b6
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/aff.h>
15 #include <isl/constraint.h>
16 #include <isl/set.h>
17 #include <isl/ilp.h>
18 #include <isl/union_set.h>
19 #include <isl/union_map.h>
20 #include <isl/schedule_node.h>
21 #include <isl_sort.h>
22 #include <isl_tarjan.h>
23 #include <isl_ast_private.h>
24 #include <isl_ast_build_expr.h>
25 #include <isl_ast_build_private.h>
26 #include <isl_ast_graft_private.h>
28 /* Data used in generate_domain.
30 * "build" is the input build.
31 * "list" collects the results.
33 struct isl_generate_domain_data {
34 isl_ast_build *build;
36 isl_ast_graft_list *list;
39 static __isl_give isl_ast_graft_list *generate_next_level(
40 __isl_take isl_union_map *executed,
41 __isl_take isl_ast_build *build);
42 static __isl_give isl_ast_graft_list *generate_code(
43 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
44 int internal);
46 /* Generate an AST for a single domain based on
47 * the (non single valued) inverse schedule "executed".
49 * We extend the schedule with the iteration domain
50 * and continue generating through a call to generate_code.
52 * In particular, if executed has the form
54 * S -> D
56 * then we continue generating code on
58 * [S -> D] -> D
60 * The extended inverse schedule is clearly single valued
61 * ensuring that the nested generate_code will not reach this function,
62 * but will instead create calls to all elements of D that need
63 * to be executed from the current schedule domain.
65 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
66 struct isl_generate_domain_data *data)
68 isl_map *identity;
69 isl_ast_build *build;
70 isl_ast_graft_list *list;
72 build = isl_ast_build_copy(data->build);
74 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
75 executed = isl_map_domain_product(executed, identity);
76 build = isl_ast_build_set_single_valued(build, 1);
78 list = generate_code(isl_union_map_from_map(executed), build, 1);
80 data->list = isl_ast_graft_list_concat(data->list, list);
82 return isl_stat_ok;
85 /* Call the at_each_domain callback, if requested by the user,
86 * after recording the current inverse schedule in the build.
88 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
89 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
91 if (!graft || !build)
92 return isl_ast_graft_free(graft);
93 if (!build->at_each_domain)
94 return graft;
96 build = isl_ast_build_copy(build);
97 build = isl_ast_build_set_executed(build,
98 isl_union_map_from_map(isl_map_copy(executed)));
99 if (!build)
100 return isl_ast_graft_free(graft);
102 graft->node = build->at_each_domain(graft->node,
103 build, build->at_each_domain_user);
104 isl_ast_build_free(build);
106 if (!graft->node)
107 graft = isl_ast_graft_free(graft);
109 return graft;
112 /* Generate an AST for a single domain based on
113 * the inverse schedule "executed" and add it to data->list.
115 * If there is more than one domain element associated to the current
116 * schedule "time", then we need to continue the generation process
117 * in generate_non_single_valued.
118 * Note that the inverse schedule being single-valued may depend
119 * on constraints that are only available in the original context
120 * domain specified by the user. We therefore first introduce
121 * some of the constraints of data->build->domain. In particular,
122 * we intersect with a single-disjunct approximation of this set.
123 * We perform this approximation to avoid further splitting up
124 * the executed relation, possibly introducing a disjunctive guard
125 * on the statement.
127 * On the other hand, we only perform the test after having taken the gist
128 * of the domain as the resulting map is the one from which the call
129 * expression is constructed. Using this map to construct the call
130 * expression usually yields simpler results.
131 * Because we perform the single-valuedness test on the gisted map,
132 * we may in rare cases fail to recognize that the inverse schedule
133 * is single-valued. This becomes problematic if this happens
134 * from the recursive call through generate_non_single_valued
135 * as we would then end up in an infinite recursion.
136 * We therefore check if we are inside a call to generate_non_single_valued
137 * and revert to the ungisted map if the gisted map turns out not to be
138 * single-valued.
140 * Otherwise, we generate a call expression for the single executed
141 * domain element and put a guard around it based on the (simplified)
142 * domain of "executed".
144 * At this stage, any pending constraints in the build can no longer
145 * be simplified with respect to any enforced constraints since
146 * the call node does not have any enforced constraints.
147 * We therefore turn all pending constraints into guards
148 * (after simplifying them with respect to the already generated
149 * constraints) and add them to both the generated constraints
150 * and the guard of the constructed graft. This guard will ensure
151 * that the constraints are effectively generated.
153 * If the user has set an at_each_domain callback, it is called
154 * on the constructed call expression node.
156 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
158 struct isl_generate_domain_data *data = user;
159 isl_ast_build *build;
160 isl_ast_graft *graft;
161 isl_ast_graft_list *list;
162 isl_set *guard, *domain;
163 isl_map *map = NULL;
164 int empty, sv;
166 domain = isl_ast_build_get_domain(data->build);
167 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
168 executed = isl_map_intersect_domain(executed, domain);
169 empty = isl_map_is_empty(executed);
170 if (empty < 0)
171 goto error;
172 if (empty) {
173 isl_map_free(executed);
174 return isl_stat_ok;
177 executed = isl_map_coalesce(executed);
178 map = isl_map_copy(executed);
179 map = isl_ast_build_compute_gist_map_domain(data->build, map);
180 sv = isl_map_is_single_valued(map);
181 if (sv < 0)
182 goto error;
183 if (!sv) {
184 isl_map_free(map);
185 if (data->build->single_valued)
186 map = isl_map_copy(executed);
187 else
188 return generate_non_single_valued(executed, data);
190 guard = isl_map_domain(isl_map_copy(map));
191 guard = isl_set_compute_divs(guard);
192 guard = isl_set_intersect(guard,
193 isl_ast_build_get_pending(data->build));
194 guard = isl_set_coalesce(guard);
195 guard = isl_ast_build_specialize(data->build, guard);
196 guard = isl_set_gist(guard, isl_ast_build_get_generated(data->build));
198 build = isl_ast_build_copy(data->build);
199 build = isl_ast_build_replace_pending_by_guard(build,
200 isl_set_copy(guard));
201 graft = isl_ast_graft_alloc_domain(map, build);
202 graft = at_each_domain(graft, executed, build);
203 isl_ast_build_free(build);
204 isl_map_free(executed);
205 graft = isl_ast_graft_add_guard(graft, guard, data->build);
207 list = isl_ast_graft_list_from_ast_graft(graft);
208 data->list = isl_ast_graft_list_concat(data->list, list);
210 return isl_stat_ok;
211 error:
212 isl_map_free(map);
213 isl_map_free(executed);
214 return isl_stat_error;
217 /* Call build->create_leaf to a create "leaf" node in the AST,
218 * encapsulate the result in an isl_ast_graft and return the result
219 * as a 1-element list.
221 * Note that the node returned by the user may be an entire tree.
223 * Since the node itself cannot enforce any constraints, we turn
224 * all pending constraints into guards and add them to the resulting
225 * graft to ensure that they will be generated.
227 * Before we pass control to the user, we first clear some information
228 * from the build that is (presumbably) only meaningful
229 * for the current code generation.
230 * This includes the create_leaf callback itself, so we make a copy
231 * of the build first.
233 static __isl_give isl_ast_graft_list *call_create_leaf(
234 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
236 isl_set *guard;
237 isl_ast_node *node;
238 isl_ast_graft *graft;
239 isl_ast_build *user_build;
241 guard = isl_ast_build_get_pending(build);
242 user_build = isl_ast_build_copy(build);
243 user_build = isl_ast_build_replace_pending_by_guard(user_build,
244 isl_set_copy(guard));
245 user_build = isl_ast_build_set_executed(user_build, executed);
246 user_build = isl_ast_build_clear_local_info(user_build);
247 if (!user_build)
248 node = NULL;
249 else
250 node = build->create_leaf(user_build, build->create_leaf_user);
251 graft = isl_ast_graft_alloc(node, build);
252 graft = isl_ast_graft_add_guard(graft, guard, build);
253 isl_ast_build_free(build);
254 return isl_ast_graft_list_from_ast_graft(graft);
257 static __isl_give isl_ast_graft_list *build_ast_from_child(
258 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
259 __isl_take isl_union_map *executed);
261 /* Generate an AST after having handled the complete schedule
262 * of this call to the code generator or the complete band
263 * if we are generating an AST from a schedule tree.
265 * If we are inside a band node, then move on to the child of the band.
267 * If the user has specified a create_leaf callback, control
268 * is passed to the user in call_create_leaf.
270 * Otherwise, we generate one or more calls for each individual
271 * domain in generate_domain.
273 static __isl_give isl_ast_graft_list *generate_inner_level(
274 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
276 isl_ctx *ctx;
277 struct isl_generate_domain_data data = { build };
279 if (!build || !executed)
280 goto error;
282 if (isl_ast_build_has_schedule_node(build)) {
283 isl_schedule_node *node;
284 node = isl_ast_build_get_schedule_node(build);
285 build = isl_ast_build_reset_schedule_node(build);
286 return build_ast_from_child(build, node, executed);
289 if (build->create_leaf)
290 return call_create_leaf(executed, build);
292 ctx = isl_union_map_get_ctx(executed);
293 data.list = isl_ast_graft_list_alloc(ctx, 0);
294 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
295 data.list = isl_ast_graft_list_free(data.list);
297 if (0)
298 error: data.list = NULL;
299 isl_ast_build_free(build);
300 isl_union_map_free(executed);
301 return data.list;
304 /* Call the before_each_for callback, if requested by the user.
306 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
307 __isl_keep isl_ast_build *build)
309 isl_id *id;
311 if (!node || !build)
312 return isl_ast_node_free(node);
313 if (!build->before_each_for)
314 return node;
315 id = build->before_each_for(build, build->before_each_for_user);
316 node = isl_ast_node_set_annotation(node, id);
317 return node;
320 /* Call the after_each_for callback, if requested by the user.
322 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
323 __isl_keep isl_ast_build *build)
325 if (!graft || !build)
326 return isl_ast_graft_free(graft);
327 if (!build->after_each_for)
328 return graft;
329 graft->node = build->after_each_for(graft->node, build,
330 build->after_each_for_user);
331 if (!graft->node)
332 return isl_ast_graft_free(graft);
333 return graft;
336 /* Plug in all the know values of the current and outer dimensions
337 * in the domain of "executed". In principle, we only need to plug
338 * in the known value of the current dimension since the values of
339 * outer dimensions have been plugged in already.
340 * However, it turns out to be easier to just plug in all known values.
342 static __isl_give isl_union_map *plug_in_values(
343 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
345 return isl_ast_build_substitute_values_union_map_domain(build,
346 executed);
349 /* Check if the constraint "c" is a lower bound on dimension "pos",
350 * an upper bound, or independent of dimension "pos".
352 static int constraint_type(isl_constraint *c, int pos)
354 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
355 return 1;
356 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
357 return 2;
358 return 0;
361 /* Compare the types of the constraints "a" and "b",
362 * resulting in constraints that are independent of "depth"
363 * to be sorted before the lower bounds on "depth", which in
364 * turn are sorted before the upper bounds on "depth".
366 static int cmp_constraint(__isl_keep isl_constraint *a,
367 __isl_keep isl_constraint *b, void *user)
369 int *depth = user;
370 int t1 = constraint_type(a, *depth);
371 int t2 = constraint_type(b, *depth);
373 return t1 - t2;
376 /* Extract a lower bound on dimension "pos" from constraint "c".
378 * If the constraint is of the form
380 * a x + f(...) >= 0
382 * then we essentially return
384 * l = ceil(-f(...)/a)
386 * However, if the current dimension is strided, then we need to make
387 * sure that the lower bound we construct is of the form
389 * f + s a
391 * with f the offset and s the stride.
392 * We therefore compute
394 * f + s * ceil((l - f)/s)
396 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
397 int pos, __isl_keep isl_ast_build *build)
399 isl_aff *aff;
401 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
402 aff = isl_aff_ceil(aff);
404 if (isl_ast_build_has_stride(build, pos)) {
405 isl_aff *offset;
406 isl_val *stride;
408 offset = isl_ast_build_get_offset(build, pos);
409 stride = isl_ast_build_get_stride(build, pos);
411 aff = isl_aff_sub(aff, isl_aff_copy(offset));
412 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
413 aff = isl_aff_ceil(aff);
414 aff = isl_aff_scale_val(aff, stride);
415 aff = isl_aff_add(aff, offset);
418 aff = isl_ast_build_compute_gist_aff(build, aff);
420 return aff;
423 /* Return the exact lower bound (or upper bound if "upper" is set)
424 * of "domain" as a piecewise affine expression.
426 * If we are computing a lower bound (of a strided dimension), then
427 * we need to make sure it is of the form
429 * f + s a
431 * where f is the offset and s is the stride.
432 * We therefore need to include the stride constraint before computing
433 * the minimum.
435 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
436 __isl_keep isl_ast_build *build, int upper)
438 isl_set *stride;
439 isl_map *it_map;
440 isl_pw_aff *pa;
441 isl_pw_multi_aff *pma;
443 domain = isl_set_copy(domain);
444 if (!upper) {
445 stride = isl_ast_build_get_stride_constraint(build);
446 domain = isl_set_intersect(domain, stride);
448 it_map = isl_ast_build_map_to_iterator(build, domain);
449 if (upper)
450 pma = isl_map_lexmax_pw_multi_aff(it_map);
451 else
452 pma = isl_map_lexmin_pw_multi_aff(it_map);
453 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
454 isl_pw_multi_aff_free(pma);
455 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
456 pa = isl_pw_aff_coalesce(pa);
458 return pa;
461 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
462 * remove_redundant_lower_bounds.
464 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
465 void *user)
467 return isl_pw_aff_plain_cmp(a, b);
470 /* Given a list of lower bounds "list", remove those that are redundant
471 * with respect to the other bounds in "list" and the domain of "build".
473 * We first sort the bounds in the same way as they would be sorted
474 * by set_for_node_expressions so that we can try and remove the last
475 * bounds first.
477 * For a lower bound to be effective, there needs to be at least
478 * one domain element for which it is larger than all other lower bounds.
479 * For each lower bound we therefore intersect the domain with
480 * the conditions that it is larger than all other bounds and
481 * check whether the result is empty. If so, the bound can be removed.
483 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
484 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
486 int i, j, n;
487 isl_set *domain;
489 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
490 if (!list)
491 return NULL;
493 n = isl_pw_aff_list_n_pw_aff(list);
494 if (n <= 1)
495 return list;
497 domain = isl_ast_build_get_domain(build);
499 for (i = n - 1; i >= 0; --i) {
500 isl_pw_aff *pa_i;
501 isl_set *domain_i;
502 int empty;
504 domain_i = isl_set_copy(domain);
505 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
507 for (j = 0; j < n; ++j) {
508 isl_pw_aff *pa_j;
509 isl_set *better;
511 if (j == i)
512 continue;
514 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
515 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
516 domain_i = isl_set_intersect(domain_i, better);
519 empty = isl_set_is_empty(domain_i);
521 isl_set_free(domain_i);
522 isl_pw_aff_free(pa_i);
524 if (empty < 0)
525 goto error;
526 if (!empty)
527 continue;
528 list = isl_pw_aff_list_drop(list, i, 1);
529 n--;
532 isl_set_free(domain);
534 return list;
535 error:
536 isl_set_free(domain);
537 return isl_pw_aff_list_free(list);
540 /* Extract a lower bound on dimension "pos" from each constraint
541 * in "constraints" and return the list of lower bounds.
542 * If "constraints" has zero elements, then we extract a lower bound
543 * from "domain" instead.
545 * If the current dimension is strided, then the lower bound
546 * is adjusted by lower_bound to match the stride information.
547 * This modification may make one or more lower bounds redundant
548 * with respect to the other lower bounds. We therefore check
549 * for this condition and remove the redundant lower bounds.
551 static __isl_give isl_pw_aff_list *lower_bounds(
552 __isl_keep isl_constraint_list *constraints, int pos,
553 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
555 isl_ctx *ctx;
556 isl_pw_aff_list *list;
557 int i, n;
559 if (!build)
560 return NULL;
562 n = isl_constraint_list_n_constraint(constraints);
563 if (n == 0) {
564 isl_pw_aff *pa;
565 pa = exact_bound(domain, build, 0);
566 return isl_pw_aff_list_from_pw_aff(pa);
569 ctx = isl_ast_build_get_ctx(build);
570 list = isl_pw_aff_list_alloc(ctx,n);
572 for (i = 0; i < n; ++i) {
573 isl_aff *aff;
574 isl_constraint *c;
576 c = isl_constraint_list_get_constraint(constraints, i);
577 aff = lower_bound(c, pos, build);
578 isl_constraint_free(c);
579 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
582 if (isl_ast_build_has_stride(build, pos))
583 list = remove_redundant_lower_bounds(list, build);
585 return list;
588 /* Extract an upper bound on dimension "pos" from each constraint
589 * in "constraints" and return the list of upper bounds.
590 * If "constraints" has zero elements, then we extract an upper bound
591 * from "domain" instead.
593 static __isl_give isl_pw_aff_list *upper_bounds(
594 __isl_keep isl_constraint_list *constraints, int pos,
595 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
597 isl_ctx *ctx;
598 isl_pw_aff_list *list;
599 int i, n;
601 n = isl_constraint_list_n_constraint(constraints);
602 if (n == 0) {
603 isl_pw_aff *pa;
604 pa = exact_bound(domain, build, 1);
605 return isl_pw_aff_list_from_pw_aff(pa);
608 ctx = isl_ast_build_get_ctx(build);
609 list = isl_pw_aff_list_alloc(ctx,n);
611 for (i = 0; i < n; ++i) {
612 isl_aff *aff;
613 isl_constraint *c;
615 c = isl_constraint_list_get_constraint(constraints, i);
616 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
617 isl_constraint_free(c);
618 aff = isl_aff_floor(aff);
619 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
622 return list;
625 /* Return an isl_ast_expr that performs the reduction of type "type"
626 * on AST expressions corresponding to the elements in "list".
628 * The list is assumed to contain at least one element.
629 * If the list contains exactly one element, then the returned isl_ast_expr
630 * simply computes that affine expression.
631 * If the list contains more than one element, then we sort it
632 * using a fairly abitrary but hopefully reasonably stable order.
634 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
635 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
637 int i, n;
638 isl_ctx *ctx;
639 isl_ast_expr *expr;
641 if (!list)
642 return NULL;
644 n = isl_pw_aff_list_n_pw_aff(list);
646 if (n == 1)
647 return isl_ast_build_expr_from_pw_aff_internal(build,
648 isl_pw_aff_list_get_pw_aff(list, 0));
650 ctx = isl_pw_aff_list_get_ctx(list);
651 expr = isl_ast_expr_alloc_op(ctx, type, n);
652 if (!expr)
653 return NULL;
655 list = isl_pw_aff_list_copy(list);
656 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
657 if (!list)
658 return isl_ast_expr_free(expr);
660 for (i = 0; i < n; ++i) {
661 isl_ast_expr *expr_i;
663 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
664 isl_pw_aff_list_get_pw_aff(list, i));
665 if (!expr_i)
666 goto error;
667 expr->u.op.args[i] = expr_i;
670 isl_pw_aff_list_free(list);
671 return expr;
672 error:
673 isl_pw_aff_list_free(list);
674 isl_ast_expr_free(expr);
675 return NULL;
678 /* Add guards implied by the "generated constraints",
679 * but not (necessarily) enforced by the generated AST to "guard".
680 * In particular, if there is any stride constraints,
681 * then add the guard implied by those constraints.
682 * If we have generated a degenerate loop, then add the guard
683 * implied by "bounds" on the outer dimensions, i.e., the guard
684 * that ensures that the single value actually exists.
685 * Since there may also be guards implied by a combination
686 * of these constraints, we first combine them before
687 * deriving the implied constraints.
689 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
690 int degenerate, __isl_keep isl_basic_set *bounds,
691 __isl_keep isl_ast_build *build)
693 int depth, has_stride;
694 isl_space *space;
695 isl_set *dom, *set;
697 depth = isl_ast_build_get_depth(build);
698 has_stride = isl_ast_build_has_stride(build, depth);
699 if (!has_stride && !degenerate)
700 return guard;
702 space = isl_basic_set_get_space(bounds);
703 dom = isl_set_universe(space);
705 if (degenerate) {
706 bounds = isl_basic_set_copy(bounds);
707 bounds = isl_basic_set_drop_constraints_not_involving_dims(
708 bounds, isl_dim_set, depth, 1);
709 set = isl_set_from_basic_set(bounds);
710 dom = isl_set_intersect(dom, set);
713 if (has_stride) {
714 set = isl_ast_build_get_stride_constraint(build);
715 dom = isl_set_intersect(dom, set);
718 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
719 dom = isl_ast_build_compute_gist(build, dom);
720 guard = isl_set_intersect(guard, dom);
722 return guard;
725 /* Update "graft" based on "sub_build" for the degenerate case.
727 * "build" is the build in which graft->node was created
728 * "sub_build" contains information about the current level itself,
729 * including the single value attained.
731 * We set the initialization part of the for loop to the single
732 * value attained by the current dimension.
733 * The increment and condition are not strictly needed as the are known
734 * to be "1" and "iterator <= value" respectively.
736 static __isl_give isl_ast_graft *refine_degenerate(
737 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
738 __isl_keep isl_ast_build *sub_build)
740 isl_pw_aff *value;
742 if (!graft || !sub_build)
743 return isl_ast_graft_free(graft);
745 value = isl_pw_aff_copy(sub_build->value);
747 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
748 value);
749 if (!graft->node->u.f.init)
750 return isl_ast_graft_free(graft);
752 return graft;
755 /* Return the intersection of constraints in "list" as a set.
757 static __isl_give isl_set *intersect_constraints(
758 __isl_keep isl_constraint_list *list)
760 int i, n;
761 isl_basic_set *bset;
763 n = isl_constraint_list_n_constraint(list);
764 if (n < 1)
765 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
766 "expecting at least one constraint", return NULL);
768 bset = isl_basic_set_from_constraint(
769 isl_constraint_list_get_constraint(list, 0));
770 for (i = 1; i < n; ++i) {
771 isl_basic_set *bset_i;
773 bset_i = isl_basic_set_from_constraint(
774 isl_constraint_list_get_constraint(list, i));
775 bset = isl_basic_set_intersect(bset, bset_i);
778 return isl_set_from_basic_set(bset);
781 /* Compute the constraints on the outer dimensions enforced by
782 * graft->node and add those constraints to graft->enforced,
783 * in case the upper bound is expressed as a set "upper".
785 * In particular, if l(...) is a lower bound in "lower", and
787 * -a i + f(...) >= 0 or a i <= f(...)
789 * is an upper bound ocnstraint on the current dimension i,
790 * then the for loop enforces the constraint
792 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
794 * We therefore simply take each lower bound in turn, plug it into
795 * the upper bounds and compute the intersection over all lower bounds.
797 * If a lower bound is a rational expression, then
798 * isl_basic_set_preimage_multi_aff will force this rational
799 * expression to have only integer values. However, the loop
800 * itself does not enforce this integrality constraint. We therefore
801 * use the ceil of the lower bounds instead of the lower bounds themselves.
802 * Other constraints will make sure that the for loop is only executed
803 * when each of the lower bounds attains an integral value.
804 * In particular, potentially rational values only occur in
805 * lower_bound if the offset is a (seemingly) rational expression,
806 * but then outer conditions will make sure that this rational expression
807 * only attains integer values.
809 static __isl_give isl_ast_graft *set_enforced_from_set(
810 __isl_take isl_ast_graft *graft,
811 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
813 isl_space *space;
814 isl_basic_set *enforced;
815 isl_pw_multi_aff *pma;
816 int i, n;
818 if (!graft || !lower)
819 return isl_ast_graft_free(graft);
821 space = isl_set_get_space(upper);
822 enforced = isl_basic_set_universe(isl_space_copy(space));
824 space = isl_space_map_from_set(space);
825 pma = isl_pw_multi_aff_identity(space);
827 n = isl_pw_aff_list_n_pw_aff(lower);
828 for (i = 0; i < n; ++i) {
829 isl_pw_aff *pa;
830 isl_set *enforced_i;
831 isl_basic_set *hull;
832 isl_pw_multi_aff *pma_i;
834 pa = isl_pw_aff_list_get_pw_aff(lower, i);
835 pa = isl_pw_aff_ceil(pa);
836 pma_i = isl_pw_multi_aff_copy(pma);
837 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
838 enforced_i = isl_set_copy(upper);
839 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
840 hull = isl_set_simple_hull(enforced_i);
841 enforced = isl_basic_set_intersect(enforced, hull);
844 isl_pw_multi_aff_free(pma);
846 graft = isl_ast_graft_enforce(graft, enforced);
848 return graft;
851 /* Compute the constraints on the outer dimensions enforced by
852 * graft->node and add those constraints to graft->enforced,
853 * in case the upper bound is expressed as
854 * a list of affine expressions "upper".
856 * The enforced condition is that each lower bound expression is less
857 * than or equal to each upper bound expression.
859 static __isl_give isl_ast_graft *set_enforced_from_list(
860 __isl_take isl_ast_graft *graft,
861 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
863 isl_set *cond;
864 isl_basic_set *enforced;
866 lower = isl_pw_aff_list_copy(lower);
867 upper = isl_pw_aff_list_copy(upper);
868 cond = isl_pw_aff_list_le_set(lower, upper);
869 enforced = isl_set_simple_hull(cond);
870 graft = isl_ast_graft_enforce(graft, enforced);
872 return graft;
875 /* Does "aff" have a negative constant term?
877 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
878 __isl_take isl_aff *aff, void *user)
880 int *neg = user;
881 isl_val *v;
883 v = isl_aff_get_constant_val(aff);
884 *neg = isl_val_is_neg(v);
885 isl_val_free(v);
886 isl_set_free(set);
887 isl_aff_free(aff);
889 return *neg ? isl_stat_ok : isl_stat_error;
892 /* Does "pa" have a negative constant term over its entire domain?
894 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
895 void *user)
897 isl_stat r;
898 int *neg = user;
900 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
901 isl_pw_aff_free(pa);
903 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
906 /* Does each element in "list" have a negative constant term?
908 * The callback terminates the iteration as soon an element has been
909 * found that does not have a negative constant term.
911 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
913 int neg = 1;
915 if (isl_pw_aff_list_foreach(list,
916 &pw_aff_constant_is_negative, &neg) < 0 && neg)
917 return -1;
919 return neg;
922 /* Add 1 to each of the elements in "list", where each of these elements
923 * is defined over the internal schedule space of "build".
925 static __isl_give isl_pw_aff_list *list_add_one(
926 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
928 int i, n;
929 isl_space *space;
930 isl_aff *aff;
931 isl_pw_aff *one;
933 space = isl_ast_build_get_space(build, 1);
934 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
935 aff = isl_aff_add_constant_si(aff, 1);
936 one = isl_pw_aff_from_aff(aff);
938 n = isl_pw_aff_list_n_pw_aff(list);
939 for (i = 0; i < n; ++i) {
940 isl_pw_aff *pa;
941 pa = isl_pw_aff_list_get_pw_aff(list, i);
942 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
943 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
946 isl_pw_aff_free(one);
948 return list;
951 /* Set the condition part of the for node graft->node in case
952 * the upper bound is represented as a list of piecewise affine expressions.
954 * In particular, set the condition to
956 * iterator <= min(list of upper bounds)
958 * If each of the upper bounds has a negative constant term, then
959 * set the condition to
961 * iterator < min(list of (upper bound + 1)s)
964 static __isl_give isl_ast_graft *set_for_cond_from_list(
965 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
966 __isl_keep isl_ast_build *build)
968 int neg;
969 isl_ast_expr *bound, *iterator, *cond;
970 enum isl_ast_op_type type = isl_ast_op_le;
972 if (!graft || !list)
973 return isl_ast_graft_free(graft);
975 neg = list_constant_is_negative(list);
976 if (neg < 0)
977 return isl_ast_graft_free(graft);
978 list = isl_pw_aff_list_copy(list);
979 if (neg) {
980 list = list_add_one(list, build);
981 type = isl_ast_op_lt;
984 bound = reduce_list(isl_ast_op_min, list, build);
985 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
986 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
987 graft->node->u.f.cond = cond;
989 isl_pw_aff_list_free(list);
990 if (!graft->node->u.f.cond)
991 return isl_ast_graft_free(graft);
992 return graft;
995 /* Set the condition part of the for node graft->node in case
996 * the upper bound is represented as a set.
998 static __isl_give isl_ast_graft *set_for_cond_from_set(
999 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1000 __isl_keep isl_ast_build *build)
1002 isl_ast_expr *cond;
1004 if (!graft)
1005 return NULL;
1007 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1008 graft->node->u.f.cond = cond;
1009 if (!graft->node->u.f.cond)
1010 return isl_ast_graft_free(graft);
1011 return graft;
1014 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1015 * the current dimension.
1017 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1019 int depth;
1020 isl_val *v;
1021 isl_ctx *ctx;
1023 if (!build)
1024 return NULL;
1025 ctx = isl_ast_build_get_ctx(build);
1026 depth = isl_ast_build_get_depth(build);
1028 if (!isl_ast_build_has_stride(build, depth))
1029 return isl_ast_expr_alloc_int_si(ctx, 1);
1031 v = isl_ast_build_get_stride(build, depth);
1032 return isl_ast_expr_from_val(v);
1035 /* Should we express the loop condition as
1037 * iterator <= min(list of upper bounds)
1039 * or as a conjunction of constraints?
1041 * The first is constructed from a list of upper bounds.
1042 * The second is constructed from a set.
1044 * If there are no upper bounds in "constraints", then this could mean
1045 * that "domain" simply doesn't have an upper bound or that we didn't
1046 * pick any upper bound. In the first case, we want to generate the
1047 * loop condition as a(n empty) conjunction of constraints
1048 * In the second case, we will compute
1049 * a single upper bound from "domain" and so we use the list form.
1051 * If there are upper bounds in "constraints",
1052 * then we use the list form iff the atomic_upper_bound option is set.
1054 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1055 __isl_keep isl_set *domain, int depth)
1057 if (n_upper > 0)
1058 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1059 else
1060 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1063 /* Fill in the expressions of the for node in graft->node.
1065 * In particular,
1066 * - set the initialization part of the loop to the maximum of the lower bounds
1067 * - extract the increment from the stride of the current dimension
1068 * - construct the for condition either based on a list of upper bounds
1069 * or on a set of upper bound constraints.
1071 static __isl_give isl_ast_graft *set_for_node_expressions(
1072 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1073 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1074 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1076 isl_ast_node *node;
1078 if (!graft)
1079 return NULL;
1081 build = isl_ast_build_copy(build);
1083 node = graft->node;
1084 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1085 node->u.f.inc = for_inc(build);
1087 if (use_list)
1088 graft = set_for_cond_from_list(graft, upper_list, build);
1089 else
1090 graft = set_for_cond_from_set(graft, upper_set, build);
1092 isl_ast_build_free(build);
1094 if (!node->u.f.iterator || !node->u.f.init ||
1095 !node->u.f.cond || !node->u.f.inc)
1096 return isl_ast_graft_free(graft);
1098 return graft;
1101 /* Update "graft" based on "bounds" and "domain" for the generic,
1102 * non-degenerate, case.
1104 * "c_lower" and "c_upper" contain the lower and upper bounds
1105 * that the loop node should express.
1106 * "domain" is the subset of the intersection of the constraints
1107 * for which some code is executed.
1109 * There may be zero lower bounds or zero upper bounds in "constraints"
1110 * in case the list of constraints was created
1111 * based on the atomic option or based on separation with explicit bounds.
1112 * In that case, we use "domain" to derive lower and/or upper bounds.
1114 * We first compute a list of one or more lower bounds.
1116 * Then we decide if we want to express the condition as
1118 * iterator <= min(list of upper bounds)
1120 * or as a conjunction of constraints.
1122 * The set of enforced constraints is then computed either based on
1123 * a list of upper bounds or on a set of upper bound constraints.
1124 * We do not compute any enforced constraints if we were forced
1125 * to compute a lower or upper bound using exact_bound. The domains
1126 * of the resulting expressions may imply some bounds on outer dimensions
1127 * that we do not want to appear in the enforced constraints since
1128 * they are not actually enforced by the corresponding code.
1130 * Finally, we fill in the expressions of the for node.
1132 static __isl_give isl_ast_graft *refine_generic_bounds(
1133 __isl_take isl_ast_graft *graft,
1134 __isl_take isl_constraint_list *c_lower,
1135 __isl_take isl_constraint_list *c_upper,
1136 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1138 int depth;
1139 isl_ctx *ctx;
1140 isl_pw_aff_list *lower;
1141 int use_list;
1142 isl_set *upper_set = NULL;
1143 isl_pw_aff_list *upper_list = NULL;
1144 int n_lower, n_upper;
1146 if (!graft || !c_lower || !c_upper || !build)
1147 goto error;
1149 depth = isl_ast_build_get_depth(build);
1150 ctx = isl_ast_graft_get_ctx(graft);
1152 n_lower = isl_constraint_list_n_constraint(c_lower);
1153 n_upper = isl_constraint_list_n_constraint(c_upper);
1155 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1157 lower = lower_bounds(c_lower, depth, domain, build);
1159 if (use_list)
1160 upper_list = upper_bounds(c_upper, depth, domain, build);
1161 else if (n_upper > 0)
1162 upper_set = intersect_constraints(c_upper);
1163 else
1164 upper_set = isl_set_universe(isl_set_get_space(domain));
1166 if (n_lower == 0 || n_upper == 0)
1168 else if (use_list)
1169 graft = set_enforced_from_list(graft, lower, upper_list);
1170 else
1171 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1173 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1174 upper_set, build);
1176 isl_pw_aff_list_free(lower);
1177 isl_pw_aff_list_free(upper_list);
1178 isl_set_free(upper_set);
1179 isl_constraint_list_free(c_lower);
1180 isl_constraint_list_free(c_upper);
1182 return graft;
1183 error:
1184 isl_constraint_list_free(c_lower);
1185 isl_constraint_list_free(c_upper);
1186 return isl_ast_graft_free(graft);
1189 /* Internal data structure used inside count_constraints to keep
1190 * track of the number of constraints that are independent of dimension "pos",
1191 * the lower bounds in "pos" and the upper bounds in "pos".
1193 struct isl_ast_count_constraints_data {
1194 int pos;
1196 int n_indep;
1197 int n_lower;
1198 int n_upper;
1201 /* Increment data->n_indep, data->lower or data->upper depending
1202 * on whether "c" is independenct of dimensions data->pos,
1203 * a lower bound or an upper bound.
1205 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1207 struct isl_ast_count_constraints_data *data = user;
1209 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1210 data->n_lower++;
1211 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1212 data->n_upper++;
1213 else
1214 data->n_indep++;
1216 isl_constraint_free(c);
1218 return isl_stat_ok;
1221 /* Update "graft" based on "bounds" and "domain" for the generic,
1222 * non-degenerate, case.
1224 * "list" respresent the list of bounds that need to be encoded by
1225 * the for loop. Only the constraints that involve the iterator
1226 * are relevant here. The other constraints are taken care of by
1227 * the caller and are included in the generated constraints of "build".
1228 * "domain" is the subset of the intersection of the constraints
1229 * for which some code is executed.
1230 * "build" is the build in which graft->node was created.
1232 * We separate lower bounds, upper bounds and constraints that
1233 * are independent of the loop iterator.
1235 * The actual for loop bounds are generated in refine_generic_bounds.
1237 static __isl_give isl_ast_graft *refine_generic_split(
1238 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1239 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1241 struct isl_ast_count_constraints_data data;
1242 isl_constraint_list *lower;
1243 isl_constraint_list *upper;
1245 if (!list)
1246 return isl_ast_graft_free(graft);
1248 data.pos = isl_ast_build_get_depth(build);
1250 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1251 if (!list)
1252 return isl_ast_graft_free(graft);
1254 data.n_indep = data.n_lower = data.n_upper = 0;
1255 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1256 isl_constraint_list_free(list);
1257 return isl_ast_graft_free(graft);
1260 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1261 upper = isl_constraint_list_copy(lower);
1262 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1263 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1265 return refine_generic_bounds(graft, lower, upper, domain, build);
1268 /* Update "graft" based on "bounds" and "domain" for the generic,
1269 * non-degenerate, case.
1271 * "bounds" respresent the bounds that need to be encoded by
1272 * the for loop (or a guard around the for loop).
1273 * "domain" is the subset of "bounds" for which some code is executed.
1274 * "build" is the build in which graft->node was created.
1276 * We break up "bounds" into a list of constraints and continue with
1277 * refine_generic_split.
1279 static __isl_give isl_ast_graft *refine_generic(
1280 __isl_take isl_ast_graft *graft,
1281 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1282 __isl_keep isl_ast_build *build)
1284 isl_constraint_list *list;
1286 if (!build || !graft)
1287 return isl_ast_graft_free(graft);
1289 list = isl_basic_set_get_constraint_list(bounds);
1291 graft = refine_generic_split(graft, list, domain, build);
1293 return graft;
1296 /* Create a for node for the current level.
1298 * Mark the for node degenerate if "degenerate" is set.
1300 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1301 int degenerate)
1303 int depth;
1304 isl_id *id;
1305 isl_ast_node *node;
1307 if (!build)
1308 return NULL;
1310 depth = isl_ast_build_get_depth(build);
1311 id = isl_ast_build_get_iterator_id(build, depth);
1312 node = isl_ast_node_alloc_for(id);
1313 if (degenerate)
1314 node = isl_ast_node_for_mark_degenerate(node);
1316 return node;
1319 /* If the ast_build_exploit_nested_bounds option is set, then return
1320 * the constraints enforced by all elements in "list".
1321 * Otherwise, return the universe.
1323 static __isl_give isl_basic_set *extract_shared_enforced(
1324 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1326 isl_ctx *ctx;
1327 isl_space *space;
1329 if (!list)
1330 return NULL;
1332 ctx = isl_ast_graft_list_get_ctx(list);
1333 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1334 return isl_ast_graft_list_extract_shared_enforced(list, build);
1336 space = isl_ast_build_get_space(build, 1);
1337 return isl_basic_set_universe(space);
1340 /* Return the pending constraints of "build" that are not already taken
1341 * care of (by a combination of "enforced" and the generated constraints
1342 * of "build").
1344 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1345 __isl_keep isl_basic_set *enforced)
1347 isl_set *guard, *context;
1349 guard = isl_ast_build_get_pending(build);
1350 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1351 context = isl_set_intersect(context,
1352 isl_ast_build_get_generated(build));
1353 return isl_set_gist(guard, context);
1356 /* Create an AST node for the current dimension based on
1357 * the schedule domain "bounds" and return the node encapsulated
1358 * in an isl_ast_graft.
1360 * "executed" is the current inverse schedule, taking into account
1361 * the bounds in "bounds"
1362 * "domain" is the domain of "executed", with inner dimensions projected out.
1363 * It may be a strict subset of "bounds" in case "bounds" was created
1364 * based on the atomic option or based on separation with explicit bounds.
1366 * "domain" may satisfy additional equalities that result
1367 * from intersecting "executed" with "bounds" in add_node.
1368 * It may also satisfy some global constraints that were dropped out because
1369 * we performed separation with explicit bounds.
1370 * The very first step is then to copy these constraints to "bounds".
1372 * Since we may be calling before_each_for and after_each_for
1373 * callbacks, we record the current inverse schedule in the build.
1375 * We consider three builds,
1376 * "build" is the one in which the current level is created,
1377 * "body_build" is the build in which the next level is created,
1378 * "sub_build" is essentially the same as "body_build", except that
1379 * the depth has not been increased yet.
1381 * "build" already contains information (in strides and offsets)
1382 * about the strides at the current level, but this information is not
1383 * reflected in the build->domain.
1384 * We first add this information and the "bounds" to the sub_build->domain.
1385 * isl_ast_build_set_loop_bounds adds the stride information and
1386 * checks whether the current dimension attains
1387 * only a single value and whether this single value can be represented using
1388 * a single affine expression.
1389 * In the first case, the current level is considered "degenerate".
1390 * In the second, sub-case, the current level is considered "eliminated".
1391 * Eliminated levels don't need to be reflected in the AST since we can
1392 * simply plug in the affine expression. For degenerate, but non-eliminated,
1393 * levels, we do introduce a for node, but mark is as degenerate so that
1394 * it can be printed as an assignment of the single value to the loop
1395 * "iterator".
1397 * If the current level is eliminated, we explicitly plug in the value
1398 * for the current level found by isl_ast_build_set_loop_bounds in the
1399 * inverse schedule. This ensures that if we are working on a slice
1400 * of the domain based on information available in the inverse schedule
1401 * and the build domain, that then this information is also reflected
1402 * in the inverse schedule. This operation also eliminates the current
1403 * dimension from the inverse schedule making sure no inner dimensions depend
1404 * on the current dimension. Otherwise, we create a for node, marking
1405 * it degenerate if appropriate. The initial for node is still incomplete
1406 * and will be completed in either refine_degenerate or refine_generic.
1408 * We then generate a sequence of grafts for the next level,
1409 * create a surrounding graft for the current level and insert
1410 * the for node we created (if the current level is not eliminated).
1411 * Before creating a graft for the current level, we first extract
1412 * hoistable constraints from the child guards and combine them
1413 * with the pending constraints in the build. These constraints
1414 * are used to simplify the child guards and then added to the guard
1415 * of the current graft to ensure that they will be generated.
1416 * If the hoisted guard is a disjunction, then we use it directly
1417 * to gist the guards on the children before intersect it with the
1418 * pending constraints. We do so because this disjunction is typically
1419 * identical to the guards on the children such that these guards
1420 * can be effectively removed completely. After the intersection,
1421 * the gist operation would have a harder time figuring this out.
1423 * Finally, we set the bounds of the for loop in either
1424 * refine_degenerate or refine_generic.
1425 * We do so in a context where the pending constraints of the build
1426 * have been replaced by the guard of the current graft.
1428 static __isl_give isl_ast_graft *create_node_scaled(
1429 __isl_take isl_union_map *executed,
1430 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1431 __isl_take isl_ast_build *build)
1433 int depth;
1434 int degenerate, eliminated;
1435 isl_basic_set *hull;
1436 isl_basic_set *enforced;
1437 isl_set *guard, *hoisted;
1438 isl_ast_node *node = NULL;
1439 isl_ast_graft *graft;
1440 isl_ast_graft_list *children;
1441 isl_ast_build *sub_build;
1442 isl_ast_build *body_build;
1444 domain = isl_ast_build_eliminate_divs(build, domain);
1445 domain = isl_set_detect_equalities(domain);
1446 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1447 bounds = isl_basic_set_intersect(bounds, hull);
1448 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1450 depth = isl_ast_build_get_depth(build);
1451 sub_build = isl_ast_build_copy(build);
1452 bounds = isl_basic_set_remove_redundancies(bounds);
1453 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1454 isl_basic_set_copy(bounds));
1455 degenerate = isl_ast_build_has_value(sub_build);
1456 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1457 if (degenerate < 0 || eliminated < 0)
1458 executed = isl_union_map_free(executed);
1459 if (eliminated)
1460 executed = plug_in_values(executed, sub_build);
1461 else
1462 node = create_for(build, degenerate);
1464 body_build = isl_ast_build_copy(sub_build);
1465 body_build = isl_ast_build_increase_depth(body_build);
1466 if (!eliminated)
1467 node = before_each_for(node, body_build);
1468 children = generate_next_level(executed,
1469 isl_ast_build_copy(body_build));
1471 enforced = extract_shared_enforced(children, build);
1472 guard = extract_pending(sub_build, enforced);
1473 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1474 if (isl_set_n_basic_set(hoisted) > 1)
1475 children = isl_ast_graft_list_gist_guards(children,
1476 isl_set_copy(hoisted));
1477 guard = isl_set_intersect(guard, hoisted);
1478 if (!eliminated)
1479 guard = add_implied_guards(guard, degenerate, bounds, build);
1481 graft = isl_ast_graft_alloc_from_children(children,
1482 isl_set_copy(guard), enforced, build, sub_build);
1484 if (!degenerate)
1485 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1486 if (!eliminated) {
1487 isl_ast_build *for_build;
1489 graft = isl_ast_graft_insert_for(graft, node);
1490 for_build = isl_ast_build_copy(build);
1491 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1492 isl_set_copy(guard));
1493 if (degenerate)
1494 graft = refine_degenerate(graft, for_build, sub_build);
1495 else
1496 graft = refine_generic(graft, bounds,
1497 domain, for_build);
1498 isl_ast_build_free(for_build);
1500 isl_set_free(guard);
1501 if (!eliminated)
1502 graft = after_each_for(graft, body_build);
1504 isl_ast_build_free(body_build);
1505 isl_ast_build_free(sub_build);
1506 isl_ast_build_free(build);
1507 isl_basic_set_free(bounds);
1508 isl_set_free(domain);
1510 return graft;
1513 /* Internal data structure for checking if all constraints involving
1514 * the input dimension "depth" are such that the other coefficients
1515 * are multiples of "m", reducing "m" if they are not.
1516 * If "m" is reduced all the way down to "1", then the check has failed
1517 * and we break out of the iteration.
1519 struct isl_check_scaled_data {
1520 int depth;
1521 isl_val *m;
1524 /* If constraint "c" involves the input dimension data->depth,
1525 * then make sure that all the other coefficients are multiples of data->m,
1526 * reducing data->m if needed.
1527 * Break out of the iteration if data->m has become equal to "1".
1529 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1530 void *user)
1532 struct isl_check_scaled_data *data = user;
1533 int i, j, n;
1534 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1535 isl_dim_div };
1537 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1538 isl_constraint_free(c);
1539 return isl_stat_ok;
1542 for (i = 0; i < 4; ++i) {
1543 n = isl_constraint_dim(c, t[i]);
1544 for (j = 0; j < n; ++j) {
1545 isl_val *d;
1547 if (t[i] == isl_dim_in && j == data->depth)
1548 continue;
1549 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1550 continue;
1551 d = isl_constraint_get_coefficient_val(c, t[i], j);
1552 data->m = isl_val_gcd(data->m, d);
1553 if (isl_val_is_one(data->m))
1554 break;
1556 if (j < n)
1557 break;
1560 isl_constraint_free(c);
1562 return i < 4 ? isl_stat_error : isl_stat_ok;
1565 /* For each constraint of "bmap" that involves the input dimension data->depth,
1566 * make sure that all the other coefficients are multiples of data->m,
1567 * reducing data->m if needed.
1568 * Break out of the iteration if data->m has become equal to "1".
1570 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1571 void *user)
1573 isl_stat r;
1575 r = isl_basic_map_foreach_constraint(bmap,
1576 &constraint_check_scaled, user);
1577 isl_basic_map_free(bmap);
1579 return r;
1582 /* For each constraint of "map" that involves the input dimension data->depth,
1583 * make sure that all the other coefficients are multiples of data->m,
1584 * reducing data->m if needed.
1585 * Break out of the iteration if data->m has become equal to "1".
1587 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1589 isl_stat r;
1591 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1592 isl_map_free(map);
1594 return r;
1597 /* Create an AST node for the current dimension based on
1598 * the schedule domain "bounds" and return the node encapsulated
1599 * in an isl_ast_graft.
1601 * "executed" is the current inverse schedule, taking into account
1602 * the bounds in "bounds"
1603 * "domain" is the domain of "executed", with inner dimensions projected out.
1606 * Before moving on to the actual AST node construction in create_node_scaled,
1607 * we first check if the current dimension is strided and if we can scale
1608 * down this stride. Note that we only do this if the ast_build_scale_strides
1609 * option is set.
1611 * In particular, let the current dimension take on values
1613 * f + s a
1615 * with a an integer. We check if we can find an integer m that (obviously)
1616 * divides both f and s.
1618 * If so, we check if the current dimension only appears in constraints
1619 * where the coefficients of the other variables are multiples of m.
1620 * We perform this extra check to avoid the risk of introducing
1621 * divisions by scaling down the current dimension.
1623 * If so, we scale the current dimension down by a factor of m.
1624 * That is, we plug in
1626 * i = m i' (1)
1628 * Note that in principle we could always scale down strided loops
1629 * by plugging in
1631 * i = f + s i'
1633 * but this may result in i' taking on larger values than the original i,
1634 * due to the shift by "f".
1635 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1637 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1638 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1639 __isl_take isl_ast_build *build)
1641 struct isl_check_scaled_data data;
1642 isl_ctx *ctx;
1643 isl_aff *offset;
1644 isl_val *d;
1646 ctx = isl_ast_build_get_ctx(build);
1647 if (!isl_options_get_ast_build_scale_strides(ctx))
1648 return create_node_scaled(executed, bounds, domain, build);
1650 data.depth = isl_ast_build_get_depth(build);
1651 if (!isl_ast_build_has_stride(build, data.depth))
1652 return create_node_scaled(executed, bounds, domain, build);
1654 offset = isl_ast_build_get_offset(build, data.depth);
1655 data.m = isl_ast_build_get_stride(build, data.depth);
1656 if (!data.m)
1657 offset = isl_aff_free(offset);
1658 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1659 d = isl_aff_get_denominator_val(offset);
1660 if (!d)
1661 executed = isl_union_map_free(executed);
1663 if (executed && isl_val_is_divisible_by(data.m, d))
1664 data.m = isl_val_div(data.m, d);
1665 else {
1666 data.m = isl_val_set_si(data.m, 1);
1667 isl_val_free(d);
1670 if (!isl_val_is_one(data.m)) {
1671 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1672 &data) < 0 &&
1673 !isl_val_is_one(data.m))
1674 executed = isl_union_map_free(executed);
1677 if (!isl_val_is_one(data.m)) {
1678 isl_space *space;
1679 isl_multi_aff *ma;
1680 isl_aff *aff;
1681 isl_map *map;
1682 isl_union_map *umap;
1684 space = isl_ast_build_get_space(build, 1);
1685 space = isl_space_map_from_set(space);
1686 ma = isl_multi_aff_identity(space);
1687 aff = isl_multi_aff_get_aff(ma, data.depth);
1688 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1689 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1691 bounds = isl_basic_set_preimage_multi_aff(bounds,
1692 isl_multi_aff_copy(ma));
1693 domain = isl_set_preimage_multi_aff(domain,
1694 isl_multi_aff_copy(ma));
1695 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1696 umap = isl_union_map_from_map(map);
1697 executed = isl_union_map_apply_domain(executed,
1698 isl_union_map_copy(umap));
1699 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1700 umap);
1702 isl_aff_free(offset);
1703 isl_val_free(data.m);
1705 return create_node_scaled(executed, bounds, domain, build);
1708 /* Add the basic set to the list that "user" points to.
1710 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1712 isl_basic_set_list **list = user;
1714 *list = isl_basic_set_list_add(*list, bset);
1716 return isl_stat_ok;
1719 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1721 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1722 __isl_take isl_set *set)
1724 int n;
1725 isl_ctx *ctx;
1726 isl_basic_set_list *list;
1728 if (!set)
1729 return NULL;
1731 ctx = isl_set_get_ctx(set);
1733 n = isl_set_n_basic_set(set);
1734 list = isl_basic_set_list_alloc(ctx, n);
1735 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1736 list = isl_basic_set_list_free(list);
1738 isl_set_free(set);
1739 return list;
1742 /* Generate code for the schedule domain "bounds"
1743 * and add the result to "list".
1745 * We mainly detect strides here and check if the bounds do not
1746 * conflict with the current build domain
1747 * and then pass over control to create_node.
1749 * "bounds" reflects the bounds on the current dimension and possibly
1750 * some extra conditions on outer dimensions.
1751 * It does not, however, include any divs involving the current dimension,
1752 * so it does not capture any stride constraints.
1753 * We therefore need to compute that part of the schedule domain that
1754 * intersects with "bounds" and derive the strides from the result.
1756 static __isl_give isl_ast_graft_list *add_node(
1757 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1758 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1760 isl_ast_graft *graft;
1761 isl_set *domain = NULL;
1762 isl_union_set *uset;
1763 int empty, disjoint;
1765 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1766 executed = isl_union_map_intersect_domain(executed, uset);
1767 empty = isl_union_map_is_empty(executed);
1768 if (empty < 0)
1769 goto error;
1770 if (empty)
1771 goto done;
1773 uset = isl_union_map_domain(isl_union_map_copy(executed));
1774 domain = isl_set_from_union_set(uset);
1775 domain = isl_ast_build_specialize(build, domain);
1777 domain = isl_set_compute_divs(domain);
1778 domain = isl_ast_build_eliminate_inner(build, domain);
1779 disjoint = isl_set_is_disjoint(domain, build->domain);
1780 if (disjoint < 0)
1781 goto error;
1782 if (disjoint)
1783 goto done;
1785 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1787 graft = create_node(executed, bounds, domain,
1788 isl_ast_build_copy(build));
1789 list = isl_ast_graft_list_add(list, graft);
1790 isl_ast_build_free(build);
1791 return list;
1792 error:
1793 list = isl_ast_graft_list_free(list);
1794 done:
1795 isl_set_free(domain);
1796 isl_basic_set_free(bounds);
1797 isl_union_map_free(executed);
1798 isl_ast_build_free(build);
1799 return list;
1802 /* Does any element of i follow or coincide with any element of j
1803 * at the current depth for equal values of the outer dimensions?
1805 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1806 __isl_keep isl_basic_set *j, void *user)
1808 int depth = *(int *) user;
1809 isl_basic_map *test;
1810 isl_bool empty;
1811 int l;
1813 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1814 isl_basic_set_copy(j));
1815 for (l = 0; l < depth; ++l)
1816 test = isl_basic_map_equate(test, isl_dim_in, l,
1817 isl_dim_out, l);
1818 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1819 isl_dim_out, depth);
1820 empty = isl_basic_map_is_empty(test);
1821 isl_basic_map_free(test);
1823 return empty < 0 ? isl_bool_error : !empty;
1826 /* Split up each element of "list" into a part that is related to "bset"
1827 * according to "gt" and a part that is not.
1828 * Return a list that consist of "bset" and all the pieces.
1830 static __isl_give isl_basic_set_list *add_split_on(
1831 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1832 __isl_keep isl_basic_map *gt)
1834 int i, n;
1835 isl_basic_set_list *res;
1837 if (!list)
1838 bset = isl_basic_set_free(bset);
1840 gt = isl_basic_map_copy(gt);
1841 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1842 n = isl_basic_set_list_n_basic_set(list);
1843 res = isl_basic_set_list_from_basic_set(bset);
1844 for (i = 0; res && i < n; ++i) {
1845 isl_basic_set *bset;
1846 isl_set *set1, *set2;
1847 isl_basic_map *bmap;
1848 int empty;
1850 bset = isl_basic_set_list_get_basic_set(list, i);
1851 bmap = isl_basic_map_copy(gt);
1852 bmap = isl_basic_map_intersect_range(bmap, bset);
1853 bset = isl_basic_map_range(bmap);
1854 empty = isl_basic_set_is_empty(bset);
1855 if (empty < 0)
1856 res = isl_basic_set_list_free(res);
1857 if (empty) {
1858 isl_basic_set_free(bset);
1859 bset = isl_basic_set_list_get_basic_set(list, i);
1860 res = isl_basic_set_list_add(res, bset);
1861 continue;
1864 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1865 set1 = isl_set_from_basic_set(bset);
1866 bset = isl_basic_set_list_get_basic_set(list, i);
1867 set2 = isl_set_from_basic_set(bset);
1868 set1 = isl_set_subtract(set2, set1);
1869 set1 = isl_set_make_disjoint(set1);
1871 res = isl_basic_set_list_concat(res,
1872 isl_basic_set_list_from_set(set1));
1874 isl_basic_map_free(gt);
1875 isl_basic_set_list_free(list);
1876 return res;
1879 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1880 __isl_keep isl_basic_set_list *domain_list,
1881 __isl_keep isl_union_map *executed,
1882 __isl_keep isl_ast_build *build);
1884 /* Internal data structure for add_nodes.
1886 * "executed" and "build" are extra arguments to be passed to add_node.
1887 * "list" collects the results.
1889 struct isl_add_nodes_data {
1890 isl_union_map *executed;
1891 isl_ast_build *build;
1893 isl_ast_graft_list *list;
1896 /* Generate code for the schedule domains in "scc"
1897 * and add the results to "list".
1899 * The domains in "scc" form a strongly connected component in the ordering.
1900 * If the number of domains in "scc" is larger than 1, then this means
1901 * that we cannot determine a valid ordering for the domains in the component.
1902 * This should be fairly rare because the individual domains
1903 * have been made disjoint first.
1904 * The problem is that the domains may be integrally disjoint but not
1905 * rationally disjoint. For example, we may have domains
1907 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1909 * These two domains have an empty intersection, but their rational
1910 * relaxations do intersect. It is impossible to order these domains
1911 * in the second dimension because the first should be ordered before
1912 * the second for outer dimension equal to 0, while it should be ordered
1913 * after for outer dimension equal to 1.
1915 * This may happen in particular in case of unrolling since the domain
1916 * of each slice is replaced by its simple hull.
1918 * For each basic set i in "scc" and for each of the following basic sets j,
1919 * we split off that part of the basic set i that shares the outer dimensions
1920 * with j and lies before j in the current dimension.
1921 * We collect all the pieces in a new list that replaces "scc".
1923 * While the elements in "scc" should be disjoint, we double-check
1924 * this property to avoid running into an infinite recursion in case
1925 * they intersect due to some internal error.
1927 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1929 struct isl_add_nodes_data *data = user;
1930 int i, n, depth;
1931 isl_basic_set *bset, *first;
1932 isl_basic_set_list *list;
1933 isl_space *space;
1934 isl_basic_map *gt;
1936 n = isl_basic_set_list_n_basic_set(scc);
1937 bset = isl_basic_set_list_get_basic_set(scc, 0);
1938 if (n == 1) {
1939 isl_basic_set_list_free(scc);
1940 data->list = add_node(data->list,
1941 isl_union_map_copy(data->executed), bset,
1942 isl_ast_build_copy(data->build));
1943 return data->list ? isl_stat_ok : isl_stat_error;
1946 depth = isl_ast_build_get_depth(data->build);
1947 space = isl_basic_set_get_space(bset);
1948 space = isl_space_map_from_set(space);
1949 gt = isl_basic_map_universe(space);
1950 for (i = 0; i < depth; ++i)
1951 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1952 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1954 first = isl_basic_set_copy(bset);
1955 list = isl_basic_set_list_from_basic_set(bset);
1956 for (i = 1; i < n; ++i) {
1957 int disjoint;
1959 bset = isl_basic_set_list_get_basic_set(scc, i);
1961 disjoint = isl_basic_set_is_disjoint(bset, first);
1962 if (disjoint < 0)
1963 list = isl_basic_set_list_free(list);
1964 else if (!disjoint)
1965 isl_die(isl_basic_set_list_get_ctx(scc),
1966 isl_error_internal,
1967 "basic sets in scc are assumed to be disjoint",
1968 list = isl_basic_set_list_free(list));
1970 list = add_split_on(list, bset, gt);
1972 isl_basic_set_free(first);
1973 isl_basic_map_free(gt);
1974 isl_basic_set_list_free(scc);
1975 scc = list;
1976 data->list = isl_ast_graft_list_concat(data->list,
1977 generate_sorted_domains(scc, data->executed, data->build));
1978 isl_basic_set_list_free(scc);
1980 return data->list ? isl_stat_ok : isl_stat_error;
1983 /* Sort the domains in "domain_list" according to the execution order
1984 * at the current depth (for equal values of the outer dimensions),
1985 * generate code for each of them, collecting the results in a list.
1986 * If no code is generated (because the intersection of the inverse schedule
1987 * with the domains turns out to be empty), then an empty list is returned.
1989 * The caller is responsible for ensuring that the basic sets in "domain_list"
1990 * are pair-wise disjoint. It can, however, in principle happen that
1991 * two basic sets should be ordered one way for one value of the outer
1992 * dimensions and the other way for some other value of the outer dimensions.
1993 * We therefore play safe and look for strongly connected components.
1994 * The function add_nodes takes care of handling non-trivial components.
1996 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1997 __isl_keep isl_basic_set_list *domain_list,
1998 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2000 isl_ctx *ctx;
2001 struct isl_add_nodes_data data;
2002 int depth;
2003 int n;
2005 if (!domain_list)
2006 return NULL;
2008 ctx = isl_basic_set_list_get_ctx(domain_list);
2009 n = isl_basic_set_list_n_basic_set(domain_list);
2010 data.list = isl_ast_graft_list_alloc(ctx, n);
2011 if (n == 0)
2012 return data.list;
2013 if (n == 1)
2014 return add_node(data.list, isl_union_map_copy(executed),
2015 isl_basic_set_list_get_basic_set(domain_list, 0),
2016 isl_ast_build_copy(build));
2018 depth = isl_ast_build_get_depth(build);
2019 data.executed = executed;
2020 data.build = build;
2021 if (isl_basic_set_list_foreach_scc(domain_list,
2022 &domain_follows_at_depth, &depth,
2023 &add_nodes, &data) < 0)
2024 data.list = isl_ast_graft_list_free(data.list);
2026 return data.list;
2029 /* Do i and j share any values for the outer dimensions?
2031 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2032 __isl_keep isl_basic_set *j, void *user)
2034 int depth = *(int *) user;
2035 isl_basic_map *test;
2036 isl_bool empty;
2037 int l;
2039 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2040 isl_basic_set_copy(j));
2041 for (l = 0; l < depth; ++l)
2042 test = isl_basic_map_equate(test, isl_dim_in, l,
2043 isl_dim_out, l);
2044 empty = isl_basic_map_is_empty(test);
2045 isl_basic_map_free(test);
2047 return empty < 0 ? isl_bool_error : !empty;
2050 /* Internal data structure for generate_sorted_domains_wrap.
2052 * "n" is the total number of basic sets
2053 * "executed" and "build" are extra arguments to be passed
2054 * to generate_sorted_domains.
2056 * "single" is set to 1 by generate_sorted_domains_wrap if there
2057 * is only a single component.
2058 * "list" collects the results.
2060 struct isl_ast_generate_parallel_domains_data {
2061 int n;
2062 isl_union_map *executed;
2063 isl_ast_build *build;
2065 int single;
2066 isl_ast_graft_list *list;
2069 /* Call generate_sorted_domains on "scc", fuse the result into a list
2070 * with either zero or one graft and collect the these single element
2071 * lists into data->list.
2073 * If there is only one component, i.e., if the number of basic sets
2074 * in the current component is equal to the total number of basic sets,
2075 * then data->single is set to 1 and the result of generate_sorted_domains
2076 * is not fused.
2078 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2079 void *user)
2081 struct isl_ast_generate_parallel_domains_data *data = user;
2082 isl_ast_graft_list *list;
2084 list = generate_sorted_domains(scc, data->executed, data->build);
2085 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2086 if (!data->single)
2087 list = isl_ast_graft_list_fuse(list, data->build);
2088 if (!data->list)
2089 data->list = list;
2090 else
2091 data->list = isl_ast_graft_list_concat(data->list, list);
2093 isl_basic_set_list_free(scc);
2094 if (!data->list)
2095 return isl_stat_error;
2097 return isl_stat_ok;
2100 /* Look for any (weakly connected) components in the "domain_list"
2101 * of domains that share some values of the outer dimensions.
2102 * That is, domains in different components do not share any values
2103 * of the outer dimensions. This means that these components
2104 * can be freely reordered.
2105 * Within each of the components, we sort the domains according
2106 * to the execution order at the current depth.
2108 * If there is more than one component, then generate_sorted_domains_wrap
2109 * fuses the result of each call to generate_sorted_domains
2110 * into a list with either zero or one graft and collects these (at most)
2111 * single element lists into a bigger list. This means that the elements of the
2112 * final list can be freely reordered. In particular, we sort them
2113 * according to an arbitrary but fixed ordering to ease merging of
2114 * graft lists from different components.
2116 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2117 __isl_keep isl_basic_set_list *domain_list,
2118 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2120 int depth;
2121 struct isl_ast_generate_parallel_domains_data data;
2123 if (!domain_list)
2124 return NULL;
2126 data.n = isl_basic_set_list_n_basic_set(domain_list);
2127 if (data.n <= 1)
2128 return generate_sorted_domains(domain_list, executed, build);
2130 depth = isl_ast_build_get_depth(build);
2131 data.list = NULL;
2132 data.executed = executed;
2133 data.build = build;
2134 data.single = 0;
2135 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2136 &generate_sorted_domains_wrap,
2137 &data) < 0)
2138 data.list = isl_ast_graft_list_free(data.list);
2140 if (!data.single)
2141 data.list = isl_ast_graft_list_sort_guard(data.list);
2143 return data.list;
2146 /* Internal data for separate_domain.
2148 * "explicit" is set if we only want to use explicit bounds.
2150 * "domain" collects the separated domains.
2152 struct isl_separate_domain_data {
2153 isl_ast_build *build;
2154 int explicit;
2155 isl_set *domain;
2158 /* Extract implicit bounds on the current dimension for the executed "map".
2160 * The domain of "map" may involve inner dimensions, so we
2161 * need to eliminate them.
2163 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2164 __isl_keep isl_ast_build *build)
2166 isl_set *domain;
2168 domain = isl_map_domain(map);
2169 domain = isl_ast_build_eliminate(build, domain);
2171 return domain;
2174 /* Extract explicit bounds on the current dimension for the executed "map".
2176 * Rather than eliminating the inner dimensions as in implicit_bounds,
2177 * we simply drop any constraints involving those inner dimensions.
2178 * The idea is that most bounds that are implied by constraints on the
2179 * inner dimensions will be enforced by for loops and not by explicit guards.
2180 * There is then no need to separate along those bounds.
2182 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2183 __isl_keep isl_ast_build *build)
2185 isl_set *domain;
2186 int depth, dim;
2188 dim = isl_map_dim(map, isl_dim_out);
2189 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2191 domain = isl_map_domain(map);
2192 depth = isl_ast_build_get_depth(build);
2193 dim = isl_set_dim(domain, isl_dim_set);
2194 domain = isl_set_detect_equalities(domain);
2195 domain = isl_set_drop_constraints_involving_dims(domain,
2196 isl_dim_set, depth + 1, dim - (depth + 1));
2197 domain = isl_set_remove_divs_involving_dims(domain,
2198 isl_dim_set, depth, 1);
2199 domain = isl_set_remove_unknown_divs(domain);
2201 return domain;
2204 /* Split data->domain into pieces that intersect with the range of "map"
2205 * and pieces that do not intersect with the range of "map"
2206 * and then add that part of the range of "map" that does not intersect
2207 * with data->domain.
2209 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2211 struct isl_separate_domain_data *data = user;
2212 isl_set *domain;
2213 isl_set *d1, *d2;
2215 if (data->explicit)
2216 domain = explicit_bounds(map, data->build);
2217 else
2218 domain = implicit_bounds(map, data->build);
2220 domain = isl_set_coalesce(domain);
2221 domain = isl_set_make_disjoint(domain);
2222 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2223 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2224 data->domain = isl_set_intersect(data->domain, domain);
2225 data->domain = isl_set_union(data->domain, d1);
2226 data->domain = isl_set_union(data->domain, d2);
2228 return isl_stat_ok;
2231 /* Separate the schedule domains of "executed".
2233 * That is, break up the domain of "executed" into basic sets,
2234 * such that for each basic set S, every element in S is associated with
2235 * the same domain spaces.
2237 * "space" is the (single) domain space of "executed".
2239 static __isl_give isl_set *separate_schedule_domains(
2240 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2241 __isl_keep isl_ast_build *build)
2243 struct isl_separate_domain_data data = { build };
2244 isl_ctx *ctx;
2246 ctx = isl_ast_build_get_ctx(build);
2247 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2248 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2249 data.domain = isl_set_empty(space);
2250 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2251 data.domain = isl_set_free(data.domain);
2253 isl_union_map_free(executed);
2254 return data.domain;
2257 /* Temporary data used during the search for a lower bound for unrolling.
2259 * "build" is the build in which the unrolling will be performed
2260 * "domain" is the original set for which to find a lower bound
2261 * "depth" is the dimension for which to find a lower boudn
2262 * "expansion" is the expansion that needs to be applied to "domain"
2263 * in the unrolling that will be performed
2265 * "lower" is the best lower bound found so far. It is NULL if we have not
2266 * found any yet.
2267 * "n" is the corresponding size. If lower is NULL, then the value of n
2268 * is undefined.
2269 * "n_div" is the maximal number of integer divisions in the first
2270 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2271 * been computed yet.
2273 struct isl_find_unroll_data {
2274 isl_ast_build *build;
2275 isl_set *domain;
2276 int depth;
2277 isl_basic_map *expansion;
2279 isl_aff *lower;
2280 int *n;
2281 int n_div;
2284 /* Return the constraint
2286 * i_"depth" = aff + offset
2288 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2289 int offset)
2291 aff = isl_aff_copy(aff);
2292 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2293 aff = isl_aff_add_constant_si(aff, offset);
2294 return isl_equality_from_aff(aff);
2297 /* Update *user to the number of integer divsions in the first element
2298 * of "ma", if it is larger than the current value.
2300 static isl_stat update_n_div(__isl_take isl_set *set,
2301 __isl_take isl_multi_aff *ma, void *user)
2303 isl_aff *aff;
2304 int *n = user;
2305 int n_div;
2307 aff = isl_multi_aff_get_aff(ma, 0);
2308 n_div = isl_aff_dim(aff, isl_dim_div);
2309 isl_aff_free(aff);
2310 isl_multi_aff_free(ma);
2311 isl_set_free(set);
2313 if (n_div > *n)
2314 *n = n_div;
2316 return aff ? isl_stat_ok : isl_stat_error;
2319 /* Get the number of integer divisions in the expression for the iterator
2320 * value at the first slice in the unrolling based on lower bound "lower",
2321 * taking into account the expansion that needs to be performed on this slice.
2323 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2324 __isl_keep isl_aff *lower)
2326 isl_constraint *c;
2327 isl_set *set;
2328 isl_map *it_map, *expansion;
2329 isl_pw_multi_aff *pma;
2330 int n;
2332 c = at_offset(data->depth, lower, 0);
2333 set = isl_set_copy(data->domain);
2334 set = isl_set_add_constraint(set, c);
2335 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2336 set = isl_set_apply(set, expansion);
2337 it_map = isl_ast_build_map_to_iterator(data->build, set);
2338 pma = isl_pw_multi_aff_from_map(it_map);
2339 n = 0;
2340 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2341 n = -1;
2342 isl_pw_multi_aff_free(pma);
2344 return n;
2347 /* Is the lower bound "lower" with corresponding iteration count "n"
2348 * better than the one stored in "data"?
2349 * If there is no upper bound on the iteration count ("n" is infinity) or
2350 * if the count is too large, then we cannot use this lower bound.
2351 * Otherwise, if there was no previous lower bound or
2352 * if the iteration count of the new lower bound is smaller than
2353 * the iteration count of the previous lower bound, then we consider
2354 * the new lower bound to be better.
2355 * If the iteration count is the same, then compare the number
2356 * of integer divisions that would be needed to express
2357 * the iterator value at the first slice in the unrolling
2358 * according to the lower bound. If we end up computing this
2359 * number, then store the lowest value in data->n_div.
2361 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2362 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2364 int cmp;
2365 int n_div;
2367 if (!n)
2368 return -1;
2369 if (isl_val_is_infty(n))
2370 return 0;
2371 if (isl_val_cmp_si(n, INT_MAX) > 0)
2372 return 0;
2373 if (!data->lower)
2374 return 1;
2375 cmp = isl_val_cmp_si(n, *data->n);
2376 if (cmp < 0)
2377 return 1;
2378 if (cmp > 0)
2379 return 0;
2380 if (data->n_div < 0)
2381 data->n_div = get_expanded_n_div(data, data->lower);
2382 if (data->n_div < 0)
2383 return -1;
2384 if (data->n_div == 0)
2385 return 0;
2386 n_div = get_expanded_n_div(data, lower);
2387 if (n_div < 0)
2388 return -1;
2389 if (n_div >= data->n_div)
2390 return 0;
2391 data->n_div = n_div;
2393 return 1;
2396 /* Check if we can use "c" as a lower bound and if it is better than
2397 * any previously found lower bound.
2399 * If "c" does not involve the dimension at the current depth,
2400 * then we cannot use it.
2401 * Otherwise, let "c" be of the form
2403 * i >= f(j)/a
2405 * We compute the maximal value of
2407 * -ceil(f(j)/a)) + i + 1
2409 * over the domain. If there is such a value "n", then we know
2411 * -ceil(f(j)/a)) + i + 1 <= n
2413 * or
2415 * i < ceil(f(j)/a)) + n
2417 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2418 * We just need to check if we have found any lower bound before and
2419 * if the new lower bound is better (smaller n or fewer integer divisions)
2420 * than the previously found lower bounds.
2422 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2423 __isl_keep isl_constraint *c)
2425 isl_aff *aff, *lower;
2426 isl_val *max;
2427 int better;
2429 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2430 return isl_stat_ok;
2432 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2433 lower = isl_aff_ceil(lower);
2434 aff = isl_aff_copy(lower);
2435 aff = isl_aff_neg(aff);
2436 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2437 aff = isl_aff_add_constant_si(aff, 1);
2438 max = isl_set_max_val(data->domain, aff);
2439 isl_aff_free(aff);
2441 better = is_better_lower_bound(data, lower, max);
2442 if (better < 0 || !better) {
2443 isl_val_free(max);
2444 isl_aff_free(lower);
2445 return better < 0 ? isl_stat_error : isl_stat_ok;
2448 isl_aff_free(data->lower);
2449 data->lower = lower;
2450 *data->n = isl_val_get_num_si(max);
2451 isl_val_free(max);
2453 return isl_stat_ok;
2456 /* Check if we can use "c" as a lower bound and if it is better than
2457 * any previously found lower bound.
2459 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2461 struct isl_find_unroll_data *data;
2462 isl_stat r;
2464 data = (struct isl_find_unroll_data *) user;
2465 r = update_unrolling_lower_bound(data, c);
2466 isl_constraint_free(c);
2468 return r;
2471 /* Look for a lower bound l(i) on the dimension at "depth"
2472 * and a size n such that "domain" is a subset of
2474 * { [i] : l(i) <= i_d < l(i) + n }
2476 * where d is "depth" and l(i) depends only on earlier dimensions.
2477 * Furthermore, try and find a lower bound such that n is as small as possible.
2478 * In particular, "n" needs to be finite.
2479 * "build" is the build in which the unrolling will be performed.
2480 * "expansion" is the expansion that needs to be applied to "domain"
2481 * in the unrolling that will be performed.
2483 * Inner dimensions have been eliminated from "domain" by the caller.
2485 * We first construct a collection of lower bounds on the input set
2486 * by computing its simple hull. We then iterate through them,
2487 * discarding those that we cannot use (either because they do not
2488 * involve the dimension at "depth" or because they have no corresponding
2489 * upper bound, meaning that "n" would be unbounded) and pick out the
2490 * best from the remaining ones.
2492 * If we cannot find a suitable lower bound, then we consider that
2493 * to be an error.
2495 static __isl_give isl_aff *find_unroll_lower_bound(
2496 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2497 int depth, __isl_keep isl_basic_map *expansion, int *n)
2499 struct isl_find_unroll_data data =
2500 { build, domain, depth, expansion, NULL, n, -1 };
2501 isl_basic_set *hull;
2503 hull = isl_set_simple_hull(isl_set_copy(domain));
2505 if (isl_basic_set_foreach_constraint(hull,
2506 &constraint_find_unroll, &data) < 0)
2507 goto error;
2509 isl_basic_set_free(hull);
2511 if (!data.lower)
2512 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2513 "cannot find lower bound for unrolling", return NULL);
2515 return data.lower;
2516 error:
2517 isl_basic_set_free(hull);
2518 return isl_aff_free(data.lower);
2521 /* Call "fn" on each iteration of the current dimension of "domain".
2522 * If "init" is not NULL, then it is called with the number of
2523 * iterations before any call to "fn".
2524 * Return -1 on failure.
2526 * Since we are going to be iterating over the individual values,
2527 * we first check if there are any strides on the current dimension.
2528 * If there is, we rewrite the current dimension i as
2530 * i = stride i' + offset
2532 * and then iterate over individual values of i' instead.
2534 * We then look for a lower bound on i' and a size such that the domain
2535 * is a subset of
2537 * { [j,i'] : l(j) <= i' < l(j) + n }
2539 * and then take slices of the domain at values of i'
2540 * between l(j) and l(j) + n - 1.
2542 * We compute the unshifted simple hull of each slice to ensure that
2543 * we have a single basic set per offset. The slicing constraint
2544 * may get simplified away before the unshifted simple hull is taken
2545 * and may therefore in some rare cases disappear from the result.
2546 * We therefore explicitly add the constraint back after computing
2547 * the unshifted simple hull to ensure that the basic sets
2548 * remain disjoint. The constraints that are dropped by taking the hull
2549 * will be taken into account at the next level, as in the case of the
2550 * atomic option.
2552 * Finally, we map i' back to i and call "fn".
2554 static int foreach_iteration(__isl_take isl_set *domain,
2555 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2556 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2558 int i, n;
2559 int empty;
2560 int depth;
2561 isl_multi_aff *expansion;
2562 isl_basic_map *bmap;
2563 isl_aff *lower = NULL;
2564 isl_ast_build *stride_build;
2566 depth = isl_ast_build_get_depth(build);
2568 domain = isl_ast_build_eliminate_inner(build, domain);
2569 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2570 stride_build = isl_ast_build_copy(build);
2571 stride_build = isl_ast_build_detect_strides(stride_build,
2572 isl_set_copy(domain));
2573 expansion = isl_ast_build_get_stride_expansion(stride_build);
2575 domain = isl_set_preimage_multi_aff(domain,
2576 isl_multi_aff_copy(expansion));
2577 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2578 isl_ast_build_free(stride_build);
2580 bmap = isl_basic_map_from_multi_aff(expansion);
2582 empty = isl_set_is_empty(domain);
2583 if (empty < 0) {
2584 n = -1;
2585 } else if (empty) {
2586 n = 0;
2587 } else {
2588 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2589 if (!lower)
2590 n = -1;
2592 if (n >= 0 && init && init(n, user) < 0)
2593 n = -1;
2594 for (i = 0; i < n; ++i) {
2595 isl_set *set;
2596 isl_basic_set *bset;
2597 isl_constraint *slice;
2599 slice = at_offset(depth, lower, i);
2600 set = isl_set_copy(domain);
2601 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2602 bset = isl_set_unshifted_simple_hull(set);
2603 bset = isl_basic_set_add_constraint(bset, slice);
2604 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2606 if (fn(bset, user) < 0)
2607 break;
2610 isl_aff_free(lower);
2611 isl_set_free(domain);
2612 isl_basic_map_free(bmap);
2614 return n < 0 || i < n ? -1 : 0;
2617 /* Data structure for storing the results and the intermediate objects
2618 * of compute_domains.
2620 * "list" is the main result of the function and contains a list
2621 * of disjoint basic sets for which code should be generated.
2623 * "executed" and "build" are inputs to compute_domains.
2624 * "schedule_domain" is the domain of "executed".
2626 * "option" constains the domains at the current depth that should by
2627 * atomic, separated or unrolled. These domains are as specified by
2628 * the user, except that inner dimensions have been eliminated and
2629 * that they have been made pair-wise disjoint.
2631 * "sep_class" contains the user-specified split into separation classes
2632 * specialized to the current depth.
2633 * "done" contains the union of the separation domains that have already
2634 * been handled.
2636 struct isl_codegen_domains {
2637 isl_basic_set_list *list;
2639 isl_union_map *executed;
2640 isl_ast_build *build;
2641 isl_set *schedule_domain;
2643 isl_set *option[4];
2645 isl_map *sep_class;
2646 isl_set *done;
2649 /* Internal data structure for do_unroll.
2651 * "domains" stores the results of compute_domains.
2652 * "class_domain" is the original class domain passed to do_unroll.
2653 * "unroll_domain" collects the unrolled iterations.
2655 struct isl_ast_unroll_data {
2656 struct isl_codegen_domains *domains;
2657 isl_set *class_domain;
2658 isl_set *unroll_domain;
2661 /* Given an iteration of an unrolled domain represented by "bset",
2662 * add it to data->domains->list.
2663 * Since we may have dropped some constraints, we intersect with
2664 * the class domain again to ensure that each element in the list
2665 * is disjoint from the other class domains.
2667 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2669 struct isl_ast_unroll_data *data = user;
2670 isl_set *set;
2671 isl_basic_set_list *list;
2673 set = isl_set_from_basic_set(bset);
2674 data->unroll_domain = isl_set_union(data->unroll_domain,
2675 isl_set_copy(set));
2676 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2677 set = isl_set_make_disjoint(set);
2678 list = isl_basic_set_list_from_set(set);
2679 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2680 list);
2682 return 0;
2685 /* Extend domains->list with a list of basic sets, one for each value
2686 * of the current dimension in "domain" and remove the corresponding
2687 * sets from the class domain. Return the updated class domain.
2688 * The divs that involve the current dimension have not been projected out
2689 * from this domain.
2691 * We call foreach_iteration to iterate over the individual values and
2692 * in do_unroll_iteration we collect the individual basic sets in
2693 * domains->list and their union in data->unroll_domain, which is then
2694 * used to update the class domain.
2696 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2697 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2699 struct isl_ast_unroll_data data;
2701 if (!domain)
2702 return isl_set_free(class_domain);
2703 if (!class_domain)
2704 return isl_set_free(domain);
2706 data.domains = domains;
2707 data.class_domain = class_domain;
2708 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2710 if (foreach_iteration(domain, domains->build, NULL,
2711 &do_unroll_iteration, &data) < 0)
2712 data.unroll_domain = isl_set_free(data.unroll_domain);
2714 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2716 return class_domain;
2719 /* Add domains to domains->list for each individual value of the current
2720 * dimension, for that part of the schedule domain that lies in the
2721 * intersection of the option domain and the class domain.
2722 * Remove the corresponding sets from the class domain and
2723 * return the updated class domain.
2725 * We first break up the unroll option domain into individual pieces
2726 * and then handle each of them separately. The unroll option domain
2727 * has been made disjoint in compute_domains_init_options,
2729 * Note that we actively want to combine different pieces of the
2730 * schedule domain that have the same value at the current dimension.
2731 * We therefore need to break up the unroll option domain before
2732 * intersecting with class and schedule domain, hoping that the
2733 * unroll option domain specified by the user is relatively simple.
2735 static __isl_give isl_set *compute_unroll_domains(
2736 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2738 isl_set *unroll_domain;
2739 isl_basic_set_list *unroll_list;
2740 int i, n;
2741 int empty;
2743 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2744 if (empty < 0)
2745 return isl_set_free(class_domain);
2746 if (empty)
2747 return class_domain;
2749 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2750 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2752 n = isl_basic_set_list_n_basic_set(unroll_list);
2753 for (i = 0; i < n; ++i) {
2754 isl_basic_set *bset;
2756 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2757 unroll_domain = isl_set_from_basic_set(bset);
2758 unroll_domain = isl_set_intersect(unroll_domain,
2759 isl_set_copy(class_domain));
2760 unroll_domain = isl_set_intersect(unroll_domain,
2761 isl_set_copy(domains->schedule_domain));
2763 empty = isl_set_is_empty(unroll_domain);
2764 if (empty >= 0 && empty) {
2765 isl_set_free(unroll_domain);
2766 continue;
2769 class_domain = do_unroll(domains, unroll_domain, class_domain);
2772 isl_basic_set_list_free(unroll_list);
2774 return class_domain;
2777 /* Try and construct a single basic set that includes the intersection of
2778 * the schedule domain, the atomic option domain and the class domain.
2779 * Add the resulting basic set(s) to domains->list and remove them
2780 * from class_domain. Return the updated class domain.
2782 * We construct a single domain rather than trying to combine
2783 * the schedule domains of individual domains because we are working
2784 * within a single component so that non-overlapping schedule domains
2785 * should already have been separated.
2786 * We do however need to make sure that this single domains is a subset
2787 * of the class domain so that it would not intersect with any other
2788 * class domains. This means that we may end up splitting up the atomic
2789 * domain in case separation classes are being used.
2791 * "domain" is the intersection of the schedule domain and the class domain,
2792 * with inner dimensions projected out.
2794 static __isl_give isl_set *compute_atomic_domain(
2795 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2797 isl_basic_set *bset;
2798 isl_basic_set_list *list;
2799 isl_set *domain, *atomic_domain;
2800 int empty;
2802 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2803 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2804 domain = isl_set_intersect(domain,
2805 isl_set_copy(domains->schedule_domain));
2806 empty = isl_set_is_empty(domain);
2807 if (empty < 0)
2808 class_domain = isl_set_free(class_domain);
2809 if (empty) {
2810 isl_set_free(domain);
2811 return class_domain;
2814 domain = isl_ast_build_eliminate(domains->build, domain);
2815 domain = isl_set_coalesce(domain);
2816 bset = isl_set_unshifted_simple_hull(domain);
2817 domain = isl_set_from_basic_set(bset);
2818 atomic_domain = isl_set_copy(domain);
2819 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2820 class_domain = isl_set_subtract(class_domain, atomic_domain);
2821 domain = isl_set_make_disjoint(domain);
2822 list = isl_basic_set_list_from_set(domain);
2823 domains->list = isl_basic_set_list_concat(domains->list, list);
2825 return class_domain;
2828 /* Split up the schedule domain into uniform basic sets,
2829 * in the sense that each element in a basic set is associated to
2830 * elements of the same domains, and add the result to domains->list.
2831 * Do this for that part of the schedule domain that lies in the
2832 * intersection of "class_domain" and the separate option domain.
2834 * "class_domain" may or may not include the constraints
2835 * of the schedule domain, but this does not make a difference
2836 * since we are going to intersect it with the domain of the inverse schedule.
2837 * If it includes schedule domain constraints, then they may involve
2838 * inner dimensions, but we will eliminate them in separation_domain.
2840 static int compute_separate_domain(struct isl_codegen_domains *domains,
2841 __isl_keep isl_set *class_domain)
2843 isl_space *space;
2844 isl_set *domain;
2845 isl_union_map *executed;
2846 isl_basic_set_list *list;
2847 int empty;
2849 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2850 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2851 executed = isl_union_map_copy(domains->executed);
2852 executed = isl_union_map_intersect_domain(executed,
2853 isl_union_set_from_set(domain));
2854 empty = isl_union_map_is_empty(executed);
2855 if (empty < 0 || empty) {
2856 isl_union_map_free(executed);
2857 return empty < 0 ? -1 : 0;
2860 space = isl_set_get_space(class_domain);
2861 domain = separate_schedule_domains(space, executed, domains->build);
2863 list = isl_basic_set_list_from_set(domain);
2864 domains->list = isl_basic_set_list_concat(domains->list, list);
2866 return 0;
2869 /* Split up the domain at the current depth into disjoint
2870 * basic sets for which code should be generated separately
2871 * for the given separation class domain.
2873 * If any separation classes have been defined, then "class_domain"
2874 * is the domain of the current class and does not refer to inner dimensions.
2875 * Otherwise, "class_domain" is the universe domain.
2877 * We first make sure that the class domain is disjoint from
2878 * previously considered class domains.
2880 * The separate domains can be computed directly from the "class_domain".
2882 * The unroll, atomic and remainder domains need the constraints
2883 * from the schedule domain.
2885 * For unrolling, the actual schedule domain is needed (with divs that
2886 * may refer to the current dimension) so that stride detection can be
2887 * performed.
2889 * For atomic and remainder domains, inner dimensions and divs involving
2890 * the current dimensions should be eliminated.
2891 * In case we are working within a separation class, we need to intersect
2892 * the result with the current "class_domain" to ensure that the domains
2893 * are disjoint from those generated from other class domains.
2895 * The domain that has been made atomic may be larger than specified
2896 * by the user since it needs to be representable as a single basic set.
2897 * This possibly larger domain is removed from class_domain by
2898 * compute_atomic_domain. It is computed first so that the extended domain
2899 * would not overlap with any domains computed before.
2900 * Similary, the unrolled domains may have some constraints removed and
2901 * may therefore also be larger than specified by the user.
2903 * If anything is left after handling separate, unroll and atomic,
2904 * we split it up into basic sets and append the basic sets to domains->list.
2906 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2907 __isl_take isl_set *class_domain)
2909 isl_basic_set_list *list;
2910 isl_set *domain;
2912 class_domain = isl_set_subtract(class_domain,
2913 isl_set_copy(domains->done));
2914 domains->done = isl_set_union(domains->done,
2915 isl_set_copy(class_domain));
2917 class_domain = compute_atomic_domain(domains, class_domain);
2918 class_domain = compute_unroll_domains(domains, class_domain);
2920 domain = isl_set_copy(class_domain);
2922 if (compute_separate_domain(domains, domain) < 0)
2923 goto error;
2924 domain = isl_set_subtract(domain,
2925 isl_set_copy(domains->option[isl_ast_loop_separate]));
2927 domain = isl_set_intersect(domain,
2928 isl_set_copy(domains->schedule_domain));
2930 domain = isl_ast_build_eliminate(domains->build, domain);
2931 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2933 domain = isl_set_coalesce(domain);
2934 domain = isl_set_make_disjoint(domain);
2936 list = isl_basic_set_list_from_set(domain);
2937 domains->list = isl_basic_set_list_concat(domains->list, list);
2939 isl_set_free(class_domain);
2941 return isl_stat_ok;
2942 error:
2943 isl_set_free(domain);
2944 isl_set_free(class_domain);
2945 return isl_stat_error;
2948 /* Split up the domain at the current depth into disjoint
2949 * basic sets for which code should be generated separately
2950 * for the separation class identified by "pnt".
2952 * We extract the corresponding class domain from domains->sep_class,
2953 * eliminate inner dimensions and pass control to compute_partial_domains.
2955 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2957 struct isl_codegen_domains *domains = user;
2958 isl_set *class_set;
2959 isl_set *domain;
2960 int disjoint;
2962 class_set = isl_set_from_point(pnt);
2963 domain = isl_map_domain(isl_map_intersect_range(
2964 isl_map_copy(domains->sep_class), class_set));
2965 domain = isl_ast_build_compute_gist(domains->build, domain);
2966 domain = isl_ast_build_eliminate(domains->build, domain);
2968 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2969 if (disjoint < 0)
2970 return isl_stat_error;
2971 if (disjoint) {
2972 isl_set_free(domain);
2973 return isl_stat_ok;
2976 return compute_partial_domains(domains, domain);
2979 /* Extract the domains at the current depth that should be atomic,
2980 * separated or unrolled and store them in option.
2982 * The domains specified by the user might overlap, so we make
2983 * them disjoint by subtracting earlier domains from later domains.
2985 static void compute_domains_init_options(isl_set *option[4],
2986 __isl_keep isl_ast_build *build)
2988 enum isl_ast_loop_type type, type2;
2989 isl_set *unroll;
2991 for (type = isl_ast_loop_atomic;
2992 type <= isl_ast_loop_separate; ++type) {
2993 option[type] = isl_ast_build_get_option_domain(build, type);
2994 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
2995 option[type] = isl_set_subtract(option[type],
2996 isl_set_copy(option[type2]));
2999 unroll = option[isl_ast_loop_unroll];
3000 unroll = isl_set_coalesce(unroll);
3001 unroll = isl_set_make_disjoint(unroll);
3002 option[isl_ast_loop_unroll] = unroll;
3005 /* Split up the domain at the current depth into disjoint
3006 * basic sets for which code should be generated separately,
3007 * based on the user-specified options.
3008 * Return the list of disjoint basic sets.
3010 * There are three kinds of domains that we need to keep track of.
3011 * - the "schedule domain" is the domain of "executed"
3012 * - the "class domain" is the domain corresponding to the currrent
3013 * separation class
3014 * - the "option domain" is the domain corresponding to one of the options
3015 * atomic, unroll or separate
3017 * We first consider the individial values of the separation classes
3018 * and split up the domain for each of them separately.
3019 * Finally, we consider the remainder. If no separation classes were
3020 * specified, then we call compute_partial_domains with the universe
3021 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3022 * with inner dimensions removed. We do this because we want to
3023 * avoid computing the complement of the class domains (i.e., the difference
3024 * between the universe and domains->done).
3026 static __isl_give isl_basic_set_list *compute_domains(
3027 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3029 struct isl_codegen_domains domains;
3030 isl_ctx *ctx;
3031 isl_set *domain;
3032 isl_union_set *schedule_domain;
3033 isl_set *classes;
3034 isl_space *space;
3035 int n_param;
3036 enum isl_ast_loop_type type;
3037 int empty;
3039 if (!executed)
3040 return NULL;
3042 ctx = isl_union_map_get_ctx(executed);
3043 domains.list = isl_basic_set_list_alloc(ctx, 0);
3045 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3046 domain = isl_set_from_union_set(schedule_domain);
3048 compute_domains_init_options(domains.option, build);
3050 domains.sep_class = isl_ast_build_get_separation_class(build);
3051 classes = isl_map_range(isl_map_copy(domains.sep_class));
3052 n_param = isl_set_dim(classes, isl_dim_param);
3053 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3055 space = isl_set_get_space(domain);
3056 domains.build = build;
3057 domains.schedule_domain = isl_set_copy(domain);
3058 domains.executed = executed;
3059 domains.done = isl_set_empty(space);
3061 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3062 domains.list = isl_basic_set_list_free(domains.list);
3063 isl_set_free(classes);
3065 empty = isl_set_is_empty(domains.done);
3066 if (empty < 0) {
3067 domains.list = isl_basic_set_list_free(domains.list);
3068 domain = isl_set_free(domain);
3069 } else if (empty) {
3070 isl_set_free(domain);
3071 domain = isl_set_universe(isl_set_get_space(domains.done));
3072 } else {
3073 domain = isl_ast_build_eliminate(build, domain);
3075 if (compute_partial_domains(&domains, domain) < 0)
3076 domains.list = isl_basic_set_list_free(domains.list);
3078 isl_set_free(domains.schedule_domain);
3079 isl_set_free(domains.done);
3080 isl_map_free(domains.sep_class);
3081 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3082 isl_set_free(domains.option[type]);
3084 return domains.list;
3087 /* Generate code for a single component, after shifting (if any)
3088 * has been applied, in case the schedule was specified as a union map.
3090 * We first split up the domain at the current depth into disjoint
3091 * basic sets based on the user-specified options.
3092 * Then we generated code for each of them and concatenate the results.
3094 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3095 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3097 isl_basic_set_list *domain_list;
3098 isl_ast_graft_list *list = NULL;
3100 domain_list = compute_domains(executed, build);
3101 list = generate_parallel_domains(domain_list, executed, build);
3103 isl_basic_set_list_free(domain_list);
3104 isl_union_map_free(executed);
3105 isl_ast_build_free(build);
3107 return list;
3110 /* Generate code for a single component, after shifting (if any)
3111 * has been applied, in case the schedule was specified as a schedule tree
3112 * and the separate option was specified.
3114 * We perform separation on the domain of "executed" and then generate
3115 * an AST for each of the resulting disjoint basic sets.
3117 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3118 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3120 isl_space *space;
3121 isl_set *domain;
3122 isl_basic_set_list *domain_list;
3123 isl_ast_graft_list *list;
3125 space = isl_ast_build_get_space(build, 1);
3126 domain = separate_schedule_domains(space,
3127 isl_union_map_copy(executed), build);
3128 domain_list = isl_basic_set_list_from_set(domain);
3130 list = generate_parallel_domains(domain_list, executed, build);
3132 isl_basic_set_list_free(domain_list);
3133 isl_union_map_free(executed);
3134 isl_ast_build_free(build);
3136 return list;
3139 /* Internal data structure for generate_shifted_component_tree_unroll.
3141 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3142 * "list" collects the constructs grafts.
3144 struct isl_ast_unroll_tree_data {
3145 isl_union_map *executed;
3146 isl_ast_build *build;
3147 isl_ast_graft_list *list;
3150 /* Initialize data->list to a list of "n" elements.
3152 static int init_unroll_tree(int n, void *user)
3154 struct isl_ast_unroll_tree_data *data = user;
3155 isl_ctx *ctx;
3157 ctx = isl_ast_build_get_ctx(data->build);
3158 data->list = isl_ast_graft_list_alloc(ctx, n);
3160 return 0;
3163 /* Given an iteration of an unrolled domain represented by "bset",
3164 * generate the corresponding AST and add the result to data->list.
3166 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3168 struct isl_ast_unroll_tree_data *data = user;
3170 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3171 bset, isl_ast_build_copy(data->build));
3173 return 0;
3176 /* Generate code for a single component, after shifting (if any)
3177 * has been applied, in case the schedule was specified as a schedule tree
3178 * and the unroll option was specified.
3180 * We call foreach_iteration to iterate over the individual values and
3181 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3183 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3184 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3185 __isl_take isl_ast_build *build)
3187 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3189 if (foreach_iteration(domain, build, &init_unroll_tree,
3190 &do_unroll_tree_iteration, &data) < 0)
3191 data.list = isl_ast_graft_list_free(data.list);
3193 isl_union_map_free(executed);
3194 isl_ast_build_free(build);
3196 return data.list;
3199 /* Generate code for a single component, after shifting (if any)
3200 * has been applied, in case the schedule was specified as a schedule tree.
3201 * In particular, handle the base case where there is either no isolated
3202 * set or we are within the isolated set (in which case "isolated" is set)
3203 * or the iterations that precede or follow the isolated set.
3205 * The schedule domain is broken up or combined into basic sets
3206 * according to the AST generation option specified in the current
3207 * schedule node, which may be either atomic, separate, unroll or
3208 * unspecified. If the option is unspecified, then we currently simply
3209 * split the schedule domain into disjoint basic sets.
3211 * In case the separate option is specified, the AST generation is
3212 * handled by generate_shifted_component_tree_separate.
3213 * In the other cases, we need the global schedule domain.
3214 * In the unroll case, the AST generation is then handled by
3215 * generate_shifted_component_tree_unroll which needs the actual
3216 * schedule domain (with divs that may refer to the current dimension)
3217 * so that stride detection can be performed.
3218 * In the atomic or unspecified case, inner dimensions and divs involving
3219 * the current dimensions should be eliminated.
3220 * The result is then either combined into a single basic set or
3221 * split up into disjoint basic sets.
3222 * Finally an AST is generated for each basic set and the results are
3223 * concatenated.
3225 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3226 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3227 int isolated)
3229 isl_union_set *schedule_domain;
3230 isl_set *domain;
3231 isl_basic_set_list *domain_list;
3232 isl_ast_graft_list *list;
3233 enum isl_ast_loop_type type;
3235 type = isl_ast_build_get_loop_type(build, isolated);
3236 if (type < 0)
3237 goto error;
3239 if (type == isl_ast_loop_separate)
3240 return generate_shifted_component_tree_separate(executed,
3241 build);
3243 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3244 domain = isl_set_from_union_set(schedule_domain);
3246 if (type == isl_ast_loop_unroll)
3247 return generate_shifted_component_tree_unroll(executed, domain,
3248 build);
3250 domain = isl_ast_build_eliminate(build, domain);
3251 domain = isl_set_coalesce(domain);
3253 if (type == isl_ast_loop_atomic) {
3254 isl_basic_set *hull;
3255 hull = isl_set_unshifted_simple_hull(domain);
3256 domain_list = isl_basic_set_list_from_basic_set(hull);
3257 } else {
3258 domain = isl_set_make_disjoint(domain);
3259 domain_list = isl_basic_set_list_from_set(domain);
3262 list = generate_parallel_domains(domain_list, executed, build);
3264 isl_basic_set_list_free(domain_list);
3265 isl_union_map_free(executed);
3266 isl_ast_build_free(build);
3268 return list;
3269 error:
3270 isl_union_map_free(executed);
3271 isl_ast_build_free(build);
3272 return NULL;
3275 /* Extract out the disjunction imposed by "domain" on the outer
3276 * schedule dimensions.
3278 * In particular, remove all inner dimensions from "domain" (including
3279 * the current dimension) and then remove the constraints that are shared
3280 * by all disjuncts in the result.
3282 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3283 __isl_keep isl_ast_build *build)
3285 isl_set *hull;
3286 int depth, dim;
3288 domain = isl_ast_build_specialize(build, domain);
3289 depth = isl_ast_build_get_depth(build);
3290 dim = isl_set_dim(domain, isl_dim_set);
3291 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3292 domain = isl_set_remove_unknown_divs(domain);
3293 hull = isl_set_copy(domain);
3294 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3295 domain = isl_set_gist(domain, hull);
3297 return domain;
3300 /* Add "guard" to the grafts in "list".
3301 * "build" is the outer AST build, while "sub_build" includes "guard"
3302 * in its generated domain.
3304 * First combine the grafts into a single graft and then add the guard.
3305 * If the list is empty, or if some error occurred, then simply return
3306 * the list.
3308 static __isl_give isl_ast_graft_list *list_add_guard(
3309 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3310 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3312 isl_ast_graft *graft;
3314 list = isl_ast_graft_list_fuse(list, sub_build);
3316 if (isl_ast_graft_list_n_ast_graft(list) != 1)
3317 return list;
3319 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3320 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3321 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3323 return list;
3326 /* Generate code for a single component, after shifting (if any)
3327 * has been applied, in case the schedule was specified as a schedule tree.
3328 * In particular, do so for the specified subset of the schedule domain.
3330 * If we are outside of the isolated part, then "domain" may include
3331 * a disjunction. Explicitly generate this disjunction at this point
3332 * instead of relying on the disjunction getting hoisted back up
3333 * to this level.
3335 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3336 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3337 __isl_keep isl_ast_build *build, int isolated)
3339 isl_union_set *uset;
3340 isl_ast_graft_list *list;
3341 isl_ast_build *sub_build;
3342 int empty;
3344 uset = isl_union_set_from_set(isl_set_copy(domain));
3345 executed = isl_union_map_copy(executed);
3346 executed = isl_union_map_intersect_domain(executed, uset);
3347 empty = isl_union_map_is_empty(executed);
3348 if (empty < 0)
3349 goto error;
3350 if (empty) {
3351 isl_ctx *ctx;
3352 isl_union_map_free(executed);
3353 isl_set_free(domain);
3354 ctx = isl_ast_build_get_ctx(build);
3355 return isl_ast_graft_list_alloc(ctx, 0);
3358 sub_build = isl_ast_build_copy(build);
3359 if (!isolated) {
3360 domain = extract_disjunction(domain, build);
3361 sub_build = isl_ast_build_restrict_generated(sub_build,
3362 isl_set_copy(domain));
3364 list = generate_shifted_component_tree_base(executed,
3365 isl_ast_build_copy(sub_build), isolated);
3366 if (!isolated)
3367 list = list_add_guard(list, domain, build, sub_build);
3368 isl_ast_build_free(sub_build);
3369 isl_set_free(domain);
3370 return list;
3371 error:
3372 isl_union_map_free(executed);
3373 isl_set_free(domain);
3374 return NULL;
3377 /* Generate code for a single component, after shifting (if any)
3378 * has been applied, in case the schedule was specified as a schedule tree.
3380 * We first check if the user has specified an isolated schedule domain
3381 * and that we are not already outside of this isolated schedule domain.
3382 * If so, we break up the schedule domain into iterations that
3383 * precede the isolated domain, the isolated domain itself,
3384 * the iterations that follow the isolated domain and
3385 * the remaining iterations (those that are incomparable
3386 * to the isolated domain).
3387 * We generate an AST for each piece and concatenate the results.
3388 * If no isolated set has been specified, then we generate an
3389 * AST for the entire inverse schedule.
3391 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3392 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3394 int i, depth;
3395 int empty, has_isolate;
3396 isl_space *space;
3397 isl_union_set *schedule_domain;
3398 isl_set *domain;
3399 isl_basic_set *hull;
3400 isl_set *isolated, *before, *after, *test;
3401 isl_map *gt, *lt;
3402 isl_ast_graft_list *list, *res;
3404 build = isl_ast_build_extract_isolated(build);
3405 has_isolate = isl_ast_build_has_isolated(build);
3406 if (has_isolate < 0)
3407 executed = isl_union_map_free(executed);
3408 else if (!has_isolate)
3409 return generate_shifted_component_tree_base(executed, build, 0);
3411 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3412 domain = isl_set_from_union_set(schedule_domain);
3414 isolated = isl_ast_build_get_isolated(build);
3415 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3416 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3417 empty = isl_set_is_empty(test);
3418 isl_set_free(test);
3419 if (empty < 0)
3420 goto error;
3421 if (empty) {
3422 isl_set_free(isolated);
3423 isl_set_free(domain);
3424 return generate_shifted_component_tree_base(executed, build, 0);
3426 isolated = isl_ast_build_eliminate(build, isolated);
3427 hull = isl_set_unshifted_simple_hull(isolated);
3428 isolated = isl_set_from_basic_set(hull);
3430 depth = isl_ast_build_get_depth(build);
3431 space = isl_space_map_from_set(isl_set_get_space(isolated));
3432 gt = isl_map_universe(space);
3433 for (i = 0; i < depth; ++i)
3434 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3435 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3436 lt = isl_map_reverse(isl_map_copy(gt));
3437 before = isl_set_apply(isl_set_copy(isolated), gt);
3438 after = isl_set_apply(isl_set_copy(isolated), lt);
3440 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3441 domain = isl_set_subtract(domain, isl_set_copy(before));
3442 domain = isl_set_subtract(domain, isl_set_copy(after));
3443 after = isl_set_subtract(after, isl_set_copy(isolated));
3444 after = isl_set_subtract(after, isl_set_copy(before));
3445 before = isl_set_subtract(before, isl_set_copy(isolated));
3447 res = generate_shifted_component_tree_part(executed, before, build, 0);
3448 list = generate_shifted_component_tree_part(executed, isolated,
3449 build, 1);
3450 res = isl_ast_graft_list_concat(res, list);
3451 list = generate_shifted_component_tree_part(executed, after, build, 0);
3452 res = isl_ast_graft_list_concat(res, list);
3453 list = generate_shifted_component_tree_part(executed, domain, build, 0);
3454 res = isl_ast_graft_list_concat(res, list);
3456 isl_union_map_free(executed);
3457 isl_ast_build_free(build);
3459 return res;
3460 error:
3461 isl_set_free(domain);
3462 isl_set_free(isolated);
3463 isl_union_map_free(executed);
3464 isl_ast_build_free(build);
3465 return NULL;
3468 /* Generate code for a single component, after shifting (if any)
3469 * has been applied.
3471 * Call generate_shifted_component_tree or generate_shifted_component_flat
3472 * depending on whether the schedule was specified as a schedule tree.
3474 static __isl_give isl_ast_graft_list *generate_shifted_component(
3475 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3477 if (isl_ast_build_has_schedule_node(build))
3478 return generate_shifted_component_tree(executed, build);
3479 else
3480 return generate_shifted_component_flat(executed, build);
3483 struct isl_set_map_pair {
3484 isl_set *set;
3485 isl_map *map;
3488 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3489 * of indices into the "domain" array,
3490 * return the union of the "map" fields of the elements
3491 * indexed by the first "n" elements of "order".
3493 static __isl_give isl_union_map *construct_component_executed(
3494 struct isl_set_map_pair *domain, int *order, int n)
3496 int i;
3497 isl_map *map;
3498 isl_union_map *executed;
3500 map = isl_map_copy(domain[order[0]].map);
3501 executed = isl_union_map_from_map(map);
3502 for (i = 1; i < n; ++i) {
3503 map = isl_map_copy(domain[order[i]].map);
3504 executed = isl_union_map_add_map(executed, map);
3507 return executed;
3510 /* Generate code for a single component, after shifting (if any)
3511 * has been applied.
3513 * The component inverse schedule is specified as the "map" fields
3514 * of the elements of "domain" indexed by the first "n" elements of "order".
3516 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3517 struct isl_set_map_pair *domain, int *order, int n,
3518 __isl_take isl_ast_build *build)
3520 isl_union_map *executed;
3522 executed = construct_component_executed(domain, order, n);
3523 return generate_shifted_component(executed, build);
3526 /* Does set dimension "pos" of "set" have an obviously fixed value?
3528 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3530 int fixed;
3531 isl_val *v;
3533 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3534 if (!v)
3535 return -1;
3536 fixed = !isl_val_is_nan(v);
3537 isl_val_free(v);
3539 return fixed;
3542 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3543 * of indices into the "domain" array,
3544 * do all (except for at most one) of the "set" field of the elements
3545 * indexed by the first "n" elements of "order" have a fixed value
3546 * at position "depth"?
3548 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3549 int *order, int n, int depth)
3551 int i;
3552 int non_fixed = -1;
3554 for (i = 0; i < n; ++i) {
3555 int f;
3557 f = dim_is_fixed(domain[order[i]].set, depth);
3558 if (f < 0)
3559 return -1;
3560 if (f)
3561 continue;
3562 if (non_fixed >= 0)
3563 return 0;
3564 non_fixed = i;
3567 return 1;
3570 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3571 * of indices into the "domain" array,
3572 * eliminate the inner dimensions from the "set" field of the elements
3573 * indexed by the first "n" elements of "order", provided the current
3574 * dimension does not have a fixed value.
3576 * Return the index of the first element in "order" with a corresponding
3577 * "set" field that does not have an (obviously) fixed value.
3579 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3580 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3582 int i;
3583 int base = -1;
3585 for (i = n - 1; i >= 0; --i) {
3586 int f;
3587 f = dim_is_fixed(domain[order[i]].set, depth);
3588 if (f < 0)
3589 return -1;
3590 if (f)
3591 continue;
3592 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3593 domain[order[i]].set);
3594 base = i;
3597 return base;
3600 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3601 * of indices into the "domain" array,
3602 * find the element of "domain" (amongst those indexed by the first "n"
3603 * elements of "order") with the "set" field that has the smallest
3604 * value for the current iterator.
3606 * Note that the domain with the smallest value may depend on the parameters
3607 * and/or outer loop dimension. Since the result of this function is only
3608 * used as heuristic, we only make a reasonable attempt at finding the best
3609 * domain, one that should work in case a single domain provides the smallest
3610 * value for the current dimension over all values of the parameters
3611 * and outer dimensions.
3613 * In particular, we compute the smallest value of the first domain
3614 * and replace it by that of any later domain if that later domain
3615 * has a smallest value that is smaller for at least some value
3616 * of the parameters and outer dimensions.
3618 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3619 __isl_keep isl_ast_build *build)
3621 int i;
3622 isl_map *min_first;
3623 int first = 0;
3625 min_first = isl_ast_build_map_to_iterator(build,
3626 isl_set_copy(domain[order[0]].set));
3627 min_first = isl_map_lexmin(min_first);
3629 for (i = 1; i < n; ++i) {
3630 isl_map *min, *test;
3631 int empty;
3633 min = isl_ast_build_map_to_iterator(build,
3634 isl_set_copy(domain[order[i]].set));
3635 min = isl_map_lexmin(min);
3636 test = isl_map_copy(min);
3637 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3638 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3639 empty = isl_map_is_empty(test);
3640 isl_map_free(test);
3641 if (empty >= 0 && !empty) {
3642 isl_map_free(min_first);
3643 first = i;
3644 min_first = min;
3645 } else
3646 isl_map_free(min);
3648 if (empty < 0)
3649 break;
3652 isl_map_free(min_first);
3654 return i < n ? -1 : first;
3657 /* Construct a shifted inverse schedule based on the original inverse schedule,
3658 * the stride and the offset.
3660 * The original inverse schedule is specified as the "map" fields
3661 * of the elements of "domain" indexed by the first "n" elements of "order".
3663 * "stride" and "offset" are such that the difference
3664 * between the values of the current dimension of domain "i"
3665 * and the values of the current dimension for some reference domain are
3666 * equal to
3668 * stride * integer + offset[i]
3670 * Moreover, 0 <= offset[i] < stride.
3672 * For each domain, we create a map
3674 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3676 * where j refers to the current dimension and the other dimensions are
3677 * unchanged, and apply this map to the original schedule domain.
3679 * For example, for the original schedule
3681 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3683 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3684 * we apply the mapping
3686 * { [j] -> [j, 0] }
3688 * to the schedule of the "A" domain and the mapping
3690 * { [j - 1] -> [j, 1] }
3692 * to the schedule of the "B" domain.
3695 * Note that after the transformation, the differences between pairs
3696 * of values of the current dimension over all domains are multiples
3697 * of stride and that we have therefore exposed the stride.
3700 * To see that the mapping preserves the lexicographic order,
3701 * first note that each of the individual maps above preserves the order.
3702 * If the value of the current iterator is j1 in one domain and j2 in another,
3703 * then if j1 = j2, we know that the same map is applied to both domains
3704 * and the order is preserved.
3705 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3706 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3708 * j1 - c1 < j2 - c2
3710 * and the order is preserved.
3711 * If c1 < c2, then we know
3713 * 0 <= c2 - c1 < s
3715 * We also have
3717 * j2 - j1 = n * s + r
3719 * with n >= 0 and 0 <= r < s.
3720 * In other words, r = c2 - c1.
3721 * If n > 0, then
3723 * j1 - c1 < j2 - c2
3725 * If n = 0, then
3727 * j1 - c1 = j2 - c2
3729 * and so
3731 * (j1 - c1, c1) << (j2 - c2, c2)
3733 * with "<<" the lexicographic order, proving that the order is preserved
3734 * in all cases.
3736 static __isl_give isl_union_map *contruct_shifted_executed(
3737 struct isl_set_map_pair *domain, int *order, int n,
3738 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3739 __isl_take isl_ast_build *build)
3741 int i;
3742 isl_union_map *executed;
3743 isl_space *space;
3744 isl_map *map;
3745 int depth;
3746 isl_constraint *c;
3748 depth = isl_ast_build_get_depth(build);
3749 space = isl_ast_build_get_space(build, 1);
3750 executed = isl_union_map_empty(isl_space_copy(space));
3751 space = isl_space_map_from_set(space);
3752 map = isl_map_identity(isl_space_copy(space));
3753 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3754 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3755 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3757 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3758 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3759 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3761 for (i = 0; i < n; ++i) {
3762 isl_map *map_i;
3763 isl_val *v;
3765 v = isl_multi_val_get_val(offset, i);
3766 if (!v)
3767 break;
3768 map_i = isl_map_copy(map);
3769 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3770 isl_val_copy(v));
3771 v = isl_val_neg(v);
3772 c = isl_constraint_set_constant_val(c, v);
3773 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3775 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3776 map_i);
3777 executed = isl_union_map_add_map(executed, map_i);
3780 isl_constraint_free(c);
3781 isl_map_free(map);
3783 if (i < n)
3784 executed = isl_union_map_free(executed);
3786 return executed;
3789 /* Generate code for a single component, after exposing the stride,
3790 * given that the schedule domain is "shifted strided".
3792 * The component inverse schedule is specified as the "map" fields
3793 * of the elements of "domain" indexed by the first "n" elements of "order".
3795 * The schedule domain being "shifted strided" means that the differences
3796 * between the values of the current dimension of domain "i"
3797 * and the values of the current dimension for some reference domain are
3798 * equal to
3800 * stride * integer + offset[i]
3802 * We first look for the domain with the "smallest" value for the current
3803 * dimension and adjust the offsets such that the offset of the "smallest"
3804 * domain is equal to zero. The other offsets are reduced modulo stride.
3806 * Based on this information, we construct a new inverse schedule in
3807 * contruct_shifted_executed that exposes the stride.
3808 * Since this involves the introduction of a new schedule dimension,
3809 * the build needs to be changed accodingly.
3810 * After computing the AST, the newly introduced dimension needs
3811 * to be removed again from the list of grafts. We do this by plugging
3812 * in a mapping that represents the new schedule domain in terms of the
3813 * old schedule domain.
3815 static __isl_give isl_ast_graft_list *generate_shift_component(
3816 struct isl_set_map_pair *domain, int *order, int n,
3817 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3818 __isl_take isl_ast_build *build)
3820 isl_ast_graft_list *list;
3821 int first;
3822 int depth;
3823 isl_val *val;
3824 isl_multi_val *mv;
3825 isl_space *space;
3826 isl_multi_aff *ma, *zero;
3827 isl_union_map *executed;
3829 depth = isl_ast_build_get_depth(build);
3831 first = first_offset(domain, order, n, build);
3832 if (first < 0)
3833 goto error;
3835 mv = isl_multi_val_copy(offset);
3836 val = isl_multi_val_get_val(offset, first);
3837 val = isl_val_neg(val);
3838 mv = isl_multi_val_add_val(mv, val);
3839 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3841 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3842 build);
3843 space = isl_ast_build_get_space(build, 1);
3844 space = isl_space_map_from_set(space);
3845 ma = isl_multi_aff_identity(isl_space_copy(space));
3846 space = isl_space_from_domain(isl_space_domain(space));
3847 space = isl_space_add_dims(space, isl_dim_out, 1);
3848 zero = isl_multi_aff_zero(space);
3849 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3850 build = isl_ast_build_insert_dim(build, depth + 1);
3851 list = generate_shifted_component(executed, build);
3853 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3855 isl_multi_val_free(mv);
3857 return list;
3858 error:
3859 isl_ast_build_free(build);
3860 return NULL;
3863 /* Does any node in the schedule tree rooted at the current schedule node
3864 * of "build" depend on outer schedule nodes?
3866 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
3868 isl_schedule_node *node;
3869 int dependent = 0;
3871 node = isl_ast_build_get_schedule_node(build);
3872 dependent = isl_schedule_node_is_subtree_anchored(node);
3873 isl_schedule_node_free(node);
3875 return dependent;
3878 /* Generate code for a single component.
3880 * The component inverse schedule is specified as the "map" fields
3881 * of the elements of "domain" indexed by the first "n" elements of "order".
3883 * This function may modify the "set" fields of "domain".
3885 * Before proceeding with the actual code generation for the component,
3886 * we first check if there are any "shifted" strides, meaning that
3887 * the schedule domains of the individual domains are all strided,
3888 * but that they have different offsets, resulting in the union
3889 * of schedule domains not being strided anymore.
3891 * The simplest example is the schedule
3893 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3895 * Both schedule domains are strided, but their union is not.
3896 * This function detects such cases and then rewrites the schedule to
3898 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3900 * In the new schedule, the schedule domains have the same offset (modulo
3901 * the stride), ensuring that the union of schedule domains is also strided.
3904 * If there is only a single domain in the component, then there is
3905 * nothing to do. Similarly, if the current schedule dimension has
3906 * a fixed value for almost all domains then there is nothing to be done.
3907 * In particular, we need at least two domains where the current schedule
3908 * dimension does not have a fixed value.
3909 * Finally, in case of a schedule map input,
3910 * if any of the options refer to the current schedule dimension,
3911 * then we bail out as well. It would be possible to reformulate the options
3912 * in terms of the new schedule domain, but that would introduce constraints
3913 * that separate the domains in the options and that is something we would
3914 * like to avoid.
3915 * In the case of a schedule tree input, we bail out if any of
3916 * the descendants of the current schedule node refer to outer
3917 * schedule nodes in any way.
3920 * To see if there is any shifted stride, we look at the differences
3921 * between the values of the current dimension in pairs of domains
3922 * for equal values of outer dimensions. These differences should be
3923 * of the form
3925 * m x + r
3927 * with "m" the stride and "r" a constant. Note that we cannot perform
3928 * this analysis on individual domains as the lower bound in each domain
3929 * may depend on parameters or outer dimensions and so the current dimension
3930 * itself may not have a fixed remainder on division by the stride.
3932 * In particular, we compare the first domain that does not have an
3933 * obviously fixed value for the current dimension to itself and all
3934 * other domains and collect the offsets and the gcd of the strides.
3935 * If the gcd becomes one, then we failed to find shifted strides.
3936 * If the gcd is zero, then the differences were all fixed, meaning
3937 * that some domains had non-obviously fixed values for the current dimension.
3938 * If all the offsets are the same (for those domains that do not have
3939 * an obviously fixed value for the current dimension), then we do not
3940 * apply the transformation.
3941 * If none of the domains were skipped, then there is nothing to do.
3942 * If some of them were skipped, then if we apply separation, the schedule
3943 * domain should get split in pieces with a (non-shifted) stride.
3945 * Otherwise, we apply a shift to expose the stride in
3946 * generate_shift_component.
3948 static __isl_give isl_ast_graft_list *generate_component(
3949 struct isl_set_map_pair *domain, int *order, int n,
3950 __isl_take isl_ast_build *build)
3952 int i, d;
3953 int depth;
3954 isl_ctx *ctx;
3955 isl_map *map;
3956 isl_set *deltas;
3957 isl_val *gcd = NULL;
3958 isl_multi_val *mv;
3959 int fixed, skip;
3960 int base;
3961 isl_ast_graft_list *list;
3962 int res = 0;
3964 depth = isl_ast_build_get_depth(build);
3966 skip = n == 1;
3967 if (skip >= 0 && !skip)
3968 skip = at_most_one_non_fixed(domain, order, n, depth);
3969 if (skip >= 0 && !skip) {
3970 if (isl_ast_build_has_schedule_node(build))
3971 skip = has_anchored_subtree(build);
3972 else
3973 skip = isl_ast_build_options_involve_depth(build);
3975 if (skip < 0)
3976 goto error;
3977 if (skip)
3978 return generate_shifted_component_from_list(domain,
3979 order, n, build);
3981 base = eliminate_non_fixed(domain, order, n, depth, build);
3982 if (base < 0)
3983 goto error;
3985 ctx = isl_ast_build_get_ctx(build);
3987 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3989 fixed = 1;
3990 for (i = 0; i < n; ++i) {
3991 isl_val *r, *m;
3993 map = isl_map_from_domain_and_range(
3994 isl_set_copy(domain[order[base]].set),
3995 isl_set_copy(domain[order[i]].set));
3996 for (d = 0; d < depth; ++d)
3997 map = isl_map_equate(map, isl_dim_in, d,
3998 isl_dim_out, d);
3999 deltas = isl_map_deltas(map);
4000 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4001 isl_set_free(deltas);
4002 if (res < 0)
4003 break;
4005 if (i == 0)
4006 gcd = m;
4007 else
4008 gcd = isl_val_gcd(gcd, m);
4009 if (isl_val_is_one(gcd)) {
4010 isl_val_free(r);
4011 break;
4013 mv = isl_multi_val_set_val(mv, i, r);
4015 res = dim_is_fixed(domain[order[i]].set, depth);
4016 if (res < 0)
4017 break;
4018 if (res)
4019 continue;
4021 if (fixed && i > base) {
4022 isl_val *a, *b;
4023 a = isl_multi_val_get_val(mv, i);
4024 b = isl_multi_val_get_val(mv, base);
4025 if (isl_val_ne(a, b))
4026 fixed = 0;
4027 isl_val_free(a);
4028 isl_val_free(b);
4032 if (res < 0 || !gcd) {
4033 isl_ast_build_free(build);
4034 list = NULL;
4035 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4036 list = generate_shifted_component_from_list(domain,
4037 order, n, build);
4038 } else {
4039 list = generate_shift_component(domain, order, n, gcd, mv,
4040 build);
4043 isl_val_free(gcd);
4044 isl_multi_val_free(mv);
4046 return list;
4047 error:
4048 isl_ast_build_free(build);
4049 return NULL;
4052 /* Store both "map" itself and its domain in the
4053 * structure pointed to by *next and advance to the next array element.
4055 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4057 struct isl_set_map_pair **next = user;
4059 (*next)->map = isl_map_copy(map);
4060 (*next)->set = isl_map_domain(map);
4061 (*next)++;
4063 return isl_stat_ok;
4066 static int after_in_tree(__isl_keep isl_union_map *umap,
4067 __isl_keep isl_schedule_node *node);
4069 /* Is any domain element of "umap" scheduled after any of
4070 * the corresponding image elements by the tree rooted at
4071 * the child of "node"?
4073 static int after_in_child(__isl_keep isl_union_map *umap,
4074 __isl_keep isl_schedule_node *node)
4076 isl_schedule_node *child;
4077 int after;
4079 child = isl_schedule_node_get_child(node, 0);
4080 after = after_in_tree(umap, child);
4081 isl_schedule_node_free(child);
4083 return after;
4086 /* Is any domain element of "umap" scheduled after any of
4087 * the corresponding image elements by the tree rooted at
4088 * the band node "node"?
4090 * We first check if any domain element is scheduled after any
4091 * of the corresponding image elements by the band node itself.
4092 * If not, we restrict "map" to those pairs of element that
4093 * are scheduled together by the band node and continue with
4094 * the child of the band node.
4095 * If there are no such pairs then the map passed to after_in_child
4096 * will be empty causing it to return 0.
4098 static int after_in_band(__isl_keep isl_union_map *umap,
4099 __isl_keep isl_schedule_node *node)
4101 isl_multi_union_pw_aff *mupa;
4102 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4103 isl_union_set *domain, *range;
4104 isl_space *space;
4105 int empty;
4106 int after;
4108 if (isl_schedule_node_band_n_member(node) == 0)
4109 return after_in_child(umap, node);
4111 mupa = isl_schedule_node_band_get_partial_schedule(node);
4112 space = isl_multi_union_pw_aff_get_space(mupa);
4113 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4114 test = isl_union_map_copy(umap);
4115 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4116 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4117 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4118 test = isl_union_map_intersect(test, gt);
4119 empty = isl_union_map_is_empty(test);
4120 isl_union_map_free(test);
4122 if (empty < 0 || !empty) {
4123 isl_union_map_free(partial);
4124 return empty < 0 ? -1 : 1;
4127 universe = isl_union_map_universe(isl_union_map_copy(umap));
4128 domain = isl_union_map_domain(isl_union_map_copy(universe));
4129 range = isl_union_map_range(universe);
4130 umap1 = isl_union_map_copy(partial);
4131 umap1 = isl_union_map_intersect_domain(umap1, domain);
4132 umap2 = isl_union_map_intersect_domain(partial, range);
4133 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4134 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4135 after = after_in_child(test, node);
4136 isl_union_map_free(test);
4137 return after;
4140 /* Is any domain element of "umap" scheduled after any of
4141 * the corresponding image elements by the tree rooted at
4142 * the context node "node"?
4144 * The context constraints apply to the schedule domain,
4145 * so we cannot apply them directly to "umap", which contains
4146 * pairs of statement instances. Instead, we add them
4147 * to the range of the prefix schedule for both domain and
4148 * range of "umap".
4150 static int after_in_context(__isl_keep isl_union_map *umap,
4151 __isl_keep isl_schedule_node *node)
4153 isl_union_map *prefix, *universe, *umap1, *umap2;
4154 isl_union_set *domain, *range;
4155 isl_set *context;
4156 int after;
4158 umap = isl_union_map_copy(umap);
4159 context = isl_schedule_node_context_get_context(node);
4160 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4161 universe = isl_union_map_universe(isl_union_map_copy(umap));
4162 domain = isl_union_map_domain(isl_union_map_copy(universe));
4163 range = isl_union_map_range(universe);
4164 umap1 = isl_union_map_copy(prefix);
4165 umap1 = isl_union_map_intersect_domain(umap1, domain);
4166 umap2 = isl_union_map_intersect_domain(prefix, range);
4167 umap1 = isl_union_map_intersect_range(umap1,
4168 isl_union_set_from_set(context));
4169 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4170 umap = isl_union_map_intersect(umap, umap1);
4172 after = after_in_child(umap, node);
4174 isl_union_map_free(umap);
4176 return after;
4179 /* Is any domain element of "umap" scheduled after any of
4180 * the corresponding image elements by the tree rooted at
4181 * the expansion node "node"?
4183 * We apply the expansion to domain and range of "umap" and
4184 * continue with its child.
4186 static int after_in_expansion(__isl_keep isl_union_map *umap,
4187 __isl_keep isl_schedule_node *node)
4189 isl_union_map *expansion;
4190 int after;
4192 expansion = isl_schedule_node_expansion_get_expansion(node);
4193 umap = isl_union_map_copy(umap);
4194 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4195 umap = isl_union_map_apply_range(umap, expansion);
4197 after = after_in_child(umap, node);
4199 isl_union_map_free(umap);
4201 return after;
4204 /* Is any domain element of "umap" scheduled after any of
4205 * the corresponding image elements by the tree rooted at
4206 * the extension node "node"?
4208 * Since the extension node may add statement instances before or
4209 * after the pairs of statement instances in "umap", we return 1
4210 * to ensure that these pairs are not broken up.
4212 static int after_in_extension(__isl_keep isl_union_map *umap,
4213 __isl_keep isl_schedule_node *node)
4215 return 1;
4218 /* Is any domain element of "umap" scheduled after any of
4219 * the corresponding image elements by the tree rooted at
4220 * the filter node "node"?
4222 * We intersect domain and range of "umap" with the filter and
4223 * continue with its child.
4225 static int after_in_filter(__isl_keep isl_union_map *umap,
4226 __isl_keep isl_schedule_node *node)
4228 isl_union_set *filter;
4229 int after;
4231 umap = isl_union_map_copy(umap);
4232 filter = isl_schedule_node_filter_get_filter(node);
4233 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4234 umap = isl_union_map_intersect_range(umap, filter);
4236 after = after_in_child(umap, node);
4238 isl_union_map_free(umap);
4240 return after;
4243 /* Is any domain element of "umap" scheduled after any of
4244 * the corresponding image elements by the tree rooted at
4245 * the set node "node"?
4247 * This is only the case if this condition holds in any
4248 * of the (filter) children of the set node.
4249 * In particular, if the domain and the range of "umap"
4250 * are contained in different children, then the condition
4251 * does not hold.
4253 static int after_in_set(__isl_keep isl_union_map *umap,
4254 __isl_keep isl_schedule_node *node)
4256 int i, n;
4258 n = isl_schedule_node_n_children(node);
4259 for (i = 0; i < n; ++i) {
4260 isl_schedule_node *child;
4261 int after;
4263 child = isl_schedule_node_get_child(node, i);
4264 after = after_in_tree(umap, child);
4265 isl_schedule_node_free(child);
4267 if (after < 0 || after)
4268 return after;
4271 return 0;
4274 /* Return the filter of child "i" of "node".
4276 static __isl_give isl_union_set *child_filter(
4277 __isl_keep isl_schedule_node *node, int i)
4279 isl_schedule_node *child;
4280 isl_union_set *filter;
4282 child = isl_schedule_node_get_child(node, i);
4283 filter = isl_schedule_node_filter_get_filter(child);
4284 isl_schedule_node_free(child);
4286 return filter;
4289 /* Is any domain element of "umap" scheduled after any of
4290 * the corresponding image elements by the tree rooted at
4291 * the sequence node "node"?
4293 * This happens in particular if any domain element is
4294 * contained in a later child than one containing a range element or
4295 * if the condition holds within a given child in the sequence.
4296 * The later part of the condition is checked by after_in_set.
4298 static int after_in_sequence(__isl_keep isl_union_map *umap,
4299 __isl_keep isl_schedule_node *node)
4301 int i, j, n;
4302 isl_union_map *umap_i;
4303 int empty, after = 0;
4305 n = isl_schedule_node_n_children(node);
4306 for (i = 1; i < n; ++i) {
4307 isl_union_set *filter_i;
4309 umap_i = isl_union_map_copy(umap);
4310 filter_i = child_filter(node, i);
4311 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4312 empty = isl_union_map_is_empty(umap_i);
4313 if (empty < 0)
4314 goto error;
4315 if (empty) {
4316 isl_union_map_free(umap_i);
4317 continue;
4320 for (j = 0; j < i; ++j) {
4321 isl_union_set *filter_j;
4322 isl_union_map *umap_ij;
4324 umap_ij = isl_union_map_copy(umap_i);
4325 filter_j = child_filter(node, j);
4326 umap_ij = isl_union_map_intersect_range(umap_ij,
4327 filter_j);
4328 empty = isl_union_map_is_empty(umap_ij);
4329 isl_union_map_free(umap_ij);
4331 if (empty < 0)
4332 goto error;
4333 if (!empty)
4334 after = 1;
4335 if (after)
4336 break;
4339 isl_union_map_free(umap_i);
4340 if (after)
4341 break;
4344 if (after < 0 || after)
4345 return after;
4347 return after_in_set(umap, node);
4348 error:
4349 isl_union_map_free(umap_i);
4350 return -1;
4353 /* Is any domain element of "umap" scheduled after any of
4354 * the corresponding image elements by the tree rooted at "node"?
4356 * If "umap" is empty, then clearly there is no such element.
4357 * Otherwise, consider the different types of nodes separately.
4359 static int after_in_tree(__isl_keep isl_union_map *umap,
4360 __isl_keep isl_schedule_node *node)
4362 int empty;
4363 enum isl_schedule_node_type type;
4365 empty = isl_union_map_is_empty(umap);
4366 if (empty < 0)
4367 return -1;
4368 if (empty)
4369 return 0;
4370 if (!node)
4371 return -1;
4373 type = isl_schedule_node_get_type(node);
4374 switch (type) {
4375 case isl_schedule_node_error:
4376 return -1;
4377 case isl_schedule_node_leaf:
4378 return 0;
4379 case isl_schedule_node_band:
4380 return after_in_band(umap, node);
4381 case isl_schedule_node_domain:
4382 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4383 "unexpected internal domain node", return -1);
4384 case isl_schedule_node_context:
4385 return after_in_context(umap, node);
4386 case isl_schedule_node_expansion:
4387 return after_in_expansion(umap, node);
4388 case isl_schedule_node_extension:
4389 return after_in_extension(umap, node);
4390 case isl_schedule_node_filter:
4391 return after_in_filter(umap, node);
4392 case isl_schedule_node_guard:
4393 case isl_schedule_node_mark:
4394 return after_in_child(umap, node);
4395 case isl_schedule_node_set:
4396 return after_in_set(umap, node);
4397 case isl_schedule_node_sequence:
4398 return after_in_sequence(umap, node);
4401 return 1;
4404 /* Is any domain element of "map1" scheduled after any domain
4405 * element of "map2" by the subtree underneath the current band node,
4406 * while at the same time being scheduled together by the current
4407 * band node, i.e., by "map1" and "map2?
4409 * If the child of the current band node is a leaf, then
4410 * no element can be scheduled after any other element.
4412 * Otherwise, we construct a relation between domain elements
4413 * of "map1" and domain elements of "map2" that are scheduled
4414 * together and then check if the subtree underneath the current
4415 * band node determines their relative order.
4417 static int after_in_subtree(__isl_keep isl_ast_build *build,
4418 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4420 isl_schedule_node *node;
4421 isl_map *map;
4422 isl_union_map *umap;
4423 int after;
4425 node = isl_ast_build_get_schedule_node(build);
4426 if (!node)
4427 return -1;
4428 node = isl_schedule_node_child(node, 0);
4429 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4430 isl_schedule_node_free(node);
4431 return 0;
4433 map = isl_map_copy(map2);
4434 map = isl_map_apply_domain(map, isl_map_copy(map1));
4435 umap = isl_union_map_from_map(map);
4436 after = after_in_tree(umap, node);
4437 isl_union_map_free(umap);
4438 isl_schedule_node_free(node);
4439 return after;
4442 /* Internal data for any_scheduled_after.
4444 * "build" is the build in which the AST is constructed.
4445 * "depth" is the number of loops that have already been generated
4446 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4447 * "domain" is an array of set-map pairs corresponding to the different
4448 * iteration domains. The set is the schedule domain, i.e., the domain
4449 * of the inverse schedule, while the map is the inverse schedule itself.
4451 struct isl_any_scheduled_after_data {
4452 isl_ast_build *build;
4453 int depth;
4454 int group_coscheduled;
4455 struct isl_set_map_pair *domain;
4458 /* Is any element of domain "i" scheduled after any element of domain "j"
4459 * (for a common iteration of the first data->depth loops)?
4461 * data->domain[i].set contains the domain of the inverse schedule
4462 * for domain "i", i.e., elements in the schedule domain.
4464 * If we are inside a band of a schedule tree and there is a pair
4465 * of elements in the two domains that is schedule together by
4466 * the current band, then we check if any element of "i" may be schedule
4467 * after element of "j" by the descendants of the band node.
4469 * If data->group_coscheduled is set, then we also return 1 if there
4470 * is any pair of elements in the two domains that are scheduled together.
4472 static isl_bool any_scheduled_after(int i, int j, void *user)
4474 struct isl_any_scheduled_after_data *data = user;
4475 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4476 int pos;
4478 for (pos = data->depth; pos < dim; ++pos) {
4479 int follows;
4481 follows = isl_set_follows_at(data->domain[i].set,
4482 data->domain[j].set, pos);
4484 if (follows < -1)
4485 return isl_bool_error;
4486 if (follows > 0)
4487 return isl_bool_true;
4488 if (follows < 0)
4489 return isl_bool_false;
4492 if (isl_ast_build_has_schedule_node(data->build)) {
4493 int after;
4495 after = after_in_subtree(data->build, data->domain[i].map,
4496 data->domain[j].map);
4497 if (after < 0 || after)
4498 return after;
4501 return data->group_coscheduled;
4504 /* Look for independent components at the current depth and generate code
4505 * for each component separately. The resulting lists of grafts are
4506 * merged in an attempt to combine grafts with identical guards.
4508 * Code for two domains can be generated separately if all the elements
4509 * of one domain are scheduled before (or together with) all the elements
4510 * of the other domain. We therefore consider the graph with as nodes
4511 * the domains and an edge between two nodes if any element of the first
4512 * node is scheduled after any element of the second node.
4513 * If the ast_build_group_coscheduled is set, then we also add an edge if
4514 * there is any pair of elements in the two domains that are scheduled
4515 * together.
4516 * Code is then generated (by generate_component)
4517 * for each of the strongly connected components in this graph
4518 * in their topological order.
4520 * Since the test is performed on the domain of the inverse schedules of
4521 * the different domains, we precompute these domains and store
4522 * them in data.domain.
4524 static __isl_give isl_ast_graft_list *generate_components(
4525 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4527 int i;
4528 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4529 int n = isl_union_map_n_map(executed);
4530 struct isl_any_scheduled_after_data data;
4531 struct isl_set_map_pair *next;
4532 struct isl_tarjan_graph *g = NULL;
4533 isl_ast_graft_list *list = NULL;
4534 int n_domain = 0;
4536 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4537 if (!data.domain)
4538 goto error;
4539 n_domain = n;
4541 next = data.domain;
4542 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4543 goto error;
4545 if (!build)
4546 goto error;
4547 data.build = build;
4548 data.depth = isl_ast_build_get_depth(build);
4549 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4550 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4551 if (!g)
4552 goto error;
4554 list = isl_ast_graft_list_alloc(ctx, 0);
4556 i = 0;
4557 while (list && n) {
4558 isl_ast_graft_list *list_c;
4559 int first = i;
4561 if (g->order[i] == -1)
4562 isl_die(ctx, isl_error_internal, "cannot happen",
4563 goto error);
4564 ++i; --n;
4565 while (g->order[i] != -1) {
4566 ++i; --n;
4569 list_c = generate_component(data.domain,
4570 g->order + first, i - first,
4571 isl_ast_build_copy(build));
4572 list = isl_ast_graft_list_merge(list, list_c, build);
4574 ++i;
4577 if (0)
4578 error: list = isl_ast_graft_list_free(list);
4579 isl_tarjan_graph_free(g);
4580 for (i = 0; i < n_domain; ++i) {
4581 isl_map_free(data.domain[i].map);
4582 isl_set_free(data.domain[i].set);
4584 free(data.domain);
4585 isl_union_map_free(executed);
4586 isl_ast_build_free(build);
4588 return list;
4591 /* Generate code for the next level (and all inner levels).
4593 * If "executed" is empty, i.e., no code needs to be generated,
4594 * then we return an empty list.
4596 * If we have already generated code for all loop levels, then we pass
4597 * control to generate_inner_level.
4599 * If "executed" lives in a single space, i.e., if code needs to be
4600 * generated for a single domain, then there can only be a single
4601 * component and we go directly to generate_shifted_component.
4602 * Otherwise, we call generate_components to detect the components
4603 * and to call generate_component on each of them separately.
4605 static __isl_give isl_ast_graft_list *generate_next_level(
4606 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4608 int depth;
4610 if (!build || !executed)
4611 goto error;
4613 if (isl_union_map_is_empty(executed)) {
4614 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4615 isl_union_map_free(executed);
4616 isl_ast_build_free(build);
4617 return isl_ast_graft_list_alloc(ctx, 0);
4620 depth = isl_ast_build_get_depth(build);
4621 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4622 return generate_inner_level(executed, build);
4624 if (isl_union_map_n_map(executed) == 1)
4625 return generate_shifted_component(executed, build);
4627 return generate_components(executed, build);
4628 error:
4629 isl_union_map_free(executed);
4630 isl_ast_build_free(build);
4631 return NULL;
4634 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4635 * internal, executed and build are the inputs to generate_code.
4636 * list collects the output.
4638 struct isl_generate_code_data {
4639 int internal;
4640 isl_union_map *executed;
4641 isl_ast_build *build;
4643 isl_ast_graft_list *list;
4646 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4648 * [E -> S] -> D
4650 * with E the external build schedule and S the additional schedule "space",
4651 * reformulate the inverse schedule in terms of the internal schedule domain,
4652 * i.e., return
4654 * [I -> S] -> D
4656 * We first obtain a mapping
4658 * I -> E
4660 * take the inverse and the product with S -> S, resulting in
4662 * [I -> S] -> [E -> S]
4664 * Applying the map to the input produces the desired result.
4666 static __isl_give isl_union_map *internal_executed(
4667 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4668 __isl_keep isl_ast_build *build)
4670 isl_map *id, *proj;
4672 proj = isl_ast_build_get_schedule_map(build);
4673 proj = isl_map_reverse(proj);
4674 space = isl_space_map_from_set(isl_space_copy(space));
4675 id = isl_map_identity(space);
4676 proj = isl_map_product(proj, id);
4677 executed = isl_union_map_apply_domain(executed,
4678 isl_union_map_from_map(proj));
4679 return executed;
4682 /* Generate an AST that visits the elements in the range of data->executed
4683 * in the relative order specified by the corresponding domain element(s)
4684 * for those domain elements that belong to "set".
4685 * Add the result to data->list.
4687 * The caller ensures that "set" is a universe domain.
4688 * "space" is the space of the additional part of the schedule.
4689 * It is equal to the space of "set" if build->domain is parametric.
4690 * Otherwise, it is equal to the range of the wrapped space of "set".
4692 * If the build space is not parametric and
4693 * if isl_ast_build_node_from_schedule_map
4694 * was called from an outside user (data->internal not set), then
4695 * the (inverse) schedule refers to the external build domain and needs to
4696 * be transformed to refer to the internal build domain.
4698 * If the build space is parametric, then we add some of the parameter
4699 * constraints to the executed relation. Adding these constraints
4700 * allows for an earlier detection of conflicts in some cases.
4701 * However, we do not want to divide the executed relation into
4702 * more disjuncts than necessary. We therefore approximate
4703 * the constraints on the parameters by a single disjunct set.
4705 * The build is extended to include the additional part of the schedule.
4706 * If the original build space was not parametric, then the options
4707 * in data->build refer only to the additional part of the schedule
4708 * and they need to be adjusted to refer to the complete AST build
4709 * domain.
4711 * After having adjusted inverse schedule and build, we start generating
4712 * code with the outer loop of the current code generation
4713 * in generate_next_level.
4715 * If the original build space was not parametric, we undo the embedding
4716 * on the resulting isl_ast_node_list so that it can be used within
4717 * the outer AST build.
4719 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4720 __isl_take isl_set *set, __isl_take isl_space *space)
4722 isl_union_map *executed;
4723 isl_ast_build *build;
4724 isl_ast_graft_list *list;
4725 int embed;
4727 executed = isl_union_map_copy(data->executed);
4728 executed = isl_union_map_intersect_domain(executed,
4729 isl_union_set_from_set(set));
4731 embed = !isl_set_is_params(data->build->domain);
4732 if (embed && !data->internal)
4733 executed = internal_executed(executed, space, data->build);
4734 if (!embed) {
4735 isl_set *domain;
4736 domain = isl_ast_build_get_domain(data->build);
4737 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4738 executed = isl_union_map_intersect_params(executed, domain);
4741 build = isl_ast_build_copy(data->build);
4742 build = isl_ast_build_product(build, space);
4744 list = generate_next_level(executed, build);
4746 list = isl_ast_graft_list_unembed(list, embed);
4748 data->list = isl_ast_graft_list_concat(data->list, list);
4750 return isl_stat_ok;
4753 /* Generate an AST that visits the elements in the range of data->executed
4754 * in the relative order specified by the corresponding domain element(s)
4755 * for those domain elements that belong to "set".
4756 * Add the result to data->list.
4758 * The caller ensures that "set" is a universe domain.
4760 * If the build space S is not parametric, then the space of "set"
4761 * need to be a wrapped relation with S as domain. That is, it needs
4762 * to be of the form
4764 * [S -> T]
4766 * Check this property and pass control to generate_code_in_space
4767 * passing along T.
4768 * If the build space is not parametric, then T is the space of "set".
4770 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4772 struct isl_generate_code_data *data = user;
4773 isl_space *space, *build_space;
4774 int is_domain;
4776 space = isl_set_get_space(set);
4778 if (isl_set_is_params(data->build->domain))
4779 return generate_code_in_space(data, set, space);
4781 build_space = isl_ast_build_get_space(data->build, data->internal);
4782 space = isl_space_unwrap(space);
4783 is_domain = isl_space_is_domain(build_space, space);
4784 isl_space_free(build_space);
4785 space = isl_space_range(space);
4787 if (is_domain < 0)
4788 goto error;
4789 if (!is_domain)
4790 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4791 "invalid nested schedule space", goto error);
4793 return generate_code_in_space(data, set, space);
4794 error:
4795 isl_set_free(set);
4796 isl_space_free(space);
4797 return isl_stat_error;
4800 /* Generate an AST that visits the elements in the range of "executed"
4801 * in the relative order specified by the corresponding domain element(s).
4803 * "build" is an isl_ast_build that has either been constructed by
4804 * isl_ast_build_from_context or passed to a callback set by
4805 * isl_ast_build_set_create_leaf.
4806 * In the first case, the space of the isl_ast_build is typically
4807 * a parametric space, although this is currently not enforced.
4808 * In the second case, the space is never a parametric space.
4809 * If the space S is not parametric, then the domain space(s) of "executed"
4810 * need to be wrapped relations with S as domain.
4812 * If the domain of "executed" consists of several spaces, then an AST
4813 * is generated for each of them (in arbitrary order) and the results
4814 * are concatenated.
4816 * If "internal" is set, then the domain "S" above refers to the internal
4817 * schedule domain representation. Otherwise, it refers to the external
4818 * representation, as returned by isl_ast_build_get_schedule_space.
4820 * We essentially run over all the spaces in the domain of "executed"
4821 * and call generate_code_set on each of them.
4823 static __isl_give isl_ast_graft_list *generate_code(
4824 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4825 int internal)
4827 isl_ctx *ctx;
4828 struct isl_generate_code_data data = { 0 };
4829 isl_space *space;
4830 isl_union_set *schedule_domain;
4831 isl_union_map *universe;
4833 if (!build)
4834 goto error;
4835 space = isl_ast_build_get_space(build, 1);
4836 space = isl_space_align_params(space,
4837 isl_union_map_get_space(executed));
4838 space = isl_space_align_params(space,
4839 isl_union_map_get_space(build->options));
4840 build = isl_ast_build_align_params(build, isl_space_copy(space));
4841 executed = isl_union_map_align_params(executed, space);
4842 if (!executed || !build)
4843 goto error;
4845 ctx = isl_ast_build_get_ctx(build);
4847 data.internal = internal;
4848 data.executed = executed;
4849 data.build = build;
4850 data.list = isl_ast_graft_list_alloc(ctx, 0);
4852 universe = isl_union_map_universe(isl_union_map_copy(executed));
4853 schedule_domain = isl_union_map_domain(universe);
4854 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
4855 &data) < 0)
4856 data.list = isl_ast_graft_list_free(data.list);
4858 isl_union_set_free(schedule_domain);
4859 isl_union_map_free(executed);
4861 isl_ast_build_free(build);
4862 return data.list;
4863 error:
4864 isl_union_map_free(executed);
4865 isl_ast_build_free(build);
4866 return NULL;
4869 /* Generate an AST that visits the elements in the domain of "schedule"
4870 * in the relative order specified by the corresponding image element(s).
4872 * "build" is an isl_ast_build that has either been constructed by
4873 * isl_ast_build_from_context or passed to a callback set by
4874 * isl_ast_build_set_create_leaf.
4875 * In the first case, the space of the isl_ast_build is typically
4876 * a parametric space, although this is currently not enforced.
4877 * In the second case, the space is never a parametric space.
4878 * If the space S is not parametric, then the range space(s) of "schedule"
4879 * need to be wrapped relations with S as domain.
4881 * If the range of "schedule" consists of several spaces, then an AST
4882 * is generated for each of them (in arbitrary order) and the results
4883 * are concatenated.
4885 * We first initialize the local copies of the relevant options.
4886 * We do this here rather than when the isl_ast_build is created
4887 * because the options may have changed between the construction
4888 * of the isl_ast_build and the call to isl_generate_code.
4890 * The main computation is performed on an inverse schedule (with
4891 * the schedule domain in the domain and the elements to be executed
4892 * in the range) called "executed".
4894 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
4895 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4897 isl_ast_graft_list *list;
4898 isl_ast_node *node;
4899 isl_union_map *executed;
4901 build = isl_ast_build_copy(build);
4902 build = isl_ast_build_set_single_valued(build, 0);
4903 schedule = isl_union_map_coalesce(schedule);
4904 schedule = isl_union_map_remove_redundancies(schedule);
4905 executed = isl_union_map_reverse(schedule);
4906 list = generate_code(executed, isl_ast_build_copy(build), 0);
4907 node = isl_ast_node_from_graft_list(list, build);
4908 isl_ast_build_free(build);
4910 return node;
4913 /* The old name for isl_ast_build_node_from_schedule_map.
4914 * It is being kept for backward compatibility, but
4915 * it will be removed in the future.
4917 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
4918 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4920 return isl_ast_build_node_from_schedule_map(build, schedule);
4923 /* Generate an AST that visits the elements in the domain of "executed"
4924 * in the relative order specified by the band node "node" and its descendants.
4926 * The relation "executed" maps the outer generated loop iterators
4927 * to the domain elements executed by those iterations.
4929 * If the band is empty, we continue with its descendants.
4930 * Otherwise, we extend the build and the inverse schedule with
4931 * the additional space/partial schedule and continue generating
4932 * an AST in generate_next_level.
4933 * As soon as we have extended the inverse schedule with the additional
4934 * partial schedule, we look for equalities that may exists between
4935 * the old and the new part.
4937 static __isl_give isl_ast_graft_list *build_ast_from_band(
4938 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4939 __isl_take isl_union_map *executed)
4941 isl_space *space;
4942 isl_multi_union_pw_aff *extra;
4943 isl_union_map *extra_umap;
4944 isl_ast_graft_list *list;
4945 unsigned n1, n2;
4947 if (!build || !node || !executed)
4948 goto error;
4950 if (isl_schedule_node_band_n_member(node) == 0)
4951 return build_ast_from_child(build, node, executed);
4953 extra = isl_schedule_node_band_get_partial_schedule(node);
4954 extra = isl_multi_union_pw_aff_align_params(extra,
4955 isl_ast_build_get_space(build, 1));
4956 space = isl_multi_union_pw_aff_get_space(extra);
4958 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
4959 extra_umap = isl_union_map_reverse(extra_umap);
4961 executed = isl_union_map_domain_product(executed, extra_umap);
4962 executed = isl_union_map_detect_equalities(executed);
4964 n1 = isl_ast_build_dim(build, isl_dim_param);
4965 build = isl_ast_build_product(build, space);
4966 n2 = isl_ast_build_dim(build, isl_dim_param);
4967 if (n2 > n1)
4968 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
4969 "band node is not allowed to introduce new parameters",
4970 build = isl_ast_build_free(build));
4971 build = isl_ast_build_set_schedule_node(build, node);
4973 list = generate_next_level(executed, build);
4975 list = isl_ast_graft_list_unembed(list, 1);
4977 return list;
4978 error:
4979 isl_schedule_node_free(node);
4980 isl_union_map_free(executed);
4981 isl_ast_build_free(build);
4982 return NULL;
4985 /* Hoist a list of grafts (in practice containing a single graft)
4986 * from "sub_build" (which includes extra context information)
4987 * to "build".
4989 * In particular, project out all additional parameters introduced
4990 * by the context node from the enforced constraints and the guard
4991 * of the single graft.
4993 static __isl_give isl_ast_graft_list *hoist_out_of_context(
4994 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
4995 __isl_keep isl_ast_build *sub_build)
4997 isl_ast_graft *graft;
4998 isl_basic_set *enforced;
4999 isl_set *guard;
5000 unsigned n_param, extra_param;
5002 if (!build || !sub_build)
5003 return isl_ast_graft_list_free(list);
5005 n_param = isl_ast_build_dim(build, isl_dim_param);
5006 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5008 if (extra_param == n_param)
5009 return list;
5011 extra_param -= n_param;
5012 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5013 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5014 n_param, extra_param);
5015 enforced = isl_basic_set_remove_unknown_divs(enforced);
5016 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5017 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5018 n_param, extra_param);
5019 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5020 guard = isl_set_compute_divs(guard);
5021 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5022 build, sub_build);
5023 list = isl_ast_graft_list_from_ast_graft(graft);
5025 return list;
5028 /* Generate an AST that visits the elements in the domain of "executed"
5029 * in the relative order specified by the context node "node"
5030 * and its descendants.
5032 * The relation "executed" maps the outer generated loop iterators
5033 * to the domain elements executed by those iterations.
5035 * The context node may introduce additional parameters as well as
5036 * constraints on the outer schedule dimenions or original parameters.
5038 * We add the extra parameters to a new build and the context
5039 * constraints to both the build and (as a single disjunct)
5040 * to the domain of "executed". Since the context constraints
5041 * are specified in terms of the input schedule, we first need
5042 * to map them to the internal schedule domain.
5044 * After constructing the AST from the descendants of "node",
5045 * we combine the list of grafts into a single graft within
5046 * the new build, in order to be able to exploit the additional
5047 * context constraints during this combination.
5049 * Additionally, if the current node is the outermost node in
5050 * the schedule tree (apart from the root domain node), we generate
5051 * all pending guards, again to be able to exploit the additional
5052 * context constraints. We currently do not do this for internal
5053 * context nodes since we may still want to hoist conditions
5054 * to outer AST nodes.
5056 * If the context node introduced any new parameters, then they
5057 * are removed from the set of enforced constraints and guard
5058 * in hoist_out_of_context.
5060 static __isl_give isl_ast_graft_list *build_ast_from_context(
5061 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5062 __isl_take isl_union_map *executed)
5064 isl_set *context;
5065 isl_space *space;
5066 isl_multi_aff *internal2input;
5067 isl_ast_build *sub_build;
5068 isl_ast_graft_list *list;
5069 int n, depth;
5071 depth = isl_schedule_node_get_tree_depth(node);
5072 space = isl_ast_build_get_space(build, 1);
5073 context = isl_schedule_node_context_get_context(node);
5074 context = isl_set_align_params(context, space);
5075 sub_build = isl_ast_build_copy(build);
5076 space = isl_set_get_space(context);
5077 sub_build = isl_ast_build_align_params(sub_build, space);
5078 internal2input = isl_ast_build_get_internal2input(sub_build);
5079 context = isl_set_preimage_multi_aff(context, internal2input);
5080 sub_build = isl_ast_build_restrict_generated(sub_build,
5081 isl_set_copy(context));
5082 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5083 executed = isl_union_map_intersect_domain(executed,
5084 isl_union_set_from_set(context));
5086 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5087 node, executed);
5088 n = isl_ast_graft_list_n_ast_graft(list);
5089 if (n < 0)
5090 list = isl_ast_graft_list_free(list);
5092 list = isl_ast_graft_list_fuse(list, sub_build);
5093 if (depth == 1)
5094 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5095 sub_build);
5096 if (n >= 1)
5097 list = hoist_out_of_context(list, build, sub_build);
5099 isl_ast_build_free(build);
5100 isl_ast_build_free(sub_build);
5102 return list;
5105 /* Generate an AST that visits the elements in the domain of "executed"
5106 * in the relative order specified by the expansion node "node" and
5107 * its descendants.
5109 * The relation "executed" maps the outer generated loop iterators
5110 * to the domain elements executed by those iterations.
5112 * We expand the domain elements by the expansion and
5113 * continue with the descendants of the node.
5115 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5116 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5117 __isl_take isl_union_map *executed)
5119 isl_union_map *expansion;
5120 unsigned n1, n2;
5122 expansion = isl_schedule_node_expansion_get_expansion(node);
5123 expansion = isl_union_map_align_params(expansion,
5124 isl_union_map_get_space(executed));
5126 n1 = isl_union_map_dim(executed, isl_dim_param);
5127 executed = isl_union_map_apply_range(executed, expansion);
5128 n2 = isl_union_map_dim(executed, isl_dim_param);
5129 if (n2 > n1)
5130 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5131 "expansion node is not allowed to introduce "
5132 "new parameters", goto error);
5134 return build_ast_from_child(build, node, executed);
5135 error:
5136 isl_ast_build_free(build);
5137 isl_schedule_node_free(node);
5138 isl_union_map_free(executed);
5139 return NULL;
5142 /* Generate an AST that visits the elements in the domain of "executed"
5143 * in the relative order specified by the extension node "node" and
5144 * its descendants.
5146 * The relation "executed" maps the outer generated loop iterators
5147 * to the domain elements executed by those iterations.
5149 * Extend the inverse schedule with the extension applied to current
5150 * set of generated constraints. Since the extension if formulated
5151 * in terms of the input schedule, it first needs to be transformed
5152 * to refer to the internal schedule.
5154 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5155 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5156 __isl_take isl_union_map *executed)
5158 isl_union_set *schedule_domain;
5159 isl_union_map *extension;
5160 isl_set *set;
5162 set = isl_ast_build_get_generated(build);
5163 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5164 schedule_domain = isl_union_set_from_set(set);
5166 extension = isl_schedule_node_extension_get_extension(node);
5168 extension = isl_union_map_preimage_domain_multi_aff(extension,
5169 isl_multi_aff_copy(build->internal2input));
5170 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5171 extension = isl_ast_build_substitute_values_union_map_domain(build,
5172 extension);
5173 executed = isl_union_map_union(executed, extension);
5175 return build_ast_from_child(build, node, executed);
5178 /* Generate an AST that visits the elements in the domain of "executed"
5179 * in the relative order specified by the filter node "node" and
5180 * its descendants.
5182 * The relation "executed" maps the outer generated loop iterators
5183 * to the domain elements executed by those iterations.
5185 * We simply intersect the iteration domain (i.e., the range of "executed")
5186 * with the filter and continue with the descendants of the node,
5187 * unless the resulting inverse schedule is empty, in which
5188 * case we return an empty list.
5190 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5191 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5192 __isl_take isl_union_map *executed)
5194 isl_ctx *ctx;
5195 isl_union_set *filter;
5196 isl_ast_graft_list *list;
5197 int empty;
5198 unsigned n1, n2;
5200 if (!build || !node || !executed)
5201 goto error;
5203 filter = isl_schedule_node_filter_get_filter(node);
5204 filter = isl_union_set_align_params(filter,
5205 isl_union_map_get_space(executed));
5206 n1 = isl_union_map_dim(executed, isl_dim_param);
5207 executed = isl_union_map_intersect_range(executed, filter);
5208 n2 = isl_union_map_dim(executed, isl_dim_param);
5209 if (n2 > n1)
5210 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5211 "filter node is not allowed to introduce "
5212 "new parameters", goto error);
5214 empty = isl_union_map_is_empty(executed);
5215 if (empty < 0)
5216 goto error;
5217 if (!empty)
5218 return build_ast_from_child(build, node, executed);
5220 ctx = isl_ast_build_get_ctx(build);
5221 list = isl_ast_graft_list_alloc(ctx, 0);
5222 isl_ast_build_free(build);
5223 isl_schedule_node_free(node);
5224 isl_union_map_free(executed);
5225 return list;
5226 error:
5227 isl_ast_build_free(build);
5228 isl_schedule_node_free(node);
5229 isl_union_map_free(executed);
5230 return NULL;
5233 /* Generate an AST that visits the elements in the domain of "executed"
5234 * in the relative order specified by the guard node "node" and
5235 * its descendants.
5237 * The relation "executed" maps the outer generated loop iterators
5238 * to the domain elements executed by those iterations.
5240 * Ensure that the associated guard is enforced by the outer AST
5241 * constructs by adding it to the guard of the graft.
5242 * Since we know that we will enforce the guard, we can also include it
5243 * in the generated constraints used to construct an AST for
5244 * the descendant nodes.
5246 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5247 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5248 __isl_take isl_union_map *executed)
5250 isl_space *space;
5251 isl_set *guard, *hoisted;
5252 isl_basic_set *enforced;
5253 isl_ast_build *sub_build;
5254 isl_ast_graft *graft;
5255 isl_ast_graft_list *list;
5256 unsigned n1, n2;
5258 space = isl_ast_build_get_space(build, 1);
5259 guard = isl_schedule_node_guard_get_guard(node);
5260 n1 = isl_space_dim(space, isl_dim_param);
5261 guard = isl_set_align_params(guard, space);
5262 n2 = isl_set_dim(guard, isl_dim_param);
5263 if (n2 > n1)
5264 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5265 "guard node is not allowed to introduce "
5266 "new parameters", guard = isl_set_free(guard));
5267 guard = isl_set_preimage_multi_aff(guard,
5268 isl_multi_aff_copy(build->internal2input));
5269 guard = isl_ast_build_specialize(build, guard);
5270 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5272 sub_build = isl_ast_build_copy(build);
5273 sub_build = isl_ast_build_restrict_generated(sub_build,
5274 isl_set_copy(guard));
5276 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5277 node, executed);
5279 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5280 if (isl_set_n_basic_set(hoisted) > 1)
5281 list = isl_ast_graft_list_gist_guards(list,
5282 isl_set_copy(hoisted));
5283 guard = isl_set_intersect(guard, hoisted);
5284 enforced = extract_shared_enforced(list, build);
5285 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5286 build, sub_build);
5288 isl_ast_build_free(sub_build);
5289 isl_ast_build_free(build);
5290 return isl_ast_graft_list_from_ast_graft(graft);
5293 /* Call the before_each_mark callback, if requested by the user.
5295 * Return 0 on success and -1 on error.
5297 * The caller is responsible for recording the current inverse schedule
5298 * in "build".
5300 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5301 __isl_keep isl_ast_build *build)
5303 if (!build)
5304 return isl_stat_error;
5305 if (!build->before_each_mark)
5306 return isl_stat_ok;
5307 return build->before_each_mark(mark, build,
5308 build->before_each_mark_user);
5311 /* Call the after_each_mark callback, if requested by the user.
5313 * The caller is responsible for recording the current inverse schedule
5314 * in "build".
5316 static __isl_give isl_ast_graft *after_each_mark(
5317 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5319 if (!graft || !build)
5320 return isl_ast_graft_free(graft);
5321 if (!build->after_each_mark)
5322 return graft;
5323 graft->node = build->after_each_mark(graft->node, build,
5324 build->after_each_mark_user);
5325 if (!graft->node)
5326 return isl_ast_graft_free(graft);
5327 return graft;
5331 /* Generate an AST that visits the elements in the domain of "executed"
5332 * in the relative order specified by the mark node "node" and
5333 * its descendants.
5335 * The relation "executed" maps the outer generated loop iterators
5336 * to the domain elements executed by those iterations.
5338 * Since we may be calling before_each_mark and after_each_mark
5339 * callbacks, we record the current inverse schedule in the build.
5341 * We generate an AST for the child of the mark node, combine
5342 * the graft list into a single graft and then insert the mark
5343 * in the AST of that single graft.
5345 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5346 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5347 __isl_take isl_union_map *executed)
5349 isl_id *mark;
5350 isl_ast_graft *graft;
5351 isl_ast_graft_list *list;
5352 int n;
5354 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5356 mark = isl_schedule_node_mark_get_id(node);
5357 if (before_each_mark(mark, build) < 0)
5358 node = isl_schedule_node_free(node);
5360 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5361 list = isl_ast_graft_list_fuse(list, build);
5362 n = isl_ast_graft_list_n_ast_graft(list);
5363 if (n < 0)
5364 list = isl_ast_graft_list_free(list);
5365 if (n == 0) {
5366 isl_id_free(mark);
5367 } else {
5368 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5369 graft = isl_ast_graft_insert_mark(graft, mark);
5370 graft = after_each_mark(graft, build);
5371 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5373 isl_ast_build_free(build);
5375 return list;
5378 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5379 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5380 __isl_take isl_union_map *executed);
5382 /* Generate an AST that visits the elements in the domain of "executed"
5383 * in the relative order specified by the sequence (or set) node "node" and
5384 * its descendants.
5386 * The relation "executed" maps the outer generated loop iterators
5387 * to the domain elements executed by those iterations.
5389 * We simply generate an AST for each of the children and concatenate
5390 * the results.
5392 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5393 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5394 __isl_take isl_union_map *executed)
5396 int i, n;
5397 isl_ctx *ctx;
5398 isl_ast_graft_list *list;
5400 ctx = isl_ast_build_get_ctx(build);
5401 list = isl_ast_graft_list_alloc(ctx, 0);
5403 n = isl_schedule_node_n_children(node);
5404 for (i = 0; i < n; ++i) {
5405 isl_schedule_node *child;
5406 isl_ast_graft_list *list_i;
5408 child = isl_schedule_node_get_child(node, i);
5409 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5410 child, isl_union_map_copy(executed));
5411 list = isl_ast_graft_list_concat(list, list_i);
5413 isl_ast_build_free(build);
5414 isl_schedule_node_free(node);
5415 isl_union_map_free(executed);
5417 return list;
5420 /* Generate an AST that visits the elements in the domain of "executed"
5421 * in the relative order specified by the node "node" and its descendants.
5423 * The relation "executed" maps the outer generated loop iterators
5424 * to the domain elements executed by those iterations.
5426 * If the node is a leaf, then we pass control to generate_inner_level.
5427 * Note that the current build does not refer to any band node, so
5428 * that generate_inner_level will not try to visit the child of
5429 * the leaf node.
5431 * The other node types are handled in separate functions.
5432 * Set nodes are currently treated in the same way as sequence nodes.
5433 * The children of a set node may be executed in any order,
5434 * including the order of the children.
5436 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5437 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5438 __isl_take isl_union_map *executed)
5440 enum isl_schedule_node_type type;
5442 type = isl_schedule_node_get_type(node);
5444 switch (type) {
5445 case isl_schedule_node_error:
5446 goto error;
5447 case isl_schedule_node_leaf:
5448 isl_schedule_node_free(node);
5449 return generate_inner_level(executed, build);
5450 case isl_schedule_node_band:
5451 return build_ast_from_band(build, node, executed);
5452 case isl_schedule_node_context:
5453 return build_ast_from_context(build, node, executed);
5454 case isl_schedule_node_domain:
5455 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5456 "unexpected internal domain node", goto error);
5457 case isl_schedule_node_expansion:
5458 return build_ast_from_expansion(build, node, executed);
5459 case isl_schedule_node_extension:
5460 return build_ast_from_extension(build, node, executed);
5461 case isl_schedule_node_filter:
5462 return build_ast_from_filter(build, node, executed);
5463 case isl_schedule_node_guard:
5464 return build_ast_from_guard(build, node, executed);
5465 case isl_schedule_node_mark:
5466 return build_ast_from_mark(build, node, executed);
5467 case isl_schedule_node_sequence:
5468 case isl_schedule_node_set:
5469 return build_ast_from_sequence(build, node, executed);
5472 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5473 "unhandled type", goto error);
5474 error:
5475 isl_union_map_free(executed);
5476 isl_schedule_node_free(node);
5477 isl_ast_build_free(build);
5479 return NULL;
5482 /* Generate an AST that visits the elements in the domain of "executed"
5483 * in the relative order specified by the (single) child of "node" and
5484 * its descendants.
5486 * The relation "executed" maps the outer generated loop iterators
5487 * to the domain elements executed by those iterations.
5489 * This function is never called on a leaf, set or sequence node,
5490 * so the node always has exactly one child.
5492 static __isl_give isl_ast_graft_list *build_ast_from_child(
5493 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5494 __isl_take isl_union_map *executed)
5496 node = isl_schedule_node_child(node, 0);
5497 return build_ast_from_schedule_node(build, node, executed);
5500 /* Generate an AST that visits the elements in the domain of the domain
5501 * node "node" in the relative order specified by its descendants.
5503 * An initial inverse schedule is created that maps a zero-dimensional
5504 * schedule space to the node domain.
5505 * The input "build" is assumed to have a parametric domain and
5506 * is replaced by the same zero-dimensional schedule space.
5508 * We also add some of the parameter constraints in the build domain
5509 * to the executed relation. Adding these constraints
5510 * allows for an earlier detection of conflicts in some cases.
5511 * However, we do not want to divide the executed relation into
5512 * more disjuncts than necessary. We therefore approximate
5513 * the constraints on the parameters by a single disjunct set.
5515 static __isl_give isl_ast_node *build_ast_from_domain(
5516 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5518 isl_ctx *ctx;
5519 isl_union_set *domain, *schedule_domain;
5520 isl_union_map *executed;
5521 isl_space *space;
5522 isl_set *set;
5523 isl_ast_graft_list *list;
5524 isl_ast_node *ast;
5525 int is_params;
5527 if (!build)
5528 goto error;
5530 ctx = isl_ast_build_get_ctx(build);
5531 space = isl_ast_build_get_space(build, 1);
5532 is_params = isl_space_is_params(space);
5533 isl_space_free(space);
5534 if (is_params < 0)
5535 goto error;
5536 if (!is_params)
5537 isl_die(ctx, isl_error_unsupported,
5538 "expecting parametric initial context", goto error);
5540 domain = isl_schedule_node_domain_get_domain(node);
5541 domain = isl_union_set_coalesce(domain);
5543 space = isl_union_set_get_space(domain);
5544 space = isl_space_set_from_params(space);
5545 build = isl_ast_build_product(build, space);
5547 set = isl_ast_build_get_domain(build);
5548 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5549 schedule_domain = isl_union_set_from_set(set);
5551 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5552 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5553 ast = isl_ast_node_from_graft_list(list, build);
5554 isl_ast_build_free(build);
5556 return ast;
5557 error:
5558 isl_schedule_node_free(node);
5559 isl_ast_build_free(build);
5560 return NULL;
5563 /* Generate an AST that visits the elements in the domain of "schedule"
5564 * in the relative order specified by the schedule tree.
5566 * "build" is an isl_ast_build that has been created using
5567 * isl_ast_build_alloc or isl_ast_build_from_context based
5568 * on a parametric set.
5570 * The construction starts at the root node of the schedule,
5571 * which is assumed to be a domain node.
5573 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5574 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5576 isl_ctx *ctx;
5577 isl_schedule_node *node;
5579 if (!build || !schedule)
5580 goto error;
5582 ctx = isl_ast_build_get_ctx(build);
5584 node = isl_schedule_get_root(schedule);
5585 isl_schedule_free(schedule);
5587 build = isl_ast_build_copy(build);
5588 build = isl_ast_build_set_single_valued(build, 0);
5589 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5590 isl_die(ctx, isl_error_unsupported,
5591 "expecting root domain node",
5592 build = isl_ast_build_free(build));
5593 return build_ast_from_domain(build, node);
5594 error:
5595 isl_schedule_free(schedule);
5596 return NULL;