isl_ast_codegen.c: after_each_mark: fix documentation
[isl.git] / isl_ast_codegen.c
blob02a021d7b41eb48eb8cda6f9c55f1b11a0f9502b
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 int 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 0;
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 int 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 0;
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 0;
211 error:
212 isl_map_free(map);
213 isl_map_free(executed);
214 return -1;
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 int 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 ? 0 : -1;
892 /* Does "pa" have a negative constant term over its entire domain?
894 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
896 int r;
897 int *neg = user;
899 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
900 isl_pw_aff_free(pa);
902 return (*neg && r >= 0) ? 0 : -1;
905 /* Does each element in "list" have a negative constant term?
907 * The callback terminates the iteration as soon an element has been
908 * found that does not have a negative constant term.
910 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
912 int neg = 1;
914 if (isl_pw_aff_list_foreach(list,
915 &pw_aff_constant_is_negative, &neg) < 0 && neg)
916 return -1;
918 return neg;
921 /* Add 1 to each of the elements in "list", where each of these elements
922 * is defined over the internal schedule space of "build".
924 static __isl_give isl_pw_aff_list *list_add_one(
925 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
927 int i, n;
928 isl_space *space;
929 isl_aff *aff;
930 isl_pw_aff *one;
932 space = isl_ast_build_get_space(build, 1);
933 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
934 aff = isl_aff_add_constant_si(aff, 1);
935 one = isl_pw_aff_from_aff(aff);
937 n = isl_pw_aff_list_n_pw_aff(list);
938 for (i = 0; i < n; ++i) {
939 isl_pw_aff *pa;
940 pa = isl_pw_aff_list_get_pw_aff(list, i);
941 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
942 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
945 isl_pw_aff_free(one);
947 return list;
950 /* Set the condition part of the for node graft->node in case
951 * the upper bound is represented as a list of piecewise affine expressions.
953 * In particular, set the condition to
955 * iterator <= min(list of upper bounds)
957 * If each of the upper bounds has a negative constant term, then
958 * set the condition to
960 * iterator < min(list of (upper bound + 1)s)
963 static __isl_give isl_ast_graft *set_for_cond_from_list(
964 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
965 __isl_keep isl_ast_build *build)
967 int neg;
968 isl_ast_expr *bound, *iterator, *cond;
969 enum isl_ast_op_type type = isl_ast_op_le;
971 if (!graft || !list)
972 return isl_ast_graft_free(graft);
974 neg = list_constant_is_negative(list);
975 if (neg < 0)
976 return isl_ast_graft_free(graft);
977 list = isl_pw_aff_list_copy(list);
978 if (neg) {
979 list = list_add_one(list, build);
980 type = isl_ast_op_lt;
983 bound = reduce_list(isl_ast_op_min, list, build);
984 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
985 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
986 graft->node->u.f.cond = cond;
988 isl_pw_aff_list_free(list);
989 if (!graft->node->u.f.cond)
990 return isl_ast_graft_free(graft);
991 return graft;
994 /* Set the condition part of the for node graft->node in case
995 * the upper bound is represented as a set.
997 static __isl_give isl_ast_graft *set_for_cond_from_set(
998 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
999 __isl_keep isl_ast_build *build)
1001 isl_ast_expr *cond;
1003 if (!graft)
1004 return NULL;
1006 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1007 graft->node->u.f.cond = cond;
1008 if (!graft->node->u.f.cond)
1009 return isl_ast_graft_free(graft);
1010 return graft;
1013 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1014 * the current dimension.
1016 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1018 int depth;
1019 isl_val *v;
1020 isl_ctx *ctx;
1022 if (!build)
1023 return NULL;
1024 ctx = isl_ast_build_get_ctx(build);
1025 depth = isl_ast_build_get_depth(build);
1027 if (!isl_ast_build_has_stride(build, depth))
1028 return isl_ast_expr_alloc_int_si(ctx, 1);
1030 v = isl_ast_build_get_stride(build, depth);
1031 return isl_ast_expr_from_val(v);
1034 /* Should we express the loop condition as
1036 * iterator <= min(list of upper bounds)
1038 * or as a conjunction of constraints?
1040 * The first is constructed from a list of upper bounds.
1041 * The second is constructed from a set.
1043 * If there are no upper bounds in "constraints", then this could mean
1044 * that "domain" simply doesn't have an upper bound or that we didn't
1045 * pick any upper bound. In the first case, we want to generate the
1046 * loop condition as a(n empty) conjunction of constraints
1047 * In the second case, we will compute
1048 * a single upper bound from "domain" and so we use the list form.
1050 * If there are upper bounds in "constraints",
1051 * then we use the list form iff the atomic_upper_bound option is set.
1053 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1054 __isl_keep isl_set *domain, int depth)
1056 if (n_upper > 0)
1057 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1058 else
1059 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1062 /* Fill in the expressions of the for node in graft->node.
1064 * In particular,
1065 * - set the initialization part of the loop to the maximum of the lower bounds
1066 * - extract the increment from the stride of the current dimension
1067 * - construct the for condition either based on a list of upper bounds
1068 * or on a set of upper bound constraints.
1070 static __isl_give isl_ast_graft *set_for_node_expressions(
1071 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1072 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1073 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1075 isl_ast_node *node;
1077 if (!graft)
1078 return NULL;
1080 build = isl_ast_build_copy(build);
1082 node = graft->node;
1083 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1084 node->u.f.inc = for_inc(build);
1086 if (use_list)
1087 graft = set_for_cond_from_list(graft, upper_list, build);
1088 else
1089 graft = set_for_cond_from_set(graft, upper_set, build);
1091 isl_ast_build_free(build);
1093 if (!node->u.f.iterator || !node->u.f.init ||
1094 !node->u.f.cond || !node->u.f.inc)
1095 return isl_ast_graft_free(graft);
1097 return graft;
1100 /* Update "graft" based on "bounds" and "domain" for the generic,
1101 * non-degenerate, case.
1103 * "c_lower" and "c_upper" contain the lower and upper bounds
1104 * that the loop node should express.
1105 * "domain" is the subset of the intersection of the constraints
1106 * for which some code is executed.
1108 * There may be zero lower bounds or zero upper bounds in "constraints"
1109 * in case the list of constraints was created
1110 * based on the atomic option or based on separation with explicit bounds.
1111 * In that case, we use "domain" to derive lower and/or upper bounds.
1113 * We first compute a list of one or more lower bounds.
1115 * Then we decide if we want to express the condition as
1117 * iterator <= min(list of upper bounds)
1119 * or as a conjunction of constraints.
1121 * The set of enforced constraints is then computed either based on
1122 * a list of upper bounds or on a set of upper bound constraints.
1123 * We do not compute any enforced constraints if we were forced
1124 * to compute a lower or upper bound using exact_bound. The domains
1125 * of the resulting expressions may imply some bounds on outer dimensions
1126 * that we do not want to appear in the enforced constraints since
1127 * they are not actually enforced by the corresponding code.
1129 * Finally, we fill in the expressions of the for node.
1131 static __isl_give isl_ast_graft *refine_generic_bounds(
1132 __isl_take isl_ast_graft *graft,
1133 __isl_take isl_constraint_list *c_lower,
1134 __isl_take isl_constraint_list *c_upper,
1135 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1137 int depth;
1138 isl_ctx *ctx;
1139 isl_pw_aff_list *lower;
1140 int use_list;
1141 isl_set *upper_set = NULL;
1142 isl_pw_aff_list *upper_list = NULL;
1143 int n_lower, n_upper;
1145 if (!graft || !c_lower || !c_upper || !build)
1146 goto error;
1148 depth = isl_ast_build_get_depth(build);
1149 ctx = isl_ast_graft_get_ctx(graft);
1151 n_lower = isl_constraint_list_n_constraint(c_lower);
1152 n_upper = isl_constraint_list_n_constraint(c_upper);
1154 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1156 lower = lower_bounds(c_lower, depth, domain, build);
1158 if (use_list)
1159 upper_list = upper_bounds(c_upper, depth, domain, build);
1160 else if (n_upper > 0)
1161 upper_set = intersect_constraints(c_upper);
1162 else
1163 upper_set = isl_set_universe(isl_set_get_space(domain));
1165 if (n_lower == 0 || n_upper == 0)
1167 else if (use_list)
1168 graft = set_enforced_from_list(graft, lower, upper_list);
1169 else
1170 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1172 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1173 upper_set, build);
1175 isl_pw_aff_list_free(lower);
1176 isl_pw_aff_list_free(upper_list);
1177 isl_set_free(upper_set);
1178 isl_constraint_list_free(c_lower);
1179 isl_constraint_list_free(c_upper);
1181 return graft;
1182 error:
1183 isl_constraint_list_free(c_lower);
1184 isl_constraint_list_free(c_upper);
1185 return isl_ast_graft_free(graft);
1188 /* Internal data structure used inside count_constraints to keep
1189 * track of the number of constraints that are independent of dimension "pos",
1190 * the lower bounds in "pos" and the upper bounds in "pos".
1192 struct isl_ast_count_constraints_data {
1193 int pos;
1195 int n_indep;
1196 int n_lower;
1197 int n_upper;
1200 /* Increment data->n_indep, data->lower or data->upper depending
1201 * on whether "c" is independenct of dimensions data->pos,
1202 * a lower bound or an upper bound.
1204 static int count_constraints(__isl_take isl_constraint *c, void *user)
1206 struct isl_ast_count_constraints_data *data = user;
1208 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1209 data->n_lower++;
1210 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1211 data->n_upper++;
1212 else
1213 data->n_indep++;
1215 isl_constraint_free(c);
1217 return 0;
1220 /* Update "graft" based on "bounds" and "domain" for the generic,
1221 * non-degenerate, case.
1223 * "list" respresent the list of bounds that need to be encoded by
1224 * the for loop. Only the constraints that involve the iterator
1225 * are relevant here. The other constraints are taken care of by
1226 * the caller and are included in the generated constraints of "build".
1227 * "domain" is the subset of the intersection of the constraints
1228 * for which some code is executed.
1229 * "build" is the build in which graft->node was created.
1231 * We separate lower bounds, upper bounds and constraints that
1232 * are independent of the loop iterator.
1234 * The actual for loop bounds are generated in refine_generic_bounds.
1236 static __isl_give isl_ast_graft *refine_generic_split(
1237 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1238 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1240 struct isl_ast_count_constraints_data data;
1241 isl_constraint_list *lower;
1242 isl_constraint_list *upper;
1244 if (!list)
1245 return isl_ast_graft_free(graft);
1247 data.pos = isl_ast_build_get_depth(build);
1249 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1250 if (!list)
1251 return isl_ast_graft_free(graft);
1253 data.n_indep = data.n_lower = data.n_upper = 0;
1254 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1255 isl_constraint_list_free(list);
1256 return isl_ast_graft_free(graft);
1259 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1260 upper = isl_constraint_list_copy(lower);
1261 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1262 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1264 return refine_generic_bounds(graft, lower, upper, domain, build);
1267 /* Update "graft" based on "bounds" and "domain" for the generic,
1268 * non-degenerate, case.
1270 * "bounds" respresent the bounds that need to be encoded by
1271 * the for loop (or a guard around the for loop).
1272 * "domain" is the subset of "bounds" for which some code is executed.
1273 * "build" is the build in which graft->node was created.
1275 * We break up "bounds" into a list of constraints and continue with
1276 * refine_generic_split.
1278 static __isl_give isl_ast_graft *refine_generic(
1279 __isl_take isl_ast_graft *graft,
1280 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1281 __isl_keep isl_ast_build *build)
1283 isl_constraint_list *list;
1285 if (!build || !graft)
1286 return isl_ast_graft_free(graft);
1288 list = isl_basic_set_get_constraint_list(bounds);
1290 graft = refine_generic_split(graft, list, domain, build);
1292 return graft;
1295 /* Create a for node for the current level.
1297 * Mark the for node degenerate if "degenerate" is set.
1299 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1300 int degenerate)
1302 int depth;
1303 isl_id *id;
1304 isl_ast_node *node;
1306 if (!build)
1307 return NULL;
1309 depth = isl_ast_build_get_depth(build);
1310 id = isl_ast_build_get_iterator_id(build, depth);
1311 node = isl_ast_node_alloc_for(id);
1312 if (degenerate)
1313 node = isl_ast_node_for_mark_degenerate(node);
1315 return node;
1318 /* If the ast_build_exploit_nested_bounds option is set, then return
1319 * the constraints enforced by all elements in "list".
1320 * Otherwise, return the universe.
1322 static __isl_give isl_basic_set *extract_shared_enforced(
1323 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1325 isl_ctx *ctx;
1326 isl_space *space;
1328 if (!list)
1329 return NULL;
1331 ctx = isl_ast_graft_list_get_ctx(list);
1332 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1333 return isl_ast_graft_list_extract_shared_enforced(list, build);
1335 space = isl_ast_build_get_space(build, 1);
1336 return isl_basic_set_universe(space);
1339 /* Return the pending constraints of "build" that are not already taken
1340 * care of (by a combination of "enforced" and the generated constraints
1341 * of "build").
1343 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1344 __isl_keep isl_basic_set *enforced)
1346 isl_set *guard, *context;
1348 guard = isl_ast_build_get_pending(build);
1349 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1350 context = isl_set_intersect(context,
1351 isl_ast_build_get_generated(build));
1352 return isl_set_gist(guard, context);
1355 /* Create an AST node for the current dimension based on
1356 * the schedule domain "bounds" and return the node encapsulated
1357 * in an isl_ast_graft.
1359 * "executed" is the current inverse schedule, taking into account
1360 * the bounds in "bounds"
1361 * "domain" is the domain of "executed", with inner dimensions projected out.
1362 * It may be a strict subset of "bounds" in case "bounds" was created
1363 * based on the atomic option or based on separation with explicit bounds.
1365 * "domain" may satisfy additional equalities that result
1366 * from intersecting "executed" with "bounds" in add_node.
1367 * It may also satisfy some global constraints that were dropped out because
1368 * we performed separation with explicit bounds.
1369 * The very first step is then to copy these constraints to "bounds".
1371 * Since we may be calling before_each_for and after_each_for
1372 * callbacks, we record the current inverse schedule in the build.
1374 * We consider three builds,
1375 * "build" is the one in which the current level is created,
1376 * "body_build" is the build in which the next level is created,
1377 * "sub_build" is essentially the same as "body_build", except that
1378 * the depth has not been increased yet.
1380 * "build" already contains information (in strides and offsets)
1381 * about the strides at the current level, but this information is not
1382 * reflected in the build->domain.
1383 * We first add this information and the "bounds" to the sub_build->domain.
1384 * isl_ast_build_set_loop_bounds adds the stride information and
1385 * checks whether the current dimension attains
1386 * only a single value and whether this single value can be represented using
1387 * a single affine expression.
1388 * In the first case, the current level is considered "degenerate".
1389 * In the second, sub-case, the current level is considered "eliminated".
1390 * Eliminated levels don't need to be reflected in the AST since we can
1391 * simply plug in the affine expression. For degenerate, but non-eliminated,
1392 * levels, we do introduce a for node, but mark is as degenerate so that
1393 * it can be printed as an assignment of the single value to the loop
1394 * "iterator".
1396 * If the current level is eliminated, we explicitly plug in the value
1397 * for the current level found by isl_ast_build_set_loop_bounds in the
1398 * inverse schedule. This ensures that if we are working on a slice
1399 * of the domain based on information available in the inverse schedule
1400 * and the build domain, that then this information is also reflected
1401 * in the inverse schedule. This operation also eliminates the current
1402 * dimension from the inverse schedule making sure no inner dimensions depend
1403 * on the current dimension. Otherwise, we create a for node, marking
1404 * it degenerate if appropriate. The initial for node is still incomplete
1405 * and will be completed in either refine_degenerate or refine_generic.
1407 * We then generate a sequence of grafts for the next level,
1408 * create a surrounding graft for the current level and insert
1409 * the for node we created (if the current level is not eliminated).
1410 * Before creating a graft for the current level, we first extract
1411 * hoistable constraints from the child guards and combine them
1412 * with the pending constraints in the build. These constraints
1413 * are used to simplify the child guards and then added to the guard
1414 * of the current graft to ensure that they will be generated.
1415 * If the hoisted guard is a disjunction, then we use it directly
1416 * to gist the guards on the children before intersect it with the
1417 * pending constraints. We do so because this disjunction is typically
1418 * identical to the guards on the children such that these guards
1419 * can be effectively removed completely. After the intersection,
1420 * the gist operation would have a harder time figuring this out.
1422 * Finally, we set the bounds of the for loop in either
1423 * refine_degenerate or refine_generic.
1424 * We do so in a context where the pending constraints of the build
1425 * have been replaced by the guard of the current graft.
1427 static __isl_give isl_ast_graft *create_node_scaled(
1428 __isl_take isl_union_map *executed,
1429 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1430 __isl_take isl_ast_build *build)
1432 int depth;
1433 int degenerate, eliminated;
1434 isl_basic_set *hull;
1435 isl_basic_set *enforced;
1436 isl_set *guard, *hoisted;
1437 isl_ast_node *node = NULL;
1438 isl_ast_graft *graft;
1439 isl_ast_graft_list *children;
1440 isl_ast_build *sub_build;
1441 isl_ast_build *body_build;
1443 domain = isl_ast_build_eliminate_divs(build, domain);
1444 domain = isl_set_detect_equalities(domain);
1445 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1446 bounds = isl_basic_set_intersect(bounds, hull);
1447 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1449 depth = isl_ast_build_get_depth(build);
1450 sub_build = isl_ast_build_copy(build);
1451 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1452 isl_basic_set_copy(bounds));
1453 degenerate = isl_ast_build_has_value(sub_build);
1454 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1455 if (degenerate < 0 || eliminated < 0)
1456 executed = isl_union_map_free(executed);
1457 if (eliminated)
1458 executed = plug_in_values(executed, sub_build);
1459 else
1460 node = create_for(build, degenerate);
1462 body_build = isl_ast_build_copy(sub_build);
1463 body_build = isl_ast_build_increase_depth(body_build);
1464 if (!eliminated)
1465 node = before_each_for(node, body_build);
1466 children = generate_next_level(executed,
1467 isl_ast_build_copy(body_build));
1469 enforced = extract_shared_enforced(children, build);
1470 guard = extract_pending(sub_build, enforced);
1471 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1472 if (isl_set_n_basic_set(hoisted) > 1)
1473 children = isl_ast_graft_list_gist_guards(children,
1474 isl_set_copy(hoisted));
1475 guard = isl_set_intersect(guard, hoisted);
1476 if (!eliminated)
1477 guard = add_implied_guards(guard, degenerate, bounds, build);
1479 graft = isl_ast_graft_alloc_from_children(children,
1480 isl_set_copy(guard), enforced, build, sub_build);
1482 if (!degenerate)
1483 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1484 if (!eliminated) {
1485 isl_ast_build *for_build;
1487 graft = isl_ast_graft_insert_for(graft, node);
1488 for_build = isl_ast_build_copy(build);
1489 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1490 isl_set_copy(guard));
1491 if (degenerate)
1492 graft = refine_degenerate(graft, for_build, sub_build);
1493 else
1494 graft = refine_generic(graft, bounds,
1495 domain, for_build);
1496 isl_ast_build_free(for_build);
1498 isl_set_free(guard);
1499 if (!eliminated)
1500 graft = after_each_for(graft, body_build);
1502 isl_ast_build_free(body_build);
1503 isl_ast_build_free(sub_build);
1504 isl_ast_build_free(build);
1505 isl_basic_set_free(bounds);
1506 isl_set_free(domain);
1508 return graft;
1511 /* Internal data structure for checking if all constraints involving
1512 * the input dimension "depth" are such that the other coefficients
1513 * are multiples of "m", reducing "m" if they are not.
1514 * If "m" is reduced all the way down to "1", then the check has failed
1515 * and we break out of the iteration.
1517 struct isl_check_scaled_data {
1518 int depth;
1519 isl_val *m;
1522 /* If constraint "c" involves the input dimension data->depth,
1523 * then make sure that all the other coefficients are multiples of data->m,
1524 * reducing data->m if needed.
1525 * Break out of the iteration if data->m has become equal to "1".
1527 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1529 struct isl_check_scaled_data *data = user;
1530 int i, j, n;
1531 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1532 isl_dim_div };
1534 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1535 isl_constraint_free(c);
1536 return 0;
1539 for (i = 0; i < 4; ++i) {
1540 n = isl_constraint_dim(c, t[i]);
1541 for (j = 0; j < n; ++j) {
1542 isl_val *d;
1544 if (t[i] == isl_dim_in && j == data->depth)
1545 continue;
1546 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1547 continue;
1548 d = isl_constraint_get_coefficient_val(c, t[i], j);
1549 data->m = isl_val_gcd(data->m, d);
1550 if (isl_val_is_one(data->m))
1551 break;
1553 if (j < n)
1554 break;
1557 isl_constraint_free(c);
1559 return i < 4 ? -1 : 0;
1562 /* For each constraint of "bmap" that involves the input dimension data->depth,
1563 * make sure that all the other coefficients are multiples of data->m,
1564 * reducing data->m if needed.
1565 * Break out of the iteration if data->m has become equal to "1".
1567 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1569 int r;
1571 r = isl_basic_map_foreach_constraint(bmap,
1572 &constraint_check_scaled, user);
1573 isl_basic_map_free(bmap);
1575 return r;
1578 /* For each constraint of "map" that involves the input dimension data->depth,
1579 * make sure that all the other coefficients are multiples of data->m,
1580 * reducing data->m if needed.
1581 * Break out of the iteration if data->m has become equal to "1".
1583 static int map_check_scaled(__isl_take isl_map *map, void *user)
1585 int r;
1587 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1588 isl_map_free(map);
1590 return r;
1593 /* Create an AST node for the current dimension based on
1594 * the schedule domain "bounds" and return the node encapsulated
1595 * in an isl_ast_graft.
1597 * "executed" is the current inverse schedule, taking into account
1598 * the bounds in "bounds"
1599 * "domain" is the domain of "executed", with inner dimensions projected out.
1602 * Before moving on to the actual AST node construction in create_node_scaled,
1603 * we first check if the current dimension is strided and if we can scale
1604 * down this stride. Note that we only do this if the ast_build_scale_strides
1605 * option is set.
1607 * In particular, let the current dimension take on values
1609 * f + s a
1611 * with a an integer. We check if we can find an integer m that (obviously)
1612 * divides both f and s.
1614 * If so, we check if the current dimension only appears in constraints
1615 * where the coefficients of the other variables are multiples of m.
1616 * We perform this extra check to avoid the risk of introducing
1617 * divisions by scaling down the current dimension.
1619 * If so, we scale the current dimension down by a factor of m.
1620 * That is, we plug in
1622 * i = m i' (1)
1624 * Note that in principle we could always scale down strided loops
1625 * by plugging in
1627 * i = f + s i'
1629 * but this may result in i' taking on larger values than the original i,
1630 * due to the shift by "f".
1631 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1633 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1634 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1635 __isl_take isl_ast_build *build)
1637 struct isl_check_scaled_data data;
1638 isl_ctx *ctx;
1639 isl_aff *offset;
1640 isl_val *d;
1642 ctx = isl_ast_build_get_ctx(build);
1643 if (!isl_options_get_ast_build_scale_strides(ctx))
1644 return create_node_scaled(executed, bounds, domain, build);
1646 data.depth = isl_ast_build_get_depth(build);
1647 if (!isl_ast_build_has_stride(build, data.depth))
1648 return create_node_scaled(executed, bounds, domain, build);
1650 offset = isl_ast_build_get_offset(build, data.depth);
1651 data.m = isl_ast_build_get_stride(build, data.depth);
1652 if (!data.m)
1653 offset = isl_aff_free(offset);
1654 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1655 d = isl_aff_get_denominator_val(offset);
1656 if (!d)
1657 executed = isl_union_map_free(executed);
1659 if (executed && isl_val_is_divisible_by(data.m, d))
1660 data.m = isl_val_div(data.m, d);
1661 else {
1662 data.m = isl_val_set_si(data.m, 1);
1663 isl_val_free(d);
1666 if (!isl_val_is_one(data.m)) {
1667 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1668 &data) < 0 &&
1669 !isl_val_is_one(data.m))
1670 executed = isl_union_map_free(executed);
1673 if (!isl_val_is_one(data.m)) {
1674 isl_space *space;
1675 isl_multi_aff *ma;
1676 isl_aff *aff;
1677 isl_map *map;
1678 isl_union_map *umap;
1680 space = isl_ast_build_get_space(build, 1);
1681 space = isl_space_map_from_set(space);
1682 ma = isl_multi_aff_identity(space);
1683 aff = isl_multi_aff_get_aff(ma, data.depth);
1684 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1685 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1687 bounds = isl_basic_set_preimage_multi_aff(bounds,
1688 isl_multi_aff_copy(ma));
1689 domain = isl_set_preimage_multi_aff(domain,
1690 isl_multi_aff_copy(ma));
1691 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1692 umap = isl_union_map_from_map(map);
1693 executed = isl_union_map_apply_domain(executed,
1694 isl_union_map_copy(umap));
1695 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1696 umap);
1698 isl_aff_free(offset);
1699 isl_val_free(data.m);
1701 return create_node_scaled(executed, bounds, domain, build);
1704 /* Add the basic set to the list that "user" points to.
1706 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1708 isl_basic_set_list **list = user;
1710 *list = isl_basic_set_list_add(*list, bset);
1712 return 0;
1715 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1717 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1718 __isl_take isl_set *set)
1720 int n;
1721 isl_ctx *ctx;
1722 isl_basic_set_list *list;
1724 if (!set)
1725 return NULL;
1727 ctx = isl_set_get_ctx(set);
1729 n = isl_set_n_basic_set(set);
1730 list = isl_basic_set_list_alloc(ctx, n);
1731 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1732 list = isl_basic_set_list_free(list);
1734 isl_set_free(set);
1735 return list;
1738 /* Generate code for the schedule domain "bounds"
1739 * and add the result to "list".
1741 * We mainly detect strides here and check if the bounds do not
1742 * conflict with the current build domain
1743 * and then pass over control to create_node.
1745 * "bounds" reflects the bounds on the current dimension and possibly
1746 * some extra conditions on outer dimensions.
1747 * It does not, however, include any divs involving the current dimension,
1748 * so it does not capture any stride constraints.
1749 * We therefore need to compute that part of the schedule domain that
1750 * intersects with "bounds" and derive the strides from the result.
1752 static __isl_give isl_ast_graft_list *add_node(
1753 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1754 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1756 isl_ast_graft *graft;
1757 isl_set *domain = NULL;
1758 isl_union_set *uset;
1759 int empty, disjoint;
1761 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1762 executed = isl_union_map_intersect_domain(executed, uset);
1763 empty = isl_union_map_is_empty(executed);
1764 if (empty < 0)
1765 goto error;
1766 if (empty)
1767 goto done;
1769 uset = isl_union_map_domain(isl_union_map_copy(executed));
1770 domain = isl_set_from_union_set(uset);
1771 domain = isl_ast_build_specialize(build, domain);
1773 domain = isl_set_compute_divs(domain);
1774 domain = isl_ast_build_eliminate_inner(build, domain);
1775 disjoint = isl_set_is_disjoint(domain, build->domain);
1776 if (disjoint < 0)
1777 goto error;
1778 if (disjoint)
1779 goto done;
1781 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1783 graft = create_node(executed, bounds, domain,
1784 isl_ast_build_copy(build));
1785 list = isl_ast_graft_list_add(list, graft);
1786 isl_ast_build_free(build);
1787 return list;
1788 error:
1789 list = isl_ast_graft_list_free(list);
1790 done:
1791 isl_set_free(domain);
1792 isl_basic_set_free(bounds);
1793 isl_union_map_free(executed);
1794 isl_ast_build_free(build);
1795 return list;
1798 /* Does any element of i follow or coincide with any element of j
1799 * at the current depth for equal values of the outer dimensions?
1801 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1802 __isl_keep isl_basic_set *j, void *user)
1804 int depth = *(int *) user;
1805 isl_basic_map *test;
1806 int empty;
1807 int l;
1809 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1810 isl_basic_set_copy(j));
1811 for (l = 0; l < depth; ++l)
1812 test = isl_basic_map_equate(test, isl_dim_in, l,
1813 isl_dim_out, l);
1814 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1815 isl_dim_out, depth);
1816 empty = isl_basic_map_is_empty(test);
1817 isl_basic_map_free(test);
1819 return empty < 0 ? -1 : !empty;
1822 /* Split up each element of "list" into a part that is related to "bset"
1823 * according to "gt" and a part that is not.
1824 * Return a list that consist of "bset" and all the pieces.
1826 static __isl_give isl_basic_set_list *add_split_on(
1827 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1828 __isl_keep isl_basic_map *gt)
1830 int i, n;
1831 isl_basic_set_list *res;
1833 if (!list)
1834 bset = isl_basic_set_free(bset);
1836 gt = isl_basic_map_copy(gt);
1837 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1838 n = isl_basic_set_list_n_basic_set(list);
1839 res = isl_basic_set_list_from_basic_set(bset);
1840 for (i = 0; res && i < n; ++i) {
1841 isl_basic_set *bset;
1842 isl_set *set1, *set2;
1843 isl_basic_map *bmap;
1844 int empty;
1846 bset = isl_basic_set_list_get_basic_set(list, i);
1847 bmap = isl_basic_map_copy(gt);
1848 bmap = isl_basic_map_intersect_range(bmap, bset);
1849 bset = isl_basic_map_range(bmap);
1850 empty = isl_basic_set_is_empty(bset);
1851 if (empty < 0)
1852 res = isl_basic_set_list_free(res);
1853 if (empty) {
1854 isl_basic_set_free(bset);
1855 bset = isl_basic_set_list_get_basic_set(list, i);
1856 res = isl_basic_set_list_add(res, bset);
1857 continue;
1860 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1861 set1 = isl_set_from_basic_set(bset);
1862 bset = isl_basic_set_list_get_basic_set(list, i);
1863 set2 = isl_set_from_basic_set(bset);
1864 set1 = isl_set_subtract(set2, set1);
1865 set1 = isl_set_make_disjoint(set1);
1867 res = isl_basic_set_list_concat(res,
1868 isl_basic_set_list_from_set(set1));
1870 isl_basic_map_free(gt);
1871 isl_basic_set_list_free(list);
1872 return res;
1875 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1876 __isl_keep isl_basic_set_list *domain_list,
1877 __isl_keep isl_union_map *executed,
1878 __isl_keep isl_ast_build *build);
1880 /* Internal data structure for add_nodes.
1882 * "executed" and "build" are extra arguments to be passed to add_node.
1883 * "list" collects the results.
1885 struct isl_add_nodes_data {
1886 isl_union_map *executed;
1887 isl_ast_build *build;
1889 isl_ast_graft_list *list;
1892 /* Generate code for the schedule domains in "scc"
1893 * and add the results to "list".
1895 * The domains in "scc" form a strongly connected component in the ordering.
1896 * If the number of domains in "scc" is larger than 1, then this means
1897 * that we cannot determine a valid ordering for the domains in the component.
1898 * This should be fairly rare because the individual domains
1899 * have been made disjoint first.
1900 * The problem is that the domains may be integrally disjoint but not
1901 * rationally disjoint. For example, we may have domains
1903 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1905 * These two domains have an empty intersection, but their rational
1906 * relaxations do intersect. It is impossible to order these domains
1907 * in the second dimension because the first should be ordered before
1908 * the second for outer dimension equal to 0, while it should be ordered
1909 * after for outer dimension equal to 1.
1911 * This may happen in particular in case of unrolling since the domain
1912 * of each slice is replaced by its simple hull.
1914 * For each basic set i in "scc" and for each of the following basic sets j,
1915 * we split off that part of the basic set i that shares the outer dimensions
1916 * with j and lies before j in the current dimension.
1917 * We collect all the pieces in a new list that replaces "scc".
1919 * While the elements in "scc" should be disjoint, we double-check
1920 * this property to avoid running into an infinite recursion in case
1921 * they intersect due to some internal error.
1923 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1925 struct isl_add_nodes_data *data = user;
1926 int i, n, depth;
1927 isl_basic_set *bset, *first;
1928 isl_basic_set_list *list;
1929 isl_space *space;
1930 isl_basic_map *gt;
1932 n = isl_basic_set_list_n_basic_set(scc);
1933 bset = isl_basic_set_list_get_basic_set(scc, 0);
1934 if (n == 1) {
1935 isl_basic_set_list_free(scc);
1936 data->list = add_node(data->list,
1937 isl_union_map_copy(data->executed), bset,
1938 isl_ast_build_copy(data->build));
1939 return data->list ? 0 : -1;
1942 depth = isl_ast_build_get_depth(data->build);
1943 space = isl_basic_set_get_space(bset);
1944 space = isl_space_map_from_set(space);
1945 gt = isl_basic_map_universe(space);
1946 for (i = 0; i < depth; ++i)
1947 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1948 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1950 first = isl_basic_set_copy(bset);
1951 list = isl_basic_set_list_from_basic_set(bset);
1952 for (i = 1; i < n; ++i) {
1953 int disjoint;
1955 bset = isl_basic_set_list_get_basic_set(scc, i);
1957 disjoint = isl_basic_set_is_disjoint(bset, first);
1958 if (disjoint < 0)
1959 list = isl_basic_set_list_free(list);
1960 else if (!disjoint)
1961 isl_die(isl_basic_set_list_get_ctx(scc),
1962 isl_error_internal,
1963 "basic sets in scc are assumed to be disjoint",
1964 list = isl_basic_set_list_free(list));
1966 list = add_split_on(list, bset, gt);
1968 isl_basic_set_free(first);
1969 isl_basic_map_free(gt);
1970 isl_basic_set_list_free(scc);
1971 scc = list;
1972 data->list = isl_ast_graft_list_concat(data->list,
1973 generate_sorted_domains(scc, data->executed, data->build));
1974 isl_basic_set_list_free(scc);
1976 return data->list ? 0 : -1;
1979 /* Sort the domains in "domain_list" according to the execution order
1980 * at the current depth (for equal values of the outer dimensions),
1981 * generate code for each of them, collecting the results in a list.
1982 * If no code is generated (because the intersection of the inverse schedule
1983 * with the domains turns out to be empty), then an empty list is returned.
1985 * The caller is responsible for ensuring that the basic sets in "domain_list"
1986 * are pair-wise disjoint. It can, however, in principle happen that
1987 * two basic sets should be ordered one way for one value of the outer
1988 * dimensions and the other way for some other value of the outer dimensions.
1989 * We therefore play safe and look for strongly connected components.
1990 * The function add_nodes takes care of handling non-trivial components.
1992 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1993 __isl_keep isl_basic_set_list *domain_list,
1994 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1996 isl_ctx *ctx;
1997 struct isl_add_nodes_data data;
1998 int depth;
1999 int n;
2001 if (!domain_list)
2002 return NULL;
2004 ctx = isl_basic_set_list_get_ctx(domain_list);
2005 n = isl_basic_set_list_n_basic_set(domain_list);
2006 data.list = isl_ast_graft_list_alloc(ctx, n);
2007 if (n == 0)
2008 return data.list;
2009 if (n == 1)
2010 return add_node(data.list, isl_union_map_copy(executed),
2011 isl_basic_set_list_get_basic_set(domain_list, 0),
2012 isl_ast_build_copy(build));
2014 depth = isl_ast_build_get_depth(build);
2015 data.executed = executed;
2016 data.build = build;
2017 if (isl_basic_set_list_foreach_scc(domain_list,
2018 &domain_follows_at_depth, &depth,
2019 &add_nodes, &data) < 0)
2020 data.list = isl_ast_graft_list_free(data.list);
2022 return data.list;
2025 /* Do i and j share any values for the outer dimensions?
2027 static int shared_outer(__isl_keep isl_basic_set *i,
2028 __isl_keep isl_basic_set *j, void *user)
2030 int depth = *(int *) user;
2031 isl_basic_map *test;
2032 int empty;
2033 int l;
2035 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2036 isl_basic_set_copy(j));
2037 for (l = 0; l < depth; ++l)
2038 test = isl_basic_map_equate(test, isl_dim_in, l,
2039 isl_dim_out, l);
2040 empty = isl_basic_map_is_empty(test);
2041 isl_basic_map_free(test);
2043 return empty < 0 ? -1 : !empty;
2046 /* Internal data structure for generate_sorted_domains_wrap.
2048 * "n" is the total number of basic sets
2049 * "executed" and "build" are extra arguments to be passed
2050 * to generate_sorted_domains.
2052 * "single" is set to 1 by generate_sorted_domains_wrap if there
2053 * is only a single component.
2054 * "list" collects the results.
2056 struct isl_ast_generate_parallel_domains_data {
2057 int n;
2058 isl_union_map *executed;
2059 isl_ast_build *build;
2061 int single;
2062 isl_ast_graft_list *list;
2065 /* Call generate_sorted_domains on "scc", fuse the result into a list
2066 * with either zero or one graft and collect the these single element
2067 * lists into data->list.
2069 * If there is only one component, i.e., if the number of basic sets
2070 * in the current component is equal to the total number of basic sets,
2071 * then data->single is set to 1 and the result of generate_sorted_domains
2072 * is not fused.
2074 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2075 void *user)
2077 struct isl_ast_generate_parallel_domains_data *data = user;
2078 isl_ast_graft_list *list;
2080 list = generate_sorted_domains(scc, data->executed, data->build);
2081 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2082 if (!data->single)
2083 list = isl_ast_graft_list_fuse(list, data->build);
2084 if (!data->list)
2085 data->list = list;
2086 else
2087 data->list = isl_ast_graft_list_concat(data->list, list);
2089 isl_basic_set_list_free(scc);
2090 if (!data->list)
2091 return -1;
2093 return 0;
2096 /* Look for any (weakly connected) components in the "domain_list"
2097 * of domains that share some values of the outer dimensions.
2098 * That is, domains in different components do not share any values
2099 * of the outer dimensions. This means that these components
2100 * can be freely reordered.
2101 * Within each of the components, we sort the domains according
2102 * to the execution order at the current depth.
2104 * If there is more than one component, then generate_sorted_domains_wrap
2105 * fuses the result of each call to generate_sorted_domains
2106 * into a list with either zero or one graft and collects these (at most)
2107 * single element lists into a bigger list. This means that the elements of the
2108 * final list can be freely reordered. In particular, we sort them
2109 * according to an arbitrary but fixed ordering to ease merging of
2110 * graft lists from different components.
2112 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2113 __isl_keep isl_basic_set_list *domain_list,
2114 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2116 int depth;
2117 struct isl_ast_generate_parallel_domains_data data;
2119 if (!domain_list)
2120 return NULL;
2122 data.n = isl_basic_set_list_n_basic_set(domain_list);
2123 if (data.n <= 1)
2124 return generate_sorted_domains(domain_list, executed, build);
2126 depth = isl_ast_build_get_depth(build);
2127 data.list = NULL;
2128 data.executed = executed;
2129 data.build = build;
2130 data.single = 0;
2131 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2132 &generate_sorted_domains_wrap,
2133 &data) < 0)
2134 data.list = isl_ast_graft_list_free(data.list);
2136 if (!data.single)
2137 data.list = isl_ast_graft_list_sort_guard(data.list);
2139 return data.list;
2142 /* Internal data for separate_domain.
2144 * "explicit" is set if we only want to use explicit bounds.
2146 * "domain" collects the separated domains.
2148 struct isl_separate_domain_data {
2149 isl_ast_build *build;
2150 int explicit;
2151 isl_set *domain;
2154 /* Extract implicit bounds on the current dimension for the executed "map".
2156 * The domain of "map" may involve inner dimensions, so we
2157 * need to eliminate them.
2159 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2160 __isl_keep isl_ast_build *build)
2162 isl_set *domain;
2164 domain = isl_map_domain(map);
2165 domain = isl_ast_build_eliminate(build, domain);
2167 return domain;
2170 /* Extract explicit bounds on the current dimension for the executed "map".
2172 * Rather than eliminating the inner dimensions as in implicit_bounds,
2173 * we simply drop any constraints involving those inner dimensions.
2174 * The idea is that most bounds that are implied by constraints on the
2175 * inner dimensions will be enforced by for loops and not by explicit guards.
2176 * There is then no need to separate along those bounds.
2178 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2179 __isl_keep isl_ast_build *build)
2181 isl_set *domain;
2182 int depth, dim;
2184 dim = isl_map_dim(map, isl_dim_out);
2185 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2187 domain = isl_map_domain(map);
2188 depth = isl_ast_build_get_depth(build);
2189 dim = isl_set_dim(domain, isl_dim_set);
2190 domain = isl_set_detect_equalities(domain);
2191 domain = isl_set_drop_constraints_involving_dims(domain,
2192 isl_dim_set, depth + 1, dim - (depth + 1));
2193 domain = isl_set_remove_divs_involving_dims(domain,
2194 isl_dim_set, depth, 1);
2195 domain = isl_set_remove_unknown_divs(domain);
2197 return domain;
2200 /* Split data->domain into pieces that intersect with the range of "map"
2201 * and pieces that do not intersect with the range of "map"
2202 * and then add that part of the range of "map" that does not intersect
2203 * with data->domain.
2205 static int separate_domain(__isl_take isl_map *map, void *user)
2207 struct isl_separate_domain_data *data = user;
2208 isl_set *domain;
2209 isl_set *d1, *d2;
2211 if (data->explicit)
2212 domain = explicit_bounds(map, data->build);
2213 else
2214 domain = implicit_bounds(map, data->build);
2216 domain = isl_set_coalesce(domain);
2217 domain = isl_set_make_disjoint(domain);
2218 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2219 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2220 data->domain = isl_set_intersect(data->domain, domain);
2221 data->domain = isl_set_union(data->domain, d1);
2222 data->domain = isl_set_union(data->domain, d2);
2224 return 0;
2227 /* Separate the schedule domains of "executed".
2229 * That is, break up the domain of "executed" into basic sets,
2230 * such that for each basic set S, every element in S is associated with
2231 * the same domain spaces.
2233 * "space" is the (single) domain space of "executed".
2235 static __isl_give isl_set *separate_schedule_domains(
2236 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2237 __isl_keep isl_ast_build *build)
2239 struct isl_separate_domain_data data = { build };
2240 isl_ctx *ctx;
2242 ctx = isl_ast_build_get_ctx(build);
2243 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2244 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2245 data.domain = isl_set_empty(space);
2246 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2247 data.domain = isl_set_free(data.domain);
2249 isl_union_map_free(executed);
2250 return data.domain;
2253 /* Temporary data used during the search for a lower bound for unrolling.
2255 * "build" is the build in which the unrolling will be performed
2256 * "domain" is the original set for which to find a lower bound
2257 * "depth" is the dimension for which to find a lower boudn
2258 * "expansion" is the expansion that needs to be applied to "domain"
2259 * in the unrolling that will be performed
2261 * "lower" is the best lower bound found so far. It is NULL if we have not
2262 * found any yet.
2263 * "n" is the corresponding size. If lower is NULL, then the value of n
2264 * is undefined.
2265 * "n_div" is the maximal number of integer divisions in the first
2266 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2267 * been computed yet.
2269 struct isl_find_unroll_data {
2270 isl_ast_build *build;
2271 isl_set *domain;
2272 int depth;
2273 isl_basic_map *expansion;
2275 isl_aff *lower;
2276 int *n;
2277 int n_div;
2280 /* Return the constraint
2282 * i_"depth" = aff + offset
2284 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2285 int offset)
2287 aff = isl_aff_copy(aff);
2288 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2289 aff = isl_aff_add_constant_si(aff, offset);
2290 return isl_equality_from_aff(aff);
2293 /* Update *user to the number of integer divsions in the first element
2294 * of "ma", if it is larger than the current value.
2296 static int update_n_div(__isl_take isl_set *set, __isl_take isl_multi_aff *ma,
2297 void *user)
2299 isl_aff *aff;
2300 int *n = user;
2301 int n_div;
2303 aff = isl_multi_aff_get_aff(ma, 0);
2304 n_div = isl_aff_dim(aff, isl_dim_div);
2305 isl_aff_free(aff);
2306 isl_multi_aff_free(ma);
2307 isl_set_free(set);
2309 if (n_div > *n)
2310 *n = n_div;
2312 return aff ? 0 : -1;
2315 /* Get the number of integer divisions in the expression for the iterator
2316 * value at the first slice in the unrolling based on lower bound "lower",
2317 * taking into account the expansion that needs to be performed on this slice.
2319 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2320 __isl_keep isl_aff *lower)
2322 isl_constraint *c;
2323 isl_set *set;
2324 isl_map *it_map, *expansion;
2325 isl_pw_multi_aff *pma;
2326 int n;
2328 c = at_offset(data->depth, lower, 0);
2329 set = isl_set_copy(data->domain);
2330 set = isl_set_add_constraint(set, c);
2331 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2332 set = isl_set_apply(set, expansion);
2333 it_map = isl_ast_build_map_to_iterator(data->build, set);
2334 pma = isl_pw_multi_aff_from_map(it_map);
2335 n = 0;
2336 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2337 n = -1;
2338 isl_pw_multi_aff_free(pma);
2340 return n;
2343 /* Is the lower bound "lower" with corresponding iteration count "n"
2344 * better than the one stored in "data"?
2345 * If there is no upper bound on the iteration count ("n" is infinity) or
2346 * if the count is too large, then we cannot use this lower bound.
2347 * Otherwise, if there was no previous lower bound or
2348 * if the iteration count of the new lower bound is smaller than
2349 * the iteration count of the previous lower bound, then we consider
2350 * the new lower bound to be better.
2351 * If the iteration count is the same, then compare the number
2352 * of integer divisions that would be needed to express
2353 * the iterator value at the first slice in the unrolling
2354 * according to the lower bound. If we end up computing this
2355 * number, then store the lowest value in data->n_div.
2357 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2358 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2360 int cmp;
2361 int n_div;
2363 if (!n)
2364 return -1;
2365 if (isl_val_is_infty(n))
2366 return 0;
2367 if (isl_val_cmp_si(n, INT_MAX) > 0)
2368 return 0;
2369 if (!data->lower)
2370 return 1;
2371 cmp = isl_val_cmp_si(n, *data->n);
2372 if (cmp < 0)
2373 return 1;
2374 if (cmp > 0)
2375 return 0;
2376 if (data->n_div < 0)
2377 data->n_div = get_expanded_n_div(data, data->lower);
2378 if (data->n_div < 0)
2379 return -1;
2380 if (data->n_div == 0)
2381 return 0;
2382 n_div = get_expanded_n_div(data, lower);
2383 if (n_div < 0)
2384 return -1;
2385 if (n_div >= data->n_div)
2386 return 0;
2387 data->n_div = n_div;
2389 return 1;
2392 /* Check if we can use "c" as a lower bound and if it is better than
2393 * any previously found lower bound.
2395 * If "c" does not involve the dimension at the current depth,
2396 * then we cannot use it.
2397 * Otherwise, let "c" be of the form
2399 * i >= f(j)/a
2401 * We compute the maximal value of
2403 * -ceil(f(j)/a)) + i + 1
2405 * over the domain. If there is such a value "n", then we know
2407 * -ceil(f(j)/a)) + i + 1 <= n
2409 * or
2411 * i < ceil(f(j)/a)) + n
2413 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2414 * We just need to check if we have found any lower bound before and
2415 * if the new lower bound is better (smaller n or fewer integer divisions)
2416 * than the previously found lower bounds.
2418 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2419 __isl_keep isl_constraint *c)
2421 isl_aff *aff, *lower;
2422 isl_val *max;
2423 int better;
2425 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2426 return 0;
2428 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2429 lower = isl_aff_ceil(lower);
2430 aff = isl_aff_copy(lower);
2431 aff = isl_aff_neg(aff);
2432 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2433 aff = isl_aff_add_constant_si(aff, 1);
2434 max = isl_set_max_val(data->domain, aff);
2435 isl_aff_free(aff);
2437 better = is_better_lower_bound(data, lower, max);
2438 if (better < 0 || !better) {
2439 isl_val_free(max);
2440 isl_aff_free(lower);
2441 return better < 0 ? -1 : 0;
2444 isl_aff_free(data->lower);
2445 data->lower = lower;
2446 *data->n = isl_val_get_num_si(max);
2447 isl_val_free(max);
2449 return 1;
2452 /* Check if we can use "c" as a lower bound and if it is better than
2453 * any previously found lower bound.
2455 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2457 struct isl_find_unroll_data *data;
2458 int r;
2460 data = (struct isl_find_unroll_data *) user;
2461 r = update_unrolling_lower_bound(data, c);
2462 isl_constraint_free(c);
2464 return r;
2467 /* Look for a lower bound l(i) on the dimension at "depth"
2468 * and a size n such that "domain" is a subset of
2470 * { [i] : l(i) <= i_d < l(i) + n }
2472 * where d is "depth" and l(i) depends only on earlier dimensions.
2473 * Furthermore, try and find a lower bound such that n is as small as possible.
2474 * In particular, "n" needs to be finite.
2475 * "build" is the build in which the unrolling will be performed.
2476 * "expansion" is the expansion that needs to be applied to "domain"
2477 * in the unrolling that will be performed.
2479 * Inner dimensions have been eliminated from "domain" by the caller.
2481 * We first construct a collection of lower bounds on the input set
2482 * by computing its simple hull. We then iterate through them,
2483 * discarding those that we cannot use (either because they do not
2484 * involve the dimension at "depth" or because they have no corresponding
2485 * upper bound, meaning that "n" would be unbounded) and pick out the
2486 * best from the remaining ones.
2488 * If we cannot find a suitable lower bound, then we consider that
2489 * to be an error.
2491 static __isl_give isl_aff *find_unroll_lower_bound(
2492 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2493 int depth, __isl_keep isl_basic_map *expansion, int *n)
2495 struct isl_find_unroll_data data =
2496 { build, domain, depth, expansion, NULL, n, -1 };
2497 isl_basic_set *hull;
2499 hull = isl_set_simple_hull(isl_set_copy(domain));
2501 if (isl_basic_set_foreach_constraint(hull,
2502 &constraint_find_unroll, &data) < 0)
2503 goto error;
2505 isl_basic_set_free(hull);
2507 if (!data.lower)
2508 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2509 "cannot find lower bound for unrolling", return NULL);
2511 return data.lower;
2512 error:
2513 isl_basic_set_free(hull);
2514 return isl_aff_free(data.lower);
2517 /* Call "fn" on each iteration of the current dimension of "domain".
2518 * If "init" is not NULL, then it is called with the number of
2519 * iterations before any call to "fn".
2520 * Return -1 on failure.
2522 * Since we are going to be iterating over the individual values,
2523 * we first check if there are any strides on the current dimension.
2524 * If there is, we rewrite the current dimension i as
2526 * i = stride i' + offset
2528 * and then iterate over individual values of i' instead.
2530 * We then look for a lower bound on i' and a size such that the domain
2531 * is a subset of
2533 * { [j,i'] : l(j) <= i' < l(j) + n }
2535 * and then take slices of the domain at values of i'
2536 * between l(j) and l(j) + n - 1.
2538 * We compute the unshifted simple hull of each slice to ensure that
2539 * we have a single basic set per offset. The slicing constraint
2540 * may get simplified away before the unshifted simple hull is taken
2541 * and may therefore in some rare cases disappear from the result.
2542 * We therefore explicitly add the constraint back after computing
2543 * the unshifted simple hull to ensure that the basic sets
2544 * remain disjoint. The constraints that are dropped by taking the hull
2545 * will be taken into account at the next level, as in the case of the
2546 * atomic option.
2548 * Finally, we map i' back to i and call "fn".
2550 static int foreach_iteration(__isl_take isl_set *domain,
2551 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2552 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2554 int i, n;
2555 int depth;
2556 isl_multi_aff *expansion;
2557 isl_basic_map *bmap;
2558 isl_aff *lower;
2559 isl_ast_build *stride_build;
2561 depth = isl_ast_build_get_depth(build);
2563 domain = isl_ast_build_eliminate_inner(build, domain);
2564 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2565 stride_build = isl_ast_build_copy(build);
2566 stride_build = isl_ast_build_detect_strides(stride_build,
2567 isl_set_copy(domain));
2568 expansion = isl_ast_build_get_stride_expansion(stride_build);
2570 domain = isl_set_preimage_multi_aff(domain,
2571 isl_multi_aff_copy(expansion));
2572 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2573 isl_ast_build_free(stride_build);
2575 bmap = isl_basic_map_from_multi_aff(expansion);
2577 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2578 if (!lower)
2579 domain = isl_set_free(domain);
2581 if (init && init(n, user) < 0)
2582 domain = isl_set_free(domain);
2583 for (i = 0; i < n; ++i) {
2584 isl_set *set;
2585 isl_basic_set *bset;
2586 isl_constraint *slice;
2588 slice = at_offset(depth, lower, i);
2589 set = isl_set_copy(domain);
2590 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2591 bset = isl_set_unshifted_simple_hull(set);
2592 bset = isl_basic_set_add_constraint(bset, slice);
2593 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2595 if (fn(bset, user) < 0)
2596 break;
2599 isl_aff_free(lower);
2600 isl_set_free(domain);
2601 isl_basic_map_free(bmap);
2603 return i < n ? -1 : 0;
2606 /* Data structure for storing the results and the intermediate objects
2607 * of compute_domains.
2609 * "list" is the main result of the function and contains a list
2610 * of disjoint basic sets for which code should be generated.
2612 * "executed" and "build" are inputs to compute_domains.
2613 * "schedule_domain" is the domain of "executed".
2615 * "option" constains the domains at the current depth that should by
2616 * atomic, separated or unrolled. These domains are as specified by
2617 * the user, except that inner dimensions have been eliminated and
2618 * that they have been made pair-wise disjoint.
2620 * "sep_class" contains the user-specified split into separation classes
2621 * specialized to the current depth.
2622 * "done" contains the union of the separation domains that have already
2623 * been handled.
2625 struct isl_codegen_domains {
2626 isl_basic_set_list *list;
2628 isl_union_map *executed;
2629 isl_ast_build *build;
2630 isl_set *schedule_domain;
2632 isl_set *option[4];
2634 isl_map *sep_class;
2635 isl_set *done;
2638 /* Internal data structure for do_unroll.
2640 * "domains" stores the results of compute_domains.
2641 * "class_domain" is the original class domain passed to do_unroll.
2642 * "unroll_domain" collects the unrolled iterations.
2644 struct isl_ast_unroll_data {
2645 struct isl_codegen_domains *domains;
2646 isl_set *class_domain;
2647 isl_set *unroll_domain;
2650 /* Given an iteration of an unrolled domain represented by "bset",
2651 * add it to data->domains->list.
2652 * Since we may have dropped some constraints, we intersect with
2653 * the class domain again to ensure that each element in the list
2654 * is disjoint from the other class domains.
2656 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2658 struct isl_ast_unroll_data *data = user;
2659 isl_set *set;
2660 isl_basic_set_list *list;
2662 set = isl_set_from_basic_set(bset);
2663 data->unroll_domain = isl_set_union(data->unroll_domain,
2664 isl_set_copy(set));
2665 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2666 set = isl_set_make_disjoint(set);
2667 list = isl_basic_set_list_from_set(set);
2668 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2669 list);
2671 return 0;
2674 /* Extend domains->list with a list of basic sets, one for each value
2675 * of the current dimension in "domain" and remove the corresponding
2676 * sets from the class domain. Return the updated class domain.
2677 * The divs that involve the current dimension have not been projected out
2678 * from this domain.
2680 * We call foreach_iteration to iterate over the individual values and
2681 * in do_unroll_iteration we collect the individual basic sets in
2682 * domains->list and their union in data->unroll_domain, which is then
2683 * used to update the class domain.
2685 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2686 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2688 struct isl_ast_unroll_data data;
2690 if (!domain)
2691 return isl_set_free(class_domain);
2692 if (!class_domain)
2693 return isl_set_free(domain);
2695 data.domains = domains;
2696 data.class_domain = class_domain;
2697 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2699 if (foreach_iteration(domain, domains->build, NULL,
2700 &do_unroll_iteration, &data) < 0)
2701 data.unroll_domain = isl_set_free(data.unroll_domain);
2703 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2705 return class_domain;
2708 /* Add domains to domains->list for each individual value of the current
2709 * dimension, for that part of the schedule domain that lies in the
2710 * intersection of the option domain and the class domain.
2711 * Remove the corresponding sets from the class domain and
2712 * return the updated class domain.
2714 * We first break up the unroll option domain into individual pieces
2715 * and then handle each of them separately. The unroll option domain
2716 * has been made disjoint in compute_domains_init_options,
2718 * Note that we actively want to combine different pieces of the
2719 * schedule domain that have the same value at the current dimension.
2720 * We therefore need to break up the unroll option domain before
2721 * intersecting with class and schedule domain, hoping that the
2722 * unroll option domain specified by the user is relatively simple.
2724 static __isl_give isl_set *compute_unroll_domains(
2725 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2727 isl_set *unroll_domain;
2728 isl_basic_set_list *unroll_list;
2729 int i, n;
2730 int empty;
2732 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2733 if (empty < 0)
2734 return isl_set_free(class_domain);
2735 if (empty)
2736 return class_domain;
2738 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2739 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2741 n = isl_basic_set_list_n_basic_set(unroll_list);
2742 for (i = 0; i < n; ++i) {
2743 isl_basic_set *bset;
2745 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2746 unroll_domain = isl_set_from_basic_set(bset);
2747 unroll_domain = isl_set_intersect(unroll_domain,
2748 isl_set_copy(class_domain));
2749 unroll_domain = isl_set_intersect(unroll_domain,
2750 isl_set_copy(domains->schedule_domain));
2752 empty = isl_set_is_empty(unroll_domain);
2753 if (empty >= 0 && empty) {
2754 isl_set_free(unroll_domain);
2755 continue;
2758 class_domain = do_unroll(domains, unroll_domain, class_domain);
2761 isl_basic_set_list_free(unroll_list);
2763 return class_domain;
2766 /* Try and construct a single basic set that includes the intersection of
2767 * the schedule domain, the atomic option domain and the class domain.
2768 * Add the resulting basic set(s) to domains->list and remove them
2769 * from class_domain. Return the updated class domain.
2771 * We construct a single domain rather than trying to combine
2772 * the schedule domains of individual domains because we are working
2773 * within a single component so that non-overlapping schedule domains
2774 * should already have been separated.
2775 * We do however need to make sure that this single domains is a subset
2776 * of the class domain so that it would not intersect with any other
2777 * class domains. This means that we may end up splitting up the atomic
2778 * domain in case separation classes are being used.
2780 * "domain" is the intersection of the schedule domain and the class domain,
2781 * with inner dimensions projected out.
2783 static __isl_give isl_set *compute_atomic_domain(
2784 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2786 isl_basic_set *bset;
2787 isl_basic_set_list *list;
2788 isl_set *domain, *atomic_domain;
2789 int empty;
2791 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2792 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2793 domain = isl_set_intersect(domain,
2794 isl_set_copy(domains->schedule_domain));
2795 empty = isl_set_is_empty(domain);
2796 if (empty < 0)
2797 class_domain = isl_set_free(class_domain);
2798 if (empty) {
2799 isl_set_free(domain);
2800 return class_domain;
2803 domain = isl_ast_build_eliminate(domains->build, domain);
2804 domain = isl_set_coalesce(domain);
2805 bset = isl_set_unshifted_simple_hull(domain);
2806 domain = isl_set_from_basic_set(bset);
2807 atomic_domain = isl_set_copy(domain);
2808 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2809 class_domain = isl_set_subtract(class_domain, atomic_domain);
2810 domain = isl_set_make_disjoint(domain);
2811 list = isl_basic_set_list_from_set(domain);
2812 domains->list = isl_basic_set_list_concat(domains->list, list);
2814 return class_domain;
2817 /* Split up the schedule domain into uniform basic sets,
2818 * in the sense that each element in a basic set is associated to
2819 * elements of the same domains, and add the result to domains->list.
2820 * Do this for that part of the schedule domain that lies in the
2821 * intersection of "class_domain" and the separate option domain.
2823 * "class_domain" may or may not include the constraints
2824 * of the schedule domain, but this does not make a difference
2825 * since we are going to intersect it with the domain of the inverse schedule.
2826 * If it includes schedule domain constraints, then they may involve
2827 * inner dimensions, but we will eliminate them in separation_domain.
2829 static int compute_separate_domain(struct isl_codegen_domains *domains,
2830 __isl_keep isl_set *class_domain)
2832 isl_space *space;
2833 isl_set *domain;
2834 isl_union_map *executed;
2835 isl_basic_set_list *list;
2836 int empty;
2838 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2839 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2840 executed = isl_union_map_copy(domains->executed);
2841 executed = isl_union_map_intersect_domain(executed,
2842 isl_union_set_from_set(domain));
2843 empty = isl_union_map_is_empty(executed);
2844 if (empty < 0 || empty) {
2845 isl_union_map_free(executed);
2846 return empty < 0 ? -1 : 0;
2849 space = isl_set_get_space(class_domain);
2850 domain = separate_schedule_domains(space, executed, domains->build);
2852 list = isl_basic_set_list_from_set(domain);
2853 domains->list = isl_basic_set_list_concat(domains->list, list);
2855 return 0;
2858 /* Split up the domain at the current depth into disjoint
2859 * basic sets for which code should be generated separately
2860 * for the given separation class domain.
2862 * If any separation classes have been defined, then "class_domain"
2863 * is the domain of the current class and does not refer to inner dimensions.
2864 * Otherwise, "class_domain" is the universe domain.
2866 * We first make sure that the class domain is disjoint from
2867 * previously considered class domains.
2869 * The separate domains can be computed directly from the "class_domain".
2871 * The unroll, atomic and remainder domains need the constraints
2872 * from the schedule domain.
2874 * For unrolling, the actual schedule domain is needed (with divs that
2875 * may refer to the current dimension) so that stride detection can be
2876 * performed.
2878 * For atomic and remainder domains, inner dimensions and divs involving
2879 * the current dimensions should be eliminated.
2880 * In case we are working within a separation class, we need to intersect
2881 * the result with the current "class_domain" to ensure that the domains
2882 * are disjoint from those generated from other class domains.
2884 * The domain that has been made atomic may be larger than specified
2885 * by the user since it needs to be representable as a single basic set.
2886 * This possibly larger domain is removed from class_domain by
2887 * compute_atomic_domain. It is computed first so that the extended domain
2888 * would not overlap with any domains computed before.
2889 * Similary, the unrolled domains may have some constraints removed and
2890 * may therefore also be larger than specified by the user.
2892 * If anything is left after handling separate, unroll and atomic,
2893 * we split it up into basic sets and append the basic sets to domains->list.
2895 static int compute_partial_domains(struct isl_codegen_domains *domains,
2896 __isl_take isl_set *class_domain)
2898 isl_basic_set_list *list;
2899 isl_set *domain;
2901 class_domain = isl_set_subtract(class_domain,
2902 isl_set_copy(domains->done));
2903 domains->done = isl_set_union(domains->done,
2904 isl_set_copy(class_domain));
2906 class_domain = compute_atomic_domain(domains, class_domain);
2907 class_domain = compute_unroll_domains(domains, class_domain);
2909 domain = isl_set_copy(class_domain);
2911 if (compute_separate_domain(domains, domain) < 0)
2912 goto error;
2913 domain = isl_set_subtract(domain,
2914 isl_set_copy(domains->option[isl_ast_loop_separate]));
2916 domain = isl_set_intersect(domain,
2917 isl_set_copy(domains->schedule_domain));
2919 domain = isl_ast_build_eliminate(domains->build, domain);
2920 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2922 domain = isl_set_coalesce(domain);
2923 domain = isl_set_make_disjoint(domain);
2925 list = isl_basic_set_list_from_set(domain);
2926 domains->list = isl_basic_set_list_concat(domains->list, list);
2928 isl_set_free(class_domain);
2930 return 0;
2931 error:
2932 isl_set_free(domain);
2933 isl_set_free(class_domain);
2934 return -1;
2937 /* Split up the domain at the current depth into disjoint
2938 * basic sets for which code should be generated separately
2939 * for the separation class identified by "pnt".
2941 * We extract the corresponding class domain from domains->sep_class,
2942 * eliminate inner dimensions and pass control to compute_partial_domains.
2944 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2946 struct isl_codegen_domains *domains = user;
2947 isl_set *class_set;
2948 isl_set *domain;
2949 int disjoint;
2951 class_set = isl_set_from_point(pnt);
2952 domain = isl_map_domain(isl_map_intersect_range(
2953 isl_map_copy(domains->sep_class), class_set));
2954 domain = isl_ast_build_compute_gist(domains->build, domain);
2955 domain = isl_ast_build_eliminate(domains->build, domain);
2957 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2958 if (disjoint < 0)
2959 return -1;
2960 if (disjoint) {
2961 isl_set_free(domain);
2962 return 0;
2965 return compute_partial_domains(domains, domain);
2968 /* Extract the domains at the current depth that should be atomic,
2969 * separated or unrolled and store them in option.
2971 * The domains specified by the user might overlap, so we make
2972 * them disjoint by subtracting earlier domains from later domains.
2974 static void compute_domains_init_options(isl_set *option[4],
2975 __isl_keep isl_ast_build *build)
2977 enum isl_ast_loop_type type, type2;
2978 isl_set *unroll;
2980 for (type = isl_ast_loop_atomic;
2981 type <= isl_ast_loop_separate; ++type) {
2982 option[type] = isl_ast_build_get_option_domain(build, type);
2983 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
2984 option[type] = isl_set_subtract(option[type],
2985 isl_set_copy(option[type2]));
2988 unroll = option[isl_ast_loop_unroll];
2989 unroll = isl_set_coalesce(unroll);
2990 unroll = isl_set_make_disjoint(unroll);
2991 option[isl_ast_loop_unroll] = unroll;
2994 /* Split up the domain at the current depth into disjoint
2995 * basic sets for which code should be generated separately,
2996 * based on the user-specified options.
2997 * Return the list of disjoint basic sets.
2999 * There are three kinds of domains that we need to keep track of.
3000 * - the "schedule domain" is the domain of "executed"
3001 * - the "class domain" is the domain corresponding to the currrent
3002 * separation class
3003 * - the "option domain" is the domain corresponding to one of the options
3004 * atomic, unroll or separate
3006 * We first consider the individial values of the separation classes
3007 * and split up the domain for each of them separately.
3008 * Finally, we consider the remainder. If no separation classes were
3009 * specified, then we call compute_partial_domains with the universe
3010 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3011 * with inner dimensions removed. We do this because we want to
3012 * avoid computing the complement of the class domains (i.e., the difference
3013 * between the universe and domains->done).
3015 static __isl_give isl_basic_set_list *compute_domains(
3016 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3018 struct isl_codegen_domains domains;
3019 isl_ctx *ctx;
3020 isl_set *domain;
3021 isl_union_set *schedule_domain;
3022 isl_set *classes;
3023 isl_space *space;
3024 int n_param;
3025 enum isl_ast_loop_type type;
3026 int empty;
3028 if (!executed)
3029 return NULL;
3031 ctx = isl_union_map_get_ctx(executed);
3032 domains.list = isl_basic_set_list_alloc(ctx, 0);
3034 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3035 domain = isl_set_from_union_set(schedule_domain);
3037 compute_domains_init_options(domains.option, build);
3039 domains.sep_class = isl_ast_build_get_separation_class(build);
3040 classes = isl_map_range(isl_map_copy(domains.sep_class));
3041 n_param = isl_set_dim(classes, isl_dim_param);
3042 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3044 space = isl_set_get_space(domain);
3045 domains.build = build;
3046 domains.schedule_domain = isl_set_copy(domain);
3047 domains.executed = executed;
3048 domains.done = isl_set_empty(space);
3050 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3051 domains.list = isl_basic_set_list_free(domains.list);
3052 isl_set_free(classes);
3054 empty = isl_set_is_empty(domains.done);
3055 if (empty < 0) {
3056 domains.list = isl_basic_set_list_free(domains.list);
3057 domain = isl_set_free(domain);
3058 } else if (empty) {
3059 isl_set_free(domain);
3060 domain = isl_set_universe(isl_set_get_space(domains.done));
3061 } else {
3062 domain = isl_ast_build_eliminate(build, domain);
3064 if (compute_partial_domains(&domains, domain) < 0)
3065 domains.list = isl_basic_set_list_free(domains.list);
3067 isl_set_free(domains.schedule_domain);
3068 isl_set_free(domains.done);
3069 isl_map_free(domains.sep_class);
3070 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3071 isl_set_free(domains.option[type]);
3073 return domains.list;
3076 /* Generate code for a single component, after shifting (if any)
3077 * has been applied, in case the schedule was specified as a union map.
3079 * We first split up the domain at the current depth into disjoint
3080 * basic sets based on the user-specified options.
3081 * Then we generated code for each of them and concatenate the results.
3083 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3084 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3086 isl_basic_set_list *domain_list;
3087 isl_ast_graft_list *list = NULL;
3089 domain_list = compute_domains(executed, build);
3090 list = generate_parallel_domains(domain_list, executed, build);
3092 isl_basic_set_list_free(domain_list);
3093 isl_union_map_free(executed);
3094 isl_ast_build_free(build);
3096 return list;
3099 /* Generate code for a single component, after shifting (if any)
3100 * has been applied, in case the schedule was specified as a schedule tree
3101 * and the separate option was specified.
3103 * We perform separation on the domain of "executed" and then generate
3104 * an AST for each of the resulting disjoint basic sets.
3106 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3107 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3109 isl_space *space;
3110 isl_set *domain;
3111 isl_basic_set_list *domain_list;
3112 isl_ast_graft_list *list;
3114 space = isl_ast_build_get_space(build, 1);
3115 domain = separate_schedule_domains(space,
3116 isl_union_map_copy(executed), build);
3117 domain_list = isl_basic_set_list_from_set(domain);
3119 list = generate_parallel_domains(domain_list, executed, build);
3121 isl_basic_set_list_free(domain_list);
3122 isl_union_map_free(executed);
3123 isl_ast_build_free(build);
3125 return list;
3128 /* Internal data structure for generate_shifted_component_tree_unroll.
3130 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3131 * "list" collects the constructs grafts.
3133 struct isl_ast_unroll_tree_data {
3134 isl_union_map *executed;
3135 isl_ast_build *build;
3136 isl_ast_graft_list *list;
3139 /* Initialize data->list to a list of "n" elements.
3141 static int init_unroll_tree(int n, void *user)
3143 struct isl_ast_unroll_tree_data *data = user;
3144 isl_ctx *ctx;
3146 ctx = isl_ast_build_get_ctx(data->build);
3147 data->list = isl_ast_graft_list_alloc(ctx, n);
3149 return 0;
3152 /* Given an iteration of an unrolled domain represented by "bset",
3153 * generate the corresponding AST and add the result to data->list.
3155 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3157 struct isl_ast_unroll_tree_data *data = user;
3159 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3160 bset, isl_ast_build_copy(data->build));
3162 return 0;
3165 /* Generate code for a single component, after shifting (if any)
3166 * has been applied, in case the schedule was specified as a schedule tree
3167 * and the unroll option was specified.
3169 * We call foreach_iteration to iterate over the individual values and
3170 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3172 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3173 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3174 __isl_take isl_ast_build *build)
3176 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3178 if (foreach_iteration(domain, build, &init_unroll_tree,
3179 &do_unroll_tree_iteration, &data) < 0)
3180 data.list = isl_ast_graft_list_free(data.list);
3182 isl_union_map_free(executed);
3183 isl_ast_build_free(build);
3185 return data.list;
3188 /* Generate code for a single component, after shifting (if any)
3189 * has been applied, in case the schedule was specified as a schedule tree.
3190 * In particular, handle the base case where there is either no isolated
3191 * set or we are within the isolated set (in which case "isolated" is set)
3192 * or the iterations that precede or follow the isolated set.
3194 * The schedule domain is broken up or combined into basic sets
3195 * according to the AST generation option specified in the current
3196 * schedule node, which may be either atomic, separate, unroll or
3197 * unspecified. If the option is unspecified, then we currently simply
3198 * split the schedule domain into disjoint basic sets.
3200 * In case the separate option is specified, the AST generation is
3201 * handled by generate_shifted_component_tree_separate.
3202 * In the other cases, we need the global schedule domain.
3203 * In the unroll case, the AST generation is then handled by
3204 * generate_shifted_component_tree_unroll which needs the actual
3205 * schedule domain (with divs that may refer to the current dimension)
3206 * so that stride detection can be performed.
3207 * In the atomic or unspecified case, inner dimensions and divs involving
3208 * the current dimensions should be eliminated.
3209 * The result is then either combined into a single basic set or
3210 * split up into disjoint basic sets.
3211 * Finally an AST is generated for each basic set and the results are
3212 * concatenated.
3214 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3215 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3216 int isolated)
3218 isl_union_set *schedule_domain;
3219 isl_set *domain;
3220 isl_basic_set_list *domain_list;
3221 isl_ast_graft_list *list;
3222 enum isl_ast_loop_type type;
3224 type = isl_ast_build_get_loop_type(build, isolated);
3225 if (type < 0)
3226 goto error;
3228 if (type == isl_ast_loop_separate)
3229 return generate_shifted_component_tree_separate(executed,
3230 build);
3232 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3233 domain = isl_set_from_union_set(schedule_domain);
3235 if (type == isl_ast_loop_unroll)
3236 return generate_shifted_component_tree_unroll(executed, domain,
3237 build);
3239 domain = isl_ast_build_eliminate(build, domain);
3240 domain = isl_set_coalesce(domain);
3242 if (type == isl_ast_loop_atomic) {
3243 isl_basic_set *hull;
3244 hull = isl_set_unshifted_simple_hull(domain);
3245 domain_list = isl_basic_set_list_from_basic_set(hull);
3246 } else {
3247 domain = isl_set_make_disjoint(domain);
3248 domain_list = isl_basic_set_list_from_set(domain);
3251 list = generate_parallel_domains(domain_list, executed, build);
3253 isl_basic_set_list_free(domain_list);
3254 isl_union_map_free(executed);
3255 isl_ast_build_free(build);
3257 return list;
3258 error:
3259 isl_union_map_free(executed);
3260 isl_ast_build_free(build);
3261 return NULL;
3264 /* Generate code for a single component, after shifting (if any)
3265 * has been applied, in case the schedule was specified as a schedule tree.
3266 * In particular, do so for the specified subset of the schedule domsain.
3268 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3269 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3270 __isl_keep isl_ast_build *build, int isolated)
3272 isl_union_set *uset;
3273 int empty;
3275 uset = isl_union_set_from_set(domain);
3276 executed = isl_union_map_copy(executed);
3277 executed = isl_union_map_intersect_domain(executed, uset);
3278 empty = isl_union_map_is_empty(executed);
3279 if (empty < 0)
3280 goto error;
3281 if (empty) {
3282 isl_ctx *ctx;
3283 isl_union_map_free(executed);
3284 ctx = isl_ast_build_get_ctx(build);
3285 return isl_ast_graft_list_alloc(ctx, 0);
3288 build = isl_ast_build_copy(build);
3289 return generate_shifted_component_tree_base(executed, build, isolated);
3290 error:
3291 isl_union_map_free(executed);
3292 return NULL;
3295 /* Generate code for a single component, after shifting (if any)
3296 * has been applied, in case the schedule was specified as a schedule tree.
3298 * We first check if the user has specified a (non-empty) isolated
3299 * schedule domain.
3300 * If so, we break up the schedule domain into iterations that
3301 * precede the isolated domain, the isolated domain itself,
3302 * the iterations that follow the isolated domain and
3303 * the remaining iterations (those that are incomparable
3304 * to the isolated domain).
3305 * We generate an AST for each piece and concatenate the results.
3306 * If no isolated set has been specified, then we generate an
3307 * AST for the entire inverse schedule.
3309 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3310 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3312 int i, depth;
3313 int empty, has_isolate;
3314 isl_space *space;
3315 isl_union_set *schedule_domain;
3316 isl_set *domain;
3317 isl_basic_set *hull;
3318 isl_set *isolated, *before, *after;
3319 isl_map *gt, *lt;
3320 isl_ast_graft_list *list, *res;
3322 build = isl_ast_build_extract_isolated(build);
3323 has_isolate = isl_ast_build_has_isolated(build);
3324 if (has_isolate < 0)
3325 executed = isl_union_map_free(executed);
3326 else if (!has_isolate)
3327 return generate_shifted_component_tree_base(executed, build, 0);
3329 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3330 domain = isl_set_from_union_set(schedule_domain);
3332 isolated = isl_ast_build_get_isolated(build);
3333 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3334 empty = isl_set_is_empty(isolated);
3335 if (empty < 0)
3336 goto error;
3337 if (empty) {
3338 isl_set_free(isolated);
3339 isl_set_free(domain);
3340 return generate_shifted_component_tree_base(executed, build, 0);
3342 isolated = isl_ast_build_eliminate(build, isolated);
3343 hull = isl_set_unshifted_simple_hull(isolated);
3344 isolated = isl_set_from_basic_set(hull);
3346 depth = isl_ast_build_get_depth(build);
3347 space = isl_space_map_from_set(isl_set_get_space(isolated));
3348 gt = isl_map_universe(space);
3349 for (i = 0; i < depth; ++i)
3350 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3351 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3352 lt = isl_map_reverse(isl_map_copy(gt));
3353 before = isl_set_apply(isl_set_copy(isolated), gt);
3354 after = isl_set_apply(isl_set_copy(isolated), lt);
3356 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3357 domain = isl_set_subtract(domain, isl_set_copy(before));
3358 domain = isl_set_subtract(domain, isl_set_copy(after));
3359 after = isl_set_subtract(after, isl_set_copy(isolated));
3360 after = isl_set_subtract(after, isl_set_copy(before));
3361 before = isl_set_subtract(before, isl_set_copy(isolated));
3363 res = generate_shifted_component_tree_part(executed, before, build, 0);
3364 list = generate_shifted_component_tree_part(executed, isolated,
3365 build, 1);
3366 res = isl_ast_graft_list_concat(res, list);
3367 list = generate_shifted_component_tree_part(executed, after, build, 0);
3368 res = isl_ast_graft_list_concat(res, list);
3369 list = generate_shifted_component_tree_part(executed, domain, build, 0);
3370 res = isl_ast_graft_list_concat(res, list);
3372 isl_union_map_free(executed);
3373 isl_ast_build_free(build);
3375 return res;
3376 error:
3377 isl_set_free(domain);
3378 isl_set_free(isolated);
3379 isl_union_map_free(executed);
3380 isl_ast_build_free(build);
3381 return NULL;
3384 /* Generate code for a single component, after shifting (if any)
3385 * has been applied.
3387 * Call generate_shifted_component_tree or generate_shifted_component_flat
3388 * depending on whether the schedule was specified as a schedule tree.
3390 static __isl_give isl_ast_graft_list *generate_shifted_component(
3391 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3393 if (isl_ast_build_has_schedule_node(build))
3394 return generate_shifted_component_tree(executed, build);
3395 else
3396 return generate_shifted_component_flat(executed, build);
3399 struct isl_set_map_pair {
3400 isl_set *set;
3401 isl_map *map;
3404 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3405 * of indices into the "domain" array,
3406 * return the union of the "map" fields of the elements
3407 * indexed by the first "n" elements of "order".
3409 static __isl_give isl_union_map *construct_component_executed(
3410 struct isl_set_map_pair *domain, int *order, int n)
3412 int i;
3413 isl_map *map;
3414 isl_union_map *executed;
3416 map = isl_map_copy(domain[order[0]].map);
3417 executed = isl_union_map_from_map(map);
3418 for (i = 1; i < n; ++i) {
3419 map = isl_map_copy(domain[order[i]].map);
3420 executed = isl_union_map_add_map(executed, map);
3423 return executed;
3426 /* Generate code for a single component, after shifting (if any)
3427 * has been applied.
3429 * The component inverse schedule is specified as the "map" fields
3430 * of the elements of "domain" indexed by the first "n" elements of "order".
3432 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3433 struct isl_set_map_pair *domain, int *order, int n,
3434 __isl_take isl_ast_build *build)
3436 isl_union_map *executed;
3438 executed = construct_component_executed(domain, order, n);
3439 return generate_shifted_component(executed, build);
3442 /* Does set dimension "pos" of "set" have an obviously fixed value?
3444 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3446 int fixed;
3447 isl_val *v;
3449 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3450 if (!v)
3451 return -1;
3452 fixed = !isl_val_is_nan(v);
3453 isl_val_free(v);
3455 return fixed;
3458 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3459 * of indices into the "domain" array,
3460 * do all (except for at most one) of the "set" field of the elements
3461 * indexed by the first "n" elements of "order" have a fixed value
3462 * at position "depth"?
3464 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3465 int *order, int n, int depth)
3467 int i;
3468 int non_fixed = -1;
3470 for (i = 0; i < n; ++i) {
3471 int f;
3473 f = dim_is_fixed(domain[order[i]].set, depth);
3474 if (f < 0)
3475 return -1;
3476 if (f)
3477 continue;
3478 if (non_fixed >= 0)
3479 return 0;
3480 non_fixed = i;
3483 return 1;
3486 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3487 * of indices into the "domain" array,
3488 * eliminate the inner dimensions from the "set" field of the elements
3489 * indexed by the first "n" elements of "order", provided the current
3490 * dimension does not have a fixed value.
3492 * Return the index of the first element in "order" with a corresponding
3493 * "set" field that does not have an (obviously) fixed value.
3495 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3496 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3498 int i;
3499 int base = -1;
3501 for (i = n - 1; i >= 0; --i) {
3502 int f;
3503 f = dim_is_fixed(domain[order[i]].set, depth);
3504 if (f < 0)
3505 return -1;
3506 if (f)
3507 continue;
3508 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3509 domain[order[i]].set);
3510 base = i;
3513 return base;
3516 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3517 * of indices into the "domain" array,
3518 * find the element of "domain" (amongst those indexed by the first "n"
3519 * elements of "order") with the "set" field that has the smallest
3520 * value for the current iterator.
3522 * Note that the domain with the smallest value may depend on the parameters
3523 * and/or outer loop dimension. Since the result of this function is only
3524 * used as heuristic, we only make a reasonable attempt at finding the best
3525 * domain, one that should work in case a single domain provides the smallest
3526 * value for the current dimension over all values of the parameters
3527 * and outer dimensions.
3529 * In particular, we compute the smallest value of the first domain
3530 * and replace it by that of any later domain if that later domain
3531 * has a smallest value that is smaller for at least some value
3532 * of the parameters and outer dimensions.
3534 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3535 __isl_keep isl_ast_build *build)
3537 int i;
3538 isl_map *min_first;
3539 int first = 0;
3541 min_first = isl_ast_build_map_to_iterator(build,
3542 isl_set_copy(domain[order[0]].set));
3543 min_first = isl_map_lexmin(min_first);
3545 for (i = 1; i < n; ++i) {
3546 isl_map *min, *test;
3547 int empty;
3549 min = isl_ast_build_map_to_iterator(build,
3550 isl_set_copy(domain[order[i]].set));
3551 min = isl_map_lexmin(min);
3552 test = isl_map_copy(min);
3553 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3554 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3555 empty = isl_map_is_empty(test);
3556 isl_map_free(test);
3557 if (empty >= 0 && !empty) {
3558 isl_map_free(min_first);
3559 first = i;
3560 min_first = min;
3561 } else
3562 isl_map_free(min);
3564 if (empty < 0)
3565 break;
3568 isl_map_free(min_first);
3570 return i < n ? -1 : first;
3573 /* Construct a shifted inverse schedule based on the original inverse schedule,
3574 * the stride and the offset.
3576 * The original inverse schedule is specified as the "map" fields
3577 * of the elements of "domain" indexed by the first "n" elements of "order".
3579 * "stride" and "offset" are such that the difference
3580 * between the values of the current dimension of domain "i"
3581 * and the values of the current dimension for some reference domain are
3582 * equal to
3584 * stride * integer + offset[i]
3586 * Moreover, 0 <= offset[i] < stride.
3588 * For each domain, we create a map
3590 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3592 * where j refers to the current dimension and the other dimensions are
3593 * unchanged, and apply this map to the original schedule domain.
3595 * For example, for the original schedule
3597 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3599 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3600 * we apply the mapping
3602 * { [j] -> [j, 0] }
3604 * to the schedule of the "A" domain and the mapping
3606 * { [j - 1] -> [j, 1] }
3608 * to the schedule of the "B" domain.
3611 * Note that after the transformation, the differences between pairs
3612 * of values of the current dimension over all domains are multiples
3613 * of stride and that we have therefore exposed the stride.
3616 * To see that the mapping preserves the lexicographic order,
3617 * first note that each of the individual maps above preserves the order.
3618 * If the value of the current iterator is j1 in one domain and j2 in another,
3619 * then if j1 = j2, we know that the same map is applied to both domains
3620 * and the order is preserved.
3621 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3622 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3624 * j1 - c1 < j2 - c2
3626 * and the order is preserved.
3627 * If c1 < c2, then we know
3629 * 0 <= c2 - c1 < s
3631 * We also have
3633 * j2 - j1 = n * s + r
3635 * with n >= 0 and 0 <= r < s.
3636 * In other words, r = c2 - c1.
3637 * If n > 0, then
3639 * j1 - c1 < j2 - c2
3641 * If n = 0, then
3643 * j1 - c1 = j2 - c2
3645 * and so
3647 * (j1 - c1, c1) << (j2 - c2, c2)
3649 * with "<<" the lexicographic order, proving that the order is preserved
3650 * in all cases.
3652 static __isl_give isl_union_map *contruct_shifted_executed(
3653 struct isl_set_map_pair *domain, int *order, int n,
3654 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3655 __isl_take isl_ast_build *build)
3657 int i;
3658 isl_union_map *executed;
3659 isl_space *space;
3660 isl_map *map;
3661 int depth;
3662 isl_constraint *c;
3664 depth = isl_ast_build_get_depth(build);
3665 space = isl_ast_build_get_space(build, 1);
3666 executed = isl_union_map_empty(isl_space_copy(space));
3667 space = isl_space_map_from_set(space);
3668 map = isl_map_identity(isl_space_copy(space));
3669 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3670 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3671 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3673 c = isl_equality_alloc(isl_local_space_from_space(space));
3674 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3675 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3677 for (i = 0; i < n; ++i) {
3678 isl_map *map_i;
3679 isl_val *v;
3681 v = isl_multi_val_get_val(offset, i);
3682 if (!v)
3683 break;
3684 map_i = isl_map_copy(map);
3685 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3686 isl_val_copy(v));
3687 v = isl_val_neg(v);
3688 c = isl_constraint_set_constant_val(c, v);
3689 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3691 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3692 map_i);
3693 executed = isl_union_map_add_map(executed, map_i);
3696 isl_constraint_free(c);
3697 isl_map_free(map);
3699 if (i < n)
3700 executed = isl_union_map_free(executed);
3702 return executed;
3705 /* Generate code for a single component, after exposing the stride,
3706 * given that the schedule domain is "shifted strided".
3708 * The component inverse schedule is specified as the "map" fields
3709 * of the elements of "domain" indexed by the first "n" elements of "order".
3711 * The schedule domain being "shifted strided" means that the differences
3712 * between the values of the current dimension of domain "i"
3713 * and the values of the current dimension for some reference domain are
3714 * equal to
3716 * stride * integer + offset[i]
3718 * We first look for the domain with the "smallest" value for the current
3719 * dimension and adjust the offsets such that the offset of the "smallest"
3720 * domain is equal to zero. The other offsets are reduced modulo stride.
3722 * Based on this information, we construct a new inverse schedule in
3723 * contruct_shifted_executed that exposes the stride.
3724 * Since this involves the introduction of a new schedule dimension,
3725 * the build needs to be changed accodingly.
3726 * After computing the AST, the newly introduced dimension needs
3727 * to be removed again from the list of grafts. We do this by plugging
3728 * in a mapping that represents the new schedule domain in terms of the
3729 * old schedule domain.
3731 static __isl_give isl_ast_graft_list *generate_shift_component(
3732 struct isl_set_map_pair *domain, int *order, int n,
3733 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3734 __isl_take isl_ast_build *build)
3736 isl_ast_graft_list *list;
3737 int first;
3738 int depth;
3739 isl_val *val;
3740 isl_multi_val *mv;
3741 isl_space *space;
3742 isl_multi_aff *ma, *zero;
3743 isl_union_map *executed;
3745 depth = isl_ast_build_get_depth(build);
3747 first = first_offset(domain, order, n, build);
3748 if (first < 0)
3749 goto error;
3751 mv = isl_multi_val_copy(offset);
3752 val = isl_multi_val_get_val(offset, first);
3753 val = isl_val_neg(val);
3754 mv = isl_multi_val_add_val(mv, val);
3755 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3757 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3758 build);
3759 space = isl_ast_build_get_space(build, 1);
3760 space = isl_space_map_from_set(space);
3761 ma = isl_multi_aff_identity(isl_space_copy(space));
3762 space = isl_space_from_domain(isl_space_domain(space));
3763 space = isl_space_add_dims(space, isl_dim_out, 1);
3764 zero = isl_multi_aff_zero(space);
3765 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3766 build = isl_ast_build_insert_dim(build, depth + 1);
3767 list = generate_shifted_component(executed, build);
3769 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3771 isl_multi_val_free(mv);
3773 return list;
3774 error:
3775 isl_ast_build_free(build);
3776 return NULL;
3779 /* Does any node in the schedule tree rooted at the current schedule node
3780 * of "build" depend on outer schedule nodes?
3782 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
3784 isl_schedule_node *node;
3785 int dependent = 0;
3787 node = isl_ast_build_get_schedule_node(build);
3788 dependent = isl_schedule_node_is_subtree_anchored(node);
3789 isl_schedule_node_free(node);
3791 return dependent;
3794 /* Generate code for a single component.
3796 * The component inverse schedule is specified as the "map" fields
3797 * of the elements of "domain" indexed by the first "n" elements of "order".
3799 * This function may modify the "set" fields of "domain".
3801 * Before proceeding with the actual code generation for the component,
3802 * we first check if there are any "shifted" strides, meaning that
3803 * the schedule domains of the individual domains are all strided,
3804 * but that they have different offsets, resulting in the union
3805 * of schedule domains not being strided anymore.
3807 * The simplest example is the schedule
3809 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3811 * Both schedule domains are strided, but their union is not.
3812 * This function detects such cases and then rewrites the schedule to
3814 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3816 * In the new schedule, the schedule domains have the same offset (modulo
3817 * the stride), ensuring that the union of schedule domains is also strided.
3820 * If there is only a single domain in the component, then there is
3821 * nothing to do. Similarly, if the current schedule dimension has
3822 * a fixed value for almost all domains then there is nothing to be done.
3823 * In particular, we need at least two domains where the current schedule
3824 * dimension does not have a fixed value.
3825 * Finally, in case of a schedule map input,
3826 * if any of the options refer to the current schedule dimension,
3827 * then we bail out as well. It would be possible to reformulate the options
3828 * in terms of the new schedule domain, but that would introduce constraints
3829 * that separate the domains in the options and that is something we would
3830 * like to avoid.
3831 * In the case of a schedule tree input, we bail out if any of
3832 * the descendants of the current schedule node refer to outer
3833 * schedule nodes in any way.
3836 * To see if there is any shifted stride, we look at the differences
3837 * between the values of the current dimension in pairs of domains
3838 * for equal values of outer dimensions. These differences should be
3839 * of the form
3841 * m x + r
3843 * with "m" the stride and "r" a constant. Note that we cannot perform
3844 * this analysis on individual domains as the lower bound in each domain
3845 * may depend on parameters or outer dimensions and so the current dimension
3846 * itself may not have a fixed remainder on division by the stride.
3848 * In particular, we compare the first domain that does not have an
3849 * obviously fixed value for the current dimension to itself and all
3850 * other domains and collect the offsets and the gcd of the strides.
3851 * If the gcd becomes one, then we failed to find shifted strides.
3852 * If the gcd is zero, then the differences were all fixed, meaning
3853 * that some domains had non-obviously fixed values for the current dimension.
3854 * If all the offsets are the same (for those domains that do not have
3855 * an obviously fixed value for the current dimension), then we do not
3856 * apply the transformation.
3857 * If none of the domains were skipped, then there is nothing to do.
3858 * If some of them were skipped, then if we apply separation, the schedule
3859 * domain should get split in pieces with a (non-shifted) stride.
3861 * Otherwise, we apply a shift to expose the stride in
3862 * generate_shift_component.
3864 static __isl_give isl_ast_graft_list *generate_component(
3865 struct isl_set_map_pair *domain, int *order, int n,
3866 __isl_take isl_ast_build *build)
3868 int i, d;
3869 int depth;
3870 isl_ctx *ctx;
3871 isl_map *map;
3872 isl_set *deltas;
3873 isl_val *gcd = NULL;
3874 isl_multi_val *mv;
3875 int fixed, skip;
3876 int base;
3877 isl_ast_graft_list *list;
3878 int res = 0;
3880 depth = isl_ast_build_get_depth(build);
3882 skip = n == 1;
3883 if (skip >= 0 && !skip)
3884 skip = at_most_one_non_fixed(domain, order, n, depth);
3885 if (skip >= 0 && !skip) {
3886 if (isl_ast_build_has_schedule_node(build))
3887 skip = has_anchored_subtree(build);
3888 else
3889 skip = isl_ast_build_options_involve_depth(build);
3891 if (skip < 0)
3892 goto error;
3893 if (skip)
3894 return generate_shifted_component_from_list(domain,
3895 order, n, build);
3897 base = eliminate_non_fixed(domain, order, n, depth, build);
3898 if (base < 0)
3899 goto error;
3901 ctx = isl_ast_build_get_ctx(build);
3903 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3905 fixed = 1;
3906 for (i = 0; i < n; ++i) {
3907 isl_val *r, *m;
3909 map = isl_map_from_domain_and_range(
3910 isl_set_copy(domain[order[base]].set),
3911 isl_set_copy(domain[order[i]].set));
3912 for (d = 0; d < depth; ++d)
3913 map = isl_map_equate(map, isl_dim_in, d,
3914 isl_dim_out, d);
3915 deltas = isl_map_deltas(map);
3916 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3917 isl_set_free(deltas);
3918 if (res < 0)
3919 break;
3921 if (i == 0)
3922 gcd = m;
3923 else
3924 gcd = isl_val_gcd(gcd, m);
3925 if (isl_val_is_one(gcd)) {
3926 isl_val_free(r);
3927 break;
3929 mv = isl_multi_val_set_val(mv, i, r);
3931 res = dim_is_fixed(domain[order[i]].set, depth);
3932 if (res < 0)
3933 break;
3934 if (res)
3935 continue;
3937 if (fixed && i > base) {
3938 isl_val *a, *b;
3939 a = isl_multi_val_get_val(mv, i);
3940 b = isl_multi_val_get_val(mv, base);
3941 if (isl_val_ne(a, b))
3942 fixed = 0;
3943 isl_val_free(a);
3944 isl_val_free(b);
3948 if (res < 0 || !gcd) {
3949 isl_ast_build_free(build);
3950 list = NULL;
3951 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3952 list = generate_shifted_component_from_list(domain,
3953 order, n, build);
3954 } else {
3955 list = generate_shift_component(domain, order, n, gcd, mv,
3956 build);
3959 isl_val_free(gcd);
3960 isl_multi_val_free(mv);
3962 return list;
3963 error:
3964 isl_ast_build_free(build);
3965 return NULL;
3968 /* Store both "map" itself and its domain in the
3969 * structure pointed to by *next and advance to the next array element.
3971 static int extract_domain(__isl_take isl_map *map, void *user)
3973 struct isl_set_map_pair **next = user;
3975 (*next)->map = isl_map_copy(map);
3976 (*next)->set = isl_map_domain(map);
3977 (*next)++;
3979 return 0;
3982 static int after_in_tree(__isl_keep isl_union_map *umap,
3983 __isl_keep isl_schedule_node *node);
3985 /* Is any domain element of "umap" scheduled after any of
3986 * the corresponding image elements by the tree rooted at
3987 * the child of "node"?
3989 static int after_in_child(__isl_keep isl_union_map *umap,
3990 __isl_keep isl_schedule_node *node)
3992 isl_schedule_node *child;
3993 int after;
3995 child = isl_schedule_node_get_child(node, 0);
3996 after = after_in_tree(umap, child);
3997 isl_schedule_node_free(child);
3999 return after;
4002 /* Is any domain element of "umap" scheduled after any of
4003 * the corresponding image elements by the tree rooted at
4004 * the band node "node"?
4006 * We first check if any domain element is scheduled after any
4007 * of the corresponding image elements by the band node itself.
4008 * If not, we restrict "map" to those pairs of element that
4009 * are scheduled together by the band node and continue with
4010 * the child of the band node.
4011 * If there are no such pairs then the map passed to after_in_child
4012 * will be empty causing it to return 0.
4014 static int after_in_band(__isl_keep isl_union_map *umap,
4015 __isl_keep isl_schedule_node *node)
4017 isl_multi_union_pw_aff *mupa;
4018 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4019 isl_union_set *domain, *range;
4020 isl_space *space;
4021 int empty;
4022 int after;
4024 if (isl_schedule_node_band_n_member(node) == 0)
4025 return after_in_child(umap, node);
4027 mupa = isl_schedule_node_band_get_partial_schedule(node);
4028 space = isl_multi_union_pw_aff_get_space(mupa);
4029 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4030 test = isl_union_map_copy(umap);
4031 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4032 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4033 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4034 test = isl_union_map_intersect(test, gt);
4035 empty = isl_union_map_is_empty(test);
4036 isl_union_map_free(test);
4038 if (empty < 0 || !empty) {
4039 isl_union_map_free(partial);
4040 return empty < 0 ? -1 : 1;
4043 universe = isl_union_map_universe(isl_union_map_copy(umap));
4044 domain = isl_union_map_domain(isl_union_map_copy(universe));
4045 range = isl_union_map_range(universe);
4046 umap1 = isl_union_map_copy(partial);
4047 umap1 = isl_union_map_intersect_domain(umap1, domain);
4048 umap2 = isl_union_map_intersect_domain(partial, range);
4049 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4050 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4051 after = after_in_child(test, node);
4052 isl_union_map_free(test);
4053 return after;
4056 /* Is any domain element of "umap" scheduled after any of
4057 * the corresponding image elements by the tree rooted at
4058 * the context node "node"?
4060 * The context constraints apply to the schedule domain,
4061 * so we cannot apply them directly to "umap", which contains
4062 * pairs of statement instances. Instead, we add them
4063 * to the range of the prefix schedule for both domain and
4064 * range of "umap".
4066 static int after_in_context(__isl_keep isl_union_map *umap,
4067 __isl_keep isl_schedule_node *node)
4069 isl_union_map *prefix, *universe, *umap1, *umap2;
4070 isl_union_set *domain, *range;
4071 isl_set *context;
4072 int after;
4074 umap = isl_union_map_copy(umap);
4075 context = isl_schedule_node_context_get_context(node);
4076 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4077 universe = isl_union_map_universe(isl_union_map_copy(umap));
4078 domain = isl_union_map_domain(isl_union_map_copy(universe));
4079 range = isl_union_map_range(universe);
4080 umap1 = isl_union_map_copy(prefix);
4081 umap1 = isl_union_map_intersect_domain(umap1, domain);
4082 umap2 = isl_union_map_intersect_domain(prefix, range);
4083 umap1 = isl_union_map_intersect_range(umap1,
4084 isl_union_set_from_set(context));
4085 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4086 umap = isl_union_map_intersect(umap, umap1);
4088 after = after_in_child(umap, node);
4090 isl_union_map_free(umap);
4092 return after;
4095 /* Is any domain element of "umap" scheduled after any of
4096 * the corresponding image elements by the tree rooted at
4097 * the expansion node "node"?
4099 * We apply the expansion to domain and range of "umap" and
4100 * continue with its child.
4102 static int after_in_expansion(__isl_keep isl_union_map *umap,
4103 __isl_keep isl_schedule_node *node)
4105 isl_union_map *expansion;
4106 int after;
4108 expansion = isl_schedule_node_expansion_get_expansion(node);
4109 umap = isl_union_map_copy(umap);
4110 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4111 umap = isl_union_map_apply_range(umap, expansion);
4113 after = after_in_child(umap, node);
4115 isl_union_map_free(umap);
4117 return after;
4120 /* Is any domain element of "umap" scheduled after any of
4121 * the corresponding image elements by the tree rooted at
4122 * the extension node "node"?
4124 * Since the extension node may add statement instances before or
4125 * after the pairs of statement instances in "umap", we return 1
4126 * to ensure that these pairs are not broken up.
4128 static int after_in_extension(__isl_keep isl_union_map *umap,
4129 __isl_keep isl_schedule_node *node)
4131 return 1;
4134 /* Is any domain element of "umap" scheduled after any of
4135 * the corresponding image elements by the tree rooted at
4136 * the filter node "node"?
4138 * We intersect domain and range of "umap" with the filter and
4139 * continue with its child.
4141 static int after_in_filter(__isl_keep isl_union_map *umap,
4142 __isl_keep isl_schedule_node *node)
4144 isl_union_set *filter;
4145 int after;
4147 umap = isl_union_map_copy(umap);
4148 filter = isl_schedule_node_filter_get_filter(node);
4149 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4150 umap = isl_union_map_intersect_range(umap, filter);
4152 after = after_in_child(umap, node);
4154 isl_union_map_free(umap);
4156 return after;
4159 /* Is any domain element of "umap" scheduled after any of
4160 * the corresponding image elements by the tree rooted at
4161 * the set node "node"?
4163 * This is only the case if this condition holds in any
4164 * of the (filter) children of the set node.
4165 * In particular, if the domain and the range of "umap"
4166 * are contained in different children, then the condition
4167 * does not hold.
4169 static int after_in_set(__isl_keep isl_union_map *umap,
4170 __isl_keep isl_schedule_node *node)
4172 int i, n;
4174 n = isl_schedule_node_n_children(node);
4175 for (i = 0; i < n; ++i) {
4176 isl_schedule_node *child;
4177 int after;
4179 child = isl_schedule_node_get_child(node, i);
4180 after = after_in_tree(umap, child);
4181 isl_schedule_node_free(child);
4183 if (after < 0 || after)
4184 return after;
4187 return 0;
4190 /* Return the filter of child "i" of "node".
4192 static __isl_give isl_union_set *child_filter(
4193 __isl_keep isl_schedule_node *node, int i)
4195 isl_schedule_node *child;
4196 isl_union_set *filter;
4198 child = isl_schedule_node_get_child(node, i);
4199 filter = isl_schedule_node_filter_get_filter(child);
4200 isl_schedule_node_free(child);
4202 return filter;
4205 /* Is any domain element of "umap" scheduled after any of
4206 * the corresponding image elements by the tree rooted at
4207 * the sequence node "node"?
4209 * This happens in particular if any domain element is
4210 * contained in a later child than one containing a range element or
4211 * if the condition holds within a given child in the sequence.
4212 * The later part of the condition is checked by after_in_set.
4214 static int after_in_sequence(__isl_keep isl_union_map *umap,
4215 __isl_keep isl_schedule_node *node)
4217 int i, j, n;
4218 isl_union_map *umap_i;
4219 int empty, after = 0;
4221 n = isl_schedule_node_n_children(node);
4222 for (i = 1; i < n; ++i) {
4223 isl_union_set *filter_i;
4225 umap_i = isl_union_map_copy(umap);
4226 filter_i = child_filter(node, i);
4227 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4228 empty = isl_union_map_is_empty(umap_i);
4229 if (empty < 0)
4230 goto error;
4231 if (empty) {
4232 isl_union_map_free(umap_i);
4233 continue;
4236 for (j = 0; j < i; ++j) {
4237 isl_union_set *filter_j;
4238 isl_union_map *umap_ij;
4240 umap_ij = isl_union_map_copy(umap_i);
4241 filter_j = child_filter(node, j);
4242 umap_ij = isl_union_map_intersect_range(umap_ij,
4243 filter_j);
4244 empty = isl_union_map_is_empty(umap_ij);
4245 isl_union_map_free(umap_ij);
4247 if (empty < 0)
4248 goto error;
4249 if (!empty)
4250 after = 1;
4251 if (after)
4252 break;
4255 isl_union_map_free(umap_i);
4256 if (after)
4257 break;
4260 if (after < 0 || after)
4261 return after;
4263 return after_in_set(umap, node);
4264 error:
4265 isl_union_map_free(umap_i);
4266 return -1;
4269 /* Is any domain element of "umap" scheduled after any of
4270 * the corresponding image elements by the tree rooted at "node"?
4272 * If "umap" is empty, then clearly there is no such element.
4273 * Otherwise, consider the different types of nodes separately.
4275 static int after_in_tree(__isl_keep isl_union_map *umap,
4276 __isl_keep isl_schedule_node *node)
4278 int empty;
4279 enum isl_schedule_node_type type;
4281 empty = isl_union_map_is_empty(umap);
4282 if (empty < 0)
4283 return -1;
4284 if (empty)
4285 return 0;
4286 if (!node)
4287 return -1;
4289 type = isl_schedule_node_get_type(node);
4290 switch (type) {
4291 case isl_schedule_node_error:
4292 return -1;
4293 case isl_schedule_node_leaf:
4294 return 0;
4295 case isl_schedule_node_band:
4296 return after_in_band(umap, node);
4297 case isl_schedule_node_domain:
4298 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4299 "unexpected internal domain node", return -1);
4300 case isl_schedule_node_context:
4301 return after_in_context(umap, node);
4302 case isl_schedule_node_expansion:
4303 return after_in_expansion(umap, node);
4304 case isl_schedule_node_extension:
4305 return after_in_extension(umap, node);
4306 case isl_schedule_node_filter:
4307 return after_in_filter(umap, node);
4308 case isl_schedule_node_guard:
4309 case isl_schedule_node_mark:
4310 return after_in_child(umap, node);
4311 case isl_schedule_node_set:
4312 return after_in_set(umap, node);
4313 case isl_schedule_node_sequence:
4314 return after_in_sequence(umap, node);
4317 return 1;
4320 /* Is any domain element of "map1" scheduled after any domain
4321 * element of "map2" by the subtree underneath the current band node,
4322 * while at the same time being scheduled together by the current
4323 * band node, i.e., by "map1" and "map2?
4325 * If the child of the current band node is a leaf, then
4326 * no element can be scheduled after any other element.
4328 * Otherwise, we construct a relation between domain elements
4329 * of "map1" and domain elements of "map2" that are scheduled
4330 * together and then check if the subtree underneath the current
4331 * band node determines their relative order.
4333 static int after_in_subtree(__isl_keep isl_ast_build *build,
4334 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4336 isl_schedule_node *node;
4337 isl_map *map;
4338 isl_union_map *umap;
4339 int after;
4341 node = isl_ast_build_get_schedule_node(build);
4342 if (!node)
4343 return -1;
4344 node = isl_schedule_node_child(node, 0);
4345 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4346 isl_schedule_node_free(node);
4347 return 0;
4349 map = isl_map_copy(map2);
4350 map = isl_map_apply_domain(map, isl_map_copy(map1));
4351 umap = isl_union_map_from_map(map);
4352 after = after_in_tree(umap, node);
4353 isl_union_map_free(umap);
4354 isl_schedule_node_free(node);
4355 return after;
4358 /* Internal data for any_scheduled_after.
4360 * "build" is the build in which the AST is constructed.
4361 * "depth" is the number of loops that have already been generated
4362 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4363 * "domain" is an array of set-map pairs corresponding to the different
4364 * iteration domains. The set is the schedule domain, i.e., the domain
4365 * of the inverse schedule, while the map is the inverse schedule itself.
4367 struct isl_any_scheduled_after_data {
4368 isl_ast_build *build;
4369 int depth;
4370 int group_coscheduled;
4371 struct isl_set_map_pair *domain;
4374 /* Is any element of domain "i" scheduled after any element of domain "j"
4375 * (for a common iteration of the first data->depth loops)?
4377 * data->domain[i].set contains the domain of the inverse schedule
4378 * for domain "i", i.e., elements in the schedule domain.
4380 * If we are inside a band of a schedule tree and there is a pair
4381 * of elements in the two domains that is schedule together by
4382 * the current band, then we check if any element of "i" may be schedule
4383 * after element of "j" by the descendants of the band node.
4385 * If data->group_coscheduled is set, then we also return 1 if there
4386 * is any pair of elements in the two domains that are scheduled together.
4388 static int any_scheduled_after(int i, int j, void *user)
4390 struct isl_any_scheduled_after_data *data = user;
4391 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4392 int pos;
4394 for (pos = data->depth; pos < dim; ++pos) {
4395 int follows;
4397 follows = isl_set_follows_at(data->domain[i].set,
4398 data->domain[j].set, pos);
4400 if (follows < -1)
4401 return -1;
4402 if (follows > 0)
4403 return 1;
4404 if (follows < 0)
4405 return 0;
4408 if (isl_ast_build_has_schedule_node(data->build)) {
4409 int after;
4411 after = after_in_subtree(data->build, data->domain[i].map,
4412 data->domain[j].map);
4413 if (after < 0 || after)
4414 return after;
4417 return data->group_coscheduled;
4420 /* Look for independent components at the current depth and generate code
4421 * for each component separately. The resulting lists of grafts are
4422 * merged in an attempt to combine grafts with identical guards.
4424 * Code for two domains can be generated separately if all the elements
4425 * of one domain are scheduled before (or together with) all the elements
4426 * of the other domain. We therefore consider the graph with as nodes
4427 * the domains and an edge between two nodes if any element of the first
4428 * node is scheduled after any element of the second node.
4429 * If the ast_build_group_coscheduled is set, then we also add an edge if
4430 * there is any pair of elements in the two domains that are scheduled
4431 * together.
4432 * Code is then generated (by generate_component)
4433 * for each of the strongly connected components in this graph
4434 * in their topological order.
4436 * Since the test is performed on the domain of the inverse schedules of
4437 * the different domains, we precompute these domains and store
4438 * them in data.domain.
4440 static __isl_give isl_ast_graft_list *generate_components(
4441 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4443 int i;
4444 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4445 int n = isl_union_map_n_map(executed);
4446 struct isl_any_scheduled_after_data data;
4447 struct isl_set_map_pair *next;
4448 struct isl_tarjan_graph *g = NULL;
4449 isl_ast_graft_list *list = NULL;
4450 int n_domain = 0;
4452 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4453 if (!data.domain)
4454 goto error;
4455 n_domain = n;
4457 next = data.domain;
4458 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4459 goto error;
4461 if (!build)
4462 goto error;
4463 data.build = build;
4464 data.depth = isl_ast_build_get_depth(build);
4465 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4466 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4467 if (!g)
4468 goto error;
4470 list = isl_ast_graft_list_alloc(ctx, 0);
4472 i = 0;
4473 while (list && n) {
4474 isl_ast_graft_list *list_c;
4475 int first = i;
4477 if (g->order[i] == -1)
4478 isl_die(ctx, isl_error_internal, "cannot happen",
4479 goto error);
4480 ++i; --n;
4481 while (g->order[i] != -1) {
4482 ++i; --n;
4485 list_c = generate_component(data.domain,
4486 g->order + first, i - first,
4487 isl_ast_build_copy(build));
4488 list = isl_ast_graft_list_merge(list, list_c, build);
4490 ++i;
4493 if (0)
4494 error: list = isl_ast_graft_list_free(list);
4495 isl_tarjan_graph_free(g);
4496 for (i = 0; i < n_domain; ++i) {
4497 isl_map_free(data.domain[i].map);
4498 isl_set_free(data.domain[i].set);
4500 free(data.domain);
4501 isl_union_map_free(executed);
4502 isl_ast_build_free(build);
4504 return list;
4507 /* Generate code for the next level (and all inner levels).
4509 * If "executed" is empty, i.e., no code needs to be generated,
4510 * then we return an empty list.
4512 * If we have already generated code for all loop levels, then we pass
4513 * control to generate_inner_level.
4515 * If "executed" lives in a single space, i.e., if code needs to be
4516 * generated for a single domain, then there can only be a single
4517 * component and we go directly to generate_shifted_component.
4518 * Otherwise, we call generate_components to detect the components
4519 * and to call generate_component on each of them separately.
4521 static __isl_give isl_ast_graft_list *generate_next_level(
4522 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4524 int depth;
4526 if (!build || !executed)
4527 goto error;
4529 if (isl_union_map_is_empty(executed)) {
4530 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4531 isl_union_map_free(executed);
4532 isl_ast_build_free(build);
4533 return isl_ast_graft_list_alloc(ctx, 0);
4536 depth = isl_ast_build_get_depth(build);
4537 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4538 return generate_inner_level(executed, build);
4540 if (isl_union_map_n_map(executed) == 1)
4541 return generate_shifted_component(executed, build);
4543 return generate_components(executed, build);
4544 error:
4545 isl_union_map_free(executed);
4546 isl_ast_build_free(build);
4547 return NULL;
4550 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4551 * internal, executed and build are the inputs to generate_code.
4552 * list collects the output.
4554 struct isl_generate_code_data {
4555 int internal;
4556 isl_union_map *executed;
4557 isl_ast_build *build;
4559 isl_ast_graft_list *list;
4562 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4564 * [E -> S] -> D
4566 * with E the external build schedule and S the additional schedule "space",
4567 * reformulate the inverse schedule in terms of the internal schedule domain,
4568 * i.e., return
4570 * [I -> S] -> D
4572 * We first obtain a mapping
4574 * I -> E
4576 * take the inverse and the product with S -> S, resulting in
4578 * [I -> S] -> [E -> S]
4580 * Applying the map to the input produces the desired result.
4582 static __isl_give isl_union_map *internal_executed(
4583 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4584 __isl_keep isl_ast_build *build)
4586 isl_map *id, *proj;
4588 proj = isl_ast_build_get_schedule_map(build);
4589 proj = isl_map_reverse(proj);
4590 space = isl_space_map_from_set(isl_space_copy(space));
4591 id = isl_map_identity(space);
4592 proj = isl_map_product(proj, id);
4593 executed = isl_union_map_apply_domain(executed,
4594 isl_union_map_from_map(proj));
4595 return executed;
4598 /* Generate an AST that visits the elements in the range of data->executed
4599 * in the relative order specified by the corresponding domain element(s)
4600 * for those domain elements that belong to "set".
4601 * Add the result to data->list.
4603 * The caller ensures that "set" is a universe domain.
4604 * "space" is the space of the additional part of the schedule.
4605 * It is equal to the space of "set" if build->domain is parametric.
4606 * Otherwise, it is equal to the range of the wrapped space of "set".
4608 * If the build space is not parametric and
4609 * if isl_ast_build_node_from_schedule_map
4610 * was called from an outside user (data->internal not set), then
4611 * the (inverse) schedule refers to the external build domain and needs to
4612 * be transformed to refer to the internal build domain.
4614 * If the build space is parametric, then we add some of the parameter
4615 * constraints to the executed relation. Adding these constraints
4616 * allows for an earlier detection of conflicts in some cases.
4617 * However, we do not want to divide the executed relation into
4618 * more disjuncts than necessary. We therefore approximate
4619 * the constraints on the parameters by a single disjunct set.
4621 * The build is extended to include the additional part of the schedule.
4622 * If the original build space was not parametric, then the options
4623 * in data->build refer only to the additional part of the schedule
4624 * and they need to be adjusted to refer to the complete AST build
4625 * domain.
4627 * After having adjusted inverse schedule and build, we start generating
4628 * code with the outer loop of the current code generation
4629 * in generate_next_level.
4631 * If the original build space was not parametric, we undo the embedding
4632 * on the resulting isl_ast_node_list so that it can be used within
4633 * the outer AST build.
4635 static int generate_code_in_space(struct isl_generate_code_data *data,
4636 __isl_take isl_set *set, __isl_take isl_space *space)
4638 isl_union_map *executed;
4639 isl_ast_build *build;
4640 isl_ast_graft_list *list;
4641 int embed;
4643 executed = isl_union_map_copy(data->executed);
4644 executed = isl_union_map_intersect_domain(executed,
4645 isl_union_set_from_set(set));
4647 embed = !isl_set_is_params(data->build->domain);
4648 if (embed && !data->internal)
4649 executed = internal_executed(executed, space, data->build);
4650 if (!embed) {
4651 isl_set *domain;
4652 domain = isl_ast_build_get_domain(data->build);
4653 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4654 executed = isl_union_map_intersect_params(executed, domain);
4657 build = isl_ast_build_copy(data->build);
4658 build = isl_ast_build_product(build, space);
4660 list = generate_next_level(executed, build);
4662 list = isl_ast_graft_list_unembed(list, embed);
4664 data->list = isl_ast_graft_list_concat(data->list, list);
4666 return 0;
4669 /* Generate an AST that visits the elements in the range of data->executed
4670 * in the relative order specified by the corresponding domain element(s)
4671 * for those domain elements that belong to "set".
4672 * Add the result to data->list.
4674 * The caller ensures that "set" is a universe domain.
4676 * If the build space S is not parametric, then the space of "set"
4677 * need to be a wrapped relation with S as domain. That is, it needs
4678 * to be of the form
4680 * [S -> T]
4682 * Check this property and pass control to generate_code_in_space
4683 * passing along T.
4684 * If the build space is not parametric, then T is the space of "set".
4686 static int generate_code_set(__isl_take isl_set *set, void *user)
4688 struct isl_generate_code_data *data = user;
4689 isl_space *space, *build_space;
4690 int is_domain;
4692 space = isl_set_get_space(set);
4694 if (isl_set_is_params(data->build->domain))
4695 return generate_code_in_space(data, set, space);
4697 build_space = isl_ast_build_get_space(data->build, data->internal);
4698 space = isl_space_unwrap(space);
4699 is_domain = isl_space_is_domain(build_space, space);
4700 isl_space_free(build_space);
4701 space = isl_space_range(space);
4703 if (is_domain < 0)
4704 goto error;
4705 if (!is_domain)
4706 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4707 "invalid nested schedule space", goto error);
4709 return generate_code_in_space(data, set, space);
4710 error:
4711 isl_set_free(set);
4712 isl_space_free(space);
4713 return -1;
4716 /* Generate an AST that visits the elements in the range of "executed"
4717 * in the relative order specified by the corresponding domain element(s).
4719 * "build" is an isl_ast_build that has either been constructed by
4720 * isl_ast_build_from_context or passed to a callback set by
4721 * isl_ast_build_set_create_leaf.
4722 * In the first case, the space of the isl_ast_build is typically
4723 * a parametric space, although this is currently not enforced.
4724 * In the second case, the space is never a parametric space.
4725 * If the space S is not parametric, then the domain space(s) of "executed"
4726 * need to be wrapped relations with S as domain.
4728 * If the domain of "executed" consists of several spaces, then an AST
4729 * is generated for each of them (in arbitrary order) and the results
4730 * are concatenated.
4732 * If "internal" is set, then the domain "S" above refers to the internal
4733 * schedule domain representation. Otherwise, it refers to the external
4734 * representation, as returned by isl_ast_build_get_schedule_space.
4736 * We essentially run over all the spaces in the domain of "executed"
4737 * and call generate_code_set on each of them.
4739 static __isl_give isl_ast_graft_list *generate_code(
4740 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4741 int internal)
4743 isl_ctx *ctx;
4744 struct isl_generate_code_data data = { 0 };
4745 isl_space *space;
4746 isl_union_set *schedule_domain;
4747 isl_union_map *universe;
4749 if (!build)
4750 goto error;
4751 space = isl_ast_build_get_space(build, 1);
4752 space = isl_space_align_params(space,
4753 isl_union_map_get_space(executed));
4754 space = isl_space_align_params(space,
4755 isl_union_map_get_space(build->options));
4756 build = isl_ast_build_align_params(build, isl_space_copy(space));
4757 executed = isl_union_map_align_params(executed, space);
4758 if (!executed || !build)
4759 goto error;
4761 ctx = isl_ast_build_get_ctx(build);
4763 data.internal = internal;
4764 data.executed = executed;
4765 data.build = build;
4766 data.list = isl_ast_graft_list_alloc(ctx, 0);
4768 universe = isl_union_map_universe(isl_union_map_copy(executed));
4769 schedule_domain = isl_union_map_domain(universe);
4770 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
4771 &data) < 0)
4772 data.list = isl_ast_graft_list_free(data.list);
4774 isl_union_set_free(schedule_domain);
4775 isl_union_map_free(executed);
4777 isl_ast_build_free(build);
4778 return data.list;
4779 error:
4780 isl_union_map_free(executed);
4781 isl_ast_build_free(build);
4782 return NULL;
4785 /* Generate an AST that visits the elements in the domain of "schedule"
4786 * in the relative order specified by the corresponding image element(s).
4788 * "build" is an isl_ast_build that has either been constructed by
4789 * isl_ast_build_from_context or passed to a callback set by
4790 * isl_ast_build_set_create_leaf.
4791 * In the first case, the space of the isl_ast_build is typically
4792 * a parametric space, although this is currently not enforced.
4793 * In the second case, the space is never a parametric space.
4794 * If the space S is not parametric, then the range space(s) of "schedule"
4795 * need to be wrapped relations with S as domain.
4797 * If the range of "schedule" consists of several spaces, then an AST
4798 * is generated for each of them (in arbitrary order) and the results
4799 * are concatenated.
4801 * We first initialize the local copies of the relevant options.
4802 * We do this here rather than when the isl_ast_build is created
4803 * because the options may have changed between the construction
4804 * of the isl_ast_build and the call to isl_generate_code.
4806 * The main computation is performed on an inverse schedule (with
4807 * the schedule domain in the domain and the elements to be executed
4808 * in the range) called "executed".
4810 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
4811 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4813 isl_ast_graft_list *list;
4814 isl_ast_node *node;
4815 isl_union_map *executed;
4817 build = isl_ast_build_copy(build);
4818 build = isl_ast_build_set_single_valued(build, 0);
4819 schedule = isl_union_map_coalesce(schedule);
4820 schedule = isl_union_map_remove_redundancies(schedule);
4821 executed = isl_union_map_reverse(schedule);
4822 list = generate_code(executed, isl_ast_build_copy(build), 0);
4823 node = isl_ast_node_from_graft_list(list, build);
4824 isl_ast_build_free(build);
4826 return node;
4829 /* The old name for isl_ast_build_node_from_schedule_map.
4830 * It is being kept for backward compatibility, but
4831 * it will be removed in the future.
4833 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
4834 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4836 return isl_ast_build_node_from_schedule_map(build, schedule);
4839 /* Generate an AST that visits the elements in the domain of "executed"
4840 * in the relative order specified by the band node "node" and its descendants.
4842 * The relation "executed" maps the outer generated loop iterators
4843 * to the domain elements executed by those iterations.
4845 * If the band is empty, we continue with its descendants.
4846 * Otherwise, we extend the build and the inverse schedule with
4847 * the additional space/partial schedule and continue generating
4848 * an AST in generate_next_level.
4849 * As soon as we have extended the inverse schedule with the additional
4850 * partial schedule, we look for equalities that may exists between
4851 * the old and the new part.
4853 static __isl_give isl_ast_graft_list *build_ast_from_band(
4854 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4855 __isl_take isl_union_map *executed)
4857 isl_space *space;
4858 isl_multi_union_pw_aff *extra;
4859 isl_union_map *extra_umap;
4860 isl_ast_graft_list *list;
4861 unsigned n1, n2;
4863 if (!build || !node || !executed)
4864 goto error;
4866 if (isl_schedule_node_band_n_member(node) == 0)
4867 return build_ast_from_child(build, node, executed);
4869 extra = isl_schedule_node_band_get_partial_schedule(node);
4870 extra = isl_multi_union_pw_aff_align_params(extra,
4871 isl_ast_build_get_space(build, 1));
4872 space = isl_multi_union_pw_aff_get_space(extra);
4874 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
4875 extra_umap = isl_union_map_reverse(extra_umap);
4877 executed = isl_union_map_domain_product(executed, extra_umap);
4878 executed = isl_union_map_detect_equalities(executed);
4880 n1 = isl_ast_build_dim(build, isl_dim_param);
4881 build = isl_ast_build_product(build, space);
4882 n2 = isl_ast_build_dim(build, isl_dim_param);
4883 if (n2 > n1)
4884 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
4885 "band node is not allowed to introduce new parameters",
4886 build = isl_ast_build_free(build));
4887 build = isl_ast_build_set_schedule_node(build, node);
4889 list = generate_next_level(executed, build);
4891 list = isl_ast_graft_list_unembed(list, 1);
4893 return list;
4894 error:
4895 isl_schedule_node_free(node);
4896 isl_union_map_free(executed);
4897 isl_ast_build_free(build);
4898 return NULL;
4901 /* Hoist a list of grafts (in practice containing a single graft)
4902 * from "sub_build" (which includes extra context information)
4903 * to "build".
4905 * In particular, project out all additional parameters introduced
4906 * by the context node from the enforced constraints and the guard
4907 * of the single graft.
4909 static __isl_give isl_ast_graft_list *hoist_out_of_context(
4910 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
4911 __isl_keep isl_ast_build *sub_build)
4913 isl_ast_graft *graft;
4914 isl_basic_set *enforced;
4915 isl_set *guard;
4916 unsigned n_param, extra_param;
4918 if (!build || !sub_build)
4919 return isl_ast_graft_list_free(list);
4921 n_param = isl_ast_build_dim(build, isl_dim_param);
4922 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
4924 if (extra_param == n_param)
4925 return list;
4927 extra_param -= n_param;
4928 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
4929 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
4930 n_param, extra_param);
4931 enforced = isl_basic_set_remove_unknown_divs(enforced);
4932 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
4933 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
4934 n_param, extra_param);
4935 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
4936 guard = isl_set_compute_divs(guard);
4937 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
4938 build, sub_build);
4939 list = isl_ast_graft_list_from_ast_graft(graft);
4941 return list;
4944 /* Generate an AST that visits the elements in the domain of "executed"
4945 * in the relative order specified by the context node "node"
4946 * and its descendants.
4948 * The relation "executed" maps the outer generated loop iterators
4949 * to the domain elements executed by those iterations.
4951 * The context node may introduce additional parameters as well as
4952 * constraints on the outer schedule dimenions or original parameters.
4954 * We add the extra parameters to a new build and the context
4955 * constraints to both the build and (as a single disjunct)
4956 * to the domain of "executed". Since the context constraints
4957 * are specified in terms of the input schedule, we first need
4958 * to map them to the internal schedule domain.
4960 * After constructing the AST from the descendants of "node",
4961 * we combine the list of grafts into a single graft within
4962 * the new build, in order to be able to exploit the additional
4963 * context constraints during this combination.
4965 * Additionally, if the current node is the outermost node in
4966 * the schedule tree (apart from the root domain node), we generate
4967 * all pending guards, again to be able to exploit the additional
4968 * context constraints. We currently do not do this for internal
4969 * context nodes since we may still want to hoist conditions
4970 * to outer AST nodes.
4972 * If the context node introduced any new parameters, then they
4973 * are removed from the set of enforced constraints and guard
4974 * in hoist_out_of_context.
4976 static __isl_give isl_ast_graft_list *build_ast_from_context(
4977 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4978 __isl_take isl_union_map *executed)
4980 isl_set *context;
4981 isl_space *space;
4982 isl_multi_aff *internal2input;
4983 isl_ast_build *sub_build;
4984 isl_ast_graft_list *list;
4985 int n, depth;
4987 depth = isl_schedule_node_get_tree_depth(node);
4988 space = isl_ast_build_get_space(build, 1);
4989 context = isl_schedule_node_context_get_context(node);
4990 context = isl_set_align_params(context, space);
4991 sub_build = isl_ast_build_copy(build);
4992 space = isl_set_get_space(context);
4993 sub_build = isl_ast_build_align_params(sub_build, space);
4994 internal2input = isl_ast_build_get_internal2input(sub_build);
4995 context = isl_set_preimage_multi_aff(context, internal2input);
4996 sub_build = isl_ast_build_restrict_generated(sub_build,
4997 isl_set_copy(context));
4998 context = isl_set_from_basic_set(isl_set_simple_hull(context));
4999 executed = isl_union_map_intersect_domain(executed,
5000 isl_union_set_from_set(context));
5002 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5003 node, executed);
5004 n = isl_ast_graft_list_n_ast_graft(list);
5005 if (n < 0)
5006 list = isl_ast_graft_list_free(list);
5008 list = isl_ast_graft_list_fuse(list, sub_build);
5009 if (depth == 1)
5010 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5011 sub_build);
5012 if (n >= 1)
5013 list = hoist_out_of_context(list, build, sub_build);
5015 isl_ast_build_free(build);
5016 isl_ast_build_free(sub_build);
5018 return list;
5021 /* Generate an AST that visits the elements in the domain of "executed"
5022 * in the relative order specified by the expansion node "node" and
5023 * its descendants.
5025 * The relation "executed" maps the outer generated loop iterators
5026 * to the domain elements executed by those iterations.
5028 * We expand the domain elements by the expansion and
5029 * continue with the descendants of the node.
5031 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5032 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5033 __isl_take isl_union_map *executed)
5035 isl_union_map *expansion;
5036 unsigned n1, n2;
5038 expansion = isl_schedule_node_expansion_get_expansion(node);
5039 expansion = isl_union_map_align_params(expansion,
5040 isl_union_map_get_space(executed));
5042 n1 = isl_union_map_dim(executed, isl_dim_param);
5043 executed = isl_union_map_apply_range(executed, expansion);
5044 n2 = isl_union_map_dim(executed, isl_dim_param);
5045 if (n2 > n1)
5046 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5047 "expansion node is not allowed to introduce "
5048 "new parameters", goto error);
5050 return build_ast_from_child(build, node, executed);
5051 error:
5052 isl_ast_build_free(build);
5053 isl_schedule_node_free(node);
5054 isl_union_map_free(executed);
5055 return NULL;
5058 /* Generate an AST that visits the elements in the domain of "executed"
5059 * in the relative order specified by the extension node "node" and
5060 * its descendants.
5062 * The relation "executed" maps the outer generated loop iterators
5063 * to the domain elements executed by those iterations.
5065 * Extend the inverse schedule with the extension applied to current
5066 * set of generated constraints. Since the extension if formulated
5067 * in terms of the input schedule, it first needs to be transformed
5068 * to refer to the internal schedule.
5070 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5071 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5072 __isl_take isl_union_map *executed)
5074 isl_union_set *schedule_domain;
5075 isl_union_map *extension;
5076 isl_set *set;
5078 set = isl_ast_build_get_generated(build);
5079 schedule_domain = isl_union_set_from_set(set);
5081 extension = isl_schedule_node_extension_get_extension(node);
5083 extension = isl_union_map_preimage_domain_multi_aff(extension,
5084 isl_multi_aff_copy(build->internal2input));
5085 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5086 extension = isl_ast_build_substitute_values_union_map_domain(build,
5087 extension);
5088 executed = isl_union_map_union(executed, extension);
5090 return build_ast_from_child(build, node, executed);
5093 /* Generate an AST that visits the elements in the domain of "executed"
5094 * in the relative order specified by the filter node "node" and
5095 * its descendants.
5097 * The relation "executed" maps the outer generated loop iterators
5098 * to the domain elements executed by those iterations.
5100 * We simply intersect the iteration domain (i.e., the range of "executed")
5101 * with the filter and continue with the descendants of the node,
5102 * unless the resulting inverse schedule is empty, in which
5103 * case we return an empty list.
5105 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5106 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5107 __isl_take isl_union_map *executed)
5109 isl_ctx *ctx;
5110 isl_union_set *filter;
5111 isl_ast_graft_list *list;
5112 int empty;
5113 unsigned n1, n2;
5115 if (!build || !node || !executed)
5116 goto error;
5118 filter = isl_schedule_node_filter_get_filter(node);
5119 filter = isl_union_set_align_params(filter,
5120 isl_union_map_get_space(executed));
5121 n1 = isl_union_map_dim(executed, isl_dim_param);
5122 executed = isl_union_map_intersect_range(executed, filter);
5123 n2 = isl_union_map_dim(executed, isl_dim_param);
5124 if (n2 > n1)
5125 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5126 "filter node is not allowed to introduce "
5127 "new parameters", goto error);
5129 empty = isl_union_map_is_empty(executed);
5130 if (empty < 0)
5131 goto error;
5132 if (!empty)
5133 return build_ast_from_child(build, node, executed);
5135 ctx = isl_ast_build_get_ctx(build);
5136 list = isl_ast_graft_list_alloc(ctx, 0);
5137 isl_ast_build_free(build);
5138 isl_schedule_node_free(node);
5139 isl_union_map_free(executed);
5140 return list;
5141 error:
5142 isl_ast_build_free(build);
5143 isl_schedule_node_free(node);
5144 isl_union_map_free(executed);
5145 return NULL;
5148 /* Generate an AST that visits the elements in the domain of "executed"
5149 * in the relative order specified by the guard node "node" and
5150 * its descendants.
5152 * The relation "executed" maps the outer generated loop iterators
5153 * to the domain elements executed by those iterations.
5155 * Ensure that the associated guard is enforced by the outer AST
5156 * constructs by adding it to the guard of the graft.
5157 * Since we know that we will enforce the guard, we can also include it
5158 * in the generated constraints used to construct an AST for
5159 * the descendant nodes.
5161 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5162 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5163 __isl_take isl_union_map *executed)
5165 isl_space *space;
5166 isl_set *guard, *hoisted;
5167 isl_basic_set *enforced;
5168 isl_ast_build *sub_build;
5169 isl_ast_graft *graft;
5170 isl_ast_graft_list *list;
5171 unsigned n1, n2;
5173 space = isl_ast_build_get_space(build, 1);
5174 guard = isl_schedule_node_guard_get_guard(node);
5175 n1 = isl_space_dim(space, isl_dim_param);
5176 guard = isl_set_align_params(guard, space);
5177 n2 = isl_set_dim(guard, isl_dim_param);
5178 if (n2 > n1)
5179 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5180 "guard node is not allowed to introduce "
5181 "new parameters", guard = isl_set_free(guard));
5182 guard = isl_set_preimage_multi_aff(guard,
5183 isl_multi_aff_copy(build->internal2input));
5184 guard = isl_ast_build_specialize(build, guard);
5185 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5187 sub_build = isl_ast_build_copy(build);
5188 sub_build = isl_ast_build_restrict_generated(sub_build,
5189 isl_set_copy(guard));
5191 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5192 node, executed);
5194 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5195 if (isl_set_n_basic_set(hoisted) > 1)
5196 list = isl_ast_graft_list_gist_guards(list,
5197 isl_set_copy(hoisted));
5198 guard = isl_set_intersect(guard, hoisted);
5199 enforced = extract_shared_enforced(list, build);
5200 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5201 build, sub_build);
5203 isl_ast_build_free(sub_build);
5204 isl_ast_build_free(build);
5205 return isl_ast_graft_list_from_ast_graft(graft);
5208 /* Call the before_each_mark callback, if requested by the user.
5210 * Return 0 on success and -1 on error.
5212 * The caller is responsible for recording the current inverse schedule
5213 * in "build".
5215 static int before_each_mark(__isl_keep isl_id *mark,
5216 __isl_keep isl_ast_build *build)
5218 if (!build)
5219 return -1;
5220 if (!build->before_each_mark)
5221 return 0;
5222 return build->before_each_mark(mark, build,
5223 build->before_each_mark_user);
5226 /* Call the after_each_mark callback, if requested by the user.
5228 * The caller is responsible for recording the current inverse schedule
5229 * in "build".
5231 static __isl_give isl_ast_graft *after_each_mark(
5232 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5234 if (!graft || !build)
5235 return isl_ast_graft_free(graft);
5236 if (!build->after_each_mark)
5237 return graft;
5238 graft->node = build->after_each_mark(graft->node, build,
5239 build->after_each_mark_user);
5240 if (!graft->node)
5241 return isl_ast_graft_free(graft);
5242 return graft;
5246 /* Generate an AST that visits the elements in the domain of "executed"
5247 * in the relative order specified by the mark node "node" and
5248 * its descendants.
5250 * The relation "executed" maps the outer generated loop iterators
5251 * to the domain elements executed by those iterations.
5253 * Since we may be calling before_each_mark and after_each_mark
5254 * callbacks, we record the current inverse schedule in the build.
5256 * We generate an AST for the child of the mark node, combine
5257 * the graft list into a single graft and then insert the mark
5258 * in the AST of that single graft.
5260 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5261 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5262 __isl_take isl_union_map *executed)
5264 isl_id *mark;
5265 isl_ast_graft *graft;
5266 isl_ast_graft_list *list;
5267 int n;
5269 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5271 mark = isl_schedule_node_mark_get_id(node);
5272 if (before_each_mark(mark, build) < 0)
5273 node = isl_schedule_node_free(node);
5275 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5276 list = isl_ast_graft_list_fuse(list, build);
5277 n = isl_ast_graft_list_n_ast_graft(list);
5278 if (n < 0)
5279 list = isl_ast_graft_list_free(list);
5280 if (n == 0) {
5281 isl_id_free(mark);
5282 } else {
5283 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5284 graft = isl_ast_graft_insert_mark(graft, mark);
5285 graft = after_each_mark(graft, build);
5286 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5288 isl_ast_build_free(build);
5290 return list;
5293 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5294 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5295 __isl_take isl_union_map *executed);
5297 /* Generate an AST that visits the elements in the domain of "executed"
5298 * in the relative order specified by the sequence (or set) node "node" and
5299 * its descendants.
5301 * The relation "executed" maps the outer generated loop iterators
5302 * to the domain elements executed by those iterations.
5304 * We simply generate an AST for each of the children and concatenate
5305 * the results.
5307 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5308 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5309 __isl_take isl_union_map *executed)
5311 int i, n;
5312 isl_ctx *ctx;
5313 isl_ast_graft_list *list;
5315 ctx = isl_ast_build_get_ctx(build);
5316 list = isl_ast_graft_list_alloc(ctx, 0);
5318 n = isl_schedule_node_n_children(node);
5319 for (i = 0; i < n; ++i) {
5320 isl_schedule_node *child;
5321 isl_ast_graft_list *list_i;
5323 child = isl_schedule_node_get_child(node, i);
5324 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5325 child, isl_union_map_copy(executed));
5326 list = isl_ast_graft_list_concat(list, list_i);
5328 isl_ast_build_free(build);
5329 isl_schedule_node_free(node);
5330 isl_union_map_free(executed);
5332 return list;
5335 /* Generate an AST that visits the elements in the domain of "executed"
5336 * in the relative order specified by the node "node" and its descendants.
5338 * The relation "executed" maps the outer generated loop iterators
5339 * to the domain elements executed by those iterations.
5341 * If the node is a leaf, then we pass control to generate_inner_level.
5342 * Note that the current build does not refer to any band node, so
5343 * that generate_inner_level will not try to visit the child of
5344 * the leaf node.
5346 * The other node types are handled in separate functions.
5347 * Set nodes are currently treated in the same way as sequence nodes.
5348 * The children of a set node may be executed in any order,
5349 * including the order of the children.
5351 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5352 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5353 __isl_take isl_union_map *executed)
5355 enum isl_schedule_node_type type;
5357 type = isl_schedule_node_get_type(node);
5359 switch (type) {
5360 case isl_schedule_node_error:
5361 goto error;
5362 case isl_schedule_node_leaf:
5363 isl_schedule_node_free(node);
5364 return generate_inner_level(executed, build);
5365 case isl_schedule_node_band:
5366 return build_ast_from_band(build, node, executed);
5367 case isl_schedule_node_context:
5368 return build_ast_from_context(build, node, executed);
5369 case isl_schedule_node_domain:
5370 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5371 "unexpected internal domain node", goto error);
5372 case isl_schedule_node_expansion:
5373 return build_ast_from_expansion(build, node, executed);
5374 case isl_schedule_node_extension:
5375 return build_ast_from_extension(build, node, executed);
5376 case isl_schedule_node_filter:
5377 return build_ast_from_filter(build, node, executed);
5378 case isl_schedule_node_guard:
5379 return build_ast_from_guard(build, node, executed);
5380 case isl_schedule_node_mark:
5381 return build_ast_from_mark(build, node, executed);
5382 case isl_schedule_node_sequence:
5383 case isl_schedule_node_set:
5384 return build_ast_from_sequence(build, node, executed);
5387 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5388 "unhandled type", goto error);
5389 error:
5390 isl_union_map_free(executed);
5391 isl_schedule_node_free(node);
5392 isl_ast_build_free(build);
5394 return NULL;
5397 /* Generate an AST that visits the elements in the domain of "executed"
5398 * in the relative order specified by the (single) child of "node" and
5399 * its descendants.
5401 * The relation "executed" maps the outer generated loop iterators
5402 * to the domain elements executed by those iterations.
5404 * This function is never called on a leaf, set or sequence node,
5405 * so the node always has exactly one child.
5407 static __isl_give isl_ast_graft_list *build_ast_from_child(
5408 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5409 __isl_take isl_union_map *executed)
5411 node = isl_schedule_node_child(node, 0);
5412 return build_ast_from_schedule_node(build, node, executed);
5415 /* Generate an AST that visits the elements in the domain of the domain
5416 * node "node" in the relative order specified by its descendants.
5418 * An initial inverse schedule is created that maps a zero-dimensional
5419 * schedule space to the node domain.
5420 * The input "build" is assumed to have a parametric domain and
5421 * is replaced by the same zero-dimensional schedule space.
5423 * We also add some of the parameter constraints in the build domain
5424 * to the executed relation. Adding these constraints
5425 * allows for an earlier detection of conflicts in some cases.
5426 * However, we do not want to divide the executed relation into
5427 * more disjuncts than necessary. We therefore approximate
5428 * the constraints on the parameters by a single disjunct set.
5430 static __isl_give isl_ast_node *build_ast_from_domain(
5431 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5433 isl_ctx *ctx;
5434 isl_union_set *domain, *schedule_domain;
5435 isl_union_map *executed;
5436 isl_space *space;
5437 isl_set *set;
5438 isl_ast_graft_list *list;
5439 isl_ast_node *ast;
5440 int is_params;
5442 if (!build)
5443 goto error;
5445 ctx = isl_ast_build_get_ctx(build);
5446 space = isl_ast_build_get_space(build, 1);
5447 is_params = isl_space_is_params(space);
5448 isl_space_free(space);
5449 if (is_params < 0)
5450 goto error;
5451 if (!is_params)
5452 isl_die(ctx, isl_error_unsupported,
5453 "expecting parametric initial context", goto error);
5455 domain = isl_schedule_node_domain_get_domain(node);
5456 domain = isl_union_set_coalesce(domain);
5458 space = isl_union_set_get_space(domain);
5459 space = isl_space_set_from_params(space);
5460 build = isl_ast_build_product(build, space);
5462 set = isl_ast_build_get_domain(build);
5463 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5464 schedule_domain = isl_union_set_from_set(set);
5466 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5467 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5468 ast = isl_ast_node_from_graft_list(list, build);
5469 isl_ast_build_free(build);
5471 return ast;
5472 error:
5473 isl_schedule_node_free(node);
5474 isl_ast_build_free(build);
5475 return NULL;
5478 /* Generate an AST that visits the elements in the domain of "schedule"
5479 * in the relative order specified by the schedule tree.
5481 * "build" is an isl_ast_build that has been created using
5482 * isl_ast_build_alloc or isl_ast_build_from_context based
5483 * on a parametric set.
5485 * The construction starts at the root node of the schedule,
5486 * which is assumed to be a domain node.
5488 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5489 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5491 isl_ctx *ctx;
5492 isl_schedule_node *node;
5494 if (!build || !schedule)
5495 goto error;
5497 ctx = isl_ast_build_get_ctx(build);
5499 node = isl_schedule_get_root(schedule);
5500 isl_schedule_free(schedule);
5502 build = isl_ast_build_copy(build);
5503 build = isl_ast_build_set_single_valued(build, 0);
5504 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5505 isl_die(ctx, isl_error_unsupported,
5506 "expecting root domain node",
5507 build = isl_ast_build_free(build));
5508 return build_ast_from_domain(build, node);
5509 error:
5510 isl_schedule_free(schedule);
5511 return NULL;