isl_map_convex_hull: detect equalities before checking number of basic maps
[isl.git] / isl_ast_codegen.c
blob21b4596270540482d5216b1770a01f1739da4c5f
1 /*
2 * Copyright 2012-2014 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <limits.h>
11 #include <isl/aff.h>
12 #include <isl/set.h>
13 #include <isl/ilp.h>
14 #include <isl/union_map.h>
15 #include <isl_sort.h>
16 #include <isl_tarjan.h>
17 #include <isl_ast_private.h>
18 #include <isl_ast_build_expr.h>
19 #include <isl_ast_build_private.h>
20 #include <isl_ast_graft_private.h>
22 /* Add the constraint to the list that "user" points to, if it is not
23 * a div constraint.
25 static int collect_constraint(__isl_take isl_constraint *constraint,
26 void *user)
28 isl_constraint_list **list = user;
30 if (isl_constraint_is_div_constraint(constraint))
31 isl_constraint_free(constraint);
32 else
33 *list = isl_constraint_list_add(*list, constraint);
35 return 0;
38 /* Extract the constraints of "bset" (except the div constraints)
39 * and collect them in an isl_constraint_list.
41 static __isl_give isl_constraint_list *isl_constraint_list_from_basic_set(
42 __isl_take isl_basic_set *bset)
44 int n;
45 isl_ctx *ctx;
46 isl_constraint_list *list;
48 if (!bset)
49 return NULL;
51 ctx = isl_basic_set_get_ctx(bset);
53 n = isl_basic_set_n_constraint(bset);
54 list = isl_constraint_list_alloc(ctx, n);
55 if (isl_basic_set_foreach_constraint(bset,
56 &collect_constraint, &list) < 0)
57 list = isl_constraint_list_free(list);
59 isl_basic_set_free(bset);
60 return list;
63 /* Data used in generate_domain.
65 * "build" is the input build.
66 * "list" collects the results.
68 struct isl_generate_domain_data {
69 isl_ast_build *build;
71 isl_ast_graft_list *list;
74 static __isl_give isl_ast_graft_list *generate_next_level(
75 __isl_take isl_union_map *executed,
76 __isl_take isl_ast_build *build);
77 static __isl_give isl_ast_graft_list *generate_code(
78 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
79 int internal);
81 /* Generate an AST for a single domain based on
82 * the (non single valued) inverse schedule "executed".
84 * We extend the schedule with the iteration domain
85 * and continue generating through a call to generate_code.
87 * In particular, if executed has the form
89 * S -> D
91 * then we continue generating code on
93 * [S -> D] -> D
95 * The extended inverse schedule is clearly single valued
96 * ensuring that the nested generate_code will not reach this function,
97 * but will instead create calls to all elements of D that need
98 * to be executed from the current schedule domain.
100 static int generate_non_single_valued(__isl_take isl_map *executed,
101 struct isl_generate_domain_data *data)
103 isl_map *identity;
104 isl_ast_build *build;
105 isl_ast_graft_list *list;
107 build = isl_ast_build_copy(data->build);
109 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
110 executed = isl_map_domain_product(executed, identity);
111 build = isl_ast_build_set_single_valued(build, 1);
113 list = generate_code(isl_union_map_from_map(executed), build, 1);
115 data->list = isl_ast_graft_list_concat(data->list, list);
117 return 0;
120 /* Call the at_each_domain callback, if requested by the user,
121 * after recording the current inverse schedule in the build.
123 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
124 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
126 if (!graft || !build)
127 return isl_ast_graft_free(graft);
128 if (!build->at_each_domain)
129 return graft;
131 build = isl_ast_build_copy(build);
132 build = isl_ast_build_set_executed(build,
133 isl_union_map_from_map(isl_map_copy(executed)));
134 if (!build)
135 return isl_ast_graft_free(graft);
137 graft->node = build->at_each_domain(graft->node,
138 build, build->at_each_domain_user);
139 isl_ast_build_free(build);
141 if (!graft->node)
142 graft = isl_ast_graft_free(graft);
144 return graft;
147 /* Generate an AST for a single domain based on
148 * the inverse schedule "executed" and add it to data->list.
150 * If there is more than one domain element associated to the current
151 * schedule "time", then we need to continue the generation process
152 * in generate_non_single_valued.
153 * Note that the inverse schedule being single-valued may depend
154 * on constraints that are only available in the original context
155 * domain specified by the user. We therefore first introduce
156 * the constraints from data->build->domain.
157 * On the other hand, we only perform the test after having taken the gist
158 * of the domain as the resulting map is the one from which the call
159 * expression is constructed. Using this map to construct the call
160 * expression usually yields simpler results.
161 * Because we perform the single-valuedness test on the gisted map,
162 * we may in rare cases fail to recognize that the inverse schedule
163 * is single-valued. This becomes problematic if this happens
164 * from the recursive call through generate_non_single_valued
165 * as we would then end up in an infinite recursion.
166 * We therefore check if we are inside a call to generate_non_single_valued
167 * and revert to the ungisted map if the gisted map turns out not to be
168 * single-valued.
170 * Otherwise, we generate a call expression for the single executed
171 * domain element and put a guard around it based on the (simplified)
172 * domain of "executed".
174 * If the user has set an at_each_domain callback, it is called
175 * on the constructed call expression node.
177 static int generate_domain(__isl_take isl_map *executed, void *user)
179 struct isl_generate_domain_data *data = user;
180 isl_ast_graft *graft;
181 isl_ast_graft_list *list;
182 isl_set *guard;
183 isl_map *map = NULL;
184 int empty, sv;
186 executed = isl_map_intersect_domain(executed,
187 isl_set_copy(data->build->domain));
188 empty = isl_map_is_empty(executed);
189 if (empty < 0)
190 goto error;
191 if (empty) {
192 isl_map_free(executed);
193 return 0;
196 executed = isl_map_coalesce(executed);
197 map = isl_map_copy(executed);
198 map = isl_ast_build_compute_gist_map_domain(data->build, map);
199 sv = isl_map_is_single_valued(map);
200 if (sv < 0)
201 goto error;
202 if (!sv) {
203 isl_map_free(map);
204 if (data->build->single_valued)
205 map = isl_map_copy(executed);
206 else
207 return generate_non_single_valued(executed, data);
209 guard = isl_map_domain(isl_map_copy(map));
210 guard = isl_set_compute_divs(guard);
211 guard = isl_set_coalesce(guard);
212 guard = isl_ast_build_compute_gist(data->build, guard);
213 graft = isl_ast_graft_alloc_domain(map, data->build);
214 graft = at_each_domain(graft, executed, data->build);
216 isl_map_free(executed);
217 graft = isl_ast_graft_add_guard(graft, guard, data->build);
219 list = isl_ast_graft_list_from_ast_graft(graft);
220 data->list = isl_ast_graft_list_concat(data->list, list);
222 return 0;
223 error:
224 isl_map_free(map);
225 isl_map_free(executed);
226 return -1;
229 /* Call build->create_leaf to a create "leaf" node in the AST,
230 * encapsulate the result in an isl_ast_graft and return the result
231 * as a 1-element list.
233 * Note that the node returned by the user may be an entire tree.
235 * Before we pass control to the user, we first clear some information
236 * from the build that is (presumbably) only meaningful
237 * for the current code generation.
238 * This includes the create_leaf callback itself, so we make a copy
239 * of the build first.
241 static __isl_give isl_ast_graft_list *call_create_leaf(
242 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
244 isl_ast_node *node;
245 isl_ast_graft *graft;
246 isl_ast_build *user_build;
248 user_build = isl_ast_build_copy(build);
249 user_build = isl_ast_build_set_executed(user_build, executed);
250 user_build = isl_ast_build_clear_local_info(user_build);
251 if (!user_build)
252 node = NULL;
253 else
254 node = build->create_leaf(user_build, build->create_leaf_user);
255 graft = isl_ast_graft_alloc(node, build);
256 isl_ast_build_free(build);
257 return isl_ast_graft_list_from_ast_graft(graft);
260 /* Generate an AST after having handled the complete schedule
261 * of this call to the code generator.
263 * If the user has specified a create_leaf callback, control
264 * is passed to the user in call_create_leaf.
266 * Otherwise, we generate one or more calls for each individual
267 * domain in generate_domain.
269 static __isl_give isl_ast_graft_list *generate_inner_level(
270 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
272 isl_ctx *ctx;
273 struct isl_generate_domain_data data = { build };
275 if (!build || !executed)
276 goto error;
278 if (build->create_leaf)
279 return call_create_leaf(executed, build);
281 ctx = isl_union_map_get_ctx(executed);
282 data.list = isl_ast_graft_list_alloc(ctx, 0);
283 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
284 data.list = isl_ast_graft_list_free(data.list);
286 if (0)
287 error: data.list = NULL;
288 isl_ast_build_free(build);
289 isl_union_map_free(executed);
290 return data.list;
293 /* Call the before_each_for callback, if requested by the user.
295 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
296 __isl_keep isl_ast_build *build)
298 isl_id *id;
300 if (!node || !build)
301 return isl_ast_node_free(node);
302 if (!build->before_each_for)
303 return node;
304 id = build->before_each_for(build, build->before_each_for_user);
305 node = isl_ast_node_set_annotation(node, id);
306 return node;
309 /* Call the after_each_for callback, if requested by the user.
311 static __isl_give isl_ast_graft *after_each_for(__isl_keep isl_ast_graft *graft,
312 __isl_keep isl_ast_build *build)
314 if (!graft || !build)
315 return isl_ast_graft_free(graft);
316 if (!build->after_each_for)
317 return graft;
318 graft->node = build->after_each_for(graft->node, build,
319 build->after_each_for_user);
320 if (!graft->node)
321 return isl_ast_graft_free(graft);
322 return graft;
325 /* Plug in all the know values of the current and outer dimensions
326 * in the domain of "executed". In principle, we only need to plug
327 * in the known value of the current dimension since the values of
328 * outer dimensions have been plugged in already.
329 * However, it turns out to be easier to just plug in all known values.
331 static __isl_give isl_union_map *plug_in_values(
332 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
334 return isl_ast_build_substitute_values_union_map_domain(build,
335 executed);
338 /* Check if the constraint "c" is a lower bound on dimension "pos",
339 * an upper bound, or independent of dimension "pos".
341 static int constraint_type(isl_constraint *c, int pos)
343 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
344 return 1;
345 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
346 return 2;
347 return 0;
350 /* Compare the types of the constraints "a" and "b",
351 * resulting in constraints that are independent of "depth"
352 * to be sorted before the lower bounds on "depth", which in
353 * turn are sorted before the upper bounds on "depth".
355 static int cmp_constraint(__isl_keep isl_constraint *a,
356 __isl_keep isl_constraint *b, void *user)
358 int *depth = user;
359 int t1 = constraint_type(a, *depth);
360 int t2 = constraint_type(b, *depth);
362 return t1 - t2;
365 /* Extract a lower bound on dimension "pos" from constraint "c".
367 * If the constraint is of the form
369 * a x + f(...) >= 0
371 * then we essentially return
373 * l = ceil(-f(...)/a)
375 * However, if the current dimension is strided, then we need to make
376 * sure that the lower bound we construct is of the form
378 * f + s a
380 * with f the offset and s the stride.
381 * We therefore compute
383 * f + s * ceil((l - f)/s)
385 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
386 int pos, __isl_keep isl_ast_build *build)
388 isl_aff *aff;
390 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
391 aff = isl_aff_ceil(aff);
393 if (isl_ast_build_has_stride(build, pos)) {
394 isl_aff *offset;
395 isl_val *stride;
397 offset = isl_ast_build_get_offset(build, pos);
398 stride = isl_ast_build_get_stride(build, pos);
400 aff = isl_aff_sub(aff, isl_aff_copy(offset));
401 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
402 aff = isl_aff_ceil(aff);
403 aff = isl_aff_scale_val(aff, stride);
404 aff = isl_aff_add(aff, offset);
407 aff = isl_ast_build_compute_gist_aff(build, aff);
409 return aff;
412 /* Return the exact lower bound (or upper bound if "upper" is set)
413 * of "domain" as a piecewise affine expression.
415 * If we are computing a lower bound (of a strided dimension), then
416 * we need to make sure it is of the form
418 * f + s a
420 * where f is the offset and s is the stride.
421 * We therefore need to include the stride constraint before computing
422 * the minimum.
424 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
425 __isl_keep isl_ast_build *build, int upper)
427 isl_set *stride;
428 isl_map *it_map;
429 isl_pw_aff *pa;
430 isl_pw_multi_aff *pma;
432 domain = isl_set_copy(domain);
433 if (!upper) {
434 stride = isl_ast_build_get_stride_constraint(build);
435 domain = isl_set_intersect(domain, stride);
437 it_map = isl_ast_build_map_to_iterator(build, domain);
438 if (upper)
439 pma = isl_map_lexmax_pw_multi_aff(it_map);
440 else
441 pma = isl_map_lexmin_pw_multi_aff(it_map);
442 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
443 isl_pw_multi_aff_free(pma);
444 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
445 pa = isl_pw_aff_coalesce(pa);
447 return pa;
450 /* Extract a lower bound on dimension "pos" from each constraint
451 * in "constraints" and return the list of lower bounds.
452 * If "constraints" has zero elements, then we extract a lower bound
453 * from "domain" instead.
455 static __isl_give isl_pw_aff_list *lower_bounds(
456 __isl_keep isl_constraint_list *constraints, int pos,
457 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
459 isl_ctx *ctx;
460 isl_pw_aff_list *list;
461 int i, n;
463 if (!build)
464 return NULL;
466 n = isl_constraint_list_n_constraint(constraints);
467 if (n == 0) {
468 isl_pw_aff *pa;
469 pa = exact_bound(domain, build, 0);
470 return isl_pw_aff_list_from_pw_aff(pa);
473 ctx = isl_ast_build_get_ctx(build);
474 list = isl_pw_aff_list_alloc(ctx,n);
476 for (i = 0; i < n; ++i) {
477 isl_aff *aff;
478 isl_constraint *c;
480 c = isl_constraint_list_get_constraint(constraints, i);
481 aff = lower_bound(c, pos, build);
482 isl_constraint_free(c);
483 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
486 return list;
489 /* Extract an upper bound on dimension "pos" from each constraint
490 * in "constraints" and return the list of upper bounds.
491 * If "constraints" has zero elements, then we extract an upper bound
492 * from "domain" instead.
494 static __isl_give isl_pw_aff_list *upper_bounds(
495 __isl_keep isl_constraint_list *constraints, int pos,
496 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
498 isl_ctx *ctx;
499 isl_pw_aff_list *list;
500 int i, n;
502 n = isl_constraint_list_n_constraint(constraints);
503 if (n == 0) {
504 isl_pw_aff *pa;
505 pa = exact_bound(domain, build, 1);
506 return isl_pw_aff_list_from_pw_aff(pa);
509 ctx = isl_ast_build_get_ctx(build);
510 list = isl_pw_aff_list_alloc(ctx,n);
512 for (i = 0; i < n; ++i) {
513 isl_aff *aff;
514 isl_constraint *c;
516 c = isl_constraint_list_get_constraint(constraints, i);
517 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
518 isl_constraint_free(c);
519 aff = isl_aff_floor(aff);
520 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
523 return list;
526 /* Callback for sorting the isl_pw_aff_list passed to reduce_list.
528 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
529 void *user)
531 return isl_pw_aff_plain_cmp(a, b);
534 /* Return an isl_ast_expr that performs the reduction of type "type"
535 * on AST expressions corresponding to the elements in "list".
537 * The list is assumed to contain at least one element.
538 * If the list contains exactly one element, then the returned isl_ast_expr
539 * simply computes that affine expression.
540 * If the list contains more than one element, then we sort it
541 * using a fairly abitrary but hopefully reasonably stable order.
543 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
544 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
546 int i, n;
547 isl_ctx *ctx;
548 isl_ast_expr *expr;
550 if (!list)
551 return NULL;
553 n = isl_pw_aff_list_n_pw_aff(list);
555 if (n == 1)
556 return isl_ast_build_expr_from_pw_aff_internal(build,
557 isl_pw_aff_list_get_pw_aff(list, 0));
559 ctx = isl_pw_aff_list_get_ctx(list);
560 expr = isl_ast_expr_alloc_op(ctx, type, n);
561 if (!expr)
562 return NULL;
564 list = isl_pw_aff_list_copy(list);
565 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
566 if (!list)
567 return isl_ast_expr_free(expr);
569 for (i = 0; i < n; ++i) {
570 isl_ast_expr *expr_i;
572 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
573 isl_pw_aff_list_get_pw_aff(list, i));
574 if (!expr_i)
575 goto error;
576 expr->u.op.args[i] = expr_i;
579 isl_pw_aff_list_free(list);
580 return expr;
581 error:
582 isl_pw_aff_list_free(list);
583 isl_ast_expr_free(expr);
584 return NULL;
587 /* Add a guard to "graft" based on "bound" in the case of a degenerate
588 * level (including the special case of an eliminated level).
590 * We eliminate the current dimension, simplify the result in the current
591 * build and add the result as guards to the graft.
593 * Note that we cannot simply drop the constraints on the current dimension
594 * even in the eliminated case, because the single affine expression may
595 * not be explicitly available in "bounds". Moreover, the single affine
596 * expression may only be defined on a subset of the build domain,
597 * so we do in some cases need to insert a guard even in the eliminated case.
599 static __isl_give isl_ast_graft *add_degenerate_guard(
600 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
601 __isl_keep isl_ast_build *build)
603 int depth;
604 isl_set *dom;
606 depth = isl_ast_build_get_depth(build);
608 dom = isl_set_from_basic_set(isl_basic_set_copy(bounds));
609 if (isl_ast_build_has_stride(build, depth)) {
610 isl_set *stride;
612 stride = isl_ast_build_get_stride_constraint(build);
613 dom = isl_set_intersect(dom, stride);
615 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
616 dom = isl_ast_build_compute_gist(build, dom);
618 graft = isl_ast_graft_add_guard(graft, dom, build);
620 return graft;
623 /* Update "graft" based on "bounds" for the eliminated case.
625 * In the eliminated case, no for node is created, so we only need
626 * to check if "bounds" imply any guards that need to be inserted.
628 static __isl_give isl_ast_graft *refine_eliminated(
629 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
630 __isl_keep isl_ast_build *build)
632 return add_degenerate_guard(graft, bounds, build);
635 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
637 * "build" is the build in which graft->node was created
638 * "sub_build" contains information about the current level itself,
639 * including the single value attained.
641 * We first set the initialization part of the for loop to the single
642 * value attained by the current dimension.
643 * The increment and condition are not strictly needed as the are known
644 * to be "1" and "iterator <= value" respectively.
645 * Then we set the size of the iterator and
646 * check if "bounds" imply any guards that need to be inserted.
648 static __isl_give isl_ast_graft *refine_degenerate(
649 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
650 __isl_keep isl_ast_build *build,
651 __isl_keep isl_ast_build *sub_build)
653 isl_pw_aff *value;
655 if (!graft || !sub_build)
656 return isl_ast_graft_free(graft);
658 value = isl_pw_aff_copy(sub_build->value);
660 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
661 value);
662 if (!graft->node->u.f.init)
663 return isl_ast_graft_free(graft);
665 graft = add_degenerate_guard(graft, bounds, build);
667 return graft;
670 /* Return the intersection of constraints in "list" as a set.
672 static __isl_give isl_set *intersect_constraints(
673 __isl_keep isl_constraint_list *list)
675 int i, n;
676 isl_basic_set *bset;
678 n = isl_constraint_list_n_constraint(list);
679 if (n < 1)
680 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
681 "expecting at least one constraint", return NULL);
683 bset = isl_basic_set_from_constraint(
684 isl_constraint_list_get_constraint(list, 0));
685 for (i = 1; i < n; ++i) {
686 isl_basic_set *bset_i;
688 bset_i = isl_basic_set_from_constraint(
689 isl_constraint_list_get_constraint(list, i));
690 bset = isl_basic_set_intersect(bset, bset_i);
693 return isl_set_from_basic_set(bset);
696 /* Compute the constraints on the outer dimensions enforced by
697 * graft->node and add those constraints to graft->enforced,
698 * in case the upper bound is expressed as a set "upper".
700 * In particular, if l(...) is a lower bound in "lower", and
702 * -a i + f(...) >= 0 or a i <= f(...)
704 * is an upper bound ocnstraint on the current dimension i,
705 * then the for loop enforces the constraint
707 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
709 * We therefore simply take each lower bound in turn, plug it into
710 * the upper bounds and compute the intersection over all lower bounds.
712 * If a lower bound is a rational expression, then
713 * isl_basic_set_preimage_multi_aff will force this rational
714 * expression to have only integer values. However, the loop
715 * itself does not enforce this integrality constraint. We therefore
716 * use the ceil of the lower bounds instead of the lower bounds themselves.
717 * Other constraints will make sure that the for loop is only executed
718 * when each of the lower bounds attains an integral value.
719 * In particular, potentially rational values only occur in
720 * lower_bound if the offset is a (seemingly) rational expression,
721 * but then outer conditions will make sure that this rational expression
722 * only attains integer values.
724 static __isl_give isl_ast_graft *set_enforced_from_set(
725 __isl_take isl_ast_graft *graft,
726 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
728 isl_space *space;
729 isl_basic_set *enforced;
730 isl_pw_multi_aff *pma;
731 int i, n;
733 if (!graft || !lower)
734 return isl_ast_graft_free(graft);
736 space = isl_set_get_space(upper);
737 enforced = isl_basic_set_universe(isl_space_copy(space));
739 space = isl_space_map_from_set(space);
740 pma = isl_pw_multi_aff_identity(space);
742 n = isl_pw_aff_list_n_pw_aff(lower);
743 for (i = 0; i < n; ++i) {
744 isl_pw_aff *pa;
745 isl_set *enforced_i;
746 isl_basic_set *hull;
747 isl_pw_multi_aff *pma_i;
749 pa = isl_pw_aff_list_get_pw_aff(lower, i);
750 pa = isl_pw_aff_ceil(pa);
751 pma_i = isl_pw_multi_aff_copy(pma);
752 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
753 enforced_i = isl_set_copy(upper);
754 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
755 hull = isl_set_simple_hull(enforced_i);
756 enforced = isl_basic_set_intersect(enforced, hull);
759 isl_pw_multi_aff_free(pma);
761 graft = isl_ast_graft_enforce(graft, enforced);
763 return graft;
766 /* Compute the constraints on the outer dimensions enforced by
767 * graft->node and add those constraints to graft->enforced,
768 * in case the upper bound is expressed as
769 * a list of affine expressions "upper".
771 * The enforced condition is that each lower bound expression is less
772 * than or equal to each upper bound expression.
774 static __isl_give isl_ast_graft *set_enforced_from_list(
775 __isl_take isl_ast_graft *graft,
776 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
778 isl_set *cond;
779 isl_basic_set *enforced;
781 lower = isl_pw_aff_list_copy(lower);
782 upper = isl_pw_aff_list_copy(upper);
783 cond = isl_pw_aff_list_le_set(lower, upper);
784 enforced = isl_set_simple_hull(cond);
785 graft = isl_ast_graft_enforce(graft, enforced);
787 return graft;
790 /* Does "aff" have a negative constant term?
792 static int aff_constant_is_negative(__isl_take isl_set *set,
793 __isl_take isl_aff *aff, void *user)
795 int *neg = user;
796 isl_val *v;
798 v = isl_aff_get_constant_val(aff);
799 *neg = isl_val_is_neg(v);
800 isl_val_free(v);
801 isl_set_free(set);
802 isl_aff_free(aff);
804 return *neg ? 0 : -1;
807 /* Does "pa" have a negative constant term over its entire domain?
809 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
811 int r;
812 int *neg = user;
814 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
815 isl_pw_aff_free(pa);
817 return *neg ? 0 : -1;
820 /* Does each element in "list" have a negative constant term?
822 * The callback terminates the iteration as soon an element has been
823 * found that does not have a negative constant term.
825 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
827 int neg = 1;
829 if (isl_pw_aff_list_foreach(list,
830 &pw_aff_constant_is_negative, &neg) < 0 && neg)
831 return -1;
833 return neg;
836 /* Add 1 to each of the elements in "list", where each of these elements
837 * is defined over the internal schedule space of "build".
839 static __isl_give isl_pw_aff_list *list_add_one(
840 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
842 int i, n;
843 isl_space *space;
844 isl_aff *aff;
845 isl_pw_aff *one;
847 space = isl_ast_build_get_space(build, 1);
848 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
849 aff = isl_aff_add_constant_si(aff, 1);
850 one = isl_pw_aff_from_aff(aff);
852 n = isl_pw_aff_list_n_pw_aff(list);
853 for (i = 0; i < n; ++i) {
854 isl_pw_aff *pa;
855 pa = isl_pw_aff_list_get_pw_aff(list, i);
856 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
857 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
860 isl_pw_aff_free(one);
862 return list;
865 /* Set the condition part of the for node graft->node in case
866 * the upper bound is represented as a list of piecewise affine expressions.
868 * In particular, set the condition to
870 * iterator <= min(list of upper bounds)
872 * If each of the upper bounds has a negative constant term, then
873 * set the condition to
875 * iterator < min(list of (upper bound + 1)s)
878 static __isl_give isl_ast_graft *set_for_cond_from_list(
879 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
880 __isl_keep isl_ast_build *build)
882 int neg;
883 isl_ast_expr *bound, *iterator, *cond;
884 enum isl_ast_op_type type = isl_ast_op_le;
886 if (!graft || !list)
887 return isl_ast_graft_free(graft);
889 neg = list_constant_is_negative(list);
890 if (neg < 0)
891 return isl_ast_graft_free(graft);
892 list = isl_pw_aff_list_copy(list);
893 if (neg) {
894 list = list_add_one(list, build);
895 type = isl_ast_op_lt;
898 bound = reduce_list(isl_ast_op_min, list, build);
899 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
900 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
901 graft->node->u.f.cond = cond;
903 isl_pw_aff_list_free(list);
904 if (!graft->node->u.f.cond)
905 return isl_ast_graft_free(graft);
906 return graft;
909 /* Set the condition part of the for node graft->node in case
910 * the upper bound is represented as a set.
912 static __isl_give isl_ast_graft *set_for_cond_from_set(
913 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
914 __isl_keep isl_ast_build *build)
916 isl_ast_expr *cond;
918 if (!graft)
919 return NULL;
921 cond = isl_ast_build_expr_from_set(build, isl_set_copy(set));
922 graft->node->u.f.cond = cond;
923 if (!graft->node->u.f.cond)
924 return isl_ast_graft_free(graft);
925 return graft;
928 /* Construct an isl_ast_expr for the increment (i.e., stride) of
929 * the current dimension.
931 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
933 int depth;
934 isl_val *v;
935 isl_ctx *ctx;
937 if (!build)
938 return NULL;
939 ctx = isl_ast_build_get_ctx(build);
940 depth = isl_ast_build_get_depth(build);
942 if (!isl_ast_build_has_stride(build, depth))
943 return isl_ast_expr_alloc_int_si(ctx, 1);
945 v = isl_ast_build_get_stride(build, depth);
946 return isl_ast_expr_from_val(v);
949 /* Should we express the loop condition as
951 * iterator <= min(list of upper bounds)
953 * or as a conjunction of constraints?
955 * The first is constructed from a list of upper bounds.
956 * The second is constructed from a set.
958 * If there are no upper bounds in "constraints", then this could mean
959 * that "domain" simply doesn't have an upper bound or that we didn't
960 * pick any upper bound. In the first case, we want to generate the
961 * loop condition as a(n empty) conjunction of constraints
962 * In the second case, we will compute
963 * a single upper bound from "domain" and so we use the list form.
965 * If there are upper bounds in "constraints",
966 * then we use the list form iff the atomic_upper_bound option is set.
968 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
969 __isl_keep isl_set *domain, int depth)
971 if (n_upper > 0)
972 return isl_options_get_ast_build_atomic_upper_bound(ctx);
973 else
974 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
977 /* Fill in the expressions of the for node in graft->node.
979 * In particular,
980 * - set the initialization part of the loop to the maximum of the lower bounds
981 * - set the size of the iterator based on the values attained by the iterator
982 * - extract the increment from the stride of the current dimension
983 * - construct the for condition either based on a list of upper bounds
984 * or on a set of upper bound constraints.
986 static __isl_give isl_ast_graft *set_for_node_expressions(
987 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
988 int use_list, __isl_keep isl_pw_aff_list *upper_list,
989 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
991 isl_ast_node *node;
993 if (!graft)
994 return NULL;
996 build = isl_ast_build_copy(build);
997 build = isl_ast_build_set_enforced(build,
998 isl_ast_graft_get_enforced(graft));
1000 node = graft->node;
1001 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1002 node->u.f.inc = for_inc(build);
1004 if (use_list)
1005 graft = set_for_cond_from_list(graft, upper_list, build);
1006 else
1007 graft = set_for_cond_from_set(graft, upper_set, build);
1009 isl_ast_build_free(build);
1011 if (!node->u.f.iterator || !node->u.f.init ||
1012 !node->u.f.cond || !node->u.f.inc)
1013 return isl_ast_graft_free(graft);
1015 return graft;
1018 /* Update "graft" based on "bounds" and "domain" for the generic,
1019 * non-degenerate, case.
1021 * "c_lower" and "c_upper" contain the lower and upper bounds
1022 * that the loop node should express.
1023 * "domain" is the subset of the intersection of the constraints
1024 * for which some code is executed.
1026 * There may be zero lower bounds or zero upper bounds in "constraints"
1027 * in case the list of constraints was created
1028 * based on the atomic option or based on separation with explicit bounds.
1029 * In that case, we use "domain" to derive lower and/or upper bounds.
1031 * We first compute a list of one or more lower bounds.
1033 * Then we decide if we want to express the condition as
1035 * iterator <= min(list of upper bounds)
1037 * or as a conjunction of constraints.
1039 * The set of enforced constraints is then computed either based on
1040 * a list of upper bounds or on a set of upper bound constraints.
1041 * We do not compute any enforced constraints if we were forced
1042 * to compute a lower or upper bound using exact_bound. The domains
1043 * of the resulting expressions may imply some bounds on outer dimensions
1044 * that we do not want to appear in the enforced constraints since
1045 * they are not actually enforced by the corresponding code.
1047 * Finally, we fill in the expressions of the for node.
1049 static __isl_give isl_ast_graft *refine_generic_bounds(
1050 __isl_take isl_ast_graft *graft,
1051 __isl_take isl_constraint_list *c_lower,
1052 __isl_take isl_constraint_list *c_upper,
1053 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1055 int depth;
1056 isl_ctx *ctx;
1057 isl_pw_aff_list *lower;
1058 int use_list;
1059 isl_set *upper_set = NULL;
1060 isl_pw_aff_list *upper_list = NULL;
1061 int n_lower, n_upper;
1063 if (!graft || !c_lower || !c_upper || !build)
1064 goto error;
1066 depth = isl_ast_build_get_depth(build);
1067 ctx = isl_ast_graft_get_ctx(graft);
1069 n_lower = isl_constraint_list_n_constraint(c_lower);
1070 n_upper = isl_constraint_list_n_constraint(c_upper);
1072 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1074 lower = lower_bounds(c_lower, depth, domain, build);
1076 if (use_list)
1077 upper_list = upper_bounds(c_upper, depth, domain, build);
1078 else if (n_upper > 0)
1079 upper_set = intersect_constraints(c_upper);
1080 else
1081 upper_set = isl_set_universe(isl_set_get_space(domain));
1083 if (n_lower == 0 || n_upper == 0)
1085 else if (use_list)
1086 graft = set_enforced_from_list(graft, lower, upper_list);
1087 else
1088 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1090 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1091 upper_set, build);
1093 isl_pw_aff_list_free(lower);
1094 isl_pw_aff_list_free(upper_list);
1095 isl_set_free(upper_set);
1096 isl_constraint_list_free(c_lower);
1097 isl_constraint_list_free(c_upper);
1099 return graft;
1100 error:
1101 isl_constraint_list_free(c_lower);
1102 isl_constraint_list_free(c_upper);
1103 return isl_ast_graft_free(graft);
1106 /* Internal data structure used inside count_constraints to keep
1107 * track of the number of constraints that are independent of dimension "pos",
1108 * the lower bounds in "pos" and the upper bounds in "pos".
1110 struct isl_ast_count_constraints_data {
1111 int pos;
1113 int n_indep;
1114 int n_lower;
1115 int n_upper;
1118 /* Increment data->n_indep, data->lower or data->upper depending
1119 * on whether "c" is independenct of dimensions data->pos,
1120 * a lower bound or an upper bound.
1122 static int count_constraints(__isl_take isl_constraint *c, void *user)
1124 struct isl_ast_count_constraints_data *data = user;
1126 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1127 data->n_lower++;
1128 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1129 data->n_upper++;
1130 else
1131 data->n_indep++;
1133 isl_constraint_free(c);
1135 return 0;
1138 /* Update "graft" based on "bounds" and "domain" for the generic,
1139 * non-degenerate, case.
1141 * "list" respresent the list of bounds that need to be encoded by
1142 * the for loop (or a guard around the for loop).
1143 * "domain" is the subset of the intersection of the constraints
1144 * for which some code is executed.
1145 * "build" is the build in which graft->node was created.
1147 * We separate lower bounds, upper bounds and constraints that
1148 * are independent of the loop iterator.
1150 * The actual for loop bounds are generated in refine_generic_bounds.
1151 * If there are any constraints that are independent of the loop iterator,
1152 * we need to put a guard around the for loop (which may get hoisted up
1153 * to higher levels) and we call refine_generic_bounds in a build
1154 * where this guard is enforced.
1156 static __isl_give isl_ast_graft *refine_generic_split(
1157 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1158 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1160 isl_ast_build *for_build;
1161 isl_set *guard;
1162 struct isl_ast_count_constraints_data data;
1163 isl_constraint_list *lower;
1164 isl_constraint_list *upper;
1166 if (!list)
1167 return isl_ast_graft_free(graft);
1169 data.pos = isl_ast_build_get_depth(build);
1171 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1172 if (!list)
1173 return isl_ast_graft_free(graft);
1175 data.n_indep = data.n_lower = data.n_upper = 0;
1176 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1177 isl_constraint_list_free(list);
1178 return isl_ast_graft_free(graft);
1181 lower = isl_constraint_list_copy(list);
1182 lower = isl_constraint_list_drop(lower, 0, data.n_indep);
1183 upper = isl_constraint_list_copy(lower);
1184 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1185 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1187 if (data.n_indep == 0) {
1188 isl_constraint_list_free(list);
1189 return refine_generic_bounds(graft, lower, upper,
1190 domain, build);
1193 list = isl_constraint_list_drop(list, data.n_indep,
1194 data.n_lower + data.n_upper);
1195 guard = intersect_constraints(list);
1196 isl_constraint_list_free(list);
1198 for_build = isl_ast_build_copy(build);
1199 for_build = isl_ast_build_restrict_pending(for_build,
1200 isl_set_copy(guard));
1201 graft = refine_generic_bounds(graft, lower, upper, domain, for_build);
1202 isl_ast_build_free(for_build);
1204 graft = isl_ast_graft_add_guard(graft, guard, build);
1206 return graft;
1209 /* Add the guard implied by the current stride constraint (if any),
1210 * but not (necessarily) enforced by the generated AST to "graft".
1212 static __isl_give isl_ast_graft *add_stride_guard(
1213 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
1215 int depth;
1216 isl_set *dom;
1218 depth = isl_ast_build_get_depth(build);
1219 if (!isl_ast_build_has_stride(build, depth))
1220 return graft;
1222 dom = isl_ast_build_get_stride_constraint(build);
1223 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
1224 dom = isl_ast_build_compute_gist(build, dom);
1226 graft = isl_ast_graft_add_guard(graft, dom, build);
1228 return graft;
1231 /* Update "graft" based on "bounds" and "domain" for the generic,
1232 * non-degenerate, case.
1234 * "bounds" respresent the bounds that need to be encoded by
1235 * the for loop (or a guard around the for loop).
1236 * "domain" is the subset of "bounds" for which some code is executed.
1237 * "build" is the build in which graft->node was created.
1239 * We break up "bounds" into a list of constraints and continue with
1240 * refine_generic_split.
1242 static __isl_give isl_ast_graft *refine_generic(
1243 __isl_take isl_ast_graft *graft,
1244 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1245 __isl_keep isl_ast_build *build)
1247 isl_constraint_list *list;
1249 if (!build || !graft)
1250 return isl_ast_graft_free(graft);
1252 bounds = isl_basic_set_copy(bounds);
1253 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1254 list = isl_constraint_list_from_basic_set(bounds);
1256 graft = refine_generic_split(graft, list, domain, build);
1257 graft = add_stride_guard(graft, build);
1259 return graft;
1262 /* Create a for node for the current level.
1264 * Mark the for node degenerate if "degenerate" is set.
1266 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1267 int degenerate)
1269 int depth;
1270 isl_id *id;
1271 isl_ast_node *node;
1273 if (!build)
1274 return NULL;
1276 depth = isl_ast_build_get_depth(build);
1277 id = isl_ast_build_get_iterator_id(build, depth);
1278 node = isl_ast_node_alloc_for(id);
1279 if (degenerate)
1280 node = isl_ast_node_for_mark_degenerate(node);
1282 return node;
1285 /* Create an AST node for the current dimension based on
1286 * the schedule domain "bounds" and return the node encapsulated
1287 * in an isl_ast_graft.
1289 * "executed" is the current inverse schedule, taking into account
1290 * the bounds in "bounds"
1291 * "domain" is the domain of "executed", with inner dimensions projected out.
1292 * It may be a strict subset of "bounds" in case "bounds" was created
1293 * based on the atomic option or based on separation with explicit bounds.
1295 * "domain" may satisfy additional equalities that result
1296 * from intersecting "executed" with "bounds" in add_node.
1297 * It may also satisfy some global constraints that were dropped out because
1298 * we performed separation with explicit bounds.
1299 * The very first step is then to copy these constraints to "bounds".
1301 * Since we may be calling before_each_for and after_each_for
1302 * callbacks, we record the current inverse schedule in the build.
1304 * We consider three builds,
1305 * "build" is the one in which the current level is created,
1306 * "body_build" is the build in which the next level is created,
1307 * "sub_build" is essentially the same as "body_build", except that
1308 * the depth has not been increased yet.
1310 * "build" already contains information (in strides and offsets)
1311 * about the strides at the current level, but this information is not
1312 * reflected in the build->domain.
1313 * We first add this information and the "bounds" to the sub_build->domain.
1314 * isl_ast_build_set_loop_bounds checks whether the current dimension attains
1315 * only a single value and whether this single value can be represented using
1316 * a single affine expression.
1317 * In the first case, the current level is considered "degenerate".
1318 * In the second, sub-case, the current level is considered "eliminated".
1319 * Eliminated level don't need to be reflected in the AST since we can
1320 * simply plug in the affine expression. For degenerate, but non-eliminated,
1321 * levels, we do introduce a for node, but mark is as degenerate so that
1322 * it can be printed as an assignment of the single value to the loop
1323 * "iterator".
1325 * If the current level is eliminated, we explicitly plug in the value
1326 * for the current level found by isl_ast_build_set_loop_bounds in the
1327 * inverse schedule. This ensures that if we are working on a slice
1328 * of the domain based on information available in the inverse schedule
1329 * and the build domain, that then this information is also reflected
1330 * in the inverse schedule. This operation also eliminates the current
1331 * dimension from the inverse schedule making sure no inner dimensions depend
1332 * on the current dimension. Otherwise, we create a for node, marking
1333 * it degenerate if appropriate. The initial for node is still incomplete
1334 * and will be completed in either refine_degenerate or refine_generic.
1336 * We then generate a sequence of grafts for the next level,
1337 * create a surrounding graft for the current level and insert
1338 * the for node we created (if the current level is not eliminated).
1340 * Finally, we set the bounds of the for loop and insert guards
1341 * (either in the AST or in the graft) in one of
1342 * refine_eliminated, refine_degenerate or refine_generic.
1344 static __isl_give isl_ast_graft *create_node_scaled(
1345 __isl_take isl_union_map *executed,
1346 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1347 __isl_take isl_ast_build *build)
1349 int depth;
1350 int degenerate, eliminated;
1351 isl_basic_set *hull;
1352 isl_ast_node *node = NULL;
1353 isl_ast_graft *graft;
1354 isl_ast_graft_list *children;
1355 isl_ast_build *sub_build;
1356 isl_ast_build *body_build;
1358 domain = isl_ast_build_eliminate_divs(build, domain);
1359 domain = isl_set_detect_equalities(domain);
1360 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1361 bounds = isl_basic_set_intersect(bounds, hull);
1362 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1364 depth = isl_ast_build_get_depth(build);
1365 sub_build = isl_ast_build_copy(build);
1366 sub_build = isl_ast_build_include_stride(sub_build);
1367 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1368 isl_basic_set_copy(bounds));
1369 degenerate = isl_ast_build_has_value(sub_build);
1370 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1371 if (degenerate < 0 || eliminated < 0)
1372 executed = isl_union_map_free(executed);
1373 if (eliminated)
1374 executed = plug_in_values(executed, sub_build);
1375 else
1376 node = create_for(build, degenerate);
1378 body_build = isl_ast_build_copy(sub_build);
1379 body_build = isl_ast_build_increase_depth(body_build);
1380 if (!eliminated)
1381 node = before_each_for(node, body_build);
1382 children = generate_next_level(executed,
1383 isl_ast_build_copy(body_build));
1385 graft = isl_ast_graft_alloc_level(children, build, sub_build);
1386 if (!eliminated)
1387 graft = isl_ast_graft_insert_for(graft, node);
1388 if (eliminated)
1389 graft = refine_eliminated(graft, bounds, build);
1390 else if (degenerate)
1391 graft = refine_degenerate(graft, bounds, build, sub_build);
1392 else
1393 graft = refine_generic(graft, bounds, domain, build);
1394 if (!eliminated)
1395 graft = after_each_for(graft, body_build);
1397 isl_ast_build_free(body_build);
1398 isl_ast_build_free(sub_build);
1399 isl_ast_build_free(build);
1400 isl_basic_set_free(bounds);
1401 isl_set_free(domain);
1403 return graft;
1406 /* Internal data structure for checking if all constraints involving
1407 * the input dimension "depth" are such that the other coefficients
1408 * are multiples of "m", reducing "m" if they are not.
1409 * If "m" is reduced all the way down to "1", then the check has failed
1410 * and we break out of the iteration.
1412 struct isl_check_scaled_data {
1413 int depth;
1414 isl_val *m;
1417 /* If constraint "c" involves the input dimension data->depth,
1418 * then make sure that all the other coefficients are multiples of data->m,
1419 * reducing data->m if needed.
1420 * Break out of the iteration if data->m has become equal to "1".
1422 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1424 struct isl_check_scaled_data *data = user;
1425 int i, j, n;
1426 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1427 isl_dim_div };
1429 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1430 isl_constraint_free(c);
1431 return 0;
1434 for (i = 0; i < 4; ++i) {
1435 n = isl_constraint_dim(c, t[i]);
1436 for (j = 0; j < n; ++j) {
1437 isl_val *d;
1439 if (t[i] == isl_dim_in && j == data->depth)
1440 continue;
1441 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1442 continue;
1443 d = isl_constraint_get_coefficient_val(c, t[i], j);
1444 data->m = isl_val_gcd(data->m, d);
1445 if (isl_val_is_one(data->m))
1446 break;
1448 if (j < n)
1449 break;
1452 isl_constraint_free(c);
1454 return i < 4 ? -1 : 0;
1457 /* For each constraint of "bmap" that involves the input dimension data->depth,
1458 * make sure that all the other coefficients are multiples of data->m,
1459 * reducing data->m if needed.
1460 * Break out of the iteration if data->m has become equal to "1".
1462 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1464 int r;
1466 r = isl_basic_map_foreach_constraint(bmap,
1467 &constraint_check_scaled, user);
1468 isl_basic_map_free(bmap);
1470 return r;
1473 /* For each constraint of "map" that involves the input dimension data->depth,
1474 * make sure that all the other coefficients are multiples of data->m,
1475 * reducing data->m if needed.
1476 * Break out of the iteration if data->m has become equal to "1".
1478 static int map_check_scaled(__isl_take isl_map *map, void *user)
1480 int r;
1482 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1483 isl_map_free(map);
1485 return r;
1488 /* Create an AST node for the current dimension based on
1489 * the schedule domain "bounds" and return the node encapsulated
1490 * in an isl_ast_graft.
1492 * "executed" is the current inverse schedule, taking into account
1493 * the bounds in "bounds"
1494 * "domain" is the domain of "executed", with inner dimensions projected out.
1497 * Before moving on to the actual AST node construction in create_node_scaled,
1498 * we first check if the current dimension is strided and if we can scale
1499 * down this stride. Note that we only do this if the ast_build_scale_strides
1500 * option is set.
1502 * In particular, let the current dimension take on values
1504 * f + s a
1506 * with a an integer. We check if we can find an integer m that (obviouly)
1507 * divides both f and s.
1509 * If so, we check if the current dimension only appears in constraints
1510 * where the coefficients of the other variables are multiples of m.
1511 * We perform this extra check to avoid the risk of introducing
1512 * divisions by scaling down the current dimension.
1514 * If so, we scale the current dimension down by a factor of m.
1515 * That is, we plug in
1517 * i = m i' (1)
1519 * Note that in principle we could always scale down strided loops
1520 * by plugging in
1522 * i = f + s i'
1524 * but this may result in i' taking on larger values than the original i,
1525 * due to the shift by "f".
1526 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1528 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1529 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1530 __isl_take isl_ast_build *build)
1532 struct isl_check_scaled_data data;
1533 isl_ctx *ctx;
1534 isl_aff *offset;
1535 isl_val *d;
1537 ctx = isl_ast_build_get_ctx(build);
1538 if (!isl_options_get_ast_build_scale_strides(ctx))
1539 return create_node_scaled(executed, bounds, domain, build);
1541 data.depth = isl_ast_build_get_depth(build);
1542 if (!isl_ast_build_has_stride(build, data.depth))
1543 return create_node_scaled(executed, bounds, domain, build);
1545 offset = isl_ast_build_get_offset(build, data.depth);
1546 data.m = isl_ast_build_get_stride(build, data.depth);
1547 if (!data.m)
1548 offset = isl_aff_free(offset);
1549 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1550 d = isl_aff_get_denominator_val(offset);
1551 if (!d)
1552 executed = isl_union_map_free(executed);
1554 if (executed && isl_val_is_divisible_by(data.m, d))
1555 data.m = isl_val_div(data.m, d);
1556 else {
1557 data.m = isl_val_set_si(data.m, 1);
1558 isl_val_free(d);
1561 if (!isl_val_is_one(data.m)) {
1562 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1563 &data) < 0 &&
1564 !isl_val_is_one(data.m))
1565 executed = isl_union_map_free(executed);
1568 if (!isl_val_is_one(data.m)) {
1569 isl_space *space;
1570 isl_multi_aff *ma;
1571 isl_aff *aff;
1572 isl_map *map;
1573 isl_union_map *umap;
1575 space = isl_ast_build_get_space(build, 1);
1576 space = isl_space_map_from_set(space);
1577 ma = isl_multi_aff_identity(space);
1578 aff = isl_multi_aff_get_aff(ma, data.depth);
1579 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1580 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1582 bounds = isl_basic_set_preimage_multi_aff(bounds,
1583 isl_multi_aff_copy(ma));
1584 domain = isl_set_preimage_multi_aff(domain,
1585 isl_multi_aff_copy(ma));
1586 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1587 umap = isl_union_map_from_map(map);
1588 executed = isl_union_map_apply_domain(executed,
1589 isl_union_map_copy(umap));
1590 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1591 umap);
1593 isl_aff_free(offset);
1594 isl_val_free(data.m);
1596 return create_node_scaled(executed, bounds, domain, build);
1599 /* Add the basic set to the list that "user" points to.
1601 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1603 isl_basic_set_list **list = user;
1605 *list = isl_basic_set_list_add(*list, bset);
1607 return 0;
1610 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1612 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1613 __isl_take isl_set *set)
1615 int n;
1616 isl_ctx *ctx;
1617 isl_basic_set_list *list;
1619 if (!set)
1620 return NULL;
1622 ctx = isl_set_get_ctx(set);
1624 n = isl_set_n_basic_set(set);
1625 list = isl_basic_set_list_alloc(ctx, n);
1626 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1627 list = isl_basic_set_list_free(list);
1629 isl_set_free(set);
1630 return list;
1633 /* Generate code for the schedule domain "bounds"
1634 * and add the result to "list".
1636 * We mainly detect strides and additional equalities here
1637 * and then pass over control to create_node.
1639 * "bounds" reflects the bounds on the current dimension and possibly
1640 * some extra conditions on outer dimensions.
1641 * It does not, however, include any divs involving the current dimension,
1642 * so it does not capture any stride constraints.
1643 * We therefore need to compute that part of the schedule domain that
1644 * intersects with "bounds" and derive the strides from the result.
1646 static __isl_give isl_ast_graft_list *add_node(
1647 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1648 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1650 isl_ast_graft *graft;
1651 isl_set *domain = NULL;
1652 isl_union_set *uset;
1653 int empty;
1655 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1656 executed = isl_union_map_intersect_domain(executed, uset);
1657 empty = isl_union_map_is_empty(executed);
1658 if (empty < 0)
1659 goto error;
1660 if (empty)
1661 goto done;
1663 uset = isl_union_map_domain(isl_union_map_copy(executed));
1664 domain = isl_set_from_union_set(uset);
1665 domain = isl_ast_build_compute_gist(build, domain);
1666 empty = isl_set_is_empty(domain);
1667 if (empty < 0)
1668 goto error;
1669 if (empty)
1670 goto done;
1672 domain = isl_ast_build_eliminate_inner(build, domain);
1673 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1675 graft = create_node(executed, bounds, domain,
1676 isl_ast_build_copy(build));
1677 list = isl_ast_graft_list_add(list, graft);
1678 isl_ast_build_free(build);
1679 return list;
1680 error:
1681 list = isl_ast_graft_list_free(list);
1682 done:
1683 isl_set_free(domain);
1684 isl_basic_set_free(bounds);
1685 isl_union_map_free(executed);
1686 isl_ast_build_free(build);
1687 return list;
1690 /* Does any element of i follow or coincide with any element of j
1691 * at the current depth for equal values of the outer dimensions?
1693 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1694 __isl_keep isl_basic_set *j, void *user)
1696 int depth = *(int *) user;
1697 isl_basic_map *test;
1698 int empty;
1699 int l;
1701 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1702 isl_basic_set_copy(j));
1703 for (l = 0; l < depth; ++l)
1704 test = isl_basic_map_equate(test, isl_dim_in, l,
1705 isl_dim_out, l);
1706 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1707 isl_dim_out, depth);
1708 empty = isl_basic_map_is_empty(test);
1709 isl_basic_map_free(test);
1711 return empty < 0 ? -1 : !empty;
1714 /* Split up each element of "list" into a part that is related to "bset"
1715 * according to "gt" and a part that is not.
1716 * Return a list that consist of "bset" and all the pieces.
1718 static __isl_give isl_basic_set_list *add_split_on(
1719 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1720 __isl_keep isl_basic_map *gt)
1722 int i, n;
1723 isl_basic_set_list *res;
1725 if (!list)
1726 bset = isl_basic_set_free(bset);
1728 gt = isl_basic_map_copy(gt);
1729 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1730 n = isl_basic_set_list_n_basic_set(list);
1731 res = isl_basic_set_list_from_basic_set(bset);
1732 for (i = 0; res && i < n; ++i) {
1733 isl_basic_set *bset;
1734 isl_set *set1, *set2;
1735 isl_basic_map *bmap;
1736 int empty;
1738 bset = isl_basic_set_list_get_basic_set(list, i);
1739 bmap = isl_basic_map_copy(gt);
1740 bmap = isl_basic_map_intersect_range(bmap, bset);
1741 bset = isl_basic_map_range(bmap);
1742 empty = isl_basic_set_is_empty(bset);
1743 if (empty < 0)
1744 res = isl_basic_set_list_free(res);
1745 if (empty) {
1746 isl_basic_set_free(bset);
1747 bset = isl_basic_set_list_get_basic_set(list, i);
1748 res = isl_basic_set_list_add(res, bset);
1749 continue;
1752 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1753 set1 = isl_set_from_basic_set(bset);
1754 bset = isl_basic_set_list_get_basic_set(list, i);
1755 set2 = isl_set_from_basic_set(bset);
1756 set1 = isl_set_subtract(set2, set1);
1757 set1 = isl_set_make_disjoint(set1);
1759 res = isl_basic_set_list_concat(res,
1760 isl_basic_set_list_from_set(set1));
1762 isl_basic_map_free(gt);
1763 isl_basic_set_list_free(list);
1764 return res;
1767 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1768 __isl_keep isl_basic_set_list *domain_list,
1769 __isl_keep isl_union_map *executed,
1770 __isl_keep isl_ast_build *build);
1772 /* Internal data structure for add_nodes.
1774 * "executed" and "build" are extra arguments to be passed to add_node.
1775 * "list" collects the results.
1777 struct isl_add_nodes_data {
1778 isl_union_map *executed;
1779 isl_ast_build *build;
1781 isl_ast_graft_list *list;
1784 /* Generate code for the schedule domains in "scc"
1785 * and add the results to "list".
1787 * The domains in "scc" form a strongly connected component in the ordering.
1788 * If the number of domains in "scc" is larger than 1, then this means
1789 * that we cannot determine a valid ordering for the domains in the component.
1790 * This should be fairly rare because the individual domains
1791 * have been made disjoint first.
1792 * The problem is that the domains may be integrally disjoint but not
1793 * rationally disjoint. For example, we may have domains
1795 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1797 * These two domains have an empty intersection, but their rational
1798 * relaxations do intersect. It is impossible to order these domains
1799 * in the second dimension because the first should be ordered before
1800 * the second for outer dimension equal to 0, while it should be ordered
1801 * after for outer dimension equal to 1.
1803 * This may happen in particular in case of unrolling since the domain
1804 * of each slice is replaced by its simple hull.
1806 * For each basic set i in "scc" and for each of the following basic sets j,
1807 * we split off that part of the basic set i that shares the outer dimensions
1808 * with j and lies before j in the current dimension.
1809 * We collect all the pieces in a new list that replaces "scc".
1811 * While the elements in "scc" should be disjoint, we double-check
1812 * this property to avoid running into an infinite recursion in case
1813 * they intersect due to some internal error.
1815 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1817 struct isl_add_nodes_data *data = user;
1818 int i, n, depth;
1819 isl_basic_set *bset, *first;
1820 isl_basic_set_list *list;
1821 isl_space *space;
1822 isl_basic_map *gt;
1824 n = isl_basic_set_list_n_basic_set(scc);
1825 bset = isl_basic_set_list_get_basic_set(scc, 0);
1826 if (n == 1) {
1827 isl_basic_set_list_free(scc);
1828 data->list = add_node(data->list,
1829 isl_union_map_copy(data->executed), bset,
1830 isl_ast_build_copy(data->build));
1831 return data->list ? 0 : -1;
1834 depth = isl_ast_build_get_depth(data->build);
1835 space = isl_basic_set_get_space(bset);
1836 space = isl_space_map_from_set(space);
1837 gt = isl_basic_map_universe(space);
1838 for (i = 0; i < depth; ++i)
1839 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1840 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1842 first = isl_basic_set_copy(bset);
1843 list = isl_basic_set_list_from_basic_set(bset);
1844 for (i = 1; i < n; ++i) {
1845 int disjoint;
1847 bset = isl_basic_set_list_get_basic_set(scc, i);
1849 disjoint = isl_basic_set_is_disjoint(bset, first);
1850 if (disjoint < 0)
1851 list = isl_basic_set_list_free(list);
1852 else if (!disjoint)
1853 isl_die(isl_basic_set_list_get_ctx(scc),
1854 isl_error_internal,
1855 "basic sets in scc are assumed to be disjoint",
1856 list = isl_basic_set_list_free(list));
1858 list = add_split_on(list, bset, gt);
1860 isl_basic_set_free(first);
1861 isl_basic_map_free(gt);
1862 isl_basic_set_list_free(scc);
1863 scc = list;
1864 data->list = isl_ast_graft_list_concat(data->list,
1865 generate_sorted_domains(scc, data->executed, data->build));
1866 isl_basic_set_list_free(scc);
1868 return data->list ? 0 : -1;
1871 /* Sort the domains in "domain_list" according to the execution order
1872 * at the current depth (for equal values of the outer dimensions),
1873 * generate code for each of them, collecting the results in a list.
1874 * If no code is generated (because the intersection of the inverse schedule
1875 * with the domains turns out to be empty), then an empty list is returned.
1877 * The caller is responsible for ensuring that the basic sets in "domain_list"
1878 * are pair-wise disjoint. It can, however, in principle happen that
1879 * two basic sets should be ordered one way for one value of the outer
1880 * dimensions and the other way for some other value of the outer dimensions.
1881 * We therefore play safe and look for strongly connected components.
1882 * The function add_nodes takes care of handling non-trivial components.
1884 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1885 __isl_keep isl_basic_set_list *domain_list,
1886 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1888 isl_ctx *ctx;
1889 struct isl_add_nodes_data data;
1890 int depth;
1891 int n;
1893 if (!domain_list)
1894 return NULL;
1896 ctx = isl_basic_set_list_get_ctx(domain_list);
1897 n = isl_basic_set_list_n_basic_set(domain_list);
1898 data.list = isl_ast_graft_list_alloc(ctx, n);
1899 if (n == 0)
1900 return data.list;
1901 if (n == 1)
1902 return add_node(data.list, isl_union_map_copy(executed),
1903 isl_basic_set_list_get_basic_set(domain_list, 0),
1904 isl_ast_build_copy(build));
1906 depth = isl_ast_build_get_depth(build);
1907 data.executed = executed;
1908 data.build = build;
1909 if (isl_basic_set_list_foreach_scc(domain_list,
1910 &domain_follows_at_depth, &depth,
1911 &add_nodes, &data) < 0)
1912 data.list = isl_ast_graft_list_free(data.list);
1914 return data.list;
1917 /* Do i and j share any values for the outer dimensions?
1919 static int shared_outer(__isl_keep isl_basic_set *i,
1920 __isl_keep isl_basic_set *j, void *user)
1922 int depth = *(int *) user;
1923 isl_basic_map *test;
1924 int empty;
1925 int l;
1927 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1928 isl_basic_set_copy(j));
1929 for (l = 0; l < depth; ++l)
1930 test = isl_basic_map_equate(test, isl_dim_in, l,
1931 isl_dim_out, l);
1932 empty = isl_basic_map_is_empty(test);
1933 isl_basic_map_free(test);
1935 return empty < 0 ? -1 : !empty;
1938 /* Internal data structure for generate_sorted_domains_wrap.
1940 * "n" is the total number of basic sets
1941 * "executed" and "build" are extra arguments to be passed
1942 * to generate_sorted_domains.
1944 * "single" is set to 1 by generate_sorted_domains_wrap if there
1945 * is only a single component.
1946 * "list" collects the results.
1948 struct isl_ast_generate_parallel_domains_data {
1949 int n;
1950 isl_union_map *executed;
1951 isl_ast_build *build;
1953 int single;
1954 isl_ast_graft_list *list;
1957 /* Call generate_sorted_domains on "scc", fuse the result into a list
1958 * with either zero or one graft and collect the these single element
1959 * lists into data->list.
1961 * If there is only one component, i.e., if the number of basic sets
1962 * in the current component is equal to the total number of basic sets,
1963 * then data->single is set to 1 and the result of generate_sorted_domains
1964 * is not fused.
1966 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
1967 void *user)
1969 struct isl_ast_generate_parallel_domains_data *data = user;
1970 isl_ast_graft_list *list;
1972 list = generate_sorted_domains(scc, data->executed, data->build);
1973 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
1974 if (!data->single)
1975 list = isl_ast_graft_list_fuse(list, data->build);
1976 if (!data->list)
1977 data->list = list;
1978 else
1979 data->list = isl_ast_graft_list_concat(data->list, list);
1981 isl_basic_set_list_free(scc);
1982 if (!data->list)
1983 return -1;
1985 return 0;
1988 /* Look for any (weakly connected) components in the "domain_list"
1989 * of domains that share some values of the outer dimensions.
1990 * That is, domains in different components do not share any values
1991 * of the outer dimensions. This means that these components
1992 * can be freely reordered.
1993 * Within each of the components, we sort the domains according
1994 * to the execution order at the current depth.
1996 * If there is more than one component, then generate_sorted_domains_wrap
1997 * fuses the result of each call to generate_sorted_domains
1998 * into a list with either zero or one graft and collects these (at most)
1999 * single element lists into a bigger list. This means that the elements of the
2000 * final list can be freely reordered. In particular, we sort them
2001 * according to an arbitrary but fixed ordering to ease merging of
2002 * graft lists from different components.
2004 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2005 __isl_keep isl_basic_set_list *domain_list,
2006 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2008 int depth;
2009 struct isl_ast_generate_parallel_domains_data data;
2011 if (!domain_list)
2012 return NULL;
2014 data.n = isl_basic_set_list_n_basic_set(domain_list);
2015 if (data.n <= 1)
2016 return generate_sorted_domains(domain_list, executed, build);
2018 depth = isl_ast_build_get_depth(build);
2019 data.list = NULL;
2020 data.executed = executed;
2021 data.build = build;
2022 data.single = 0;
2023 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2024 &generate_sorted_domains_wrap,
2025 &data) < 0)
2026 data.list = isl_ast_graft_list_free(data.list);
2028 if (!data.single)
2029 data.list = isl_ast_graft_list_sort_guard(data.list);
2031 return data.list;
2034 /* Internal data for separate_domain.
2036 * "explicit" is set if we only want to use explicit bounds.
2038 * "domain" collects the separated domains.
2040 struct isl_separate_domain_data {
2041 isl_ast_build *build;
2042 int explicit;
2043 isl_set *domain;
2046 /* Extract implicit bounds on the current dimension for the executed "map".
2048 * The domain of "map" may involve inner dimensions, so we
2049 * need to eliminate them.
2051 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2052 __isl_keep isl_ast_build *build)
2054 isl_set *domain;
2056 domain = isl_map_domain(map);
2057 domain = isl_ast_build_eliminate(build, domain);
2059 return domain;
2062 /* Extract explicit bounds on the current dimension for the executed "map".
2064 * Rather than eliminating the inner dimensions as in implicit_bounds,
2065 * we simply drop any constraints involving those inner dimensions.
2066 * The idea is that most bounds that are implied by constraints on the
2067 * inner dimensions will be enforced by for loops and not by explicit guards.
2068 * There is then no need to separate along those bounds.
2070 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2071 __isl_keep isl_ast_build *build)
2073 isl_set *domain;
2074 int depth, dim;
2076 dim = isl_map_dim(map, isl_dim_out);
2077 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2079 domain = isl_map_domain(map);
2080 depth = isl_ast_build_get_depth(build);
2081 dim = isl_set_dim(domain, isl_dim_set);
2082 domain = isl_set_detect_equalities(domain);
2083 domain = isl_set_drop_constraints_involving_dims(domain,
2084 isl_dim_set, depth + 1, dim - (depth + 1));
2085 domain = isl_set_remove_divs_involving_dims(domain,
2086 isl_dim_set, depth, 1);
2087 domain = isl_set_remove_unknown_divs(domain);
2089 return domain;
2092 /* Split data->domain into pieces that intersect with the range of "map"
2093 * and pieces that do not intersect with the range of "map"
2094 * and then add that part of the range of "map" that does not intersect
2095 * with data->domain.
2097 static int separate_domain(__isl_take isl_map *map, void *user)
2099 struct isl_separate_domain_data *data = user;
2100 isl_set *domain;
2101 isl_set *d1, *d2;
2103 if (data->explicit)
2104 domain = explicit_bounds(map, data->build);
2105 else
2106 domain = implicit_bounds(map, data->build);
2108 domain = isl_set_coalesce(domain);
2109 domain = isl_set_make_disjoint(domain);
2110 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2111 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2112 data->domain = isl_set_intersect(data->domain, domain);
2113 data->domain = isl_set_union(data->domain, d1);
2114 data->domain = isl_set_union(data->domain, d2);
2116 return 0;
2119 /* Separate the schedule domains of "executed".
2121 * That is, break up the domain of "executed" into basic sets,
2122 * such that for each basic set S, every element in S is associated with
2123 * the same domain spaces.
2125 * "space" is the (single) domain space of "executed".
2127 static __isl_give isl_set *separate_schedule_domains(
2128 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2129 __isl_keep isl_ast_build *build)
2131 struct isl_separate_domain_data data = { build };
2132 isl_ctx *ctx;
2134 ctx = isl_ast_build_get_ctx(build);
2135 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2136 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2137 data.domain = isl_set_empty(space);
2138 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2139 data.domain = isl_set_free(data.domain);
2141 isl_union_map_free(executed);
2142 return data.domain;
2145 /* Temporary data used during the search for a lower bound for unrolling.
2147 * "domain" is the original set for which to find a lower bound
2148 * "depth" is the dimension for which to find a lower boudn
2150 * "lower" is the best lower bound found so far. It is NULL if we have not
2151 * found any yet.
2152 * "n" is the corresponding size. If lower is NULL, then the value of n
2153 * is undefined.
2155 struct isl_find_unroll_data {
2156 isl_set *domain;
2157 int depth;
2159 isl_aff *lower;
2160 int *n;
2163 /* Check if we can use "c" as a lower bound and if it is better than
2164 * any previously found lower bound.
2166 * If "c" does not involve the dimension at the current depth,
2167 * then we cannot use it.
2168 * Otherwise, let "c" be of the form
2170 * i >= f(j)/a
2172 * We compute the maximal value of
2174 * -ceil(f(j)/a)) + i + 1
2176 * over the domain. If there is such a value "n", then we know
2178 * -ceil(f(j)/a)) + i + 1 <= n
2180 * or
2182 * i < ceil(f(j)/a)) + n
2184 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2185 * We just need to check if we have found any lower bound before and
2186 * if the new lower bound is better (smaller n) than the previously found
2187 * lower bounds.
2189 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2190 __isl_keep isl_constraint *c)
2192 isl_aff *aff, *lower;
2193 isl_val *max;
2195 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2196 return 0;
2198 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2199 lower = isl_aff_ceil(lower);
2200 aff = isl_aff_copy(lower);
2201 aff = isl_aff_neg(aff);
2202 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2203 aff = isl_aff_add_constant_si(aff, 1);
2204 max = isl_set_max_val(data->domain, aff);
2205 isl_aff_free(aff);
2207 if (!max)
2208 goto error;
2209 if (isl_val_is_infty(max)) {
2210 isl_val_free(max);
2211 isl_aff_free(lower);
2212 return 0;
2215 if (isl_val_cmp_si(max, INT_MAX) <= 0 &&
2216 (!data->lower || isl_val_cmp_si(max, *data->n) < 0)) {
2217 isl_aff_free(data->lower);
2218 data->lower = lower;
2219 *data->n = isl_val_get_num_si(max);
2220 } else
2221 isl_aff_free(lower);
2222 isl_val_free(max);
2224 return 1;
2225 error:
2226 isl_aff_free(lower);
2227 return -1;
2230 /* Check if we can use "c" as a lower bound and if it is better than
2231 * any previously found lower bound.
2233 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2235 struct isl_find_unroll_data *data;
2236 int r;
2238 data = (struct isl_find_unroll_data *) user;
2239 r = update_unrolling_lower_bound(data, c);
2240 isl_constraint_free(c);
2242 return r;
2245 /* Look for a lower bound l(i) on the dimension at "depth"
2246 * and a size n such that "domain" is a subset of
2248 * { [i] : l(i) <= i_d < l(i) + n }
2250 * where d is "depth" and l(i) depends only on earlier dimensions.
2251 * Furthermore, try and find a lower bound such that n is as small as possible.
2252 * In particular, "n" needs to be finite.
2254 * Inner dimensions have been eliminated from "domain" by the caller.
2256 * We first construct a collection of lower bounds on the input set
2257 * by computing its simple hull. We then iterate through them,
2258 * discarding those that we cannot use (either because they do not
2259 * involve the dimension at "depth" or because they have no corresponding
2260 * upper bound, meaning that "n" would be unbounded) and pick out the
2261 * best from the remaining ones.
2263 * If we cannot find a suitable lower bound, then we consider that
2264 * to be an error.
2266 static __isl_give isl_aff *find_unroll_lower_bound(__isl_keep isl_set *domain,
2267 int depth, int *n)
2269 struct isl_find_unroll_data data = { domain, depth, NULL, n };
2270 isl_basic_set *hull;
2272 hull = isl_set_simple_hull(isl_set_copy(domain));
2274 if (isl_basic_set_foreach_constraint(hull,
2275 &constraint_find_unroll, &data) < 0)
2276 goto error;
2278 isl_basic_set_free(hull);
2280 if (!data.lower)
2281 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2282 "cannot find lower bound for unrolling", return NULL);
2284 return data.lower;
2285 error:
2286 isl_basic_set_free(hull);
2287 return isl_aff_free(data.lower);
2290 /* Return the constraint
2292 * i_"depth" = aff + offset
2294 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2295 int offset)
2297 aff = isl_aff_copy(aff);
2298 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2299 aff = isl_aff_add_constant_si(aff, offset);
2300 return isl_equality_from_aff(aff);
2303 /* Data structure for storing the results and the intermediate objects
2304 * of compute_domains.
2306 * "list" is the main result of the function and contains a list
2307 * of disjoint basic sets for which code should be generated.
2309 * "executed" and "build" are inputs to compute_domains.
2310 * "schedule_domain" is the domain of "executed".
2312 * "option" constains the domains at the current depth that should by
2313 * atomic, separated or unrolled. These domains are as specified by
2314 * the user, except that inner dimensions have been eliminated and
2315 * that they have been made pair-wise disjoint.
2317 * "sep_class" contains the user-specified split into separation classes
2318 * specialized to the current depth.
2319 * "done" contains the union of the separation domains that have already
2320 * been handled.
2322 struct isl_codegen_domains {
2323 isl_basic_set_list *list;
2325 isl_union_map *executed;
2326 isl_ast_build *build;
2327 isl_set *schedule_domain;
2329 isl_set *option[3];
2331 isl_map *sep_class;
2332 isl_set *done;
2335 /* Extend domains->list with a list of basic sets, one for each value
2336 * of the current dimension in "domain" and remove the corresponding
2337 * sets from the class domain. Return the updated class domain.
2338 * The divs that involve the current dimension have not been projected out
2339 * from this domain.
2341 * Since we are going to be iterating over the individual values,
2342 * we first check if there are any strides on the current dimension.
2343 * If there is, we rewrite the current dimension i as
2345 * i = stride i' + offset
2347 * and then iterate over individual values of i' instead.
2349 * We then look for a lower bound on i' and a size such that the domain
2350 * is a subset of
2352 * { [j,i'] : l(j) <= i' < l(j) + n }
2354 * and then take slices of the domain at values of i'
2355 * between l(j) and l(j) + n - 1.
2357 * We compute the unshifted simple hull of each slice to ensure that
2358 * we have a single basic set per offset. The slicing constraint
2359 * may get simplified away before the unshifted simple hull is taken
2360 * and may therefore in some rare cases disappear from the result.
2361 * We therefore explicitly add the constraint back after computing
2362 * the unshifted simple hull to ensure that the basic sets
2363 * remain disjoint. The constraints that are dropped by taking the hull
2364 * will be taken into account at the next level, as in the case of the
2365 * atomic option.
2367 * Finally, we map i' back to i and add each basic set to the list.
2368 * Since we may have dropped some constraints, we intersect with
2369 * the class domain again to ensure that each element in the list
2370 * is disjoint from the other class domains.
2372 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2373 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2375 int i, n;
2376 int depth;
2377 isl_ctx *ctx;
2378 isl_aff *lower;
2379 isl_multi_aff *expansion;
2380 isl_basic_map *bmap;
2381 isl_set *unroll_domain;
2382 isl_ast_build *build;
2384 if (!domain)
2385 return isl_set_free(class_domain);
2387 ctx = isl_set_get_ctx(domain);
2388 depth = isl_ast_build_get_depth(domains->build);
2389 build = isl_ast_build_copy(domains->build);
2390 domain = isl_ast_build_eliminate_inner(build, domain);
2391 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
2392 expansion = isl_ast_build_get_stride_expansion(build);
2394 domain = isl_set_preimage_multi_aff(domain,
2395 isl_multi_aff_copy(expansion));
2396 domain = isl_ast_build_eliminate_divs(build, domain);
2398 isl_ast_build_free(build);
2400 lower = find_unroll_lower_bound(domain, depth, &n);
2401 if (!lower)
2402 class_domain = isl_set_free(class_domain);
2404 bmap = isl_basic_map_from_multi_aff(expansion);
2406 unroll_domain = isl_set_empty(isl_set_get_space(domain));
2408 for (i = 0; class_domain && i < n; ++i) {
2409 isl_set *set;
2410 isl_basic_set *bset;
2411 isl_constraint *slice;
2412 isl_basic_set_list *list;
2414 slice = at_offset(depth, lower, i);
2415 set = isl_set_copy(domain);
2416 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2417 bset = isl_set_unshifted_simple_hull(set);
2418 bset = isl_basic_set_add_constraint(bset, slice);
2419 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2420 set = isl_set_from_basic_set(bset);
2421 unroll_domain = isl_set_union(unroll_domain, isl_set_copy(set));
2422 set = isl_set_intersect(set, isl_set_copy(class_domain));
2423 set = isl_set_make_disjoint(set);
2424 list = isl_basic_set_list_from_set(set);
2425 domains->list = isl_basic_set_list_concat(domains->list, list);
2428 class_domain = isl_set_subtract(class_domain, unroll_domain);
2430 isl_aff_free(lower);
2431 isl_set_free(domain);
2432 isl_basic_map_free(bmap);
2434 return class_domain;
2437 /* Add domains to domains->list for each individual value of the current
2438 * dimension, for that part of the schedule domain that lies in the
2439 * intersection of the option domain and the class domain.
2440 * Remove the corresponding sets from the class domain and
2441 * return the updated class domain.
2443 * We first break up the unroll option domain into individual pieces
2444 * and then handle each of them separately. The unroll option domain
2445 * has been made disjoint in compute_domains_init_options,
2447 * Note that we actively want to combine different pieces of the
2448 * schedule domain that have the same value at the current dimension.
2449 * We therefore need to break up the unroll option domain before
2450 * intersecting with class and schedule domain, hoping that the
2451 * unroll option domain specified by the user is relatively simple.
2453 static __isl_give isl_set *compute_unroll_domains(
2454 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2456 isl_set *unroll_domain;
2457 isl_basic_set_list *unroll_list;
2458 int i, n;
2459 int empty;
2461 empty = isl_set_is_empty(domains->option[unroll]);
2462 if (empty < 0)
2463 return isl_set_free(class_domain);
2464 if (empty)
2465 return class_domain;
2467 unroll_domain = isl_set_copy(domains->option[unroll]);
2468 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2470 n = isl_basic_set_list_n_basic_set(unroll_list);
2471 for (i = 0; i < n; ++i) {
2472 isl_basic_set *bset;
2474 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2475 unroll_domain = isl_set_from_basic_set(bset);
2476 unroll_domain = isl_set_intersect(unroll_domain,
2477 isl_set_copy(class_domain));
2478 unroll_domain = isl_set_intersect(unroll_domain,
2479 isl_set_copy(domains->schedule_domain));
2481 empty = isl_set_is_empty(unroll_domain);
2482 if (empty >= 0 && empty) {
2483 isl_set_free(unroll_domain);
2484 continue;
2487 class_domain = do_unroll(domains, unroll_domain, class_domain);
2490 isl_basic_set_list_free(unroll_list);
2492 return class_domain;
2495 /* Try and construct a single basic set that includes the intersection of
2496 * the schedule domain, the atomic option domain and the class domain.
2497 * Add the resulting basic set(s) to domains->list and remove them
2498 * from class_domain. Return the updated class domain.
2500 * We construct a single domain rather than trying to combine
2501 * the schedule domains of individual domains because we are working
2502 * within a single component so that non-overlapping schedule domains
2503 * should already have been separated.
2504 * We do however need to make sure that this single domains is a subset
2505 * of the class domain so that it would not intersect with any other
2506 * class domains. This means that we may end up splitting up the atomic
2507 * domain in case separation classes are being used.
2509 * "domain" is the intersection of the schedule domain and the class domain,
2510 * with inner dimensions projected out.
2512 static __isl_give isl_set *compute_atomic_domain(
2513 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2515 isl_basic_set *bset;
2516 isl_basic_set_list *list;
2517 isl_set *domain, *atomic_domain;
2518 int empty;
2520 domain = isl_set_copy(domains->option[atomic]);
2521 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2522 domain = isl_set_intersect(domain,
2523 isl_set_copy(domains->schedule_domain));
2524 empty = isl_set_is_empty(domain);
2525 if (empty < 0)
2526 class_domain = isl_set_free(class_domain);
2527 if (empty) {
2528 isl_set_free(domain);
2529 return class_domain;
2532 domain = isl_ast_build_eliminate(domains->build, domain);
2533 domain = isl_set_coalesce(domain);
2534 bset = isl_set_unshifted_simple_hull(domain);
2535 domain = isl_set_from_basic_set(bset);
2536 atomic_domain = isl_set_copy(domain);
2537 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2538 class_domain = isl_set_subtract(class_domain, atomic_domain);
2539 domain = isl_set_make_disjoint(domain);
2540 list = isl_basic_set_list_from_set(domain);
2541 domains->list = isl_basic_set_list_concat(domains->list, list);
2543 return class_domain;
2546 /* Split up the schedule domain into uniform basic sets,
2547 * in the sense that each element in a basic set is associated to
2548 * elements of the same domains, and add the result to domains->list.
2549 * Do this for that part of the schedule domain that lies in the
2550 * intersection of "class_domain" and the separate option domain.
2552 * "class_domain" may or may not include the constraints
2553 * of the schedule domain, but this does not make a difference
2554 * since we are going to intersect it with the domain of the inverse schedule.
2555 * If it includes schedule domain constraints, then they may involve
2556 * inner dimensions, but we will eliminate them in separation_domain.
2558 static int compute_separate_domain(struct isl_codegen_domains *domains,
2559 __isl_keep isl_set *class_domain)
2561 isl_space *space;
2562 isl_set *domain;
2563 isl_union_map *executed;
2564 isl_basic_set_list *list;
2565 int empty;
2567 domain = isl_set_copy(domains->option[separate]);
2568 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2569 executed = isl_union_map_copy(domains->executed);
2570 executed = isl_union_map_intersect_domain(executed,
2571 isl_union_set_from_set(domain));
2572 empty = isl_union_map_is_empty(executed);
2573 if (empty < 0 || empty) {
2574 isl_union_map_free(executed);
2575 return empty < 0 ? -1 : 0;
2578 space = isl_set_get_space(class_domain);
2579 domain = separate_schedule_domains(space, executed, domains->build);
2581 list = isl_basic_set_list_from_set(domain);
2582 domains->list = isl_basic_set_list_concat(domains->list, list);
2584 return 0;
2587 /* Split up the domain at the current depth into disjoint
2588 * basic sets for which code should be generated separately
2589 * for the given separation class domain.
2591 * If any separation classes have been defined, then "class_domain"
2592 * is the domain of the current class and does not refer to inner dimensions.
2593 * Otherwise, "class_domain" is the universe domain.
2595 * We first make sure that the class domain is disjoint from
2596 * previously considered class domains.
2598 * The separate domains can be computed directly from the "class_domain".
2600 * The unroll, atomic and remainder domains need the constraints
2601 * from the schedule domain.
2603 * For unrolling, the actual schedule domain is needed (with divs that
2604 * may refer to the current dimension) so that stride detection can be
2605 * performed.
2607 * For atomic and remainder domains, inner dimensions and divs involving
2608 * the current dimensions should be eliminated.
2609 * In case we are working within a separation class, we need to intersect
2610 * the result with the current "class_domain" to ensure that the domains
2611 * are disjoint from those generated from other class domains.
2613 * The domain that has been made atomic may be larger than specified
2614 * by the user since it needs to be representable as a single basic set.
2615 * This possibly larger domain is removed from class_domain by
2616 * compute_atomic_domain. It is computed first so that the extended domain
2617 * would not overlap with any domains computed before.
2618 * Similary, the unrolled domains may have some constraints removed and
2619 * may therefore also be larger than specified by the user.
2621 * If anything is left after handling separate, unroll and atomic,
2622 * we split it up into basic sets and append the basic sets to domains->list.
2624 static int compute_partial_domains(struct isl_codegen_domains *domains,
2625 __isl_take isl_set *class_domain)
2627 isl_basic_set_list *list;
2628 isl_set *domain;
2630 class_domain = isl_set_subtract(class_domain,
2631 isl_set_copy(domains->done));
2632 domains->done = isl_set_union(domains->done,
2633 isl_set_copy(class_domain));
2635 class_domain = compute_atomic_domain(domains, class_domain);
2636 class_domain = compute_unroll_domains(domains, class_domain);
2638 domain = isl_set_copy(class_domain);
2640 if (compute_separate_domain(domains, domain) < 0)
2641 goto error;
2642 domain = isl_set_subtract(domain,
2643 isl_set_copy(domains->option[separate]));
2645 domain = isl_set_intersect(domain,
2646 isl_set_copy(domains->schedule_domain));
2648 domain = isl_ast_build_eliminate(domains->build, domain);
2649 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2651 domain = isl_set_coalesce(domain);
2652 domain = isl_set_make_disjoint(domain);
2654 list = isl_basic_set_list_from_set(domain);
2655 domains->list = isl_basic_set_list_concat(domains->list, list);
2657 isl_set_free(class_domain);
2659 return 0;
2660 error:
2661 isl_set_free(domain);
2662 isl_set_free(class_domain);
2663 return -1;
2666 /* Split up the domain at the current depth into disjoint
2667 * basic sets for which code should be generated separately
2668 * for the separation class identified by "pnt".
2670 * We extract the corresponding class domain from domains->sep_class,
2671 * eliminate inner dimensions and pass control to compute_partial_domains.
2673 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2675 struct isl_codegen_domains *domains = user;
2676 isl_set *class_set;
2677 isl_set *domain;
2678 int disjoint;
2680 class_set = isl_set_from_point(pnt);
2681 domain = isl_map_domain(isl_map_intersect_range(
2682 isl_map_copy(domains->sep_class), class_set));
2683 domain = isl_ast_build_compute_gist(domains->build, domain);
2684 domain = isl_ast_build_eliminate(domains->build, domain);
2686 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2687 if (disjoint < 0)
2688 return -1;
2689 if (disjoint) {
2690 isl_set_free(domain);
2691 return 0;
2694 return compute_partial_domains(domains, domain);
2697 /* Extract the domains at the current depth that should be atomic,
2698 * separated or unrolled and store them in option.
2700 * The domains specified by the user might overlap, so we make
2701 * them disjoint by subtracting earlier domains from later domains.
2703 static void compute_domains_init_options(isl_set *option[3],
2704 __isl_keep isl_ast_build *build)
2706 enum isl_ast_build_domain_type type, type2;
2708 for (type = atomic; type <= separate; ++type) {
2709 option[type] = isl_ast_build_get_option_domain(build, type);
2710 for (type2 = atomic; type2 < type; ++type2)
2711 option[type] = isl_set_subtract(option[type],
2712 isl_set_copy(option[type2]));
2715 option[unroll] = isl_set_coalesce(option[unroll]);
2716 option[unroll] = isl_set_make_disjoint(option[unroll]);
2719 /* Split up the domain at the current depth into disjoint
2720 * basic sets for which code should be generated separately,
2721 * based on the user-specified options.
2722 * Return the list of disjoint basic sets.
2724 * There are three kinds of domains that we need to keep track of.
2725 * - the "schedule domain" is the domain of "executed"
2726 * - the "class domain" is the domain corresponding to the currrent
2727 * separation class
2728 * - the "option domain" is the domain corresponding to one of the options
2729 * atomic, unroll or separate
2731 * We first consider the individial values of the separation classes
2732 * and split up the domain for each of them separately.
2733 * Finally, we consider the remainder. If no separation classes were
2734 * specified, then we call compute_partial_domains with the universe
2735 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2736 * with inner dimensions removed. We do this because we want to
2737 * avoid computing the complement of the class domains (i.e., the difference
2738 * between the universe and domains->done).
2740 static __isl_give isl_basic_set_list *compute_domains(
2741 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2743 struct isl_codegen_domains domains;
2744 isl_ctx *ctx;
2745 isl_set *domain;
2746 isl_union_set *schedule_domain;
2747 isl_set *classes;
2748 isl_space *space;
2749 int n_param;
2750 enum isl_ast_build_domain_type type;
2751 int empty;
2753 if (!executed)
2754 return NULL;
2756 ctx = isl_union_map_get_ctx(executed);
2757 domains.list = isl_basic_set_list_alloc(ctx, 0);
2759 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
2760 domain = isl_set_from_union_set(schedule_domain);
2762 compute_domains_init_options(domains.option, build);
2764 domains.sep_class = isl_ast_build_get_separation_class(build);
2765 classes = isl_map_range(isl_map_copy(domains.sep_class));
2766 n_param = isl_set_dim(classes, isl_dim_param);
2767 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
2769 space = isl_set_get_space(domain);
2770 domains.build = build;
2771 domains.schedule_domain = isl_set_copy(domain);
2772 domains.executed = executed;
2773 domains.done = isl_set_empty(space);
2775 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
2776 domains.list = isl_basic_set_list_free(domains.list);
2777 isl_set_free(classes);
2779 empty = isl_set_is_empty(domains.done);
2780 if (empty < 0) {
2781 domains.list = isl_basic_set_list_free(domains.list);
2782 domain = isl_set_free(domain);
2783 } else if (empty) {
2784 isl_set_free(domain);
2785 domain = isl_set_universe(isl_set_get_space(domains.done));
2786 } else {
2787 domain = isl_ast_build_eliminate(build, domain);
2789 if (compute_partial_domains(&domains, domain) < 0)
2790 domains.list = isl_basic_set_list_free(domains.list);
2792 isl_set_free(domains.schedule_domain);
2793 isl_set_free(domains.done);
2794 isl_map_free(domains.sep_class);
2795 for (type = atomic; type <= separate; ++type)
2796 isl_set_free(domains.option[type]);
2798 return domains.list;
2801 /* Generate code for a single component, after shifting (if any)
2802 * has been applied.
2804 * We first split up the domain at the current depth into disjoint
2805 * basic sets based on the user-specified options.
2806 * Then we generated code for each of them and concatenate the results.
2808 static __isl_give isl_ast_graft_list *generate_shifted_component(
2809 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
2811 isl_basic_set_list *domain_list;
2812 isl_ast_graft_list *list = NULL;
2814 domain_list = compute_domains(executed, build);
2815 list = generate_parallel_domains(domain_list, executed, build);
2817 isl_basic_set_list_free(domain_list);
2818 isl_union_map_free(executed);
2819 isl_ast_build_free(build);
2821 return list;
2824 struct isl_set_map_pair {
2825 isl_set *set;
2826 isl_map *map;
2829 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2830 * of indices into the "domain" array,
2831 * return the union of the "map" fields of the elements
2832 * indexed by the first "n" elements of "order".
2834 static __isl_give isl_union_map *construct_component_executed(
2835 struct isl_set_map_pair *domain, int *order, int n)
2837 int i;
2838 isl_map *map;
2839 isl_union_map *executed;
2841 map = isl_map_copy(domain[order[0]].map);
2842 executed = isl_union_map_from_map(map);
2843 for (i = 1; i < n; ++i) {
2844 map = isl_map_copy(domain[order[i]].map);
2845 executed = isl_union_map_add_map(executed, map);
2848 return executed;
2851 /* Generate code for a single component, after shifting (if any)
2852 * has been applied.
2854 * The component inverse schedule is specified as the "map" fields
2855 * of the elements of "domain" indexed by the first "n" elements of "order".
2857 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
2858 struct isl_set_map_pair *domain, int *order, int n,
2859 __isl_take isl_ast_build *build)
2861 isl_union_map *executed;
2863 executed = construct_component_executed(domain, order, n);
2864 return generate_shifted_component(executed, build);
2867 /* Does set dimension "pos" of "set" have an obviously fixed value?
2869 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
2871 int fixed;
2872 isl_val *v;
2874 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
2875 if (!v)
2876 return -1;
2877 fixed = !isl_val_is_nan(v);
2878 isl_val_free(v);
2880 return fixed;
2883 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2884 * of indices into the "domain" array,
2885 * do all (except for at most one) of the "set" field of the elements
2886 * indexed by the first "n" elements of "order" have a fixed value
2887 * at position "depth"?
2889 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
2890 int *order, int n, int depth)
2892 int i;
2893 int non_fixed = -1;
2895 for (i = 0; i < n; ++i) {
2896 int f;
2898 f = dim_is_fixed(domain[order[i]].set, depth);
2899 if (f < 0)
2900 return -1;
2901 if (f)
2902 continue;
2903 if (non_fixed >= 0)
2904 return 0;
2905 non_fixed = i;
2908 return 1;
2911 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2912 * of indices into the "domain" array,
2913 * eliminate the inner dimensions from the "set" field of the elements
2914 * indexed by the first "n" elements of "order", provided the current
2915 * dimension does not have a fixed value.
2917 * Return the index of the first element in "order" with a corresponding
2918 * "set" field that does not have an (obviously) fixed value.
2920 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
2921 int *order, int n, int depth, __isl_keep isl_ast_build *build)
2923 int i;
2924 int base = -1;
2926 for (i = n - 1; i >= 0; --i) {
2927 int f;
2928 f = dim_is_fixed(domain[order[i]].set, depth);
2929 if (f < 0)
2930 return -1;
2931 if (f)
2932 continue;
2933 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
2934 domain[order[i]].set);
2935 base = i;
2938 return base;
2941 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2942 * of indices into the "domain" array,
2943 * find the element of "domain" (amongst those indexed by the first "n"
2944 * elements of "order") with the "set" field that has the smallest
2945 * value for the current iterator.
2947 * Note that the domain with the smallest value may depend on the parameters
2948 * and/or outer loop dimension. Since the result of this function is only
2949 * used as heuristic, we only make a reasonable attempt at finding the best
2950 * domain, one that should work in case a single domain provides the smallest
2951 * value for the current dimension over all values of the parameters
2952 * and outer dimensions.
2954 * In particular, we compute the smallest value of the first domain
2955 * and replace it by that of any later domain if that later domain
2956 * has a smallest value that is smaller for at least some value
2957 * of the parameters and outer dimensions.
2959 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
2960 __isl_keep isl_ast_build *build)
2962 int i;
2963 isl_map *min_first;
2964 int first = 0;
2966 min_first = isl_ast_build_map_to_iterator(build,
2967 isl_set_copy(domain[order[0]].set));
2968 min_first = isl_map_lexmin(min_first);
2970 for (i = 1; i < n; ++i) {
2971 isl_map *min, *test;
2972 int empty;
2974 min = isl_ast_build_map_to_iterator(build,
2975 isl_set_copy(domain[order[i]].set));
2976 min = isl_map_lexmin(min);
2977 test = isl_map_copy(min);
2978 test = isl_map_apply_domain(isl_map_copy(min_first), test);
2979 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
2980 empty = isl_map_is_empty(test);
2981 isl_map_free(test);
2982 if (empty >= 0 && !empty) {
2983 isl_map_free(min_first);
2984 first = i;
2985 min_first = min;
2986 } else
2987 isl_map_free(min);
2989 if (empty < 0)
2990 break;
2993 isl_map_free(min_first);
2995 return i < n ? -1 : first;
2998 /* Construct a shifted inverse schedule based on the original inverse schedule,
2999 * the stride and the offset.
3001 * The original inverse schedule is specified as the "map" fields
3002 * of the elements of "domain" indexed by the first "n" elements of "order".
3004 * "stride" and "offset" are such that the difference
3005 * between the values of the current dimension of domain "i"
3006 * and the values of the current dimension for some reference domain are
3007 * equal to
3009 * stride * integer + offset[i]
3011 * Moreover, 0 <= offset[i] < stride.
3013 * For each domain, we create a map
3015 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3017 * where j refers to the current dimension and the other dimensions are
3018 * unchanged, and apply this map to the original schedule domain.
3020 * For example, for the original schedule
3022 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3024 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3025 * we apply the mapping
3027 * { [j] -> [j, 0] }
3029 * to the schedule of the "A" domain and the mapping
3031 * { [j - 1] -> [j, 1] }
3033 * to the schedule of the "B" domain.
3036 * Note that after the transformation, the differences between pairs
3037 * of values of the current dimension over all domains are multiples
3038 * of stride and that we have therefore exposed the stride.
3041 * To see that the mapping preserves the lexicographic order,
3042 * first note that each of the individual maps above preserves the order.
3043 * If the value of the current iterator is j1 in one domain and j2 in another,
3044 * then if j1 = j2, we know that the same map is applied to both domains
3045 * and the order is preserved.
3046 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3047 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3049 * j1 - c1 < j2 - c2
3051 * and the order is preserved.
3052 * If c1 < c2, then we know
3054 * 0 <= c2 - c1 < s
3056 * We also have
3058 * j2 - j1 = n * s + r
3060 * with n >= 0 and 0 <= r < s.
3061 * In other words, r = c2 - c1.
3062 * If n > 0, then
3064 * j1 - c1 < j2 - c2
3066 * If n = 0, then
3068 * j1 - c1 = j2 - c2
3070 * and so
3072 * (j1 - c1, c1) << (j2 - c2, c2)
3074 * with "<<" the lexicographic order, proving that the order is preserved
3075 * in all cases.
3077 static __isl_give isl_union_map *contruct_shifted_executed(
3078 struct isl_set_map_pair *domain, int *order, int n,
3079 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3080 __isl_take isl_ast_build *build)
3082 int i;
3083 isl_union_map *executed;
3084 isl_space *space;
3085 isl_map *map;
3086 int depth;
3087 isl_constraint *c;
3089 depth = isl_ast_build_get_depth(build);
3090 space = isl_ast_build_get_space(build, 1);
3091 executed = isl_union_map_empty(isl_space_copy(space));
3092 space = isl_space_map_from_set(space);
3093 map = isl_map_identity(isl_space_copy(space));
3094 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3095 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3096 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3098 c = isl_equality_alloc(isl_local_space_from_space(space));
3099 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3100 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3102 for (i = 0; i < n; ++i) {
3103 isl_map *map_i;
3104 isl_val *v;
3106 v = isl_multi_val_get_val(offset, i);
3107 if (!v)
3108 break;
3109 map_i = isl_map_copy(map);
3110 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3111 isl_val_copy(v));
3112 v = isl_val_neg(v);
3113 c = isl_constraint_set_constant_val(c, v);
3114 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3116 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3117 map_i);
3118 executed = isl_union_map_add_map(executed, map_i);
3121 isl_constraint_free(c);
3122 isl_map_free(map);
3124 if (i < n)
3125 executed = isl_union_map_free(executed);
3127 return executed;
3130 /* Generate code for a single component, after exposing the stride,
3131 * given that the schedule domain is "shifted strided".
3133 * The component inverse schedule is specified as the "map" fields
3134 * of the elements of "domain" indexed by the first "n" elements of "order".
3136 * The schedule domain being "shifted strided" means that the differences
3137 * between the values of the current dimension of domain "i"
3138 * and the values of the current dimension for some reference domain are
3139 * equal to
3141 * stride * integer + offset[i]
3143 * We first look for the domain with the "smallest" value for the current
3144 * dimension and adjust the offsets such that the offset of the "smallest"
3145 * domain is equal to zero. The other offsets are reduced modulo stride.
3147 * Based on this information, we construct a new inverse schedule in
3148 * contruct_shifted_executed that exposes the stride.
3149 * Since this involves the introduction of a new schedule dimension,
3150 * the build needs to be changed accodingly.
3151 * After computing the AST, the newly introduced dimension needs
3152 * to be removed again from the list of grafts. We do this by plugging
3153 * in a mapping that represents the new schedule domain in terms of the
3154 * old schedule domain.
3156 static __isl_give isl_ast_graft_list *generate_shift_component(
3157 struct isl_set_map_pair *domain, int *order, int n,
3158 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3159 __isl_take isl_ast_build *build)
3161 isl_ast_graft_list *list;
3162 int first;
3163 int depth;
3164 isl_ctx *ctx;
3165 isl_val *val;
3166 isl_multi_val *mv;
3167 isl_space *space;
3168 isl_multi_aff *ma, *zero;
3169 isl_union_map *executed;
3171 ctx = isl_ast_build_get_ctx(build);
3172 depth = isl_ast_build_get_depth(build);
3174 first = first_offset(domain, order, n, build);
3175 if (first < 0)
3176 goto error;
3178 mv = isl_multi_val_copy(offset);
3179 val = isl_multi_val_get_val(offset, first);
3180 val = isl_val_neg(val);
3181 mv = isl_multi_val_add_val(mv, val);
3182 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3184 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3185 build);
3186 space = isl_ast_build_get_space(build, 1);
3187 space = isl_space_map_from_set(space);
3188 ma = isl_multi_aff_identity(isl_space_copy(space));
3189 space = isl_space_from_domain(isl_space_domain(space));
3190 space = isl_space_add_dims(space, isl_dim_out, 1);
3191 zero = isl_multi_aff_zero(space);
3192 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3193 build = isl_ast_build_insert_dim(build, depth + 1);
3194 list = generate_shifted_component(executed, build);
3196 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3198 isl_multi_val_free(mv);
3200 return list;
3201 error:
3202 isl_ast_build_free(build);
3203 return NULL;
3206 /* Generate code for a single component.
3208 * The component inverse schedule is specified as the "map" fields
3209 * of the elements of "domain" indexed by the first "n" elements of "order".
3211 * This function may modify the "set" fields of "domain".
3213 * Before proceeding with the actual code generation for the component,
3214 * we first check if there are any "shifted" strides, meaning that
3215 * the schedule domains of the individual domains are all strided,
3216 * but that they have different offsets, resulting in the union
3217 * of schedule domains not being strided anymore.
3219 * The simplest example is the schedule
3221 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3223 * Both schedule domains are strided, but their union is not.
3224 * This function detects such cases and then rewrites the schedule to
3226 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3228 * In the new schedule, the schedule domains have the same offset (modulo
3229 * the stride), ensuring that the union of schedule domains is also strided.
3232 * If there is only a single domain in the component, then there is
3233 * nothing to do. Similarly, if the current schedule dimension has
3234 * a fixed value for almost all domains then there is nothing to be done.
3235 * In particular, we need at least two domains where the current schedule
3236 * dimension does not have a fixed value.
3237 * Finally, if any of the options refer to the current schedule dimension,
3238 * then we bail out as well. It would be possible to reformulate the options
3239 * in terms of the new schedule domain, but that would introduce constraints
3240 * that separate the domains in the options and that is something we would
3241 * like to avoid.
3244 * To see if there is any shifted stride, we look at the differences
3245 * between the values of the current dimension in pairs of domains
3246 * for equal values of outer dimensions. These differences should be
3247 * of the form
3249 * m x + r
3251 * with "m" the stride and "r" a constant. Note that we cannot perform
3252 * this analysis on individual domains as the lower bound in each domain
3253 * may depend on parameters or outer dimensions and so the current dimension
3254 * itself may not have a fixed remainder on division by the stride.
3256 * In particular, we compare the first domain that does not have an
3257 * obviously fixed value for the current dimension to itself and all
3258 * other domains and collect the offsets and the gcd of the strides.
3259 * If the gcd becomes one, then we failed to find shifted strides.
3260 * If the gcd is zero, then the differences were all fixed, meaning
3261 * that some domains had non-obviously fixed values for the current dimension.
3262 * If all the offsets are the same (for those domains that do not have
3263 * an obviously fixed value for the current dimension), then we do not
3264 * apply the transformation.
3265 * If none of the domains were skipped, then there is nothing to do.
3266 * If some of them were skipped, then if we apply separation, the schedule
3267 * domain should get split in pieces with a (non-shifted) stride.
3269 * Otherwise, we apply a shift to expose the stride in
3270 * generate_shift_component.
3272 static __isl_give isl_ast_graft_list *generate_component(
3273 struct isl_set_map_pair *domain, int *order, int n,
3274 __isl_take isl_ast_build *build)
3276 int i, d;
3277 int depth;
3278 isl_ctx *ctx;
3279 isl_map *map;
3280 isl_set *deltas;
3281 isl_val *gcd = NULL;
3282 isl_multi_val *mv;
3283 int fixed, skip;
3284 int base;
3285 isl_ast_graft_list *list;
3286 int res = 0;
3288 depth = isl_ast_build_get_depth(build);
3290 skip = n == 1;
3291 if (skip >= 0 && !skip)
3292 skip = at_most_one_non_fixed(domain, order, n, depth);
3293 if (skip >= 0 && !skip)
3294 skip = isl_ast_build_options_involve_depth(build);
3295 if (skip < 0)
3296 goto error;
3297 if (skip)
3298 return generate_shifted_component_from_list(domain,
3299 order, n, build);
3301 base = eliminate_non_fixed(domain, order, n, depth, build);
3302 if (base < 0)
3303 goto error;
3305 ctx = isl_ast_build_get_ctx(build);
3307 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3309 fixed = 1;
3310 for (i = 0; i < n; ++i) {
3311 isl_val *r, *m;
3313 map = isl_map_from_domain_and_range(
3314 isl_set_copy(domain[order[base]].set),
3315 isl_set_copy(domain[order[i]].set));
3316 for (d = 0; d < depth; ++d)
3317 map = isl_map_equate(map, isl_dim_in, d,
3318 isl_dim_out, d);
3319 deltas = isl_map_deltas(map);
3320 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3321 isl_set_free(deltas);
3322 if (res < 0)
3323 break;
3325 if (i == 0)
3326 gcd = m;
3327 else
3328 gcd = isl_val_gcd(gcd, m);
3329 if (isl_val_is_one(gcd)) {
3330 isl_val_free(r);
3331 break;
3333 mv = isl_multi_val_set_val(mv, i, r);
3335 res = dim_is_fixed(domain[order[i]].set, depth);
3336 if (res < 0)
3337 break;
3338 if (res)
3339 continue;
3341 if (fixed && i > base) {
3342 isl_val *a, *b;
3343 a = isl_multi_val_get_val(mv, i);
3344 b = isl_multi_val_get_val(mv, base);
3345 if (isl_val_ne(a, b))
3346 fixed = 0;
3347 isl_val_free(a);
3348 isl_val_free(b);
3352 if (res < 0 || !gcd) {
3353 isl_ast_build_free(build);
3354 list = NULL;
3355 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3356 list = generate_shifted_component_from_list(domain,
3357 order, n, build);
3358 } else {
3359 list = generate_shift_component(domain, order, n, gcd, mv,
3360 build);
3363 isl_val_free(gcd);
3364 isl_multi_val_free(mv);
3366 return list;
3367 error:
3368 isl_ast_build_free(build);
3369 return NULL;
3372 /* Store both "map" itself and its domain in the
3373 * structure pointed to by *next and advance to the next array element.
3375 static int extract_domain(__isl_take isl_map *map, void *user)
3377 struct isl_set_map_pair **next = user;
3379 (*next)->map = isl_map_copy(map);
3380 (*next)->set = isl_map_domain(map);
3381 (*next)++;
3383 return 0;
3386 /* Internal data for any_scheduled_after.
3388 * "depth" is the number of loops that have already been generated
3389 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3390 * "domain" is an array of set-map pairs corresponding to the different
3391 * iteration domains. The set is the schedule domain, i.e., the domain
3392 * of the inverse schedule, while the map is the inverse schedule itself.
3394 struct isl_any_scheduled_after_data {
3395 int depth;
3396 int group_coscheduled;
3397 struct isl_set_map_pair *domain;
3400 /* Is any element of domain "i" scheduled after any element of domain "j"
3401 * (for a common iteration of the first data->depth loops)?
3403 * data->domain[i].set contains the domain of the inverse schedule
3404 * for domain "i", i.e., elements in the schedule domain.
3406 * If data->group_coscheduled is set, then we also return 1 if there
3407 * is any pair of elements in the two domains that are scheduled together.
3409 static int any_scheduled_after(int i, int j, void *user)
3411 struct isl_any_scheduled_after_data *data = user;
3412 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
3413 int pos;
3415 for (pos = data->depth; pos < dim; ++pos) {
3416 int follows;
3418 follows = isl_set_follows_at(data->domain[i].set,
3419 data->domain[j].set, pos);
3421 if (follows < -1)
3422 return -1;
3423 if (follows > 0)
3424 return 1;
3425 if (follows < 0)
3426 return 0;
3429 return data->group_coscheduled;
3432 /* Look for independent components at the current depth and generate code
3433 * for each component separately. The resulting lists of grafts are
3434 * merged in an attempt to combine grafts with identical guards.
3436 * Code for two domains can be generated separately if all the elements
3437 * of one domain are scheduled before (or together with) all the elements
3438 * of the other domain. We therefore consider the graph with as nodes
3439 * the domains and an edge between two nodes if any element of the first
3440 * node is scheduled after any element of the second node.
3441 * If the ast_build_group_coscheduled is set, then we also add an edge if
3442 * there is any pair of elements in the two domains that are scheduled
3443 * together.
3444 * Code is then generated (by generate_component)
3445 * for each of the strongly connected components in this graph
3446 * in their topological order.
3448 * Since the test is performed on the domain of the inverse schedules of
3449 * the different domains, we precompute these domains and store
3450 * them in data.domain.
3452 static __isl_give isl_ast_graft_list *generate_components(
3453 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3455 int i;
3456 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3457 int n = isl_union_map_n_map(executed);
3458 struct isl_any_scheduled_after_data data;
3459 struct isl_set_map_pair *next;
3460 struct isl_tarjan_graph *g = NULL;
3461 isl_ast_graft_list *list = NULL;
3462 int n_domain = 0;
3464 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
3465 if (!data.domain)
3466 goto error;
3467 n_domain = n;
3469 next = data.domain;
3470 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
3471 goto error;
3473 if (!build)
3474 goto error;
3475 data.depth = isl_ast_build_get_depth(build);
3476 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
3477 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
3478 if (!g)
3479 goto error;
3481 list = isl_ast_graft_list_alloc(ctx, 0);
3483 i = 0;
3484 while (list && n) {
3485 isl_ast_graft_list *list_c;
3486 int first = i;
3488 if (g->order[i] == -1)
3489 isl_die(ctx, isl_error_internal, "cannot happen",
3490 goto error);
3491 ++i; --n;
3492 while (g->order[i] != -1) {
3493 ++i; --n;
3496 list_c = generate_component(data.domain,
3497 g->order + first, i - first,
3498 isl_ast_build_copy(build));
3499 list = isl_ast_graft_list_merge(list, list_c, build);
3501 ++i;
3504 if (0)
3505 error: list = isl_ast_graft_list_free(list);
3506 isl_tarjan_graph_free(g);
3507 for (i = 0; i < n_domain; ++i) {
3508 isl_map_free(data.domain[i].map);
3509 isl_set_free(data.domain[i].set);
3511 free(data.domain);
3512 isl_union_map_free(executed);
3513 isl_ast_build_free(build);
3515 return list;
3518 /* Generate code for the next level (and all inner levels).
3520 * If "executed" is empty, i.e., no code needs to be generated,
3521 * then we return an empty list.
3523 * If we have already generated code for all loop levels, then we pass
3524 * control to generate_inner_level.
3526 * If "executed" lives in a single space, i.e., if code needs to be
3527 * generated for a single domain, then there can only be a single
3528 * component and we go directly to generate_shifted_component.
3529 * Otherwise, we call generate_components to detect the components
3530 * and to call generate_component on each of them separately.
3532 static __isl_give isl_ast_graft_list *generate_next_level(
3533 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3535 int depth;
3537 if (!build || !executed)
3538 goto error;
3540 if (isl_union_map_is_empty(executed)) {
3541 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3542 isl_union_map_free(executed);
3543 isl_ast_build_free(build);
3544 return isl_ast_graft_list_alloc(ctx, 0);
3547 depth = isl_ast_build_get_depth(build);
3548 if (depth >= isl_set_dim(build->domain, isl_dim_set))
3549 return generate_inner_level(executed, build);
3551 if (isl_union_map_n_map(executed) == 1)
3552 return generate_shifted_component(executed, build);
3554 return generate_components(executed, build);
3555 error:
3556 isl_union_map_free(executed);
3557 isl_ast_build_free(build);
3558 return NULL;
3561 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3562 * internal, executed and build are the inputs to generate_code.
3563 * list collects the output.
3565 struct isl_generate_code_data {
3566 int internal;
3567 isl_union_map *executed;
3568 isl_ast_build *build;
3570 isl_ast_graft_list *list;
3573 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3575 * [E -> S] -> D
3577 * with E the external build schedule and S the additional schedule "space",
3578 * reformulate the inverse schedule in terms of the internal schedule domain,
3579 * i.e., return
3581 * [I -> S] -> D
3583 * We first obtain a mapping
3585 * I -> E
3587 * take the inverse and the product with S -> S, resulting in
3589 * [I -> S] -> [E -> S]
3591 * Applying the map to the input produces the desired result.
3593 static __isl_give isl_union_map *internal_executed(
3594 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
3595 __isl_keep isl_ast_build *build)
3597 isl_map *id, *proj;
3599 proj = isl_ast_build_get_schedule_map(build);
3600 proj = isl_map_reverse(proj);
3601 space = isl_space_map_from_set(isl_space_copy(space));
3602 id = isl_map_identity(space);
3603 proj = isl_map_product(proj, id);
3604 executed = isl_union_map_apply_domain(executed,
3605 isl_union_map_from_map(proj));
3606 return executed;
3609 /* Generate an AST that visits the elements in the range of data->executed
3610 * in the relative order specified by the corresponding image element(s)
3611 * for those image elements that belong to "set".
3612 * Add the result to data->list.
3614 * The caller ensures that "set" is a universe domain.
3615 * "space" is the space of the additional part of the schedule.
3616 * It is equal to the space of "set" if build->domain is parametric.
3617 * Otherwise, it is equal to the range of the wrapped space of "set".
3619 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3620 * was called from an outside user (data->internal not set), then
3621 * the (inverse) schedule refers to the external build domain and needs to
3622 * be transformed to refer to the internal build domain.
3624 * The build is extended to include the additional part of the schedule.
3625 * If the original build space was not parametric, then the options
3626 * in data->build refer only to the additional part of the schedule
3627 * and they need to be adjusted to refer to the complete AST build
3628 * domain.
3630 * After having adjusted inverse schedule and build, we start generating
3631 * code with the outer loop of the current code generation
3632 * in generate_next_level.
3634 * If the original build space was not parametric, we undo the embedding
3635 * on the resulting isl_ast_node_list so that it can be used within
3636 * the outer AST build.
3638 static int generate_code_in_space(struct isl_generate_code_data *data,
3639 __isl_take isl_set *set, __isl_take isl_space *space)
3641 isl_union_map *executed;
3642 isl_ast_build *build;
3643 isl_ast_graft_list *list;
3644 int embed;
3646 executed = isl_union_map_copy(data->executed);
3647 executed = isl_union_map_intersect_domain(executed,
3648 isl_union_set_from_set(set));
3650 embed = !isl_set_is_params(data->build->domain);
3651 if (embed && !data->internal)
3652 executed = internal_executed(executed, space, data->build);
3654 build = isl_ast_build_copy(data->build);
3655 build = isl_ast_build_product(build, space);
3657 list = generate_next_level(executed, build);
3659 list = isl_ast_graft_list_unembed(list, embed);
3661 data->list = isl_ast_graft_list_concat(data->list, list);
3663 return 0;
3666 /* Generate an AST that visits the elements in the range of data->executed
3667 * in the relative order specified by the corresponding domain element(s)
3668 * for those domain elements that belong to "set".
3669 * Add the result to data->list.
3671 * The caller ensures that "set" is a universe domain.
3673 * If the build space S is not parametric, then the space of "set"
3674 * need to be a wrapped relation with S as domain. That is, it needs
3675 * to be of the form
3677 * [S -> T]
3679 * Check this property and pass control to generate_code_in_space
3680 * passing along T.
3681 * If the build space is not parametric, then T is the space of "set".
3683 static int generate_code_set(__isl_take isl_set *set, void *user)
3685 struct isl_generate_code_data *data = user;
3686 isl_space *space, *build_space;
3687 int is_domain;
3689 space = isl_set_get_space(set);
3691 if (isl_set_is_params(data->build->domain))
3692 return generate_code_in_space(data, set, space);
3694 build_space = isl_ast_build_get_space(data->build, data->internal);
3695 space = isl_space_unwrap(space);
3696 is_domain = isl_space_is_domain(build_space, space);
3697 isl_space_free(build_space);
3698 space = isl_space_range(space);
3700 if (is_domain < 0)
3701 goto error;
3702 if (!is_domain)
3703 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3704 "invalid nested schedule space", goto error);
3706 return generate_code_in_space(data, set, space);
3707 error:
3708 isl_set_free(set);
3709 isl_space_free(space);
3710 return -1;
3713 /* Generate an AST that visits the elements in the range of "executed"
3714 * in the relative order specified by the corresponding domain element(s).
3716 * "build" is an isl_ast_build that has either been constructed by
3717 * isl_ast_build_from_context or passed to a callback set by
3718 * isl_ast_build_set_create_leaf.
3719 * In the first case, the space of the isl_ast_build is typically
3720 * a parametric space, although this is currently not enforced.
3721 * In the second case, the space is never a parametric space.
3722 * If the space S is not parametric, then the domain space(s) of "executed"
3723 * need to be wrapped relations with S as domain.
3725 * If the domain of "executed" consists of several spaces, then an AST
3726 * is generated for each of them (in arbitrary order) and the results
3727 * are concatenated.
3729 * If "internal" is set, then the domain "S" above refers to the internal
3730 * schedule domain representation. Otherwise, it refers to the external
3731 * representation, as returned by isl_ast_build_get_schedule_space.
3733 * We essentially run over all the spaces in the domain of "executed"
3734 * and call generate_code_set on each of them.
3736 static __isl_give isl_ast_graft_list *generate_code(
3737 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3738 int internal)
3740 isl_ctx *ctx;
3741 struct isl_generate_code_data data = { 0 };
3742 isl_space *space;
3743 isl_union_set *schedule_domain;
3744 isl_union_map *universe;
3746 if (!build)
3747 goto error;
3748 space = isl_ast_build_get_space(build, 1);
3749 space = isl_space_align_params(space,
3750 isl_union_map_get_space(executed));
3751 space = isl_space_align_params(space,
3752 isl_union_map_get_space(build->options));
3753 build = isl_ast_build_align_params(build, isl_space_copy(space));
3754 executed = isl_union_map_align_params(executed, space);
3755 if (!executed || !build)
3756 goto error;
3758 ctx = isl_ast_build_get_ctx(build);
3760 data.internal = internal;
3761 data.executed = executed;
3762 data.build = build;
3763 data.list = isl_ast_graft_list_alloc(ctx, 0);
3765 universe = isl_union_map_universe(isl_union_map_copy(executed));
3766 schedule_domain = isl_union_map_domain(universe);
3767 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
3768 &data) < 0)
3769 data.list = isl_ast_graft_list_free(data.list);
3771 isl_union_set_free(schedule_domain);
3772 isl_union_map_free(executed);
3774 isl_ast_build_free(build);
3775 return data.list;
3776 error:
3777 isl_union_map_free(executed);
3778 isl_ast_build_free(build);
3779 return NULL;
3782 /* Generate an AST that visits the elements in the domain of "schedule"
3783 * in the relative order specified by the corresponding image element(s).
3785 * "build" is an isl_ast_build that has either been constructed by
3786 * isl_ast_build_from_context or passed to a callback set by
3787 * isl_ast_build_set_create_leaf.
3788 * In the first case, the space of the isl_ast_build is typically
3789 * a parametric space, although this is currently not enforced.
3790 * In the second case, the space is never a parametric space.
3791 * If the space S is not parametric, then the range space(s) of "schedule"
3792 * need to be wrapped relations with S as domain.
3794 * If the range of "schedule" consists of several spaces, then an AST
3795 * is generated for each of them (in arbitrary order) and the results
3796 * are concatenated.
3798 * We first initialize the local copies of the relevant options.
3799 * We do this here rather than when the isl_ast_build is created
3800 * because the options may have changed between the construction
3801 * of the isl_ast_build and the call to isl_generate_code.
3803 * The main computation is performed on an inverse schedule (with
3804 * the schedule domain in the domain and the elements to be executed
3805 * in the range) called "executed".
3807 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
3808 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
3810 isl_ast_graft_list *list;
3811 isl_ast_node *node;
3812 isl_union_map *executed;
3814 build = isl_ast_build_copy(build);
3815 build = isl_ast_build_set_single_valued(build, 0);
3816 schedule = isl_union_map_coalesce(schedule);
3817 executed = isl_union_map_reverse(schedule);
3818 list = generate_code(executed, isl_ast_build_copy(build), 0);
3819 node = isl_ast_node_from_graft_list(list, build);
3820 isl_ast_build_free(build);
3822 return node;