isl_ast_codegen.c: build_ast_from_extension: only include simple hull of domain
[isl.git] / isl_ast_codegen.c
blob8733e7202f5a4474458e155a8cc46181872beb96
1 /*
2 * Copyright 2012-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <limits.h>
14 #include <isl/aff.h>
15 #include <isl/constraint.h>
16 #include <isl/set.h>
17 #include <isl/ilp.h>
18 #include <isl/union_set.h>
19 #include <isl/union_map.h>
20 #include <isl/schedule_node.h>
21 #include <isl_sort.h>
22 #include <isl_tarjan.h>
23 #include <isl_ast_private.h>
24 #include <isl_ast_build_expr.h>
25 #include <isl_ast_build_private.h>
26 #include <isl_ast_graft_private.h>
28 /* Data used in generate_domain.
30 * "build" is the input build.
31 * "list" collects the results.
33 struct isl_generate_domain_data {
34 isl_ast_build *build;
36 isl_ast_graft_list *list;
39 static __isl_give isl_ast_graft_list *generate_next_level(
40 __isl_take isl_union_map *executed,
41 __isl_take isl_ast_build *build);
42 static __isl_give isl_ast_graft_list *generate_code(
43 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
44 int internal);
46 /* Generate an AST for a single domain based on
47 * the (non single valued) inverse schedule "executed".
49 * We extend the schedule with the iteration domain
50 * and continue generating through a call to generate_code.
52 * In particular, if executed has the form
54 * S -> D
56 * then we continue generating code on
58 * [S -> D] -> D
60 * The extended inverse schedule is clearly single valued
61 * ensuring that the nested generate_code will not reach this function,
62 * but will instead create calls to all elements of D that need
63 * to be executed from the current schedule domain.
65 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
66 struct isl_generate_domain_data *data)
68 isl_map *identity;
69 isl_ast_build *build;
70 isl_ast_graft_list *list;
72 build = isl_ast_build_copy(data->build);
74 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
75 executed = isl_map_domain_product(executed, identity);
76 build = isl_ast_build_set_single_valued(build, 1);
78 list = generate_code(isl_union_map_from_map(executed), build, 1);
80 data->list = isl_ast_graft_list_concat(data->list, list);
82 return isl_stat_ok;
85 /* Call the at_each_domain callback, if requested by the user,
86 * after recording the current inverse schedule in the build.
88 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
89 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
91 if (!graft || !build)
92 return isl_ast_graft_free(graft);
93 if (!build->at_each_domain)
94 return graft;
96 build = isl_ast_build_copy(build);
97 build = isl_ast_build_set_executed(build,
98 isl_union_map_from_map(isl_map_copy(executed)));
99 if (!build)
100 return isl_ast_graft_free(graft);
102 graft->node = build->at_each_domain(graft->node,
103 build, build->at_each_domain_user);
104 isl_ast_build_free(build);
106 if (!graft->node)
107 graft = isl_ast_graft_free(graft);
109 return graft;
112 /* Generate an AST for a single domain based on
113 * the inverse schedule "executed" and add it to data->list.
115 * If there is more than one domain element associated to the current
116 * schedule "time", then we need to continue the generation process
117 * in generate_non_single_valued.
118 * Note that the inverse schedule being single-valued may depend
119 * on constraints that are only available in the original context
120 * domain specified by the user. We therefore first introduce
121 * some of the constraints of data->build->domain. In particular,
122 * we intersect with a single-disjunct approximation of this set.
123 * We perform this approximation to avoid further splitting up
124 * the executed relation, possibly introducing a disjunctive guard
125 * on the statement.
127 * On the other hand, we only perform the test after having taken the gist
128 * of the domain as the resulting map is the one from which the call
129 * expression is constructed. Using this map to construct the call
130 * expression usually yields simpler results.
131 * Because we perform the single-valuedness test on the gisted map,
132 * we may in rare cases fail to recognize that the inverse schedule
133 * is single-valued. This becomes problematic if this happens
134 * from the recursive call through generate_non_single_valued
135 * as we would then end up in an infinite recursion.
136 * We therefore check if we are inside a call to generate_non_single_valued
137 * and revert to the ungisted map if the gisted map turns out not to be
138 * single-valued.
140 * Otherwise, we generate a call expression for the single executed
141 * domain element and put a guard around it based on the (simplified)
142 * domain of "executed".
144 * At this stage, any pending constraints in the build can no longer
145 * be simplified with respect to any enforced constraints since
146 * the call node does not have any enforced constraints.
147 * We therefore turn all pending constraints into guards
148 * (after simplifying them with respect to the already generated
149 * constraints) and add them to both the generated constraints
150 * and the guard of the constructed graft. This guard will ensure
151 * that the constraints are effectively generated.
153 * If the user has set an at_each_domain callback, it is called
154 * on the constructed call expression node.
156 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
158 struct isl_generate_domain_data *data = user;
159 isl_ast_build *build;
160 isl_ast_graft *graft;
161 isl_ast_graft_list *list;
162 isl_set *guard, *domain;
163 isl_map *map = NULL;
164 int empty, sv;
166 domain = isl_ast_build_get_domain(data->build);
167 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
168 executed = isl_map_intersect_domain(executed, domain);
169 empty = isl_map_is_empty(executed);
170 if (empty < 0)
171 goto error;
172 if (empty) {
173 isl_map_free(executed);
174 return isl_stat_ok;
177 executed = isl_map_coalesce(executed);
178 map = isl_map_copy(executed);
179 map = isl_ast_build_compute_gist_map_domain(data->build, map);
180 sv = isl_map_is_single_valued(map);
181 if (sv < 0)
182 goto error;
183 if (!sv) {
184 isl_map_free(map);
185 if (data->build->single_valued)
186 map = isl_map_copy(executed);
187 else
188 return generate_non_single_valued(executed, data);
190 guard = isl_map_domain(isl_map_copy(map));
191 guard = isl_set_compute_divs(guard);
192 guard = isl_set_intersect(guard,
193 isl_ast_build_get_pending(data->build));
194 guard = isl_set_coalesce(guard);
195 guard = isl_ast_build_specialize(data->build, guard);
196 guard = isl_set_gist(guard, isl_ast_build_get_generated(data->build));
198 build = isl_ast_build_copy(data->build);
199 build = isl_ast_build_replace_pending_by_guard(build,
200 isl_set_copy(guard));
201 graft = isl_ast_graft_alloc_domain(map, build);
202 graft = at_each_domain(graft, executed, build);
203 isl_ast_build_free(build);
204 isl_map_free(executed);
205 graft = isl_ast_graft_add_guard(graft, guard, data->build);
207 list = isl_ast_graft_list_from_ast_graft(graft);
208 data->list = isl_ast_graft_list_concat(data->list, list);
210 return isl_stat_ok;
211 error:
212 isl_map_free(map);
213 isl_map_free(executed);
214 return isl_stat_error;
217 /* Call build->create_leaf to a create "leaf" node in the AST,
218 * encapsulate the result in an isl_ast_graft and return the result
219 * as a 1-element list.
221 * Note that the node returned by the user may be an entire tree.
223 * Since the node itself cannot enforce any constraints, we turn
224 * all pending constraints into guards and add them to the resulting
225 * graft to ensure that they will be generated.
227 * Before we pass control to the user, we first clear some information
228 * from the build that is (presumbably) only meaningful
229 * for the current code generation.
230 * This includes the create_leaf callback itself, so we make a copy
231 * of the build first.
233 static __isl_give isl_ast_graft_list *call_create_leaf(
234 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
236 isl_set *guard;
237 isl_ast_node *node;
238 isl_ast_graft *graft;
239 isl_ast_build *user_build;
241 guard = isl_ast_build_get_pending(build);
242 user_build = isl_ast_build_copy(build);
243 user_build = isl_ast_build_replace_pending_by_guard(user_build,
244 isl_set_copy(guard));
245 user_build = isl_ast_build_set_executed(user_build, executed);
246 user_build = isl_ast_build_clear_local_info(user_build);
247 if (!user_build)
248 node = NULL;
249 else
250 node = build->create_leaf(user_build, build->create_leaf_user);
251 graft = isl_ast_graft_alloc(node, build);
252 graft = isl_ast_graft_add_guard(graft, guard, build);
253 isl_ast_build_free(build);
254 return isl_ast_graft_list_from_ast_graft(graft);
257 static __isl_give isl_ast_graft_list *build_ast_from_child(
258 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
259 __isl_take isl_union_map *executed);
261 /* Generate an AST after having handled the complete schedule
262 * of this call to the code generator or the complete band
263 * if we are generating an AST from a schedule tree.
265 * If we are inside a band node, then move on to the child of the band.
267 * If the user has specified a create_leaf callback, control
268 * is passed to the user in call_create_leaf.
270 * Otherwise, we generate one or more calls for each individual
271 * domain in generate_domain.
273 static __isl_give isl_ast_graft_list *generate_inner_level(
274 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
276 isl_ctx *ctx;
277 struct isl_generate_domain_data data = { build };
279 if (!build || !executed)
280 goto error;
282 if (isl_ast_build_has_schedule_node(build)) {
283 isl_schedule_node *node;
284 node = isl_ast_build_get_schedule_node(build);
285 build = isl_ast_build_reset_schedule_node(build);
286 return build_ast_from_child(build, node, executed);
289 if (build->create_leaf)
290 return call_create_leaf(executed, build);
292 ctx = isl_union_map_get_ctx(executed);
293 data.list = isl_ast_graft_list_alloc(ctx, 0);
294 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
295 data.list = isl_ast_graft_list_free(data.list);
297 if (0)
298 error: data.list = NULL;
299 isl_ast_build_free(build);
300 isl_union_map_free(executed);
301 return data.list;
304 /* Call the before_each_for callback, if requested by the user.
306 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
307 __isl_keep isl_ast_build *build)
309 isl_id *id;
311 if (!node || !build)
312 return isl_ast_node_free(node);
313 if (!build->before_each_for)
314 return node;
315 id = build->before_each_for(build, build->before_each_for_user);
316 node = isl_ast_node_set_annotation(node, id);
317 return node;
320 /* Call the after_each_for callback, if requested by the user.
322 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
323 __isl_keep isl_ast_build *build)
325 if (!graft || !build)
326 return isl_ast_graft_free(graft);
327 if (!build->after_each_for)
328 return graft;
329 graft->node = build->after_each_for(graft->node, build,
330 build->after_each_for_user);
331 if (!graft->node)
332 return isl_ast_graft_free(graft);
333 return graft;
336 /* Plug in all the know values of the current and outer dimensions
337 * in the domain of "executed". In principle, we only need to plug
338 * in the known value of the current dimension since the values of
339 * outer dimensions have been plugged in already.
340 * However, it turns out to be easier to just plug in all known values.
342 static __isl_give isl_union_map *plug_in_values(
343 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
345 return isl_ast_build_substitute_values_union_map_domain(build,
346 executed);
349 /* Check if the constraint "c" is a lower bound on dimension "pos",
350 * an upper bound, or independent of dimension "pos".
352 static int constraint_type(isl_constraint *c, int pos)
354 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
355 return 1;
356 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
357 return 2;
358 return 0;
361 /* Compare the types of the constraints "a" and "b",
362 * resulting in constraints that are independent of "depth"
363 * to be sorted before the lower bounds on "depth", which in
364 * turn are sorted before the upper bounds on "depth".
366 static int cmp_constraint(__isl_keep isl_constraint *a,
367 __isl_keep isl_constraint *b, void *user)
369 int *depth = user;
370 int t1 = constraint_type(a, *depth);
371 int t2 = constraint_type(b, *depth);
373 return t1 - t2;
376 /* Extract a lower bound on dimension "pos" from constraint "c".
378 * If the constraint is of the form
380 * a x + f(...) >= 0
382 * then we essentially return
384 * l = ceil(-f(...)/a)
386 * However, if the current dimension is strided, then we need to make
387 * sure that the lower bound we construct is of the form
389 * f + s a
391 * with f the offset and s the stride.
392 * We therefore compute
394 * f + s * ceil((l - f)/s)
396 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
397 int pos, __isl_keep isl_ast_build *build)
399 isl_aff *aff;
401 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
402 aff = isl_aff_ceil(aff);
404 if (isl_ast_build_has_stride(build, pos)) {
405 isl_aff *offset;
406 isl_val *stride;
408 offset = isl_ast_build_get_offset(build, pos);
409 stride = isl_ast_build_get_stride(build, pos);
411 aff = isl_aff_sub(aff, isl_aff_copy(offset));
412 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
413 aff = isl_aff_ceil(aff);
414 aff = isl_aff_scale_val(aff, stride);
415 aff = isl_aff_add(aff, offset);
418 aff = isl_ast_build_compute_gist_aff(build, aff);
420 return aff;
423 /* Return the exact lower bound (or upper bound if "upper" is set)
424 * of "domain" as a piecewise affine expression.
426 * If we are computing a lower bound (of a strided dimension), then
427 * we need to make sure it is of the form
429 * f + s a
431 * where f is the offset and s is the stride.
432 * We therefore need to include the stride constraint before computing
433 * the minimum.
435 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
436 __isl_keep isl_ast_build *build, int upper)
438 isl_set *stride;
439 isl_map *it_map;
440 isl_pw_aff *pa;
441 isl_pw_multi_aff *pma;
443 domain = isl_set_copy(domain);
444 if (!upper) {
445 stride = isl_ast_build_get_stride_constraint(build);
446 domain = isl_set_intersect(domain, stride);
448 it_map = isl_ast_build_map_to_iterator(build, domain);
449 if (upper)
450 pma = isl_map_lexmax_pw_multi_aff(it_map);
451 else
452 pma = isl_map_lexmin_pw_multi_aff(it_map);
453 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
454 isl_pw_multi_aff_free(pma);
455 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
456 pa = isl_pw_aff_coalesce(pa);
458 return pa;
461 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
462 * remove_redundant_lower_bounds.
464 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
465 void *user)
467 return isl_pw_aff_plain_cmp(a, b);
470 /* Given a list of lower bounds "list", remove those that are redundant
471 * with respect to the other bounds in "list" and the domain of "build".
473 * We first sort the bounds in the same way as they would be sorted
474 * by set_for_node_expressions so that we can try and remove the last
475 * bounds first.
477 * For a lower bound to be effective, there needs to be at least
478 * one domain element for which it is larger than all other lower bounds.
479 * For each lower bound we therefore intersect the domain with
480 * the conditions that it is larger than all other bounds and
481 * check whether the result is empty. If so, the bound can be removed.
483 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
484 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
486 int i, j, n;
487 isl_set *domain;
489 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
490 if (!list)
491 return NULL;
493 n = isl_pw_aff_list_n_pw_aff(list);
494 if (n <= 1)
495 return list;
497 domain = isl_ast_build_get_domain(build);
499 for (i = n - 1; i >= 0; --i) {
500 isl_pw_aff *pa_i;
501 isl_set *domain_i;
502 int empty;
504 domain_i = isl_set_copy(domain);
505 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
507 for (j = 0; j < n; ++j) {
508 isl_pw_aff *pa_j;
509 isl_set *better;
511 if (j == i)
512 continue;
514 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
515 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
516 domain_i = isl_set_intersect(domain_i, better);
519 empty = isl_set_is_empty(domain_i);
521 isl_set_free(domain_i);
522 isl_pw_aff_free(pa_i);
524 if (empty < 0)
525 goto error;
526 if (!empty)
527 continue;
528 list = isl_pw_aff_list_drop(list, i, 1);
529 n--;
532 isl_set_free(domain);
534 return list;
535 error:
536 isl_set_free(domain);
537 return isl_pw_aff_list_free(list);
540 /* Extract a lower bound on dimension "pos" from each constraint
541 * in "constraints" and return the list of lower bounds.
542 * If "constraints" has zero elements, then we extract a lower bound
543 * from "domain" instead.
545 * If the current dimension is strided, then the lower bound
546 * is adjusted by lower_bound to match the stride information.
547 * This modification may make one or more lower bounds redundant
548 * with respect to the other lower bounds. We therefore check
549 * for this condition and remove the redundant lower bounds.
551 static __isl_give isl_pw_aff_list *lower_bounds(
552 __isl_keep isl_constraint_list *constraints, int pos,
553 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
555 isl_ctx *ctx;
556 isl_pw_aff_list *list;
557 int i, n;
559 if (!build)
560 return NULL;
562 n = isl_constraint_list_n_constraint(constraints);
563 if (n == 0) {
564 isl_pw_aff *pa;
565 pa = exact_bound(domain, build, 0);
566 return isl_pw_aff_list_from_pw_aff(pa);
569 ctx = isl_ast_build_get_ctx(build);
570 list = isl_pw_aff_list_alloc(ctx,n);
572 for (i = 0; i < n; ++i) {
573 isl_aff *aff;
574 isl_constraint *c;
576 c = isl_constraint_list_get_constraint(constraints, i);
577 aff = lower_bound(c, pos, build);
578 isl_constraint_free(c);
579 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
582 if (isl_ast_build_has_stride(build, pos))
583 list = remove_redundant_lower_bounds(list, build);
585 return list;
588 /* Extract an upper bound on dimension "pos" from each constraint
589 * in "constraints" and return the list of upper bounds.
590 * If "constraints" has zero elements, then we extract an upper bound
591 * from "domain" instead.
593 static __isl_give isl_pw_aff_list *upper_bounds(
594 __isl_keep isl_constraint_list *constraints, int pos,
595 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
597 isl_ctx *ctx;
598 isl_pw_aff_list *list;
599 int i, n;
601 n = isl_constraint_list_n_constraint(constraints);
602 if (n == 0) {
603 isl_pw_aff *pa;
604 pa = exact_bound(domain, build, 1);
605 return isl_pw_aff_list_from_pw_aff(pa);
608 ctx = isl_ast_build_get_ctx(build);
609 list = isl_pw_aff_list_alloc(ctx,n);
611 for (i = 0; i < n; ++i) {
612 isl_aff *aff;
613 isl_constraint *c;
615 c = isl_constraint_list_get_constraint(constraints, i);
616 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
617 isl_constraint_free(c);
618 aff = isl_aff_floor(aff);
619 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
622 return list;
625 /* Return an isl_ast_expr that performs the reduction of type "type"
626 * on AST expressions corresponding to the elements in "list".
628 * The list is assumed to contain at least one element.
629 * If the list contains exactly one element, then the returned isl_ast_expr
630 * simply computes that affine expression.
631 * If the list contains more than one element, then we sort it
632 * using a fairly abitrary but hopefully reasonably stable order.
634 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
635 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
637 int i, n;
638 isl_ctx *ctx;
639 isl_ast_expr *expr;
641 if (!list)
642 return NULL;
644 n = isl_pw_aff_list_n_pw_aff(list);
646 if (n == 1)
647 return isl_ast_build_expr_from_pw_aff_internal(build,
648 isl_pw_aff_list_get_pw_aff(list, 0));
650 ctx = isl_pw_aff_list_get_ctx(list);
651 expr = isl_ast_expr_alloc_op(ctx, type, n);
652 if (!expr)
653 return NULL;
655 list = isl_pw_aff_list_copy(list);
656 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
657 if (!list)
658 return isl_ast_expr_free(expr);
660 for (i = 0; i < n; ++i) {
661 isl_ast_expr *expr_i;
663 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
664 isl_pw_aff_list_get_pw_aff(list, i));
665 if (!expr_i)
666 goto error;
667 expr->u.op.args[i] = expr_i;
670 isl_pw_aff_list_free(list);
671 return expr;
672 error:
673 isl_pw_aff_list_free(list);
674 isl_ast_expr_free(expr);
675 return NULL;
678 /* Add guards implied by the "generated constraints",
679 * but not (necessarily) enforced by the generated AST to "guard".
680 * In particular, if there is any stride constraints,
681 * then add the guard implied by those constraints.
682 * If we have generated a degenerate loop, then add the guard
683 * implied by "bounds" on the outer dimensions, i.e., the guard
684 * that ensures that the single value actually exists.
685 * Since there may also be guards implied by a combination
686 * of these constraints, we first combine them before
687 * deriving the implied constraints.
689 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
690 int degenerate, __isl_keep isl_basic_set *bounds,
691 __isl_keep isl_ast_build *build)
693 int depth, has_stride;
694 isl_space *space;
695 isl_set *dom, *set;
697 depth = isl_ast_build_get_depth(build);
698 has_stride = isl_ast_build_has_stride(build, depth);
699 if (!has_stride && !degenerate)
700 return guard;
702 space = isl_basic_set_get_space(bounds);
703 dom = isl_set_universe(space);
705 if (degenerate) {
706 bounds = isl_basic_set_copy(bounds);
707 bounds = isl_basic_set_drop_constraints_not_involving_dims(
708 bounds, isl_dim_set, depth, 1);
709 set = isl_set_from_basic_set(bounds);
710 dom = isl_set_intersect(dom, set);
713 if (has_stride) {
714 set = isl_ast_build_get_stride_constraint(build);
715 dom = isl_set_intersect(dom, set);
718 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
719 dom = isl_ast_build_compute_gist(build, dom);
720 guard = isl_set_intersect(guard, dom);
722 return guard;
725 /* Update "graft" based on "sub_build" for the degenerate case.
727 * "build" is the build in which graft->node was created
728 * "sub_build" contains information about the current level itself,
729 * including the single value attained.
731 * We set the initialization part of the for loop to the single
732 * value attained by the current dimension.
733 * The increment and condition are not strictly needed as the are known
734 * to be "1" and "iterator <= value" respectively.
736 static __isl_give isl_ast_graft *refine_degenerate(
737 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
738 __isl_keep isl_ast_build *sub_build)
740 isl_pw_aff *value;
742 if (!graft || !sub_build)
743 return isl_ast_graft_free(graft);
745 value = isl_pw_aff_copy(sub_build->value);
747 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
748 value);
749 if (!graft->node->u.f.init)
750 return isl_ast_graft_free(graft);
752 return graft;
755 /* Return the intersection of constraints in "list" as a set.
757 static __isl_give isl_set *intersect_constraints(
758 __isl_keep isl_constraint_list *list)
760 int i, n;
761 isl_basic_set *bset;
763 n = isl_constraint_list_n_constraint(list);
764 if (n < 1)
765 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
766 "expecting at least one constraint", return NULL);
768 bset = isl_basic_set_from_constraint(
769 isl_constraint_list_get_constraint(list, 0));
770 for (i = 1; i < n; ++i) {
771 isl_basic_set *bset_i;
773 bset_i = isl_basic_set_from_constraint(
774 isl_constraint_list_get_constraint(list, i));
775 bset = isl_basic_set_intersect(bset, bset_i);
778 return isl_set_from_basic_set(bset);
781 /* Compute the constraints on the outer dimensions enforced by
782 * graft->node and add those constraints to graft->enforced,
783 * in case the upper bound is expressed as a set "upper".
785 * In particular, if l(...) is a lower bound in "lower", and
787 * -a i + f(...) >= 0 or a i <= f(...)
789 * is an upper bound ocnstraint on the current dimension i,
790 * then the for loop enforces the constraint
792 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
794 * We therefore simply take each lower bound in turn, plug it into
795 * the upper bounds and compute the intersection over all lower bounds.
797 * If a lower bound is a rational expression, then
798 * isl_basic_set_preimage_multi_aff will force this rational
799 * expression to have only integer values. However, the loop
800 * itself does not enforce this integrality constraint. We therefore
801 * use the ceil of the lower bounds instead of the lower bounds themselves.
802 * Other constraints will make sure that the for loop is only executed
803 * when each of the lower bounds attains an integral value.
804 * In particular, potentially rational values only occur in
805 * lower_bound if the offset is a (seemingly) rational expression,
806 * but then outer conditions will make sure that this rational expression
807 * only attains integer values.
809 static __isl_give isl_ast_graft *set_enforced_from_set(
810 __isl_take isl_ast_graft *graft,
811 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
813 isl_space *space;
814 isl_basic_set *enforced;
815 isl_pw_multi_aff *pma;
816 int i, n;
818 if (!graft || !lower)
819 return isl_ast_graft_free(graft);
821 space = isl_set_get_space(upper);
822 enforced = isl_basic_set_universe(isl_space_copy(space));
824 space = isl_space_map_from_set(space);
825 pma = isl_pw_multi_aff_identity(space);
827 n = isl_pw_aff_list_n_pw_aff(lower);
828 for (i = 0; i < n; ++i) {
829 isl_pw_aff *pa;
830 isl_set *enforced_i;
831 isl_basic_set *hull;
832 isl_pw_multi_aff *pma_i;
834 pa = isl_pw_aff_list_get_pw_aff(lower, i);
835 pa = isl_pw_aff_ceil(pa);
836 pma_i = isl_pw_multi_aff_copy(pma);
837 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
838 enforced_i = isl_set_copy(upper);
839 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
840 hull = isl_set_simple_hull(enforced_i);
841 enforced = isl_basic_set_intersect(enforced, hull);
844 isl_pw_multi_aff_free(pma);
846 graft = isl_ast_graft_enforce(graft, enforced);
848 return graft;
851 /* Compute the constraints on the outer dimensions enforced by
852 * graft->node and add those constraints to graft->enforced,
853 * in case the upper bound is expressed as
854 * a list of affine expressions "upper".
856 * The enforced condition is that each lower bound expression is less
857 * than or equal to each upper bound expression.
859 static __isl_give isl_ast_graft *set_enforced_from_list(
860 __isl_take isl_ast_graft *graft,
861 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
863 isl_set *cond;
864 isl_basic_set *enforced;
866 lower = isl_pw_aff_list_copy(lower);
867 upper = isl_pw_aff_list_copy(upper);
868 cond = isl_pw_aff_list_le_set(lower, upper);
869 enforced = isl_set_simple_hull(cond);
870 graft = isl_ast_graft_enforce(graft, enforced);
872 return graft;
875 /* Does "aff" have a negative constant term?
877 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
878 __isl_take isl_aff *aff, void *user)
880 int *neg = user;
881 isl_val *v;
883 v = isl_aff_get_constant_val(aff);
884 *neg = isl_val_is_neg(v);
885 isl_val_free(v);
886 isl_set_free(set);
887 isl_aff_free(aff);
889 return *neg ? isl_stat_ok : isl_stat_error;
892 /* Does "pa" have a negative constant term over its entire domain?
894 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
895 void *user)
897 isl_stat r;
898 int *neg = user;
900 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
901 isl_pw_aff_free(pa);
903 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
906 /* Does each element in "list" have a negative constant term?
908 * The callback terminates the iteration as soon an element has been
909 * found that does not have a negative constant term.
911 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
913 int neg = 1;
915 if (isl_pw_aff_list_foreach(list,
916 &pw_aff_constant_is_negative, &neg) < 0 && neg)
917 return -1;
919 return neg;
922 /* Add 1 to each of the elements in "list", where each of these elements
923 * is defined over the internal schedule space of "build".
925 static __isl_give isl_pw_aff_list *list_add_one(
926 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
928 int i, n;
929 isl_space *space;
930 isl_aff *aff;
931 isl_pw_aff *one;
933 space = isl_ast_build_get_space(build, 1);
934 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
935 aff = isl_aff_add_constant_si(aff, 1);
936 one = isl_pw_aff_from_aff(aff);
938 n = isl_pw_aff_list_n_pw_aff(list);
939 for (i = 0; i < n; ++i) {
940 isl_pw_aff *pa;
941 pa = isl_pw_aff_list_get_pw_aff(list, i);
942 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
943 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
946 isl_pw_aff_free(one);
948 return list;
951 /* Set the condition part of the for node graft->node in case
952 * the upper bound is represented as a list of piecewise affine expressions.
954 * In particular, set the condition to
956 * iterator <= min(list of upper bounds)
958 * If each of the upper bounds has a negative constant term, then
959 * set the condition to
961 * iterator < min(list of (upper bound + 1)s)
964 static __isl_give isl_ast_graft *set_for_cond_from_list(
965 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
966 __isl_keep isl_ast_build *build)
968 int neg;
969 isl_ast_expr *bound, *iterator, *cond;
970 enum isl_ast_op_type type = isl_ast_op_le;
972 if (!graft || !list)
973 return isl_ast_graft_free(graft);
975 neg = list_constant_is_negative(list);
976 if (neg < 0)
977 return isl_ast_graft_free(graft);
978 list = isl_pw_aff_list_copy(list);
979 if (neg) {
980 list = list_add_one(list, build);
981 type = isl_ast_op_lt;
984 bound = reduce_list(isl_ast_op_min, list, build);
985 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
986 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
987 graft->node->u.f.cond = cond;
989 isl_pw_aff_list_free(list);
990 if (!graft->node->u.f.cond)
991 return isl_ast_graft_free(graft);
992 return graft;
995 /* Set the condition part of the for node graft->node in case
996 * the upper bound is represented as a set.
998 static __isl_give isl_ast_graft *set_for_cond_from_set(
999 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1000 __isl_keep isl_ast_build *build)
1002 isl_ast_expr *cond;
1004 if (!graft)
1005 return NULL;
1007 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1008 graft->node->u.f.cond = cond;
1009 if (!graft->node->u.f.cond)
1010 return isl_ast_graft_free(graft);
1011 return graft;
1014 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1015 * the current dimension.
1017 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1019 int depth;
1020 isl_val *v;
1021 isl_ctx *ctx;
1023 if (!build)
1024 return NULL;
1025 ctx = isl_ast_build_get_ctx(build);
1026 depth = isl_ast_build_get_depth(build);
1028 if (!isl_ast_build_has_stride(build, depth))
1029 return isl_ast_expr_alloc_int_si(ctx, 1);
1031 v = isl_ast_build_get_stride(build, depth);
1032 return isl_ast_expr_from_val(v);
1035 /* Should we express the loop condition as
1037 * iterator <= min(list of upper bounds)
1039 * or as a conjunction of constraints?
1041 * The first is constructed from a list of upper bounds.
1042 * The second is constructed from a set.
1044 * If there are no upper bounds in "constraints", then this could mean
1045 * that "domain" simply doesn't have an upper bound or that we didn't
1046 * pick any upper bound. In the first case, we want to generate the
1047 * loop condition as a(n empty) conjunction of constraints
1048 * In the second case, we will compute
1049 * a single upper bound from "domain" and so we use the list form.
1051 * If there are upper bounds in "constraints",
1052 * then we use the list form iff the atomic_upper_bound option is set.
1054 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1055 __isl_keep isl_set *domain, int depth)
1057 if (n_upper > 0)
1058 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1059 else
1060 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1063 /* Fill in the expressions of the for node in graft->node.
1065 * In particular,
1066 * - set the initialization part of the loop to the maximum of the lower bounds
1067 * - extract the increment from the stride of the current dimension
1068 * - construct the for condition either based on a list of upper bounds
1069 * or on a set of upper bound constraints.
1071 static __isl_give isl_ast_graft *set_for_node_expressions(
1072 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1073 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1074 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1076 isl_ast_node *node;
1078 if (!graft)
1079 return NULL;
1081 build = isl_ast_build_copy(build);
1083 node = graft->node;
1084 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1085 node->u.f.inc = for_inc(build);
1087 if (use_list)
1088 graft = set_for_cond_from_list(graft, upper_list, build);
1089 else
1090 graft = set_for_cond_from_set(graft, upper_set, build);
1092 isl_ast_build_free(build);
1094 if (!node->u.f.iterator || !node->u.f.init ||
1095 !node->u.f.cond || !node->u.f.inc)
1096 return isl_ast_graft_free(graft);
1098 return graft;
1101 /* Update "graft" based on "bounds" and "domain" for the generic,
1102 * non-degenerate, case.
1104 * "c_lower" and "c_upper" contain the lower and upper bounds
1105 * that the loop node should express.
1106 * "domain" is the subset of the intersection of the constraints
1107 * for which some code is executed.
1109 * There may be zero lower bounds or zero upper bounds in "constraints"
1110 * in case the list of constraints was created
1111 * based on the atomic option or based on separation with explicit bounds.
1112 * In that case, we use "domain" to derive lower and/or upper bounds.
1114 * We first compute a list of one or more lower bounds.
1116 * Then we decide if we want to express the condition as
1118 * iterator <= min(list of upper bounds)
1120 * or as a conjunction of constraints.
1122 * The set of enforced constraints is then computed either based on
1123 * a list of upper bounds or on a set of upper bound constraints.
1124 * We do not compute any enforced constraints if we were forced
1125 * to compute a lower or upper bound using exact_bound. The domains
1126 * of the resulting expressions may imply some bounds on outer dimensions
1127 * that we do not want to appear in the enforced constraints since
1128 * they are not actually enforced by the corresponding code.
1130 * Finally, we fill in the expressions of the for node.
1132 static __isl_give isl_ast_graft *refine_generic_bounds(
1133 __isl_take isl_ast_graft *graft,
1134 __isl_take isl_constraint_list *c_lower,
1135 __isl_take isl_constraint_list *c_upper,
1136 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1138 int depth;
1139 isl_ctx *ctx;
1140 isl_pw_aff_list *lower;
1141 int use_list;
1142 isl_set *upper_set = NULL;
1143 isl_pw_aff_list *upper_list = NULL;
1144 int n_lower, n_upper;
1146 if (!graft || !c_lower || !c_upper || !build)
1147 goto error;
1149 depth = isl_ast_build_get_depth(build);
1150 ctx = isl_ast_graft_get_ctx(graft);
1152 n_lower = isl_constraint_list_n_constraint(c_lower);
1153 n_upper = isl_constraint_list_n_constraint(c_upper);
1155 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1157 lower = lower_bounds(c_lower, depth, domain, build);
1159 if (use_list)
1160 upper_list = upper_bounds(c_upper, depth, domain, build);
1161 else if (n_upper > 0)
1162 upper_set = intersect_constraints(c_upper);
1163 else
1164 upper_set = isl_set_universe(isl_set_get_space(domain));
1166 if (n_lower == 0 || n_upper == 0)
1168 else if (use_list)
1169 graft = set_enforced_from_list(graft, lower, upper_list);
1170 else
1171 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1173 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1174 upper_set, build);
1176 isl_pw_aff_list_free(lower);
1177 isl_pw_aff_list_free(upper_list);
1178 isl_set_free(upper_set);
1179 isl_constraint_list_free(c_lower);
1180 isl_constraint_list_free(c_upper);
1182 return graft;
1183 error:
1184 isl_constraint_list_free(c_lower);
1185 isl_constraint_list_free(c_upper);
1186 return isl_ast_graft_free(graft);
1189 /* Internal data structure used inside count_constraints to keep
1190 * track of the number of constraints that are independent of dimension "pos",
1191 * the lower bounds in "pos" and the upper bounds in "pos".
1193 struct isl_ast_count_constraints_data {
1194 int pos;
1196 int n_indep;
1197 int n_lower;
1198 int n_upper;
1201 /* Increment data->n_indep, data->lower or data->upper depending
1202 * on whether "c" is independenct of dimensions data->pos,
1203 * a lower bound or an upper bound.
1205 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1207 struct isl_ast_count_constraints_data *data = user;
1209 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1210 data->n_lower++;
1211 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1212 data->n_upper++;
1213 else
1214 data->n_indep++;
1216 isl_constraint_free(c);
1218 return isl_stat_ok;
1221 /* Update "graft" based on "bounds" and "domain" for the generic,
1222 * non-degenerate, case.
1224 * "list" respresent the list of bounds that need to be encoded by
1225 * the for loop. Only the constraints that involve the iterator
1226 * are relevant here. The other constraints are taken care of by
1227 * the caller and are included in the generated constraints of "build".
1228 * "domain" is the subset of the intersection of the constraints
1229 * for which some code is executed.
1230 * "build" is the build in which graft->node was created.
1232 * We separate lower bounds, upper bounds and constraints that
1233 * are independent of the loop iterator.
1235 * The actual for loop bounds are generated in refine_generic_bounds.
1237 static __isl_give isl_ast_graft *refine_generic_split(
1238 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1239 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1241 struct isl_ast_count_constraints_data data;
1242 isl_constraint_list *lower;
1243 isl_constraint_list *upper;
1245 if (!list)
1246 return isl_ast_graft_free(graft);
1248 data.pos = isl_ast_build_get_depth(build);
1250 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1251 if (!list)
1252 return isl_ast_graft_free(graft);
1254 data.n_indep = data.n_lower = data.n_upper = 0;
1255 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1256 isl_constraint_list_free(list);
1257 return isl_ast_graft_free(graft);
1260 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1261 upper = isl_constraint_list_copy(lower);
1262 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1263 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1265 return refine_generic_bounds(graft, lower, upper, domain, build);
1268 /* Update "graft" based on "bounds" and "domain" for the generic,
1269 * non-degenerate, case.
1271 * "bounds" respresent the bounds that need to be encoded by
1272 * the for loop (or a guard around the for loop).
1273 * "domain" is the subset of "bounds" for which some code is executed.
1274 * "build" is the build in which graft->node was created.
1276 * We break up "bounds" into a list of constraints and continue with
1277 * refine_generic_split.
1279 static __isl_give isl_ast_graft *refine_generic(
1280 __isl_take isl_ast_graft *graft,
1281 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1282 __isl_keep isl_ast_build *build)
1284 isl_constraint_list *list;
1286 if (!build || !graft)
1287 return isl_ast_graft_free(graft);
1289 list = isl_basic_set_get_constraint_list(bounds);
1291 graft = refine_generic_split(graft, list, domain, build);
1293 return graft;
1296 /* Create a for node for the current level.
1298 * Mark the for node degenerate if "degenerate" is set.
1300 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1301 int degenerate)
1303 int depth;
1304 isl_id *id;
1305 isl_ast_node *node;
1307 if (!build)
1308 return NULL;
1310 depth = isl_ast_build_get_depth(build);
1311 id = isl_ast_build_get_iterator_id(build, depth);
1312 node = isl_ast_node_alloc_for(id);
1313 if (degenerate)
1314 node = isl_ast_node_for_mark_degenerate(node);
1316 return node;
1319 /* If the ast_build_exploit_nested_bounds option is set, then return
1320 * the constraints enforced by all elements in "list".
1321 * Otherwise, return the universe.
1323 static __isl_give isl_basic_set *extract_shared_enforced(
1324 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1326 isl_ctx *ctx;
1327 isl_space *space;
1329 if (!list)
1330 return NULL;
1332 ctx = isl_ast_graft_list_get_ctx(list);
1333 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1334 return isl_ast_graft_list_extract_shared_enforced(list, build);
1336 space = isl_ast_build_get_space(build, 1);
1337 return isl_basic_set_universe(space);
1340 /* Return the pending constraints of "build" that are not already taken
1341 * care of (by a combination of "enforced" and the generated constraints
1342 * of "build").
1344 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1345 __isl_keep isl_basic_set *enforced)
1347 isl_set *guard, *context;
1349 guard = isl_ast_build_get_pending(build);
1350 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1351 context = isl_set_intersect(context,
1352 isl_ast_build_get_generated(build));
1353 return isl_set_gist(guard, context);
1356 /* Create an AST node for the current dimension based on
1357 * the schedule domain "bounds" and return the node encapsulated
1358 * in an isl_ast_graft.
1360 * "executed" is the current inverse schedule, taking into account
1361 * the bounds in "bounds"
1362 * "domain" is the domain of "executed", with inner dimensions projected out.
1363 * It may be a strict subset of "bounds" in case "bounds" was created
1364 * based on the atomic option or based on separation with explicit bounds.
1366 * "domain" may satisfy additional equalities that result
1367 * from intersecting "executed" with "bounds" in add_node.
1368 * It may also satisfy some global constraints that were dropped out because
1369 * we performed separation with explicit bounds.
1370 * The very first step is then to copy these constraints to "bounds".
1372 * Since we may be calling before_each_for and after_each_for
1373 * callbacks, we record the current inverse schedule in the build.
1375 * We consider three builds,
1376 * "build" is the one in which the current level is created,
1377 * "body_build" is the build in which the next level is created,
1378 * "sub_build" is essentially the same as "body_build", except that
1379 * the depth has not been increased yet.
1381 * "build" already contains information (in strides and offsets)
1382 * about the strides at the current level, but this information is not
1383 * reflected in the build->domain.
1384 * We first add this information and the "bounds" to the sub_build->domain.
1385 * isl_ast_build_set_loop_bounds adds the stride information and
1386 * checks whether the current dimension attains
1387 * only a single value and whether this single value can be represented using
1388 * a single affine expression.
1389 * In the first case, the current level is considered "degenerate".
1390 * In the second, sub-case, the current level is considered "eliminated".
1391 * Eliminated levels don't need to be reflected in the AST since we can
1392 * simply plug in the affine expression. For degenerate, but non-eliminated,
1393 * levels, we do introduce a for node, but mark is as degenerate so that
1394 * it can be printed as an assignment of the single value to the loop
1395 * "iterator".
1397 * If the current level is eliminated, we explicitly plug in the value
1398 * for the current level found by isl_ast_build_set_loop_bounds in the
1399 * inverse schedule. This ensures that if we are working on a slice
1400 * of the domain based on information available in the inverse schedule
1401 * and the build domain, that then this information is also reflected
1402 * in the inverse schedule. This operation also eliminates the current
1403 * dimension from the inverse schedule making sure no inner dimensions depend
1404 * on the current dimension. Otherwise, we create a for node, marking
1405 * it degenerate if appropriate. The initial for node is still incomplete
1406 * and will be completed in either refine_degenerate or refine_generic.
1408 * We then generate a sequence of grafts for the next level,
1409 * create a surrounding graft for the current level and insert
1410 * the for node we created (if the current level is not eliminated).
1411 * Before creating a graft for the current level, we first extract
1412 * hoistable constraints from the child guards and combine them
1413 * with the pending constraints in the build. These constraints
1414 * are used to simplify the child guards and then added to the guard
1415 * of the current graft to ensure that they will be generated.
1416 * If the hoisted guard is a disjunction, then we use it directly
1417 * to gist the guards on the children before intersect it with the
1418 * pending constraints. We do so because this disjunction is typically
1419 * identical to the guards on the children such that these guards
1420 * can be effectively removed completely. After the intersection,
1421 * the gist operation would have a harder time figuring this out.
1423 * Finally, we set the bounds of the for loop in either
1424 * refine_degenerate or refine_generic.
1425 * We do so in a context where the pending constraints of the build
1426 * have been replaced by the guard of the current graft.
1428 static __isl_give isl_ast_graft *create_node_scaled(
1429 __isl_take isl_union_map *executed,
1430 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1431 __isl_take isl_ast_build *build)
1433 int depth;
1434 int degenerate, eliminated;
1435 isl_basic_set *hull;
1436 isl_basic_set *enforced;
1437 isl_set *guard, *hoisted;
1438 isl_ast_node *node = NULL;
1439 isl_ast_graft *graft;
1440 isl_ast_graft_list *children;
1441 isl_ast_build *sub_build;
1442 isl_ast_build *body_build;
1444 domain = isl_ast_build_eliminate_divs(build, domain);
1445 domain = isl_set_detect_equalities(domain);
1446 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1447 bounds = isl_basic_set_intersect(bounds, hull);
1448 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1450 depth = isl_ast_build_get_depth(build);
1451 sub_build = isl_ast_build_copy(build);
1452 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1453 isl_basic_set_copy(bounds));
1454 degenerate = isl_ast_build_has_value(sub_build);
1455 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1456 if (degenerate < 0 || eliminated < 0)
1457 executed = isl_union_map_free(executed);
1458 if (eliminated)
1459 executed = plug_in_values(executed, sub_build);
1460 else
1461 node = create_for(build, degenerate);
1463 body_build = isl_ast_build_copy(sub_build);
1464 body_build = isl_ast_build_increase_depth(body_build);
1465 if (!eliminated)
1466 node = before_each_for(node, body_build);
1467 children = generate_next_level(executed,
1468 isl_ast_build_copy(body_build));
1470 enforced = extract_shared_enforced(children, build);
1471 guard = extract_pending(sub_build, enforced);
1472 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1473 if (isl_set_n_basic_set(hoisted) > 1)
1474 children = isl_ast_graft_list_gist_guards(children,
1475 isl_set_copy(hoisted));
1476 guard = isl_set_intersect(guard, hoisted);
1477 if (!eliminated)
1478 guard = add_implied_guards(guard, degenerate, bounds, build);
1480 graft = isl_ast_graft_alloc_from_children(children,
1481 isl_set_copy(guard), enforced, build, sub_build);
1483 if (!degenerate)
1484 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1485 if (!eliminated) {
1486 isl_ast_build *for_build;
1488 graft = isl_ast_graft_insert_for(graft, node);
1489 for_build = isl_ast_build_copy(build);
1490 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1491 isl_set_copy(guard));
1492 if (degenerate)
1493 graft = refine_degenerate(graft, for_build, sub_build);
1494 else
1495 graft = refine_generic(graft, bounds,
1496 domain, for_build);
1497 isl_ast_build_free(for_build);
1499 isl_set_free(guard);
1500 if (!eliminated)
1501 graft = after_each_for(graft, body_build);
1503 isl_ast_build_free(body_build);
1504 isl_ast_build_free(sub_build);
1505 isl_ast_build_free(build);
1506 isl_basic_set_free(bounds);
1507 isl_set_free(domain);
1509 return graft;
1512 /* Internal data structure for checking if all constraints involving
1513 * the input dimension "depth" are such that the other coefficients
1514 * are multiples of "m", reducing "m" if they are not.
1515 * If "m" is reduced all the way down to "1", then the check has failed
1516 * and we break out of the iteration.
1518 struct isl_check_scaled_data {
1519 int depth;
1520 isl_val *m;
1523 /* If constraint "c" involves the input dimension data->depth,
1524 * then make sure that all the other coefficients are multiples of data->m,
1525 * reducing data->m if needed.
1526 * Break out of the iteration if data->m has become equal to "1".
1528 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1529 void *user)
1531 struct isl_check_scaled_data *data = user;
1532 int i, j, n;
1533 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1534 isl_dim_div };
1536 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1537 isl_constraint_free(c);
1538 return isl_stat_ok;
1541 for (i = 0; i < 4; ++i) {
1542 n = isl_constraint_dim(c, t[i]);
1543 for (j = 0; j < n; ++j) {
1544 isl_val *d;
1546 if (t[i] == isl_dim_in && j == data->depth)
1547 continue;
1548 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1549 continue;
1550 d = isl_constraint_get_coefficient_val(c, t[i], j);
1551 data->m = isl_val_gcd(data->m, d);
1552 if (isl_val_is_one(data->m))
1553 break;
1555 if (j < n)
1556 break;
1559 isl_constraint_free(c);
1561 return i < 4 ? isl_stat_error : isl_stat_ok;
1564 /* For each constraint of "bmap" that involves the input dimension data->depth,
1565 * make sure that all the other coefficients are multiples of data->m,
1566 * reducing data->m if needed.
1567 * Break out of the iteration if data->m has become equal to "1".
1569 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1570 void *user)
1572 isl_stat r;
1574 r = isl_basic_map_foreach_constraint(bmap,
1575 &constraint_check_scaled, user);
1576 isl_basic_map_free(bmap);
1578 return r;
1581 /* For each constraint of "map" that involves the input dimension data->depth,
1582 * make sure that all the other coefficients are multiples of data->m,
1583 * reducing data->m if needed.
1584 * Break out of the iteration if data->m has become equal to "1".
1586 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1588 isl_stat r;
1590 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1591 isl_map_free(map);
1593 return r;
1596 /* Create an AST node for the current dimension based on
1597 * the schedule domain "bounds" and return the node encapsulated
1598 * in an isl_ast_graft.
1600 * "executed" is the current inverse schedule, taking into account
1601 * the bounds in "bounds"
1602 * "domain" is the domain of "executed", with inner dimensions projected out.
1605 * Before moving on to the actual AST node construction in create_node_scaled,
1606 * we first check if the current dimension is strided and if we can scale
1607 * down this stride. Note that we only do this if the ast_build_scale_strides
1608 * option is set.
1610 * In particular, let the current dimension take on values
1612 * f + s a
1614 * with a an integer. We check if we can find an integer m that (obviously)
1615 * divides both f and s.
1617 * If so, we check if the current dimension only appears in constraints
1618 * where the coefficients of the other variables are multiples of m.
1619 * We perform this extra check to avoid the risk of introducing
1620 * divisions by scaling down the current dimension.
1622 * If so, we scale the current dimension down by a factor of m.
1623 * That is, we plug in
1625 * i = m i' (1)
1627 * Note that in principle we could always scale down strided loops
1628 * by plugging in
1630 * i = f + s i'
1632 * but this may result in i' taking on larger values than the original i,
1633 * due to the shift by "f".
1634 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1636 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1637 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1638 __isl_take isl_ast_build *build)
1640 struct isl_check_scaled_data data;
1641 isl_ctx *ctx;
1642 isl_aff *offset;
1643 isl_val *d;
1645 ctx = isl_ast_build_get_ctx(build);
1646 if (!isl_options_get_ast_build_scale_strides(ctx))
1647 return create_node_scaled(executed, bounds, domain, build);
1649 data.depth = isl_ast_build_get_depth(build);
1650 if (!isl_ast_build_has_stride(build, data.depth))
1651 return create_node_scaled(executed, bounds, domain, build);
1653 offset = isl_ast_build_get_offset(build, data.depth);
1654 data.m = isl_ast_build_get_stride(build, data.depth);
1655 if (!data.m)
1656 offset = isl_aff_free(offset);
1657 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1658 d = isl_aff_get_denominator_val(offset);
1659 if (!d)
1660 executed = isl_union_map_free(executed);
1662 if (executed && isl_val_is_divisible_by(data.m, d))
1663 data.m = isl_val_div(data.m, d);
1664 else {
1665 data.m = isl_val_set_si(data.m, 1);
1666 isl_val_free(d);
1669 if (!isl_val_is_one(data.m)) {
1670 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1671 &data) < 0 &&
1672 !isl_val_is_one(data.m))
1673 executed = isl_union_map_free(executed);
1676 if (!isl_val_is_one(data.m)) {
1677 isl_space *space;
1678 isl_multi_aff *ma;
1679 isl_aff *aff;
1680 isl_map *map;
1681 isl_union_map *umap;
1683 space = isl_ast_build_get_space(build, 1);
1684 space = isl_space_map_from_set(space);
1685 ma = isl_multi_aff_identity(space);
1686 aff = isl_multi_aff_get_aff(ma, data.depth);
1687 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1688 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1690 bounds = isl_basic_set_preimage_multi_aff(bounds,
1691 isl_multi_aff_copy(ma));
1692 domain = isl_set_preimage_multi_aff(domain,
1693 isl_multi_aff_copy(ma));
1694 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1695 umap = isl_union_map_from_map(map);
1696 executed = isl_union_map_apply_domain(executed,
1697 isl_union_map_copy(umap));
1698 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1699 umap);
1701 isl_aff_free(offset);
1702 isl_val_free(data.m);
1704 return create_node_scaled(executed, bounds, domain, build);
1707 /* Add the basic set to the list that "user" points to.
1709 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1711 isl_basic_set_list **list = user;
1713 *list = isl_basic_set_list_add(*list, bset);
1715 return isl_stat_ok;
1718 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1720 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1721 __isl_take isl_set *set)
1723 int n;
1724 isl_ctx *ctx;
1725 isl_basic_set_list *list;
1727 if (!set)
1728 return NULL;
1730 ctx = isl_set_get_ctx(set);
1732 n = isl_set_n_basic_set(set);
1733 list = isl_basic_set_list_alloc(ctx, n);
1734 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1735 list = isl_basic_set_list_free(list);
1737 isl_set_free(set);
1738 return list;
1741 /* Generate code for the schedule domain "bounds"
1742 * and add the result to "list".
1744 * We mainly detect strides here and check if the bounds do not
1745 * conflict with the current build domain
1746 * and then pass over control to create_node.
1748 * "bounds" reflects the bounds on the current dimension and possibly
1749 * some extra conditions on outer dimensions.
1750 * It does not, however, include any divs involving the current dimension,
1751 * so it does not capture any stride constraints.
1752 * We therefore need to compute that part of the schedule domain that
1753 * intersects with "bounds" and derive the strides from the result.
1755 static __isl_give isl_ast_graft_list *add_node(
1756 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1757 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1759 isl_ast_graft *graft;
1760 isl_set *domain = NULL;
1761 isl_union_set *uset;
1762 int empty, disjoint;
1764 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1765 executed = isl_union_map_intersect_domain(executed, uset);
1766 empty = isl_union_map_is_empty(executed);
1767 if (empty < 0)
1768 goto error;
1769 if (empty)
1770 goto done;
1772 uset = isl_union_map_domain(isl_union_map_copy(executed));
1773 domain = isl_set_from_union_set(uset);
1774 domain = isl_ast_build_specialize(build, domain);
1776 domain = isl_set_compute_divs(domain);
1777 domain = isl_ast_build_eliminate_inner(build, domain);
1778 disjoint = isl_set_is_disjoint(domain, build->domain);
1779 if (disjoint < 0)
1780 goto error;
1781 if (disjoint)
1782 goto done;
1784 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1786 graft = create_node(executed, bounds, domain,
1787 isl_ast_build_copy(build));
1788 list = isl_ast_graft_list_add(list, graft);
1789 isl_ast_build_free(build);
1790 return list;
1791 error:
1792 list = isl_ast_graft_list_free(list);
1793 done:
1794 isl_set_free(domain);
1795 isl_basic_set_free(bounds);
1796 isl_union_map_free(executed);
1797 isl_ast_build_free(build);
1798 return list;
1801 /* Does any element of i follow or coincide with any element of j
1802 * at the current depth for equal values of the outer dimensions?
1804 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1805 __isl_keep isl_basic_set *j, void *user)
1807 int depth = *(int *) user;
1808 isl_basic_map *test;
1809 isl_bool empty;
1810 int l;
1812 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1813 isl_basic_set_copy(j));
1814 for (l = 0; l < depth; ++l)
1815 test = isl_basic_map_equate(test, isl_dim_in, l,
1816 isl_dim_out, l);
1817 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1818 isl_dim_out, depth);
1819 empty = isl_basic_map_is_empty(test);
1820 isl_basic_map_free(test);
1822 return empty < 0 ? isl_bool_error : !empty;
1825 /* Split up each element of "list" into a part that is related to "bset"
1826 * according to "gt" and a part that is not.
1827 * Return a list that consist of "bset" and all the pieces.
1829 static __isl_give isl_basic_set_list *add_split_on(
1830 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1831 __isl_keep isl_basic_map *gt)
1833 int i, n;
1834 isl_basic_set_list *res;
1836 if (!list)
1837 bset = isl_basic_set_free(bset);
1839 gt = isl_basic_map_copy(gt);
1840 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1841 n = isl_basic_set_list_n_basic_set(list);
1842 res = isl_basic_set_list_from_basic_set(bset);
1843 for (i = 0; res && i < n; ++i) {
1844 isl_basic_set *bset;
1845 isl_set *set1, *set2;
1846 isl_basic_map *bmap;
1847 int empty;
1849 bset = isl_basic_set_list_get_basic_set(list, i);
1850 bmap = isl_basic_map_copy(gt);
1851 bmap = isl_basic_map_intersect_range(bmap, bset);
1852 bset = isl_basic_map_range(bmap);
1853 empty = isl_basic_set_is_empty(bset);
1854 if (empty < 0)
1855 res = isl_basic_set_list_free(res);
1856 if (empty) {
1857 isl_basic_set_free(bset);
1858 bset = isl_basic_set_list_get_basic_set(list, i);
1859 res = isl_basic_set_list_add(res, bset);
1860 continue;
1863 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1864 set1 = isl_set_from_basic_set(bset);
1865 bset = isl_basic_set_list_get_basic_set(list, i);
1866 set2 = isl_set_from_basic_set(bset);
1867 set1 = isl_set_subtract(set2, set1);
1868 set1 = isl_set_make_disjoint(set1);
1870 res = isl_basic_set_list_concat(res,
1871 isl_basic_set_list_from_set(set1));
1873 isl_basic_map_free(gt);
1874 isl_basic_set_list_free(list);
1875 return res;
1878 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1879 __isl_keep isl_basic_set_list *domain_list,
1880 __isl_keep isl_union_map *executed,
1881 __isl_keep isl_ast_build *build);
1883 /* Internal data structure for add_nodes.
1885 * "executed" and "build" are extra arguments to be passed to add_node.
1886 * "list" collects the results.
1888 struct isl_add_nodes_data {
1889 isl_union_map *executed;
1890 isl_ast_build *build;
1892 isl_ast_graft_list *list;
1895 /* Generate code for the schedule domains in "scc"
1896 * and add the results to "list".
1898 * The domains in "scc" form a strongly connected component in the ordering.
1899 * If the number of domains in "scc" is larger than 1, then this means
1900 * that we cannot determine a valid ordering for the domains in the component.
1901 * This should be fairly rare because the individual domains
1902 * have been made disjoint first.
1903 * The problem is that the domains may be integrally disjoint but not
1904 * rationally disjoint. For example, we may have domains
1906 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1908 * These two domains have an empty intersection, but their rational
1909 * relaxations do intersect. It is impossible to order these domains
1910 * in the second dimension because the first should be ordered before
1911 * the second for outer dimension equal to 0, while it should be ordered
1912 * after for outer dimension equal to 1.
1914 * This may happen in particular in case of unrolling since the domain
1915 * of each slice is replaced by its simple hull.
1917 * For each basic set i in "scc" and for each of the following basic sets j,
1918 * we split off that part of the basic set i that shares the outer dimensions
1919 * with j and lies before j in the current dimension.
1920 * We collect all the pieces in a new list that replaces "scc".
1922 * While the elements in "scc" should be disjoint, we double-check
1923 * this property to avoid running into an infinite recursion in case
1924 * they intersect due to some internal error.
1926 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1928 struct isl_add_nodes_data *data = user;
1929 int i, n, depth;
1930 isl_basic_set *bset, *first;
1931 isl_basic_set_list *list;
1932 isl_space *space;
1933 isl_basic_map *gt;
1935 n = isl_basic_set_list_n_basic_set(scc);
1936 bset = isl_basic_set_list_get_basic_set(scc, 0);
1937 if (n == 1) {
1938 isl_basic_set_list_free(scc);
1939 data->list = add_node(data->list,
1940 isl_union_map_copy(data->executed), bset,
1941 isl_ast_build_copy(data->build));
1942 return data->list ? isl_stat_ok : isl_stat_error;
1945 depth = isl_ast_build_get_depth(data->build);
1946 space = isl_basic_set_get_space(bset);
1947 space = isl_space_map_from_set(space);
1948 gt = isl_basic_map_universe(space);
1949 for (i = 0; i < depth; ++i)
1950 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1951 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1953 first = isl_basic_set_copy(bset);
1954 list = isl_basic_set_list_from_basic_set(bset);
1955 for (i = 1; i < n; ++i) {
1956 int disjoint;
1958 bset = isl_basic_set_list_get_basic_set(scc, i);
1960 disjoint = isl_basic_set_is_disjoint(bset, first);
1961 if (disjoint < 0)
1962 list = isl_basic_set_list_free(list);
1963 else if (!disjoint)
1964 isl_die(isl_basic_set_list_get_ctx(scc),
1965 isl_error_internal,
1966 "basic sets in scc are assumed to be disjoint",
1967 list = isl_basic_set_list_free(list));
1969 list = add_split_on(list, bset, gt);
1971 isl_basic_set_free(first);
1972 isl_basic_map_free(gt);
1973 isl_basic_set_list_free(scc);
1974 scc = list;
1975 data->list = isl_ast_graft_list_concat(data->list,
1976 generate_sorted_domains(scc, data->executed, data->build));
1977 isl_basic_set_list_free(scc);
1979 return data->list ? isl_stat_ok : isl_stat_error;
1982 /* Sort the domains in "domain_list" according to the execution order
1983 * at the current depth (for equal values of the outer dimensions),
1984 * generate code for each of them, collecting the results in a list.
1985 * If no code is generated (because the intersection of the inverse schedule
1986 * with the domains turns out to be empty), then an empty list is returned.
1988 * The caller is responsible for ensuring that the basic sets in "domain_list"
1989 * are pair-wise disjoint. It can, however, in principle happen that
1990 * two basic sets should be ordered one way for one value of the outer
1991 * dimensions and the other way for some other value of the outer dimensions.
1992 * We therefore play safe and look for strongly connected components.
1993 * The function add_nodes takes care of handling non-trivial components.
1995 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1996 __isl_keep isl_basic_set_list *domain_list,
1997 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1999 isl_ctx *ctx;
2000 struct isl_add_nodes_data data;
2001 int depth;
2002 int n;
2004 if (!domain_list)
2005 return NULL;
2007 ctx = isl_basic_set_list_get_ctx(domain_list);
2008 n = isl_basic_set_list_n_basic_set(domain_list);
2009 data.list = isl_ast_graft_list_alloc(ctx, n);
2010 if (n == 0)
2011 return data.list;
2012 if (n == 1)
2013 return add_node(data.list, isl_union_map_copy(executed),
2014 isl_basic_set_list_get_basic_set(domain_list, 0),
2015 isl_ast_build_copy(build));
2017 depth = isl_ast_build_get_depth(build);
2018 data.executed = executed;
2019 data.build = build;
2020 if (isl_basic_set_list_foreach_scc(domain_list,
2021 &domain_follows_at_depth, &depth,
2022 &add_nodes, &data) < 0)
2023 data.list = isl_ast_graft_list_free(data.list);
2025 return data.list;
2028 /* Do i and j share any values for the outer dimensions?
2030 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2031 __isl_keep isl_basic_set *j, void *user)
2033 int depth = *(int *) user;
2034 isl_basic_map *test;
2035 isl_bool empty;
2036 int l;
2038 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2039 isl_basic_set_copy(j));
2040 for (l = 0; l < depth; ++l)
2041 test = isl_basic_map_equate(test, isl_dim_in, l,
2042 isl_dim_out, l);
2043 empty = isl_basic_map_is_empty(test);
2044 isl_basic_map_free(test);
2046 return empty < 0 ? isl_bool_error : !empty;
2049 /* Internal data structure for generate_sorted_domains_wrap.
2051 * "n" is the total number of basic sets
2052 * "executed" and "build" are extra arguments to be passed
2053 * to generate_sorted_domains.
2055 * "single" is set to 1 by generate_sorted_domains_wrap if there
2056 * is only a single component.
2057 * "list" collects the results.
2059 struct isl_ast_generate_parallel_domains_data {
2060 int n;
2061 isl_union_map *executed;
2062 isl_ast_build *build;
2064 int single;
2065 isl_ast_graft_list *list;
2068 /* Call generate_sorted_domains on "scc", fuse the result into a list
2069 * with either zero or one graft and collect the these single element
2070 * lists into data->list.
2072 * If there is only one component, i.e., if the number of basic sets
2073 * in the current component is equal to the total number of basic sets,
2074 * then data->single is set to 1 and the result of generate_sorted_domains
2075 * is not fused.
2077 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2078 void *user)
2080 struct isl_ast_generate_parallel_domains_data *data = user;
2081 isl_ast_graft_list *list;
2083 list = generate_sorted_domains(scc, data->executed, data->build);
2084 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2085 if (!data->single)
2086 list = isl_ast_graft_list_fuse(list, data->build);
2087 if (!data->list)
2088 data->list = list;
2089 else
2090 data->list = isl_ast_graft_list_concat(data->list, list);
2092 isl_basic_set_list_free(scc);
2093 if (!data->list)
2094 return isl_stat_error;
2096 return isl_stat_ok;
2099 /* Look for any (weakly connected) components in the "domain_list"
2100 * of domains that share some values of the outer dimensions.
2101 * That is, domains in different components do not share any values
2102 * of the outer dimensions. This means that these components
2103 * can be freely reordered.
2104 * Within each of the components, we sort the domains according
2105 * to the execution order at the current depth.
2107 * If there is more than one component, then generate_sorted_domains_wrap
2108 * fuses the result of each call to generate_sorted_domains
2109 * into a list with either zero or one graft and collects these (at most)
2110 * single element lists into a bigger list. This means that the elements of the
2111 * final list can be freely reordered. In particular, we sort them
2112 * according to an arbitrary but fixed ordering to ease merging of
2113 * graft lists from different components.
2115 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2116 __isl_keep isl_basic_set_list *domain_list,
2117 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2119 int depth;
2120 struct isl_ast_generate_parallel_domains_data data;
2122 if (!domain_list)
2123 return NULL;
2125 data.n = isl_basic_set_list_n_basic_set(domain_list);
2126 if (data.n <= 1)
2127 return generate_sorted_domains(domain_list, executed, build);
2129 depth = isl_ast_build_get_depth(build);
2130 data.list = NULL;
2131 data.executed = executed;
2132 data.build = build;
2133 data.single = 0;
2134 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2135 &generate_sorted_domains_wrap,
2136 &data) < 0)
2137 data.list = isl_ast_graft_list_free(data.list);
2139 if (!data.single)
2140 data.list = isl_ast_graft_list_sort_guard(data.list);
2142 return data.list;
2145 /* Internal data for separate_domain.
2147 * "explicit" is set if we only want to use explicit bounds.
2149 * "domain" collects the separated domains.
2151 struct isl_separate_domain_data {
2152 isl_ast_build *build;
2153 int explicit;
2154 isl_set *domain;
2157 /* Extract implicit bounds on the current dimension for the executed "map".
2159 * The domain of "map" may involve inner dimensions, so we
2160 * need to eliminate them.
2162 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2163 __isl_keep isl_ast_build *build)
2165 isl_set *domain;
2167 domain = isl_map_domain(map);
2168 domain = isl_ast_build_eliminate(build, domain);
2170 return domain;
2173 /* Extract explicit bounds on the current dimension for the executed "map".
2175 * Rather than eliminating the inner dimensions as in implicit_bounds,
2176 * we simply drop any constraints involving those inner dimensions.
2177 * The idea is that most bounds that are implied by constraints on the
2178 * inner dimensions will be enforced by for loops and not by explicit guards.
2179 * There is then no need to separate along those bounds.
2181 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2182 __isl_keep isl_ast_build *build)
2184 isl_set *domain;
2185 int depth, dim;
2187 dim = isl_map_dim(map, isl_dim_out);
2188 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2190 domain = isl_map_domain(map);
2191 depth = isl_ast_build_get_depth(build);
2192 dim = isl_set_dim(domain, isl_dim_set);
2193 domain = isl_set_detect_equalities(domain);
2194 domain = isl_set_drop_constraints_involving_dims(domain,
2195 isl_dim_set, depth + 1, dim - (depth + 1));
2196 domain = isl_set_remove_divs_involving_dims(domain,
2197 isl_dim_set, depth, 1);
2198 domain = isl_set_remove_unknown_divs(domain);
2200 return domain;
2203 /* Split data->domain into pieces that intersect with the range of "map"
2204 * and pieces that do not intersect with the range of "map"
2205 * and then add that part of the range of "map" that does not intersect
2206 * with data->domain.
2208 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2210 struct isl_separate_domain_data *data = user;
2211 isl_set *domain;
2212 isl_set *d1, *d2;
2214 if (data->explicit)
2215 domain = explicit_bounds(map, data->build);
2216 else
2217 domain = implicit_bounds(map, data->build);
2219 domain = isl_set_coalesce(domain);
2220 domain = isl_set_make_disjoint(domain);
2221 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2222 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2223 data->domain = isl_set_intersect(data->domain, domain);
2224 data->domain = isl_set_union(data->domain, d1);
2225 data->domain = isl_set_union(data->domain, d2);
2227 return isl_stat_ok;
2230 /* Separate the schedule domains of "executed".
2232 * That is, break up the domain of "executed" into basic sets,
2233 * such that for each basic set S, every element in S is associated with
2234 * the same domain spaces.
2236 * "space" is the (single) domain space of "executed".
2238 static __isl_give isl_set *separate_schedule_domains(
2239 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2240 __isl_keep isl_ast_build *build)
2242 struct isl_separate_domain_data data = { build };
2243 isl_ctx *ctx;
2245 ctx = isl_ast_build_get_ctx(build);
2246 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2247 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2248 data.domain = isl_set_empty(space);
2249 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2250 data.domain = isl_set_free(data.domain);
2252 isl_union_map_free(executed);
2253 return data.domain;
2256 /* Temporary data used during the search for a lower bound for unrolling.
2258 * "build" is the build in which the unrolling will be performed
2259 * "domain" is the original set for which to find a lower bound
2260 * "depth" is the dimension for which to find a lower boudn
2261 * "expansion" is the expansion that needs to be applied to "domain"
2262 * in the unrolling that will be performed
2264 * "lower" is the best lower bound found so far. It is NULL if we have not
2265 * found any yet.
2266 * "n" is the corresponding size. If lower is NULL, then the value of n
2267 * is undefined.
2268 * "n_div" is the maximal number of integer divisions in the first
2269 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2270 * been computed yet.
2272 struct isl_find_unroll_data {
2273 isl_ast_build *build;
2274 isl_set *domain;
2275 int depth;
2276 isl_basic_map *expansion;
2278 isl_aff *lower;
2279 int *n;
2280 int n_div;
2283 /* Return the constraint
2285 * i_"depth" = aff + offset
2287 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2288 int offset)
2290 aff = isl_aff_copy(aff);
2291 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2292 aff = isl_aff_add_constant_si(aff, offset);
2293 return isl_equality_from_aff(aff);
2296 /* Update *user to the number of integer divsions in the first element
2297 * of "ma", if it is larger than the current value.
2299 static isl_stat update_n_div(__isl_take isl_set *set,
2300 __isl_take isl_multi_aff *ma, void *user)
2302 isl_aff *aff;
2303 int *n = user;
2304 int n_div;
2306 aff = isl_multi_aff_get_aff(ma, 0);
2307 n_div = isl_aff_dim(aff, isl_dim_div);
2308 isl_aff_free(aff);
2309 isl_multi_aff_free(ma);
2310 isl_set_free(set);
2312 if (n_div > *n)
2313 *n = n_div;
2315 return aff ? isl_stat_ok : isl_stat_error;
2318 /* Get the number of integer divisions in the expression for the iterator
2319 * value at the first slice in the unrolling based on lower bound "lower",
2320 * taking into account the expansion that needs to be performed on this slice.
2322 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2323 __isl_keep isl_aff *lower)
2325 isl_constraint *c;
2326 isl_set *set;
2327 isl_map *it_map, *expansion;
2328 isl_pw_multi_aff *pma;
2329 int n;
2331 c = at_offset(data->depth, lower, 0);
2332 set = isl_set_copy(data->domain);
2333 set = isl_set_add_constraint(set, c);
2334 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2335 set = isl_set_apply(set, expansion);
2336 it_map = isl_ast_build_map_to_iterator(data->build, set);
2337 pma = isl_pw_multi_aff_from_map(it_map);
2338 n = 0;
2339 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2340 n = -1;
2341 isl_pw_multi_aff_free(pma);
2343 return n;
2346 /* Is the lower bound "lower" with corresponding iteration count "n"
2347 * better than the one stored in "data"?
2348 * If there is no upper bound on the iteration count ("n" is infinity) or
2349 * if the count is too large, then we cannot use this lower bound.
2350 * Otherwise, if there was no previous lower bound or
2351 * if the iteration count of the new lower bound is smaller than
2352 * the iteration count of the previous lower bound, then we consider
2353 * the new lower bound to be better.
2354 * If the iteration count is the same, then compare the number
2355 * of integer divisions that would be needed to express
2356 * the iterator value at the first slice in the unrolling
2357 * according to the lower bound. If we end up computing this
2358 * number, then store the lowest value in data->n_div.
2360 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2361 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2363 int cmp;
2364 int n_div;
2366 if (!n)
2367 return -1;
2368 if (isl_val_is_infty(n))
2369 return 0;
2370 if (isl_val_cmp_si(n, INT_MAX) > 0)
2371 return 0;
2372 if (!data->lower)
2373 return 1;
2374 cmp = isl_val_cmp_si(n, *data->n);
2375 if (cmp < 0)
2376 return 1;
2377 if (cmp > 0)
2378 return 0;
2379 if (data->n_div < 0)
2380 data->n_div = get_expanded_n_div(data, data->lower);
2381 if (data->n_div < 0)
2382 return -1;
2383 if (data->n_div == 0)
2384 return 0;
2385 n_div = get_expanded_n_div(data, lower);
2386 if (n_div < 0)
2387 return -1;
2388 if (n_div >= data->n_div)
2389 return 0;
2390 data->n_div = n_div;
2392 return 1;
2395 /* Check if we can use "c" as a lower bound and if it is better than
2396 * any previously found lower bound.
2398 * If "c" does not involve the dimension at the current depth,
2399 * then we cannot use it.
2400 * Otherwise, let "c" be of the form
2402 * i >= f(j)/a
2404 * We compute the maximal value of
2406 * -ceil(f(j)/a)) + i + 1
2408 * over the domain. If there is such a value "n", then we know
2410 * -ceil(f(j)/a)) + i + 1 <= n
2412 * or
2414 * i < ceil(f(j)/a)) + n
2416 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2417 * We just need to check if we have found any lower bound before and
2418 * if the new lower bound is better (smaller n or fewer integer divisions)
2419 * than the previously found lower bounds.
2421 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2422 __isl_keep isl_constraint *c)
2424 isl_aff *aff, *lower;
2425 isl_val *max;
2426 int better;
2428 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2429 return isl_stat_ok;
2431 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2432 lower = isl_aff_ceil(lower);
2433 aff = isl_aff_copy(lower);
2434 aff = isl_aff_neg(aff);
2435 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2436 aff = isl_aff_add_constant_si(aff, 1);
2437 max = isl_set_max_val(data->domain, aff);
2438 isl_aff_free(aff);
2440 better = is_better_lower_bound(data, lower, max);
2441 if (better < 0 || !better) {
2442 isl_val_free(max);
2443 isl_aff_free(lower);
2444 return better < 0 ? isl_stat_error : isl_stat_ok;
2447 isl_aff_free(data->lower);
2448 data->lower = lower;
2449 *data->n = isl_val_get_num_si(max);
2450 isl_val_free(max);
2452 return isl_stat_ok;
2455 /* Check if we can use "c" as a lower bound and if it is better than
2456 * any previously found lower bound.
2458 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2460 struct isl_find_unroll_data *data;
2461 isl_stat r;
2463 data = (struct isl_find_unroll_data *) user;
2464 r = update_unrolling_lower_bound(data, c);
2465 isl_constraint_free(c);
2467 return r;
2470 /* Look for a lower bound l(i) on the dimension at "depth"
2471 * and a size n such that "domain" is a subset of
2473 * { [i] : l(i) <= i_d < l(i) + n }
2475 * where d is "depth" and l(i) depends only on earlier dimensions.
2476 * Furthermore, try and find a lower bound such that n is as small as possible.
2477 * In particular, "n" needs to be finite.
2478 * "build" is the build in which the unrolling will be performed.
2479 * "expansion" is the expansion that needs to be applied to "domain"
2480 * in the unrolling that will be performed.
2482 * Inner dimensions have been eliminated from "domain" by the caller.
2484 * We first construct a collection of lower bounds on the input set
2485 * by computing its simple hull. We then iterate through them,
2486 * discarding those that we cannot use (either because they do not
2487 * involve the dimension at "depth" or because they have no corresponding
2488 * upper bound, meaning that "n" would be unbounded) and pick out the
2489 * best from the remaining ones.
2491 * If we cannot find a suitable lower bound, then we consider that
2492 * to be an error.
2494 static __isl_give isl_aff *find_unroll_lower_bound(
2495 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2496 int depth, __isl_keep isl_basic_map *expansion, int *n)
2498 struct isl_find_unroll_data data =
2499 { build, domain, depth, expansion, NULL, n, -1 };
2500 isl_basic_set *hull;
2502 hull = isl_set_simple_hull(isl_set_copy(domain));
2504 if (isl_basic_set_foreach_constraint(hull,
2505 &constraint_find_unroll, &data) < 0)
2506 goto error;
2508 isl_basic_set_free(hull);
2510 if (!data.lower)
2511 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2512 "cannot find lower bound for unrolling", return NULL);
2514 return data.lower;
2515 error:
2516 isl_basic_set_free(hull);
2517 return isl_aff_free(data.lower);
2520 /* Call "fn" on each iteration of the current dimension of "domain".
2521 * If "init" is not NULL, then it is called with the number of
2522 * iterations before any call to "fn".
2523 * Return -1 on failure.
2525 * Since we are going to be iterating over the individual values,
2526 * we first check if there are any strides on the current dimension.
2527 * If there is, we rewrite the current dimension i as
2529 * i = stride i' + offset
2531 * and then iterate over individual values of i' instead.
2533 * We then look for a lower bound on i' and a size such that the domain
2534 * is a subset of
2536 * { [j,i'] : l(j) <= i' < l(j) + n }
2538 * and then take slices of the domain at values of i'
2539 * between l(j) and l(j) + n - 1.
2541 * We compute the unshifted simple hull of each slice to ensure that
2542 * we have a single basic set per offset. The slicing constraint
2543 * may get simplified away before the unshifted simple hull is taken
2544 * and may therefore in some rare cases disappear from the result.
2545 * We therefore explicitly add the constraint back after computing
2546 * the unshifted simple hull to ensure that the basic sets
2547 * remain disjoint. The constraints that are dropped by taking the hull
2548 * will be taken into account at the next level, as in the case of the
2549 * atomic option.
2551 * Finally, we map i' back to i and call "fn".
2553 static int foreach_iteration(__isl_take isl_set *domain,
2554 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2555 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2557 int i, n;
2558 int empty;
2559 int depth;
2560 isl_multi_aff *expansion;
2561 isl_basic_map *bmap;
2562 isl_aff *lower = NULL;
2563 isl_ast_build *stride_build;
2565 depth = isl_ast_build_get_depth(build);
2567 domain = isl_ast_build_eliminate_inner(build, domain);
2568 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2569 stride_build = isl_ast_build_copy(build);
2570 stride_build = isl_ast_build_detect_strides(stride_build,
2571 isl_set_copy(domain));
2572 expansion = isl_ast_build_get_stride_expansion(stride_build);
2574 domain = isl_set_preimage_multi_aff(domain,
2575 isl_multi_aff_copy(expansion));
2576 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2577 isl_ast_build_free(stride_build);
2579 bmap = isl_basic_map_from_multi_aff(expansion);
2581 empty = isl_set_is_empty(domain);
2582 if (empty < 0) {
2583 n = -1;
2584 } else if (empty) {
2585 n = 0;
2586 } else {
2587 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2588 if (!lower)
2589 n = -1;
2591 if (n >= 0 && init && init(n, user) < 0)
2592 n = -1;
2593 for (i = 0; i < n; ++i) {
2594 isl_set *set;
2595 isl_basic_set *bset;
2596 isl_constraint *slice;
2598 slice = at_offset(depth, lower, i);
2599 set = isl_set_copy(domain);
2600 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2601 bset = isl_set_unshifted_simple_hull(set);
2602 bset = isl_basic_set_add_constraint(bset, slice);
2603 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2605 if (fn(bset, user) < 0)
2606 break;
2609 isl_aff_free(lower);
2610 isl_set_free(domain);
2611 isl_basic_map_free(bmap);
2613 return n < 0 || i < n ? -1 : 0;
2616 /* Data structure for storing the results and the intermediate objects
2617 * of compute_domains.
2619 * "list" is the main result of the function and contains a list
2620 * of disjoint basic sets for which code should be generated.
2622 * "executed" and "build" are inputs to compute_domains.
2623 * "schedule_domain" is the domain of "executed".
2625 * "option" constains the domains at the current depth that should by
2626 * atomic, separated or unrolled. These domains are as specified by
2627 * the user, except that inner dimensions have been eliminated and
2628 * that they have been made pair-wise disjoint.
2630 * "sep_class" contains the user-specified split into separation classes
2631 * specialized to the current depth.
2632 * "done" contains the union of the separation domains that have already
2633 * been handled.
2635 struct isl_codegen_domains {
2636 isl_basic_set_list *list;
2638 isl_union_map *executed;
2639 isl_ast_build *build;
2640 isl_set *schedule_domain;
2642 isl_set *option[4];
2644 isl_map *sep_class;
2645 isl_set *done;
2648 /* Internal data structure for do_unroll.
2650 * "domains" stores the results of compute_domains.
2651 * "class_domain" is the original class domain passed to do_unroll.
2652 * "unroll_domain" collects the unrolled iterations.
2654 struct isl_ast_unroll_data {
2655 struct isl_codegen_domains *domains;
2656 isl_set *class_domain;
2657 isl_set *unroll_domain;
2660 /* Given an iteration of an unrolled domain represented by "bset",
2661 * add it to data->domains->list.
2662 * Since we may have dropped some constraints, we intersect with
2663 * the class domain again to ensure that each element in the list
2664 * is disjoint from the other class domains.
2666 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2668 struct isl_ast_unroll_data *data = user;
2669 isl_set *set;
2670 isl_basic_set_list *list;
2672 set = isl_set_from_basic_set(bset);
2673 data->unroll_domain = isl_set_union(data->unroll_domain,
2674 isl_set_copy(set));
2675 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2676 set = isl_set_make_disjoint(set);
2677 list = isl_basic_set_list_from_set(set);
2678 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2679 list);
2681 return 0;
2684 /* Extend domains->list with a list of basic sets, one for each value
2685 * of the current dimension in "domain" and remove the corresponding
2686 * sets from the class domain. Return the updated class domain.
2687 * The divs that involve the current dimension have not been projected out
2688 * from this domain.
2690 * We call foreach_iteration to iterate over the individual values and
2691 * in do_unroll_iteration we collect the individual basic sets in
2692 * domains->list and their union in data->unroll_domain, which is then
2693 * used to update the class domain.
2695 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2696 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2698 struct isl_ast_unroll_data data;
2700 if (!domain)
2701 return isl_set_free(class_domain);
2702 if (!class_domain)
2703 return isl_set_free(domain);
2705 data.domains = domains;
2706 data.class_domain = class_domain;
2707 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2709 if (foreach_iteration(domain, domains->build, NULL,
2710 &do_unroll_iteration, &data) < 0)
2711 data.unroll_domain = isl_set_free(data.unroll_domain);
2713 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2715 return class_domain;
2718 /* Add domains to domains->list for each individual value of the current
2719 * dimension, for that part of the schedule domain that lies in the
2720 * intersection of the option domain and the class domain.
2721 * Remove the corresponding sets from the class domain and
2722 * return the updated class domain.
2724 * We first break up the unroll option domain into individual pieces
2725 * and then handle each of them separately. The unroll option domain
2726 * has been made disjoint in compute_domains_init_options,
2728 * Note that we actively want to combine different pieces of the
2729 * schedule domain that have the same value at the current dimension.
2730 * We therefore need to break up the unroll option domain before
2731 * intersecting with class and schedule domain, hoping that the
2732 * unroll option domain specified by the user is relatively simple.
2734 static __isl_give isl_set *compute_unroll_domains(
2735 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2737 isl_set *unroll_domain;
2738 isl_basic_set_list *unroll_list;
2739 int i, n;
2740 int empty;
2742 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2743 if (empty < 0)
2744 return isl_set_free(class_domain);
2745 if (empty)
2746 return class_domain;
2748 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2749 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2751 n = isl_basic_set_list_n_basic_set(unroll_list);
2752 for (i = 0; i < n; ++i) {
2753 isl_basic_set *bset;
2755 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2756 unroll_domain = isl_set_from_basic_set(bset);
2757 unroll_domain = isl_set_intersect(unroll_domain,
2758 isl_set_copy(class_domain));
2759 unroll_domain = isl_set_intersect(unroll_domain,
2760 isl_set_copy(domains->schedule_domain));
2762 empty = isl_set_is_empty(unroll_domain);
2763 if (empty >= 0 && empty) {
2764 isl_set_free(unroll_domain);
2765 continue;
2768 class_domain = do_unroll(domains, unroll_domain, class_domain);
2771 isl_basic_set_list_free(unroll_list);
2773 return class_domain;
2776 /* Try and construct a single basic set that includes the intersection of
2777 * the schedule domain, the atomic option domain and the class domain.
2778 * Add the resulting basic set(s) to domains->list and remove them
2779 * from class_domain. Return the updated class domain.
2781 * We construct a single domain rather than trying to combine
2782 * the schedule domains of individual domains because we are working
2783 * within a single component so that non-overlapping schedule domains
2784 * should already have been separated.
2785 * We do however need to make sure that this single domains is a subset
2786 * of the class domain so that it would not intersect with any other
2787 * class domains. This means that we may end up splitting up the atomic
2788 * domain in case separation classes are being used.
2790 * "domain" is the intersection of the schedule domain and the class domain,
2791 * with inner dimensions projected out.
2793 static __isl_give isl_set *compute_atomic_domain(
2794 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2796 isl_basic_set *bset;
2797 isl_basic_set_list *list;
2798 isl_set *domain, *atomic_domain;
2799 int empty;
2801 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2802 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2803 domain = isl_set_intersect(domain,
2804 isl_set_copy(domains->schedule_domain));
2805 empty = isl_set_is_empty(domain);
2806 if (empty < 0)
2807 class_domain = isl_set_free(class_domain);
2808 if (empty) {
2809 isl_set_free(domain);
2810 return class_domain;
2813 domain = isl_ast_build_eliminate(domains->build, domain);
2814 domain = isl_set_coalesce(domain);
2815 bset = isl_set_unshifted_simple_hull(domain);
2816 domain = isl_set_from_basic_set(bset);
2817 atomic_domain = isl_set_copy(domain);
2818 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2819 class_domain = isl_set_subtract(class_domain, atomic_domain);
2820 domain = isl_set_make_disjoint(domain);
2821 list = isl_basic_set_list_from_set(domain);
2822 domains->list = isl_basic_set_list_concat(domains->list, list);
2824 return class_domain;
2827 /* Split up the schedule domain into uniform basic sets,
2828 * in the sense that each element in a basic set is associated to
2829 * elements of the same domains, and add the result to domains->list.
2830 * Do this for that part of the schedule domain that lies in the
2831 * intersection of "class_domain" and the separate option domain.
2833 * "class_domain" may or may not include the constraints
2834 * of the schedule domain, but this does not make a difference
2835 * since we are going to intersect it with the domain of the inverse schedule.
2836 * If it includes schedule domain constraints, then they may involve
2837 * inner dimensions, but we will eliminate them in separation_domain.
2839 static int compute_separate_domain(struct isl_codegen_domains *domains,
2840 __isl_keep isl_set *class_domain)
2842 isl_space *space;
2843 isl_set *domain;
2844 isl_union_map *executed;
2845 isl_basic_set_list *list;
2846 int empty;
2848 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2849 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2850 executed = isl_union_map_copy(domains->executed);
2851 executed = isl_union_map_intersect_domain(executed,
2852 isl_union_set_from_set(domain));
2853 empty = isl_union_map_is_empty(executed);
2854 if (empty < 0 || empty) {
2855 isl_union_map_free(executed);
2856 return empty < 0 ? -1 : 0;
2859 space = isl_set_get_space(class_domain);
2860 domain = separate_schedule_domains(space, executed, domains->build);
2862 list = isl_basic_set_list_from_set(domain);
2863 domains->list = isl_basic_set_list_concat(domains->list, list);
2865 return 0;
2868 /* Split up the domain at the current depth into disjoint
2869 * basic sets for which code should be generated separately
2870 * for the given separation class domain.
2872 * If any separation classes have been defined, then "class_domain"
2873 * is the domain of the current class and does not refer to inner dimensions.
2874 * Otherwise, "class_domain" is the universe domain.
2876 * We first make sure that the class domain is disjoint from
2877 * previously considered class domains.
2879 * The separate domains can be computed directly from the "class_domain".
2881 * The unroll, atomic and remainder domains need the constraints
2882 * from the schedule domain.
2884 * For unrolling, the actual schedule domain is needed (with divs that
2885 * may refer to the current dimension) so that stride detection can be
2886 * performed.
2888 * For atomic and remainder domains, inner dimensions and divs involving
2889 * the current dimensions should be eliminated.
2890 * In case we are working within a separation class, we need to intersect
2891 * the result with the current "class_domain" to ensure that the domains
2892 * are disjoint from those generated from other class domains.
2894 * The domain that has been made atomic may be larger than specified
2895 * by the user since it needs to be representable as a single basic set.
2896 * This possibly larger domain is removed from class_domain by
2897 * compute_atomic_domain. It is computed first so that the extended domain
2898 * would not overlap with any domains computed before.
2899 * Similary, the unrolled domains may have some constraints removed and
2900 * may therefore also be larger than specified by the user.
2902 * If anything is left after handling separate, unroll and atomic,
2903 * we split it up into basic sets and append the basic sets to domains->list.
2905 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2906 __isl_take isl_set *class_domain)
2908 isl_basic_set_list *list;
2909 isl_set *domain;
2911 class_domain = isl_set_subtract(class_domain,
2912 isl_set_copy(domains->done));
2913 domains->done = isl_set_union(domains->done,
2914 isl_set_copy(class_domain));
2916 class_domain = compute_atomic_domain(domains, class_domain);
2917 class_domain = compute_unroll_domains(domains, class_domain);
2919 domain = isl_set_copy(class_domain);
2921 if (compute_separate_domain(domains, domain) < 0)
2922 goto error;
2923 domain = isl_set_subtract(domain,
2924 isl_set_copy(domains->option[isl_ast_loop_separate]));
2926 domain = isl_set_intersect(domain,
2927 isl_set_copy(domains->schedule_domain));
2929 domain = isl_ast_build_eliminate(domains->build, domain);
2930 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2932 domain = isl_set_coalesce(domain);
2933 domain = isl_set_make_disjoint(domain);
2935 list = isl_basic_set_list_from_set(domain);
2936 domains->list = isl_basic_set_list_concat(domains->list, list);
2938 isl_set_free(class_domain);
2940 return isl_stat_ok;
2941 error:
2942 isl_set_free(domain);
2943 isl_set_free(class_domain);
2944 return isl_stat_error;
2947 /* Split up the domain at the current depth into disjoint
2948 * basic sets for which code should be generated separately
2949 * for the separation class identified by "pnt".
2951 * We extract the corresponding class domain from domains->sep_class,
2952 * eliminate inner dimensions and pass control to compute_partial_domains.
2954 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2956 struct isl_codegen_domains *domains = user;
2957 isl_set *class_set;
2958 isl_set *domain;
2959 int disjoint;
2961 class_set = isl_set_from_point(pnt);
2962 domain = isl_map_domain(isl_map_intersect_range(
2963 isl_map_copy(domains->sep_class), class_set));
2964 domain = isl_ast_build_compute_gist(domains->build, domain);
2965 domain = isl_ast_build_eliminate(domains->build, domain);
2967 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2968 if (disjoint < 0)
2969 return isl_stat_error;
2970 if (disjoint) {
2971 isl_set_free(domain);
2972 return isl_stat_ok;
2975 return compute_partial_domains(domains, domain);
2978 /* Extract the domains at the current depth that should be atomic,
2979 * separated or unrolled and store them in option.
2981 * The domains specified by the user might overlap, so we make
2982 * them disjoint by subtracting earlier domains from later domains.
2984 static void compute_domains_init_options(isl_set *option[4],
2985 __isl_keep isl_ast_build *build)
2987 enum isl_ast_loop_type type, type2;
2988 isl_set *unroll;
2990 for (type = isl_ast_loop_atomic;
2991 type <= isl_ast_loop_separate; ++type) {
2992 option[type] = isl_ast_build_get_option_domain(build, type);
2993 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
2994 option[type] = isl_set_subtract(option[type],
2995 isl_set_copy(option[type2]));
2998 unroll = option[isl_ast_loop_unroll];
2999 unroll = isl_set_coalesce(unroll);
3000 unroll = isl_set_make_disjoint(unroll);
3001 option[isl_ast_loop_unroll] = unroll;
3004 /* Split up the domain at the current depth into disjoint
3005 * basic sets for which code should be generated separately,
3006 * based on the user-specified options.
3007 * Return the list of disjoint basic sets.
3009 * There are three kinds of domains that we need to keep track of.
3010 * - the "schedule domain" is the domain of "executed"
3011 * - the "class domain" is the domain corresponding to the currrent
3012 * separation class
3013 * - the "option domain" is the domain corresponding to one of the options
3014 * atomic, unroll or separate
3016 * We first consider the individial values of the separation classes
3017 * and split up the domain for each of them separately.
3018 * Finally, we consider the remainder. If no separation classes were
3019 * specified, then we call compute_partial_domains with the universe
3020 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3021 * with inner dimensions removed. We do this because we want to
3022 * avoid computing the complement of the class domains (i.e., the difference
3023 * between the universe and domains->done).
3025 static __isl_give isl_basic_set_list *compute_domains(
3026 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3028 struct isl_codegen_domains domains;
3029 isl_ctx *ctx;
3030 isl_set *domain;
3031 isl_union_set *schedule_domain;
3032 isl_set *classes;
3033 isl_space *space;
3034 int n_param;
3035 enum isl_ast_loop_type type;
3036 int empty;
3038 if (!executed)
3039 return NULL;
3041 ctx = isl_union_map_get_ctx(executed);
3042 domains.list = isl_basic_set_list_alloc(ctx, 0);
3044 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3045 domain = isl_set_from_union_set(schedule_domain);
3047 compute_domains_init_options(domains.option, build);
3049 domains.sep_class = isl_ast_build_get_separation_class(build);
3050 classes = isl_map_range(isl_map_copy(domains.sep_class));
3051 n_param = isl_set_dim(classes, isl_dim_param);
3052 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3054 space = isl_set_get_space(domain);
3055 domains.build = build;
3056 domains.schedule_domain = isl_set_copy(domain);
3057 domains.executed = executed;
3058 domains.done = isl_set_empty(space);
3060 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3061 domains.list = isl_basic_set_list_free(domains.list);
3062 isl_set_free(classes);
3064 empty = isl_set_is_empty(domains.done);
3065 if (empty < 0) {
3066 domains.list = isl_basic_set_list_free(domains.list);
3067 domain = isl_set_free(domain);
3068 } else if (empty) {
3069 isl_set_free(domain);
3070 domain = isl_set_universe(isl_set_get_space(domains.done));
3071 } else {
3072 domain = isl_ast_build_eliminate(build, domain);
3074 if (compute_partial_domains(&domains, domain) < 0)
3075 domains.list = isl_basic_set_list_free(domains.list);
3077 isl_set_free(domains.schedule_domain);
3078 isl_set_free(domains.done);
3079 isl_map_free(domains.sep_class);
3080 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3081 isl_set_free(domains.option[type]);
3083 return domains.list;
3086 /* Generate code for a single component, after shifting (if any)
3087 * has been applied, in case the schedule was specified as a union map.
3089 * We first split up the domain at the current depth into disjoint
3090 * basic sets based on the user-specified options.
3091 * Then we generated code for each of them and concatenate the results.
3093 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3094 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3096 isl_basic_set_list *domain_list;
3097 isl_ast_graft_list *list = NULL;
3099 domain_list = compute_domains(executed, build);
3100 list = generate_parallel_domains(domain_list, executed, build);
3102 isl_basic_set_list_free(domain_list);
3103 isl_union_map_free(executed);
3104 isl_ast_build_free(build);
3106 return list;
3109 /* Generate code for a single component, after shifting (if any)
3110 * has been applied, in case the schedule was specified as a schedule tree
3111 * and the separate option was specified.
3113 * We perform separation on the domain of "executed" and then generate
3114 * an AST for each of the resulting disjoint basic sets.
3116 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3117 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3119 isl_space *space;
3120 isl_set *domain;
3121 isl_basic_set_list *domain_list;
3122 isl_ast_graft_list *list;
3124 space = isl_ast_build_get_space(build, 1);
3125 domain = separate_schedule_domains(space,
3126 isl_union_map_copy(executed), build);
3127 domain_list = isl_basic_set_list_from_set(domain);
3129 list = generate_parallel_domains(domain_list, executed, build);
3131 isl_basic_set_list_free(domain_list);
3132 isl_union_map_free(executed);
3133 isl_ast_build_free(build);
3135 return list;
3138 /* Internal data structure for generate_shifted_component_tree_unroll.
3140 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3141 * "list" collects the constructs grafts.
3143 struct isl_ast_unroll_tree_data {
3144 isl_union_map *executed;
3145 isl_ast_build *build;
3146 isl_ast_graft_list *list;
3149 /* Initialize data->list to a list of "n" elements.
3151 static int init_unroll_tree(int n, void *user)
3153 struct isl_ast_unroll_tree_data *data = user;
3154 isl_ctx *ctx;
3156 ctx = isl_ast_build_get_ctx(data->build);
3157 data->list = isl_ast_graft_list_alloc(ctx, n);
3159 return 0;
3162 /* Given an iteration of an unrolled domain represented by "bset",
3163 * generate the corresponding AST and add the result to data->list.
3165 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3167 struct isl_ast_unroll_tree_data *data = user;
3169 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3170 bset, isl_ast_build_copy(data->build));
3172 return 0;
3175 /* Generate code for a single component, after shifting (if any)
3176 * has been applied, in case the schedule was specified as a schedule tree
3177 * and the unroll option was specified.
3179 * We call foreach_iteration to iterate over the individual values and
3180 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3182 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3183 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3184 __isl_take isl_ast_build *build)
3186 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3188 if (foreach_iteration(domain, build, &init_unroll_tree,
3189 &do_unroll_tree_iteration, &data) < 0)
3190 data.list = isl_ast_graft_list_free(data.list);
3192 isl_union_map_free(executed);
3193 isl_ast_build_free(build);
3195 return data.list;
3198 /* Generate code for a single component, after shifting (if any)
3199 * has been applied, in case the schedule was specified as a schedule tree.
3200 * In particular, handle the base case where there is either no isolated
3201 * set or we are within the isolated set (in which case "isolated" is set)
3202 * or the iterations that precede or follow the isolated set.
3204 * The schedule domain is broken up or combined into basic sets
3205 * according to the AST generation option specified in the current
3206 * schedule node, which may be either atomic, separate, unroll or
3207 * unspecified. If the option is unspecified, then we currently simply
3208 * split the schedule domain into disjoint basic sets.
3210 * In case the separate option is specified, the AST generation is
3211 * handled by generate_shifted_component_tree_separate.
3212 * In the other cases, we need the global schedule domain.
3213 * In the unroll case, the AST generation is then handled by
3214 * generate_shifted_component_tree_unroll which needs the actual
3215 * schedule domain (with divs that may refer to the current dimension)
3216 * so that stride detection can be performed.
3217 * In the atomic or unspecified case, inner dimensions and divs involving
3218 * the current dimensions should be eliminated.
3219 * The result is then either combined into a single basic set or
3220 * split up into disjoint basic sets.
3221 * Finally an AST is generated for each basic set and the results are
3222 * concatenated.
3224 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3225 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3226 int isolated)
3228 isl_union_set *schedule_domain;
3229 isl_set *domain;
3230 isl_basic_set_list *domain_list;
3231 isl_ast_graft_list *list;
3232 enum isl_ast_loop_type type;
3234 type = isl_ast_build_get_loop_type(build, isolated);
3235 if (type < 0)
3236 goto error;
3238 if (type == isl_ast_loop_separate)
3239 return generate_shifted_component_tree_separate(executed,
3240 build);
3242 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3243 domain = isl_set_from_union_set(schedule_domain);
3245 if (type == isl_ast_loop_unroll)
3246 return generate_shifted_component_tree_unroll(executed, domain,
3247 build);
3249 domain = isl_ast_build_eliminate(build, domain);
3250 domain = isl_set_coalesce(domain);
3252 if (type == isl_ast_loop_atomic) {
3253 isl_basic_set *hull;
3254 hull = isl_set_unshifted_simple_hull(domain);
3255 domain_list = isl_basic_set_list_from_basic_set(hull);
3256 } else {
3257 domain = isl_set_make_disjoint(domain);
3258 domain_list = isl_basic_set_list_from_set(domain);
3261 list = generate_parallel_domains(domain_list, executed, build);
3263 isl_basic_set_list_free(domain_list);
3264 isl_union_map_free(executed);
3265 isl_ast_build_free(build);
3267 return list;
3268 error:
3269 isl_union_map_free(executed);
3270 isl_ast_build_free(build);
3271 return NULL;
3274 /* Generate code for a single component, after shifting (if any)
3275 * has been applied, in case the schedule was specified as a schedule tree.
3276 * In particular, do so for the specified subset of the schedule domain.
3278 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3279 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3280 __isl_keep isl_ast_build *build, int isolated)
3282 isl_union_set *uset;
3283 int empty;
3285 uset = isl_union_set_from_set(domain);
3286 executed = isl_union_map_copy(executed);
3287 executed = isl_union_map_intersect_domain(executed, uset);
3288 empty = isl_union_map_is_empty(executed);
3289 if (empty < 0)
3290 goto error;
3291 if (empty) {
3292 isl_ctx *ctx;
3293 isl_union_map_free(executed);
3294 ctx = isl_ast_build_get_ctx(build);
3295 return isl_ast_graft_list_alloc(ctx, 0);
3298 build = isl_ast_build_copy(build);
3299 return generate_shifted_component_tree_base(executed, build, isolated);
3300 error:
3301 isl_union_map_free(executed);
3302 return NULL;
3305 /* Generate code for a single component, after shifting (if any)
3306 * has been applied, in case the schedule was specified as a schedule tree.
3308 * We first check if the user has specified an isolated schedule domain
3309 * and that we are not already outside of this isolated schedule domain.
3310 * If so, we break up the schedule domain into iterations that
3311 * precede the isolated domain, the isolated domain itself,
3312 * the iterations that follow the isolated domain and
3313 * the remaining iterations (those that are incomparable
3314 * to the isolated domain).
3315 * We generate an AST for each piece and concatenate the results.
3316 * If no isolated set has been specified, then we generate an
3317 * AST for the entire inverse schedule.
3319 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3320 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3322 int i, depth;
3323 int empty, has_isolate;
3324 isl_space *space;
3325 isl_union_set *schedule_domain;
3326 isl_set *domain;
3327 isl_basic_set *hull;
3328 isl_set *isolated, *before, *after, *test;
3329 isl_map *gt, *lt;
3330 isl_ast_graft_list *list, *res;
3332 build = isl_ast_build_extract_isolated(build);
3333 has_isolate = isl_ast_build_has_isolated(build);
3334 if (has_isolate < 0)
3335 executed = isl_union_map_free(executed);
3336 else if (!has_isolate)
3337 return generate_shifted_component_tree_base(executed, build, 0);
3339 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3340 domain = isl_set_from_union_set(schedule_domain);
3342 isolated = isl_ast_build_get_isolated(build);
3343 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3344 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3345 empty = isl_set_is_empty(test);
3346 isl_set_free(test);
3347 if (empty < 0)
3348 goto error;
3349 if (empty) {
3350 isl_set_free(isolated);
3351 isl_set_free(domain);
3352 return generate_shifted_component_tree_base(executed, build, 0);
3354 isolated = isl_ast_build_eliminate(build, isolated);
3355 hull = isl_set_unshifted_simple_hull(isolated);
3356 isolated = isl_set_from_basic_set(hull);
3358 depth = isl_ast_build_get_depth(build);
3359 space = isl_space_map_from_set(isl_set_get_space(isolated));
3360 gt = isl_map_universe(space);
3361 for (i = 0; i < depth; ++i)
3362 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3363 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3364 lt = isl_map_reverse(isl_map_copy(gt));
3365 before = isl_set_apply(isl_set_copy(isolated), gt);
3366 after = isl_set_apply(isl_set_copy(isolated), lt);
3368 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3369 domain = isl_set_subtract(domain, isl_set_copy(before));
3370 domain = isl_set_subtract(domain, isl_set_copy(after));
3371 after = isl_set_subtract(after, isl_set_copy(isolated));
3372 after = isl_set_subtract(after, isl_set_copy(before));
3373 before = isl_set_subtract(before, isl_set_copy(isolated));
3375 res = generate_shifted_component_tree_part(executed, before, build, 0);
3376 list = generate_shifted_component_tree_part(executed, isolated,
3377 build, 1);
3378 res = isl_ast_graft_list_concat(res, list);
3379 list = generate_shifted_component_tree_part(executed, after, build, 0);
3380 res = isl_ast_graft_list_concat(res, list);
3381 list = generate_shifted_component_tree_part(executed, domain, build, 0);
3382 res = isl_ast_graft_list_concat(res, list);
3384 isl_union_map_free(executed);
3385 isl_ast_build_free(build);
3387 return res;
3388 error:
3389 isl_set_free(domain);
3390 isl_set_free(isolated);
3391 isl_union_map_free(executed);
3392 isl_ast_build_free(build);
3393 return NULL;
3396 /* Generate code for a single component, after shifting (if any)
3397 * has been applied.
3399 * Call generate_shifted_component_tree or generate_shifted_component_flat
3400 * depending on whether the schedule was specified as a schedule tree.
3402 static __isl_give isl_ast_graft_list *generate_shifted_component(
3403 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3405 if (isl_ast_build_has_schedule_node(build))
3406 return generate_shifted_component_tree(executed, build);
3407 else
3408 return generate_shifted_component_flat(executed, build);
3411 struct isl_set_map_pair {
3412 isl_set *set;
3413 isl_map *map;
3416 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3417 * of indices into the "domain" array,
3418 * return the union of the "map" fields of the elements
3419 * indexed by the first "n" elements of "order".
3421 static __isl_give isl_union_map *construct_component_executed(
3422 struct isl_set_map_pair *domain, int *order, int n)
3424 int i;
3425 isl_map *map;
3426 isl_union_map *executed;
3428 map = isl_map_copy(domain[order[0]].map);
3429 executed = isl_union_map_from_map(map);
3430 for (i = 1; i < n; ++i) {
3431 map = isl_map_copy(domain[order[i]].map);
3432 executed = isl_union_map_add_map(executed, map);
3435 return executed;
3438 /* Generate code for a single component, after shifting (if any)
3439 * has been applied.
3441 * The component inverse schedule is specified as the "map" fields
3442 * of the elements of "domain" indexed by the first "n" elements of "order".
3444 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3445 struct isl_set_map_pair *domain, int *order, int n,
3446 __isl_take isl_ast_build *build)
3448 isl_union_map *executed;
3450 executed = construct_component_executed(domain, order, n);
3451 return generate_shifted_component(executed, build);
3454 /* Does set dimension "pos" of "set" have an obviously fixed value?
3456 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3458 int fixed;
3459 isl_val *v;
3461 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3462 if (!v)
3463 return -1;
3464 fixed = !isl_val_is_nan(v);
3465 isl_val_free(v);
3467 return fixed;
3470 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3471 * of indices into the "domain" array,
3472 * do all (except for at most one) of the "set" field of the elements
3473 * indexed by the first "n" elements of "order" have a fixed value
3474 * at position "depth"?
3476 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3477 int *order, int n, int depth)
3479 int i;
3480 int non_fixed = -1;
3482 for (i = 0; i < n; ++i) {
3483 int f;
3485 f = dim_is_fixed(domain[order[i]].set, depth);
3486 if (f < 0)
3487 return -1;
3488 if (f)
3489 continue;
3490 if (non_fixed >= 0)
3491 return 0;
3492 non_fixed = i;
3495 return 1;
3498 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3499 * of indices into the "domain" array,
3500 * eliminate the inner dimensions from the "set" field of the elements
3501 * indexed by the first "n" elements of "order", provided the current
3502 * dimension does not have a fixed value.
3504 * Return the index of the first element in "order" with a corresponding
3505 * "set" field that does not have an (obviously) fixed value.
3507 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3508 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3510 int i;
3511 int base = -1;
3513 for (i = n - 1; i >= 0; --i) {
3514 int f;
3515 f = dim_is_fixed(domain[order[i]].set, depth);
3516 if (f < 0)
3517 return -1;
3518 if (f)
3519 continue;
3520 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3521 domain[order[i]].set);
3522 base = i;
3525 return base;
3528 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3529 * of indices into the "domain" array,
3530 * find the element of "domain" (amongst those indexed by the first "n"
3531 * elements of "order") with the "set" field that has the smallest
3532 * value for the current iterator.
3534 * Note that the domain with the smallest value may depend on the parameters
3535 * and/or outer loop dimension. Since the result of this function is only
3536 * used as heuristic, we only make a reasonable attempt at finding the best
3537 * domain, one that should work in case a single domain provides the smallest
3538 * value for the current dimension over all values of the parameters
3539 * and outer dimensions.
3541 * In particular, we compute the smallest value of the first domain
3542 * and replace it by that of any later domain if that later domain
3543 * has a smallest value that is smaller for at least some value
3544 * of the parameters and outer dimensions.
3546 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3547 __isl_keep isl_ast_build *build)
3549 int i;
3550 isl_map *min_first;
3551 int first = 0;
3553 min_first = isl_ast_build_map_to_iterator(build,
3554 isl_set_copy(domain[order[0]].set));
3555 min_first = isl_map_lexmin(min_first);
3557 for (i = 1; i < n; ++i) {
3558 isl_map *min, *test;
3559 int empty;
3561 min = isl_ast_build_map_to_iterator(build,
3562 isl_set_copy(domain[order[i]].set));
3563 min = isl_map_lexmin(min);
3564 test = isl_map_copy(min);
3565 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3566 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3567 empty = isl_map_is_empty(test);
3568 isl_map_free(test);
3569 if (empty >= 0 && !empty) {
3570 isl_map_free(min_first);
3571 first = i;
3572 min_first = min;
3573 } else
3574 isl_map_free(min);
3576 if (empty < 0)
3577 break;
3580 isl_map_free(min_first);
3582 return i < n ? -1 : first;
3585 /* Construct a shifted inverse schedule based on the original inverse schedule,
3586 * the stride and the offset.
3588 * The original inverse schedule is specified as the "map" fields
3589 * of the elements of "domain" indexed by the first "n" elements of "order".
3591 * "stride" and "offset" are such that the difference
3592 * between the values of the current dimension of domain "i"
3593 * and the values of the current dimension for some reference domain are
3594 * equal to
3596 * stride * integer + offset[i]
3598 * Moreover, 0 <= offset[i] < stride.
3600 * For each domain, we create a map
3602 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3604 * where j refers to the current dimension and the other dimensions are
3605 * unchanged, and apply this map to the original schedule domain.
3607 * For example, for the original schedule
3609 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3611 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3612 * we apply the mapping
3614 * { [j] -> [j, 0] }
3616 * to the schedule of the "A" domain and the mapping
3618 * { [j - 1] -> [j, 1] }
3620 * to the schedule of the "B" domain.
3623 * Note that after the transformation, the differences between pairs
3624 * of values of the current dimension over all domains are multiples
3625 * of stride and that we have therefore exposed the stride.
3628 * To see that the mapping preserves the lexicographic order,
3629 * first note that each of the individual maps above preserves the order.
3630 * If the value of the current iterator is j1 in one domain and j2 in another,
3631 * then if j1 = j2, we know that the same map is applied to both domains
3632 * and the order is preserved.
3633 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3634 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3636 * j1 - c1 < j2 - c2
3638 * and the order is preserved.
3639 * If c1 < c2, then we know
3641 * 0 <= c2 - c1 < s
3643 * We also have
3645 * j2 - j1 = n * s + r
3647 * with n >= 0 and 0 <= r < s.
3648 * In other words, r = c2 - c1.
3649 * If n > 0, then
3651 * j1 - c1 < j2 - c2
3653 * If n = 0, then
3655 * j1 - c1 = j2 - c2
3657 * and so
3659 * (j1 - c1, c1) << (j2 - c2, c2)
3661 * with "<<" the lexicographic order, proving that the order is preserved
3662 * in all cases.
3664 static __isl_give isl_union_map *contruct_shifted_executed(
3665 struct isl_set_map_pair *domain, int *order, int n,
3666 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3667 __isl_take isl_ast_build *build)
3669 int i;
3670 isl_union_map *executed;
3671 isl_space *space;
3672 isl_map *map;
3673 int depth;
3674 isl_constraint *c;
3676 depth = isl_ast_build_get_depth(build);
3677 space = isl_ast_build_get_space(build, 1);
3678 executed = isl_union_map_empty(isl_space_copy(space));
3679 space = isl_space_map_from_set(space);
3680 map = isl_map_identity(isl_space_copy(space));
3681 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3682 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3683 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3685 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3686 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3687 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3689 for (i = 0; i < n; ++i) {
3690 isl_map *map_i;
3691 isl_val *v;
3693 v = isl_multi_val_get_val(offset, i);
3694 if (!v)
3695 break;
3696 map_i = isl_map_copy(map);
3697 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3698 isl_val_copy(v));
3699 v = isl_val_neg(v);
3700 c = isl_constraint_set_constant_val(c, v);
3701 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3703 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3704 map_i);
3705 executed = isl_union_map_add_map(executed, map_i);
3708 isl_constraint_free(c);
3709 isl_map_free(map);
3711 if (i < n)
3712 executed = isl_union_map_free(executed);
3714 return executed;
3717 /* Generate code for a single component, after exposing the stride,
3718 * given that the schedule domain is "shifted strided".
3720 * The component inverse schedule is specified as the "map" fields
3721 * of the elements of "domain" indexed by the first "n" elements of "order".
3723 * The schedule domain being "shifted strided" means that the differences
3724 * between the values of the current dimension of domain "i"
3725 * and the values of the current dimension for some reference domain are
3726 * equal to
3728 * stride * integer + offset[i]
3730 * We first look for the domain with the "smallest" value for the current
3731 * dimension and adjust the offsets such that the offset of the "smallest"
3732 * domain is equal to zero. The other offsets are reduced modulo stride.
3734 * Based on this information, we construct a new inverse schedule in
3735 * contruct_shifted_executed that exposes the stride.
3736 * Since this involves the introduction of a new schedule dimension,
3737 * the build needs to be changed accodingly.
3738 * After computing the AST, the newly introduced dimension needs
3739 * to be removed again from the list of grafts. We do this by plugging
3740 * in a mapping that represents the new schedule domain in terms of the
3741 * old schedule domain.
3743 static __isl_give isl_ast_graft_list *generate_shift_component(
3744 struct isl_set_map_pair *domain, int *order, int n,
3745 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3746 __isl_take isl_ast_build *build)
3748 isl_ast_graft_list *list;
3749 int first;
3750 int depth;
3751 isl_val *val;
3752 isl_multi_val *mv;
3753 isl_space *space;
3754 isl_multi_aff *ma, *zero;
3755 isl_union_map *executed;
3757 depth = isl_ast_build_get_depth(build);
3759 first = first_offset(domain, order, n, build);
3760 if (first < 0)
3761 goto error;
3763 mv = isl_multi_val_copy(offset);
3764 val = isl_multi_val_get_val(offset, first);
3765 val = isl_val_neg(val);
3766 mv = isl_multi_val_add_val(mv, val);
3767 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3769 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3770 build);
3771 space = isl_ast_build_get_space(build, 1);
3772 space = isl_space_map_from_set(space);
3773 ma = isl_multi_aff_identity(isl_space_copy(space));
3774 space = isl_space_from_domain(isl_space_domain(space));
3775 space = isl_space_add_dims(space, isl_dim_out, 1);
3776 zero = isl_multi_aff_zero(space);
3777 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3778 build = isl_ast_build_insert_dim(build, depth + 1);
3779 list = generate_shifted_component(executed, build);
3781 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3783 isl_multi_val_free(mv);
3785 return list;
3786 error:
3787 isl_ast_build_free(build);
3788 return NULL;
3791 /* Does any node in the schedule tree rooted at the current schedule node
3792 * of "build" depend on outer schedule nodes?
3794 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
3796 isl_schedule_node *node;
3797 int dependent = 0;
3799 node = isl_ast_build_get_schedule_node(build);
3800 dependent = isl_schedule_node_is_subtree_anchored(node);
3801 isl_schedule_node_free(node);
3803 return dependent;
3806 /* Generate code for a single component.
3808 * The component inverse schedule is specified as the "map" fields
3809 * of the elements of "domain" indexed by the first "n" elements of "order".
3811 * This function may modify the "set" fields of "domain".
3813 * Before proceeding with the actual code generation for the component,
3814 * we first check if there are any "shifted" strides, meaning that
3815 * the schedule domains of the individual domains are all strided,
3816 * but that they have different offsets, resulting in the union
3817 * of schedule domains not being strided anymore.
3819 * The simplest example is the schedule
3821 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3823 * Both schedule domains are strided, but their union is not.
3824 * This function detects such cases and then rewrites the schedule to
3826 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3828 * In the new schedule, the schedule domains have the same offset (modulo
3829 * the stride), ensuring that the union of schedule domains is also strided.
3832 * If there is only a single domain in the component, then there is
3833 * nothing to do. Similarly, if the current schedule dimension has
3834 * a fixed value for almost all domains then there is nothing to be done.
3835 * In particular, we need at least two domains where the current schedule
3836 * dimension does not have a fixed value.
3837 * Finally, in case of a schedule map input,
3838 * if any of the options refer to the current schedule dimension,
3839 * then we bail out as well. It would be possible to reformulate the options
3840 * in terms of the new schedule domain, but that would introduce constraints
3841 * that separate the domains in the options and that is something we would
3842 * like to avoid.
3843 * In the case of a schedule tree input, we bail out if any of
3844 * the descendants of the current schedule node refer to outer
3845 * schedule nodes in any way.
3848 * To see if there is any shifted stride, we look at the differences
3849 * between the values of the current dimension in pairs of domains
3850 * for equal values of outer dimensions. These differences should be
3851 * of the form
3853 * m x + r
3855 * with "m" the stride and "r" a constant. Note that we cannot perform
3856 * this analysis on individual domains as the lower bound in each domain
3857 * may depend on parameters or outer dimensions and so the current dimension
3858 * itself may not have a fixed remainder on division by the stride.
3860 * In particular, we compare the first domain that does not have an
3861 * obviously fixed value for the current dimension to itself and all
3862 * other domains and collect the offsets and the gcd of the strides.
3863 * If the gcd becomes one, then we failed to find shifted strides.
3864 * If the gcd is zero, then the differences were all fixed, meaning
3865 * that some domains had non-obviously fixed values for the current dimension.
3866 * If all the offsets are the same (for those domains that do not have
3867 * an obviously fixed value for the current dimension), then we do not
3868 * apply the transformation.
3869 * If none of the domains were skipped, then there is nothing to do.
3870 * If some of them were skipped, then if we apply separation, the schedule
3871 * domain should get split in pieces with a (non-shifted) stride.
3873 * Otherwise, we apply a shift to expose the stride in
3874 * generate_shift_component.
3876 static __isl_give isl_ast_graft_list *generate_component(
3877 struct isl_set_map_pair *domain, int *order, int n,
3878 __isl_take isl_ast_build *build)
3880 int i, d;
3881 int depth;
3882 isl_ctx *ctx;
3883 isl_map *map;
3884 isl_set *deltas;
3885 isl_val *gcd = NULL;
3886 isl_multi_val *mv;
3887 int fixed, skip;
3888 int base;
3889 isl_ast_graft_list *list;
3890 int res = 0;
3892 depth = isl_ast_build_get_depth(build);
3894 skip = n == 1;
3895 if (skip >= 0 && !skip)
3896 skip = at_most_one_non_fixed(domain, order, n, depth);
3897 if (skip >= 0 && !skip) {
3898 if (isl_ast_build_has_schedule_node(build))
3899 skip = has_anchored_subtree(build);
3900 else
3901 skip = isl_ast_build_options_involve_depth(build);
3903 if (skip < 0)
3904 goto error;
3905 if (skip)
3906 return generate_shifted_component_from_list(domain,
3907 order, n, build);
3909 base = eliminate_non_fixed(domain, order, n, depth, build);
3910 if (base < 0)
3911 goto error;
3913 ctx = isl_ast_build_get_ctx(build);
3915 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3917 fixed = 1;
3918 for (i = 0; i < n; ++i) {
3919 isl_val *r, *m;
3921 map = isl_map_from_domain_and_range(
3922 isl_set_copy(domain[order[base]].set),
3923 isl_set_copy(domain[order[i]].set));
3924 for (d = 0; d < depth; ++d)
3925 map = isl_map_equate(map, isl_dim_in, d,
3926 isl_dim_out, d);
3927 deltas = isl_map_deltas(map);
3928 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3929 isl_set_free(deltas);
3930 if (res < 0)
3931 break;
3933 if (i == 0)
3934 gcd = m;
3935 else
3936 gcd = isl_val_gcd(gcd, m);
3937 if (isl_val_is_one(gcd)) {
3938 isl_val_free(r);
3939 break;
3941 mv = isl_multi_val_set_val(mv, i, r);
3943 res = dim_is_fixed(domain[order[i]].set, depth);
3944 if (res < 0)
3945 break;
3946 if (res)
3947 continue;
3949 if (fixed && i > base) {
3950 isl_val *a, *b;
3951 a = isl_multi_val_get_val(mv, i);
3952 b = isl_multi_val_get_val(mv, base);
3953 if (isl_val_ne(a, b))
3954 fixed = 0;
3955 isl_val_free(a);
3956 isl_val_free(b);
3960 if (res < 0 || !gcd) {
3961 isl_ast_build_free(build);
3962 list = NULL;
3963 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3964 list = generate_shifted_component_from_list(domain,
3965 order, n, build);
3966 } else {
3967 list = generate_shift_component(domain, order, n, gcd, mv,
3968 build);
3971 isl_val_free(gcd);
3972 isl_multi_val_free(mv);
3974 return list;
3975 error:
3976 isl_ast_build_free(build);
3977 return NULL;
3980 /* Store both "map" itself and its domain in the
3981 * structure pointed to by *next and advance to the next array element.
3983 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
3985 struct isl_set_map_pair **next = user;
3987 (*next)->map = isl_map_copy(map);
3988 (*next)->set = isl_map_domain(map);
3989 (*next)++;
3991 return isl_stat_ok;
3994 static int after_in_tree(__isl_keep isl_union_map *umap,
3995 __isl_keep isl_schedule_node *node);
3997 /* Is any domain element of "umap" scheduled after any of
3998 * the corresponding image elements by the tree rooted at
3999 * the child of "node"?
4001 static int after_in_child(__isl_keep isl_union_map *umap,
4002 __isl_keep isl_schedule_node *node)
4004 isl_schedule_node *child;
4005 int after;
4007 child = isl_schedule_node_get_child(node, 0);
4008 after = after_in_tree(umap, child);
4009 isl_schedule_node_free(child);
4011 return after;
4014 /* Is any domain element of "umap" scheduled after any of
4015 * the corresponding image elements by the tree rooted at
4016 * the band node "node"?
4018 * We first check if any domain element is scheduled after any
4019 * of the corresponding image elements by the band node itself.
4020 * If not, we restrict "map" to those pairs of element that
4021 * are scheduled together by the band node and continue with
4022 * the child of the band node.
4023 * If there are no such pairs then the map passed to after_in_child
4024 * will be empty causing it to return 0.
4026 static int after_in_band(__isl_keep isl_union_map *umap,
4027 __isl_keep isl_schedule_node *node)
4029 isl_multi_union_pw_aff *mupa;
4030 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4031 isl_union_set *domain, *range;
4032 isl_space *space;
4033 int empty;
4034 int after;
4036 if (isl_schedule_node_band_n_member(node) == 0)
4037 return after_in_child(umap, node);
4039 mupa = isl_schedule_node_band_get_partial_schedule(node);
4040 space = isl_multi_union_pw_aff_get_space(mupa);
4041 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4042 test = isl_union_map_copy(umap);
4043 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4044 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4045 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4046 test = isl_union_map_intersect(test, gt);
4047 empty = isl_union_map_is_empty(test);
4048 isl_union_map_free(test);
4050 if (empty < 0 || !empty) {
4051 isl_union_map_free(partial);
4052 return empty < 0 ? -1 : 1;
4055 universe = isl_union_map_universe(isl_union_map_copy(umap));
4056 domain = isl_union_map_domain(isl_union_map_copy(universe));
4057 range = isl_union_map_range(universe);
4058 umap1 = isl_union_map_copy(partial);
4059 umap1 = isl_union_map_intersect_domain(umap1, domain);
4060 umap2 = isl_union_map_intersect_domain(partial, range);
4061 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4062 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4063 after = after_in_child(test, node);
4064 isl_union_map_free(test);
4065 return after;
4068 /* Is any domain element of "umap" scheduled after any of
4069 * the corresponding image elements by the tree rooted at
4070 * the context node "node"?
4072 * The context constraints apply to the schedule domain,
4073 * so we cannot apply them directly to "umap", which contains
4074 * pairs of statement instances. Instead, we add them
4075 * to the range of the prefix schedule for both domain and
4076 * range of "umap".
4078 static int after_in_context(__isl_keep isl_union_map *umap,
4079 __isl_keep isl_schedule_node *node)
4081 isl_union_map *prefix, *universe, *umap1, *umap2;
4082 isl_union_set *domain, *range;
4083 isl_set *context;
4084 int after;
4086 umap = isl_union_map_copy(umap);
4087 context = isl_schedule_node_context_get_context(node);
4088 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4089 universe = isl_union_map_universe(isl_union_map_copy(umap));
4090 domain = isl_union_map_domain(isl_union_map_copy(universe));
4091 range = isl_union_map_range(universe);
4092 umap1 = isl_union_map_copy(prefix);
4093 umap1 = isl_union_map_intersect_domain(umap1, domain);
4094 umap2 = isl_union_map_intersect_domain(prefix, range);
4095 umap1 = isl_union_map_intersect_range(umap1,
4096 isl_union_set_from_set(context));
4097 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4098 umap = isl_union_map_intersect(umap, umap1);
4100 after = after_in_child(umap, node);
4102 isl_union_map_free(umap);
4104 return after;
4107 /* Is any domain element of "umap" scheduled after any of
4108 * the corresponding image elements by the tree rooted at
4109 * the expansion node "node"?
4111 * We apply the expansion to domain and range of "umap" and
4112 * continue with its child.
4114 static int after_in_expansion(__isl_keep isl_union_map *umap,
4115 __isl_keep isl_schedule_node *node)
4117 isl_union_map *expansion;
4118 int after;
4120 expansion = isl_schedule_node_expansion_get_expansion(node);
4121 umap = isl_union_map_copy(umap);
4122 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4123 umap = isl_union_map_apply_range(umap, expansion);
4125 after = after_in_child(umap, node);
4127 isl_union_map_free(umap);
4129 return after;
4132 /* Is any domain element of "umap" scheduled after any of
4133 * the corresponding image elements by the tree rooted at
4134 * the extension node "node"?
4136 * Since the extension node may add statement instances before or
4137 * after the pairs of statement instances in "umap", we return 1
4138 * to ensure that these pairs are not broken up.
4140 static int after_in_extension(__isl_keep isl_union_map *umap,
4141 __isl_keep isl_schedule_node *node)
4143 return 1;
4146 /* Is any domain element of "umap" scheduled after any of
4147 * the corresponding image elements by the tree rooted at
4148 * the filter node "node"?
4150 * We intersect domain and range of "umap" with the filter and
4151 * continue with its child.
4153 static int after_in_filter(__isl_keep isl_union_map *umap,
4154 __isl_keep isl_schedule_node *node)
4156 isl_union_set *filter;
4157 int after;
4159 umap = isl_union_map_copy(umap);
4160 filter = isl_schedule_node_filter_get_filter(node);
4161 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4162 umap = isl_union_map_intersect_range(umap, filter);
4164 after = after_in_child(umap, node);
4166 isl_union_map_free(umap);
4168 return after;
4171 /* Is any domain element of "umap" scheduled after any of
4172 * the corresponding image elements by the tree rooted at
4173 * the set node "node"?
4175 * This is only the case if this condition holds in any
4176 * of the (filter) children of the set node.
4177 * In particular, if the domain and the range of "umap"
4178 * are contained in different children, then the condition
4179 * does not hold.
4181 static int after_in_set(__isl_keep isl_union_map *umap,
4182 __isl_keep isl_schedule_node *node)
4184 int i, n;
4186 n = isl_schedule_node_n_children(node);
4187 for (i = 0; i < n; ++i) {
4188 isl_schedule_node *child;
4189 int after;
4191 child = isl_schedule_node_get_child(node, i);
4192 after = after_in_tree(umap, child);
4193 isl_schedule_node_free(child);
4195 if (after < 0 || after)
4196 return after;
4199 return 0;
4202 /* Return the filter of child "i" of "node".
4204 static __isl_give isl_union_set *child_filter(
4205 __isl_keep isl_schedule_node *node, int i)
4207 isl_schedule_node *child;
4208 isl_union_set *filter;
4210 child = isl_schedule_node_get_child(node, i);
4211 filter = isl_schedule_node_filter_get_filter(child);
4212 isl_schedule_node_free(child);
4214 return filter;
4217 /* Is any domain element of "umap" scheduled after any of
4218 * the corresponding image elements by the tree rooted at
4219 * the sequence node "node"?
4221 * This happens in particular if any domain element is
4222 * contained in a later child than one containing a range element or
4223 * if the condition holds within a given child in the sequence.
4224 * The later part of the condition is checked by after_in_set.
4226 static int after_in_sequence(__isl_keep isl_union_map *umap,
4227 __isl_keep isl_schedule_node *node)
4229 int i, j, n;
4230 isl_union_map *umap_i;
4231 int empty, after = 0;
4233 n = isl_schedule_node_n_children(node);
4234 for (i = 1; i < n; ++i) {
4235 isl_union_set *filter_i;
4237 umap_i = isl_union_map_copy(umap);
4238 filter_i = child_filter(node, i);
4239 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4240 empty = isl_union_map_is_empty(umap_i);
4241 if (empty < 0)
4242 goto error;
4243 if (empty) {
4244 isl_union_map_free(umap_i);
4245 continue;
4248 for (j = 0; j < i; ++j) {
4249 isl_union_set *filter_j;
4250 isl_union_map *umap_ij;
4252 umap_ij = isl_union_map_copy(umap_i);
4253 filter_j = child_filter(node, j);
4254 umap_ij = isl_union_map_intersect_range(umap_ij,
4255 filter_j);
4256 empty = isl_union_map_is_empty(umap_ij);
4257 isl_union_map_free(umap_ij);
4259 if (empty < 0)
4260 goto error;
4261 if (!empty)
4262 after = 1;
4263 if (after)
4264 break;
4267 isl_union_map_free(umap_i);
4268 if (after)
4269 break;
4272 if (after < 0 || after)
4273 return after;
4275 return after_in_set(umap, node);
4276 error:
4277 isl_union_map_free(umap_i);
4278 return -1;
4281 /* Is any domain element of "umap" scheduled after any of
4282 * the corresponding image elements by the tree rooted at "node"?
4284 * If "umap" is empty, then clearly there is no such element.
4285 * Otherwise, consider the different types of nodes separately.
4287 static int after_in_tree(__isl_keep isl_union_map *umap,
4288 __isl_keep isl_schedule_node *node)
4290 int empty;
4291 enum isl_schedule_node_type type;
4293 empty = isl_union_map_is_empty(umap);
4294 if (empty < 0)
4295 return -1;
4296 if (empty)
4297 return 0;
4298 if (!node)
4299 return -1;
4301 type = isl_schedule_node_get_type(node);
4302 switch (type) {
4303 case isl_schedule_node_error:
4304 return -1;
4305 case isl_schedule_node_leaf:
4306 return 0;
4307 case isl_schedule_node_band:
4308 return after_in_band(umap, node);
4309 case isl_schedule_node_domain:
4310 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4311 "unexpected internal domain node", return -1);
4312 case isl_schedule_node_context:
4313 return after_in_context(umap, node);
4314 case isl_schedule_node_expansion:
4315 return after_in_expansion(umap, node);
4316 case isl_schedule_node_extension:
4317 return after_in_extension(umap, node);
4318 case isl_schedule_node_filter:
4319 return after_in_filter(umap, node);
4320 case isl_schedule_node_guard:
4321 case isl_schedule_node_mark:
4322 return after_in_child(umap, node);
4323 case isl_schedule_node_set:
4324 return after_in_set(umap, node);
4325 case isl_schedule_node_sequence:
4326 return after_in_sequence(umap, node);
4329 return 1;
4332 /* Is any domain element of "map1" scheduled after any domain
4333 * element of "map2" by the subtree underneath the current band node,
4334 * while at the same time being scheduled together by the current
4335 * band node, i.e., by "map1" and "map2?
4337 * If the child of the current band node is a leaf, then
4338 * no element can be scheduled after any other element.
4340 * Otherwise, we construct a relation between domain elements
4341 * of "map1" and domain elements of "map2" that are scheduled
4342 * together and then check if the subtree underneath the current
4343 * band node determines their relative order.
4345 static int after_in_subtree(__isl_keep isl_ast_build *build,
4346 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4348 isl_schedule_node *node;
4349 isl_map *map;
4350 isl_union_map *umap;
4351 int after;
4353 node = isl_ast_build_get_schedule_node(build);
4354 if (!node)
4355 return -1;
4356 node = isl_schedule_node_child(node, 0);
4357 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4358 isl_schedule_node_free(node);
4359 return 0;
4361 map = isl_map_copy(map2);
4362 map = isl_map_apply_domain(map, isl_map_copy(map1));
4363 umap = isl_union_map_from_map(map);
4364 after = after_in_tree(umap, node);
4365 isl_union_map_free(umap);
4366 isl_schedule_node_free(node);
4367 return after;
4370 /* Internal data for any_scheduled_after.
4372 * "build" is the build in which the AST is constructed.
4373 * "depth" is the number of loops that have already been generated
4374 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4375 * "domain" is an array of set-map pairs corresponding to the different
4376 * iteration domains. The set is the schedule domain, i.e., the domain
4377 * of the inverse schedule, while the map is the inverse schedule itself.
4379 struct isl_any_scheduled_after_data {
4380 isl_ast_build *build;
4381 int depth;
4382 int group_coscheduled;
4383 struct isl_set_map_pair *domain;
4386 /* Is any element of domain "i" scheduled after any element of domain "j"
4387 * (for a common iteration of the first data->depth loops)?
4389 * data->domain[i].set contains the domain of the inverse schedule
4390 * for domain "i", i.e., elements in the schedule domain.
4392 * If we are inside a band of a schedule tree and there is a pair
4393 * of elements in the two domains that is schedule together by
4394 * the current band, then we check if any element of "i" may be schedule
4395 * after element of "j" by the descendants of the band node.
4397 * If data->group_coscheduled is set, then we also return 1 if there
4398 * is any pair of elements in the two domains that are scheduled together.
4400 static isl_bool any_scheduled_after(int i, int j, void *user)
4402 struct isl_any_scheduled_after_data *data = user;
4403 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4404 int pos;
4406 for (pos = data->depth; pos < dim; ++pos) {
4407 int follows;
4409 follows = isl_set_follows_at(data->domain[i].set,
4410 data->domain[j].set, pos);
4412 if (follows < -1)
4413 return isl_bool_error;
4414 if (follows > 0)
4415 return isl_bool_true;
4416 if (follows < 0)
4417 return isl_bool_false;
4420 if (isl_ast_build_has_schedule_node(data->build)) {
4421 int after;
4423 after = after_in_subtree(data->build, data->domain[i].map,
4424 data->domain[j].map);
4425 if (after < 0 || after)
4426 return after;
4429 return data->group_coscheduled;
4432 /* Look for independent components at the current depth and generate code
4433 * for each component separately. The resulting lists of grafts are
4434 * merged in an attempt to combine grafts with identical guards.
4436 * Code for two domains can be generated separately if all the elements
4437 * of one domain are scheduled before (or together with) all the elements
4438 * of the other domain. We therefore consider the graph with as nodes
4439 * the domains and an edge between two nodes if any element of the first
4440 * node is scheduled after any element of the second node.
4441 * If the ast_build_group_coscheduled is set, then we also add an edge if
4442 * there is any pair of elements in the two domains that are scheduled
4443 * together.
4444 * Code is then generated (by generate_component)
4445 * for each of the strongly connected components in this graph
4446 * in their topological order.
4448 * Since the test is performed on the domain of the inverse schedules of
4449 * the different domains, we precompute these domains and store
4450 * them in data.domain.
4452 static __isl_give isl_ast_graft_list *generate_components(
4453 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4455 int i;
4456 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4457 int n = isl_union_map_n_map(executed);
4458 struct isl_any_scheduled_after_data data;
4459 struct isl_set_map_pair *next;
4460 struct isl_tarjan_graph *g = NULL;
4461 isl_ast_graft_list *list = NULL;
4462 int n_domain = 0;
4464 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4465 if (!data.domain)
4466 goto error;
4467 n_domain = n;
4469 next = data.domain;
4470 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4471 goto error;
4473 if (!build)
4474 goto error;
4475 data.build = build;
4476 data.depth = isl_ast_build_get_depth(build);
4477 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4478 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4479 if (!g)
4480 goto error;
4482 list = isl_ast_graft_list_alloc(ctx, 0);
4484 i = 0;
4485 while (list && n) {
4486 isl_ast_graft_list *list_c;
4487 int first = i;
4489 if (g->order[i] == -1)
4490 isl_die(ctx, isl_error_internal, "cannot happen",
4491 goto error);
4492 ++i; --n;
4493 while (g->order[i] != -1) {
4494 ++i; --n;
4497 list_c = generate_component(data.domain,
4498 g->order + first, i - first,
4499 isl_ast_build_copy(build));
4500 list = isl_ast_graft_list_merge(list, list_c, build);
4502 ++i;
4505 if (0)
4506 error: list = isl_ast_graft_list_free(list);
4507 isl_tarjan_graph_free(g);
4508 for (i = 0; i < n_domain; ++i) {
4509 isl_map_free(data.domain[i].map);
4510 isl_set_free(data.domain[i].set);
4512 free(data.domain);
4513 isl_union_map_free(executed);
4514 isl_ast_build_free(build);
4516 return list;
4519 /* Generate code for the next level (and all inner levels).
4521 * If "executed" is empty, i.e., no code needs to be generated,
4522 * then we return an empty list.
4524 * If we have already generated code for all loop levels, then we pass
4525 * control to generate_inner_level.
4527 * If "executed" lives in a single space, i.e., if code needs to be
4528 * generated for a single domain, then there can only be a single
4529 * component and we go directly to generate_shifted_component.
4530 * Otherwise, we call generate_components to detect the components
4531 * and to call generate_component on each of them separately.
4533 static __isl_give isl_ast_graft_list *generate_next_level(
4534 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4536 int depth;
4538 if (!build || !executed)
4539 goto error;
4541 if (isl_union_map_is_empty(executed)) {
4542 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4543 isl_union_map_free(executed);
4544 isl_ast_build_free(build);
4545 return isl_ast_graft_list_alloc(ctx, 0);
4548 depth = isl_ast_build_get_depth(build);
4549 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4550 return generate_inner_level(executed, build);
4552 if (isl_union_map_n_map(executed) == 1)
4553 return generate_shifted_component(executed, build);
4555 return generate_components(executed, build);
4556 error:
4557 isl_union_map_free(executed);
4558 isl_ast_build_free(build);
4559 return NULL;
4562 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4563 * internal, executed and build are the inputs to generate_code.
4564 * list collects the output.
4566 struct isl_generate_code_data {
4567 int internal;
4568 isl_union_map *executed;
4569 isl_ast_build *build;
4571 isl_ast_graft_list *list;
4574 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4576 * [E -> S] -> D
4578 * with E the external build schedule and S the additional schedule "space",
4579 * reformulate the inverse schedule in terms of the internal schedule domain,
4580 * i.e., return
4582 * [I -> S] -> D
4584 * We first obtain a mapping
4586 * I -> E
4588 * take the inverse and the product with S -> S, resulting in
4590 * [I -> S] -> [E -> S]
4592 * Applying the map to the input produces the desired result.
4594 static __isl_give isl_union_map *internal_executed(
4595 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4596 __isl_keep isl_ast_build *build)
4598 isl_map *id, *proj;
4600 proj = isl_ast_build_get_schedule_map(build);
4601 proj = isl_map_reverse(proj);
4602 space = isl_space_map_from_set(isl_space_copy(space));
4603 id = isl_map_identity(space);
4604 proj = isl_map_product(proj, id);
4605 executed = isl_union_map_apply_domain(executed,
4606 isl_union_map_from_map(proj));
4607 return executed;
4610 /* Generate an AST that visits the elements in the range of data->executed
4611 * in the relative order specified by the corresponding domain element(s)
4612 * for those domain elements that belong to "set".
4613 * Add the result to data->list.
4615 * The caller ensures that "set" is a universe domain.
4616 * "space" is the space of the additional part of the schedule.
4617 * It is equal to the space of "set" if build->domain is parametric.
4618 * Otherwise, it is equal to the range of the wrapped space of "set".
4620 * If the build space is not parametric and
4621 * if isl_ast_build_node_from_schedule_map
4622 * was called from an outside user (data->internal not set), then
4623 * the (inverse) schedule refers to the external build domain and needs to
4624 * be transformed to refer to the internal build domain.
4626 * If the build space is parametric, then we add some of the parameter
4627 * constraints to the executed relation. Adding these constraints
4628 * allows for an earlier detection of conflicts in some cases.
4629 * However, we do not want to divide the executed relation into
4630 * more disjuncts than necessary. We therefore approximate
4631 * the constraints on the parameters by a single disjunct set.
4633 * The build is extended to include the additional part of the schedule.
4634 * If the original build space was not parametric, then the options
4635 * in data->build refer only to the additional part of the schedule
4636 * and they need to be adjusted to refer to the complete AST build
4637 * domain.
4639 * After having adjusted inverse schedule and build, we start generating
4640 * code with the outer loop of the current code generation
4641 * in generate_next_level.
4643 * If the original build space was not parametric, we undo the embedding
4644 * on the resulting isl_ast_node_list so that it can be used within
4645 * the outer AST build.
4647 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4648 __isl_take isl_set *set, __isl_take isl_space *space)
4650 isl_union_map *executed;
4651 isl_ast_build *build;
4652 isl_ast_graft_list *list;
4653 int embed;
4655 executed = isl_union_map_copy(data->executed);
4656 executed = isl_union_map_intersect_domain(executed,
4657 isl_union_set_from_set(set));
4659 embed = !isl_set_is_params(data->build->domain);
4660 if (embed && !data->internal)
4661 executed = internal_executed(executed, space, data->build);
4662 if (!embed) {
4663 isl_set *domain;
4664 domain = isl_ast_build_get_domain(data->build);
4665 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4666 executed = isl_union_map_intersect_params(executed, domain);
4669 build = isl_ast_build_copy(data->build);
4670 build = isl_ast_build_product(build, space);
4672 list = generate_next_level(executed, build);
4674 list = isl_ast_graft_list_unembed(list, embed);
4676 data->list = isl_ast_graft_list_concat(data->list, list);
4678 return isl_stat_ok;
4681 /* Generate an AST that visits the elements in the range of data->executed
4682 * in the relative order specified by the corresponding domain element(s)
4683 * for those domain elements that belong to "set".
4684 * Add the result to data->list.
4686 * The caller ensures that "set" is a universe domain.
4688 * If the build space S is not parametric, then the space of "set"
4689 * need to be a wrapped relation with S as domain. That is, it needs
4690 * to be of the form
4692 * [S -> T]
4694 * Check this property and pass control to generate_code_in_space
4695 * passing along T.
4696 * If the build space is not parametric, then T is the space of "set".
4698 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4700 struct isl_generate_code_data *data = user;
4701 isl_space *space, *build_space;
4702 int is_domain;
4704 space = isl_set_get_space(set);
4706 if (isl_set_is_params(data->build->domain))
4707 return generate_code_in_space(data, set, space);
4709 build_space = isl_ast_build_get_space(data->build, data->internal);
4710 space = isl_space_unwrap(space);
4711 is_domain = isl_space_is_domain(build_space, space);
4712 isl_space_free(build_space);
4713 space = isl_space_range(space);
4715 if (is_domain < 0)
4716 goto error;
4717 if (!is_domain)
4718 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4719 "invalid nested schedule space", goto error);
4721 return generate_code_in_space(data, set, space);
4722 error:
4723 isl_set_free(set);
4724 isl_space_free(space);
4725 return isl_stat_error;
4728 /* Generate an AST that visits the elements in the range of "executed"
4729 * in the relative order specified by the corresponding domain element(s).
4731 * "build" is an isl_ast_build that has either been constructed by
4732 * isl_ast_build_from_context or passed to a callback set by
4733 * isl_ast_build_set_create_leaf.
4734 * In the first case, the space of the isl_ast_build is typically
4735 * a parametric space, although this is currently not enforced.
4736 * In the second case, the space is never a parametric space.
4737 * If the space S is not parametric, then the domain space(s) of "executed"
4738 * need to be wrapped relations with S as domain.
4740 * If the domain of "executed" consists of several spaces, then an AST
4741 * is generated for each of them (in arbitrary order) and the results
4742 * are concatenated.
4744 * If "internal" is set, then the domain "S" above refers to the internal
4745 * schedule domain representation. Otherwise, it refers to the external
4746 * representation, as returned by isl_ast_build_get_schedule_space.
4748 * We essentially run over all the spaces in the domain of "executed"
4749 * and call generate_code_set on each of them.
4751 static __isl_give isl_ast_graft_list *generate_code(
4752 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4753 int internal)
4755 isl_ctx *ctx;
4756 struct isl_generate_code_data data = { 0 };
4757 isl_space *space;
4758 isl_union_set *schedule_domain;
4759 isl_union_map *universe;
4761 if (!build)
4762 goto error;
4763 space = isl_ast_build_get_space(build, 1);
4764 space = isl_space_align_params(space,
4765 isl_union_map_get_space(executed));
4766 space = isl_space_align_params(space,
4767 isl_union_map_get_space(build->options));
4768 build = isl_ast_build_align_params(build, isl_space_copy(space));
4769 executed = isl_union_map_align_params(executed, space);
4770 if (!executed || !build)
4771 goto error;
4773 ctx = isl_ast_build_get_ctx(build);
4775 data.internal = internal;
4776 data.executed = executed;
4777 data.build = build;
4778 data.list = isl_ast_graft_list_alloc(ctx, 0);
4780 universe = isl_union_map_universe(isl_union_map_copy(executed));
4781 schedule_domain = isl_union_map_domain(universe);
4782 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
4783 &data) < 0)
4784 data.list = isl_ast_graft_list_free(data.list);
4786 isl_union_set_free(schedule_domain);
4787 isl_union_map_free(executed);
4789 isl_ast_build_free(build);
4790 return data.list;
4791 error:
4792 isl_union_map_free(executed);
4793 isl_ast_build_free(build);
4794 return NULL;
4797 /* Generate an AST that visits the elements in the domain of "schedule"
4798 * in the relative order specified by the corresponding image element(s).
4800 * "build" is an isl_ast_build that has either been constructed by
4801 * isl_ast_build_from_context or passed to a callback set by
4802 * isl_ast_build_set_create_leaf.
4803 * In the first case, the space of the isl_ast_build is typically
4804 * a parametric space, although this is currently not enforced.
4805 * In the second case, the space is never a parametric space.
4806 * If the space S is not parametric, then the range space(s) of "schedule"
4807 * need to be wrapped relations with S as domain.
4809 * If the range of "schedule" consists of several spaces, then an AST
4810 * is generated for each of them (in arbitrary order) and the results
4811 * are concatenated.
4813 * We first initialize the local copies of the relevant options.
4814 * We do this here rather than when the isl_ast_build is created
4815 * because the options may have changed between the construction
4816 * of the isl_ast_build and the call to isl_generate_code.
4818 * The main computation is performed on an inverse schedule (with
4819 * the schedule domain in the domain and the elements to be executed
4820 * in the range) called "executed".
4822 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
4823 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4825 isl_ast_graft_list *list;
4826 isl_ast_node *node;
4827 isl_union_map *executed;
4829 build = isl_ast_build_copy(build);
4830 build = isl_ast_build_set_single_valued(build, 0);
4831 schedule = isl_union_map_coalesce(schedule);
4832 schedule = isl_union_map_remove_redundancies(schedule);
4833 executed = isl_union_map_reverse(schedule);
4834 list = generate_code(executed, isl_ast_build_copy(build), 0);
4835 node = isl_ast_node_from_graft_list(list, build);
4836 isl_ast_build_free(build);
4838 return node;
4841 /* The old name for isl_ast_build_node_from_schedule_map.
4842 * It is being kept for backward compatibility, but
4843 * it will be removed in the future.
4845 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
4846 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4848 return isl_ast_build_node_from_schedule_map(build, schedule);
4851 /* Generate an AST that visits the elements in the domain of "executed"
4852 * in the relative order specified by the band node "node" and its descendants.
4854 * The relation "executed" maps the outer generated loop iterators
4855 * to the domain elements executed by those iterations.
4857 * If the band is empty, we continue with its descendants.
4858 * Otherwise, we extend the build and the inverse schedule with
4859 * the additional space/partial schedule and continue generating
4860 * an AST in generate_next_level.
4861 * As soon as we have extended the inverse schedule with the additional
4862 * partial schedule, we look for equalities that may exists between
4863 * the old and the new part.
4865 static __isl_give isl_ast_graft_list *build_ast_from_band(
4866 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4867 __isl_take isl_union_map *executed)
4869 isl_space *space;
4870 isl_multi_union_pw_aff *extra;
4871 isl_union_map *extra_umap;
4872 isl_ast_graft_list *list;
4873 unsigned n1, n2;
4875 if (!build || !node || !executed)
4876 goto error;
4878 if (isl_schedule_node_band_n_member(node) == 0)
4879 return build_ast_from_child(build, node, executed);
4881 extra = isl_schedule_node_band_get_partial_schedule(node);
4882 extra = isl_multi_union_pw_aff_align_params(extra,
4883 isl_ast_build_get_space(build, 1));
4884 space = isl_multi_union_pw_aff_get_space(extra);
4886 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
4887 extra_umap = isl_union_map_reverse(extra_umap);
4889 executed = isl_union_map_domain_product(executed, extra_umap);
4890 executed = isl_union_map_detect_equalities(executed);
4892 n1 = isl_ast_build_dim(build, isl_dim_param);
4893 build = isl_ast_build_product(build, space);
4894 n2 = isl_ast_build_dim(build, isl_dim_param);
4895 if (n2 > n1)
4896 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
4897 "band node is not allowed to introduce new parameters",
4898 build = isl_ast_build_free(build));
4899 build = isl_ast_build_set_schedule_node(build, node);
4901 list = generate_next_level(executed, build);
4903 list = isl_ast_graft_list_unembed(list, 1);
4905 return list;
4906 error:
4907 isl_schedule_node_free(node);
4908 isl_union_map_free(executed);
4909 isl_ast_build_free(build);
4910 return NULL;
4913 /* Hoist a list of grafts (in practice containing a single graft)
4914 * from "sub_build" (which includes extra context information)
4915 * to "build".
4917 * In particular, project out all additional parameters introduced
4918 * by the context node from the enforced constraints and the guard
4919 * of the single graft.
4921 static __isl_give isl_ast_graft_list *hoist_out_of_context(
4922 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
4923 __isl_keep isl_ast_build *sub_build)
4925 isl_ast_graft *graft;
4926 isl_basic_set *enforced;
4927 isl_set *guard;
4928 unsigned n_param, extra_param;
4930 if (!build || !sub_build)
4931 return isl_ast_graft_list_free(list);
4933 n_param = isl_ast_build_dim(build, isl_dim_param);
4934 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
4936 if (extra_param == n_param)
4937 return list;
4939 extra_param -= n_param;
4940 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
4941 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
4942 n_param, extra_param);
4943 enforced = isl_basic_set_remove_unknown_divs(enforced);
4944 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
4945 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
4946 n_param, extra_param);
4947 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
4948 guard = isl_set_compute_divs(guard);
4949 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
4950 build, sub_build);
4951 list = isl_ast_graft_list_from_ast_graft(graft);
4953 return list;
4956 /* Generate an AST that visits the elements in the domain of "executed"
4957 * in the relative order specified by the context node "node"
4958 * and its descendants.
4960 * The relation "executed" maps the outer generated loop iterators
4961 * to the domain elements executed by those iterations.
4963 * The context node may introduce additional parameters as well as
4964 * constraints on the outer schedule dimenions or original parameters.
4966 * We add the extra parameters to a new build and the context
4967 * constraints to both the build and (as a single disjunct)
4968 * to the domain of "executed". Since the context constraints
4969 * are specified in terms of the input schedule, we first need
4970 * to map them to the internal schedule domain.
4972 * After constructing the AST from the descendants of "node",
4973 * we combine the list of grafts into a single graft within
4974 * the new build, in order to be able to exploit the additional
4975 * context constraints during this combination.
4977 * Additionally, if the current node is the outermost node in
4978 * the schedule tree (apart from the root domain node), we generate
4979 * all pending guards, again to be able to exploit the additional
4980 * context constraints. We currently do not do this for internal
4981 * context nodes since we may still want to hoist conditions
4982 * to outer AST nodes.
4984 * If the context node introduced any new parameters, then they
4985 * are removed from the set of enforced constraints and guard
4986 * in hoist_out_of_context.
4988 static __isl_give isl_ast_graft_list *build_ast_from_context(
4989 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
4990 __isl_take isl_union_map *executed)
4992 isl_set *context;
4993 isl_space *space;
4994 isl_multi_aff *internal2input;
4995 isl_ast_build *sub_build;
4996 isl_ast_graft_list *list;
4997 int n, depth;
4999 depth = isl_schedule_node_get_tree_depth(node);
5000 space = isl_ast_build_get_space(build, 1);
5001 context = isl_schedule_node_context_get_context(node);
5002 context = isl_set_align_params(context, space);
5003 sub_build = isl_ast_build_copy(build);
5004 space = isl_set_get_space(context);
5005 sub_build = isl_ast_build_align_params(sub_build, space);
5006 internal2input = isl_ast_build_get_internal2input(sub_build);
5007 context = isl_set_preimage_multi_aff(context, internal2input);
5008 sub_build = isl_ast_build_restrict_generated(sub_build,
5009 isl_set_copy(context));
5010 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5011 executed = isl_union_map_intersect_domain(executed,
5012 isl_union_set_from_set(context));
5014 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5015 node, executed);
5016 n = isl_ast_graft_list_n_ast_graft(list);
5017 if (n < 0)
5018 list = isl_ast_graft_list_free(list);
5020 list = isl_ast_graft_list_fuse(list, sub_build);
5021 if (depth == 1)
5022 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5023 sub_build);
5024 if (n >= 1)
5025 list = hoist_out_of_context(list, build, sub_build);
5027 isl_ast_build_free(build);
5028 isl_ast_build_free(sub_build);
5030 return list;
5033 /* Generate an AST that visits the elements in the domain of "executed"
5034 * in the relative order specified by the expansion node "node" and
5035 * its descendants.
5037 * The relation "executed" maps the outer generated loop iterators
5038 * to the domain elements executed by those iterations.
5040 * We expand the domain elements by the expansion and
5041 * continue with the descendants of the node.
5043 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5044 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5045 __isl_take isl_union_map *executed)
5047 isl_union_map *expansion;
5048 unsigned n1, n2;
5050 expansion = isl_schedule_node_expansion_get_expansion(node);
5051 expansion = isl_union_map_align_params(expansion,
5052 isl_union_map_get_space(executed));
5054 n1 = isl_union_map_dim(executed, isl_dim_param);
5055 executed = isl_union_map_apply_range(executed, expansion);
5056 n2 = isl_union_map_dim(executed, isl_dim_param);
5057 if (n2 > n1)
5058 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5059 "expansion node is not allowed to introduce "
5060 "new parameters", goto error);
5062 return build_ast_from_child(build, node, executed);
5063 error:
5064 isl_ast_build_free(build);
5065 isl_schedule_node_free(node);
5066 isl_union_map_free(executed);
5067 return NULL;
5070 /* Generate an AST that visits the elements in the domain of "executed"
5071 * in the relative order specified by the extension node "node" and
5072 * its descendants.
5074 * The relation "executed" maps the outer generated loop iterators
5075 * to the domain elements executed by those iterations.
5077 * Extend the inverse schedule with the extension applied to current
5078 * set of generated constraints. Since the extension if formulated
5079 * in terms of the input schedule, it first needs to be transformed
5080 * to refer to the internal schedule.
5082 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5083 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5084 __isl_take isl_union_map *executed)
5086 isl_union_set *schedule_domain;
5087 isl_union_map *extension;
5088 isl_set *set;
5090 set = isl_ast_build_get_generated(build);
5091 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5092 schedule_domain = isl_union_set_from_set(set);
5094 extension = isl_schedule_node_extension_get_extension(node);
5096 extension = isl_union_map_preimage_domain_multi_aff(extension,
5097 isl_multi_aff_copy(build->internal2input));
5098 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5099 extension = isl_ast_build_substitute_values_union_map_domain(build,
5100 extension);
5101 executed = isl_union_map_union(executed, extension);
5103 return build_ast_from_child(build, node, executed);
5106 /* Generate an AST that visits the elements in the domain of "executed"
5107 * in the relative order specified by the filter node "node" and
5108 * its descendants.
5110 * The relation "executed" maps the outer generated loop iterators
5111 * to the domain elements executed by those iterations.
5113 * We simply intersect the iteration domain (i.e., the range of "executed")
5114 * with the filter and continue with the descendants of the node,
5115 * unless the resulting inverse schedule is empty, in which
5116 * case we return an empty list.
5118 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5119 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5120 __isl_take isl_union_map *executed)
5122 isl_ctx *ctx;
5123 isl_union_set *filter;
5124 isl_ast_graft_list *list;
5125 int empty;
5126 unsigned n1, n2;
5128 if (!build || !node || !executed)
5129 goto error;
5131 filter = isl_schedule_node_filter_get_filter(node);
5132 filter = isl_union_set_align_params(filter,
5133 isl_union_map_get_space(executed));
5134 n1 = isl_union_map_dim(executed, isl_dim_param);
5135 executed = isl_union_map_intersect_range(executed, filter);
5136 n2 = isl_union_map_dim(executed, isl_dim_param);
5137 if (n2 > n1)
5138 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5139 "filter node is not allowed to introduce "
5140 "new parameters", goto error);
5142 empty = isl_union_map_is_empty(executed);
5143 if (empty < 0)
5144 goto error;
5145 if (!empty)
5146 return build_ast_from_child(build, node, executed);
5148 ctx = isl_ast_build_get_ctx(build);
5149 list = isl_ast_graft_list_alloc(ctx, 0);
5150 isl_ast_build_free(build);
5151 isl_schedule_node_free(node);
5152 isl_union_map_free(executed);
5153 return list;
5154 error:
5155 isl_ast_build_free(build);
5156 isl_schedule_node_free(node);
5157 isl_union_map_free(executed);
5158 return NULL;
5161 /* Generate an AST that visits the elements in the domain of "executed"
5162 * in the relative order specified by the guard node "node" and
5163 * its descendants.
5165 * The relation "executed" maps the outer generated loop iterators
5166 * to the domain elements executed by those iterations.
5168 * Ensure that the associated guard is enforced by the outer AST
5169 * constructs by adding it to the guard of the graft.
5170 * Since we know that we will enforce the guard, we can also include it
5171 * in the generated constraints used to construct an AST for
5172 * the descendant nodes.
5174 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5175 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5176 __isl_take isl_union_map *executed)
5178 isl_space *space;
5179 isl_set *guard, *hoisted;
5180 isl_basic_set *enforced;
5181 isl_ast_build *sub_build;
5182 isl_ast_graft *graft;
5183 isl_ast_graft_list *list;
5184 unsigned n1, n2;
5186 space = isl_ast_build_get_space(build, 1);
5187 guard = isl_schedule_node_guard_get_guard(node);
5188 n1 = isl_space_dim(space, isl_dim_param);
5189 guard = isl_set_align_params(guard, space);
5190 n2 = isl_set_dim(guard, isl_dim_param);
5191 if (n2 > n1)
5192 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5193 "guard node is not allowed to introduce "
5194 "new parameters", guard = isl_set_free(guard));
5195 guard = isl_set_preimage_multi_aff(guard,
5196 isl_multi_aff_copy(build->internal2input));
5197 guard = isl_ast_build_specialize(build, guard);
5198 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5200 sub_build = isl_ast_build_copy(build);
5201 sub_build = isl_ast_build_restrict_generated(sub_build,
5202 isl_set_copy(guard));
5204 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5205 node, executed);
5207 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5208 if (isl_set_n_basic_set(hoisted) > 1)
5209 list = isl_ast_graft_list_gist_guards(list,
5210 isl_set_copy(hoisted));
5211 guard = isl_set_intersect(guard, hoisted);
5212 enforced = extract_shared_enforced(list, build);
5213 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5214 build, sub_build);
5216 isl_ast_build_free(sub_build);
5217 isl_ast_build_free(build);
5218 return isl_ast_graft_list_from_ast_graft(graft);
5221 /* Call the before_each_mark callback, if requested by the user.
5223 * Return 0 on success and -1 on error.
5225 * The caller is responsible for recording the current inverse schedule
5226 * in "build".
5228 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5229 __isl_keep isl_ast_build *build)
5231 if (!build)
5232 return isl_stat_error;
5233 if (!build->before_each_mark)
5234 return isl_stat_ok;
5235 return build->before_each_mark(mark, build,
5236 build->before_each_mark_user);
5239 /* Call the after_each_mark callback, if requested by the user.
5241 * The caller is responsible for recording the current inverse schedule
5242 * in "build".
5244 static __isl_give isl_ast_graft *after_each_mark(
5245 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5247 if (!graft || !build)
5248 return isl_ast_graft_free(graft);
5249 if (!build->after_each_mark)
5250 return graft;
5251 graft->node = build->after_each_mark(graft->node, build,
5252 build->after_each_mark_user);
5253 if (!graft->node)
5254 return isl_ast_graft_free(graft);
5255 return graft;
5259 /* Generate an AST that visits the elements in the domain of "executed"
5260 * in the relative order specified by the mark node "node" and
5261 * its descendants.
5263 * The relation "executed" maps the outer generated loop iterators
5264 * to the domain elements executed by those iterations.
5266 * Since we may be calling before_each_mark and after_each_mark
5267 * callbacks, we record the current inverse schedule in the build.
5269 * We generate an AST for the child of the mark node, combine
5270 * the graft list into a single graft and then insert the mark
5271 * in the AST of that single graft.
5273 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5274 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5275 __isl_take isl_union_map *executed)
5277 isl_id *mark;
5278 isl_ast_graft *graft;
5279 isl_ast_graft_list *list;
5280 int n;
5282 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5284 mark = isl_schedule_node_mark_get_id(node);
5285 if (before_each_mark(mark, build) < 0)
5286 node = isl_schedule_node_free(node);
5288 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5289 list = isl_ast_graft_list_fuse(list, build);
5290 n = isl_ast_graft_list_n_ast_graft(list);
5291 if (n < 0)
5292 list = isl_ast_graft_list_free(list);
5293 if (n == 0) {
5294 isl_id_free(mark);
5295 } else {
5296 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5297 graft = isl_ast_graft_insert_mark(graft, mark);
5298 graft = after_each_mark(graft, build);
5299 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5301 isl_ast_build_free(build);
5303 return list;
5306 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5307 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5308 __isl_take isl_union_map *executed);
5310 /* Generate an AST that visits the elements in the domain of "executed"
5311 * in the relative order specified by the sequence (or set) node "node" and
5312 * its descendants.
5314 * The relation "executed" maps the outer generated loop iterators
5315 * to the domain elements executed by those iterations.
5317 * We simply generate an AST for each of the children and concatenate
5318 * the results.
5320 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5321 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5322 __isl_take isl_union_map *executed)
5324 int i, n;
5325 isl_ctx *ctx;
5326 isl_ast_graft_list *list;
5328 ctx = isl_ast_build_get_ctx(build);
5329 list = isl_ast_graft_list_alloc(ctx, 0);
5331 n = isl_schedule_node_n_children(node);
5332 for (i = 0; i < n; ++i) {
5333 isl_schedule_node *child;
5334 isl_ast_graft_list *list_i;
5336 child = isl_schedule_node_get_child(node, i);
5337 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5338 child, isl_union_map_copy(executed));
5339 list = isl_ast_graft_list_concat(list, list_i);
5341 isl_ast_build_free(build);
5342 isl_schedule_node_free(node);
5343 isl_union_map_free(executed);
5345 return list;
5348 /* Generate an AST that visits the elements in the domain of "executed"
5349 * in the relative order specified by the node "node" and its descendants.
5351 * The relation "executed" maps the outer generated loop iterators
5352 * to the domain elements executed by those iterations.
5354 * If the node is a leaf, then we pass control to generate_inner_level.
5355 * Note that the current build does not refer to any band node, so
5356 * that generate_inner_level will not try to visit the child of
5357 * the leaf node.
5359 * The other node types are handled in separate functions.
5360 * Set nodes are currently treated in the same way as sequence nodes.
5361 * The children of a set node may be executed in any order,
5362 * including the order of the children.
5364 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5365 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5366 __isl_take isl_union_map *executed)
5368 enum isl_schedule_node_type type;
5370 type = isl_schedule_node_get_type(node);
5372 switch (type) {
5373 case isl_schedule_node_error:
5374 goto error;
5375 case isl_schedule_node_leaf:
5376 isl_schedule_node_free(node);
5377 return generate_inner_level(executed, build);
5378 case isl_schedule_node_band:
5379 return build_ast_from_band(build, node, executed);
5380 case isl_schedule_node_context:
5381 return build_ast_from_context(build, node, executed);
5382 case isl_schedule_node_domain:
5383 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5384 "unexpected internal domain node", goto error);
5385 case isl_schedule_node_expansion:
5386 return build_ast_from_expansion(build, node, executed);
5387 case isl_schedule_node_extension:
5388 return build_ast_from_extension(build, node, executed);
5389 case isl_schedule_node_filter:
5390 return build_ast_from_filter(build, node, executed);
5391 case isl_schedule_node_guard:
5392 return build_ast_from_guard(build, node, executed);
5393 case isl_schedule_node_mark:
5394 return build_ast_from_mark(build, node, executed);
5395 case isl_schedule_node_sequence:
5396 case isl_schedule_node_set:
5397 return build_ast_from_sequence(build, node, executed);
5400 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5401 "unhandled type", goto error);
5402 error:
5403 isl_union_map_free(executed);
5404 isl_schedule_node_free(node);
5405 isl_ast_build_free(build);
5407 return NULL;
5410 /* Generate an AST that visits the elements in the domain of "executed"
5411 * in the relative order specified by the (single) child of "node" and
5412 * its descendants.
5414 * The relation "executed" maps the outer generated loop iterators
5415 * to the domain elements executed by those iterations.
5417 * This function is never called on a leaf, set or sequence node,
5418 * so the node always has exactly one child.
5420 static __isl_give isl_ast_graft_list *build_ast_from_child(
5421 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5422 __isl_take isl_union_map *executed)
5424 node = isl_schedule_node_child(node, 0);
5425 return build_ast_from_schedule_node(build, node, executed);
5428 /* Generate an AST that visits the elements in the domain of the domain
5429 * node "node" in the relative order specified by its descendants.
5431 * An initial inverse schedule is created that maps a zero-dimensional
5432 * schedule space to the node domain.
5433 * The input "build" is assumed to have a parametric domain and
5434 * is replaced by the same zero-dimensional schedule space.
5436 * We also add some of the parameter constraints in the build domain
5437 * to the executed relation. Adding these constraints
5438 * allows for an earlier detection of conflicts in some cases.
5439 * However, we do not want to divide the executed relation into
5440 * more disjuncts than necessary. We therefore approximate
5441 * the constraints on the parameters by a single disjunct set.
5443 static __isl_give isl_ast_node *build_ast_from_domain(
5444 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5446 isl_ctx *ctx;
5447 isl_union_set *domain, *schedule_domain;
5448 isl_union_map *executed;
5449 isl_space *space;
5450 isl_set *set;
5451 isl_ast_graft_list *list;
5452 isl_ast_node *ast;
5453 int is_params;
5455 if (!build)
5456 goto error;
5458 ctx = isl_ast_build_get_ctx(build);
5459 space = isl_ast_build_get_space(build, 1);
5460 is_params = isl_space_is_params(space);
5461 isl_space_free(space);
5462 if (is_params < 0)
5463 goto error;
5464 if (!is_params)
5465 isl_die(ctx, isl_error_unsupported,
5466 "expecting parametric initial context", goto error);
5468 domain = isl_schedule_node_domain_get_domain(node);
5469 domain = isl_union_set_coalesce(domain);
5471 space = isl_union_set_get_space(domain);
5472 space = isl_space_set_from_params(space);
5473 build = isl_ast_build_product(build, space);
5475 set = isl_ast_build_get_domain(build);
5476 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5477 schedule_domain = isl_union_set_from_set(set);
5479 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5480 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5481 ast = isl_ast_node_from_graft_list(list, build);
5482 isl_ast_build_free(build);
5484 return ast;
5485 error:
5486 isl_schedule_node_free(node);
5487 isl_ast_build_free(build);
5488 return NULL;
5491 /* Generate an AST that visits the elements in the domain of "schedule"
5492 * in the relative order specified by the schedule tree.
5494 * "build" is an isl_ast_build that has been created using
5495 * isl_ast_build_alloc or isl_ast_build_from_context based
5496 * on a parametric set.
5498 * The construction starts at the root node of the schedule,
5499 * which is assumed to be a domain node.
5501 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5502 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5504 isl_ctx *ctx;
5505 isl_schedule_node *node;
5507 if (!build || !schedule)
5508 goto error;
5510 ctx = isl_ast_build_get_ctx(build);
5512 node = isl_schedule_get_root(schedule);
5513 isl_schedule_free(schedule);
5515 build = isl_ast_build_copy(build);
5516 build = isl_ast_build_set_single_valued(build, 0);
5517 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5518 isl_die(ctx, isl_error_unsupported,
5519 "expecting root domain node",
5520 build = isl_ast_build_free(build));
5521 return build_ast_from_domain(build, node);
5522 error:
5523 isl_schedule_free(schedule);
5524 return NULL;