isl_map_subtract.c: map_subtract: avoid use of isl_map_empty_like
[isl.git] / isl_ast_codegen.c
blob565f3fed71fe798bd63d76569d3cdf0eef7590d3
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 n = -1;
2580 else if (init && init(n, user) < 0)
2581 n = -1;
2582 for (i = 0; i < n; ++i) {
2583 isl_set *set;
2584 isl_basic_set *bset;
2585 isl_constraint *slice;
2587 slice = at_offset(depth, lower, i);
2588 set = isl_set_copy(domain);
2589 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2590 bset = isl_set_unshifted_simple_hull(set);
2591 bset = isl_basic_set_add_constraint(bset, slice);
2592 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2594 if (fn(bset, user) < 0)
2595 break;
2598 isl_aff_free(lower);
2599 isl_set_free(domain);
2600 isl_basic_map_free(bmap);
2602 return n < 0 || i < n ? -1 : 0;
2605 /* Data structure for storing the results and the intermediate objects
2606 * of compute_domains.
2608 * "list" is the main result of the function and contains a list
2609 * of disjoint basic sets for which code should be generated.
2611 * "executed" and "build" are inputs to compute_domains.
2612 * "schedule_domain" is the domain of "executed".
2614 * "option" constains the domains at the current depth that should by
2615 * atomic, separated or unrolled. These domains are as specified by
2616 * the user, except that inner dimensions have been eliminated and
2617 * that they have been made pair-wise disjoint.
2619 * "sep_class" contains the user-specified split into separation classes
2620 * specialized to the current depth.
2621 * "done" contains the union of the separation domains that have already
2622 * been handled.
2624 struct isl_codegen_domains {
2625 isl_basic_set_list *list;
2627 isl_union_map *executed;
2628 isl_ast_build *build;
2629 isl_set *schedule_domain;
2631 isl_set *option[4];
2633 isl_map *sep_class;
2634 isl_set *done;
2637 /* Internal data structure for do_unroll.
2639 * "domains" stores the results of compute_domains.
2640 * "class_domain" is the original class domain passed to do_unroll.
2641 * "unroll_domain" collects the unrolled iterations.
2643 struct isl_ast_unroll_data {
2644 struct isl_codegen_domains *domains;
2645 isl_set *class_domain;
2646 isl_set *unroll_domain;
2649 /* Given an iteration of an unrolled domain represented by "bset",
2650 * add it to data->domains->list.
2651 * Since we may have dropped some constraints, we intersect with
2652 * the class domain again to ensure that each element in the list
2653 * is disjoint from the other class domains.
2655 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2657 struct isl_ast_unroll_data *data = user;
2658 isl_set *set;
2659 isl_basic_set_list *list;
2661 set = isl_set_from_basic_set(bset);
2662 data->unroll_domain = isl_set_union(data->unroll_domain,
2663 isl_set_copy(set));
2664 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2665 set = isl_set_make_disjoint(set);
2666 list = isl_basic_set_list_from_set(set);
2667 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2668 list);
2670 return 0;
2673 /* Extend domains->list with a list of basic sets, one for each value
2674 * of the current dimension in "domain" and remove the corresponding
2675 * sets from the class domain. Return the updated class domain.
2676 * The divs that involve the current dimension have not been projected out
2677 * from this domain.
2679 * We call foreach_iteration to iterate over the individual values and
2680 * in do_unroll_iteration we collect the individual basic sets in
2681 * domains->list and their union in data->unroll_domain, which is then
2682 * used to update the class domain.
2684 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2685 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2687 struct isl_ast_unroll_data data;
2689 if (!domain)
2690 return isl_set_free(class_domain);
2691 if (!class_domain)
2692 return isl_set_free(domain);
2694 data.domains = domains;
2695 data.class_domain = class_domain;
2696 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2698 if (foreach_iteration(domain, domains->build, NULL,
2699 &do_unroll_iteration, &data) < 0)
2700 data.unroll_domain = isl_set_free(data.unroll_domain);
2702 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2704 return class_domain;
2707 /* Add domains to domains->list for each individual value of the current
2708 * dimension, for that part of the schedule domain that lies in the
2709 * intersection of the option domain and the class domain.
2710 * Remove the corresponding sets from the class domain and
2711 * return the updated class domain.
2713 * We first break up the unroll option domain into individual pieces
2714 * and then handle each of them separately. The unroll option domain
2715 * has been made disjoint in compute_domains_init_options,
2717 * Note that we actively want to combine different pieces of the
2718 * schedule domain that have the same value at the current dimension.
2719 * We therefore need to break up the unroll option domain before
2720 * intersecting with class and schedule domain, hoping that the
2721 * unroll option domain specified by the user is relatively simple.
2723 static __isl_give isl_set *compute_unroll_domains(
2724 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2726 isl_set *unroll_domain;
2727 isl_basic_set_list *unroll_list;
2728 int i, n;
2729 int empty;
2731 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2732 if (empty < 0)
2733 return isl_set_free(class_domain);
2734 if (empty)
2735 return class_domain;
2737 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2738 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2740 n = isl_basic_set_list_n_basic_set(unroll_list);
2741 for (i = 0; i < n; ++i) {
2742 isl_basic_set *bset;
2744 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2745 unroll_domain = isl_set_from_basic_set(bset);
2746 unroll_domain = isl_set_intersect(unroll_domain,
2747 isl_set_copy(class_domain));
2748 unroll_domain = isl_set_intersect(unroll_domain,
2749 isl_set_copy(domains->schedule_domain));
2751 empty = isl_set_is_empty(unroll_domain);
2752 if (empty >= 0 && empty) {
2753 isl_set_free(unroll_domain);
2754 continue;
2757 class_domain = do_unroll(domains, unroll_domain, class_domain);
2760 isl_basic_set_list_free(unroll_list);
2762 return class_domain;
2765 /* Try and construct a single basic set that includes the intersection of
2766 * the schedule domain, the atomic option domain and the class domain.
2767 * Add the resulting basic set(s) to domains->list and remove them
2768 * from class_domain. Return the updated class domain.
2770 * We construct a single domain rather than trying to combine
2771 * the schedule domains of individual domains because we are working
2772 * within a single component so that non-overlapping schedule domains
2773 * should already have been separated.
2774 * We do however need to make sure that this single domains is a subset
2775 * of the class domain so that it would not intersect with any other
2776 * class domains. This means that we may end up splitting up the atomic
2777 * domain in case separation classes are being used.
2779 * "domain" is the intersection of the schedule domain and the class domain,
2780 * with inner dimensions projected out.
2782 static __isl_give isl_set *compute_atomic_domain(
2783 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2785 isl_basic_set *bset;
2786 isl_basic_set_list *list;
2787 isl_set *domain, *atomic_domain;
2788 int empty;
2790 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2791 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2792 domain = isl_set_intersect(domain,
2793 isl_set_copy(domains->schedule_domain));
2794 empty = isl_set_is_empty(domain);
2795 if (empty < 0)
2796 class_domain = isl_set_free(class_domain);
2797 if (empty) {
2798 isl_set_free(domain);
2799 return class_domain;
2802 domain = isl_ast_build_eliminate(domains->build, domain);
2803 domain = isl_set_coalesce(domain);
2804 bset = isl_set_unshifted_simple_hull(domain);
2805 domain = isl_set_from_basic_set(bset);
2806 atomic_domain = isl_set_copy(domain);
2807 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2808 class_domain = isl_set_subtract(class_domain, atomic_domain);
2809 domain = isl_set_make_disjoint(domain);
2810 list = isl_basic_set_list_from_set(domain);
2811 domains->list = isl_basic_set_list_concat(domains->list, list);
2813 return class_domain;
2816 /* Split up the schedule domain into uniform basic sets,
2817 * in the sense that each element in a basic set is associated to
2818 * elements of the same domains, and add the result to domains->list.
2819 * Do this for that part of the schedule domain that lies in the
2820 * intersection of "class_domain" and the separate option domain.
2822 * "class_domain" may or may not include the constraints
2823 * of the schedule domain, but this does not make a difference
2824 * since we are going to intersect it with the domain of the inverse schedule.
2825 * If it includes schedule domain constraints, then they may involve
2826 * inner dimensions, but we will eliminate them in separation_domain.
2828 static int compute_separate_domain(struct isl_codegen_domains *domains,
2829 __isl_keep isl_set *class_domain)
2831 isl_space *space;
2832 isl_set *domain;
2833 isl_union_map *executed;
2834 isl_basic_set_list *list;
2835 int empty;
2837 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2838 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2839 executed = isl_union_map_copy(domains->executed);
2840 executed = isl_union_map_intersect_domain(executed,
2841 isl_union_set_from_set(domain));
2842 empty = isl_union_map_is_empty(executed);
2843 if (empty < 0 || empty) {
2844 isl_union_map_free(executed);
2845 return empty < 0 ? -1 : 0;
2848 space = isl_set_get_space(class_domain);
2849 domain = separate_schedule_domains(space, executed, domains->build);
2851 list = isl_basic_set_list_from_set(domain);
2852 domains->list = isl_basic_set_list_concat(domains->list, list);
2854 return 0;
2857 /* Split up the domain at the current depth into disjoint
2858 * basic sets for which code should be generated separately
2859 * for the given separation class domain.
2861 * If any separation classes have been defined, then "class_domain"
2862 * is the domain of the current class and does not refer to inner dimensions.
2863 * Otherwise, "class_domain" is the universe domain.
2865 * We first make sure that the class domain is disjoint from
2866 * previously considered class domains.
2868 * The separate domains can be computed directly from the "class_domain".
2870 * The unroll, atomic and remainder domains need the constraints
2871 * from the schedule domain.
2873 * For unrolling, the actual schedule domain is needed (with divs that
2874 * may refer to the current dimension) so that stride detection can be
2875 * performed.
2877 * For atomic and remainder domains, inner dimensions and divs involving
2878 * the current dimensions should be eliminated.
2879 * In case we are working within a separation class, we need to intersect
2880 * the result with the current "class_domain" to ensure that the domains
2881 * are disjoint from those generated from other class domains.
2883 * The domain that has been made atomic may be larger than specified
2884 * by the user since it needs to be representable as a single basic set.
2885 * This possibly larger domain is removed from class_domain by
2886 * compute_atomic_domain. It is computed first so that the extended domain
2887 * would not overlap with any domains computed before.
2888 * Similary, the unrolled domains may have some constraints removed and
2889 * may therefore also be larger than specified by the user.
2891 * If anything is left after handling separate, unroll and atomic,
2892 * we split it up into basic sets and append the basic sets to domains->list.
2894 static int compute_partial_domains(struct isl_codegen_domains *domains,
2895 __isl_take isl_set *class_domain)
2897 isl_basic_set_list *list;
2898 isl_set *domain;
2900 class_domain = isl_set_subtract(class_domain,
2901 isl_set_copy(domains->done));
2902 domains->done = isl_set_union(domains->done,
2903 isl_set_copy(class_domain));
2905 class_domain = compute_atomic_domain(domains, class_domain);
2906 class_domain = compute_unroll_domains(domains, class_domain);
2908 domain = isl_set_copy(class_domain);
2910 if (compute_separate_domain(domains, domain) < 0)
2911 goto error;
2912 domain = isl_set_subtract(domain,
2913 isl_set_copy(domains->option[isl_ast_loop_separate]));
2915 domain = isl_set_intersect(domain,
2916 isl_set_copy(domains->schedule_domain));
2918 domain = isl_ast_build_eliminate(domains->build, domain);
2919 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2921 domain = isl_set_coalesce(domain);
2922 domain = isl_set_make_disjoint(domain);
2924 list = isl_basic_set_list_from_set(domain);
2925 domains->list = isl_basic_set_list_concat(domains->list, list);
2927 isl_set_free(class_domain);
2929 return 0;
2930 error:
2931 isl_set_free(domain);
2932 isl_set_free(class_domain);
2933 return -1;
2936 /* Split up the domain at the current depth into disjoint
2937 * basic sets for which code should be generated separately
2938 * for the separation class identified by "pnt".
2940 * We extract the corresponding class domain from domains->sep_class,
2941 * eliminate inner dimensions and pass control to compute_partial_domains.
2943 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2945 struct isl_codegen_domains *domains = user;
2946 isl_set *class_set;
2947 isl_set *domain;
2948 int disjoint;
2950 class_set = isl_set_from_point(pnt);
2951 domain = isl_map_domain(isl_map_intersect_range(
2952 isl_map_copy(domains->sep_class), class_set));
2953 domain = isl_ast_build_compute_gist(domains->build, domain);
2954 domain = isl_ast_build_eliminate(domains->build, domain);
2956 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2957 if (disjoint < 0)
2958 return -1;
2959 if (disjoint) {
2960 isl_set_free(domain);
2961 return 0;
2964 return compute_partial_domains(domains, domain);
2967 /* Extract the domains at the current depth that should be atomic,
2968 * separated or unrolled and store them in option.
2970 * The domains specified by the user might overlap, so we make
2971 * them disjoint by subtracting earlier domains from later domains.
2973 static void compute_domains_init_options(isl_set *option[4],
2974 __isl_keep isl_ast_build *build)
2976 enum isl_ast_loop_type type, type2;
2977 isl_set *unroll;
2979 for (type = isl_ast_loop_atomic;
2980 type <= isl_ast_loop_separate; ++type) {
2981 option[type] = isl_ast_build_get_option_domain(build, type);
2982 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
2983 option[type] = isl_set_subtract(option[type],
2984 isl_set_copy(option[type2]));
2987 unroll = option[isl_ast_loop_unroll];
2988 unroll = isl_set_coalesce(unroll);
2989 unroll = isl_set_make_disjoint(unroll);
2990 option[isl_ast_loop_unroll] = unroll;
2993 /* Split up the domain at the current depth into disjoint
2994 * basic sets for which code should be generated separately,
2995 * based on the user-specified options.
2996 * Return the list of disjoint basic sets.
2998 * There are three kinds of domains that we need to keep track of.
2999 * - the "schedule domain" is the domain of "executed"
3000 * - the "class domain" is the domain corresponding to the currrent
3001 * separation class
3002 * - the "option domain" is the domain corresponding to one of the options
3003 * atomic, unroll or separate
3005 * We first consider the individial values of the separation classes
3006 * and split up the domain for each of them separately.
3007 * Finally, we consider the remainder. If no separation classes were
3008 * specified, then we call compute_partial_domains with the universe
3009 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3010 * with inner dimensions removed. We do this because we want to
3011 * avoid computing the complement of the class domains (i.e., the difference
3012 * between the universe and domains->done).
3014 static __isl_give isl_basic_set_list *compute_domains(
3015 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3017 struct isl_codegen_domains domains;
3018 isl_ctx *ctx;
3019 isl_set *domain;
3020 isl_union_set *schedule_domain;
3021 isl_set *classes;
3022 isl_space *space;
3023 int n_param;
3024 enum isl_ast_loop_type type;
3025 int empty;
3027 if (!executed)
3028 return NULL;
3030 ctx = isl_union_map_get_ctx(executed);
3031 domains.list = isl_basic_set_list_alloc(ctx, 0);
3033 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3034 domain = isl_set_from_union_set(schedule_domain);
3036 compute_domains_init_options(domains.option, build);
3038 domains.sep_class = isl_ast_build_get_separation_class(build);
3039 classes = isl_map_range(isl_map_copy(domains.sep_class));
3040 n_param = isl_set_dim(classes, isl_dim_param);
3041 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3043 space = isl_set_get_space(domain);
3044 domains.build = build;
3045 domains.schedule_domain = isl_set_copy(domain);
3046 domains.executed = executed;
3047 domains.done = isl_set_empty(space);
3049 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3050 domains.list = isl_basic_set_list_free(domains.list);
3051 isl_set_free(classes);
3053 empty = isl_set_is_empty(domains.done);
3054 if (empty < 0) {
3055 domains.list = isl_basic_set_list_free(domains.list);
3056 domain = isl_set_free(domain);
3057 } else if (empty) {
3058 isl_set_free(domain);
3059 domain = isl_set_universe(isl_set_get_space(domains.done));
3060 } else {
3061 domain = isl_ast_build_eliminate(build, domain);
3063 if (compute_partial_domains(&domains, domain) < 0)
3064 domains.list = isl_basic_set_list_free(domains.list);
3066 isl_set_free(domains.schedule_domain);
3067 isl_set_free(domains.done);
3068 isl_map_free(domains.sep_class);
3069 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3070 isl_set_free(domains.option[type]);
3072 return domains.list;
3075 /* Generate code for a single component, after shifting (if any)
3076 * has been applied, in case the schedule was specified as a union map.
3078 * We first split up the domain at the current depth into disjoint
3079 * basic sets based on the user-specified options.
3080 * Then we generated code for each of them and concatenate the results.
3082 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3083 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3085 isl_basic_set_list *domain_list;
3086 isl_ast_graft_list *list = NULL;
3088 domain_list = compute_domains(executed, build);
3089 list = generate_parallel_domains(domain_list, executed, build);
3091 isl_basic_set_list_free(domain_list);
3092 isl_union_map_free(executed);
3093 isl_ast_build_free(build);
3095 return list;
3098 /* Generate code for a single component, after shifting (if any)
3099 * has been applied, in case the schedule was specified as a schedule tree
3100 * and the separate option was specified.
3102 * We perform separation on the domain of "executed" and then generate
3103 * an AST for each of the resulting disjoint basic sets.
3105 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3106 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3108 isl_space *space;
3109 isl_set *domain;
3110 isl_basic_set_list *domain_list;
3111 isl_ast_graft_list *list;
3113 space = isl_ast_build_get_space(build, 1);
3114 domain = separate_schedule_domains(space,
3115 isl_union_map_copy(executed), build);
3116 domain_list = isl_basic_set_list_from_set(domain);
3118 list = generate_parallel_domains(domain_list, executed, build);
3120 isl_basic_set_list_free(domain_list);
3121 isl_union_map_free(executed);
3122 isl_ast_build_free(build);
3124 return list;
3127 /* Internal data structure for generate_shifted_component_tree_unroll.
3129 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3130 * "list" collects the constructs grafts.
3132 struct isl_ast_unroll_tree_data {
3133 isl_union_map *executed;
3134 isl_ast_build *build;
3135 isl_ast_graft_list *list;
3138 /* Initialize data->list to a list of "n" elements.
3140 static int init_unroll_tree(int n, void *user)
3142 struct isl_ast_unroll_tree_data *data = user;
3143 isl_ctx *ctx;
3145 ctx = isl_ast_build_get_ctx(data->build);
3146 data->list = isl_ast_graft_list_alloc(ctx, n);
3148 return 0;
3151 /* Given an iteration of an unrolled domain represented by "bset",
3152 * generate the corresponding AST and add the result to data->list.
3154 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3156 struct isl_ast_unroll_tree_data *data = user;
3158 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3159 bset, isl_ast_build_copy(data->build));
3161 return 0;
3164 /* Generate code for a single component, after shifting (if any)
3165 * has been applied, in case the schedule was specified as a schedule tree
3166 * and the unroll option was specified.
3168 * We call foreach_iteration to iterate over the individual values and
3169 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3171 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3172 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3173 __isl_take isl_ast_build *build)
3175 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3177 if (foreach_iteration(domain, build, &init_unroll_tree,
3178 &do_unroll_tree_iteration, &data) < 0)
3179 data.list = isl_ast_graft_list_free(data.list);
3181 isl_union_map_free(executed);
3182 isl_ast_build_free(build);
3184 return data.list;
3187 /* Generate code for a single component, after shifting (if any)
3188 * has been applied, in case the schedule was specified as a schedule tree.
3189 * In particular, handle the base case where there is either no isolated
3190 * set or we are within the isolated set (in which case "isolated" is set)
3191 * or the iterations that precede or follow the isolated set.
3193 * The schedule domain is broken up or combined into basic sets
3194 * according to the AST generation option specified in the current
3195 * schedule node, which may be either atomic, separate, unroll or
3196 * unspecified. If the option is unspecified, then we currently simply
3197 * split the schedule domain into disjoint basic sets.
3199 * In case the separate option is specified, the AST generation is
3200 * handled by generate_shifted_component_tree_separate.
3201 * In the other cases, we need the global schedule domain.
3202 * In the unroll case, the AST generation is then handled by
3203 * generate_shifted_component_tree_unroll which needs the actual
3204 * schedule domain (with divs that may refer to the current dimension)
3205 * so that stride detection can be performed.
3206 * In the atomic or unspecified case, inner dimensions and divs involving
3207 * the current dimensions should be eliminated.
3208 * The result is then either combined into a single basic set or
3209 * split up into disjoint basic sets.
3210 * Finally an AST is generated for each basic set and the results are
3211 * concatenated.
3213 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3214 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3215 int isolated)
3217 isl_union_set *schedule_domain;
3218 isl_set *domain;
3219 isl_basic_set_list *domain_list;
3220 isl_ast_graft_list *list;
3221 enum isl_ast_loop_type type;
3223 type = isl_ast_build_get_loop_type(build, isolated);
3224 if (type < 0)
3225 goto error;
3227 if (type == isl_ast_loop_separate)
3228 return generate_shifted_component_tree_separate(executed,
3229 build);
3231 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3232 domain = isl_set_from_union_set(schedule_domain);
3234 if (type == isl_ast_loop_unroll)
3235 return generate_shifted_component_tree_unroll(executed, domain,
3236 build);
3238 domain = isl_ast_build_eliminate(build, domain);
3239 domain = isl_set_coalesce(domain);
3241 if (type == isl_ast_loop_atomic) {
3242 isl_basic_set *hull;
3243 hull = isl_set_unshifted_simple_hull(domain);
3244 domain_list = isl_basic_set_list_from_basic_set(hull);
3245 } else {
3246 domain = isl_set_make_disjoint(domain);
3247 domain_list = isl_basic_set_list_from_set(domain);
3250 list = generate_parallel_domains(domain_list, executed, build);
3252 isl_basic_set_list_free(domain_list);
3253 isl_union_map_free(executed);
3254 isl_ast_build_free(build);
3256 return list;
3257 error:
3258 isl_union_map_free(executed);
3259 isl_ast_build_free(build);
3260 return NULL;
3263 /* Generate code for a single component, after shifting (if any)
3264 * has been applied, in case the schedule was specified as a schedule tree.
3265 * In particular, do so for the specified subset of the schedule domain.
3267 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3268 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3269 __isl_keep isl_ast_build *build, int isolated)
3271 isl_union_set *uset;
3272 int empty;
3274 uset = isl_union_set_from_set(domain);
3275 executed = isl_union_map_copy(executed);
3276 executed = isl_union_map_intersect_domain(executed, uset);
3277 empty = isl_union_map_is_empty(executed);
3278 if (empty < 0)
3279 goto error;
3280 if (empty) {
3281 isl_ctx *ctx;
3282 isl_union_map_free(executed);
3283 ctx = isl_ast_build_get_ctx(build);
3284 return isl_ast_graft_list_alloc(ctx, 0);
3287 build = isl_ast_build_copy(build);
3288 return generate_shifted_component_tree_base(executed, build, isolated);
3289 error:
3290 isl_union_map_free(executed);
3291 return NULL;
3294 /* Generate code for a single component, after shifting (if any)
3295 * has been applied, in case the schedule was specified as a schedule tree.
3297 * We first check if the user has specified an isolated schedule domain
3298 * and that we are not already outside of this isolated schedule domain.
3299 * If so, we break up the schedule domain into iterations that
3300 * precede the isolated domain, the isolated domain itself,
3301 * the iterations that follow the isolated domain and
3302 * the remaining iterations (those that are incomparable
3303 * to the isolated domain).
3304 * We generate an AST for each piece and concatenate the results.
3305 * If no isolated set has been specified, then we generate an
3306 * AST for the entire inverse schedule.
3308 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3309 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3311 int i, depth;
3312 int empty, has_isolate;
3313 isl_space *space;
3314 isl_union_set *schedule_domain;
3315 isl_set *domain;
3316 isl_basic_set *hull;
3317 isl_set *isolated, *before, *after, *test;
3318 isl_map *gt, *lt;
3319 isl_ast_graft_list *list, *res;
3321 build = isl_ast_build_extract_isolated(build);
3322 has_isolate = isl_ast_build_has_isolated(build);
3323 if (has_isolate < 0)
3324 executed = isl_union_map_free(executed);
3325 else if (!has_isolate)
3326 return generate_shifted_component_tree_base(executed, build, 0);
3328 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3329 domain = isl_set_from_union_set(schedule_domain);
3331 isolated = isl_ast_build_get_isolated(build);
3332 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3333 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3334 empty = isl_set_is_empty(test);
3335 isl_set_free(test);
3336 if (empty < 0)
3337 goto error;
3338 if (empty) {
3339 isl_set_free(isolated);
3340 isl_set_free(domain);
3341 return generate_shifted_component_tree_base(executed, build, 0);
3343 isolated = isl_ast_build_eliminate(build, isolated);
3344 hull = isl_set_unshifted_simple_hull(isolated);
3345 isolated = isl_set_from_basic_set(hull);
3347 depth = isl_ast_build_get_depth(build);
3348 space = isl_space_map_from_set(isl_set_get_space(isolated));
3349 gt = isl_map_universe(space);
3350 for (i = 0; i < depth; ++i)
3351 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3352 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3353 lt = isl_map_reverse(isl_map_copy(gt));
3354 before = isl_set_apply(isl_set_copy(isolated), gt);
3355 after = isl_set_apply(isl_set_copy(isolated), lt);
3357 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3358 domain = isl_set_subtract(domain, isl_set_copy(before));
3359 domain = isl_set_subtract(domain, isl_set_copy(after));
3360 after = isl_set_subtract(after, isl_set_copy(isolated));
3361 after = isl_set_subtract(after, isl_set_copy(before));
3362 before = isl_set_subtract(before, isl_set_copy(isolated));
3364 res = generate_shifted_component_tree_part(executed, before, build, 0);
3365 list = generate_shifted_component_tree_part(executed, isolated,
3366 build, 1);
3367 res = isl_ast_graft_list_concat(res, list);
3368 list = generate_shifted_component_tree_part(executed, after, build, 0);
3369 res = isl_ast_graft_list_concat(res, list);
3370 list = generate_shifted_component_tree_part(executed, domain, build, 0);
3371 res = isl_ast_graft_list_concat(res, list);
3373 isl_union_map_free(executed);
3374 isl_ast_build_free(build);
3376 return res;
3377 error:
3378 isl_set_free(domain);
3379 isl_set_free(isolated);
3380 isl_union_map_free(executed);
3381 isl_ast_build_free(build);
3382 return NULL;
3385 /* Generate code for a single component, after shifting (if any)
3386 * has been applied.
3388 * Call generate_shifted_component_tree or generate_shifted_component_flat
3389 * depending on whether the schedule was specified as a schedule tree.
3391 static __isl_give isl_ast_graft_list *generate_shifted_component(
3392 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3394 if (isl_ast_build_has_schedule_node(build))
3395 return generate_shifted_component_tree(executed, build);
3396 else
3397 return generate_shifted_component_flat(executed, build);
3400 struct isl_set_map_pair {
3401 isl_set *set;
3402 isl_map *map;
3405 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3406 * of indices into the "domain" array,
3407 * return the union of the "map" fields of the elements
3408 * indexed by the first "n" elements of "order".
3410 static __isl_give isl_union_map *construct_component_executed(
3411 struct isl_set_map_pair *domain, int *order, int n)
3413 int i;
3414 isl_map *map;
3415 isl_union_map *executed;
3417 map = isl_map_copy(domain[order[0]].map);
3418 executed = isl_union_map_from_map(map);
3419 for (i = 1; i < n; ++i) {
3420 map = isl_map_copy(domain[order[i]].map);
3421 executed = isl_union_map_add_map(executed, map);
3424 return executed;
3427 /* Generate code for a single component, after shifting (if any)
3428 * has been applied.
3430 * The component inverse schedule is specified as the "map" fields
3431 * of the elements of "domain" indexed by the first "n" elements of "order".
3433 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3434 struct isl_set_map_pair *domain, int *order, int n,
3435 __isl_take isl_ast_build *build)
3437 isl_union_map *executed;
3439 executed = construct_component_executed(domain, order, n);
3440 return generate_shifted_component(executed, build);
3443 /* Does set dimension "pos" of "set" have an obviously fixed value?
3445 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3447 int fixed;
3448 isl_val *v;
3450 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3451 if (!v)
3452 return -1;
3453 fixed = !isl_val_is_nan(v);
3454 isl_val_free(v);
3456 return fixed;
3459 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3460 * of indices into the "domain" array,
3461 * do all (except for at most one) of the "set" field of the elements
3462 * indexed by the first "n" elements of "order" have a fixed value
3463 * at position "depth"?
3465 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3466 int *order, int n, int depth)
3468 int i;
3469 int non_fixed = -1;
3471 for (i = 0; i < n; ++i) {
3472 int f;
3474 f = dim_is_fixed(domain[order[i]].set, depth);
3475 if (f < 0)
3476 return -1;
3477 if (f)
3478 continue;
3479 if (non_fixed >= 0)
3480 return 0;
3481 non_fixed = i;
3484 return 1;
3487 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3488 * of indices into the "domain" array,
3489 * eliminate the inner dimensions from the "set" field of the elements
3490 * indexed by the first "n" elements of "order", provided the current
3491 * dimension does not have a fixed value.
3493 * Return the index of the first element in "order" with a corresponding
3494 * "set" field that does not have an (obviously) fixed value.
3496 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3497 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3499 int i;
3500 int base = -1;
3502 for (i = n - 1; i >= 0; --i) {
3503 int f;
3504 f = dim_is_fixed(domain[order[i]].set, depth);
3505 if (f < 0)
3506 return -1;
3507 if (f)
3508 continue;
3509 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3510 domain[order[i]].set);
3511 base = i;
3514 return base;
3517 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3518 * of indices into the "domain" array,
3519 * find the element of "domain" (amongst those indexed by the first "n"
3520 * elements of "order") with the "set" field that has the smallest
3521 * value for the current iterator.
3523 * Note that the domain with the smallest value may depend on the parameters
3524 * and/or outer loop dimension. Since the result of this function is only
3525 * used as heuristic, we only make a reasonable attempt at finding the best
3526 * domain, one that should work in case a single domain provides the smallest
3527 * value for the current dimension over all values of the parameters
3528 * and outer dimensions.
3530 * In particular, we compute the smallest value of the first domain
3531 * and replace it by that of any later domain if that later domain
3532 * has a smallest value that is smaller for at least some value
3533 * of the parameters and outer dimensions.
3535 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3536 __isl_keep isl_ast_build *build)
3538 int i;
3539 isl_map *min_first;
3540 int first = 0;
3542 min_first = isl_ast_build_map_to_iterator(build,
3543 isl_set_copy(domain[order[0]].set));
3544 min_first = isl_map_lexmin(min_first);
3546 for (i = 1; i < n; ++i) {
3547 isl_map *min, *test;
3548 int empty;
3550 min = isl_ast_build_map_to_iterator(build,
3551 isl_set_copy(domain[order[i]].set));
3552 min = isl_map_lexmin(min);
3553 test = isl_map_copy(min);
3554 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3555 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3556 empty = isl_map_is_empty(test);
3557 isl_map_free(test);
3558 if (empty >= 0 && !empty) {
3559 isl_map_free(min_first);
3560 first = i;
3561 min_first = min;
3562 } else
3563 isl_map_free(min);
3565 if (empty < 0)
3566 break;
3569 isl_map_free(min_first);
3571 return i < n ? -1 : first;
3574 /* Construct a shifted inverse schedule based on the original inverse schedule,
3575 * the stride and the offset.
3577 * The original inverse schedule is specified as the "map" fields
3578 * of the elements of "domain" indexed by the first "n" elements of "order".
3580 * "stride" and "offset" are such that the difference
3581 * between the values of the current dimension of domain "i"
3582 * and the values of the current dimension for some reference domain are
3583 * equal to
3585 * stride * integer + offset[i]
3587 * Moreover, 0 <= offset[i] < stride.
3589 * For each domain, we create a map
3591 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3593 * where j refers to the current dimension and the other dimensions are
3594 * unchanged, and apply this map to the original schedule domain.
3596 * For example, for the original schedule
3598 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3600 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3601 * we apply the mapping
3603 * { [j] -> [j, 0] }
3605 * to the schedule of the "A" domain and the mapping
3607 * { [j - 1] -> [j, 1] }
3609 * to the schedule of the "B" domain.
3612 * Note that after the transformation, the differences between pairs
3613 * of values of the current dimension over all domains are multiples
3614 * of stride and that we have therefore exposed the stride.
3617 * To see that the mapping preserves the lexicographic order,
3618 * first note that each of the individual maps above preserves the order.
3619 * If the value of the current iterator is j1 in one domain and j2 in another,
3620 * then if j1 = j2, we know that the same map is applied to both domains
3621 * and the order is preserved.
3622 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3623 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3625 * j1 - c1 < j2 - c2
3627 * and the order is preserved.
3628 * If c1 < c2, then we know
3630 * 0 <= c2 - c1 < s
3632 * We also have
3634 * j2 - j1 = n * s + r
3636 * with n >= 0 and 0 <= r < s.
3637 * In other words, r = c2 - c1.
3638 * If n > 0, then
3640 * j1 - c1 < j2 - c2
3642 * If n = 0, then
3644 * j1 - c1 = j2 - c2
3646 * and so
3648 * (j1 - c1, c1) << (j2 - c2, c2)
3650 * with "<<" the lexicographic order, proving that the order is preserved
3651 * in all cases.
3653 static __isl_give isl_union_map *contruct_shifted_executed(
3654 struct isl_set_map_pair *domain, int *order, int n,
3655 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3656 __isl_take isl_ast_build *build)
3658 int i;
3659 isl_union_map *executed;
3660 isl_space *space;
3661 isl_map *map;
3662 int depth;
3663 isl_constraint *c;
3665 depth = isl_ast_build_get_depth(build);
3666 space = isl_ast_build_get_space(build, 1);
3667 executed = isl_union_map_empty(isl_space_copy(space));
3668 space = isl_space_map_from_set(space);
3669 map = isl_map_identity(isl_space_copy(space));
3670 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3671 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3672 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3674 c = isl_equality_alloc(isl_local_space_from_space(space));
3675 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3676 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3678 for (i = 0; i < n; ++i) {
3679 isl_map *map_i;
3680 isl_val *v;
3682 v = isl_multi_val_get_val(offset, i);
3683 if (!v)
3684 break;
3685 map_i = isl_map_copy(map);
3686 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3687 isl_val_copy(v));
3688 v = isl_val_neg(v);
3689 c = isl_constraint_set_constant_val(c, v);
3690 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3692 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3693 map_i);
3694 executed = isl_union_map_add_map(executed, map_i);
3697 isl_constraint_free(c);
3698 isl_map_free(map);
3700 if (i < n)
3701 executed = isl_union_map_free(executed);
3703 return executed;
3706 /* Generate code for a single component, after exposing the stride,
3707 * given that the schedule domain is "shifted strided".
3709 * The component inverse schedule is specified as the "map" fields
3710 * of the elements of "domain" indexed by the first "n" elements of "order".
3712 * The schedule domain being "shifted strided" means that the differences
3713 * between the values of the current dimension of domain "i"
3714 * and the values of the current dimension for some reference domain are
3715 * equal to
3717 * stride * integer + offset[i]
3719 * We first look for the domain with the "smallest" value for the current
3720 * dimension and adjust the offsets such that the offset of the "smallest"
3721 * domain is equal to zero. The other offsets are reduced modulo stride.
3723 * Based on this information, we construct a new inverse schedule in
3724 * contruct_shifted_executed that exposes the stride.
3725 * Since this involves the introduction of a new schedule dimension,
3726 * the build needs to be changed accodingly.
3727 * After computing the AST, the newly introduced dimension needs
3728 * to be removed again from the list of grafts. We do this by plugging
3729 * in a mapping that represents the new schedule domain in terms of the
3730 * old schedule domain.
3732 static __isl_give isl_ast_graft_list *generate_shift_component(
3733 struct isl_set_map_pair *domain, int *order, int n,
3734 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3735 __isl_take isl_ast_build *build)
3737 isl_ast_graft_list *list;
3738 int first;
3739 int depth;
3740 isl_val *val;
3741 isl_multi_val *mv;
3742 isl_space *space;
3743 isl_multi_aff *ma, *zero;
3744 isl_union_map *executed;
3746 depth = isl_ast_build_get_depth(build);
3748 first = first_offset(domain, order, n, build);
3749 if (first < 0)
3750 goto error;
3752 mv = isl_multi_val_copy(offset);
3753 val = isl_multi_val_get_val(offset, first);
3754 val = isl_val_neg(val);
3755 mv = isl_multi_val_add_val(mv, val);
3756 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3758 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3759 build);
3760 space = isl_ast_build_get_space(build, 1);
3761 space = isl_space_map_from_set(space);
3762 ma = isl_multi_aff_identity(isl_space_copy(space));
3763 space = isl_space_from_domain(isl_space_domain(space));
3764 space = isl_space_add_dims(space, isl_dim_out, 1);
3765 zero = isl_multi_aff_zero(space);
3766 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3767 build = isl_ast_build_insert_dim(build, depth + 1);
3768 list = generate_shifted_component(executed, build);
3770 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3772 isl_multi_val_free(mv);
3774 return list;
3775 error:
3776 isl_ast_build_free(build);
3777 return NULL;
3780 /* Does any node in the schedule tree rooted at the current schedule node
3781 * of "build" depend on outer schedule nodes?
3783 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
3785 isl_schedule_node *node;
3786 int dependent = 0;
3788 node = isl_ast_build_get_schedule_node(build);
3789 dependent = isl_schedule_node_is_subtree_anchored(node);
3790 isl_schedule_node_free(node);
3792 return dependent;
3795 /* Generate code for a single component.
3797 * The component inverse schedule is specified as the "map" fields
3798 * of the elements of "domain" indexed by the first "n" elements of "order".
3800 * This function may modify the "set" fields of "domain".
3802 * Before proceeding with the actual code generation for the component,
3803 * we first check if there are any "shifted" strides, meaning that
3804 * the schedule domains of the individual domains are all strided,
3805 * but that they have different offsets, resulting in the union
3806 * of schedule domains not being strided anymore.
3808 * The simplest example is the schedule
3810 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3812 * Both schedule domains are strided, but their union is not.
3813 * This function detects such cases and then rewrites the schedule to
3815 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3817 * In the new schedule, the schedule domains have the same offset (modulo
3818 * the stride), ensuring that the union of schedule domains is also strided.
3821 * If there is only a single domain in the component, then there is
3822 * nothing to do. Similarly, if the current schedule dimension has
3823 * a fixed value for almost all domains then there is nothing to be done.
3824 * In particular, we need at least two domains where the current schedule
3825 * dimension does not have a fixed value.
3826 * Finally, in case of a schedule map input,
3827 * if any of the options refer to the current schedule dimension,
3828 * then we bail out as well. It would be possible to reformulate the options
3829 * in terms of the new schedule domain, but that would introduce constraints
3830 * that separate the domains in the options and that is something we would
3831 * like to avoid.
3832 * In the case of a schedule tree input, we bail out if any of
3833 * the descendants of the current schedule node refer to outer
3834 * schedule nodes in any way.
3837 * To see if there is any shifted stride, we look at the differences
3838 * between the values of the current dimension in pairs of domains
3839 * for equal values of outer dimensions. These differences should be
3840 * of the form
3842 * m x + r
3844 * with "m" the stride and "r" a constant. Note that we cannot perform
3845 * this analysis on individual domains as the lower bound in each domain
3846 * may depend on parameters or outer dimensions and so the current dimension
3847 * itself may not have a fixed remainder on division by the stride.
3849 * In particular, we compare the first domain that does not have an
3850 * obviously fixed value for the current dimension to itself and all
3851 * other domains and collect the offsets and the gcd of the strides.
3852 * If the gcd becomes one, then we failed to find shifted strides.
3853 * If the gcd is zero, then the differences were all fixed, meaning
3854 * that some domains had non-obviously fixed values for the current dimension.
3855 * If all the offsets are the same (for those domains that do not have
3856 * an obviously fixed value for the current dimension), then we do not
3857 * apply the transformation.
3858 * If none of the domains were skipped, then there is nothing to do.
3859 * If some of them were skipped, then if we apply separation, the schedule
3860 * domain should get split in pieces with a (non-shifted) stride.
3862 * Otherwise, we apply a shift to expose the stride in
3863 * generate_shift_component.
3865 static __isl_give isl_ast_graft_list *generate_component(
3866 struct isl_set_map_pair *domain, int *order, int n,
3867 __isl_take isl_ast_build *build)
3869 int i, d;
3870 int depth;
3871 isl_ctx *ctx;
3872 isl_map *map;
3873 isl_set *deltas;
3874 isl_val *gcd = NULL;
3875 isl_multi_val *mv;
3876 int fixed, skip;
3877 int base;
3878 isl_ast_graft_list *list;
3879 int res = 0;
3881 depth = isl_ast_build_get_depth(build);
3883 skip = n == 1;
3884 if (skip >= 0 && !skip)
3885 skip = at_most_one_non_fixed(domain, order, n, depth);
3886 if (skip >= 0 && !skip) {
3887 if (isl_ast_build_has_schedule_node(build))
3888 skip = has_anchored_subtree(build);
3889 else
3890 skip = isl_ast_build_options_involve_depth(build);
3892 if (skip < 0)
3893 goto error;
3894 if (skip)
3895 return generate_shifted_component_from_list(domain,
3896 order, n, build);
3898 base = eliminate_non_fixed(domain, order, n, depth, build);
3899 if (base < 0)
3900 goto error;
3902 ctx = isl_ast_build_get_ctx(build);
3904 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3906 fixed = 1;
3907 for (i = 0; i < n; ++i) {
3908 isl_val *r, *m;
3910 map = isl_map_from_domain_and_range(
3911 isl_set_copy(domain[order[base]].set),
3912 isl_set_copy(domain[order[i]].set));
3913 for (d = 0; d < depth; ++d)
3914 map = isl_map_equate(map, isl_dim_in, d,
3915 isl_dim_out, d);
3916 deltas = isl_map_deltas(map);
3917 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3918 isl_set_free(deltas);
3919 if (res < 0)
3920 break;
3922 if (i == 0)
3923 gcd = m;
3924 else
3925 gcd = isl_val_gcd(gcd, m);
3926 if (isl_val_is_one(gcd)) {
3927 isl_val_free(r);
3928 break;
3930 mv = isl_multi_val_set_val(mv, i, r);
3932 res = dim_is_fixed(domain[order[i]].set, depth);
3933 if (res < 0)
3934 break;
3935 if (res)
3936 continue;
3938 if (fixed && i > base) {
3939 isl_val *a, *b;
3940 a = isl_multi_val_get_val(mv, i);
3941 b = isl_multi_val_get_val(mv, base);
3942 if (isl_val_ne(a, b))
3943 fixed = 0;
3944 isl_val_free(a);
3945 isl_val_free(b);
3949 if (res < 0 || !gcd) {
3950 isl_ast_build_free(build);
3951 list = NULL;
3952 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3953 list = generate_shifted_component_from_list(domain,
3954 order, n, build);
3955 } else {
3956 list = generate_shift_component(domain, order, n, gcd, mv,
3957 build);
3960 isl_val_free(gcd);
3961 isl_multi_val_free(mv);
3963 return list;
3964 error:
3965 isl_ast_build_free(build);
3966 return NULL;
3969 /* Store both "map" itself and its domain in the
3970 * structure pointed to by *next and advance to the next array element.
3972 static int extract_domain(__isl_take isl_map *map, void *user)
3974 struct isl_set_map_pair **next = user;
3976 (*next)->map = isl_map_copy(map);
3977 (*next)->set = isl_map_domain(map);
3978 (*next)++;
3980 return 0;
3983 static int after_in_tree(__isl_keep isl_union_map *umap,
3984 __isl_keep isl_schedule_node *node);
3986 /* Is any domain element of "umap" scheduled after any of
3987 * the corresponding image elements by the tree rooted at
3988 * the child of "node"?
3990 static int after_in_child(__isl_keep isl_union_map *umap,
3991 __isl_keep isl_schedule_node *node)
3993 isl_schedule_node *child;
3994 int after;
3996 child = isl_schedule_node_get_child(node, 0);
3997 after = after_in_tree(umap, child);
3998 isl_schedule_node_free(child);
4000 return after;
4003 /* Is any domain element of "umap" scheduled after any of
4004 * the corresponding image elements by the tree rooted at
4005 * the band node "node"?
4007 * We first check if any domain element is scheduled after any
4008 * of the corresponding image elements by the band node itself.
4009 * If not, we restrict "map" to those pairs of element that
4010 * are scheduled together by the band node and continue with
4011 * the child of the band node.
4012 * If there are no such pairs then the map passed to after_in_child
4013 * will be empty causing it to return 0.
4015 static int after_in_band(__isl_keep isl_union_map *umap,
4016 __isl_keep isl_schedule_node *node)
4018 isl_multi_union_pw_aff *mupa;
4019 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4020 isl_union_set *domain, *range;
4021 isl_space *space;
4022 int empty;
4023 int after;
4025 if (isl_schedule_node_band_n_member(node) == 0)
4026 return after_in_child(umap, node);
4028 mupa = isl_schedule_node_band_get_partial_schedule(node);
4029 space = isl_multi_union_pw_aff_get_space(mupa);
4030 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4031 test = isl_union_map_copy(umap);
4032 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4033 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4034 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4035 test = isl_union_map_intersect(test, gt);
4036 empty = isl_union_map_is_empty(test);
4037 isl_union_map_free(test);
4039 if (empty < 0 || !empty) {
4040 isl_union_map_free(partial);
4041 return empty < 0 ? -1 : 1;
4044 universe = isl_union_map_universe(isl_union_map_copy(umap));
4045 domain = isl_union_map_domain(isl_union_map_copy(universe));
4046 range = isl_union_map_range(universe);
4047 umap1 = isl_union_map_copy(partial);
4048 umap1 = isl_union_map_intersect_domain(umap1, domain);
4049 umap2 = isl_union_map_intersect_domain(partial, range);
4050 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4051 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4052 after = after_in_child(test, node);
4053 isl_union_map_free(test);
4054 return after;
4057 /* Is any domain element of "umap" scheduled after any of
4058 * the corresponding image elements by the tree rooted at
4059 * the context node "node"?
4061 * The context constraints apply to the schedule domain,
4062 * so we cannot apply them directly to "umap", which contains
4063 * pairs of statement instances. Instead, we add them
4064 * to the range of the prefix schedule for both domain and
4065 * range of "umap".
4067 static int after_in_context(__isl_keep isl_union_map *umap,
4068 __isl_keep isl_schedule_node *node)
4070 isl_union_map *prefix, *universe, *umap1, *umap2;
4071 isl_union_set *domain, *range;
4072 isl_set *context;
4073 int after;
4075 umap = isl_union_map_copy(umap);
4076 context = isl_schedule_node_context_get_context(node);
4077 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4078 universe = isl_union_map_universe(isl_union_map_copy(umap));
4079 domain = isl_union_map_domain(isl_union_map_copy(universe));
4080 range = isl_union_map_range(universe);
4081 umap1 = isl_union_map_copy(prefix);
4082 umap1 = isl_union_map_intersect_domain(umap1, domain);
4083 umap2 = isl_union_map_intersect_domain(prefix, range);
4084 umap1 = isl_union_map_intersect_range(umap1,
4085 isl_union_set_from_set(context));
4086 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4087 umap = isl_union_map_intersect(umap, umap1);
4089 after = after_in_child(umap, node);
4091 isl_union_map_free(umap);
4093 return after;
4096 /* Is any domain element of "umap" scheduled after any of
4097 * the corresponding image elements by the tree rooted at
4098 * the expansion node "node"?
4100 * We apply the expansion to domain and range of "umap" and
4101 * continue with its child.
4103 static int after_in_expansion(__isl_keep isl_union_map *umap,
4104 __isl_keep isl_schedule_node *node)
4106 isl_union_map *expansion;
4107 int after;
4109 expansion = isl_schedule_node_expansion_get_expansion(node);
4110 umap = isl_union_map_copy(umap);
4111 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4112 umap = isl_union_map_apply_range(umap, expansion);
4114 after = after_in_child(umap, node);
4116 isl_union_map_free(umap);
4118 return after;
4121 /* Is any domain element of "umap" scheduled after any of
4122 * the corresponding image elements by the tree rooted at
4123 * the extension node "node"?
4125 * Since the extension node may add statement instances before or
4126 * after the pairs of statement instances in "umap", we return 1
4127 * to ensure that these pairs are not broken up.
4129 static int after_in_extension(__isl_keep isl_union_map *umap,
4130 __isl_keep isl_schedule_node *node)
4132 return 1;
4135 /* Is any domain element of "umap" scheduled after any of
4136 * the corresponding image elements by the tree rooted at
4137 * the filter node "node"?
4139 * We intersect domain and range of "umap" with the filter and
4140 * continue with its child.
4142 static int after_in_filter(__isl_keep isl_union_map *umap,
4143 __isl_keep isl_schedule_node *node)
4145 isl_union_set *filter;
4146 int after;
4148 umap = isl_union_map_copy(umap);
4149 filter = isl_schedule_node_filter_get_filter(node);
4150 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4151 umap = isl_union_map_intersect_range(umap, filter);
4153 after = after_in_child(umap, node);
4155 isl_union_map_free(umap);
4157 return after;
4160 /* Is any domain element of "umap" scheduled after any of
4161 * the corresponding image elements by the tree rooted at
4162 * the set node "node"?
4164 * This is only the case if this condition holds in any
4165 * of the (filter) children of the set node.
4166 * In particular, if the domain and the range of "umap"
4167 * are contained in different children, then the condition
4168 * does not hold.
4170 static int after_in_set(__isl_keep isl_union_map *umap,
4171 __isl_keep isl_schedule_node *node)
4173 int i, n;
4175 n = isl_schedule_node_n_children(node);
4176 for (i = 0; i < n; ++i) {
4177 isl_schedule_node *child;
4178 int after;
4180 child = isl_schedule_node_get_child(node, i);
4181 after = after_in_tree(umap, child);
4182 isl_schedule_node_free(child);
4184 if (after < 0 || after)
4185 return after;
4188 return 0;
4191 /* Return the filter of child "i" of "node".
4193 static __isl_give isl_union_set *child_filter(
4194 __isl_keep isl_schedule_node *node, int i)
4196 isl_schedule_node *child;
4197 isl_union_set *filter;
4199 child = isl_schedule_node_get_child(node, i);
4200 filter = isl_schedule_node_filter_get_filter(child);
4201 isl_schedule_node_free(child);
4203 return filter;
4206 /* Is any domain element of "umap" scheduled after any of
4207 * the corresponding image elements by the tree rooted at
4208 * the sequence node "node"?
4210 * This happens in particular if any domain element is
4211 * contained in a later child than one containing a range element or
4212 * if the condition holds within a given child in the sequence.
4213 * The later part of the condition is checked by after_in_set.
4215 static int after_in_sequence(__isl_keep isl_union_map *umap,
4216 __isl_keep isl_schedule_node *node)
4218 int i, j, n;
4219 isl_union_map *umap_i;
4220 int empty, after = 0;
4222 n = isl_schedule_node_n_children(node);
4223 for (i = 1; i < n; ++i) {
4224 isl_union_set *filter_i;
4226 umap_i = isl_union_map_copy(umap);
4227 filter_i = child_filter(node, i);
4228 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4229 empty = isl_union_map_is_empty(umap_i);
4230 if (empty < 0)
4231 goto error;
4232 if (empty) {
4233 isl_union_map_free(umap_i);
4234 continue;
4237 for (j = 0; j < i; ++j) {
4238 isl_union_set *filter_j;
4239 isl_union_map *umap_ij;
4241 umap_ij = isl_union_map_copy(umap_i);
4242 filter_j = child_filter(node, j);
4243 umap_ij = isl_union_map_intersect_range(umap_ij,
4244 filter_j);
4245 empty = isl_union_map_is_empty(umap_ij);
4246 isl_union_map_free(umap_ij);
4248 if (empty < 0)
4249 goto error;
4250 if (!empty)
4251 after = 1;
4252 if (after)
4253 break;
4256 isl_union_map_free(umap_i);
4257 if (after)
4258 break;
4261 if (after < 0 || after)
4262 return after;
4264 return after_in_set(umap, node);
4265 error:
4266 isl_union_map_free(umap_i);
4267 return -1;
4270 /* Is any domain element of "umap" scheduled after any of
4271 * the corresponding image elements by the tree rooted at "node"?
4273 * If "umap" is empty, then clearly there is no such element.
4274 * Otherwise, consider the different types of nodes separately.
4276 static int after_in_tree(__isl_keep isl_union_map *umap,
4277 __isl_keep isl_schedule_node *node)
4279 int empty;
4280 enum isl_schedule_node_type type;
4282 empty = isl_union_map_is_empty(umap);
4283 if (empty < 0)
4284 return -1;
4285 if (empty)
4286 return 0;
4287 if (!node)
4288 return -1;
4290 type = isl_schedule_node_get_type(node);
4291 switch (type) {
4292 case isl_schedule_node_error:
4293 return -1;
4294 case isl_schedule_node_leaf:
4295 return 0;
4296 case isl_schedule_node_band:
4297 return after_in_band(umap, node);
4298 case isl_schedule_node_domain:
4299 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4300 "unexpected internal domain node", return -1);
4301 case isl_schedule_node_context:
4302 return after_in_context(umap, node);
4303 case isl_schedule_node_expansion:
4304 return after_in_expansion(umap, node);
4305 case isl_schedule_node_extension:
4306 return after_in_extension(umap, node);
4307 case isl_schedule_node_filter:
4308 return after_in_filter(umap, node);
4309 case isl_schedule_node_guard:
4310 case isl_schedule_node_mark:
4311 return after_in_child(umap, node);
4312 case isl_schedule_node_set:
4313 return after_in_set(umap, node);
4314 case isl_schedule_node_sequence:
4315 return after_in_sequence(umap, node);
4318 return 1;
4321 /* Is any domain element of "map1" scheduled after any domain
4322 * element of "map2" by the subtree underneath the current band node,
4323 * while at the same time being scheduled together by the current
4324 * band node, i.e., by "map1" and "map2?
4326 * If the child of the current band node is a leaf, then
4327 * no element can be scheduled after any other element.
4329 * Otherwise, we construct a relation between domain elements
4330 * of "map1" and domain elements of "map2" that are scheduled
4331 * together and then check if the subtree underneath the current
4332 * band node determines their relative order.
4334 static int after_in_subtree(__isl_keep isl_ast_build *build,
4335 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4337 isl_schedule_node *node;
4338 isl_map *map;
4339 isl_union_map *umap;
4340 int after;
4342 node = isl_ast_build_get_schedule_node(build);
4343 if (!node)
4344 return -1;
4345 node = isl_schedule_node_child(node, 0);
4346 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4347 isl_schedule_node_free(node);
4348 return 0;
4350 map = isl_map_copy(map2);
4351 map = isl_map_apply_domain(map, isl_map_copy(map1));
4352 umap = isl_union_map_from_map(map);
4353 after = after_in_tree(umap, node);
4354 isl_union_map_free(umap);
4355 isl_schedule_node_free(node);
4356 return after;
4359 /* Internal data for any_scheduled_after.
4361 * "build" is the build in which the AST is constructed.
4362 * "depth" is the number of loops that have already been generated
4363 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4364 * "domain" is an array of set-map pairs corresponding to the different
4365 * iteration domains. The set is the schedule domain, i.e., the domain
4366 * of the inverse schedule, while the map is the inverse schedule itself.
4368 struct isl_any_scheduled_after_data {
4369 isl_ast_build *build;
4370 int depth;
4371 int group_coscheduled;
4372 struct isl_set_map_pair *domain;
4375 /* Is any element of domain "i" scheduled after any element of domain "j"
4376 * (for a common iteration of the first data->depth loops)?
4378 * data->domain[i].set contains the domain of the inverse schedule
4379 * for domain "i", i.e., elements in the schedule domain.
4381 * If we are inside a band of a schedule tree and there is a pair
4382 * of elements in the two domains that is schedule together by
4383 * the current band, then we check if any element of "i" may be schedule
4384 * after element of "j" by the descendants of the band node.
4386 * If data->group_coscheduled is set, then we also return 1 if there
4387 * is any pair of elements in the two domains that are scheduled together.
4389 static int any_scheduled_after(int i, int j, void *user)
4391 struct isl_any_scheduled_after_data *data = user;
4392 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4393 int pos;
4395 for (pos = data->depth; pos < dim; ++pos) {
4396 int follows;
4398 follows = isl_set_follows_at(data->domain[i].set,
4399 data->domain[j].set, pos);
4401 if (follows < -1)
4402 return -1;
4403 if (follows > 0)
4404 return 1;
4405 if (follows < 0)
4406 return 0;
4409 if (isl_ast_build_has_schedule_node(data->build)) {
4410 int after;
4412 after = after_in_subtree(data->build, data->domain[i].map,
4413 data->domain[j].map);
4414 if (after < 0 || after)
4415 return after;
4418 return data->group_coscheduled;
4421 /* Look for independent components at the current depth and generate code
4422 * for each component separately. The resulting lists of grafts are
4423 * merged in an attempt to combine grafts with identical guards.
4425 * Code for two domains can be generated separately if all the elements
4426 * of one domain are scheduled before (or together with) all the elements
4427 * of the other domain. We therefore consider the graph with as nodes
4428 * the domains and an edge between two nodes if any element of the first
4429 * node is scheduled after any element of the second node.
4430 * If the ast_build_group_coscheduled is set, then we also add an edge if
4431 * there is any pair of elements in the two domains that are scheduled
4432 * together.
4433 * Code is then generated (by generate_component)
4434 * for each of the strongly connected components in this graph
4435 * in their topological order.
4437 * Since the test is performed on the domain of the inverse schedules of
4438 * the different domains, we precompute these domains and store
4439 * them in data.domain.
4441 static __isl_give isl_ast_graft_list *generate_components(
4442 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4444 int i;
4445 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4446 int n = isl_union_map_n_map(executed);
4447 struct isl_any_scheduled_after_data data;
4448 struct isl_set_map_pair *next;
4449 struct isl_tarjan_graph *g = NULL;
4450 isl_ast_graft_list *list = NULL;
4451 int n_domain = 0;
4453 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4454 if (!data.domain)
4455 goto error;
4456 n_domain = n;
4458 next = data.domain;
4459 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4460 goto error;
4462 if (!build)
4463 goto error;
4464 data.build = build;
4465 data.depth = isl_ast_build_get_depth(build);
4466 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4467 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4468 if (!g)
4469 goto error;
4471 list = isl_ast_graft_list_alloc(ctx, 0);
4473 i = 0;
4474 while (list && n) {
4475 isl_ast_graft_list *list_c;
4476 int first = i;
4478 if (g->order[i] == -1)
4479 isl_die(ctx, isl_error_internal, "cannot happen",
4480 goto error);
4481 ++i; --n;
4482 while (g->order[i] != -1) {
4483 ++i; --n;
4486 list_c = generate_component(data.domain,
4487 g->order + first, i - first,
4488 isl_ast_build_copy(build));
4489 list = isl_ast_graft_list_merge(list, list_c, build);
4491 ++i;
4494 if (0)
4495 error: list = isl_ast_graft_list_free(list);
4496 isl_tarjan_graph_free(g);
4497 for (i = 0; i < n_domain; ++i) {
4498 isl_map_free(data.domain[i].map);
4499 isl_set_free(data.domain[i].set);
4501 free(data.domain);
4502 isl_union_map_free(executed);
4503 isl_ast_build_free(build);
4505 return list;
4508 /* Generate code for the next level (and all inner levels).
4510 * If "executed" is empty, i.e., no code needs to be generated,
4511 * then we return an empty list.
4513 * If we have already generated code for all loop levels, then we pass
4514 * control to generate_inner_level.
4516 * If "executed" lives in a single space, i.e., if code needs to be
4517 * generated for a single domain, then there can only be a single
4518 * component and we go directly to generate_shifted_component.
4519 * Otherwise, we call generate_components to detect the components
4520 * and to call generate_component on each of them separately.
4522 static __isl_give isl_ast_graft_list *generate_next_level(
4523 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4525 int depth;
4527 if (!build || !executed)
4528 goto error;
4530 if (isl_union_map_is_empty(executed)) {
4531 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4532 isl_union_map_free(executed);
4533 isl_ast_build_free(build);
4534 return isl_ast_graft_list_alloc(ctx, 0);
4537 depth = isl_ast_build_get_depth(build);
4538 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4539 return generate_inner_level(executed, build);
4541 if (isl_union_map_n_map(executed) == 1)
4542 return generate_shifted_component(executed, build);
4544 return generate_components(executed, build);
4545 error:
4546 isl_union_map_free(executed);
4547 isl_ast_build_free(build);
4548 return NULL;
4551 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4552 * internal, executed and build are the inputs to generate_code.
4553 * list collects the output.
4555 struct isl_generate_code_data {
4556 int internal;
4557 isl_union_map *executed;
4558 isl_ast_build *build;
4560 isl_ast_graft_list *list;
4563 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4565 * [E -> S] -> D
4567 * with E the external build schedule and S the additional schedule "space",
4568 * reformulate the inverse schedule in terms of the internal schedule domain,
4569 * i.e., return
4571 * [I -> S] -> D
4573 * We first obtain a mapping
4575 * I -> E
4577 * take the inverse and the product with S -> S, resulting in
4579 * [I -> S] -> [E -> S]
4581 * Applying the map to the input produces the desired result.
4583 static __isl_give isl_union_map *internal_executed(
4584 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4585 __isl_keep isl_ast_build *build)
4587 isl_map *id, *proj;
4589 proj = isl_ast_build_get_schedule_map(build);
4590 proj = isl_map_reverse(proj);
4591 space = isl_space_map_from_set(isl_space_copy(space));
4592 id = isl_map_identity(space);
4593 proj = isl_map_product(proj, id);
4594 executed = isl_union_map_apply_domain(executed,
4595 isl_union_map_from_map(proj));
4596 return executed;
4599 /* Generate an AST that visits the elements in the range of data->executed
4600 * in the relative order specified by the corresponding domain element(s)
4601 * for those domain elements that belong to "set".
4602 * Add the result to data->list.
4604 * The caller ensures that "set" is a universe domain.
4605 * "space" is the space of the additional part of the schedule.
4606 * It is equal to the space of "set" if build->domain is parametric.
4607 * Otherwise, it is equal to the range of the wrapped space of "set".
4609 * If the build space is not parametric and
4610 * if isl_ast_build_node_from_schedule_map
4611 * was called from an outside user (data->internal not set), then
4612 * the (inverse) schedule refers to the external build domain and needs to
4613 * be transformed to refer to the internal build domain.
4615 * If the build space is parametric, then we add some of the parameter
4616 * constraints to the executed relation. Adding these constraints
4617 * allows for an earlier detection of conflicts in some cases.
4618 * However, we do not want to divide the executed relation into
4619 * more disjuncts than necessary. We therefore approximate
4620 * the constraints on the parameters by a single disjunct set.
4622 * The build is extended to include the additional part of the schedule.
4623 * If the original build space was not parametric, then the options
4624 * in data->build refer only to the additional part of the schedule
4625 * and they need to be adjusted to refer to the complete AST build
4626 * domain.
4628 * After having adjusted inverse schedule and build, we start generating
4629 * code with the outer loop of the current code generation
4630 * in generate_next_level.
4632 * If the original build space was not parametric, we undo the embedding
4633 * on the resulting isl_ast_node_list so that it can be used within
4634 * the outer AST build.
4636 static int generate_code_in_space(struct isl_generate_code_data *data,
4637 __isl_take isl_set *set, __isl_take isl_space *space)
4639 isl_union_map *executed;
4640 isl_ast_build *build;
4641 isl_ast_graft_list *list;
4642 int embed;
4644 executed = isl_union_map_copy(data->executed);
4645 executed = isl_union_map_intersect_domain(executed,
4646 isl_union_set_from_set(set));
4648 embed = !isl_set_is_params(data->build->domain);
4649 if (embed && !data->internal)
4650 executed = internal_executed(executed, space, data->build);
4651 if (!embed) {
4652 isl_set *domain;
4653 domain = isl_ast_build_get_domain(data->build);
4654 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4655 executed = isl_union_map_intersect_params(executed, domain);
4658 build = isl_ast_build_copy(data->build);
4659 build = isl_ast_build_product(build, space);
4661 list = generate_next_level(executed, build);
4663 list = isl_ast_graft_list_unembed(list, embed);
4665 data->list = isl_ast_graft_list_concat(data->list, list);
4667 return 0;
4670 /* Generate an AST that visits the elements in the range of data->executed
4671 * in the relative order specified by the corresponding domain element(s)
4672 * for those domain elements that belong to "set".
4673 * Add the result to data->list.
4675 * The caller ensures that "set" is a universe domain.
4677 * If the build space S is not parametric, then the space of "set"
4678 * need to be a wrapped relation with S as domain. That is, it needs
4679 * to be of the form
4681 * [S -> T]
4683 * Check this property and pass control to generate_code_in_space
4684 * passing along T.
4685 * If the build space is not parametric, then T is the space of "set".
4687 static int generate_code_set(__isl_take isl_set *set, void *user)
4689 struct isl_generate_code_data *data = user;
4690 isl_space *space, *build_space;
4691 int is_domain;
4693 space = isl_set_get_space(set);
4695 if (isl_set_is_params(data->build->domain))
4696 return generate_code_in_space(data, set, space);
4698 build_space = isl_ast_build_get_space(data->build, data->internal);
4699 space = isl_space_unwrap(space);
4700 is_domain = isl_space_is_domain(build_space, space);
4701 isl_space_free(build_space);
4702 space = isl_space_range(space);
4704 if (is_domain < 0)
4705 goto error;
4706 if (!is_domain)
4707 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4708 "invalid nested schedule space", goto error);
4710 return generate_code_in_space(data, set, space);
4711 error:
4712 isl_set_free(set);
4713 isl_space_free(space);
4714 return -1;
4717 /* Generate an AST that visits the elements in the range of "executed"
4718 * in the relative order specified by the corresponding domain element(s).
4720 * "build" is an isl_ast_build that has either been constructed by
4721 * isl_ast_build_from_context or passed to a callback set by
4722 * isl_ast_build_set_create_leaf.
4723 * In the first case, the space of the isl_ast_build is typically
4724 * a parametric space, although this is currently not enforced.
4725 * In the second case, the space is never a parametric space.
4726 * If the space S is not parametric, then the domain space(s) of "executed"
4727 * need to be wrapped relations with S as domain.
4729 * If the domain of "executed" consists of several spaces, then an AST
4730 * is generated for each of them (in arbitrary order) and the results
4731 * are concatenated.
4733 * If "internal" is set, then the domain "S" above refers to the internal
4734 * schedule domain representation. Otherwise, it refers to the external
4735 * representation, as returned by isl_ast_build_get_schedule_space.
4737 * We essentially run over all the spaces in the domain of "executed"
4738 * and call generate_code_set on each of them.
4740 static __isl_give isl_ast_graft_list *generate_code(
4741 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4742 int internal)
4744 isl_ctx *ctx;
4745 struct isl_generate_code_data data = { 0 };
4746 isl_space *space;
4747 isl_union_set *schedule_domain;
4748 isl_union_map *universe;
4750 if (!build)
4751 goto error;
4752 space = isl_ast_build_get_space(build, 1);
4753 space = isl_space_align_params(space,
4754 isl_union_map_get_space(executed));
4755 space = isl_space_align_params(space,
4756 isl_union_map_get_space(build->options));
4757 build = isl_ast_build_align_params(build, isl_space_copy(space));
4758 executed = isl_union_map_align_params(executed, space);
4759 if (!executed || !build)
4760 goto error;
4762 ctx = isl_ast_build_get_ctx(build);
4764 data.internal = internal;
4765 data.executed = executed;
4766 data.build = build;
4767 data.list = isl_ast_graft_list_alloc(ctx, 0);
4769 universe = isl_union_map_universe(isl_union_map_copy(executed));
4770 schedule_domain = isl_union_map_domain(universe);
4771 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
4772 &data) < 0)
4773 data.list = isl_ast_graft_list_free(data.list);
4775 isl_union_set_free(schedule_domain);
4776 isl_union_map_free(executed);
4778 isl_ast_build_free(build);
4779 return data.list;
4780 error:
4781 isl_union_map_free(executed);
4782 isl_ast_build_free(build);
4783 return NULL;
4786 /* Generate an AST that visits the elements in the domain of "schedule"
4787 * in the relative order specified by the corresponding image element(s).
4789 * "build" is an isl_ast_build that has either been constructed by
4790 * isl_ast_build_from_context or passed to a callback set by
4791 * isl_ast_build_set_create_leaf.
4792 * In the first case, the space of the isl_ast_build is typically
4793 * a parametric space, although this is currently not enforced.
4794 * In the second case, the space is never a parametric space.
4795 * If the space S is not parametric, then the range space(s) of "schedule"
4796 * need to be wrapped relations with S as domain.
4798 * If the range of "schedule" consists of several spaces, then an AST
4799 * is generated for each of them (in arbitrary order) and the results
4800 * are concatenated.
4802 * We first initialize the local copies of the relevant options.
4803 * We do this here rather than when the isl_ast_build is created
4804 * because the options may have changed between the construction
4805 * of the isl_ast_build and the call to isl_generate_code.
4807 * The main computation is performed on an inverse schedule (with
4808 * the schedule domain in the domain and the elements to be executed
4809 * in the range) called "executed".
4811 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
4812 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4814 isl_ast_graft_list *list;
4815 isl_ast_node *node;
4816 isl_union_map *executed;
4818 build = isl_ast_build_copy(build);
4819 build = isl_ast_build_set_single_valued(build, 0);
4820 schedule = isl_union_map_coalesce(schedule);
4821 schedule = isl_union_map_remove_redundancies(schedule);
4822 executed = isl_union_map_reverse(schedule);
4823 list = generate_code(executed, isl_ast_build_copy(build), 0);
4824 node = isl_ast_node_from_graft_list(list, build);
4825 isl_ast_build_free(build);
4827 return node;
4830 /* The old name for isl_ast_build_node_from_schedule_map.
4831 * It is being kept for backward compatibility, but
4832 * it will be removed in the future.
4834 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
4835 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4837 return isl_ast_build_node_from_schedule_map(build, schedule);
4840 /* Generate an AST that visits the elements in the domain of "executed"
4841 * in the relative order specified by the band node "node" and its descendants.
4843 * The relation "executed" maps the outer generated loop iterators
4844 * to the domain elements executed by those iterations.
4846 * If the band is empty, we continue with its descendants.
4847 * Otherwise, we extend the build and the inverse schedule with
4848 * the additional space/partial schedule and continue generating
4849 * an AST in generate_next_level.
4850 * As soon as we have extended the inverse schedule with the additional
4851 * partial schedule, we look for equalities that may exists between
4852 * the old and the new part.
4854 static __isl_give isl_ast_graft_list *build_ast_from_band(
4855 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4856 __isl_take isl_union_map *executed)
4858 isl_space *space;
4859 isl_multi_union_pw_aff *extra;
4860 isl_union_map *extra_umap;
4861 isl_ast_graft_list *list;
4862 unsigned n1, n2;
4864 if (!build || !node || !executed)
4865 goto error;
4867 if (isl_schedule_node_band_n_member(node) == 0)
4868 return build_ast_from_child(build, node, executed);
4870 extra = isl_schedule_node_band_get_partial_schedule(node);
4871 extra = isl_multi_union_pw_aff_align_params(extra,
4872 isl_ast_build_get_space(build, 1));
4873 space = isl_multi_union_pw_aff_get_space(extra);
4875 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
4876 extra_umap = isl_union_map_reverse(extra_umap);
4878 executed = isl_union_map_domain_product(executed, extra_umap);
4879 executed = isl_union_map_detect_equalities(executed);
4881 n1 = isl_ast_build_dim(build, isl_dim_param);
4882 build = isl_ast_build_product(build, space);
4883 n2 = isl_ast_build_dim(build, isl_dim_param);
4884 if (n2 > n1)
4885 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
4886 "band node is not allowed to introduce new parameters",
4887 build = isl_ast_build_free(build));
4888 build = isl_ast_build_set_schedule_node(build, node);
4890 list = generate_next_level(executed, build);
4892 list = isl_ast_graft_list_unembed(list, 1);
4894 return list;
4895 error:
4896 isl_schedule_node_free(node);
4897 isl_union_map_free(executed);
4898 isl_ast_build_free(build);
4899 return NULL;
4902 /* Hoist a list of grafts (in practice containing a single graft)
4903 * from "sub_build" (which includes extra context information)
4904 * to "build".
4906 * In particular, project out all additional parameters introduced
4907 * by the context node from the enforced constraints and the guard
4908 * of the single graft.
4910 static __isl_give isl_ast_graft_list *hoist_out_of_context(
4911 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
4912 __isl_keep isl_ast_build *sub_build)
4914 isl_ast_graft *graft;
4915 isl_basic_set *enforced;
4916 isl_set *guard;
4917 unsigned n_param, extra_param;
4919 if (!build || !sub_build)
4920 return isl_ast_graft_list_free(list);
4922 n_param = isl_ast_build_dim(build, isl_dim_param);
4923 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
4925 if (extra_param == n_param)
4926 return list;
4928 extra_param -= n_param;
4929 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
4930 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
4931 n_param, extra_param);
4932 enforced = isl_basic_set_remove_unknown_divs(enforced);
4933 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
4934 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
4935 n_param, extra_param);
4936 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
4937 guard = isl_set_compute_divs(guard);
4938 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
4939 build, sub_build);
4940 list = isl_ast_graft_list_from_ast_graft(graft);
4942 return list;
4945 /* Generate an AST that visits the elements in the domain of "executed"
4946 * in the relative order specified by the context node "node"
4947 * and its descendants.
4949 * The relation "executed" maps the outer generated loop iterators
4950 * to the domain elements executed by those iterations.
4952 * The context node may introduce additional parameters as well as
4953 * constraints on the outer schedule dimenions or original parameters.
4955 * We add the extra parameters to a new build and the context
4956 * constraints to both the build and (as a single disjunct)
4957 * to the domain of "executed". Since the context constraints
4958 * are specified in terms of the input schedule, we first need
4959 * to map them to the internal schedule domain.
4961 * After constructing the AST from the descendants of "node",
4962 * we combine the list of grafts into a single graft within
4963 * the new build, in order to be able to exploit the additional
4964 * context constraints during this combination.
4966 * Additionally, if the current node is the outermost node in
4967 * the schedule tree (apart from the root domain node), we generate
4968 * all pending guards, again to be able to exploit the additional
4969 * context constraints. We currently do not do this for internal
4970 * context nodes since we may still want to hoist conditions
4971 * to outer AST nodes.
4973 * If the context node introduced any new parameters, then they
4974 * are removed from the set of enforced constraints and guard
4975 * in hoist_out_of_context.
4977 static __isl_give isl_ast_graft_list *build_ast_from_context(
4978 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4979 __isl_take isl_union_map *executed)
4981 isl_set *context;
4982 isl_space *space;
4983 isl_multi_aff *internal2input;
4984 isl_ast_build *sub_build;
4985 isl_ast_graft_list *list;
4986 int n, depth;
4988 depth = isl_schedule_node_get_tree_depth(node);
4989 space = isl_ast_build_get_space(build, 1);
4990 context = isl_schedule_node_context_get_context(node);
4991 context = isl_set_align_params(context, space);
4992 sub_build = isl_ast_build_copy(build);
4993 space = isl_set_get_space(context);
4994 sub_build = isl_ast_build_align_params(sub_build, space);
4995 internal2input = isl_ast_build_get_internal2input(sub_build);
4996 context = isl_set_preimage_multi_aff(context, internal2input);
4997 sub_build = isl_ast_build_restrict_generated(sub_build,
4998 isl_set_copy(context));
4999 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5000 executed = isl_union_map_intersect_domain(executed,
5001 isl_union_set_from_set(context));
5003 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5004 node, executed);
5005 n = isl_ast_graft_list_n_ast_graft(list);
5006 if (n < 0)
5007 list = isl_ast_graft_list_free(list);
5009 list = isl_ast_graft_list_fuse(list, sub_build);
5010 if (depth == 1)
5011 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5012 sub_build);
5013 if (n >= 1)
5014 list = hoist_out_of_context(list, build, sub_build);
5016 isl_ast_build_free(build);
5017 isl_ast_build_free(sub_build);
5019 return list;
5022 /* Generate an AST that visits the elements in the domain of "executed"
5023 * in the relative order specified by the expansion node "node" and
5024 * its descendants.
5026 * The relation "executed" maps the outer generated loop iterators
5027 * to the domain elements executed by those iterations.
5029 * We expand the domain elements by the expansion and
5030 * continue with the descendants of the node.
5032 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5033 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5034 __isl_take isl_union_map *executed)
5036 isl_union_map *expansion;
5037 unsigned n1, n2;
5039 expansion = isl_schedule_node_expansion_get_expansion(node);
5040 expansion = isl_union_map_align_params(expansion,
5041 isl_union_map_get_space(executed));
5043 n1 = isl_union_map_dim(executed, isl_dim_param);
5044 executed = isl_union_map_apply_range(executed, expansion);
5045 n2 = isl_union_map_dim(executed, isl_dim_param);
5046 if (n2 > n1)
5047 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5048 "expansion node is not allowed to introduce "
5049 "new parameters", goto error);
5051 return build_ast_from_child(build, node, executed);
5052 error:
5053 isl_ast_build_free(build);
5054 isl_schedule_node_free(node);
5055 isl_union_map_free(executed);
5056 return NULL;
5059 /* Generate an AST that visits the elements in the domain of "executed"
5060 * in the relative order specified by the extension node "node" and
5061 * its descendants.
5063 * The relation "executed" maps the outer generated loop iterators
5064 * to the domain elements executed by those iterations.
5066 * Extend the inverse schedule with the extension applied to current
5067 * set of generated constraints. Since the extension if formulated
5068 * in terms of the input schedule, it first needs to be transformed
5069 * to refer to the internal schedule.
5071 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5072 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5073 __isl_take isl_union_map *executed)
5075 isl_union_set *schedule_domain;
5076 isl_union_map *extension;
5077 isl_set *set;
5079 set = isl_ast_build_get_generated(build);
5080 schedule_domain = isl_union_set_from_set(set);
5082 extension = isl_schedule_node_extension_get_extension(node);
5084 extension = isl_union_map_preimage_domain_multi_aff(extension,
5085 isl_multi_aff_copy(build->internal2input));
5086 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5087 extension = isl_ast_build_substitute_values_union_map_domain(build,
5088 extension);
5089 executed = isl_union_map_union(executed, extension);
5091 return build_ast_from_child(build, node, executed);
5094 /* Generate an AST that visits the elements in the domain of "executed"
5095 * in the relative order specified by the filter node "node" and
5096 * its descendants.
5098 * The relation "executed" maps the outer generated loop iterators
5099 * to the domain elements executed by those iterations.
5101 * We simply intersect the iteration domain (i.e., the range of "executed")
5102 * with the filter and continue with the descendants of the node,
5103 * unless the resulting inverse schedule is empty, in which
5104 * case we return an empty list.
5106 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5107 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5108 __isl_take isl_union_map *executed)
5110 isl_ctx *ctx;
5111 isl_union_set *filter;
5112 isl_ast_graft_list *list;
5113 int empty;
5114 unsigned n1, n2;
5116 if (!build || !node || !executed)
5117 goto error;
5119 filter = isl_schedule_node_filter_get_filter(node);
5120 filter = isl_union_set_align_params(filter,
5121 isl_union_map_get_space(executed));
5122 n1 = isl_union_map_dim(executed, isl_dim_param);
5123 executed = isl_union_map_intersect_range(executed, filter);
5124 n2 = isl_union_map_dim(executed, isl_dim_param);
5125 if (n2 > n1)
5126 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5127 "filter node is not allowed to introduce "
5128 "new parameters", goto error);
5130 empty = isl_union_map_is_empty(executed);
5131 if (empty < 0)
5132 goto error;
5133 if (!empty)
5134 return build_ast_from_child(build, node, executed);
5136 ctx = isl_ast_build_get_ctx(build);
5137 list = isl_ast_graft_list_alloc(ctx, 0);
5138 isl_ast_build_free(build);
5139 isl_schedule_node_free(node);
5140 isl_union_map_free(executed);
5141 return list;
5142 error:
5143 isl_ast_build_free(build);
5144 isl_schedule_node_free(node);
5145 isl_union_map_free(executed);
5146 return NULL;
5149 /* Generate an AST that visits the elements in the domain of "executed"
5150 * in the relative order specified by the guard node "node" and
5151 * its descendants.
5153 * The relation "executed" maps the outer generated loop iterators
5154 * to the domain elements executed by those iterations.
5156 * Ensure that the associated guard is enforced by the outer AST
5157 * constructs by adding it to the guard of the graft.
5158 * Since we know that we will enforce the guard, we can also include it
5159 * in the generated constraints used to construct an AST for
5160 * the descendant nodes.
5162 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5163 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5164 __isl_take isl_union_map *executed)
5166 isl_space *space;
5167 isl_set *guard, *hoisted;
5168 isl_basic_set *enforced;
5169 isl_ast_build *sub_build;
5170 isl_ast_graft *graft;
5171 isl_ast_graft_list *list;
5172 unsigned n1, n2;
5174 space = isl_ast_build_get_space(build, 1);
5175 guard = isl_schedule_node_guard_get_guard(node);
5176 n1 = isl_space_dim(space, isl_dim_param);
5177 guard = isl_set_align_params(guard, space);
5178 n2 = isl_set_dim(guard, isl_dim_param);
5179 if (n2 > n1)
5180 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5181 "guard node is not allowed to introduce "
5182 "new parameters", guard = isl_set_free(guard));
5183 guard = isl_set_preimage_multi_aff(guard,
5184 isl_multi_aff_copy(build->internal2input));
5185 guard = isl_ast_build_specialize(build, guard);
5186 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5188 sub_build = isl_ast_build_copy(build);
5189 sub_build = isl_ast_build_restrict_generated(sub_build,
5190 isl_set_copy(guard));
5192 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5193 node, executed);
5195 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5196 if (isl_set_n_basic_set(hoisted) > 1)
5197 list = isl_ast_graft_list_gist_guards(list,
5198 isl_set_copy(hoisted));
5199 guard = isl_set_intersect(guard, hoisted);
5200 enforced = extract_shared_enforced(list, build);
5201 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5202 build, sub_build);
5204 isl_ast_build_free(sub_build);
5205 isl_ast_build_free(build);
5206 return isl_ast_graft_list_from_ast_graft(graft);
5209 /* Call the before_each_mark callback, if requested by the user.
5211 * Return 0 on success and -1 on error.
5213 * The caller is responsible for recording the current inverse schedule
5214 * in "build".
5216 static int before_each_mark(__isl_keep isl_id *mark,
5217 __isl_keep isl_ast_build *build)
5219 if (!build)
5220 return -1;
5221 if (!build->before_each_mark)
5222 return 0;
5223 return build->before_each_mark(mark, build,
5224 build->before_each_mark_user);
5227 /* Call the after_each_mark callback, if requested by the user.
5229 * The caller is responsible for recording the current inverse schedule
5230 * in "build".
5232 static __isl_give isl_ast_graft *after_each_mark(
5233 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5235 if (!graft || !build)
5236 return isl_ast_graft_free(graft);
5237 if (!build->after_each_mark)
5238 return graft;
5239 graft->node = build->after_each_mark(graft->node, build,
5240 build->after_each_mark_user);
5241 if (!graft->node)
5242 return isl_ast_graft_free(graft);
5243 return graft;
5247 /* Generate an AST that visits the elements in the domain of "executed"
5248 * in the relative order specified by the mark node "node" and
5249 * its descendants.
5251 * The relation "executed" maps the outer generated loop iterators
5252 * to the domain elements executed by those iterations.
5254 * Since we may be calling before_each_mark and after_each_mark
5255 * callbacks, we record the current inverse schedule in the build.
5257 * We generate an AST for the child of the mark node, combine
5258 * the graft list into a single graft and then insert the mark
5259 * in the AST of that single graft.
5261 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5262 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5263 __isl_take isl_union_map *executed)
5265 isl_id *mark;
5266 isl_ast_graft *graft;
5267 isl_ast_graft_list *list;
5268 int n;
5270 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5272 mark = isl_schedule_node_mark_get_id(node);
5273 if (before_each_mark(mark, build) < 0)
5274 node = isl_schedule_node_free(node);
5276 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5277 list = isl_ast_graft_list_fuse(list, build);
5278 n = isl_ast_graft_list_n_ast_graft(list);
5279 if (n < 0)
5280 list = isl_ast_graft_list_free(list);
5281 if (n == 0) {
5282 isl_id_free(mark);
5283 } else {
5284 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5285 graft = isl_ast_graft_insert_mark(graft, mark);
5286 graft = after_each_mark(graft, build);
5287 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5289 isl_ast_build_free(build);
5291 return list;
5294 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5295 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5296 __isl_take isl_union_map *executed);
5298 /* Generate an AST that visits the elements in the domain of "executed"
5299 * in the relative order specified by the sequence (or set) node "node" and
5300 * its descendants.
5302 * The relation "executed" maps the outer generated loop iterators
5303 * to the domain elements executed by those iterations.
5305 * We simply generate an AST for each of the children and concatenate
5306 * the results.
5308 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5309 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5310 __isl_take isl_union_map *executed)
5312 int i, n;
5313 isl_ctx *ctx;
5314 isl_ast_graft_list *list;
5316 ctx = isl_ast_build_get_ctx(build);
5317 list = isl_ast_graft_list_alloc(ctx, 0);
5319 n = isl_schedule_node_n_children(node);
5320 for (i = 0; i < n; ++i) {
5321 isl_schedule_node *child;
5322 isl_ast_graft_list *list_i;
5324 child = isl_schedule_node_get_child(node, i);
5325 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5326 child, isl_union_map_copy(executed));
5327 list = isl_ast_graft_list_concat(list, list_i);
5329 isl_ast_build_free(build);
5330 isl_schedule_node_free(node);
5331 isl_union_map_free(executed);
5333 return list;
5336 /* Generate an AST that visits the elements in the domain of "executed"
5337 * in the relative order specified by the node "node" and its descendants.
5339 * The relation "executed" maps the outer generated loop iterators
5340 * to the domain elements executed by those iterations.
5342 * If the node is a leaf, then we pass control to generate_inner_level.
5343 * Note that the current build does not refer to any band node, so
5344 * that generate_inner_level will not try to visit the child of
5345 * the leaf node.
5347 * The other node types are handled in separate functions.
5348 * Set nodes are currently treated in the same way as sequence nodes.
5349 * The children of a set node may be executed in any order,
5350 * including the order of the children.
5352 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5353 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5354 __isl_take isl_union_map *executed)
5356 enum isl_schedule_node_type type;
5358 type = isl_schedule_node_get_type(node);
5360 switch (type) {
5361 case isl_schedule_node_error:
5362 goto error;
5363 case isl_schedule_node_leaf:
5364 isl_schedule_node_free(node);
5365 return generate_inner_level(executed, build);
5366 case isl_schedule_node_band:
5367 return build_ast_from_band(build, node, executed);
5368 case isl_schedule_node_context:
5369 return build_ast_from_context(build, node, executed);
5370 case isl_schedule_node_domain:
5371 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5372 "unexpected internal domain node", goto error);
5373 case isl_schedule_node_expansion:
5374 return build_ast_from_expansion(build, node, executed);
5375 case isl_schedule_node_extension:
5376 return build_ast_from_extension(build, node, executed);
5377 case isl_schedule_node_filter:
5378 return build_ast_from_filter(build, node, executed);
5379 case isl_schedule_node_guard:
5380 return build_ast_from_guard(build, node, executed);
5381 case isl_schedule_node_mark:
5382 return build_ast_from_mark(build, node, executed);
5383 case isl_schedule_node_sequence:
5384 case isl_schedule_node_set:
5385 return build_ast_from_sequence(build, node, executed);
5388 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5389 "unhandled type", goto error);
5390 error:
5391 isl_union_map_free(executed);
5392 isl_schedule_node_free(node);
5393 isl_ast_build_free(build);
5395 return NULL;
5398 /* Generate an AST that visits the elements in the domain of "executed"
5399 * in the relative order specified by the (single) child of "node" and
5400 * its descendants.
5402 * The relation "executed" maps the outer generated loop iterators
5403 * to the domain elements executed by those iterations.
5405 * This function is never called on a leaf, set or sequence node,
5406 * so the node always has exactly one child.
5408 static __isl_give isl_ast_graft_list *build_ast_from_child(
5409 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5410 __isl_take isl_union_map *executed)
5412 node = isl_schedule_node_child(node, 0);
5413 return build_ast_from_schedule_node(build, node, executed);
5416 /* Generate an AST that visits the elements in the domain of the domain
5417 * node "node" in the relative order specified by its descendants.
5419 * An initial inverse schedule is created that maps a zero-dimensional
5420 * schedule space to the node domain.
5421 * The input "build" is assumed to have a parametric domain and
5422 * is replaced by the same zero-dimensional schedule space.
5424 * We also add some of the parameter constraints in the build domain
5425 * to the executed relation. Adding these constraints
5426 * allows for an earlier detection of conflicts in some cases.
5427 * However, we do not want to divide the executed relation into
5428 * more disjuncts than necessary. We therefore approximate
5429 * the constraints on the parameters by a single disjunct set.
5431 static __isl_give isl_ast_node *build_ast_from_domain(
5432 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5434 isl_ctx *ctx;
5435 isl_union_set *domain, *schedule_domain;
5436 isl_union_map *executed;
5437 isl_space *space;
5438 isl_set *set;
5439 isl_ast_graft_list *list;
5440 isl_ast_node *ast;
5441 int is_params;
5443 if (!build)
5444 goto error;
5446 ctx = isl_ast_build_get_ctx(build);
5447 space = isl_ast_build_get_space(build, 1);
5448 is_params = isl_space_is_params(space);
5449 isl_space_free(space);
5450 if (is_params < 0)
5451 goto error;
5452 if (!is_params)
5453 isl_die(ctx, isl_error_unsupported,
5454 "expecting parametric initial context", goto error);
5456 domain = isl_schedule_node_domain_get_domain(node);
5457 domain = isl_union_set_coalesce(domain);
5459 space = isl_union_set_get_space(domain);
5460 space = isl_space_set_from_params(space);
5461 build = isl_ast_build_product(build, space);
5463 set = isl_ast_build_get_domain(build);
5464 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5465 schedule_domain = isl_union_set_from_set(set);
5467 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5468 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5469 ast = isl_ast_node_from_graft_list(list, build);
5470 isl_ast_build_free(build);
5472 return ast;
5473 error:
5474 isl_schedule_node_free(node);
5475 isl_ast_build_free(build);
5476 return NULL;
5479 /* Generate an AST that visits the elements in the domain of "schedule"
5480 * in the relative order specified by the schedule tree.
5482 * "build" is an isl_ast_build that has been created using
5483 * isl_ast_build_alloc or isl_ast_build_from_context based
5484 * on a parametric set.
5486 * The construction starts at the root node of the schedule,
5487 * which is assumed to be a domain node.
5489 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5490 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5492 isl_ctx *ctx;
5493 isl_schedule_node *node;
5495 if (!build || !schedule)
5496 goto error;
5498 ctx = isl_ast_build_get_ctx(build);
5500 node = isl_schedule_get_root(schedule);
5501 isl_schedule_free(schedule);
5503 build = isl_ast_build_copy(build);
5504 build = isl_ast_build_set_single_valued(build, 0);
5505 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5506 isl_die(ctx, isl_error_unsupported,
5507 "expecting root domain node",
5508 build = isl_ast_build_free(build));
5509 return build_ast_from_domain(build, node);
5510 error:
5511 isl_schedule_free(schedule);
5512 return NULL;